How do i implement a button in react that when pre...
# ask-ai
a
How do i implement a button in react that when pressed instantly stops the ai agent from talking
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.
Thank you for reaching out! To help you better, could you please share a bit more about the AI agent you're using? For instance, is it integrated with LiveKit, or does it work as a separate service? Also, I'd love to understand how the AI agent's speech functionality is set up right now. Your details will really help me assist you more effectively!
a
Yes it is integrated with live kit await session.start( room=ctx.room, agent=Assistant(chat_ctx=initial_ctx), room_input_options=RoomInputOptions( noise_cancellation=noise_cancellation.BVC(), text_enabled=True, audio_enabled=True, ), ) await ctx.connect(auto_subscribe=AutoSubscribe.AUDIO_ONLY) await ctx.wait_for_participant() #Customize welcome message based on whether we have study material if study_material: welcome_msg = WELCOME_MESSAGE_1 else: welcome_msg = WELCOME_MESSAGE_2 await session.generate_reply(instructions=welcome_msg) if name == "__main__": options = agents.WorkerOptions( entrypoint_fnc=entrypoint, initialize_process_timeout=30.0, # Increase from default 10 seconds ) agents.cli.run_app(options)
t
Hi there! To help you implement a button in React that stops the AI agent integrated with LiveKit, here’s a friendly approach you can try: 1. Use RPC to Trigger Agent Stop: Your React frontend can send an RPC message (like "stop_agent") to the agent to gracefully shut it down. This means the agent needs to have an RPC method registered. Here’s an example:
Copy code
// Agent-side code
ctx.room.localParticipant.registerRpcMethod('stop_agent', async () => {
  ctx.shutdown('Stopped by user request');
  return 'Agent stopping';
});
Then, from your React app, you can call this RPC like so:
Copy code
// React-side code
const { room } = useRoomContext();

const stopAgent = () => {
  room.localParticipant.rpc('stop_agent');
};

return <button onClick={stopAgent}>Stop Agent</button>;
2. Another Option: End the Entire Room Session: If your goal is to end the session for all participants, you can disconnect the local participant from the room using this method:
Copy code
const { disconnect } = useConnection();

const endSession = () => {
  disconnect(); // This disconnects the frontend from the room
};
If you’d like, I’m here to help you walk through setting up the React component or the agent-side code step-by-step. Just let me know! Sources: Text and transcriptions | LiveKit Docs | Job lifecycle | LiveKit Docs | Inside a session | LiveKit Docs | Inside a session | LiveKit Docs | Turn detection and interruptions | LiveKit Docs
a
I don’t want to disconnect or remove the agent I just want a button to stop what the agent is currently saying
t
Hi there! To gently pause the AI agent's current speech without disconnecting it, you can simply use the
interrupt()
method on your agent session. This handy method stops any speech that's happening right now and clears any responses waiting in line, all while keeping your agent connected and ready for whatever comes next. Here’s a quick example in Python to get you started:
Copy code
# Stop the agent's current speech
session.interrupt()
And if you’re working with Node.js, it looks like this:
Copy code
import { voice } from '@livekit/agents';

const session = new voice.AgentSession({
  // ... configuration
});

// Stop the agent's current speech
session.interrupt();
Now, if you happen to have a specific speech handle you want to stop, you can do that too! In Python:
Copy code
handle = session.say("Hello world")
handle.interrupt()
And in Node.js:
Copy code
const handle = session.say('Hello world');
handle.interrupt();
Does this sound good? If you'd like, I’d be happy to help you add this feature to your React button—just let me know! Sources: Text and transcriptions | LiveKit Docs | Turn detection and interruptions | LiveKit Docs | Agent speech and audio | LiveKit Docs | Agent speech and audio | LiveKit Docs | VoicePipelineAgent | LiveKit Docs