Skip to main content

SessionManager

Struct SessionManager 

Source
pub struct SessionManager<P: Platform> { /* private fields */ }
Expand description

Manages conversation sessions with in-memory caching and JSONL persistence.

Sessions are identified by a string key (typically "{channel}:{chat_id}"). The manager uses a write-through cache: reads check the in-memory cache first, then fall back to loading from disk. Writes update both the cache and the JSONL file.

§JSONL format

Each session file is named {sanitized_key}.jsonl and contains:

  • Line 1: metadata object with _type, created_at, updated_at, metadata, and last_consolidated fields.
  • Lines 2+: message objects with role, content, and timestamp fields.

§Platform abstraction

All filesystem I/O goes through the Platform::fs() trait, making SessionManager testable with mock filesystems and WASM-portable.

Implementations§

Source§

impl<P: Platform> SessionManager<P>

Source

pub async fn new(platform: Arc<P>) -> Result<Self>

Create a new session manager using the given platform.

Discovers the sessions directory by checking:

  1. ~/.clawft/workspace/sessions/
  2. ~/.nanobot/workspace/sessions/ (legacy fallback)

If neither exists, defaults to ~/.clawft/workspace/sessions/ and creates it. Returns an error if the home directory cannot be determined.

Source

pub fn with_dir(platform: Arc<P>, sessions_dir: PathBuf) -> Self

Create a session manager with an explicit sessions directory.

Useful for testing or when the directory is already known.

Source

pub async fn get_or_create(&self, key: &str) -> Result<Session>

Get an existing session or create a new one.

Checks the in-memory cache first, then attempts to load from disk. If neither succeeds, creates a fresh empty session and caches it.

§Errors

Returns an error if key fails session-ID validation.

Source

pub async fn load_session(&self, key: &str) -> Result<Session>

Load a session from its JSONL file on disk.

Parses the first line as metadata and remaining lines as messages. Returns an error if the file does not exist or contains invalid JSON.

Includes a migration path: if the percent-encoded file does not exist but the old underscore-encoded file does, the content is copied to the new filename (the old file is preserved for safety).

Source

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

Save a session to its JSONL file on disk.

Writes the full session: metadata line followed by all message lines. Also updates the in-memory cache.

Source

pub async fn append_turn( &self, key: &str, role: &str, content: &str, ) -> Result<()>

Append a single conversation turn to a session.

Updates both the in-memory cache and appends to the JSONL file on disk. If the session does not exist yet, it is created via [get_or_create].

Source

pub async fn list_sessions(&self) -> Result<Vec<String>>

List all session keys (derived from .jsonl filenames on disk).

Decodes percent-encoded filenames back to the original session key. Files that cannot be decoded as valid UTF-8 are skipped with a warning.

Source

pub async fn invalidate(&self, key: &str)

Remove a session from the in-memory cache.

The JSONL file on disk is not deleted; only the cached copy is evicted. The next [get_or_create] call will reload from disk.

Source

pub async fn delete_session(&self, key: &str) -> Result<()>

Delete a session file from disk and remove from cache.

Source

pub fn sessions_dir(&self) -> &PathBuf

Get the sessions directory path.

Auto Trait Implementations§

§

impl<P> Freeze for SessionManager<P>

§

impl<P> !RefUnwindSafe for SessionManager<P>

§

impl<P> Send for SessionManager<P>

§

impl<P> Sync for SessionManager<P>

§

impl<P> Unpin for SessionManager<P>

§

impl<P> UnsafeUnpin for SessionManager<P>

§

impl<P> !UnwindSafe for SessionManager<P>

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

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
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