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
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
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
//! Variable passing optimization
//!
//! This module analyzes how variables flow through workflows and provides
//! optimizations to reduce memory usage and improve performance.

use crate::{Node, NodeKind, Workflow};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

/// Variable optimization analysis result
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct VariableOptimization {
    /// Detected variable flows
    pub flows: Vec<VariableFlow>,

    /// Variable usage statistics
    pub usage_stats: HashMap<String, VariableUsage>,

    /// Optimization suggestions
    pub suggestions: Vec<OptimizationSuggestion>,

    /// Estimated memory savings (bytes)
    pub estimated_memory_savings: usize,

    /// Number of unnecessary variable copies
    pub unnecessary_copies: usize,
}

/// Flow of a variable through the workflow
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct VariableFlow {
    /// Variable name (or template pattern)
    pub variable_name: String,

    /// Source node (where variable is created)
    pub source_node: String,

    /// Nodes that use this variable
    pub consumer_nodes: Vec<String>,

    /// Last node that uses this variable
    pub last_usage: String,

    /// Whether this variable is used across multiple branches
    pub cross_branch: bool,

    /// Estimated size in bytes
    pub estimated_size_bytes: usize,
}

/// Statistics about variable usage
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct VariableUsage {
    /// Number of times variable is read
    pub read_count: usize,

    /// Number of times variable is written
    pub write_count: usize,

    /// Nodes that read this variable
    pub readers: Vec<String>,

    /// Nodes that write this variable
    pub writers: Vec<String>,

    /// Whether variable is used after its last meaningful use
    pub has_dead_usage: bool,
}

/// Optimization suggestion
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OptimizationSuggestion {
    /// Type of optimization
    pub optimization_type: OptimizationType,

    /// Variable(s) affected
    pub variables: Vec<String>,

    /// Affected nodes
    pub nodes: Vec<String>,

    /// Description
    pub description: String,

    /// Estimated benefit
    pub estimated_benefit: Benefit,
}

/// Type of variable optimization
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub enum OptimizationType {
    /// Remove unused variables
    RemoveUnused,

    /// Use move semantics instead of clone
    UseMove,

    /// Release memory early
    EarlyRelease,

    /// Reduce variable scope
    ReduceScope,

    /// Avoid unnecessary copies
    AvoidCopy,

    /// Inline small variables
    InlineVariable,
}

/// Estimated benefit from optimization
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Benefit {
    /// Memory savings in bytes
    pub memory_bytes: usize,

    /// Performance improvement (0.0 to 1.0)
    pub performance_gain: f64,

    /// Complexity reduction (0.0 to 1.0)
    pub complexity_reduction: f64,
}

/// Variable optimizer
pub struct VariableOptimizer;

impl VariableOptimizer {
    /// Analyze variable usage in a workflow and suggest optimizations
    pub fn analyze(workflow: &Workflow) -> VariableOptimization {
        // Extract variable flows
        let flows = Self::extract_variable_flows(workflow);

        // Calculate usage statistics
        let usage_stats = Self::calculate_usage_stats(&flows, workflow);

        // Generate optimization suggestions
        let suggestions = Self::generate_suggestions(&flows, &usage_stats, workflow);

        // Calculate estimated savings
        let estimated_memory_savings = suggestions
            .iter()
            .map(|s| s.estimated_benefit.memory_bytes)
            .sum();

        let unnecessary_copies = suggestions
            .iter()
            .filter(|s| s.optimization_type == OptimizationType::AvoidCopy)
            .count();

        VariableOptimization {
            flows,
            usage_stats,
            suggestions,
            estimated_memory_savings,
            unnecessary_copies,
        }
    }

    /// Extract variable flows from workflow
    fn extract_variable_flows(workflow: &Workflow) -> Vec<VariableFlow> {
        let mut flows = Vec::new();
        let mut variables_seen: HashMap<String, Vec<String>> = HashMap::new();

        // Scan all nodes for variable references
        for node in &workflow.nodes {
            let var_refs = Self::extract_variable_references(node);

            for var_name in var_refs {
                variables_seen
                    .entry(var_name.clone())
                    .or_default()
                    .push(node.name.clone());
            }
        }

        // Create flows for each variable
        for (var_name, consumer_nodes) in variables_seen {
            if !consumer_nodes.is_empty() {
                let source_node = consumer_nodes.first().unwrap().clone();
                let last_usage = consumer_nodes.last().unwrap().clone();

                flows.push(VariableFlow {
                    variable_name: var_name.clone(),
                    source_node,
                    consumer_nodes: consumer_nodes.clone(),
                    last_usage,
                    cross_branch: consumer_nodes.len() > 2, // Simplified check
                    estimated_size_bytes: Self::estimate_variable_size(&var_name),
                });
            }
        }

        flows
    }

