@@ -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
656649function 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 */
11781180function 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 ||
0 commit comments