paladin-ai 0.5.1

Enterprise AI orchestration framework with multi-agent coordination patterns
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
//! Tool result formatting for LLM context injection
//!
//! This module provides formatting capabilities to convert tool execution results
//! into human-readable text suitable for injection into LLM conversation context.
//!
//! # Examples
//!
//! ```rust,no_run
//! use paladin::infrastructure::adapters::arsenal::tool_result_formatter::ToolResultFormatter;
//! use paladin::core::platform::container::arsenal::{ArmamentCall, ArmamentResult};
//! use std::collections::HashMap;
//! use serde_json::Value;
//! use uuid::Uuid;
//!
//! let formatter = ToolResultFormatter::new();
//! let call_id = Uuid::new_v4();
//!
//! let call = ArmamentCall::new("calculator", HashMap::new());
//! let result = ArmamentResult::success(call_id, Value::String("42".to_string()), 150);
//!
//! let formatted = formatter.format_result(&call, &result);
//! println!("{}", formatted);
//! ```

use crate::core::platform::container::arsenal::{ArmamentCall, ArmamentResult};
use serde_json::Value;

/// Formats tool execution results for LLM context injection
///
/// The formatter converts structured `ArmamentResult` objects into
/// human-readable markdown-like text that can be injected into
/// LLM conversation context. This allows the LLM to understand
/// what tools were called and what results they returned.
///
/// # Format Structure
///
/// Successful result:
/// ```text
/// 🔧 Tool Execution: tool_name
///
/// Arguments:
/// - arg1: value1
/// - arg2: value2
///
/// Result: SUCCESS
/// Output:
/// tool output here
///
/// Execution Time: 150ms
/// ```
///
/// Failed result:
/// ```text
/// 🔧 Tool Execution: tool_name
///
/// Arguments:
/// - arg1: value1
///
/// Result: FAILED
/// Error: error message here
///
/// Execution Time: 75ms
/// ```
#[doc(hidden)]
#[derive(Debug, Clone)]
pub struct ToolResultFormatter {
    /// Whether to include execution time in formatted output
    include_timing: bool,
    /// Whether to use emoji indicators
    use_emoji: bool,
}

impl ToolResultFormatter {
    /// Creates a new ToolResultFormatter with default settings
    ///
    /// Default settings:
    /// - Include timing: true
    /// - Use emoji: true
    ///
    /// # Example
    ///
    /// ```rust
    /// use paladin::infrastructure::adapters::arsenal::tool_result_formatter::ToolResultFormatter;
    ///
    /// let formatter = ToolResultFormatter::new();
    /// ```
    pub fn new() -> Self {
        Self {
            include_timing: true,
            use_emoji: true,
        }
    }

    /// Creates a formatter without emoji indicators
    ///
    /// Useful for contexts where emoji may not render correctly.
    ///
    /// # Example
    ///
    /// ```rust
    /// use paladin::infrastructure::adapters::arsenal::tool_result_formatter::ToolResultFormatter;
    ///
    /// let formatter = ToolResultFormatter::without_emoji();
    /// ```
    pub fn without_emoji() -> Self {
        Self {
            include_timing: true,
            use_emoji: false,
        }
    }

    /// Creates a formatter without execution timing
    ///
    /// # Example
    ///
    /// ```rust
    /// use paladin::infrastructure::adapters::arsenal::tool_result_formatter::ToolResultFormatter;
    ///
    /// let formatter = ToolResultFormatter::without_timing();
    /// ```
    pub fn without_timing() -> Self {
        Self {
            include_timing: false,
            use_emoji: true,
        }
    }

    /// Formats a tool execution result into LLM-readable text
    ///
    /// Converts the structured `ArmamentResult` into a markdown-like format
    /// that includes the tool name, arguments, result status, output/error,
    /// and execution time.
    ///
    /// # Arguments
    ///
    /// * `call` - The tool invocation that was executed
    /// * `result` - The result of the tool execution
    ///
    /// # Returns
    ///
    /// A formatted string suitable for injection into LLM context
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// # use paladin::infrastructure::adapters::arsenal::tool_result_formatter::ToolResultFormatter;
    /// # use paladin::core::platform::container::arsenal::{ArmamentCall, ArmamentResult};
    /// # use std::collections::HashMap;
    /// # use serde_json::Value;
    /// # use uuid::Uuid;
    /// let formatter = ToolResultFormatter::new();
    /// let call = ArmamentCall::new("search", HashMap::new());
    /// let result = ArmamentResult::success(Uuid::new_v4(), Value::String("Found 10 results".to_string()), 200);
    ///
    /// let formatted = formatter.format_result(&call, &result);
    /// ```
    pub fn format_result(&self, call: &ArmamentCall, result: &ArmamentResult) -> String {
        let mut output = String::new();

        // Header
        let tool_icon = if self.use_emoji { "🔧 " } else { "" };
        output.push_str(&format!(
            "{}Tool Execution: {}\n\n",
            tool_icon, call.tool_name
        ));

        // Arguments section
        if !call.arguments.is_empty() {
            output.push_str("Arguments:\n");
            for (key, value) in &call.arguments {
                let formatted_value = self.format_value(value);
                output.push_str(&format!("- {}: {}\n", key, formatted_value));
            }
            output.push('\n');
        }

        // Result section
        if result.success {
            let success_icon = if self.use_emoji { "" } else { "" };
            output.push_str(&format!("{}Result: SUCCESS\n", success_icon));

            if let Some(ref result_output) = result.output {
                output.push_str("Output:\n");
                output.push_str(&self.format_output_value(result_output));
                output.push('\n');
            }
        } else {
            let error_icon = if self.use_emoji { "" } else { "" };
            output.push_str(&format!("{}Result: FAILED\n", error_icon));

            if let Some(ref error) = result.error {
                output.push_str(&format!("Error: {}\n", error));
            }
        }

        // Execution time
        if self.include_timing {
            output.push_str(&format!(
                "\nExecution Time: {}ms\n",
                result.execution_time_ms
            ));
        }

        output
    }

