periplon 0.2.0

Rust SDK for building multi-agent AI workflows and automation
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
//! Test all loop types: for_each, repeat, while, repeat_until

use periplon_sdk::dsl::parser::parse_workflow_file;
use periplon_sdk::dsl::schema::{CollectionSource, ConditionSpec, LoopSpec};
use serde_json::json;

#[tokio::test]
async fn test_foreach_inline_collection() {
    let workflow_path = "tests/fixtures/loop_types.yaml";
    let workflow = parse_workflow_file(workflow_path).expect("Failed to parse workflow");

    let task = workflow
        .tasks
        .get("test_foreach_inline")
        .expect("test_foreach_inline should exist");

    // Verify loop specification
    assert!(task.loop_spec.is_some(), "Task should have loop spec");

    match task.loop_spec.as_ref().unwrap() {
        LoopSpec::ForEach {
            collection,
            iterator,
            parallel,
            max_parallel,
        } => {
            // Verify iterator name
            assert_eq!(iterator, "fruit");

            // Verify not parallel
            assert!(!(*parallel));
            assert!(max_parallel.is_none());

            // Verify inline collection
            match collection {
                CollectionSource::Inline { items } => {
                    assert_eq!(items.len(), 4);
                    assert_eq!(items[0], json!("apple"));
                    assert_eq!(items[1], json!("banana"));
                    assert_eq!(items[2], json!("cherry"));
                    assert_eq!(items[3], json!("date"));
                }
                _ => panic!("Expected inline collection"),
            }
        }
        _ => panic!("Expected ForEach loop"),
    }

    // Verify subtasks exist
    assert_eq!(task.subtasks.len(), 1);
    assert!(task.subtasks[0].contains_key("process_item"));

    println!("✓ for_each with inline collection test passed");
}

#[tokio::test]
async fn test_foreach_range_collection() {
    let workflow_path = "tests/fixtures/loop_types.yaml";
    let workflow = parse_workflow_file(workflow_path).expect("Failed to parse workflow");

    let task = workflow
        .tasks
        .get("test_foreach_range")
        .expect("test_foreach_range should exist");

    match task.loop_spec.as_ref().unwrap() {
        LoopSpec::ForEach {
            collection,
            iterator,
            parallel,
            ..
        } => {
            assert_eq!(iterator, "number");
            assert!(!(*parallel));

            match collection {
                CollectionSource::Range { start, end, step } => {
                    assert_eq!(*start, 1);
                    assert_eq!(*end, 5);
                    assert_eq!(*step, Some(1));
                }
                _ => panic!("Expected range collection"),
            }
        }
        _ => panic!("Expected ForEach loop"),
    }

    println!("✓ for_each with range collection test passed");
}

#[tokio::test]
async fn test_foreach_parallel() {
    let workflow_path = "tests/fixtures/loop_types.yaml";
    let workflow = parse_workflow_file(workflow_path).expect("Failed to parse workflow");

    let task = workflow
        .tasks
        .get("test_foreach_parallel")
        .expect("test_foreach_parallel should exist");

    match task.loop_spec.as_ref().unwrap() {
        LoopSpec::ForEach {
            collection,
            iterator,
            parallel,
            max_parallel,
        } => {
            assert_eq!(iterator, "task_name");
            assert!(*parallel, "Should be parallel");
            assert_eq!(*max_parallel, Some(2), "Should have max_parallel=2");

            match collection {
                CollectionSource::Inline { items } => {
                    assert_eq!(items.len(), 4);
                }
                _ => panic!("Expected inline collection"),
            }
        }
        _ => panic!("Expected ForEach loop"),
    }

    println!("✓ for_each with parallel execution test passed");
}

#[tokio::test]
async fn test_repeat_loop() {
    let workflow_path = "tests/fixtures/loop_types.yaml";
    let workflow = parse_workflow_file(workflow_path).expect("Failed to parse workflow");

    let task = workflow
        .tasks
        .get("test_repeat")
        .expect("test_repeat should exist");

    assert!(task.loop_spec.is_some());

    match task.loop_spec.as_ref().unwrap() {
        LoopSpec::Repeat {
            count,
            iterator,
            parallel,
            max_parallel,
        } => {
            assert_eq!(*count, 3, "Should repeat 3 times");
            assert_eq!(iterator, &Some("index".to_string()));
            assert!(!(*parallel));
            assert!(max_parallel.is_none());
        }
        _ => panic!("Expected Repeat loop"),
    }

    println!("✓ repeat loop test passed");
}

