The planner and router read only the first 120 characters of a tool's description. personalNotes started by describing the notes, so for '앞으로 나한테는 짧게 대답해줘' the planner planned a plain reply and the answer promised to be brief from now on without saving anything. Eval (qwen3.6-35b-a3b, game PC): from-now-on cases 0/6 -> 6/6, the one-off and note-shapes-answer cases still pass (15/15). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
88 lines
3.7 KiB
Python
88 lines
3.7 KiB
Python
"""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 (
|
|
"Save (action='add') how the person wants to be answered from now on: how to address "
|
|
"them, answer length, units. Use 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; saying you will do it without this call saves nothing. 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.")
|