mixtape-core 0.4.0

An agentic AI framework for Rust
Documentation
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
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
//! Tool management and execution for Agent

use std::time::Instant;

use futures::stream::{self, StreamExt};
use serde_json::Value;

use crate::events::AgentEvent;
use crate::permission::{Authorization, AuthorizationResponse};
use crate::tool::{box_tool, ToolResult};
use crate::types::{Message, ToolResultBlock, ToolResultStatus, ToolUseBlock};

use super::types::{AgentError, ToolCallInfo, ToolInfo};
use super::Agent;

#[cfg(feature = "session")]
use crate::session::ToolCall;

impl Agent {
    /// Add a tool to the agent's toolbox
    pub fn add_tool<T: crate::tool::Tool + 'static>(&mut self, tool: T)
    where
        T::Input: serde::Serialize,
    {
        let tool_name = tool.name().to_string();

        // Check for duplicate tool names
        if self.tools.iter().any(|t| t.name() == tool_name) {
            eprintln!(
                "Warning: Tool '{}' is already registered. This will cause errors when calling the model.",
                tool_name
            );
            eprintln!("   Consider using .with_namespace() on MCP servers to avoid conflicts.");
        }

        self.tools.push(box_tool(tool));
    }

    /// List all configured tools
    pub fn list_tools(&self) -> Vec<ToolInfo> {
        self.tools
            .iter()
            .map(|t| ToolInfo {
                name: t.name().to_string(),
                description: t.description().to_string(),
            })
            .collect()
    }

    /// Format tool input parameters for presentation
    ///
    /// Returns formatted string if the tool has a custom presenter,
    /// None otherwise (caller should fall back to JSON).
    pub fn format_tool_input(
        &self,
        tool_name: &str,
        params: &Value,
        context: crate::presentation::Display,
    ) -> Option<String> {
        let tool = self.tools.iter().find(|t| t.name() == tool_name)?;

        Some(match context {
            crate::presentation::Display::Cli => tool.format_input_ansi(params),
        })
    }

    /// Format tool output for presentation
    ///
    /// Returns formatted string for the tool output.
    pub fn format_tool_output(
        &self,
        tool_name: &str,
        result: &crate::tool::ToolResult,
        context: crate::presentation::Display,
    ) -> Option<String> {
        let tool = self.tools.iter().find(|t| t.name() == tool_name)?;

        Some(match context {
            crate::presentation::Display::Cli => tool.format_output_ansi(result),
        })
    }

    /// Execute a tool with approval checking
    pub(super) async fn execute_tool(
        &self,
        tool_use: &ToolUseBlock,
    ) -> Result<ToolResult, AgentError> {
        let tool_start = Instant::now();
        let tool_id = tool_use.id.clone();
        let tool_name = tool_use.name.clone();
        let input = tool_use.input.clone();

        // Emit ToolRequested (always fires exactly once)
        self.emit_event(AgentEvent::ToolRequested {
            tool_use_id: tool_id.clone(),
            name: tool_name.clone(),
            input: input.clone(),
        });

        // Validate that input is a JSON object (per Anthropic/Bedrock spec)
        if !input.is_object() {
            let type_name = match &input {
                Value::Null => "null",
                Value::Bool(_) => "boolean",
                Value::Number(_) => "number",
                Value::String(_) => "string",
                Value::Array(_) => "array",
                Value::Object(_) => "object", // Won't reach here
            };
            let error_msg = format!("Tool input must be a JSON object, got: {}", type_name);
            self.emit_event(AgentEvent::ToolFailed {
                tool_use_id: tool_id,
                name: tool_name,
                error: error_msg.clone(),
                duration: tool_start.elapsed(),
            });
            return Err(AgentError::InvalidToolInput(error_msg));
        }

        let tool = self
            .tools
            .iter()
            .find(|t| t.name() == tool_use.name)
            .ok_or_else(|| {
                self.emit_event(AgentEvent::ToolFailed {
                    tool_use_id: tool_id.clone(),
                    name: tool_name.clone(),
                    error: format!("Tool not found: {}", tool_name),
                    duration: tool_start.elapsed(),
                });
                AgentError::ToolNotFound(tool_name.clone())
            })?;

        // Check approval (emits permission events as needed)
        self.check_tool_approval(&tool_id, &tool_name, &input, tool_start)
            .await?;

        // Emit ToolExecuting (after permission granted)
        self.emit_event(AgentEvent::ToolExecuting {
            tool_use_id: tool_id.clone(),
            name: tool_name.clone(),
        });

        // Execute the tool
        match tool.execute_raw(input).await {
            Ok(result) => {
                self.emit_event(AgentEvent::ToolCompleted {
                    tool_use_id: tool_id,
                    name: tool_name,
                    output: result.clone(),
                    duration: tool_start.elapsed(),
                });
                Ok(result)
            }
            Err(e) => {
                let error_msg = e.to_string();
                self.emit_event(AgentEvent::ToolFailed {
                    tool_use_id: tool_id,
                    name: tool_name,
                    error: error_msg,
                    duration: tool_start.elapsed(),
                });
                Err(AgentError::Tool(e))
            }
        }
    }

