From 6211d01a648e941fc69954d0855bcdcef98f27b9 Mon Sep 17 00:00:00 2001 From: sigoden Date: Sat, 14 Sep 2024 18:05:29 +0800 Subject: feat: add `.save agent-config` repl command (#870) --- src/config/agent.rs | 35 ++++++++++++++++++++++++++--------- 1 file changed, 26 insertions(+), 9 deletions(-) (limited to 'src/config/agent.rs') diff --git a/src/config/agent.rs b/src/config/agent.rs index 36af15e..813df3a 100644 --- a/src/config/agent.rs +++ b/src/config/agent.rs @@ -39,7 +39,7 @@ impl Agent { let agent_config = if config_path.exists() { AgentConfig::load(&config_path)? } else { - AgentConfig::default() + AgentConfig::new(&config.read()) }; let mut definition = AgentDefinition::load(&definition_file_path)?; init_variables(&variables_path, &mut definition.variables) @@ -91,6 +91,18 @@ impl Agent { }) } + pub fn save_config(&self) -> Result<()> { + let config_path = Config::agent_config_file(&self.name)?; + ensure_parent_exists(&config_path)?; + let content = serde_yaml::to_string(&self.config)?; + fs::write(&config_path, content).with_context(|| { + format!("Failed to save agent config to '{}'", config_path.display()) + })?; + + println!("✨ Saved agent config to '{}'", config_path.display()); + Ok(()) + } + pub fn export(&self) -> Result { let mut agent = self.clone(); agent.definition.instructions = self.interpolated_instructions(); @@ -143,6 +155,10 @@ impl Agent { self.config.agent_prelude.as_deref() } + pub fn set_agent_prelude(&mut self, value: Option) { + self.config.agent_prelude = value; + } + pub fn variables(&self) -> &[AgentVariable] { &self.definition.variables } @@ -208,22 +224,23 @@ impl RoleLike for Agent { #[derive(Debug, Clone, Default, Deserialize, Serialize)] pub struct AgentConfig { - #[serde( - rename(serialize = "model", deserialize = "model"), - skip_serializing_if = "Option::is_none" - )] + #[serde(rename(serialize = "model", deserialize = "model"))] pub model_id: Option, - #[serde(skip_serializing_if = "Option::is_none")] pub temperature: Option, - #[serde(skip_serializing_if = "Option::is_none")] pub top_p: Option, - #[serde(skip_serializing_if = "Option::is_none")] pub use_tools: Option, - #[serde(skip_serializing_if = "Option::is_none")] pub agent_prelude: Option, } impl AgentConfig { + pub fn new(config: &Config) -> Self { + Self { + use_tools: config.use_tools.clone(), + agent_prelude: config.agent_prelude.clone(), + ..Default::default() + } + } + pub fn load(path: &Path) -> Result { let contents = read_to_string(path) .with_context(|| format!("Failed to read agent config file at '{}'", path.display()))?; -- cgit v1.2.3