|
| 1 | +// The model of a bot that does not run on Claude Code (spec 30), picked below |
| 2 | +// the chat like Claude's. The agent lists its models; its effort is part of |
| 3 | +// each name ("... (High)"), so there is no second picker. |
| 4 | + |
| 5 | +import { ChevronDown } from "lucide-react"; |
| 6 | +import { useCallback, useEffect, useRef, useState } from "react"; |
| 7 | +import { useT } from "../../i18n"; |
| 8 | +import type { AgentModel, Bot } from "../../lib/protocol.gen"; |
| 9 | +import { useApi, useApp } from "../../store/context"; |
| 10 | +import { POPOVER } from "../../ui/surface"; |
| 11 | +import { attempt } from "../../ui/toast"; |
| 12 | +import { useDismiss } from "../../ui/useDismiss"; |
| 13 | +import { PickerOption } from "./PickerOption"; |
| 14 | + |
| 15 | +export function AgentModelPicker({ bot, onLater }: { bot: Bot; onLater(text: string): void }) { |
| 16 | + const api = useApi(); |
| 17 | + const putBot = useApp((state) => state.putBot); |
| 18 | + const m = useT().chat.agentModel; |
| 19 | + const [open, setOpen] = useState(false); |
| 20 | + const [models, setModels] = useState<AgentModel[] | null>(null); |
| 21 | + const [failed, setFailed] = useState(false); |
| 22 | + const root = useRef<HTMLDivElement>(null); |
| 23 | + const close = useCallback(() => setOpen(false), []); |
| 24 | + useDismiss(open, root, close); |
| 25 | + |
| 26 | + // Asked when the list is opened first: it takes a few seconds. |
| 27 | + useEffect(() => { |
| 28 | + if (!open || models !== null) { |
| 29 | + return; |
| 30 | + } |
| 31 | + let live = true; |
| 32 | + api |
| 33 | + .call("agents.models", { agent: bot.agent }) |
| 34 | + .then((list) => live && setModels(list)) |
| 35 | + .catch(() => live && setFailed(true)); |
| 36 | + return () => { |
| 37 | + live = false; |
| 38 | + }; |
| 39 | + }, [open, models, api, bot.agent]); |
| 40 | + |
| 41 | + const current = bot.agentModel; |
| 42 | + const nameOf = (id: string) => models?.find((model) => model.id === id)?.name ?? id; |
| 43 | + const label = |
| 44 | + current === null ? (bot.modelInUse ? nameOf(bot.modelInUse) : m.short) : nameOf(current); |
| 45 | + |
| 46 | + const choose = async (model: string | null) => { |
| 47 | + setOpen(false); |
| 48 | + if (model === current) { |
| 49 | + return; |
| 50 | + } |
| 51 | + const working = bot.state === "busy" || bot.state === "needs_approval"; |
| 52 | + await attempt(m.failed, async () => { |
| 53 | + putBot(await api.call("bots.setAgentModel", { botId: bot.id, model })); |
| 54 | + if (working) { |
| 55 | + onLater(m.later(bot.name, model === null ? m.short : nameOf(model))); |
| 56 | + } |
| 57 | + }); |
| 58 | + }; |
| 59 | + |
| 60 | + return ( |
| 61 | + <div ref={root} className="relative min-w-0"> |
| 62 | + {open && ( |
| 63 | + <div |
| 64 | + role="menu" |
| 65 | + aria-label={m.title} |
| 66 | + className={`absolute right-0 bottom-full mb-2 max-h-80 w-[22rem] max-w-[80vw] origin-bottom-right overflow-y-auto ${POPOVER}`} |
| 67 | + > |
| 68 | + <p className="px-2.5 pt-1.5 pb-1 font-medium text-muted text-xs">{m.title}</p> |
| 69 | + <PickerOption |
| 70 | + name={m.short} |
| 71 | + hint={m.defaultHint(bot.name)} |
| 72 | + checked={current === null} |
| 73 | + onSelect={() => choose(null)} |
| 74 | + /> |
| 75 | + {models === null && !failed && ( |
| 76 | + <p className="px-2.5 py-2 text-muted text-xs">{m.loading}</p> |
| 77 | + )} |
| 78 | + {failed && <p className="px-2.5 py-2 text-danger text-xs">{m.listFailed}</p>} |
| 79 | + {models?.map((model) => ( |
| 80 | + <PickerOption |
| 81 | + key={model.id} |
| 82 | + name={model.name} |
| 83 | + hint={model.id} |
| 84 | + checked={current === model.id} |
| 85 | + onSelect={() => choose(model.id)} |
| 86 | + /> |
| 87 | + ))} |
| 88 | + </div> |
| 89 | + )} |
| 90 | + <button |
| 91 | + type="button" |
| 92 | + aria-haspopup="menu" |
| 93 | + aria-expanded={open} |
| 94 | + aria-label={m.button(label)} |
| 95 | + onClick={() => setOpen(!open)} |
| 96 | + className={`flex h-8 max-w-full items-center gap-1 rounded-full px-2.5 text-sm text-ink-soft transition-colors hover:bg-sunken hover:text-ink ${ |
| 97 | + open ? "bg-sunken text-ink" : "" |
| 98 | + }`} |
| 99 | + > |
| 100 | + <span className="truncate">{label}</span> |
| 101 | + <ChevronDown |
| 102 | + aria-hidden |
| 103 | + size={14} |
| 104 | + className={`hidden shrink-0 opacity-60 transition-transform duration-200 @xs:block ${open ? "rotate-180" : ""}`} |
| 105 | + /> |
| 106 | + </button> |
| 107 | + </div> |
| 108 | + ); |
| 109 | +} |
0 commit comments