import m, {MithrilComponent} from '@garage44/mithril' import {Alert, Badge, Button, Card, Dialog, FieldText} from '@garage44/mithril/components' import {journeyOf} from '@/components/landing' import {act} from '@/lib/actions' import {api} from '@/lib/api' import type {Schedule, Template, TemplateRun, TemplatesState} from '@/lib/api' import {age, ahead, shortName} from '@/lib/format' import {shortWords, underway} from '@/lib/landing' import {speaker} from '@/lib/names' import {askProjects, projectName, projects} from '@/lib/projects' import {bind, local} from '@/lib/reactive' import {go, here} from '@/lib/route' import {steers} from '@/lib/store' import {hosted} from '@/lib/stream' import type {Hosted} from '@/lib/stream' /** * The autopilot (autopilot.rs, templates.rs): an agent that answers a * session's questions and requests for the person — landing included — and * says what next when it stops; turned on per session (the chip under the * message box), for many at once (Take over), or for every session a * template starts, now or on a schedule. Its colour is its own (`--pilot`), * so what it decided never reads as the person's. */ /** Who it is in the log. */ export const PILOT = 'autopilot' /** Its mark: a small plane. */ export const plane = (size = 14) => ( ) /** What it decided and why, under the request or message it is about. */ export function pilotWhy(label: string, why: string | null | undefined, tone: '' | 'left' = ''): m.Children { return (
{plane(12)} {label}
{why ?
{why}
: null}
) } /** While it decides a request: said on the request, whose buttons stay. */ export function pilotDeciding(): m.Children { return (
The autopilot is reading the conversation and the code. You can still answer yourself.
) } /** Its state, for the session's line in the list and its head: none when it is off. */ export function pilotState(s: Hosted): {text: string; tone: 'pilot' | 'you'} | null { const p = s.autopilot if (!p?.on || s.finished || s.status === 'stopped' || s.status === 'failed') return null if (p.state === 'left') return {text: p.left_urgent === false ? 'autopilot left this for your next block' : 'autopilot leaves this to you', tone: 'you'} if (p.state === 'deciding') return {text: 'autopilot · deciding…', tone: 'pilot'} return null } /** Until when, in words. */ function untilWords(until: number | null | undefined): string { if (!until) return 'until you stop it' const d = new Date(until * 1000) const today = new Date() const t = d.toLocaleTimeString([], {hour: '2-digit', minute: '2-digit'}) return d.toDateString() === today.toDateString() || (until - Date.now() / 1000 < 3600 && d.getHours() === 0) ? `until ${t}` : `until ${d.toLocaleDateString([], {weekday: 'short'})} ${t}` } /** What a turned-on line says: who, and until when. */ export function pilotLine(on: boolean, by: string | undefined, until: number | null | undefined, why?: string | null): string { if (!on) return `autopilot off${why ? ` · ${why}` : by && by !== PILOT ? ` · ${speaker(by).title}` : ''}` return `autopilot on · ${speaker(by ?? '?').title} · ${untilWords(until)}` } // ── Turning it on ─────────────────────────────────────────────────────────── /** How long it stays on: an hour, the rest of today, or until turned off. */ const SPANS = [ {id: 'hour', name: '1 hour'}, {id: 'today', name: 'today'}, {id: 'stop', name: 'until I stop it'}, ] function untilOf(span: string): number | null { if (span === 'hour') return Math.floor(Date.now() / 1000) + 3600 if (span === 'today') { const d = new Date() d.setHours(24, 0, 0, 0) return Math.floor(d.getTime() / 1000) } return null } /** A row of choices, one on. */ export function seg(options: {id: string; name: string}[], value: string, set: (id: string) => void, disabled = false): m.Children { return (
{options.map((o) => ( ))}
) } /** The sheet that turns it on, for one session or several (`ids`). */ const sheet = local({open: false, ids: [] as string[], goal: '', span: 'today', sending: false}) export function openPilotSheet(ids: string[]) { Object.assign(sheet, {open: true, ids, goal: '', span: 'today', sending: false}) } /** Mounted once, in the frame, as the confirm dialog is. */ export class PilotHost extends MithrilComponent { view() { const n = sheet.ids.length const go = async () => { sheet.sending = true const until = untilOf(sheet.span) const ok = await act(n === 1 ? 'autopilot on' : `autopilot on for ${n} sessions`, async () => { if (n === 1) await api.setAutopilot(sheet.ids[0]!, true, until, sheet.goal.trim()) else await api.takeOver(sheet.ids, sheet.goal.trim(), until) }) sheet.sending = false if (ok) sheet.open = false } return ( (sheet.open = false)}> Cancel , , ]} >