feat(app): an earcon tells the speaker Jarvis took the call
A short two-tone chime, made in code, plays right before each voice answer starts and at once for a wake word said alone while Jarvis is free; the question that joins it gets no second one. call.earcon turns it off. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -5,7 +5,7 @@
|
||||
//! `Conversation` holds no Discord or worker types: it talks to the voice side through `Voice`, so the
|
||||
//! glue is tested with a fake. `run` drives it from the core's events with a timer for the policy.
|
||||
|
||||
use std::collections::{HashMap, VecDeque};
|
||||
use std::collections::{HashMap, HashSet, VecDeque};
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
use tokio::sync::mpsc;
|
||||
@@ -28,6 +28,8 @@ pub trait Voice {
|
||||
fn cancel(&self, id: &str);
|
||||
/// Stop all speech at once.
|
||||
fn stop_all(&self);
|
||||
/// Play the earcon now (queued like speech; dropped by a stop).
|
||||
fn earcon(&self);
|
||||
fn ask_addressed(&self, id: &str, user: u64, text: &str) -> bool;
|
||||
fn ask_rank(&self, id: &str, candidates: &[(String, u64, String)]) -> bool;
|
||||
/// Have the brain answer a chat message (not spoken); its text comes as `CoreEvent::ReplyText`.
|
||||
@@ -49,6 +51,9 @@ impl Voice for DiscordCore {
|
||||
fn stop_all(&self) {
|
||||
DiscordCore::stop(self);
|
||||
}
|
||||
fn earcon(&self) {
|
||||
DiscordCore::earcon(self);
|
||||
}
|
||||
fn ask_addressed(&self, id: &str, user: u64, text: &str) -> bool {
|
||||
DiscordCore::ask_addressed(self, id, user, text)
|
||||
}
|
||||
@@ -135,13 +140,16 @@ pub struct Conversation<V: Voice> {
|
||||
/// A wake word said alone, per speaker, waiting for what they say next; the deadline for the next
|
||||
/// utterance to start (None while it runs).
|
||||
wakes: HashMap<u64, (Request, Option<Instant>)>,
|
||||
/// Requests whose earcon already played (a wake word said alone was answered with it).
|
||||
chimed: HashSet<u64>,
|
||||
}
|
||||
|
||||
impl<V: Voice> Conversation<V> {
|
||||
pub fn new(voice: V, calls: CallSettings, policy: PolicySettings) -> Self {
|
||||
Self { voice, calls, policy: Policy::new(policy), next_id: 0, answers: HashMap::new(),
|
||||
by_speech: HashMap::new(), asks: HashMap::new(), missed: VecDeque::new(), chat: ChatCalls::default(),
|
||||
chat_answers: HashMap::new(), started: HashMap::new(), wakes: HashMap::new() }
|
||||
chat_answers: HashMap::new(), started: HashMap::new(), wakes: HashMap::new(),
|
||||
chimed: HashSet::new() }
|
||||
}
|
||||
|
||||
/// Answer the registered chat channels too.
|
||||
@@ -210,11 +218,14 @@ impl<V: Voice> Conversation<V> {
|
||||
self.started.remove(&speaker);
|
||||
}
|
||||
// What follows a wake word said alone is joined to it.
|
||||
let text = match self.wakes.remove(&speaker) {
|
||||
Some((wake, _)) => format!("{} {}", wake.text, f.text),
|
||||
None => f.text.clone(),
|
||||
let (text, chimed) = match self.wakes.remove(&speaker) {
|
||||
Some((wake, _)) => (format!("{} {}", wake.text, f.text), self.chimed.remove(&wake.id)),
|
||||
None => (f.text.clone(), false),
|
||||
};
|
||||
self.next_id += 1;
|
||||
if chimed {
|
||||
self.chimed.insert(self.next_id);
|
||||
}
|
||||
let req = Request { id: self.next_id, speaker, text: text.clone(), direct: false,
|
||||
ended_at: now.checked_sub(Duration::from_millis(f.wait_ms)).unwrap_or(now), arrived_at: now };
|
||||
let call = self.calls.judge(speaker, &text);
|
||||
@@ -226,6 +237,11 @@ impl<V: Voice> Conversation<V> {
|
||||
let a = self.policy.utterance_closed(speaker, now);
|
||||
self.apply(a, now);
|
||||
}
|
||||
// "I heard you" at once, as voice assistants do after their name; only when
|
||||
// Jarvis is free, since a busy Jarvis may not take the question.
|
||||
if self.calls.earcon && self.answers.is_empty() && self.chimed.insert(req.id) {
|
||||
self.voice.earcon();
|
||||
}
|
||||
let deadline = (!going_on).then(|| now + self.calls.follow_up);
|
||||
self.wakes.insert(speaker, (Request { direct: true, ..req }, deadline));
|
||||
return;
|
||||
@@ -295,7 +311,9 @@ impl<V: Voice> Conversation<V> {
|
||||
}
|
||||
CoreEvent::WorkerLost => {
|
||||
self.started.clear();
|
||||
self.wakes.clear();
|
||||
for (_, (req, _)) in self.wakes.drain() {
|
||||
self.chimed.remove(&req.id);
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
@@ -306,6 +324,7 @@ impl<V: Voice> Conversation<V> {
|
||||
self.wakes.clear();
|
||||
let a = self.policy.stop();
|
||||
self.apply(a, now);
|
||||
self.chimed.clear(); // nothing is waiting any more
|
||||
for (_, (req, _)) in self.asks.drain().collect::<Vec<_>>() {
|
||||
let a = self.policy.utterance_closed(req.speaker, now);
|
||||
self.apply(a, now);
|
||||
@@ -346,7 +365,13 @@ impl<V: Voice> Conversation<V> {
|
||||
fn apply(&mut self, actions: Vec<Action>, now: Instant) {
|
||||
for action in actions {
|
||||
match action {
|
||||
Action::Answer(r) => match self.voice.answer(r.speaker, &r.text) {
|
||||
Action::Answer(r) => match {
|
||||
// The earcon plays first, then the answer as soon as it is written.
|
||||
if self.calls.earcon && !self.chimed.remove(&r.id) {
|
||||
self.voice.earcon();
|
||||
}
|
||||
self.voice.answer(r.speaker, &r.text)
|
||||
} {
|
||||
Some(sid) => {
|
||||
debug_log!("conversation", "answer req={} speaker={} speech={sid}", r.id, r.speaker);
|
||||
self.by_speech.insert(sid.clone(), r.id);
|
||||
@@ -364,6 +389,7 @@ impl<V: Voice> Conversation<V> {
|
||||
}
|
||||
}
|
||||
Action::Missed(r, reason) => {
|
||||
self.chimed.remove(&r.id);
|
||||
debug_log!("conversation", "missed req={} speaker={} reason={reason:?}", r.id, r.speaker);
|
||||
if self.missed.len() == MISSED_KEPT {
|
||||
self.missed.pop_front();
|
||||
@@ -420,6 +446,7 @@ mod tests {
|
||||
Answer(u64, String),
|
||||
Cancel(String),
|
||||
StopAll,
|
||||
Earcon,
|
||||
Addressed(String, u64),
|
||||
Rank(String, Vec<String>),
|
||||
ChatAnswer(String, u64, String),
|
||||
@@ -444,6 +471,9 @@ mod tests {
|
||||
fn stop_all(&self) {
|
||||
self.did.borrow_mut().push(Did::StopAll);
|
||||
}
|
||||
fn earcon(&self) {
|
||||
self.did.borrow_mut().push(Did::Earcon);
|
||||
}
|
||||
fn ask_addressed(&self, id: &str, user: u64, _text: &str) -> bool {
|
||||
self.did.borrow_mut().push(Did::Addressed(id.into(), user));
|
||||
true
|
||||
@@ -474,10 +504,62 @@ mod tests {
|
||||
|
||||
fn conv(kind: PolicyKind, mode: CallMode) -> (Conversation<Fake>, Fake, Instant) {
|
||||
let f = Fake::default();
|
||||
let calls = CallSettings { mode, ..CallSettings::default() };
|
||||
let calls = CallSettings { mode, earcon: false, ..CallSettings::default() };
|
||||
let policy = PolicySettings { kind, collect: Duration::ZERO, ..PolicySettings::default() };
|
||||
(Conversation::new(f.clone(), calls, policy), f, Instant::now())
|
||||
}
|
||||
fn conv_earcon(kind: PolicyKind) -> (Conversation<Fake>, Fake, Instant) {
|
||||
let f = Fake::default();
|
||||
let policy = PolicySettings { kind, collect: Duration::ZERO, ..PolicySettings::default() };
|
||||
(Conversation::new(f.clone(), CallSettings::default(), policy), f, Instant::now())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_earcon_plays_right_before_each_answer_is_asked_for() {
|
||||
let (mut c, f, t) = conv_earcon(PolicyKind::InOrder);
|
||||
c.on_event(&fin(A, "자비스 날씨 어때", 100), ms(t, 0));
|
||||
assert_eq!(f.take(), vec![Did::Earcon, Did::Answer(A, "자비스 날씨 어때".into())]);
|
||||
c.on_event(&fin(B, "자비스 불 꺼 줘", 100), ms(t, 200));
|
||||
assert!(f.take().is_empty(), "a waiting request gets its earcon when its answer starts");
|
||||
c.on_event(&end("s1", false), ms(t, 3000));
|
||||
assert_eq!(f.take(), vec![Did::Earcon, Did::Answer(B, "자비스 불 꺼 줘".into())]);
|
||||
c.on_event(&fin(B, "오늘 뭐 먹지", 100), ms(t, 3100));
|
||||
assert!(f.take().is_empty(), "no call, no earcon");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_wake_word_alone_gets_the_earcon_at_once_and_its_question_does_not_get_another() {
|
||||
let (mut c, f, t) = conv_earcon(PolicyKind::First);
|
||||
c.on_event(&start(A, 1), ms(t, 0));
|
||||
c.on_event(&fin_utt(A, 1, "자비스"), ms(t, 900));
|
||||
assert_eq!(f.take(), vec![Did::Earcon]);
|
||||
c.on_event(&start(A, 2), ms(t, 1200));
|
||||
c.on_event(&fin_utt(A, 2, "지금 몇 시야?"), ms(t, 3000));
|
||||
assert_eq!(f.take(), vec![Did::Answer(A, "자비스 지금 몇 시야?".into())]);
|
||||
// Nothing followed the next one: it is answered alone, still with one earcon.
|
||||
c.on_event(&end("s1", false), ms(t, 5000));
|
||||
c.on_event(&fin_utt(A, 3, "자비스"), ms(t, 6000));
|
||||
c.tick(ms(t, 6000) + CallSettings::default().follow_up);
|
||||
assert_eq!(f.take(), vec![Did::Earcon, Did::Answer(A, "자비스".into())]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_busy_jarvis_gives_no_earcon_for_a_call_it_does_not_take() {
|
||||
let (mut c, f, t) = conv_earcon(PolicyKind::First);
|
||||
c.on_event(&fin(A, "자비스 날씨", 100), ms(t, 0));
|
||||
assert_eq!(f.take(), vec![Did::Earcon, Did::Answer(A, "자비스 날씨".into())]);
|
||||
c.on_event(&fin(B, "자비스 안녕", 100), ms(t, 100));
|
||||
c.on_event(&fin_utt(B, 2, "자비스"), ms(t, 200));
|
||||
assert!(f.take().is_empty(), "missed while busy, and a wake word alone waits silently");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_earcon_can_be_turned_off() {
|
||||
let (mut c, f, t) = conv(PolicyKind::First, CallMode::WakeWord);
|
||||
c.on_event(&fin_utt(A, 1, "자비스"), ms(t, 0));
|
||||
c.on_event(&fin_utt(A, 2, "안녕"), ms(t, 500));
|
||||
assert_eq!(f.take(), vec![Did::Answer(A, "자비스 안녕".into())]);
|
||||
}
|
||||
fn fin(speaker: u64, text: &str, wait_ms: u64) -> CoreEvent {
|
||||
CoreEvent::Final(HeardFinal { heard: Heard { speaker_id: speaker, speaker_name: "x".into(), utt: 1 },
|
||||
text: text.into(), end_reason: "silence".into(), audio_ms: 1000, wait_ms, stt_ms: 100 })
|
||||
@@ -710,7 +792,8 @@ mod tests {
|
||||
(CH_ALL, ChatChannelCall { everything: true, send: true }),
|
||||
(CH_MUTE, ChatChannelCall { everything: true, send: false }),
|
||||
].into() };
|
||||
let c = Conversation::new(f.clone(), CallSettings::default(), PolicySettings::default()).with_chat(chat);
|
||||
let voice = CallSettings { earcon: false, ..CallSettings::default() };
|
||||
let c = Conversation::new(f.clone(), voice, PolicySettings::default()).with_chat(chat);
|
||||
(c, f, Instant::now())
|
||||
}
|
||||
fn chat(channel: u64, message: u64, author: u64, bot: bool, text: &str) -> CoreEvent {
|
||||
|
||||
Reference in New Issue
Block a user