paladin-ai 0.5.0

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
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
//! Core output formatting with colors, boxes, and headers
//!
//! Provides utilities for formatted terminal output that respects NO_COLOR
//! environment variable and supports quiet/verbose modes.

use colored::*;
use std::env;

/// Output styling options
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum OutputStyle {
    /// Success message (green)
    Success,
    /// Error message (red)
    Error,
    /// Warning message (yellow)
    Warning,
    /// Informational message (blue)
    Info,
    /// Link or reference (cyan)
    Link,
    /// Default styling
    Default,
}

/// Output formatter with color and style support
#[derive(Debug, Clone)]
pub struct OutputFormatter {
    /// Whether colors are enabled
    colors_enabled: bool,
    /// Quiet mode (minimal output)
    quiet: bool,
    /// Verbose mode (detailed output)
    verbose: bool,
}

impl OutputFormatter {
    /// Create a new output formatter
    pub fn new() -> Self {
        Self {
            colors_enabled: !Self::no_color_requested(),
            quiet: false,
            verbose: false,
        }
    }

    /// Create a formatter with quiet mode enabled
    pub fn quiet() -> Self {
        Self {
            colors_enabled: !Self::no_color_requested(),
            quiet: true,
            verbose: false,
        }
    }

    /// Create a formatter with verbose mode enabled
    pub fn with_verbose() -> Self {
        Self {
            colors_enabled: !Self::no_color_requested(),
            quiet: false,
            verbose: true,
        }
    }

    /// Check if NO_COLOR environment variable is set
    fn no_color_requested() -> bool {
        env::var("NO_COLOR").is_ok()
    }

    /// Set quiet mode
    pub fn set_quiet(&mut self, quiet: bool) {
        self.quiet = quiet;
        if quiet {
            self.verbose = false;
        }
    }

    /// Set verbose mode
    pub fn set_verbose(&mut self, verbose: bool) {
        self.verbose = verbose;
        if verbose {
            self.quiet = false;
        }
    }

    /// Check if quiet mode is enabled
    pub fn is_quiet(&self) -> bool {
        self.quiet
    }

    /// Check if verbose mode is enabled
    pub fn is_verbose(&self) -> bool {
        self.verbose
    }

    /// Format text with the given style
    pub fn style(&self, text: &str, style: OutputStyle) -> String {
        if !self.colors_enabled {
            return text.to_string();
        }

        match style {
            OutputStyle::Success => text.green().to_string(),
            OutputStyle::Error => text.red().to_string(),
            OutputStyle::Warning => text.yellow().to_string(),
            OutputStyle::Info => text.blue().to_string(),
            OutputStyle::Link => text.cyan().to_string(),
            OutputStyle::Default => text.to_string(),
        }
    }

    /// Print a success message
    pub fn success(&self, message: &str) {
        if !self.quiet {
            println!("{} {}", self.style("", OutputStyle::Success), message);
        }
    }

    /// Print an error message
    pub fn error(&self, message: &str) {
        eprintln!("{} {}", self.style("", OutputStyle::Error), message);
    }

    /// Print a warning message
    pub fn warning(&self, message: &str) {
        if !self.quiet {
            println!("{} {}", self.style("", OutputStyle::Warning), message);
        }
    }

    /// Print an info message
    pub fn info(&self, message: &str) {
        if !self.quiet {
            println!("{} {}", self.style("", OutputStyle::Info), message);
        }
    }

    /// Print a verbose message (only in verbose mode)
    pub fn verbose(&self, message: &str) {
        if self.verbose {
            println!("{}", self.style(message, OutputStyle::Default));
        }
    }

    /// Print a header with box drawing
    pub fn header(&self, title: &str) {
        if self.quiet {
            return;
        }

        let width = title.len() + 4;
        let border = "".repeat(width);

        println!("{}", border);
        println!("{}", self.style(title, OutputStyle::Info));
        println!("{}", border);
    }

    /// Print a section header
    pub fn section(&self, title: &str) {
        if self.quiet {
            return;
        }

        println!(
            "\n{}",
            self.style(&format!("━━ {} ━━", title), OutputStyle::Info)
        );
    }

