mistralrs 0.8.1

Fast, flexible LLM inference.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
//! Agentic loop implementation for mistral.rs
//!
//! This module provides an `Agent` that runs an agentic loop with tool calling.
//! The agent takes a model, registers tools, and automatically handles the
//! tool calling loop until the model produces a final response.
//!
//! # Features
//!
//! - **Async tools**: Native support for async tool functions
//! - **Parallel execution**: Execute multiple tool calls concurrently
//! - **Streaming**: Stream assistant responses and tool execution events
//!
//! # Example
//!
//! ```ignore
//! use mistralrs::{tool, AgentBuilder, AgentEvent};
//!
//! // Async tool - runs natively async
//! #[tool(description = "Fetch a URL")]
//! async fn fetch_url(url: String) -> Result<String> {
//!     reqwest::get(&url).await?.text().await.map_err(Into::into)
//! }
//!
//! // Sync tool
//! #[tool(description = "Get weather")]
//! fn get_weather(city: String) -> Result<WeatherInfo> {
//!     Ok(WeatherInfo { temperature: 22.5 })
//! }
//!
//! #[tokio::main]
//! async fn main() -> anyhow::Result<()> {
//!     let model = TextModelBuilder::new("model-id").build().await?;
//!
//!     let agent = AgentBuilder::new(model)
//!         .with_system_prompt("You are helpful.")
//!         .register_tool(fetch_url_tool_with_callback())
//!         .register_tool(get_weather_tool_with_callback())
//!         .build();
//!
//!     // Streaming execution
//!     let mut stream = agent.run_stream("What's the weather?").await?;
//!     while let Some(event) = stream.next().await {
//!         match event {
//!             AgentEvent::TextDelta(text) => print!("{}", text),
//!             AgentEvent::Complete(response) => println!("\nDone!"),
//!             _ => {}
//!         }
//!     }
//!     Ok(())
//! }
//! ```

use std::collections::HashMap;
use std::future::Future;
use std::pin::Pin;
use std::sync::Arc;

use crate::{
    CalledFunction, ChatCompletionChunkResponse, ChatCompletionResponse, ChunkChoice, Delta, Model,
    RequestBuilder, Response, TextMessageRole, Tool, ToolCallResponse, ToolCallback, ToolChoice,
};

/// Async tool callback type for native async tool support
pub type AsyncToolCallback = dyn Fn(CalledFunction) -> Pin<Box<dyn Future<Output = anyhow::Result<String>> + Send>>
    + Send
    + Sync;

/// Unified tool callback that can be sync or async
#[derive(Clone)]
pub enum ToolCallbackType {
    /// Synchronous callback (runs in spawn_blocking for parallel execution)
    Sync(Arc<ToolCallback>),
    /// Asynchronous callback (runs natively async)
    Async(Arc<AsyncToolCallback>),
}

/// Configuration for the agentic loop
#[derive(Clone, Debug)]
pub struct AgentConfig {
    /// Maximum number of iterations before stopping (default: 10)
    pub max_iterations: usize,
    /// Tool choice strategy (default: Auto)
    pub tool_choice: ToolChoice,
    /// Optional system prompt for the agent
    pub system_prompt: Option<String>,
    /// Whether to execute multiple tool calls in parallel (default: true)
    pub parallel_tool_execution: bool,
}

impl Default for AgentConfig {
    fn default() -> Self {
        Self {
            max_iterations: 10,
            tool_choice: ToolChoice::Auto,
            system_prompt: None,
            parallel_tool_execution: true,
        }
    }
}

/// Represents a single step in the agent execution
#[derive(Debug, Clone)]
pub struct AgentStep {
    /// The model's response for this step
    pub response: ChatCompletionResponse,
    /// Tool calls made in this step (if any)
    pub tool_calls: Vec<ToolCallResponse>,
    /// Results from tool executions
    pub tool_results: Vec<ToolResult>,
}