#[tokio::test]
async fn test_repeat_parallel() {
    let workflow_path = "tests/fixtures/loop_types.yaml";
    let workflow = parse_workflow_file(workflow_path).expect("Failed to parse workflow");

    let task = workflow
        .tasks
        .get("test_repeat_parallel")
        .expect("test_repeat_parallel should exist");

    match task.loop_spec.as_ref().unwrap() {
        LoopSpec::Repeat {
            count,
            iterator,
            parallel,
            max_parallel,
        } => {
            assert_eq!(*count, 4);
            assert_eq!(iterator, &Some("batch".to_string()));
            assert!(*parallel);
            assert_eq!(*max_parallel, Some(2));
        }
        _ => panic!("Expected Repeat loop"),
    }

    println!("✓ repeat with parallel execution test passed");
}

#[tokio::test]
async fn test_while_loop() {
    let workflow_path = "tests/fixtures/loop_types.yaml";
    let workflow = parse_workflow_file(workflow_path).expect("Failed to parse workflow");

    let task = workflow
        .tasks
        .get("test_while")
        .expect("test_while should exist");

    assert!(task.loop_spec.is_some());

    match task.loop_spec.as_ref().unwrap() {
        LoopSpec::While {
            condition,
            max_iterations,
            iteration_variable,
            delay_between_secs,
        } => {
            assert_eq!(*max_iterations, 5, "Should have max 5 iterations");
            assert_eq!(iteration_variable, &Some("iteration".to_string()));
            assert_eq!(*delay_between_secs, Some(1));

            // Verify condition
            assert!(matches!(condition.as_ref(), ConditionSpec::Single(_)));
        }
        _ => panic!("Expected While loop"),
    }

    println!("✓ while loop test passed");
}

#[tokio::test]
async fn test_repeat_until_loop() {
    let workflow_path = "tests/fixtures/loop_types.yaml";
    let workflow = parse_workflow_file(workflow_path).expect("Failed to parse workflow");

    let task = workflow
        .tasks
        .get("test_repeat_until")
        .expect("test_repeat_until should exist");

    assert!(task.loop_spec.is_some());

    match task.loop_spec.as_ref().unwrap() {
        LoopSpec::RepeatUntil {
            condition,
            min_iterations,
            max_iterations,
            iteration_variable,
            delay_between_secs,
        } => {
            assert_eq!(*min_iterations, Some(1));
            assert_eq!(*max_iterations, 3);
            assert_eq!(iteration_variable, &Some("attempt".to_string()));
            assert_eq!(*delay_between_secs, Some(1));

            // Verify condition
            assert!(matches!(condition.as_ref(), ConditionSpec::Single(_)));
        }
        _ => panic!("Expected RepeatUntil loop"),
    }

    println!("✓ repeat_until loop test passed");
}

#[tokio::test]
async fn test_loop_with_break_condition() {
    let workflow_path = "tests/fixtures/loop_types.yaml";
    let workflow = parse_workflow_file(workflow_path).expect("Failed to parse workflow");

    let task = workflow
        .tasks
        .get("test_loop_break")
        .expect("test_loop_break should exist");

    // Verify loop control
    assert!(task.loop_control.is_some());
    let control = task.loop_control.as_ref().unwrap();

    assert!(
        control.break_condition.is_some(),
        "Should have break condition"
    );
    assert!(control.collect_results);
    assert_eq!(control.result_key, Some("break_results".to_string()));

    println!("✓ loop with break condition test passed");
}

#[tokio::test]
async fn test_loop_with_continue_condition() {
    let workflow_path = "tests/fixtures/loop_types.yaml";
    let workflow = parse_workflow_file(workflow_path).expect("Failed to parse workflow");

    let task = workflow
        .tasks
        .get("test_loop_continue")
        .expect("test_loop_continue should exist");

    // Verify loop control
    assert!(task.loop_control.is_some());
    let control = task.loop_control.as_ref().unwrap();

    assert!(
        control.continue_condition.is_some(),
        "Should have continue condition"
    );
    assert!(control.collect_results);
    assert_eq!(control.result_key, Some("continue_results".to_string()));

    println!("✓ loop with continue condition test passed");
}

#[tokio::test]
async fn test_loop_with_result_collection() {
    let workflow_path = "tests/fixtures/loop_types.yaml";
    let workflow = parse_workflow_file(workflow_path).expect("Failed to parse workflow");

    let task = workflow
        .tasks
        .get("test_loop_results")
        .expect("test_loop_results should exist");

    // Verify loop control
    assert!(task.loop_control.is_some());
    let control = task.loop_control.as_ref().unwrap();

    assert!(control.collect_results);
    assert_eq!(control.result_key, Some("collected_items".to_string()));

    // Verify subtask has outputs
    let subtask = task.subtasks[0].get("collect_task").unwrap();
    assert_eq!(subtask.outputs.len(), 1);
    assert!(subtask.outputs.contains_key("item_result"));

    println!("✓ loop with result collection test passed");
}

#[tokio::test]
async fn test_nested_loops() {
    let workflow_path = "tests/fixtures/loop_types.yaml";
    let workflow = parse_workflow_file(workflow_path).expect("Failed to parse workflow");

    let outer_task = workflow
        .tasks
        .get("test_nested_loops")
        .expect("test_nested_loops should exist");

    // Verify outer loop
    assert!(outer_task.loop_spec.is_some());
    match outer_task.loop_spec.as_ref().unwrap() {
        LoopSpec::Repeat {
            count, iterator, ..
        } => {
            assert_eq!(*count, 2);
            assert_eq!(iterator, &Some("outer".to_string()));
        }
        _ => panic!("Expected Repeat loop for outer"),
    }

    // Verify inner loop subtask
    assert_eq!(outer_task.subtasks.len(), 1);
    let inner_task = outer_task.subtasks[0].get("inner_loop").unwrap();

    assert!(inner_task.loop_spec.is_some());
    match inner_task.loop_spec.as_ref().unwrap() {
        LoopSpec::ForEach { iterator, .. } => {
            assert_eq!(iterator, "inner");
        }
        _ => panic!("Expected ForEach loop for inner"),
    }

    // Verify innermost subtask
    assert_eq!(inner_task.subtasks.len(), 1);
    assert!(inner_task.subtasks[0].contains_key("nested_task"));

    println!("✓ nested loops test passed");
}

#[tokio::test]
async fn test_loop_with_timeout() {
    let workflow_path = "tests/fixtures/loop_types.yaml";
    let workflow = parse_workflow_file(workflow_path).expect("Failed to parse workflow");

    let task = workflow
        .tasks
        .get("test_loop_timeout")
        .expect("test_loop_timeout should exist");

    // Verify loop control with timeout
    assert!(task.loop_control.is_some());
    let control = task.loop_control.as_ref().unwrap();

    assert_eq!(control.timeout_secs, Some(30));
    assert_eq!(control.checkpoint_interval, Some(10));

    println!("✓ loop with timeout test passed");
}

#[tokio::test]
async fn test_loop_with_complex_condition() {
    let workflow_path = "tests/fixtures/loop_types.yaml";
    let workflow = parse_workflow_file(workflow_path).expect("Failed to parse workflow");

    let task = workflow
        .tasks
        .get("test_complex_condition")
        .expect("test_complex_condition should exist");

    // Verify loop control with complex condition
    assert!(task.loop_control.is_some());
    let control = task.loop_control.as_ref().unwrap();

    assert!(control.break_condition.is_some());

    // Verify it's an AND condition
    match control.break_condition.as_ref().unwrap() {
        ConditionSpec::And { and } => {
            assert_eq!(and.len(), 2, "Should have 2 conditions in AND");
        }
        _ => panic!("Expected AND condition"),
    }

    println!("✓ loop with complex condition test passed");
}

