pub struct Conversation { /* private fields */ }Expand description
Host-side bookkeeping over a DuplexSession.
See the module docs for the full design.
Implementations§
Source§impl Conversation
impl Conversation
Sourcepub fn new(session: DuplexSession) -> Self
pub fn new(session: DuplexSession) -> Self
Wrap a DuplexSession in a fresh Conversation.
The conversation starts with an empty history and zeroed
counters; the underlying session is not touched until the
first Conversation::send.
Sourcepub fn with_budget(self, budget: BudgetTracker) -> Self
pub fn with_budget(self, budget: BudgetTracker) -> Self
Attach a BudgetTracker for cumulative-cost ceilings.
Every turn’s cost (from TurnResult::total_cost_usd) is
recorded on the tracker, and Conversation::send returns
Error::BudgetExceeded
before dispatching a turn if the ceiling has been hit. Clone a
tracker across several conversations to enforce a shared
ceiling.
Sourcepub fn budget(&self) -> Option<&BudgetTracker>
pub fn budget(&self) -> Option<&BudgetTracker>
The attached BudgetTracker, if any.
Sourcepub async fn send(&mut self, prompt: impl Into<String>) -> Result<&TurnResult>
pub async fn send(&mut self, prompt: impl Into<String>) -> Result<&TurnResult>
Send one user message and record the resulting TurnResult.
Pre-turn: if a BudgetTracker is attached and its ceiling is
hit, returns
Error::BudgetExceeded
without touching the underlying session.
On success the returned reference points at the just-recorded
last entry of Conversation::history. Errors from the
underlying DuplexSession::send
(e.g. Error::DuplexTurnInFlight,
Error::DuplexClosed)
propagate unchanged and do not update the history or cost
counters.
Sourcepub fn history(&self) -> &[TurnResult]
pub fn history(&self) -> &[TurnResult]
Per-turn result history, in arrival order.
Sourcepub fn last(&self) -> Option<&TurnResult>
pub fn last(&self) -> Option<&TurnResult>
Result of the most recent turn, if any.
Sourcepub fn total_cost_usd(&self) -> f64
pub fn total_cost_usd(&self) -> f64
Cumulative cost in USD across every recorded turn.
Sourcepub fn total_turns(&self) -> u32
pub fn total_turns(&self) -> u32
Number of turns recorded through Conversation::send.
Sourcepub fn session_id(&self) -> Option<&str>
pub fn session_id(&self) -> Option<&str>
Session id from the most recent turn’s result payload, if
any. Returns None until the first turn lands; later turns on
a single duplex child reuse the same id.
Sourcepub fn session(&self) -> &DuplexSession
pub fn session(&self) -> &DuplexSession
Borrow the underlying DuplexSession.
Use this for DuplexSession::subscribe,
DuplexSession::interrupt, and
DuplexSession::respond_to_permission. Those calls bypass
Conversation’s bookkeeping on purpose – an interrupt still
produces a TurnResult that the in-flight
Conversation::send records cleanly when the truncated turn
lands.
Sourcepub async fn close(self) -> Result<()>
pub async fn close(self) -> Result<()>
Close the underlying DuplexSession and wait for its task to
exit. Consumes the Conversation.