    /// Check if a tool is authorized for execution
    async fn check_tool_approval(
        &self,
        tool_id: &str,
        tool_name: &str,
        input: &Value,
        tool_start: Instant,
    ) -> Result<(), AgentError> {
        let authorizer = self.authorizer.read().await;

        match authorizer.check(tool_name, input).await {
            Authorization::Granted { grant } => {
                self.emit_event(AgentEvent::PermissionGranted {
                    tool_use_id: tool_id.to_string(),
                    tool_name: tool_name.to_string(),
                    scope: Some(grant.scope),
                });
                Ok(())
            }
            Authorization::Denied { reason } => {
                self.emit_event(AgentEvent::PermissionDenied {
                    tool_use_id: tool_id.to_string(),
                    tool_name: tool_name.to_string(),
                    reason: reason.clone(),
                });
                self.emit_event(AgentEvent::ToolFailed {
                    tool_use_id: tool_id.to_string(),
                    name: tool_name.to_string(),
                    error: reason,
                    duration: tool_start.elapsed(),
                });
                Err(AgentError::ToolDenied(tool_name.to_string()))
            }
            Authorization::PendingApproval { params_hash } => {
                // Need to drop the lock before requesting authorization
                drop(authorizer);

                self.request_authorization(tool_id, tool_name, input, params_hash, tool_start)
                    .await
            }
        }
    }

    /// Request authorization for a tool
    async fn request_authorization(
        &self,
        tool_id: &str,
        tool_name: &str,
        input: &Value,
        params_hash: String,
        tool_start: Instant,
    ) -> Result<(), AgentError> {
        let (tx, mut rx) = tokio::sync::mpsc::channel::<AuthorizationResponse>(1);

        // Use tool_id as proposal_id for consistency
        let proposal_id = tool_id.to_string();

        // Register pending authorization
        {
            let mut pending = self.pending_authorizations.write().await;
            pending.insert(proposal_id.clone(), tx);
        }

        // Emit permission required event
        self.emit_event(AgentEvent::PermissionRequired {
            proposal_id: proposal_id.clone(),
            tool_name: tool_name.to_string(),
            params: input.clone(),
            params_hash: params_hash.clone(),
        });

        // Wait for response with timeout
        let response = match tokio::time::timeout(self.authorization_timeout, rx.recv()).await {
            Ok(Some(response)) => response,
            Ok(None) => AuthorizationResponse::Deny {
                reason: Some("Channel closed".to_string()),
            },
            Err(_) => {
                self.emit_event(AgentEvent::PermissionDenied {
                    tool_use_id: tool_id.to_string(),
                    tool_name: tool_name.to_string(),
                    reason: "Authorization request timed out".to_string(),
                });
                AuthorizationResponse::Deny {
                    reason: Some("Timeout".to_string()),
                }
            }
        };

        // Clean up pending authorization
        {
            let mut pending = self.pending_authorizations.write().await;
            pending.remove(&proposal_id);
        }

        match response {
            AuthorizationResponse::Once => {
                self.emit_event(AgentEvent::PermissionGranted {
                    tool_use_id: tool_id.to_string(),
                    tool_name: tool_name.to_string(),
                    scope: None,
                });
                Ok(())
            }
            AuthorizationResponse::Trust { grant } => {
                // Save the grant to the authorizer
                let authorizer = self.authorizer.read().await;
                let result = if grant.is_tool_wide() {
                    authorizer.grant_tool(&grant.tool).await
                } else if let Some(ref hash) = grant.params_hash {
                    authorizer.grant_params_hash(&grant.tool, hash).await
                } else {
                    authorizer.grant_tool(&grant.tool).await
                };
                if let Err(e) = result {
                    eprintln!("Warning: Failed to save grant: {}", e);
                }
                self.emit_event(AgentEvent::PermissionGranted {
                    tool_use_id: tool_id.to_string(),
                    tool_name: tool_name.to_string(),
                    scope: Some(grant.scope),
                });
                Ok(())
            }
            AuthorizationResponse::Deny { reason } => {
                let reason_str =
                    reason.unwrap_or_else(|| "Authorization denied by user".to_string());
                self.emit_event(AgentEvent::PermissionDenied {
                    tool_use_id: tool_id.to_string(),
                    tool_name: tool_name.to_string(),
                    reason: reason_str,
                });
                self.emit_event(AgentEvent::ToolFailed {
                    tool_use_id: tool_id.to_string(),
                    name: tool_name.to_string(),
                    error: "Tool execution denied by user".to_string(),
                    duration: tool_start.elapsed(),
                });
                Err(AgentError::ToolDenied(tool_name.to_string()))
            }
        }
    }

