获取可用模型列表,返回符合 OpenAI API 规范的格式 Returns: 模型列表,格式为字典列表(用于兼容现有代码) Raises: 返回空列表如果获取失败
()
| 826 | # ==================== 模型和配额查询 ==================== |
| 827 | |
| 828 | async def fetch_available_models() -> List[Dict[str, Any]]: |
| 829 | """ |
| 830 | 获取可用模型列表,返回符合 OpenAI API 规范的格式 |
| 831 | |
| 832 | Returns: |
| 833 | 模型列表,格式为字典列表(用于兼容现有代码) |
| 834 | |
| 835 | Raises: |
| 836 | 返回空列表如果获取失败 |
| 837 | """ |
| 838 | # 获取凭证管理器和可用凭证 |
| 839 | cred_result = await credential_manager.get_valid_credential(mode="antigravity") |
| 840 | if not cred_result: |
| 841 | log.error("[ANTIGRAVITY] No valid credentials available for fetching models") |
| 842 | return [] |
| 843 | |
| 844 | current_file, credential_data = cred_result |
| 845 | access_token = credential_data.get("access_token") or credential_data.get("token") |
| 846 | |
| 847 | if not access_token: |
| 848 | log.error(f"[ANTIGRAVITY] No access token in credential: {current_file}") |
| 849 | return [] |
| 850 | |
| 851 | # 构建请求头 |
| 852 | headers = build_antigravity_headers(access_token) |
| 853 | |
| 854 | try: |
| 855 | # 使用 POST 请求获取模型列表 |
| 856 | antigravity_url = await get_antigravity_api_url() |
| 857 | |
| 858 | response = await post_async( |
| 859 | url=f"{antigravity_url}/v1internal:fetchAvailableModels", |
| 860 | json={}, # 空的请求体 |
| 861 | headers=headers |
| 862 | ) |
| 863 | |
| 864 | if response.status_code == 200: |
| 865 | data = response.json() |
| 866 | log.debug(f"[ANTIGRAVITY] Raw models response: {json.dumps(data, ensure_ascii=False)[:500]}") |
| 867 | |
| 868 | # 转换为 OpenAI 格式的模型列表,使用 Model 类 |
| 869 | model_list = [] |
| 870 | current_timestamp = int(datetime.now(timezone.utc).timestamp()) |
| 871 | |
| 872 | if 'models' in data and isinstance(data['models'], dict): |
| 873 | # 遍历模型字典 |
| 874 | for model_id in data['models'].keys(): |
| 875 | model = Model( |
| 876 | id=model_id, |
| 877 | object='model', |
| 878 | created=current_timestamp, |
| 879 | owned_by='google' |
| 880 | ) |
| 881 | model_list.append(model_to_dict(model)) |
| 882 | # 添加额外的 claude-sonnet-4-6-thinking 模型 |
| 883 | if "claude-sonnet-4-6" in data.get('models', {}): |
| 884 | model = Model( |
| 885 | id='claude-sonnet-4-6-thinking', |
no test coverage detected