Skip to main content

SessionManager

Struct SessionManager 

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

AI-optimized session manager for creating and managing multiple terminal sessions.

The SessionManager provides a centralized way to create, track, and manage AI-enhanced terminal sessions. It includes automatic cleanup, session restoration, and efficient resource management.

§Features

  • Session Pooling: Efficient management of multiple concurrent sessions
  • Automatic Cleanup: Garbage collection of terminated sessions
  • Session Restoration: Restore sessions from persistent storage
  • Resource Management: Automatic cleanup and memory management

§Examples

§Basic Session Management

use ai_session::{SessionManager, SessionConfig};

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    let manager = SessionManager::new();
     
    // Create multiple sessions
    let session1 = manager.create_session().await?;
    let session2 = manager.create_session().await?;
     
    session1.start().await?;
    session2.start().await?;
     
    // List all active sessions
    let session_ids = manager.list_sessions();
    println!("Active sessions: {}", session_ids.len());
     
    // Clean up
    manager.remove_session(&session1.id).await?;
    manager.remove_session(&session2.id).await?;
     
    Ok(())
}

§Custom Configuration

use ai_session::{SessionManager, SessionConfig, ContextConfig};

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    let manager = SessionManager::new();
     
    // Configure for AI development agent
    let mut config = SessionConfig::default();
    config.enable_ai_features = true;
    config.agent_role = Some("backend-developer".to_string());
    config.working_directory = "/project/backend".into();
    config.context_config = ContextConfig {
        max_tokens: 8192,
        compression_threshold: 0.8,
    };
     
    let session = manager.create_session_with_config(config).await?;
    session.start().await?;
     
    // Session is optimized for AI backend development
    Ok(())
}

§Session Persistence

use ai_session::{SessionManager, SessionConfig, SessionId};
use chrono::Utc;

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    let manager = SessionManager::new();
     
    // Create session
    let config = SessionConfig::default();
    let session = manager.create_session_with_config(config.clone()).await?;
    let session_id = session.id.clone();
    let created_at = session.created_at;
     
    // Later, restore the session
    let restored = manager.restore_session(session_id, config, created_at).await?;
    restored.start().await?;
     
    Ok(())
}

Implementations§

Source§

impl SessionManager

Source

pub fn new() -> Self

Create a new session manager

Source

pub async fn create_session(&self) -> Result<Arc<AISession>>

Create a new session with default config

Source

pub async fn create_session_with_config( &self, config: SessionConfig, ) -> Result<Arc<AISession>>

Create a new session with custom config

Source

pub async fn restore_session( &self, id: SessionId, config: SessionConfig, created_at: DateTime<Utc>, ) -> Result<Arc<AISession>>

Restore a session with a specific ID (for persistence)

Source

pub fn get_session(&self, id: &SessionId) -> Option<Arc<AISession>>

Get a session by ID

Source

pub fn list_sessions(&self) -> Vec<SessionId>

List all active sessions

Source

pub fn list_session_refs(&self) -> Vec<Arc<AISession>>

List all active session references

Source

pub async fn remove_session(&self, id: &SessionId) -> Result<()>

Remove a session

Source

pub async fn cleanup_terminated(&self) -> Result<usize>

Clean up terminated sessions

Source

pub fn find_session_by_name(&self, name: &str) -> Option<Arc<AISession>>

Find a session by its name (exact match)

§Arguments
  • name - The exact name to search for
§Returns

The session if found, None otherwise

Source

pub fn list_sessions_by_prefix(&self, prefix: &str) -> Vec<Arc<AISession>>

Find sessions with names matching a prefix

§Arguments
  • prefix - The prefix to match against session names
§Returns

List of sessions whose names start with the given prefix

Source

pub fn list_sessions_detailed(&self) -> Vec<SessionInfo>

List all sessions with detailed information (snapshot, no locking required)

Returns a vector of SessionInfo containing summary information about all active sessions. This is more efficient than listing sessions and then querying each one individually.

Trait Implementations§

Source§

impl Default for SessionManager

Source§

fn default() -> Self

Returns the “default value” for a type. 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> Downcast for T
where T: Any,

Source§

fn into_any(self: Box<T>) -> Box<dyn Any>

Convert Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.
Source§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

Convert Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait.
Source§

fn as_any(&self) -> &(dyn Any + 'static)

Convert &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s.
Source§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Convert &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
Source§

impl<T> DowncastSync for T
where T: Any + Send + Sync,

Source§

fn into_any_arc(self: Arc<T>) -> Arc<dyn Any + Sync + Send>

Convert Arc<Trait> (where Trait: Downcast) to Arc<Any>. Arc<Any> can then be further downcast into Arc<ConcreteType> where ConcreteType implements Trait.
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