    /// Print a box with content
    pub fn box_message(&self, content: &[&str]) {
        if self.quiet {
            return;
        }

        let max_width = content.iter().map(|s| s.len()).max().unwrap_or(0);
        let border = "".repeat(max_width + 2);

        println!("{}", border);
        for line in content {
            println!("│ {:<width$} │", line, width = max_width);
        }
        println!("{}", border);
    }

    /// Format a key-value pair
    pub fn key_value(&self, key: &str, value: &str) -> String {
        format!("{}: {}", self.style(key, OutputStyle::Info), value)
    }

    /// Print an emoji if colors are enabled, otherwise print alternative text
    pub fn emoji_or<'a>(&self, emoji: &'a str, alt: &'a str) -> &'a str {
        if self.colors_enabled { emoji } else { alt }
    }

    /// Print a separator line
    pub fn separator(&self) {
        if !self.quiet {
            println!("{}", "".repeat(64));
        }
    }

    /// Print a blank line (unless in quiet mode)
    pub fn blank_line(&self) {
        if !self.quiet {
            println!();
        }
    }

    /// Format a Paladin result for human-readable output
    ///
    /// # Arguments
    ///
    /// * `result` - The Paladin execution result
    /// * `verbose` - Whether to include detailed information (loops, timing, etc.)
    ///
    /// # Example Output (Normal Mode)
    ///
    /// ```text
    /// ════════════════════════════════════════════════════════════════════
    /// 📊 Paladin Execution Result
    /// ════════════════════════════════════════════════════════════════════
    ///
    /// → Output:
    /// The analysis shows that...
    ///
    /// → Statistics:
    ///   • Execution Time: 1.25s
    ///   • Tokens Used: 150
    ///   • Status: Completed ✓
    /// ════════════════════════════════════════════════════════════════════
    /// ```
    pub fn format_paladin_result(
        &self,
        result: &paladin_ports::output::paladin_port::PaladinResult,
        verbose: bool,
    ) -> String {
        use paladin_ports::output::paladin_port::StopReason;

        let mut output = String::new();

        output.push_str(&"".repeat(80));
        output.push_str(&format!(
            "\n{} Paladin Execution Result\n",
            self.style("📊", OutputStyle::Info)
        ));
        output.push_str(&"".repeat(80));
        output.push('\n');

        // Main output
        output.push_str(&format!(
            "\n{} Output:\n",
            self.style("", OutputStyle::Info)
        ));
        output.push_str(&format!("{}\n", result.output));

        // Statistics section
        output.push_str(&format!(
            "\n{} Statistics:\n",
            self.style("", OutputStyle::Info)
        ));
        output.push_str(&format!(
            "  {} Execution Time: {:.2}s\n",
            self.style("", OutputStyle::Info),
            result.execution_time_ms as f64 / 1000.0
        ));
        output.push_str(&format!(
            "  {} Tokens Used: {}\n",
            self.style("", OutputStyle::Info),
            result.token_count
        ));

        // Status with color coding
        let status_str = match &result.stop_reason {
            StopReason::Completed => format!(
                "{} {}",
                self.style("Completed", OutputStyle::Success),
                self.style("", OutputStyle::Success)
            ),
            StopReason::StopWord(word) => format!(
                "{} {} ({})",
                self.style("Stopped", OutputStyle::Warning),
                self.style("", OutputStyle::Warning),
                word
            ),
            StopReason::MaxLoops => format!(
                "{} {}",
                self.style("Max Loops Reached", OutputStyle::Warning),
                self.style("", OutputStyle::Warning)
            ),
            StopReason::Timeout => format!(
                "{} {}",
                self.style("Timeout", OutputStyle::Error),
                self.style("", OutputStyle::Error)
            ),
        };
        output.push_str(&format!(
            "  {} Status: {}\n",
            self.style("", OutputStyle::Info),
            status_str
        ));

        // Verbose mode: additional details
        if verbose {
            output.push_str(&format!(
                "  {} Reasoning Loops: {}\n",
                self.style("", OutputStyle::Info),
                result.loop_count
            ));
            output.push_str(&format!(
                "  {} Stop Reason: {:?}\n",
                self.style("", OutputStyle::Info),
                result.stop_reason
            ));
        }

        output.push_str(&"".repeat(80));
        output.push('\n');

        output
    }

