oxify-model 0.1.0

Data models and types for OxiFY workflows, execution, and configuration
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
use crate::{NodeId, WorkflowId};
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use uuid::Uuid;

#[cfg(feature = "openapi")]
use utoipa::ToSchema;

/// Unique identifier for a workflow execution
pub type ExecutionId = Uuid;

/// Context for workflow execution
#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
pub struct ExecutionContext {
    /// Unique execution identifier
    #[cfg_attr(feature = "openapi", schema(value_type = String))]
    pub execution_id: ExecutionId,

    /// The workflow being executed
    #[cfg_attr(feature = "openapi", schema(value_type = String))]
    pub workflow_id: WorkflowId,

    /// When the execution started
    pub started_at: DateTime<Utc>,

    /// When the execution completed (if finished)
    pub completed_at: Option<DateTime<Utc>>,

    /// Current execution state
    pub state: ExecutionState,

    /// Node execution results
    #[cfg_attr(feature = "openapi", schema(value_type = HashMap<String, NodeExecutionResult>))]
    pub node_results: HashMap<NodeId, NodeExecutionResult>,

    /// Global variables/context available to all nodes
    #[serde(default)]
    pub variables: HashMap<String, serde_json::Value>,

    /// Checkpoint data for resume capability
    #[serde(default)]
    pub checkpoint: Option<ExecutionCheckpoint>,
}

impl ExecutionContext {
    pub fn new(workflow_id: WorkflowId) -> Self {
        Self {
            execution_id: Uuid::new_v4(),
            workflow_id,
            started_at: Utc::now(),
            completed_at: None,
            state: ExecutionState::Running,
            node_results: HashMap::new(),
            variables: HashMap::new(),
            checkpoint: None,
        }
    }

    /// Create a checkpoint of the current execution state
    pub fn create_checkpoint(&mut self) -> ExecutionCheckpoint {
        let checkpoint = ExecutionCheckpoint {
            timestamp: Utc::now(),
            completed_nodes: self.node_results.keys().copied().collect(),
            variables: self.variables.clone(),
            state: self.state.clone(),
        };
        self.checkpoint = Some(checkpoint.clone());
        checkpoint
    }

    /// Resume from a checkpoint
    pub fn resume_from_checkpoint(
        checkpoint: ExecutionCheckpoint,
        workflow_id: WorkflowId,
    ) -> Self {
        let variables = checkpoint.variables.clone();
        let state = checkpoint.state.clone();
        Self {
            execution_id: Uuid::new_v4(),
            workflow_id,
            started_at: checkpoint.timestamp,
            completed_at: None,
            state,
            node_results: HashMap::new(), // Will be restored by engine
            variables,
            checkpoint: Some(checkpoint),
        }
    }

    /// Check if execution can be resumed
    pub fn can_resume(&self) -> bool {
        self.checkpoint.is_some() && matches!(self.state, ExecutionState::Paused)
    }

    /// Pause execution
    pub fn pause(&mut self) {
        self.state = ExecutionState::Paused;
        self.create_checkpoint();
    }

    /// Resume paused execution
    pub fn resume(&mut self) {
        if self.state == ExecutionState::Paused {
            self.state = ExecutionState::Running;
        }
    }

    /// Cancel execution
    pub fn cancel(&mut self) {
        self.state = ExecutionState::Cancelled;
        self.mark_completed();
    }

    /// Mark execution as completed
    pub fn mark_completed(&mut self) {
        if self.completed_at.is_none() {
            self.completed_at = Some(Utc::now());
        }
    }

    /// Record the result of a node execution
    pub fn record_node_result(&mut self, node_id: NodeId, result: NodeExecutionResult) {
        self.node_results.insert(node_id, result);
    }

    /// Get the result of a previous node execution
    pub fn get_node_result(&self, node_id: &NodeId) -> Option<&NodeExecutionResult> {
        self.node_results.get(node_id)
    }

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

    /// Get a variable from the execution context
    pub fn get_variable(&self, key: &str) -> Option<&serde_json::Value> {
        self.variables.get(key)
    }
}

/// State of workflow execution
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
pub enum ExecutionState {
    /// Execution is currently running
    Running,

    /// Execution completed successfully
    Completed,

    /// Execution failed
    Failed(String),

