Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.
Go
perf(chat): cap session list last-message preview at 256 chars (MUL-8627) (#11428)
Bohan Jiangca089ccdcfa2
diff --git a/packages/core/types/chat.ts b/packages/core/types/chat.tsindex c5e33ee3e1..72ed480260 100644--- a/packages/core/types/chat.ts+++ b/packages/core/types/chat.ts@@ -71,6 +71,8 @@ export interface ChatQuickActionsFailureState {/** Preview of a session's most recent message, for the IM-style list. */export interface ChatLastMessage {+ /** One-line preview with code fences and Markdown markers removed, whitespace+ * collapsed, and a cap of 256 Unicode code points. */content: string;role: "user" | "assistant";created_at: string;diff --git a/server/internal/handler/chat.go b/server/internal/handler/chat.goindex 60ebfedca6..55af3d1239 100644--- a/server/internal/handler/chat.go+++ b/server/internal/handler/chat.go@@ -9,6 +9,8 @@ import ("strconv""strings""time"+ "unicode"+ "unicode/utf8""github.com/go-chi/chi/v5""github.com/jackc/pgx/v5"@@ -2124,6 +2126,55 @@ type ChatLastMessage struct {MessageKind string `json:"message_kind"`}+// chatLastMessagePreviewMaxRunes caps ChatLastMessage.Content. The list only+// renders a one-line snippet, but shipping each session's full last reply made+// the list payload grow with every long agent answer (#9954).+const chatLastMessagePreviewMaxRunes = 256++// chatLastMessagePreview mirrors the list client's cleanup before applying the+// cap. Complete fences become whitespace, and Markdown markers are dropped before+// counting runes so they cannot crowd out visible text.+func chatLastMessagePreview(content string) string {+ const fence = "```"+ var preview strings.Builder+ preview.Grow(min(len(content), chatLastMessagePreviewMaxRunes))+ remaining := chatLastMessagePreviewMaxRunes+ pendingSpace := false++ for len(content) > 0 && remaining > 0 {+ if strings.HasPrefix(content, fence) {+ if end := strings.Index(content[len(fence):], fence); end >= 0 {+ content = content[end+2*len(fence):]+ pendingSpace = preview.Len() > 0+ continue+ }+ }++ r, width := utf8.DecodeRuneInString(content)+ content = content[width:]+ switch r {+ case '#', '*', '`', '>', '~':+ continue+ }+ // JavaScript's whitespace includes BOM and excludes NEXT LINE.+ if r == '\uFEFF' || (r != '\u0085' && unicode.IsSpace(r)) {+ pendingSpace = preview.Len() > 0+ continue+ }+ if pendingSpace {+ if remaining == 1 {+ break+ }+ preview.WriteByte(' ')+ remaining--+ pendingSpace = false+ }+ preview.WriteRune(r)+ remaining--+ }+ return preview.String()+}+// buildChatLastMessage assembles the preview from list-row columns; returns nil// when there is no last message (the LEFT JOIN produced a NULL timestamp).func buildChatLastMessage(at pgtype.Timestamptz, content, role string, failure pgtype.Text, kind string) *ChatLastMessage {@@ -2134,7 +2185,7 @@ func buildChatLastMessage(at pgtype.Timestamptz, content, role string, failure pcontent = ""}return &ChatLastMessage{- Content: content,+ Content: chatLastMessagePreview(content),Role: role,CreatedAt: timestampToString(at),FailureReason: textToPtr(failure),diff --git a/server/internal/handler/chat_last_message_preview_test.go b/server/internal/handler/chat_last_message_preview_test.gonew file mode 100644index 0000000000..d9b993f799--- /dev/null+++ b/server/internal/handler/chat_last_message_preview_test.go@@ -0,0 +1,56 @@+package handler++import (+ "strings"+ "testing"+ "unicode/utf8"++ "github.com/jackc/pgx/v5/pgtype"+ "github.com/multica-ai/multica/server/pkg/protocol"+)++func TestChatLastMessagePreview(t *testing.T) {+ long := strings.Repeat("回复", chatLastMessagePreviewMaxRunes)+ cases := []struct {+ name string+ in string+ want string+ }{+ {"short message unchanged", "Done. See the PR.", "Done. See the PR."},+ {"whitespace collapsed", "line one\n\n line two\t", "line one line two"},+ {"leading code fence dropped before truncating",+ "```go\n" + strings.Repeat("x := 1\n", 100) + "```\nThe fix is above.",+ "The fix is above."},+ {"unclosed fence keeps its text", "```go\nx := 1", "go x := 1"},+ {"leading Markdown markers do not use the preview budget",+ strings.Repeat("*", chatLastMessagePreviewMaxRunes) + "\n部署完成", "部署完成"},+ {"Markdown markers removed before collapsing whitespace", "# **Done** \n> See `logs` ~soon~.", "Done See logs soon."},+ {"matches client whitespace", "\ufeffDone\u00a0\t today\u0085.", "Done today\u0085."},+ {"does not end with a truncated separator",+ strings.Repeat("a", chatLastMessagePreviewMaxRunes-1) + " b",+ strings.Repeat("a", chatLastMessagePreviewMaxRunes-1)},+ {"capped by runes, not bytes", long, string([]rune(long)[:chatLastMessagePreviewMaxRunes])},+ }+ for _, tc := range cases {+ t.Run(tc.name, func(t *testing.T) {+ got := chatLastMessagePreview(tc.in)+ if got != tc.want {+ t.Fatalf("chatLastMessagePreview() = %q, want %q", got, tc.want)+ }+ if !utf8.ValidString(got) {+ t.Fatalf("preview is not valid UTF-8: %q", got)+ }+ })+ }+}++func TestBuildChatLastMessageCapsContent(t *testing.T) {+ at := pgtype.Timestamptz{Valid: true}+ msg := buildChatLastMessage(at, strings.Repeat("a", 20_000), "assistant", pgtype.Text{}, protocol.ChatMessageKindMessage)+ if msg == nil {+ t.Fatal("buildChatLastMessage returned nil")+ }+ if n := utf8.RuneCountInString(msg.Content); n != chatLastMessagePreviewMaxRunes {+ t.Fatalf("content length = %d runes, want %d", n, chatLastMessagePreviewMaxRunes)+ }+}