Session

Struct Session 

Source
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: String

Unique session identifier

§cwd: PathBuf

Working directory for this session

Implementations§

Source§

impl Session

Source

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 because the can_use_tool callback needs Arc. We use OnceLock to break the circular dependency between Session and callback.

§Arguments
  • session_id - Unique identifier for this session
  • cwd - Working directory
  • config - Agent configuration from environment
  • meta - Session metadata from the new session request
Source

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
Source

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.

Source

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.

Source

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.

Source

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).

Source

pub fn permission_cache(&self) -> Arc<DashMap<String, bool>>

Get a reference to the permission_cache for sharing with hooks

Source

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.

Source

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).

Source

pub fn tool_use_id_cache(&self) -> Arc<DashMap<String, String>>

Get a reference to the tool_use_id_cache for sharing with hooks

Source

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.

Source

pub async fn connect(&self) -> Result<()>

Connect to Claude CLI

This spawns the Claude CLI process and establishes JSON-RPC communication.

Source

pub async fn disconnect(&self) -> Result<()>

Disconnect from Claude CLI

Source

pub fn is_connected(&self) -> bool

Check if the session is connected

Source

pub async fn client(&self) -> RwLockReadGuard<'_, ClaudeClient>

Get read access to the client

Source

pub async fn client_mut(&self) -> RwLockWriteGuard<'_, ClaudeClient>

Get write access to the client

Source

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.

Source

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.

Source

pub async fn permission(&self) -> RwLockReadGuard<'_, PermissionHandler>

Get the permission handler

Source

pub async fn permission_mode(&self) -> PermissionMode

Get the current permission mode

Source

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.

Source

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.

Source

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.

Source

pub fn current_model(&self) -> Option<String>

Get the current model ID

Note: Not yet used because sacp SDK does not support SetSessionModel.

Source

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.

Source

pub fn usage_tracker(&self) -> &UsageTracker

Get the usage tracker

Source

pub fn converter(&self) -> &NotificationConverter

Get the notification converter

Source

pub fn hook_callback_registry(&self) -> &Arc<HookCallbackRegistry>

Get the hook callback registry

Source

pub fn permission_checker(&self) -> &Arc<RwLock<PermissionChecker>>

Get the permission checker

Source

pub fn register_post_tool_use_callback( &self, tool_use_id: String, callback: PostToolUseCallback, )

Register a PostToolUse callback for a tool use

Source

pub fn acp_mcp_server(&self) -> &Arc<AcpMcpServer>

Get the ACP MCP server

Source

pub fn background_processes(&self) -> &Arc<BackgroundProcessManager>

Get the background process manager

Source

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§

Source§

impl Debug for Session

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. 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> 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> IntoMaybeUndefined<T> for T

Source§

impl<T> IntoOption<T> for T

Source§

impl<T> IntoRequest<T> for T

Source§

fn into_request(self) -> Request<T>

Wrap the input message T in a tonic::Request
Source§

impl<L> LayerExt<L> for L

Source§

fn named_layer<S>(&self, service: S) -> Layered<<L as Layer<S>>::Service, S>
where L: Layer<S>,

Applies the layer to a service and wraps it in Layered.
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, 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