♻️ Quality: internal/skillcreate without smells (5 → 0)
- Main as parseArgs, cliArgs.flow, cliArgs.answer and failed (one place that prints why nothing was written); args.go - the line wizard's run with take; linewizard.go - Flow.Set with a flowSetters table (overwrite: no keeps no answer) Checked old vs new binary on 13 mm skillcreate runs (-h, unknown flag, --yes complete and incomplete, missing --body-file, line mode, too many keywords, defaults, overwrite no and yes, config error, bad name, -o -, input ending early): identical output, exit codes and files written. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
96ba0ec parent: 5a0f4b1 added
internal/skillcreate/args.go +88 -0 | new file mode 100644 | ||
| @@ -0,0 +1,88 @@ | ||
| 1 | +package skillcreate | |
| 2 | + | |
| 3 | +import ( | |
| 4 | + "bufio" | |
| 5 | + "context" | |
| 6 | + "errors" | |
| 7 | + "flag" | |
| 8 | + "fmt" | |
| 9 | + "io" | |
| 10 | + "os" | |
| 11 | +) | |
| 12 | + | |
| 13 | +// cliArgs are mm skillcreate's flags. | |
| 14 | +type cliArgs struct { | |
| 15 | + spec Spec | |
| 16 | + configPath, bodyFile, output string | |
| 17 | + yes, line bool | |
| 18 | +} | |
| 19 | + | |
| 20 | +// parseArgs reads the flags. ok is false when Main must stop with code: 0 | |
| 21 | +// for -h, 2 for a flag it does not know. | |
| 22 | +func parseArgs(args []string, stdout io.Writer) (a cliArgs, code int, ok bool) { | |
| 23 | + fs := flag.NewFlagSet("mm skillcreate", flag.ContinueOnError) | |
| 24 | + fs.SetOutput(stdout) | |
| 25 | + fs.StringVar(&a.spec.Name, "name", "", "the skill's name; written lower case with dashes (Vulcan Salute → vulcan-salute)") | |
| 26 | + fs.StringVar(&a.spec.Description, "description", "", "one line, shown in the catalogue the model chooses from") | |
| 27 | + fs.StringVar(&a.spec.Keywords, "keywords", "", "comma-separated words a user might type; the first 8 reach the catalogue") | |
| 28 | + fs.StringVar(&a.spec.Body, "body", "", "the skill's text, written under \"## What to do\"") | |
| 29 | + fs.StringVar(&a.bodyFile, "body-file", "", "read the skill's text from this file") | |
| 30 | + fs.StringVar(&a.configPath, "config", "", "the agent file whose skillsDir is used (default: as mm finds it — $AGENT_CONFIG, ./agent.yaml, ./.mm/agent.yaml)") | |
| 31 | + fs.StringVar(&a.output, "o", "", "the file to write (default <skillsDir>/<name>/SKILL.md; - for stdout, which creates nothing)") | |
| 32 | + fs.BoolVar(&a.yes, "yes", false, "ask nothing: name, description and text must be given; an existing skill is never replaced") | |
| 33 | + fs.BoolVar(&a.line, "line", false, "ask line by line even in a terminal (no window)") | |
| 34 | + if err := fs.Parse(args); err != nil { | |
| 35 | + if errors.Is(err, flag.ErrHelp) { | |
| 36 | + return a, 0, false | |
| 37 | + } | |
| 38 | + return a, 2, false | |
| 39 | + } | |
| 40 | + return a, 0, true | |
| 41 | +} | |
| 42 | + | |
| 43 | +// flow is the flow the flags start: the skill's text read from --body-file, | |
| 44 | +// the place found from the agent's config, the flags' answers recorded. | |
| 45 | +func (a cliArgs) flow() (*Flow, error) { | |
| 46 | + if a.bodyFile != "" { | |
| 47 | + data, err := os.ReadFile(a.bodyFile) | |
| 48 | + if err != nil { | |
| 49 | + return nil, err | |
| 50 | + } | |
| 51 | + a.spec.Body = string(data) | |
| 52 | + } | |
| 53 | + place, err := Locate(a.configPath) | |
| 54 | + if err != nil { | |
| 55 | + return nil, fmt.Errorf("config error: %v", err) | |
| 56 | + } | |
| 57 | + flow := NewFlow(place, Spec{}, a.output) | |
| 58 | + return flow, flow.SetGiven(a.spec) | |
| 59 | +} | |
| 60 | + | |
| 61 | +// errNothingWritten: the window was closed before the skill was written. | |
| 62 | +var errNothingWritten = errors.New("nothing written") | |
| 63 | + | |
| 64 | +// answer answers the questions the flags left: with the defaults (--yes), | |
| 65 | +// in the window in a terminal, line by line otherwise or with --line. | |
| 66 | +func (a cliArgs) answer(flow *Flow, o Options, stdin io.Reader, stdout io.Writer) error { | |
| 67 | + switch { | |
| 68 | + case a.yes: | |
| 69 | + return flow.takeDefaults() | |
| 70 | + case o.Window != nil && !a.line && o.Terminal != nil && o.Terminal(): | |
| 71 | + done, err := o.Window(context.Background(), flow) | |
| 72 | + if err == nil && !done { | |
| 73 | + err = errNothingWritten | |
| 74 | + } | |
| 75 | + return err | |
| 76 | + } | |
| 77 | + return (&lineWizard{in: bufio.NewReader(stdin), out: stdout}).run(flow) | |
| 78 | +} | |
| 79 | + | |
| 80 | +// failed says why nothing was written, and is the exit code: 1. | |
| 81 | +func failed(stdout io.Writer, err error) int { | |
| 82 | + if errors.Is(err, errNothingWritten) { | |
| 83 | + fmt.Fprintln(stdout, err.Error()) | |
| 84 | + } else { | |
| 85 | + fmt.Fprintln(stdout, "⚠️ "+err.Error()) | |
| 86 | + } | |
| 87 | + return 1 | |
| 88 | +} | |
| new file mode 100644 | |||
| @@ -0,0 +1,88 @@ | |||
| 1 | +package skillcreate | ||
| 2 | + | ||
| 3 | +import ( | ||
| 4 | + "bufio" | ||
| 5 | + "context" | ||
| 6 | + "errors" | ||
| 7 | + "flag" | ||
| 8 | + "fmt" | ||
| 9 | + "io" | ||
| 10 | + "os" | ||
| 11 | +) | ||
| 12 | + | ||
| 13 | +// cliArgs are mm skillcreate's flags. | ||
| 14 | +type cliArgs struct { | ||
| 15 | + spec Spec | ||
| 16 | + configPath, bodyFile, output string | ||
| 17 | + yes, line bool | ||
| 18 | +} | ||
| 19 | + | ||
| 20 | +// parseArgs reads the flags. ok is false when Main must stop with code: 0 | ||
| 21 | +// for -h, 2 for a flag it does not know. | ||
| 22 | +func parseArgs(args []string, stdout io.Writer) (a cliArgs, code int, ok bool) { | ||
| 23 | + fs := flag.NewFlagSet("mm skillcreate", flag.ContinueOnError) | ||
| 24 | + fs.SetOutput(stdout) | ||
| 25 | + fs.StringVar(&a.spec.Name, "name", "", "the skill's name; written lower case with dashes (Vulcan Salute → vulcan-salute)") | ||
| 26 | + fs.StringVar(&a.spec.Description, "description", "", "one line, shown in the catalogue the model chooses from") | ||
| 27 | + fs.StringVar(&a.spec.Keywords, "keywords", "", "comma-separated words a user might type; the first 8 reach the catalogue") | ||
| 28 | + fs.StringVar(&a.spec.Body, "body", "", "the skill's text, written under \"## What to do\"") | ||
| 29 | + fs.StringVar(&a.bodyFile, "body-file", "", "read the skill's text from this file") | ||
| 30 | + fs.StringVar(&a.configPath, "config", "", "the agent file whose skillsDir is used (default: as mm finds it — $AGENT_CONFIG, ./agent.yaml, ./.mm/agent.yaml)") | ||
| 31 | + fs.StringVar(&a.output, "o", "", "the file to write (default <skillsDir>/<name>/SKILL.md; - for stdout, which creates nothing)") | ||
| 32 | + fs.BoolVar(&a.yes, "yes", false, "ask nothing: name, description and text must be given; an existing skill is never replaced") | ||
| 33 | + fs.BoolVar(&a.line, "line", false, "ask line by line even in a terminal (no window)") | ||
| 34 | + if err := fs.Parse(args); err != nil { | ||
| 35 | + if errors.Is(err, flag.ErrHelp) { | ||
| 36 | + return a, 0, false | ||
| 37 | + } | ||
| 38 | + return a, 2, false | ||
| 39 | + } | ||
| 40 | + return a, 0, true | ||
| 41 | +} | ||
| 42 | + | ||
| 43 | +// flow is the flow the flags start: the skill's text read from --body-file, | ||
| 44 | +// the place found from the agent's config, the flags' answers recorded. | ||
| 45 | +func (a cliArgs) flow() (*Flow, error) { | ||
| 46 | + if a.bodyFile != "" { | ||
| 47 | + data, err := os.ReadFile(a.bodyFile) | ||
| 48 | + if err != nil { | ||
| 49 | + return nil, err | ||
| 50 | + } | ||
| 51 | + a.spec.Body = string(data) | ||
| 52 | + } | ||
| 53 | + place, err := Locate(a.configPath) | ||
| 54 | + if err != nil { | ||
| 55 | + return nil, fmt.Errorf("config error: %v", err) | ||
| 56 | + } | ||
| 57 | + flow := NewFlow(place, Spec{}, a.output) | ||
| 58 | + return flow, flow.SetGiven(a.spec) | ||
| 59 | +} | ||
| 60 | + | ||
| 61 | +// errNothingWritten: the window was closed before the skill was written. | ||
| 62 | +var errNothingWritten = errors.New("nothing written") | ||
| 63 | + | ||
| 64 | +// answer answers the questions the flags left: with the defaults (--yes), | ||
| 65 | +// in the window in a terminal, line by line otherwise or with --line. | ||
| 66 | +func (a cliArgs) answer(flow *Flow, o Options, stdin io.Reader, stdout io.Writer) error { | ||
| 67 | + switch { | ||
| 68 | + case a.yes: | ||
| 69 | + return flow.takeDefaults() | ||
| 70 | + case o.Window != nil && !a.line && o.Terminal != nil && o.Terminal(): | ||
| 71 | + done, err := o.Window(context.Background(), flow) | ||
| 72 | + if err == nil && !done { | ||
| 73 | + err = errNothingWritten | ||
| 74 | + } | ||
| 75 | + return err | ||
| 76 | + } | ||
| 77 | + return (&lineWizard{in: bufio.NewReader(stdin), out: stdout}).run(flow) | ||
| 78 | +} | ||
| 79 | + | ||
| 80 | +// failed says why nothing was written, and is the exit code: 1. | ||
| 81 | +func failed(stdout io.Writer, err error) int { | ||
| 82 | + if errors.Is(err, errNothingWritten) { | ||
| 83 | + fmt.Fprintln(stdout, err.Error()) | ||
| 84 | + } else { | ||
| 85 | + fmt.Fprintln(stdout, "⚠️ "+err.Error()) | ||
| 86 | + } | ||
| 87 | + return 1 | ||
| 88 | +} | ||
modified
internal/skillcreate/flow.go +32 -14 | @@ -133,41 +133,59 @@ func (f *Flow) Set(key, value string) error { | ||
| 133 | 133 | if strings.TrimSpace(value) == "" { |
| 134 | 134 | return fmt.Errorf("%s: an answer is needed", key) |
| 135 | 135 | } |
| 136 | - switch key { | |
| 137 | - case "name": | |
| 136 | + set, ok := flowSetters[key] | |
| 137 | + if !ok { | |
| 138 | + return fmt.Errorf("no question %q", key) | |
| 139 | + } | |
| 140 | + if keep, err := set(f, value); err != nil || !keep { | |
| 141 | + return err | |
| 142 | + } | |
| 143 | + f.answer[key] = value | |
| 144 | + return nil | |
| 145 | +} | |
| 146 | + | |
| 147 | +// flowSetters answer each question; keep is false for an answer that is not | |
| 148 | +// kept as one (overwrite: no sends back to the name question). | |
| 149 | +var flowSetters = map[string]func(f *Flow, value string) (keep bool, err error){ | |
| 150 | + "name": func(f *Flow, value string) (bool, error) { | |
| 138 | 151 | if err := CheckName(value); err != nil { |
| 139 | - return err | |
| 152 | + return false, err | |
| 140 | 153 | } |
| 141 | 154 | if Normalize(value) != Normalize(f.spec.Name) { |
| 142 | 155 | f.forgetOverwrite() |
| 143 | 156 | } |
| 144 | 157 | f.spec.Name = value |
| 145 | - case "overwrite": | |
| 158 | + return true, nil | |
| 159 | + }, | |
| 160 | + "overwrite": func(f *Flow, value string) (bool, error) { | |
| 146 | 161 | if value != "yes" && value != "no" { |
| 147 | - return fmt.Errorf("overwrite: answer yes or no, not %q", value) | |
| 162 | + return false, fmt.Errorf("overwrite: answer yes or no, not %q", value) | |
| 148 | 163 | } |
| 149 | 164 | if value == "no" { |
| 150 | 165 | // Back to the name question, even for a name given by a flag. |
| 151 | 166 | f.spec.Name = "" |
| 152 | 167 | delete(f.given, "name") |
| 153 | 168 | delete(f.answer, "name") |
| 154 | - return nil | |
| 169 | + return false, nil | |
| 155 | 170 | } |
| 156 | 171 | f.overwrite = true |
| 157 | - case "description": | |
| 172 | + return true, nil | |
| 173 | + }, | |
| 174 | + "description": func(f *Flow, value string) (bool, error) { | |
| 158 | 175 | f.spec.Description = value |
| 159 | - case "keywords": | |
| 176 | + return true, nil | |
| 177 | + }, | |
| 178 | + "keywords": func(f *Flow, value string) (bool, error) { | |
| 160 | 179 | f.spec.Keywords = "" |
| 161 | 180 | if value != noKeywords { |
| 162 | 181 | f.spec.Keywords = value |
| 163 | 182 | } |
| 164 | - case "body": | |
| 183 | + return true, nil | |
| 184 | + }, | |
| 185 | + "body": func(f *Flow, value string) (bool, error) { | |
| 165 | 186 | f.spec.Body = value |
| 166 | - default: | |
| 167 | - return fmt.Errorf("no question %q", key) | |
| 168 | - } | |
| 169 | - f.answer[key] = value | |
| 170 | - return nil | |
| 187 | + return true, nil | |
| 188 | + }, | |
| 171 | 189 | } |
| 172 | 190 | |
| 173 | 191 | // Unset takes an answer back (the window's Esc), unless a flag gave it. |
| @@ -133,41 +133,59 @@ func (f *Flow) Set(key, value string) error { | |||
| 133 | if strings.TrimSpace(value) == "" { | 133 | if strings.TrimSpace(value) == "" { |
| 134 | return fmt.Errorf("%s: an answer is needed", key) | 134 | return fmt.Errorf("%s: an answer is needed", key) |
| 135 | } | 135 | } |
| 136 | - switch key { | 136 | + set, ok := flowSetters[key] |
| 137 | - case "name": | 137 | + if !ok { |
| 138 | + return fmt.Errorf("no question %q", key) | ||
| 139 | + } | ||
| 140 | + if keep, err := set(f, value); err != nil || !keep { | ||
| 141 | + return err | ||
| 142 | + } | ||
| 143 | + f.answer[key] = value | ||
| 144 | + return nil | ||
| 145 | +} | ||
| 146 | + | ||
| 147 | +// flowSetters answer each question; keep is false for an answer that is not | ||
| 148 | +// kept as one (overwrite: no sends back to the name question). | ||
| 149 | +var flowSetters = map[string]func(f *Flow, value string) (keep bool, err error){ | ||
| 150 | + "name": func(f *Flow, value string) (bool, error) { | ||
| 138 | if err := CheckName(value); err != nil { | 151 | if err := CheckName(value); err != nil { |
| 139 | - return err | 152 | + return false, err |
| 140 | } | 153 | } |
| 141 | if Normalize(value) != Normalize(f.spec.Name) { | 154 | if Normalize(value) != Normalize(f.spec.Name) { |
| 142 | f.forgetOverwrite() | 155 | f.forgetOverwrite() |
| 143 | } | 156 | } |
| 144 | f.spec.Name = value | 157 | f.spec.Name = value |
| 145 | - case "overwrite": | 158 | + return true, nil |
| 159 | + }, | ||
| 160 | + "overwrite": func(f *Flow, value string) (bool, error) { | ||
| 146 | if value != "yes" && value != "no" { | 161 | if value != "yes" && value != "no" { |
| 147 | - return fmt.Errorf("overwrite: answer yes or no, not %q", value) | 162 | + return false, fmt.Errorf("overwrite: answer yes or no, not %q", value) |
| 148 | } | 163 | } |
| 149 | if value == "no" { | 164 | if value == "no" { |
| 150 | // Back to the name question, even for a name given by a flag. | 165 | // Back to the name question, even for a name given by a flag. |
| 151 | f.spec.Name = "" | 166 | f.spec.Name = "" |
| 152 | delete(f.given, "name") | 167 | delete(f.given, "name") |
| 153 | delete(f.answer, "name") | 168 | delete(f.answer, "name") |
| 154 | - return nil | 169 | + return false, nil |
| 155 | } | 170 | } |
| 156 | f.overwrite = true | 171 | f.overwrite = true |
| 157 | - case "description": | 172 | + return true, nil |
| 173 | + }, | ||
| 174 | + "description": func(f *Flow, value string) (bool, error) { | ||
| 158 | f.spec.Description = value | 175 | f.spec.Description = value |
| 159 | - case "keywords": | 176 | + return true, nil |
| 177 | + }, | ||
| 178 | + "keywords": func(f *Flow, value string) (bool, error) { | ||
| 160 | f.spec.Keywords = "" | 179 | f.spec.Keywords = "" |
| 161 | if value != noKeywords { | 180 | if value != noKeywords { |
| 162 | f.spec.Keywords = value | 181 | f.spec.Keywords = value |
| 163 | } | 182 | } |
| 164 | - case "body": | 183 | + return true, nil |
| 184 | + }, | ||
| 185 | + "body": func(f *Flow, value string) (bool, error) { | ||
| 165 | f.spec.Body = value | 186 | f.spec.Body = value |
| 166 | - default: | 187 | + return true, nil |
| 167 | - return fmt.Errorf("no question %q", key) | 188 | + }, |
| 168 | - } | ||
| 169 | - f.answer[key] = value | ||
| 170 | - return nil | ||
| 171 | } | 189 | } |
| 172 | 190 | ||
| 173 | // Unset takes an answer back (the window's Esc), unless a flag gave it. | 191 | // Unset takes an answer back (the window's Esc), unless a flag gave it. |
added
internal/skillcreate/linewizard.go +94 -0 | new file mode 100644 | ||
| @@ -0,0 +1,94 @@ | ||
| 1 | +package skillcreate | |
| 2 | + | |
| 3 | +import ( | |
| 4 | + "bufio" | |
| 5 | + "fmt" | |
| 6 | + "io" | |
| 7 | + "strings" | |
| 8 | + | |
| 9 | + "mm/internal/agentinit" | |
| 10 | + "mm/internal/elicit" | |
| 11 | + "mm/internal/skills" | |
| 12 | +) | |
| 13 | + | |
| 14 | +// lineWizard asks the questions one per line, like mm init's line mode; the | |
| 15 | +// skill's text is read to the end of the input. | |
| 16 | +type lineWizard struct { | |
| 17 | + in *bufio.Reader | |
| 18 | + out io.Writer | |
| 19 | +} | |
| 20 | + | |
| 21 | +func (w *lineWizard) run(f *Flow) error { | |
| 22 | + for { | |
| 23 | + pending := f.Pending() | |
| 24 | + if len(pending) == 0 { | |
| 25 | + return nil | |
| 26 | + } | |
| 27 | + st := pending[0] | |
| 28 | + answer, ended := w.ask(st) | |
| 29 | + if err := w.take(f, st, answer, ended); err != nil { | |
| 30 | + return err | |
| 31 | + } | |
| 32 | + } | |
| 33 | +} | |
| 34 | + | |
| 35 | +// take records one answer. An answer missing or refused is said, and asked | |
| 36 | +// again while there is input left; once the input has ended, it is the | |
| 37 | +// error that ends the wizard. | |
| 38 | +func (w *lineWizard) take(f *Flow, st agentinit.Step, answer string, ended bool) error { | |
| 39 | + if strings.TrimSpace(answer) == "" { | |
| 40 | + if ended { | |
| 41 | + return fmt.Errorf("%s: no answer", strings.ToLower(st.Title)) | |
| 42 | + } | |
| 43 | + // A question without a default is asked again. | |
| 44 | + fmt.Fprintf(w.out, "⚠️ %s: an answer is needed\n", strings.ToLower(st.Title)) | |
| 45 | + return nil | |
| 46 | + } | |
| 47 | + if err := f.Set(st.Key, answer); err != nil { | |
| 48 | + fmt.Fprintln(w.out, "⚠️ "+err.Error()) | |
| 49 | + if ended { | |
| 50 | + return err | |
| 51 | + } | |
| 52 | + return nil | |
| 53 | + } | |
| 54 | + if n := len(KeywordList(f.spec.Keywords)); st.Key == "keywords" && n > skills.MaxKeywords { | |
| 55 | + fmt.Fprintf(w.out, "⚠️ %d keywords: only the first %d reach the catalogue\n", n, skills.MaxKeywords) | |
| 56 | + } | |
| 57 | + return nil | |
| 58 | +} | |
| 59 | + | |
| 60 | +// ask asks one step; it reports whether the input ended. | |
| 61 | +func (w *lineWizard) ask(st agentinit.Step) (string, bool) { | |
| 62 | + if st.Multiline { | |
| 63 | + fmt.Fprintln(w.out, "? "+st.Prompt) | |
| 64 | + fmt.Fprintln(w.out, " (several lines; end with Ctrl-D — the end of the input)") | |
| 65 | + data, _ := io.ReadAll(w.in) | |
| 66 | + return string(data), true | |
| 67 | + } | |
| 68 | + q := elicit.Question{Prompt: st.Prompt, Kind: st.Kind, Default: st.Default} | |
| 69 | + for _, o := range st.Options { | |
| 70 | + q.Options = append(q.Options, o.Value) | |
| 71 | + } | |
| 72 | + for { | |
| 73 | + lines := elicit.Lines(q) | |
| 74 | + lines[len(lines)-1] = strings.Replace(lines[len(lines)-1], "/skip to decline", "Enter = the default", 1) | |
| 75 | + if st.Optional { | |
| 76 | + lines[len(lines)-1] = " your answer (Enter = none)" | |
| 77 | + } | |
| 78 | + for _, l := range lines { | |
| 79 | + fmt.Fprintln(w.out, l) | |
| 80 | + } | |
| 81 | + fmt.Fprint(w.out, "> ") | |
| 82 | + text, err := w.in.ReadString('\n') | |
| 83 | + ended := err != nil | |
| 84 | + if elicit.IsSkip(text) || strings.TrimSpace(text) == "" { | |
| 85 | + return q.Default, ended | |
| 86 | + } | |
| 87 | + if a, ok := elicit.ParseLine(q, text); ok && len(a.Values) > 0 { | |
| 88 | + return a.Values[0], ended | |
| 89 | + } | |
| 90 | + if ended { | |
| 91 | + return q.Default, true | |
| 92 | + } | |
| 93 | + } | |
| 94 | +} | |
| new file mode 100644 | |||
| @@ -0,0 +1,94 @@ | |||
| 1 | +package skillcreate | ||
| 2 | + | ||
| 3 | +import ( | ||
| 4 | + "bufio" | ||
| 5 | + "fmt" | ||
| 6 | + "io" | ||
| 7 | + "strings" | ||
| 8 | + | ||
| 9 | + "mm/internal/agentinit" | ||
| 10 | + "mm/internal/elicit" | ||
| 11 | + "mm/internal/skills" | ||
| 12 | +) | ||
| 13 | + | ||
| 14 | +// lineWizard asks the questions one per line, like mm init's line mode; the | ||
| 15 | +// skill's text is read to the end of the input. | ||
| 16 | +type lineWizard struct { | ||
| 17 | + in *bufio.Reader | ||
| 18 | + out io.Writer | ||
| 19 | +} | ||
| 20 | + | ||
| 21 | +func (w *lineWizard) run(f *Flow) error { | ||
| 22 | + for { | ||
| 23 | + pending := f.Pending() | ||
| 24 | + if len(pending) == 0 { | ||
| 25 | + return nil | ||
| 26 | + } | ||
| 27 | + st := pending[0] | ||
| 28 | + answer, ended := w.ask(st) | ||
| 29 | + if err := w.take(f, st, answer, ended); err != nil { | ||
| 30 | + return err | ||
| 31 | + } | ||
| 32 | + } | ||
| 33 | +} | ||
| 34 | + | ||
| 35 | +// take records one answer. An answer missing or refused is said, and asked | ||
| 36 | +// again while there is input left; once the input has ended, it is the | ||
| 37 | +// error that ends the wizard. | ||
| 38 | +func (w *lineWizard) take(f *Flow, st agentinit.Step, answer string, ended bool) error { | ||
| 39 | + if strings.TrimSpace(answer) == "" { | ||
| 40 | + if ended { | ||
| 41 | + return fmt.Errorf("%s: no answer", strings.ToLower(st.Title)) | ||
| 42 | + } | ||
| 43 | + // A question without a default is asked again. | ||
| 44 | + fmt.Fprintf(w.out, "⚠️ %s: an answer is needed\n", strings.ToLower(st.Title)) | ||
| 45 | + return nil | ||
| 46 | + } | ||
| 47 | + if err := f.Set(st.Key, answer); err != nil { | ||
| 48 | + fmt.Fprintln(w.out, "⚠️ "+err.Error()) | ||
| 49 | + if ended { | ||
| 50 | + return err | ||
| 51 | + } | ||
| 52 | + return nil | ||
| 53 | + } | ||
| 54 | + if n := len(KeywordList(f.spec.Keywords)); st.Key == "keywords" && n > skills.MaxKeywords { | ||
| 55 | + fmt.Fprintf(w.out, "⚠️ %d keywords: only the first %d reach the catalogue\n", n, skills.MaxKeywords) | ||
| 56 | + } | ||
| 57 | + return nil | ||
| 58 | +} | ||
| 59 | + | ||
| 60 | +// ask asks one step; it reports whether the input ended. | ||
| 61 | +func (w *lineWizard) ask(st agentinit.Step) (string, bool) { | ||
| 62 | + if st.Multiline { | ||
| 63 | + fmt.Fprintln(w.out, "? "+st.Prompt) | ||
| 64 | + fmt.Fprintln(w.out, " (several lines; end with Ctrl-D — the end of the input)") | ||
| 65 | + data, _ := io.ReadAll(w.in) | ||
| 66 | + return string(data), true | ||
| 67 | + } | ||
| 68 | + q := elicit.Question{Prompt: st.Prompt, Kind: st.Kind, Default: st.Default} | ||
| 69 | + for _, o := range st.Options { | ||
| 70 | + q.Options = append(q.Options, o.Value) | ||
| 71 | + } | ||
| 72 | + for { | ||
| 73 | + lines := elicit.Lines(q) | ||
| 74 | + lines[len(lines)-1] = strings.Replace(lines[len(lines)-1], "/skip to decline", "Enter = the default", 1) | ||
| 75 | + if st.Optional { | ||
| 76 | + lines[len(lines)-1] = " your answer (Enter = none)" | ||
| 77 | + } | ||
| 78 | + for _, l := range lines { | ||
| 79 | + fmt.Fprintln(w.out, l) | ||
| 80 | + } | ||
| 81 | + fmt.Fprint(w.out, "> ") | ||
| 82 | + text, err := w.in.ReadString('\n') | ||
| 83 | + ended := err != nil | ||
| 84 | + if elicit.IsSkip(text) || strings.TrimSpace(text) == "" { | ||
| 85 | + return q.Default, ended | ||
| 86 | + } | ||
| 87 | + if a, ok := elicit.ParseLine(q, text); ok && len(a.Values) > 0 { | ||
| 88 | + return a.Values[0], ended | ||
| 89 | + } | ||
| 90 | + if ended { | ||
| 91 | + return q.Default, true | ||
| 92 | + } | ||
| 93 | + } | ||
| 94 | +} | ||
modified
internal/skillcreate/main.go +7 -136 | @@ -1,17 +1,12 @@ | ||
| 1 | 1 | package skillcreate |
| 2 | 2 | |
| 3 | 3 | import ( |
| 4 | - "bufio" | |
| 5 | 4 | "context" |
| 6 | - "errors" | |
| 7 | - "flag" | |
| 8 | 5 | "fmt" |
| 9 | 6 | "io" |
| 10 | 7 | "os" |
| 11 | 8 | "strings" |
| 12 | 9 | |
| 13 | - "mm/internal/agentinit" | |
| 14 | - "mm/internal/elicit" | |
| 15 | 10 | "mm/internal/skills" |
| 16 | 11 | ) |
| 17 | 12 | |
| @@ -39,66 +34,16 @@ func Main(args []string, stdin io.Reader, stdout io.Writer, opts ...Options) int | ||
| 39 | 34 | if len(opts) > 0 { |
| 40 | 35 | o = opts[0] |
| 41 | 36 | } |
| 42 | - fs := flag.NewFlagSet("mm skillcreate", flag.ContinueOnError) | |
| 43 | - fs.SetOutput(stdout) | |
| 44 | - var spec Spec | |
| 45 | - var configPath, bodyFile, output string | |
| 46 | - var yes, line bool | |
| 47 | - fs.StringVar(&spec.Name, "name", "", "the skill's name; written lower case with dashes (Vulcan Salute → vulcan-salute)") | |
| 48 | - fs.StringVar(&spec.Description, "description", "", "one line, shown in the catalogue the model chooses from") | |
| 49 | - fs.StringVar(&spec.Keywords, "keywords", "", "comma-separated words a user might type; the first 8 reach the catalogue") | |
| 50 | - fs.StringVar(&spec.Body, "body", "", "the skill's text, written under \"## What to do\"") | |
| 51 | - fs.StringVar(&bodyFile, "body-file", "", "read the skill's text from this file") | |
| 52 | - fs.StringVar(&configPath, "config", "", "the agent file whose skillsDir is used (default: as mm finds it — $AGENT_CONFIG, ./agent.yaml, ./.mm/agent.yaml)") | |
| 53 | - fs.StringVar(&output, "o", "", "the file to write (default <skillsDir>/<name>/SKILL.md; - for stdout, which creates nothing)") | |
| 54 | - fs.BoolVar(&yes, "yes", false, "ask nothing: name, description and text must be given; an existing skill is never replaced") | |
| 55 | - fs.BoolVar(&line, "line", false, "ask line by line even in a terminal (no window)") | |
| 56 | - if err := fs.Parse(args); err != nil { | |
| 57 | - if errors.Is(err, flag.ErrHelp) { | |
| 58 | - return 0 | |
| 59 | - } | |
| 60 | - return 2 | |
| 37 | + a, code, ok := parseArgs(args, stdout) | |
| 38 | + if !ok { | |
| 39 | + return code | |
| 61 | 40 | } |
| 62 | - if bodyFile != "" { | |
| 63 | - data, err := os.ReadFile(bodyFile) | |
| 64 | - if err != nil { | |
| 65 | - fmt.Fprintln(stdout, "⚠️ "+err.Error()) | |
| 66 | - return 1 | |
| 67 | - } | |
| 68 | - spec.Body = string(data) | |
| 41 | + flow, err := a.flow() | |
| 42 | + if err == nil { | |
| 43 | + err = a.answer(flow, o, stdin, stdout) | |
| 69 | 44 | } |
| 70 | - place, err := Locate(configPath) | |
| 71 | 45 | if err != nil { |
| 72 | - fmt.Fprintf(stdout, "⚠️ config error: %v\n", err) | |
| 73 | - return 1 | |
| 74 | - } | |
| 75 | - flow := NewFlow(place, Spec{}, output) | |
| 76 | - if err := flow.SetGiven(spec); err != nil { | |
| 77 | - fmt.Fprintln(stdout, "⚠️ "+err.Error()) | |
| 78 | - return 1 | |
| 79 | - } | |
| 80 | - | |
| 81 | - switch { | |
| 82 | - case yes: | |
| 83 | - if err := flow.takeDefaults(); err != nil { | |
| 84 | - fmt.Fprintln(stdout, "⚠️ "+err.Error()) | |
| 85 | - return 1 | |
| 86 | - } | |
| 87 | - case o.Window != nil && !line && o.Terminal != nil && o.Terminal(): | |
| 88 | - done, err := o.Window(context.Background(), flow) | |
| 89 | - if err != nil { | |
| 90 | - fmt.Fprintln(stdout, "⚠️ "+err.Error()) | |
| 91 | - return 1 | |
| 92 | - } | |
| 93 | - if !done { | |
| 94 | - fmt.Fprintln(stdout, "nothing written") | |
| 95 | - return 1 | |
| 96 | - } | |
| 97 | - default: | |
| 98 | - if err := (&lineWizard{in: bufio.NewReader(stdin), out: stdout}).run(flow); err != nil { | |
| 99 | - fmt.Fprintln(stdout, "⚠️ "+err.Error()) | |
| 100 | - return 1 | |
| 101 | - } | |
| 46 | + return failed(stdout, err) | |
| 102 | 47 | } |
| 103 | 48 | return finish(flow, stdout) |
| 104 | 49 | } |
| @@ -193,77 +138,3 @@ func write(flow *Flow, spec Spec, place Place, output string) (string, error) { | ||
| 193 | 138 | } |
| 194 | 139 | return output, os.WriteFile(output, []byte(Render(spec)), 0o644) |
| 195 | 140 | } |
| 196 | - | |
| 197 | -// lineWizard asks the questions one per line, like mm init's line mode; the | |
| 198 | -// skill's text is read to the end of the input. | |
| 199 | -type lineWizard struct { | |
| 200 | - in *bufio.Reader | |
| 201 | - out io.Writer | |
| 202 | -} | |
| 203 | - | |
| 204 | -func (w *lineWizard) run(f *Flow) error { | |
| 205 | - for { | |
| 206 | - pending := f.Pending() | |
| 207 | - if len(pending) == 0 { | |
| 208 | - return nil | |
| 209 | - } | |
| 210 | - st := pending[0] | |
| 211 | - answer, ended := w.ask(st) | |
| 212 | - if strings.TrimSpace(answer) == "" { | |
| 213 | - if ended { | |
| 214 | - return fmt.Errorf("%s: no answer", strings.ToLower(st.Title)) | |
| 215 | - } | |
| 216 | - // A question without a default is asked again. | |
| 217 | - fmt.Fprintf(w.out, "⚠️ %s: an answer is needed\n", strings.ToLower(st.Title)) | |
| 218 | - continue | |
| 219 | - } | |
| 220 | - if err := f.Set(st.Key, answer); err != nil { | |
| 221 | - fmt.Fprintln(w.out, "⚠️ "+err.Error()) | |
| 222 | - if ended { | |
| 223 | - return err | |
| 224 | - } | |
| 225 | - continue | |
| 226 | - } | |
| 227 | - if st.Key == "keywords" { | |
| 228 | - if n := len(KeywordList(f.spec.Keywords)); n > skills.MaxKeywords { | |
| 229 | - fmt.Fprintf(w.out, "⚠️ %d keywords: only the first %d reach the catalogue\n", n, skills.MaxKeywords) | |
| 230 | - } | |
| 231 | - } | |
| 232 | - } | |
| 233 | -} | |
| 234 | - | |
| 235 | -// ask asks one step; it reports whether the input ended. | |
| 236 | -func (w *lineWizard) ask(st agentinit.Step) (string, bool) { | |
| 237 | - if st.Multiline { | |
| 238 | - fmt.Fprintln(w.out, "? "+st.Prompt) | |
| 239 | - fmt.Fprintln(w.out, " (several lines; end with Ctrl-D — the end of the input)") | |
| 240 | - data, _ := io.ReadAll(w.in) | |
| 241 | - return string(data), true | |
| 242 | - } | |
| 243 | - q := elicit.Question{Prompt: st.Prompt, Kind: st.Kind, Default: st.Default} | |
| 244 | - for _, o := range st.Options { | |
| 245 | - q.Options = append(q.Options, o.Value) | |
| 246 | - } | |
| 247 | - for { | |
| 248 | - lines := elicit.Lines(q) | |
| 249 | - lines[len(lines)-1] = strings.Replace(lines[len(lines)-1], "/skip to decline", "Enter = the default", 1) | |
| 250 | - if st.Optional { | |
| 251 | - lines[len(lines)-1] = " your answer (Enter = none)" | |
| 252 | - } | |
| 253 | - for _, l := range lines { | |
| 254 | - fmt.Fprintln(w.out, l) | |
| 255 | - } | |
| 256 | - fmt.Fprint(w.out, "> ") | |
| 257 | - text, err := w.in.ReadString('\n') | |
| 258 | - ended := err != nil | |
| 259 | - if elicit.IsSkip(text) || strings.TrimSpace(text) == "" { | |
| 260 | - return q.Default, ended | |
| 261 | - } | |
| 262 | - if a, ok := elicit.ParseLine(q, text); ok && len(a.Values) > 0 { | |
| 263 | - return a.Values[0], ended | |
| 264 | - } | |
| 265 | - if ended { | |
| 266 | - return q.Default, true | |
| 267 | - } | |
| 268 | - } | |
| 269 | -} | |
| @@ -1,17 +1,12 @@ | |||
| 1 | package skillcreate | 1 | package skillcreate |
| 2 | 2 | ||
| 3 | import ( | 3 | import ( |
| 4 | - "bufio" | ||
| 5 | "context" | 4 | "context" |
| 6 | - "errors" | ||
| 7 | - "flag" | ||
| 8 | "fmt" | 5 | "fmt" |
| 9 | "io" | 6 | "io" |
| 10 | "os" | 7 | "os" |
| 11 | "strings" | 8 | "strings" |
| 12 | 9 | ||
| 13 | - "mm/internal/agentinit" | ||
| 14 | - "mm/internal/elicit" | ||
| 15 | "mm/internal/skills" | 10 | "mm/internal/skills" |
| 16 | ) | 11 | ) |
| 17 | 12 | ||
| @@ -39,66 +34,16 @@ func Main(args []string, stdin io.Reader, stdout io.Writer, opts ...Options) int | |||
| 39 | if len(opts) > 0 { | 34 | if len(opts) > 0 { |
| 40 | o = opts[0] | 35 | o = opts[0] |
| 41 | } | 36 | } |
| 42 | - fs := flag.NewFlagSet("mm skillcreate", flag.ContinueOnError) | 37 | + a, code, ok := parseArgs(args, stdout) |
| 43 | - fs.SetOutput(stdout) | 38 | + if !ok { |
| 44 | - var spec Spec | 39 | + return code |
| 45 | - var configPath, bodyFile, output string | ||
| 46 | - var yes, line bool | ||
| 47 | - fs.StringVar(&spec.Name, "name", "", "the skill's name; written lower case with dashes (Vulcan Salute → vulcan-salute)") | ||
| 48 | - fs.StringVar(&spec.Description, "description", "", "one line, shown in the catalogue the model chooses from") | ||
| 49 | - fs.StringVar(&spec.Keywords, "keywords", "", "comma-separated words a user might type; the first 8 reach the catalogue") | ||
| 50 | - fs.StringVar(&spec.Body, "body", "", "the skill's text, written under \"## What to do\"") | ||
| 51 | - fs.StringVar(&bodyFile, "body-file", "", "read the skill's text from this file") | ||
| 52 | - fs.StringVar(&configPath, "config", "", "the agent file whose skillsDir is used (default: as mm finds it — $AGENT_CONFIG, ./agent.yaml, ./.mm/agent.yaml)") | ||
| 53 | - fs.StringVar(&output, "o", "", "the file to write (default <skillsDir>/<name>/SKILL.md; - for stdout, which creates nothing)") | ||
| 54 | - fs.BoolVar(&yes, "yes", false, "ask nothing: name, description and text must be given; an existing skill is never replaced") | ||
| 55 | - fs.BoolVar(&line, "line", false, "ask line by line even in a terminal (no window)") | ||
| 56 | - if err := fs.Parse(args); err != nil { | ||
| 57 | - if errors.Is(err, flag.ErrHelp) { | ||
| 58 | - return 0 | ||
| 59 | - } | ||
| 60 | - return 2 | ||
| 61 | } | 40 | } |
| 62 | - if bodyFile != "" { | 41 | + flow, err := a.flow() |
| 63 | - data, err := os.ReadFile(bodyFile) | 42 | + if err == nil { |
| 64 | - if err != nil { | 43 | + err = a.answer(flow, o, stdin, stdout) |
| 65 | - fmt.Fprintln(stdout, "⚠️ "+err.Error()) | ||
| 66 | - return 1 | ||
| 67 | - } | ||
| 68 | - spec.Body = string(data) | ||
| 69 | } | 44 | } |
| 70 | - place, err := Locate(configPath) | ||
| 71 | if err != nil { | 45 | if err != nil { |
| 72 | - fmt.Fprintf(stdout, "⚠️ config error: %v\n", err) | 46 | + return failed(stdout, err) |
| 73 | - return 1 | ||
| 74 | - } | ||
| 75 | - flow := NewFlow(place, Spec{}, output) | ||
| 76 | - if err := flow.SetGiven(spec); err != nil { | ||
| 77 | - fmt.Fprintln(stdout, "⚠️ "+err.Error()) | ||
| 78 | - return 1 | ||
| 79 | - } | ||
| 80 | - | ||
| 81 | - switch { | ||
| 82 | - case yes: | ||
| 83 | - if err := flow.takeDefaults(); err != nil { | ||
| 84 | - fmt.Fprintln(stdout, "⚠️ "+err.Error()) | ||
| 85 | - return 1 | ||
| 86 | - } | ||
| 87 | - case o.Window != nil && !line && o.Terminal != nil && o.Terminal(): | ||
| 88 | - done, err := o.Window(context.Background(), flow) | ||
| 89 | - if err != nil { | ||
| 90 | - fmt.Fprintln(stdout, "⚠️ "+err.Error()) | ||
| 91 | - return 1 | ||
| 92 | - } | ||
| 93 | - if !done { | ||
| 94 | - fmt.Fprintln(stdout, "nothing written") | ||
| 95 | - return 1 | ||
| 96 | - } | ||
| 97 | - default: | ||
| 98 | - if err := (&lineWizard{in: bufio.NewReader(stdin), out: stdout}).run(flow); err != nil { | ||
| 99 | - fmt.Fprintln(stdout, "⚠️ "+err.Error()) | ||
| 100 | - return 1 | ||
| 101 | - } | ||
| 102 | } | 47 | } |
| 103 | return finish(flow, stdout) | 48 | return finish(flow, stdout) |
| 104 | } | 49 | } |
| @@ -193,77 +138,3 @@ func write(flow *Flow, spec Spec, place Place, output string) (string, error) { | |||
| 193 | } | 138 | } |
| 194 | return output, os.WriteFile(output, []byte(Render(spec)), 0o644) | 139 | return output, os.WriteFile(output, []byte(Render(spec)), 0o644) |
| 195 | } | 140 | } |
| 196 | - | ||
| 197 | -// lineWizard asks the questions one per line, like mm init's line mode; the | ||
| 198 | -// skill's text is read to the end of the input. | ||
| 199 | -type lineWizard struct { | ||
| 200 | - in *bufio.Reader | ||
| 201 | - out io.Writer | ||
| 202 | -} | ||
| 203 | - | ||
| 204 | -func (w *lineWizard) run(f *Flow) error { | ||
| 205 | - for { | ||
| 206 | - pending := f.Pending() | ||
| 207 | - if len(pending) == 0 { | ||
| 208 | - return nil | ||
| 209 | - } | ||
| 210 | - st := pending[0] | ||
| 211 | - answer, ended := w.ask(st) | ||
| 212 | - if strings.TrimSpace(answer) == "" { | ||
| 213 | - if ended { | ||
| 214 | - return fmt.Errorf("%s: no answer", strings.ToLower(st.Title)) | ||
| 215 | - } | ||
| 216 | - // A question without a default is asked again. | ||
| 217 | - fmt.Fprintf(w.out, "⚠️ %s: an answer is needed\n", strings.ToLower(st.Title)) | ||
| 218 | - continue | ||
| 219 | - } | ||
| 220 | - if err := f.Set(st.Key, answer); err != nil { | ||
| 221 | - fmt.Fprintln(w.out, "⚠️ "+err.Error()) | ||
| 222 | - if ended { | ||
| 223 | - return err | ||
| 224 | - } | ||
| 225 | - continue | ||
| 226 | - } | ||
| 227 | - if st.Key == "keywords" { | ||
| 228 | - if n := len(KeywordList(f.spec.Keywords)); n > skills.MaxKeywords { | ||
| 229 | - fmt.Fprintf(w.out, "⚠️ %d keywords: only the first %d reach the catalogue\n", n, skills.MaxKeywords) | ||
| 230 | - } | ||
| 231 | - } | ||
| 232 | - } | ||
| 233 | -} | ||
| 234 | - | ||
| 235 | -// ask asks one step; it reports whether the input ended. | ||
| 236 | -func (w *lineWizard) ask(st agentinit.Step) (string, bool) { | ||
| 237 | - if st.Multiline { | ||
| 238 | - fmt.Fprintln(w.out, "? "+st.Prompt) | ||
| 239 | - fmt.Fprintln(w.out, " (several lines; end with Ctrl-D — the end of the input)") | ||
| 240 | - data, _ := io.ReadAll(w.in) | ||
| 241 | - return string(data), true | ||
| 242 | - } | ||
| 243 | - q := elicit.Question{Prompt: st.Prompt, Kind: st.Kind, Default: st.Default} | ||
| 244 | - for _, o := range st.Options { | ||
| 245 | - q.Options = append(q.Options, o.Value) | ||
| 246 | - } | ||
| 247 | - for { | ||
| 248 | - lines := elicit.Lines(q) | ||
| 249 | - lines[len(lines)-1] = strings.Replace(lines[len(lines)-1], "/skip to decline", "Enter = the default", 1) | ||
| 250 | - if st.Optional { | ||
| 251 | - lines[len(lines)-1] = " your answer (Enter = none)" | ||
| 252 | - } | ||
| 253 | - for _, l := range lines { | ||
| 254 | - fmt.Fprintln(w.out, l) | ||
| 255 | - } | ||
| 256 | - fmt.Fprint(w.out, "> ") | ||
| 257 | - text, err := w.in.ReadString('\n') | ||
| 258 | - ended := err != nil | ||
| 259 | - if elicit.IsSkip(text) || strings.TrimSpace(text) == "" { | ||
| 260 | - return q.Default, ended | ||
| 261 | - } | ||
| 262 | - if a, ok := elicit.ParseLine(q, text); ok && len(a.Values) > 0 { | ||
| 263 | - return a.Values[0], ended | ||
| 264 | - } | ||
| 265 | - if ended { | ||
| 266 | - return q.Default, true | ||
| 267 | - } | ||
| 268 | - } | ||
| 269 | -} | ||