@@ -605,10 +605,7 @@ try {
605605 }
606606
607607 function summarizeConversation ( preserveCount ) {
608- // First, compact the conversation
609- compactConversationContext ( preserveCount )
610-
611- // Then generate and display a user-friendly summary of the entire conversation
608+ // Generate a detailed summary and replace the conversation with it
612609 var stats = refreshConversationStats ( activeAgent )
613610 if ( ! isObject ( stats ) || ! isArray ( stats . entries ) || stats . entries . length === 0 ) {
614611 print ( colorifyText ( "No conversation to summarize." , hintColor ) )
@@ -620,8 +617,31 @@ try {
620617 return
621618 }
622619
620+ var keepCount = isNumber ( preserveCount ) ? Math . max ( 1 , Math . floor ( preserveCount ) ) : 6
621+ var entries = stats . entries . slice ( )
622+ var keepSize = Math . min ( keepCount , entries . length )
623+ var keepTail = entries . slice ( entries . length - keepSize )
624+ var summarizeUntil = entries . length - keepTail . length
625+
626+ var preserved = [ ]
627+ var summarizeCandidates = [ ]
628+ for ( var i = 0 ; i < summarizeUntil ; i ++ ) {
629+ var entry = entries [ i ]
630+ var role = isString ( entry . role ) ? entry . role . toLowerCase ( ) : ""
631+ if ( role === "system" || role === "developer" ) {
632+ preserved . push ( entry )
633+ } else {
634+ summarizeCandidates . push ( entry )
635+ }
636+ }
637+
638+ if ( summarizeCandidates . length === 0 ) {
639+ print ( colorifyText ( "No user or assistant messages to summarize from earlier history." , hintColor ) )
640+ return
641+ }
642+
623643 var conversationText = [ ]
624- stats . entries . forEach ( function ( entry ) {
644+ summarizeCandidates . forEach ( function ( entry ) {
625645 var role = isString ( entry . role ) ? entry . role . toUpperCase ( ) : "UNKNOWN"
626646 var content = flattenConversationContent ( entry . content )
627647 conversationText . push ( `${ role } : ${ content } ` )
@@ -644,16 +664,45 @@ try {
644664 } )
645665
646666 if ( isString ( fullSummary ) && fullSummary . trim ( ) . length > 0 ) {
647- print ( )
648- print ( colorifyText ( "=" . repeat ( 60 ) , accentColor ) )
649- print ( colorifyText ( "Conversation Summary" , "BOLD," + accentColor ) )
650- print ( colorifyText ( "=" . repeat ( 60 ) , accentColor ) )
651- print ( )
652- var _m = jsonParse ( fullSummary )
653- if ( isMap ( _m ) && isString ( _m . answer ) ) _m . answer = ow . format . withMD ( _m . answer )
654- print ( isObject ( _m ) ? printTree ( _m ) : ow . format . withMD ( fullSummary . trim ( ) ) )
655- print ( )
656- print ( colorifyText ( "=" . repeat ( 60 ) , accentColor ) )
667+ // Replace the conversation with the summary
668+ var summaryText = fullSummary . trim ( )
669+ var summaryEntry = {
670+ role : "assistant" ,
671+ content : `Conversation summary (${ summarizeCandidates . length } messages condensed on ${ new Date ( ) . toISOString ( ) } ):\n\n${ summaryText } `
672+ }
673+
674+ var newConversation = preserved . concat ( [ summaryEntry ] ) . concat ( keepTail )
675+ var convoPath = getConversationPath ( )
676+ if ( ! isString ( convoPath ) || convoPath . trim ( ) . length === 0 ) {
677+ print ( colorifyText ( "Conversation path is not configured. Use /set conversation <path> first." , hintColor ) )
678+ return
679+ }
680+
681+ io . writeFileJSON ( convoPath , { u : new Date ( ) , c : newConversation } , "" )
682+ if ( isObject ( activeAgent ) && isObject ( activeAgent . llm ) && typeof activeAgent . llm . getGPT === "function" ) {
683+ try { activeAgent . llm . getGPT ( ) . setConversation ( newConversation ) } catch ( ignoreSetConversation ) { }
684+ }
685+
686+ var previousTokens = stats . totalTokens
687+ var updatedStats = refreshConversationStats ( activeAgent )
688+ var afterTokens = isObject ( updatedStats ) ? updatedStats . totalTokens : 0
689+ var reduction = previousTokens > 0 ? Math . max ( 0 , previousTokens - afterTokens ) : 0
690+ print (
691+ colorifyText ( "Conversation summarized and replaced. Preserved " , successColor ) +
692+ colorifyText ( String ( keepTail . length ) , numericColor ) +
693+ colorifyText ( " recent message" + ( keepTail . length === 1 ? "" : "s" ) + "." , successColor )
694+ )
695+ if ( previousTokens > 0 ) {
696+ print (
697+ colorifyText ( "Estimated tokens: ~" , hintColor ) +
698+ colorifyText ( String ( previousTokens ) , numericColor ) +
699+ colorifyText ( " → ~" , hintColor ) +
700+ colorifyText ( String ( afterTokens ) , numericColor ) +
701+ colorifyText ( " (saved ~" , hintColor ) +
702+ colorifyText ( String ( reduction ) , numericColor ) +
703+ colorifyText ( ")." , hintColor )
704+ )
705+ }
657706 } else {
658707 print ( colorifyText ( "Unable to generate summary. Response was: " + stringify ( fullSummary ) , errorColor ) )
659708 print ( colorifyText ( "Conversation payload length: " + conversationPayload . length + " characters" , hintColor ) )
0 commit comments