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
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
//! Mock Storage for Testing
//!
//! Provides a configurable in-memory storage implementation for testing worker
//! processing, execution tracking, and workflow management without external dependencies.

#[cfg(feature = "server")]
use crate::dsl::schema::DSLWorkflow;
#[cfg(feature = "server")]
use crate::server::storage::{
    Checkpoint, CheckpointStorage, Execution, ExecutionFilter, ExecutionLog, ExecutionStatus,
    ExecutionStorage, Result, Schedule, ScheduleFilter, ScheduleRun, ScheduleStorage, StorageError,
    WorkflowFilter, WorkflowMetadata, WorkflowStorage,
};
#[cfg(feature = "server")]
use async_trait::async_trait;
#[cfg(feature = "server")]
use std::collections::HashMap;
#[cfg(feature = "server")]
use std::sync::{Arc, Mutex};
#[cfg(feature = "server")]
use uuid::Uuid;

#[cfg(feature = "server")]
#[derive(Clone)]
pub struct MockStorage {
    state: Arc<Mutex<StorageState>>,
}

#[cfg(feature = "server")]
struct StorageState {
    workflows: HashMap<Uuid, (DSLWorkflow, WorkflowMetadata)>,
    executions: HashMap<Uuid, Execution>,
    execution_logs: HashMap<Uuid, Vec<ExecutionLog>>,
    checkpoints: HashMap<Uuid, Checkpoint>,
    schedules: HashMap<Uuid, Schedule>,
    schedule_runs: HashMap<Uuid, Vec<ScheduleRun>>,
    should_fail_get: bool,
    should_fail_store: bool,
}

#[cfg(feature = "server")]
impl MockStorage {
    pub fn new() -> Self {
        Self {
            state: Arc::new(Mutex::new(StorageState {
                workflows: HashMap::new(),
                executions: HashMap::new(),
                execution_logs: HashMap::new(),
                checkpoints: HashMap::new(),
                schedules: HashMap::new(),
                schedule_runs: HashMap::new(),
                should_fail_get: false,
                should_fail_store: false,
            })),
        }
    }

    /// Configure storage to fail get operations
    pub fn fail_get(&self) {
        let mut state = self.state.lock().unwrap();
        state.should_fail_get = true;
    }

    /// Configure storage to fail store operations
    pub fn fail_store(&self) {
        let mut state = self.state.lock().unwrap();
        state.should_fail_store = true;
    }

    /// Get number of stored workflows
    pub fn workflow_count(&self) -> usize {
        self.state.lock().unwrap().workflows.len()
    }

    /// Get number of stored executions
    pub fn execution_count(&self) -> usize {
        self.state.lock().unwrap().executions.len()
    }

    /// Get all executions
    pub fn get_all_executions(&self) -> Vec<Execution> {
        self.state
            .lock()
            .unwrap()
            .executions
            .values()
            .cloned()
            .collect()
    }

    /// Get executions by status
    pub fn get_executions_by_status(&self, status: ExecutionStatus) -> Vec<Execution> {
        self.state
            .lock()
            .unwrap()
            .executions
            .values()
            .filter(|e| e.status == status)
            .cloned()
            .collect()
    }

    /// Clear all state
    pub fn clear(&self) {
        let mut state = self.state.lock().unwrap();
        state.workflows.clear();
        state.executions.clear();
        state.execution_logs.clear();
        state.checkpoints.clear();
        state.schedules.clear();
        state.schedule_runs.clear();
        state.should_fail_get = false;
        state.should_fail_store = false;
    }

    /// Get number of stored schedules
    pub fn schedule_count(&self) -> usize {
        self.state.lock().unwrap().schedules.len()
    }
}

#[cfg(feature = "server")]
impl Default for MockStorage {
    fn default() -> Self {
        Self::new()
    }
}

#[cfg(feature = "server")]
#[async_trait]
impl WorkflowStorage for MockStorage {
    async fn store_workflow(
        &self,
        workflow: &DSLWorkflow,
        metadata: &WorkflowMetadata,
    ) -> Result<Uuid> {
        let mut state = self.state.lock().unwrap();

        if state.should_fail_store {
            return Err(StorageError::IoError("Store failure".to_string()));
        }

        state
            .workflows
            .insert(metadata.id, (workflow.clone(), metadata.clone()));
        Ok(metadata.id)
    }

    async fn get_workflow(&self, id: Uuid) -> Result<Option<(DSLWorkflow, WorkflowMetadata)>> {
        let state = self.state.lock().unwrap();

        if state.should_fail_get {
            return Err(StorageError::IoError("Get failure".to_string()));
        }

        Ok(state.workflows.get(&id).cloned())
    }

