Skip to content

Commit 817558d

Browse files
authored
chore(agents): add support for rich errors from cloud-agents (#541)
* chore(agents): add support for rich errors from `cloud-agents` * chore(cli): bump version
1 parent 8d56a05 commit 817558d

File tree

6 files changed

+59
-34
lines changed

6 files changed

+59
-34
lines changed

cmd/lk/agent.go

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727

2828
"github.com/BurntSushi/toml"
2929
"github.com/charmbracelet/huh"
30+
"github.com/twitchtv/twirp"
3031
"github.com/urfave/cli/v3"
3132

3233
"github.com/livekit/livekit-cli/v2/pkg/agentfs"
@@ -362,6 +363,7 @@ func createAgent(ctx context.Context, cmd *cli.Command) error {
362363
if err := huh.NewInput().
363364
Title("Agent name").
364365
Value(&name).
366+
WithTheme(util.Theme).
365367
Run(); err != nil {
366368
return err
367369
}
@@ -524,6 +526,11 @@ func createAgent(ctx context.Context, cmd *cli.Command) error {
524526

525527
resp, err := agentsClient.CreateAgent(ctx, req)
526528
if err != nil {
529+
if twerr, ok := err.(twirp.Error); ok {
530+
if twerr.Meta("type") == "agent_hosting_disabled" {
531+
return fmt.Errorf("agent hosting is disabled for this project -- join the beta program here [%s]", twerr.Meta("redirect_url"))
532+
}
533+
}
527534
return err
528535
}
529536

@@ -1124,13 +1131,13 @@ func selectSecrets(secrets map[string]string) (map[string]string, error) {
11241131
return options[i].Key < options[j].Key
11251132
})
11261133

1127-
if err := huh.NewMultiSelect[string]().
1128-
Title("Select secrets to include").
1129-
Description("Press space to toggle, enter to confirm").
1130-
Options(options...).
1131-
Value(&keys).
1132-
Height(6).
1133-
WithTheme(util.Theme).
1134+
if err := huh.NewForm(
1135+
huh.NewGroup(huh.NewMultiSelect[string]().
1136+
Title("Select secrets to include").
1137+
Options(options...).
1138+
Value(&keys).
1139+
Height(6).
1140+
WithTheme(util.Theme))).
11341141
Run(); err != nil {
11351142
return nil, fmt.Errorf("error running TUI: %w", err)
11361143
}

cmd/lk/app.go

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -152,12 +152,13 @@ func requireProject(ctx context.Context, cmd *cli.Command) (context.Context, err
152152
for _, p := range cliConfig.Projects {
153153
options = append(options, huh.NewOption(p.Name+" ["+p.APIKey+"]", &p))
154154
}
155-
if err = huh.NewSelect[*config.ProjectConfig]().
156-
Title("Select a project to use for this app").
157-
Description("If you'd like to use a different project, run `lk cloud auth` to add credentials").
158-
Options(options...).
159-
Value(&project).
160-
WithTheme(util.Theme).
155+
if err = huh.NewForm(
156+
huh.NewGroup(huh.NewSelect[*config.ProjectConfig]().
157+
Title("Select a project to use for this app").
158+
Description("If you'd like to use a different project, run `lk cloud auth` to add credentials").
159+
Options(options...).
160+
Value(&project).
161+
WithTheme(util.Theme))).
161162
Run(); err != nil {
162163
return nil, err
163164
}
@@ -512,11 +513,12 @@ func runTask(ctx context.Context, cmd *cli.Command) error {
512513
options = append(options, huh.NewOption(name, name))
513514
}
514515

515-
if err := huh.NewSelect[string]().
516-
Title("Select Task").
517-
Options(options...).
518-
Value(&taskName).
519-
WithTheme(util.Theme).
516+
if err := huh.NewForm(
517+
huh.NewGroup(huh.NewSelect[string]().
518+
Title("Select Task").
519+
Options(options...).
520+
Value(&taskName).
521+
WithTheme(util.Theme))).
520522
Run(); err != nil {
521523
return err
522524
}

cmd/lk/token.go

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -287,20 +287,21 @@ func createToken(ctx context.Context, c *cli.Command) error {
287287

288288
permissions := make([]permission, 0)
289289

290-
if err := huh.NewMultiSelect[permission]().
291-
Options(
292-
huh.NewOption("Create", pCreate),
293-
huh.NewOption("List", pList),
294-
huh.NewOption("Join", pJoin),
295-
huh.NewOption("Admin", pAdmin),
296-
huh.NewOption("Egress", pEgress),
297-
huh.NewOption("Ingress", pIngress),
298-
huh.NewOption("Update metadata", pMetadata),
299-
).
300-
Title("Token Permissions").
301-
Description("See https://docs.livekit.io/home/get-started/authentication/#Video-grant").
302-
Value(&permissions).
303-
WithTheme(util.Theme).
290+
if err := huh.NewForm(
291+
huh.NewGroup(huh.NewMultiSelect[permission]().
292+
Options(
293+
huh.NewOption("Create", pCreate),
294+
huh.NewOption("List", pList),
295+
huh.NewOption("Join", pJoin),
296+
huh.NewOption("Admin", pAdmin),
297+
huh.NewOption("Egress", pEgress),
298+
huh.NewOption("Ingress", pIngress),
299+
huh.NewOption("Update metadata", pMetadata),
300+
).
301+
Title("Token Permissions").
302+
Description("See https://docs.livekit.io/home/get-started/authentication/#Video-grant").
303+
Value(&permissions).
304+
WithTheme(util.Theme))).
304305
Run(); err != nil || len(permissions) == 0 {
305306
return errors.New("no permissions were given in this grant, see --help")
306307
} else {

pkg/agentfs/docker.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626

2727
"github.com/charmbracelet/huh"
2828

29+
"github.com/livekit/livekit-cli/v2/pkg/util"
2930
"github.com/livekit/protocol/logger"
3031
)
3132

@@ -176,7 +177,8 @@ func validateEntrypoint(dir string, dockerfileContent []byte, projectType string
176177
huh.NewSelect[string]().
177178
Title(fmt.Sprintf("Select %s file to use as entrypoint", projectType)).
178179
Options(huh.NewOptions(options...)...).
179-
Value(&selected),
180+
Value(&selected).
181+
WithTheme(util.Theme),
180182
),
181183
)
182184

pkg/agentfs/logs.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package agentfs
1717
import (
1818
"bufio"
1919
"context"
20+
"encoding/json"
2021
"fmt"
2122
"net/http"
2223
"net/url"
@@ -29,6 +30,11 @@ import (
2930
"github.com/livekit/protocol/logger"
3031
)
3132

33+
type APIError struct {
34+
Message string `json:"msg"`
35+
Meta *map[string]string `json:"meta,omitempty"`
36+
}
37+
3238
func LogHelper(ctx context.Context, id string, name string, logType string, projectConfig *config.ProjectConfig) error {
3339
if logType == "" {
3440
logType = "deploy"
@@ -85,6 +91,13 @@ func LogHelper(ctx context.Context, id string, name string, logType string, proj
8591

8692
if resp.StatusCode != http.StatusOK {
8793
logger.Debugw("failed to get logs", "status", resp.Status)
94+
95+
var errorResponse APIError
96+
if err := json.NewDecoder(resp.Body).Decode(&errorResponse); err != nil {
97+
return fmt.Errorf("failed to parse error response: %w", err)
98+
} else {
99+
return fmt.Errorf("failed to get logs: %s", errorResponse.Message)
100+
}
88101
}
89102

90103
scanner := bufio.NewScanner(resp.Body)

version.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,5 @@
1515
package livekitcli
1616

1717
const (
18-
Version = "2.4.1"
18+
Version = "2.4.2"
1919
)

0 commit comments

Comments
 (0)