feat: Codex 请求转换引入 patch_for_variant 快速路径,Kiro 重复账号允许覆盖

- FormatNormalizer 新增 patch_for_variant 可选方法,同格式 + variant 场景
  跳过 internal 往返,直接在原始请求体上做最小补丁
- OpenAICliNormalizer 实现 Codex variant 快速路径,registry 优先尝试
- Codex request_patching 补充 stream=true、parallel_tool_calls=true、
  移除 previous_response_id
- Kiro 重复账号从拒绝改为允许覆盖(用户重新导入同一账号场景)
- Kiro Key 命名增加 auth_method 后缀,提取 _build_kiro_key_name 辅助函数
- Kiro token refresh 错误日志从 debug 提升为 warning,响应体截取增至 500 字符
This commit is contained in:
fawney19
2026-02-21 03:53:29 +08:00
parent 457fe83a4f
commit a1d972419d
6 changed files with 103 additions and 28 deletions

View File

@@ -1,20 +1,21 @@
"""
Codex provider request patching helpers (standalone / passthrough path).
Codex provider request patching helpers (passthrough path).
In the main request pipeline, Codex-specific transformations are handled by the
``openai:cli`` normalizer with ``target_variant="codex"`` (triggered automatically
via ``register_behavior_variant("codex", same_format=True)``).
This module provides an **equivalent** standalone patcher for contexts where the full
normalizer pipeline is not used (e.g. external tooling, one-off scripts, or future
passthrough-only paths). It is intentionally kept in sync with the normalizer logic.
This is the **primary** Codex request transformation used by the normalizer's
``patch_for_variant("codex")`` fast path. It applies minimal, non-destructive
patches directly on the original request dict -- no internal representation
round-trip, so every field the client sent is preserved as-is unless explicitly
modified here.
Transformations applied:
- Force ``store=false`` (avoid persistence features not supported by some gateways).
- Force ``stream=true`` (Codex gateways require streaming).
- Force ``parallel_tool_calls=true``.
- Ensure ``instructions`` exists (Codex expects it in some deployments).
- Convert ``role=system`` messages to ``role=developer`` (Codex may not accept ``system``).
- Drop request parameters known to be rejected by Codex gateways.
- Ensure ``include`` contains ``"reasoning.encrypted_content"`` for parity with CLI behavior.
- Remove ``previous_response_id`` (not supported by Codex gateways).
"""
from __future__ import annotations
@@ -31,6 +32,7 @@ _REJECTED_PARAMS: frozenset[str] = frozenset(
"temperature",
"top_p",
"service_tier",
"previous_response_id",
}
)
@@ -51,6 +53,12 @@ def patch_openai_cli_request_for_codex(request_body: dict[str, Any]) -> dict[str
# Codex gateways often reject/ignore persistence; be explicit.
out["store"] = False
# Codex gateways require streaming.
out["stream"] = True
# Codex expects parallel tool calls enabled.
out["parallel_tool_calls"] = True
# Ensure instructions exists (some gateways require it even if empty).
instructions = out.get("instructions")
if not isinstance(instructions, str):

View File

@@ -150,12 +150,13 @@ async def refresh_social_token(
)
if resp.status_code < 200 or resp.status_code >= 300:
logger.debug(
body_text = (resp.text or "").strip()[:500]
logger.warning(
"kiro social refresh error: HTTP {} | {}",
resp.status_code,
(resp.text or "").strip()[:200],
body_text,
)
raise RuntimeError(f"kiro social refresh failed: HTTP {resp.status_code}")
raise RuntimeError(f"kiro social refresh failed: HTTP {resp.status_code} | {body_text}")
data: dict[str, Any]
try:
@@ -242,12 +243,13 @@ async def refresh_idc_token(
)
if resp.status_code < 200 or resp.status_code >= 300:
logger.debug(
body_text = (resp.text or "").strip()[:500]
logger.warning(
"kiro idc refresh error: HTTP {} | {}",
resp.status_code,
(resp.text or "").strip()[:200],
body_text,
)
raise RuntimeError(f"kiro idc refresh failed: HTTP {resp.status_code}")
raise RuntimeError(f"kiro idc refresh failed: HTTP {resp.status_code} | {body_text}")
data: dict[str, Any]
try: