Skip to main content

AgentInvoker

Trait AgentInvoker 

Source
pub trait AgentInvoker: Send + Sync {
    // Required method
    fn invoke<'life0, 'life1, 'life2, 'async_trait>(
        &'life0 self,
        user_id: &'life1 str,
        session_id: &'life2 str,
        content: Content,
    ) -> Pin<Box<dyn Future<Output = Result<Pin<Box<dyn Stream<Item = Result<Event, AdkError>> + Send>>, AdkError>> + Send + 'async_trait>>
       where 'life0: 'async_trait,
             'life1: 'async_trait,
             'life2: 'async_trait,
             Self: 'async_trait;

    // Provided method
    fn agent(&self) -> Option<Arc<dyn Agent>> { ... }
}
Expand description

Core traits and types.

Always available regardless of feature flags. Includes:

  • Agent - The fundamental trait for all agents
  • Tool / Toolset - For extending agents with capabilities
  • Session / State - For managing conversation context
  • Event - For streaming agent responses
  • AdkError / Result - Unified error handling Starts an agent turn for a session, creating the session when it does not exist.

Callers that hand work to an agent from outside a conversation — a background trigger, a queue consumer, a scheduler — need one operation: “run this content through the agent and give me the events.” They should not have to know which session service holds the session, or that a session must be registered before a turn can start.

adk-runner implements this for Runner, so a caller can accept Arc<dyn AgentInvoker> and stay independent of the runner’s construction. Implementations are responsible for creating a missing session rather than failing, because an external event has no opportunity to register one first. Implementations that permit concurrent calls should also serialize turns targeting the same session until the returned event stream completes or is dropped.

§Example

use std::sync::Arc;
use adk_core::{AgentInvoker, Content};

async fn on_event(invoker: Arc<dyn AgentInvoker>) -> adk_core::Result<()> {
    let mut events = invoker
        .invoke("system", "nightly-sweep", Content::new("user").with_text("run the sweep"))
        .await?;
    while let Some(event) = futures::StreamExt::next(&mut events).await {
        let _ = event?;
    }
    Ok(())
}

Required Methods§

Source

fn invoke<'life0, 'life1, 'life2, 'async_trait>( &'life0 self, user_id: &'life1 str, session_id: &'life2 str, content: Content, ) -> Pin<Box<dyn Future<Output = Result<Pin<Box<dyn Stream<Item = Result<Event, AdkError>> + Send>>, AdkError>> + Send + 'async_trait>>
where 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait, Self: 'async_trait,

Starts a turn for (user_id, session_id) with content and returns the event stream.

§Errors

Returns an error if either identifier fails validation, if the session cannot be created or retrieved, or if invocation setup fails. Failures during agent execution are yielded by the returned stream rather than returned here.

Provided Methods§

Source

fn agent(&self) -> Option<Arc<dyn Agent>>

Returns the agent this invoker executes when it can expose one.

Wrappers that do not own an in-process agent may keep the default. Consumers use this to align diagnostics and lifecycle metadata with the executable root without coupling to a concrete runner type.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§