Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
fix(codex): filter unsupported DeepSeek V4 custom tools (#3003)
许君山 Junshanc64152c644fd
diff --git a/apps/desktop/src/main/maker-host/__tests__/codexProxyHost.test.ts b/apps/desktop/src/main/maker-host/__tests__/codexProxyHost.test.tsindex c6a1bae36b..6525cb7033 100644--- a/apps/desktop/src/main/maker-host/__tests__/codexProxyHost.test.ts+++ b/apps/desktop/src/main/maker-host/__tests__/codexProxyHost.test.ts@@ -2396,8 +2396,8 @@ describe('codex proxy host', () => {// upstream 是函数形态(每请求现取,model-access 下发可运行期换 endpoint);// 断言其当前求值 = 网关 base + /v1upstream: expect.any(Function),- // [encrypted activeStrip, image generation activeStrip, provider-aware Guardian reviewer, locked Subagent route, instructions 注入, locked Subagent exec guard, Gateway 原生 web_search, 跨来源压缩块兼容, xAI ModelInput activeStrip, strict gateway history 兼容, xAI ModelInput sanitize, xAI Responses 兼容, ByteDance Seed tool 兼容, MiniMax effort 兼容, provider model rewrite, 视觉桥(controller 未注入 → 短路透传), stripNonAnthropicFields]- transformRequest: [expect.any(Function), expect.any(Function), expect.any(Function), expect.any(Function), expect.any(Function), expect.any(Function), expect.any(Function), expect.any(Function), expect.any(Function), expect.any(Function), expect.any(Function), expect.any(Function), expect.any(Function), expect.any(Function), expect.any(Function), expect.any(Function), mockState.stripNonAnthropicFields],+ // [encrypted activeStrip, image generation activeStrip, provider-aware Guardian reviewer, locked Subagent route, instructions 注入, locked Subagent exec guard, Gateway 原生 web_search, 跨来源压缩块兼容, xAI ModelInput activeStrip, strict gateway history 兼容, xAI ModelInput sanitize, DeepSeek V4 custom tool 兼容, xAI Responses 兼容, ByteDance Seed tool 兼容, MiniMax effort 兼容, provider model rewrite, 视觉桥(controller 未注入 → 短路透传), stripNonAnthropicFields]+ transformRequest: [expect.any(Function), expect.any(Function), expect.any(Function), expect.any(Function), expect.any(Function), expect.any(Function), expect.any(Function), expect.any(Function), expect.any(Function), expect.any(Function), expect.any(Function), expect.any(Function), expect.any(Function), expect.any(Function), expect.any(Function), expect.any(Function), expect.any(Function), mockState.stripNonAnthropicFields],routingTransform: expect.any(Function),recoveryRules: expect.arrayContaining([expect.objectContaining({ id: 'encrypted_content' }),@@ -4014,6 +4014,45 @@ describe('codex proxy host', () => {});});+ it('removes unsupported DeepSeek V4 custom tools but retains apply_patch', async () => {+ const host = await freshCodexProxyHost();+ mockState.createAnthropicCompatProxy.mockResolvedValueOnce({+ url: 'http://127.0.0.1:43210',+ dispose: vi.fn(async () => undefined),+ });+ await host.ensureCodexProxyReady();++ const transforms = mockState.createAnthropicCompatProxy.mock.calls[0]?.[0]?.transformRequest ?? [];+ let current: unknown = {+ model: 'deepseek/deepseek-v4-pro',+ tools: [+ 'exec',+ 'apply_patch',+ { type: 'custom', name: 'exec', description: 'run a command' },+ { type: 'custom', name: 'apply_patch', description: 'edit files' },+ { type: 'function', name: 'read_file', parameters: { type: 'object' } },+ ],+ tool_choice: { type: 'custom', name: 'exec' },+ input: 'review this file',+ };+ const ctx = { method: 'POST', url: '/responses', headers: {} };+ for (const transform of transforms) {+ const next = transform(current, ctx);+ if (next !== null && next !== undefined) current = next;+ }++ expect(current).toEqual({+ model: 'deepseek/deepseek-v4-pro',+ tools: [+ 'apply_patch',+ { type: 'custom', name: 'apply_patch', description: 'edit files' },+ { type: 'function', name: 'read_file', parameters: { type: 'object' } },+ ],+ tool_choice: 'auto',+ input: 'review this file',+ });+ });+it('keeps parallel strict-gateway tool calls grouped before their matched outputs', async () => {const host = await freshCodexProxyHost();mockState.createAnthropicCompatProxy.mockResolvedValueOnce({@@ -4862,7 +4901,7 @@ describe('codex proxy host', () => {await host.ensureCodexProxyReady();const transforms = mockState.createAnthropicCompatProxy.mock.calls[0]?.[0]?.transformRequest ?? [];- expect(transforms).toHaveLength(18); // encrypted activeStrip, image generation activeStrip, provider-aware Guardian reviewer, locked Subagent route, instructions 注入, locked Subagent exec guard, Gateway 原生 web_search, 跨来源压缩块兼容, xAI ModelInput activeStrip, strict gateway history 兼容, xAI ModelInput sanitize, xAI Responses 兼容, ByteDance Seed tool 兼容, MiniMax effort 兼容, provider model rewrite, 视觉桥(短路), stripNonAnthropicFields, dump+ expect(transforms).toHaveLength(19); // encrypted activeStrip, image generation activeStrip, provider-aware Guardian reviewer, locked Subagent route, instructions 注入, locked Subagent exec guard, Gateway 原生 web_search, 跨来源压缩块兼容, xAI ModelInput activeStrip, strict gateway history 兼容, xAI ModelInput sanitize, DeepSeek V4 custom tool 兼容, xAI Responses 兼容, ByteDance Seed tool 兼容, MiniMax effort 兼容, provider model rewrite, 视觉桥(短路), stripNonAnthropicFields, dumpconst ctx = {method: 'POST',url: '/v1/responses',diff --git a/apps/desktop/src/main/maker-host/codex-proxy-host.ts b/apps/desktop/src/main/maker-host/codex-proxy-host.tsindex fb29f57bea..beeca53dd0 100644--- a/apps/desktop/src/main/maker-host/codex-proxy-host.ts+++ b/apps/desktop/src/main/maker-host/codex-proxy-host.ts@@ -1527,6 +1527,15 @@ const STRICT_GATEWAY_TOOL_HISTORY_MODELS = new Set(['deepseek/deepseek-v4-pro','deepseek/deepseek-v4-flash',]);+// DeepSeek V4's Responses compatibility endpoint accepts the host's patch+// custom tool, but rejects every other custom tool (notably Codex's `exec`).+// Keep ordinary function tools untouched: the restriction is specifically on+// the Responses `custom` tool dialect, not on function calling as a whole.+const DEEPSEEK_V4_MODELS = new Set([+ 'deepseek/deepseek-v4-pro',+ 'deepseek/deepseek-v4-flash',+]);+const DEEPSEEK_V4_SUPPORTED_CUSTOM_TOOL_NAMES = new Set(['apply_patch']);const RESPONSE_TOOL_CALL_TYPES = new Set(['function_call', 'custom_tool_call']);const RESPONSE_TOOL_OUTPUT_TYPES = new Set(['function_call_output', 'custom_tool_call_output']);@@ -1685,6 +1694,58 @@ function createStrictGatewayHistoryCompatTransform(): RequestTransform {};}+function deepSeekToolChoiceReferencesRemovedCustomTool(+ toolChoice: unknown,+ tools: readonly unknown[],+): boolean {+ if (!isPlainObject(toolChoice) || toolChoice.type !== 'custom' || typeof toolChoice.name !== 'string') {+ return false;+ }+ return !tools.some((tool) =>+ tool === toolChoice.name ||+ (isPlainObject(tool) && tool.type === 'custom' && tool.name === toolChoice.name),+ );+}++/**+ * DeepSeek V4 rejects Codex's general-purpose custom `exec` tool before it+ * reaches the model, while accepting `apply_patch`. Filter only that custom+ * tool dialect so function/MCP declarations keep their existing semantics.+ */+function sanitizeDeepSeekV4CustomTools(body: unknown): Record<string, unknown> | null {+ if (!isPlainObject(body)) return null;+ if (!DEEPSEEK_V4_MODELS.has(typeof body.model === 'string' ? body.model : '')) return null;+ if (!Array.isArray(body.tools)) return null;++ let changed = false;+ const tools: unknown[] = [];+ for (const tool of body.tools) {+ const customToolName =+ typeof tool === 'string'+ ? tool+ : isPlainObject(tool) && tool.type === 'custom' && typeof tool.name === 'string'+ ? tool.name+ : null;+ if (customToolName !== null && !DEEPSEEK_V4_SUPPORTED_CUSTOM_TOOL_NAMES.has(customToolName)) {+ changed = true;+ continue;+ }+ tools.push(tool);+ }+ if (!changed) return null;++ const next: Record<string, unknown> = { ...body };+ if (tools.length > 0) {+ next.tools = tools;+ if (deepSeekToolChoiceReferencesRemovedCustomTool(next.tool_choice, tools)) next.tool_choice = 'auto';+ } else {+ delete next.tools;+ delete next.tool_choice;+ delete next.parallel_tool_calls;+ }+ return next;+}+/** Seed accepts the reasoning effort, but rejects Responses' summary selector. */function sanitizeByteDanceSeedReasoning(body: Record<string, unknown>): Record<string, unknown> | null {if (!isPlainObject(body.reasoning) || !('summary' in body.reasoning)) {@@ -2375,6 +2436,7 @@ function createTransformRequestChain(// Gateway / LiteLLM / 自定义 grok 不走 xAI 订阅 transform,但仍必须在// ModelInput deserialize 前洗 input[]。订阅直连那条会再洗一次(幂等)。createXaiModelInputSanitizeTransform(),+ sanitizeDeepSeekV4CustomTools,createXaiResponsesCompatTransform(),createByteDanceSeedResponsesCompatTransform(),createMiniMaxResponsesCompatTransform(),