rexis-graph 0.1.0

Rexis Graph - Graph-based agent orchestration with hybrid state management and memory integration
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
//! # Core Graph Abstractions
//!
//! This module contains the fundamental types and traits that form the foundation
//! of the RGraph system, including the workflow graph, nodes, edges, and execution context.

use crate::state::GraphState;
use crate::{RGraphError, RGraphResult};
use async_trait::async_trait;
use petgraph::{Directed, Graph};
use std::collections::HashMap;
use std::sync::Arc;
use uuid::Uuid;
type NodeIndex = petgraph::graph::NodeIndex;
#[allow(dead_code)]
type EdgeIndex = petgraph::graph::EdgeIndex;
use parking_lot::RwLock;

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

/// Unique identifier for a node in the workflow graph
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct NodeId(pub String);

impl NodeId {
    /// Create a new node ID
    pub fn new(id: impl Into<String>) -> Self {
        Self(id.into())
    }

    /// Generate a random node ID
    pub fn generate() -> Self {
        Self(Uuid::new_v4().to_string())
    }

    /// Get the string representation
    pub fn as_str(&self) -> &str {
        &self.0
    }
}

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

impl From<&str> for NodeId {
    fn from(id: &str) -> Self {
        NodeId(id.to_string())
    }
}

/// Unique identifier for an edge in the workflow graph
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct EdgeId(pub String);

impl EdgeId {
    /// Create a new edge ID
    pub fn new(id: impl Into<String>) -> Self {
        Self(id.into())
    }

    /// Generate a random edge ID
    pub fn generate() -> Self {
        Self(Uuid::new_v4().to_string())
    }
}

/// Core trait for all executable nodes in the workflow graph
#[async_trait]
pub trait Node: Send + Sync {
    /// Execute the node with the given state and context
    async fn execute(
        &self,
        state: &mut GraphState,
        context: &ExecutionContext,
    ) -> RGraphResult<ExecutionResult>;

    /// Get the node's unique identifier
    fn id(&self) -> &NodeId;

    /// Get the node's display name
    fn name(&self) -> &str;

    /// Get the node's description
    fn description(&self) -> Option<&str> {
        None
    }

    /// Get the expected input keys from the state
    fn input_keys(&self) -> Vec<&str> {
        vec![]
    }

    /// Get the output keys that this node will write to the state
    fn output_keys(&self) -> Vec<&str> {
        vec![]
    }

    /// Validate that the node can execute with the current state
    fn validate(&self, _state: &GraphState) -> RGraphResult<()> {
        Ok(())
    }

    /// Get node metadata for observability
    fn metadata(&self) -> NodeMetadata {
        NodeMetadata {
            id: self.id().clone(),
            name: self.name().to_string(),
            description: self.description().map(|s| s.to_string()),
            input_keys: self.input_keys().iter().map(|s| s.to_string()).collect(),
            output_keys: self.output_keys().iter().map(|s| s.to_string()).collect(),
        }
    }
}

/// Metadata about a node for introspection and observability
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct NodeMetadata {
    pub id: NodeId,
    pub name: String,
    pub description: Option<String>,
    pub input_keys: Vec<String>,
    pub output_keys: Vec<String>,
}

/// Represents an edge in the workflow graph
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Edge {
    pub id: EdgeId,
    pub from: NodeId,
    pub to: NodeId,
    pub condition: Option<EdgeCondition>,
}

/// Condition that must be met for an edge to be traversed
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum EdgeCondition {
    /// Always traverse the edge
    Always,
    /// Traverse if the condition function returns true
    Conditional(String), // Serialized condition function
    /// Traverse if the state contains a specific value
    StateCondition {
        key: String,
        expected_value: serde_json::Value,
    },
}

/// Result of executing a node
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum ExecutionResult {
    /// Continue to the next node
    Continue,
    /// Stop execution and return the current state
    Stop,
    /// Jump to a specific node
    JumpTo(NodeId),
    /// Conditional routing based on state
    Route(String), // Next node ID based on routing logic
}

