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-safe —
Send + Syncbounds on the innerFn. - Async-capable — the callback can await I/O (e.g., writing to a database).
- Zero-overhead for sync work — use
sync_callbackto 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
MessageCallbackthat emits structuredtracingevents for every message variant.
Type Aliases§
- Message
Callback - Callback invoked for each message received from the agent.