bamboo-agent-core 2026.4.14

Core agent abstractions and execution primitives for the Bamboo agent framework
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
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
//! Agentic tool execution framework.
//!
//! This module provides a framework for implementing autonomous agent tools
//! that can iterate and interact with the environment through tool calls.
//!
//! # Overview
//!
//! The agentic system allows tools to:
//! - Execute multiple iterations toward a goal
//! - Maintain state across iterations
//! - Record interactions between user, assistant, and tools
//! - Perform autonomous code review and refinement
//!
//! # Key Types
//!
//! - [`ToolGoal`] - Defines the objective and parameters for an agentic tool
//! - [`AgenticContext`] - Execution context with state and interaction history
//! - [`Interaction`] - Records of user/assistant/tool exchanges
//! - [`AgenticToolExecutor`] - Trait for executing tool calls
//! - [`AgenticTool`] - Trait for autonomous agentic tools
//!
//! # Example
//!
//! ```rust,ignore
//! use bamboo_agent::agent::core::tools::agentic::*;
//!
//! let goal = ToolGoal::new("Review code for bugs", json!({"file": "main.rs"}));
//! let context = AgenticContext::new(executor);
//! let tool = SmartCodeReviewTool::new();
//! let result = tool.execute(goal, context).await?;
//! ```

use async_trait::async_trait;
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::sync::Arc;
use tokio::sync::RwLock;

use crate::tools::{FunctionCall, ToolCall, ToolError};

/// Represents the objective and parameters for an agentic tool execution.
///
/// A `ToolGoal` defines what an agentic tool should accomplish, including
/// the intent, input parameters, and iteration limits.
///
/// # Fields
///
/// * `intent` - High-level description of what the tool should achieve
/// * `params` - JSON parameters for the tool execution
/// * `max_iterations` - Maximum number of iterations before stopping (default: 10)
///
/// # Example
///
/// ```rust,ignore
/// let goal = ToolGoal::new("Fix all bugs", json!({"files": ["main.rs", "lib.rs"]}))
///     .with_max_iterations(20);
/// ```
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ToolGoal {
    pub intent: String,
    /// Input parameters for the tool execution
    pub params: Value,
    /// Maximum iterations before stopping
    pub max_iterations: usize,
}

impl ToolGoal {
    /// Create a new tool goal with intent and parameters.
    ///
    /// Uses the default maximum iteration count of 10.
    ///
    /// # Arguments
    ///
    /// * `intent` - High-level description of the goal
    /// * `params` - JSON parameters for execution
    ///
    /// # Example
    ///
    /// ```rust,ignore
    /// let goal = ToolGoal::new("Review code", json!({"path": "src/main.rs"}));
    /// ```
    pub fn new(intent: impl Into<String>, params: Value) -> Self {
        Self {
            intent: intent.into(),
            params,
            max_iterations: 10,
        }
    }

    /// Set custom maximum iteration count.
    ///
    /// # Arguments
    ///
    /// * `max_iterations` - Maximum iterations allowed
    ///
    /// # Example
    ///
    /// ```rust,ignore
    /// let goal = ToolGoal::new("Complex task", params)
    ///     .with_max_iterations(50);
    /// ```
    pub fn with_max_iterations(mut self, max_iterations: usize) -> Self {
        self.max_iterations = max_iterations;
        self
    }
}

/// Role of an interaction participant.
///
/// Identifies who generated an interaction in the agentic loop.
///
/// # Variants
///
/// * `User` - Human user input
/// * `Assistant` - AI assistant response
/// * `Tool` - Tool execution result
/// * `System` - System message
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "lowercase")]
pub enum InteractionRole {
    User,
    Assistant,
    Tool,
    System,
}

