import React, { useEffect, useState } from 'react'; import { Bot, Check, ChevronDown, Clock, Cpu, Eye, EyeOff, Shield, X } from 'lucide-react'; import { LLMSettingsService } from '../services/api'; const SETTINGS_KEY = '_app_settings'; const DEFAULTS = { httpTimeout: 120, tokenBudget: 100000, theme: 'dark', }; function loadSettings() { try { const raw = localStorage.getItem(SETTINGS_KEY); return raw ? { ...DEFAULTS, ...JSON.parse(raw) } : { ...DEFAULTS }; } catch { return { ...DEFAULTS }; } } function saveSettings(s) { localStorage.setItem(SETTINGS_KEY, JSON.stringify(s)); } export function useAppSettings() { const [settings, setSettings] = useState(loadSettings); useEffect(() => { saveSettings(settings); }, [settings]); const update = (key, value) => setSettings(prev => ({ ...prev, [key]: value })); return { settings, update }; } // ── Shared styles ───────────────────────────────────────────────────────────── const sectionStyle = { background: 'var(--bg-2)', border: '1px solid var(--line-2)', borderRadius: 10, padding: '16px 20px', }; const labelStyle = { fontSize: 10, textTransform: 'uppercase', letterSpacing: '0.1em', color: 'var(--fg-3)', fontWeight: 700, display: 'block', marginBottom: 6, }; const sectionHeadStyle = { display: 'flex', alignItems: 'center', gap: 8, marginBottom: 16, }; // ── AI Models section ───────────────────────────────────────────────────────── function ProviderCard({ provider, onSave, onClear }) { const [key, setKey] = useState(''); const [show, setShow] = useState(false); const [saving, setSaving] = useState(false); const [flash, setFlash] = useState(null); // 'ok' | 'err' const handleSave = async () => { if (!key.trim()) return; setSaving(true); try { await onSave(provider.provider_id, key.trim()); setKey(''); setFlash('ok'); } catch { setFlash('err'); } finally { setSaving(false); setTimeout(() => setFlash(null), 2000); } }; const handleClear = async () => { await onClear(provider.provider_id); }; return (
{provider.display_name} {provider.is_active ? 'Configured' : 'Not set'}
{provider.key_preview && (
Current key: {provider.key_preview}
)}
setKey(e.target.value)} className="input" style={{ width: '100%', paddingRight: 32, fontSize: 12 }} />
{provider.is_active && ( )}
); } function TaskRow({ task, providers, registry, onSave }) { const [provId, setProvId] = useState(task.provider_id || ''); const [model, setModel] = useState(task.model_name || ''); const [temp, setTemp] = useState(task.temperature ?? 0.0); const [saving, setSaving] = useState(false); const [saved, setSaved] = useState(false); const activeProviders = providers.filter(p => p.is_active); const models = provId && registry[provId] ? registry[provId].models : []; useEffect(() => { if (!models.includes(model)) setModel(models[0] || ''); }, [provId]); const handleSave = async () => { setSaving(true); try { await onSave(task.task_name, { provider_id: provId || null, model_name: model || null, temperature: temp }); setSaved(true); setTimeout(() => setSaved(false), 2000); } finally { setSaving(false); } }; const noKey = provId && !providers.find(p => p.provider_id === provId)?.is_active; return (
{task.label} {/* Provider dropdown */}
{/* Model dropdown */}
{/* Temperature */} setTemp(parseFloat(e.target.value) || 0)} className="input" style={{ fontSize: 12, textAlign: 'center' }} /> {/* Save button */}
); } function AIModelsSection() { const [data, setData] = useState(null); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); const load = async () => { setLoading(true); setError(null); try { const d = await LLMSettingsService.get(); setData(d); } catch (e) { setError(e.message); } finally { setLoading(false); } }; useEffect(() => { load(); }, []); const handleSaveKey = async (providerId, apiKey) => { await LLMSettingsService.saveProviderKey(providerId, apiKey); // Auto-configure any task that has no provider set yet const current = await LLMSettingsService.get(); const registry = current.registry[providerId]; const defaultModel = registry?.models?.[0] || ''; await Promise.all( current.tasks .filter(t => !t.provider_id) .map(t => LLMSettingsService.saveTaskConfig(t.task_name, { provider_id: providerId, model_name: defaultModel, temperature: t.temperature ?? 0.0, })) ); await load(); }; const handleClearKey = async (providerId) => { await LLMSettingsService.deleteProviderKey(providerId); await load(); }; const handleSaveTask = async (taskName, config) => { await LLMSettingsService.saveTaskConfig(taskName, config); }; const handleApplyAll = async (providerId) => { const registry = data.registry[providerId]; const defaultModel = registry?.models?.[0] || ''; await Promise.all( data.tasks.map(t => LLMSettingsService.saveTaskConfig(t.task_name, { provider_id: providerId, model_name: defaultModel, temperature: t.temperature ?? 0.0, })) ); await load(); }; return (
AI Models
{loading &&
Loading…
} {error &&
{error}
} {data && (
{/* Provider API keys */}
{data.providers.map(p => ( ))}
{/* Task routing */}
{data.providers.filter(p => p.is_active).length > 0 && (
{data.providers.filter(p => p.is_active).map(p => ( ))}
)}
{data.tasks.some(t => !t.provider_id) && (
Some tasks have no provider set — plan generation and execution will fail until all tasks are configured. Use the button above to apply in one click.
)} {/* Header row */}
{['Task', 'Provider', 'Model', 'Temp', ''].map((h, i) => ( {h} ))}
{data.tasks.map(t => ( ))}
)}
); } // ── Main page ───────────────────────────────────────────────────────────────── export default function SettingsPage() { const [settings, setSettings] = useState(loadSettings); const [saved, setSaved] = useState(false); const update = (key, value) => setSettings(prev => ({ ...prev, [key]: value })); const handleSave = () => { saveSettings(settings); setSaved(true); setTimeout(() => setSaved(false), 2000); }; return (
Configuration

Settings

{/* AI Models — live backend config */} {/* HTTP */}
HTTP Execution
update('httpTimeout', parseInt(e.target.value, 10) || DEFAULTS.httpTimeout)} className="input" style={{ width: 100 }} /> seconds

Note: the backend timeout is set in execution.py. This setting is for reference.

{/* Token budget */}
Usage Limits
update('tokenBudget', parseInt(e.target.value, 10) || DEFAULTS.tokenBudget)} className="input" style={{ width: 140 }} /> tokens

Sessions exceeding this limit will be blocked. Set high to disable the guard.

{/* Security info */}
Security
{[ 'Credentials and API keys are encrypted at rest with Fernet symmetric encryption.', 'SSRF protection is enabled — private/loopback IPs are blocked.', 'Rate limiting is active (10 plan/min, 30 execute/min, 20 chat/min).', 'JWTs expire after the configured JWT_EXPIRE_HOURS from .env.', ].map((text, i) => (
{text}
))}
{/* Save (for localStorage settings) */}
); }