@@ -42,6 +42,7 @@ import {
4242 ORGANIZATION_ALLOW_ALL ,
4343 DEFAULT_MODES ,
4444 DEFAULT_CHECKPOINT_TIMEOUT_SECONDS ,
45+ getModelId ,
4546} from "@roo-code/types"
4647import { TelemetryService } from "@roo-code/telemetry"
4748import { CloudService , BridgeOrchestrator , getRooCodeApiUrl } from "@roo-code/cloud"
@@ -1295,6 +1296,31 @@ export class ClineProvider
12951296
12961297 // Provider Profile Management
12971298
1299+ /**
1300+ * Updates the current task's API handler if the provider or model has changed.
1301+ * This prevents unnecessary context condensing when only non-model settings change.
1302+ * @param providerSettings The new provider settings to apply
1303+ */
1304+ private updateTaskApiHandlerIfNeeded ( providerSettings : ProviderSettings ) : void {
1305+ const task = this . getCurrentTask ( )
1306+
1307+ if ( task && task . apiConfiguration ) {
1308+ // Only rebuild API handler if provider or model actually changed
1309+ // to avoid triggering unnecessary context condensing
1310+ const currentProvider = task . apiConfiguration . apiProvider
1311+ const newProvider = providerSettings . apiProvider
1312+ const currentModelId = getModelId ( task . apiConfiguration )
1313+ const newModelId = getModelId ( providerSettings )
1314+
1315+ if ( currentProvider !== newProvider || currentModelId !== newModelId ) {
1316+ task . api = buildApiHandler ( providerSettings )
1317+ }
1318+ } else if ( task ) {
1319+ // Fallback: rebuild if apiConfiguration is not available
1320+ task . api = buildApiHandler ( providerSettings )
1321+ }
1322+ }
1323+
12981324 getProviderProfileEntries ( ) : ProviderSettingsEntry [ ] {
12991325 return this . contextProxy . getValues ( ) . listApiConfigMeta || [ ]
13001326 }
@@ -1342,11 +1368,7 @@ export class ClineProvider
13421368
13431369 // Change the provider for the current task.
13441370 // TODO: We should rename `buildApiHandler` for clarity (e.g. `getProviderClient`).
1345- const task = this . getCurrentTask ( )
1346-
1347- if ( task ) {
1348- task . api = buildApiHandler ( providerSettings )
1349- }
1371+ this . updateTaskApiHandlerIfNeeded ( providerSettings )
13501372 } else {
13511373 await this . updateGlobalState ( "listApiConfigMeta" , await this . providerSettingsManager . listConfig ( ) )
13521374 }
@@ -1403,11 +1425,7 @@ export class ClineProvider
14031425 }
14041426
14051427 // Change the provider for the current task.
1406- const task = this . getCurrentTask ( )
1407-
1408- if ( task ) {
1409- task . api = buildApiHandler ( providerSettings )
1410- }
1428+ this . updateTaskApiHandlerIfNeeded ( providerSettings )
14111429
14121430 await this . postStateToWebview ( )
14131431
0 commit comments