mirror of
https://gitlab.com/soapbox-pub/shakespeare.git
synced 2026-12-06 00:27:24 +00:00
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:
3 files changed
+38
-3
No files matched your search
@@ -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,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -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> {
|
||||
|
||||
Reference in new issue
Block a user