2026-03-18 12:35:57 +08:00
|
|
|
"""Codex request-scoped context.
|
2026-02-08 23:51:50 +08:00
|
|
|
|
2026-03-18 12:35:57 +08:00
|
|
|
Codex only needs a small amount of per-request runtime state that does not belong in
|
|
|
|
|
the outbound payload itself. Today that state is the compact-mode flag used by the
|
|
|
|
|
transport and upstream stream-policy layers.
|
2026-02-08 23:51:50 +08:00
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
|
|
import contextvars
|
|
|
|
|
from dataclasses import dataclass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@dataclass(frozen=True, slots=True)
|
|
|
|
|
class CodexRequestContext:
|
2026-03-18 12:35:57 +08:00
|
|
|
"""Per-request context for the Codex adapter."""
|
2026-02-08 23:51:50 +08:00
|
|
|
|
2026-02-19 21:26:18 +08:00
|
|
|
is_compact: bool = False
|
2026-02-08 23:51:50 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
_codex_request_context: contextvars.ContextVar[CodexRequestContext | None] = contextvars.ContextVar(
|
|
|
|
|
"codex_request_context",
|
|
|
|
|
default=None,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def set_codex_request_context(ctx: CodexRequestContext | None) -> None:
|
|
|
|
|
_codex_request_context.set(ctx)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_codex_request_context() -> CodexRequestContext | None:
|
|
|
|
|
return _codex_request_context.get()
|
|
|
|
|
|
|
|
|
|
|
2026-03-18 12:35:57 +08:00
|
|
|
def is_codex_compact_request(*, endpoint_sig: str | None = None) -> bool:
|
|
|
|
|
"""Return whether the current Codex request should use compact semantics.
|
|
|
|
|
|
|
|
|
|
Modern configurations use a dedicated ``openai:compact`` endpoint. Older ones may
|
|
|
|
|
still route compact traffic through ``openai:cli`` and rely on request-scoped
|
|
|
|
|
context instead.
|
|
|
|
|
"""
|
|
|
|
|
normalized_sig = str(endpoint_sig or "").strip().lower()
|
|
|
|
|
if normalized_sig == "openai:compact":
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
ctx = get_codex_request_context()
|
|
|
|
|
return bool(ctx and ctx.is_compact)
|
|
|
|
|
|
|
|
|
|
|
2026-02-08 23:51:50 +08:00
|
|
|
__all__ = [
|
|
|
|
|
"CodexRequestContext",
|
|
|
|
|
"get_codex_request_context",
|
2026-03-18 12:35:57 +08:00
|
|
|
"is_codex_compact_request",
|
2026-02-08 23:51:50 +08:00
|
|
|
"set_codex_request_context",
|
|
|
|
|
]
|