@@ -1572,6 +1572,37 @@ export class Task extends EventEmitter<TaskEvents> implements TaskLike {
15721572 }
15731573 }
15741574
1575+ private async getCondensingApiHandler (
1576+ state :
1577+ | {
1578+ condensingApiConfigOverride ?: boolean
1579+ condensingApiConfigId ?: string
1580+ listApiConfigMeta ?: Array < { id : string } >
1581+ }
1582+ | undefined ,
1583+ ) : Promise < ApiHandler | undefined > {
1584+ const configId = state ?. condensingApiConfigId
1585+ if ( ! state ?. condensingApiConfigOverride || ! configId ) {
1586+ return undefined
1587+ }
1588+
1589+ if ( ! state . listApiConfigMeta ?. some ( ( config ) => config . id === configId ) ) {
1590+ console . warn ( `[Task] Context condensing profile "${ configId } " no longer exists; using the current mode.` )
1591+ return undefined
1592+ }
1593+
1594+ try {
1595+ const profile = await this . providerRef . deref ( ) ?. providerSettingsManager . getProfile ( { id : configId } )
1596+ return profile ?. apiProvider ? buildApiHandler ( profile ) : undefined
1597+ } catch ( error ) {
1598+ console . warn (
1599+ `[Task] Failed to load context condensing profile "${ configId } "; using the current mode.` ,
1600+ error ,
1601+ )
1602+ return undefined
1603+ }
1604+ }
1605+
15751606 public async condenseContext ( ) : Promise < void > {
15761607 // CRITICAL: Flush any pending tool results before condensing
15771608 // to ensure tool_use/tool_result pairs are complete in history
@@ -1583,6 +1614,7 @@ export class Task extends EventEmitter<TaskEvents> implements TaskLike {
15831614 const state = await this . providerRef . deref ( ) ?. getState ( )
15841615 const customCondensingPrompt = state ?. customSupportPrompts ?. CONDENSE
15851616 const { mode, apiConfiguration } = state ?? { }
1617+ const condensingApiHandler = await this . getCondensingApiHandler ( state )
15861618
15871619 const { contextTokens : prevContextTokens } = this . getTokenUsage ( )
15881620
@@ -1638,6 +1670,7 @@ export class Task extends EventEmitter<TaskEvents> implements TaskLike {
16381670 } = await summarizeConversation ( {
16391671 messages : this . apiConversationHistory ,
16401672 apiHandler : this . api ,
1673+ condensingApiHandler,
16411674 systemPrompt,
16421675 taskId : this . taskId ,
16431676 isAutomaticTrigger : false ,
@@ -3802,6 +3835,8 @@ export class Task extends EventEmitter<TaskEvents> implements TaskLike {
38023835 private async handleContextWindowExceededError ( ) : Promise < void > {
38033836 const state = await this . providerRef . deref ( ) ?. getState ( )
38043837 const { profileThresholds = { } , mode, apiConfiguration } = state ?? { }
3838+ const customCondensingPrompt = state ?. customSupportPrompts ?. CONDENSE
3839+ const condensingApiHandler = await this . getCondensingApiHandler ( state )
38053840
38063841 const { contextTokens } = this . getTokenUsage ( )
38073842 const modelInfo = this . api . getModel ( ) . info
@@ -3876,10 +3911,12 @@ export class Task extends EventEmitter<TaskEvents> implements TaskLike {
38763911 maxTokens,
38773912 contextWindow,
38783913 apiHandler : this . api ,
3914+ condensingApiHandler,
38793915 autoCondenseContext : true ,
38803916 autoCondenseContextPercent : FORCED_CONTEXT_REDUCTION_PERCENT ,
38813917 systemPrompt : await this . getSystemPrompt ( ) ,
38823918 taskId : this . taskId ,
3919+ customCondensingPrompt,
38833920 profileThresholds,
38843921 currentProfileId,
38853922 metadata,
@@ -4042,11 +4079,13 @@ export class Task extends EventEmitter<TaskEvents> implements TaskLike {
40424079 lastMessageTokens,
40434080 useAvailableInputForContextPercent,
40444081 } )
4082+ let condensingApiHandler : ApiHandler | undefined
40454083
40464084 // Send condenseTaskContextStarted BEFORE manageContext to show in-progress indicator
40474085 // This notification must be sent here (not earlier) because the early check uses stale token count
40484086 // (before user message is added to history), which could incorrectly skip showing the indicator
40494087 if ( contextManagementWillRun && autoCondenseContext ) {
4088+ condensingApiHandler = await this . getCondensingApiHandler ( state )
40504089 await this . providerRef
40514090 . deref ( )
40524091 ?. postMessageToWebview ( { type : "condenseTaskContextStarted" , text : this . taskId } )
@@ -4111,6 +4150,7 @@ export class Task extends EventEmitter<TaskEvents> implements TaskLike {
41114150 maxTokens,
41124151 contextWindow,
41134152 apiHandler : this . api ,
4153+ condensingApiHandler,
41144154 autoCondenseContext,
41154155 autoCondenseContextPercent,
41164156 systemPrompt,
0 commit comments