Skip to main content

agent_io/agent/
config.rs

1//! Agent configuration types
2
3use std::collections::HashMap;
4use std::sync::Arc;
5
6use derive_builder::Builder;
7
8use crate::llm::ToolChoice;
9use crate::tools::{EphemeralConfig as ToolEphemeralConfig, Tool};
10
11/// Default maximum iterations
12pub const DEFAULT_MAX_ITERATIONS: usize = 200;
13
14/// Agent configuration
15#[derive(Builder, Clone)]
16#[builder(pattern = "owned")]
17pub struct AgentConfig {
18    /// System prompt
19    #[builder(setter(into, strip_option), default = "None")]
20    pub system_prompt: Option<String>,
21
22    /// Maximum iterations before stopping
23    #[builder(default = "DEFAULT_MAX_ITERATIONS")]
24    pub max_iterations: usize,
25
26    /// Tool choice strategy
27    #[builder(default = "ToolChoice::Auto")]
28    pub tool_choice: ToolChoice,
29
30    /// Enable context compaction
31    #[builder(default = "false")]
32    pub enable_compaction: bool,
33
34    /// Compaction threshold (ratio of context window)
35    #[builder(default = "0.80")]
36    pub compaction_threshold: f32,
37
38    /// Enable cost tracking
39    #[builder(default = "false")]
40    pub include_cost: bool,
41}
42
43impl Default for AgentConfig {
44    fn default() -> Self {
45        Self {
46            system_prompt: None,
47            max_iterations: DEFAULT_MAX_ITERATIONS,
48            tool_choice: ToolChoice::Auto,
49            enable_compaction: false,
50            compaction_threshold: 0.80,
51            include_cost: false,
52        }
53    }
54}
55
56/// Ephemeral message configuration per tool
57#[derive(Debug, Clone, Copy)]
58pub struct EphemeralConfig {
59    /// How many outputs to keep (None = not ephemeral)
60    pub keep_count: usize,
61}
62
63impl Default for EphemeralConfig {
64    fn default() -> Self {
65        Self { keep_count: 1 }
66    }
67}
68
69pub(crate) fn build_ephemeral_config(tools: &[Arc<dyn Tool>]) -> HashMap<String, EphemeralConfig> {
70    tools
71        .iter()
72        .filter_map(|tool| {
73            let keep_count = match tool.ephemeral() {
74                ToolEphemeralConfig::None => return None,
75                ToolEphemeralConfig::Single => 1,
76                ToolEphemeralConfig::Count(count) => count,
77            };
78            Some((tool.name().to_string(), EphemeralConfig { keep_count }))
79        })
80        .collect()
81}