Files
pipelines/internal/server/chat_reply_attention.go
T
alan d1846441cc Stream ntfy notifications to app clients over /connect
Record every notification published to ntfy (run outbox, stage notify
hooks, chat question/approval/reply) in a persisted app_notifications
outbox and stream it to subscribe_notifications clients with an after
cursor; clear_notifications and ack_attention clear across devices, as do
runs leaving waiting, deleted runs and answered chat questions. Add the
notify.ntfy_enabled setting (default on) to stop only the ntfy publish.
2026-11-01 02:03:58 +00:00

104 lines
3.3 KiB
Go

package server
import (
"strconv"
"strings"
"git.a-wan.net/alan/pipelines/internal/protocol"
)
// chatReplyBodyRunes bounds the reply excerpt carried in the notification body.
const chatReplyBodyRunes = 200
// chatReplyFallbackBody is used when the finished turn produced no text the
// hub retained (tool-only turns, or text beyond the retention cap).
const chatReplyFallbackBody = "The agent finished its reply."
// noteReplyAttentionLocked announces a finished assistant turn once. prior is
// the session's step state before frame was folded. Only an authoritative
// (non-intermediate) ended step that follows an observed, unfinished turn
// counts, so a repeated or replayed end with no turn in between never
// republishes; a native turn id is also claimed in the hub-wide dedupe set.
// Whether the operator wants these is decided at publish time
// (chat_reply_finished). Callers must hold h.mu.
func (h *chatHub) noteReplyAttentionLocked(prior *protocol.ChatStep, frame protocol.ChatEventFrame) {
step := frame.Step
if frame.Kind != protocol.ChatEventStep || step == nil || step.Status != protocol.ChatStepEnded || step.Intermediate || frame.SessionID == "" {
return
}
if prior == nil || (prior.Status == protocol.ChatStepEnded && !prior.Intermediate) {
return
}
if !h.attentionWanted() {
return
}
if step.TurnID != "" && !h.claimChatAttentionLocked("reply\x00"+frame.SessionID+"\x00"+step.TurnID) {
return
}
body := h.finalReplyTextLocked(frame.SessionID)
if body == "" {
body = chatReplyFallbackBody
}
turnID := step.TurnID
if turnID == "" {
turnID = strconv.FormatInt(h.clock().UnixNano(), 36)
}
h.startChatAttention(chatAttention{sessionID: frame.SessionID, reply: true, body: body, id: chatNotificationID(frame.SessionID, "reply", turnID)})
}
// finalReplyTextLocked returns a bounded single-line excerpt of the latest
// assistant message's text in the current turn: messages after the last user
// message, scanned newest first. Callers must hold h.mu.
func (h *chatHub) finalReplyTextLocked(sessionID string) string {
turn := h.turns[sessionID]
if turn == nil {
return ""
}
for i := len(turn.order) - 1; i >= 0; i-- {
message := turn.messages[turn.order[i]]
if message == nil {
continue
}
if message.role == protocol.ChatRoleUser {
return ""
}
var text strings.Builder
for _, id := range message.order {
part := message.parts[id]
if part == nil || part.part.Type != protocol.ChatEventText || len(part.text) == 0 {
continue
}
if text.Len() > 0 {
text.WriteByte(' ')
}
text.Write(part.text)
if text.Len() > chatReplyBodyRunes*4 {
break
}
}
if excerpt := singleLine(text.String()); excerpt != "" {
return truncateRunes(excerpt, chatReplyBodyRunes)
}
}
return ""
}
// truncateRunes shortens text to at most limit runes, marking the cut.
func truncateRunes(text string, limit int) string {
runes := []rune(text)
if len(runes) <= limit {
return text
}
return strings.TrimSpace(string(runes[:limit])) + "…"
}
// chatReplyText builds the single-line ntfy title/body for a finished reply.
func chatReplyText(name, body string) (string, string) {
name = singleLine(name)
if name == "" {
name = "Chat"
}
const suffix = " replied"
return truncateUTF8(name, 256-len(suffix)) + suffix, truncateUTF8(singleLine(body), 2000)
}