/// Context information available during node execution
#[derive(Clone)]
pub struct ExecutionContext {
    pub graph_id: String,
    pub execution_id: String,
    pub current_node: NodeId,
    pub execution_path: Vec<NodeId>,
    pub start_time: chrono::DateTime<chrono::Utc>,
    pub metadata: HashMap<String, serde_json::Value>,

    /// Optional persistent memory backend for agents
    #[cfg(feature = "rexis-rag-integration")]
    pub memory: Option<Arc<dyn rexis_rag::storage::Memory>>,
}

impl std::fmt::Debug for ExecutionContext {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let mut debug_struct = f.debug_struct("ExecutionContext");
        debug_struct
            .field("graph_id", &self.graph_id)
            .field("execution_id", &self.execution_id)
            .field("current_node", &self.current_node)
            .field("execution_path", &self.execution_path)
            .field("start_time", &self.start_time)
            .field("metadata", &self.metadata);

        #[cfg(feature = "rexis-rag-integration")]
        debug_struct.field("memory", &self.memory.as_ref().map(|_| "<Memory>"));

        debug_struct.finish()
    }
}

impl ExecutionContext {
    pub fn new(graph_id: String, current_node: NodeId) -> Self {
        Self {
            graph_id,
            execution_id: Uuid::new_v4().to_string(),
            current_node,
            execution_path: Vec::new(),
            start_time: chrono::Utc::now(),
            metadata: HashMap::new(),
            #[cfg(feature = "rexis-rag-integration")]
            memory: None,
        }
    }

    pub fn with_metadata(mut self, key: String, value: serde_json::Value) -> Self {
        self.metadata.insert(key, value);
        self
    }

    /// Set persistent memory backend (requires 'rrag-integration' feature)
    #[cfg(feature = "rexis-rag-integration")]
    pub fn with_memory(mut self, memory: Arc<dyn rexis_rag::storage::Memory>) -> Self {
        self.memory = Some(memory);
        self
    }

    /// Get memory backend if available
    #[cfg(feature = "rexis-rag-integration")]
    pub fn memory(&self) -> Option<Arc<dyn rexis_rag::storage::Memory>> {
        self.memory.clone()
    }
}

/// The main workflow graph that orchestrates node execution
pub struct WorkflowGraph {
    id: String,
    name: String,
    description: Option<String>,
    graph: Arc<RwLock<Graph<Arc<dyn Node>, Edge, Directed>>>,
    node_lookup: Arc<RwLock<HashMap<NodeId, NodeIndex>>>,
    entry_points: Arc<RwLock<Vec<NodeId>>>,
    exit_points: Arc<RwLock<Vec<NodeId>>>,
}

impl WorkflowGraph {
    /// Create a new workflow graph
    pub fn new(name: impl Into<String>) -> Self {
        Self {
            id: Uuid::new_v4().to_string(),
            name: name.into(),
            description: None,
            graph: Arc::new(RwLock::new(Graph::new())),
            node_lookup: Arc::new(RwLock::new(HashMap::new())),
            entry_points: Arc::new(RwLock::new(Vec::new())),
            exit_points: Arc::new(RwLock::new(Vec::new())),
        }
    }

    /// Set the graph description
    pub fn with_description(mut self, description: impl Into<String>) -> Self {
        self.description = Some(description.into());
        self
    }

    /// Add a node to the graph
    pub async fn add_node(
        &mut self,
        node_id: impl Into<NodeId>,
        node: Arc<dyn Node>,
    ) -> RGraphResult<()> {
        let node_id = node_id.into();

        // Validate the node
        let dummy_state = GraphState::new();
        node.validate(&dummy_state)?;

        let mut graph = self.graph.write();
        let mut lookup = self.node_lookup.write();

        // Check if node already exists
        if lookup.contains_key(&node_id) {
            return Err(RGraphError::validation(format!(
                "Node '{}' already exists",
                node_id.as_str()
            )));
        }

        // Add node to the graph
        let node_index = graph.add_node(node);
        lookup.insert(node_id.clone(), node_index);

        // If this is the first node, make it an entry point
        if lookup.len() == 1 {
            self.entry_points.write().push(node_id);
        }

        Ok(())
    }

