107 lines
4.5 KiB
Python
107 lines
4.5 KiB
Python
"""``trackers.stamp`` — a plugin stamps an open / close of one of **its own**
|
|
trackers on a conversation (plugins/trackers.py), for what a transcript can't
|
|
show: a tool that made the thing without a shell command, a clean-up run
|
|
elsewhere.
|
|
|
|
* ``POST …/plugins/{pid}/hub/trackers/{tid}/events`` ``{"sessionId", "do":
|
|
"open" | "close", "key", "name"?, "group"?, "at"?}`` → ``{ok, sessionId,
|
|
items}`` (the tracker's stamped items on that conversation). ``at`` is an
|
|
ISO time (a close without one is stamped now); ``sessionId`` a session or a
|
|
conversation id.
|
|
|
|
* ``GET …/plugins/{pid}/hub/trackers/{tid}/sessions`` → ``{tracker,
|
|
sessions: {<session id>: {conversationId, title, state, worker, items}}}`` — every
|
|
conversation holding items of that tracker (the parse's and the stamps',
|
|
merged), with its card's live ``state`` and the machine it ran on
|
|
(``worker``, null = the lab): who opened what, where, and whether they are
|
|
still running. What a clean-up needs before it closes things.
|
|
|
|
The stamps live in ``meta.trackers["<pid>:<tid>"]`` and merge with the
|
|
parse's by ``key``. Owner only, operator approval; the plugin's own handlers
|
|
and its runs (a script a run calls with its run token). A run's stamp on a
|
|
guest's conversation is refused, as ``conversations.data``; the plugin's own
|
|
**handler** may close (never open) an item there — the owner removing what a
|
|
guest's run made (the worktrees plugin's ``rm``), as the core route
|
|
``POST /api/conversation-meta {worktree}`` always could.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from typing import Any
|
|
|
|
from fastapi import APIRouter, Request
|
|
from starlette.concurrency import run_in_threadpool
|
|
|
|
from plugins import trackers
|
|
from plugins.hubconvdata import session_of
|
|
from plugins.proxyroutes import _active_plugin, _error, _who
|
|
|
|
router = APIRouter(include_in_schema=False)
|
|
|
|
CAP = trackers.CAP
|
|
|
|
|
|
def _gate(request: Request, pid: str, tid: str):
|
|
"""``(deps, who, tracker key, None)`` for the owner's active plugin that
|
|
holds the capability and declares the tracker, else a refusal last."""
|
|
who, err = _who(request, pid)
|
|
if err:
|
|
return None, None, None, err
|
|
if who.handler not in (None, pid):
|
|
return None, None, None, _error(403, f"{CAP} is only available to the plugin itself",
|
|
"handler_only")
|
|
if not who.is_owner:
|
|
return None, None, None, _error(403, f"{CAP} is owner-only", "capability")
|
|
deps = request.app.state.ai
|
|
_, rec, _, _, err = _active_plugin(deps, who, pid)
|
|
if err:
|
|
return None, None, None, err
|
|
if CAP not in (rec["manifest"].get("capabilities") or []):
|
|
return None, None, None, _error(403, f"plugin lacks {CAP} capability", "capability")
|
|
key = f"{pid}:{tid}"
|
|
if key not in trackers.registry(deps).by_key:
|
|
return None, None, None, _error(404, "no such tracker", "not_found")
|
|
return deps, who, key, None
|
|
|
|
|
|
def _sessions(request: Request, pid: str, tid: str):
|
|
deps, _, key, err = _gate(request, pid, tid)
|
|
if err:
|
|
return err
|
|
return {"tracker": key, "sessions": trackers.tracker_sessions(deps, key)}
|
|
|
|
|
|
def _stamp(request: Request, pid: str, tid: str, body: dict):
|
|
deps, who, key, err = _gate(request, pid, tid)
|
|
if err:
|
|
return err
|
|
ev = trackers.clean_event(body)
|
|
if ev is None:
|
|
return _error(400, "the body is {sessionId, do: open|close, key, name?, group?, at?}",
|
|
"bad_request")
|
|
sid = session_of(deps, str(body.get("sessionId") or ""))
|
|
if not sid:
|
|
return _error(404, "conversation not found", "not_found")
|
|
if (deps.meta_store.peek(sid) or {}).get("owner") \
|
|
and not (who.handler == pid and ev["do"] == "close"):
|
|
return _error(403, "a guest's conversation", "capability")
|
|
merged = trackers.stamp(deps, sid, key, ev) or {}
|
|
return {"ok": True, "sessionId": sid,
|
|
"items": (merged.get("trackers") or {}).get(key) or []}
|
|
|
|
|
|
@router.get("/proxy/tools/plugins/{pid}/hub/trackers/{tid}/sessions")
|
|
async def plugin_hub_tracker_sessions(pid: str, tid: str, request: Request):
|
|
return await run_in_threadpool(_sessions, request, pid, tid)
|
|
|
|
|
|
@router.post("/proxy/tools/plugins/{pid}/hub/trackers/{tid}/events")
|
|
async def plugin_hub_tracker_event(pid: str, tid: str, request: Request):
|
|
try:
|
|
body: Any = await request.json()
|
|
except (ValueError, UnicodeDecodeError):
|
|
body = None
|
|
if not isinstance(body, dict):
|
|
return _error(400, "the body is a JSON object", "bad_request")
|
|
return await run_in_threadpool(_stamp, request, pid, tid, body)
|