|
| 1 | +package skillcommands |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "net/http" |
| 6 | + "strings" |
| 7 | + |
| 8 | + consolerest "github.com/langgenius/mosoo-connector/internal/generated/consolerest" |
| 9 | + latheruntime "github.com/lathe-cli/lathe/pkg/runtime" |
| 10 | + "github.com/spf13/cobra" |
| 11 | +) |
| 12 | + |
| 13 | +const ( |
| 14 | + inspectOperationID = "Skill_Inspect" |
| 15 | + packageOperationID = "Skill_Package" |
| 16 | +) |
| 17 | + |
| 18 | +// Install mounts hand-maintained replacements for skill package upload commands |
| 19 | +// that require multipart/form-data file parts. Lathe currently exposes the |
| 20 | +// multipart request body but cannot build the file part from generated specs. |
| 21 | +func Install(root *cobra.Command) error { |
| 22 | + surface := findChild(root, "console-rest") |
| 23 | + if surface == nil { |
| 24 | + return fmt.Errorf("console-rest command tree is not mounted") |
| 25 | + } |
| 26 | + skills := findChild(surface, "skills") |
| 27 | + if skills == nil { |
| 28 | + return fmt.Errorf("console-rest skills command tree is not mounted") |
| 29 | + } |
| 30 | + |
| 31 | + replaceCommand(skills, "inspect", newInspectCommand()) |
| 32 | + replaceCommand(skills, "package", newPackageCommand()) |
| 33 | + return nil |
| 34 | +} |
| 35 | + |
| 36 | +type uploadOptions struct { |
| 37 | + appID string |
| 38 | + file string |
| 39 | + githubURL string |
| 40 | + skillID string |
| 41 | +} |
| 42 | + |
| 43 | +func newInspectCommand() *cobra.Command { |
| 44 | + var opts uploadOptions |
| 45 | + cmd := &cobra.Command{ |
| 46 | + Use: "inspect", |
| 47 | + Short: "Inspect a skill package from upload or GitHub URL", |
| 48 | + Long: "Inspect a skill package by uploading a local package file or by passing a GitHub URL.", |
| 49 | + Example: "mosoo console-rest skills inspect --file ./mosoo-skill.zip -o json", |
| 50 | + RunE: func(cmd *cobra.Command, _ []string) error { |
| 51 | + if err := opts.validate(false); err != nil { |
| 52 | + return err |
| 53 | + } |
| 54 | + client, err := NewClient(cmd) |
| 55 | + if err != nil { |
| 56 | + return err |
| 57 | + } |
| 58 | + data, err := client.Inspect(cmd.Context(), opts) |
| 59 | + if err != nil { |
| 60 | + return err |
| 61 | + } |
| 62 | + format, _ := cmd.Root().PersistentFlags().GetString("output") |
| 63 | + return latheruntime.FormatOutput(data, format, cmd.OutOrStdout(), inspectCatalogSpec(cmd).Output) |
| 64 | + }, |
| 65 | + } |
| 66 | + addSourceFlags(cmd, &opts) |
| 67 | + latheruntime.AttachCatalogCommand(cmd, "console-rest", inspectCatalogSpec(cmd)) |
| 68 | + return cmd |
| 69 | +} |
| 70 | + |
| 71 | +func newPackageCommand() *cobra.Command { |
| 72 | + var opts uploadOptions |
| 73 | + cmd := &cobra.Command{ |
| 74 | + Use: "package", |
| 75 | + Short: "Create or update a skill package upload", |
| 76 | + Long: "Create or update a skill package by uploading a local package file or by passing a GitHub URL.", |
| 77 | + Example: "mosoo console-rest skills package --app-id <app-id> --file ./mosoo-skill.zip -o json", |
| 78 | + RunE: func(cmd *cobra.Command, _ []string) error { |
| 79 | + if err := opts.validate(true); err != nil { |
| 80 | + return err |
| 81 | + } |
| 82 | + client, err := NewClient(cmd) |
| 83 | + if err != nil { |
| 84 | + return err |
| 85 | + } |
| 86 | + data, err := client.Package(cmd.Context(), opts) |
| 87 | + if err != nil { |
| 88 | + return err |
| 89 | + } |
| 90 | + format, _ := cmd.Root().PersistentFlags().GetString("output") |
| 91 | + return latheruntime.FormatOutput(data, format, cmd.OutOrStdout(), packageCatalogSpec(cmd).Output) |
| 92 | + }, |
| 93 | + } |
| 94 | + flags := cmd.Flags() |
| 95 | + flags.StringVar(&opts.appID, "app-id", "", "App ID that owns the skill. (form, required, ulid)") |
| 96 | + flags.StringVar(&opts.skillID, "skill-id", "", "Existing skill ID to update. (form, ulid)") |
| 97 | + addSourceFlags(cmd, &opts) |
| 98 | + _ = cmd.MarkFlagRequired("app-id") |
| 99 | + latheruntime.AttachCatalogCommand(cmd, "console-rest", packageCatalogSpec(cmd)) |
| 100 | + return cmd |
| 101 | +} |
| 102 | + |
| 103 | +func addSourceFlags(cmd *cobra.Command, opts *uploadOptions) { |
| 104 | + flags := cmd.Flags() |
| 105 | + flags.StringVarP(&opts.file, "file", "f", "", "Skill package file path (.zip, .skill, or SKILL.md)") |
| 106 | + flags.StringVar(&opts.githubURL, "github-url", "", "GitHub URL for the skill package source") |
| 107 | +} |
| 108 | + |
| 109 | +func (o uploadOptions) validate(requireApp bool) error { |
| 110 | + if requireApp && strings.TrimSpace(o.appID) == "" { |
| 111 | + return fmt.Errorf("--app-id is required") |
| 112 | + } |
| 113 | + hasFile := strings.TrimSpace(o.file) != "" |
| 114 | + hasURL := strings.TrimSpace(o.githubURL) != "" |
| 115 | + switch { |
| 116 | + case hasFile && hasURL: |
| 117 | + return fmt.Errorf("only one of --file or --github-url can be set") |
| 118 | + case !hasFile && !hasURL: |
| 119 | + return fmt.Errorf("one of --file or --github-url is required") |
| 120 | + default: |
| 121 | + return nil |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +func inspectCatalogSpec(cmd *cobra.Command) latheruntime.CommandSpec { |
| 126 | + spec := generatedSpec(inspectOperationID) |
| 127 | + spec.Long = cmd.Long |
| 128 | + spec.Example = cmd.Example |
| 129 | + spec.Params = []latheruntime.ParamSpec{ |
| 130 | + {Name: "file", Flag: "file", In: latheruntime.InFormData, GoType: "string", Help: "Skill package file path (.zip, .skill, or SKILL.md)"}, |
| 131 | + {Name: "githubUrl", Flag: "github-url", In: latheruntime.InFormData, GoType: "string", Help: "GitHub URL for the skill package source"}, |
| 132 | + } |
| 133 | + spec.KnownErrors = skillKnownErrors() |
| 134 | + return spec |
| 135 | +} |
| 136 | + |
| 137 | +func packageCatalogSpec(cmd *cobra.Command) latheruntime.CommandSpec { |
| 138 | + spec := generatedSpec(packageOperationID) |
| 139 | + spec.Long = cmd.Long |
| 140 | + spec.Example = cmd.Example |
| 141 | + spec.Params = []latheruntime.ParamSpec{ |
| 142 | + {Name: "appId", Flag: "app-id", In: latheruntime.InFormData, GoType: "string", Help: "App ID that owns the skill. (form, required, ulid)", Required: true, Format: "ulid"}, |
| 143 | + {Name: "skillId", Flag: "skill-id", In: latheruntime.InFormData, GoType: "string", Help: "Existing skill ID to update. (form, ulid)", Format: "ulid"}, |
| 144 | + {Name: "file", Flag: "file", In: latheruntime.InFormData, GoType: "string", Help: "Skill package file path (.zip, .skill, or SKILL.md)"}, |
| 145 | + {Name: "githubUrl", Flag: "github-url", In: latheruntime.InFormData, GoType: "string", Help: "GitHub URL for the skill package source"}, |
| 146 | + } |
| 147 | + spec.KnownErrors = skillKnownErrors() |
| 148 | + return spec |
| 149 | +} |
| 150 | + |
| 151 | +func skillKnownErrors() []latheruntime.KnownError { |
| 152 | + return []latheruntime.KnownError{ |
| 153 | + {Status: http.StatusBadRequest, Cause: "Invalid package source, malformed multipart form, or unsupported skill package layout."}, |
| 154 | + {Status: http.StatusUnauthorized, Cause: "Missing, invalid, or revoked personal access token."}, |
| 155 | + } |
| 156 | +} |
| 157 | + |
| 158 | +func generatedSpec(operationID string) latheruntime.CommandSpec { |
| 159 | + for _, spec := range consolerest.Specs { |
| 160 | + if spec.OperationID == operationID { |
| 161 | + return spec |
| 162 | + } |
| 163 | + } |
| 164 | + panic(fmt.Sprintf("missing generated console-rest spec %q", operationID)) |
| 165 | +} |
| 166 | + |
| 167 | +func replaceCommand(parent *cobra.Command, name string, replacement *cobra.Command) { |
| 168 | + if existing := findChild(parent, name); existing != nil { |
| 169 | + parent.RemoveCommand(existing) |
| 170 | + } |
| 171 | + parent.AddCommand(replacement) |
| 172 | +} |
| 173 | + |
| 174 | +func findChild(parent *cobra.Command, name string) *cobra.Command { |
| 175 | + for _, child := range parent.Commands() { |
| 176 | + if child.Name() == name { |
| 177 | + return child |
| 178 | + } |
| 179 | + } |
| 180 | + return nil |
| 181 | +} |
0 commit comments