import { useState, useEffect } from 'react'; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { Card, CardContent, CardHeader, CardTitle, CardDescription, CardFooter } from "@/components/ui/card"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select"; import { Switch } from "@/components/ui/switch"; import { Save, Loader2 } from "lucide-react"; import { api } from "@/lib/api"; interface LLMConfig { id: string; provider: string; model: string; api_key?: string; api_base?: string; is_active: boolean; } export function Settings() { const [configId, setConfigId] = useState(null); const [apiKey, setApiKey] = useState(''); const [provider, setProvider] = useState('openai'); const [model, setModel] = useState('gpt-4-turbo'); const [baseUrl, setBaseUrl] = useState('https://api.openai.com/v1'); const [enableVoice, setEnableVoice] = useState(false); const [isLoading, setIsLoading] = useState(false); const [isSaving, setIsSaving] = useState(false); useEffect(() => { fetchConfig(); }, []); const fetchConfig = async () => { setIsLoading(true); try { const configs = await api.get('/api/v1/llm'); const activeConfig = configs.find(c => c.is_active) || configs[0]; if (activeConfig) { setConfigId(activeConfig.id); setProvider(activeConfig.provider); setModel(activeConfig.model); setApiKey(activeConfig.api_key || ''); setBaseUrl(activeConfig.api_base || ''); } } catch (error) { console.error("Failed to fetch LLM config", error); } finally { setIsLoading(false); } }; const handleSave = async () => { setIsSaving(true); try { const configData = { provider, model, api_key: apiKey, api_base: baseUrl, is_active: true }; if (configId) { await api.put(`/api/v1/llm/${configId}`, configData); } else { const newId = Date.now().toString(); await api.post('/api/v1/llm', { ...configData, id: newId }); setConfigId(newId); } alert("Settings saved successfully!"); } catch (error) { console.error("Failed to save settings", error); alert("Failed to save settings"); } finally { setIsSaving(false); } }; if (isLoading) { return (
); } return (

Settings

Configure AI model and application preferences

LLM Configuration Manage your Large Language Model settings
setApiKey(e.target.value)} />
setBaseUrl(e.target.value)} />
Interface Settings Customize your experience

Enable voice input and output

Toggle dark/light theme

); }