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(anthropic-bridge): replay collab agent messages (#2279)

许君山 Junshan4647a3ac9823
diff --git a/packages/responses-anthropic-bridge/src/__tests__/translate-request.test.ts b/packages/responses-anthropic-bridge/src/__tests__/translate-request.test.ts
index 33a6836801..7c783305b3 100644
--- a/packages/responses-anthropic-bridge/src/__tests__/translate-request.test.ts
+++ b/packages/responses-anthropic-bridge/src/__tests__/translate-request.test.ts
@@ -56,6 +56,104 @@ describe('Responses → Anthropic request translation', () => {
})).toThrowError('instructions[0].input_image');
});
+ it('replays collab agent messages as Anthropic assistant text', () => {
+ const result = translateResponsesRequest({
+ model: 'claude',
+ input: [
+ { role: 'user', content: 'delegate this' },
+ {
+ type: 'agent_message',
+ author: '/root/\r\n researcher',
+ content: [
+ { type: 'input_text', text: 'first finding' },
+ { type: 'encrypted_content', data: 'provider-secret' },
+ { type: 'output_text', text: 'second finding' },
+ ],
+ },
+ {
+ role: 'user',
+ content: [{ type: 'input_image', image_url: 'data:image/png;base64,abc' }],
+ },
+ ],
+ });
+
+ expect(result.request.messages[1]).toEqual({
+ role: 'assistant',
+ content: [{
+ type: 'text',
+ text: '[collab /root/ researcher]\nfirst finding\nsecond finding',
+ }],
+ });
+ expect(result.request.messages[2].content).toEqual([{
+ type: 'image',
+ source: { type: 'base64', media_type: 'image/png', data: 'abc' },
+ }]);
+ expect(JSON.stringify(result.request)).not.toContain('agent_message');
+ expect(JSON.stringify(result.request)).not.toContain('encrypted_content');
+ expect(JSON.stringify(result.request)).not.toContain('provider-secret');
+ });
+
+ it('keeps collab string, empty, and encrypted-only history distinguishable', () => {
+ const result = translateResponsesRequest({
+ model: 'claude',
+ input: [
+ { role: 'user', content: 'delegate this' },
+ { type: 'agent_message', content: 'plain result' },
+ { type: 'agent_message', author: '', content: [] },
+ {
+ type: 'agent_message',
+ author: 'worker',
+ content: [{ type: 'encrypted_content', data: 'opaque' }],
+ },
+ { role: 'user', content: 'continue' },
+ ],
+ });
+
+ expect(result.request.messages[1].content).toEqual([
+ { type: 'text', text: '[collab agent]\nplain result' },
+ { type: 'text', text: '[collab message from agent; empty content]' },
+ { type: 'text', text: '[collab message from worker; encrypted payload omitted]' },
+ ]);
+ });
+
+ it('rejects malformed collab content with its input path', () => {
+ expect(() => translateResponsesRequest({
+ model: 'claude',
+ input: [{ type: 'agent_message', content: [{ type: 'image_url' }] }],
+ })).toThrowError('input[0].content.image_url');
+
+ expect(() => translateResponsesRequest({
+ model: 'claude',
+ input: [{ type: 'agent_message', content: [{ type: 'text' }] }],
+ })).toThrowError('input[0].content.text');
+
+ expect(() => translateResponsesRequest({
+ model: 'claude',
+ input: [{ type: 'agent_message', content: null }],
+ })).toThrowError('input[0].content');
+ });
+
+ it('keeps collab text inside a valid tool-use round', () => {
+ const result = translateResponsesRequest({
+ model: 'claude',
+ input: [
+ { role: 'user', content: 'run it' },
+ { type: 'function_call', call_id: 'c1', name: 'run', arguments: '{}' },
+ { type: 'agent_message', author: 'worker', content: 'working result' },
+ { type: 'function_call_output', call_id: 'c1', output: 'ok' },
+ ],
+ tools: [{ type: 'function', name: 'run', parameters: { type: 'object' } }],
+ });
+
+ expect(result.request.messages[1].content).toEqual([
+ expect.objectContaining({ type: 'tool_use', id: 'c1', name: 'run' }),
+ { type: 'text', text: '[collab worker]\nworking result' },
+ ]);
+ expect(result.request.messages[2].content).toEqual([
+ expect.objectContaining({ type: 'tool_result', tool_use_id: 'c1' }),
+ ]);
+ });
+
it('rejects structured-output constraints instead of silently dropping them', () => {
expect(() => translateResponsesRequest({
model: 'claude',
diff --git a/packages/responses-anthropic-bridge/src/translate-request.ts b/packages/responses-anthropic-bridge/src/translate-request.ts
index c23e949c48..614118265a 100644
--- a/packages/responses-anthropic-bridge/src/translate-request.ts
+++ b/packages/responses-anthropic-bridge/src/translate-request.ts
@@ -97,6 +97,45 @@ function instructionsText(value: ResponsesRequest['instructions']): string {
}).join('');
}
+function agentMessageText(item: JsonObject, 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 (!isObject(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]`;
+}
+
function parseDataUrl(value: string): { mediaType: string; data: string } | null {
const match = /^data:([^;,]+)(?:;[^,]*)*;base64,([\s\S]+)$/i.exec(value);
if (!match) return null;
@@ -1112,7 +1151,8 @@ export function translateResponsesRequest(
assistant = null;
};
- for (const rawItem of items) {
+ for (let itemIndex = 0; itemIndex < items.length; itemIndex += 1) {
+ const rawItem = items[itemIndex];
if (!isObject(rawItem)) throw new UnsupportedResponsesFeatureError('input item');
const type = stringValue(rawItem.type);
if (type === 'reasoning') {
@@ -1168,6 +1208,11 @@ export function translateResponsesRequest(
});
continue;
}
+ if (type === 'agent_message') {
+ assistant ??= { role: 'assistant', content: [] };
+ assistant.content.push(textPart(agentMessageText(rawItem, itemIndex)));
+ continue;
+ }
if (type === 'input_text' || type === 'input_image' || type === 'input_file') {
flushAssistant();
appendMessage(messages, 'user', inputContentToAnthropic([rawItem], 'user'));