mofa-foundation 0.1.1

MoFA Foundation - Core building blocks and utilities
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
//! Workflow DSL Schema
//!
//! Defines the data structures for declarative workflow configuration.

use crate::workflow::node::NodeType;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

/// Workflow definition from YAML/TOML
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WorkflowDefinition {
    /// Workflow metadata
    pub metadata: WorkflowMetadata,

    /// Workflow configuration
    #[serde(default)]
    pub config: WorkflowConfig,

    /// Node definitions
    #[serde(default)]
    pub nodes: Vec<NodeDefinition>,

    /// Edge definitions
    #[serde(default)]
    pub edges: Vec<EdgeDefinition>,

    /// Agent definitions (inline or reusable)
    #[serde(default)]
    pub agents: HashMap<String, LlmAgentConfig>,
}

/// Workflow metadata
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WorkflowMetadata {
    /// Unique workflow identifier
    pub id: String,

    /// Human-readable name
    pub name: String,

    /// Workflow description
    #[serde(default)]
    pub description: String,

    /// Workflow version
    #[serde(default)]
    pub version: Option<String>,

    /// Author
    #[serde(default)]
    pub author: Option<String>,

    /// Tags
    #[serde(default)]
    pub tags: Vec<String>,
}

/// Workflow-level configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WorkflowConfig {
    /// Maximum parallel executions
    #[serde(default = "default_max_parallel")]
    pub max_parallel: usize,

    /// Default timeout (milliseconds)
    #[serde(default = "default_timeout")]
    pub default_timeout_ms: u64,

    /// Enable checkpoints
    #[serde(default)]
    pub enable_checkpoints: bool,

    /// Retry policy for all nodes
    #[serde(default)]
    pub retry_policy: Option<RetryPolicy>,
}

impl Default for WorkflowConfig {
    fn default() -> Self {
        Self {
            max_parallel: default_max_parallel(),
            default_timeout_ms: default_timeout(),
            enable_checkpoints: false,
            retry_policy: None,
        }
    }
}

fn default_max_parallel() -> usize {
    10
}

fn default_timeout() -> u64 {
    60000 // 1 minute
}

/// Node definition (tagged enum for different node types)
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum NodeDefinition {
    /// Start node
    Start {
        id: String,
        #[serde(default)]
        name: Option<String>,
    },

    /// End node
    End {
        id: String,
        #[serde(default)]
        name: Option<String>,
    },

    /// Task node (custom executor)
    Task {
        id: String,
        name: String,
        #[serde(flatten)]
        executor: TaskExecutorDef,
        #[serde(default)]
        config: NodeConfigDef,
    },

    /// LLM Agent node
    LlmAgent {
        id: String,
        name: String,
        /// Agent reference (agent_id for registry agents, inline for embedded)
        agent: AgentRef,
        /// Optional prompt template
        #[serde(default)]
        prompt_template: Option<String>,
        #[serde(default)]
        config: NodeConfigDef,
    },

    /// Condition node
    Condition {
        id: String,
        name: String,
        condition: ConditionDef,
        #[serde(default)]
        config: NodeConfigDef,
    },

    /// Parallel node
    Parallel {
        id: String,
        name: String,
        #[serde(default)]
        config: NodeConfigDef,
    },

    /// Join node
    Join {
        id: String,
        name: String,
        /// List of node IDs to wait for
        #[serde(default)]
        wait_for: Vec<String>,
        #[serde(default)]
        config: NodeConfigDef,
    },

    /// Loop node
    Loop {
        id: String,
        name: String,
        #[serde(flatten)]
        body: TaskExecutorDef,
        condition: LoopConditionDef,
        #[serde(default)]
        max_iterations: u32,
        #[serde(default)]
        config: NodeConfigDef,
    },

    /// Transform node
    Transform {
        id: String,
        name: String,
        #[serde(flatten)]
        transform: TransformDef,
        #[serde(default)]
        config: NodeConfigDef,
    },

    /// Sub-workflow node
    SubWorkflow {
        id: String,
        name: String,
        /// Reference to another workflow
        workflow_id: String,
        #[serde(default)]
        config: NodeConfigDef,
    },

    /// Wait node
    Wait {
        id: String,
        name: String,
        /// Event type to wait for
        event_type: String,
        #[serde(default)]
        config: NodeConfigDef,
    },
}

