Skip to content
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
59 changes: 37 additions & 22 deletions cmd/lk/agent_eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ import (
"github.com/livekit/livekit-cli/v2/pkg/util"
)

// init appends `lk agent eval` to `lk agent`. It reads a project's evals, its
// judge defaults and its session labels. Evals exist only on LiveKit Cloud,
// init appends `lk agent eval` to `lk agent`. It reads and changes a
// project's evals, its judge defaults and its session labels; the changes are
// in agent_eval_writes.go. Evals exist only on LiveKit Cloud,
// and only the Public API serves them, so every eval command requires
// --user-auth and calls pkg/public directly. Like the other Public-API-only
// groups, it is Hidden until a user signs in (see setUserAuthHidden). The
Expand All @@ -41,7 +42,7 @@ func init() {
var agentEvalCommand = &cli.Command{
Name: "eval",
Aliases: []string{"evals"},
Usage: "Read the evals that score your agents' sessions on LiveKit Cloud (requires --user-auth)",
Usage: "Manage the evals that score your agents' sessions on LiveKit Cloud (requires --user-auth)",
Hidden: true,
Metadata: userAuthOnly,
Description: `An eval is a set of criteria that LiveKit checks on each finished live session
Expand All @@ -58,8 +59,9 @@ only on the sessions with a label.
EVAL_ID is the EVAL_ ID that list prints. The results of the evals are under
lk analytics: evals, eval-sessions, labels and session evals.

Reading evals and labels requires the READ role on the project.`,
Commands: []*cli.Command{
Reading evals and labels, and validate, require the READ role on the project;
changing them requires the WRITE role.`,
Commands: append([]*cli.Command{
{
Name: "list",
Usage: "List the evals of a project",
Expand Down Expand Up @@ -88,7 +90,7 @@ Reading evals and labels requires the READ role on the project.`,
"the definition with the project's judge defaults when it was published, which LiveKit scores sessions with. " +
"A deleted eval is still readable.",
ArgsUsage: "EVAL_ID",
Action: evalRead(evalVersionOptions, fetchEval),
Action: evalAction(evalVersionOptions, fetchEval),
Flags: append([]cli.Flag{jsonFlag}, evalGetFlags()...),
},
{
Expand All @@ -99,7 +101,7 @@ Reading evals and labels requires the READ role on the project.`,
"and each change of its name or description. Changes Scores says whether a new revision scores sessions " +
"differently from the one before it. --json prints the entries as the API sends them.",
ArgsUsage: "EVAL_ID",
Action: evalRead(evalHistoryOptions, fetchEvalHistory),
Action: evalAction(evalHistoryOptions, fetchEvalHistory),
Flags: append([]cli.Flag{
jsonFlag,
&cli.StringSliceFlag{
Expand All @@ -117,21 +119,22 @@ Reading evals and labels requires the READ role on the project.`,
"~ for one changed. A VERSION is a revision number or draft. The definitions are compared as they were written, " +
"without the project's judge defaults. --json prints the changes as the API sends them.",
ArgsUsage: "EVAL_ID",
Action: evalRead(evalDiffOptions, fetchEvalDiff),
Action: evalAction(evalDiffOptions, fetchEvalDiff),
Flags: append([]cli.Flag{jsonFlag}, evalDiffFlags()...),
},
{
Name: "defaults",
Usage: "Print the project's judge defaults and the judge models a definition can name",
Description: "Each eval uses the judge defaults for the judge settings that its definition doesn't set. " +
"A revision keeps the defaults it was published with. --json prints the API's response.",
Action: projectRead(noReadOptions, fetchEvalDefaults),
Flags: []cli.Flag{jsonFlag},
Action: projectRead(noReadOptions, fetchEvalDefaults),
Flags: []cli.Flag{jsonFlag},
Commands: []*cli.Command{evalDefaultsSetCommand},
},
{
Name: "label",
Usage: "Read the session labels of a project",
Commands: []*cli.Command{
Usage: "Manage the session labels of a project",
Commands: append([]*cli.Command{
{
Name: "list",
Usage: "List the session labels of a project",
Expand All @@ -158,32 +161,44 @@ Reading evals and labels requires the READ role on the project.`,
Description: "--json prints the settings as the API sends them.",
Action: projectRead(noReadOptions, fetchSessionLabelSettings),
Flags: []cli.Flag{jsonFlag},
Commands: []*cli.Command{labelSettingsSetCommand},
},
},
}, labelWriteCommands...),
},
},
}, evalWriteCommands...),
}

// noReadOptions is the options reader of a read that takes no flags of its
// own.
func noReadOptions(*cli.Command) (struct{}, error) { return struct{}{}, nil }

// evalRead builds the action of a command that reads one eval of a project.
// Like projectRead's, the action refuses to run without --user-auth before
// checking anything else, then reads the EVAL_ID argument and the command's
// options, so a bad flag fails before the project lookup.
func evalRead[O any](
// evalAction builds the action of a command that reads or changes one eval of
// a project, named by the EVAL_ID argument (see actionWithID).
func evalAction[O any](
readOptions func(*cli.Command) (O, error),
fetch func(ctx context.Context, client *public.Client, projectID, evalID string, opts O, asJSON bool) error,
) cli.ActionFunc {
return actionWithID("eval ID", readOptions, fetch)
}

// actionWithID builds the action of a command on one item of a project, such
// as an eval, named by its first argument; idName names the argument in the
// error when it is missing. Like projectRead's, the action refuses to run
// without --user-auth before checking anything else, then reads the argument
// and the command's options, so a bad flag fails before the project lookup.
func actionWithID[O any](
idName string,
readOptions func(*cli.Command) (O, error),
run func(ctx context.Context, client *public.Client, projectID, id string, opts O, asJSON bool) error,
) cli.ActionFunc {
return func(ctx context.Context, cmd *cli.Command) error {
if err := requireUserAuth(cmd); err != nil {
return err
}
evalID, err := extractArg(cmd)
id, err := extractArg(cmd)
if err != nil {
_ = cli.ShowSubcommandHelp(cmd)
return errors.New("eval ID is required")
return errors.New(idName + " is required")
}
opts, err := readOptions(cmd)
if err != nil {
Expand All @@ -197,7 +212,7 @@ func evalRead[O any](
if err != nil {
return err
}
return fetch(ctx, client, projectID, evalID, opts, cmd.Bool("json"))
return run(ctx, client, projectID, id, opts, cmd.Bool("json"))
}
}

Expand Down
Loading
Loading