fix(app): a wake word said alone waits for what the speaker says next

A pause after the name ("자비스, ... 지금 몇 시야?") is longer than the
worker's 300 ms end silence, so the wake word ended its own utterance:
Jarvis answered the bare name and ignored the question that followed (no
wake word). Found on real Discord. A voice utterance that is only a wake
word is now joined to the same speaker's next final when that utterance
is running or starts within call.followUpMs (default 1500); otherwise the
wake word alone is the request after that time. Stops drop it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
javis-bot
2026-11-12 09:44:06 +09:00
parent 9ff307d7fe
commit bd2eb49e3d
5 changed files with 149 additions and 11 deletions

View File

@@ -130,13 +130,18 @@ pub struct Conversation<V: Voice> {
chat: ChatCalls,
/// Chat answer id -> the message it answers (channel, message) and when it was asked.
chat_answers: HashMap<String, (u64, u64, Instant)>,
/// Each speaker's latest utterance that started.
started: HashMap<u64, u32>,
/// 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>)>,
}
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() }
chat_answers: HashMap::new(), started: HashMap::new(), wakes: HashMap::new() }
}
/// Answer the registered chat channels too.
@@ -178,7 +183,13 @@ impl<V: Voice> Conversation<V> {
pub fn on_event(&mut self, ev: &CoreEvent, now: Instant) {
match ev {
CoreEvent::SpeechStart(h) => self.policy.utterance_open(h.speaker_id),
CoreEvent::SpeechStart(h) => {
self.policy.utterance_open(h.speaker_id);
self.started.insert(h.speaker_id, h.utt);
if let Some((_, deadline)) = self.wakes.get_mut(&h.speaker_id) {
*deadline = None; // they go on: wait for this utterance's final
}
}
CoreEvent::Chat(m) => self.on_chat(m, now),
CoreEvent::ReplyText { id, text, failed } => {
if let Some((channel, message, asked)) = self.chat_answers.remove(id) {
@@ -195,11 +206,27 @@ impl<V: Voice> Conversation<V> {
CoreEvent::SpeakingStopped(user) => self.policy.speaking_stopped(*user, now),
CoreEvent::Final(f) => {
let speaker = f.heard.speaker_id;
// 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(),
};
self.next_id += 1;
let req = Request { id: self.next_id, speaker, text: f.text.clone(), direct: false,
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, &f.text);
let call = self.calls.judge(speaker, &text);
debug_log!("conversation", "final speaker={speaker} req={} call={call:?}", req.id);
if matches!(call, Call::Request { direct: true }) && self.calls.wake_word_only(&text) {
let going_on = self.started.get(&speaker).is_some_and(|&u| u > f.heard.utt);
debug_log!("conversation", "wake word alone req={} speaker={speaker} going_on={going_on}", req.id);
if !going_on {
let a = self.policy.utterance_closed(speaker, now);
self.apply(a, now);
}
let deadline = (!going_on).then(|| now + self.calls.follow_up);
self.wakes.insert(speaker, (Request { direct: true, ..req }, deadline));
return;
}
match call {
Call::Ignore => {
let a = self.policy.utterance_closed(speaker, now);
@@ -217,7 +244,7 @@ impl<V: Voice> Conversation<V> {
}
Call::Ask => {
let id = format!("q{}", req.id);
if self.voice.ask_addressed(&id, speaker, &f.text) {
if self.voice.ask_addressed(&id, speaker, &text) {
self.asks.insert(id, (req, now));
} else {
let a = self.policy.utterance_closed(speaker, now);
@@ -263,6 +290,7 @@ impl<V: Voice> Conversation<V> {
/// The policy's stop, and every pending "was it said to Jarvis?" question is void.
fn stop_everything(&mut self, now: Instant) {
self.wakes.clear();
let a = self.policy.stop();
self.apply(a, now);
for (_, (req, _)) in self.asks.drain().collect::<Vec<_>>() {
@@ -282,16 +310,24 @@ impl<V: Voice> Conversation<V> {
self.apply(a, now);
}
}
// Nothing followed a wake word said alone: it is the request.
let due: Vec<u64> = self.wakes.iter()
.filter(|(_, (_, d))| d.is_some_and(|d| now >= d)).map(|(s, _)| *s).collect();
for speaker in due {
if let Some((req, _)) = self.wakes.remove(&speaker) {
debug_log!("conversation", "wake word alone req={} answered alone", req.id);
let a = self.policy.request(req, now);
self.apply(a, now);
}
}
let a = self.policy.tick(now);
self.apply(a, now);
}
pub fn next_deadline(&self) -> Option<Instant> {
let asks = self.asks.values().map(|(_, at)| *at + ADDRESSED_TIMEOUT).min();
match (self.policy.next_deadline(), asks) {
(Some(a), Some(b)) => Some(a.min(b)),
(a, b) => a.or(b),
}
let asks = self.asks.values().map(|(_, at)| *at + ADDRESSED_TIMEOUT);
let wakes = self.wakes.values().filter_map(|(_, d)| *d);
self.policy.next_deadline().into_iter().chain(asks).chain(wakes).min()
}
fn apply(&mut self, actions: Vec<Action>, now: Instant) {
@@ -440,6 +476,71 @@ mod tests {
t + Duration::from_millis(n)
}
fn fin_utt(speaker: u64, utt: u32, text: &str) -> CoreEvent {
CoreEvent::Final(HeardFinal { heard: Heard { speaker_id: speaker, speaker_name: "x".into(), utt },
text: text.into(), end_reason: "silence".into(), audio_ms: 1000, wait_ms: 100, stt_ms: 100 })
}
fn start(speaker: u64, utt: u32) -> CoreEvent {
CoreEvent::SpeechStart(Heard { speaker_id: speaker, speaker_name: "x".into(), utt })
}
#[test]
fn a_wake_word_alone_waits_for_what_the_speaker_says_next() {
let (mut c, f, t) = conv(PolicyKind::First, CallMode::WakeWord);
// "자비스," then a pause that ended the utterance; the question follows.
c.on_event(&start(A, 1), ms(t, 0));
c.on_event(&fin_utt(A, 1, "자비스,"), ms(t, 900));
assert!(f.take().is_empty(), "a wake word alone is not the question yet");
c.on_event(&start(A, 2), ms(t, 1200));
c.tick(ms(t, 5000)); // still talking: no time limit while the next utterance runs
assert!(f.take().is_empty());
c.on_event(&fin_utt(A, 2, "지금 몇 시야?"), ms(t, 5200));
assert_eq!(f.take(), vec![Did::Answer(A, "자비스, 지금 몇 시야?".into())]);
assert_eq!(c.missed().count(), 0);
}
#[test]
fn a_wake_word_alone_waits_for_a_next_utterance_that_already_started() {
let (mut c, f, t) = conv(PolicyKind::First, CallMode::WakeWord);
c.on_event(&start(A, 1), ms(t, 0));
c.on_event(&start(A, 2), ms(t, 600)); // the final of utterance 1 comes after 2 started
c.on_event(&fin_utt(A, 1, "자비스"), ms(t, 900));
c.tick(ms(t, 900) + CallSettings::default().follow_up + Duration::from_millis(1));
assert!(f.take().is_empty());
c.on_event(&fin_utt(A, 2, "멈춰"), ms(t, 3000));
assert_eq!(f.take(), vec![Did::StopAll], "joined, it is a stop");
}
#[test]
fn a_wake_word_alone_is_answered_when_nothing_follows() {
let (mut c, f, t) = conv(PolicyKind::First, CallMode::WakeWord);
c.on_event(&start(A, 1), ms(t, 0));
c.on_event(&fin_utt(A, 1, "자비스"), ms(t, 900));
let wait = CallSettings::default().follow_up;
assert_eq!(c.next_deadline(), Some(ms(t, 900) + wait));
c.tick(ms(t, 900) + wait - Duration::from_millis(1));
assert!(f.take().is_empty());
c.tick(ms(t, 900) + wait);
assert_eq!(f.take(), vec![Did::Answer(A, "자비스".into())]);
// Another speaker's words do not join it.
c.on_event(&end("s1", false), ms(t, 4000));
c.on_event(&start(A, 2), ms(t, 4100));
c.on_event(&fin_utt(A, 2, "자비스"), ms(t, 4500));
c.on_event(&fin_utt(B, 1, "오늘 뭐 먹지"), ms(t, 4600));
c.tick(ms(t, 4500) + wait);
assert_eq!(f.take(), vec![Did::Answer(A, "자비스".into())]);
}
#[test]
fn a_stop_drops_a_waiting_wake_word() {
let (mut c, f, t) = conv(PolicyKind::First, CallMode::WakeWord);
c.on_event(&fin_utt(A, 1, "자비스"), ms(t, 900));
c.on_event(&CoreEvent::Stopped, ms(t, 1000));
c.tick(ms(t, 900) + CallSettings::default().follow_up);
assert!(f.take().is_empty());
assert_eq!(c.next_deadline(), None);
}
#[test]
fn a_call_is_answered_and_the_next_one_waits_for_its_speech_to_end() {
let (mut c, f, t) = conv(PolicyKind::InOrder, CallMode::WakeWord);