Skip to main content

Agent

Struct Agent 

Source
pub struct Agent { /* private fields */ }
Expand description

High-level agent handle: connect, chat, shutdown.

Owns the connection, runners, and background dispatcher. Drop aborts background tasks; call Agent::shutdown for a clean teardown.

§Examples

use localharness::{Agent, GeminiAgentConfig};

let agent = Agent::start_gemini(
    GeminiAgentConfig::new("key")
        .with_system_instructions("Be concise."),
).await?;
let resp = agent.chat("Hello").await?;
println!("{}", resp.text().await?);
agent.shutdown().await?;

Implementations§

Source§

impl Agent

Source

pub async fn start_gemini(config: GeminiAgentConfig) -> Result<Self>

Start an Agent backed by the Rust-native Gemini runtime.

Source

pub fn cumulative_usage(&self) -> UsageMetadata

Token usage accumulated across every turn in this agent’s conversation. Surfaced in the browser app’s Usage tab.

Source

pub fn cancel_turn(&self)

Cooperatively cancel the in-flight turn (e.g. a UI stop button). The backend stops at its next safe boundary — between streamed chunks or before the next model call / tool dispatch — so no more tokens are spent and no further tools run. No-op when idle.

Source

pub fn history_bytes(&self) -> Result<Option<Vec<u8>>>

Opaque snapshot of the current conversation history (Gemini or, with the anthropic feature, the Anthropic backend). Returns None for backends without a typed session handle. Round-trips through the matching with_history_bytes for session resume.

Source

pub async fn compact(&self) -> bool

Manually trigger context compaction. Summarises older history entries and replaces them with a single synthetic turn, freeing context-window budget. Returns true if compaction changed the history, false if it was too short or not applicable. Returns false for backends without a typed session handle.

Source

pub fn clear_history(&self)

Wipe the conversation history, returning the agent to a fresh, empty context — the in-tab clear_context tool / a “clear the chat” request. Synchronous (clearing a Vec needs no network). No-op for backends without a typed session handle.

Source

pub fn transcript(&self) -> Vec<TranscriptEntry>

Human-readable transcript of the current session, including tool-call activity — see TranscriptEntry for the shape. Returns an empty vec for backends without a typed session handle.

Source

pub fn conversation(&self) -> &Conversation

The underlying conversation session.

Source

pub fn conversation_id(&self) -> String

The backend-assigned conversation identifier.

Source

pub fn hooks(&self) -> &HookRunner

The hook runner; use to register additional hooks after start.

Source

pub fn tools(&self) -> &ToolRunner

The tool runner; use to register additional tools after start.

Source

pub async fn chat(&self, content: impl Into<Content>) -> Result<ChatResponse>

Send a prompt and return a streaming ChatResponse.

§Examples
let response = agent.chat("What is Rust?").await?;
println!("{}", response.text().await?);
Source

pub async fn shutdown(self) -> Result<()>

Cleanly shut down the agent, aborting triggers and dispatchers.

Trait Implementations§

Source§

impl Drop for Agent

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more

Auto Trait Implementations§

§

impl !Freeze for Agent

§

impl !RefUnwindSafe for Agent

§

impl !UnwindSafe for Agent

§

impl Send for Agent

§

impl Sync for Agent

§

impl Unpin for Agent

§

impl UnsafeUnpin for Agent

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more