summaryrefslogtreecommitdiffstats
path: root/src/config
diff options
context:
space:
mode:
authorsigoden <sigoden@gmail.com>2024-06-17 10:59:59 +0800
committerGitHub <noreply@github.com>2024-06-17 10:59:59 +0800
commit189f2271bcc6f841264cb960e35e78c870663645 (patch)
tree88dfcce749695668582c05bdc024b1a39ffc06e5 /src/config
parent72e689fab0b414ea943c300c3eb92901c77ce775 (diff)
downloadaichat-189f2271bcc6f841264cb960e35e78c870663645.tar.gz
feat: add `.edit session` repl command (#606)
Diffstat (limited to 'src/config')
-rw-r--r--src/config/mod.rs37
-rw-r--r--src/config/session.rs25
2 files changed, 42 insertions, 20 deletions
diff --git a/src/config/mod.rs b/src/config/mod.rs
index efbf984..6cbbc05 100644
--- a/src/config/mod.rs
+++ b/src/config/mod.rs
@@ -713,17 +713,42 @@ impl Config {
Ok(())
}
- pub fn save_session(&mut self, name: &str) -> Result<()> {
- let sessions_dir = self.sessions_dir()?;
+ pub fn save_session(&mut self, name: Option<&str>) -> Result<()> {
+ let name = match &self.session {
+ Some(session) => match name {
+ Some(v) => v.to_string(),
+ None => session.name().to_string(),
+ },
+ None => bail!("No session"),
+ };
+ let session_path = self.session_file(&name)?;
if let Some(session) = self.session.as_mut() {
- if !name.is_empty() {
- session.set_name(name);
- }
- session.save(&sessions_dir)?;
+ session.save(&session_path)?;
}
Ok(())
}
+ pub fn edit_session(&mut self) -> Result<()> {
+ let name = match &self.session {
+ Some(session) => session.name().to_string(),
+ None => bail!("No session"),
+ };
+ let editor = match self.buffer_editor() {
+ Some(editor) => editor,
+ None => bail!("No editor, please set $EDITOR/$VISUAL."),
+ };
+ let session_path = self.session_file(&name)?;
+ self.save_session(Some(&name))?;
+ edit_file(&editor, &session_path).with_context(|| {
+ format!(
+ "Failed to edit '{}' with '{editor}'",
+ session_path.display()
+ )
+ })?;
+ self.session = Some(Session::load(self, &name, &session_path)?);
+ Ok(())
+ }
+
pub fn clear_session_messages(&mut self) -> Result<()> {
if let Some(session) = self.session.as_mut() {
session.clear_messages();
diff --git a/src/config/session.rs b/src/config/session.rs
index 7400f30..b95fcc5 100644
--- a/src/config/session.rs
+++ b/src/config/session.rs
@@ -240,10 +240,6 @@ impl Session {
(tokens, percent)
}
- pub fn set_name(&mut self, name: &str) {
- self.name = name.to_string();
- }
-
pub fn set_role(&mut self, role: Role) {
self.model_id = role.model().id();
self.temperature = role.temperature();
@@ -290,7 +286,7 @@ impl Session {
self.dirty = true;
}
- pub fn exit(&mut self, sessions_dir: &Path, is_repl: bool) -> Result<()> {
+ pub fn exit(&mut self, session_dir: &Path, is_repl: bool) -> Result<()> {
let save_session = self.save_session();
if self.dirty && save_session != Some(false) {
if save_session.is_none() {
@@ -315,25 +311,26 @@ impl Session {
.prompt()?;
}
}
- self.save(sessions_dir)?;
+ let session_path = session_dir.join(format!("{}.yaml", self.name()));
+ self.save(&session_path)?;
}
Ok(())
}
- pub fn save(&mut self, sessions_dir: &Path) -> Result<()> {
- let mut session_path = sessions_dir.to_path_buf();
- session_path.push(format!("{}.yaml", self.name()));
- if !sessions_dir.exists() {
- create_dir_all(sessions_dir).with_context(|| {
- format!("Failed to create session_dir '{}'", sessions_dir.display())
- })?;
+ pub fn save(&mut self, session_path: &Path) -> Result<()> {
+ if let Some(sessions_dir) = session_path.parent() {
+ if !sessions_dir.exists() {
+ create_dir_all(sessions_dir).with_context(|| {
+ format!("Failed to create session_dir '{}'", sessions_dir.display())
+ })?;
+ }
}
self.path = Some(session_path.display().to_string());
let content = serde_yaml::to_string(&self)
.with_context(|| format!("Failed to serde session {}", self.name))?;
- fs::write(&session_path, content).with_context(|| {
+ fs::write(session_path, content).with_context(|| {
format!(
"Failed to write session {} to {}",
self.name,