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:
@@ -33,6 +33,14 @@ class FakeBrain:
|
||||
self.chat = chat
|
||||
return self.answer(user, text)
|
||||
|
||||
def addressed(self, user, text, names):
|
||||
self.calls.append(("addressed", user, text, names))
|
||||
return "jarvis" in text
|
||||
|
||||
def rank(self, candidates):
|
||||
self.calls.append(("rank", [c["id"] for c in candidates]))
|
||||
return [c["id"] for c in reversed(candidates)]
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def store(tmp_path):
|
||||
@@ -500,3 +508,141 @@ def test_a_late_answer_from_an_earlier_connection_is_not_taken_for_a_new_action(
|
||||
assert old != new
|
||||
assert all(r.get("message_ids") != ["stale"] for r in results), results
|
||||
assert results[-1]["ok"] is False
|
||||
|
||||
|
||||
@pytest.mark.unit
|
||||
def test_addressed_and_rank_answer_from_the_brain(store):
|
||||
brain = FakeBrain()
|
||||
|
||||
async def body(url):
|
||||
async with connect(url) as ws:
|
||||
await hello(ws)
|
||||
await send(ws, type="addressed", id="q1", user="100", text="jarvis, lights off")
|
||||
await send(ws, type="addressed", id="q2", user="100", text="did you see that")
|
||||
await send(ws, type="rank", id="k1", candidates=[{"id": "7", "user": "100", "text": "a"},
|
||||
{"id": "8", "user": "200", "text": "b"}])
|
||||
got = {}
|
||||
while len(got) < 3:
|
||||
m = await recv_until(ws, lambda m: isinstance(m, dict) and m.get("type") in
|
||||
("addressed_result", "rank_result"))
|
||||
got[m[-1]["id"]] = m[-1]
|
||||
return got
|
||||
|
||||
got = asyncio.run(with_worker(body, store=store, brain=brain))
|
||||
assert got["q1"] == {"type": "addressed_result", "id": "q1", "addressed": True, "failed": False}
|
||||
assert got["q2"]["addressed"] is False and got["q2"]["failed"] is False
|
||||
assert got["k1"] == {"type": "rank_result", "id": "k1", "order": ["8", "7"], "failed": False}
|
||||
|
||||
|
||||
@pytest.mark.unit
|
||||
def test_addressed_and_rank_do_not_wait_behind_a_running_reply(store):
|
||||
import threading
|
||||
release = threading.Event()
|
||||
|
||||
def slow(user, text):
|
||||
release.wait(10)
|
||||
return "late"
|
||||
|
||||
brain = FakeBrain(slow)
|
||||
|
||||
async def body(url):
|
||||
async with connect(url) as ws:
|
||||
await hello(ws)
|
||||
await send(ws, type="reply", id="r1", user="100", text="q", via="voice", speak=False)
|
||||
await send(ws, type="rank", id="k1", candidates=[{"id": "7", "user": "100", "text": "a"},
|
||||
{"id": "8", "user": "200", "text": "b"}])
|
||||
seen = await recv_until(ws, lambda m: isinstance(m, dict) and m.get("type") == "rank_result")
|
||||
release.set()
|
||||
await recv_until(ws, lambda m: isinstance(m, dict) and m.get("type") == "reply_text")
|
||||
return seen
|
||||
|
||||
seen = asyncio.run(with_worker(body, store=store, brain=brain))
|
||||
assert not any(isinstance(m, dict) and m.get("type") == "reply_text" for m in seen)
|
||||
|
||||
|
||||
@pytest.mark.unit
|
||||
@pytest.mark.parametrize("fails", [lambda *a: None, lambda *a: 1 / 0])
|
||||
def test_a_failed_judgement_or_ranking_is_reported(store, fails):
|
||||
brain = FakeBrain()
|
||||
brain.addressed = fails
|
||||
brain.rank = fails
|
||||
|
||||
async def body(url):
|
||||
async with connect(url) as ws:
|
||||
await hello(ws)
|
||||
await send(ws, type="addressed", id="q1", user="100", text="x")
|
||||
a = await recv_until(ws, lambda m: isinstance(m, dict) and m.get("type") == "addressed_result")
|
||||
await send(ws, type="rank", id="k1", candidates=[{"id": "7", "user": "100", "text": "a"}])
|
||||
r = await recv_until(ws, lambda m: isinstance(m, dict) and m.get("type") == "rank_result")
|
||||
return a[-1], r[-1]
|
||||
|
||||
a, r = asyncio.run(with_worker(body, store=store, brain=brain))
|
||||
assert a == {"type": "addressed_result", "id": "q1", "addressed": False, "failed": True}
|
||||
assert r == {"type": "rank_result", "id": "k1", "order": [], "failed": True}
|
||||
|
||||
|
||||
@pytest.mark.unit
|
||||
def test_a_reply_cancelled_while_the_brain_thinks_is_not_spoken(store):
|
||||
import threading
|
||||
release = threading.Event()
|
||||
|
||||
def slow(user, text):
|
||||
release.wait(10)
|
||||
return f"answer for {text}"
|
||||
|
||||
async def body(url):
|
||||
async with connect(url) as ws:
|
||||
await hello(ws)
|
||||
await send(ws, type="voice_join", channel="vc1")
|
||||
await send(ws, type="reply", id="r1", user="100", text="q", via="voice", speak=True)
|
||||
await send(ws, type="tts_cancel", id="r1")
|
||||
await send(ws, type="reply", id="r2", user="100", text="next", via="voice", speak=True)
|
||||
await asyncio.sleep(0.1)
|
||||
release.set()
|
||||
return await recv_until(ws, lambda m: isinstance(m, dict) and m.get("type") == "tts_done"
|
||||
and m["id"] == "r2")
|
||||
|
||||
seen = asyncio.run(with_worker(body, store=store, brain=FakeBrain(slow), tts=FakeTts()))
|
||||
frames = {decode_tts_frame(m)[0] for m in seen if isinstance(m, bytes)}
|
||||
done = {m["id"]: m for m in seen if isinstance(m, dict) and m.get("type") == "tts_done"}
|
||||
assert frames == {"r2"}
|
||||
assert done["r1"]["cancelled"] and not done["r2"]["cancelled"]
|
||||
replies = [m["id"] for m in seen if isinstance(m, dict) and m.get("type") == "reply_text"]
|
||||
assert replies == ["r1", "r2"], "the text still comes back; only the speech is skipped"
|
||||
|
||||
|
||||
@pytest.mark.unit
|
||||
def test_the_brains_judgements_use_its_model_and_map_back_to_ids(monkeypatch):
|
||||
import voice_worker.judges as judges
|
||||
from voice_worker.brain import JarvisBrain
|
||||
|
||||
asked = []
|
||||
|
||||
def fake_llm(base_url, model, system, user, **kw):
|
||||
asked.append((base_url, model, system, user))
|
||||
return "2, 1" if "<requests>" in user else "Yes."
|
||||
|
||||
monkeypatch.setattr(judges, "call_llm_direct", fake_llm)
|
||||
brain = JarvisBrain.__new__(JarvisBrain)
|
||||
brain.cfg = type("Cfg", (), {"ollama_base_url": "http://s/v1", "ollama_chat_model": "m"})()
|
||||
assert brain.addressed("100", "불 꺼 줘", ["자비스"]) is True
|
||||
assert brain.rank([{"id": "7", "user": "1", "text": "a"}, {"id": "8", "user": "2", "text": "b"}]) == ["8", "7"]
|
||||
assert asked[0][:2] == ("http://s/v1", "m") and "자비스" in asked[0][2]
|
||||
assert asked[0][3] == "<utterance>불 꺼 줘</utterance>", "the heard text is fenced as data"
|
||||
|
||||
|
||||
@pytest.mark.unit
|
||||
@pytest.mark.parametrize("raw,expected", [("no", False), ("NO.", False), ("maybe", None), (None, None)])
|
||||
def test_an_unclear_judgement_is_none(monkeypatch, raw, expected):
|
||||
import voice_worker.judges as judges
|
||||
monkeypatch.setattr(judges, "call_llm_direct", lambda *a, **k: raw)
|
||||
assert judges.is_addressed("u", "m", [], "x") is expected
|
||||
|
||||
|
||||
@pytest.mark.unit
|
||||
def test_a_ranking_keeps_left_out_requests_in_order_and_fails_without_numbers(monkeypatch):
|
||||
import voice_worker.judges as judges
|
||||
monkeypatch.setattr(judges, "call_llm_direct", lambda *a, **k: "3 then 9")
|
||||
assert judges.rank_requests("u", "m", ["a", "b", "c"]) == [2, 0, 1]
|
||||
monkeypatch.setattr(judges, "call_llm_direct", lambda *a, **k: "none of them")
|
||||
assert judges.rank_requests("u", "m", ["a", "b"]) is None
|
||||
|
||||
Reference in New Issue
Block a user