paladin-ai 0.4.3

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
//! Configuration file loading utilities

use crate::application::cli::config::battalion_config::BattalionYamlConfig;
use crate::application::cli::config::paladin_config::{
    ArsenalConfig, GarrisonConfig, PaladinYamlConfig, Validate,
};
use crate::application::cli::error::CliError;
use crate::application::services::arsenal::arsenal_execution_service::ArsenalExecutionService;
use crate::application::services::arsenal::arsenal_registry_service::ArsenalRegistryService;
use crate::core::platform::container::garrison::{
    EvictionStrategy, GarrisonConfig as CoreGarrisonConfig,
};
use crate::infrastructure::adapters::arsenal::mcp_protocol::MCPClient;
use crate::infrastructure::adapters::arsenal::mcp_sse_adapter::MCPSseAdapter;
use crate::infrastructure::adapters::arsenal::mcp_stdio_adapter::MCPStdioAdapter;
use crate::infrastructure::adapters::garrison::in_memory_garrison::InMemoryGarrison;
use crate::infrastructure::adapters::garrison::sqlite_garrison::SqliteGarrison;
use paladin_ports::output::arsenal_port::{ArsenalPort, ArsenalRegistry};
use paladin_ports::output::garrison_port::GarrisonPort;
use std::fs;
use std::path::Path;
use std::sync::Arc;

/// Load Paladin configuration from YAML file
///
/// # Arguments
/// * `path` - Path to the YAML configuration file
///
/// # Returns
/// * `Ok(PaladinYamlConfig)` - Loaded and validated configuration
/// * `Err(CliError)` - File not found, invalid YAML, or validation error
pub fn load_paladin_config(path: &Path) -> Result<PaladinYamlConfig, CliError> {
    // Check if file exists
    if !path.exists() {
        return Err(CliError::ConfigFileNotFound {
            path: path.to_path_buf(),
        });
    }

    // Read file contents
    let contents = fs::read_to_string(path).map_err(|e| CliError::IoError {
        message: format!("Failed to read config file: {}", path.display()),
        source: e,
    })?;

    // Parse YAML
    let config: PaladinYamlConfig =
        serde_yaml::from_str(&contents).map_err(|e| CliError::InvalidYaml {
            path: path.to_path_buf(),
            source: e,
        })?;

    // Validate configuration
    config.validate()?;

    Ok(config)
}

/// Load Battalion configuration from YAML file
///
/// # Arguments
/// * `path` - Path to the YAML configuration file
///
/// # Returns
/// * `Ok(BattalionYamlConfig)` - Loaded and validated configuration
/// * `Err(CliError)` - File not found, invalid YAML, or validation error
pub fn load_battalion_config(path: &Path) -> Result<BattalionYamlConfig, CliError> {
    // Check if file exists
    if !path.exists() {
        return Err(CliError::ConfigFileNotFound {
            path: path.to_path_buf(),
        });
    }

    // Read file contents
    let contents = fs::read_to_string(path).map_err(|e| CliError::IoError {
        message: format!("Failed to read config file: {}", path.display()),
        source: e,
    })?;

    // Parse YAML
    let config: BattalionYamlConfig =
        serde_yaml::from_str(&contents).map_err(|e| CliError::InvalidYaml {
            path: path.to_path_buf(),
            source: e,
        })?;

    // Validate configuration
    config.validate()?;

    Ok(config)
}

