Skip to main content

SessionTracer

Struct SessionTracer 

Source
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

Source

pub fn new(storage: Arc<dyn TraceStorage>) -> SessionTracer

Create a new session tracer with a storage backend.

Source

pub fn with_hooks( storage: Arc<dyn TraceStorage>, hooks: HookManager, ) -> SessionTracer

Create a new session tracer with custom hooks.

Source

pub fn set_auto_save(&mut self, auto_save: bool)

Set whether to auto-save events to storage.

Source

pub async fn register_hook( &self, event: HookEvent, callback: Arc<dyn Fn(&HookContext) + Send + Sync>, priority: i32, )

Register a hook callback.

Source

pub async fn start_session( &self, session_id: Option<&str>, metadata: HashMap<String, Value>, ) -> Result<String, TracingError>

Start a new session.

§Arguments
  • session_id - Optional session ID. If None, a UUID will be generated.
  • metadata - Session-level metadata.
§Returns

The session ID.

Source

pub async fn end_session( &self, save: bool, ) -> Result<SessionTrace, TracingError>

End the current session.

§Arguments
  • save - Whether to save the session to storage (ignored if auto_save is true).
§Returns

The completed session trace.

Source

pub async fn current_session_id(&self) -> Option<String>

Get the current session ID (if any).

Source

pub async fn query( &self, sql: &str, params: QueryParams, ) -> Result<Vec<Value>, TracingError>

Execute a raw SQL query against the underlying storage.

Source

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.
Source

pub async fn end_timestep(&self) -> Result<(), TracingError>

End the current timestep.

Source

pub async fn current_step_id(&self) -> Option<String>

Get the current step ID (if any).

Source

pub async fn record_event( &self, event: TracingEvent, ) -> Result<Option<i64>, TracingError>

Record an event.

§Arguments
  • event - The event to record.
§Returns

The database ID of the event (if auto_save is enabled).

Source

pub async fn record_message( &self, content: MessageContent, message_type: &str, metadata: HashMap<String, Value>, ) -> Result<Option<i64>, TracingError>

Record a message.

§Arguments
  • content - Message content.
  • message_type - Type of message (user, assistant, system, tool_use, tool_result).
  • metadata - Message metadata.
§Returns

The database ID of the message (if auto_save is enabled).

Source

pub async fn record_outcome_reward( &self, reward: OutcomeReward, ) -> Result<Option<i64>, TracingError>

Record an outcome (session-level) reward.

Source

pub async fn record_event_reward( &self, event_id: i64, reward: EventReward, ) -> Result<Option<i64>, TracingError>

Record an event-level reward.

Source

pub async fn get_session( &self, session_id: &str, ) -> Result<Option<SessionTrace>, TracingError>

Get a session trace by ID from storage.

Source

pub async fn delete_session( &self, session_id: &str, ) -> Result<bool, TracingError>

Delete a session from storage.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

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 more
Source§

impl<T> IntoRequest<T> for T

Source§

fn into_request(self) -> Request<T>

Wrap the input message T in a tonic::Request
Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<A, B, T> HttpServerConnExec<A, B> for T
where B: Body,