The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { |
| 2 | ANTIGRAVITY_DEFAULT_MODEL, |
| 3 | DEFAULT_TEXT_GENERATION_MODEL, |
| 4 | DEFAULT_SERVER_SETTINGS, |
| 5 | ProjectId, |
| 6 | ProviderDriverKind, |
| 7 | ProviderInstanceId, |
| 8 | type ServerProvider, |
| 9 | } from "@t3tools/contracts"; |
| 10 | import { |
| 11 | DEFAULT_CLIENT_SETTINGS, |
| 12 | DEFAULT_UNIFIED_SETTINGS, |
| 13 | type UnifiedSettings, |
| 14 | } from "@t3tools/contracts/settings"; |
| 15 | import { describe, expect, it } from "vite-plus/test"; |
| 16 | import { createModelSelection } from "@t3tools/shared/model"; |
| 17 | import { deriveEffectiveComposerModelState } from "./composerDraftStore"; |
| 18 | import { getComposerProviderState } from "./components/chat/composerProviderState"; |
| 19 | import { resolveComposerProviderSelection } from "./components/ChatView.logic"; |
| 20 | import { |
| 21 | deriveProviderInstanceEntries, |
| 22 | NO_PROVIDER_MODEL_SELECTION, |
| 23 | providerInstanceEntriesFor, |
| 24 | } from "./providerInstances"; |
| 25 | import { |
| 26 | defaultModelSelectionsAreMixed, |
| 27 | getAppModelOptionsForInstance, |
| 28 | getCustomModelOptionsByInstance, |
| 29 | getTextGenerationModelOptionsByInstance, |
| 30 | resolveAppModelSelectionForInstance, |
| 31 | resolveAppModelSelectionState, |
| 32 | resolveEffectiveDefaultModelSelection, |
| 33 | } from "./modelSelection"; |
| 34 | |
| 35 | function provider(input: { |
| 36 | provider?: ProviderDriverKind; |
| 37 | instanceId: string; |
| 38 | models?: ReadonlyArray<string>; |
| 39 | supportsTextGeneration?: boolean; |
| 40 | }): ServerProvider { |
| 41 | const driver = |
| 42 | input.provider ?? |
| 43 | (input.instanceId.startsWith("claude_") |
| 44 | ? ProviderDriverKind.make("claudeAgent") |
| 45 | : ProviderDriverKind.make("codex")); |
| 46 | return { |
| 47 | instanceId: ProviderInstanceId.make(input.instanceId), |
| 48 | driver, |
| 49 | ...(input.supportsTextGeneration === undefined |
| 50 | ? {} |
| 51 | : { supportsTextGeneration: input.supportsTextGeneration }), |
| 52 | enabled: true, |
| 53 | installed: true, |
| 54 | version: null, |
| 55 | status: "ready", |
| 56 | auth: { status: "authenticated" }, |
| 57 | checkedAt: "2026-01-01T00:00:00.000Z", |
| 58 | models: (input.models ?? []).map((slug) => ({ |
| 59 | slug, |
| 60 | name: slug, |
| 61 | isCustom: false, |
| 62 | capabilities: {}, |
| 63 | })), |
| 64 | slashCommands: [], |
| 65 | skills: [], |
| 66 | }; |
| 67 | } |
| 68 | |
| 69 | function settingsWithProviderInstances(): UnifiedSettings { |
| 70 | return { |
| 71 | ...DEFAULT_UNIFIED_SETTINGS, |
| 72 | providerInstances: { |
| 73 | [ProviderInstanceId.make("claudeAgent")]: { |
| 74 | driver: ProviderDriverKind.make("claudeAgent"), |
| 75 | config: { customModels: [] }, |
| 76 | }, |
| 77 | [ProviderInstanceId.make("claude_openrouter")]: { |
| 78 | driver: ProviderDriverKind.make("claudeAgent"), |
| 79 | config: { customModels: ["openai/gpt-5.5"] }, |
| 80 | }, |
| 81 | }, |
| 82 | }; |
| 83 | } |
| 84 | |
| 85 | describe("scoped default model selections", () => { |
| 86 | // regression: #27997 equal automatic defaults can resolve to different providers |
| 87 | it("compares automatic defaults using each environment's models", () => { |
| 88 | const codex = { |
| 89 | projectId: null, |
| 90 | settings: DEFAULT_UNIFIED_SETTINGS, |
| 91 | providers: [provider({ instanceId: "codex", models: ["gpt-6-astra"] })], |
| 92 | }; |
| 93 | const claude = { |
| 94 | projectId: null, |
| 95 | settings: DEFAULT_UNIFIED_SETTINGS, |
| 96 | providers: [ |
| 97 | provider({ |
| 98 | instanceId: "claudeAgent", |
| 99 | provider: ProviderDriverKind.make("claudeAgent"), |
| 100 | models: ["claude-sonnet-4-6"], |
| 101 | }), |
| 102 | ], |
| 103 | }; |
| 104 | |
| 105 | expect(defaultModelSelectionsAreMixed(DEFAULT_CLIENT_SETTINGS, [codex, claude])).toBe(true); |
| 106 | expect(defaultModelSelectionsAreMixed(DEFAULT_CLIENT_SETTINGS, [claude, codex])).toBe(true); |
| 107 | expect(defaultModelSelectionsAreMixed(DEFAULT_CLIENT_SETTINGS, [codex, codex])).toBe(false); |
| 108 | expect(defaultModelSelectionsAreMixed(DEFAULT_CLIENT_SETTINGS, [claude])).toBe(false); |
| 109 | expect(defaultModelSelectionsAreMixed(DEFAULT_CLIENT_SETTINGS, [])).toBe(false); |
| 110 | expect( |
| 111 | defaultModelSelectionsAreMixed(DEFAULT_CLIENT_SETTINGS, [codex, { ...codex, providers: [] }]), |
| 112 | ).toBe(true); |
| 113 | expect( |
| 114 | defaultModelSelectionsAreMixed(DEFAULT_CLIENT_SETTINGS, [ |
| 115 | codex, |
| 116 | { ...codex, providers: [provider({ instanceId: "codex", models: ["gpt-6-sol"] })] }, |
| 117 | ]), |
| 118 | ).toBe(true); |
| 119 | |
| 120 | const projectId = ProjectId.make("claude-only"); |
| 121 | const project = { |
| 122 | projectId, |
| 123 | settings: { |
| 124 | ...DEFAULT_SERVER_SETTINGS, |
| 125 | projectSettingsOverrides: { |
| 126 | [projectId]: { |
| 127 | providerInstances: { [ProviderInstanceId.make("codex")]: { enabled: false } }, |
| 128 | }, |
| 129 | }, |
| 130 | }, |
| 131 | providers: [...codex.providers, ...claude.providers], |
| 132 | }; |
| 133 | const remoteProject = { ...claude, projectId: ProjectId.make("remote-project") }; |
| 134 | expect(defaultModelSelectionsAreMixed(DEFAULT_CLIENT_SETTINGS, [project, remoteProject])).toBe( |
| 135 | false, |
| 136 | ); |
| 137 | expect(defaultModelSelectionsAreMixed(DEFAULT_CLIENT_SETTINGS, [remoteProject, project])).toBe( |
| 138 | false, |
| 139 | ); |
| 140 | expect( |
| 141 | defaultModelSelectionsAreMixed(DEFAULT_CLIENT_SETTINGS, [ |
| 142 | project, |
| 143 | { ...project, projectId: ProjectId.make("other-project") }, |
| 144 | ]), |
| 145 | ).toBe(true); |
| 146 | |
| 147 | const configured = { |
| 148 | projectId: null, |
| 149 | settings: { |
| 150 | ...DEFAULT_SERVER_SETTINGS, |
| 151 | providerModelPreferences: { |
| 152 | [ProviderInstanceId.make("codex")]: { |
| 153 | hiddenModels: ["gpt-6-astra"], |
| 154 | modelOrder: [], |
| 155 | }, |
| 156 | }, |
| 157 | }, |
| 158 | providers: [provider({ instanceId: "codex", models: ["gpt-6-astra", "gpt-6-sol"] })], |
| 159 | }; |
| 160 | const inherited = { ...configured, settings: DEFAULT_SERVER_SETTINGS }; |
| 161 | expect(defaultModelSelectionsAreMixed(DEFAULT_CLIENT_SETTINGS, [configured, inherited])).toBe( |
| 162 | true, |
| 163 | ); |
| 164 | expect(defaultModelSelectionsAreMixed(DEFAULT_CLIENT_SETTINGS, [inherited, configured])).toBe( |
| 165 | true, |
| 166 | ); |
| 167 | const clientSettings = { |
| 168 | ...DEFAULT_CLIENT_SETTINGS, |
| 169 | providerModelPreferences: configured.settings.providerModelPreferences, |
| 170 | }; |
| 171 | expect(defaultModelSelectionsAreMixed(clientSettings, [configured, inherited])).toBe(false); |
| 172 | expect(defaultModelSelectionsAreMixed(clientSettings, [inherited, configured])).toBe(false); |
| 173 | expect( |
| 174 | defaultModelSelectionsAreMixed(clientSettings, [ |
| 175 | { ...inherited, settings: { ...DEFAULT_SERVER_SETTINGS, providerModelPreferences: {} } }, |
| 176 | inherited, |
| 177 | ]), |
| 178 | ).toBe(true); |
| 179 | }); |
| 180 | |
| 181 | it("keeps automatic, explicit, and different saved options distinct", () => { |
| 182 | const providers = [provider({ instanceId: "codex", models: ["gpt-6-astra"] })]; |
| 183 | const selected = { |
| 184 | projectId: null, |
| 185 | providers, |
| 186 | settings: { |
| 187 | ...DEFAULT_UNIFIED_SETTINGS, |
| 188 | defaultModelSelection: createModelSelection( |
| 189 | ProviderInstanceId.make("codex"), |
| 190 | "gpt-6-astra", |
| 191 | ), |
| 192 | }, |
| 193 | }; |
| 194 | expect( |
| 195 | defaultModelSelectionsAreMixed(DEFAULT_CLIENT_SETTINGS, [selected, { ...selected }]), |
| 196 | ).toBe(false); |
| 197 | expect( |
| 198 | defaultModelSelectionsAreMixed(DEFAULT_CLIENT_SETTINGS, [ |
| 199 | selected, |
| 200 | { projectId: null, providers, settings: DEFAULT_UNIFIED_SETTINGS }, |
| 201 | ]), |
| 202 | ).toBe(true); |
| 203 | expect( |
| 204 | defaultModelSelectionsAreMixed(DEFAULT_CLIENT_SETTINGS, [ |
| 205 | selected, |
| 206 | { |
| 207 | ...selected, |
| 208 | settings: { |
| 209 | ...selected.settings, |
| 210 | defaultModelSelection: { |
| 211 | ...selected.settings.defaultModelSelection, |
| 212 | options: [{ id: "reasoningEffort", value: "xhigh" }], |
| 213 | }, |
| 214 | }, |
| 215 | }, |
| 216 | ]), |
| 217 | ).toBe(true); |
| 218 | }); |
| 219 | }); |
| 220 | |
| 221 | describe("instance-scoped model selection", () => { |
| 222 | it("keeps custom models on the provider instance that declared them", () => { |
| 223 | const providers = [ |
| 224 | provider({ |
| 225 | instanceId: "claudeAgent", |
| 226 | models: ["claude-sonnet-4-6"], |
| 227 | }), |
| 228 | provider({ |
| 229 | instanceId: "claude_openrouter", |
| 230 | models: ["claude-sonnet-4-6"], |
| 231 | }), |
| 232 | ]; |
| 233 | const entries = deriveProviderInstanceEntries(providers); |
| 234 | const stock = entries.find((entry) => entry.instanceId === "claudeAgent")!; |
| 235 | const openrouter = entries.find((entry) => entry.instanceId === "claude_openrouter")!; |
| 236 | |
| 237 | expect( |
| 238 | getAppModelOptionsForInstance(settingsWithProviderInstances(), stock).map( |
| 239 | (option) => option.slug, |
| 240 | ), |
| 241 | ).not.toContain("openai/gpt-5.5"); |
| 242 | expect( |
| 243 | getAppModelOptionsForInstance(settingsWithProviderInstances(), openrouter).map( |
| 244 | (option) => option.slug, |
| 245 | ), |
| 246 | ).toContain("openai/gpt-5.5"); |
| 247 | }); |
| 248 | |
| 249 | it("preserves a custom slug that collides with a provider alias", () => { |
| 250 | const providers = [ |
| 251 | provider({ |
| 252 | provider: ProviderDriverKind.make("claudeAgent"), |
| 253 | instanceId: "claude_openrouter", |
| 254 | models: ["claude-opus-4-8"], |
| 255 | }), |
| 256 | ]; |
| 257 | const settings: UnifiedSettings = { |
| 258 | ...settingsWithProviderInstances(), |
| 259 | providerInstances: { |
| 260 | ...settingsWithProviderInstances().providerInstances, |
| 261 | [ProviderInstanceId.make("claude_openrouter")]: { |
| 262 | driver: ProviderDriverKind.make("claudeAgent"), |
| 263 | config: { customModels: ["opus"] }, |
| 264 | }, |
| 265 | }, |
| 266 | }; |
| 267 | const openrouter = deriveProviderInstanceEntries(providers)[0]!; |
| 268 | |
| 269 | expect( |
| 270 | getAppModelOptionsForInstance(settings, openrouter).map((option) => option.slug), |
| 271 | ).toEqual(["claude-opus-4-8", "opus"]); |
| 272 | expect( |
| 273 | resolveAppModelSelectionForInstance( |
| 274 | ProviderInstanceId.make("claude_openrouter"), |
| 275 | settings, |
| 276 | providers, |
| 277 | "opus", |
| 278 | ), |
| 279 | ).toBe("opus"); |
| 280 | }); |
| 281 | |
| 282 | it("offers and resolves every saved custom model, past the first 32", () => { |
| 283 | const providers = [ |
| 284 | provider({ |
| 285 | provider: ProviderDriverKind.make("claudeAgent"), |
| 286 | instanceId: "claude_openrouter", |
| 287 | }), |
| 288 | ]; |
| 289 | const customModels = Array.from({ length: 40 }, (_, index) => `proxy/model-${index + 1}`); |
| 290 | const settings: UnifiedSettings = { |
| 291 | ...settingsWithProviderInstances(), |
| 292 | providerInstances: { |
| 293 | ...settingsWithProviderInstances().providerInstances, |
| 294 | [ProviderInstanceId.make("claude_openrouter")]: { |
| 295 | driver: ProviderDriverKind.make("claudeAgent"), |
| 296 | config: { customModels }, |
| 297 | }, |
| 298 | }, |
| 299 | }; |
| 300 | const openrouter = deriveProviderInstanceEntries(providers)[0]!; |
| 301 | |
| 302 | expect( |
| 303 | getAppModelOptionsForInstance(settings, openrouter).map((option) => option.slug), |
| 304 | ).toEqual(customModels); |
| 305 | expect( |
| 306 | resolveAppModelSelectionForInstance( |
| 307 | ProviderInstanceId.make("claude_openrouter"), |
| 308 | settings, |
| 309 | providers, |
| 310 | "proxy/model-40", |
| 311 | ), |
| 312 | ).toBe("proxy/model-40"); |
| 313 | }); |
| 314 | |
| 315 | it("drops server-reported custom rows that are no longer in settings", () => { |
| 316 | const baseProvider = provider({ |
| 317 | instanceId: "claude_openrouter", |
| 318 | models: ["claude-sonnet-4-6"], |
| 319 | }); |
| 320 | const providers = [ |
| 321 | { |
| 322 | ...baseProvider, |
| 323 | models: [ |
| 324 | ...baseProvider.models, |
| 325 | { slug: "removed/custom", name: "removed/custom", isCustom: true, capabilities: {} }, |
| 326 | ], |
| 327 | }, |
| 328 | ]; |
| 329 | const openrouter = deriveProviderInstanceEntries(providers)[0]!; |
| 330 | |
| 331 | expect( |
| 332 | getAppModelOptionsForInstance(settingsWithProviderInstances(), openrouter).map( |
| 333 | (option) => option.slug, |
| 334 | ), |
| 335 | ).toEqual(["claude-sonnet-4-6", "openai/gpt-5.5"]); |
| 336 | }); |
| 337 | |
| 338 | it("falls back when the selected model is hidden", () => { |
| 339 | const providers = [ |
| 340 | provider({ |
| 341 | instanceId: "claudeAgent", |
| 342 | models: ["claude-opus-4-6", "claude-sonnet-4-6"], |
| 343 | }), |
| 344 | ]; |
| 345 | const settings: UnifiedSettings = { |
| 346 | ...settingsWithProviderInstances(), |
| 347 | providerModelPreferences: { |
| 348 | [ProviderInstanceId.make("claudeAgent")]: { |
| 349 | hiddenModels: ["claude-opus-4-6"], |
| 350 | modelOrder: [], |
| 351 | }, |
| 352 | }, |
| 353 | }; |
| 354 | |
| 355 | expect( |
| 356 | resolveAppModelSelectionForInstance( |
| 357 | ProviderInstanceId.make("claudeAgent"), |
| 358 | settings, |
| 359 | providers, |
| 360 | "claude-opus-4-6", |
| 361 | ), |
| 362 | ).toBe("claude-sonnet-4-6"); |
| 363 | expect( |
| 364 | resolveAppModelSelectionForInstance( |
| 365 | ProviderInstanceId.make("claudeAgent"), |
| 366 | settings, |
| 367 | providers, |
| 368 | "claude-opus-4-6", |
| 369 | { preserveUnavailableSelection: true }, |
| 370 | ), |
| 371 | ).toBe("claude-sonnet-4-6"); |
| 372 | }); |
| 373 | |
| 374 | it("falls back instead of resolving a custom slug against the wrong instance", () => { |
| 375 | const providers = [ |
| 376 | provider({ |
| 377 | instanceId: "claudeAgent", |
| 378 | models: ["claude-sonnet-4-6"], |
| 379 | }), |
| 380 | provider({ |
| 381 | instanceId: "claude_openrouter", |
| 382 | models: ["claude-sonnet-4-6"], |
| 383 | }), |
| 384 | ]; |
| 385 | |
| 386 | expect( |
| 387 | resolveAppModelSelectionForInstance( |
| 388 | ProviderInstanceId.make("claudeAgent"), |
| 389 | settingsWithProviderInstances(), |
| 390 | providers, |
| 391 | "openai/gpt-5.5", |
| 392 | ), |
| 393 | ).toBe("claude-sonnet-4-6"); |
| 394 | }); |
| 395 | |
| 396 | describe.each([ |
| 397 | { |
| 398 | driverName: "pi", |
| 399 | availableModel: "default", |
| 400 | missingModel: "anthropic/claude-sonnet-4", |
| 401 | }, |
| 402 | { |
| 403 | driverName: "acpRegistry", |
| 404 | availableModel: "default", |
| 405 | missingModel: "gateway/chosen-model", |
| 406 | }, |
| 407 | { |
| 408 | driverName: "opencode", |
| 409 | availableModel: "opencode/big-pickle", |
| 410 | missingModel: "opencode/kimi-k3", |
| 411 | }, |
| 412 | { |
| 413 | driverName: "antigravity", |
| 414 | availableModel: "gemini-3.1-pro", |
| 415 | missingModel: "gemini-3.1-pro-high", |
| 416 | }, |
| 417 | { |
| 418 | driverName: "cursor", |
| 419 | availableModel: "auto-smart", |
| 420 | missingModel: "claude-opus-5-5", |
| 421 | }, |
| 422 | ])("$driverName catalog gaps", ({ driverName, availableModel, missingModel }) => { |
| 423 | it("preserves a selected model when a catalog refresh no longer contains it", () => { |
| 424 | const providers = [ |
| 425 | provider({ |
| 426 | provider: ProviderDriverKind.make(driverName), |
| 427 | instanceId: driverName, |
| 428 | models: [availableModel], |
| 429 | }), |
| 430 | ]; |
| 431 | |
| 432 | expect( |
| 433 | resolveAppModelSelectionForInstance( |
| 434 | ProviderInstanceId.make(driverName), |
| 435 | settingsWithProviderInstances(), |
| 436 | providers, |
| 437 | missingModel, |
| 438 | { preserveUnavailableSelection: true }, |
| 439 | ), |
| 440 | ).toBe(missingModel); |
| 441 | expect( |
| 442 | resolveAppModelSelectionForInstance( |
| 443 | ProviderInstanceId.make(driverName), |
| 444 | settingsWithProviderInstances(), |
| 445 | providers, |
| 446 | missingModel, |
| 447 | ), |
| 448 | ).toBe(availableModel); |
| 449 | }); |
| 450 | |
| 451 | it("adds the selected missing model as an unavailable option", () => { |
| 452 | const providers = [ |
| 453 | provider({ |
| 454 | provider: ProviderDriverKind.make(driverName), |
| 455 | instanceId: driverName, |
| 456 | models: [availableModel], |
| 457 | }), |
| 458 | ]; |
| 459 | const entry = deriveProviderInstanceEntries(providers)[0]!; |
| 460 | |
| 461 | expect( |
| 462 | getAppModelOptionsForInstance(settingsWithProviderInstances(), entry, missingModel), |
| 463 | ).toEqual([ |
| 464 | expect.objectContaining({ slug: availableModel }), |
| 465 | expect.objectContaining({ |
| 466 | slug: missingModel, |
| 467 | name: missingModel, |
| 468 | isUnavailable: true, |
| 469 | }), |
| 470 | ]); |
| 471 | }); |
| 472 | |
| 473 | it("does not resurrect a hidden model when the raw catalog omits it", () => { |
| 474 | const instanceId = ProviderInstanceId.make(driverName); |
| 475 | const driver = ProviderDriverKind.make(driverName); |
| 476 | const settings: UnifiedSettings = { |
| 477 | ...settingsWithProviderInstances(), |
| 478 | providerModelPreferences: { |
| 479 | [instanceId]: { |
| 480 | hiddenModels: [missingModel], |
| 481 | modelOrder: [], |
| 482 | }, |
| 483 | }, |
| 484 | }; |
| 485 | const providers = [provider({ provider: driver, instanceId, models: [] })]; |
| 486 | const entry = deriveProviderInstanceEntries(providers)[0]!; |
| 487 | |
| 488 | expect(getAppModelOptionsForInstance(settings, entry, missingModel)).toEqual([]); |
| 489 | expect( |
| 490 | resolveAppModelSelectionForInstance(instanceId, settings, providers, missingModel, { |
| 491 | preserveUnavailableSelection: true, |
| 492 | }), |
| 493 | ).toBeNull(); |
| 494 | }); |
| 495 | }); |
| 496 | |
| 497 | it("falls back from an explicit non-OpenCode draft with a missing model", () => { |
| 498 | const instanceId = ProviderInstanceId.make("codex"); |
| 499 | const driver = ProviderDriverKind.make("codex"); |
| 500 | const providers = [provider({ provider: driver, instanceId, models: ["gpt-5.6-sol"] })]; |
| 501 | const state = deriveEffectiveComposerModelState({ |
| 502 | threadStarted: false, |
| 503 | draft: { |
| 504 | activeProvider: instanceId, |
| 505 | modelSelectionByProvider: { |
| 506 | [instanceId]: createModelSelection(instanceId, "gpt-missing", [ |
| 507 | { id: "effort", value: "max" }, |
| 508 | ]), |
| 509 | }, |
| 510 | }, |
| 511 | providers, |
| 512 | selectedProvider: driver, |
| 513 | selectedInstanceId: instanceId, |
| 514 | threadModelSelection: null, |
| 515 | projectModelSelection: null, |
| 516 | settings: settingsWithProviderInstances(), |
| 517 | }); |
| 518 | const dispatch = getComposerProviderState({ |
| 519 | provider: driver, |
| 520 | model: state.selectedModel, |
| 521 | models: providers[0]!.models, |
| 522 | modelOptions: state.modelOptions?.[instanceId], |
| 523 | }); |
| 524 | |
| 525 | expect(state.selectedModel).toBe("gpt-5.6-sol"); |
| 526 | expect(dispatch.modelOptionsForDispatch).toBeUndefined(); |
| 527 | }); |
| 528 | |
| 529 | it("sends a Cursor thread's stored model and options when the catalog lacks them", () => { |
| 530 | const instanceId = ProviderInstanceId.make("cursor"); |
| 531 | const driver = ProviderDriverKind.make("cursor"); |
| 532 | const stored = createModelSelection(instanceId, "claude-opus-5-5", [ |
| 533 | { id: "effort", value: "medium" }, |
| 534 | ]); |
| 535 | const providers = [provider({ provider: driver, instanceId, models: ["auto-smart"] })]; |
| 536 | const state = deriveEffectiveComposerModelState({ |
| 537 | threadStarted: false, |
| 538 | draft: null, |
| 539 | providers, |
| 540 | selectedProvider: driver, |
| 541 | selectedInstanceId: instanceId, |
| 542 | threadModelSelection: stored, |
| 543 | projectModelSelection: null, |
| 544 | settings: settingsWithProviderInstances(), |
| 545 | }); |
| 546 | const dispatch = getComposerProviderState({ |
| 547 | provider: driver, |
| 548 | model: state.selectedModel, |
| 549 | models: providers[0]!.models, |
| 550 | modelOptions: state.modelOptions?.[instanceId], |
| 551 | }); |
| 552 | |
| 553 | expect( |
| 554 | createModelSelection(instanceId, state.selectedModel, dispatch.modelOptionsForDispatch), |
| 555 | ).toEqual(stored); |
| 556 | }); |
| 557 | |
| 558 | it("preserves an explicit draft OpenCode selection while the catalog is empty", () => { |
| 559 | const instanceId = ProviderInstanceId.make("opencode_work"); |
| 560 | const driver = ProviderDriverKind.make("opencode"); |
| 561 | const draftSelection = createModelSelection(instanceId, "openrouter/kimi-k3", [ |
| 562 | { id: "variant", value: "max" }, |
| 563 | { id: "agent", value: "build" }, |
| 564 | ]); |
| 565 | const providers = [provider({ provider: driver, instanceId, models: [] })]; |
| 566 | const state = deriveEffectiveComposerModelState({ |
| 567 | threadStarted: false, |
| 568 | draft: { |
| 569 | activeProvider: instanceId, |
| 570 | modelSelectionByProvider: { [instanceId]: draftSelection }, |
| 571 | }, |
| 572 | providers, |
| 573 | selectedProvider: driver, |
| 574 | selectedInstanceId: instanceId, |
| 575 | threadModelSelection: null, |
| 576 | projectModelSelection: null, |
| 577 | settings: settingsWithProviderInstances(), |
| 578 | }); |
| 579 | |
| 580 | expect(state.selectedModel).toBe("openrouter/kimi-k3"); |
| 581 | expect(state.modelOptions?.[instanceId]).toEqual(draftSelection.options); |
| 582 | }); |
| 583 | |
| 584 | it("preserves the Antigravity model in drafts and existing threads after sign-out", () => { |
| 585 | const instanceId = ProviderInstanceId.make("antigravity_work"); |
| 586 | const driver = ProviderDriverKind.make("antigravity"); |
| 587 | const saved = createModelSelection(instanceId, "gemini-3.1-pro-high"); |
| 588 | const providers = [ |
| 589 | { |
| 590 | ...provider({ provider: driver, instanceId, models: [] }), |
| 591 | status: "error" as const, |
| 592 | auth: { status: "unauthenticated" as const }, |
| 593 | }, |
| 594 | ]; |
| 595 | for (const draft of [ |
| 596 | null, |
| 597 | { activeProvider: instanceId, modelSelectionByProvider: { [instanceId]: saved } }, |
| 598 | ]) { |
| 599 | const state = deriveEffectiveComposerModelState({ |
| 600 | threadStarted: false, |
| 601 | draft, |
| 602 | providers, |
| 603 | selectedProvider: driver, |
| 604 | selectedInstanceId: instanceId, |
| 605 | threadModelSelection: saved, |
| 606 | projectModelSelection: null, |
| 607 | settings: settingsWithProviderInstances(), |
| 608 | }); |
| 609 | expect(state.selectedModel).toBe(saved.model); |
| 610 | } |
| 611 | }); |
| 612 | |
| 613 | it("does not borrow a default model while a new Antigravity account has no catalog", () => { |
| 614 | const driver = ProviderDriverKind.make("antigravity"); |
| 615 | const instanceId = ProviderInstanceId.make("antigravity_work"); |
| 616 | const providers = [ |
| 617 | provider({ instanceId: "codex", models: ["gpt-5.6-sol"] }), |
| 618 | provider({ provider: driver, instanceId: "antigravity", models: ["gemini-other-account"] }), |
| 619 | provider({ provider: driver, instanceId, models: [] }), |
| 620 | ]; |
| 621 | |
| 622 | const otherAccountId = ProviderInstanceId.make("antigravity"); |
| 623 | for (const draft of [ |
| 624 | null, |
| 625 | { |
| 626 | activeProvider: instanceId, |
| 627 | modelSelectionByProvider: { |
| 628 | [otherAccountId]: createModelSelection(otherAccountId, "gemini-other-account"), |
| 629 | }, |
| 630 | }, |
| 631 | ]) { |
| 632 | const state = deriveEffectiveComposerModelState({ |
| 633 | threadStarted: false, |
| 634 | draft, |
| 635 | providers, |
| 636 | selectedProvider: driver, |
| 637 | selectedInstanceId: instanceId, |
| 638 | threadModelSelection: null, |
| 639 | projectModelSelection: createModelSelection( |
| 640 | ProviderInstanceId.make("codex"), |
| 641 | "gpt-5.6-sol", |
| 642 | ), |
| 643 | settings: settingsWithProviderInstances(), |
| 644 | }); |
| 645 | expect(state.selectedModel).toBe(""); |
| 646 | } |
| 647 | }); |
| 648 | |
| 649 | it("offers only account catalog models for Antigravity despite custom model settings", () => { |
| 650 | const driver = ProviderDriverKind.make("antigravity"); |
| 651 | const customId = ProviderInstanceId.make("antigravity_work"); |
| 652 | const nativeModel = "gemini-3.1-pro"; |
| 653 | const settings: UnifiedSettings = { |
| 654 | ...DEFAULT_UNIFIED_SETTINGS, |
| 655 | providerInstances: { |
| 656 | [ProviderInstanceId.make("antigravity")]: { |
| 657 | driver, |
| 658 | config: { customModels: ["api-only-model"] }, |
| 659 | }, |
| 660 | [customId]: { driver, config: { customModels: ["unknown-model"] } }, |
| 661 | }, |
| 662 | }; |
| 663 | const entries = deriveProviderInstanceEntries([ |
| 664 | provider({ provider: driver, instanceId: "antigravity", models: [nativeModel] }), |
| 665 | provider({ provider: driver, instanceId: customId, models: [nativeModel] }), |
| 666 | ]); |
| 667 | |
| 668 | for (const entry of entries) { |
| 669 | expect(getAppModelOptionsForInstance(settings, entry).map((model) => model.slug)).toEqual([ |
| 670 | nativeModel, |
| 671 | ]); |
| 672 | } |
| 673 | }); |
| 674 | |
| 675 | it.each([ |
| 676 | { driver: "antigravity", marker: ANTIGRAVITY_DEFAULT_MODEL, nativeModel: "gemini-3.1-pro" }, |
| 677 | { driver: "acpRegistry", marker: "default", nativeModel: "agent-model" }, |
| 678 | ])( |
| 679 | "resolves the $driver default marker without creating an unavailable model", |
| 680 | ({ driver, marker, nativeModel }) => { |
| 681 | const instanceId = ProviderInstanceId.make(`${driver}_work`); |
| 682 | const base = provider({ |
| 683 | provider: ProviderDriverKind.make(driver), |
| 684 | instanceId, |
| 685 | models: [nativeModel], |
| 686 | }); |
| 687 | const liveProvider = { |
| 688 | ...base, |
| 689 | models: base.models.map((model) => ({ |
| 690 | ...model, |
| 691 | isDefault: true, |
| 692 | ...(driver === "antigravity" ? { aliases: [marker] } : {}), |
| 693 | })), |
| 694 | }; |
| 695 | const settings = settingsWithProviderInstances(); |
| 696 | |
| 697 | expect( |
| 698 | getAppModelOptionsForInstance( |
| 699 | settings, |
| 700 | deriveProviderInstanceEntries([liveProvider])[0]!, |
| 701 | marker, |
| 702 | ).map((model) => model.slug), |
| 703 | ).toEqual([nativeModel]); |
| 704 | expect( |
| 705 | resolveAppModelSelectionForInstance(instanceId, settings, [liveProvider], marker, { |
| 706 | preserveUnavailableSelection: true, |
| 707 | }), |
| 708 | ).toBe(nativeModel); |
| 709 | |
| 710 | const hiddenSettings: UnifiedSettings = { |
| 711 | ...settings, |
| 712 | providerModelPreferences: { |
| 713 | [instanceId]: { hiddenModels: [nativeModel], modelOrder: [] }, |
| 714 | }, |
| 715 | }; |
| 716 | expect( |
| 717 | resolveAppModelSelectionForInstance(instanceId, hiddenSettings, [liveProvider], marker, { |
| 718 | preserveUnavailableSelection: true, |
| 719 | }), |
| 720 | ).toBeNull(); |
| 721 | expect( |
| 722 | getAppModelOptionsForInstance( |
| 723 | settings, |
| 724 | deriveProviderInstanceEntries([{ ...base, models: [] }])[0]!, |
| 725 | marker, |
| 726 | ), |
| 727 | ).toEqual([]); |
| 728 | }, |
| 729 | ); |
| 730 | |
| 731 | it("preserves saved options through dispatch when the model is absent from the catalog", () => { |
| 732 | const instanceId = ProviderInstanceId.make("opencode"); |
| 733 | const driver = ProviderDriverKind.make("opencode"); |
| 734 | const providers = [provider({ provider: driver, instanceId, models: ["opencode/big-pickle"] })]; |
| 735 | const saved = createModelSelection(instanceId, "opencode/kimi-k3", [ |
| 736 | { id: "variant", value: "max" }, |
| 737 | { id: "agent", value: "build" }, |
| 738 | ]); |
| 739 | const state = deriveEffectiveComposerModelState({ |
| 740 | threadStarted: false, |
| 741 | draft: null, |
| 742 | providers, |
| 743 | selectedProvider: driver, |
| 744 | selectedInstanceId: instanceId, |
| 745 | threadModelSelection: saved, |
| 746 | projectModelSelection: null, |
| 747 | settings: settingsWithProviderInstances(), |
| 748 | }); |
| 749 | const dispatch = getComposerProviderState({ |
| 750 | provider: driver, |
| 751 | model: state.selectedModel, |
| 752 | models: providers[0]!.models, |
| 753 | modelOptions: state.modelOptions?.[instanceId], |
| 754 | }); |
| 755 | |
| 756 | expect( |
| 757 | createModelSelection(instanceId, state.selectedModel, dispatch.modelOptionsForDispatch), |
| 758 | ).toEqual(saved); |
| 759 | }); |
| 760 | |
| 761 | describe("composer model option precedence", () => { |
| 762 | const instanceId = ProviderInstanceId.make("claudeAgent"); |
| 763 | const driver = ProviderDriverKind.make("claudeAgent"); |
| 764 | const base = provider({ provider: driver, instanceId, models: ["claude-opus-5-5"] }); |
| 765 | const providers = [ |
| 766 | { |
| 767 | ...base, |
| 768 | models: base.models.map((model) => ({ |
| 769 | ...model, |
| 770 | capabilities: { |
| 771 | optionDescriptors: [ |
| 772 | { |
| 773 | id: "effort", |
| 774 | label: "Effort", |
| 775 | type: "select" as const, |
| 776 | options: [ |
| 777 | { id: "medium", label: "Medium", isDefault: true }, |
| 778 | { id: "high", label: "High" }, |
| 779 | ], |
| 780 | currentValue: "medium", |
| 781 | }, |
| 782 | ], |
| 783 | }, |
| 784 | })), |
| 785 | }, |
| 786 | ]; |
| 787 | const settingsDefault = createModelSelection(instanceId, "claude-opus-5-5", [ |
| 788 | { id: "effort", value: "high" }, |
| 789 | ]); |
| 790 | const dispatchedSelection = ( |
| 791 | threadModelSelection: ReturnType<typeof createModelSelection> | null, |
| 792 | ) => { |
| 793 | const state = deriveEffectiveComposerModelState({ |
| 794 | threadStarted: false, |
| 795 | draft: null, |
| 796 | providers, |
| 797 | selectedProvider: driver, |
| 798 | selectedInstanceId: instanceId, |
| 799 | threadModelSelection, |
| 800 | projectModelSelection: settingsDefault, |
| 801 | settings: settingsWithProviderInstances(), |
| 802 | }); |
| 803 | const dispatch = getComposerProviderState({ |
| 804 | provider: driver, |
| 805 | model: state.selectedModel, |
| 806 | models: providers[0]!.models, |
| 807 | modelOptions: state.modelOptions?.[instanceId], |
| 808 | }); |
| 809 | return createModelSelection( |
| 810 | instanceId, |
| 811 | state.selectedModel, |
| 812 | dispatch.modelOptionsForDispatch, |
| 813 | ); |
| 814 | }; |
| 815 | |
| 816 | it("does not inherit settings options for an optionless existing thread", () => { |
| 817 | const threadSelection = createModelSelection(instanceId, "claude-opus-5-5"); |
| 818 | expect(dispatchedSelection(threadSelection)).toEqual(threadSelection); |
| 819 | }); |
| 820 | |
| 821 | it("keeps the options it was saved with", () => { |
| 822 | const threadSelection = createModelSelection(instanceId, "claude-opus-5-5", [ |
| 823 | { id: "effort", value: "medium" }, |
| 824 | ]); |
| 825 | expect(dispatchedSelection(threadSelection)).toEqual(threadSelection); |
| 826 | }); |
| 827 | |
| 828 | it("leaves the settings default's options in place before a thread exists", () => { |
| 829 | expect(dispatchedSelection(null)).toEqual(settingsDefault); |
| 830 | }); |
| 831 | }); |
| 832 | |
| 833 | it("keeps a custom-instance draft model while dropping unsupported options", () => { |
| 834 | const instanceId = ProviderInstanceId.make("claude_openrouter"); |
| 835 | const driver = ProviderDriverKind.make("claudeAgent"); |
| 836 | const providers = [ |
| 837 | provider({ provider: driver, instanceId: "claudeAgent", models: ["claude-opus-5"] }), |
| 838 | provider({ provider: driver, instanceId, models: ["claude-opus-5"] }), |
| 839 | ]; |
| 840 | const threadSelection = createModelSelection(instanceId, "claude-opus-5", [ |
| 841 | { id: "effort", value: "high" }, |
| 842 | ]); |
| 843 | const draftSelection = createModelSelection(instanceId, "openai/gpt-5.5", [ |
| 844 | { id: "effort", value: "max" }, |
| 845 | ]); |
| 846 | const state = deriveEffectiveComposerModelState({ |
| 847 | threadStarted: false, |
| 848 | draft: { |
| 849 | activeProvider: instanceId, |
| 850 | modelSelectionByProvider: { [instanceId]: draftSelection }, |
| 851 | }, |
| 852 | providers, |
| 853 | selectedProvider: driver, |
| 854 | selectedInstanceId: instanceId, |
| 855 | threadModelSelection: threadSelection, |
| 856 | projectModelSelection: null, |
| 857 | settings: settingsWithProviderInstances(), |
| 858 | }); |
| 859 | const dispatch = getComposerProviderState({ |
| 860 | provider: driver, |
| 861 | model: state.selectedModel, |
| 862 | models: providers[1]!.models, |
| 863 | modelOptions: state.modelOptions?.[instanceId], |
| 864 | }); |
| 865 | |
| 866 | expect( |
| 867 | createModelSelection(instanceId, state.selectedModel, dispatch.modelOptionsForDispatch), |
| 868 | ).toEqual(createModelSelection(instanceId, "openai/gpt-5.5")); |
| 869 | }); |
| 870 | |
| 871 | it("keeps a thread's stored options while the draft holds options for another provider", () => { |
| 872 | const codex = ProviderInstanceId.make("codex"); |
| 873 | const claude = ProviderInstanceId.make("claudeAgent"); |
| 874 | const providers = [ |
| 875 | provider({ instanceId: "codex", models: ["gpt-5.5", "gpt-5.4"] }), |
| 876 | provider({ |
| 877 | provider: ProviderDriverKind.make("claudeAgent"), |
| 878 | instanceId: "claudeAgent", |
| 879 | models: ["claude-opus-5-5"], |
| 880 | }), |
| 881 | ]; |
| 882 | const stored = createModelSelection(codex, "gpt-5.5", [ |
| 883 | { id: "reasoningEffort", value: "high" }, |
| 884 | { id: "fastMode", value: true }, |
| 885 | ]); |
| 886 | const claudePick = createModelSelection(claude, "claude-opus-5-5", [ |
| 887 | { id: "effort", value: "max" }, |
| 888 | ]); |
| 889 | const optionsFor = ( |
| 890 | draftSelections: Record<string, ReturnType<typeof createModelSelection>>, |
| 891 | threadModelSelection = stored, |
| 892 | ) => |
| 893 | deriveEffectiveComposerModelState({ |
| 894 | threadStarted: true, |
| 895 | draft: { |
| 896 | activeProvider: codex, |
| 897 | modelSelectionByProvider: draftSelections, |
| 898 | modelSelectionExplicit: true, |
| 899 | }, |
| 900 | providers, |
| 901 | selectedProvider: ProviderDriverKind.make("codex"), |
| 902 | selectedInstanceId: codex, |
| 903 | requestedInstanceId: claude, |
| 904 | threadModelSelection, |
| 905 | projectModelSelection: null, |
| 906 | settings: DEFAULT_UNIFIED_SETTINGS, |
| 907 | }).modelOptions; |
| 908 | |
| 909 | expect( |
| 910 | optionsFor({ [claude]: claudePick, [codex]: createModelSelection(codex, "gpt-5.5") }), |
| 911 | ).toEqual({ [claude]: claudePick.options, [codex]: stored.options }); |
| 912 | // Claude turned off after an unsent pick: the composer is back on the thread's instance. |
| 913 | expect(optionsFor({ [claude]: claudePick })?.[codex]).toEqual(stored.options); |
| 914 | const fastOff = createModelSelection(codex, "gpt-5.5", [ |
| 915 | { id: "reasoningEffort", value: "high" }, |
| 916 | { id: "fastMode", value: false }, |
| 917 | ]); |
| 918 | expect(optionsFor({ [claude]: claudePick, [codex]: fastOff })?.[codex]).toEqual( |
| 919 | fastOff.options, |
| 920 | ); |
| 921 | // A stored alias of the shown model still counts as that model. |
| 922 | const storedAlias = createModelSelection(codex, "gpt-5-codex", stored.options); |
| 923 | expect(optionsFor({ [claude]: claudePick }, storedAlias)?.[codex]).toEqual(stored.options); |
| 924 | const storedOtherModel = createModelSelection(codex, "gpt-5.4", stored.options); |
| 925 | expect( |
| 926 | optionsFor( |
| 927 | { [claude]: claudePick, [codex]: createModelSelection(codex, "gpt-5.5") }, |
| 928 | storedOtherModel, |
| 929 | )?.[codex], |
| 930 | ).toBeUndefined(); |
| 931 | }); |
| 932 | |
| 933 | it("keeps the default instance's draft model off a custom instance of the same driver", () => { |
| 934 | const driver = ProviderDriverKind.make("opencode"); |
| 935 | const defaultInstanceId = ProviderInstanceId.make("opencode"); |
| 936 | const kimiInstanceId = ProviderInstanceId.make("kimi_api"); |
| 937 | const kimiModel = "kimi-code-plan-global/k3.1-pro"; |
| 938 | const providers = [ |
| 939 | provider({ |
| 940 | provider: driver, |
| 941 | instanceId: defaultInstanceId, |
| 942 | models: ["opencode-go/kimi-k3.1-pro"], |
| 943 | }), |
| 944 | provider({ provider: driver, instanceId: kimiInstanceId, models: [kimiModel] }), |
| 945 | ]; |
| 946 | const state = deriveEffectiveComposerModelState({ |
| 947 | threadStarted: false, |
| 948 | draft: { |
| 949 | activeProvider: kimiInstanceId, |
| 950 | modelSelectionByProvider: { |
| 951 | [defaultInstanceId]: createModelSelection(defaultInstanceId, "opencode-go/kimi-k3.1-pro"), |
| 952 | }, |
| 953 | }, |
| 954 | providers, |
| 955 | selectedProvider: driver, |
| 956 | selectedInstanceId: kimiInstanceId, |
| 957 | threadModelSelection: null, |
| 958 | projectModelSelection: null, |
| 959 | settings: DEFAULT_UNIFIED_SETTINGS, |
| 960 | }); |
| 961 | |
| 962 | expect(state.selectedModel).toBe(kimiModel); |
| 963 | }); |
| 964 | |
| 965 | it("self-heals a persisted selection pointing at a text-generation-incapable instance", () => { |
| 966 | const providers = [ |
| 967 | provider({ |
| 968 | provider: ProviderDriverKind.make("acpRegistry"), |
| 969 | instanceId: "acp_gemini", |
| 970 | models: ["default"], |
| 971 | supportsTextGeneration: false, |
| 972 | }), |
| 973 | provider({ |
| 974 | instanceId: "codex", |
| 975 | models: ["gpt-5.6-luna"], |
| 976 | }), |
| 977 | ]; |
| 978 | const settings: UnifiedSettings = { |
| 979 | ...settingsWithProviderInstances(), |
| 980 | textGenerationModelSelection: { |
| 981 | instanceId: ProviderInstanceId.make("acp_gemini"), |
| 982 | model: "default", |
| 983 | }, |
| 984 | }; |
| 985 | |
| 986 | expect(resolveAppModelSelectionState(settings, providers).instanceId).toBe( |
| 987 | ProviderInstanceId.make("codex"), |
| 988 | ); |
| 989 | }); |
| 990 | it("falls back to the text generation default, not the chat default, for a removed model", () => { |
| 991 | const codex = provider({ instanceId: "codex", models: ["gpt-6-astra", "gpt-6-luna"] }); |
| 992 | const providers = [ |
| 993 | { ...codex, models: codex.models.map((m, i) => (i === 0 ? { ...m, isDefault: true } : m)) }, |
| 994 | ]; |
| 995 | const settings: UnifiedSettings = { |
| 996 | ...settingsWithProviderInstances(), |
| 997 | textGenerationModelSelection: createModelSelection( |
| 998 | ProviderInstanceId.make("codex"), |
| 999 | "gpt-5.6-luna", |
| 1000 | ), |
| 1001 | }; |
| 1002 | |
| 1003 | expect(resolveAppModelSelectionState(settings, providers).model).toBe( |
| 1004 | DEFAULT_TEXT_GENERATION_MODEL, |
| 1005 | ); |
| 1006 | }); |
| 1007 | |
| 1008 | it("does not select a provider that cannot generate system text", () => { |
| 1009 | const instanceId = ProviderInstanceId.make("antigravity"); |
| 1010 | const unsupported = { |
| 1011 | ...provider({ |
| 1012 | provider: ProviderDriverKind.make("antigravity"), |
| 1013 | instanceId, |
| 1014 | models: ["gemini-3.1-pro"], |
| 1015 | }), |
| 1016 | supportsTextGeneration: false, |
| 1017 | }; |
| 1018 | const supported = provider({ instanceId: "codex", models: ["gpt-5.6-sol"] }); |
| 1019 | const settings = { |
| 1020 | ...settingsWithProviderInstances(), |
| 1021 | textGenerationModelSelection: createModelSelection(instanceId, "gemini-3.1-pro"), |
| 1022 | }; |
| 1023 | |
| 1024 | expect(resolveAppModelSelectionState(settings, [unsupported, supported])).toEqual( |
| 1025 | createModelSelection(supported.instanceId, "gpt-5.6-sol"), |
| 1026 | ); |
| 1027 | expect(resolveAppModelSelectionState(settings, [unsupported])).toEqual( |
| 1028 | NO_PROVIDER_MODEL_SELECTION, |
| 1029 | ); |
| 1030 | }); |
| 1031 | |
| 1032 | it("excludes customized extension models while preserving unknown custom and thread models", () => { |
| 1033 | const pi = provider({ |
| 1034 | provider: ProviderDriverKind.make("pi"), |
| 1035 | instanceId: "pi", |
| 1036 | models: ["default", "demo/demo-model"], |
| 1037 | }); |
| 1038 | const providers = [ |
| 1039 | { |
| 1040 | ...pi, |
| 1041 | models: pi.models.map((model) => |
| 1042 | model.slug === "demo/demo-model" ? { ...model, supportsTextGeneration: false } : model, |
| 1043 | ), |
| 1044 | }, |
| 1045 | ]; |
| 1046 | const settings: UnifiedSettings = { |
| 1047 | ...DEFAULT_UNIFIED_SETTINGS, |
| 1048 | providerInstances: { |
| 1049 | [pi.instanceId]: { |
| 1050 | driver: ProviderDriverKind.make("pi"), |
| 1051 | config: { |
| 1052 | customModels: [ |
| 1053 | { slug: "demo/demo-model", name: "My extension model" }, |
| 1054 | { slug: "unknown/model", name: "My custom model" }, |
| 1055 | ], |
| 1056 | }, |
| 1057 | }, |
| 1058 | }, |
| 1059 | textGenerationModelSelection: createModelSelection(pi.instanceId, "demo/demo-model"), |
| 1060 | }; |
| 1061 | const supported = providers.filter((provider) => provider.supportsTextGeneration !== false); |
| 1062 | expect( |
| 1063 | getTextGenerationModelOptionsByInstance(settings, supported, pi.instanceId, "demo/demo-model") |
| 1064 | .get(pi.instanceId) |
| 1065 | ?.map((option) => option.slug), |
| 1066 | ).toEqual(["default", "unknown/model"]); |
| 1067 | expect(resolveAppModelSelectionState(settings, supported)).toEqual( |
| 1068 | createModelSelection(pi.instanceId, "default"), |
| 1069 | ); |
| 1070 | expect( |
| 1071 | resolveAppModelSelectionState( |
| 1072 | { |
| 1073 | ...settings, |
| 1074 | textGenerationModelSelection: createModelSelection(pi.instanceId, "unknown/model"), |
| 1075 | }, |
| 1076 | supported, |
| 1077 | ).model, |
| 1078 | ).toBe("unknown/model"); |
| 1079 | expect( |
| 1080 | getCustomModelOptionsByInstance(settings, providers) |
| 1081 | .get(pi.instanceId) |
| 1082 | ?.map((option) => option.slug), |
| 1083 | ).toEqual(["default", "demo/demo-model", "unknown/model"]); |
| 1084 | }); |
| 1085 | }); |
| 1086 | |
| 1087 | describe("resolveAppModelSelectionState with the opencode plan agent", () => { |
| 1088 | const instanceId = ProviderInstanceId.make("opencode"); |
| 1089 | const model = "opencode/gpt-5.4"; |
| 1090 | const opencode: ServerProvider = { |
| 1091 | ...provider({ provider: ProviderDriverKind.make("opencode"), instanceId: "opencode" }), |
| 1092 | models: [ |
| 1093 | { |
| 1094 | slug: model, |
| 1095 | name: model, |
| 1096 | isCustom: false, |
| 1097 | capabilities: { |
| 1098 | optionDescriptors: [ |
| 1099 | { |
| 1100 | id: "agent", |
| 1101 | label: "Agent", |
| 1102 | type: "select", |
| 1103 | options: [ |
| 1104 | { id: "build", label: "Build", isDefault: true }, |
| 1105 | { id: "plan", label: "Plan" }, |
| 1106 | ], |
| 1107 | currentValue: "build", |
| 1108 | }, |
| 1109 | ], |
| 1110 | }, |
| 1111 | }, |
| 1112 | ], |
| 1113 | }; |
| 1114 | const settings: UnifiedSettings = { |
| 1115 | ...DEFAULT_UNIFIED_SETTINGS, |
| 1116 | textGenerationModelSelection: createModelSelection(instanceId, model, [ |
| 1117 | { id: "agent", value: "plan" }, |
| 1118 | ]), |
| 1119 | }; |
| 1120 | |
| 1121 | it("keeps a stored plan agent this device did not pick", () => { |
| 1122 | expect(resolveAppModelSelectionState(settings, [opencode])).toEqual( |
| 1123 | createModelSelection(instanceId, model, [{ id: "agent", value: "plan" }]), |
| 1124 | ); |
| 1125 | }); |
| 1126 | }); |
| 1127 | |
| 1128 | describe("default model settings and new-thread composer parity", () => { |
| 1129 | it.each([ |
| 1130 | ["hidden", "claudeAgent", "claude-opus-5", "claude-opus-5-5", "medium"], |
| 1131 | ["visible", "claudeAgent", "claude-opus-5", "claude-opus-5", "max"], |
| 1132 | ["unavailable", "opencode", "openrouter/kimi-k3", "openrouter/kimi-k3", null], |
| 1133 | ] as const)( |
| 1134 | "resolves the %s default and its traits", |
| 1135 | (visibility, kind, model, expectedModel, effort) => { |
| 1136 | const driver = ProviderDriverKind.make(kind); |
| 1137 | const instanceId = ProviderInstanceId.make(`${kind}_work`); |
| 1138 | const snapshot = provider({ provider: driver, instanceId }); |
| 1139 | const models: ServerProvider["models"] = ["claude-opus-5-5", "claude-opus-5"].map( |
| 1140 | (slug, index) => ({ |
| 1141 | slug, |
| 1142 | name: slug, |
| 1143 | isCustom: false, |
| 1144 | capabilities: { |
| 1145 | optionDescriptors: [ |
| 1146 | { |
| 1147 | id: "effort", |
| 1148 | label: "Effort", |
| 1149 | type: "select", |
| 1150 | options: (index === 0 ? ["medium", "high"] : ["high", "max"]).map( |
| 1151 | (id, optionIndex) => ({ id, label: id, isDefault: optionIndex === 0 }), |
| 1152 | ), |
| 1153 | }, |
| 1154 | ], |
| 1155 | }, |
| 1156 | }), |
| 1157 | ); |
| 1158 | const providers = [{ ...snapshot, models }]; |
| 1159 | const stored = createModelSelection(instanceId, model, [{ id: "effort", value: "max" }]); |
| 1160 | const settings: UnifiedSettings = { |
| 1161 | ...settingsWithProviderInstances(), |
| 1162 | providerModelPreferences: { |
| 1163 | [instanceId]: { hiddenModels: visibility === "hidden" ? [model] : [], modelOrder: [] }, |
| 1164 | }, |
| 1165 | }; |
| 1166 | // ChatView builds the local draft with the project default as its thread selection. |
| 1167 | const composer = deriveEffectiveComposerModelState({ |
| 1168 | threadStarted: false, |
| 1169 | draft: null, |
| 1170 | providers, |
| 1171 | selectedProvider: driver, |
| 1172 | selectedInstanceId: instanceId, |
| 1173 | threadModelSelection: stored, |
| 1174 | projectModelSelection: stored, |
| 1175 | settings, |
| 1176 | }); |
| 1177 | const effective = resolveEffectiveDefaultModelSelection(settings, providers, stored); |
| 1178 | const traits = (model: string, modelOptions: typeof stored.options) => |
| 1179 | getComposerProviderState({ |
| 1180 | provider: driver, |
| 1181 | model, |
| 1182 | models, |
| 1183 | modelOptions, |
| 1184 | }); |
| 1185 | const composerTraits = traits(composer.selectedModel, composer.modelOptions?.[instanceId]); |
| 1186 | |
| 1187 | expect(composer.selectedModel).toBe(expectedModel); |
| 1188 | expect(traits(effective.model, effective.options)).toEqual(composerTraits); |
| 1189 | expect(effective).toEqual({ ...stored, model: composer.selectedModel }); |
| 1190 | expect(composerTraits.promptEffort).toBe(effort); |
| 1191 | }, |
| 1192 | ); |
| 1193 | }); |
| 1194 | |
| 1195 | describe("composer fallback to another instance of the same driver", () => { |
| 1196 | const claude = ProviderDriverKind.make("claudeAgent"); |
| 1197 | const codex = ProviderDriverKind.make("codex"); |
| 1198 | const disabled = ProviderInstanceId.make("claudeAgent"); |
| 1199 | const fallback = ProviderInstanceId.make("claude_team"); |
| 1200 | const effortModels: ServerProvider["models"] = [ |
| 1201 | { |
| 1202 | slug: "claude-opus-5-5", |
| 1203 | name: "Claude Opus 5.5", |
| 1204 | isCustom: false, |
| 1205 | capabilities: { |
| 1206 | optionDescriptors: [ |
| 1207 | { |
| 1208 | id: "effort", |
| 1209 | label: "Effort", |
| 1210 | type: "select", |
| 1211 | options: ["medium", "high", "xhigh"].map((id) => ({ |
| 1212 | id, |
| 1213 | label: id, |
| 1214 | isDefault: id === "medium", |
| 1215 | })), |
| 1216 | }, |
| 1217 | ], |
| 1218 | }, |
| 1219 | }, |
| 1220 | ]; |
| 1221 | const providers: ReadonlyArray<ServerProvider> = [ |
| 1222 | { |
| 1223 | ...provider({ provider: claude, instanceId: disabled }), |
| 1224 | enabled: false, |
| 1225 | models: effortModels, |
| 1226 | }, |
| 1227 | { ...provider({ provider: claude, instanceId: fallback }), models: effortModels }, |
| 1228 | ]; |
| 1229 | const picked = createModelSelection(disabled, "claude-opus-5-5", [ |
| 1230 | { id: "effort", value: "xhigh" }, |
| 1231 | ]); |
| 1232 | |
| 1233 | // The same steps useComposerProviderModel takes, without the draft store. |
| 1234 | function composerFor(input: { |
| 1235 | readonly providers: ReadonlyArray<ServerProvider>; |
| 1236 | readonly draft?: Parameters<typeof deriveEffectiveComposerModelState>[0]["draft"]; |
| 1237 | readonly threadModelSelection: ReturnType<typeof createModelSelection>; |
| 1238 | readonly customModels?: Partial<Record<string, ReadonlyArray<string>>>; |
| 1239 | }) { |
| 1240 | const settings = { |
| 1241 | ...DEFAULT_UNIFIED_SETTINGS, |
| 1242 | providerInstances: Object.fromEntries( |
| 1243 | input.providers.map((provider) => [ |
| 1244 | provider.instanceId, |
| 1245 | { |
| 1246 | driver: provider.driver, |
| 1247 | enabled: provider.enabled, |
| 1248 | config: { |
| 1249 | customModels: (input.customModels?.[provider.instanceId] ?? []).map((slug) => ({ |
| 1250 | slug, |
| 1251 | name: slug, |
| 1252 | capabilities: null, |
| 1253 | })), |
| 1254 | }, |
| 1255 | }, |
| 1256 | ]), |
| 1257 | ), |
| 1258 | }; |
| 1259 | const selection = resolveComposerProviderSelection({ |
| 1260 | entries: providerInstanceEntriesFor(input.providers, settings, null), |
| 1261 | candidateInstanceIds: [input.threadModelSelection.instanceId], |
| 1262 | lockedProvider: null, |
| 1263 | lockedInstanceId: undefined, |
| 1264 | }); |
| 1265 | const selectedInstanceId = selection.selectedProviderEntry!.instanceId; |
| 1266 | const selectedProvider = selection.selectedProviderEntry!.driverKind; |
| 1267 | const state = deriveEffectiveComposerModelState({ |
| 1268 | threadStarted: false, |
| 1269 | draft: input.draft ?? null, |
| 1270 | providers: input.providers, |
| 1271 | selectedProvider, |
| 1272 | selectedInstanceId, |
| 1273 | requestedInstanceId: selection.requestedInstanceId, |
| 1274 | threadModelSelection: input.threadModelSelection, |
| 1275 | projectModelSelection: null, |
| 1276 | settings, |
| 1277 | }); |
| 1278 | const traits = getComposerProviderState({ |
| 1279 | provider: selectedProvider, |
| 1280 | model: state.selectedModel, |
| 1281 | models: selection.selectedProviderEntry!.models, |
| 1282 | modelOptions: state.modelOptions?.[selectedInstanceId], |
| 1283 | }); |
| 1284 | return { selectedInstanceId, state, traits }; |
| 1285 | } |
| 1286 | |
| 1287 | it("keeps the requested instance's effort on the fallback instance", () => { |
| 1288 | const { selectedInstanceId, state, traits } = composerFor({ |
| 1289 | providers, |
| 1290 | threadModelSelection: picked, |
| 1291 | }); |
| 1292 | |
| 1293 | expect(selectedInstanceId).toBe(fallback); |
| 1294 | expect(state.selectedModel).toBe("claude-opus-5-5"); |
| 1295 | expect(traits.promptEffort).toBe("xhigh"); |
| 1296 | expect( |
| 1297 | createModelSelection(fallback, state.selectedModel, traits.modelOptionsForDispatch), |
| 1298 | ).toEqual( |
| 1299 | createModelSelection(fallback, "claude-opus-5-5", [{ id: "effort", value: "xhigh" }]), |
| 1300 | ); |
| 1301 | }); |
| 1302 | |
| 1303 | it("keeps options the fallback instance already has", () => { |
| 1304 | const { traits } = composerFor({ |
| 1305 | providers, |
| 1306 | threadModelSelection: picked, |
| 1307 | draft: { |
| 1308 | activeProvider: disabled, |
| 1309 | modelSelectionByProvider: { |
| 1310 | [disabled]: picked, |
| 1311 | [fallback]: createModelSelection(fallback, "claude-opus-5-5", [ |
| 1312 | { id: "effort", value: "high" }, |
| 1313 | ]), |
| 1314 | }, |
| 1315 | }, |
| 1316 | }); |
| 1317 | |
| 1318 | expect(traits.promptEffort).toBe("high"); |
| 1319 | }); |
| 1320 | |
| 1321 | it("does not carry options picked with another model", () => { |
| 1322 | // An optionless draft pick takes its options from the thread, which holds another model. |
| 1323 | const { state, traits } = composerFor({ |
| 1324 | providers, |
| 1325 | threadModelSelection: createModelSelection(disabled, "claude-sonnet-4-6", [ |
| 1326 | { id: "effort", value: "high" }, |
| 1327 | ]), |
| 1328 | draft: { |
| 1329 | activeProvider: disabled, |
| 1330 | modelSelectionByProvider: { [disabled]: createModelSelection(disabled, "claude-opus-5-5") }, |
| 1331 | }, |
| 1332 | }); |
| 1333 | |
| 1334 | expect(state.selectedModel).toBe("claude-opus-5-5"); |
| 1335 | expect(traits.promptEffort).toBe("medium"); |
| 1336 | expect(traits.modelOptionsForDispatch).toBeUndefined(); |
| 1337 | }); |
| 1338 | |
| 1339 | describe("with model aliases", () => { |
| 1340 | const reasoningModel = ( |
| 1341 | slug: string, |
| 1342 | id: string, |
| 1343 | extra: Partial<ServerProvider["models"][number]> = {}, |
| 1344 | ): ServerProvider["models"][number] => ({ |
| 1345 | slug, |
| 1346 | name: slug, |
| 1347 | isCustom: false, |
| 1348 | ...extra, |
| 1349 | capabilities: { |
| 1350 | optionDescriptors: [ |
| 1351 | { |
| 1352 | id, |
| 1353 | label: id, |
| 1354 | type: "select", |
| 1355 | options: ["medium", "high", "xhigh"].map((value) => ({ |
| 1356 | id: value, |
| 1357 | label: value, |
| 1358 | isDefault: value === "medium", |
| 1359 | })), |
| 1360 | }, |
| 1361 | ], |
| 1362 | }, |
| 1363 | }); |
| 1364 | |
| 1365 | it.each([ |
| 1366 | ["5.4", "gpt-5.4"], |
| 1367 | ["gpt-5-codex", "gpt-5.4"], |
| 1368 | ["gpt-5.4", "5.4"], |
| 1369 | ])("keeps the effort from %s on fallback %s", (savedModel, fallbackModel) => { |
| 1370 | const disabledCodex = ProviderInstanceId.make("codex"); |
| 1371 | const fallbackCodex = ProviderInstanceId.make("codex_team"); |
| 1372 | const models = [reasoningModel(fallbackModel, "reasoningEffort")]; |
| 1373 | const { selectedInstanceId, state, traits } = composerFor({ |
| 1374 | providers: [ |
| 1375 | { ...provider({ provider: codex, instanceId: disabledCodex }), enabled: false, models }, |
| 1376 | { ...provider({ provider: codex, instanceId: fallbackCodex }), models }, |
| 1377 | ], |
| 1378 | threadModelSelection: createModelSelection(disabledCodex, savedModel, [ |
| 1379 | { id: "reasoningEffort", value: "xhigh" }, |
| 1380 | ]), |
| 1381 | }); |
| 1382 | |
| 1383 | expect(selectedInstanceId).toBe(fallbackCodex); |
| 1384 | expect(state.selectedModel).toBe(fallbackModel); |
| 1385 | expect(traits.promptEffort).toBe("xhigh"); |
| 1386 | }); |
| 1387 | |
| 1388 | it.each([ |
| 1389 | { |
| 1390 | // A disabled Codex instance reports only its custom models. |
| 1391 | state: "disabled", |
| 1392 | requested: { |
| 1393 | enabled: false, |
| 1394 | models: [reasoningModel("5.4", "reasoningEffort", { isCustom: true })], |
| 1395 | }, |
| 1396 | }, |
| 1397 | { |
| 1398 | // An unavailable instance reports no models; its custom models are only in settings. |
| 1399 | state: "unavailable", |
| 1400 | requested: { enabled: false, availability: "unavailable" as const, models: [] }, |
| 1401 | }, |
| 1402 | ])( |
| 1403 | "does not alias-expand a custom model named 5.4 when the requested instance is $state", |
| 1404 | ({ requested }) => { |
| 1405 | const disabledCodex = ProviderInstanceId.make("codex"); |
| 1406 | const { state, traits } = composerFor({ |
| 1407 | providers: [ |
| 1408 | { ...provider({ provider: codex, instanceId: disabledCodex }), ...requested }, |
| 1409 | { |
| 1410 | ...provider({ provider: codex, instanceId: "codex_team" }), |
| 1411 | models: [reasoningModel("gpt-5.4", "reasoningEffort")], |
| 1412 | }, |
| 1413 | ], |
| 1414 | customModels: { [disabledCodex]: ["5.4"] }, |
| 1415 | threadModelSelection: createModelSelection(disabledCodex, "5.4", [ |
| 1416 | { id: "reasoningEffort", value: "xhigh" }, |
| 1417 | ]), |
| 1418 | }); |
| 1419 | |
| 1420 | expect(state.selectedModel).toBe("gpt-5.4"); |
| 1421 | expect(traits.modelOptionsForDispatch).toBeUndefined(); |
| 1422 | }, |
| 1423 | ); |
| 1424 | |
| 1425 | it("does not alias-expand the fallback instance's custom model", () => { |
| 1426 | const { state, traits } = composerFor({ |
| 1427 | providers: [ |
| 1428 | { ...provider({ provider: codex, instanceId: "codex" }), enabled: false }, |
| 1429 | { |
| 1430 | ...provider({ provider: codex, instanceId: "codex_team" }), |
| 1431 | models: [reasoningModel("5.4", "reasoningEffort", { isCustom: true })], |
| 1432 | }, |
| 1433 | ], |
| 1434 | customModels: { codex_team: ["5.4"] }, |
| 1435 | threadModelSelection: createModelSelection(ProviderInstanceId.make("codex"), "gpt-5.4", [ |
| 1436 | { id: "reasoningEffort", value: "xhigh" }, |
| 1437 | ]), |
| 1438 | }); |
| 1439 | |
| 1440 | expect(state.selectedModel).toBe("5.4"); |
| 1441 | expect(traits.modelOptionsForDispatch).toBeUndefined(); |
| 1442 | }); |
| 1443 | |
| 1444 | it("does not carry options through an alias that names another model per instance", () => { |
| 1445 | // "antigravity-default" is each Antigravity instance's current model. |
| 1446 | const antigravity = ProviderDriverKind.make("antigravity"); |
| 1447 | const disabledAntigravity = ProviderInstanceId.make("antigravity"); |
| 1448 | const current = { isDefault: true, aliases: [ANTIGRAVITY_DEFAULT_MODEL] }; |
| 1449 | const { state, traits } = composerFor({ |
| 1450 | providers: [ |
| 1451 | { |
| 1452 | ...provider({ provider: antigravity, instanceId: disabledAntigravity }), |
| 1453 | enabled: false, |
| 1454 | models: [reasoningModel("gemini-3-pro", "thinking", current)], |
| 1455 | }, |
| 1456 | { |
| 1457 | ...provider({ provider: antigravity, instanceId: "antigravity_work" }), |
| 1458 | models: [reasoningModel("claude-sonnet-4-6", "thinking", current)], |
| 1459 | }, |
| 1460 | ], |
| 1461 | threadModelSelection: createModelSelection(disabledAntigravity, ANTIGRAVITY_DEFAULT_MODEL, [ |
| 1462 | { id: "thinking", value: "high" }, |
| 1463 | ]), |
| 1464 | }); |
| 1465 | |
| 1466 | expect(state.selectedModel).toBe("claude-sonnet-4-6"); |
| 1467 | expect(traits.modelOptionsForDispatch).toBeUndefined(); |
| 1468 | }); |
| 1469 | }); |
| 1470 | |
| 1471 | it("does not carry options to another driver", () => { |
| 1472 | const codexOnly: ReadonlyArray<ServerProvider> = [ |
| 1473 | { |
| 1474 | ...provider({ provider: claude, instanceId: disabled }), |
| 1475 | enabled: false, |
| 1476 | models: effortModels, |
| 1477 | }, |
| 1478 | provider({ provider: codex, instanceId: "codex", models: ["gpt-6-astra"] }), |
| 1479 | ]; |
| 1480 | const { selectedInstanceId, state } = composerFor({ |
| 1481 | providers: codexOnly, |
| 1482 | threadModelSelection: picked, |
| 1483 | }); |
| 1484 | |
| 1485 | expect(selectedInstanceId).toBe(ProviderInstanceId.make("codex")); |
| 1486 | expect(state.modelOptions?.[selectedInstanceId]).toBeUndefined(); |
| 1487 | }); |
| 1488 | }); |