@@ -248,6 +248,8 @@ describe("truncateConversationIfNeeded", () => {
248248 contextWindow : modelInfo . contextWindow ,
249249 maxTokens : modelInfo . maxTokens ,
250250 apiHandler : mockApiHandler ,
251+ autoCondenseContext : false ,
252+ autoCondenseContextPercent : 100 ,
251253 systemPrompt : "System prompt" ,
252254 } )
253255
@@ -277,6 +279,8 @@ describe("truncateConversationIfNeeded", () => {
277279 contextWindow : modelInfo . contextWindow ,
278280 maxTokens : modelInfo . maxTokens ,
279281 apiHandler : mockApiHandler ,
282+ autoCondenseContext : false ,
283+ autoCondenseContextPercent : 100 ,
280284 systemPrompt : "System prompt" ,
281285 } )
282286
@@ -304,6 +308,8 @@ describe("truncateConversationIfNeeded", () => {
304308 contextWindow : modelInfo1 . contextWindow ,
305309 maxTokens : modelInfo1 . maxTokens ,
306310 apiHandler : mockApiHandler ,
311+ autoCondenseContext : false ,
312+ autoCondenseContextPercent : 100 ,
307313 systemPrompt : "System prompt" ,
308314 } )
309315
@@ -313,6 +319,8 @@ describe("truncateConversationIfNeeded", () => {
313319 contextWindow : modelInfo2 . contextWindow ,
314320 maxTokens : modelInfo2 . maxTokens ,
315321 apiHandler : mockApiHandler ,
322+ autoCondenseContext : false ,
323+ autoCondenseContextPercent : 100 ,
316324 systemPrompt : "System prompt" ,
317325 } )
318326
@@ -329,6 +337,8 @@ describe("truncateConversationIfNeeded", () => {
329337 contextWindow : modelInfo1 . contextWindow ,
330338 maxTokens : modelInfo1 . maxTokens ,
331339 apiHandler : mockApiHandler ,
340+ autoCondenseContext : false ,
341+ autoCondenseContextPercent : 100 ,
332342 systemPrompt : "System prompt" ,
333343 } )
334344
@@ -338,6 +348,8 @@ describe("truncateConversationIfNeeded", () => {
338348 contextWindow : modelInfo2 . contextWindow ,
339349 maxTokens : modelInfo2 . maxTokens ,
340350 apiHandler : mockApiHandler ,
351+ autoCondenseContext : false ,
352+ autoCondenseContextPercent : 100 ,
341353 systemPrompt : "System prompt" ,
342354 } )
343355
@@ -369,6 +381,8 @@ describe("truncateConversationIfNeeded", () => {
369381 contextWindow : modelInfo . contextWindow ,
370382 maxTokens,
371383 apiHandler : mockApiHandler ,
384+ autoCondenseContext : false ,
385+ autoCondenseContextPercent : 100 ,
372386 systemPrompt : "System prompt" ,
373387 } )
374388 expect ( resultWithSmall ) . toEqual ( {
@@ -399,6 +413,8 @@ describe("truncateConversationIfNeeded", () => {
399413 contextWindow : modelInfo . contextWindow ,
400414 maxTokens,
401415 apiHandler : mockApiHandler ,
416+ autoCondenseContext : false ,
417+ autoCondenseContextPercent : 100 ,
402418 systemPrompt : "System prompt" ,
403419 } )
404420 expect ( resultWithLarge . messages ) . not . toEqual ( messagesWithLargeContent ) // Should truncate
@@ -422,6 +438,8 @@ describe("truncateConversationIfNeeded", () => {
422438 contextWindow : modelInfo . contextWindow ,
423439 maxTokens,
424440 apiHandler : mockApiHandler ,
441+ autoCondenseContext : false ,
442+ autoCondenseContextPercent : 100 ,
425443 systemPrompt : "System prompt" ,
426444 } )
427445 expect ( resultWithVeryLarge . messages ) . not . toEqual ( messagesWithVeryLargeContent ) // Should truncate
@@ -448,6 +466,8 @@ describe("truncateConversationIfNeeded", () => {
448466 contextWindow : modelInfo . contextWindow ,
449467 maxTokens : modelInfo . maxTokens ,
450468 apiHandler : mockApiHandler ,
469+ autoCondenseContext : false ,
470+ autoCondenseContextPercent : 100 ,
451471 systemPrompt : "System prompt" ,
452472 } )
453473 expect ( result ) . toEqual ( {
@@ -488,6 +508,7 @@ describe("truncateConversationIfNeeded", () => {
488508 maxTokens : modelInfo . maxTokens ,
489509 apiHandler : mockApiHandler ,
490510 autoCondenseContext : true ,
511+ autoCondenseContextPercent : 100 ,
491512 systemPrompt : "System prompt" ,
492513 } )
493514
@@ -534,6 +555,7 @@ describe("truncateConversationIfNeeded", () => {
534555 maxTokens : modelInfo . maxTokens ,
535556 apiHandler : mockApiHandler ,
536557 autoCondenseContext : true ,
558+ autoCondenseContextPercent : 100 ,
537559 systemPrompt : "System prompt" ,
538560 } )
539561
@@ -570,6 +592,7 @@ describe("truncateConversationIfNeeded", () => {
570592 maxTokens : modelInfo . maxTokens ,
571593 apiHandler : mockApiHandler ,
572594 autoCondenseContext : false ,
595+ autoCondenseContextPercent : 50 , // This shouldn't matter since autoCondenseContext is false
573596 systemPrompt : "System prompt" ,
574597 } )
575598
@@ -587,6 +610,94 @@ describe("truncateConversationIfNeeded", () => {
587610 // Clean up
588611 summarizeSpy . mockRestore ( )
589612 } )
613+
614+ it ( "should use summarizeConversation when autoCondenseContext is true and context percent exceeds threshold" , async ( ) => {
615+ // Mock the summarizeConversation function
616+ const mockSummary = "This is a summary of the conversation"
617+ const mockCost = 0.05
618+ const mockSummarizeResponse : condenseModule . SummarizeResponse = {
619+ messages : [
620+ { role : "user" , content : "First message" } ,
621+ { role : "assistant" , content : mockSummary , isSummary : true } ,
622+ { role : "user" , content : "Last message" } ,
623+ ] ,
624+ summary : mockSummary ,
625+ cost : mockCost ,
626+ newContextTokens : 100 ,
627+ }
628+
629+ const summarizeSpy = jest
630+ . spyOn ( condenseModule , "summarizeConversation" )
631+ . mockResolvedValue ( mockSummarizeResponse )
632+
633+ const modelInfo = createModelInfo ( 100000 , 30000 )
634+ // Set tokens to be below the allowedTokens threshold but above the percentage threshold
635+ const contextWindow = modelInfo . contextWindow
636+ const totalTokens = 60000 // Below allowedTokens but 60% of context window
637+ const messagesWithSmallContent = [ ...messages . slice ( 0 , - 1 ) , { ...messages [ messages . length - 1 ] , content : "" } ]
638+
639+ const result = await truncateConversationIfNeeded ( {
640+ messages : messagesWithSmallContent ,
641+ totalTokens,
642+ contextWindow,
643+ maxTokens : modelInfo . maxTokens ,
644+ apiHandler : mockApiHandler ,
645+ autoCondenseContext : true ,
646+ autoCondenseContextPercent : 50 , // Set threshold to 50% - our tokens are at 60%
647+ systemPrompt : "System prompt" ,
648+ } )
649+
650+ // Verify summarizeConversation was called with the right parameters
651+ expect ( summarizeSpy ) . toHaveBeenCalledWith ( messagesWithSmallContent , mockApiHandler , "System prompt" )
652+
653+ // Verify the result contains the summary information
654+ expect ( result ) . toMatchObject ( {
655+ messages : mockSummarizeResponse . messages ,
656+ summary : mockSummary ,
657+ cost : mockCost ,
658+ prevContextTokens : totalTokens ,
659+ } )
660+
661+ // Clean up
662+ summarizeSpy . mockRestore ( )
663+ } )
664+
665+ it ( "should not use summarizeConversation when autoCondenseContext is true but context percent is below threshold" , async ( ) => {
666+ // Reset any previous mock calls
667+ jest . clearAllMocks ( )
668+ const summarizeSpy = jest . spyOn ( condenseModule , "summarizeConversation" )
669+
670+ const modelInfo = createModelInfo ( 100000 , 30000 )
671+ // Set tokens to be below both the allowedTokens threshold and the percentage threshold
672+ const contextWindow = modelInfo . contextWindow
673+ const totalTokens = 40000 // 40% of context window
674+ const messagesWithSmallContent = [ ...messages . slice ( 0 , - 1 ) , { ...messages [ messages . length - 1 ] , content : "" } ]
675+
676+ const result = await truncateConversationIfNeeded ( {
677+ messages : messagesWithSmallContent ,
678+ totalTokens,
679+ contextWindow,
680+ maxTokens : modelInfo . maxTokens ,
681+ apiHandler : mockApiHandler ,
682+ autoCondenseContext : true ,
683+ autoCondenseContextPercent : 50 , // Set threshold to 50% - our tokens are at 40%
684+ systemPrompt : "System prompt" ,
685+ } )
686+
687+ // Verify summarizeConversation was not called
688+ expect ( summarizeSpy ) . not . toHaveBeenCalled ( )
689+
690+ // Verify no truncation or summarization occurred
691+ expect ( result ) . toEqual ( {
692+ messages : messagesWithSmallContent ,
693+ summary : "" ,
694+ cost : 0 ,
695+ prevContextTokens : totalTokens ,
696+ } )
697+
698+ // Clean up
699+ summarizeSpy . mockRestore ( )
700+ } )
590701} )
591702
592703/**
@@ -624,6 +735,8 @@ describe("getMaxTokens", () => {
624735 contextWindow : modelInfo . contextWindow ,
625736 maxTokens : modelInfo . maxTokens ,
626737 apiHandler : mockApiHandler ,
738+ autoCondenseContext : false ,
739+ autoCondenseContextPercent : 100 ,
627740 systemPrompt : "System prompt" ,
628741 } )
629742 expect ( result1 ) . toEqual ( {
@@ -640,6 +753,8 @@ describe("getMaxTokens", () => {
640753 contextWindow : modelInfo . contextWindow ,
641754 maxTokens : modelInfo . maxTokens ,
642755 apiHandler : mockApiHandler ,
756+ autoCondenseContext : false ,
757+ autoCondenseContextPercent : 100 ,
643758 systemPrompt : "System prompt" ,
644759 } )
645760 expect ( result2 . messages ) . not . toEqual ( messagesWithSmallContent )
@@ -664,6 +779,8 @@ describe("getMaxTokens", () => {
664779 contextWindow : modelInfo . contextWindow ,
665780 maxTokens : modelInfo . maxTokens ,
666781 apiHandler : mockApiHandler ,
782+ autoCondenseContext : false ,
783+ autoCondenseContextPercent : 100 ,
667784 systemPrompt : "System prompt" ,
668785 } )
669786 expect ( result1 ) . toEqual ( {
@@ -680,6 +797,8 @@ describe("getMaxTokens", () => {
680797 contextWindow : modelInfo . contextWindow ,
681798 maxTokens : modelInfo . maxTokens ,
682799 apiHandler : mockApiHandler ,
800+ autoCondenseContext : false ,
801+ autoCondenseContextPercent : 100 ,
683802 systemPrompt : "System prompt" ,
684803 } )
685804 expect ( result2 . messages ) . not . toEqual ( messagesWithSmallContent )
@@ -703,6 +822,8 @@ describe("getMaxTokens", () => {
703822 contextWindow : modelInfo . contextWindow ,
704823 maxTokens : modelInfo . maxTokens ,
705824 apiHandler : mockApiHandler ,
825+ autoCondenseContext : false ,
826+ autoCondenseContextPercent : 100 ,
706827 systemPrompt : "System prompt" ,
707828 } )
708829 expect ( result1 . messages ) . toEqual ( messagesWithSmallContent )
@@ -714,6 +835,8 @@ describe("getMaxTokens", () => {
714835 contextWindow : modelInfo . contextWindow ,
715836 maxTokens : modelInfo . maxTokens ,
716837 apiHandler : mockApiHandler ,
838+ autoCondenseContext : false ,
839+ autoCondenseContextPercent : 100 ,
717840 systemPrompt : "System prompt" ,
718841 } )
719842 expect ( result2 ) . not . toEqual ( messagesWithSmallContent )
@@ -735,6 +858,8 @@ describe("getMaxTokens", () => {
735858 contextWindow : modelInfo . contextWindow ,
736859 maxTokens : modelInfo . maxTokens ,
737860 apiHandler : mockApiHandler ,
861+ autoCondenseContext : false ,
862+ autoCondenseContextPercent : 100 ,
738863 systemPrompt : "System prompt" ,
739864 } )
740865 expect ( result1 . messages ) . toEqual ( messagesWithSmallContent )
@@ -746,6 +871,8 @@ describe("getMaxTokens", () => {
746871 contextWindow : modelInfo . contextWindow ,
747872 maxTokens : modelInfo . maxTokens ,
748873 apiHandler : mockApiHandler ,
874+ autoCondenseContext : false ,
875+ autoCondenseContextPercent : 100 ,
749876 systemPrompt : "System prompt" ,
750877 } )
751878 expect ( result2 ) . not . toEqual ( messagesWithSmallContent )
0 commit comments