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
//! Edge types and routing logic.
//!
//! Edges define the control flow between nodes in a REGULA graph.
//! Normal edges provide fixed routing, while conditional edges use
//! a router function to determine the next node(s) dynamically.

use crate::constants::{end, NodeId};
use crate::error::Result;
use crate::state::GraphState;
use async_trait::async_trait;
use std::collections::HashMap;
use std::future::Future;
use std::marker::PhantomData;
use std::sync::Arc;

/// An edge in the state graph.
///
/// Edges connect nodes and define the control flow of the graph.
#[derive(Clone)]
pub enum Edge<S: GraphState> {
    /// A fixed transition from one node to another.
    Normal {
        /// Source node.
        from: NodeId,
        /// Target node.
        to: NodeId,
    },

    /// A conditional transition where the target is determined at runtime.
    Conditional {
        /// Source node.
        from: NodeId,
        /// Router that determines the target(s).
        router: Arc<dyn EdgeRouter<S>>,
        /// Optional mapping from router output strings to node IDs.
        path_map: Option<HashMap<String, NodeId>>,
    },
}

impl<S: GraphState> Edge<S> {
    /// Create a normal edge.
    pub fn normal(from: impl Into<NodeId>, to: impl Into<NodeId>) -> Self {
        Self::Normal {
            from: from.into(),
            to: to.into(),
        }
    }

    /// Create a conditional edge.
    pub fn conditional<R: EdgeRouter<S> + 'static>(from: impl Into<NodeId>, router: R) -> Self {
        Self::Conditional {
            from: from.into(),
            router: Arc::new(router),
            path_map: None,
        }
    }

    /// Create a conditional edge with a path map.
    pub fn conditional_with_map<R: EdgeRouter<S> + 'static>(
        from: impl Into<NodeId>,
        router: R,
        path_map: HashMap<String, NodeId>,
    ) -> Self {
        Self::Conditional {
            from: from.into(),
            router: Arc::new(router),
            path_map: Some(path_map),
        }
    }

    /// Get the source node of this edge.
    pub fn from(&self) -> &NodeId {
        match self {
            Self::Normal { from, .. } => from,
            Self::Conditional { from, .. } => from,
        }
    }

    /// Check if this is a conditional edge.
    pub fn is_conditional(&self) -> bool {
        matches!(self, Self::Conditional { .. })
    }
}

impl<S: GraphState> std::fmt::Debug for Edge<S> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Normal { from, to } => {
                f.debug_struct("Edge::Normal")
                    .field("from", from)
                    .field("to", to)
                    .finish()
            }
            Self::Conditional { from, path_map, .. } => {
                f.debug_struct("Edge::Conditional")
                    .field("from", from)
                    .field("path_map", path_map)
                    .finish()
            }
        }
    }
}

/// The result of a routing decision.
#[derive(Debug, Clone)]
pub enum RouteOutput {
    /// Go to a single node.
    One(NodeId),

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

    /// End the graph execution.
    End,
}

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

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

    /// Create a route to end the graph.
    pub fn end() -> Self {
        Self::End
    }

    /// Check if this route ends the graph.
    pub fn is_end(&self) -> bool {
        match self {
            Self::End => true,
            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.clone()],
            Self::Many(ids) => ids.clone(),
            Self::End => vec![end()],
        }
    }

    /// Convert to node IDs, replacing END sentinel with actual end.
    pub fn into_nodes(self) -> Vec<NodeId> {
        self.targets()
    }
}

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

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

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

impl From<Vec<NodeId>> for RouteOutput {
    fn from(ids: Vec<NodeId>) -> Self {
        if ids.is_empty() {
            Self::End
        } else {
            Self::Many(ids)
        }
    }
}

/// Trait for edge routing logic.
///
/// Implement this trait to create custom routing logic for conditional edges.
/// The router receives the current state and returns the next node(s).
///
/// # Examples
///
/// ```ignore
/// use regula_core::{EdgeRouter, RouteOutput, GraphState, Result};
/// use async_trait::async_trait;
///
/// struct MyRouter;
///
/// #[async_trait]
/// impl<S: GraphState> EdgeRouter<S> for MyRouter {
///     async fn route(&self, state: &S) -> Result<RouteOutput> {
///         // Decide based on state
///         Ok(RouteOutput::one("next_node"))
///     }
/// }
/// ```
#[async_trait]
pub trait EdgeRouter<S: GraphState>: Send + Sync {
    /// Determine the next node(s) based on the current state.
    async fn route(&self, state: &S) -> Result<RouteOutput>;

    /// Get a description of this router (for debugging).
    fn description(&self) -> Option<&str> {
        None
    }
}

/// A router created from a sync function.
pub struct FnRouter<S, F>
where
    S: GraphState,
    F: Fn(&S) -> RouteOutput + Send + Sync,
{
    func: F,
    description: Option<String>,
    _marker: PhantomData<S>,
}

