adk_rs/agents/base.rs
1//! [`BaseAgent`] trait — the common shape of every agent.
2
3use std::sync::Arc;
4
5use async_trait::async_trait;
6
7use crate::core::{EventStream, InvocationContext};
8use crate::error::Result;
9
10/// Common shape of every agent. Concrete agents in this crate (`LlmAgent`,
11/// `SequentialAgent`, etc.) implement this; user-defined agents can too.
12#[async_trait]
13pub trait BaseAgent: Send + Sync + std::fmt::Debug + 'static {
14 /// The agent's name. Must be a valid identifier and unique within an
15 /// agent tree; the runner addresses agents by name for transfer.
16 fn name(&self) -> &str;
17
18 /// Human-readable description (shown to the *model* — keep it short
19 /// and informative; the LLM uses it to decide whether to delegate).
20 fn description(&self) -> &str {
21 ""
22 }
23
24 /// Direct sub-agents (children in the agent tree).
25 fn sub_agents(&self) -> &[Arc<dyn BaseAgent>] {
26 &[]
27 }
28
29 /// Resolve a descendant by name (depth-first).
30 fn find_agent(&self, name: &str) -> Option<Arc<dyn BaseAgent>> {
31 for sub in self.sub_agents() {
32 if sub.name() == name {
33 return Some(sub.clone());
34 }
35 if let Some(found) = sub.find_agent(name) {
36 return Some(found);
37 }
38 }
39 None
40 }
41
42 /// Run the agent within the given invocation context. Emits a stream of
43 /// events. The stream terminates when the agent has produced its final
44 /// response (or errors out).
45 async fn run(self: Arc<Self>, ctx: Arc<InvocationContext>) -> Result<EventStream<'static>>;
46}