feat(codex): 引入 upstream_headers hook 机制,为 Codex 注入 session/conversation/account headers

- 新增 upstream_headers.py:可注册 provider+endpoint 维度的 extra headers 构建 hook
- Codex openai:cli 注入 session_id + conversation_id(由 prompt_cache_key sha256 派生)
- Codex openai:compact 注入 chatgpt-account-id(来自 auth_config)+ session_id,不注入 conversation_id
- 修复 prompt_cache:compact 格式现统一为 codex 策略,不再跳过注入
- chat_handler_base / cli_request_mixin 均在 extra_headers 阶段调用 build_upstream_extra_headers
This commit is contained in:
fawney19
2026-03-18 20:43:14 +08:00
parent 203cd5a9d5
commit 3d5b6141a5
8 changed files with 305 additions and 17 deletions

View File

@@ -106,6 +106,21 @@ def test_maybe_patch_request_with_prompt_cache_key_for_codex_openai_cli() -> Non
assert out["prompt_cache_key"] == build_stable_codex_prompt_cache_key("user-key-123")
def test_maybe_patch_request_with_prompt_cache_key_for_codex_openai_compact() -> None:
req = {"model": "gpt-5", "input": []}
out = maybe_patch_request_with_prompt_cache_key(
req,
provider_api_format="openai:compact",
provider_type="codex",
base_url="https://chatgpt.com/backend-api/codex",
user_api_key_id="user-key-123",
)
assert out is not req
assert out["prompt_cache_key"] == build_stable_codex_prompt_cache_key("user-key-123")
def test_maybe_patch_request_with_prompt_cache_key_skips_official_compact() -> None:
req = {"model": "gpt-5", "input": []}
@@ -121,7 +136,7 @@ def test_maybe_patch_request_with_prompt_cache_key_skips_official_compact() -> N
assert "prompt_cache_key" not in out
def test_maybe_patch_request_with_prompt_cache_key_skips_legacy_codex_compact_context() -> None:
def test_maybe_patch_request_with_prompt_cache_key_for_legacy_codex_compact_context() -> None:
req = {"model": "gpt-5", "input": []}
try:
@@ -136,8 +151,8 @@ def test_maybe_patch_request_with_prompt_cache_key_skips_legacy_codex_compact_co
finally:
set_codex_request_context(None)
assert out is req
assert "prompt_cache_key" not in out
assert out is not req
assert out["prompt_cache_key"] == build_stable_codex_prompt_cache_key("user-key-123")
def test_maybe_patch_request_with_prompt_cache_key_preserves_existing_key() -> None: