adk_agent/workflow/
sequential_agent.rs

1use crate::workflow::LoopAgent;
2use adk_core::{
3    AfterAgentCallback, Agent, BeforeAgentCallback, EventStream, InvocationContext, Result,
4};
5use async_trait::async_trait;
6use std::sync::Arc;
7
8/// Sequential agent executes sub-agents once in order
9pub struct SequentialAgent {
10    loop_agent: LoopAgent,
11}
12
13impl SequentialAgent {
14    pub fn new(name: impl Into<String>, sub_agents: Vec<Arc<dyn Agent>>) -> Self {
15        Self { loop_agent: LoopAgent::new(name, sub_agents).with_max_iterations(1) }
16    }
17
18    pub fn with_description(mut self, desc: impl Into<String>) -> Self {
19        self.loop_agent = self.loop_agent.with_description(desc);
20        self
21    }
22
23    pub fn before_callback(mut self, callback: BeforeAgentCallback) -> Self {
24        self.loop_agent = self.loop_agent.before_callback(callback);
25        self
26    }
27
28    pub fn after_callback(mut self, callback: AfterAgentCallback) -> Self {
29        self.loop_agent = self.loop_agent.after_callback(callback);
30        self
31    }
32}
33
34#[async_trait]
35impl Agent for SequentialAgent {
36    fn name(&self) -> &str {
37        self.loop_agent.name()
38    }
39
40    fn description(&self) -> &str {
41        self.loop_agent.description()
42    }
43
44    fn sub_agents(&self) -> &[Arc<dyn Agent>] {
45        self.loop_agent.sub_agents()
46    }
47
48    async fn run(&self, ctx: Arc<dyn InvocationContext>) -> Result<EventStream> {
49        self.loop_agent.run(ctx).await
50    }
51}