Skip to main content

agent_base/types/
config.rs

1use serde_json::Value;
2
3#[derive(Clone, Debug)]
4pub struct RetryConfig {
5    pub max_retries: u32,
6    pub initial_backoff_ms: u64,
7    pub max_backoff_ms: u64,
8    pub backoff_multiplier: f64,
9    pub jitter: bool,
10}
11
12impl Default for RetryConfig {
13    fn default() -> Self {
14        Self {
15            max_retries: 3,
16            initial_backoff_ms: 500,
17            max_backoff_ms: 10_000,
18            backoff_multiplier: 2.0,
19            jitter: true,
20        }
21    }
22}
23
24impl RetryConfig {
25    pub fn new() -> Self {
26        Self::default()
27    }
28
29    pub fn max_retries(mut self, n: u32) -> Self {
30        self.max_retries = n;
31        self
32    }
33
34    pub fn initial_backoff_ms(mut self, ms: u64) -> Self {
35        self.initial_backoff_ms = ms;
36        self
37    }
38
39    pub fn max_backoff_ms(mut self, ms: u64) -> Self {
40        self.max_backoff_ms = ms;
41        self
42    }
43
44    pub fn no_jitter(mut self) -> Self {
45        self.jitter = false;
46        self
47    }
48}
49
50#[derive(Clone, Debug)]
51pub enum ResponseFormat {
52    JsonObject,
53    JsonSchema {
54        name: String,
55        schema: Value,
56    },
57}
58
59impl ResponseFormat {
60    pub fn to_api_value(&self) -> Value {
61        match self {
62            ResponseFormat::JsonObject => {
63                serde_json::json!({ "type": "json_object" })
64            }
65            ResponseFormat::JsonSchema { name, schema } => {
66                serde_json::json!({
67                    "type": "json_schema",
68                    "json_schema": {
69                        "name": name,
70                        "schema": schema,
71                    }
72                })
73            }
74        }
75    }
76}
77
78#[derive(Clone, Debug)]
79pub struct AgentConfig {
80    pub system_prompt: Option<String>,
81    pub enable_thought: bool,
82    pub enable_thinking: Option<bool>,
83    pub max_turns: Option<u32>,
84    pub tool_timeout_ms: Option<u64>,
85    pub max_tool_output_chars: Option<usize>,
86    pub response_format: Option<ResponseFormat>,
87    pub llm_retry: Option<RetryConfig>,
88}
89
90impl Default for AgentConfig {
91    fn default() -> Self {
92        Self {
93            system_prompt: None,
94            enable_thought: false,
95            enable_thinking: None,
96            max_turns: None,
97            tool_timeout_ms: None,
98            max_tool_output_chars: None,
99            response_format: None,
100            llm_retry: None,
101        }
102    }
103}