/// Represents a single interaction in the agentic execution loop.
///
/// Records exchanges between user, assistant, and tools during
/// autonomous agent execution.
///
/// # Variants
///
/// * `User` - User message with timestamp
/// * `Assistant` - Assistant response with optional metadata
/// * `ToolAction` - Tool call initiated
/// * `ToolObservation` - Tool execution result
/// * `System` - System message
///
/// # Example
///
/// ```rust,ignore
/// let user_msg = Interaction::User {
///     message: "Fix the bug".to_string(),
///     timestamp: Utc::now(),
/// };
///
/// let tool_action = Interaction::ToolAction {
///     call: tool_call,
///     timestamp: Utc::now(),
/// };
/// ```
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum Interaction {
    /// User message
    User {
        /// Message content from user
        message: String,
        /// When the message was sent
        timestamp: DateTime<Utc>,
    },
    /// Assistant response
    Assistant {
        /// Response message
        message: String,
        /// Optional metadata (e.g., confidence, reasoning)
        metadata: Option<Value>,
        /// When the response was generated
        timestamp: DateTime<Utc>,
    },
    /// Tool call initiated
    ToolAction {
        /// The tool call being made
        call: ToolCall,
        /// When the call was initiated
        timestamp: DateTime<Utc>,
    },
    /// Tool execution result
    ToolObservation {
        /// Name of the tool that was executed
        tool_name: String,
        /// Output/result from the tool
        output: String,
        /// When the observation was made
        timestamp: DateTime<Utc>,
    },
    /// System message
    System {
        /// System message content
        message: String,
        /// When the message was generated
        timestamp: DateTime<Utc>,
    },
}

impl Interaction {
    fn from_role(
        role: InteractionRole,
        content: impl Into<String>,
        metadata: Option<Value>,
    ) -> Self {
        let content = content.into();
        let timestamp = Utc::now();

        match role {
            InteractionRole::User => Self::User {
                message: content,
                timestamp,
            },
            InteractionRole::Assistant => Self::Assistant {
                message: content,
                metadata,
                timestamp,
            },
            InteractionRole::Tool => Self::ToolObservation {
                tool_name: "agentic_tool".to_string(),
                output: content,
                timestamp,
            },
            InteractionRole::System => Self::System {
                message: content,
                timestamp,
            },
        }
    }
}

/// Execution context for agentic tools.
///
/// Maintains state, interaction history, and tool executor reference
/// for autonomous agent execution loops.
///
/// # Fields
///
/// * `state` - Shared mutable state for cross-iteration persistence
/// * `interaction_history` - Record of all interactions
/// * `base_executor` - Tool executor for running tools
///
/// # Thread Safety
///
/// The `state` field uses `Arc<RwLock<Value>>` for thread-safe
/// concurrent access from multiple async tasks.
///
/// # Example
///
/// ```rust,ignore
/// let executor = Arc::new(BuiltinToolExecutor::new());
/// let context = AgenticContext::new(executor);
///
/// // Record interactions
/// context.record_interaction(InteractionRole::User, "Hello");
///
/// // Update state
/// context.update_state(json!({"step": 1})).await;
///
/// // Check iterations
/// if !context.increment_iteration(10) {
///     // Continue execution
/// }
/// ```
pub struct AgenticContext {
    /// Shared mutable state (thread-safe)
    pub state: Arc<RwLock<Value>>,
    /// History of all interactions
    pub interaction_history: Vec<Interaction>,
    /// Tool executor reference
    pub base_executor: Arc<dyn AgenticToolExecutor>,
    /// Current iteration count
    iteration_count: usize,
}

impl AgenticContext {
    /// Create a new agentic context with empty state.
    ///
    /// # Arguments
    ///
    /// * `base_executor` - Tool executor for running tools
    ///
    /// # Example
    ///
    /// ```rust,ignore
    /// let executor = Arc::new(MyExecutor::new());
    /// let context = AgenticContext::new(executor);
    /// ```
    pub fn new(base_executor: Arc<dyn AgenticToolExecutor>) -> Self {
        Self {
            state: Arc::new(RwLock::new(serde_json::json!({}))),
            interaction_history: Vec::new(),
            base_executor,
            iteration_count: 0,
        }
    }

    /// Create context with custom initial state.
    ///
    /// # Arguments
    ///
    /// * `base_executor` - Tool executor
    /// * `initial_state` - Initial state value
    ///
    /// # Example
    ///
    /// ```rust,ignore
    /// let state = json!({"files": ["main.rs"]});
    /// let context = AgenticContext::with_state(executor, state);
    /// ```
    pub fn with_state(base_executor: Arc<dyn AgenticToolExecutor>, initial_state: Value) -> Self {
        Self {
            state: Arc::new(RwLock::new(initial_state)),
            interaction_history: Vec::new(),
            base_executor,
            iteration_count: 0,
        }
    }

    /// Record a simple interaction without metadata.
    ///
    /// # Arguments
    ///
    /// * `role` - Role of the interaction source
    /// * `content` - Interaction content
    pub fn record_interaction(&mut self, role: InteractionRole, content: impl Into<String>) {
        self.interaction_history
            .push(Interaction::from_role(role, content, None));
    }

