Skip to content

Commit e46f144

Browse files
fix(web): keep legacy favorites in the model picker list
1 parent 5caf3c8 commit e46f144

4 files changed

Lines changed: 141 additions & 113 deletions

File tree

‎apps/web/src/components/chat/ModelPickerContent.tsx‎

Lines changed: 17 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,7 @@ import {
6161
isProviderInstancePickerVisible,
6262
type ProviderInstanceEntry,
6363
} from "../../providerInstances";
64-
import {
65-
partitionLegacyModels,
66-
providerModelKey,
67-
sortProviderModelItems,
68-
} from "../../modelOrdering";
64+
import { providerModelKey, sortProviderModelItems } from "../../modelOrdering";
6965

7066
type ModelPickerItem = {
7167
slug: string;
@@ -434,22 +430,15 @@ export const ModelPickerContent = memo(function ModelPickerContent(props: {
434430
},
435431
);
436432
const favoritesSet = new Set(favorites.map((fav) => providerModelKey(fav.provider, fav.model)));
437-
const [expandedLegacyInstances, setExpandedLegacyInstances] = useState(() => {
438-
// Auto-expand the legacy group only when the active model actually lives
439-
// there. A favorited legacy model is hoisted into the main list, so
440-
// expanding the group for it would open an unrelated section.
441-
const activeIsFavorite = favoritesSet.has(
442-
providerModelKey(props.activeInstanceId, activeModelSlug),
443-
);
444-
return new Set<ProviderInstanceId>(
445-
!activeIsFavorite &&
446-
modelOptionsByInstance
447-
.get(props.activeInstanceId)
448-
?.some((model) => model.slug === activeModelSlug && model.isLegacy)
449-
? [props.activeInstanceId]
450-
: [],
451-
);
452-
});
433+
const [expandedLegacyInstances, setExpandedLegacyInstances] = useState(
434+
() =>
435+
new Set<ProviderInstanceId>(
436+
activeModel?.isLegacy &&
437+
!favoritesSet.has(providerModelKey(props.activeInstanceId, activeModelSlug))
438+
? [props.activeInstanceId]
439+
: [],
440+
),
441+
);
453442
const serverKeybindings = useAtomValue(primaryServerKeybindingsAtom);
454443
const keybindings = providedKeybindings ?? serverKeybindings;
455444
const [searchHeight, setSearchHeight] = useState(0);
@@ -668,14 +657,13 @@ export const ModelPickerContent = memo(function ModelPickerContent(props: {
668657
if (isSearching || selectedInstanceId === "favorites") {
669658
return null;
670659
}
671-
// Favorited models are hoisted to the top, and that hoist has to win over
672-
// the legacy split: a favorited legacy model stays in the main list rather
673-
// than being buried in the collapsed legacy group. Unfavoriting it drops it
674-
// back into the legacy section on the next render.
675-
const { current: currentModels, legacy: legacyModels } = partitionLegacyModels(
676-
filteredModels,
677-
(model) => favoritesSet.has(providerModelKey(model.instanceId, model.slug)),
678-
);
660+
const currentModels: ModelPickerItem[] = [];
661+
const legacyModels: ModelPickerItem[] = [];
662+
for (const model of filteredModels) {
663+
const isLegacy =
664+
model.isLegacy && !favoritesSet.has(providerModelKey(model.instanceId, model.slug));
665+
(isLegacy ? legacyModels : currentModels).push(model);
666+
}
679667
if (legacyModels.length === 0) {
680668
return null;
681669
}

‎apps/web/src/components/chat/ProviderModelPicker.browser.test.tsx‎

Lines changed: 124 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,47 @@ import {
44
ProviderInstanceId,
55
type ServerProvider,
66
} from "@t3tools/contracts";
7+
import { RegistryContext, useAtomSet, useAtomValue } from "@effect/atom-react";
8+
import { Atom, AtomRegistry } from "effect/reactivity";
79
import { act, type ReactNode } from "react";
8-
import { createRoot } from "react-dom/client";
10+
import { createRoot, type Root } from "react-dom/client";
911
import { afterEach, describe, expect, it, vi } from "vite-plus/test";
12+
import { page, userEvent } from "vite-plus/test/browser";
13+
14+
import "~/index.css";
1015

1116
import { deriveProviderInstanceEntries } from "../../providerInstances";
1217
import { ProviderModelPicker } from "./ProviderModelPicker";
1318
import type { ModelEsque } from "./providerIconUtils";
1419

20+
const favoritesAtom = Atom.make<ReadonlyArray<{ provider: ProviderInstanceId; model: string }>>(
21+
[],
22+
).pipe(Atom.keepAlive);
23+
24+
vi.mock(import("~/hooks/useSettings"), async (original) => ({
25+
...(await original()),
26+
useEnvironmentModelPreferences: () => ({
27+
favorites: useAtomValue(favoritesAtom),
28+
providerModelPreferences: {},
29+
}),
30+
useToggleEnvironmentModelFavorite: () => {
31+
const setFavorites = useAtomSet(favoritesAtom);
32+
return async (provider: ProviderInstanceId, model: string) => {
33+
setFavorites((favorites) =>
34+
favorites.filter((favorite) => favorite.provider !== provider || favorite.model !== model),
35+
);
36+
};
37+
},
38+
}));
39+
40+
vi.mock(import("@tanstack/react-router"), async (original) => ({
41+
...(await original()),
42+
useNavigate: () => vi.fn(),
43+
}));
44+
45+
let pickerRoot: Root | undefined;
46+
let pickerRegistry: AtomRegistry.AtomRegistry | undefined;
47+
1548
function providerEntry(instanceId: string, driver: string) {
1649
const provider: ServerProvider = {
1750
instanceId: ProviderInstanceId.make(instanceId),
@@ -29,7 +62,11 @@ function providerEntry(instanceId: string, driver: string) {
2962
return deriveProviderInstanceEntries([provider])[0]!;
3063
}
3164

32-
afterEach(() => {
65+
afterEach(async () => {
66+
if (pickerRoot) await act(async () => pickerRoot!.unmount());
67+
pickerRoot = undefined;
68+
pickerRegistry?.dispose();
69+
pickerRegistry = undefined;
3370
document.body.replaceChildren();
3471
vi.unstubAllGlobals();
3572
});
@@ -70,7 +107,92 @@ function renderPicker(input: {
70107
);
71108
}
72109

110+
async function click(target: Parameters<typeof userEvent.click>[0]) {
111+
await act(async () => userEvent.click(target));
112+
}
113+
114+
async function openLegacyPicker(activeModel: string) {
115+
vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
116+
const entry = providerEntry("codex", "codex");
117+
pickerRegistry = AtomRegistry.make();
118+
pickerRegistry.set(favoritesAtom, [
119+
{ provider: entry.instanceId, model: "current-favorite" },
120+
{ provider: entry.instanceId, model: "legacy-favorite" },
121+
]);
122+
pickerRoot = createRoot(document.body.appendChild(document.createElement("div")));
123+
await act(async () =>
124+
pickerRoot!.render(
125+
<RegistryContext.Provider value={pickerRegistry!}>
126+
<ProviderModelPicker
127+
environmentId={null}
128+
activeInstanceId={entry.instanceId}
129+
model={activeModel}
130+
lockedProvider={null}
131+
instanceEntries={[entry]}
132+
modelOptionsByInstance={
133+
new Map([
134+
[
135+
entry.instanceId,
136+
[
137+
{ slug: "current", name: "Current model" },
138+
{ slug: "legacy-favorite", name: "Legacy favorite", isLegacy: true },
139+
{ slug: "current-favorite", name: "Current favorite" },
140+
{ slug: "other-legacy", name: "Other legacy", isLegacy: true },
141+
],
142+
],
143+
])
144+
}
145+
onInstanceModelChange={() => {}}
146+
/>
147+
</RegistryContext.Provider>,
148+
),
149+
);
150+
await click(document.querySelector("button")!);
151+
await click(page.getByRole("button", { name: entry.displayName, exact: true }));
152+
}
153+
73154
describe("ProviderModelPicker", () => {
155+
it("hoists legacy favorites in provider order and returns an unfavorited model to legacy", async () => {
156+
await openLegacyPicker("current");
157+
const legacyFavorite = page.getByRole("option", { name: /Legacy favorite/ });
158+
const currentFavorite = page.getByRole("option", { name: /Current favorite/ });
159+
const current = page.getByRole("option", { name: /Current model/ });
160+
const legacySection = page.getByRole("option", { name: /Legacy models/ });
161+
162+
await expect.element(legacyFavorite).toBeVisible();
163+
await expect.element(currentFavorite).toBeVisible();
164+
await expect.element(current).toBeVisible();
165+
expect(legacyFavorite.element().getBoundingClientRect().top).toBeLessThan(
166+
currentFavorite.element().getBoundingClientRect().top,
167+
);
168+
expect(currentFavorite.element().getBoundingClientRect().top).toBeLessThan(
169+
current.element().getBoundingClientRect().top,
170+
);
171+
172+
await click(legacyFavorite.getByRole("button", { name: "Remove from favorites" }));
173+
await expect.element(legacyFavorite).not.toBeInTheDocument();
174+
await click(legacySection);
175+
await expect.element(legacyFavorite).toBeVisible();
176+
await expect.element(page.getByRole("option", { name: /Other legacy/ })).toBeVisible();
177+
expect(legacyFavorite.element().getBoundingClientRect().top).toBeGreaterThan(
178+
legacySection.element().getBoundingClientRect().top,
179+
);
180+
await expect
181+
.element(legacyFavorite.getByRole("button", { name: "Add to favorites" }))
182+
.toBeVisible();
183+
});
184+
185+
it("keeps unrelated legacy models collapsed when the active model is a favorite", async () => {
186+
await openLegacyPicker("legacy-favorite");
187+
188+
await expect.element(page.getByRole("option", { name: /Legacy favorite/ })).toBeVisible();
189+
await expect
190+
.element(page.getByRole("option", { name: /Other legacy/ }))
191+
.not.toBeInTheDocument();
192+
await click(page.getByRole("option", { name: /Legacy models/ }));
193+
await expect.element(page.getByRole("option", { name: /Other legacy/ })).toBeVisible();
194+
});
195+
74196
it("shows a neutral aggregate value without a representative model or availability badge", async () => {
75197
const label = await renderPicker({
76198
instanceId: "codex_personal",

‎apps/web/src/modelOrdering.test.ts‎

Lines changed: 0 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { describe, expect, it } from "vite-plus/test";
22
import { ProviderInstanceId } from "@t3tools/contracts";
33

44
import {
5-
partitionLegacyModels,
65
providerModelKey,
76
sortModelsForProviderInstance,
87
sortProviderModelItems,
@@ -50,59 +49,4 @@ describe("model ordering", () => {
5049
}).map((item) => item.slug),
5150
).toEqual(["gpt-5.4-mini", "gpt-5.5", "crest-alpha", "claude-opus-4-6"]);
5251
});
53-
54-
describe("partitionLegacyModels", () => {
55-
const models = [
56-
{ slug: "opus-5" },
57-
{ slug: "sonnet-5" },
58-
{ slug: "opus-4-8", isLegacy: true },
59-
{ slug: "haiku-3", isLegacy: true },
60-
];
61-
62-
it("keeps a favorited legacy model in the main list instead of the legacy group", () => {
63-
const { current, legacy } = partitionLegacyModels(
64-
models,
65-
(model) => model.slug === "opus-4-8",
66-
);
67-
68-
expect(current.map((model) => model.slug)).toEqual(["opus-5", "sonnet-5", "opus-4-8"]);
69-
expect(legacy.map((model) => model.slug)).toEqual(["haiku-3"]);
70-
});
71-
72-
it("returns legacy models to the legacy group once unfavorited", () => {
73-
const { current, legacy } = partitionLegacyModels(models, () => false);
74-
75-
expect(current.map((model) => model.slug)).toEqual(["opus-5", "sonnet-5"]);
76-
expect(legacy.map((model) => model.slug)).toEqual(["opus-4-8", "haiku-3"]);
77-
});
78-
79-
it("returns empty partitions for an empty list", () => {
80-
const { current, legacy } = partitionLegacyModels([], () => false);
81-
82-
expect(current).toEqual([]);
83-
expect(legacy).toEqual([]);
84-
});
85-
86-
it("treats an explicit isLegacy: false as a current model", () => {
87-
const { current, legacy } = partitionLegacyModels(
88-
[{ slug: "opus-5", isLegacy: false }],
89-
() => false,
90-
);
91-
92-
expect(current.map((model) => model.slug)).toEqual(["opus-5"]);
93-
expect(legacy).toEqual([]);
94-
});
95-
96-
it("leaves the legacy group empty when every legacy model is favorited", () => {
97-
const { current, legacy } = partitionLegacyModels(models, (model) => model.isLegacy === true);
98-
99-
expect(current.map((model) => model.slug)).toEqual([
100-
"opus-5",
101-
"sonnet-5",
102-
"opus-4-8",
103-
"haiku-3",
104-
]);
105-
expect(legacy).toEqual([]);
106-
});
107-
});
10852
});

‎apps/web/src/modelOrdering.ts‎

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -55,32 +55,6 @@ export function sortModelsForProviderInstance<T extends ModelSlugItem>(
5555
return Arr.sort(models, Order.combineAll(orders));
5656
}
5757

58-
export interface LegacyPartitionItem {
59-
readonly isLegacy?: boolean | undefined;
60-
}
61-
62-
/**
63-
* Split an already-sorted model list into the main list and the collapsed
64-
* legacy group. Favorited models stay in the main list even when they are
65-
* legacy, so the favorite hoist wins over the legacy split; unfavoriting a
66-
* legacy model returns it to the legacy group. `isFavorite` decides membership.
67-
*/
68-
export function partitionLegacyModels<T extends LegacyPartitionItem>(
69-
models: ReadonlyArray<T>,
70-
isFavorite: (model: T) => boolean,
71-
): { readonly current: T[]; readonly legacy: T[] } {
72-
const current: T[] = [];
73-
const legacy: T[] = [];
74-
for (const model of models) {
75-
if (model.isLegacy === true && !isFavorite(model)) {
76-
legacy.push(model);
77-
} else {
78-
current.push(model);
79-
}
80-
}
81-
return { current, legacy };
82-
}
83-
8458
export function sortProviderModelItems<T extends ProviderModelItem>(
8559
items: ReadonlyArray<T>,
8660
options?: {

0 commit comments

Comments
 (0)