restart fix + slack commands

This commit is contained in:
ari committed 2026-11-01 15:10:39 -05:00
1 parent e90c9f324b
commit 25fcf85855
22 files changed
+1077 -119

No files matched your search

+102
View File
@@ -0,0 +1,102 @@
package bot
import (
"bytes"
"context"
"encoding/json"
"fmt"
"log"
"sync"
"slackerbot/internal/model"
"slackerbot/internal/tools"
)
const maxReplyBytes = 1 << 20
// agentCommands runs the Slack commands the agent calls as tools during one
// turn, as the person whose message started it. Only stop waits for the turn
// to end, because stopping the container would cut the reply off.
type agentCommands struct {
b *Bot
u *model.Owner
m incoming
root string
wg sync.WaitGroup
mu sync.Mutex
stopWhen bool
}
func (b *Bot) agentCommands(u *model.Owner, m incoming, root string) *agentCommands {
m.threadTS = root
return &agentCommands{b: b, u: u, m: m, root: root}
}
// Update must not block: it's called from the ACP read loop.
func (a *agentCommands) Update(ctx context.Context, up acpUpdate) {
c, args, ok := tools.CommandCall(up)
if !ok || c.Missing(args) {
return
}
a.wg.Add(1)
go func() {
defer a.wg.Done()
reply, isErr := a.run(ctx, c, args)
a.answer(ctx, c, args, reply, isErr)
}()
}
func (a *agentCommands) run(ctx context.Context, c tools.SlackCommand, args string) (string, bool) {
text := c.Text(args)
log.Printf("agent: %s ran %q for %s in %s/%s", a.u.Machine, text, a.m.user, a.m.channel, a.root)
if c.Name == tools.Stop {
return a.queueStop(), false
}
verb, rest := splitCommand(text)
var reply string
var ok bool
if c.Name == tools.Move {
reply, ok = a.b.move(ctx, a.m, rest, a.root, true)
} else {
reply, ok = a.b.runCommand(ctx, a.m, verb, rest, true)
}
if !ok {
return fmt.Sprintf("`%s` isn't something slackerbot understands. Check the tool's description for the args it takes.", text), true
}
if c.Changes(args) && c.Name != tools.Move {
a.b.say(ctx, a.m.channel, a.root, fmt.Sprintf(":robot_face: The agent ran `%s` for %s:\n%s", text, a.b.displayName(ctx, a.m.user), reply))
}
return reply, false
}
func (a *agentCommands) queueStop() string {
if a.b.store.Owner(a.u.ID) == nil {
return "There's no container to stop."
}
a.mu.Lock()
a.stopWhen = true
a.mu.Unlock()
return "The container will shut down as soon as this reply ends. Its files are kept, and the next message starts it again."
}
func (a *agentCommands) answer(ctx context.Context, c tools.SlackCommand, args, text string, isErr bool) {
data, _ := json.Marshal(tools.Reply{Text: text, Error: isErr})
p := tools.ReplyPath(a.b.cfg.AgentCwd, c.Name, args)
if err := a.b.mgr.WriteFile(ctx, a.u, p, maxReplyBytes, bytes.NewReader(data)); err != nil {
log.Printf("agent: answering %s in %s: %v", c.Name, a.u.Machine, err)
}
}
// Finish waits for the turn's commands, then does what had to wait for the turn to end.
func (a *agentCommands) Finish(ctx context.Context) {
a.wg.Wait()
a.mu.Lock()
stop := a.stopWhen
a.mu.Unlock()
if !stop {
return
}
reply := a.b.stopFor(ctx, a.u.ID)
a.b.say(ctx, a.m.channel, a.root, fmt.Sprintf(":robot_face: The agent ran `stop` for %s:\n%s", a.b.displayName(ctx, a.m.user), reply))
}