diff --git a/src/components/AIMessageItem.tsx b/src/components/AIMessageItem.tsx index 4517ce86..a6754705 100644 --- a/src/components/AIMessageItem.tsx +++ b/src/components/AIMessageItem.tsx @@ -80,14 +80,6 @@ export const AIMessageItem = memo(({ // 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 - : textParts.length > 0 - ? textParts.map(part => part.text).join('\n') - : ''; // Extract image URLs from content array const imageUrls = contentArray @@ -102,7 +94,7 @@ export const AIMessageItem = memo(({
{/* Render text content and attachments first */} - {userContent && } + {/* Render images after text and attachments */} {imageUrls.length > 0 && (
diff --git a/src/components/UserMessage.tsx b/src/components/UserMessage.tsx index 1abcd883..0bca0680 100644 --- a/src/components/UserMessage.tsx +++ b/src/components/UserMessage.tsx @@ -3,7 +3,7 @@ import { Badge } from '@/components/ui/badge'; import type OpenAI from 'openai'; interface UserMessageProps { - content: string | Array; + content: string | Array; } /** @@ -13,71 +13,12 @@ interface UserMessageProps { */ export function UserMessage({ content }: UserMessageProps) { // Convert content to array format for uniform processing - const contentParts = typeof content === 'string' + const parts = typeof content === 'string' ? [{ type: 'text' as const, text: content }] - : content.filter(part => part.type === 'text') as Array<{ type: 'text'; text: string }>; - - // Process each content part and categorize as text or file - const processedParts: Array<{ type: 'text' | 'file'; content: string; filepath?: string }> = []; - - contentParts.forEach((part, index) => { - const text = part.text.trim(); - - // First content part is always treated as text - if (index === 0) { - processedParts.push({ type: 'text', content: text }); - return; - } - - // Matches the pattern anywhere in the text - // Regular expression to match "Added file: " patterns - const filePattern = /^Added file: (\/[^\s\n]+$)/g; - - // Find all file patterns in this text part - const fileMatches = [...text.matchAll(filePattern)].map(match => ({ - filepath: match[1], // "/tmp/filename.txt" - index: match.index, - length: match[0].length, - })); - - if (fileMatches.length > 0) { - // Split text around file patterns and process each segment - let lastIndex = 0; - - fileMatches.forEach((fileMatch) => { - // Add text before the file pattern - if (fileMatch.index > lastIndex) { - const textBefore = text.substring(lastIndex, fileMatch.index).trim(); - if (textBefore) { - processedParts.push({ type: 'text', content: textBefore }); - } - } - - // Add the file attachment badge (without showing "Added file:" text) - processedParts.push({ - type: 'file', - content: '', // Empty - badge will show filename only - filepath: fileMatch.filepath - }); - - lastIndex = fileMatch.index + fileMatch.length; - }); - - // Add remaining text after the last file pattern - if (lastIndex < text.length) { - const textAfter = text.substring(lastIndex).trim(); - if (textAfter) { - processedParts.push({ type: 'text', content: textAfter }); - } - } - } else if (text) { - // No file patterns found, treat as regular text - processedParts.push({ type: 'text', content: text }); - } - }); + : content.filter(part => part.type === 'text'); // If no content, return empty - if (processedParts.length === 0) { + if (parts.length === 0) { return null; } @@ -87,28 +28,42 @@ export function UserMessage({ content }: UserMessageProps) { return (
- {processedParts.map((part, index) => { + {parts.map((part, index) => { if (part.type === 'text') { // Only render text if it has content - if (!part.content.trim()) return null; + if (!part.text.trim()) return null; + + // Detect "Added file: " pattern + const fileAddedMatch = /^Added file: (\/[^\s\n]+$)/g.exec(part.text); + console.log('fileAddedMatch', part, fileAddedMatch); + if (fileAddedMatch && fileAddedMatch[1]) { + const path = fileAddedMatch[1]; + const filename = getFileName(path); + return ( +
+ + + + + + {filename} + + +
+ ); + } + + // Regular text rendering return (
- {part.content} + {part.text}
); } else { - // Render file attachment badge (no "Added file:" text) - const filename = getFileName(part.filepath!); + // Unsupported content type return ( -
- - - - - - {filename} - - +
+ [Unsupported content type]
); } diff --git a/src/lib/SessionManager.ts b/src/lib/SessionManager.ts index 00649778..decf2986 100644 --- a/src/lib/SessionManager.ts +++ b/src/lib/SessionManager.ts @@ -347,7 +347,7 @@ export class SessionManager { if (textParts.length > 0) { return { ...msg, - content: textParts.map(part => (part as OpenAI.Chat.Completions.ChatCompletionContentPartText).text).join('\n') + content: textParts, }; }