    /// Formats an output value for display
    ///
    /// Converts output values into human-readable strings with
    /// proper formatting and indentation.
    fn format_output_value(&self, value: &Value) -> String {
        match value {
            Value::String(s) => s.clone(),
            Value::Null => "null".to_string(),
            Value::Bool(b) => b.to_string(),
            Value::Number(n) => n.to_string(),
            Value::Array(_) | Value::Object(_) => {
                // Pretty print JSON structures
                serde_json::to_string_pretty(value).unwrap_or_else(|_| value.to_string())
            }
        }
    }

    /// Formats a JSON value for display in arguments
    ///
    /// Converts JSON values into human-readable strings, handling
    /// different value types appropriately.
    fn format_value(&self, value: &Value) -> String {
        match value {
            Value::Null => "null".to_string(),
            Value::Bool(b) => b.to_string(),
            Value::Number(n) => n.to_string(),
            Value::String(s) => s.clone(),
            Value::Array(arr) => {
                if arr.len() <= 3 {
                    format!(
                        "[{}]",
                        arr.iter()
                            .map(|v| self.format_value(v))
                            .collect::<Vec<_>>()
                            .join(", ")
                    )
                } else {
                    format!("[{} items]", arr.len())
                }
            }
            Value::Object(obj) => {
                if obj.len() <= 3 {
                    format!(
                        "{{{}}}",
                        obj.iter()
                            .map(|(k, v)| format!("{}: {}", k, self.format_value(v)))
                            .collect::<Vec<_>>()
                            .join(", ")
                    )
                } else {
                    format!("{{{}}} fields", obj.len())
                }
            }
        }
    }

    /// Formats multiple tool results into a single text block
    ///
    /// Useful for batch formatting of multiple tool invocations.
    ///
    /// # Arguments
    ///
    /// * `results` - Slice of (call, result) tuples
    ///
    /// # Returns
    ///
    /// A formatted string with all results separated by dividers
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// # use paladin::infrastructure::adapters::arsenal::tool_result_formatter::ToolResultFormatter;
    /// # use paladin::core::platform::container::arsenal::{ArmamentCall, ArmamentResult};
    /// # use std::collections::HashMap;
    /// # use serde_json::Value;
    /// # use uuid::Uuid;
    /// let formatter = ToolResultFormatter::new();
    /// let results = vec![
    ///     (ArmamentCall::new("tool1", HashMap::new()),
    ///      ArmamentResult::success(Uuid::new_v4(), Value::String("result1".to_string()), 100)),
    ///     (ArmamentCall::new("tool2", HashMap::new()),
    ///      ArmamentResult::success(Uuid::new_v4(), Value::String("result2".to_string()), 150)),
    /// ];
    ///
    /// let formatted = formatter.format_batch(&results);
    /// ```
    pub fn format_batch(&self, results: &[(ArmamentCall, ArmamentResult)]) -> String {
        if results.is_empty() {
            return String::new();
        }

        let divider = if self.use_emoji {
            "═══════════════════════════════════════\n"
        } else {
            "---------------------------------------\n"
        };

        results
            .iter()
            .map(|(call, result)| self.format_result(call, result))
            .collect::<Vec<_>>()
            .join(divider)
    }
}

