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:
@@ -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
|
||||
|
||||
@@ -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:
|
||||
|
||||
145
src/jarvis/tools/builtin/schedule.py
Normal file
145
src/jarvis/tools/builtin/schedule.py
Normal file
@@ -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'])}")
|
||||
49
src/jarvis/tools/builtin/schedule.spec.md
Normal file
49
src/jarvis/tools/builtin/schedule.spec.md
Normal file
@@ -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 <id>: <kind> at YYYY-MM-DD HH:MM (<weekday>)[, repeats <repeat>]: <text>`.
|
||||
|
||||
## 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`.
|
||||
@@ -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(),
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
27
src/voice_worker/schedule_bridge.py
Normal file
27
src/voice_worker/schedule_bridge.py
Normal file
@@ -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__})"}
|
||||
@@ -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:
|
||||
|
||||
@@ -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": "<id>", "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": "<id>", "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": "<id>",
|
||||
"via": "voice|chat", "channel": "<chat channel id>", // channel: only for via chat, when given
|
||||
"kind": "remind|task", "text": "...", "at": 1762830000.0, "repeat": "none|daily|weekdays|weekly",
|
||||
"schedule": "<id>"} // 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": "<id>", "text": "<the task>", "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
|
||||
|
||||
Reference in New Issue
Block a user