|
| 1 | +from typing import Any, Optional |
| 2 | + |
| 3 | +_PROVIDER_ALIASES: dict[str, str] = { |
| 4 | + "zai_coding_plan": "zai-coding-plan", |
| 5 | + "zhipuai_coding_plan": "zhipuai-coding-plan", |
| 6 | + "alibaba_coding_plan": "alibaba-coding-plan", |
| 7 | + "alibaba_coding_plan_cn": "alibaba-coding-plan-cn", |
| 8 | +} |
| 9 | + |
| 10 | +# Provider default base URLs used when callers omit an explicit base URL. |
| 11 | +_DEFAULT_BASE_URL_BY_PROVIDER: dict[str, str] = { |
| 12 | + "openai": "https://api.openai.com/v1", |
| 13 | + "zhipu": "https://open.bigmodel.cn/api/paas/v4", |
| 14 | + "dashscope": "https://dashscope.aliyuncs.com/compatible-mode/v1", |
| 15 | + # Opencode / models.dev naming |
| 16 | + "zai-coding-plan": "https://api.z.ai/api/coding/paas/v4", |
| 17 | + "zhipuai-coding-plan": "https://open.bigmodel.cn/api/coding/paas/v4", |
| 18 | + # Alibaba Bailian (Model Studio) coding plan |
| 19 | + "alibaba-coding-plan": "https://coding-intl.dashscope.aliyuncs.com/v1", |
| 20 | + "alibaba-coding-plan-cn": "https://coding.dashscope.aliyuncs.com/v1", |
| 21 | +} |
| 22 | + |
| 23 | +_CURATED_MODELS_BY_PROVIDER: dict[str, tuple[str, ...]] = { |
| 24 | + "alibaba-coding-plan": ( |
| 25 | + "glm-4.7", |
| 26 | + "glm-5", |
| 27 | + "qwen3-coder-next", |
| 28 | + "qwen3-coder-plus", |
| 29 | + "qwen3-max-2026-01-23", |
| 30 | + "qwen3.5-plus", |
| 31 | + ), |
| 32 | + "alibaba-coding-plan-cn": ( |
| 33 | + "glm-4.7", |
| 34 | + "glm-5", |
| 35 | + "qwen3-coder-next", |
| 36 | + "qwen3-coder-plus", |
| 37 | + "qwen3-max-2026-01-23", |
| 38 | + "qwen3.5-plus", |
| 39 | + ), |
| 40 | +} |
| 41 | + |
| 42 | +_SUPPORTED_PROVIDER_METADATA: tuple[dict[str, Any], ...] = ( |
| 43 | + { |
| 44 | + "id": "openai", |
| 45 | + "name": "OpenAI", |
| 46 | + "description": "OpenAI API compatible models", |
| 47 | + "requires_base_url": False, |
| 48 | + "compatibility": "openai_compatible", |
| 49 | + }, |
| 50 | + { |
| 51 | + "id": "zai-coding-plan", |
| 52 | + "name": "Z.AI Coding Plan", |
| 53 | + "description": "GLM coding plan via Z.AI", |
| 54 | + "requires_base_url": False, |
| 55 | + "compatibility": "openai_compatible", |
| 56 | + }, |
| 57 | + { |
| 58 | + "id": "zhipuai-coding-plan", |
| 59 | + "name": "Zhipu AI Coding Plan", |
| 60 | + "description": "GLM coding plan via Zhipu AI", |
| 61 | + "requires_base_url": False, |
| 62 | + "compatibility": "openai_compatible", |
| 63 | + }, |
| 64 | + { |
| 65 | + "id": "alibaba-coding-plan", |
| 66 | + "name": "Alibaba Coding Plan", |
| 67 | + "description": "Alibaba Bailian (Model Studio) coding plan", |
| 68 | + "requires_base_url": False, |
| 69 | + "compatibility": "openai_compatible", |
| 70 | + }, |
| 71 | + { |
| 72 | + "id": "alibaba-coding-plan-cn", |
| 73 | + "name": "Alibaba Coding Plan (China)", |
| 74 | + "description": "Alibaba Bailian (Model Studio) coding plan (China)", |
| 75 | + "requires_base_url": False, |
| 76 | + "compatibility": "openai_compatible", |
| 77 | + }, |
| 78 | + { |
| 79 | + "id": "claude", |
| 80 | + "name": "Anthropic Claude", |
| 81 | + "description": "Anthropic's Claude models", |
| 82 | + "requires_base_url": False, |
| 83 | + "compatibility": "claude_compatible", |
| 84 | + }, |
| 85 | + { |
| 86 | + "id": "gemini", |
| 87 | + "name": "Google Gemini", |
| 88 | + "description": "Google's Gemini models", |
| 89 | + "requires_base_url": False, |
| 90 | + }, |
| 91 | + { |
| 92 | + "id": "xinference", |
| 93 | + "name": "Xinference", |
| 94 | + "description": "Xinference models for local inference", |
| 95 | + "requires_base_url": True, |
| 96 | + }, |
| 97 | + { |
| 98 | + "id": "zhipu", |
| 99 | + "name": "Zhipu AI", |
| 100 | + "description": "Zhipu AI models (GLM series) using zai SDK", |
| 101 | + "requires_base_url": False, |
| 102 | + }, |
| 103 | + { |
| 104 | + "id": "dashscope", |
| 105 | + "name": "DashScope", |
| 106 | + "description": "Alibaba Cloud's DashScope models", |
| 107 | + "requires_base_url": False, |
| 108 | + }, |
| 109 | +) |
| 110 | + |
| 111 | + |
| 112 | +def _normalize_provider(provider: str) -> str: |
| 113 | + return provider.lower().strip() |
| 114 | + |
| 115 | + |
| 116 | +def canonical_provider_name(provider: str) -> str: |
| 117 | + normalized = _normalize_provider(provider) |
| 118 | + return _PROVIDER_ALIASES.get(normalized, normalized) |
| 119 | + |
| 120 | + |
| 121 | +def default_base_url_for_provider(provider: str) -> Optional[str]: |
| 122 | + return _DEFAULT_BASE_URL_BY_PROVIDER.get(canonical_provider_name(provider)) |
| 123 | + |
| 124 | + |
| 125 | +def curated_models_for_provider(provider: str) -> tuple[str, ...]: |
| 126 | + return _CURATED_MODELS_BY_PROVIDER.get(canonical_provider_name(provider), ()) |
| 127 | + |
| 128 | + |
| 129 | +def provider_compatibility_for_provider(provider: str) -> Optional[str]: |
| 130 | + provider_id = canonical_provider_name(provider) |
| 131 | + for provider_info in _SUPPORTED_PROVIDER_METADATA: |
| 132 | + if provider_info["id"] == provider_id: |
| 133 | + compatibility = provider_info.get("compatibility") |
| 134 | + return str(compatibility) if compatibility is not None else None |
| 135 | + return None |
| 136 | + |
| 137 | + |
| 138 | +def get_supported_provider_metadata() -> list[dict[str, Any]]: |
| 139 | + providers: list[dict[str, Any]] = [] |
| 140 | + for provider in _SUPPORTED_PROVIDER_METADATA: |
| 141 | + provider_info = dict(provider) |
| 142 | + default_base_url = default_base_url_for_provider(provider_info["id"]) |
| 143 | + if default_base_url is not None: |
| 144 | + provider_info["default_base_url"] = default_base_url |
| 145 | + providers.append(provider_info) |
| 146 | + return providers |
0 commit comments