Skip to content

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

fix(tts): keep punctuation attached and bound Katakana graphemes

Afloatb820dff83ae5
diff --git a/server/tts/speech-text.test.ts b/server/tts/speech-text.test.ts
index 26ef3cbd3d..6ddab828fc 100644
--- a/server/tts/speech-text.test.ts
+++ b/server/tts/speech-text.test.ts
@@ -162,6 +162,32 @@ describe("toUtterances", () => {
expect(out.every((piece) => Buffer.from(piece).toString("utf8") === piece)).toBe(true);
expect(out.join("")).toBe(text);
});
+ it("keeps a terminal period attached to a word at either speech cap", () => {
+ const long = `${"a".repeat(320)}.`;
+ expect(toUtterances(long)).toEqual([long]);
+ const first = `${"a".repeat(90)}.`;
+ expect(toUtterances(first, { firstBlockMaxChars: 90 })).toEqual([first]);
+ });
+
+ it("does not turn punctuation around an indivisible word into an utterance", () => {
+ const text = `(${"a".repeat(320)}).`;
+ expect(toUtterances(text)).toEqual([text]);
+ });
+
+ it("caps a decomposed Katakana word without splitting its graphemes", () => {
+ const text = "カ\u3099".repeat(321);
+ const out = toUtterances(text, { maxChars: 320, firstBlockMaxChars: 91 });
+ expect(out.length).toBeGreaterThan(1);
+ expect(out[0]!.length).toBeLessThanOrEqual(91);
+ expect(out.every((piece) => piece.length > 0 && piece.length <= 320 && /^(?:カ\u3099)+$/u.test(piece))).toBe(true);
+ expect(out.join("")).toBe(speakable(text));
+ });
+
+ it("keeps Latin words with combining marks indivisible", () => {
+ const text = `${"e\u0301".repeat(200)}.`;
+ expect(toUtterances(text, { firstBlockMaxChars: 91 })).toEqual([text]);
+ });
+
});
describe("narrateTool", () => {
diff --git a/server/tts/speech-text.ts b/server/tts/speech-text.ts
index 73b61d6106..84c99f0a06 100644
--- a/server/tts/speech-text.ts
+++ b/server/tts/speech-text.ts
@@ -97,6 +97,7 @@ 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" });
+const GRAPHEME_SEGMENTER = new Intl.Segmenter(undefined, { granularity: "grapheme" });
export function toUtterances(
input: string,
@@ -146,10 +147,7 @@ function splitLong(text: string, maxChars: number): string[] {
const at = Math.max(window.lastIndexOf(", "), window.lastIndexOf("; "), 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;
- }
+ cut = unspacedCut(rest, maxChars);
}
if (cut <= 0) break;
out.push(rest.slice(0, cut).trim());
@@ -159,6 +157,24 @@ function splitLong(text: string, maxChars: number): string[] {
return out;
}
+function unspacedCut(text: string, maxChars: number): number {
+ let firstWord: Intl.SegmentData | undefined;
+ let cut = -1;
+ for (const segment of WORD_SEGMENTER.segment(text)) {
+ if (segment.index > maxChars) break;
+ if (!segment.isWordLike) continue;
+ if (firstWord) cut = segment.index;
+ else firstWord = segment;
+ }
+ if (cut > 0 || !firstWord || !/^[\p{Script_Extensions=Katakana}\p{M}]+$/u.test(firstWord.segment)) return cut;
+ for (const { index } of GRAPHEME_SEGMENTER.segment(firstWord.segment)) {
+ const boundary = firstWord.index + index;
+ if (boundary > maxChars) break;
+ if (index > 0) cut = boundary;
+ }
+ return cut;
+}
+
export function narrateTool(toolName: string): string | null {
const name = toolName.replace(/^mcp__[^_]+__/, "").trim();
if (!name) return null;