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 { ArrowDownIcon, ArrowUpIcon, PencilIcon, PlusIcon, StarIcon, XIcon } from "lucide-react"; |
| 2 | import { msg, T, useGT, useMessages } from "gt-react"; |
| 3 | import { memo, useEffect, useRef, useState } from "react"; |
| 4 | import { |
| 5 | ProviderDriverKind, |
| 6 | type ProviderInstanceId, |
| 7 | type ServerProviderModel, |
| 8 | } from "@t3tools/contracts"; |
| 9 | import { isReasoningControl } from "@t3tools/client-runtime/model-traits"; |
| 10 | import { type CustomModelDefinition, normalizeCustomModelSlug } from "@t3tools/shared/model"; |
| 11 | |
| 12 | import { useStableCallback } from "../../hooks/useStableCallback"; |
| 13 | import type { MessageResolver } from "../../i18n/messages"; |
| 14 | import { cn } from "../../lib/utils"; |
| 15 | import { sortModelsForProviderInstance } from "../../modelOrdering"; |
| 16 | import { MAX_CUSTOM_MODEL_LENGTH } from "../../modelSelection"; |
| 17 | import { Button } from "../ui/button"; |
| 18 | import { Input } from "../ui/input"; |
| 19 | import { Switch } from "../ui/switch"; |
| 20 | import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip"; |
| 21 | import { CustomModelEditor } from "./CustomModelEditor"; |
| 22 | |
| 23 | const CUSTOM_MODEL_PLACEHOLDER_BY_KIND: Partial<Record<ProviderDriverKind, string>> = { |
| 24 | [ProviderDriverKind.make("codex")]: "gpt-6.7-codex-ultra-preview", |
| 25 | [ProviderDriverKind.make("claudeAgent")]: "claude-sonnet-5", |
| 26 | [ProviderDriverKind.make("cursor")]: "claude-sonnet-4-6", |
| 27 | [ProviderDriverKind.make("opencode")]: "openai/gpt-5", |
| 28 | [ProviderDriverKind.make("pi")]: "anthropic/claude-sonnet-5", |
| 29 | }; |
| 30 | |
| 31 | const FILTER_THRESHOLD = 8; |
| 32 | |
| 33 | const PROVIDER_ALIAS_SLUGS = new Set(["default", "auto"]); |
| 34 | |
| 35 | function hasReasoningControl(model: ServerProviderModel): boolean { |
| 36 | return (model.capabilities?.optionDescriptors ?? []).some(isReasoningControl); |
| 37 | } |
| 38 | |
| 39 | // Reasoning is the norm, so only its absence is labeled, and only when other models have it. |
| 40 | // Custom and null capabilities are fallbacks or user-written, not discovered, so they prove nothing. |
| 41 | // Alias slugs ("default", Cursor's "auto") defer to the provider's own model choice, so reasoning is unknown. |
| 42 | function describeModelCapabilities( |
| 43 | model: ServerProviderModel, |
| 44 | flagMissingReasoning: boolean, |
| 45 | resolve: MessageResolver, |
| 46 | ): string[] { |
| 47 | const descriptors = model.capabilities?.optionDescriptors ?? []; |
| 48 | const labels: string[] = []; |
| 49 | const hasFastMode = descriptors.some( |
| 50 | (descriptor) => |
| 51 | descriptor.id === "fastMode" || |
| 52 | (descriptor.id === "serviceTier" && |
| 53 | descriptor.type === "select" && |
| 54 | descriptor.options.some((option) => option.id === "fast" || option.label === "Fast")), |
| 55 | ); |
| 56 | if (hasFastMode) labels.push(resolve(msg("Fast mode"))); |
| 57 | if ( |
| 58 | flagMissingReasoning && |
| 59 | !model.isCustom && |
| 60 | model.capabilities && |
| 61 | !PROVIDER_ALIAS_SLUGS.has(model.slug) && |
| 62 | !hasReasoningControl(model) |
| 63 | ) { |
| 64 | labels.push(resolve(msg("No reasoning"))); |
| 65 | } |
| 66 | return labels; |
| 67 | } |
| 68 | |
| 69 | function groupModelsForDisplay<T extends { readonly slug: string; readonly isCustom: boolean }>( |
| 70 | models: ReadonlyArray<T>, |
| 71 | options: { |
| 72 | readonly favoriteModels: ReadonlySet<string>; |
| 73 | readonly hiddenModels: ReadonlySet<string>; |
| 74 | readonly modelOrder: ReadonlyArray<string>; |
| 75 | }, |
| 76 | ): T[] { |
| 77 | const ordered = sortModelsForProviderInstance(models, { |
| 78 | favoriteModels: options.favoriteModels, |
| 79 | groupFavorites: true, |
| 80 | modelOrder: options.modelOrder, |
| 81 | }); |
| 82 | const isHidden = (model: T) => !model.isCustom && options.hiddenModels.has(model.slug); |
| 83 | return [ |
| 84 | ...ordered.filter((model) => options.favoriteModels.has(model.slug)), |
| 85 | ...ordered.filter((model) => !options.favoriteModels.has(model.slug) && !isHidden(model)), |
| 86 | ...ordered.filter((model) => !options.favoriteModels.has(model.slug) && isHidden(model)), |
| 87 | ]; |
| 88 | } |
| 89 | |
| 90 | function nextHiddenModelsForBulkToggle( |
| 91 | models: ReadonlyArray<Pick<ServerProviderModel, "slug" | "isCustom">>, |
| 92 | hiddenModels: ReadonlyArray<string>, |
| 93 | ): string[] { |
| 94 | const builtInSlugs = models.filter((model) => !model.isCustom).map((model) => model.slug); |
| 95 | const builtInSlugSet = new Set(builtInSlugs); |
| 96 | const allBuiltInModelsHidden = builtInSlugs.every((slug) => hiddenModels.includes(slug)); |
| 97 | |
| 98 | if (allBuiltInModelsHidden) { |
| 99 | return hiddenModels.filter((slug) => !builtInSlugSet.has(slug)); |
| 100 | } |
| 101 | |
| 102 | return [...new Set([...hiddenModels, ...builtInSlugs])]; |
| 103 | } |
| 104 | |
| 105 | interface ProviderModelsSectionProps { |
| 106 | readonly instanceId: ProviderInstanceId; |
| 107 | readonly driverKind: ProviderDriverKind | null; |
| 108 | readonly models: ReadonlyArray<ServerProviderModel>; |
| 109 | readonly customModels: ReadonlyArray<CustomModelDefinition>; |
| 110 | readonly canManageCustomModels: boolean; |
| 111 | readonly canWritePreferences?: boolean; |
| 112 | readonly hiddenModels: ReadonlyArray<string>; |
| 113 | readonly favoriteModels: ReadonlyArray<string>; |
| 114 | readonly modelOrder: ReadonlyArray<string>; |
| 115 | /** Resolves true once the custom models are saved. */ |
| 116 | readonly onChange: (next: ReadonlyArray<CustomModelDefinition>) => Promise<boolean>; |
| 117 | readonly onHiddenModelsChange: ( |
| 118 | next: ReadonlyArray<string>, |
| 119 | affectedModels: ReadonlyArray<string>, |
| 120 | ) => void; |
| 121 | readonly onFavoriteModelsChange: (next: ReadonlyArray<string>) => void; |
| 122 | readonly onToggleFavorite: (model: string) => void; |
| 123 | readonly onModelOrderChange: (next: ReadonlyArray<string>) => void; |
| 124 | } |
| 125 | |
| 126 | export function ProviderModelsSection({ |
| 127 | instanceId, |
| 128 | driverKind, |
| 129 | models, |
| 130 | customModels, |
| 131 | canManageCustomModels, |
| 132 | canWritePreferences = true, |
| 133 | hiddenModels, |
| 134 | favoriteModels, |
| 135 | modelOrder, |
| 136 | onChange, |
| 137 | onHiddenModelsChange, |
| 138 | onFavoriteModelsChange, |
| 139 | onToggleFavorite, |
| 140 | onModelOrderChange, |
| 141 | }: ProviderModelsSectionProps) { |
| 142 | const gt = useGT(); |
| 143 | const [input, setInput] = useState(""); |
| 144 | const [isAdding, setIsAdding] = useState(false); |
| 145 | const [isSaving, setIsSaving] = useState(false); |
| 146 | const addAttemptRef = useRef(0); |
| 147 | const [filter, setFilter] = useState(""); |
| 148 | const [error, setError] = useState<string | null>(null); |
| 149 | const [editingSlug, setEditingSlug] = useState<string | null>(null); |
| 150 | const editAttemptRef = useRef(0); |
| 151 | const listRef = useRef<HTMLDivElement>(null); |
| 152 | const scrollToSlugRef = useRef<string | null>(null); |
| 153 | // A row toggled on its own keeps its placement until a bulk toggle or remount regroups the list. |
| 154 | const [heldPlacements, setHeldPlacements] = useState<ReadonlyMap<string, boolean>>( |
| 155 | () => new Map(), |
| 156 | ); |
| 157 | const hiddenModelSet = new Set(hiddenModels); |
| 158 | const favoriteModelSet = new Set(favoriteModels); |
| 159 | const reportedSlugs = new Set(models.map((model) => model.slug)); |
| 160 | // A favorite the provider no longer reports (disabled, signed out, model removed) |
| 161 | // still needs a row, or its star could never be cleared. |
| 162 | const unreportedFavoriteModels = favoriteModels.flatMap((slug): ServerProviderModel[] => |
| 163 | reportedSlugs.has(slug) ? [] : [{ slug, name: slug, isCustom: false, capabilities: null }], |
| 164 | ); |
| 165 | const listedModels = [...models, ...unreportedFavoriteModels]; |
| 166 | const placedHiddenSet = new Set( |
| 167 | listedModels |
| 168 | .filter((model) => heldPlacements.get(model.slug) ?? hiddenModelSet.has(model.slug)) |
| 169 | .map((model) => model.slug), |
| 170 | ); |
| 171 | const displayModels = groupModelsForDisplay(listedModels, { |
| 172 | favoriteModels: favoriteModelSet, |
| 173 | hiddenModels: placedHiddenSet, |
| 174 | modelOrder, |
| 175 | }); |
| 176 | const favoriteCount = displayModels.filter((model) => favoriteModelSet.has(model.slug)).length; |
| 177 | const hiddenCount = displayModels.filter( |
| 178 | (model) => !model.isCustom && hiddenModelSet.has(model.slug), |
| 179 | ).length; |
| 180 | const flagMissingReasoning = models.some(hasReasoningControl); |
| 181 | const showFilter = models.length > FILTER_THRESHOLD; |
| 182 | const normalizedFilter = filter.trim().toLowerCase(); |
| 183 | const isFiltering = showFilter && normalizedFilter.length > 0; |
| 184 | const visibleModels = isFiltering |
| 185 | ? displayModels.filter( |
| 186 | (model) => |
| 187 | model.name.toLowerCase().includes(normalizedFilter) || |
| 188 | model.slug.toLowerCase().includes(normalizedFilter), |
| 189 | ) |
| 190 | : displayModels; |
| 191 | |
| 192 | // A model added or moved lands in a later commit, so each commit looks for the pending row. |
| 193 | // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- scrolls the pending model's DOM row into view once a later commit renders it |
| 194 | useEffect(() => { |
| 195 | const slug = scrollToSlugRef.current; |
| 196 | if (slug === null) return; |
| 197 | const row = listRef.current?.querySelector<HTMLElement>( |
| 198 | `[data-model-slug="${CSS.escape(slug)}"]`, |
| 199 | ); |
| 200 | if (!row) return; |
| 201 | scrollToSlugRef.current = null; |
| 202 | row.scrollIntoView({ block: "nearest" }); |
| 203 | }); |
| 204 | |
| 205 | // Derive after the effect and use hiddenModels directly to preserve the editor's option-array cache. |
| 206 | const builtInModels = models.filter((model) => !model.isCustom); |
| 207 | const allBuiltInModelsHidden = |
| 208 | builtInModels.length > 0 && builtInModels.every((model) => hiddenModels.includes(model.slug)); |
| 209 | |
| 210 | const handleAdd = async () => { |
| 211 | if (!canManageCustomModels || driverKind === "antigravity" || isSaving) return; |
| 212 | const normalized = normalizeCustomModelSlug(input); |
| 213 | if (!normalized) { |
| 214 | setError(gt("Enter a model slug.")); |
| 215 | return; |
| 216 | } |
| 217 | if (models.some((model) => !model.isCustom && model.slug === normalized)) { |
| 218 | setError(gt("That model is already built in.")); |
| 219 | return; |
| 220 | } |
| 221 | if (normalized.length > MAX_CUSTOM_MODEL_LENGTH) { |
| 222 | setError( |
| 223 | gt("Model slugs must be {max} characters or less.", { max: MAX_CUSTOM_MODEL_LENGTH }), |
| 224 | ); |
| 225 | return; |
| 226 | } |
| 227 | if (customModels.some((entry) => entry.slug === normalized)) { |
| 228 | setError(gt("That custom model is already saved.")); |
| 229 | return; |
| 230 | } |
| 231 | |
| 232 | addAttemptRef.current += 1; |
| 233 | const attempt = addAttemptRef.current; |
| 234 | setIsSaving(true); |
| 235 | const saved = await onChange([ |
| 236 | ...customModels, |
| 237 | { slug: normalized, name: normalized, capabilities: null }, |
| 238 | ]); |
| 239 | setIsSaving(false); |
| 240 | // Cancel bumps the attempt, so a late save cannot close the draft opened after it. |
| 241 | if (!saved || attempt !== addAttemptRef.current) return; |
| 242 | scrollToSlugRef.current = normalized; |
| 243 | setFilter(""); |
| 244 | setInput(""); |
| 245 | setError(null); |
| 246 | setIsAdding(false); |
| 247 | }; |
| 248 | |
| 249 | const cancelAdd = () => { |
| 250 | addAttemptRef.current += 1; |
| 251 | setInput(""); |
| 252 | setError(null); |
| 253 | setIsAdding(false); |
| 254 | }; |
| 255 | |
| 256 | const handleRemove = (slug: string) => { |
| 257 | if (!canManageCustomModels) return; |
| 258 | if (editingSlug === slug) { |
| 259 | editAttemptRef.current += 1; |
| 260 | setEditingSlug(null); |
| 261 | } |
| 262 | void onChange(customModels.filter((entry) => entry.slug !== slug)); |
| 263 | onModelOrderChange(modelOrder.filter((model) => model !== slug)); |
| 264 | onFavoriteModelsChange(favoriteModels.filter((model) => model !== slug)); |
| 265 | setError(null); |
| 266 | }; |
| 267 | |
| 268 | const handleSaveEdit = async (next: CustomModelDefinition) => { |
| 269 | if (!canManageCustomModels) return; |
| 270 | const attempt = ++editAttemptRef.current; |
| 271 | const saved = await onChange( |
| 272 | customModels.map((entry) => (entry.slug === next.slug ? next : entry)), |
| 273 | ); |
| 274 | if (saved && attempt === editAttemptRef.current) setEditingSlug(null); |
| 275 | }; |
| 276 | |
| 277 | const setHidden = (slug: string, hidden: boolean) => { |
| 278 | if (!canWritePreferences || hidden === hiddenModelSet.has(slug)) return; |
| 279 | setHeldPlacements((current) => |
| 280 | current.has(slug) ? current : new Map(current).set(slug, hiddenModelSet.has(slug)), |
| 281 | ); |
| 282 | onHiddenModelsChange( |
| 283 | hidden ? [...hiddenModels, slug] : hiddenModels.filter((model) => model !== slug), |
| 284 | [slug], |
| 285 | ); |
| 286 | }; |
| 287 | |
| 288 | const handleToggleFavorite = (slug: string) => { |
| 289 | if (!canWritePreferences) return; |
| 290 | onToggleFavorite(slug); |
| 291 | }; |
| 292 | |
| 293 | const groupOf = (model: (typeof displayModels)[number]) => |
| 294 | favoriteModelSet.has(model.slug) |
| 295 | ? "favorite" |
| 296 | : !model.isCustom && placedHiddenSet.has(model.slug) |
| 297 | ? "hidden" |
| 298 | : "visible"; |
| 299 | const handleMove = (slug: string, direction: -1 | 1) => { |
| 300 | if (!canWritePreferences) return; |
| 301 | const index = displayModels.findIndex((model) => model.slug === slug); |
| 302 | const nextIndex = index + direction; |
| 303 | if (index < 0 || nextIndex < 0 || nextIndex >= displayModels.length) return; |
| 304 | if (groupOf(displayModels[index]!) !== groupOf(displayModels[nextIndex]!)) return; |
| 305 | const next = displayModels.map((model) => model.slug); |
| 306 | [next[index], next[nextIndex]] = [next[nextIndex]!, next[index]!]; |
| 307 | onModelOrderChange(next); |
| 308 | }; |
| 309 | |
| 310 | const handleToggleEdit = useStableCallback((slug: string) => { |
| 311 | if (!canManageCustomModels) return; |
| 312 | editAttemptRef.current += 1; |
| 313 | setEditingSlug((current) => (current === slug ? null : slug)); |
| 314 | }); |
| 315 | const rowHandlers = { |
| 316 | onToggleFavorite: useStableCallback(handleToggleFavorite), |
| 317 | onMove: useStableCallback(handleMove), |
| 318 | onSetHidden: useStableCallback(setHidden), |
| 319 | onToggleEdit: handleToggleEdit, |
| 320 | onRemove: useStableCallback(handleRemove), |
| 321 | }; |
| 322 | |
| 323 | const renderRow = (model: (typeof displayModels)[number]) => { |
| 324 | const group = groupOf(model); |
| 325 | const index = displayModels.indexOf(model); |
| 326 | const previousModel = displayModels[index - 1]; |
| 327 | const nextModel = displayModels[index + 1]; |
| 328 | return ( |
| 329 | <ProviderModelRow |
| 330 | key={`${instanceId}:${model.slug}`} |
| 331 | model={model} |
| 332 | isHidden={!model.isCustom && hiddenModelSet.has(model.slug)} |
| 333 | isFavorite={group === "favorite"} |
| 334 | canMoveUp={!isFiltering && previousModel !== undefined && groupOf(previousModel) === group} |
| 335 | canMoveDown={!isFiltering && nextModel !== undefined && groupOf(nextModel) === group} |
| 336 | isFiltering={isFiltering} |
| 337 | flagMissingReasoning={flagMissingReasoning} |
| 338 | canWritePreferences={canWritePreferences} |
| 339 | canManageCustomModels={canManageCustomModels} |
| 340 | {...rowHandlers} |
| 341 | /> |
| 342 | ); |
| 343 | }; |
| 344 | |
| 345 | const groupLabel = (label: string, isFirst: boolean) => ( |
| 346 | <div className={cn("px-2 pb-1.5 text-2xs text-muted-foreground", isFirst ? "pt-1" : "pt-5")}> |
| 347 | {label} |
| 348 | </div> |
| 349 | ); |
| 350 | |
| 351 | return ( |
| 352 | <div className="lg:flex lg:h-full lg:min-h-0 lg:flex-col"> |
| 353 | <div className="flex flex-wrap items-center justify-between gap-2"> |
| 354 | {showFilter ? ( |
| 355 | <Input |
| 356 | value={filter} |
| 357 | onChange={(event) => setFilter(event.target.value)} |
| 358 | placeholder={gt("Filter models")} |
| 359 | size="sm" |
| 360 | className="w-56 max-w-full" |
| 361 | spellCheck={false} |
| 362 | aria-label={gt("Filter models")} |
| 363 | /> |
| 364 | ) : null} |
| 365 | <div className="flex items-center gap-2"> |
| 366 | {builtInModels.length > 0 ? ( |
| 367 | <Button |
| 368 | type="button" |
| 369 | size="xs" |
| 370 | variant="ghost-muted" |
| 371 | onClick={() => { |
| 372 | setHeldPlacements(new Map()); |
| 373 | onHiddenModelsChange( |
| 374 | nextHiddenModelsForBulkToggle(models, hiddenModels), |
| 375 | builtInModels.map((model) => model.slug), |
| 376 | ); |
| 377 | }} |
| 378 | > |
| 379 | {allBuiltInModelsHidden ? gt("Enable all") : gt("Disable all")} |
| 380 | </Button> |
| 381 | ) : null} |
| 382 | <span className="text-xs text-muted-foreground"> |
| 383 | {gt("{count, plural, one {{count} model} other {{count} models}}", { |
| 384 | count: models.length, |
| 385 | })} |
| 386 | {favoriteCount > 0 |
| 387 | ? ` · ${gt("{count, plural, one {{count} favorite} other {{count} favorites}}", { |
| 388 | count: favoriteCount, |
| 389 | })}` |
| 390 | : ""} |
| 391 | {hiddenCount > 0 |
| 392 | ? ` · ${gt("{count, plural, other {{count} hidden}}", { count: hiddenCount })}` |
| 393 | : ""} |
| 394 | </span> |
| 395 | </div> |
| 396 | {driverKind !== "antigravity" && !isAdding ? ( |
| 397 | <Button |
| 398 | type="button" |
| 399 | size="xs" |
| 400 | variant="ghost-muted" |
| 401 | // The ghost padding would inset the label from the column edge the toggles align to. |
| 402 | className="-me-2 ml-auto" |
| 403 | onClick={() => setIsAdding(true)} |
| 404 | > |
| 405 | <PlusIcon className="size-3" /> |
| 406 | <T>Add custom model</T> |
| 407 | </Button> |
| 408 | ) : null} |
| 409 | </div> |
| 410 | <div |
| 411 | ref={listRef} |
| 412 | className="mt-2 -mx-2 max-h-64 overflow-y-auto lg:max-h-none lg:min-h-0 lg:flex-1" |
| 413 | > |
| 414 | {visibleModels.length === 0 ? ( |
| 415 | <p className="px-2 py-2 text-xs text-muted-foreground"> |
| 416 | {isFiltering ? gt("No models match.") : gt("No models reported for this provider yet.")} |
| 417 | </p> |
| 418 | ) : null} |
| 419 | {visibleModels.map((model, index) => { |
| 420 | const group = groupOf(model); |
| 421 | const previous = visibleModels[index - 1]; |
| 422 | const startsGroup = previous === undefined || groupOf(previous) !== group; |
| 423 | const editingEntry = |
| 424 | canManageCustomModels && model.isCustom && editingSlug === model.slug |
| 425 | ? customModels.find((entry) => entry.slug === model.slug) |
| 426 | : undefined; |
| 427 | return ( |
| 428 | <div key={`${instanceId}:${model.slug}:group`}> |
| 429 | {startsGroup && favoriteCount > 0 && group === "favorite" |
| 430 | ? groupLabel(gt("Favorites"), index === 0) |
| 431 | : null} |
| 432 | {startsGroup && favoriteCount > 0 && group === "visible" |
| 433 | ? groupLabel(gt("All", { $context: "group of all visible models" }), index === 0) |
| 434 | : null} |
| 435 | {startsGroup && group === "hidden" |
| 436 | ? groupLabel(gt("Hidden from picker"), index === 0) |
| 437 | : null} |
| 438 | {renderRow(model)} |
| 439 | {editingEntry ? ( |
| 440 | <CustomModelEditor |
| 441 | key={`${instanceId}:${model.slug}:editor`} |
| 442 | instanceId={instanceId} |
| 443 | driverKind={driverKind} |
| 444 | entry={editingEntry} |
| 445 | builtInModels={builtInModels} |
| 446 | onSave={(next) => void handleSaveEdit(next)} |
| 447 | onDraftChange={() => { |
| 448 | editAttemptRef.current += 1; |
| 449 | }} |
| 450 | onCancel={() => { |
| 451 | editAttemptRef.current += 1; |
| 452 | setEditingSlug(null); |
| 453 | }} |
| 454 | /> |
| 455 | ) : null} |
| 456 | </div> |
| 457 | ); |
| 458 | })} |
| 459 | </div> |
| 460 | |
| 461 | {driverKind === "antigravity" || !canManageCustomModels ? null : isAdding ? ( |
| 462 | <div className="mt-3 flex flex-col gap-2 sm:flex-row"> |
| 463 | <Input |
| 464 | id={`provider-instance-${instanceId}-custom-model`} |
| 465 | size="sm" |
| 466 | autoFocus |
| 467 | readOnly={isSaving} |
| 468 | value={input} |
| 469 | onChange={(event) => { |
| 470 | setInput(event.target.value); |
| 471 | if (error) setError(null); |
| 472 | }} |
| 473 | onKeyDown={(event) => { |
| 474 | if (event.key === "Escape") { |
| 475 | event.preventDefault(); |
| 476 | cancelAdd(); |
| 477 | return; |
| 478 | } |
| 479 | if (event.key !== "Enter") return; |
| 480 | event.preventDefault(); |
| 481 | void handleAdd(); |
| 482 | }} |
| 483 | placeholder={driverKind ? CUSTOM_MODEL_PLACEHOLDER_BY_KIND[driverKind] : "model-slug"} |
| 484 | spellCheck={false} |
| 485 | /> |
| 486 | <div className="flex shrink-0 gap-2"> |
| 487 | <Button size="sm" variant="outline" disabled={isSaving} onClick={handleAdd}> |
| 488 | {gt("Add", { $context: "verb, adds a custom model" })} |
| 489 | </Button> |
| 490 | <Button size="sm" variant="ghost" onClick={cancelAdd}> |
| 491 | <T>Cancel</T> |
| 492 | </Button> |
| 493 | </div> |
| 494 | </div> |
| 495 | ) : null} |
| 496 | |
| 497 | {driverKind !== "antigravity" && error ? ( |
| 498 | <p className="mt-2 text-xs text-destructive">{error}</p> |
| 499 | ) : null} |
| 500 | </div> |
| 501 | ); |
| 502 | } |
| 503 | |
| 504 | const ProviderModelRow = memo(function ProviderModelRow({ |
| 505 | model, |
| 506 | isHidden, |
| 507 | isFavorite, |
| 508 | canMoveUp, |
| 509 | canMoveDown, |
| 510 | isFiltering, |
| 511 | flagMissingReasoning, |
| 512 | canWritePreferences, |
| 513 | canManageCustomModels, |
| 514 | onToggleFavorite, |
| 515 | onMove, |
| 516 | onSetHidden, |
| 517 | onToggleEdit, |
| 518 | onRemove, |
| 519 | }: { |
| 520 | readonly model: ServerProviderModel; |
| 521 | readonly isHidden: boolean; |
| 522 | readonly isFavorite: boolean; |
| 523 | readonly canMoveUp: boolean; |
| 524 | readonly canMoveDown: boolean; |
| 525 | readonly isFiltering: boolean; |
| 526 | readonly flagMissingReasoning: boolean; |
| 527 | readonly canWritePreferences: boolean; |
| 528 | readonly canManageCustomModels: boolean; |
| 529 | readonly onToggleFavorite: (slug: string) => void; |
| 530 | readonly onMove: (slug: string, direction: -1 | 1) => void; |
| 531 | readonly onSetHidden: (slug: string, hidden: boolean) => void; |
| 532 | readonly onToggleEdit: (slug: string) => void; |
| 533 | readonly onRemove: (slug: string) => void; |
| 534 | }) { |
| 535 | const gt = useGT(); |
| 536 | const m = useMessages(); |
| 537 | const capLabels = describeModelCapabilities(model, flagMissingReasoning, m); |
| 538 | const nameClassName = cn("text-xs", isHidden ? "text-muted-foreground" : "text-foreground/90"); |
| 539 | const pickerTooltip = model.isCustom |
| 540 | ? gt("Custom models are always shown in the picker") |
| 541 | : isHidden |
| 542 | ? gt("Hidden from picker") |
| 543 | : gt("Shown in picker"); |
| 544 | |
| 545 | return ( |
| 546 | <div |
| 547 | data-model-slug={model.slug} |
| 548 | className={cn( |
| 549 | "grid h-7 grid-cols-[1.5rem_minmax(0,1fr)_auto_minmax(5.5rem,auto)_auto] items-center gap-2 rounded-md px-2 transition-colors hover:bg-muted/30", |
| 550 | isHidden && "opacity-50", |
| 551 | )} |
| 552 | > |
| 553 | <Tooltip> |
| 554 | <TooltipTrigger |
| 555 | render={ |
| 556 | <Button |
| 557 | size="icon-micro" |
| 558 | variant="ghost-muted" |
| 559 | disabled={!canWritePreferences} |
| 560 | onClick={() => onToggleFavorite(model.slug)} |
| 561 | aria-label={ |
| 562 | isFavorite |
| 563 | ? gt("Remove {name} from favorites", { name: model.name }) |
| 564 | : gt("Add {name} to favorites", { name: model.name }) |
| 565 | } |
| 566 | /> |
| 567 | } |
| 568 | > |
| 569 | <StarIcon className={cn("size-3", isFavorite && "fill-current text-warning")} /> |
| 570 | </TooltipTrigger> |
| 571 | <TooltipPopup side="top"> |
| 572 | {isFavorite ? gt("Remove from favorites") : gt("Add to favorites")} |
| 573 | </TooltipPopup> |
| 574 | </Tooltip> |
| 575 | <span className="flex min-w-0 items-baseline gap-2"> |
| 576 | <Tooltip> |
| 577 | <TooltipTrigger |
| 578 | render={ |
| 579 | <span tabIndex={0} className="flex min-w-0 items-baseline gap-2 overflow-hidden" /> |
| 580 | } |
| 581 | > |
| 582 | <span className={cn(nameClassName, "max-w-full shrink-0 truncate")}>{model.name}</span> |
| 583 | {model.name !== model.slug ? ( |
| 584 | <code className="truncate font-mono text-2xs text-muted-foreground/70"> |
| 585 | {model.slug} |
| 586 | </code> |
| 587 | ) : null} |
| 588 | </TooltipTrigger> |
| 589 | <TooltipPopup side="top" align="start"> |
| 590 | <div>{model.name}</div> |
| 591 | {model.name !== model.slug ? ( |
| 592 | <code className="font-mono text-2xs text-muted-foreground">{model.slug}</code> |
| 593 | ) : null} |
| 594 | </TooltipPopup> |
| 595 | </Tooltip> |
| 596 | {model.isCustom ? ( |
| 597 | <span className="shrink-0 text-2xs text-muted-foreground/70"> |
| 598 | {gt("custom", { $context: "badge, user-added model" })} |
| 599 | </span> |
| 600 | ) : null} |
| 601 | </span> |
| 602 | <span className="text-2xs text-muted-foreground/70"> |
| 603 | {capLabels.length > 0 ? ( |
| 604 | <span className="hidden sm:inline">{capLabels.join(" · ")}</span> |
| 605 | ) : null} |
| 606 | </span> |
| 607 | <span className="flex shrink-0 items-center justify-end gap-0.5"> |
| 608 | {!isHidden && !isFiltering ? ( |
| 609 | <> |
| 610 | <Tooltip> |
| 611 | <TooltipTrigger |
| 612 | render={ |
| 613 | <Button |
| 614 | size="icon-micro" |
| 615 | variant="ghost-muted" |
| 616 | disabled={!canWritePreferences || !canMoveUp} |
| 617 | onClick={() => onMove(model.slug, -1)} |
| 618 | aria-label={gt("Move {name} up", { name: model.name })} |
| 619 | /> |
| 620 | } |
| 621 | > |
| 622 | <ArrowUpIcon className="size-3" /> |
| 623 | </TooltipTrigger> |
| 624 | <TooltipPopup side="top"> |
| 625 | <T>Move up</T> |
| 626 | </TooltipPopup> |
| 627 | </Tooltip> |
| 628 | <Tooltip> |
| 629 | <TooltipTrigger |
| 630 | render={ |
| 631 | <Button |
| 632 | size="icon-micro" |
| 633 | variant="ghost-muted" |
| 634 | disabled={!canWritePreferences || !canMoveDown} |
| 635 | onClick={() => onMove(model.slug, 1)} |
| 636 | aria-label={gt("Move {name} down", { name: model.name })} |
| 637 | /> |
| 638 | } |
| 639 | > |
| 640 | <ArrowDownIcon className="size-3" /> |
| 641 | </TooltipTrigger> |
| 642 | <TooltipPopup side="top"> |
| 643 | <T>Move down</T> |
| 644 | </TooltipPopup> |
| 645 | </Tooltip> |
| 646 | </> |
| 647 | ) : null} |
| 648 | {model.isCustom ? ( |
| 649 | <> |
| 650 | <Tooltip> |
| 651 | <TooltipTrigger |
| 652 | render={ |
| 653 | <Button |
| 654 | size="icon-micro" |
| 655 | variant="ghost-muted" |
| 656 | disabled={!canManageCustomModels} |
| 657 | aria-label={gt("Edit {name}", { name: model.slug })} |
| 658 | onClick={() => onToggleEdit(model.slug)} |
| 659 | /> |
| 660 | } |
| 661 | > |
| 662 | <PencilIcon className="size-3" /> |
| 663 | </TooltipTrigger> |
| 664 | <TooltipPopup side="top"> |
| 665 | <T>Edit name and options</T> |
| 666 | </TooltipPopup> |
| 667 | </Tooltip> |
| 668 | <Tooltip> |
| 669 | <TooltipTrigger |
| 670 | render={ |
| 671 | <Button |
| 672 | size="icon-micro" |
| 673 | variant="ghost-muted" |
| 674 | disabled={!canManageCustomModels} |
| 675 | aria-label={gt("Remove {name}", { name: model.slug })} |
| 676 | onClick={() => onRemove(model.slug)} |
| 677 | /> |
| 678 | } |
| 679 | > |
| 680 | <XIcon className="size-3" /> |
| 681 | </TooltipTrigger> |
| 682 | <TooltipPopup side="top"> |
| 683 | <T>Remove custom model</T> |
| 684 | </TooltipPopup> |
| 685 | </Tooltip> |
| 686 | </> |
| 687 | ) : null} |
| 688 | </span> |
| 689 | {/* Disabled switches receive no pointer events; wrap the tooltip trigger. */} |
| 690 | <Tooltip> |
| 691 | <TooltipTrigger render={<span className="flex shrink-0 items-center" />}> |
| 692 | <Switch |
| 693 | size="sm" |
| 694 | checked={!isHidden} |
| 695 | disabled={!canWritePreferences || model.isCustom} |
| 696 | onCheckedChange={(checked) => onSetHidden(model.slug, !checked)} |
| 697 | aria-label={gt("Show {name} in the model picker", { name: model.name })} |
| 698 | /> |
| 699 | </TooltipTrigger> |
| 700 | <TooltipPopup side="top">{pickerTooltip}</TooltipPopup> |
| 701 | </Tooltip> |
| 702 | </div> |
| 703 | ); |
| 704 | }); |