feat(agents): add warm transfer task#1458
Conversation
| private setResult(result: WarmTransferResult | Error): void { | ||
| if (this.done) { | ||
| return; | ||
| } | ||
|
|
||
| if (this._humanAgentSession) { | ||
| this._humanAgentSession.shutdown({ drain: false }); | ||
| this._humanAgentSession = null; | ||
| } | ||
|
|
||
| if (this._holdAudioHandle) { | ||
| this._holdAudioHandle.stop(); | ||
| this._holdAudioHandle = null; | ||
| } | ||
| void this._backgroundAudio.close().catch((error) => { | ||
| this._logger.warn({ error }, 'failed to close background audio'); | ||
| }); | ||
|
|
||
| this.setIoEnabled(true); | ||
| this.complete(result); | ||
| } |
There was a problem hiding this comment.
🔴 Human agent Room never disconnected, leaking a WebSocket connection on every transfer
In setResult, the _humanAgentSession is shut down but _humanAgentRoom is never disconnected. AgentSession.shutdown() calls closeImplInner which closes the RoomIO (agents/src/voice/agent_session.ts:1301) but RoomIO.close() (agents/src/voice/room_io/room_io.ts:555) does not call room.disconnect(). The only place in the codebase that calls room.disconnect() is agents/src/ipc/job_proc_lazy_main.ts:176 for the main job room. This means every completed WarmTransferTask leaves a connected Room object (with its underlying WebSocket to the LiveKit server) that is never cleaned up, accumulating leaked connections over successive transfers.
| private setResult(result: WarmTransferResult | Error): void { | |
| if (this.done) { | |
| return; | |
| } | |
| if (this._humanAgentSession) { | |
| this._humanAgentSession.shutdown({ drain: false }); | |
| this._humanAgentSession = null; | |
| } | |
| if (this._holdAudioHandle) { | |
| this._holdAudioHandle.stop(); | |
| this._holdAudioHandle = null; | |
| } | |
| void this._backgroundAudio.close().catch((error) => { | |
| this._logger.warn({ error }, 'failed to close background audio'); | |
| }); | |
| this.setIoEnabled(true); | |
| this.complete(result); | |
| } | |
| private setResult(result: WarmTransferResult | Error): void { | |
| if (this.done) { | |
| return; | |
| } | |
| if (this._humanAgentSession) { | |
| this._humanAgentSession.shutdown({ drain: false }); | |
| this._humanAgentSession = null; | |
| } | |
| if (this._humanAgentRoom) { | |
| this._humanAgentRoom.off(RoomEvent.Disconnected, this.onHumanAgentRoomClose); | |
| void this._humanAgentRoom.disconnect().catch((error) => { | |
| this._logger.warn({ error }, 'failed to disconnect human agent room'); | |
| }); | |
| this._humanAgentRoom = null; | |
| } | |
| if (this._holdAudioHandle) { | |
| this._holdAudioHandle.stop(); | |
| this._holdAudioHandle = null; | |
| } | |
| void this._backgroundAudio.close().catch((error) => { | |
| this._logger.warn({ error }, 'failed to close background audio'); | |
| }); | |
| this.setIoEnabled(true); | |
| this.complete(result); | |
| } |
Was this helpful? React with 👍 or 👎 to provide feedback.
| const sip = new SipClient(jobCtx.info.url); | ||
| await sip.createSipParticipant( | ||
| this._sipTrunkId ?? '', | ||
| this._sipCallTo, | ||
| humanAgentRoomName, | ||
| { | ||
| participantIdentity: this._humanAgentIdentity, | ||
| waitUntilAnswered: true, | ||
| fromNumber: this._sipNumber || undefined, | ||
| headers: this._sipHeaders, | ||
| }, | ||
| this._sipConnection, | ||
| ); | ||
|
|
||
| this._humanAgentRoom = room; | ||
| return humanAgentSession; |
There was a problem hiding this comment.
🔴 Room and AgentSession leaked when SIP call creation fails after room connect
In dialHumanAgent(), the Room is connected (line 357) and humanAgentSession is started (line 379-387) before createSipParticipant is called (line 390). If createSipParticipant throws, the method exits without assigning either to instance fields (this._humanAgentRoom at line 403 and the return at line 404 are never reached). The caller in onEnter catches the error and calls setResult, but setResult checks this._humanAgentSession (which is null) and this._humanAgentRoom (also null), so neither the session nor the room is cleaned up. The connected Room and running AgentSession become unreachable leaked resources.
Prompt for agents
In dialHumanAgent(), resources (the Room and AgentSession) are created before the SIP call and only assigned to instance fields after it succeeds. If createSipParticipant throws, these resources leak.
Approach: Assign this._humanAgentRoom = room and this._humanAgentSession = humanAgentSession immediately after they are created/started (before the createSipParticipant call), rather than at the end of the method. This way, if createSipParticipant fails and the error propagates to onEnter's catch block which calls setResult(), the cleanup code in setResult can properly shut down the session and disconnect the room.
Alternatively, wrap the SIP call in a try/catch inside dialHumanAgent that cleans up the room and session on failure before rethrowing.
Was this helpful? React with 👍 or 👎 to provide feedback.
🦋 Changeset detectedLatest commit: 8a74fe3 The changes in this PR will be included in the next version bump. This PR includes changesets to release 31 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
Summary
WarmTransferTaskworkflow for SIP-based human handoffs.Validation
pnpm --filter @livekit/agents buildpnpm --filter @livekit/agents build:typespnpm --filter @livekit/agents lint(passes with existing warnings)pnpm --filter @livekit/agents api:check(blocked by existing API Extractorexport * aslimitation indist/index.d.ts)