Skip to content
Draft
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
69 changes: 54 additions & 15 deletions cmd/lk/simulate.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ var simulateCommand = &cli.Command{
Action: func(ctx context.Context, cmd *cli.Command) error {
return cli.ShowSubcommandHelp(cmd)
},
// --set values may hold commas
DisableSliceFlagSeparator: true,
Commands: []*cli.Command{
simulateTextCommand, simulateAudioCommand, simulateListCommand, simulateViewCommand, simulateExportCommand,
simulateGetCommand, simulateCancelCommand,
Expand Down Expand Up @@ -111,6 +113,10 @@ var simulateCommand = &cli.Command{
Name: "deployment",
Usage: "`NAME` of the agent deployment to run against, such as a non-production deployment. Defaults to production. Requires --agent-name.",
},
&cli.StringSliceFlag{
Name: "set",
Usage: "Pass `KEY=VALUE` to the agent under test, which reads it with SimulationContext.defines(). The same on every scenario of the run. Repeat to pass more than one.",
},
},
}

Expand Down Expand Up @@ -142,13 +148,15 @@ func loadSimulateProject(ctx context.Context, cmd *cli.Command) (context.Context
}

var simulateTextCommand = &cli.Command{
Name: "text",
Usage: "Run scenarios in text mode",
Description: "Options on lk agent simulate apply here too, e.g. --scenarios and --agent-name.",
Metadata: map[string]any{docsInheritsOptions: true},
ArgsUsage: "[entrypoint]",
HideHelpCommand: true,
Before: loadSimulateProject,
Name: "text",
// --set values may hold commas
DisableSliceFlagSeparator: true,
Usage: "Run scenarios in text mode",
Description: "Options on lk agent simulate apply here too, e.g. --scenarios and --agent-name.",
Metadata: map[string]any{docsInheritsOptions: true},
ArgsUsage: "[entrypoint]",
HideHelpCommand: true,
Before: loadSimulateProject,
Action: func(ctx context.Context, cmd *cli.Command) error {
return runSimulate(ctx, cmd, livekit.SimulationMode_SIMULATION_MODE_TEXT)
},
Expand All @@ -158,13 +166,15 @@ var simulateTextCommand = &cli.Command{
// persistent (cli.FlagBase.Local defaults to false), so they parse on either
// side of the subcommand name.
var simulateAudioCommand = &cli.Command{
Name: "audio",
Usage: "Run scenarios through the full STT-LLM-TTS pipeline",
Description: "Options on lk agent simulate apply here too, e.g. --scenarios and --agent-name.",
Metadata: map[string]any{docsInheritsOptions: true},
ArgsUsage: "[entrypoint]",
HideHelpCommand: true,
Before: loadSimulateProject,
Name: "audio",
// --set values may hold commas
DisableSliceFlagSeparator: true,
Usage: "Run scenarios through the full STT-LLM-TTS pipeline",
Description: "Options on lk agent simulate apply here too, e.g. --scenarios and --agent-name.",
Metadata: map[string]any{docsInheritsOptions: true},
ArgsUsage: "[entrypoint]",
HideHelpCommand: true,
Before: loadSimulateProject,
Action: func(ctx context.Context, cmd *cli.Command) error {
return runSimulate(ctx, cmd, livekit.SimulationMode_SIMULATION_MODE_AUDIO)
},
Expand Down Expand Up @@ -443,6 +453,9 @@ type simulateConfig struct {

// named deployment of agentName to dispatch to; empty is production
deployment string

// --set KEY=VALUE pairs, surfaced to the agent as SimulationContext.defines()
defines map[string]string
}

type simulateMode int
Expand Down Expand Up @@ -566,6 +579,11 @@ func runSimulate(ctx context.Context, cmd *cli.Command, simulationMode livekit.S
return fmt.Errorf("--to requires --from")
}

defines, err := parseSetFlags(cmd.StringSlice("set"))
if err != nil {
return err
}

// never auto-discovered: an explicit --scenarios file is the source of
// truth, otherwise scenarios are generated from the agent's source
scenariosPath := cmd.String("scenarios")
Expand All @@ -585,7 +603,6 @@ func runSimulate(ctx context.Context, cmd *cli.Command, simulationMode livekit.S
projectType agentfs.ProjectType
entrypoint string
liveAgent bool
err error
)

// --agent-name (even empty) means: run against an already-running agent,
Expand Down Expand Up @@ -665,6 +682,7 @@ func runSimulate(ctx context.Context, cmd *cli.Command, simulationMode livekit.S
simulationMode: simulationMode,
agentName: agentName,
deployment: deployment,
defines: defines,
projectDir: projectDir,
projectType: projectType,
entrypoint: entrypoint,
Expand Down Expand Up @@ -850,6 +868,7 @@ func createSimulationRun(ctx context.Context, c *simulateConfig) (string, *livek
req := &livekit.SimulationRun_Create_Request{
AgentName: c.agentName,
Deployment: c.deployment,
Defines: c.defines,
NumSimulations: c.numSimulations,
Mode: c.simulationMode,
BackgroundNoise: c.backgroundNoise,
Expand Down Expand Up @@ -1223,3 +1242,23 @@ func decodeRunSummary(run *livekit.SimulationRun) *livekit.SimulationRunSummary
summary, _ := decodeRunSummaryStrict(run)
return summary
}

// parseSetFlags reads repeated --set KEY=VALUE flags. VALUE may be empty or
// contain '='; KEY may not repeat.
func parseSetFlags(values []string) (map[string]string, error) {
if len(values) == 0 {
return nil, nil
}
defines := make(map[string]string, len(values))
for _, kv := range values {
k, v, ok := strings.Cut(kv, "=")
if !ok || k == "" {
return nil, fmt.Errorf("--set must be KEY=VALUE (got %q)", kv)
}
if _, dup := defines[k]; dup {
return nil, fmt.Errorf("--set %s is given more than once", k)
}
defines[k] = v
}
return defines, nil
}
55 changes: 55 additions & 0 deletions cmd/lk/simulate_set_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Copyright 2026 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"
"testing"

"github.com/stretchr/testify/require"
"github.com/urfave/cli/v3"
)

func TestParseSetFlags(t *testing.T) {
defines, err := parseSetFlags([]string{"llm=gpt-5", "prompt=a=b", "empty="})
require.NoError(t, err)
require.Equal(t, map[string]string{"llm": "gpt-5", "prompt": "a=b", "empty": ""}, defines)

defines, err = parseSetFlags(nil)
require.NoError(t, err)
require.Nil(t, defines)

for _, bad := range [][]string{{"llm"}, {"=x"}, {"llm=a", "llm=b"}} {
_, err := parseSetFlags(bad)
require.Error(t, err, "%v", bad)
}
}

func TestSetFlagKeepsCommas(t *testing.T) {
for _, sub := range []*cli.Command{simulateTextCommand, simulateAudioCommand} {
var got []string
leaf := *sub
leaf.Before = nil
leaf.Action = func(_ context.Context, cmd *cli.Command) error {
got = cmd.StringSlice("set")
return nil
}
parent := *simulateCommand
parent.Commands = []*cli.Command{&leaf}
root := &cli.Command{Name: "lk", Commands: []*cli.Command{&parent}}
require.NoError(t, root.Run(context.Background(), []string{"lk", "simulate", "--set", "prompt=hello,world", sub.Name, "--set", "pair=a,b=c"}))
require.Equal(t, []string{"prompt=hello,world", "pair=a,b=c"}, got, sub.Name)
}
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ require (
github.com/gorilla/websocket v1.5.4-0.20250319132907-e064f32e3674
github.com/joho/godotenv v1.5.1
github.com/klauspost/compress v1.20.1
github.com/livekit/protocol v1.53.3-0.20261104230654-7bbaddbd56db
github.com/livekit/protocol v1.53.3-0.20261106232337-0db29842693a
github.com/livekit/server-sdk-go/v2 v2.19.0
github.com/mattn/go-isatty v0.0.22
github.com/moby/moby/client v0.4.1
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,8 @@ github.com/livekit/mediatransportutil v0.0.0-20260821083140-f234b534b095 h1:Bcli
github.com/livekit/mediatransportutil v0.0.0-20260821083140-f234b534b095/go.mod h1:o8CFmAdrVwzJNOCsQCLUzXRjokkufNshnQHOe4fRaqU=
github.com/livekit/protocol v1.53.3-0.20261104230654-7bbaddbd56db h1:O1/7sYUmSiAra66R/JIjKzsvgHyHXswwMkHSH3e0lEE=
github.com/livekit/protocol v1.53.3-0.20261104230654-7bbaddbd56db/go.mod h1:oSYtuivQ/y77D+cCZStm9heI6yE+w9lAacWnzSIRLiQ=
github.com/livekit/protocol v1.53.3-0.20261106232337-0db29842693a h1:EkZ6YZLw75gdP8mPOySgdBAeie9kA6ARDPc+OS15OEk=
github.com/livekit/protocol v1.53.3-0.20261106232337-0db29842693a/go.mod h1:S0AnW03n6zgnWZOq+T4L1PtT9POxTBgrgEH1jyR6fl4=
github.com/livekit/psrpc v0.8.2 h1:7tbkhBJoYlK5Csfkdv1XYbZjyAQeaEZgupvIXNCv7Dc=
github.com/livekit/psrpc v0.8.2/go.mod h1:6KnU8vULoizX0Er9NNfeKJDV8ug87j3fuo19M0bhj6Q=
github.com/livekit/server-sdk-go/v2 v2.19.0 h1:fZ0zmeC/5V+rXiI/MeC05EjwTfXmpaGkr/DXLG3EzgA=
Expand Down
Loading