adk-graph 0.6.0

Graph-based workflow orchestration for ADK-Rust agents
Documentation
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
//! GraphAgent - ADK Agent integration for graph workflows
//!
//! Provides a builder pattern similar to LlmAgent and RealtimeAgent.

use crate::checkpoint::Checkpointer;
use crate::edge::{END, Edge, EdgeTarget, START};
use crate::error::{GraphError, Result};
use crate::graph::{CompiledGraph, StateGraph};
use crate::node::{ExecutionConfig, FunctionNode, Node, NodeContext, NodeOutput};
use crate::state::{State, StateSchema};
use crate::stream::{StreamEvent, StreamMode};
use adk_core::{Agent, Content, Event, EventStream, InvocationContext};
use async_trait::async_trait;
use serde_json::json;
use std::collections::HashMap;
use std::future::Future;
use std::pin::Pin;
use std::sync::Arc;

/// Type alias for callbacks
pub type BeforeAgentCallback = Arc<
    dyn Fn(Arc<dyn InvocationContext>) -> Pin<Box<dyn Future<Output = adk_core::Result<()>> + Send>>
        + Send
        + Sync,
>;

pub type AfterAgentCallback = Arc<
    dyn Fn(
            Arc<dyn InvocationContext>,
            Event,
        ) -> Pin<Box<dyn Future<Output = adk_core::Result<()>> + Send>>
        + Send
        + Sync,
>;

/// Type alias for input mapper function
pub type InputMapper = Arc<dyn Fn(&dyn InvocationContext) -> State + Send + Sync>;

/// Type alias for output mapper function
pub type OutputMapper = Arc<dyn Fn(&State) -> Vec<Event> + Send + Sync>;

/// GraphAgent wraps a CompiledGraph as an ADK Agent
pub struct GraphAgent {
    name: String,
    description: String,
    graph: Arc<CompiledGraph>,
    /// Map InvocationContext to graph input state
    input_mapper: InputMapper,
    /// Map graph output state to ADK Events
    output_mapper: OutputMapper,
    /// Before agent callback
    before_callback: Option<BeforeAgentCallback>,
    /// After agent callback
    after_callback: Option<AfterAgentCallback>,
}

impl GraphAgent {
    /// Create a new GraphAgent builder
    pub fn builder(name: &str) -> GraphAgentBuilder {
        GraphAgentBuilder::new(name)
    }

    /// Create directly from a compiled graph
    pub fn from_graph(name: &str, graph: CompiledGraph) -> Self {
        Self {
            name: name.to_string(),
            description: String::new(),
            graph: Arc::new(graph),
            input_mapper: Arc::new(default_input_mapper),
            output_mapper: Arc::new(default_output_mapper),
            before_callback: None,
            after_callback: None,
        }
    }

    /// Build a `GraphAgent` from a `WorkflowSchema`.
    ///
    /// Delegates to `schema.build_graph()` to construct the graph from the
    /// workflow schema's action nodes, edges, and conditions.
    #[cfg(feature = "action")]
    pub fn from_workflow_schema(
        name: &str,
        schema: &crate::workflow::WorkflowSchema,
    ) -> Result<Self> {
        schema.build_graph(name)
    }

    /// Get the underlying compiled graph
    pub fn graph(&self) -> &CompiledGraph {
        &self.graph
    }

    /// Execute the graph directly (bypassing Agent trait)
    pub async fn invoke(&self, input: State, config: ExecutionConfig) -> Result<State> {
        self.graph.invoke(input, config).await
    }

    /// Stream execution
    pub fn stream(
        &self,
        input: State,
        config: ExecutionConfig,
        mode: StreamMode,
    ) -> impl futures::Stream<Item = Result<StreamEvent>> + '_ {
        self.graph.stream(input, config, mode)
    }
}

#[async_trait]
impl Agent for GraphAgent {
    fn name(&self) -> &str {
        &self.name
    }

    fn description(&self) -> &str {
        &self.description
    }

    fn sub_agents(&self) -> &[Arc<dyn Agent>] {
        &[]
    }

