running and cancel all commands

This commit is contained in:
ari committed 2026-11-01 20:29:39 -05:00
1 parent 917a361370
commit af249326e8
7 files changed
+145 -6

No files matched your search

+85
View File
@@ -11,6 +11,7 @@ import (
"github.com/slack-go/slack"
"slackerbot/internal/commands"
"slackerbot/internal/model"
)
@@ -28,6 +29,90 @@ const (
func isInFlight(verb string) bool { return verb == "in-flight" || verb == "inflight" }
func isCancelAll(verb, args string) bool { return verb == "cancel" && strings.EqualFold(args, "all") }
func (b *Bot) channelTurns(channel string) []model.Turn {
var out []model.Turn
for _, t := range b.store.Turns() {
if t.Channel == channel {
out = append(out, t)
}
}
return out
}
// A turn can only be cancelled once its prompt is running, the same as the Cancel button.
func (b *Bot) turnCancel(t model.Turn) (context.CancelFunc, bool) {
cancel, ok := b.running.Load(t.Channel + "/" + t.ReplyTS)
if !ok {
return nil, false
}
return cancel.(context.CancelFunc), true
}
func (b *Bot) runningList(ctx context.Context, channel string) string {
turns := b.channelTurns(channel)
if len(turns) == 0 {
return "Nothing is running here right now."
}
var sb strings.Builder
sb.WriteString("*Running replies* (`cancel all` stops them all):\n")
for _, t := range turns {
name := title(t.Text)
if s := b.store.Session(t.Channel, t.ThreadTS); s != nil {
name = s.Title
}
if link, err := b.api.GetPermalinkContext(ctx, &slack.PermalinkParameters{Channel: t.Channel, Ts: t.ThreadTS}); err == nil {
name = fmt.Sprintf("<%s|%s>", link, name)
}
doing := "getting the container ready"
if _, ok := b.turnCancel(t); ok {
doing = "working"
}
fmt.Fprintf(&sb, "• %s · for %s · %s, started %s ago\n", name, b.displayName(ctx, t.User), doing,
commands.Human(time.Since(t.StartedAt)))
}
return strings.TrimSpace(sb.String())
}
// The agent's own reply is spared, or calling the tool would cut off its answer.
func (b *Bot) cancelAll(m incoming, agent bool) string {
var cancelled, starting int
for _, t := range b.channelTurns(m.channel) {
if agent && t.ThreadTS == m.threadTS {
continue
}
cancel, ok := b.turnCancel(t)
if !ok {
starting++
continue
}
cancel()
cancelled++
}
if cancelled == 0 && starting == 0 {
if agent {
return "No other replies are running here."
}
return "Nothing is running here right now."
}
reply := fmt.Sprintf("Cancelled %s. Each session carries on with the next message in its thread.", replies(cancelled))
if cancelled == 0 {
reply = "Nothing was cancelled."
}
if starting > 0 {
reply += fmt.Sprintf(" Not cancelled yet, because the container is still getting ready: %s. Try again in a moment.", replies(starting))
}
return reply
}
func replies(n int) string {
if n == 1 {
return "1 reply"
}
return fmt.Sprintf("%d replies", n)
}
func (b *Bot) trackTurn(m incoming, root, replyTS string, prior *model.Turn) *model.Turn {
ownerID, _ := m.owner()
t := &model.Turn{