Enable AI to see images when using the read tool

Return JPG/PNG images as structured image content parts from ReadTool
instead of embedding base64 data URLs as plain text. Since OpenAI tool
messages only support text content, images are injected as a follow-up
user message with image_url parts that vision models can interpret.
This commit is contained in:
Alex Gleason committed 2026-03-28 18:27:41 -05:00
1 parent d6e8b9bc84
commit 2641252c6a
3 files changed
+38 -3

No files matched your search

+7 -2
View File
@@ -86,10 +86,15 @@ Usage:
const base64 = this.arrayBufferToBase64(bytes);
const ext = this.getFileExtension(filepath).toLowerCase();
const mimeType = this.getMimeType(ext);
const dataUrl = `data:${mimeType};base64,${base64}`;
// For vision-supported formats (JPG/PNG), return as a structured image
// so the AI can actually "see" the image
const isVisionSupported = ['jpg', 'jpeg', 'png'].includes(ext);
// Return base64 data URL in the content
return {
content: `[Image file: ${ext.toUpperCase()} file]\n\nImage read successfully.\n\nData URL: data:${mimeType};base64,${base64}`,
content: `[Image file: ${ext.toUpperCase()} file]\n\nImage read successfully.${!isVisionSupported ? `\n\nData URL: ${dataUrl}` : ''}`,
images: isVisionSupported ? [{ type: 'image' as const, url: dataUrl }] : undefined,
};
}
+6
View File
@@ -1,8 +1,14 @@
import type z from "zod";
export interface ToolResultImage {
type: 'image';
url: string; // data URL (e.g. data:image/png;base64,...)
}
export interface ToolResult {
content: string;
cost?: number;
images?: ToolResultImage[];
}
export interface Tool<TParams> {