b91ef04ab0
- Server: chat ntfy messages carry a sequence id derived from their app id; clearing a chat app notification also clears it in ntfy (PUT /<topic>/<seq>/clear, verified on ntfy v2.38.1). - Server: a turn that sent an agent notify does not also send chat_reply. - notify tool and MCP instructions: Pipelines already notifies at reply end and for questions/approvals; notify is for mid-turn or background news. - Shown notify items carry channels (apps/phone); the transcript row says where it went. - App: NotificationBridge.close(tag); web closes tracked Notifications and tells other tabs over a BroadcastChannel; native bridge cancels by tag and now supports macOS. AgentNotices closes on notifications_cleared and on resubscribe cleared ids, and a tap sends clear_notifications. - Docs: skill, chat reference, ntfy/notifications docs, app note. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
164 lines
5.2 KiB
Go
164 lines
5.2 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 = 1000
|
|
|
|
// 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.ChatStepStarted && !step.Intermediate {
|
|
// A new turn has not notified yet.
|
|
delete(h.notifiedTurns, frame.SessionID)
|
|
}
|
|
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
|
|
}
|
|
if h.takeTurnNotifiedLocked(frame.SessionID) {
|
|
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)})
|
|
}
|
|
|
|
// markTurnNotified records that sessionID's turn in progress sent an agent
|
|
// notify, so its end is not announced as a reply too. A notify outside a turn
|
|
// the hub is following (background work after the turn) marks nothing.
|
|
func (h *chatHub) markTurnNotified(sessionID string) {
|
|
h.mu.Lock()
|
|
defer h.mu.Unlock()
|
|
turn := h.turns[sessionID]
|
|
if turn == nil || turn.step == nil || (turn.step.Status == protocol.ChatStepEnded && !turn.step.Intermediate) {
|
|
return
|
|
}
|
|
if h.notifiedTurns == nil {
|
|
h.notifiedTurns = map[string]struct{}{}
|
|
}
|
|
h.notifiedTurns[sessionID] = struct{}{}
|
|
}
|
|
|
|
// takeTurnNotifiedLocked reports and forgets whether sessionID's ending turn
|
|
// already sent an agent notify. Callers must hold h.mu.
|
|
func (h *chatHub) takeTurnNotifiedLocked(sessionID string) bool {
|
|
if _, ok := h.notifiedTurns[sessionID]; !ok {
|
|
return false
|
|
}
|
|
delete(h.notifiedTurns, sessionID)
|
|
return true
|
|
}
|
|
|
|
// finalReplyTextLocked returns a bounded excerpt, paragraph breaks kept, 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 ""
|
|
}
|
|
if message.kind != "" {
|
|
// An agent report or notice the harness delivered is not the reply.
|
|
continue
|
|
}
|
|
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.WriteString("\n\n")
|
|
}
|
|
text.Write(part.text)
|
|
if text.Len() > chatReplyBodyRunes*4 {
|
|
break
|
|
}
|
|
}
|
|
if excerpt := paragraphs(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 a finished reply's title and body. The body keeps the
|
|
// reply's paragraph breaks for the app's notifications; ntfy gets it on one
|
|
// line (see chatAttentionPublishBody).
|
|
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(paragraphs(body), 4000)
|
|
}
|
|
|
|
// paragraphs normalises text for a multi-line notification: lines trimmed,
|
|
// runs of blank lines collapsed to one, NULs and carriage returns dropped.
|
|
func paragraphs(text string) string {
|
|
text = strings.NewReplacer("\x00", "", "\r", "").Replace(text)
|
|
var out []string
|
|
blank := false
|
|
for _, line := range strings.Split(text, "\n") {
|
|
line = strings.Join(strings.Fields(line), " ")
|
|
if line == "" {
|
|
blank = len(out) > 0
|
|
continue
|
|
}
|
|
if blank {
|
|
out = append(out, "")
|
|
blank = false
|
|
}
|
|
out = append(out, line)
|
|
}
|
|
return strings.Join(out, "\n")
|
|
}
|