/// Result of a tool execution
#[derive(Debug, Clone)]
pub struct ToolResult {
    /// The tool call ID this result corresponds to
    pub tool_call_id: String,
    /// Name of the tool that was called
    pub tool_name: String,
    /// The result: Ok(output) or Err(error_message)
    pub result: Result<String, String>,
}

/// Final response from the agent
#[derive(Debug, Clone)]
pub struct AgentResponse {
    /// All steps taken during execution
    pub steps: Vec<AgentStep>,
    /// Final text response (if any)
    pub final_response: Option<String>,
    /// Total number of iterations performed
    pub iterations: usize,
    /// Why the agent stopped
    pub stop_reason: AgentStopReason,
}

/// Reason why the agent stopped executing
#[derive(Debug, Clone, PartialEq)]
pub enum AgentStopReason {
    /// Model produced a text response with no tool calls
    TextResponse,
    /// Maximum iterations reached
    MaxIterations,
    /// No tool calls and no text response
    NoAction,
    /// Error during execution
    Error(String),
}

/// Events yielded during agent streaming
#[derive(Debug, Clone)]
pub enum AgentEvent {
    /// Text content delta from the model
    TextDelta(String),
    /// Model is calling tools (with the tool calls)
    ToolCallsStart(Vec<ToolCallResponse>),
    /// A single tool completed execution
    ToolResult(ToolResult),
    /// All tools completed, continuing to next iteration
    ToolCallsComplete,
    /// Agent finished with final response
    Complete(AgentResponse),
}

/// Internal state for the agent stream
enum AgentStreamState {
    /// Currently streaming model response
    Streaming {
        messages: RequestBuilder,
        iteration: usize,
        accumulated_content: String,
        accumulated_tool_calls: Vec<ToolCallResponse>,
        steps: Vec<AgentStep>,
    },
    /// Executing tool calls
    ExecutingTools {
        messages: RequestBuilder,
        iteration: usize,
        response: ChatCompletionResponse,
        tool_calls: Vec<ToolCallResponse>,
        tool_results: Vec<ToolResult>,
        pending_indices: Vec<usize>,
        steps: Vec<AgentStep>,
    },
    /// Agent has completed
    Done,
}

/// Stream of agent events during execution
pub struct AgentStream<'a> {
    agent: &'a Agent,
    state: AgentStreamState,
    model_stream: Option<crate::model::Stream<'a>>,
}

