diff --git a/apps/mobile/src/features/threads/ThreadSettingsSheet.tsx b/apps/mobile/src/features/threads/ThreadSettingsSheet.tsx index 07eebd9e891c4..37e8d38a9ff85 100644 --- a/apps/mobile/src/features/threads/ThreadSettingsSheet.tsx +++ b/apps/mobile/src/features/threads/ThreadSettingsSheet.tsx @@ -1,6 +1,7 @@ import { getPromptControlledTraits, getTraitSelectChange, + optionsForModelSwitch, } from "@t3tools/client-runtime/model-traits"; import * as Dialog from "../../components/ui/Dialog"; import { createV5StackNavigator as createNativeStackNavigator } from "../../native/createV5StackNavigator"; @@ -1410,14 +1411,21 @@ export function ExistingThreadSettingsRouteScreen({ prompt={prompt} onPromptChange={(text) => setComposerDraftText(draftKey, text)} reportedModelSelection={reportedModelSelection} - onSelectModel={(option) => + onSelectModel={(option) => { + const remembered = withRememberedModelOptions(option.selection); + const options = optionsForModelSwitch({ + fromOptions: modelSelection.options, + fromCapabilities: currentModelOption?.capabilities, + toCapabilities: option.capabilities, + baseOptions: remembered.options, + }); + const selection = options + ? { ...option.selection, options: [...options] } + : { instanceId: option.selection.instanceId, model: option.selection.model }; updateModelSelection( - withStoredThreadModelOptions( - withRememberedModelOptions(option.selection), - selectedThread.modelSelection, - ), - ) - } + withStoredThreadModelOptions(selection, selectedThread.modelSelection), + ); + }} restoreSelection={withRememberedModelOptions} optionDescriptors={resolveProviderOptionDescriptors({ capabilities: currentModelOption?.capabilities, @@ -1453,7 +1461,7 @@ export function NewTaskThreadSettingsRouteScreen() { selectedModel={flow.selectedModel} prompt={flow.prompt} onPromptChange={flow.setPrompt} - onSelectModel={(option) => flow.setSelectedModelKey(option.key, option.selection.options)} + onSelectModel={(option) => flow.setSelectedModelKey(option.key)} restoreSelection={withRememberedModelOptions} optionDescriptors={optionDescriptors} onUpdateOptionSelections={flow.setSelectedModelOptions} diff --git a/apps/mobile/src/features/threads/new-task-flow-provider.tsx b/apps/mobile/src/features/threads/new-task-flow-provider.tsx index 9ad78fd94039a..a9be180841bd6 100644 --- a/apps/mobile/src/features/threads/new-task-flow-provider.tsx +++ b/apps/mobile/src/features/threads/new-task-flow-provider.tsx @@ -1,4 +1,5 @@ import { ActiveNewTaskContext } from "../../state/active-new-task"; +import { optionsForModelSwitch } from "@t3tools/client-runtime/model-traits"; import { resolveNewThreadModelSelection } from "@t3tools/shared/usageLimits"; import { useNowMinuteMs } from "../../lib/clock"; import * as Dialog from "../../components/ui/Dialog"; @@ -647,9 +648,18 @@ export function NewTaskFlowProvider( if (!option) { return; } - const selection = withRememberedModelOptions( + const remembered = withRememberedModelOptions( options ? { ...option.selection, options } : option.selection, ); + const nextOptions = optionsForModelSwitch({ + fromOptions: selectedModel?.options, + fromCapabilities: selectedModelOption?.capabilities, + toCapabilities: option.capabilities, + baseOptions: remembered.options, + }); + const selection = nextOptions + ? { ...remembered, options: nextOptions } + : { instanceId: remembered.instanceId, model: remembered.model }; updateComposerDraftSettings(selectedProjectDraftKey, { modelSelection: selection }); setStickyComposerModelSelection(selection); }; diff --git a/apps/web/src/components/chat/useComposerSelection.ts b/apps/web/src/components/chat/useComposerSelection.ts index 01562f0cf536d..59ed9df409530 100644 --- a/apps/web/src/components/chat/useComposerSelection.ts +++ b/apps/web/src/components/chat/useComposerSelection.ts @@ -17,7 +17,9 @@ import { scopeThreadRef } from "@t3tools/client-runtime/environment"; import { type SetStateAction } from "react"; import { useNavigate } from "@tanstack/react-router"; import { toastManager } from "../ui/toast"; +import { optionsForModelSwitch } from "@t3tools/client-runtime/model-traits"; import { resolveAppModelSelectionForInstance } from "../../modelSelection"; +import { getProviderModelCapabilities } from "../../providerModels"; import { draftModelSelectionApplies, resolveNewDraftStartFromOrigin, @@ -206,9 +208,26 @@ export function useComposerSelection(input: ComposerSelectionInput) { ) ? threadSelection.options : undefined; + const fromProvider = providerStatuses.find( + (snapshot) => snapshot.instanceId === shownSelection.instanceId, + ); + const nextOptions = optionsForModelSwitch({ + fromOptions: shownSelection.options, + fromCapabilities: fromProvider + ? getProviderModelCapabilities( + fromProvider.models, + shownSelection.model, + fromProvider.driver, + ) + : null, + toCapabilities: entry + ? getProviderModelCapabilities(entry.models, resolvedModel, entry.driver) + : null, + baseOptions: pickOptions, + }); const nextModelSelection: ModelSelection = - pickOptions !== undefined && pickOptions.length > 0 - ? { instanceId, model: resolvedModel, options: [...pickOptions] } + nextOptions !== undefined && nextOptions.length > 0 + ? { instanceId, model: resolvedModel, options: [...nextOptions] } : { instanceId, model: resolvedModel }; const modelChangeBlockReason = getStartedThreadModelChangeBlockReason({ hasStartedSession: activeRuntime !== null, diff --git a/packages/client-runtime/src/modelTraits.test.ts b/packages/client-runtime/src/modelTraits.test.ts index 24da035532a04..e15873a87fb85 100644 --- a/packages/client-runtime/src/modelTraits.test.ts +++ b/packages/client-runtime/src/modelTraits.test.ts @@ -1,7 +1,13 @@ -import type { ProviderOptionDescriptor } from "@t3tools/contracts"; +import type { ModelCapabilities, ProviderOptionDescriptor } from "@t3tools/contracts"; import { describe, expect, it } from "vite-plus/test"; -import { buildTraitsTriggerDisplay, getDescriptorStringValue } from "./modelTraits.ts"; +import { + buildTraitsTriggerDisplay, + getDescriptorStringValue, + optionsForModelSwitch, + resolveReasoningEffortForModel, + statedReasoningEffort, +} from "./modelTraits.ts"; describe("buildTraitsTriggerDisplay", () => { it.each([ @@ -33,3 +39,181 @@ describe("buildTraitsTriggerDisplay", () => { expect(getDescriptorStringValue(descriptor)).toBe(input.value); }); }); + +function effortCapabilities(input: { + readonly id?: string; + readonly values: ReadonlyArray; + readonly defaultValue?: string; + readonly promptInjected?: ReadonlyArray; + readonly extra?: ModelCapabilities["optionDescriptors"]; +}): ModelCapabilities { + return { + optionDescriptors: [ + { + id: input.id ?? "effort", + label: "Effort", + type: "select", + options: input.values.map((id) => ({ + id, + label: id, + ...(id === input.defaultValue ? { isDefault: true } : {}), + })), + ...(input.defaultValue ? { currentValue: input.defaultValue } : {}), + ...(input.promptInjected ? { promptInjectedValues: [...input.promptInjected] } : {}), + }, + ...(input.extra ?? []), + ], + }; +} + +describe("resolveReasoningEffortForModel", () => { + const opus = effortCapabilities({ + values: ["low", "medium", "high", "xhigh"], + defaultValue: "medium", + }); + const sonnet = effortCapabilities({ values: ["low", "medium", "high"], defaultValue: "medium" }); + + it("keeps an effort the next model offers", () => { + expect(resolveReasoningEffortForModel("high", sonnet)).toEqual({ + id: "effort", + value: "high", + kept: true, + }); + }); + + it("falls back to the next model's default when the effort is not offered", () => { + expect(resolveReasoningEffortForModel("xhigh", sonnet)).toEqual({ + id: "effort", + value: "medium", + kept: false, + }); + expect(resolveReasoningEffortForModel("xhigh", opus)?.kept).toBe(true); + }); + + it("maps the effort onto the next model's reasoning id", () => { + const codex = effortCapabilities({ + id: "reasoningEffort", + values: ["low", "high"], + defaultValue: "low", + }); + expect(resolveReasoningEffortForModel("high", codex)).toEqual({ + id: "reasoningEffort", + value: "high", + kept: true, + }); + }); + + it("does not keep a prompt-injected level", () => { + const claude = effortCapabilities({ + values: ["high", "ultrathink"], + defaultValue: "high", + promptInjected: ["ultrathink"], + }); + expect(resolveReasoningEffortForModel("ultrathink", claude)).toEqual({ + id: "effort", + value: "high", + kept: false, + }); + }); + + it("returns nothing for a model with no reasoning control", () => { + expect( + resolveReasoningEffortForModel("high", { + optionDescriptors: [ + { id: "fastMode", label: "Fast", type: "boolean", currentValue: false }, + ], + }), + ).toBeNull(); + }); +}); + +describe("optionsForModelSwitch", () => { + const from = effortCapabilities({ + values: ["low", "high", "xhigh"], + defaultValue: "high", + }); + const to = effortCapabilities({ + id: "reasoningEffort", + values: ["low", "medium", "high"], + defaultValue: "medium", + extra: [{ id: "fastMode", label: "Fast", type: "boolean" }], + }); + + it("keeps the current effort and supported options, and drops the rest", () => { + expect( + optionsForModelSwitch({ + fromOptions: [{ id: "effort", value: "high" }], + fromCapabilities: from, + toCapabilities: to, + baseOptions: [ + { id: "fastMode", value: true }, + { id: "contextWindow", value: "1m" }, + { id: "effort", value: "low" }, + ], + }), + ).toEqual([ + { id: "fastMode", value: true }, + { id: "reasoningEffort", value: "high" }, + ]); + }); + + it("leaves the next model's default in place when the effort is not offered", () => { + expect( + optionsForModelSwitch({ + fromOptions: [{ id: "effort", value: "xhigh" }], + fromCapabilities: from, + toCapabilities: to, + baseOptions: [ + { id: "fastMode", value: false }, + { id: "reasoningEffort", value: "low" }, + ], + }), + ).toEqual([{ id: "fastMode", value: false }]); + expect(statedReasoningEffort([{ id: "effort", value: "xhigh" }], from)).toBe("xhigh"); + }); + + it("keeps an unset effort when that default is offered", () => { + expect(statedReasoningEffort(undefined, from)).toBe("high"); + expect( + optionsForModelSwitch({ + fromOptions: undefined, + fromCapabilities: from, + toCapabilities: to, + }), + ).toEqual([{ id: "reasoningEffort", value: "high" }]); + }); + + it("keeps a boolean thinking toggle the next model offers", () => { + const next = effortCapabilities({ + values: ["low", "high"], + defaultValue: "high", + extra: [{ id: "thinking", label: "Thinking", type: "boolean" }], + }); + expect( + optionsForModelSwitch({ + fromOptions: [{ id: "effort", value: "high" }], + fromCapabilities: effortCapabilities({ values: ["low", "high"], defaultValue: "high" }), + toCapabilities: next, + baseOptions: [ + { id: "thinking", value: false }, + { id: "effort", value: "low" }, + ], + }), + ).toEqual([ + { id: "thinking", value: false }, + { id: "effort", value: "high" }, + ]); + }); + + it("keeps stored options when the next model has no catalog", () => { + const stored = [{ id: "reasoningEffort", value: "high" }]; + expect( + optionsForModelSwitch({ + fromOptions: stored, + fromCapabilities: null, + toCapabilities: null, + baseOptions: stored, + }), + ).toEqual(stored); + }); +}); diff --git a/packages/client-runtime/src/modelTraits.ts b/packages/client-runtime/src/modelTraits.ts index 6e84acf10fc0d..47b3e7d76c34f 100644 --- a/packages/client-runtime/src/modelTraits.ts +++ b/packages/client-runtime/src/modelTraits.ts @@ -1,8 +1,14 @@ -import type { ModelSelection, ProviderOptionDescriptor } from "@t3tools/contracts"; +import type { + ModelCapabilities, + ModelSelection, + ProviderOptionDescriptor, + ProviderOptionSelection, +} from "@t3tools/contracts"; import { buildProviderOptionSelectionsFromDescriptors, getProviderOptionCurrentLabel, getProviderOptionCurrentValue, + getProviderOptionDescriptors, isClaudeUltrathinkPrompt, } from "@t3tools/shared/model"; import { REASONING_EFFORT_OPTION_IDS } from "./state/threadExecution.ts"; @@ -186,3 +192,99 @@ export function buildTraitsTriggerDisplay(input: { } return { label: labels.join(" ยท "), speedLabel, ultracode }; } + +function reasoningDescriptor( + capabilities: ModelCapabilities | null | undefined, +): Extract | null { + return capabilities?.optionDescriptors?.find(isReasoningSelect) ?? null; +} + +function explicitReasoningValue( + options: ReadonlyArray | null | undefined, +): string | null { + if (!options) return null; + for (const id of REASONING_DESCRIPTOR_IDS) { + const value = options.find( + (option) => option.id === id && typeof option.value === "string", + )?.value; + if (typeof value === "string") return value; + } + return null; +} + +export function statedReasoningEffort( + options: ReadonlyArray | null | undefined, + capabilities: ModelCapabilities | null | undefined, +): string | null { + const descriptor = reasoningDescriptor(capabilities); + if (!descriptor || !capabilities?.optionDescriptors?.length) { + return explicitReasoningValue(options); + } + const resolved = getProviderOptionDescriptors({ + caps: capabilities, + selections: options, + }).find((candidate) => candidate.id === descriptor.id); + if (!resolved || resolved.type !== "select") return explicitReasoningValue(options); + const value = getProviderOptionCurrentValue(resolved); + return typeof value === "string" ? value : null; +} + +export function resolveReasoningEffortForModel( + effort: string | null, + capabilities: ModelCapabilities | null | undefined, +): { readonly id: string; readonly value: string; readonly kept: boolean } | null { + const descriptor = reasoningDescriptor(capabilities); + if (!descriptor) return null; + const choices = selectableProviderOptionChoices(descriptor); + if (effort && choices.some((choice) => choice.id === effort)) { + return { id: descriptor.id, value: effort, kept: true }; + } + const fallback = + choices.find((choice) => choice.isDefault)?.id ?? + (descriptor.currentValue && choices.some((choice) => choice.id === descriptor.currentValue) + ? descriptor.currentValue + : undefined); + if (!fallback) return null; + return { id: descriptor.id, value: fallback, kept: false }; +} + +function isCarriedReasoningOption( + id: string, + capabilities: ModelCapabilities | null | undefined, +): boolean { + const descriptor = capabilities?.optionDescriptors?.find((candidate) => candidate.id === id); + if (!descriptor) return REASONING_DESCRIPTOR_IDS.has(id); + return isReasoningSelect(descriptor); +} + +export function optionsForModelSwitch(input: { + readonly fromOptions: ReadonlyArray | null | undefined; + readonly fromCapabilities: ModelCapabilities | null | undefined; + readonly toCapabilities: ModelCapabilities | null | undefined; + readonly baseOptions?: ReadonlyArray | null | undefined; +}): ReadonlyArray | undefined { + const descriptors = input.toCapabilities?.optionDescriptors; + if (!descriptors || descriptors.length === 0) { + return input.baseOptions && input.baseOptions.length > 0 ? [...input.baseOptions] : undefined; + } + const next: Array = []; + for (const option of input.baseOptions ?? []) { + if (isCarriedReasoningOption(option.id, input.toCapabilities)) continue; + const descriptor = descriptors.find((candidate) => candidate.id === option.id); + if (!descriptor) continue; + if (descriptor.type === "boolean") { + if (typeof option.value === "boolean") next.push({ id: option.id, value: option.value }); + continue; + } + if (typeof option.value !== "string") continue; + if (descriptor.promptInjectedValues?.includes(option.value)) continue; + if (!descriptor.options.some((choice) => choice.id === option.value)) continue; + next.push({ id: option.id, value: option.value }); + } + const resolved = resolveReasoningEffortForModel( + statedReasoningEffort(input.fromOptions, input.fromCapabilities), + input.toCapabilities, + ); + if (resolved?.kept) next.push({ id: resolved.id, value: resolved.value }); + return next.length > 0 ? next : undefined; +}