How can i check on my react frontend that AI Agent...
# ask-ai
w
How can i check on my react frontend that AI Agent is in the room or not? using
livekit-client
sdk
t
Thanks for reaching out, Rahul. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Hi there! To help you check if an AI Agent is in the room using the
livekit-client
SDK in your React frontend, a great way is to use the
useVoiceAssistant
hook from the
@livekit/components-react
package. This handy hook gives you access to the agent participant object along with its current state. Here’s a simple example of how you can set it up:
Copy code
import { useVoiceAssistant } from '@livekit/components-react';

function MyComponent() {
  const { agent } = useVoiceAssistant();
  const isAgentInRoom = !!agent; // true if agent is present
  return Agent is {isAgentInRoom ? "in" : "not in"} the room.;
}
Now, if you want to see whether the agent is available for interaction, you can check the
state
property from the hook. Here’s how you might do that:
Copy code
import { useVoiceAssistant } from '@livekit/components-react';

function isAgentAvailable(agentState) {
  return agentState === 'listening' || agentState === 'thinking' || agentState === 'speaking';
}

function MyComponent() {
  const { state: agentState, agent } = useVoiceAssistant();
  const isAgentAvailableNow = isAgentAvailable(agentState);

  return Agent is currently {agentState}.;
}
This way, you can easily keep track of the agent’s presence and whether it’s ready to interact with you. If you have any questions or want to dive deeper, feel free to ask—I’m here to help! Sources: packages/react/src/hooks/useVoiceAssistant.ts | Web and mobile frontends | LiveKit Docs | complex-agents/shopify-voice-shopper/shopify-voice-frontend/components/NoAg...