Skip to content

Commit 7c90d51

Browse files
fix(web): composer keeps the picked effort when it falls back to another instance (#23957)
Co-authored-by: maria-rcks <[email protected]>
1 parent da94a13 commit 7c90d51

4 files changed

Lines changed: 224 additions & 17 deletions

File tree

‎apps/web/src/components/ChatView.logic.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,7 @@ export function resolveComposerProviderSelection(input: {
531531
: requestedInstanceId;
532532
return {
533533
selectedProviderEntry,
534+
requestedInstanceId,
534535
requestedDriverKind,
535536
lockedContinuationGroupKey,
536537
unavailableProviderInstanceId,

‎apps/web/src/components/chat/useComposerProviderModel.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ export function useComposerProviderModel(
7070
providers: providerStatuses,
7171
selectedProvider,
7272
selectedInstanceId,
73+
requestedInstanceId: selection.requestedInstanceId,
7374
threadModelSelection,
7475
projectModelSelection,
7576
settings,

‎apps/web/src/composerDraftStore.ts‎

Lines changed: 60 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -637,17 +637,10 @@ interface ComposerDraftModelState {
637637
modelSelectionByProvider: Partial<Record<ProviderInstanceId, ModelSelection>>;
638638
}
639639

640-
function providerSelectionsFromModelSelection(
640+
function selectionsByInstance(
641641
modelSelection: ModelSelection | null | undefined,
642-
): ProviderOptionSelectionsByProvider | null {
643-
if (!modelSelection) {
644-
return null;
645-
}
646-
const options = modelSelection.options;
647-
if (!options || options.length === 0) {
648-
return null;
649-
}
650-
return { [modelSelection.instanceId]: options };
642+
): Partial<Record<string, ModelSelection>> | null {
643+
return modelSelection ? { [modelSelection.instanceId]: modelSelection } : null;
651644
}
652645

653646
function modelSelectionByProviderToOptions(
@@ -1100,6 +1093,8 @@ export function deriveEffectiveComposerModelState(input: {
11001093
providers: ReadonlyArray<ServerProvider>;
11011094
selectedProvider: ProviderDriverKind;
11021095
selectedInstanceId?: ProviderInstanceId | null | undefined;
1096+
/** The instance the draft, thread or project asked for; differs from the selection on fallback. */
1097+
requestedInstanceId?: ProviderInstanceId | null | undefined;
11031098
threadModelSelection: ModelSelection | null | undefined;
11041099
projectModelSelection: ModelSelection | null | undefined;
11051100
settings: UnifiedSettings;
@@ -1159,18 +1154,67 @@ export function deriveEffectiveComposerModelState(input: {
11591154
activeSelection.model,
11601155
))
11611156
: baseModel;
1162-
const modelOptions =
1163-
modelSelectionByProviderToOptions(input.draft?.modelSelectionByProvider) ??
1164-
providerSelectionsFromModelSelection(input.threadModelSelection) ??
1165-
providerSelectionsFromModelSelection(input.projectModelSelection) ??
1166-
null;
1157+
// Options come from the first of the draft, thread and project that has any. Their selections
1158+
// stay with them, so a fallback instance can check which model the options belong to.
1159+
const optionSelections =
1160+
[
1161+
input.draft?.modelSelectionByProvider,
1162+
selectionsByInstance(input.threadModelSelection),
1163+
selectionsByInstance(input.projectModelSelection),
1164+
].find((selections) => modelSelectionByProviderToOptions(selections) !== null) ?? null;
1165+
const modelOptions = modelSelectionByProviderToOptions(optionSelections);
11671166

11681167
return {
11691168
selectedModel,
1170-
modelOptions,
1169+
modelOptions: withFallbackInstanceOptions({
1170+
...input,
1171+
modelOptions,
1172+
optionSelections,
1173+
selectedModel,
1174+
}),
11711175
};
11721176
}
11731177

1178+
/**
1179+
* When the requested instance is disabled or unavailable, the composer shows another instance of
1180+
* the same driver. When that instance has no options of its own and shows the model the requested
1181+
* instance's options were picked with, it takes those options instead of the model's defaults.
1182+
*/
1183+
function withFallbackInstanceOptions(input: {
1184+
readonly modelOptions: ProviderOptionSelectionsByProvider | null;
1185+
readonly optionSelections: Partial<Record<string, ModelSelection>> | null;
1186+
readonly selectedModel: string;
1187+
readonly providers: ReadonlyArray<ServerProvider>;
1188+
readonly selectedProvider: ProviderDriverKind;
1189+
readonly selectedInstanceId?: ProviderInstanceId | null | undefined;
1190+
readonly requestedInstanceId?: ProviderInstanceId | null | undefined;
1191+
}): ProviderOptionSelectionsByProvider | null {
1192+
const { modelOptions, selectedInstanceId, requestedInstanceId } = input;
1193+
if (
1194+
!modelOptions ||
1195+
!selectedInstanceId ||
1196+
!requestedInstanceId ||
1197+
selectedInstanceId === requestedInstanceId ||
1198+
modelOptions[selectedInstanceId] !== undefined
1199+
) {
1200+
return modelOptions;
1201+
}
1202+
const requestedOptions = modelOptions[requestedInstanceId];
1203+
const requestedDriver = input.providers.find(
1204+
(provider) => provider.instanceId === requestedInstanceId,
1205+
)?.driver;
1206+
// The model of the selection the options came from, which can differ from the shown model's source.
1207+
const requestedModel = input.optionSelections?.[requestedInstanceId]?.model;
1208+
if (
1209+
!requestedOptions ||
1210+
requestedDriver !== input.selectedProvider ||
1211+
requestedModel !== input.selectedModel
1212+
) {
1213+
return modelOptions;
1214+
}
1215+
return { ...modelOptions, [selectedInstanceId]: requestedOptions };
1216+
}
1217+
11741218
function revokeObjectPreviewUrl(previewUrl: string): void {
11751219
if (typeof URL === "undefined") {
11761220
return;

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

Lines changed: 162 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,12 @@ import { describe, expect, it } from "vite-plus/test";
1010
import { createModelSelection } from "@t3tools/shared/model";
1111
import { deriveEffectiveComposerModelState } from "./composerDraftStore";
1212
import { getComposerProviderState } from "./components/chat/composerProviderState";
13-
import { deriveProviderInstanceEntries, NO_PROVIDER_MODEL_SELECTION } from "./providerInstances";
13+
import { resolveComposerProviderSelection } from "./components/ChatView.logic";
14+
import {
15+
deriveProviderInstanceEntries,
16+
NO_PROVIDER_MODEL_SELECTION,
17+
providerInstanceEntriesFor,
18+
} from "./providerInstances";
1419
import {
1520
getAppModelOptionsForInstance,
1621
resolveAppModelSelectionForInstance,
@@ -780,3 +785,159 @@ describe("default model settings and new-thread composer parity", () => {
780785
},
781786
);
782787
});
788+
789+
describe("composer fallback to another instance of the same driver", () => {
790+
const claude = ProviderDriverKind.make("claudeAgent");
791+
const codex = ProviderDriverKind.make("codex");
792+
const disabled = ProviderInstanceId.make("claudeAgent");
793+
const fallback = ProviderInstanceId.make("claude_team");
794+
const effortModels: ServerProvider["models"] = [
795+
{
796+
slug: "claude-opus-5-5",
797+
name: "Claude Opus 5.5",
798+
isCustom: false,
799+
capabilities: {
800+
optionDescriptors: [
801+
{
802+
id: "effort",
803+
label: "Effort",
804+
type: "select",
805+
options: ["medium", "high", "xhigh"].map((id) => ({
806+
id,
807+
label: id,
808+
isDefault: id === "medium",
809+
})),
810+
},
811+
],
812+
},
813+
},
814+
];
815+
const providers: ReadonlyArray<ServerProvider> = [
816+
{
817+
...provider({ provider: claude, instanceId: disabled }),
818+
enabled: false,
819+
models: effortModels,
820+
},
821+
{ ...provider({ provider: claude, instanceId: fallback }), models: effortModels },
822+
];
823+
const picked = createModelSelection(disabled, "claude-opus-5-5", [
824+
{ id: "effort", value: "xhigh" },
825+
]);
826+
827+
// The same steps useComposerProviderModel takes, without the draft store.
828+
function composerFor(input: {
829+
readonly providers: ReadonlyArray<ServerProvider>;
830+
readonly draft?: Parameters<typeof deriveEffectiveComposerModelState>[0]["draft"];
831+
readonly threadModelSelection: ReturnType<typeof createModelSelection>;
832+
}) {
833+
const settings = {
834+
...DEFAULT_UNIFIED_SETTINGS,
835+
providerInstances: Object.fromEntries(
836+
input.providers.map((provider) => [
837+
provider.instanceId,
838+
{
839+
driver: provider.driver,
840+
enabled: provider.enabled,
841+
config: {},
842+
},
843+
]),
844+
),
845+
};
846+
const selection = resolveComposerProviderSelection({
847+
entries: providerInstanceEntriesFor(input.providers, settings, null),
848+
candidateInstanceIds: [input.threadModelSelection.instanceId],
849+
lockedProvider: null,
850+
lockedInstanceId: undefined,
851+
});
852+
const selectedInstanceId = selection.selectedProviderEntry!.instanceId;
853+
const selectedProvider = selection.selectedProviderEntry!.driverKind;
854+
const state = deriveEffectiveComposerModelState({
855+
draft: input.draft ?? null,
856+
providers: input.providers,
857+
selectedProvider,
858+
selectedInstanceId,
859+
requestedInstanceId: selection.requestedInstanceId,
860+
threadModelSelection: input.threadModelSelection,
861+
projectModelSelection: null,
862+
settings,
863+
});
864+
const traits = getComposerProviderState({
865+
provider: selectedProvider,
866+
model: state.selectedModel,
867+
models: selection.selectedProviderEntry!.models,
868+
modelOptions: state.modelOptions?.[selectedInstanceId],
869+
planModeEnabled: false,
870+
});
871+
return { selectedInstanceId, state, traits };
872+
}
873+
874+
it("keeps the requested instance's effort on the fallback instance", () => {
875+
const { selectedInstanceId, state, traits } = composerFor({
876+
providers,
877+
threadModelSelection: picked,
878+
});
879+
880+
expect(selectedInstanceId).toBe(fallback);
881+
expect(state.selectedModel).toBe("claude-opus-5-5");
882+
expect(traits.promptEffort).toBe("xhigh");
883+
expect(
884+
createModelSelection(fallback, state.selectedModel, traits.modelOptionsForDispatch),
885+
).toEqual(
886+
createModelSelection(fallback, "claude-opus-5-5", [{ id: "effort", value: "xhigh" }]),
887+
);
888+
});
889+
890+
it("keeps options the fallback instance already has", () => {
891+
const { traits } = composerFor({
892+
providers,
893+
threadModelSelection: picked,
894+
draft: {
895+
activeProvider: disabled,
896+
modelSelectionByProvider: {
897+
[disabled]: picked,
898+
[fallback]: createModelSelection(fallback, "claude-opus-5-5", [
899+
{ id: "effort", value: "high" },
900+
]),
901+
},
902+
},
903+
});
904+
905+
expect(traits.promptEffort).toBe("high");
906+
});
907+
908+
it("does not carry options picked with another model", () => {
909+
// An optionless draft pick takes its options from the thread, which holds another model.
910+
const { state, traits } = composerFor({
911+
providers,
912+
threadModelSelection: createModelSelection(disabled, "claude-sonnet-4-6", [
913+
{ id: "effort", value: "high" },
914+
]),
915+
draft: {
916+
activeProvider: disabled,
917+
modelSelectionByProvider: { [disabled]: createModelSelection(disabled, "claude-opus-5-5") },
918+
},
919+
});
920+
921+
expect(state.selectedModel).toBe("claude-opus-5-5");
922+
expect(traits.promptEffort).toBe("medium");
923+
expect(traits.modelOptionsForDispatch).toBeUndefined();
924+
});
925+
926+
it("does not carry options to another driver", () => {
927+
const codexOnly: ReadonlyArray<ServerProvider> = [
928+
{
929+
...provider({ provider: claude, instanceId: disabled }),
930+
enabled: false,
931+
models: effortModels,
932+
},
933+
provider({ provider: codex, instanceId: "codex", models: ["gpt-6-astra"] }),
934+
];
935+
const { selectedInstanceId, state } = composerFor({
936+
providers: codexOnly,
937+
threadModelSelection: picked,
938+
});
939+
940+
expect(selectedInstanceId).toBe(ProviderInstanceId.make("codex"));
941+
expect(state.modelOptions?.[selectedInstanceId]).toBeUndefined();
942+
});
943+
});

0 commit comments

Comments
 (0)