wonderful-account-72949
09/12/2025, 9:28 AMconst 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 :-
const dispatch = await agentDispatchClient.createDispatch(
roomName,
"local-agent",
{
metadata: '{"user_id": "12345"}',
}
);fancy-painting-20947
09/12/2025, 10:42 AMrefined-appointment-81829
09/12/2025, 12:01 PMwonderful-account-72949
09/16/2025, 1:57 PM