Skip to main content

Crate ccwire

Crate ccwire 

Source
Expand description

A Rust SDK for Claude Code.

ccwire drives the claude CLI over its stream-json protocol.

  • protocol: the protocol types, pure data and serde, no I/O and no async runtime, so anything that reads or writes stream-json (a transcript viewer, a log analyzer, a process supervisor) can use them.
  • Options: a session’s command line, built without running it.
  • framing: one JSON frame per line, with a bounded line length (a sync reader always; the async reader and writer with runtime).
  • session (feature runtime): a session over any AsyncRead and AsyncWrite (user messages, the frame stream, control round trips, interrupt, the run-end rule), not tied to a child process.
use ccwire::protocol::{Frame, Inbound, Message};

let line = r#"{"type":"result","subtype":"success","num_turns":1,"session_id":"s1"}"#;
let inbound = Inbound::from_line(line).expect("a JSON object");
match inbound.split() {
    Frame::Message(Message::Result(r)) => assert!(r.is_success()),
    other => panic!("unexpected {other:?}"),
}

§Async sessions

Enable ccwire = { version = "0.1", features = ["runtime"] } and run inside a Tokio runtime. The caller owns process startup and cleanup: Options builds argv, and session::Session handles the pipes.

use ccwire::session::Session;

let mut session = Session::new(stdout, stdin);
session.send("Hello!").await?;
while let Some(frame) = session.next().await {
    let frame = frame?;
    println!("{frame:?}");
    if frame.ends_turn() {
        break;
    }
}
session.close().await?;

See the complete CLI example for spawning and reaping a child process.

§Features

FeatureDefaultEnables
runtimeNoTokio-based async framing and sessions
tracingNoDebug logs for unknown frames; warnings for malformed frames

Without features, protocol parsing, synchronous framing, and CLI options work without an async runtime. Checked against TESTED_CLI_VERSION; unknown protocol variants and extra fields are preserved.

§License

Licensed under the Apache License, Version 2.0 (Apache-2.0).

Re-exports§

pub use options::Options;

Modules§

framing
Line-delimited JSON framing: stream-json is one JSON object per line.
options
Options: the claude command line of a session, built and checked without running anything.
protocol
The stream-json protocol of claude -p --input-format stream-json --output-format stream-json, as Rust types.
sessionruntime
A session with the CLI over any reader and writer (feature runtime).

Constants§

TESTED_CLI_VERSION
The Claude Code version the golden corpus was recorded with: the protocol these types are checked against.