mirror of
https://gitlab.com/soapbox-pub/shakespeare.git
synced 2026-12-06 00:27:24 +00:00
Support adding files through the VFS
This commit is contained in:
6 files changed
+478
-6
No files matched your search
@@ -5,6 +5,7 @@ import { Textarea } from '@/components/ui/textarea';
|
||||
import { Skeleton } from '@/components/ui/skeleton';
|
||||
import { CircularProgress } from '@/components/ui/circular-progress';
|
||||
import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger } from '@/components/ui/tooltip';
|
||||
import { FileAttachment } from '@/components/ui/file-attachment';
|
||||
import { Square, Loader2, ChevronDown, ArrowUp } from 'lucide-react';
|
||||
import { useAISettings } from '@/hooks/useAISettings';
|
||||
import { useFS } from '@/hooks/useFS';
|
||||
@@ -36,6 +37,7 @@ import { Tool } from '@/lib/tools/Tool';
|
||||
import OpenAI from 'openai';
|
||||
import { makeSystemPrompt } from '@/lib/system';
|
||||
import { assistantContentEmpty } from '@/lib/ai-messages';
|
||||
import { saveFileToTmp } from '@/lib/fileUtils';
|
||||
|
||||
interface ChatPaneProps {
|
||||
projectId: string;
|
||||
@@ -67,6 +69,7 @@ export const ChatPane = forwardRef<ChatPaneRef, ChatPaneProps>(({
|
||||
const [input, setInput] = useState('');
|
||||
const [systemPrompt, setSystemPrompt] = useState('');
|
||||
const [showScrollToBottom, setShowScrollToBottom] = useState(false);
|
||||
const [attachedFiles, setAttachedFiles] = useState<File[]>([]);
|
||||
|
||||
// Use external state if provided, otherwise default to false
|
||||
const isBuildLoading = externalIsBuildLoading || false;
|
||||
@@ -274,12 +277,44 @@ export const ChatPane = forwardRef<ChatPaneRef, ChatPaneProps>(({
|
||||
}
|
||||
}, [messages, streamingMessage, isLoading]);
|
||||
|
||||
const handleSend = async () => {
|
||||
if (!input.trim() || isLoading || !providerModel.trim()) return;
|
||||
const handleFileSelect = (file: File) => {
|
||||
setAttachedFiles(prev => [...prev, file]);
|
||||
};
|
||||
|
||||
const messageContent = input;
|
||||
const handleFileRemove = (fileToRemove: File) => {
|
||||
setAttachedFiles(prev => prev.filter(file => file !== fileToRemove));
|
||||
};
|
||||
|
||||
const handleSend = async () => {
|
||||
if ((!input.trim() && attachedFiles.length === 0) || isLoading || !providerModel.trim()) return;
|
||||
|
||||
let messageContent = input;
|
||||
const modelToUse = providerModel.trim();
|
||||
|
||||
// Process attached files
|
||||
if (attachedFiles.length > 0) {
|
||||
const filePromises = attachedFiles.map(async (file) => {
|
||||
try {
|
||||
const savedPath = await saveFileToTmp(fs, file);
|
||||
return `File added to ${savedPath}`;
|
||||
} catch (error) {
|
||||
console.error('Failed to save file:', error);
|
||||
return `Failed to save file: ${file.name}`;
|
||||
}
|
||||
});
|
||||
|
||||
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');
|
||||
}
|
||||
}
|
||||
|
||||
setInput('');
|
||||
setAttachedFiles([]);
|
||||
|
||||
// Add model to recently used when sending a message
|
||||
addRecentlyUsedModel(modelToUse);
|
||||
@@ -464,6 +499,15 @@ export const ChatPane = forwardRef<ChatPaneRef, ChatPaneProps>(({
|
||||
|
||||
{/* Bottom Controls Row */}
|
||||
<div className="absolute bottom-2 left-2 right-2 flex items-center gap-4">
|
||||
{/* File Attachment */}
|
||||
<FileAttachment
|
||||
onFileSelect={handleFileSelect}
|
||||
onFileRemove={handleFileRemove}
|
||||
selectedFiles={attachedFiles}
|
||||
disabled={isLoading}
|
||||
multiple={true}
|
||||
/>
|
||||
|
||||
{/* Context Usage Wheel */}
|
||||
{contextUsagePercentage >= 10 && currentModel?.contextLength && lastInputTokens > 0 && (
|
||||
<TooltipProvider>
|
||||
@@ -526,7 +570,7 @@ export const ChatPane = forwardRef<ChatPaneRef, ChatPaneProps>(({
|
||||
<Button
|
||||
onClick={handleSend}
|
||||
onMouseDown={handleFirstInteraction}
|
||||
disabled={!input.trim() || !providerModel.trim()}
|
||||
disabled={(!input.trim() && attachedFiles.length === 0) || !providerModel.trim()}
|
||||
size="sm"
|
||||
className="size-8 [&_svg]:size-5 rounded-full p-0"
|
||||
>
|
||||
|
||||
@@ -0,0 +1,102 @@
|
||||
import { describe, it, expect, vi } from 'vitest';
|
||||
import { render, screen, fireEvent } from '@testing-library/react';
|
||||
import { FileAttachment } from './file-attachment';
|
||||
|
||||
describe('FileAttachment', () => {
|
||||
it('renders the attachment button', () => {
|
||||
const onFileSelect = vi.fn();
|
||||
|
||||
render(
|
||||
<FileAttachment onFileSelect={onFileSelect} />
|
||||
);
|
||||
|
||||
const button = screen.getByRole('button');
|
||||
expect(button).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('calls onFileSelect when file is selected', async () => {
|
||||
const onFileSelect = vi.fn();
|
||||
|
||||
render(
|
||||
<FileAttachment onFileSelect={onFileSelect} />
|
||||
);
|
||||
|
||||
const button = screen.getByRole('button');
|
||||
fireEvent.click(button);
|
||||
|
||||
// Get the hidden file input
|
||||
const fileInput = document.querySelector('input[type="file"]') as HTMLInputElement;
|
||||
expect(fileInput).toBeInTheDocument();
|
||||
|
||||
// Create a mock file
|
||||
const file = new File(['test content'], 'test.txt', { type: 'text/plain' });
|
||||
|
||||
// Mock the file input change event
|
||||
Object.defineProperty(fileInput, 'files', {
|
||||
value: [file],
|
||||
writable: false,
|
||||
});
|
||||
|
||||
fireEvent.change(fileInput);
|
||||
|
||||
expect(onFileSelect).toHaveBeenCalledWith(file);
|
||||
});
|
||||
|
||||
it('displays selected files', () => {
|
||||
const onFileSelect = vi.fn();
|
||||
const onFileRemove = vi.fn();
|
||||
const selectedFiles = [
|
||||
new File(['content1'], 'file1.txt', { type: 'text/plain' }),
|
||||
new File(['content2'], 'file2.txt', { type: 'text/plain' }),
|
||||
];
|
||||
|
||||
render(
|
||||
<FileAttachment
|
||||
onFileSelect={onFileSelect}
|
||||
onFileRemove={onFileRemove}
|
||||
selectedFiles={selectedFiles}
|
||||
/>
|
||||
);
|
||||
|
||||
expect(screen.getByText('file1.txt')).toBeInTheDocument();
|
||||
expect(screen.getByText('file2.txt')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('calls onFileRemove when remove button is clicked', () => {
|
||||
const onFileSelect = vi.fn();
|
||||
const onFileRemove = vi.fn();
|
||||
const selectedFiles = [
|
||||
new File(['content'], 'test.txt', { type: 'text/plain' }),
|
||||
];
|
||||
|
||||
render(
|
||||
<FileAttachment
|
||||
onFileSelect={onFileSelect}
|
||||
onFileRemove={onFileRemove}
|
||||
selectedFiles={selectedFiles}
|
||||
/>
|
||||
);
|
||||
|
||||
const removeButtons = screen.getAllByRole('button');
|
||||
// First button is the attachment button, second is the remove button
|
||||
const removeButton = removeButtons[1];
|
||||
|
||||
fireEvent.click(removeButton);
|
||||
|
||||
expect(onFileRemove).toHaveBeenCalledWith(selectedFiles[0]);
|
||||
});
|
||||
|
||||
it('disables interaction when disabled prop is true', () => {
|
||||
const onFileSelect = vi.fn();
|
||||
|
||||
render(
|
||||
<FileAttachment onFileSelect={onFileSelect} disabled={true} />
|
||||
);
|
||||
|
||||
const button = screen.getByRole('button');
|
||||
expect(button).toBeDisabled();
|
||||
|
||||
const fileInput = document.querySelector('input[type="file"]') as HTMLInputElement;
|
||||
expect(fileInput).toBeDisabled();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,100 @@
|
||||
import React, { useRef } from 'react';
|
||||
import { Button } from '@/components/ui/button';
|
||||
import { Paperclip, X } from 'lucide-react';
|
||||
import { cn } from '@/lib/utils';
|
||||
|
||||
interface FileAttachmentProps {
|
||||
onFileSelect: (file: File) => void;
|
||||
onFileRemove?: (file: File) => void;
|
||||
selectedFiles?: File[];
|
||||
disabled?: boolean;
|
||||
accept?: string;
|
||||
multiple?: boolean;
|
||||
className?: string;
|
||||
}
|
||||
|
||||
export function FileAttachment({
|
||||
onFileSelect,
|
||||
onFileRemove,
|
||||
selectedFiles = [],
|
||||
disabled = false,
|
||||
accept,
|
||||
multiple = false,
|
||||
className
|
||||
}: FileAttachmentProps) {
|
||||
const fileInputRef = useRef<HTMLInputElement>(null);
|
||||
|
||||
const handleButtonClick = () => {
|
||||
fileInputRef.current?.click();
|
||||
};
|
||||
|
||||
const handleFileChange = (event: React.ChangeEvent<HTMLInputElement>) => {
|
||||
const files = event.target.files;
|
||||
if (files) {
|
||||
Array.from(files).forEach(file => {
|
||||
onFileSelect(file);
|
||||
});
|
||||
}
|
||||
// Reset input so the same file can be selected again
|
||||
if (fileInputRef.current) {
|
||||
fileInputRef.current.value = '';
|
||||
}
|
||||
};
|
||||
|
||||
const handleRemoveFile = (file: File) => {
|
||||
if (onFileRemove) {
|
||||
onFileRemove(file);
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<div className={cn("flex items-center gap-2", className)}>
|
||||
<input
|
||||
ref={fileInputRef}
|
||||
type="file"
|
||||
onChange={handleFileChange}
|
||||
accept={accept}
|
||||
multiple={multiple}
|
||||
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"
|
||||
>
|
||||
<Paperclip className="h-4 w-4" />
|
||||
</Button>
|
||||
|
||||
{selectedFiles.length > 0 && (
|
||||
<div className="flex flex-wrap gap-1 max-w-48">
|
||||
{selectedFiles.map((file, index) => (
|
||||
<div
|
||||
key={`${file.name}-${index}`}
|
||||
className="flex items-center gap-1 px-2 py-1 bg-muted/50 rounded-md text-xs"
|
||||
>
|
||||
<span className="truncate max-w-24" title={file.name}>
|
||||
{file.name}
|
||||
</span>
|
||||
{onFileRemove && (
|
||||
<Button
|
||||
type="button"
|
||||
variant="ghost"
|
||||
size="sm"
|
||||
onClick={() => handleRemoveFile(file)}
|
||||
className="h-3 w-3 p-0 hover:bg-muted/70"
|
||||
>
|
||||
<X className="h-2 w-2" />
|
||||
</Button>
|
||||
)}
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user