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
impl Agent
Sourcepub async fn start_gemini(config: GeminiAgentConfig) -> Result<Self>
pub async fn start_gemini(config: GeminiAgentConfig) -> Result<Self>
Start an Agent backed by the Rust-native Gemini runtime.
Sourcepub fn cumulative_usage(&self) -> UsageMetadata
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.
Sourcepub fn cancel_turn(&self)
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.
Sourcepub fn history_bytes(&self) -> Result<Option<Vec<u8>>>
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.
Sourcepub async fn compact(&self) -> bool
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.
Sourcepub fn clear_history(&self)
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.
Sourcepub fn transcript(&self) -> Vec<TranscriptEntry>
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.
Sourcepub fn conversation(&self) -> &Conversation
pub fn conversation(&self) -> &Conversation
The underlying conversation session.
Sourcepub fn conversation_id(&self) -> String
pub fn conversation_id(&self) -> String
The backend-assigned conversation identifier.
Sourcepub fn hooks(&self) -> &HookRunner
pub fn hooks(&self) -> &HookRunner
The hook runner; use to register additional hooks after start.
Sourcepub fn tools(&self) -> &ToolRunner
pub fn tools(&self) -> &ToolRunner
The tool runner; use to register additional tools after start.
Sourcepub async fn chat(&self, content: impl Into<Content>) -> Result<ChatResponse>
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?);