Skip to content
Merged
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
31 changes: 25 additions & 6 deletions cmd/sync/link.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,16 @@ var linkExamples = map[syncdomain.Kind]string{
--select 'agents[id="support"]' \
--map prompt=instructions \
--project production --config-key support --key default \
--yes --no-input

# Create the config and the variation when LaunchDarkly has none
ldcli sync prompts link \
--file app/agents.py \
--format python \
--select 'agents[id="support"]' \
--map prompt=instructions \
--project production --config-key support --key default \
--upsert --model-config-key gpt-5 \
--yes --no-input`,
syncdomain.KindTool: ` # Link one TypeScript MCP tool
ldcli sync tools link \
Expand Down Expand Up @@ -52,7 +62,7 @@ func newLinkCmd(client resources.Client, kind syncdomain.Kind) *cobra.Command {
Short: fmt.Sprintf("Link a repository source to one %s", noun),
Long: fmt.Sprintf("Link a repository source to one %s. "+
"Link reads only the named file, shows the descriptor and the sync plan, and writes the descriptor after you confirm. "+
"It then syncs the %s.", noun, noun),
"It then syncs the %s. With --upsert, it creates the %s in LaunchDarkly when it does not exist, and the config of a variation too.", noun, noun, noun),
Example: linkExamples[kind],
Args: validatedArgs(cobra.NoArgs),
RunE: runCommand(client, scope, func(cmd *cobra.Command, _ []string) (syncprompt.CommandAction, error) {
Expand All @@ -64,12 +74,16 @@ func newLinkCmd(client resources.Client, kind syncdomain.Kind) *cobra.Command {
if err != nil {
return nil, err
}
upsert, _ := cmd.Flags().GetBool(upsertFlag)
return syncprompt.SourceLinkAction{
ID: id,
File: mustString(cmd, fileFlag),
Format: mustString(cmd, formatFlag),
Select: mustString(cmd, selectFlag),
Map: fieldMap,
ID: id,
File: mustString(cmd, fileFlag),
Format: mustString(cmd, formatFlag),
Select: mustString(cmd, selectFlag),
Map: fieldMap,
Upsert: upsert,
ModelConfigKey: mustString(cmd, modelConfigKeyFlag),
Mode: syncdomain.VariationMode(mustString(cmd, modeFlag)),
}, nil
}),
}
Expand All @@ -78,6 +92,11 @@ func newLinkCmd(client resources.Client, kind syncdomain.Kind) *cobra.Command {
cmd.Flags().String(formatFlag, "", "Source format, for example python, typescript, markdown, or json")
cmd.Flags().String(selectFlag, "", "Source value to select, for example 'agents[id=\"support\"]'")
cmd.Flags().StringArray(mapFlag, nil, "Mapped field in field=path form. Repeat for more fields.")
cmd.Flags().Bool(upsertFlag, false, fmt.Sprintf("Create the %s in LaunchDarkly when it does not exist", noun))
if kind == syncdomain.KindVariation {
cmd.Flags().String(modelConfigKeyFlag, "", "Model config of a variation that --upsert creates. The create uses its latest version.")
cmd.Flags().String(modeFlag, "", "Mode of a config that --upsert creates: agent or completion. The default comes from the source.")
}
_ = cmd.MarkFlagRequired(fileFlag)
_ = cmd.MarkFlagRequired(formatFlag)
addApplyFlags(cmd, scope, false)
Expand Down
2 changes: 2 additions & 0 deletions cmd/sync/prompt.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,15 @@ const (
formatFlag = "format"
jsonSchemaFlag = "json-schema"
keyFlag = "key"
modeFlag = "mode"
modelConfigKeyFlag = "model-config-key"
nameFlag = "name"
noInputFlag = "no-input"
resolveFlag = "resolve"
selectFlag = "select"
mapFlag = "map"
toFlag = "to"
upsertFlag = "upsert"
yesFlag = "yes"
)

Expand Down
23 changes: 22 additions & 1 deletion cmd/sync/prompt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,13 @@ func TestPromptCommandUsesActionSubcommands(t *testing.T) {
link, _, err := command.Find([]string{"link"})
require.NoError(t, err)
assert.Nil(t, link.Flags().Lookup(toFlag))
assert.Nil(t, link.Flags().Lookup(modelConfigKeyFlag))
assert.NotNil(t, link.Flags().Lookup(upsertFlag))
assert.NotNil(t, link.Flags().Lookup(modelConfigKeyFlag), "a link with --upsert can name the model config of the new variation")
assert.NotNil(t, link.Flags().Lookup(modeFlag))
toolLink := newLinkCmd(nil, syncdomain.KindTool)
assert.NotNil(t, toolLink.Flags().Lookup(upsertFlag))
assert.Nil(t, toolLink.Flags().Lookup(modelConfigKeyFlag), "a tool has no model config")
assert.Nil(t, toolLink.Flags().Lookup(modeFlag))
}

func TestSourceLinkFlagsBuildTargetAndMappings(t *testing.T) {
Expand All @@ -57,6 +63,21 @@ func TestSourceLinkFlagsBuildTargetAndMappings(t *testing.T) {
assert.Equal(t, map[string]string{"prompt": "instructions"}, mappings)
}

func TestSourceLinkFlagsCarryTheUpsertIntent(t *testing.T) {
command := newLinkCmd(nil, syncdomain.KindVariation)
require.NoError(t, command.ParseFlags([]string{
"--file", "agents.py", "--format", "python", "--select", `agents[id="support"]`,
"--project", "production", "--config-key", "support", "--key", "default",
"--upsert", "--model-config-key", "gpt-5", "--mode", "completion",
}))

upsert, err := command.Flags().GetBool(upsertFlag)
require.NoError(t, err)
assert.True(t, upsert)
assert.Equal(t, "gpt-5", mustString(command, modelConfigKeyFlag))
assert.Equal(t, "completion", mustString(command, modeFlag))
}

func TestSyncProblemRendersStableFields(t *testing.T) {
problem := syncdomain.NewProblem(
syncdomain.ErrorPathNotFound,
Expand Down
8 changes: 4 additions & 4 deletions cmd/sync/scope_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,18 @@ var syncCommandFlags = map[string][]string{
"sync prompts detach": {"archive", "config-key", "key", "no-input", "project", "yes"},
"sync prompts link": {
"config-key", "conflict", "diff", "file", "format", "key", "map",
"no-input", "project", "resolve", "select", "yes",
"mode", "model-config-key", "no-input", "project", "resolve", "select", "upsert", "yes",
},
"sync tools": {"conflict", "diff", "dry-run", "key", "no-input", "project", "yes"},
"sync tools watch": {"conflict", "diff", "key", "no-input", "project", "yes"},
"sync tools add": {"description", "diff", "dry-run", "json-schema", "key", "no-input", "project", "yes"},
"sync tools detach": {"key", "no-input", "project", "yes"},
"sync tools link": {"conflict", "diff", "file", "format", "key", "map", "no-input", "project", "select", "yes"},
"sync tools link": {"conflict", "diff", "file", "format", "key", "map", "no-input", "project", "select", "upsert", "yes"},
"sync skills": {"conflict", "diff", "dry-run", "key", "no-input", "project", "yes"},
"sync skills watch": {"conflict", "diff", "key", "no-input", "project", "yes"},
"sync skills add": {"content", "description", "diff", "dry-run", "key", "name", "no-input", "project", "yes"},
"sync skills detach": {"key", "no-input", "project", "yes"},
"sync skills link": {"conflict", "diff", "file", "format", "key", "map", "no-input", "project", "select", "yes"},
"sync skills link": {"conflict", "diff", "file", "format", "key", "map", "no-input", "project", "select", "upsert", "yes"},
}

func TestSyncCommandsHaveExactFlags(t *testing.T) {
Expand Down Expand Up @@ -72,7 +72,7 @@ func TestSyncCommandsReadSyncFlagsOnlyFromTheCommandLine(t *testing.T) {
}
for _, name := range []string{
"config-key", "conflict", "content", "description", "file", "format", "json-schema", "key",
"map", "model-config-key", "name", "resolve", "select", "to",
"map", "mode", "model-config-key", "name", "resolve", "select", "to",
} {
t.Setenv("LD_"+strings.ToUpper(strings.ReplaceAll(name, "-", "_")), "from-environment")
}
Expand Down
16 changes: 16 additions & 0 deletions internal/sync/api/catalog.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package api
import (
"encoding/json"
"fmt"
"net/http"
"strconv"
"strings"

Expand Down Expand Up @@ -115,6 +116,21 @@ func (client Client) Config(projectKey, configKey string) (Config, error) {
return config, nil
}

// CreateConfig creates an AI Config with no variations. A sync creates one
// before the first variation of a linked source when the user asks for it.
func (client Client) CreateConfig(projectKey string, config Config) error {
request := mutation{
method: http.MethodPost, action: "create", resource: "config", key: config.Key,
body: struct {
Key string `json:"key"`
Name string `json:"name"`
Mode syncdomain.VariationMode `json:"mode"`
}{Key: config.Key, Name: config.Name, Mode: config.Mode},
}
_, err := client.mutate(request, projectPath(projectKey, "ai-configs")...)
return err
}

// ModelConfig returns the latest version of one model config.
func (client Client) ModelConfig(projectKey, modelConfigKey string) (ModelConfig, error) {
response, err := client.read(
Expand Down
15 changes: 15 additions & 0 deletions internal/sync/api/catalog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package api
import (
"encoding/json"
"errors"
"net/http"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -322,3 +323,17 @@ func mustCatalogJSON(t *testing.T, value any) []byte {

return data
}

func TestClientCreatesConfig(t *testing.T) {
transport := &recordingClient{Responses: [][]byte{[]byte(`{"key":"support","name":"Support","mode":"agent","variations":[]}`)}}
client := NewClient(transport, "token", "https://example.com")

err := client.CreateConfig("project", Config{Key: "support", Name: "Support", Mode: syncdomain.VariationModeAgent})

require.NoError(t, err)
require.Len(t, transport.Requests, 1)
request := transport.Requests[0]
assert.Equal(t, http.MethodPost, request.Method)
assert.Equal(t, "https://example.com/api/v2/projects/project/ai-configs", request.Path)
assert.JSONEq(t, `{"key":"support","name":"Support","mode":"agent"}`, string(request.Body))
}
Loading
Loading