Skip to main content

walrus_core/agent/
builder.rs

1//! Fluent builder for constructing an [`Agent`].
2
3use super::tool::ToolSender;
4use crate::{
5    agent::{Agent, config::AgentConfig},
6    model::{Model, Tool},
7};
8
9/// Fluent builder for [`Agent<M>`].
10///
11/// Requires a model at construction. Use [`AgentConfig`] builder methods
12/// for field configuration, then pass it via [`AgentBuilder::config`].
13pub struct AgentBuilder<M: Model> {
14    config: AgentConfig,
15    model: M,
16    tools: Vec<Tool>,
17    tool_tx: Option<ToolSender>,
18}
19
20impl<M: Model> AgentBuilder<M> {
21    /// Create a new builder with the given model.
22    pub fn new(model: M) -> Self {
23        Self {
24            config: AgentConfig::default(),
25            model,
26            tools: Vec::new(),
27            tool_tx: None,
28        }
29    }
30
31    /// Set the full config, replacing all fields.
32    pub fn config(mut self, config: AgentConfig) -> Self {
33        self.config = config;
34        self
35    }
36
37    /// Set the tool schemas advertised to the LLM.
38    pub fn tools(mut self, tools: Vec<Tool>) -> Self {
39        self.tools = tools;
40        self
41    }
42
43    /// Set the tool sender for dispatching tool calls.
44    pub fn tool_tx(mut self, tx: ToolSender) -> Self {
45        self.tool_tx = Some(tx);
46        self
47    }
48
49    /// Build the [`Agent`].
50    pub fn build(self) -> Agent<M> {
51        Agent {
52            config: self.config,
53            model: self.model,
54            tools: self.tools,
55            tool_tx: self.tool_tx,
56        }
57    }
58}