fix(opencode): handle child approvals, stops, and model catalogs (#8480)
This commit is contained in:
1 parent
0009aacdf1
commit
cb49e5d720
42 files changed
+7889
-581
No files matched your search
@@ -997,16 +997,24 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
|
||||
// Instance-keyed option list so the picker can show each configured
|
||||
// instance (built-in + custom) as a first-class sidebar entry. The
|
||||
// options are server-reported models plus that exact instance's
|
||||
// configured custom models; selected slugs are not injected into lists.
|
||||
// configured custom models. A missing OpenCode selection is included as
|
||||
// an unavailable row until the catalog reports it again.
|
||||
const modelOptionsByInstance = useMemo<
|
||||
ReadonlyMap<ProviderInstanceId, ReadonlyArray<AppModelOption>>
|
||||
>(() => {
|
||||
const out = new Map<ProviderInstanceId, ReadonlyArray<AppModelOption>>();
|
||||
for (const entry of providerInstanceEntries) {
|
||||
out.set(entry.instanceId, getAppModelOptionsForInstance(settings, entry));
|
||||
out.set(
|
||||
entry.instanceId,
|
||||
getAppModelOptionsForInstance(
|
||||
settings,
|
||||
entry,
|
||||
entry.instanceId === selectedInstanceId ? selectedModelForPicker : null,
|
||||
),
|
||||
);
|
||||
}
|
||||
return out;
|
||||
}, [providerInstanceEntries, settings]);
|
||||
}, [providerInstanceEntries, selectedInstanceId, selectedModelForPicker, settings]);
|
||||
const selectedModelForPickerWithCustomFallback = useMemo(() => {
|
||||
const currentOptions = modelOptionsByInstance.get(selectedInstanceId) ?? [];
|
||||
return currentOptions.some((option) => option.slug === selectedModelForPicker)
|
||||
|
||||
@@ -9,6 +9,7 @@ import {
|
||||
} from "./providerIconUtils";
|
||||
import { ComboboxItem } from "../ui/combobox";
|
||||
import { Button } from "../ui/button";
|
||||
import { Badge } from "../ui/badge";
|
||||
import { Kbd } from "../ui/kbd";
|
||||
import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip";
|
||||
import { cn } from "~/lib/utils";
|
||||
@@ -34,6 +35,7 @@ export const ModelListRow = memo(function ModelListRow(props: {
|
||||
preferShortName?: boolean;
|
||||
useTriggerLabel?: boolean;
|
||||
showNewBadge?: boolean;
|
||||
unavailable?: boolean;
|
||||
jumpLabel?: string | null;
|
||||
disabledReason?: string | null;
|
||||
onToggleFavorite: () => void;
|
||||
@@ -75,6 +77,11 @@ export const ModelListRow = memo(function ModelListRow(props: {
|
||||
New
|
||||
</span>
|
||||
) : null}
|
||||
{props.unavailable ? (
|
||||
<Badge variant="outline" size="sm">
|
||||
Unavailable
|
||||
</Badge>
|
||||
) : null}
|
||||
</div>
|
||||
{props.showProvider && (
|
||||
<div className="mt-1 flex items-center gap-1.5">
|
||||
|
||||
@@ -0,0 +1,67 @@
|
||||
import { ProviderDriverKind, ProviderInstanceId, type ServerProvider } from "@t3tools/contracts";
|
||||
import { describe, expect, it } from "vite-plus/test";
|
||||
|
||||
import { deriveProviderInstanceEntries } from "../../providerInstances";
|
||||
import { shouldIncludeModelPickerOption } from "./ModelPickerContent";
|
||||
|
||||
function entry(status: ServerProvider["status"]) {
|
||||
return deriveProviderInstanceEntries([
|
||||
{
|
||||
instanceId: ProviderInstanceId.make("opencode_work"),
|
||||
driver: ProviderDriverKind.make("opencode"),
|
||||
enabled: true,
|
||||
installed: true,
|
||||
version: null,
|
||||
status,
|
||||
auth: { status: "authenticated" },
|
||||
checkedAt: "2026-08-28T00:00:00.000Z",
|
||||
models: [],
|
||||
slashCommands: [],
|
||||
skills: [],
|
||||
},
|
||||
])[0]!;
|
||||
}
|
||||
|
||||
describe("shouldIncludeModelPickerOption", () => {
|
||||
it.each(["error", "warning"] as const)(
|
||||
"keeps only the active synthetic OpenCode row when the provider status is %s",
|
||||
(status) => {
|
||||
const providerEntry = entry(status);
|
||||
const activeInstanceId = ProviderInstanceId.make("opencode_work");
|
||||
const activeModel = "openrouter/kimi-k3";
|
||||
|
||||
expect(
|
||||
shouldIncludeModelPickerOption({
|
||||
entry: providerEntry,
|
||||
option: {
|
||||
slug: activeModel,
|
||||
name: activeModel,
|
||||
isUnavailable: true,
|
||||
},
|
||||
activeInstanceId,
|
||||
activeModel,
|
||||
}),
|
||||
).toBe(true);
|
||||
expect(
|
||||
shouldIncludeModelPickerOption({
|
||||
entry: providerEntry,
|
||||
option: { slug: "stale/model", name: "Stale model" },
|
||||
activeInstanceId,
|
||||
activeModel,
|
||||
}),
|
||||
).toBe(false);
|
||||
expect(
|
||||
shouldIncludeModelPickerOption({
|
||||
entry: providerEntry,
|
||||
option: {
|
||||
slug: "other/missing",
|
||||
name: "Other missing",
|
||||
isUnavailable: true,
|
||||
},
|
||||
activeInstanceId,
|
||||
activeModel,
|
||||
}),
|
||||
).toBe(false);
|
||||
},
|
||||
);
|
||||
});
|
||||
@@ -53,8 +53,25 @@ type ModelPickerItem = {
|
||||
instanceAccentColor?: string | undefined;
|
||||
continuationGroupKey?: string | undefined;
|
||||
isLegacy?: boolean | undefined;
|
||||
isUnavailable?: boolean | undefined;
|
||||
};
|
||||
|
||||
export function shouldIncludeModelPickerOption(input: {
|
||||
readonly entry: ProviderInstanceEntry;
|
||||
readonly option: ModelEsque;
|
||||
readonly activeInstanceId: ProviderInstanceId;
|
||||
readonly activeModel: string;
|
||||
}): boolean {
|
||||
if (isProviderInstancePickerReady(input.entry)) return true;
|
||||
return (
|
||||
input.entry.enabled &&
|
||||
input.entry.driverKind === "opencode" &&
|
||||
input.entry.instanceId === input.activeInstanceId &&
|
||||
input.option.slug === input.activeModel &&
|
||||
input.option.isUnavailable === true
|
||||
);
|
||||
}
|
||||
|
||||
const EMPTY_MODEL_JUMP_LABELS = new Map<string, string>();
|
||||
|
||||
function ModelListSeparator() {
|
||||
@@ -107,9 +124,23 @@ export const ModelPickerContent = memo(function ModelPickerContent(props: {
|
||||
const modelListRef = useRef<LegendListRef | null>(null);
|
||||
const highlightedModelKeyRef = useRef<string | null>(null);
|
||||
const favorites = useClientSettings((s) => s.favorites ?? []);
|
||||
const activeEntry = props.instanceEntries.find(
|
||||
(entry) => entry.instanceId === props.activeInstanceId,
|
||||
);
|
||||
const activeInstanceHasSelectableUnavailableModel =
|
||||
activeEntry !== undefined &&
|
||||
(modelOptionsByInstance.get(props.activeInstanceId) ?? []).some((option) =>
|
||||
shouldIncludeModelPickerOption({
|
||||
entry: activeEntry,
|
||||
option,
|
||||
activeInstanceId: props.activeInstanceId,
|
||||
activeModel: props.model,
|
||||
}),
|
||||
) &&
|
||||
!isProviderInstancePickerReady(activeEntry);
|
||||
const [selectedInstanceId, setSelectedInstanceId] = useState<ProviderInstanceId | "favorites">(
|
||||
() => {
|
||||
if (props.lockedProvider !== null) {
|
||||
if (props.lockedProvider !== null || activeInstanceHasSelectableUnavailableModel) {
|
||||
// When locked, prime the sidebar to the currently-active instance
|
||||
// so jumping into the picker keeps the focused instance visible.
|
||||
return props.activeInstanceId;
|
||||
@@ -189,15 +220,12 @@ export const ModelPickerContent = memo(function ModelPickerContent(props: {
|
||||
[props.lockedContinuationGroupKey, props.lockedProvider],
|
||||
);
|
||||
|
||||
const readyInstanceSet = useMemo(() => {
|
||||
const ready = new Set<ProviderInstanceId>();
|
||||
for (const entry of instanceEntries) {
|
||||
if (isProviderInstancePickerReady(entry)) {
|
||||
ready.add(entry.instanceId);
|
||||
}
|
||||
const selectableUnavailableInstanceIds = useMemo(() => {
|
||||
if (!activeInstanceHasSelectableUnavailableModel) {
|
||||
return undefined;
|
||||
}
|
||||
return ready;
|
||||
}, [instanceEntries]);
|
||||
return new Set([props.activeInstanceId]);
|
||||
}, [activeInstanceHasSelectableUnavailableModel, props.activeInstanceId]);
|
||||
|
||||
// Flatten models into a searchable array. One pass over the
|
||||
// instance-keyed map; each model carries its instance id + driver kind
|
||||
@@ -212,16 +240,24 @@ export const ModelPickerContent = memo(function ModelPickerContent(props: {
|
||||
// its models — stale options shouldn't appear in the picker.
|
||||
continue;
|
||||
}
|
||||
if (!readyInstanceSet.has(instanceId)) {
|
||||
continue;
|
||||
}
|
||||
for (const model of models) {
|
||||
if (
|
||||
!shouldIncludeModelPickerOption({
|
||||
entry,
|
||||
option: model,
|
||||
activeInstanceId: props.activeInstanceId,
|
||||
activeModel: props.model,
|
||||
})
|
||||
) {
|
||||
continue;
|
||||
}
|
||||
out.push({
|
||||
slug: model.slug,
|
||||
name: model.name,
|
||||
...(model.shortName ? { shortName: model.shortName } : {}),
|
||||
...(model.subProvider ? { subProvider: model.subProvider } : {}),
|
||||
...(model.isLegacy ? { isLegacy: true } : {}),
|
||||
...(model.isUnavailable ? { isUnavailable: true } : {}),
|
||||
instanceId,
|
||||
driverKind: entry.driverKind,
|
||||
instanceDisplayName: entry.displayName,
|
||||
@@ -233,7 +269,7 @@ export const ModelPickerContent = memo(function ModelPickerContent(props: {
|
||||
}
|
||||
}
|
||||
return out;
|
||||
}, [modelOptionsByInstance, entryByInstanceId, readyInstanceSet]);
|
||||
}, [modelOptionsByInstance, entryByInstanceId, props.activeInstanceId, props.model]);
|
||||
|
||||
const isLocked = props.lockedProvider !== null;
|
||||
const isSearching = searchQuery.trim().length > 0;
|
||||
@@ -609,6 +645,7 @@ export const ModelPickerContent = memo(function ModelPickerContent(props: {
|
||||
onSelectInstance={handleSelectInstance}
|
||||
instanceEntries={sidebarInstanceEntries}
|
||||
showFavorites
|
||||
{...(selectableUnavailableInstanceIds ? { selectableUnavailableInstanceIds } : {})}
|
||||
{...(lockedDisabledInstanceIds
|
||||
? {
|
||||
disabledInstanceIds: lockedDisabledInstanceIds,
|
||||
@@ -768,6 +805,7 @@ export const ModelPickerContent = memo(function ModelPickerContent(props: {
|
||||
preferShortName={!isLocked}
|
||||
useTriggerLabel={false}
|
||||
showNewBadge={isModelPickerNewModel(model.driverKind, model.slug)}
|
||||
unavailable={model.isUnavailable === true}
|
||||
jumpLabel={modelJumpLabelByKey.get(modelKey) ?? null}
|
||||
disabledReason={disabledReason}
|
||||
onToggleFavorite={() => toggleFavorite(model.instanceId, model.slug)}
|
||||
|
||||
@@ -54,6 +54,8 @@ export const ModelPickerSidebar = memo(function ModelPickerSidebar(props: {
|
||||
showFavorites?: boolean;
|
||||
/** Instance ids shown in the rail but unavailable for the current picker context. */
|
||||
disabledInstanceIds?: ReadonlySet<ProviderInstanceId>;
|
||||
/** Non-ready instances whose selected unavailable model remains reachable. */
|
||||
selectableUnavailableInstanceIds?: ReadonlySet<ProviderInstanceId>;
|
||||
getDisabledInstanceTooltip?: (entry: ProviderInstanceEntry) => string;
|
||||
/**
|
||||
* Instance id values that should render the "new" sparkle badge. Callers
|
||||
@@ -135,7 +137,10 @@ export const ModelPickerSidebar = memo(function ModelPickerSidebar(props: {
|
||||
{props.instanceEntries.map((entry) => {
|
||||
const isUnavailable = !isProviderInstancePickerReady(entry);
|
||||
const isContextDisabled = props.disabledInstanceIds?.has(entry.instanceId) ?? false;
|
||||
const isDisabled = isUnavailable || isContextDisabled;
|
||||
const unavailableSelectionIsReachable =
|
||||
props.selectableUnavailableInstanceIds?.has(entry.instanceId) ?? false;
|
||||
const isDisabled =
|
||||
(isUnavailable && !unavailableSelectionIsReachable) || isContextDisabled;
|
||||
const isSelected = props.selectedInstanceId === entry.instanceId;
|
||||
const isHovered = hoveredInstanceId === entry.instanceId;
|
||||
const showNewBadge = props.newBadgeInstanceIds?.has(entry.instanceId) ?? false;
|
||||
@@ -168,7 +173,7 @@ export const ModelPickerSidebar = memo(function ModelPickerSidebar(props: {
|
||||
disabled={isDisabled}
|
||||
type="button"
|
||||
aria-label={
|
||||
isDisabled
|
||||
isUnavailable || isContextDisabled
|
||||
? tooltip
|
||||
: showNewBadge
|
||||
? `${entry.displayName}, new`
|
||||
|
||||
@@ -0,0 +1,102 @@
|
||||
import { ProviderDriverKind, ProviderInstanceId, type ServerProvider } from "@t3tools/contracts";
|
||||
import { renderToStaticMarkup } from "react-dom/server";
|
||||
import { describe, expect, it } from "vite-plus/test";
|
||||
|
||||
import { deriveProviderInstanceEntries } from "../../providerInstances";
|
||||
import { ProviderModelPicker } from "./ProviderModelPicker";
|
||||
import type { ModelEsque } from "./providerIconUtils";
|
||||
|
||||
function providerEntry(instanceId: string, driver: string) {
|
||||
const provider: ServerProvider = {
|
||||
instanceId: ProviderInstanceId.make(instanceId),
|
||||
driver: ProviderDriverKind.make(driver),
|
||||
enabled: true,
|
||||
installed: true,
|
||||
version: null,
|
||||
status: "ready",
|
||||
auth: { status: "authenticated" },
|
||||
checkedAt: "2026-08-28T00:00:00.000Z",
|
||||
models: [],
|
||||
slashCommands: [],
|
||||
skills: [],
|
||||
};
|
||||
return deriveProviderInstanceEntries([provider])[0]!;
|
||||
}
|
||||
|
||||
function renderPicker(input: {
|
||||
instanceId: string;
|
||||
driver: string;
|
||||
model: string;
|
||||
options: ReadonlyArray<ModelEsque>;
|
||||
includeEntry?: boolean;
|
||||
}) {
|
||||
const instanceId = ProviderInstanceId.make(input.instanceId);
|
||||
const entry = providerEntry(input.instanceId, input.driver);
|
||||
return renderToStaticMarkup(
|
||||
<ProviderModelPicker
|
||||
activeInstanceId={instanceId}
|
||||
model={input.model}
|
||||
lockedProvider={null}
|
||||
instanceEntries={input.includeEntry === false ? [] : [entry]}
|
||||
modelOptionsByInstance={new Map([[instanceId, input.options]])}
|
||||
onInstanceModelChange={() => {}}
|
||||
/>,
|
||||
);
|
||||
}
|
||||
|
||||
describe("ProviderModelPicker", () => {
|
||||
it("shows a missing model slug for a custom OpenCode instance", () => {
|
||||
const markup = renderPicker({
|
||||
instanceId: "team_runtime",
|
||||
driver: "opencode",
|
||||
model: "openrouter/missing-model",
|
||||
options: [{ slug: "openrouter/fallback", name: "Fallback model" }],
|
||||
});
|
||||
|
||||
expect(markup).toContain("openrouter/missing-model");
|
||||
expect(markup).not.toContain("Fallback model");
|
||||
});
|
||||
|
||||
it.each(["codex", "claudeAgent", "cursor", "grok"])(
|
||||
"uses the first option label for a missing %s model",
|
||||
(driver) => {
|
||||
const markup = renderPicker({
|
||||
instanceId: `${driver}_work`,
|
||||
driver,
|
||||
model: "missing-model",
|
||||
options: [{ slug: "fallback-model", name: "Fallback model" }],
|
||||
});
|
||||
|
||||
expect(markup).toContain("Fallback model");
|
||||
expect(markup).not.toContain(">missing-model<");
|
||||
},
|
||||
);
|
||||
|
||||
it("prefers a matching model for OpenCode", () => {
|
||||
const markup = renderPicker({
|
||||
instanceId: "custom_runtime",
|
||||
driver: "opencode",
|
||||
model: "openrouter/selected",
|
||||
options: [
|
||||
{ slug: "openrouter/fallback", name: "Fallback model" },
|
||||
{ slug: "openrouter/selected", name: "Selected model" },
|
||||
],
|
||||
});
|
||||
|
||||
expect(markup).toContain("Selected model");
|
||||
expect(markup).not.toContain("Fallback model");
|
||||
});
|
||||
|
||||
it("uses the first option when the active instance entry is missing", () => {
|
||||
const markup = renderPicker({
|
||||
instanceId: "missing_instance",
|
||||
driver: "opencode",
|
||||
model: "missing-model",
|
||||
options: [{ slug: "fallback-model", name: "Fallback model" }],
|
||||
includeEntry: false,
|
||||
});
|
||||
|
||||
expect(markup).toContain("Fallback model");
|
||||
expect(markup).not.toContain(">missing-model<");
|
||||
});
|
||||
});
|
||||
@@ -5,6 +5,7 @@ import {
|
||||
} from "@t3tools/contracts";
|
||||
import { memo, useEffect, useMemo, useState } from "react";
|
||||
import type { VariantProps } from "class-variance-authority";
|
||||
import { Badge } from "../ui/badge";
|
||||
import { buttonVariants } from "../ui/button";
|
||||
import { Popover, PopoverPopup, PopoverTrigger } from "../ui/popover";
|
||||
import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip";
|
||||
@@ -58,15 +59,15 @@ export const ProviderModelPicker = memo(function ProviderModelPicker(props: {
|
||||
|
||||
const activeInstanceId = props.activeInstanceId;
|
||||
const selectedInstanceOptions = props.modelOptionsByInstance.get(activeInstanceId) ?? [];
|
||||
// If the current slug belongs to a different instance (for example after
|
||||
// a provider switch or disable), prefer the active instance's first
|
||||
// option so the trigger icon and label stay in sync instead of showing
|
||||
// a stale foreign slug.
|
||||
// OpenCode can keep a model through a transient catalog refresh. Other
|
||||
// providers keep the active instance's first option as their normal fallback.
|
||||
const selectedModel =
|
||||
selectedInstanceOptions.find((option) => option.slug === props.model) ??
|
||||
selectedInstanceOptions[0];
|
||||
(activeEntry?.driverKind === "opencode" ? undefined : selectedInstanceOptions[0]);
|
||||
const triggerTitle = selectedModel ? getTriggerDisplayModelName(selectedModel) : props.model;
|
||||
const triggerLabel = selectedModel ? getTriggerDisplayModelLabel(selectedModel) : props.model;
|
||||
const triggerLabel = selectedModel
|
||||
? `${getTriggerDisplayModelLabel(selectedModel)}${selectedModel.isUnavailable ? " (Unavailable)" : ""}`
|
||||
: props.model;
|
||||
const showInstanceBadge =
|
||||
activeEntry !== null && shouldShowInstanceBadge(activeEntry, props.instanceEntries);
|
||||
|
||||
@@ -179,6 +180,11 @@ export const ProviderModelPicker = memo(function ProviderModelPicker(props: {
|
||||
</TooltipTrigger>
|
||||
<TooltipPopup side="top">{triggerLabel}</TooltipPopup>
|
||||
</Tooltip>
|
||||
{selectedModel?.isUnavailable ? (
|
||||
<Badge variant="outline" size="sm">
|
||||
Unavailable
|
||||
</Badge>
|
||||
) : null}
|
||||
</span>
|
||||
<span aria-hidden="true" className="flex items-center">
|
||||
<ComposerControlChevron />
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { describe, expect, it } from "vite-plus/test";
|
||||
import { ProviderDriverKind, type ProviderOptionDescriptor } from "@t3tools/contracts";
|
||||
import { buildTraitsTriggerDisplay } from "./TraitsPicker";
|
||||
import { buildTraitsTriggerDisplay, buildUnavailableModelOptionDescriptors } from "./TraitsPicker";
|
||||
|
||||
function selectDescriptor(
|
||||
id: string,
|
||||
@@ -155,3 +155,36 @@ describe("buildTraitsTriggerDisplay", () => {
|
||||
).toEqual({ label: "Ultrathink", showFastModeIcon: true });
|
||||
});
|
||||
});
|
||||
|
||||
describe("buildUnavailableModelOptionDescriptors", () => {
|
||||
it("shows only saved values without inventing alternatives", () => {
|
||||
expect(
|
||||
buildUnavailableModelOptionDescriptors([
|
||||
{ id: "variant", value: "max" },
|
||||
{ id: "agent", value: "build" },
|
||||
{ id: "fastMode", value: true },
|
||||
]),
|
||||
).toEqual([
|
||||
{
|
||||
id: "variant",
|
||||
label: "Variant",
|
||||
type: "select",
|
||||
options: [{ id: "max", label: "max" }],
|
||||
currentValue: "max",
|
||||
},
|
||||
{
|
||||
id: "agent",
|
||||
label: "Agent",
|
||||
type: "select",
|
||||
options: [{ id: "build", label: "build" }],
|
||||
currentValue: "build",
|
||||
},
|
||||
{
|
||||
id: "fastMode",
|
||||
label: "Fast Mode",
|
||||
type: "boolean",
|
||||
currentValue: true,
|
||||
},
|
||||
]);
|
||||
});
|
||||
});
|
||||
@@ -13,6 +13,7 @@ import {
|
||||
getProviderOptionCurrentValue,
|
||||
getProviderOptionDescriptors,
|
||||
isClaudeUltrathinkPrompt,
|
||||
normalizeModelSlug,
|
||||
} from "@t3tools/shared/model";
|
||||
import { memo, useCallback, useState } from "react";
|
||||
import type { VariantProps } from "class-variance-authority";
|
||||
@@ -35,6 +36,42 @@ import { ComposerControl, ComposerControlChevron, ComposerControlIcon } from "./
|
||||
|
||||
type ProviderOptions = ReadonlyArray<ProviderOptionSelection>;
|
||||
|
||||
const SAVED_OPTION_LABELS: Readonly<Record<string, string>> = {
|
||||
agent: "Agent",
|
||||
effort: "Effort",
|
||||
reasoningEffort: "Reasoning effort",
|
||||
variant: "Variant",
|
||||
};
|
||||
|
||||
function savedOptionLabel(id: string): string {
|
||||
return (
|
||||
SAVED_OPTION_LABELS[id] ??
|
||||
id.replace(/([a-z0-9])([A-Z])/g, "$1 $2").replace(/^./, (character) => character.toUpperCase())
|
||||
);
|
||||
}
|
||||
|
||||
/** Read-only descriptors for saved values whose OpenCode model metadata is unavailable. */
|
||||
export function buildUnavailableModelOptionDescriptors(
|
||||
selections: ProviderOptions | null | undefined,
|
||||
): ReadonlyArray<ProviderOptionDescriptor> {
|
||||
return (selections ?? []).map((selection) =>
|
||||
typeof selection.value === "boolean"
|
||||
? {
|
||||
id: selection.id,
|
||||
label: savedOptionLabel(selection.id),
|
||||
type: "boolean" as const,
|
||||
currentValue: selection.value,
|
||||
}
|
||||
: {
|
||||
id: selection.id,
|
||||
label: savedOptionLabel(selection.id),
|
||||
type: "select" as const,
|
||||
options: [{ id: selection.value, label: selection.value }],
|
||||
currentValue: selection.value,
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
type TraitsPersistence =
|
||||
| {
|
||||
threadRef?: ScopedThreadRef;
|
||||
@@ -99,10 +136,19 @@ function getSelectedTraits(
|
||||
planModeEnabled: boolean,
|
||||
) {
|
||||
const caps = getProviderModelCapabilities(models, model, provider, planModeEnabled);
|
||||
const descriptors = getProviderOptionDescriptors({
|
||||
caps,
|
||||
selections: modelOptions,
|
||||
});
|
||||
const modelIsUnavailable =
|
||||
provider === "opencode" &&
|
||||
!models.some((candidate) => candidate.slug === normalizeModelSlug(model, provider));
|
||||
const descriptors = modelIsUnavailable
|
||||
? buildUnavailableModelOptionDescriptors(
|
||||
planModeEnabled
|
||||
? modelOptions
|
||||
: modelOptions?.filter((option) => option.id !== "agent" || option.value !== "plan"),
|
||||
)
|
||||
: getProviderOptionDescriptors({
|
||||
caps,
|
||||
selections: modelOptions,
|
||||
});
|
||||
const selectDescriptors = descriptors.filter(
|
||||
(descriptor): descriptor is Extract<ProviderOptionDescriptor, { type: "select" }> =>
|
||||
descriptor.type === "select",
|
||||
@@ -158,6 +204,7 @@ function getSelectedTraits(
|
||||
ultrathinkInBodyText,
|
||||
selectedAgent,
|
||||
selectedAgentLabel,
|
||||
modelIsUnavailable,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -193,7 +240,13 @@ function getTraitsSectionVisibility(input: {
|
||||
showFastMode,
|
||||
showContextWindow,
|
||||
showAgent,
|
||||
hasAnyControls: showEffort || showThinking || showFastMode || showContextWindow || showAgent,
|
||||
hasAnyControls:
|
||||
showEffort ||
|
||||
showThinking ||
|
||||
showFastMode ||
|
||||
showContextWindow ||
|
||||
showAgent ||
|
||||
(selected.modelIsUnavailable && selected.descriptors.length > 0),
|
||||
};
|
||||
}
|
||||
|
||||
@@ -262,6 +315,7 @@ export const TraitsMenuContent = memo(function TraitsMenuContentImpl({
|
||||
ultrathinkPromptControlled,
|
||||
ultrathinkInBodyText,
|
||||
hasAnyControls,
|
||||
modelIsUnavailable,
|
||||
} = getTraitsSectionVisibility({
|
||||
provider,
|
||||
models,
|
||||
@@ -300,6 +354,28 @@ export const TraitsMenuContent = memo(function TraitsMenuContentImpl({
|
||||
return null;
|
||||
}
|
||||
|
||||
if (modelIsUnavailable) {
|
||||
return (
|
||||
<>
|
||||
{descriptors.map((descriptor, index) => {
|
||||
const value = getProviderOptionCurrentLabel(descriptor);
|
||||
if (!value) return null;
|
||||
return (
|
||||
<div key={descriptor.id}>
|
||||
{index > 0 ? <MenuDivider /> : null}
|
||||
<MenuGroup>
|
||||
<div className="px-2 pt-1.5 pb-1 font-medium text-muted-foreground text-xs">
|
||||
{descriptor.label}
|
||||
</div>
|
||||
<div className="px-2 pb-1.5 text-muted-foreground/80 text-xs">{value}</div>
|
||||
</MenuGroup>
|
||||
</div>
|
||||
);
|
||||
})}
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<>
|
||||
{selectDescriptors.map((descriptor, index) => {
|
||||
|
||||
@@ -238,6 +238,94 @@ describe("getComposerProviderState", () => {
|
||||
});
|
||||
});
|
||||
|
||||
it("preserves explicit options when the selected model is absent from the catalog", () => {
|
||||
const state = getComposerProviderState({
|
||||
provider: ProviderDriverKind.make("opencode"),
|
||||
model: "opencode/kimi-k3",
|
||||
models: [
|
||||
{
|
||||
slug: "opencode/big-pickle",
|
||||
name: "Big Pickle",
|
||||
isCustom: false,
|
||||
capabilities: {},
|
||||
},
|
||||
],
|
||||
modelOptions: selections(["variant", "max"], ["agent", "build"]),
|
||||
planModeEnabled: false,
|
||||
});
|
||||
|
||||
expect(state.modelOptionsForDispatch).toEqual(
|
||||
selections(["variant", "max"], ["agent", "build"]),
|
||||
);
|
||||
});
|
||||
|
||||
it.each(["codex", "claudeAgent", "cursor", "grok"])(
|
||||
"does not preserve unknown options for a missing %s model",
|
||||
(provider) => {
|
||||
const state = getComposerProviderState({
|
||||
provider: ProviderDriverKind.make(provider),
|
||||
model: "missing-model",
|
||||
models: modelWith([]),
|
||||
modelOptions: selections(["unknown", "value"]),
|
||||
planModeEnabled: true,
|
||||
});
|
||||
|
||||
expect(state.modelOptionsForDispatch).toBeUndefined();
|
||||
},
|
||||
);
|
||||
|
||||
it("preserves explicit options while the catalog is empty", () => {
|
||||
const state = getComposerProviderState({
|
||||
provider: ProviderDriverKind.make("opencode"),
|
||||
model: "opencode/kimi-k3",
|
||||
models: [],
|
||||
modelOptions: selections(["variant", "max"], ["agent", "build"]),
|
||||
planModeEnabled: false,
|
||||
});
|
||||
|
||||
expect(state.modelOptionsForDispatch).toEqual(
|
||||
selections(["variant", "max"], ["agent", "build"]),
|
||||
);
|
||||
});
|
||||
|
||||
it("validates options for a known model selected through a legacy alias", () => {
|
||||
const state = getComposerProviderState({
|
||||
provider: ProviderDriverKind.make("claudeAgent"),
|
||||
model: "opus",
|
||||
models: [
|
||||
{
|
||||
slug: "claude-opus-5",
|
||||
name: "Claude Opus 5",
|
||||
isCustom: false,
|
||||
capabilities: {
|
||||
optionDescriptors: [
|
||||
selectDescriptor("effort", [
|
||||
{ id: "low", label: "Low" },
|
||||
{ id: "high", label: "High", isDefault: true },
|
||||
]),
|
||||
],
|
||||
},
|
||||
},
|
||||
],
|
||||
modelOptions: selections(["effort", "low"], ["unknown", "value"]),
|
||||
planModeEnabled: false,
|
||||
});
|
||||
|
||||
expect(state.modelOptionsForDispatch).toEqual(selections(["effort", "low"]));
|
||||
});
|
||||
|
||||
it("still drops the plan agent when an absent model has a saved plan selection", () => {
|
||||
const state = getComposerProviderState({
|
||||
provider: ProviderDriverKind.make("opencode"),
|
||||
model: "opencode/kimi-k3",
|
||||
models: [],
|
||||
modelOptions: selections(["variant", "max"], ["agent", "plan"]),
|
||||
planModeEnabled: false,
|
||||
});
|
||||
|
||||
expect(state.modelOptionsForDispatch).toEqual(selections(["variant", "max"]));
|
||||
});
|
||||
|
||||
it("adds ultrathink class names when the prompt triggers a promptInjectedValues descriptor", () => {
|
||||
const state = getComposerProviderState({
|
||||
provider: PROVIDER,
|
||||
|
||||
@@ -10,6 +10,7 @@ import {
|
||||
getProviderOptionCurrentValue,
|
||||
getProviderOptionDescriptors,
|
||||
isClaudeUltrathinkPrompt,
|
||||
normalizeModelSlug,
|
||||
} from "@t3tools/shared/model";
|
||||
import type { ReactNode } from "react";
|
||||
|
||||
@@ -63,6 +64,21 @@ export function getComposerProviderState(input: ComposerProviderStateInput): Com
|
||||
promptInjectionState = "none",
|
||||
planModeEnabled,
|
||||
} = input;
|
||||
if (provider === "opencode") {
|
||||
const normalizedModel = normalizeModelSlug(model, provider);
|
||||
const modelIsInCatalog = models.some((candidate) => candidate.slug === normalizedModel);
|
||||
if (!modelIsInCatalog) {
|
||||
const preservedOptions = modelOptions?.filter(
|
||||
(option) => planModeEnabled || option.id !== "agent" || option.value !== "plan",
|
||||
);
|
||||
return {
|
||||
provider,
|
||||
promptEffort: null,
|
||||
modelOptionsForDispatch:
|
||||
preservedOptions && preservedOptions.length > 0 ? preservedOptions : undefined,
|
||||
};
|
||||
}
|
||||
}
|
||||
const caps = getProviderModelCapabilities(models, model, provider, planModeEnabled);
|
||||
const descriptors = getProviderOptionDescriptors({ caps, selections: modelOptions });
|
||||
const primarySelectDescriptor = descriptors.find(
|
||||
|
||||
@@ -27,6 +27,7 @@ export type ModelEsque = {
|
||||
shortName?: string | undefined;
|
||||
subProvider?: string | undefined;
|
||||
isLegacy?: boolean | undefined;
|
||||
isUnavailable?: boolean | undefined;
|
||||
};
|
||||
|
||||
function escapeRegExp(value: string): string {
|
||||
|
||||
@@ -417,6 +417,8 @@ function ProjectDetail({ group }: { group: SidebarProjectSnapshot }) {
|
||||
// ----- default model -----
|
||||
const storedSelection = representative.defaultModelSelection;
|
||||
const resolvedSelection = resolveDefaultProviderModelSelection(serverProviders, storedSelection);
|
||||
const resolvedInstanceId = resolvedSelection?.instanceId ?? null;
|
||||
const resolvedModel = resolvedSelection?.model ?? null;
|
||||
const instanceEntries = useMemo(
|
||||
() =>
|
||||
sortProviderInstanceEntries(
|
||||
@@ -425,12 +427,11 @@ function ProjectDetail({ group }: { group: SidebarProjectSnapshot }) {
|
||||
[serverProviders, settings],
|
||||
);
|
||||
const modelOptionsByInstance = useMemo(
|
||||
() => getCustomModelOptionsByInstance(settings, serverProviders),
|
||||
[serverProviders, settings],
|
||||
);
|
||||
const activeEntry = instanceEntries.find(
|
||||
(entry) => entry.instanceId === resolvedSelection?.instanceId,
|
||||
() =>
|
||||
getCustomModelOptionsByInstance(settings, serverProviders, resolvedInstanceId, resolvedModel),
|
||||
[resolvedInstanceId, resolvedModel, serverProviders, settings],
|
||||
);
|
||||
const activeEntry = instanceEntries.find((entry) => entry.instanceId === resolvedInstanceId);
|
||||
const setDefaultModel = useCallback(
|
||||
(selection: ModelSelection | null) =>
|
||||
void updateAllMembers({ defaultModelSelection: selection }, "Failed to update default model"),
|
||||
|
||||
@@ -1025,6 +1025,10 @@ export function deriveEffectiveComposerModelState(input: {
|
||||
}): EffectiveComposerModelState {
|
||||
const baseModelCandidate =
|
||||
input.threadModelSelection?.model ?? input.projectModelSelection?.model ?? null;
|
||||
const preserveThreadModel =
|
||||
input.selectedInstanceId !== null &&
|
||||
input.selectedInstanceId !== undefined &&
|
||||
input.threadModelSelection?.instanceId === input.selectedInstanceId;
|
||||
const baseModel =
|
||||
(input.selectedInstanceId
|
||||
? resolveAppModelSelectionForInstance(
|
||||
@@ -1032,6 +1036,7 @@ export function deriveEffectiveComposerModelState(input: {
|
||||
input.settings,
|
||||
input.providers,
|
||||
baseModelCandidate,
|
||||
{ preserveUnavailableSelection: preserveThreadModel },
|
||||
)
|
||||
: null) ??
|
||||
resolveAppModelSelection(
|
||||
@@ -1061,6 +1066,7 @@ export function deriveEffectiveComposerModelState(input: {
|
||||
input.settings,
|
||||
input.providers,
|
||||
activeSelection.model,
|
||||
{ preserveUnavailableSelection: true },
|
||||
) ??
|
||||
resolveAppModelSelection(
|
||||
input.selectedProvider,
|
||||
|
||||
@@ -2,8 +2,11 @@ import { ProviderDriverKind, ProviderInstanceId, type ServerProvider } from "@t3
|
||||
import { DEFAULT_UNIFIED_SETTINGS, type UnifiedSettings } from "@t3tools/contracts/settings";
|
||||
import { describe, expect, it } from "vite-plus/test";
|
||||
import { createModelSelection } from "@t3tools/shared/model";
|
||||
import { deriveEffectiveComposerModelState } from "./composerDraftStore";
|
||||
import { getComposerProviderState } from "./components/chat/composerProviderState";
|
||||
import { deriveProviderInstanceEntries } from "./providerInstances";
|
||||
import {
|
||||
getCustomModelOptionsByInstance,
|
||||
getAppModelOptionsForInstance,
|
||||
resolveAppModelSelectionForInstance,
|
||||
resolveAppModelSelectionState,
|
||||
@@ -275,6 +278,15 @@ describe("instance-scoped model selection", () => {
|
||||
"claude-opus-4-6",
|
||||
),
|
||||
).toBe("claude-sonnet-4-6");
|
||||
expect(
|
||||
resolveAppModelSelectionForInstance(
|
||||
ProviderInstanceId.make("claudeAgent"),
|
||||
settings,
|
||||
providers,
|
||||
"claude-opus-4-6",
|
||||
{ preserveUnavailableSelection: true },
|
||||
),
|
||||
).toBe("claude-sonnet-4-6");
|
||||
});
|
||||
|
||||
it("falls back instead of resolving a custom slug against the wrong instance", () => {
|
||||
@@ -299,6 +311,287 @@ describe("instance-scoped model selection", () => {
|
||||
).toBe("claude-sonnet-4-6");
|
||||
});
|
||||
|
||||
it("preserves an existing OpenCode model when a catalog refresh no longer contains it", () => {
|
||||
const providers = [
|
||||
provider({
|
||||
provider: ProviderDriverKind.make("opencode"),
|
||||
instanceId: "opencode",
|
||||
models: ["opencode/big-pickle"],
|
||||
}),
|
||||
];
|
||||
|
||||
expect(
|
||||
resolveAppModelSelectionForInstance(
|
||||
ProviderInstanceId.make("opencode"),
|
||||
settingsWithProviderInstances(),
|
||||
providers,
|
||||
"opencode/kimi-k3",
|
||||
{ preserveUnavailableSelection: true },
|
||||
),
|
||||
).toBe("opencode/kimi-k3");
|
||||
expect(
|
||||
resolveAppModelSelectionForInstance(
|
||||
ProviderInstanceId.make("opencode"),
|
||||
settingsWithProviderInstances(),
|
||||
providers,
|
||||
"opencode/kimi-k3",
|
||||
),
|
||||
).toBe("opencode/big-pickle");
|
||||
});
|
||||
|
||||
it("adds the selected missing OpenCode model as an unavailable option", () => {
|
||||
const providers = [
|
||||
provider({
|
||||
provider: ProviderDriverKind.make("opencode"),
|
||||
instanceId: "opencode",
|
||||
models: ["opencode/big-pickle"],
|
||||
}),
|
||||
];
|
||||
const entry = deriveProviderInstanceEntries(providers)[0]!;
|
||||
|
||||
expect(
|
||||
getAppModelOptionsForInstance(settingsWithProviderInstances(), entry, "opencode/kimi-k3"),
|
||||
).toEqual([
|
||||
expect.objectContaining({ slug: "opencode/big-pickle" }),
|
||||
expect.objectContaining({
|
||||
slug: "opencode/kimi-k3",
|
||||
name: "opencode/kimi-k3",
|
||||
isUnavailable: true,
|
||||
}),
|
||||
]);
|
||||
});
|
||||
|
||||
it("keeps a missing OpenCode option scoped to the selected instance", () => {
|
||||
const selectedInstanceId = ProviderInstanceId.make("opencode_work");
|
||||
const otherInstanceId = ProviderInstanceId.make("opencode_personal");
|
||||
const driver = ProviderDriverKind.make("opencode");
|
||||
const providers = [
|
||||
provider({ provider: driver, instanceId: selectedInstanceId, models: [] }),
|
||||
provider({ provider: driver, instanceId: otherInstanceId, models: [] }),
|
||||
];
|
||||
const options = getCustomModelOptionsByInstance(
|
||||
settingsWithProviderInstances(),
|
||||
providers,
|
||||
selectedInstanceId,
|
||||
"openrouter/kimi-k3",
|
||||
);
|
||||
|
||||
expect(options.get(selectedInstanceId)).toEqual([
|
||||
expect.objectContaining({ slug: "openrouter/kimi-k3", isUnavailable: true }),
|
||||
]);
|
||||
expect(options.get(otherInstanceId)).toEqual([]);
|
||||
});
|
||||
|
||||
it("replaces the unavailable marker with catalog metadata after recovery", () => {
|
||||
const instanceId = ProviderInstanceId.make("opencode");
|
||||
const driver = ProviderDriverKind.make("opencode");
|
||||
const selectedModel = "opencode/kimi-k3";
|
||||
const pendingProviders = [
|
||||
provider({ provider: driver, instanceId, models: ["opencode/big-pickle"] }),
|
||||
];
|
||||
const recoveredProviders = [
|
||||
provider({ provider: driver, instanceId, models: ["opencode/big-pickle", selectedModel] }),
|
||||
];
|
||||
|
||||
expect(
|
||||
getAppModelOptionsForInstance(
|
||||
settingsWithProviderInstances(),
|
||||
deriveProviderInstanceEntries(pendingProviders)[0]!,
|
||||
selectedModel,
|
||||
).find((option) => option.slug === selectedModel)?.isUnavailable,
|
||||
).toBe(true);
|
||||
expect(
|
||||
getAppModelOptionsForInstance(
|
||||
settingsWithProviderInstances(),
|
||||
deriveProviderInstanceEntries(recoveredProviders)[0]!,
|
||||
selectedModel,
|
||||
).find((option) => option.slug === selectedModel)?.isUnavailable,
|
||||
).toBeUndefined();
|
||||
expect(
|
||||
resolveAppModelSelectionForInstance(
|
||||
instanceId,
|
||||
settingsWithProviderInstances(),
|
||||
recoveredProviders,
|
||||
selectedModel,
|
||||
{ preserveUnavailableSelection: true },
|
||||
),
|
||||
).toBe(selectedModel);
|
||||
});
|
||||
|
||||
it("does not resurrect a hidden OpenCode model when the raw catalog omits it", () => {
|
||||
const instanceId = ProviderInstanceId.make("opencode");
|
||||
const driver = ProviderDriverKind.make("opencode");
|
||||
const settings: UnifiedSettings = {
|
||||
...settingsWithProviderInstances(),
|
||||
providerModelPreferences: {
|
||||
[instanceId]: {
|
||||
hiddenModels: ["opencode/kimi-k3"],
|
||||
modelOrder: [],
|
||||
},
|
||||
},
|
||||
};
|
||||
const providers = [provider({ provider: driver, instanceId, models: [] })];
|
||||
const entry = deriveProviderInstanceEntries(providers)[0]!;
|
||||
|
||||
expect(getAppModelOptionsForInstance(settings, entry, "opencode/kimi-k3")).toEqual([]);
|
||||
expect(
|
||||
resolveAppModelSelectionForInstance(instanceId, settings, providers, "opencode/kimi-k3", {
|
||||
preserveUnavailableSelection: true,
|
||||
}),
|
||||
).toBeNull();
|
||||
});
|
||||
|
||||
it("does not add unavailable options for other providers", () => {
|
||||
const providers = [
|
||||
provider({
|
||||
provider: ProviderDriverKind.make("codex"),
|
||||
instanceId: "codex",
|
||||
models: ["gpt-5.6-sol"],
|
||||
}),
|
||||
];
|
||||
const entry = deriveProviderInstanceEntries(providers)[0]!;
|
||||
|
||||
expect(
|
||||
getAppModelOptionsForInstance(settingsWithProviderInstances(), entry, "gpt-missing").map(
|
||||
(option) => option.slug,
|
||||
),
|
||||
).toEqual(["gpt-5.6-sol"]);
|
||||
expect(
|
||||
resolveAppModelSelectionForInstance(
|
||||
ProviderInstanceId.make("codex"),
|
||||
settingsWithProviderInstances(),
|
||||
providers,
|
||||
"gpt-missing",
|
||||
{ preserveUnavailableSelection: true },
|
||||
),
|
||||
).toBe("gpt-5.6-sol");
|
||||
});
|
||||
|
||||
it("falls back from an explicit non-OpenCode draft with a missing model", () => {
|
||||
const instanceId = ProviderInstanceId.make("codex");
|
||||
const driver = ProviderDriverKind.make("codex");
|
||||
const providers = [provider({ provider: driver, instanceId, models: ["gpt-5.6-sol"] })];
|
||||
const state = deriveEffectiveComposerModelState({
|
||||
draft: {
|
||||
activeProvider: instanceId,
|
||||
modelSelectionByProvider: {
|
||||
[instanceId]: createModelSelection(instanceId, "gpt-missing", [
|
||||
{ id: "effort", value: "max" },
|
||||
]),
|
||||
},
|
||||
},
|
||||
providers,
|
||||
selectedProvider: driver,
|
||||
selectedInstanceId: instanceId,
|
||||
threadModelSelection: null,
|
||||
projectModelSelection: null,
|
||||
settings: settingsWithProviderInstances(),
|
||||
});
|
||||
const dispatch = getComposerProviderState({
|
||||
provider: driver,
|
||||
model: state.selectedModel,
|
||||
models: providers[0]!.models,
|
||||
modelOptions: state.modelOptions?.[instanceId],
|
||||
planModeEnabled: false,
|
||||
});
|
||||
|
||||
expect(state.selectedModel).toBe("gpt-5.6-sol");
|
||||
expect(dispatch.modelOptionsForDispatch).toBeUndefined();
|
||||
});
|
||||
|
||||
it("preserves an explicit draft OpenCode selection while the catalog is empty", () => {
|
||||
const instanceId = ProviderInstanceId.make("opencode_work");
|
||||
const driver = ProviderDriverKind.make("opencode");
|
||||
const draftSelection = createModelSelection(instanceId, "openrouter/kimi-k3", [
|
||||
{ id: "variant", value: "max" },
|
||||
{ id: "agent", value: "build" },
|
||||
]);
|
||||
const providers = [provider({ provider: driver, instanceId, models: [] })];
|
||||
const state = deriveEffectiveComposerModelState({
|
||||
draft: {
|
||||
activeProvider: instanceId,
|
||||
modelSelectionByProvider: { [instanceId]: draftSelection },
|
||||
},
|
||||
providers,
|
||||
selectedProvider: driver,
|
||||
selectedInstanceId: instanceId,
|
||||
threadModelSelection: null,
|
||||
projectModelSelection: null,
|
||||
settings: settingsWithProviderInstances(),
|
||||
});
|
||||
|
||||
expect(state.selectedModel).toBe("openrouter/kimi-k3");
|
||||
expect(state.modelOptions?.[instanceId]).toEqual(draftSelection.options);
|
||||
});
|
||||
|
||||
it("preserves saved options through dispatch when the model is absent from the catalog", () => {
|
||||
const instanceId = ProviderInstanceId.make("opencode");
|
||||
const driver = ProviderDriverKind.make("opencode");
|
||||
const providers = [provider({ provider: driver, instanceId, models: ["opencode/big-pickle"] })];
|
||||
const saved = createModelSelection(instanceId, "opencode/kimi-k3", [
|
||||
{ id: "variant", value: "max" },
|
||||
{ id: "agent", value: "build" },
|
||||
]);
|
||||
const state = deriveEffectiveComposerModelState({
|
||||
draft: null,
|
||||
providers,
|
||||
selectedProvider: driver,
|
||||
selectedInstanceId: instanceId,
|
||||
threadModelSelection: saved,
|
||||
projectModelSelection: null,
|
||||
settings: settingsWithProviderInstances(),
|
||||
});
|
||||
const dispatch = getComposerProviderState({
|
||||
provider: driver,
|
||||
model: state.selectedModel,
|
||||
models: providers[0]!.models,
|
||||
modelOptions: state.modelOptions?.[instanceId],
|
||||
planModeEnabled: false,
|
||||
});
|
||||
|
||||
expect(
|
||||
createModelSelection(instanceId, state.selectedModel, dispatch.modelOptionsForDispatch),
|
||||
).toEqual(saved);
|
||||
});
|
||||
|
||||
it("keeps a custom-instance draft model while dropping unsupported options", () => {
|
||||
const instanceId = ProviderInstanceId.make("claude_openrouter");
|
||||
const driver = ProviderDriverKind.make("claudeAgent");
|
||||
const providers = [
|
||||
provider({ provider: driver, instanceId: "claudeAgent", models: ["claude-opus-5"] }),
|
||||
provider({ provider: driver, instanceId, models: ["claude-opus-5"] }),
|
||||
];
|
||||
const threadSelection = createModelSelection(instanceId, "claude-opus-5", [
|
||||
{ id: "effort", value: "high" },
|
||||
]);
|
||||
const draftSelection = createModelSelection(instanceId, "openai/gpt-5.5", [
|
||||
{ id: "effort", value: "max" },
|
||||
]);
|
||||
const state = deriveEffectiveComposerModelState({
|
||||
draft: {
|
||||
activeProvider: instanceId,
|
||||
modelSelectionByProvider: { [instanceId]: draftSelection },
|
||||
},
|
||||
providers,
|
||||
selectedProvider: driver,
|
||||
selectedInstanceId: instanceId,
|
||||
threadModelSelection: threadSelection,
|
||||
projectModelSelection: null,
|
||||
settings: settingsWithProviderInstances(),
|
||||
});
|
||||
const dispatch = getComposerProviderState({
|
||||
provider: driver,
|
||||
model: state.selectedModel,
|
||||
models: providers[1]!.models,
|
||||
modelOptions: state.modelOptions?.[instanceId],
|
||||
planModeEnabled: false,
|
||||
});
|
||||
|
||||
expect(
|
||||
createModelSelection(instanceId, state.selectedModel, dispatch.modelOptionsForDispatch),
|
||||
).toEqual(createModelSelection(instanceId, "openai/gpt-5.5"));
|
||||
});
|
||||
|
||||
it("preserves custom provider instances in settings model selection", () => {
|
||||
const providers = [
|
||||
provider({
|
||||
|
||||
@@ -78,6 +78,27 @@ export interface AppModelOption {
|
||||
isCustom: boolean;
|
||||
isDefault?: boolean;
|
||||
isLegacy?: boolean;
|
||||
isUnavailable?: boolean;
|
||||
}
|
||||
|
||||
function appendUnavailableOpenCodeSelection(
|
||||
options: AppModelOption[],
|
||||
rawModels: ReadonlyArray<ServerProvider["models"][number]>,
|
||||
provider: ProviderDriverKind,
|
||||
selectedModel: string | null | undefined,
|
||||
hiddenModels: ReadonlyArray<string>,
|
||||
): AppModelOption[] {
|
||||
if (provider !== "opencode") return options;
|
||||
const slug = normalizeCustomModelSlug(selectedModel);
|
||||
if (!slug) return options;
|
||||
|
||||
// A model that exists in the raw catalog can be absent from `options`
|
||||
// because the user hid it. Keep that preference authoritative.
|
||||
if (rawModels.some((model) => model.slug === slug)) return options;
|
||||
if (hiddenModels.includes(slug)) return options;
|
||||
if (options.some((option) => option.slug === slug)) return options;
|
||||
|
||||
return [...options, { slug, name: slug, isCustom: false, isUnavailable: true }];
|
||||
}
|
||||
|
||||
function toAppModelOption(model: ServerProvider["models"][number]): AppModelOption {
|
||||
@@ -151,9 +172,10 @@ export function getAppModelOptions(
|
||||
settings: UnifiedSettings,
|
||||
providers: ReadonlyArray<ServerProvider>,
|
||||
provider: ProviderDriverKind,
|
||||
_selectedModel?: string | null,
|
||||
selectedModel?: string | null,
|
||||
): AppModelOption[] {
|
||||
const options: AppModelOption[] = getProviderModels(providers, provider).map(toAppModelOption);
|
||||
const rawModels = getProviderModels(providers, provider);
|
||||
const options: AppModelOption[] = rawModels.map(toAppModelOption);
|
||||
const seen = new Set(options.map((option) => option.slug));
|
||||
const builtInModelSlugs = new Set(
|
||||
Arr.filterMap(getProviderModels(providers, provider), (model) =>
|
||||
@@ -180,9 +202,13 @@ export function getAppModelOptions(
|
||||
});
|
||||
}
|
||||
|
||||
return applyInstanceModelPreferences(
|
||||
options,
|
||||
readInstanceModelPreferences(settings, defaultInstanceId),
|
||||
const preferences = readInstanceModelPreferences(settings, defaultInstanceId);
|
||||
return appendUnavailableOpenCodeSelection(
|
||||
applyInstanceModelPreferences(options, preferences),
|
||||
rawModels,
|
||||
provider,
|
||||
selectedModel,
|
||||
preferences.hiddenModels,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -200,6 +226,7 @@ export function getAppModelOptions(
|
||||
export function getAppModelOptionsForInstance(
|
||||
settings: UnifiedSettings,
|
||||
entry: ProviderInstanceEntry,
|
||||
selectedModel?: string | null,
|
||||
): AppModelOption[] {
|
||||
const options: AppModelOption[] = entry.models.map(toAppModelOption);
|
||||
const seen = new Set(options.map((option) => option.slug));
|
||||
@@ -219,9 +246,13 @@ export function getAppModelOptionsForInstance(
|
||||
options.push({ slug, name: slug, isCustom: true });
|
||||
}
|
||||
|
||||
return applyInstanceModelPreferences(
|
||||
options,
|
||||
readInstanceModelPreferences(settings, entry.instanceId),
|
||||
const preferences = readInstanceModelPreferences(settings, entry.instanceId);
|
||||
return appendUnavailableOpenCodeSelection(
|
||||
applyInstanceModelPreferences(options, preferences),
|
||||
entry.models,
|
||||
entry.driverKind,
|
||||
selectedModel,
|
||||
preferences.hiddenModels,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -244,14 +275,29 @@ export function resolveAppModelSelectionForInstance(
|
||||
settings: UnifiedSettings,
|
||||
providers: ReadonlyArray<ServerProvider>,
|
||||
selectedModel: string | null | undefined,
|
||||
resolutionOptions?: { readonly preserveUnavailableSelection?: boolean },
|
||||
): string | null {
|
||||
const entry = deriveProviderInstanceEntries(providers).find(
|
||||
(candidate) => candidate.instanceId === instanceId,
|
||||
);
|
||||
if (!entry) return null;
|
||||
const options = getAppModelOptionsForInstance(settings, entry);
|
||||
const options = getAppModelOptionsForInstance(
|
||||
settings,
|
||||
entry,
|
||||
resolutionOptions?.preserveUnavailableSelection ? selectedModel : null,
|
||||
);
|
||||
const resolvedSelection = resolveSelectableModel(entry.driverKind, selectedModel, options);
|
||||
if (resolvedSelection) {
|
||||
return resolvedSelection;
|
||||
}
|
||||
if (resolutionOptions?.preserveUnavailableSelection && entry.driverKind === "opencode") {
|
||||
const unavailableSelection = normalizeCustomModelSlug(selectedModel);
|
||||
const hiddenModels = readInstanceModelPreferences(settings, entry.instanceId).hiddenModels;
|
||||
if (unavailableSelection && !hiddenModels.includes(unavailableSelection)) {
|
||||
return unavailableSelection;
|
||||
}
|
||||
}
|
||||
return (
|
||||
resolveSelectableModel(entry.driverKind, selectedModel, options) ??
|
||||
options.find((option) => option.isDefault)?.slug ??
|
||||
options[0]?.slug ??
|
||||
entry.models.find((model) => model.isDefault)?.slug ??
|
||||
@@ -268,12 +314,19 @@ export function resolveAppModelSelectionForInstance(
|
||||
export function getCustomModelOptionsByInstance(
|
||||
settings: UnifiedSettings,
|
||||
providers: ReadonlyArray<ServerProvider>,
|
||||
_selectedInstanceId?: ProviderInstanceId | null,
|
||||
_selectedModel?: string | null,
|
||||
selectedInstanceId?: ProviderInstanceId | null,
|
||||
selectedModel?: string | null,
|
||||
): ReadonlyMap<ProviderInstanceId, ReadonlyArray<ModelEsque>> {
|
||||
const out = new Map<ProviderInstanceId, ReadonlyArray<ModelEsque>>();
|
||||
for (const entry of deriveProviderInstanceEntries(providers)) {
|
||||
out.set(entry.instanceId, getAppModelOptionsForInstance(settings, entry));
|
||||
out.set(
|
||||
entry.instanceId,
|
||||
getAppModelOptionsForInstance(
|
||||
settings,
|
||||
entry,
|
||||
entry.instanceId === selectedInstanceId ? selectedModel : null,
|
||||
),
|
||||
);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user