Skip to content

Commit 94f9ded

Browse files
committed
fix: rename conversationalState on agent to lifecycleState
1 parent 3b37d23 commit 94f9ded

File tree

1 file changed

+37
-36
lines changed

1 file changed

+37
-36
lines changed

packages/react/src/hooks/useAgent.ts

Lines changed: 37 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,20 @@ import { ConversationInstance } from './useConversationWith';
1414
const DEFAULT_AGENT_CONNECT_TIMEOUT_MILLISECONDS = 20_000;
1515

1616
/** State representing the current status of the agent, whether it is ready for speach, etc */
17-
export type AgentConversationalState = 'unset' | 'initializing' | 'failed' | 'idle' | 'listening' | 'thinking' | 'speaking';
17+
export type AgentLifecycleState = 'unset' | 'initializing' | 'failed' | 'idle' | 'listening' | 'thinking' | 'speaking';
1818

1919
export enum AgentEvent {
2020
CameraChanged = 'cameraChanged',
2121
MicrophoneChanged = 'microphoneChanged',
2222
AttributesChanged = 'attributesChanged',
23-
ConversationalStateChanged = 'conversationalStateChanged',
23+
ConversationalStateChanged = 'lifecycleStateChanged',
2424
}
2525

2626
export type AgentCallbacks = {
2727
[AgentEvent.CameraChanged]: (newTrack: TrackReference<Track.Source.Camera, RemoteParticipant> | null) => void;
2828
[AgentEvent.MicrophoneChanged]: (newTrack: TrackReference<Track.Source.Microphone, RemoteParticipant> | null) => void;
2929
[AgentEvent.AttributesChanged]: (newAttributes: Record<string, string>) => void;
30-
[AgentEvent.ConversationalStateChanged]: (newAgentConversationalState: AgentConversationalState) => void;
30+
[AgentEvent.ConversationalStateChanged]: (newAgentConversationalState: AgentLifecycleState) => void;
3131
};
3232

3333
type AgentInstanceCommon = {
@@ -43,14 +43,14 @@ type AgentInstanceCommon = {
4343
workerParticipant: RemoteParticipant | null;
4444

4545
/** A computed version of the old {@link AgentState} value returned by {@link useVoiceAssistant}
46-
* @deprecated Use conversation.connectionState / agent.conversationalState if at all possible
46+
* @deprecated Use conversation.connectionState / agent.lifecycleState if at all possible
4747
*/
4848
legacyAgentState: LegacyAgentState;
4949
};
5050
};
5151

