status button

This commit is contained in:
ari committed 2026-11-01 15:29:38 -05:00
1 parent c7547a9125
commit 9e94c05fb6
5 files changed
+109 -23

No files matched your search

+50 -16
View File
@@ -2,6 +2,7 @@ package bot
import (
"context"
"fmt"
"log"
"strings"
"sync"
@@ -14,7 +15,7 @@ import (
)
const (
slackMsgLimit = 3500
slackMsgLimit = 3000 // a section block's text limit
flushEvery = 1500 * time.Millisecond // under Slack's edit rate limit
)
@@ -22,10 +23,14 @@ type Streamer struct {
api *slack.Client
channel string
thread string
// The placeholder's ts, which keys the reply in Bot.running.
cancelKey string
mu sync.Mutex
full strings.Builder
dirty bool
mu sync.Mutex
full strings.Builder
dirty bool
status string
last time.Time
// Only the flush loop touches these.
ts string
@@ -36,7 +41,8 @@ type Streamer struct {
}
func NewStreamer(api *slack.Client, channel, thread, placeholderTS string) *Streamer {
s := &Streamer{api: api, channel: channel, thread: thread, ts: placeholderTS,
s := &Streamer{api: api, channel: channel, thread: thread, ts: placeholderTS, cancelKey: placeholderTS,
dirty: true, status: "Waiting for the agent", last: time.Now(),
quit: make(chan struct{}), done: make(chan struct{})}
go s.loop()
return s
@@ -45,21 +51,29 @@ func NewStreamer(api *slack.Client, channel, thread, placeholderTS string) *Stre
func (s *Streamer) Update(u acp.Update) {
s.mu.Lock()
defer s.mu.Unlock()
s.last = time.Now()
s.dirty = true
switch u.Kind {
case "agent_thought_chunk":
s.status = "Thinking"
case "agent_message_chunk":
s.status = "Writing the reply"
s.full.WriteString(u.Text)
case "tool_call":
s.status = "Using a tool"
if u.Title == "" {
return
}
s.status = "Running " + u.Title
if t := s.full.String(); t != "" && !strings.HasSuffix(t, "\n") {
s.full.WriteString("\n")
}
s.full.WriteString("> :wrench: " + u.Title + "\n")
default:
return
case "tool_call_update":
if u.Status == "completed" || u.Status == "failed" {
s.status = "Thinking"
}
}
s.dirty = true
}
func (s *Streamer) Finish(stopReason string, err error) {
@@ -90,24 +104,27 @@ func (s *Streamer) Finish(stopReason string, err error) {
s.dirty = true
s.mu.Unlock()
s.flush()
s.flush(false)
}
func (s *Streamer) loop() {
defer close(s.done)
t := time.NewTicker(flushEvery)
defer t.Stop()
s.flush(true)
for {
select {
case <-s.quit:
return
case <-t.C:
s.flush()
s.flush(true)
}
}
}
func (s *Streamer) flush() {
// flush edits the reply. While live, the newest message also carries a status
// line and a Cancel button, which the final flush removes.
func (s *Streamer) flush(live bool) {
s.mu.Lock()
if !s.dirty {
s.mu.Unlock()
@@ -115,6 +132,11 @@ func (s *Streamer) flush() {
}
s.dirty = false
text := s.full.String()[s.offset:]
footer := ""
if live {
footer = fmt.Sprintf(":hourglass_flowing_sand: %s… · last update from the agent <!date^%d^{time_secs}|%s>",
s.status, s.last.Unix(), s.last.Format("15:04:05 MST"))
}
s.mu.Unlock()
ctx := context.Background()
@@ -126,7 +148,7 @@ func (s *Streamer) flush() {
cut--
}
}
s.edit(ctx, text[:cut])
s.edit(ctx, text[:cut], "")
_, ts, err := s.api.PostMessageContext(ctx, s.channel,
slack.MsgOptionText("…", false), slack.MsgOptionTS(s.thread))
if err != nil {
@@ -137,14 +159,26 @@ func (s *Streamer) flush() {
s.offset += cut
text = text[cut:]
}
s.edit(ctx, text)
s.edit(ctx, text, footer)
}
func (s *Streamer) edit(ctx context.Context, text string) {
func (s *Streamer) edit(ctx context.Context, text, footer string) {
if strings.TrimSpace(text) == "" {
text = "…"
text = "_working…_"
}
if _, _, _, err := s.api.UpdateMessageContext(ctx, s.channel, s.ts, slack.MsgOptionText(text, false)); err != nil {
blocks := []slack.Block{}
if footer != "" {
cancel := slack.NewButtonBlockElement("cancel", s.cancelKey,
slack.NewTextBlockObject(slack.PlainTextType, "Cancel", false, false))
cancel.Style = slack.StyleDanger
blocks = []slack.Block{
slack.NewSectionBlock(slack.NewTextBlockObject(slack.MarkdownType, text, false, false), nil, nil),
slack.NewContextBlock("status", slack.NewTextBlockObject(slack.MarkdownType, footer, false, false)),
slack.NewActionBlock("reply", cancel),
}
}
if _, _, _, err := s.api.UpdateMessageContext(ctx, s.channel, s.ts,
slack.MsgOptionText(text, false), slack.MsgOptionBlocks(blocks...)); err != nil {
log.Printf("slack update: %v", err)
}
}