pub struct SyncClient { /* private fields */ }Expand description
Synchronous multi-turn client for the Codex app-server.
Communicates with a long-lived codex app-server process via
newline-delimited JSON-RPC over stdio. Manages request/response
correlation and buffers incoming notifications that arrive while
waiting for RPC responses.
The client automatically kills the app-server process when dropped.
Implementations§
Source§impl SyncClient
impl SyncClient
Sourcepub fn start() -> Result<Self>
pub fn start() -> Result<Self>
Start an app-server with default settings.
Spawns codex app-server --listen stdio:// and returns a connected client.
§Errors
Returns an error if the codex CLI is not installed, the version is
incompatible, or the process fails to start.
Sourcepub fn start_with(builder: AppServerBuilder) -> Result<Self>
pub fn start_with(builder: AppServerBuilder) -> Result<Self>
Start an app-server with a custom AppServerBuilder.
Use this to configure a custom binary path or working directory.
§Errors
Returns an error if the process fails to start or stdio pipes cannot be established.
Sourcepub fn request<P: Serialize, R: DeserializeOwned>(
&mut self,
method: &str,
params: &P,
) -> Result<R>
pub fn request<P: Serialize, R: DeserializeOwned>( &mut self, method: &str, params: &P, ) -> Result<R>
Send a JSON-RPC request and wait for the matching response.
Any notifications or server requests that arrive before the response
are buffered and can be retrieved via SyncClient::next_message.
§Errors
Error::JsonRpcif the server returns a JSON-RPC errorError::ServerClosedif the connection drops before a response arrivesError::Jsonif response deserialization fails
Sourcepub fn thread_start(
&mut self,
params: &ThreadStartParams,
) -> Result<ThreadStartResponse>
pub fn thread_start( &mut self, params: &ThreadStartParams, ) -> Result<ThreadStartResponse>
Start a new thread (conversation session).
A thread must be created before any turns can be started. The returned
ThreadStartResponse contains the thread_id needed for subsequent calls.
Sourcepub fn turn_start(
&mut self,
params: &TurnStartParams,
) -> Result<TurnStartResponse>
pub fn turn_start( &mut self, params: &TurnStartParams, ) -> Result<TurnStartResponse>
Start a new turn within a thread.
Sends user input to the agent. After calling this, use SyncClient::events
or SyncClient::next_message to consume notifications until turn/completed.
Sourcepub fn turn_interrupt(
&mut self,
params: &TurnInterruptParams,
) -> Result<TurnInterruptResponse>
pub fn turn_interrupt( &mut self, params: &TurnInterruptParams, ) -> Result<TurnInterruptResponse>
Interrupt an active turn.
Sourcepub fn thread_archive(
&mut self,
params: &ThreadArchiveParams,
) -> Result<ThreadArchiveResponse>
pub fn thread_archive( &mut self, params: &ThreadArchiveParams, ) -> Result<ThreadArchiveResponse>
Archive a thread.
Sourcepub fn respond<R: Serialize>(&mut self, id: RequestId, result: &R) -> Result<()>
pub fn respond<R: Serialize>(&mut self, id: RequestId, result: &R) -> Result<()>
Respond to a server-to-client request (e.g., approval flow).
When the server sends a ServerMessage::Request, it expects a response.
Use this method with the request’s id and a result payload. For command
approval, pass a CommandExecutionApprovalResponse.
For file change approval, pass a FileChangeApprovalResponse.
Sourcepub fn respond_error(
&mut self,
id: RequestId,
code: i64,
message: &str,
) -> Result<()>
pub fn respond_error( &mut self, id: RequestId, code: i64, message: &str, ) -> Result<()>
Respond to a server-to-client request with an error.
Sourcepub fn next_message(&mut self) -> Result<Option<ServerMessage>>
pub fn next_message(&mut self) -> Result<Option<ServerMessage>>
Read the next incoming server message (notification or server request).
Returns buffered messages first (from notifications that arrived during
a SyncClient::request call), then reads from the wire.
Returns Ok(None) when the app-server closes the connection (EOF).
Sourcepub fn events(&mut self) -> EventIterator<'_> ⓘ
pub fn events(&mut self) -> EventIterator<'_> ⓘ
Return an iterator over ServerMessages.
The iterator yields Result<ServerMessage> and terminates when the
connection closes (EOF). This is the idiomatic way to consume a turn’s
notifications in synchronous code.