Video SDK useAgentParticipant Hook - React Native
useAgentParticipant Hook
useAgentParticipant is the hook for an AI agent in the room. It takes the agent's participant ID and callback functions as parameters, and returns the agent's properties and methods.
An AI agent joins the room as a participant. Use useAgentParticipant() for an agent and useParticipant() for everyone else. useAgentParticipant() throws an error when it is given the ID of a participant who is not an agent, and useParticipant() throws an error when it is given an agent's ID.
useAgentParticipant example
The participants from useRoom() include the agent. For the agent, isAgent() from useParticipant() returns true.
import { Text, View } from "react-native";
import {
Constants,
useAgentParticipant,
useParticipant,
useRoom,
} from "@videosdk.live/react-native-sdk";
function AgentView({ participantId }) {
function onAgentStateChanged(data) {
console.log("Agent state:", data);
}
function onAgentTranscriptionReceived(data) {
console.log("Agent transcription:", data);
}
const { displayName, getAgentState } = useAgentParticipant(participantId, {
onAgentStateChanged,
onAgentTranscriptionReceived,
});
const isSpeaking = getAgentState() === Constants.AgentState.SPEAKING;
return (
<Text>
{displayName} {isSpeaking ? "is speaking" : ""}
</Text>
);
}
function ParticipantTile({ participantId }) {
const { isAgent, displayName } = useParticipant(participantId);
return isAgent() ? (
<AgentView participantId={participantId} />
) : (
<Text>{displayName}</Text>
);
}
function ParticipantList() {
const { participants } = useRoom();
return (
<View>
{[...participants.keys()].map((participantId) => (
<ParticipantTile key={participantId} participantId={participantId} />
))}
</View>
);
}
Parameters
participantId
- type :
String REQUIRED- The participant ID of the agent.
onStreamEnabled
onStreamEnabled()will be triggered when the agent's audio or video stream is enabled. It receives theRTCStream.
onStreamDisabled
onStreamDisabled()will be triggered when the agent's audio or video stream is disabled. It receives theRTCStream.
onMediaStatusChanged
onMediaStatusChanged()will be triggered when the agent turns its audio or video on or off.- It receives
{ kind, peerId, newStatus }.kindis the media that changed,peerIdis the agent's participant ID, andnewStatusistruewhen it was turned on.
onAgentStateChanged
onAgentStateChanged()will be triggered when the agent's state changes.- It receives the new state, one of the
Constants.AgentStatevalues:IDLE,LISTENING,THINKINGorSPEAKING.
onAgentTranscriptionReceived
onAgentTranscriptionReceived()will be triggered for each line of the agent's transcription.- It receives
{ text, timestamp }.timestampis in milliseconds since the epoch.
onAgentMetrics
onAgentMetrics()will be triggered when the agent reports its metrics, such aslatencyandspeech.
Returns
Properties
- agentParticipant:
AgentParticipant, the object for the agent. - id:
String, the participant ID of the agent. - agentId:
String, the ID of the agent. - displayName:
String, the name of the agent. - webcamStream and micStream:
RTCStream, the agent's video and audio streams. Render video withnew MediaStream([webcamStream.track]).toURL(). - webcamOn and micOn:
Boolean,truewhen the agent's video or audio is on. - isActiveSpeaker:
Boolean,truewhen the agent is speaking. - pinState:
{ cam: Boolean, share: Boolean }, whether the agent is pinned. - metaData:
Object, the additional information the agent joined with. - mode:
String, the agent's mode. - agentState:
String, the agent's current state, one of theConstants.AgentStatevalues. - isAgent(): always
true. - getAgentId():
String | null, the ID of the agent. - getAgentState():
String | null, the agent's current state, one of theConstants.AgentStatevalues. Calling it while rendering re-renders the component when the state changes.
Methods
These methods work the same as the useParticipant() methods with the same names.
- pin(type) and unpin(type): pin or unpin the agent.
typeis"SHARE_AND_CAM","CAM"or"SHARE". - remove(): removes the agent from the room.
- consumeMicStreams() and consumeWebcamStreams(): start receiving the agent's audio or video when
autoConsumeisfalse. - stopConsumingMicStreams() and stopConsumingWebcamStreams(): stop receiving the agent's audio or video.
- These four methods take effect only when the room was created with
autoConsumeset tofalse. - getAudioStats() and getVideoStats(): resolve the statistics of the agent's audio and video streams.
Each of these methods returns a Promise.
Got a Question? Ask us on discord

