mirror of
https://gitlab.com/soapbox-pub/shakespeare.git
synced 2026-12-05 16:23:36 +00:00
Add model selector
This commit is contained in:
2 files changed
+182
-11
No files matched your search
@@ -0,0 +1,158 @@
|
||||
import { useState, useEffect, useMemo } from 'react';
|
||||
import { Check, ChevronDown, Edit3 } from 'lucide-react';
|
||||
import { Button } from '@/components/ui/button';
|
||||
import { Input } from '@/components/ui/input';
|
||||
import { Popover, PopoverContent, PopoverTrigger } from '@/components/ui/popover';
|
||||
import { Command, CommandEmpty, CommandGroup, CommandInput, CommandItem, CommandList, CommandSeparator } from '@/components/ui/command';
|
||||
import { useAISettings } from '@/hooks/useAISettings';
|
||||
import { cn } from '@/lib/utils';
|
||||
|
||||
interface ModelSelectorProps {
|
||||
value: string;
|
||||
onChange: (value: string) => void;
|
||||
className?: string;
|
||||
disabled?: boolean;
|
||||
placeholder?: string;
|
||||
}
|
||||
|
||||
export function ModelSelector({
|
||||
value,
|
||||
onChange,
|
||||
className,
|
||||
disabled = false,
|
||||
placeholder = "Select or enter a model...",
|
||||
}: ModelSelectorProps) {
|
||||
const [open, setOpen] = useState(false);
|
||||
const [isCustomInput, setIsCustomInput] = useState(false);
|
||||
const [customValue, setCustomValue] = useState('');
|
||||
const { settings, addRecentlyUsedModel } = useAISettings();
|
||||
|
||||
const recentlyUsedModels = useMemo(() => settings.recentlyUsedModels || [], [settings.recentlyUsedModels]);
|
||||
|
||||
// Initialize with first recently used model if value is empty
|
||||
useEffect(() => {
|
||||
if (!value && recentlyUsedModels.length > 0) {
|
||||
onChange(recentlyUsedModels[0]);
|
||||
}
|
||||
}, [value, recentlyUsedModels, onChange]);
|
||||
|
||||
const handleSelect = (selectedValue: string) => {
|
||||
if (selectedValue === '__custom__') {
|
||||
setIsCustomInput(true);
|
||||
setCustomValue(value);
|
||||
setOpen(false);
|
||||
return;
|
||||
}
|
||||
|
||||
onChange(selectedValue);
|
||||
addRecentlyUsedModel(selectedValue);
|
||||
setOpen(false);
|
||||
};
|
||||
|
||||
const handleCustomSubmit = () => {
|
||||
if (customValue.trim()) {
|
||||
onChange(customValue.trim());
|
||||
addRecentlyUsedModel(customValue.trim());
|
||||
setIsCustomInput(false);
|
||||
setCustomValue('');
|
||||
}
|
||||
};
|
||||
|
||||
const handleCustomKeyDown = (e: React.KeyboardEvent) => {
|
||||
if (e.key === 'Enter') {
|
||||
e.preventDefault();
|
||||
handleCustomSubmit();
|
||||
} else if (e.key === 'Escape') {
|
||||
setIsCustomInput(false);
|
||||
setCustomValue('');
|
||||
}
|
||||
};
|
||||
|
||||
if (isCustomInput) {
|
||||
return (
|
||||
<div className={cn("flex items-center gap-1", className)}>
|
||||
<Input
|
||||
value={customValue}
|
||||
onChange={(e) => setCustomValue(e.target.value)}
|
||||
onKeyDown={handleCustomKeyDown}
|
||||
onBlur={handleCustomSubmit}
|
||||
placeholder="provider/model (e.g., openrouter/anthropic/claude-sonnet-4)"
|
||||
className="h-8 text-xs"
|
||||
autoFocus
|
||||
/>
|
||||
<Button
|
||||
size="sm"
|
||||
variant="ghost"
|
||||
className="h-8 w-8 p-0"
|
||||
onClick={handleCustomSubmit}
|
||||
>
|
||||
<Check className="h-3 w-3" />
|
||||
</Button>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<Popover open={open} onOpenChange={setOpen}>
|
||||
<PopoverTrigger asChild>
|
||||
<Button
|
||||
variant="ghost"
|
||||
role="combobox"
|
||||
aria-expanded={open}
|
||||
className={cn(
|
||||
"h-8 justify-between text-xs border-0 bg-transparent hover:bg-muted/50 hover:text-foreground focus-visible:ring-0 focus-visible:ring-offset-0 text-muted-foreground",
|
||||
className
|
||||
)}
|
||||
disabled={disabled}
|
||||
>
|
||||
<span className="truncate">
|
||||
{value || placeholder}
|
||||
</span>
|
||||
<ChevronDown className="ml-2 h-3 w-3 shrink-0 opacity-50" />
|
||||
</Button>
|
||||
</PopoverTrigger>
|
||||
<PopoverContent className="w-80 p-0" align="end">
|
||||
<Command>
|
||||
<CommandInput placeholder="Search models..." className="h-9" />
|
||||
<CommandList>
|
||||
<CommandEmpty>No models found.</CommandEmpty>
|
||||
|
||||
{recentlyUsedModels.length > 0 && (
|
||||
<CommandGroup heading="Recently Used">
|
||||
{recentlyUsedModels.map((model) => (
|
||||
<CommandItem
|
||||
key={model}
|
||||
value={model}
|
||||
onSelect={() => handleSelect(model)}
|
||||
className="cursor-pointer"
|
||||
>
|
||||
<Check
|
||||
className={cn(
|
||||
"mr-2 h-4 w-4",
|
||||
value === model ? "opacity-100" : "opacity-0"
|
||||
)}
|
||||
/>
|
||||
<span className="truncate">{model}</span>
|
||||
</CommandItem>
|
||||
))}
|
||||
</CommandGroup>
|
||||
)}
|
||||
|
||||
{recentlyUsedModels.length > 0 && <CommandSeparator />}
|
||||
|
||||
<CommandGroup>
|
||||
<CommandItem
|
||||
value="__custom__"
|
||||
onSelect={() => handleSelect('__custom__')}
|
||||
className="cursor-pointer"
|
||||
>
|
||||
<Edit3 className="mr-2 h-4 w-4" />
|
||||
Enter custom model...
|
||||
</CommandItem>
|
||||
</CommandGroup>
|
||||
</CommandList>
|
||||
</Command>
|
||||
</PopoverContent>
|
||||
</Popover>
|
||||
);
|
||||
}
|
||||
@@ -1,7 +1,6 @@
|
||||
import { useState, useRef, useEffect } from 'react';
|
||||
import { useSearchParams } from 'react-router-dom';
|
||||
import { Button } from '@/components/ui/button';
|
||||
import { Input } from '@/components/ui/input';
|
||||
import { Textarea } from '@/components/ui/textarea';
|
||||
import { Skeleton } from '@/components/ui/skeleton';
|
||||
import { Send, Settings, Play, CloudUpload, Loader2, MessageSquarePlus, Square } from 'lucide-react';
|
||||
@@ -10,6 +9,7 @@ import { useFS } from '@/hooks/useFS';
|
||||
import { useJSRuntime } from '@/hooks/useJSRuntime';
|
||||
import { useKeepAlive } from '@/hooks/useKeepAlive';
|
||||
import { useAIChat } from '@/hooks/useAIChat';
|
||||
import { ModelSelector } from '@/components/ModelSelector';
|
||||
import { copyDirectory } from '@/lib/copyFiles';
|
||||
import { generateSecretKey, getPublicKey, nip19 } from 'nostr-tools';
|
||||
import { bytesToHex } from 'nostr-tools/utils';
|
||||
@@ -34,11 +34,14 @@ interface ChatPaneProps {
|
||||
|
||||
export function ChatPane({ projectId, projectName }: ChatPaneProps) {
|
||||
const [input, setInput] = useState('');
|
||||
const [providerModel, setProviderModel] = useState('openrouter/anthropic/claude-sonnet-4');
|
||||
const [isBuildLoading, setIsBuildLoading] = useState(false);
|
||||
const [isDeployLoading, setIsDeployLoading] = useState(false);
|
||||
const scrollAreaRef = useRef<HTMLDivElement>(null);
|
||||
const { isConfigured } = useAISettings();
|
||||
const { isConfigured, settings, addRecentlyUsedModel } = useAISettings();
|
||||
const [providerModel, setProviderModel] = useState(() => {
|
||||
// Default to first recently used model or fallback
|
||||
return settings.recentlyUsedModels?.[0] || 'openrouter/anthropic/claude-sonnet-4';
|
||||
});
|
||||
const { fs: browserFS } = useFS();
|
||||
const { runtime } = useJSRuntime();
|
||||
const [searchParams, setSearchParams] = useSearchParams();
|
||||
@@ -125,10 +128,14 @@ When creating new components or pages, follow the existing patterns in the codeb
|
||||
setSearchParams(newSearchParams, { replace: true });
|
||||
|
||||
// Start AI generation
|
||||
startGeneration(providerModel.trim() || undefined);
|
||||
const modelToUse = providerModel.trim() || undefined;
|
||||
if (modelToUse) {
|
||||
addRecentlyUsedModel(modelToUse);
|
||||
}
|
||||
startGeneration(modelToUse);
|
||||
}
|
||||
}
|
||||
}, [searchParams, setSearchParams, isConfigured, messages, isLoading, startGeneration, providerModel]);
|
||||
}, [searchParams, setSearchParams, isConfigured, messages, isLoading, startGeneration, providerModel, addRecentlyUsedModel]);
|
||||
|
||||
// Keep-alive functionality to prevent tab throttling during AI processing
|
||||
const { updateMetadata } = useKeepAlive({
|
||||
@@ -247,10 +254,16 @@ BASE_DOMAIN=nostrdeploy.com`);
|
||||
if (!input.trim() || isLoading) return;
|
||||
|
||||
const messageContent = input;
|
||||
const modelToUse = providerModel.trim() || undefined;
|
||||
setInput('');
|
||||
|
||||
// Add model to recently used when sending a message
|
||||
if (modelToUse) {
|
||||
addRecentlyUsedModel(modelToUse);
|
||||
}
|
||||
|
||||
try {
|
||||
await sendMessage(messageContent, providerModel.trim() || undefined);
|
||||
await sendMessage(messageContent, modelToUse);
|
||||
} catch (error) {
|
||||
console.error('AI chat error:', error);
|
||||
// Error handling is done in the useAIChat hook
|
||||
@@ -464,14 +477,14 @@ BASE_DOMAIN=nostrdeploy.com`);
|
||||
|
||||
{/* Bottom Controls Row */}
|
||||
<div className="absolute bottom-2 left-2 right-2 flex items-center gap-2">
|
||||
{/* Model Input */}
|
||||
{/* Model Selector */}
|
||||
<div className="flex-1 max-w-72 ml-auto">
|
||||
<Input
|
||||
<ModelSelector
|
||||
value={providerModel}
|
||||
onChange={(e) => setProviderModel(e.target.value)}
|
||||
placeholder="provider/model (e.g., openrouter/anthropic/claude-sonnet-4)"
|
||||
className="h-8 text-xs border-0 bg-transparent hover:bg-muted/50 focus-visible:ring-0 focus-visible:ring-offset-0 placeholder:text-muted-foreground"
|
||||
onChange={setProviderModel}
|
||||
className="w-full"
|
||||
disabled={isLoading}
|
||||
placeholder="provider/model (e.g., openrouter/anthropic/claude-sonnet-4)"
|
||||
/>
|
||||
</div>
|
||||
|
||||
|
||||
Reference in new issue
Block a user