[STAGE5] Migrate PhotoBot to Tool Gateway with local tools
This commit is contained in:
1 parent
c0ec0d2d3d
commit
8844dabf94
5 files changed
+70
-2
No files matched your search
@@ -20,6 +20,12 @@ direct 127.0.0.1:9120 → blocked
|
||||
|
||||
Поэтому Gate G5 «агент не имеет production-доступа вне Tool Gateway» **не доказан для всего флота**.
|
||||
|
||||
## Прогресс
|
||||
|
||||
- `PhotoBot` мигрирован на gateway local-tools (`photo.stats`, `photo.space`);
|
||||
- feature flag `HUB_GATEWAY_PHOTO=1` включён;
|
||||
- код и env имеют backup; hub-bots active, 9 ботов на связи.
|
||||
|
||||
## Порядок миграции
|
||||
|
||||
1. Добавить в Tool Gateway недостающие tools:
|
||||
|
||||
@@ -25,6 +25,14 @@ tools:
|
||||
gateway: http://127.0.0.1:9180/v1/events
|
||||
mode: read
|
||||
approval: false
|
||||
- name: photo.stats
|
||||
mode: read
|
||||
local: photo_stats
|
||||
approval: false
|
||||
- name: photo.space
|
||||
mode: read
|
||||
local: photo_space
|
||||
approval: false
|
||||
- name: audit.write
|
||||
gateway: http://127.0.0.1:9160/api/sprint/status
|
||||
mode: write
|
||||
|
||||
@@ -7,6 +7,7 @@ Write tools require X-Approval: approved.
|
||||
"""
|
||||
import json
|
||||
import os
|
||||
import sys
|
||||
import time
|
||||
import uuid
|
||||
import urllib.error
|
||||
@@ -28,6 +29,23 @@ def tools():
|
||||
return {t["name"]: t for t in (yaml.safe_load(open(POLICY).read()).get("tools") or [])}
|
||||
|
||||
|
||||
def _photo_local():
|
||||
sys.path.insert(0, "/opt/hub-bots")
|
||||
from hub import local
|
||||
return local
|
||||
|
||||
|
||||
def local_call(handler):
|
||||
if handler == "photo_stats":
|
||||
c, err = _photo_local().ImmichDb().counts()
|
||||
return {"ok": not bool(err), "counts": c, "error": err}
|
||||
if handler == "photo_space":
|
||||
out, err = _photo_local().pct_exec(151, ["su", "-", "postgres", "-c",
|
||||
"psql -d immich_db -tAc \"select pg_size_pretty(pg_database_size('immich_db'))\""])
|
||||
return {"ok": not bool(err), "size": out, "error": err}
|
||||
raise ValueError("unknown local handler")
|
||||
|
||||
|
||||
def models():
|
||||
return {m["id"]: m for m in (yaml.safe_load(open(MODELS).read()).get("models") or [])}
|
||||
|
||||
@@ -162,6 +180,14 @@ class H(BaseHTTPRequestHandler):
|
||||
t = tools().get(name)
|
||||
if not t:
|
||||
return self._json(404, {"error": "unknown tool"})
|
||||
if t.get("mode") == "read" and t.get("local"):
|
||||
try:
|
||||
body = local_call(t["local"])
|
||||
log({"tool": name, "mode": "local", "code": 200})
|
||||
return self._json(200, {"tool": name, "body": body})
|
||||
except Exception as e:
|
||||
log({"tool": name, "mode": "local", "error": str(e)[:200]})
|
||||
return self._json(502, {"error": str(e)[:200]})
|
||||
if t["mode"] != "read":
|
||||
return self._json(403, {"error": "write tool requires POST approval"})
|
||||
try:
|
||||
|
||||
+13
-2
@@ -1,3 +1,4 @@
|
||||
import os
|
||||
"""Флот ботов: у каждого свой домен и свои команды."""
|
||||
import datetime
|
||||
|
||||
@@ -823,7 +824,12 @@ class PhotoBot(Bot):
|
||||
self.cmd("space", "сколько занимает база фото")(lambda a: self._space())
|
||||
|
||||
def _stats(self):
|
||||
c, err = local.ImmichDb().counts()
|
||||
if os.environ.get("HUB_GATEWAY_PHOTO") == "1":
|
||||
from .gateway import call
|
||||
d = call("photo.stats")
|
||||
c, err = d.get("counts"), d.get("error")
|
||||
else:
|
||||
c, err = local.ImmichDb().counts()
|
||||
if err:
|
||||
return f"Не смог прочитать базу Immich: {err}"
|
||||
lines = ["**Фотографии**", "",
|
||||
@@ -837,7 +843,12 @@ class PhotoBot(Bot):
|
||||
return "\n".join(lines)
|
||||
|
||||
def _space(self):
|
||||
out, err = local.pct_exec(151, ["su", "-", "postgres", "-c",
|
||||
if os.environ.get("HUB_GATEWAY_PHOTO") == "1":
|
||||
from .gateway import call
|
||||
d = call("photo.space")
|
||||
out, err = d.get("size"), d.get("error")
|
||||
else:
|
||||
out, err = local.pct_exec(151, ["su", "-", "postgres", "-c",
|
||||
"psql -d immich_db -tAc \"select pg_size_pretty(pg_database_size('immich_db'))\""])
|
||||
if err:
|
||||
return f"Не удалось узнать размер: {err}"
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
"""Tiny client for the Buryans Tool Gateway."""
|
||||
import json
|
||||
import urllib.request
|
||||
|
||||
BASE = "http://127.0.0.1:9137"
|
||||
|
||||
|
||||
def call(tool):
|
||||
with urllib.request.urlopen(BASE + "/v1/call?tool=" + tool, timeout=25) as r:
|
||||
d = json.loads(r.read().decode())
|
||||
body = d.get("body")
|
||||
if isinstance(body, str):
|
||||
try:
|
||||
return json.loads(body)
|
||||
except Exception:
|
||||
return {"raw": body}
|
||||
return body or {}
|
||||
Reference in new issue
Block a user