|
| 1 | +// A bot asking for a routine (spec 20.12), for itself or for another bot |
| 2 | +// of its crew. The owner can change the name, what to do and when before |
| 3 | +// creating it, or say no with a note for the bot. A refusal the daemon |
| 4 | +// explains with a reason is worded here. Once answered it shrinks to one |
| 5 | +// line. |
| 6 | + |
| 7 | +import { AlarmClock, Ban, Check, ChevronRight, TimerOff } from "lucide-react"; |
| 8 | +import { useId, useState } from "react"; |
| 9 | +import { useT } from "../../i18n"; |
| 10 | +import { errorText } from "../../lib/api"; |
| 11 | +import { type ApprovalItem, type Bot, FIELD_LIMITS } from "../../lib/protocol.gen"; |
| 12 | +import { RpcError } from "../../lib/rpc"; |
| 13 | +import { useApi, useApp } from "../../store/context"; |
| 14 | +import { Button } from "../../ui/Button"; |
| 15 | +import { TextArea, TextField } from "../../ui/Field"; |
| 16 | +import { describeSchedule, systemZone } from "./describe"; |
| 17 | +import { formOf, type RoutineFields, type RoutineForm, scheduleOf } from "./form"; |
| 18 | +import { MoreOptions } from "./MoreOptions"; |
| 19 | +import { ScheduleFields } from "./ScheduleFields"; |
| 20 | + |
| 21 | +export const ROUTINE_TOOL = "mcp__botloft__schedule_routine"; |
| 22 | + |
| 23 | +/** The routine asked for, and the bot it is for by handle. */ |
| 24 | +interface Asked { |
| 25 | + routine: RoutineFields; |
| 26 | + bot?: string; |
| 27 | +} |
| 28 | + |
| 29 | +/** The routine in the request's input; defaults for what is cut short. */ |
| 30 | +export function askedOf(input: string): Asked { |
| 31 | + const blank: RoutineFields = { |
| 32 | + name: "", |
| 33 | + prompt: "", |
| 34 | + schedule: { kind: "weekly", days: [1, 2, 3, 4, 5], time: "09:00" }, |
| 35 | + timezone: systemZone(), |
| 36 | + overlap: "skip", |
| 37 | + missed: "run_once", |
| 38 | + }; |
| 39 | + try { |
| 40 | + const { bot, ...fields } = JSON.parse(input) as Partial<RoutineFields> & { bot?: string }; |
| 41 | + return { routine: { ...blank, ...fields }, ...(bot && { bot }) }; |
| 42 | + } catch { |
| 43 | + return { routine: { ...blank, prompt: input } }; |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +function formFields(form: RoutineForm): RoutineFields { |
| 48 | + return { |
| 49 | + name: form.name, |
| 50 | + prompt: form.prompt, |
| 51 | + schedule: scheduleOf(form), |
| 52 | + timezone: form.timezone, |
| 53 | + overlap: form.overlap, |
| 54 | + missed: form.missed, |
| 55 | + }; |
| 56 | +} |
| 57 | + |
| 58 | +/** The bot the routine is for: the one that asked, or one of its crew. */ |
| 59 | +function useRunner(bot: Bot, handle: string | undefined): Bot { |
| 60 | + const bots = useApp((state) => state.bots); |
| 61 | + if (!handle) { |
| 62 | + return bot; |
| 63 | + } |
| 64 | + const found = Object.values(bots).find( |
| 65 | + (other) => other.crewId === bot.crewId && other.handle === handle, |
| 66 | + ); |
| 67 | + return found ?? { ...bot, name: `@${handle}` }; |
| 68 | +} |
| 69 | + |
| 70 | +export function RoutineRequestCard({ approval, bot }: { approval: ApprovalItem; bot: Bot }) { |
| 71 | + const t = useT(); |
| 72 | + const r = t.chat.routineRequest; |
| 73 | + const d = t.routines.dialog; |
| 74 | + const api = useApi(); |
| 75 | + const asked = askedOf(approval.input); |
| 76 | + const runner = useRunner(bot, asked.bot); |
| 77 | + const [form, setForm] = useState<RoutineForm>(() => formOf(asked.routine)); |
| 78 | + const [note, setNote] = useState(""); |
| 79 | + const [busy, setBusy] = useState(false); |
| 80 | + const [error, setError] = useState<string | null>(null); |
| 81 | + const noteId = useId(); |
| 82 | + if (approval.status !== "pending") { |
| 83 | + return <Answered approval={approval} />; |
| 84 | + } |
| 85 | + const change = (patch: Partial<RoutineForm>) => setForm((current) => ({ ...current, ...patch })); |
| 86 | + |
| 87 | + const answer = async (allow: boolean) => { |
| 88 | + setBusy(true); |
| 89 | + setError(null); |
| 90 | + const edited = formFields(form); |
| 91 | + const changed = JSON.stringify(edited) !== JSON.stringify(formFields(formOf(asked.routine))); |
| 92 | + const trimmed = note.trim(); |
| 93 | + try { |
| 94 | + await api.call("approvals.answer", { |
| 95 | + approvalId: approval.approvalId, |
| 96 | + allow, |
| 97 | + ...(!allow && trimmed ? { note: trimmed } : {}), |
| 98 | + ...(allow && changed ? { input: JSON.stringify(edited) } : {}), |
| 99 | + }); |
| 100 | + } catch (failure) { |
| 101 | + const reason = failure instanceof RpcError ? failure.reason : undefined; |
| 102 | + const worded = |
| 103 | + reason && reason in d.reasons ? d.reasons[reason as keyof typeof d.reasons] : null; |
| 104 | + setError(worded ?? `${allow ? r.createFailed : r.declineFailed}: ${errorText(failure)}`); |
| 105 | + } |
| 106 | + setBusy(false); |
| 107 | + }; |
| 108 | + |
| 109 | + const title = asked.bot ? r.titleFor(bot.name, runner.name) : r.title(bot.name); |
| 110 | + return ( |
| 111 | + <section |
| 112 | + aria-label={title} |
| 113 | + className="my-1 overflow-hidden rounded-2xl border border-line bg-panel shadow-sm animate-attention" |
| 114 | + > |
| 115 | + <p className="flex items-center gap-2.5 border-line border-b px-4 py-3 font-semibold text-sm"> |
| 116 | + <span className="grid h-7 w-7 place-items-center rounded-full bg-accent/12 text-accent"> |
| 117 | + <AlarmClock aria-hidden size={15} /> |
| 118 | + </span> |
| 119 | + {title} |
| 120 | + </p> |
| 121 | + <div className="flex flex-col gap-4 px-4 py-3"> |
| 122 | + <TextField |
| 123 | + label={d.name} |
| 124 | + value={form.name} |
| 125 | + max={80} |
| 126 | + onChange={(event) => change({ name: event.target.value })} |
| 127 | + /> |
| 128 | + <TextArea |
| 129 | + label={d.prompt(runner.name)} |
| 130 | + value={form.prompt} |
| 131 | + max={FIELD_LIMITS.message} |
| 132 | + rows={4} |
| 133 | + onChange={(event) => change({ prompt: event.target.value })} |
| 134 | + /> |
| 135 | + <ScheduleFields form={form} change={change} /> |
| 136 | + <MoreOptions form={form} change={change} /> |
| 137 | + <p className="text-muted text-xs">{r.explain(runner.name)}</p> |
| 138 | + {error && ( |
| 139 | + <p role="alert" className="text-danger text-sm"> |
| 140 | + {error} |
| 141 | + </p> |
| 142 | + )} |
| 143 | + </div> |
| 144 | + <div className="border-line border-t bg-canvas/40 px-4 py-3"> |
| 145 | + <label htmlFor={noteId} className="sr-only"> |
| 146 | + {r.noteLabel(bot.name)} |
| 147 | + </label> |
| 148 | + <textarea |
| 149 | + id={noteId} |
| 150 | + rows={2} |
| 151 | + value={note} |
| 152 | + onChange={(event) => setNote(event.target.value)} |
| 153 | + placeholder={r.notePlaceholder(bot.name)} |
| 154 | + className="block w-full resize-none rounded-xl border border-line-strong bg-panel px-3 py-2 text-sm outline-none placeholder:text-muted focus:border-muted" |
| 155 | + /> |
| 156 | + <div className="mt-2.5 flex flex-wrap gap-2"> |
| 157 | + <Button |
| 158 | + variant="primary" |
| 159 | + icon={Check} |
| 160 | + disabled={busy || !form.name.trim() || !form.prompt.trim()} |
| 161 | + onClick={() => answer(true)} |
| 162 | + > |
| 163 | + {r.create} |
| 164 | + </Button> |
| 165 | + <Button icon={Ban} disabled={busy} onClick={() => answer(false)}> |
| 166 | + {r.decline} |
| 167 | + </Button> |
| 168 | + </div> |
| 169 | + </div> |
| 170 | + </section> |
| 171 | + ); |
| 172 | +} |
| 173 | + |
| 174 | +/** One line once answered; what was asked stays a click away. */ |
| 175 | +function Answered({ approval }: { approval: ApprovalItem }) { |
| 176 | + const t = useT(); |
| 177 | + const r = t.chat.routineRequest; |
| 178 | + const { routine } = askedOf(approval.input); |
| 179 | + const name = routine.name || t.routines.dialog.name; |
| 180 | + const [Icon, text, tone] = |
| 181 | + approval.status === "allowed" |
| 182 | + ? [Check, r.created(name), "text-ok"] |
| 183 | + : approval.status === "denied" |
| 184 | + ? [Ban, r.declined(name), "text-danger"] |
| 185 | + : [TimerOff, r.expired(name), "text-quiet"]; |
| 186 | + return ( |
| 187 | + <details className="group"> |
| 188 | + <summary className="flex min-w-0 cursor-default items-center gap-2 rounded-lg px-1.5 py-1 text-sm hover:bg-sunken"> |
| 189 | + <Icon aria-hidden size={14} className={`shrink-0 ${tone}`} /> |
| 190 | + <span className="shrink-0 font-medium">{text}</span> |
| 191 | + {approval.note && ( |
| 192 | + <span className="truncate text-muted text-xs">{`“${approval.note}”`}</span> |
| 193 | + )} |
| 194 | + <ChevronRight |
| 195 | + aria-hidden |
| 196 | + size={13} |
| 197 | + className="shrink-0 text-muted transition-transform group-open:rotate-90" |
| 198 | + /> |
| 199 | + </summary> |
| 200 | + <dl |
| 201 | + className="mt-1.5 ml-6 grid grid-cols-[auto_1fr] gap-x-3 gap-y-1 rounded-xl border border-line bg-panel px-4 py-3 text-sm" |
| 202 | + data-selectable |
| 203 | + > |
| 204 | + <dt className="text-muted">{t.routines.dialog.when}</dt> |
| 205 | + <dd>{describeSchedule(routine.schedule, t.routines.when)}</dd> |
| 206 | + <dt className="text-muted">{t.routines.dialog.name}</dt> |
| 207 | + <dd>{routine.name}</dd> |
| 208 | + <dd className="col-span-2 whitespace-pre-wrap">{routine.prompt}</dd> |
| 209 | + </dl> |
| 210 | + </details> |
| 211 | + ); |
| 212 | +} |
0 commit comments