paladin-ai 0.1.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
//! Battalion YAML configuration types for multi-agent orchestration
//!
//! This module defines configuration schemas for four Battalion orchestration patterns:
//! - **Formation**: Sequential execution (output N → input N+1)
//! - **Phalanx**: Concurrent execution with result aggregation
//! - **Campaign**: Graph/DAG-based conditional routing
//! - **Chain of Command**: Hierarchical delegation with commander
//!
//! # Example Formation Config
//!
//! ```yaml
//! type: formation
//! name: "document-pipeline"
//! agents:
//!   - name: "analyzer"
//!     system_prompt: "Analyze the input"
//!     model: "gpt-4"
//!     provider:
//!       type: "openai"
//!   - name: "summarizer"
//!     system_prompt: "Summarize the analysis"
//!     model: "gpt-4"
//!     provider:
//!       type: "openai"
//! pass_output_to_next: true
//! ```

use crate::application::cli::error::CliError;
use serde::{Deserialize, Serialize};

/// Battalion YAML configuration with type discriminator
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type")]
pub enum BattalionYamlConfig {
    #[serde(rename = "formation")]
    Formation(FormationConfig),

    #[serde(rename = "phalanx")]
    Phalanx(PhalanxConfig),

    #[serde(rename = "campaign")]
    Campaign(CampaignConfig),

    #[serde(rename = "chain-of-command")]
    ChainOfCommand(ChainOfCommandConfig),

    #[serde(rename = "conclave")]
    Conclave(ConclaveConfig),

    #[serde(rename = "maneuver")]
    Maneuver(ManeuverConfig),
}

impl BattalionYamlConfig {
    /// Get the battalion type as a string
    pub fn battalion_type(&self) -> &str {
        match self {
            BattalionYamlConfig::Formation(_) => "formation",
            BattalionYamlConfig::Phalanx(_) => "phalanx",
            BattalionYamlConfig::Campaign(_) => "campaign",
            BattalionYamlConfig::ChainOfCommand(_) => "chain-of-command",
            BattalionYamlConfig::Conclave(_) => "conclave",
            BattalionYamlConfig::Maneuver(_) => "maneuver",
        }
    }

    /// Validate the battalion configuration
    pub fn validate(&self) -> Result<(), CliError> {
        match self {
            BattalionYamlConfig::Formation(config) => config.validate(),
            BattalionYamlConfig::Phalanx(config) => config.validate(),
            BattalionYamlConfig::Campaign(config) => config.validate(),
            BattalionYamlConfig::ChainOfCommand(config) => config.validate(),
            BattalionYamlConfig::Conclave(config) => config.validate(),
            BattalionYamlConfig::Maneuver(config) => config.validate(),
        }
    }
}

/// Formation configuration - Sequential execution
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FormationConfig {
    /// Name of the formation
    pub name: String,

    /// List of Paladins in execution order
    pub paladins: Vec<PaladinReference>,

    /// Whether to pass output from one Paladin to the next
    #[serde(default = "default_true")]
    pub pass_output_to_next: bool,
}

impl FormationConfig {
    fn validate(&self) -> Result<(), CliError> {
        if self.name.is_empty() {
            return Err(CliError::MissingRequiredField {
                field: "name".to_string(),
                message: "Campaign name is required".to_string(),
            });
        }

        if self.paladins.is_empty() {
            return Err(CliError::ValidationError {
                message: "Formation must have at least one Paladin".to_string(),
            });
        }

        Ok(())
    }
}

/// Phalanx configuration - Parallel execution
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PhalanxConfig {
    /// Name of the phalanx
    pub name: String,

    /// List of Paladins to execute in parallel
    pub paladins: Vec<PaladinReference>,

    /// Optional inputs for each Paladin (if different)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub inputs: Option<Vec<String>>,
}

