pub struct StdioTransport { /* private fields */ }Expand description
Generic JSON-RPC-over-stdio transport for local subprocess agents.
Wraps a child process and provides:
call: send a request and await its response.notify: send a fire-and-forget notification.respond/respond_error: reply to incoming server-initiated requests.set_notification_handler: register the handler that receives all incoming server→client messages.
The child process is killed when this struct is dropped.
Implementations§
Source§impl StdioTransport
impl StdioTransport
Sourcepub fn from_child(
child: Child,
stdin: ChildStdin,
stdout: ChildStdout,
stderr: ChildStderr,
rpc_timeout: Duration,
) -> Self
pub fn from_child( child: Child, stdin: ChildStdin, stdout: ChildStdout, stderr: ChildStderr, rpc_timeout: Duration, ) -> Self
Wire up transport from a spawned subprocess’s stdin/stdout/stderr.
Spawns background tasks for the writer (stdin), stderr logger, and the reader (stdout) that dispatches JSON-RPC messages.
Sourcepub fn new_for_testing(
write_tx: UnboundedSender<String>,
rpc_timeout: Duration,
) -> Self
pub fn new_for_testing( write_tx: UnboundedSender<String>, rpc_timeout: Duration, ) -> Self
Construct a transport with a pre-wired channel for unit tests.
No subprocess is spawned and no background tasks are started. The caller can drive the mock by reading from the paired receiver.
Sourcepub fn set_notification_handler(
&self,
handler: Arc<dyn Fn(Value) -> Result<()> + Send + Sync>,
)
pub fn set_notification_handler( &self, handler: Arc<dyn Fn(Value) -> Result<()> + Send + Sync>, )
Register a handler for incoming server→client requests and notifications.
Must be called once after construction. Subsequent calls overwrite the
previous handler. The handler receives the raw JSON message value for
every incoming message that is not a response to a pending call.
Sourcepub async fn call(&self, method: &str, params: Value) -> AcpResult<Value>
pub async fn call(&self, method: &str, params: Value) -> AcpResult<Value>
Send a JSON-RPC request and wait for its response.
Assigns a monotonically increasing id, inserts it into the pending
table, serialises the message, and awaits the reply up to rpc_timeout.
§Errors
Returns AcpError::Timeout if the peer does not reply in time, or
AcpError::Internal if the transport is shut down.
Sourcepub fn notify(&self, method: &str, params: Value) -> AcpResult<()>
pub fn notify(&self, method: &str, params: Value) -> AcpResult<()>
Send a JSON-RPC notification (no response expected).
§Errors
Returns an error if serialisation fails or the writer task has shut down.
Sourcepub fn respond(&self, id: i64, result: Value) -> AcpResult<()>
pub fn respond(&self, id: i64, result: Value) -> AcpResult<()>
Send a JSON-RPC success response to an incoming server request.
Use this to reply to messages received by the notification handler when
they carry an id field (i.e. they expect a response).
§Errors
Returns an error if serialisation fails or the writer task has shut down.