    async fn run(&self, ctx: Arc<dyn InvocationContext>) -> adk_core::Result<EventStream> {
        // Call before callback
        if let Some(callback) = &self.before_callback {
            callback(ctx.clone()).await?;
        }

        // Map context to input state
        let input = (self.input_mapper)(ctx.as_ref());

        // Create execution config from context
        let config = ExecutionConfig::new(ctx.session_id());

        // Execute graph
        let graph = self.graph.clone();
        let output_mapper = self.output_mapper.clone();
        let after_callback = self.after_callback.clone();
        let ctx_clone = ctx.clone();

        let stream = async_stream::stream! {
            match graph.invoke(input, config).await {
                Ok(state) => {
                    let events = output_mapper(&state);
                    for event in events {
                        // Call after callback for each event
                        if let Some(callback) = &after_callback {
                            if let Err(e) = callback(ctx_clone.clone(), event.clone()).await {
                                yield Err(e);
                                return;
                            }
                        }
                        yield Ok(event);
                    }
                }
                Err(GraphError::Interrupted(interrupt)) => {
                    // Create an interrupt event
                    let mut event = Event::new("graph_interrupted");
                    event.set_content(Content::new("assistant").with_text(format!(
                        "Graph interrupted: {:?}\nThread: {}\nCheckpoint: {}",
                        interrupt.interrupt,
                        interrupt.thread_id,
                        interrupt.checkpoint_id
                    )));
                    yield Ok(event);
                }
                Err(e) => {
                    yield Err(adk_core::AdkError::agent(e.to_string()));
                }
            }
        };

        Ok(Box::pin(stream))
    }
}

/// Default input mapper - extracts content from InvocationContext
fn default_input_mapper(ctx: &dyn InvocationContext) -> State {
    let mut state = State::new();

    // Get user content
    let content = ctx.user_content();
    let text: String = content.parts.iter().filter_map(|p| p.text()).collect::<Vec<_>>().join("\n");

    if !text.is_empty() {
        state.insert("input".to_string(), json!(text));
        state.insert("messages".to_string(), json!([{"role": "user", "content": text}]));
    }

    // Add session ID
    state.insert("session_id".to_string(), json!(ctx.session_id()));

    state
}

/// Default output mapper - creates events from state
fn default_output_mapper(state: &State) -> Vec<Event> {
    let mut events = Vec::new();

    // Try to get output from common fields
    let output_text = state
        .get("output")
        .and_then(|v| v.as_str())
        .or_else(|| state.get("result").and_then(|v| v.as_str()))
        .or_else(|| {
            state
                .get("messages")
                .and_then(|v| v.as_array())
                .and_then(|arr| arr.last())
                .and_then(|msg| msg.get("content"))
                .and_then(|c| c.as_str())
        });

    let text = if let Some(text) = output_text {
        text.to_string()
    } else {
        // Return the full state as JSON
        serde_json::to_string_pretty(state).unwrap_or_default()
    };

    let mut event = Event::new("graph_output");
    event.set_content(Content::new("assistant").with_text(&text));
    events.push(event);

    events
}

/// Builder for GraphAgent
pub struct GraphAgentBuilder {
    name: String,
    description: String,
    schema: StateSchema,
    nodes: Vec<Arc<dyn Node>>,
    edges: Vec<Edge>,
    checkpointer: Option<Arc<dyn Checkpointer>>,
    interrupt_before: Vec<String>,
    interrupt_after: Vec<String>,
    recursion_limit: usize,
    input_mapper: Option<InputMapper>,
    output_mapper: Option<OutputMapper>,
    before_callback: Option<BeforeAgentCallback>,
    after_callback: Option<AfterAgentCallback>,
}

impl GraphAgentBuilder {
    /// Create a new builder
    pub fn new(name: &str) -> Self {
        Self {
            name: name.to_string(),
            description: String::new(),
            schema: StateSchema::simple(&["input", "output", "messages"]),
            nodes: vec![],
            edges: vec![],
            checkpointer: None,
            interrupt_before: vec![],
            interrupt_after: vec![],
            recursion_limit: 50,
            input_mapper: None,
            output_mapper: None,
            before_callback: None,
            after_callback: None,
        }
    }

    /// Set description
    pub fn description(mut self, desc: &str) -> Self {
        self.description = desc.to_string();
        self
    }

    /// Set state schema
    pub fn state_schema(mut self, schema: StateSchema) -> Self {
        self.schema = schema;
        self
    }

