bodhi-realtime-agent / BackgroundAgent
Interface: BackgroundAgent
Defined in: src/agent/background-agent.ts:137
Lifecycle hooks for a user-defined background producer.
Lifecycle:
onStart(ctx)fires exactly ONCE — on the firstsession.connectedenvelope (NOT on construction; deferred until the live transport is ready, so the first publish lands on a live wire).onAgentTransfer(event)fires on everyagent.transfer_completed.onReconnect()fires on everysession.reconnected(after the initialonStart). Implement only if you need to resync external state.onStop(reason)fires onsession.close_requested/transport.closed, or onagent.transfer_completedwhencancelOnTransfer === true.
Throws inside any hook are caught by the host actor and logged — a misbehaving agent does not bring down the session (per the 'background-agents': resume supervision policy).
Properties
cancelOnTransfer?
readonlyoptionalcancelOnTransfer?:boolean
Defined in: src/agent/background-agent.ts:147
If true, ctx.signal is aborted and onStop('transfer') is invoked when an agent.transfer_completed envelope is observed. Default false — reminder-style producers usually survive transfer; flow- specific producers opt in.
name
readonlyname:string
Defined in: src/agent/background-agent.ts:139
Stable name; used for logging and as the publish correlationId prefix.
Methods
onAgentTransfer()?
optionalonAgentTransfer(event):void
Defined in: src/agent/background-agent.ts:161
Called after agent.transfer_completed. Invoked regardless of cancelOnTransfer (so the agent can observe the transition); when cancelOnTransfer === true, onStop('transfer') runs after this hook returns.
Parameters
event
fromAgent
string
toAgent
string
Returns
void
onReconnect()?
optionalonReconnect():void
Defined in: src/agent/background-agent.ts:169
Called on every transport reconnect (after the initial onStart). Optional — most agents don't need it because the host preserves their state across reconnect. Implement only when you need to resync state with external systems.
Returns
void
onStart()
onStart(
ctx):void|Promise<void>
Defined in: src/agent/background-agent.ts:150
Called exactly once, on the first session.connected envelope.
Parameters
ctx
Returns
void | Promise<void>
onStop()?
optionalonStop(reason):void|Promise<void>
Defined in: src/agent/background-agent.ts:153
Called when the agent is removed, the session closes, or cancelOnTransfer triggers.
Parameters
reason
string
Returns
void | Promise<void>