@@ -1941,127 +1941,28 @@ export class ClineProvider
19411941 activate : boolean = true ,
19421942 ) : Promise < string | undefined > {
19431943 try {
1944- return await this . enqueueProviderProfileMutation ( async ( signal , epoch ) => {
1945- if ( ! this . isProfileMutationActive ( epoch , signal ) ) {
1946- return undefined
1947- }
1944+ return await this . enqueueProviderProfileMutation ( async ( signal ) => {
1945+ const id = await this . providerSettingsManager . saveConfig ( name , providerSettings )
19481946
1949- const previousApiConfigName = this . contextProxy . getValues ( ) . currentApiConfigName
1950- const previousProviderSettings = this . contextProxy . getProviderSettings ( )
1951- let previousMode : Mode | undefined
1952- let previousModeConfigId : string | undefined
1953- let hadPreviousModeConfig = false
1947+ if ( signal . aborted || this . _disposed ) return id
19541948
19551949 if ( activate ) {
1956- try {
1957- const state = await this . getState ( )
1958- previousMode = state . mode
1959- previousModeConfigId = await this . providerSettingsManager . getModeConfigId ( state . mode )
1960- hadPreviousModeConfig = previousModeConfigId !== undefined
1961- } catch {
1962- // Ignore lookup failures during pre-save capture
1963- }
1964- }
1965-
1966- if ( ! this . isProfileMutationActive ( epoch , signal ) ) {
1967- return undefined
1968- }
1969-
1970- const { id, existed, previousProfile } = await this . providerSettingsManager . saveConfigWithPrevious (
1971- name ,
1972- providerSettings ,
1973- )
1974- const savedProfile : ProviderSettingsWithId = { ...providerSettings , id }
1975-
1976- const rollback = async ( ) => {
1977- try {
1978- const isStale = ( ) => epoch !== undefined && epoch !== this . profileMutationEpoch
1979- if ( isStale ( ) || ( existed && ! previousProfile ) ) return
1980- const restored = await this . providerSettingsManager . restoreConfigIfMatches (
1981- name ,
1982- savedProfile ,
1983- existed ? previousProfile : undefined ,
1984- )
1985- if ( isStale ( ) ) return
1986- if ( restored ) {
1987- await this . updateGlobalState (
1988- "listApiConfigMeta" ,
1989- await this . providerSettingsManager . listConfig ( ) ,
1990- )
1991- if ( activate ) {
1992- if ( isStale ( ) ) return
1993- if ( previousApiConfigName !== undefined )
1994- await this . updateGlobalState ( "currentApiConfigName" , previousApiConfigName )
1995- if ( isStale ( ) ) return
1996- if ( previousMode )
1997- await this . providerSettingsManager . setModeConfig (
1998- previousMode ,
1999- hadPreviousModeConfig ? previousModeConfigId : undefined ,
2000- )
2001- if ( isStale ( ) ) return
2002- if ( previousProviderSettings ) {
2003- const currentConfig = this . contextProxy . getValues ( ) . currentApiConfigName
2004- if ( currentConfig === name || currentConfig === previousApiConfigName ) {
2005- await this . contextProxy . setProviderSettings ( previousProviderSettings )
2006- }
2007- }
2008- }
2009- }
2010- } catch ( rollbackError ) {
2011- this . log (
2012- `Failed to rollback upsert for profile '${ name } ': ${ rollbackError instanceof Error ? rollbackError . message : String ( rollbackError ) } ` ,
2013- )
2014- }
2015- }
2016-
2017- if ( ! this . isProfileMutationActive ( epoch , signal ) ) {
2018- await rollback ( )
2019- return undefined
2020- }
2021-
2022- try {
2023- if ( activate ) {
2024- const mode = previousMode ?? ( await this . getState ( ) ) . mode
2025-
2026- if ( ! this . isProfileMutationActive ( epoch , signal ) ) {
2027- await rollback ( )
2028- return undefined
2029- }
2030-
2031- await Promise . all ( [
2032- this . updateGlobalState (
2033- "listApiConfigMeta" ,
2034- await this . providerSettingsManager . listConfig ( ) ,
2035- ) ,
2036- this . updateGlobalState ( "currentApiConfigName" , name ) ,
2037- this . providerSettingsManager . setModeConfig ( mode , id ) ,
2038- this . contextProxy . setProviderSettings ( providerSettings ) ,
2039- ] )
2040-
2041- if ( ! this . isProfileMutationActive ( epoch , signal ) ) {
2042- await rollback ( )
2043- return undefined
2044- }
1950+ const { mode } = await this . getState ( )
20451951
2046- // Change the provider for the current task.
2047- this . updateTaskApiHandlerIfNeeded ( providerSettings , { forceRebuild : true } )
1952+ await Promise . all ( [
1953+ this . updateGlobalState ( "listApiConfigMeta" , await this . providerSettingsManager . listConfig ( ) ) ,
1954+ this . updateGlobalState ( "currentApiConfigName" , name ) ,
1955+ this . providerSettingsManager . setModeConfig ( mode , id ) ,
1956+ this . contextProxy . setProviderSettings ( providerSettings ) ,
1957+ ] )
20481958
2049- // Keep the current task's sticky provider profile in sync with the newly-activated profile.
2050- await this . persistStickyProviderProfileToCurrentTask ( name , { signal, epoch } )
2051- } else {
2052- await this . updateGlobalState (
2053- "listApiConfigMeta" ,
2054- await this . providerSettingsManager . listConfig ( ) ,
2055- )
2056- }
2057- } catch ( updateError ) {
2058- await rollback ( )
2059- throw updateError
2060- }
1959+ // Change the provider for the current task.
1960+ this . updateTaskApiHandlerIfNeeded ( providerSettings , { forceRebuild : true } )
20611961
2062- if ( ! this . isProfileMutationActive ( epoch , signal ) ) {
2063- await rollback ( )
2064- return undefined
1962+ // Keep the current task's sticky provider profile in sync with the newly-activated profile.
1963+ await this . persistStickyProviderProfileToCurrentTask ( name )
1964+ } else {
1965+ await this . updateGlobalState ( "listApiConfigMeta" , await this . providerSettingsManager . listConfig ( ) )
20651966 }
20661967
20671968 await this . postStateToWebview ( )
0 commit comments