pub struct AgentBuilder { /* private fields */ }Expand description
Builder for Agent.
Implementations§
Source§impl AgentBuilder
impl AgentBuilder
Sourcepub fn model<M>(self, model: M) -> Selfwhere
M: ChatModel + 'static,
pub fn model<M>(self, model: M) -> Selfwhere
M: ChatModel + 'static,
Sets the model adapter used by the agent.
Examples found in repository?
More examples
examples/di_override.rs (line 97)
43async fn main() -> Result<(), Box<dyn Error>> {
44 let read_dep_tool = ToolSpec::new("read_dep", "read injected value")
45 .with_schema(json!({
46 "type": "object",
47 "properties": {},
48 "required": [],
49 "additionalProperties": false
50 }))?
51 .with_handler(|_args, deps| {
52 let value = deps.get::<u32>().map(|v| *v).unwrap_or_default();
53 async move { Ok(ToolOutcome::Text(value.to_string())) }
54 });
55
56 let done_tool = ToolSpec::new("done", "finish")
57 .with_schema(json!({
58 "type": "object",
59 "properties": {
60 "message": {"type": "string"}
61 },
62 "required": ["message"],
63 "additionalProperties": false
64 }))?
65 .with_handler(|args, _deps| async move {
66 let message = args
67 .get("message")
68 .and_then(|v| v.as_str())
69 .unwrap_or("done");
70 Ok(ToolOutcome::Done(message.to_string()))
71 });
72
73 let model = ScriptedModel::new(vec![
74 Ok(ModelCompletion {
75 text: None,
76 thinking: None,
77 tool_calls: vec![ModelToolCall {
78 id: "call_1".to_string(),
79 name: "read_dep".to_string(),
80 arguments: json!({}),
81 }],
82 usage: None,
83 }),
84 Ok(ModelCompletion {
85 text: None,
86 thinking: None,
87 tool_calls: vec![ModelToolCall {
88 id: "call_2".to_string(),
89 name: "done".to_string(),
90 arguments: json!({"message": "dependency override applied"}),
91 }],
92 usage: None,
93 }),
94 ]);
95
96 let mut agent = Agent::builder()
97 .model(model)
98 .tool(read_dep_tool)
99 .tool(done_tool)
100 .dependency(1_u32)
101 .dependency_override(9_u32)
102 .build()?;
103
104 let response = agent.query("use dependency").await?;
105 println!("final: {response}");
106
107 Ok(())
108}Sourcepub fn tool(self, tool: ToolSpec) -> Self
pub fn tool(self, tool: ToolSpec) -> Self
Adds one tool to the registry.
Examples found in repository?
More examples
examples/di_override.rs (line 98)
43async fn main() -> Result<(), Box<dyn Error>> {
44 let read_dep_tool = ToolSpec::new("read_dep", "read injected value")
45 .with_schema(json!({
46 "type": "object",
47 "properties": {},
48 "required": [],
49 "additionalProperties": false
50 }))?
51 .with_handler(|_args, deps| {
52 let value = deps.get::<u32>().map(|v| *v).unwrap_or_default();
53 async move { Ok(ToolOutcome::Text(value.to_string())) }
54 });
55
56 let done_tool = ToolSpec::new("done", "finish")
57 .with_schema(json!({
58 "type": "object",
59 "properties": {
60 "message": {"type": "string"}
61 },
62 "required": ["message"],
63 "additionalProperties": false
64 }))?
65 .with_handler(|args, _deps| async move {
66 let message = args
67 .get("message")
68 .and_then(|v| v.as_str())
69 .unwrap_or("done");
70 Ok(ToolOutcome::Done(message.to_string()))
71 });
72
73 let model = ScriptedModel::new(vec![
74 Ok(ModelCompletion {
75 text: None,
76 thinking: None,
77 tool_calls: vec![ModelToolCall {
78 id: "call_1".to_string(),
79 name: "read_dep".to_string(),
80 arguments: json!({}),
81 }],
82 usage: None,
83 }),
84 Ok(ModelCompletion {
85 text: None,
86 thinking: None,
87 tool_calls: vec![ModelToolCall {
88 id: "call_2".to_string(),
89 name: "done".to_string(),
90 arguments: json!({"message": "dependency override applied"}),
91 }],
92 usage: None,
93 }),
94 ]);
95
96 let mut agent = Agent::builder()
97 .model(model)
98 .tool(read_dep_tool)
99 .tool(done_tool)
100 .dependency(1_u32)
101 .dependency_override(9_u32)
102 .build()?;
103
104 let response = agent.query("use dependency").await?;
105 println!("final: {response}");
106
107 Ok(())
108}Sourcepub fn config(self, config: AgentConfig) -> Self
pub fn config(self, config: AgentConfig) -> Self
Replaces the full agent config.
Sourcepub fn system_prompt(self, system_prompt: impl Into<String>) -> Self
pub fn system_prompt(self, system_prompt: impl Into<String>) -> Self
Sets the system prompt.
Sourcepub fn require_done_tool(self, require_done_tool: bool) -> Self
pub fn require_done_tool(self, require_done_tool: bool) -> Self
Enables or disables explicit done completion mode.
Sourcepub fn max_iterations(self, max_iterations: u32) -> Self
pub fn max_iterations(self, max_iterations: u32) -> Self
Sets max iterations for each query.
Sourcepub fn tool_choice(self, tool_choice: AgentToolChoice) -> Self
pub fn tool_choice(self, tool_choice: AgentToolChoice) -> Self
Sets tool-choice policy for model invocations.
Sourcepub fn llm_retry_config(
self,
max_retries: u32,
base_delay_ms: u64,
max_delay_ms: u64,
) -> Self
pub fn llm_retry_config( self, max_retries: u32, base_delay_ms: u64, max_delay_ms: u64, ) -> Self
Configures request retry behavior (exponential backoff).
Sets a hidden user prompt injected once if model returns no tool calls.
Sourcepub fn dependency<T>(self, value: T) -> Self
pub fn dependency<T>(self, value: T) -> Self
Inserts a typed runtime dependency.
Examples found in repository?
examples/di_override.rs (line 100)
43async fn main() -> Result<(), Box<dyn Error>> {
44 let read_dep_tool = ToolSpec::new("read_dep", "read injected value")
45 .with_schema(json!({
46 "type": "object",
47 "properties": {},
48 "required": [],
49 "additionalProperties": false
50 }))?
51 .with_handler(|_args, deps| {
52 let value = deps.get::<u32>().map(|v| *v).unwrap_or_default();
53 async move { Ok(ToolOutcome::Text(value.to_string())) }
54 });
55
56 let done_tool = ToolSpec::new("done", "finish")
57 .with_schema(json!({
58 "type": "object",
59 "properties": {
60 "message": {"type": "string"}
61 },
62 "required": ["message"],
63 "additionalProperties": false
64 }))?
65 .with_handler(|args, _deps| async move {
66 let message = args
67 .get("message")
68 .and_then(|v| v.as_str())
69 .unwrap_or("done");
70 Ok(ToolOutcome::Done(message.to_string()))
71 });
72
73 let model = ScriptedModel::new(vec![
74 Ok(ModelCompletion {
75 text: None,
76 thinking: None,
77 tool_calls: vec![ModelToolCall {
78 id: "call_1".to_string(),
79 name: "read_dep".to_string(),
80 arguments: json!({}),
81 }],
82 usage: None,
83 }),
84 Ok(ModelCompletion {
85 text: None,
86 thinking: None,
87 tool_calls: vec![ModelToolCall {
88 id: "call_2".to_string(),
89 name: "done".to_string(),
90 arguments: json!({"message": "dependency override applied"}),
91 }],
92 usage: None,
93 }),
94 ]);
95
96 let mut agent = Agent::builder()
97 .model(model)
98 .tool(read_dep_tool)
99 .tool(done_tool)
100 .dependency(1_u32)
101 .dependency_override(9_u32)
102 .build()?;
103
104 let response = agent.query("use dependency").await?;
105 println!("final: {response}");
106
107 Ok(())
108}Sourcepub fn dependency_named<T>(self, key: impl Into<String>, value: T) -> Self
pub fn dependency_named<T>(self, key: impl Into<String>, value: T) -> Self
Inserts a named runtime dependency.
Sourcepub fn dependency_override<T>(self, value: T) -> Self
pub fn dependency_override<T>(self, value: T) -> Self
Inserts a typed dependency override.
Examples found in repository?
examples/di_override.rs (line 101)
43async fn main() -> Result<(), Box<dyn Error>> {
44 let read_dep_tool = ToolSpec::new("read_dep", "read injected value")
45 .with_schema(json!({
46 "type": "object",
47 "properties": {},
48 "required": [],
49 "additionalProperties": false
50 }))?
51 .with_handler(|_args, deps| {
52 let value = deps.get::<u32>().map(|v| *v).unwrap_or_default();
53 async move { Ok(ToolOutcome::Text(value.to_string())) }
54 });
55
56 let done_tool = ToolSpec::new("done", "finish")
57 .with_schema(json!({
58 "type": "object",
59 "properties": {
60 "message": {"type": "string"}
61 },
62 "required": ["message"],
63 "additionalProperties": false
64 }))?
65 .with_handler(|args, _deps| async move {
66 let message = args
67 .get("message")
68 .and_then(|v| v.as_str())
69 .unwrap_or("done");
70 Ok(ToolOutcome::Done(message.to_string()))
71 });
72
73 let model = ScriptedModel::new(vec![
74 Ok(ModelCompletion {
75 text: None,
76 thinking: None,
77 tool_calls: vec![ModelToolCall {
78 id: "call_1".to_string(),
79 name: "read_dep".to_string(),
80 arguments: json!({}),
81 }],
82 usage: None,
83 }),
84 Ok(ModelCompletion {
85 text: None,
86 thinking: None,
87 tool_calls: vec![ModelToolCall {
88 id: "call_2".to_string(),
89 name: "done".to_string(),
90 arguments: json!({"message": "dependency override applied"}),
91 }],
92 usage: None,
93 }),
94 ]);
95
96 let mut agent = Agent::builder()
97 .model(model)
98 .tool(read_dep_tool)
99 .tool(done_tool)
100 .dependency(1_u32)
101 .dependency_override(9_u32)
102 .build()?;
103
104 let response = agent.query("use dependency").await?;
105 println!("final: {response}");
106
107 Ok(())
108}Sourcepub fn dependency_override_named<T>(
self,
key: impl Into<String>,
value: T,
) -> Self
pub fn dependency_override_named<T>( self, key: impl Into<String>, value: T, ) -> Self
Inserts a named dependency override.
Sourcepub fn build(self) -> Result<Agent, AgentError>
pub fn build(self) -> Result<Agent, AgentError>
Builds an Agent and validates required config.
Examples found in repository?
More examples
examples/di_override.rs (line 102)
43async fn main() -> Result<(), Box<dyn Error>> {
44 let read_dep_tool = ToolSpec::new("read_dep", "read injected value")
45 .with_schema(json!({
46 "type": "object",
47 "properties": {},
48 "required": [],
49 "additionalProperties": false
50 }))?
51 .with_handler(|_args, deps| {
52 let value = deps.get::<u32>().map(|v| *v).unwrap_or_default();
53 async move { Ok(ToolOutcome::Text(value.to_string())) }
54 });
55
56 let done_tool = ToolSpec::new("done", "finish")
57 .with_schema(json!({
58 "type": "object",
59 "properties": {
60 "message": {"type": "string"}
61 },
62 "required": ["message"],
63 "additionalProperties": false
64 }))?
65 .with_handler(|args, _deps| async move {
66 let message = args
67 .get("message")
68 .and_then(|v| v.as_str())
69 .unwrap_or("done");
70 Ok(ToolOutcome::Done(message.to_string()))
71 });
72
73 let model = ScriptedModel::new(vec![
74 Ok(ModelCompletion {
75 text: None,
76 thinking: None,
77 tool_calls: vec![ModelToolCall {
78 id: "call_1".to_string(),
79 name: "read_dep".to_string(),
80 arguments: json!({}),
81 }],
82 usage: None,
83 }),
84 Ok(ModelCompletion {
85 text: None,
86 thinking: None,
87 tool_calls: vec![ModelToolCall {
88 id: "call_2".to_string(),
89 name: "done".to_string(),
90 arguments: json!({"message": "dependency override applied"}),
91 }],
92 usage: None,
93 }),
94 ]);
95
96 let mut agent = Agent::builder()
97 .model(model)
98 .tool(read_dep_tool)
99 .tool(done_tool)
100 .dependency(1_u32)
101 .dependency_override(9_u32)
102 .build()?;
103
104 let response = agent.query("use dependency").await?;
105 println!("final: {response}");
106
107 Ok(())
108}Trait Implementations§
Auto Trait Implementations§
impl Freeze for AgentBuilder
impl !RefUnwindSafe for AgentBuilder
impl Send for AgentBuilder
impl Sync for AgentBuilder
impl Unpin for AgentBuilder
impl UnsafeUnpin for AgentBuilder
impl !UnwindSafe for AgentBuilder
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more