1- mod chat_state;
21mod command;
32mod context;
43mod conversation_state;
54mod input_source;
65mod parse;
76mod parser;
87mod prompt;
8+ mod summarization_state;
99mod tools;
1010
11- // Re-export types for use in this module
1211use std:: borrow:: Cow ;
1312use std:: collections:: {
1413 HashMap ,
1514 HashSet ,
16- VecDeque ,
1715} ;
1816use std:: io:: {
1917 IsTerminal ,
@@ -31,10 +29,6 @@ use std::{
3129 fs,
3230} ;
3331
34- use chat_state:: {
35- SummarizationState ,
36- TokenWarningLevel ,
37- } ;
3832use command:: {
3933 Command ,
4034 ToolsSubcommand ,
@@ -71,6 +65,10 @@ use fig_api_client::model::{
7165use fig_os_shim:: Context ;
7266use fig_settings:: Settings ;
7367use fig_util:: CLI_BINARY_NAME ;
68+ use summarization_state:: {
69+ SummarizationState ,
70+ TokenWarningLevel ,
71+ } ;
7472
7573/// Help text for the compact command
7674fn compact_help_text ( ) -> String {
@@ -79,11 +77,11 @@ fn compact_help_text() -> String {
7977<magenta,em>Conversation Compaction</magenta,em>
8078
8179The <em>/compact</em> command summarizes the conversation history to free up context space
82- while preserving the essential information. This is useful for long-running conversations
80+ while preserving essential information. This is useful for long-running conversations
8381that may eventually reach memory constraints.
8482
8583<cyan!>Usage</cyan!>
86- <em>/compact</em> <black!>Summarize conversation and clear history</black!>
84+ <em>/compact</em> <black!>Summarize the conversation and clear history</black!>
8785 <em>/compact [prompt]</em> <black!>Provide custom guidance for summarization</black!>
8886 <em>/compact --summary</em> <black!>Show the summary after compacting</black!>
8987
@@ -157,7 +155,7 @@ const WELCOME_TEXT: &str = color_print::cstr! {"
157155<em>/issue</em> <black!>Report an issue or make a feature request</black!>
158156<em>/profile</em> <black!>(Beta) Manage profiles for the chat session</black!>
159157<em>/context</em> <black!>(Beta) Manage context files for a profile</black!>
160- <em>/compact</em> <black!>Summarize conversation to free up context space</black!>
158+ <em>/compact</em> <black!>Summarize the conversation to free up context space</black!>
161159<em>/help</em> <black!>Show the help dialogue</black!>
162160<em>/quit</em> <black!>Quit the application</black!>
163161
@@ -175,7 +173,7 @@ const HELP_TEXT: &str = color_print::cstr! {"
175173<em>/editor</em> <black!>Open $EDITOR (defaults to vi) to compose a prompt</black!>
176174<em>/help</em> <black!>Show this help dialogue</black!>
177175<em>/quit</em> <black!>Quit the application</black!>
178- <em>/compact</em> <black!>Summarize conversation to free up context space</black!>
176+ <em>/compact</em> <black!>Summarize the conversation to free up context space</black!>
179177 <em>help</em> <black!>Show help for the compact command</black!>
180178 <em>[prompt]</em> <black!>Optional custom prompt to guide summarization</black!>
181179 <em>--summary</em> <black!>Display the summary after compacting</black!>
@@ -858,7 +856,7 @@ where
858856 execute ! (
859857 self . output,
860858 style:: SetForegroundColor ( Color :: Green ) ,
861- style:: Print ( "\n Conversation history and summary cleared.\n \n " ) ,
859+ style:: Print ( "\n Conversation history cleared.\n \n " ) ,
862860 style:: SetForegroundColor ( Color :: Reset )
863861 ) ?;
864862
@@ -890,7 +888,7 @@ where
890888 }
891889
892890 // Check if conversation history is long enough to compact
893- if self . conversation_state . history_len ( ) <= 3 {
891+ if self . conversation_state . history ( ) . len ( ) <= 3 {
894892 execute ! (
895893 self . output,
896894 style:: SetForegroundColor ( Color :: Yellow ) ,
@@ -905,23 +903,12 @@ where
905903 } ) ;
906904 }
907905
908- // Get private history field using accessor
909- let saved_history = VecDeque :: from ( self . conversation_state . history_as_vec ( ) . clone ( ) ) ;
910-
911906 // Set up summarization state with history, custom prompt, and show_summary flag
912907 let mut summarization_state = SummarizationState :: with_prompt ( prompt. clone ( ) ) ;
913- summarization_state. original_history = Some ( saved_history ) ;
908+ summarization_state. original_history = Some ( self . conversation_state . history ( ) . clone ( ) ) ;
914909 summarization_state. show_summary = show_summary; // Store the show_summary flag
915910 self . summarization_state = Some ( summarization_state) ;
916911
917- // Tell user we're working on the summary
918- execute ! (
919- self . output,
920- style:: SetForegroundColor ( Color :: Green ) ,
921- style:: Print ( "\n Compacting conversation history...\n " ) ,
922- style:: SetForegroundColor ( Color :: Reset )
923- ) ?;
924-
925912 // Create a summary request based on user input or default
926913 let summary_request = match prompt {
927914 Some ( custom_prompt) => {
@@ -971,7 +958,7 @@ where
971958
972959 // Use spinner while we wait
973960 if self . interactive {
974- queue ! ( self . output, cursor:: Hide ) ?;
961+ execute ! ( self . output, cursor:: Hide , style :: Print ( " \n " ) ) ?;
975962 self . spinner = Some ( Spinner :: new ( Spinners :: Dots , "Creating summary..." . to_string ( ) ) ) ;
976963 }
977964
@@ -1821,7 +1808,13 @@ where
18211808 buf. push ( '\n' ) ;
18221809 }
18231810
1824- if tool_name_being_recvd. is_none ( ) && !buf. is_empty ( ) && self . interactive && self . spinner . is_some ( ) {
1811+ // TODO: refactor summarization into a separate ChatState value
1812+ if tool_name_being_recvd. is_none ( )
1813+ && !buf. is_empty ( )
1814+ && self . interactive
1815+ && self . spinner . is_some ( )
1816+ && !is_summarization
1817+ {
18251818 drop ( self . spinner . take ( ) ) ;
18261819 queue ! (
18271820 self . output,
@@ -1905,8 +1898,18 @@ where
19051898
19061899 // Handle summarization completion if we were in summarization mode
19071900 if let Some ( summarization_state) = self . summarization_state . take ( ) {
1901+ if self . spinner . is_some ( ) {
1902+ drop ( self . spinner . take ( ) ) ;
1903+ queue ! (
1904+ self . output,
1905+ terminal:: Clear ( terminal:: ClearType :: CurrentLine ) ,
1906+ cursor:: MoveToColumn ( 0 ) ,
1907+ cursor:: Show
1908+ ) ?;
1909+ }
1910+
19081911 // Get the latest message content (the summary)
1909- let summary = match self . conversation_state . history_as_vec ( ) . last ( ) {
1912+ let summary = match self . conversation_state . history ( ) . back ( ) {
19101913 Some ( ChatMessage :: AssistantResponseMessage ( message) ) => message. content . clone ( ) ,
19111914 _ => "Summary could not be generated." . to_string ( ) ,
19121915 } ;
@@ -1928,12 +1931,10 @@ where
19281931 // Add the message
19291932 self . conversation_state . push_assistant_message ( special_message) ;
19301933
1931- // Display confirmation message
1932-
19331934 execute ! (
19341935 self . output,
19351936 style:: SetForegroundColor ( Color :: Green ) ,
1936- style:: Print ( "\n ✅ Conversation has been successfully summarized and cleared !\n \n " ) ,
1937+ style:: Print ( "✔ Conversation history has been compacted successfully !\n \n " ) ,
19371938 style:: SetForegroundColor ( Color :: DarkGrey )
19381939 ) ?;
19391940
@@ -1969,18 +1970,17 @@ where
19691970 style:: Print ( "\n " ) ,
19701971 style:: SetAttribute ( Attribute :: Bold ) ,
19711972 style:: Print ( " CONVERSATION SUMMARY" ) ,
1972- style:: SetAttribute ( Attribute :: Reset ) ,
19731973 style:: Print ( "\n " ) ,
19741974 style:: Print ( & border) ,
1975- style:: SetForegroundColor ( Color :: Reset ) ,
1975+ style:: SetAttribute ( Attribute :: Reset ) ,
19761976 style:: Print ( "\n \n " ) ,
19771977 style:: Print ( & summary) ,
19781978 style:: Print ( "\n \n " ) ,
19791979 style:: SetForegroundColor ( Color :: Cyan ) ,
19801980 style:: Print ( "This summary is stored in memory and available to the assistant.\n " ) ,
19811981 style:: Print ( "It contains all important details from previous interactions.\n " ) ,
19821982 style:: Print ( & border) ,
1983- style:: Print ( "\n " ) ,
1983+ style:: Print ( "\n \n " ) ,
19841984 style:: SetForegroundColor ( Color :: Reset )
19851985 ) ?;
19861986 }
0 commit comments