Skip to main content

SessionManager

Struct SessionManager 

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

Manager for session persistence and lifecycle.

The SessionManager provides file-based persistence for sessions, storing them in {workspace}/.thulp/sessions/ as JSON files.

§Example

use thulp_workspace::{Workspace, SessionManager, SessionType};

let workspace = Workspace::new("my-workspace", "My Workspace", PathBuf::from("."));
let manager = SessionManager::new(&workspace).await?;

let session = manager.create_session("My Session", SessionType::Conversation {
    purpose: "Testing".to_string(),
}).await?;

manager.add_entry(&session.id(), EntryType::UserMessage, json!({"text": "Hello"})).await?;

Implementations§

Source§

impl SessionManager

Source

pub async fn new(workspace: &Workspace) -> Result<Self>

Create a new session manager for the given workspace.

This will create the sessions directory if it doesn’t exist.

Source

pub async fn with_sessions_dir(sessions_dir: PathBuf) -> Result<Self>

Create a new session manager with a custom sessions directory.

Source

pub async fn create_session( &self, name: impl Into<String>, session_type: SessionType, ) -> Result<Session>

Create a new session.

The session is automatically persisted to disk and cached in memory.

Source

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

Load a session from disk.

If the session is already cached in memory, returns the cached version.

Source

pub async fn save_session(&self, session: &Session) -> Result<()>

Save a session to disk.

Source

pub async fn add_entry( &self, session_id: &SessionId, entry_type: EntryType, content: Value, ) -> Result<SessionEntry>

Add an entry to a session.

The session is automatically saved after adding the entry.

Source

pub async fn complete_session(&self, session_id: &SessionId) -> Result<()>

Complete a session.

Marks the session as completed and saves it.

Source

pub async fn fail_session(&self, session_id: &SessionId) -> Result<()>

Fail a session.

Marks the session as failed and saves it.

Source

pub async fn cancel_session(&self, session_id: &SessionId) -> Result<()>

Cancel a session.

Marks the session as cancelled and saves it.

Source

pub async fn pause_session(&self, session_id: &SessionId) -> Result<()>

Pause a session.

Marks the session as paused and saves it.

Source

pub async fn resume_session(&self, session_id: &SessionId) -> Result<()>

Resume a paused session.

Marks the session as active and saves it.

Source

pub async fn list_sessions( &self, filter: Option<&SessionFilter>, ) -> Result<Vec<SessionMetadata>>

List all sessions, optionally filtered.

Source

pub async fn delete_session(&self, session_id: &SessionId) -> Result<()>

Delete a session.

Removes the session from disk and cache.

Source

pub async fn session_exists(&self, session_id: &SessionId) -> bool

Check if a session exists.

Source

pub async fn peek_session(&self, session_id: &SessionId) -> Result<Session>

Get a session without loading it into cache.

Useful for one-off reads where caching isn’t beneficial.

Source

pub async fn evict_from_cache(&self, session_id: &SessionId)

Evict a session from the in-memory cache.

The session remains on disk but is removed from memory.

Source

pub async fn clear_cache(&self)

Clear all sessions from the in-memory cache.

Source

pub async fn cached_session_count(&self) -> usize

Get the number of cached sessions.

Source

pub async fn active_sessions(&self) -> Vec<Session>

Get all active sessions (in-memory only).

Source

pub async fn find_by_tag(&self, tag: &str) -> Result<Vec<SessionMetadata>>

Find sessions by tag.

Source

pub async fn find_by_type( &self, session_type_name: &str, ) -> Result<Vec<SessionMetadata>>

Find sessions by type.

Source

pub async fn find_by_status( &self, status: SessionStatus, ) -> Result<Vec<SessionMetadata>>

Find sessions by status.

Source

pub async fn find_created_after( &self, timestamp: Timestamp, ) -> Result<Vec<SessionMetadata>>

Find sessions created after a timestamp.

Source

pub async fn find_updated_after( &self, timestamp: Timestamp, ) -> Result<Vec<SessionMetadata>>

Find sessions updated after a timestamp.

Source

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

Get session count on disk.

Trait Implementations§

Source§

impl Debug for SessionManager

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