pub trait TextAgent: Send + Sync {
// Required methods
fn name(&self) -> &str;
fn run<'life0, 'life1, 'async_trait>(
&'life0 self,
state: &'life1 State,
) -> Pin<Box<dyn Future<Output = Result<String, AgentError>> + Send + 'async_trait>>
where 'life0: 'async_trait,
'life1: 'async_trait,
Self: 'async_trait;
// Provided methods
fn run_with<'life0, 'life1, 'async_trait>(
&'life0 self,
request: RunRequest,
state: &'life1 State,
) -> Pin<Box<dyn Future<Output = Result<RunResult, AgentError>> + Send + 'async_trait>>
where 'life0: 'async_trait,
'life1: 'async_trait,
Self: 'async_trait { ... }
fn run_stream<'a>(
&'a self,
request: RunRequest,
state: State,
) -> Pin<Box<dyn Stream<Item = Result<RunEvent, AgentError>> + Send + 'a>> { ... }
fn stream(
&self,
prompt: impl Into<String>,
) -> Pin<Box<dyn Stream<Item = Result<RunEvent, AgentError>> + Send + '_>>
where Self: Sized { ... }
fn ask<'life0, 'async_trait>(
&'life0 self,
prompt: impl Into<String> + Send + 'async_trait,
) -> Pin<Box<dyn Future<Output = Result<String, AgentError>> + Send + 'async_trait>>
where 'life0: 'async_trait,
Self: Sized + 'async_trait { ... }
fn ask_as<'life0, 'async_trait, T>(
&'life0 self,
prompt: impl Into<String> + Send + 'async_trait,
) -> Pin<Box<dyn Future<Output = Result<T, AgentError>> + Send + 'async_trait>>
where 'life0: 'async_trait,
Self: Sized + 'async_trait,
T: DeserializeOwned + JsonSchema + Send + 'async_trait { ... }
fn chat(&self) -> Chat<&Self>
where Self: Sized { ... }
}Expand description
A text-based agent that runs via BaseLlm::generate() (request/response).
Unlike Agent (which requires a Live WebSocket session), TextAgent can be
dispatched from anywhere — event hooks, background tasks, CLI tools.
Ask a question, get a typed answer, or hold a conversation:
use gemini_adk_rs::llm::{LlmResponse, MockLlm};
use gemini_adk_rs::text::{LlmTextAgent, TextAgent};
#[derive(serde::Deserialize, schemars::JsonSchema)]
struct City {
name: String,
country: String,
}
let llm = MockLlm::script([
LlmResponse::from_text("Paris."),
LlmResponse::from_text(r#"{"name":"Paris","country":"France"}"#),
]);
let agent = LlmTextAgent::new("geo", llm);
assert_eq!(agent.ask("Capital of France?").await.unwrap(), "Paris.");
let city: City = agent.ask_as("Describe the capital of France.").await.unwrap();
assert_eq!(city.country, "France");run_with is the primitive the others are built on;
run is the state-in, state-out form combinators use, reading
the prompt from the "input" state key.
Required Methods§
Provided Methods§
Sourcefn run_with<'life0, 'life1, 'async_trait>(
&'life0 self,
request: RunRequest,
state: &'life1 State,
) -> Pin<Box<dyn Future<Output = Result<RunResult, AgentError>> + Send + 'async_trait>>where
'life0: 'async_trait,
'life1: 'async_trait,
Self: 'async_trait,
fn run_with<'life0, 'life1, 'async_trait>(
&'life0 self,
request: RunRequest,
state: &'life1 State,
) -> Pin<Box<dyn Future<Output = Result<RunResult, AgentError>> + Send + 'async_trait>>where
'life0: 'async_trait,
'life1: 'async_trait,
Self: 'async_trait,
Run one request and report everything it produced: the reply, the turns to append to a conversation, token usage and tool calls.
The default writes the request’s text to the "input" state key and
calls run, so every agent supports it; history and a
response schema are honoured by agents that call a model
(LlmTextAgent).
Sourcefn run_stream<'a>(
&'a self,
request: RunRequest,
state: State,
) -> Pin<Box<dyn Stream<Item = Result<RunEvent, AgentError>> + Send + 'a>>
fn run_stream<'a>( &'a self, request: RunRequest, state: State, ) -> Pin<Box<dyn Stream<Item = Result<RunEvent, AgentError>> + Send + 'a>>
Run one request as a stream of RunEvents: text as the model writes
it, each tool call and result, then RunEvent::Finished.
The default emits the reply of run_with as a single
delta. LlmTextAgent streams from the model; when it has middleware
(which may rewrite a reply, e.g. to redact it), each model turn is
emitted only after the middleware has seen it.
Sourcefn stream(
&self,
prompt: impl Into<String>,
) -> Pin<Box<dyn Stream<Item = Result<RunEvent, AgentError>> + Send + '_>>where
Self: Sized,
fn stream(
&self,
prompt: impl Into<String>,
) -> Pin<Box<dyn Stream<Item = Result<RunEvent, AgentError>> + Send + '_>>where
Self: Sized,
Stream the reply to one question, with no history and fresh state.
use futures_util::StreamExt;
use gemini_adk_rs::llm::MockLlm;
use gemini_adk_rs::text::{LlmTextAgent, RunEvent, TextAgent};
let agent = LlmTextAgent::new("storyteller", MockLlm::text("Once upon a time"));
let mut events = agent.stream("Tell me a story.");
let mut story = String::new();
while let Some(event) = events.next().await {
if let RunEvent::TextDelta(text) = event.unwrap() {
story.push_str(&text);
}
}
assert_eq!(story, "Once upon a time");Sourcefn ask<'life0, 'async_trait>(
&'life0 self,
prompt: impl Into<String> + Send + 'async_trait,
) -> Pin<Box<dyn Future<Output = Result<String, AgentError>> + Send + 'async_trait>>where
'life0: 'async_trait,
Self: Sized + 'async_trait,
fn ask<'life0, 'async_trait>(
&'life0 self,
prompt: impl Into<String> + Send + 'async_trait,
) -> Pin<Box<dyn Future<Output = Result<String, AgentError>> + Send + 'async_trait>>where
'life0: 'async_trait,
Self: Sized + 'async_trait,
Ask one question, with no history and fresh state, and get the reply.
Sourcefn ask_as<'life0, 'async_trait, T>(
&'life0 self,
prompt: impl Into<String> + Send + 'async_trait,
) -> Pin<Box<dyn Future<Output = Result<T, AgentError>> + Send + 'async_trait>>where
'life0: 'async_trait,
Self: Sized + 'async_trait,
T: DeserializeOwned + JsonSchema + Send + 'async_trait,
fn ask_as<'life0, 'async_trait, T>(
&'life0 self,
prompt: impl Into<String> + Send + 'async_trait,
) -> Pin<Box<dyn Future<Output = Result<T, AgentError>> + Send + 'async_trait>>where
'life0: 'async_trait,
Self: Sized + 'async_trait,
T: DeserializeOwned + JsonSchema + Send + 'async_trait,
Ask one question and get the reply as a T.
T’s JSON Schema is sent as the response schema. If the reply still
does not parse, the model is shown the error and asked once more;
a second failure is AgentError::InvalidOutput.