feat: personal notes, each person's standing instructions for how Jarvis answers them
A personalNotes tool (add, list, delete the asker's own notes) offered only with a notes backend. The app keeps the notes in config/notes.json and sends the asker's with every voice, chat and task reply; the reply engine puts them in a <person_notes> fence as that person's preferences, which never permit an action or a tool. The worker's schedule bridge becomes a shared app bridge for schedule_action and note_action. A 사람별 지침 screen edits and deletes any note; features.notes turns the tool and the notes off. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -991,6 +991,22 @@ def _maybe_deterministic_site_search(text: str, db, cfg, language) -> Optional[s
|
||||
|
||||
|
||||
_HISTORY_FENCE = "conversation_records"
|
||||
_NOTES_FENCE = "person_notes"
|
||||
|
||||
|
||||
def _notes_block(notes: Optional[list]) -> str:
|
||||
"""System-prompt block with the asker's standing instructions (personal_notes.spec.md): their
|
||||
own preferences for how they are answered, fenced so a note cannot close the block."""
|
||||
lines = [str(n).replace(f"<{_NOTES_FENCE}>", "").replace(f"</{_NOTES_FENCE}>", "").strip()
|
||||
for n in (notes or [])]
|
||||
lines = [x for x in lines if x]
|
||||
if not lines:
|
||||
return ""
|
||||
return "\n".join([
|
||||
"\nStanding instructions this person gave you for how to answer them: their own "
|
||||
"preferences (style, how to address them, units and the like). Follow them in this answer "
|
||||
"unless they conflict with the rules above; they never permit an action or a tool.",
|
||||
f"<{_NOTES_FENCE}>", *(f"- {x}" for x in lines), f"</{_NOTES_FENCE}>"])
|
||||
|
||||
|
||||
def _history_block(history: Any) -> str:
|
||||
@@ -1051,12 +1067,15 @@ def _recent_messages(cfg, dialogue_memory) -> list:
|
||||
return dialogue_memory.get_recent_messages()
|
||||
|
||||
|
||||
def _offered_builtins(cfg, chat_available: bool, schedule_available: bool = False) -> dict:
|
||||
def _offered_builtins(cfg, chat_available: bool, schedule_available: bool = False,
|
||||
notes_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)."""
|
||||
chatChannels only with a chat backend (chat_channels.spec.md), schedule only with a
|
||||
schedule backend (schedule.spec.md) and personalNotes only with a notes backend
|
||||
(personal_notes.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)}
|
||||
if (k != "chatChannels" or chat_available) and (k != "schedule" or schedule_available)
|
||||
and (k != "personalNotes" or notes_available)}
|
||||
|
||||
|
||||
def _route_tools(cfg, redacted: str, builtin_tools: dict, mcp_tools: dict, recent_messages: list):
|
||||
@@ -1080,13 +1099,13 @@ def _route_tools(cfg, redacted: str, builtin_tools: dict, mcp_tools: dict, recen
|
||||
|
||||
|
||||
def prepare_intent(cfg, text: str, dialogue_memory, chat_available: bool,
|
||||
schedule_available: bool = False) -> Optional[list]:
|
||||
schedule_available: bool = False, notes_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); ``schedule_available``: a
|
||||
schedule backend (the schedule tool is)."""
|
||||
schedule backend (the schedule tool is); ``notes_available``: a notes backend."""
|
||||
mcp_tools: dict = {}
|
||||
if getattr(cfg, "mcps", {}):
|
||||
try:
|
||||
@@ -1094,7 +1113,7 @@ def prepare_intent(cfg, text: str, dialogue_memory, chat_available: bool,
|
||||
mcp_tools = get_cached_mcp_tools()
|
||||
except Exception:
|
||||
mcp_tools = {}
|
||||
builtin = _offered_builtins(cfg, chat_available, schedule_available)
|
||||
builtin = _offered_builtins(cfg, chat_available, schedule_available, notes_available)
|
||||
try:
|
||||
routed = _route_tools(cfg, redact(text), builtin, mcp_tools, _recent_messages(cfg, dialogue_memory))
|
||||
except Exception as exc: # pragma: no cover — defensive
|
||||
@@ -1113,7 +1132,8 @@ def run_reply_engine(db: "Database", cfg, tts: Optional[Any],
|
||||
default_language: Optional[str] = None,
|
||||
compact_prompt: bool = False,
|
||||
intent_judge: bool = False,
|
||||
prepared_tools: Optional[list] = None) -> Optional[str]:
|
||||
prepared_tools: Optional[list] = None,
|
||||
person_notes: Optional[list] = None) -> Optional[str]:
|
||||
"""
|
||||
Main entry point for reply generation.
|
||||
|
||||
@@ -1234,9 +1254,10 @@ 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, get_schedule as _get_schedule
|
||||
from .turn_state import get_chat as _get_chat, get_schedule as _get_schedule, get_notes as _get_notes
|
||||
_chat_backend = _get_chat()
|
||||
_builtin_tools = _offered_builtins(cfg, _chat_backend is not None, _get_schedule() is not None)
|
||||
_builtin_tools = _offered_builtins(cfg, _chat_backend is not None, _get_schedule() is not None,
|
||||
_get_notes() 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
|
||||
@@ -1902,6 +1923,10 @@ def run_reply_engine(db: "Database", cfg, tts: Optional[Any],
|
||||
# gated by the planner's searchMemory decision.
|
||||
guidance.append("\n" + warm_profile_block)
|
||||
|
||||
_nb = _notes_block(person_notes)
|
||||
if _nb:
|
||||
guidance.append(_nb)
|
||||
|
||||
_hb = _history_block(history)
|
||||
if _hb:
|
||||
guidance.append(_hb)
|
||||
|
||||
@@ -25,6 +25,9 @@ chat : Optional[object]
|
||||
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).
|
||||
notes : Optional[object]
|
||||
v2: the asker's standing instructions backend (personal_notes.spec.md). None means the
|
||||
personalNotes tool is not offered.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
@@ -42,6 +45,15 @@ def reset() -> None:
|
||||
_state.user = None
|
||||
_state.chat = None
|
||||
_state.schedule = None
|
||||
_state.notes = None
|
||||
|
||||
|
||||
def set_notes(backend) -> None:
|
||||
_state.notes = backend
|
||||
|
||||
|
||||
def get_notes():
|
||||
return getattr(_state, "notes", None)
|
||||
|
||||
|
||||
def set_schedule(backend) -> None:
|
||||
|
||||
87
src/jarvis/tools/builtin/personal_notes.py
Normal file
87
src/jarvis/tools/builtin/personal_notes.py
Normal file
@@ -0,0 +1,87 @@
|
||||
"""personalNotes: the asker's standing instructions for how Jarvis answers them (v2).
|
||||
Contract: personal_notes.spec.md."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import re
|
||||
from typing import Any, Dict, Optional
|
||||
|
||||
from ..base import Tool, ToolContext
|
||||
from ..types import ToolExecutionResult
|
||||
from ...debug import debug_log
|
||||
|
||||
_ACTIONS = ("add", "list", "delete")
|
||||
MAX_TEXT_CHARS = 300
|
||||
|
||||
|
||||
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)
|
||||
|
||||
|
||||
class PersonalNotesTool(Tool):
|
||||
@property
|
||||
def name(self) -> str:
|
||||
return "personalNotes"
|
||||
|
||||
@property
|
||||
def description(self) -> str:
|
||||
return (
|
||||
"The person's standing instructions for how you answer them from now on, such as how to "
|
||||
"address them or which units to use. Call it with action='add' only when they ask for "
|
||||
"something to apply from now on (not for a one-off request), with text as one short "
|
||||
"instruction in their language; list or delete them when asked. The saved instructions "
|
||||
"are shown to you on every answer to this person."
|
||||
)
|
||||
|
||||
@property
|
||||
def inputSchema(self) -> Dict[str, Any]:
|
||||
return {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"action": {"type": "string", "enum": list(_ACTIONS)},
|
||||
"text": {"type": "string", "description": "add: the instruction, one short sentence"},
|
||||
"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_notes
|
||||
|
||||
backend = get_notes()
|
||||
if backend is None:
|
||||
return _fail("standing instructions are not available here")
|
||||
args = args if isinstance(args, dict) else {}
|
||||
action = next((w for w in re.findall(r"[a-z]+", str(args.get("action") or "").lower()) if w in _ACTIONS), "")
|
||||
if not action:
|
||||
return _fail(f"action must be one of {', '.join(_ACTIONS)}")
|
||||
debug_log(f"personalNotes {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("Standing instructions of this person:\n" + "\n".join(
|
||||
f"- id {i.get('id')}: {i.get('text')}" for i in items) if items
|
||||
else "No standing instructions are saved for this person.")
|
||||
if action == "delete":
|
||||
nid = str(args.get("id") or "").strip()
|
||||
if not nid:
|
||||
return _fail("delete needs id (call action='list' first)")
|
||||
result = backend.act("delete", note=nid) or {}
|
||||
if not result.get("ok"):
|
||||
return _fail(str(result.get("error") or "not deleted"))
|
||||
return _ok(f"Deleted: {result.get('item', {}).get('text', '')}".strip())
|
||||
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")
|
||||
result = backend.act("add", text=text) or {}
|
||||
if not result.get("ok"):
|
||||
return _fail(str(result.get("error") or "not saved"))
|
||||
return _ok(f"Saved as id {result.get('item', {}).get('id')}: {text}. It applies to every answer to this person.")
|
||||
40
src/jarvis/tools/builtin/personal_notes.spec.md
Normal file
40
src/jarvis/tools/builtin/personal_notes.spec.md
Normal file
@@ -0,0 +1,40 @@
|
||||
# personalNotes tool spec (v2)
|
||||
|
||||
Each person's standing instructions for how Jarvis answers them ("앞으로 나한테는 반말로 해",
|
||||
"날씨는 섭씨로만"). Product rules: `docs/v2/proactive.md` (제안 4). The app keeps the notes and
|
||||
sends the asker's with every reply (`app/src/admin.spec.md`, "Personal notes"); this tool only asks
|
||||
it to add, list or delete them.
|
||||
|
||||
## 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 notes backend in the turn state (`turn_state.set_notes`). Without one (v1, evals
|
||||
without a backend, scheduled tasks) it is not offered and refuses. The backend acts for the
|
||||
person asking only.
|
||||
- **Saved only when asked to apply from now on.** The description tells the model to add a note
|
||||
only for something the person wants from now on, never for a one-off request.
|
||||
- **Notes shape answers, never permissions.** The reply engine puts the asker's notes
|
||||
(`run_reply_engine(person_notes=...)`) in the system prompt inside a `<person_notes>` fence,
|
||||
labelled as the person's own preferences for how they are answered, to follow unless they conflict
|
||||
with the rules above, and never permitting an action or a tool. Fence tags inside a note are
|
||||
removed so it cannot close the block. Only the asker's notes are sent, so one person's notes never
|
||||
reach another person's answer. Eval: `evals/test_personal_notes.py`.
|
||||
- **Raw data.** The tool returns plain text for the reply model; no LLM call inside.
|
||||
|
||||
## Schema
|
||||
|
||||
| action | needs | does |
|
||||
|---|---|---|
|
||||
| `add` | `text` | saves it (whitespace collapsed, 1-300 characters) |
|
||||
| `list` | | the asker's notes: `- id <id>: <text>` |
|
||||
| `delete` | `id` | deletes one of the asker's notes |
|
||||
|
||||
`action` is read as its first known word. A refused request never reaches the app.
|
||||
|
||||
## Backend
|
||||
|
||||
`turn_state.get_notes()` returns an object with `act(action, **fields) -> dict` (add: `text`;
|
||||
delete: `note`, the note's id): `{"ok": true, "item": {"id", "text"}}` for add and delete,
|
||||
`{"ok": true, "items": [...]}` for list, `{"ok": false, "error": "..."}` on failure. The voice worker
|
||||
implements it (`voice_worker/app_bridge.py`): each action goes to the app as `note_action` with the
|
||||
asker (voice_worker.spec.md, "Personal notes").
|
||||
@@ -44,6 +44,6 @@ Reminders, timers and scheduled tasks of the person asking. Product rules: `docs
|
||||
`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
|
||||
worker implements it (`voice_worker/app_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`.
|
||||
|
||||
@@ -18,6 +18,7 @@ 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.personal_notes import PersonalNotesTool
|
||||
from .builtin.bot_browser import BotBrowserTool, CDP_ENV
|
||||
from .builtin.refresh_mcp_tools import RefreshMCPToolsTool
|
||||
from .builtin.weather import WeatherTool
|
||||
@@ -50,6 +51,7 @@ BUILTIN_TOOLS = {
|
||||
"setBroadcast": SetBroadcastTool(),
|
||||
"chatChannels": ChatChannelsTool(),
|
||||
"schedule": ScheduleTool(),
|
||||
"personalNotes": PersonalNotesTool(),
|
||||
"botBrowser": BotBrowserTool(),
|
||||
}
|
||||
|
||||
|
||||
30
src/voice_worker/app_bridge.py
Normal file
30
src/voice_worker/app_bridge.py
Normal file
@@ -0,0 +1,30 @@
|
||||
"""Backends the brain's schedule and personalNotes tools use during one reply (schedule.spec.md,
|
||||
personal_notes.spec.md).
|
||||
|
||||
The app keeps the schedules and the notes: every action goes to it as ``<kind>_action`` for the
|
||||
person asking and waits for its ``<kind>_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 AppBridge:
|
||||
def __init__(self, session, loop: asyncio.AbstractEventLoop, kind: str, user: str, via: str,
|
||||
channel: Optional[str]):
|
||||
self._session = session
|
||||
self._loop = loop
|
||||
self._kind = kind # "schedule" or "note"
|
||||
# Who asked and where: the app delivers schedules 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.app_action(self._kind, 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__})"}
|
||||
@@ -18,12 +18,14 @@ from jarvis.memory.history import HistoryContext, Scope
|
||||
|
||||
class Brain(Protocol):
|
||||
def reply(self, user: str, text: str, history: HistoryContext, chat=None, schedule=None,
|
||||
task: bool = False) -> Optional[str]:
|
||||
task: bool = False, notes: Optional[list] = None, notes_backend=None) -> 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.
|
||||
``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``)."""
|
||||
``schedule``: the schedule backend for the schedule tool (voice_worker/app_bridge.py).
|
||||
``task``: a scheduled task runs, with the read-only tools only (``TASK_TOOLS``).
|
||||
``notes``: the asker's standing instructions, put in the prompt; ``notes_backend``: the
|
||||
backend for the personalNotes tool (personal_notes.spec.md)."""
|
||||
|
||||
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.
|
||||
@@ -267,7 +269,8 @@ class JarvisBrain:
|
||||
self._prepared[user] = p
|
||||
t0 = time.perf_counter()
|
||||
try:
|
||||
p.tools = Muted.run(prepare_intent, self.cfg, text, mem, chat, schedule_available=True)
|
||||
p.tools = Muted.run(prepare_intent, self.cfg, text, mem, chat, schedule_available=True,
|
||||
notes_available=True)
|
||||
finally:
|
||||
p.done.set()
|
||||
debug_log(f"brain prepare user={user} picked={p.tools is not None} "
|
||||
@@ -290,7 +293,7 @@ class JarvisBrain:
|
||||
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]:
|
||||
task: bool = False, notes: Optional[list] = None, notes_backend=None) -> Optional[str]:
|
||||
from jarvis.memory.conversation import DialogueMemory
|
||||
from jarvis.reply import turn_state
|
||||
from jarvis.reply.engine import run_reply_engine
|
||||
@@ -309,11 +312,13 @@ class JarvisBrain:
|
||||
turn_state.set_user(user)
|
||||
turn_state.set_chat(None if task else chat)
|
||||
turn_state.set_schedule(None if task else schedule)
|
||||
turn_state.set_notes(None if task else notes_backend)
|
||||
try:
|
||||
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)
|
||||
intent_judge=True, prepared_tools=prepared,
|
||||
person_notes=notes or None)
|
||||
finally:
|
||||
turn_state.reset()
|
||||
|
||||
|
||||
@@ -1,27 +0,0 @@
|
||||
"""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__})"}
|
||||
@@ -48,6 +48,11 @@ class _LlmPaused(Exception):
|
||||
"""The app paused the LLM (a game runs): nothing is answered until it is ready again."""
|
||||
|
||||
|
||||
# The asker's standing instructions the app sends with a reply (personal_notes.spec.md).
|
||||
MAX_NOTES = 20
|
||||
MAX_NOTE_CHARS = 300
|
||||
|
||||
|
||||
class VoiceWorker:
|
||||
def _partials_held(self) -> bool:
|
||||
return self.llm_game and self.answering > 0
|
||||
@@ -347,7 +352,8 @@ 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,
|
||||
"schedule_action_result": self._on_app_action_result,
|
||||
"note_action_result": self._on_app_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:
|
||||
@@ -516,13 +522,14 @@ 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)."""
|
||||
async def app_action(self, kind: str, action: str, args: dict) -> dict:
|
||||
"""Ask the app to add, list or delete a schedule or a note (``kind``) 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")
|
||||
self.send({**args, "type": f"{kind}_action", "id": aid, "action": action})
|
||||
debug_log(f"{kind} action {aid} {action}", "voice_worker")
|
||||
try:
|
||||
return await asyncio.wait_for(fut, self.worker.chat_timeout_s)
|
||||
except asyncio.TimeoutError:
|
||||
@@ -530,7 +537,7 @@ class Session:
|
||||
finally:
|
||||
self.chat_waiting.pop(aid, None)
|
||||
|
||||
def _on_schedule_action_result(self, msg: dict) -> None:
|
||||
def _on_app_action_result(self, msg: dict) -> None:
|
||||
fut = self.chat_waiting.get(msg.get("id"))
|
||||
if fut is None or fut.done():
|
||||
return
|
||||
@@ -559,16 +566,19 @@ class Session:
|
||||
return
|
||||
rid, user, text = got
|
||||
via, speak, channel = msg.get("via"), msg.get("speak", False), msg.get("channel")
|
||||
notes = msg.get("notes", [])
|
||||
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)):
|
||||
or (via == "task" and speak) or not (channel is None or isinstance(channel, str)) \
|
||||
or not (isinstance(notes, list) and len(notes) <= MAX_NOTES
|
||||
and all(isinstance(n, str) and len(n) <= MAX_NOTE_CHARS for n in notes)):
|
||||
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)
|
||||
"an optional string channel, at most 20 string notes 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))
|
||||
asyncio.get_running_loop().create_task(self._task(rid, user, text, notes))
|
||||
return
|
||||
if speak:
|
||||
self.thinking_replies.add(rid)
|
||||
@@ -578,9 +588,9 @@ 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, channel))
|
||||
asyncio.get_running_loop().create_task(self._reply(rid, user, text, via, speak, early, channel, notes))
|
||||
|
||||
async def _task(self, rid: str, user: str, text: str) -> None:
|
||||
async def _task(self, rid: str, user: str, text: str, notes: list | None = None) -> 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."""
|
||||
@@ -600,7 +610,8 @@ class Session:
|
||||
history = HistoryContext()
|
||||
try:
|
||||
async with self._llm():
|
||||
answer = await asyncio.to_thread(w.brain.reply, user, text, history, task=True)
|
||||
answer = await asyncio.to_thread(w.brain.reply, user, text, history, task=True,
|
||||
notes=notes or [])
|
||||
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()
|
||||
@@ -742,9 +753,9 @@ class Session:
|
||||
asyncio.get_running_loop().create_task(rank())
|
||||
|
||||
async def _reply(self, rid: str, user: str, text: str, via: str, speak: bool, early: bool = False,
|
||||
channel: str | None = None) -> None:
|
||||
channel: str | None = None, notes: list | None = None) -> None:
|
||||
try:
|
||||
await self._reply_inner(rid, user, text, via, speak, early, channel)
|
||||
await self._reply_inner(rid, user, text, via, speak, early, channel, notes or [])
|
||||
finally:
|
||||
self.thinking_replies.discard(rid)
|
||||
self.cancelled_replies.discard(rid)
|
||||
@@ -782,7 +793,7 @@ class Session:
|
||||
self._delivered(rid, ok)
|
||||
|
||||
async def _reply_inner(self, rid: str, user: str, text: str, via: str, speak: bool, early: bool,
|
||||
chat_channel: str | None = None) -> None:
|
||||
chat_channel: str | None = None, notes: list | 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.
|
||||
@@ -805,15 +816,17 @@ class Session:
|
||||
from .chat_bridge import ChatBridge
|
||||
chat = ChatBridge(self, asyncio.get_running_loop(), turn, w.pending_deletes.setdefault(user, {}), 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)
|
||||
from .app_bridge import AppBridge
|
||||
origin = (user, via, chat_channel if via == "chat" else None)
|
||||
schedule = AppBridge(self, asyncio.get_running_loop(), "schedule", *origin)
|
||||
notes_backend = AppBridge(self, asyncio.get_running_loop(), "note", *origin)
|
||||
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,
|
||||
schedule=schedule)
|
||||
schedule=schedule, notes=notes or [],
|
||||
notes_backend=notes_backend)
|
||||
finally:
|
||||
w.answering -= 1
|
||||
if not w.answering and w.scheduler:
|
||||
|
||||
@@ -200,7 +200,8 @@ 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
|
||||
// via chat: optional "channel" it was written in;
|
||||
// optional "notes": the asker's standing instructions
|
||||
<- {"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
|
||||
@@ -347,6 +348,25 @@ the schedules (`app/src/admin.spec.md`, "Schedules"):
|
||||
(`speak: true` is `bad_message`), and its answer is not stored; the app delivers it. It waits for
|
||||
LLM switches like any reply.
|
||||
|
||||
### Personal notes
|
||||
|
||||
The brain's personalNotes tool (`src/jarvis/tools/builtin/personal_notes.spec.md`) asks the app,
|
||||
which keeps each person's standing instructions and sends the asker's with every `reply`
|
||||
(`app/src/admin.spec.md`, "Personal notes"):
|
||||
|
||||
```
|
||||
-> {"type": "reply", ..., "notes": ["나한테는 반말로 해"]} // optional; at most 20, each at most 300 characters
|
||||
<- {"type": "note_action", "id": "n1", "action": "add|list|delete", "user": "<id>", "via": "voice|chat",
|
||||
"channel": "...", "text": "...", "note": "<id>"} // add: text; delete: note
|
||||
-> {"type": "note_action_result", "id": "n1", "ok": true, "item": {...}} // or "items", or ok false with "error"
|
||||
```
|
||||
|
||||
- `notes` that are not a list of at most 20 strings of at most 300 characters are `bad_message`.
|
||||
The brain puts them in the reply's prompt (`person_notes`); a scheduled task (`via: "task"`) gets
|
||||
them too, but no notes tool.
|
||||
- Every voice and chat reply gets a notes backend for its asker; ids and waiting are those of chat
|
||||
and schedule actions.
|
||||
|
||||
### 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