Skip to main content

Conversation

Struct Conversation 

Source
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

Source

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.

Source

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.

Source

pub fn budget(&self) -> Option<&BudgetTracker>

The attached BudgetTracker, if any.

Source

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.

Source

pub fn history(&self) -> &[TurnResult]

Per-turn result history, in arrival order.

Source

pub fn last(&self) -> Option<&TurnResult>

Result of the most recent turn, if any.

Source

pub fn total_cost_usd(&self) -> f64

Cumulative cost in USD across every recorded turn.

Source

pub fn total_turns(&self) -> u32

Number of turns recorded through Conversation::send.

Source

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.

Source

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.

Source

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

Close the underlying DuplexSession and wait for its task to exit. Consumes the Conversation.

Trait Implementations§

Source§

impl Debug for Conversation

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

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, 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