Skip to content
Merged
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
5 changes: 5 additions & 0 deletions .changeset/spicy-heads-smoke.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@livekit/components-react': patch
---

Add explicit failure when agent disconnects from the room
43 changes: 41 additions & 2 deletions packages/react/src/hooks/useAgent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,7 @@ export function useAgent(session?: SessionStub): UseAgentReturn {
emitter.emit(AgentEvent.MicrophoneChanged, audioTrack);
}, [emitter, audioTrack]);

// Listen for room connection state updates
const [roomConnectionState, setRoomConnectionState] = React.useState(room.state);
React.useEffect(() => {
const handleConnectionStateChanged = (connectionState: ConnectionState) => {
Expand All @@ -354,6 +355,37 @@ export function useAgent(session?: SessionStub): UseAgentReturn {
};
}, [room]);

// If the agent participant disconnects in the middle of a conversation unexpectedly, mark that as an explicit failure
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trying to rethink the disambiguation here, IMO this is fine for single agent use case (no "handoff", (re)joining, etc.)

const [agentDisconnectedFailureReason, setAgentDisconnectedFailureReason] = React.useState<
string | null
>(null);
React.useEffect(() => {
if (!agentParticipant) {
return;
}

const onParticipantDisconnect = (participant: RemoteParticipant) => {
if (participant.identity !== agentParticipant?.identity) {
return;
}
setAgentDisconnectedFailureReason('Agent left the room unexpectedly.');
};

room.on(RoomEvent.ParticipantDisconnected, onParticipantDisconnect);

return () => {
room.off(RoomEvent.ParticipantDisconnected, onParticipantDisconnect);
};
}, [agentParticipant, room]);

React.useEffect(() => {
if (roomConnectionState !== ConnectionState.Disconnected) {
return;
}
// Clear the agent disconnect failure state when the room disconnects
setAgentDisconnectedFailureReason(null);
}, [roomConnectionState]);

const [localMicTrack, setLocalMicTrack] = React.useState<LocalTrackPublication | null>(
() => room.localParticipant.getTrackPublication(Track.Source.Microphone) ?? null,
);
Expand Down Expand Up @@ -386,8 +418,15 @@ export function useAgent(session?: SessionStub): UseAgentReturn {
}, [room.localParticipant]);

const failureReasons = React.useMemo(() => {
return agentTimeoutFailureReason ? [agentTimeoutFailureReason] : [];
}, [agentTimeoutFailureReason]);
const reasons = [];
if (agentTimeoutFailureReason) {
reasons.push(agentTimeoutFailureReason);
}
if (agentDisconnectedFailureReason) {
reasons.push(agentDisconnectedFailureReason);
}
return reasons;
}, [agentTimeoutFailureReason, agentDisconnectedFailureReason]);

const [state, isBufferingSpeech] = React.useMemo(() => {
if (failureReasons.length > 0) {
Expand Down