diff --git a/agent/model_metadata.py b/agent/model_metadata.py index be63719e2..bea53611a 100644 --- a/agent/model_metadata.py +++ b/agent/model_metadata.py @@ -151,22 +151,41 @@ def _is_custom_endpoint(base_url: str) -> bool: return bool(normalized) and not _is_openrouter_base_url(normalized) -def _is_known_provider_base_url(base_url: str) -> bool: +_URL_TO_PROVIDER: Dict[str, str] = { + "api.openai.com": "openai", + "chatgpt.com": "openai", + "api.anthropic.com": "anthropic", + "api.z.ai": "zai", + "api.moonshot.ai": "kimi-coding", + "api.kimi.com": "kimi-coding", + "api.minimax": "minimax", + "dashscope.aliyuncs.com": "alibaba", + "openrouter.ai": "openrouter", + "inference-api.nousresearch.com": "nous", + "api.deepseek.com": "deepseek", +} + + +def _infer_provider_from_url(base_url: str) -> Optional[str]: + """Infer the models.dev provider name from a base URL. + + This allows context length resolution via models.dev for custom endpoints + like DashScope (Alibaba), Z.AI, Kimi, etc. without requiring the user to + explicitly set the provider name in config. + """ normalized = _normalize_base_url(base_url) if not normalized: - return False + return None parsed = urlparse(normalized if "://" in normalized else f"https://{normalized}") host = parsed.netloc.lower() or parsed.path.lower() - known_hosts = ( - "api.openai.com", - "chatgpt.com", - "api.anthropic.com", - "api.z.ai", - "api.moonshot.ai", - "api.kimi.com", - "api.minimax", - ) - return any(known_host in host for known_host in known_hosts) + for url_part, provider in _URL_TO_PROVIDER.items(): + if url_part in host: + return provider + return None + + +def _is_known_provider_base_url(base_url: str) -> bool: + return _infer_provider_from_url(base_url) is not None def is_local_endpoint(base_url: str) -> bool: @@ -808,13 +827,21 @@ def get_model_context_length( # These are provider-specific and take priority over the generic OR cache, # since the same model can have different context limits per provider # (e.g. claude-opus-4.6 is 1M on Anthropic but 128K on GitHub Copilot). - if provider == "nous": + # If provider is generic (openrouter/custom/empty), try to infer from URL. + effective_provider = provider + if not effective_provider or effective_provider in ("openrouter", "custom"): + if base_url: + inferred = _infer_provider_from_url(base_url) + if inferred: + effective_provider = inferred + + if effective_provider == "nous": ctx = _resolve_nous_context_length(model) if ctx: return ctx - if provider: + if effective_provider: from agent.models_dev import lookup_models_dev_context - ctx = lookup_models_dev_context(provider, model) + ctx = lookup_models_dev_context(effective_provider, model) if ctx: return ctx