Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

fix(notify): truncate previews at grapheme boundaries

mschoi04112223dfd990e8
diff --git a/server/notify.test.ts b/server/notify.test.ts
index 8befdfbc9a..4c1e336402 100644
--- a/server/notify.test.ts
+++ b/server/notify.test.ts
@@ -122,6 +122,12 @@ describe("summarize", () => {
expect(summarize("short")).toBe("short");
});
+ it.each(["🙂", "e\u0301", "👩‍💻"])("keeps %s whole when truncating a notification preview", (grapheme) => {
+ const prefix = "x".repeat(138);
+ const frame = buildNotification("done", bot, bot.threadId, `${prefix}${grapheme} done`);
+ expect(frame?.body).toBe(`${prefix}…`);
+ });
+
it("reads a bot's Markdown as the plain line a lock screen shows", () => {
expect(summarize("**Facture reçue** — consulter le [document](https://example.com/facture)"))
.toBe("Facture reçue — consulter le document");
diff --git a/server/notify.ts b/server/notify.ts
index 5a409c8450..396ddf3201 100644
--- a/server/notify.ts
+++ b/server/notify.ts
@@ -5,10 +5,15 @@ import { conversationLanguage, serverLine, type ServerLineKey } from "./server-l
export type { Notification, NotifyKind } from "../shared/notification.ts";
+const graphemes = new Intl.Segmenter(undefined, { granularity: "grapheme" });
+
/** Fenced code stays out of a banner, as before; the rest of the Markdown becomes the readable line a lock screen shows. */
export function summarize(text: string, max = 140): string {
const line = plainPreviewText(text.replace(/```[\s\S]*?```/g, " "));
- return line.length > max ? `${line.slice(0, max - 1).trimEnd()}…` : line;
+ if (line.length <= max) return line;
+ const cutoff = max - 1;
+ const boundary = graphemes.segment(line).containing(cutoff)?.index ?? cutoff;
+ return `${line.slice(0, boundary).trimEnd()}…`;
}
export interface NotifyBot {