Skip to main content

walrus_core/agent/
builder.rs

1//! Fluent builder for constructing an [`Agent`].
2
3use crate::{
4    agent::{Agent, CompactHook, config::AgentConfig, tool::ToolSender},
5    model::{Model, Tool},
6};
7use std::sync::Arc;
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    compact_hook: Option<Arc<dyn CompactHook>>,
19}
20
21impl<M: Model> AgentBuilder<M> {
22    /// Create a new builder with the given model.
23    pub fn new(model: M) -> Self {
24        Self {
25            config: AgentConfig::default(),
26            model,
27            tools: Vec::new(),
28            tool_tx: None,
29            compact_hook: None,
30        }
31    }
32
33    /// Set the full config, replacing all fields.
34    pub fn config(mut self, config: AgentConfig) -> Self {
35        self.config = config;
36        self
37    }
38
39    /// Set the tool schemas advertised to the LLM.
40    pub fn tools(mut self, tools: Vec<Tool>) -> Self {
41        self.tools = tools;
42        self
43    }
44
45    /// Set the tool sender for dispatching tool calls.
46    pub fn tool_tx(mut self, tx: ToolSender) -> Self {
47        self.tool_tx = Some(tx);
48        self
49    }
50
51    /// Set the compact hook for auto-compaction.
52    pub fn compact_hook(mut self, hook: Arc<dyn CompactHook>) -> Self {
53        self.compact_hook = Some(hook);
54        self
55    }
56
57    /// Build the [`Agent`].
58    pub fn build(self) -> Agent<M> {
59        Agent {
60            config: self.config,
61            model: self.model,
62            tools: self.tools,
63            tool_tx: self.tool_tx,
64            compact_hook: self.compact_hook,
65        }
66    }
67}