regula-core 0.1.0

Core abstractions for REGULA agentic orchestration framework
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
//! Node trait and related types.
//!
//! Nodes are the fundamental building blocks of a REGULA graph. Each node
//! is an async function that receives state and produces updates.

use crate::constants::NodeId;
use crate::error::Result;
use crate::state::GraphState;
use async_trait::async_trait;
use serde::{Deserialize, Serialize};
use std::future::Future;
use std::marker::PhantomData;
use std::sync::Arc;

/// The output from a node execution.
///
/// Nodes can return either a simple state update or a command that
/// includes routing information.
#[derive(Debug, Clone)]
pub enum NodeOutput {
    /// A partial state update to be merged into the current state.
    Update(serde_json::Value),

    /// A command that includes both state update and routing.
    Command(Command),

    /// No output - the state remains unchanged.
    None,
}

impl NodeOutput {
    /// Create an update output from a JSON value.
    pub fn update(value: serde_json::Value) -> Self {
        Self::Update(value)
    }

    /// Create an update output, serializing the given value.
    pub fn update_from<T: Serialize>(value: &T) -> Result<Self> {
        Ok(Self::Update(serde_json::to_value(value)?))
    }

    /// Create a command output.
    pub fn command(cmd: Command) -> Self {
        Self::Command(cmd)
    }

    /// Create a no-op output.
    pub fn none() -> Self {
        Self::None
    }

    /// Check if this output has state updates.
    pub fn has_update(&self) -> bool {
        match self {
            Self::Update(v) => !v.is_null() && v != &serde_json::Value::Object(Default::default()),
            Self::Command(cmd) => cmd.update.is_some(),
            Self::None => false,
        }
    }

    /// Get the state update, if any.
    pub fn get_update(&self) -> Option<&serde_json::Value> {
        match self {
            Self::Update(v) => Some(v),
            Self::Command(cmd) => cmd.update.as_ref(),
            Self::None => None,
        }
    }

    /// Get routing from a command, if any.
    pub fn get_goto(&self) -> Option<&CommandGoto> {
        match self {
            Self::Command(cmd) => cmd.goto.as_ref(),
            _ => None,
        }
    }
}

/// A command that combines state update with explicit routing.
///
/// Commands allow nodes to control which node(s) execute next,
/// overriding the graph's edge definitions.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Command {
    /// State update to apply.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub update: Option<serde_json::Value>,

    /// Explicit next node(s) to execute.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub goto: Option<CommandGoto>,

    /// Value to resume with (for interrupt handling).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub resume: Option<serde_json::Value>,
}

impl Command {
    /// Create a new empty command.
    pub fn new() -> Self {
        Self {
            update: None,
            goto: None,
            resume: None,
        }
    }

    /// Create a command with a state update.
    pub fn with_update(update: serde_json::Value) -> Self {
        Self {
            update: Some(update),
            goto: None,
            resume: None,
        }
    }

    /// Create a command with a goto target.
    pub fn with_goto(goto: impl Into<CommandGoto>) -> Self {
        Self {
            update: None,
            goto: Some(goto.into()),
            resume: None,
        }
    }

    /// Add a state update to this command.
    pub fn update(mut self, update: serde_json::Value) -> Self {
        self.update = Some(update);
        self
    }

    /// Add a goto target to this command.
    pub fn goto(mut self, target: impl Into<CommandGoto>) -> Self {
        self.goto = Some(target.into());
        self
    }

    /// Add a resume value to this command.
    pub fn resume(mut self, value: serde_json::Value) -> Self {
        self.resume = Some(value);
        self
    }
}

impl Default for Command {
    fn default() -> Self {
        Self::new()
    }
}

/// Target for command routing.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum CommandGoto {
    /// Go to a single node.
    One(NodeId),

    /// Go to multiple nodes in parallel.
    Many(Vec<NodeId>),
}

impl CommandGoto {
    /// Create a goto to a single node.
    pub fn one(node: impl Into<NodeId>) -> Self {
        Self::One(node.into())
    }

    /// Create a goto to multiple nodes.
    pub fn many(nodes: impl IntoIterator<Item = impl Into<NodeId>>) -> Self {
        Self::Many(nodes.into_iter().map(Into::into).collect())
    }

