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
//! CliError implementation - conversions, formatting, and helper methods

use super::error::CliError;

impl CliError {
    /// Create a configuration error
    pub fn configuration(msg: impl Into<String>) -> Self {
        Self::ValidationError {
            message: msg.into(),
        }
    }

    /// Create a network error
    pub fn network(msg: impl Into<String>) -> Self {
        Self::NetworkError(msg.into())
    }

    /// Create a validation error
    pub fn validation(msg: impl Into<String>) -> Self {
        Self::ValidationError {
            message: msg.into(),
        }
    }

    /// Create a user input error
    pub fn user_input(msg: impl Into<String>) -> Self {
        Self::UserInputError(msg.into())
    }

    /// Create an LLM error
    pub fn llm(msg: impl Into<String>) -> Self {
        Self::LlmError {
            message: msg.into(),
        }
    }

    /// Create an execution error
    pub fn execution(msg: impl Into<String>) -> Self {
        Self::ExecutionError {
            message: msg.into(),
        }
    }

    /// Create a missing field error
    pub fn missing_field(field: impl Into<String>) -> Self {
        Self::MissingRequiredField {
            field: field.into(),
            message: String::new(),
        }
    }

    /// Create an invalid argument error
    pub fn invalid_argument(msg: impl Into<String>) -> Self {
        Self::InvalidFieldValue {
            field: "argument".into(),
            message: msg.into(),
        }
    }