    /// Process tool calls from a model response
    ///
    /// Executes all tool calls in parallel (up to max_concurrent_tools),
    /// collecting results and recording statistics.
    pub(super) async fn process_tool_calls(
        &self,
        message: &Message,
        tool_call_infos: &mut Vec<ToolCallInfo>,
        #[cfg(feature = "session")] session_tool_calls: &mut Vec<ToolCall>,
        #[cfg(feature = "session")] session_tool_results: &mut Vec<crate::session::ToolResult>,
    ) -> Vec<ToolResultBlock> {
        let tool_uses = message.tool_uses();
        let tool_use_blocks: Vec<_> = tool_uses.into_iter().cloned().collect();

        // Execute tools in parallel with concurrency limit
        let futures: Vec<_> = tool_use_blocks
            .iter()
            .map(|tool_use| {
                let tool_use = tool_use.clone();
                async move {
                    let start = Instant::now();
                    let result = self.execute_tool(&tool_use).await;
                    let duration = start.elapsed();
                    (tool_use, result, duration)
                }
            })
            .collect();

        let results: Vec<_> = stream::iter(futures)
            .buffer_unordered(self.max_concurrent_tools)
            .collect()
            .await;

        results
            .into_iter()
            .map(|(tool_use, result, duration)| {
                // Record tool call for session
                #[cfg(feature = "session")]
                {
                    session_tool_calls.push(ToolCall {
                        id: tool_use.id.clone(),
                        name: tool_use.name.clone(),
                        input: tool_use.input.to_string(),
                    });
                }

                match result {
                    Ok(ref tool_result) => {
                        // Record tool call info for response
                        tool_call_infos.push(ToolCallInfo {
                            name: tool_use.name.clone(),
                            input: tool_use.input.clone(),
                            output: tool_result.as_text(),
                            success: true,
                            duration,
                        });

                        // Record tool result for session
                        #[cfg(feature = "session")]
                        {
                            session_tool_results.push(crate::session::ToolResult {
                                tool_use_id: tool_use.id.clone(),
                                success: true,
                                content: tool_result.as_text(),
                            });
                        }

                        ToolResultBlock {
                            tool_use_id: tool_use.id,
                            content: tool_result.clone(),
                            status: ToolResultStatus::Success,
                        }
                    }
                    Err(ref e) => {
                        let error_msg = format!("Error: {}", e);

                        // Record tool call info for response
                        tool_call_infos.push(ToolCallInfo {
                            name: tool_use.name.clone(),
                            input: tool_use.input.clone(),
                            output: error_msg.clone(),
                            success: false,
                            duration,
                        });

                        // Record tool error for session
                        #[cfg(feature = "session")]
                        {
                            session_tool_results.push(crate::session::ToolResult {
                                tool_use_id: tool_use.id.clone(),
                                success: false,
                                content: error_msg.clone(),
                            });
                        }

                        ToolResultBlock {
                            tool_use_id: tool_use.id,
                            content: ToolResult::Text(error_msg),
                            status: ToolResultStatus::Error,
                        }
                    }
                }
            })
            .collect()
    }
}

