File tree Expand file tree Collapse file tree 2 files changed +27
-1
lines changed
Expand file tree Collapse file tree 2 files changed +27
-1
lines changed Original file line number Diff line number Diff line change @@ -110,7 +110,16 @@ fn main() {
110110}
111111
112112fn process_query ( query : & str , api_key : & str , config_dir : & PathBuf , history_path : & str ) {
113- match api:: get_gemini_response ( query, api_key, config_dir) {
113+
114+ let recent_history = memory:: get_recent_history ( history_path, 3 ) ;
115+
116+ let context_query = if !recent_history. is_empty ( ) {
117+ format ! ( "Previous conversations for context:\n {}\n \n New query: {}" , recent_history, query)
118+ } else {
119+ query. to_string ( )
120+ } ;
121+
122+ match api:: get_gemini_response ( & context_query, api_key, config_dir) {
114123 Ok ( response) => {
115124 let formatted_response = utils:: render_response ( & response) ;
116125 println ! ( "{}" , formatted_response) ;
Original file line number Diff line number Diff line change @@ -27,3 +27,20 @@ pub fn load_chat(file_path: &str) -> Vec<ChatLog> {
2727 . filter_map ( |line| serde_json:: from_str :: < ChatLog > ( & line. unwrap ( ) ) . ok ( ) )
2828 . collect ( )
2929}
30+
31+ pub fn get_recent_history ( history_path : & str , limit : usize ) -> String {
32+ let chat_history = load_chat ( history_path) ;
33+ if chat_history. is_empty ( ) {
34+ return String :: new ( ) ;
35+ }
36+ let recent_chats = chat_history. iter ( ) . rev ( ) . take ( limit) . rev ( ) ;
37+
38+ let mut result = String :: new ( ) ;
39+ for ( idx, entry) in recent_chats. enumerate ( ) {
40+ result. push_str ( & format ! ( "--- Previous Conversation #{} ---\n " , idx + 1 ) ) ;
41+ result. push_str ( & format ! ( "User: {}\n " , entry. user) ) ;
42+ result. push_str ( & format ! ( "AI: {}\n \n " , entry. bot) ) ;
43+ }
44+
45+ result
46+ }
You can’t perform that action at this time.
0 commit comments