feat(voice): in a game, partial transcription waits while an answer is written

- the app's ready message says whether the game stage runs (llm ready game=true); the worker then
  holds partial passes while a reply is being written and runs them again, the newest audio per
  speaker, when no answer is; finals never wait; without a game nothing changes
- M0 gamemode_tune decision: the game placement answered 1.7 times faster with partials held
  (64.9 against 37.5 tok/s with a partial pass every 2 s)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
javis-bot
2026-11-14 10:58:11 +09:00
parent 5316f4e9b0
commit 3d58cf8ab1
8 changed files with 138 additions and 20 deletions

View File

@@ -49,6 +49,9 @@ class _LlmPaused(Exception):
class VoiceWorker:
def _partials_held(self) -> bool:
return self.llm_game and self.answering > 0
def __init__(self, token: str, transcriber: Transcriber | None, vad_factory: Callable[[], VadStream],
tts_engine: TtsEngine | None = None, hello_timeout_s: float = 5.0, tick_s: float = 0.05,
history=None, brain: Brain | None = None, chat_timeout_s: float = 15.0,
@@ -80,6 +83,10 @@ class VoiceWorker:
self.llm_active = 0
self.llm_idle_to = None # the session to tell once nothing runs
self.llm_switch_id = None # the switch to name in llm_idle
# In a game (the app says so with `ready`), partial passes wait while an answer is written
# (M0 gamemode_tune: the LLM answers 1.7 times faster); finals never wait.
self.llm_game = False
self.answering = 0
self.chat_timeout_s = chat_timeout_s
self.delivery_timeout_s = delivery_timeout_s # a chat answer the app never reports: not posted
# chat_action ids are unique across connections and worker runs, so a late answer from an
@@ -105,7 +112,7 @@ class VoiceWorker:
if n:
debug_log(f"closed {n} presence intervals left open by a previous run", "voice_worker")
if self.transcriber is not None:
self.scheduler = SttScheduler(self.transcriber)
self.scheduler = SttScheduler(self.transcriber, partials_held=self._partials_held)
self.scheduler.start()
if self.tts_engine is not None:
self.tts = TtsService(self.tts_engine)
@@ -541,13 +548,17 @@ class Session:
asyncio.get_running_loop().create_task(self._reply(rid, user, text, via, speak, early))
def _on_llm(self, msg: dict) -> None:
state, notice, sid = msg.get("state"), msg.get("notice"), msg.get("id")
state, notice, sid, game = msg.get("state"), msg.get("notice"), msg.get("id"), msg.get("game", False)
if state not in ("switching", "ready", "paused") or not (notice is None or isinstance(notice, str)) \
or not (sid is None or (isinstance(sid, int) and not isinstance(sid, bool))):
self.error("bad_message", "llm needs state switching|ready|paused, an optional string notice and an optional integer id")
or not (sid is None or (isinstance(sid, int) and not isinstance(sid, bool))) or not isinstance(game, bool):
self.error("bad_message", "llm needs state switching|ready|paused, an optional string notice, integer id and boolean game")
return
w = self.worker
w.llm_state = state
if state == "ready":
w.llm_game = game
if w.scheduler:
w.scheduler.wake()
debug_log(f"llm {state}", "llm")
if state == "ready":
w.llm_notice, w.llm_idle_to = None, None
@@ -735,7 +746,13 @@ class Session:
if self.chat_channels else None
try:
async with self._llm(rid if speak else None):
answer = await asyncio.to_thread(w.brain.reply, user, text, history, chat=chat)
w.answering += 1
try:
answer = await asyncio.to_thread(w.brain.reply, user, text, history, chat=chat)
finally:
w.answering -= 1
if not w.answering and w.scheduler:
w.scheduler.wake() # held partials go on
except Exception as e: # the brain must never take the session down
debug_log(f"brain reply {rid} failed: {type(e).__name__}", "llm")
answer = None