pub struct SessionManager { /* private fields */ }Expand description
Session manager handles multiple concurrent sessions
Implementations§
Source§impl SessionManager
impl SessionManager
Sourcepub fn new(
llm_client: Option<Arc<dyn LlmClient>>,
tool_executor: Arc<ToolExecutor>,
) -> Self
pub fn new( llm_client: Option<Arc<dyn LlmClient>>, tool_executor: Arc<ToolExecutor>, ) -> Self
Create a new session manager without persistence
Sourcepub async fn with_persistence<P: AsRef<Path>>(
llm_client: Option<Arc<dyn LlmClient>>,
tool_executor: Arc<ToolExecutor>,
sessions_dir: P,
) -> Result<Self>
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.
Sourcepub fn with_store(
llm_client: Option<Arc<dyn LlmClient>>,
tool_executor: Arc<ToolExecutor>,
store: Arc<dyn SessionStore>,
backend: StorageBackend,
) -> Self
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.
Sourcepub async fn set_default_llm(&self, client: Option<Arc<dyn LlmClient>>)
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.
Sourcepub async fn set_skill_registry(
&self,
registry: Arc<SkillRegistry>,
skills_dir: PathBuf,
)
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.
Sourcepub async fn skill_registry(&self) -> Option<Arc<SkillRegistry>>
pub async fn skill_registry(&self) -> Option<Arc<SkillRegistry>>
Get the current skill registry, if any.
Sourcepub async fn set_session_skill_registry(
&self,
session_id: impl Into<String>,
registry: Arc<SkillRegistry>,
)
pub async fn set_session_skill_registry( &self, session_id: impl Into<String>, registry: Arc<SkillRegistry>, )
Override the active skill registry for a single session.
Sourcepub async fn session_skill_registry(
&self,
session_id: &str,
) -> Option<Arc<SkillRegistry>>
pub async fn session_skill_registry( &self, session_id: &str, ) -> Option<Arc<SkillRegistry>>
Get the active skill registry for a single session, if one was set.
Sourcepub async fn set_memory_store(&self, store: Arc<dyn MemoryStore>)
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.
Sourcepub async fn memory_store(&self) -> Option<Arc<dyn MemoryStore>>
pub async fn memory_store(&self) -> Option<Arc<dyn MemoryStore>>
Get the current memory store, if any.
Sourcepub async fn set_mcp_manager(&self, manager: Arc<McpManager>)
pub async fn set_mcp_manager(&self, manager: Arc<McpManager>)
Set the shared MCP manager.
When set, all tools from connected MCP servers are registered into the
ToolExecutor so they are available in every session.
Sourcepub async fn add_mcp_server(&self, config: McpServerConfig) -> Result<()>
pub async fn add_mcp_server(&self, config: McpServerConfig) -> Result<()>
Dynamically add and connect an MCP server to the shared manager.
Registers the server config, connects it, and registers its tools into
the ToolExecutor so all subsequent sessions can use them.
Sourcepub async fn remove_mcp_server(&self, name: &str) -> Result<()>
pub async fn remove_mcp_server(&self, name: &str) -> Result<()>
Disconnect and remove an MCP server from the shared manager.
Also unregisters its tools from the ToolExecutor.
Sourcepub async fn mcp_status(&self) -> HashMap<String, McpServerStatus>
pub async fn mcp_status(&self) -> HashMap<String, McpServerStatus>
Get status of all connected MCP servers.
Sourcepub async fn set_document_parser_registry(
&self,
registry: Arc<DocumentParserRegistry>,
)
pub async fn set_document_parser_registry( &self, registry: Arc<DocumentParserRegistry>, )
Set the shared document parser registry for document-aware tools.
Sourcepub async fn document_parser_registry(
&self,
) -> Option<Arc<DocumentParserRegistry>>
pub async fn document_parser_registry( &self, ) -> Option<Arc<DocumentParserRegistry>>
Get the current shared document parser registry, if any.
Sourcepub async fn restore_session_by_id(&self, session_id: &str) -> Result<()>
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.
Sourcepub async fn load_all_sessions(&mut self) -> Result<usize>
pub async fn load_all_sessions(&mut self) -> Result<usize>
Load all sessions from all registered stores
Sourcepub async fn create_session(
&self,
id: String,
config: SessionConfig,
) -> Result<String>
pub async fn create_session( &self, id: String, config: SessionConfig, ) -> Result<String>
Create a new session
Sourcepub async fn destroy_session(&self, id: &str) -> Result<()>
pub async fn destroy_session(&self, id: &str) -> Result<()>
Destroy a session
Sourcepub async fn create_child_session(
&self,
parent_id: &str,
child_id: String,
config: SessionConfig,
) -> Result<String>
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.
Sourcepub async fn get_child_sessions(&self, parent_id: &str) -> Vec<String>
pub async fn get_child_sessions(&self, parent_id: &str) -> Vec<String>
Get all child sessions for a parent session
Sourcepub async fn is_child_session(&self, session_id: &str) -> Result<bool>
pub async fn is_child_session(&self, session_id: &str) -> Result<bool>
Check if a session is a child session
Sourcepub async fn generate(
&self,
session_id: &str,
prompt: &str,
) -> Result<AgentResult>
pub async fn generate( &self, session_id: &str, prompt: &str, ) -> Result<AgentResult>
Generate response for a prompt
Sourcepub async fn generate_streaming(
&self,
session_id: &str,
prompt: &str,
) -> Result<(Receiver<AgentEvent>, JoinHandle<Result<AgentResult>>, CancellationToken)>
pub async fn generate_streaming( &self, session_id: &str, prompt: &str, ) -> Result<(Receiver<AgentEvent>, JoinHandle<Result<AgentResult>>, CancellationToken)>
Generate response with streaming events
Sourcepub async fn context_usage(&self, session_id: &str) -> Result<ContextUsage>
pub async fn context_usage(&self, session_id: &str) -> Result<ContextUsage>
Get context usage for a session
Sourcepub async fn history(&self, session_id: &str) -> Result<Vec<Message>>
pub async fn history(&self, session_id: &str) -> Result<Vec<Message>>
Get conversation history for a session
Sourcepub async fn maybe_auto_compact(&self, session_id: &str) -> Result<bool>
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_compactis enabled in session configcontext_usage.percentexceedsauto_compact_threshold
Sourcepub async fn get_llm_for_session(
&self,
session_id: &str,
) -> Result<Option<Arc<dyn LlmClient>>>
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.
Sourcepub async fn btw_with_context(
&self,
session_id: &str,
question: &str,
runtime_context: Option<&str>,
) -> Result<BtwResult>
pub async fn btw_with_context( &self, session_id: &str, question: &str, runtime_context: Option<&str>, ) -> Result<BtwResult>
Ask an ephemeral side question for an existing managed session.
Uses a read-only snapshot of the session history plus current queue /
confirmation / task state. Optional runtime_context lets hosts inject
extra transient context such as browser-only UI state.
Sourcepub async fn configure(
&self,
session_id: &str,
thinking: Option<bool>,
budget: Option<usize>,
model_config: Option<LlmConfig>,
) -> Result<()>
pub async fn configure( &self, session_id: &str, thinking: Option<bool>, budget: Option<usize>, model_config: Option<LlmConfig>, ) -> Result<()>
Configure session
Sourcepub async fn set_system_prompt(
&self,
session_id: &str,
system_prompt: Option<String>,
) -> Result<()>
pub async fn set_system_prompt( &self, session_id: &str, system_prompt: Option<String>, ) -> Result<()>
Update a session’s system prompt in place.
Sourcepub async fn session_count(&self) -> usize
pub async fn session_count(&self) -> usize
Get session count
Sourcepub async fn store_health(&self) -> Vec<(String, Result<()>)>
pub async fn store_health(&self) -> Vec<(String, Result<()>)>
Check health of all registered stores
Sourcepub fn list_tools(&self) -> Vec<ToolDefinition>
pub fn list_tools(&self) -> Vec<ToolDefinition>
List all loaded tools (built-in tools)
Sourcepub async fn pause_session(&self, session_id: &str) -> Result<bool>
pub async fn pause_session(&self, session_id: &str) -> Result<bool>
Pause a session
Sourcepub async fn resume_session(&self, session_id: &str) -> Result<bool>
pub async fn resume_session(&self, session_id: &str) -> Result<bool>
Resume a session
Sourcepub async fn cancel_operation(&self, session_id: &str) -> Result<bool>
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.
Sourcepub async fn get_all_sessions(&self) -> Vec<Arc<RwLock<Session>>>
pub async fn get_all_sessions(&self) -> Vec<Arc<RwLock<Session>>>
Get all sessions (returns session locks for iteration)
Sourcepub fn tool_executor(&self) -> &Arc<ToolExecutor>
pub fn tool_executor(&self) -> &Arc<ToolExecutor>
Get tool executor reference
Sourcepub async fn confirm_tool(
&self,
session_id: &str,
tool_id: &str,
approved: bool,
reason: Option<String>,
) -> Result<bool>
pub async fn confirm_tool( &self, session_id: &str, tool_id: &str, approved: bool, reason: Option<String>, ) -> Result<bool>
Confirm a tool execution (HITL)
Sourcepub async fn set_confirmation_policy(
&self,
session_id: &str,
policy: ConfirmationPolicy,
) -> Result<ConfirmationPolicy>
pub async fn set_confirmation_policy( &self, session_id: &str, policy: ConfirmationPolicy, ) -> Result<ConfirmationPolicy>
Set confirmation policy for a session (HITL)
Sourcepub async fn set_permission_policy(
&self,
session_id: &str,
policy: PermissionPolicy,
) -> Result<PermissionPolicy>
pub async fn set_permission_policy( &self, session_id: &str, policy: PermissionPolicy, ) -> Result<PermissionPolicy>
Set permission policy for a session.
Sourcepub async fn get_confirmation_policy(
&self,
session_id: &str,
) -> Result<ConfirmationPolicy>
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
impl Clone for SessionManager
Source§fn clone(&self) -> SessionManager
fn clone(&self) -> SessionManager
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more