jorm 0.1.1

A lightweight DAG execution engine with natural language processing
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
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
use crate::core::error::JormError;
use crate::parser::dag_parser::DagParser;
use regex::Regex;
use std::collections::HashMap;

pub struct NlpProcessor {
    // Pattern matchers for different task types
    shell_patterns: Vec<Regex>,
    file_patterns: Vec<Regex>,
    http_patterns: Vec<Regex>,
    python_patterns: Vec<Regex>,
    rust_patterns: Vec<Regex>,
    jorm_patterns: Vec<Regex>,
    dependency_patterns: Vec<Regex>,
}

#[derive(Debug, Clone)]
struct ParsedTask {
    name: String,
    task_type: String,
    parameters: HashMap<String, String>,
    dependencies: Vec<String>,
}

impl NlpProcessor {
    pub async fn new() -> Result<Self, JormError> {
        let shell_patterns = vec![
            Regex::new(r#"(?i)run\s+(?:command\s+)?['"]([^'"]+)['"]"#).unwrap(),
            Regex::new(r#"(?i)execute\s+['"]([^'"]+)['"]"#).unwrap(),
            Regex::new(r#"(?i)(?:shell|command|cmd):\s*['"]([^'"]+)['"]"#).unwrap(),
            Regex::new(r"(?i)(?:bash|shell)\s+task").unwrap(),
            Regex::new(r"(?i)print\s+(?:the\s+)?(?:number|task)").unwrap(),
            Regex::new(r"(?i)echo\s+").unwrap(),
            Regex::new(r"(?i)(\d+)\s+(?:bash|shell)\s+task").unwrap(),
        ];

        let file_patterns = vec![
            Regex::new(r"(?i)copy\s+([^\s]+)\s+to\s+([^\s]+)").unwrap(),
            Regex::new(r"(?i)move\s+([^\s]+)\s+to\s+([^\s]+)").unwrap(),
            Regex::new(r"(?i)delete\s+([^\s]+)").unwrap(),
            Regex::new(r"(?i)remove\s+([^\s]+)").unwrap(),
            Regex::new(r"(?i)copy\s+files").unwrap(),
            Regex::new(r"(?i)backup\s+files").unwrap(),
            Regex::new(r"(?i)backup\s+results").unwrap(),
            Regex::new(r"(?i)copy\s+config\s+files").unwrap(),
        ];

        let http_patterns = vec![
            Regex::new(r"(?i)(?:send|make)\s+(?:a\s+)?(?:get|post|put|delete)\s+(?:request\s+)?to\s+([^\s]+)").unwrap(),
            Regex::new(r"(?i)(?:call|request)\s+([^\s]+)").unwrap(),
            Regex::new(r"(?i)webhook\s+to\s+([^\s]+)").unwrap(),
            Regex::new(r"(?i)send\s+notification").unwrap(),
            Regex::new(r"(?i)notify").unwrap(),
        ];

        let python_patterns = vec![
            Regex::new(r"(?i)run\s+python\s+(?:script\s+)?([^\s]+)").unwrap(),
            Regex::new(r"(?i)execute\s+([^\s]+\.py)").unwrap(),
            Regex::new(r"(?i)python\s+([^\s]+)").unwrap(),
        ];

        let rust_patterns = vec![
            Regex::new(r"(?i)(?:cargo\s+)?build\s+(?:the\s+)?(?:rust\s+)?project").unwrap(),
            Regex::new(r"(?i)(?:cargo\s+)?test\s+(?:the\s+)?(?:rust\s+)?project").unwrap(),
            Regex::new(r"(?i)(?:cargo\s+)?run\s+(?:the\s+)?(?:rust\s+)?project").unwrap(),
            Regex::new(r"(?i)compile\s+(?:the\s+)?(?:rust\s+)?project").unwrap(),
            Regex::new(r"(?i)run\s+tests").unwrap(),
            Regex::new(r"(?i)execute\s+tests").unwrap(),
        ];

        let jorm_patterns = vec![
            Regex::new(r"(?i)jorm\s+execute\s+([^\s]+)").unwrap(),
            Regex::new(r"(?i)jorm\s+generate\s+").unwrap(),
            Regex::new(r"(?i)jorm\s+server").unwrap(),
            Regex::new(r"(?i)jorm\s+schedule").unwrap(),
            Regex::new(r"(?i)run\s+(?:existing\s+)?(?:jorm\s+)?(?:work)?flow").unwrap(),
            Regex::new(r"(?i)execute\s+(?:existing\s+)?(?:jorm\s+)?(?:work)?flow").unwrap(),
            Regex::new(r"(?i)create\s+(?:new\s+)?dag").unwrap(),
            Regex::new(r"(?i)generate\s+(?:new\s+)?dag").unwrap(),
            Regex::new(r"(?i)start\s+(?:jorm\s+)?server").unwrap(),
        ];

        let dependency_patterns = vec![
            Regex::new(r"(?i)(?:then|after|afterwards)\s+").unwrap(),
            Regex::new(r"(?i)(?:first|initially)\s+").unwrap(),
            Regex::new(r"(?i)(?:finally|lastly)\s+").unwrap(),
        ];

        Ok(Self {
            shell_patterns,
            file_patterns,
            http_patterns,
            python_patterns,
            rust_patterns,
            jorm_patterns,
            dependency_patterns,
        })
    }

    pub async fn generate_dag(&self, description: &str) -> Result<String, JormError> {
        let tasks = self.parse_natural_language(description)?;
        let dag_content = self.generate_dag_syntax(tasks)?;

        // Validate the generated DAG
        self.validate_generated_dag(&dag_content)?;

        Ok(dag_content)
    }

    fn parse_natural_language(&self, description: &str) -> Result<Vec<ParsedTask>, JormError> {
        let mut tasks = Vec::new();

        // Check for patterns that indicate multiple similar tasks (like "10 bash tasks")
        if let Some(multiple_tasks) = self.parse_multiple_tasks(description)? {
            tasks.extend(multiple_tasks);
        } else {
            // Parse as individual sentences
            let sentences = self.split_into_sentences(description);

            for (index, sentence) in sentences.iter().enumerate() {
                if let Some(task) = self.parse_sentence(sentence, index)? {
                    tasks.push(task);
                }
            }

            // Add dependencies based on order and keywords for sentence-based parsing
            if !tasks.is_empty() {
                self.infer_dependencies(&mut tasks, &sentences);
            }
        }

        if tasks.is_empty() {
            return Err(JormError::NlpError(
                "Could not parse any tasks from the description".to_string(),
            ));
        }

        Ok(tasks)
    }

    fn parse_multiple_tasks(
        &self,
        description: &str,
    ) -> Result<Option<Vec<ParsedTask>>, JormError> {
        // Pattern for "N bash/shell tasks that do X"
        let multiple_pattern =
            Regex::new(r"(?i)(\d+)\s+(?:bash|shell)\s+task(?:s)?\s+that\s+(.+)").unwrap();

        if let Some(captures) = multiple_pattern.captures(description) {
            let count: usize = captures.get(1).unwrap().as_str().parse().unwrap_or(1);
            let action = captures.get(2).unwrap().as_str();

            let mut tasks = Vec::new();

            for i in 1..=count {
                let task_name = format!("task_{}", i);
                let mut parameters = HashMap::new();

                // Generate command based on the action
                let command = if action.contains("print") && action.contains("number") {
                    format!("echo 'Task number {}'", i)
                } else if action.contains("print") && action.contains("task") {
                    format!("echo 'This is task {}'", i)
                } else {
                    format!("echo '{} - Task {}'", action, i)
                };

                parameters.insert("command".to_string(), command);

                tasks.push(ParsedTask {
                    name: task_name,
                    task_type: "shell".to_string(),
                    parameters,
                    dependencies: Vec::new(),
                });
            }

            return Ok(Some(tasks));
        }

        // Pattern for "simple workflow with N tasks"
        let workflow_pattern =
            Regex::new(r"(?i)(?:simple\s+)?workflow\s+with\s+(\d+)\s+(?:bash|shell)\s+task")
                .unwrap();

        if let Some(captures) = workflow_pattern.captures(description) {
            let count: usize = captures.get(1).unwrap().as_str().parse().unwrap_or(1);

            let mut tasks = Vec::new();

            for i in 1..=count {
                let task_name = format!("task_{}", i);
                let mut parameters = HashMap::new();

                let command = if description.contains("print") && description.contains("number") {
                    format!("echo 'Task number {}'", i)
                } else {
                    format!("echo 'Executing task {}'", i)
                };

                parameters.insert("command".to_string(), command);

                tasks.push(ParsedTask {
                    name: task_name,
                    task_type: "shell".to_string(),
                    parameters,
                    dependencies: Vec::new(),
                });
            }

            return Ok(Some(tasks));
        }

        Ok(None)
    }

    fn split_into_sentences(&self, description: &str) -> Vec<String> {
        // Split on sentence-ending punctuation and conjunctions, but not on dots in filenames
        let sentence_regex =
            Regex::new(r"[,;]\s*|\s+(?:then|and|after|afterwards)\s+|\n+").unwrap();
        let sentences: Vec<String> = sentence_regex
            .split(description)
            .map(|s| s.trim().to_string())
            .filter(|s| !s.is_empty())
            .collect();

        // If no sentence separators found, treat the whole description as one sentence
        if sentences.len() <= 1 {
            vec![description.trim().to_string()]
        } else {
            sentences
        }
    }

    fn parse_sentence(
        &self,
        sentence: &str,
        index: usize,
    ) -> Result<Option<ParsedTask>, JormError> {
        let task_name = format!("task_{}", index + 1);

        // Try to match shell commands
        for pattern in &self.shell_patterns {
            if let Some(captures) = pattern.captures(sentence) {
                let command = captures.get(1).unwrap().as_str();
                let mut parameters = HashMap::new();
                parameters.insert("command".to_string(), command.to_string());

                return Ok(Some(ParsedTask {
                    name: task_name,
                    task_type: "shell".to_string(),
                    parameters,
                    dependencies: Vec::new(),
                }));
            }
        }

        // Try to match file operations
        for pattern in &self.file_patterns {
            if pattern.is_match(sentence) {
                let mut parameters = HashMap::new();

                if sentence.to_lowercase().contains("copy config files") {
                    // Copy config files task
                    parameters.insert("source".to_string(), "config.template".to_string());
                    parameters.insert("destination".to_string(), "config.prod".to_string());
                    return Ok(Some(ParsedTask {
                        name: task_name,
                        task_type: "file_copy".to_string(),
                        parameters,
                        dependencies: Vec::new(),
                    }));
                } else if sentence.to_lowercase().contains("backup results") {
                    // Backup results task
                    parameters.insert("source".to_string(), "./results/".to_string());
                    parameters.insert("destination".to_string(), "./backup/results/".to_string());
                    return Ok(Some(ParsedTask {
                        name: task_name,
                        task_type: "file_copy".to_string(),
                        parameters,
                        dependencies: Vec::new(),
                    }));
                } else if sentence.to_lowercase().contains("backup files") {
                    // Generic backup files task
                    parameters.insert("source".to_string(), "./data/".to_string());
                    parameters.insert("destination".to_string(), "./backup/".to_string());
                    return Ok(Some(ParsedTask {
                        name: task_name,
                        task_type: "file_copy".to_string(),
                        parameters,
                        dependencies: Vec::new(),
                    }));
                } else if sentence.to_lowercase().contains("copy files") {
                    // Generic copy files task
                    parameters.insert("source".to_string(), "*.txt".to_string());
                    parameters.insert("destination".to_string(), "./backup/".to_string());
                    return Ok(Some(ParsedTask {
                        name: task_name,
                        task_type: "file_copy".to_string(),
                        parameters,
                        dependencies: Vec::new(),
                    }));
                } else if let Some(captures) = pattern.captures(sentence) {
                    // Handle patterns with capture groups
                    if sentence.to_lowercase().contains("copy") && captures.len() >= 3 {
                        parameters.insert(
                            "source".to_string(),
                            captures.get(1).unwrap().as_str().to_string(),
                        );
                        parameters.insert(
                            "destination".to_string(),
                            captures.get(2).unwrap().as_str().to_string(),
                        );
                        return Ok(Some(ParsedTask {
                            name: task_name,
                            task_type: "file_copy".to_string(),
                            parameters,
                            dependencies: Vec::new(),
                        }));
                    } else if sentence.to_lowercase().contains("move") && captures.len() >= 3 {
                        parameters.insert(
                            "source".to_string(),
                            captures.get(1).unwrap().as_str().to_string(),
                        );
                        parameters.insert(
                            "destination".to_string(),
                            captures.get(2).unwrap().as_str().to_string(),
                        );
                        return Ok(Some(ParsedTask {
                            name: task_name,
                            task_type: "file_move".to_string(),
                            parameters,
                            dependencies: Vec::new(),
                        }));
                    } else if (sentence.to_lowercase().contains("delete")
                        || sentence.to_lowercase().contains("remove"))
                        && captures.len() >= 2
                    {
                        parameters.insert(
                            "path".to_string(),
                            captures.get(1).unwrap().as_str().to_string(),
                        );
                        return Ok(Some(ParsedTask {
                            name: task_name,
                            task_type: "file_delete".to_string(),
                            parameters,
                            dependencies: Vec::new(),
                        }));
                    }
                }
            }
        }

        // Try to match HTTP requests
        for pattern in &self.http_patterns {
            if let Some(captures) = pattern.captures(sentence) {
                let mut parameters = HashMap::new();

                if captures.len() >= 2 {
                    let url = captures.get(1).unwrap().as_str();
                    parameters.insert("url".to_string(), url.to_string());
                } else if sentence.to_lowercase().contains("notification")
                    || sentence.to_lowercase().contains("notify")
                {
                    // Generic notification
                    parameters.insert(
                        "url".to_string(),
                        "https://hooks.slack.com/webhook".to_string(),
                    );
                    parameters.insert(
                        "body".to_string(),
                        r#"{"text": "Task completed successfully"}"#.to_string(),
                    );
                }

                // Determine HTTP method from context
                let method = if sentence.to_lowercase().contains("post") {
                    "POST"
                } else if sentence.to_lowercase().contains("put") {
                    "PUT"
                } else if sentence.to_lowercase().contains("delete") {
                    "DELETE"
                } else if sentence.to_lowercase().contains("notification")
                    || sentence.to_lowercase().contains("notify")
                {
                    "POST"
                } else {
                    "GET"
                };
                parameters.insert("method".to_string(), method.to_string());

                return Ok(Some(ParsedTask {
                    name: task_name,
                    task_type: "http".to_string(),
                    parameters,
                    dependencies: Vec::new(),
                }));
            }
        }

        // Try to match Python scripts
        for pattern in &self.python_patterns {
            if let Some(captures) = pattern.captures(sentence) {
                let script = captures.get(1).unwrap().as_str();
                let mut parameters = HashMap::new();
                parameters.insert("script".to_string(), script.to_string());

                return Ok(Some(ParsedTask {
                    name: task_name,
                    task_type: "python".to_string(),
                    parameters,
                    dependencies: Vec::new(),
                }));
            }
        }

        // Try to match Rust/Cargo commands
        for pattern in &self.rust_patterns {
            if pattern.is_match(sentence) {
                let mut parameters = HashMap::new();

                let command = if sentence.to_lowercase().contains("build") {
                    "cargo build"
                } else if sentence.to_lowercase().contains("test")
                    || sentence.to_lowercase().contains("run tests")
                {
                    "cargo test"
                } else if sentence.to_lowercase().contains("run")
                    && !sentence.to_lowercase().contains("tests")
                {
                    "cargo run"
                } else {
                    "cargo test"
                };

                parameters.insert("command".to_string(), command.to_string());

                return Ok(Some(ParsedTask {
                    name: task_name,
                    task_type: "rust".to_string(),
                    parameters,
                    dependencies: Vec::new(),
                }));
            }
        }

        // Try to match Jorm commands
        for pattern in &self.jorm_patterns {
            if let Some(captures) = pattern.captures(sentence) {
                let mut parameters = HashMap::new();

                if sentence.to_lowercase().contains("execute") && captures.len() >= 2 {
                    // jorm execute <dag_file>
                    let dag_file = captures.get(1).unwrap().as_str();
                    parameters.insert("command".to_string(), "execute".to_string());
                    parameters.insert("dag_file".to_string(), dag_file.to_string());
                } else if sentence.to_lowercase().contains("create")
                    || sentence.to_lowercase().contains("generate")
                {
                    // jorm generate
                    parameters.insert("command".to_string(), "generate".to_string());
                    parameters.insert(
                        "description".to_string(),
                        "Build and test workflow".to_string(),
                    );
                } else if sentence.to_lowercase().contains("server") {
                    // jorm server
                    parameters.insert("command".to_string(), "server".to_string());
                } else if sentence.to_lowercase().contains("schedule") {
                    // jorm schedule
                    parameters.insert("command".to_string(), "schedule".to_string());
                } else if sentence.to_lowercase().contains("run")
                    || sentence.to_lowercase().contains("execute")
                {
                    // Run existing workflow
                    parameters.insert("command".to_string(), "execute".to_string());
                    parameters.insert("dag_file".to_string(), "existing_workflow.txt".to_string());
                } else {
                    // Default to execute
                    parameters.insert("command".to_string(), "execute".to_string());
                    parameters.insert("dag_file".to_string(), "workflow.txt".to_string());
                }

                return Ok(Some(ParsedTask {
                    name: task_name,
                    task_type: "jorm".to_string(),
                    parameters,
                    dependencies: Vec::new(),
                }));
            } else if pattern.is_match(sentence) {
                // Handle patterns without captures
                let mut parameters = HashMap::new();

                if sentence.to_lowercase().contains("create")
                    || sentence.to_lowercase().contains("generate")
                {
                    parameters.insert("command".to_string(), "generate".to_string());
                    parameters.insert("description".to_string(), "Generated workflow".to_string());
                } else if sentence.to_lowercase().contains("server") {
                    parameters.insert("command".to_string(), "server".to_string());
                } else if sentence.to_lowercase().contains("run")
                    || sentence.to_lowercase().contains("execute")
                {
                    parameters.insert("command".to_string(), "execute".to_string());
                    parameters.insert("dag_file".to_string(), "existing_workflow.txt".to_string());
                } else {
                    parameters.insert("command".to_string(), "execute".to_string());
                    parameters.insert("dag_file".to_string(), "workflow.txt".to_string());
                }

                return Ok(Some(ParsedTask {
                    name: task_name,
                    task_type: "jorm".to_string(),
                    parameters,
                    dependencies: Vec::new(),
                }));
            }
        }

        Ok(None)
    }

    fn infer_dependencies(&self, tasks: &mut [ParsedTask], sentences: &[String]) {
        // If we have multiple tasks from a single description, check if they should be sequential
        if tasks.len() > 1 && sentences.len() == 1 {
            let description = &sentences[0].to_lowercase();

            // For simple workflows, make tasks sequential by default
            if description.contains("workflow") || description.contains("sequential") {
                for i in 1..tasks.len() {
                    let prev_name = tasks[i - 1].name.clone();
                    tasks[i].dependencies.push(prev_name);
                }
                return;
            }

            // For parallel tasks (like "10 bash tasks"), don't add dependencies
            if description.contains("parallel") || description.contains("independent") {
                return;
            }

            // Default for multiple similar tasks: make them sequential
            for i in 1..tasks.len() {
                let prev_name = tasks[i - 1].name.clone();
                tasks[i].dependencies.push(prev_name);
            }
            return;
        }

        // Original logic for multiple sentences
        for (i, sentence) in sentences.iter().enumerate() {
            if i > 0 && i < tasks.len() {
                // Check for dependency keywords
                let mut has_explicit_dependency = false;
                for pattern in &self.dependency_patterns {
                    if pattern.is_match(sentence) {
                        if sentence.to_lowercase().contains("then")
                            || sentence.to_lowercase().contains("after")
                        {
                            // Current task depends on previous task
                            if i > 0 {
                                let prev_name = tasks[i - 1].name.clone();
                                tasks[i].dependencies.push(prev_name);
                                has_explicit_dependency = true;
                            }
                        }
                        break;
                    }
                }

                // Default: if no explicit dependency keywords, assume sequential execution
                if !has_explicit_dependency && tasks[i].dependencies.is_empty() && i > 0 {
                    let prev_name = tasks[i - 1].name.clone();
                    tasks[i].dependencies.push(prev_name);
                }
            }
        }
    }

    fn generate_dag_syntax(&self, tasks: Vec<ParsedTask>) -> Result<String, JormError> {
        let mut dag_content = String::new();
        dag_content.push_str("# Generated DAG from natural language\n\n");

        for task in tasks {
            dag_content.push_str(&format!("task {} {{\n", task.name));
            dag_content.push_str(&format!("    type: {}\n", task.task_type));

            // Handle special parameters for jorm tasks
            if task.task_type == "jorm" {
                if let Some(command) = task.parameters.get("command") {
                    dag_content.push_str(&format!("    command: \"{}\"\n", command));
                }
                if let Some(dag_file) = task.parameters.get("dag_file") {
                    dag_content.push_str(&format!("    args: [\"{}\"]\n", dag_file));
                } else if let Some(description) = task.parameters.get("description") {
                    dag_content.push_str(&format!(
                        "    args: [\"--description\", \"{}\"]\n",
                        description
                    ));
                }
            } else {
                // Handle regular parameters
                for (key, value) in &task.parameters {
                    dag_content.push_str(&format!("    {}: \"{}\"\n", key, value));
                }
            }

            if !task.dependencies.is_empty() {
                dag_content.push_str("    depends_on: [");
                dag_content.push_str(&task.dependencies.join(", "));
                dag_content.push_str("]\n");
            }

            dag_content.push_str("}\n\n");
        }

        Ok(dag_content)
    }

    pub fn validate_generated_dag(&self, dag_content: &str) -> Result<(), JormError> {
        // Use the existing DAG parser to validate the generated content
        let parser = DagParser::new();
        match parser.parse_content(dag_content) {
            Ok(_) => Ok(()),
            Err(e) => Err(JormError::NlpError(format!(
                "Generated DAG is invalid: {}",
                e
            ))),
        }
    }

    pub async fn generate_dag_with_preview(
        &self,
        description: &str,
    ) -> Result<DagPreview, JormError> {
        let dag_content = self.generate_dag(description).await?;
        let parser = DagParser::new();
        let dag = parser.parse_content(&dag_content)?;

        Ok(DagPreview {
            original_description: description.to_string(),
            generated_dag_content: dag_content,
            task_count: dag.tasks.len(),
            task_names: dag.tasks.keys().cloned().collect(),
            has_dependencies: dag.dependencies.values().any(|deps| !deps.is_empty()),
        })
    }

    pub fn edit_generated_dag(
        &self,
        dag_content: &str,
        edits: Vec<DagEdit>,
    ) -> Result<String, JormError> {
        let mut modified_content = dag_content.to_string();

        for edit in edits {
            match edit {
                DagEdit::RenameTask { old_name, new_name } => {
                    modified_content = modified_content
                        .replace(&format!("task {}", old_name), &format!("task {}", new_name));
                    modified_content = modified_content.replace(&old_name, &new_name);
                }
                DagEdit::ModifyParameter {
                    task_name,
                    parameter,
                    new_value,
                } => {
                    // Simple regex-based parameter modification
                    let pattern =
                        format!(r#"(task {}\s*\{{[^}}]*){}: "[^"]*""#, task_name, parameter);
                    let replacement = format!(r#"$1{}: "{}""#, parameter, new_value);
                    if let Ok(re) = Regex::new(&pattern) {
                        modified_content = re
                            .replace(&modified_content, replacement.as_str())
                            .to_string();
                    }
                }
                DagEdit::AddDependency {
                    task_name,
                    dependency,
                } => {
                    // Add dependency to existing depends_on or create new one
                    let task_pattern = format!(r"(task {}\s*\{{[^}}]*)", task_name);
                    if let Ok(re) = Regex::new(&task_pattern) {
                        if modified_content.contains(&format!("task {}", task_name))
                            && modified_content.contains("depends_on:")
                        {
                            // Add to existing depends_on
                            let depends_pattern =
                                format!(r"(task {}\s*\{{[^}}]*depends_on: \[[^\]]*)", task_name);
                            if let Ok(depends_re) = Regex::new(&depends_pattern) {
                                let replacement = format!("$1, {}", dependency);
                                modified_content = depends_re
                                    .replace(&modified_content, replacement.as_str())
                                    .to_string();
                            }
                        } else {
                            // Add new depends_on line
                            let replacement = format!("$1\n    depends_on: [{}]", dependency);
                            modified_content = re
                                .replace(&modified_content, replacement.as_str())
                                .to_string();
                        }
                    }
                }
            }
        }

        // Validate the modified DAG
        self.validate_generated_dag(&modified_content)?;

        Ok(modified_content)
    }
}

#[derive(Debug, Clone)]
pub struct DagPreview {
    pub original_description: String,
    pub generated_dag_content: String,
    pub task_count: usize,
    pub task_names: Vec<String>,
    pub has_dependencies: bool,
}

#[derive(Debug, Clone)]
pub enum DagEdit {
    RenameTask {
        old_name: String,
        new_name: String,
    },
    ModifyParameter {
        task_name: String,
        parameter: String,
        new_value: String,
    },
    AddDependency {
        task_name: String,
        dependency: String,
    },
}

impl DagPreview {
    pub fn summary(&self) -> String {
        format!(
            "Generated DAG from: \"{}\"\n\
             Tasks: {} ({})\n\
             Dependencies: {}\n\n\
             DAG Content:\n{}",
            self.original_description,
            self.task_count,
            self.task_names.join(", "),
            if self.has_dependencies { "Yes" } else { "No" },
            self.generated_dag_content
        )
    }
}