Skip to main content

stream_query_sync

Function stream_query_sync 

Source
pub fn stream_query_sync<F>(
    claude: &Claude,
    cmd: &QueryCommand,
    handler: F,
) -> Result<CommandOutput>
where F: FnMut(StreamEvent),
Expand description

Blocking mirror of stream_query. Reads NDJSON lines from the child’s stdout on a worker thread, dispatches each parsed event to handler on the caller’s thread, and drains stderr on a separate worker thread so the child can’t deadlock on a full pipe.

Requires both sync and json features.

The handler is invoked on the caller’s thread — no Send bound — so it can capture non-Send state. If a timeout is configured on the Claude client, the child is SIGKILLed and reaped once the deadline passes; partial events already dispatched to the handler are not rolled back.

§Example

use claude_wrapper::{Claude, OutputFormat, QueryCommand};
use claude_wrapper::streaming::{StreamEvent, stream_query_sync};

let claude = Claude::builder().build()?;
let cmd = QueryCommand::new("explain quicksort")
    .output_format(OutputFormat::StreamJson);

stream_query_sync(&claude, &cmd, |event: StreamEvent| {
    if let Some(t) = event.event_type() {
        println!("[{t}] {:?}", event.data);
    }
})?;