@@ -355,6 +355,13 @@ export class Task extends EventEmitter<ClineEvents> {
355355 }
356356 }
357357
358+ /* Condenses a task's message history to use fewer tokens. */
359+ async condenseHistory ( ) {
360+ // TODO(canyon): Replace with LLM summarization a la https://github.com/cline/cline/pull/3086
361+ const previousApiReqIndex = findLastIndex ( this . clineMessages , ( m ) => m . say === "api_req_started" )
362+ await this . maybeTruncateConversationHistory ( previousApiReqIndex )
363+ }
364+
358365 // Note that `partial` has three valid states true (partial message),
359366 // false (completion of partial message), undefined (individual complete
360367 // message).
@@ -1419,49 +1426,7 @@ export class Task extends EventEmitter<ClineEvents> {
14191426 )
14201427 } ) ( )
14211428
1422- // If the previous API request's total token usage is close to the
1423- // context window, truncate the conversation history to free up space
1424- // for the new request.
1425- if ( previousApiReqIndex >= 0 ) {
1426- const previousRequest = this . clineMessages [ previousApiReqIndex ] ?. text
1427-
1428- if ( ! previousRequest ) {
1429- return
1430- }
1431-
1432- const {
1433- tokensIn = 0 ,
1434- tokensOut = 0 ,
1435- cacheWrites = 0 ,
1436- cacheReads = 0 ,
1437- } : ClineApiReqInfo = JSON . parse ( previousRequest )
1438-
1439- const totalTokens = tokensIn + tokensOut + cacheWrites + cacheReads
1440-
1441- // Default max tokens value for thinking models when no specific
1442- // value is set.
1443- const DEFAULT_THINKING_MODEL_MAX_TOKENS = 16_384
1444-
1445- const modelInfo = this . api . getModel ( ) . info
1446-
1447- const maxTokens = modelInfo . thinking
1448- ? this . apiConfiguration . modelMaxTokens || DEFAULT_THINKING_MODEL_MAX_TOKENS
1449- : modelInfo . maxTokens
1450-
1451- const contextWindow = modelInfo . contextWindow
1452-
1453- const trimmedMessages = await truncateConversationIfNeeded ( {
1454- messages : this . apiConversationHistory ,
1455- totalTokens,
1456- maxTokens,
1457- contextWindow,
1458- apiHandler : this . api ,
1459- } )
1460-
1461- if ( trimmedMessages !== this . apiConversationHistory ) {
1462- await this . overwriteApiConversationHistory ( trimmedMessages )
1463- }
1464- }
1429+ await this . maybeTruncateConversationHistory ( previousApiReqIndex )
14651430
14661431 // Clean conversation history by:
14671432 // 1. Converting to Anthropic.MessageParam by spreading only the API-required properties.
@@ -1585,6 +1550,52 @@ export class Task extends EventEmitter<ClineEvents> {
15851550 yield * iterator
15861551 }
15871552
1553+ private async maybeTruncateConversationHistory ( previousApiReqIndex : number ) {
1554+ // If the previous API request's total token usage is close to the
1555+ // context window, truncate the conversation history to free up space
1556+ // for the new request.
1557+ if ( previousApiReqIndex >= 0 ) {
1558+ const previousRequest = this . clineMessages [ previousApiReqIndex ] ?. text
1559+
1560+ if ( ! previousRequest ) {
1561+ return
1562+ }
1563+
1564+ const {
1565+ tokensIn = 0 ,
1566+ tokensOut = 0 ,
1567+ cacheWrites = 0 ,
1568+ cacheReads = 0 ,
1569+ } : ClineApiReqInfo = JSON . parse ( previousRequest )
1570+
1571+ const totalTokens = tokensIn + tokensOut + cacheWrites + cacheReads
1572+
1573+ // Default max tokens value for thinking models when no specific
1574+ // value is set.
1575+ const DEFAULT_THINKING_MODEL_MAX_TOKENS = 16_384
1576+
1577+ const modelInfo = this . api . getModel ( ) . info
1578+
1579+ const maxTokens = modelInfo . thinking
1580+ ? this . apiConfiguration . modelMaxTokens || DEFAULT_THINKING_MODEL_MAX_TOKENS
1581+ : modelInfo . maxTokens
1582+
1583+ const contextWindow = modelInfo . contextWindow
1584+
1585+ const trimmedMessages = await truncateConversationIfNeeded ( {
1586+ messages : this . apiConversationHistory ,
1587+ totalTokens,
1588+ maxTokens,
1589+ contextWindow,
1590+ apiHandler : this . api ,
1591+ } )
1592+
1593+ if ( trimmedMessages !== this . apiConversationHistory ) {
1594+ await this . overwriteApiConversationHistory ( trimmedMessages )
1595+ }
1596+ }
1597+ }
1598+
15881599 // Checkpoints
15891600
15901601 public async checkpointSave ( ) {
0 commit comments