    /// Check if this goto ends the graph.
    pub fn is_end(&self) -> bool {
        match self {
            Self::One(id) => id.is_end(),
            Self::Many(ids) => ids.iter().all(|id| id.is_end()),
        }
    }

    /// Get all target node IDs.
    pub fn targets(&self) -> Vec<&NodeId> {
        match self {
            Self::One(id) => vec![id],
            Self::Many(ids) => ids.iter().collect(),
        }
    }
}

impl From<NodeId> for CommandGoto {
    fn from(id: NodeId) -> Self {
        Self::One(id)
    }
}

impl From<&str> for CommandGoto {
    fn from(s: &str) -> Self {
        Self::One(NodeId::from(s))
    }
}

impl From<String> for CommandGoto {
    fn from(s: String) -> Self {
        Self::One(NodeId::from(s))
    }
}

impl From<Vec<NodeId>> for CommandGoto {
    fn from(ids: Vec<NodeId>) -> Self {
        Self::Many(ids)
    }
}

/// Configuration passed to nodes during execution.
///
/// This is a re-export from the config module, included here
/// for convenience in node signatures.
pub use crate::config::RunnableConfig;

/// Trait for graph nodes.
///
/// Nodes are async functions that receive the current state and configuration,
/// and return a state update or command.
///
/// # Type Parameters
///
/// - `S`: The state type for this graph.
///
/// # Examples
///
/// ```ignore
/// use regula_core::{Node, NodeOutput, GraphState, RunnableConfig, Result};
/// use async_trait::async_trait;
///
/// struct MyNode;
///
/// #[async_trait]
/// impl<S: GraphState> Node<S> for MyNode {
///     async fn execute(&self, state: &S, config: &RunnableConfig) -> Result<NodeOutput> {
///         // Process state and return update
///         Ok(NodeOutput::update(serde_json::json!({
///             "counter": 42
///         })))
///     }
/// }
/// ```
#[async_trait]
pub trait Node<S: GraphState>: Send + Sync {
    /// Execute the node with the current state.
    ///
    /// # Arguments
    ///
    /// - `state`: The current graph state.
    /// - `config`: Runtime configuration and context.
    ///
    /// # Returns
    ///
    /// A `NodeOutput` containing state updates and/or routing commands.
    async fn execute(&self, state: &S, config: &RunnableConfig) -> Result<NodeOutput>;

    /// Get the name of this node (for debugging/tracing).
    fn name(&self) -> Option<&str> {
        None
    }

    /// Check if this node should be retried on failure.
    fn retry_policy(&self) -> RetryPolicy {
        RetryPolicy::None
    }
}

/// Retry policy for node execution.
#[derive(Debug, Clone, Default)]
pub enum RetryPolicy {
    /// No retries.
    #[default]
    None,

    /// Retry with exponential backoff.
    Exponential {
        /// Maximum number of retries.
        max_retries: usize,
        /// Initial delay in milliseconds.
        initial_delay_ms: u64,
        /// Maximum delay in milliseconds.
        max_delay_ms: u64,
    },

    /// Retry with fixed delay.
    Fixed {
        /// Maximum number of retries.
        max_retries: usize,
        /// Delay between retries in milliseconds.
        delay_ms: u64,
    },
}

/// A node created from an async function.
pub struct FnNode<S, F, Fut>
where
    S: GraphState,
    F: Fn(&S, &RunnableConfig) -> Fut + Send + Sync,
    Fut: Future<Output = Result<NodeOutput>> + Send,
{
    func: F,
    name: Option<String>,
    _marker: PhantomData<fn(S) -> Fut>,
}

impl<S, F, Fut> FnNode<S, F, Fut>
where
    S: GraphState,
    F: Fn(&S, &RunnableConfig) -> Fut + Send + Sync,
    Fut: Future<Output = Result<NodeOutput>> + Send,
{
    /// Create a new function node.
    pub fn new(func: F) -> Self {
        Self {
            func,
            name: None,
            _marker: PhantomData,
        }
    }

    /// Create a new function node with a name.
    pub fn with_name(func: F, name: impl Into<String>) -> Self {
        Self {
            func,
            name: Some(name.into()),
            _marker: PhantomData,
        }
    }
}

