voirs-cli 0.1.0-rc.1

Command-line interface for VoiRS speech synthesis
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
//! Workflow Definition Types
//!
//! This module defines the data structures for workflow definitions,
//! including steps, conditions, retries, and metadata.

use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::path::Path;

use crate::error::CliError;

type Result<T> = std::result::Result<T, CliError>;

/// Workflow definition
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Workflow {
    /// Workflow metadata
    pub metadata: WorkflowMetadata,
    /// Global variables
    #[serde(default)]
    pub variables: HashMap<String, Variable>,
    /// Workflow steps
    pub steps: Vec<Step>,
    /// Workflow-level configuration
    #[serde(default)]
    pub config: WorkflowConfig,
}

impl Workflow {
    /// Create a new workflow
    pub fn new(name: &str, version: &str, description: &str) -> Self {
        Self {
            metadata: WorkflowMetadata {
                name: name.to_string(),
                version: version.to_string(),
                description: description.to_string(),
                author: None,
                tags: Vec::new(),
            },
            variables: HashMap::new(),
            steps: Vec::new(),
            config: WorkflowConfig::default(),
        }
    }

    /// Load workflow from YAML file
    pub async fn load_from_file(path: &Path) -> Result<Self> {
        let content = tokio::fs::read_to_string(path).await?;

        if path.extension().is_some_and(|ext| ext == "json") {
            Ok(serde_json::from_str(&content)?)
        } else {
            // Assume YAML for .yaml, .yml, or no extension
            Ok(serde_yaml::from_str(&content).map_err(|e| {
                CliError::SerializationError(format!("Failed to parse YAML: {}", e))
            })?)
        }
    }

    /// Save workflow to file
    pub async fn save_to_file(&self, path: &Path) -> Result<()> {
        let content = if path.extension().is_some_and(|ext| ext == "json") {
            serde_json::to_string_pretty(self)?
        } else {
            serde_yaml::to_string(self).map_err(|e| {
                CliError::SerializationError(format!("Failed to serialize to YAML: {}", e))
            })?
        };

        tokio::fs::write(path, content).await?;
        Ok(())
    }

    /// Add a step to the workflow
    pub fn add_step(&mut self, step: Step) {
        self.steps.push(step);
    }

    /// Add a variable
    pub fn add_variable(&mut self, name: String, value: Variable) {
        self.variables.insert(name, value);
    }

    /// Get step by name
    pub fn get_step(&self, name: &str) -> Option<&Step> {
        self.steps.iter().find(|s| s.name == name)
    }

    /// Validate workflow structure
    pub fn validate(&self) -> Result<()> {
        // Check for duplicate step names
        let mut step_names = std::collections::HashSet::new();
        for step in &self.steps {
            if !step_names.insert(&step.name) {
                return Err(CliError::Workflow(format!(
                    "Duplicate step name: {}",
                    step.name
                )));
            }
        }

        // Check dependencies exist
        for step in &self.steps {
            for dep in &step.depends_on {
                if !self.steps.iter().any(|s| s.name == dep.step_name) {
                    return Err(CliError::Workflow(format!(
                        "Step '{}' depends on non-existent step '{}'",
                        step.name, dep.step_name
                    )));
                }
            }
        }

        Ok(())
    }
}

/// Workflow metadata
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WorkflowMetadata {
    /// Workflow name
    pub name: String,
    /// Version string
    pub version: String,
    /// Human-readable description
    pub description: String,
    /// Optional author information
    #[serde(skip_serializing_if = "Option::is_none")]
    pub author: Option<String>,
    /// Tags for categorization
    #[serde(default)]
    pub tags: Vec<String>,
}

/// Workflow configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WorkflowConfig {
    /// Maximum parallel steps
    #[serde(default = "default_max_parallel")]
    pub max_parallel: usize,
    /// Timeout in seconds (0 = no timeout)
    #[serde(default)]
    pub timeout_seconds: u64,
    /// Whether to continue on error
    #[serde(default)]
    pub continue_on_error: bool,
    /// Whether to save state for resumption
    #[serde(default = "default_true")]
    pub save_state: bool,
}

fn default_max_parallel() -> usize {
    4
}

fn default_true() -> bool {
    true
}

impl Default for WorkflowConfig {
    fn default() -> Self {
        Self {
            max_parallel: default_max_parallel(),
            timeout_seconds: 0,
            continue_on_error: false,
            save_state: true,
        }
    }
}

