mirror of
https://gitlab.com/soapbox-pub/shakespeare.git
synced 2026-12-06 00:27:24 +00:00
Add AI settings
This commit is contained in:
12 files changed
+527
-40
No files matched your search
@@ -1,11 +1,14 @@
|
||||
import { useState, useRef, useEffect } from 'react';
|
||||
import { createAIChat } from '@/lib/ai-chat';
|
||||
import { streamText, tool } from 'ai';
|
||||
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 { Send, Bot, User, Settings } from 'lucide-react';
|
||||
import { cn } from '@/lib/utils';
|
||||
import { useAISettings } from '@/contexts/AISettingsContext';
|
||||
import { aiTools } from '@/lib/ai-tools';
|
||||
import { z } from 'zod';
|
||||
|
||||
interface Message {
|
||||
id: string;
|
||||
@@ -25,6 +28,7 @@ export function ChatPane({ projectId, projectName }: ChatPaneProps) {
|
||||
const [input, setInput] = useState('');
|
||||
const [isLoading, setIsLoading] = useState(false);
|
||||
const scrollAreaRef = useRef<HTMLDivElement>(null);
|
||||
const { settings, isConfigured } = useAISettings();
|
||||
|
||||
useEffect(() => {
|
||||
// Add welcome message
|
||||
@@ -44,6 +48,158 @@ export function ChatPane({ projectId, projectName }: ChatPaneProps) {
|
||||
}
|
||||
}, [messages]);
|
||||
|
||||
const createAIChat = async (projectId: string, messages: any[]) => {
|
||||
if (!isConfigured) {
|
||||
throw new Error('AI settings not configured');
|
||||
}
|
||||
|
||||
const fileOperationSchema = z.object({
|
||||
projectId: z.string(),
|
||||
filePath: z.string(),
|
||||
content: z.string().optional(),
|
||||
});
|
||||
|
||||
const buildSchema = z.object({
|
||||
projectId: z.string(),
|
||||
});
|
||||
|
||||
// Create a custom fetch function for the AI provider
|
||||
const customFetch = async (url: string, options: RequestInit) => {
|
||||
return fetch(url, {
|
||||
...options,
|
||||
headers: {
|
||||
...options.headers,
|
||||
'Authorization': `Bearer ${settings.apiKey}`,
|
||||
},
|
||||
});
|
||||
};
|
||||
|
||||
// Import the appropriate provider based on the base URL
|
||||
const { createOpenAI } = await import('@ai-sdk/openai');
|
||||
const openai = createOpenAI({
|
||||
baseURL: settings.baseUrl,
|
||||
apiKey: settings.apiKey,
|
||||
fetch: settings.baseUrl.includes('openrouter.ai') ? customFetch : undefined,
|
||||
});
|
||||
const provider = openai(settings.model);
|
||||
|
||||
return streamText({
|
||||
model: provider,
|
||||
messages,
|
||||
tools: {
|
||||
readFile: tool({
|
||||
description: 'Read the contents of a file in the project',
|
||||
parameters: fileOperationSchema,
|
||||
execute: async ({ projectId, filePath }) => {
|
||||
return await aiTools.executeOperation({
|
||||
type: 'read',
|
||||
projectId,
|
||||
filePath,
|
||||
});
|
||||
},
|
||||
}),
|
||||
writeFile: tool({
|
||||
description: 'Write content to a file in the project',
|
||||
parameters: fileOperationSchema,
|
||||
execute: async ({ projectId, filePath, content }) => {
|
||||
return await aiTools.executeOperation({
|
||||
type: 'write',
|
||||
projectId,
|
||||
filePath,
|
||||
content: content || '',
|
||||
});
|
||||
},
|
||||
}),
|
||||
deleteFile: tool({
|
||||
description: 'Delete a file from the project',
|
||||
parameters: fileOperationSchema,
|
||||
execute: async ({ projectId, filePath }) => {
|
||||
return await aiTools.executeOperation({
|
||||
type: 'delete',
|
||||
projectId,
|
||||
filePath,
|
||||
});
|
||||
},
|
||||
}),
|
||||
listFiles: tool({
|
||||
description: 'List files in a directory',
|
||||
parameters: fileOperationSchema,
|
||||
execute: async ({ projectId, filePath }) => {
|
||||
return await aiTools.executeOperation({
|
||||
type: 'list',
|
||||
projectId,
|
||||
filePath,
|
||||
});
|
||||
},
|
||||
}),
|
||||
fileExists: tool({
|
||||
description: 'Check if a file exists',
|
||||
parameters: fileOperationSchema,
|
||||
execute: async ({ projectId, filePath }) => {
|
||||
return await aiTools.executeOperation({
|
||||
type: 'exists',
|
||||
projectId,
|
||||
filePath,
|
||||
});
|
||||
},
|
||||
}),
|
||||
buildProject: tool({
|
||||
description: 'Build the project using Vite',
|
||||
parameters: buildSchema,
|
||||
execute: async ({ projectId }) => {
|
||||
return await aiTools.executeOperation({
|
||||
type: 'build',
|
||||
projectId,
|
||||
});
|
||||
},
|
||||
}),
|
||||
getProjectStructure: tool({
|
||||
description: 'Get the complete file structure of the project',
|
||||
parameters: buildSchema,
|
||||
execute: async ({ projectId }) => {
|
||||
return await aiTools.getProjectStructure(projectId);
|
||||
},
|
||||
}),
|
||||
searchFiles: tool({
|
||||
description: 'Search for files containing specific text',
|
||||
parameters: z.object({
|
||||
projectId: z.string(),
|
||||
query: z.string(),
|
||||
}),
|
||||
execute: async ({ projectId, query }) => {
|
||||
return await aiTools.searchFiles(projectId, query);
|
||||
},
|
||||
}),
|
||||
},
|
||||
system: `You are an AI assistant helping users build custom Nostr websites. You have access to tools that allow you to read, write, and manage files in the project, as well as build the project.
|
||||
|
||||
Key capabilities:
|
||||
- Read and write files in the project
|
||||
- List directory contents
|
||||
- Check if files exist
|
||||
- Build the project using Vite
|
||||
- Get project structure overview
|
||||
- Search through files
|
||||
|
||||
Guidelines:
|
||||
- Always check if files exist before writing to avoid overwriting
|
||||
- Provide helpful explanations of what you're doing
|
||||
- Suggest improvements and best practices
|
||||
- Use modern React patterns and TypeScript
|
||||
- Follow Nostr protocol standards when relevant
|
||||
- Ensure the project builds successfully
|
||||
|
||||
The project uses:
|
||||
- React 18 with TypeScript
|
||||
- Vite for building
|
||||
- TailwindCSS for styling
|
||||
- Nostrify for Nostr integration
|
||||
- shadcn/ui components
|
||||
|
||||
When creating new components or pages, follow the existing patterns in the codebase.`
|
||||
});
|
||||
};
|
||||
|
||||
const handleSend = async () => {
|
||||
if (!input.trim() || isLoading) return;
|
||||
|
||||
@@ -96,7 +252,7 @@ export function ChatPane({ projectId, projectName }: ChatPaneProps) {
|
||||
const errorMessage: Message = {
|
||||
id: (Date.now() + 1).toString(),
|
||||
role: 'assistant',
|
||||
content: 'Sorry, I encountered an error. Please try again.',
|
||||
content: error instanceof Error ? error.message : 'Sorry, I encountered an error. Please try again.',
|
||||
timestamp: new Date(),
|
||||
};
|
||||
setMessages((prev) => [...prev, errorMessage]);
|
||||
@@ -112,11 +268,75 @@ export function ChatPane({ projectId, projectName }: ChatPaneProps) {
|
||||
}
|
||||
};
|
||||
|
||||
if (!isConfigured) {
|
||||
return (
|
||||
<div className="h-full flex flex-col">
|
||||
<div className="border-b px-4 py-3 flex justify-between items-center">
|
||||
<div>
|
||||
<h2 className="font-semibold">AI Assistant</h2>
|
||||
<p className="text-sm text-muted-foreground">Chat to build your project</p>
|
||||
</div>
|
||||
<Button
|
||||
variant="outline"
|
||||
size="sm"
|
||||
className="gap-2"
|
||||
onClick={() => {
|
||||
// This will be handled by the AISettingsDialog component
|
||||
const event = new CustomEvent('openAISettings');
|
||||
window.dispatchEvent(event);
|
||||
}}
|
||||
>
|
||||
<Settings className="h-4 w-4" />
|
||||
Settings
|
||||
</Button>
|
||||
</div>
|
||||
|
||||
<div className="flex-1 flex items-center justify-center">
|
||||
<div className="text-center space-y-4">
|
||||
<Bot className="h-12 w-12 mx-auto text-muted-foreground" />
|
||||
<div>
|
||||
<h3 className="text-lg font-semibold mb-2">AI Assistant Not Configured</h3>
|
||||
<p className="text-sm text-muted-foreground mb-4">
|
||||
Please configure your AI settings to start building with AI assistance.
|
||||
</p>
|
||||
<Button
|
||||
variant="outline"
|
||||
size="sm"
|
||||
className="gap-2"
|
||||
onClick={() => {
|
||||
const event = new CustomEvent('openAISettings');
|
||||
window.dispatchEvent(event);
|
||||
}}
|
||||
>
|
||||
<Settings className="h-4 w-4" />
|
||||
Configure AI Settings
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
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 className="border-b px-4 py-3 flex justify-between items-center">
|
||||
<div>
|
||||
<h2 className="font-semibold">AI Assistant</h2>
|
||||
<p className="text-sm text-muted-foreground">Chat to build your project</p>
|
||||
</div>
|
||||
<Button
|
||||
variant="outline"
|
||||
size="sm"
|
||||
className="gap-2"
|
||||
onClick={() => {
|
||||
const event = new CustomEvent('openAISettings');
|
||||
window.dispatchEvent(event);
|
||||
}}
|
||||
>
|
||||
<Settings className="h-4 w-4" />
|
||||
Settings
|
||||
</Button>
|
||||
</div>
|
||||
|
||||
<ScrollArea className="flex-1 p-4" ref={scrollAreaRef}>
|
||||
@@ -209,4 +429,4 @@ export function ChatPane({ projectId, projectName }: ChatPaneProps) {
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
import { useState } from 'react';
|
||||
import { Settings } from 'lucide-react';
|
||||
import { Button } from '@/components/ui/button';
|
||||
import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle, DialogTrigger } from '@/components/ui/dialog';
|
||||
import { Input } from '@/components/ui/input';
|
||||
import { Label } from '@/components/ui/label';
|
||||
import { useAISettings } from '@/contexts/AISettingsContext';
|
||||
|
||||
export function AISettingsDialog() {
|
||||
const { settings, updateSettings, isConfigured } = useAISettings();
|
||||
const [open, setOpen] = useState(false);
|
||||
const [localSettings, setLocalSettings] = useState(settings);
|
||||
|
||||
const handleSave = () => {
|
||||
updateSettings(localSettings);
|
||||
setOpen(false);
|
||||
};
|
||||
|
||||
const handleCancel = () => {
|
||||
setLocalSettings(settings);
|
||||
setOpen(false);
|
||||
};
|
||||
|
||||
return (
|
||||
<Dialog open={open} onOpenChange={setOpen}>
|
||||
<DialogTrigger asChild>
|
||||
<Button variant="ghost" size="sm" className="gap-2">
|
||||
<Settings className="h-4 w-4" />
|
||||
AI Settings
|
||||
{!isConfigured && (
|
||||
<span className="ml-1 h-2 w-2 rounded-full bg-red-500" />
|
||||
)}
|
||||
</Button>
|
||||
</DialogTrigger>
|
||||
<DialogContent className="sm:max-w-[425px]">
|
||||
<DialogHeader>
|
||||
<DialogTitle>AI Settings</DialogTitle>
|
||||
<DialogDescription>
|
||||
Configure your AI provider settings. These settings are stored locally in your browser.
|
||||
</DialogDescription>
|
||||
</DialogHeader>
|
||||
<div className="grid gap-4 py-4">
|
||||
<div className="grid gap-2">
|
||||
<Label htmlFor="apiKey">API Key</Label>
|
||||
<Input
|
||||
id="apiKey"
|
||||
type="password"
|
||||
placeholder="Enter your API key"
|
||||
value={localSettings.apiKey}
|
||||
onChange={(e) => setLocalSettings({ ...localSettings, apiKey: e.target.value })}
|
||||
/>
|
||||
{!localSettings.apiKey && (
|
||||
<p className="text-sm text-muted-foreground">
|
||||
Required for AI functionality
|
||||
</p>
|
||||
)}
|
||||
</div>
|
||||
<div className="grid gap-2">
|
||||
<Label htmlFor="baseUrl">Base URL</Label>
|
||||
<Input
|
||||
id="baseUrl"
|
||||
placeholder="https://openrouter.ai/api/v1"
|
||||
value={localSettings.baseUrl}
|
||||
onChange={(e) => setLocalSettings({ ...localSettings, baseUrl: e.target.value })}
|
||||
/>
|
||||
</div>
|
||||
<div className="grid gap-2">
|
||||
<Label htmlFor="model">Model</Label>
|
||||
<Input
|
||||
id="model"
|
||||
placeholder="anthropic/claude-sonnet-4"
|
||||
value={localSettings.model}
|
||||
onChange={(e) => setLocalSettings({ ...localSettings, model: e.target.value })}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
<div className="flex justify-end gap-2">
|
||||
<Button variant="outline" onClick={handleCancel}>
|
||||
Cancel
|
||||
</Button>
|
||||
<Button onClick={handleSave}>
|
||||
Save Settings
|
||||
</Button>
|
||||
</div>
|
||||
</DialogContent>
|
||||
</Dialog>
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user