Skip to content
Closed
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
40 changes: 40 additions & 0 deletions apps/web/src/components/chat/ModelPickerContent.test.ts
Original file line number Diff line number Diff line change
@@ -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");
});
});
52 changes: 41 additions & 11 deletions apps/web/src/components/chat/ModelPickerContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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<ProviderInstanceEntry>;
selectedInstanceId: ProviderInstanceId | "favorites";
Expand Down Expand Up @@ -417,18 +438,24 @@ export const ModelPickerContent = memo(function ModelPickerContent(props: {
activeEntry,
modelOptionsByInstance.get(props.activeInstanceId) ?? [],
);
const [selectedInstanceId, setSelectedInstanceId] = useState<ProviderInstanceId | "favorites">(
() => {
if (
const [lastOrigin] = useState(readModelPickerOrigin);
const [chosenInstanceId, setChosenInstanceId] = useState<ProviderInstanceId | "favorites" | null>(
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(
() =>
Expand All @@ -449,7 +476,7 @@ export const ModelPickerContent = memo(function ModelPickerContent(props: {
};

const handleSelectInstance = (instanceId: ProviderInstanceId | "favorites") => {
setSelectedInstanceId(instanceId);
setChosenInstanceId(instanceId);
window.requestAnimationFrame(() => {
focusSearchInput();
});
Expand Down Expand Up @@ -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 {
Expand Down
26 changes: 26 additions & 0 deletions apps/web/src/components/chat/modelPickerOrigin.ts
Original file line number Diff line number Diff line change
@@ -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);
}
}
Loading