Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
fix(bridge): keep Chat requests working without web search (#2433)
nanacoa649562a91b5
diff --git a/packages/responses-chat-bridge/src/__tests__/handler.test.ts b/packages/responses-chat-bridge/src/__tests__/handler.test.tsindex fabd4cd1f2..e31b76c267 100644--- a/packages/responses-chat-bridge/src/__tests__/handler.test.ts+++ b/packages/responses-chat-bridge/src/__tests__/handler.test.ts@@ -73,7 +73,49 @@ describe('createResponsesChatHandler', () => {expect(res.ended).toBe(true);});- it('rejects a built-in web_search tool instead of silently dropping it', async () => {+ it('drops an unsupported built-in web_search tool and continues upstream', async () => {+ const fetchImpl = vi.fn(async (_url: string | URL | Request, init?: RequestInit) => {+ const body = JSON.parse(String(init?.body));+ expect(body.tools).toEqual([+ { type: 'function', function: { name: 'exec', parameters: { type: 'object' } } },+ ]);+ return streamResponse([+ { id: 'chat_1', choices: [{ delta: { content: 'ok' } }] },+ { id: 'chat_1', choices: [{ delta: {}, finish_reason: 'stop' }] },+ ]);+ }) as typeof fetch;+ const warn = vi.fn();+ const handler = createResponsesChatHandler({+ upstreamBase: 'https://provider.example/v1',+ buildHeaders: async () => ({}),+ }, { fetchImpl, logger: { warn } });+ const res = new FakeResponse();++ await handler.handle({+ parsedBody: {+ model: 'custom-model',+ input: [{ type: 'message', role: 'user', content: 'search' }],+ tools: [+ { type: 'function', name: 'exec', parameters: { type: 'object' } },+ { type: 'web_search' },+ ],+ },+ res: res as never,+ });++ expect(fetchImpl).toHaveBeenCalledTimes(1);+ expect(res.status).toBe(200);+ expect(res.chunks.join('')).toContain('ok');+ expect(res.ended).toBe(true);+ expect(warn).toHaveBeenCalledWith('responses-chat bridge dropped unsupported built-in tool', {+ model: 'custom-model',+ tool: 'web_search',+ index: 1,+ action: 'continue_without_tool',+ });+ });++ it('rejects an explicit tool_choice for a dropped web_search tool', async () => {const fetchImpl = vi.fn() as unknown as typeof fetch;const warn = vi.fn();const handler = createResponsesChatHandler({@@ -87,20 +129,184 @@ describe('createResponsesChatHandler', () => {model: 'custom-model',input: [{ type: 'message', role: 'user', content: 'search' }],tools: [{ type: 'web_search' }],+ tool_choice: { type: 'function', name: 'web_search' },},res: res as never,});expect(res.status).toBe(400);expect(res.chunks.join('')).toContain('unsupported_feature');- expect(res.chunks.join('')).toContain('tools[0]');- expect(res.chunks.join('')).toContain('web_search');+ expect(res.chunks.join('')).toContain('tool_choice.web_search');expect(fetchImpl).not.toHaveBeenCalled();- expect(warn).toHaveBeenCalledTimes(1);expect(warn).toHaveBeenCalledWith('responses-chat bridge rejected unsupported feature', {model: 'custom-model',- feature: 'tools[0].web_search',+ feature: 'tool_choice.web_search',+ });+ });++ it('rejects a required tool_choice when the only tool is a dropped web_search', async () => {+ const fetchImpl = vi.fn() as unknown as typeof fetch;+ const warn = vi.fn();+ const handler = createResponsesChatHandler({+ upstreamBase: 'https://provider.example/v1',+ buildHeaders: async () => ({}),+ }, { fetchImpl, logger: { warn } });+ const res = new FakeResponse();++ await handler.handle({+ parsedBody: {+ model: 'custom-model',+ input: [{ type: 'message', role: 'user', content: 'search' }],+ tools: [{ type: 'web_search' }],+ tool_choice: 'required',+ },+ res: res as never,+ });++ expect(res.status).toBe(400);+ expect(res.chunks.join('')).toContain('unsupported_feature');+ expect(res.chunks.join('')).toContain('tool_choice.web_search');+ expect(fetchImpl).not.toHaveBeenCalled();+ });++ it('keeps a required tool_choice when another tool survives beside web_search', async () => {+ const fetchImpl = vi.fn(async (_url: string | URL | Request, init?: RequestInit) => {+ const body = JSON.parse(String(init?.body));+ expect(body.tools).toEqual([+ { type: 'function', function: { name: 'exec', parameters: { type: 'object' } } },+ ]);+ expect(body.tool_choice).toBe('required');+ return streamResponse([+ { id: 'chat_1', choices: [{ delta: {}, finish_reason: 'stop' }] },+ ]);+ }) as typeof fetch;+ const handler = createResponsesChatHandler({+ upstreamBase: 'https://provider.example/v1',+ buildHeaders: async () => ({}),+ }, { fetchImpl });+ const res = new FakeResponse();++ await handler.handle({+ parsedBody: {+ model: 'custom-model',+ input: [{ type: 'message', role: 'user', content: 'do it' }],+ tools: [+ { type: 'function', name: 'exec', parameters: { type: 'object' } },+ { type: 'web_search' },+ ],+ tool_choice: 'required',+ },+ res: res as never,});++ expect(fetchImpl).toHaveBeenCalledTimes(1);+ expect(res.status).toBe(200);+ });++ it('keeps a same-named retained function tool selectable beside web_search', async () => {+ const fetchImpl = vi.fn(async (_url: string | URL | Request, init?: RequestInit) => {+ const body = JSON.parse(String(init?.body));+ expect(body.tools).toEqual([+ { type: 'function', function: { name: 'web_search', parameters: { type: 'object' } } },+ ]);+ expect(body.tool_choice).toEqual({+ type: 'function',+ function: { name: 'web_search' },+ });+ return streamResponse([+ { id: 'chat_1', choices: [{ delta: {}, finish_reason: 'stop' }] },+ ]);+ }) as typeof fetch;+ const handler = createResponsesChatHandler({+ upstreamBase: 'https://provider.example/v1',+ buildHeaders: async () => ({}),+ }, { fetchImpl });+ const res = new FakeResponse();++ await handler.handle({+ parsedBody: {+ model: 'custom-model',+ input: [{ type: 'message', role: 'user', content: 'call the function' }],+ tools: [+ { type: 'function', name: 'web_search', parameters: { type: 'object' } },+ { type: 'web_search' },+ ],+ tool_choice: { type: 'function', name: 'web_search' },+ },+ res: res as never,+ });++ expect(fetchImpl).toHaveBeenCalledTimes(1);+ expect(res.status).toBe(200);+ });++ it('keeps a same-named string custom tool selectable beside web_search', async () => {+ const fetchImpl = vi.fn(async (_url: string | URL | Request, init?: RequestInit) => {+ const body = JSON.parse(String(init?.body));+ expect(body.tools?.map((tool: { function: { name: string } }) => tool.function.name))+ .toContain('web_search');+ expect(body.tool_choice).toEqual({+ type: 'function',+ function: { name: 'web_search' },+ });+ return streamResponse([+ { id: 'chat_1', choices: [{ delta: {}, finish_reason: 'stop' }] },+ ]);+ }) as typeof fetch;+ const handler = createResponsesChatHandler({+ upstreamBase: 'https://provider.example/v1',+ buildHeaders: async () => ({}),+ }, { fetchImpl });+ const res = new FakeResponse();++ await handler.handle({+ parsedBody: {+ model: 'custom-model',+ input: [{ type: 'message', role: 'user', content: 'call the custom tool' }],+ tools: ['web_search', { type: 'web_search' }],+ tool_choice: { type: 'custom', name: 'web_search' },+ },+ res: res as never,+ });++ expect(fetchImpl).toHaveBeenCalledTimes(1);+ expect(res.status).toBe(200);+ });++ it('keeps a nested same-named function selectable beside web_search', async () => {+ const fetchImpl = vi.fn(async (_url: string | URL | Request, init?: RequestInit) => {+ const body = JSON.parse(String(init?.body));+ expect(body.tools?.map((tool: { function: { name: string } }) => tool.function.name))+ .toContain('web_search');+ expect(body.tool_choice).toEqual({+ type: 'function',+ function: { name: 'web_search' },+ });+ return streamResponse([+ { id: 'chat_1', choices: [{ delta: {}, finish_reason: 'stop' }] },+ ]);+ }) as typeof fetch;+ const handler = createResponsesChatHandler({+ upstreamBase: 'https://provider.example/v1',+ buildHeaders: async () => ({}),+ }, { fetchImpl });+ const res = new FakeResponse();++ await handler.handle({+ parsedBody: {+ model: 'custom-model',+ input: [{ type: 'message', role: 'user', content: 'call the nested function' }],+ tools: [+ { type: 'function', function: { name: 'web_search', parameters: { type: 'object' } } },+ { type: 'web_search' },+ ],+ tool_choice: { type: 'function', name: 'web_search' },+ },+ res: res as never,+ });++ expect(fetchImpl).toHaveBeenCalledTimes(1);+ expect(res.status).toBe(200);});it('preserves the upstream base query when applying the chat path', async () => {diff --git a/packages/responses-chat-bridge/src/handler.ts b/packages/responses-chat-bridge/src/handler.tsindex 7556a1975b..f1146063d6 100644--- a/packages/responses-chat-bridge/src/handler.ts+++ b/packages/responses-chat-bridge/src/handler.ts@@ -166,7 +166,13 @@ export function createResponsesChatHandler(capabilities: provider.capabilities,onDroppedTool: (type, index) => {if (type === 'web_search') {- throw new UnsupportedResponsesFeatureError(`tools[${index}].web_search`);+ log.warn?.('responses-chat bridge dropped unsupported built-in tool', {+ model: request.model,+ tool: type,+ index,+ action: 'continue_without_tool',+ });+ return;}log.warn?.('responses-chat bridge dropped non-function tool', { type, index });},diff --git a/packages/responses-chat-bridge/src/translate-request.ts b/packages/responses-chat-bridge/src/translate-request.tsindex 759fc330bf..530bee9987 100644--- a/packages/responses-chat-bridge/src/translate-request.ts+++ b/packages/responses-chat-bridge/src/translate-request.ts@@ -692,6 +692,55 @@ function reportDroppedTools(});}+function hasDroppedWebSearchTool(tools: ResponsesRequest['tools']): boolean {+ return (tools ?? []).some((tool) => (+ isPlainObject(tool) && tool.type === 'web_search'+ ));+}++function hasRetainedTool(+ tools: ResponsesRequest['tools'],+ kind: 'function' | 'custom',+ name: string,+ namespace?: string,+): boolean {+ for (const tool of tools ?? []) {+ if (typeof tool === 'string') {+ if (kind === 'custom' && namespace === undefined && tool === name) return true;+ continue;+ }+ if (!isPlainObject(tool)) continue;+ const nested = isPlainObject(tool.function) ? tool.function : undefined;+ const toolName = typeof tool.name === 'string'+ ? tool.name+ : typeof nested?.name === 'string'+ ? nested.name+ : undefined;+ if (tool.type === kind && toolName === name && namespace === undefined) return true;+ if (tool.type === 'namespace' && tool.name === namespace) {+ const children = Array.isArray(tool.tools) ? tool.tools : tool.children;+ if (Array.isArray(children) && children.some((child) => (+ isPlainObject(child) && child.type === kind && child.name === name+ ))) return true;+ }+ }+ return false;+}++function explicitlySelectsDroppedWebSearch(+ tools: ResponsesRequest['tools'],+ choice: unknown,+): boolean {+ if (!isPlainObject(choice)) return false;+ if (choice.type === 'web_search') return true;+ if (choice.type !== 'function' && choice.type !== 'custom') return false;+ const nestedFunction = isPlainObject(choice.function) ? choice.function : undefined;+ const name = typeof choice.name === 'string' ? choice.name : nestedFunction?.name;+ if (name !== 'web_search') return false;+ const namespace = typeof choice.namespace === 'string' ? choice.namespace : undefined;+ return !hasRetainedTool(tools, choice.type, name, namespace);+}+function translateToolChoice(choice: unknown,forceAuto: boolean,@@ -806,6 +855,23 @@ export function translateResponsesRequestWithContext(const developerRole = capabilities.developerRole ?? 'system';const toolContext = ChatBridgeToolContext.fromRequest(input);reportDroppedTools(input.tools, opts.onDroppedTool);+ if (hasDroppedWebSearchTool(input.tools)) {+ // Ordinary/auto requests keep degrading (web_search removed, other tools preserved). Stay+ // fail-closed only when the request can be satisfied *solely* by the dropped web_search:+ // either it explicitly selects web_search, or it demands `required` while every convertible+ // tool was dropped, leaving no tool the upstream could call. Otherwise `required` is silently+ // relaxed and a search-less provider answers as though no tool were requested.+ const requiresDroppedWebSearch =+ explicitlySelectsDroppedWebSearch(input.tools, input.tool_choice)+ || (+ input.tool_choice === 'required'+ && capabilities.forceAutoToolChoice !== true+ && toolContext.chatTools === undefined+ );+ if (requiresDroppedWebSearch) {+ throw new UnsupportedResponsesFeatureError('tool_choice.web_search');+ }+ }const messages = translateInput(input.input, {developerRole,mediaCapabilities: capabilities,