I have two agent, how do i dispatch a specific age...
# ask-ai
c
I have two agent, how do i dispatch a specific agent to my room?
t
Thanks for reaching out, Tuy. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
To dispatch a specific agent to a room, you can use explicit agent dispatch. Here’s how you can do it: 1. Ensure your agent is registered with an
agentName
when setting up the worker options: `opts = WorkerOptions( ... agent_name="test-agent", )`When the
agent_name
is set, the agent will not be automatically dispatched to newly created rooms. 2. Use the
AgentDispatchService
or
AgentDispatchClient
to explicitly dispatch the agent to a room. For example, using the API client:
Copy code
import asyncio
from livekit import api

room_name = "my-room"
agent_name = "test-agent"

async def create_explicit_dispatch():
    lkapi = api.LiveKitAPI()
    dispatch = await lkapi.agent_dispatch.create_dispatch(
        api.CreateAgentDispatchRequest(
            agent_name=agent_name, room=room_name, metadata="my_job_metadata"
        )
    )
    print("created dispatch", dispatch)
3. Alternatively, you can use the
createDispatch
method: `createDispatch( roomName: string, agentName: string, options?: CreateDispatchOptions ): Promise<agentdispatch>`This method creates an explicit dispatch for an agent to join a room and returns the dispatch that was created. Let me know if you need further assistance! Sources: Dispatching agents | LiveKit Docs | AgentDispatchClient | LiveKit JS Server SDK - v2.9.7
c
so when generating token i need to specific the agent name ?
t
Yes, when generating a token, you can specify the agent name to include agent dispatch information. Here’s an example of how to do it:
Copy code
def create_token_with_agent_dispatch() -> str:
    token = (
        AccessToken()
        .with_identity("my_participant")
        .with_grants(VideoGrants(room_join=True, room=room_name))
        .with_room_config(
            RoomConfiguration(
                agents=[
                    RoomAgentDispatch(agent_name="test-agent", metadata="my_metadata")
                ],
            ),
        )
        .to_jwt()
    )
    return token
In this example, the token is configured to dispatch the agent named
test-agent
with some metadata. Replace
test-agent
with your specific agent name. Let me know if you need further clarification! Sources: Dispatching agents | LiveKit Docs
c
can metadata be json
t
It appears that metadata in
RoomAgentDispatch
is typically set as a string, based on available examples. While other parts of LiveKit often use JSON (stringified) for metadata, there is no explicit confirmation that JSON is supported for
RoomAgentDispatch
metadata specifically. If you’d like, you can try stringifying your JSON object and passing it as a string to see if it works. Let me know if you need further assistance! Sources: Authentication | LiveKit Docs
c
what the different between .with_metadata(json.dumps(metadata)) and agents=[ RoomAgentDispatch(agent_name="test-agent", metadata="my_metadata") ],
t
Unfortunately, I couldn't find specific information about the difference between
.with_metadata(json.dumps(metadata))
and
RoomAgentDispatch
metadata. However, it seems that
.with_metadata
is used to set metadata for a participant or token, while
RoomAgentDispatch
metadata is specifically tied to the agent dispatch configuration. If you have a specific use case in mind, feel free to share more details, and I’ll do my best to assist further!