refactor: Claude 适配器改用认证头区分 CLI/Chat 模式

将 build_claude_adapter 的判断依据从 x-app 自定义头改为标准认证头:
Bearer token (无 x-api-key) 走 CLI 模式,x-api-key 走 Chat 模式。
移除 ClaudeCliAdapter.get_cli_extra_headers 中不再需要的 x-app 头注入。
This commit is contained in:
fawney19
2026-02-21 15:39:59 +08:00
parent bb7d393128
commit 414f4e40c7
3 changed files with 15 additions and 18 deletions

View File

@@ -264,9 +264,17 @@ class ClaudeChatAdapter(ChatAdapterBase):
# build_request_body 使用基类实现,通过 format_conversion_registry 自动转换 OPENAI -> CLAUDE
def build_claude_adapter(x_app_header: str | None) -> Any:
"""根据 x-app 头部构造 Chat 或 Claude Code 适配器。"""
if x_app_header and x_app_header.lower() == "cli":
def build_claude_adapter(request: Request) -> Any:
"""根据认证头构造 Chat 或 Claude Code 适配器。
- Authorization: Bearer (且无 x-api-key) -> CLI 模式
- x-api-key -> Chat 模式
"""
auth_header = request.headers.get("authorization", "")
has_bearer = auth_header.lower().startswith("bearer ")
has_api_key = bool(request.headers.get("x-api-key"))
if has_bearer and not has_api_key:
from src.api.handlers.claude_cli.adapter import ClaudeCliAdapter
return ClaudeCliAdapter()

View File

@@ -138,12 +138,5 @@ class ClaudeCliAdapter(CliAdapterBase):
"""获取Claude CLI User-Agent"""
return config.internal_user_agent_claude_cli
@classmethod
def get_cli_extra_headers(cls, *, base_url: str | None = None) -> dict[str, str]:
"""获取Claude CLI额外请求头包含 x-app: cli 标识"""
headers = super().get_cli_extra_headers(base_url=base_url)
headers["x-app"] = "cli" # 标识 CLI 模式,让上游使用正确的认证方式
return headers
__all__ = ["ClaudeCliAdapter"]