impl<'a> AgentStream<'a> {
    /// Get the next event from the agent stream
    pub async fn next(&mut self) -> Option<AgentEvent> {
        loop {
            match &mut self.state {
                AgentStreamState::Done => return None,

                AgentStreamState::Streaming {
                    messages,
                    iteration,
                    accumulated_content,
                    accumulated_tool_calls,
                    steps,
                } => {
                    // Get next chunk from model stream
                    if let Some(ref mut stream) = self.model_stream {
                        if let Some(response) = stream.next().await {
                            match response {
                                Response::Chunk(ChatCompletionChunkResponse {
                                    choices, ..
                                }) => {
                                    if let Some(ChunkChoice {
                                        delta:
                                            Delta {
                                                content,
                                                tool_calls,
                                                ..
                                            },
                                        finish_reason,
                                        ..
                                    }) = choices.first()
                                    {
                                        // Accumulate content
                                        if let Some(text) = content {
                                            accumulated_content.push_str(text);
                                            return Some(AgentEvent::TextDelta(text.clone()));
                                        }

                                        // Accumulate tool calls
                                        if let Some(calls) = tool_calls {
                                            accumulated_tool_calls.extend(calls.clone());
                                        }

                                        // Check if done
                                        if finish_reason.is_some() {
                                            self.model_stream = None;

                                            if accumulated_tool_calls.is_empty() {
                                                // No tool calls - we're done
                                                let final_response =
                                                    if accumulated_content.is_empty() {
                                                        None
                                                    } else {
                                                        Some(accumulated_content.clone())
                                                    };

                                                let stop_reason = if final_response.is_some() {
                                                    AgentStopReason::TextResponse
                                                } else {
                                                    AgentStopReason::NoAction
                                                };

                                                let response = AgentResponse {
                                                    steps: steps.clone(),
                                                    final_response,
                                                    iterations: *iteration + 1,
                                                    stop_reason,
                                                };

                                                self.state = AgentStreamState::Done;
                                                return Some(AgentEvent::Complete(response));
                                            } else {
                                                // Transition to executing tools
                                                let tool_calls = accumulated_tool_calls.clone();
                                                let event =
                                                    AgentEvent::ToolCallsStart(tool_calls.clone());

                                                // Create a placeholder response for the step
                                                let placeholder_response = ChatCompletionResponse {
                                                    id: String::new(),
                                                    choices: vec![],
                                                    created: 0,
                                                    model: String::new(),
                                                    system_fingerprint: String::new(),
                                                    object: String::new(),
                                                    usage: crate::Usage {
                                                        completion_tokens: 0,
                                                        prompt_tokens: 0,
                                                        total_tokens: 0,
                                                        avg_tok_per_sec: 0.0,
                                                        avg_prompt_tok_per_sec: 0.0,
                                                        avg_compl_tok_per_sec: 0.0,
                                                        total_time_sec: 0.0,
                                                        total_prompt_time_sec: 0.0,
                                                        total_completion_time_sec: 0.0,
                                                    },
                                                };

                                                self.state = AgentStreamState::ExecutingTools {
                                                    messages: messages.clone(),
                                                    iteration: *iteration,
                                                    response: placeholder_response,
                                                    tool_calls: tool_calls.clone(),
                                                    tool_results: Vec::new(),
                                                    pending_indices: (0..tool_calls.len())
                                                        .collect(),
                                                    steps: steps.clone(),
                                                };

                                                return Some(event);
                                            }
                                        }
                                    }
                                }
                                Response::Done(response) => {
                                    self.model_stream = None;
                                    let tool_calls = response
                                        .choices
                                        .first()
                                        .and_then(|c| c.message.tool_calls.clone())
                                        .unwrap_or_default();

                                    if tool_calls.is_empty() {
                                        let final_response = response
                                            .choices
                                            .first()
                                            .and_then(|c| c.message.content.clone());
                                        let stop_reason = if final_response.is_some() {
                                            AgentStopReason::TextResponse
                                        } else {
                                            AgentStopReason::NoAction
                                        };

                                        let agent_response = AgentResponse {
                                            steps: steps.clone(),
                                            final_response,
                                            iterations: *iteration + 1,
                                            stop_reason,
                                        };

                                        self.state = AgentStreamState::Done;
                                        return Some(AgentEvent::Complete(agent_response));
                                    } else {
                                        let event = AgentEvent::ToolCallsStart(tool_calls.clone());

                                        self.state = AgentStreamState::ExecutingTools {
                                            messages: messages.clone(),
                                            iteration: *iteration,
                                            response: response.clone(),
                                            tool_calls: tool_calls.clone(),
                                            tool_results: Vec::new(),
                                            pending_indices: (0..tool_calls.len()).collect(),
                                            steps: steps.clone(),
                                        };

                                        return Some(event);
                                    }
                                }
                                _ => continue,
                            }
                        }
                    }

                    // Stream ended unexpectedly
                    self.state = AgentStreamState::Done;
                    return None;
                }

                AgentStreamState::ExecutingTools {
                    messages,
                    iteration,
                    response,
                    tool_calls,
                    tool_results,
                    pending_indices,
                    steps,
                } => {
                    if pending_indices.is_empty() {
                        // All tools executed - prepare for next iteration
                        let mut new_messages = messages.clone();

                        // Add assistant message with tool calls
                        new_messages = new_messages.add_message_with_tool_call(
                            TextMessageRole::Assistant,
                            response
                                .choices
                                .first()
                                .and_then(|c| c.message.content.clone())
                                .unwrap_or_default(),
                            tool_calls.clone(),
                        );

                        // Add tool results
                        for result in tool_results.iter() {
                            let result_str = match &result.result {
                                Ok(s) => s.clone(),
                                Err(e) => format!("Error: {}", e),
                            };
                            new_messages =
                                new_messages.add_tool_message(&result_str, &result.tool_call_id);
                        }

                        // Record step
                        let step = AgentStep {
                            response: response.clone(),
                            tool_calls: tool_calls.clone(),
                            tool_results: tool_results.clone(),
                        };
                        let mut new_steps = steps.clone();
                        new_steps.push(step);

                        let new_iteration = *iteration + 1;

                        // Check max iterations
                        if new_iteration >= self.agent.config.max_iterations {
                            let agent_response = AgentResponse {
                                steps: new_steps,
                                final_response: None,
                                iterations: new_iteration,
                                stop_reason: AgentStopReason::MaxIterations,
                            };
                            self.state = AgentStreamState::Done;
                            return Some(AgentEvent::Complete(agent_response));
                        }

                        // Start new model request
                        let request = new_messages
                            .clone()
                            .set_tools(self.agent.tools.clone())
                            .set_tool_choice(self.agent.config.tool_choice.clone());

                        match self.agent.model.stream_chat_request(request).await {
                            Ok(stream) => {
                                self.model_stream = Some(stream);
                                self.state = AgentStreamState::Streaming {
                                    messages: new_messages,
                                    iteration: new_iteration,
                                    accumulated_content: String::new(),
                                    accumulated_tool_calls: Vec::new(),
                                    steps: new_steps,
                                };
                                return Some(AgentEvent::ToolCallsComplete);
                            }
                            Err(e) => {
                                let agent_response = AgentResponse {
                                    steps: new_steps,
                                    final_response: None,
                                    iterations: new_iteration,
                                    stop_reason: AgentStopReason::Error(e.to_string()),
                                };
                                self.state = AgentStreamState::Done;
                                return Some(AgentEvent::Complete(agent_response));
                            }
                        }
                    }

                    // Execute next pending tool
                    let idx = pending_indices.remove(0);
                    let tool_call = &tool_calls[idx];
                    let result = self.agent.execute_tool_async(tool_call).await;
                    let event = AgentEvent::ToolResult(result.clone());
                    tool_results.push(result);
                    return Some(event);
                }
            }
        }
    }
}

