diff --git a/packages/client/src/utils/WebRTCConnection.test.ts b/packages/client/src/utils/WebRTCConnection.test.ts index b90b62f2..78f82d07 100644 --- a/packages/client/src/utils/WebRTCConnection.test.ts +++ b/packages/client/src/utils/WebRTCConnection.test.ts @@ -29,6 +29,9 @@ vi.mock("livekit-client", () => { off: vi.fn(), localParticipant: mockLocalParticipant, name: "conv_test123", + remoteParticipants: new Map([ + ["agent-test", { identity: "agent-test" }], + ]), }; return { @@ -41,6 +44,7 @@ vi.mock("livekit-client", () => { DataReceived: "dataReceived", TrackSubscribed: "trackSubscribed", ActiveSpeakersChanged: "activeSpeakersChanged", + ParticipantConnected: "participantConnected", ParticipantDisconnected: "participantDisconnected", }, Track: { diff --git a/packages/client/src/utils/WebRTCConnection.ts b/packages/client/src/utils/WebRTCConnection.ts index c85f22a4..d51d15ff 100644 --- a/packages/client/src/utils/WebRTCConnection.ts +++ b/packages/client/src/utils/WebRTCConnection.ts @@ -288,6 +288,33 @@ export class WebRTCConnection extends BaseConnection { room.name.match(/(conv_[a-zA-Z0-9]+)/)?.[0] || room.name; } + // Wait for the server-side agent to join the room before sending + // initiation data. Without this, publishData can fire before the + // agent has connected and the SFU silently drops the message. + await new Promise((resolve, reject) => { + const timeoutId = setTimeout(() => { + room.off(RoomEvent.ParticipantConnected, onParticipant); + reject(new Error("Timeout waiting for agent to join the room")); + }, 30_000); + + for (const p of room.remoteParticipants.values()) { + if (p.identity.includes("agent")) { + clearTimeout(timeoutId); + resolve(); + return; + } + } + + const onParticipant = (participant: RemoteParticipant) => { + if (participant.identity.includes("agent")) { + clearTimeout(timeoutId); + room.off(RoomEvent.ParticipantConnected, onParticipant); + resolve(); + } + }; + room.on(RoomEvent.ParticipantConnected, onParticipant); + }); + const overridesEvent = constructOverrides(config); connection.debug({