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
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
//! Step Executor
//!
//! Executes individual workflow steps with retry logic and state management.

use super::{
    definition::{Step, StepType, Workflow},
    retry::RetryManager,
    state::WorkflowState,
    WorkflowStats,
};
use crate::error::CliError;

type Result<T> = std::result::Result<T, CliError>;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::time::Instant;

/// Execution context for a workflow
#[derive(Clone)]
pub struct ExecutionContext {
    /// The workflow being executed
    workflow: Workflow,
    /// Current variables
    variables: HashMap<String, serde_json::Value>,
    /// Completed steps and their results
    completed: HashMap<String, StepResult>,
    /// Skipped steps
    skipped: Vec<String>,
    /// Total retries performed
    retries: usize,
}

impl ExecutionContext {
    /// Create new execution context
    pub fn new(workflow: Workflow) -> Self {
        // Initialize variables from workflow definition
        let mut variables = HashMap::new();
        for (key, value) in &workflow.variables {
            let json_value = match value {
                super::definition::Variable::String(s) => serde_json::Value::String(s.clone()),
                super::definition::Variable::Number(n) => serde_json::json!(n),
                super::definition::Variable::Boolean(b) => serde_json::Value::Bool(*b),
                super::definition::Variable::Array(arr) => serde_json::Value::Array(arr.clone()),
                super::definition::Variable::Object(obj) => {
                    serde_json::Value::Object(serde_json::Map::from_iter(obj.clone()))
                }
            };
            variables.insert(key.clone(), json_value);
        }

        Self {
            workflow,
            variables,
            completed: HashMap::new(),
            skipped: Vec::new(),
            retries: 0,
        }
    }

    /// Get workflow reference
    pub fn workflow(&self) -> &Workflow {
        &self.workflow
    }

    /// Get current variables
    pub fn get_variables(&self) -> HashMap<String, serde_json::Value> {
        self.variables.clone()
    }

    /// Set a variable
    pub fn set_variable(&mut self, name: String, value: serde_json::Value) {
        self.variables.insert(name, value);
    }

    /// Record step completion
    pub fn complete_step(&mut self, name: &str, result: StepResult) {
        self.completed.insert(name.to_string(), result);
    }

    /// Record step skip
    pub fn skip_step(&mut self, name: &str, reason: &str) {
        self.skipped.push(name.to_string());
        tracing::info!("Skipping step '{}': {}", name, reason);
    }

    /// Get completed steps
    pub fn completed_steps(&self) -> &HashMap<String, StepResult> {
        &self.completed
    }

    /// Get skipped steps
    pub fn skipped_steps(&self) -> &[String] {
        &self.skipped
    }

    /// Increment retry counter
    pub fn increment_retries(&mut self) {
        self.retries += 1;
    }

    /// Get total retries
    pub fn total_retries(&self) -> usize {
        self.retries
    }

    /// Resume from saved state
    pub fn resume_from_state(&mut self, state: WorkflowState) {
        self.variables = state.variables;
        self.completed = state.completed_steps;
        self.skipped = state.skipped_steps;
        self.retries = state.total_retries;
    }

    /// Get current state
    pub fn get_state(&self) -> WorkflowState {
        WorkflowState {
            workflow_name: self.workflow.metadata.name.clone(),
            state: super::state::ExecutionState::Running,
            variables: self.variables.clone(),
            completed_steps: self.completed.clone(),
            skipped_steps: self.skipped.clone(),
            current_step: None,
            total_retries: self.retries,
            last_updated: chrono::Utc::now(),
        }
    }
}

/// Result of a step execution
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StepResult {
    /// Step name
    pub step_name: String,
    /// Success status
    pub success: bool,
    /// Result message
    pub message: String,
    /// Output data
    pub output: HashMap<String, serde_json::Value>,
    /// Execution duration in milliseconds
    pub duration_ms: u64,
    /// Number of retry attempts
    pub attempts: usize,
}

impl StepResult {
    /// Create success result
    pub fn success(step_name: String, message: String, duration_ms: u64) -> Self {
        Self {
            step_name,
            success: true,
            message,
            output: HashMap::new(),
            duration_ms,
            attempts: 1,
        }
    }

