diff --git a/src/lib/SessionManager.ts b/src/lib/SessionManager.ts index 5f17d40c..d2568541 100644 --- a/src/lib/SessionManager.ts +++ b/src/lib/SessionManager.ts @@ -4,7 +4,7 @@ import type { JSRuntimeFS } from './JSRuntime'; import { DotAI } from './DotAI'; import { parseProviderModel } from './parseProviderModel'; import { createAIClient } from './ai-client'; -import type { Tool } from './tools/Tool'; +import type { Tool, ToolResultImage } from './tools/Tool'; import type { NUser } from '@nostrify/react/login'; import type { AIProvider } from '@/contexts/AISettingsContext'; import { makeSystemPrompt } from './system'; @@ -656,6 +656,11 @@ export class SessionManager { const result = await tool.execute(toolArgs); await this.addToolMessage(projectId, functionToolCall.id, result.content); + // If tool returned images, inject a user message so the AI can "see" them + if (result.images?.length) { + await this.addToolImageMessage(projectId, result.images); + } + // Update session cost if tool returned a cost if (result.cost !== undefined) { const updatedSession = this.sessions.get(projectId); @@ -799,6 +804,25 @@ export class SessionManager { await this.addMessage(projectId, toolMessage); } + /** + * Helper to inject a user message with images from tool results. + * Since tool messages only support text content, images are sent + * as a follow-up user message so vision models can actually see them. + */ + private async addToolImageMessage(projectId: string, images: ToolResultImage[]): Promise { + const content: OpenAI.Chat.Completions.ChatCompletionContentPart[] = images.map((img) => ({ + type: 'image_url' as const, + image_url: { url: img.url }, + })); + + const imageMessage: AIMessage = { + role: 'user', + content, + }; + + await this.addMessage(projectId, imageMessage); + } + /** * Update session cost and context usage based on usage data */ diff --git a/src/lib/tools/ReadTool.ts b/src/lib/tools/ReadTool.ts index 009189bc..ac10d2fa 100644 --- a/src/lib/tools/ReadTool.ts +++ b/src/lib/tools/ReadTool.ts @@ -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, }; } diff --git a/src/lib/tools/Tool.ts b/src/lib/tools/Tool.ts index fa700cd0..e99826b7 100644 --- a/src/lib/tools/Tool.ts +++ b/src/lib/tools/Tool.ts @@ -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 {