From f092429d15b6f4ae8547bb09c5e2c99c2fb40ba3 Mon Sep 17 00:00:00 2001 From: Imri Paran Date: Sat, 4 Apr 2026 12:13:30 +0000 Subject: [PATCH] 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. --- pkg/commands/executor.go | 6 ++++-- pkg/commands/executor_test.go | 20 +++++++++++++++----- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/pkg/commands/executor.go b/pkg/commands/executor.go index 78a50e6c2..4b635d3b9 100644 --- a/pkg/commands/executor.go +++ b/pkg/commands/executor.go @@ -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} -} +} \ No newline at end of file diff --git a/pkg/commands/executor_test.go b/pkg/commands/executor_test.go index 09350f1b6..858f528d0 100644 --- a/pkg/commands/executor_test.go +++ b/pkg/commands/executor_test.go @@ -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) } -} +} \ No newline at end of file