auto create rooms during create agent dispatch api request (#3158)

This commit is contained in:
Paul Wells
2024-11-05 16:15:05 -08:00
committed by GitHub
parent 365e63230d
commit 09f140afa8
7 changed files with 75 additions and 111 deletions
+19 -1
View File
@@ -17,6 +17,7 @@ package service
import (
"context"
"github.com/livekit/livekit-server/pkg/routing"
"github.com/livekit/protocol/livekit"
"github.com/livekit/protocol/rpc"
"github.com/livekit/protocol/utils/guid"
@@ -25,12 +26,21 @@ import (
type AgentDispatchService struct {
agentDispatchClient rpc.TypedAgentDispatchInternalClient
topicFormatter rpc.TopicFormatter
roomAllocator RoomAllocator
router routing.MessageRouter
}
func NewAgentDispatchService(agentDispatchClient rpc.TypedAgentDispatchInternalClient, topicFormatter rpc.TopicFormatter) *AgentDispatchService {
func NewAgentDispatchService(
agentDispatchClient rpc.TypedAgentDispatchInternalClient,
topicFormatter rpc.TopicFormatter,
roomAllocator RoomAllocator,
router routing.MessageRouter,
) *AgentDispatchService {
return &AgentDispatchService{
agentDispatchClient: agentDispatchClient,
topicFormatter: topicFormatter,
roomAllocator: roomAllocator,
router: router,
}
}
@@ -40,6 +50,14 @@ func (ag *AgentDispatchService) CreateDispatch(ctx context.Context, req *livekit
return nil, twirpAuthError(err)
}
if ag.roomAllocator.AutoCreateEnabled(ctx) {
// ensure at least one node is available to handle the request
_, 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,