From 03032b15f4004c668633336fe4df36893a461854 Mon Sep 17 00:00:00 2001 From: mingmxren Date: Sun, 1 Mar 2026 17:52:06 +0800 Subject: [PATCH] refactor(commands): remove unused dispatcher abstraction --- pkg/commands/dispatcher.go | 47 --------------------- pkg/commands/dispatcher_test.go | 75 --------------------------------- 2 files changed, 122 deletions(-) delete mode 100644 pkg/commands/dispatcher_test.go diff --git a/pkg/commands/dispatcher.go b/pkg/commands/dispatcher.go index 2d8859760..c47a87ae9 100644 --- a/pkg/commands/dispatcher.go +++ b/pkg/commands/dispatcher.go @@ -16,53 +16,6 @@ type Request struct { Reply func(text string) error } -type Result struct { - Matched bool - Handled bool - Command string - Err error -} - -type Dispatcher struct { - reg *Registry -} - -type Dispatching interface { - Dispatch(ctx context.Context, req Request) Result -} - -type DispatchFunc func(ctx context.Context, req Request) Result - -func (f DispatchFunc) Dispatch(ctx context.Context, req Request) Result { - return f(ctx, req) -} - -func NewDispatcher(reg *Registry) *Dispatcher { - return &Dispatcher{reg: reg} -} - -func (d *Dispatcher) Dispatch(ctx context.Context, req Request) Result { - cmdName, ok := parseCommandName(req.Text) - if !ok { - return Result{Matched: false} - } - - for _, def := range d.reg.ForChannel(req.Channel) { - if def.Name != cmdName && !contains(def.Aliases, cmdName) { - continue - } - if def.Handler == nil { - // Definition-only command (for menu registration / discovery). - // Let the inbound message continue to the agent loop. - return Result{Matched: false, Handled: false, Command: def.Name} - } - err := def.Handler(ctx, req) - return Result{Matched: true, Handled: true, Command: def.Name, Err: err} - } - - return Result{Matched: false} -} - func firstToken(input string) string { parts := strings.Fields(strings.TrimSpace(input)) if len(parts) == 0 { diff --git a/pkg/commands/dispatcher_test.go b/pkg/commands/dispatcher_test.go deleted file mode 100644 index 8590e4860..000000000 --- a/pkg/commands/dispatcher_test.go +++ /dev/null @@ -1,75 +0,0 @@ -package commands - -import ( - "context" - "testing" -) - -func TestDispatcher_MatchSlashCommand(t *testing.T) { - called := false - defs := []Definition{ - { - Name: "help", - Handler: func(context.Context, Request) error { - called = true - return nil - }, - }, - } - d := NewDispatcher(NewRegistry(defs)) - - res := d.Dispatch(context.Background(), Request{ - Channel: "telegram", - Text: "/help", - }) - if !res.Matched || !called || res.Err != nil { - t.Fatalf("dispatch result = %+v, called=%v", res, called) - } -} - -func TestDispatcher_DoesNotMatchWithoutSlash(t *testing.T) { - d := NewDispatcher(NewRegistry([]Definition{{Name: "help"}})) - - res := d.Dispatch(context.Background(), Request{ - Channel: "telegram", - Text: "help", - }) - if res.Matched { - t.Fatalf("expected unmatched for plain text, got %+v", res) - } -} - -func TestDispatcher_MatchTelegramMentionSyntax(t *testing.T) { - called := false - d := NewDispatcher(NewRegistry([]Definition{ - { - Name: "help", - Handler: func(context.Context, Request) error { - called = true - return nil - }, - }, - })) - - res := d.Dispatch(context.Background(), Request{ - Channel: "telegram", - Text: "/help@my_bot", - }) - if !res.Matched || !res.Handled || !called || res.Err != nil { - t.Fatalf("dispatch result = %+v, called=%v", res, called) - } -} - -func TestDispatcher_PassThroughDefinitionWithoutHandler(t *testing.T) { - d := NewDispatcher(NewRegistry([]Definition{ - {Name: "session"}, // menu-only / pass-through definition - })) - - res := d.Dispatch(context.Background(), Request{ - Channel: "telegram", - Text: "/session list", - }) - if res.Matched { - t.Fatalf("expected pass-through unmatched result, got %+v", res) - } -}