    /// Format error with actionable guidance
    pub fn format_detailed(&self) -> String {
        match self {
            CliError::ConfigFileNotFound { path } => {
                format!(
                    "\n\x1b[31mError:\x1b[0m Configuration file not found\n\n\
                     \x1b[33mDetails:\x1b[0m The configuration file '{}' does not exist.\n\n\
                     \x1b[32mSuggestion:\x1b[0m Check the file path and ensure the file exists. \
                     You can create a new configuration with:\n\n\
                     \x1b[36mExample:\x1b[0m\n  paladin agent new -n my-agent -o config.yaml\n",
                    path.display()
                )
            }
            CliError::InvalidYaml { path, source } => {
                format!(
                    "\n\x1b[31mError:\x1b[0m Invalid YAML syntax\n\n\
                     \x1b[33mDetails:\x1b[0m The configuration file '{}' contains invalid YAML:\n  {}\n\n\
                     \x1b[32mSuggestion:\x1b[0m Check the YAML syntax. Common issues include:\n\
                     • Missing or extra colons\n\
                     • Incorrect indentation (use spaces, not tabs)\n\
                     • Unquoted special characters\n\
                     • Missing closing brackets or quotes\n\n\
                     \x1b[36mExample:\x1b[0m Correct YAML structure:\n\
                     name: my_agent\n\
                     provider:\n\
                       type: openai\n\
                       model: gpt-4\n",
                    path.display(),
                    source
                )
            }
            CliError::ValidationError { message } => {
                format!(
                    "\n\x1b[31mError:\x1b[0m Configuration validation failed\n\n\
                     \x1b[33mDetails:\x1b[0m {}\n\n\
                     \x1b[32mSuggestion:\x1b[0m Review the configuration file and ensure all fields meet the requirements.\n",
                    message
                )
            }
            CliError::MissingRequiredField { field, message } => {
                format!(
                    "\n\x1b[31mError:\x1b[0m Missing required field\n\n\
                     \x1b[33mDetails:\x1b[0m The configuration is missing the required field '{}'.\n  {}\n\n\
                     \x1b[32mSuggestion:\x1b[0m Add the field to your configuration file.\n\n\
                     \x1b[36mExample:\x1b[0m\n  {}: <value>\n",
                    field, message, field
                )
            }
            CliError::InvalidFieldValue { field, message } => {
                format!(
                    "\n\x1b[31mError:\x1b[0m Invalid field value\n\n\
                     \x1b[33mDetails:\x1b[0m The field '{}' has an invalid value:\n  {}\n\n\
                     \x1b[32mSuggestion:\x1b[0m Check the field value and ensure it meets the requirements.\n",
                    field, message
                )
            }
            CliError::MissingApiKey { provider, env_var } => {
                format!(
                    "\n\x1b[31mError:\x1b[0m Missing API key\n\n\
                     \x1b[33mDetails:\x1b[0m The environment variable '{}' is not set for provider '{}'.\n\n\
                     \x1b[32mSuggestion:\x1b[0m Set the environment variable:\n\n\
                     \x1b[36mExample:\x1b[0m\n  export {}=<your-api-key>\n  # Or add to your ~/.bashrc or ~/.zshrc:\n  echo 'export {}=<your-api-key>' >> ~/.bashrc\n",
                    env_var, provider, env_var, env_var
                )
            }
            CliError::FileAlreadyExists { path } => {
                format!(
                    "\n\x1b[31mError:\x1b[0m File already exists\n\n\
                     \x1b[33mDetails:\x1b[0m The file '{}' already exists.\n\n\
                     \x1b[32mSuggestion:\x1b[0m Choose a different output path or delete the existing file:\n\n\
                     \x1b[36mExample:\x1b[0m\n  rm {}\n  # Or use a different output file:\n  --output ./new-config.yaml\n",
                    path.display(),
                    path.display()
                )
            }
            CliError::IoError { message, .. } => {
                format!(
                    "\n\x1b[31mError:\x1b[0m IO operation failed\n\n\
                     \x1b[33mDetails:\x1b[0m {}\n\n\
                     \x1b[32mSuggestion:\x1b[0m Check file permissions and disk space. Ensure the path is accessible.\n",
                    message
                )
            }
            CliError::Cancelled => "\n\x1b[33mInfo:\x1b[0m Operation cancelled by user\n\n\
                 The operation was interrupted. No changes were made.\n"
                .to_string(),
            CliError::LlmError { message } => {
                format!(
                    "\n\x1b[31mError:\x1b[0m LLM execution failed\n\n\
                     \x1b[33mDetails:\x1b[0m {}\n\n\
                     \x1b[32mSuggestion:\x1b[0m Check your API key, network connection, and LLM service status.\n\
                     • Verify the API key is valid and has sufficient credits\n\
                     • Check if the model name is correct\n\
                     • Ensure you have a stable internet connection\n",
                    message
                )
            }
            CliError::LlmProviderError { message } => {
                format!(
                    "\n\x1b[31mError:\x1b[0m LLM provider configuration failed\n\n\
                     \x1b[33mDetails:\x1b[0m {}\n\n\
                     \x1b[32mSuggestion:\x1b[0m Check your provider configuration in the config file.\n\n\
                     \x1b[36mExample:\x1b[0m Valid providers:\n\
                     provider:\n\
                       type: openai    # or: deepseek, anthropic\n\
                       model: gpt-4    # or appropriate model for provider\n",
                    message
                )
            }
            CliError::ExecutionError { message } => {
                format!(
                    "\n\x1b[31mError:\x1b[0m Paladin execution failed\n\n\
                     \x1b[33mDetails:\x1b[0m {}\n\n\
                     \x1b[32mSuggestion:\x1b[0m Review the Paladin configuration and input.\n\
                     • Check if the system prompt is clear and actionable\n\
                     • Verify that max_loops is set appropriately\n\
                     • Ensure the input is valid for the task\n",
                    message
                )
            }
            CliError::BattalionError { message } => {
                format!(
                    "\n\x1b[31mError:\x1b[0m Battalion execution failed\n\n\
                     \x1b[33mDetails:\x1b[0m {}\n\n\
                     \x1b[32mSuggestion:\x1b[0m Review the Battalion configuration.\n\
                     • Check that all Paladin configurations are valid\n\
                     • Verify the Battalion type matches the configuration\n\
                     • Ensure all required Paladins are configured\n",
                    message
                )
            }
            CliError::ToolError { message } => {
                format!(
                    "\n\x1b[31mError:\x1b[0m Tool execution failed\n\n\
                     \x1b[33mDetails:\x1b[0m {}\n\n\
                     \x1b[32mSuggestion:\x1b[0m Check the tool configuration and MCP server.\n\
                     • Verify the MCP server is running and accessible\n\
                     • Check that the tool parameters are correct\n\
                     • Ensure the tool supports the requested operation\n",
                    message
                )
            }
            CliError::McpConnectionError { message } => {
                format!(
                    "\n\x1b[31mError:\x1b[0m MCP connection failed\n\n\
                     \x1b[33mDetails:\x1b[0m {}\n\n\
                     \x1b[32mSuggestion:\x1b[0m Troubleshoot the MCP server connection.\n\
                     • Verify the command exists and is in your PATH\n\
                     • Check that the server implements the MCP protocol\n\
                     • Test the command manually in a terminal\n\
                     • Review server logs for error messages\n\n\
                     \x1b[36mExample:\x1b[0m Test MCP server:\n  paladin arsenal test --mcp-stdio \"python3 server.py\"\n",
                    message
                )
            }
            CliError::SerializationError { message } => {
                format!(
                    "\n\x1b[31mError:\x1b[0m Serialization failed\n\n\
                     \x1b[33mDetails:\x1b[0m {}\n\n\
                     \x1b[32mSuggestion:\x1b[0m Check that the data format is valid.\n",
                    message
                )
            }
            CliError::InvalidFilePath { path, message } => {
                format!(
                    "\n\x1b[31mError:\x1b[0m Invalid file path\n\n\
                     \x1b[33mDetails:\x1b[0m The file '{}' is invalid:\n  {}\n\n\
                     \x1b[32mSuggestion:\x1b[0m Check that the file exists and the path is correct.\n\n\
                     \x1b[36mExample:\x1b[0m\n  ls -la {}\n",
                    path, message, path
                )
            }
            CliError::UnsupportedFormat { format, supported } => {
                format!(
                    "\n\x1b[31mError:\x1b[0m Unsupported file format\n\n\
                     \x1b[33mDetails:\x1b[0m The format '{}' is not supported.\n\n\
                     \x1b[32mSupported formats:\x1b[0m {}\n\n\
                     \x1b[32mSuggestion:\x1b[0m Convert the file to a supported format or use a different file.\n",
                    format, supported
                )
            }
            CliError::FileReadError { path, message } => {
                format!(
                    "\n\x1b[31mError:\x1b[0m Failed to read file\n\n\
                     \x1b[33mDetails:\x1b[0m Could not read '{}':\n  {}\n\n\
                     \x1b[32mSuggestion:\x1b[0m Check file permissions and ensure the file is not corrupted.\n\n\
                     \x1b[36mExample:\x1b[0m\n  chmod 644 {}\n  file {}\n",
                    path, message, path, path
                )
            }
            CliError::VisionProcessingError { message } => {
                format!(
                    "\n\x1b[31mError:\x1b[0m Vision processing failed\n\n\
                     \x1b[33mDetails:\x1b[0m {}\n\n\
                     \x1b[32mSuggestion:\x1b[0m Check that:\n\
                     • The image file is valid and not corrupted\n\
                     • The image format is supported (png, jpg, jpeg, gif, webp)\n\
                     • The LLM model supports vision capabilities\n\
                     • Vision mode is enabled in the configuration\n",
                    message
                )
            }
            CliError::DocumentProcessingError { message } => {
                format!(
                    "\n\x1b[31mError:\x1b[0m Document processing failed\n\n\
                     \x1b[33mDetails:\x1b[0m {}\n\n\
                     \x1b[32mSuggestion:\x1b[0m Check that:\n\
                     • The document file is valid and not corrupted\n\
                     • The document format is supported (pdf, txt, md)\n\
                     • The file is not encrypted or password-protected\n\
                     • The file has appropriate read permissions\n",
                    message
                )
            }
            CliError::NetworkError(message) => {
                format!(
                    "\n\x1b[31mError:\x1b[0m Network error\n\n\
                     \x1b[33mDetails:\x1b[0m {}\n\n\
                     \x1b[32mSuggestion:\x1b[0m Check your internet connection and try again.\n",
                    message
                )
            }
            CliError::UserInputError(message) => {
                format!(
                    "\n\x1b[31mError:\x1b[0m Invalid user input\n\n\
                     \x1b[33mDetails:\x1b[0m {}\n\n\
                     \x1b[32mSuggestion:\x1b[0m Check the input format and try again.\n",
                    message
                )
            }
            CliError::JsonError(message) => {
                format!(
                    "\n\x1b[31mError:\x1b[0m JSON parsing error\n\n\
                     \x1b[33mDetails:\x1b[0m {}\n\n\
                     \x1b[32mSuggestion:\x1b[0m Check the JSON format and ensure it's valid.\n",
                    message
                )
            }
            CliError::GarrisonConfigError { message } => {
                format!(
                    "\n\x1b[31mError:\x1b[0m Garrison configuration error\n\n\
                     \x1b[33mDetails:\x1b[0m {}\n\n\
                     \x1b[32mSuggestion:\x1b[0m Check your garrison configuration in the YAML file.\n",
                    message
                )
            }
            CliError::ArsenalConfigError { message } => {
                format!(
                    "\n\x1b[31mError:\x1b[0m Arsenal configuration error\n\n\
                     \x1b[33mDetails:\x1b[0m {}\n\n\
                     \x1b[32mSuggestion:\x1b[0m Check your arsenal/MCP server configuration in the YAML file.\n",
                    message
                )
            }
            CliError::Other(message) => {
                format!("\n\x1b[31mError:\x1b[0m {}\n", message)
            }
        }
    }