    /// Record an interaction with metadata.
    ///
    /// # Arguments
    ///
    /// * `role` - Role of the interaction source
    /// * `content` - Interaction content
    /// * `metadata` - Additional metadata
    pub fn record_interaction_with_metadata(
        &mut self,
        role: InteractionRole,
        content: impl Into<String>,
        metadata: Value,
    ) {
        self.interaction_history
            .push(Interaction::from_role(role, content, Some(metadata)));
    }

    /// Record a tool call action.
    ///
    /// # Arguments
    ///
    /// * `call` - Tool call to record
    pub fn record_tool_action(&mut self, call: ToolCall) {
        self.interaction_history.push(Interaction::ToolAction {
            call,
            timestamp: Utc::now(),
        });
    }

    /// Record a tool execution result.
    ///
    /// # Arguments
    ///
    /// * `tool_name` - Name of the tool
    /// * `output` - Tool execution output
    pub fn record_tool_observation(
        &mut self,
        tool_name: impl Into<String>,
        output: impl Into<String>,
    ) {
        self.interaction_history.push(Interaction::ToolObservation {
            tool_name: tool_name.into(),
            output: output.into(),
            timestamp: Utc::now(),
        });
    }

    /// Increment iteration count and check limit.
    ///
    /// # Arguments
    ///
    /// * `max_iterations` - Maximum allowed iterations
    ///
    /// # Returns
    ///
    /// `true` if limit exceeded, `false` if within limit
    pub fn increment_iteration(&mut self, max_iterations: usize) -> bool {
        self.iteration_count += 1;
        self.iteration_count > max_iterations
    }

    /// Get current iteration count.
    pub fn iteration_count(&self) -> usize {
        self.iteration_count
    }

    /// Check if this is the first iteration.
    pub fn is_first_iteration(&self) -> bool {
        self.iteration_count == 0
    }

    /// Read shared state asynchronously.
    ///
    /// Returns a read guard that releases when dropped.
    pub async fn read_state(&self) -> tokio::sync::RwLockReadGuard<'_, Value> {
        self.state.read().await
    }

    /// Write shared state asynchronously.
    ///
    /// Returns a write guard that releases when dropped.
    pub async fn write_state(&self) -> tokio::sync::RwLockWriteGuard<'_, Value> {
        self.state.write().await
    }

    /// Replace state with a new value.
    ///
    /// # Arguments
    ///
    /// * `new_state` - New state value
    pub async fn update_state(&self, new_state: Value) {
        let mut state = self.state.write().await;
        *state = new_state;
    }

    /// Merge partial state into existing state.
    ///
    /// Only updates keys that exist in the partial value.
    /// Requires both existing and partial states to be JSON objects.
    ///
    /// # Arguments
    ///
    /// * `partial` - Partial state to merge
    pub async fn merge_state(&self, partial: Value) {
        let mut state = self.state.write().await;
        if let Value::Object(ref mut existing) = *state {
            if let Value::Object(partial_map) = partial {
                for (key, value) in partial_map {
                    existing.insert(key, value);
                }
            }
        }
    }
}

/// Trait for executing tool calls in agentic context.
///
/// Implement this trait to provide custom tool execution logic
/// for agentic tools.
///
/// # Required Methods
///
/// - `execute` - Execute a tool call
///
/// # Example
///
/// ```rust,ignore
/// struct MyExecutor;
///
/// #[async_trait]
/// impl AgenticToolExecutor for MyExecutor {
///     async fn execute(&self, call: &ToolCall) -> Result<ToolResult, ToolError> {
///         // Execute tool and return result
///         Ok(ToolResult::success("Done"))
///     }
/// }
/// ```
#[async_trait]
pub trait AgenticToolExecutor: Send + Sync {
    /// Execute a tool call.
    async fn execute(&self, call: &ToolCall) -> Result<ToolResult, ToolError>;
}

