feat: reminders and scheduled tasks (schedule tool, app store and delivery)

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 <noreply@anthropic.com>
This commit is contained in:
javis-bot
2026-11-14 16:27:42 +09:00
parent 3d75d74f92
commit ea2d3ba958
32 changed files with 1568 additions and 59 deletions

View File

@@ -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: