Skip to main content

walrus_core/agent/
builder.rs

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