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

@@ -129,6 +129,12 @@ pub enum WorkerEvent {
TtsAudio { id: String, seq: u32, pcm: Vec<u8> },
TtsDone(TtsDone),
ChatAction(ChatAction),
/// The brain's answer to `reply` (empty and `failed` when it gave none).
ReplyText { id: String, text: String, failed: bool },
/// Was the utterance said to Jarvis? (a failed judgement is a no)
Addressed { id: String, addressed: bool },
/// Candidate ids, most important first (empty when the ranking failed).
Ranked { id: String, order: Vec<String> },
Error(WorkerError),
}
@@ -301,6 +307,43 @@ impl WorkerClient {
"message_id": message.to_string()}));
}
// --- brain (voice_worker.spec.md "History and brain", conversation.spec.md) -------------------
/// Ask the brain to answer `user`. With `speak` the answer is spoken under `id` (its audio and
/// `tts_done` come like a TTS request's); it ends as failed when it cannot be sent, the brain gives
/// no answer, or the worker refuses it.
pub fn reply(&self, id: &str, user: u64, text: &str, speak: bool) -> bool {
if speak {
self.inner.state.lock().unwrap().open_tts.insert(id.to_owned());
}
let sent = self.send_json(json!({"type": "reply", "id": id, "user": user.to_string(), "text": text,
"via": "voice", "speak": speak}));
if !sent && speak {
self.end_failed(id);
}
sent
}
fn end_failed(&self, id: &str) {
if self.inner.state.lock().unwrap().open_tts.remove(id) {
let _ = self.inner.events.send(WorkerEvent::TtsDone(TtsDone { id: id.into(), chunks: 0, cancelled: false,
failed: true }));
}
}
/// Ask whether `text` was said to Jarvis (call mode `addressed`).
pub fn addressed(&self, id: &str, user: u64, text: &str) -> bool {
self.send_json(json!({"type": "addressed", "id": id, "user": user.to_string(), "text": text}))
}
/// Ask the brain to rank tied candidates `(id, user, text)` by importance.
pub fn rank(&self, id: &str, candidates: &[(String, u64, String)]) -> bool {
let list: Vec<Value> = candidates.iter()
.map(|(cid, user, text)| json!({"id": cid, "user": user.to_string(), "text": text}))
.collect();
self.send_json(json!({"type": "rank", "id": id, "candidates": list}))
}
/// Change STT settings now and for every later connection.
pub fn configure(&self, changes: Map<String, Value>) {
self.inner.state.lock().unwrap().stt.extend(changes.clone());
@@ -458,13 +501,36 @@ fn on_text(inner: &Inner, t: &str) {
"partial" => serde_json::from_value(v).ok().map(WorkerEvent::Partial),
"final" => serde_json::from_value(v).ok().map(WorkerEvent::Final),
"chat_action" => serde_json::from_value(v).ok().map(WorkerEvent::ChatAction),
"reply_text" => {
let id = v["id"].as_str().unwrap_or_default().to_owned();
let failed = v["failed"].as_bool().unwrap_or(true);
let text = v["text"].as_str().unwrap_or_default().to_owned();
// No answer means nothing will be spoken: end the speech the app is waiting for.
if failed && inner.state.lock().unwrap().open_tts.remove(&id) {
let _ = inner.events.send(WorkerEvent::ReplyText { id: id.clone(), text, failed });
let _ = inner.events.send(WorkerEvent::TtsDone(TtsDone { id, chunks: 0, cancelled: false, failed: true }));
return;
}
Some(WorkerEvent::ReplyText { id, text, failed })
}
"addressed_result" => Some(WorkerEvent::Addressed {
id: v["id"].as_str().unwrap_or_default().to_owned(),
addressed: v["addressed"].as_bool().unwrap_or(false) && !v["failed"].as_bool().unwrap_or(false),
}),
"rank_result" => Some(WorkerEvent::Ranked {
id: v["id"].as_str().unwrap_or_default().to_owned(),
order: if v["failed"].as_bool().unwrap_or(false) { vec![] } else {
v["order"].as_array().map(|a| a.iter().filter_map(|x| x.as_str().map(str::to_owned)).collect())
.unwrap_or_default()
},
}),
"tts_done" => serde_json::from_value::<TtsDone>(v).ok().and_then(|d| {
inner.state.lock().unwrap().open_tts.remove(&d.id).then_some(WorkerEvent::TtsDone(d))
}),
"error" => serde_json::from_value::<WorkerError>(v).ok().map(|e| {
debug_log!("worker", "voice worker error {}", e.code);
// A request the worker refused outright never gets a tts_done.
if e.code == "tts_unavailable" {
if e.code == "tts_unavailable" || e.code == "brain_unavailable" {
if let Some(id) = &e.id {
if inner.state.lock().unwrap().open_tts.remove(id) {
let _ = inner.events.send(WorkerEvent::Error(e.clone()));