motosan_agent_workflow/lib.rs
1//! # motosan-agent-workflow
2//!
3//! A general-purpose DAG-based agent workflow engine for orchestrating LLM agents.
4//!
5//! ## Quick Start
6//!
7//! ```rust,no_run
8//! use motosan_agent_workflow::*;
9//! use serde_json::json;
10//!
11//! // Build a workflow
12//! let workflow = Workflow::builder("research")
13//! .name("Research Pipeline")
14//! .node(
15//! Node::agent("researcher")
16//! .system_prompt("Find key facts about the topic.")
17//! .build(),
18//! )
19//! .node(
20//! Node::agent("writer")
21//! .system_prompt("Write a structured report.")
22//! .input_from("researcher")
23//! .build(),
24//! )
25//! .edge("researcher", "writer")
26//! .build()
27//! .unwrap();
28//! ```
29//!
30//! ## Node Types
31//!
32//! | Type | Description | LLM |
33//! |------|-------------|-----|
34//! | [`Node::agent`] | LLM-powered agent | Yes |
35//! | [`Node::human`] | Human-in-the-loop gate | No |
36//! | [`Node::transform`] | Pure function transform | No |
37//! | [`Node::condition`] | Conditional branching | No |
38//! | [`Node::loop_node`] | Iterative loop with `_loop` context injection | Depends |
39//! | [`Node::sub_workflow`] | Nested sub-workflow | Depends |
40//!
41//! ## Features
42//!
43//! - **DAG execution**: Topological sort with automatic parallelization
44//! - **YAML/JSON loader**: [`load_workflow`] and [`load_workflow_from_str`]
45//! - **Schema validation**: JSON Schema with auto self-correction
46//! - **Retry policies**: Exponential backoff with Skip/Abort/Fallback modes
47//! - **Event streaming**: [`WorkflowEvent`] via tokio mpsc channel
48//! - **Cost estimation**: [`estimate_cost`] with per-node token tracking
49//! - **Skill system**: [`SkillResolver`] with extends/compose resolution
50//! - **Node `$ref`**: Reusable node definitions via [`load_workflow_with_base`]
51//! - **Loop context injection**: Body nodes receive `_loop` with iteration, history, previous
52//! - **8 built-in templates**: [`builtin_workflows`]
53//!
54//! ## Feature Flags
55//!
56//! | Flag | Description |
57//! |------|-------------|
58//! | `ag-ui` | AG-UI protocol event adapter |
59
60#[cfg(feature = "ag-ui")]
61pub mod ag_ui;
62
63pub mod context;
64pub mod cost;
65pub mod dag;
66pub mod error;
67pub mod event;
68pub mod execution;
69pub mod loader;
70pub mod node;
71pub mod runtime;
72pub mod schema;
73pub mod skill;
74pub mod state;
75pub mod templates;
76pub mod workflow;
77
78// Re-exports for convenience.
79pub use context::WorkflowContext;
80pub use error::{Result, WorkflowError};
81pub use event::WorkflowEvent;
82pub use node::{
83 AgentConfig, ConditionBranch, ConditionConfig, ConditionOp, FailureMode, HumanConfig,
84 AsyncTransformFn, IntoInputIds, LoopConfig, Node, NodeKind, RetryPolicy, SubWorkflowConfig,
85 TransformConfig, TransformFn,
86};
87pub use runtime::{
88 HumanInputProvider, HumanResponse, LlmClient, LlmResponse, NodeOutput, ResumeToken, Runtime,
89 TokenUsage, WorkflowResult, WorkflowStatus,
90};
91pub use cost::{estimate_cost, CostEstimate, ModelPricing, NodeCostEstimate};
92pub use loader::{load_workflow, load_workflow_from_str, load_workflow_with_base, Format};
93pub use skill::{
94 parse_skill_md, ComposeRef, FileSystemSkillProvider, SkillDef, SkillProvider, SkillResolver,
95 SkillType,
96};
97pub use state::{DiffResult, FileStateStore, StateStore, diff_and_store};
98pub use templates::{builtin_workflow, builtin_workflows};
99pub use workflow::{Edge, Workflow, WorkflowBuilder};
100pub use execution::{
101 ErrorSummary, ExecutionFilter, ExecutionObserver, ExecutionRecord, ExecutionStats,
102 ExecutionStatus, ExecutionStore, FileExecutionStore, LlmCallRecord, NodeRecord, NodeStatus,
103 ToolCallRecord, TriggerType, WorkflowStats,
104};