Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for agent dispatch management #383

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: "1.21"
go-version: "1.22"

- name: Download Go modules
run: go mod download
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: "1.21"
go-version: "1.22"

- name: Run GoReleaser
uses: goreleaser/goreleaser-action@v6
Expand Down
218 changes: 218 additions & 0 deletions cmd/lk/agent_dispatch.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,218 @@
// 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 main

import (
"context"
"fmt"
"time"

"github.com/urfave/cli/v3"

"github.com/livekit/protocol/livekit"
lksdk "github.com/livekit/server-sdk-go/v2"
)

var (
AgentDispatchCommands = []*cli.Command{
{
Name: "dispatch",
Usage: "Manage agent dispatches for a room",
Category: "Agents",
Commands: []*cli.Command{
{
Name: "create",
Usage: "Create an agent dispatch",
UsageText: "lk agentdispatch create [OPTIONS]",
Before: createAgentDispatchClient,
Action: createAgentDispatch,
Flags: []cli.Flag{
&cli.StringFlag{
Name: "room",
Usage: "`NAME` of the room to create the dispatch in",
Required: true,
},
&cli.StringFlag{
Name: "agent-name",
Usage: "`AGENT_NAME` to dispatch the job to",
Required: false,
},
&cli.StringFlag{
Name: "metadata",
Usage: "`METADATA` to pass to the agent workers",
Required: false,
},
},
},
{
Name: "delete",
Usage: "Delete an agent dispatch",
UsageText: "lk agentdispatch delete [OPTIONS]",
Before: createAgentDispatchClient,
Action: deleteAgentDispatch,
Flags: []cli.Flag{
&cli.StringFlag{
Name: "room",
Usage: "`NAME` of the room to delete the dispatch from",
Required: true,
},
&cli.StringFlag{
Name: "id",
Usage: "`ID` of the dispatch to delete",
Required: true,
},
},
},

{
Name: "list",
Usage: "List all active agent dispatches",
UsageText: "lk agentdispatch list [OPTIONS]",
Before: createAgentDispatchClient,
Action: listAgentDispatches,
Flags: []cli.Flag{
&cli.StringFlag{
Name: "room",
Usage: "List agents dispatches for room `NAME`",
Required: true,
},
&cli.StringFlag{
Name: "id",
Usage: "List a specific agent dispatch `ID`",
Required: false,
},
},
},
},
},
}

AgentCommands = []*cli.Command{
{
Name: "agent",
Usage: "Manage agents for a room",
Category: "Agents",
Commands: AgentDispatchCommands,
},
}

agentDispatchClient *lksdk.AgentDispatchClient
)

func createAgentDispatchClient(ctx context.Context, cmd *cli.Command) error {
pc, err := loadProjectDetails(cmd)
if err != nil {
return err
}

agentDispatchClient = lksdk.NewAgentDispatchServiceClient(pc.URL, pc.APIKey, pc.APISecret, withDefaultClientOpts(pc)...)
return nil
}

func listAgentDispatches(ctx context.Context, cmd *cli.Command) error {
res, err := agentDispatchClient.ListDispatch(context.Background(), &livekit.ListAgentDispatchRequest{
Room: cmd.String("room"),
DispatchId: cmd.String("id"),
})
if err != nil {
return err
}

table := CreateTable().
Headers("DispatchID", "AgentName", "Room")
for _, item := range res.AgentDispatches {
if item == nil {
continue
}

table.Row(
item.Id,
item.AgentName,
item.Room,
)
}
fmt.Println(table)

if cmd.Bool("verbose") {
PrintJSON(res)
}

return nil
}

func createAgentDispatch(ctx context.Context, cmd *cli.Command) error {
res, err := agentDispatchClient.CreateDispatch(context.Background(), &livekit.CreateAgentDispatchRequest{
Room: cmd.String("room"),
AgentName: cmd.String("agent-name"),
Metadata: cmd.String("metadata"),
})
if err != nil {
return err
}

printAgentDispatch(res)

if cmd.Bool("verbose") {
PrintJSON(res)
}

return nil
}

func deleteAgentDispatch(ctx context.Context, cmd *cli.Command) error {
res, err := agentDispatchClient.DeleteDispatch(context.Background(), &livekit.DeleteAgentDispatchRequest{
Room: cmd.String("room"),
DispatchId: cmd.String("id"),
})
if err != nil {
return err
}

printAgentDispatch(res)

if cmd.Bool("verbose") {
PrintJSON(res)
}

return nil
}