    async fn update_workflow(
        &self,
        id: Uuid,
        workflow: &DSLWorkflow,
        metadata: &WorkflowMetadata,
    ) -> Result<()> {
        let mut state = self.state.lock().unwrap();

        if state.should_fail_store {
            return Err(StorageError::IoError("Update failure".to_string()));
        }

        if let std::collections::hash_map::Entry::Occupied(mut e) = state.workflows.entry(id) {
            e.insert((workflow.clone(), metadata.clone()));
            Ok(())
        } else {
            Err(StorageError::NotFound(format!("Workflow {} not found", id)))
        }
    }

    async fn delete_workflow(&self, id: Uuid) -> Result<()> {
        let mut state = self.state.lock().unwrap();

        if state.workflows.remove(&id).is_some() {
            Ok(())
        } else {
            Err(StorageError::NotFound(format!("Workflow {} not found", id)))
        }
    }

    async fn list_workflows(
        &self,
        filter: &WorkflowFilter,
    ) -> Result<Vec<(DSLWorkflow, WorkflowMetadata)>> {
        let state = self.state.lock().unwrap();

        let mut workflows: Vec<_> = state.workflows.values().cloned().collect();

        // Apply filters
        if let Some(ref name) = filter.name {
            workflows.retain(|(_, m)| m.name == *name);
        }

        if let Some(is_active) = filter.is_active {
            workflows.retain(|(_, m)| m.is_active == is_active);
        }

        if let Some(limit) = filter.limit {
            workflows.truncate(limit);
        }

        Ok(workflows)
    }

    async fn get_workflow_version(
        &self,
        id: Uuid,
        version: &str,
    ) -> Result<Option<(DSLWorkflow, WorkflowMetadata)>> {
        let state = self.state.lock().unwrap();

        Ok(state
            .workflows
            .get(&id)
            .filter(|(_, m)| m.version == version)
            .cloned())
    }

    async fn store_workflow_version(
        &self,
        workflow: &DSLWorkflow,
        metadata: &WorkflowMetadata,
    ) -> Result<()> {
        self.store_workflow(workflow, metadata).await?;
        Ok(())
    }
}

#[cfg(feature = "server")]
#[async_trait]
impl ExecutionStorage for MockStorage {
    async fn store_execution(&self, execution: &Execution) -> Result<Uuid> {
        let mut state = self.state.lock().unwrap();

        if state.should_fail_store {
            return Err(StorageError::IoError("Store execution failure".to_string()));
        }

        state.executions.insert(execution.id, execution.clone());
        Ok(execution.id)
    }

    async fn get_execution(&self, id: Uuid) -> Result<Option<Execution>> {
        let state = self.state.lock().unwrap();

        if state.should_fail_get {
            return Err(StorageError::IoError("Get execution failure".to_string()));
        }

        Ok(state.executions.get(&id).cloned())
    }

    async fn update_execution(&self, id: Uuid, execution: &Execution) -> Result<()> {
        let mut state = self.state.lock().unwrap();

        if state.should_fail_store {
            return Err(StorageError::IoError(
                "Update execution failure".to_string(),
            ));
        }

        if let std::collections::hash_map::Entry::Occupied(mut e) = state.executions.entry(id) {
            e.insert(execution.clone());
            Ok(())
        } else {
            Err(StorageError::NotFound(format!(
                "Execution {} not found",
                id
            )))
        }
    }

    async fn list_executions(&self, filter: &ExecutionFilter) -> Result<Vec<Execution>> {
        let state = self.state.lock().unwrap();

        let mut executions: Vec<_> = state.executions.values().cloned().collect();

        // Apply filters
        if let Some(workflow_id) = filter.workflow_id {
            executions.retain(|e| e.workflow_id == workflow_id);
        }

        if let Some(ref status) = filter.status {
            executions.retain(|e| e.status == *status);
        }

        if let Some(ref triggered_by) = filter.triggered_by {
            executions.retain(|e| e.triggered_by.as_ref() == Some(triggered_by));
        }

        if let Some(limit) = filter.limit {
            executions.truncate(limit);
        }

        Ok(executions)
    }

    async fn store_execution_log(&self, log: &ExecutionLog) -> Result<()> {
        let mut state = self.state.lock().unwrap();
        let logs = state.execution_logs.entry(log.execution_id).or_default();
        logs.push(log.clone());
        Ok(())
    }

    async fn get_execution_logs(
        &self,
        execution_id: Uuid,
        _limit: Option<usize>,
    ) -> Result<Vec<ExecutionLog>> {
        let state = self.state.lock().unwrap();
        Ok(state
            .execution_logs
            .get(&execution_id)
            .cloned()
            .unwrap_or_default())
    }