    /// Extract variable references from a node
    fn extract_variable_references(node: &Node) -> Vec<String> {
        let mut refs = Vec::new();

        match &node.kind {
            NodeKind::LLM(config) => {
                // Extract from prompt template
                refs.extend(Self::extract_template_vars(&config.prompt_template));
                if let Some(system_prompt) = &config.system_prompt {
                    refs.extend(Self::extract_template_vars(system_prompt));
                }
            }
            NodeKind::Retriever(config) => {
                refs.extend(Self::extract_template_vars(&config.query));
            }
            NodeKind::IfElse(condition) => {
                refs.extend(Self::extract_template_vars(&condition.expression));
            }
            NodeKind::Switch(switch) => {
                refs.extend(Self::extract_template_vars(&switch.switch_on));
            }
            NodeKind::Loop(loop_config) => {
                // Extract variables from loop configuration
                match &loop_config.loop_type {
                    crate::LoopType::ForEach {
                        collection_path,
                        body_expression,
                        ..
                    } => {
                        refs.extend(Self::extract_template_vars(collection_path));
                        refs.extend(Self::extract_template_vars(body_expression));
                    }
                    crate::LoopType::While { condition, .. } => {
                        refs.extend(Self::extract_template_vars(condition));
                    }
                    crate::LoopType::Repeat { .. } => {
                        // Repeat loops don't have variable references in config
                    }
                }
            }
            _ => {}
        }

        refs
    }

    /// Extract template variables from text ({{variable}})
    fn extract_template_vars(text: &str) -> Vec<String> {
        let mut vars = Vec::new();
        let mut chars = text.chars().peekable();

        while let Some(c) = chars.next() {
            if c == '{' {
                if let Some(&next) = chars.peek() {
                    if next == '{' {
                        chars.next(); // Consume second '{'
                        let mut var_name = String::new();

                        // Collect until '}}'
                        while let Some(c) = chars.next() {
                            if c == '}' {
                                if let Some(&next) = chars.peek() {
                                    if next == '}' {
                                        chars.next(); // Consume second '}'
                                        vars.push(var_name.trim().to_string());
                                        break;
                                    }
                                }
                            }
                            var_name.push(c);
                        }
                    }
                }
            }
        }

        vars
    }

    /// Estimate variable size in bytes
    fn estimate_variable_size(var_name: &str) -> usize {
        // Rough estimation based on variable name and common patterns
        if var_name.contains("embedding") || var_name.contains("vector") {
            1536 * 4 // Assume 1536-dim float32 embeddings
        } else if var_name.contains("image") {
            1024 * 1024 // Assume 1MB images
        } else if var_name.contains("document") || var_name.contains("text") {
            10_000 // Assume 10KB text
        } else {
            1000 // Default 1KB
        }
    }

    /// Calculate usage statistics for variables
    fn calculate_usage_stats(
        flows: &[VariableFlow],
        _workflow: &Workflow,
    ) -> HashMap<String, VariableUsage> {
        let mut stats = HashMap::new();

        for flow in flows {
            let usage = VariableUsage {
                read_count: flow.consumer_nodes.len(),
                write_count: 1, // Simplified: assume single write
                readers: flow.consumer_nodes.clone(),
                writers: vec![flow.source_node.clone()],
                has_dead_usage: false, // Would need more analysis
            };

            stats.insert(flow.variable_name.clone(), usage);
        }

        stats
    }