    /// Add channels to state schema
    pub fn channels(mut self, channels: &[&str]) -> Self {
        self.schema = StateSchema::simple(channels);
        self
    }

    /// Add a node
    pub fn node<N: Node + 'static>(mut self, node: N) -> Self {
        self.nodes.push(Arc::new(node));
        self
    }

    /// Add a function as a node
    pub fn node_fn<F, Fut>(mut self, name: &str, func: F) -> Self
    where
        F: Fn(NodeContext) -> Fut + Send + Sync + 'static,
        Fut: Future<Output = Result<NodeOutput>> + Send + 'static,
    {
        self.nodes.push(Arc::new(FunctionNode::new(name, func)));
        self
    }

    /// Add a direct edge
    pub fn edge(mut self, source: &str, target: &str) -> Self {
        let target =
            if target == END { EdgeTarget::End } else { EdgeTarget::Node(target.to_string()) };

        if source == START {
            let entry_idx = self.edges.iter().position(|e| matches!(e, Edge::Entry { .. }));
            match entry_idx {
                Some(idx) => {
                    if let Edge::Entry { targets } = &mut self.edges[idx] {
                        if let EdgeTarget::Node(node) = &target {
                            if !targets.contains(node) {
                                targets.push(node.clone());
                            }
                        }
                    }
                }
                None => {
                    if let EdgeTarget::Node(node) = target {
                        self.edges.push(Edge::Entry { targets: vec![node] });
                    }
                }
            }
        } else {
            self.edges.push(Edge::Direct { source: source.to_string(), target });
        }

        self
    }

    /// Add a conditional edge
    pub fn conditional_edge<F, I>(mut self, source: &str, router: F, targets: I) -> Self
    where
        F: Fn(&State) -> String + Send + Sync + 'static,
        I: IntoIterator<Item = (&'static str, &'static str)>,
    {
        let targets_map: HashMap<String, EdgeTarget> = targets
            .into_iter()
            .map(|(k, v)| {
                let target =
                    if v == END { EdgeTarget::End } else { EdgeTarget::Node(v.to_string()) };
                (k.to_string(), target)
            })
            .collect();

        self.edges.push(Edge::Conditional {
            source: source.to_string(),
            router: Arc::new(router),
            targets: targets_map,
        });

        self
    }

    /// Set checkpointer
    pub fn checkpointer<C: Checkpointer + 'static>(mut self, checkpointer: C) -> Self {
        self.checkpointer = Some(Arc::new(checkpointer));
        self
    }

    /// Set checkpointer with Arc
    pub fn checkpointer_arc(mut self, checkpointer: Arc<dyn Checkpointer>) -> Self {
        self.checkpointer = Some(checkpointer);
        self
    }

    /// Set nodes to interrupt before
    pub fn interrupt_before(mut self, nodes: &[&str]) -> Self {
        self.interrupt_before = nodes.iter().map(|s| s.to_string()).collect();
        self
    }

    /// Set nodes to interrupt after
    pub fn interrupt_after(mut self, nodes: &[&str]) -> Self {
        self.interrupt_after = nodes.iter().map(|s| s.to_string()).collect();
        self
    }

    /// Set recursion limit
    pub fn recursion_limit(mut self, limit: usize) -> Self {
        self.recursion_limit = limit;
        self
    }

    /// Set custom input mapper
    pub fn input_mapper<F>(mut self, mapper: F) -> Self
    where
        F: Fn(&dyn InvocationContext) -> State + Send + Sync + 'static,
    {
        self.input_mapper = Some(Arc::new(mapper));
        self
    }

    /// Set custom output mapper
    pub fn output_mapper<F>(mut self, mapper: F) -> Self
    where
        F: Fn(&State) -> Vec<Event> + Send + Sync + 'static,
    {
        self.output_mapper = Some(Arc::new(mapper));
        self
    }

    /// Set before agent callback
    pub fn before_agent_callback<F, Fut>(mut self, callback: F) -> Self
    where
        F: Fn(Arc<dyn InvocationContext>) -> Fut + Send + Sync + 'static,
        Fut: Future<Output = adk_core::Result<()>> + Send + 'static,
    {
        self.before_callback = Some(Arc::new(move |ctx| Box::pin(callback(ctx))));
        self
    }

    /// Set after agent callback
    ///
    /// Note: The callback receives a cloned Event to avoid lifetime issues.
    pub fn after_agent_callback<F, Fut>(mut self, callback: F) -> Self
    where
        F: Fn(Arc<dyn InvocationContext>, Event) -> Fut + Send + Sync + 'static,
        Fut: Future<Output = adk_core::Result<()>> + Send + 'static,
    {
        self.after_callback = Some(Arc::new(move |ctx, event| {
            let event_clone = event.clone();
            Box::pin(callback(ctx, event_clone))
        }));
        self
    }

    /// Add an action node to the graph.
    ///
    /// Wraps the `ActionNodeConfig` in an `ActionNodeExecutor` and registers it
    /// as a node. If the config is a `SwitchNodeConfig`, conditional edges are
    /// also auto-registered from the switch conditions.
    #[cfg(feature = "action")]
    pub fn action_node(mut self, config: adk_action::ActionNodeConfig) -> Self {
        use crate::action::ActionNodeExecutor;

        // If this is a Switch node, register conditional edges
        if let adk_action::ActionNodeConfig::Switch(ref switch_config) = config {
            let conditions = switch_config.conditions.clone();
            let eval_mode = switch_config.evaluation_mode.clone();
            let default_branch = switch_config.default_branch.clone();
            let source = config.standard().id.clone();

            let mut targets_map: HashMap<String, EdgeTarget> = HashMap::new();
            for condition in &conditions {
                targets_map.insert(
                    condition.output_port.clone(),
                    EdgeTarget::Node(condition.output_port.clone()),
                );
            }
            if let Some(ref default) = default_branch {
                let target = if default == END {
                    EdgeTarget::End
                } else {
                    EdgeTarget::Node(default.clone())
                };
                targets_map.insert(default.clone(), target);
            }
            targets_map.insert(END.to_string(), EdgeTarget::End);

            let router = Arc::new(move |state: &State| -> String {
                match crate::action::switch::evaluate_switch_conditions(
                    &conditions,
                    state,
                    &eval_mode,
                    default_branch.as_deref(),
                ) {
                    Ok(ports) => ports.into_iter().next().unwrap_or_else(|| END.to_string()),
                    Err(_) => END.to_string(),
                }
            });

            self.edges.push(Edge::Conditional { source, router, targets: targets_map });
        }

        let executor = ActionNodeExecutor::new(config);
        self.nodes.push(Arc::new(executor));
        self
    }

    /// Build the GraphAgent
    pub fn build(self) -> Result<GraphAgent> {
        // Build the graph
        let mut graph = StateGraph::new(self.schema);

        // Add nodes
        for node in self.nodes {
            graph.nodes.insert(node.name().to_string(), node);
        }

        // Add edges
        graph.edges = self.edges;

        // Compile
        let mut compiled = graph.compile()?;

        // Configure
        if let Some(cp) = self.checkpointer {
            compiled.checkpointer = Some(cp);
        }
        compiled.interrupt_before = self.interrupt_before.into_iter().collect();
        compiled.interrupt_after = self.interrupt_after.into_iter().collect();
        compiled.recursion_limit = self.recursion_limit;

        Ok(GraphAgent {
            name: self.name,
            description: self.description,
            graph: Arc::new(compiled),
            input_mapper: self.input_mapper.unwrap_or(Arc::new(default_input_mapper)),
            output_mapper: self.output_mapper.unwrap_or(Arc::new(default_output_mapper)),
            before_callback: self.before_callback,
            after_callback: self.after_callback,
        })
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;

    #[tokio::test]
    async fn test_graph_agent_builder() {
        let agent = GraphAgent::builder("test")
            .description("Test agent")
            .channels(&["value"])
            .node_fn("set", |_ctx| async { Ok(NodeOutput::new().with_update("value", json!(42))) })
            .edge(START, "set")
            .edge("set", END)
            .build()
            .unwrap();

        assert_eq!(agent.name(), "test");
        assert_eq!(agent.description(), "Test agent");

        // Test direct invocation
        let result = agent.invoke(State::new(), ExecutionConfig::new("test")).await.unwrap();

        assert_eq!(result.get("value"), Some(&json!(42)));
    }
}