Files
gabrielvidalandClaude Opus 5.5 4d0dcfa3ff refactor(toolproxy): the tools are plugins — retire the core brokers, the guests' tool groups and the hub editions
Since 2026-11-25 nanobanana, video-extract, 3d-gen, 3d-render, matomo,
movies and meta are live plugins with their own proxies and managed grants,
but the core still served the old path next to them — and its hub editions
won over the plugin skills on workers. This finishes the move (form
ai-agent-moves-to-ai-agent-plugins_o2qxmd, move A). Rebased on 41665fa2,
which had already removed the core's Meta Business.

- gone: toolproxy/{matomo,media,webroutes,owned,catalog}.py, the nanobanana /
  3d-gen / 3d-render / video-extract / matomo / movies routes and the movies
  webhook, cli/skills/{nanobanana,3d-gen,3d-render,video-extract,matomo,
  movies}, the guests' `tools` groups (users store, Caller.tool_groups,
  Me/GuestUser.tools, tools.matomoSites), the User page's Paid tools and
  Tools budget panels
- kept: the /proxy/tools door (tokens, whoami — now the caller's plugins and
  spend — and the shared client), the tool ledger, the tools chip, a guest
  job's tool token (c6e15991: tool_scope still gives one with plugin skills)
- labsync: a plugin skill wins over a lab skill of the same name; plugin
  skills with a proxy keep their hub.json
- guest runs get their plugins' skills on spawn, resume, reply, fork and No
  project runs alike (deps passed to scope_for / scratch_scope everywhere)
- Movies brings Jellyfin when the movies grant comes on (new guest, or the
  owner's switch)
- plugins/carryover_tools.py: tool groups → grants / installs (never over an
  explicit grant), Meta keys + saved days (only where the plugin has none),
  Matomo viewers, Seerr users and pending requests, 3d-gen ownership → the
  plugins' data, once
- matomo_sites: users.json tools.matomoSites → the matomo grant's
  `extraSites` (list grantField, sibling ai-agent-plugins PR), and the grant
  on for an `analytics` guest — once per guest, so Cel (analytics + site 11
  after the groups moved) gets it; an explicit grant only gets extraSites
  when it has none; waits for a matomo manifest with the field
- Me → Budget's Tools card reads the guest's managed plugins

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-27 11:44:24 +01:00

93 lines
3.7 KiB
Python

"""
``/proxy/tools/…`` — the hub's tool proxy door, outside ``/api`` like the
Claude proxy: no Authelia, no trusted-caller gate. The bearer is the whole
auth (``access.resolve``): a guest's run token, a worker's proxy token, the
lab's internal token or a plugin handler's token. Traefik serves it on
``ai-agent.gabvdl.xyz`` (guests) and on ``ai-agent.lab`` (``/proxy/``, the
owner's workers over the tailnet).
Every tool behind it is a live plugin now (``/proxy/tools/plugins/<pid>/…``,
``plugins/proxyroutes.py``): Nano Banana, video-extract, 3d-gen, 3d-render,
Matomo, Movies and Meta Business left the core for their plugins on
2026-11-26 (the per-tool brokers, the guests' tool groups and the hub
editions of their skills are gone — ``plugins/carryover.py`` moved their data
and the guests' switches onto the plugins). What stays here:
* ``whoami`` — who the bearer is, the live plugins whose skills its runs get,
and this month's tool spend (the ledger every plugin call writes);
* the shared HTTP client and the bearer helpers the plugin routes reuse.
"""
from __future__ import annotations
import httpx
from fastapi import APIRouter, Request
from fastapi.responses import JSONResponse
from users.caller import on_public_surface
from workers import guests
from . import access
from .ledger import LEDGER
router = APIRouter()
_client: httpx.AsyncClient | None = None
def _http() -> httpx.AsyncClient:
global _client
if _client is None:
_client = httpx.AsyncClient(timeout=httpx.Timeout(30.0, read=300.0),
limits=httpx.Limits(max_connections=32))
return _client
def _error(status: int, message: str, kind: str = "error") -> JSONResponse:
return JSONResponse({"error": {"message": message, "kind": kind}}, status_code=status)
def _bearer(request: Request) -> str:
auth = request.headers.get("authorization") or ""
return auth[7:].strip() if auth.lower().startswith("bearer ") else ""
def _deps(request: Request):
return request.app.state.ai
def _who(request: Request):
if guests.ip_locked(request):
return None, _error(429, "too many bad tokens from this address — try again in an hour",
"locked")
deps = _deps(request)
who = access.resolve(deps, _bearer(request), guest_surface=on_public_surface(request))
if who is None:
guests.bad_token(deps, request, "/proxy/tools")
return None, _error(401, "unknown or expired tool token — the hub mints a new one "
"with each run", "auth")
return who, None
# ── whoami ───────────────────────────────────────────────────────────────────
@router.get("/proxy/tools/whoami", include_in_schema=False)
async def whoami(request: Request):
"""Who the bearer is, the live plugins (installed or granted) whose skills
its runs get, and this month's spend in the tool ledger."""
who, err = _who(request)
if err:
return err
deps = _deps(request)
name = "owner"
if not who.is_owner:
name = (deps.users.get(who.uid) or {}).get("name") or who.uid
plugins: list[str] = []
store = getattr(deps, "plugin_store", None)
if store is not None:
try:
plugins = sorted({p["id"] for p in store.live_skill_dirs(who.key) if p.get("id")})
except Exception: # noqa: BLE001 — whoami must answer even then
plugins = []
return {"who": who.key, "name": name, "via": who.via,
"worker": (who.worker or {}).get("name"), "plugins": plugins,
"spentUsd": round(LEDGER.spent(who.key), 4)}