-
Notifications
You must be signed in to change notification settings - Fork 2.6k
feat: add token-based condensing threshold support #7441
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -124,14 +124,19 @@ export async function truncateConversationIfNeeded({ | |||||||||||||||
|
|
||||||||||||||||
| // Determine the effective threshold to use | ||||||||||||||||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would it be helpful to add a comment here explaining the threshold logic? Something like:
Suggested change
This would make it immediately clear to future maintainers how the different threshold values are interpreted. |
||||||||||||||||
| let effectiveThreshold = autoCondenseContextPercent | ||||||||||||||||
| let effectiveTokenThreshold: number | undefined = undefined | ||||||||||||||||
| const profileThreshold = profileThresholds[currentProfileId] | ||||||||||||||||
|
|
||||||||||||||||
| if (profileThreshold !== undefined) { | ||||||||||||||||
| if (profileThreshold === -1) { | ||||||||||||||||
| // Special case: -1 means inherit from global setting | ||||||||||||||||
| effectiveThreshold = autoCondenseContextPercent | ||||||||||||||||
| } else if (profileThreshold >= MIN_CONDENSE_THRESHOLD && profileThreshold <= MAX_CONDENSE_THRESHOLD) { | ||||||||||||||||
| // Valid custom threshold | ||||||||||||||||
| // Valid percentage threshold | ||||||||||||||||
| effectiveThreshold = profileThreshold | ||||||||||||||||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider extracting the magic number 100 as a constant like PERCENTAGE_TOKEN_BOUNDARY. This would make the distinction between percentage and token thresholds clearer throughout the code. |
||||||||||||||||
| } else if (profileThreshold > MAX_CONDENSE_THRESHOLD) { | ||||||||||||||||
| // Values above 100 are treated as token counts | ||||||||||||||||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we consider adding an upper bound check for token thresholds? For example, values larger than typical context windows (e.g., > 1,000,000) might indicate a configuration error. |
||||||||||||||||
| effectiveTokenThreshold = profileThreshold | ||||||||||||||||
| } else { | ||||||||||||||||
| // Invalid threshold value, fall back to global setting | ||||||||||||||||
| console.warn( | ||||||||||||||||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The warning message could be more informative about the new token-based thresholds:
Suggested change
|
||||||||||||||||
|
|
@@ -144,7 +149,13 @@ export async function truncateConversationIfNeeded({ | |||||||||||||||
|
|
||||||||||||||||
| if (autoCondenseContext) { | ||||||||||||||||
| const contextPercent = (100 * prevContextTokens) / contextWindow | ||||||||||||||||
| if (contextPercent >= effectiveThreshold || prevContextTokens > allowedTokens) { | ||||||||||||||||
| // Check both percentage and token thresholds | ||||||||||||||||
| const shouldCondenseByPercent = contextPercent >= effectiveThreshold | ||||||||||||||||
| const shouldCondenseByTokens = | ||||||||||||||||
| effectiveTokenThreshold !== undefined && prevContextTokens >= effectiveTokenThreshold | ||||||||||||||||
| const shouldCondenseByLimit = prevContextTokens > allowedTokens | ||||||||||||||||
|
|
||||||||||||||||
| if (shouldCondenseByPercent || shouldCondenseByTokens || shouldCondenseByLimit) { | ||||||||||||||||
| // Attempt to intelligently condense the context | ||||||||||||||||
| const result = await summarizeConversation( | ||||||||||||||||
| messages, | ||||||||||||||||
|
|
||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it be worth adding a comment explaining this approximation? The 4 characters per token ratio is a testing simplification that future maintainers might find helpful to understand.