fix(app): the dashboard keeps each run's events, each answer's own times, and only its session's captions
- Each start gets a new dashboard; stop ends the event feeding and waits for it before forgetting, so queued or earlier-run events never reach the screen. - AnswerAsked carries the request's speech end and final times, so a second waiting question does not change the first answer's timing. - Captions are shown only while Jarvis is in a voice channel. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -4,7 +4,7 @@
|
||||
//! people said for the screen only; nothing here is logged or written.
|
||||
|
||||
use std::collections::{HashMap, VecDeque};
|
||||
use std::time::{Duration, Instant};
|
||||
use std::time::Instant;
|
||||
|
||||
use serde::Serialize;
|
||||
use serde_json::{json, Value};
|
||||
@@ -27,7 +27,8 @@ pub struct Caption {
|
||||
utt: u32,
|
||||
}
|
||||
|
||||
/// One voice answer's stages in milliseconds (None: that stage could not be seen).
|
||||
/// One voice answer's stages in milliseconds (None: that stage could not be seen, e.g. no answer text
|
||||
/// before the sound).
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Default)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct AnswerTiming {
|
||||
@@ -43,8 +44,8 @@ pub struct AnswerTiming {
|
||||
|
||||
struct Pending {
|
||||
asked: Instant,
|
||||
/// The asker's latest final: when their speech ended and when the final came.
|
||||
heard: Option<(Instant, Instant)>,
|
||||
/// When the request's speech ended and when its final came.
|
||||
heard: (Instant, Instant),
|
||||
reply: Option<Instant>,
|
||||
}
|
||||
|
||||
@@ -53,7 +54,6 @@ pub struct Dashboard {
|
||||
bot_tag: Option<String>,
|
||||
voice_channel: Option<u64>,
|
||||
captions: VecDeque<Caption>,
|
||||
last_final: HashMap<u64, (Instant, Instant)>,
|
||||
pending: HashMap<String, Pending>,
|
||||
answers: VecDeque<AnswerTiming>,
|
||||
}
|
||||
@@ -72,18 +72,16 @@ impl Dashboard {
|
||||
self.captions.clear();
|
||||
self.pending.clear();
|
||||
}
|
||||
CoreEvent::Partial(h, text) => self.caption(h.speaker_id, &h.speaker_name, h.utt, text, false),
|
||||
CoreEvent::Final(f) => {
|
||||
// Captions belong to the voice session: a transcript arriving after Jarvis left is dropped.
|
||||
CoreEvent::Partial(h, text) if self.voice_channel.is_some() =>
|
||||
self.caption(h.speaker_id, &h.speaker_name, h.utt, text, false),
|
||||
CoreEvent::Final(f) if self.voice_channel.is_some() => {
|
||||
let h = &f.heard;
|
||||
self.caption(h.speaker_id, &h.speaker_name, h.utt, &f.text, true);
|
||||
if !f.text.trim().is_empty() {
|
||||
let ended = now.checked_sub(Duration::from_millis(f.wait_ms)).unwrap_or(now);
|
||||
self.last_final.insert(h.speaker_id, (ended, now));
|
||||
}
|
||||
}
|
||||
CoreEvent::AnswerAsked { id, user } => {
|
||||
let heard = self.last_final.get(user).copied();
|
||||
self.pending.insert(id.clone(), Pending { asked: now, heard, reply: None });
|
||||
// The request's own times: a second question waiting behind the first does not move them.
|
||||
CoreEvent::AnswerAsked { id, ended_at, final_at, .. } => {
|
||||
self.pending.insert(id.clone(), Pending { asked: now, heard: (*ended_at, *final_at), reply: None });
|
||||
}
|
||||
CoreEvent::ReplyText { id, .. } => {
|
||||
if let Some(p) = self.pending.get_mut(id) {
|
||||
@@ -93,10 +91,10 @@ impl Dashboard {
|
||||
CoreEvent::SpeakStart(id) => {
|
||||
if let Some(p) = self.pending.remove(id) {
|
||||
let t = AnswerTiming {
|
||||
final_ms: p.heard.map(|(e, f)| ms(e, f)),
|
||||
final_ms: Some(ms(p.heard.0, p.heard.1)),
|
||||
brain_ms: p.reply.map(|r| ms(p.asked, r)),
|
||||
voice_ms: p.reply.map(|r| ms(r, now)),
|
||||
total_ms: p.heard.map(|(e, _)| ms(e, now)),
|
||||
total_ms: Some(ms(p.heard.0, now)),
|
||||
};
|
||||
if self.answers.len() == ANSWERS_KEPT {
|
||||
self.answers.pop_front();
|
||||
@@ -180,6 +178,7 @@ mod tests {
|
||||
use crate::discord::{Heard, HeardFinal};
|
||||
use crate::playback::PlaybackEnd;
|
||||
use crate::policy::MissReason;
|
||||
use std::time::Duration;
|
||||
|
||||
const A: u64 = 11;
|
||||
fn heard(utt: u32) -> Heard {
|
||||
@@ -192,10 +191,23 @@ mod tests {
|
||||
fn at(t: Instant, ms: u64) -> Instant {
|
||||
t + Duration::from_millis(ms)
|
||||
}
|
||||
fn joined() -> CoreEvent {
|
||||
CoreEvent::Joined { guild_id: 1, channel_id: 5 }
|
||||
}
|
||||
fn left() -> CoreEvent {
|
||||
CoreEvent::Left { guild_id: 1, channel_id: 5, reason: "command".into() }
|
||||
}
|
||||
fn asked(id: &str, t: Instant, ended: u64, final_at: u64) -> CoreEvent {
|
||||
CoreEvent::AnswerAsked { id: id.into(), user: A, ended_at: at(t, ended), final_at: at(t, final_at) }
|
||||
}
|
||||
fn reply(id: &str) -> CoreEvent {
|
||||
CoreEvent::ReplyText { id: id.into(), text: "네".into(), failed: false }
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_partial_is_replaced_by_the_next_and_then_by_the_final() {
|
||||
let (mut d, t) = (Dashboard::default(), Instant::now());
|
||||
d.on_event(&joined(), t);
|
||||
d.on_event(&CoreEvent::Partial(heard(1), "자비스 오늘".into()), t);
|
||||
d.on_event(&CoreEvent::Partial(heard(1), "자비스 오늘 날씨".into()), t);
|
||||
assert_eq!(d.captions().iter().map(|c| (c.text.as_str(), c.is_final)).collect::<Vec<_>>(),
|
||||
@@ -211,31 +223,35 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn captions_are_capped_and_cleared_when_jarvis_leaves() {
|
||||
fn captions_are_capped_and_cleared_when_jarvis_leaves_and_late_ones_stay_out() {
|
||||
let (mut d, t) = (Dashboard::default(), Instant::now());
|
||||
d.on_event(&CoreEvent::Joined { guild_id: 1, channel_id: 5 }, t);
|
||||
d.on_event(&joined(), t);
|
||||
for i in 0..(CAPTIONS_KEPT as u32 + 5) {
|
||||
d.on_event(&fin(i, &format!("말 {i}"), 100), t);
|
||||
}
|
||||
let caps = d.captions();
|
||||
assert_eq!(caps.len(), CAPTIONS_KEPT);
|
||||
assert_eq!(caps.last().unwrap().text, format!("말 {}", CAPTIONS_KEPT + 4));
|
||||
d.on_event(&CoreEvent::Left { guild_id: 1, channel_id: 5, reason: "command".into() }, t);
|
||||
d.on_event(&left(), t);
|
||||
assert!(d.captions().is_empty());
|
||||
// A transcript of the session that ended arrives after the leave: it does not come back.
|
||||
d.on_event(&CoreEvent::Partial(heard(90), "늦게 온 말".into()), t);
|
||||
d.on_event(&fin(90, "늦게 온 말", 100), t);
|
||||
assert!(d.captions().is_empty());
|
||||
assert_eq!(d.view(&[], t)["voiceChannel"], Value::Null);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_answer_is_timed_from_its_askers_speech_end_to_its_first_sound() {
|
||||
fn an_answer_is_timed_from_its_own_requests_speech_end_to_its_first_sound() {
|
||||
let (mut d, t) = (Dashboard::default(), Instant::now());
|
||||
d.on_event(&fin(1, "자비스 몇 시야", 700), at(t, 1000)); // speech ended at 300
|
||||
d.on_event(&CoreEvent::AnswerAsked { id: "r1".into(), user: A }, at(t, 1010));
|
||||
d.on_event(&CoreEvent::ReplyText { id: "r1".into(), text: "열 시입니다.".into(), failed: false }, at(t, 1700));
|
||||
d.on_event(&joined(), t);
|
||||
d.on_event(&asked("r1", t, 300, 1000), at(t, 1010));
|
||||
d.on_event(&reply("r1"), at(t, 1700));
|
||||
d.on_event(&CoreEvent::SpeakStart("r1".into()), at(t, 1900));
|
||||
assert_eq!(d.answers(), vec![AnswerTiming { final_ms: Some(700), brain_ms: Some(690), voice_ms: Some(200),
|
||||
total_ms: Some(1600) }]);
|
||||
// An answer stopped before it started is not timed; the earcon (another id) is not either.
|
||||
d.on_event(&CoreEvent::AnswerAsked { id: "r3".into(), user: A }, at(t, 3000));
|
||||
d.on_event(&asked("r3", t, 2000, 2500), at(t, 3000));
|
||||
d.on_event(&CoreEvent::SpeakStart("r2".into()), at(t, 3000));
|
||||
d.on_event(&CoreEvent::SpeakEnd(PlaybackEnd { id: "r3".into(), played: 0, cancelled: true, failed: false }),
|
||||
at(t, 3100));
|
||||
@@ -243,25 +259,38 @@ mod tests {
|
||||
assert_eq!(d.answers().len(), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_second_question_waiting_does_not_change_the_first_answers_times() {
|
||||
let (mut d, t) = (Dashboard::default(), Instant::now());
|
||||
d.on_event(&joined(), t);
|
||||
// First question ends at 0 (final at 700); the same person asks again, ending at 1000, while
|
||||
// the first is still answered. The first answer starts sounding at 3700.
|
||||
d.on_event(&fin(1, "첫 질문", 700), at(t, 700));
|
||||
d.on_event(&asked("r1", t, 0, 700), at(t, 710));
|
||||
d.on_event(&fin(2, "둘째 질문", 700), at(t, 1700));
|
||||
d.on_event(&reply("r1"), at(t, 3500));
|
||||
d.on_event(&CoreEvent::SpeakStart("r1".into()), at(t, 3700));
|
||||
assert_eq!(d.answers()[0].total_ms, Some(3700));
|
||||
assert_eq!(d.answers()[0].final_ms, Some(700));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_medians_cover_the_last_answers_kept() {
|
||||
let (mut d, t) = (Dashboard::default(), Instant::now());
|
||||
for (i, brain) in [500u64, 900, 700].into_iter().enumerate() {
|
||||
let base = i as u64 * 10_000;
|
||||
d.on_event(&fin(i as u32, "자비스", 600), at(t, base + 1000));
|
||||
d.on_event(&CoreEvent::AnswerAsked { id: format!("r{i}"), user: A }, at(t, base + 1000));
|
||||
d.on_event(&CoreEvent::ReplyText { id: format!("r{i}"), text: "네".into(), failed: false },
|
||||
at(t, base + 1000 + brain));
|
||||
d.on_event(&asked(&format!("r{i}"), t, base + 400, base + 1000), at(t, base + 1000));
|
||||
d.on_event(&reply(&format!("r{i}")), at(t, base + 1000 + brain));
|
||||
d.on_event(&CoreEvent::SpeakStart(format!("r{i}")), at(t, base + 1000 + brain + 100));
|
||||
}
|
||||
let m = d.medians();
|
||||
assert_eq!((m.final_ms, m.brain_ms, m.voice_ms, m.total_ms), (Some(600), Some(700), Some(100), Some(1400)));
|
||||
for i in 0..ANSWERS_KEPT as u64 {
|
||||
d.on_event(&CoreEvent::AnswerAsked { id: format!("x{i}"), user: 99 }, at(t, 100_000 + i));
|
||||
d.on_event(&asked(&format!("x{i}"), t, 100_000, 100_000), at(t, 100_000 + i));
|
||||
d.on_event(&CoreEvent::SpeakStart(format!("x{i}")), at(t, 100_000 + i));
|
||||
}
|
||||
assert_eq!(d.answers().len(), ANSWERS_KEPT);
|
||||
assert_eq!(d.medians().final_ms, None, "no final seen for the asker of the answers kept");
|
||||
assert_eq!(d.medians().brain_ms, None, "no answer text seen for the answers kept");
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
Reference in New Issue
Block a user