Files
ai-agent/backend/toolproxy/owned.py
gabrielvidalandClaude Opus 5.5 11ef9615f5 feat(toolproxy): the lab's paid tools for guests and the Macs, keys kept in the hub
/proxy/tools/… (outside /api, bearer-only): Nano Banana (Gemini
generateContent with the hub's GEMINI_API_KEY, priced from usageMetadata
at Google's list price), an allowlist of the 3d-gen API as guest-<uid> on
their own jobs and folders only, and video-extract / 3d-render run by the
host sidecar's new /tools/ routes (a video library per guest, the EVOX2
woken first, sources limited to URLs and the caller's own uploads).

- Callers: a guest run's token (aiat1.<uid>.<exp>.<hmac>, minted into the
  run's scope → AIA_TOOLS_URL / AIA_TOOLS_TOKEN) or a worker's proxy token
  (guest computer = its guest, owner's worker = the owner, unlimited).
- Per guest, off by default: users.json tools {groups, pro, budgetUsd} —
  images / video / 3d, Nano Banana Pro, a monthly tools budget apart from
  the Claude budget. EVOX2 / CPU jobs are free but counted.
- A tool ledger of its own (PROXY_LEDGER_DIR/tools/); a call from the
  caller's own conversation stamps meta.tools for the card's chip.
- Guest runs get the hub editions as shared:<name>; guest computers and
  the owner's workers get them through the lab sync with a hub.json
  (their URL + proxy token, 0600 on the worker).
- The new scope key 'tools' needs the host sidecar restarted first.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-06 17:07:59 +01:00

74 lines
2.3 KiB
Python

"""
Which guest a 3d-gen job and model folder belong to — guests' models are
private to them (the owner sees every one in the 3d-gen library, where they
carry ``guest-<uid>`` as author).
The proxy records each job a guest creates, and the folder a job of theirs
produced the first time it reads it, in ``TOOLS_OWNED_PATH`` (JSON).
"""
from __future__ import annotations
import json
import os
import pathlib
import threading
import time
PATH = pathlib.Path(os.environ.get("TOOLS_OWNED_PATH", "/data/tools-owned.json"))
class Owned:
def __init__(self, path: pathlib.Path = PATH):
self.path = pathlib.Path(path)
self._lock = threading.Lock()
self._d: dict | None = None
def _data(self) -> dict:
if self._d is None:
try:
d = json.loads(self.path.read_text())
except (OSError, ValueError):
d = {}
self._d = {"jobs": dict(d.get("jobs") or {}), "folders": dict(d.get("folders") or {})}
return self._d
def _save(self) -> None:
tmp = self.path.with_suffix(".tmp")
try:
self.path.parent.mkdir(parents=True, exist_ok=True)
tmp.write_text(json.dumps(self._d, separators=(",", ":")))
os.replace(tmp, self.path)
except OSError:
pass
def add_job(self, uid: str, job: str) -> None:
with self._lock:
self._data()["jobs"][job] = {"uid": uid, "t": time.time()}
self._save()
def add_folder(self, uid: str, folder: str) -> None:
with self._lock:
d = self._data()
if (d["folders"].get(folder) or {}).get("uid") != uid:
d["folders"][folder] = {"uid": uid, "t": time.time()}
self._save()
def job_of(self, job: str) -> str | None:
with self._lock:
return (self._data()["jobs"].get(job) or {}).get("uid")
def folder_of(self, folder: str) -> str | None:
with self._lock:
return (self._data()["folders"].get(folder) or {}).get("uid")
def jobs(self, uid: str, limit: int = 50) -> list[str]:
"""A guest's jobs, newest first."""
with self._lock:
mine = [(v.get("t") or 0, k) for k, v in self._data()["jobs"].items()
if v.get("uid") == uid]
return [k for _, k in sorted(mine, reverse=True)[:limit]]
OWNED = Owned()