// ============================================================================
// Tests
// ============================================================================

#[cfg(test)]
mod tests {
    use super::*;
    use crate::provider::{ModelProvider, ProviderError};
    use crate::tool::{Tool, ToolError, ToolResult as MxToolResult};
    use crate::types::{ContentBlock, Message, Role, StopReason, ToolDefinition, ToolUseBlock};
    use crate::{Agent, ModelResponse};
    use schemars::JsonSchema;
    use serde::{Deserialize, Serialize};
    use std::sync::Arc;

    /// Mock provider for testing
    #[derive(Clone)]
    struct MockProvider {
        responses: Arc<parking_lot::Mutex<Vec<ModelResponse>>>,
    }

    impl MockProvider {
        fn new() -> Self {
            Self {
                responses: Arc::new(parking_lot::Mutex::new(Vec::new())),
            }
        }

        fn with_text(self, text: impl Into<String>) -> Self {
            let message = Message {
                role: Role::Assistant,
                content: vec![ContentBlock::Text(text.into())],
            };
            let response = ModelResponse {
                message,
                stop_reason: StopReason::EndTurn,
                usage: None,
            };
            self.responses.lock().push(response);
            self
        }
    }

    #[async_trait::async_trait]
    impl ModelProvider for MockProvider {
        fn name(&self) -> &str {
            "MockProvider"
        }

        fn max_context_tokens(&self) -> usize {
            200_000
        }

        fn max_output_tokens(&self) -> usize {
            8_192
        }

        async fn generate(
            &self,
            _messages: Vec<Message>,
            _tools: Vec<ToolDefinition>,
            _system_prompt: Option<String>,
        ) -> Result<ModelResponse, ProviderError> {
            let mut responses = self.responses.lock();
            if responses.is_empty() {
                return Err(ProviderError::Other("No more responses".to_string()));
            }
            Ok(responses.remove(0))
        }
    }

    /// Input for the Echo test tool
    #[derive(Debug, Deserialize, Serialize, JsonSchema)]
    struct EchoInput {
        message: String,
    }

    /// Simple test tool that echoes input
    struct EchoTool;

    impl Tool for EchoTool {
        type Input = EchoInput;

        fn name(&self) -> &str {
            "echo"
        }

        fn description(&self) -> &str {
            "Echoes the input back"
        }

        async fn execute(&self, input: Self::Input) -> Result<MxToolResult, ToolError> {
            Ok(MxToolResult::text(input.message))
        }
    }

    /// Input for the Add test tool
    #[derive(Debug, Deserialize, Serialize, JsonSchema)]
    struct AddInput {
        a: f64,
        b: f64,
    }

    /// Simple test tool that adds two numbers
    struct AddTool;

    impl Tool for AddTool {
        type Input = AddInput;

        fn name(&self) -> &str {
            "add"
        }

        fn description(&self) -> &str {
            "Adds two numbers"
        }

        async fn execute(&self, input: Self::Input) -> Result<MxToolResult, ToolError> {
            Ok(MxToolResult::text(format!("{}", input.a + input.b)))
        }
    }

    /// Input for the FailingTool test tool
    #[derive(Debug, Deserialize, Serialize, JsonSchema)]
    struct EmptyInput {}

    /// Tool that always fails
    struct FailingTool;

    impl Tool for FailingTool {
        type Input = EmptyInput;

        fn name(&self) -> &str {
            "failing_tool"
        }

        fn description(&self) -> &str {
            "A tool that always fails"
        }

