fix(app): schedules fire their latest time, count speech only when heard, survive stops
- A repeating schedule the app was off for is judged by its latest time up to now, so today's reminder is not skipped after a night off. - A spoken reminder counts as delivered only when its speech ends played to the end; speech that failed or was stopped goes to the chat channel. - A schedule being delivered is marked firing; one still marked at the next start gets a failed outcome instead of vanishing without a reason. - Deliveries run in the loop's JoinSet, so stopping the services stops them. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -203,8 +203,13 @@ struct Inner {
|
||||
schedules: OnceLock<Arc<crate::schedule::Store>>,
|
||||
/// Scheduled tasks waiting for the brain's answer, by reply id.
|
||||
tasks: Mutex<HashMap<String, tokio::sync::oneshot::Sender<String>>>,
|
||||
/// Speech requests whose end someone waits for (`say_to_end`), by request id.
|
||||
speech_waits: Mutex<HashMap<String, tokio::sync::oneshot::Sender<PlaybackEnd>>>,
|
||||
}
|
||||
|
||||
/// How long `say_to_end` waits for a reminder's speech (behind what is queued before it).
|
||||
const SPEECH_TIMEOUT: Duration = Duration::from_secs(120);
|
||||
|
||||
/// How long a scheduled task may take, waiting for an LLM switch included (the worker gives up
|
||||
/// waiting for one after 180 s).
|
||||
const TASK_TIMEOUT: Duration = Duration::from_secs(300);
|
||||
@@ -270,6 +275,7 @@ impl DiscordCore {
|
||||
utt_sessions: Mutex::new(UttSessions::default()),
|
||||
schedules: OnceLock::new(),
|
||||
tasks: Mutex::new(HashMap::new()),
|
||||
speech_waits: Mutex::new(HashMap::new()),
|
||||
});
|
||||
tokio::spawn(player_task(Arc::downgrade(&inner), player_rx));
|
||||
tokio::spawn(worker_task(Arc::downgrade(&inner), worker_events));
|
||||
@@ -470,6 +476,22 @@ impl DiscordCore {
|
||||
Some(id)
|
||||
}
|
||||
|
||||
/// Speak text and wait for its end: None when not in a voice channel or when no end came within
|
||||
/// `SPEECH_TIMEOUT`.
|
||||
pub async fn say_to_end(&self, text: &str) -> Option<PlaybackEnd> {
|
||||
self.voice_channel_id()?;
|
||||
let (tx, rx) = tokio::sync::oneshot::channel();
|
||||
// Registered before the playback's events go out, so an end at once is not missed.
|
||||
let id = with_playback(&self.inner, |p| {
|
||||
let id = p.say(text, false);
|
||||
self.inner.speech_waits.lock().unwrap().insert(id.clone(), tx);
|
||||
id
|
||||
});
|
||||
let end = tokio::time::timeout(SPEECH_TIMEOUT, rx).await;
|
||||
self.inner.speech_waits.lock().unwrap().remove(&id);
|
||||
end.ok().and_then(Result::ok)
|
||||
}
|
||||
|
||||
/// Have the brain answer `user` and speak it as one request in the queue. The request id (its
|
||||
/// `SpeakStart` and `SpeakEnd` follow as for `say`), or None when not in a voice channel. It ends as
|
||||
/// failed when the brain gives no answer. `heard`: when the request's speech ended and its final
|
||||
@@ -667,8 +689,8 @@ impl crate::schedule::Deliver for DiscordCore {
|
||||
fn hears(&self, user: u64) -> bool {
|
||||
self.voice_members().iter().any(|(u, _)| *u == user)
|
||||
}
|
||||
fn say(&self, text: &str) -> bool {
|
||||
DiscordCore::say(self, text, false).is_some()
|
||||
async fn say(&self, text: &str) -> bool {
|
||||
self.say_to_end(text).await.is_some_and(|e| !e.cancelled && !e.failed && e.played > 0)
|
||||
}
|
||||
async fn post(&self, channel: u64, text: &str) -> Result<(), String> {
|
||||
self.send_chat(channel, text).await.map(|_| ()).map_err(|e| e.to_string())
|
||||
@@ -685,6 +707,11 @@ fn with_playback<T>(inner: &Inner, f: impl FnOnce(&mut Playback<WorkerClient, Pl
|
||||
(out, p.take_events())
|
||||
};
|
||||
for e in events {
|
||||
if let PlaybackEvent::End(end) = &e {
|
||||
if let Some(tx) = inner.speech_waits.lock().unwrap().remove(&end.id) {
|
||||
let _ = tx.send(end.clone());
|
||||
}
|
||||
}
|
||||
let _ = inner.events.send(match e {
|
||||
PlaybackEvent::Start(id) => CoreEvent::SpeakStart(id),
|
||||
PlaybackEvent::End(e) => CoreEvent::SpeakEnd(e),
|
||||
|
||||
Reference in New Issue
Block a user