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:
javis-bot
2026-11-11 22:26:47 +09:00
parent e885630e91
commit f892a1dce9
20 changed files with 1174 additions and 14 deletions

View File

@@ -81,6 +81,44 @@ impl<B: TtsBackend, S: ClipSink> Playback<B, S> {
id
}
/// Queue a request whose speech the worker makes from the brain's answer (`reply` with `speak`),
/// under the returned id. Nothing is sent here.
pub fn adopt(&mut self) -> String {
self.counter += 1;
let id = format!("r{}", self.counter);
self.queue.push_back(Request { id: id.clone(), chunks: BTreeMap::new(), next: 0, done: false, failed: false,
played: 0, started: false });
crate::debug_log!("play", "answer id={id} queued={}", self.queue.len());
id
}
/// End a request that will get no audio (the brain gave no answer).
pub fn fail(&mut self, id: &str) {
if let Some(r) = self.queue.iter_mut().find(|r| r.id == id && !r.done) {
r.done = true;
r.failed = true;
self.pump();
}
}
/// Stop one request: its clip if it is playing, its unsent sentences at the worker. The rest of the
/// queue goes on.
pub fn cancel(&mut self, id: &str) {
let Some(pos) = self.queue.iter().position(|r| r.id == id) else { return };
let r = self.queue.remove(pos).expect("found");
if !r.done {
self.backend.tts_cancel(&r.id);
}
if pos == 0 && self.playing {
self.playing = false;
self.sink.stop();
}
crate::debug_log!("play", "cancel id={} played={}", r.id, r.played);
self.events.push(PlaybackEvent::End(PlaybackEnd { id: r.id, played: r.played, cancelled: true,
failed: false }));
self.pump();
}
/// More text for an open request (an LLM answer that is still streaming).
pub fn continue_text(&mut self, id: &str, text: &str, more: bool) {
if self.queue.iter().any(|r| r.id == id && !r.done) {
@@ -302,6 +340,53 @@ mod tests {
assert_eq!(sent, vec![("첫 문장.".into(), true), (" 둘째 문장.".into(), false)]);
}
#[test]
fn an_adopted_answer_plays_the_brains_audio_without_sending_text() {
let (mut p, f) = setup();
let id = p.adopt();
assert!(f.0.borrow().sent.is_empty(), "the brain speaks it under this id");
p.audio(&id, 0, b("a0"));
p.done(&done(&id, false));
p.clip_ended();
assert_eq!(f.0.borrow().played, vec!["a0"]);
assert_eq!(p.take_events(), vec![PlaybackEvent::Start(id.clone()), end(&id, 1, false, false)]);
assert_ne!(p.adopt(), id, "ids never repeat");
}
#[test]
fn an_answer_with_no_text_fails_and_the_next_request_plays() {
let (mut p, f) = setup();
let id = p.adopt();
let c = p.say("b", false);
p.audio(&c, 0, b("b0"));
assert!(f.0.borrow().played.is_empty());
p.fail(&id);
assert_eq!(f.0.borrow().played, vec!["b0"]);
assert_eq!(p.take_events()[0], end(&id, 0, false, true));
}
#[test]
fn cancel_stops_one_request_and_leaves_the_others() {
let (mut p, f) = setup();
let a = p.say("a", true);
let c = p.say("b", false);
p.audio(&a, 0, b("a0"));
p.audio(&c, 0, b("b0"));
p.take_events();
p.cancel(&a);
assert_eq!(f.0.borrow().stops, 1);
assert_eq!(f.0.borrow().cancelled, vec![a.clone()]);
assert_eq!(f.0.borrow().played, vec!["a0", "b0"], "the next request plays at once");
assert_eq!(p.take_events(), vec![end(&a, 1, true, false), PlaybackEvent::Start(c.clone())]);
// A queued request that has not started is cancelled without touching the clip.
let d = p.say("d", false);
p.cancel(&d);
assert_eq!(f.0.borrow().stops, 1);
assert_eq!(p.take_events(), vec![end(&d, 0, true, false)]);
p.cancel("nope");
assert!(p.take_events().is_empty());
}
#[test]
fn a_request_the_worker_never_accepted_fails_without_blocking_the_next() {
let (mut p, f) = setup();