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