|
| 1 | +// The crew's chief suggesting a new bot (spec 10.2). The owner can change |
| 2 | +// the name, role, model and instructions before creating it, or say no |
| 3 | +// with a note for the chief. Once answered it shrinks to one line. |
| 4 | + |
| 5 | +import { Ban, Check, ChevronRight, TimerOff, UserPlus } from "lucide-react"; |
| 6 | +import { useId, useState } from "react"; |
| 7 | +import { useT } from "../../i18n"; |
| 8 | +import type { ApprovalItem, Bot, BotModel } from "../../lib/protocol.gen"; |
| 9 | +import { Button } from "../../ui/Button"; |
| 10 | +import { SelectField, TextArea, TextField } from "../../ui/Field"; |
| 11 | +import { useAnswer } from "./useAnswer"; |
| 12 | + |
| 13 | +export const SUGGEST_TOOL = "mcp__botloft__suggest_bot"; |
| 14 | + |
| 15 | +const MODELS: BotModel[] = ["default", "fable", "opus", "sonnet", "haiku"]; |
| 16 | + |
| 17 | +interface Suggestion { |
| 18 | + name: string; |
| 19 | + role: string; |
| 20 | + instructions: string; |
| 21 | + model?: BotModel; |
| 22 | + reason: string; |
| 23 | +} |
| 24 | + |
| 25 | +/** The suggestion in the request's input; empty fields if it is cut short. */ |
| 26 | +export function suggestionOf(input: string): Suggestion { |
| 27 | + try { |
| 28 | + const parsed = JSON.parse(input) as Partial<Suggestion>; |
| 29 | + return { |
| 30 | + name: parsed.name ?? "", |
| 31 | + role: parsed.role ?? "", |
| 32 | + instructions: parsed.instructions ?? "", |
| 33 | + ...(parsed.model && { model: parsed.model }), |
| 34 | + reason: parsed.reason ?? "", |
| 35 | + }; |
| 36 | + } catch { |
| 37 | + return { name: "", role: "", instructions: input, reason: "" }; |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +export function SuggestionCard({ approval, bot }: { approval: ApprovalItem; bot: Bot }) { |
| 42 | + const t = useT(); |
| 43 | + const s = t.chat.suggestion; |
| 44 | + const asked = suggestionOf(approval.input); |
| 45 | + const [edited, setEdited] = useState<Suggestion>(asked); |
| 46 | + const { note, setNote, busy, answer } = useAnswer(approval, { |
| 47 | + allow: s.createFailed, |
| 48 | + deny: s.declineFailed, |
| 49 | + }); |
| 50 | + const noteId = useId(); |
| 51 | + if (approval.status !== "pending") { |
| 52 | + return <Answered approval={approval} />; |
| 53 | + } |
| 54 | + const changed = JSON.stringify(edited) !== JSON.stringify(asked); |
| 55 | + const set = (field: keyof Suggestion) => (value: string) => |
| 56 | + setEdited((current) => ({ ...current, [field]: value })); |
| 57 | + |
| 58 | + return ( |
| 59 | + <section |
| 60 | + aria-label={s.title(bot.name)} |
| 61 | + className="my-1 overflow-hidden rounded-2xl border border-line bg-panel shadow-sm" |
| 62 | + > |
| 63 | + <p className="flex items-center gap-2.5 border-line border-b px-4 py-3 font-semibold text-sm"> |
| 64 | + <span className="grid h-7 w-7 place-items-center rounded-full bg-accent/12 text-accent"> |
| 65 | + <UserPlus aria-hidden size={15} /> |
| 66 | + </span> |
| 67 | + {s.title(bot.name)} |
| 68 | + </p> |
| 69 | + <div className="flex flex-col gap-3 px-4 py-3"> |
| 70 | + {asked.reason && ( |
| 71 | + <p className="text-ink-soft text-sm" data-selectable> |
| 72 | + <span className="font-medium text-ink">{s.why}: </span> |
| 73 | + {asked.reason} |
| 74 | + </p> |
| 75 | + )} |
| 76 | + <div className="grid grid-cols-[1fr_12rem] gap-3"> |
| 77 | + <TextField |
| 78 | + label={s.name} |
| 79 | + value={edited.name} |
| 80 | + onChange={(event) => set("name")(event.target.value)} |
| 81 | + /> |
| 82 | + <SelectField |
| 83 | + label={s.model} |
| 84 | + value={edited.model ?? "default"} |
| 85 | + onChange={(event) => set("model")(event.target.value)} |
| 86 | + options={MODELS.map((value) => ({ value, label: t.chat.model.names[value] }))} |
| 87 | + /> |
| 88 | + </div> |
| 89 | + <TextField |
| 90 | + label={s.role} |
| 91 | + value={edited.role} |
| 92 | + onChange={(event) => set("role")(event.target.value)} |
| 93 | + /> |
| 94 | + <TextArea |
| 95 | + label={s.instructions} |
| 96 | + value={edited.instructions} |
| 97 | + rows={5} |
| 98 | + onChange={(event) => set("instructions")(event.target.value)} |
| 99 | + /> |
| 100 | + <p className="text-muted text-xs">{s.startsNow(bot.name)}</p> |
| 101 | + </div> |
| 102 | + <div className="border-line border-t bg-canvas/40 px-4 py-3"> |
| 103 | + <label htmlFor={noteId} className="sr-only"> |
| 104 | + {s.noteLabel(bot.name)} |
| 105 | + </label> |
| 106 | + <textarea |
| 107 | + id={noteId} |
| 108 | + rows={2} |
| 109 | + value={note} |
| 110 | + onChange={(event) => setNote(event.target.value)} |
| 111 | + placeholder={s.notePlaceholder(bot.name)} |
| 112 | + 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" |
| 113 | + /> |
| 114 | + <div className="mt-2.5 flex flex-wrap gap-2"> |
| 115 | + <Button |
| 116 | + variant="primary" |
| 117 | + icon={Check} |
| 118 | + disabled={busy || !edited.name.trim()} |
| 119 | + onClick={() => answer(true, changed ? JSON.stringify(edited) : undefined)} |
| 120 | + > |
| 121 | + {s.create} |
| 122 | + </Button> |
| 123 | + <Button icon={Ban} disabled={busy} onClick={() => answer(false)}> |
| 124 | + {s.decline} |
| 125 | + </Button> |
| 126 | + </div> |
| 127 | + </div> |
| 128 | + </section> |
| 129 | + ); |
| 130 | +} |
| 131 | + |
| 132 | +/** One line once answered; what was suggested stays a click away. */ |
| 133 | +function Answered({ approval }: { approval: ApprovalItem }) { |
| 134 | + const t = useT(); |
| 135 | + const s = t.chat.suggestion; |
| 136 | + const suggestion = suggestionOf(approval.input); |
| 137 | + const name = suggestion.name || s.name; |
| 138 | + const [Icon, text, tone] = |
| 139 | + approval.status === "allowed" |
| 140 | + ? [Check, s.created(name), "text-ok"] |
| 141 | + : approval.status === "denied" |
| 142 | + ? [Ban, s.declined(name), "text-danger"] |
| 143 | + : [TimerOff, s.expired(name), "text-quiet"]; |
| 144 | + return ( |
| 145 | + <details className="group"> |
| 146 | + <summary className="flex min-w-0 cursor-default items-center gap-2 rounded-lg px-1.5 py-1 text-sm hover:bg-sunken"> |
| 147 | + <Icon aria-hidden size={14} className={`shrink-0 ${tone}`} /> |
| 148 | + <span className="shrink-0 font-medium">{text}</span> |
| 149 | + {approval.note && ( |
| 150 | + <span className="truncate text-muted text-xs">{`“${approval.note}”`}</span> |
| 151 | + )} |
| 152 | + <ChevronRight |
| 153 | + aria-hidden |
| 154 | + size={13} |
| 155 | + className="shrink-0 text-muted transition-transform group-open:rotate-90" |
| 156 | + /> |
| 157 | + </summary> |
| 158 | + <dl |
| 159 | + 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" |
| 160 | + data-selectable |
| 161 | + > |
| 162 | + <dt className="text-muted">{s.role}</dt> |
| 163 | + <dd>{suggestion.role}</dd> |
| 164 | + <dt className="text-muted">{s.model}</dt> |
| 165 | + <dd>{t.chat.model.names[suggestion.model ?? "default"]}</dd> |
| 166 | + <dt className="text-muted">{s.instructions}</dt> |
| 167 | + <dd className="whitespace-pre-wrap">{suggestion.instructions}</dd> |
| 168 | + </dl> |
| 169 | + </details> |
| 170 | + ); |
| 171 | +} |
0 commit comments