Attached pictures through the bridge, and a notification for a scheduled task's result
The picture command also serves a message's attached file (/attachments/<id>, one plain id, 8 MB at most) as a data: URL, for thumbnails; the page still reaches the server only through the bridge. The watch asks /schedules beside the chats and tells, by the schedule's title, of a new run the server says is worth telling (`notified`); a silent run never, a server without the routes never. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
3 files changed
+163
-15
No files matched your search
+104
-1
@@ -1,11 +1,17 @@
|
||||
//! Native notifications: a reply finished, or an agent chat's work did, while the person was not
|
||||
//! looking at it. The app's own, so the server's Web Push can stay off.
|
||||
//! looking at it; and a scheduled task has a result the server decided to tell of. The app's own,
|
||||
//! so the server's Web Push can stay off.
|
||||
//!
|
||||
//! The app asks the server's chat list (`/api/v2/chats`) every `EVERY` and notes which chats are
|
||||
//! being answered; a chat that was and no longer is has finished. The notification is shown
|
||||
//! unless that chat is open in a window in front. Its words come from the page (`set_words`), in
|
||||
//! the page's language; the chat's title is the person's own. Nothing from the reply itself is
|
||||
//! put on screen outside the app.
|
||||
//!
|
||||
//! Scheduled tasks: the app asks `/api/v2/schedules` as often, and a schedule whose last run is a
|
||||
//! new one, with the server's word that it is worth telling (`notified`: its notify policy, and a
|
||||
//! `[SILENT]` run not), is said once -- by the schedule's title, never the result's words. A
|
||||
//! server without the routes answers 404, and nothing is said.
|
||||
|
||||
use std::collections::HashMap;
|
||||
use std::sync::Mutex;
|
||||
@@ -23,6 +29,20 @@ pub struct Words {
|
||||
pub reply: String,
|
||||
/// An agent chat has finished its work.
|
||||
pub agent: String,
|
||||
/// A scheduled task has a result.
|
||||
#[serde(default = "scheduled_word")]
|
||||
pub scheduled: String,
|
||||
/// A scheduled task's run failed.
|
||||
#[serde(default = "scheduled_failed_word")]
|
||||
pub scheduled_failed: String,
|
||||
}
|
||||
|
||||
fn scheduled_word() -> String {
|
||||
"A scheduled task has a result.".into()
|
||||
}
|
||||
|
||||
fn scheduled_failed_word() -> String {
|
||||
"A scheduled task failed.".into()
|
||||
}
|
||||
|
||||
impl Default for Words {
|
||||
@@ -30,6 +50,8 @@ impl Default for Words {
|
||||
Words {
|
||||
reply: "The reply is ready.".into(),
|
||||
agent: "The agent has finished.".into(),
|
||||
scheduled: scheduled_word(),
|
||||
scheduled_failed: scheduled_failed_word(),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -49,6 +71,15 @@ pub struct Watch {
|
||||
pub words: Mutex<Words>,
|
||||
/// Which chats were being answered at the last look (None before the first look).
|
||||
running: Mutex<Option<HashMap<String, bool>>>,
|
||||
/// Each schedule's last run at the last look (None before the first look).
|
||||
runs: Mutex<Option<HashMap<String, String>>>,
|
||||
}
|
||||
|
||||
/// A scheduled task's new run that the server says is worth telling of.
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct Ran {
|
||||
pub title: String,
|
||||
pub failed: bool,
|
||||
}
|
||||
|
||||
impl Watch {
|
||||
@@ -89,9 +120,50 @@ impl Watch {
|
||||
done
|
||||
}
|
||||
|
||||
/// Note the schedules as the server gave them; the new runs to tell of since the last look.
|
||||
/// The first look only notes, as with the chats.
|
||||
pub fn look_schedules(&self, list: &Value) -> Vec<Ran> {
|
||||
let rows = list
|
||||
.get("schedules")
|
||||
.and_then(Value::as_array)
|
||||
.cloned()
|
||||
.unwrap_or_default();
|
||||
let mut seen = self.runs.lock().unwrap();
|
||||
let first = seen.is_none();
|
||||
let mut now = HashMap::new();
|
||||
let mut out = Vec::new();
|
||||
for row in &rows {
|
||||
let Some(id) = row.get("id").and_then(Value::as_str) else {
|
||||
continue;
|
||||
};
|
||||
let Some(run) = row.get("last_run").filter(|r| r.is_object()) else {
|
||||
continue;
|
||||
};
|
||||
let Some(run_id) = run.get("id").and_then(Value::as_str) else {
|
||||
continue;
|
||||
};
|
||||
let before = seen.as_ref().and_then(|s| s.get(id));
|
||||
let told = run.get("notified").and_then(Value::as_bool) == Some(true);
|
||||
if !first && before.map(String::as_str) != Some(run_id) && told {
|
||||
out.push(Ran {
|
||||
title: row
|
||||
.get("title")
|
||||
.and_then(Value::as_str)
|
||||
.unwrap_or("")
|
||||
.to_string(),
|
||||
failed: run.get("status").and_then(Value::as_str) == Some("failed"),
|
||||
});
|
||||
}
|
||||
now.insert(id.to_string(), run_id.to_string());
|
||||
}
|
||||
*seen = Some(now);
|
||||
out
|
||||
}
|
||||
|
||||
/// Start again from nothing: another server, or signed out.
|
||||
pub fn reset(&self) {
|
||||
*self.running.lock().unwrap() = None;
|
||||
*self.runs.lock().unwrap() = None;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -100,6 +172,37 @@ mod tests {
|
||||
use super::*;
|
||||
use serde_json::json;
|
||||
|
||||
fn schedules(rows: &[(&str, Option<(&str, &str, bool)>)]) -> Value {
|
||||
json!({"schedules": rows.iter().map(|(id, run)| json!({
|
||||
"id": id, "title": format!("Task {id}"),
|
||||
"last_run": run.map(|(r, status, told)| json!({"id": r, "status": status, "notified": told})),
|
||||
})).collect::<Vec<_>>()})
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_new_run_the_server_tells_of_is_said_once_and_a_silent_one_never() {
|
||||
let w = Watch::default();
|
||||
assert!(w
|
||||
.look_schedules(&schedules(&[("a", Some(("r1", "ok", true))), ("b", None)]))
|
||||
.is_empty());
|
||||
let ran = w.look_schedules(&schedules(&[
|
||||
("a", Some(("r2", "ok", true))),
|
||||
("b", Some(("r3", "silent", false))),
|
||||
]));
|
||||
assert_eq!(
|
||||
ran,
|
||||
vec![Ran {
|
||||
title: "Task a".into(),
|
||||
failed: false
|
||||
}]
|
||||
);
|
||||
assert!(w
|
||||
.look_schedules(&schedules(&[("a", Some(("r2", "ok", true)))]))
|
||||
.is_empty());
|
||||
let failed = w.look_schedules(&schedules(&[("a", Some(("r4", "failed", true)))]));
|
||||
assert!(failed[0].failed);
|
||||
}
|
||||
|
||||
fn list(rows: &[(&str, bool, &str)]) -> Value {
|
||||
json!({"chats": rows.iter().map(|(id, running, kind)| json!({
|
||||
"id": id, "title": format!("Chat {id}"), "running": running, "kind": kind
|
||||
|
||||
Reference in new issue
Block a user