Skip to content

Commit b4547bd

Browse files
committed
fix(web): match the fallback options to the model they were picked with
1 parent 0ce97f7 commit b4547bd

2 files changed

Lines changed: 41 additions & 26 deletions

File tree

‎apps/web/src/composerDraftStore.ts‎

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -640,17 +640,10 @@ interface ComposerDraftModelState {
640640
modelSelectionByProvider: Partial<Record<ProviderInstanceId, ModelSelection>>;
641641
}
642642

643-
function providerSelectionsFromModelSelection(
643+
function selectionsByInstance(
644644
modelSelection: ModelSelection | null | undefined,
645-
): ProviderOptionSelectionsByProvider | null {
646-
if (!modelSelection) {
647-
return null;
648-
}
649-
const options = modelSelection.options;
650-
if (!options || options.length === 0) {
651-
return null;
652-
}
653-
return { [modelSelection.instanceId]: options };
645+
): Partial<Record<string, ModelSelection>> | null {
646+
return modelSelection ? { [modelSelection.instanceId]: modelSelection } : null;
654647
}
655648

656649
function modelSelectionByProviderToOptions(
@@ -1158,33 +1151,40 @@ export function deriveEffectiveComposerModelState(input: {
11581151
activeSelection.model,
11591152
))
11601153
: baseModel;
1161-
const modelOptions =
1162-
modelSelectionByProviderToOptions(input.draft?.modelSelectionByProvider) ??
1163-
providerSelectionsFromModelSelection(input.threadModelSelection) ??
1164-
providerSelectionsFromModelSelection(input.projectModelSelection) ??
1165-
null;
1154+
// Options come from the first of the draft, thread and project that has any. Their selections
1155+
// stay with them, so a fallback instance can check which model the options belong to.
1156+
const optionSelections =
1157+
[
1158+
input.draft?.modelSelectionByProvider,
1159+
selectionsByInstance(input.threadModelSelection),
1160+
selectionsByInstance(input.projectModelSelection),
1161+
].find((selections) => modelSelectionByProviderToOptions(selections) !== null) ?? null;
1162+
const modelOptions = modelSelectionByProviderToOptions(optionSelections);
11661163

11671164
return {
11681165
selectedModel,
1169-
modelOptions: withFallbackInstanceOptions({ ...input, modelOptions, selectedModel }),
1166+
modelOptions: withFallbackInstanceOptions({
1167+
...input,
1168+
modelOptions,
1169+
optionSelections,
1170+
selectedModel,
1171+
}),
11701172
};
11711173
}
11721174

11731175
/**
11741176
* When the requested instance is disabled or unavailable, the composer shows another instance of
1175-
* the same driver with the same model. That instance has no options of its own, so it takes the
1176-
* requested instance's options instead of the model's defaults.
1177+
* the same driver. When that instance has no options of its own and shows the model the requested
1178+
* instance's options were picked with, it takes those options instead of the model's defaults.
11771179
*/
11781180
function withFallbackInstanceOptions(input: {
11791181
readonly modelOptions: ProviderOptionSelectionsByProvider | null;
1182+
readonly optionSelections: Partial<Record<string, ModelSelection>> | null;
11801183
readonly selectedModel: string;
11811184
readonly providers: ReadonlyArray<ServerProvider>;
11821185
readonly selectedProvider: ProviderDriverKind;
11831186
readonly selectedInstanceId?: ProviderInstanceId | null | undefined;
11841187
readonly requestedInstanceId?: ProviderInstanceId | null | undefined;
1185-
readonly draft: Pick<ComposerThreadDraftState, "modelSelectionByProvider"> | null | undefined;
1186-
readonly threadModelSelection: ModelSelection | null | undefined;
1187-
readonly projectModelSelection: ModelSelection | null | undefined;
11881188
}): ProviderOptionSelectionsByProvider | null {
11891189
const { modelOptions, selectedInstanceId, requestedInstanceId } = input;
11901190
if (
@@ -1200,11 +1200,8 @@ function withFallbackInstanceOptions(input: {
12001200
const requestedDriver = input.providers.find(
12011201
(provider) => provider.instanceId === requestedInstanceId,
12021202
)?.driver;
1203-
const requestedModel =
1204-
input.draft?.modelSelectionByProvider?.[requestedInstanceId]?.model ??
1205-
[input.threadModelSelection, input.projectModelSelection].find(
1206-
(selection) => selection?.instanceId === requestedInstanceId,
1207-
)?.model;
1203+
// The model of the selection the options came from, which can differ from the shown model's source.
1204+
const requestedModel = input.optionSelections?.[requestedInstanceId]?.model;
12081205
if (
12091206
!requestedOptions ||
12101207
requestedDriver !== input.selectedProvider ||

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -892,6 +892,24 @@ describe("composer fallback to another instance of the same driver", () => {
892892
expect(traits.promptEffort).toBe("high");
893893
});
894894

895+
it("does not carry options picked with another model", () => {
896+
// An optionless draft pick takes its options from the thread, which holds another model.
897+
const { state, traits } = composerFor({
898+
providers,
899+
threadModelSelection: createModelSelection(disabled, "claude-sonnet-4-6", [
900+
{ id: "effort", value: "high" },
901+
]),
902+
draft: {
903+
activeProvider: disabled,
904+
modelSelectionByProvider: { [disabled]: createModelSelection(disabled, "claude-opus-5-5") },
905+
},
906+
});
907+
908+
expect(state.selectedModel).toBe("claude-opus-5-5");
909+
expect(traits.promptEffort).toBe("medium");
910+
expect(traits.modelOptionsForDispatch).toBeUndefined();
911+
});
912+
895913
it("does not carry options to another driver", () => {
896914
const codexOnly: ReadonlyArray<ServerProvider> = [
897915
{

0 commit comments

Comments
 (0)