Merge branch 'main' into viewer-controls-bigger
This commit is contained in:
@@ -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)
|
||||
}
|
||||
}()
|
||||
|
||||
@@ -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")
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user