mirror of
https://gitlab.com/soapbox-pub/shakespeare.git
synced 2026-12-03 07:57:22 +00:00
Parse user messages
This commit is contained in:
5 files changed
+181
-5
No files matched your search
@@ -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
|
||||
|
||||
@@ -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 (
|
||||
<div className="flex justify-end py-6">
|
||||
<div className="max-w-[80%] bg-secondary rounded-2xl rounded-br-md px-4 py-3">
|
||||
<div className="text-sm whitespace-pre-wrap break-words">
|
||||
{getContent()}
|
||||
<div className="text-sm break-words">
|
||||
<UserMessage content={getContent()} />
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -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(<UserMessage content={content} />);
|
||||
|
||||
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(<UserMessage content={content} />);
|
||||
|
||||
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(<UserMessage content={content} />);
|
||||
|
||||
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(<UserMessage content={content} />);
|
||||
|
||||
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(<UserMessage content={content} />);
|
||||
|
||||
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(<UserMessage content={content} />);
|
||||
|
||||
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(<UserMessage content={content} />);
|
||||
|
||||
// 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(<UserMessage content={content} />);
|
||||
|
||||
expect(screen.getByText('Starting text')).toBeInTheDocument();
|
||||
expect(screen.getByText('middle.txt')).toBeInTheDocument();
|
||||
expect(screen.getByText('ending text')).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
@@ -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/<filename>" patterns and renders them as badges
|
||||
*/
|
||||
export function UserMessage({ content }: UserMessageProps) {
|
||||
// Regular expression to match "File added to /tmp/<filename>" 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 <span className="whitespace-pre-wrap">{content}</span>;
|
||||
}
|
||||
|
||||
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 <FileText className="h-3 w-3" />;
|
||||
}
|
||||
|
||||
return <File className="h-3 w-3" />;
|
||||
};
|
||||
|
||||
const getFileName = (filepath: string) => {
|
||||
return filepath.split('/').pop() || filepath;
|
||||
};
|
||||
|
||||
return (
|
||||
<div className="space-y-2">
|
||||
{parts.map((part, index) => {
|
||||
if (part.type === 'text') {
|
||||
return (
|
||||
<span key={index} className="whitespace-pre-wrap">
|
||||
{part.content}
|
||||
</span>
|
||||
);
|
||||
} else {
|
||||
const filename = getFileName(part.filepath!);
|
||||
return (
|
||||
<div key={index} className="flex items-center gap-2">
|
||||
<Badge variant="secondary" className="flex items-center gap-1.5 px-2 py-1">
|
||||
{getFileIcon(filename)}
|
||||
<span className="text-xs font-medium">{filename}</span>
|
||||
<span className="text-xs text-muted-foreground">added to {part.filepath}</span>
|
||||
</Badge>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
})}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -58,14 +58,14 @@ export function FileAttachment({
|
||||
className="hidden"
|
||||
disabled={disabled}
|
||||
/>
|
||||
|
||||
|
||||
<Button
|
||||
type="button"
|
||||
variant="ghost"
|
||||
size="sm"
|
||||
onClick={handleButtonClick}
|
||||
disabled={disabled}
|
||||
className="h-8 w-8 p-0 hover:bg-muted/50"
|
||||
className="h-8 w-8 p-0 rounded-full hover:bg-muted/50"
|
||||
>
|
||||
<Paperclip className="h-4 w-4" />
|
||||
</Button>
|
||||
|
||||
Reference in new issue
Block a user