Hi :wave: Has anyone been able to update an exist...
# agents
b
Hi 👋 Has anyone been able to update an existing agent’s name via the cli (obviously, not supported yet via web)? Is that supported yet? I tried the below but never worked: lk agent update CA_****** --name "new-agent-name”
l
You can't specify the agent name through the LK CLI, you can only specify it in the agent, https://docs.livekit.io/agents/build/sessions/#rtc-session-decorator, then redeploy.
Just to say, ^ applies to how you specify the agent_name in session options. If you are still using WorkerOptions, then change your agent_name there.
b
@lively-winter-88668 thanks for the response! I will be attempting those changes tomorrow and revert with any question I might have.
What I was trying to achieve with the naming (we are using the plan that allows for spinning up 2 agents) is to dedicate one agent for dev and the other for prod environment.
With that, we could easily test new updates on dev without having to mess around with prod.
With what you explained above, it doesn't seem achievable.
l
Typically we recommend creating separate projects for dev and prod, so you can test in dev before deploying to prod. I know this isn't as straightforward as it could be, and it's an area we want to improve in the future, but let me know if you have any issues with the separate project approach.
👍 1
b
Hi @lively-winter-88668. I hope you are well. So I had time to start switch to dispatching agent explicitly 2 days agao. However, I started having audio issue - simply put, agent joins the room but I cannot hear anything they say. I created a gist for how I am generating token & dispatching the agent, agent.py code , and react-mobile app code: https://gist.github.com/D-sense/e77fe10748d50a477c686d5bd1b76fac
I might be wrong, but my suspicion lies within the agent.py code. I had upgraded to the latest option too but issue persists.
l
I'm not so sure it's your agent code, I took the
agent.py
you provided at https://gist.github.com/D-sense/e77fe10748d50a477c686d5bd1b76fac, modified it slightly to remove the agent name, and run it in isolation... I was able to hear the agent. Initially I had suspected
tts=None,  # RealtimeModel has built-in TTS
was somehow confusing things, but I didn't need to delete it. I tried running your agent with
python agent.py console
and also using the agents playground with
python agent.py dev
then https://agents-playground.livekit.io/. I recommend reproducing what I did, so you know this isn't an agent code issue. Then double-checking the agent is definitely joining your room (a frequent error is users seeing an agent join their room, but it is actually a previously deployed agent - I'm not saying that's the issue here, I'm just saying it's a common issue 🙂 ) I remember we have an outstanding issue on React native: https://github.com/livekit-examples/agent-starter-react-native/issues/8 - this affects Android emulators, so be aware of that. Another possibility is an issue with token permissions, perhaps - you could try validating your token at http://www.yepher.com/livekit/validate_token.html to double-check your front-end has the correct permissions.
b
Yes, something is weird with dispatching agent to the room. Testing via playground, it only displays “`Waiting for agent audio track…`” when i joined the room using the generated token.
Thanks for mentioning the token validation tool (i didn’t know such exists). I tried it but it failed with
Error: Cannot read properties of undefined (reading 'digest')
My code for dispatching agent to the room is pretty simple:
Copy code
func (c *Client) CreateRoomWithAgentDispatch(roomName string, agentName string, metadata string) (*livekit.Room, error) {
	agentDispatch := &livekit.RoomAgentDispatch{
		AgentName: agentName,
		Metadata:  metadata,
	}

	room, err := c.roomServiceClient.CreateRoom(c.ctx, &livekit.CreateRoomRequest{
		EmptyTimeout:     30,
		DepartureTimeout: 60 * 60, // duration of 1 hour per session
		Name:             roomName,
		Agents:           []*livekit.RoomAgentDispatch{agentDispatch},
	})

	if err != nil {
		if c.logger != nil {
			c.logger.Errorw("error creating room with agent dispatch",
				"room_name", roomName,
				"agent_name", agentName,
				"error", err)
		}
		return nil, err
	}

	if c.logger != nil {
		c.logger.Infow("created room with agent dispatch",
			"room_name", roomName,
			"room_sid", room.Sid,
			"agent_name", agentName)
	}

	return room, nil
}
so i am not really sure what’s happening
What i would explore next is finding another way of dispatching the agent to the room and generating a token, then I can use the token to join via web/playground
Some updates on this issue (I doubt I am the only person facing this issue, and i don’t think it’s mentioned/addressed in any part of the documentation): Simply put, When you
explicitly
dispatch an agent and it joins the room, it waits for about
3 seconds
max for a track to be published by the client, if none, it becomes inactive (although,
it still remains in the room
which is a big issue here) and gets killed by the worker/job-/scheduler/something else. What this means is, if a client fails to join within 3 seconds from when the agent is explicitly dispatched, they might find themselves in a situation where, when they eventually joined the room, they see the agent in the room but can’t hear any audio from the agent; why? because the agent has been marked inactive and has been scheduled to be
killed
soon. But they will never know!
Again, I am talking about
explicit agent dispatch
here. So it’s either this is a
bad design
of how client and agent join a room or a
bug
that should be fixed (more like,
agent should not be allowed to join a room if the client has not joined yet
). One solution I thought of this morning [but yet to be implemented] is to split the process of generating token and dispatching agent into 2 steps/endpoints: `step/endpoint 1`: client calls backend to generate token and uses that token to join the room. `step/endpoint 2`: client makes another call to ask for an agent to be dispatched to the room: so backend confirms the client has joined the room and process the explicit agent dispatch request. [this approach i think, might work…I will share more update later today/tomorrow]
l
One solution I thought of this morning [but yet to be implemented] is to split the process of generating token...
This seems sensible. Alternatively, did you see this: https://docs.livekit.io/agents/server/agent-dispatch/#dispatch-on-participant-connection
I tried it but it failed with
Error: Cannot read properties of undefined (reading 'digest')
Strange - I haven't tried that tool myself - we have an internal tool for also checking tokens, so I can try there if you have further issues
b
Yes, that is what I am using:
Copy code
func (c *Client) AccessTokenWithJobMetadata(userId string, grant *auth.VideoGrant, agentName string, metadata string) (string, error) {
    // Validate that API key and secret are set
    if c.apiKey == "" {
       return "", fmt.Errorf("LiveKit API key is not set")
    }
    if c.secretKey == "" {
       return "", fmt.Errorf("LiveKit API secret is not set")
    }

    at := auth.NewAccessToken(c.apiKey, c.secretKey)

    roomConfig := &livekit.RoomConfiguration{
       Agents: []*livekit.RoomAgentDispatch{
          {
             AgentName: agentName,
             Metadata:  metadata, // This becomes ctx.job.metadata in the agent
          },
       },
    }

   
    at.SetVideoGrant(grant).
       SetIdentity(userId).
       SetName(userId).
       SetRoomConfig(roomConfig).
       SetValidFor(time.Hour)

    token, err := at.ToJWT()
    if err != nil {
       return "", fmt.Errorf("failed to generate JWT token: %w", err)
    }

     

    return token, nil
}