-
Notifications
You must be signed in to change notification settings - Fork 62
[wip] Add ai add & ai remove commands
#1538
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
Tomasz Turkowski (tturkowski)
wants to merge
17
commits into
main
Choose a base branch
from
feat/ai-add-remove
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
90a82e8
feat(ai): ai add installs bundled skills via skills.sh (global scope)
tturkowski 245e4c3
feat(ai): ai add installs bundled skills via skills.sh (global + proj…
tturkowski 57d7c3f
feat(ai): ai add installs git-delivered skills (resolve latest releas…
tturkowski 6cdaab1
feat(ai): gate git-skill installs on the owner compatibility check
tturkowski f8aae02
feat(ai): ai remove uninstalls CLI-recorded skills via skills.sh
tturkowski efcb822
refactor(ai): rename client->agent, drop issue-id comments, testify s…
tturkowski 0c2cfa1
refactor(ai): clearer error when a git skill is installed globally
tturkowski 94b3422
docs(ai): add manual test plan for ai add/remove
tturkowski b4cb6f5
manual testing instructions + script
tturkowski 9a55ea4
feat(ai): expose the ai command group in --help
tturkowski 46783f7
docs(ai): ai command group is no longer hidden
tturkowski 04d450d
fix(ai): address review — skills op in errors, bounded download, getw…
tturkowski be7a31e
feat(ai): ai remove trusts recorded state; warn on failed state write
tturkowski 6ad1913
fix(ai): warn when an installed skill cannot be recorded
tturkowski e0558d8
docs(ai): align manual test sandbox and harden the runner
tturkowski 1a2be69
Merge branch 'main' into feat/ai-add-remove
tturkowski 3845de9
Merge branch 'main' into feat/ai-add-remove
tturkowski File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,218 @@ | ||
| package ai | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "errors" | ||
| "fmt" | ||
| "io" | ||
| "os" | ||
| "strings" | ||
|
|
||
| "github.com/spf13/cobra" | ||
|
|
||
| "github.com/shopware/shopware-cli/internal/ai/directory" | ||
| "github.com/shopware/shopware-cli/internal/ai/state" | ||
| ) | ||
|
|
||
| // The CLI hardcodes no agent names: which agents exist is skills.sh's business, | ||
| // so a new one it supports works without a CLI change. | ||
|
|
||
| // addResult is the machine-readable shape of `ai add` (--format json). | ||
| type addResult struct { | ||
| Name string `json:"name"` | ||
| Agent string `json:"agent"` | ||
| Scope state.Scope `json:"scope"` | ||
| RequestedTag string `json:"requestedTag"` | ||
| ResolvedRevision string `json:"resolvedRevision"` | ||
| DryRun bool `json:"dryRun"` | ||
| Command []string `json:"command"` | ||
| } | ||
|
|
||
| var aiAddCmd = &cobra.Command{ | ||
| Use: "add <name>[@<tag>]", | ||
| Short: "Install a Shopware AI integration into an AI agent", | ||
| Args: cobra.ExactArgs(1), | ||
| SilenceUsage: true, | ||
| RunE: func(cmd *cobra.Command, args []string) error { | ||
| format, err := resolveFormat(cmd) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| name, tag := splitNameTag(args[0]) | ||
| agent, _ := cmd.Flags().GetString("agent") | ||
| global, _ := cmd.Flags().GetBool("global") | ||
| dryRun, _ := cmd.Flags().GetBool("dry-run") | ||
|
|
||
| entry, ok := directory.Load().Get(name) | ||
| if !ok { | ||
| return fmt.Errorf("unknown integration %q (see `shopware-cli ai list`)", name) | ||
| } | ||
|
|
||
| // Skills only for now (MCP arrives later). The agent value is passed | ||
| // straight to skills.sh. | ||
| if entry.Type != directory.TypeSkill { | ||
| return fmt.Errorf("installing %q is not supported yet (only skills for now)", name) | ||
| } | ||
| // A git skill's compatibility check runs against a project, so there is | ||
| // nothing to check for a global install. | ||
| if entry.Delivery.Kind == directory.DeliveryGit && global { | ||
| return fmt.Errorf("%q must be installed into a project, not globally: it checks compatibility against that project (omit --global)", entry.Name) | ||
| } | ||
| if agent == "" { | ||
| return errors.New("specify the target agent with --agent (e.g. --agent claude-code)") | ||
| } | ||
|
|
||
| // State lives where the config lives: a --global install and its state | ||
| // go to the user config dir; a project install and its state go to the | ||
| // current directory, matching where skills.sh writes the agent config. | ||
| scope := state.ScopeGlobal | ||
| readState := state.Read | ||
| saveState := state.Save | ||
| if !global { | ||
| root, err := os.Getwd() | ||
| if err != nil { | ||
| return err | ||
| } | ||
| scope = state.ScopeProject | ||
| readState = func() (state.File, error) { return state.ReadProject(root) } | ||
| saveState = func(f state.File) error { return state.SaveProject(root, f) } | ||
| } | ||
|
|
||
| // Resolve the source repo and the ref to install. A bundled skill's | ||
| // version follows the CLI; a git skill uses the explicit tag or the | ||
| // latest stable release from its repository. | ||
| var source, ref string | ||
| switch entry.Delivery.Kind { | ||
| case directory.DeliveryBundled: | ||
| ref = tag | ||
| if ref == "" { | ||
| ref = cmd.Root().Version | ||
| } | ||
| source = "shopware/shopware-cli" | ||
| case directory.DeliveryGit: | ||
| ref = tag | ||
| if ref == "" { | ||
| if ref, err = resolveLatestTag(cmd.Context(), entry.Delivery.Repository); err != nil { | ||
| return err | ||
| } | ||
| } | ||
| source = ownerRepo(entry.Delivery.Repository) | ||
| default: | ||
| return fmt.Errorf("unsupported delivery %q", entry.Delivery.Kind) | ||
| } | ||
| if ref != "" { | ||
| source += "@" + ref | ||
| } | ||
|
|
||
| // skills.sh must never prompt: this command already supplies the skill, | ||
| // agent and scope, so its confirmation prompts are always skipped. | ||
| argv := skillsAddArgs(source, entry.Name, agent, global, true) | ||
|
|
||
| result := addResult{ | ||
| Name: entry.Name, | ||
| Agent: agent, | ||
| Scope: scope, | ||
| RequestedTag: tag, | ||
| ResolvedRevision: ref, | ||
| DryRun: dryRun, | ||
| Command: argv, | ||
| } | ||
|
|
||
| if dryRun { | ||
| return writeAddResult(cmd.OutOrStdout(), format, result) | ||
| } | ||
|
|
||
| current, err := readState() | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| // Idempotent: the same integration, agent, scope and revision is a no-op. | ||
| if !isInstalled(current, result) { | ||
| // A git skill declares an owner-maintained compatibility check; run | ||
| // it against the project before installing anything. | ||
| if entry.Delivery.Kind == directory.DeliveryGit && entry.Compatibility != nil { | ||
| projectDir, err := os.Getwd() | ||
| if err != nil { | ||
| return err | ||
| } | ||
| if err := runCompatCheck(cmd.Context(), ownerRepo(entry.Delivery.Repository), entry.Name, ref, projectDir, cmd.ErrOrStderr()); err != nil { | ||
| return err | ||
| } | ||
| } | ||
|
|
||
| if err := runSkills(cmd.Context(), argv, cmd.ErrOrStderr()); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| next := state.Upsert(current, state.InstalledEntry{ | ||
| Name: result.Name, | ||
| Agent: result.Agent, | ||
| Scope: result.Scope, | ||
| RequestedTag: result.RequestedTag, | ||
| ResolvedRevision: result.ResolvedRevision, | ||
| }) | ||
| if err := saveState(next); err != nil { | ||
| _, _ = fmt.Fprintf(cmd.ErrOrStderr(), "warning: %s was installed but could not be recorded (%v); re-run `ai add` to record it\n", result.Name, err) | ||
| return err | ||
| } | ||
| } | ||
|
|
||
| return writeAddResult(cmd.OutOrStdout(), format, result) | ||
| }, | ||
| } | ||
|
|
||
| // splitNameTag splits "name@tag" into its parts; a missing tag yields "". | ||
| func splitNameTag(s string) (name, tag string) { | ||
| if i := strings.IndexByte(s, '@'); i >= 0 { | ||
| return s[:i], s[i+1:] | ||
| } | ||
|
|
||
| return s, "" | ||
| } | ||
|
|
||
| // isInstalled reports whether the state already records this exact install. | ||
| func isInstalled(f state.File, r addResult) bool { | ||
| for _, e := range f.Installed { | ||
| if e.Name == r.Name && e.Agent == r.Agent && e.Scope == r.Scope && e.ResolvedRevision == r.ResolvedRevision { | ||
| return true | ||
| } | ||
| } | ||
|
|
||
| return false | ||
| } | ||
|
|
||
| func writeAddResult(w io.Writer, format string, r addResult) error { | ||
| if format == formatJSON { | ||
| out, err := json.Marshal(r) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| _, err = fmt.Fprintln(w, string(out)) | ||
|
|
||
| return err | ||
| } | ||
|
|
||
| if r.DryRun { | ||
| _, err := fmt.Fprintf(w, "[dry-run] would install %s for %s (%s):\n %s\n", r.Name, r.Agent, r.Scope, strings.Join(r.Command, " ")) | ||
|
|
||
| return err | ||
| } | ||
|
|
||
| rev := "" | ||
| if r.ResolvedRevision != "" { | ||
| rev = " @" + r.ResolvedRevision | ||
| } | ||
| _, err := fmt.Fprintf(w, "Installed %s for %s (%s)%s\n", r.Name, r.Agent, r.Scope, rev) | ||
|
|
||
| return err | ||
| } | ||
|
|
||
| func init() { | ||
| aiRootCmd.AddCommand(aiAddCmd) | ||
| aiAddCmd.Flags().String("agent", "", "Target AI agent (e.g. claude-code)") | ||
| aiAddCmd.Flags().Bool("global", false, "Install at user level instead of the current project") | ||
| aiAddCmd.Flags().Bool("dry-run", false, "Show what would be installed without changing anything") | ||
| addFormatFlag(aiAddCmd) | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.