Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
fix(responses-chat-bridge): support agent message history (#1671)
天魔939e02bc9dad
diff --git a/packages/responses-chat-bridge/src/__tests__/translate-request.test.ts b/packages/responses-chat-bridge/src/__tests__/translate-request.test.tsindex 2f3040155f..1e47d93e31 100644--- a/packages/responses-chat-bridge/src/__tests__/translate-request.test.ts+++ b/packages/responses-chat-bridge/src/__tests__/translate-request.test.ts@@ -349,6 +349,89 @@ describe('translateResponsesRequest', () => {}))).toThrowError(UnsupportedResponsesFeatureError);});+ it('converts replayed agent messages to assistant text', () => {+ const out = translateResponsesRequest(base({+ input: [+ { type: 'message', role: 'user', content: 'start' },+ {+ type: 'agent_message',+ author: 'researcher\r\nreviewer',+ content: [+ { type: 'output_text', text: ' Findings' },+ { type: 'encrypted_content', encrypted_content: 'opaque' },+ ],+ },+ {+ type: 'agent_message',+ author: 'reviewer',+ content: [{ type: 'encrypted_content', encrypted_content: 'opaque' }],+ },+ {+ type: 'agent_message',+ author: 'observer',+ content: [{ type: 'output_text', text: ' ' }],+ },+ { type: 'message', role: 'assistant', content: 'Final answer' },+ { type: 'message', role: 'user', content: 'finish' },+ ],+ }));++ expect(out.messages).toEqual([+ { role: 'user', content: 'start' },+ {+ role: 'assistant',+ content: [+ '[collab researcher reviewer]\n Findings',+ '[collab message from reviewer; encrypted payload omitted]',+ '[collab message from observer; empty content]',+ ].join('\n'),+ },+ { role: 'assistant', content: 'Final answer' },+ { role: 'user', content: 'finish' },+ ]);+ });++ it.each(['input_text', 'output_text', 'text'])(+ 'reports a malformed agent message %s part by path',+ (type) => {+ expect(() => translateResponsesRequest(base({+ input: [{ type: 'agent_message', content: [{ type }] }],+ }))).toThrow(`input[0].content.${type}`);+ },+ );++ it('reports an unknown agent message part by path', () => {+ expect(() => translateResponsesRequest(base({+ input: [{ type: 'agent_message', content: [{ type: 'image_url' }] }],+ }))).toThrow('input[0].content.image_url');+ });++ it('keeps agent messages after the preceding tool result', () => {+ const out = translateResponsesRequest(base({+ input: [+ { type: 'function_call', call_id: 'call_1', name: 'shell', arguments: '{}' },+ {+ type: 'agent_message',+ author: 'researcher',+ content: [{ type: 'output_text', text: 'Findings' }],+ },+ { type: 'function_call_output', call_id: 'call_1', output: 'done' },+ ],+ }));++ 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: '[collab researcher]\nFindings' },+ ]);+ });+it('round-trips replayed Codex tool_search items without breaking tool-call merging', () => {const out = translateResponsesRequest(base({input: [diff --git a/packages/responses-chat-bridge/src/translate-request.ts b/packages/responses-chat-bridge/src/translate-request.tsindex 3031ec4188..759fc330bf 100644--- a/packages/responses-chat-bridge/src/translate-request.ts+++ b/packages/responses-chat-bridge/src/translate-request.ts@@ -299,6 +299,45 @@ function reasoningText(value: unknown): string {return '';}+function agentMessageText(item: Record<string, unknown>, itemIndex: number): string {+ const normalizedAuthor = typeof item.author === 'string'+ ? item.author.replace(/\s*[\r\n]+\s*/g, ' ').trim()+ : '';+ const author = normalizedAuthor || 'agent';+ let body = '';+ let omittedEncryptedContent = false;+ if (typeof item.content === 'string') {+ body = item.content;+ } else if (Array.isArray(item.content)) {+ const parts: string[] = [];+ for (const part of item.content) {+ if (!isPlainObject(part) || typeof part.type !== 'string') {+ throw new UnsupportedResponsesFeatureError(`input[${itemIndex}].content`);+ }+ if (part.type === 'encrypted_content') {+ omittedEncryptedContent = true;+ continue;+ }+ if (part.type === 'input_text' || part.type === 'output_text' || part.type === 'text') {+ if (typeof part.text !== 'string') {+ throw new UnsupportedResponsesFeatureError(`input[${itemIndex}].content.${part.type}`);+ }+ parts.push(part.text);+ continue;+ }+ throw new UnsupportedResponsesFeatureError(`input[${itemIndex}].content.${part.type}`);+ }+ body = parts.join('\n');+ } else {+ throw new UnsupportedResponsesFeatureError(`input[${itemIndex}].content`);+ }+ return body.trim()+ ? `[collab ${author}]\n${body}`+ : omittedEncryptedContent+ ? `[collab message from ${author}; encrypted payload omitted]`+ : `[collab message from ${author}; empty content]`;+}+interface TranslateInputOptions {developerRole: ChatDeveloperRole;mediaCapabilities: ChatMediaCapabilities;@@ -555,6 +594,19 @@ function translateInput(input: ResponsesRequest['input'], opts: TranslateInputOpcontinue;}+ if (item.type === 'agent_message') {+ const content = agentMessageText(record, index);+ if ((assistant?.tool_calls?.length ?? 0) > 0 || pendingToolCalls.length > 0) {+ pushBarrier({ role: 'assistant', content });+ } else {+ assistant ??= { role: 'assistant', content: null };+ assistant.content = assistant.content ? `${assistant.content}\n${content}` : content;+ const nextItem = input[index + 1];+ if (!isPlainObject(nextItem) || nextItem.type !== 'agent_message') flushAssistant();+ }+ continue;+ }+if (item.type === 'reasoning') {if (opts.reasoningHistoryField === 'reasoning_content') {pendingReasoning += reasoningText(item);