summaryrefslogtreecommitdiffstats
path: root/src/repl
diff options
context:
space:
mode:
authorsigoden <sigoden@gmail.com>2024-10-05 18:39:42 +0800
committerGitHub <noreply@github.com>2024-10-05 18:39:42 +0800
commitea4c2131e40be1124110fe4203bd018a8018760c (patch)
tree6f52dcdfb06b34401651f8dbd815bed2338d17ec /src/repl
parent11a706a68d1a1a22817f2264bdbbbe7036897378 (diff)
downloadaichat-ea4c2131e40be1124110fe4203bd018a8018760c.tar.gz
feat: add `.compress session` REPL command (#907)
Diffstat (limited to 'src/repl')
-rw-r--r--src/repl/mod.rs36
1 files changed, 25 insertions, 11 deletions
diff --git a/src/repl/mod.rs b/src/repl/mod.rs
index 2b103f4..5a99ae9 100644
--- a/src/repl/mod.rs
+++ b/src/repl/mod.rs
@@ -10,7 +10,7 @@ use crate::client::{call_chat_completions, call_chat_completions_streaming};
use crate::config::{AssertState, Config, GlobalConfig, Input, StateFlags};
use crate::function::need_send_tool_results;
use crate::render::render_error;
-use crate::utils::{create_abort_signal, set_text, temp_file, AbortSignal};
+use crate::utils::{create_abort_signal, create_spinner, set_text, temp_file, AbortSignal};
use anyhow::{bail, Context, Result};
use fancy_regex::Regex;
@@ -31,7 +31,7 @@ lazy_static::lazy_static! {
const MENU_NAME: &str = "completion_menu";
lazy_static::lazy_static! {
- static ref REPL_COMMANDS: [ReplCommand; 33] = [
+ static ref REPL_COMMANDS: [ReplCommand; 34] = [
ReplCommand::new(".help", "Show this help message", AssertState::pass()),
ReplCommand::new(".info", "View system info", AssertState::pass()),
ReplCommand::new(".model", "Change the current LLM", AssertState::pass()),
@@ -76,6 +76,11 @@ lazy_static::lazy_static! {
AssertState::True(StateFlags::SESSION)
),
ReplCommand::new(
+ ".compress session",
+ "Compress messages in the current session",
+ AssertState::True(StateFlags::SESSION)
+ ),
+ ReplCommand::new(
".info session",
"View session info",
AssertState::True(StateFlags::SESSION_EMPTY | StateFlags::SESSION),
@@ -360,6 +365,23 @@ impl Repl {
}
}
}
+ ".compress" => {
+ match args.map(|v| match v.split_once(' ') {
+ Some((subcmd, args)) => (subcmd, Some(args.trim())),
+ None => (v, None),
+ }) {
+ Some(("session", _)) => {
+ let spinner = create_spinner("Compressing").await;
+ let ret = Config::compress_session(&self.config).await;
+ spinner.stop();
+ ret?;
+ println!("✨ Successfully compressed the session");
+ }
+ _ => {
+ println!(r#"Usage: .compress session"#)
+ }
+ }
+ }
".rebuild" => {
match args.map(|v| match v.split_once(' ') {
Some((subcmd, args)) => (subcmd, Some(args.trim())),
@@ -657,7 +679,7 @@ async fn ask(
color.italic().paint("Compressing the session."),
);
tokio::spawn(async move {
- let _ = compress_session(&config).await;
+ let _ = Config::compress_session(&config).await;
config.write().end_compressing_session();
});
}
@@ -696,14 +718,6 @@ fn parse_command(line: &str) -> Option<(&str, Option<&str>)> {
}
}
-async fn compress_session(config: &GlobalConfig) -> Result<()> {
- let input = Input::from_str(config, config.read().summarize_prompt(), None);
- let client = input.create_client()?;
- let summary = client.chat_completions(input).await?.text;
- config.write().compress_session(&summary);
- Ok(())
-}
-
fn split_files_text(args: &str) -> (&str, &str) {
match SPLIT_FILES_TEXT_ARGS_RE.find(args).ok().flatten() {
Some(mat) => {