Skip to main content

crabtalk_core/agent/
builder.rs

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