summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorsigoden <sigoden@gmail.com>2024-11-13 06:58:13 +0800
committerGitHub <noreply@github.com>2024-11-13 06:58:13 +0800
commit36e884c4b58e2d81fc44b9f3201543632cde33fa (patch)
tree87cc3efd8a8df6c81e09a021da06349458fd3c85 /src
parent522422d9d81d2ea0d2138512aca1adee2554ff11 (diff)
downloadaichat-36e884c4b58e2d81fc44b9f3201543632cde33fa.tar.gz
refactor: no side effect `--save-session` (#987)
Diffstat (limited to 'src')
-rw-r--r--src/cli.rs2
-rw-r--r--src/config/mod.rs10
-rw-r--r--src/config/session.rs11
-rw-r--r--src/main.rs2
4 files changed, 22 insertions, 3 deletions
diff --git a/src/cli.rs b/src/cli.rs
index 643b1d8..21734b8 100644
--- a/src/cli.rs
+++ b/src/cli.rs
@@ -18,7 +18,7 @@ pub struct Cli {
/// Ensure the session is empty
#[clap(long)]
pub empty_session: bool,
- /// Force the session to be saved
+ /// Ensure the new conversation is saved to the session
#[clap(long)]
pub save_session: bool,
/// Start a agent
diff --git a/src/config/mod.rs b/src/config/mod.rs
index 2455588..aa0fe00 100644
--- a/src/config/mod.rs
+++ b/src/config/mod.rs
@@ -1108,6 +1108,16 @@ impl Config {
self.last_message = None;
Ok(())
}
+
+ pub fn set_append_conversation(&mut self) -> Result<()> {
+ if let Some(session) = self.session.as_mut() {
+ session.set_append_conversation();
+ } else {
+ bail!("No session")
+ }
+ Ok(())
+ }
+
pub fn list_sessions(&self) -> Vec<String> {
list_file_names(self.sessions_dir(), ".yaml")
}
diff --git a/src/config/session.rs b/src/config/session.rs
index 31a6e02..c072d1c 100644
--- a/src/config/session.rs
+++ b/src/config/session.rs
@@ -50,6 +50,8 @@ pub struct Session {
#[serde(skip)]
dirty: bool,
#[serde(skip)]
+ append_conversation: bool,
+ #[serde(skip)]
compressing: bool,
}
@@ -276,6 +278,10 @@ impl Session {
}
}
+ pub fn set_append_conversation(&mut self) {
+ self.append_conversation = true;
+ }
+
pub fn set_compress_threshold(&mut self, value: Option<usize>) {
if self.compress_threshold != value {
self.compress_threshold = value;
@@ -308,7 +314,10 @@ impl Session {
}
pub fn exit(&mut self, session_dir: &Path, is_repl: bool) -> Result<()> {
- let save_session = self.save_session();
+ let mut save_session = self.save_session();
+ if self.append_conversation {
+ save_session = Some(true);
+ }
if self.dirty && save_session != Some(false) {
let mut session_name = self.name().to_string();
if save_session.is_none() {
diff --git a/src/main.rs b/src/main.rs
index 4f24f25..23aa7bc 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -130,7 +130,7 @@ async fn run(config: GlobalConfig, cli: Cli, text: Option<String>) -> Result<()>
config.write().empty_session()?;
}
if cli.save_session {
- config.write().set_save_session(Some(true));
+ config.write().set_append_conversation()?;
}
if cli.info {
let info = config.read().info()?;