    async fn delete_execution(&self, id: Uuid) -> Result<()> {
        let mut state = self.state.lock().unwrap();

        if state.executions.remove(&id).is_some() {
            state.execution_logs.remove(&id);
            Ok(())
        } else {
            Err(StorageError::NotFound(format!(
                "Execution {} not found",
                id
            )))
        }
    }
}

#[cfg(feature = "server")]
#[async_trait]
impl CheckpointStorage for MockStorage {
    async fn store_checkpoint(&self, checkpoint: &Checkpoint) -> Result<Uuid> {
        let mut state = self.state.lock().unwrap();

        if state.should_fail_store {
            return Err(StorageError::IoError("Save checkpoint failure".to_string()));
        }

        state.checkpoints.insert(checkpoint.id, checkpoint.clone());
        Ok(checkpoint.id)
    }

    async fn get_checkpoint(&self, execution_id: Uuid, name: &str) -> Result<Option<Checkpoint>> {
        let state = self.state.lock().unwrap();

        if state.should_fail_get {
            return Err(StorageError::IoError("Get checkpoint failure".to_string()));
        }

        Ok(state
            .checkpoints
            .values()
            .find(|c| c.execution_id == execution_id && c.checkpoint_name == name)
            .cloned())
    }

    async fn list_checkpoints(&self, execution_id: Uuid) -> Result<Vec<Checkpoint>> {
        let state = self.state.lock().unwrap();

        Ok(state
            .checkpoints
            .values()
            .filter(|c| c.execution_id == execution_id)
            .cloned()
            .collect())
    }

    async fn delete_checkpoint(&self, id: Uuid) -> Result<()> {
        let mut state = self.state.lock().unwrap();
        state.checkpoints.remove(&id);
        Ok(())
    }
}

#[cfg(feature = "server")]
#[async_trait]
impl ScheduleStorage for MockStorage {
    async fn store_schedule(&self, schedule: &Schedule) -> Result<Uuid> {
        let mut state = self.state.lock().unwrap();

        if state.should_fail_store {
            return Err(StorageError::IoError("Store schedule failure".to_string()));
        }

        state.schedules.insert(schedule.id, schedule.clone());
        Ok(schedule.id)
    }

    async fn get_schedule(&self, id: Uuid) -> Result<Option<Schedule>> {
        let state = self.state.lock().unwrap();

        if state.should_fail_get {
            return Err(StorageError::IoError("Get schedule failure".to_string()));
        }

        Ok(state.schedules.get(&id).cloned())
    }

    async fn update_schedule(&self, id: Uuid, schedule: &Schedule) -> Result<()> {
        let mut state = self.state.lock().unwrap();

        if state.should_fail_store {
            return Err(StorageError::IoError("Update schedule failure".to_string()));
        }

        if let std::collections::hash_map::Entry::Occupied(mut e) = state.schedules.entry(id) {
            e.insert(schedule.clone());
            Ok(())
        } else {
            Err(StorageError::NotFound(format!("Schedule {} not found", id)))
        }
    }

    async fn delete_schedule(&self, id: Uuid) -> Result<()> {
        let mut state = self.state.lock().unwrap();

        if state.schedules.remove(&id).is_some() {
            state.schedule_runs.remove(&id);
            Ok(())
        } else {
            Err(StorageError::NotFound(format!("Schedule {} not found", id)))
        }
    }

    async fn list_schedules(&self, filter: &ScheduleFilter) -> Result<Vec<Schedule>> {
        let state = self.state.lock().unwrap();

        let mut schedules: Vec<_> = state.schedules.values().cloned().collect();

        // Apply filters
        if let Some(workflow_id) = filter.workflow_id {
            schedules.retain(|s| s.workflow_id == workflow_id);
        }

        if let Some(is_active) = filter.is_active {
            schedules.retain(|s| s.is_active == is_active);
        }

        if let Some(ref created_by) = filter.created_by {
            schedules.retain(|s| s.created_by.as_ref() == Some(created_by));
        }

        if let Some(limit) = filter.limit {
            schedules.truncate(limit);
        }

        Ok(schedules)
    }

    async fn get_due_schedules(
        &self,
        before: chrono::DateTime<chrono::Utc>,
    ) -> Result<Vec<Schedule>> {
        let state = self.state.lock().unwrap();

        let schedules: Vec<_> = state
            .schedules
            .values()
            .filter(|s| s.is_active && s.next_run_at.map(|next| next <= before).unwrap_or(false))
            .cloned()
            .collect();

        Ok(schedules)
    }

    async fn store_schedule_run(&self, run: &ScheduleRun) -> Result<Uuid> {
        let mut state = self.state.lock().unwrap();

        if state.should_fail_store {
            return Err(StorageError::IoError(
                "Store schedule run failure".to_string(),
            ));
        }

        let runs = state.schedule_runs.entry(run.schedule_id).or_default();
        runs.push(run.clone());
        Ok(run.id)
    }

