pub struct SessionTracer { /* private fields */ }Expand description
Session tracer for recording traces.
Provides a high-level API for recording session traces with timesteps, events, and messages. Supports real-time persistence and hook callbacks.
§Example
ⓘ
use synth_ai_core::tracing::{SessionTracer, LibsqlTraceStorage, TracingEvent, LMCAISEvent};
use std::sync::Arc;
let storage = Arc::new(LibsqlTraceStorage::new_memory().await?);
let tracer = SessionTracer::new(storage);
let session_id = tracer.start_session(None, Default::default()).await?;
tracer.start_timestep("step-1", Some(1), Default::default()).await?;
tracer.record_event(TracingEvent::Cais(LMCAISEvent { ... })).await?;
tracer.end_timestep().await?;
let trace = tracer.end_session(true).await?;Implementations§
Source§impl SessionTracer
impl SessionTracer
Sourcepub fn new(storage: Arc<dyn TraceStorage>) -> SessionTracer
pub fn new(storage: Arc<dyn TraceStorage>) -> SessionTracer
Create a new session tracer with a storage backend.
Sourcepub fn with_hooks(
storage: Arc<dyn TraceStorage>,
hooks: HookManager,
) -> SessionTracer
pub fn with_hooks( storage: Arc<dyn TraceStorage>, hooks: HookManager, ) -> SessionTracer
Create a new session tracer with custom hooks.
Sourcepub fn set_auto_save(&mut self, auto_save: bool)
pub fn set_auto_save(&mut self, auto_save: bool)
Set whether to auto-save events to storage.
Sourcepub async fn register_hook(
&self,
event: HookEvent,
callback: Arc<dyn Fn(&HookContext) + Send + Sync>,
priority: i32,
)
pub async fn register_hook( &self, event: HookEvent, callback: Arc<dyn Fn(&HookContext) + Send + Sync>, priority: i32, )
Register a hook callback.
Sourcepub async fn start_session(
&self,
session_id: Option<&str>,
metadata: HashMap<String, Value>,
) -> Result<String, TracingError>
pub async fn start_session( &self, session_id: Option<&str>, metadata: HashMap<String, Value>, ) -> Result<String, TracingError>
Sourcepub async fn end_session(
&self,
save: bool,
) -> Result<SessionTrace, TracingError>
pub async fn end_session( &self, save: bool, ) -> Result<SessionTrace, TracingError>
Sourcepub async fn current_session_id(&self) -> Option<String>
pub async fn current_session_id(&self) -> Option<String>
Get the current session ID (if any).
Sourcepub async fn query(
&self,
sql: &str,
params: QueryParams,
) -> Result<Vec<Value>, TracingError>
pub async fn query( &self, sql: &str, params: QueryParams, ) -> Result<Vec<Value>, TracingError>
Execute a raw SQL query against the underlying storage.
Sourcepub async fn start_timestep(
&self,
step_id: &str,
turn_number: Option<i32>,
metadata: HashMap<String, Value>,
) -> Result<(), TracingError>
pub async fn start_timestep( &self, step_id: &str, turn_number: Option<i32>, metadata: HashMap<String, Value>, ) -> Result<(), TracingError>
Start a new timestep.
§Arguments
step_id- Unique step identifier.turn_number- Optional conversation turn number.metadata- Step-level metadata.
Sourcepub async fn end_timestep(&self) -> Result<(), TracingError>
pub async fn end_timestep(&self) -> Result<(), TracingError>
End the current timestep.
Sourcepub async fn current_step_id(&self) -> Option<String>
pub async fn current_step_id(&self) -> Option<String>
Get the current step ID (if any).
Sourcepub async fn record_event(
&self,
event: TracingEvent,
) -> Result<Option<i64>, TracingError>
pub async fn record_event( &self, event: TracingEvent, ) -> Result<Option<i64>, TracingError>
Sourcepub async fn record_message(
&self,
content: MessageContent,
message_type: &str,
metadata: HashMap<String, Value>,
) -> Result<Option<i64>, TracingError>
pub async fn record_message( &self, content: MessageContent, message_type: &str, metadata: HashMap<String, Value>, ) -> Result<Option<i64>, TracingError>
Sourcepub async fn record_outcome_reward(
&self,
reward: OutcomeReward,
) -> Result<Option<i64>, TracingError>
pub async fn record_outcome_reward( &self, reward: OutcomeReward, ) -> Result<Option<i64>, TracingError>
Record an outcome (session-level) reward.
Sourcepub async fn record_event_reward(
&self,
event_id: i64,
reward: EventReward,
) -> Result<Option<i64>, TracingError>
pub async fn record_event_reward( &self, event_id: i64, reward: EventReward, ) -> Result<Option<i64>, TracingError>
Record an event-level reward.
Sourcepub async fn get_session(
&self,
session_id: &str,
) -> Result<Option<SessionTrace>, TracingError>
pub async fn get_session( &self, session_id: &str, ) -> Result<Option<SessionTrace>, TracingError>
Get a session trace by ID from storage.
Sourcepub async fn delete_session(
&self,
session_id: &str,
) -> Result<bool, TracingError>
pub async fn delete_session( &self, session_id: &str, ) -> Result<bool, TracingError>
Delete a session from storage.
Auto Trait Implementations§
impl !Freeze for SessionTracer
impl !RefUnwindSafe for SessionTracer
impl Send for SessionTracer
impl Sync for SessionTracer
impl Unpin for SessionTracer
impl !UnwindSafe for SessionTracer
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
Wrap the input message
T in a tonic::Request