impl<S, F> FnRouter<S, F>
where
    S: GraphState,
    F: Fn(&S) -> RouteOutput + Send + Sync,
{
    /// Create a new function router.
    pub fn new(func: F) -> Self {
        Self {
            func,
            description: None,
            _marker: PhantomData,
        }
    }

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

#[async_trait]
impl<S, F> EdgeRouter<S> for FnRouter<S, F>
where
    S: GraphState,
    F: Fn(&S) -> RouteOutput + Send + Sync,
{
    async fn route(&self, state: &S) -> Result<RouteOutput> {
        Ok((self.func)(state))
    }

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

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

impl<S, F, Fut> AsyncFnRouter<S, F, Fut>
where
    S: GraphState,
    F: Fn(&S) -> Fut + Send + Sync,
    Fut: Future<Output = Result<RouteOutput>> + Send,
{
    /// Create a new async function router.
    pub fn new(func: F) -> Self {
        Self {
            func,
            description: None,
            _marker: PhantomData,
        }
    }

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

#[async_trait]
impl<S, F, Fut> EdgeRouter<S> for AsyncFnRouter<S, F, Fut>
where
    S: GraphState,
    F: Fn(&S) -> Fut + Send + Sync,
    Fut: Future<Output = Result<RouteOutput>> + Send,
{
    async fn route(&self, state: &S) -> Result<RouteOutput> {
        (self.func)(state).await
    }

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

/// Create a router from a sync function.
///
/// # Examples
///
/// ```ignore
/// use regula_core::{router_fn, RouteOutput};
///
/// let router = router_fn(|state: &MyState| {
///     if state.done {
///         RouteOutput::end()
///     } else {
///         RouteOutput::one("continue")
///     }
/// });
/// ```
pub fn router_fn<S, F>(f: F) -> impl EdgeRouter<S>
where
    S: GraphState,
    F: Fn(&S) -> RouteOutput + Send + Sync + 'static,
{
    FnRouter::new(f)
}

/// Create a named router from a sync function.
pub fn router_fn_named<S, F>(description: impl Into<String>, f: F) -> impl EdgeRouter<S>
where
    S: GraphState,
    F: Fn(&S) -> RouteOutput + Send + Sync + 'static,
{
    FnRouter::with_description(f, description)
}

/// Create a router from an async function.
///
/// # Examples
///
/// ```ignore
/// use regula_core::{async_router_fn, RouteOutput, Result};
///
/// let router = async_router_fn(|state: &MyState| async move {
///     // Can do async operations here
///     Ok(RouteOutput::one("next"))
/// });
/// ```
pub fn async_router_fn<S, F, Fut>(f: F) -> impl EdgeRouter<S>
where
    S: GraphState,
    F: Fn(&S) -> Fut + Send + Sync + 'static,
    Fut: Future<Output = Result<RouteOutput>> + Send + 'static,
{
    AsyncFnRouter::new(f)
}

/// A boxed router for type erasure.
pub type BoxedRouter<S> = Arc<dyn EdgeRouter<S>>;

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

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

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

    #[test]
    fn test_edge_normal() {
        let edge: Edge<TestState> = Edge::normal("a", "b");
        assert_eq!(edge.from(), &NodeId::from("a"));
        assert!(!edge.is_conditional());
    }

    #[test]
    fn test_edge_conditional() {
        let router = router_fn(|state: &TestState| {
            if state.done {
                RouteOutput::end()
            } else {
                RouteOutput::one("continue")
            }
        });

        let edge: Edge<TestState> = Edge::conditional("a", router);
        assert_eq!(edge.from(), &NodeId::from("a"));
        assert!(edge.is_conditional());
    }

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

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

    #[test]
    fn test_route_output_end() {
        let route = RouteOutput::end();
        assert!(route.is_end());
    }

    #[test]
    fn test_route_output_from_str() {
        let route: RouteOutput = "agent".into();
        assert!(matches!(route, RouteOutput::One(_)));
    }

    #[tokio::test]
    async fn test_fn_router() {
        let router = router_fn(|state: &TestState| {
            if state.done {
                RouteOutput::end()
            } else {
                RouteOutput::one("continue")
            }
        });

        let state = TestState { done: false };
        let result = router.route(&state).await.unwrap();
        assert!(matches!(result, RouteOutput::One(_)));

        let state = TestState { done: true };
        let result = router.route(&state).await.unwrap();
        assert!(result.is_end());
    }

    #[tokio::test]
    async fn test_async_fn_router() {
        let router = async_router_fn(|state: &TestState| {
            let done = state.done;
            async move {
                if done {
                    Ok(RouteOutput::end())
                } else {
                    Ok(RouteOutput::one("continue"))
                }
            }
        });

        let state = TestState { done: false };
        let result = router.route(&state).await.unwrap();
        assert!(matches!(result, RouteOutput::One(_)));
    }
}