feat: answer calls in the voice channel through the response policy
- conversation.rs: the loop between the core's events and the rest. Finals go through call detection and the response policy; answers are asked of the brain and spoken from the app's queue, an interruption cancels only that answer, a voice stop silences everything, and missed requests are kept without text (last 100). Tested with a fake voice side. - Core: answer(user, text) adopts a playback request the worker speaks under the same id, cancel(id) stops one request, ask_addressed and ask_rank pass the conversation's questions; new SpeakingStopped, Addressed and Ranked events. - Worker client: reply, addressed and rank messages and their results; a reply with no answer, refused or not sent ends its speech as failed. - Voice worker: addressed and rank messages on their own threads, beside a running reply; a spoken reply cancelled while the brain is answering is not spoken. judges.py holds the two short LLM judgements with the heard text fenced as data; eval evals/test_voice_judges.py. - Specs (conversation, discord core, voice worker), docs/llm_contexts.md and the roadmap updated. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -162,6 +162,9 @@ class Session:
|
||||
self.names: dict[str, str] = {}
|
||||
self.speakers: dict[str, _Speaker] = {}
|
||||
self.tts_requests: dict[str, TtsRequest] = {}
|
||||
# Spoken replies whose speech was cancelled while the brain was still answering.
|
||||
self.cancelled_replies: set[str] = set()
|
||||
self.thinking_replies: set[str] = set()
|
||||
self.out: asyncio.Queue = asyncio.Queue()
|
||||
self.closed = False
|
||||
|
||||
@@ -295,6 +298,7 @@ class Session:
|
||||
"member_join": self._on_member_join, "member_leave": self._on_member_leave,
|
||||
"chat_message": self._on_chat_message, "chat_edit": self._on_chat_edit,
|
||||
"chat_delete": self._on_chat_delete, "reply": self._on_reply,
|
||||
"addressed": self._on_addressed, "rank": self._on_rank,
|
||||
"chat_channels": self._on_chat_channels, "chat_action_result": self._on_chat_action_result,
|
||||
"memory_config": self._on_memory_config}.get(kind)
|
||||
if handler is None:
|
||||
@@ -349,6 +353,8 @@ class Session:
|
||||
tts.feed(req, text, more)
|
||||
|
||||
def _on_tts_cancel(self, msg: dict) -> None:
|
||||
if msg.get("id") in self.thinking_replies:
|
||||
self.cancelled_replies.add(msg["id"])
|
||||
req = self.tts_requests.get(msg.get("id"))
|
||||
if req is not None and self.worker.tts:
|
||||
self.worker.tts.cancel(req)
|
||||
@@ -482,9 +488,61 @@ class Session:
|
||||
if self.worker.brain is None:
|
||||
self.error("brain_unavailable", "the worker was started without --brain", id=rid)
|
||||
return
|
||||
if speak:
|
||||
self.thinking_replies.add(rid)
|
||||
asyncio.get_running_loop().create_task(self._reply(rid, user, text, via, speak))
|
||||
|
||||
def _on_addressed(self, msg: dict) -> None:
|
||||
got = self._strings(msg, "id", "user", "text")
|
||||
if got is None:
|
||||
return
|
||||
qid, user, text = got
|
||||
if self.worker.brain is None:
|
||||
self.error("brain_unavailable", "the worker was started without --brain", id=qid)
|
||||
return
|
||||
|
||||
async def judge():
|
||||
try:
|
||||
yes = await asyncio.to_thread(self.worker.brain.addressed, user, text, list(self.settings.hotwords))
|
||||
except Exception as e: # the brain must never take the session down
|
||||
debug_log(f"brain addressed {qid} failed: {type(e).__name__}", "voice_worker")
|
||||
yes = None
|
||||
debug_log(f"brain addressed {qid} -> {yes}", "voice_worker")
|
||||
self.send({"type": "addressed_result", "id": qid, "addressed": bool(yes), "failed": yes is None})
|
||||
asyncio.get_running_loop().create_task(judge())
|
||||
|
||||
def _on_rank(self, msg: dict) -> None:
|
||||
qid, cands = msg.get("id"), msg.get("candidates")
|
||||
ok = isinstance(qid, str) and isinstance(cands, list) and all(
|
||||
isinstance(c, dict) and all(isinstance(c.get(k), str) for k in ("id", "user", "text")) for c in cands)
|
||||
if not ok:
|
||||
self.error("bad_message", "rank needs a string id and candidates of string id, user and text")
|
||||
return
|
||||
if self.worker.brain is None:
|
||||
self.error("brain_unavailable", "the worker was started without --brain", id=qid)
|
||||
return
|
||||
cands = [{"id": c["id"], "user": c["user"], "text": c["text"]} for c in cands]
|
||||
|
||||
async def rank():
|
||||
try:
|
||||
order = await asyncio.to_thread(self.worker.brain.rank, cands)
|
||||
except Exception as e:
|
||||
debug_log(f"brain rank {qid} failed: {type(e).__name__}", "voice_worker")
|
||||
order = None
|
||||
known = {c["id"] for c in cands}
|
||||
order = [i for i in (order or []) if i in known] if order is not None else None
|
||||
debug_log(f"brain rank {qid} candidates={len(cands)} ok={order is not None}", "voice_worker")
|
||||
self.send({"type": "rank_result", "id": qid, "order": order or [], "failed": order is None})
|
||||
asyncio.get_running_loop().create_task(rank())
|
||||
|
||||
async def _reply(self, rid: str, user: str, text: str, via: str, speak: bool) -> None:
|
||||
try:
|
||||
await self._reply_inner(rid, user, text, via, speak)
|
||||
finally:
|
||||
self.thinking_replies.discard(rid)
|
||||
self.cancelled_replies.discard(rid)
|
||||
|
||||
async def _reply_inner(self, rid: str, user: str, text: str, via: str, speak: bool) -> None:
|
||||
w = self.worker
|
||||
channel = self.voice_channel
|
||||
async with w.brain_lock:
|
||||
@@ -514,7 +572,10 @@ class Session:
|
||||
return
|
||||
if via == "voice" and channel and w.history is not None:
|
||||
w.history.add_reply(channel, user, answer, time.time())
|
||||
if speak:
|
||||
if speak and rid in self.cancelled_replies:
|
||||
debug_log(f"brain reply {rid} not spoken: cancelled while answering", "voice_worker")
|
||||
self.send({"type": "tts_done", "id": rid, "chunks": 0, "cancelled": True, "failed": False})
|
||||
elif speak:
|
||||
self._on_tts({"type": "tts", "id": rid, "text": answer, "more": False})
|
||||
|
||||
def _tts_chunk_cb(self, rid: str):
|
||||
|
||||
Reference in New Issue
Block a user