Files
livekit/pkg/service/agent_dispatch_service.go
zeekay f47fd29d53 revert the KV client and psrpc swaps here — the build needs upstream types
livekit cannot use github.com/hanzokv/go, for a structural reason rather than a
stylistic one: the client type flows through two upstream libraries we do not
fork. livekit/protocol/redis.GetRedisClient PRODUCES a
github.com/redis/go-redis/v9.UniversalClient and livekit/psrpc CONSUMES one, so
substituting the client in the middle fails to type-check at both seams:

  cannot use redis2.GetRedisClient(&conf.Redis) (go-redis UniversalClient)
    as kv.UniversalClient in return statement
  cannot use rc (kv.UniversalClient)
    as go-redis UniversalClient in argument to psrpc.NewRedisMessageBus

Forking psrpc made it worse rather than better: livekit/protocol/rpc demands
livekit/psrpc's OWN types, so pointing at hanzoai/psrpc broke pkg/agent, which
had compiled. Verified by building pkg/agent at the commit before the fork
(compiles) and after (2 type errors) with a working SDKROOT — an earlier check
of mine reported "compiles in both" and was simply wrong.

So this repo keeps upstream go-redis and upstream psrpc (v0.7.1). That is the
honest consequence of livekit/protocol being unforkable: you cannot substitute a
client library inside a module whose upstream dependencies both produce and
consume that library's types.

What stays, because it is correct and independent: the module path
(github.com/hanzoai/livekit, which this fork now actually declares) and the
luxfi/metric repair in the parent commit that made this repo build at all.
2026-07-28 01:30:41 -07:00

120 lines
3.9 KiB
Go

// Copyright 2023 LiveKit, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package service
import (
"context"
"fmt"
"github.com/hanzoai/livekit/pkg/routing"
"github.com/livekit/protocol/agent"
"github.com/livekit/protocol/livekit"
"github.com/livekit/protocol/logger"
"github.com/livekit/protocol/rpc"
"github.com/livekit/protocol/utils"
"github.com/livekit/protocol/utils/guid"
"github.com/livekit/psrpc"
)
type AgentDispatchService struct {
agentDispatchClient rpc.TypedAgentDispatchInternalClient
topicFormatter rpc.TopicFormatter
roomAllocator RoomAllocator
router routing.MessageRouter
}
func NewAgentDispatchService(
agentDispatchClient rpc.TypedAgentDispatchInternalClient,
topicFormatter rpc.TopicFormatter,
roomAllocator RoomAllocator,
router routing.MessageRouter,
) *AgentDispatchService {
return &AgentDispatchService{
agentDispatchClient: agentDispatchClient,
topicFormatter: topicFormatter,
roomAllocator: roomAllocator,
router: router,
}
}
func (ag *AgentDispatchService) CreateDispatch(ctx context.Context, req *livekit.CreateAgentDispatchRequest) (*livekit.AgentDispatch, error) {
AppendLogFields(ctx, "room", req.Room, "request", logger.Proto(redactCreateAgentDispatchRequest(req)))
err := EnsureAdminPermission(ctx, livekit.RoomName(req.Room))
if err != nil {
return nil, twirpAuthError(err)
}
if err := agent.ValidateDeployment(req.GetDeployment()); err != nil {
return nil, psrpc.NewError(psrpc.InvalidArgument, err)
}
if ag.roomAllocator.AutoCreateEnabled(ctx) {
err := ag.roomAllocator.SelectRoomNode(ctx, livekit.RoomName(req.Room), "")
if err != nil {
return nil, err
}
_, err = ag.router.CreateRoom(ctx, &livekit.CreateRoomRequest{Name: req.Room})
if err != nil {
return nil, err
}
}
dispatch := &livekit.AgentDispatch{
Id: guid.New(guid.AgentDispatchPrefix),
AgentName: req.AgentName,
Room: req.Room,
Metadata: req.Metadata,
RestartPolicy: req.RestartPolicy,
Deployment: req.Deployment,
}
return ag.agentDispatchClient.CreateDispatch(ctx, ag.topicFormatter.RoomTopic(ctx, livekit.RoomName(req.Room)), dispatch)
}
func (ag *AgentDispatchService) DeleteDispatch(ctx context.Context, req *livekit.DeleteAgentDispatchRequest) (*livekit.AgentDispatch, error) {
AppendLogFields(ctx, "room", req.Room, "request", logger.Proto(req))
err := EnsureAdminPermission(ctx, livekit.RoomName(req.Room))
if err != nil {
return nil, twirpAuthError(err)
}
return ag.agentDispatchClient.DeleteDispatch(ctx, ag.topicFormatter.RoomTopic(ctx, livekit.RoomName(req.Room)), req)
}
func (ag *AgentDispatchService) ListDispatch(ctx context.Context, req *livekit.ListAgentDispatchRequest) (*livekit.ListAgentDispatchResponse, error) {
AppendLogFields(ctx, "room", req.Room, "request", logger.Proto(req))
err := EnsureAdminPermission(ctx, livekit.RoomName(req.Room))
if err != nil {
return nil, twirpAuthError(err)
}
return ag.agentDispatchClient.ListDispatch(ctx, ag.topicFormatter.RoomTopic(ctx, livekit.RoomName(req.Room)), req)
}
func redactCreateAgentDispatchRequest(req *livekit.CreateAgentDispatchRequest) *livekit.CreateAgentDispatchRequest {
if req.Metadata == "" {
return req
}
clone := utils.CloneProto(req)
// replace with size of metadata to provide visibility on request size
if clone.Metadata != "" {
clone.Metadata = fmt.Sprintf("__size: %d", len(clone.Metadata))
}
return clone
}