/// Instantiate a garrison adapter from YAML configuration
///
/// # Arguments
/// * `config` - Optional garrison configuration from YAML
/// * `paladin_name` - Name of the paladin (used as ID for SQLite garrison)
///
/// # Returns
/// * `Ok(Some(Arc<dyn GarrisonPort>))` - If garrison config is provided and valid
/// * `Ok(None)` - If no garrison config is provided
/// * `Err(CliError)` - If garrison instantiation fails
///
/// # Configuration Schema
///
/// ```yaml
/// garrison:
///   type: "in_memory"  # or "sqlite"
///   config:
///     max_entries: 100
///     path: "./garrison.db"  # Required for sqlite type
/// ```
pub async fn instantiate_garrison(
    config: &Option<GarrisonConfig>,
    paladin_name: &str,
) -> Result<Option<Arc<dyn GarrisonPort>>, CliError> {
    let Some(garrison_config) = config else {
        return Ok(None);
    };

    // Validate garrison type
    if garrison_config.garrison_type != "in_memory" && garrison_config.garrison_type != "sqlite" {
        return Err(CliError::GarrisonConfigError {
            message: format!(
                "garrison.type must be 'in_memory' or 'sqlite', got: '{}'",
                garrison_config.garrison_type
            ),
        });
    }

    // Extract configuration parameters with defaults
    let max_entries = garrison_config
        .config
        .as_ref()
        .and_then(|c| c.max_entries)
        .unwrap_or(100);

    // Create core garrison configuration
    let core_config = CoreGarrisonConfig {
        max_entries,
        max_tokens: Some(4000),
        eviction_strategy: EvictionStrategy::ImportanceBased,
        preserve_recent_count: 10,
    };

    // Instantiate the appropriate garrison type
    match garrison_config.garrison_type.as_str() {
        "in_memory" => {
            let garrison = InMemoryGarrison::new(core_config);
            Ok(Some(Arc::new(garrison) as Arc<dyn GarrisonPort>))
        }
        "sqlite" => {
            // Extract path from config
            let path = garrison_config
                .config
                .as_ref()
                .and_then(|c| c.path.as_ref())
                .ok_or_else(|| CliError::GarrisonConfigError {
                    message: "garrison.config.path is required for type: sqlite".to_string(),
                })?;

            // Validate path is writable (check parent directory exists)
            if let Some(parent) = Path::new(path).parent()
                && !parent.exists()
            {
                // Try to create parent directory
                std::fs::create_dir_all(parent).map_err(|e| CliError::GarrisonConfigError {
                    message: format!(
                        "garrison.config.path parent directory does not exist and could not be created: {} - {}",
                        parent.display(),
                        e
                    ),
                })?;
            }

            // Connect to SQLite garrison
            let garrison = SqliteGarrison::connect(path, core_config, paladin_name)
                .await
                .map_err(|e| CliError::GarrisonConfigError {
                    message: format!("Failed to connect to SQLite garrison at '{}': {}", path, e),
                })?;

            Ok(Some(Arc::new(garrison) as Arc<dyn GarrisonPort>))
        }
        _ => unreachable!("Garrison type already validated"),
    }
}

