diff --git a/internal/sync/prompt/acceptance_test.go b/internal/sync/prompt/acceptance_test.go index ed5267d0c..7c9e9272c 100644 --- a/internal/sync/prompt/acceptance_test.go +++ b/internal/sync/prompt/acceptance_test.go @@ -167,6 +167,20 @@ func (api *directAPI) MakeRequest( } return nil, api.createErr } + if strings.HasSuffix(path, "/ai-tools") && method == http.MethodGet { + items := make([]versionedTool, 0, len(api.tools)) + for _, key := range slices.Sorted(maps.Keys(api.tools)) { + items = append(items, api.tools[key]) + } + return json.Marshal(map[string]any{"items": items, "totalCount": len(items)}) + } + if strings.HasSuffix(path, "/ai-configs/skills") && method == http.MethodGet { + items := make([]versionedSkill, 0, len(api.skills)) + for _, key := range slices.Sorted(maps.Keys(api.skills)) { + items = append(items, api.skills[key]) + } + return json.Marshal(map[string]any{"items": items, "totalCount": len(items)}) + } if strings.HasSuffix(path, "/ai-tools") && method == http.MethodPost { var tool versionedTool if err := json.Unmarshal(body, &tool.Tool); err != nil { @@ -652,6 +666,39 @@ func TestPromptAttachesLatestSkillAsMarkdownFile(t *testing.T) { assert.Equal(t, "---\nkey: support\ndescription: Customer support guidance\n---\n\nFollow the support process.\n", string(content)) } +// An attach stops before the variation step when the project has no tool, +// so the user does not answer every prompt only to find an empty list. +func TestPromptAttachToolStopsWhenTheProjectHasNoTools(t *testing.T) { + root := initRepository(t) + baseline := variation("Baseline") + writeVariation(t, root, baseline, true) + writeManifest(t, root, baseline) + api := &directAPI{variation: pointer(baseline), tools: map[string]versionedTool{}} + + _, _, err := runPrompt(t, root, api, "attach", "tool", "--yes", "--no-input") + + require.Error(t, err) + assert.Contains(t, err.Error(), `project "production" has no tools to attach. Create one in LaunchDarkly first.`) + assert.Equal(t, 1, countRequests(api.requests, http.MethodGet, "/ai-tools"), "one read of the tool list") + assert.Empty(t, api.variation.Tools, "nothing was attached") +} + +// The same stop applies to skills. +func TestPromptAttachSkillStopsWhenTheProjectHasNoSkills(t *testing.T) { + root := initRepository(t) + baseline := variation("Baseline") + writeVariation(t, root, baseline, true) + writeManifest(t, root, baseline) + api := &directAPI{variation: pointer(baseline), skills: map[string]versionedSkill{}} + + _, _, err := runPrompt(t, root, api, "attach", "skill", "--yes", "--no-input") + + require.Error(t, err) + assert.Contains(t, err.Error(), `project "production" has no skills to attach. Create one in LaunchDarkly first.`) + assert.Equal(t, 1, countRequests(api.requests, http.MethodGet, "/ai-configs/skills"), "one read of the skill list") + assert.Empty(t, api.variation.Skills, "nothing was attached") +} + func TestPromptRejectsSkillAttachmentForCompletionVariation(t *testing.T) { root := initRepository(t) completion := variation("Completion") diff --git a/internal/sync/prompt/attach.go b/internal/sync/prompt/attach.go index 491183e62..e10bfa666 100644 --- a/internal/sync/prompt/attach.go +++ b/internal/sync/prompt/attach.go @@ -39,6 +39,12 @@ func attachToVariation(store synclocal.Store, client syncapi.Client, options att if err != nil { return err } + // A named key reads one resource, and a missing one gets its own error. + if options.Key == "" { + if err := requireAttachments(client, projectKey, options.Kind); err != nil { + return err + } + } target, err := selectManagedVariation(variations, projectKey, options) if err != nil { return err @@ -75,6 +81,20 @@ func attachToVariation(store synclocal.Store, client syncapi.Client, options att return err } +// requireAttachments stops an attach that has no key when the project has no +// tool or skill of the kind. It runs before the variation step, so that the +// user does not answer every prompt only to find an empty list at the end. +func requireAttachments(client syncapi.Client, projectKey string, kind syncdomain.AttachmentKind) error { + page, err := client.SearchAttachments(projectKey, kind, "", 1, 0) + if err != nil { + return err + } + if len(page.Items) == 0 && page.TotalCount == 0 { + return fmt.Errorf("project %q has no %ss to attach. Create one in LaunchDarkly first.", projectKey, kind) + } + return nil +} + // localAttachment reads the local file of an attachment. It returns nil when // the attachment has no local file. func localAttachment(repositoryRoot string, id ResourceID) (*syncdomain.Attachment, error) {