Skip to main content

AgentSession

Struct AgentSession 

Source
pub struct AgentSession<P: EventProducer<StateT>, StateT: AgentState = JsonValue> { /* private fields */ }
Expand description

Manages an agent session with run lifecycle events.

This struct provides high-level management of agent runs, including starting, finishing, and error handling.

§Example

let mut session = AgentSession::new(sender);

// Start a run
let run_id = session.start_run().await?;

// Do work...

// Finish the run
session.finish_run(Some(json!({"result": "success"}))).await?;

Implementations§

Source§

impl<P: EventProducer<StateT>, StateT: AgentState> AgentSession<P, StateT>

Source

pub fn new(producer: P) -> Self

Create a new session with the given producer.

Generates a random thread ID for the session.

Source

pub fn with_thread_id(producer: P, thread_id: ThreadId) -> Self

Create a new session with a specific thread ID.

Source

pub async fn start_run(&mut self) -> Result<RunId, ServerError>

Start a new run.

Emits a RunStarted event and stores the run ID. Returns an error if a run is already in progress.

Source

pub async fn finish_run( &mut self, result: Option<JsonValue>, ) -> Result<(), ServerError>

Finish the current run.

Emits a RunFinished event with an optional result. Does nothing if no run is in progress.

Source

pub async fn run_error( &mut self, message: impl Into<String>, ) -> Result<(), ServerError>

Signal a run error.

Emits a RunError event and clears the current run.

Source

pub async fn run_error_with_code( &mut self, message: impl Into<String>, code: impl Into<String>, ) -> Result<(), ServerError>

Signal a run error with an error code.

Source

pub fn producer(&self) -> &P

Get a reference to the underlying producer.

Source

pub fn thread_id(&self) -> &ThreadId

Get the thread ID for this session.

Source

pub fn run_id(&self) -> Option<&RunId>

Get the current run ID, if any.

Source

pub fn is_running(&self) -> bool

Check if a run is currently in progress.

Source

pub fn is_connected(&self) -> bool

Check if the connection is still open.

Source

pub async fn start_thinking( &self, title: Option<impl Into<String>>, ) -> Result<ThinkingStep<'_, P, StateT>, ServerError>

Start a thinking step.

Convenience method that creates a ThinkingStep using this session’s producer.

§Example
let step = session.start_thinking(Some("Planning response")).await?;
// ... emit thinking content ...
step.end().await?;
Source

pub async fn interrupt( &mut self, reason: Option<impl Into<String>>, payload: Option<JsonValue>, ) -> Result<(), ServerError>

Interrupt the current run for human-in-the-loop interaction.

Finishes the run with an interrupt outcome, signaling that human input is required before the agent can continue. The client should display appropriate UI based on the interrupt info and resume with user input.

§Example
session.start_run().await?;

// Request human approval
session.interrupt(
    Some("human_approval"),
    Some(serde_json::json!({"action": "send_email", "to": "user@example.com"}))
).await?;
Source

pub async fn interrupt_with_id( &mut self, id: impl Into<String>, reason: Option<impl Into<String>>, payload: Option<JsonValue>, ) -> Result<(), ServerError>

Interrupt with a specific interrupt ID for tracking.

The interrupt ID can be used by the client to correlate the resume request with the original interrupt.

§Example
session.start_run().await?;

// Request approval with tracking ID
session.interrupt_with_id(
    "approval-001",
    Some("database_modification"),
    Some(serde_json::json!({"query": "DELETE FROM users WHERE inactive"}))
).await?;

Auto Trait Implementations§

§

impl<P, StateT> Freeze for AgentSession<P, StateT>
where P: Freeze,

§

impl<P, StateT> RefUnwindSafe for AgentSession<P, StateT>
where P: RefUnwindSafe, StateT: RefUnwindSafe,

§

impl<P, StateT> Send for AgentSession<P, StateT>

§

impl<P, StateT> Sync for AgentSession<P, StateT>

§

impl<P, StateT> Unpin for AgentSession<P, StateT>
where P: Unpin, StateT: Unpin,

§

impl<P, StateT> UnsafeUnpin for AgentSession<P, StateT>
where P: UnsafeUnpin,

§

impl<P, StateT> UnwindSafe for AgentSession<P, StateT>
where P: UnwindSafe, StateT: UnwindSafe,

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> Same for T

Source§

type Output = T

Should always be Self
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<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

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
Source§

impl<A, B, T> HttpServerConnExec<A, B> for T
where B: Body,