diff --git a/src/components/AIMessageItem.tsx b/src/components/AIMessageItem.tsx index aaf2bcad..f2c6294b 100644 --- a/src/components/AIMessageItem.tsx +++ b/src/components/AIMessageItem.tsx @@ -7,6 +7,8 @@ import { UserMessage } from '@/components/UserMessage'; import OpenAI from 'openai'; import { useTheme } from '@/hooks/useTheme'; import { isEmptyMessage } from '@/lib/isEmptyMessage'; +import { Dialog, DialogPortal, DialogOverlay } from '@/components/ui/dialog'; +import * as DialogPrimitive from '@radix-ui/react-dialog'; // Type guard to check if message has reasoning content function hasReasoningContent(message: AIMessage): message is AIMessage & { reasoning_content: string } { @@ -26,6 +28,7 @@ export const AIMessageItem = memo(({ }: AIMessageItemProps) => { const [isToolExpanded, setIsToolExpanded] = useState(false); const [isReasoningExpanded, setIsReasoningExpanded] = useState(false); + const [expandedImageUrl, setExpandedImageUrl] = useState(null); const { displayTheme } = useTheme(); // Get content to display @@ -295,20 +298,90 @@ export const AIMessageItem = memo(({ // Regular rendering for non-tool messages if (message.role === 'user') { // User messages: right-aligned bubble without avatar/name + // Extract text and image parts from message content + const contentArray = Array.isArray(message.content) ? message.content : null; + const textParts = contentArray + ? contentArray.filter(part => part.type === 'text') as OpenAI.Chat.Completions.ChatCompletionContentPartText[] + : []; const userContent = typeof message.content === 'string' ? message.content - : Array.isArray(message.content) - ? message.content.filter(part => part.type === 'text') as OpenAI.Chat.Completions.ChatCompletionContentPartText[] + : textParts.length > 0 + ? textParts.map(part => part.text).join('\n') : ''; + // Extract image URLs from content array + const imageUrls = contentArray + ? contentArray + .filter(part => part.type === 'image_url') + .map(part => (part as OpenAI.Chat.Completions.ChatCompletionContentPartImage).image_url.url) + : []; + return ( -
-
-
- + <> +
+
+
+ {/* Render text content and attachments first */} + {userContent && } + {/* Render images after text and attachments */} + {imageUrls.length > 0 && ( +
+ {imageUrls.map((url, index) => ( +
setExpandedImageUrl(url)} + > + {`User { + // Fallback if image fails to load + const target = e.target as HTMLImageElement; + target.style.display = 'none'; + const fallback = document.createElement('div'); + fallback.className = 'p-2 text-xs text-muted-foreground bg-muted rounded'; + fallback.textContent = `Image failed to load: ${url}`; + target.parentElement?.appendChild(fallback); + }} + /> +
+ ))} +
+ )} +
-
+ + {/* Image expansion dialog */} + !open && setExpandedImageUrl(null)}> + + + setExpandedImageUrl(null)} + onEscapeKeyDown={() => setExpandedImageUrl(null)} + > + {expandedImageUrl && ( +
+ Expanded image +
+ )} +
+
+
+ ); } diff --git a/src/components/Shakespeare/ChatPane.tsx b/src/components/Shakespeare/ChatPane.tsx index 9e7b0f72..88529db3 100644 --- a/src/components/Shakespeare/ChatPane.tsx +++ b/src/components/Shakespeare/ChatPane.tsx @@ -41,11 +41,11 @@ import { ProjectPreviewConsoleError } from '@/lib/consoleMessages'; import { toolToOpenAI } from '@/lib/tools/openai-adapter'; import { Tool } from '@/lib/tools/Tool'; import OpenAI from 'openai'; -import { saveFileToTmp } from '@/lib/fileUtils'; import { useGit } from '@/hooks/useGit'; import { Quilly } from '@/components/Quilly'; import { ShakespeareLogo } from '@/components/ShakespeareLogo'; import { ChatInput } from '@/components/Shakespeare/ChatInput'; +import { buildMessageContent } from '@/lib/buildMessageContent'; // Clean interfaces now handled by proper hooks @@ -456,45 +456,18 @@ export const ChatPane = forwardRef(({ const modelToUse = providerModel.trim(); - // Build message content as text parts - const contentParts: Array = []; - - // Add user input as text part if present - if (input.trim()) { - contentParts.push({ - type: 'text', - text: input.trim() - }); - } - - // Process attached files and add as separate text parts - if (attachedFiles.length > 0) { - const filePromises = attachedFiles.map(async (file) => { - try { - const savedPath = await saveFileToTmp(fs, file, tmpPath); - return `Added file: ${savedPath}`; - } catch (error) { - console.error('Failed to save file:', error); - return `Failed to save file: ${file.name}`; - } - }); - - const fileResults = await Promise.all(filePromises); - - // Add each file as a separate text part - fileResults.forEach(fileResult => { - contentParts.push({ - type: 'text', - text: fileResult - }); - }); - } + // Build message content from input and attached files + // Images are converted to base64-encoded data URLs + const messageContent = await buildMessageContent( + input, + attachedFiles, + fs, + tmpPath + ); // Add model to recently used when sending a message addRecentlyUsedModel(modelToUse); - // Send as text parts if we have multiple parts, otherwise send as string for simplicity - const messageContent = contentParts.length === 1 ? contentParts[0].text : contentParts; await sendMessage(messageContent, modelToUse); }, [addRecentlyUsedModel, fs, isConfigured, isLoading, providerModel, sendMessage, tmpPath]); diff --git a/src/components/Shakespeare/FileEditor.tsx b/src/components/Shakespeare/FileEditor.tsx index 3e79d416..a106bab5 100644 --- a/src/components/Shakespeare/FileEditor.tsx +++ b/src/components/Shakespeare/FileEditor.tsx @@ -3,9 +3,10 @@ import { useTranslation } from 'react-i18next'; import { Button } from '@/components/ui/button'; import { Textarea } from '@/components/ui/textarea'; import { CardContent, CardHeader, CardTitle } from '@/components/ui/card'; -import { Save, Loader2 } from 'lucide-react'; +import { Save, Loader2, ImageOff } from 'lucide-react'; import { cn } from '@/lib/utils'; import { useGitStatus } from '@/hooks/useGitStatus'; +import { isMediaFile } from '@/lib/fileUtils'; interface FileEditorProps { filePath: string; @@ -93,6 +94,7 @@ export function FileEditor({ filePath, content, onSave, isLoading, projectId }: // Get git status for the current file const currentFileGitStatus = getFileGitStatus(filePath); const gitStatusClasses = getGitStatusClasses(currentFileGitStatus); + const isMedia = isMediaFile(filePath); return (
@@ -100,32 +102,36 @@ export function FileEditor({ filePath, content, onSave, isLoading, projectId }:
{filePath}
- {hasChanges && ( + {hasChanges && !isMedia && ( {t('unsavedChanges')} )} - + {!isMedia && ( + + )}
-
- {t('languageLabel')}: {getLanguageFromPath(filePath)} -
+ {!isMedia && ( +
+ {t('languageLabel')}: {getLanguageFromPath(filePath)} +
+ )} @@ -133,6 +139,10 @@ export function FileEditor({ filePath, content, onSave, isLoading, projectId }:
+ ) : isMedia ? ( +
+ +
) : (