summaryrefslogtreecommitdiffstats
path: root/src/config
diff options
context:
space:
mode:
authorsigoden <sigoden@gmail.com>2025-01-19 17:32:22 +0800
committerGitHub <noreply@github.com>2025-01-19 17:32:22 +0800
commitbc532d99be0c407fe5e6ef82018350837ac2fb97 (patch)
tree8d26ad57b79e0fbf46e9299b5fffd4348b8fea3d /src/config
parentef5febaa0ea5779d11bba062a2f623262931147e (diff)
downloadaichat-bc532d99be0c407fe5e6ef82018350837ac2fb97.tar.gz
feat: support `.edit config` and `.edit agent-config` repl commands (#1103)
Diffstat (limited to 'src/config')
-rw-r--r--src/config/mod.rs52
-rw-r--r--src/config/session.rs2
2 files changed, 51 insertions, 3 deletions
diff --git a/src/config/mod.rs b/src/config/mod.rs
index e2f9048..97e095c 100644
--- a/src/config/mod.rs
+++ b/src/config/mod.rs
@@ -474,6 +474,18 @@ impl Config {
Ok((log_level, log_path))
}
+ pub fn edit_config(&self) -> Result<()> {
+ let config_path = Self::config_file();
+ let editor = self.editor()?;
+ edit_file(&editor, &config_path)?;
+ println!(
+ "NOTE: Remember to restart {} if there are changes made to '{}",
+ env!("CARGO_CRATE_NAME"),
+ config_path.display(),
+ );
+ Ok(())
+ }
+
pub fn current_model(&self) -> &Model {
if let Some(session) = self.session.as_ref() {
session.model()
@@ -953,6 +965,9 @@ impl Config {
ensure_parent_exists(&role_path)?;
let editor = self.editor()?;
edit_file(&editor, &role_path)?;
+ if self.working_mode.is_repl() {
+ println!("✓ Saved the role to '{}'.", role_path.display());
+ }
Ok(())
}
@@ -1508,6 +1523,34 @@ impl Config {
}
}
+ pub fn edit_agent_config(&self) -> Result<()> {
+ let agent_name = match &self.agent {
+ Some(agent) => agent.name(),
+ None => bail!("No agent"),
+ };
+ let agent_config_path = Config::agent_config_file(agent_name);
+ ensure_parent_exists(&agent_config_path)?;
+ if !agent_config_path.exists() {
+ std::fs::write(
+ &agent_config_path,
+ "# see https://github.com/sigoden/aichat/blob/main/config.agent.example.yaml\n",
+ )
+ .with_context(|| {
+ format!(
+ "Failed to write to agent config file at '{}'",
+ agent_config_path.display()
+ )
+ })?;
+ }
+ let editor = self.editor()?;
+ edit_file(&editor, &agent_config_path)?;
+ println!(
+ "NOTE: Remember to reload the agent if there are changes made to '{}'",
+ agent_config_path.display()
+ );
+ Ok(())
+ }
+
pub fn exit_agent(&mut self) -> Result<()> {
self.exit_session()?;
if self.agent.take().is_some() {
@@ -2550,7 +2593,12 @@ fn create_config_file(config_path: &Path) -> Result<()> {
);
ensure_parent_exists(config_path)?;
- std::fs::write(config_path, config_data).with_context(|| "Failed to write to config file")?;
+ std::fs::write(config_path, config_data).with_context(|| {
+ format!(
+ "Failed to write to config file at '{}'",
+ config_path.display()
+ )
+ })?;
#[cfg(unix)]
{
use std::os::unix::prelude::PermissionsExt;
@@ -2558,7 +2606,7 @@ fn create_config_file(config_path: &Path) -> Result<()> {
std::fs::set_permissions(config_path, perms)?;
}
- println!("✓ Saved config file to '{}'.\n", config_path.display());
+ println!("✓ Saved the config file to '{}'.\n", config_path.display());
Ok(())
}
diff --git a/src/config/session.rs b/src/config/session.rs
index 13af751..2bc1b6c 100644
--- a/src/config/session.rs
+++ b/src/config/session.rs
@@ -438,7 +438,7 @@ impl Session {
})?;
if is_repl {
- println!("✓ Saved session to '{}'.", session_path.display());
+ println!("✓ Saved the session to '{}'.", session_path.display());
}
if self.name() != session_name {