/// Workflow step definition
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Step {
    /// Unique step name
    pub name: String,
    /// Step type
    #[serde(rename = "type")]
    pub step_type: StepType,
    /// Optional description
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
    /// Step parameters
    #[serde(default)]
    pub parameters: HashMap<String, serde_json::Value>,
    /// Execution condition
    #[serde(skip_serializing_if = "Option::is_none")]
    pub condition: Option<Condition>,
    /// Dependencies on other steps
    #[serde(default)]
    pub depends_on: Vec<StepDependency>,
    /// Retry strategy
    #[serde(skip_serializing_if = "Option::is_none")]
    pub retry: Option<RetryStrategy>,
    /// For-each loop variable
    #[serde(skip_serializing_if = "Option::is_none")]
    pub for_each: Option<String>,
    /// Whether to run in parallel with other steps
    #[serde(default)]
    pub parallel: bool,
}

/// Step type enumeration
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "lowercase")]
pub enum StepType {
    /// Synthesis step
    Synthesize,
    /// Quality validation step
    Validate,
    /// File operation step
    FileOp,
    /// Command execution step
    Command,
    /// Script execution step
    Script,
    /// Conditional branch step
    Branch,
    /// Loop step
    Loop,
    /// Sub-workflow invocation
    Workflow,
    /// Wait/delay step
    Wait,
    /// Notification step
    Notify,
}

/// Step dependency
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StepDependency {
    /// Name of the step to depend on
    pub step_name: String,
    /// Whether the dependency must succeed
    #[serde(default = "default_true")]
    pub must_succeed: bool,
}

/// Condition definition
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Condition {
    /// Left operand (variable or value)
    pub left: String,
    /// Operator
    pub operator: ConditionOperator,
    /// Right operand (variable or value)
    pub right: String,
}

impl Condition {
    /// Create a new condition
    pub fn new(left: String, operator: ConditionOperator, right: String) -> Self {
        Self {
            left,
            operator,
            right,
        }
    }

    /// Evaluate condition with variable substitution
    pub fn evaluate(&self, variables: &HashMap<String, serde_json::Value>) -> bool {
        let left_val = self.resolve_value(&self.left, variables);
        let right_val = self.resolve_value(&self.right, variables);

        match self.operator {
            ConditionOperator::Equals => left_val == right_val,
            ConditionOperator::NotEquals => left_val != right_val,
            ConditionOperator::GreaterThan => {
                self.compare_numeric(&left_val, &right_val, |a, b| a > b)
            }
            ConditionOperator::LessThan => {
                self.compare_numeric(&left_val, &right_val, |a, b| a < b)
            }
            ConditionOperator::GreaterOrEqual => {
                self.compare_numeric(&left_val, &right_val, |a, b| a >= b)
            }
            ConditionOperator::LessOrEqual => {
                self.compare_numeric(&left_val, &right_val, |a, b| a <= b)
            }
            ConditionOperator::Contains => {
                if let (Some(left_str), Some(right_str)) = (left_val.as_str(), right_val.as_str()) {
                    left_str.contains(right_str)
                } else {
                    false
                }
            }
            ConditionOperator::Matches => {
                // Regex match (simplified for now)
                if let (Some(left_str), Some(right_str)) = (left_val.as_str(), right_val.as_str()) {
                    regex::Regex::new(right_str)
                        .map(|re| re.is_match(left_str))
                        .unwrap_or(false)
                } else {
                    false
                }
            }
        }
    }

    fn resolve_value(
        &self,
        value: &str,
        variables: &HashMap<String, serde_json::Value>,
    ) -> serde_json::Value {
        // Check if it's a variable reference ${var}
        if let Some(var_name) = value.strip_prefix("${").and_then(|s| s.strip_suffix('}')) {
            variables
                .get(var_name)
                .cloned()
                .unwrap_or(serde_json::Value::Null)
        } else {
            // Try to parse as JSON value
            serde_json::from_str(value)
                .unwrap_or_else(|_| serde_json::Value::String(value.to_string()))
        }
    }

    fn compare_numeric<F>(&self, left: &serde_json::Value, right: &serde_json::Value, op: F) -> bool
    where
        F: Fn(f64, f64) -> bool,
    {
        match (left.as_f64(), right.as_f64()) {
            (Some(l), Some(r)) => op(l, r),
            _ => false,
        }
    }
}

/// Condition operators
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "lowercase")]
pub enum ConditionOperator {
    /// Equality
    #[serde(rename = "==")]
    Equals,
    /// Inequality
    #[serde(rename = "!=")]
    NotEquals,
    /// Greater than
    #[serde(rename = ">")]
    GreaterThan,
    /// Less than
    #[serde(rename = "<")]
    LessThan,
    /// Greater or equal
    #[serde(rename = ">=")]
    GreaterOrEqual,
    /// Less or equal
    #[serde(rename = "<=")]
    LessOrEqual,
    /// String contains
    Contains,
    /// Regex match
    Matches,
}

