Skip to content

Commit 42a0e82

Browse files
authored
Ensure that if an agent connects after a timeout failure, the useSession status doesn't get stuck in "failed" (#1236)
1 parent ae19ec9 commit 42a0e82

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

.changeset/true-moose-cheer.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@livekit/components-react': patch
3+
---
4+
5+
Ensure that if an agent connects after a timeout failure, the status doesn't get stuck in "failed"

packages/react/src/hooks/useAgent.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,7 @@ export const useAgentTimeoutIdStore = (): {
280280
agentTimeoutFailureReason: string | null;
281281
startAgentTimeout: (agentConnectTimeoutMilliseconds?: number) => void;
282282
clearAgentTimeout: () => void;
283+
clearAgentTimeoutFailureReason: () => void;
283284
updateAgentTimeoutState: (agentState: AgentState) => void;
284285
updateAgentTimeoutParticipantExists: (agentParticipantExists: boolean) => void;
285286
} => {
@@ -333,6 +334,9 @@ export const useAgentTimeoutIdStore = (): {
333334
agentStateRef.current = 'connecting';
334335
agentParticipantExistsRef.current = false;
335336
}, [agentTimeoutId]),
337+
clearAgentTimeoutFailureReason: React.useCallback(() => {
338+
setAgentTimeoutFailureReason(null);
339+
}, []),
336340

337341
updateAgentTimeoutState: React.useCallback((agentState: AgentState) => {
338342
agentStateRef.current = agentState;
@@ -479,6 +483,7 @@ export function useAgent(session?: SessionStub): UseAgentReturn {
479483
agentTimeoutFailureReason,
480484
startAgentTimeout,
481485
clearAgentTimeout,
486+
clearAgentTimeoutFailureReason,
482487
updateAgentTimeoutState,
483488
updateAgentTimeoutParticipantExists,
484489
},
@@ -572,6 +577,15 @@ export function useAgent(session?: SessionStub): UseAgentReturn {
572577
};
573578
}, [room]);
574579

580+
// When the agent participant connects, reset the timeout failure state
581+
React.useEffect(() => {
582+
if (!agentParticipant) {
583+
return;
584+
}
585+
586+
clearAgentTimeoutFailureReason();
587+
}, [agentParticipant]);
588+
575589
// If the agent participant disconnects in the middle of a conversation unexpectedly, mark that as an explicit failure
576590
const [agentDisconnectedFailureReason, setAgentDisconnectedFailureReason] = React.useState<
577591
string | null

packages/react/src/hooks/useSession.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ type SessionStateCommon = {
7777
agentTimeoutFailureReason: string | null;
7878
startAgentTimeout: (agentConnectTimeoutMilliseconds?: number) => void;
7979
clearAgentTimeout: () => void;
80+
clearAgentTimeoutFailureReason: () => void;
8081
updateAgentTimeoutState: (agentState: AgentState) => void;
8182
updateAgentTimeoutParticipantExists: (agentParticipantExists: boolean) => void;
8283
};
@@ -391,6 +392,7 @@ export function useSession(
391392
agentTimeoutFailureReason,
392393
startAgentTimeout,
393394
clearAgentTimeout,
395+
clearAgentTimeoutFailureReason,
394396
updateAgentTimeoutState,
395397
updateAgentTimeoutParticipantExists,
396398
} = useAgentTimeoutIdStore();
@@ -404,6 +406,7 @@ export function useSession(
404406
agentTimeoutFailureReason,
405407
startAgentTimeout,
406408
clearAgentTimeout,
409+
clearAgentTimeoutFailureReason,
407410
updateAgentTimeoutState,
408411
updateAgentTimeoutParticipantExists,
409412
}),
@@ -414,6 +417,7 @@ export function useSession(
414417
agentTimeoutFailureReason,
415418
startAgentTimeout,
416419
clearAgentTimeout,
420+
clearAgentTimeoutFailureReason,
417421
updateAgentTimeoutState,
418422
updateAgentTimeoutParticipantExists,
419423
],

0 commit comments

Comments
 (0)