impl PhalanxConfig {
    fn validate(&self) -> Result<(), CliError> {
        if self.name.is_empty() {
            return Err(CliError::MissingRequiredField {
                field: "name".to_string(),
                message: "Phalanx name is required".to_string(),
            });
        }

        if self.paladins.is_empty() {
            return Err(CliError::ValidationError {
                message: "Phalanx must have at least one Paladin".to_string(),
            });
        }

        // If inputs provided, must match paladin count
        if let Some(inputs) = &self.inputs
            && inputs.len() != self.paladins.len()
        {
            return Err(CliError::ValidationError {
                message: format!(
                    "Number of inputs ({}) must match number of Paladins ({})",
                    inputs.len(),
                    self.paladins.len()
                ),
            });
        }

        Ok(())
    }
}

/// Campaign configuration - DAG-based execution
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CampaignConfig {
    /// Name of the campaign
    pub name: String,

    /// Nodes in the DAG (Paladins)
    pub nodes: Vec<CampaignNode>,

    /// Edges defining dependencies
    pub edges: Vec<CampaignEdge>,

    /// Starting node ID
    pub start_node: String,
}

impl CampaignConfig {
    fn validate(&self) -> Result<(), CliError> {
        if self.name.is_empty() {
            return Err(CliError::MissingRequiredField {
                field: "name".to_string(),
                message: "Campaign name is required".to_string(),
            });
        }

        if self.nodes.is_empty() {
            return Err(CliError::ValidationError {
                message: "Campaign must have at least one node".to_string(),
            });
        }

        // Validate start_node exists
        if !self.nodes.iter().any(|n| n.id == self.start_node) {
            return Err(CliError::ValidationError {
                message: format!("Start node '{}' not found in nodes", self.start_node),
            });
        }

        // Validate all edge references exist
        for edge in &self.edges {
            if !self.nodes.iter().any(|n| n.id == edge.from) {
                return Err(CliError::ValidationError {
                    message: format!("Edge references non-existent node: {}", edge.from),
                });
            }
            if !self.nodes.iter().any(|n| n.id == edge.to) {
                return Err(CliError::ValidationError {
                    message: format!("Edge references non-existent node: {}", edge.to),
                });
            }
        }

        Ok(())
    }
}

/// Campaign node (Paladin in DAG)
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CampaignNode {
    /// Unique node ID
    pub id: String,

    /// Paladin reference
    pub paladin: PaladinReference,
}

/// Campaign edge (dependency between nodes)
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CampaignEdge {
    /// Source node ID
    pub from: String,

    /// Target node ID
    pub to: String,
}

/// Chain of Command configuration - Hierarchical delegation
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ChainOfCommandConfig {
    /// Name of the chain
    pub name: String,

    /// Commander Paladin (top level)
    pub commander: PaladinReference,

    /// Delegate Paladins
    pub delegates: Vec<PaladinReference>,
}

impl ChainOfCommandConfig {
    fn validate(&self) -> Result<(), CliError> {
        if self.name.is_empty() {
            return Err(CliError::MissingRequiredField {
                field: "name".to_string(),
                message: "Chain of Command name is required".to_string(),
            });
        }

        if self.delegates.is_empty() {
            return Err(CliError::ValidationError {
                message: "Chain of Command must have at least one delegate".to_string(),
            });
        }

        Ok(())
    }
}

/// Conclave configuration - Mixture of Agents pattern
///
/// Multiple expert Paladins analyze the task in parallel, then an aggregator
/// Paladin synthesizes their outputs into a comprehensive response.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ConclaveConfig {
    /// Name of the conclave
    pub name: String,

    /// Expert Paladins (minimum 2 required)
    pub experts: Vec<PaladinReference>,

    /// Aggregator Paladin for synthesis
    pub aggregator: PaladinReference,

    /// Timeout in seconds (optional, defaults to 300)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub timeout_seconds: Option<u64>,

    /// Number of retry attempts for failed experts (optional, defaults to 2)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub retry_attempts: Option<u32>,

    /// Custom synthesis prompt for aggregator (optional)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub synthesis_prompt: Option<String>,

    /// Include expert names in aggregator input (optional, defaults to true)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub include_expert_names: Option<bool>,

    /// Max tokens per expert output before truncation (optional)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub max_expert_output_tokens: Option<usize>,

    /// Observability level: minimal, standard, verbose (optional, defaults to standard)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub observability_level: Option<String>,
}

