feat(providers): Codex 缓存提示协议可选,Responses 对齐 CLI - #436
Merged
Conversation
Codex 供应商此前只要开启 Prompt 缓存就无条件补 prompt_cache_key, 第三方中转 / NIM / DeepSeek / Groq 等严格校验未知参数的 OpenAI 兼容端点 因此直接 400(#307)。根因是把"要不要缓存"与"端点认哪种缓存提示协议" 混成一个布尔开关——协议是端点能力,不该由 on/off 决定。 改为四选一的 promptCacheHintMode: - auto(默认)按 base URL 主机名判定,仅 api.openai.com 发 prompt_cache_key、 openrouter.ai 发 x-session-id,其余一律不发,退回服务端自动前缀缓存; - openai-key / openrouter-session 供自建端点手动指定; - none 完全不发。 模型级可选覆盖,缺省继承供应商设置,解决同一供应商下官方与中转模型混用。 旧配置无损迁移:promptCachingEnabled:false → none,未设置 → auto。 mode=none 时把 cacheRetention 一并压成 none:缓存提示由 pi-ai 在源头按 retention 生成(如 OpenRouter 上 anthropic/* 的 cache_control 断点), 事后剥 payload 已知字段拦不住。 Closes #435
StackCairn
marked this pull request as draft
August 12, 2026 11:30
Contributor
|
PR governance checks failed — this PR has been converted to draft.
Fix the items above, then click Ready for review to re-run the checks. |
su-fen
marked this pull request as ready for review
August 12, 2026 11:34
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Linked issue
Closes #435
Summary
Codex(OpenAI 兼容)供应商此前只用一个布尔开关控制 Prompt 缓存,无法区分 Responses API、Chat Completions,以及不同端点支持的缓存提示协议。本 PR 将它拆成四选一的
promptCacheHintMode,并让auto同时依据请求格式与端点能力决定 wire 行为。auto(默认,推荐)prompt_cache_key,与 Codex CLI 对齐;Chat Completions:api.openai.com发送prompt_cache_key,openrouter.ai发送x-session-id,其他端点不发送缓存提示openai-keyprompt_cache_key(按会话 id,64 字符上限)openrouter-sessionx-session-id请求头(256 字符上限)none设计要点:
prompt_cache_key,不按 Base URL 主机名关闭该字段;LiveAgent 的auto现在采用相同行为,第三方 Responses 端点也会收到该键。auto下不会收到prompt_cache_key,避免 调用模型报错 #307 的 400;OpenRouter 改用x-session-id会话粘性头。openai-key/openrouter-session覆盖自动判定,或用none完全禁用。标题、压缩等请求级cacheRetention: none仍会压过auto,不会发送缓存字段。ProviderModelConfig.promptCacheHintMode可选,缺省继承供应商设置,适合同一供应商下混用不同协议或能力的模型。promptCachingEnabled: false→none,未设置 →auto。布尔字段仅用于读取旧设置;非 Codex 供应商不受影响,Anthropic 的 ephemeral 缓存断点与 retention 档位保持原行为。none从源头禁用缓存提示。该模式会把cacheRetention一并压成none,阻止 pi-ai 在 Responses 或兼容链路生成缓存字段;最终 payload 中间件还会剥离既有的prompt_cache_key、prompt_cache_retention与prompt_cache_options。Change scope
agent-gui(前端 + src-tauri)/agent-ui(共享设置 UI)/agent-gateway/web(WebUI 镜像)crates/agent-gui/src/lib/providers/runtime/codexPromptCache.tsauto先按 Responses/Completions 分流,再按端点选择注入或剥离策略crates/agent-gui/src/lib/providers/runtime/payloadPipeline.tscrates/agent-gui/src/lib/providers/runtime/requestOptions.tscrates/agent-gui/src/lib/providers/runtime/types.ts/providerRuntimeConfig.ts/textOnlyRuntime.tscrates/agent-gui/src/lib/chat/runner/agentRunner.tscrates/agent-gui/src/lib/settings/index.ts+crates/agent-gateway/web/src/lib/settings/index.tscrates/agent-ui/src/pages/settings/ProvidersSection.tsxcrates/agent-gui/src-tauri/src/services/gateway_bridge.rscrates/agent-gui/src-tauri/src/services/proxy.rsx-session-id转发回归测试i18n/config.tsScreenshots / preview
UI 变更:Codex 供应商弹窗的 Prompt 缓存卡片,开关替换为“缓存提示协议”下拉(4 项);模型编辑区新增“缓存提示协议”下拉(5 项,含“继承供应商设置”)。Anthropic 供应商仍保持开关 + 保留档位。
wire 行为:
边界行为也有测试覆盖:pi-ai 已写入的字符串
prompt_cache_key不被覆盖;用户自带的X-Session-ID不被顶掉;会话 ID 按 64 / 256 字符分别截断;显式模式覆盖auto;请求级cacheRetention: none在 Responses 下仍禁止发送;prompt_cache_key: undefined不触发无意义 payload 拷贝。Verification
新增/更新的测试:
test/providers/request-options.test.mjs:请求格式优先级、第三方 Responses 的prompt_cache_key矩阵、第三方 Completions 字段剥离、OpenRouter 头注入、显式覆盖、长度上限、请求级禁用。pi-aiResponsesstream()截获最终 wire payload,确认第三方 Base URL 在auto下实际发送稳定的会话 key,而不仅是中间件结构断言。test/settings/normalization.test.mjs+crates/agent-gateway/test/webui/web-settings.test.mjs:双端归一化、旧配置迁移、非法值回落、模型级覆盖、非 Codex 供应商隔离。services/proxy.rs:x-session-id转发。Pre-submit checklist