Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/client/src/utils/WebRTCConnection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -41,6 +44,7 @@ vi.mock("livekit-client", () => {
DataReceived: "dataReceived",
TrackSubscribed: "trackSubscribed",
ActiveSpeakersChanged: "activeSpeakersChanged",
ParticipantConnected: "participantConnected",
ParticipantDisconnected: "participantDisconnected",
},
Track: {
Expand Down
27 changes: 27 additions & 0 deletions packages/client/src/utils/WebRTCConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void>((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({
Expand Down
Loading