'use client' // Automations ("hey superbrain"): compile a natural-language request into a // validated task proposal, let the user confirm/edit it, then manage tasks // and watch their runs. The server compiles AND re-validates; this UI never // treats the model's output as trusted — the proposal is shown in plain // language plus its raw spec before anything is stored. import { useCallback, useEffect, useState } from 'react' import { Button } from '@/components/ui/button' import { Input } from '@/components/ui/input' import { Switch } from '@/components/ui/switch' import { Badge } from '@/components/ui/badge' import { Textarea } from '@/components/ui/textarea' import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@/components/ui/select' import { Sparkles, Trash2, Play, Loader2, Wand2 } from 'lucide-react' import { SafeTime } from '@/components/safe-format' import { toast } from 'sonner' type Condition = { type: string; url?: string; question?: string; op?: string threshold?: number; text?: string; keyword?: string } type ActionT = { type: string; channelId?: string; to?: string title?: string; body?: string; url?: string } type Spec = { condition: Condition; action: ActionT; pollMinutes: number; once: boolean } type Task = { id: string; name: string; spec: Spec; summary: string; enabled: boolean status: string; fireCount: number; lastCheckedAt: string | null lastFiredAt: string | null; version: number; createdAt: string } type Run = { id: string; taskId: string; outcome: string; detail: string | null; createdAt: string } type ChannelOption = { id: string; name: string; kind: string; enabled: boolean } async function api(path: string, init?: RequestInit): Promise { return fetch(path, { headers: { 'Content-Type': 'application/json' }, cache: 'no-store', ...init }) } export function AutomationsSettings() { const [tasks, setTasks] = useState([]) const [runs, setRuns] = useState([]) const [channels, setChannels] = useState([]) const [loading, setLoading] = useState(true) const [available, setAvailable] = useState(true) // Composer state const [text, setText] = useState('') const [compiling, setCompiling] = useState(false) const [proposal, setProposal] = useState<{ name: string; spec: Spec; summary: string } | null>(null) const [showRaw, setShowRaw] = useState(false) const [runningId, setRunningId] = useState(null) const reload = useCallback(() => Promise.all([ api('/api/agent/tasks').then(r => r.json()), api('/api/agent/runs').then(r => r.json()), api('/api/notifications/channels').then(r => r.json()), api('/api/ai/status').then(r => r.json()).catch(() => null), ]) .then(([tk, rn, ch, st]) => { if (Array.isArray(tk)) setTasks(tk) if (Array.isArray(rn)) setRuns(rn.slice(0, 10)) if (Array.isArray(ch)) setChannels(ch) if (st && typeof st === 'object') setAvailable(st.effective !== 'none') }) .catch(() => { toast.error('Could not load automations') }) .finally(() => { setLoading(false) }), []) useEffect(() => { reload() }, [reload]) const reportError = async (res: Response, fallback: string): Promise => { let msg = fallback try { const data = await res.json() if (data?.error) msg = data.error } catch { /* non-JSON body */ } if (res.status === 428) setAvailable(false) toast.error(msg) return msg } const compile = async () => { if (!text.trim()) { toast.error('Describe what should happen first'); return } setCompiling(true) setProposal(null) try { const res = await api('/api/agent/compile', { method: 'POST', body: JSON.stringify({ text }) }) if (res.ok) { const data = await res.json() setProposal({ name: data.name, spec: data.spec, summary: data.summary }) setAvailable(true) } else { await reportError(res, 'Could not understand that request') } } catch { toast.error('Could not reach the AI service') } finally { setCompiling(false) } } const createTask = async () => { if (!proposal) return // A compiled notify action has no channel id (the model never knows // them); the user must pick one before creation. if (proposal.spec.action.type === 'notify' && !proposal.spec.action.channelId) { toast.error('Pick a notification channel for this task') return } const res = await api('/api/agent/tasks', { method: 'POST', body: JSON.stringify({ name: proposal.name, spec: proposal.spec }), }) if (res.ok) { toast.success('Automation created') setProposal(null) setText('') await reload() } else { await reportError(res, 'Could not create the automation') } } const toggleTask = async (t: Task, enabled: boolean) => { const res = await api(`/api/agent/tasks/${t.id}`, { method: 'PATCH', body: JSON.stringify({ enabled, version: t.version }), }) if (res.ok) await reload() else await reportError(res, 'Could not update the task') } const runNow = async (t: Task) => { setRunningId(t.id) try { const res = await api(`/api/agent/tasks/${t.id}/run`, { method: 'POST' }) if (res.ok) { const data = await res.json() toast.info(`Run finished: ${data.outcome}`) await reload() } else { await reportError(res, 'Run failed') } } finally { setRunningId(null) } } const removeTask = async (t: Task) => { const res = await api(`/api/agent/tasks/${t.id}`, { method: 'DELETE' }) if (res.ok) { toast.success('Automation deleted'); await reload() } else await reportError(res, 'Could not delete the task') } if (loading) { return
Loading automations…
} return (
{/* Composer */}

Hey Superbrain

Describe an automation in plain language. Superbrain compiles it into a task you review before anything is saved. It can watch a public web page and answer one question about it (prices, availability, a phrase), or react to new mailbox signals — then notify you, email someone, or save a bookmark. It cannot log in to sites, book anything, or chain multiple sites yet.

{!available && (

No AI is available to you yet — open the AI tab in Settings and add your own provider and API key{` `} (or your plan may include the server-provided key once the operator enables it). Signal-condition tasks run without AI.

)}