feat: news notifications for the background collection and a collectedNews tool

Each collection target can announce new items at once or in a daily digest
(off by default) in collect.notifyChannel, as titles and links only, with no
LLM; each post is recorded in the activity log. The brain gets a read-only
collectedNews tool (every target's newest items by default, fenced as
untrusted web content), offered with the app's collection file and the
collection feature, and available to scheduled tasks.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
javis-bot
2026-11-14 17:22:54 +09:00
parent a5bd25b86f
commit 324fd2ea0a
21 changed files with 653 additions and 28 deletions

205
app/src/collect_notify.rs Normal file
View File

@@ -0,0 +1,205 @@
//! News notifications (`app/src/admin.spec.md`, "News notifications"; `docs/v2/proactive.md` 제안 2):
//! new items of the background collection announced in a chat channel, at once or once a day, as
//! titles and links only (no LLM, nothing written by Jarvis about them).
use std::collections::HashMap;
use std::path::{Path, PathBuf};
use std::time::Duration;
use chrono::{DateTime, Local, Timelike};
use serde::{Deserialize, Serialize};
use serde_json::Value;
use crate::settings::Settings;
/// Items listed per target in one message; the rest are counted.
pub const LISTED: usize = 10;
const TITLE_CHARS: usize = 200;
pub const TICK: Duration = Duration::from_secs(60);
#[derive(Debug, Default, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct State {
/// Per target: the `at` of the newest item announced (or seen when its notifications began).
#[serde(default)]
seen: HashMap<String, String>,
/// Per daily target: the day its digest last went out (YYYY-MM-DD).
#[serde(default)]
digest_day: HashMap<String, String>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Message {
pub target: String,
pub count: usize,
pub text: String,
}
fn state_file(data: &Path) -> PathBuf {
data.join("config").join("collect-notify.json")
}
pub fn load(data: &Path) -> State {
std::fs::read_to_string(state_file(data)).ok().and_then(|t| serde_json::from_str(&t).ok()).unwrap_or_default()
}
pub fn save(data: &Path, s: &State) -> std::io::Result<()> {
crate::datadir::write_atomic(&state_file(data), serde_json::to_string_pretty(s).unwrap().as_bytes())
}
fn line(item: &Value) -> String {
let title: String = item["title"].as_str().unwrap_or("").split_whitespace().collect::<Vec<_>>().join(" ")
.chars().take(TITLE_CHARS).collect();
let url = item["url"].as_str().unwrap_or("");
let title = if title.is_empty() { "(제목 없음)".to_owned() } else { title };
// <url> keeps Discord from showing a preview for every link.
if url.starts_with("http://") || url.starts_with("https://") { format!("- {title} <{url}>") } else { format!("- {title}") }
}
fn message(target: &str, new: &[Value], daily: bool) -> Message {
let head = if daily { format!("수집 소식 하루 요약: {target} 새 글 {}건", new.len()) }
else { format!("수집 소식: {target} 새 글 {}건", new.len()) };
let mut lines = vec![head];
lines.extend(new.iter().take(LISTED).map(line));
if new.len() > LISTED {
lines.push(format!("외 {}건", new.len() - LISTED));
}
Message { target: target.to_owned(), count: new.len(), text: lines.join("\n") }
}
/// The messages due at `now`, updating `state`. Nothing without the feature or a channel. A target
/// whose notifications just began only marks what it has as seen; `now` targets announce what is
/// new since then; `daily` ones once a day at `digest_hour`, what came since their last digest.
pub fn due(data: &Path, s: &Settings, now: DateTime<Local>, state: &mut State) -> Vec<Message> {
if !s.features.collect || s.collect.notify_channel.is_none() {
return vec![];
}
let today = now.date_naive().to_string();
let mut out = vec![];
for t in s.collect.targets.iter().filter(|t| t.enabled && t.notify != "off") {
let items = crate::collect::results(data, Some(&t.name), 500).unwrap_or_default();
let newest = items.first().and_then(|i| i["at"].as_str()).map(str::to_owned);
let Some(last) = state.seen.get(&t.name).cloned() else {
state.seen.insert(t.name.clone(), newest.unwrap_or_else(|| now.to_rfc3339_opts(chrono::SecondsFormat::Secs, false)));
if t.notify == "daily" {
state.digest_day.insert(t.name.clone(), today.clone());
}
continue;
};
if t.notify == "daily" && (now.hour() != s.collect.digest_hour || state.digest_day.get(&t.name) == Some(&today)) {
continue;
}
// `at` is the local time with its offset in one format, so text order is time order.
let new: Vec<Value> = items.into_iter().filter(|i| i["at"].as_str().is_some_and(|a| a > last.as_str())).collect();
if t.notify == "daily" {
state.digest_day.insert(t.name.clone(), today.clone());
}
if new.is_empty() {
continue;
}
if let Some(n) = new[0]["at"].as_str() {
state.seen.insert(t.name.clone(), n.to_owned());
}
out.push(message(&t.name, &new, t.notify == "daily"));
}
out
}
/// Announce due news every minute until aborted; each post is recorded in the activity log.
pub async fn run(data: PathBuf, settings: Settings, d: std::sync::Arc<dyn crate::schedule::Deliver>,
activity: std::sync::Arc<crate::activity::Activity>) {
loop {
let mut state = load(&data);
let msgs = due(&data, &settings, Local::now(), &mut state);
if let Err(e) = save(&data, &state) {
crate::warn_log!("conversation", "Warning: could not save the news notification state: {e}");
}
if let Some(channel) = settings.collect.notify_channel {
for m in msgs {
let posted = d.post(channel, &m.text).await;
crate::info_log!("conversation", "news {} items={} posted={}", m.target, m.count, posted.is_ok());
activity.record("collect.notify", &format!("수집 {} → 채널 {channel}", m.target), None, posted.is_ok(),
posted.err().map(|_| "채팅 채널에 보내지 못함".to_owned()));
}
}
tokio::time::sleep(TICK).await;
}
}
#[cfg(test)]
mod tests {
use super::*;
use chrono::TimeZone;
use serde_json::json;
fn settings(notify: &str) -> Settings {
crate::settings::parse_settings(&json!({
"chatChannels": [{"id": "123456789012345678"}],
"collect": {"notifyChannel": "123456789012345678", "digestHour": 9, "targets": [
{"name": "뉴스", "kind": "rss", "value": "https://feed.example/rss", "notify": notify}]}})).unwrap()
}
fn write(data: &Path, day: &str, items: &[(&str, &str)]) {
let dir = data.join("collect").join("뉴스");
std::fs::create_dir_all(&dir).unwrap();
let mut text = std::fs::read_to_string(dir.join(format!("{day}.jsonl"))).unwrap_or_default();
for (at, title) in items {
text.push_str(&json!({"at": at, "title": title, "url": format!("https://e.example/{title}")}).to_string());
text.push('\n');
}
std::fs::write(dir.join(format!("{day}.jsonl")), text).unwrap();
}
fn at(h: u32, m: u32) -> DateTime<Local> {
Local.with_ymd_and_hms(2026, 11, 14, h, m, 0).unwrap()
}
#[test]
fn at_once_announces_only_what_is_new_since_notifications_began() {
let data = crate::datadir::temp_dir("news-now");
let s = settings("now");
write(&data, "2026-11-14", &[("2026-11-14T08:00:00+09:00", "old")]);
let mut st = State::default();
assert!(due(&data, &s, at(8, 30), &mut st).is_empty(), "what was there before is not announced");
write(&data, "2026-11-14", &[("2026-11-14T08:40:00+09:00", "new one")]);
let m = due(&data, &s, at(8, 41), &mut st);
assert_eq!(m.len(), 1);
assert_eq!(m[0].count, 1);
assert!(m[0].text.contains("new one <https://e.example/new one>") && !m[0].text.contains("old"), "{}", m[0].text);
assert!(due(&data, &s, at(8, 42), &mut st).is_empty(), "each item once");
}
#[test]
fn a_daily_digest_goes_out_once_at_its_hour_with_everything_since_the_last() {
let data = crate::datadir::temp_dir("news-daily");
let s = settings("daily");
let mut st = State::default();
assert!(due(&data, &s, at(7, 0), &mut st).is_empty());
let many: Vec<(String, String)> = (0..12).map(|i| (format!("2026-11-14T08:{i:02}:00+09:00"), format!("t{i}"))).collect();
write(&data, "2026-11-14", &many.iter().map(|(a, t)| (a.as_str(), t.as_str())).collect::<Vec<_>>());
assert!(due(&data, &s, at(8, 59), &mut st).is_empty(), "not before its hour");
// The first day's digest was marked when notifications began; the next day's goes out.
let next = Local.with_ymd_and_hms(2026, 11, 15, 9, 0, 0).unwrap();
let m = due(&data, &s, next, &mut st);
assert_eq!(m.len(), 1);
assert_eq!(m[0].count, 12);
assert!(m[0].text.contains("외 2건"), "{}", m[0].text);
assert!(due(&data, &s, next + chrono::Duration::minutes(30), &mut st).is_empty(), "once a day");
}
#[test]
fn nothing_is_announced_without_a_channel_the_feature_or_with_notify_off() {
let data = crate::datadir::temp_dir("news-off");
write(&data, "2026-11-14", &[("2026-11-14T08:00:00+09:00", "x")]);
let mut off = settings("now");
off.collect.notify_channel = None;
let mut nofeature = settings("now");
nofeature.features.collect = false;
for s in [off, nofeature, settings("off")] {
let mut st = State::default();
due(&data, &s, at(8, 0), &mut st);
write(&data, "2026-11-14", &[("2026-11-14T08:30:00+09:00", "y")]);
assert!(due(&data, &s, at(8, 31), &mut st).is_empty());
}
}
}