        async fn execute(&self, _input: Self::Input) -> Result<MxToolResult, ToolError> {
            Err(ToolError::Custom("Tool execution failed".to_string()))
        }
    }

    // ===== add_tool Tests =====

    #[tokio::test]
    async fn test_add_tool() {
        let provider = MockProvider::new().with_text("ok");
        let mut agent = Agent::builder().provider(provider).build().await.unwrap();

        // Initially no tools
        assert_eq!(agent.list_tools().len(), 0);

        // Add a tool
        agent.add_tool(EchoTool);

        // Should have one tool
        let tools = agent.list_tools();
        assert_eq!(tools.len(), 1);
        assert_eq!(tools[0].name, "echo");
        assert_eq!(tools[0].description, "Echoes the input back");
    }

    #[tokio::test]
    async fn test_add_multiple_tools() {
        let provider = MockProvider::new().with_text("ok");
        let mut agent = Agent::builder().provider(provider).build().await.unwrap();

        agent.add_tool(EchoTool);
        agent.add_tool(AddTool);

        let tools = agent.list_tools();
        assert_eq!(tools.len(), 2);

        let names: Vec<&str> = tools.iter().map(|t| t.name.as_str()).collect();
        assert!(names.contains(&"echo"));
        assert!(names.contains(&"add"));
    }

    #[tokio::test]
    async fn test_add_tool_with_builder() {
        let provider = MockProvider::new().with_text("ok");
        let agent = Agent::builder()
            .provider(provider)
            .add_tool(EchoTool)
            .add_tool(AddTool)
            .build()
            .await
            .unwrap();

        let tools = agent.list_tools();
        assert_eq!(tools.len(), 2);
    }

    // ===== list_tools Tests =====

    #[tokio::test]
    async fn test_list_tools_empty() {
        let provider = MockProvider::new().with_text("ok");
        let agent = Agent::builder().provider(provider).build().await.unwrap();

        let tools = agent.list_tools();
        assert!(tools.is_empty());
    }

    #[tokio::test]
    async fn test_list_tools_preserves_order() {
        let provider = MockProvider::new().with_text("ok");
        let mut agent = Agent::builder().provider(provider).build().await.unwrap();

        agent.add_tool(EchoTool);
        agent.add_tool(AddTool);
        agent.add_tool(FailingTool);

        let tools = agent.list_tools();
        assert_eq!(tools[0].name, "echo");
        assert_eq!(tools[1].name, "add");
        assert_eq!(tools[2].name, "failing_tool");
    }

    // ===== execute_tool Tests =====

    #[tokio::test]
    async fn test_execute_tool_success() {
        let provider = MockProvider::new().with_text("ok");
        let mut agent = Agent::builder().provider(provider).build().await.unwrap();

        agent.add_tool(EchoTool);

        // Grant permission to the echo tool
        agent
            .authorizer()
            .write()
            .await
            .grant_tool("echo")
            .await
            .unwrap();

        let tool_use = ToolUseBlock {
            id: "tool_123".to_string(),
            name: "echo".to_string(),
            input: serde_json::json!({"message": "Hello, world!"}),
        };

        let result = agent.execute_tool(&tool_use).await;
        assert!(result.is_ok());
        assert_eq!(result.unwrap().as_text(), "Hello, world!");
    }

    #[tokio::test]
    async fn test_execute_tool_not_found() {
        let provider = MockProvider::new().with_text("ok");
        let agent = Agent::builder().provider(provider).build().await.unwrap();

        let tool_use = ToolUseBlock {
            id: "tool_123".to_string(),
            name: "nonexistent_tool".to_string(),
            input: serde_json::json!({}),
        };

        let result = agent.execute_tool(&tool_use).await;
        assert!(result.is_err());
        assert!(matches!(result.unwrap_err(), AgentError::ToolNotFound(_)));
    }

    #[tokio::test]
    async fn test_execute_tool_invalid_input_not_object() {
        let provider = MockProvider::new().with_text("ok");
        let mut agent = Agent::builder().provider(provider).build().await.unwrap();

        agent.add_tool(EchoTool);

        // Test with string input (not an object)
        let tool_use = ToolUseBlock {
            id: "tool_123".to_string(),
            name: "echo".to_string(),
            input: serde_json::json!("not an object"),
        };

        let result = agent.execute_tool(&tool_use).await;
        assert!(result.is_err());
        assert!(matches!(
            result.unwrap_err(),
            AgentError::InvalidToolInput(_)
        ));
    }