    /// Format a Paladin result as JSON for file output
    ///
    /// Includes comprehensive metadata, timing information, and execution details.
    pub fn format_paladin_result_json(
        result: &paladin_ports::output::paladin_port::PaladinResult,
    ) -> serde_json::Value {
        use serde_json::json;

        json!({
            "output": result.output,
            "metadata": {
                "token_count": result.token_count,
                "execution_time_ms": result.execution_time_ms,
                "execution_time_seconds": result.execution_time_ms as f64 / 1000.0,
                "loop_count": result.loop_count,
                "stop_reason": format!("{:?}", result.stop_reason),
                "is_successful": result.stop_reason.is_successful(),
                "is_limit_reached": result.stop_reason.is_limit(),
            },
            "timestamp": chrono::Utc::now().to_rfc3339(),
        })
    }

    /// Format a Battalion result for human-readable output
    ///
    /// # Arguments
    ///
    /// * `result` - The Battalion execution result
    /// * `verbose` - Whether to include detailed per-Paladin information
    pub fn format_battalion_result(
        &self,
        result: &crate::core::platform::container::battalion::BattalionResult,
        verbose: bool,
    ) -> String {
        use crate::core::platform::container::battalion::BattalionStatus;
        use paladin_ports::output::paladin_port::StopReason;

        let mut output = String::new();

        output.push_str(&"".repeat(80));
        output.push_str(&format!(
            "\n{} Battalion Execution Result: {}\n",
            self.style("🏰", OutputStyle::Info),
            self.style(&result.battalion_name, OutputStyle::Info)
        ));
        output.push_str(&"".repeat(80));
        output.push('\n');

        // Final aggregated output
        output.push_str(&format!(
            "\n{} Final Output:\n",
            self.style("", OutputStyle::Info)
        ));
        output.push_str(&format!("{}\n", result.final_output));

        // Statistics section
        output.push_str(&format!(
            "\n{} Statistics:\n",
            self.style("", OutputStyle::Info)
        ));
        output.push_str(&format!(
            "  {} Total Paladins: {}\n",
            self.style("", OutputStyle::Info),
            result.paladin_results.len()
        ));

        let success_style = if result.paladin_success_count == result.paladin_results.len() {
            OutputStyle::Success
        } else {
            OutputStyle::Warning
        };
        output.push_str(&format!(
            "  {} Successful: {} {}\n",
            self.style("", OutputStyle::Info),
            result.paladin_success_count,
            self.style(
                if success_style == OutputStyle::Success {
                    ""
                } else {
                    ""
                },
                success_style
            )
        ));

        if result.paladin_failure_count > 0 {
            output.push_str(&format!(
                "  {} Failed: {} {}\n",
                self.style("", OutputStyle::Info),
                result.paladin_failure_count,
                self.style("", OutputStyle::Error)
            ));
        }

        let total_time = (result.completed_at - result.started_at)
            .num_milliseconds()
            .max(0) as u64;
        output.push_str(&format!(
            "  {} Total Time: {:.2}s\n",
            self.style("", OutputStyle::Info),
            total_time as f64 / 1000.0
        ));

        output.push_str(&format!(
            "  {} Strategy: {:?}\n",
            self.style("", OutputStyle::Info),
            result.strategy_used
        ));

        // Strategy selection reasoning (Auto mode)
        if let Some(reasoning) = &result.strategy_selection_reasoning {
            output.push_str(&format!(
                "  {} Strategy Selection: {}\n",
                self.style("", OutputStyle::Info),
                reasoning
            ));
        }

        // Status
        let status_str = match result.status {
            BattalionStatus::Completed => format!(
                "{} {}",
                self.style("Completed", OutputStyle::Success),
                self.style("", OutputStyle::Success)
            ),
            BattalionStatus::Failed => format!(
                "{} {}",
                self.style("Failed", OutputStyle::Error),
                self.style("", OutputStyle::Error)
            ),
            BattalionStatus::Cancelled => format!(
                "{} {}",
                self.style("Cancelled", OutputStyle::Warning),
                self.style("", OutputStyle::Warning)
            ),
            _ => format!("{:?}", result.status),
        };
        output.push_str(&format!(
            "  {} Status: {}\n",
            self.style("", OutputStyle::Info),
            status_str
        ));

        // Verbose mode: show individual Paladin results
        if verbose && !result.paladin_results.is_empty() {
            output.push_str(&format!(
                "\n{} Individual Paladin Results:\n",
                self.style("", OutputStyle::Info)
            ));
            output.push_str(&"".repeat(80));
            output.push('\n');

            for (idx, paladin_result) in result.paladin_results.iter().enumerate() {
                // Try to find timing from per_paladin_times HashMap by index-based name,
                // fall back to the PaladinResult's own execution_time_ms
                let timing = result
                    .per_paladin_times
                    .get(&format!("paladin_{}", idx))
                    .copied()
                    .unwrap_or(paladin_result.execution_time_ms);

                output.push_str(&format!(
                    "\n{} Paladin {} - {} loops, {:.2}s, {} tokens\n",
                    self.style(&format!("{}.", idx + 1), OutputStyle::Info),
                    idx + 1,
                    paladin_result.loop_count,
                    timing as f64 / 1000.0,
                    paladin_result.token_count
                ));

                let (status_emoji, status_style) = match &paladin_result.stop_reason {
                    StopReason::Completed => ("", OutputStyle::Success),
                    StopReason::StopWord(_) => ("", OutputStyle::Warning),
                    StopReason::MaxLoops => ("", OutputStyle::Warning),
                    StopReason::Timeout => ("", OutputStyle::Error),
                };
                output.push_str(&format!(
                    "   Status: {} {:?}\n",
                    self.style(status_emoji, status_style),
                    paladin_result.stop_reason
                ));

                // Show first 200 chars of output
                let preview = if paladin_result.output.len() > 200 {
                    format!("{}...", &paladin_result.output[..200])
                } else {
                    paladin_result.output.clone()
                };
                output.push_str(&format!("   Output: {}\n", preview));
            }

            output.push_str(&"".repeat(80));
            output.push('\n');
        }

        output.push_str(&"".repeat(80));
        output.push('\n');

        output
    }

