Skip to main content

Module client

Module client 

Source
Expand description

Public Client - the entry point consumers hold.

Client is a cheap-clone handle (Arc-backed) over a background reader task that owns the claude subprocess. All methods take &self, so the same Client can be passed by reference into multiple tasks. The events stream is returned alongside the Client at spawn time as a ClientEvents receiver - single-consumer, independent of the writer-side handle.

Internally:

  • The reader task pulls lines from the subprocess, decodes them, pushes regular Messages onto an mpsc that backs the returned ClientEvents, dispatches inbound control_requests on detached tasks, and routes outbound control_responses to per-request oneshots so Client::send_control callers can await their typed reply.
  • The writer half is a clonable SharedWriter cloned from Subprocess; outbound writes go through it without contending on &mut self.

Re-exportsยง

pub(crate) use control_dispatch::ControlDispatchHandle; ๐Ÿ”’
use std::collections::HashMap; ๐Ÿ”’
use std::collections::VecDeque; ๐Ÿ”’
use std::sync::Arc; ๐Ÿ”’
use std::sync::atomic::AtomicBool; ๐Ÿ”’
use std::sync::atomic::Ordering; ๐Ÿ”’
use tokio::sync::Mutex; ๐Ÿ”’
use tokio::sync::mpsc; ๐Ÿ”’
use tokio::sync::oneshot; ๐Ÿ”’
use tokio::task::JoinHandle; ๐Ÿ”’
use tracing::debug; ๐Ÿ”’
use crate::Error; ๐Ÿ”’
use crate::client::runtime::PendingControls; ๐Ÿ”’
use crate::client::runtime::SharedSessionId; ๐Ÿ”’
use crate::client::runtime::excerpt; ๐Ÿ”’
use crate::client::runtime::spawn_reader_task; ๐Ÿ”’
use crate::mcp::orchestration::McpHosts; ๐Ÿ”’
use crate::options::Options; ๐Ÿ”’
use crate::transport::codec::DecodedLine; ๐Ÿ”’
use crate::transport::codec::decode_dispatch; ๐Ÿ”’
use crate::transport::process::Subprocess; ๐Ÿ”’
use forge_primitives::Message; ๐Ÿ”’

Modulesยง

control_dispatch ๐Ÿ”’
Inbound control_request dispatch: ControlDispatchHandle routes permission checks, MCP JSON-RPC, and hook callbacks to the appropriate handler and writes the matching control_response via a clonable SharedWriter.
control_send ๐Ÿ”’
Outbound control_request typed wrappers - the user-facing commands (interrupt, set_permission_mode, set_model, mcp_reconnect, mcp_toggle, stop_task, mcp_status, get_context_usage) plus the _raw escape hatches for mcp_status and get_context_usage.
runtime ๐Ÿ”’
Background runtime for Client - owns the subprocess reader, decodes incoming frames, dispatches inbound control_requests on detached tasks, and routes outbound control_responses to the PendingControls map so send_control callers can await their typed reply.

Structsยง

Client
An active claude binary subprocess.
ClientInner ๐Ÿ”’
PendingGuard ๐Ÿ”’
Removes a pending_controls entry when the awaiting future is dropped without a response having been routed - the drop guard send_control parks in its future.

Constantsยง

INIT_HANDSHAKE_TIMEOUT
Budget for the initialize handshake inside Client::spawn. A wedged CLI (alive, stdout open, never answering) would otherwise park the caller forever with the child leaked.

Type Aliasesยง

ClientEvents
Stream of regular Messages produced by the reader task. Returned alongside Client from Client::spawn. Single-consumer; the writer-side Client is Clone and can move freely between tasks while one task owns the receiver.