    #[tokio::test]
    async fn test_execute_tool_invalid_input_array() {
        let provider = MockProvider::new().with_text("ok");
        let mut agent = Agent::builder().provider(provider).build().await.unwrap();

        agent.add_tool(EchoTool);

        let tool_use = ToolUseBlock {
            id: "tool_123".to_string(),
            name: "echo".to_string(),
            input: serde_json::json!([1, 2, 3]),
        };

        let result = agent.execute_tool(&tool_use).await;
        assert!(result.is_err());
        let err = result.unwrap_err();
        if let AgentError::InvalidToolInput(msg) = &err {
            assert!(msg.contains("array"));
        }
    }

    #[tokio::test]
    async fn test_execute_tool_invalid_input_null() {
        let provider = MockProvider::new().with_text("ok");
        let mut agent = Agent::builder().provider(provider).build().await.unwrap();

        agent.add_tool(EchoTool);

        let tool_use = ToolUseBlock {
            id: "tool_123".to_string(),
            name: "echo".to_string(),
            input: serde_json::Value::Null,
        };

        let result = agent.execute_tool(&tool_use).await;
        assert!(result.is_err());
        let err = result.unwrap_err();
        if let AgentError::InvalidToolInput(msg) = &err {
            assert!(msg.contains("null"));
        }
    }

    #[tokio::test]
    async fn test_execute_tool_execution_failure() {
        let provider = MockProvider::new().with_text("ok");
        let mut agent = Agent::builder().provider(provider).build().await.unwrap();

        agent.add_tool(FailingTool);

        // Grant permission to the failing tool
        agent
            .authorizer()
            .write()
            .await
            .grant_tool("failing_tool")
            .await
            .unwrap();

        let tool_use = ToolUseBlock {
            id: "tool_123".to_string(),
            name: "failing_tool".to_string(),
            input: serde_json::json!({}),
        };

        let result = agent.execute_tool(&tool_use).await;
        assert!(result.is_err());
        assert!(matches!(result.unwrap_err(), AgentError::Tool(_)));
    }

    // ===== format_tool_input/output Tests =====

    #[tokio::test]
    async fn test_format_tool_input_existing_tool() {
        let provider = MockProvider::new().with_text("ok");
        let mut agent = Agent::builder().provider(provider).build().await.unwrap();

        agent.add_tool(EchoTool);

        let params = serde_json::json!({"message": "test"});
        let formatted = agent.format_tool_input("echo", &params, crate::presentation::Display::Cli);

        // Should return some formatted output
        assert!(formatted.is_some());
    }

    #[tokio::test]
    async fn test_format_tool_input_nonexistent_tool() {
        let provider = MockProvider::new().with_text("ok");
        let agent = Agent::builder().provider(provider).build().await.unwrap();

        let params = serde_json::json!({"message": "test"});
        let formatted =
            agent.format_tool_input("nonexistent", &params, crate::presentation::Display::Cli);

        assert!(formatted.is_none());
    }

    #[tokio::test]
    async fn test_format_tool_output_existing_tool() {
        let provider = MockProvider::new().with_text("ok");
        let mut agent = Agent::builder().provider(provider).build().await.unwrap();

        agent.add_tool(EchoTool);

        let result = crate::tool::ToolResult::text("output");
        let formatted =
            agent.format_tool_output("echo", &result, crate::presentation::Display::Cli);

        assert!(formatted.is_some());
    }

    #[tokio::test]
    async fn test_format_tool_output_nonexistent_tool() {
        let provider = MockProvider::new().with_text("ok");
        let agent = Agent::builder().provider(provider).build().await.unwrap();

        let result = crate::tool::ToolResult::text("output");
        let formatted =
            agent.format_tool_output("nonexistent", &result, crate::presentation::Display::Cli);

        assert!(formatted.is_none());
    }
}