fix: Claude CLI 模型列表使用正确的 Bearer 认证方式

This commit is contained in:
fawney19
2026-02-09 22:45:21 +08:00
parent b11e9de6a7
commit 80d65ad2e4
2 changed files with 19 additions and 11 deletions

View File

@@ -160,13 +160,22 @@ class ClaudeChatAdapter(ChatAdapterBase):
api_key: str, api_key: str,
extra_headers: dict[str, str] | None = None, extra_headers: dict[str, str] | None = None,
) -> tuple[list, str | None]: ) -> tuple[list, str | None]:
"""查询 Claude API 支持的模型列表 """查询 Claude API 支持的模型列表(使用 x-api-key 认证)"""
headers = cls.build_headers_with_extra(api_key, extra_headers)
return await cls._fetch_models_paginated(client, base_url, headers, cls.FORMAT_ID)
@staticmethod
async def _fetch_models_paginated(
client: httpx.AsyncClient,
base_url: str,
headers: dict[str, str],
format_id: str,
) -> tuple[list, str | None]:
"""Claude 模型列表分页获取核心逻辑
Anthropic 的 /v1/models 是分页接口has_more/first_id/last_id Anthropic 的 /v1/models 是分页接口has_more/first_id/last_id
默认只返回一页。这里做 best-effort 的全量拉取,确保管理端能展示完整模型列表。 默认只返回一页。这里做 best-effort 的全量拉取,确保管理端能展示完整模型列表。
""" """
headers = cls.build_headers_with_extra(api_key, extra_headers)
# 构建 /v1/models URL # 构建 /v1/models URL
base_url = base_url.rstrip("/") base_url = base_url.rstrip("/")
if base_url.endswith("/v1"): if base_url.endswith("/v1"):
@@ -210,7 +219,7 @@ class ClaudeChatAdapter(ChatAdapterBase):
continue continue
if isinstance(mid, str) and mid: if isinstance(mid, str) and mid:
seen_ids.add(mid) seen_ids.add(mid)
m["api_format"] = cls.FORMAT_ID m["api_format"] = format_id
all_models.append(m) all_models.append(m)
# Pagination (Anthropic list response shape) # Pagination (Anthropic list response shape)

View File

@@ -110,16 +110,15 @@ class ClaudeCliAdapter(CliAdapterBase):
api_key: str, api_key: str,
extra_headers: dict[str, str] | None = None, extra_headers: dict[str, str] | None = None,
) -> tuple[list, str | None]: ) -> tuple[list, str | None]:
"""查询 Claude API 支持的模型列表( CLI User-Agent""" """查询 Claude API 支持的模型列表(使用 CLI Bearer 认证"""
# 复用 ClaudeChatAdapter 的实现,添加 CLI User-Agent
cli_headers = {"User-Agent": config.internal_user_agent_claude_cli} cli_headers = {"User-Agent": config.internal_user_agent_claude_cli}
if extra_headers: if extra_headers:
cli_headers.update(extra_headers) cli_headers.update(extra_headers)
models, error = await ClaudeChatAdapter.fetch_models(client, base_url, api_key, cli_headers) # 使用 CLI adapter 自己的认证头Authorization: Bearer而非 Chat 的 x-api-key
# 更新 api_format 为 CLI 格式 headers = cls.build_headers_with_extra(api_key, cli_headers)
for m in models: return await ClaudeChatAdapter._fetch_models_paginated(
m["api_format"] = cls.FORMAT_ID client, base_url, headers, cls.FORMAT_ID
return models, error )
@classmethod @classmethod
def build_endpoint_url( def build_endpoint_url(