fix(provider): preserve Codex prompt cache affinity

This commit is contained in:
chengyongru
2026-09-03 16:09:51 +08:00
committed by chengyongru
parent 30509b6d99
commit 972cdde8da
2 changed files with 41 additions and 8 deletions
+21 -5
View File
@@ -111,6 +111,7 @@ class OpenAICodexProvider(LLMProvider):
model=_strip_model_prefix(model),
)
session_id = provider_context.session_id if provider_context is not None else None
session_routing_key = _prompt_cache_key(session_id) if session_id else None
body: dict[str, Any] = {
"model": _strip_model_prefix(model),
@@ -122,8 +123,8 @@ class OpenAICodexProvider(LLMProvider):
"tool_choice": tool_choice or "auto",
"parallel_tool_calls": True,
}
if session_id:
body["prompt_cache_key"] = _prompt_cache_key(session_id)
if session_routing_key:
body["prompt_cache_key"] = session_routing_key
body["include"] = ["reasoning.encrypted_content"]
reasoning_options = _build_reasoning_options(reasoning_effort)
if replayed and "gpt-5.6" in _strip_model_prefix(model).lower():
@@ -136,13 +137,20 @@ class OpenAICodexProvider(LLMProvider):
if self._extra_body:
# Apply explicit provider overrides last, matching other provider backends.
body.update(self._extra_body)
effective_cache_key = body.get("prompt_cache_key")
stage = "oauth_token"
native_compaction_applied = False
native_compaction_state: ProviderConversationState | None = None
try:
token = await asyncio.to_thread(get_codex_token, proxy=self.proxy)
headers = _build_headers(cast(str, token.account_id), token.access)
headers = _build_headers(
cast(str, token.account_id),
token.access,
session_routing_key=(
effective_cache_key if isinstance(effective_cache_key, str) else None
),
)
async def _send(
request_body: dict[str, Any],
@@ -416,8 +424,13 @@ def _build_reasoning_options(reasoning_effort: str | None) -> dict[str, str] | N
return options
def _build_headers(account_id: str, token: str) -> dict[str, str]:
return {
def _build_headers(
account_id: str,
token: str,
*,
session_routing_key: str | None = None,
) -> dict[str, str]:
headers = {
"Authorization": f"Bearer {token}",
"chatgpt-account-id": account_id,
"OpenAI-Beta": "responses=experimental",
@@ -426,6 +439,9 @@ def _build_headers(account_id: str, token: str) -> dict[str, str]:
"accept": "text/event-stream",
"content-type": "application/json",
}
if session_routing_key:
headers["session-id"] = session_routing_key
return headers
class _CodexHTTPError(RuntimeError):