    async fn get_schedule_runs(
        &self,
        schedule_id: Uuid,
        limit: Option<usize>,
    ) -> Result<Vec<ScheduleRun>> {
        let state = self.state.lock().unwrap();

        let mut runs = state
            .schedule_runs
            .get(&schedule_id)
            .cloned()
            .unwrap_or_default();

        if let Some(limit) = limit {
            runs.truncate(limit);
        }

        Ok(runs)
    }
}

// Note: We implement the core storage traits needed for testing.
// Full Storage trait implementation requires many additional traits (OrganizationStorage,
// TeamStorage, ApiKeyStorage, etc.) which can be added as needed.

#[cfg(all(test, feature = "server"))]
mod tests {
    use super::*;
    use chrono::Utc;

    fn create_test_workflow() -> (DSLWorkflow, WorkflowMetadata) {
        let workflow = DSLWorkflow {
            name: "test-workflow".to_string(),
            version: "1.0.0".to_string(),
            dsl_version: "1.0.0".to_string(),
            provider: Default::default(),
            model: None,
            cwd: None,
            create_cwd: None,
            secrets: Default::default(),
            inputs: Default::default(),
            outputs: Default::default(),
            agents: Default::default(),
            tasks: Default::default(),
            workflows: Default::default(),
            tools: None,
            communication: None,
            mcp_servers: Default::default(),
            subflows: Default::default(),
            imports: Default::default(),
            notifications: None,
            limits: None,
        };

        let metadata = WorkflowMetadata {
            id: Uuid::new_v4(),
            name: "test-workflow".to_string(),
            version: "1.0.0".to_string(),
            description: None,
            created_at: Utc::now(),
            updated_at: Utc::now(),
            created_by: None,
            tags: vec![],
            is_active: true,
        };

        (workflow, metadata)
    }

    fn create_test_execution(workflow_id: Uuid) -> Execution {
        Execution {
            id: Uuid::new_v4(),
            workflow_id,
            workflow_version: "1.0.0".to_string(),
            status: ExecutionStatus::Queued,
            started_at: None,
            completed_at: None,
            created_at: Utc::now(),
            triggered_by: None,
            trigger_type: "manual".to_string(),
            input_params: None,
            result: None,
            error: None,
            retry_count: 0,
            parent_execution_id: None,
        }
    }

    #[tokio::test]
    async fn test_store_and_get_workflow() {
        let storage = MockStorage::new();
        let (workflow, metadata) = create_test_workflow();
        let workflow_id = metadata.id;

        storage.store_workflow(&workflow, &metadata).await.unwrap();
        assert_eq!(storage.workflow_count(), 1);

        let retrieved = storage.get_workflow(workflow_id).await.unwrap().unwrap();
        assert_eq!(retrieved.1.name, "test-workflow");
    }

    #[tokio::test]
    async fn test_create_and_get_execution() {
        let storage = MockStorage::new();
        let execution = create_test_execution(Uuid::new_v4());
        let execution_id = execution.id;

        storage.store_execution(&execution).await.unwrap();
        assert_eq!(storage.execution_count(), 1);

        let retrieved = storage.get_execution(execution_id).await.unwrap().unwrap();
        assert_eq!(retrieved.status, ExecutionStatus::Queued);
    }

    #[tokio::test]
    async fn test_update_execution() {
        let storage = MockStorage::new();
        let mut execution = create_test_execution(Uuid::new_v4());
        let execution_id = execution.id;

        storage.store_execution(&execution).await.unwrap();

        execution.status = ExecutionStatus::Running;
        storage
            .update_execution(execution_id, &execution)
            .await
            .unwrap();

        let retrieved = storage.get_execution(execution_id).await.unwrap().unwrap();
        assert_eq!(retrieved.status, ExecutionStatus::Running);
    }

    #[tokio::test]
    async fn test_list_executions_by_status() {
        let storage = MockStorage::new();
        let workflow_id = Uuid::new_v4();

        for _ in 0..3 {
            let mut exec = create_test_execution(workflow_id);
            exec.status = ExecutionStatus::Running;
            storage.store_execution(&exec).await.unwrap();
        }

        for _ in 0..2 {
            let exec = create_test_execution(workflow_id);
            storage.store_execution(&exec).await.unwrap();
        }

        let running = storage.get_executions_by_status(ExecutionStatus::Running);
        assert_eq!(running.len(), 3);

        let queued = storage.get_executions_by_status(ExecutionStatus::Queued);
        assert_eq!(queued.len(), 2);
    }
}