impl Default for ToolResultFormatter {
    fn default() -> Self {
        Self::new()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::collections::HashMap;
    use uuid::Uuid;

    #[test]
    fn test_format_success_result() {
        let formatter = ToolResultFormatter::new();
        let call_id = Uuid::new_v4();

        let mut args = HashMap::new();
        args.insert(
            "query".to_string(),
            Value::String("test search".to_string()),
        );
        args.insert("limit".to_string(), Value::Number(10.into()));

        let call = ArmamentCall {
            tool_name: "web_search".to_string(),
            arguments: args,
            call_id,
        };

        let result =
            ArmamentResult::success(call_id, Value::String("Found 10 results".to_string()), 250);

        let formatted = formatter.format_result(&call, &result);

        assert!(formatted.contains("🔧 Tool Execution: web_search"));
        assert!(formatted.contains("Arguments:"));
        assert!(formatted.contains("query: test search"));
        assert!(formatted.contains("limit: 10"));
        assert!(formatted.contains("✅ Result: SUCCESS"));
        assert!(formatted.contains("Output:"));
        assert!(formatted.contains("Found 10 results"));
        assert!(formatted.contains("Execution Time: 250ms"));
    }

    #[test]
    fn test_format_error_result() {
        let formatter = ToolResultFormatter::new();
        let call_id = Uuid::new_v4();

        let mut args = HashMap::new();
        args.insert("file".to_string(), Value::String("missing.txt".to_string()));

        let call = ArmamentCall {
            tool_name: "read_file".to_string(),
            arguments: args,
            call_id,
        };

        let result = ArmamentResult::failure(call_id, "File not found", 50);

        let formatted = formatter.format_result(&call, &result);

        assert!(formatted.contains("🔧 Tool Execution: read_file"));
        assert!(formatted.contains("file: missing.txt"));
        assert!(formatted.contains("❌ Result: FAILED"));
        assert!(formatted.contains("Error: File not found"));
        assert!(formatted.contains("Execution Time: 50ms"));
    }

    #[test]
    fn test_format_without_emoji() {
        let formatter = ToolResultFormatter::without_emoji();
        let call_id = Uuid::new_v4();

        let call = ArmamentCall::new("calculator", HashMap::new());
        let result = ArmamentResult::success(call_id, Value::String("42".to_string()), 100);

        let formatted = formatter.format_result(&call, &result);

        assert!(!formatted.contains("🔧"));
        assert!(!formatted.contains(""));
        assert!(formatted.contains("Tool Execution: calculator"));
        assert!(formatted.contains("Result: SUCCESS"));
    }

    #[test]
    fn test_format_without_timing() {
        let formatter = ToolResultFormatter::without_timing();
        let call_id = Uuid::new_v4();

        let call = ArmamentCall::new("ping", HashMap::new());
        let result = ArmamentResult::success(call_id, Value::String("pong".to_string()), 25);

        let formatted = formatter.format_result(&call, &result);

        assert!(!formatted.contains("Execution Time"));
        assert!(!formatted.contains("25ms"));
    }

    #[test]
    fn test_format_batch() {
        let formatter = ToolResultFormatter::new();
        let call1_id = Uuid::new_v4();
        let call2_id = Uuid::new_v4();

        let call1 = ArmamentCall::new("tool1", HashMap::new());
        let result1 = ArmamentResult::success(call1_id, Value::String("result1".to_string()), 100);

        let call2 = ArmamentCall::new("tool2", HashMap::new());
        let result2 = ArmamentResult::success(call2_id, Value::String("result2".to_string()), 150);

        let results = vec![(call1, result1), (call2, result2)];
        let formatted = formatter.format_batch(&results);

        assert!(formatted.contains("tool1"));
        assert!(formatted.contains("tool2"));
        assert!(formatted.contains("result1"));
        assert!(formatted.contains("result2"));
        assert!(formatted.contains("═══════════════"));
    }

    #[test]
    fn test_format_value_types() {
        let formatter = ToolResultFormatter::new();

        assert_eq!(formatter.format_value(&Value::Null), "null");
        assert_eq!(formatter.format_value(&Value::Bool(true)), "true");
        assert_eq!(formatter.format_value(&Value::Number(42.into())), "42");
        assert_eq!(
            formatter.format_value(&Value::String("test".to_string())),
            "test"
        );

        let arr = Value::Array(vec![Value::Number(1.into()), Value::Number(2.into())]);
        assert_eq!(formatter.format_value(&arr), "[1, 2]");

        let large_arr = Value::Array(vec![Value::Number(1.into()); 5]);
        assert!(formatter.format_value(&large_arr).contains("[5 items]"));
    }

    #[test]
    fn test_format_empty_arguments() {
        let formatter = ToolResultFormatter::new();
        let call_id = Uuid::new_v4();

        let call = ArmamentCall::new("no_args_tool", HashMap::new());
        let result = ArmamentResult::success(call_id, Value::String("done".to_string()), 10);

        let formatted = formatter.format_result(&call, &result);

        // Should not have "Arguments:" section when empty
        assert!(!formatted.contains("Arguments:"));
        assert!(formatted.contains("Tool Execution: no_args_tool"));
    }
}