/// Retry strategy
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RetryStrategy {
    /// Maximum retry attempts
    pub max_attempts: usize,
    /// Backoff strategy
    pub backoff: BackoffType,
    /// Initial delay in milliseconds
    #[serde(default = "default_initial_delay")]
    pub initial_delay_ms: u64,
    /// Maximum delay in milliseconds
    #[serde(default = "default_max_delay")]
    pub max_delay_ms: u64,
    /// Multiplier for exponential backoff
    #[serde(default = "default_backoff_multiplier")]
    pub backoff_multiplier: f64,
}

fn default_initial_delay() -> u64 {
    1000
}

fn default_max_delay() -> u64 {
    60_000
}

fn default_backoff_multiplier() -> f64 {
    2.0
}

impl Default for RetryStrategy {
    fn default() -> Self {
        Self {
            max_attempts: 3,
            backoff: BackoffType::Exponential,
            initial_delay_ms: default_initial_delay(),
            max_delay_ms: default_max_delay(),
            backoff_multiplier: default_backoff_multiplier(),
        }
    }
}

/// Backoff types
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "lowercase")]
pub enum BackoffType {
    /// Fixed delay
    Fixed,
    /// Linear increase
    Linear,
    /// Exponential backoff
    Exponential,
    /// Exponential with jitter
    ExponentialJitter,
}

/// Variable value types
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum Variable {
    /// String value
    String(String),
    /// Numeric value
    Number(f64),
    /// Boolean value
    Boolean(bool),
    /// Array of values
    Array(Vec<serde_json::Value>),
    /// Object/map
    Object(HashMap<String, serde_json::Value>),
}

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

    #[test]
    fn test_workflow_creation() {
        let workflow = Workflow::new("test", "1.0", "Test workflow");
        assert_eq!(workflow.metadata.name, "test");
        assert_eq!(workflow.metadata.version, "1.0");
        assert_eq!(workflow.steps.len(), 0);
    }

    #[test]
    fn test_workflow_add_step() {
        let mut workflow = Workflow::new("test", "1.0", "Test workflow");

        let step = Step {
            name: "step1".to_string(),
            step_type: StepType::Synthesize,
            description: None,
            parameters: HashMap::new(),
            condition: None,
            depends_on: Vec::new(),
            retry: None,
            for_each: None,
            parallel: false,
        };

        workflow.add_step(step);
        assert_eq!(workflow.steps.len(), 1);
        assert_eq!(workflow.steps[0].name, "step1");
    }

    #[test]
    fn test_workflow_validation_duplicate_names() {
        let mut workflow = Workflow::new("test", "1.0", "Test workflow");

        let step1 = Step {
            name: "duplicate".to_string(),
            step_type: StepType::Synthesize,
            description: None,
            parameters: HashMap::new(),
            condition: None,
            depends_on: Vec::new(),
            retry: None,
            for_each: None,
            parallel: false,
        };

        let step2 = Step {
            name: "duplicate".to_string(),
            step_type: StepType::Validate,
            description: None,
            parameters: HashMap::new(),
            condition: None,
            depends_on: Vec::new(),
            retry: None,
            for_each: None,
            parallel: false,
        };

        workflow.add_step(step1);
        workflow.add_step(step2);

        assert!(workflow.validate().is_err());
    }

    #[test]
    fn test_condition_evaluation_equals() {
        let condition = Condition::new(
            "${status}".to_string(),
            ConditionOperator::Equals,
            "success".to_string(),
        );

        let mut variables = HashMap::new();
        variables.insert(
            "status".to_string(),
            serde_json::Value::String("success".to_string()),
        );

        assert!(condition.evaluate(&variables));
    }

    #[test]
    fn test_condition_evaluation_greater_than() {
        let condition = Condition::new(
            "${score}".to_string(),
            ConditionOperator::GreaterThan,
            "4.0".to_string(),
        );

        let mut variables = HashMap::new();
        variables.insert("score".to_string(), serde_json::json!(4.5));

        assert!(condition.evaluate(&variables));
    }

    #[test]
    fn test_condition_evaluation_contains() {
        let condition = Condition::new(
            "${output}".to_string(),
            ConditionOperator::Contains,
            "error".to_string(),
        );

        let mut variables = HashMap::new();
        variables.insert(
            "output".to_string(),
            serde_json::Value::String("An error occurred".to_string()),
        );

        assert!(condition.evaluate(&variables));
    }

    #[test]
    fn test_retry_strategy_defaults() {
        let retry = RetryStrategy::default();
        assert_eq!(retry.max_attempts, 3);
        assert_eq!(retry.backoff, BackoffType::Exponential);
        assert_eq!(retry.initial_delay_ms, 1000);
    }

    #[test]
    fn test_workflow_config_defaults() {
        let config = WorkflowConfig::default();
        assert_eq!(config.max_parallel, 4);
        assert_eq!(config.timeout_seconds, 0);
        assert!(!config.continue_on_error);
        assert!(config.save_state);
    }
}