bodhi-realtime-agent / SessionManager
Class: SessionManager
Defined in: src/core/session-manager.ts:34
Manages the session state machine and resumption handle. Publishes state-change events to the EventBus and fires lifecycle hooks. Also buffers client messages during disconnected states (RECONNECTING/TRANSFERRING).
Constructors
Constructor
new SessionManager(
config,eventBus,hooks,postSession?,managed?):SessionManager
Defined in: src/core/session-manager.ts:57
Parameters
config
eventBus
hooks
postSession?
SessionPostProcessing
Optional post-session pipeline. When absent, close behaves exactly as before.
managed?
boolean = false
True when a VoiceSession owns this manager. reset() then refuses once a close has been claimed or the state is CLOSED, because the session's history and post-session work finalize on session.close.
Returns
SessionManager
Properties
initialAgent
readonlyinitialAgent:string
Defined in: src/core/session-manager.ts:55
sessionId
readonlysessionId:string
Defined in: src/core/session-manager.ts:53
userId
readonlyuserId:string
Defined in: src/core/session-manager.ts:54
Accessors
isActive
Get Signature
get isActive():
boolean
Defined in: src/core/session-manager.ts:82
Returns
boolean
isDisconnected
Get Signature
get isDisconnected():
boolean
Defined in: src/core/session-manager.ts:86
Returns
boolean
resumptionHandle
Get Signature
get resumptionHandle():
string|null
Defined in: src/core/session-manager.ts:94
Returns
string | null
startedAtMs
Get Signature
get startedAtMs():
number|null
Defined in: src/core/session-manager.ts:74
Millisecond timestamp of first ACTIVE, or null if never activated.
Returns
number | null
state
Get Signature
get state():
SessionState
Defined in: src/core/session-manager.ts:78
Returns
Methods
bufferMessage()
bufferMessage(
message):void
Defined in: src/core/session-manager.ts:298
Parameters
message
Returns
void
clearResumptionHandle()
clearResumptionHandle():
void
Defined in: src/core/session-manager.ts:294
Clear the cached resumption handle. Called when the transport reports a non-resumable session update — keeping a stale handle would let a later reconnect-with-state attempt a resume that loses data.
Returns
void
closeWithReason()
closeWithReason(
reason):Promise<void>
Defined in: src/core/session-manager.ts:231
The single reason-carrying entry point to CLOSED. Idempotent: the first call claims close (sets the guard, records the reason); re-entrant or duplicate calls — including a raced reconnect/transfer-fail path — return the same promise, so session.close fires exactly once with the caller's reason.
Phases: (1) claim, (2) run pre-close finalizers (awaited, all-settled), (3) transitionTo CLOSED (fires onSessionEnd + publishes session.close), (4) dispatch the post-session pipeline (drain mode awaits its report).
Fast path (no finalizers): phases 3–4 run synchronously, preserving legacy close timing (dispatch returns a handle synchronously).
Parameters
reason
SessionEndReason
Returns
Promise<void>
drainBufferedMessages()
drainBufferedMessages():
ClientMessage[]
Defined in: src/core/session-manager.ts:302
Returns
registerPreCloseFinalizer()
registerPreCloseFinalizer(
fn):void
Defined in: src/core/session-manager.ts:205
Register a finalizer to run (awaited, all-settled) before session.close publishes — transcript flush, turn finalize, snapshot-capture of soon-to-be-disposed state. A throwing finalizer is logged, never blocks close. Must be registered before close.
Parameters
fn
() => void | Promise<void>
Returns
void
registerSnapshotBuilder()
registerSnapshotBuilder(
build):void
Defined in: src/core/session-manager.ts:214
Register the per-session snapshot builder dispatched in phase 4 of close. Register once, before the session goes active. No-op effect unless a post-session pipeline was provided to the constructor.
Parameters
build
PostSessionSnapshotBuilder
Returns
void
reset()
reset():
void
Defined in: src/core/session-manager.ts:113
Return to CREATED with no resumption handle and no buffered messages, so a closed manager can run another CONNECTING → ACTIVE → CLOSED cycle. The close guard is cleared as well, so the next closeWithReason() publishes its own session.close. startedAtMs keeps the first activation time, and registered finalizers and the snapshot builder stay registered.
Throws SessionError while a close is still in flight (from the claim until its promise settles, which includes a drained post-session report still pending after the CLOSED transition): resetting then would let two finalizations overlap. On a manager a VoiceSession owns, it also throws once a close has been claimed or the state is CLOSED, since that session is finalized; use recoverUpstream() under upstreamLossPolicy: 'hold' to redial without closing, or construct a new VoiceSession.
Returns
void
transitionTo()
transitionTo(
newState):void
Defined in: src/core/session-manager.ts:135
Parameters
newState
Returns
void
updateResumptionHandle()
updateResumptionHandle(
handle):void
Defined in: src/core/session-manager.ts:283
Parameters
handle
string
Returns
void