loopable / api /tool_groups /intelligence.py
fsanyoto's picture
Deploy AIOS web (React glide grid + FastAPI slice)
c72f0a1 verified
Raw History Blame
4.75 kB
"""Read Intelligence metadata first, then one file, and submit edits for approval."""
from functools import wraps
from core.external_tools import ToolContext
from deps import err
import intelligence_store as files
def _handler(action, allowed, required=()):
def decorate(fn):
@wraps(fn)
def call(ctx, args):
if not isinstance(ctx, ToolContext):
raise err(403, "tool_context_required", "Use the authenticated Brain tool.")
if (not isinstance(args, dict) or set(args) - set(allowed) - {"action"}
or set(required) - set(args) or args.get("action", action) != action):
raise err(400, "bad_arguments", f"Use only the declared arguments for intelligence.{action}.")
try:
return fn(ctx, args)
except files.IntelligenceError as exc:
raise err(exc.status, exc.code, str(exc)) from None
return call
return decorate
@_handler("index", ())
def index(ctx, args):
"""Read section counts and your visible pins in at most 4 KB, with explicit truncation."""
return files.index_files(ctx.session)
@_handler("search", ("queries",), ("queries",))
def search(ctx, args):
"""Search several phrasings for up to 20 titles and one-line descriptions, without bodies."""
return files.search_files(ctx.session, args["queries"])
@_handler("get", ("path",), ("path",))
def get(ctx, args):
"""Read one whole file, including a Database note's current shared text."""
return {"file": files.read_file(ctx.session, args["path"])}
@_handler("links", ("database", "field"), ("database",))
def links(ctx, args):
"""Find up to 20 titles and one-line descriptions naming a database and optional field, without bodies."""
return files.linked_files(ctx, args["database"], args.get("field"))
@_handler("propose", ("path", "body", "expectedVersion"), ("path", "body"))
def propose(ctx, args):
"""Submit Markdown for its owner or declared approvers without replacing the approved file."""
return files.propose_file(ctx, args["path"], args["body"], expected_version=args.get("expectedVersion"))
@_handler("propose", ("path", "body", "expectedVersion"), ("path", "body"))
def _propose_receipt_access(ctx, args):
"""Recheck current file and source access without executing the proposal writer."""
files.read_file(ctx.session, args["path"], record_use=False)
return True
def _action(handler, properties=None, required=(), *, read=True, receipt_access=None):
entry = {"read": read, "destructive": False, "confirm": not read,
"schema": {"type": "object", "description": handler.__doc__, "properties": properties or {},
"required": list(required), "additionalProperties": False}, "handler": handler}
if receipt_access is not None:
entry["receipt_access"] = receipt_access
return entry
_PATH = {"type": "string", "maxLength": files.MAX_PATH, "description": "The relative .md path from index or search."}
GROUP = {
"name": "intelligence",
"title": "Brain",
"guide": "Read index for sections and your pins, then search with several phrasings. "
"Both return metadata only and flag truncation. Use get for one whole file. "
"Database notes are live files at database-notes/<database key>.md. "
"Their text stays in the database's shared note. Agent edits require approval. "
"Authenticated AI clients use links with an exact database key and optional field key. "
"It matches keys named together in current front matter or body, including database.field, "
"and returns bounded metadata with truncation. "
"Propose previews a write for confirmation and then requests the file's approvals. "
"Only approved text replaces the file. Pins use your intelligence.pins preference.",
"actions": {
"index": _action(index),
"search": _action(search, {"queries": {"type": "array", "minItems": 1, "maxItems": 20,
"items": {"type": "string", "minLength": 1, "maxLength": 500}}}, ("queries",)),
"get": _action(get, {"path": _PATH}, ("path",)),
"links": _action(links, {"database": {"type": "string", "minLength": 1, "maxLength": 200},
"field": {"type": "string", "minLength": 1, "maxLength": 200}}, ("database",)),
"propose": _action(propose, {"path": _PATH, "body": {"type": "string"},
"expectedVersion": {"type": "integer", "minimum": 0}}, ("path", "body"),
read=False, receipt_access=_propose_receipt_access),
},
}