The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import { |
| 2 | type EnvironmentId, |
| 3 | isSecretReference, |
| 4 | type ProviderInstanceId, |
| 5 | type SecretReference, |
| 6 | type ServerProvider, |
| 7 | TextGeneratorConfig, |
| 8 | type TextGeneratorModel, |
| 9 | } from "@t3tools/contracts"; |
| 10 | import { pickModelSelection } from "@t3tools/shared/model"; |
| 11 | import { AsyncResult } from "effect/reactivity"; |
| 12 | import * as Option from "effect/Option"; |
| 13 | import * as Schema from "effect/Schema"; |
| 14 | import { CheckIcon, MoreVertical, PlusIcon, ServerIcon } from "lucide-react"; |
| 15 | import { useState } from "react"; |
| 16 | import { msg, T, useGT, useMessages, Var } from "gt-react"; |
| 17 | |
| 18 | import { useEnvironmentSettings, useUpdateEnvironmentSettings } from "../../hooks/useSettings"; |
| 19 | import { |
| 20 | checkTextGeneratorConfig, |
| 21 | rereadTextGeneratorModels, |
| 22 | nextTextGeneratorId, |
| 23 | refreshTextGeneratorModels, |
| 24 | testTextGeneratorModel, |
| 25 | useTextGenerationTargets, |
| 26 | useTextGeneratorModels, |
| 27 | TEXT_GENERATOR_PRESETS, |
| 28 | type TextGeneratorPreset, |
| 29 | textGeneratorBrand, |
| 30 | textGeneratorDisplayName, |
| 31 | } from "../../textGenerators"; |
| 32 | import { getTextGenerationModelOptionsByInstance } from "../../modelSelection"; |
| 33 | import { ProviderInstanceIcon } from "../chat/ProviderInstanceIcon"; |
| 34 | import { ProviderModelPicker } from "../chat/ProviderModelPicker"; |
| 35 | import { |
| 36 | AlertDialog, |
| 37 | AlertDialogClose, |
| 38 | AlertDialogDescription, |
| 39 | AlertDialogFooter, |
| 40 | AlertDialogHeader, |
| 41 | AlertDialogPopup, |
| 42 | AlertDialogTitle, |
| 43 | } from "../ui/alert-dialog"; |
| 44 | import { Button } from "../ui/button"; |
| 45 | import { RefreshIcon } from "../ui/refresh-icon"; |
| 46 | import { toastManager } from "../ui/toast"; |
| 47 | import { |
| 48 | Dialog, |
| 49 | DialogDescription, |
| 50 | DialogFooter, |
| 51 | DialogHeader, |
| 52 | DialogPanel, |
| 53 | DialogPopup, |
| 54 | DialogTitle, |
| 55 | } from "../ui/dialog"; |
| 56 | import { Input } from "../ui/input"; |
| 57 | import { Menu, MenuItem, MenuPopup, MenuTrigger } from "../ui/menu"; |
| 58 | import { Label } from "../ui/label"; |
| 59 | import { Spinner } from "../ui/spinner"; |
| 60 | import { Switch } from "../ui/switch"; |
| 61 | import { WizardFooter, WizardHeader, WizardPanel, WizardPopup, WizardSteps } from "../ui/wizard"; |
| 62 | import { SecretSourceInput, useSecretSources } from "./SecretSourceInput"; |
| 63 | import { useSearchableSetting } from "./settingsSearch"; |
| 64 | import { SETTINGS_PICKER_TRIGGER_CLASSNAME, SettingsRow, SettingsSection } from "./settingsLayout"; |
| 65 | |
| 66 | const decodeTextGenerator = Schema.decodeUnknownOption(TextGeneratorConfig); |
| 67 | |
| 68 | type Saved = { readonly id: ProviderInstanceId; readonly config: TextGeneratorConfig }; |
| 69 | type Editing = Saved | "new"; |
| 70 | |
| 71 | /** Rows that list, add, edit, and remove one environment's text generators. */ |
| 72 | export function TextGeneratorsSettings({ |
| 73 | environmentId, |
| 74 | environmentLabel, |
| 75 | providers, |
| 76 | readOnly, |
| 77 | }: { |
| 78 | readonly environmentId: EnvironmentId; |
| 79 | readonly environmentLabel: string; |
| 80 | readonly providers: ReadonlyArray<ServerProvider>; |
| 81 | readonly readOnly: boolean; |
| 82 | }) { |
| 83 | const setting = useSearchableSetting(); |
| 84 | const gt = useGT(); |
| 85 | const messages = useMessages(); |
| 86 | const settings = useEnvironmentSettings(environmentId); |
| 87 | const updateSettings = useUpdateEnvironmentSettings(environmentId); |
| 88 | const [editing, setEditing] = useState<Editing | null>(null); |
| 89 | const [removing, setRemoving] = useState<ProviderInstanceId | null>(null); |
| 90 | const [viewing, setViewing] = useState<Saved | null>(null); |
| 91 | const generators = Object.entries(settings.textGenerators) as Array< |
| 92 | [ProviderInstanceId, TextGeneratorConfig] |
| 93 | >; |
| 94 | const removingConfig = removing === null ? undefined : settings.textGenerators[removing]; |
| 95 | |
| 96 | return ( |
| 97 | <> |
| 98 | <SettingsSection |
| 99 | {...setting("text-generators")} |
| 100 | headerAction={ |
| 101 | !readOnly ? ( |
| 102 | <Button size="xs" variant="outline" onClick={() => setEditing("new")}> |
| 103 | <PlusIcon className="size-3" aria-hidden /> |
| 104 | <T>Add generator</T> |
| 105 | </Button> |
| 106 | ) : null |
| 107 | } |
| 108 | > |
| 109 | {generators.length === 0 ? ( |
| 110 | <SettingsRow |
| 111 | title={gt("No text generators yet")} |
| 112 | description={gt( |
| 113 | "Add a model API to write thread titles, commit messages, and other short text without starting a coding agent.", |
| 114 | )} |
| 115 | /> |
| 116 | ) : null} |
| 117 | {generators.map(([id, config]) => { |
| 118 | const name = textGeneratorDisplayName(config, messages); |
| 119 | return ( |
| 120 | <SettingsRow |
| 121 | key={id} |
| 122 | title={ |
| 123 | <span className="inline-flex items-center gap-2.5"> |
| 124 | <ProviderInstanceIcon |
| 125 | driverKind={textGeneratorBrand(config)} |
| 126 | displayName={name} |
| 127 | badgeContent="none" |
| 128 | /> |
| 129 | {name} |
| 130 | </span> |
| 131 | } |
| 132 | description={ |
| 133 | // Indented past the icon so it lines up with the name. |
| 134 | <span className="block truncate ps-7.5"> |
| 135 | {config.enabled ? "" : gt("Off · ")} |
| 136 | {config.baseUrl.replace(/^https?:\/\//, "")} |
| 137 | </span> |
| 138 | } |
| 139 | control={ |
| 140 | readOnly ? null : ( |
| 141 | <div className="flex items-center gap-2"> |
| 142 | <Menu> |
| 143 | <MenuTrigger |
| 144 | render={ |
| 145 | <Button |
| 146 | size="icon-sm" |
| 147 | variant="ghost-muted" |
| 148 | aria-label={gt("More actions for {name}", { name })} |
| 149 | /> |
| 150 | } |
| 151 | > |
| 152 | <MoreVertical /> |
| 153 | </MenuTrigger> |
| 154 | <MenuPopup align="end"> |
| 155 | <MenuItem onClick={() => setEditing({ id, config })}> |
| 156 | <T>Edit</T> |
| 157 | </MenuItem> |
| 158 | <MenuItem |
| 159 | disabled={!config.enabled} |
| 160 | onClick={() => setViewing({ id, config })} |
| 161 | > |
| 162 | <T>Show models</T> |
| 163 | </MenuItem> |
| 164 | <MenuItem variant="destructive" onClick={() => setRemoving(id)}> |
| 165 | <T>Remove</T> |
| 166 | </MenuItem> |
| 167 | </MenuPopup> |
| 168 | </Menu> |
| 169 | <Switch |
| 170 | checked={config.enabled} |
| 171 | onCheckedChange={(enabled) => |
| 172 | updateSettings({ textGenerators: { [id]: { ...config, enabled } } }) |
| 173 | } |
| 174 | aria-label={gt("Enable {name}", { name })} |
| 175 | /> |
| 176 | </div> |
| 177 | ) |
| 178 | } |
| 179 | /> |
| 180 | ); |
| 181 | })} |
| 182 | </SettingsSection> |
| 183 | {editing !== null ? ( |
| 184 | <TextGeneratorWizard |
| 185 | environmentId={environmentId} |
| 186 | editing={editing} |
| 187 | providers={providers} |
| 188 | onClose={() => setEditing(null)} |
| 189 | onSave={(id, config) => |
| 190 | updateSettings( |
| 191 | { textGenerators: { [id]: config } }, |
| 192 | { onSaved: () => rereadTextGeneratorModels(environmentId, id) }, |
| 193 | ) |
| 194 | } |
| 195 | newId={(preset) => nextTextGeneratorId(preset.id, settings, providers)} |
| 196 | /> |
| 197 | ) : null} |
| 198 | {viewing !== null ? ( |
| 199 | <TextGeneratorModelsDialog |
| 200 | environmentId={environmentId} |
| 201 | saved={viewing} |
| 202 | onClose={() => setViewing(null)} |
| 203 | /> |
| 204 | ) : null} |
| 205 | <AlertDialog |
| 206 | open={removingConfig !== undefined} |
| 207 | onOpenChange={(open) => { |
| 208 | if (!open) setRemoving(null); |
| 209 | }} |
| 210 | > |
| 211 | <AlertDialogPopup> |
| 212 | <AlertDialogHeader> |
| 213 | <AlertDialogTitle> |
| 214 | <T> |
| 215 | Remove{" "} |
| 216 | <Var> |
| 217 | {removingConfig ? textGeneratorDisplayName(removingConfig, messages) : ""} |
| 218 | </Var> |
| 219 | ? |
| 220 | </T> |
| 221 | </AlertDialogTitle> |
| 222 | <AlertDialogDescription> |
| 223 | <T> |
| 224 | This deletes the API key stored on <Var>{environmentLabel}</Var>. Anything set to |
| 225 | use this generator falls back to the text generation model. |
| 226 | </T> |
| 227 | </AlertDialogDescription> |
| 228 | </AlertDialogHeader> |
| 229 | <AlertDialogFooter> |
| 230 | <AlertDialogClose render={<Button variant="outline" />}> |
| 231 | <T>Cancel</T> |
| 232 | </AlertDialogClose> |
| 233 | <Button |
| 234 | variant="destructive" |
| 235 | onClick={() => { |
| 236 | if (removing !== null) updateSettings({ textGenerators: { [removing]: null } }); |
| 237 | setRemoving(null); |
| 238 | }} |
| 239 | > |
| 240 | <T>Remove</T> |
| 241 | </Button> |
| 242 | </AlertDialogFooter> |
| 243 | </AlertDialogPopup> |
| 244 | </AlertDialog> |
| 245 | </> |
| 246 | ); |
| 247 | } |
| 248 | |
| 249 | const STEPS = [msg("Connect", { $context: "wizard step" }), msg("Use it")] as const; |
| 250 | |
| 251 | /** Text tasks a generator can write, in the order the settings pages show them. */ |
| 252 | const TASKS = [ |
| 253 | { key: "textGenerationModelSelection", label: msg("Thread titles and other generated text") }, |
| 254 | { |
| 255 | key: "sourceControlWriterModelSelection", |
| 256 | label: msg("Commit messages, pull requests, and branch names"), |
| 257 | }, |
| 258 | { key: "speechPostProcessingModelSelection", label: msg("Voice transcript cleanup") }, |
| 259 | ] as const; |
| 260 | |
| 261 | /** |
| 262 | * Connect checks the settings by listing the service's models and saves the generator; Use it then |
| 263 | * shows each text task's model picker, so any of them can switch to it. The key goes once to the |
| 264 | * environment's secret storage; settings keep only a redaction marker. |
| 265 | */ |
| 266 | function TextGeneratorWizard({ |
| 267 | environmentId, |
| 268 | editing, |
| 269 | providers, |
| 270 | onClose, |
| 271 | onSave, |
| 272 | newId, |
| 273 | }: { |
| 274 | readonly environmentId: EnvironmentId; |
| 275 | readonly editing: Editing; |
| 276 | readonly providers: ReadonlyArray<ServerProvider>; |
| 277 | readonly onClose: () => void; |
| 278 | readonly onSave: (id: ProviderInstanceId, config: TextGeneratorConfig) => void; |
| 279 | readonly newId: (preset: TextGeneratorPreset) => ProviderInstanceId; |
| 280 | }) { |
| 281 | const messages = useMessages(); |
| 282 | const gt = useGT(); |
| 283 | const existing = editing === "new" ? null : editing.config; |
| 284 | const [step, setStep] = useState(0); |
| 285 | const [preset, setPreset] = useState(TEXT_GENERATOR_PRESETS[0]!); |
| 286 | const [baseUrl, setBaseUrl] = useState(existing?.baseUrl ?? preset.baseUrl); |
| 287 | const [label, setLabel] = useState(existing?.label ?? ""); |
| 288 | const secretSources = useSecretSources(environmentId); |
| 289 | const [keyDraft, setKeyDraft] = useState<string | SecretReference>( |
| 290 | isSecretReference(existing?.apiKey) ? existing.apiKey : "", |
| 291 | ); |
| 292 | const [keyValid, setKeyValid] = useState(true); |
| 293 | const [checking, setChecking] = useState(false); |
| 294 | const [checkError, setCheckError] = useState<string | null>(null); |
| 295 | const [modelCount, setModelCount] = useState(0); |
| 296 | // Fixed once saved, so editing the form again after Back updates the same generator. |
| 297 | const [savedId, setSavedId] = useState<ProviderInstanceId | null>( |
| 298 | editing === "new" ? null : editing.id, |
| 299 | ); |
| 300 | |
| 301 | // A saved literal key arrives redacted; sending the marker back keeps it. |
| 302 | const storedKey = typeof existing?.apiKey === "string" ? existing.apiKey : ""; |
| 303 | const apiKey = typeof keyDraft === "string" ? keyDraft.trim() || storedKey : keyDraft; |
| 304 | const kind = existing?.kind ?? preset.kind; |
| 305 | const config = decodeTextGenerator({ |
| 306 | kind, |
| 307 | ...(label.trim() ? { label: label.trim() } : {}), |
| 308 | baseUrl: baseUrl.trim(), |
| 309 | apiKey, |
| 310 | enabled: existing?.enabled ?? true, |
| 311 | }); |
| 312 | const keyRequired = existing === null && preset.id !== "custom"; |
| 313 | // Presets know their URL; only a custom endpoint, or one already saved as such, asks for it. |
| 314 | const showBaseUrl = |
| 315 | existing === null |
| 316 | ? preset.id === "custom" |
| 317 | : !["openai", "anthropic", "openrouter"].includes(textGeneratorBrand(existing)); |
| 318 | const canConnect = keyValid && Option.isSome(config) && (!keyRequired || apiKey !== ""); |
| 319 | const name = Option.isSome(config) |
| 320 | ? textGeneratorDisplayName(config.value, messages) |
| 321 | : messages(preset.label); |
| 322 | |
| 323 | const connect = async () => { |
| 324 | if (!canConnect || checking) return; |
| 325 | const id = savedId ?? newId(preset); |
| 326 | setChecking(true); |
| 327 | setCheckError(null); |
| 328 | // The form is disabled while this runs, so the checked config is the one saved below. |
| 329 | const result = await checkTextGeneratorConfig(environmentId, id, config.value); |
| 330 | setChecking(false); |
| 331 | if ("error" in result) { |
| 332 | setCheckError(result.error); |
| 333 | return; |
| 334 | } |
| 335 | onSave(id, config.value); |
| 336 | setSavedId(id); |
| 337 | setModelCount(result.models.length); |
| 338 | setStep(1); |
| 339 | }; |
| 340 | |
| 341 | return ( |
| 342 | <Dialog |
| 343 | open |
| 344 | onOpenChange={(open) => { |
| 345 | if (!open) onClose(); |
| 346 | }} |
| 347 | > |
| 348 | <WizardPopup> |
| 349 | <WizardHeader |
| 350 | title={ |
| 351 | existing |
| 352 | ? gt("Edit {name}", { name: textGeneratorDisplayName(existing, messages) }) |
| 353 | : gt("Add a text generator") |
| 354 | } |
| 355 | description={gt( |
| 356 | "Writes thread titles, commit messages, and other short text without starting a coding agent. Your API key is stored on this environment.", |
| 357 | )} |
| 358 | > |
| 359 | <WizardSteps |
| 360 | steps={STEPS.map((step) => messages(step))} |
| 361 | currentStep={step} |
| 362 | isStepDisabled={(index) => index > step} |
| 363 | onStepChange={setStep} |
| 364 | /> |
| 365 | </WizardHeader> |
| 366 | <WizardPanel> |
| 367 | {step === 0 ? ( |
| 368 | <form |
| 369 | className="grid gap-4" |
| 370 | onSubmit={(event) => { |
| 371 | event.preventDefault(); |
| 372 | void connect(); |
| 373 | }} |
| 374 | > |
| 375 | <fieldset disabled={checking} className="contents"> |
| 376 | {existing === null ? ( |
| 377 | <div className="grid gap-1.5"> |
| 378 | <span className="text-sm font-medium"> |
| 379 | <T>Service</T> |
| 380 | </span> |
| 381 | <div |
| 382 | role="radiogroup" |
| 383 | aria-label={gt("Service")} |
| 384 | className="grid grid-cols-2 gap-2 sm:grid-cols-4" |
| 385 | > |
| 386 | {TEXT_GENERATOR_PRESETS.map((candidate) => { |
| 387 | const selected = candidate.id === preset.id; |
| 388 | return ( |
| 389 | <button |
| 390 | key={candidate.id} |
| 391 | type="button" |
| 392 | role="radio" |
| 393 | aria-checked={selected} |
| 394 | className={ |
| 395 | selected |
| 396 | ? "flex flex-col items-center gap-2 rounded-lg border border-primary bg-primary/5 px-2 py-3 text-sm font-medium text-foreground ring-1 ring-primary ring-inset outline-none focus-visible:ring-2 focus-visible:ring-inset" |
| 397 | : "flex flex-col items-center gap-2 rounded-lg border bg-background px-2 py-3 text-sm text-muted-foreground outline-none hover:bg-accent/50 hover:text-foreground focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-inset" |
| 398 | } |
| 399 | onClick={() => { |
| 400 | setPreset(candidate); |
| 401 | setBaseUrl(candidate.baseUrl); |
| 402 | setCheckError(null); |
| 403 | }} |
| 404 | > |
| 405 | {candidate.id === "custom" ? ( |
| 406 | <ServerIcon className="size-5" aria-hidden /> |
| 407 | ) : ( |
| 408 | <ProviderInstanceIcon |
| 409 | driverKind={textGeneratorBrand(candidate)} |
| 410 | displayName={candidate.label} |
| 411 | badgeContent="none" |
| 412 | /> |
| 413 | )} |
| 414 | {candidate.id === "custom" ? gt("Custom") : candidate.label} |
| 415 | </button> |
| 416 | ); |
| 417 | })} |
| 418 | </div> |
| 419 | </div> |
| 420 | ) : null} |
| 421 | {showBaseUrl ? ( |
| 422 | <div className="grid gap-1.5"> |
| 423 | <Label htmlFor="text-generator-url"> |
| 424 | <T>Base URL</T> |
| 425 | </Label> |
| 426 | <Input |
| 427 | id="text-generator-url" |
| 428 | placeholder="http://localhost:11434/v1" |
| 429 | value={baseUrl} |
| 430 | onChange={(event) => { |
| 431 | setBaseUrl(event.target.value); |
| 432 | setCheckError(null); |
| 433 | }} |
| 434 | spellCheck={false} |
| 435 | autoFocus={existing === null && preset.id === "custom"} |
| 436 | /> |
| 437 | </div> |
| 438 | ) : null} |
| 439 | <div className="grid gap-1.5"> |
| 440 | <Label htmlFor="text-generator-key"> |
| 441 | {keyRequired ? gt("API key") : gt("API key (optional)")} |
| 442 | </Label> |
| 443 | <SecretSourceInput |
| 444 | id="text-generator-key" |
| 445 | aria-label={gt("API key")} |
| 446 | value={keyDraft} |
| 447 | configured={storedKey !== ""} |
| 448 | sources={secretSources} |
| 449 | onChange={(event) => { |
| 450 | setKeyDraft(event.target.value); |
| 451 | setCheckError(null); |
| 452 | }} |
| 453 | onSecretChange={(secret) => setKeyDraft(secret ?? "")} |
| 454 | autoFocus={existing === null && preset.id !== "custom"} |
| 455 | onValidityChange={setKeyValid} |
| 456 | /> |
| 457 | </div> |
| 458 | <div className="grid gap-1.5"> |
| 459 | <Label htmlFor="text-generator-label"> |
| 460 | <T>Name (optional)</T> |
| 461 | </Label> |
| 462 | <Input |
| 463 | id="text-generator-label" |
| 464 | placeholder={ |
| 465 | existing !== null |
| 466 | ? textGeneratorDisplayName(existing, messages) |
| 467 | : preset.id === "custom" |
| 468 | ? gt("e.g. My local model") |
| 469 | : messages(preset.label) |
| 470 | } |
| 471 | value={label} |
| 472 | onChange={(event) => setLabel(event.target.value)} |
| 473 | /> |
| 474 | </div> |
| 475 | {checkError ? ( |
| 476 | <p role="alert" className="text-sm text-destructive"> |
| 477 | {checkError} |
| 478 | </p> |
| 479 | ) : null} |
| 480 | </fieldset> |
| 481 | </form> |
| 482 | ) : ( |
| 483 | <div className="grid gap-4"> |
| 484 | <p className="text-sm text-muted-foreground"> |
| 485 | {gt( |
| 486 | "{count, plural, one {{name} is connected and lists {count} model. Switch any of these to it, or keep what they use now.} other {{name} is connected and lists {count} models. Switch any of these to it, or keep what they use now.}}", |
| 487 | { name, count: modelCount }, |
| 488 | )} |
| 489 | </p> |
| 490 | {/* One grid for all rows, so pickers and Test buttons line up in columns. */} |
| 491 | <div className="grid grid-cols-[minmax(0,1fr)_auto_auto] items-center gap-x-2 gap-y-4"> |
| 492 | {TASKS.map((task) => ( |
| 493 | <TextGenerationTaskPicker |
| 494 | key={task.key} |
| 495 | environmentId={environmentId} |
| 496 | providers={providers} |
| 497 | settingKey={task.key} |
| 498 | label={messages(task.label)} |
| 499 | /> |
| 500 | ))} |
| 501 | </div> |
| 502 | </div> |
| 503 | )} |
| 504 | </WizardPanel> |
| 505 | <WizardFooter |
| 506 | leading={ |
| 507 | step > 0 ? ( |
| 508 | <Button variant="ghost" onClick={() => setStep(0)}> |
| 509 | <T>Back</T> |
| 510 | </Button> |
| 511 | ) : null |
| 512 | } |
| 513 | > |
| 514 | {step === 0 ? ( |
| 515 | <> |
| 516 | <Button variant="outline" onClick={onClose}> |
| 517 | <T>Cancel</T> |
| 518 | </Button> |
| 519 | <Button onClick={() => void connect()} disabled={!canConnect || checking}> |
| 520 | {checking ? <Spinner /> : null} |
| 521 | {checking ? gt("Connecting") : existing ? gt("Save and continue") : gt("Connect")} |
| 522 | </Button> |
| 523 | </> |
| 524 | ) : ( |
| 525 | // Connect's button unmounts here; without this, focus lands on the step list. |
| 526 | <Button autoFocus onClick={onClose}> |
| 527 | <T>Done</T> |
| 528 | </Button> |
| 529 | )} |
| 530 | </WizardFooter> |
| 531 | </WizardPopup> |
| 532 | </Dialog> |
| 533 | ); |
| 534 | } |
| 535 | |
| 536 | /** One text task's model, saved as it changes, with every text generation target to pick from. */ |
| 537 | function TextGenerationTaskPicker({ |
| 538 | environmentId, |
| 539 | providers, |
| 540 | settingKey, |
| 541 | label, |
| 542 | }: { |
| 543 | readonly environmentId: EnvironmentId; |
| 544 | readonly providers: ReadonlyArray<ServerProvider>; |
| 545 | readonly settingKey: (typeof TASKS)[number]["key"]; |
| 546 | readonly label: string; |
| 547 | }) { |
| 548 | const settings = useEnvironmentSettings(environmentId); |
| 549 | const updateSettings = useUpdateEnvironmentSettings(environmentId); |
| 550 | const own = settings[settingKey]; |
| 551 | // Source control and voice follow the text generation model until they get their own. |
| 552 | const selection = own ?? settings.textGenerationModelSelection; |
| 553 | const targets = useTextGenerationTargets({ |
| 554 | environmentId, |
| 555 | settings, |
| 556 | providers: providers.filter((provider) => provider.supportsTextGeneration !== false), |
| 557 | projectId: null, |
| 558 | selections: [selection], |
| 559 | }); |
| 560 | // Only a text generator's model can be tried cheaply; agents start a whole session. |
| 561 | const testable = Object.hasOwn(settings.textGenerators, selection.instanceId); |
| 562 | const testKey = `${selection.instanceId}:${selection.model}`; |
| 563 | const [testing, setTesting] = useState(false); |
| 564 | const [result, setResult] = useState<{ |
| 565 | readonly key: string; |
| 566 | readonly error: string | null; |
| 567 | } | null>(null); |
| 568 | const shown = result?.key === testKey ? result : null; |
| 569 | const test = async () => { |
| 570 | setTesting(true); |
| 571 | const error = await testTextGeneratorModel( |
| 572 | environmentId, |
| 573 | selection.instanceId, |
| 574 | selection.model, |
| 575 | ); |
| 576 | setTesting(false); |
| 577 | setResult({ key: testKey, error }); |
| 578 | }; |
| 579 | return ( |
| 580 | <> |
| 581 | <div className="grid min-w-0 gap-0.5 pe-2"> |
| 582 | <span className="text-sm">{label}</span> |
| 583 | {shown?.error ? ( |
| 584 | <span role="alert" className="text-xs text-destructive"> |
| 585 | {shown.error} |
| 586 | </span> |
| 587 | ) : shown ? ( |
| 588 | <span role="status" className="text-xs text-muted-foreground"> |
| 589 | <T> |
| 590 | <Var>{selection.model}</Var> answered. It works. |
| 591 | </T> |
| 592 | </span> |
| 593 | ) : own === null ? ( |
| 594 | <span className="text-xs text-muted-foreground"> |
| 595 | <T>Follows the text generation model</T> |
| 596 | </span> |
| 597 | ) : null} |
| 598 | </div> |
| 599 | <div className="justify-self-end"> |
| 600 | <ProviderModelPicker |
| 601 | environmentId={environmentId} |
| 602 | activeInstanceId={selection.instanceId} |
| 603 | model={selection.model} |
| 604 | lockedProvider={null} |
| 605 | instanceEntries={targets.entries} |
| 606 | modelOptionsByInstance={getTextGenerationModelOptionsByInstance( |
| 607 | settings, |
| 608 | targets.providers, |
| 609 | selection.instanceId, |
| 610 | selection.model, |
| 611 | )} |
| 612 | triggerClassName={SETTINGS_PICKER_TRIGGER_CLASSNAME} |
| 613 | onInstanceModelChange={(instanceId, model) => |
| 614 | updateSettings({ [settingKey]: pickModelSelection(selection, instanceId, model) }) |
| 615 | } |
| 616 | /> |
| 617 | </div> |
| 618 | <div className="justify-self-end"> |
| 619 | {testable ? ( |
| 620 | <Button |
| 621 | size="xs" |
| 622 | variant="outline" |
| 623 | aria-busy={testing} |
| 624 | disabled={testing} |
| 625 | onClick={() => void test()} |
| 626 | > |
| 627 | {/* Keeps the label's width while testing, so the row never reflows. */} |
| 628 | <span className="relative"> |
| 629 | <span className={testing ? "invisible" : undefined}> |
| 630 | <T>Test</T> |
| 631 | </span> |
| 632 | {testing ? ( |
| 633 | <span className="absolute inset-0 flex items-center justify-center"> |
| 634 | <Spinner /> |
| 635 | </span> |
| 636 | ) : null} |
| 637 | </span> |
| 638 | </Button> |
| 639 | ) : null} |
| 640 | </div> |
| 641 | </> |
| 642 | ); |
| 643 | } |
| 644 | |
| 645 | /** Searchable models; selectable when `onSelect` is given. */ |
| 646 | function TextGeneratorModelList({ |
| 647 | models, |
| 648 | selected, |
| 649 | onSelect, |
| 650 | }: { |
| 651 | readonly models: ReadonlyArray<TextGeneratorModel>; |
| 652 | readonly selected?: string | null; |
| 653 | readonly onSelect?: (id: string) => void; |
| 654 | }) { |
| 655 | const gt = useGT(); |
| 656 | const [query, setQuery] = useState(""); |
| 657 | const needle = query.trim().toLowerCase(); |
| 658 | const shown = needle |
| 659 | ? models.filter( |
| 660 | (model) => |
| 661 | model.id.toLowerCase().includes(needle) || model.name.toLowerCase().includes(needle), |
| 662 | ) |
| 663 | : models; |
| 664 | return ( |
| 665 | <div className="grid gap-2"> |
| 666 | {models.length > 8 ? ( |
| 667 | <Input |
| 668 | type="search" |
| 669 | placeholder={gt("Search models")} |
| 670 | aria-label={gt("Search models")} |
| 671 | autoFocus |
| 672 | value={query} |
| 673 | onChange={(event) => setQuery(event.target.value)} |
| 674 | /> |
| 675 | ) : null} |
| 676 | <div className="max-h-80 overflow-y-auto rounded-lg border divide-y divide-border/60"> |
| 677 | {shown.length === 0 ? ( |
| 678 | <p className="px-3 py-2 text-sm text-muted-foreground"> |
| 679 | <T>No models match.</T> |
| 680 | </p> |
| 681 | ) : ( |
| 682 | <ul role={onSelect ? "listbox" : "list"} aria-label={gt("Models")}> |
| 683 | {shown.map((model) => { |
| 684 | const isSelected = model.id === selected; |
| 685 | const label = ( |
| 686 | <> |
| 687 | <span className="grid min-w-0 flex-1"> |
| 688 | <span className="truncate">{model.name}</span> |
| 689 | {model.name !== model.id ? ( |
| 690 | <span className="truncate font-mono text-xs text-muted-foreground"> |
| 691 | {model.id} |
| 692 | </span> |
| 693 | ) : null} |
| 694 | </span> |
| 695 | {isSelected ? <CheckIcon className="size-4 shrink-0 text-primary" /> : null} |
| 696 | </> |
| 697 | ); |
| 698 | return ( |
| 699 | <li |
| 700 | key={model.id} |
| 701 | role={onSelect ? "option" : undefined} |
| 702 | aria-selected={onSelect ? isSelected : undefined} |
| 703 | > |
| 704 | {onSelect ? ( |
| 705 | <button |
| 706 | type="button" |
| 707 | className="flex w-full items-center gap-2 px-3 py-2 text-left text-sm hover:bg-accent focus-visible:bg-accent focus-visible:outline-none" |
| 708 | onClick={() => onSelect(model.id)} |
| 709 | > |
| 710 | {label} |
| 711 | </button> |
| 712 | ) : ( |
| 713 | <div className="flex items-center gap-2 px-3 py-2 text-sm">{label}</div> |
| 714 | )} |
| 715 | </li> |
| 716 | ); |
| 717 | })} |
| 718 | </ul> |
| 719 | )} |
| 720 | </div> |
| 721 | </div> |
| 722 | ); |
| 723 | } |
| 724 | |
| 725 | /** A saved generator's models, with a refresh that asks the API again. */ |
| 726 | function TextGeneratorModelsDialog({ |
| 727 | environmentId, |
| 728 | saved, |
| 729 | onClose, |
| 730 | }: { |
| 731 | readonly environmentId: EnvironmentId; |
| 732 | readonly saved: Saved; |
| 733 | readonly onClose: () => void; |
| 734 | }) { |
| 735 | const gt = useGT(); |
| 736 | const messages = useMessages(); |
| 737 | const name = textGeneratorDisplayName(saved.config, messages); |
| 738 | const result = useTextGeneratorModels(environmentId, saved.id, saved.config); |
| 739 | const [refreshing, setRefreshing] = useState(false); |
| 740 | const refresh = async () => { |
| 741 | setRefreshing(true); |
| 742 | const outcome = await refreshTextGeneratorModels(environmentId, saved.id); |
| 743 | setRefreshing(false); |
| 744 | if ("error" in outcome) { |
| 745 | toastManager.add({ |
| 746 | type: "error", |
| 747 | title: gt("Couldn't load {name} models", { name }), |
| 748 | description: outcome.error, |
| 749 | }); |
| 750 | } |
| 751 | }; |
| 752 | const models = AsyncResult.isSuccess(result) ? result.value : null; |
| 753 | const error = Option.getOrUndefined(AsyncResult.error(result)); |
| 754 | return ( |
| 755 | <Dialog |
| 756 | open |
| 757 | onOpenChange={(open) => { |
| 758 | if (!open) onClose(); |
| 759 | }} |
| 760 | > |
| 761 | <DialogPopup className="max-w-md"> |
| 762 | <DialogHeader> |
| 763 | <DialogTitle>{gt("{name} models", { name })}</DialogTitle> |
| 764 | <DialogDescription> |
| 765 | {models === null |
| 766 | ? error |
| 767 | ? gt("The model list couldn't load.") |
| 768 | : gt("Loading the model list.") |
| 769 | : gt( |
| 770 | "{count, plural, one {{count} model available. Pick one in a text generation model picker.} other {{count} models available. Pick one in a text generation model picker.}}", |
| 771 | { count: models.length }, |
| 772 | )} |
| 773 | </DialogDescription> |
| 774 | </DialogHeader> |
| 775 | <DialogPanel> |
| 776 | {error ? ( |
| 777 | <p role="alert" className="text-sm text-destructive"> |
| 778 | {error} |
| 779 | </p> |
| 780 | ) : models === null ? ( |
| 781 | <Spinner tone="muted" /> |
| 782 | ) : ( |
| 783 | <TextGeneratorModelList models={models} /> |
| 784 | )} |
| 785 | </DialogPanel> |
| 786 | <DialogFooter variant="bare"> |
| 787 | <Button |
| 788 | variant="outline" |
| 789 | aria-busy={refreshing} |
| 790 | disabled={refreshing} |
| 791 | onClick={() => void refresh()} |
| 792 | > |
| 793 | <RefreshIcon refreshing={refreshing} /> |
| 794 | <T>Refresh</T> |
| 795 | </Button> |
| 796 | <Button onClick={onClose}> |
| 797 | <T>Done</T> |
| 798 | </Button> |
| 799 | </DialogFooter> |
| 800 | </DialogPopup> |
| 801 | </Dialog> |
| 802 | ); |
| 803 | } |