#[tokio::test]
async fn test_loop_variable_substitution() {
    let workflow_path = "tests/fixtures/loop_types.yaml";
    let workflow = parse_workflow_file(workflow_path).expect("Failed to parse workflow");

    // Test variable substitution in for_each
    let foreach_task = workflow.tasks.get("test_foreach_inline").unwrap();
    let subtask = foreach_task.subtasks[0].get("process_item").unwrap();
    assert!(subtask.description.contains("${loop.fruit}"));
    assert!(subtask.output.as_ref().unwrap().contains("${loop.fruit}"));

    // Test variable substitution in repeat
    let repeat_task = workflow.tasks.get("test_repeat").unwrap();
    let subtask = repeat_task.subtasks[0].get("repeat_task").unwrap();
    assert!(subtask.description.contains("${loop.index}"));
    assert!(subtask.output.as_ref().unwrap().contains("${loop.index}"));

    // Test variable substitution in while
    let while_task = workflow.tasks.get("test_while").unwrap();
    let subtask = while_task.subtasks[0].get("while_task").unwrap();
    assert!(subtask.description.contains("${loop.iteration}"));

    // Test variable substitution in nested loops
    let nested_task = workflow.tasks.get("test_nested_loops").unwrap();
    let inner_loop = nested_task.subtasks[0].get("inner_loop").unwrap();
    let innermost = inner_loop.subtasks[0].get("nested_task").unwrap();
    assert!(innermost.description.contains("${loop.outer}"));
    assert!(innermost.description.contains("${loop.inner}"));
    assert!(innermost.output.as_ref().unwrap().contains("${loop.outer}"));
    assert!(innermost.output.as_ref().unwrap().contains("${loop.inner}"));

    println!("✓ loop variable substitution test passed");
}

#[tokio::test]
async fn test_all_loop_types_present() {
    let workflow_path = "tests/fixtures/loop_types.yaml";
    let workflow = parse_workflow_file(workflow_path).expect("Failed to parse workflow");

    // Count each loop type
    let mut foreach_count = 0;
    let mut repeat_count = 0;
    let mut while_count = 0;
    let mut repeat_until_count = 0;

    for task in workflow.tasks.values() {
        if let Some(loop_spec) = &task.loop_spec {
            match loop_spec {
                LoopSpec::ForEach { .. } => foreach_count += 1,
                LoopSpec::Repeat { .. } => repeat_count += 1,
                LoopSpec::While { .. } => while_count += 1,
                LoopSpec::RepeatUntil { .. } => repeat_until_count += 1,
            }
        }
    }

    assert!(foreach_count >= 3, "Should have at least 3 for_each loops");
    assert!(repeat_count >= 2, "Should have at least 2 repeat loops");
    assert_eq!(while_count, 1, "Should have 1 while loop");
    assert_eq!(repeat_until_count, 1, "Should have 1 repeat_until loop");

    println!("✓ All loop types present test passed");
    println!("  - for_each loops: {}", foreach_count);
    println!("  - repeat loops: {}", repeat_count);
    println!("  - while loops: {}", while_count);
    println!("  - repeat_until loops: {}", repeat_until_count);
}

#[tokio::test]
async fn test_loop_max_parallel() {
    let workflow_path = "tests/fixtures/loop_types.yaml";
    let workflow = parse_workflow_file(workflow_path).expect("Failed to parse workflow");

    // Test for_each with max_parallel
    let foreach_parallel = workflow.tasks.get("test_foreach_parallel").unwrap();
    if let Some(LoopSpec::ForEach { .. }) = &foreach_parallel.loop_spec {
        let max_par = foreach_parallel.loop_spec.as_ref().unwrap().max_parallel();
        assert_eq!(max_par, Some(2), "for_each should have max_parallel=2");
    }

    // Test repeat with max_parallel
    let repeat_parallel = workflow.tasks.get("test_repeat_parallel").unwrap();
    if let Some(LoopSpec::Repeat { .. }) = &repeat_parallel.loop_spec {
        let max_par = repeat_parallel.loop_spec.as_ref().unwrap().max_parallel();
        assert_eq!(max_par, Some(2), "repeat should have max_parallel=2");
    }

    println!("✓ loop max_parallel test passed");
}