Member Junction
    Preparing search index...

    Server-side lifecycle manager for AIAgentSession records (not the media/socket transport — that arrives in P5). Stateless with respect to the caller: every method takes the request's contextUser and request-scoped IMetadataProvider so all entity operations run under the correct identity/provider and never pin the process-global provider.

    The only in-memory state it holds is a small per-session last-heartbeat-write timestamp cache used purely to coalesce Heartbeat writes (see heartbeatLastWrite for why it's bounded), plus the injected RealtimeClientSessionService used by finalizeObservabilityRuns; it carries no user or provider state across calls.

    Responsibilities:

    • Create — authorize (CanRun), optionally mint a Conversation, persist an Active session.
    • Close — terminal transition; disconnect channels; idempotent.
    • Heartbeat — coalesced LastActiveAt bump; reactivates Idle → Active.
    • MarkIdle — Active → Idle transition when the last channel goes quiet.

    P5 will extend CloseSession to also abort any in-flight AIAgentRun (via its cancellation token) and call ClientToolRequestManager.ClearSession(...); those resources don't exist yet at this layer, so close here only reconciles the durable record + channel rows.

    Index

    Constructors

    Methods

    • Close a session (terminal). Sets Status = 'Closed' + ClosedAt = now + CloseReason, then disconnects all of its channel rows. Idempotent: closing an already-Closed session is a no-op that returns true (and never overwrites the original CloseReason). A missing/unloadable session returns false.

      Parameters

      • agentSessionID: string
      • contextUser: UserInfo
      • provider: IMetadataProvider
      • closeReason: SessionCloseReason = 'Explicit'

        Why the session is being closed. Defaults to 'Explicit' so existing call sites (the user-initiated close mutations) stamp the common case without modification; background callers (janitor sweeps, shutdown drain, error teardowns) must pass their reason explicitly. NULL rows remain only for legacy data closed before the column existed.

      • OptionalpreloadedChannels: MJAIAgentSessionChannelEntity[]

      Returns Promise<boolean>

      P5 adds in-flight-run abort + ClientToolRequestManager.ClearSession here.

    • Closes a session that was created for one bridged agent (a bot in a meeting room) once that agent's bridge has ended, whatever ended it: the agent was stopped, the meeting was ended, everyone left, the model session was lost. Without this the session stays Active until the janitor's staleness sweep closes it, 15 minutes or more later.

      The close is CloseSession, the one the janitor runs, with the bridge's reason: Explicit for a stop, an ended meeting or an emptied room (HostEnded), otherwise the bridge's own (Error, Janitor, Shutdown). A session already closed (by the janitor, or by its user) keeps its reason.

      Never throws: it runs as the bridge's end-of-session bookkeeping. A failure is logged and leaves the session to the janitor, as before.

      Parameters

      • agentSessionID: string

        The session created for the bridged agent.

      • reason: BridgeDisconnectReason

        Why the bridge ended.

      • contextUser: UserInfo

        The user the session was created for.

      • provider: IMetadataProvider

        The metadata provider the session was created through.

      Returns Promise<boolean>

      true when the session is (or already was) closed.

    • Closes a session that was created for one agent's time in a room when that agent failed to start: its model session could not open, the bot's token could not be minted, the bridge did not start. The agent never joined, so no end-of-session hook will close the session; without this it stays Active until the janitor's staleness sweep.

      The close is CloseSession with reason Error. A session already closed keeps its reason.

      Never throws: it runs while the caller handles the failed start, whose error is the one the caller reports. A failure is logged and leaves the session to the janitor.

      Parameters

      • agentSessionID: string

        The session created for the agent.

      • contextUser: UserInfo

        The user the session was created for.

      • provider: IMetadataProvider

        The metadata provider the session was created through.

      Returns Promise<boolean>

      true when the session is (or already was) closed.

    • Record activity on a session. Coalesces persisted writes so at most one LastActiveAt update lands per session per HEARTBEAT_MIN_WRITE_INTERVAL_MS; intervening calls return true without touching the DB. Reactivates an Idle session to Active. A Closed session is not reactivated (returns false).

      Parameters

      Returns Promise<boolean>