diff options
| author | sigoden <sigoden@gmail.com> | 2024-09-14 18:05:29 +0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-09-14 18:05:29 +0800 |
| commit | 6211d01a648e941fc69954d0855bcdcef98f27b9 (patch) | |
| tree | 88d3a44b8e6906bbcb39917ea72e5cc3aa5087d6 /src/config/agent.rs | |
| parent | 5a26c59a12e25cc96c6eb44c64395e673207135c (diff) | |
| download | aichat-6211d01a648e941fc69954d0855bcdcef98f27b9.tar.gz | |
feat: add `.save agent-config` repl command (#870)
Diffstat (limited to 'src/config/agent.rs')
| -rw-r--r-- | src/config/agent.rs | 35 |
1 files changed, 26 insertions, 9 deletions
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<String> { 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<String>) { + 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<String>, - #[serde(skip_serializing_if = "Option::is_none")] pub temperature: Option<f64>, - #[serde(skip_serializing_if = "Option::is_none")] pub top_p: Option<f64>, - #[serde(skip_serializing_if = "Option::is_none")] pub use_tools: Option<String>, - #[serde(skip_serializing_if = "Option::is_none")] pub agent_prelude: Option<String>, } 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<Self> { let contents = read_to_string(path) .with_context(|| format!("Failed to read agent config file at '{}'", path.display()))?; |