    /// Execution was cancelled
    Cancelled,

    /// Execution is paused
    Paused,
}

/// Result of executing a single node
#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
pub struct NodeExecutionResult {
    /// When this node started executing
    pub started_at: DateTime<Utc>,

    /// When this node finished executing
    pub completed_at: Option<DateTime<Utc>>,

    /// The result of the execution
    pub result: ExecutionResult,

    /// Number of retry attempts made (0 means no retries)
    #[serde(default)]
    pub retry_count: u32,

    /// Execution metrics (token usage, costs, etc.)
    #[serde(default)]
    pub metrics: Option<NodeMetrics>,
}

/// Execution metrics for a node
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
pub struct NodeMetrics {
    /// Execution duration in milliseconds
    pub duration_ms: Option<u64>,

    /// Token usage for LLM nodes
    #[serde(default)]
    pub token_usage: Option<TokenUsage>,

    /// Estimated cost in USD (for LLM API calls)
    #[serde(default)]
    pub cost_usd: Option<f64>,

    /// API calls made by this node
    #[serde(default)]
    pub api_calls: u32,

    /// Bytes transferred (input + output)
    #[serde(default)]
    pub bytes_transferred: u64,

    /// Memory usage in bytes (if tracked)
    #[serde(default)]
    pub memory_bytes: Option<u64>,

    /// Custom metrics (provider-specific)
    #[serde(default)]
    pub custom: HashMap<String, serde_json::Value>,
}

/// Token usage for LLM nodes
#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
pub struct TokenUsage {
    /// Input/prompt tokens
    pub input_tokens: u32,

    /// Output/completion tokens
    pub output_tokens: u32,

    /// Total tokens (input + output)
    pub total_tokens: u32,

    /// Cached tokens (if applicable)
    #[serde(default)]
    pub cached_tokens: Option<u32>,
}

impl TokenUsage {
    /// Create new token usage record
    pub fn new(input_tokens: u32, output_tokens: u32) -> Self {
        Self {
            input_tokens,
            output_tokens,
            total_tokens: input_tokens + output_tokens,
            cached_tokens: None,
        }
    }

    /// Estimate cost based on provider pricing
    pub fn estimate_cost(&self, input_price_per_1k: f64, output_price_per_1k: f64) -> f64 {
        let input_cost = (self.input_tokens as f64 / 1000.0) * input_price_per_1k;
        let output_cost = (self.output_tokens as f64 / 1000.0) * output_price_per_1k;
        input_cost + output_cost
    }
}

impl NodeExecutionResult {
    pub fn new() -> Self {
        Self {
            started_at: Utc::now(),
            completed_at: None,
            result: ExecutionResult::Pending,
            retry_count: 0,
            metrics: None,
        }
    }

    pub fn complete(mut self, result: ExecutionResult) -> Self {
        let completed = Utc::now();
        let duration_ms = (completed - self.started_at).num_milliseconds() as u64;
        self.completed_at = Some(completed);
        self.result = result;

        // Auto-populate duration if metrics exist
        if let Some(ref mut metrics) = self.metrics {
            metrics.duration_ms = Some(duration_ms);
        } else {
            self.metrics = Some(NodeMetrics {
                duration_ms: Some(duration_ms),
                ..Default::default()
            });
        }

        self
    }

    /// Add metrics to the execution result
    pub fn with_metrics(mut self, metrics: NodeMetrics) -> Self {
        self.metrics = Some(metrics);
        self
    }

    /// Add token usage to the execution result
    pub fn with_token_usage(mut self, usage: TokenUsage) -> Self {
        if let Some(ref mut metrics) = self.metrics {
            metrics.token_usage = Some(usage);
        } else {
            self.metrics = Some(NodeMetrics {
                token_usage: Some(usage),
                ..Default::default()
            });
        }
        self
    }

    /// Get execution duration in milliseconds
    pub fn duration_ms(&self) -> Option<u64> {
        self.metrics.as_ref().and_then(|m| m.duration_ms)
    }

    /// Get total token count
    pub fn total_tokens(&self) -> Option<u32> {
        self.metrics
            .as_ref()
            .and_then(|m| m.token_usage.as_ref())
            .map(|t| t.total_tokens)
    }

