Files
pipelines/internal/server/chat_reply_attention.go
T
alan 498c6f17a6 Add server notification settings with app checkboxes
Persist three global ntfy switches in server_settings: run_finished
(default on: auto-mode runs notify on success, read inside the
transition transaction; on/off modes unchanged), chat_needs_you
(default on: gates chat question/approval alerts) and
chat_reply_finished (default off: one alert per finished assistant
turn). Expose notification_settings / set_notification_settings over
the websocket protocol, add a Phone notifications section with
Material and Cupertino checkboxes in Settings, document the settings,
and keep the cli, tui, stage and pipeline test packages from
inheriting a real ntfy configuration.
2026-10-30 21:36:27 +00:00

99 lines
3.1 KiB
Go

package server
import (
"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 !chatAttentionConfigured() {
return
}
if step.TurnID != "" && !h.claimChatAttentionLocked("reply\x00"+frame.SessionID+"\x00"+step.TurnID) {
return
}
body := h.finalReplyTextLocked(frame.SessionID)
if body == "" {
body = chatReplyFallbackBody
}
h.startChatAttention(chatAttention{sessionID: frame.SessionID, reply: true, body: body})
}
// 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)
}