Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
fix(desktop): support ByteDance Seed tools in Codex proxy (#339)
Yecheng Fufb3d4f5013c4
diff --git a/apps/desktop/src/main/maker-host/__tests__/codexProxyHost.test.ts b/apps/desktop/src/main/maker-host/__tests__/codexProxyHost.test.tsindex 8bfe009b12..e689e2cec8 100644--- a/apps/desktop/src/main/maker-host/__tests__/codexProxyHost.test.ts+++ b/apps/desktop/src/main/maker-host/__tests__/codexProxyHost.test.ts@@ -452,8 +452,8 @@ describe('codex proxy host', () => {// upstream 是函数形态(每请求现取,model-access 下发可运行期换 endpoint);// 断言其当前求值 = 网关 base + /v1upstream: expect.any(Function),- // [encrypted activeStrip, image generation activeStrip, instructions 注入, 跨来源压缩块兼容, xAI Responses 兼容, MiniMax effort 兼容, provider model rewrite, stripNonAnthropicFields]- transformRequest: [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, instructions 注入, 跨来源压缩块兼容, xAI Responses 兼容, ByteDance Seed tool 兼容, MiniMax effort 兼容, provider model rewrite, 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), mockState.stripNonAnthropicFields],routingTransform: expect.any(Function),recoveryRules: expect.arrayContaining([expect.objectContaining({ id: 'encrypted_content' }),@@ -662,6 +662,198 @@ describe('codex proxy host', () => {clearSessionProvider('session-openai-custom-tool');});+ it('normalizes the Codex tool set to ByteDance Seed Responses capabilities', async () => {+ const host = await freshCodexProxyHost();+ const { setSessionProvider, clearSessionProvider } = await import('../session-provider-store.js');+ mockState.createAnthropicCompatProxy.mockResolvedValueOnce({+ url: 'http://127.0.0.1:43210',+ dispose: vi.fn(async () => undefined),+ });+ await host.ensureCodexProxyReady();+ host.registerComposed('session-seed', 'thread-seed', 'PRODUCT_PROMPT');+ setSessionProvider('session-seed', 'xd');++ const transforms = mockState.createAnthropicCompatProxy.mock.calls[0]?.[0]?.transformRequest ?? [];+ let current: unknown = {+ model: 'bytedance-seed/seed-2.1-pro',+ tools: [+ { type: 'function', name: 'exec_command' },+ { type: 'function', name: 'write_stdin' },+ { type: 'namespace', name: 'multi_agent_v1', tools: [{ type: 'function', name: 'close_agent' }] },+ { type: 'web_search', external_web_access: true },+ { type: 'image_generation' },+ ],+ tool_choice: 'auto',+ parallel_tool_calls: false,+ input: [+ { type: 'message', role: 'assistant', content: [{ type: 'output_text', text: 'earlier answer' }] },+ { type: 'reasoning', summary: [], content: null },+ { type: 'message', role: 'user', content: [{ type: 'input_text', text: 'hello' }] },+ ],+ };+ const ctx = {+ method: 'POST',+ url: '/responses',+ headers: { 'thread-id': 'thread-seed' },+ };+ for (const transform of transforms) {+ const next = transform(current, ctx);+ if (next !== null && next !== undefined) current = next;+ }++ expect(current).toEqual({+ model: 'bytedance-seed/seed-2.1-pro',+ tools: [+ { type: 'function', name: 'exec_command' },+ { type: 'function', name: 'write_stdin' },+ { type: 'web_search' },+ ],+ tool_choice: 'auto',+ parallel_tool_calls: false,+ input: [+ {+ type: 'message',+ role: 'assistant',+ status: 'completed',+ content: [{ type: 'output_text', text: 'earlier answer' }],+ },+ { type: 'reasoning', summary: [], content: null },+ { type: 'message', role: 'user', content: [{ type: 'input_text', text: 'hello' }] },+ ],+ });+ clearSessionProvider('session-seed');+ });++ it('removes Seed tool controls when every declared tool is unsupported', 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: 'bytedance-seed/seed-2.1-pro',+ tools: [{ type: 'namespace', name: 'multi_agent_v1', tools: [] }],+ tool_choice: 'auto',+ parallel_tool_calls: false,+ input: 'hello',+ };+ 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: 'bytedance-seed/seed-2.1-pro',+ input: 'hello',+ });+ });++ it('drops Seed web search when Codex explicitly disables live web access', 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: 'bytedance-seed/seed-2.1-pro',+ tools: [+ { type: 'function', name: 'exec_command' },+ { type: 'web_search', external_web_access: false },+ ],+ tool_choice: 'auto',+ parallel_tool_calls: false,+ input: 'hello',+ };+ 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: 'bytedance-seed/seed-2.1-pro',+ tools: [{ type: 'function', name: 'exec_command' }],+ tool_choice: 'auto',+ parallel_tool_calls: false,+ input: 'hello',+ });+ });++ it('resets a Seed tool choice that references a filtered tool', 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: 'bytedance-seed/seed-2.1-pro',+ tools: [+ { type: 'function', name: 'exec_command' },+ { type: 'image_generation' },+ ],+ tool_choice: { type: 'image_generation' },+ parallel_tool_calls: false,+ input: 'hello',+ };+ 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: 'bytedance-seed/seed-2.1-pro',+ tools: [{ type: 'function', name: 'exec_command' }],+ tool_choice: 'auto',+ parallel_tool_calls: false,+ input: 'hello',+ });+ });++ it('keeps a Seed tool choice that references a retained function', 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: 'bytedance-seed/seed-2.1-pro',+ tools: [+ { type: 'function', name: 'exec_command' },+ { type: 'namespace', name: 'multi_agent_v1', tools: [] },+ ],+ tool_choice: { type: 'function', name: 'exec_command' },+ parallel_tool_calls: false,+ input: 'hello',+ };+ 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: 'bytedance-seed/seed-2.1-pro',+ tools: [{ type: 'function', name: 'exec_command' }],+ tool_choice: { type: 'function', name: 'exec_command' },+ parallel_tool_calls: false,+ input: 'hello',+ });+ });+it('keeps Codex namespace tools for non-xAI requests', async () => {const host = await freshCodexProxyHost();const { setSessionProvider, clearSessionProvider } = await import('../session-provider-store.js');@@ -1008,7 +1200,7 @@ describe('codex proxy host', () => {await host.ensureCodexProxyReady();const transforms = mockState.createAnthropicCompatProxy.mock.calls[0]?.[0]?.transformRequest ?? [];- expect(transforms).toHaveLength(9); // encrypted activeStrip, image generation activeStrip, instructions 注入, 跨来源压缩块兼容, xAI Responses 兼容, MiniMax effort 兼容, provider model rewrite, stripNonAnthropicFields, dump+ expect(transforms).toHaveLength(10); // encrypted activeStrip, image generation activeStrip, instructions 注入, 跨来源压缩块兼容, 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 672b74bba3..3ba32dfc52 100644--- a/apps/desktop/src/main/maker-host/codex-proxy-host.ts+++ b/apps/desktop/src/main/maker-host/codex-proxy-host.ts@@ -576,6 +576,110 @@ function sanitizeXaiTools(body: Record<string, unknown>): Record<string, unknownreturn next;}+/**+ * ByteDance Seed accepts standard function tools and web search. Codex also+ * emits namespaced and other built-in descriptors that Volcengine rejects+ * before the request reaches the model. Its web-search descriptor must also+ * be reduced to the standard shape: Codex's `external_web_access` extension+ * is rejected as an unknown field.+ */+function isByteDanceSeedModel(model: unknown): boolean {+ return typeof model === 'string' && model.startsWith('bytedance-seed/');+}++function seedToolChoiceReferencesRemovedTool(+ toolChoice: unknown,+ tools: Record<string, unknown>[],+): boolean {+ if (!isPlainObject(toolChoice) || typeof toolChoice.type !== 'string') return false;++ return !tools.some((tool) => {+ if (tool.type !== toolChoice.type) return false;+ if (toolChoice.type !== 'function') return true;+ return typeof toolChoice.name === 'string' && tool.name === toolChoice.name;+ });+}++function sanitizeByteDanceSeedTools(body: Record<string, unknown>): Record<string, unknown> | null {+ if (!isByteDanceSeedModel(body.model) || !Array.isArray(body.tools)) return null;++ let changed = false;+ const tools: Record<string, unknown>[] = [];+ for (const tool of body.tools) {+ if (!isPlainObject(tool)) {+ changed = true;+ continue;+ }+ if (tool.type === 'function') {+ tools.push(tool);+ continue;+ }+ if (tool.type === 'web_search') {+ // Seed cannot represent Codex's cache-only search policy. Dropping the+ // tool preserves the caller's explicit prohibition on live web access.+ if (tool.external_web_access === false) {+ changed = true;+ continue;+ }+ tools.push({ type: 'web_search' });+ if (Object.keys(tool).length !== 1) changed = true;+ continue;+ }+ changed = true;+ }+ if (!changed) return null;++ const next: Record<string, unknown> = { ...body };+ if (tools.length > 0) {+ next.tools = tools;+ if (seedToolChoiceReferencesRemovedTool(next.tool_choice, tools)) next.tool_choice = 'auto';+ } else {+ delete next.tools;+ delete next.tool_choice;+ delete next.parallel_tool_calls;+ }+ return next;+}++/** Volcengine requires replayed assistant messages to carry their output status. */+function normalizeByteDanceSeedInput(body: Record<string, unknown>): Record<string, unknown> | null {+ if (!isByteDanceSeedModel(body.model) || !Array.isArray(body.input)) return null;++ let changed = false;+ const input = body.input.map((item) => {+ if (+ !isPlainObject(item) ||+ item.type !== 'message' ||+ item.role !== 'assistant' ||+ typeof item.status === 'string'+ ) {+ return item;+ }+ changed = true;+ return { ...item, status: 'completed' };+ });+ return changed ? { ...body, input } : null;+}++function createByteDanceSeedResponsesCompatTransform(): RequestTransform {+ return (body) => {+ if (!isPlainObject(body)) return null;+ let changed = false;+ let current = body;+ const withSanitizedTools = sanitizeByteDanceSeedTools(current);+ if (withSanitizedTools) {+ current = withSanitizedTools;+ changed = true;+ }+ const withNormalizedInput = normalizeByteDanceSeedInput(current);+ if (withNormalizedInput) {+ current = withNormalizedInput;+ changed = true;+ }+ return changed ? current : null;+ };+}+function createXaiResponsesCompatTransform(): RequestTransform {return (body, ctx) => {if (!isPlainObject(body)) return null;@@ -1041,6 +1145,7 @@ function createTransformRequestChain(): RequestTransform[] {// 针对具体供应商的 input 归一化才能按标准 message 处理它。createCrossProviderCompactionCompatTransform(),createXaiResponsesCompatTransform(),+ createByteDanceSeedResponsesCompatTransform(),createMiniMaxResponsesCompatTransform(),createProviderModelRewriteTransform(),stripNonAnthropicFields,