Skip to content

🀯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

πŸ› fix(context-engine): stop reporting image tool results as failed calls (#18795)

Arvin Xuc143d887d7b4
diff --git a/packages/context-engine/src/processors/ToolMessageReorder.ts b/packages/context-engine/src/processors/ToolMessageReorder.ts
index 0b9ca7ea77f..f8ef0c86de6 100644
--- a/packages/context-engine/src/processors/ToolMessageReorder.ts
+++ b/packages/context-engine/src/processors/ToolMessageReorder.ts
@@ -21,6 +21,29 @@ const DEFAULT_TOOL_FAILURE_CONTENT = JSON.stringify({
synthetic: true,
});
+/**
+ * Whether a tool result still carries something the model can read.
+ *
+ * `MessageContentProcessor` rewrites a tool result that produced images (e.g.
+ * `readFile` on a screenshot, which uploads the file and carries the URL on
+ * `pluginState.images`) into multimodal parts β€” `[{ type: 'text' }, { type:
+ * 'image_url' }]` β€” whenever the model supports vision. A bare
+ * `typeof content === 'string'` guard therefore rejects exactly the results
+ * that carry an image: this pass would drop the parts and hand the model
+ * {@link DEFAULT_TOOL_FAILURE_CONTENT} instead, so a successful screenshot read
+ * arrived as "Tool call failed" and the image never reached the request at all.
+ */
+const hasUsableToolContent = (content: unknown, pluginErrorMessage?: string): boolean => {
+ // Multimodal parts (text + image_url / video_url / audio_url).
+ if (Array.isArray(content)) return content.length > 0;
+ if (typeof content !== 'string') return false;
+
+ // An empty string is a legitimate result for a tool that produced no output β€”
+ // unless the row also recorded an error, in which case the error text is the
+ // more useful thing to show the model.
+ return content.length > 0 || !pluginErrorMessage;
+};
+
/**
* Reorder tool messages to ensure that tool messages are displayed in the correct order.
* see https://github.com/lobehub/lobe-chat/pull/3155
@@ -151,11 +174,9 @@ export class ToolMessageReorder extends BaseProcessor {
reorderedMessages.push({
...matchedToolMessage,
- content:
- typeof matchedToolMessage.content === 'string' &&
- (matchedToolMessage.content.length > 0 || !pluginErrorMessage)
- ? matchedToolMessage.content
- : pluginErrorMessage || DEFAULT_TOOL_FAILURE_CONTENT,
+ content: hasUsableToolContent(matchedToolMessage.content, pluginErrorMessage)
+ ? matchedToolMessage.content
+ : pluginErrorMessage || DEFAULT_TOOL_FAILURE_CONTENT,
});
toolMessages.delete(toolCall.id);
continue;
diff --git a/packages/context-engine/src/processors/__tests__/ToolMessageReorder.test.ts b/packages/context-engine/src/processors/__tests__/ToolMessageReorder.test.ts
index c8c13ef1744..8be809c2d00 100644
--- a/packages/context-engine/src/processors/__tests__/ToolMessageReorder.test.ts
+++ b/packages/context-engine/src/processors/__tests__/ToolMessageReorder.test.ts
@@ -306,4 +306,63 @@ describe('ToolMessageReorder', () => {
},
]);
});
+
+ // Regression: MessageContentProcessor turns a tool result that produced an
+ // image into multimodal parts for vision models. The old string-only guard
+ // here replaced those parts with the synthetic "Tool call failed" payload, so
+ // a successful `readFile` on a screenshot reached the model as a failure and
+ // the image was dropped from the request.
+ it('should keep multimodal tool result parts produced for vision models', async () => {
+ const proc = new ToolMessageReorder();
+ const multimodalContent = [
+ { text: '[Image: screenshot.png]', type: 'text' },
+ {
+ image_url: { detail: 'auto', url: 'https://app.lobehub.com/f/file_abc' },
+ type: 'image_url',
+ },
+ ];
+ const ctx = createContext([
+ {
+ id: 'a1',
+ role: 'assistant',
+ content: '',
+ tool_calls: [
+ { function: { arguments: '{}', name: 'readFile' }, id: 'call_1', type: 'function' },
+ ],
+ },
+ {
+ id: 't1',
+ role: 'tool',
+ content: multimodalContent,
+ tool_call_id: 'call_1',
+ },
+ ]);
+
+ const result = await proc.process(ctx);
+
+ expect(result.messages[1].content).toEqual(multimodalContent);
+ });
+
+ it('should fall back to the synthetic failure when tool content is empty or unusable', async () => {
+ const proc = new ToolMessageReorder();
+ const ctx = createContext([
+ {
+ id: 'a1',
+ role: 'assistant',
+ content: '',
+ tool_calls: [
+ { function: { arguments: '{}', name: 'readFile' }, id: 'call_1', type: 'function' },
+ { function: { arguments: '{}', name: 'readFile' }, id: 'call_2', type: 'function' },
+ ],
+ },
+ { id: 't1', role: 'tool', content: [], tool_call_id: 'call_1' },
+ { id: 't2', role: 'tool', content: undefined, tool_call_id: 'call_2' },
+ ]);
+
+ const result = await proc.process(ctx);
+
+ const failure = JSON.stringify({ error: 'Tool call failed', success: false, synthetic: true });
+ expect(result.messages[1].content).toBe(failure);
+ expect(result.messages[2].content).toBe(failure);
+ });
});