From 895b23d2da6adf17986de123c55fd8596f931423 Mon Sep 17 00:00:00 2001 From: Alan Silva Date: Mon, 2 Nov 2026 17:08:19 +0000 Subject: [PATCH] Chat reply notifications: up to 1000 characters, paragraphs kept The finished-reply notification now carries up to 1000 characters of the reply (was 200), keeping its paragraph breaks for the app's expanded notification; ntfy still gets it on one line within its 2048-byte limit. --- internal/server/chat_attention.go | 3 +- internal/server/chat_reply_attention.go | 35 +++++++++++++++---- internal/server/chat_reply_paragraphs_test.go | 26 ++++++++++++++ 3 files changed, 57 insertions(+), 7 deletions(-) create mode 100644 internal/server/chat_reply_paragraphs_test.go diff --git a/internal/server/chat_attention.go b/internal/server/chat_attention.go index 15d02f6b..04721219 100644 --- a/internal/server/chat_attention.go +++ b/internal/server/chat_attention.go @@ -191,7 +191,8 @@ func (h *chatHub) startChatAttention(a chatAttention) { if !chatAttentionConfigured() || !settings.NtfyEnabled { return } - if _, err := chatAttentionPublish(ctx, "system", title, body, "", nil, click, views...); err != nil { + // ntfy takes a single-line message of at most 2048 bytes. + if _, err := chatAttentionPublish(ctx, "system", title, truncateUTF8(singleLine(body), 2000), "", nil, click, views...); err != nil { log.Printf("chat attention notification session=%s: %v", a.sessionID, err) } }() diff --git a/internal/server/chat_reply_attention.go b/internal/server/chat_reply_attention.go index 98abaa6b..35d0d686 100644 --- a/internal/server/chat_reply_attention.go +++ b/internal/server/chat_reply_attention.go @@ -8,7 +8,7 @@ import ( ) // chatReplyBodyRunes bounds the reply excerpt carried in the notification body. -const chatReplyBodyRunes = 200 +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). @@ -46,7 +46,7 @@ func (h *chatHub) noteReplyAttentionLocked(prior *protocol.ChatStep, frame proto 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 +// 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 { @@ -73,14 +73,14 @@ func (h *chatHub) finalReplyTextLocked(sessionID string) string { continue } if text.Len() > 0 { - text.WriteByte(' ') + text.WriteString("\n\n") } text.Write(part.text) if text.Len() > chatReplyBodyRunes*4 { break } } - if excerpt := singleLine(text.String()); excerpt != "" { + if excerpt := paragraphs(text.String()); excerpt != "" { return truncateRunes(excerpt, chatReplyBodyRunes) } } @@ -96,12 +96,35 @@ func truncateRunes(text string, limit int) string { return strings.TrimSpace(string(runes[:limit])) + "…" } -// chatReplyText builds the single-line ntfy title/body for a finished reply. +// 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(singleLine(body), 2000) + 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") } diff --git a/internal/server/chat_reply_paragraphs_test.go b/internal/server/chat_reply_paragraphs_test.go new file mode 100644 index 00000000..9233ea7e --- /dev/null +++ b/internal/server/chat_reply_paragraphs_test.go @@ -0,0 +1,26 @@ +package server + +import ( + "strings" + "testing" + "unicode/utf8" +) + +func TestReplyNotificationKeepsParagraphs(t *testing.T) { + got := paragraphs(" First line \r\n\r\n\n\n Second paragraph \nthird\x00 line\n\n") + if want := "First line\n\nSecond paragraph\nthird line"; got != want { + t.Fatalf("paragraphs = %q, want %q", got, want) + } + _, body := chatReplyText("Chat", "One\n\nTwo") + if body != "One\n\nTwo" { + t.Fatalf("app body should keep paragraph breaks, got %q", body) + } + // ntfy gets the same text on one line. + if flat := singleLine(body); strings.Contains(flat, "\n") || flat != "One Two" { + t.Fatalf("ntfy body = %q", flat) + } + long := truncateRunes(strings.Repeat("a", 1500), chatReplyBodyRunes) + if n := utf8.RuneCountInString(long); n != chatReplyBodyRunes+1 || chatReplyBodyRunes != 1000 { + t.Fatalf("excerpt is %d runes (limit %d)", n, chatReplyBodyRunes) + } +}