Skip to main content

SessionLoop

Struct SessionLoop 

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

Supervised session loop — one per active session.

Runs as a background tokio::spawned task. Receives user events via an mpsc channel, processes each turn through the real Runner, and broadcasts session events via a tokio::broadcast channel.

§Example

use std::sync::Arc;
use std::time::Duration;
use tokio::sync::{broadcast, mpsc, Mutex, Notify};
use tokio_util::sync::CancellationToken;
use adk_managed::session_loop::SessionLoop;
use adk_managed::parking::ToolParkingLot;

let (event_tx, event_rx) = mpsc::channel(64);
let (broadcast_tx, _) = broadcast::channel(256);
let cancel = CancellationToken::new();
let parking = Arc::new(ToolParkingLot::new(Duration::from_secs(300)));

let loop_handle = SessionLoop::new(
    "session_001".to_string(),
    event_rx,
    broadcast_tx,
    parking,
    cancel.clone(),
    agent,
    session_service,
);

let handle = tokio::spawn(loop_handle.run());
// Send events via event_tx...

Implementations§

Source§

impl SessionLoop

Source

pub fn new( session_id: String, event_rx: Receiver<UserEvent>, event_tx: Sender<SessionEvent>, parking: Arc<ToolParkingLot>, cancel_token: CancellationToken, agent: Arc<dyn Agent>, session_service: Arc<dyn SessionService>, ) -> Self

Create a new session loop.

§Arguments
  • session_id - The session this loop operates on.
  • event_rx - Receiver for incoming user events.
  • event_tx - Broadcast sender for outgoing session events.
  • parking - Shared parking lot for custom tool calls.
  • cancel_token - Token to signal interrupt/shutdown.
  • agent - The built agent to drive through the Runner.
  • session_service - Session persistence for the Runner.
Source

pub fn with_pause_controls( session_id: String, event_rx: Receiver<UserEvent>, event_tx: Sender<SessionEvent>, parking: Arc<ToolParkingLot>, cancel_token: CancellationToken, pause_flag: Arc<Mutex<bool>>, pause_notify: Arc<Notify>, checkpoint: Arc<RwLock<CheckpointManager>>, agent: Arc<dyn Agent>, session_service: Arc<dyn SessionService>, ) -> Self

Create a session loop with custom pause controls (for external pause/resume).

See the memory-enabled variant for full documentation.

Source

pub fn with_owner( self, app_name: impl Into<String>, user_id: impl Into<String>, ) -> Self

Makes this loop’s Runner calls under owner.

Without this the loop used the constants managed / managed_user, so every managed session shared one logical namespace and no session could be attributed to a caller.

§Example
let session_loop = SessionLoop::with_pause_controls(/* ... */)
    .with_owner(owner.app_name(), owner.user_id());
Source

pub fn with_shared_status(self, status: Arc<RwLock<SessionStatus>>) -> Self

Reports status into the handle the caller already holds.

The runtime’s ActiveSession owns the status a caller observes through ManagedAgentRuntime::status. Installing it here is what makes normal queued → running → idle transitions visible; without it the loop wrote to its own field and the public handle stayed Queued through an entire session.

§Example
let session_loop = SessionLoop::with_pause_controls(/* ... */)
    .with_shared_status(Arc::clone(&active.status));
Source

pub fn pause_flag(&self) -> Arc<Mutex<bool>>

Get a clone of the pause flag for external control.

Source

pub fn pause_notify(&self) -> Arc<Notify>

Get a clone of the pause notify for external control.

Source

pub async fn run(self) -> Result<(), RuntimeError>

Run the session loop (consumes self).

This is the main loop body, designed to be tokio::spawned. It runs until the input channel is closed or the cancellation token is triggered.

§Returns

Returns Ok(()) on graceful shutdown, or Err(RuntimeError) if an unrecoverable error occurs.

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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

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> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

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