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:
@@ -47,8 +47,11 @@ class _Job:
|
||||
|
||||
|
||||
class SttScheduler:
|
||||
def __init__(self, transcriber: Transcriber, max_batch: int = 16, max_partial_batch: int = 2):
|
||||
def __init__(self, transcriber: Transcriber, max_batch: int = 16, max_partial_batch: int = 2,
|
||||
partials_held: Callable[[], bool] = lambda: False):
|
||||
self.transcriber = transcriber
|
||||
# While this says so (an answer is written in a game), partial passes wait; finals never do.
|
||||
self.partials_held = partials_held
|
||||
self.max_batch = max_batch
|
||||
# Whisper's encoder always runs on a 30 s window, so its cost grows with every clip in a batch
|
||||
# and batching saves little. A small partial batch bounds how long a final can wait behind one.
|
||||
@@ -80,6 +83,10 @@ class SttScheduler:
|
||||
self._partials[key] = _Job(key, audio, opts, callback)
|
||||
self._wake.set()
|
||||
|
||||
def wake(self) -> None:
|
||||
"""Look again (partials are no longer held)."""
|
||||
self._wake.set()
|
||||
|
||||
def drop_partials(self, predicate: Callable[[Hashable], bool]) -> None:
|
||||
for key in [k for k in self._partials if predicate(k)]:
|
||||
del self._partials[key]
|
||||
@@ -87,7 +94,7 @@ class SttScheduler:
|
||||
def _take(self) -> tuple[str, list[_Job]]:
|
||||
if self._finals:
|
||||
kind, pool = "final", self._finals
|
||||
elif self._partials:
|
||||
elif self._partials and not self.partials_held():
|
||||
kind, pool = "partial", list(self._partials.values())
|
||||
else:
|
||||
return "", []
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -125,7 +125,8 @@ wait seconds behind it; two clips keep that wait to about one clip's time. With
|
||||
captions fall behind and only the newest audio per speaker is transcribed.
|
||||
|
||||
Batches are grouped by decode settings (beam, language, hotwords). A partial whose utterance has
|
||||
already ended, or that is older than the last partial sent, is dropped.
|
||||
already ended, or that is older than the last partial sent, is dropped. In a game, partials wait
|
||||
while an answer is being written ("LLM switches"); finals never wait.
|
||||
|
||||
### Hallucination gate
|
||||
|
||||
@@ -356,7 +357,7 @@ The app restarts the LLM with another placement when a game starts or ends (app/
|
||||
```
|
||||
-> {"type": "llm", "state": "switching", "id": 7, "notice": "잠깐만요, 게임 모드로 바꾸는 중이에요"} // id, notice optional
|
||||
<- {"type": "llm_idle", "id": 7} // nothing that uses the LLM runs any more (id: the switch's, when it had one)
|
||||
-> {"type": "llm", "state": "ready"} // the LLM answers again
|
||||
-> {"type": "llm", "state": "ready", "game": true} // the LLM answers again; game optional (default false)
|
||||
-> {"type": "llm", "state": "paused"} // the LLM is stopped (a game runs)
|
||||
```
|
||||
|
||||
@@ -369,7 +370,13 @@ The app restarts the LLM with another placement when a game starts or ends (app/
|
||||
then fails without calling the brain (the LLM may still be away, or a measuring server stand in).
|
||||
- On `paused` brain work answers nothing at once (`reply_text` `failed`, judgements failed) until
|
||||
`ready`; what waited for a switch stops waiting the same way.
|
||||
- Any other `state`, a notice that is not a string or an id that is not an integer is `bad_message`.
|
||||
- `game` on `ready` says the LLM runs in the game placement: from then on, while a `reply` is being
|
||||
written, partial passes wait (finals go on at once, "Scheduling"); when no answer is being written
|
||||
they go on, the newest audio per speaker. `ready` without it (or false) ends that. Without a game
|
||||
partials never wait (M0 `gamemode_tune`: with partials held the game placement answered 1.7
|
||||
times faster, 64.9 against 37.5 tok/s with a partial pass every 2 s).
|
||||
- Any other `state`, a notice that is not a string, an id that is not an integer or a `game` that is
|
||||
not a boolean is `bad_message`.
|
||||
- The app sends its latest `llm` message again first on every connection, so a worker that missed
|
||||
one (a dropped connection, a restarted worker) catches up.
|
||||
|
||||
|
||||
Reference in New Issue
Block a user