impl NodeDefinition {
    /// Get the node ID
    pub fn id(&self) -> &str {
        match self {
            NodeDefinition::Start { id, .. } => id,
            NodeDefinition::End { id, .. } => id,
            NodeDefinition::Task { id, .. } => id,
            NodeDefinition::LlmAgent { id, .. } => id,
            NodeDefinition::Condition { id, .. } => id,
            NodeDefinition::Parallel { id, .. } => id,
            NodeDefinition::Join { id, .. } => id,
            NodeDefinition::Loop { id, .. } => id,
            NodeDefinition::Transform { id, .. } => id,
            NodeDefinition::SubWorkflow { id, .. } => id,
            NodeDefinition::Wait { id, .. } => id,
        }
    }

    /// Get the node type
    pub fn node_type(&self) -> NodeType {
        match self {
            NodeDefinition::Start { .. } => NodeType::Start,
            NodeDefinition::End { .. } => NodeType::End,
            NodeDefinition::Task { .. } => NodeType::Task,
            NodeDefinition::LlmAgent { .. } => NodeType::Agent,
            NodeDefinition::Condition { .. } => NodeType::Condition,
            NodeDefinition::Parallel { .. } => NodeType::Parallel,
            NodeDefinition::Join { .. } => NodeType::Join,
            NodeDefinition::Loop { .. } => NodeType::Loop,
            NodeDefinition::Transform { .. } => NodeType::Transform,
            NodeDefinition::SubWorkflow { .. } => NodeType::SubWorkflow,
            NodeDefinition::Wait { .. } => NodeType::Wait,
        }
    }
}

/// Edge definition
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EdgeDefinition {
    /// Source node ID
    pub from: String,

    /// Target node ID
    pub to: String,

    /// Conditional edge (optional)
    #[serde(default)]
    pub condition: Option<String>,

    /// Edge label (optional)
    #[serde(default)]
    pub label: Option<String>,
}

/// LLM Agent configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LlmAgentConfig {
    /// Model identifier
    pub model: String,

    /// System prompt
    #[serde(default)]
    pub system_prompt: Option<String>,

    /// Temperature
    #[serde(default)]
    pub temperature: Option<f32>,

    /// Max tokens
    #[serde(default)]
    pub max_tokens: Option<u32>,

    /// Context window size (in rounds)
    #[serde(default)]
    pub context_window_size: Option<usize>,

    /// User ID for persistence
    #[serde(default)]
    pub user_id: Option<String>,

    /// Tenant ID for persistence
    #[serde(default)]
    pub tenant_id: Option<String>,
}

/// Agent reference (can be registry or inline)
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum AgentRef {
    /// Reference to agent by ID (in registry)
    Registry { agent_id: String },

    /// Inline agent configuration
    Inline(Box<LlmAgentConfig>),
}

/// Task executor definition
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "executor_type", rename_all = "snake_case")]
pub enum TaskExecutorDef {
    /// Function executor (for code-defined tasks)
    Function { function: String },

    /// HTTP request executor
    Http {
        url: String,
        #[serde(default)]
        method: Option<String>,
    },

    /// Script executor (Rhai)
    Script { script: String },

    /// No-op executor (for testing)
    None,
}

/// Condition definition
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "condition_type", rename_all = "snake_case")]
pub enum ConditionDef {
    /// Expression-based condition
    Expression { expr: String },

    /// Value-based condition
    Value {
        field: String,
        operator: String,
        value: serde_json::Value,
    },
}

/// Loop condition definition
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "condition_type", rename_all = "snake_case")]
pub enum LoopConditionDef {
    /// While-style loop
    While { expr: String },

    /// Until-style loop
    Until { expr: String },

    /// Count-based loop
    Count { max: u32 },
}

/// Transform definition
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "transform_type", rename_all = "snake_case")]
pub enum TransformDef {
    /// Jinja-style template
    Template { template: String },