    /// Add an edge between two nodes
    pub fn add_edge(
        &mut self,
        from: impl Into<NodeId>,
        to: impl Into<NodeId>,
    ) -> RGraphResult<EdgeId> {
        self.add_edge_with_condition(from, to, EdgeCondition::Always)
    }

    /// Add an edge with a condition
    pub fn add_edge_with_condition(
        &mut self,
        from: impl Into<NodeId>,
        to: impl Into<NodeId>,
        condition: EdgeCondition,
    ) -> RGraphResult<EdgeId> {
        let from_id = from.into();
        let to_id = to.into();
        let edge_id = EdgeId::generate();

        let graph_lock = self.graph.clone();
        let lookup_lock = self.node_lookup.clone();

        let mut graph = graph_lock.write();
        let lookup = lookup_lock.read();

        // Get node indices
        let from_index = lookup.get(&from_id).ok_or_else(|| {
            RGraphError::validation(format!("Node '{}' not found", from_id.as_str()))
        })?;
        let to_index = lookup.get(&to_id).ok_or_else(|| {
            RGraphError::validation(format!("Node '{}' not found", to_id.as_str()))
        })?;

        // Create edge
        let edge = Edge {
            id: edge_id.clone(),
            from: from_id,
            to: to_id,
            condition: Some(condition),
        };

        // Add edge to graph
        graph.add_edge(*from_index, *to_index, edge);

        Ok(edge_id)
    }

    /// Add a conditional edge with a custom routing function
    pub fn add_conditional_edge<F>(
        &mut self,
        from: impl Into<NodeId>,
        _condition_fn: F,
    ) -> RGraphResult<EdgeId>
    where
        F: Fn(&GraphState) -> RGraphResult<String> + Send + Sync + 'static,
    {
        // In a real implementation, we'd store the condition function
        // For now, we'll create a placeholder edge
        let _from_id = from.into();
        let edge_id = EdgeId::generate();

        // This is a simplified implementation - in reality, we'd need to handle
        // the conditional routing during execution
        Ok(edge_id)
    }

    /// Set entry points for the graph
    pub fn set_entry_points(&mut self, entry_points: Vec<NodeId>) {
        *self.entry_points.write() = entry_points;
    }

    /// Set exit points for the graph
    pub fn set_exit_points(&mut self, exit_points: Vec<NodeId>) {
        *self.exit_points.write() = exit_points;
    }

    /// Get the graph ID
    pub fn id(&self) -> &str {
        &self.id
    }

    /// Get the graph name
    pub fn name(&self) -> &str {
        &self.name
    }

    /// Get the graph description
    pub fn description(&self) -> Option<&str> {
        self.description.as_deref()
    }

    /// Get all node IDs in the graph
    pub fn node_ids(&self) -> Vec<NodeId> {
        self.node_lookup.read().keys().cloned().collect()
    }

    /// Get entry points (returns owned values to avoid lifetime issues)
    pub fn entry_points(&self) -> Vec<NodeId> {
        self.entry_points.read().clone()
    }

    /// Get entry points as owned values
    pub fn entry_points_owned(&self) -> Vec<NodeId> {
        self.entry_points.read().clone()
    }

    /// Get a node by ID
    pub fn get_node(&self, node_id: &NodeId) -> Option<Arc<dyn Node>> {
        let lookup = self.node_lookup.read();
        let graph = self.graph.read();

        if let Some(&node_index) = lookup.get(node_id) {
            if let Some(node_weight) = graph.node_weight(node_index) {
                return Some(node_weight.clone());
            }
        }
        None
    }

    /// Validate the graph structure
    pub fn validate(&self) -> RGraphResult<()> {
        let lookup = self.node_lookup.read();
        let entry_points = self.entry_points.read();

        // Check that we have nodes
        if lookup.is_empty() {
            return Err(RGraphError::validation("Graph has no nodes"));
        }

        // Check that we have entry points
        if entry_points.is_empty() {
            return Err(RGraphError::validation("Graph has no entry points"));
        }

        // Validate that all entry points exist
        for entry_point in entry_points.iter() {
            if !lookup.contains_key(entry_point) {
                return Err(RGraphError::validation(format!(
                    "Entry point '{}' does not exist",
                    entry_point.as_str()
                )));
            }
        }

        Ok(())
    }
}

