From 3f693ea060d96b0adc397768c3b2dd47708a20ce Mon Sep 17 00:00:00 2001 From: sigoden Date: Mon, 4 Mar 2024 11:08:59 +0800 Subject: 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 --- src/repl/mod.rs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) (limited to 'src/repl/mod.rs') 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::*; -- cgit v1.2.3