    /// JavaScript expression
    Expression { expr: String },

    /// Map/reduce operation
    MapReduce {
        #[serde(default)]
        map: Option<String>,
        #[serde(default)]
        reduce: Option<String>,
    },
}

/// Node-level configuration
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct NodeConfigDef {
    /// Retry policy
    #[serde(default)]
    pub retry_policy: Option<RetryPolicy>,

    /// Timeout (milliseconds)
    #[serde(default)]
    pub timeout_ms: Option<u64>,

    /// Custom metadata
    #[serde(default)]
    pub metadata: HashMap<String, String>,
}

/// Retry policy configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RetryPolicy {
    /// Maximum retry attempts
    #[serde(default = "default_max_retries")]
    pub max_retries: u32,

    /// Delay between retries (milliseconds)
    #[serde(default = "default_retry_delay")]
    pub retry_delay_ms: u64,

    /// Enable exponential backoff
    #[serde(default = "default_exponential_backoff")]
    pub exponential_backoff: bool,

    /// Maximum delay (milliseconds)
    #[serde(default = "default_max_delay")]
    pub max_delay_ms: u64,
}

impl Default for RetryPolicy {
    fn default() -> Self {
        Self {
            max_retries: default_max_retries(),
            retry_delay_ms: default_retry_delay(),
            exponential_backoff: default_exponential_backoff(),
            max_delay_ms: default_max_delay(),
        }
    }
}

fn default_max_retries() -> u32 {
    3
}

fn default_retry_delay() -> u64 {
    1000
}

fn default_exponential_backoff() -> bool {
    true
}

fn default_max_delay() -> u64 {
    30000
}

/// Timeout configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TimeoutConfig {
    /// Execution timeout (milliseconds)
    pub execution_timeout_ms: u64,

    /// Cancel on timeout
    #[serde(default = "default_cancel_on_timeout")]
    pub cancel_on_timeout: bool,
}

impl Default for TimeoutConfig {
    fn default() -> Self {
        Self {
            execution_timeout_ms: 60000,
            cancel_on_timeout: true,
        }
    }
}

fn default_cancel_on_timeout() -> bool {
    true
}

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

    #[test]
    fn test_parse_workflow_yaml() {
        let yaml = r#"
metadata:
  id: test_workflow
  name: Test Workflow
  description: A test workflow

nodes:
  - type: start
    id: start

  - type: llm_agent
    id: agent1
    name: First Agent
    agent:
      agent_id: my_agent

  - type: end
    id: end

edges:
  - from: start
    to: agent1
  - from: agent1
    to: end
"#;

        let def: WorkflowDefinition = serde_yaml::from_str(yaml).unwrap();
        assert_eq!(def.metadata.id, "test_workflow");
        assert_eq!(def.nodes.len(), 3);
        assert_eq!(def.edges.len(), 2);
    }

    #[test]
    fn test_parse_agent_config() {
        let yaml = r#"
metadata:
  id: agent_config_test
  name: Agent Config Test

nodes:
  - type: start
    id: start
  - type: end
    id: end

agents:
  my_agent:
    model: gpt-4
    system_prompt: "You are helpful"
    temperature: 0.7
    max_tokens: 2000
"#;

        let def: WorkflowDefinition = serde_yaml::from_str(yaml).unwrap();
        assert_eq!(def.agents.len(), 1);
        let agent = def.agents.get("my_agent").unwrap();
        assert_eq!(agent.model, "gpt-4");
        assert_eq!(agent.temperature, Some(0.7));
    }

    #[test]
    fn test_parse_toml() {
        let toml = r#"
[metadata]
id = "test_workflow"
name = "Test Workflow"

[[nodes]]
type = "start"
id = "start"

[[nodes]]
type = "end"
id = "end"

[[edges]]
from = "start"
to = "end"
"#;

        let def: WorkflowDefinition = toml::from_str(toml).unwrap();
        assert_eq!(def.metadata.id, "test_workflow");
        assert_eq!(def.nodes.len(), 2);
    }
}