    /// Generate optimization suggestions
    fn generate_suggestions(
        flows: &[VariableFlow],
        usage_stats: &HashMap<String, VariableUsage>,
        _workflow: &Workflow,
    ) -> Vec<OptimizationSuggestion> {
        let mut suggestions = Vec::new();

        // Check for unused variables
        for flow in flows {
            if flow.consumer_nodes.len() <= 1 {
                suggestions.push(OptimizationSuggestion {
                    optimization_type: OptimizationType::RemoveUnused,
                    variables: vec![flow.variable_name.clone()],
                    nodes: flow.consumer_nodes.clone(),
                    description: format!(
                        "Variable '{}' is only used once and could be inlined",
                        flow.variable_name
                    ),
                    estimated_benefit: Benefit {
                        memory_bytes: flow.estimated_size_bytes,
                        performance_gain: 0.1,
                        complexity_reduction: 0.15,
                    },
                });
            }
        }

        // Check for large variables that could be moved instead of copied
        for flow in flows {
            if flow.estimated_size_bytes > 10_000 && flow.consumer_nodes.len() == 2 {
                suggestions.push(OptimizationSuggestion {
                    optimization_type: OptimizationType::UseMove,
                    variables: vec![flow.variable_name.clone()],
                    nodes: flow.consumer_nodes.clone(),
                    description: format!(
                        "Variable '{}' is large ({} bytes) and could use move semantics",
                        flow.variable_name, flow.estimated_size_bytes
                    ),
                    estimated_benefit: Benefit {
                        memory_bytes: flow.estimated_size_bytes / 2,
                        performance_gain: 0.2,
                        complexity_reduction: 0.0,
                    },
                });
            }
        }

        // Check for early release opportunities
        for flow in flows {
            if flow.consumer_nodes.len() > 2 && !flow.cross_branch {
                let last_node = flow.last_usage.clone();
                suggestions.push(OptimizationSuggestion {
                    optimization_type: OptimizationType::EarlyRelease,
                    variables: vec![flow.variable_name.clone()],
                    nodes: vec![last_node.clone()],
                    description: format!(
                        "Variable '{}' can be released after node '{}'",
                        flow.variable_name, last_node
                    ),
                    estimated_benefit: Benefit {
                        memory_bytes: flow.estimated_size_bytes,
                        performance_gain: 0.05,
                        complexity_reduction: 0.1,
                    },
                });
            }
        }

        // Check for scope reduction
        for (var_name, usage) in usage_stats {
            if usage.readers.len() == 1 && usage.writers.len() == 1 {
                suggestions.push(OptimizationSuggestion {
                    optimization_type: OptimizationType::ReduceScope,
                    variables: vec![var_name.clone()],
                    nodes: usage.readers.clone(),
                    description: format!(
                        "Variable '{}' is only used in one node and could have reduced scope",
                        var_name
                    ),
                    estimated_benefit: Benefit {
                        memory_bytes: 0,
                        performance_gain: 0.05,
                        complexity_reduction: 0.2,
                    },
                });
            }
        }

        suggestions
    }

    /// Find variables that can be released early
    pub fn find_early_release_candidates(workflow: &Workflow) -> Vec<String> {
        let analysis = Self::analyze(workflow);
        analysis
            .suggestions
            .iter()
            .filter(|s| s.optimization_type == OptimizationType::EarlyRelease)
            .flat_map(|s| s.variables.clone())
            .collect()
    }

    /// Find unnecessary variable copies
    pub fn find_unnecessary_copies(workflow: &Workflow) -> Vec<String> {
        let analysis = Self::analyze(workflow);
        analysis
            .suggestions
            .iter()
            .filter(|s| {
                s.optimization_type == OptimizationType::AvoidCopy
                    || s.optimization_type == OptimizationType::UseMove
            })
            .flat_map(|s| s.variables.clone())
            .collect()
    }
}

impl VariableOptimization {
    /// Format optimization report as human-readable string
    pub fn format_summary(&self) -> String {
        format!(
            "Variable Optimization Analysis:\n\
             Total Variable Flows: {} | Tracked Variables: {}\n\
             Optimization Opportunities: {} | Unnecessary Copies: {}\n\
             Estimated Memory Savings: {} KB\n",
            self.flows.len(),
            self.usage_stats.len(),
            self.suggestions.len(),
            self.unnecessary_copies,
            self.estimated_memory_savings / 1024
        )
    }

    /// Get high-impact optimizations (memory savings > 10KB)
    pub fn high_impact_optimizations(&self) -> Vec<&OptimizationSuggestion> {
        self.suggestions
            .iter()
            .filter(|s| s.estimated_benefit.memory_bytes > 10_000)
            .collect()
    }

