@@ -781,38 +781,7 @@ export class ClineProvider implements vscode.WebviewViewProvider {
781781 await this . postStateToWebview ( )
782782 break
783783 case "mode" :
784- const newMode = message . text as Mode
785- await this . updateGlobalState ( "mode" , newMode )
786-
787- // Load the saved API config for the new mode if it exists
788- const savedConfigId = await this . configManager . getModeConfigId ( newMode )
789- const listApiConfig = await this . configManager . listConfig ( )
790-
791- // Update listApiConfigMeta first to ensure UI has latest data
792- await this . updateGlobalState ( "listApiConfigMeta" , listApiConfig )
793-
794- // If this mode has a saved config, use it
795- if ( savedConfigId ) {
796- const config = listApiConfig ?. find ( ( c ) => c . id === savedConfigId )
797- if ( config ?. name ) {
798- const apiConfig = await this . configManager . loadConfig ( config . name )
799- await Promise . all ( [
800- this . updateGlobalState ( "currentApiConfigName" , config . name ) ,
801- this . updateApiConfiguration ( apiConfig ) ,
802- ] )
803- }
804- } else {
805- // If no saved config for this mode, save current config as default
806- const currentApiConfigName = await this . getGlobalState ( "currentApiConfigName" )
807- if ( currentApiConfigName ) {
808- const config = listApiConfig ?. find ( ( c ) => c . name === currentApiConfigName )
809- if ( config ?. id ) {
810- await this . configManager . setModeConfig ( newMode , config . id )
811- }
812- }
813- }
814-
815- await this . postStateToWebview ( )
784+ await this . handleModeSwitch ( message . text as Mode )
816785 break
817786 case "updateSupportPrompt" :
818787 try {
@@ -1241,6 +1210,44 @@ export class ClineProvider implements vscode.WebviewViewProvider {
12411210 )
12421211 }
12431212
1213+ /**
1214+ * Handle switching to a new mode, including updating the associated API configuration
1215+ * @param newMode The mode to switch to
1216+ */
1217+ public async handleModeSwitch ( newMode : Mode ) {
1218+ await this . updateGlobalState ( "mode" , newMode )
1219+
1220+ // Load the saved API config for the new mode if it exists
1221+ const savedConfigId = await this . configManager . getModeConfigId ( newMode )
1222+ const listApiConfig = await this . configManager . listConfig ( )
1223+
1224+ // Update listApiConfigMeta first to ensure UI has latest data
1225+ await this . updateGlobalState ( "listApiConfigMeta" , listApiConfig )
1226+
1227+ // If this mode has a saved config, use it
1228+ if ( savedConfigId ) {
1229+ const config = listApiConfig ?. find ( ( c ) => c . id === savedConfigId )
1230+ if ( config ?. name ) {
1231+ const apiConfig = await this . configManager . loadConfig ( config . name )
1232+ await Promise . all ( [
1233+ this . updateGlobalState ( "currentApiConfigName" , config . name ) ,
1234+ this . updateApiConfiguration ( apiConfig ) ,
1235+ ] )
1236+ }
1237+ } else {
1238+ // If no saved config for this mode, save current config as default
1239+ const currentApiConfigName = await this . getGlobalState ( "currentApiConfigName" )
1240+ if ( currentApiConfigName ) {
1241+ const config = listApiConfig ?. find ( ( c ) => c . name === currentApiConfigName )
1242+ if ( config ?. id ) {
1243+ await this . configManager . setModeConfig ( newMode , config . id )
1244+ }
1245+ }
1246+ }
1247+
1248+ await this . postStateToWebview ( )
1249+ }
1250+
12441251 private async updateApiConfiguration ( apiConfiguration : ApiConfiguration ) {
12451252 // Update mode's default config
12461253 const { mode } = await this . getState ( )
0 commit comments