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
//! # RGraph - Graph-based Agent Orchestration System
//!
//! RGraph is a powerful graph-based workflow orchestration system designed for building
//! sophisticated AI agent applications. Inspired by LangGraph, it provides a declarative
//! way to define complex agent workflows with state management, conditional execution,
//! and seamless integration with the RRAG framework.
//!
//! ## Key Features
//!
//! - **Graph-Based Workflows**: Define agent behavior as directed graphs
//! - **State Management**: Persistent state across node executions
//! - **Conditional Routing**: Dynamic workflow paths based on execution results
//! - **Agent Orchestration**: Coordinate multiple AI agents and tools
//! - **RRAG Integration**: Built-in support for RAG-powered agents
//! - **Async Execution**: High-performance concurrent execution
//! - **Observability**: Comprehensive monitoring and debugging
//! - **Persistence**: Durable workflow state and history
//!
//! ## Quick Start
//!
//! ```rust
//! use rexis_graph::prelude::*;
//!
//! # async fn example() -> RGraphResult<()> {
//! // Define a simple agent workflow
//! let mut graph = WorkflowGraph::new("research_assistant");
//!
//! // Add nodes
//! graph.add_node("understand_query", QueryAnalysisNode::new()).await?;
//! graph.add_node("search_knowledge", RagSearchNode::new()).await?;
//! graph.add_node("synthesize_response", ResponseGenerationNode::new()).await?;
//!
//! // Define edges
//! graph.add_edge("understand_query", "search_knowledge")?;
//! graph.add_edge("search_knowledge", "synthesize_response")?;
//!
//! // Execute the workflow
//! let initial_state = GraphState::new()
//! .with_input("user_query", "What is machine learning?");
//!
//! let result = graph.execute(initial_state).await?;
//! tracing::debug!("Response: {}", result.get_output("final_response")?);
//! # Ok(())
//! # }
//! ```
//!
//! ## Advanced Examples
//!
//! ### Multi-Agent Collaboration
//! ```rust
//! use rexis_graph::prelude::*;
//!
//! # async fn example() -> RGraphResult<()> {
//! let mut graph = WorkflowGraph::new("multi_agent_system");
//!
//! // Research agent
//! graph.add_node("researcher",
//! AgentNode::new("research_agent")
//! .with_system_prompt("You are a research specialist...")
//! .with_tools(vec![web_search_tool(), database_query_tool()])
//! ).await?;
//!
//! // Analysis agent
//! graph.add_node("analyst",
//! AgentNode::new("analysis_agent")
//! .with_system_prompt("You analyze research data...")
//! .with_tools(vec![data_analysis_tool(), visualization_tool()])
//! ).await?;
//!
//! // Writer agent
//! graph.add_node("writer",
//! AgentNode::new("writing_agent")
//! .with_system_prompt("You write comprehensive reports...")
//! ).await?;
//!
//! // Conditional routing based on research results
//! graph.add_conditional_edge("researcher", |state: &GraphState| {
//! if state.get("research_quality_score")? > 0.8 {
//! Ok("analyst".to_string())
//! } else {
//! Ok("researcher".to_string()) // Loop back for more research
//! }
//! })?;
//!
//! graph.add_edge("analyst", "writer")?;
//!
//! let result = graph.execute(
//! GraphState::new().with_input("research_topic", "Climate Change Impact")
//! ).await?;
//! # Ok(())
//! # }
//! ```
//!
//! ### RAG-Powered Knowledge Agent
//! ```rust
//! use rexis_graph::prelude::*;
//! # #[cfg(feature = "rexis-rag-integration")]
//! # async fn example() -> RGraphResult<()> {
//! let mut graph = WorkflowGraph::new("knowledge_agent");
//!
//! // RAG retrieval node
//! graph.add_node("retrieve_context",
//! RagRetrievalNode::new()
//! .with_rrag_system(rrag_system)
//! .with_top_k(5)
//! .with_score_threshold(0.7)
//! ).await?;
//!
//! // Context evaluation node
//! graph.add_node("evaluate_context",
//! ContextEvaluationNode::new()
//! .with_relevance_threshold(0.6)
//! ).await?;
//!
//! // Response generation with context
//! graph.add_node("generate_response",
//! ContextualGenerationNode::new()
//! .with_context_window(4096)
//! ).await?;
//!
//! // Conditional routing based on context quality
//! graph.add_conditional_edge("evaluate_context", |state: &GraphState| {
//! let context_score: f32 = state.get("context_relevance_score")?;
//! if context_score > 0.6 {
//! Ok("generate_response".to_string())
//! } else {
//! Ok("retrieve_context".to_string()) // Retry with different strategy
//! }
//! })?;
//!
//! let result = graph.execute(
//! GraphState::new().with_input("query", "Explain quantum computing")
//! ).await?;
//! # Ok(())
//! # }
//! ```
//!
//! ## Architecture
//!
//! RGraph is built around several core concepts:
//!
//! ### Workflow Graph
//! A directed graph representing the agent workflow, where nodes are execution units
//! and edges define the flow of control and data.
//!
//! ### Graph State
//! A shared state object that flows through the graph, accumulating results and
//! providing context for decision-making.
//!
//! ### Nodes
//! Execution units that perform specific tasks:
//! - **Agent Nodes**: LLM-powered agents with tools
//! - **Tool Nodes**: Direct tool execution
//! - **RAG Nodes**: Retrieval-augmented generation
//! - **Condition Nodes**: Decision points in the workflow
//! - **Transform Nodes**: Data transformation and processing
//!
//! ### Execution Engine
//! The runtime system that executes graphs with support for:
//! - Parallel execution where possible
//! - State management and persistence
//! - Error handling and recovery
//! - Observability and debugging
//!
//! ## Integration with RRAG
//!
//! RGraph seamlessly integrates with the RRAG framework to provide:
//! - RAG-powered agent nodes
//! - Knowledge retrieval capabilities
//! - Document processing workflows
//! - Embedding-based routing decisions
//! - Multi-modal processing support
// Re-export core types for easy access
pub use crate;
pub use crate;
pub use crate;
pub use crate;
pub use crate;
// Error handling
use Error;
/// Result type for RGraph operations
pub type RGraphResult<T> = ;
/// Error types for RGraph operations
/// Framework constants
pub const VERSION: &str = env!;
pub const NAME: &str = "RGraph";
pub const DESCRIPTION: &str = "Graph-based Agent Orchestration System";