/// An agent that runs an agentic loop with tool calling
pub struct Agent {
    model: Model,
    tools: Vec<Tool>,
    callbacks: HashMap<String, ToolCallbackType>,
    config: AgentConfig,
}

impl Agent {
    /// Create a new agent with the given model and configuration
    pub fn new(model: Model, config: AgentConfig) -> Self {
        Self {
            model,
            tools: Vec::new(),
            callbacks: HashMap::new(),
            config,
        }
    }

    /// Add a tool with its callback
    pub fn with_tool(mut self, tool: Tool, callback: ToolCallbackType) -> Self {
        let name = tool.function.name.clone();
        self.tools.push(tool);
        self.callbacks.insert(name, callback);
        self
    }

    /// Run the agentic loop with the given user message
    ///
    /// This method will:
    /// 1. Send the user message to the model
    /// 2. If the model returns tool calls, execute them and send results back
    /// 3. Repeat until the model returns a text response or max iterations is reached
    pub async fn run(&self, user_message: impl ToString) -> anyhow::Result<AgentResponse> {
        let mut steps = Vec::new();
        let mut messages = RequestBuilder::new();

        // Add system prompt if configured
        if let Some(ref system) = self.config.system_prompt {
            messages = messages.add_message(TextMessageRole::System, system);
        }

        // Add initial user message
        messages = messages.add_message(TextMessageRole::User, user_message.to_string());

        for iteration in 0..self.config.max_iterations {
            // Configure tools for this request
            let request = messages
                .clone()
                .set_tools(self.tools.clone())
                .set_tool_choice(self.config.tool_choice.clone());

            // Send request to model
            let response = self.model.send_chat_request(request).await?;

            let choice = response
                .choices
                .first()
                .ok_or_else(|| anyhow::anyhow!("No choices in response"))?;

            // Check for tool calls
            let tool_calls = choice.message.tool_calls.clone().unwrap_or_default();

            if tool_calls.is_empty() {
                // No tool calls - we're done
                let final_text = choice.message.content.clone();
                steps.push(AgentStep {
                    response: response.clone(),
                    tool_calls: vec![],
                    tool_results: vec![],
                });

                let stop_reason = if final_text.is_some() {
                    AgentStopReason::TextResponse
                } else {
                    AgentStopReason::NoAction
                };

                return Ok(AgentResponse {
                    steps,
                    final_response: final_text,
                    iterations: iteration + 1,
                    stop_reason,
                });
            }

            // Execute tool calls (parallel or sequential based on config)
            let tool_results = if self.config.parallel_tool_execution {
                self.execute_tools_parallel(&tool_calls).await
            } else {
                let mut results = Vec::new();
                for tool_call in &tool_calls {
                    results.push(self.execute_tool_async(tool_call).await);
                }
                results
            };

            // Add assistant message with tool calls
            messages = messages.add_message_with_tool_call(
                TextMessageRole::Assistant,
                choice.message.content.clone().unwrap_or_default(),
                tool_calls.clone(),
            );

            // Add tool results to messages
            for result in &tool_results {
                let result_str = match &result.result {
                    Ok(s) => s.clone(),
                    Err(e) => format!("Error: {}", e),
                };
                messages = messages.add_tool_message(&result_str, &result.tool_call_id);
            }

            steps.push(AgentStep {
                response: response.clone(),
                tool_calls: tool_calls.clone(),
                tool_results,
            });
        }

        // Max iterations reached
        Ok(AgentResponse {
            steps,
            final_response: None,
            iterations: self.config.max_iterations,
            stop_reason: AgentStopReason::MaxIterations,
        })
    }

