fs: refactor agent ctl to map-based dispatch
Replace switch statement with rdwrHandler map. Each command is a function value in agentCtlHandlers — easy to extend, test individually, and introspect.
This commit is contained in:
parent
993d5a812a
commit
0025003aa0
|
|
@ -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) {
|
||||
|
|
|
|||
Loading…
Reference in New Issue