Files
wehub-resource-sync 4b6817381b
CI (OpenClaw E2E) / openclaw test (push) Has been cancelled
CI / coverage-report (push) Has been cancelled
CI / test-kubernetes (push) Has been cancelled
CI / should-run-thorough (push) Has been cancelled
CI / test-thorough (cloudwatch-demo) (push) Has been cancelled
CI / test-thorough (flink-ecs) (push) Has been cancelled
CI / test-thorough (upstream-lambda) (push) Has been cancelled
CI / test-thorough (prefect-ecs-fargate) (push) Has been cancelled
Release / build-binaries (zip, opensre.exe, onefile, windows-latest, windows-x64) (push) Has been cancelled
Benchmark image — build + push to ECR (any adapter) / build + push (push) Has been cancelled
CI / quality (ubuntu-latest) (push) Has been cancelled
CI / test (tools-runtime) (push) Has been cancelled
CI / test (e2e-general) (push) Has been cancelled
CI / test (cli-runtime) (push) Has been cancelled
CI / test (e2e-provider-and-openclaw) (push) Has been cancelled
CI / test (integrations-and-misc) (push) Has been cancelled
Release / verify (push) Has been cancelled
Release / build-python-dist (push) Has been cancelled
Release / build-binaries (tar.gz, opensre, onedir, macos-15-intel, darwin-x64) (push) Has been cancelled
Release / build-binaries (tar.gz, opensre, onedir, macos-latest, darwin-arm64) (push) Has been cancelled
Release / build-binaries (tar.gz, opensre, onedir, ubuntu-22.04, linux-x64) (push) Has been cancelled
Release / publish-release (push) Has been cancelled
Release / publish-main-release (push) Has been cancelled
Interactive Shell Live (PR + post-merge) / turn-checks (no-LLM) (push) Has been cancelled
CodeQL / Analyze (python) (push) Has been cancelled
Interactive Shell Live (PR + post-merge) / turn-live shard ${{ matrix.shard_index }} (push) Has been cancelled
Release / prepare (push) Has been cancelled
Release / build-binaries (tar.gz, opensre, onedir, ubuntu-22.04-arm, linux-arm64) (push) Has been cancelled
Synthetic Deterministic Tests / Synthetic offline (deterministic) (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 13:10:45 +08:00

76 lines
2.6 KiB
Python

"""LLM provider auth-method selection.
The public provider remains the vendor name (for example ``openai``), while
some auth methods use a provider-specific runtime backend under the hood.
"""
from __future__ import annotations
import os
from typing import Literal
LLM_AUTH_METHOD_ENV = "LLM_AUTH_METHOD"
LLMAuthMethod = Literal["api_key", "oauth"]
API_KEY_AUTH_METHOD: LLMAuthMethod = "api_key"
OAUTH_AUTH_METHOD: LLMAuthMethod = "oauth"
OAUTH_BACKEND_PROVIDER_BY_PROVIDER: dict[str, str] = {
"openai": "codex",
"anthropic": "claude-code",
}
OAUTH_PROVIDER_BY_BACKEND_PROVIDER: dict[str, str] = {
backend: provider for provider, backend in OAUTH_BACKEND_PROVIDER_BY_PROVIDER.items()
}
def normalize_llm_auth_method(value: str | None) -> LLMAuthMethod:
"""Return a supported auth method, defaulting to API-key auth."""
normalized = (value or "").strip().lower()
if normalized == OAUTH_AUTH_METHOD:
return OAUTH_AUTH_METHOD
return API_KEY_AUTH_METHOD
def supports_oauth_auth_method(provider: str) -> bool:
"""Whether onboarding exposes OAuth for this public provider."""
return provider.strip().lower() in OAUTH_BACKEND_PROVIDER_BY_PROVIDER
def canonical_llm_provider(provider: str) -> str:
"""Map legacy OAuth backend provider values to their public provider."""
normalized_provider = provider.strip().lower()
return OAUTH_PROVIDER_BY_BACKEND_PROVIDER.get(normalized_provider, normalized_provider)
def get_configured_llm_auth_method(provider: str | None = None) -> LLMAuthMethod:
"""Return the active auth method from env, with legacy CLI compatibility."""
normalized_provider = (provider or os.getenv("LLM_PROVIDER") or "").strip().lower()
if normalized_provider in OAUTH_PROVIDER_BY_BACKEND_PROVIDER:
return OAUTH_AUTH_METHOD
return normalize_llm_auth_method(os.getenv(LLM_AUTH_METHOD_ENV))
def effective_llm_provider(provider: str, auth_method: str | None = None) -> str:
"""Map a public provider/auth pair to the runtime provider implementation."""
normalized_provider = provider.strip().lower()
method = normalize_llm_auth_method(auth_method)
if method == OAUTH_AUTH_METHOD:
return OAUTH_BACKEND_PROVIDER_BY_PROVIDER.get(normalized_provider, normalized_provider)
return normalized_provider
__all__ = [
"API_KEY_AUTH_METHOD",
"LLM_AUTH_METHOD_ENV",
"LLMAuthMethod",
"OAUTH_AUTH_METHOD",
"OAUTH_BACKEND_PROVIDER_BY_PROVIDER",
"OAUTH_PROVIDER_BY_BACKEND_PROVIDER",
"canonical_llm_provider",
"effective_llm_provider",
"get_configured_llm_auth_method",
"normalize_llm_auth_method",
"supports_oauth_auth_method",
]