Fix generate_image tool, fix rendering of image from VFS

This commit is contained in:
Alex Gleason committed 2025-12-07 02:43:29 -06:00
1 parent b8638ac13a
commit 6885e2beec
6 files changed
+288 -118

No files matched your search

+21 -17
View File
@@ -4,6 +4,7 @@ import { Wrench, Eye, FileText, Edit, Package, PackageMinus, GitCommit, BookOpen
import type { AIMessage } from '@/lib/SessionManager';
import { cn } from '@/lib/utils';
import { UserMessage } from '@/components/UserMessage';
import { VFSImage } from '@/components/VFSImage';
import OpenAI from 'openai';
import { useTheme } from '@/hooks/useTheme';
import { isEmptyMessage } from '@/lib/isEmptyMessage';
@@ -274,18 +275,13 @@ export const AIMessageItem = memo(({
const filename = imagePath.split('/').pop() || imagePath;
return (
<div className="mt-1 p-3 bg-muted/30 rounded border">
<div className="space-y-2">
<div className="text-xs text-muted-foreground font-mono">
{imagePath}
</div>
<img
src={imagePath}
alt={filename}
className="max-w-full h-auto rounded cursor-pointer hover:opacity-90 transition-opacity"
onClick={() => setExpandedImageUrl(imagePath)}
/>
</div>
<div className="mt-1 p-3 bg-muted/30 rounded border max-w-xs">
<VFSImage
path={imagePath}
alt={filename}
className="max-w-full h-auto rounded cursor-pointer hover:opacity-90 transition-opacity"
onClick={() => setExpandedImageUrl(imagePath)}
/>
</div>
);
}
@@ -398,11 +394,19 @@ export const AIMessageItem = memo(({
>
{expandedImageUrl && (
<div className="relative flex items-center justify-center">
<img
src={expandedImageUrl}
alt="Expanded image"
className="max-w-full max-h-[95vh] w-auto h-auto object-contain rounded-lg"
/>
{expandedImageUrl.startsWith('/') ? (
<VFSImage
path={expandedImageUrl}
alt="Expanded image"
className="max-w-full max-h-[95vh] w-auto h-auto object-contain rounded-lg"
/>
) : (
<img
src={expandedImageUrl}
alt="Expanded image"
className="max-w-full max-h-[95vh] w-auto h-auto object-contain rounded-lg"
/>
)}
</div>
)}
</DialogPrimitive.Content>
+2
View File
@@ -49,6 +49,8 @@ const AppConfigSchema = z.object({
fsPathTemplates: z.string().min(1),
sentryDsn: z.string(),
sentryEnabled: z.boolean(),
systemPrompt: z.string(),
imageModel: z.string(),
}) satisfies z.ZodType<AppConfig>;
export function AppProvider(props: AppProviderProps) {
+6 -6
View File
@@ -47,6 +47,7 @@ import { ShakespeareLogo } from '@/components/ShakespeareLogo';
import { ChatInput } from '@/components/Shakespeare/ChatInput';
import { buildMessageContent } from '@/lib/buildMessageContent';
import { DotAI } from '@/lib/DotAI';
import { parseProviderModel } from '@/lib/parseProviderModel';
// Clean interfaces now handled by proper hooks
@@ -186,19 +187,18 @@ export const ChatPane = forwardRef<ChatPaneRef, ChatPaneProps>(({
// Add generate_image tool if imageModel is configured
if (config.imageModel) {
// Extract provider ID from imageModel (format: "provider/model")
const providerId = config.imageModel.split('/')[0];
const provider = settings.providers.find(p => p.id === providerId);
if (provider) {
try {
const { provider, model } = parseProviderModel(config.imageModel, settings.providers);
tools.generate_image = new GenerateImageTool(
fs,
tmpPath,
provider,
config.imageModel,
model,
user,
config.corsProxy
);
} catch (error) {
console.warn('Failed to parse imageModel:', error);
}
}
+98
View File
@@ -0,0 +1,98 @@
import { useEffect, useState } from 'react';
import { useFS } from '@/hooks/useFS';
import { Skeleton } from '@/components/ui/skeleton';
import { AlertCircle } from 'lucide-react';
interface VFSImageProps {
path: string;
alt: string;
className?: string;
onClick?: () => void;
}
export function VFSImage({ path, alt, className, onClick }: VFSImageProps) {
const { fs } = useFS();
const [dataUri, setDataUri] = useState<string | null>(null);
const [error, setError] = useState<string | null>(null);
const [isLoading, setIsLoading] = useState(true);
useEffect(() => {
let isMounted = true;
async function loadImage() {
try {
setIsLoading(true);
setError(null);
// Read the file from VFS
const imageData = await fs.readFile(path);
if (!isMounted) return;
// Determine MIME type from file extension
const extension = path.split('.').pop()?.toLowerCase() || '';
const mimeTypes: Record<string, string> = {
'png': 'image/png',
'jpg': 'image/jpeg',
'jpeg': 'image/jpeg',
'gif': 'image/gif',
'webp': 'image/webp',
'svg': 'image/svg+xml',
'bmp': 'image/bmp',
};
const mimeType = mimeTypes[extension] || 'image/png';
// Convert bytes to base64
const base64 = btoa(
Array.from(imageData)
.map(byte => String.fromCharCode(byte))
.join('')
);
// Create data URI
const uri = `data:${mimeType};base64,${base64}`;
if (isMounted) {
setDataUri(uri);
setIsLoading(false);
}
} catch (err) {
if (isMounted) {
setError(err instanceof Error ? err.message : 'Failed to load image');
setIsLoading(false);
}
}
}
loadImage();
return () => {
isMounted = false;
};
}, [fs, path]);
if (isLoading) {
return <Skeleton className={className || 'w-full h-64'} />;
}
if (error || !dataUri) {
return (
<div className={className || 'w-full h-64 flex items-center justify-center bg-muted rounded'}>
<div className="text-center text-muted-foreground space-y-2">
<AlertCircle className="h-8 w-8 mx-auto" />
<p className="text-xs">{error || 'Failed to load image'}</p>
<p className="text-xs font-mono">{path}</p>
</div>
</div>
);
}
return (
<img
src={dataUri}
alt={alt}
className={className}
onClick={onClick}
/>
);
}