mirror of
https://gitlab.com/soapbox-pub/shakespeare.git
synced 2026-12-05 16:23:36 +00:00
Add preset providers
This commit is contained in:
1 file changed
+231
-86
@@ -1,19 +1,68 @@
|
||||
import { useState } from 'react';
|
||||
import { Settings, Plus, Trash2 } from 'lucide-react';
|
||||
import { Settings, Trash2, Check } 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 { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card';
|
||||
import { Card, CardContent } from '@/components/ui/card';
|
||||
import { Separator } from '@/components/ui/separator';
|
||||
import { Badge } from '@/components/ui/badge';
|
||||
import { Accordion, AccordionContent, AccordionItem, AccordionTrigger } from '@/components/ui/accordion';
|
||||
import { useAISettings } from '@/hooks/useAISettings';
|
||||
import type { AIConnection } from '@/contexts/AISettingsContext';
|
||||
|
||||
interface PresetProvider {
|
||||
id: string;
|
||||
name: string;
|
||||
baseURL: string;
|
||||
description: string;
|
||||
}
|
||||
|
||||
const PRESET_PROVIDERS: PresetProvider[] = [
|
||||
{
|
||||
id: 'openai',
|
||||
name: 'OpenAI',
|
||||
baseURL: 'https://api.openai.com/v1',
|
||||
description: 'GPT-4, GPT-3.5 Turbo, and other OpenAI models'
|
||||
},
|
||||
{
|
||||
id: 'anthropic',
|
||||
name: 'Anthropic',
|
||||
baseURL: 'https://api.anthropic.com/v1',
|
||||
description: 'Claude 3.5 Sonnet, Claude 3 Opus, and other Claude models'
|
||||
},
|
||||
{
|
||||
id: 'openrouter',
|
||||
name: 'OpenRouter',
|
||||
baseURL: 'https://openrouter.ai/api/v1',
|
||||
description: 'Access to hundreds of models from various providers'
|
||||
},
|
||||
{
|
||||
id: 'together',
|
||||
name: 'Together AI',
|
||||
baseURL: 'https://api.together.xyz/v1',
|
||||
description: 'Open-source models like Llama, Mistral, and more'
|
||||
},
|
||||
{
|
||||
id: 'groq',
|
||||
name: 'Groq',
|
||||
baseURL: 'https://api.groq.com/openai/v1',
|
||||
description: 'Ultra-fast inference for Llama, Mixtral, and Gemma models'
|
||||
},
|
||||
{
|
||||
id: 'deepseek',
|
||||
name: 'DeepSeek',
|
||||
baseURL: 'https://api.deepseek.com/v1',
|
||||
description: 'DeepSeek-V2.5 and other high-performance models'
|
||||
}
|
||||
];
|
||||
|
||||
export function AISettingsDialog() {
|
||||
const { settings, updateSettings, isConfigured } = useAISettings();
|
||||
const [open, setOpen] = useState(false);
|
||||
const [localProviders, setLocalProviders] = useState(settings.providers);
|
||||
const [newProviderName, setNewProviderName] = useState('');
|
||||
const [customProviderName, setCustomProviderName] = useState('');
|
||||
const [customBaseURL, setCustomBaseURL] = useState('');
|
||||
|
||||
const handleSave = () => {
|
||||
updateSettings({ providers: localProviders });
|
||||
@@ -22,23 +71,37 @@ export function AISettingsDialog() {
|
||||
|
||||
const handleCancel = () => {
|
||||
setLocalProviders(settings.providers);
|
||||
setNewProviderName('');
|
||||
setCustomProviderName('');
|
||||
setCustomBaseURL('');
|
||||
setOpen(false);
|
||||
};
|
||||
|
||||
const handleAddProvider = () => {
|
||||
if (!newProviderName.trim()) return;
|
||||
|
||||
const handleAddPresetProvider = (preset: PresetProvider) => {
|
||||
const newProvider: AIConnection = {
|
||||
baseURL: '',
|
||||
baseURL: preset.baseURL,
|
||||
apiKey: '',
|
||||
};
|
||||
|
||||
setLocalProviders(prev => ({
|
||||
...prev,
|
||||
[newProviderName.trim()]: newProvider,
|
||||
[preset.id]: newProvider,
|
||||
}));
|
||||
setNewProviderName('');
|
||||
};
|
||||
|
||||
const handleAddCustomProvider = () => {
|
||||
if (!customProviderName.trim() || !customBaseURL.trim()) return;
|
||||
|
||||
const newProvider: AIConnection = {
|
||||
baseURL: customBaseURL.trim(),
|
||||
apiKey: '',
|
||||
};
|
||||
|
||||
setLocalProviders(prev => ({
|
||||
...prev,
|
||||
[customProviderName.trim()]: newProvider,
|
||||
}));
|
||||
setCustomProviderName('');
|
||||
setCustomBaseURL('');
|
||||
};
|
||||
|
||||
const handleRemoveProvider = (name: string) => {
|
||||
@@ -58,7 +121,8 @@ export function AISettingsDialog() {
|
||||
}));
|
||||
};
|
||||
|
||||
const providerNames = Object.keys(localProviders);
|
||||
const configuredProviderIds = Object.keys(localProviders);
|
||||
const availablePresets = PRESET_PROVIDERS.filter(preset => !configuredProviderIds.includes(preset.id));
|
||||
|
||||
return (
|
||||
<Dialog open={open} onOpenChange={setOpen}>
|
||||
@@ -71,96 +135,176 @@ export function AISettingsDialog() {
|
||||
)}
|
||||
</Button>
|
||||
</DialogTrigger>
|
||||
<DialogContent className="sm:max-w-[600px] max-h-[80vh] overflow-y-auto">
|
||||
<DialogContent className="sm:max-w-[700px] max-h-[80vh] overflow-y-auto">
|
||||
<DialogHeader>
|
||||
<DialogTitle>AI Settings</DialogTitle>
|
||||
<DialogDescription>
|
||||
Configure multiple AI providers. These settings are stored locally in your browser.
|
||||
Configure AI providers by adding your API keys. Settings are stored locally in your browser.
|
||||
</DialogDescription>
|
||||
</DialogHeader>
|
||||
|
||||
<div className="space-y-4 py-4">
|
||||
{/* Existing Providers */}
|
||||
{providerNames.length > 0 && (
|
||||
<div className="space-y-6 py-4">
|
||||
{/* Configured Providers */}
|
||||
{configuredProviderIds.length > 0 && (
|
||||
<div className="space-y-3">
|
||||
<h4 className="text-sm font-medium">Configured Providers</h4>
|
||||
{providerNames.map((name) => (
|
||||
<Card key={name}>
|
||||
<CardHeader className="pb-3">
|
||||
<div className="flex items-center justify-between">
|
||||
<CardTitle className="text-base">{name}</CardTitle>
|
||||
<Button
|
||||
variant="ghost"
|
||||
size="sm"
|
||||
onClick={() => handleRemoveProvider(name)}
|
||||
className="h-8 w-8 p-0 text-muted-foreground hover:text-destructive"
|
||||
>
|
||||
<Trash2 className="h-4 w-4" />
|
||||
</Button>
|
||||
</div>
|
||||
</CardHeader>
|
||||
<CardContent className="space-y-3">
|
||||
<div className="grid gap-2">
|
||||
<Label htmlFor={`${name}-apiKey`}>API Key</Label>
|
||||
<Input
|
||||
id={`${name}-apiKey`}
|
||||
type="password"
|
||||
placeholder="Enter your API key"
|
||||
value={localProviders[name]?.apiKey || ''}
|
||||
onChange={(e) => handleUpdateProvider(name, { apiKey: e.target.value })}
|
||||
/>
|
||||
</div>
|
||||
<div className="grid gap-2">
|
||||
<Label htmlFor={`${name}-baseURL`}>Base URL</Label>
|
||||
<Input
|
||||
id={`${name}-baseURL`}
|
||||
placeholder="https://api.example.com/v1"
|
||||
value={localProviders[name]?.baseURL || ''}
|
||||
onChange={(e) => handleUpdateProvider(name, { baseURL: e.target.value })}
|
||||
/>
|
||||
</div>
|
||||
</CardContent>
|
||||
</Card>
|
||||
))}
|
||||
<Accordion type="multiple" className="w-full space-y-2">
|
||||
{configuredProviderIds.map((providerId) => {
|
||||
const preset = PRESET_PROVIDERS.find(p => p.id === providerId);
|
||||
const provider = localProviders[providerId];
|
||||
const isCustom = !preset;
|
||||
const hasApiKey = provider?.apiKey && provider.apiKey.length > 0;
|
||||
|
||||
return (
|
||||
<AccordionItem key={providerId} value={providerId} className="border rounded-lg">
|
||||
<AccordionTrigger className="px-4 py-3 hover:no-underline">
|
||||
<div className="flex items-center justify-between w-full mr-3">
|
||||
<div className="flex items-center gap-2">
|
||||
<span className="font-medium">
|
||||
{preset?.name || providerId}
|
||||
</span>
|
||||
{isCustom && <Badge variant="outline">Custom</Badge>}
|
||||
{hasApiKey && (
|
||||
<Badge variant="secondary" className="text-xs">
|
||||
Configured
|
||||
</Badge>
|
||||
)}
|
||||
</div>
|
||||
<Button
|
||||
variant="ghost"
|
||||
size="sm"
|
||||
onClick={(e) => {
|
||||
e.stopPropagation();
|
||||
handleRemoveProvider(providerId);
|
||||
}}
|
||||
className="h-8 w-8 p-0 text-muted-foreground hover:text-destructive"
|
||||
>
|
||||
<Trash2 className="h-4 w-4" />
|
||||
</Button>
|
||||
</div>
|
||||
</AccordionTrigger>
|
||||
<AccordionContent className="px-4 pb-4">
|
||||
<div className="space-y-3">
|
||||
{preset && (
|
||||
<p className="text-sm text-muted-foreground">{preset.description}</p>
|
||||
)}
|
||||
<div className="grid gap-2">
|
||||
<Label htmlFor={`${providerId}-apiKey`}>API Key</Label>
|
||||
<Input
|
||||
id={`${providerId}-apiKey`}
|
||||
type="password"
|
||||
placeholder="Enter your API key"
|
||||
value={provider?.apiKey || ''}
|
||||
onChange={(e) => handleUpdateProvider(providerId, { apiKey: e.target.value })}
|
||||
/>
|
||||
</div>
|
||||
<div className="grid gap-2">
|
||||
<Label htmlFor={`${providerId}-baseURL`}>Base URL</Label>
|
||||
<Input
|
||||
id={`${providerId}-baseURL`}
|
||||
placeholder="https://api.example.com/v1"
|
||||
value={provider?.baseURL || ''}
|
||||
onChange={(e) => handleUpdateProvider(providerId, { baseURL: e.target.value })}
|
||||
/>
|
||||
{preset && (
|
||||
<p className="text-xs text-muted-foreground">
|
||||
Default: {preset.baseURL}
|
||||
</p>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
</AccordionContent>
|
||||
</AccordionItem>
|
||||
);
|
||||
})}
|
||||
</Accordion>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{providerNames.length > 0 && <Separator />}
|
||||
{configuredProviderIds.length > 0 && availablePresets.length > 0 && <Separator />}
|
||||
|
||||
{/* Add New Provider */}
|
||||
<div className="space-y-3">
|
||||
<h4 className="text-sm font-medium">Add New Provider</h4>
|
||||
<div className="flex gap-2">
|
||||
<Input
|
||||
placeholder="Provider name (e.g., openai, anthropic)"
|
||||
value={newProviderName}
|
||||
onChange={(e) => setNewProviderName(e.target.value)}
|
||||
onKeyDown={(e) => {
|
||||
if (e.key === 'Enter') {
|
||||
e.preventDefault();
|
||||
handleAddProvider();
|
||||
}
|
||||
}}
|
||||
/>
|
||||
<Button
|
||||
onClick={handleAddProvider}
|
||||
disabled={!newProviderName.trim() || newProviderName.trim() in localProviders}
|
||||
size="sm"
|
||||
className="gap-2"
|
||||
>
|
||||
<Plus className="h-4 w-4" />
|
||||
Add
|
||||
</Button>
|
||||
{/* Available Preset Providers */}
|
||||
{availablePresets.length > 0 && (
|
||||
<div className="space-y-3">
|
||||
<h4 className="text-sm font-medium">Add Provider</h4>
|
||||
<div className="grid gap-3">
|
||||
{availablePresets.map((preset) => (
|
||||
<Card key={preset.id} className="cursor-pointer hover:bg-muted/50 transition-colors">
|
||||
<CardContent className="p-4">
|
||||
<div className="flex items-center justify-between">
|
||||
<div className="space-y-1">
|
||||
<div className="flex items-center gap-2">
|
||||
<h5 className="font-medium">{preset.name}</h5>
|
||||
<Badge variant="secondary" className="text-xs">Preset</Badge>
|
||||
</div>
|
||||
<p className="text-sm text-muted-foreground">{preset.description}</p>
|
||||
<p className="text-xs text-muted-foreground font-mono">{preset.baseURL}</p>
|
||||
</div>
|
||||
<Button
|
||||
onClick={() => handleAddPresetProvider(preset)}
|
||||
size="sm"
|
||||
className="gap-2"
|
||||
>
|
||||
<Check className="h-4 w-4" />
|
||||
Add
|
||||
</Button>
|
||||
</div>
|
||||
</CardContent>
|
||||
</Card>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
{newProviderName.trim() in localProviders && (
|
||||
<p className="text-sm text-destructive">
|
||||
Provider with this name already exists
|
||||
</p>
|
||||
)}
|
||||
)}
|
||||
|
||||
{availablePresets.length > 0 && <Separator />}
|
||||
|
||||
{/* Custom Provider */}
|
||||
<div className="space-y-3">
|
||||
<h4 className="text-sm font-medium">Add Custom Provider</h4>
|
||||
<Card>
|
||||
<CardContent className="p-4 space-y-3">
|
||||
<div className="grid gap-2">
|
||||
<Label htmlFor="custom-name">Provider Name</Label>
|
||||
<Input
|
||||
id="custom-name"
|
||||
placeholder="e.g., my-custom-api"
|
||||
value={customProviderName}
|
||||
onChange={(e) => setCustomProviderName(e.target.value)}
|
||||
/>
|
||||
</div>
|
||||
<div className="grid gap-2">
|
||||
<Label htmlFor="custom-baseurl">Base URL</Label>
|
||||
<Input
|
||||
id="custom-baseurl"
|
||||
placeholder="https://api.example.com/v1"
|
||||
value={customBaseURL}
|
||||
onChange={(e) => setCustomBaseURL(e.target.value)}
|
||||
/>
|
||||
</div>
|
||||
<Button
|
||||
onClick={handleAddCustomProvider}
|
||||
disabled={
|
||||
!customProviderName.trim() ||
|
||||
!customBaseURL.trim() ||
|
||||
customProviderName.trim() in localProviders
|
||||
}
|
||||
size="sm"
|
||||
className="gap-2"
|
||||
>
|
||||
<Check className="h-4 w-4" />
|
||||
Add Custom Provider
|
||||
</Button>
|
||||
{customProviderName.trim() in localProviders && (
|
||||
<p className="text-sm text-destructive">
|
||||
Provider with this name already exists
|
||||
</p>
|
||||
)}
|
||||
</CardContent>
|
||||
</Card>
|
||||
</div>
|
||||
|
||||
{/* Usage Instructions */}
|
||||
<div className="bg-muted/50 rounded-lg p-3 space-y-2">
|
||||
<div className="bg-muted/50 rounded-lg p-4 space-y-2">
|
||||
<h4 className="text-sm font-medium">Usage</h4>
|
||||
<p className="text-sm text-muted-foreground">
|
||||
In the chat, use the format <code className="bg-background px-1 rounded">provider/model</code> to specify which provider and model to use.
|
||||
@@ -168,9 +312,10 @@ export function AISettingsDialog() {
|
||||
<div className="text-sm text-muted-foreground space-y-1">
|
||||
<p><strong>Examples:</strong></p>
|
||||
<ul className="list-disc list-inside ml-2 space-y-0.5">
|
||||
<li><code className="bg-background px-1 rounded">openrouter/anthropic/claude-sonnet-4</code></li>
|
||||
<li><code className="bg-background px-1 rounded">openrouter/anthropic/claude-3.5-sonnet</code></li>
|
||||
<li><code className="bg-background px-1 rounded">openai/gpt-4o</code></li>
|
||||
<li><code className="bg-background px-1 rounded">anthropic/claude-3-5-sonnet-20241022</code></li>
|
||||
<li><code className="bg-background px-1 rounded">groq/llama-3.1-70b-versatile</code></li>
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
Reference in new issue
Block a user