Skip to content
Open
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
9 changes: 8 additions & 1 deletion apps/web/src/components/chat/ModelListRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export const ModelListRow = memo(function ModelListRow(props: {
slug: string;
name: string;
subProvider?: string | undefined;
showSubProviderBadge?: boolean;
instanceId: ProviderInstanceId;
driverKind: ProviderDriverKind;
providerDisplayName: string;
Expand Down Expand Up @@ -83,7 +84,13 @@ export const ModelListRow = memo(function ModelListRow(props: {
</div>
{props.isSelected ? <CheckIcon className="size-3.5 shrink-0" aria-hidden="true" /> : null}
{props.subProvider ? (
<span className="min-w-0 shrink truncate text-xs text-muted-foreground/70">
<span
className={
props.showSubProviderBadge === false
? "sr-only"
: "min-w-0 shrink truncate text-xs text-muted-foreground/70"
}
>
{props.subProvider}
</span>
) : null}
Expand Down
169 changes: 130 additions & 39 deletions apps/web/src/components/chat/ModelPickerContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
type EnvironmentId,
type ProviderDriverKind,
type ResolvedKeybindingsConfig,
type ServerProviderSubProvider,
MODEL_PICKER_JUMP_KEYBINDING_COMMANDS,
} from "@t3tools/contracts";
import { resolveSelectableModel } from "@t3tools/shared/model";
Expand Down Expand Up @@ -466,14 +467,14 @@ export const ModelPickerContent = memo(function ModelPickerContent(props: {
: [],
),
);
// The thread's backend opens expanded, so its models show without a click.
const [expandedSubProviderKeys, setExpandedSubProviderKeys] = useState(
() =>
new Set<string>(
activeModel?.subProvider
? [modelPickerSubProviderSectionKey(props.activeInstanceId, activeModel.subProvider)]
: [],
),
const [subProviderStateAtOpen] = useState(() => ({
instanceId: props.activeInstanceId,
modelKey: activeModelKey,
subProvider: activeModel?.subProvider,
favorites: favoritesSet,
}));
const [subProviderExpansion, setSubProviderExpansion] = useState(
() => new Map<string, boolean>(),
);
const serverKeybindings = useAtomValue(primaryServerKeybindingsAtom);
const keybindings = providedKeybindings ?? serverKeybindings;
Expand Down Expand Up @@ -506,9 +507,11 @@ export const ModelPickerContent = memo(function ModelPickerContent(props: {
};

const toggleSubProviderSection = (key: string) => {
setExpandedSubProviderKeys((expanded) => {
const next = new Set(expanded);
if (!next.delete(key)) next.add(key);
const section = subProviderSectionByKey.get(key);
if (!section) return;
setSubProviderExpansion((expanded) => {
const next = new Map(expanded);
next.set(key, !(expanded.get(key) ?? section.expandedByDefault));
return next;
});
};
Expand Down Expand Up @@ -620,13 +623,51 @@ export const ModelPickerContent = memo(function ModelPickerContent(props: {
return out;
})();

const subProvidersByInstance = new Map(
instanceEntries.flatMap((entry) =>
entry.snapshot.subProviders?.length
? [[entry.instanceId, entry.snapshot.subProviders] as const]
: [],
),
);
const subProvidersByInstance = new Map<
ProviderInstanceId,
{ subProviders: ReadonlyArray<ServerProviderSubProvider>; fromOpenCodeCatalog: boolean }
>();
for (const entry of instanceEntries) {
if (entry.snapshot.subProviders?.length) {
subProvidersByInstance.set(entry.instanceId, {
subProviders: entry.snapshot.subProviders,
fromOpenCodeCatalog: false,
});
}
}
const openCodeVendorsByInstance = new Map<
ProviderInstanceId,
{ names: Set<string>; namesAtOpen: Set<string> }
>();
for (const model of flatModels) {
if (
model.driverKind !== "opencode" ||
!model.subProvider ||
model.isLegacy ||
subProvidersByInstance.has(model.instanceId)
) {
continue;
}
const vendors = openCodeVendorsByInstance.get(model.instanceId) ?? {
names: new Set<string>(),
namesAtOpen: new Set<string>(),
};
vendors.names.add(model.subProvider);
if (!subProviderStateAtOpen.favorites.has(providerModelKey(model.instanceId, model.slug))) {
vendors.namesAtOpen.add(model.subProvider);
}
openCodeVendorsByInstance.set(model.instanceId, vendors);
}
for (const [instanceId, vendors] of openCodeVendorsByInstance) {
const { names, namesAtOpen } = vendors;
if (namesAtOpen.size < 2) continue;
subProvidersByInstance.set(instanceId, {
subProviders: [...names]
.toSorted((a, b) => a.localeCompare(b, locale, { sensitivity: "base" }))
.map((name) => ({ name })),
fromOpenCodeCatalog: true,
});
}
const isLocked = lockedProvider !== null;
const isSearching = searchQuery.trim().length > 0;
// Fit the tallest tab so switching tabs never resizes the popup and moves the rail under the cursor.
Expand All @@ -651,8 +692,8 @@ export const ModelPickerContent = memo(function ModelPickerContent(props: {
}
}
for (const instanceId of tabsWithLegacyRow) addRow(instanceId);
for (const [instanceId, subProviders] of subProvidersByInstance) {
for (const _subProvider of subProviders) addRow(instanceId);
for (const [instanceId, grouping] of subProvidersByInstance) {
for (const _subProvider of grouping.subProviders) addRow(instanceId);
}
return Math.max(0, ...unitsByTab.values());
})();
Expand Down Expand Up @@ -745,30 +786,57 @@ export const ModelPickerContent = memo(function ModelPickerContent(props: {
};
})();

// A provider whose models run on several backends, such as fx, lists each
// backend as a collapsible heading. Search and Favorites stay flat.
// Search and Favorites stay flat, including models in collapsed sections.
const subProviderSections = (() => {
const subProviders =
isSearching || selectedInstanceId === "favorites"
? undefined
: subProvidersByInstance.get(selectedInstanceId);
if (!subProviders || selectedInstanceId === "favorites") return null;
if (isSearching || selectedInstanceId === "favorites") return null;
const grouping = subProvidersByInstance.get(selectedInstanceId);
if (!grouping) return null;
const { subProviders, fromOpenCodeCatalog } = grouping;
const openCodeLegacySection = fromOpenCodeCatalog ? legacySection : null;
const { ungrouped, groups } = groupModelsBySubProvider(
filteredModels,
openCodeLegacySection?.currentModels ?? filteredModels,
subProviders,
(model) => model.subProvider,
(model) =>
fromOpenCodeCatalog && favoritesSet.has(providerModelKey(model.instanceId, model.slug))
? undefined
: model.subProvider,
);
const activeSubProvider =
selectedInstanceId === subProviderStateAtOpen.instanceId
? fromOpenCodeCatalog
? filteredModels.find(
(model) =>
modelPickerModelKey(model.instanceId, model.slug) ===
subProviderStateAtOpen.modelKey,
)?.subProvider
: subProviderStateAtOpen.subProvider
: undefined;
const defaultSubProvider =
fromOpenCodeCatalog &&
(!activeSubProvider ||
!openCodeVendorsByInstance.get(selectedInstanceId)?.namesAtOpen.has(activeSubProvider))
? subProviders.find(({ name }) =>
openCodeVendorsByInstance.get(selectedInstanceId)?.namesAtOpen.has(name),
)?.name
: activeSubProvider;
return {
ungrouped,
sections: groups.map(({ subProvider, models }) => {
fromOpenCodeCatalog,
legacySection: openCodeLegacySection,
sections: groups.flatMap(({ subProvider, models }) => {
if (fromOpenCodeCatalog && models.length === 0) return [];
const key = modelPickerSubProviderSectionKey(selectedInstanceId, subProvider.name);
return {
key,
name: subProvider.name,
unavailableReason: subProvider.unavailableReason,
models,
isExpanded: expandedSubProviderKeys.has(key),
};
const expandedByDefault = subProvider.name === defaultSubProvider;
return [
{
key,
name: subProvider.name,
unavailableReason: subProvider.unavailableReason,
models,
expandedByDefault,
isExpanded: subProviderExpansion.get(key) ?? expandedByDefault,
},
];
}),
};
})();
Expand All @@ -783,6 +851,9 @@ export const ModelPickerContent = memo(function ModelPickerContent(props: {
...subProviderSections.sections.flatMap((section) =>
section.isExpanded ? section.models : [],
),
...(subProviderSections.legacySection?.isExpanded
? subProviderSections.legacySection.legacyModels
: []),
];
}
if (!legacySection) {
Expand Down Expand Up @@ -832,7 +903,7 @@ export const ModelPickerContent = memo(function ModelPickerContent(props: {
.filter((model) => model.isLegacy)
.map((model) => modelPickerLegacySectionKey(model.instanceId)),
),
...[...subProvidersByInstance].flatMap(([instanceId, subProviders]) =>
...[...subProvidersByInstance].flatMap(([instanceId, { subProviders }]) =>
subProviders.map((subProvider) =>
modelPickerSubProviderSectionKey(instanceId, subProvider.name),
),
Expand All @@ -847,6 +918,14 @@ export const ModelPickerContent = memo(function ModelPickerContent(props: {
section.key,
...(section.isExpanded ? section.models.map(keyOf) : []),
]),
...(subProviderSections.legacySection
? [
...(subProviderSections.legacySection.isExpanded
? subProviderSections.legacySection.legacyModels.map(keyOf)
: []),
subProviderSections.legacySection.key,
]
: []),
];
}
const modelKeys = visibleModels.map(keyOf);
Expand All @@ -873,6 +952,8 @@ export const ModelPickerContent = memo(function ModelPickerContent(props: {
const modelListExtraData = {
badgedInstanceIds,
favoritesSet,
legacySection,
subProviderSectionByKey,
modelJumpLabelByKey,
activeModelKey,
selectedModelKeySet,
Expand Down Expand Up @@ -1189,20 +1270,30 @@ export const ModelPickerContent = memo(function ModelPickerContent(props: {
}
const disabledReason =
getModelDisabledReason?.(model.instanceId, model.slug) ?? null;
const isFavorite = favoritesSet.has(providerModelKey(model.instanceId, model.slug));
const showSubProviderBadge = !(
subProviderSections?.fromOpenCodeCatalog &&
!isFavorite &&
!model.isLegacy &&
subProviderSectionByKey.has(
modelPickerSubProviderSectionKey(model.instanceId, model.subProvider ?? ""),
)
);
return (
<ModelListRow
key={modelKey}
index={index}
slug={model.slug}
name={model.name}
subProvider={model.subProvider}
showSubProviderBadge={showSubProviderBadge}
instanceId={model.instanceId}
driverKind={model.driverKind}
providerDisplayName={model.instanceDisplayName}
providerAccentColor={model.instanceAccentColor}
acpRegistryAgentId={model.acpRegistryAgentId}
acpRegistryIconUrl={model.acpRegistryIconUrl}
isFavorite={favoritesSet.has(providerModelKey(model.instanceId, model.slug))}
isFavorite={isFavorite}
isSelected={
selectedModelKeys !== undefined
? selectedModelKeySet.has(modelKey)
Expand Down
79 changes: 79 additions & 0 deletions apps/web/src/components/chat/ProviderModelPicker.browser.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,85 @@ it("keeps a docked picker open after a pick and hands focus to that model's effo
await expect.element(search).not.toBeInTheDocument();
});

it("folds OpenCode vendors into sections that open on click and stay searchable", async () => {
const instanceId = ProviderInstanceId.make("opencode");
const entries = deriveProviderInstanceEntries([
{
instanceId,
driver: ProviderDriverKind.make("opencode"),
enabled: true,
installed: true,
version: null,
status: "ready",
auth: { status: "authenticated" },
checkedAt: "2026-01-01T00:00:00.000Z",
models: [],
slashCommands: [],
skills: [],
},
]);
const options = new Map([
[
instanceId,
[
{ slug: "anthropic/claude-sonnet", name: "Claude Sonnet", subProvider: "Anthropic" },
{ slug: "openai/gpt-pro", name: "GPT Pro", subProvider: "OpenAI" },
{ slug: "openai/old-model", name: "Old model", subProvider: "OpenAI", isLegacy: true },
],
],
]);

function Picker() {
const [model, setModel] = useState("anthropic/claude-sonnet");
return (
<ProviderModelPicker
environmentId={null}
activeInstanceId={instanceId}
model={model}
lockedProvider={null}
instanceEntries={entries}
modelOptionsByInstance={options}
triggerAriaLabel="Choose model"
onInstanceModelChange={(_, selected) => setModel(selected)}
effortColumn={[]}
/>
);
}

await using screen = await render(<Picker />);
await screen.getByRole("button", { name: "Choose model" }).click();
const gptPro = page.getByRole("option", { name: /GPT Pro/ });
await expect
.element(page.getByRole("option", { name: /Claude Sonnet.*Anthropic/ }))
.toBeVisible();
await expect.element(gptPro).not.toBeInTheDocument();

await userEvent.keyboard("gpt");
await expect.element(gptPro).toBeVisible();
await userEvent.clear(page.getByPlaceholder("Search models..."));
await expect.element(gptPro).not.toBeInTheDocument();

const openAiSection = page.getByRole("option", { name: /^OpenAI/ });
await expect.element(openAiSection).toHaveAttribute("aria-expanded", "false");
await openAiSection.click();
await expect.element(gptPro).toBeVisible();
await expect.element(openAiSection).toHaveAttribute("aria-expanded", "true");

await gptPro.click();
await expect
.element(page.getByRole("option", { name: /^Anthropic/ }))
.toHaveAttribute("aria-expanded", "true");
await expect.element(page.getByRole("option", { name: /Claude Sonnet/ })).toBeVisible();

const oldModel = page.getByRole("option", { name: /Old model/ });
await expect.element(oldModel).not.toBeInTheDocument();
await page.getByRole("option", { name: "Show more" }).click();
await expect.element(oldModel).toBeVisible();
await expect
.element(page.getByRole("option", { name: "Show less" }))
.toHaveAttribute("aria-expanded", "true");
});

it("lists a model reported twice by one provider once, with contiguous jump shortcuts", async () => {
const instanceId = ProviderInstanceId.make("claude");
const entries = deriveProviderInstanceEntries([
Expand Down
Loading