From 76fa9940db1f27a09b21a47cb2fe5cfa7a521683 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Sun, 14 Sep 2025 14:13:18 -0500 Subject: [PATCH] Parse user messages --- README.md | 2 +- src/components/AIMessageItem.tsx | 5 +- src/components/UserMessage.test.tsx | 83 ++++++++++++++++++++++++ src/components/UserMessage.tsx | 92 +++++++++++++++++++++++++++ src/components/ui/file-attachment.tsx | 4 +- 5 files changed, 181 insertions(+), 5 deletions(-) create mode 100644 src/components/UserMessage.test.tsx create mode 100644 src/components/UserMessage.tsx diff --git a/README.md b/README.md index f8bce56d..ffc92583 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ Shakespeare is an AI chat application that allows users to build custom Nostr we ## Features - **AI-Powered Development**: Chat with an AI assistant to build your Nostr website -- **File Attachments**: Attach files directly to chat messages for AI processing +- **File Attachments**: Attach files directly to chat messages with visual badges and deduplication - **Project Management**: Create and manage multiple projects with sidebar navigation - **Real-time Editing**: Edit files directly in the browser with syntax highlighting - **File Explorer**: Browse and manage your project files diff --git a/src/components/AIMessageItem.tsx b/src/components/AIMessageItem.tsx index 113f18a7..18b7f55d 100644 --- a/src/components/AIMessageItem.tsx +++ b/src/components/AIMessageItem.tsx @@ -3,6 +3,7 @@ import { Streamdown } from 'streamdown'; import { Wrench, Eye, FileText, Edit, Package, PackageMinus, GitCommit, BookOpen, Download, Hash, Tag, Network, List, Plus, Terminal, Globe, CheckCircle } from 'lucide-react'; import type { AIMessage } from '@/lib/SessionManager'; import { cn } from '@/lib/utils'; +import { UserMessage } from '@/components/UserMessage'; import OpenAI from 'openai'; interface AIMessageItemProps { @@ -208,8 +209,8 @@ export const AIMessageItem = memo(({ return (
-
- {getContent()} +
+
diff --git a/src/components/UserMessage.test.tsx b/src/components/UserMessage.test.tsx new file mode 100644 index 00000000..0c392d4d --- /dev/null +++ b/src/components/UserMessage.test.tsx @@ -0,0 +1,83 @@ +import { describe, it, expect } from 'vitest'; +import { render, screen } from '@testing-library/react'; +import { UserMessage } from './UserMessage'; + +describe('UserMessage', () => { + it('renders regular text without file attachments', () => { + const content = 'This is a regular message without any file attachments.'; + + render(); + + expect(screen.getByText(content)).toBeInTheDocument(); + }); + + it('renders file attachment badges for single file', () => { + const content = 'Here is my file: File added to /tmp/test.txt'; + + render(); + + expect(screen.getByText('Here is my file:')).toBeInTheDocument(); + expect(screen.getByText('test.txt')).toBeInTheDocument(); + expect(screen.getByText('added to /tmp/test.txt')).toBeInTheDocument(); + }); + + it('renders multiple file attachments', () => { + const content = 'Files uploaded:\nFile added to /tmp/document.pdf\nFile added to /tmp/image.jpg\nAll done!'; + + render(); + + expect(screen.getByText('Files uploaded:')).toBeInTheDocument(); + expect(screen.getByText('document.pdf')).toBeInTheDocument(); + expect(screen.getByText('image.jpg')).toBeInTheDocument(); + expect(screen.getByText('All done!')).toBeInTheDocument(); + }); + + it('renders text files with FileText icon', () => { + const content = 'File added to /tmp/script.js'; + + render(); + + expect(screen.getByText('script.js')).toBeInTheDocument(); + // FileText icon should be rendered (we can't easily test the icon itself, but we can test the component renders) + }); + + it('handles files without extensions', () => { + const content = 'File added to /tmp/README'; + + render(); + + expect(screen.getByText('README')).toBeInTheDocument(); + expect(screen.getByText('added to /tmp/README')).toBeInTheDocument(); + }); + + it('handles complex filenames with underscores and numbers', () => { + const content = 'File added to /tmp/my_file_1.txt'; + + render(); + + expect(screen.getByText('my_file_1.txt')).toBeInTheDocument(); + expect(screen.getByText('added to /tmp/my_file_1.txt')).toBeInTheDocument(); + }); + + it('preserves whitespace in text content', () => { + const content = 'Line 1\n\nLine 3 with spaces \nFile added to /tmp/test.txt\n\nFinal line'; + + const { container } = render(); + + // Check that the component renders and contains the expected text content + expect(container.textContent).toContain('Line 1'); + expect(container.textContent).toContain('Line 3 with spaces'); + expect(container.textContent).toContain('Final line'); + expect(screen.getByText('test.txt')).toBeInTheDocument(); + }); + + it('handles mixed content with file attachments in the middle', () => { + const content = 'Starting text File added to /tmp/middle.txt ending text'; + + render(); + + expect(screen.getByText('Starting text')).toBeInTheDocument(); + expect(screen.getByText('middle.txt')).toBeInTheDocument(); + expect(screen.getByText('ending text')).toBeInTheDocument(); + }); +}); \ No newline at end of file diff --git a/src/components/UserMessage.tsx b/src/components/UserMessage.tsx new file mode 100644 index 00000000..0b3c547c --- /dev/null +++ b/src/components/UserMessage.tsx @@ -0,0 +1,92 @@ +import { FileText, File } from 'lucide-react'; +import { Badge } from '@/components/ui/badge'; + +interface UserMessageProps { + content: string; +} + +/** + * Component to render user messages nicely + * Detects "File added to /tmp/" patterns and renders them as badges + */ +export function UserMessage({ content }: UserMessageProps) { + // Regular expression to match "File added to /tmp/" patterns + const filePattern = /File added to (\/tmp\/[^\s\n]+)/g; + + // Split content by file attachments to render them separately + const parts: Array<{ type: 'text' | 'file'; content: string; filepath?: string }> = []; + let lastIndex = 0; + let match; + + while ((match = filePattern.exec(content)) !== null) { + // Add text before the file attachment + if (match.index > lastIndex) { + const textBefore = content.slice(lastIndex, match.index); + if (textBefore.trim()) { + parts.push({ type: 'text', content: textBefore }); + } + } + + // Add the file attachment + const fullMatch = match[0]; // "File added to /tmp/filename.txt" + const filepath = match[1]; // "/tmp/filename.txt" + parts.push({ type: 'file', content: fullMatch, filepath }); + + lastIndex = match.index + fullMatch.length; + } + + // Add remaining text after the last file attachment + if (lastIndex < content.length) { + const remainingText = content.slice(lastIndex); + if (remainingText.trim()) { + parts.push({ type: 'text', content: remainingText }); + } + } + + // If no file attachments found, render as normal text + if (parts.length === 0) { + return {content}; + } + + 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 ( +
+ {parts.map((part, index) => { + if (part.type === 'text') { + return ( + + {part.content} + + ); + } else { + const filename = getFileName(part.filepath!); + return ( +
+ + {getFileIcon(filename)} + {filename} + added to {part.filepath} + +
+ ); + } + })} +
+ ); +} \ No newline at end of file diff --git a/src/components/ui/file-attachment.tsx b/src/components/ui/file-attachment.tsx index b52e8270..780c3b4a 100644 --- a/src/components/ui/file-attachment.tsx +++ b/src/components/ui/file-attachment.tsx @@ -58,14 +58,14 @@ export function FileAttachment({ className="hidden" disabled={disabled} /> - +