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.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 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") }