How did i interrupt an agent in react
# ask-ai
a
How did i interrupt an agent in react
t
Thanks for reaching out, David. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Hi there! If you’re looking to interrupt an agent in React using LiveKit, you can easily do this with the
session.interrupt()
method from the LiveKit Agents framework. This handy method lets you stop the agent’s speech right away, no matter if interruptions are turned on or off for the current speech. Here’s how you can interrupt an agent: • Using `session.interrupt()`: Just call the
interrupt()
method on the agent session to immediately pause the agent’s speech. For example:
Copy code
const handle = session.say("Hello world");
// Later, to interrupt:
session.interrupt();
• Using `SpeechHandle.interrupt()`: If you have a reference to the
SpeechHandle
(which you get from
say()
or
generate_reply()
), you can interrupt that specific speech directly. Like this:
Copy code
const handle = session.say("Hello world");
handle.interrupt();
When you interrupt, the agent will stop speaking right away and shorten the conversation history to only include what was said before the interruption. If you’d like to dive deeper, feel free to check out the Turns and Interruptions documentation. I hope this helps you get things working smoothly! Sources: agents/src/voice/agent_activity.ts | Docs::Agents | Turn detection and interruptions | LiveKit Docs
a
What react component can i call to do this. From the frontend
t
Hi there! While LiveKit doesn’t offer a dedicated React component specifically for interrupting an agent from the frontend, you can definitely achieve this by using the
session.interrupt()
method on the agent session object. I’m here to help guide you through it! Here’s a friendly way to implement an Interruption Button in React: 1. Make sure you have access to the Agent Session: Your React component will need the session object, which you can usually pass down through context or state management. 2. Create a Custom Button:
Copy code
function InterruptButton({ session }) {
  const handleClick = () => {
    session.interrupt(); // This will interrupt the agent programmatically
  };

  return (
    <button onClick={handleClick} aria-label="Interrupt Agent">
      Interrupt
    </button>
  );
}
Bonus tip: You can use the
useVoiceAssistant
hook from
@livekit/components-react
to check the agent’s current state (like if they’re "speaking") and show or enable the button accordingly:
Copy code
const { state } = useVoiceAssistant();
const isSpeaking = state === 'speaking';

return (
  <button onClick={handleClick} disabled={!isSpeaking}>
    {isSpeaking ? 'Interrupt' : 'Agent Not Speaking'}
  </button>
);
If you’d like, I’m more than happy to walk you through integrating this into your frontend or help you manage the session object in React. Just let me know! Sources: Docs::Agents | Turn detection and interruptions | LiveKit Docs | Web and mobile frontends | LiveKit Docs | Docs::Agents | complex-agents/drive-thru/frontend/components/livekit/agent-control-bar/age...
a
How do i pass down the session
t
I'm happy to help you pass down the session object in React for LiveKit! You can easily do this by using the
LiveKitRoom
component, which conveniently provides the session (room) context to all child components via React Context. This way, you don’t have to worry about manually passing props down the component tree. Here’s a simple way to pass down the session: 1. Wrap Your Application with `LiveKitRoom`: This component handles connecting to the LiveKit server and sets up a context provider for you. 2. Access the Session with Hooks: In any child component, you can use the
useRoomContext()
hook from
@livekit/components-react
to get the session object. Let me show you an example:
Copy code
import { LiveKitRoom } from '@livekit/components-react';
import { useRoomContext } from '@livekit/components-react';

function App() {
  return (
    <LiveKitRoom serverUrl="wss://your-livekit-server.com" token="your-access-token" connect={true}>
      <RoomView />
    </LiveKitRoom>
  );
}

function RoomView() {
  const room = useRoomContext(); // Here you can access the session (room) object

  return <div>Connected to room: {room?.name}</div>;
}
If you’d like, I’m here to guide you further with setting up the
LiveKitRoom
component or managing the session object. Just let me know how I can assist! Sources: docs/index.html | components/session-view.tsx | Workflows | LiveKit Docs
a
So can i do room.interrupt()
t
Hi there! Just to clarify,
room.interrupt()
isn’t the right way to interrupt an agent in LiveKit. Instead, you’ll want to use
session.interrupt()
, which you can find on the
AgentSession
object. This method gently stops the agent’s current speech and updates the conversation context for you. If you have the session object handy, you can simply call:
Copy code
session.interrupt();
If you'd like, I’d be happy to help you make sure the session object is accessible within your React setup. Just let me know! Sources: Agent speech and audio | LiveKit Docs | Turn detection and interruptions | LiveKit Docs | pipeline-tts/short_replies_only.py | Docs::Agents
a
This is my current livekit set up and i want to handle session interrupt in the <chat> component <LiveKitRoom token={token} serverUrl={import.meta.env.VITE_LIVEKIT_URL} connect={true} video={false} audio={true} > <RoomAudioRenderer /> <InactivityMonitor inactivityTimeout={5 * 60 * 1000} // 5 minutes warningTime={60 * 1000} // 1 minute warning lastActivityTimestamp={lastActivityTimestamp} /> <div className="main-content-container"> {/* Left Section - Wrapper with AudioVisualizer and Controls */} <div className="left-section"> <div className="wrapper"> <AudioVisualizer /> <div className="control-bar-wrapper"> <VoiceAssistantControlBar /> </div> </div> </div> {/* Center Section - Chat */} <div className="center-section"> <Chat onActivity={handleActivity} /> </div> {/* Right Section - PDF Viewer */} {pdfUrl && ( <div className="right-section"> <div className="pdf-viewer"> <iframe src={pdfUrl} title="PDF Viewer" width="100%" height="100%" /> </div> </div> )} </div> </LiveKitRoom>
t
To handle
session.interrupt()
in your
<Chat>
component, it’s important to make sure the session object is accessible. The
LiveKitRoom
component provides the room context to all child components, but the session object itself isn’t directly available through that context. A helpful approach is to manage the session object separately and then pass it down to your
<Chat>
component. Here’s an example to guide you:
Copy code
// In your main component
const session = createAgentSession(); // Initialize the session object

<LiveKitRoom
  token={token}
  serverUrl={import.meta.env.VITE_LIVEKIT_URL}
  connect={true}
  video={false}
  audio={true}
>
  <Chat session={session} />
</LiveKitRoom>
And then, inside your
<Chat>
component:
Copy code
function Chat({ session }) {
  const handleInterrupt = () => {
    session.interrupt(); // Call session.interrupt()
  };

  return (
    <div>
      Chat component
      <button onClick={handleInterrupt}>Interrupt Agent</button>
    </div>
  );
}
If you’d like, I’m here to help you further with managing the session object or integrating it smoothly into your setup. Just let me know! Sources: src/components/LiveKitRoom.tsx | complex-agents/nova-sonic/nova-sonic-form-agent/components/session-view.tsx