Skip to main content

Module client_async

Module client_async 

Source
Expand description

Asynchronous multi-turn client for the Codex app-server.

Spawns codex app-server --listen stdio:// and communicates over newline-delimited JSON-RPC. The connection stays open for multiple turns until explicitly shut down.

§Lifecycle

  1. Create a client with AsyncClient::start (spawns and initializes the app-server)
  2. Call AsyncClient::thread_start to create a conversation session
  3. Call AsyncClient::turn_start to send user input
  4. Consume AsyncClient::next_message to stream notifications
  5. Handle approval requests via AsyncClient::respond
  6. Repeat steps 3-5 for follow-up turns
  7. The client kills the app-server on Drop

§Example

use codex_codes::{AsyncClient, ThreadStartParams, TurnStartParams, UserInput, ServerMessage};

let mut client = AsyncClient::start().await?;
let thread = client.thread_start(&ThreadStartParams::default()).await?;

client.turn_start(&TurnStartParams {
    thread_id: thread.thread_id().to_string(),
    input: vec![UserInput::Text { text: "Hello!".into() }],
    model: None,
    reasoning_effort: None,
    sandbox_policy: None,
}).await?;

while let Some(msg) = client.next_message().await? {
    match msg {
        ServerMessage::Notification { method, params } => {
            if method == "turn/completed" { break; }
        }
        ServerMessage::Request { id, method, .. } => {
            client.respond(id, &serde_json::json!({"decision": "accept"})).await?;
        }
    }
}

Structs§

AsyncClient
Asynchronous multi-turn client for the Codex app-server.
EventStream
Async stream of ServerMessages from an AsyncClient.