TypeScript
fix(mobile): picking a model shows its remembered options before saving (#15719)
nekohasekaiad50f62eefd3
diff --git a/apps/mobile/src/features/threads/ThreadComposer.tsx b/apps/mobile/src/features/threads/ThreadComposer.tsxindex 8aecb5b07e6..ee34ce242c0 100644--- a/apps/mobile/src/features/threads/ThreadComposer.tsx+++ b/apps/mobile/src/features/threads/ThreadComposer.tsx@@ -605,6 +605,7 @@ export const ThreadComposer = function ThreadComposer(props: ThreadComposerPropsreportedModelSelection: props.reportedModelSelection,onSelectModel: (option) =>props.onUpdateModelSelection(withRememberedModelOptions(option.selection)),+ restoreSelection: withRememberedModelOptions,optionDescriptors: providerOptionDescriptors,onUpdateOptionSelections: (options) => {rememberModelOptions(diff --git a/apps/mobile/src/features/threads/ThreadSettingsSheet.tsx b/apps/mobile/src/features/threads/ThreadSettingsSheet.tsxindex 0df2b64e1e6..3d3c471cb9e 100644--- a/apps/mobile/src/features/threads/ThreadSettingsSheet.tsx+++ b/apps/mobile/src/features/threads/ThreadSettingsSheet.tsx@@ -28,7 +28,15 @@ import { useNavigation, useRoute, type RouteProp } from "@react-navigation/nativimport { type NativeStackNavigationProp } from "@react-navigation/native-stack";import * as Haptics from "expo-haptics";import { AsyncResult } from "effect/reactivity";-import { createContext, use, useEffect, useRef, useState, type ReactNode } from "react";+import {+ createContext,+ use,+ useEffect,+ useEffectEvent,+ useRef,+ useState,+ type ReactNode,+} from "react";import { Platform, Pressable, RefreshControl, ScrollView, TextInput, View } from "react-native";import Animated, { FadeIn, FadeOut, LinearTransition } from "react-native-reanimated";import { useSafeAreaInsets } from "react-native-safe-area-context";@@ -51,7 +59,10 @@ import {import { applyProviderOptionSelection } from "../../lib/providerOptions";import { resolveProviderOptionDescriptors } from "../../lib/providerOptions";import { useUniwindTheme } from "../../lib/useUniwindTheme";-import { rememberModelOptions } from "../../state/use-model-option-memory";+import {+ rememberModelOptions,+ withRememberedModelOptions,+} from "../../state/use-model-option-memory";import { NativeStackScreenOptions, nativeHeaderScrollEdgeEffects } from "../../native/StackHeader";import { NATIVE_LIQUID_GLASS_SUPPORTED } from "../../native/native-glass";import { ChatGptSharingStatus } from "./ChatGptSharingStatus";@@ -234,6 +245,7 @@ type ThreadSettingsSessionProps = {readonly selectedModel: ModelSelection | null;readonly reportedModelSelection?: ModelSelection | null;readonly onSelectModel: (option: ModelOption) => void;+ readonly restoreSelection?: (selection: ModelSelection) => ModelSelection;readonly optionDescriptors: ReadonlyArray<ProviderOptionDescriptor>;readonly onUpdateOptionSelections: (selections: ReadonlyArray<ProviderOptionSelection>) => void;readonly runtimeMode: RuntimeMode;@@ -533,6 +545,7 @@ function ThreadSettingsSessionProvider(current,pressed: option,pressedIsApplied: isApplied(option),+ restoreSelection: props.restoreSelection,}),);};@@ -1122,10 +1135,10 @@ function ThreadSettingsModelsScreen() {const usesNativeMailSearchToolbar = useNativeMailSearchToolbar();const theme = useUniwindTheme();const hasCustomCatalogFilter = session.providerFilter !== null || session.showLegacy;- const commitAndClose = () => {+ const commitAndClose = useEffectEvent(() => {if (!session.commitPendingModel()) return;presentation.onClose();- };+ });const providerFilters = [{ id: "all-providers", title: "All providers", value: null },{ id: "favorites", title: "Favorites", value: FAVORITES_PROVIDER_FILTER },@@ -1455,6 +1468,7 @@ export function NewTaskThreadSettingsRouteScreen() {providerGroups={flow.providerGroups}selectedModel={flow.selectedModel}onSelectModel={(option) => flow.setSelectedModelKey(option.key, option.selection.options)}+ restoreSelection={withRememberedModelOptions}optionDescriptors={optionDescriptors}onUpdateOptionSelections={flow.setSelectedModelOptions}runtimeMode={flow.runtimeMode}diff --git a/apps/mobile/src/features/threads/thread-settings-sheet-state.test.ts b/apps/mobile/src/features/threads/thread-settings-sheet-state.test.tsindex 61e606aa6dd..8c65f3e6e22 100644--- a/apps/mobile/src/features/threads/thread-settings-sheet-state.test.ts+++ b/apps/mobile/src/features/threads/thread-settings-sheet-state.test.ts@@ -1,8 +1,16 @@-import { describe, expect, it } from "vite-plus/test";+import { afterEach, beforeEach, describe, expect, it } from "@effect/vitest";+import { vi } from "vite-plus/test";import { ProviderInstanceId, type ProviderOptionSelection } from "@t3tools/contracts";+import { getProviderOptionDescriptors } from "@t3tools/shared/model";import type { ModelOption } from "../../lib/modelOptions";+import { appAtomRegistry } from "../../state/atom-registry";+import { modelOptionMemoryAtom } from "../../state/use-composer-drafts";+import {+ rememberModelOptions,+ withRememberedModelOptions,+} from "../../state/use-model-option-memory";import {canCommitPendingModel,favoritesFirst,@@ -23,7 +31,30 @@ function modelOption(providerDriver: "codex",isDefault: false,isLegacy: false,- capabilities: null,+ capabilities: {+ optionDescriptors: [+ {+ id: "effort",+ label: "Reasoning",+ type: "select",+ options: [+ { id: "medium", label: "Medium", isDefault: true },+ { id: "high", label: "High" },+ ],+ currentValue: "medium",+ },+ {+ id: "serviceTier",+ label: "Service Tier",+ type: "select",+ options: [+ { id: "default", label: "Standard", isDefault: true },+ { id: "priority", label: "Fast" },+ ],+ currentValue: "default",+ },+ ],+ },selection: {instanceId: ProviderInstanceId.make("codex"),model,@@ -32,6 +63,16 @@ function modelOption(};}+beforeEach(() => {+ vi.useFakeTimers();+});++afterEach(() => {+ vi.clearAllTimers();+ vi.useRealTimers();+ appAtomRegistry.set(modelOptionMemoryAtom, {});+});+describe("thread settings sheet state", () => {it("keeps favorites in catalog order ahead of other models", () => {const models = [@@ -101,7 +142,8 @@ describe("thread settings sheet state", () => {).toBeNull();});- it("preserves staged options when the highlighted model is pressed again", () => {+ it("preserves in-progress edits when the staged model is pressed again", () => {+ rememberModelOptions("codex", "gpt-next", [{ id: "effort", value: "medium" }]);const pending = modelOption("gpt-next", [{ id: "effort", value: "high" }]);expect(@@ -109,20 +151,59 @@ describe("thread settings sheet state", () => {current: pending,pressed: modelOption("gpt-next"),pressedIsApplied: false,- }),- ).toBe(pending);+ restoreSelection: withRememberedModelOptions,+ })?.selection.options,+ ).toEqual([{ id: "effort", value: "high" }]);+ });++ it("previews remembered options before Save when switching models", () => {+ const remembered = [+ { id: "effort", value: "high" },+ { id: "serviceTier", value: "priority" },+ ];+ rememberModelOptions("codex", "gpt-other", remembered);+ const pressed = modelOption("gpt-other");++ const pending = pendingModelAfterPress({+ current: modelOption("gpt-next"),+ pressed,+ pressedIsApplied: false,+ restoreSelection: withRememberedModelOptions,+ });++ expect(pending?.selection).toEqual({ ...pressed.selection, options: remembered });+ const preview = getProviderOptionDescriptors({+ caps: pressed.capabilities!,+ selections: pending?.selection.options,+ });+ expect(preview.map(({ id, currentValue }) => ({ id, value: currentValue }))).toEqual(+ remembered,+ );+ expect(pending?.selection).toEqual(withRememberedModelOptions(pressed.selection));});- it("stages a different model", () => {+ it("keeps catalog defaults for scheduled-task picks even when options are remembered", () => {+ rememberModelOptions("codex", "gpt-other", [+ { id: "effort", value: "high" },+ { id: "serviceTier", value: "priority" },+ ]);const pressed = modelOption("gpt-other");- expect(- pendingModelAfterPress({- current: modelOption("gpt-next"),- pressed,- pressedIsApplied: false,- }),- ).toBe(pressed);+ const pending = pendingModelAfterPress({+ current: modelOption("gpt-next"),+ pressed,+ pressedIsApplied: false,+ });++ expect(pending?.selection).toEqual(pressed.selection);+ const preview = getProviderOptionDescriptors({+ caps: pressed.capabilities!,+ selections: pending?.selection.options,+ });+ expect(preview.map(({ id, currentValue }) => ({ id, value: currentValue }))).toEqual([+ { id: "effort", value: "medium" },+ { id: "serviceTier", value: "default" },+ ]);});it("cannot select a hidden model exposed only for visibility management", () => {diff --git a/apps/mobile/src/features/threads/thread-settings-sheet-state.ts b/apps/mobile/src/features/threads/thread-settings-sheet-state.tsindex 98519b7349e..4fff09253ab 100644--- a/apps/mobile/src/features/threads/thread-settings-sheet-state.ts+++ b/apps/mobile/src/features/threads/thread-settings-sheet-state.ts@@ -1,5 +1,5 @@import type { ModelOption, ProviderGroup } from "../../lib/modelOptions";-import type { ProviderInstanceId } from "@t3tools/contracts";+import type { ModelSelection, ProviderInstanceId } from "@t3tools/contracts";export function modelFavoriteKey(provider: ProviderInstanceId, model: string): string {return `${provider}:${model}`;@@ -39,11 +39,17 @@ export function pendingModelAfterPress(input: {readonly current: ModelOption | null;readonly pressed: ModelOption;readonly pressedIsApplied: boolean;+ readonly restoreSelection?: (selection: ModelSelection) => ModelSelection;}): ModelOption | null {if (input.pressedIsApplied) {return null;}- return input.current?.key === input.pressed.key ? input.current : input.pressed;+ if (input.current?.key === input.pressed.key) {+ return input.current;+ }+ return input.restoreSelection+ ? { ...input.pressed, selection: input.restoreSelection(input.pressed.selection) }+ : input.pressed;}export function canCommitPendingModel(