/// Builder for creating workflow graphs with a fluent API
pub struct GraphBuilder {
    graph: WorkflowGraph,
}

impl GraphBuilder {
    /// Create a new graph builder
    pub fn new(name: impl Into<String>) -> Self {
        Self {
            graph: WorkflowGraph::new(name),
        }
    }

    /// Set the graph description
    pub fn description(mut self, description: impl Into<String>) -> Self {
        self.graph = self.graph.with_description(description);
        self
    }

    /// Add a node to the graph
    pub async fn add_node(
        mut self,
        node_id: impl Into<NodeId>,
        node: Arc<dyn Node>,
    ) -> RGraphResult<Self> {
        self.graph.add_node(node_id, node).await?;
        Ok(self)
    }

    /// Add an edge between two nodes
    pub fn add_edge(
        mut self,
        from: impl Into<NodeId>,
        to: impl Into<NodeId>,
    ) -> RGraphResult<Self> {
        self.graph.add_edge(from, to)?;
        Ok(self)
    }

    /// Set entry points
    pub fn entry_points(mut self, entry_points: Vec<NodeId>) -> Self {
        self.graph.set_entry_points(entry_points);
        self
    }

    /// Build the workflow graph
    pub fn build(self) -> RGraphResult<WorkflowGraph> {
        self.graph.validate()?;
        Ok(self.graph)
    }
}

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

    // Mock node for testing
    struct TestNode {
        id: NodeId,
        name: String,
    }

    impl TestNode {
        fn new(id: impl Into<NodeId>, name: impl Into<String>) -> Arc<Self> {
            Arc::new(Self {
                id: id.into(),
                name: name.into(),
            })
        }
    }

    #[async_trait]
    impl Node for TestNode {
        async fn execute(
            &self,
            state: &mut GraphState,
            _context: &ExecutionContext,
        ) -> RGraphResult<ExecutionResult> {
            state.set(
                "executed_nodes",
                StateValue::Array(vec![StateValue::String(self.name.clone())]),
            );
            Ok(ExecutionResult::Continue)
        }

        fn id(&self) -> &NodeId {
            &self.id
        }

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

    #[tokio::test]
    async fn test_graph_creation() {
        let mut graph = WorkflowGraph::new("test_graph");
        assert_eq!(graph.name(), "test_graph");

        let node = TestNode::new("test_node", "Test Node");
        graph.add_node("test_node", node).await.unwrap();

        assert_eq!(graph.node_ids().len(), 1);
        assert!(graph.node_ids().contains(&NodeId::new("test_node")));
    }

    #[tokio::test]
    async fn test_graph_builder() {
        let node1 = TestNode::new("node1", "Node 1");
        let node2 = TestNode::new("node2", "Node 2");

        let graph = GraphBuilder::new("test_graph")
            .description("A test graph")
            .add_node("node1", node1)
            .await
            .unwrap()
            .add_node("node2", node2)
            .await
            .unwrap()
            .add_edge("node1", "node2")
            .unwrap()
            .build()
            .unwrap();

        assert_eq!(graph.name(), "test_graph");
        assert_eq!(graph.description(), Some("A test graph"));
        assert_eq!(graph.node_ids().len(), 2);
    }

    #[test]
    fn test_node_id() {
        let id1 = NodeId::new("test");
        let id2 = NodeId::from("test");
        let id3: NodeId = "test".into();

        assert_eq!(id1, id2);
        assert_eq!(id2, id3);
        assert_eq!(id1.as_str(), "test");
    }

    #[test]
    fn test_execution_context() {
        let context = ExecutionContext::new("graph1".to_string(), NodeId::new("node1"))
            .with_metadata("key".to_string(), serde_json::json!("value"));

        assert_eq!(context.graph_id, "graph1");
        assert_eq!(context.current_node, NodeId::new("node1"));
        assert!(context.metadata.contains_key("key"));
    }
}