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
//! # graph-flow
//!
//! A high-performance, type-safe framework for building multi-agent workflow systems in Rust.
//!
//! ## Features
//!
//! - **Type-Safe Workflows**: Compile-time guarantees for workflow correctness
//! - **Flexible Execution**: Step-by-step, batch, or mixed execution modes
//! - **Built-in Persistence**: PostgreSQL and in-memory storage backends
//! - **LLM Integration**: Optional integration with Rig for AI agent capabilities
//! - **Human-in-the-Loop**: Natural workflow interruption and resumption
//! - **Async/Await Native**: Built from the ground up for async Rust
//!
//! ## Quick Start
//!
//! ```rust
//! use graph_flow::{Context, Task, TaskResult, NextAction, GraphBuilder, FlowRunner, InMemorySessionStorage, Session, SessionStorage};
//! use async_trait::async_trait;
//! use std::sync::Arc;
//!
//! // Define a task
//! struct HelloTask;
//!
//! #[async_trait]
//! impl Task for HelloTask {
//! fn id(&self) -> &str {
//! "hello_task"
//! }
//!
//! async fn run(&self, context: Context) -> graph_flow::Result<TaskResult> {
//! let name: String = context.get("name").await.unwrap_or("World".to_string());
//! let greeting = format!("Hello, {}!", name);
//!
//! context.set("greeting", greeting.clone()).await;
//! Ok(TaskResult::new(Some(greeting), NextAction::Continue))
//! }
//! }
//!
//! # #[tokio::main]
//! # async fn main() -> graph_flow::Result<()> {
//! // Build the workflow
//! let hello_task = Arc::new(HelloTask);
//! let graph = Arc::new(
//! GraphBuilder::new("greeting_workflow")
//! .add_task(hello_task.clone())
//! .build()
//! );
//!
//! // Set up session storage and runner
//! let session_storage = Arc::new(InMemorySessionStorage::new());
//! let flow_runner = FlowRunner::new(graph.clone(), session_storage.clone());
//!
//! // Create and execute session
//! let session = Session::new_from_task("user_123".to_string(), hello_task.id());
//! session.context.set("name", "Alice".to_string()).await;
//! session_storage.save(session).await?;
//!
//! let result = flow_runner.run("user_123").await?;
//! println!("Response: {:?}", result.response);
//! # Ok(())
//! # }
//! ```
//!
//! ## Core Concepts
//!
//! ### Tasks
//!
//! Tasks are the building blocks of your workflow. They implement the [`Task`] trait:
//!
//! ```rust
//! use graph_flow::{Task, TaskResult, NextAction, Context};
//! use async_trait::async_trait;
//!
//! struct MyTask;
//!
//! #[async_trait]
//! impl Task for MyTask {
//! fn id(&self) -> &str {
//! "my_task"
//! }
//!
//! async fn run(&self, context: Context) -> graph_flow::Result<TaskResult> {
//! // Your task logic here
//! Ok(TaskResult::new(Some("Done!".to_string()), NextAction::End))
//! }
//! }
//! ```
//!
//! ### Context
//!
//! The [`Context`] provides thread-safe state management across your workflow:
//!
//! ```rust
//! # use graph_flow::Context;
//! # #[tokio::main]
//! # async fn main() {
//! let context = Context::new();
//!
//! // Store and retrieve data
//! context.set("key", "value").await;
//! let value: Option<String> = context.get("key").await;
//!
//! // Chat history management
//! context.add_user_message("Hello!".to_string()).await;
//! context.add_assistant_message("Hi there!".to_string()).await;
//! # }
//! ```
//!
//! ### Graph Building
//!
//! Use [`GraphBuilder`] to create complex workflows:
//!
//! ```rust
//! # use graph_flow::{GraphBuilder, Task, TaskResult, NextAction, Context};
//! # use async_trait::async_trait;
//! # use std::sync::Arc;
//! # struct Task1; struct Task2; struct Task3;
//! # #[async_trait] impl Task for Task1 { fn id(&self) -> &str { "task1" } async fn run(&self, _: Context) -> graph_flow::Result<TaskResult> { Ok(TaskResult::new(None, NextAction::End)) } }
//! # #[async_trait] impl Task for Task2 { fn id(&self) -> &str { "task2" } async fn run(&self, _: Context) -> graph_flow::Result<TaskResult> { Ok(TaskResult::new(None, NextAction::End)) } }
//! # #[async_trait] impl Task for Task3 { fn id(&self) -> &str { "task3" } async fn run(&self, _: Context) -> graph_flow::Result<TaskResult> { Ok(TaskResult::new(None, NextAction::End)) } }
//! # let task1 = Arc::new(Task1); let task2 = Arc::new(Task2); let task3 = Arc::new(Task3);
//! let graph = GraphBuilder::new("my_workflow")
//! .add_task(task1.clone())
//! .add_task(task2.clone())
//! .add_task(task3.clone())
//! .add_edge(task1.id(), task2.id())
//! .add_conditional_edge(
//! task2.id(),
//! |ctx| ctx.get_sync::<bool>("condition").unwrap_or(false),
//! task3.id(), // if true
//! task1.id(), // if false
//! )
//! .build();
//! ```
//!
//! ### Execution
//!
//! Use [`FlowRunner`] for convenient session-based execution:
//!
//! ```rust,no_run
//! # use graph_flow::{FlowRunner, InMemorySessionStorage, Session, Graph, SessionStorage};
//! # use std::sync::Arc;
//! # #[tokio::main]
//! # async fn main() -> graph_flow::Result<()> {
//! # let graph = Arc::new(Graph::new("test"));
//! let storage = Arc::new(InMemorySessionStorage::new());
//! let runner = FlowRunner::new(graph, storage.clone());
//!
//! // Create session
//! let session = Session::new_from_task("session_id".to_string(), "start_task");
//! storage.save(session).await?;
//!
//! // Execute workflow
//! let result = runner.run("session_id").await?;
//! # Ok(())
//! # }
//! ```
//!
//! ## Features
//!
//! - **Default**: Core workflow functionality
//! - **`rig`**: Enables LLM integration via the Rig crate
//!
//! ## Storage Backends
//!
//! - [`InMemorySessionStorage`]: For development and testing
//! - [`PostgresSessionStorage`]: For production use with PostgreSQL
// Re-export commonly used types
pub use ;
pub use ;
pub use ;
pub use FlowRunner;
pub use ;
pub use PostgresSessionStorage;
pub use ;
pub use FanOutTask;