pub struct SessionRecorder { /* private fields */ }Expand description
Records every AgentEvent into a structured tree of Sessions and
LoopRecords.
Call on_event for every event emitted on the agent’s
tx channel, then flush before shutdown or saving.
§Session grouping
Sessions are keyed by session_id. Every AgentStart event that carries a
session_id the recorder has not seen before opens a new Session; all
subsequent loops with the same session_id are appended to that session.
The recorder never rotates sessions on its own. If you want a new session
to start after a period of inactivity, call
BasicAgent::check_and_rotate (or
BasicAgent::new_session) before the next
prompt. The next AgentStart will carry the new session_id and the recorder
will open a fresh Session automatically, with
SessionFormation::InactivityTimeout or SessionFormation::FirstLoop
as the recorded reason.
§Example
use phi_core::session::{SessionRecorder, SessionRecorderConfig};
use phi_core::AgentEvent;
let mut recorder = SessionRecorder::new(SessionRecorderConfig::default());
// Feed events as they arrive:
// recorder.on_event(event);
recorder.flush();Implementations§
Source§impl SessionRecorder
impl SessionRecorder
Sourcepub fn new(config: SessionRecorderConfig) -> Self
pub fn new(config: SessionRecorderConfig) -> Self
Create a new recorder with the given configuration.
Sourcepub fn on_event(&mut self, event: AgentEvent)
pub fn on_event(&mut self, event: AgentEvent)
Feed one event into the recorder.
Must be called for every event emitted on the agent’s tx channel.
Sourcepub fn flush(&mut self)
pub fn flush(&mut self)
Finalize all open LoopRecords (status → LoopStatus::Aborted) and
move them into their sessions.
Call before saving or on process shutdown.
Sourcepub fn checkpoint(&mut self) -> usize
pub fn checkpoint(&mut self) -> usize
Promote sessions that have no remaining open loops to the completed list, without aborting any running loops.
A session is eligible when every loop belonging to it has already received
an AgentEnd event (i.e. it has no entry in
the internal open-loops map). Sessions that still have active loops are left
in place.
This is intended for periodic checkpointing in production: save finished
sessions to disk while leaving in-flight agent runs untouched. In contrast,
flush first aborts all open loops and then promotes
everything.
Returns the number of sessions that were promoted.
Sourcepub fn drain_completed(&mut self) -> Vec<Session>
pub fn drain_completed(&mut self) -> Vec<Session>
Drain all completed sessions out of the recorder (consuming them).
Useful for periodic checkpointing. Call flush first
if you want to include in-progress sessions, or checkpoint
to drain only fully-finished sessions without aborting active loops.
Sourcepub fn sessions(&self) -> impl Iterator<Item = &Session>
pub fn sessions(&self) -> impl Iterator<Item = &Session>
All sessions known to this recorder (completed and in-progress).
Sourcepub fn get_session(&self, session_id: &str) -> Option<&Session>
pub fn get_session(&self, session_id: &str) -> Option<&Session>
Look up a session by session_id.
Sourcepub fn current_loop(&self, loop_id: &str) -> Option<&LoopRecord>
pub fn current_loop(&self, loop_id: &str) -> Option<&LoopRecord>
Look up an in-progress LoopRecord by loop_id.