Skip to main content
Version: 2.x.x

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.

useAgentParticipant react native hook
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 the RTCStream.

onStreamDisabled​

  • onStreamDisabled() will be triggered when the agent's audio or video stream is disabled. It receives the RTCStream.

onMediaStatusChanged​

  • onMediaStatusChanged() will be triggered when the agent turns its audio or video on or off.
  • It receives { kind, peerId, newStatus }. kind is the media that changed, peerId is the agent's participant ID, and newStatus is true when it was turned on.

onAgentStateChanged​

  • onAgentStateChanged() will be triggered when the agent's state changes.
  • It receives the new state, one of the Constants.AgentState values: IDLE, LISTENING, THINKING or SPEAKING.

onAgentTranscriptionReceived​

  • onAgentTranscriptionReceived() will be triggered for each line of the agent's transcription.
  • It receives { text, timestamp }. timestamp is in milliseconds since the epoch.

onAgentMetrics​

  • onAgentMetrics() will be triggered when the agent reports its metrics, such as latency and speech.

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 with new MediaStream([webcamStream.track]).toURL().
  • webcamOn and micOn: Boolean, true when the agent's video or audio is on.
  • isActiveSpeaker: Boolean, true when 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 the Constants.AgentState values.
  • isAgent(): always true.
  • getAgentId(): String | null, the ID of the agent.
  • getAgentState(): String | null, the agent's current state, one of the Constants.AgentState values. 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. type is "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 autoConsume is false.
  • stopConsumingMicStreams() and stopConsumingWebcamStreams(): stop receiving the agent's audio or video.
  • These four methods take effect only when the room was created with autoConsume set to false.
  • 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