From 0025003aa0717dc7a520be2e1919f1fb7ebd8b36 Mon Sep 17 00:00:00 2001 From: Ollie Agent Date: Sun, 9 Aug 2026 02:53:17 +0200 Subject: [PATCH] fs: refactor agent ctl to map-based dispatch MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Replace switch statement with rdwrHandler map. Each command is a function value in agentCtlHandlers — easy to extend, test individually, and introspect. --- fs/handlers.go | 55 +++++++++++++++++++++++++++++++++----------------- 1 file changed, 37 insertions(+), 18 deletions(-) diff --git a/fs/handlers.go b/fs/handlers.go index e3c813a..6c37b99 100644 --- a/fs/handlers.go +++ b/fs/handlers.go @@ -664,34 +664,49 @@ func requestAgentCtl(ctx HandlerCtx, data []byte) ([]byte, error) { cmd := parts[0] args := parts[1:] - switch cmd { - case "kill": + h, ok := agentCtlHandlers[cmd] + if !ok { + return nil, fmt.Errorf("unknown ctl command: %s", cmd) + } + return h(ctx, args) +} + +// rdwrHandler is a request-response ctl command handler. +type rdwrHandler func(ctx HandlerCtx, args []string) ([]byte, error) + +var agentCtlHandlers = map[string]rdwrHandler{ + "kill": func(ctx HandlerCtx, _ []string) ([]byte, error) { if ctx.Session.Core != nil { ctx.Session.Core.RemoveAgent(ctx.Agent.ID()) session.PublishEvent("session."+ctx.Session.ID()+".agent."+ctx.Agent.ID()+".kill", "") } return []byte("ok\n"), nil - case "stop": + }, + "stop": func(ctx HandlerCtx, _ []string) ([]byte, error) { ctx.Agent.Interrupt(agent.ErrInterrupted) return []byte("ok\n"), nil - case "compact": + }, + "compact": func(ctx HandlerCtx, _ []string) ([]byte, error) { if err := ctx.Agent.Compact(ctx.Session.Ctx()); err != nil { return nil, err } return []byte("ok\n"), nil - case "clear": + }, + "clear": func(ctx HandlerCtx, _ []string) ([]byte, error) { if err := ctx.Agent.Clear(); err != nil { return nil, err } return []byte("ok\n"), nil - case "inject": + }, + "inject": func(ctx HandlerCtx, args []string) ([]byte, error) { text := strings.Join(args, " ") if text == "" { return nil, fmt.Errorf("inject requires text") } ctx.Agent.InjectRewrite(text) return []byte("ok\n"), nil - case "agent": + }, + "agent": func(ctx HandlerCtx, args []string) ([]byte, error) { if len(args) == 0 { return []byte(ctx.Agent.Profile() + "\n"), nil } @@ -699,7 +714,8 @@ func requestAgentCtl(ctx HandlerCtx, data []byte) ([]byte, error) { return nil, err } return []byte(args[0] + "\n"), nil - case "model": + }, + "model": func(ctx HandlerCtx, args []string) ([]byte, error) { if len(args) == 0 { if be := ctx.Agent.Backend(); be != nil { return []byte(be.Model() + "\n"), nil @@ -710,9 +726,11 @@ func requestAgentCtl(ctx HandlerCtx, data []byte) ([]byte, error) { be.SetModel(strings.Join(args, " ")) } return []byte(strings.Join(args, " ") + "\n"), nil - case "models": + }, + "models": func(ctx HandlerCtx, _ []string) ([]byte, error) { return []byte(ctx.Session.CachedListModels() + "\n"), nil - case "tools": + }, + "tools": func(ctx HandlerCtx, _ []string) ([]byte, error) { if ctx.Session.ToolsConn() == nil { return []byte("(no tool server)\n"), nil } @@ -729,7 +747,8 @@ func requestAgentCtl(ctx HandlerCtx, data []byte) ([]byte, error) { } } return []byte(sb.String()), nil - case "tool_load": + }, + "tool_load": func(ctx HandlerCtx, args []string) ([]byte, error) { if len(args) == 0 { return nil, fmt.Errorf("tool_load requires a tool name") } @@ -737,22 +756,24 @@ func requestAgentCtl(ctx HandlerCtx, data []byte) ([]byte, error) { return nil, err } return []byte(args[0] + "\n"), nil - case "backend": + }, + "backend": func(ctx HandlerCtx, args []string) ([]byte, error) { if len(args) == 0 { if be := ctx.Agent.Backend(); be != nil { return []byte(be.Name() + "\n"), nil } return nil, nil } - // backend switching not supported via ctl (requires session-level setup) return nil, fmt.Errorf("backend switching not supported via ctl; use cfg") - case "name": + }, + "name": func(ctx HandlerCtx, args []string) ([]byte, error) { if len(args) == 0 { return []byte(ctx.Agent.Name() + "\n"), nil } ctx.Agent.SetName(strings.Join(args, " ")) return []byte(strings.Join(args, " ") + "\n"), nil - case "cwd": + }, + "cwd": func(ctx HandlerCtx, args []string) ([]byte, error) { if len(args) == 0 { return []byte(ctx.Agent.Cwd() + "\n"), nil } @@ -762,9 +783,7 @@ func requestAgentCtl(ctx HandlerCtx, data []byte) ([]byte, error) { } ctx.Agent.SetCWD(dir) return []byte(dir + "\n"), nil - default: - return nil, fmt.Errorf("unknown ctl command: %s", cmd) - } + }, } func readAgentStats(ctx HandlerCtx) ([]byte, error) {