feat(body-rules): 支持按 provider_type 覆盖 cache-sensitive 保护字段集合

Codex 通过 OpenAI CLI/Compact 格式转发时,endpoint body_rules 需要能
修改 instructions/input/tools 等 prompt 字段。新增 provider_type 维度的
保护集合映射,Codex 场景仅保护 prompt_cache_key,其余 prompt 字段交由
body_rules 自由调整。
This commit is contained in:
fawney19
2026-03-17 17:09:39 +08:00
parent c4bb6b8161
commit c1ed42fd3a
7 changed files with 98 additions and 7 deletions

View File

@@ -846,7 +846,10 @@ class ChatHandlerBase(BaseMessageHandler, ABC):
mapped_model=mapped_model,
envelope=envelope,
extra_headers=extra_headers,
protected_body_keys=get_cache_sensitive_protected_body_keys(provider_api_format),
protected_body_keys=get_cache_sensitive_protected_body_keys(
provider_api_format,
provider_type=provider_type,
),
upstream_is_stream=upstream_is_stream,
needs_conversion=needs_conversion,
provider_api_format=provider_api_format,

View File

@@ -442,7 +442,10 @@ class CliStreamMixin:
extra_headers=extra_headers if extra_headers else None,
pre_computed_auth=auth_info.as_tuple() if auth_info else None,
envelope=envelope,
protected_body_keys=get_cache_sensitive_protected_body_keys(provider_api_format),
protected_body_keys=get_cache_sensitive_protected_body_keys(
provider_api_format,
provider_type=provider_type,
),
provider_api_format=provider_api_format,
)
if upstream_is_stream:

View File

@@ -263,7 +263,10 @@ class CliSyncMixin:
extra_headers=extra_headers if extra_headers else None,
pre_computed_auth=auth_info.as_tuple() if auth_info else None,
envelope=envelope,
protected_body_keys=get_cache_sensitive_protected_body_keys(provider_api_format),
protected_body_keys=get_cache_sensitive_protected_body_keys(
provider_api_format,
provider_type=provider_type,
),
provider_api_format=provider_api_format,
)
if upstream_is_stream:

View File

@@ -514,7 +514,10 @@ class HandlerAdapterBase(ApiAdapter):
body = apply_body_rules(
body,
body_rules,
protected_keys=get_cache_sensitive_protected_body_keys(cls.FORMAT_ID),
protected_keys=get_cache_sensitive_protected_body_keys(
cls.FORMAT_ID,
provider_type=provider_type,
),
original_body=body,
)

View File

@@ -129,11 +129,27 @@ _CACHE_SENSITIVE_BODY_FIELDS_BY_FORMAT: dict[str, frozenset[str]] = {
),
}
# Provider 维度可覆盖默认 prompt-bearing 保护集合。
# Codex OpenAI CLI/Compact 允许 endpoint body_rules 调整 instructions/input/tools/tool_choice
# 仅继续保护系统字段与 prompt_cache_key避免"已保存但规则不生效"。
_CODEX_PROTECTED: frozenset[str] = frozenset({"prompt_cache_key"})
_CACHE_SENSITIVE_BODY_FIELDS_BY_PROVIDER_AND_FORMAT: dict[tuple[str, str], frozenset[str]] = {
("codex", "openai:cli"): _CODEX_PROTECTED,
("codex", "openai:compact"): _CODEX_PROTECTED,
}
def get_cache_sensitive_protected_body_keys(provider_api_format: str | None) -> frozenset[str]:
"""根据目标 Provider API 格式返回需要保护的顶层请求字段。"""
def get_cache_sensitive_protected_body_keys(
provider_api_format: str | None,
*,
provider_type: str | None = None,
) -> frozenset[str]:
"""根据目标 Provider API 格式和 provider_type 返回需要保护的顶层请求字段。"""
fmt = str(provider_api_format or "").strip().lower()
extra = _CACHE_SENSITIVE_BODY_FIELDS_BY_FORMAT.get(fmt, frozenset())
pt = str(provider_type or "").strip().lower()
extra = _CACHE_SENSITIVE_BODY_FIELDS_BY_PROVIDER_AND_FORMAT.get((pt, fmt))
if extra is None:
extra = _CACHE_SENSITIVE_BODY_FIELDS_BY_FORMAT.get(fmt, frozenset())
if not extra:
return PROTECTED_BODY_FIELDS
return frozenset({*PROTECTED_BODY_FIELDS, *extra})