diff --git a/frontend/src/components/AppLayout.tsx b/frontend/src/components/AppLayout.tsx index 5c8dfbf..77412ee 100644 --- a/frontend/src/components/AppLayout.tsx +++ b/frontend/src/components/AppLayout.tsx @@ -18,6 +18,7 @@ import { List, Globe, BarChart3, + Bot, } from 'lucide-react'; interface NavItem { @@ -42,6 +43,7 @@ const navItems: NavItem[] = [ { to: '/ops/pipeline', label: 'Pipeline', icon: , group: 'Ops' }, { to: '/ops/ingestion', label: 'Ingestion', icon: , group: 'Ops' }, { to: '/ops/model', label: 'Model Perf', icon: , group: 'Ops' }, + { to: '/agents', label: 'Agents', icon: , group: 'Ops' }, { to: '/ops/coverage', label: 'Coverage', icon: , group: 'Ops' }, { to: '/analytics/query', label: 'SQL Explorer', icon: , group: 'Analytics' }, { to: '/analytics/dashboards', label: 'Dashboards', icon: , group: 'Analytics' }, diff --git a/frontend/src/pages/Agents.tsx b/frontend/src/pages/Agents.tsx new file mode 100644 index 0000000..1b7def9 --- /dev/null +++ b/frontend/src/pages/Agents.tsx @@ -0,0 +1,459 @@ +import { useState } from 'react'; +import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query'; +import { apiGet, apiPost, apiPut, apiDelete } from '../api/client'; +import { Card, LoadingSpinner, StatusBadge } from '../components/ui'; +import { + LineChart, Line, XAxis, YAxis, Tooltip, ResponsiveContainer, CartesianGrid, +} from 'recharts'; + +// --------------------------------------------------------------------------- +// Types +// --------------------------------------------------------------------------- + +interface Agent { + id: string; + name: string; + slug: string; + purpose: string; + model_provider: string; + model_name: string; + system_prompt: string; + user_prompt_template: string; + prompt_version: string; + schema_version: string; + temperature: number; + max_tokens: number; + timeout_seconds: number; + max_retries: number; + active: boolean; + source: string; + created_at: string; + updated_at: string; +} + +interface AgentPerformance { + total_invocations: number; + successes: number; + failures: number; + avg_duration_ms: number | null; + p95_duration_ms: number | null; + avg_confidence: number | null; + avg_retries: number | null; + total_input_tokens: number | null; + total_output_tokens: number | null; + success_rate: number | null; +} + +interface PerfHistoryPoint { + hour: string; + invocations: number; + successes: number; + avg_duration_ms: number; + avg_confidence: number; +} + +// --------------------------------------------------------------------------- +// Hooks +// --------------------------------------------------------------------------- + +function useAgents() { + return useQuery({ + queryKey: ['agents'], + queryFn: () => apiGet('query', '/api/agents'), + }); +} + +function useAgentPerformance(agentId: string | undefined, hours = 24) { + return useQuery({ + queryKey: ['agent-performance', agentId, hours], + queryFn: () => apiGet('query', `/api/agents/${agentId}/performance?hours=${hours}`), + enabled: !!agentId, + }); +} + +function useAgentPerfHistory(agentId: string | undefined, hours = 24) { + return useQuery({ + queryKey: ['agent-perf-history', agentId, hours], + queryFn: () => apiGet('query', `/api/agents/${agentId}/performance/history?hours=${hours}`), + enabled: !!agentId, + }); +} + +// --------------------------------------------------------------------------- +// Page Component +// --------------------------------------------------------------------------- + +export function AgentsPage() { + const qc = useQueryClient(); + const { data: agents, isLoading } = useAgents(); + const [selectedId, setSelectedId] = useState(undefined); + const [editing, setEditing] = useState(false); + const [creating, setCreating] = useState(false); + + const selected = agents?.find((a) => a.id === selectedId); + + if (isLoading) return ; + + return ( + + {/* Agent List Sidebar */} + + + Agents + { setCreating(true); setSelectedId(undefined); setEditing(false); }} + className="rounded bg-brand-600 px-2 py-0.5 text-[10px] font-medium text-white hover:bg-brand-700" + > + + New + + + {(agents ?? []).map((agent) => ( + { setSelectedId(agent.id); setEditing(false); setCreating(false); }} + className={`w-full rounded-md px-3 py-2 text-left text-sm transition-colors ${ + selectedId === agent.id + ? 'bg-brand-600/20 border border-brand-500/50 text-brand-300' + : 'text-gray-300 hover:bg-surface-800' + }`} + > + + {agent.name} + {!agent.active && OFF} + + + {agent.model_name} + + {agent.source} + + + + ))} + + + {/* Detail Panel */} + + {creating ? ( + { qc.invalidateQueries({ queryKey: ['agents'] }); setSelectedId(id); setCreating(false); }} + onCancel={() => setCreating(false)} + /> + ) : selected ? ( + editing ? ( + { qc.invalidateQueries({ queryKey: ['agents'] }); setEditing(false); }} onCancel={() => setEditing(false)} /> + ) : ( + setEditing(true)} onDeleted={() => { qc.invalidateQueries({ queryKey: ['agents'] }); setSelectedId(undefined); }} /> + ) + ) : ( + + Select an agent to view its configuration and performance, or create a new one. + + )} + + + ); +} + +// --------------------------------------------------------------------------- +// Agent Detail View +// --------------------------------------------------------------------------- + +function AgentDetail({ agent, onEdit, onDeleted }: { agent: Agent; onEdit: () => void; onDeleted: () => void }) { + const qc = useQueryClient(); + const { data: perf } = useAgentPerformance(agent.id); + const { data: history } = useAgentPerfHistory(agent.id); + + const deleteMut = useMutation({ + mutationFn: () => apiDelete('query', `/api/agents/${agent.id}`), + onSuccess: () => { qc.invalidateQueries({ queryKey: ['agents'] }); onDeleted(); }, + }); + + const chartData = (history ?? []).map((pt) => ({ + hour: new Date(pt.hour).toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' }), + invocations: pt.invocations, + successRate: pt.invocations > 0 ? Math.round((pt.successes / pt.invocations) * 100) : 0, + avgLatency: pt.avg_duration_ms, + avgConfidence: Math.round((pt.avg_confidence ?? 0) * 100), + })); + + return ( + + {/* Header */} + + + {agent.name} + + + {agent.source} + + + + Edit + {agent.source === 'user' && ( + deleteMut.mutate()} className="rounded-md border border-red-700/50 px-3 py-1.5 text-sm text-red-400 hover:bg-red-900/20">Delete + )} + + + + {/* Config */} + + + Model{agent.model_provider}/{agent.model_name} + Prompt Version{agent.prompt_version || '—'} + Schema Version{agent.schema_version} + Temperature{agent.temperature} + Max Tokens{agent.max_tokens.toLocaleString()} + Timeout{agent.timeout_seconds}s + Max Retries{agent.max_retries} + Updated{new Date(agent.updated_at).toLocaleString()} + + + + {agent.purpose && ( + + Purpose + {agent.purpose} + + )} + + + System Prompt + {agent.system_prompt || '(none)'} + + + {/* Performance Metrics */} + {perf && ( + + Performance (24h) + + + = 0.95 ? 'text-green-400' : 'text-yellow-400'} /> + + + + + + Retries avg: {perf.avg_retries?.toFixed(1) ?? '—'} + Input tokens: {perf.total_input_tokens?.toLocaleString() ?? '—'} + Output tokens: {perf.total_output_tokens?.toLocaleString() ?? '—'} + + + )} + + {/* Performance Chart */} + {chartData.length > 1 && ( + + Performance Over Time + + + + + + + + + + + + + + )} + + ); +} + +// --------------------------------------------------------------------------- +// Edit Form +// --------------------------------------------------------------------------- + +function EditAgentForm({ agent, onSaved, onCancel }: { agent: Agent; onSaved: () => void; onCancel: () => void }) { + const [form, setForm] = useState({ + name: agent.name, + purpose: agent.purpose, + model_provider: agent.model_provider, + model_name: agent.model_name, + system_prompt: agent.system_prompt, + user_prompt_template: agent.user_prompt_template, + prompt_version: agent.prompt_version, + schema_version: agent.schema_version, + temperature: agent.temperature, + max_tokens: agent.max_tokens, + timeout_seconds: agent.timeout_seconds, + max_retries: agent.max_retries, + active: agent.active, + }); + + const mutation = useMutation({ + mutationFn: (body: Record) => apiPut('query', `/api/agents/${agent.id}`, body), + onSuccess: onSaved, + }); + + function handleSubmit(e: React.FormEvent) { + e.preventDefault(); + mutation.mutate(form); + } + + return ( + + Edit Agent: {agent.name} + + + setForm({ ...form, name: e.target.value })} className="w-full rounded-md border border-surface-700 bg-surface-950 px-2 py-1.5 text-sm text-gray-200 focus:border-brand-500 focus:outline-none" /> + + + setForm({ ...form, purpose: e.target.value })} className="w-full rounded-md border border-surface-700 bg-surface-950 px-2 py-1.5 text-sm text-gray-200 focus:border-brand-500 focus:outline-none h-16" /> + + + + setForm({ ...form, model_provider: e.target.value })} className="w-full rounded-md border border-surface-700 bg-surface-950 px-2 py-1.5 text-sm text-gray-200 focus:border-brand-500 focus:outline-none" /> + + + setForm({ ...form, model_name: e.target.value })} className="w-full rounded-md border border-surface-700 bg-surface-950 px-2 py-1.5 text-sm text-gray-200 focus:border-brand-500 focus:outline-none" /> + + + + setForm({ ...form, system_prompt: e.target.value })} className="w-full rounded-md border border-surface-700 bg-surface-950 px-2 py-1.5 text-sm text-gray-200 focus:border-brand-500 focus:outline-none h-32 font-mono text-xs" /> + + + setForm({ ...form, user_prompt_template: e.target.value })} className="w-full rounded-md border border-surface-700 bg-surface-950 px-2 py-1.5 text-sm text-gray-200 focus:border-brand-500 focus:outline-none h-24 font-mono text-xs" /> + + + + setForm({ ...form, temperature: Number(e.target.value) })} className="w-full rounded-md border border-surface-700 bg-surface-950 px-2 py-1.5 text-sm text-gray-200 focus:border-brand-500 focus:outline-none" /> + + + setForm({ ...form, max_tokens: Number(e.target.value) })} className="w-full rounded-md border border-surface-700 bg-surface-950 px-2 py-1.5 text-sm text-gray-200 focus:border-brand-500 focus:outline-none" /> + + + setForm({ ...form, timeout_seconds: Number(e.target.value) })} className="w-full rounded-md border border-surface-700 bg-surface-950 px-2 py-1.5 text-sm text-gray-200 focus:border-brand-500 focus:outline-none" /> + + + setForm({ ...form, max_retries: Number(e.target.value) })} className="w-full rounded-md border border-surface-700 bg-surface-950 px-2 py-1.5 text-sm text-gray-200 focus:border-brand-500 focus:outline-none" /> + + + + + setForm({ ...form, active: e.target.checked })} /> + Active + + + + + {mutation.isPending ? 'Saving…' : 'Save'} + + Cancel + + {mutation.isError && Failed to save} + + + ); +} + +// --------------------------------------------------------------------------- +// Create Form +// --------------------------------------------------------------------------- + +function CreateAgentForm({ onCreated, onCancel }: { onCreated: (id: string) => void; onCancel: () => void }) { + const [form, setForm] = useState({ + name: '', + slug: '', + purpose: '', + model_provider: 'ollama', + model_name: 'llama3.1:8b', + system_prompt: '', + user_prompt_template: '', + temperature: 0, + max_tokens: 32768, + timeout_seconds: 120, + max_retries: 2, + }); + + const mutation = useMutation({ + mutationFn: (body: Record) => apiPost<{ id: string }>('query', '/api/agents', body), + onSuccess: (data) => onCreated(data.id), + }); + + // Auto-generate slug from name + function handleNameChange(name: string) { + const slug = name.toLowerCase().replace(/[^a-z0-9]+/g, '-').replace(/^-|-$/g, ''); + setForm({ ...form, name, slug }); + } + + function handleSubmit(e: React.FormEvent) { + e.preventDefault(); + mutation.mutate(form); + } + + return ( + + Create New Agent + + + + handleNameChange(e.target.value)} className="w-full rounded-md border border-surface-700 bg-surface-950 px-2 py-1.5 text-sm text-gray-200 focus:border-brand-500 focus:outline-none" required /> + + + setForm({ ...form, slug: e.target.value })} className="w-full rounded-md border border-surface-700 bg-surface-950 px-2 py-1.5 text-sm text-gray-200 focus:border-brand-500 focus:outline-none font-mono" required /> + + + + setForm({ ...form, purpose: e.target.value })} className="w-full rounded-md border border-surface-700 bg-surface-950 px-2 py-1.5 text-sm text-gray-200 focus:border-brand-500 focus:outline-none h-16" /> + + + + setForm({ ...form, model_provider: e.target.value })} className="w-full rounded-md border border-surface-700 bg-surface-950 px-2 py-1.5 text-sm text-gray-200 focus:border-brand-500 focus:outline-none" /> + + + setForm({ ...form, model_name: e.target.value })} className="w-full rounded-md border border-surface-700 bg-surface-950 px-2 py-1.5 text-sm text-gray-200 focus:border-brand-500 focus:outline-none" /> + + + + setForm({ ...form, system_prompt: e.target.value })} className="w-full rounded-md border border-surface-700 bg-surface-950 px-2 py-1.5 text-sm text-gray-200 focus:border-brand-500 focus:outline-none h-32 font-mono text-xs" /> + + + + setForm({ ...form, temperature: Number(e.target.value) })} className="w-full rounded-md border border-surface-700 bg-surface-950 px-2 py-1.5 text-sm text-gray-200 focus:border-brand-500 focus:outline-none" /> + + + setForm({ ...form, max_tokens: Number(e.target.value) })} className="w-full rounded-md border border-surface-700 bg-surface-950 px-2 py-1.5 text-sm text-gray-200 focus:border-brand-500 focus:outline-none" /> + + + setForm({ ...form, timeout_seconds: Number(e.target.value) })} className="w-full rounded-md border border-surface-700 bg-surface-950 px-2 py-1.5 text-sm text-gray-200 focus:border-brand-500 focus:outline-none" /> + + + setForm({ ...form, max_retries: Number(e.target.value) })} className="w-full rounded-md border border-surface-700 bg-surface-950 px-2 py-1.5 text-sm text-gray-200 focus:border-brand-500 focus:outline-none" /> + + + + + {mutation.isPending ? 'Creating…' : 'Create'} + + Cancel + + {mutation.isError && Failed to create agent} + + + ); +} + +// --------------------------------------------------------------------------- +// Shared Components +// --------------------------------------------------------------------------- + +function FormRow({ label, children }: { label: string; children: React.ReactNode }) { + return ( + + {label} + {children} + + ); +} + +function MetricCard({ label, value, color = 'text-gray-100' }: { label: string; value: string; color?: string }) { + return ( + + {value} + {label} + + ); +} diff --git a/frontend/src/routes.tsx b/frontend/src/routes.tsx index 4c71bde..90107f3 100644 --- a/frontend/src/routes.tsx +++ b/frontend/src/routes.tsx @@ -29,6 +29,7 @@ import { DashboardsPage } from './pages/Dashboards'; import { HomePage } from './pages/Home'; import { GlobalEventsPage } from './pages/GlobalEvents'; import { GlobalEventDetailPage } from './pages/GlobalEventDetail'; +import { AgentsPage } from './pages/Agents'; // Root route wraps everything in the app shell layout const rootRoute = createRootRoute({ @@ -157,6 +158,12 @@ const globalEventDetailRoute = createRoute({ component: GlobalEventDetailPage, }); +const agentsRoute = createRoute({ + getParentRoute: () => rootRoute, + path: '/agents', + component: AgentsPage, +}); + const routeTree = rootRoute.addChildren([ indexRoute, companiesRoute, @@ -181,6 +188,7 @@ const routeTree = rootRoute.addChildren([ analyticsDashboardsRoute, globalEventsRoute, globalEventDetailRoute, + agentsRoute, ]); export const router = createRouter({ routeTree }); diff --git a/infra/migrations/026_ai_agents.sql b/infra/migrations/026_ai_agents.sql new file mode 100644 index 0000000..fa70213 --- /dev/null +++ b/infra/migrations/026_ai_agents.sql @@ -0,0 +1,86 @@ +-- AI Agent configurations: user-editable agent profiles. +-- Seed rows have source='system' and are re-inserted on migration only if +-- missing, so user edits (source='user') are never overwritten. + +CREATE TABLE IF NOT EXISTS ai_agents ( + id UUID PRIMARY KEY DEFAULT gen_random_uuid(), + name VARCHAR(100) NOT NULL UNIQUE, + slug VARCHAR(100) NOT NULL UNIQUE, + purpose TEXT NOT NULL DEFAULT '', + model_provider VARCHAR(50) NOT NULL DEFAULT 'ollama', + model_name VARCHAR(200) NOT NULL DEFAULT 'llama3.1:8b', + system_prompt TEXT NOT NULL DEFAULT '', + user_prompt_template TEXT NOT NULL DEFAULT '', + prompt_version VARCHAR(100) NOT NULL DEFAULT '', + schema_version VARCHAR(50) NOT NULL DEFAULT '1.0.0', + temperature FLOAT DEFAULT 0.0, + max_tokens INTEGER DEFAULT 32768, + timeout_seconds INTEGER DEFAULT 120, + max_retries INTEGER DEFAULT 2, + active BOOLEAN NOT NULL DEFAULT TRUE, + source VARCHAR(20) NOT NULL DEFAULT 'system', -- 'system' or 'user' + created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(), + updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW() +); + +CREATE INDEX IF NOT EXISTS idx_ai_agents_slug ON ai_agents(slug); +CREATE INDEX IF NOT EXISTS idx_ai_agents_active ON ai_agents(active); + +-- Seed the three built-in agents (only if they don't already exist) +INSERT INTO ai_agents (name, slug, purpose, model_provider, model_name, system_prompt, prompt_version, schema_version, source) +SELECT * FROM (VALUES + ( + 'Document Intelligence Extractor', + 'document-extractor', + 'Extracts structured intelligence (sentiment, catalysts, impact scores, key facts, risks) from company news, SEC filings, earnings transcripts, and press releases.', + 'ollama', + 'llama3.1:8b', + 'You are a financial document analyst. Extract structured data as JSON. Return ONLY a single JSON object. No markdown fences, no explanation, no text before or after the JSON. Every field in the schema is required. Use "other" for catalyst_type if unsure. Keep evidence_spans short (under 20 words each). Keep key_facts to 3-5 items max.', + 'document-intel-v2', + '2.0.0', + 'system' + ), + ( + 'Global Event Classifier', + 'event-classifier', + 'Classifies global/geopolitical news into structured macro events with impact type, severity, affected regions/sectors/commodities, and estimated duration.', + 'ollama', + 'llama3.1:8b', + 'Classify this global news article as a macro event. Fill every field. RULES: - Only extract facts EXPLICITLY stated in the article - Do NOT infer geopolitical implications not stated - Distinguish between announced policy and rumored policy - If severity is unclear, default to "low" - confidence: 0.0-1.0 your confidence in this classification', + 'event-classification-v1', + '1.0.0', + 'system' + ), + ( + 'Thesis Rewriter', + 'thesis-rewriter', + 'Rewrites deterministic trade thesis summaries into clear, professional analyst prose. Optional layer — system falls back to deterministic thesis if this fails.', + 'ollama', + 'llama3.1:8b', + 'You are a concise financial analyst. You rewrite structured trade thesis summaries into clear, professional prose suitable for an internal research note. STRICT RULES: 1. Do NOT add any information not present in the input. 2. Do NOT fabricate numbers, dates, company names. 3. Keep under 150 words. 4. Preserve all factual claims, risk notes, evidence counts. 5. Neutral, professional tone. 6. Return ONLY the rewritten thesis text.', + 'thesis-rewrite-v1', + '1.0.0', + 'system' + ) +) AS v(name, slug, purpose, model_provider, model_name, system_prompt, prompt_version, schema_version, source) +WHERE NOT EXISTS (SELECT 1 FROM ai_agents WHERE ai_agents.slug = v.slug); + +-- Agent performance log: per-invocation metrics linked to agent config. +-- This supplements model_performance_metrics with agent-level attribution. +CREATE TABLE IF NOT EXISTS agent_performance_log ( + id UUID PRIMARY KEY DEFAULT gen_random_uuid(), + agent_id UUID NOT NULL REFERENCES ai_agents(id) ON DELETE CASCADE, + document_id UUID REFERENCES documents(id) ON DELETE SET NULL, + ticker VARCHAR(20), + success BOOLEAN NOT NULL, + duration_ms INTEGER NOT NULL DEFAULT 0, + confidence FLOAT DEFAULT 0.0, + retry_count INTEGER DEFAULT 0, + input_tokens INTEGER DEFAULT 0, + output_tokens INTEGER DEFAULT 0, + error_message TEXT, + recorded_at TIMESTAMPTZ NOT NULL DEFAULT NOW() +); + +CREATE INDEX IF NOT EXISTS idx_agent_perf_agent ON agent_performance_log(agent_id, recorded_at DESC); +CREATE INDEX IF NOT EXISTS idx_agent_perf_time ON agent_performance_log(recorded_at DESC); diff --git a/services/api/app.py b/services/api/app.py index 6f3a1e8..8aa8fdf 100644 --- a/services/api/app.py +++ b/services/api/app.py @@ -2640,3 +2640,190 @@ async def get_decision_history( "decisions": decisions, "count": len(decisions), } + +# --------------------------------------------------------------------------- +# AI Agents (Editable agent configurations + performance tracking) +# --------------------------------------------------------------------------- + + +class AgentUpdateBody(BaseModel): + name: Optional[str] = None + purpose: Optional[str] = None + model_provider: Optional[str] = None + model_name: Optional[str] = None + system_prompt: Optional[str] = None + user_prompt_template: Optional[str] = None + prompt_version: Optional[str] = None + schema_version: Optional[str] = None + temperature: Optional[float] = None + max_tokens: Optional[int] = None + timeout_seconds: Optional[int] = None + max_retries: Optional[int] = None + active: Optional[bool] = None + + +class AgentCreateBody(BaseModel): + name: str + slug: str + purpose: str = "" + model_provider: str = "ollama" + model_name: str = "llama3.1:8b" + system_prompt: str = "" + user_prompt_template: str = "" + prompt_version: str = "" + schema_version: str = "1.0.0" + temperature: float = 0.0 + max_tokens: int = 32768 + timeout_seconds: int = 120 + max_retries: int = 2 + + +@app.get("/api/agents") +async def list_agents(active_only: bool = False): + """List all AI agent configurations.""" + where = "WHERE active = TRUE" if active_only else "" + rows = await pool.fetch( + f"""SELECT id, name, slug, purpose, model_provider, model_name, + system_prompt, user_prompt_template, prompt_version, + schema_version, temperature, max_tokens, timeout_seconds, + max_retries, active, source, created_at, updated_at + FROM ai_agents {where} + ORDER BY source DESC, name ASC""" + ) + return [_row_to_dict(r) for r in rows] + + +@app.get("/api/agents/{agent_id}") +async def get_agent(agent_id: str): + """Get a single agent configuration.""" + row = await pool.fetchrow( + """SELECT id, name, slug, purpose, model_provider, model_name, + system_prompt, user_prompt_template, prompt_version, + schema_version, temperature, max_tokens, timeout_seconds, + max_retries, active, source, created_at, updated_at + FROM ai_agents WHERE id = $1""", + agent_id, + ) + if not row: + raise HTTPException(404, "Agent not found") + return _row_to_dict(row) + + +@app.post("/api/agents", status_code=201) +async def create_agent(body: AgentCreateBody): + """Create a new user-defined agent.""" + row = await pool.fetchrow( + """INSERT INTO ai_agents ( + name, slug, purpose, model_provider, model_name, + system_prompt, user_prompt_template, prompt_version, + schema_version, temperature, max_tokens, timeout_seconds, + max_retries, source + ) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, 'user') + RETURNING id, name, slug, source, created_at""", + body.name, body.slug, body.purpose, body.model_provider, body.model_name, + body.system_prompt, body.user_prompt_template, body.prompt_version, + body.schema_version, body.temperature, body.max_tokens, body.timeout_seconds, + body.max_retries, + ) + return _row_to_dict(row) + + +@app.put("/api/agents/{agent_id}") +async def update_agent(agent_id: str, body: AgentUpdateBody): + """Update an agent configuration. + + Both system and user agents can be edited. User changes are preserved + across reinstalls because migration 026 only inserts system agents + that don't already exist (by slug). + """ + updates: list[str] = [] + params: list[Any] = [] + idx = 1 + + for field_name, value in body.model_dump(exclude_none=True).items(): + updates.append(f"{field_name} = ${idx}") + params.append(value) + idx += 1 + + if not updates: + raise HTTPException(400, "No fields to update") + + updates.append("updated_at = NOW()") + set_clause = ", ".join(updates) + params.append(agent_id) + + row = await pool.fetchrow( + f"""UPDATE ai_agents SET {set_clause} + WHERE id = ${idx} + RETURNING id, name, slug, purpose, model_provider, model_name, + system_prompt, user_prompt_template, prompt_version, + schema_version, temperature, max_tokens, timeout_seconds, + max_retries, active, source, created_at, updated_at""", + *params, + ) + if not row: + raise HTTPException(404, "Agent not found") + return _row_to_dict(row) + + +@app.delete("/api/agents/{agent_id}") +async def delete_agent(agent_id: str): + """Delete a user-created agent. System agents cannot be deleted.""" + row = await pool.fetchrow( + "SELECT source FROM ai_agents WHERE id = $1", agent_id, + ) + if not row: + raise HTTPException(404, "Agent not found") + if row["source"] == "system": + raise HTTPException(403, "Cannot delete system agents — deactivate instead") + + await pool.execute("DELETE FROM ai_agents WHERE id = $1", agent_id) + return {"deleted": True} + + +@app.get("/api/agents/{agent_id}/performance") +async def get_agent_performance(agent_id: str, hours: int = Query(default=24, le=720)): + """Get aggregated performance metrics for an agent.""" + row = await pool.fetchrow( + """SELECT + COUNT(*) AS total_invocations, + COUNT(*) FILTER (WHERE success) AS successes, + COUNT(*) FILTER (WHERE NOT success) AS failures, + ROUND(AVG(duration_ms)::numeric) AS avg_duration_ms, + ROUND(PERCENTILE_CONT(0.95) WITHIN GROUP (ORDER BY duration_ms)::numeric) AS p95_duration_ms, + ROUND(AVG(confidence)::numeric, 4) AS avg_confidence, + ROUND(AVG(retry_count)::numeric, 2) AS avg_retries, + SUM(input_tokens) AS total_input_tokens, + SUM(output_tokens) AS total_output_tokens + FROM agent_performance_log + WHERE agent_id = $1 + AND recorded_at >= NOW() - make_interval(hours => $2)""", + agent_id, hours, + ) + d = _row_to_dict(row) if row else {} + total = int(d.get("total_invocations", 0) or 0) + successes = int(d.get("successes", 0) or 0) + d["success_rate"] = round(successes / total, 4) if total > 0 else None + return d + + +@app.get("/api/agents/{agent_id}/performance/history") +async def get_agent_performance_history( + agent_id: str, + hours: int = Query(default=24, le=720), +): + """Get hourly performance time-series for an agent.""" + rows = await pool.fetch( + """SELECT + date_trunc('hour', recorded_at) AS hour, + COUNT(*) AS invocations, + COUNT(*) FILTER (WHERE success) AS successes, + ROUND(AVG(duration_ms)::numeric) AS avg_duration_ms, + ROUND(AVG(confidence)::numeric, 4) AS avg_confidence + FROM agent_performance_log + WHERE agent_id = $1 + AND recorded_at >= NOW() - make_interval(hours => $2) + GROUP BY 1 ORDER BY 1""", + agent_id, hours, + ) + return [_row_to_dict(r) for r in rows]
Select an agent to view its configuration and performance, or create a new one.
{agent.purpose}
{agent.system_prompt || '(none)'}
Failed to save
Failed to create agent