/// Result from agentic tool execution.
///
/// Indicates the outcome of an agentic tool's execution step.
///
/// # Variants
///
/// * `Success` - Task completed successfully
/// * `Error` - Execution failed
/// * `NeedClarification` - Need user input
/// * `NeedMoreActions` - More tool calls needed
///
/// # Example
///
/// ```rust,ignore
/// let result = ToolResult::success("Task completed");
/// let error = ToolResult::error("Failed to execute");
/// let clarify = ToolResult::need_clarification("Which file?");
/// let more = ToolResult::need_more_actions(vec![tool_call], "Need to read file");
/// ```
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type", content = "data")]
pub enum ToolResult {
    Success {
        result: String,
    },
    Error {
        error: String,
    },
    NeedClarification {
        question: String,
        options: Option<Vec<String>>,
    },
    NeedMoreActions {
        /// Tool calls to execute
        actions: Vec<ToolCall>,
        /// Reason for needing more actions
        reason: String,
    },
}

/// Type alias for agentic tool results.
pub type AgenticToolResult = ToolResult;

impl ToolResult {
    /// Create a success result.
    pub fn success(result: impl Into<String>) -> Self {
        Self::Success {
            result: result.into(),
        }
    }

    /// Create an error result.
    pub fn error(error: impl Into<String>) -> Self {
        Self::Error {
            error: error.into(),
        }
    }

    /// Create a clarification request without options.
    pub fn need_clarification(question: impl Into<String>) -> Self {
        Self::NeedClarification {
            question: question.into(),
            options: None,
        }
    }

    /// Create a clarification request with predefined options.
    pub fn need_clarification_with_options(
        question: impl Into<String>,
        options: Vec<String>,
    ) -> Self {
        Self::NeedClarification {
            question: question.into(),
            options: Some(options),
        }
    }

    /// Request more tool executions.
    pub fn need_more_actions(actions: Vec<ToolCall>, reason: impl Into<String>) -> Self {
        Self::NeedMoreActions {
            actions,
            reason: reason.into(),
        }
    }

    /// Check if result is successful.
    pub fn is_success(&self) -> bool {
        matches!(self, Self::Success { .. })
    }

    /// Check if result is an error.
    pub fn is_error(&self) -> bool {
        matches!(self, Self::Error { .. })
    }

    /// Check if clarification is needed.
    pub fn needs_clarification(&self) -> bool {
        matches!(self, Self::NeedClarification { .. })
    }

    /// Check if more actions are needed.
    pub fn needs_more_actions(&self) -> bool {
        matches!(self, Self::NeedMoreActions { .. })
    }
}

/// Trait for implementing autonomous agentic tools.
///
/// Agentic tools can execute multiple iterations, maintain state,
/// and make autonomous decisions about which tools to call.
///
/// # Required Methods
///
/// - `name()` - Tool identifier
/// - `description()` - Tool description
/// - `execute()` - Execute the agentic tool
///
/// # Difference from Regular Tools
///
/// Unlike regular [`Tool`](crate::tools::registry::Tool) trait:
/// - Receives a [`ToolGoal`] instead of direct arguments
/// - Has access to [`AgenticContext`] for state and history
/// - Can execute multiple iterations
/// - Returns [`ToolResult`] with continuation options
///
/// # Example
///
/// ```rust,ignore
/// struct SmartCodeReviewTool;
///
/// #[async_trait]
/// impl AgenticTool for SmartCodeReviewTool {
///     fn name(&self) -> &str {
///         "smart_code_review"
///     }
///
///     fn description(&self) -> &str {
///         "Autonomously review and fix code issues"
///     }
///
///     async fn execute(
///         &self,
///         goal: ToolGoal,
///         context: &mut AgenticContext,
///     ) -> Result<ToolResult, ToolError> {
///         // Iterate until goal achieved
///         loop {
///             if context.increment_iteration(goal.max_iterations) {
///                 return Ok(ToolResult::error("Max iterations reached"));
///             }
///
///             // Analyze code and decide actions
///             // Execute tools via context.base_executor
///             // Update state and record interactions
///
///             if goal_achieved(&context).await {
///                 return Ok(ToolResult::success("Review complete"));
///             }
///         }
///     }
/// }
/// ```
#[async_trait]
pub trait AgenticTool: Send + Sync {
    /// Get tool name.
    fn name(&self) -> &str;

    /// Get tool description.
    fn description(&self) -> &str;

    /// Execute the agentic tool with goal and context.
    ///
    /// # Arguments
    ///
    /// * `goal` - Execution goal and parameters
    /// * `context` - Execution context with state and history
    ///
    /// # Returns
    ///
    /// Tool execution result indicating success, error, or need for clarification.
    async fn execute(
        &self,
        goal: ToolGoal,
        context: &mut AgenticContext,
    ) -> Result<ToolResult, ToolError>;
}

