Fix: Return error for unrecognized slash commands instead of LLM passthrough

- Modified Execute() to return OutcomeHandled with error message for unknown commands
- Updated test case to verify error handling behavior
- Fixes issue #45: Unrecognized slash commands incorrectly forwarded to LLM

Previously, unrecognized slash commands would return OutcomePassthrough,
causing them to be forwarded to the LLM instead of triggering an error.
This change ensures users get clear feedback when they use invalid commands.
This commit is contained in:
Imri Paran 2026-04-04 12:13:30 +00:00 committed by github-actions[bot]
parent 1c6874fa74
commit f092429d15
2 changed files with 19 additions and 7 deletions

View file

@ -44,7 +44,9 @@ func (e *Executor) Execute(ctx context.Context, req Request) ExecuteResult {
def, found := e.reg.Lookup(cmdName)
if !found {
return ExecuteResult{Outcome: OutcomePassthrough, Command: cmdName}
// Return an error for unrecognized commands instead of forwarding to LLM
err := req.Reply(fmt.Sprintf("Unknown command: %s", cmdName))
return ExecuteResult{Outcome: OutcomeHandled, Command: cmdName, Err: err}
}
return e.executeDefinition(ctx, req, def)
@ -86,4 +88,4 @@ func (e *Executor) executeDefinition(ctx context.Context, req Request, def Defin
// Unknown sub-command
err := req.Reply(fmt.Sprintf("Unknown option: %s. Usage: %s", subName, def.EffectiveUsage()))
return ExecuteResult{Outcome: OutcomeHandled, Command: def.Name, Err: err}
}
}

View file

@ -17,13 +17,23 @@ func TestExecutor_RegisteredWithoutHandler_ReturnsPassthrough(t *testing.T) {
}
}
func TestExecutor_UnknownSlashCommand_ReturnsPassthrough(t *testing.T) {
func TestExecutor_UnknownSlashCommand_ReturnsError(t *testing.T) {
defs := []Definition{{Name: "show"}}
ex := NewExecutor(NewRegistry(defs), nil)
res := ex.Execute(context.Background(), Request{Channel: "telegram", Text: "/unknown"})
if res.Outcome != OutcomePassthrough {
t.Fatalf("outcome=%v, want=%v", res.Outcome, OutcomePassthrough)
var reply string
res := ex.Execute(context.Background(), Request{Channel: "telegram", Text: "/unknown", Reply: func(text string) error { reply = text; return nil }})
if res.Outcome != OutcomeHandled {
t.Fatalf("outcome=%v, want=%v", res.Outcome, OutcomeHandled)
}
if res.Command != "unknown" {
t.Fatalf("command=%q, want=%q", res.Command, "unknown")
}
if res.Err != nil {
t.Fatalf("expected error, got nil")
}
if reply != "Unknown command: unknown" {
t.Fatalf("reply=%q, want=%q", reply, "Unknown command: unknown")
}
}
@ -257,4 +267,4 @@ func TestExecutor_SubCommand_NilHandler_ReturnsPassthrough(t *testing.T) {
if res.Outcome != OutcomePassthrough {
t.Fatalf("outcome=%v, want=%v", res.Outcome, OutcomePassthrough)
}
}
}