pub struct Session {
pub session_id: String,
pub cwd: PathBuf,
/* private fields */
}Expand description
An active Claude session
Each session holds its own ClaudeClient instance and maintains independent state for usage tracking, permissions, and message conversion.
Fields§
§session_id: StringUnique session identifier
cwd: PathBufWorking directory for this session
Implementations§
Source§impl Session
impl Session
Sourcepub fn new(
session_id: String,
cwd: PathBuf,
config: &AgentConfig,
meta: Option<&NewSessionMeta>,
) -> Result<Arc<Self>>
pub fn new( session_id: String, cwd: PathBuf, config: &AgentConfig, meta: Option<&NewSessionMeta>, ) -> Result<Arc<Self>>
Create a new session and wrap in Arc
Returns Arc
§Arguments
session_id- Unique identifier for this sessioncwd- Working directoryconfig- Agent configuration from environmentmeta- Session metadata from the new session request
Sourcepub fn set_external_mcp_servers(&self, servers: Vec<McpServer>)
pub fn set_external_mcp_servers(&self, servers: Vec<McpServer>)
Set external MCP servers to connect
§Arguments
servers- List of MCP servers from the client request
Sourcepub fn set_connection_cx(&self, cx: JrConnectionCx<AgentToClient>)
pub fn set_connection_cx(&self, cx: JrConnectionCx<AgentToClient>)
Set the connection context for ACP requests
This is called once during handle_prompt to enable permission requests. The OnceLock ensures it’s only set once even if called multiple times.
Sourcepub fn get_connection_cx(&self) -> Option<&JrConnectionCx<AgentToClient>>
pub fn get_connection_cx(&self) -> Option<&JrConnectionCx<AgentToClient>>
Get the connection context if available
Returns None if called before handle_prompt sets the connection.
Sourcepub fn cache_permission(&self, tool_input: &Value, allowed: bool)
pub fn cache_permission(&self, tool_input: &Value, allowed: bool)
Cache a permission result for a tool_input
Called by PreToolUse hook after user grants permission. The can_use_tool callback checks this cache before sending permission requests.
Sourcepub fn check_cached_permission(&self, tool_input: &Value) -> Option<bool>
pub fn check_cached_permission(&self, tool_input: &Value) -> Option<bool>
Check if a tool_input has cached permission
Called by can_use_tool callback to check if permission was already granted. Returns Some(true) if allowed, Some(false) if denied, None if not cached. Removes the entry from cache after retrieval (one-time use).
Sourcepub fn permission_cache(&self) -> Arc<DashMap<String, bool>>
pub fn permission_cache(&self) -> Arc<DashMap<String, bool>>
Get a reference to the permission_cache for sharing with hooks
Sourcepub fn cache_tool_use_id(&self, tool_input: &Value, tool_use_id: &str)
pub fn cache_tool_use_id(&self, tool_input: &Value, tool_use_id: &str)
Cache tool_use_id for a tool_input
Called by PreToolUse hook when Ask decision is made. The can_use_tool callback uses this to get tool_use_id when CLI doesn’t provide it.
Sourcepub fn get_cached_tool_use_id(&self, tool_input: &Value) -> Option<String>
pub fn get_cached_tool_use_id(&self, tool_input: &Value) -> Option<String>
Get cached tool_use_id for a tool_input
Called by can_use_tool callback to get tool_use_id when CLI doesn’t provide it. Returns the tool_use_id if cached, None otherwise. Removes the entry from cache after retrieval (one-time use).
Sourcepub fn tool_use_id_cache(&self) -> Arc<DashMap<String, String>>
pub fn tool_use_id_cache(&self) -> Arc<DashMap<String, String>>
Get a reference to the tool_use_id_cache for sharing with hooks
Sourcepub async fn connect_external_mcp_servers(&self) -> Result<()>
pub async fn connect_external_mcp_servers(&self) -> Result<()>
Connect to external MCP servers
This should be called before the first prompt to ensure all external MCP tools are available.
Sourcepub async fn connect(&self) -> Result<()>
pub async fn connect(&self) -> Result<()>
Connect to Claude CLI
This spawns the Claude CLI process and establishes JSON-RPC communication.
Sourcepub async fn disconnect(&self) -> Result<()>
pub async fn disconnect(&self) -> Result<()>
Disconnect from Claude CLI
Sourcepub fn is_connected(&self) -> bool
pub fn is_connected(&self) -> bool
Check if the session is connected
Sourcepub async fn client(&self) -> RwLockReadGuard<'_, ClaudeClient>
pub async fn client(&self) -> RwLockReadGuard<'_, ClaudeClient>
Get read access to the client
Sourcepub async fn client_mut(&self) -> RwLockWriteGuard<'_, ClaudeClient>
pub async fn client_mut(&self) -> RwLockWriteGuard<'_, ClaudeClient>
Get write access to the client
Sourcepub fn cancel_receiver(&self) -> Receiver<()>
pub fn cancel_receiver(&self) -> Receiver<()>
Get a receiver for cancel signals
This can be used to listen for MCP cancellation notifications. When a cancel notification is received, a signal is sent through the channel.
Sourcepub async fn cancel(&self)
pub async fn cancel(&self)
Cancel this session and interrupt the Claude CLI
This sends an interrupt signal to the Claude CLI to stop the current operation. Note: This does NOT use a cancelled flag anymore - cancellation is handled per-prompt via CancellationToken in the PromptManager.
Sourcepub async fn permission(&self) -> RwLockReadGuard<'_, PermissionHandler>
pub async fn permission(&self) -> RwLockReadGuard<'_, PermissionHandler>
Get the permission handler
Sourcepub async fn permission_mode(&self) -> PermissionMode
pub async fn permission_mode(&self) -> PermissionMode
Get the current permission mode
Sourcepub async fn set_permission_mode(&self, mode: PermissionMode)
pub async fn set_permission_mode(&self, mode: PermissionMode)
Set the permission mode
Updates the PermissionHandler. The hook will read the mode from the same PermissionHandler, ensuring consistency.
Sourcepub fn send_mode_update(&self, mode: &str)
pub fn send_mode_update(&self, mode: &str)
Send session/update notification for permission mode change
This sends a CurrentModeUpdate notification to the client to inform it that the permission mode has changed. This is used for ExitPlanMode to notify the UI that the mode has been switched.
Sourcepub async fn add_permission_allow_rule(&self, tool_name: &str)
pub async fn add_permission_allow_rule(&self, tool_name: &str)
Add an allow rule for a tool
This is called when user selects “Always Allow” in permission prompt.
Sourcepub fn current_model(&self) -> Option<String>
pub fn current_model(&self) -> Option<String>
Get the current model ID
Note: Not yet used because sacp SDK does not support SetSessionModel.
Sourcepub fn set_model(&self, model_id: String)
pub fn set_model(&self, model_id: String)
Set the model for this session
Note: Not yet used because sacp SDK does not support SetSessionModel.
Sourcepub fn usage_tracker(&self) -> &UsageTracker
pub fn usage_tracker(&self) -> &UsageTracker
Get the usage tracker
Sourcepub fn converter(&self) -> &NotificationConverter
pub fn converter(&self) -> &NotificationConverter
Get the notification converter
Sourcepub fn hook_callback_registry(&self) -> &Arc<HookCallbackRegistry>
pub fn hook_callback_registry(&self) -> &Arc<HookCallbackRegistry>
Get the hook callback registry
Sourcepub fn permission_checker(&self) -> &Arc<RwLock<PermissionChecker>>
pub fn permission_checker(&self) -> &Arc<RwLock<PermissionChecker>>
Get the permission checker
Sourcepub fn register_post_tool_use_callback(
&self,
tool_use_id: String,
callback: PostToolUseCallback,
)
pub fn register_post_tool_use_callback( &self, tool_use_id: String, callback: PostToolUseCallback, )
Register a PostToolUse callback for a tool use
Sourcepub fn acp_mcp_server(&self) -> &Arc<AcpMcpServer>
pub fn acp_mcp_server(&self) -> &Arc<AcpMcpServer>
Get the ACP MCP server
Sourcepub fn background_processes(&self) -> &Arc<BackgroundProcessManager>
pub fn background_processes(&self) -> &Arc<BackgroundProcessManager>
Get the background process manager
Sourcepub async fn configure_acp_server(
&self,
connection_cx: JrConnectionCx<AgentToClient>,
terminal_client: Option<Arc<TerminalClient>>,
)
pub async fn configure_acp_server( &self, connection_cx: JrConnectionCx<AgentToClient>, terminal_client: Option<Arc<TerminalClient>>, )
Configure the ACP MCP server with connection and terminal client
This should be called after creating the session to enable Terminal API integration for Bash commands.
Trait Implementations§
Auto Trait Implementations§
impl !Freeze for Session
impl !RefUnwindSafe for Session
impl Send for Session
impl Sync for Session
impl Unpin for Session
impl !UnwindSafe for Session
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> FutureExt for T
impl<T> FutureExt for T
Source§fn with_context(self, otel_cx: Context) -> WithContext<Self>
fn with_context(self, otel_cx: Context) -> WithContext<Self>
Source§fn with_current_context(self) -> WithContext<Self>
fn with_current_context(self) -> WithContext<Self>
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoMaybeUndefined<T> for T
impl<T> IntoMaybeUndefined<T> for T
fn into_maybe_undefined(self) -> MaybeUndefined<T>
Source§impl<T> IntoOption<T> for T
impl<T> IntoOption<T> for T
fn into_option(self) -> Option<T>
Source§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a tonic::Request