feat(claude-code): 为 claude_code provider 补全 Claude Code 请求体特征

非 Claude Code 客户端(如 pi)经 OAuth 的 claude_code provider 转发时,
只有请求头被伪装成 Claude Code,请求体仍是客户端原样,被上游以
429 rate_limit_error 拒绝(响应无 ratelimit 额度头,并非真实限流)。

参考 sub2api 的 OAuth 请求体伪装,在传输层补齐请求体:
- system 重写为计费头 + 身份句 + 通用提示词三块(Fable 仅保留前两块)
- 原 system 迁入 messages 开头,避免丢失客户端指令
- 缺失时补 metadata.user_id,device/session id 基于 key 稳定派生
- 缺失时补 tools/temperature/max_tokens,并限制 cache_control 不超过 4 个
- 已带计费块且有 metadata.user_id 的真实 Claude Code 请求原样放行,
  重复应用幂等

接入点覆盖跨格式路径(apply_transport_request_body_semantics)和
原生 claude:messages 同格式路径,仅作用于 provider_type=claude_code。
This commit is contained in:
dalamudx
2026-09-29 18:35:23 +08:00
parent 00a315e3bf
commit c1aa5d618d
4 changed files with 734 additions and 1 deletions
@@ -1,5 +1,6 @@
mod auth;
mod fingerprint;
mod mimicry;
mod policy;
mod profile;
mod request;
@@ -10,6 +11,10 @@ pub use fingerprint::{
generate_fingerprint, generate_random_fingerprint, header_fingerprint_from_fingerprint,
sanitize_fingerprint,
};
pub use mimicry::{
apply_claude_code_body_mimicry, apply_claude_code_body_mimicry_for_transport,
ClaudeCodeBodyMimicryContext,
};
pub use policy::{
local_claude_code_transport_unsupported_reason_with_network,
supports_local_claude_code_transport_with_network,