/// Instantiate an arsenal from YAML configuration
///
/// # Arguments
/// * `config` - Optional arsenal configuration from YAML
///
/// # Returns
/// * `Ok(Some(Arc<dyn ArsenalPort>))` - If arsenal config is provided and valid
/// * `Ok(None)` - If no arsenal config is provided
/// * `Err(CliError)` - If arsenal instantiation fails
///
/// # Configuration Schema
///
/// ```yaml
/// arsenal:
///   mcp_servers:
///     - name: "web_search"
///       type: "stdio"
///       command: "uvx"
///       args:
///         - "mcp-web-search"
///     - name: "api_service"
///       type: "sse"
///       endpoint: "http://localhost:8080/mcp"
/// ```
pub async fn instantiate_arsenal(
    config: &Option<ArsenalConfig>,
) -> Result<Option<Arc<dyn ArsenalPort>>, CliError> {
    let Some(arsenal_config) = config else {
        return Ok(None);
    };

    // Create registry service
    let registry = ArsenalRegistryService::new();

    // If no MCP servers configured, return empty arsenal
    if arsenal_config.mcp_servers.is_empty() {
        let service = ArsenalExecutionService::new(Arc::new(registry));
        return Ok(Some(Arc::new(service) as Arc<dyn ArsenalPort>));
    }

    // Process each MCP server
    for server_config in &arsenal_config.mcp_servers {
        // Validate server type
        if server_config.server_type != "stdio" && server_config.server_type != "sse" {
            return Err(CliError::ArsenalConfigError {
                message: format!(
                    "arsenal.mcp_servers[{}].type must be 'stdio' or 'sse', got: '{}'",
                    server_config.name, server_config.server_type
                ),
            });
        }

        match server_config.server_type.as_str() {
            "stdio" => {
                // Validate required fields for stdio
                let command =
                    server_config
                        .command
                        .as_ref()
                        .ok_or_else(|| CliError::ArsenalConfigError {
                            message: format!(
                                "arsenal.mcp_servers[{}].command is required for stdio type",
                                server_config.name
                            ),
                        })?;

                let args = server_config.args.clone().unwrap_or_default();

                // Create and connect STDIO adapter
                let mut adapter = MCPStdioAdapter::new(command, args);
                adapter
                    .connect()
                    .await
                    .map_err(|e| CliError::ArsenalConfigError {
                        message: format!(
                            "Failed to connect to STDIO MCP server '{}': {}",
                            server_config.name, e
                        ),
                    })?;

                // Create MCP client and discover tools
                let client = MCPClient::new(Box::new(adapter));
                let tools =
                    client
                        .discover_tools()
                        .await
                        .map_err(|e| CliError::ArsenalConfigError {
                            message: format!(
                                "Failed to discover tools from MCP server '{}': {}",
                                server_config.name, e
                            ),
                        })?;

                // Register all tools
                for tool in tools {
                    registry.register(tool).await;
                }
            }
            "sse" => {
                // Validate required fields for sse
                let endpoint = server_config.endpoint.as_ref().ok_or_else(|| {
                    CliError::ArsenalConfigError {
                        message: format!(
                            "arsenal.mcp_servers[{}].endpoint is required for sse type",
                            server_config.name
                        ),
                    }
                })?;

                // Validate URL format
                if !endpoint.starts_with("http://") && !endpoint.starts_with("https://") {
                    return Err(CliError::ArsenalConfigError {
                        message: format!(
                            "arsenal.mcp_servers[{}].endpoint must start with 'http://' or 'https://', got: '{}'",
                            server_config.name, endpoint
                        ),
                    });
                }

                // Create and connect SSE adapter
                let mut adapter = MCPSseAdapter::new(endpoint);
                adapter
                    .connect()
                    .await
                    .map_err(|e| CliError::ArsenalConfigError {
                        message: format!(
                            "Failed to connect to SSE MCP server '{}': {}",
                            server_config.name, e
                        ),
                    })?;

                // Create MCP client and discover tools
                let client = MCPClient::new(Box::new(adapter));
                let tools =
                    client
                        .discover_tools()
                        .await
                        .map_err(|e| CliError::ArsenalConfigError {
                            message: format!(
                                "Failed to discover tools from MCP server '{}': {}",
                                server_config.name, e
                            ),
                        })?;

                // Register all tools
                for tool in tools {
                    registry.register(tool).await;
                }
            }
            _ => unreachable!("Server type already validated"),
        }
    }

    // Wrap registry in execution service
    let service = ArsenalExecutionService::new(Arc::new(registry));
    Ok(Some(Arc::new(service) as Arc<dyn ArsenalPort>))
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::io::Write;
    use tempfile::NamedTempFile;

    #[test]
    fn test_load_valid_paladin_config() {
        let yaml = r#"
name: test-paladin
system_prompt: "You are a helpful assistant"
model: gpt-4
temperature: 0.7
max_loops: 3
timeout_seconds: 300
stop_words: []
provider:
  type: openai
"#;

        let mut file = NamedTempFile::new().unwrap();
        file.write_all(yaml.as_bytes()).unwrap();
        file.flush().unwrap();

        let config = load_paladin_config(file.path()).unwrap();
        assert_eq!(config.name, "test-paladin");
        assert_eq!(config.model, "gpt-4");
        assert_eq!(config.provider.provider_type, "openai");
    }

    #[test]
    fn test_load_paladin_config_file_not_found() {
        let result = load_paladin_config(Path::new("/nonexistent/file.yaml"));
        assert!(matches!(result, Err(CliError::ConfigFileNotFound { .. })));
    }

    #[test]
    fn test_load_paladin_config_invalid_yaml() {
        let yaml = r#"
name: test
invalid yaml syntax: [unclosed
"#;

        let mut file = NamedTempFile::new().unwrap();
        file.write_all(yaml.as_bytes()).unwrap();
        file.flush().unwrap();

        let result = load_paladin_config(file.path());
        assert!(matches!(result, Err(CliError::InvalidYaml { .. })));
    }

    #[test]
    fn test_load_paladin_config_missing_required_field() {
        let yaml = r#"
name: test
# Missing system_prompt
model: gpt-4
provider:
  type: openai
"#;

        let mut file = NamedTempFile::new().unwrap();
        file.write_all(yaml.as_bytes()).unwrap();
        file.flush().unwrap();

        let result = load_paladin_config(file.path());
        // Serde will fail on missing required field during deserialization
        assert!(matches!(result, Err(CliError::InvalidYaml { .. })));
    }

    #[test]
    fn test_load_paladin_config_validation_error() {
        // Test validation errors (empty fields that pass deserialization)
        let yaml = r#"
name: ""
system_prompt: "test"
model: gpt-4
provider:
  type: openai
"#;

        let mut file = NamedTempFile::new().unwrap();
        file.write_all(yaml.as_bytes()).unwrap();
        file.flush().unwrap();

        let result = load_paladin_config(file.path());
        assert!(matches!(result, Err(CliError::MissingRequiredField { .. })));
    }

    #[test]
    fn test_load_valid_formation_config() {
        let yaml = r#"
type: formation
name: test-formation
pass_output_to_next: true
paladins:
  - file: paladin1.yaml
  - file: paladin2.yaml
"#;

        let mut file = NamedTempFile::new().unwrap();
        file.write_all(yaml.as_bytes()).unwrap();
        file.flush().unwrap();

        let config = load_battalion_config(file.path()).unwrap();
        assert_eq!(config.battalion_type(), "formation");
    }

    #[test]
    fn test_load_paladin_config_with_vision_fields() {
        use std::io::Write;

        // Create temporary image and document files
        let mut img_file = NamedTempFile::new().unwrap();
        img_file.write_all(b"fake image").unwrap();
        let img_path_temp = img_file.path().to_str().unwrap().to_string();
        let img_path = format!("{}.png", img_path_temp);
        std::fs::copy(&img_path_temp, &img_path).unwrap();

        let mut doc_file = NamedTempFile::new().unwrap();
        doc_file.write_all(b"fake pdf").unwrap();
        let doc_path_temp = doc_file.path().to_str().unwrap().to_string();
        let doc_path = format!("{}.pdf", doc_path_temp);
        std::fs::copy(&doc_path_temp, &doc_path).unwrap();

        let yaml = format!(
            r#"
name: vision-paladin
system_prompt: "You are a vision-capable assistant"
model: gpt-4
temperature: 0.7
max_loops: 3
timeout_seconds: 300
vision_enabled: true
images:
  - {}
documents:
  - {}
provider:
  type: openai
"#,
            img_path, doc_path
        );

        let mut file = NamedTempFile::new().unwrap();
        file.write_all(yaml.as_bytes()).unwrap();
        file.flush().unwrap();

        let config = load_paladin_config(file.path()).unwrap();
        assert_eq!(config.name, "vision-paladin");
        assert!(config.vision_enabled);
        assert_eq!(config.images.len(), 1);
        assert_eq!(config.documents.len(), 1);

        // Cleanup
        std::fs::remove_file(&img_path).ok();
        std::fs::remove_file(&doc_path).ok();
    }

    #[test]
    fn test_load_paladin_config_vision_enabled_without_files() {
        let yaml = r#"
name: test-paladin
system_prompt: "You are a helpful assistant"
model: gpt-4
temperature: 0.7
max_loops: 3
timeout_seconds: 300
vision_enabled: true
images: []
documents: []
provider:
  type: openai
"#;

        let mut file = NamedTempFile::new().unwrap();
        file.write_all(yaml.as_bytes()).unwrap();
        file.flush().unwrap();

        let result = load_paladin_config(file.path());
        // Should fail validation: vision_enabled true but no files
        assert!(matches!(
            result,
            Err(CliError::InvalidFieldValue { field, .. }) if field == "vision_enabled"
        ));
    }
}