Skip to content

Commit 8f52b76

Browse files
committed
feat:Context-Aware Queries
1 parent 9bd28c2 commit 8f52b76

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

src/main.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,16 @@ fn main() {
110110
}
111111

112112
fn 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\nNew 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);

src/memory.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff 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+
}

0 commit comments

Comments
 (0)