    /// Create failure result
    pub fn failure(step_name: String, message: String, duration_ms: u64) -> Self {
        Self {
            step_name,
            success: false,
            message,
            output: HashMap::new(),
            duration_ms,
            attempts: 1,
        }
    }

    /// Add output data
    pub fn with_output(mut self, key: String, value: serde_json::Value) -> Self {
        self.output.insert(key, value);
        self
    }

    /// Set attempt count
    pub fn with_attempts(mut self, attempts: usize) -> Self {
        self.attempts = attempts;
        self
    }
}

/// Step executor
pub struct StepExecutor {
    retry_manager: RetryManager,
}

impl StepExecutor {
    /// Create new step executor
    pub fn new() -> Self {
        Self {
            retry_manager: RetryManager::new(),
        }
    }

    /// Execute a step
    pub async fn execute_step(
        &self,
        step: &Step,
        context: &mut ExecutionContext,
    ) -> Result<StepResult> {
        let start_time = Instant::now();

        // Handle for-each loop
        if let Some(ref for_each_var) = step.for_each {
            return self.execute_for_each(step, for_each_var, context).await;
        }

        // Execute with retry if configured
        if let Some(ref retry_strategy) = step.retry {
            let mut attempts = 0;
            loop {
                attempts += 1;
                match self.execute_step_once(step, context).await {
                    Ok(result) => {
                        let duration = start_time.elapsed().as_millis() as u64;
                        context.complete_step(&step.name, result.clone().with_attempts(attempts));
                        return Ok(result.with_attempts(attempts));
                    }
                    Err(e) if attempts < retry_strategy.max_attempts => {
                        context.increment_retries();
                        let delay = self.retry_manager.calculate_delay(retry_strategy, attempts);
                        tokio::time::sleep(tokio::time::Duration::from_millis(delay)).await;
                        tracing::warn!(
                            "Step '{}' failed (attempt {}), retrying: {}",
                            step.name,
                            attempts,
                            e
                        );
                        continue;
                    }
                    Err(e) => {
                        let duration = start_time.elapsed().as_millis() as u64;
                        let result = StepResult::failure(
                            step.name.clone(),
                            format!("Error: {}", e),
                            duration,
                        )
                        .with_attempts(attempts);
                        context.complete_step(&step.name, result.clone());
                        return Ok(result);
                    }
                }
            }
        } else {
            let result = self.execute_step_once(step, context).await;
            let duration = start_time.elapsed().as_millis() as u64;

            match result {
                Ok(mut result) => {
                    result.duration_ms = duration;
                    context.complete_step(&step.name, result.clone());
                    Ok(result)
                }
                Err(e) => {
                    let result =
                        StepResult::failure(step.name.clone(), format!("Error: {}", e), duration);
                    context.complete_step(&step.name, result.clone());
                    Ok(result)
                }
            }
        }
    }

    /// Execute step once (without retry)
    async fn execute_step_once(
        &self,
        step: &Step,
        context: &ExecutionContext,
    ) -> Result<StepResult> {
        let start_time = Instant::now();

        // Resolve parameters with variable substitution
        let resolved_params =
            self.resolve_parameters(&step.parameters, &context.get_variables())?;

        // Execute based on step type
        let result = match step.step_type {
            StepType::Synthesize => self.execute_synthesize(step, &resolved_params).await,
            StepType::Validate => self.execute_validate(step, &resolved_params).await,
            StepType::FileOp => self.execute_file_op(step, &resolved_params).await,
            StepType::Command => self.execute_command(step, &resolved_params).await,
            StepType::Script => self.execute_script(step, &resolved_params).await,
            StepType::Branch => self.execute_branch(step, &resolved_params).await,
            StepType::Loop => self.execute_loop(step, &resolved_params).await,
            StepType::Workflow => self.execute_subworkflow(step, &resolved_params).await,
            StepType::Wait => self.execute_wait(step, &resolved_params).await,
            StepType::Notify => self.execute_notify(step, &resolved_params).await,
        }?;

        let duration = start_time.elapsed().as_millis() as u64;

        Ok(StepResult::success(step.name.clone(), result, duration))
    }