impl ConclaveConfig {
    fn validate(&self) -> Result<(), CliError> {
        if self.name.is_empty() {
            return Err(CliError::MissingRequiredField {
                field: "name".to_string(),
                message: "Conclave name is required".to_string(),
            });
        }

        if self.experts.len() < 2 {
            return Err(CliError::ValidationError {
                message: "Conclave requires at least 2 expert Paladins".to_string(),
            });
        }

        // Validate timeout if provided
        if let Some(timeout) = self.timeout_seconds
            && timeout == 0
        {
            return Err(CliError::ValidationError {
                message: "Timeout must be greater than 0".to_string(),
            });
        }

        // Validate observability level if provided
        if let Some(ref level) = self.observability_level {
            let valid_levels = ["minimal", "standard", "verbose"];
            if !valid_levels.contains(&level.as_str()) {
                return Err(CliError::InvalidFieldValue {
                    field: "observability_level".to_string(),
                    message: format!(
                        "must be one of: {}. Got: {}",
                        valid_levels.join(", "),
                        level
                    ),
                });
            }
        }

        Ok(())
    }
}

/// Maneuver configuration - Flow DSL-based orchestration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ManeuverConfig {
    /// Name of the maneuver
    pub name: String,

    /// Flow expression (e.g., "agent1 -> agent2", "(a, b) -> c")
    pub flow: String,

    /// List of available Paladins for the maneuver
    pub paladins: Vec<PaladinReference>,

    /// Optional visualization format (ascii or mermaid)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub visualize: Option<String>,
}

impl ManeuverConfig {
    fn validate(&self) -> Result<(), CliError> {
        if self.name.is_empty() {
            return Err(CliError::MissingRequiredField {
                field: "name".to_string(),
                message: "Maneuver name is required".to_string(),
            });
        }

        if self.flow.is_empty() {
            return Err(CliError::MissingRequiredField {
                field: "flow".to_string(),
                message: "Flow expression is required".to_string(),
            });
        }

        if self.paladins.is_empty() {
            return Err(CliError::ValidationError {
                message: "Maneuver must have at least one Paladin".to_string(),
            });
        }

        // Validate flow expression syntax
        use crate::core::platform::container::battalion::parser::FlowParser;
        FlowParser::parse(&self.flow).map_err(|e| CliError::ValidationError {
            message: format!("Invalid flow expression: {}", e),
        })?;

        Ok(())
    }
}

/// Reference to a Paladin (inline config or external file)
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum PaladinReference {
    /// Path to external Paladin config file
    File { file: String },

    /// Inline Paladin configuration
    Inline(Box<crate::application::cli::config::paladin_config::PaladinYamlConfig>),
}

fn default_true() -> bool {
    true
}

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

    #[test]
    fn test_formation_valid() {
        let config = FormationConfig {
            name: "test".to_string(),
            paladins: vec![PaladinReference::File {
                file: "paladin1.yaml".to_string(),
            }],
            pass_output_to_next: true,
        };

        assert!(config.validate().is_ok());
    }

    #[test]
    fn test_formation_empty_name() {
        let config = FormationConfig {
            name: "".to_string(),
            paladins: vec![PaladinReference::File {
                file: "paladin1.yaml".to_string(),
            }],
            pass_output_to_next: true,
        };

        assert!(matches!(
            config.validate(),
            Err(CliError::MissingRequiredField { .. })
        ));
    }

    #[test]
    fn test_phalanx_inputs_mismatch() {
        let config = PhalanxConfig {
            name: "test".to_string(),
            paladins: vec![
                PaladinReference::File {
                    file: "paladin1.yaml".to_string(),
                },
                PaladinReference::File {
                    file: "paladin2.yaml".to_string(),
                },
            ],
            inputs: Some(vec!["input1".to_string()]), // Only 1 input for 2 paladins
        };

        assert!(matches!(
            config.validate(),
            Err(CliError::ValidationError { .. })
        ));
    }
}