5252
type AgentStateAvailable = AgentInstanceCommon & {
53-
conversationalState: "listening" | "thinking" | "speaking";
53+
lifecycleState: "listening" | "thinking" | "speaking";
5454
failureReasons: null;
5555

5656
/** Is the agent ready for user interaction? */
@@ -61,7 +61,7 @@ type AgentStateAvailable = AgentInstanceCommon & {
6161
};
6262

6363
type AgentStateUnAvailable = AgentInstanceCommon & {
64-
conversationalState: "unset" | "initializing" | "idle";
64+
lifecycleState: "unset" | "initializing" | "idle";
6565
failureReasons: null;
6666

6767
/** Is the agent ready for user interaction? */
@@ -72,7 +72,7 @@ type AgentStateUnAvailable = AgentInstanceCommon & {
7272
};
7373

7474
type AgentStateFailed = AgentInstanceCommon & {
75-
conversationalState: "failed";
75+
lifecycleState: "failed";
7676
failureReasons: Array<string>;
7777

7878
/** Is the agent ready for user interaction? */
@@ -96,25 +96,25 @@ type AgentActions = {
9696
type AgentState = AgentStateAvailable | AgentStateUnAvailable | AgentStateFailed;
9797
export type AgentInstance = AgentState & AgentActions;
9898

99-
const generateDerivedConversationalStateValues = <ConversationalState extends AgentConversationalState>(conversationalState: ConversationalState) => ({
99+
const generateDerivedLifecycleStateValues = <LifecycleState extends AgentLifecycleState>(lifecycleState: LifecycleState) => ({
100100
isAvailable: (
101-
conversationalState === 'listening' ||
102-
conversationalState === 'thinking' ||
103-
conversationalState === 'speaking'
101+
lifecycleState === 'listening' ||
102+
lifecycleState === 'thinking' ||
103+
lifecycleState === 'speaking'
104104
),
105105
} as {
106-
isAvailable: ConversationalState extends 'listening' | 'thinking' | 'speaking' ? true : false,
106+
isAvailable: LifecycleState extends 'listening' | 'thinking' | 'speaking' ? true : false,
107107
});
108108

109109
const useAgentTimeoutIdStore = create<{
110110
agentTimeoutFailureReason: string | null,
111111
startAgentTimeout: (agentConnectTimeoutMilliseconds?: number) => void;
112112
clearAgentTimeout: () => void;
113-
updateAgentTimeoutConversationalState: (agentConversationalState: AgentConversationalState) => void;
113+
updateAgentTimeoutConversationalState: (agentConversationalState: AgentLifecycleState) => void;
114114
updateAgentTimeoutParticipantExists: (agentParticipantExists: boolean) => void;
115115
subtle: {
116116
agentTimeoutId: ReturnType<typeof setTimeout> | null;
117-
agentConversationalState: AgentConversationalState;
117+
agentConversationalState: AgentLifecycleState;
118118
agentParticipantExists: boolean;
119119
};
120120
}>((set, get) => {
@@ -126,7 +126,7 @@ const useAgentTimeoutIdStore = create<{
126126
return;
127127
}
128128

129-
const { isAvailable } = generateDerivedConversationalStateValues(agentConversationalState);
129+
const { isAvailable } = generateDerivedLifecycleStateValues(agentConversationalState);
130130
if (!isAvailable) {
131131
set((old) => ({ ...old, agentTimeoutFailureReason: 'Agent connected but did not complete initializing.' }));
132132
return;
@@ -172,7 +172,7 @@ const useAgentTimeoutIdStore = create<{
172172
});
173173
},
174174

175-
updateAgentTimeoutConversationalState: (agentConversationalState: AgentConversationalState) => {
175+
updateAgentTimeoutConversationalState: (agentConversationalState: AgentLifecycleState) => {
176176
set((old) => ({ ...old, subtle: { ...old.subtle, agentConversationalState } }));
177177
},
178178
updateAgentTimeoutParticipantExists: (agentParticipantExists: boolean) => {
@@ -300,12 +300,12 @@ export function useAgent(conversation: ConversationStub, _name?: string): AgentI
300300
return agentTimeoutFailureReason ? [ agentTimeoutFailureReason ] : [];
301301
}, [agentTimeoutFailureReason]);
302302

303-
const conversationalState = useMemo(() => {
303+
const lifecycleState = useMemo(() => {
304304
if (failureReasons.length > 0) {
305305
return 'failed';
306306
}
307307

308-
let newConversationalState: AgentConversationalState = 'unset';
308+
let newConversationalState: AgentLifecycleState = 'unset';
309309

310310
if (roomConnectionState !== ConnectionState.Disconnected) {
311311
newConversationalState = 'initializing';
@@ -327,9 +327,10 @@ export function useAgent(conversation: ConversationStub, _name?: string): AgentI
327327
}, [failureReasons, roomConnectionState, localMicTrack, agentParticipant, agentParticipantAttributes]);
328328

329329
useEffect(() => {
330-
emitter.emit(AgentEvent.ConversationalStateChanged, conversationalState);
331-
updateAgentTimeoutConversationalState(conversationalState);
332-
}, [emitter, conversationalState]);
330+
console.log('AGENT TIMEOUT FAILURE REASON:', lifecycleState);
331+
emitter.emit(AgentEvent.ConversationalStateChanged, lifecycleState);
332+
updateAgentTimeoutConversationalState(lifecycleState);
333+
}, [emitter, lifecycleState]);
333334
useEffect(() => {
334335
updateAgentTimeoutParticipantExists(agentParticipant !== null);
335336
}, [agentParticipant]);
@@ -356,12 +357,12 @@ export function useAgent(conversation: ConversationStub, _name?: string): AgentI
356357
case 'connected':
357358
case 'reconnecting':
358359
case 'signalReconnecting':
359-
switch (conversationalState) {
360+
switch (lifecycleState) {
360361
case 'speaking':
361362
case 'listening':
362363
case 'initializing':
363364
case 'thinking':
364-
return conversationalState;
365+
return lifecycleState;
365366

366367
case 'idle':
367368
case 'unset':
@@ -370,7 +371,7 @@ export function useAgent(conversation: ConversationStub, _name?: string): AgentI
370371
return 'disconnected';
371372
}
372373
}
373-
}, [conversation.connectionState, conversationalState]);
374+
}, [conversation.connectionState, lifecycleState]);
374375

375376
const agentState: AgentState = useMemo(() => {
376377
const common: AgentInstanceCommon = {
@@ -386,15 +387,15 @@ export function useAgent(conversation: ConversationStub, _name?: string): AgentI
386387
},
387388
};
388389

389-
switch (conversationalState) {
390+
switch (lifecycleState) {
390391
case 'listening':
391392
case 'thinking':
392393
case 'speaking':
393394
return {
394395
...common,
395396

396-
conversationalState,
397-
...generateDerivedConversationalStateValues(conversationalState),
397+
lifecycleState,
398+
...generateDerivedLifecycleStateValues(lifecycleState),
398399
failureReasons: null,
399400

400401
camera: videoTrack,
@@ -407,8 +408,8 @@ export function useAgent(conversation: ConversationStub, _name?: string): AgentI
407408
return {
408409
...common,
409410

410-
conversationalState,
411-
...generateDerivedConversationalStateValues(conversationalState),
411+
lifecycleState,
412+
...generateDerivedLifecycleStateValues(lifecycleState),
412413
failureReasons: null,
413414

414415
// Clear inner values if no longer connected
@@ -420,8 +421,8 @@ export function useAgent(conversation: ConversationStub, _name?: string): AgentI
420421
return {
421422
...common,
422423

423-
conversationalState: 'failed',
424-
...generateDerivedConversationalStateValues('failed'),
424+
lifecycleState: 'failed',
425+
...generateDerivedLifecycleStateValues('failed'),
425426
failureReasons,
426427

427428
// Clear inner values if no longer connected
@@ -434,20 +435,20 @@ export function useAgent(conversation: ConversationStub, _name?: string): AgentI
434435
emitter,
435436
agentParticipant,
436437

437-
conversationalState,
438+
lifecycleState,
438439
videoTrack,
439440
audioTrack,
440441
]);
441442

442443
const waitUntilAvailable = useCallback(async (signal?: AbortSignal) => {
443-
const { isAvailable } = generateDerivedConversationalStateValues(conversationalState);
444+
const { isAvailable } = generateDerivedLifecycleStateValues(lifecycleState);
444445
if (isAvailable) {
445446
return;
446447
}
447448

448449
return new Promise<void>((resolve, reject) => {
449-
const stateChangedHandler = (state: AgentConversationalState) => {
450-
const { isAvailable } = generateDerivedConversationalStateValues(state);
450+
const stateChangedHandler = (state: AgentLifecycleState) => {
451+
const { isAvailable } = generateDerivedLifecycleStateValues(state);
451452
if (!isAvailable) {
452453
return;
453454
}
@@ -467,7 +468,7 @@ export function useAgent(conversation: ConversationStub, _name?: string): AgentI
467468
emitter.on(AgentEvent.ConversationalStateChanged, stateChangedHandler);
468469
signal?.addEventListener('abort', abortHandler);
469470
});
470-
}, [conversationalState, emitter]);
471+
}, [lifecycleState, emitter]);
471472

472473
const waitUntilCamera = useCallback((signal?: AbortSignal) => {
473474
return new Promise<TrackReference<Track.Source.Camera, RemoteParticipant>>((resolve, reject) => {

0 commit comments

Comments
 (0)