Skip to main content

adk_agent/workflow/
sequential_agent.rs

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