Skip to main content

adk_agent/workflow/
sequential_agent.rs

1use crate::workflow::LoopAgent;
2use adk_core::{
3    AfterAgentCallback, Agent, BeforeAgentCallback, EventStream, InvocationContext, Result,
4};
5use adk_skill::{SelectionPolicy, SkillIndex};
6use async_trait::async_trait;
7use std::sync::Arc;
8
9/// Sequential agent executes sub-agents once in order
10pub struct SequentialAgent {
11    loop_agent: LoopAgent,
12}
13
14impl SequentialAgent {
15    pub fn new(name: impl Into<String>, sub_agents: Vec<Arc<dyn Agent>>) -> Self {
16        Self { loop_agent: LoopAgent::new(name, sub_agents).with_max_iterations(1) }
17    }
18
19    pub fn with_description(mut self, desc: impl Into<String>) -> Self {
20        self.loop_agent = self.loop_agent.with_description(desc);
21        self
22    }
23
24    pub fn before_callback(mut self, callback: BeforeAgentCallback) -> Self {
25        self.loop_agent = self.loop_agent.before_callback(callback);
26        self
27    }
28
29    pub fn after_callback(mut self, callback: AfterAgentCallback) -> Self {
30        self.loop_agent = self.loop_agent.after_callback(callback);
31        self
32    }
33
34    pub fn with_skills(mut self, index: SkillIndex) -> Self {
35        self.loop_agent = self.loop_agent.with_skills(index);
36        self
37    }
38
39    pub fn with_auto_skills(self) -> Result<Self> {
40        self.with_skills_from_root(".")
41    }
42
43    pub fn with_skills_from_root(mut self, root: impl AsRef<std::path::Path>) -> Result<Self> {
44        self.loop_agent = self.loop_agent.with_skills_from_root(root)?;
45        Ok(self)
46    }
47
48    pub fn with_skill_policy(mut self, policy: SelectionPolicy) -> Self {
49        self.loop_agent = self.loop_agent.with_skill_policy(policy);
50        self
51    }
52
53    pub fn with_skill_budget(mut self, max_chars: usize) -> Self {
54        self.loop_agent = self.loop_agent.with_skill_budget(max_chars);
55        self
56    }
57}
58
59#[async_trait]
60impl Agent for SequentialAgent {
61    fn name(&self) -> &str {
62        self.loop_agent.name()
63    }
64
65    fn description(&self) -> &str {
66        self.loop_agent.description()
67    }
68
69    fn sub_agents(&self) -> &[Arc<dyn Agent>] {
70        self.loop_agent.sub_agents()
71    }
72
73    async fn run(&self, ctx: Arc<dyn InvocationContext>) -> Result<EventStream> {
74        self.loop_agent.run(ctx).await
75    }
76}