diff --git a/apps/web/src/components/chat/ModelPickerContent.test.ts b/apps/web/src/components/chat/ModelPickerContent.test.ts new file mode 100644 index 0000000000000..0ed30d552cca9 --- /dev/null +++ b/apps/web/src/components/chat/ModelPickerContent.test.ts @@ -0,0 +1,40 @@ +import { ProviderInstanceId } from "@t3tools/contracts"; +import { describe, expect, it } from "vite-plus/test"; + +import { initialModelPickerProvider } from "./ModelPickerContent"; + +describe("initialModelPickerProvider", () => { + const activeInstanceId = ProviderInstanceId.make("claudeAgent_personal"); + const input = { + activeInstanceId, + keepActiveInstance: false, + activeInstanceVisible: true, + hasFavorites: true, + lastOrigin: null, + } as const; + + it("reopens on the tab the last model was picked from", () => { + expect(initialModelPickerProvider({ ...input, lastOrigin: "favorites" })).toBe("favorites"); + expect(initialModelPickerProvider({ ...input, lastOrigin: "instance" })).toBe(activeInstanceId); + expect(initialModelPickerProvider(input)).toBe(activeInstanceId); + }); + + it("keeps the active instance when it is locked, needs setup, or there are no favorites", () => { + expect( + initialModelPickerProvider({ ...input, keepActiveInstance: true, lastOrigin: "favorites" }), + ).toBe(activeInstanceId); + expect( + initialModelPickerProvider({ ...input, hasFavorites: false, lastOrigin: "favorites" }), + ).toBe(activeInstanceId); + }); + + it("falls back to favorites when the active instance is not in the rail", () => { + expect( + initialModelPickerProvider({ + ...input, + activeInstanceVisible: false, + lastOrigin: "instance", + }), + ).toBe("favorites"); + }); +}); diff --git a/apps/web/src/components/chat/ModelPickerContent.tsx b/apps/web/src/components/chat/ModelPickerContent.tsx index 924f7455fc3c9..3d8de06d3c5e5 100644 --- a/apps/web/src/components/chat/ModelPickerContent.tsx +++ b/apps/web/src/components/chat/ModelPickerContent.tsx @@ -24,6 +24,11 @@ import { import { ChevronRightIcon } from "lucide-react"; import { ModelListRow } from "./ModelListRow"; import { ModelPickerSidebar } from "./ModelPickerSidebar"; +import { + type ModelPickerOrigin, + readModelPickerOrigin, + writeModelPickerOrigin, +} from "./modelPickerOrigin"; import { getProviderStatusMessage, hasProviderSetup } from "./ProviderStatusBanner"; import { modelPickerLegacySectionKey, @@ -131,6 +136,22 @@ function shouldOfferModelPickerSetup( ); } +/** Reopens on the rail tab the last model was picked from. */ +export function initialModelPickerProvider(input: { + activeInstanceId: ProviderInstanceId; + keepActiveInstance: boolean; + activeInstanceVisible: boolean; + hasFavorites: boolean; + lastOrigin: ModelPickerOrigin | null; +}): ProviderInstanceId | "favorites" { + if (input.keepActiveInstance || !input.hasFavorites) { + return input.activeInstanceId; + } + return !input.activeInstanceVisible || input.lastOrigin === "favorites" + ? "favorites" + : input.activeInstanceId; +} + function adjacentModelPickerProvider(input: { entries: ReadonlyArray; selectedInstanceId: ProviderInstanceId | "favorites"; @@ -417,18 +438,24 @@ export const ModelPickerContent = memo(function ModelPickerContent(props: { activeEntry, modelOptionsByInstance.get(props.activeInstanceId) ?? [], ); - const [selectedInstanceId, setSelectedInstanceId] = useState( - () => { - if ( + const [lastOrigin] = useState(readModelPickerOrigin); + const [chosenInstanceId, setChosenInstanceId] = useState( + null, + ); + // Favorites can arrive after the picker opens, so follow them until the user picks a tab. + const selectedInstanceId = + chosenInstanceId ?? + initialModelPickerProvider({ + activeInstanceId: props.activeInstanceId, + keepActiveInstance: props.lockedProvider !== null || activeInstanceHasSelectableUnavailableModel || - activeInstanceNeedsSetup - ) { - return props.activeInstanceId; - } - return favorites.length > 0 ? "favorites" : props.activeInstanceId; - }, - ); + activeInstanceNeedsSetup, + activeInstanceVisible: + activeEntry !== undefined && isProviderInstancePickerVisible(activeEntry), + hasFavorites: favorites.length > 0, + lastOrigin, + }); const favoritesSet = new Set(favorites.map((fav) => providerModelKey(fav.provider, fav.model))); const [expandedLegacyInstances, setExpandedLegacyInstances] = useState( () => @@ -449,7 +476,7 @@ export const ModelPickerContent = memo(function ModelPickerContent(props: { }; const handleSelectInstance = (instanceId: ProviderInstanceId | "favorites") => { - setSelectedInstanceId(instanceId); + setChosenInstanceId(instanceId); window.requestAnimationFrame(() => { focusSearchInput(); }); @@ -499,6 +526,9 @@ export const ModelPickerContent = memo(function ModelPickerContent(props: { } const resolvedModel = resolveSelectableModel(entry.driverKind, modelSlug, options); if (resolvedModel) { + if (!isSearching) { + writeModelPickerOrigin(selectedInstanceId === "favorites" ? "favorites" : "instance"); + } if (additive && onToggleModel) { onToggleModel(instanceId, resolvedModel); } else { diff --git a/apps/web/src/components/chat/modelPickerOrigin.ts b/apps/web/src/components/chat/modelPickerOrigin.ts new file mode 100644 index 0000000000000..9765e57cc6088 --- /dev/null +++ b/apps/web/src/components/chat/modelPickerOrigin.ts @@ -0,0 +1,26 @@ +import * as Schema from "effect/Schema"; + +import { getLocalStorageItem, setLocalStorageItem } from "../../hooks/useLocalStorage"; + +const MODEL_PICKER_ORIGIN_STORAGE_KEY = "t3code:model-picker-origin:v1"; + +/** Which rail tab the last model was picked from, so the picker reopens there. */ +const ModelPickerOrigin = Schema.Literals(["favorites", "instance"]); +export type ModelPickerOrigin = typeof ModelPickerOrigin.Type; + +export function readModelPickerOrigin(): ModelPickerOrigin | null { + try { + return getLocalStorageItem(MODEL_PICKER_ORIGIN_STORAGE_KEY, ModelPickerOrigin); + } catch (error) { + console.error("Could not read model picker origin.", error); + return null; + } +} + +export function writeModelPickerOrigin(origin: ModelPickerOrigin): void { + try { + setLocalStorageItem(MODEL_PICKER_ORIGIN_STORAGE_KEY, origin, ModelPickerOrigin); + } catch (error) { + console.error("Could not persist model picker origin.", error); + } +}