    /// Run the agent with streaming output
    ///
    /// Returns a stream of `AgentEvent` that can be used to observe
    /// the agent's progress in real-time.
    pub async fn run_stream(&self, user_message: impl ToString) -> anyhow::Result<AgentStream<'_>> {
        let mut messages = RequestBuilder::new();

        // Add system prompt if configured
        if let Some(ref system) = self.config.system_prompt {
            messages = messages.add_message(TextMessageRole::System, system);
        }

        // Add initial user message
        messages = messages.add_message(TextMessageRole::User, user_message.to_string());

        // Configure tools for this request
        let request = messages
            .clone()
            .set_tools(self.tools.clone())
            .set_tool_choice(self.config.tool_choice.clone());

        // Start streaming
        let stream = self.model.stream_chat_request(request).await?;

        Ok(AgentStream {
            agent: self,
            state: AgentStreamState::Streaming {
                messages,
                iteration: 0,
                accumulated_content: String::new(),
                accumulated_tool_calls: Vec::new(),
                steps: Vec::new(),
            },
            model_stream: Some(stream),
        })
    }

    /// Execute multiple tool calls in parallel
    async fn execute_tools_parallel(&self, tool_calls: &[ToolCallResponse]) -> Vec<ToolResult> {
        let futures: Vec<_> = tool_calls
            .iter()
            .map(|tc| self.execute_tool_async(tc))
            .collect();

        futures::future::join_all(futures).await
    }

    /// Execute a single tool call (async-compatible)
    async fn execute_tool_async(&self, tool_call: &ToolCallResponse) -> ToolResult {
        let tool_name = &tool_call.function.name;

        let result = match self.callbacks.get(tool_name) {
            Some(ToolCallbackType::Sync(callback)) => {
                // Run sync callback in spawn_blocking to not block async runtime
                let callback = Arc::clone(callback);
                let function = tool_call.function.clone();
                tokio::task::spawn_blocking(move || callback(&function))
                    .await
                    .map_err(|e| anyhow::anyhow!("Task join error: {}", e))
                    .and_then(|r| r)
                    .map_err(|e| e.to_string())
            }
            Some(ToolCallbackType::Async(callback)) => {
                let function = tool_call.function.clone();
                callback(function).await.map_err(|e| e.to_string())
            }
            None => Err(format!("Unknown tool: {}", tool_name)),
        };

        ToolResult {
            tool_call_id: tool_call.id.clone(),
            tool_name: tool_name.clone(),
            result,
        }
    }

    /// Get a reference to the underlying model
    pub fn model(&self) -> &Model {
        &self.model
    }

    /// Get a reference to the registered tools
    pub fn tools(&self) -> &[Tool] {
        &self.tools
    }

    /// Get a reference to the agent configuration
    pub fn config(&self) -> &AgentConfig {
        &self.config
    }
}