func printAgentDispatch(ad *livekit.AgentDispatch) {
var createdAt time.Time

table := CreateTable().
Headers("JobID", "Job Type", "Participant Identity")

if ad.State != nil {
createdAt = time.Unix(0, ad.State.CreatedAt)

for _, item := range ad.State.Jobs {
identity := ""
if item.Participant != nil {
identity = item.Participant.Identity
}

table.Row(
item.Id,
item.Type.String(),
identity,
)
}
}

fmt.Printf("DispatchID: %v CreatedAt: %v\n", ad.Id, createdAt)
fmt.Println(table)

}
1 change: 1 addition & 0 deletions cmd/lk/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ func main() {
app.Commands = append(app.Commands, LoadTestCommands...)
app.Commands = append(app.Commands, ProjectCommands...)
app.Commands = append(app.Commands, SIPCommands...)
app.Commands = append(app.Commands, AgentCommands...)

// Register cleanup hook for SIGINT, SIGTERM, SIGQUIT
ctx, stop := signal.NotifyContext(
Expand Down
31 changes: 16 additions & 15 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ require (
github.com/charmbracelet/lipgloss v0.12.1
github.com/frostbyte73/core v0.0.10
github.com/go-logr/logr v1.4.2
github.com/livekit/protocol v1.19.2-0.20240710171229-73ece66d30e0
github.com/livekit/server-sdk-go/v2 v2.2.0
github.com/livekit/protocol v1.19.4-0.20240808180722-581b59b65309
github.com/livekit/server-sdk-go/v2 v2.2.1-0.20240808191016-0780628d37ea
github.com/pion/rtcp v1.2.14
github.com/pion/rtp v1.8.7
github.com/pion/webrtc/v3 v3.2.49
github.com/pion/rtp v1.8.9
github.com/pion/webrtc/v3 v3.2.50
github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c
github.com/pkg/errors v0.9.1
github.com/stretchr/testify v1.9.0
Expand Down Expand Up @@ -53,7 +53,7 @@ require (
github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/gammazero/deque v0.2.1 // indirect
github.com/go-jose/go-jose/v4 v4.0.3 // indirect
github.com/go-jose/go-jose/v3 v3.0.3 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/google/cel-go v0.20.1 // indirect
github.com/google/uuid v1.6.0 // indirect
Expand All @@ -63,8 +63,8 @@ require (
github.com/klauspost/cpuid/v2 v2.2.7 // indirect
github.com/lithammer/shortuuid/v4 v4.0.0 // indirect
github.com/livekit/mageutil v0.0.0-20230125210925-54e8a70427c1 // indirect
github.com/livekit/mediatransportutil v0.0.0-20240613015318-84b69facfb75 // indirect
github.com/livekit/psrpc v0.5.3-0.20240526192918-fbdaf10e6aa5 // indirect
github.com/livekit/mediatransportutil v0.0.0-20240730083616-559fa5ece598 // indirect
github.com/livekit/psrpc v0.5.3-0.20240616012458-ac39c8549a0a // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/magefile/mage v1.15.0 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
Expand All @@ -79,7 +79,7 @@ require (
github.com/nats-io/nuid v1.0.1 // indirect
github.com/pion/datachannel v1.5.8 // indirect
github.com/pion/dtls/v2 v2.2.12 // indirect
github.com/pion/ice/v2 v2.3.29 // indirect
github.com/pion/ice/v2 v2.3.31 // indirect
github.com/pion/interceptor v0.1.29 // indirect
github.com/pion/logging v0.2.2 // indirect
github.com/pion/mdns v0.0.12 // indirect
Expand All @@ -88,29 +88,30 @@ require (
github.com/pion/sdp/v3 v3.0.9 // indirect
github.com/pion/srtp/v2 v2.0.20 // indirect
github.com/pion/stun v0.6.1 // indirect
github.com/pion/transport/v2 v2.2.5 // indirect
github.com/pion/transport/v2 v2.2.8 // indirect
github.com/pion/turn/v2 v2.1.6 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_golang v1.19.0 // indirect
github.com/prometheus/client_model v0.5.0 // indirect
github.com/prometheus/common v0.48.0 // indirect
github.com/prometheus/procfs v0.12.0 // indirect
github.com/puzpuzpuz/xsync/v3 v3.1.0 // indirect
github.com/redis/go-redis/v9 v9.5.3 // indirect
github.com/redis/go-redis/v9 v9.6.1 // indirect
github.com/rivo/uniseg v0.4.7 // indirect
github.com/stoewer/go-strcase v1.3.0 // indirect
github.com/wlynxg/anet v0.0.3 // indirect
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect
github.com/xrash/smetrics v0.0.0-20240312152122-5f08fbb34913 // indirect
github.com/zeebo/xxh3 v1.0.2 // indirect
go.uber.org/multierr v1.11.0 // indirect
go.uber.org/zap v1.27.0 // indirect
go.uber.org/zap/exp v0.2.0 // indirect
golang.org/x/crypto v0.25.0 // indirect
golang.org/x/exp v0.0.0-20240613232115-7f521ea00fb8 // indirect
golang.org/x/net v0.26.0 // indirect
golang.org/x/exp v0.0.0-20240719175910-8a7402abbf56 // indirect
golang.org/x/net v0.27.0 // indirect
golang.org/x/sys v0.22.0 // indirect
golang.org/x/text v0.16.0 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20240401170217-c3f982113cda // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240617180043-68d350f18fd4 // indirect
google.golang.org/grpc v1.64.0 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20240528184218-531527333157 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240725223205-93522f1f2a9f // indirect
google.golang.org/grpc v1.65.0 // indirect
)
Loading