Hi <@U08CKJH7C12> , can you please help me with t...
# agents
w
Hi @refined-appointment-81829 , can you please help me with this issue? Same AI Agent joining my room multiple times, leading to overlapping AI voices, how can i make sure to restrict it to single AI Agent?? Here is my logic to dispatch-event by also making sure that not more then single agent joins the room :-
Copy code
const agentDispatchClient = new AgentDispatchClient(
					env.LIVEKIT_URL,
					env.LIVEKIT_API_KEY,
					env.LIVEKIT_API_SECRET
				);

				// Check if there are already active dispatches for this room
				const existingDispatches = await agentDispatchClient.listDispatch(
					roomName
				);
				console.log(
					`Found ${existingDispatches.length} existing dispatches in ${roomName}`
				);

				// Filter for active dispatches (not completed or failed)
				// Note: Using any type temporarily until we confirm the exact properties
				const activeDispatches = existingDispatches.filter(
					(dispatch: any) =>
						dispatch.status === "pending" || dispatch.status === "running"
				);

				if (activeDispatches.length > 0) {
					console.log(
						`Agent already dispatched to room ${roomName}, skipping duplicate dispatch`
					);
					return {
						message: "Agent already dispatched to this room",
						existingDispatches: activeDispatches.length,
						skipped: true,
					};
				}

				// create a dispatch request for an agent named "local-agent" to join the room
				const dispatch = await agentDispatchClient.createDispatch(
					roomName,
					"local-agent",
					{
						metadata: '{"user_id": "12345"}',
					}
				);
				console.log("created dispatch", dispatch);

				const dispatches = await agentDispatchClient.listDispatch(roomName);
				console.log(`there are ${dispatches.length} dispatches in ${roomName}`);
I asked Livekit AI as well, it said to keep a unique name to agent to prevent this, but i've already done that as you can see here :-
Copy code
const dispatch = await agentDispatchClient.createDispatch(
					roomName,
					"local-agent",
					{
						metadata: '{"user_id": "12345"}',
					}
				);
f
hey @wonderful-account-72949 , had same issue once and I did something like this. I basically have a queue in a scheduler class into which i put in command to control the agent flow. so if i put a command to dispatch the agent, i call the dispatch API from the sdk and change the state of the agent to DISPATCHED in my class. next time if another dispatch command comes i check if an agent has already been dispatched from this state. The state becomes connected and then to disconnected as the agent joins and leaves the room ( again some commands are send to this scheduler class). This is not a livekit API only solution. but if u keep a extra wrapper class to manage it yourself, i think u can prevent the situation. Maybe there is a better way that i don't know of.
r
As @fancy-painting-20947 says if you dispatch multiple times then multiple agents can land in that room. Another thing I like to do is make sure I use unique room names each time.
w
Thanks @fancy-painting-20947 @refined-appointment-81829