Skip to content

Personal AI, On Personal Devices

Python

fix(frontend): read replies aloud without markdown symbols (#1145)

Wagner Gadea Lorenzedf3cafeb369
diff --git a/frontend/src/lib/message-text.test.ts b/frontend/src/lib/message-text.test.ts
new file mode 100644
index 00000000..553d473b
--- /dev/null
+++ b/frontend/src/lib/message-text.test.ts
@@ -0,0 +1,31 @@
+import { describe, expect, it } from 'vitest';
+import { toSpeakableText } from './message-text';
+
+describe('toSpeakableText', () => {
+ it('removes emphasis markers so they are not read aloud', () => {
+ expect(toSpeakableText('This is **very important** and *useful*.')).toBe(
+ 'This is very important and useful.',
+ );
+ });
+
+ it('drops headings, bullets and numbering and pauses between lines', () => {
+ const out = toSpeakableText('## Steps\n- first\n- second\n1. third');
+ expect(out).toBe('Steps. first. second. third.');
+ });
+
+ it('keeps link text, drops URLs and inline code ticks', () => {
+ expect(toSpeakableText('See [the docs](https://x.dev/a) and run `ls` now. https://x.dev')).toBe(
+ 'See the docs and run ls now.',
+ );
+ });
+
+ it('replaces code blocks and removes emoji and think tags', () => {
+ const out = toSpeakableText('<think>hmm</think>Done πŸ˜€\n```py\nprint(1)\n```');
+ expect(out).toContain('code block omitted');
+ expect(out).not.toMatch(/print|πŸ˜€|think|hmm/);
+ });
+
+ it('keeps snake_case words intact', () => {
+ expect(toSpeakableText('use file_read now')).toBe('use file_read now');
+ });
+});
diff --git a/frontend/src/lib/message-text.ts b/frontend/src/lib/message-text.ts
index e994bc02..061ea4e1 100644
--- a/frontend/src/lib/message-text.ts
+++ b/frontend/src/lib/message-text.ts
@@ -6,3 +6,29 @@ export function stripThinkTags(text: string): string {
cleaned = cleaned.replace(/^[\s\S]*?<\/think>\s*/i, '');
return cleaned.trim();
}
+
+/**
+ * Turn a (usually markdown) answer into text that sounds right when read aloud: no emphasis
+ * markers, heading hashes, list bullets, link syntax, bare URLs or emoji, code blocks replaced
+ * by a short note, and a pause after lines that lack final punctuation.
+ */
+export function toSpeakableText(text: string): string {
+ let t = stripThinkTags(text);
+ t = t.replace(/```[\s\S]*?```/g, ' (code block omitted). ');
+ t = t.replace(/`([^`]*)`/g, '$1');
+ t = t.replace(/\[([^\]]+)\]\([^)]+\)/g, '$1');
+ t = t.replace(/https?:\/\/\S+/g, '');
+ t = t.replace(/^\s{0,3}#{1,6}\s*/gm, '');
+ t = t.replace(/^\s*(?:[-*β€’]|\d+[.)])\s+/gm, '');
+ t = t.replace(/\*{1,3}|~~/g, '');
+ t = t.replace(/(?<![\p{L}\p{N}])_+|_+(?![\p{L}\p{N}])/gu, '');
+ t = t.replace(/[\p{Extended_Pictographic}️‍]/gu, '');
+ const lines = t
+ .split(/\r?\n/)
+ .map((l) => l.trim())
+ .filter(Boolean);
+ // Only separate multiple lines; a single line is spoken as written.
+ const sentences =
+ lines.length > 1 ? lines.map((l) => (/[.!?:;]$/.test(l) ? l : `${l}.`)) : lines;
+ return sentences.join(' ').replace(/\s{2,}/g, ' ').trim();
+}
diff --git a/frontend/src/lib/tts.ts b/frontend/src/lib/tts.ts
index b331e86a..b1e9617c 100644
--- a/frontend/src/lib/tts.ts
+++ b/frontend/src/lib/tts.ts
@@ -1,5 +1,6 @@
import { create } from 'zustand';
import { synthesizeSpeech, fetchTtsHealth } from './api';
+import { toSpeakableText } from './message-text';
export type TtsState = 'idle' | 'loading' | 'speaking';
@@ -98,7 +99,7 @@ export const useTtsStore = create<TtsStore>((set, get) => ({
markAutoSpoken: (id: string) => set({ autoSpokenId: id }),
speak: async (id: string, text: string) => {
- const trimmed = text.trim();
+ const trimmed = toSpeakableText(text);
if (!trimmed) return;
// Bump before teardown so a synthesis still in flight is both aborted and