    /// Get estimated cost
    pub fn cost_usd(&self) -> Option<f64> {
        self.metrics.as_ref().and_then(|m| m.cost_usd)
    }
}

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

/// Result of a node execution
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
pub enum ExecutionResult {
    /// Node hasn't executed yet
    Pending,

    /// Node executed successfully with output
    Success(serde_json::Value),

    /// Node execution failed
    Failure(String),

    /// Node execution was skipped (e.g., conditional branch not taken)
    Skipped,
}

/// Checkpoint for resumable execution
#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
pub struct ExecutionCheckpoint {
    /// When this checkpoint was created
    pub timestamp: DateTime<Utc>,

    /// Nodes that have been completed
    #[cfg_attr(feature = "openapi", schema(value_type = Vec<String>))]
    pub completed_nodes: Vec<NodeId>,

    /// Variables at checkpoint time
    pub variables: HashMap<String, serde_json::Value>,

    /// Execution state at checkpoint
    pub state: ExecutionState,
}

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

    #[test]
    fn test_execution_context() {
        let workflow_id = Uuid::new_v4();
        let mut ctx = ExecutionContext::new(workflow_id);

        let node_id = Uuid::new_v4();
        let result = NodeExecutionResult::new().complete(ExecutionResult::Success(
            serde_json::json!({"output": "test"}),
        ));

        ctx.record_node_result(node_id, result);

        assert!(ctx.get_node_result(&node_id).is_some());
        assert_eq!(ctx.state, ExecutionState::Running);
    }

    #[test]
    fn test_execution_context_new() {
        let workflow_id = Uuid::new_v4();
        let ctx = ExecutionContext::new(workflow_id);

        assert_eq!(ctx.workflow_id, workflow_id);
        assert_eq!(ctx.state, ExecutionState::Running);
        assert_eq!(ctx.node_results.len(), 0);
        assert_eq!(ctx.variables.len(), 0);
        assert!(ctx.completed_at.is_none());
        assert!(ctx.checkpoint.is_none());
    }

    #[test]
    fn test_execution_context_pause_resume() {
        let workflow_id = Uuid::new_v4();
        let mut ctx = ExecutionContext::new(workflow_id);

        assert_eq!(ctx.state, ExecutionState::Running);
        assert!(!ctx.can_resume());

        ctx.pause();
        assert_eq!(ctx.state, ExecutionState::Paused);
        assert!(ctx.can_resume());
        assert!(ctx.checkpoint.is_some());

        ctx.resume();
        assert_eq!(ctx.state, ExecutionState::Running);
    }

    #[test]
    fn test_execution_context_cancel() {
        let workflow_id = Uuid::new_v4();
        let mut ctx = ExecutionContext::new(workflow_id);

        ctx.cancel();
        assert_eq!(ctx.state, ExecutionState::Cancelled);
        assert!(ctx.completed_at.is_some());
    }

    #[test]
    fn test_execution_context_mark_completed() {
        let workflow_id = Uuid::new_v4();
        let mut ctx = ExecutionContext::new(workflow_id);

        assert!(ctx.completed_at.is_none());

        ctx.mark_completed();
        assert!(ctx.completed_at.is_some());

        let first_completion = ctx.completed_at.unwrap();
        ctx.mark_completed(); // Should not update
        assert_eq!(ctx.completed_at.unwrap(), first_completion);
    }

    #[test]
    fn test_execution_context_variables() {
        let workflow_id = Uuid::new_v4();
        let mut ctx = ExecutionContext::new(workflow_id);

        ctx.set_variable("key1".to_string(), serde_json::json!("value1"));
        ctx.set_variable("key2".to_string(), serde_json::json!(42));

        assert_eq!(ctx.get_variable("key1"), Some(&serde_json::json!("value1")));
        assert_eq!(ctx.get_variable("key2"), Some(&serde_json::json!(42)));
        assert_eq!(ctx.get_variable("key3"), None);
    }

    #[test]
    fn test_execution_context_checkpoint() {
        let workflow_id = Uuid::new_v4();
        let mut ctx = ExecutionContext::new(workflow_id);

        ctx.set_variable("var1".to_string(), serde_json::json!("test"));

        let checkpoint = ctx.create_checkpoint();

        assert_eq!(checkpoint.variables.len(), 1);
        assert_eq!(checkpoint.state, ExecutionState::Running);
        assert!(ctx.checkpoint.is_some());
    }

    #[test]
    fn test_execution_context_resume_from_checkpoint() {
        let workflow_id = Uuid::new_v4();
        let mut original_ctx = ExecutionContext::new(workflow_id);

        original_ctx.set_variable("var1".to_string(), serde_json::json!("test"));
        let checkpoint = original_ctx.create_checkpoint();

        let resumed_ctx = ExecutionContext::resume_from_checkpoint(checkpoint, workflow_id);

        assert_eq!(resumed_ctx.workflow_id, workflow_id);
        assert_eq!(resumed_ctx.variables.len(), 1);
        assert_eq!(
            resumed_ctx.get_variable("var1"),
            Some(&serde_json::json!("test"))
        );
    }

    #[test]
    fn test_node_execution_result_new() {
        let result = NodeExecutionResult::new();

        assert_eq!(result.retry_count, 0);
        assert!(result.completed_at.is_none());
        assert!(result.metrics.is_none());
        assert_eq!(result.result, ExecutionResult::Pending);
    }

    #[test]
    fn test_node_execution_result_complete() {
        let result = NodeExecutionResult::new().complete(ExecutionResult::Success(
            serde_json::json!({"data": "test"}),
        ));

        assert!(result.completed_at.is_some());
        assert!(matches!(result.result, ExecutionResult::Success(_)));
    }

    #[test]
    fn test_node_execution_result_with_metrics() {
        let metrics = NodeMetrics {
            duration_ms: Some(100),
            token_usage: Some(TokenUsage {
                input_tokens: 50,
                output_tokens: 30,
                total_tokens: 80,
                cached_tokens: None,
            }),
            cost_usd: Some(0.001),
            api_calls: 1,
            bytes_transferred: 1024,
            memory_bytes: Some(128),
            custom: Default::default(),
        };

        let result = NodeExecutionResult::new().with_metrics(metrics.clone());

        assert!(result.metrics.is_some());
        let result_metrics = result.metrics.unwrap();
        assert_eq!(result_metrics.duration_ms, Some(100));
        assert_eq!(result_metrics.cost_usd, Some(0.001));
        assert_eq!(result_metrics.api_calls, 1);
        assert_eq!(result_metrics.bytes_transferred, 1024);
    }

    #[test]
    fn test_execution_result_variants() {
        assert!(matches!(ExecutionResult::Pending, ExecutionResult::Pending));
        assert!(matches!(
            ExecutionResult::Success(serde_json::json!(null)),
            ExecutionResult::Success(_)
        ));
        assert!(matches!(
            ExecutionResult::Failure("test".to_string()),
            ExecutionResult::Failure(_)
        ));
        assert!(matches!(ExecutionResult::Skipped, ExecutionResult::Skipped));
    }

    #[test]
    fn test_execution_state_variants() {
        assert_eq!(ExecutionState::Running, ExecutionState::Running);
        assert_eq!(ExecutionState::Completed, ExecutionState::Completed);
        assert_eq!(ExecutionState::Cancelled, ExecutionState::Cancelled);
        assert_eq!(ExecutionState::Paused, ExecutionState::Paused);
        assert_eq!(
            ExecutionState::Failed("error".to_string()),
            ExecutionState::Failed("error".to_string())
        );
    }

    #[test]
    fn test_token_usage() {
        let token_usage = TokenUsage {
            input_tokens: 100,
            output_tokens: 50,
            total_tokens: 150,
            cached_tokens: None,
        };

        assert_eq!(token_usage.input_tokens, 100);
        assert_eq!(token_usage.output_tokens, 50);
        assert_eq!(token_usage.total_tokens, 150);
        assert_eq!(token_usage.cached_tokens, None);
    }

    #[test]
    fn test_node_metrics_default() {
        let metrics = NodeMetrics::default();

        assert_eq!(metrics.duration_ms, None);
        assert_eq!(metrics.token_usage, None);
        assert_eq!(metrics.cost_usd, None);
        assert_eq!(metrics.api_calls, 0);
        assert_eq!(metrics.bytes_transferred, 0);
        assert_eq!(metrics.memory_bytes, None);
    }
}