mirror of
https://gitlab.com/soapbox-pub/shakespeare.git
synced 2026-12-05 16:23:36 +00:00
Shakespeare
This commit is contained in:
14 files changed
+2405
-8
No files matched your search
@@ -0,0 +1,212 @@
|
||||
import { useState, useRef, useEffect } from 'react';
|
||||
import { createAIChat } from '@/lib/ai-chat';
|
||||
import { Button } from '@/components/ui/button';
|
||||
import { Textarea } from '@/components/ui/textarea';
|
||||
import { ScrollArea } from '@/components/ui/scroll-area';
|
||||
import { Card } from '@/components/ui/card';
|
||||
import { Send, Bot, User } from 'lucide-react';
|
||||
import { cn } from '@/lib/utils';
|
||||
|
||||
interface Message {
|
||||
id: string;
|
||||
role: 'user' | 'assistant';
|
||||
content: string;
|
||||
timestamp: Date;
|
||||
toolCalls?: any[];
|
||||
}
|
||||
|
||||
interface ChatPaneProps {
|
||||
projectId: string;
|
||||
projectName: string;
|
||||
}
|
||||
|
||||
export function ChatPane({ projectId, projectName }: ChatPaneProps) {
|
||||
const [messages, setMessages] = useState<Message[]>([]);
|
||||
const [input, setInput] = useState('');
|
||||
const [isLoading, setIsLoading] = useState(false);
|
||||
const scrollAreaRef = useRef<HTMLDivElement>(null);
|
||||
|
||||
useEffect(() => {
|
||||
// Add welcome message
|
||||
setMessages([
|
||||
{
|
||||
id: 'welcome',
|
||||
role: 'assistant',
|
||||
content: `Hello! I'm here to help you build "${projectName}". I can help you edit files, add new features, and build your Nostr website. What would you like to work on?`,
|
||||
timestamp: new Date(),
|
||||
},
|
||||
]);
|
||||
}, [projectName]);
|
||||
|
||||
useEffect(() => {
|
||||
if (scrollAreaRef.current) {
|
||||
scrollAreaRef.current.scrollTop = scrollAreaRef.current.scrollHeight;
|
||||
}
|
||||
}, [messages]);
|
||||
|
||||
const handleSend = async () => {
|
||||
if (!input.trim() || isLoading) return;
|
||||
|
||||
const userMessage: Message = {
|
||||
id: Date.now().toString(),
|
||||
role: 'user',
|
||||
content: input,
|
||||
timestamp: new Date(),
|
||||
};
|
||||
|
||||
setMessages((prev) => [...prev, userMessage]);
|
||||
setInput('');
|
||||
setIsLoading(true);
|
||||
|
||||
try {
|
||||
const aiMessages = [
|
||||
...messages.map((msg) => ({
|
||||
role: msg.role,
|
||||
content: msg.content,
|
||||
})),
|
||||
{ role: 'user' as const, content: input },
|
||||
];
|
||||
|
||||
const result = await createAIChat(projectId, aiMessages);
|
||||
|
||||
let assistantContent = '';
|
||||
const toolCalls: any[] = [];
|
||||
|
||||
for await (const part of result.fullStream) {
|
||||
if (part.type === 'text-delta') {
|
||||
assistantContent += part.textDelta;
|
||||
} else if (part.type === 'tool-call') {
|
||||
toolCalls.push(part);
|
||||
} else if (part.type === 'tool-result') {
|
||||
// Handle tool results if needed
|
||||
}
|
||||
}
|
||||
|
||||
const assistantMessage: Message = {
|
||||
id: (Date.now() + 1).toString(),
|
||||
role: 'assistant',
|
||||
content: assistantContent || 'I completed the requested changes.',
|
||||
timestamp: new Date(),
|
||||
toolCalls,
|
||||
};
|
||||
|
||||
setMessages((prev) => [...prev, assistantMessage]);
|
||||
} catch (error) {
|
||||
console.error('AI chat error:', error);
|
||||
const errorMessage: Message = {
|
||||
id: (Date.now() + 1).toString(),
|
||||
role: 'assistant',
|
||||
content: 'Sorry, I encountered an error. Please try again.',
|
||||
timestamp: new Date(),
|
||||
};
|
||||
setMessages((prev) => [...prev, errorMessage]);
|
||||
} finally {
|
||||
setIsLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
const handleKeyDown = (e: React.KeyboardEvent) => {
|
||||
if (e.key === 'Enter' && !e.shiftKey) {
|
||||
e.preventDefault();
|
||||
handleSend();
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<div className="h-full flex flex-col">
|
||||
<div className="border-b px-4 py-3">
|
||||
<h2 className="font-semibold">AI Assistant</h2>
|
||||
<p className="text-sm text-muted-foreground">Chat to build your project</p>
|
||||
</div>
|
||||
|
||||
<ScrollArea className="flex-1 p-4" ref={scrollAreaRef}>
|
||||
<div className="space-y-4">
|
||||
{messages.map((message) => (
|
||||
<div
|
||||
key={message.id}
|
||||
className={cn(
|
||||
'flex gap-3',
|
||||
message.role === 'user' ? 'justify-end' : 'justify-start'
|
||||
)}
|
||||
>
|
||||
<div
|
||||
className={cn(
|
||||
'flex gap-2 max-w-[80%]',
|
||||
message.role === 'user' ? 'flex-row-reverse' : 'flex-row'
|
||||
)}
|
||||
>
|
||||
<div
|
||||
className={cn(
|
||||
'w-8 h-8 rounded-full flex items-center justify-center flex-shrink-0',
|
||||
message.role === 'user'
|
||||
? 'bg-primary text-primary-foreground'
|
||||
: 'bg-muted'
|
||||
)}
|
||||
>
|
||||
{message.role === 'user' ? (
|
||||
<User className="h-4 w-4" />
|
||||
) : (
|
||||
<Bot className="h-4 w-4" />
|
||||
)}
|
||||
</div>
|
||||
<Card
|
||||
className={cn(
|
||||
'px-4 py-2',
|
||||
message.role === 'user'
|
||||
? 'bg-primary text-primary-foreground'
|
||||
: 'bg-muted'
|
||||
)}
|
||||
>
|
||||
<div className="whitespace-pre-wrap text-sm">
|
||||
{message.content}
|
||||
</div>
|
||||
{message.toolCalls && message.toolCalls.length > 0 && (
|
||||
<div className="mt-2 text-xs opacity-75">
|
||||
Used {message.toolCalls.length} tool{message.toolCalls.length !== 1 ? 's' : ''}
|
||||
</div>
|
||||
)}
|
||||
</Card>
|
||||
</div>
|
||||
</div>
|
||||
))}
|
||||
|
||||
{isLoading && (
|
||||
<div className="flex gap-2">
|
||||
<div className="w-8 h-8 rounded-full bg-muted flex items-center justify-center flex-shrink-0">
|
||||
<Bot className="h-4 w-4" />
|
||||
</div>
|
||||
<Card className="px-4 py-2 bg-muted">
|
||||
<div className="flex space-x-1">
|
||||
<div className="h-2 w-2 bg-current rounded-full animate-bounce [animation-delay:-0.3s]"></div>
|
||||
<div className="h-2 w-2 bg-current rounded-full animate-bounce [animation-delay:-0.15s]"></div>
|
||||
<div className="h-2 w-2 bg-current rounded-full animate-bounce"></div>
|
||||
</div>
|
||||
</Card>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</ScrollArea>
|
||||
|
||||
<div className="border-t p-4">
|
||||
<div className="flex gap-2">
|
||||
<Textarea
|
||||
value={input}
|
||||
onChange={(e) => setInput(e.target.value)}
|
||||
onKeyDown={handleKeyDown}
|
||||
placeholder="Ask me to add features, edit files, or build your project..."
|
||||
className="min-h-[60px] resize-none"
|
||||
disabled={isLoading}
|
||||
/>
|
||||
<Button
|
||||
onClick={handleSend}
|
||||
disabled={!input.trim() || isLoading}
|
||||
className="self-end"
|
||||
size="icon"
|
||||
>
|
||||
<Send className="h-4 w-4" />
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,121 @@
|
||||
import { useState, useEffect } from 'react';
|
||||
import { Button } from '@/components/ui/button';
|
||||
import { Textarea } from '@/components/ui/textarea';
|
||||
import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card';
|
||||
import { Save, Loader2 } from 'lucide-react';
|
||||
import { cn } from '@/lib/utils';
|
||||
|
||||
interface FileEditorProps {
|
||||
filePath: string;
|
||||
content: string;
|
||||
onSave: (content: string) => void;
|
||||
isLoading: boolean;
|
||||
}
|
||||
|
||||
export function FileEditor({ filePath, content, onSave, isLoading }: FileEditorProps) {
|
||||
const [editedContent, setEditedContent] = useState(content);
|
||||
const [isSaving, setIsSaving] = useState(false);
|
||||
const [hasChanges, setHasChanges] = useState(false);
|
||||
|
||||
useEffect(() => {
|
||||
setEditedContent(content);
|
||||
setHasChanges(false);
|
||||
}, [content, filePath]);
|
||||
|
||||
useEffect(() => {
|
||||
setHasChanges(editedContent !== content);
|
||||
}, [editedContent, content]);
|
||||
|
||||
const handleSave = async () => {
|
||||
setIsSaving(true);
|
||||
try {
|
||||
await onSave(editedContent);
|
||||
setHasChanges(false);
|
||||
} catch (error) {
|
||||
console.error('Failed to save file:', error);
|
||||
} finally {
|
||||
setIsSaving(false);
|
||||
}
|
||||
};
|
||||
|
||||
const getLanguageFromPath = (path: string): string => {
|
||||
const extension = path.split('.').pop()?.toLowerCase();
|
||||
const languageMap: Record<string, string> = {
|
||||
js: 'javascript',
|
||||
jsx: 'javascript',
|
||||
ts: 'typescript',
|
||||
tsx: 'typescript',
|
||||
html: 'html',
|
||||
css: 'css',
|
||||
json: 'json',
|
||||
md: 'markdown',
|
||||
py: 'python',
|
||||
java: 'java',
|
||||
cpp: 'cpp',
|
||||
c: 'c',
|
||||
sh: 'bash',
|
||||
yml: 'yaml',
|
||||
yaml: 'yaml',
|
||||
};
|
||||
return languageMap[extension || ''] || 'text';
|
||||
};
|
||||
|
||||
const getMonospaceFont = () => {
|
||||
return 'font-mono text-sm';
|
||||
};
|
||||
|
||||
return (
|
||||
<div className="h-full flex flex-col">
|
||||
<CardHeader className="border-b py-3">
|
||||
<div className="flex items-center justify-between">
|
||||
<CardTitle className="text-base">{filePath}</CardTitle>
|
||||
<div className="flex items-center space-x-2">
|
||||
{hasChanges && (
|
||||
<span className="text-sm text-muted-foreground">Unsaved changes</span>
|
||||
)}
|
||||
<Button
|
||||
onClick={handleSave}
|
||||
disabled={!hasChanges || isSaving || isLoading}
|
||||
size="sm"
|
||||
>
|
||||
{isSaving ? (
|
||||
<>
|
||||
<Loader2 className="mr-2 h-4 w-4 animate-spin" />
|
||||
Saving...
|
||||
</>
|
||||
) : (
|
||||
<>
|
||||
<Save className="mr-2 h-4 w-4" />
|
||||
Save
|
||||
</>
|
||||
)}
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
<div className="text-xs text-muted-foreground mt-1">
|
||||
Language: {getLanguageFromPath(filePath)}
|
||||
</div>
|
||||
</CardHeader>
|
||||
|
||||
<CardContent className="flex-1 p-0">
|
||||
{isLoading ? (
|
||||
<div className="flex items-center justify-center h-full">
|
||||
<div className="animate-spin rounded-full h-6 w-6 border-b-2 border-primary"></div>
|
||||
</div>
|
||||
) : (
|
||||
<Textarea
|
||||
value={editedContent}
|
||||
onChange={(e) => setEditedContent(e.target.value)}
|
||||
className={cn(
|
||||
'w-full h-full resize-none border-0 rounded-none',
|
||||
'focus:outline-none focus:ring-0',
|
||||
getMonospaceFont()
|
||||
)}
|
||||
placeholder={`// Edit ${filePath}...`}
|
||||
spellCheck={false}
|
||||
/>
|
||||
)}
|
||||
</CardContent>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,168 @@
|
||||
import { useState, useEffect } from 'react';
|
||||
import { fsManager } from '@/lib/fs';
|
||||
import { ChevronRight, ChevronDown, File, Folder } from 'lucide-react';
|
||||
import { cn } from '@/lib/utils';
|
||||
|
||||
interface FileTreeProps {
|
||||
projectId: string;
|
||||
onFileSelect: (filePath: string) => void;
|
||||
selectedFile: string | null;
|
||||
}
|
||||
|
||||
interface FileNode {
|
||||
name: string;
|
||||
path: string;
|
||||
type: 'file' | 'directory';
|
||||
children?: FileNode[];
|
||||
isOpen?: boolean;
|
||||
}
|
||||
|
||||
export function FileTree({ projectId, onFileSelect, selectedFile }: FileTreeProps) {
|
||||
const [tree, setTree] = useState<FileNode[]>([]);
|
||||
const [isLoading, setIsLoading] = useState(true);
|
||||
|
||||
useEffect(() => {
|
||||
loadFileTree();
|
||||
}, [projectId]);
|
||||
|
||||
const loadFileTree = async () => {
|
||||
setIsLoading(true);
|
||||
try {
|
||||
const structure = await buildFileTree(projectId, '');
|
||||
setTree(structure);
|
||||
} catch (error) {
|
||||
console.error('Failed to load file tree:', error);
|
||||
} finally {
|
||||
setIsLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
const buildFileTree = async (projectId: string, dirPath: string): Promise<FileNode[]> => {
|
||||
const items = await fsManager.listFiles(projectId, dirPath);
|
||||
const nodes: FileNode[] = [];
|
||||
|
||||
for (const item of items) {
|
||||
const itemPath = dirPath ? `${dirPath}/${item}` : item;
|
||||
const fullPath = `${fsManager['dir']}/${projectId}/${itemPath}`;
|
||||
|
||||
try {
|
||||
const stat = await fsManager['fs'].promises.stat(fullPath);
|
||||
|
||||
if (stat.isDirectory()) {
|
||||
const children = await buildFileTree(projectId, itemPath);
|
||||
nodes.push({
|
||||
name: item,
|
||||
path: itemPath,
|
||||
type: 'directory',
|
||||
children,
|
||||
isOpen: false,
|
||||
});
|
||||
} else {
|
||||
nodes.push({
|
||||
name: item,
|
||||
path: itemPath,
|
||||
type: 'file',
|
||||
});
|
||||
}
|
||||
} catch (error) {
|
||||
// Skip inaccessible items
|
||||
}
|
||||
}
|
||||
|
||||
return nodes.sort((a, b) => {
|
||||
if (a.type !== b.type) {
|
||||
return a.type === 'directory' ? -1 : 1;
|
||||
}
|
||||
return a.name.localeCompare(b.name);
|
||||
});
|
||||
};
|
||||
|
||||
const toggleDirectory = (path: string) => {
|
||||
const toggleNode = (nodes: FileNode[]): FileNode[] => {
|
||||
return nodes.map(node => {
|
||||
if (node.path === path && node.type === 'directory') {
|
||||
return { ...node, isOpen: !node.isOpen };
|
||||
}
|
||||
if (node.children) {
|
||||
return { ...node, children: toggleNode(node.children) };
|
||||
}
|
||||
return node;
|
||||
});
|
||||
};
|
||||
|
||||
setTree(prev => toggleNode(prev));
|
||||
};
|
||||
|
||||
const handleFileClick = (filePath: string, type: 'file' | 'directory') => {
|
||||
if (type === 'file') {
|
||||
onFileSelect(filePath);
|
||||
} else {
|
||||
toggleDirectory(filePath);
|
||||
}
|
||||
};
|
||||
|
||||
const renderNode = (node: FileNode, depth: number = 0) => {
|
||||
const isSelected = selectedFile === node.path;
|
||||
|
||||
return (
|
||||
<div key={node.path}>
|
||||
<div
|
||||
className={cn(
|
||||
'flex items-center gap-2 px-2 py-1 cursor-pointer hover:bg-muted rounded',
|
||||
isSelected && 'bg-muted',
|
||||
depth > 0 && 'ml-4'
|
||||
)}
|
||||
onClick={() => handleFileClick(node.path, node.type)}
|
||||
style={{ paddingLeft: `${8 + depth * 16}px` }}
|
||||
>
|
||||
{node.type === 'directory' ? (
|
||||
<>
|
||||
{node.isOpen ? (
|
||||
<ChevronDown className="h-4 w-4 flex-shrink-0" />
|
||||
) : (
|
||||
<ChevronRight className="h-4 w-4 flex-shrink-0" />
|
||||
)}
|
||||
<Folder className="h-4 w-4 text-blue-500 flex-shrink-0" />
|
||||
</>
|
||||
) : (
|
||||
<>
|
||||
<div className="w-4" /> {/* Spacer for alignment */}
|
||||
<File className="h-4 w-4 text-gray-500 flex-shrink-0" />
|
||||
</>
|
||||
)}
|
||||
<span className="text-sm truncate">{node.name}</span>
|
||||
</div>
|
||||
|
||||
{node.type === 'directory' && node.isOpen && node.children && (
|
||||
<div>
|
||||
{node.children.map(child => renderNode(child, depth + 1))}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
if (isLoading) {
|
||||
return (
|
||||
<div className="p-4">
|
||||
<div className="space-y-2">
|
||||
{[...Array(5)].map((_, i) => (
|
||||
<div key={i} className="h-6 bg-muted rounded animate-pulse"></div>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="p-2">
|
||||
{tree.length === 0 ? (
|
||||
<div className="text-center text-sm text-muted-foreground py-8">
|
||||
No files found
|
||||
</div>
|
||||
) : (
|
||||
tree.map(node => renderNode(node))
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,110 @@
|
||||
import { useState, useEffect } from 'react';
|
||||
import { fsManager } from '@/lib/fs';
|
||||
import { Tabs, TabsContent, TabsList, TabsTrigger } from '@/components/ui/tabs';
|
||||
import { ScrollArea } from '@/components/ui/scroll-area';
|
||||
import { Card } from '@/components/ui/card';
|
||||
import { FileTree } from './FileTree';
|
||||
import { FileEditor } from './FileEditor';
|
||||
|
||||
interface PreviewPaneProps {
|
||||
projectId: string;
|
||||
activeTab: 'preview' | 'code';
|
||||
}
|
||||
|
||||
export function PreviewPane({ projectId, activeTab }: PreviewPaneProps) {
|
||||
const [selectedFile, setSelectedFile] = useState<string | null>(null);
|
||||
const [fileContent, setFileContent] = useState<string>('');
|
||||
const [isLoading, setIsLoading] = useState(false);
|
||||
|
||||
useEffect(() => {
|
||||
if (selectedFile) {
|
||||
loadFileContent(selectedFile);
|
||||
}
|
||||
}, [selectedFile, projectId]);
|
||||
|
||||
const loadFileContent = async (filePath: string) => {
|
||||
setIsLoading(true);
|
||||
try {
|
||||
const content = await fsManager.readFile(projectId, filePath);
|
||||
setFileContent(content);
|
||||
} catch (error) {
|
||||
console.error('Failed to load file:', error);
|
||||
setFileContent('');
|
||||
} finally {
|
||||
setIsLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
const handleFileSelect = (filePath: string) => {
|
||||
setSelectedFile(filePath);
|
||||
};
|
||||
|
||||
const handleFileSave = async (content: string) => {
|
||||
if (!selectedFile) return;
|
||||
|
||||
try {
|
||||
await fsManager.writeFile(projectId, selectedFile, content);
|
||||
setFileContent(content);
|
||||
} catch (error) {
|
||||
console.error('Failed to save file:', error);
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<div className="h-full">
|
||||
<Tabs value={activeTab} className="h-full">
|
||||
<TabsContent value="preview" className="h-full mt-0">
|
||||
<div className="h-full flex items-center justify-center bg-muted">
|
||||
<div className="text-center">
|
||||
<h3 className="text-lg font-semibold mb-2">Project Preview</h3>
|
||||
<p className="text-muted-foreground mb-4">
|
||||
Your project will be built and displayed here
|
||||
</p>
|
||||
<div className="bg-background border rounded-lg p-8 max-w-md">
|
||||
<p className="text-sm text-muted-foreground">
|
||||
Vite build integration coming soon...
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</TabsContent>
|
||||
|
||||
<TabsContent value="code" className="h-full mt-0">
|
||||
<div className="h-full flex">
|
||||
<div className="w-1/3 border-r">
|
||||
<div className="p-4 border-b">
|
||||
<h3 className="font-semibold">File Explorer</h3>
|
||||
</div>
|
||||
<ScrollArea className="h-[calc(100%-60px)]">
|
||||
<FileTree
|
||||
projectId={projectId}
|
||||
onFileSelect={handleFileSelect}
|
||||
selectedFile={selectedFile}
|
||||
/>
|
||||
</ScrollArea>
|
||||
</div>
|
||||
|
||||
<div className="flex-1">
|
||||
{selectedFile ? (
|
||||
<FileEditor
|
||||
filePath={selectedFile}
|
||||
content={fileContent}
|
||||
onSave={handleFileSave}
|
||||
isLoading={isLoading}
|
||||
/>
|
||||
) : (
|
||||
<div className="h-full flex items-center justify-center">
|
||||
<div className="text-center">
|
||||
<p className="text-muted-foreground">
|
||||
Select a file from the explorer to edit
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
</TabsContent>
|
||||
</Tabs>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user