Skip to content

DeepTutor: Lifelong Personalized Tutoring. https://deeptutor.info/.

fix(voice): stop speaking asterisks and drop the play-aloud auto-play prompt

Anthony4b1bcba8a907
diff --git a/deeptutor/services/voice/base.py b/deeptutor/services/voice/base.py
index 7c22eae8f..dce58d63f 100644
--- a/deeptutor/services/voice/base.py
+++ b/deeptutor/services/voice/base.py
@@ -145,13 +145,36 @@ _LINK = re.compile(r"\[([^\]]+)\]\([^)]*\)")
_HEADING = re.compile(r"^\s{0,3}#{1,6}\s*", re.MULTILINE)
_BLOCKQUOTE = re.compile(r"^\s{0,3}>\s?", re.MULTILINE)
_LIST_MARKER = re.compile(r"^\s{0,3}(?:[-*+]|\d+[.)])\s+", re.MULTILINE)
-_EMPHASIS = re.compile(r"(\*{1,3}|_{1,3}|~~)(\S.*?\S|\S)\1")
+_BOLD_STARS = re.compile(r"\*\*(.+?)\*\*", re.DOTALL)
+_BOLD_UNDERS = re.compile(r"__(.+?)__", re.DOTALL)
+_STRIKE = re.compile(r"~~(.+?)~~", re.DOTALL)
+_ITALIC_STARS = re.compile(r"(?<!\*)\*(?!\*)(.+?)(?<!\*)\*(?!\*)", re.DOTALL)
+_ITALIC_UNDERS = re.compile(r"(?<!\w)_(?!_)(.+?)(?<!_)_(?!\w)", re.DOTALL)
_HTML_TAG = re.compile(r"<[^>]+>")
_TABLE_PIPE = re.compile(r"^\s*\|.*\|\s*$", re.MULTILINE)
_WHITESPACE = re.compile(r"[ \t]+")
_BLANK_LINES = re.compile(r"\n{3,}")
+def _unwrap_emphasis_for_speech(text: str) -> str:
+ """Turn Markdown bold/italic/strike into plain words.
+
+ Voice models otherwise speak ``*`` as "asterisk". Math must already have
+ been verbalized so TeX ``*`` / ``_`` inside ``$…$`` is not treated as
+ emphasis. Leftover unmatched ``**`` markers are dropped; a remaining
+ single ``*`` is turned into a space so "asterisk" is never read.
+ """
+ out = _BOLD_STARS.sub(r"\1", text)
+ out = _BOLD_UNDERS.sub(r"\1", out)
+ out = _STRIKE.sub(r"\1", out)
+ out = _ITALIC_STARS.sub(r"\1", out)
+ out = _ITALIC_UNDERS.sub(r"\1", out)
+ out = out.replace("**", "")
+ out = out.replace("__", "")
+ out = out.replace("*", " ")
+ return out
+
+
def strip_markdown_for_speech(
text: str, *, max_chars: int = 0, math_speak: bool = True
) -> str:
@@ -180,7 +203,7 @@ def strip_markdown_for_speech(
# the emphasis regex would otherwise pair a prose underscore with one
# inside `$x_i$`.
out = verbalize_latex_for_speech(out, math_speak=math_speak)
- out = _EMPHASIS.sub(r"\2", out)
+ out = _unwrap_emphasis_for_speech(out)
out = _WHITESPACE.sub(" ", out)
out = _BLANK_LINES.sub("\n\n", out).strip()
if max_chars and len(out) > max_chars:
diff --git a/tests/services/test_voice.py b/tests/services/test_voice.py
index cf3b2d695..f7a55aaa0 100644
--- a/tests/services/test_voice.py
+++ b/tests/services/test_voice.py
@@ -115,6 +115,15 @@ def test_strip_markdown_drops_code_and_unwraps_links() -> None:
assert "Title" in out and "Hello world" in out and "the docs" in out
assert "print(1)" not in out # fenced code dropped
assert "**" not in out and "[" not in out and "#" not in out
+ assert "*" not in out
+
+
+def test_strip_markdown_does_not_speak_asterisks_for_bold() -> None:
+ out = strip_markdown_for_speech("This is **bold** and *italic* and ** $x^2$ **.")
+ assert "*" not in out
+ assert "bold" in out
+ assert "italic" in out
+ assert "squared" in out
def test_strip_markdown_truncates_on_boundary() -> None:
diff --git a/web/features/chat/messages/ChatMessageList.tsx b/web/features/chat/messages/ChatMessageList.tsx
index 19de731d7..39378a31a 100644
--- a/web/features/chat/messages/ChatMessageList.tsx
+++ b/web/features/chat/messages/ChatMessageList.tsx
@@ -51,6 +51,7 @@ import type {
MessageRequestSnapshot,
} from "@/features/chat/ChatStateAdapter";
import { apiFetch, apiUrl } from "@/lib/api";
+import { notify } from "@/lib/notifications";
import { docIconFor } from "@/lib/doc-attachments";
import { useVoiceAutoplay } from "@/hooks/useVoiceAutoplay";
import { extractMathAnimatorResult } from "@/lib/math-animator-types";
@@ -1285,10 +1286,24 @@ export function CopyActionButton({
);
}
-// Speaker button: synthesizes the reply via the configured TTS provider and
-// plays it. On the first manual play of a session it offers to auto-play the
-// rest; `autoPlayFresh` triggers playback automatically for a reply that just
-// finished generating when auto-play is on.
+// Speaker button: synthesizes this one reply and plays it. Auto-play of later
+// replies is a Settings preference (`autoPlayFresh`), not a first-click prompt.
+let stopActivePlayback: (() => void) | null = null;
+
+async function ttsErrorMessage(resp: Response): Promise<string> {
+ try {
+ const body = (await resp.json()) as { detail?: unknown };
+ if (typeof body.detail === "string" && body.detail.trim()) return body.detail;
+ if (Array.isArray(body.detail)) {
+ const first = body.detail[0] as { msg?: string } | undefined;
+ if (typeof first?.msg === "string" && first.msg.trim()) return first.msg;
+ }
+ } catch {
+ /* non-JSON error body */
+ }
+ return "";
+}
+
export function PlayAudioButton({
content,
conversationKey,
@@ -1299,19 +1314,17 @@ export function PlayAudioButton({
autoPlayFresh: boolean;
}) {
const { t } = useTranslation();
- const {
- autoplayEnabled,
- enableForSession,
- markPrompted,
- shouldPromptOnFirstPlay,
- } = useVoiceAutoplay(conversationKey);
+ const { autoplayEnabled } = useVoiceAutoplay(conversationKey);
const [state, setState] = useState<"idle" | "loading" | "playing">("idle");
- const [showPrompt, setShowPrompt] = useState(false);
const audioRef = useRef<HTMLAudioElement | null>(null);
const urlRef = useRef<string | null>(null);
+ const abortRef = useRef<AbortController | null>(null);
+ const genRef = useRef(0);
const autoPlayedRef = useRef(false);
const cleanup = useCallback(() => {
+ abortRef.current?.abort();
+ abortRef.current = null;
if (audioRef.current) {
audioRef.current.pause();
audioRef.current = null;
@@ -1322,57 +1335,86 @@ export function PlayAudioButton({
}
}, []);
+ const stop = useCallback(() => {
+ genRef.current += 1;
+ cleanup();
+ setState("idle");
+ if (stopActivePlayback === stop) stopActivePlayback = null;
+ }, [cleanup]);
+
const play = useCallback(async () => {
+ stopActivePlayback?.();
+ const gen = ++genRef.current;
+ abortRef.current?.abort();
+ const ac = new AbortController();
+ abortRef.current = ac;
+ stopActivePlayback = stop;
setState("loading");
try {
const resp = await apiFetch(apiUrl("/api/voice/tts"), {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ text: content }),
+ signal: ac.signal,
});
+ if (gen !== genRef.current) return;
if (!resp.ok) {
- cleanup();
- setState("idle");
+ const detail = await ttsErrorMessage(resp);
+ notify(
+ detail || t("Could not play this reply. Check Text-to-Speech in Settings."),
+ { tone: "error" },
+ );
+ stop();
return;
}
const blob = await resp.blob();
- cleanup();
+ if (gen !== genRef.current) return;
+ if (urlRef.current) {
+ URL.revokeObjectURL(urlRef.current);
+ urlRef.current = null;
+ }
const url = URL.createObjectURL(blob);
urlRef.current = url;
const audio = new Audio(url);
audioRef.current = audio;
audio.onended = () => {
+ if (gen !== genRef.current) return;
setState("idle");
cleanup();
+ if (stopActivePlayback === stop) stopActivePlayback = null;
};
audio.onerror = () => {
- setState("idle");
- cleanup();
+ if (gen !== genRef.current) return;
+ notify(t("Could not play this reply. Check Text-to-Speech in Settings."), {
+ tone: "error",
+ });
+ stop();
};
await audio.play();
+ if (gen !== genRef.current) {
+ audio.pause();
+ return;
+ }
setState("playing");
- } catch {
- cleanup();
- setState("idle");
+ } catch (err) {
+ if (gen !== genRef.current) return;
+ if (err instanceof Error && err.name === "AbortError") return;
+ notify(t("Could not play this reply. Check Text-to-Speech in Settings."), {
+ tone: "error",
+ });
+ stop();
}
- }, [cleanup, content]);
+ }, [cleanup, content, stop, t]);
const handleClick = useCallback(() => {
if (state === "playing" || state === "loading") {
- cleanup();
- setState("idle");
+ stop();
return;
}
- const willPrompt = shouldPromptOnFirstPlay();
void play();
- if (willPrompt) {
- markPrompted();
- setShowPrompt(true);
- }
- }, [cleanup, markPrompted, play, shouldPromptOnFirstPlay, state]);
+ }, [play, state, stop]);
- // Auto-play a freshly-generated reply when enabled, exactly once. Deferred
- // to a timer so synthesis (which sets state) starts off the effect body.
+ // Auto-play a freshly-generated reply when Settings auto-play is on.
useEffect(() => {
if (!autoPlayFresh || !autoplayEnabled) return;
if (autoPlayedRef.current) return;
@@ -1409,32 +1451,6 @@ export function PlayAudioButton({
)}
</button>
</Tooltip>
- {showPrompt && (
- <div className="absolute bottom-full left-0 z-30 mb-2 w-60 rounded-lg border border-[var(--border)] bg-[var(--card)] p-3 shadow-lg">
- <p className="text-[12px] leading-relaxed text-[var(--foreground)]">
- {t("Auto-play replies in this conversation?")}
- </p>
- <div className="mt-2.5 flex justify-end gap-2">
- <button
- type="button"
- onClick={() => setShowPrompt(false)}
- className="rounded-md px-2.5 py-1 text-[11.5px] text-[var(--muted-foreground)] hover:bg-[var(--muted)]/50 hover:text-[var(--foreground)]"
- >
- {t("Not now")}
- </button>
- <button
- type="button"
- onClick={() => {
- enableForSession();
- setShowPrompt(false);
- }}
- className="rounded-md bg-[var(--primary)] px-2.5 py-1 text-[11.5px] font-medium text-[var(--primary-foreground)] hover:bg-[var(--primary)]/90"
- >
- {t("Turn on")}
- </button>
- </div>
- </div>
- )}
</div>
);
}
diff --git a/web/locales/en/app.json b/web/locales/en/app.json
index cbcae00d8..107feac83 100644
--- a/web/locales/en/app.json
+++ b/web/locales/en/app.json
@@ -4937,6 +4937,7 @@
"Subagent": "Subagent",
"File": "File",
"Play aloud": "Play aloud",
+ "Could not play this reply. Check Text-to-Speech in Settings.": "Could not play this reply. Check Text-to-Speech in Settings.",
"Auto-play replies in this conversation?": "Auto-play replies in this conversation?",
"Turn on": "Turn on",
"items": "items",
diff --git a/web/locales/zh/app.json b/web/locales/zh/app.json
index e5c800042..a4aaeb0bb 100644
--- a/web/locales/zh/app.json
+++ b/web/locales/zh/app.json
@@ -4937,6 +4937,7 @@
"Subagent": "子智能体",
"File": "文件",
"Play aloud": "朗读",
+ "Could not play this reply. Check Text-to-Speech in Settings.": "无法朗读这条回复。请检查设置中的文本转语音。",
"Auto-play replies in this conversation?": "自动朗读此对话中的回复?",
"Turn on": "开启",
"items": "项",