fix(voice): stop backtick TTS crash and actually count error turns
Claude replies with markdown/backticks by default; MeloTTS's Korean text
normaliser has no entry for '`' and dies with KeyError: '`', so any reply
mentioning a command/code block crashed the whole voice turn (500 on
/api/voice-turn). Fix at the shared synth() choke point with
normalize_for_speech(), which flattens code fences/inline code/links/markdown
and guarantees no backtick reaches the worker — covering both the dashboard
voice turn and the Discord speak() bridge. Also add a PERSONA line asking the
model to avoid markdown (belt-and-suspenders; the code strip is the real fix).
errors_total never moved for turn-level failures: it was only bumped by
log("error") events, and the dashboard voice path calls turn.finish(error=...)
without logging. Emit one error-level log event from Turn.finish() when a turn
ends in error, so both the server counter and the browser SSE mirror stay
consistent, guarded to count at most once. Drop the now-redundant pipeline
log("error") to avoid double counting and remove the dead _publish stub.
Verified: raw backtick -> worker KeyError '`' reproduced; after fix real
MeloTTS synth of a backtick+fenced reply succeeds; /api/voice-turn returns 200
with a wav body on a backtick reply and errors_total stays 0, and an induced
synth failure returns 500 with errors_total incrementing to exactly 1. Full
suite 18 passed.
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
@@ -125,7 +125,8 @@ class ClaudeBrain:
|
||||
PERSONA = (
|
||||
"너는 사용자의 디스코드 화면공유를 실시간으로 함께 보는 AI 파트너야. "
|
||||
"화면 설명을 참고해 자연스러운 반말/존댓말은 사용자에 맞추고, 짧고 대화하듯 답해. "
|
||||
"화면을 못 봤으면 솔직히 말해."
|
||||
"화면을 못 봤으면 솔직히 말해. "
|
||||
"네 답변은 음성으로 읽히니 마크다운·코드블록·백틱 같은 서식 없이 평범한 말로만 답해."
|
||||
)
|
||||
|
||||
def __init__(self, *, model: str = "claude-sonnet-4-5", api_key: str | None = None) -> None:
|
||||
|
||||
@@ -22,6 +22,7 @@ import collections
|
||||
import json
|
||||
import logging
|
||||
import os
|
||||
import re
|
||||
import time
|
||||
from pathlib import Path
|
||||
from typing import Awaitable, Callable
|
||||
@@ -32,6 +33,42 @@ log = logging.getLogger("wsai.tts.melo")
|
||||
|
||||
_DEFAULT_PYTHON = "/home/claude/jarvis-tts/melo311/bin/python"
|
||||
|
||||
_FENCE_RE = re.compile(r"```[^\n`]*\n?(.*?)```", re.DOTALL)
|
||||
_LINK_RE = re.compile(r"\[([^\]]+)\]\([^)]*\)")
|
||||
_INLINE_CODE_RE = re.compile(r"`+([^`]*)`+")
|
||||
|
||||
|
||||
def normalize_for_speech(text: str) -> str:
|
||||
"""Flatten Claude's markdown/code formatting into plain prose before TTS.
|
||||
|
||||
MeloTTS's Korean text normaliser has no dictionary entry for characters
|
||||
like the backtick and dies with ``KeyError: '`'`` — which crashes the whole
|
||||
voice turn the moment the model mentions a command or shows a code block.
|
||||
Code/markdown also reads terribly aloud. So strip the formatting and keep
|
||||
the words. Every spoken path (dashboard voice turn and the Discord speak()
|
||||
bridge) funnels through ``synth()``, so normalising there covers them both.
|
||||
"""
|
||||
if not text:
|
||||
return text
|
||||
# Fenced code block -> keep its inner text as spoken words, drop the fences.
|
||||
text = _FENCE_RE.sub(lambda m: " " + m.group(1) + " ", text)
|
||||
# [label](url) -> label
|
||||
text = _LINK_RE.sub(r"\1", text)
|
||||
# `code` -> code
|
||||
text = _INLINE_CODE_RE.sub(r"\1", text)
|
||||
# Any stray/unbalanced backtick that survived -> gone. This is the exact
|
||||
# character that crashes MeloTTS, so guarantee none remain.
|
||||
text = text.replace("`", "")
|
||||
# Markdown structure markers -> plain text.
|
||||
text = re.sub(r"(?m)^\s{0,3}#{1,6}\s*", "", text) # ATX headings
|
||||
text = re.sub(r"(?m)^\s{0,3}>\s?", "", text) # blockquotes
|
||||
text = re.sub(r"(?m)^\s{0,3}[-*+]\s+", "", text) # bullet list markers
|
||||
text = re.sub(r"[*_]{1,3}", "", text) # bold/italic emphasis
|
||||
# Collapse the whitespace the stripping leaves behind.
|
||||
text = re.sub(r"[ \t]+", " ", text)
|
||||
text = re.sub(r"\n{2,}", "\n", text)
|
||||
return text.strip()
|
||||
|
||||
# A sink receives the finished wav path plus the reply it voices.
|
||||
Sink = Callable[[str, Reply], Awaitable[None]]
|
||||
|
||||
@@ -135,6 +172,7 @@ class MeloTTS:
|
||||
"""Synthesize `text` to a wav and return its path (no sink). Reusable by
|
||||
callers that want the wav directly (e.g. the Discord voice bridge)."""
|
||||
await self._ensure()
|
||||
text = normalize_for_speech(text)
|
||||
self._n += 1
|
||||
out = str(self.out_dir / f"tts-{self._n:06d}.wav")
|
||||
req = json.dumps({"text": text, "out": out, "speed": self.speed})
|
||||
|
||||
Reference in New Issue
Block a user