The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { |
| 2 | buildTraitsTriggerDisplay, |
| 3 | getDescriptorStringValue, |
| 4 | isReasoningControl, |
| 5 | selectableProviderOptionChoices, |
| 6 | } from "@t3tools/client-runtime/model-traits"; |
| 7 | import type { ProviderOptionDescriptor, RuntimeMode } from "@t3tools/contracts"; |
| 8 | import type { EffortDisplay } from "@t3tools/contracts/settings"; |
| 9 | import { getProviderOptionCurrentLabel } from "@t3tools/shared/model"; |
| 10 | import { msg, T, useGT, useMessages } from "gt-react"; |
| 11 | import { CheckIcon, ChevronRightIcon, ChevronUpIcon, WorkflowIcon } from "lucide-react"; |
| 12 | import { type KeyboardEvent, type ReactNode, use, useRef, useState } from "react"; |
| 13 | |
| 14 | import { m, type MessageResolver } from "~/i18n/messages"; |
| 15 | import { cn } from "~/lib/utils"; |
| 16 | import { |
| 17 | Menu, |
| 18 | MenuPopup, |
| 19 | MenuRadioGroup, |
| 20 | MenuRadioItem, |
| 21 | MenuRadioItemIndicator, |
| 22 | MenuTrigger, |
| 23 | } from "../ui/menu"; |
| 24 | import { Switch } from "../ui/switch"; |
| 25 | import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip"; |
| 26 | import type { ComposerControlSize } from "./ComposerControl"; |
| 27 | import { ComposerPermissionsSection, type RuntimeModeOption } from "./ComposerFooterControls"; |
| 28 | import { composerFloatingLayerProps, focusCheckedPermission } from "./composerEventScope"; |
| 29 | import { EffortGlyph, FastBolt, resolveEffortGlyph } from "./EffortGlyph"; |
| 30 | import { DockedPickerContext } from "./ProviderModelPicker"; |
| 31 | import { |
| 32 | getTraitsSectionVisibility, |
| 33 | type TraitsSelectionInput, |
| 34 | ULTRATHINK_IN_BODY_NOTE, |
| 35 | useTraitsSelection, |
| 36 | } from "./TraitsPicker"; |
| 37 | |
| 38 | type SelectDescriptor = Extract<ProviderOptionDescriptor, { type: "select" }>; |
| 39 | type BooleanDescriptor = Extract<ProviderOptionDescriptor, { type: "boolean" }>; |
| 40 | |
| 41 | /** Service tiers, Claude's Fast mode, and Ultracode sit in the footer, not the column. */ |
| 42 | function splitSpeed( |
| 43 | selects: ReadonlyArray<SelectDescriptor>, |
| 44 | toggles: ReadonlyArray<BooleanDescriptor>, |
| 45 | ) { |
| 46 | const speedSelect = selects.find((descriptor) => descriptor.id === "serviceTier") ?? null; |
| 47 | const fastToggle = toggles.find((descriptor) => descriptor.id === "fastMode") ?? null; |
| 48 | const ultracodeToggle = toggles.find((descriptor) => descriptor.id === "ultracode") ?? null; |
| 49 | return { |
| 50 | speedSelect, |
| 51 | fastToggle, |
| 52 | ultracodeToggle, |
| 53 | columnSelects: selects.filter((descriptor) => descriptor !== speedSelect), |
| 54 | columnToggles: toggles.filter( |
| 55 | (descriptor) => descriptor !== fastToggle && descriptor !== ultracodeToggle, |
| 56 | ), |
| 57 | }; |
| 58 | } |
| 59 | |
| 60 | type ColumnControl = |
| 61 | | { kind: "select"; descriptor: SelectDescriptor } |
| 62 | | { kind: "boolean"; descriptor: BooleanDescriptor }; |
| 63 | |
| 64 | /** |
| 65 | * The column lists the model's reasoning levels under the header that names it. Other options, |
| 66 | * such as an agent's session mode, sit below as one row each. |
| 67 | */ |
| 68 | function columnControls( |
| 69 | selects: ReadonlyArray<SelectDescriptor>, |
| 70 | toggles: ReadonlyArray<BooleanDescriptor>, |
| 71 | ) { |
| 72 | const { columnSelects, columnToggles } = splitSpeed(selects, toggles); |
| 73 | const controls: ReadonlyArray<ColumnControl> = [ |
| 74 | ...columnSelects.map((descriptor) => ({ kind: "select" as const, descriptor })), |
| 75 | ...columnToggles.map((descriptor) => ({ kind: "boolean" as const, descriptor })), |
| 76 | ]; |
| 77 | const effort = controls.find((control) => isReasoningControl(control.descriptor)) ?? null; |
| 78 | return { effort, others: controls.filter((control) => control !== effort) }; |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * What the model trigger shows after the model name: the effort word (or bars, per the |
| 83 | * Appearance setting), a bolt for Fast, a workflow icon for Ultracode, and other traits such as a |
| 84 | * context window. |
| 85 | */ |
| 86 | export function describeComposerEffort( |
| 87 | traits: TraitsSelectionInput | null, |
| 88 | effortDisplay: EffortDisplay, |
| 89 | size: ComposerControlSize, |
| 90 | resolve: MessageResolver = m, |
| 91 | ): { suffix: ReactNode; detail: string | null } { |
| 92 | if (!traits) return { suffix: null, detail: null }; |
| 93 | const visibility = getTraitsSectionVisibility({ ...traits, resolve }); |
| 94 | if (!visibility.hasAnyControls) return { suffix: null, detail: null }; |
| 95 | const modelSelection = |
| 96 | traits.instanceId && traits.model |
| 97 | ? { instanceId: traits.instanceId, model: traits.model, options: traits.modelOptions ?? [] } |
| 98 | : null; |
| 99 | const primary = visibility.primarySelectDescriptor; |
| 100 | const display = buildTraitsTriggerDisplay( |
| 101 | { |
| 102 | descriptors: visibility.descriptors, |
| 103 | primarySelectDescriptorId: primary?.id ?? null, |
| 104 | ultrathinkPromptControlled: visibility.ultrathinkPromptControlled, |
| 105 | modelSelection, |
| 106 | reportedModelSelection: traits.reportedModelSelection, |
| 107 | }, |
| 108 | resolve, |
| 109 | ); |
| 110 | // The same reasoning control the column lists, wherever the agent put it among its options. |
| 111 | const reasoning = |
| 112 | columnControls(visibility.selectDescriptors, visibility.booleanDescriptors).effort |
| 113 | ?.descriptor ?? null; |
| 114 | const ultrathink = visibility.ultrathinkPromptControlled && reasoning === primary; |
| 115 | const glyph = |
| 116 | reasoning?.type === "select" |
| 117 | ? resolveEffortGlyph( |
| 118 | reasoning, |
| 119 | ultrathink |
| 120 | ? "ultrathink" |
| 121 | : getDescriptorStringValue(reasoning, modelSelection, traits.reportedModelSelection), |
| 122 | ) |
| 123 | : null; |
| 124 | // Workflow levels such as Ultracode always show their name; the setting names every level. |
| 125 | const word = glyph |
| 126 | ? (glyph.special ?? (effortDisplay === "label" ? glyph.label : null)) |
| 127 | : !reasoning |
| 128 | ? null |
| 129 | : reasoning.type === "boolean" |
| 130 | ? reasoning.currentValue === true |
| 131 | ? resolve(msg("{option} On", { option: reasoning.label })) |
| 132 | : resolve(msg("{option} Off", { option: reasoning.label })) |
| 133 | : ultrathink |
| 134 | ? "Ultrathink" |
| 135 | : (getProviderOptionCurrentLabel( |
| 136 | reasoning, |
| 137 | modelSelection, |
| 138 | traits.reportedModelSelection, |
| 139 | ) ?? (reasoning.id === "thinking" ? reasoning.label : null)); |
| 140 | const fast = display.speedLabel === "Fast" || display.speedLabel === "Ultrafast"; |
| 141 | // Agent options such as modes live in the picker; the trigger adds only the context window. |
| 142 | const contextWindow = |
| 143 | visibility.contextWindowDescriptor && |
| 144 | getProviderOptionCurrentLabel( |
| 145 | visibility.contextWindowDescriptor, |
| 146 | modelSelection, |
| 147 | traits.reportedModelSelection, |
| 148 | ); |
| 149 | if (!word && !glyph && !fast && !display.ultracode && !contextWindow) |
| 150 | return { suffix: null, detail: display.label }; |
| 151 | return { |
| 152 | detail: display.label, |
| 153 | suffix: ( |
| 154 | <span |
| 155 | className={cn( |
| 156 | "flex shrink-0 items-center gap-1.5 text-muted-foreground group-hover/model-trigger:text-foreground group-data-pressed/model-trigger:text-foreground group-data-popup-open/model-trigger:text-foreground", |
| 157 | // A word space after the model name: the regular trigger's gap is wider than one. |
| 158 | size === "sm" && "-ms-0.5", |
| 159 | )} |
| 160 | > |
| 161 | {word ? ( |
| 162 | <span data-composer-control-label className="max-w-24 truncate"> |
| 163 | {word} |
| 164 | </span> |
| 165 | ) : glyph ? ( |
| 166 | <EffortGlyph count={glyph.count} filled={glyph.filled} size={size} /> |
| 167 | ) : null} |
| 168 | {fast ? <FastBolt ultra={display.speedLabel === "Ultrafast"} /> : null} |
| 169 | {display.ultracode ? ( |
| 170 | <WorkflowIcon aria-hidden="true" data-composer-control-icon className="size-3" /> |
| 171 | ) : null} |
| 172 | {contextWindow ? ( |
| 173 | <span |
| 174 | // A label, so icon-only footers drop it; compacted rows drop it before the model. |
| 175 | data-composer-control-label |
| 176 | data-composer-secondary-trait |
| 177 | className="max-w-24 truncate text-xs" |
| 178 | > |
| 179 | {contextWindow} |
| 180 | </span> |
| 181 | ) : null} |
| 182 | </span> |
| 183 | ), |
| 184 | }; |
| 185 | } |
| 186 | |
| 187 | const COLUMN_ROW = |
| 188 | "flex h-8 w-full shrink-0 cursor-pointer items-center gap-2 rounded-sm px-2 text-left text-sm text-foreground outline-none hover:bg-foreground/[0.09] focus-visible:bg-foreground/[0.09] disabled:cursor-not-allowed disabled:opacity-64"; |
| 189 | const COLUMN_LABEL = "px-2 pt-1.5 pb-1 font-medium text-muted-foreground text-xs"; |
| 190 | |
| 191 | function ColumnRow(props: { |
| 192 | label: string; |
| 193 | /** Shown on hover; the column is too narrow to print it under the label. */ |
| 194 | description?: string | undefined; |
| 195 | selected: boolean; |
| 196 | /** The column's one tab stop: the selected level, else the first when none shows as selected. */ |
| 197 | tabStop: boolean; |
| 198 | isDefault?: boolean | undefined; |
| 199 | disabled?: boolean; |
| 200 | onPick: () => void; |
| 201 | }) { |
| 202 | const gt = useGT(); |
| 203 | const rowProps = { |
| 204 | type: "button", |
| 205 | role: "radio", |
| 206 | "aria-checked": props.selected, |
| 207 | disabled: props.disabled, |
| 208 | "data-effort-row": true, |
| 209 | // Focus lands on a level after a model pick. |
| 210 | "data-effort-level": true, |
| 211 | // Up and Down move between rows. |
| 212 | tabIndex: props.tabStop ? 0 : -1, |
| 213 | className: COLUMN_ROW, |
| 214 | onClick: props.onPick, |
| 215 | } as const; |
| 216 | const content = ( |
| 217 | <> |
| 218 | <span className="min-w-0 flex-1 truncate">{props.label}</span> |
| 219 | {props.selected ? ( |
| 220 | <> |
| 221 | {props.isDefault ? ( |
| 222 | <span className="sr-only"> |
| 223 | {gt("Default", { $context: "marks the default option" })} |
| 224 | </span> |
| 225 | ) : null} |
| 226 | <CheckIcon aria-hidden="true" className="size-3.5" /> |
| 227 | </> |
| 228 | ) : props.isDefault ? ( |
| 229 | <span className="text-muted-foreground/70 text-xs"> |
| 230 | {gt("Default", { $context: "marks the default option" })} |
| 231 | </span> |
| 232 | ) : null} |
| 233 | </> |
| 234 | ); |
| 235 | if (!props.description) return <button {...rowProps}>{content}</button>; |
| 236 | return ( |
| 237 | <Tooltip> |
| 238 | <TooltipTrigger render={<button {...rowProps} />}>{content}</TooltipTrigger> |
| 239 | <TooltipPopup side="right" className="max-w-56"> |
| 240 | {props.description} |
| 241 | </TooltipPopup> |
| 242 | </Tooltip> |
| 243 | ); |
| 244 | } |
| 245 | |
| 246 | function moveBetweenRows(event: KeyboardEvent<HTMLElement>) { |
| 247 | if (event.key !== "ArrowDown" && event.key !== "ArrowUp") return; |
| 248 | // An option's side menu is a React child here, but it moves through its own items. |
| 249 | if (!event.currentTarget.contains(event.target as Node)) return; |
| 250 | const rows = Array.from( |
| 251 | event.currentTarget.querySelectorAll<HTMLButtonElement>("[data-effort-row]:not(:disabled)"), |
| 252 | ); |
| 253 | const index = rows.findIndex((row) => row === document.activeElement); |
| 254 | const next = |
| 255 | rows[Math.min(rows.length - 1, Math.max(0, index + (event.key === "ArrowDown" ? 1 : -1)))]; |
| 256 | if (!next) return; |
| 257 | event.preventDefault(); |
| 258 | event.stopPropagation(); |
| 259 | next.focus(); |
| 260 | next.scrollIntoView({ block: "nearest" }); |
| 261 | } |
| 262 | |
| 263 | function EffortLevels(props: { traits: TraitsSelectionInput }) { |
| 264 | const traits = useTraitsSelection(props.traits); |
| 265 | const { effort, others } = columnControls(traits.selectDescriptors, traits.booleanDescriptors); |
| 266 | if (traits.modelIsUnavailable) { |
| 267 | return ( |
| 268 | <> |
| 269 | {traits.descriptors.map((descriptor) => { |
| 270 | const value = traits.currentLabel(descriptor); |
| 271 | return value ? ( |
| 272 | <div key={descriptor.id}> |
| 273 | <div className={COLUMN_LABEL}>{descriptor.label}</div> |
| 274 | <div className="px-2 pb-1.5 text-muted-foreground/80 text-xs">{value}</div> |
| 275 | </div> |
| 276 | ) : null; |
| 277 | })} |
| 278 | </> |
| 279 | ); |
| 280 | } |
| 281 | if (!effort && others.length === 0) return <NoEffortRow />; |
| 282 | // Ultrathink in the prompt locks the levels; the first option then holds the tab stop. |
| 283 | const locked = |
| 284 | effort?.descriptor.id === traits.primarySelectDescriptor?.id && traits.ultrathinkInBodyText; |
| 285 | return ( |
| 286 | <> |
| 287 | {effort ? <EffortRows control={effort} traits={traits} locked={locked} /> : null} |
| 288 | {others.length > 0 ? ( |
| 289 | <div |
| 290 | className={cn("flex flex-col gap-0.5", effort && "mt-1 border-t border-border/70 pt-1")} |
| 291 | > |
| 292 | {others.map((control, index) => |
| 293 | control.kind === "boolean" ? ( |
| 294 | <OptionSwitchRow |
| 295 | key={control.descriptor.id} |
| 296 | descriptor={control.descriptor} |
| 297 | traits={traits} |
| 298 | tabStop={(!effort || locked) && index === 0} |
| 299 | /> |
| 300 | ) : ( |
| 301 | <OptionMenuRow |
| 302 | key={control.descriptor.id} |
| 303 | descriptor={control.descriptor} |
| 304 | traits={traits} |
| 305 | tabStop={(!effort || locked) && index === 0} |
| 306 | /> |
| 307 | ), |
| 308 | )} |
| 309 | </div> |
| 310 | ) : null} |
| 311 | </> |
| 312 | ); |
| 313 | } |
| 314 | |
| 315 | type TraitsSelection = ReturnType<typeof useTraitsSelection>; |
| 316 | |
| 317 | function EffortRows(props: { control: ColumnControl; traits: TraitsSelection; locked: boolean }) { |
| 318 | const gt = useGT(); |
| 319 | const messages = useMessages(); |
| 320 | const { control, traits } = props; |
| 321 | const { descriptor } = control; |
| 322 | if (control.kind === "boolean") { |
| 323 | return ( |
| 324 | <div role="radiogroup" aria-label={descriptor.label} className="flex flex-col gap-0.5"> |
| 325 | {([true, false] as const).map((value) => ( |
| 326 | <ColumnRow |
| 327 | key={String(value)} |
| 328 | label={ |
| 329 | value |
| 330 | ? gt("On", { $context: "option state" }) |
| 331 | : gt("Off", { $context: "option state" }) |
| 332 | } |
| 333 | selected={(control.descriptor.currentValue === true) === value} |
| 334 | tabStop={(control.descriptor.currentValue === true) === value} |
| 335 | onPick={() => traits.changeBoolean(descriptor.id, value)} |
| 336 | /> |
| 337 | ))} |
| 338 | </div> |
| 339 | ); |
| 340 | } |
| 341 | const { locked } = props; |
| 342 | const value = traits.selectValue(control.descriptor); |
| 343 | const choices = selectableProviderOptionChoices(control.descriptor); |
| 344 | // An "Ultrathink:" prefix selects a level the column does not offer. |
| 345 | const shown = choices.some((option) => option.id === value); |
| 346 | return ( |
| 347 | <div role="radiogroup" aria-label={descriptor.label} className="flex flex-col gap-0.5"> |
| 348 | {locked ? ( |
| 349 | <div className="px-2 pb-1.5 text-muted-foreground/80 text-xs"> |
| 350 | {messages(ULTRATHINK_IN_BODY_NOTE)} |
| 351 | </div> |
| 352 | ) : null} |
| 353 | {choices.map((option, index) => ( |
| 354 | <ColumnRow |
| 355 | key={option.id} |
| 356 | label={option.label} |
| 357 | selected={option.id === value} |
| 358 | tabStop={shown ? option.id === value : index === 0} |
| 359 | description={option.description} |
| 360 | isDefault={option.isDefault} |
| 361 | disabled={locked} |
| 362 | onPick={() => traits.changeSelect(control.descriptor, option.id)} |
| 363 | /> |
| 364 | ))} |
| 365 | </div> |
| 366 | ); |
| 367 | } |
| 368 | |
| 369 | const OPTION_ROW = cn(COLUMN_ROW, "h-auto py-1"); |
| 370 | |
| 371 | /** An option's name over its current value. */ |
| 372 | function OptionRowText(props: { label: string; value: string }) { |
| 373 | return ( |
| 374 | <span className="flex min-w-0 flex-1 flex-col"> |
| 375 | <span className="truncate text-muted-foreground text-xs">{props.label}</span> |
| 376 | <span className="truncate">{props.value}</span> |
| 377 | </span> |
| 378 | ); |
| 379 | } |
| 380 | |
| 381 | /** |
| 382 | * One select besides reasoning, such as an agent's session mode. It names the current choice and |
| 383 | * opens the rest beside the column, so any number of options leaves the levels in place. |
| 384 | */ |
| 385 | function OptionMenuRow(props: { |
| 386 | descriptor: SelectDescriptor; |
| 387 | traits: TraitsSelection; |
| 388 | /** Holds the column's tab stop when the model has no levels. */ |
| 389 | tabStop: boolean; |
| 390 | }) { |
| 391 | const gt = useGT(); |
| 392 | const { descriptor, traits } = props; |
| 393 | const [open, setOpen] = useState(false); |
| 394 | const popupRef = useRef<HTMLDivElement>(null); |
| 395 | const choices = selectableProviderOptionChoices(descriptor); |
| 396 | const value = traits.selectValue(descriptor); |
| 397 | const current = choices.find((choice) => choice.id === value)?.label ?? ""; |
| 398 | return ( |
| 399 | <Menu |
| 400 | open={open} |
| 401 | // A modal menu would cover the picker, so the next click there would only close it. |
| 402 | modal={false} |
| 403 | onOpenChange={setOpen} |
| 404 | onOpenChangeComplete={(isOpen) => { |
| 405 | if (!isOpen) return; |
| 406 | const checked = popupRef.current?.querySelector<HTMLElement>( |
| 407 | '[role="menuitemradio"][aria-checked="true"]', |
| 408 | ); |
| 409 | checked?.focus({ preventScroll: true }); |
| 410 | checked?.scrollIntoView({ block: "nearest" }); |
| 411 | }} |
| 412 | > |
| 413 | {/* No hover opening: an opened menu takes focus, which would steal typing from search. */} |
| 414 | <MenuTrigger |
| 415 | render={ |
| 416 | <button |
| 417 | type="button" |
| 418 | data-effort-row |
| 419 | tabIndex={props.tabStop ? 0 : -1} |
| 420 | aria-label={`${descriptor.label}: ${current}`} |
| 421 | className={cn(OPTION_ROW, "data-popup-open:bg-foreground/[0.09]")} |
| 422 | onKeyDown={(event) => { |
| 423 | if (event.key !== "ArrowRight") return; |
| 424 | event.preventDefault(); |
| 425 | event.stopPropagation(); |
| 426 | setOpen(true); |
| 427 | }} |
| 428 | /> |
| 429 | } |
| 430 | > |
| 431 | <OptionRowText label={descriptor.label} value={current} /> |
| 432 | <ChevronRightIcon |
| 433 | aria-hidden="true" |
| 434 | className="-me-1 size-3.5 shrink-0 text-muted-foreground" |
| 435 | /> |
| 436 | </MenuTrigger> |
| 437 | <MenuPopup |
| 438 | ref={popupRef} |
| 439 | side="right" |
| 440 | align="start" |
| 441 | sideOffset={6} |
| 442 | // A full-width column on phones leaves no room beside it, so fall below or above. |
| 443 | collisionAvoidance={{ fallbackAxisSide: "end" }} |
| 444 | className="max-w-72" |
| 445 | data-model-picker-option-menu |
| 446 | {...composerFloatingLayerProps} |
| 447 | onKeyDown={(event) => { |
| 448 | if (event.key !== "ArrowLeft") return; |
| 449 | event.preventDefault(); |
| 450 | event.stopPropagation(); |
| 451 | setOpen(false); |
| 452 | }} |
| 453 | > |
| 454 | <MenuRadioGroup |
| 455 | aria-label={descriptor.label} |
| 456 | value={value} |
| 457 | onValueChange={(next: string) => { |
| 458 | if (next !== value) traits.changeSelect(descriptor, next); |
| 459 | }} |
| 460 | > |
| 461 | {choices.map((choice) => ( |
| 462 | <MenuRadioItem key={choice.id} value={choice.id} closeOnClick> |
| 463 | <span className="flex items-center gap-3"> |
| 464 | <span className="flex min-w-0 flex-1 flex-col"> |
| 465 | <span className="truncate">{choice.label}</span> |
| 466 | {choice.description ? ( |
| 467 | <span className="line-clamp-2 text-muted-foreground text-xs"> |
| 468 | {choice.description} |
| 469 | </span> |
| 470 | ) : null} |
| 471 | </span> |
| 472 | {choice.isDefault ? ( |
| 473 | <span className="text-muted-foreground/70 text-xs"> |
| 474 | {gt("Default", { $context: "marks the default option" })} |
| 475 | </span> |
| 476 | ) : null} |
| 477 | <MenuRadioItemIndicator /> |
| 478 | </span> |
| 479 | </MenuRadioItem> |
| 480 | ))} |
| 481 | </MenuRadioGroup> |
| 482 | </MenuPopup> |
| 483 | </Menu> |
| 484 | ); |
| 485 | } |
| 486 | |
| 487 | /** A two-state option toggles in place; a menu would add a click for nothing. */ |
| 488 | function OptionSwitchRow(props: { |
| 489 | descriptor: BooleanDescriptor; |
| 490 | traits: TraitsSelection; |
| 491 | tabStop: boolean; |
| 492 | }) { |
| 493 | const gt = useGT(); |
| 494 | const { descriptor, traits } = props; |
| 495 | const checked = descriptor.currentValue === true; |
| 496 | return ( |
| 497 | <label className={cn(OPTION_ROW, "has-focus-visible:bg-foreground/[0.09]")}> |
| 498 | <OptionRowText |
| 499 | label={descriptor.label} |
| 500 | value={ |
| 501 | checked ? gt("On", { $context: "option state" }) : gt("Off", { $context: "option state" }) |
| 502 | } |
| 503 | /> |
| 504 | <Switch |
| 505 | size="sm" |
| 506 | checked={checked} |
| 507 | onCheckedChange={(next) => traits.changeBoolean(descriptor.id, next)} |
| 508 | aria-label={descriptor.label} |
| 509 | data-effort-row |
| 510 | tabIndex={props.tabStop ? 0 : -1} |
| 511 | /> |
| 512 | </label> |
| 513 | ); |
| 514 | } |
| 515 | |
| 516 | function NoEffortRow() { |
| 517 | return ( |
| 518 | <div className="flex h-8 items-center px-2 text-muted-foreground text-sm"> |
| 519 | <T>No effort levels</T> |
| 520 | </div> |
| 521 | ); |
| 522 | } |
| 523 | |
| 524 | /** The model picker's effort column: the selected model's levels and its other options. */ |
| 525 | export function ComposerEffortColumn(props: { traits: TraitsSelectionInput | null }) { |
| 526 | const gt = useGT(); |
| 527 | const messages = useMessages(); |
| 528 | const picker = use(DockedPickerContext); |
| 529 | const visibility = props.traits |
| 530 | ? getTraitsSectionVisibility({ ...props.traits, resolve: messages }) |
| 531 | : null; |
| 532 | const controls = |
| 533 | props.traits && visibility |
| 534 | ? columnControls(visibility.selectDescriptors, visibility.booleanDescriptors) |
| 535 | : null; |
| 536 | return ( |
| 537 | <> |
| 538 | {/* Matches the search row's box, so one rule runs across the top of the popup. */} |
| 539 | <div className="shrink-0 px-3 pt-2.5"> |
| 540 | <div className="flex h-8.25 -translate-y-px items-center gap-2 border-b border-border/70 pb-1.5 text-sm"> |
| 541 | <span className="text-muted-foreground"> |
| 542 | {controls?.effort?.descriptor.label ?? |
| 543 | (controls?.others.length |
| 544 | ? gt("Options", { $context: "heading for model options" }) |
| 545 | : gt("Effort", { $context: "heading for reasoning effort levels" }))} |
| 546 | </span> |
| 547 | <span className="ms-auto min-w-0 truncate text-muted-foreground text-xs"> |
| 548 | {picker.modelLabel} |
| 549 | </span> |
| 550 | </div> |
| 551 | </div> |
| 552 | {/* pt-2.5 puts the first level on the same line as the first model. */} |
| 553 | <div |
| 554 | className="flex min-h-0 flex-1 flex-col gap-0.5 overflow-y-auto overscroll-contain px-2 pt-2.5 pb-1.5" |
| 555 | // Capture, so Up and Down on an option row move rows instead of opening its menu. |
| 556 | onKeyDownCapture={moveBetweenRows} |
| 557 | > |
| 558 | {props.traits ? <EffortLevels traits={props.traits} /> : <NoEffortRow />} |
| 559 | </div> |
| 560 | </> |
| 561 | ); |
| 562 | } |
| 563 | |
| 564 | const FOOTER_SEGMENT = |
| 565 | "flex h-6 cursor-pointer items-center justify-center gap-1 px-2 text-muted-foreground text-xs outline-none first:rounded-l-md last:rounded-r-md hover:text-foreground data-checked:bg-input/72 data-checked:text-foreground focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-inset"; |
| 566 | |
| 567 | function SpeedSegment(props: { |
| 568 | checked: boolean; |
| 569 | label: string; |
| 570 | /** The untranslated speed name that picks the bolt, when `label` is translated. */ |
| 571 | speed?: string; |
| 572 | description?: string | undefined; |
| 573 | onPick: () => void; |
| 574 | }) { |
| 575 | const speed = props.speed ?? props.label; |
| 576 | const buttonProps = { |
| 577 | type: "button", |
| 578 | role: "radio", |
| 579 | "aria-checked": props.checked, |
| 580 | "data-checked": props.checked || undefined, |
| 581 | className: FOOTER_SEGMENT, |
| 582 | onClick: props.onPick, |
| 583 | } as const; |
| 584 | const content = ( |
| 585 | <> |
| 586 | {speed === "Fast" || speed === "Ultrafast" ? ( |
| 587 | <FastBolt ultra={speed === "Ultrafast"} /> |
| 588 | ) : null} |
| 589 | {props.label} |
| 590 | </> |
| 591 | ); |
| 592 | if (!props.description) return <button {...buttonProps}>{content}</button>; |
| 593 | return ( |
| 594 | <Tooltip> |
| 595 | <TooltipTrigger render={<button {...buttonProps} />}>{content}</TooltipTrigger> |
| 596 | <TooltipPopup side="top">{props.description}</TooltipPopup> |
| 597 | </Tooltip> |
| 598 | ); |
| 599 | } |
| 600 | |
| 601 | function SpeedControl(props: { traits: TraitsSelectionInput }) { |
| 602 | const gt = useGT(); |
| 603 | const traits = useTraitsSelection(props.traits); |
| 604 | const { speedSelect, fastToggle } = splitSpeed( |
| 605 | traits.selectDescriptors, |
| 606 | traits.booleanDescriptors, |
| 607 | ); |
| 608 | if (traits.modelIsUnavailable || (!speedSelect && !fastToggle)) return null; |
| 609 | return ( |
| 610 | <div |
| 611 | role="radiogroup" |
| 612 | aria-label={speedSelect?.label ?? fastToggle?.label} |
| 613 | className="flex rounded-lg bg-input/40 p-0.5" |
| 614 | > |
| 615 | {speedSelect |
| 616 | ? selectableProviderOptionChoices(speedSelect).map((option) => ( |
| 617 | <SpeedSegment |
| 618 | key={option.id} |
| 619 | checked={option.id === traits.selectValue(speedSelect)} |
| 620 | label={option.label} |
| 621 | description={option.description} |
| 622 | onPick={() => traits.changeSelect(speedSelect, option.id)} |
| 623 | /> |
| 624 | )) |
| 625 | : fastToggle |
| 626 | ? ([false, true] as const).map((value) => ( |
| 627 | <SpeedSegment |
| 628 | key={String(value)} |
| 629 | checked={(fastToggle.currentValue === true) === value} |
| 630 | speed={value ? "Fast" : "Standard"} |
| 631 | label={ |
| 632 | value |
| 633 | ? gt("Fast", { $context: "response speed" }) |
| 634 | : gt("Standard", { $context: "response speed" }) |
| 635 | } |
| 636 | onPick={() => traits.changeBoolean(fastToggle.id, value)} |
| 637 | /> |
| 638 | )) |
| 639 | : null} |
| 640 | </div> |
| 641 | ); |
| 642 | } |
| 643 | |
| 644 | function UltracodeControl(props: { traits: TraitsSelectionInput }) { |
| 645 | const traits = useTraitsSelection(props.traits); |
| 646 | const { ultracodeToggle } = splitSpeed(traits.selectDescriptors, traits.booleanDescriptors); |
| 647 | if (traits.modelIsUnavailable || !ultracodeToggle) return null; |
| 648 | const on = ultracodeToggle.currentValue === true; |
| 649 | return ( |
| 650 | <div className="flex rounded-lg bg-input/40 p-0.5"> |
| 651 | <button |
| 652 | type="button" |
| 653 | aria-pressed={on} |
| 654 | data-checked={on || undefined} |
| 655 | className={FOOTER_SEGMENT} |
| 656 | onClick={() => traits.changeBoolean(ultracodeToggle.id, !on)} |
| 657 | > |
| 658 | <WorkflowIcon aria-hidden="true" className="size-3" /> |
| 659 | {ultracodeToggle.label} |
| 660 | </button> |
| 661 | </div> |
| 662 | ); |
| 663 | } |
| 664 | |
| 665 | /** Permissions change rarely, so the footer names the mode and nests the choice in a menu. */ |
| 666 | function PermissionsMenu(props: { |
| 667 | runtimeMode: RuntimeMode; |
| 668 | options: ReadonlyArray<RuntimeModeOption>; |
| 669 | onChange: (mode: RuntimeMode) => void; |
| 670 | }) { |
| 671 | const gt = useGT(); |
| 672 | const triggerRef = useRef<HTMLButtonElement>(null); |
| 673 | const current = props.options.find((option) => option.mode === props.runtimeMode); |
| 674 | if (!current) return null; |
| 675 | const CurrentIcon = current.icon; |
| 676 | return ( |
| 677 | <Menu |
| 678 | onOpenChangeComplete={(open) => { |
| 679 | if (open) focusCheckedPermission(triggerRef.current); |
| 680 | }} |
| 681 | > |
| 682 | <MenuTrigger |
| 683 | render={ |
| 684 | <button |
| 685 | ref={triggerRef} |
| 686 | type="button" |
| 687 | data-composer-permissions-trigger |
| 688 | aria-label={gt("Permissions: {mode}", { mode: current.label })} |
| 689 | className="ms-auto flex h-7 cursor-pointer items-center gap-1.5 rounded-md px-2 text-muted-foreground text-xs outline-none hover:bg-foreground/[0.09] hover:text-foreground focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-inset data-popup-open:bg-foreground/[0.09] data-popup-open:text-foreground" |
| 690 | /> |
| 691 | } |
| 692 | > |
| 693 | <CurrentIcon aria-hidden="true" className="size-3.5" /> |
| 694 | {current.label} |
| 695 | <ChevronUpIcon aria-hidden="true" className="size-3" /> |
| 696 | </MenuTrigger> |
| 697 | <MenuPopup side="top" align="end" {...composerFloatingLayerProps}> |
| 698 | <ComposerPermissionsSection |
| 699 | runtimeMode={props.runtimeMode} |
| 700 | runtimeModeOptions={props.options} |
| 701 | onRuntimeModeChange={props.onChange} |
| 702 | /> |
| 703 | </MenuPopup> |
| 704 | </Menu> |
| 705 | ); |
| 706 | } |
| 707 | |
| 708 | /** The model picker's footer: speed stays one click away, permissions sit one menu deeper. */ |
| 709 | export function ComposerPickerFooter(props: { |
| 710 | traits: TraitsSelectionInput | null; |
| 711 | runtimeMode: RuntimeMode; |
| 712 | runtimeModeOptions: ReadonlyArray<RuntimeModeOption>; |
| 713 | onRuntimeModeChange: (mode: RuntimeMode) => void; |
| 714 | }) { |
| 715 | return ( |
| 716 | <div className="flex flex-wrap items-center gap-2 border-t border-border/70 bg-muted/40 px-3 py-2"> |
| 717 | {props.traits ? <SpeedControl traits={props.traits} /> : null} |
| 718 | {props.traits ? <UltracodeControl traits={props.traits} /> : null} |
| 719 | <PermissionsMenu |
| 720 | runtimeMode={props.runtimeMode} |
| 721 | options={props.runtimeModeOptions} |
| 722 | onChange={props.onRuntimeModeChange} |
| 723 | /> |
| 724 | </div> |
| 725 | ); |
| 726 | } |