Skip to content

Commit 40c2b17

Browse files
committed
condense if we reach the threshold
1 parent 4b45265 commit 40c2b17

File tree

2 files changed

+29
-13
lines changed

2 files changed

+29
-13
lines changed

src/core/sliding-window/index.ts

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ type TruncateOptions = {
6363
contextWindow: number
6464
maxTokens?: number | null
6565
apiHandler: ApiHandler
66-
autoCondenseContext?: boolean
66+
autoCondenseContext: boolean
67+
autoCondenseContextPercent: number
6768
systemPrompt: string
6869
}
6970

@@ -83,6 +84,7 @@ export async function truncateConversationIfNeeded({
8384
maxTokens,
8485
apiHandler,
8586
autoCondenseContext,
87+
autoCondenseContextPercent,
8688
systemPrompt,
8789
}: TruncateOptions): Promise<TruncateResponse> {
8890
// Calculate the maximum tokens reserved for response
@@ -96,21 +98,28 @@ export async function truncateConversationIfNeeded({
9698
: await estimateTokenCount([{ type: "text", text: lastMessageContent as string }], apiHandler)
9799

98100
// Calculate total effective tokens (totalTokens never includes the last message)
99-
const effectiveTokens = totalTokens + lastMessageTokens
101+
const prevContextTokens = totalTokens + lastMessageTokens
100102

101103
// Calculate available tokens for conversation history
102104
// Truncate if we're within TOKEN_BUFFER_PERCENTAGE of the context window
103105
const allowedTokens = contextWindow * (1 - TOKEN_BUFFER_PERCENTAGE) - reservedTokens
104106

105-
// Determine if truncation is needed and apply if necessary
106-
if (effectiveTokens <= allowedTokens) {
107-
return { messages, summary: "", cost: 0, prevContextTokens: effectiveTokens }
108-
} else if (autoCondenseContext) {
109-
const result = await summarizeConversation(messages, apiHandler, systemPrompt)
110-
if (result.summary) {
111-
return { ...result, prevContextTokens: effectiveTokens }
107+
if (autoCondenseContext) {
108+
const contextPercent = (100 * prevContextTokens) / contextWindow
109+
if (contextPercent >= autoCondenseContextPercent || prevContextTokens > allowedTokens) {
110+
// Attempt to intelligently condense the context
111+
const result = await summarizeConversation(messages, apiHandler, systemPrompt)
112+
if (result.summary) {
113+
return { ...result, prevContextTokens }
114+
}
112115
}
113116
}
114-
const truncatedMessages = truncateConversation(messages, 0.5)
115-
return { messages: truncatedMessages, prevContextTokens: effectiveTokens, summary: "", cost: 0 }
117+
118+
// Fall back to sliding window truncation if needed
119+
if (prevContextTokens > allowedTokens) {
120+
const truncatedMessages = truncateConversation(messages, 0.5)
121+
return { messages: truncatedMessages, prevContextTokens, summary: "", cost: 0 }
122+
}
123+
// No truncation or condensation needed
124+
return { messages, summary: "", cost: 0, prevContextTokens }
116125
}

src/core/task/Task.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1460,8 +1460,14 @@ export class Task extends EventEmitter<ClineEvents> {
14601460
}
14611461

14621462
public async *attemptApiRequest(retryAttempt: number = 0): ApiStream {
1463-
const { apiConfiguration, autoApprovalEnabled, alwaysApproveResubmit, requestDelaySeconds, experiments } =
1464-
(await this.providerRef.deref()?.getState()) ?? {}
1463+
const {
1464+
apiConfiguration,
1465+
autoApprovalEnabled,
1466+
alwaysApproveResubmit,
1467+
requestDelaySeconds,
1468+
experiments,
1469+
autoCondenseContextPercent = 100,
1470+
} = (await this.providerRef.deref()?.getState()) ?? {}
14651471

14661472
let rateLimitDelay = 0
14671473

@@ -1510,6 +1516,7 @@ export class Task extends EventEmitter<ClineEvents> {
15101516
contextWindow,
15111517
apiHandler: this.api,
15121518
autoCondenseContext,
1519+
autoCondenseContextPercent,
15131520
systemPrompt,
15141521
})
15151522
if (truncateResult.messages !== this.apiConversationHistory) {

0 commit comments

Comments
 (0)