summaryrefslogtreecommitdiffstats
path: root/src/repl/mod.rs
diff options
context:
space:
mode:
authorsigoden <sigoden@gmail.com>2024-03-04 11:08:59 +0800
committerGitHub <noreply@github.com>2024-03-04 11:08:59 +0800
commit3f693ea060d96b0adc397768c3b2dd47708a20ce (patch)
treed805b526aa7423d839a1c52907057e13efa0f38c /src/repl/mod.rs
parent9e15a3409e7080f0cf06b6dcfc0077142e3e8972 (diff)
downloadaichat-3f693ea060d96b0adc397768c3b2dd47708a20ce.tar.gz
feat: compress session automaticlly (#333)
* feat: compress session automaticlly * non-block * update field description * set compress_threshold * update session::clear_messages * able to override session compress_threshold * enable compress_threshold by default * make session compress_threshold optional
Diffstat (limited to 'src/repl/mod.rs')
-rw-r--r--src/repl/mod.rs20
1 files changed, 20 insertions, 0 deletions
diff --git a/src/repl/mod.rs b/src/repl/mod.rs
index e11b495..6ce52f2 100644
--- a/src/repl/mod.rs
+++ b/src/repl/mod.rs
@@ -258,6 +258,9 @@ impl Repl {
if text.is_empty() && files.is_empty() {
return Ok(());
}
+ while self.config.read().is_compressing_session() {
+ std::thread::sleep(std::time::Duration::from_millis(100));
+ }
let input = if files.is_empty() {
Input::from_str(text)
} else {
@@ -269,6 +272,14 @@ impl Repl {
let output = render_stream(&input, client.as_ref(), &self.config, self.abort.clone())?;
self.config.write().save_message(input, &output)?;
self.config.read().maybe_copy(&output);
+ if self.config.write().should_compress_session() {
+ let config = self.config.clone();
+ std::thread::spawn(move || -> anyhow::Result<()> {
+ let _ = compress_session(&config);
+ config.write().end_compressing_session();
+ Ok(())
+ });
+ }
Ok(())
}
@@ -418,6 +429,15 @@ fn parse_command(line: &str) -> Option<(&str, Option<&str>)> {
}
}
+fn compress_session(config: &GlobalConfig) -> Result<()> {
+ let input = Input::from_str(&config.read().summarize_prompt);
+ let mut client = init_client(config)?;
+ ensure_model_capabilities(client.as_mut(), input.required_capabilities())?;
+ let summary = client.send_message(input)?;
+ config.write().compress_session(&summary);
+ Ok(())
+}
+
#[cfg(test)]
mod tests {
use super::*;