    /// Get the appropriate exit code for this error
    ///
    /// Exit codes:
    /// - 0: Success
    /// - 1: User errors (config, validation, missing args)
    /// - 2: Runtime errors (LLM, execution, tools)
    /// - 130: SIGINT (Ctrl+C)
    pub fn exit_code(&self) -> i32 {
        match self {
            // User errors: exit code 1
            CliError::ConfigFileNotFound { .. }
            | CliError::InvalidYaml { .. }
            | CliError::ValidationError { .. }
            | CliError::MissingRequiredField { .. }
            | CliError::InvalidFieldValue { .. }
            | CliError::MissingApiKey { .. }
            | CliError::FileAlreadyExists { .. }
            | CliError::InvalidFilePath { .. }
            | CliError::UnsupportedFormat { .. }
            | CliError::UserInputError(_) => 1,

            // Cancellation: exit code 130 (SIGINT)
            CliError::Cancelled => 130,

            // Runtime errors: exit code 2
            CliError::IoError { .. }
            | CliError::LlmError { .. }
            | CliError::LlmProviderError { .. }
            | CliError::ExecutionError { .. }
            | CliError::BattalionError { .. }
            | CliError::ToolError { .. }
            | CliError::McpConnectionError { .. }
            | CliError::SerializationError { .. }
            | CliError::FileReadError { .. }
            | CliError::VisionProcessingError { .. }
            | CliError::DocumentProcessingError { .. }
            | CliError::GarrisonConfigError { .. }
            | CliError::ArsenalConfigError { .. }
            | CliError::NetworkError(_)
            | CliError::JsonError(_)
            | CliError::Other(_) => 2,
        }
    }
}