/// Convert agentic tool result to standard tool result.
///
/// Maps [`ToolResult`] from this module to the standard
/// [`crate::tools::types::ToolResult`] format.
///
/// # Arguments
///
/// * `agentic_result` - Agentic tool result to convert
///
/// # Returns
///
/// Standard tool result with appropriate display preferences.
///
/// # Mapping
///
/// - `Success` → `success: true, display_preference: None`
/// - `Error` → `success: false, display_preference: "error"`
/// - `NeedClarification` → `success: true, display_preference: "clarification"`
/// - `NeedMoreActions` → `success: true, display_preference: "continuation"`
pub fn convert_to_standard_result(agentic_result: ToolResult) -> crate::tools::types::ToolResult {
    match agentic_result {
        ToolResult::Success { result } => crate::tools::types::ToolResult {
            success: true,
            result,
            display_preference: None,
        },
        ToolResult::Error { error } => crate::tools::types::ToolResult {
            success: false,
            result: error,
            display_preference: Some("error".to_string()),
        },
        ToolResult::NeedClarification { question, .. } => crate::tools::types::ToolResult {
            success: true,
            result: question,
            display_preference: Some("clarification".to_string()),
        },
        ToolResult::NeedMoreActions { reason, .. } => crate::tools::types::ToolResult {
            success: true,
            result: reason,
            display_preference: Some("actions_needed".to_string()),
        },
    }
}

/// Convert standard tool result to agentic tool result.
///
/// Inverse of [`convert_to_standard_result`].
///
/// # Arguments
///
/// * `standard_result` - Standard tool result to convert
///
/// # Returns
///
/// Agentic tool result based on success and display preference.
///
/// # Mapping
///
/// - `success: true` + `"clarification"` → `NeedClarification`
/// - `success: true` + `"actions_needed"` → `NeedMoreActions`
/// - `success: true` + other → `Success`
/// - `success: false` → `Error`
pub fn convert_from_standard_result(
    standard_result: crate::tools::types::ToolResult,
) -> ToolResult {
    if standard_result.success {
        match standard_result.display_preference.as_deref() {
            Some("clarification") => ToolResult::NeedClarification {
                question: standard_result.result,
                options: None,
            },
            Some("actions_needed") => ToolResult::NeedMoreActions {
                actions: Vec::new(),
                reason: standard_result.result,
            },
            _ => ToolResult::Success {
                result: standard_result.result,
            },
        }
    } else {
        ToolResult::Error {
            error: standard_result.result,
        }
    }
}

/// Smart code review tool with autonomous execution.
///
/// An agentic tool that autonomously reviews code, identifies issues,
/// and plans fixes using multiple tool calls.
///
/// # Capabilities
///
/// - Chooses appropriate review strategy
/// - Asks clarifying questions when needed
/// - Plans and executes follow-up actions
/// - Tracks progress across iterations
///
/// # Example
///
/// ```rust,ignore
/// use bamboo_agent::agent::core::tools::agentic::*;
///
/// let tool = SmartCodeReviewTool::new();
/// let executor = Arc::new(BuiltinToolExecutor::new());
/// let mut context = AgenticContext::new(executor);
///
/// let goal = ToolGoal::new(
///     "Review code for bugs",
///     json!({"files": ["src/main.rs"]})
/// );
///
/// let result = tool.execute(goal, &mut context).await?;
/// ```
pub struct SmartCodeReviewTool {
    /// Tool name
    name: String,
    /// Tool description
    description: String,
}

impl Default for SmartCodeReviewTool {
    fn default() -> Self {
        Self {
            name: "smart_code_review".to_string(),
            description: "Autonomous reviewer that chooses review strategy, asks clarifying questions, and plans follow-up tool actions.".to_string(),
        }
    }
}

impl SmartCodeReviewTool {
    /// Create a new smart code review tool.
    pub fn new() -> Self {
        Self::default()
    }

