From ea2d3ba9583cb4bfb8c01b4baeabb48b43370c69 Mon Sep 17 00:00:00 2001 From: javis-bot Date: Sat, 14 Nov 2026 16:27:42 +0900 Subject: [PATCH] feat: reminders and scheduled tasks (schedule tool, app store and delivery) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The brain gets a schedule tool (add, list, delete the asker's own reminders, timers and tasks) offered only with a schedule backend. The voice worker asks the app through schedule_action and runs fired tasks as reply via task with read-only tools, no MCP and a fresh short-term memory. The app keeps the schedules in config/schedules.json, fires them every 5 s, speaks them when the person is in Jarvis's voice channel and otherwise posts in the chat channel they asked in (or schedule.chatChannel for voice). A feature switch and a 예약 screen list and delete everyone's schedules. Co-Authored-By: Claude Opus 5.5 --- CLAUDE.md | 1 + README.md | 1 + app/src/admin.spec.md | 45 +- app/src/admin_api.rs | 37 ++ app/src/conversation.rs | 15 +- app/src/discord.rs | 70 ++- app/src/features.rs | 8 +- app/src/lib.rs | 1 + app/src/logs.rs | 2 +- app/src/schedule.rs | 523 ++++++++++++++++++++++ app/src/services.rs | 14 +- app/src/settings.rs | 28 +- app/src/settings_edit.rs | 5 + app/src/worker_client.rs | 57 ++- app/tests/fixtures/fake_engine_worker.py | 11 +- app/tests/worker_protocol.rs | 38 ++ app/ui/app.js | 29 +- app/ui/index.html | 1 + docs/llm_contexts.md | 1 + docs/v2/proactive.md | 10 +- evals/test_schedule_tool.py | 99 ++++ src/jarvis/reply/engine.py | 22 +- src/jarvis/reply/turn_state.py | 12 + src/jarvis/tools/builtin/schedule.py | 145 ++++++ src/jarvis/tools/builtin/schedule.spec.md | 49 ++ src/jarvis/tools/registry.py | 2 + src/voice_worker/brain.py | 35 +- src/voice_worker/schedule_bridge.py | 27 ++ src/voice_worker/server.py | 83 +++- src/voice_worker/voice_worker.spec.md | 28 ++ tests/test_schedule_tool.py | 109 +++++ tests/test_voice_worker_brain.py | 119 ++++- 32 files changed, 1568 insertions(+), 59 deletions(-) create mode 100644 app/src/schedule.rs create mode 100644 evals/test_schedule_tool.py create mode 100644 src/jarvis/tools/builtin/schedule.py create mode 100644 src/jarvis/tools/builtin/schedule.spec.md create mode 100644 src/voice_worker/schedule_bridge.py create mode 100644 tests/test_schedule_tool.py diff --git a/CLAUDE.md b/CLAUDE.md index d3a2e9d..7eed29a 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -23,6 +23,7 @@ Any code change must either adhere to our spec files perfectly or you should ask | `src/jarvis/reply/prompts/prompts.spec.md` | System/user prompt templates | — | | `src/jarvis/tools/builtin/web_search.spec.md` | webSearch tool: STREAM_BROWSER routing (browser/Gemini), cascade fetch, SSRF guard, prompt-injection fence, links-only envelope | Untrusted web content is fenced as data, not instructions; rank preference over speed; honest failure over confabulation | | `src/jarvis/tools/builtin/chat_channels.spec.md` | chatChannels tool (v2): list, read, search, send, edit, react, delete, pin registered Discord chat channels through the voice worker and app | Offered only with a chat backend; per-channel switches; chat is fenced data; delete needs the user's yes in a later turn | +| `src/jarvis/tools/builtin/schedule.spec.md` | schedule tool (v2): reminders, timers and scheduled tasks of the asker; the app keeps, fires and delivers them | Offered only with a schedule backend; only the asker's own; tasks get read-only tools; never claimed without the tool | | `src/jarvis/tools/builtin/bot_browser.spec.md` | botBrowser tool (v2): open, read, click, type, back in the bot's own Chrome over CDP | Offered only when the app started the bot browser; only http(s); page content is fenced data; no LLM call | | `src/jarvis/tools/builtin/browse_and_play.spec.md` | browseAndPlay tool: play YouTube on the shared screen (screen-share mode only) | Node layer owns Chrome/CDP; mode-gated; fail-open, no LLM call | | `src/jarvis/tools/builtin/nutrition/log_meal.spec.md` | logMeal tool: single-property schema for planner fast-path, internal nutrition extraction, untrusted-data fence, follow-ups | Public schema is a single optional `meal` string; nutrition fields are internal; user text is fenced as data | diff --git a/README.md b/README.md index 1ada089..92ce08a 100644 --- a/README.md +++ b/README.md @@ -21,6 +21,7 @@ - 호출어는 GUI에서 여러 개 등록합니다. - "지정한 사람만 응답"과 "지정한 사람은 호출어 없이 대답"을 각각 켜고 끕니다. - **AI 채팅 채널**: 등록한 디스코드 채팅 채널을 자비스가 읽고, 메시지를 보내고, 자기 메시지를 고칩니다. 채널 ID, 이름, 설명은 GUI에서 편집하고, 고친 이름·설명은 실제 디스코드 채널 이름·주제에도 반영합니다(끌 수 있음). +- **예약 알림과 정기 작업**: "10분 뒤에 알려 줘", "내일 7시에 회의 알려 줘", "평일 아침 9시에 날씨 알려 줘"처럼 말이나 채팅으로 맡깁니다. 맡긴 사람이 자비스와 같은 음성 채널에 있으면 음성으로, 아니면 채팅 채널에 글로 전합니다. 정기 작업은 날씨, 웹 검색처럼 찾아보는 기능만 쓰고, 관리 앱 "예약" 화면에서 목록을 보고 지울 수 있습니다. - **대화 기억**: 본인이 말했던 기록, 등록한 채팅 채널, 같은 음성 채널에 함께 있던 다른 사람 말에서 이번 질문과 관련된 내용을 찾아 답할 때 참고합니다. 다른 사람 말 참고는 GUI에서 끌 수 있습니다. - **화면 보기**: - 전체 화면, 모니터별, 프로그램(창)별 화면을 자비스가 직접 봅니다. diff --git a/app/src/admin.spec.md b/app/src/admin.spec.md index 07aa2c3..73ee47f 100644 --- a/app/src/admin.spec.md +++ b/app/src/admin.spec.md @@ -63,7 +63,8 @@ manages the MCP servers it connects. collection, below; no brain tool) and `browser` (the bot browser, below "Bot browser", and its brain tool `botBrowser`, `src/jarvis/tools/builtin/bot_browser.spec.md`), also on by default; `browserShare` (봇용 크롬창 화면공유 선택 표시, the bot browser on the virtual monitor, below - "Virtual monitor"), off by default. The tools of a feature that + "Virtual monitor"), off by default; `schedule` (reminders and scheduled tasks, below + "Schedules", and the brain tool `schedule`), on by default. The tools of a feature that is off go into the brain's `disabled_tools`: they are neither offered nor run (`voice_worker.spec.md`, "Tools"). The other features of the 기능 screen (PC control, screen viewing) come with their milestone (M7). @@ -103,6 +104,39 @@ sites, search words and RSS feeds on a schedule; there are **no targets by defau first), each item with its `target`; a name that is not a valid target name is refused, so no other folder is read. Lines that do not parse are skipped. +## Schedules (`app/src/schedule.rs`) + +`docs/v2/proactive.md` (제안 1). People ask Jarvis by voice or chat to remind them, set a timer or +check something at a time or regularly; the brain's `schedule` tool +(`src/jarvis/tools/builtin/schedule.spec.md`) asks the app, which keeps, fires and delivers them. + +- **Store**: `config/schedules.json` in the data folder, `{nextId, items}`; each item `{id, user, + name, kind, text, repeat, next, via, channel, created, last}`. Read and written whole, one change + at a time in the process (the services' loop and the screen share it). Text 1-500 characters + (whitespace collapsed), `kind` `remind|task`, `repeat` `none|daily|weekdays|weekly`, `via` + `voice|chat` (chat needs the channel it was asked in), at most 50 per person. A one-off time that + has passed is refused; a repeating one starts at its first time after now (`weekdays` skips + Saturday and Sunday, `weekly` keeps the weekday). Repeats keep the local wall time. +- **The brain's actions** (`schedule_action`, `voice_worker.spec.md`, "Schedules") are done for the + person named in it only: `add` (named as Discord shows them now), `list` (their schedules that + still fire, soonest first), `delete` (one of theirs). The answer carries `{id, kind, text, next, + repeat}`; a refusal says why. With `features.schedule` off the tool is not offered; schedules + already kept still fire and can be deleted on the screen. +- **Firing**: while the services run, every 5 s the due ones are taken (a repeating one moves on to + its next time at once; a one-off one stops firing). One more than 15 minutes late (the app was + off, the PC asleep) is not delivered and is kept as missed. Each delivery runs on its own task. +- **Delivery**: a `task` first runs in the brain (`reply` `via: "task"`: read-only tools, its own + short-term memory, at most 300 s); no answer is a failure. The text sent is `님, `. Asked by voice: spoken when the person is in Jarvis's voice channel, else posted in + `schedule.chatChannel`; asked in chat: posted in that channel. Posts ping nobody and need the + channel's send switch. A delivered one-off schedule is removed; one that failed, or was missed, + stays (not firing again) with its outcome `last: {at, ok, note}` for the screen, and a repeating + one keeps its latest outcome. +- `schedule.chatChannel` (new section, null by default): a registered chat channel with sending on + (anything else is refused), for voice schedules whose person is not in the voice channel. Null: + those are not delivered (and the screen says so). +- Logs (tab `conversation`): ids, kinds, repeats, channel ids, counts and outcomes, never the text. + ## Secrets (`app/src/secrets.rs`) `config/secrets.dat` holds a JSON object of named secrets (`discordToken`, the MCP servers' @@ -120,7 +154,7 @@ headless runs). `debug_log!` and `error_log!` keep printing to stderr and also feed the log hub once the app installs one (`logs::install`). Each line has a time, a level (`error`, `warn`, `info`, `debug`), a service tab and its text. The tab comes from the line's category: `app`, `discord` (`voice`, -`receive`, `play`, `discord`), `chat`, `conversation`, `worker` (the voice worker's own lines; its +`receive`, `play`, `discord`), `chat`, `conversation` (also `schedule`), `worker` (the voice worker's own lines; its STT, LLM and TTS lines go to `stt`, `llm`, `tts` by their worker category), `browser`, `capture`, `game` (the last three from M6-M7). Unknown categories go to `app`. @@ -496,6 +530,7 @@ back as Korean messages for the screen. The Tauri shell passes the screens' `inv | `settings_fields` / `settings_view` / `settings_apply` `{changes}` | the settings editor above; `restartNeeded` when the services run; `sync` with the chat channels sent to Discord (below) | | `service_start` / `service_stop` / `service_restart` / `dashboard` | the controller and its snapshot; start and restart run in the background, stop also cancels a start | | `discord_channel_info` `{id}` / `channel_sync_status` | a channel's Discord name and topic / each synced chat channel's last result (above) | +| `schedules_list` / `schedule_delete` `{id}` | every person's schedules (above), soonest first, with IDs as strings / delete one | | `collect_results` `{target, limit}` | `{targets, items}`: every target folder with results, and the collected items newest first (`target` null: every target; `limit` 1-500, default 100) | | `mcp_env_status` / `mcp_env_set` `{server, name, value}` | which MCP environment values are stored (`{server: {NAME: bool}}`, never the values) / store one (an empty value removes it); only for a saved server and name | | `display_driver_status` / `display_driver_install` / `display_driver_uninstall` | the virtual monitor's driver: `{supported, installed, leftover, other, action}` (Windows only, above) / install or remove it elevated (above), then its status; refused unless the services are stopped or failed, and `service_start` / `service_restart` are refused meanwhile | @@ -524,7 +559,7 @@ Plain HTML, CSS and an ES module, no build step, dark theme after `DESIGN.md`. A Strata's last line, and stop in the header; the header with start, restart and stop is rebuilt only when the state changes, so a click is never lost to the refresh. Every screen change starts a new generation: an answer that arrives after the user moved to another screen is dropped. -- **Settings screens** (봇, 채널·호출, 채팅 채널, 대화 기억, 모델, 기능, 백그라운드 수집, 게임 모드): built from `settings_fields`; only +- **Settings screens** (봇, 채널·호출, 채팅 채널, 대화 기억, 모델, 기능, 백그라운드 수집, 예약, 게임 모드): built from `settings_fields`; only changed fields are sent; the server and voice channel are pickers (choosing another server clears the voice channel and lists only that server's channels); the chat channels are a table with their switches; the bot screen also changes the token; the 기능 screen has the feature @@ -534,7 +569,9 @@ Plain HTML, CSS and an ES module, no build step, dark theme after `DESIGN.md`. A state and the button its `action` names, so a leftover can always be removed). The 백그라운드 수집 screen has the targets (a table: name, kind, address or words, interval in minutes, on/off, delete), the keep days, and the results (the newest 100, filtered by any target that has results, each with its time, title, link and the start of its text shown - as plain text). + as plain text). The 예약 screen has `schedule.chatChannel` and every person's schedules (next time + or "다시 울리지 않음", repeat, who and how they asked, kind and text as plain text, the last + outcome, delete). - **업데이트**: the version here, the state, "다시 확인", "받기" (with its percentage) and "설치하고 다시 시작", and the release's notes as plain text. The app checks once when the main screens open; a newer release renames the button "업데이트 (새 버전)" and says so once. diff --git a/app/src/admin_api.rs b/app/src/admin_api.rs index 08e9e15..3df5fc3 100644 --- a/app/src/admin_api.rs +++ b/app/src/admin_api.rs @@ -355,6 +355,23 @@ impl AdminApi { Ok(json!({"targets": crate::collect::result_targets(dir.path()), "items": crate::collect::results(dir.path(), target, limit)?})) } + // Schedules (admin.spec.md, "Schedules"): every person's, and deleting any of them. + "schedules_list" => { + let items = crate::schedule::Store::new(self.dir()?.path()).all()?; + Ok(json!({"items": items.iter().map(|i| { + let mut v = json!(i); + v["id"] = json!(i.id.to_string()); + v["user"] = json!(i.user.to_string()); + v["channel"] = json!(i.channel.map(|c| c.to_string())); + v + }).collect::>()})) + } + "schedule_delete" => { + let id: u64 = arg_str(&args, "id")?.parse().map_err(|_| "예약 ID가 올바르지 않습니다")?; + crate::schedule::Store::new(self.dir()?.path()).delete(id, None)?; + crate::info_log!("admin", "schedule {id} deleted"); + Ok(json!({"deleted": id.to_string()})) + } "channel_sync_status" => Ok(Value::Object(self.syncer()?.status().into_iter() .map(|(id, s)| (id.to_string(), json!(s))).collect())), "discord_channel_info" => { @@ -474,6 +491,26 @@ mod tests { assert!(a.call("nope", json!({})).await.is_err()); } + #[tokio::test] + async fn the_schedule_screen_lists_everyones_schedules_and_deletes_any() { + let (a, base) = api("api-schedule"); + let data = base.join("d"); + a.call("folder_choose", json!({"path": data.to_string_lossy()})).await.unwrap(); + assert_eq!(a.call("schedules_list", json!({})).await.unwrap()["items"], json!([])); + let store = crate::schedule::Store::new(&data); + let now = chrono::Utc::now().timestamp(); + store.add(123456789012345678, "가", "remind", "회의", now + 600, "none", "voice", None, now).unwrap(); + let b = store.add(9, "나", "task", "날씨", now + 60, "daily", "chat", Some(77), now).unwrap(); + let items = a.call("schedules_list", json!({})).await.unwrap()["items"].as_array().unwrap().clone(); + // Soonest first; IDs as strings (JSON numbers lose Discord IDs). + assert_eq!(items.iter().map(|i| i["text"].as_str().unwrap()).collect::>(), ["날씨", "회의"]); + assert_eq!(items[1]["user"], "123456789012345678"); + assert_eq!(items[0]["channel"], "77"); + a.call("schedule_delete", json!({"id": b.id.to_string()})).await.unwrap(); + assert_eq!(store.all().unwrap().len(), 1); + assert!(a.call("schedule_delete", json!({"id": b.id.to_string()})).await.is_err()); + } + #[tokio::test] async fn collection_targets_are_saved_and_their_results_read_from_the_data_folder() { let (a, base) = api("api-collect"); diff --git a/app/src/conversation.rs b/app/src/conversation.rs index 1093f20..423d706 100644 --- a/app/src/conversation.rs +++ b/app/src/conversation.rs @@ -36,7 +36,7 @@ pub trait Voice { fn ask_addressed(&self, id: &str, user: u64, text: &str) -> bool; fn ask_rank(&self, id: &str, candidates: &[(String, u64, String)]) -> bool; /// Have the brain answer a chat message (not spoken); its text comes as `CoreEvent::ReplyText`. - fn chat_answer(&self, id: &str, user: u64, text: &str) -> bool; + fn chat_answer(&self, id: &str, user: u64, channel: u64, text: &str) -> bool; /// Post the chat answer `id` in `channel` as a reply to `message` (never pinging anyone), then /// tell the brain whether it was posted. fn send_reply(&self, id: &str, channel: u64, message: u64, text: &str); @@ -66,8 +66,8 @@ impl Voice for DiscordCore { fn ask_rank(&self, id: &str, candidates: &[(String, u64, String)]) -> bool { DiscordCore::ask_rank(self, id, candidates) } - fn chat_answer(&self, id: &str, user: u64, text: &str) -> bool { - DiscordCore::chat_answer(self, id, user, text) + fn chat_answer(&self, id: &str, user: u64, channel: u64, text: &str) -> bool { + DiscordCore::chat_answer(self, id, user, channel, text) } fn send_reply(&self, id: &str, channel: u64, message: u64, text: &str) { let core = self.clone(); @@ -190,7 +190,7 @@ impl Conversation { self.chat_answers.retain(|_, (_, _, at)| now < *at + CHAT_ANSWER_KEPT); self.next_id += 1; let id = format!("c{}", self.next_id); - if self.voice.chat_answer(&id, m.author_id, &m.content) { + if self.voice.chat_answer(&id, m.author_id, m.channel_id, &m.content) { self.chat_answers.insert(id, (m.channel_id, m.message_id, now)); } } @@ -485,6 +485,8 @@ mod tests { Addressed(String, u64), Rank(String, Vec), ChatAnswer(String, u64, String), + /// The channel a chat answer was asked for. + ChatIn(u64), SendReply(String, u64, u64, String), NotPosted(String), } @@ -493,6 +495,7 @@ mod tests { struct Fake { did: Rc>>, n: Rc>, + chat_in: Rc>>, } impl Voice for Fake { fn answer(&self, user: u64, text: &str, _heard: (Instant, Instant)) -> Option { @@ -520,7 +523,8 @@ mod tests { self.did.borrow_mut().push(Did::Rank(id.into(), c.iter().map(|x| x.0.clone()).collect())); true } - fn chat_answer(&self, id: &str, user: u64, text: &str) -> bool { + fn chat_answer(&self, id: &str, user: u64, channel: u64, text: &str) -> bool { + self.chat_in.borrow_mut().push(Did::ChatIn(channel)); self.did.borrow_mut().push(Did::ChatAnswer(id.into(), user, text.into())); true } @@ -869,6 +873,7 @@ mod tests { c.on_event(&chat(CH_WAKE, 5, A, false, "자비스 내일 일정 알려 줘"), t); let Did::ChatAnswer(id, user, text) = f.take().remove(0) else { panic!() }; assert_eq!((user, text.as_str()), (A, "자비스 내일 일정 알려 줘")); + assert_eq!(*f.chat_in.borrow(), vec![Did::ChatIn(CH_WAKE)], "the brain is told where it was asked"); c.on_event(&CoreEvent::ReplyText { id: id.clone(), text: "내일은 비어 있어요.".into(), failed: false }, t); assert_eq!(f.take(), vec![Did::SendReply(id.clone(), CH_WAKE, 5, "내일은 비어 있어요.".into())]); // A second reply_text for the same id sends nothing. diff --git a/app/src/discord.rs b/app/src/discord.rs index 970ad43..42e68e1 100644 --- a/app/src/discord.rs +++ b/app/src/discord.rs @@ -199,8 +199,16 @@ struct Inner { /// The latest voice session's number, and the session each utterance in progress started in. voice_session: std::sync::atomic::AtomicU64, utt_sessions: Mutex, + /// The reminders and scheduled tasks the brain's schedule tool adds to (admin.spec.md, "Schedules"). + schedules: OnceLock>, + /// Scheduled tasks waiting for the brain's answer, by reply id. + tasks: Mutex>>, } +/// 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); + /// The voice session each utterance in progress started in (`Heard::session`). #[derive(Default)] pub struct UttSessions(HashMap<(u64, u32), u64>); @@ -260,6 +268,8 @@ impl DiscordCore { names: Mutex::new(HashMap::new()), voice_session: std::sync::atomic::AtomicU64::new(0), utt_sessions: Mutex::new(UttSessions::default()), + schedules: OnceLock::new(), + tasks: Mutex::new(HashMap::new()), }); tokio::spawn(player_task(Arc::downgrade(&inner), player_rx)); tokio::spawn(worker_task(Arc::downgrade(&inner), worker_events)); @@ -480,9 +490,28 @@ impl DiscordCore { Some(with_playback(&self.inner, |p| p.clip(crate::earcon::pcm().to_vec()))) } - /// Have the brain answer a chat message under `id`; the text comes as `CoreEvent::ReplyText`. - pub fn chat_answer(&self, id: &str, user: u64, text: &str) -> bool { - self.inner.worker.reply(id, user, text, "chat", false) + /// Have the brain answer a chat message written in `channel` under `id`; the text comes as + /// `CoreEvent::ReplyText`. + pub fn chat_answer(&self, id: &str, user: u64, channel: u64, text: &str) -> bool { + self.inner.worker.reply_in(id, user, text, "chat", false, Some(channel)) + } + + /// The store the brain's schedule tool works on; without one its actions fail. + pub fn set_schedules(&self, store: Arc) { + let _ = self.inner.schedules.set(store); + } + + /// Run a scheduled task in the brain (read-only tools, not spoken) and wait for its answer. + pub async fn run_task(&self, id: &str, user: u64, text: &str) -> Option { + let (tx, rx) = tokio::sync::oneshot::channel(); + self.inner.tasks.lock().unwrap().insert(id.to_owned(), tx); + if !self.inner.worker.reply(id, user, text, "task", false) { + self.inner.tasks.lock().unwrap().remove(id); + return None; + } + let got = tokio::time::timeout(TASK_TIMEOUT, rx).await; + self.inner.tasks.lock().unwrap().remove(id); + got.ok().and_then(Result::ok).filter(|t| !t.trim().is_empty()) } /// Tell the brain whether the chat answer `id` was posted. @@ -633,6 +662,22 @@ impl DiscordCore { use songbird::CoreEvent as CoreEvent_; +#[async_trait] +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 post(&self, channel: u64, text: &str) -> Result<(), String> { + self.send_chat(channel, text).await.map(|_| ()).map_err(|e| e.to_string()) + } + async fn task(&self, id: &str, user: u64, text: &str) -> Option { + self.run_task(id, user, text).await + } +} + fn with_playback(inner: &Inner, f: impl FnOnce(&mut Playback) -> T) -> T { let (out, events) = { let mut p = inner.playback.lock().unwrap(); @@ -778,6 +823,10 @@ async fn worker_task(core: std::sync::Weak, mut rx: mpsc::UnboundedReceiv WorkerEvent::TtsDone(d) => with_playback(&inner, |p| p.done(&d)), WorkerEvent::ReplyText { id, text, failed } => { debug_log!("voice", "reply {id} chars={} failed={failed}", text.chars().count()); + if let Some(tx) = inner.tasks.lock().unwrap().remove(&id) { + let _ = tx.send(if failed { String::new() } else { text }); + continue; + } let _ = inner.events.send(CoreEvent::ReplyText { id, text, failed }); } WorkerEvent::Addressed { id, addressed } => { @@ -800,9 +849,24 @@ async fn worker_task(core: std::sync::Weak, mut rx: mpsc::UnboundedReceiv core.inner.worker.chat_action_result(&a.id, result); }); } + WorkerEvent::ScheduleAction(a) => { + let result = match inner.schedules.get() { + Some(store) => { + let name = a.user.parse().map(|u| name_of(&inner, u)).unwrap_or_else(|_| a.user.clone()); + crate::schedule::act(store, &a, &name, chrono::Utc::now().timestamp()) + } + None => Err("schedules are not available".into()), + }; + if let Err(e) = &result { + debug_log!("schedule", "schedule {} {} refused: {e}", a.action, a.id); + } + inner.worker.schedule_action_result(&a.id, result); + } WorkerEvent::Disconnected => { // The restarted worker numbers utterances afresh: the old ones' sessions are void. inner.utt_sessions.lock().unwrap().clear(); + // Its tasks will not be answered. + inner.tasks.lock().unwrap().clear(); let _ = inner.events.send(CoreEvent::WorkerLost); } WorkerEvent::Error(_) => {} diff --git a/app/src/features.rs b/app/src/features.rs index cfa11c2..3d32938 100644 --- a/app/src/features.rs +++ b/app/src/features.rs @@ -17,6 +17,7 @@ pub const FEATURE_TOOLS: &[(&str, &[&str])] = &[ ("webSearch", &["webSearch", "fetchWebPage"]), ("meals", &["logMeal", "fetchMeals", "deleteMeal"]), ("browser", &["botBrowser"]), + ("schedule", &["schedule"]), ]; /// The built-in tools of the features that are off. @@ -25,6 +26,7 @@ pub fn disabled_tools(f: &FeatureSettings) -> Vec<&'static str> { "weather" => f.weather, "webSearch" => f.web_search, "browser" => f.browser, + "schedule" => f.schedule, _ => f.meals, }; FEATURE_TOOLS.iter().filter(|(k, _)| !on(k)).flat_map(|(_, tools)| tools.iter().copied()).collect() @@ -102,10 +104,10 @@ mod tests { #[test] fn features_that_are_off_turn_their_tools_off() { - let all_on = FeatureSettings { weather: true, web_search: true, meals: true, collect: true, browser: true, browser_share: true }; + let all_on = FeatureSettings { weather: true, web_search: true, meals: true, collect: true, browser: true, browser_share: true, schedule: true }; assert!(disabled_tools(&all_on).is_empty()); - let off = disabled_tools(&FeatureSettings { weather: false, web_search: true, meals: false, collect: false, browser: false, browser_share: false }); - assert_eq!(off, vec!["getWeather", "logMeal", "fetchMeals", "deleteMeal", "botBrowser"]); + let off = disabled_tools(&FeatureSettings { weather: false, web_search: true, meals: false, collect: false, browser: false, browser_share: false, schedule: false }); + assert_eq!(off, vec!["getWeather", "logMeal", "fetchMeals", "deleteMeal", "botBrowser", "schedule"]); } #[test] diff --git a/app/src/lib.rs b/app/src/lib.rs index 2f7026d..dcc1c32 100644 --- a/app/src/lib.rs +++ b/app/src/lib.rs @@ -25,6 +25,7 @@ pub mod perf; pub mod placement; pub mod policy; pub mod receive; +pub mod schedule; pub mod secrets; pub mod services; pub mod settings; diff --git a/app/src/logs.rs b/app/src/logs.rs index 5aeb8cd..1edfb7d 100644 --- a/app/src/logs.rs +++ b/app/src/logs.rs @@ -44,7 +44,7 @@ pub fn tab_of(category: &str) -> &'static str { match category { "voice" | "receive" | "play" | "discord" => "discord", "chat" => "chat", - "conversation" => "conversation", + "conversation" | "schedule" => "conversation", "worker" | "voice_worker" => "worker", "stt" => "stt", "llm" => "llm", diff --git a/app/src/schedule.rs b/app/src/schedule.rs new file mode 100644 index 0000000..797a8a0 --- /dev/null +++ b/app/src/schedule.rs @@ -0,0 +1,523 @@ +//! Reminders and scheduled tasks (`app/src/admin.spec.md`, "Schedules"): the store in the data +//! folder, the next times, the brain's `schedule_action`, and the loop that fires and delivers. + +use std::path::{Path, PathBuf}; +use std::sync::Mutex; +use std::time::Duration; + +use chrono::{Datelike, Local, NaiveDateTime, TimeZone, Weekday}; +use serde::{Deserialize, Serialize}; +use serde_json::{json, Value}; + +use crate::worker_client::ScheduleAction; + +/// A schedule later than this when its time comes (the app was off, the PC asleep) is not delivered. +pub const GRACE_SECS: i64 = 15 * 60; +/// Schedules one person may have. +pub const PER_PERSON: usize = 50; +pub const MAX_TEXT_CHARS: usize = 500; +/// How often the loop looks for schedules that are due. +pub const TICK: Duration = Duration::from_secs(5); + +/// The store's file is read and written whole; one change at a time in this process (the services' +/// loop and the settings screen both use it). +static FILE_LOCK: Mutex<()> = Mutex::new(()); + +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +pub struct Outcome { + pub at: i64, + pub ok: bool, + /// Why it failed, for the screen (no text of the schedule). + #[serde(default, skip_serializing_if = "Option::is_none")] + pub note: Option, +} + +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +pub struct Schedule { + pub id: u64, + pub user: u64, + pub name: String, + /// `remind` or `task`. + pub kind: String, + pub text: String, + /// `none`, `daily`, `weekdays` or `weekly`. + pub repeat: String, + /// When it fires next (Unix seconds); None: it will not fire again (kept to show its outcome). + pub next: Option, + /// `voice` or `chat`: where it was asked. + pub via: String, + /// The chat channel it was asked in (via chat). + #[serde(default, skip_serializing_if = "Option::is_none")] + pub channel: Option, + pub created: i64, + #[serde(default, skip_serializing_if = "Option::is_none")] + pub last: Option, +} + +#[derive(Debug, Default, Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +struct File { + next_id: u64, + items: Vec, +} + +pub struct Store { + file: PathBuf, +} + +/// The local time `t` (Unix seconds) one day, a weekday or a week on, at the same wall time. +fn step(t: i64, repeat: &str) -> i64 { + let local = Local.timestamp_opt(t, 0).single().unwrap_or_else(Local::now); + let wall = local.naive_local(); + let mut day = wall.date(); + loop { + day = day.succ_opt().unwrap_or(day); + let ok = match repeat { + "weekdays" => !matches!(day.weekday(), Weekday::Sat | Weekday::Sun), + "weekly" => day.weekday() == wall.weekday() && day != wall.date(), + _ => true, + }; + if ok { + break; + } + } + let at = NaiveDateTime::new(day, wall.time()); + // A wall time a clock change skips is taken an hour later. + Local.from_local_datetime(&at).earliest() + .or_else(|| Local.from_local_datetime(&(at + chrono::Duration::hours(1))).earliest()) + .map(|d| d.timestamp()).unwrap_or(t + 86_400) +} + +fn is_weekend(t: i64) -> bool { + Local.timestamp_opt(t, 0).single().is_some_and(|d| matches!(d.weekday(), Weekday::Sat | Weekday::Sun)) +} + +/// The first time a schedule asked for `at` fires: `at` itself, the next weekday for `weekdays`, +/// and for a repeating one that has passed, its first time after `now`. +pub fn first_time(at: i64, repeat: &str, now: i64) -> i64 { + let mut t = at; + if repeat == "weekdays" && is_weekend(t) { + t = step(t, repeat); + } + while repeat != "none" && t <= now { + t = step(t, repeat); + } + t +} + +/// What `take_due` hands out to deliver. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct Due { + pub item: Schedule, + /// The time it was due (the item's `next` has moved on for a repeating one). + pub due: i64, +} + +impl Store { + pub fn new(data: &Path) -> Self { + Self { file: data.join("config").join("schedules.json") } + } + + fn load(&self) -> Result { + match std::fs::read_to_string(&self.file) { + Ok(t) => serde_json::from_str(&t).map_err(|e| format!("예약 파일을 읽을 수 없습니다: {e}")), + Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(File::default()), + Err(e) => Err(format!("예약 파일을 읽을 수 없습니다: {e}")), + } + } + + fn save(&self, f: &File) -> Result<(), String> { + let mut text = serde_json::to_string_pretty(f).unwrap(); + text.push('\n'); + crate::datadir::write_atomic(&self.file, text.as_bytes()).map_err(|e| format!("예약 파일을 쓸 수 없습니다: {e}")) + } + + fn change(&self, f: impl FnOnce(&mut File) -> Result) -> Result { + let _g = FILE_LOCK.lock().unwrap_or_else(|e| e.into_inner()); + let mut file = self.load()?; + let out = f(&mut file)?; + self.save(&file)?; + Ok(out) + } + + /// Every schedule, soonest first (those that will not fire again last). + pub fn all(&self) -> Result, String> { + let _g = FILE_LOCK.lock().unwrap_or_else(|e| e.into_inner()); + let mut items = self.load()?.items; + items.sort_by_key(|i| (i.next.is_none(), i.next, i.id)); + Ok(items) + } + + /// One person's schedules, soonest first. + pub fn of(&self, user: u64) -> Result, String> { + Ok(self.all()?.into_iter().filter(|i| i.user == user).collect()) + } + + #[allow(clippy::too_many_arguments)] + pub fn add(&self, user: u64, name: &str, kind: &str, text: &str, at: i64, repeat: &str, via: &str, + channel: Option, now: i64) -> Result { + let text = text.split_whitespace().collect::>().join(" "); + if text.is_empty() || text.chars().count() > MAX_TEXT_CHARS { + return Err(format!("text must be 1-{MAX_TEXT_CHARS} characters")); + } + if !["remind", "task"].contains(&kind) { + return Err("kind must be remind or task".into()); + } + if !["none", "daily", "weekdays", "weekly"].contains(&repeat) { + return Err("repeat must be none, daily, weekdays or weekly".into()); + } + let channel = match via { + "voice" => None, + "chat" => Some(channel.ok_or("a chat schedule needs the channel it was asked in")?), + _ => return Err("via must be voice or chat".into()), + }; + if repeat == "none" && at <= now { + return Err("that time has already passed".into()); + } + let next = first_time(at, repeat, now); + self.change(|f| { + if f.items.iter().filter(|i| i.user == user && i.next.is_some()).count() >= PER_PERSON { + return Err(format!("at most {PER_PERSON} schedules per person; delete one first")); + } + f.next_id += 1; + let item = Schedule { id: f.next_id, user, name: name.to_owned(), kind: kind.into(), text, + repeat: repeat.into(), next: Some(next), via: via.into(), channel, created: now, last: None }; + f.items.push(item.clone()); + Ok(item) + }) + } + + /// Delete a schedule; with `user`, only one of theirs. + pub fn delete(&self, id: u64, user: Option) -> Result { + self.change(|f| { + let pos = f.items.iter().position(|i| i.id == id && user.is_none_or(|u| u == i.user)) + .ok_or_else(|| "no such schedule of this person".to_string())?; + Ok(f.items.remove(pos)) + }) + } + + /// The schedules due at `now`. A repeating one moves on to its next time; a one-off one stops + /// (`next` None) until `finished` records how it went. One more than `GRACE_SECS` late is not + /// handed out and is recorded as missed. + pub fn take_due(&self, now: i64) -> Result, String> { + let _g = FILE_LOCK.lock().unwrap_or_else(|e| e.into_inner()); + let mut file = self.load()?; + let mut due = vec![]; + let mut changed = false; + for item in file.items.iter_mut() { + let Some(t) = item.next.filter(|t| *t <= now) else { continue }; + changed = true; + item.next = (item.repeat != "none").then(|| first_time(t, &item.repeat, now)); + if now - t > GRACE_SECS { + item.last = Some(Outcome { at: now, ok: false, note: Some("앱이 꺼져 있어 제때 전하지 못함".into()) }); + crate::warn_log!("schedule", "Warning: schedule {} missed ({} s late)", item.id, now - t); + } else { + due.push(Due { item: item.clone(), due: t }); + } + } + if changed { + self.save(&file)?; + } + Ok(due) + } + + /// Record how a delivery went. A one-off schedule delivered is removed; one that failed stays + /// (not firing again) so the screen shows it. + pub fn finished(&self, id: u64, outcome: Outcome) -> Result<(), String> { + self.change(|f| { + if let Some(pos) = f.items.iter().position(|i| i.id == id) { + if outcome.ok && f.items[pos].next.is_none() { + f.items.remove(pos); + } else { + f.items[pos].last = Some(outcome); + } + } + Ok(()) + }) + } +} + +/// A schedule as the brain's tool sees it. +pub fn brain_item(s: &Schedule) -> Value { + json!({"id": s.id.to_string(), "kind": s.kind, "text": s.text, "repeat": s.repeat, + "next": s.next.map(|t| t as f64)}) +} + +/// Do what the brain's schedule tool asked for the person in `a.user` (named `name`): the item(s) +/// for its result, or why not. +pub fn act(store: &Store, a: &ScheduleAction, name: &str, now: i64) -> Result { + let user: u64 = a.user.parse().map_err(|_| "bad user id".to_string())?; + match a.action.as_str() { + "list" => Ok(json!({"items": store.of(user)?.iter().filter(|s| s.next.is_some()).map(brain_item).collect::>()})), + "delete" => { + let id: u64 = a.schedule.as_deref().and_then(|s| s.trim().parse().ok()).ok_or("delete needs the schedule's id")?; + Ok(json!({"item": brain_item(&store.delete(id, Some(user))?)})) + } + "add" => { + let at = a.at.filter(|t| t.is_finite()).ok_or("add needs at")?; + let channel = a.channel.as_deref().and_then(|c| c.parse().ok()); + let s = store.add(user, name, a.kind.as_deref().unwrap_or("remind"), a.text.as_deref().unwrap_or(""), + at.round() as i64, a.repeat.as_deref().unwrap_or("none"), &a.via, channel, now)?; + crate::info_log!("schedule", "schedule {} added: {} {} by {user}", s.id, s.kind, s.repeat); + Ok(json!({"item": brain_item(&s)})) + } + other => Err(format!("unknown schedule action {other}")), + } +} + +/// What the loop needs to deliver (the Discord core; a fake in tests). +#[async_trait::async_trait] +pub trait Deliver: Send + Sync { + /// The person is in Jarvis's voice channel. + fn hears(&self, user: u64) -> bool; + /// Speak in the voice channel; false when not in one. + fn say(&self, text: &str) -> bool; + /// Post in a registered chat channel (never pinging anyone). + async fn post(&self, channel: u64, text: &str) -> Result<(), String>; + /// Run a task in the brain (read-only tools) and wait for its answer. + async fn task(&self, id: &str, user: u64, text: &str) -> Option; +} + +/// Deliver one due schedule: a task runs first; then by voice when it was asked by voice and the +/// person is in Jarvis's voice channel, else in the chat channel it was asked in, or for voice the +/// fallback channel (`schedule.chatChannel`). +pub async fn deliver(d: &dyn Deliver, due: &Due, fallback: Option) -> Outcome { + let s = &due.item; + let now = || chrono::Utc::now().timestamp(); + let fail = |note: &str| Outcome { at: now(), ok: false, note: Some(note.into()) }; + let body = if s.kind == "task" { + match d.task(&format!("s{}-{}", s.id, due.due), s.user, &s.text).await { + Some(a) if !a.trim().is_empty() => a, + _ => return fail("작업 결과를 만들지 못함"), + } + } else { + s.text.clone() + }; + let text = format!("{}님, {}", s.name, body.trim()); + if s.via == "voice" && d.hears(s.user) && d.say(&text) { + crate::debug_log!("schedule", "schedule {} spoken chars={}", s.id, text.chars().count()); + return Outcome { at: now(), ok: true, note: None }; + } + let Some(channel) = (if s.via == "chat" { s.channel } else { fallback }) else { + return fail("음성 채널에 없고 알림 채팅 채널이 정해지지 않음"); + }; + match d.post(channel, &text).await { + Ok(()) => { + crate::debug_log!("schedule", "schedule {} posted in {channel} chars={}", s.id, text.chars().count()); + Outcome { at: now(), ok: true, note: None } + } + Err(e) => { + crate::warn_log!("schedule", "Warning: schedule {} could not be posted in {channel}: {e}", s.id); + fail("채팅 채널에 보내지 못함") + } + } +} + +/// Fire due schedules until the task is aborted. Each delivery runs on its own, so a slow task does +/// not hold up a reminder due meanwhile. +pub async fn run(store: std::sync::Arc, d: std::sync::Arc, fallback: Option) { + loop { + match store.take_due(chrono::Utc::now().timestamp()) { + Ok(due) => for item in due { + let (store, d) = (store.clone(), d.clone()); + tokio::spawn(async move { + let out = deliver(d.as_ref(), &item, fallback).await; + crate::info_log!("schedule", "schedule {} {}", item.item.id, if out.ok { "delivered" } else { "not delivered" }); + if let Err(e) = store.finished(item.item.id, out) { + crate::warn_log!("schedule", "Warning: {e}"); + } + }); + }, + Err(e) => { + crate::warn_log!("schedule", "Warning: {e}"); + } + } + tokio::time::sleep(TICK).await; + } +} + +#[cfg(test)] +mod tests { + use super::*; + use chrono::Timelike; + + fn local(y: i32, mo: u32, d: u32, h: u32, mi: u32) -> i64 { + Local.with_ymd_and_hms(y, mo, d, h, mi, 0).earliest().unwrap().timestamp() + } + + fn store() -> Store { + Store::new(&crate::datadir::temp_dir("schedule")) + } + + #[test] + fn repeats_keep_the_wall_time_and_skip_what_they_leave_out() { + // 2026-11-13 is a Friday. + let fri = local(2026, 11, 13, 9, 0); + assert_eq!(step(fri, "daily"), local(2026, 11, 14, 9, 0)); + assert_eq!(step(fri, "weekdays"), local(2026, 11, 16, 9, 0)); + assert_eq!(step(fri, "weekly"), local(2026, 11, 20, 9, 0)); + // Asked for a Saturday on weekdays: Monday. A daily one that passed: its next time. + assert_eq!(first_time(local(2026, 11, 14, 9, 0), "weekdays", 0), local(2026, 11, 16, 9, 0)); + assert_eq!(first_time(fri, "daily", local(2026, 11, 15, 10, 0)), local(2026, 11, 16, 9, 0)); + assert_eq!(Local.timestamp_opt(step(fri, "daily"), 0).unwrap().hour(), 9); + } + + #[test] + fn a_person_sees_and_deletes_only_their_own() { + let s = store(); + let a = s.add(1, "가", "remind", " 회의 준비 ", 2000, "none", "voice", None, 1000).unwrap(); + let b = s.add(2, "나", "task", "날씨", 3000, "daily", "chat", Some(77), 1000).unwrap(); + assert_eq!(a.text, "회의 준비"); + assert_eq!(s.of(1).unwrap(), vec![a.clone()]); + assert!(s.delete(b.id, Some(1)).is_err()); + assert_eq!(s.delete(b.id, None).unwrap(), b); + assert_eq!(s.all().unwrap(), vec![a]); + } + + #[test] + fn bad_adds_are_refused() { + let s = store(); + for (kind, text, at, repeat, via, channel) in [ + ("remind", "", 2000, "none", "voice", None), + ("remind", "x", 500, "none", "voice", None), + ("alarm", "x", 2000, "none", "voice", None), + ("remind", "x", 2000, "hourly", "voice", None), + ("remind", "x", 2000, "none", "chat", None), + ] { + assert!(s.add(1, "가", kind, text, at, repeat, via, channel, 1000).is_err(), "{kind} {text} {repeat} {via}"); + } + assert!(s.add(1, "가", "remind", &"x".repeat(MAX_TEXT_CHARS + 1), 2000, "none", "voice", None, 1000).is_err()); + for _ in 0..PER_PERSON { + s.add(1, "가", "remind", "x", 2000, "none", "voice", None, 1000).unwrap(); + } + assert!(s.add(1, "가", "remind", "x", 2000, "none", "voice", None, 1000).is_err()); + assert!(s.add(2, "나", "remind", "x", 2000, "none", "voice", None, 1000).is_ok()); + } + + #[test] + fn due_schedules_are_handed_out_once_and_late_ones_are_recorded_as_missed() { + let s = store(); + let once = s.add(1, "가", "remind", "a", 2000, "none", "voice", None, 1000).unwrap(); + let daily = s.add(1, "가", "remind", "b", local(2026, 11, 13, 9, 0), "daily", "voice", None, 0).unwrap(); + let late = s.add(1, "가", "remind", "c", 1500, "none", "voice", None, 1000).unwrap(); + assert!(s.take_due(1499).unwrap().is_empty()); + let now = local(2026, 11, 13, 9, 1); + let got: Vec = s.take_due(now).unwrap().into_iter().map(|d| d.item.id).collect(); + // `once` and `late` were due long before `now`: missed. The daily one moves to tomorrow. + assert_eq!(got, vec![daily.id]); + assert!(s.take_due(now).unwrap().is_empty()); + let all = s.all().unwrap(); + let find = |id| all.iter().find(|i| i.id == id).unwrap().clone(); + assert_eq!(find(daily.id).next, Some(local(2026, 11, 14, 9, 0))); + for id in [once.id, late.id] { + let i = find(id); + assert_eq!(i.next, None); + assert!(!i.last.unwrap().ok); + } + } + + #[test] + fn a_delivered_one_off_schedule_is_removed_and_a_failed_one_kept() { + let s = store(); + let a = s.add(1, "가", "remind", "a", 2000, "none", "voice", None, 1000).unwrap(); + let b = s.add(1, "가", "remind", "b", 2000, "none", "voice", None, 1000).unwrap(); + assert_eq!(s.take_due(2000).unwrap().len(), 2); + s.finished(a.id, Outcome { at: 2001, ok: true, note: None }).unwrap(); + s.finished(b.id, Outcome { at: 2001, ok: false, note: Some("x".into()) }).unwrap(); + let all = s.all().unwrap(); + assert_eq!(all.len(), 1); + assert_eq!(all[0].id, b.id); + assert!(!all[0].last.as_ref().unwrap().ok); + } + + #[derive(Default)] + struct Fake { + hears: bool, + said: Mutex>, + posted: Mutex>, + post_fails: bool, + answer: Option, + tasks: Mutex>, + } + + #[async_trait::async_trait] + impl Deliver for Fake { + fn hears(&self, _user: u64) -> bool { + self.hears + } + fn say(&self, text: &str) -> bool { + self.said.lock().unwrap().push(text.into()); + true + } + async fn post(&self, channel: u64, text: &str) -> Result<(), String> { + if self.post_fails { + return Err("no".into()); + } + self.posted.lock().unwrap().push((channel, text.into())); + Ok(()) + } + async fn task(&self, _id: &str, user: u64, text: &str) -> Option { + self.tasks.lock().unwrap().push((user, text.into())); + self.answer.clone() + } + } + + fn due(kind: &str, via: &str, channel: Option) -> Due { + Due { item: Schedule { id: 1, user: 5, name: "철수".into(), kind: kind.into(), text: "회의".into(), + repeat: "none".into(), next: None, via: via.into(), channel, created: 0, last: None }, due: 10 } + } + + #[tokio::test] + async fn a_voice_reminder_is_spoken_to_someone_there_and_posted_otherwise() { + let f = Fake { hears: true, ..Default::default() }; + assert!(deliver(&f, &due("remind", "voice", None), Some(9)).await.ok); + assert_eq!(*f.said.lock().unwrap(), vec!["철수님, 회의".to_string()]); + let away = Fake::default(); + assert!(deliver(&away, &due("remind", "voice", None), Some(9)).await.ok); + assert_eq!(*away.posted.lock().unwrap(), vec![(9, "철수님, 회의".to_string())]); + // Nowhere to go: not delivered, and says why. + let out = deliver(&away, &due("remind", "voice", None), None).await; + assert!(!out.ok && out.note.is_some()); + } + + #[tokio::test] + async fn a_chat_reminder_goes_to_its_channel_even_when_the_person_is_in_voice() { + let f = Fake { hears: true, ..Default::default() }; + assert!(deliver(&f, &due("remind", "chat", Some(77)), Some(9)).await.ok); + assert!(f.said.lock().unwrap().is_empty()); + assert_eq!(*f.posted.lock().unwrap(), vec![(77, "철수님, 회의".to_string())]); + let fails = Fake { post_fails: true, ..Default::default() }; + assert!(!deliver(&fails, &due("remind", "chat", Some(77)), None).await.ok); + } + + #[tokio::test] + async fn a_task_delivers_the_brains_answer_and_fails_without_one() { + let f = Fake { answer: Some("맑아요".into()), ..Default::default() }; + assert!(deliver(&f, &due("task", "chat", Some(77)), None).await.ok); + assert_eq!(*f.tasks.lock().unwrap(), vec![(5, "회의".to_string())]); + assert_eq!(*f.posted.lock().unwrap(), vec![(77, "철수님, 맑아요".to_string())]); + let none = Fake::default(); + let out = deliver(&none, &due("task", "chat", Some(77)), None).await; + assert!(!out.ok && none.posted.lock().unwrap().is_empty()); + } + + #[test] + fn the_brains_actions_are_done_for_the_asker_only() { + let s = store(); + let a = |action: &str, user: &str, extra: Value| -> ScheduleAction { + let mut v = json!({"id": "x", "action": action, "user": user, "via": "voice"}); + v.as_object_mut().unwrap().extend(extra.as_object().unwrap().clone()); + serde_json::from_value(v).unwrap() + }; + let added = act(&s, &a("add", "1", json!({"kind": "remind", "text": "물", "at": 2000.4, "repeat": "none"})), "가", 1000).unwrap(); + assert_eq!(added["item"]["id"], "1"); + assert_eq!(added["item"]["next"], 2000.0); + assert_eq!(act(&s, &a("list", "2", json!({})), "나", 1000).unwrap()["items"], json!([])); + assert!(act(&s, &a("delete", "2", json!({"schedule": "1"})), "나", 1000).is_err()); + assert_eq!(act(&s, &a("list", "1", json!({})), "가", 1000).unwrap()["items"].as_array().unwrap().len(), 1); + assert!(act(&s, &a("delete", "1", json!({"schedule": "1"})), "가", 1000).is_ok()); + assert!(act(&s, &a("add", "1", json!({"text": "x", "at": 2000.0, "via": "chat"})), "가", 1000).is_err()); + } +} diff --git a/app/src/services.rs b/app/src/services.rs index 922a60c..e49f2ba 100644 --- a/app/src/services.rs +++ b/app/src/services.rs @@ -27,6 +27,8 @@ pub struct Services { pub game: Arc, /// Moves the LLM between the performance stages as games come and go. pub perf: std::sync::Mutex>>, + /// Fires the reminders and scheduled tasks (admin.spec.md, "Schedules"). + pub schedules: std::sync::Mutex>>, } /// The bot browser of a run (admin.spec.md, "Bot browser"). @@ -68,6 +70,11 @@ impl Services { p.abort(); let _ = p.await; } + let schedules = self.schedules.lock().unwrap().take(); + if let Some(s) = schedules { + s.abort(); + let _ = s.await; + } self.game.stop(); self.core.close().await; self.worker.close(); @@ -191,7 +198,10 @@ pub async fn start_services(data_dir: &Path, llm: Arc) -> Result) -> Result, pub collect: CollectSettings, pub game: GameSettings, + /// The registered chat channel a schedule asked by voice is posted in when its person is not in + /// Jarvis's voice channel (admin.spec.md, "Schedules"); None: not delivered then. + pub schedule_channel: Option, } #[derive(Debug, Clone, PartialEq, Eq)] @@ -217,10 +222,11 @@ impl Default for Settings { response: PolicySettings::default(), memory: Map::new(), llm: LlmSettings { url: None, port: 8080, context: 32768 }, - features: FeatureSettings { weather: true, web_search: true, meals: true, collect: true, browser: true, browser_share: false }, + features: FeatureSettings { weather: true, web_search: true, meals: true, collect: true, browser: true, browser_share: false, schedule: true }, mcp_servers: vec![], collect: CollectSettings { keep_days: 30, targets: vec![] }, game: GameSettings { detect: true, games: vec![], not_games: vec![], other_room_gb: 1.0, reserve_gb: 8.0, revert_seconds: 30, pause_in_game: false }, + schedule_channel: None, } } } @@ -496,6 +502,10 @@ pub fn parse_settings(raw: &Value) -> Result { sync_discord: get_bool(Some(m), "syncDiscord", &path, true)?, }); } + let schedule_channel = opt_snowflake(section(raw, "schedule")?, "chatChannel", "schedule", None, "a Discord channel ID")?; + if schedule_channel.is_some_and(|c| !chat_channels.iter().any(|x| x.id == c && x.send)) { + return err("schedule.chatChannel must be a registered chat channel with sending on"); + } Ok(Settings { discord: DiscordSettings { @@ -541,11 +551,13 @@ pub fn parse_settings(raw: &Value) -> Result { collect: get_bool(f, "collect", "features", d.features.collect)?, browser: get_bool(f, "browser", "features", d.features.browser)?, browser_share: get_bool(f, "browserShare", "features", d.features.browser_share)?, + schedule: get_bool(f, "schedule", "features", d.features.schedule)?, } }, mcp_servers: parse_mcp_servers(raw)?, collect: parse_collect(raw, &d.collect)?, game: parse_game(raw, &d.game)?, + schedule_channel, }) } @@ -881,18 +893,26 @@ mod tests { #[test] fn features_and_mcp_servers_are_read_and_checked() { let s = parse_settings(&json!({})); - assert_eq!(s.unwrap().features, FeatureSettings { weather: true, web_search: true, meals: true, collect: true, browser: true, browser_share: false }); + assert_eq!(s.unwrap().features, FeatureSettings { weather: true, web_search: true, meals: true, collect: true, browser: true, browser_share: false, schedule: true }); let s = parse_settings(&json!({ - "features": {"weather": false, "meals": false, "browserShare": true}, + "features": {"weather": false, "meals": false, "browserShare": true, "schedule": false}, "mcpServers": [{"name": "brave", "command": "npx", "args": ["-y", "brave"], "env": ["BRAVE_API_KEY", " "]}, {"name": "time-2", "command": "uvx", "enabled": false}] })).unwrap(); - assert_eq!(s.features, FeatureSettings { weather: false, web_search: true, meals: false, collect: true, browser: true, browser_share: true }); + assert_eq!(s.features, FeatureSettings { weather: false, web_search: true, meals: false, collect: true, browser: true, browser_share: true, schedule: false }); assert_eq!(s.mcp_servers, vec![ McpServerSettings { name: "brave".into(), command: "npx".into(), args: vec!["-y".into(), "brave".into()], env: vec!["BRAVE_API_KEY".into()], enabled: true }, McpServerSettings { name: "time-2".into(), command: "uvx".into(), args: vec![], env: vec![], enabled: false }, ]); + // A schedule channel must be a registered one that Jarvis may send in. + let ch = json!([{"id": "123456789012345678", "send": true}, {"id": "123456789012345679", "send": false}]); + let s = parse_settings(&json!({"chatChannels": ch, "schedule": {"chatChannel": "123456789012345678"}})).unwrap(); + assert_eq!(s.schedule_channel, Some(123456789012345678)); + for c in ["123456789012345679", "123456789012345670"] { + let e = parse_settings(&json!({"chatChannels": ch, "schedule": {"chatChannel": c}})).unwrap_err(); + assert!(e.0.contains("schedule.chatChannel"), "{e}"); + } let cases = [ (json!({"features": {"weather": "no"}}), "features.weather"), (json!({"mcpServers": {}}), "mcpServers"), diff --git a/app/src/settings_edit.rs b/app/src/settings_edit.rs index 5c97ff4..524f7d6 100644 --- a/app/src/settings_edit.rs +++ b/app/src/settings_edit.rs @@ -22,6 +22,7 @@ pub enum Screen { Features, Collect, Game, + Schedule, } #[derive(Debug, Clone, PartialEq, serde::Serialize)] @@ -126,10 +127,14 @@ pub fn fields() -> Vec { f("features.collect", Features, Bool, json!(true), "백그라운드 수집"), f("features.browser", Features, Bool, json!(true), "브라우저 제어(봇용 크롬)"), f("features.browserShare", Features, Bool, json!(false), "봇용 크롬창 화면공유 선택 표시(가상 모니터, Windows)"), + f("features.schedule", Features, Bool, json!(true), "예약 알림과 정기 작업"), f("mcpServers", Features, McpServers, json!([]), "MCP 서버"), // Background collection (admin.spec.md, "Background collection"): no targets by default. f("collect.targets", Collect, CollectTargets, json!([]), "수집 대상"), f("collect.keepDays", Collect, Int { min: 1, max: 365 }, json!(30), "보관 기간(일)"), + // Schedules (admin.spec.md, "Schedules"). + f("schedule.chatChannel", Schedule, Id, Value::Null, + "음성으로 맡긴 알림을 그 사람이 음성 채널에 없을 때 보낼 채팅 채널(등록한 채널)"), // Game detection (admin.spec.md, "Game detection"). f("game.detect", Game, Bool, json!(true), "게임 감지"), f("game.games", Game, TextList { max_items: 100, nullable: false }, json!([]), "게임으로 볼 프로그램(파일 이름, 예: MyGame.exe)"), diff --git a/app/src/worker_client.rs b/app/src/worker_client.rs index 591fd2b..537eb3a 100644 --- a/app/src/worker_client.rs +++ b/app/src/worker_client.rs @@ -122,6 +122,30 @@ pub struct ChatAction { pub emoji: Option, } +/// The brain's schedule tool asks the app for the person asking (voice_worker.spec.md, "Schedules"). +#[derive(Debug, Clone, Deserialize, PartialEq)] +pub struct ScheduleAction { + pub id: String, + /// `add`, `list` or `delete`. + pub action: String, + pub user: String, + /// `voice` or `chat`: where they asked. + pub via: String, + #[serde(default)] + pub channel: Option, + #[serde(default)] + pub kind: Option, + #[serde(default)] + pub text: Option, + #[serde(default)] + pub at: Option, + #[serde(default)] + pub repeat: Option, + /// delete: the schedule's id. + #[serde(default)] + pub schedule: Option, +} + #[derive(Debug, Clone, PartialEq)] pub enum WorkerEvent { Ready(HelloOk), @@ -132,6 +156,7 @@ pub enum WorkerEvent { TtsAudio { id: String, seq: u32, pcm: Vec }, TtsDone(TtsDone), ChatAction(ChatAction), + ScheduleAction(ScheduleAction), /// The brain's answer to `reply` (empty and `failed` when it gave none). ReplyText { id: String, text: String, failed: bool }, /// Was the utterance said to Jarvis? (a failed judgement is a no) @@ -320,6 +345,21 @@ impl WorkerClient { self.send_json(v); } + /// The outcome of a `schedule_action`: `{item}` or `{items}`, or why it failed. + pub fn schedule_action_result(&self, id: &str, result: Result) { + let msg = match result { + Ok(Value::Object(mut m)) => { + m.insert("type".into(), json!("schedule_action_result")); + m.insert("id".into(), json!(id)); + m.insert("ok".into(), json!(true)); + Value::Object(m) + } + Ok(_) => json!({"type": "schedule_action_result", "id": id, "ok": true}), + Err(e) => json!({"type": "schedule_action_result", "id": id, "ok": false, "error": e}), + }; + self.send_json(msg); + } + pub fn chat_delete(&self, channel: u64, message: u64) { self.send_json(json!({"type": "chat_delete", "channel": channel.to_string(), "message_id": message.to_string()})); @@ -330,13 +370,23 @@ impl WorkerClient { /// Ask the brain to answer `user`. With `speak` the answer is spoken under `id` (its audio and /// `tts_done` come like a TTS request's); it ends as failed when it cannot be sent, the brain gives /// no answer, or the worker refuses it. - /// `via`: `voice` or `chat` (the brain's history scope and where the answer is stored). + /// `via`: `voice`, `chat` or `task` (a scheduled task: read-only tools, never spoken). pub fn reply(&self, id: &str, user: u64, text: &str, via: &str, speak: bool) -> bool { + self.reply_in(id, user, text, via, speak, None) + } + + /// `reply`, naming the chat channel a chat message was written in (the brain's schedule tool + /// delivers there). + pub fn reply_in(&self, id: &str, user: u64, text: &str, via: &str, speak: bool, channel: Option) -> bool { if speak { self.inner.state.lock().unwrap().open_tts.insert(id.to_owned()); } - let sent = self.send_json(json!({"type": "reply", "id": id, "user": user.to_string(), "text": text, - "via": via, "speak": speak})); + let mut msg = json!({"type": "reply", "id": id, "user": user.to_string(), "text": text, "via": via, + "speak": speak}); + if let Some(c) = channel { + msg["channel"] = json!(c.to_string()); + } + let sent = self.send_json(msg); if !sent && speak { self.end_failed(id); } @@ -580,6 +630,7 @@ fn on_text(inner: &Inner, t: &str) { "partial" => serde_json::from_value(v).ok().map(WorkerEvent::Partial), "final" => serde_json::from_value(v).ok().map(WorkerEvent::Final), "chat_action" => serde_json::from_value(v).ok().map(WorkerEvent::ChatAction), + "schedule_action" => serde_json::from_value(v).ok().map(WorkerEvent::ScheduleAction), "llm_idle" => { if let Some(id) = v["id"].as_u64() { inner.llm_idle.send_if_modified(|got| if id > *got { *got = id; true } else { false }); diff --git a/app/tests/fixtures/fake_engine_worker.py b/app/tests/fixtures/fake_engine_worker.py index d95ad58..c7ef061 100644 --- a/app/tests/fixtures/fake_engine_worker.py +++ b/app/tests/fixtures/fake_engine_worker.py @@ -25,9 +25,16 @@ class LevelTranscriber: class FakeBrain: - """Answers with two sentences, says only "켜" (on) requests were said to it, ranks in reverse.""" + """Answers with two sentences, says only "켜" (on) requests were said to it, ranks in reverse. + "예약" adds a reminder through the schedule backend and answers with its result; a task answers + "task: ".""" - def reply(self, user, text, history, chat=None): + def reply(self, user, text, history, chat=None, schedule=None, task=False): + if task: + return f"task: {text}" + if "예약" in text and schedule is not None: + got = schedule.act("add", kind="remind", text="물 마시기", at=4_000_000_000.0, repeat="daily") + return f"scheduled {got.get('item', {}).get('id')} {got.get('error', '')}".strip() return "첫 문장입니다. 둘째 문장입니다." def addressed(self, user, text, names): diff --git a/app/tests/worker_protocol.rs b/app/tests/worker_protocol.rs index e86efa5..a48f9c0 100644 --- a/app/tests/worker_protocol.rs +++ b/app/tests/worker_protocol.rs @@ -145,6 +145,44 @@ async fn audio_and_tts_round_trip_through_the_python_server() { .expect("judgements"); assert_eq!(addressed, Some(("q1".to_string(), true))); assert_eq!(ranked, Some(("k1".to_string(), vec!["2".to_string(), "1".to_string()]))); + + // The brain's schedule tool asks the app for the person and the channel they asked in; the + // app's store answers; a scheduled task comes back as text. + let data = std::env::temp_dir().join(format!("jarvis-proto-schedule-{}", std::process::id())); + let store = jarvis_app::schedule::Store::new(&data); + assert!(c.reply_in("c1", 42, "예약해 줘", "chat", false, Some(77))); + let text = tokio::time::timeout(Duration::from_secs(5), async { + loop { + match rx.recv().await.unwrap() { + WorkerEvent::ScheduleAction(a) => { + assert_eq!((a.action.as_str(), a.user.as_str(), a.via.as_str(), a.channel.as_deref()), + ("add", "42", "chat", Some("77"))); + c.schedule_action_result(&a.id, jarvis_app::schedule::act(&store, &a, "가", 1000)); + } + WorkerEvent::ReplyText { id, text, .. } if id == "c1" => return text, + _ => {} + } + } + }) + .await + .expect("schedule round trip"); + assert_eq!(text, "scheduled 1"); + let kept = store.all().unwrap(); + assert_eq!((kept[0].user, kept[0].channel, kept[0].repeat.as_str()), (42, Some(77), "daily")); + assert!(c.reply("t1", 42, "날씨", "task", false)); + let task = tokio::time::timeout(Duration::from_secs(5), async { + loop { + if let WorkerEvent::ReplyText { id, text, failed } = rx.recv().await.unwrap() { + if id == "t1" { + return (text, failed); + } + } + } + }) + .await + .expect("task answer"); + assert_eq!(task, ("task: 날씨".to_string(), false)); + let _ = std::fs::remove_dir_all(&data); c.close(); let _ = child.kill().await; } diff --git a/app/ui/app.js b/app/ui/app.js index bf6bcf0..4f67217 100644 --- a/app/ui/app.js +++ b/app/ui/app.js @@ -306,6 +306,32 @@ async function collectResults() { h("div", { class: "field" }, h("label", {}, "대상"), pick, h("button", { class: "btn", onclick: load }, "새로 고침")), list); } +const SCHEDULE_KIND = { remind: "알림", task: "작업" }; +const REPEAT = { none: "한 번", daily: "매일", weekdays: "평일", weekly: "매주" }; +/** Everyone's reminders and scheduled tasks (admin.spec.md, "Schedules"); the text is shown as plain text. */ +async function scheduleList() { + const list = h("div"); + const when = (t) => t == null ? "다시 울리지 않음" : new Date(t * 1000).toLocaleString("ko-KR", + { month: "numeric", day: "numeric", weekday: "short", hour: "2-digit", minute: "2-digit" }); + const load = async () => { + try { + const { items } = await api("schedules_list"); + list.replaceChildren(items.length ? h("table", {}, h("tbody", {}, items.map((i) => h("tr", {}, + h("td", { class: "small" }, when(i.next), h("div", { class: "small muted" }, REPEAT[i.repeat] || i.repeat)), + h("td", { class: "small" }, i.name, h("div", { class: "small muted" }, i.via === "chat" ? "채팅으로 맡김" : "음성으로 맡김")), + h("td", {}, h("div", {}, (SCHEDULE_KIND[i.kind] || i.kind) + ": " + i.text), + i.last ? h("div", { class: "small muted" }, i.last.ok ? "지난번: 전함" : "지난번: 전하지 못함" + (i.last.note ? ` (${i.last.note})` : "")) : null), + h("td", {}, h("button", { class: "btn", onclick: async () => { + try { await api("schedule_delete", { id: i.id }); toast("지웠습니다"); load(); } catch (e) { fail(e); } + } }, "삭제")))))) : h("div", { class: "muted" }, "예약이 없습니다. 음성이나 채팅으로 \"내일 7시에 회의 알려 줘\"처럼 맡기면 여기에 나옵니다.")); + } catch (e) { fail(e); } + }; + load(); + return h("div", { class: "card" }, h("h2", {}, "예약 목록"), + h("div", { class: "hint" }, "정기 작업은 날씨, 웹 검색처럼 찾아보는 기능만 씁니다. 보내기, 지우기, PC 조작은 하지 않습니다."), + h("div", { class: "field" }, h("button", { class: "btn", onclick: load }, "새로 고침")), list); +} + async function settingsScreen(screen, title, extra) { const [all, view] = await Promise.all([fields(), api("settings_view")]); const list = all.filter((f) => f.screen === screen); @@ -624,10 +650,11 @@ async function show(name) { if (name === "logs") { const l = logsScreen(); screen.replaceChildren(l.view); ticker = setInterval(l.tick, 1000); return; } if (name === "update") { const u = updateScreen(); screen.replaceChildren(u.view); ticker = setInterval(u.tick, 1000); return; } const titles = { bot: "봇", channels: "채널·호출", chat: "채팅 채널", memory: "대화 기억", model: "모델", features: "기능", - collect: "백그라운드 수집", game: "게임 모드" }; + collect: "백그라운드 수집", game: "게임 모드", schedule: "예약" }; const extra = name === "bot" ? await tokenCard() : name === "features" ? await displayDriverCard() : null; const view = await settingsScreen(name, titles[name], extra); if (name === "collect") view.append(await collectResults()); + if (name === "schedule") view.append(await scheduleList()); if (my === gen) screen.replaceChildren(view); } catch (e) { fail(e); } } diff --git a/app/ui/index.html b/app/ui/index.html index d46e9ad..c4bd1d2 100644 --- a/app/ui/index.html +++ b/app/ui/index.html @@ -19,6 +19,7 @@ + diff --git a/docs/llm_contexts.md b/docs/llm_contexts.md index e3609a6..7ce42b2 100644 --- a/docs/llm_contexts.md +++ b/docs/llm_contexts.md @@ -23,6 +23,7 @@ Every distinct LLM call in Jarvis, what feeds it, what consumes it, and how it i - **v2 history context** (optional, caller-supplied `history=` on `run_reply_engine`): the recent window and search matches from the history store ([src/jarvis/memory/history.py](src/jarvis/memory/history.py), [spec](src/jarvis/memory/history.spec.md)), scoped to the asker and capped by the store's size budget (default about 4,000 tokens, matches at most half). No LLM call; not gated by the planner. Injected in a `` fence labelled "Information shared in previous conversations"; the lines are data, not instructions. Eval: `evals/test_history_context_recall.py`. With `legacy_memory=False` (the v2 brain in the voice worker) the warm profile and the whole memory enrichment chain (#3 extractor, #3b recall gate, #4 digest and the diary/graph reads) are skipped for the turn, because v1's long-term memory belongs to a single user. - Time + location context (re-injected each turn) - **v2 chat channel tool**: when the voice worker's brain sets a chat backend (`turn_state.set_chat`), `chatChannels` is added to the turn's tools after routing (like `setBroadcast`) and its read/search results enter the loop inside an UNTRUSTED CHAT MESSAGES fence; without a backend it is not in the catalogue at all. Eval: `evals/test_chat_channels_tool.py`. + - **v2 schedule tool and scheduled tasks**: when the voice worker's brain sets a schedule backend (`turn_state.set_schedule`, every voice and chat reply), `schedule` is in the catalogue for the router (#7) and `prepare_intent()` (not force-offered every turn); without a backend it is not in the catalogue at all ([spec](src/jarvis/tools/builtin/schedule.spec.md)). A scheduled `task` the app fires is one more run of this loop (`reply` `via: "task"`, at the task's time, in the worker's chat lane, waiting for LLM switches like any reply): the asker's history scope, a fresh short-term memory, no prepared pick, no chat or schedule backend, `mcps={}` and `disabled_tools` widened to everything but `TASK_TOOLS` (`getWeather`, `webSearch`, `fetchWebPage`, `fetchMeals`, `toolSearchTool`), so the router, planner and loop see only those. The app waits at most 300 s for its answer and delivers it (`app/src/admin.spec.md`, "Schedules"). Eval: `evals/test_schedule_tool.py`. - Tool schema: native via `generate_tools_json_schema()` ([src/jarvis/tools/registry.py](src/jarvis/tools/registry.py)) or text fallback via `_text_tool_call_guidance()` ([engine.py:68](src/jarvis/reply/engine.py:68)) - Tool results from prior turns (raw or digested — see #5) - **Output**: OpenAI-style `{content, tool_calls, thinking}`. Consumed by the tool orchestrator and TTS pipeline. Natural-language content is delivered immediately; no post-turn evaluator runs. Spoken-answer length: the persona (`system_prompt.py`) and `voice_style` (`prompts/system.py`) both constrain the reply to a SINGLE sentence — any dry aside must fold into that one sentence as a trailing clause, never a second sentence. This keeps TTS latency down (synth time scales with text length) and matches the `agents/llm.md` operator instruction. diff --git a/docs/v2/proactive.md b/docs/v2/proactive.md index 1d5d57d..f952a56 100644 --- a/docs/v2/proactive.md +++ b/docs/v2/proactive.md @@ -1,6 +1,12 @@ -# 먼저 챙기는 비서: OpenAI dots 참고안 (2026-11-14, 제안 단계) +# 먼저 챙기는 비서: OpenAI dots 참고안 (2026-11-14) -사용자 요청으로 OpenAI dots(2026-09-29 발표)와 비슷한 오픈소스 프로젝트를 살펴보고, Jarvis에 가져올 만한 것을 골랐습니다. 아래 "제안"은 사용자가 정해 주기 전까지 만들지 않습니다. 정해진 것은 해당 문서(conversation.md, pc-features.md, 스펙 파일)로 옮깁니다. +사용자 요청으로 OpenAI dots(2026-09-29 발표)와 비슷한 오픈소스 프로젝트를 살펴보고, Jarvis에 가져올 만한 것을 골랐습니다. 사용자가 다섯 가지 모두 추천 순서대로, 아래 기본값으로 만들기로 정했습니다(2026-11-14, [decisions.md](decisions.md)). 만든 것은 해당 스펙 파일로 옮깁니다. + +진행 상황: + +- 1 예약 알림과 정기 작업: 구현(2026-11-14). 스펙은 [schedule.spec.md](../../src/jarvis/tools/builtin/schedule.spec.md)(두뇌 도구), [admin.spec.md](../../app/src/admin.spec.md) "Schedules"(저장·시각·전달·예약 화면), [voice_worker.spec.md](../../src/voice_worker/voice_worker.spec.md) "Schedules". 음성으로 맡긴 사람이 음성 채널에 없을 때 보낼 곳은 사람마다 고르지 않고 관리 앱에서 채팅 채널 하나를 정합니다. 정기 작업이 쓸 수 있는 도구에 "수집 결과 읽기"는 아직 없습니다(두뇌가 수집 결과를 읽는 도구가 없음, 제안 2 때 함께 봄). 평가 `evals/test_schedule_tool.py`를 이 서버의 qwen3:8b(실제 모델과 같은 계열)로 v2 설정(짧은 지시문, 의도 판단, 한국어 답)에서 세 번: "5분 타이머"(전에는 도구 없이 "설정했습니다"라고만 답함), "내일 7시 회의 알림", "예약 목록"은 9번 모두 통과했고 알림 문구도 한국어였습니다. "평일마다 아침 9시에 서울 날씨"는 3번 중 1번만 작업·평일로 맞게 예약했고, 나머지는 반복을 평일로 두지 않거나 지금 날씨를 바로 답했습니다. 실제 모델(Qwen3.6-35B-A3B, 게임PC)로는 사용자가 알려 주는 GPU 시간에 돌립니다. +- 3 행동별 세 단계 권한: M7 PC 제어와 함께 만듦. 시킬 수 있는 사람은 관리 앱에 아이디로 등록한 사람만. +- 5, 4, 2: 그다음 차례. ## dots가 하는 일 (공개 자료 기준) diff --git a/evals/test_schedule_tool.py b/evals/test_schedule_tool.py new file mode 100644 index 0000000..b8a0eca --- /dev/null +++ b/evals/test_schedule_tool.py @@ -0,0 +1,99 @@ +""" +Eval: the model uses the v2 schedule tool for reminders, timers and scheduled tasks instead of +claiming it set them (src/jarvis/tools/builtin/schedule.spec.md). + +Before the tool existed the fast model answered "5분 타이머 맞춰 줘" with "설정했습니다" and nothing +was set (docs/v2/decisions.md, 도구 호출). The schedule backend is a fake that keeps what it is +given; the tool itself runs for real. Cases: + +- a timer in minutes is a reminder about that many minutes from now, +- "tomorrow at 7" is a one-off reminder at 07:00 tomorrow, local time, +- "every weekday at 9, tell me the weather" is a weekday task at 09:00, +- asking what is scheduled lists, and adds nothing, +- with the v2 brain's reply language (Korean) the reminder text is Korean too: it is delivered as it is. + +Run: EVAL_JUDGE_MODEL=qwen2.5:7b python -m pytest evals/test_schedule_tool.py -v +(v2 brain setup: EVAL_COMPACT_PROMPT=1 EVAL_INTENT_JUDGE=1) +""" + +import time +from datetime import datetime, timedelta + +import pytest + +from conftest import requires_judge_llm + + +class FakeSchedules: + def __init__(self): + self.acts = [] + + def act(self, action, **kw): + self.acts.append((action, kw)) + if action == "add": + return {"ok": True, "item": {"id": str(len(self.acts)), "next": kw["at"], **kw}} + if action == "list": + return {"ok": True, "items": [{"id": "1", "kind": "remind", "text": "회의", "repeat": "none", + "next": time.time() + 3600}]} + return {"ok": True, "item": {"id": kw.get("schedule"), "kind": "remind", "text": "회의", + "repeat": "none", "next": time.time() + 3600}} + + def adds(self): + return [kw for a, kw in self.acts if a == "add"] + + +def _run(mock_config, eval_db, eval_dialogue_memory, query): + from helpers import JUDGE_BASE_URL, JUDGE_MODEL + from jarvis.memory.history import HistoryContext + from jarvis.reply import turn_state + from jarvis.reply.engine import run_reply_engine + + mock_config.ollama_base_url = JUDGE_BASE_URL + mock_config.ollama_chat_model = JUDGE_MODEL + sched = FakeSchedules() + turn_state.reset() + turn_state.set_user("100") + turn_state.set_schedule(sched) + try: + reply = run_reply_engine(db=eval_db, cfg=mock_config, tts=None, text=query, + dialogue_memory=eval_dialogue_memory, history=HistoryContext(), + legacy_memory=False, default_language="Korean") + finally: + turn_state.reset() + print(f"\n schedule [{JUDGE_MODEL}] {query!r}: acts={sched.acts} reply={(reply or '')[:200]!r}") + return sched, reply + + +@pytest.mark.eval +@requires_judge_llm +class TestScheduleTool: + def test_a_timer_is_scheduled_not_claimed(self, mock_config, eval_db, eval_dialogue_memory): + before = time.time() + sched, _ = _run(mock_config, eval_db, eval_dialogue_memory, "5분 타이머 맞춰 줘") + adds = sched.adds() + assert adds, sched.acts + assert before + 4 * 60 <= adds[0]["at"] <= time.time() + 6 * 60, adds + assert adds[0]["repeat"] == "none" + text = adds[0]["text"] + assert any("\uac00" <= c <= "\ud7a3" for c in text) and not any("\u4e00" <= c <= "\u9fff" for c in text), text + + def test_a_reminder_tomorrow_at_seven(self, mock_config, eval_db, eval_dialogue_memory): + sched, _ = _run(mock_config, eval_db, eval_dialogue_memory, "내일 아침 7시에 회의 있다고 알려 줘") + adds = sched.adds() + assert adds, sched.acts + want = (datetime.now() + timedelta(days=1)).replace(hour=7, minute=0, second=0, microsecond=0) + assert adds[0]["at"] == want.timestamp(), (datetime.fromtimestamp(adds[0]["at"]), want) + assert adds[0]["kind"] == "remind" and adds[0]["repeat"] == "none" + + def test_a_weekday_weather_task(self, mock_config, eval_db, eval_dialogue_memory): + sched, _ = _run(mock_config, eval_db, eval_dialogue_memory, "평일마다 아침 9시에 서울 날씨 알려 줘") + adds = sched.adds() + assert adds, sched.acts + at = datetime.fromtimestamp(adds[0]["at"]) + assert (at.hour, at.minute) == (9, 0), at + assert adds[0]["kind"] == "task" and adds[0]["repeat"] == "weekdays", adds + + def test_asking_what_is_scheduled_lists(self, mock_config, eval_db, eval_dialogue_memory): + sched, _ = _run(mock_config, eval_db, eval_dialogue_memory, "내가 예약해 둔 알림 뭐 있어?") + assert any(a == "list" for a, _ in sched.acts), sched.acts + assert not sched.adds(), sched.acts diff --git a/src/jarvis/reply/engine.py b/src/jarvis/reply/engine.py index ed48974..19d98d3 100644 --- a/src/jarvis/reply/engine.py +++ b/src/jarvis/reply/engine.py @@ -1051,10 +1051,12 @@ def _recent_messages(cfg, dialogue_memory) -> list: return dialogue_memory.get_recent_messages() -def _offered_builtins(cfg, chat_available: bool) -> dict: - """The built-in tools this turn can offer: the enabled ones (``enabled_builtin_tools``), and - chatChannels only with a chat backend (chat_channels.spec.md).""" - return {k: v for k, v in enabled_builtin_tools(cfg).items() if k != "chatChannels" or chat_available} +def _offered_builtins(cfg, chat_available: bool, schedule_available: bool = False) -> dict: + """The built-in tools this turn can offer: the enabled ones (``enabled_builtin_tools``), + chatChannels only with a chat backend (chat_channels.spec.md) and schedule only with a + schedule backend (schedule.spec.md).""" + return {k: v for k, v in enabled_builtin_tools(cfg).items() + if (k != "chatChannels" or chat_available) and (k != "schedule" or schedule_available)} def _route_tools(cfg, redacted: str, builtin_tools: dict, mcp_tools: dict, recent_messages: list): @@ -1077,12 +1079,14 @@ def _route_tools(cfg, redacted: str, builtin_tools: dict, mcp_tools: dict, recen ) -def prepare_intent(cfg, text: str, dialogue_memory, chat_available: bool) -> Optional[list]: +def prepare_intent(cfg, text: str, dialogue_memory, chat_available: bool, + schedule_available: bool = False) -> Optional[list]: """Run the intent judge (the tool router) for ``text`` ahead of its reply (v2: on a partial transcript while the user still speaks), with the inputs ``run_reply_engine(intent_judge=True)`` would give it, so its pick can be passed back as ``prepared_tools``. None when the router gave up (its fall-open to the whole catalogue is not a pick). ``chat_available``: the reply will - have a chat backend (the chatChannels tool is in the catalogue).""" + have a chat backend (the chatChannels tool is in the catalogue); ``schedule_available``: a + schedule backend (the schedule tool is).""" mcp_tools: dict = {} if getattr(cfg, "mcps", {}): try: @@ -1090,7 +1094,7 @@ def prepare_intent(cfg, text: str, dialogue_memory, chat_available: bool) -> Opt mcp_tools = get_cached_mcp_tools() except Exception: mcp_tools = {} - builtin = _offered_builtins(cfg, chat_available) + builtin = _offered_builtins(cfg, chat_available, schedule_available) try: routed = _route_tools(cfg, redact(text), builtin, mcp_tools, _recent_messages(cfg, dialogue_memory)) except Exception as exc: # pragma: no cover — defensive @@ -1230,9 +1234,9 @@ def run_reply_engine(db: "Database", cfg, tts: Optional[Any], # needed. Both cases are preserved for the engine to distinguish. # chatChannels exists only when the v2 brain gave this turn a chat backend # (chat_channels.spec.md); otherwise it is not in the catalogue at all. - from .turn_state import get_chat as _get_chat + from .turn_state import get_chat as _get_chat, get_schedule as _get_schedule _chat_backend = _get_chat() - _builtin_tools = _offered_builtins(cfg, _chat_backend is not None) + _builtin_tools = _offered_builtins(cfg, _chat_backend is not None, _get_schedule() is not None) _all_builtin_names = list(_builtin_tools.keys()) _all_mcp_names = list(mcp_tools.keys()) _full_catalog_names = _all_builtin_names + _all_mcp_names diff --git a/src/jarvis/reply/turn_state.py b/src/jarvis/reply/turn_state.py index 92dc296..ef2dd33 100644 --- a/src/jarvis/reply/turn_state.py +++ b/src/jarvis/reply/turn_state.py @@ -22,6 +22,9 @@ user : Optional[str] chat : Optional[object] v2: the chat channel backend for this turn (chat_channels.spec.md). None means the chatChannels tool is not offered. +schedule : Optional[object] + v2: the asker's reminders and scheduled tasks for this turn (schedule.spec.md). None means the + schedule tool is not offered (v1, evals, and scheduled tasks themselves). """ from __future__ import annotations @@ -38,6 +41,15 @@ def reset() -> None: _state.broadcast_action = None _state.user = None _state.chat = None + _state.schedule = None + + +def set_schedule(backend) -> None: + _state.schedule = backend + + +def get_schedule(): + return getattr(_state, "schedule", None) def set_chat(backend) -> None: diff --git a/src/jarvis/tools/builtin/schedule.py b/src/jarvis/tools/builtin/schedule.py new file mode 100644 index 0000000..b06f888 --- /dev/null +++ b/src/jarvis/tools/builtin/schedule.py @@ -0,0 +1,145 @@ +"""schedule: reminders and scheduled tasks of the person asking (v2). Contract: schedule.spec.md.""" + +from __future__ import annotations + +import re +import time +from datetime import datetime +from typing import Any, Dict, Optional + +from ..base import Tool, ToolContext +from ..types import ToolExecutionResult +from ...debug import debug_log + +_ACTIONS = ("add", "list", "delete") +_KINDS = ("remind", "task") +_REPEATS = ("none", "daily", "weekdays", "weekly") +_FORMATS = ("%Y-%m-%d %H:%M", "%Y-%m-%dT%H:%M", "%Y-%m-%d %H:%M:%S", "%Y-%m-%dT%H:%M:%S") +MAX_TEXT_CHARS = 500 + + +def _fail(text: str) -> ToolExecutionResult: + return ToolExecutionResult(success=False, reply_text=f"Error: {text}", error_message=text) + + +def _ok(text: str) -> ToolExecutionResult: + return ToolExecutionResult(success=True, reply_text=text) + + +def _word(raw: Any, words: tuple) -> str: + """The first known word, also from planner forms like "add_reminder" or "delete schedule".""" + return next((w for w in re.findall(r"[a-z]+", str(raw or "").lower()) if w in words), "") + + +def _local(ts: float) -> str: + return time.strftime("%Y-%m-%d %H:%M (%a)", time.localtime(ts)) + + +def _when(args: dict, now: float) -> Optional[float]: + """The first time as a Unix time: ``in_minutes`` from now, or ``at`` in this PC's local time.""" + minutes = args.get("in_minutes") + if minutes not in (None, ""): + try: + m = float(minutes) + except (TypeError, ValueError): + return None + return now + m * 60 if m > 0 else None + at = str(args.get("at") or "").strip() + for fmt in _FORMATS: + try: + return datetime.strptime(at, fmt).timestamp() + except ValueError: + continue + return None + + +def _line(item: dict) -> str: + repeat = item.get("repeat") or "none" + return (f"- id {item.get('id')}: {item.get('kind')} at {_local(float(item.get('next') or 0))}" + f"{'' if repeat == 'none' else f', repeats {repeat}'}: {item.get('text')}") + + +class ScheduleTool(Tool): + @property + def name(self) -> str: + return "schedule" + + @property + def description(self) -> str: + return ( + "Reminders, timers and scheduled tasks of the person asking. Call it whenever they ask to be " + "reminded or told something later, to set a timer or alarm, to have something checked at a " + "time or regularly (e.g. every morning), or to see or cancel what they scheduled. " + "kind='remind' delivers a fixed message at that time; when what they want to hear then " + "changes over time (weather, news, prices, a web search), use kind='task': text is looked " + "up at that time and they hear the result. When they ask for it more than once, set repeat " + "to match exactly (weekdays is Monday to Friday only). Write text in the language the " + "person speaks. " + "Never say something was scheduled unless this tool said so." + ) + + @property + def inputSchema(self) -> Dict[str, Any]: + return { + "type": "object", + "properties": { + "action": {"type": "string", "enum": list(_ACTIONS)}, + "kind": {"type": "string", "enum": list(_KINDS), + "description": "remind: a fixed message; task: look something up at that time"}, + "text": {"type": "string", + "description": "remind: the message to tell them; task: what to look up and tell"}, + "at": {"type": "string", "description": "Local time YYYY-MM-DD HH:MM"}, + "in_minutes": {"type": "number", "description": "Minutes from now, instead of at"}, + "repeat": {"type": "string", "enum": list(_REPEATS), + "description": "daily: every day; weekdays: Monday to Friday; weekly: once a week"}, + "id": {"type": "string", "description": "delete: the id from list"}, + }, + "required": ["action"], + } + + def run(self, args: Optional[Dict[str, Any]], context: ToolContext) -> ToolExecutionResult: + from ...reply.turn_state import get_schedule + + backend = get_schedule() + if backend is None: + return _fail("scheduling is not available here") + args = args if isinstance(args, dict) else {} + action = _word(args.get("action"), _ACTIONS) + if not action: + return _fail(f"action must be one of {', '.join(_ACTIONS)}") + debug_log(f"schedule {action}", "tools") + + if action == "list": + result = backend.act("list") or {} + if not result.get("ok"): + return _fail(str(result.get("error") or "the list could not be read")) + items = result.get("items") or [] + return _ok(("Scheduled for this person:\n" + "\n".join(_line(i) for i in items)) if items + else "Nothing is scheduled for this person.") + + if action == "delete": + sid = str(args.get("id") or "").strip() + if not sid: + return _fail("delete needs id (call action='list' first)") + result = backend.act("delete", schedule=sid) or {} + if not result.get("ok"): + return _fail(str(result.get("error") or "not deleted")) + return _ok(f"Deleted: {_line(result['item'])}" if result.get("item") else "Deleted.") + + text = " ".join(str(args.get("text") or "").split()) + if not text: + return _fail("add needs text") + if len(text) > MAX_TEXT_CHARS: + return _fail(f"text must be at most {MAX_TEXT_CHARS} characters") + kind = _word(args.get("kind"), _KINDS) or "remind" + repeat = _word(args.get("repeat"), _REPEATS) or "none" + now = time.time() + at = _when(args, now) + if at is None: + return _fail("add needs at (local time YYYY-MM-DD HH:MM) or in_minutes above 0") + if repeat == "none" and at <= now: + return _fail(f"{_local(at)} has already passed (now {_local(now)})") + result = backend.act("add", kind=kind, text=text, at=at, repeat=repeat) or {} + if not result.get("ok"): + return _fail(str(result.get("error") or "not scheduled")) + return _ok(f"Scheduled: {_line(result['item'])}") diff --git a/src/jarvis/tools/builtin/schedule.spec.md b/src/jarvis/tools/builtin/schedule.spec.md new file mode 100644 index 0000000..2b51f73 --- /dev/null +++ b/src/jarvis/tools/builtin/schedule.spec.md @@ -0,0 +1,49 @@ +# schedule tool spec (v2) + +Reminders, timers and scheduled tasks of the person asking. Product rules: `docs/v2/proactive.md` +(제안 1). The app keeps the schedules, fires them and delivers them (`app/src/admin.spec.md`, +"Schedules"); this tool only asks it to add, list or delete. + +## Principles + +- **Only in v2, only the asker's own.** The tool exists for a turn only when the v2 voice worker's + brain put a schedule backend in the turn state (`turn_state.set_schedule`). Without one (v1, evals + without a backend, and scheduled tasks themselves) it is not offered and refuses. The backend acts + for the person asking only: they list and delete only what they scheduled themselves. +- **Never claimed without the tool.** The description tells the model to call it for reminders, + timers, alarms and things to check at a time or regularly, and never to say something was + scheduled unless the tool said so (the "5분 타이머" confabulation, `docs/v2/decisions.md`). +- **Tasks only look things up.** A `task` runs at its time with nobody there to ask, so it gets the + read-only tools only (voice worker `TASK_TOOLS`: weather, web search, reading a page, the asker's + meals, the tool search) and no MCP tools. It cannot send, delete, schedule or operate anything. +- **Raw data.** The tool returns plain text for the reply model; no LLM call inside. + +## Schema + +| action | needs | does | +|---|---|---| +| `add` | `text`, `at` or `in_minutes` (`kind`, `repeat`) | schedules it; returns its id, kind, next time and repeat | +| `list` | | the asker's schedules, soonest first | +| `delete` | `id` | deletes one of the asker's schedules | + +- `kind`: `remind` (default) tells `text` at the time; `task` does `text` at the time and tells the + result. +- `at`: local time of this PC, `YYYY-MM-DD HH:MM` (a `T` between and seconds are accepted). + `in_minutes`: minutes from now (above 0), used instead of `at` when given; it spares the model + the arithmetic for timers. +- `repeat`: `none` (default), `daily`, `weekdays` (Monday to Friday), `weekly` (the weekday of `at`). + A time that has passed is refused unless it repeats (the app then takes the next one). +- `text`: whitespace collapsed, 1-500 characters. +- `action`, `kind` and `repeat` are read as their first known word, so planner forms like + `add_reminder` work. A refused add never reaches the app. +- Lines: `- id : at YYYY-MM-DD HH:MM ()[, repeats ]: `. + +## Backend + +`turn_state.get_schedule()` returns an object with `act(action, **fields) -> dict` (add: `kind`, +`text`, `at`, `repeat`; delete: `schedule`, the schedule's id): `{"ok": true, +"item": {...}}` for add and delete, `{"ok": true, "items": [...]}` for list, `{"ok": false, "error": +"..."}` on failure. Items carry `id`, `kind`, `text`, `next` (Unix time) and `repeat`. The voice +worker implements it (`voice_worker/schedule_bridge.py`): each action goes to the app as +`schedule_action` with the asker and where they asked (voice_worker.spec.md, "Schedules"). +Failures come back as `Error: ...` text with `success=False`. diff --git a/src/jarvis/tools/registry.py b/src/jarvis/tools/registry.py index 99ecd30..1cd2c36 100644 --- a/src/jarvis/tools/registry.py +++ b/src/jarvis/tools/registry.py @@ -17,6 +17,7 @@ from .builtin.nutrition.log_meal import LogMealTool from .builtin.nutrition.fetch_meals import FetchMealsTool from .builtin.nutrition.delete_meal import DeleteMealTool from .builtin.chat_channels import ChatChannelsTool +from .builtin.schedule import ScheduleTool from .builtin.bot_browser import BotBrowserTool, CDP_ENV from .builtin.refresh_mcp_tools import RefreshMCPToolsTool from .builtin.weather import WeatherTool @@ -48,6 +49,7 @@ BUILTIN_TOOLS = { "controlBrowser": ControlBrowserTool(), "setBroadcast": SetBroadcastTool(), "chatChannels": ChatChannelsTool(), + "schedule": ScheduleTool(), "botBrowser": BotBrowserTool(), } diff --git a/src/voice_worker/brain.py b/src/voice_worker/brain.py index ff4a9d3..fe23bb3 100644 --- a/src/voice_worker/brain.py +++ b/src/voice_worker/brain.py @@ -17,10 +17,13 @@ from jarvis.memory.history import HistoryContext, Scope class Brain(Protocol): - def reply(self, user: str, text: str, history: HistoryContext, chat=None) -> Optional[str]: + def reply(self, user: str, text: str, history: HistoryContext, chat=None, schedule=None, + task: bool = False) -> Optional[str]: """Blocking; called on a worker thread, one call per person at a time (a voice and a chat reply for different people may overlap). ``chat``: the chat backend for - the chatChannels tool (voice_worker/chat_bridge.py), or None without registered channels.""" + the chatChannels tool (voice_worker/chat_bridge.py), or None without registered channels. + ``schedule``: the schedule backend for the schedule tool (voice_worker/schedule_bridge.py). + ``task``: a scheduled task runs, with the read-only tools only (``TASK_TOOLS``).""" def addressed(self, user: str, text: str, names: list[str]) -> Optional[bool]: """Blocking: was ``text`` said to the assistant (called ``names``)? None when it could not tell. @@ -150,6 +153,9 @@ MCP_ENV_VAR = "JARVIS_MCP_ENV" # voice channel or a registered chat channel asks, so they are never offered (M7 brings screen # viewing with its own permissions). V2_HOST_TOOLS = ("localFiles", "screenshot") +# The only tools a scheduled task may use: it runs with nobody there to ask, so it only looks +# things up (schedule.spec.md, "Tasks"). No MCP tools either: what they do is not known. +TASK_TOOLS = ("getWeather", "webSearch", "fetchWebPage", "fetchMeals", "toolSearchTool") def take_mcp_env() -> dict: @@ -261,7 +267,7 @@ class JarvisBrain: self._prepared[user] = p t0 = time.perf_counter() try: - p.tools = Muted.run(prepare_intent, self.cfg, text, mem, chat) + p.tools = Muted.run(prepare_intent, self.cfg, text, mem, chat, schedule_available=True) finally: p.done.set() debug_log(f"brain prepare user={user} picked={p.tools is not None} " @@ -277,21 +283,34 @@ class JarvisBrain: return None return p.tools - def reply(self, user: str, text: str, history: HistoryContext, chat=None) -> Optional[str]: + def _task_cfg(self): + from jarvis.tools.registry import BUILTIN_TOOLS + + off = (*self.cfg.disabled_tools, *(t for t in BUILTIN_TOOLS if t not in TASK_TOOLS)) + return dataclasses.replace(self.cfg, disabled_tools=tuple(dict.fromkeys(off)), mcps={}) + + def reply(self, user: str, text: str, history: HistoryContext, chat=None, schedule=None, + task: bool = False) -> Optional[str]: + from jarvis.memory.conversation import DialogueMemory from jarvis.reply import turn_state from jarvis.reply.engine import run_reply_engine t0 = time.perf_counter() - prepared = self._prepared_tools(user, text) + # A task has its own short-term memory: it is not a turn of the person's conversation. + prepared = None if task else self._prepared_tools(user, text) + cfg = self._task_cfg() if task else self.cfg + memory = DialogueMemory(inactivity_timeout=self.cfg.dialogue_memory_timeout, max_interactions=20) \ + if task else self._memory(user) # legacy_memory=False: v1's profile, diary and graph belong to one user; several people # share this assistant, so only the asker's scoped history may reach the prompt. def turn(): # Tools with personal data (meals) read the asker from the turn state. turn_state.reset() turn_state.set_user(user) - turn_state.set_chat(chat) + turn_state.set_chat(None if task else chat) + turn_state.set_schedule(None if task else schedule) try: - return run_reply_engine(self.db, self.cfg, None, text, self._memory(user), + return run_reply_engine(self.db, cfg, None, text, memory, language=self.language, history=history, legacy_memory=False, default_language=self.reply_language, compact_prompt=True, intent_judge=True, prepared_tools=prepared) @@ -299,7 +318,7 @@ class JarvisBrain: turn_state.reset() answer = Muted.run(turn) - debug_log(f"brain reply user={user} ok={bool(answer)} prepared={prepared is not None} " + debug_log(f"brain reply user={user} ok={bool(answer)} task={task} prepared={prepared is not None} " f"ms={(time.perf_counter() - t0) * 1000:.0f}", "llm") return answer diff --git a/src/voice_worker/schedule_bridge.py b/src/voice_worker/schedule_bridge.py new file mode 100644 index 0000000..aba7b8b --- /dev/null +++ b/src/voice_worker/schedule_bridge.py @@ -0,0 +1,27 @@ +"""The schedule backend the brain's schedule tool uses during one reply (schedule.spec.md). + +The app keeps the schedules: every action goes to it as ``schedule_action`` for the person asking +and waits for its ``schedule_action_result``. Built per reply, on the event loop; ``act`` is called +from the brain's thread. +""" + +from __future__ import annotations + +import asyncio +from typing import Any, Optional + + +class ScheduleBridge: + def __init__(self, session, loop: asyncio.AbstractEventLoop, user: str, via: str, channel: Optional[str]): + self._session = session + self._loop = loop + # Who asked and where: the app delivers there (voice, or the chat channel asked in). + self._origin = {"user": user, "via": via, **({"channel": channel} if channel else {})} + + def act(self, action: str, **kw: Any) -> dict: + fut = asyncio.run_coroutine_threadsafe( + self._session.schedule_action(action, {**self._origin, **kw}), self._loop) + try: + return fut.result(self._session.worker.chat_timeout_s + 5) + except Exception as e: # loop gone or the wait broke + return {"ok": False, "error": f"the app did not answer ({type(e).__name__})"} diff --git a/src/voice_worker/server.py b/src/voice_worker/server.py index a63ff91..c3a7514 100644 --- a/src/voice_worker/server.py +++ b/src/voice_worker/server.py @@ -347,6 +347,7 @@ class Session: "chat_delete": self._on_chat_delete, "reply": self._on_reply, "addressed": self._on_addressed, "rank": self._on_rank, "chat_channels": self._on_chat_channels, "chat_action_result": self._on_chat_action_result, + "schedule_action_result": self._on_schedule_action_result, "memory_config": self._on_memory_config, "reply_delivered": self._on_reply_delivered, "prepare": self._on_prepare, "llm": self._on_llm}.get(kind) if handler is None: @@ -515,6 +516,34 @@ class Session: result["error"] = str(msg.get("error") or "the app could not do it") fut.set_result(result) + async def schedule_action(self, action: str, args: dict) -> dict: + """Ask the app to add, list or delete a schedule and wait for its answer (or a timeout).""" + aid = f"{self.worker.chat_action_prefix}-{self.sid}-{next(self._chat_ids)}" + fut = asyncio.get_running_loop().create_future() + self.chat_waiting[aid] = fut + self.send({**args, "type": "schedule_action", "id": aid, "action": action}) + debug_log(f"schedule action {aid} {action}", "voice_worker") + try: + return await asyncio.wait_for(fut, self.worker.chat_timeout_s) + except asyncio.TimeoutError: + return {"ok": False, "error": "the app did not answer in time"} + finally: + self.chat_waiting.pop(aid, None) + + def _on_schedule_action_result(self, msg: dict) -> None: + fut = self.chat_waiting.get(msg.get("id")) + if fut is None or fut.done(): + return + if msg.get("ok") is not True: + fut.set_result({"ok": False, "error": str(msg.get("error") or "the app could not do it")}) + return + result = {"ok": True} + if isinstance(msg.get("item"), dict): + result["item"] = msg["item"] + if isinstance(msg.get("items"), list): + result["items"] = [i for i in msg["items"] if isinstance(i, dict)] + fut.set_result(result) + def _on_memory_config(self, msg: dict) -> None: changes = {k: v for k, v in msg.items() if k != "type"} try: @@ -529,14 +558,18 @@ class Session: if got is None: return rid, user, text = got - via, speak = msg.get("via"), msg.get("speak", False) - if via not in ("voice", "chat") or not isinstance(speak, bool) or len(rid.encode()) > 255: - self.error("bad_message", "reply needs via voice|chat, a boolean speak and an id under 256 bytes", - id=rid) + via, speak, channel = msg.get("via"), msg.get("speak", False), msg.get("channel") + if via not in ("voice", "chat", "task") or not isinstance(speak, bool) or len(rid.encode()) > 255 \ + or (via == "task" and speak) or not (channel is None or isinstance(channel, str)): + self.error("bad_message", "reply needs via voice|chat|task, a boolean speak (false for a task), " + "an optional string channel and an id under 256 bytes", id=rid) return if self.worker.brain is None: self.error("brain_unavailable", "the worker was started without --brain", id=rid) return + if via == "task": + asyncio.get_running_loop().create_task(self._task(rid, user, text)) + return if speak: self.thinking_replies.add(rid) if user in self.preparing: @@ -545,7 +578,33 @@ class Session: waiting = self.worker.replies_unanswered early = waiting.get(user, 0) > 0 # sent before their previous answer: not a reply to it waiting[user] = waiting.get(user, 0) + 1 - asyncio.get_running_loop().create_task(self._reply(rid, user, text, via, speak, early)) + asyncio.get_running_loop().create_task(self._reply(rid, user, text, via, speak, early, channel)) + + async def _task(self, rid: str, user: str, text: str) -> None: + """A scheduled task (schedule.spec.md, "Tasks"): answered in the chat lane with the read-only + tools, its own short-term memory and the person's history scope. It is not a turn of their + conversation, so their turn count and delete confirmations stay as they are.""" + w = self.worker + answer = None + async with w.brain_people.setdefault(user, asyncio.Lock()), w.brain_lanes["chat"]: + if self.closed: + return + now = time.time() + if w.history is not None: + m = self.memory + history = await asyncio.to_thread( + w.history.context, user, text, now, m.scope(now), minutes=m.recent_minutes, + max_records=m.recent_records, limit=m.matches, budget_tokens=m.budget_tokens) + else: + from jarvis.memory.history import HistoryContext + history = HistoryContext() + try: + async with self._llm(): + answer = await asyncio.to_thread(w.brain.reply, user, text, history, task=True) + except Exception as e: # the brain must never take the session down + debug_log(f"brain task {rid} failed: {type(e).__name__}", "llm") + answer = (answer or "").strip() + self.send({"type": "reply_text", "id": rid, "text": answer, "failed": not answer}) def _on_llm(self, msg: dict) -> None: state, notice, sid, game = msg.get("state"), msg.get("notice"), msg.get("id"), msg.get("game", False) @@ -682,9 +741,10 @@ class Session: self.send({"type": "rank_result", "id": qid, "order": order or [], "failed": order is None}) asyncio.get_running_loop().create_task(rank()) - async def _reply(self, rid: str, user: str, text: str, via: str, speak: bool, early: bool = False) -> None: + async def _reply(self, rid: str, user: str, text: str, via: str, speak: bool, early: bool = False, + channel: str | None = None) -> None: try: - await self._reply_inner(rid, user, text, via, speak, early) + await self._reply_inner(rid, user, text, via, speak, early, channel) finally: self.thinking_replies.discard(rid) self.cancelled_replies.discard(rid) @@ -721,7 +781,8 @@ class Session: if got is not None and got[2] is not None: # only chat answers wait for the app's report self._delivered(rid, ok) - async def _reply_inner(self, rid: str, user: str, text: str, via: str, speak: bool, early: bool) -> None: + async def _reply_inner(self, rid: str, user: str, text: str, via: str, speak: bool, early: bool, + chat_channel: str | None = None) -> None: w = self.worker channel = self.voice_channel # Person first, then lane: a reply waiting for its asker's other answer holds no lane. @@ -744,11 +805,15 @@ class Session: from .chat_bridge import ChatBridge chat = ChatBridge(self, asyncio.get_running_loop(), turn, w.pending_deletes.setdefault(user, {})) \ if self.chat_channels else None + from .schedule_bridge import ScheduleBridge + schedule = ScheduleBridge(self, asyncio.get_running_loop(), user, via, + chat_channel if via == "chat" else None) try: async with self._llm(rid if speak else None): w.answering += 1 try: - answer = await asyncio.to_thread(w.brain.reply, user, text, history, chat=chat) + answer = await asyncio.to_thread(w.brain.reply, user, text, history, chat=chat, + schedule=schedule) finally: w.answering -= 1 if not w.answering and w.scheduler: diff --git a/src/voice_worker/voice_worker.spec.md b/src/voice_worker/voice_worker.spec.md index 73b6604..8882273 100644 --- a/src/voice_worker/voice_worker.spec.md +++ b/src/voice_worker/voice_worker.spec.md @@ -200,6 +200,7 @@ which carry Discord's own time. -> {"type": "chat_edit", "channel": "...", "message_id": "...", "text": "..."} -> {"type": "chat_delete", "channel": "...", "message_id": "..."} -> {"type": "reply", "id": "r1", "user": "", "text": "...", "via": "voice", "speak": true} + // via chat: optional "channel" it was written in <- {"type": "reply_text", "id": "r1", "text": "...", "failed": false} -> {"type": "reply_delivered", "id": "c1", "ok": true} // a chat answer was posted (ok: false: it was not) -> {"type": "prepare", "user": "", "text": "..."} // a call still being said; nothing comes back @@ -318,6 +319,33 @@ which carry Discord's own time. `budget_tokens` (4000). - Logs carry IDs, counts and timings only, never record, question or answer text. +### Schedules + +The brain's schedule tool (`src/jarvis/tools/builtin/schedule.spec.md`) asks the app, which keeps +the schedules (`app/src/admin.spec.md`, "Schedules"): + +``` +<- {"type": "schedule_action", "id": "s1", "action": "add|list|delete", "user": "", + "via": "voice|chat", "channel": "", // channel: only for via chat, when given + "kind": "remind|task", "text": "...", "at": 1762830000.0, "repeat": "none|daily|weekdays|weekly", + "schedule": ""} // add: kind..repeat; delete: schedule +-> {"type": "schedule_action_result", "id": "s1", "ok": true, "item": {...}} // add, delete +-> {"type": "schedule_action_result", "id": "s1", "ok": true, "items": [...]} // list +-> {"type": "schedule_action_result", "id": "s1", "ok": false, "error": "..."} +-> {"type": "reply", "id": "t1", "user": "", "text": "", "via": "task"} +<- {"type": "reply_text", "id": "t1", "text": "...", "failed": false} +``` + +- Every voice and chat reply gets a schedule backend for its asker, the reply's `via` and, for + chat, the `channel` the `reply` named. Ids and waiting are those of chat actions (unique across + connections and runs, 15 s, a closed session fails what waits). +- `via: "task"` runs a scheduled task the app fires: in the chat lane and one at a time with the + person's other replies, with their history scope, but its own short-term memory, no chat or + schedule backend and the read-only tools only (`brain.TASK_TOOLS`, no MCP tools). It is not a turn + of the person's conversation (their turn count and delete confirmations stay), is never spoken + (`speak: true` is `bad_message`), and its answer is not stored; the app delivers it. It waits for + LLM switches like any reply. + ### Background collection (M6) With `JARVIS_COLLECT_PATH` set, the worker collects on its own thread, apart from the voice and the diff --git a/tests/test_schedule_tool.py b/tests/test_schedule_tool.py new file mode 100644 index 0000000..e5e31bf --- /dev/null +++ b/tests/test_schedule_tool.py @@ -0,0 +1,109 @@ +"""The schedule tool (src/jarvis/tools/builtin/schedule.spec.md) against a fake app backend.""" + +from __future__ import annotations + +import time +from datetime import datetime, timedelta + +import pytest + +from jarvis.reply import turn_state +from jarvis.tools.builtin.schedule import ScheduleTool + + +class FakeApp: + """Keeps schedules as the app would for one person: add returns the item, list all, delete one.""" + + def __init__(self): + self.items, self.calls = [], [] + + def act(self, action, **kw): + self.calls.append((action, kw)) + if action == "add": + item = {"id": str(len(self.items) + 1), "next": kw["at"], **kw} + self.items.append(item) + return {"ok": True, "item": item} + if action == "list": + return {"ok": True, "items": list(self.items)} + found = [i for i in self.items if i["id"] == kw["schedule"]] + if not found: + return {"ok": False, "error": "no such schedule of yours"} + self.items.remove(found[0]) + return {"ok": True, "item": found[0]} + + +@pytest.fixture +def app(): + a = FakeApp() + turn_state.reset() + turn_state.set_schedule(a) + yield a + turn_state.reset() + + +def run(**args): + return ScheduleTool().run(args, None) + + +@pytest.mark.unit +def test_a_timer_in_minutes_is_a_reminder_that_many_minutes_from_now(app): + before = time.time() + r = run(action="add", text="라면 다 됐어요", in_minutes=5) + assert r.success + action, kw = app.calls[-1] + assert action == "add" and kw["kind"] == "remind" and kw["repeat"] == "none" and kw["text"] == "라면 다 됐어요" + assert before + 300 <= kw["at"] <= time.time() + 300 + assert "id 1" in r.reply_text + + +@pytest.mark.unit +def test_a_local_time_and_a_repeat_reach_the_app(app): + at = (datetime.now() + timedelta(days=1)).replace(hour=9, minute=0, second=0, microsecond=0) + r = run(action="add_task", kind="task", text="날씨 알려 줘", at=at.strftime("%Y-%m-%d %H:%M"), repeat="daily") + assert r.success + _, kw = app.calls[-1] + assert kw["kind"] == "task" and kw["repeat"] == "daily" and kw["at"] == at.timestamp() + assert "repeats daily" in r.reply_text + + +@pytest.mark.unit +@pytest.mark.parametrize("args", [ + {"text": "x"}, # no time + {"text": "x", "at": "tomorrow 7"}, # not a time + {"text": "x", "in_minutes": 0}, + {"text": "", "in_minutes": 5}, # nothing to say + {"text": "x" * 501, "in_minutes": 5}, + {"text": "x", "at": "2000-01-01 07:00"}, # passed, and not repeating +]) +def test_a_bad_add_is_refused_without_asking_the_app(app, args): + r = run(action="add", **args) + assert not r.success and r.reply_text.startswith("Error:") + assert app.calls == [] + + +@pytest.mark.unit +def test_list_and_delete_go_through_the_app(app): + run(action="add", text="회의", in_minutes=60) + listed = run(action="list") + assert listed.success and "회의" in listed.reply_text and "id 1" in listed.reply_text + assert run(action="delete", id="1").success + assert run(action="list").reply_text == "Nothing is scheduled for this person." + gone = run(action="delete", id="1") + assert not gone.success and "no such schedule" in gone.reply_text + + +@pytest.mark.unit +def test_without_a_backend_the_tool_refuses_and_is_not_offered(tmp_path, monkeypatch): + import json + from jarvis.config import load_settings + from jarvis.reply.engine import _offered_builtins + + turn_state.reset() + r = run(action="add", text="x", in_minutes=5) + assert not r.success + cfg = tmp_path / "config.json" + cfg.write_text(json.dumps({"db_path": str(tmp_path / "j.db")})) + monkeypatch.setenv("JARVIS_CONFIG_PATH", str(cfg)) + s = load_settings() + assert "schedule" not in _offered_builtins(s, chat_available=False) + assert "schedule" in _offered_builtins(s, chat_available=False, schedule_available=True) diff --git a/tests/test_voice_worker_brain.py b/tests/test_voice_worker_brain.py index acec860..a0a8f72 100644 --- a/tests/test_voice_worker_brain.py +++ b/tests/test_voice_worker_brain.py @@ -28,9 +28,9 @@ class FakeBrain: self.answer = answer self.calls = [] - def reply(self, user, text, history, chat=None): + def reply(self, user, text, history, chat=None, schedule=None, task=False): self.calls.append((user, text, history)) - self.chat = chat + self.chat, self.schedule, self.task = chat, schedule, task return self.answer(user, text) def addressed(self, user, text, names): @@ -1037,7 +1037,8 @@ def _jarvis_brain(tmp_path, monkeypatch, plans): monkeypatch.setenv("JARVIS_CONFIG_PATH", str(cfg)) seen = {"judged": [], "prepared": []} - def fake_prepare(cfg, text, mem, chat_available): + def fake_prepare(cfg, text, mem, chat_available, schedule_available=False): + assert schedule_available # the reply it is made for has the schedule tool seen["judged"].append((text, chat_available)) return plans.get(text) @@ -1095,7 +1096,7 @@ def test_a_reply_waits_for_a_pick_still_being_prepared_for_its_text(tmp_path, mo b, seen = _jarvis_brain(tmp_path, monkeypatch, {}) import jarvis.reply.engine as engine - def slow_prepare(cfg, text, mem, chat_available): + def slow_prepare(cfg, text, mem, chat_available, schedule_available=False): _time.sleep(0.4) return plan monkeypatch.setattr(engine, "prepare_intent", slow_prepare) @@ -1290,3 +1291,113 @@ def test_in_a_game_no_partials_are_transcribed_while_an_answer_is_written(store) assert first_partial is None or (replies and first_partial > replies[0]), "no partial while answering" else: assert partials and replies and first_partial < replies[0], "partials go on without a game" + + +@pytest.mark.unit +@pytest.mark.parametrize("via, channel", [("voice", None), ("chat", "c1")]) +def test_a_schedule_action_goes_to_the_app_for_the_asker_and_where_they_asked(store, via, channel): + results = [] + brain = FakeBrain(lambda u, t: results.append(brain.schedule.act("add", kind="remind", text="회의", + at=1_900_000_000.0, repeat="none")) or "ok") + + async def body(url): + async with connect(url) as ws: + await hello(ws) + await send(ws, type="reply", id="r1", user="100", text="q", via=via, speak=False, + **({"channel": channel} if channel else {})) + seen = await recv_until(ws, lambda m: isinstance(m, dict) and m.get("type") == "schedule_action") + action = seen[-1] + await send(ws, type="schedule_action_result", id=action["id"], ok=True, item={"id": "7", "text": "회의"}) + await recv_until(ws, lambda m: isinstance(m, dict) and m.get("type") == "reply_text") + return action + + action = asyncio.run(with_worker(body, store=store, brain=brain)) + assert {k: v for k, v in action.items() if k not in ("type", "id")} == { + "action": "add", "user": "100", "via": via, **({"channel": channel} if channel else {}), + "kind": "remind", "text": "회의", "at": 1_900_000_000.0, "repeat": "none"} + assert results == [{"ok": True, "item": {"id": "7", "text": "회의"}}] + + +@pytest.mark.unit +def test_a_schedule_action_the_app_refuses_or_never_answers_fails(store): + results = [] + brain = FakeBrain(lambda u, t: results.extend([brain.schedule.act("list"), brain.schedule.act("list")]) or "ok") + + async def body(url): + async with connect(url) as ws: + await hello(ws) + await send(ws, type="reply", id="r1", user="100", text="q", via="voice", speak=False) + seen = await recv_until(ws, lambda m: isinstance(m, dict) and m.get("type") == "schedule_action") + await send(ws, type="schedule_action_result", id=seen[-1]["id"], ok=False, error="no") + await recv_until(ws, lambda m: isinstance(m, dict) and m.get("type") == "reply_text") + + asyncio.run(with_worker(body, store=store, brain=brain, chat_timeout_s=0.3)) + assert results == [{"ok": False, "error": "no"}, {"ok": False, "error": "the app did not answer in time"}] + + +@pytest.mark.unit +def test_a_scheduled_task_is_answered_without_schedule_or_chat_and_is_no_turn(store): + brain = FakeBrain() + + async def body(url): + async with connect(url) as ws: + await hello(ws) + await ws.send(json.dumps(CHANNELS)) + await send(ws, type="reply", id="t1", user="100", text="날씨 알려 줘", via="task") + seen = await recv_until(ws, lambda m: isinstance(m, dict) and m.get("type") == "reply_text") + await send(ws, type="reply", id="t2", user="100", text="x", via="task", speak=True) + seen += await recv_until(ws, lambda m: isinstance(m, dict) and m.get("type") == "error") + return seen + + worker_turns = {} + + async def run(): + worker = VoiceWorker(TOKEN, LevelTranscriber(), AmplitudeVad, None, history=store, brain=brain) + server = await worker.serve("127.0.0.1", 0) + try: + seen = await asyncio.wait_for(body(f"ws://127.0.0.1:{server.sockets[0].getsockname()[1]}"), 15) + worker_turns.update(worker.reply_turns) + return seen + finally: + server.close() + await server.wait_closed() + await worker.stop() + + seen = asyncio.run(run()) + answer = next(m for m in seen if isinstance(m, dict) and m.get("type") == "reply_text") + assert answer == {"type": "reply_text", "id": "t1", "text": "answer for 날씨 알려 줘", "failed": False} + assert brain.task is True and brain.chat is None and brain.schedule is None + assert worker_turns == {} # not a turn: a delete asked before it is still confirmable + assert any(isinstance(m, dict) and m.get("type") == "error" and m.get("id") == "t2" for m in seen) + + +@pytest.mark.unit +def test_a_scheduled_task_runs_with_the_read_only_tools_and_no_mcp(tmp_path, monkeypatch): + pytest.importorskip("mcp") + from jarvis.memory.history import HistoryContext + from jarvis.reply import turn_state + from jarvis.reply.engine import _offered_builtins + from voice_worker import brain as brain_mod + + cfg = tmp_path / "config.json" + cfg.write_text(json.dumps({"db_path": str(tmp_path / "jarvis.db"), "disabled_tools": ["getWeather"], + "mcps": {"x": {"transport": "stdio", "command": "nope"}}})) + monkeypatch.setenv("JARVIS_CONFIG_PATH", str(cfg)) + monkeypatch.setattr(brain_mod, "_connect_mcp", lambda mcps: None) + seen = [] + + def fake_engine(db, cfg, tts, text, mem, **kw): + seen.append((set(_offered_builtins(cfg, turn_state.get_chat() is not None, + turn_state.get_schedule() is not None)), cfg.mcps, mem)) + return "ok" + + import jarvis.reply.engine as engine + monkeypatch.setattr(engine, "run_reply_engine", fake_engine) + b = brain_mod.JarvisBrain() + b.reply("100", "q", HistoryContext(), chat=object(), schedule=object()) + b.reply("100", "q", HistoryContext(), chat=object(), schedule=object(), task=True) + (normal, normal_mcps, normal_mem), (task, task_mcps, task_mem) = seen + assert {"schedule", "chatChannels", "logMeal"} <= normal and normal_mcps + # Only looking things up, and a feature switched off stays off. + assert task == set(brain_mod.TASK_TOOLS) - {"getWeather"} and task_mcps == {} + assert task_mem is not normal_mem # not a turn of their conversation