#[async_trait]
impl<S, F, Fut> Node<S> for FnNode<S, F, Fut>
where
    S: GraphState,
    F: Fn(&S, &RunnableConfig) -> Fut + Send + Sync,
    Fut: Future<Output = Result<NodeOutput>> + Send,
{
    async fn execute(&self, state: &S, config: &RunnableConfig) -> Result<NodeOutput> {
        (self.func)(state, config).await
    }

    fn name(&self) -> Option<&str> {
        self.name.as_deref()
    }
}

/// Create a node from an async function.
///
/// This is a convenience function to create nodes from closures or
/// async functions without implementing the `Node` trait manually.
///
/// # Examples
///
/// ```ignore
/// use regula_core::{node_fn, NodeOutput, Result};
///
/// let my_node = node_fn(|state: &MyState, config: &RunnableConfig| async move {
///     Ok(NodeOutput::update(serde_json::json!({
///         "counter": state.counter + 1
///     })))
/// });
/// ```
pub fn node_fn<S, F, Fut>(f: F) -> impl Node<S>
where
    S: GraphState,
    F: Fn(&S, &RunnableConfig) -> Fut + Send + Sync + 'static,
    Fut: Future<Output = Result<NodeOutput>> + Send + 'static,
{
    FnNode::new(f)
}

/// Create a named node from an async function.
pub fn node_fn_named<S, F, Fut>(name: impl Into<String>, f: F) -> impl Node<S>
where
    S: GraphState,
    F: Fn(&S, &RunnableConfig) -> Fut + Send + Sync + 'static,
    Fut: Future<Output = Result<NodeOutput>> + Send + 'static,
{
    FnNode::with_name(f, name)
}

/// A boxed node for type erasure.
pub type BoxedNode<S> = Arc<dyn Node<S>>;

#[cfg(test)]
mod tests {
    use super::*;
    use crate::channel::ChannelSpec;
    use std::collections::HashMap;

    #[derive(Clone, serde::Serialize, serde::Deserialize)]
    struct TestState {
        counter: i32,
    }

    impl GraphState for TestState {
        fn channels() -> HashMap<String, ChannelSpec> {
            let mut channels = HashMap::new();
            channels.insert("counter".to_string(), ChannelSpec::LastValue);
            channels
        }
    }

    #[test]
    fn test_node_output_update() {
        let output = NodeOutput::update(serde_json::json!({"counter": 42}));
        assert!(output.has_update());
        assert!(matches!(output, NodeOutput::Update(_)));
    }

    #[test]
    fn test_node_output_none() {
        let output = NodeOutput::none();
        assert!(!output.has_update());
    }

    #[test]
    fn test_command_builder() {
        let cmd = Command::new()
            .update(serde_json::json!({"counter": 100}))
            .goto("next_node");

        assert!(cmd.update.is_some());
        assert!(cmd.goto.is_some());
    }

    #[test]
    fn test_command_goto_one() {
        let goto = CommandGoto::one("agent");
        assert!(!goto.is_end());
        assert_eq!(goto.targets().len(), 1);
    }

    #[test]
    fn test_command_goto_many() {
        let goto = CommandGoto::many(vec!["a", "b", "c"]);
        assert!(!goto.is_end());
        assert_eq!(goto.targets().len(), 3);
    }

    #[test]
    fn test_command_goto_from_str() {
        let goto: CommandGoto = "agent".into();
        assert!(matches!(goto, CommandGoto::One(_)));
    }

    #[tokio::test]
    async fn test_fn_node() {
        let node = node_fn(|state: &TestState, _config: &RunnableConfig| {
            let counter = state.counter;
            async move {
                Ok(NodeOutput::update(serde_json::json!({
                    "counter": counter + 1
                })))
            }
        });

        let state = TestState { counter: 10 };
        let config = RunnableConfig::new();

        let output = node.execute(&state, &config).await.unwrap();
        assert!(output.has_update());

        if let NodeOutput::Update(value) = output {
            assert_eq!(value["counter"], 11);
        }
    }

    #[test]
    fn test_retry_policy_default() {
        let policy = RetryPolicy::default();
        assert!(matches!(policy, RetryPolicy::None));
    }
}