    /// Format a Battalion result as JSON for file output
    ///
    /// Includes comprehensive metadata, all individual Paladin results, and timing data.
    pub fn format_battalion_result_json(
        result: &crate::core::platform::container::battalion::BattalionResult,
    ) -> serde_json::Value {
        use serde_json::json;

        json!({
            "battalion_id": result.battalion_id,
            "battalion_name": result.battalion_name,
            "started_at": result.started_at.to_rfc3339(),
            "completed_at": result.completed_at.to_rfc3339(),
            "total_time_ms": (result.completed_at - result.started_at).num_milliseconds().max(0),
            "final_output": result.final_output,
            "status": format!("{:?}", result.status),
            "strategy_used": format!("{:?}", result.strategy_used),
            "strategy_selection_reasoning": result.strategy_selection_reasoning,
            "strategy_selection_time_ms": result.strategy_selection_time_ms,
            "paladin_results": result.paladin_results.iter().enumerate().map(|(idx, r)| {
                let timing = result.per_paladin_times.get(&format!("paladin_{}", idx)).copied().unwrap_or(r.execution_time_ms);
                json!({
                    "index": idx,
                    "output": r.output,
                    "token_count": r.token_count,
                    "execution_time_ms": timing,
                    "loop_count": r.loop_count,
                    "stop_reason": format!("{:?}", r.stop_reason),
                    "is_successful": r.stop_reason.is_successful(),
                })
            }).collect::<Vec<_>>(),
            "summary": {
                "total_paladins": result.paladin_results.len(),
                "successful": result.paladin_success_count,
                "failed": result.paladin_failure_count,
            },
            "timestamp": chrono::Utc::now().to_rfc3339(),
        })
    }
}

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