Skip to content

Commit b4e750a

Browse files
authored
Add participant identity, name, and metadata to root useAgent() return value (#1246)
1 parent 3a76e9c commit b4e750a

File tree

2 files changed

+75
-4
lines changed

2 files changed

+75
-4
lines changed

.changeset/vast-queens-fold.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+
Add participant identity, name, and metadata to root useAgent() return value

packages/react/src/hooks/useAgent.ts

Lines changed: 70 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
RemoteParticipant,
77
RoomEvent,
88
Track,
9+
Participant,
910
} from 'livekit-client';
1011
import type TypedEventEmitter from 'typed-emitter';
1112
import { EventEmitter } from 'events';
@@ -16,6 +17,7 @@ import { useParticipantTracks } from './useParticipantTracks';
1617
import { useRemoteParticipants } from './useRemoteParticipants';
1718
import { UseSessionReturn } from './useSession';
1819
import { useMaybeSessionContext } from '../context';
20+
import { useParticipantInfo } from './useParticipantInfo';
1921

2022
// FIXME: make this 10 seconds once room dispatch booting info is discoverable
2123
const DEFAULT_AGENT_CONNECT_TIMEOUT_MILLISECONDS = 20_000;
@@ -63,7 +65,7 @@ export type AgentCallbacks = {
6365

6466
type AgentStateCommon = {
6567
// FIXME: maybe add some sort of schema to this?
66-
attributes: Record<string, string>;
68+
attributes: Participant['attributes'];
6769

6870
internal: {
6971
emitter: TypedEventEmitter<AgentCallbacks>;
@@ -77,6 +79,11 @@ type AgentStateAvailable = AgentStateCommon & {
7779
state: 'listening' | 'thinking' | 'speaking';
7880
failureReasons: null;
7981

82+
/** The agent's assigned identity, coming from the JWT token. */
83+
identity: Participant['identity'];
84+
name: Participant['name'];
85+
metadata: Participant['metadata'];
86+
8087
/** Is the agent connected to the client? */
8188
isConnected: true;
8289

@@ -102,6 +109,11 @@ type AgentStatePreConnectBuffering = AgentStateCommon & {
102109
state: 'pre-connect-buffering';
103110
failureReasons: null;
104111

112+
/** The client's assigned identity, coming from the JWT token. */
113+
identity: Participant['identity'];
114+
name: Participant['name'];
115+
metadata: Participant['metadata'];
116+
105117
/** Is the agent connected to the client? */
106118
isConnected: false;
107119

@@ -127,6 +139,11 @@ type AgentStateUnAvailable = AgentStateCommon & {
127139
state: 'initializing' | 'idle';
128140
failureReasons: null;
129141

142+
/** The client's assigned identity, coming from the JWT token. */
143+
identity: Participant['identity'];
144+
name: Participant['name'];
145+
metadata: Participant['metadata'];
146+
130147
/** Is the agent connected to the client? */
131148
isConnected: false;
132149

@@ -152,6 +169,11 @@ type AgentStateConnecting = AgentStateCommon & {
152169
state: 'connecting';
153170
failureReasons: null;
154171

172+
/** The client's assigned identity, coming from the JWT token. */
173+
identity: undefined;
174+
name: undefined;
175+
metadata: undefined;
176+
155177
/** Is the agent connected to the client? */
156178
isConnected: false;
157179

@@ -177,6 +199,11 @@ type AgentStateDisconnected = AgentStateCommon & {
177199
state: 'disconnected';
178200
failureReasons: null;
179201

202+
/** The client's assigned identity, coming from the JWT token. */
203+
identity: undefined;
204+
name: undefined;
205+
metadata: undefined;
206+
180207
/** Is the agent connected to the client? */
181208
isConnected: false;
182209

@@ -202,6 +229,11 @@ type AgentStateFailed = AgentStateCommon & {
202229
state: 'failed';
203230
failureReasons: Array<string>;
204231

232+
/** The client's assigned identity, coming from the JWT token. */
233+
identity: undefined;
234+
name: undefined;
235+
metadata: undefined;
236+
205237
/** Is the agent connected to the client? */
206238
isConnected: false;
207239

@@ -517,8 +549,8 @@ export function useAgent(session?: SessionStub): UseAgentReturn {
517549

518550
// 1. Listen for agent participant attribute changes
519551
const [agentParticipantAttributes, setAgentParticipantAttributes] = React.useState<
520-
Record<string, string>
521-
>({});
552+
Participant['attributes']
553+
>(agentParticipant?.attributes ?? {});
522554
React.useEffect(() => {
523555
if (!agentParticipant) {
524556
return;
@@ -710,6 +742,12 @@ export function useAgent(session?: SessionStub): UseAgentReturn {
710742
};
711743
}, [isSessionDisconnected, agentConnectTimeoutMilliseconds]);
712744

745+
const {
746+
identity: agentParticipantIdentity,
747+
name: agentParticipantName,
748+
metadata: agentParticipantMetadata,
749+
} = useParticipantInfo({ participant: agentParticipant ?? undefined });
750+
713751
const agentState: AgentStateCases = React.useMemo(() => {
714752
const common: AgentStateCommon = {
715753
attributes: agentParticipantAttributes,
@@ -725,6 +763,9 @@ export function useAgent(session?: SessionStub): UseAgentReturn {
725763
case 'disconnected':
726764
return {
727765
...common,
766+
identity: undefined,
767+
name: undefined,
768+
metadata: undefined,
728769

729770
state,
730771
...generateDerivedStateValues(state),
@@ -738,6 +779,9 @@ export function useAgent(session?: SessionStub): UseAgentReturn {
738779
case 'connecting':
739780
return {
740781
...common,
782+
identity: undefined,
783+
name: undefined,
784+
metadata: undefined,
741785

742786
state,
743787
...generateDerivedStateValues(state),
@@ -752,6 +796,9 @@ export function useAgent(session?: SessionStub): UseAgentReturn {
752796
case 'idle':
753797
return {
754798
...common,
799+
identity: agentParticipantIdentity!,
800+
name: agentParticipantName,
801+
metadata: agentParticipantMetadata,
755802

756803
state,
757804
...generateDerivedStateValues(state),
@@ -764,6 +811,9 @@ export function useAgent(session?: SessionStub): UseAgentReturn {
764811
case 'pre-connect-buffering':
765812
return {
766813
...common,
814+
identity: agentParticipantIdentity!,
815+
name: agentParticipantName,
816+
metadata: agentParticipantMetadata,
767817

768818
state,
769819
...generateDerivedStateValues(state),
@@ -778,6 +828,9 @@ export function useAgent(session?: SessionStub): UseAgentReturn {
778828
case 'speaking':
779829
return {
780830
...common,
831+
identity: agentParticipantIdentity!,
832+
name: agentParticipantName,
833+
metadata: agentParticipantMetadata,
781834

782835
state,
783836
...generateDerivedStateValues(state),
@@ -790,6 +843,9 @@ export function useAgent(session?: SessionStub): UseAgentReturn {
790843
case 'failed':
791844
return {
792845
...common,
846+
identity: undefined,
847+
name: undefined,
848+
metadata: undefined,
793849

794850
state: 'failed',
795851
...generateDerivedStateValues('failed'),
@@ -800,7 +856,17 @@ export function useAgent(session?: SessionStub): UseAgentReturn {
800856
microphoneTrack: undefined,
801857
};
802858
}
803-
}, [agentParticipantAttributes, emitter, agentParticipant, state, videoTrack, audioTrack]);
859+
}, [
860+
agentParticipantIdentity,
861+
agentParticipantName,
862+
agentParticipantMetadata,
863+
agentParticipantAttributes,
864+
emitter,
865+
agentParticipant,
866+
state,
867+
videoTrack,
868+
audioTrack,
869+
]);
804870

805871
const { waitUntilConnected, waitUntilCouldBeListening, waitUntilFinished } =
806872
useAgentWaitUntilDerivedStates(emitter, state);

0 commit comments

Comments
 (0)