// Note: Old CLI error conversion removed as src/cli/ has been deleted
// All code now uses unified CliError from application::cli::error

impl From<std::io::Error> for CliError {
    fn from(error: std::io::Error) -> Self {
        CliError::IoError {
            message: error.to_string(),
            source: error,
        }
    }
}

impl From<serde_yaml::Error> for CliError {
    fn from(error: serde_yaml::Error) -> Self {
        CliError::Other(format!("YAML error: {}", error))
    }
}

impl From<serde_json::Error> for CliError {
    fn from(error: serde_json::Error) -> Self {
        CliError::JsonError(error.to_string())
    }
}

impl From<crate::application::services::paladin::error::PaladinError> for CliError {
    fn from(error: crate::application::services::paladin::error::PaladinError) -> Self {
        CliError::ExecutionError {
            message: error.to_string(),
        }
    }
}

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

    #[test]
    fn test_config_file_not_found_formatting() {
        let error = CliError::ConfigFileNotFound {
            path: PathBuf::from("config.yaml"),
        };
        let formatted = error.format_detailed();

        assert!(formatted.contains("Configuration file not found"));
        assert!(formatted.contains("config.yaml"));
        assert!(formatted.contains("paladin agent new"));
    }

    #[test]
    fn test_validation_error_formatting() {
        let error = CliError::ValidationError {
            message: "Temperature must be between 0 and 1".to_string(),
        };
        let formatted = error.format_detailed();

        assert!(formatted.contains("Configuration validation failed"));
        assert!(formatted.contains("Temperature must be between 0 and 1"));
    }

    #[test]
    fn test_helper_methods() {
        let _ = CliError::configuration("test");
        let _ = CliError::network("test");
        let _ = CliError::validation("test");
        let _ = CliError::user_input("test");
        let _ = CliError::llm("test");
        let _ = CliError::execution("test");
        let _ = CliError::missing_field("test");
        let _ = CliError::invalid_argument("test");
    }

    #[test]
    fn test_exit_code_user_errors() {
        assert_eq!(
            CliError::ConfigFileNotFound {
                path: PathBuf::from("test")
            }
            .exit_code(),
            1
        );
        assert_eq!(
            CliError::ValidationError {
                message: "test".into()
            }
            .exit_code(),
            1
        );
    }

    #[test]
    fn test_exit_code_runtime_errors() {
        assert_eq!(
            CliError::LlmError {
                message: "test".into()
            }
            .exit_code(),
            2
        );
        assert_eq!(
            CliError::ExecutionError {
                message: "test".into()
            }
            .exit_code(),
            2
        );
    }

    #[test]
    fn test_exit_code_cancelled() {
        assert_eq!(CliError::Cancelled.exit_code(), 130);
    }

    #[test]
    fn test_io_error_conversion() {
        let io_err = std::io::Error::new(std::io::ErrorKind::NotFound, "file not found");
        let cli_err: CliError = io_err.into();
        assert!(matches!(cli_err, CliError::IoError { .. }));
    }

    #[test]
    fn test_yaml_error_conversion() {
        let yaml_err = serde_yaml::from_str::<serde_yaml::Value>("bad: [yaml").unwrap_err();
        let cli_err: CliError = yaml_err.into();
        assert!(matches!(cli_err, CliError::Other(_)));
    }

    #[test]
    fn test_json_error_conversion() {
        let json_err = serde_json::from_str::<serde_json::Value>("{bad json").unwrap_err();
        let cli_err: CliError = json_err.into();
        assert!(matches!(cli_err, CliError::JsonError(_)));
    }
}