Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 16 additions & 8 deletions apps/mobile/src/features/threads/ThreadSettingsSheet.tsx
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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}
Expand Down
12 changes: 11 additions & 1 deletion apps/mobile/src/features/threads/new-task-flow-provider.tsx
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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);
};
Expand Down
23 changes: 21 additions & 2 deletions apps/web/src/components/chat/useComposerSelection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
188 changes: 186 additions & 2 deletions packages/client-runtime/src/modelTraits.test.ts
Original file line number Diff line number Diff line change
@@ -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([
Expand Down Expand Up @@ -33,3 +39,181 @@ describe("buildTraitsTriggerDisplay", () => {
expect(getDescriptorStringValue(descriptor)).toBe(input.value);
});
});

function effortCapabilities(input: {
readonly id?: string;
readonly values: ReadonlyArray<string>;
readonly defaultValue?: string;
readonly promptInjected?: ReadonlyArray<string>;
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);
});
});
Loading
Loading