    /// Collect code findings from content analysis.
    ///
    /// Analyzes code for common issues and patterns.
    ///
    /// # Arguments
    ///
    /// * `content` - Code content to analyze
    ///
    /// # Returns
    ///
    /// Tuple of (findings list, has_critical_issues)
    fn collect_findings(&self, content: &str) -> (Vec<String>, bool) {
        let mut findings = Vec::new();
        let mut has_critical = false;

        if content.contains("unsafe ") {
            has_critical = true;
            findings.push("critical: unsafe block detected".to_string());
        }

        if content.contains("unwrap()") {
            findings.push("warning: unwrap() detected".to_string());
        }

        if content.contains("todo!") || content.contains("TODO") {
            findings.push("warning: unresolved TODO detected".to_string());
        }

        let long_line_count = content.lines().filter(|line| line.len() > 120).count();
        if long_line_count > 0 {
            findings.push(format!(
                "warning: {} line(s) exceed 120 characters",
                long_line_count
            ));
        }

        (findings, has_critical)
    }

    /// Choose review strategy based on content and findings.
    ///
    /// # Arguments
    ///
    /// * `content` - Code content
    /// * `findings` - List of findings
    ///
    /// # Returns
    ///
    /// Strategy name: "quick", "standard", or "deep"
    fn choose_strategy(&self, content: &str, findings: &[String]) -> &'static str {
        let line_count = content.lines().count();

        if line_count > 300 || findings.len() > 3 {
            "deep"
        } else if line_count > 80 || !findings.is_empty() {
            "standard"
        } else {
            "quick"
        }
    }

    /// Build follow-up tool actions for code review.
    ///
    /// Creates tool calls for running linters and tests.
    ///
    /// # Arguments
    ///
    /// * `file_path` - Optional file path to review
    ///
    /// # Returns
    ///
    /// List of tool calls to execute
    fn build_actions(&self, file_path: Option<&str>) -> Vec<ToolCall> {
        let path_hint = file_path.unwrap_or("<unknown-file>");

        vec![
            ToolCall {
                id: "agentic-action-1".to_string(),
                tool_type: "function".to_string(),
                function: FunctionCall {
                    name: "execute_command".to_string(),
                    arguments: serde_json::json!({
                        "command": format!("cargo clippy --all-targets --all-features -- {}", path_hint)
                    })
                    .to_string(),
                },
            },
            ToolCall {
                id: "agentic-action-2".to_string(),
                tool_type: "function".to_string(),
                function: FunctionCall {
                    name: "execute_command".to_string(),
                    arguments: serde_json::json!({
                        "command": "cargo test"
                    })
                    .to_string(),
                },
            },
        ]
    }
}

#[async_trait]
impl AgenticTool for SmartCodeReviewTool {
    fn name(&self) -> &str {
        &self.name
    }

    fn description(&self) -> &str {
        &self.description
    }