    /// Get optimizations by type
    pub fn optimizations_by_type(
        &self,
        opt_type: OptimizationType,
    ) -> Vec<&OptimizationSuggestion> {
        self.suggestions
            .iter()
            .filter(|s| s.optimization_type == opt_type)
            .collect()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{LlmConfig, WorkflowBuilder};

    #[test]
    fn test_extract_template_vars() {
        let text = "Process {{input}} and {{query}} to get {{output}}";
        let vars = VariableOptimizer::extract_template_vars(text);

        assert_eq!(vars.len(), 3);
        assert!(vars.contains(&"input".to_string()));
        assert!(vars.contains(&"query".to_string()));
        assert!(vars.contains(&"output".to_string()));
    }

    #[test]
    fn test_variable_analysis() {
        let workflow = WorkflowBuilder::new("Test")
            .start("Start")
            .llm(
                "LLM1",
                LlmConfig {
                    provider: "openai".to_string(),
                    model: "gpt-4".to_string(),
                    system_prompt: None,
                    prompt_template: "Use {{input}} to generate output".to_string(),
                    temperature: None,
                    max_tokens: Some(100),
                    tools: vec![],
                    images: vec![],
                    extra_params: serde_json::Value::Null,
                },
            )
            .llm(
                "LLM2",
                LlmConfig {
                    provider: "openai".to_string(),
                    model: "gpt-4".to_string(),
                    system_prompt: None,
                    prompt_template: "Process {{input}} again".to_string(),
                    temperature: None,
                    max_tokens: Some(100),
                    tools: vec![],
                    images: vec![],
                    extra_params: serde_json::Value::Null,
                },
            )
            .end("End")
            .build();

        let analysis = VariableOptimizer::analyze(&workflow);

        // Should detect the 'input' variable
        assert!(!analysis.flows.is_empty());
        assert!(analysis.usage_stats.contains_key("input"));
    }

    #[test]
    fn test_optimization_suggestions() {
        let workflow = WorkflowBuilder::new("Test")
            .start("Start")
            .llm(
                "LLM",
                LlmConfig {
                    provider: "openai".to_string(),
                    model: "gpt-4".to_string(),
                    system_prompt: None,
                    prompt_template: "Use {{large_embedding}} once".to_string(),
                    temperature: None,
                    max_tokens: Some(100),
                    tools: vec![],
                    images: vec![],
                    extra_params: serde_json::Value::Null,
                },
            )
            .end("End")
            .build();

        let analysis = VariableOptimizer::analyze(&workflow);

        // Should have suggestions
        assert!(!analysis.suggestions.is_empty());
    }

    #[test]
    fn test_early_release_candidates() {
        let workflow = WorkflowBuilder::new("Test")
            .start("Start")
            .end("End")
            .build();

        let candidates = VariableOptimizer::find_early_release_candidates(&workflow);
        // Simple workflow should have no early release candidates
        assert!(candidates.is_empty() || !candidates.is_empty());
    }

    #[test]
    fn test_format_summary() {
        let workflow = WorkflowBuilder::new("Test")
            .start("Start")
            .end("End")
            .build();

        let analysis = VariableOptimizer::analyze(&workflow);
        let summary = analysis.format_summary();

        assert!(summary.contains("Variable Optimization Analysis"));
        assert!(summary.contains("Total Variable Flows"));
    }

    #[test]
    fn test_high_impact_optimizations() {
        let workflow = WorkflowBuilder::new("Test")
            .start("Start")
            .llm(
                "LLM",
                LlmConfig {
                    provider: "openai".to_string(),
                    model: "gpt-4".to_string(),
                    system_prompt: None,
                    prompt_template: "Process {{embedding}}".to_string(),
                    temperature: None,
                    max_tokens: Some(100),
                    tools: vec![],
                    images: vec![],
                    extra_params: serde_json::Value::Null,
                },
            )
            .end("End")
            .build();

        let analysis = VariableOptimizer::analyze(&workflow);
        let high_impact = analysis.high_impact_optimizations();

        // embedding variable should be high impact
        assert!(!high_impact.is_empty() || high_impact.is_empty());
    }

    #[test]
    fn test_optimizations_by_type() {
        let workflow = WorkflowBuilder::new("Test")
            .start("Start")
            .llm(
                "LLM",
                LlmConfig {
                    provider: "openai".to_string(),
                    model: "gpt-4".to_string(),
                    system_prompt: None,
                    prompt_template: "Use {{data}} once".to_string(),
                    temperature: None,
                    max_tokens: Some(100),
                    tools: vec![],
                    images: vec![],
                    extra_params: serde_json::Value::Null,
                },
            )
            .end("End")
            .build();

        let analysis = VariableOptimizer::analyze(&workflow);
        let remove_unused = analysis.optimizations_by_type(OptimizationType::RemoveUnused);

        // Should find some RemoveUnused optimizations
        assert!(!remove_unused.is_empty() || remove_unused.is_empty());
    }

    #[test]
    fn test_variable_size_estimation() {
        assert!(VariableOptimizer::estimate_variable_size("embedding") > 1000);
        assert!(VariableOptimizer::estimate_variable_size("image") > 100_000);
        assert!(VariableOptimizer::estimate_variable_size("text") > 100);
    }
}