Skip to main content

Module callback

Module callback 

Source
Expand description

Message callback type for real-time message notifications.

The MessageCallback type is the primary extension point for consumers who need to observe messages as they stream — for logging, UI updates, persistence, metrics, or any other side effect — without interfering with the primary stream consumption.

§Design

Callbacks are Arc<dyn Fn(Message) -> Pin<Box<dyn Future<Output = ()> + Send>>>. This makes them:

  • Cloneable — can be shared across tasks via Arc::clone.
  • Thread-safeSend + Sync bounds on the inner Fn.
  • Async-capable — the callback can await I/O (e.g., writing to a database).
  • Zero-overhead for sync work — use sync_callback to wrap a plain closure.

§Example

use gemini_cli_sdk::callback::{sync_callback, tracing_callback};

// Simple logging callback
let cb = sync_callback(|msg| {
    println!("received: {:?}", msg.session_id());
});

// Built-in tracing callback
let _trace_cb = tracing_callback();

Functions§

sync_callback
Wrap a synchronous closure in a MessageCallback.
tracing_callback
Create a MessageCallback that emits structured tracing events for every message variant.

Type Aliases§

MessageCallback
Callback invoked for each message received from the agent.