Skip to content

Commit 79973a2

Browse files
authored
feat: change agent config/variables (#967)
- abandon `.save agent-config` - add variables to agent config.yaml - don't save agent variables after initializing the agent and using `.variable <key> <value>`
1 parent 9fa3d8c commit 79973a2

File tree

4 files changed

+29
-84
lines changed

4 files changed

+29
-84
lines changed

config.agent.example.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Agent-specific configuration
2+
3+
model: openai:gpt-4o # Specify the LLM to use
4+
temperature: null # Set default temperature parameter
5+
top_p: null # Set default top-p parameter, range (0, 1)
6+
use_tools: null # Which additional tools to use by agent. (e.g. 'fs,web_search')
7+
agent_prelude: null # Set a session to use when starting the agent. (e.g. temp, default)
8+
9+
variables: # Custom default values for the agent variables
10+
<key>: <value>

src/config/agent.rs

Lines changed: 17 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,7 @@ use crate::{client::Model, function::Functions};
44

55
use anyhow::{Context, Result};
66
use inquire::{validator::Validation, Text};
7-
use std::{
8-
fs::{self, read_to_string},
9-
path::Path,
10-
};
7+
use std::{fs::read_to_string, path::Path};
118

129
use serde::{Deserialize, Serialize};
1310

@@ -36,7 +33,6 @@ impl Agent {
3633
bail!("Unknown agent `{name}`");
3734
}
3835
let functions_file_path = functions_dir.join("functions.json");
39-
let variables_path = Config::agent_variables_file(name)?;
4036
let rag_path = Config::agent_rag_file(name, "rag")?;
4137
let config_path = Config::agent_config_file(name)?;
4238
let agent_config = if config_path.exists() {
@@ -45,7 +41,7 @@ impl Agent {
4541
AgentConfig::new(&config.read())
4642
};
4743
let mut definition = AgentDefinition::load(&definition_file_path)?;
48-
init_variables(&variables_path, &mut definition.variables)
44+
init_variables(&mut definition.variables, &agent_config.variables)
4945
.context("Failed to init variables")?;
5046

5147
let functions = if functions_file_path.exists() {
@@ -94,18 +90,6 @@ impl Agent {
9490
})
9591
}
9692

97-
pub fn save_config(&self) -> Result<()> {
98-
let config_path = Config::agent_config_file(&self.name)?;
99-
ensure_parent_exists(&config_path)?;
100-
let content = serde_yaml::to_string(&self.config)?;
101-
fs::write(&config_path, content).with_context(|| {
102-
format!("Failed to save agent config to '{}'", config_path.display())
103-
})?;
104-
105-
println!("✨ Saved agent config to '{}'.", config_path.display());
106-
Ok(())
107-
}
108-
10993
pub fn export(&self) -> Result<String> {
11094
let mut agent = self.clone();
11195
agent.definition.instructions = self.interpolated_instructions();
@@ -118,7 +102,7 @@ impl Agent {
118102
.display()
119103
.to_string()
120104
.into();
121-
value["variables_file"] = Config::agent_variables_file(&self.name)?
105+
value["config_file"] = Config::agent_config_file(&self.name)?
122106
.display()
123107
.to_string()
124108
.into();
@@ -166,8 +150,6 @@ impl Agent {
166150
match self.definition.variables.iter_mut().find(|v| v.name == key) {
167151
Some(variable) => {
168152
variable.value = value.to_string();
169-
let variables_path = Config::agent_variables_file(&self.name)?;
170-
save_variables(&variables_path, self.variables())?;
171153
Ok(())
172154
}
173155
None => bail!("Unknown variable '{key}'"),
@@ -229,6 +211,8 @@ pub struct AgentConfig {
229211
pub top_p: Option<f64>,
230212
pub use_tools: Option<String>,
231213
pub agent_prelude: Option<String>,
214+
#[serde(default)]
215+
pub variables: IndexMap<String, String>,
232216
}
233217

234218
impl AgentConfig {
@@ -364,65 +348,37 @@ fn list_agents_impl() -> Result<Vec<String>> {
364348
Ok(agents)
365349
}
366350

367-
fn init_variables(variables_path: &Path, variables: &mut [AgentVariable]) -> Result<()> {
351+
fn init_variables(
352+
variables: &mut [AgentVariable],
353+
config_variable: &IndexMap<String, String>,
354+
) -> Result<()> {
368355
if variables.is_empty() {
369356
return Ok(());
370357
}
371-
let variable_values = if variables_path.exists() {
372-
let content = read_to_string(variables_path).with_context(|| {
373-
format!(
374-
"Failed to read variables from '{}'",
375-
variables_path.display()
376-
)
377-
})?;
378-
let variable_values: IndexMap<String, String> = serde_yaml::from_str(&content)?;
379-
variable_values
380-
} else {
381-
Default::default()
382-
};
383-
let mut initialized = false;
384358
for variable in variables.iter_mut() {
385-
match variable_values.get(&variable.name) {
359+
match config_variable.get(&variable.name) {
386360
Some(value) => variable.value = value.to_string(),
387361
None => {
388-
if !initialized {
389-
println!("The agent has the variables and is initializing them...");
390-
initialized = true;
362+
if let Some(value) = variable.default.clone() {
363+
variable.value = value;
364+
continue;
391365
}
392366
if *IS_STDOUT_TERMINAL {
393-
let mut text =
394-
Text::new(&variable.description).with_validator(|input: &str| {
367+
let value = Text::new(&variable.description)
368+
.with_validator(|input: &str| {
395369
if input.trim().is_empty() {
396370
Ok(Validation::Invalid("This field is required".into()))
397371
} else {
398372
Ok(Validation::Valid)
399373
}
400-
});
401-
if let Some(default) = &variable.default {
402-
text = text.with_default(default);
403-
}
404-
let value = text.prompt()?;
374+
})
375+
.prompt()?;
405376
variable.value = value;
406377
} else {
407378
bail!("Failed to init agent variables in the script mode.");
408379
}
409380
}
410381
}
411382
}
412-
if initialized {
413-
save_variables(variables_path, variables)?;
414-
}
415-
Ok(())
416-
}
417-
418-
fn save_variables(variables_path: &Path, variables: &[AgentVariable]) -> Result<()> {
419-
ensure_parent_exists(variables_path)?;
420-
let variable_values: IndexMap<String, String> = variables
421-
.iter()
422-
.map(|v| (v.name.clone(), v.value.clone()))
423-
.collect();
424-
let content = serde_yaml::to_string(&variable_values)?;
425-
fs::write(variables_path, content)
426-
.with_context(|| format!("Failed to save variables to '{}'", variables_path.display()))?;
427383
Ok(())
428384
}

src/config/mod.rs

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ const FUNCTIONS_DIR_NAME: &str = "functions";
5151
const FUNCTIONS_FILE_NAME: &str = "functions.json";
5252
const FUNCTIONS_BIN_DIR_NAME: &str = "bin";
5353
const AGENTS_DIR_NAME: &str = "agents";
54-
const AGENT_VARIABLES_FILE_NAME: &str = "variables.yaml";
5554

5655
pub const TEMP_ROLE_NAME: &str = "%%";
5756
pub const TEMP_RAG_NAME: &str = "temp";
@@ -367,10 +366,6 @@ impl Config {
367366
Ok(Self::agent_data_dir(agent_name)?.join(format!("{rag_name}.yaml")))
368367
}
369368

370-
pub fn agent_variables_file(name: &str) -> Result<PathBuf> {
371-
Ok(Self::agent_data_dir(name)?.join(AGENT_VARIABLES_FILE_NAME))
372-
}
373-
374369
pub fn agents_functions_dir() -> Result<PathBuf> {
375370
Ok(Self::functions_dir()?.join(AGENTS_DIR_NAME))
376371
}
@@ -1419,14 +1414,6 @@ impl Config {
14191414
Ok(())
14201415
}
14211416

1422-
pub fn save_agent_config(&mut self) -> Result<()> {
1423-
let agent = match &self.agent {
1424-
Some(v) => v,
1425-
None => bail!("No agent"),
1426-
};
1427-
agent.save_config()
1428-
}
1429-
14301417
pub fn exit_agent(&mut self) -> Result<()> {
14311418
self.exit_session()?;
14321419
if self.agent.take().is_some() {

src/repl/mod.rs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ lazy_static::lazy_static! {
3131
const MENU_NAME: &str = "completion_menu";
3232

3333
lazy_static::lazy_static! {
34-
static ref REPL_COMMANDS: [ReplCommand; 35] = [
34+
static ref REPL_COMMANDS: [ReplCommand; 34] = [
3535
ReplCommand::new(".help", "Show this help message", AssertState::pass()),
3636
ReplCommand::new(".info", "View system info", AssertState::pass()),
3737
ReplCommand::new(".model", "Change the current LLM", AssertState::pass()),
@@ -141,11 +141,6 @@ lazy_static::lazy_static! {
141141
"Set agent variable",
142142
AssertState::True(StateFlags::AGENT)
143143
),
144-
ReplCommand::new(
145-
".save agent-config",
146-
"Save the current agent config to file",
147-
AssertState::True(StateFlags::AGENT)
148-
),
149144
ReplCommand::new(
150145
".info agent",
151146
"View agent info",
@@ -346,11 +341,8 @@ impl Repl {
346341
Some(("session", name)) => {
347342
self.config.write().save_session(name)?;
348343
}
349-
Some(("agent-config", _)) => {
350-
self.config.write().save_agent_config()?;
351-
}
352344
_ => {
353-
println!(r#"Usage: .save <role|session|agent-config> [name]"#)
345+
println!(r#"Usage: .save <role|session> [name]"#)
354346
}
355347
}
356348
}

0 commit comments

Comments
 (0)