diff --git a/README.md b/README.md
index ffc92583..1874a138 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 with visual badges and deduplication
+- **File Attachments**: Attach files directly to chat messages with structured text parts, 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 18b7f55d..9c693988 100644
--- a/src/components/AIMessageItem.tsx
+++ b/src/components/AIMessageItem.tsx
@@ -206,11 +206,17 @@ export const AIMessageItem = memo(({
// Regular rendering for non-tool messages
if (message.role === 'user') {
// User messages: right-aligned bubble without avatar/name
+ const userContent = typeof message.content === 'string'
+ ? message.content
+ : Array.isArray(message.content)
+ ? message.content.filter(part => part.type === 'text') as OpenAI.Chat.Completions.ChatCompletionContentPartText[]
+ : '';
+
return (
diff --git a/src/components/Shakespeare/ChatPane.tsx b/src/components/Shakespeare/ChatPane.tsx
index d6aff05a..81b1b093 100644
--- a/src/components/Shakespeare/ChatPane.tsx
+++ b/src/components/Shakespeare/ChatPane.tsx
@@ -288,10 +288,20 @@ export const ChatPane = forwardRef(({
const handleSend = async () => {
if ((!input.trim() && attachedFiles.length === 0) || isLoading || !providerModel.trim()) return;
- let messageContent = input;
const modelToUse = providerModel.trim();
- // Process attached files
+ // Build message content as text parts
+ const contentParts: Array = [];
+
+ // Add user input as text part if present
+ if (input.trim()) {
+ contentParts.push({
+ type: 'text',
+ text: input.trim()
+ });
+ }
+
+ // Process attached files and add as separate text parts
if (attachedFiles.length > 0) {
const filePromises = attachedFiles.map(async (file) => {
try {
@@ -305,12 +315,13 @@ export const ChatPane = forwardRef(({
const fileResults = await Promise.all(filePromises);
- // Append file information to the message
- if (messageContent.trim()) {
- messageContent += '\n\n' + fileResults.join('\n');
- } else {
- messageContent = fileResults.join('\n');
- }
+ // Add each file as a separate text part
+ fileResults.forEach(fileResult => {
+ contentParts.push({
+ type: 'text',
+ text: fileResult
+ });
+ });
}
setInput('');
@@ -320,6 +331,8 @@ export const ChatPane = forwardRef(({
addRecentlyUsedModel(modelToUse);
try {
+ // Send as text parts if we have multiple parts, otherwise send as string for simplicity
+ const messageContent = contentParts.length === 1 ? contentParts[0].text : contentParts;
await sendMessage(messageContent, modelToUse);
} catch (error) {
console.error('AI chat error:', error);
diff --git a/src/components/UserMessage.integration.test.tsx b/src/components/UserMessage.integration.test.tsx
new file mode 100644
index 00000000..4c3dbd54
--- /dev/null
+++ b/src/components/UserMessage.integration.test.tsx
@@ -0,0 +1,59 @@
+import { describe, it, expect } from 'vitest';
+import { render, screen } from '@testing-library/react';
+import { UserMessage } from './UserMessage';
+
+describe('UserMessage Integration', () => {
+ it('renders text parts with file attachments correctly', () => {
+ // Simulate the new text parts structure that ChatPane would create
+ const content = [
+ { type: 'text' as const, text: 'Please analyze this file:' },
+ { type: 'text' as const, text: 'File added to /tmp/data.csv' },
+ { type: 'text' as const, text: 'Let me know what you find!' }
+ ];
+
+ render();
+
+ // Check that regular text is displayed
+ expect(screen.getByText('Please analyze this file:')).toBeInTheDocument();
+ expect(screen.getByText('Let me know what you find!')).toBeInTheDocument();
+
+ // Check that file attachment is rendered as a badge
+ expect(screen.getByText('data.csv')).toBeInTheDocument();
+ expect(screen.getByText('added to /tmp/data.csv')).toBeInTheDocument();
+ });
+
+ it('handles single text part with file attachment', () => {
+ const content = [
+ { type: 'text' as const, text: 'File added to /tmp/script.js' }
+ ];
+
+ render();
+
+ expect(screen.getByText('script.js')).toBeInTheDocument();
+ expect(screen.getByText('added to /tmp/script.js')).toBeInTheDocument();
+ });
+
+ it('handles multiple file attachments in separate parts', () => {
+ const content = [
+ { type: 'text' as const, text: 'Here are my files:' },
+ { type: 'text' as const, text: 'File added to /tmp/config.json' },
+ { type: 'text' as const, text: 'File added to /tmp/styles.css' },
+ { type: 'text' as const, text: 'File added to /tmp/main.js' }
+ ];
+
+ render();
+
+ expect(screen.getByText('Here are my files:')).toBeInTheDocument();
+ expect(screen.getByText('config.json')).toBeInTheDocument();
+ expect(screen.getByText('styles.css')).toBeInTheDocument();
+ expect(screen.getByText('main.js')).toBeInTheDocument();
+ });
+
+ it('handles empty content gracefully', () => {
+ const content: Array<{ type: 'text'; text: string }> = [];
+
+ const { container } = render();
+
+ expect(container.firstChild).toBeNull();
+ });
+});
\ No newline at end of file
diff --git a/src/components/UserMessage.test.tsx b/src/components/UserMessage.test.tsx
index 0c392d4d..440aa8d5 100644
--- a/src/components/UserMessage.test.tsx
+++ b/src/components/UserMessage.test.tsx
@@ -11,18 +11,35 @@ describe('UserMessage', () => {
expect(screen.getByText(content)).toBeInTheDocument();
});
- it('renders file attachment badges for single file', () => {
+ it('handles backward compatibility with string content containing file attachments', () => {
const content = 'Here is my file: File added to /tmp/test.txt';
render();
+ // When using string content, it should still work but won't separate file attachments
+ expect(screen.getByText(content)).toBeInTheDocument();
+ });
+
+ it('renders file attachment badges for single file with text parts', () => {
+ const content = [
+ { type: 'text' as const, text: 'Here is my file:' },
+ { type: 'text' as const, text: '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!';
+ it('renders multiple file attachments with text parts', () => {
+ const content = [
+ { type: 'text' as const, text: 'Files uploaded:' },
+ { type: 'text' as const, text: 'File added to /tmp/document.pdf' },
+ { type: 'text' as const, text: 'File added to /tmp/image.jpg' },
+ { type: 'text' as const, text: 'All done!' }
+ ];
render();
@@ -33,7 +50,7 @@ describe('UserMessage', () => {
});
it('renders text files with FileText icon', () => {
- const content = 'File added to /tmp/script.js';
+ const content = [{ type: 'text' as const, text: 'File added to /tmp/script.js' }];
render();
@@ -42,7 +59,7 @@ describe('UserMessage', () => {
});
it('handles files without extensions', () => {
- const content = 'File added to /tmp/README';
+ const content = [{ type: 'text' as const, text: 'File added to /tmp/README' }];
render();
@@ -51,7 +68,7 @@ describe('UserMessage', () => {
});
it('handles complex filenames with underscores and numbers', () => {
- const content = 'File added to /tmp/my_file_1.txt';
+ const content = [{ type: 'text' as const, text: 'File added to /tmp/my_file_1.txt' }];
render();
@@ -60,7 +77,10 @@ describe('UserMessage', () => {
});
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 content = [
+ { type: 'text' as const, text: 'Line 1\n\nLine 3 with spaces \n\nFinal line' },
+ { type: 'text' as const, text: 'File added to /tmp/test.txt' }
+ ];
const { container } = render();
@@ -72,7 +92,11 @@ describe('UserMessage', () => {
});
it('handles mixed content with file attachments in the middle', () => {
- const content = 'Starting text File added to /tmp/middle.txt ending text';
+ const content = [
+ { type: 'text' as const, text: 'Starting text' },
+ { type: 'text' as const, text: 'File added to /tmp/middle.txt' },
+ { type: 'text' as const, text: 'ending text' }
+ ];
render();
diff --git a/src/components/UserMessage.tsx b/src/components/UserMessage.tsx
index 0b3c547c..8e219de4 100644
--- a/src/components/UserMessage.tsx
+++ b/src/components/UserMessage.tsx
@@ -1,51 +1,45 @@
import { FileText, File } from 'lucide-react';
import { Badge } from '@/components/ui/badge';
+import type OpenAI from 'openai';
interface UserMessageProps {
- content: string;
+ content: string | Array;
}
/**
* Component to render user messages nicely
+ * Handles both string content and array content (text parts)
* Detects "File added to /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 "File added to /tmp/" patterns
- const filePattern = /File added to (\/tmp\/[^\s\n]+)/g;
+ const filePattern = /^File added to (\/tmp\/[^\s\n]+)$/;
- // Split content by file attachments to render them separately
- const parts: Array<{ type: 'text' | 'file'; content: string; filepath?: string }> = [];
- let lastIndex = 0;
- let match;
+ // Process each content part and categorize as text or file
+ const processedParts: Array<{ type: 'text' | 'file'; content: string; filepath?: string }> = [];
- 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 });
- }
+ 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 });
}
+ });
- // 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};
+ // If no content, return empty
+ if (processedParts.length === 0) {
+ return null;
}
const getFileIcon = (filename: string) => {
@@ -67,12 +61,12 @@ export function UserMessage({ content }: UserMessageProps) {
return (
- {parts.map((part, index) => {
+ {processedParts.map((part, index) => {
if (part.type === 'text') {
return (
-
+
{part.content}
-
+
);
} else {
const filename = getFileName(part.filepath!);
diff --git a/src/hooks/useAIChat.ts b/src/hooks/useAIChat.ts
index 52af4cdb..1011a4eb 100644
--- a/src/hooks/useAIChat.ts
+++ b/src/hooks/useAIChat.ts
@@ -111,7 +111,7 @@ export function useAIChat({
}, [projectId]);
// Actions
- const sendMessage = useCallback(async (content: string, providerModel: string) => {
+ const sendMessage = useCallback(async (content: string | Array, providerModel: string) => {
await initSession();
await sessionManager.sendMessage(projectId, content, providerModel);
}, [projectId, sessionManager, initSession]);
diff --git a/src/lib/SessionManager.ts b/src/lib/SessionManager.ts
index 176c1d23..fd0cc14e 100644
--- a/src/lib/SessionManager.ts
+++ b/src/lib/SessionManager.ts
@@ -160,7 +160,7 @@ export class SessionManager {
/**
* Send a message and start AI generation
*/
- async sendMessage(projectId: string, content: string, providerModel: string): Promise {
+ async sendMessage(projectId: string, content: string | Array, providerModel: string): Promise {
const session = this.sessions.get(projectId);
if (!session || session.isLoading) return;