import { FileText, File } 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 const filePattern = /^Added file: (\/tmp\/[^\s\n]+)$/; // 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(); const match = text.match(filePattern); if (match) { // This is a file attachment const filepath = match[1]; // "/tmp/filename.txt" processedParts.push({ type: 'file', content: text, filepath }); } else if (text) { // This is regular text content processedParts.push({ type: 'text', content: text }); } }); // If no content, return empty if (processedParts.length === 0) { return null; } const getFileIcon = (filename: string) => { const extension = filename.split('.').pop()?.toLowerCase(); // Return FileText icon for text-like files, File icon for others const textExtensions = ['txt', 'md', 'json', 'js', 'ts', 'jsx', 'tsx', 'css', 'html', 'xml', 'yml', 'yaml']; if (extension && textExtensions.includes(extension)) { return ; } return ; }; const getFileName = (filepath: string) => { return filepath.split('/').pop() || filepath; }; return (
{processedParts.map((part, index) => { if (part.type === 'text') { return (
{part.content}
); } else { const filename = getFileName(part.filepath!); return (
{getFileIcon(filename)} {filename}
); } })}
); }