Skip to content

Commit

Permalink
refactor: init agent variables (#977)
Browse files Browse the repository at this point in the history
  • Loading branch information
sigoden authored Nov 6, 2024
1 parent cc74b36 commit 503c282
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
8 changes: 5 additions & 3 deletions src/config/agent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,11 @@ impl Agent {
None
};

let shared_variables = agent_config.variables.clone();

Ok(Self {
name: name.to_string(),
config: agent_config,
definition,
shared_variables,
shared_variables: Default::default(),
session_variables: None,
functions,
rag,
Expand Down Expand Up @@ -229,6 +227,10 @@ impl Agent {
}
}

pub fn config_variables(&self) -> &IndexMap<String, String> {
&self.config.variables
}

pub fn shared_variables(&self) -> &IndexMap<String, String> {
&self.shared_variables
}
Expand Down
16 changes: 11 additions & 5 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1062,8 +1062,8 @@ impl Config {
session.exit(&sessions_dir, self.working_mode.is_repl())?;
self.last_message = None;
}
if self.agent.is_some() {
self.init_agent_shared_variables()?;
if let Some(agent) = self.agent.as_mut() {
agent.set_session_variables(None);
}
Ok(())
}
Expand Down Expand Up @@ -1863,9 +1863,8 @@ impl Config {
None => return Ok(()),
};
let new_variables =
Agent::init_agent_variables(agent.defined_variables(), agent.shared_variables())?;
Agent::init_agent_variables(agent.defined_variables(), agent.config_variables())?;
agent.set_shared_variables(new_variables);
agent.set_session_variables(None);
Ok(())
}

Expand All @@ -1875,9 +1874,16 @@ impl Config {
_ => return Ok(()),
};
let shared_variables = agent.shared_variables();
let mut all_variables = shared_variables.clone();
let mut all_variables = if shared_variables.is_empty() {
agent.config_variables().clone()
} else {
shared_variables.clone()
};
all_variables.extend(session.agent_variables().clone());
let new_variables = Agent::init_agent_variables(agent.defined_variables(), &all_variables)?;
if shared_variables.is_empty() {
agent.set_shared_variables(new_variables.clone());
}
agent.set_session_variables(Some(new_variables));
session.sync_agent(agent, false);
Ok(())
Expand Down

0 comments on commit 503c282

Please sign in to comment.