From a3e2c2a14718b4f23831cea9bce14ee4430dc90b Mon Sep 17 00:00:00 2001 From: sigoden Date: Wed, 9 Oct 2024 08:28:34 +0800 Subject: refactor: throw error if using non-exist RAG in CMD mode (#917) --- src/config/agent.rs | 3 +++ src/config/mod.rs | 12 +++++++++--- 2 files changed, 12 insertions(+), 3 deletions(-) (limited to 'src/config') diff --git a/src/config/agent.rs b/src/config/agent.rs index 813df3a..2f44477 100644 --- a/src/config/agent.rs +++ b/src/config/agent.rs @@ -32,6 +32,9 @@ impl Agent { ) -> Result { let functions_dir = Config::agent_functions_dir(name)?; let definition_file_path = functions_dir.join("index.yaml"); + if !definition_file_path.exists() { + bail!("Unknown agent `{name}`"); + } let functions_file_path = functions_dir.join("functions.json"); let variables_path = Config::agent_variables_file(name)?; let rag_path = Config::agent_rag_file(name, "rag")?; diff --git a/src/config/mod.rs b/src/config/mod.rs index 8e3e2c3..33bde56 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -205,7 +205,7 @@ impl Default for Config { agent: None, model: Default::default(), functions: Default::default(), - working_mode: WorkingMode::Command, + working_mode: WorkingMode::Cmd, last_message: None, } } @@ -1199,6 +1199,9 @@ impl Config { Some(name) => { let rag_path = config.read().rag_file(name)?; if !rag_path.exists() { + if config.read().working_mode.is_cmd() { + bail!("Unknown RAG '{name}'") + } Rag::init(config, name, &rag_path, &[], abort_signal).await? } else { Rag::load(config, name, &rag_path)? @@ -1376,7 +1379,7 @@ impl Config { pub fn apply_prelude(&mut self) -> Result<()> { let prelude = match self.working_mode { - WorkingMode::Command => self.prelude.as_ref(), + WorkingMode::Cmd => self.prelude.as_ref(), WorkingMode::Repl => self.repl_prelude.as_ref().or(self.prelude.as_ref()), WorkingMode::Serve => return Ok(()), }; @@ -2043,12 +2046,15 @@ pub fn load_env_file() -> Result<()> { #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] pub enum WorkingMode { - Command, + Cmd, Repl, Serve, } impl WorkingMode { + pub fn is_cmd(&self) -> bool { + *self == WorkingMode::Cmd + } pub fn is_repl(&self) -> bool { *self == WorkingMode::Repl } -- cgit v1.2.3