picoclaw/pkg/commands/cmd_switch.go
mingmxren ac3b00e80e refactor(commands): consolidate /switch into commands package, fix ! prefix
Move /switch model and /switch channel handling from inline loop.go
logic into cmd_switch.go using the SubCommand + Deps pattern. This
removes the OutcomePassthrough branch in handleCommand entirely.

Also replace the hardcoded "/" prefix check with commands.HasCommandPrefix
so that "!" prefixed commands are correctly routed to the Executor.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-05 14:45:47 +08:00

59 lines
1.6 KiB
Go

package commands
import (
"context"
"fmt"
)
func switchCommand(deps *Deps) Definition {
return Definition{
Name: "switch",
Description: "Switch model or channel",
SubCommands: []SubCommand{
{
Name: "model",
Description: "Switch to a different model",
ArgsUsage: "to <name>",
Handler: func(_ context.Context, req Request) error {
if req.Reply == nil {
return nil
}
if deps.SwitchModel == nil {
return req.Reply("Command unavailable in current context.")
}
// Parse: /switch model to <value>
value := nthToken(req.Text, 3) // tokens: [/switch, model, to, <value>]
if nthToken(req.Text, 2) != "to" || value == "" {
return req.Reply("Usage: /switch model to <name>")
}
oldModel, err := deps.SwitchModel(value)
if err != nil {
return req.Reply(err.Error())
}
return req.Reply(fmt.Sprintf("Switched model from %s to %s", oldModel, value))
},
},
{
Name: "channel",
Description: "Switch to a different channel",
ArgsUsage: "to <name>",
Handler: func(_ context.Context, req Request) error {
if req.Reply == nil {
return nil
}
if deps.SwitchChannel == nil {
return req.Reply("Command unavailable in current context.")
}
value := nthToken(req.Text, 3)
if nthToken(req.Text, 2) != "to" || value == "" {
return req.Reply("Usage: /switch channel to <name>")
}
if err := deps.SwitchChannel(value); err != nil {
return req.Reply(err.Error())
}
return req.Reply(fmt.Sprintf("Switched target channel to %s", value))
},
},
},
}
}