Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

fix(chat-bridge): 兼容已完成网页搜索的历史以恢复任务 (#5273)

lengjingxu776c93f2d140
diff --git a/packages/responses-chat-bridge/src/__tests__/handler.test.ts b/packages/responses-chat-bridge/src/__tests__/handler.test.ts
index 78a348d2e1..d7d3399507 100644
--- a/packages/responses-chat-bridge/src/__tests__/handler.test.ts
+++ b/packages/responses-chat-bridge/src/__tests__/handler.test.ts
@@ -532,6 +532,38 @@ describe('createResponsesChatHandler', () => {
expect(res.ended).toBe(true);
});
+ it('resumes completed web search history with one upstream request', async () => {
+ const action = { type: 'search', queries: ['image editor', 'canvas export'] };
+ const fetchImpl = vi.fn(async (_url: string | URL | Request, init?: RequestInit) => {
+ expect(JSON.parse(String(init?.body)).messages).toEqual([
+ { role: 'assistant', content: '[completed web search]\n' + JSON.stringify(action) },
+ { role: 'assistant', content: 'Findings: https://example.com/docs' },
+ { role: 'user', content: 'Continue' },
+ ]);
+ return streamResponse([{ choices: [{ delta: { content: 'Continuing' }, finish_reason: 'stop' }] }]);
+ });
+ const handler = createResponsesChatHandler({
+ upstreamBase: 'https://provider.example/v1', buildHeaders: async () => ({}),
+ }, { fetchImpl });
+ const res = new FakeResponse();
+ await handler.handle({
+ parsedBody: {
+ model: 'test-model',
+ input: [
+ { type: 'web_search_call', id: 'ws_history', status: 'completed', action },
+ { type: 'message', role: 'assistant', content: 'Findings: https://example.com/docs' },
+ { type: 'message', role: 'user', content: 'Continue' },
+ ],
+ },
+ res: res as never,
+ });
+
+ expect(fetchImpl).toHaveBeenCalledOnce();
+ expect(res.status).toBe(200);
+ expect(res.chunks.join('')).toContain('event: response.completed');
+ expect(res.chunks.join('')).toContain('Continuing');
+ });
+
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));
diff --git a/packages/responses-chat-bridge/src/__tests__/translate-request.test.ts b/packages/responses-chat-bridge/src/__tests__/translate-request.test.ts
index 9783548dd2..7f4d16acfe 100644
--- a/packages/responses-chat-bridge/src/__tests__/translate-request.test.ts
+++ b/packages/responses-chat-bridge/src/__tests__/translate-request.test.ts
@@ -439,6 +439,108 @@ describe('translateResponsesRequest', () => {
}))).toThrowError(UnsupportedResponsesFeatureError);
});
+ it.each([
+ { type: 'search', query: 'image editor', queries: ['image editor', 'canvas export'], sources: [{ url: 'https://example.com/docs' }] },
+ { type: 'open_page', url: 'https://example.com/docs' },
+ { type: 'find_in_page', url: 'https://example.com/docs', pattern: 'export' },
+ ])('preserves completed web search $type history as assistant text', (action) => {
+ const input: ResponsesRequest = base({
+ input: [
+ { type: 'message', role: 'user', content: 'Research image editors' },
+ { type: 'web_search_call', id: 'ws_history', status: 'completed', action },
+ { type: 'message', role: 'assistant', content: 'Findings: https://example.com/docs' },
+ { type: 'message', role: 'user', content: 'Continue' },
+ ],
+ });
+ const original = JSON.stringify(input);
+ const out = translateResponsesRequest(input);
+
+ expect(out.messages).toEqual([
+ { role: 'user', content: 'Research image editors' },
+ { role: 'assistant', content: '[completed web search]\n' + JSON.stringify(action) },
+ { role: 'assistant', content: 'Findings: https://example.com/docs' },
+ { role: 'user', content: 'Continue' },
+ ]);
+ expect(JSON.stringify(input)).toBe(original);
+ expect(out.tools).toBeUndefined();
+ });
+
+ it.each([
+ { status: 'in_progress', action: { type: 'search', query: 'image editor' } },
+ { status: 'searching', action: { type: 'search', query: 'image editor' } },
+ { status: 'failed', action: { type: 'search', query: 'image editor' } },
+ { action: { type: 'search', query: 'image editor' } },
+ { status: 'completed' },
+ { status: 'completed', action: 'image editor' },
+ { status: 'completed', action: {} },
+ ])('rejects unfinished or malformed web search history: %j', (item) => {
+ expect(() => translateResponsesRequest(base({
+ input: [
+ { type: 'web_search_call', ...item },
+ { type: 'message', role: 'user', content: 'Continue' },
+ ],
+ }))).toThrow("input item 'web_search_call'");
+ });
+
+ it('keeps completed web search history after a pending function result', () => {
+ const action = { type: 'search', query: 'image editor' };
+ const out = translateResponsesRequest(base({
+ input: [
+ { type: 'function_call', call_id: 'call_1', name: 'shell', arguments: '{}' },
+ { type: 'web_search_call', id: 'ws_history', status: 'completed', action },
+ { type: 'function_call_output', call_id: 'call_1', output: 'done' },
+ { type: 'message', role: 'user', content: 'Continue' },
+ ],
+ }));
+
+ expect(out.messages).toEqual([
+ {
+ role: 'assistant', content: null,
+ tool_calls: [{ id: 'call_1', type: 'function', function: { name: 'shell', arguments: '{}' } }],
+ },
+ { role: 'tool', tool_call_id: 'call_1', content: 'done' },
+ { role: 'assistant', content: '[completed web search]\n' + JSON.stringify(action) },
+ { role: 'user', content: 'Continue' },
+ ]);
+ });
+
+ it.each([
+ { outputIds: ['call_1'] },
+ { outputIds: ['call_1', 'call_2'] },
+ { outputIds: ['call_2', 'call_1'] },
+ ])('preserves real results when web search interleaves calls: $outputIds', ({ outputIds }) => {
+ const action = { type: 'search', query: 'image editor' };
+ const out = translateResponsesRequest(base({
+ input: [
+ { type: 'function_call', call_id: 'call_1', name: 'shell', arguments: '{}' },
+ { type: 'web_search_call', id: 'ws_history', status: 'completed', action },
+ { type: 'function_call', call_id: 'call_2', name: 'shell', arguments: '{}' },
+ ...outputIds.map((callId) => ({
+ type: 'function_call_output', call_id: callId, output: 'result for ' + callId,
+ })),
+ { type: 'message', role: 'user', content: 'Continue' },
+ ],
+ }));
+
+ expect(out.messages[0]).toEqual({
+ role: 'assistant', content: null,
+ tool_calls: ['call_1', 'call_2'].map((callId) => ({
+ id: callId, type: 'function', function: { name: 'shell', arguments: '{}' },
+ })),
+ });
+ const results = out.messages.filter((message) => message.role === 'tool');
+ for (const callId of outputIds) {
+ expect(results.find((message) => message.tool_call_id === callId)?.content)
+ .toBe('result for ' + callId);
+ }
+ expect(out.messages.slice(-2)).toEqual([
+ { role: 'assistant', content: '[completed web search]\n' + JSON.stringify(action) },
+ { role: 'user', content: 'Continue' },
+ ]);
+ expect(out.messages.map((message) => message.role))
+ .toEqual(['assistant', 'tool', 'tool', 'assistant', 'user']);
+ });
+
it('converts replayed agent messages to assistant text', () => {
const out = translateResponsesRequest(base({
input: [
diff --git a/packages/responses-chat-bridge/src/translate-request.ts b/packages/responses-chat-bridge/src/translate-request.ts
index be0448ec15..5b9c181739 100644
--- a/packages/responses-chat-bridge/src/translate-request.ts
+++ b/packages/responses-chat-bridge/src/translate-request.ts
@@ -654,6 +654,25 @@ function translateInput(input: ResponsesRequest['input'], opts: TranslateInputOp
continue;
}
+ if (item.type === 'web_search_call') {
+ if (
+ record.status !== 'completed'
+ || !isPlainObject(record.action)
+ || typeof record.action.type !== 'string'
+ || !record.action.type.trim()
+ ) throw new UnsupportedResponsesFeatureError("input item 'web_search_call'");
+ const message: ChatMessage = {
+ role: 'assistant',
+ content: '[completed web search]\n' + JSON.stringify(record.action),
+ };
+ if ((assistant?.tool_calls?.length ?? 0) > 0 || pendingToolCalls.length > 0) {
+ deferredBarriers.push(message);
+ } else {
+ pushBarrier(message);
+ }
+ continue;
+ }
+
if (item.type === 'function_call' || item.type === 'custom_tool_call' || item.type === 'tool_search_call') {
if (!assistant && pendingToolCalls.length > 0) closeUnresolvedToolRound();
const callId = typeof record.call_id === 'string' && record.call_id