lc_langgraph/compiled/
types.rs1use crate::edge::GraphEdge;
5use crate::node::GraphNode;
6use crate::state::{StateSchema, StateUpdate};
7use async_trait::async_trait;
8use serde_json::Value as JsonValue;
9use std::collections::HashMap;
10use std::sync::Arc;
11
12#[derive(Debug)]
14pub struct GraphInvocation<S: StateSchema> {
15 pub final_state: S,
17 pub steps: Vec<ExecutionStep>,
19 pub recursion_count: usize,
21}
22
23impl<S: StateSchema> GraphInvocation<S> {
24 pub fn state(&self) -> &S {
26 &self.final_state
27 }
28
29 pub fn steps(&self) -> &[ExecutionStep] {
31 &self.steps
32 }
33}
34
35#[derive(Debug, Clone)]
37pub enum ExecutionStep {
38 Node {
40 name: String,
42 metadata: HashMap<String, JsonValue>,
44 },
45 Checkpoint {
47 id: String,
49 next_node: String,
51 },
52 ParallelNode {
54 branch: String,
56 metadata: HashMap<String, JsonValue>,
58 },
59}
60
61impl ExecutionStep {
62 pub fn node(name: impl Into<String>, metadata: HashMap<String, JsonValue>) -> Self {
64 Self::Node {
65 name: name.into(),
66 metadata,
67 }
68 }
69
70 pub fn checkpoint(id: impl Into<String>, next_node: impl Into<String>) -> Self {
72 Self::Checkpoint {
73 id: id.into(),
74 next_node: next_node.into(),
75 }
76 }
77
78 pub fn parallel_node(branch: impl Into<String>, metadata: HashMap<String, JsonValue>) -> Self {
80 Self::ParallelNode {
81 branch: branch.into(),
82 metadata,
83 }
84 }
85}
86
87#[derive(Debug, Clone)]
89pub struct ParallelBranch<S: StateSchema> {
90 pub name: String,
92 pub final_state: S,
94 pub steps: Vec<ExecutionStep>,
96}
97
98#[derive(Debug)]
100pub struct ParallelInvocation<S: StateSchema> {
101 pub final_state: S,
103 pub steps: Vec<ExecutionStep>,
105 pub recursion_count: usize,
107 pub parallel_branches: Vec<ParallelBranch<S>>,
109}
110
111impl<S: StateSchema> ParallelInvocation<S> {
112 pub fn state(&self) -> &S {
114 &self.final_state
115 }
116
117 pub fn branches(&self) -> &[ParallelBranch<S>] {
119 &self.parallel_branches
120 }
121}
122
123#[derive(Debug, Clone)]
125pub enum StreamEvent<S: StateSchema> {
126 Start(S),
128 EnterNode(String, S),
130 NodeComplete(String, StateUpdate<S>),
132 StateUpdate(S),
134 End(S),
136}
137
138impl<S: StateSchema> StreamEvent<S> {
139 pub fn start(state: S) -> Self {
141 Self::Start(state)
142 }
143
144 pub fn enter_node(name: impl Into<String>, state: S) -> Self {
146 Self::EnterNode(name.into(), state)
147 }
148
149 pub fn node_complete(name: impl Into<String>, update: StateUpdate<S>) -> Self {
151 Self::NodeComplete(name.into(), update)
152 }
153
154 pub fn state_update(state: S) -> Self {
156 Self::StateUpdate(state)
157 }
158
159 pub fn end(state: S) -> Self {
161 Self::End(state)
162 }
163}
164
165#[derive(Debug, Clone)]
167pub struct GraphExecution<S: StateSchema> {
168 pub state: S,
170 pub current_node: String,
172 pub steps: Vec<ExecutionStep>,
174 pub recursion_count: usize,
176 pub interrupted_at: String,
178}
179
180impl<S: StateSchema> GraphExecution<S> {
181 pub fn new(
183 state: S,
184 current_node: impl Into<String>,
185 interrupted_at: impl Into<String>,
186 ) -> Self {
187 Self {
188 state,
189 current_node: current_node.into(),
190 steps: Vec::new(),
191 recursion_count: 0,
192 interrupted_at: interrupted_at.into(),
193 }
194 }
195
196 pub fn state(&self) -> &S {
198 &self.state
199 }
200
201 pub fn interrupted_at(&self) -> &str {
203 &self.interrupted_at
204 }
205}
206
207#[derive(Debug, Clone)]
211pub struct DynamicTask {
212 pub id: String,
214 pub description: String,
216}
217
218pub struct DynamicInjection<S: StateSchema> {
220 pub nodes: Vec<(String, Arc<dyn GraphNode<S>>)>,
222 pub edges: Vec<GraphEdge>,
224}
225
226#[async_trait]
228pub trait DynamicPlanner<S: StateSchema>: Send + Sync {
229 async fn plan(
231 &self,
232 tasks: &[DynamicTask],
233 current_state: &S,
234 ) -> Result<DynamicInjection<S>, String>;
235}