Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
fix: split unspaced speech at Unicode word boundaries
Afloat216ba4ea3b95
diff --git a/server/tts/speech-text.test.ts b/server/tts/speech-text.test.tsindex 8432848fa0..26ef3cbd3d 100644--- a/server/tts/speech-text.test.ts+++ b/server/tts/speech-text.test.ts@@ -146,6 +146,22 @@ describe("toUtterances", () => {expect(capped).toEqual(toUtterances(text));expect(capped[0]).toContain("a".repeat(95));});++ it.each(["漢".repeat(325), "日本語の文章を読み上げます。".repeat(40)])("bounds unspaced speech without dropping text", (text) => {+ const out = toUtterances(text, { maxChars: 320, firstBlockMaxChars: 90 });+ expect(out.length).toBeGreaterThan(1);+ expect(out[0]!.length).toBeLessThanOrEqual(90);+ expect(out.every((piece) => piece.length > 0 && piece.length <= 320)).toBe(true);+ expect(out.join("")).toBe(speakable(text));+ });++ it("keeps supplementary Unicode letters intact at an odd speech boundary", () => {+ const text = "𠮷".repeat(95);+ const out = toUtterances(text, { firstBlockMaxChars: 91 });+ expect(out[0]!.length).toBeLessThanOrEqual(91);+ expect(out.every((piece) => Buffer.from(piece).toString("utf8") === piece)).toBe(true);+ expect(out.join("")).toBe(text);+ });});describe("narrateTool", () => {diff --git a/server/tts/speech-text.ts b/server/tts/speech-text.tsindex 0ad4ae8cd5..73b61d6106 100644--- a/server/tts/speech-text.ts+++ b/server/tts/speech-text.ts@@ -96,6 +96,7 @@ export function speakable(input: string): string {export const VOICE_FIRST_BLOCK_MAX_CHARS = 90;const BOUNDARY = /(?<!\b(?:e\.g|i\.e|etc|vs|Dr|Mr|Mrs|Ms|No|approx))(?<![.\d])([.!?])(["')\]]*)\s+/g;+const WORD_SEGMENTER = new Intl.Segmenter(undefined, { granularity: "word" });export function toUtterances(input: string,@@ -121,7 +122,7 @@ export function toUtterances(const parts = piece.length <= maxChars ? [piece] : splitLong(piece, maxChars);for (const part of parts) {const prev = out[out.length - 1];- if (prev && (prev.length < minChars || part.length < minChars)) {+ if (prev && prev.length + part.length + 1 <= maxChars && (prev.length < minChars || part.length < minChars)) {out[out.length - 1] = `${prev} ${part}`;} else {out.push(part);@@ -143,7 +144,13 @@ function splitLong(text: string, maxChars: number): string[] {while (rest.length > maxChars) {const window = rest.slice(0, maxChars);const at = Math.max(window.lastIndexOf(", "), window.lastIndexOf("; "), window.lastIndexOf(" — "));- const cut = at > maxChars / 2 ? at + 1 : window.lastIndexOf(" ");+ let cut = at > maxChars / 2 ? at + 1 : window.lastIndexOf(" ");+ if (cut <= 0) {+ for (const { index } of WORD_SEGMENTER.segment(rest)) {+ if (index > maxChars) break;+ if (index > 0) cut = index;+ }+ }if (cut <= 0) break;out.push(rest.slice(0, cut).trim());rest = rest.slice(cut).trim();