import { Paperclip } from 'lucide-react'; import { Badge } from '@/components/ui/badge'; import type OpenAI from 'openai'; interface UserMessageProps { content: string | Array; } /** * Component to render user messages nicely * Handles both string content and array content (text parts) * Detects "Added file: /tmp/" patterns and renders them as badges */ export function UserMessage({ content }: UserMessageProps) { // Convert content to array format for uniform processing const contentParts = typeof content === 'string' ? [{ type: 'text' as const, text: content }] : content.filter(part => part.type === 'text') as Array<{ type: 'text'; text: string }>; // Regular expression to match "Added file: /tmp/" patterns // Matches the pattern anywhere in the text const filePattern = /Added file:\s*(\/tmp\/[^\s\n]+)/g; // Process each content part and categorize as text or file const processedParts: Array<{ type: 'text' | 'file'; content: string; filepath?: string }> = []; contentParts.forEach(part => { const text = part.text.trim(); // Find all file patterns in this text part const fileMatches: Array<{ filepath: string; index: number; length: number }> = []; let match; filePattern.lastIndex = 0; // Reset regex while ((match = filePattern.exec(text)) !== null) { fileMatches.push({ 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 }); } }); // If no content, return empty if (processedParts.length === 0) { return null; } const getFileName = (filepath: string) => { return filepath.split('/').pop() || filepath; }; return (
{processedParts.map((part, index) => { if (part.type === 'text') { // Only render text if it has content if (!part.content.trim()) return null; return (
{part.content}
); } else { // Render file attachment badge (no "Added file:" text) const filename = getFileName(part.filepath!); return (
{filename}
); } })}
); }