/// Builder for creating agents with a fluent API
pub struct AgentBuilder {
    model: Model,
    tools: Vec<Tool>,
    callbacks: HashMap<String, ToolCallbackType>,
    config: AgentConfig,
}

impl AgentBuilder {
    /// Create a new agent builder with the given model
    pub fn new(model: Model) -> Self {
        Self {
            model,
            tools: Vec::new(),
            callbacks: HashMap::new(),
            config: AgentConfig::default(),
        }
    }

    /// Set the maximum number of iterations
    pub fn with_max_iterations(mut self, max: usize) -> Self {
        self.config.max_iterations = max;
        self
    }

    /// Set the system prompt for the agent
    pub fn with_system_prompt(mut self, prompt: impl ToString) -> Self {
        self.config.system_prompt = Some(prompt.to_string());
        self
    }

    /// Set the tool choice strategy
    pub fn with_tool_choice(mut self, choice: ToolChoice) -> Self {
        self.config.tool_choice = choice;
        self
    }

    /// Enable or disable parallel tool execution (default: true)
    pub fn with_parallel_tool_execution(mut self, enabled: bool) -> Self {
        self.config.parallel_tool_execution = enabled;
        self
    }

    /// Add a sync tool with its callback
    pub fn with_sync_tool(mut self, tool: Tool, callback: Arc<ToolCallback>) -> Self {
        let name = tool.function.name.clone();
        self.tools.push(tool);
        self.callbacks
            .insert(name, ToolCallbackType::Sync(callback));
        self
    }

    /// Add an async tool with its callback
    pub fn with_async_tool(mut self, tool: Tool, callback: Arc<AsyncToolCallback>) -> Self {
        let name = tool.function.name.clone();
        self.tools.push(tool);
        self.callbacks
            .insert(name, ToolCallbackType::Async(callback));
        self
    }

    /// Register a tool using a tuple of (Tool, ToolCallbackType)
    ///
    /// This is designed to work with the `_tool_with_callback()` functions
    /// generated by the `#[tool]` macro.
    pub fn register_tool(mut self, (tool, callback): (Tool, ToolCallbackType)) -> Self {
        let name = tool.function.name.clone();
        self.tools.push(tool);
        self.callbacks.insert(name, callback);
        self
    }

    /// Build the agent
    pub fn build(self) -> Agent {
        Agent {
            model: self.model,
            tools: self.tools,
            callbacks: self.callbacks,
            config: self.config,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_agent_config_default() {
        let config = AgentConfig::default();
        assert_eq!(config.max_iterations, 10);
        assert!(config.system_prompt.is_none());
        assert!(config.parallel_tool_execution);
    }

    #[test]
    fn test_agent_stop_reason_equality() {
        assert_eq!(AgentStopReason::TextResponse, AgentStopReason::TextResponse);
        assert_eq!(
            AgentStopReason::MaxIterations,
            AgentStopReason::MaxIterations
        );
        assert_ne!(
            AgentStopReason::TextResponse,
            AgentStopReason::MaxIterations
        );
    }

    #[test]
    fn test_tool_callback_type_clone() {
        // Ensure ToolCallbackType can be cloned
        let sync_cb: Arc<ToolCallback> = Arc::new(|_| Ok("test".to_string()));
        let cb_type = ToolCallbackType::Sync(sync_cb);
        let _ = cb_type.clone();
    }
}