    /// Execute for-each loop
    async fn execute_for_each(
        &self,
        step: &Step,
        for_each_var: &str,
        context: &mut ExecutionContext,
    ) -> Result<StepResult> {
        let variables = context.get_variables();

        // Resolve for-each variable
        let var_name = for_each_var
            .strip_prefix("${")
            .and_then(|s| s.strip_suffix('}'))
            .unwrap_or(for_each_var);

        let items = variables
            .get(var_name)
            .and_then(|v| v.as_array())
            .ok_or_else(|| {
                CliError::Workflow(format!(
                    "For-each variable '{}' not found or not an array",
                    var_name
                ))
            })?;

        let start_time = Instant::now();
        let mut all_results = Vec::new();

        for (idx, item) in items.iter().enumerate() {
            // Create new step with current item as variable
            let mut step_clone = step.clone();
            step_clone.for_each = None;
            step_clone.name = format!("{}[{}]", step.name, idx);

            // Set loop variable
            context.set_variable(format!("{}_item", var_name), item.clone());
            context.set_variable(format!("{}_index", var_name), serde_json::json!(idx));

            let result = self.execute_step_once(&step_clone, context).await?;
            all_results.push(result);
        }

        let duration = start_time.elapsed().as_millis() as u64;
        let success = all_results.iter().all(|r| r.success);

        Ok(StepResult {
            step_name: step.name.clone(),
            success,
            message: format!("Executed {} iterations", all_results.len()),
            output: HashMap::new(),
            duration_ms: duration,
            attempts: 1,
        })
    }

    /// Resolve parameters with variable substitution
    fn resolve_parameters(
        &self,
        params: &HashMap<String, serde_json::Value>,
        variables: &HashMap<String, serde_json::Value>,
    ) -> Result<HashMap<String, serde_json::Value>> {
        let mut resolved = HashMap::new();

        for (key, value) in params {
            let resolved_value = self.resolve_value(value, variables);
            resolved.insert(key.clone(), resolved_value);
        }

        Ok(resolved)
    }

    /// Resolve a single value with variable substitution
    fn resolve_value(
        &self,
        value: &serde_json::Value,
        variables: &HashMap<String, serde_json::Value>,
    ) -> serde_json::Value {
        match value {
            serde_json::Value::String(s) => {
                if let Some(var_name) = s.strip_prefix("${").and_then(|s| s.strip_suffix('}')) {
                    variables
                        .get(var_name)
                        .cloned()
                        .unwrap_or(serde_json::Value::Null)
                } else {
                    value.clone()
                }
            }
            serde_json::Value::Array(arr) => serde_json::Value::Array(
                arr.iter()
                    .map(|v| self.resolve_value(v, variables))
                    .collect(),
            ),
            serde_json::Value::Object(obj) => serde_json::Value::Object(
                obj.iter()
                    .map(|(k, v)| (k.clone(), self.resolve_value(v, variables)))
                    .collect(),
            ),
            _ => value.clone(),
        }
    }

    // Step type implementations (placeholders for actual implementation)

    async fn execute_synthesize(
        &self,
        _step: &Step,
        _params: &HashMap<String, serde_json::Value>,
    ) -> Result<String> {
        Ok("Synthesis completed".to_string())
    }

    async fn execute_validate(
        &self,
        _step: &Step,
        _params: &HashMap<String, serde_json::Value>,
    ) -> Result<String> {
        Ok("Validation passed".to_string())
    }

    async fn execute_file_op(
        &self,
        _step: &Step,
        _params: &HashMap<String, serde_json::Value>,
    ) -> Result<String> {
        Ok("File operation completed".to_string())
    }

    async fn execute_command(
        &self,
        _step: &Step,
        _params: &HashMap<String, serde_json::Value>,
    ) -> Result<String> {
        Ok("Command executed".to_string())
    }

    async fn execute_script(
        &self,
        _step: &Step,
        _params: &HashMap<String, serde_json::Value>,
    ) -> Result<String> {
        Ok("Script executed".to_string())
    }

