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 | buildTraitsTriggerDisplay, |
| 3 | getDescriptorStringValue, |
| 4 | getTraitSelectChange, |
| 5 | replaceDescriptorCurrentValue, |
| 6 | selectableProviderOptionChoices, |
| 7 | isReasoningSelect, |
| 8 | } from "@t3tools/client-runtime/model-traits"; |
| 9 | import { |
| 10 | type ModelSelection, |
| 11 | type ProviderDriverKind, |
| 12 | type ProviderInstanceId, |
| 13 | type ProviderOptionDescriptor, |
| 14 | type ProviderOptionSelection, |
| 15 | type ScopedThreadRef, |
| 16 | type ServerProviderModel, |
| 17 | } from "@t3tools/contracts"; |
| 18 | import { |
| 19 | buildProviderOptionSelectionsFromDescriptors, |
| 20 | getProviderOptionCurrentLabel, |
| 21 | getProviderOptionDescriptors, |
| 22 | isClaudeUltrathinkPrompt, |
| 23 | normalizeModelSlug, |
| 24 | } from "@t3tools/shared/model"; |
| 25 | import { msg, T, useMessages } from "gt-react"; |
| 26 | import { memo } from "react"; |
| 27 | import { GaugeIcon } from "lucide-react"; |
| 28 | import { |
| 29 | Menu, |
| 30 | MenuGroup, |
| 31 | MenuPopup, |
| 32 | MenuRadioGroup, |
| 33 | MenuRadioItem, |
| 34 | MenuSeparator as MenuDivider, |
| 35 | MenuTrigger, |
| 36 | } from "../ui/menu"; |
| 37 | import { useComposerDraftStore, DraftId } from "../../composerDraftStore"; |
| 38 | import { getProviderModelCapabilities, keepsUnavailableModelOptions } from "../../providerModels"; |
| 39 | import { m, type MessageResolver } from "~/i18n/messages"; |
| 40 | import { cn } from "~/lib/utils"; |
| 41 | import { Badge } from "../ui/badge"; |
| 42 | import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip"; |
| 43 | import { ComposerControl, ComposerControlIcon, type ComposerControlSize } from "./ComposerControl"; |
| 44 | import { useComposerMenuState } from "./useComposerMenuState"; |
| 45 | |
| 46 | type ProviderOptions = ReadonlyArray<ProviderOptionSelection>; |
| 47 | |
| 48 | /** |
| 49 | * All the picker reads from the prompt while rendering. Passing this instead of |
| 50 | * the prompt keeps the picker from re-rendering on every keystroke. |
| 51 | */ |
| 52 | export type PromptUltrathink = "none" | "prefix" | "body"; |
| 53 | |
| 54 | export function readPromptUltrathink(prompt: string): PromptUltrathink { |
| 55 | if (!isClaudeUltrathinkPrompt(prompt)) return "none"; |
| 56 | return isClaudeUltrathinkPrompt(prompt.replace(/^Ultrathink:\s*/i, "")) ? "body" : "prefix"; |
| 57 | } |
| 58 | |
| 59 | const SAVED_OPTION_LABELS: Readonly<Record<string, string>> = { |
| 60 | agent: msg("Agent", { $context: "model option name" }), |
| 61 | effort: msg("Effort", { $context: "model option name" }), |
| 62 | reasoningEffort: msg("Reasoning effort"), |
| 63 | variant: msg("Reasoning", { $context: "model option name" }), |
| 64 | }; |
| 65 | |
| 66 | function savedOptionLabel(id: string, resolve: MessageResolver): string { |
| 67 | const saved = SAVED_OPTION_LABELS[id]; |
| 68 | return saved !== undefined |
| 69 | ? resolve(saved) |
| 70 | : id |
| 71 | .replace(/([a-z0-9])([A-Z])/g, "$1 $2") |
| 72 | .replace(/^./, (character) => character.toUpperCase()); |
| 73 | } |
| 74 | |
| 75 | function buildUnavailableModelOptionDescriptors( |
| 76 | selections: ProviderOptions | null | undefined, |
| 77 | resolve: MessageResolver, |
| 78 | ): ReadonlyArray<ProviderOptionDescriptor> { |
| 79 | return (selections ?? []).map((selection) => |
| 80 | typeof selection.value === "boolean" |
| 81 | ? { |
| 82 | id: selection.id, |
| 83 | label: savedOptionLabel(selection.id, resolve), |
| 84 | type: "boolean" as const, |
| 85 | currentValue: selection.value, |
| 86 | } |
| 87 | : { |
| 88 | id: selection.id, |
| 89 | label: savedOptionLabel(selection.id, resolve), |
| 90 | type: "select" as const, |
| 91 | options: [{ id: selection.value, label: selection.value }], |
| 92 | currentValue: selection.value, |
| 93 | }, |
| 94 | ); |
| 95 | } |
| 96 | |
| 97 | type TraitsPersistence = |
| 98 | | { |
| 99 | threadRef?: ScopedThreadRef; |
| 100 | draftId?: DraftId; |
| 101 | /** Runs after a change is written to the draft. */ |
| 102 | onDraftModelOptionsChange?: () => void; |
| 103 | onModelOptionsChange?: never; |
| 104 | } |
| 105 | | { |
| 106 | threadRef?: undefined; |
| 107 | onDraftModelOptionsChange?: never; |
| 108 | onModelOptionsChange: (nextOptions: ProviderOptions | undefined) => void; |
| 109 | }; |
| 110 | |
| 111 | function DefaultBadge() { |
| 112 | return ( |
| 113 | <Badge variant="outline" size="sm" className="min-w-0"> |
| 114 | <T context="the default option in a list">Default</T> |
| 115 | </Badge> |
| 116 | ); |
| 117 | } |
| 118 | |
| 119 | function getSelectedTraits( |
| 120 | provider: ProviderDriverKind, |
| 121 | models: ReadonlyArray<ServerProviderModel>, |
| 122 | model: string | null | undefined, |
| 123 | promptUltrathink: PromptUltrathink, |
| 124 | modelOptions: ProviderOptions | null | undefined, |
| 125 | allowPromptInjectedEffort: boolean, |
| 126 | includePlanAgent: boolean | undefined, |
| 127 | resolve: MessageResolver = m, |
| 128 | ) { |
| 129 | const caps = getProviderModelCapabilities(models, model, provider, includePlanAgent); |
| 130 | const modelIsUnavailable = |
| 131 | keepsUnavailableModelOptions(provider, model) && |
| 132 | !models.some((candidate) => candidate.slug === normalizeModelSlug(model, provider)); |
| 133 | const descriptors = modelIsUnavailable |
| 134 | ? buildUnavailableModelOptionDescriptors( |
| 135 | includePlanAgent |
| 136 | ? modelOptions |
| 137 | : modelOptions?.filter((option) => option.id !== "agent" || option.value !== "plan"), |
| 138 | resolve, |
| 139 | ) |
| 140 | : getProviderOptionDescriptors({ |
| 141 | caps, |
| 142 | selections: modelOptions, |
| 143 | }); |
| 144 | const selectDescriptors = descriptors.filter( |
| 145 | (descriptor): descriptor is Extract<ProviderOptionDescriptor, { type: "select" }> => |
| 146 | descriptor.type === "select", |
| 147 | ); |
| 148 | const booleanDescriptors = descriptors.filter( |
| 149 | (descriptor): descriptor is Extract<ProviderOptionDescriptor, { type: "boolean" }> => |
| 150 | descriptor.type === "boolean", |
| 151 | ); |
| 152 | const primarySelectDescriptor = selectDescriptors[0] ?? null; |
| 153 | const contextWindowDescriptor = |
| 154 | selectDescriptors.find((descriptor) => descriptor.id === "contextWindow") ?? null; |
| 155 | const agentDescriptor = selectDescriptors.find((descriptor) => descriptor.id === "agent") ?? null; |
| 156 | const fastModeDescriptor = |
| 157 | booleanDescriptors.find((descriptor) => descriptor.id === "fastMode") ?? null; |
| 158 | const thinkingDescriptor = |
| 159 | booleanDescriptors.find((descriptor) => descriptor.id === "thinking") ?? null; |
| 160 | |
| 161 | const ultrathinkPromptControlled = |
| 162 | allowPromptInjectedEffort && |
| 163 | (primarySelectDescriptor?.promptInjectedValues?.length ?? 0) > 0 && |
| 164 | promptUltrathink !== "none"; |
| 165 | const ultrathinkInBodyText = ultrathinkPromptControlled && promptUltrathink === "body"; |
| 166 | const effort = |
| 167 | (ultrathinkPromptControlled |
| 168 | ? "ultrathink" |
| 169 | : getDescriptorStringValue(primarySelectDescriptor)) ?? null; |
| 170 | const thinkingEnabled = |
| 171 | typeof thinkingDescriptor?.currentValue === "boolean" ? thinkingDescriptor.currentValue : null; |
| 172 | const contextWindow = getDescriptorStringValue(contextWindowDescriptor); |
| 173 | const selectedAgent = getDescriptorStringValue(agentDescriptor); |
| 174 | const selectedAgentLabel = agentDescriptor |
| 175 | ? getProviderOptionCurrentLabel(agentDescriptor) |
| 176 | : null; |
| 177 | |
| 178 | return { |
| 179 | caps, |
| 180 | descriptors, |
| 181 | selectDescriptors, |
| 182 | booleanDescriptors, |
| 183 | primarySelectDescriptor, |
| 184 | contextWindowDescriptor, |
| 185 | agentDescriptor, |
| 186 | fastModeDescriptor, |
| 187 | thinkingDescriptor, |
| 188 | effort, |
| 189 | thinkingEnabled, |
| 190 | contextWindow, |
| 191 | ultrathinkPromptControlled, |
| 192 | ultrathinkInBodyText, |
| 193 | selectedAgent, |
| 194 | selectedAgentLabel, |
| 195 | modelIsUnavailable, |
| 196 | }; |
| 197 | } |
| 198 | |
| 199 | export function getTraitsSectionVisibility(input: { |
| 200 | provider: ProviderDriverKind; |
| 201 | models: ReadonlyArray<ServerProviderModel>; |
| 202 | model: string | null | undefined; |
| 203 | promptUltrathink?: PromptUltrathink | undefined; |
| 204 | modelOptions?: ProviderOptions | null | undefined; |
| 205 | allowPromptInjectedEffort?: boolean; |
| 206 | includePlanAgent?: boolean | undefined; |
| 207 | resolve?: MessageResolver; |
| 208 | }) { |
| 209 | const selected = getSelectedTraits( |
| 210 | input.provider, |
| 211 | input.models, |
| 212 | input.model, |
| 213 | input.promptUltrathink ?? "none", |
| 214 | input.modelOptions, |
| 215 | input.allowPromptInjectedEffort ?? true, |
| 216 | input.includePlanAgent, |
| 217 | input.resolve, |
| 218 | ); |
| 219 | |
| 220 | const showEffort = selected.primarySelectDescriptor !== null; |
| 221 | const showThinking = selected.thinkingDescriptor !== null; |
| 222 | const showFastMode = selected.fastModeDescriptor !== null; |
| 223 | const showContextWindow = selected.contextWindowDescriptor !== null; |
| 224 | const showAgent = selected.agentDescriptor !== null; |
| 225 | |
| 226 | return { |
| 227 | ...selected, |
| 228 | showEffort, |
| 229 | showThinking, |
| 230 | showFastMode, |
| 231 | showContextWindow, |
| 232 | showAgent, |
| 233 | hasAnyControls: |
| 234 | showEffort || |
| 235 | showThinking || |
| 236 | showFastMode || |
| 237 | showContextWindow || |
| 238 | showAgent || |
| 239 | (selected.modelIsUnavailable && selected.descriptors.length > 0), |
| 240 | }; |
| 241 | } |
| 242 | |
| 243 | /** |
| 244 | * `modelOptions` must already carry the composer's implicit defaults (see |
| 245 | * `withImplicitFastModeDefault`) so the persisted selection matches what the |
| 246 | * picker would write. |
| 247 | */ |
| 248 | export function getReasoningTraitStep(input: { |
| 249 | provider: ProviderDriverKind; |
| 250 | instanceId: ProviderInstanceId; |
| 251 | model: string; |
| 252 | models: ReadonlyArray<ServerProviderModel>; |
| 253 | modelOptions: ProviderOptions | undefined; |
| 254 | reportedModelSelection: ModelSelection | null | undefined; |
| 255 | prompt: string; |
| 256 | includePlanAgent?: boolean | undefined; |
| 257 | direction: 1 | -1; |
| 258 | }) { |
| 259 | const selected = getSelectedTraits( |
| 260 | input.provider, |
| 261 | input.models, |
| 262 | input.model, |
| 263 | readPromptUltrathink(input.prompt), |
| 264 | input.modelOptions, |
| 265 | true, |
| 266 | input.includePlanAgent, |
| 267 | ); |
| 268 | if (selected.modelIsUnavailable || selected.ultrathinkInBodyText) return null; |
| 269 | const descriptor = selected.selectDescriptors.find(isReasoningSelect); |
| 270 | const choices = descriptor ? selectableProviderOptionChoices(descriptor) : []; |
| 271 | if (!descriptor || choices.length < 2) return null; |
| 272 | const isPrimary = descriptor.id === selected.primarySelectDescriptor?.id; |
| 273 | const current = |
| 274 | isPrimary && selected.ultrathinkPromptControlled |
| 275 | ? "ultrathink" |
| 276 | : getDescriptorStringValue( |
| 277 | descriptor, |
| 278 | { instanceId: input.instanceId, model: input.model, options: input.modelOptions ?? [] }, |
| 279 | input.reportedModelSelection, |
| 280 | ); |
| 281 | // Walk the full catalog order so a typed Ultrathink keeps its place, then land on a pickable level. |
| 282 | const ordered = input.direction > 0 ? descriptor.options : descriptor.options.toReversed(); |
| 283 | const position = |
| 284 | [current, getDescriptorStringValue(descriptor)] |
| 285 | .map((value) => ordered.findIndex((option) => option.id === value)) |
| 286 | .find((index) => index >= 0) ?? -1; |
| 287 | const next = [...ordered.slice(position + 1), ...ordered.slice(0, Math.max(position, 0))].find( |
| 288 | (option) => choices.includes(option), |
| 289 | ); |
| 290 | const change = next && getTraitSelectChange(selected, descriptor, input.prompt, next.id); |
| 291 | return change?.modelOptions ? { prompt: change.prompt, modelOptions: change.modelOptions } : null; |
| 292 | } |
| 293 | |
| 294 | export interface TraitsMenuContentProps { |
| 295 | provider: ProviderDriverKind; |
| 296 | instanceId?: ProviderInstanceId; |
| 297 | models: ReadonlyArray<ServerProviderModel>; |
| 298 | model: string | null | undefined; |
| 299 | promptUltrathink?: PromptUltrathink | undefined; |
| 300 | /** Receives an edit of the live prompt; the picker never holds the prompt text itself. */ |
| 301 | onPromptEdit?: (edit: (prompt: string) => string) => void; |
| 302 | modelOptions?: ProviderOptions | null | undefined; |
| 303 | reportedModelSelection?: ModelSelection | null | undefined; |
| 304 | allowPromptInjectedEffort?: boolean; |
| 305 | includePlanAgent?: boolean | undefined; |
| 306 | triggerClassName?: string; |
| 307 | } |
| 308 | |
| 309 | export type TraitsSelectionInput = TraitsMenuContentProps & TraitsPersistence; |
| 310 | |
| 311 | /** The model's traits with their shown values, and writers that persist a change. */ |
| 312 | export function useTraitsSelection({ |
| 313 | provider, |
| 314 | instanceId, |
| 315 | models, |
| 316 | model, |
| 317 | promptUltrathink, |
| 318 | onPromptEdit, |
| 319 | modelOptions, |
| 320 | reportedModelSelection, |
| 321 | allowPromptInjectedEffort = true, |
| 322 | includePlanAgent, |
| 323 | triggerClassName: _triggerClassName, |
| 324 | ...persistence |
| 325 | }: TraitsSelectionInput) { |
| 326 | const modelSelection = |
| 327 | instanceId && model ? { instanceId, model, options: modelOptions ?? [] } : null; |
| 328 | const setProviderModelOptions = useComposerDraftStore((store) => store.setProviderModelOptions); |
| 329 | const resolve = useMessages(); |
| 330 | const updateModelOptions = (nextOptions: ProviderOptions | undefined) => { |
| 331 | if ("onModelOptionsChange" in persistence) { |
| 332 | persistence.onModelOptionsChange(nextOptions); |
| 333 | return; |
| 334 | } |
| 335 | const threadTarget = persistence.threadRef ?? persistence.draftId; |
| 336 | if (!threadTarget) { |
| 337 | return; |
| 338 | } |
| 339 | setProviderModelOptions(threadTarget, provider, nextOptions, { |
| 340 | ...(instanceId ? { instanceId } : {}), |
| 341 | model, |
| 342 | persistSticky: true, |
| 343 | }); |
| 344 | persistence.onDraftModelOptionsChange?.(); |
| 345 | }; |
| 346 | const selected = getTraitsSectionVisibility({ |
| 347 | provider, |
| 348 | models, |
| 349 | model, |
| 350 | promptUltrathink, |
| 351 | modelOptions, |
| 352 | allowPromptInjectedEffort, |
| 353 | includePlanAgent, |
| 354 | resolve, |
| 355 | }); |
| 356 | return { |
| 357 | ...selected, |
| 358 | /** The option a select shows, where an Ultrathink prompt owns the primary select. */ |
| 359 | selectValue: (descriptor: Extract<ProviderOptionDescriptor, { type: "select" }>) => |
| 360 | selected.ultrathinkPromptControlled && descriptor.id === selected.primarySelectDescriptor?.id |
| 361 | ? "ultrathink" |
| 362 | : (getDescriptorStringValue(descriptor, modelSelection, reportedModelSelection) ?? ""), |
| 363 | currentLabel: (descriptor: ProviderOptionDescriptor) => |
| 364 | getProviderOptionCurrentLabel(descriptor, modelSelection, reportedModelSelection), |
| 365 | changeSelect: ( |
| 366 | descriptor: Extract<ProviderOptionDescriptor, { type: "select" }>, |
| 367 | value: string, |
| 368 | ) => { |
| 369 | // Whether a pick applies, and the options it writes, do not depend on the prompt text. |
| 370 | const change = getTraitSelectChange(selected, descriptor, "", value); |
| 371 | if (!change) return; |
| 372 | onPromptEdit?.( |
| 373 | (prompt) => getTraitSelectChange(selected, descriptor, prompt, value)?.prompt ?? prompt, |
| 374 | ); |
| 375 | updateModelOptions(change.modelOptions); |
| 376 | }, |
| 377 | changeBoolean: (descriptorId: string, value: boolean) => { |
| 378 | updateModelOptions( |
| 379 | buildProviderOptionSelectionsFromDescriptors( |
| 380 | replaceDescriptorCurrentValue(selected.descriptors, descriptorId, value), |
| 381 | ), |
| 382 | ); |
| 383 | }, |
| 384 | }; |
| 385 | } |
| 386 | |
| 387 | export const ULTRATHINK_IN_BODY_NOTE = msg( |
| 388 | 'Your prompt contains "ultrathink" in the text. Remove it to change this option.', |
| 389 | ); |
| 390 | |
| 391 | const TraitsMenuContent = memo(function TraitsMenuContentImpl(props: TraitsSelectionInput) { |
| 392 | const traits = useTraitsSelection(props); |
| 393 | const resolve = useMessages(); |
| 394 | const { |
| 395 | descriptors, |
| 396 | selectDescriptors, |
| 397 | booleanDescriptors, |
| 398 | primarySelectDescriptor, |
| 399 | ultrathinkInBodyText, |
| 400 | hasAnyControls, |
| 401 | modelIsUnavailable, |
| 402 | } = traits; |
| 403 | |
| 404 | if (!hasAnyControls) { |
| 405 | return null; |
| 406 | } |
| 407 | |
| 408 | if (modelIsUnavailable) { |
| 409 | return ( |
| 410 | <> |
| 411 | {descriptors.map((descriptor, index) => { |
| 412 | const value = traits.currentLabel(descriptor); |
| 413 | if (!value) return null; |
| 414 | return ( |
| 415 | <div key={descriptor.id}> |
| 416 | {index > 0 ? <MenuDivider /> : null} |
| 417 | <MenuGroup> |
| 418 | <div className="px-2 pt-1.5 pb-1 font-medium text-muted-foreground text-xs"> |
| 419 | {descriptor.label} |
| 420 | </div> |
| 421 | <div className="px-2 pb-1.5 text-muted-foreground/80 text-xs">{value}</div> |
| 422 | </MenuGroup> |
| 423 | </div> |
| 424 | ); |
| 425 | })} |
| 426 | </> |
| 427 | ); |
| 428 | } |
| 429 | |
| 430 | return ( |
| 431 | <> |
| 432 | {selectDescriptors.map((descriptor, index) => ( |
| 433 | <div key={descriptor.id}> |
| 434 | {index > 0 ? <MenuDivider /> : null} |
| 435 | <MenuGroup> |
| 436 | <div className="px-2 pt-1.5 pb-1 font-medium text-muted-foreground text-xs"> |
| 437 | {descriptor.label} |
| 438 | </div> |
| 439 | {ultrathinkInBodyText && descriptor.id === primarySelectDescriptor?.id ? ( |
| 440 | <div className="px-2 pb-1.5 text-muted-foreground/80 text-xs"> |
| 441 | {resolve(ULTRATHINK_IN_BODY_NOTE)} |
| 442 | </div> |
| 443 | ) : null} |
| 444 | <MenuRadioGroup |
| 445 | value={traits.selectValue(descriptor)} |
| 446 | onValueChange={(value) => traits.changeSelect(descriptor, value)} |
| 447 | > |
| 448 | {selectableProviderOptionChoices(descriptor).map((option) => ( |
| 449 | <MenuRadioItem |
| 450 | key={option.id} |
| 451 | value={option.id} |
| 452 | hideIndicator |
| 453 | closeOnClick |
| 454 | disabled={ultrathinkInBodyText && descriptor.id === primarySelectDescriptor?.id} |
| 455 | > |
| 456 | <span className="flex w-full min-w-0 flex-col"> |
| 457 | <span className="flex min-w-0 items-center gap-1"> |
| 458 | <span className="min-w-0 truncate">{option.label}</span> |
| 459 | {option.isDefault ? <DefaultBadge /> : null} |
| 460 | </span> |
| 461 | {option.description ? ( |
| 462 | <span className="max-w-56 text-pretty text-muted-foreground/80 text-xs"> |
| 463 | {option.description} |
| 464 | </span> |
| 465 | ) : null} |
| 466 | </span> |
| 467 | </MenuRadioItem> |
| 468 | ))} |
| 469 | </MenuRadioGroup> |
| 470 | </MenuGroup> |
| 471 | </div> |
| 472 | ))} |
| 473 | {booleanDescriptors.map((descriptor, index) => ( |
| 474 | <div key={descriptor.id}> |
| 475 | {index > 0 || selectDescriptors.length > 0 ? <MenuDivider /> : null} |
| 476 | <MenuGroup> |
| 477 | <div className="px-2 py-1.5 font-medium text-muted-foreground text-xs"> |
| 478 | {descriptor.label} |
| 479 | </div> |
| 480 | <MenuRadioGroup |
| 481 | value={descriptor.currentValue === true ? "on" : "off"} |
| 482 | onValueChange={(value) => traits.changeBoolean(descriptor.id, value === "on")} |
| 483 | > |
| 484 | {(["on", "off"] as const).map((value) => ( |
| 485 | <MenuRadioItem key={value} value={value} hideIndicator closeOnClick> |
| 486 | <span className="flex w-full min-w-0 items-center justify-between gap-3"> |
| 487 | <span> |
| 488 | {value === "on" ? ( |
| 489 | <T context="toggle state">On</T> |
| 490 | ) : ( |
| 491 | <T context="toggle state">Off</T> |
| 492 | )} |
| 493 | </span> |
| 494 | </span> |
| 495 | </MenuRadioItem> |
| 496 | ))} |
| 497 | </MenuRadioGroup> |
| 498 | </MenuGroup> |
| 499 | </div> |
| 500 | ))} |
| 501 | </> |
| 502 | ); |
| 503 | }); |
| 504 | |
| 505 | export const TraitsPicker = memo(function TraitsPicker({ |
| 506 | size = "sm", |
| 507 | hidden = false, |
| 508 | disabled = false, |
| 509 | ...props |
| 510 | }: TraitsSelectionInput & { |
| 511 | size?: ComposerControlSize; |
| 512 | hidden?: boolean; |
| 513 | disabled?: boolean; |
| 514 | }) { |
| 515 | const [isMenuOpen, setIsMenuOpen] = useComposerMenuState(hidden || disabled); |
| 516 | const traits = useTraitsSelection(props); |
| 517 | const resolve = useMessages(); |
| 518 | if (!traits.hasAnyControls) { |
| 519 | return null; |
| 520 | } |
| 521 | const { label: triggerLabel } = buildTraitsTriggerDisplay( |
| 522 | { |
| 523 | descriptors: traits.descriptors, |
| 524 | primarySelectDescriptorId: traits.primarySelectDescriptor?.id ?? null, |
| 525 | ultrathinkPromptControlled: traits.ultrathinkPromptControlled, |
| 526 | modelSelection: |
| 527 | props.instanceId && props.model |
| 528 | ? { instanceId: props.instanceId, model: props.model, options: props.modelOptions ?? [] } |
| 529 | : null, |
| 530 | reportedModelSelection: props.reportedModelSelection, |
| 531 | }, |
| 532 | resolve, |
| 533 | ); |
| 534 | const isCodexStyle = props.provider === "codex"; |
| 535 | |
| 536 | return ( |
| 537 | <Menu |
| 538 | open={isMenuOpen} |
| 539 | onOpenChange={(open) => { |
| 540 | setIsMenuOpen(open && !disabled); |
| 541 | }} |
| 542 | > |
| 543 | <Tooltip> |
| 544 | <TooltipTrigger |
| 545 | render={ |
| 546 | <MenuTrigger |
| 547 | render={ |
| 548 | <ComposerControl |
| 549 | disabled={disabled} |
| 550 | aria-label={triggerLabel} |
| 551 | size={size} |
| 552 | className={cn( |
| 553 | isCodexStyle |
| 554 | ? "min-w-0 max-w-40 shrink justify-start overflow-hidden whitespace-nowrap sm:max-w-48" |
| 555 | : "shrink-0 whitespace-nowrap", |
| 556 | props.triggerClassName, |
| 557 | )} |
| 558 | /> |
| 559 | } |
| 560 | /> |
| 561 | } |
| 562 | > |
| 563 | {isCodexStyle ? ( |
| 564 | <span |
| 565 | className={cn( |
| 566 | "flex min-w-0 w-full items-center", |
| 567 | size === "xs" ? "gap-1" : "gap-1.5", |
| 568 | )} |
| 569 | > |
| 570 | <ComposerControlIcon icon={GaugeIcon} size={size} /> |
| 571 | <span data-composer-control-label className="min-w-0 truncate"> |
| 572 | {triggerLabel} |
| 573 | </span> |
| 574 | </span> |
| 575 | ) : ( |
| 576 | <> |
| 577 | <ComposerControlIcon icon={GaugeIcon} size={size} /> |
| 578 | <span data-composer-control-label>{triggerLabel}</span> |
| 579 | </> |
| 580 | )} |
| 581 | </TooltipTrigger> |
| 582 | <TooltipPopup side="top">{triggerLabel}</TooltipPopup> |
| 583 | </Tooltip> |
| 584 | <MenuPopup align="start"> |
| 585 | <TraitsMenuContent {...props} /> |
| 586 | </MenuPopup> |
| 587 | </Menu> |
| 588 | ); |
| 589 | }); |