|
| 1 | +// Settings, "General": where the experimental agents' programs are. Botloft |
| 2 | +// looks in the usual places; when someone installed one somewhere else (mise, |
| 3 | +// npm, a folder of their own), they say where here (spec 30). |
| 4 | + |
| 5 | +import { useState } from "react"; |
| 6 | +import { useT } from "../../i18n"; |
| 7 | +import type { AgentCheck } from "../../lib/protocol.gen"; |
| 8 | +import { useApi, useApp } from "../../store/context"; |
| 9 | +import { Button } from "../../ui/Button"; |
| 10 | +import { TextField } from "../../ui/Field"; |
| 11 | +import { attempt } from "../../ui/toast"; |
| 12 | + |
| 13 | +const NO_CHECKS: AgentCheck[] = []; |
| 14 | + |
| 15 | +export function AgentPrograms() { |
| 16 | + const checks = useApp((state) => state.system?.agentChecks ?? NO_CHECKS); |
| 17 | + if (checks.length === 0) { |
| 18 | + return null; |
| 19 | + } |
| 20 | + return ( |
| 21 | + <div className="flex flex-col gap-4"> |
| 22 | + {checks.map((check) => ( |
| 23 | + <Program key={check.agent} check={check} /> |
| 24 | + ))} |
| 25 | + </div> |
| 26 | + ); |
| 27 | +} |
| 28 | + |
| 29 | +function Program({ check }: { check: AgentCheck }) { |
| 30 | + const t = useT(); |
| 31 | + const s = t.account.settings; |
| 32 | + const api = useApi(); |
| 33 | + const system = useApp((state) => state.system); |
| 34 | + const putSystem = useApp((state) => state.putSystem); |
| 35 | + const name = t.bots.dialog.agent.names[check.agent]; |
| 36 | + const [text, setText] = useState(""); |
| 37 | + const [busy, setBusy] = useState(false); |
| 38 | + |
| 39 | + const found = check.path !== null; |
| 40 | + const status = found |
| 41 | + ? check.version === null |
| 42 | + ? s.programFoundNoVersion(check.path ?? "") |
| 43 | + : s.programFound(check.version, check.path ?? "") |
| 44 | + : s.programMissing(name); |
| 45 | + |
| 46 | + const save = async (path: string) => { |
| 47 | + setBusy(true); |
| 48 | + const saved = await attempt(s.programFailed, async () => { |
| 49 | + const next = await api.call("agents.setPath", { agent: check.agent, path }); |
| 50 | + if (system) { |
| 51 | + putSystem({ |
| 52 | + ...system, |
| 53 | + agentChecks: system.agentChecks.map((have) => (have.agent === next.agent ? next : have)), |
| 54 | + }); |
| 55 | + } |
| 56 | + }); |
| 57 | + setBusy(false); |
| 58 | + if (saved) { |
| 59 | + setText(""); |
| 60 | + } |
| 61 | + }; |
| 62 | + |
| 63 | + return ( |
| 64 | + <div className="flex flex-col gap-2"> |
| 65 | + <p className="font-medium text-sm">{name}</p> |
| 66 | + <p |
| 67 | + className={`text-xs leading-relaxed ${found ? "text-muted" : "text-warn"}`} |
| 68 | + data-selectable |
| 69 | + > |
| 70 | + {status} |
| 71 | + </p> |
| 72 | + <div className="flex items-end gap-2"> |
| 73 | + <div className="min-w-0 flex-1"> |
| 74 | + <TextField |
| 75 | + label={s.programPath(name)} |
| 76 | + value={text} |
| 77 | + placeholder={s.programPlaceholder} |
| 78 | + onChange={(event) => setText(event.target.value)} |
| 79 | + /> |
| 80 | + </div> |
| 81 | + <Button |
| 82 | + variant="secondary" |
| 83 | + disabled={busy || text.trim() === ""} |
| 84 | + onClick={() => save(text)} |
| 85 | + > |
| 86 | + {s.programSave} |
| 87 | + </Button> |
| 88 | + </div> |
| 89 | + <p className="text-muted text-xs leading-relaxed">{s.programHint}</p> |
| 90 | + </div> |
| 91 | + ); |
| 92 | +} |
0 commit comments