Skip to main content

SessionManager

Struct SessionManager 

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

Session manager handles multiple concurrent sessions

Implementations§

Source§

impl SessionManager

Source

pub fn new( llm_client: Option<Arc<dyn LlmClient>>, tool_executor: Arc<ToolExecutor>, ) -> Self

Create a new session manager without persistence

Source

pub async fn with_persistence<P: AsRef<Path>>( llm_client: Option<Arc<dyn LlmClient>>, tool_executor: Arc<ToolExecutor>, sessions_dir: P, ) -> Result<Self>

Create a session manager with file-based persistence

Sessions will be automatically saved to disk and restored on startup.

Source

pub fn with_store( llm_client: Option<Arc<dyn LlmClient>>, tool_executor: Arc<ToolExecutor>, store: Arc<dyn SessionStore>, backend: StorageBackend, ) -> Self

Create a session manager with a custom store

The backend parameter determines which StorageBackend key the store is registered under. Sessions created with a matching storage_type will use this store.

Source

pub async fn set_default_llm(&self, client: Option<Arc<dyn LlmClient>>)

Hot-swap the default LLM client used by sessions without a per-session client.

Called by SafeClaw when PUT /api/agent/config updates the model config. All subsequent generate / generate_streaming calls will use the new client.

Source

pub async fn set_skill_registry( &self, registry: Arc<SkillRegistry>, skills_dir: PathBuf, )

Set the skill registry for runtime skill management.

Skills registered here will be injected into the system prompt for all subsequent generate / generate_streaming calls. Also registers the manage_skill tool on the ToolExecutor so the Agent can create, list, and remove skills at runtime.

Source

pub async fn skill_registry(&self) -> Option<Arc<SkillRegistry>>

Get the current skill registry, if any.

Source

pub async fn set_memory_store(&self, store: Arc<dyn MemoryStore>)

Set the shared memory store for agent long-term memory.

When set, every generate/generate_streaming call wraps this store in an AgentMemory and injects it into AgentConfig.memory, enabling automatic recall_similar before prompts and remember_success/ remember_failure after tool execution.

Source

pub async fn memory_store(&self) -> Option<Arc<dyn MemoryStore>>

Get the current memory store, if any.

Source

pub async fn restore_session_by_id(&self, session_id: &str) -> Result<()>

Restore a single session by ID from the store

Searches all registered stores for the given session ID and restores it into the in-memory session map. Returns an error if not found.

Source

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

Load all sessions from all registered stores

Source

pub async fn create_session( &self, id: String, config: SessionConfig, ) -> Result<String>

Create a new session

Source

pub async fn destroy_session(&self, id: &str) -> Result<()>

Destroy a session

Source

pub async fn get_session(&self, id: &str) -> Result<Arc<RwLock<Session>>>

Get a session by ID

Source

pub async fn create_child_session( &self, parent_id: &str, child_id: String, config: SessionConfig, ) -> Result<String>

Create a child session for a subagent

Child sessions inherit the parent’s LLM client but have their own permission policy and configuration.

Source

pub async fn get_child_sessions(&self, parent_id: &str) -> Vec<String>

Get all child sessions for a parent session

Source

pub async fn is_child_session(&self, session_id: &str) -> Result<bool>

Check if a session is a child session

Source

pub async fn generate( &self, session_id: &str, prompt: &str, ) -> Result<AgentResult>

Generate response for a prompt

Source

pub async fn generate_streaming( &self, session_id: &str, prompt: &str, ) -> Result<(Receiver<AgentEvent>, JoinHandle<Result<AgentResult>>)>

Generate response with streaming events

Source

pub async fn context_usage(&self, session_id: &str) -> Result<ContextUsage>

Get context usage for a session

Source

pub async fn history(&self, session_id: &str) -> Result<Vec<Message>>

Get conversation history for a session

Source

pub async fn clear(&self, session_id: &str) -> Result<()>

Clear session history

Source

pub async fn compact(&self, session_id: &str) -> Result<()>

Compact session context

Source

pub async fn maybe_auto_compact(&self, session_id: &str) -> Result<bool>

Check if auto-compaction should be triggered and perform it if needed.

Called after generate() / generate_streaming() updates session usage. Triggers compaction when:

  • auto_compact is enabled in session config
  • context_usage.percent exceeds auto_compact_threshold
Source

pub async fn get_llm_for_session( &self, session_id: &str, ) -> Result<Option<Arc<dyn LlmClient>>>

Resolve the LLM client for a session (session-level -> default fallback)

Returns None if no LLM client is configured at either level.

Source

pub async fn configure( &self, session_id: &str, thinking: Option<bool>, budget: Option<usize>, model_config: Option<LlmConfig>, ) -> Result<()>

Configure session

Source

pub async fn session_count(&self) -> usize

Get session count

Source

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

Check health of all registered stores

Source

pub fn list_tools(&self) -> Vec<ToolDefinition>

List all loaded tools (built-in tools)

Source

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

Pause a session

Source

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

Resume a session

Source

pub async fn cancel_operation(&self, session_id: &str) -> Result<bool>

Cancel an ongoing operation for a session

Returns true if an operation was cancelled, false if no operation was running.

Source

pub async fn get_all_sessions(&self) -> Vec<Arc<RwLock<Session>>>

Get all sessions (returns session locks for iteration)

Source

pub fn tool_executor(&self) -> &Arc<ToolExecutor>

Get tool executor reference

Source

pub async fn confirm_tool( &self, session_id: &str, tool_id: &str, approved: bool, reason: Option<String>, ) -> Result<bool>

Confirm a tool execution (HITL)

Source

pub async fn set_confirmation_policy( &self, session_id: &str, policy: ConfirmationPolicy, ) -> Result<ConfirmationPolicy>

Set confirmation policy for a session (HITL)

Source

pub async fn get_confirmation_policy( &self, session_id: &str, ) -> Result<ConfirmationPolicy>

Get confirmation policy for a session (HITL)

Trait Implementations§

Source§

impl Clone for SessionManager

Source§

fn clone(&self) -> SessionManager

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. 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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> FutureExt for T

Source§

fn with_context(self, otel_cx: Context) -> WithContext<Self>

Attaches the provided Context to this type, returning a WithContext wrapper. Read more
Source§

fn with_current_context(self) -> WithContext<Self>

Attaches the current Context to this type, returning a WithContext wrapper. Read more
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> 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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