@@ -10,7 +10,6 @@ import {
1010import type TypedEventEmitter from 'typed-emitter' ;
1111import { EventEmitter } from 'events' ;
1212import * as React from 'react' ;
13- import { create } from 'zustand' ;
1413import { ParticipantAgentAttributes , TrackReference } from '@livekit/components-core' ;
1514
1615import { useParticipantTracks } from './useParticipantTracks' ;
@@ -172,91 +171,73 @@ const generateDerivedStateValues = <State extends AgentState>(state: State) =>
172171 isAvailable : State extends 'listening' | 'thinking' | 'speaking' ? true : false ;
173172 } ;
174173
175- const useAgentTimeoutIdStore = create < {
174+ /** Internal hook used by useSession to store global agent state */
175+ export const useAgentTimeoutIdStore = ( ) : {
176176 agentTimeoutFailureReason : string | null ;
177177 startAgentTimeout : ( agentConnectTimeoutMilliseconds ?: number ) => void ;
178178 clearAgentTimeout : ( ) => void ;
179179 updateAgentTimeoutState : ( agentState : AgentState ) => void ;
180180 updateAgentTimeoutParticipantExists : ( agentParticipantExists : boolean ) => void ;
181- internal : {
182- agentTimeoutId : ReturnType < typeof setTimeout > | null ;
183- agentState : AgentState ;
184- agentParticipantExists : boolean ;
185- } ;
186- } > ( ( set , get ) => {
181+ } => {
182+ const [ agentTimeoutFailureReason , setAgentTimeoutFailureReason ] = React . useState < string | null > (
183+ null ,
184+ ) ;
185+ const [ agentTimeoutId , setAgentTimeoutId ] = React . useState < ReturnType < typeof setTimeout > | null > (
186+ null ,
187+ ) ;
188+
189+ const agentStateRef = React . useRef < AgentState > ( 'connecting' ) ;
190+ const agentParticipantExistsRef = React . useRef ( false ) ;
191+
187192 const startAgentConnectedTimeout = ( agentConnectTimeoutMilliseconds ?: number ) => {
188193 return setTimeout ( ( ) => {
189- const {
190- internal : { agentState, agentParticipantExists } ,
191- } = get ( ) ;
192- if ( ! agentParticipantExists ) {
193- set ( ( old ) => ( { ...old , agentTimeoutFailureReason : 'Agent did not join the room.' } ) ) ;
194+ if ( ! agentParticipantExistsRef . current ) {
195+ setAgentTimeoutFailureReason ( 'Agent did not join the room.' ) ;
194196 return ;
195197 }
196198
197- const { isAvailable } = generateDerivedStateValues ( agentState ) ;
199+ const { isAvailable } = generateDerivedStateValues ( agentStateRef . current ) ;
198200 if ( ! isAvailable ) {
199- set ( ( old ) => ( {
200- ...old ,
201- agentTimeoutFailureReason : 'Agent connected but did not complete initializing.' ,
202- } ) ) ;
201+ setAgentTimeoutFailureReason ( 'Agent connected but did not complete initializing.' ) ;
203202 return ;
204203 }
205204 } , agentConnectTimeoutMilliseconds ?? DEFAULT_AGENT_CONNECT_TIMEOUT_MILLISECONDS ) ;
206205 } ;
207206
208207 return {
209- agentTimeoutFailureReason : null ,
210- startAgentTimeout : ( agentConnectTimeoutMilliseconds ?: number ) => {
211- set ( ( old ) => {
212- if ( old . internal . agentTimeoutId ) {
213- clearTimeout ( old . internal . agentTimeoutId ) ;
214- }
215-
216- return {
217- ...old ,
218- agentTimeoutFailureReason : null ,
219- internal : {
220- ...old . internal ,
221- agentTimeoutId : startAgentConnectedTimeout ( agentConnectTimeoutMilliseconds ) ,
222- agentState : 'connecting' ,
223- agentParticipantExists : false ,
224- } ,
225- } ;
226- } ) ;
227- } ,
228- clearAgentTimeout : ( ) => {
229- set ( ( old ) => {
230- if ( old . internal . agentTimeoutId ) {
231- clearTimeout ( old . internal . agentTimeoutId ) ;
208+ agentTimeoutFailureReason,
209+ startAgentTimeout : React . useCallback (
210+ ( agentConnectTimeoutMilliseconds ?: number ) => {
211+ if ( agentTimeoutId ) {
212+ clearTimeout ( agentTimeoutId ) ;
232213 }
233- return {
234- ...old ,
235- agentTimeoutFailureReason : null ,
236- internal : {
237- ...old . internal ,
238- agentTimeoutId : null ,
239- agentState : 'connecting' ,
240- agentParticipantExists : false ,
241- } ,
242- } ;
243- } ) ;
244- } ,
245214
246- updateAgentTimeoutState : ( agentState : AgentState ) => {
247- set ( ( old ) => ( { ...old , internal : { ...old . internal , agentState : agentState } } ) ) ;
248- } ,
249- updateAgentTimeoutParticipantExists : ( agentParticipantExists : boolean ) => {
250- set ( ( old ) => ( { ...old , internal : { ...old . internal , agentParticipantExists } } ) ) ;
251- } ,
215+ setAgentTimeoutFailureReason ( null ) ;
216+ setAgentTimeoutId ( startAgentConnectedTimeout ( agentConnectTimeoutMilliseconds ) ) ;
217+ agentStateRef . current = 'connecting' ;
218+ agentParticipantExistsRef . current = false ;
219+ } ,
220+ [ agentTimeoutId ] ,
221+ ) ,
222+ clearAgentTimeout : React . useCallback ( ( ) => {
223+ if ( agentTimeoutId ) {
224+ clearTimeout ( agentTimeoutId ) ;
225+ }
252226
253- internal : {
254- agentTimeoutId : null ,
255- agentState : 'connecting' ,
256- agentParticipantExists : false ,
257- } ,
227+ setAgentTimeoutFailureReason ( null ) ;
228+ setAgentTimeoutId ( null ) ;
229+ agentStateRef . current = 'connecting' ;
230+ agentParticipantExistsRef . current = false ;
231+ } , [ agentTimeoutId ] ) ,
232+
233+ updateAgentTimeoutState : React . useCallback ( ( agentState : AgentState ) => {
234+ agentStateRef . current = agentState ;
235+ } , [ ] ) ,
236+ updateAgentTimeoutParticipantExists : React . useCallback ( ( agentParticipantExists : boolean ) => {
237+ agentParticipantExistsRef . current = agentParticipantExists ;
238+ } , [ ] ) ,
258239 } ;
259- } ) ;
240+ } ;
260241
261242type SessionStub = Pick < UseSessionReturn , 'connectionState' | 'room' | 'internal' > ;
262243
@@ -275,7 +256,15 @@ export function useAgent(session?: SessionStub): UseAgentReturn {
275256
276257 const {
277258 room,
278- internal : { agentConnectTimeoutMilliseconds } ,
259+ internal : {
260+ agentConnectTimeoutMilliseconds,
261+
262+ agentTimeoutFailureReason,
263+ startAgentTimeout,
264+ clearAgentTimeout,
265+ updateAgentTimeoutState,
266+ updateAgentTimeoutParticipantExists,
267+ } ,
279268 } = session ;
280269
281270 const emitter = React . useMemo ( ( ) => new EventEmitter ( ) as TypedEventEmitter < AgentCallbacks > , [ ] ) ;
@@ -398,14 +387,6 @@ export function useAgent(session?: SessionStub): UseAgentReturn {
398387 } ;
399388 } , [ room . localParticipant ] ) ;
400389
401- const {
402- agentTimeoutFailureReason,
403- startAgentTimeout,
404- clearAgentTimeout,
405- updateAgentTimeoutState,
406- updateAgentTimeoutParticipantExists,
407- } = useAgentTimeoutIdStore ( ) ;
408-
409390 const failureReasons = React . useMemo ( ( ) => {
410391 return agentTimeoutFailureReason ? [ agentTimeoutFailureReason ] : [ ] ;
411392 } , [ agentTimeoutFailureReason ] ) ;
0 commit comments