Files
slacker-bot/slackerbot/internal/tools/commands.go
T
2026-11-01 15:52:46 -05:00

165 lines
6.7 KiB
Go

package tools
import (
"crypto/sha256"
"encoding/hex"
"encoding/json"
"path/filepath"
"strings"
"slackerbot/internal/acp"
"slackerbot/internal/util"
)
// SlackCommand is a Slack command offered to the agent as a tool. Its text is Verb
// followed by the call's args, exactly as a user would type it.
type SlackCommand struct {
Name string
Verb string
Help string
// Describes the args parameter. Empty when the command takes none.
Args string
Required bool
// With no args it only shows a setting.
Shows bool
// It only shows things, whatever the args.
ReadOnly bool
}
const (
Stop = "stop"
Move = "move"
)
var Commands = []SlackCommand{
{Name: "status", Verb: "status", Shows: true,
Help: "Show this container's state, size, node, time unused, idle limit, home folder usage and every node."},
{Name: "sessions", Verb: "sessions", Shows: true,
Help: "List this container's recent sessions (Slack threads), with links."},
{Name: "nodes", Verb: "nodes", Shows: true,
Help: "List every node with the CPU, RAM and storage free on it, and where this container runs."},
{Name: "permissions", Verb: "permissions", Shows: true,
Help: "Show who may scale, change storage, move or set the idle limit here, and who the user managers are."},
{Name: Stop, Verb: "stop",
Help: "Shut this container down once your current reply is finished. The home folder is kept, and the next message starts it again."},
{Name: "scale", Verb: "scale", Required: true,
Help: "Change this container's RAM and CPU. A running container changes right away; shrinking memory may stop programs.",
Args: "`up` or `down` to double or halve, an exact size like `8G 4cpu`, or a change like `+2G` or `-1cpu`"},
{Name: "keep", Verb: "keep", Shows: true,
Help: "Show or set how long this container may sit unused before it's stopped.",
Args: "A time like `24h`, `3d` or `2w`, `indefinite` to never stop it, or `default` to reset. Empty shows the current limit"},
{Name: "storage", Verb: "storage", Shows: true,
Help: "Show the home folder's usage, or change its guaranteed space and max size.",
Args: "A new max like `20G`, a change like `+5G`, or `min 8G` / `min 8G max 20G`. Empty shows usage"},
{Name: Move, Verb: "move",
Help: "Move this container to another node. Progress is posted in Slack. Near the end the container is briefly stopped, so finish your reply soon after calling this.",
Args: "A node name from `nodes`, or `auto` (or empty) to pick the best one"},
{Name: "listen", Verb: "listen", Shows: true,
Help: "Show or change which replies in a channel thread reach you. DMs always read every message.",
Args: "`all-messages` to read every reply in this thread, `only-mentions` for replies that @mention the bot. Put `channel` first to set the channel's default. Empty shows the setting"},
{Name: "grant", Verb: "grant", Required: true,
Help: "Give people permissions in this channel. People are Slack mentions like <@U012AB>, as they appear in messages.",
Args: "Mentions then permissions (scale, storage, move, keep, manage or all), like `<@U012AB> move scale`. Start with `default` to change the server-wide list"},
{Name: "revoke", Verb: "revoke", Required: true,
Help: "Take permissions away in this channel. People are Slack mentions like <@U012AB>.",
Args: "Mentions, then optionally permissions, like `<@U012AB> move`. Only mentions removes everything. Start with `default` for the server-wide list"},
{Name: "reset_permissions", Verb: "permissions reset",
Help: "Make this channel use the server-wide default list of super users again."},
{Name: "dm_permission", Verb: "dm", Required: true,
Help: "Allow or forbid everyone changing a setting on their own DM container. User managers only.",
Args: "A permission and on or off, like `storage off`"},
{Name: "in_flight", Verb: "in-flight", Shows: true, ReadOnly: true,
Help: "For debugging: show slackerbot's in-flight files, the replies it's still working on and would pick back up after a restart. Super users and user managers only.",
Args: "`all` for every channel's file. Empty shows this channel's"},
{Name: "managers", Verb: "managers", Shows: true,
Help: "Show, add or remove user managers.",
Args: "`add <@U012AB>` or `remove <@U012AB>`. Empty lists them"},
}
func commandNamed(name string) (SlackCommand, bool) {
for _, c := range Commands {
if c.Name == name {
return c, true
}
}
return SlackCommand{}, false
}
func (c SlackCommand) Text(args string) string {
return strings.TrimSpace(c.Verb + " " + strings.TrimSpace(args))
}
// Missing calls are refused by the server, so the bot doesn't run them.
func (c SlackCommand) Missing(args string) bool {
return c.Required && strings.TrimSpace(args) == ""
}
func (c SlackCommand) Changes(args string) bool {
return !c.ReadOnly && (!c.Shows || strings.TrimSpace(args) != "")
}
// Reply is what the bot answers a command call with.
type Reply struct {
Text string `json:"text"`
Error bool `json:"error,omitempty"`
}
// The MCP server can't reach the bot, so the bot runs the command when it
// sees the call over ACP and drops the reply here for the server to pick up.
// The server only knows the tool and its args, so they name the file.
func replyDir(home string) string { return filepath.Join(home, ".slackerbot", "replies") }
func replyPrefix(name, args string) string {
sum := sha256.Sum256([]byte(name + "\x00" + strings.TrimSpace(args)))
return hex.EncodeToString(sum[:8]) + "-"
}
func ReplyPath(home, name, args string) string {
return filepath.Join(replyDir(home), replyPrefix(name, args)+util.RandomHex(6)+".json")
}
func argsOf(raw json.RawMessage) string {
var in struct {
Args string `json:"args"`
}
_ = json.Unmarshal(raw, &in)
return in.Args
}
// toolName is the slackerbot tool an ACP tool call is for. Goose names it in
// _meta, and older versions only in the title ("slackerbot: send file · …").
func toolName(u acp.Update) (string, bool) {
if name, ok := strings.CutPrefix(u.ToolName, ServerName+"__"); ok {
return name, true
}
best := ""
for _, name := range append([]string{SendFile}, commandNames()...) {
t := ServerName + ": " + strings.ReplaceAll(name, "_", " ")
if (u.Title == t || strings.HasPrefix(u.Title, t+" ")) && len(name) > len(best) {
best = name
}
}
return best, best != ""
}
func commandNames() []string {
names := make([]string, len(Commands))
for i, c := range Commands {
names[i] = c.Name
}
return names
}
// CommandCall reports whether an ACP update starts a command tool call, and with which args.
func CommandCall(u acp.Update) (c SlackCommand, args string, ok bool) {
if u.Kind != "tool_call" {
return SlackCommand{}, "", false
}
name, ok := toolName(u)
if !ok {
return SlackCommand{}, "", false
}
c, ok = commandNamed(name)
return c, argsOf(u.RawInput), ok
}