summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorsigoden <sigoden@gmail.com>2024-10-09 08:28:34 +0800
committerGitHub <noreply@github.com>2024-10-09 08:28:34 +0800
commita3e2c2a14718b4f23831cea9bce14ee4430dc90b (patch)
tree957cee02cd54edf030e630fee7d4bda8f1449294
parent4bc74f7e9006fc7f182c9dc6ea309ea83c5bb4f9 (diff)
downloadaichat-a3e2c2a14718b4f23831cea9bce14ee4430dc90b.tar.gz
refactor: throw error if using non-exist RAG in CMD mode (#917)
-rw-r--r--src/config/agent.rs3
-rw-r--r--src/config/mod.rs12
-rw-r--r--src/main.rs2
-rw-r--r--src/render/mod.rs2
4 files changed, 14 insertions, 5 deletions
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<Self> {
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
}
diff --git a/src/main.rs b/src/main.rs
index 3c52f0d..299dc6b 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -50,7 +50,7 @@ async fn main() -> Result<()> {
} else if text.is_none() && cli.file.is_empty() {
WorkingMode::Repl
} else {
- WorkingMode::Command
+ WorkingMode::Cmd
};
setup_logger(working_mode.is_serve())?;
let config = Arc::new(RwLock::new(Config::init(working_mode)?));
diff --git a/src/render/mod.rs b/src/render/mod.rs
index 97161f8..b2ed55d 100644
--- a/src/render/mod.rs
+++ b/src/render/mod.rs
@@ -26,5 +26,5 @@ pub async fn render_stream(
}
pub fn render_error(err: anyhow::Error) {
- eprintln!("{}", error_text(&pretty_error(&err)));
+ eprint!("{}", error_text(&pretty_error(&err)));
}