diff --git a/litellm/litellm_core_utils/clientside_credential_handler.py b/litellm/litellm_core_utils/clientside_credential_handler.py index 2bd58405693..5f668e135dd 100644 --- a/litellm/litellm_core_utils/clientside_credential_handler.py +++ b/litellm/litellm_core_utils/clientside_credential_handler.py @@ -11,7 +11,7 @@ If given, generate a unique model_id for the deployment. Ensures cooldowns are applied correctly. """ -from collections.abc import MutableMapping +from collections.abc import Mapping, MutableMapping from typing import Final, cast # noqa: TID251 # narrows the untyped request dict for mutation from litellm.exceptions import BadRequestError @@ -84,6 +84,16 @@ def _admin_config_fields_to_clear_on_base_override() -> list[str]: _ADMIN_CONFIG_FIELDS_TO_CLEAR_ON_BASE_OVERRIDE: Final = _admin_config_fields_to_clear_on_base_override() +def github_copilot_caller_endpoint_keys( + deployment_litellm_params: Mapping[str, object], request_kwargs: Mapping[str, object] +) -> tuple[str, ...]: + model: Final = deployment_litellm_params.get("model") + is_github_copilot: Final = deployment_litellm_params.get("custom_llm_provider") == "github_copilot" or ( + isinstance(model, str) and model.startswith("github_copilot/") + ) + return tuple(key for key in ("api_base", "base_url") if is_github_copilot and request_kwargs.get(key)) + + def is_clientside_credential(request_kwargs: dict) -> bool: """ Check if the credential is a clientside credential. @@ -130,8 +140,8 @@ def get_dynamic_litellm_params(litellm_params: dict, request_kwargs: dict) -> di # A per-user OAuth credential must survive the clear: dropping # litellm_credential_name / github_copilot_auth_type would flip the call to # shared mode and send the admin's Copilot token to the redirected host. - # Per-user mode ignores the caller's api_base anyway (the session's - # validated host wins), so keeping the mode is fail-closed. + # Per-user mode ignores the caller's api_base anyway (only the + # operator-configured Copilot base is used), so keeping the mode is fail-closed. credential_name_value: Final[object] = litellm_params.get( # pyright: ignore[reportUnknownMemberType, reportUnknownVariableType] # litellm_params is the untyped request dict "litellm_credential_name" ) diff --git a/litellm/litellm_core_utils/exception_mapping_utils.py b/litellm/litellm_core_utils/exception_mapping_utils.py index 6d39f272f3e..8de992bd2ca 100644 --- a/litellm/litellm_core_utils/exception_mapping_utils.py +++ b/litellm/litellm_core_utils/exception_mapping_utils.py @@ -2548,6 +2548,15 @@ def exception_type( return original_exception if _is_guardrail_block(original_exception): return original_exception + if isinstance(original_exception, Exception) and getattr(original_exception, "status_code", None) == 401: + from litellm.llms.github_copilot.per_user_auth import github_copilot_caller_rejection + + github_copilot_rejection: Final = github_copilot_caller_rejection( + original_exception, + cast(object, extra_kwargs), # cast-ok: extra_kwargs arrives as an untyped request dict + ) + if github_copilot_rejection is not None: + raise github_copilot_rejection from original_exception if isinstance(original_exception, ImportError) and ( original_exception.name in ("boto3", "botocore") or custom_llm_provider in ("bedrock", "bedrock_mantle") ): diff --git a/litellm/litellm_core_utils/get_llm_provider_logic.py b/litellm/litellm_core_utils/get_llm_provider_logic.py index 1ffb5e2f8e5..aef1cbbbb31 100644 --- a/litellm/litellm_core_utils/get_llm_provider_logic.py +++ b/litellm/litellm_core_utils/get_llm_provider_logic.py @@ -835,7 +835,7 @@ def _get_openai_compatible_provider_info( api_base = api_base or get_secret("GALADRIEL_API_BASE") or "https://api.galadriel.com/v1" dynamic_api_key = api_key or get_secret_str("GALADRIEL_API_KEY") elif custom_llm_provider == "github_copilot": - from litellm.llms.github_copilot.common_utils import DEFAULT_GITHUB_COPILOT_API_BASE + from litellm.llms.github_copilot.common_utils import DEFAULT_GITHUB_COPILOT_API_BASE, get_copilot_api_base from litellm.llms.github_copilot.per_user_auth import ( github_copilot_per_user_credential_name, github_copilot_user_session_from, @@ -845,7 +845,9 @@ def _get_openai_compatible_provider_info( cast("dict[str, object]", litellm_params) # cast-ok: litellm_params arrives as an untyped request dict ) if user_session is not None: - api_base = user_session.api_base # rebind-ok: resolves provider args in place + api_base = get_copilot_api_base( # rebind-ok: resolves provider args in place + None, lambda: user_session.token, user_session.endpoints + ) dynamic_api_key = user_session.token # rebind-ok: resolves provider args in place elif ( github_copilot_per_user_credential_name( @@ -855,7 +857,7 @@ def _get_openai_compatible_provider_info( ): # per-user deployments resolve no token at provider-info time; the # caller's session supplies base + key per request - api_base = api_base or DEFAULT_GITHUB_COPILOT_API_BASE # rebind-ok: resolves provider args in place + api_base = DEFAULT_GITHUB_COPILOT_API_BASE # rebind-ok: resolves provider args in place dynamic_api_key = None # rebind-ok: resolves provider args in place else: ( diff --git a/litellm/litellm_core_utils/llm_response_utils/get_api_base.py b/litellm/litellm_core_utils/llm_response_utils/get_api_base.py index 4d731b5e63a..5b39e14548b 100644 --- a/litellm/litellm_core_utils/llm_response_utils/get_api_base.py +++ b/litellm/litellm_core_utils/llm_response_utils/get_api_base.py @@ -10,9 +10,11 @@ from ...litellm_core_utils.get_llm_provider_logic import ( from ...types.router import LiteLLM_Params -def _api_base_without_login(provider: str) -> str | None: +def _api_base_without_login(provider: str, params: LiteLLM_Params) -> str | None: if provider == "github_copilot": - return litellm.GithubCopilotConfig().api_base_without_login() + from litellm.llms.github_copilot.per_user_auth import github_copilot_user_session_from + + return litellm.GithubCopilotConfig().api_base_without_login(github_copilot_user_session_from(params)) if provider == "chatgpt": return litellm.ChatGPTConfig().api_base_without_login() return None @@ -68,7 +70,7 @@ def get_api_base(model: str, optional_params: dict | LiteLLM_Params) -> str | No model = litellm.model_alias_map[model] declared: Final = declared_authenticating_provider(model, _optional_params.custom_llm_provider) if declared is not None: - return _api_base_without_login(declared) + return _api_base_without_login(declared, _optional_params) try: ( model, diff --git a/litellm/llms/github_copilot/authenticator.py b/litellm/llms/github_copilot/authenticator.py index 3af3b50ed8f..d585697dd29 100644 --- a/litellm/llms/github_copilot/authenticator.py +++ b/litellm/llms/github_copilot/authenticator.py @@ -1,30 +1,23 @@ -import hashlib import json import os import time from collections.abc import Mapping -from typing import Final, Protocol, cast # noqa: TID251 # pins untyped clients and caches to local Protocols +from typing import Final import httpx from pydantic import ConfigDict, TypeAdapter, with_config from typing_extensions import NotRequired, ReadOnly, TypedDict from litellm._logging import verbose_logger -from litellm.caching.in_memory_cache import InMemoryCache -from litellm.constants import GITHUB_COPILOT_USER_SESSION_TTL_SECONDS from litellm.litellm_core_utils.asyncify import can_block_current_thread from litellm.llms.custom_httpx.http_handler import get_httpx_client from .common_utils import ( GetAccessTokenError, GetDeviceCodeError, + get_copilot_auth_headers, ) - -# Constants (default values — overridable via environment variables at call time) -DEFAULT_GITHUB_CLIENT_ID: Final = "Iv1.b507a08c87ecfe98" -DEFAULT_GITHUB_DEVICE_CODE_URL: Final = "https://github.com/login/device/code" -DEFAULT_GITHUB_ACCESS_TOKEN_URL: Final = "https://github.com/login/oauth/access_token" -DEFAULT_GITHUB_COPILOT_USER_URL: Final = "https://api.github.com/copilot_internal/user" +from .github_host import DEFAULT_GITHUB_ACCESS_TOKEN_URL, DEFAULT_GITHUB_CLIENT_ID, DEFAULT_GITHUB_DEVICE_CODE_URL def _read_token_file(path: str) -> str: @@ -53,75 +46,6 @@ _DEVICE_CODE: Final = TypeAdapter(_DeviceCode) _ACCESS_TOKEN_POLL: Final = TypeAdapter(_AccessTokenPoll) -class _CopilotAPIBaseCache(Protocol): - def get_cache(self, key: str) -> object: ... - - def set_cache(self, key: str, value: str, ttl: float | None = None) -> object: ... - - -class _SyncGetClient(Protocol): - def get(self, url: str, *, headers: Mapping[str, str]) -> httpx.Response: ... - - -_COPILOT_API_BASE_CACHE: Final[_CopilotAPIBaseCache] = cast( # cast-ok: [LIT006] InMemoryCache methods lack type stubs - _CopilotAPIBaseCache, - InMemoryCache( - max_size_in_memory=1000, - default_ttl=GITHUB_COPILOT_USER_SESSION_TTL_SECONDS, - ), -) - - -def _copilot_api_base_for_token(access_token: str) -> str | None: - cache_key: Final = hashlib.sha256(access_token.encode()).hexdigest() - cached_api_base: Final[object] = _COPILOT_API_BASE_CACHE.get_cache(cache_key) - if isinstance(cached_api_base, str): - return cached_api_base - - try: - response: Final = cast( # cast-ok: [LIT006] HTTPHandler.get has no return annotation - _SyncGetClient, - get_httpx_client(), - ).get( - DEFAULT_GITHUB_COPILOT_USER_URL, - headers=github_api_headers(access_token), - ) - if not response.is_success: - return None - payload: Final[Mapping[str, object]] = _JSON_OBJECT.validate_python(response.json()) - endpoints: Final[Mapping[str, object]] = _JSON_OBJECT.validate_python(payload.get("endpoints")) - api_base: Final[object] = endpoints.get("api") - if not isinstance(api_base, str): - return None - from .github_host import github_host_endpoints - from .per_user_auth import validated_copilot_api_base - - validated: Final = validated_copilot_api_base(api_base, github_host_endpoints("github.com")) - if validated != api_base.rstrip("/"): - return None - _COPILOT_API_BASE_CACHE.set_cache(cache_key, validated) - except Exception: - return None - else: - return validated - - -def github_api_headers( - access_token: str | None = None, -) -> dict[str, str]: # mutable-ok: returned straight into httpx handlers whose headers params require dict - headers: Final = { - "accept": "application/json", - "editor-version": "vscode/1.85.1", - "editor-plugin-version": "copilot/1.155.0", - "user-agent": "GithubCopilot/1.155.0", - "accept-encoding": "gzip,deflate,br", - "content-type": "application/json", - } - if access_token: - headers["authorization"] = f"token {access_token}" - return headers - - class Authenticator: def __init__(self) -> None: """Initialize the GitHub Copilot authenticator with configurable token paths.""" @@ -136,6 +60,15 @@ class Authenticator: ) self._ensure_token_dir() + def stored_access_token(self) -> str | None: + try: + return _read_token_file(self.access_token_file).strip() or None + except OSError: + return None + except ValueError as e: + verbose_logger.warning("Ignoring %s, not a text access token: %s", self.access_token_file, e) + return None + def get_access_token(self) -> str: """ Login to Copilot with retry 3 times. @@ -146,14 +79,10 @@ class Authenticator: Raises: GetAccessTokenError: If unable to obtain an access token after retries. """ - try: - saved_token: Final = _read_token_file(self.access_token_file).strip() - if saved_token: - return saved_token - except OSError: - verbose_logger.warning("No existing access token found or error reading file") - except ValueError as e: - verbose_logger.warning("Ignoring %s, not a text access token: %s", self.access_token_file, e) + stored: Final = self.stored_access_token() + if stored is not None: + return stored + verbose_logger.warning("No existing access token found or error reading file") if not can_block_current_thread(): raise GetAccessTokenError( @@ -190,36 +119,13 @@ class Authenticator: """Return the stored GitHub access token, loading it with device flow when needed.""" return self.get_access_token() - def get_api_base(self) -> str | None: - """Return the Copilot API endpoint for a stored token without starting device flow.""" - access_token: Final = self._stored_access_token() - if access_token is None: - return None - return _copilot_api_base_for_token(access_token) - - def _stored_access_token(self) -> str | None: - try: - access_token: Final = _read_token_file(self.access_token_file).strip() - except (OSError, ValueError): - return None - return access_token or None - def _ensure_token_dir(self) -> None: """Ensure the token directory exists.""" if not os.path.exists(self.token_dir): os.makedirs(self.token_dir, exist_ok=True) - def _get_github_headers(self, access_token: str | None = None) -> dict[str, str]: - """ - Generate standard GitHub headers for API requests. - - Args: - access_token: Optional access token to include in the headers. - - Returns: - Dict[str, str]: Headers for GitHub API requests. - """ - return github_api_headers(access_token) + def _get_github_headers(self) -> dict[str, str]: + return get_copilot_auth_headers() def _get_device_code(self) -> _DeviceCode: """ diff --git a/litellm/llms/github_copilot/chat/transformation.py b/litellm/llms/github_copilot/chat/transformation.py index 0285bde2a87..c4e5838a1fc 100644 --- a/litellm/llms/github_copilot/chat/transformation.py +++ b/litellm/llms/github_copilot/chat/transformation.py @@ -1,5 +1,4 @@ import json -import os from collections.abc import Mapping, Sequence from typing import TYPE_CHECKING, Final, cast # noqa: TID251 # spread dict loses the role literal without a cast @@ -15,11 +14,12 @@ from litellm.types.utils import ModelResponse from ..authenticator import Authenticator from ..common_utils import ( - DEFAULT_GITHUB_COPILOT_API_BASE, GetAccessTokenError, + InvalidAPIBaseError, + get_copilot_api_base, get_copilot_default_headers, ) -from ..per_user_auth import require_github_copilot_user_session +from ..per_user_auth import GithubCopilotUserSession, require_github_copilot_user_session if TYPE_CHECKING: from litellm.litellm_core_utils.litellm_logging import Logging as LiteLLMLoggingObj @@ -38,13 +38,13 @@ class GithubCopilotConfig(OpenAIConfig): super().__init__() self.authenticator = Authenticator() - def api_base_without_login(self, api_base: str | None = None) -> str: - return ( - api_base - or self.authenticator.get_api_base() - or os.getenv("GITHUB_COPILOT_API_BASE") - or DEFAULT_GITHUB_COPILOT_API_BASE - ) + def api_base_without_login(self, user_session: GithubCopilotUserSession | None = None) -> str | None: + try: + if user_session is not None: + return get_copilot_api_base(None, lambda: user_session.token, user_session.endpoints) + return get_copilot_api_base(None, self.authenticator.stored_access_token) + except InvalidAPIBaseError: + return None def _get_openai_compatible_provider_info( self, @@ -53,7 +53,6 @@ class GithubCopilotConfig(OpenAIConfig): api_key: str | None, custom_llm_provider: str, ) -> tuple[str | None, str | None, str]: - dynamic_api_base: Final = self.api_base_without_login(api_base) try: dynamic_api_key: Final = self.authenticator.get_api_key() except GetAccessTokenError as e: @@ -62,7 +61,7 @@ class GithubCopilotConfig(OpenAIConfig): llm_provider=custom_llm_provider, message=str(e), ) - return dynamic_api_base, dynamic_api_key, custom_llm_provider + return get_copilot_api_base(api_base, lambda: dynamic_api_key), dynamic_api_key, custom_llm_provider def get_openai_compatible_provider_info( self, diff --git a/litellm/llms/github_copilot/common_utils.py b/litellm/llms/github_copilot/common_utils.py index fcadb0cb2ef..eb6f70125e4 100644 --- a/litellm/llms/github_copilot/common_utils.py +++ b/litellm/llms/github_copilot/common_utils.py @@ -2,20 +2,100 @@ Constants for Copilot integration """ -from collections.abc import MutableMapping -from typing import Final +import asyncio +import hashlib +import os +from collections.abc import Callable, Mapping, MutableMapping +from functools import partial +from types import MappingProxyType +from typing import ( + Final, + Protocol, + cast, # noqa: TID251 # casts pin the untyped litellm clients and cache to the local Protocols +) +from urllib.parse import urlsplit from uuid import uuid4 import httpx +from pydantic import ConfigDict, TypeAdapter, with_config +from typing_extensions import NotRequired, ReadOnly, TypedDict +from litellm.caching.in_memory_cache import InMemoryCache +from litellm.constants import GITHUB_COPILOT_DEFAULT_HOST, GITHUB_COPILOT_USER_SESSION_TTL_SECONDS from litellm.llms.base_llm.chat.transformation import BaseLLMException -# Constants +from .github_host import GithubHostEndpoints, github_host_endpoints + COPILOT_VERSION: Final = "0.26.7" EDITOR_PLUGIN_VERSION: Final = f"copilot-chat/{COPILOT_VERSION}" USER_AGENT: Final = f"GitHubCopilotChat/{COPILOT_VERSION}" API_VERSION: Final = "2025-04-01" DEFAULT_GITHUB_COPILOT_API_BASE: Final = "https://api.githubcopilot.com" +_GITHUB_API_HOST: Final = "api.github.com" +_PLAN_API_BASE_RETRY_SECONDS: Final = 60 +_PLAN_API_BASE_TIMEOUT_SECONDS: Final = 10.0 +DEFAULT_COPILOT_INTEGRATION_ID: Final = "vscode-chat" +DEFAULT_COPILOT_EDITOR_VERSION: Final = "vscode/1.115.0" +DEFAULT_COPILOT_EDITOR_PLUGIN_VERSION: Final = EDITOR_PLUGIN_VERSION +DEFAULT_COPILOT_USER_AGENT: Final = USER_AGENT + +_COPILOT_AUTH_HEADER_CONFIG: Final = ( + ("accept", "GITHUB_COPILOT_ACCEPT", "application/json"), + ("content-type", "GITHUB_COPILOT_CONTENT_TYPE", "application/json"), + ("copilot-integration-id", "GITHUB_COPILOT_INTEGRATION_ID", DEFAULT_COPILOT_INTEGRATION_ID), + ("editor-version", "GITHUB_COPILOT_EDITOR_VERSION", DEFAULT_COPILOT_EDITOR_VERSION), + ("editor-plugin-version", "GITHUB_COPILOT_EDITOR_PLUGIN_VERSION", DEFAULT_COPILOT_EDITOR_PLUGIN_VERSION), + ("user-agent", "GITHUB_COPILOT_USER_AGENT", DEFAULT_COPILOT_USER_AGENT), +) +_COPILOT_REQUEST_HEADER_CONFIG: Final = _COPILOT_AUTH_HEADER_CONFIG + ( + ("openai-intent", "GITHUB_COPILOT_OPENAI_INTENT", "conversation-panel"), + ("x-github-api-version", "GITHUB_COPILOT_API_VERSION", API_VERSION), + ( + "x-vscode-user-agent-library-version", + "GITHUB_COPILOT_USER_AGENT_LIBRARY_VERSION", + "electron-fetch", + ), +) + + +class _PlanApiBaseCache(Protocol): + def get_cache(self, key: str) -> object: ... + + def set_cache(self, key: str, value: object, ttl: float | None = None) -> object: ... + + def flush_cache(self) -> None: ... + + +class _SyncGetClient(Protocol): + def get( + self, url: str, headers: Mapping[str, str] | None = None, timeout: float | None = None + ) -> httpx.Response: ... + + +class _AsyncGetClient(Protocol): + async def get( + self, url: str, headers: Mapping[str, str] | None = None, timeout: float | None = None + ) -> httpx.Response: ... + + +@with_config(ConfigDict(extra="allow")) +class _CopilotUserEndpoints(TypedDict): + api: ReadOnly[NotRequired[object]] + + +@with_config(ConfigDict(extra="allow")) +class _CopilotUserPayload(TypedDict): + endpoints: ReadOnly[NotRequired[_CopilotUserEndpoints]] + + +_COPILOT_USER: Final = TypeAdapter(_CopilotUserPayload) +_PLAN_API_BASES: Final = cast( # cast-ok: pins the untyped module cache to _PlanApiBaseCache + _PlanApiBaseCache, + InMemoryCache(max_size_in_memory=1000, default_ttl=GITHUB_COPILOT_USER_SESSION_TTL_SECONDS), +) +_PLAN_API_BASE_LOOKUPS: Final[ # mutable-ok: single-flight registry of in-progress plan host lookups per token + dict[str, "asyncio.Task[None]"] +] = {} class GithubCopilotError(BaseLLMException): @@ -46,28 +126,250 @@ class GetAccessTokenError(GithubCopilotError): pass +class InvalidAPIBaseError(GithubCopilotError): + pass + + +def _get_copilot_header_value(environment_variable: str, default: str | None) -> str | None: + value: Final = os.getenv(environment_variable) + if value is None: + return default + return value or None + + +def _get_configured_copilot_headers( + config: tuple[tuple[str, str, str | None], ...], +) -> Mapping[str, str]: + return { + header: value + for header, environment_variable, default in config + if (value := _get_copilot_header_value(environment_variable, default)) is not None + } + + +def get_copilot_auth_headers( + access_token: str | None = None, +) -> dict[str, str]: # mutable-ok: returned straight into httpx handlers whose headers params require dict + headers: Final = _get_configured_copilot_headers(_COPILOT_AUTH_HEADER_CONFIG) + return {**headers, "authorization": f"token {access_token}"} if access_token else {**headers} + + def get_copilot_default_headers( api_key: str, ) -> dict[str, str]: # mutable-ok: callers merge caller headers into the returned dict - """ - Get default headers for GitHub Copilot Responses API. - - Based on copilot-api's header configuration. - """ return { + **_get_configured_copilot_headers(_COPILOT_REQUEST_HEADER_CONFIG), "Authorization": f"Bearer {api_key}", - "content-type": "application/json", - "copilot-integration-id": "vscode-chat", - "editor-version": "vscode/1.95.0", # Fixed version for stability - "editor-plugin-version": EDITOR_PLUGIN_VERSION, - "user-agent": USER_AGENT, - "openai-intent": "conversation-panel", - "x-github-api-version": API_VERSION, "x-request-id": str(uuid4()), - "x-vscode-user-agent-library-version": "electron-fetch", } +_OPENAI_SDK_HEADER_NAMES: Final = MappingProxyType( + {"accept": "Accept", "content-type": "Content-Type", "user-agent": "User-Agent"} +) + + +def with_openai_sdk_header_names( + headers: Mapping[str, str], +) -> dict[str, str]: # mutable-ok: becomes the chat request's extra_headers, which the SDK merges as a dict + return {_OPENAI_SDK_HEADER_NAMES.get(name.lower(), name): value for name, value in headers.items()} + + +def _hostname(url: str) -> str | None: + try: + return urlsplit(url).hostname + except ValueError: + return None + + +def _is_valid_api_base(api_base: str) -> bool: + try: + parsed_api_base: Final = urlsplit(api_base) + except ValueError: + return False + scheme: Final = parsed_api_base.scheme.lower() + hostname: Final = parsed_api_base.hostname + return ( + hostname is not None + and scheme in ("http", "https") + and parsed_api_base.username is None + and parsed_api_base.password is None + and not parsed_api_base.query + and not parsed_api_base.fragment + ) + + +def _reject_enterprise_token_exchange_url(endpoints: GithubHostEndpoints) -> None: + api_key_url: Final = os.getenv("GITHUB_COPILOT_API_KEY_URL") + if endpoints.host == GITHUB_COPILOT_DEFAULT_HOST and api_key_url and _hostname(api_key_url) != _GITHUB_API_HOST: + raise InvalidAPIBaseError( + status_code=500, + message=( + "GITHUB_COPILOT_API_KEY_URL is no longer used. Set GITHUB_COPILOT_API_BASE to your Copilot API host, " + "such as https://copilot-api..ghe.com" + ), + ) + + +def _configured_copilot_api_base_source(api_base: str | None, endpoints: GithubHostEndpoints) -> tuple[str, str | None]: + return ("api_base", api_base) if api_base else ("GITHUB_COPILOT_API_BASE", endpoints.configured_copilot_api_base) + + +def _configured_copilot_api_base(api_base: str | None, endpoints: GithubHostEndpoints) -> str | None: + source, configured_api_base = _configured_copilot_api_base_source(api_base, endpoints) + if not configured_api_base: + _reject_enterprise_token_exchange_url(endpoints) + return None + if not _is_valid_api_base(configured_api_base): + raise InvalidAPIBaseError( + status_code=500, + message=(f"GitHub Copilot {source} must be an HTTP or HTTPS URL without credentials, query, or fragment"), + ) + return configured_api_base + + +def _trusted_plan_api_base(endpoints_api: object, endpoints: GithubHostEndpoints) -> str | None: + if not isinstance(endpoints_api, str) or not _is_valid_api_base(endpoints_api): + return None + try: + parsed: Final = urlsplit(endpoints_api) + port: Final = parsed.port + except ValueError: + return None + hostname: Final = (parsed.hostname or "").lower() + is_copilot_host: Final = parsed.scheme.lower() == "https" and endpoints.is_copilot_api_host(hostname) + return endpoints_api.rstrip("/") if is_copilot_host and port in (None, 443) else None + + +def _plan_api_base_from(response: httpx.Response, endpoints: GithubHostEndpoints) -> tuple[str, int]: + if not response.is_success: + return endpoints.default_copilot_api_base, _PLAN_API_BASE_RETRY_SECONDS + try: + plan_endpoints: Final = _COPILOT_USER.validate_python(response.json()).get("endpoints") + except ValueError: + return endpoints.default_copilot_api_base, _PLAN_API_BASE_RETRY_SECONDS + endpoints_api: Final = plan_endpoints.get("api") if plan_endpoints is not None else None + trusted_api_base: Final = _trusted_plan_api_base(endpoints_api, endpoints) + return trusted_api_base or endpoints.default_copilot_api_base, GITHUB_COPILOT_USER_SESSION_TTL_SECONDS + + +def _plan_api_base_key(github_token: str, endpoints: GithubHostEndpoints) -> str: + return hashlib.sha256(f"{endpoints.host}\x00{github_token}".encode()).hexdigest() + + +def _cached_plan_api_base(key: str) -> str | None: + cached: Final = _PLAN_API_BASES.get_cache(key) + return cached if isinstance(cached, str) else None + + +def _remember_plan_api_base(key: str, resolved: tuple[str, int]) -> None: + api_base, ttl = resolved + _PLAN_API_BASES.set_cache(key, api_base, ttl=ttl) + + +def _plan_api_base_is_needed(github_token: str, api_base: str | None, endpoints: GithubHostEndpoints) -> bool: + return ( + _configured_copilot_api_base(api_base, endpoints) is None + and _cached_plan_api_base(_plan_api_base_key(github_token, endpoints)) is None + ) + + +def _lookup_plan_api_base(github_token: str, endpoints: GithubHostEndpoints) -> tuple[str, int]: + import litellm + + client: Final = cast(_SyncGetClient, litellm.module_level_client) # cast-ok: pins the untyped client + try: + response: Final = client.get( + endpoints.copilot_user_url, + headers=get_copilot_auth_headers(github_token), + timeout=_PLAN_API_BASE_TIMEOUT_SECONDS, + ) + except httpx.HTTPError: + return endpoints.default_copilot_api_base, _PLAN_API_BASE_RETRY_SECONDS + return _plan_api_base_from(response, endpoints) + + +async def _alookup_plan_api_base(github_token: str, endpoints: GithubHostEndpoints) -> tuple[str, int]: + from litellm.llms.custom_httpx import http_handler + + client: Final = cast( # cast-ok: pins the untyped client factory to the local Protocol + Callable[[str], _AsyncGetClient], http_handler.get_async_httpx_client + )("github_copilot") + try: + response: Final = await client.get( + endpoints.copilot_user_url, + headers=get_copilot_auth_headers(github_token), + timeout=_PLAN_API_BASE_TIMEOUT_SECONDS, + ) + except httpx.HTTPError: + return endpoints.default_copilot_api_base, _PLAN_API_BASE_RETRY_SECONDS + return _plan_api_base_from(response, endpoints) + + +def _default_host_endpoints() -> GithubHostEndpoints: + return github_host_endpoints(GITHUB_COPILOT_DEFAULT_HOST) + + +def prefetch_copilot_plan_api_base( + github_token: str, api_base: str | None = None, endpoints: GithubHostEndpoints | None = None +) -> None: + host_endpoints: Final = endpoints or _default_host_endpoints() + if _plan_api_base_is_needed(github_token, api_base, host_endpoints): + _remember_plan_api_base( + _plan_api_base_key(github_token, host_endpoints), _lookup_plan_api_base(github_token, host_endpoints) + ) + + +async def _alookup_and_remember_plan_api_base(key: str, github_token: str, endpoints: GithubHostEndpoints) -> None: + _remember_plan_api_base(key, await _alookup_plan_api_base(github_token, endpoints)) + + +def _forget_plan_api_base_lookup(key: str, lookup: "asyncio.Task[None]") -> None: + if _PLAN_API_BASE_LOOKUPS.get(key) is lookup: + _PLAN_API_BASE_LOOKUPS.pop(key, None) + + +def _plan_api_base_lookup(key: str, github_token: str, endpoints: GithubHostEndpoints) -> "asyncio.Task[None]": + loop: Final = asyncio.get_running_loop() + in_flight: Final = _PLAN_API_BASE_LOOKUPS.get(key) + if in_flight is not None and in_flight.get_loop() is loop: + return in_flight + lookup: Final = loop.create_task(_alookup_and_remember_plan_api_base(key, github_token, endpoints)) + _PLAN_API_BASE_LOOKUPS[key] = lookup + lookup.add_done_callback(partial(_forget_plan_api_base_lookup, key)) + return lookup + + +async def aprefetch_copilot_plan_api_base( + github_token: str, api_base: str | None = None, endpoints: GithubHostEndpoints | None = None +) -> None: + host_endpoints: Final = endpoints or _default_host_endpoints() + if _plan_api_base_is_needed(github_token, api_base, host_endpoints): + key: Final = _plan_api_base_key(github_token, host_endpoints) + await asyncio.shield(_plan_api_base_lookup(key, github_token, host_endpoints)) + + +def get_copilot_api_base( + api_base: str | None = None, + github_token: Callable[[], str | None] | None = None, + endpoints: GithubHostEndpoints | None = None, +) -> str: + host_endpoints: Final = endpoints or _default_host_endpoints() + configured_api_base: Final = _configured_copilot_api_base(api_base, host_endpoints) + if configured_api_base is not None: + return configured_api_base + token: Final = github_token() if github_token is not None else None + plan_api_base: Final = _cached_plan_api_base(_plan_api_base_key(token, host_endpoints)) if token else None + return plan_api_base or host_endpoints.default_copilot_api_base + + +async def aget_copilot_api_base( + github_token: str, api_base: str | None = None, endpoints: GithubHostEndpoints | None = None +) -> str: + await aprefetch_copilot_plan_api_base(github_token, api_base, endpoints) + return get_copilot_api_base(api_base, lambda: github_token, endpoints) + + def pin_session_authorization( headers: MutableMapping[str, str], # mutable-ok: the helper fixes caller-owned headers in place session_token: str, diff --git a/litellm/llms/github_copilot/embedding/transformation.py b/litellm/llms/github_copilot/embedding/transformation.py index 740397f0419..4e0945bd399 100644 --- a/litellm/llms/github_copilot/embedding/transformation.py +++ b/litellm/llms/github_copilot/embedding/transformation.py @@ -7,7 +7,6 @@ Implementation based on analysis of the copilot-api project by caozhiyuan: https://github.com/caozhiyuan/copilot-api """ -import os from typing import ( TYPE_CHECKING, Any, @@ -29,12 +28,11 @@ from litellm.utils import convert_to_model_response_object from ..authenticator import Authenticator from ..common_utils import ( - DEFAULT_GITHUB_COPILOT_API_BASE, GetAccessTokenError, get_copilot_default_headers, pin_session_authorization, ) -from ..per_user_auth import require_github_copilot_user_session +from ..per_user_auth import require_github_copilot_user_session, resolve_copilot_api_base if TYPE_CHECKING: from litellm.litellm_core_utils.litellm_logging import Logging as _LiteLLMLoggingObj @@ -122,15 +120,8 @@ class GithubCopilotEmbeddingConfig(BaseEmbeddingConfig): user_session: Final = require_github_copilot_user_session( cast("dict[str, object]", litellm_params) # cast-ok: litellm_params arrives as an untyped request dict ) - effective_api_base: Final = ( - (user_session.api_base if user_session is not None else None) - or api_base - or self.authenticator.get_api_base() - or os.getenv("GITHUB_COPILOT_API_BASE") - or DEFAULT_GITHUB_COPILOT_API_BASE - ) - - return join_api_base(effective_api_base, "embeddings") + resolved: Final = resolve_copilot_api_base(api_base, user_session, self.authenticator.get_api_key) + return join_api_base(resolved, "embeddings") def transform_embedding_request( self, diff --git a/litellm/llms/github_copilot/github_host.py b/litellm/llms/github_copilot/github_host.py index 09a5a79575a..e4ade36b203 100644 --- a/litellm/llms/github_copilot/github_host.py +++ b/litellm/llms/github_copilot/github_host.py @@ -5,12 +5,10 @@ from typing import Final from litellm.constants import GITHUB_COPILOT_DEFAULT_HOST -from .authenticator import ( - DEFAULT_GITHUB_ACCESS_TOKEN_URL, - DEFAULT_GITHUB_CLIENT_ID, - DEFAULT_GITHUB_COPILOT_USER_URL, - DEFAULT_GITHUB_DEVICE_CODE_URL, -) +DEFAULT_GITHUB_CLIENT_ID: Final = "Iv1.b507a08c87ecfe98" +DEFAULT_GITHUB_DEVICE_CODE_URL: Final = "https://github.com/login/device/code" +DEFAULT_GITHUB_ACCESS_TOKEN_URL: Final = "https://github.com/login/oauth/access_token" +DEFAULT_GITHUB_COPILOT_USER_URL: Final = "https://api.github.com/copilot_internal/user" _GHE_SUBDOMAIN_PATTERN: Final = re.compile(r"[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?") @@ -26,6 +24,12 @@ class GithubHostEndpoints: copilot_api_hosts: frozenset[str] copilot_api_host_suffix: str | None default_copilot_api_base: str + configured_copilot_api_base: str | None + + def is_copilot_api_host(self, hostname: str) -> bool: + return hostname in self.copilot_api_hosts or ( + self.copilot_api_host_suffix is not None and hostname.endswith(self.copilot_api_host_suffix) + ) def normalized_github_host(raw: object) -> str | None: @@ -60,6 +64,7 @@ def github_host_endpoints(host: str) -> GithubHostEndpoints: copilot_api_hosts=frozenset({"githubcopilot.com"}), copilot_api_host_suffix=".githubcopilot.com", default_copilot_api_base="https://api.githubcopilot.com", + configured_copilot_api_base=os.getenv("GITHUB_COPILOT_API_BASE"), ) subdomain: Final = normalized.removesuffix(".ghe.com") @@ -75,4 +80,5 @@ def github_host_endpoints(host: str) -> GithubHostEndpoints: copilot_api_hosts=frozenset({copilot_api_host}), copilot_api_host_suffix=None, default_copilot_api_base=f"https://{copilot_api_host}", + configured_copilot_api_base=None, ) diff --git a/litellm/llms/github_copilot/messages/transformation.py b/litellm/llms/github_copilot/messages/transformation.py index bed855ca178..2d91a84afb7 100644 --- a/litellm/llms/github_copilot/messages/transformation.py +++ b/litellm/llms/github_copilot/messages/transformation.py @@ -9,12 +9,11 @@ from litellm.llms.anthropic.messages.native.transformation import ( from ..authenticator import Authenticator from ..common_utils import ( - DEFAULT_GITHUB_COPILOT_API_BASE, GetAccessTokenError, get_copilot_default_headers, pin_session_authorization, ) -from ..per_user_auth import require_github_copilot_user_session +from ..per_user_auth import require_github_copilot_user_session, resolve_copilot_api_base _MESSAGES_PROXY_API_VERSION: Final = "2026-06-01" @@ -63,23 +62,10 @@ class GithubCopilotAnthropicMessagesConfig(AnthropicMessagesConfig): ) -> tuple[dict, str | None]: """ Validate environment for GitHub Copilot and add Copilot-specific headers. - - The caller-supplied ``api_base`` is intentionally ignored. Routing this - request anywhere other than the authenticated Copilot endpoint would - leak the Copilot bearer token to a caller-controlled URL. """ - # Always use the Copilot endpoint resolved from the authenticated - # session, never the caller-supplied api_base. rstrip so a - # tenant-specific base with a trailing slash does not yield a - # double-slash URL once "/v1/messages" is appended downstream. user_session: Final = require_github_copilot_user_session( cast("dict[str, object]", litellm_params) # cast-ok: litellm_params arrives as an untyped request dict ) - dynamic_api_base: Final = ( - user_session.api_base - if user_session is not None - else (self.authenticator.get_api_base() or DEFAULT_GITHUB_COPILOT_API_BASE) - ).rstrip("/") try: dynamic_api_key: Final = ( user_session.token if user_session is not None else self.authenticator.get_api_key() @@ -90,6 +76,7 @@ class GithubCopilotAnthropicMessagesConfig(AnthropicMessagesConfig): llm_provider="github_copilot", message=str(e), ) + dynamic_api_base: Final = resolve_copilot_api_base(api_base, user_session, lambda: dynamic_api_key).rstrip("/") # Merge Copilot headers with provided headers copilot_headers: Final = get_copilot_default_headers(dynamic_api_key) @@ -126,21 +113,9 @@ class GithubCopilotAnthropicMessagesConfig(AnthropicMessagesConfig): ) -> str: """ Return the complete URL for GitHub Copilot /v1/messages endpoint. - - ``api_base`` here is the value already resolved by - ``validate_anthropic_messages_environment`` (the authenticated Copilot - host), not the raw caller-supplied base — that one is discarded there to - avoid leaking the Copilot bearer token to a caller-controlled URL. We - reuse it to avoid a second authenticator read, falling back to a fresh - resolution only if it was not provided. """ user_session: Final = require_github_copilot_user_session( cast("dict[str, object]", litellm_params) # cast-ok: litellm_params arrives as an untyped request dict ) - resolved: Final = ( - (user_session.api_base if user_session is not None else None) - or api_base - or self.authenticator.get_api_base() - or DEFAULT_GITHUB_COPILOT_API_BASE - ) + resolved: Final = resolve_copilot_api_base(api_base, user_session, self.authenticator.get_api_key) return ensure_api_route(resolved, "v1/messages") diff --git a/litellm/llms/github_copilot/per_user_auth.py b/litellm/llms/github_copilot/per_user_auth.py index 54c64c48084..ffd92196e80 100644 --- a/litellm/llms/github_copilot/per_user_auth.py +++ b/litellm/llms/github_copilot/per_user_auth.py @@ -1,12 +1,9 @@ -"""Per-user GitHub Copilot OAuth sessions and the device-flow helpers the proxy -endpoints use to establish those connections. The shared device login -(Authenticator) is never touched on the per-user path. +"""Per-user GitHub Copilot OAuth: send a caller's stored GitHub OAuth token +directly to Copilot, plus the device-flow helpers the proxy endpoints use to +establish that connection. The shared device login (Authenticator) is never +touched on the per-user path. """ -import asyncio -import hmac -import threading -import time from collections.abc import Callable, Mapping from dataclasses import dataclass, field from typing import ( @@ -16,18 +13,15 @@ from typing import ( TypeAlias, cast, # noqa: TID251 # casts pin the untyped litellm clients/caches to the local Protocols ) -from urllib.parse import urlparse import httpx -from pydantic import ConfigDict, TypeAdapter, ValidationError, with_config +from pydantic import ConfigDict, TypeAdapter, with_config from typing_extensions import NotRequired, ReadOnly, TypedDict -from litellm.caching.in_memory_cache import InMemoryCache from litellm.constants import ( GITHUB_COPILOT_AUTH_TYPE_KEY, GITHUB_COPILOT_HOST_KEY, GITHUB_COPILOT_PER_USER_AUTH_TYPE, - GITHUB_COPILOT_USER_SESSION_TTL_SECONDS, ) from litellm.exceptions import ( APIConnectionError, @@ -37,32 +31,20 @@ from litellm.exceptions import ( ServiceUnavailableError, ) -from .authenticator import github_api_headers -from .common_utils import get_copilot_default_headers +from .authenticator import Authenticator +from .common_utils import ( + aget_copilot_api_base, + aprefetch_copilot_plan_api_base, + get_copilot_api_base, + get_copilot_auth_headers, + get_copilot_default_headers, + prefetch_copilot_plan_api_base, +) from .github_host import GithubHostEndpoints, github_host_endpoints, normalized_github_host GITHUB_COPILOT_USER_SESSION_KWARG_KEY: Final = "github_copilot_user_session" _LLM_PROVIDER: Final = "github_copilot" _DEVICE_FLOW_SCOPE: Final = "read:user" -_SESSION_CACHE_MAX_SIZE: Final = 1000 -_SYNC_LOCK_COUNT: Final = 64 - - -class _SessionCache(Protocol): - def get_cache(self, key: str) -> object: ... - - def set_cache(self, key: str, value: object, ttl: float | None = None) -> object: ... - - def delete_cache(self, key: str) -> object: ... - - -class _SyncGetClient(Protocol): - def get( - self, - url: str, - params: dict[str, str] | None = None, # mutable-ok: mirrors the untyped client's dict parameters - headers: dict[str, str] | None = None, # mutable-ok: mirrors the untyped client's dict parameters - ) -> httpx.Response: ... class _AsyncGitHubClient(Protocol): @@ -83,26 +65,6 @@ class _AsyncGitHubClient(Protocol): ) -> httpx.Response: ... -_SESSION_CACHE: Final = cast( # cast-ok: pins the untyped module cache to _SessionCache - _SessionCache, - InMemoryCache( - max_size_in_memory=_SESSION_CACHE_MAX_SIZE, - default_ttl=GITHUB_COPILOT_USER_SESSION_TTL_SECONDS, - ), -) -_RESOLVE_LOCKS: Final = tuple(threading.Lock() for _ in range(_SYNC_LOCK_COUNT)) -_IN_FLIGHT: Final[ # mutable-ok: single-flight dedup registry mutated under its own locking - dict[str, tuple[str, "asyncio.Future[GithubCopilotUserSession]"]] -] = {} - - -@with_config(ConfigDict(extra="allow", strict=True)) -class _CopilotUserPayload(TypedDict): - access_type_sku: ReadOnly[NotRequired[object]] - chat_enabled: ReadOnly[NotRequired[object]] - endpoints: ReadOnly[NotRequired[object]] - - @with_config(ConfigDict(extra="allow", strict=True)) class _DeviceCodePayload(TypedDict): device_code: ReadOnly[str] @@ -120,35 +82,27 @@ class _AccessTokenPollPayload(TypedDict): interval: ReadOnly[NotRequired[object]] -@with_config(ConfigDict(extra="allow", strict=True)) -class _CopilotEndpointsPayload(TypedDict): - api: ReadOnly[NotRequired[object]] - - @with_config(ConfigDict(extra="allow", strict=True)) class _GithubUserPayload(TypedDict): login: ReadOnly[str] -_COPILOT_USER: Final = TypeAdapter(_CopilotUserPayload) _DEVICE_CODE: Final = TypeAdapter(_DeviceCodePayload) _ACCESS_TOKEN_POLL: Final = TypeAdapter(_AccessTokenPollPayload) _GITHUB_USER: Final = TypeAdapter(_GithubUserPayload) -_COPILOT_ENDPOINTS: Final = TypeAdapter(_CopilotEndpointsPayload) -_CACHED_SESSION_ENTRY: Final = TypeAdapter(tuple[str, str, str, float]) @dataclass(frozen=True, slots=True, repr=False) class GithubCopilotUserSession: - """A resolved per-user Copilot token plus the validated host it may be sent - to. Only this module constructs it, so ``isinstance`` checks downstream are - unforgeable by request JSON.""" + """A caller's stored GitHub OAuth token plus the GitHub host it belongs to, + resolved for this request. Only this module constructs it, so ``isinstance`` + checks downstream are unforgeable by request JSON.""" token: str = field(repr=False) - api_base: str + endpoints: GithubHostEndpoints def __repr__(self) -> str: - return f"GithubCopilotUserSession(api_base={self.api_base!r}, token=REDACTED)" + return f"GithubCopilotUserSession(github_host={self.endpoints.host!r}, token=REDACTED)" __str__ = __repr__ @@ -172,27 +126,39 @@ class GithubCopilotDeviceFlowPoll: access_token: str | None = field(default=None, repr=False) -def validated_copilot_api_base(endpoints_api: object, endpoints: GithubHostEndpoints) -> str: - if not isinstance(endpoints_api, str): - return endpoints.default_copilot_api_base - try: - parsed: Final = urlparse(endpoints_api) - port: Final = parsed.port - except ValueError: - return endpoints.default_copilot_api_base - hostname: Final = (parsed.hostname or "").lower() - if ( - parsed.scheme.lower() != "https" - or not ( - hostname in endpoints.copilot_api_hosts - or (endpoints.copilot_api_host_suffix is not None and hostname.endswith(endpoints.copilot_api_host_suffix)) - ) - or parsed.username is not None - or parsed.password is not None - or port not in (None, 443) - ): - return endpoints.default_copilot_api_base - return endpoints_api.rstrip("/") +def _reject_connection(credential_name: str) -> CallerCredentialAuthenticationError: + return CallerCredentialAuthenticationError( + message=( + f"GitHub rejected the GitHub Copilot connection for credential '{credential_name}'. " + f"Reconnect GitHub Copilot for credential '{credential_name}' in the LiteLLM UI (LLM Credentials)" + ), + llm_provider=_LLM_PROVIDER, + model="", + ) + + +def _to_int(raw: object) -> int: + if isinstance(raw, bool): + return 0 + if isinstance(raw, int): + return raw + if isinstance(raw, float): + return int(raw) + if isinstance(raw, str): + try: + return int(raw) + except ValueError: + return 0 + return 0 + + +def _new_async_github_client() -> _AsyncGitHubClient: + from litellm.llms.custom_httpx import http_handler + + return cast( # cast-ok: pins the untyped litellm client factory to the local Protocol + Callable[[str], _AsyncGitHubClient], + http_handler.get_async_httpx_client, + )(_LLM_PROVIDER) def github_copilot_host_endpoints(credential_name: str) -> GithubHostEndpoints: @@ -212,223 +178,12 @@ def github_copilot_host_endpoints(credential_name: str) -> GithubHostEndpoints: return github_host_endpoints(host) -def _session_cache_key(user_id: str, credential_name: str, github_host: str) -> str: - return f"{user_id}\x00{credential_name}\x00{github_host}" - - -def evict_copilot_user_session(user_id: str, credential_name: str, github_host: str) -> None: - _SESSION_CACHE.delete_cache(_session_cache_key(user_id, credential_name, github_host)) - - -def _cached_session(cache_key: str, github_token: str) -> GithubCopilotUserSession | None: - cached: Final[object] = _SESSION_CACHE.get_cache(cache_key) - try: - cached_github_token, token, api_base, expires_at = _CACHED_SESSION_ENTRY.validate_python(cached) - except ValidationError: - return None - if not hmac.compare_digest(cached_github_token, github_token): - return None - if expires_at <= time.time(): - return None - return GithubCopilotUserSession(token=token, api_base=api_base) - - -def _cache_session(cache_key: str, github_token: str, session: GithubCopilotUserSession, expires_at: float) -> None: - ttl: Final = expires_at - time.time() - if ttl <= 0: - return - _SESSION_CACHE.set_cache(cache_key, (github_token, session.token, session.api_base, expires_at), ttl=ttl) - - -def _reject_connection(credential_name: str) -> CallerCredentialAuthenticationError: - return CallerCredentialAuthenticationError( - message=( - f"GitHub rejected the GitHub Copilot connection for credential '{credential_name}'. " - f"Reconnect GitHub Copilot for credential '{credential_name}' in the LiteLLM UI (LLM Credentials)" - ), - llm_provider=_LLM_PROVIDER, - model="", - ) - - -def _session_from_response( - response: httpx.Response, - github_token: str, - credential_name: str, - cache_key: str, - endpoints: GithubHostEndpoints, -) -> GithubCopilotUserSession: - if response.status_code in (401, 403, 404): - _SESSION_CACHE.delete_cache(cache_key) - raise _reject_connection(credential_name) - if response.status_code == 429: - raise CallerCredentialRateLimitError( - message="GitHub rate limited the GitHub Copilot user lookup", - llm_provider=_LLM_PROVIDER, - model="", - ) - if not response.is_success: - raise ServiceUnavailableError( - message=f"GitHub Copilot user lookup failed with status {response.status_code}", - llm_provider=_LLM_PROVIDER, - model="", - ) - try: - payload: Final = _COPILOT_USER.validate_python(response.json()) - except Exception as e: - raise ServiceUnavailableError( - message="GitHub Copilot user lookup returned an invalid response", - llm_provider=_LLM_PROVIDER, - model="", - ) from e - - if payload.get("chat_enabled") is False or payload.get("access_type_sku") == "no_access": - _SESSION_CACHE.delete_cache(cache_key) - raise _reject_connection(credential_name) - - api_base: Final = validated_copilot_api_base(_endpoints_api(payload.get("endpoints")), endpoints) - session: Final = GithubCopilotUserSession(token=github_token, api_base=api_base) - _cache_session(cache_key, github_token, session, time.time() + GITHUB_COPILOT_USER_SESSION_TTL_SECONDS) - return session - - -def _endpoints_api(payload_endpoints: object) -> object: - if not isinstance(payload_endpoints, Mapping): - return None - return _COPILOT_ENDPOINTS.validate_python(payload_endpoints).get("api") - - -def _to_int(raw: object) -> int: - if isinstance(raw, bool): - return 0 - if isinstance(raw, int): - return raw - if isinstance(raw, float): - return int(raw) - if isinstance(raw, str): - try: - return int(raw) - except ValueError: - return 0 - return 0 - - -def _do_resolve( - github_token: str, - credential_name: str, - cache_key: str, - endpoints: GithubHostEndpoints, -) -> GithubCopilotUserSession: - import litellm - - try: - client: Final = cast( # cast-ok: pins the untyped litellm client to the local Protocol - _SyncGetClient, litellm.module_level_client - ) - response: Final = client.get( - endpoints.copilot_user_url, - headers=github_api_headers(github_token), - ) - except Exception as e: - raise APIConnectionError( - message="GitHub Copilot user lookup request failed", - llm_provider=_LLM_PROVIDER, - model="", - ) from e - return _session_from_response(response, github_token, credential_name, cache_key, endpoints) - - -def _new_async_github_client() -> _AsyncGitHubClient: - from litellm.llms.custom_httpx import http_handler - - return cast( # cast-ok: pins the untyped litellm client factory to the local Protocol - Callable[[str], _AsyncGitHubClient], - http_handler.get_async_httpx_client, - )(_LLM_PROVIDER) - - -async def _do_aresolve( - github_token: str, - credential_name: str, - cache_key: str, - endpoints: GithubHostEndpoints, -) -> GithubCopilotUserSession: - client: Final = _new_async_github_client() - try: - response: Final = await client.get( - endpoints.copilot_user_url, - headers=github_api_headers(github_token), - ) - except Exception as e: - raise APIConnectionError( - message="GitHub Copilot user lookup request failed", - llm_provider=_LLM_PROVIDER, - model="", - ) from e - return _session_from_response(response, github_token, credential_name, cache_key, endpoints) - - -def resolve_copilot_user_session( - user_id: str, - github_token: str, - credential_name: str, - endpoints: GithubHostEndpoints, -) -> GithubCopilotUserSession: - cache_key: Final = _session_cache_key(user_id, credential_name, endpoints.host) - cached: Final = _cached_session(cache_key, github_token) - if cached is not None: - return cached - lock: Final = _RESOLVE_LOCKS[hash(cache_key) % _SYNC_LOCK_COUNT] - with lock: - rechecked: Final = _cached_session(cache_key, github_token) - if rechecked is not None: - return rechecked - return _do_resolve(github_token, credential_name, cache_key, endpoints) - - -async def aresolve_copilot_user_session( - user_id: str, - github_token: str, - credential_name: str, - endpoints: GithubHostEndpoints, -) -> GithubCopilotUserSession: - cache_key: Final = _session_cache_key(user_id, credential_name, endpoints.host) - cached: Final = _cached_session(cache_key, github_token) - if cached is not None: - return cached - loop: Final = asyncio.get_running_loop() - in_flight_entry: Final = _IN_FLIGHT.get(cache_key) - if in_flight_entry is not None: - in_flight_token, in_flight = in_flight_entry - if not hmac.compare_digest(in_flight_token, github_token): - return await _do_aresolve(github_token, credential_name, cache_key, endpoints) - if not in_flight.done() and in_flight.get_loop() is loop: - return await asyncio.shield(in_flight) - future: Final = loop.create_future() - entry: Final = (github_token, future) - _IN_FLIGHT[cache_key] = entry - try: - session: Final = await _do_aresolve(github_token, credential_name, cache_key, endpoints) - except BaseException as e: - if not future.done(): - future.set_exception(e) - future.exception() # consume so unawaited waiters do not log "exception was never retrieved" - raise - else: - if not future.done(): - future.set_result(session) - return session - finally: - if _IN_FLIGHT.get(cache_key) is entry: - _IN_FLIGHT.pop(cache_key, None) - - async def astart_device_flow(endpoints: GithubHostEndpoints) -> GithubCopilotDeviceFlowStart: client: Final = _new_async_github_client() try: response: Final = await client.post( endpoints.device_code_url, - headers=github_api_headers(), + headers=get_copilot_auth_headers(), json={"client_id": endpoints.client_id, "scope": _DEVICE_FLOW_SCOPE}, ) except httpx.HTTPStatusError as e: @@ -465,7 +220,7 @@ async def apoll_device_flow(device_code: str, endpoints: GithubHostEndpoints) -> try: response: Final = await client.post( endpoints.access_token_url, - headers=github_api_headers(), + headers=get_copilot_auth_headers(), json={ "client_id": endpoints.client_id, "device_code": device_code, @@ -528,7 +283,7 @@ async def afetch_github_login(github_token: str, endpoints: GithubHostEndpoints) try: response: Final = await client.get( endpoints.user_url, - headers=github_api_headers(github_token), + headers=get_copilot_auth_headers(github_token), ) except Exception as e: raise APIConnectionError( @@ -553,47 +308,34 @@ async def afetch_github_login(github_token: str, endpoints: GithubHostEndpoints) return payload["login"] -async def acheck_copilot_seat( - user_id: str, - github_token: str, - credential_name: str, - endpoints: GithubHostEndpoints, -) -> GithubCopilotUserSession: - session: Final = await aresolve_copilot_user_session( - user_id=user_id, - github_token=github_token, - credential_name=credential_name, - endpoints=endpoints, - ) +async def acheck_copilot_seat(github_token: str, credential_name: str, endpoints: GithubHostEndpoints) -> None: client: Final = _new_async_github_client() - models_url: Final = f"{session.api_base}/models" + api_base: Final = await aget_copilot_api_base(github_token, endpoints=endpoints) try: response: Final = await client.get( - models_url, - headers=get_copilot_default_headers(session.token), + f"{api_base.rstrip('/')}/models", + headers=get_copilot_default_headers(github_token), ) except Exception as e: raise APIConnectionError( - message="GitHub Copilot models check request failed", + message="GitHub Copilot seat check request failed", llm_provider=_LLM_PROVIDER, model="", ) from e if response.status_code in (401, 403, 404): - _SESSION_CACHE.delete_cache(_session_cache_key(user_id, credential_name, endpoints.host)) raise _reject_connection(credential_name) if response.status_code == 429: raise CallerCredentialRateLimitError( - message="GitHub rate limited the GitHub Copilot models check", + message="GitHub Copilot rate limited the seat check", llm_provider=_LLM_PROVIDER, model="", ) if not response.is_success: raise ServiceUnavailableError( - message=f"GitHub Copilot models check failed with status {response.status_code}", + message=f"GitHub Copilot seat check failed with status {response.status_code}", llm_provider=_LLM_PROVIDER, model="", ) - return session def github_copilot_auth_mode(litellm_credential_name: object, auth_type_value: object) -> bool: @@ -707,6 +449,25 @@ def require_github_copilot_user_session(litellm_params: object) -> GithubCopilot return None +def resolve_copilot_api_base( + api_base: str | None, user_session: GithubCopilotUserSession | None, shared_token: Callable[[], str | None] +) -> str: + if user_session is None: + return get_copilot_api_base(api_base, shared_token) + return get_copilot_api_base(None, lambda: user_session.token, user_session.endpoints) + + +def github_copilot_caller_rejection( + original_exception: object, request_kwargs: object +) -> CallerCredentialAuthenticationError | None: + if ( + getattr(original_exception, "status_code", None) != 401 + or github_copilot_user_session_from(request_kwargs) is None + ): + return None + return _reject_connection(github_copilot_per_user_credential_name(request_kwargs) or "") + + def is_github_copilot_per_user_request(kwargs: Mapping[str, object]) -> bool: """True when this call carries a resolved per-user Copilot session, meaning it must bypass the shared response cache (cache keys ignore caller identity). @@ -781,34 +542,45 @@ def attach_github_copilot_user_session( return credential_name: Final = credential_name_obj if isinstance(credential_name_obj, str) else "" endpoints: Final = github_copilot_host_endpoints(credential_name) - user_id, github_token = _caller_connection(kwargs, credential_name, endpoints) - session: Final = resolve_copilot_user_session( - user_id=user_id, - github_token=github_token, - credential_name=credential_name, - endpoints=endpoints, - ) + _, github_token = _caller_connection(kwargs, credential_name, endpoints) + session: Final = GithubCopilotUserSession(token=github_token, endpoints=endpoints) kwargs[GITHUB_COPILOT_USER_SESSION_KWARG_KEY] = session # rebind-ok: kwargs is the request mutation channel _strip_caller_authorization(kwargs) -async def aattach_github_copilot_user_session( +def _plan_api_base_request( + kwargs: Mapping[str, object], model: object +) -> tuple[str, str | None, GithubHostEndpoints | None] | None: + api_base_obj: Final = kwargs.get("api_base") + api_base: Final = api_base_obj if isinstance(api_base_obj, str) else None + session: Final = kwargs.get(GITHUB_COPILOT_USER_SESSION_KWARG_KEY) + if isinstance(session, GithubCopilotUserSession): + return session.token, None, session.endpoints + import litellm + + requested_model: Final = litellm.model_alias_map.get(model, model) if isinstance(model, str) else model + targets_github_copilot: Final = kwargs.get("custom_llm_provider") == _LLM_PROVIDER or ( + isinstance(requested_model, str) and requested_model.startswith(f"{_LLM_PROVIDER}/") + ) + shared_token: Final = Authenticator().stored_access_token() if targets_github_copilot else None + return (shared_token, api_base, None) if shared_token is not None else None + + +def prepare_github_copilot_request( kwargs: dict[str, object], # mutable-ok: kwargs is the request mutation channel + model: object, ) -> None: - credential_name_obj: Final[object] = kwargs.get("litellm_credential_name") - auth_type_obj: Final[object] = kwargs.get(GITHUB_COPILOT_AUTH_TYPE_KEY) - if not github_copilot_auth_mode(credential_name_obj, auth_type_obj): - return - if isinstance(kwargs.get(GITHUB_COPILOT_USER_SESSION_KWARG_KEY), GithubCopilotUserSession): - return - credential_name: Final = credential_name_obj if isinstance(credential_name_obj, str) else "" - endpoints: Final = github_copilot_host_endpoints(credential_name) - user_id, github_token = _caller_connection(kwargs, credential_name, endpoints) - session: Final = await aresolve_copilot_user_session( - user_id=user_id, - github_token=github_token, - credential_name=credential_name, - endpoints=endpoints, - ) - kwargs[GITHUB_COPILOT_USER_SESSION_KWARG_KEY] = session # rebind-ok: kwargs is the request mutation channel - _strip_caller_authorization(kwargs) + attach_github_copilot_user_session(kwargs) + plan_request: Final = _plan_api_base_request(kwargs, model) + if plan_request is not None: + prefetch_copilot_plan_api_base(*plan_request) + + +async def aprepare_github_copilot_request( + kwargs: dict[str, object], # mutable-ok: kwargs is the request mutation channel + model: object, +) -> None: + attach_github_copilot_user_session(kwargs) + plan_request: Final = _plan_api_base_request(kwargs, model) + if plan_request is not None: + await aprefetch_copilot_plan_api_base(*plan_request) diff --git a/litellm/llms/github_copilot/responses/transformation.py b/litellm/llms/github_copilot/responses/transformation.py index f337d4ae0d3..6ba462ed783 100644 --- a/litellm/llms/github_copilot/responses/transformation.py +++ b/litellm/llms/github_copilot/responses/transformation.py @@ -8,7 +8,6 @@ Implementation based on analysis of the copilot-api project by caozhiyuan: https://github.com/caozhiyuan/copilot-api """ -import os from collections.abc import Mapping from typing import ( TYPE_CHECKING, @@ -37,12 +36,11 @@ from litellm.utils import cached_get_model_info_helper from ..authenticator import Authenticator from ..common_utils import ( - DEFAULT_GITHUB_COPILOT_API_BASE, GetAccessTokenError, get_copilot_default_headers, pin_session_authorization, ) -from ..per_user_auth import require_github_copilot_user_session +from ..per_user_auth import require_github_copilot_user_session, resolve_copilot_api_base if TYPE_CHECKING: from litellm.litellm_core_utils.litellm_logging import Logging as _LiteLLMLoggingObj @@ -274,15 +272,8 @@ class GithubCopilotResponsesAPIConfig(OpenAIResponsesAPIConfig): user_session: Final = require_github_copilot_user_session( cast("dict[str, object]", litellm_params) # cast-ok: litellm_params arrives as an untyped request dict ) - effective_api_base: Final = ( - (user_session.api_base if user_session is not None else None) - or api_base - or self.authenticator.get_api_base() - or os.getenv("GITHUB_COPILOT_API_BASE") - or DEFAULT_GITHUB_COPILOT_API_BASE - ) - - return join_api_base(effective_api_base, "responses") + resolved: Final = resolve_copilot_api_base(api_base, user_session, self.authenticator.get_api_key) + return join_api_base(resolved, "responses") def _handle_reasoning_item(self, item: dict[str, object]) -> dict[str, object]: """ diff --git a/litellm/main.py b/litellm/main.py index 6b9964a450f..3beaebc989e 100644 --- a/litellm/main.py +++ b/litellm/main.py @@ -2819,6 +2819,7 @@ def _complete_custom_openai( from litellm.llms.github_copilot.common_utils import ( get_copilot_default_headers, pin_session_authorization, + with_openai_sdk_header_names, ) from litellm.llms.github_copilot.per_user_auth import ( require_github_copilot_user_session, @@ -2834,7 +2835,7 @@ def _complete_custom_openai( ) if user_session is not None: pin_session_authorization(copilot_headers, user_session.token) - extra_headers = copilot_headers + extra_headers = with_openai_sdk_header_names(copilot_headers) use_base_llm_http_handler: Final = get_secret_bool("EXPERIMENTAL_OPENAI_BASE_LLM_HTTP_HANDLER") diff --git a/litellm/proxy/credential_endpoints/endpoints.py b/litellm/proxy/credential_endpoints/endpoints.py index 86e716f19e3..ae9b84eeb05 100644 --- a/litellm/proxy/credential_endpoints/endpoints.py +++ b/litellm/proxy/credential_endpoints/endpoints.py @@ -74,7 +74,6 @@ from .user_provider_credentials import ( delete_user_provider_credentials_for_credential, invalidate_user_provider_credential_cache, list_user_provider_credentials, - list_user_provider_credentials_for_credential, set_user_provider_credential_cache, upsert_user_provider_credential, ) @@ -741,12 +740,7 @@ async def poll_user_connection( if not github_token: raise HTTPException(status_code=502, detail="GitHub device flow completed without a token") try: - await acheck_copilot_seat( - user_id=user_id, - github_token=github_token, - credential_name=credential_name, - endpoints=endpoints, - ) + await acheck_copilot_seat(github_token=github_token, credential_name=credential_name, endpoints=endpoints) except litellm.CallerCredentialAuthenticationError: return UserConnectionPollResponse(status="no_copilot_seat") github_login: Final = await afetch_github_login(github_token, endpoints) @@ -791,7 +785,6 @@ async def delete_user_connection( user_api_key_dict: UserAPIKeyAuth = Depends(user_api_key_auth), # noqa: B008 # FastAPI resolves the dependency from the default ) -> UserConnectionDeleteResponse: """Disconnect the calling user's stored GitHub token for a per-user credential. Idempotent.""" - from litellm.llms.github_copilot.per_user_auth import evict_copilot_user_session from litellm.proxy.proxy_server import prisma_client, user_api_key_cache try: @@ -813,9 +806,7 @@ async def delete_user_connection( status_code=503, detail={"error": "could not revoke cached connection, retry"}, ) - prior: Final = await delete_user_provider_credential(prisma_client, user_id, credential_name) - if prior is not None: - evict_copilot_user_session(user_id, credential_name, prior.github_host) + await delete_user_provider_credential(prisma_client, user_id, credential_name) return UserConnectionDeleteResponse(status="disconnected") except Exception as e: # noqa: BLE001 # endpoint boundary: every failure becomes the proxy error contract raise handle_exception_on_proxy(e) @@ -827,28 +818,19 @@ async def _purge_user_connections_for_credential(credential_name: str) -> None: Shared cleanup for DELETE (credential removed) and PATCH (renamed or no longer per-user).""" - from litellm.llms.github_copilot.per_user_auth import evict_copilot_user_session from litellm.proxy.proxy_server import prisma_client, user_api_key_cache if prisma_client is None: return try: - prior_rows: Final = await list_user_provider_credentials_for_credential(prisma_client, credential_name) user_ids: Final = await delete_user_provider_credentials_for_credential(prisma_client, credential_name) except Exception: # noqa: BLE001 # best-effort purge must not break credential deletion verbose_proxy_logger.exception( "_purge_user_connections_for_credential: failed to drop user connections for %s", credential_name ) return - github_hosts: Final = { - row.user_id: decoded.github_host - for row in prior_rows - if (decoded := decode_user_provider_credential(row.credential_b64)) is not None - } for user_id in user_ids: await invalidate_user_provider_credential_cache(user_api_key_cache, user_id, credential_name) - if user_id in github_hosts: - evict_copilot_user_session(user_id, credential_name, github_hosts[user_id]) @router.delete( diff --git a/litellm/proxy/credential_endpoints/user_provider_credentials.py b/litellm/proxy/credential_endpoints/user_provider_credentials.py index 22ee271410e..38c85241a20 100644 --- a/litellm/proxy/credential_endpoints/user_provider_credentials.py +++ b/litellm/proxy/credential_endpoints/user_provider_credentials.py @@ -151,13 +151,6 @@ async def list_user_provider_credentials( return await _table(prisma_client).find_many(where={"user_id": user_id}) -async def list_user_provider_credentials_for_credential( - prisma_client: "PrismaClient", - credential_name: str, -) -> Sequence["prisma_models.LiteLLM_UserProviderCredentials"]: - return await _table(prisma_client).find_many(where={"credential_name": credential_name}) - - class _StringKeyCache(Protocol): async def async_get_cache( self, diff --git a/litellm/router_rust/invoke/shaping.py b/litellm/router_rust/invoke/shaping.py index 258f919c9f1..09d76b4a47c 100644 --- a/litellm/router_rust/invoke/shaping.py +++ b/litellm/router_rust/invoke/shaping.py @@ -25,10 +25,11 @@ from litellm.constants import ( from litellm.litellm_core_utils.batch_utils import get_router_metadata_variable_name from litellm.litellm_core_utils.clientside_credential_handler import ( get_dynamic_litellm_params, # pyright: ignore[reportUnknownVariableType] # litellm's untyped helpers and request values + github_copilot_caller_endpoint_keys, is_clientside_credential, # pyright: ignore[reportUnknownVariableType] # litellm's untyped helpers and request values ) from litellm.litellm_core_utils.completion_timeout import CompletionTimeout -from litellm.litellm_core_utils.core_helpers import without +from litellm.litellm_core_utils.core_helpers import drops_unsupported_params, without from litellm.litellm_core_utils.get_litellm_params import SUPPLEMENTAL_KWARGS_KEYS from litellm.litellm_core_utils.get_llm_provider_logic import declared_authenticating_provider, inferred_provider from litellm.litellm_core_utils.request_timeout_resolver import get_service_tier_timeout @@ -192,6 +193,37 @@ def _forwarded_alias_marker_keys_the_deployment_sets( return tuple(key for key in keys if isinstance(key, str) and _deployment_sets_litellm_param(deployment_params, key)) +def _github_copilot_caller_endpoint_keys_to_drop( + deployment_params: Mapping[str, object], + request: Mapping[str, object], + defaults: Mapping[str, object], + model_name: object, +) -> tuple[str, ...]: + caller_endpoint_keys: Final = github_copilot_caller_endpoint_keys(deployment_params, request) + call_drop_params: Final = next( + ( + value + for value in ( + request.get("drop_params"), + deployment_params.get("drop_params"), + defaults.get("drop_params"), + ) + if value is not None + ), + None, + ) + if caller_endpoint_keys and not drops_unsupported_params(call_drop_params): + raise litellm_exceptions.BadRequestError( + message=( + f"GitHub Copilot deployments do not accept {' or '.join(caller_endpoint_keys)} from the caller. " + "Set api_base on the deployment, or set drop_params to drop it" + ), + model=str(model_name), + llm_provider="github_copilot", + ) + return caller_endpoint_keys + + # --- reasoning effort ------------------------------------------------------------------------- @@ -410,12 +442,18 @@ def update_kwargs_with_deployment( ) -> None: """``_update_kwargs_with_deployment``, in place on the attempt's kwargs.""" deployment_params: Final = as_dict(deployment.get("litellm_params")) - for key in _forwarded_alias_marker_keys_the_deployment_sets( - deployment_params, kwargs.pop(_ALIAS_MARKER_FORWARDED_PARAMS_KWARG, ()) + defaults: Final = shaping.default_litellm_params + caller_endpoint_keys: Final = _github_copilot_caller_endpoint_keys_to_drop( + deployment_params, kwargs, defaults, deployment.get("model_name") + ) + for key in ( + *_forwarded_alias_marker_keys_the_deployment_sets( + deployment_params, kwargs.pop(_ALIAS_MARKER_FORWARDED_PARAMS_KWARG, ()) + ), + *caller_endpoint_keys, ): kwargs.pop(key, None) kwargs.pop("daily_reset_time", None) - defaults: Final = shaping.default_litellm_params merge_tools_from_deployment(deployment_params, kwargs, defaults) model_info: Mapping[str, object] = as_dict( # rebind-ok: rebound as the Python router rebinds it diff --git a/litellm/utils.py b/litellm/utils.py index 53b02ebddac..d50b3811e3e 100644 --- a/litellm/utils.py +++ b/litellm/utils.py @@ -2106,9 +2106,9 @@ def client(original_function): ## LOAD CREDENTIALS load_credentials_from_list(kwargs, call_type) - from litellm.llms.github_copilot.per_user_auth import attach_github_copilot_user_session + from litellm.llms.github_copilot.per_user_auth import prepare_github_copilot_request - attach_github_copilot_user_session(kwargs) # pyright: ignore[reportUnknownArgumentType] # kwargs is the untyped request dict + prepare_github_copilot_request(kwargs, model) # pyright: ignore[reportUnknownArgumentType] # kwargs is the untyped request dict kwargs["litellm_logging_obj"] = logging_obj LLMCachingHandler: Final = _get_cached_llm_caching_handler() _llm_caching_handler: Final[LLMCachingHandler] = LLMCachingHandler( @@ -2410,9 +2410,9 @@ def client(original_function): kwargs["litellm_logging_obj"] = logging_obj ## LOAD CREDENTIALS kwargs.update(await acredential_updates(kwargs, call_type)) # pyright: ignore[reportUnknownMemberType, reportUnknownArgumentType] # kwargs is the untyped request dict - from litellm.llms.github_copilot.per_user_auth import aattach_github_copilot_user_session + from litellm.llms.github_copilot.per_user_auth import aprepare_github_copilot_request - await aattach_github_copilot_user_session(kwargs) # pyright: ignore[reportUnknownArgumentType] # kwargs is the untyped request dict + await aprepare_github_copilot_request(kwargs, model) # pyright: ignore[reportUnknownArgumentType] # kwargs is the untyped request dict logging_obj.llm_caching_handler = _llm_caching_handler # [OPTIONAL] CHECK BUDGET if litellm.max_budget: diff --git a/tests/integration/_support/copilot_session_prometheus_worker.py b/tests/integration/_support/copilot_session_prometheus_worker.py index a6b2c5d4692..279807f3170 100644 --- a/tests/integration/_support/copilot_session_prometheus_worker.py +++ b/tests/integration/_support/copilot_session_prometheus_worker.py @@ -7,19 +7,22 @@ from __future__ import annotations import asyncio import json +import os import subprocess import sys -from itertools import chain from dataclasses import dataclass +from itertools import chain from typing import Final -import litellm import openai -from litellm.integrations.prometheus import PrometheusLogger -from litellm.llms.github_copilot.per_user_auth import GithubCopilotUserSession from prometheus_client import REGISTRY from pydantic import TypeAdapter +import litellm +from litellm.integrations.prometheus import PrometheusLogger +from litellm.llms.github_copilot.github_host import github_host_endpoints +from litellm.llms.github_copilot.per_user_auth import GithubCopilotUserSession + COPILOT_DEPLOYMENT: Final = "copilot-per-user-deployment" OPERATOR_DEPLOYMENT: Final = "operator-key-deployment" CALLER_SESSION_TOKEN: Final = "caller-copilot-session-token" @@ -77,6 +80,7 @@ _REPORT: Final = TypeAdapter(WorkerReport) def run_copilot_session_worker(api_base: str) -> WorkerReport: completed: Final = subprocess.run( (sys.executable, "-P", __file__, api_base), + env={**os.environ, "GITHUB_COPILOT_API_BASE": api_base}, capture_output=True, text=True, timeout=WORKER_TIMEOUT_SECONDS, @@ -146,7 +150,7 @@ async def _direct_attempt(api_base: str) -> Outcome: async def main(api_base: str) -> None: litellm.callbacks = [PrometheusLogger()] # test-quality-ok: this worker process exits right after the report - session: Final = GithubCopilotUserSession(token=CALLER_SESSION_TOKEN, api_base=api_base) + session: Final = GithubCopilotUserSession(token=CALLER_SESSION_TOKEN, endpoints=github_host_endpoints("github.com")) router: Final = _router(api_base) outcomes: Final = ( await _attempt(router, COPILOT_DEPLOYMENT, github_copilot_user_session=session), diff --git a/tests/integration/_support/device_login.py b/tests/integration/_support/device_login.py index 13a7169a01a..fe6f92a09ea 100644 --- a/tests/integration/_support/device_login.py +++ b/tests/integration/_support/device_login.py @@ -27,6 +27,10 @@ GITHUB_HOST: Final = "github.com" GITHUB_API_HOST: Final = "api.github.com" COPILOT_API_HOST: Final = "api.githubcopilot.com" AUTH_HOSTS: Final = (CHATGPT_AUTH_HOST, GITHUB_HOST, GITHUB_API_HOST) +GHE_HOST: Final = "octo.ghe.com" +GHE_API_HOST: Final = "api.octo.ghe.com" +GHE_COPILOT_HOST: Final = "copilot-api.octo.ghe.com" +TUNNELED_HOSTS: Final = (*AUTH_HOSTS, GHE_HOST, GHE_API_HOST, GHE_COPILOT_HOST, COPILOT_API_HOST) CHATGPT_ACCOUNT: Final = "acct-device-login" _FAR_FUTURE: Final = 4102444800 _LONG_AGO: Final = 946684800 @@ -76,8 +80,9 @@ COPILOT_REJECTED_ACCESS: Final = "device-login-github-access-rejected" COPILOT_FIRST_LOGIN_ACCESS: Final = "device-login-github-access-first-login" COPILOT_UNSAVED_KEY_ACCESS: Final = "device-login-github-access-unsaved-key" COPILOT_STORED_KEY: Final = "tid=device-login;stored" -COPILOT_MINTED_KEY: Final = "tid=device-login;minted" COPILOT_REJECTED_KEY: Final = "tid=device-login;rejected" +GHE_ACCESS: Final = "device-login-ghe-access" +GHE_LOGIN: Final = "octo-user" CONTROL_KEY: Final = "device-login-control-key" ACCEPTED_BEARERS: Final = frozenset( { @@ -87,8 +92,6 @@ ACCEPTED_BEARERS: Final = frozenset( COPILOT_ACCESS, COPILOT_FIRST_LOGIN_ACCESS, COPILOT_UNSAVED_KEY_ACCESS, - COPILOT_STORED_KEY, - COPILOT_MINTED_KEY, CONTROL_KEY, } ) @@ -143,26 +146,19 @@ def _oauth_token(request: Request, grant: threading.Event) -> Reply: return _json(400, {"error": "invalid_grant", "error_description": "refresh token was revoked"}) -def _copilot_api_key(request: Request, api_url: str) -> Reply: - if request.headers.get("authorization") not in ( - f"token {COPILOT_ACCESS}", - f"token {COPILOT_FIRST_LOGIN_ACCESS}", - f"token {COPILOT_UNSAVED_KEY_ACCESS}", - ): - return _json(401, {"message": "Bad credentials"}) - return _json( - 200, - {"token": COPILOT_MINTED_KEY, "expires_at": int(time.time()) + 1800, "endpoints": {"api": api_url}}, - ) - - def _switched_off() -> Reply: return _json(503, {"error": "device login is switched off in this scenario"}) -def _auth_hosts(api_url: str, grant: threading.Event) -> Callable[[Request], Reply]: +def _ghe_token(request: Request) -> str: + scheme, _, token = request.headers.get("authorization", "").partition(" ") + return token if scheme in ("token", "Bearer") else "" + + +def _auth_hosts(grant: threading.Event, vendor: rv.ResponsesVendor) -> Callable[[Request], Reply]: def respond(request: Request) -> Reply: - match (request.headers.get("host", ""), request.method, urlsplit(request.target).path): + host: Final = request.headers.get("host", "") + match (host, request.method, urlsplit(request.target).path): case ("auth.openai.com", "POST", "/oauth/token"): return _oauth_token(request, grant) case ("auth.openai.com", "POST", "/api/accounts/deviceauth/usercode") if grant.is_set(): @@ -189,12 +185,31 @@ def _auth_hosts(api_url: str, grant: threading.Event) -> Callable[[Request], Rep ) case ("github.com", "POST", "/login/oauth/access_token") if grant.is_set(): return _json(200, {"access_token": COPILOT_FIRST_LOGIN_ACCESS, "token_type": "bearer"}) - case ("api.github.com", "GET", "/copilot_internal/v2/token"): - return _copilot_api_key(request, api_url) case ("api.github.com", "GET", "/copilot_internal/user"): if request.headers.get("authorization") == f"token {COPILOT_REJECTED_ACCESS}": return _json(401, {"message": "Bad credentials"}) return _json(200, {"endpoints": {"api": f"https://{COPILOT_API_HOST}"}}) + case ("octo.ghe.com", "POST", "/login/device/code") if grant.is_set(): + return _json( + 200, + { + "device_code": _GITHUB_DEVICE_CODE, + "user_code": COPILOT_USER_CODE, + "verification_uri": f"https://{GHE_HOST}/login/device", + "expires_in": 900, + "interval": 5, + }, + ) + case ("octo.ghe.com", "POST", "/login/oauth/access_token") if grant.is_set(): + return _json(200, {"access_token": GHE_ACCESS, "token_type": "bearer"}) + case _ if host in (GHE_API_HOST, GHE_COPILOT_HOST) and _ghe_token(request) != GHE_ACCESS: + return _json(401, {"message": "Bad credentials"}) + case ("api.octo.ghe.com", "GET", "/copilot_internal/user"): + return _json(200, {"endpoints": {"api": f"https://{GHE_COPILOT_HOST}"}}) + case ("api.octo.ghe.com", "GET", "/user"): + return _json(200, {"login": GHE_LOGIN}) + case ("copilot-api.octo.ghe.com", _, _): + return vendor.respond(request) case _: return _switched_off() @@ -342,10 +357,10 @@ def _tunnel( return authority: Final = request_line[1] host: Final = authority.rsplit(":", 1)[0] - if host not in (*AUTH_HOSTS, COPILOT_API_HOST): + if host not in TUNNELED_HOSTS: self.wfile.write(b"HTTP/1.1 403 Forbidden\r\ncontent-length: 0\r\n\r\n") return - is_auth: Final = host in AUTH_HOSTS + is_auth: Final = host != COPILOT_API_HOST if is_auth: authorities.put(authority) if is_auth and switches.refuse_connect.is_set(): @@ -381,15 +396,16 @@ def _tunnel( def device_login_peers( directory: Path, provider: Callable[[Request], Reply] | None = None ) -> Generator[Peers]: - cert, key = write_self_signed_cert(directory, (*AUTH_HOSTS, COPILOT_API_HOST)) + cert, key = write_self_signed_cert(directory, TUNNELED_HOSTS) switches: Final = Switches(threading.Event(), threading.Event(), threading.Event(), threading.Event()) authorities: Final[SimpleQueue[str]] = SimpleQueue() hangups: Final[SimpleQueue[Hangup]] = SimpleQueue() - respond: Final = provider if provider is not None else _provider_api(rv.ResponsesVendor()) + vendor: Final = rv.ResponsesVendor() + respond: Final = provider if provider is not None else _provider_api(vendor) with ( wire_server(respond) as api, wire_server(respond, tls=server_context(cert, key)) as copilot_api, - wire_server(_auth_hosts(api.url, switches.grant), tls=server_context(cert, key)) as auth, + wire_server(_auth_hosts(switches.grant, vendor), tls=server_context(cert, key)) as auth, _tunnel(auth, copilot_api, switches, authorities, hangups) as tunnel, ): yield Peers(_ProviderAPI(api, copilot_api), auth, tunnel, cert, authorities, hangups, switches) diff --git a/tests/integration/messages_endpoint/providers/anthropic/test_anthropic_safeguards_beta_wire.py b/tests/integration/messages_endpoint/providers/anthropic/test_anthropic_safeguards_beta_wire.py index c9e78b1828e..aa8acafb60b 100644 --- a/tests/integration/messages_endpoint/providers/anthropic/test_anthropic_safeguards_beta_wire.py +++ b/tests/integration/messages_endpoint/providers/anthropic/test_anthropic_safeguards_beta_wire.py @@ -677,7 +677,7 @@ def test_github_copilot_messages_forward_safeguards_without_deriving_the_beta( model: Final = scenario.model(model="github_copilot/claude-sonnet-4-5", api_base=peers.api.url) response: Final = _post(owned, messages_body(model, marker), beta=_CLIENT_BETAS_WITH_DANGEROUS) _assert_answered(response, marker) - observed: Final = _only(peers.api.secure) + observed: Final = _only(peers.api.plain) _expect_messages( observed, target="/v1/messages", auth=("authorization", "Bearer synthetic-copilot-token"), beta=None ) diff --git a/tests/integration/providers/test_github_copilot_ghe_per_user.py b/tests/integration/providers/test_github_copilot_ghe_per_user.py new file mode 100644 index 00000000000..a7cd11fe2cb --- /dev/null +++ b/tests/integration/providers/test_github_copilot_ghe_per_user.py @@ -0,0 +1,154 @@ +import uuid +from collections.abc import Iterator, Mapping +from dataclasses import dataclass +from pathlib import Path +from typing import Final +from urllib.parse import urlsplit + +import httpx +import pytest +import yaml +from integration._support import device_login as dl +from integration._support import responses_vendor as rv +from integration._support.client import gateway_from_environment +from integration._support.process import OwnedProxy, owned_proxy_process +from integration._support.wire import Request +from pydantic import JsonValue + +pytestmark: Final = [pytest.mark.timeout(300), pytest.mark.shares_worker_state] + +_CREDENTIAL: Final = "copilot-ghe" +_MODEL: Final = "copilot-ghe-per-user" +_CLIENT_SECONDS: Final = 60 + + +@dataclass(frozen=True, slots=True) +class _Rig: + owned: OwnedProxy + peers: dl.Peers + + def post(self, path: str, key: str, body: Mapping[str, JsonValue]) -> httpx.Response: + with httpx.Client( + base_url=str(self.owned.gateway.client.base_url), timeout=_CLIENT_SECONDS, trust_env=False + ) as client: + return client.post(path, json=dict(body), headers={"Authorization": f"Bearer {key}"}) + + def disconnect(self, key: str) -> None: + with httpx.Client( + base_url=str(self.owned.gateway.client.base_url), timeout=_CLIENT_SECONDS, trust_env=False + ) as client: + response: Final = client.delete( + f"/credentials/{_CREDENTIAL}/user_connection", headers={"Authorization": f"Bearer {key}"} + ) + assert response.status_code == 200, response.text + + +def _config(directory: Path) -> Path: + config: Final = yaml.safe_load(Path("tests/integration/proxy_config.yaml").read_text()) + config["credential_list"] = [ + { + "credential_name": _CREDENTIAL, + "credential_values": {"github_copilot_auth_type": "per_user_oauth", "github_copilot_host": dl.GHE_HOST}, + "credential_info": {"custom_llm_provider": "github_copilot"}, + } + ] + config["model_list"] = [ + { + "model_name": _MODEL, + "litellm_params": { + "model": "github_copilot/gpt-5.4", + "litellm_credential_name": _CREDENTIAL, + "github_copilot_auth_type": "per_user_oauth", + }, + } + ] + path: Final = directory / "copilot-ghe-per-user.yaml" + path.write_text(yaml.safe_dump(config)) + return path + + +@pytest.fixture(scope="module") +def rig(tmp_path_factory: pytest.TempPathFactory) -> Iterator[_Rig]: + directory: Final = tmp_path_factory.mktemp("copilot-ghe-per-user") + copilot_dir: Final = directory / "copilot" + copilot_dir.mkdir() + with ( + gateway_from_environment() as gateway, + dl.device_login_peers(directory) as peers, + owned_proxy_process( + gateway, + directory, + {**peers.environment(), "GITHUB_COPILOT_TOKEN_DIR": str(copilot_dir)}, + config=_config(directory), + workers=2, + ) as owned, + ): + yield _Rig(owned, peers) + + +def _outbound(request: Request) -> tuple[str, str, str, str]: + return ( + request.headers.get("host", ""), + request.method, + urlsplit(request.target).path, + request.headers.get("authorization", ""), + ) + + +def test_a_ghe_user_connects_and_chats_with_their_own_token_on_the_tenant_hosts(rig: _Rig) -> None: + rig.peers.reset() + rig.peers.switches.grant.set() + with rig.owned.gateway.scenario() as scenario: + user_key: Final = scenario.key(user_id=scenario.user(user_role="internal_user")) + scenario.cleanups.callback(rig.disconnect, user_key) + + started: Final = rig.post(f"/credentials/{_CREDENTIAL}/user_connection/start", user_key, {}) + assert started.status_code == 200, started.text + flow: Final = rv.JSON_OBJECT.validate_json(started.text) + assert flow["verification_uri"] == f"https://{dl.GHE_HOST}/login/device", started.text + + polled: Final = rig.post( + f"/credentials/{_CREDENTIAL}/user_connection/poll", user_key, {"flow_handle": flow["flow_handle"]} + ) + assert polled.status_code == 200, polled.text + connection: Final = rv.JSON_OBJECT.validate_json(polled.text) + assert (connection["status"], connection["github_login"]) == ("connected", dl.GHE_LOGIN), polled.text + + marker: Final = uuid.uuid4().hex + chat: Final = rig.post( + "/v1/chat/completions", + user_key, + { + "model": _MODEL, + "messages": [{"role": "user", "content": f"Reply to marker-{marker}"}], + "num_retries": 0, + "cache": {"no-cache": True}, + }, + ) + assert chat.status_code == 200, chat.text + assert set(rv.MARKER.findall(chat.text)) == {marker}, chat.text + + received: Final = rig.peers.auth.drain() + outbound: Final = tuple(_outbound(request) for request in received) + (forwarded,) = (request for request in received if marker.encode() in request.body) + assert _outbound(forwarded) == ( + dl.GHE_COPILOT_HOST, + "POST", + "/chat/completions", + f"Bearer {dl.GHE_ACCESS}", + ), outbound + assert {(host, method, path) for host, method, path, _ in outbound} == { + (dl.GHE_HOST, "POST", "/login/device/code"), + (dl.GHE_HOST, "POST", "/login/oauth/access_token"), + (dl.GHE_API_HOST, "GET", "/copilot_internal/user"), + (dl.GHE_COPILOT_HOST, "GET", "/models"), + (dl.GHE_API_HOST, "GET", "/user"), + (dl.GHE_COPILOT_HOST, "POST", "/chat/completions"), + }, outbound + assert {authorization for host, _, _, authorization in outbound if host == dl.GHE_API_HOST} == { + f"token {dl.GHE_ACCESS}" + }, outbound + assert {authorization for host, _, _, authorization in outbound if host == dl.GHE_COPILOT_HOST} == { + f"Bearer {dl.GHE_ACCESS}" + }, outbound + assert rig.peers.api.drain() == (), "the github.com GITHUB_COPILOT_API_BASE took a ghe.com call" diff --git a/tests/unit/conftest.py b/tests/unit/conftest.py index 7a224232f4c..f3510028974 100644 --- a/tests/unit/conftest.py +++ b/tests/unit/conftest.py @@ -62,6 +62,9 @@ from litellm.llms.custom_httpx.async_client_cleanup import ( # noqa: E402 # sa from litellm.llms.gemini.chat import ( transformation as gemini_chat_transformation_module, # noqa: E402 # same import-time dependency ) +from litellm.llms.github_copilot import ( + common_utils as github_copilot_common_utils_module, # noqa: E402 # same import-time dependency +) from litellm.proxy.db import ( tool_registry_writer as tool_registry_writer_module, # noqa: E402 # same import-time dependency ) @@ -230,6 +233,8 @@ def _flush_client_caches() -> None: litellm.in_memory_llm_clients_cache.flush_cache() image_handling_module.in_memory_cache.flush_cache() image_handling_module.pdf_probe_verdicts.flush_cache() + github_copilot_common_utils_module._PLAN_API_BASES.flush_cache() + litellm_utils_module.ProviderConfigManager._get_provider_anthropic_messages_config_cached.cache_clear() _reset_aws_auth_caches() diff --git a/tests/unit/integrations/test_prometheus_invalid_key_filtering.py b/tests/unit/integrations/test_prometheus_invalid_key_filtering.py index 3f3e2e0d465..e6a12d8667a 100644 --- a/tests/unit/integrations/test_prometheus_invalid_key_filtering.py +++ b/tests/unit/integrations/test_prometheus_invalid_key_filtering.py @@ -19,6 +19,7 @@ from prometheus_client.samples import Sample import litellm from litellm.constants import LITELLM_LOGGING_NO_UPSTREAM_LLM_CALL from litellm.integrations.prometheus import PrometheusLogger +from litellm.llms.github_copilot.github_host import github_host_endpoints from litellm.llms.github_copilot.per_user_auth import GithubCopilotUserSession from litellm.proxy._types import ProxyErrorTypes, ProxyException, UserAPIKeyAuth @@ -223,7 +224,7 @@ class TestSkipMetricsValidation: { "litellm_params": { "github_copilot_user_session": GithubCopilotUserSession( - token="caller-session-token", api_base="https://api.githubcopilot.com" + token="caller-session-token", endpoints=github_host_endpoints("github.com") ) } }, diff --git a/tests/unit/litellm_core_utils/llm_response_utils/test_get_api_base.py b/tests/unit/litellm_core_utils/llm_response_utils/test_get_api_base.py index 07e153f537b..a782276a9b9 100644 --- a/tests/unit/litellm_core_utils/llm_response_utils/test_get_api_base.py +++ b/tests/unit/litellm_core_utils/llm_response_utils/test_get_api_base.py @@ -4,7 +4,7 @@ import respx import litellm from litellm.litellm_core_utils.llm_response_utils import get_api_base as get_api_base_module from litellm.llms.chatgpt.common_utils import CHATGPT_API_BASE -from litellm.llms.github_copilot.common_utils import DEFAULT_GITHUB_COPILOT_API_BASE +from litellm.llms.github_copilot.common_utils import DEFAULT_GITHUB_COPILOT_API_BASE, prefetch_copilot_plan_api_base @pytest.fixture @@ -52,6 +52,14 @@ class TestDeclaredAuthenticatingProvider: assert resolution_lookups == [] assert api_base == expected + def test_copilot_reports_configured_environment_base(self, isolated_token_dirs, resolution_lookups, monkeypatch): + monkeypatch.setenv("GITHUB_COPILOT_API_BASE", "https://configured.example.com/v1") + + api_base = litellm.get_api_base(model="github_copilot/gpt-4o", optional_params={}) + + assert resolution_lookups == [] + assert api_base == "https://configured.example.com/v1" + def test_copilot_uses_the_user_endpoint_for_an_enterprise_api_base(self, isolated_token_dirs, resolution_lookups): token_dir = isolated_token_dirs / "github_copilot" token_dir.mkdir() @@ -61,7 +69,9 @@ class TestDeclaredAuthenticatingProvider: route = router.get("https://api.github.com/copilot_internal/user").respond( json={"endpoints": {"api": "https://api.enterprise.githubcopilot.com"}} ) - api_base = litellm.get_api_base(model="github_copilot/gpt-4o", optional_params={}) + prefetch_copilot_plan_api_base(token) + + api_base = litellm.get_api_base(model="github_copilot/gpt-4o", optional_params={}) assert resolution_lookups == [] assert api_base == "https://api.enterprise.githubcopilot.com" @@ -84,7 +94,10 @@ class TestDeclaredAuthenticatingProvider: @pytest.mark.parametrize( "model, expected", [ - ("gemini/gemini-2.5-pro", "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-pro:generateContent"), + ( + "gemini/gemini-2.5-pro", + "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-pro:generateContent", + ), ("openai/gpt-4o", "https://api.openai.com"), ], ) diff --git a/tests/unit/llms/github_copilot/embedding/test_github_copilot_embedding_transformation.py b/tests/unit/llms/github_copilot/embedding/test_github_copilot_embedding_transformation.py index d4dd8489927..c5e8ddc2ff2 100644 --- a/tests/unit/llms/github_copilot/embedding/test_github_copilot_embedding_transformation.py +++ b/tests/unit/llms/github_copilot/embedding/test_github_copilot_embedding_transformation.py @@ -1,3 +1,5 @@ +import uuid +from typing import Final from unittest.mock import MagicMock import httpx @@ -5,7 +7,7 @@ import pytest from pydantic import ValidationError from litellm.exceptions import AuthenticationError -from litellm.llms.github_copilot.common_utils import GetAccessTokenError +from litellm.llms.github_copilot.common_utils import DEFAULT_GITHUB_COPILOT_API_BASE, GetAccessTokenError from litellm.llms.github_copilot.embedding.transformation import ( GithubCopilotEmbeddingConfig, ) @@ -36,8 +38,8 @@ def test_github_copilot_embedding_config_validate_environment(): assert validated_headers["Authorization"] == f"Bearer {mock_api_key}" assert validated_headers["copilot-integration-id"] == "vscode-chat" - assert validated_headers["editor-version"] == "vscode/1.95.0" - assert "x-request-id" in validated_headers + assert validated_headers["editor-version"] == "vscode/1.115.0" + assert str(uuid.UUID(validated_headers["x-request-id"])) == validated_headers["x-request-id"] # Test with authentication failure config.authenticator.get_api_key.side_effect = GetAccessTokenError( @@ -58,43 +60,57 @@ def test_github_copilot_embedding_config_validate_environment(): assert "Failed to get API key" in str(excinfo.value) -def test_github_copilot_embedding_config_get_complete_url(): - """Test the GitHub Copilot embedding configuration URL generation.""" - config = GithubCopilotEmbeddingConfig() +@pytest.mark.parametrize( + ("deployment_base", "environment_base", "expected_base"), + [ + (None, "https://copilot.example.test/", "https://copilot.example.test"), + (None, "http://localhost:8080/", "http://localhost:8080"), + ("https://deployment.example.test/", "https://env.example.test", "https://deployment.example.test"), + ("http://localhost:9090/", "https://env.example.test", "http://localhost:9090"), + ], +) +def test_github_copilot_embedding_config_get_complete_url( + monkeypatch: pytest.MonkeyPatch, + deployment_base: str | None, + environment_base: str | None, + expected_base: str, +) -> None: + if environment_base is None: + monkeypatch.delenv("GITHUB_COPILOT_API_BASE", raising=False) + else: + monkeypatch.setenv("GITHUB_COPILOT_API_BASE", environment_base) + config: Final = GithubCopilotEmbeddingConfig() config.authenticator = MagicMock() - # Test with default API base - config.authenticator.get_api_base.return_value = None - url = config.get_complete_url( + url: Final = config.get_complete_url( + api_base=deployment_base, + api_key=None, + model="github_copilot/text-embedding-3-small", + optional_params={}, + litellm_params={}, + ) + + assert url == f"{expected_base}/embeddings" + assert config.authenticator.mock_calls == [] + + +def test_github_copilot_embedding_url_falls_back_to_the_default_base_without_a_known_plan_host( + monkeypatch: pytest.MonkeyPatch, +) -> None: + monkeypatch.delenv("GITHUB_COPILOT_API_BASE", raising=False) + config: Final = GithubCopilotEmbeddingConfig() + config.authenticator = MagicMock() + config.authenticator.get_api_key.return_value = "shared-github-token" + + url: Final = config.get_complete_url( api_base=None, api_key=None, model="github_copilot/text-embedding-3-small", optional_params={}, litellm_params={}, ) - assert url == "https://api.githubcopilot.com/embeddings" - # Test with custom API base from authenticator - config.authenticator.get_api_base.return_value = "https://api.enterprise.githubcopilot.com" - url = config.get_complete_url( - api_base=None, - api_key=None, - model="github_copilot/text-embedding-3-small", - optional_params={}, - litellm_params={}, - ) - assert url == "https://api.enterprise.githubcopilot.com/embeddings" - - # Test with custom API base from params - config.authenticator.get_api_base.return_value = None - url = config.get_complete_url( - api_base="https://custom.api.com", - api_key=None, - model="github_copilot/text-embedding-3-small", - optional_params={}, - litellm_params={}, - ) - assert url == "https://custom.api.com/embeddings" + assert url == f"{DEFAULT_GITHUB_COPILOT_API_BASE}/embeddings" def test_github_copilot_embedding_config_transform_request(): @@ -261,12 +277,13 @@ def test_validate_environment_uses_per_user_session_and_skips_authenticator(): """With a per-user session the Authorization header carries the caller's Copilot token and the shared Authenticator is never consulted.""" from litellm.llms.github_copilot.per_user_auth import GithubCopilotUserSession + from litellm.llms.github_copilot.github_host import github_host_endpoints config = GithubCopilotEmbeddingConfig() config.authenticator = MagicMock() config.authenticator.get_api_key.side_effect = AssertionError("shared authenticator must not run") - session = GithubCopilotUserSession(token="user-copilot-token", api_base="https://api.githubcopilot.com") + session = GithubCopilotUserSession(token="user-copilot-token", endpoints=github_host_endpoints("github.com")) headers = config.validate_environment( headers={}, model="github_copilot/text-embedding-3-small", @@ -278,33 +295,47 @@ def test_validate_environment_uses_per_user_session_and_skips_authenticator(): config.authenticator.get_api_key.assert_not_called() -def test_per_user_session_token_wins_over_caller_authorization(): +@pytest.mark.parametrize("authorization_header", ["Authorization", "authorization", "AUTHORIZATION"]) +def test_per_user_session_token_wins_over_caller_authorization(authorization_header: str): from litellm.llms.github_copilot.per_user_auth import GithubCopilotUserSession + from litellm.llms.github_copilot.github_host import github_host_endpoints config = GithubCopilotEmbeddingConfig() config.authenticator = MagicMock() - session = GithubCopilotUserSession(token="user-copilot-token", api_base="https://api.githubcopilot.com") + session = GithubCopilotUserSession(token="user-copilot-token", endpoints=github_host_endpoints("github.com")) headers = config.validate_environment( - headers={"Authorization": "Bearer caller-token"}, + headers={authorization_header: "Bearer caller-token", "x-custom": "preserved"}, model="github_copilot/text-embedding-3-small", messages=[], optional_params={}, litellm_params={"github_copilot_user_session": session}, ) assert headers["Authorization"] == "Bearer user-copilot-token" + assert {key: value for key, value in headers.items() if key.lower() == "authorization"} == { + "Authorization": "Bearer user-copilot-token" + } + assert headers["x-custom"] == "preserved" + assert config.authenticator.mock_calls == [] -def test_get_complete_url_prefers_per_user_session_api_base(): +@pytest.mark.parametrize("deployment_base", [None, "https://deployment.example.test/"]) +def test_get_complete_url_with_per_user_session_ignores_the_deployment_base( + monkeypatch: pytest.MonkeyPatch, deployment_base: str | None +) -> None: from litellm.llms.github_copilot.per_user_auth import GithubCopilotUserSession + from litellm.llms.github_copilot.github_host import github_host_endpoints - config = GithubCopilotEmbeddingConfig() + monkeypatch.setenv("GITHUB_COPILOT_API_BASE", "http://localhost:8080/") + config: Final = GithubCopilotEmbeddingConfig() config.authenticator = MagicMock() - session = GithubCopilotUserSession(token="t", api_base="https://tenant.githubcopilot.com") - url = config.get_complete_url( - api_base="https://attacker.example", + session: Final = GithubCopilotUserSession(token="user-oauth-token", endpoints=github_host_endpoints("github.com")) + url: Final = config.get_complete_url( + api_base=deployment_base, api_key=None, - model="m", + model="github_copilot/text-embedding-3-small", optional_params={}, litellm_params={"github_copilot_user_session": session}, ) - assert url == "https://tenant.githubcopilot.com/embeddings" + + assert url == "http://localhost:8080/embeddings" + assert config.authenticator.mock_calls == [] diff --git a/tests/unit/llms/github_copilot/messages/test_github_copilot_messages_transformation.py b/tests/unit/llms/github_copilot/messages/test_github_copilot_messages_transformation.py index fbdf1ba4591..2f75ea290cd 100644 --- a/tests/unit/llms/github_copilot/messages/test_github_copilot_messages_transformation.py +++ b/tests/unit/llms/github_copilot/messages/test_github_copilot_messages_transformation.py @@ -5,100 +5,81 @@ import httpx import pytest from litellm.exceptions import AuthenticationError -from litellm.llms.github_copilot.common_utils import GetAccessTokenError +from litellm.llms.github_copilot.common_utils import DEFAULT_GITHUB_COPILOT_API_BASE, GetAccessTokenError from litellm.llms.github_copilot.messages.transformation import ( GithubCopilotAnthropicMessagesConfig, ) +from litellm.llms.github_copilot.github_host import github_host_endpoints from litellm.llms.github_copilot.per_user_auth import ( GITHUB_COPILOT_USER_SESSION_KWARG_KEY, GithubCopilotUserSession, ) -def test_github_copilot_anthropic_messages_get_complete_url(): - """get_complete_url builds the /v1/messages URL from the base it is handed. - - In the request flow that ``api_base`` is the value already resolved by - validate_anthropic_messages_environment (the authenticated Copilot host); the - caller-supplied base is discarded there, not here (see the validate tests). - """ - config = GithubCopilotAnthropicMessagesConfig() +@pytest.mark.parametrize( + ("resolved_base", "environment_base", "expected_base"), + [ + (None, "https://copilot.example.test/", "https://copilot.example.test"), + (None, "http://localhost:8080/", "http://localhost:8080"), + ("https://resolved.example.test", "https://env.example.test", "https://resolved.example.test"), + ("https://resolved.example.test/", "https://env.example.test", "https://resolved.example.test"), + ("https://resolved.example.test/v1/messages", None, "https://resolved.example.test"), + ], +) +def test_github_copilot_anthropic_messages_get_complete_url( + monkeypatch: pytest.MonkeyPatch, + resolved_base: str | None, + environment_base: str | None, + expected_base: str, +) -> None: + if environment_base is None: + monkeypatch.delenv("GITHUB_COPILOT_API_BASE", raising=False) + else: + monkeypatch.setenv("GITHUB_COPILOT_API_BASE", environment_base) + config: Final = GithubCopilotAnthropicMessagesConfig() config.authenticator = MagicMock() - config.authenticator.get_api_base.return_value = None - # No api_base supplied and no authenticator base -> default Copilot endpoint. - url = config.get_complete_url( + url: Final = config.get_complete_url( + api_base=resolved_base, + api_key=None, + model="github_copilot/claude-haiku-4.5", + optional_params={}, + litellm_params={}, + ) + + assert url == f"{expected_base}/v1/messages" + assert config.authenticator.mock_calls == [] + + +def test_github_copilot_anthropic_messages_url_falls_back_to_the_default_base_without_a_known_plan_host( + monkeypatch: pytest.MonkeyPatch, +) -> None: + monkeypatch.delenv("GITHUB_COPILOT_API_BASE", raising=False) + config: Final = GithubCopilotAnthropicMessagesConfig() + config.authenticator = MagicMock() + config.authenticator.get_api_key.return_value = "shared-github-token" + + url: Final = config.get_complete_url( api_base=None, api_key=None, model="github_copilot/claude-haiku-4.5", optional_params={}, litellm_params={}, ) - assert url == "https://api.githubcopilot.com/v1/messages" - # Falls back to a single authenticator read, not a hard-coded second one. - config.authenticator.get_api_base.assert_called() - # The resolved (validated) base passed in is reused verbatim; no extra read. - config.authenticator.get_api_base.reset_mock() - url = config.get_complete_url( - api_base="https://api.business.githubcopilot.com", - api_key=None, - model="github_copilot/claude-haiku-4.5", - optional_params={}, - litellm_params={}, - ) - assert url == "https://api.business.githubcopilot.com/v1/messages" - config.authenticator.get_api_base.assert_not_called() - - # A trailing slash on the base must not produce a double-slash URL. - url = config.get_complete_url( - api_base="https://api.business.githubcopilot.com/", - api_key=None, - model="github_copilot/claude-haiku-4.5", - optional_params={}, - litellm_params={}, - ) - assert url == "https://api.business.githubcopilot.com/v1/messages" - - # An already-complete /v1/messages base is left untouched. - url = config.get_complete_url( - api_base="https://api.githubcopilot.com/v1/messages", - api_key=None, - model="github_copilot/claude-haiku-4.5", - optional_params={}, - litellm_params={}, - ) - assert url == "https://api.githubcopilot.com/v1/messages" + assert url == f"{DEFAULT_GITHUB_COPILOT_API_BASE}/v1/messages" -def test_github_copilot_anthropic_messages_get_complete_url_normalizes_authenticator_trailing_slash(): - """A tenant base with a trailing slash from the authenticator fallback must - not yield a double-slash URL.""" - config = GithubCopilotAnthropicMessagesConfig() - config.authenticator = MagicMock() - config.authenticator.get_api_base.return_value = "https://api.business.githubcopilot.com/" - - url = config.get_complete_url( - api_base=None, - api_key=None, - model="github_copilot/claude-haiku-4.5", - optional_params={}, - litellm_params={}, - ) - assert url == "https://api.business.githubcopilot.com/v1/messages" - - -def test_github_copilot_anthropic_messages_validate_environment(): +def test_github_copilot_anthropic_messages_validate_environment(monkeypatch: pytest.MonkeyPatch): """Test environment validation and header injection.""" + monkeypatch.delenv("GITHUB_COPILOT_API_BASE", raising=False) config = GithubCopilotAnthropicMessagesConfig() # Mock the authenticator config.authenticator = MagicMock() config.authenticator.get_api_key.return_value = "gh.test-key-123" - config.authenticator.get_api_base.return_value = None headers = {} - # Pass a hostile api_base to confirm it is ignored. validated_headers, api_base = config.validate_anthropic_messages_environment( headers=headers, model="github_copilot/claude-haiku-4.5", @@ -106,12 +87,12 @@ def test_github_copilot_anthropic_messages_validate_environment(): optional_params={}, litellm_params={}, api_key=None, - api_base="https://attacker.example.com", + api_base=None, ) assert "copilot-integration-id" in validated_headers assert validated_headers["copilot-integration-id"] == "vscode-chat" - assert "Authorization" in validated_headers + assert validated_headers["Authorization"] == "Bearer gh.test-key-123" assert "anthropic-version" in validated_headers assert validated_headers["anthropic-version"] == "2023-06-01" # /v1/messages must use the messages-proxy intent so the Copilot backend @@ -119,7 +100,7 @@ def test_github_copilot_anthropic_messages_validate_environment(): assert validated_headers["openai-intent"] == "messages-proxy" assert validated_headers["x-interaction-type"] == "messages-proxy" assert validated_headers["x-github-api-version"] == "2026-06-01" - assert api_base == "https://api.githubcopilot.com" + assert api_base == DEFAULT_GITHUB_COPILOT_API_BASE def test_github_copilot_anthropic_messages_validate_environment_injects_beta_headers(): @@ -129,7 +110,6 @@ def test_github_copilot_anthropic_messages_validate_environment_injects_beta_hea config = GithubCopilotAnthropicMessagesConfig() config.authenticator = MagicMock() config.authenticator.get_api_key.return_value = "gh.test-key" - config.authenticator.get_api_base.return_value = None validated_headers, _ = config.validate_anthropic_messages_environment( headers={}, @@ -150,7 +130,6 @@ def test_github_copilot_anthropic_messages_validate_environment_preserves_caller config = GithubCopilotAnthropicMessagesConfig() config.authenticator = MagicMock() config.authenticator.get_api_key.return_value = "gh.test-key" - config.authenticator.get_api_base.return_value = None validated_headers, _ = config.validate_anthropic_messages_environment( headers={"anthropic-version": "2024-10-22"}, @@ -171,7 +150,6 @@ def test_github_copilot_anthropic_messages_validate_environment_injects_context_ config = GithubCopilotAnthropicMessagesConfig() config.authenticator = MagicMock() config.authenticator.get_api_key.return_value = "gh.test-key" - config.authenticator.get_api_base.return_value = None validated_headers, _ = config.validate_anthropic_messages_environment( headers={}, @@ -191,7 +169,7 @@ def test_github_copilot_anthropic_messages_validate_environment_injects_context_ @pytest.mark.usefixtures("local_beta_headers_config") def test_github_copilot_safeguards_do_not_derive_the_dangerous_tool_use_beta() -> None: - session: Final = GithubCopilotUserSession(token="gh.test-key", api_base="https://api.githubcopilot.com") + session: Final = GithubCopilotUserSession(token="gh.test-key", endpoints=github_host_endpoints("github.com")) validated_headers, _ = GithubCopilotAnthropicMessagesConfig().validate_anthropic_messages_environment( headers={}, @@ -271,25 +249,48 @@ def test_provider_config_manager_skips_non_claude_copilot_models(): assert config is None -def test_github_copilot_anthropic_messages_validate_environment_normalizes_trailing_slash(): - """A tenant base with a trailing slash from the authenticator must be - normalized so the URL built downstream has no double slash.""" - config = GithubCopilotAnthropicMessagesConfig() +@pytest.mark.parametrize( + ("deployment_base", "environment_base", "expected_base"), + [ + (None, None, DEFAULT_GITHUB_COPILOT_API_BASE), + (None, "https://copilot.example.test/", "https://copilot.example.test"), + (None, "http://localhost:8080/", "http://localhost:8080"), + ("https://deployment.example.test/", "https://copilot.example.test/", "https://deployment.example.test"), + ], +) +def test_validate_environment_uses_the_deployment_base_else_the_configured_endpoint( + monkeypatch: pytest.MonkeyPatch, deployment_base: str | None, environment_base: str | None, expected_base: str +) -> None: + if environment_base is None: + monkeypatch.delenv("GITHUB_COPILOT_API_BASE", raising=False) + else: + monkeypatch.setenv("GITHUB_COPILOT_API_BASE", environment_base) + config: Final = GithubCopilotAnthropicMessagesConfig() config.authenticator = MagicMock() config.authenticator.get_api_key.return_value = "gh.test-key" - config.authenticator.get_api_base.return_value = "https://api.business.githubcopilot.com/" - _, api_base = config.validate_anthropic_messages_environment( + headers, api_base = config.validate_anthropic_messages_environment( headers={}, model="github_copilot/claude-haiku-4.5", messages=[{"role": "user", "content": "Hello"}], optional_params={}, litellm_params={}, api_key=None, - api_base="https://attacker.example.com", + api_base=deployment_base, ) - assert api_base == "https://api.business.githubcopilot.com" + assert api_base == expected_base + assert headers["Authorization"] == "Bearer gh.test-key" + assert ( + config.get_complete_url( + api_base=api_base, + api_key=None, + model="github_copilot/claude-haiku-4.5", + optional_params={}, + litellm_params={}, + ) + == f"{expected_base}/v1/messages" + ) def test_github_copilot_config_disables_anthropic_beta_filtering(): @@ -307,7 +308,6 @@ def test_github_copilot_config_disables_anthropic_beta_filtering(): config.authenticator = MagicMock() config.authenticator.get_api_key.return_value = "gh.test-key" - config.authenticator.get_api_base.return_value = None headers, _ = config.validate_anthropic_messages_environment( headers={}, @@ -351,42 +351,73 @@ def test_github_copilot_messages_config_probes_capabilities_under_copilot_namesp assert GithubCopilotAnthropicMessagesConfig().custom_llm_provider == "github_copilot" -def test_validate_environment_uses_per_user_session_and_skips_authenticator(): +@pytest.mark.parametrize( + ("deployment_base", "environment_base", "expected_base"), + [ + (None, None, DEFAULT_GITHUB_COPILOT_API_BASE), + (None, "http://localhost:8080/", "http://localhost:8080"), + ("https://deployment.example.test", "http://localhost:8080/", "http://localhost:8080"), + ("https://deployment.example.test", None, DEFAULT_GITHUB_COPILOT_API_BASE), + ], +) +def test_validate_environment_uses_per_user_session_and_skips_authenticator( + monkeypatch: pytest.MonkeyPatch, deployment_base: str | None, environment_base: str | None, expected_base: str +) -> None: from litellm.llms.github_copilot.per_user_auth import GithubCopilotUserSession + if environment_base is None: + monkeypatch.delenv("GITHUB_COPILOT_API_BASE", raising=False) + else: + monkeypatch.setenv("GITHUB_COPILOT_API_BASE", environment_base) + config = GithubCopilotAnthropicMessagesConfig() config.authenticator = MagicMock() config.authenticator.get_api_key.side_effect = AssertionError("shared authenticator must not run") - config.authenticator.get_api_base.side_effect = AssertionError("shared authenticator must not run") - session = GithubCopilotUserSession(token="user-copilot-token", api_base="https://tenant.githubcopilot.com/") + session = GithubCopilotUserSession(token="user-copilot-token", endpoints=github_host_endpoints("github.com")) headers, api_base = config.validate_anthropic_messages_environment( headers={}, model="github_copilot/claude-sonnet-5.5", messages=[], optional_params={}, litellm_params={"github_copilot_user_session": session}, + api_base=deployment_base, ) assert headers["Authorization"] == "Bearer user-copilot-token" - assert api_base == "https://tenant.githubcopilot.com" - config.authenticator.get_api_key.assert_not_called() + assert api_base == expected_base + assert config.authenticator.mock_calls == [] + assert ( + config.get_complete_url( + api_base=api_base, + api_key=None, + model="github_copilot/claude-sonnet-5.5", + optional_params={}, + litellm_params={"github_copilot_user_session": session}, + ) + == f"{expected_base}/v1/messages" + ) -def test_per_user_session_token_wins_over_caller_authorization(): +@pytest.mark.parametrize("authorization_header", ["Authorization", "authorization", "AUTHORIZATION"]) +def test_per_user_session_token_wins_over_caller_authorization(authorization_header: str): from litellm.llms.github_copilot.per_user_auth import GithubCopilotUserSession config = GithubCopilotAnthropicMessagesConfig() config.authenticator = MagicMock() - config.authenticator.get_api_base.return_value = "https://api.githubcopilot.com" - session = GithubCopilotUserSession(token="user-copilot-token", api_base="https://api.githubcopilot.com") + session = GithubCopilotUserSession(token="user-copilot-token", endpoints=github_host_endpoints("github.com")) headers, _ = config.validate_anthropic_messages_environment( - headers={"authorization": "Bearer caller-token"}, + headers={authorization_header: "Bearer caller-token", "x-custom": "preserved"}, model="github_copilot/claude-sonnet-4.5", messages=[], optional_params={}, litellm_params={"github_copilot_user_session": session}, ) assert headers["Authorization"] == "Bearer user-copilot-token" + assert {key: value for key, value in headers.items() if key.lower() == "authorization"} == { + "Authorization": "Bearer user-copilot-token" + } + assert headers["x-custom"] == "preserved" + assert config.authenticator.mock_calls == [] def test_transform_response_carries_upstream_usage(): diff --git a/tests/unit/llms/github_copilot/responses/test_github_copilot_responses_transformation.py b/tests/unit/llms/github_copilot/responses/test_github_copilot_responses_transformation.py index c3a62592039..4858c071fb1 100644 --- a/tests/unit/llms/github_copilot/responses/test_github_copilot_responses_transformation.py +++ b/tests/unit/llms/github_copilot/responses/test_github_copilot_responses_transformation.py @@ -7,18 +7,35 @@ transformations for the Responses API. Source: litellm/llms/github_copilot/responses/transformation.py """ -from unittest.mock import patch, MagicMock +from typing import Final +import os +import uuid +from unittest.mock import MagicMock, patch import httpx import pytest + import litellm from litellm.litellm_core_utils.get_model_cost_map import get_model_cost_map -from litellm.types.utils import LlmProviders -from litellm.utils import ProviderConfigManager +from litellm.llms.github_copilot.common_utils import DEFAULT_GITHUB_COPILOT_API_BASE +from litellm.llms.github_copilot.github_host import github_host_endpoints +from litellm.llms.github_copilot.per_user_auth import GithubCopilotUserSession from litellm.llms.github_copilot.responses.transformation import ( GithubCopilotResponsesAPIConfig, ) from litellm.types.llms.openai import ResponsesAPIOptionalRequestParams +from litellm.types.utils import LlmProviders +from litellm.utils import ProviderConfigManager + + +STORED_ACCESS_TOKEN: Final = "stored-oauth-access-token" + + +@pytest.fixture +def stored_access_token(tmp_path, monkeypatch): + monkeypatch.setenv("GITHUB_COPILOT_TOKEN_DIR", str(tmp_path)) + monkeypatch.delenv("GITHUB_COPILOT_ACCESS_TOKEN_FILE", raising=False) + (tmp_path / "access-token").write_text(STORED_ACCESS_TOKEN) @pytest.fixture(autouse=True) @@ -48,52 +65,116 @@ class TestGithubCopilotResponsesAPITransformation: ) assert config.custom_llm_provider == LlmProviders.GITHUB_COPILOT, "custom_llm_provider should be GITHUB_COPILOT" - @patch("litellm.llms.github_copilot.responses.transformation.Authenticator") - def test_github_copilot_responses_endpoint_url(self, mock_authenticator_class): - """Test that get_complete_url returns correct GitHub Copilot endpoint""" - # Mock authenticator to return default base - mock_auth_instance = MagicMock() - mock_auth_instance.get_api_base.return_value = "https://api.individual.githubcopilot.com" - mock_authenticator_class.return_value = mock_auth_instance - + @pytest.mark.parametrize( + ("api_base", "environment_base", "shared_base", "per_user_base"), + ( + (None, "https://copilot-env.example", "https://copilot-env.example", "https://copilot-env.example"), + ("http://localhost:8080", "https://copilot-env.example", "http://localhost:8080", "https://copilot-env.example"), + ("https://copilot-deployment.example/", None, "https://copilot-deployment.example", DEFAULT_GITHUB_COPILOT_API_BASE), + ), + ) + @pytest.mark.parametrize("per_user", (False, True)) + def test_github_copilot_responses_endpoint_url( + self, + api_base: str | None, + environment_base: str | None, + shared_base: str, + per_user_base: str, + per_user: bool, + monkeypatch: pytest.MonkeyPatch, + ): + if environment_base is None: + monkeypatch.delenv("GITHUB_COPILOT_API_BASE", raising=False) + else: + monkeypatch.setenv("GITHUB_COPILOT_API_BASE", environment_base) config = GithubCopilotResponsesAPIConfig() - - # Test with default GitHub Copilot API base (from authenticator) - url = config.get_complete_url(api_base=None, litellm_params={}) - assert url == "https://api.individual.githubcopilot.com/responses", ( - f"Expected GitHub Copilot responses endpoint, got {url}" + config.authenticator = MagicMock() + url = config.get_complete_url( + api_base=api_base, + litellm_params=( + {"github_copilot_user_session": GithubCopilotUserSession(token="user-github-oauth-token", endpoints=github_host_endpoints("github.com"))} + if per_user + else {} + ), ) + assert url == f"{per_user_base if per_user else shared_base}/responses" + config.authenticator.get_api_key.assert_not_called() - # Test with custom api_base (overrides authenticator) - custom_url = config.get_complete_url(api_base="https://custom.githubcopilot.com", litellm_params={}) - assert custom_url == "https://custom.githubcopilot.com/responses", f"Expected custom endpoint, got {custom_url}" + def test_github_copilot_responses_url_falls_back_to_the_default_base_without_a_known_plan_host(self, monkeypatch): + monkeypatch.delenv("GITHUB_COPILOT_API_BASE", raising=False) + config = GithubCopilotResponsesAPIConfig() + config.authenticator = MagicMock() + config.authenticator.get_api_key.return_value = "shared-github-token" - # Test with trailing slash - url_with_slash = config.get_complete_url(api_base="https://api.githubcopilot.com/", litellm_params={}) - assert url_with_slash == "https://api.githubcopilot.com/responses", "Should handle trailing slash" + url = config.get_complete_url(api_base=None, litellm_params={}) - @patch("litellm.llms.github_copilot.responses.transformation.Authenticator") - def test_validate_environment_default_headers(self, mock_authenticator_class): - """Test that validate_environment generates correct default headers""" - # Mock the authenticator - mock_auth_instance = MagicMock() - mock_auth_instance.get_api_key.return_value = "test-api-key-123" - mock_authenticator_class.return_value = mock_auth_instance + assert url == f"{DEFAULT_GITHUB_COPILOT_API_BASE}/responses" + + @pytest.mark.usefixtures("stored_access_token") + def test_validate_environment_default_headers(self, monkeypatch): + for environment_variable in ( + "GITHUB_COPILOT_OPENAI_INTENT", + "GITHUB_COPILOT_API_VERSION", + "GITHUB_COPILOT_USER_AGENT_LIBRARY_VERSION", + ): + monkeypatch.delenv(environment_variable, raising=False) config = GithubCopilotResponsesAPIConfig() - headers = config.validate_environment(headers={}, model="gpt-5.1-codex", litellm_params={}) + next_headers = config.validate_environment(headers={}, model="gpt-5.1-codex", litellm_params={}) - # Check required headers - assert headers["Authorization"] == "Bearer test-api-key-123" + assert headers["Authorization"] == f"Bearer {STORED_ACCESS_TOKEN}" + assert headers["accept"] == "application/json" assert headers["content-type"] == "application/json" assert headers["copilot-integration-id"] == "vscode-chat" - assert headers["editor-version"] == "vscode/1.95.0" + assert headers["editor-version"] == "vscode/1.115.0" assert headers["editor-plugin-version"] == "copilot-chat/0.26.7" assert headers["user-agent"] == "GitHubCopilotChat/0.26.7" assert headers["openai-intent"] == "conversation-panel" assert headers["x-github-api-version"] == "2025-04-01" - assert "x-request-id" in headers + assert headers["x-vscode-user-agent-library-version"] == "electron-fetch" + assert str(uuid.UUID(headers["x-request-id"])) == headers["x-request-id"] + assert next_headers["x-request-id"] != headers["x-request-id"] + + @pytest.mark.usefixtures("stored_access_token") + def test_validate_environment_headers_from_environment(self): + environment = { + "GITHUB_COPILOT_ACCEPT": "application/vnd.github+json", + "GITHUB_COPILOT_CONTENT_TYPE": "application/custom+json", + "GITHUB_COPILOT_INTEGRATION_ID": "custom-integration", + "GITHUB_COPILOT_EDITOR_VERSION": "custom-editor/1.0", + "GITHUB_COPILOT_EDITOR_PLUGIN_VERSION": "custom-plugin/2.0", + "GITHUB_COPILOT_USER_AGENT": "CustomAgent/2.0", + "GITHUB_COPILOT_OPENAI_INTENT": "custom-intent", + "GITHUB_COPILOT_API_VERSION": "2099-01-01", + "GITHUB_COPILOT_USER_AGENT_LIBRARY_VERSION": "custom-library", + } + + with patch.dict(os.environ, environment): + headers = GithubCopilotResponsesAPIConfig().validate_environment( + headers={}, model="gpt-5.1-codex", litellm_params={} + ) + + assert headers["Authorization"] == f"Bearer {STORED_ACCESS_TOKEN}" + assert headers["accept"] == "application/vnd.github+json" + assert headers["content-type"] == "application/custom+json" + assert headers["copilot-integration-id"] == "custom-integration" + assert headers["editor-version"] == "custom-editor/1.0" + assert headers["editor-plugin-version"] == "custom-plugin/2.0" + assert headers["user-agent"] == "CustomAgent/2.0" + assert headers["openai-intent"] == "custom-intent" + assert headers["x-github-api-version"] == "2099-01-01" + assert headers["x-vscode-user-agent-library-version"] == "custom-library" + + @pytest.mark.usefixtures("stored_access_token") + def test_empty_environment_header_omits_default(self): + + with patch.dict(os.environ, {"GITHUB_COPILOT_USER_AGENT": ""}): + headers = GithubCopilotResponsesAPIConfig().validate_environment( + headers={}, model="gpt-5.1-codex", litellm_params={} + ) + + assert "user-agent" not in headers @patch("litellm.llms.github_copilot.responses.transformation.Authenticator") def test_validate_environment_user_headers_override(self, mock_authenticator_class): @@ -756,37 +837,34 @@ class TestGithubCopilotReasoningStreamItemIdNormalization: def test_validate_environment_uses_per_user_session_and_skips_authenticator(): - from litellm.llms.github_copilot.per_user_auth import GithubCopilotUserSession - config = GithubCopilotResponsesAPIConfig() config.authenticator = MagicMock() config.authenticator.get_api_key.side_effect = AssertionError("shared authenticator must not run") - session = GithubCopilotUserSession(token="user-copilot-token", api_base="https://api.githubcopilot.com") + session = GithubCopilotUserSession(token="user-github-oauth-token", endpoints=github_host_endpoints("github.com")) headers = config.validate_environment( headers={}, model="github_copilot/gpt-5.4", litellm_params={"github_copilot_user_session": session}, ) - assert headers["Authorization"] == "Bearer user-copilot-token" + assert headers["Authorization"] == f"Bearer {session.token}" config.authenticator.get_api_key.assert_not_called() def test_per_user_session_token_wins_over_caller_authorization(): """extra_headers["Authorization"] (any casing) must never displace the per-user session token on the outgoing request.""" - from litellm.llms.github_copilot.per_user_auth import GithubCopilotUserSession - config = GithubCopilotResponsesAPIConfig() config.authenticator = MagicMock() - session = GithubCopilotUserSession(token="user-copilot-token", api_base="https://api.githubcopilot.com") + session = GithubCopilotUserSession(token="user-github-oauth-token", endpoints=github_host_endpoints("github.com")) headers = config.validate_environment( headers={"Authorization": "Bearer caller-token", "authorization": "Bearer caller-token-lower"}, model="github_copilot/gpt-5.1", litellm_params={"github_copilot_user_session": session}, ) - assert headers["Authorization"] == "Bearer user-copilot-token" + assert headers["Authorization"] == f"Bearer {session.token}" assert "authorization" not in headers + config.authenticator.get_api_key.assert_not_called() def test_shared_mode_keeps_caller_authorization(): diff --git a/tests/unit/llms/github_copilot/test_github_copilot_authenticator.py b/tests/unit/llms/github_copilot/test_github_copilot_authenticator.py index 8eb70d366b1..c7e124fbe42 100644 --- a/tests/unit/llms/github_copilot/test_github_copilot_authenticator.py +++ b/tests/unit/llms/github_copilot/test_github_copilot_authenticator.py @@ -1,384 +1,495 @@ +import json +import logging import os +import uuid from concurrent.futures import ThreadPoolExecutor from pathlib import Path from typing import Final -from unittest.mock import MagicMock, patch +from unittest.mock import MagicMock, mock_open, patch import pytest import respx -from litellm.llms.github_copilot.authenticator import Authenticator, github_api_headers -from litellm.llms.github_copilot.common_utils import GetAccessTokenError, GetDeviceCodeError +from litellm.llms.github_copilot.authenticator import Authenticator +from litellm.llms.github_copilot.common_utils import ( + DEFAULT_COPILOT_EDITOR_PLUGIN_VERSION, + DEFAULT_COPILOT_EDITOR_VERSION, + DEFAULT_COPILOT_INTEGRATION_ID, + DEFAULT_COPILOT_USER_AGENT, + DEFAULT_GITHUB_COPILOT_API_BASE, + GetAccessTokenError, + GetDeviceCodeError, + InvalidAPIBaseError, + get_copilot_api_base, + get_copilot_default_headers, +) -COPILOT_USER_URL: Final = "https://api.github.com/copilot_internal/user" -DEVICE_CODE_URL: Final = "https://github.com/login/device/code" -TENANT_API: Final = "https://api.tenant.githubcopilot.com" TRUNCATED_UTF_16: Final = b"\xff\xfe\x00\xd8" @pytest.fixture -def logged_in_token_dir(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> tuple[Path, str]: +def logged_in_token_dir(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> Path: monkeypatch.setenv("GITHUB_COPILOT_TOKEN_DIR", str(tmp_path)) - token: Final = f"gho_{tmp_path.name}" - (tmp_path / "access-token").write_text(token) - return tmp_path, token + (tmp_path / "access-token").write_text("gho_logged_in") + return tmp_path -def test_init_uses_access_token_path(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None: - monkeypatch.setenv("GITHUB_COPILOT_TOKEN_DIR", str(tmp_path)) - auth: Final = Authenticator() - assert auth.token_dir == str(tmp_path) - assert auth.access_token_file == str(tmp_path / "access-token") +class TestGitHubCopilotAuthenticator: + @pytest.fixture + def authenticator(self): + with ( + patch("os.path.exists", return_value=False), + patch("os.makedirs") as mock_makedirs, + ): + auth = Authenticator() + mock_makedirs.assert_called_once() + return auth + @pytest.fixture + def mock_http_client(self): + mock_client = MagicMock() + mock_response = MagicMock() + mock_client.get.return_value = mock_response + mock_client.post.return_value = mock_response + mock_response.raise_for_status.return_value = None + return mock_client, mock_response -def test_init_uses_default_access_token_path(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None: - monkeypatch.setenv("HOME", str(tmp_path)) - monkeypatch.delenv("GITHUB_COPILOT_TOKEN_DIR", raising=False) + def test_init(self): + """Test the initialization of the authenticator.""" + with ( + patch("os.path.exists", return_value=False), + patch("os.makedirs") as mock_makedirs, + ): + auth = Authenticator() + assert auth.token_dir.endswith("/github_copilot") + assert auth.access_token_file.endswith("/access-token") + mock_makedirs.assert_called_once() - auth: Final = Authenticator() + def test_ensure_token_dir_creates_missing_directory(self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None: + monkeypatch.setenv("GITHUB_COPILOT_TOKEN_DIR", str(tmp_path / "github_copilot")) + assert Path(Authenticator().token_dir).is_dir() - assert auth.token_dir == str(tmp_path / ".config/litellm/github_copilot") - assert auth.access_token_file == str(tmp_path / ".config/litellm/github_copilot/access-token") - - -def test_ensure_token_dir_creates_missing_directory(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None: - token_dir: Final = str(tmp_path / "github_copilot") - monkeypatch.setenv("GITHUB_COPILOT_TOKEN_DIR", token_dir) - - auth: Final = Authenticator() - - assert Path(auth.token_dir).is_dir() - - -def test_get_github_headers_include_access_token() -> None: - headers: Final = github_api_headers("github-token") - - assert headers == { - "accept": "application/json", - "editor-version": "vscode/1.85.1", - "editor-plugin-version": "copilot/1.155.0", - "user-agent": "GithubCopilot/1.155.0", - "accept-encoding": "gzip,deflate,br", - "content-type": "application/json", - "authorization": "token github-token", - } - - -def test_get_access_token_reads_saved_token(logged_in_token_dir: tuple[Path, str]) -> None: - _, token = logged_in_token_dir - assert Authenticator().get_access_token() == token - - -def test_get_access_token_logs_in_and_saves_token( - logged_in_token_dir: tuple[Path, str], -) -> None: - token_dir, _ = logged_in_token_dir - access_token_file: Final = token_dir / "access-token" - access_token_file.unlink() - auth: Final = Authenticator() - - with patch.object(auth, "_login", return_value="github-access-token") as login: - assert auth.get_access_token() == "github-access-token" - - login.assert_called_once() - assert access_token_file.read_text() == "github-access-token" - - -def test_get_api_key_returns_github_token_without_http( - logged_in_token_dir: tuple[Path, str], -) -> None: - _, token = logged_in_token_dir - with patch("litellm.llms.github_copilot.authenticator.get_httpx_client") as get_client: - assert Authenticator().get_api_key() == token - get_client.assert_not_called() - - -def test_get_api_base_uses_user_endpoint_and_memoizes_per_access_token( - logged_in_token_dir: tuple[Path, str], -) -> None: - _, token = logged_in_token_dir - with respx.mock(assert_all_called=True) as router: - route: Final = router.get(COPILOT_USER_URL).respond(json={"endpoints": {"api": TENANT_API}}) - auth: Final = Authenticator() - assert auth.get_api_base() == TENANT_API - assert Authenticator().get_api_base() == TENANT_API - - assert route.call_count == 1 - assert route.calls.last.request.headers["authorization"] == f"token {token}" - - -@pytest.mark.parametrize("status_code", [401, 403, 500]) -def test_get_api_base_returns_none_when_user_lookup_fails( - logged_in_token_dir: tuple[Path, str], status_code: int -) -> None: - with respx.mock(assert_all_called=True) as router: - router.get(COPILOT_USER_URL).respond(status_code=status_code) - assert Authenticator().get_api_base() is None - - -def test_get_api_base_retries_after_failed_user_lookup(logged_in_token_dir: tuple[Path, str]) -> None: - auth: Final = Authenticator() - with respx.mock(assert_all_called=True) as router: - failed_lookup: Final = router.get(COPILOT_USER_URL).respond(status_code=503) - assert auth.get_api_base() is None - assert failed_lookup.call_count == 1 - - with respx.mock(assert_all_called=True) as router: - successful_lookup: Final = router.get(COPILOT_USER_URL).respond(json={"endpoints": {"api": TENANT_API}}) - assert auth.get_api_base() == TENANT_API - assert successful_lookup.call_count == 1 - - -def test_get_api_base_returns_none_when_user_lookup_response_has_no_endpoint( - logged_in_token_dir: tuple[Path, str], -) -> None: - with respx.mock(assert_all_called=True) as router: - router.get(COPILOT_USER_URL).respond(json={"endpoints": {}}) - assert Authenticator().get_api_base() is None - - -def test_get_api_base_returns_none_when_user_lookup_endpoints_are_not_a_mapping( - logged_in_token_dir: tuple[Path, str], -) -> None: - with respx.mock(assert_all_called=True) as router: - router.get(COPILOT_USER_URL).respond(json={"endpoints": []}) - assert Authenticator().get_api_base() is None - - -def test_get_api_base_rejects_untrusted_user_endpoint( - logged_in_token_dir: tuple[Path, str], -) -> None: - with respx.mock(assert_all_called=True) as router: - router.get(COPILOT_USER_URL).respond(json={"endpoints": {"api": "https://attacker.example"}}) - assert Authenticator().get_api_base() is None - - -def test_get_api_base_does_not_trigger_device_login_without_saved_token( - tmp_path: Path, monkeypatch: pytest.MonkeyPatch -) -> None: - monkeypatch.setenv("GITHUB_COPILOT_TOKEN_DIR", str(tmp_path)) - with patch("litellm.llms.github_copilot.authenticator.get_httpx_client") as get_client: - assert Authenticator().get_api_base() is None - get_client.assert_not_called() - - -@pytest.mark.asyncio -async def test_get_access_token_refuses_device_code_login_in_event_loop() -> None: - auth: Final = Authenticator() - with ( - patch("builtins.open", side_effect=FileNotFoundError), - patch.object(auth, "_login") as login, - ): - with pytest.raises(GetAccessTokenError) as exc: - auth.get_access_token() - - assert exc.value.status_code == 401 - assert "event loop" in str(exc.value) - login.assert_not_called() - - -def test_get_access_token_refuses_device_code_login_in_worker_thread() -> None: - auth: Final = Authenticator() - with ( - patch("builtins.open", side_effect=FileNotFoundError), - patch.object(auth, "_login") as login, - ): - with ThreadPoolExecutor(max_workers=1) as pool: - with pytest.raises(GetAccessTokenError) as exc: - pool.submit(auth.get_access_token).result() - - assert exc.value.status_code == 401 - assert "worker thread" in str(exc.value) - login.assert_not_called() - - -@pytest.mark.parametrize("encoding", ["utf-16", "utf-16-le", "utf-8-sig"]) -def test_get_access_token_reads_saved_token_with_supported_encoding( - logged_in_token_dir: tuple[Path, str], encoding: str -) -> None: - token_dir, token = logged_in_token_dir - (token_dir / "access-token").write_bytes(f"{token}\n".encode(encoding)) - - assert Authenticator().get_access_token() == token - - -def test_get_access_token_refuses_undecodable_saved_token( - logged_in_token_dir: tuple[Path, str], -) -> None: - token_dir, _ = logged_in_token_dir - access_token_file: Final = token_dir / "access-token" - access_token_file.write_bytes(TRUNCATED_UTF_16) - - with ThreadPoolExecutor(max_workers=1) as pool, pytest.raises(GetAccessTokenError) as exc: - pool.submit(Authenticator().get_access_token).result() - - assert exc.value.status_code == 401 - assert "worker thread" in str(exc.value) - assert access_token_file.read_bytes() == TRUNCATED_UTF_16 - - -def test_get_access_token_retries_device_flow_three_times( - logged_in_token_dir: tuple[Path, str], -) -> None: - token_dir, _ = logged_in_token_dir - (token_dir / "access-token").unlink() - auth: Final = Authenticator() - - with ( - patch.object( - auth, - "_login", - side_effect=GetDeviceCodeError(message="device flow failed", status_code=400), - ) as login, - pytest.raises(GetAccessTokenError, match="after 3 attempts"), - ): - auth.get_access_token() - - assert login.call_count == 3 - - -def test_get_access_token_returns_login_token_when_file_write_fails( - logged_in_token_dir: tuple[Path, str], -) -> None: - token_dir, _ = logged_in_token_dir - (token_dir / "access-token").unlink() - auth: Final = Authenticator() - - with ( - patch.object(auth, "_login", return_value="github-access-token"), - patch("builtins.open", side_effect=(FileNotFoundError, PermissionError)), - ): - assert auth.get_access_token() == "github-access-token" - - -@pytest.mark.parametrize( - ("environment", "url", "body"), - [ + @pytest.mark.parametrize( + "api_base", ( - {"GITHUB_COPILOT_DEVICE_CODE_URL": "https://custom.example/device"}, - "https://custom.example/device", - {"client_id": "Iv1.b507a08c87ecfe98", "scope": "read:user"}, + "https://configured.example.com", + "http://localhost:8080", + "http://127.0.0.1:8080", + "http://[::1]:8080", + "http://copilot-relay:8080", + "http://10.0.0.1:8080", ), + ) + def test_get_api_base_prefers_environment(self, api_base): + with patch.dict(os.environ, {"GITHUB_COPILOT_API_BASE": api_base}, clear=True): + assert get_copilot_api_base() == api_base + + @pytest.mark.parametrize( + "api_base", ( - {"GITHUB_COPILOT_CLIENT_ID": "custom-client"}, - DEVICE_CODE_URL, - {"client_id": "custom-client", "scope": "read:user"}, + "ftp://api.githubcopilot.com", + "https://user:password@api.githubcopilot.com", + "https://api.githubcopilot.com?tenant=example", + "https://api.githubcopilot.com#fragment", + "api.githubcopilot.com", + "https:///missing-host", + "https://[invalid", ), - ], -) -def test_get_device_code_uses_runtime_url_and_client_id( - environment: dict[str, str], url: str, body: dict[str, str] -) -> None: - client: Final = MagicMock() - response: Final = MagicMock() - response.json.return_value = { - "device_code": "dc", - "user_code": "UC", - "verification_uri": "https://github.com/login/device", - } - response.raise_for_status.return_value = None - client.post.return_value = response - with ( - patch.dict(os.environ, environment), - patch("litellm.llms.github_copilot.authenticator.get_httpx_client", return_value=client), - ): - assert Authenticator()._get_device_code()["device_code"] == "dc" - client.post.assert_called_once() - assert client.post.call_args.args[0] == url - assert client.post.call_args.kwargs["json"] == body - - -def test_get_device_code_sends_device_flow_request( - monkeypatch: pytest.MonkeyPatch, -) -> None: - monkeypatch.delenv("GITHUB_COPILOT_DEVICE_CODE_URL", raising=False) - monkeypatch.delenv("GITHUB_COPILOT_CLIENT_ID", raising=False) - client: Final = MagicMock() - response: Final = MagicMock() - device_code: Final = { - "device_code": "device-code", - "user_code": "ABCD-EFGH", - "verification_uri": "https://github.com/login/device", - } - response.json.return_value = device_code - response.raise_for_status.return_value = None - client.post.return_value = response - - with patch("litellm.llms.github_copilot.authenticator.get_httpx_client", return_value=client): - assert Authenticator()._get_device_code() == device_code - - client.post.assert_called_once_with( - DEVICE_CODE_URL, - headers=github_api_headers(), - json={"client_id": "Iv1.b507a08c87ecfe98", "scope": "read:user"}, ) + def test_get_api_base_rejects_invalid_configuration(self, api_base): + with ( + patch.dict(os.environ, {"GITHUB_COPILOT_API_BASE": api_base}, clear=True), + pytest.raises( + InvalidAPIBaseError, match="GitHub Copilot GITHUB_COPILOT_API_BASE must be an HTTP or HTTPS URL" + ), + ): + get_copilot_api_base() + def test_get_api_base_uses_default_when_unconfigured(self): + with patch.dict(os.environ, {}, clear=True): + assert get_copilot_api_base() == DEFAULT_GITHUB_COPILOT_API_BASE -def test_poll_for_access_token_sends_device_code_and_grant_type( - monkeypatch: pytest.MonkeyPatch, -) -> None: - monkeypatch.delenv("GITHUB_COPILOT_ACCESS_TOKEN_URL", raising=False) - monkeypatch.delenv("GITHUB_COPILOT_CLIENT_ID", raising=False) - client: Final = MagicMock() - response: Final = MagicMock() - response.json.return_value = {"access_token": "github-access-token"} - response.raise_for_status.return_value = None - client.post.return_value = response - - with patch("litellm.llms.github_copilot.authenticator.get_httpx_client", return_value=client): - assert Authenticator()._poll_for_access_token("device-code") == "github-access-token" - - client.post.assert_called_once_with( - "https://github.com/login/oauth/access_token", - headers=github_api_headers(), - json={ - "client_id": "Iv1.b507a08c87ecfe98", - "device_code": "device-code", - "grant_type": "urn:ietf:params:oauth:grant-type:device_code", - }, + @pytest.mark.parametrize( + "api_base", ("https://deployment.example.com", "http://localhost:9000", "http://deployment.example.com") ) - - -def test_poll_for_access_token_uses_runtime_url_and_client_id() -> None: - client: Final = MagicMock() - response: Final = MagicMock() - response.json.return_value = {"access_token": "github-token"} - response.raise_for_status.return_value = None - client.post.return_value = response - with ( - patch.dict( + def test_get_api_base_prefers_deployment_endpoint(self, api_base): + with patch.dict( os.environ, - { - "GITHUB_COPILOT_ACCESS_TOKEN_URL": "https://custom.example/token", - "GITHUB_COPILOT_CLIENT_ID": "custom-client", - }, - ), - patch("litellm.llms.github_copilot.authenticator.get_httpx_client", return_value=client), - patch("time.sleep"), - ): - assert Authenticator()._poll_for_access_token("dc") == "github-token" - assert client.post.call_args.args[0] == "https://custom.example/token" - assert client.post.call_args.kwargs["json"]["client_id"] == "custom-client" + {"GITHUB_COPILOT_API_BASE": "https://configured.example.com"}, + clear=True, + ): + assert get_copilot_api_base(api_base) == api_base - -def test_login_requests_device_code_and_polls_for_access_token() -> None: - auth: Final = Authenticator() - device_code: Final = { - "device_code": "device-code", - "user_code": "ABCD-EFGH", - "verification_uri": "https://github.com/login/device", - } - - with ( - patch.object(auth, "_get_device_code", return_value=device_code) as get_device_code, - patch.object(auth, "_poll_for_access_token", return_value="github-access-token") as poll, - patch("builtins.print") as print_message, - ): - assert auth._login() == "github-access-token" - - get_device_code.assert_called_once() - poll.assert_called_once_with("device-code") - print_message.assert_called_once_with( - "Please visit https://github.com/login/device and enter code ABCD-EFGH to authenticate.", - flush=True, + @pytest.mark.parametrize( + "api_base", + [ + "ftp://deployment.example.com", + "https://user:pw@deployment.example.com", + "https://deployment.example.com?q=1", + "https://deployment.example.com#fragment", + "https:///missing-host", + "deployment.example.com", + ], ) + def test_get_api_base_rejects_invalid_deployment_endpoint_instead_of_falling_back(self, api_base): + with ( + patch.dict( + os.environ, + {"GITHUB_COPILOT_API_BASE": "https://configured.example.com"}, + clear=True, + ), + pytest.raises(InvalidAPIBaseError, match="GitHub Copilot api_base must be an HTTP or HTTPS URL"), + ): + get_copilot_api_base(api_base) + + @pytest.mark.parametrize("deployment_base", [None, ""]) + @pytest.mark.parametrize("environment_base", [None, ""]) + def test_get_api_base_skips_empty_candidates_without_warning(self, deployment_base, environment_base, caplog): + environment = {} if environment_base is None else {"GITHUB_COPILOT_API_BASE": environment_base} + with patch.dict(os.environ, environment, clear=True), caplog.at_level(logging.WARNING, logger="LiteLLM"): + assert get_copilot_api_base(deployment_base) == DEFAULT_GITHUB_COPILOT_API_BASE + assert caplog.text == "" + + def test_get_api_base_refuses_an_enterprise_token_exchange_url_without_a_copilot_base(self): + with ( + patch.dict( + os.environ, + {"GITHUB_COPILOT_API_KEY_URL": "https://api.octo.ghe.com/copilot_internal/v2/token"}, + clear=True, + ), + pytest.raises(InvalidAPIBaseError, match="Set GITHUB_COPILOT_API_BASE"), + ): + get_copilot_api_base() + + @pytest.mark.parametrize( + ("environment", "expected"), + ( + ( + {"GITHUB_COPILOT_API_KEY_URL": "https://api.github.com/copilot_internal/v2/token"}, + DEFAULT_GITHUB_COPILOT_API_BASE, + ), + ( + { + "GITHUB_COPILOT_API_KEY_URL": "https://api.octo.ghe.com/copilot_internal/v2/token", + "GITHUB_COPILOT_API_BASE": "https://copilot-api.octo.ghe.com", + }, + "https://copilot-api.octo.ghe.com", + ), + ), + ) + def test_get_api_base_accepts_a_token_exchange_url_that_needs_no_action(self, environment, expected): + with patch.dict(os.environ, environment, clear=True): + assert get_copilot_api_base() == expected + + def test_get_github_headers(self, authenticator): + with patch.dict(os.environ, {}, clear=True): + headers = authenticator._get_github_headers() + assert headers == { + "accept": "application/json", + "content-type": "application/json", + "copilot-integration-id": DEFAULT_COPILOT_INTEGRATION_ID, + "editor-version": DEFAULT_COPILOT_EDITOR_VERSION, + "editor-plugin-version": DEFAULT_COPILOT_EDITOR_PLUGIN_VERSION, + "user-agent": DEFAULT_COPILOT_USER_AGENT, + } + + def test_auth_requests_support_opencode_identity(self, authenticator, mock_http_client): + mock_client, mock_response = mock_http_client + mock_response.json.side_effect = ( + { + "device_code": "dc", + "user_code": "UC", + "verification_uri": "https://github.com/login/device", + }, + {"access_token": "opencode-oauth-token"}, + ) + environment = { + "GITHUB_COPILOT_CLIENT_ID": "Ov23li8tweQw6odWQebz", + "GITHUB_COPILOT_USER_AGENT": "opencode/1.18.7", + "GITHUB_COPILOT_INTEGRATION_ID": "", + "GITHUB_COPILOT_EDITOR_VERSION": "", + "GITHUB_COPILOT_EDITOR_PLUGIN_VERSION": "", + "GITHUB_COPILOT_API_VERSION": "2026-06-01", + "GITHUB_COPILOT_OPENAI_INTENT": "conversation-edits", + "GITHUB_COPILOT_USER_AGENT_LIBRARY_VERSION": "", + "GITHUB_COPILOT_API_BASE": "https://api.githubcopilot.com", + } + + with ( + patch.dict(os.environ, environment, clear=True), + patch( + "litellm.llms.github_copilot.authenticator.get_httpx_client", + return_value=mock_client, + ), + patch.object(authenticator, "get_access_token", return_value="opencode-oauth-token"), + ): + authenticator._get_device_code() + assert authenticator._poll_for_access_token("dc") == "opencode-oauth-token" + assert authenticator.get_api_key() == "opencode-oauth-token" + request_headers = get_copilot_default_headers("opencode-oauth-token") + + expected_auth_headers = { + "accept": "application/json", + "content-type": "application/json", + "user-agent": "opencode/1.18.7", + } + assert mock_client.post.call_args_list[0].kwargs == { + "headers": expected_auth_headers, + "json": { + "client_id": "Ov23li8tweQw6odWQebz", + "scope": "read:user", + }, + } + assert mock_client.post.call_args_list[1].kwargs == { + "headers": expected_auth_headers, + "json": { + "client_id": "Ov23li8tweQw6odWQebz", + "device_code": "dc", + "grant_type": "urn:ietf:params:oauth:grant-type:device_code", + }, + } + assert {header: value for header, value in request_headers.items() if header != "x-request-id"} == { + **expected_auth_headers, + "openai-intent": "conversation-edits", + "x-github-api-version": "2026-06-01", + "Authorization": "Bearer " + "opencode-oauth-token", + } + assert str(uuid.UUID(request_headers["x-request-id"])) == request_headers["x-request-id"] + mock_client.get.assert_not_called() + + def test_get_access_token_from_file(self, logged_in_token_dir: Path) -> None: + """Test retrieving an access token from a file.""" + assert Authenticator().get_access_token() == "gho_logged_in" + + def test_get_access_token_login(self, authenticator): + mock_token = "mock-access-token" + write_open = mock_open() + + with ( + patch.object(authenticator, "_login", return_value=mock_token) as mock_login, + patch("builtins.open", side_effect=(IOError, write_open.return_value)), + ): + token = authenticator.get_access_token() + + assert token == mock_token + mock_login.assert_called_once() + write_open().write.assert_called_once_with(mock_token) + + def test_get_access_token_survives_persistence_failure(self, authenticator, caplog): + mock_token = "mock-access-token" + + with ( + patch.object(authenticator, "_login", return_value=mock_token) as mock_login, + patch("builtins.open", side_effect=IOError), + caplog.at_level(logging.ERROR, logger="LiteLLM"), + ): + token = authenticator.get_access_token() + + assert token == mock_token + mock_login.assert_called_once() + assert "Error saving access token to file" in caplog.text + + @pytest.mark.asyncio + async def test_get_access_token_refuses_device_code_login_in_event_loop(self, authenticator): + with ( + patch("builtins.open", side_effect=FileNotFoundError), + patch.object(authenticator, "_login") as mock_login, + ): + with pytest.raises(GetAccessTokenError) as exc: + authenticator.get_access_token() + + assert exc.value.status_code == 401 + assert "event loop" in str(exc.value) + assert authenticator.access_token_file not in str(exc.value) + mock_login.assert_not_called() + + def test_get_access_token_refuses_device_code_login_in_worker_thread(self, authenticator): + with ( + patch("builtins.open", side_effect=FileNotFoundError), + patch.object(authenticator, "_login") as mock_login, + ): + with ThreadPoolExecutor(max_workers=1) as pool: + with pytest.raises(GetAccessTokenError) as exc: + pool.submit(authenticator.get_access_token).result() + + assert exc.value.status_code == 401 + assert "worker thread" in str(exc.value) + assert authenticator.access_token_file not in str(exc.value) + mock_login.assert_not_called() + + @pytest.mark.parametrize("encoding", ["utf-16", "utf-16-le", "utf-8-sig"]) + def test_saved_access_token_in_another_encoding_is_sent_to_copilot( + self, logged_in_token_dir: Path, encoding: str + ) -> None: + """An access-token file a Windows editor saved as UTF-16 or with a BOM is still the token Copilot gets.""" + (logged_in_token_dir / "access-token").write_bytes("gho_logged_in\n".encode(encoding)) + + with respx.mock: + assert Authenticator().get_api_key() == "gho_logged_in" + + def test_undecodable_access_token_is_refused_like_a_missing_one(self, logged_in_token_dir: Path) -> None: + """A proxy worker gets the login hint for an access-token file it cannot decode, and the file is kept.""" + access_token_file: Final = logged_in_token_dir / "access-token" + access_token_file.write_bytes(TRUNCATED_UTF_16) + + with ThreadPoolExecutor(max_workers=1) as pool, pytest.raises(GetAccessTokenError) as exc: + pool.submit(Authenticator().get_access_token).result() + + assert "worker thread" in str(exc.value) + assert access_token_file.read_bytes() == TRUNCATED_UTF_16 + + def test_get_access_token_failure(self, authenticator): + """Test that an exception is raised after multiple login failures.""" + with ( + patch.object( + authenticator, + "_login", + side_effect=GetDeviceCodeError(message="Test error", status_code=400), + ), + patch("builtins.open", side_effect=IOError), + ): + with pytest.raises(GetAccessTokenError): + authenticator.get_access_token() + assert authenticator._login.call_count == 3 + + def test_get_api_key_returns_github_token_without_http(self, logged_in_token_dir: Path) -> None: + with patch("litellm.llms.github_copilot.authenticator.get_httpx_client") as get_client: + assert Authenticator().get_api_key() == "gho_logged_in" + get_client.assert_not_called() + + @pytest.mark.parametrize( + "malformed", + [ + b"[]", + json.dumps({"token": "tid=abc", "expires_at": "soon"}).encode(), + TRUNCATED_UTF_16, + json.dumps({"token": "tid=abc", "expires_at": 4102444800, "endpoints": []}).encode(), + ], + ids=["json-list", "text-expires-at", "truncated-utf-16", "list-endpoints-beside-live-key"], + ) + def test_get_api_key_malformed_api_key_file_is_ignored_like_a_missing_one( + self, logged_in_token_dir: Path, malformed: bytes + ) -> None: + """A leftover api-key.json in any shape never replaces the access token and is left for inspection.""" + api_key_file: Final = logged_in_token_dir / "api-key.json" + api_key_file.write_bytes(malformed) + + with respx.mock: + assert Authenticator().get_api_key() == "gho_logged_in" + + assert api_key_file.read_bytes() == malformed + + def test_get_device_code(self, authenticator, mock_http_client): + """Test getting a device code.""" + mock_client, mock_response = mock_http_client + mock_device_code_data = { + "device_code": "mock-device-code", + "user_code": "ABCD-EFGH", + "verification_uri": "https://github.com/login/device", + } + + with ( + patch( + "litellm.llms.github_copilot.authenticator.get_httpx_client", + return_value=mock_client, + ), + patch.object(mock_response, "json", return_value=mock_device_code_data), + ): + result = authenticator._get_device_code() + assert result == mock_device_code_data + mock_client.post.assert_called_once() + + def test_poll_for_access_token(self, authenticator, mock_http_client): + """Test polling for an access token.""" + mock_client, mock_response = mock_http_client + mock_token_data = {"access_token": "mock-access-token"} + + with ( + patch( + "litellm.llms.github_copilot.authenticator.get_httpx_client", + return_value=mock_client, + ), + patch.object(mock_response, "json", return_value=mock_token_data), + patch("time.sleep"), + ): + result = authenticator._poll_for_access_token("mock-device-code") + assert result == "mock-access-token" + mock_client.post.assert_called_once() + + def test_login(self, authenticator): + """Test the login process.""" + mock_device_code_data = { + "device_code": "mock-device-code", + "user_code": "ABCD-EFGH", + "verification_uri": "https://github.com/login/device", + } + mock_token = "mock-access-token" + + with ( + patch.object(authenticator, "_get_device_code", return_value=mock_device_code_data), + patch.object(authenticator, "_poll_for_access_token", return_value=mock_token), + patch("builtins.print") as mock_print, + ): + result = authenticator._login() + assert result == mock_token + authenticator._get_device_code.assert_called_once() + authenticator._poll_for_access_token.assert_called_once_with("mock-device-code") + mock_print.assert_called_once() + + def test_get_device_code_with_custom_url(self, authenticator, mock_http_client): + """GITHUB_COPILOT_DEVICE_CODE_URL env var must be used by _get_device_code at call time.""" + mock_client, mock_response = mock_http_client + custom_url = "https://custom.example.com/device" + mock_response.json.return_value = { + "device_code": "dc", + "user_code": "UC", + "verification_uri": "https://example.com", + } + with ( + patch.dict(os.environ, {"GITHUB_COPILOT_DEVICE_CODE_URL": custom_url}), + patch("litellm.llms.github_copilot.authenticator.get_httpx_client", return_value=mock_client), + ): + assert authenticator._get_device_code()["device_code"] == "dc" + assert mock_client.post.call_args[0][0] == custom_url + + def test_get_device_code_with_custom_client_id(self, authenticator, mock_http_client): + """GITHUB_COPILOT_CLIENT_ID env var must appear as client_id in the device-code request body.""" + mock_client, mock_response = mock_http_client + custom_id = "custom_client_id" + mock_response.json.return_value = { + "device_code": "dc", + "user_code": "UC", + "verification_uri": "https://example.com", + } + with ( + patch.dict(os.environ, {"GITHUB_COPILOT_CLIENT_ID": custom_id}), + patch("litellm.llms.github_copilot.authenticator.get_httpx_client", return_value=mock_client), + ): + assert authenticator._get_device_code()["device_code"] == "dc" + assert mock_client.post.call_args[1]["json"]["client_id"] == custom_id + + def test_poll_for_access_token_with_custom_url(self, authenticator, mock_http_client): + """GITHUB_COPILOT_ACCESS_TOKEN_URL env var must be used by _poll_for_access_token at call time.""" + mock_client, mock_response = mock_http_client + custom_url = "https://custom.example.com/token" + mock_response.json.return_value = {"access_token": "tok"} + with ( + patch.dict(os.environ, {"GITHUB_COPILOT_ACCESS_TOKEN_URL": custom_url}), + patch("litellm.llms.github_copilot.authenticator.get_httpx_client", return_value=mock_client), + patch("time.sleep"), + ): + assert authenticator._poll_for_access_token("dc") == "tok" + assert mock_client.post.call_args[0][0] == custom_url + + def test_poll_for_access_token_with_custom_client_id(self, authenticator, mock_http_client): + """GITHUB_COPILOT_CLIENT_ID env var must appear as client_id in the polling request body.""" + mock_client, mock_response = mock_http_client + custom_id = "custom_client_id" + mock_response.json.return_value = {"access_token": "tok"} + with ( + patch.dict(os.environ, {"GITHUB_COPILOT_CLIENT_ID": custom_id}), + patch("litellm.llms.github_copilot.authenticator.get_httpx_client", return_value=mock_client), + patch("time.sleep"), + ): + assert authenticator._poll_for_access_token("dc") == "tok" + assert mock_client.post.call_args[1]["json"]["client_id"] == custom_id diff --git a/tests/unit/llms/github_copilot/test_github_copilot_transformation.py b/tests/unit/llms/github_copilot/test_github_copilot_transformation.py index d453d7c6622..514d358291a 100644 --- a/tests/unit/llms/github_copilot/test_github_copilot_transformation.py +++ b/tests/unit/llms/github_copilot/test_github_copilot_transformation.py @@ -11,11 +11,12 @@ from litellm import ModelResponse, completion from litellm.exceptions import AuthenticationError from litellm.llms.github_copilot.chat.transformation import GithubCopilotConfig from litellm.llms.github_copilot.common_utils import ( + DEFAULT_GITHUB_COPILOT_API_BASE, GetAccessTokenError, ) -def test_github_copilot_config_get_openai_compatible_provider_info(): +def test_github_copilot_config_get_openai_compatible_provider_info(monkeypatch: pytest.MonkeyPatch): """Test the GitHub Copilot configuration provider info retrieval.""" config = GithubCopilotConfig() @@ -24,8 +25,7 @@ def test_github_copilot_config_get_openai_compatible_provider_info(): mock_api_key = "gh.test-key-123456789" config.authenticator = MagicMock() config.authenticator.get_api_key.return_value = mock_api_key - # Test with dynamic endpoint - config.authenticator.get_api_base.return_value = "https://api.enterprise.githubcopilot.com" + monkeypatch.setenv("GITHUB_COPILOT_API_BASE", "https://api.enterprise.githubcopilot.com") # Test with default values model = "github_copilot/gpt-4" @@ -43,9 +43,15 @@ def test_github_copilot_config_get_openai_compatible_provider_info(): assert api_base == "https://api.enterprise.githubcopilot.com" assert dynamic_api_key == mock_api_key assert custom_llm_provider == "github_copilot" + api_base, _, _ = config._get_openai_compatible_provider_info( + model=model, + api_base="https://api.business.githubcopilot.com", + api_key=None, + custom_llm_provider="github_copilot", + ) + assert api_base == "https://api.business.githubcopilot.com" - # Test fallback to default if no dynamic endpoint - config.authenticator.get_api_base.return_value = None + monkeypatch.delenv("GITHUB_COPILOT_API_BASE") ( api_base, dynamic_api_key, @@ -56,7 +62,7 @@ def test_github_copilot_config_get_openai_compatible_provider_info(): api_key=None, custom_llm_provider="github_copilot", ) - assert api_base == "https://api.githubcopilot.com" + assert api_base == DEFAULT_GITHUB_COPILOT_API_BASE # Test with authentication failure config.authenticator.get_api_key.side_effect = GetAccessTokenError( @@ -79,10 +85,12 @@ def test_github_copilot_config_get_openai_compatible_provider_info(): @patch("litellm.main.openai_chat_completions.completion") @patch("litellm.llms.openai.openai.OpenAIChatCompletion.completion") def test_completion_github_copilot_mock_response( - mock_class_completion, mock_instance_completion, mock_get_api_key, monkeypatch + mock_class_completion, mock_instance_completion, mock_get_api_key, monkeypatch, tmp_path ): """Test the completion function with GitHub Copilot provider.""" + monkeypatch.setenv("GITHUB_COPILOT_TOKEN_DIR", str(tmp_path)) + # Force chat path through the patched openai_chat_completions instance even if # a previous test left EXPERIMENTAL_OPENAI_BASE_LLM_HTTP_HANDLER set in the env. monkeypatch.delenv("EXPERIMENTAL_OPENAI_BASE_LLM_HTTP_HANDLER", raising=False) @@ -170,7 +178,6 @@ def test_x_initiator_header_user_request(): # Mock the authenticator config.authenticator = MagicMock() config.authenticator.get_api_key.return_value = "gh.test-key-123" - config.authenticator.get_api_base.return_value = None messages = [ {"role": "system", "content": "You are an assistant."}, @@ -197,7 +204,6 @@ def test_x_initiator_header_agent_request_with_assistant(): # Mock the authenticator config.authenticator = MagicMock() config.authenticator.get_api_key.return_value = "gh.test-key-123" - config.authenticator.get_api_base.return_value = None messages = [ {"role": "system", "content": "You are an assistant."}, @@ -224,7 +230,6 @@ def test_x_initiator_header_agent_request_with_tool(): # Mock the authenticator config.authenticator = MagicMock() config.authenticator.get_api_key.return_value = "gh.test-key-123" - config.authenticator.get_api_base.return_value = None messages = [ {"role": "system", "content": "You are an assistant."}, @@ -251,7 +256,6 @@ def test_x_initiator_header_mixed_messages_with_agent_roles(): # Mock the authenticator config.authenticator = MagicMock() config.authenticator.get_api_key.return_value = "gh.test-key-123" - config.authenticator.get_api_base.return_value = None messages = [ {"role": "user", "content": "Hello"}, @@ -279,7 +283,6 @@ def test_x_initiator_header_user_only_messages(): # Mock the authenticator config.authenticator = MagicMock() config.authenticator.get_api_key.return_value = "gh.test-key-123" - config.authenticator.get_api_base.return_value = None messages = [ {"role": "system", "content": "You are an assistant."}, @@ -307,7 +310,6 @@ def test_x_initiator_header_empty_messages(): # Mock the authenticator config.authenticator = MagicMock() config.authenticator.get_api_key.return_value = "gh.test-key-123" - config.authenticator.get_api_base.return_value = None messages = [] @@ -331,7 +333,6 @@ def test_x_initiator_header_system_only_messages(): # Mock the authenticator config.authenticator = MagicMock() config.authenticator.get_api_key.return_value = "gh.test-key-123" - config.authenticator.get_api_base.return_value = None messages = [ {"role": "system", "content": "You are an assistant."}, @@ -357,7 +358,6 @@ def test_copilot_vision_request_header_with_image(): # Mock the authenticator config.authenticator = MagicMock() config.authenticator.get_api_key.return_value = "gh.test-key-123" - config.authenticator.get_api_base.return_value = None messages = [ { @@ -393,7 +393,6 @@ def test_copilot_vision_request_header_text_only(): # Mock the authenticator config.authenticator = MagicMock() config.authenticator.get_api_key.return_value = "gh.test-key-123" - config.authenticator.get_api_base.return_value = None messages = [ {"role": "user", "content": "Just a text message"}, @@ -420,7 +419,6 @@ def test_copilot_vision_request_header_with_type_image_url(): # Mock the authenticator config.authenticator = MagicMock() config.authenticator.get_api_key.return_value = "gh.test-key-123" - config.authenticator.get_api_base.return_value = None messages = [ { @@ -894,20 +892,22 @@ def test_openai_handler_repairs_github_copilot_empty_choices(mock_request, mock_ def test_validate_environment_uses_per_user_session_and_skips_authenticator(): from litellm.llms.github_copilot.per_user_auth import GithubCopilotUserSession + from litellm.llms.github_copilot.github_host import github_host_endpoints config = GithubCopilotConfig() config.authenticator = MagicMock() config.authenticator.get_api_key.side_effect = AssertionError("shared authenticator must not run") - session = GithubCopilotUserSession(token="user-copilot-token", api_base="https://api.githubcopilot.com") + session = GithubCopilotUserSession(token="user-github-oauth-token", endpoints=github_host_endpoints("github.com")) headers = config.validate_environment( - headers={}, + headers={"Authorization": "Bearer caller-token"}, model="github_copilot/gpt-4o", messages=[], optional_params={}, litellm_params={"github_copilot_user_session": session}, + api_key="caller-api-key", ) - assert headers["Authorization"] == "Bearer user-copilot-token" + assert headers["Authorization"] == f"Bearer {session.token}" config.authenticator.get_api_key.assert_not_called() diff --git a/tests/unit/llms/github_copilot/test_github_host.py b/tests/unit/llms/github_copilot/test_github_host.py index cee02dc072f..7d6389c90cc 100644 --- a/tests/unit/llms/github_copilot/test_github_host.py +++ b/tests/unit/llms/github_copilot/test_github_host.py @@ -32,12 +32,14 @@ def test_normalized_github_host_accepts_only_bare_supported_hosts(raw: object, e def test_github_com_endpoints_read_url_overrides_at_call_time(monkeypatch: pytest.MonkeyPatch) -> None: monkeypatch.setenv("GITHUB_COPILOT_DEVICE_CODE_URL", "https://device.example/code") monkeypatch.setenv("GITHUB_COPILOT_ACCESS_TOKEN_URL", "https://device.example/token") + monkeypatch.setenv("GITHUB_COPILOT_API_BASE", "https://copilot.example") monkeypatch.setenv("GITHUB_COPILOT_CLIENT_ID", "custom-client") endpoints: Final = github_host_endpoints("github.com") assert endpoints.device_code_url == "https://device.example/code" assert endpoints.access_token_url == "https://device.example/token" + assert endpoints.configured_copilot_api_base == "https://copilot.example" assert endpoints.copilot_user_url == "https://api.github.com/copilot_internal/user" assert endpoints.client_id == "custom-client" @@ -45,6 +47,7 @@ def test_github_com_endpoints_read_url_overrides_at_call_time(monkeypatch: pytes def test_ghe_endpoints_ignore_github_com_url_overrides(monkeypatch: pytest.MonkeyPatch) -> None: monkeypatch.setenv("GITHUB_COPILOT_DEVICE_CODE_URL", "https://device.example/code") monkeypatch.setenv("GITHUB_COPILOT_ACCESS_TOKEN_URL", "https://device.example/token") + monkeypatch.setenv("GITHUB_COPILOT_API_BASE", "https://copilot.example") monkeypatch.setenv("GITHUB_COPILOT_CLIENT_ID", "custom-client") endpoints: Final = github_host_endpoints("octo.ghe.com") @@ -52,6 +55,7 @@ def test_ghe_endpoints_ignore_github_com_url_overrides(monkeypatch: pytest.Monke assert endpoints.device_code_url == "https://octo.ghe.com/login/device/code" assert endpoints.access_token_url == "https://octo.ghe.com/login/oauth/access_token" assert endpoints.copilot_user_url == "https://api.octo.ghe.com/copilot_internal/user" + assert endpoints.configured_copilot_api_base is None assert endpoints.user_url == "https://api.octo.ghe.com/user" assert endpoints.default_copilot_api_base == "https://copilot-api.octo.ghe.com" assert endpoints.client_id == "custom-client" diff --git a/tests/unit/llms/github_copilot/test_per_user_auth.py b/tests/unit/llms/github_copilot/test_per_user_auth.py index 1c3f32aa5f4..78c55ffc891 100644 --- a/tests/unit/llms/github_copilot/test_per_user_auth.py +++ b/tests/unit/llms/github_copilot/test_per_user_auth.py @@ -5,6 +5,7 @@ import datetime import time from collections.abc import Callable from contextlib import contextmanager +from pathlib import Path from typing import Final from unittest.mock import AsyncMock, MagicMock, patch @@ -12,7 +13,7 @@ import httpx import pytest import litellm -from litellm.constants import GITHUB_COPILOT_DEFAULT_HOST, GITHUB_COPILOT_USER_SESSION_TTL_SECONDS +from litellm.constants import GITHUB_COPILOT_DEFAULT_HOST from litellm.exceptions import ( APIConnectionError, AuthenticationError, @@ -22,30 +23,27 @@ from litellm.exceptions import ( ServiceUnavailableError, ) from litellm.llms.custom_httpx.http_handler import AsyncHTTPHandler, HTTPHandler +from litellm.llms.github_copilot.common_utils import InvalidAPIBaseError from litellm.llms.github_copilot.github_host import github_host_endpoints from litellm.llms.github_copilot.per_user_auth import ( - _SESSION_CACHE, GITHUB_COPILOT_USER_SESSION_KWARG_KEY, GithubCopilotUserSession, - _session_cache_key, - aattach_github_copilot_user_session, acheck_copilot_seat, + afetch_github_login, apoll_device_flow, - aresolve_copilot_user_session, astart_device_flow, attach_github_copilot_user_session, - evict_copilot_user_session, github_copilot_auth_mode, github_copilot_user_session_from, - resolve_copilot_user_session, - validated_copilot_api_base, ) from litellm.types.utils import CredentialItem, ModelResponse -COPILOT_USER_URL = "https://api.github.com/copilot_internal/user" DEVICE_CODE_URL = "https://github.com/login/device/code" ACCESS_TOKEN_URL = "https://github.com/login/oauth/access_token" + + _GITHUB_ENDPOINTS: Final = github_host_endpoints("github.com") +_GHE_ENDPOINTS: Final = github_host_endpoints("octo.ghe.com") def _credential( @@ -63,20 +61,33 @@ def _credential( ) -def _user_payload(api_base="https://api.githubcopilot.com"): - return { - "endpoints": {"api": api_base}, - } +COPILOT_USER_URL: Final = "https://api.github.com/copilot_internal/user" +DEFAULT_COPILOT_HOST: Final = "https://api.githubcopilot.com" +PLAN_COPILOT_HOST: Final = "https://api.business.githubcopilot.com" + + +def answer_plan(endpoints_api: object, status_code: int = 200) -> Callable[[httpx.Request], httpx.Response]: + return lambda request: httpx.Response(status_code, json={"endpoints": {"api": endpoints_api}}, request=request) @contextmanager -def github_http(respond: Callable[[httpx.Request], httpx.Response]): +def github_http( + respond: Callable[[httpx.Request], httpx.Response], + plan_lookup: Callable[[httpx.Request], httpx.Response] | None = None, + plan_lookups: list[httpx.Request] | None = None, +): """Swap both module-level HTTP clients for httpx MockTransport versions and collect every request, so tests assert on the real HTTP edge (URL, headers) - while no socket is ever opened.""" + while no socket is ever opened. GitHub's Copilot plan lookup is answered by + plan_lookup (the default host when unset) and collected into plan_lookups instead.""" requests: list[httpx.Request] = [] + answer_plan_lookup = plan_lookup or answer_plan(DEFAULT_COPILOT_HOST) def capture(request: httpx.Request) -> httpx.Response: + if request.url.path == "/copilot_internal/user": + if plan_lookups is not None: + plan_lookups.append(request) + return answer_plan_lookup(request) requests.append(request) return respond(request) @@ -87,26 +98,11 @@ def github_http(respond: Callable[[httpx.Request], httpx.Response]): patch("litellm.llms.custom_httpx.llm_http_handler.get_async_httpx_client", return_value=async_client), patch("litellm.llms.custom_httpx.llm_http_handler.get_httpx_client", return_value=sync_client), patch.object(litellm, "module_level_client", sync_client), + patch.object(litellm, "client_session", sync_client.client), ): yield requests -def _user_responder(payload=None, status=200): - def respond(request: httpx.Request) -> httpx.Response: - if str(request.url) == COPILOT_USER_URL: - return httpx.Response(status, json=payload or _user_payload(), request=request) - pytest.fail(f"unexpected request to {request.url}") - - return respond - - -@pytest.fixture(autouse=True) -def _clear_session_cache(): - _SESSION_CACHE.flush_cache() - yield - _SESSION_CACHE.flush_cache() - - @pytest.fixture def per_user_credential(): with patch.object(litellm, "credential_list", [_credential()]): @@ -138,83 +134,8 @@ def test_unknown_named_credential_falls_back_to_kwargs(): assert github_copilot_auth_mode("missing-cred", None) is False -@pytest.mark.parametrize( - "raw,expected", - [ - ("https://api.githubcopilot.com", "https://api.githubcopilot.com"), - ("https://api.githubcopilot.com/", "https://api.githubcopilot.com"), - ("https://tenant.githubcopilot.com:443", "https://tenant.githubcopilot.com:443"), - ("githubcopilot.com", "https://api.githubcopilot.com"), # no scheme -> default - ("http://api.githubcopilot.com", "https://api.githubcopilot.com"), # http rejected - ("https://evil.com", "https://api.githubcopilot.com"), - ("https://githubcopilot.com.evil.com", "https://api.githubcopilot.com"), - ("https://user:pw@api.githubcopilot.com", "https://api.githubcopilot.com"), - ("https://api.githubcopilot.com:8080", "https://api.githubcopilot.com"), - (None, "https://api.githubcopilot.com"), - (1234, "https://api.githubcopilot.com"), - ("https://API.GITHUBCOPILOT.COM", "https://API.GITHUBCOPILOT.COM"), - ], -) -def test_validated_copilot_api_base(raw, expected): - assert validated_copilot_api_base(raw, github_host_endpoints("github.com")) == expected - - -@pytest.mark.parametrize( - "raw", - [ - "https://copilot-api.evil.ghe.com", - "https://api.githubcopilot.com", - "http://copilot-api.octo.ghe.com", - "https://user@copilot-api.octo.ghe.com", - "https://copilot-api.octo.ghe.com:8443", - ], -) -def test_ghe_user_response_must_stay_on_its_tenant_host(raw: object) -> None: - endpoints: Final = github_host_endpoints("octo.ghe.com") - assert validated_copilot_api_base(raw, endpoints) == "https://copilot-api.octo.ghe.com" - - -def test_github_com_user_response_rejects_a_ghe_copilot_host() -> None: - endpoints: Final = github_host_endpoints("github.com") - assert validated_copilot_api_base("https://copilot-api.octo.ghe.com", endpoints) == "https://api.githubcopilot.com" - - -def test_ghe_user_lookup_uses_stored_host_not_request_kwargs(): - credential: Final = CredentialItem( - credential_name="copilot-cred", - credential_values={"github_copilot_auth_type": "per_user_oauth", "github_copilot_host": "octo.ghe.com"}, - credential_info={}, - ) - - def respond(request: httpx.Request) -> httpx.Response: - assert str(request.url) == "https://api.octo.ghe.com/copilot_internal/user" - return httpx.Response(200, json=_user_payload(api_base="https://copilot-api.octo.ghe.com"), request=request) - - with patch.object(litellm, "credential_list", [credential]), github_http(respond) as requests: - kwargs: Final = _kwargs_with_connection(github_host="octo.ghe.com") - kwargs["github_copilot_host"] = "evil.ghe.com" - attach_github_copilot_user_session(kwargs) - - session: Final = github_copilot_user_session_from(kwargs) - assert session is not None and session.api_base == "https://copilot-api.octo.ghe.com" - assert len(requests) == 1 - - -def test_invalid_stored_host_fails_before_user_lookup(): - credential: Final = CredentialItem( - credential_name="copilot-cred", - credential_values={"github_copilot_auth_type": "per_user_oauth", "github_copilot_host": "https://octo.ghe.com"}, - credential_info={}, - ) - with patch.object(litellm, "credential_list", [credential]): - with github_http(lambda request: pytest.fail(f"unexpected request to {request.url}")) as requests: - with pytest.raises(BadRequestError): - attach_github_copilot_user_session(_kwargs_with_connection()) - assert not requests - - def test_session_repr_and_str_hide_the_token(caplog): - session = GithubCopilotUserSession(token="secret-copilot-token", api_base="https://api.githubcopilot.com") + session = GithubCopilotUserSession(token="secret-copilot-token", endpoints=_GITHUB_ENDPOINTS) assert "secret-copilot-token" not in repr(session) assert "secret-copilot-token" not in str(session) with caplog.at_level("DEBUG"): @@ -224,242 +145,6 @@ def test_session_repr_and_str_hide_the_token(caplog): assert "secret-copilot-token" not in caplog.text -def test_user_lookup_returns_github_token_and_caches_per_user(): - api_base: Final = "https://tenant.githubcopilot.com" - with github_http(_user_responder(payload=_user_payload(api_base=api_base))) as requests: - s1 = resolve_copilot_user_session("user-a", "gh-token", "copilot-cred", _GITHUB_ENDPOINTS) - assert len(requests) == 1 - assert str(requests[0].url) == COPILOT_USER_URL - assert requests[0].headers["Authorization"] == "token gh-token" - s2 = resolve_copilot_user_session("user-a", "gh-token", "copilot-cred", _GITHUB_ENDPOINTS) - assert len(requests) == 1 - assert s2.token == s1.token == "gh-token" - assert s1.api_base == api_base - resolve_copilot_user_session("user-b", "gh-token", "copilot-cred", _GITHUB_ENDPOINTS) - assert len(requests) == 2 - resolve_copilot_user_session("user-a", "gh-token-2", "copilot-cred", _GITHUB_ENDPOINTS) - assert len(requests) == 3 - - -def test_cached_session_is_not_reused_after_github_token_changes(): - with github_http(_user_responder()) as requests: - resolve_copilot_user_session("user-a", "gh-token-a", "copilot-cred", _GITHUB_ENDPOINTS) - resolve_copilot_user_session("user-a", "gh-token-b", "copilot-cred", _GITHUB_ENDPOINTS) - - assert [request.headers["Authorization"] for request in requests] == ["token gh-token-a", "token gh-token-b"] - - -@pytest.mark.parametrize("status", [401, 403, 404]) -def test_user_lookup_auth_failures_raise_caller_auth_error_and_evict(status): - with github_http(_user_responder(status=status)): - with pytest.raises(CallerCredentialAuthenticationError) as exc: - resolve_copilot_user_session("user-a", "gh-token", "copilot-cred", _GITHUB_ENDPOINTS) - assert "Reconnect GitHub Copilot for credential 'copilot-cred' in the LiteLLM UI (LLM Credentials)" in str( - exc.value - ) - assert ( - _SESSION_CACHE.get_cache(_session_cache_key("user-a", "copilot-cred", GITHUB_COPILOT_DEFAULT_HOST)) is None - ) - - -def test_user_lookup_429_raises_caller_rate_limit(): - with github_http(_user_responder(status=429)): - with pytest.raises(CallerCredentialRateLimitError): - resolve_copilot_user_session("user-a", "gh-token", "copilot-cred", _GITHUB_ENDPOINTS) - - -def test_user_lookup_other_failure_raises_service_unavailable_without_body(): - with github_http(_user_responder(status=500, payload={"secret": "body"})): - with pytest.raises(ServiceUnavailableError) as exc: - resolve_copilot_user_session("user-a", "gh-token", "copilot-cred", _GITHUB_ENDPOINTS) - assert "body" not in str(exc.value) - - -def test_session_cache_uses_fixed_ttl(): - with ( - patch("litellm.llms.github_copilot.per_user_auth.time.time", return_value=1000) as clock, - github_http(_user_responder()) as requests, - ): - resolve_copilot_user_session("user-a", "gh-token", "copilot-cred", _GITHUB_ENDPOINTS) - clock.return_value = 1000 + GITHUB_COPILOT_USER_SESSION_TTL_SECONDS - 1 - resolve_copilot_user_session("user-a", "gh-token", "copilot-cred", _GITHUB_ENDPOINTS) - assert len(requests) == 1 - clock.return_value = 1000 + GITHUB_COPILOT_USER_SESSION_TTL_SECONDS + 1 - resolve_copilot_user_session("user-a", "gh-token", "copilot-cred", _GITHUB_ENDPOINTS) - assert len(requests) == 2 - - -@pytest.mark.asyncio -async def test_acheck_copilot_seat_rejects_models_403(per_user_credential): - def respond(request: httpx.Request) -> httpx.Response: - if str(request.url) == COPILOT_USER_URL: - return httpx.Response(200, json=_user_payload(), request=request) - if str(request.url) == "https://api.githubcopilot.com/models": - return httpx.Response(403, request=request) - pytest.fail(f"unexpected request to {request.url}") - - with github_http(respond) as requests: - with pytest.raises(CallerCredentialAuthenticationError): - await acheck_copilot_seat("user-a", "gh-token", "copilot-cred", _GITHUB_ENDPOINTS) - - assert [str(request.url) for request in requests] == [ - COPILOT_USER_URL, - "https://api.githubcopilot.com/models", - ] - assert requests[1].headers["Authorization"] == "Bearer gh-token" - - -@pytest.mark.asyncio -@pytest.mark.parametrize( - ("status", "error_type"), - [(429, CallerCredentialRateLimitError), (500, ServiceUnavailableError)], -) -async def test_acheck_copilot_seat_maps_models_failures( - per_user_credential, - status: int, - error_type: type[Exception], -) -> None: - def respond(request: httpx.Request) -> httpx.Response: - if str(request.url) == COPILOT_USER_URL: - return httpx.Response(200, json=_user_payload(), request=request) - if str(request.url) == "https://api.githubcopilot.com/models": - return httpx.Response(status, request=request) - pytest.fail(f"unexpected request to {request.url}") - - with github_http(respond): - with pytest.raises(error_type): - await acheck_copilot_seat("user-a", "gh-token", "copilot-cred", _GITHUB_ENDPOINTS) - - -@pytest.mark.asyncio -async def test_acheck_copilot_seat_maps_models_request_errors(per_user_credential) -> None: - def respond(request: httpx.Request) -> httpx.Response: - if str(request.url) == COPILOT_USER_URL: - return httpx.Response(200, json=_user_payload(), request=request) - if str(request.url) == "https://api.githubcopilot.com/models": - raise httpx.ConnectError("connection failed", request=request) - pytest.fail(f"unexpected request to {request.url}") - - with github_http(respond): - with pytest.raises(APIConnectionError, match="models check request failed"): - await acheck_copilot_seat("user-a", "gh-token", "copilot-cred", _GITHUB_ENDPOINTS) - - -@pytest.mark.asyncio -async def test_acheck_copilot_seat_passes_when_models_are_usable(per_user_credential): - def respond(request: httpx.Request) -> httpx.Response: - if str(request.url) == COPILOT_USER_URL: - return httpx.Response(200, json=_user_payload(), request=request) - if str(request.url) == "https://api.githubcopilot.com/models": - return httpx.Response(200, json={"data": []}, request=request) - pytest.fail(f"unexpected request to {request.url}") - - with github_http(respond) as requests: - session = await acheck_copilot_seat("user-a", "gh-token", "copilot-cred", _GITHUB_ENDPOINTS) - - assert session.token == "gh-token" - assert session.api_base == "https://api.githubcopilot.com" - assert [str(request.url) for request in requests] == [ - COPILOT_USER_URL, - "https://api.githubcopilot.com/models", - ] - - -@pytest.mark.asyncio -async def test_aresolve_single_flight_one_http_call(): - def respond(request: httpx.Request) -> httpx.Response: - return httpx.Response(200, json=_user_payload(), request=request) - - with github_http(respond) as requests: - s1, s2 = await asyncio.gather( - aresolve_copilot_user_session("user-a", "gh-token", "copilot-cred", _GITHUB_ENDPOINTS), - aresolve_copilot_user_session("user-a", "gh-token", "copilot-cred", _GITHUB_ENDPOINTS), - ) - assert len(requests) == 1 - assert s1.token == s2.token == "gh-token" - - -@pytest.mark.asyncio -async def test_aresolve_401_raises_and_evicts(): - with github_http(_user_responder(status=401)): - with pytest.raises(CallerCredentialAuthenticationError): - await aresolve_copilot_user_session("user-a", "gh-token", "copilot-cred", _GITHUB_ENDPOINTS) - assert ( - _SESSION_CACHE.get_cache(_session_cache_key("user-a", "copilot-cred", GITHUB_COPILOT_DEFAULT_HOST)) is None - ) - - -ACCOUNT_URL = "https://api.github.com/copilot_internal/user" -EXCHANGE_URL: Final = "https://api.github.com/copilot_internal/v2/token" -_CUSTOM_CLIENT_ID: Final = "Ov23liCustomOrgApp" -_ACCOUNT_API_BASE: Final = "https://api.business.githubcopilot.com" - - -def _account_payload(access_type_sku="copilot_enterprise_seat", chat_enabled=True, api_base=_ACCOUNT_API_BASE): - return {"access_type_sku": access_type_sku, "chat_enabled": chat_enabled, "endpoints": {"api": api_base}} - - -def _account_responder(payload=None, status=200, model_payload=None): - def respond(request: httpx.Request) -> httpx.Response: - if str(request.url) == ACCOUNT_URL: - return httpx.Response(status, json=payload or _account_payload(), request=request) - if model_payload is not None and request.url.host.endswith("githubcopilot.com"): - return httpx.Response(200, json=model_payload, request=request) - pytest.fail(f"unexpected request to {request.url}") - - return respond - - -@pytest.fixture(params=[None, _CUSTOM_CLIENT_ID], ids=["default-client-id", "custom-client-id"]) -def copilot_client_id(request, monkeypatch: pytest.MonkeyPatch): - if request.param is None: - monkeypatch.delenv("GITHUB_COPILOT_CLIENT_ID", raising=False) - return - monkeypatch.setenv("GITHUB_COPILOT_CLIENT_ID", request.param) - - -@pytest.mark.asyncio -async def test_user_lookup_uses_the_github_token_for_any_client_id(copilot_client_id): - endpoints: Final = github_host_endpoints("github.com") - with github_http(_account_responder()) as requests: - first = await aresolve_copilot_user_session("user-a", "gho_org_app", "copilot-cred", endpoints) - second = await aresolve_copilot_user_session("user-a", "gho_org_app", "copilot-cred", endpoints) - - assert [str(r.url) for r in requests] == [ACCOUNT_URL] - assert requests[0].headers["Authorization"] == "token gho_org_app" - assert first.token == second.token == "gho_org_app" - assert first.api_base == _ACCOUNT_API_BASE - - -@pytest.mark.parametrize( - "payload", - [ - _account_payload(access_type_sku="no_access", chat_enabled=False), - _account_payload(chat_enabled=False), - _account_payload(access_type_sku="no_access"), - ], -) -def test_user_lookup_without_a_copilot_seat_raises_reconnect(copilot_client_id, payload): - with github_http(_account_responder(payload)): - with pytest.raises(CallerCredentialAuthenticationError): - resolve_copilot_user_session("user-a", "gho_org_app", "copilot-cred", github_host_endpoints("github.com")) - - -@pytest.mark.parametrize("status", [401, 403, 404]) -def test_rejected_user_lookup_raises_reconnect(copilot_client_id, status): - with github_http(_account_responder(status=status)): - with pytest.raises(CallerCredentialAuthenticationError): - resolve_copilot_user_session("user-a", "gho_org_app", "copilot-cred", github_host_endpoints("github.com")) - - -def test_user_lookup_api_base_must_stay_on_copilot_hosts(copilot_client_id): - with github_http(_account_responder(_account_payload(api_base="https://evil.example"))): - session = resolve_copilot_user_session( - "user-a", "gho_org_app", "copilot-cred", github_host_endpoints("github.com") - ) - assert session.api_base == "https://api.githubcopilot.com" - - def _kwargs_with_connection( token: str = "gho_user_token", github_host: str | None = "github.com", @@ -478,21 +163,114 @@ def _kwargs_with_connection( } -@pytest.mark.asyncio -async def test_aattach_injects_session_into_kwargs(per_user_credential): - with github_http(_user_responder()): - kwargs = _kwargs_with_connection() - await aattach_github_copilot_user_session(kwargs) - session = github_copilot_user_session_from(kwargs) - assert session is not None - assert session.token == "gho_user_token" - - -def test_attach_sync_injects_session(per_user_credential): - with github_http(_user_responder()): - kwargs = _kwargs_with_connection() +def test_attach_uses_stored_github_token_without_network(per_user_credential): + kwargs = _kwargs_with_connection() + with github_http(lambda request: pytest.fail(f"unexpected request to {request.url}")) as requests: attach_github_copilot_user_session(kwargs) - assert isinstance(kwargs[GITHUB_COPILOT_USER_SESSION_KWARG_KEY], GithubCopilotUserSession) + assert github_copilot_user_session_from(kwargs).token == "gho_user_token" + assert requests == [] + + +@pytest.mark.parametrize("header_location", ["headers", "extra_headers", "optional_params", "litellm_params"]) +def test_attach_strips_authorization_without_mutating_caller_headers(per_user_credential, header_location): + headers = {"Authorization": "Bearer caller", "authorization": "Bearer other", "x-custom": "keep"} + kwargs = { + **_kwargs_with_connection(), + header_location: {"extra_headers": headers} + if header_location in ("optional_params", "litellm_params") + else headers, + } + attach_github_copilot_user_session(kwargs) + actual = kwargs[header_location] + if header_location in ("optional_params", "litellm_params"): + actual = actual["extra_headers"] + assert actual == {"x-custom": "keep"} + assert headers == {"Authorization": "Bearer caller", "authorization": "Bearer other", "x-custom": "keep"} + + +@pytest.mark.asyncio +@pytest.mark.parametrize("api_base", ["http://localhost:8080/", "https://configured.example.com/"]) +async def test_seat_check_sends_stored_token_to_configured_models_endpoint(monkeypatch, api_base): + monkeypatch.setenv("GITHUB_COPILOT_API_BASE", api_base) + monkeypatch.setenv("GITHUB_COPILOT_USER_AGENT", "test-agent") + with github_http(lambda request: httpx.Response(204, request=request)) as requests: + await acheck_copilot_seat("stored-github-token", "copilot-cred", github_host_endpoints("github.com")) + assert len(requests) == 1 + assert str(requests[0].url) == f"{api_base.rstrip('/')}/models" + assert requests[0].headers["authorization"] == "Bearer stored-github-token" + assert requests[0].headers["user-agent"] == "test-agent" + + +@pytest.mark.asyncio +@pytest.mark.parametrize( + "status,error_type,message", + [ + (401, CallerCredentialAuthenticationError, "GitHub rejected the GitHub Copilot connection"), + (403, CallerCredentialAuthenticationError, "GitHub rejected the GitHub Copilot connection"), + (404, CallerCredentialAuthenticationError, "GitHub rejected the GitHub Copilot connection"), + (429, CallerCredentialRateLimitError, "rate limited the seat check"), + (302, ServiceUnavailableError, "seat check failed with status 302"), + (500, ServiceUnavailableError, "seat check failed with status 500"), + ], +) +async def test_seat_check_maps_upstream_failures(status, error_type, message): + with github_http(lambda request: httpx.Response(status, json={"secret": "private-body"}, request=request)): + with pytest.raises(error_type, match=message) as exc: + await acheck_copilot_seat("stored-github-token", "copilot-cred", _GITHUB_ENDPOINTS) + assert "private-body" not in str(exc.value) + assert "stored-github-token" not in str(exc.value) + if error_type is CallerCredentialAuthenticationError: + assert "Reconnect GitHub Copilot for credential 'copilot-cred'" in str(exc.value) + + +@pytest.mark.asyncio +async def test_seat_check_transport_failure_raises_connection_error(): + def respond(request): + raise httpx.ReadError("private transport detail", request=request) + + with github_http(respond): + with pytest.raises(APIConnectionError, match="seat check request failed") as exc: + await acheck_copilot_seat("stored-github-token", "copilot-cred", _GITHUB_ENDPOINTS) + assert "private transport detail" not in str(exc.value) + + +@pytest.mark.asyncio +@pytest.mark.parametrize( + "env_name,env_value,message", + [ + ( + "GITHUB_COPILOT_API_KEY_URL", + "https://api.octo.ghe.com/copilot_internal/v2/token", + "Set GITHUB_COPILOT_API_BASE", + ), + ("GITHUB_COPILOT_API_BASE", "ftp://copilot.example.com", "must be an HTTP or HTTPS URL"), + ], +) +async def test_seat_check_surfaces_the_copilot_api_base_configuration_error(monkeypatch, env_name, env_value, message): + monkeypatch.setenv(env_name, env_value) + with ( + github_http( + lambda request: pytest.fail(f"unexpected request to {request.url}"), answer_plan(PLAN_COPILOT_HOST) + ), + pytest.raises(InvalidAPIBaseError, match=message), + ): + await acheck_copilot_seat("stored-github-token", "copilot-cred", github_host_endpoints("github.com")) + + +@pytest.mark.asyncio +async def test_github_login_uses_configured_auth_headers(monkeypatch): + monkeypatch.setenv("GITHUB_COPILOT_USER_AGENT", "test-agent") + monkeypatch.setenv("GITHUB_COPILOT_INTEGRATION_ID", "") + with github_http( + lambda request: httpx.Response(200, json={"login": "connected-user"}, request=request) + ) as requests: + login = await afetch_github_login("stored-github-token", _GITHUB_ENDPOINTS) + assert login == "connected-user" + assert len(requests) == 1 + assert str(requests[0].url) == "https://api.github.com/user" + assert requests[0].headers["authorization"] == "token stored-github-token" + assert requests[0].headers["user-agent"] == "test-agent" + assert "copilot-integration-id" not in requests[0].headers def test_attach_per_user_without_connection_raises_connect_401(per_user_credential): @@ -506,13 +284,10 @@ def test_attach_per_user_without_connection_raises_connect_401(per_user_credenti ("credential_host", "connection_host"), [("github.com", "octo.ghe.com"), ("octo.ghe.com", "github.com")], ) -def test_attach_rejects_connection_from_a_different_github_host( - credential_host: str, - connection_host: str, -): +def test_attach_rejects_connection_from_a_different_github_host(credential_host: str, connection_host: str): with patch.object(litellm, "credential_list", [_credential(github_host=credential_host)]): kwargs: Final = _kwargs_with_connection(github_host=connection_host) - with github_http(lambda request: pytest.fail(f"unexpected user lookup at {request.url}")) as requests: + with github_http(lambda request: pytest.fail(f"unexpected request to {request.url}")) as requests: with pytest.raises(CallerCredentialAuthenticationError) as exc: attach_github_copilot_user_session(kwargs) assert "Connect GitHub Copilot for credential 'copilot-cred'" in str(exc.value) @@ -520,11 +295,30 @@ def test_attach_rejects_connection_from_a_different_github_host( def test_legacy_connection_without_host_uses_github_com(per_user_credential): - with github_http(_user_responder()) as requests: - kwargs: Final = _kwargs_with_connection(github_host=None) + kwargs: Final = _kwargs_with_connection(github_host=None) + with github_http(lambda request: pytest.fail(f"unexpected request to {request.url}")) as requests: attach_github_copilot_user_session(kwargs) - assert len(requests) == 1 - assert str(requests[0].url) == COPILOT_USER_URL + session: Final = github_copilot_user_session_from(kwargs) + assert session is not None and session.endpoints.host == GITHUB_COPILOT_DEFAULT_HOST + assert not requests + + +def test_ghe_session_uses_stored_host_not_request_kwargs(): + with patch.object(litellm, "credential_list", [_credential(github_host="octo.ghe.com")]): + kwargs: Final = {**_kwargs_with_connection(github_host="octo.ghe.com"), "github_copilot_host": "evil.ghe.com"} + attach_github_copilot_user_session(kwargs) + session: Final = github_copilot_user_session_from(kwargs) + assert session is not None and session.endpoints.host == "octo.ghe.com" + + +def test_invalid_stored_host_fails_before_any_request(): + with ( + patch.object(litellm, "credential_list", [_credential(github_host="https://octo.ghe.com")]), + github_http(lambda request: pytest.fail(f"unexpected request to {request.url}")) as requests, + pytest.raises(BadRequestError), + ): + attach_github_copilot_user_session(_kwargs_with_connection()) + assert not requests def test_attach_shared_mode_does_nothing_without_connection(): @@ -534,14 +328,6 @@ def test_attach_shared_mode_does_nothing_without_connection(): assert GITHUB_COPILOT_USER_SESSION_KWARG_KEY not in kwargs -def test_evict_drops_cached_session(): - with github_http(_user_responder()) as requests: - resolve_copilot_user_session("user-a", "gh-token", "copilot-cred", _GITHUB_ENDPOINTS) - evict_copilot_user_session("user-a", "copilot-cred", _GITHUB_ENDPOINTS.host) - resolve_copilot_user_session("user-a", "gh-token", "copilot-cred", _GITHUB_ENDPOINTS) - assert len(requests) == 2 - - @pytest.mark.asyncio async def test_astart_device_flow_shape(): def respond(request: httpx.Request) -> httpx.Response: @@ -603,7 +389,7 @@ async def test_apoll_device_flow_connected(): def test_is_github_copilot_per_user_request(): from litellm.llms.github_copilot.per_user_auth import is_github_copilot_per_user_request - session = GithubCopilotUserSession(token="t", api_base="https://api.githubcopilot.com") + session = GithubCopilotUserSession(token="t", endpoints=_GITHUB_ENDPOINTS) assert is_github_copilot_per_user_request({GITHUB_COPILOT_USER_SESSION_KWARG_KEY: session}) is True assert is_github_copilot_per_user_request({}) is False assert is_github_copilot_per_user_request({GITHUB_COPILOT_USER_SESSION_KWARG_KEY: "not-a-session"}) is False @@ -626,13 +412,13 @@ def _chat_completion_response(): ) -def _secret_fields(user_id: str, github_token: str): +def _secret_fields(user_id: str, github_token: str, github_host: str = GITHUB_COPILOT_DEFAULT_HOST): return { "user_provider_credentials_user_id": user_id, "user_provider_credentials": { "copilot-cred": { "access_token": github_token, - "github_host": GITHUB_COPILOT_DEFAULT_HOST, + "github_host": github_host, } }, } @@ -647,7 +433,7 @@ def test_two_per_user_calls_with_identical_bodies_both_reach_upstream( # test-q monkeypatch.setattr(litellm, "cache", Cache()) with ( - github_http(_user_responder()), + github_http(lambda request: pytest.fail(f"unexpected request to {request.url}")), patch( "litellm.main.openai_chat_completions.completion", return_value=_chat_completion_response(), @@ -672,7 +458,7 @@ async def test_two_per_user_async_calls_with_identical_bodies_both_reach_upstrea monkeypatch.setattr(litellm, "cache", Cache()) with ( - github_http(_user_responder()), + github_http(lambda request: pytest.fail(f"unexpected request to {request.url}")), patch( "litellm.main.openai_chat_completions.acompletion", new=AsyncMock(return_value=_chat_completion_response()), @@ -700,7 +486,7 @@ def test_shared_mode_second_identical_call_hits_response_cache( # test-quality- monkeypatch.setattr(litellm, "cache", Cache()) with ( - github_http(_user_responder()), + github_http(lambda request: pytest.fail(f"unexpected request to {request.url}")), patch( "litellm.main.openai_chat_completions.completion", return_value=_chat_completion_response(), @@ -759,9 +545,12 @@ def _anthropic_messages_payload(): def _copilot_surface_responder(model_payload): def respond(request: httpx.Request) -> httpx.Response: - if str(request.url) == COPILOT_USER_URL: - return httpx.Response(200, json=_user_payload(), request=request) - if request.url.host.endswith("githubcopilot.com"): + if request.url.host in ( + "api.githubcopilot.com", + "api.business.githubcopilot.com", + "configured.example.com", + "deployment.example.com", + ): return httpx.Response(200, json=model_payload, request=request) pytest.fail(f"unexpected request to {request.url}") @@ -776,19 +565,174 @@ def _no_authenticator(): ) -def _assert_user_lookup_then_model_call(requests, github_token, model_host="https://api.githubcopilot.com"): - lookup = [r for r in requests if str(r.url) == COPILOT_USER_URL] - upstream = [r for r in requests if r.url.host.endswith("githubcopilot.com")] - assert lookup, "expected the GitHub user lookup request" - assert upstream, "expected the model request to the Copilot host" - assert lookup[0].headers["Authorization"] == f"token {github_token}" - assert upstream[0].headers["Authorization"] == f"Bearer {github_token}" - assert upstream[0].url.host == httpx.URL(model_host).host +def _assert_direct_model_call(requests, github_token, model_host="https://api.githubcopilot.com"): + assert len(requests) == 1 + assert requests[0].headers["Authorization"] == f"Bearer {github_token}" + assert requests[0].url.host == httpx.URL(model_host).host + + +_SURFACE_MODELS: Final = { + "completion": "github_copilot/gpt-4o", + "embedding": "github_copilot/text-embedding-3-small", + "responses": "github_copilot/gpt-5.3-codex", +} +_SURFACE_PATHS: Final = {"completion": "chat/completions", "embedding": "embeddings", "responses": "responses"} + + +def _surface_payload(surface): + return { + "completion": _chat_completion_response().model_dump, + "embedding": _embedding_response_payload, + "responses": _responses_payload, + }[surface]() + + +def _call_surface(target, surface, **kwargs): + if surface == "completion": + return target.completion(messages=[{"role": "user", "content": "hi"}], **kwargs) + if surface == "embedding": + return target.embedding(input=["hi"], **kwargs) + return target.responses(input="hi", **kwargs) + + +@pytest.mark.parametrize("surface", ["completion", "embedding", "responses"]) +@pytest.mark.parametrize("deployment_base", [None, "https://deployment.example.com/v1"]) +def test_per_user_request_goes_to_the_configured_base_never_the_deployment_base( + per_user_credential, monkeypatch, surface, deployment_base +): + monkeypatch.setenv("GITHUB_COPILOT_API_BASE", "https://configured.example.com/v1") + lookups: list[httpx.Request] = [] + with ( + github_http( + _copilot_surface_responder(_surface_payload(surface)), answer_plan(PLAN_COPILOT_HOST), lookups + ) as requests, + _no_authenticator(), + ): + _call_surface( + litellm, + surface, + model=_SURFACE_MODELS[surface], + litellm_credential_name="copilot-cred", + secret_fields=_secret_fields("user-a", "stored-github-token"), + extra_headers=dict(_EVIL_AUTH_HEADERS), + api_base=deployment_base, + ) + assert [str(request.url) for request in requests] == [f"https://configured.example.com/v1/{_SURFACE_PATHS[surface]}"] + assert requests[0].headers["authorization"] == "Bearer stored-github-token" + assert lookups == [] + + +def _copilot_router(surface, mode, default_litellm_params=None, **deployment_params): + return litellm.Router( + default_litellm_params=default_litellm_params, + model_list=[ + { + "model_name": "copilot", + "litellm_params": { + "model": _SURFACE_MODELS[surface], + "api_base": "https://deployment.example.com/v1", + **({"litellm_credential_name": "copilot-cred"} if mode == "per_user" else {}), + **deployment_params, + }, + } + ] + ) + + +def _call_with_caller_endpoint(router, surface, mode): + return _call_surface( + router, + surface, + model="copilot", + api_base="https://caller.example.com/v1", + base_url="https://caller.example.com/v1", + **({"secret_fields": _secret_fields("user-a", "stored-github-token")} if mode == "per_user" else {}), + ) + + +@pytest.mark.parametrize("surface", ["completion", "embedding", "responses"]) +@pytest.mark.parametrize("mode", ["shared", "per_user"]) +def test_caller_endpoint_on_a_copilot_deployment_answers_400(per_user_credential, tmp_path, monkeypatch, surface, mode): + _shared_login(tmp_path, monkeypatch) + router = _copilot_router(surface, mode) + with ( + github_http(_copilot_surface_responder(_surface_payload(surface))) as requests, + pytest.raises(BadRequestError, match="do not accept api_base or base_url from the caller"), + ): + _call_with_caller_endpoint(router, surface, mode) + assert requests == [] + + +@pytest.mark.parametrize("surface", ["completion", "embedding", "responses"]) +@pytest.mark.parametrize("mode", ["shared", "per_user"]) +@pytest.mark.parametrize( + "router_settings", + [{"drop_params": True}, {"default_litellm_params": {"drop_params": True}}], + ids=["deployment_drop_params", "router_default_drop_params"], +) +def test_caller_endpoint_on_a_copilot_deployment_is_dropped_under_drop_params( + per_user_credential, tmp_path, monkeypatch, surface, mode, router_settings +): + _shared_login(tmp_path, monkeypatch) + router = _copilot_router(surface, mode, **router_settings) + with github_http(_copilot_surface_responder(_surface_payload(surface)), answer_plan(PLAN_COPILOT_HOST)) as requests: + _call_with_caller_endpoint(router, surface, mode) + expected_base = PLAN_COPILOT_HOST if mode == "per_user" else "https://deployment.example.com/v1" + assert [str(request.url) for request in requests] == [f"{expected_base}/{_SURFACE_PATHS[surface]}"] + expected_token = "stored-github-token" if mode == "per_user" else "shared-copilot-token" + assert requests[0].headers["authorization"] == f"Bearer {expected_token}" + + +def _unauthorized(request: httpx.Request) -> httpx.Response: + return httpx.Response(401, json={"error": {"message": "Unauthorized"}}, request=request) + + +_RECONNECT_MESSAGE: Final = "Reconnect GitHub Copilot for credential 'copilot-cred'" + + +@pytest.mark.parametrize("surface", ["completion", "embedding", "responses"]) +def test_per_user_token_github_rejects_asks_that_caller_to_reconnect(per_user_credential, surface): + with ( + github_http(_unauthorized), + _no_authenticator(), + pytest.raises(CallerCredentialAuthenticationError, match=_RECONNECT_MESSAGE), + ): + _call_surface( + litellm, + surface, + model=_SURFACE_MODELS[surface], + litellm_credential_name="copilot-cred", + secret_fields=_secret_fields("user-a", "revoked-github-token"), + ) + + +@pytest.mark.asyncio +async def test_per_user_token_github_rejects_on_messages_asks_that_caller_to_reconnect(per_user_credential): + with ( + github_http(_unauthorized), + _no_authenticator(), + pytest.raises(CallerCredentialAuthenticationError, match=_RECONNECT_MESSAGE), + ): + await litellm.anthropic.messages.acreate( + model="github_copilot/claude-haiku-4.5", + max_tokens=16, + messages=[{"role": "user", "content": "hi"}], + litellm_credential_name="copilot-cred", + secret_fields=_secret_fields("user-a", "revoked-github-token"), + ) + + +@pytest.mark.parametrize("surface", ["completion", "embedding", "responses"]) +def test_shared_token_github_rejects_stays_a_deployment_authentication_error(tmp_path, monkeypatch, surface): + _shared_login(tmp_path, monkeypatch) + with github_http(_unauthorized), pytest.raises(AuthenticationError) as excinfo: + _call_surface(litellm, surface, model=_SURFACE_MODELS[surface]) + assert not isinstance(excinfo.value, CallerCredentialAuthenticationError) def test_e2e_completion_per_user_credential(per_user_credential): with ( - github_http(_user_responder()) as requests, + github_http(lambda request: pytest.fail(f"unexpected request to {request.url}")) as requests, _no_authenticator(), patch( "litellm.main.openai_chat_completions.completion", @@ -801,8 +745,7 @@ def test_e2e_completion_per_user_credential(per_user_credential): litellm_credential_name="copilot-cred", secret_fields=_secret_fields("user-a", "gho_a"), ) - assert len(requests) == 1 and str(requests[0].url) == COPILOT_USER_URL - assert requests[0].headers["Authorization"] == "token gho_a" + assert requests == [] call_kwargs = mock_completion.call_args.kwargs assert call_kwargs["api_key"] == "gho_a" assert call_kwargs["api_base"] == "https://api.githubcopilot.com" @@ -815,7 +758,7 @@ def test_e2e_completion_per_user_session_token_wins_over_caller_authorization(pe """The session token reaches the wire even when the caller passes their own Authorization in extra_headers (chat dispatches through _complete_custom_openai).""" with ( - github_http(_user_responder()), + github_http(lambda request: pytest.fail(f"unexpected request to {request.url}")), _no_authenticator(), patch( "litellm.main.openai_chat_completions.completion", @@ -837,10 +780,9 @@ def test_e2e_completion_per_user_session_token_wins_over_caller_authorization(pe _EVIL_AUTH_HEADERS = {"authorization": "Bearer caller-evil", "x-keep": "1"} -def _assert_session_token_on_the_wire(requests, github_token="gho_a"): - upstream = [r for r in requests if r.url.host.endswith("githubcopilot.com")] - assert upstream, "expected the model request to the Copilot host" - assert upstream[0].headers["authorization"] == f"Bearer {github_token}" +def _assert_session_token_on_the_wire(requests): + assert len(requests) == 1 + assert requests[0].headers["authorization"] == "Bearer gho_a" def test_e2e_completion_per_user_caller_authorization_cannot_override_session(per_user_credential): @@ -849,7 +791,7 @@ def test_e2e_completion_per_user_caller_authorization_cannot_override_session(pe handed to openai_chat_completions.completion.""" caller_headers: dict[str, str] = dict(_EVIL_AUTH_HEADERS) with ( - github_http(_user_responder()), + github_http(lambda request: pytest.fail(f"unexpected request to {request.url}")), _no_authenticator(), patch( "litellm.main.openai_chat_completions.completion", @@ -885,9 +827,13 @@ def test_e2e_embedding_per_user_caller_authorization_cannot_override_session(per _assert_session_token_on_the_wire(requests) -def test_e2e_per_user_sends_the_github_token_to_the_accounts_copilot_host(per_user_credential, copilot_client_id): +def test_e2e_custom_client_id_sends_the_github_token_to_the_accounts_copilot_host(per_user_credential, monkeypatch): + monkeypatch.setenv("GITHUB_COPILOT_CLIENT_ID", "Ov23liCustomOrgApp") + lookups: Final[list[httpx.Request]] = [] with ( - github_http(_account_responder(model_payload=_embedding_response_payload())) as requests, + github_http( + _copilot_surface_responder(_embedding_response_payload()), answer_plan(PLAN_COPILOT_HOST), lookups + ) as requests, _no_authenticator(), ): litellm.embedding( @@ -896,10 +842,8 @@ def test_e2e_per_user_sends_the_github_token_to_the_accounts_copilot_host(per_us litellm_credential_name="copilot-cred", secret_fields=_secret_fields("user-a", "gho_org_app"), ) - assert EXCHANGE_URL not in [str(r.url) for r in requests] - upstream = [r for r in requests if r.url.host.endswith("githubcopilot.com")] - assert upstream[0].url.host == httpx.URL(_ACCOUNT_API_BASE).host - assert upstream[0].headers["authorization"] == "Bearer gho_org_app" + assert [request.headers["authorization"] for request in lookups] == ["token gho_org_app"] + _assert_direct_model_call(requests, "gho_org_app", PLAN_COPILOT_HOST) def test_e2e_responses_per_user_caller_authorization_cannot_override_session(per_user_credential): @@ -947,7 +891,7 @@ def test_e2e_embedding_per_user_credential(per_user_credential): litellm_credential_name="copilot-cred", secret_fields=_secret_fields("user-a", "gho_a"), ) - _assert_user_lookup_then_model_call(requests, "gho_a") + _assert_direct_model_call(requests, "gho_a") assert requests[-1].url.path == "/embeddings" assert response.usage.prompt_tokens == 4 @@ -963,7 +907,7 @@ def test_e2e_responses_per_user_credential(per_user_credential): litellm_credential_name="copilot-cred", secret_fields=_secret_fields("user-a", "gho_a"), ) - _assert_user_lookup_then_model_call(requests, "gho_a") + _assert_direct_model_call(requests, "gho_a") assert requests[-1].url.path == "/responses" assert response.usage.total_tokens == 6 @@ -981,7 +925,7 @@ async def test_e2e_anthropic_messages_per_user_credential(per_user_credential): litellm_credential_name="copilot-cred", secret_fields=_secret_fields("user-a", "gho_a"), ) - _assert_user_lookup_then_model_call(requests, "gho_a") + _assert_direct_model_call(requests, "gho_a") assert requests[-1].url.path == "/v1/messages" assert response["usage"]["input_tokens"] == 4 @@ -1039,7 +983,7 @@ def _shared_login(tmp_path, monkeypatch): def test_e2e_completion_shared_mode_still_uses_authenticator(tmp_path, monkeypatch): _shared_login(tmp_path, monkeypatch) with ( - github_http(_user_responder()), + github_http(lambda request: pytest.fail(f"unexpected request to {request.url}")), patch( "litellm.main.openai_chat_completions.completion", return_value=_chat_completion_response(), @@ -1053,6 +997,32 @@ def test_e2e_completion_shared_mode_still_uses_authenticator(tmp_path, monkeypat assert response.usage.total_tokens > 0 +def test_completion_sends_each_copilot_client_header_once_with_its_configured_value(tmp_path, monkeypatch): + from openai import OpenAI + + _shared_login(tmp_path, monkeypatch) + monkeypatch.setenv("GITHUB_COPILOT_USER_AGENT", "opencode/1.18.7") + sent: Final[list[httpx.Request]] = [] + + def capture(request: httpx.Request) -> httpx.Response: + sent.append(request) + return httpx.Response(200, json=_chat_completion_response().model_dump(), request=request) + + sdk_client: Final = OpenAI(api_key="unused", http_client=httpx.Client(transport=httpx.MockTransport(capture))) + with github_http(lambda request: pytest.fail(f"unexpected request to {request.url}")): + litellm.completion( + model="github_copilot/gpt-4o", + messages=[{"role": "user", "content": "hi"}], + client=sdk_client, + ) + assert len(sent) == 1 + assert [sent[0].headers.get_list(name) for name in ("accept", "content-type", "user-agent")] == [ + ["application/json"], + ["application/json"], + ["opencode/1.18.7"], + ] + + def test_e2e_embedding_shared_mode_still_uses_authenticator(tmp_path, monkeypatch): _shared_login(tmp_path, monkeypatch) with github_http(_copilot_surface_responder(_embedding_response_payload())) as requests: @@ -1078,77 +1048,190 @@ def test_e2e_responses_shared_mode_still_uses_authenticator(tmp_path, monkeypatc @pytest.mark.asyncio -async def test_e2e_anthropic_messages_shared_mode_still_uses_authenticator(): +async def test_e2e_anthropic_messages_shared_mode_still_uses_authenticator(tmp_path, monkeypatch): from litellm.llms.github_copilot.authenticator import Authenticator with ( github_http(_copilot_surface_responder(_anthropic_messages_payload())) as requests, - patch.object(Authenticator, "get_api_key", return_value="shared-copilot-token"), - patch.object(Authenticator, "get_api_base", return_value="https://api.githubcopilot.com"), + patch.object( # test-quality-ok: patching the shared-login edge is the fixture under test + Authenticator, "get_api_key", return_value="shared-copilot-token" + ) as mock_api_key, ): response = await litellm.anthropic.messages.acreate( model="github_copilot/claude-haiku-4.5", max_tokens=16, messages=[{"role": "user", "content": "hi"}], ) + mock_api_key.assert_called() upstream = [r for r in requests if r.url.host.endswith("githubcopilot.com")] assert upstream and upstream[0].headers["Authorization"] == "Bearer shared-copilot-token" assert response["usage"]["input_tokens"] == 4 -def test_sync_resolve_single_flight_one_http_call(): - import threading +_MODE_TOKENS: Final = {"shared": "shared-copilot-token", "per_user": "gho_a"} - def slow_respond(request: httpx.Request) -> httpx.Response: - time.sleep(0.05) - return httpx.Response(200, json=_user_payload(), request=request) - sessions: list[GithubCopilotUserSession] = [] - with github_http(slow_respond) as requests: - threads = [ - threading.Thread( - target=lambda: sessions.append( - resolve_copilot_user_session("user-a", "gh-token", "copilot-cred", _GITHUB_ENDPOINTS) - ) - ) - for _ in range(6) +def _credential_kwargs_for(mode, tmp_path, monkeypatch): + if mode == "shared": + _shared_login(tmp_path, monkeypatch) + return {} + return {"litellm_credential_name": "copilot-cred", "secret_fields": _secret_fields("user-a", "gho_a")} + + +def _assert_plan_host_served_both_calls(requests, lookups, mode, path): + assert [str(request.url) for request in requests] == [f"{PLAN_COPILOT_HOST}/{path}"] * 2 + assert {request.headers["authorization"] for request in requests} == {f"Bearer {_MODE_TOKENS[mode]}"} + assert [request.headers["authorization"] for request in lookups] == [f"token {_MODE_TOKENS[mode]}"] + + +@pytest.mark.parametrize("surface", ["completion", "embedding", "responses"]) +@pytest.mark.parametrize("mode", ["shared", "per_user"]) +def test_requests_go_to_the_plan_host_github_reports_for_the_token( + per_user_credential, tmp_path, monkeypatch, mode, surface +): + credential_kwargs = _credential_kwargs_for(mode, tmp_path, monkeypatch) + lookups: list[httpx.Request] = [] + with github_http( + _copilot_surface_responder(_surface_payload(surface)), answer_plan(PLAN_COPILOT_HOST), lookups + ) as requests: + responses = [ + _call_surface(litellm, surface, model=_SURFACE_MODELS[surface], **credential_kwargs) for _ in range(2) ] - for t in threads: - t.start() - for t in threads: - t.join() - assert len(requests) == 1 - assert len(sessions) == 6 and all(s.token == "gh-token" for s in sessions) + _assert_plan_host_served_both_calls(requests, lookups, mode, _SURFACE_PATHS[surface]) + assert [response._hidden_params["api_base"] for response in responses] == [PLAN_COPILOT_HOST] * 2 -def test_two_users_same_github_token_do_not_share_session(): - with github_http(_user_responder()): - s1: Final = resolve_copilot_user_session("user-a", "gh-same", "copilot-cred", _GITHUB_ENDPOINTS) - s2: Final = resolve_copilot_user_session("user-b", "gh-same", "copilot-cred", _GITHUB_ENDPOINTS) - user_a_key: Final = _session_cache_key("user-a", "copilot-cred", _GITHUB_ENDPOINTS.host) - user_b_key: Final = _session_cache_key("user-b", "copilot-cred", _GITHUB_ENDPOINTS.host) - other_credential_key: Final = _session_cache_key("user-a", "another-credential", _GITHUB_ENDPOINTS.host) - other_host_key: Final = _session_cache_key("user-a", "copilot-cred", "octo.ghe.com") - assert user_a_key != user_b_key - assert user_a_key != other_credential_key - assert user_a_key != other_host_key - assert s1.token == s2.token == "gh-same" +def test_an_aliased_shared_model_goes_to_the_plan_host(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None: + _shared_login(tmp_path, monkeypatch) + monkeypatch.setattr(litellm, "model_alias_map", {"copilot": _SURFACE_MODELS["completion"]}) + lookups: Final[list[httpx.Request]] = [] + with github_http( + _copilot_surface_responder(_surface_payload("completion")), answer_plan(PLAN_COPILOT_HOST), lookups + ) as requests: + _call_surface(litellm, "completion", model="copilot") + assert [str(request.url) for request in requests] == [f"{PLAN_COPILOT_HOST}/chat/completions"] + assert [request.headers["authorization"] for request in lookups] == ["token shared-copilot-token"] -def test_same_user_and_token_do_not_share_sessions_across_hosts(): - from litellm.llms.github_copilot.github_host import github_host_endpoints +@pytest.mark.asyncio +@pytest.mark.parametrize("mode", ["shared", "per_user"]) +async def test_messages_go_to_the_plan_host_github_reports_for_the_token( + per_user_credential, tmp_path, monkeypatch, mode +): + credential_kwargs = _credential_kwargs_for(mode, tmp_path, monkeypatch) + lookups: list[httpx.Request] = [] + with github_http( + _copilot_surface_responder(_anthropic_messages_payload()), answer_plan(PLAN_COPILOT_HOST), lookups + ) as requests: + for _ in range(2): + await litellm.anthropic.messages.acreate( + model="github_copilot/claude-haiku-4.5", + max_tokens=16, + messages=[{"role": "user", "content": "hi"}], + **credential_kwargs, + ) + _assert_plan_host_served_both_calls(requests, lookups, mode, "v1/messages") - ghe_endpoints: Final = github_host_endpoints("octo.ghe.com") - def respond(request: httpx.Request) -> httpx.Response: - return httpx.Response(200, json=_user_payload(), request=request) +@pytest.mark.asyncio +@pytest.mark.parametrize("mode", ["shared", "per_user"]) +async def test_concurrent_requests_on_a_cold_cache_share_one_plan_lookup( + per_user_credential, tmp_path, monkeypatch, mode +): + credential_kwargs = _credential_kwargs_for(mode, tmp_path, monkeypatch) + lookups: list[httpx.Request] = [] - with github_http(respond) as requests: - github_session: Final = resolve_copilot_user_session("user-a", "gh-same", "copilot-cred", _GITHUB_ENDPOINTS) - ghe_session: Final = resolve_copilot_user_session("user-a", "gh-same", "copilot-cred", ghe_endpoints) - assert len(requests) == 2 - assert github_session.api_base == "https://api.githubcopilot.com" - assert ghe_session.api_base == "https://copilot-api.octo.ghe.com" + async def answer_plan_slowly(request: httpx.Request) -> httpx.Response: + await asyncio.sleep(0.05) + return answer_plan(PLAN_COPILOT_HOST)(request) + + with github_http(_copilot_surface_responder(_responses_payload()), answer_plan_slowly, lookups) as requests: + await asyncio.gather( + *(litellm.aresponses(model=_SURFACE_MODELS["responses"], input="hi", **credential_kwargs) for _ in range(5)) + ) + assert len(lookups) == 1 + assert [str(request.url) for request in requests] == [f"{PLAN_COPILOT_HOST}/responses"] * 5 + + +@pytest.mark.parametrize( + ("plan_lookup", "lookups_after_a_minute"), + [ + (lambda request: httpx.Response(200, text="not json", request=request), 2), + (answer_plan(PLAN_COPILOT_HOST, status_code=500), 2), + (answer_plan(PLAN_COPILOT_HOST), 1), + ], + ids=["malformed_body_is_retried", "server_error_is_retried", "plan_host_is_kept"], +) +def test_a_failed_plan_lookup_is_retried_a_minute_later(tmp_path, monkeypatch, plan_lookup, lookups_after_a_minute): + _shared_login(tmp_path, monkeypatch) + lookups: list[httpx.Request] = [] + started: Final = time.time() + with github_http(_copilot_surface_responder(_responses_payload()), plan_lookup, lookups): + litellm.responses(model="github_copilot/gpt-5.3-codex", input="hi") + monkeypatch.setattr(time, "time", lambda: started + 61) + litellm.responses(model="github_copilot/gpt-5.3-codex", input="hi") + assert len(lookups) == lookups_after_a_minute + + +def _refuse_connection(request: httpx.Request) -> httpx.Response: + raise httpx.ConnectError("connection refused", request=request) + + +@pytest.mark.parametrize( + "plan_lookup", + [ + answer_plan("https://api.example.com"), + answer_plan("https://api.githubcopilot.com.example.com"), + answer_plan("http://api.business.githubcopilot.com"), + answer_plan("https://api.business.githubcopilot.com:8443"), + answer_plan(None), + answer_plan(PLAN_COPILOT_HOST, status_code=500), + lambda request: httpx.Response(200, text="not json", request=request), + _refuse_connection, + ], + ids=[ + "other_host", + "lookalike_host", + "plain_http", + "custom_port", + "no_endpoint", + "server_error", + "malformed_body", + "transport_error", + ], +) +def test_requests_use_the_default_host_when_github_reports_no_usable_plan_host(tmp_path, monkeypatch, plan_lookup): + _shared_login(tmp_path, monkeypatch) + with github_http(_copilot_surface_responder(_responses_payload()), plan_lookup) as requests: + litellm.responses(model="github_copilot/gpt-5.3-codex", input="hi") + assert [str(request.url) for request in requests] == [f"{DEFAULT_COPILOT_HOST}/responses"] + + +@pytest.mark.parametrize("surface", ["completion", "embedding", "responses"]) +def test_enterprise_token_exchange_url_without_a_copilot_base_sends_nothing(tmp_path, monkeypatch, surface): + _shared_login(tmp_path, monkeypatch) + monkeypatch.setenv("GITHUB_COPILOT_API_KEY_URL", "https://api.octo.ghe.com/copilot_internal/v2/token") + lookups: list[httpx.Request] = [] + with ( + github_http( + _copilot_surface_responder(_surface_payload(surface)), answer_plan(PLAN_COPILOT_HOST), lookups + ) as requests, + pytest.raises(InvalidAPIBaseError, match="Set GITHUB_COPILOT_API_BASE"), + ): + _call_surface(litellm, surface, model=_SURFACE_MODELS[surface]) + assert requests == [] + assert lookups == [] + + +def test_provider_resolution_never_looks_up_the_plan_host(tmp_path, monkeypatch): + _shared_login(tmp_path, monkeypatch) + lookups: list[httpx.Request] = [] + with github_http( + lambda request: pytest.fail(f"unexpected request to {request.url}"), answer_plan(PLAN_COPILOT_HOST), lookups + ): + litellm.get_llm_provider("github_copilot/gpt-4o") + litellm.get_api_base(model="github_copilot/gpt-4o", optional_params={}) + assert lookups == [] def test_router_constructs_with_per_user_credential_deployment(per_user_credential): @@ -1172,7 +1255,32 @@ def test_router_constructs_with_per_user_credential_deployment(per_user_credenti assert any(d["model_name"] == "copilot" for d in deployments) -def test_provider_info_per_user_without_session_returns_default_base_and_no_key(per_user_credential): +@pytest.mark.parametrize( + ("env_name", "env_value"), + [ + ("GITHUB_COPILOT_API_KEY_URL", "https://api.octo.ghe.com/copilot_internal/v2/token"), + ("GITHUB_COPILOT_API_BASE", "copilot-api.example.com"), + ], +) +def test_router_keeps_a_ghe_per_user_deployment_whatever_the_github_com_env_says(monkeypatch, env_name, env_value): + monkeypatch.delenv("GITHUB_COPILOT_API_BASE", raising=False) + monkeypatch.setenv(env_name, env_value) + with patch.object(litellm, "credential_list", [_credential(github_host="octo.ghe.com")]): + router: Final = litellm.Router( + model_list=[ + { + "model_name": "copilot-ghe", + "litellm_params": {"model": "github_copilot/gpt-4.1", "litellm_credential_name": "copilot-cred"}, + } + ] + ) + assert [d["model_name"] for d in router.get_model_list() or []] == ["copilot-ghe"] + + +@pytest.mark.parametrize("deployment_base", [None, "https://deployment.example.com"]) +def test_provider_info_per_user_without_session_returns_the_copilot_base_and_no_key( + per_user_credential, deployment_base +): from litellm.llms.github_copilot.authenticator import Authenticator from litellm.types.router import LiteLLM_Params @@ -1182,7 +1290,7 @@ def test_provider_info_per_user_without_session_returns_default_base_and_no_key( ) with patch.object(Authenticator, "get_api_key", side_effect=AssertionError("shared login")) as get_key: _, _, dynamic_api_key, api_base = litellm.get_llm_provider( - model="github_copilot/gpt-4.1", litellm_params=params + model="github_copilot/gpt-4.1", api_base=deployment_base, litellm_params=params ) get_key.assert_not_called() assert api_base == "https://api.githubcopilot.com" @@ -1196,102 +1304,12 @@ def test_provider_info_shared_github_copilot_still_uses_authenticator(): params: Final = LiteLLM_Params(model="github_copilot/gpt-4.1") with ( patch.object(Authenticator, "get_api_key", return_value="shared-token") as get_key, - patch.object(Authenticator, "get_api_base", return_value="https://api.githubcopilot.com"), ): _, _, dynamic_api_key, _ = litellm.get_llm_provider(model="github_copilot/gpt-4.1", litellm_params=params) get_key.assert_called_once() assert dynamic_api_key == "shared-token" -@pytest.mark.asyncio -async def test_aresolve_cancelled_waiter_does_not_cancel_the_shared_session(per_user_credential): - """Two waiters share one in-flight resolution; cancelling one must not cancel the - underlying future the other is waiting on.""" - gate = asyncio.Event() - - class _GatedClient: - async def get(self, url, headers=None): - await gate.wait() - return httpx.Response(200, json=_user_payload(), request=httpx.Request("GET", url)) - - with patch( # test-quality-ok: the gate holds the HTTP edge open so two waiters overlap - "litellm.llms.custom_httpx.http_handler.get_async_httpx_client", return_value=_GatedClient() - ): - first = asyncio.ensure_future( - aresolve_copilot_user_session("user-a", "gh-shared", "copilot-cred", _GITHUB_ENDPOINTS) - ) - await asyncio.sleep(0) - second = asyncio.ensure_future( - aresolve_copilot_user_session("user-a", "gh-shared", "copilot-cred", _GITHUB_ENDPOINTS) - ) - await asyncio.sleep(0) - second.cancel() - gate.set() - session = await first - assert session.token == "gh-shared" and session.api_base == "https://api.githubcopilot.com" - with pytest.raises(asyncio.CancelledError): - await second - - -@pytest.mark.asyncio -async def test_aresolve_does_not_share_an_in_flight_session_after_token_changes(per_user_credential): - gate = asyncio.Event() - auth_headers: asyncio.Queue[str] = asyncio.Queue() - - class _GatedClient: - async def get(self, url, headers=None): - await auth_headers.put(headers["authorization"]) - await gate.wait() - return httpx.Response( - 200, - json=_user_payload(), - request=httpx.Request("GET", url), - ) - - with patch("litellm.llms.custom_httpx.http_handler.get_async_httpx_client", return_value=_GatedClient()): - first = asyncio.ensure_future( - aresolve_copilot_user_session("user-a", "gh-a", "copilot-cred", _GITHUB_ENDPOINTS) - ) - assert await asyncio.wait_for(auth_headers.get(), timeout=1) == "token gh-a" - second = asyncio.ensure_future( - aresolve_copilot_user_session("user-a", "gh-b", "copilot-cred", _GITHUB_ENDPOINTS) - ) - assert await asyncio.wait_for(auth_headers.get(), timeout=1) == "token gh-b" - gate.set() - sessions = await asyncio.gather(first, second) - - assert [session.token for session in sessions] == ["gh-a", "gh-b"] - - -@pytest.mark.asyncio -async def test_aresolve_failure_with_a_single_caller_emits_no_unretrieved_warning(per_user_credential): - """The leader removes the failed future before anyone awaits it; the stored - exception must be consumed so the loop does not log 'never retrieved'.""" - import litellm.llms.github_copilot.per_user_auth as pua - - captured: list[tuple[object, object]] = [] - loop = asyncio.get_running_loop() - previous_handler = loop.get_exception_handler() - loop.set_exception_handler(lambda _loop, context: captured.append((_loop, context))) - try: - with ( - github_http(_user_responder(status=401)), - pytest.raises(CallerCredentialAuthenticationError), - ): - await aresolve_copilot_user_session("user-a", "gh-fail", "copilot-cred", _GITHUB_ENDPOINTS) - _IN_FLIGHT_KEYS = list(pua._IN_FLIGHT) - import gc - - gc.collect() - await asyncio.sleep(0) - finally: - loop.set_exception_handler(previous_handler) - assert _IN_FLIGHT_KEYS == [] - assert not any( - "never retrieved" in str(context.get("message", "")) for _, context in captured if isinstance(context, dict) - ) - - @pytest.mark.asyncio async def test_per_user_streaming_call_writes_nothing_to_the_response_cache(per_user_credential, monkeypatch): """Per-user streamed completions must never be written to the shared response @@ -1310,7 +1328,7 @@ async def test_per_user_streaming_call_writes_nothing_to_the_response_cache(per_ "model": "github_copilot/gpt-4o", "messages": [{"role": "user", "content": "hi"}], GITHUB_COPILOT_USER_SESSION_KWARG_KEY: GithubCopilotUserSession( - token="gh-token", api_base="https://api.githubcopilot.com" + token="copilot-token", endpoints=_GITHUB_ENDPOINTS ), } await handler.async_get_cache( @@ -1404,7 +1422,93 @@ def test_shared_deployment_keeps_caller_credential_override(): with ( patch.object(litellm, "credential_list", [_credential(auth_type="shared")]), patch.object(Authenticator, "get_api_key", return_value="shared-token"), + github_http(lambda request: pytest.fail(f"unexpected request to {request.url}")), ): router: Final = _per_user_router() response: Final = _route_with_credential_name(router, "other-cred") assert response.choices[0].message.content == "ok" + + +GHE_COPILOT_USER_URL: Final = "https://api.octo.ghe.com/copilot_internal/user" +GHE_COPILOT_HOST: Final = "https://copilot-api.octo.ghe.com" + + +@pytest.mark.asyncio +async def test_ghe_seat_check_uses_the_tenant_plan_lookup_and_ignores_github_com_env(monkeypatch): + monkeypatch.setenv("GITHUB_COPILOT_API_BASE", "https://configured.example.com") + monkeypatch.setenv("GITHUB_COPILOT_API_KEY_URL", "https://api.other.ghe.com/copilot_internal/v2/token") + lookups: list[httpx.Request] = [] + with github_http( + lambda request: httpx.Response(204, request=request), answer_plan(GHE_COPILOT_HOST), lookups + ) as requests: + await acheck_copilot_seat("ghe-github-token", "copilot-cred", github_host_endpoints("octo.ghe.com")) + assert [str(request.url) for request in lookups] == [GHE_COPILOT_USER_URL] + assert lookups[0].headers["authorization"] == "token ghe-github-token" + assert [str(request.url) for request in requests] == [f"{GHE_COPILOT_HOST}/models"] + assert requests[0].headers["authorization"] == "Bearer ghe-github-token" + + +@pytest.mark.asyncio +@pytest.mark.parametrize( + "plan_host", + [ + "https://copilot-api.evil.ghe.com", + "https://api.githubcopilot.com", + "http://copilot-api.octo.ghe.com", + "https://user@copilot-api.octo.ghe.com", + "https://copilot-api.octo.ghe.com:8443", + ], +) +async def test_ghe_plan_host_outside_its_tenant_falls_back_to_the_tenant_default(plan_host): + with github_http(lambda request: httpx.Response(204, request=request), answer_plan(plan_host)) as requests: + await acheck_copilot_seat("ghe-github-token", "copilot-cred", _GHE_ENDPOINTS) + assert [str(request.url) for request in requests] == [f"{GHE_COPILOT_HOST}/models"] + + +@pytest.mark.asyncio +async def test_github_com_plan_host_on_a_ghe_tenant_falls_back_to_the_default(): + with github_http(lambda request: httpx.Response(204, request=request), answer_plan(GHE_COPILOT_HOST)) as requests: + await acheck_copilot_seat("stored-github-token", "copilot-cred", _GITHUB_ENDPOINTS) + assert [str(request.url) for request in requests] == [f"{DEFAULT_COPILOT_HOST}/models"] + + +@pytest.mark.asyncio +async def test_plan_host_cache_is_kept_per_github_host(): + lookups: list[httpx.Request] = [] + + def answer_per_host(request: httpx.Request) -> httpx.Response: + plan_host: Final = GHE_COPILOT_HOST if request.url.host == "api.octo.ghe.com" else PLAN_COPILOT_HOST + return answer_plan(plan_host)(request) + + with github_http(lambda request: httpx.Response(204, request=request), answer_per_host, lookups) as requests: + await acheck_copilot_seat("same-github-token", "copilot-cred", _GITHUB_ENDPOINTS) + await acheck_copilot_seat("same-github-token", "copilot-cred", _GHE_ENDPOINTS) + assert [str(request.url) for request in lookups] == [COPILOT_USER_URL, GHE_COPILOT_USER_URL] + assert [str(request.url) for request in requests] == [f"{PLAN_COPILOT_HOST}/models", f"{GHE_COPILOT_HOST}/models"] + + +def test_e2e_completion_per_user_ghe_credential_sends_the_token_to_its_tenant(monkeypatch): + monkeypatch.setenv("GITHUB_COPILOT_API_BASE", "https://configured.example.com") + payload: Final = _chat_completion_response().model_dump() + + def respond(request: httpx.Request) -> httpx.Response: + if request.url.host == "copilot-api.octo.ghe.com": + return httpx.Response(200, json=payload, request=request) + pytest.fail(f"unexpected request to {request.url}") + + lookups: list[httpx.Request] = [] + with ( + patch.object(litellm, "credential_list", [_credential(github_host="octo.ghe.com")]), + github_http(respond, answer_plan(GHE_COPILOT_HOST), lookups) as requests, + _no_authenticator(), + ): + response: Final = litellm.completion( + model="github_copilot/gpt-4o", + messages=[{"role": "user", "content": "hi"}], + litellm_credential_name="copilot-cred", + secret_fields=_secret_fields("user-a", "ghe-github-token", "octo.ghe.com"), + ) + assert [str(request.url) for request in lookups] == [GHE_COPILOT_USER_URL] + assert [str(request.url) for request in requests] == [f"{GHE_COPILOT_HOST}/chat/completions"] + assert requests[0].headers["authorization"] == "Bearer ghe-github-token" + assert response._hidden_params["api_base"] == GHE_COPILOT_HOST diff --git a/tests/unit/proxy/credential_endpoints/test_endpoints.py b/tests/unit/proxy/credential_endpoints/test_endpoints.py index 27fffc7e56e..20278e88339 100644 --- a/tests/unit/proxy/credential_endpoints/test_endpoints.py +++ b/tests/unit/proxy/credential_endpoints/test_endpoints.py @@ -5,7 +5,7 @@ from contextlib import contextmanager from functools import reduce from types import SimpleNamespace from typing import Final, Protocol -from unittest.mock import AsyncMock, MagicMock, call, patch +from unittest.mock import AsyncMock, MagicMock, patch import httpx import pytest @@ -18,7 +18,6 @@ from pydantic import TypeAdapter import litellm from litellm.litellm_core_utils.credential_accessor import CredentialAccessor from litellm.litellm_core_utils.litellm_logging import get_masked_values -from litellm.llms.github_copilot.per_user_auth import _SESSION_CACHE, _session_cache_key from litellm.models.credentials import CredentialSource, UserConnectionFlowHandle from litellm.proxy._types import LiteLLM_UserTable, LitellmUserRoles, UserAPIKeyAuth from litellm.proxy.auth.user_api_key_auth import user_api_key_auth @@ -1812,12 +1811,6 @@ def test_update_credential_purges_connections_when_github_host_changes(monkeypat redis_cache.async_get_cache = AsyncMock(side_effect=get_cache) monkeypatch.setattr(proxy_server, "user_api_key_cache", DualCache(redis_cache=redis_cache)) - session_key: Final = _session_cache_key("user-a", "copilot", "github.com") - _SESSION_CACHE.set_cache( - session_key, - ("gho_secret", "copilot-token", "https://api.githubcopilot.com", 4102444800), - ttl=3600, - ) response = _patch_credential( "copilot", { @@ -1828,15 +1821,9 @@ def test_update_credential_purges_connections_when_github_host_changes(monkeypat ) assert response.status_code == 200, response.text - table.find_many.assert_has_awaits( - [ - call(where={"credential_name": "copilot"}), - call(where={"credential_name": "copilot"}), - ] - ) + table.find_many.assert_awaited_once_with(where={"credential_name": "copilot"}) table.delete_many.assert_awaited_once_with(where={"credential_name": "copilot"}) assert cached_values[upc._cache_key("user-a", "copilot")] == upc._NOT_CONNECTED - assert _SESSION_CACHE.get_cache(session_key) is None def test_update_credential_does_not_purge_connections_for_case_only_host_change(restore_credential_list): @@ -1852,12 +1839,6 @@ def test_update_credential_does_not_purge_connections_for_case_only_host_change( proxy_server.prisma_client.db.litellm_userprovidercredentials = table proxy_server.prisma_client.writer_db = proxy_server.prisma_client.db - session_key: Final = _session_cache_key("user-a", "copilot", "octo.ghe.com") - _SESSION_CACHE.set_cache( - session_key, - ("gho_secret", "copilot-token", "https://copilot-api.octo.ghe.com", 4102444800), - ttl=3600, - ) response = _patch_credential( "copilot", { @@ -1870,8 +1851,6 @@ def test_update_credential_does_not_purge_connections_for_case_only_host_change( assert response.status_code == 200, response.text table.find_many.assert_not_awaited() table.delete_many.assert_not_awaited() - assert _SESSION_CACHE.get_cache(session_key) is not None - _SESSION_CACHE.delete_cache(session_key) def test_update_credential_does_not_purge_connections_for_legacy_host_case_difference(restore_credential_list): @@ -1887,12 +1866,6 @@ def test_update_credential_does_not_purge_connections_for_legacy_host_case_diffe proxy_server.prisma_client.db.litellm_userprovidercredentials = table proxy_server.prisma_client.writer_db = proxy_server.prisma_client.db - session_key: Final = _session_cache_key("user-a", "copilot", "octo.ghe.com") - _SESSION_CACHE.set_cache( - session_key, - ("gho_secret", "copilot-token", "https://copilot-api.octo.ghe.com", 4102444800), - ttl=3600, - ) response = _patch_credential( "copilot", { @@ -1905,8 +1878,6 @@ def test_update_credential_does_not_purge_connections_for_legacy_host_case_diffe assert response.status_code == 200, response.text table.find_many.assert_not_awaited() table.delete_many.assert_not_awaited() - assert _SESSION_CACHE.get_cache(session_key) is not None - _SESSION_CACHE.delete_cache(session_key) def test_update_credential_purges_connections_when_github_host_is_deleted(restore_credential_list): @@ -2065,15 +2036,15 @@ from contextlib import contextmanager as _ctx from litellm.caching.dual_cache import DualCache from litellm.llms.custom_httpx.http_handler import AsyncHTTPHandler +from litellm.llms.github_copilot.common_utils import DEFAULT_GITHUB_COPILOT_API_BASE from litellm.proxy.credential_endpoints import user_provider_credentials as upc _DEVICE_CODE_URL = "https://github.com/login/device/code" _ACCESS_TOKEN_URL = "https://github.com/login/oauth/access_token" -_COPILOT_USER_URL = "https://api.github.com/copilot_internal/user" -_COPILOT_API_BASE = "https://api.githubcopilot.com" -_COPILOT_MODELS_URL = f"{_COPILOT_API_BASE}/models" -_COPILOT_USER_RESPONSE = {"endpoints": {"api": _COPILOT_API_BASE}} +_COPILOT_MODELS_URL: Final = f"{DEFAULT_GITHUB_COPILOT_API_BASE}/models" _GITHUB_USER_URL = "https://api.github.com/user" +_COPILOT_USER_URL: Final = "https://api.github.com/copilot_internal/user" +_DEFAULT_PLAN: Final = {"endpoints": {"api": DEFAULT_GITHUB_COPILOT_API_BASE}} def _as_user(user_id="user-a"): @@ -2121,6 +2092,7 @@ def _connection_row( @pytest.fixture(autouse=True) def _connection_master_key(monkeypatch): monkeypatch.setattr("litellm.proxy.proxy_server.master_key", "sk-test-master") + monkeypatch.delenv("GITHUB_COPILOT_API_BASE", raising=False) @_ctx @@ -2140,6 +2112,15 @@ def _github_http(mapping): yield +def _copilot_models_response(github_token): + def respond(request): + assert request.method == "GET" + assert request.headers["Authorization"] == f"Bearer {github_token}" + return {"data": []} + + return respond + + _DEVICE_FLOW_START = { "device_code": "dc-1", "user_code": "UC-1", @@ -2242,14 +2223,30 @@ def test_user_connection_poll_rejects_an_undecryptable_flow_handle(monkeypatch): assert response.status_code == 400 -def test_user_connection_poll_connected_persists_and_reports_login(monkeypatch): - table: Final = _patch_user_connection_env(monkeypatch, [_per_user_credential()]) +@pytest.mark.parametrize( + ("api_base", "plan_lookup", "seat_check_base"), + [ + (None, {_COPILOT_USER_URL: _DEFAULT_PLAN}, DEFAULT_GITHUB_COPILOT_API_BASE), + ( + None, + {_COPILOT_USER_URL: {"endpoints": {"api": "https://api.business.githubcopilot.com"}}}, + "https://api.business.githubcopilot.com", + ), + ("https://copilot.test/gateway/", {}, "https://copilot.test/gateway"), + ], +) +def test_user_connection_poll_connected_persists_and_reports_login( + monkeypatch, api_base, plan_lookup, seat_check_base +): + table = _patch_user_connection_env(monkeypatch, [_per_user_credential()]) + if api_base is not None: + monkeypatch.setenv("GITHUB_COPILOT_API_BASE", api_base) with _github_http( { _DEVICE_CODE_URL: _DEVICE_FLOW_START, _ACCESS_TOKEN_URL: {"access_token": "gho_1"}, - _COPILOT_USER_URL: _COPILOT_USER_RESPONSE, - _COPILOT_MODELS_URL: {}, + **plan_lookup, + f"{seat_check_base}/models": _copilot_models_response("gho_1"), _GITHUB_USER_URL: {"login": "octo"}, } ): @@ -2268,11 +2265,10 @@ def test_user_connection_poll_connected_persists_and_reports_login(monkeypatch): assert poll.status_code == 200, poll.text assert poll.json() == {"status": "connected", "interval": None, "github_login": "octo"} table.upsert.assert_awaited_once() - stored_connection: Final = upc.decode_user_provider_credential( - table.upsert.await_args.kwargs["data"]["create"]["credential_b64"] - ) - assert stored_connection is not None - assert stored_connection.github_host == "github.com" + written = table.upsert.await_args.kwargs["data"] + for operation in ("create", "update"): + stored_connection = upc.decode_user_provider_credential(written[operation]["credential_b64"]) + assert stored_connection == upc.GithubCopilotUserConnectionPayload(access_token="gho_1", github_login="octo") def test_poll_host_change_race_requires_reconnect_on_the_next_request(monkeypatch): @@ -2280,18 +2276,16 @@ def test_poll_host_change_race_requires_reconnect_on_the_next_request(monkeypatc table: Final = _patch_user_connection_env(monkeypatch, [_per_user_credential(host="octo.ghe.com")]) - def change_host_after_access_token(request: httpx.Request) -> dict[str, str]: + def change_host_after_device_flow(request: httpx.Request) -> dict[str, str]: monkeypatch.setattr(litellm, "credential_list", [_per_user_credential()]) return {"access_token": "gho_1"} with _github_http( { "https://octo.ghe.com/login/device/code": _DEVICE_FLOW_START, - "https://octo.ghe.com/login/oauth/access_token": change_host_after_access_token, - "https://api.octo.ghe.com/copilot_internal/user": { - "endpoints": {"api": "https://copilot-api.octo.ghe.com"} - }, - "https://copilot-api.octo.ghe.com/models": {}, + "https://octo.ghe.com/login/oauth/access_token": change_host_after_device_flow, + "https://api.octo.ghe.com/copilot_internal/user": {"endpoints": {"api": "https://copilot-api.octo.ghe.com"}}, + "https://copilot-api.octo.ghe.com/models": _copilot_models_response("gho_1"), "https://api.octo.ghe.com/user": {"login": "octo"}, } ): @@ -2321,7 +2315,7 @@ def test_poll_host_change_race_requires_reconnect_on_the_next_request(monkeypatc }, } module_level_client: Final = MagicMock() - module_level_client.get.side_effect = AssertionError("unexpected GitHub user lookup") + module_level_client.get.side_effect = AssertionError("unexpected GitHub request") with _github_http({}), patch.object(litellm, "module_level_client", module_level_client): with pytest.raises(litellm.CallerCredentialAuthenticationError) as exc: attach_github_copilot_user_session(kwargs) @@ -2329,16 +2323,14 @@ def test_poll_host_change_race_requires_reconnect_on_the_next_request(monkeypatc assert stored_connection.github_host == "octo.ghe.com" -def test_ghe_user_connection_uses_tenant_urls_for_device_flow_and_user_lookup(monkeypatch): +def test_ghe_user_connection_uses_tenant_urls_for_device_flow_seat_check_and_login(monkeypatch): table: Final = _patch_user_connection_env(monkeypatch, [_per_user_credential(host="octo.ghe.com")]) with _github_http( { "https://octo.ghe.com/login/device/code": _DEVICE_FLOW_START, "https://octo.ghe.com/login/oauth/access_token": {"access_token": "gho_1"}, - "https://api.octo.ghe.com/copilot_internal/user": { - "endpoints": {"api": "https://copilot-api.octo.ghe.com"} - }, - "https://copilot-api.octo.ghe.com/models": {}, + "https://api.octo.ghe.com/copilot_internal/user": {"endpoints": {"api": "https://copilot-api.octo.ghe.com"}}, + "https://copilot-api.octo.ghe.com/models": _copilot_models_response("gho_1"), "https://api.octo.ghe.com/user": {"login": "octo"}, } ): @@ -2461,7 +2453,7 @@ def test_user_connections_isolated_per_user(monkeypatch): table.delete_many.assert_awaited_once_with(where={"user_id": "user-a", "credential_name": "copilot-cred"}) -@pytest.mark.parametrize("seat_status", [403, 404]) +@pytest.mark.parametrize("seat_status", [401, 403, 404]) def test_user_connection_poll_reports_no_copilot_seat(monkeypatch, seat_status): """A GitHub seat check that rejects the user's token maps to no_copilot_seat, stores nothing, and clears the in-flight device code.""" @@ -2474,8 +2466,10 @@ def test_user_connection_poll_reports_no_copilot_seat(monkeypatch, seat_status): if url == _ACCESS_TOKEN_URL: return httpx.Response(200, json={"access_token": "gho_seat"}, request=request) if url == _COPILOT_USER_URL: - return httpx.Response(200, json=_COPILOT_USER_RESPONSE, request=request) + return httpx.Response(200, json=_DEFAULT_PLAN, request=request) if url == _COPILOT_MODELS_URL: + assert request.method == "GET" + assert request.headers["Authorization"] == "Bearer gho_seat" return httpx.Response(seat_status, json={}, request=request) pytest.fail(f"unexpected request to {url}") @@ -2504,6 +2498,10 @@ def test_user_connection_poll_rate_limit_returns_429(monkeypatch): if url == _ACCESS_TOKEN_URL: return httpx.Response(200, json={"access_token": "gho_limited"}, request=request) if url == _COPILOT_USER_URL: + return httpx.Response(200, json=_DEFAULT_PLAN, request=request) + if url == _COPILOT_MODELS_URL: + assert request.method == "GET" + assert request.headers["Authorization"] == "Bearer gho_limited" return httpx.Response(429, json={}, request=request) pytest.fail(f"unexpected request to {url}") @@ -2733,23 +2731,6 @@ def test_pending_device_flow_survives_disconnect_via_stateless_handle(monkeypatc assert delete.json() == {"status": "disconnected"} -def test_delete_user_connection_evicts_copilot_session(monkeypatch): - table: Final = _patch_user_connection_env(monkeypatch, [_per_user_credential()], rows=[_connection_row()]) - session_key: Final = _session_cache_key("user-a", "copilot-cred", "github.com") - _SESSION_CACHE.set_cache( - session_key, - ("gho_secret", "copilot-token", "https://api.githubcopilot.com", 4102444800), - ttl=3600, - ) - - response: Final = _call_as("DELETE", "/credentials/copilot-cred/user_connection", auth=_as_user()) - - assert response.status_code == 200, response.text - assert response.json() == {"status": "disconnected"} - assert _SESSION_CACHE.get_cache(session_key) is None - table.delete_many.assert_awaited_once_with(where={"user_id": "user-a", "credential_name": "copilot-cred"}) - - def test_user_connection_cache_keys_cannot_collide_on_colons(): """user_id/credential_name are joined losslessly, so ('a:b','c') and ('a','b:c') can never share a cache or device-flow entry.""" @@ -2879,8 +2860,8 @@ def test_user_connection_poll_valid_handle_works_with_a_fresh_cache(monkeypatch) { _DEVICE_CODE_URL: _DEVICE_FLOW_START, _ACCESS_TOKEN_URL: {"access_token": "gho_1"}, - _COPILOT_USER_URL: _COPILOT_USER_RESPONSE, - _COPILOT_MODELS_URL: {}, + _COPILOT_USER_URL: _DEFAULT_PLAN, + _COPILOT_MODELS_URL: _copilot_models_response("gho_1"), _GITHUB_USER_URL: {"login": "octo"}, } ): @@ -2964,8 +2945,8 @@ def test_user_connection_poll_reports_503_when_the_cache_overwrite_cannot_land(m { _DEVICE_CODE_URL: _DEVICE_FLOW_START, _ACCESS_TOKEN_URL: {"access_token": "gho_1"}, - _COPILOT_USER_URL: _COPILOT_USER_RESPONSE, - _COPILOT_MODELS_URL: {}, + _COPILOT_USER_URL: _DEFAULT_PLAN, + _COPILOT_MODELS_URL: _copilot_models_response("gho_1"), _GITHUB_USER_URL: {"login": "octo"}, } ): diff --git a/tests/unit/proxy/proxy_server/test_routes_utils.py b/tests/unit/proxy/proxy_server/test_routes_utils.py index f93c23c8ce4..f73ac6e02a5 100644 --- a/tests/unit/proxy/proxy_server/test_routes_utils.py +++ b/tests/unit/proxy/proxy_server/test_routes_utils.py @@ -10,7 +10,6 @@ Pins (PR2): from __future__ import annotations import asyncio -import json import httpx import pytest @@ -177,15 +176,6 @@ def test_supported_openai_params_never_runs_oauth_for_authenticating_providers(c through ``get_llm_provider`` would run the provider's OAuth device flow and block the event loop.""" monkeypatch.setenv("GITHUB_COPILOT_TOKEN_DIR", str(tmp_path)) (tmp_path / "access-token").write_text("fake-access-token") - (tmp_path / "api-key.json").write_text( - json.dumps( - { - "token": "fake-api-key", - "expires_at": 4102444800, - "endpoints": {"api": "https://api.githubcopilot.com"}, - } - ) - ) router = litellm.Router( model_list=[ { diff --git a/tests/unit/router_strategy/test_complexity_router.py b/tests/unit/router_strategy/test_complexity_router.py index ba829512d8b..f6cc9f3d24d 100644 --- a/tests/unit/router_strategy/test_complexity_router.py +++ b/tests/unit/router_strategy/test_complexity_router.py @@ -4979,11 +4979,8 @@ class TestRouterPreRoutingAliasOverrides: string, and model info adopts the declared prefix. The recording wrapper raises for a copilot-directed resolution rather than calling through, so a regression fails on the recorded call instead of hanging the suite in a device-code poll.""" - import json - import time - monkeypatch.setenv("GITHUB_COPILOT_TOKEN_DIR", str(tmp_path)) - (tmp_path / "api-key.json").write_text(json.dumps({"token": "tid=test", "expires_at": int(time.time()) + 3600})) + (tmp_path / "access-token").write_text("gho_test") router = Router( model_list=[ { @@ -14880,11 +14877,8 @@ class TestContextWindowEscalation: """Resolving github_copilot runs its OAuth device flow, so a window question must adopt the declaration instead of resolving: the copilot group reads as unknown-window and the request stays put, with zero copilot resolutions recorded.""" - import json - import time - monkeypatch.setenv("GITHUB_COPILOT_TOKEN_DIR", str(tmp_path)) - (tmp_path / "api-key.json").write_text(json.dumps({"token": "tid=test", "expires_at": int(time.time()) + 3600})) + (tmp_path / "access-token").write_text("gho_test") router = ComplexityRouter( model_name="test-router", ctx=RoutingContext( diff --git a/tests/unit/router_utils/test_cooldown_handlers.py b/tests/unit/router_utils/test_cooldown_handlers.py index 6a35c8a9602..992dfd09713 100644 --- a/tests/unit/router_utils/test_cooldown_handlers.py +++ b/tests/unit/router_utils/test_cooldown_handlers.py @@ -7,6 +7,7 @@ import random import time import uuid from collections.abc import Mapping, Sequence +from pathlib import Path from typing import Final from unittest.mock import MagicMock, patch @@ -1517,20 +1518,18 @@ class TestPerUserCopilotAuthFailureCooldown: assert get_cooldown_deployments(RoutingContext(router), parent_otel_span=None) == [] @pytest.mark.asyncio - async def test_shared_mode_copilot_auth_failure_still_cools_down(self) -> None: - from litellm.llms.github_copilot.authenticator import Authenticator - + async def test_shared_mode_copilot_auth_failure_still_cools_down( + self, monkeypatch: pytest.MonkeyPatch, tmp_path: Path + ) -> None: + monkeypatch.setenv("GITHUB_COPILOT_TOKEN_DIR", str(tmp_path)) + (tmp_path / "access-token").write_text("gho_shared") model_id: Final = "shared-mode" - with ( - patch.object(Authenticator, "get_api_key", return_value="shared-copilot-token"), - patch.object(Authenticator, "get_api_base", return_value="https://api.githubcopilot.com"), - ): - router: Final = self._router(model_id, {"api_key": "sk-shared"}) - self._callback( - router, - model_id, - litellm.AuthenticationError("upstream rejected the shared token", "github_copilot", "gpt-4o"), - ) + router: Final = self._router(model_id, {"api_key": "sk-shared"}) + self._callback( + router, + model_id, + litellm.AuthenticationError("upstream rejected the shared token", "github_copilot", "gpt-4o"), + ) assert get_deployment_failures_for_current_minute(RoutingContext(router), model_id) == 1 assert get_cooldown_deployments(RoutingContext(router), parent_otel_span=None) == [model_id] @@ -1564,21 +1563,17 @@ class TestPerUserCopilotAuthFailureCooldown: assert get_cooldown_deployments(RoutingContext(router), parent_otel_span=None) == [] @pytest.mark.asyncio - async def test_fallback_shared_mode_auth_failure_still_cools_down(self) -> None: - from litellm.llms.github_copilot.authenticator import Authenticator - + async def test_fallback_shared_mode_auth_failure_still_cools_down( + self, monkeypatch: pytest.MonkeyPatch, tmp_path: Path + ) -> None: + monkeypatch.setenv("GITHUB_COPILOT_TOKEN_DIR", str(tmp_path)) + (tmp_path / "access-token").write_text("gho_shared") model_id: Final = "fallback-shared" - with ( - patch.object(Authenticator, "get_api_key", return_value="shared-copilot-token"), - patch.object(Authenticator, "get_api_base", return_value="https://api.githubcopilot.com"), - ): - router: Final = self._router(model_id, {"api_key": "sk-shared"}) - exception: Final = litellm.AuthenticationError( - "upstream rejected the shared token", "github_copilot", "gpt-4o" - ) - exception.failed_deployment_id = model_id + router: Final = self._router(model_id, {"api_key": "sk-shared"}) + exception: Final = litellm.AuthenticationError("upstream rejected the shared token", "github_copilot", "gpt-4o") + exception.failed_deployment_id = model_id - _trigger_cooldown_for_failed_deployment(ctx=RoutingContext(router), kwargs={}, exception=exception) + _trigger_cooldown_for_failed_deployment(ctx=RoutingContext(router), kwargs={}, exception=exception) assert get_cooldown_deployments(RoutingContext(router), parent_otel_span=None) == [model_id] @@ -1614,23 +1609,21 @@ class TestPerUserCopilotAuthFailureCooldown: assert get_cooldown_deployments(RoutingContext(router), parent_otel_span=None) == [] @pytest.mark.asyncio - async def test_fallback_rate_limit_on_shared_deployment_still_cools_down(self) -> None: - from litellm.llms.github_copilot.authenticator import Authenticator - + async def test_fallback_rate_limit_on_shared_deployment_still_cools_down( + self, monkeypatch: pytest.MonkeyPatch, tmp_path: Path + ) -> None: + monkeypatch.setenv("GITHUB_COPILOT_TOKEN_DIR", str(tmp_path)) + (tmp_path / "access-token").write_text("gho_shared") model_id: Final = "fallback-shared-429" - with ( - patch.object(Authenticator, "get_api_key", return_value="shared-copilot-token"), - patch.object(Authenticator, "get_api_base", return_value="https://api.githubcopilot.com"), - ): - router: Final = self._router( - model_id, - {"api_key": "sk-shared"}, - allowed_fails_policy={"RateLimitErrorAllowedFails": 0}, - ) - exception: Final = litellm.RateLimitError("copilot 429", "github_copilot", "gpt-4o") - exception.failed_deployment_id = model_id + router: Final = self._router( + model_id, + {"api_key": "sk-shared"}, + allowed_fails_policy={"RateLimitErrorAllowedFails": 0}, + ) + exception: Final = litellm.RateLimitError("copilot 429", "github_copilot", "gpt-4o") + exception.failed_deployment_id = model_id - _trigger_cooldown_for_failed_deployment(ctx=RoutingContext(router), kwargs={}, exception=exception) + _trigger_cooldown_for_failed_deployment(ctx=RoutingContext(router), kwargs={}, exception=exception) assert get_cooldown_deployments(RoutingContext(router), parent_otel_span=None) == [model_id] @@ -3203,11 +3196,12 @@ async def test_router_allowed_fails_still_benches_a_deployment_that_has_a_siblin def test_per_user_session_upstream_errors_never_cool_down_the_shared_deployment(single_deployment_router): """A 429/401 from the caller's own Copilot seat is scoped to that user; it must not cool down the shared deployment for every other caller.""" + from litellm.llms.github_copilot.github_host import github_host_endpoints from litellm.llms.github_copilot.per_user_auth import GithubCopilotUserSession session_kwargs = { "github_copilot_user_session": GithubCopilotUserSession( - token="copilot-token", api_base="https://api.githubcopilot.com" + token="gho_user", endpoints=github_host_endpoints("github.com") ) } for exc, status in ( diff --git a/tests/unit/test_internal_context.py b/tests/unit/test_internal_context.py index a446d980bcb..02301db81b5 100644 --- a/tests/unit/test_internal_context.py +++ b/tests/unit/test_internal_context.py @@ -56,8 +56,7 @@ _IN_MEMORY_ONLY_CALLERS: Final = frozenset( "litellm/llms/custom_httpx/http_handler.py", "litellm/llms/databricks/common_utils.py", "litellm/llms/gigachat/authenticator.py", - "litellm/llms/github_copilot/per_user_auth.py", - "litellm/llms/github_copilot/authenticator.py", + "litellm/llms/github_copilot/common_utils.py", "litellm/llms/litellm_proxy/skills/handler.py", "litellm/llms/openai/common_utils.py", "litellm/llms/openai_like/model_info.py", diff --git a/tests/unit/test_main.py b/tests/unit/test_main.py index 034b76507cf..80f3dc4a383 100644 --- a/tests/unit/test_main.py +++ b/tests/unit/test_main.py @@ -2533,7 +2533,8 @@ def non_openai_responses_bridge( json.dumps({"access_token": "token", "account_id": "acct", "expires_at": 4102444800}) ) monkeypatch.setenv("GITHUB_COPILOT_TOKEN_DIR", str(tmp_path)) - (tmp_path / "access-token").write_text("gho_test") + monkeypatch.setenv("GITHUB_COPILOT_API_BASE", "https://api.githubcopilot.com") + (tmp_path / "access-token").write_text("token") litellm.register_model( { **{ diff --git a/tests/unit/test_router/test_router.py b/tests/unit/test_router/test_router.py index dbc47fd6962..07543db7fd4 100644 --- a/tests/unit/test_router/test_router.py +++ b/tests/unit/test_router/test_router.py @@ -19179,7 +19179,7 @@ def test_pre_call_checks_filters_an_authenticating_provider_deployment_that_lack ): monkeypatch.setattr(litellm, "drop_params", False) monkeypatch.setenv("GITHUB_COPILOT_TOKEN_DIR", str(tmp_path)) - (tmp_path / "api-key.json").write_text(json.dumps({"token": "tid=test", "expires_at": 4102444800})) + (tmp_path / "access-token").write_text("gho_test") model_list: Final = [ { "model_name": "mixed-json-mode", diff --git a/tests/unit/test_router_model_cost_isolation.py b/tests/unit/test_router_model_cost_isolation.py index c0c8a0655fe..edddd095b15 100644 --- a/tests/unit/test_router_model_cost_isolation.py +++ b/tests/unit/test_router_model_cost_isolation.py @@ -47,9 +47,6 @@ from litellm.utils import ( ) -_COPILOT_TOKEN_EXPIRES_AT: Final = 4_102_444_800 - - def _simulate_price_data_reload(fetched_catalog): """Drive what a price data reload does to this process's litellm state. @@ -2575,7 +2572,7 @@ def test_a_github_copilot_chat_pin_opts_a_dual_endpoint_model_out_of_the_respons from litellm.llms.github_copilot.responses.transformation import github_copilot_supports_responses_api monkeypatch.setenv("GITHUB_COPILOT_TOKEN_DIR", str(tmp_path)) - (tmp_path / "api-key.json").write_text(json.dumps({"token": "tid=test", "expires_at": _COPILOT_TOKEN_EXPIRES_AT})) + (tmp_path / "access-token").write_text("gho_test") monkeypatch.setattr(litellm, "model_cost", copy.deepcopy(litellm.model_cost)) litellm.model_cost["github_copilot/routing-test-model"] = { "litellm_provider": "github_copilot", @@ -2616,7 +2613,7 @@ def test_a_github_copilot_responses_pin_still_opts_the_model_into_the_responses_ from litellm.llms.github_copilot.responses.transformation import github_copilot_supports_responses_api monkeypatch.setenv("GITHUB_COPILOT_TOKEN_DIR", str(tmp_path)) - (tmp_path / "api-key.json").write_text(json.dumps({"token": "tid=test", "expires_at": _COPILOT_TOKEN_EXPIRES_AT})) + (tmp_path / "access-token").write_text("gho_test") monkeypatch.setattr(litellm, "model_cost", copy.deepcopy(litellm.model_cost)) litellm.model_cost["github_copilot/routing-test-model"] = { "litellm_provider": "github_copilot",