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(responses-chat-bridge): 工具输出中的不支持媒体降级为占位文本

ficowang5032db093543
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 1e47d93e31..601dd2da00 100644
--- a/packages/responses-chat-bridge/src/__tests__/translate-request.test.ts
+++ b/packages/responses-chat-bridge/src/__tests__/translate-request.test.ts
@@ -136,6 +136,73 @@ describe('translateResponsesRequest', () => {
]);
});
+ it('degrades untranslatable tool-output media to a placeholder instead of failing the request (#2805)', () => {
+ // 上下文压缩 / 历史重放会把此前被拦截的图片以 input_image 重新带进工具
+ // 输出:纯文本路由(无 imageInput 能力)此前 fail-closed 拒绝整单,会话
+ // 从此每轮都被拒 —— 应降级为占位文本,保会话可用。
+ const out = translateResponsesRequest(base({
+ input: [
+ { type: 'function_call', call_id: 'call_img', name: 'ReadImage', arguments: '{}' },
+ {
+ type: 'function_call_output',
+ call_id: 'call_img',
+ output: [
+ { type: 'input_text', text: 'here is the screenshot' },
+ { type: 'input_image', image_url: 'data:image/png;base64,QUJD' },
+ ],
+ },
+ { type: 'message', role: 'user', content: 'continue after compaction' },
+ ],
+ }));
+ const tool = out.messages.find((m) => m.role === 'tool') as { content: string };
+ expect(tool.content).toContain('here is the screenshot');
+ expect(tool.content).toContain('[media omitted');
+ expect(tool.content).not.toContain('base64');
+ expect(out.messages.at(-1)).toEqual({ role: 'user', content: 'continue after compaction' });
+ });
+
+ it('still moves translatable tool-output media to a user message on image routes', () => {
+ // 对照组:路由支持图片时维持既有「搬运到后续 user 消息」语义。
+ const out = translateResponsesRequest(base({
+ input: [
+ { type: 'function_call', call_id: 'call_img', name: 'ReadImage', arguments: '{}' },
+ {
+ type: 'function_call_output',
+ call_id: 'call_img',
+ output: [{ type: 'input_image', image_url: 'https://img.example/a.png' }],
+ },
+ { type: 'message', role: 'user', content: 'go on' },
+ ],
+ }), { capabilities: { imageInput: 'image_url' } });
+ const tool = out.messages.find((m) => m.role === 'tool') as { content: string };
+ expect(tool.content).toContain('[media moved to the following user message]');
+ const mediaMessage = out.messages.find(
+ (m) => m.role === 'user' && Array.isArray((m as { content: unknown }).content),
+ ) as { content: Array<{ type: string }> };
+ expect(mediaMessage.content.some((part) => part.type === 'image_url')).toBe(true);
+ });
+
+ it('degrades untranslatable audio and file tool-output media the same way', () => {
+ const out = translateResponsesRequest(base({
+ input: [
+ { type: 'function_call', call_id: 'call_m', name: 'Fetch', arguments: '{}' },
+ {
+ type: 'function_call_output',
+ call_id: 'call_m',
+ output: [
+ { type: 'input_audio', input_audio: { data: 'QUJD', format: 'wav' } },
+ { type: 'input_file', file_id: 'file_123' },
+ ],
+ },
+ { type: 'message', role: 'user', content: 'continue' },
+ ],
+ }));
+ const tool = out.messages.find((m) => m.role === 'tool') as { content: string };
+ expect(tool.content).toContain('[media omitted');
+ expect(tool.content).not.toContain('QUJD');
+ expect(tool.content).not.toContain('file_123');
+ });
+
it('injects a reasoning_content placeholder on tool-call assistant messages for thinking models', () => {
const out = translateResponsesRequest(base({
input: [
@@ -1172,18 +1239,26 @@ describe('translateResponsesRequest', () => {
}), { capabilities: { imageInput: 'image_url' } })).toThrow('input_image.file_id');
});
- it('rejects unsupported media inside tool results instead of serializing it as text', () => {
+ it('replaces unsupported media inside tool results with a placeholder instead of rejecting (#2805)', () => {
+ // 行为变更(#2805):此前对工具结果里的不支持媒体 fail-closed 抛错,
+ // 压缩 / 重放历史带回的媒体会让会话每轮被拒、彻底卡死 —— 改为占位
+ // 降级,且不把原始媒体数据序列化进文本。
for (const part of [
{ type: 'input_image', image_url: 'https://example.com/image.png' },
{ type: 'input_file', file_data: 'BASE64' },
{ type: 'input_audio', input_audio: { data: 'BASE64', format: 'wav' } },
]) {
- expect(() => translateResponsesRequest(base({
+ const out = translateResponsesRequest(base({
input: [
{ type: 'function_call', call_id: 'c1', name: 'inspect', arguments: '{}' },
{ type: 'function_call_output', call_id: 'c1', output: [part] },
+ { type: 'message', role: 'user', content: 'continue' },
],
- }))).toThrow(part.type);
+ }));
+ const tool = out.messages.find((m) => m.role === 'tool') as { content: string };
+ expect(tool.content).toContain('[media omitted');
+ expect(tool.content).not.toContain('BASE64');
+ expect(tool.content).not.toContain('example.com');
}
});
diff --git a/packages/responses-chat-bridge/src/translate-request.ts b/packages/responses-chat-bridge/src/translate-request.ts
index 530bee9987..98826ffab7 100644
--- a/packages/responses-chat-bridge/src/translate-request.ts
+++ b/packages/responses-chat-bridge/src/translate-request.ts
@@ -21,6 +21,8 @@ import { ChatBridgeToolContext } from './tool-context.js';
const TOOL_CALL_REASONING_PLACEHOLDER = 'tool call';
const GOOGLE_THOUGHT_SIGNATURE_PLACEHOLDER = 'skip_thought_signature_validator';
const TOOL_RESULT_MEDIA_MOVED_MARKER = '[media moved to the following user message]';
+const TOOL_RESULT_MEDIA_OMITTED_MARKER =
+ '[media omitted: the current model route does not accept this content type]';
interface ChatMediaCapabilities {
imageInput?: ChatImageInput;
@@ -118,30 +120,20 @@ function hasAudioSource(part: Record<string, unknown>): boolean {
function mediaPart(
part: Record<string, unknown>,
capabilities: ChatMediaCapabilities,
- failClosed = false,
): ChatUserContentPart | undefined {
if (isResponsesImageContentPartType(part.type)) {
if (!hasImageSource(part)) return undefined;
- if (capabilities.imageInput !== 'image_url') {
- if (failClosed) throw new UnsupportedResponsesFeatureError(String(part.type));
- return undefined;
- }
+ if (capabilities.imageInput !== 'image_url') return undefined;
return imagePart(part);
}
if (part.type === 'input_file' || part.type === 'file') {
if (!hasFileSource(part)) return undefined;
- if (capabilities.fileInput !== 'file') {
- if (failClosed) throw new UnsupportedResponsesFeatureError(String(part.type));
- return undefined;
- }
+ if (capabilities.fileInput !== 'file') return undefined;
return filePart(part);
}
if (part.type === 'input_audio') {
if (!hasAudioSource(part)) return undefined;
- if (capabilities.audioInput !== 'input_audio') {
- if (failClosed) throw new UnsupportedResponsesFeatureError(String(part.type));
- return undefined;
- }
+ if (capabilities.audioInput !== 'input_audio') return undefined;
return audioPart(part);
}
return undefined;
@@ -226,23 +218,56 @@ function stringifyToolOutput(output: unknown): string {
}
}
+/** 带媒体来源但当前路由能力不支持、无法翻译的媒体 part。 */
+function isUntranslatableMediaPart(
+ part: Record<string, unknown>,
+ capabilities: ChatMediaCapabilities,
+): boolean {
+ if (isResponsesImageContentPartType(part.type)) {
+ return hasImageSource(part) && !isImagePartTranslatable(part, capabilities);
+ }
+ if (part.type === 'input_file' || part.type === 'file') {
+ return hasFileSource(part) && capabilities.fileInput !== 'file';
+ }
+ if (part.type === 'input_audio') {
+ return hasAudioSource(part) && capabilities.audioInput !== 'input_audio';
+ }
+ return false;
+}
+
+interface ToolMediaSink {
+ media: ChatUserContentPart[];
+ /** 发生过任何替换(搬运或占位)时为 true:序列化必须用替换后的结构。 */
+ substituted: boolean;
+}
+
function replaceToolMedia(
value: unknown,
- media: ChatUserContentPart[],
+ sink: ToolMediaSink,
mediaCapabilities: ChatMediaCapabilities,
): unknown {
if (Array.isArray(value)) {
- return value.map((part) => replaceToolMedia(part, media, mediaCapabilities));
+ return value.map((part) => replaceToolMedia(part, sink, mediaCapabilities));
}
if (!isPlainObject(value)) return value;
- const translated = mediaPart(value, mediaCapabilities, true);
+ const translated = mediaPart(value, mediaCapabilities);
if (translated) {
- media.push(translated);
+ sink.media.push(translated);
+ sink.substituted = true;
return { type: 'text', text: TOOL_RESULT_MEDIA_MOVED_MARKER };
}
+ if (isUntranslatableMediaPart(value, mediaCapabilities)) {
+ // 路由不支持该媒体类型时降级为占位文本而不是 fail-closed 拒绝整单:
+ // 上下文压缩 / 历史重放会把此前由视觉桥等机制拦下的图片以 input_image
+ // 重新带进工具输出,一票否决会让会话此后每一轮都被拒、彻底卡死,只能
+ // 新建任务(#2805)。文本模型本就无法接收这类媒体,占位丢弃是唯一可
+ // 继续的表达;当轮媒体拦截 / 转换仍由上游(视觉桥)负责。
+ sink.substituted = true;
+ return { type: 'text', text: TOOL_RESULT_MEDIA_OMITTED_MARKER };
+ }
return Object.fromEntries(
Object.entries(value).map(
- ([key, child]) => [key, replaceToolMedia(child, media, mediaCapabilities)],
+ ([key, child]) => [key, replaceToolMedia(child, sink, mediaCapabilities)],
),
);
}
@@ -251,11 +276,11 @@ function splitToolOutput(
output: unknown,
mediaCapabilities: ChatMediaCapabilities,
): { content: string; media: ChatUserContentPart[] } {
- const media: ChatUserContentPart[] = [];
- const replaced = replaceToolMedia(output, media, mediaCapabilities);
+ const sink: ToolMediaSink = { media: [], substituted: false };
+ const replaced = replaceToolMedia(output, sink, mediaCapabilities);
return {
- content: stringifyToolOutput(media.length > 0 ? replaced : output),
- media,
+ content: stringifyToolOutput(sink.substituted ? replaced : output),
+ media: sink.media,
};
}