    async fn execute_branch(
        &self,
        _step: &Step,
        _params: &HashMap<String, serde_json::Value>,
    ) -> Result<String> {
        Ok("Branch evaluated".to_string())
    }

    async fn execute_loop(
        &self,
        _step: &Step,
        _params: &HashMap<String, serde_json::Value>,
    ) -> Result<String> {
        Ok("Loop completed".to_string())
    }

    async fn execute_subworkflow(
        &self,
        _step: &Step,
        _params: &HashMap<String, serde_json::Value>,
    ) -> Result<String> {
        Ok("Sub-workflow completed".to_string())
    }

    async fn execute_wait(
        &self,
        _step: &Step,
        params: &HashMap<String, serde_json::Value>,
    ) -> Result<String> {
        if let Some(duration) = params.get("duration_ms") {
            if let Some(ms) = duration.as_u64() {
                tokio::time::sleep(tokio::time::Duration::from_millis(ms)).await;
            }
        }
        Ok("Wait completed".to_string())
    }

    async fn execute_notify(
        &self,
        _step: &Step,
        _params: &HashMap<String, serde_json::Value>,
    ) -> Result<String> {
        Ok("Notification sent".to_string())
    }
}

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

/// Overall workflow execution result
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ExecutionResult {
    /// Workflow name
    pub workflow_name: String,
    /// Success status
    pub success: bool,
    /// Result message
    pub message: String,
    /// Execution statistics
    pub stats: WorkflowStats,
}

impl ExecutionResult {
    /// Create success result
    pub fn success(workflow_name: String, message: String, stats: WorkflowStats) -> Self {
        Self {
            workflow_name,
            success: true,
            message,
            stats,
        }
    }

    /// Create failure result
    pub fn failure(workflow_name: String, message: String, stats: WorkflowStats) -> Self {
        Self {
            workflow_name,
            success: false,
            message,
            stats,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::workflow::definition::StepType;

    #[test]
    fn test_execution_context_creation() {
        let workflow = Workflow::new("test", "1.0", "Test workflow");
        let context = ExecutionContext::new(workflow);

        assert_eq!(context.completed_steps().len(), 0);
        assert_eq!(context.skipped_steps().len(), 0);
        assert_eq!(context.total_retries(), 0);
    }

    #[test]
    fn test_execution_context_variables() {
        let mut workflow = Workflow::new("test", "1.0", "Test workflow");
        workflow.add_variable(
            "test_var".to_string(),
            super::super::definition::Variable::String("test_value".to_string()),
        );

        let context = ExecutionContext::new(workflow);
        let variables = context.get_variables();

        assert_eq!(variables.len(), 1);
        assert_eq!(
            variables
                .get("test_var")
                .unwrap()
                .as_str()
                .unwrap_or_default(),
            "test_value"
        );
    }

    #[test]
    fn test_step_result_creation() {
        let result = StepResult::success("step1".to_string(), "Success".to_string(), 100);

        assert!(result.success);
        assert_eq!(result.step_name, "step1");
        assert_eq!(result.duration_ms, 100);
    }

    #[test]
    fn test_step_result_with_output() {
        let result = StepResult::success("step1".to_string(), "Success".to_string(), 100)
            .with_output("key1".to_string(), serde_json::json!("value1"));

        assert_eq!(result.output.len(), 1);
        assert_eq!(
            result
                .output
                .get("key1")
                .unwrap()
                .as_str()
                .unwrap_or_default(),
            "value1"
        );
    }

    #[tokio::test]
    async fn test_step_executor_creation() {
        let _executor = StepExecutor::new();
        // Verify creation works without panic
    }

    #[test]
    fn test_execution_result_success() {
        let stats = WorkflowStats::new();
        let result = ExecutionResult::success("test".to_string(), "Done".to_string(), stats);

        assert!(result.success);
        assert_eq!(result.workflow_name, "test");
    }

    #[test]
    fn test_execution_result_failure() {
        let stats = WorkflowStats::new();
        let result = ExecutionResult::failure("test".to_string(), "Failed".to_string(), stats);

        assert!(!result.success);
        assert_eq!(result.message, "Failed");
    }
}