diff options
| author | sigoden <sigoden@gmail.com> | 2024-10-22 07:38:45 +0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-10-22 07:38:45 +0800 |
| commit | c7d85c624b1bd4ab8118ff80d692ee2b40669537 (patch) | |
| tree | 90f1c957645add0f6eb33c9664257407a3d41a57 | |
| parent | a165fd19e752e3a998dedfe640e9ec838c2737c6 (diff) | |
| download | aichat-c7d85c624b1bd4ab8118ff80d692ee2b40669537.tar.gz | |
refactor: several improvements (#943)
- rename agents_config_dir to agents_data_dir
- do not save empty role name while saving the session
- rename delete keys (roles/sessions/rags/agents => role/session/rag/agent-data)
- improve some printing messages
| -rw-r--r-- | src/config/agent.rs | 4 | ||||
| -rw-r--r-- | src/config/mod.rs | 32 | ||||
| -rw-r--r-- | src/config/role.rs | 2 | ||||
| -rw-r--r-- | src/config/session.rs | 4 | ||||
| -rw-r--r-- | src/main.rs | 2 | ||||
| -rw-r--r-- | src/rag/mod.rs | 4 | ||||
| -rw-r--r-- | src/repl/mod.rs | 4 | ||||
| -rw-r--r-- | src/utils/mod.rs | 8 |
8 files changed, 34 insertions, 26 deletions
diff --git a/src/config/agent.rs b/src/config/agent.rs index 9dcf62b..d4af67a 100644 --- a/src/config/agent.rs +++ b/src/config/agent.rs @@ -102,7 +102,7 @@ impl Agent { format!("Failed to save agent config to '{}'", config_path.display()) })?; - println!("✨ Saved agent config to '{}'", config_path.display()); + println!("✨ Saved agent config to '{}'.", config_path.display()); Ok(()) } @@ -114,7 +114,7 @@ impl Agent { .display() .to_string() .into(); - value["config_dir"] = Config::agent_config_dir(&self.name)? + value["data_dir"] = Config::agent_data_dir(&self.name)? .display() .to_string() .into(); diff --git a/src/config/mod.rs b/src/config/mod.rs index 0f3f89e..142f115 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -300,7 +300,7 @@ impl Config { Ok(value) => Ok(PathBuf::from(value)), Err(_) => Self::local_path(MESSAGES_FILE_NAME), }, - Some(agent) => Ok(Self::agent_config_dir(agent.name())?.join(MESSAGES_FILE_NAME)), + Some(agent) => Ok(Self::agent_data_dir(agent.name())?.join(MESSAGES_FILE_NAME)), } } @@ -310,7 +310,7 @@ impl Config { Ok(value) => Ok(PathBuf::from(value)), Err(_) => Self::local_path(SESSIONS_DIR_NAME), }, - Some(agent) => Ok(Self::agent_config_dir(agent.name())?.join(SESSIONS_DIR_NAME)), + Some(agent) => Ok(Self::agent_data_dir(agent.name())?.join(SESSIONS_DIR_NAME)), } } @@ -348,27 +348,27 @@ impl Config { Ok(path) } - pub fn agents_config_dir() -> Result<PathBuf> { + pub fn agents_data_dir() -> Result<PathBuf> { Self::local_path(AGENTS_DIR_NAME) } - pub fn agent_config_dir(name: &str) -> Result<PathBuf> { - match env::var(format!("{}_CONFIG_DIR", normalize_env_name(name))) { + pub fn agent_data_dir(name: &str) -> Result<PathBuf> { + match env::var(format!("{}_DATA_DIR", normalize_env_name(name))) { Ok(value) => Ok(PathBuf::from(value)), - Err(_) => Ok(Self::agents_config_dir()?.join(name)), + Err(_) => Ok(Self::agents_data_dir()?.join(name)), } } pub fn agent_config_file(name: &str) -> Result<PathBuf> { - Ok(Self::agent_config_dir(name)?.join(CONFIG_FILE_NAME)) + Ok(Self::agent_data_dir(name)?.join(CONFIG_FILE_NAME)) } pub fn agent_rag_file(agent_name: &str, rag_name: &str) -> Result<PathBuf> { - Ok(Self::agent_config_dir(agent_name)?.join(format!("{rag_name}.yaml"))) + Ok(Self::agent_data_dir(agent_name)?.join(format!("{rag_name}.yaml"))) } pub fn agent_variables_file(name: &str) -> Result<PathBuf> { - Ok(Self::agent_config_dir(name)?.join(AGENT_VARIABLES_FILE_NAME)) + Ok(Self::agent_data_dir(name)?.join(AGENT_VARIABLES_FILE_NAME)) } pub fn agents_functions_dir() -> Result<PathBuf> { @@ -649,10 +649,10 @@ impl Config { pub fn delete(config: &GlobalConfig, kind: &str) -> Result<()> { let (dir, file_ext) = match kind { - "roles" => (Self::roles_dir()?, Some(".md")), - "sessions" => (config.read().sessions_dir()?, Some(".yaml")), - "rags" => (Self::rags_dir()?, Some(".yaml")), - "agents" => (Self::agents_config_dir()?, None), + "role" => (Self::roles_dir()?, Some(".md")), + "session" => (config.read().sessions_dir()?, Some(".yaml")), + "rag" => (Self::rags_dir()?, Some(".yaml")), + "agent-data" => (Self::agents_data_dir()?, None), _ => bail!("Unknown kind '{kind}'"), }; let names = match read_dir(&dir) { @@ -711,7 +711,7 @@ impl Config { } } } - println!("✨ Successfully deleted {kind}"); + println!("✨ Successfully deleted {kind}."); Ok(()) } @@ -1551,7 +1551,7 @@ impl Config { .map(|v| (format!("{v} "), None)) .collect() } - ".delete" => map_completion_values(vec!["roles", "sessions", "rags", "agents"]), + ".delete" => map_completion_values(vec!["role", "session", "rag", "agent-data"]), _ => vec![], }; filter = args[0] @@ -2122,7 +2122,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 config file to '{}'.\n", config_path.display()); Ok(()) } diff --git a/src/config/role.rs b/src/config/role.rs index 9b9f46d..8c56f0c 100644 --- a/src/config/role.rs +++ b/src/config/role.rs @@ -167,7 +167,7 @@ impl Role { })?; if is_repl { - println!("✨ Saved role to '{}'", role_path.display()); + println!("✨ Saved role to '{}'.", role_path.display()); } if role_name != self.name { diff --git a/src/config/session.rs b/src/config/session.rs index 2ad517c..717188b 100644 --- a/src/config/session.rs +++ b/src/config/session.rs @@ -240,7 +240,7 @@ impl Session { self.top_p = role.top_p(); self.use_tools = role.use_tools(); self.model = role.model().clone(); - self.role_name = Some(role.name().to_string()); + self.role_name = convert_option_string(role.name()); self.role_prompt = role.prompt().to_string(); self.dirty = true; } @@ -349,7 +349,7 @@ impl Session { })?; if is_repl { - println!("✨ Saved session to '{}'", session_path.display()); + println!("✨ Saved session to '{}'.", session_path.display()); } if self.name() != session_name { diff --git a/src/main.rs b/src/main.rs index a7cc9f1..f95be6a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -299,7 +299,7 @@ async fn shell_execute(config: &GlobalConfig, shell: &Shell, mut input: Input) - } "c" => { set_text(&eval_str)?; - println!("{}", dimmed_text("✓ Copied the command")); + println!("{}", dimmed_text("✓ Copied the command.")); } _ => {} } diff --git a/src/rag/mod.rs b/src/rag/mod.rs index 87f7a69..48ace8e 100644 --- a/src/rag/mod.rs +++ b/src/rag/mod.rs @@ -99,7 +99,7 @@ impl Rag { }, }; if rag.save()? { - println!("✨ Saved rag to '{}'", save_path.display()); + println!("✨ Saved rag to '{}'.", save_path.display()); } Ok(rag) } @@ -148,7 +148,7 @@ impl Rag { }, }; if self.save()? { - println!("✨ Saved rag to '{}'", self.path); + println!("✨ Saved rag to '{}'.", self.path); } Ok(()) } diff --git a/src/repl/mod.rs b/src/repl/mod.rs index 6b026b5..e2181cc 100644 --- a/src/repl/mod.rs +++ b/src/repl/mod.rs @@ -375,7 +375,7 @@ impl Repl { let ret = Config::compress_session(&self.config).await; spinner.stop(); ret?; - println!("✨ Successfully compressed the session"); + println!("✨ Successfully compressed the session."); } _ => { println!(r#"Usage: .compress session"#) @@ -455,7 +455,7 @@ impl Repl { Config::delete(&self.config, args)?; } _ => { - println!("Usage: .delete <roles|sessions|rags|agents>") + println!("Usage: .delete <role|session|rag|agent-data>") } }, ".copy" => { diff --git a/src/utils/mod.rs b/src/utils/mod.rs index 0bd1d41..a551855 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -117,6 +117,14 @@ where } } +pub fn convert_option_string(value: &str) -> Option<String> { + if value.is_empty() { + None + } else { + Some(value.to_string()) + } +} + pub fn fuzzy_match(text: &str, pattern: &str) -> bool { let text_chars: Vec<char> = text.chars().collect(); let pattern_chars: Vec<char> = pattern.chars().collect(); |
