♻️ Quality: internal/command without smells (3 → 0)
- /docs, /unload and /memory built by one optional() instead of three copies of the same block - ParseAsk with askRequest and askNamed Checked: the menus of the three front ends with the optional commands off and on, and ParseAsk on 14 inputs, byte-identical old vs new. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
84d5b8e parent: b315334 modified
internal/command/ask.go +31 -16 | @@ -70,22 +70,9 @@ func ParseAsk(input string) (req AskRequest, ok bool, err error) { | ||
| 70 | 70 | if !found { |
| 71 | 71 | return AskRequest{}, false, nil |
| 72 | 72 | } |
| 73 | - switch { | |
| 74 | - case cmd.Name == Ask || cmd.Name == AskWithContext: | |
| 75 | - req.WithContext = cmd.Name == AskWithContext | |
| 76 | - req.Agent, req.Prompt, _ = strings.Cut(arg, " ") | |
| 77 | - if req.Agent == "" { | |
| 78 | - return req, true, fmt.Errorf("usage: /%s <agent> <prompt> — agents: %s", cmd.Name, strings.Join(agentNames(), ", ")) | |
| 79 | - } | |
| 80 | - if !slices.Contains(agentNames(), req.Agent) { | |
| 81 | - return req, true, fmt.Errorf("unknown agent %q — agents: %s", req.Agent, strings.Join(agentNames(), ", ")) | |
| 82 | - } | |
| 83 | - case strings.HasPrefix(cmd.Name, AskWithContext+"-"): | |
| 84 | - req.WithContext, req.Agent, req.Prompt = true, strings.TrimPrefix(cmd.Name, AskWithContext+"-"), arg | |
| 85 | - case strings.HasPrefix(cmd.Name, Ask+"-"): | |
| 86 | - req.Agent, req.Prompt = strings.TrimPrefix(cmd.Name, Ask+"-"), arg | |
| 87 | - default: | |
| 88 | - return AskRequest{}, false, nil | |
| 73 | + req, ok, err = askRequest(cmd.Name, arg) | |
| 74 | + if !ok || err != nil { | |
| 75 | + return req, ok, err | |
| 89 | 76 | } |
| 90 | 77 | req.Prompt = strings.TrimSpace(req.Prompt) |
| 91 | 78 | if req.Prompt == "" { |
| @@ -94,6 +81,34 @@ func ParseAsk(input string) (req AskRequest, ok bool, err error) { | ||
| 94 | 81 | return req, true, nil |
| 95 | 82 | } |
| 96 | 83 | |
| 84 | +// askRequest reads the agent and the prompt of an /ask command: named after | |
| 85 | +// the command (/ask <agent> …), or in it (/ask-bob …). ok is false for a | |
| 86 | +// command of another family. | |
| 87 | +func askRequest(name, arg string) (AskRequest, bool, error) { | |
| 88 | + switch { | |
| 89 | + case name == Ask || name == AskWithContext: | |
| 90 | + return askNamed(name, arg) | |
| 91 | + case strings.HasPrefix(name, AskWithContext+"-"): | |
| 92 | + return AskRequest{WithContext: true, Agent: strings.TrimPrefix(name, AskWithContext+"-"), Prompt: arg}, true, nil | |
| 93 | + case strings.HasPrefix(name, Ask+"-"): | |
| 94 | + return AskRequest{Agent: strings.TrimPrefix(name, Ask+"-"), Prompt: arg}, true, nil | |
| 95 | + } | |
| 96 | + return AskRequest{}, false, nil | |
| 97 | +} | |
| 98 | + | |
| 99 | +// askNamed reads /ask <agent> <prompt>: the agent must be a configured one. | |
| 100 | +func askNamed(name, arg string) (AskRequest, bool, error) { | |
| 101 | + req := AskRequest{WithContext: name == AskWithContext} | |
| 102 | + req.Agent, req.Prompt, _ = strings.Cut(arg, " ") | |
| 103 | + if req.Agent == "" { | |
| 104 | + return req, true, fmt.Errorf("usage: /%s <agent> <prompt> — agents: %s", name, strings.Join(agentNames(), ", ")) | |
| 105 | + } | |
| 106 | + if !slices.Contains(agentNames(), req.Agent) { | |
| 107 | + return req, true, fmt.Errorf("unknown agent %q — agents: %s", req.Agent, strings.Join(agentNames(), ", ")) | |
| 108 | + } | |
| 109 | + return req, true, nil | |
| 110 | +} | |
| 111 | + | |
| 97 | 112 | // agentNames lists the agents SetAgents was given, in its order. |
| 98 | 113 | func agentNames() []string { |
| 99 | 114 | var names []string |
| @@ -70,22 +70,9 @@ func ParseAsk(input string) (req AskRequest, ok bool, err error) { | |||
| 70 | if !found { | 70 | if !found { |
| 71 | return AskRequest{}, false, nil | 71 | return AskRequest{}, false, nil |
| 72 | } | 72 | } |
| 73 | - switch { | 73 | + req, ok, err = askRequest(cmd.Name, arg) |
| 74 | - case cmd.Name == Ask || cmd.Name == AskWithContext: | 74 | + if !ok || err != nil { |
| 75 | - req.WithContext = cmd.Name == AskWithContext | 75 | + return req, ok, err |
| 76 | - req.Agent, req.Prompt, _ = strings.Cut(arg, " ") | ||
| 77 | - if req.Agent == "" { | ||
| 78 | - return req, true, fmt.Errorf("usage: /%s <agent> <prompt> — agents: %s", cmd.Name, strings.Join(agentNames(), ", ")) | ||
| 79 | - } | ||
| 80 | - if !slices.Contains(agentNames(), req.Agent) { | ||
| 81 | - return req, true, fmt.Errorf("unknown agent %q — agents: %s", req.Agent, strings.Join(agentNames(), ", ")) | ||
| 82 | - } | ||
| 83 | - case strings.HasPrefix(cmd.Name, AskWithContext+"-"): | ||
| 84 | - req.WithContext, req.Agent, req.Prompt = true, strings.TrimPrefix(cmd.Name, AskWithContext+"-"), arg | ||
| 85 | - case strings.HasPrefix(cmd.Name, Ask+"-"): | ||
| 86 | - req.Agent, req.Prompt = strings.TrimPrefix(cmd.Name, Ask+"-"), arg | ||
| 87 | - default: | ||
| 88 | - return AskRequest{}, false, nil | ||
| 89 | } | 76 | } |
| 90 | req.Prompt = strings.TrimSpace(req.Prompt) | 77 | req.Prompt = strings.TrimSpace(req.Prompt) |
| 91 | if req.Prompt == "" { | 78 | if req.Prompt == "" { |
| @@ -94,6 +81,34 @@ func ParseAsk(input string) (req AskRequest, ok bool, err error) { | |||
| 94 | return req, true, nil | 81 | return req, true, nil |
| 95 | } | 82 | } |
| 96 | 83 | ||
| 84 | +// askRequest reads the agent and the prompt of an /ask command: named after | ||
| 85 | +// the command (/ask <agent> …), or in it (/ask-bob …). ok is false for a | ||
| 86 | +// command of another family. | ||
| 87 | +func askRequest(name, arg string) (AskRequest, bool, error) { | ||
| 88 | + switch { | ||
| 89 | + case name == Ask || name == AskWithContext: | ||
| 90 | + return askNamed(name, arg) | ||
| 91 | + case strings.HasPrefix(name, AskWithContext+"-"): | ||
| 92 | + return AskRequest{WithContext: true, Agent: strings.TrimPrefix(name, AskWithContext+"-"), Prompt: arg}, true, nil | ||
| 93 | + case strings.HasPrefix(name, Ask+"-"): | ||
| 94 | + return AskRequest{Agent: strings.TrimPrefix(name, Ask+"-"), Prompt: arg}, true, nil | ||
| 95 | + } | ||
| 96 | + return AskRequest{}, false, nil | ||
| 97 | +} | ||
| 98 | + | ||
| 99 | +// askNamed reads /ask <agent> <prompt>: the agent must be a configured one. | ||
| 100 | +func askNamed(name, arg string) (AskRequest, bool, error) { | ||
| 101 | + req := AskRequest{WithContext: name == AskWithContext} | ||
| 102 | + req.Agent, req.Prompt, _ = strings.Cut(arg, " ") | ||
| 103 | + if req.Agent == "" { | ||
| 104 | + return req, true, fmt.Errorf("usage: /%s <agent> <prompt> — agents: %s", name, strings.Join(agentNames(), ", ")) | ||
| 105 | + } | ||
| 106 | + if !slices.Contains(agentNames(), req.Agent) { | ||
| 107 | + return req, true, fmt.Errorf("unknown agent %q — agents: %s", req.Agent, strings.Join(agentNames(), ", ")) | ||
| 108 | + } | ||
| 109 | + return req, true, nil | ||
| 110 | +} | ||
| 111 | + | ||
| 97 | // agentNames lists the agents SetAgents was given, in its order. | 112 | // agentNames lists the agents SetAgents was given, in its order. |
| 98 | func agentNames() []string { | 113 | func agentNames() []string { |
| 99 | var names []string | 114 | var names []string |
modified
internal/command/command.go +11 -0 | @@ -176,3 +176,14 @@ func usage(c Command) string { | ||
| 176 | 176 | } |
| 177 | 177 | return c.Slash() |
| 178 | 178 | } |
| 179 | + | |
| 180 | +// optional is c offered in every front end — the editor included — with an | |
| 181 | +// argument, when enabled; nothing otherwise, so /help and the menus do not | |
| 182 | +// show a command that is off (/memory, /docs, /unload). | |
| 183 | +func optional(enabled bool, c Command) []Command { | |
| 184 | + if !enabled { | |
| 185 | + return nil | |
| 186 | + } | |
| 187 | + c.Fronts, c.TakesArg = Terminal|Editor|Window, true | |
| 188 | + return []Command{c} | |
| 189 | +} | |
| @@ -176,3 +176,14 @@ func usage(c Command) string { | |||
| 176 | } | 176 | } |
| 177 | return c.Slash() | 177 | return c.Slash() |
| 178 | } | 178 | } |
| 179 | + | ||
| 180 | +// optional is c offered in every front end — the editor included — with an | ||
| 181 | +// argument, when enabled; nothing otherwise, so /help and the menus do not | ||
| 182 | +// show a command that is off (/memory, /docs, /unload). | ||
| 183 | +func optional(enabled bool, c Command) []Command { | ||
| 184 | + if !enabled { | ||
| 185 | + return nil | ||
| 186 | + } | ||
| 187 | + c.Fronts, c.TakesArg = Terminal|Editor|Window, true | ||
| 188 | + return []Command{c} | ||
| 189 | +} | ||
modified
internal/command/docs.go +1 -10 | @@ -10,14 +10,5 @@ var docsCommands []Command | ||
| 10 | 10 | // command.SetDocs(config.Cfg.DocSearch.DocsCommand()) |
| 11 | 11 | // cmd, arg, _ := command.Parse("/docs list state") // Docs, "list state" |
| 12 | 12 | func SetDocs(enabled bool) { |
| 13 | - docsCommands = nil | |
| 14 | - if enabled { | |
| 15 | - docsCommands = []Command{{ | |
| 16 | - Name: Docs, | |
| 17 | - Description: "Search the documentation folders (docSearch.dirs) for these words", | |
| 18 | - Fronts: Terminal | Editor | Window, | |
| 19 | - TakesArg: true, | |
| 20 | - Hint: "<words>", | |
| 21 | - }} | |
| 22 | - } | |
| 13 | + docsCommands = optional(enabled, Command{Name: Docs, Description: "Search the documentation folders (docSearch.dirs) for these words", Hint: "<words>"}) | |
| 23 | 14 | } |
| @@ -10,14 +10,5 @@ var docsCommands []Command | |||
| 10 | // command.SetDocs(config.Cfg.DocSearch.DocsCommand()) | 10 | // command.SetDocs(config.Cfg.DocSearch.DocsCommand()) |
| 11 | // cmd, arg, _ := command.Parse("/docs list state") // Docs, "list state" | 11 | // cmd, arg, _ := command.Parse("/docs list state") // Docs, "list state" |
| 12 | func SetDocs(enabled bool) { | 12 | func SetDocs(enabled bool) { |
| 13 | - docsCommands = nil | 13 | + docsCommands = optional(enabled, Command{Name: Docs, Description: "Search the documentation folders (docSearch.dirs) for these words", Hint: "<words>"}) |
| 14 | - if enabled { | ||
| 15 | - docsCommands = []Command{{ | ||
| 16 | - Name: Docs, | ||
| 17 | - Description: "Search the documentation folders (docSearch.dirs) for these words", | ||
| 18 | - Fronts: Terminal | Editor | Window, | ||
| 19 | - TakesArg: true, | ||
| 20 | - Hint: "<words>", | ||
| 21 | - }} | ||
| 22 | - } | ||
| 23 | } | 14 | } |
modified
internal/command/memory.go +1 -10 | @@ -11,14 +11,5 @@ var memoryCommands []Command | ||
| 11 | 11 | // command.SetMemory(config.Cfg.Sessions.MemoryCommand()) |
| 12 | 12 | // cmd, arg, _ := command.Parse("/memory watchdog timeout") // Memory, "watchdog timeout" |
| 13 | 13 | func SetMemory(enabled bool) { |
| 14 | - memoryCommands = nil | |
| 15 | - if enabled { | |
| 16 | - memoryCommands = []Command{{ | |
| 17 | - Name: Memory, | |
| 18 | - Description: "Search the project's stored sessions for these words", | |
| 19 | - Fronts: Terminal | Editor | Window, | |
| 20 | - TakesArg: true, | |
| 21 | - Hint: "<words>", | |
| 22 | - }} | |
| 23 | - } | |
| 14 | + memoryCommands = optional(enabled, Command{Name: Memory, Description: "Search the project's stored sessions for these words", Hint: "<words>"}) | |
| 24 | 15 | } |
| @@ -11,14 +11,5 @@ var memoryCommands []Command | |||
| 11 | // command.SetMemory(config.Cfg.Sessions.MemoryCommand()) | 11 | // command.SetMemory(config.Cfg.Sessions.MemoryCommand()) |
| 12 | // cmd, arg, _ := command.Parse("/memory watchdog timeout") // Memory, "watchdog timeout" | 12 | // cmd, arg, _ := command.Parse("/memory watchdog timeout") // Memory, "watchdog timeout" |
| 13 | func SetMemory(enabled bool) { | 13 | func SetMemory(enabled bool) { |
| 14 | - memoryCommands = nil | 14 | + memoryCommands = optional(enabled, Command{Name: Memory, Description: "Search the project's stored sessions for these words", Hint: "<words>"}) |
| 15 | - if enabled { | ||
| 16 | - memoryCommands = []Command{{ | ||
| 17 | - Name: Memory, | ||
| 18 | - Description: "Search the project's stored sessions for these words", | ||
| 19 | - Fronts: Terminal | Editor | Window, | ||
| 20 | - TakesArg: true, | ||
| 21 | - Hint: "<words>", | ||
| 22 | - }} | ||
| 23 | - } | ||
| 24 | } | 15 | } |
modified
internal/command/unload.go +1 -10 | @@ -10,14 +10,5 @@ var unloadCommands []Command | ||
| 10 | 10 | // command.SetUnload(config.Cfg.UnloadModels) |
| 11 | 11 | // cmd, arg, _ := command.Parse("/unload qwen3.5:2b") // Unload, "qwen3.5:2b" |
| 12 | 12 | func SetUnload(enabled bool) { |
| 13 | - unloadCommands = nil | |
| 14 | - if enabled { | |
| 15 | - unloadCommands = []Command{{ | |
| 16 | - Name: Unload, | |
| 17 | - Description: "Unload a model from the server's memory: this agent's, or the one named (the next question reloads it)", | |
| 18 | - Fronts: Terminal | Editor | Window, | |
| 19 | - TakesArg: true, | |
| 20 | - Hint: "[model]", | |
| 21 | - }} | |
| 22 | - } | |
| 13 | + unloadCommands = optional(enabled, Command{Name: Unload, Description: "Unload a model from the server's memory: this agent's, or the one named (the next question reloads it)", Hint: "[model]"}) | |
| 23 | 14 | } |
| @@ -10,14 +10,5 @@ var unloadCommands []Command | |||
| 10 | // command.SetUnload(config.Cfg.UnloadModels) | 10 | // command.SetUnload(config.Cfg.UnloadModels) |
| 11 | // cmd, arg, _ := command.Parse("/unload qwen3.5:2b") // Unload, "qwen3.5:2b" | 11 | // cmd, arg, _ := command.Parse("/unload qwen3.5:2b") // Unload, "qwen3.5:2b" |
| 12 | func SetUnload(enabled bool) { | 12 | func SetUnload(enabled bool) { |
| 13 | - unloadCommands = nil | 13 | + unloadCommands = optional(enabled, Command{Name: Unload, Description: "Unload a model from the server's memory: this agent's, or the one named (the next question reloads it)", Hint: "[model]"}) |
| 14 | - if enabled { | ||
| 15 | - unloadCommands = []Command{{ | ||
| 16 | - Name: Unload, | ||
| 17 | - Description: "Unload a model from the server's memory: this agent's, or the one named (the next question reloads it)", | ||
| 18 | - Fronts: Terminal | Editor | Window, | ||
| 19 | - TakesArg: true, | ||
| 20 | - Hint: "[model]", | ||
| 21 | - }} | ||
| 22 | - } | ||
| 23 | } | 14 | } |