Skip to content

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

fix(tts): preserve literal identifiers in spoken replies

Afloat1c8a687f05ab
diff --git a/server/routes/tts.test.ts b/server/routes/tts.test.ts
index e1a52908f5..4d9785cd16 100644
--- a/server/routes/tts.test.ts
+++ b/server/routes/tts.test.ts
@@ -49,6 +49,14 @@ describe("voice routes", () => {
}
});
+ it("prepares configuration instructions without changing their literal names", async () => {
+ const { answers } = await call("POST", "/api/tts/prepare", { text: "Set **API_KEY** and `API_URL` in my_config_file.json." });
+ expect(answers[0]![0]).toBe(200);
+ expect((answers[0]![1] as { utterances: string[] }).utterances).toEqual([
+ "Set API_KEY and API_URL in my_config_file.json.",
+ ]);
+ });
+
it("list no voices, and say why, when no voice provider is set up", async () => {
const { answers } = await call("GET", "/api/tts/voices?q=calm");
expect(answers).toHaveLength(1);
diff --git a/server/tts/speech-text.test.ts b/server/tts/speech-text.test.ts
index 6ddab828fc..74029491e9 100644
--- a/server/tts/speech-text.test.ts
+++ b/server/tts/speech-text.test.ts
@@ -57,6 +57,18 @@ describe("speakable", () => {
expect(speakable("**Done** ✅ — [x] shipped the _thing_")).toBe("Done — shipped the thing");
});
+ it.each([
+ ["Set API_KEY and API_URL before running the command.", "Set API_KEY and API_URL before running the command."],
+ ["Open my_config_file.json.", "Open my_config_file.json."],
+ ["Use snake_case here and _emphasis_ there.", "Use snake_case here and emphasis there."],
+ ["Set **API_KEY** and **API_URL**.", "Set API_KEY and API_URL."],
+ ["Run `npm run build_app` and set `API_KEY`.", "Run npm run build_app and set API_KEY."],
+ ["Use `*.ts` and `*.js` to match files.", "Use *.ts and *.js to match files."],
+ ["***Important***: __do _not_ change__ the name.", "Important: do not change the name."],
+ ])("removes only Markdown emphasis from %j", (input, expected) => {
+ expect(speakable(input)).toBe(expected);
+ });
+
it("gives a heading a full stop so the voice breathes", () => {
expect(speakable("## Results\nAll green")).toBe("Results. All green");
});
diff --git a/server/tts/speech-text.ts b/server/tts/speech-text.ts
index 84c99f0a06..17b4f156dc 100644
--- a/server/tts/speech-text.ts
+++ b/server/tts/speech-text.ts
@@ -1,3 +1,33 @@
+import { fromMarkdown } from "mdast-util-from-markdown";
+
+type MarkdownNode = {
+ type: string;
+ children?: MarkdownNode[];
+ position?: { start: { offset?: number }; end: { offset?: number } };
+};
+
+function stripEmphasis(text: string): string {
+ const pending: MarkdownNode[] = [fromMarkdown(text)];
+ const markers: Array<[number, number]> = [];
+ while (pending.length) {
+ const node = pending.pop()!;
+ if (node.children) pending.push(...node.children);
+ if (node.type !== "emphasis" && node.type !== "strong") continue;
+ const start = node.position?.start.offset;
+ const end = node.position?.end.offset;
+ if (start === undefined || end === undefined) continue;
+ const width = node.type === "strong" ? 2 : 1;
+ markers.push([start, start + width], [end - width, end]);
+ }
+ const parts: string[] = [];
+ let cursor = 0;
+ for (const [start, end] of markers.sort((a, b) => a[0] - b[0])) {
+ parts.push(text.slice(cursor, start));
+ cursor = end;
+ }
+ return parts.join("") + text.slice(cursor);
+}
+
function describeCodeBlock(fence: string): string {
const lang = fence.trim().split(/\s+/)[0]?.replace(/[^a-z0-9+#]/gi, "") ?? "";
type SpokenLanguages = Record<string, string>;
@@ -36,6 +66,7 @@ export function speakable(input: string): string {
// Fenced code first: nothing inside it may meet the rules below.
text = text.replace(/```([^\n]*)\n[\s\S]*?(?:```|$)/g, (_m, fence: string) => describeCodeBlock(fence));
text = text.replace(/~~~([^\n]*)\n[\s\S]*?(?:~~~|$)/g, (_m, fence: string) => describeCodeBlock(fence));
+ text = stripEmphasis(text);
// Images before links: the syntax differs by one character.
text = text.replace(/!\[([^\]]*)\]\([^)]*\)/g, (_m, alt: string) => (alt ? `. (image: ${alt}) ` : ". (an image) "));
@@ -63,8 +94,6 @@ export function speakable(input: string): string {
text = text.replace(/^\s*>\s?/gm, "");
text = text.replace(/^\s*(?:[-*_]\s*){3,}$/gm, "");
- text = text.replace(/(\*\*|__)(.*?)\1/g, "$2");
- text = text.replace(/(\*|_)(?=\S)(.*?)(?<=\S)\1/g, "$2");
text = text.replace(/~~(.*?)~~/g, "$1");
text = text.replace(/\[[ xX]\]\s*/g, "");