-
Notifications
You must be signed in to change notification settings - Fork 75
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
biglittlebigben
wants to merge
6
commits into
main
Choose a base branch
from
benjamin/agent_dispatch
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+274
−53
Open
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,209 @@ | ||||||
// 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: "agentdispatch", | ||||||
Usage: "Manage agent dispatches for a room", | ||||||
Category: "Agents", | ||||||
Commands: []*cli.Command{ | ||||||
{ | ||||||
Name: "create", | ||||||
Usage: "Create an agent dispatches", | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
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, | ||||||
}, | ||||||
}, | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: SHOUTING_CASE for arg names |
||||||
}, | ||||||
{ | ||||||
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 create the dispatch in", | ||||||
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, | ||||||
}, | ||||||
}, | ||||||
}, | ||||||
}, | ||||||
}, | ||||||
} | ||||||
|
||||||
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) | ||||||
|
||||||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should probably discuss the proper name/command hierarchy here, since
agentdispatch
may not be clearThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see nothing wrong with making
dispatch
a subcommand ofagent
command: