forked from sigoden/aichat
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpersistence.rs
59 lines (45 loc) · 1.63 KB
/
persistence.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
use std::fs;
use std::io::{self, Write, BufRead};
use std::process::Command;
use std::path::Path;
use std::env;
const CHAT_HISTORY_FILE: &str = ".config/aichat/messages.md";
fn load_chat_history() -> Vec<String> {
let contents = fs::read_to_string(CHAT_HISTORY_FILE)
.expect("Something went wrong reading the file");
contents.lines().map(|s| s.to_string()).collect()
}
fn save_chat_history(chat_history: &Vec<String>) {
let contents = chat_history.join("\n");
fs::write(CHAT_HISTORY_FILE, contents).expect("Unable to write file");
}
fn call_aichat(prompt: &str) -> String {
let output = Command::new("aichat")
.arg(prompt)
.output()
.expect("Failed to execute command");
if !output.status.success() {
eprintln!("Error calling aichat: {}", output.status);
return String::new();
}
String::from_utf8_lossy(&output.stdout).trim().to_string()
}
fn main() {
let mut chat_history = load_chat_history();
loop {
print!("You: ");
io::stdout().flush().unwrap();
let mut user_input = String::new();
io::stdin().read_line(&mut user_input).unwrap();
let user_input = user_input.trim();
if user_input == "quit" {
break;
}
chat_history.push(format!("You: {}", user_input));
let prompt = chat_history.iter().skip(chat_history.len().saturating_sub(1000)).fold(String::new(), |a, b| a + " " + b);
let ai_response = call_aichat(&prompt);
println!("AI Chat: {}", ai_response);
chat_history.push(format!("AI Chat: {}", ai_response));
save_chat_history(&chat_history);
}
}