@@ -186,11 +186,17 @@ const selectOption = (
186186
187187/** fx's model catalog changes with its backend, so it must be read after the provider write. */
188188export const applyFxModelSelection = Effect . fn ( "applyFxModelSelection" ) ( function * (
189- runtime : AcpSessionRuntime . AcpSessionRuntime [ "Service" ] ,
189+ runtime : Pick <
190+ AcpSessionRuntime . AcpSessionRuntime [ "Service" ] ,
191+ "getConfigOptions" | "setConfigOption"
192+ > ,
190193 modelSelection : ModelSelection ,
191194 defaultChoice : Effect . Effect < FxModelChoice | undefined > | undefined ,
192195) : Effect . fn . Return < string | undefined , EffectAcpErrors . AcpError > {
193- const backendOption = selectOption ( yield * runtime . getConfigOptions , FX_PROVIDER_CONFIG_ID ) ;
196+ const originalOptions = yield * runtime . getConfigOptions ;
197+ const backendOption = selectOption ( originalOptions , FX_PROVIDER_CONFIG_ID ) ;
198+ const originalBackend = backendOption ?. currentValue ;
199+ const originalModel = selectOption ( originalOptions , FX_MODEL_CONFIG_ID ) ?. currentValue ;
194200 const requested = modelSelection . model . trim ( ) ;
195201 const isDefault = requested . length === 0 || requested === FX_DEFAULT_MODEL_SLUG ;
196202 const target = isDefault
@@ -204,31 +210,46 @@ export const applyFxModelSelection = Effect.fn("applyFxModelSelection")(function
204210 ( backendOption !== undefined &&
205211 ( target . backend === backendOption . currentValue ||
206212 collectSessionConfigOptionValues ( backendOption ) . includes ( target . backend ) ) ) ;
207- if (
213+ const changingBackend =
208214 target ?. backend !== undefined &&
209215 backendOption !== undefined &&
210216 canSelectBackend &&
211- target . backend !== backendOption . currentValue
212- ) {
213- yield * runtime . setConfigOption ( FX_PROVIDER_CONFIG_ID , target . backend ) ;
214- }
215- const modelOption = selectOption ( yield * runtime . getConfigOptions , FX_MODEL_CONFIG_ID ) ;
216- const current = modelOption ?. currentValue ;
217- if ( target === undefined || target . model === current ) return current ;
218- if ( isDefault && ! canSelectBackend ) return current ;
219- if (
220- isDefault &&
221- ( modelOption === undefined ||
222- ! collectSessionConfigOptionValues ( modelOption ) . includes ( target . model ) )
223- ) {
224- return yield * new EffectAcpErrors . AcpRequestError ( {
225- code : - 32602 ,
226- errorMessage : "The default fx model is unavailable on its configured backend." ,
227- data : { configId : FX_MODEL_CONFIG_ID } ,
228- } ) ;
229- }
230- yield * runtime . setConfigOption ( FX_MODEL_CONFIG_ID , target . model , { allowUnlistedValue : true } ) ;
231- return target . model ;
217+ target . backend !== backendOption . currentValue ;
218+ return yield * Effect . gen ( function * ( ) {
219+ if ( changingBackend && target ?. backend !== undefined ) {
220+ yield * runtime . setConfigOption ( FX_PROVIDER_CONFIG_ID , target . backend ) ;
221+ }
222+ const modelOption = selectOption ( yield * runtime . getConfigOptions , FX_MODEL_CONFIG_ID ) ;
223+ const current = modelOption ?. currentValue ;
224+ if ( target === undefined || target . model === current ) return current ;
225+ if ( isDefault && ! canSelectBackend ) return current ;
226+ if (
227+ isDefault &&
228+ ( modelOption === undefined ||
229+ ! collectSessionConfigOptionValues ( modelOption ) . includes ( target . model ) )
230+ ) {
231+ return yield * new EffectAcpErrors . AcpRequestError ( {
232+ code : - 32602 ,
233+ errorMessage : "The default fx model is unavailable on its configured backend." ,
234+ data : { configId : FX_MODEL_CONFIG_ID } ,
235+ } ) ;
236+ }
237+ yield * runtime . setConfigOption ( FX_MODEL_CONFIG_ID , target . model , { allowUnlistedValue : true } ) ;
238+ return target . model ;
239+ } ) . pipe (
240+ Effect . onError ( ( ) => {
241+ if ( ! changingBackend || originalBackend === undefined ) return Effect . void ;
242+ return Effect . gen ( function * ( ) {
243+ yield * runtime . setConfigOption ( FX_PROVIDER_CONFIG_ID , originalBackend ) ;
244+ const restoredModel = selectOption ( yield * runtime . getConfigOptions , FX_MODEL_CONFIG_ID ) ;
245+ if ( originalModel !== undefined && restoredModel ?. currentValue !== originalModel ) {
246+ yield * runtime . setConfigOption ( FX_MODEL_CONFIG_ID , originalModel , {
247+ allowUnlistedValue : true ,
248+ } ) ;
249+ }
250+ } ) . pipe ( Effect . ignoreCause ( { log : true } ) ) ;
251+ } ) ,
252+ ) ;
232253} ) ;
233254
234255/** The shared option loop writes a stored effort; a thread without one starts at medium. */
0 commit comments