    async fn execute(
        &self,
        goal: ToolGoal,
        context: &mut AgenticContext,
    ) -> Result<ToolResult, ToolError> {
        context.record_interaction(
            InteractionRole::System,
            format!("Start smart review for intent: {}", goal.intent),
        );

        let content = goal.params.get("content").and_then(Value::as_str);
        if content.is_none() {
            return Ok(ToolResult::need_clarification_with_options(
                "I need the source code content to run a review. Please provide `content`."
                    .to_string(),
                vec![
                    "Paste the full file content".to_string(),
                    "Provide a trimmed snippet and target concerns".to_string(),
                ],
            ));
        }

        let content = content.unwrap_or_default();
        if content.trim().is_empty() {
            return Ok(ToolResult::need_clarification(
                "The provided content is empty. Please share non-empty code.".to_string(),
            ));
        }

        let first_iteration = context.is_first_iteration();

        if context.increment_iteration(goal.max_iterations) {
            return Ok(ToolResult::error(format!(
                "max_iterations reached: {}",
                goal.max_iterations
            )));
        }

        let file_path = goal.params.get("file_path").and_then(Value::as_str);
        let (findings, has_critical) = self.collect_findings(content);
        let strategy = self.choose_strategy(content, &findings).to_string();

        context
            .merge_state(serde_json::json!({
                "intent": goal.intent,
                "strategy": strategy,
                "findings": findings,
                "line_count": content.lines().count(),
            }))
            .await;

        if has_critical && first_iteration {
            return Ok(ToolResult::need_clarification_with_options(
                "I found critical safety signals (for example `unsafe`). Should I continue with defensive recommendations or stop for manual review?".to_string(),
                vec![
                    "Continue with defensive recommendations".to_string(),
                    "Stop and return only critical issues".to_string(),
                ],
            ));
        }

        if strategy == "deep" {
            let actions = self.build_actions(file_path);
            let auto_execute = goal
                .params
                .get("auto_execute_actions")
                .and_then(Value::as_bool)
                .unwrap_or(false);

            if auto_execute {
                if let Some(first_action) = actions.first() {
                    context.record_tool_action(first_action.clone());
                    let action_result = context.base_executor.execute(first_action).await?;
                    context.record_tool_observation(
                        first_action.function.name.as_str(),
                        format!("{:?}", action_result),
                    );
                }
            } else {
                return Ok(ToolResult::need_more_actions(
                    actions,
                    "Deep review requires lint/test evidence before final verdict.",
                ));
            }
        }

        let summary = serde_json::json!({
            "strategy": strategy,
            "finding_count": findings.len(),
            "findings": findings,
        });

        Ok(ToolResult::success(
            serde_json::to_string_pretty(&summary).unwrap_or_default(),
        ))
    }
}

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

    struct MockExecutor;

    #[async_trait]
    impl AgenticToolExecutor for MockExecutor {
        async fn execute(&self, _call: &ToolCall) -> Result<ToolResult, ToolError> {
            Ok(ToolResult::success("mock-executed"))
        }
    }

    #[test]
    fn tool_goal_should_store_intent_params_and_iteration_limit() {
        let goal = ToolGoal::new(
            "review rust",
            serde_json::json!({ "content": "fn main() {}" }),
        )
        .with_max_iterations(3);

        assert_eq!(goal.intent, "review rust");
        assert_eq!(goal.max_iterations, 3);
        assert_eq!(goal.params["content"], "fn main() {}");
    }

    #[test]
    fn interaction_history_should_use_enum_variants() {
        let executor: Arc<dyn AgenticToolExecutor> = Arc::new(MockExecutor);
        let mut context = AgenticContext::new(executor);

        context.record_interaction(InteractionRole::User, "Review this function");

        assert_eq!(context.interaction_history.len(), 1);
        assert!(matches!(
            &context.interaction_history[0],
            Interaction::User { message, .. } if message == "Review this function"
        ));
    }

    #[test]
    fn tool_result_should_support_new_agentic_variants() {
        let clarification = ToolResult::need_clarification_with_options(
            "Which standard should I follow?",
            vec!["Rust style".to_string(), "Project style".to_string()],
        );
        assert!(clarification.needs_clarification());

        let actions = ToolResult::need_more_actions(
            vec![ToolCall {
                id: "1".to_string(),
                tool_type: "function".to_string(),
                function: FunctionCall {
                    name: "execute_command".to_string(),
                    arguments: "{}".to_string(),
                },
            }],
            "Need more evidence",
        );
        assert!(actions.needs_more_actions());
    }

    #[tokio::test]
    async fn smart_review_should_ask_for_content_when_missing() {
        let tool = SmartCodeReviewTool::new();
        let executor: Arc<dyn AgenticToolExecutor> = Arc::new(MockExecutor);
        let mut context = AgenticContext::new(executor);

        let goal = ToolGoal::new("review", serde_json::json!({}));
        let result = tool.execute(goal, &mut context).await.unwrap();

        assert!(result.needs_clarification());
    }

    #[tokio::test]
    async fn smart_review_should_request_more_actions_for_deep_review() {
        let tool = SmartCodeReviewTool::new();
        let executor: Arc<dyn AgenticToolExecutor> = Arc::new(MockExecutor);
        let mut context = AgenticContext::new(executor);

        let large_code = (0..360)
            .map(|idx| format!("fn f{}() {{ println!(\"{}\") }}", idx, idx))
            .collect::<Vec<_>>()
            .join("\n");

        let goal = ToolGoal::new(
            "review",
            serde_json::json!({
                "file_path": "src/lib.rs",
                "content": large_code,
            }),
        );

        let result = tool.execute(goal, &mut context).await.unwrap();
        assert!(result.needs_more_actions());
    }

    #[tokio::test]
    async fn smart_review_should_return_success_for_small_clean_code() {
        let tool = SmartCodeReviewTool::new();
        let executor: Arc<dyn AgenticToolExecutor> = Arc::new(MockExecutor);
        let mut context = AgenticContext::new(executor);

        let goal = ToolGoal::new(
            "review",
            serde_json::json!({
                "content": "fn sum(a: i32, b: i32) -> i32 { a + b }",
            }),
        )
        .with_max_iterations(5);

        let result = tool.execute(goal, &mut context).await.unwrap();
        assert!(result.is_success());
    }
}