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 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>>)>
pub async fn generate_streaming( &self, session_id: &str, prompt: &str, ) -> Result<(Receiver<AgentEvent>, JoinHandle<Result<AgentResult>>)>
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 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 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 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)
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_permission_policy(
&self,
session_id: &str,
) -> Result<PermissionPolicy>
pub async fn get_permission_policy( &self, session_id: &str, ) -> Result<PermissionPolicy>
Get permission policy for a session
Sourcepub async fn check_permission(
&self,
session_id: &str,
tool_name: &str,
args: &Value,
) -> Result<PermissionDecision>
pub async fn check_permission( &self, session_id: &str, tool_name: &str, args: &Value, ) -> Result<PermissionDecision>
Check permission for a tool invocation
Sourcepub async fn add_permission_rule(
&self,
session_id: &str,
rule_type: &str,
rule: &str,
) -> Result<()>
pub async fn add_permission_rule( &self, session_id: &str, rule_type: &str, rule: &str, ) -> Result<()>
Add a permission rule
Sourcepub async fn add_context_provider(
&self,
session_id: &str,
provider: Arc<dyn ContextProvider>,
) -> Result<()>
pub async fn add_context_provider( &self, session_id: &str, provider: Arc<dyn ContextProvider>, ) -> Result<()>
Add a context provider to a session
Sourcepub async fn remove_context_provider(
&self,
session_id: &str,
name: &str,
) -> Result<bool>
pub async fn remove_context_provider( &self, session_id: &str, name: &str, ) -> Result<bool>
Remove a context provider from a session by name
Sourcepub async fn list_context_providers(
&self,
session_id: &str,
) -> Result<Vec<String>>
pub async fn list_context_providers( &self, session_id: &str, ) -> Result<Vec<String>>
List context provider names for a session
Sourcepub async fn set_lane_handler(
&self,
session_id: &str,
lane: SessionLane,
config: LaneHandlerConfig,
) -> Result<()>
pub async fn set_lane_handler( &self, session_id: &str, lane: SessionLane, config: LaneHandlerConfig, ) -> Result<()>
Set lane handler configuration
Sourcepub async fn get_lane_handler(
&self,
session_id: &str,
lane: SessionLane,
) -> Result<LaneHandlerConfig>
pub async fn get_lane_handler( &self, session_id: &str, lane: SessionLane, ) -> Result<LaneHandlerConfig>
Get lane handler configuration
Sourcepub async fn complete_external_task(
&self,
session_id: &str,
task_id: &str,
result: ExternalTaskResult,
) -> Result<bool>
pub async fn complete_external_task( &self, session_id: &str, task_id: &str, result: ExternalTaskResult, ) -> Result<bool>
Complete an external task
Sourcepub async fn pending_external_tasks(
&self,
session_id: &str,
) -> Result<Vec<ExternalTask>>
pub async fn pending_external_tasks( &self, session_id: &str, ) -> Result<Vec<ExternalTask>>
Get pending external tasks for a session
Sourcepub async fn set_tasks(
&self,
session_id: &str,
tasks: Vec<Task>,
) -> Result<Vec<Task>>
pub async fn set_tasks( &self, session_id: &str, tasks: Vec<Task>, ) -> Result<Vec<Task>>
Set tasks for a session
Sourcepub async fn fork_session(
&self,
source_id: &str,
new_id: String,
new_name: Option<String>,
) -> Result<String>
pub async fn fork_session( &self, source_id: &str, new_id: String, new_name: Option<String>, ) -> Result<String>
Fork a session, creating a new session with copied history and configuration
The forked session gets:
- A new unique ID
- Copied conversation history (messages)
- Copied configuration (with optional name override)
- Copied usage statistics and cost
- Copied tasks
parent_idset to the source session ID- Fresh timestamps (created_at = now)
Non-serializable state (queue, HITL, permissions) is freshly initialized.
Sourcepub async fn generate_title(&self, session_id: &str) -> Result<Option<String>>
pub async fn generate_title(&self, session_id: &str) -> Result<Option<String>>
Generate a short title for a session based on its conversation content
Uses the session’s LLM client (or default) to generate a concise title from the first few messages. The title is automatically set on the session.
Returns the generated title, or None if no LLM client is available or the session has no messages.
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