Skip to content

Commit

Permalink
refactor: throw error if using non-exist RAG in CMD mode
Browse files Browse the repository at this point in the history
  • Loading branch information
sigoden committed Oct 9, 2024
1 parent 4bc74f7 commit 8f5156a
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 5 deletions.
3 changes: 3 additions & 0 deletions src/config/agent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")?;
Expand Down
12 changes: 9 additions & 3 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}
Expand Down Expand Up @@ -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)?
Expand Down Expand Up @@ -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(()),
};
Expand Down Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?));
Expand Down
2 changes: 1 addition & 1 deletion src/render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
}

0 comments on commit 8f5156a

Please sign in to comment.