Skip to main content

synaptic_callbacks/
lib.rs

1mod composite;
2mod cost_tracking;
3mod metrics;
4mod stdout;
5mod tracing_cb;
6
7pub use composite::CompositeCallback;
8pub use cost_tracking::{
9    default_pricing, CostTrackingCallback, ModelPricing, ModelUsage, UsageSnapshot,
10};
11pub use metrics::{MetricsCallback, MetricsSnapshot, ToolMetrics};
12pub use stdout::StdOutCallbackHandler;
13pub use tracing_cb::TracingCallback;
14
15use std::sync::Arc;
16
17use async_trait::async_trait;
18use synaptic_core::{CallbackHandler, RunEvent, SynapticError};
19use tokio::sync::RwLock;
20
21/// A callback handler that records all received events for later inspection, useful for testing.
22#[derive(Default, Clone)]
23pub struct RecordingCallback {
24    events: Arc<RwLock<Vec<RunEvent>>>,
25}
26
27impl RecordingCallback {
28    pub fn new() -> Self {
29        Self::default()
30    }
31
32    pub async fn events(&self) -> Vec<RunEvent> {
33        self.events.read().await.clone()
34    }
35}
36
37#[async_trait]
38impl CallbackHandler for RecordingCallback {
39    async fn on_event(&self, event: RunEvent) -> Result<(), SynapticError> {
40        self.events.write().await.push(event);
41        Ok(())
42    }
43}
44
45#[cfg(feature = "otel")]
46mod opentelemetry_cb;
47#[cfg(feature = "otel")]
48pub use opentelemetry_cb::OpenTelemetryCallback;