graph_flow/task.rs
1//! Task definition and execution control.
2//!
3//! This module contains the core [`Task`] trait and related types for defining
4//! workflow steps and controlling execution flow.
5//!
6//! # Examples
7//!
8//! ## Basic Task Implementation
9//!
10//! ```rust
11//! use graph_flow::{Task, TaskResult, NextAction, Context};
12//! use async_trait::async_trait;
13//!
14//! struct HelloTask;
15//!
16//! #[async_trait]
17//! impl Task for HelloTask {
18//! fn id(&self) -> &str {
19//! "hello_task"
20//! }
21//!
22//! async fn run(&self, context: Context) -> graph_flow::Result<TaskResult> {
23//! let name: String = context.get("name").await.unwrap_or("World".to_string());
24//! let greeting = format!("Hello, {}!", name);
25//!
26//! // Store result for next task
27//! context.set("greeting", greeting.clone()).await;
28//!
29//! Ok(TaskResult::new(Some(greeting), NextAction::Continue))
30//! }
31//! }
32//! ```
33//!
34//! ## Task with Different Control Flow
35//!
36//! ```rust
37//! # use graph_flow::{Task, TaskResult, NextAction, Context};
38//! # use async_trait::async_trait;
39//! struct ConditionalTask;
40//!
41//! #[async_trait]
42//! impl Task for ConditionalTask {
43//! fn id(&self) -> &str {
44//! "conditional_task"
45//! }
46//!
47//! async fn run(&self, context: Context) -> graph_flow::Result<TaskResult> {
48//! let user_input: Option<String> = context.get("user_input").await;
49//!
50//! match user_input {
51//! Some(input) if !input.is_empty() => {
52//! // Process input and continue automatically
53//! context.set("processed", input.to_uppercase()).await;
54//! Ok(TaskResult::new(
55//! Some("Input processed".to_string()),
56//! NextAction::ContinueAndExecute
57//! ))
58//! }
59//! _ => {
60//! // Wait for user input
61//! Ok(TaskResult::new(
62//! Some("Please provide input".to_string()),
63//! NextAction::WaitForInput
64//! ))
65//! }
66//! }
67//! }
68//! }
69//! ```
70
71use async_trait::async_trait;
72use serde::{Deserialize, Serialize};
73
74use crate::{context::Context, error::Result};
75
76/// Result of a task execution.
77///
78/// Contains the response to send to the user and the next action to take.
79/// The `task_id` field is automatically set by the graph execution engine.
80///
81/// # Examples
82///
83/// ```rust
84/// use graph_flow::{TaskResult, NextAction};
85///
86/// // Basic task result
87/// let result = TaskResult::new(
88/// Some("Task completed successfully".to_string()),
89/// NextAction::Continue
90/// );
91///
92/// // Task result with status message
93/// let result = TaskResult::new_with_status(
94/// Some("Data validated".to_string()),
95/// NextAction::Continue,
96/// Some("All validation checks passed".to_string())
97/// );
98///
99/// // Convenience methods
100/// let result = TaskResult::move_to_next(); // Continue to next task
101/// let result = TaskResult::move_to_next_direct(); // Continue and execute immediately
102/// ```
103#[derive(Debug, Clone, Serialize, Deserialize)]
104pub struct TaskResult {
105 /// Response to send to the user
106 pub response: Option<String>,
107 /// Next action to take
108 pub next_action: NextAction,
109 /// ID of the task that generated this result
110 pub task_id: String,
111 /// Optional status message that describes the current state of the task
112 pub status_message: Option<String>,
113}
114
115impl TaskResult {
116 /// Create a new TaskResult with the given response and next action.
117 ///
118 /// The task_id will be set automatically by the graph execution engine.
119 ///
120 /// # Examples
121 ///
122 /// ```rust
123 /// use graph_flow::{TaskResult, NextAction};
124 ///
125 /// let result = TaskResult::new(
126 /// Some("Hello, World!".to_string()),
127 /// NextAction::Continue
128 /// );
129 /// ```
130 pub fn new(response: Option<String>, next_action: NextAction) -> Self {
131 Self {
132 response,
133 next_action,
134 task_id: String::new(),
135 status_message: None,
136 }
137 }
138
139 /// Create a new TaskResult with response, next action, and status message.
140 ///
141 /// The status message is used to describe the current state of the task.
142 /// It's only persisted in the context but not returned to the user.
143 /// Specifically aimed at debugging and logging.
144 ///
145 /// # Examples
146 ///
147 /// ```rust
148 /// use graph_flow::{TaskResult, NextAction};
149 ///
150 /// let result = TaskResult::new_with_status(
151 /// Some("Data processed".to_string()),
152 /// NextAction::Continue,
153 /// Some("Processing completed with 95% confidence".to_string())
154 /// );
155 /// ```
156 pub fn new_with_status(
157 response: Option<String>,
158 next_action: NextAction,
159 status_message: Option<String>,
160 ) -> Self {
161 Self {
162 response,
163 next_action,
164 task_id: String::new(),
165 status_message,
166 }
167 }
168
169 /// Create a TaskResult that moves to the next task (step-by-step execution).
170 ///
171 /// This is a convenience method equivalent to:
172 /// ```rust
173 /// # use graph_flow::{TaskResult, NextAction};
174 /// TaskResult::new(None, NextAction::Continue);
175 /// ```
176 pub fn move_to_next() -> Self {
177 Self {
178 response: None,
179 next_action: NextAction::Continue,
180 task_id: String::new(),
181 status_message: None,
182 }
183 }
184
185 /// Create a TaskResult that moves to the next task and executes it immediately.
186 ///
187 /// This is a convenience method equivalent to:
188 /// ```rust
189 /// # use graph_flow::{TaskResult, NextAction};
190 /// TaskResult::new(None, NextAction::ContinueAndExecute);
191 /// ```
192 pub fn move_to_next_direct() -> Self {
193 Self {
194 response: None,
195 next_action: NextAction::ContinueAndExecute,
196 task_id: String::new(),
197 status_message: None,
198 }
199 }
200}
201
202/// Defines what should happen after a task completes.
203///
204/// This enum controls the flow of execution in your workflow graph.
205/// Different variants provide different execution behaviors.
206///
207/// # Examples
208///
209/// ```rust
210/// use graph_flow::{NextAction, TaskResult};
211///
212/// // Step-by-step execution (pause after this task)
213/// let result = TaskResult::new(
214/// Some("Step 1 complete".to_string()),
215/// NextAction::Continue
216/// );
217///
218/// // Continuous execution (run next task immediately)
219/// let result = TaskResult::new(
220/// Some("Processing...".to_string()),
221/// NextAction::ContinueAndExecute
222/// );
223///
224/// // Wait for user input
225/// let result = TaskResult::new(
226/// Some("Please provide more information".to_string()),
227/// NextAction::WaitForInput
228/// );
229///
230/// // Jump to specific task
231/// let result = TaskResult::new(
232/// Some("Redirecting to error handler".to_string()),
233/// NextAction::GoTo("error_handler".to_string())
234/// );
235///
236/// // End the workflow
237/// let result = TaskResult::new(
238/// Some("Workflow completed!".to_string()),
239/// NextAction::End
240/// );
241/// ```
242#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
243pub enum NextAction {
244 /// Continue to the next task in the default path (step-by-step execution).
245 ///
246 /// The workflow will pause after this task and wait for the next
247 /// execution call. This gives you control over when the next task runs.
248 ///
249 /// Best for: Interactive applications, web services, debugging
250 Continue,
251
252 /// Continue to the next task and execute it immediately (continuous execution).
253 ///
254 /// The workflow will automatically proceed to the next task without
255 /// pausing. This creates a recursive execution until the workflow
256 /// reaches `End`, `WaitForInput`, or an error.
257 ///
258 /// Best for: Batch processing, automated workflows
259 ContinueAndExecute,
260
261 /// Go to a specific task by ID.
262 ///
263 /// Jump directly to the specified task, skipping the normal edge-based
264 /// flow. Useful for error handling, loops, or dynamic routing.
265 ///
266 /// # Examples
267 ///
268 /// ```rust
269 /// # use graph_flow::{NextAction, TaskResult};
270 /// // Jump to error handler
271 /// let result = TaskResult::new(
272 /// Some("Error detected, routing to handler".to_string()),
273 /// NextAction::GoTo("error_handler".to_string())
274 /// );
275 ///
276 /// // Create a retry loop
277 /// let result = TaskResult::new(
278 /// Some("Retrying...".to_string()),
279 /// NextAction::GoTo("validation_task".to_string())
280 /// );
281 /// ```
282 GoTo(String),
283
284 /// Go back to the previous task.
285 ///
286 /// Note: This currently stays at the current task. Full back navigation
287 /// logic may be implemented in future versions.
288 GoBack,
289
290 /// End the graph execution.
291 ///
292 /// Terminates the workflow completely. No further tasks will be executed.
293 End,
294
295 /// Wait for user input before continuing.
296 ///
297 /// Pauses the workflow and waits for external input. The workflow
298 /// will stay at the current task until new data is provided and
299 /// execution is resumed.
300 ///
301 /// Best for: Human-in-the-loop workflows, interactive applications
302 WaitForInput,
303}
304
305/// Core trait that all tasks must implement.
306///
307/// Tasks are the building blocks of your workflow. Each task represents
308/// a unit of work that can access shared context and control the flow
309/// of execution.
310///
311/// # Examples
312///
313/// ## Basic Task
314///
315/// ```rust
316/// use graph_flow::{Task, TaskResult, NextAction, Context};
317/// use async_trait::async_trait;
318///
319/// struct GreetingTask;
320///
321/// #[async_trait]
322/// impl Task for GreetingTask {
323/// fn id(&self) -> &str {
324/// "greeting"
325/// }
326///
327/// async fn run(&self, context: Context) -> graph_flow::Result<TaskResult> {
328/// let name: String = context.get("name").await.unwrap_or("World".to_string());
329/// let greeting = format!("Hello, {}!", name);
330///
331/// Ok(TaskResult::new(Some(greeting), NextAction::Continue))
332/// }
333/// }
334/// ```
335///
336/// ## Task with Default ID
337///
338/// ```rust
339/// # use graph_flow::{Task, TaskResult, NextAction, Context};
340/// # use async_trait::async_trait;
341/// struct DefaultIdTask;
342///
343/// #[async_trait]
344/// impl Task for DefaultIdTask {
345/// // id() is automatically implemented using the type name
346///
347/// async fn run(&self, context: Context) -> graph_flow::Result<TaskResult> {
348/// Ok(TaskResult::new(None, NextAction::End))
349/// }
350/// }
351/// ```
352///
353/// ## Complex Task with Error Handling
354///
355/// ```rust
356/// # use graph_flow::{Task, TaskResult, NextAction, Context, GraphError};
357/// # use async_trait::async_trait;
358/// struct ValidationTask {
359/// max_retries: usize,
360/// }
361///
362/// #[async_trait]
363/// impl Task for ValidationTask {
364/// fn id(&self) -> &str {
365/// "validator"
366/// }
367///
368/// async fn run(&self, context: Context) -> graph_flow::Result<TaskResult> {
369/// let data: Option<String> = context.get("data").await;
370/// let retry_count: usize = context.get("retry_count").await.unwrap_or(0);
371///
372/// match data {
373/// Some(data) if self.validate(&data) => {
374/// context.set("retry_count", 0).await; // Reset counter
375/// Ok(TaskResult::new(
376/// Some("Validation passed".to_string()),
377/// NextAction::Continue
378/// ))
379/// }
380/// Some(_) if retry_count < self.max_retries => {
381/// context.set("retry_count", retry_count + 1).await;
382/// Ok(TaskResult::new(
383/// Some("Validation failed, retrying...".to_string()),
384/// NextAction::GoTo("data_input".to_string())
385/// ))
386/// }
387/// _ => {
388/// Err(GraphError::TaskExecutionFailed(
389/// "Validation failed after max retries".to_string()
390/// ))
391/// }
392/// }
393/// }
394/// }
395///
396/// impl ValidationTask {
397/// fn validate(&self, data: &str) -> bool {
398/// !data.is_empty() && data.len() > 5
399/// }
400/// }
401/// ```
402#[async_trait]
403pub trait Task: Send + Sync {
404 /// Unique identifier for this task.
405 ///
406 /// By default, this returns the type name of the implementing struct.
407 /// Override this method if you need a custom identifier.
408 ///
409 /// # Examples
410 ///
411 /// ```rust
412 /// # use graph_flow::Task;
413 /// # use async_trait::async_trait;
414 /// # use graph_flow::{TaskResult, NextAction, Context};
415 /// // Using default implementation (type name)
416 /// struct MyTask;
417 ///
418 /// #[async_trait]
419 /// impl Task for MyTask {
420 /// // id() will return "my_module::MyTask"
421 /// async fn run(&self, _context: Context) -> graph_flow::Result<TaskResult> {
422 /// Ok(TaskResult::new(None, NextAction::End))
423 /// }
424 /// }
425 ///
426 /// // Using custom ID
427 /// struct CustomTask;
428 ///
429 /// #[async_trait]
430 /// impl Task for CustomTask {
431 /// fn id(&self) -> &str {
432 /// "custom_task_id"
433 /// }
434 ///
435 /// async fn run(&self, _context: Context) -> graph_flow::Result<TaskResult> {
436 /// Ok(TaskResult::new(None, NextAction::End))
437 /// }
438 /// }
439 /// ```
440 fn id(&self) -> &str {
441 std::any::type_name::<Self>()
442 }
443
444 /// Execute the task with the given context.
445 ///
446 /// This is where you implement your task's logic. You have access to
447 /// the shared context for reading input data and storing results.
448 ///
449 /// # Parameters
450 ///
451 /// * `context` - Shared context containing workflow state and data
452 ///
453 /// # Returns
454 ///
455 /// Returns a `Result<TaskResult>` where:
456 /// - `Ok(TaskResult)` indicates successful execution
457 /// - `Err(GraphError)` indicates an error that should stop the workflow
458 ///
459 /// # Examples
460 ///
461 /// ```rust
462 /// # use graph_flow::{Task, TaskResult, NextAction, Context};
463 /// # use async_trait::async_trait;
464 /// struct DataProcessor;
465 ///
466 /// #[async_trait]
467 /// impl Task for DataProcessor {
468 /// fn id(&self) -> &str {
469 /// "data_processor"
470 /// }
471 ///
472 /// async fn run(&self, context: Context) -> graph_flow::Result<TaskResult> {
473 /// // Read input from context
474 /// let input: String = context.get("raw_data").await
475 /// .unwrap_or_default();
476 ///
477 /// // Process the data
478 /// let processed = self.process_data(&input).await?;
479 ///
480 /// // Store result for next task
481 /// context.set("processed_data", processed.clone()).await;
482 ///
483 /// // Return result with next action
484 /// Ok(TaskResult::new(
485 /// Some(format!("Processed {} bytes", processed.len())),
486 /// NextAction::Continue
487 /// ))
488 /// }
489 /// }
490 ///
491 /// impl DataProcessor {
492 /// async fn process_data(&self, input: &str) -> graph_flow::Result<String> {
493 /// // Your processing logic here
494 /// Ok(input.to_uppercase())
495 /// }
496 /// }
497 /// ```
498 async fn run(&self, context: Context) -> Result<TaskResult>;
499}
500
501#[cfg(test)]
502mod tests {
503 use super::*;
504 use async_trait::async_trait;
505
506 struct TestTaskWithDefaultId;
507
508 #[async_trait]
509 impl Task for TestTaskWithDefaultId {
510 async fn run(&self, _context: Context) -> Result<TaskResult> {
511 Ok(TaskResult::new(None, NextAction::End))
512 }
513 }
514
515 struct TestTaskWithCustomId;
516
517 #[async_trait]
518 impl Task for TestTaskWithCustomId {
519 fn id(&self) -> &str {
520 "custom_task_id"
521 }
522
523 async fn run(&self, _context: Context) -> Result<TaskResult> {
524 Ok(TaskResult::new(None, NextAction::End))
525 }
526 }
527
528 #[test]
529 fn test_default_id_implementation() {
530 let task = TestTaskWithDefaultId;
531 assert_eq!(task.id(), "graph_flow::task::tests::TestTaskWithDefaultId");
532 }
533
534 #[test]
535 fn test_custom_id_override() {
536 let task = TestTaskWithCustomId;
537 assert_eq!(task.id(), "custom_task_id");
538 }
539}