import { z } from 'zod'; import type { Tool, ToolResult } from './Tool'; import type { AIProvider } from '@/contexts/AISettingsContext'; import type { NUser } from '@nostrify/react/login'; import { createAIClient } from '@/lib/ai-client'; import type { JSRuntimeFS } from '@/lib/JSRuntime'; import { proxyUrl } from '../proxyUrl'; interface GenerateImageParams { prompt: string; } export type ImageGenerationMode = 'chat' | 'image'; export class GenerateImageTool implements Tool { readonly description = 'Generate an image from a text prompt and save it to the virtual filesystem'; readonly inputSchema = z.object({ prompt: z.string().describe('A detailed text description of the image to generate'), }); constructor( private fs: JSRuntimeFS, private tmpPath: string, private provider: AIProvider, private model: string, private mode: ImageGenerationMode, private user?: NUser, private corsProxy?: string, ) {} async execute(args: GenerateImageParams): Promise { const { prompt } = args; try { const client = createAIClient(this.provider, this.user, this.corsProxy); let cost: number | undefined; let imageData: Uint8Array; let extension = 'png'; // Use chat completions endpoint for models that support image output modality if (this.mode === 'chat') { const response = await client.chat.completions.create({ model: this.model, messages: [ { role: 'user', content: prompt, }, ], modalities: ['image'] as never[], // Type assertion needed as OpenAI SDK doesn't have this typed }); // Extract cost from response const { usage } = response; if (usage && 'cost' in usage && typeof usage.cost === 'number') { cost = usage.cost; } // Extract image data URI from response const message = response.choices[0]?.message; if (!message) { throw new Error('No message returned from the API'); } const result = z.object({ images: z.object({ type: z.literal('image_url'), image_url: z.object({ url: z.string(), }), }).array().min(1), }).safeParse(message); if (!result.success) { throw new Error('Invalid response format from the API'); } const dataUri = result.data.images[0].image_url.url; // Parse data URI: data:image/png;base64,iVBORw0KGgoAAAANS... const dataUriMatch = dataUri.match(/^data:image\/([^;]+);base64,(.+)$/); if (!dataUriMatch) { throw new Error('Invalid data URI format'); } extension = dataUriMatch[1]; const base64Data = dataUriMatch[2]; // Decode base64 to bytes const binaryString = atob(base64Data); imageData = new Uint8Array(binaryString.length); for (let i = 0; i < binaryString.length; i++) { imageData[i] = binaryString.charCodeAt(i); } } else { // Standard OpenAI image generation const response = await client.images.generate({ model: this.model, prompt, }); // Extract cost from response const { usage } = response; if (usage && 'cost' in usage && typeof usage.cost === 'number') { cost = usage.cost; } const firstImage = response.data?.[0]; if (!firstImage) { throw new Error('No image data returned from the API'); } // Check if we got b64_json or url if (firstImage.b64_json) { // Decode base64 to bytes const binaryString = atob(firstImage.b64_json); imageData = new Uint8Array(binaryString.length); for (let i = 0; i < binaryString.length; i++) { imageData[i] = binaryString.charCodeAt(i); } } else if (firstImage.url) { // Fetch the image from the URL const imageUrl = this.corsProxy ? proxyUrl(this.corsProxy, firstImage.url) : firstImage.url; const imageResponse = await fetch(imageUrl); if (!imageResponse.ok) { throw new Error(`Failed to fetch image from URL: ${imageResponse.status} ${imageResponse.statusText}`); } const arrayBuffer = await imageResponse.arrayBuffer(); imageData = new Uint8Array(arrayBuffer); // Try to determine extension from content-type header const contentType = imageResponse.headers.get('content-type'); if (contentType) { const match = contentType.match(/image\/([^;]+)/); if (match) { extension = match[1]; } } } else { throw new Error('No base64 image data or URL returned from the API'); } // Default extension is PNG for standard OpenAI responses if (extension === 'png') { extension = 'png'; } } // Generate unique filename const timestamp = Date.now(); const filename = `generated-${timestamp}.${extension}`; const filepath = `${this.tmpPath}/${filename}`; // Ensure tmp directory exists try { await this.fs.stat(this.tmpPath); } catch { await this.fs.mkdir(this.tmpPath, { recursive: true }); } // Write image to VFS await this.fs.writeFile(filepath, imageData); return { content: `Generated image: ${filepath}`, cost }; } catch (error) { const errorMessage = error instanceof Error ? error.message : String(error); throw new Error(`Failed to generate image: ${errorMessage}`); } } }