From c7be3583e14085745b0cfc4fba3458224221a932 Mon Sep 17 00:00:00 2001 From: architect Date: Sun, 29 Nov 2026 22:07:37 +0300 Subject: [PATCH] [STAGE5] Migrate BookBot to gateway local-tools --- docs/architecture/G5-MIGRATION.md | 6 +++++- docs/architecture/REGISTRY/tool_gateway.yaml | 12 +++++++++++ docs/architecture/STAGE-5.md | 2 +- ops/ai/tool_gateway_server.py | 13 ++++++++++-- ops/bots/hub/bots.py | 21 +++++++++++++++++--- ops/bots/hub/gateway.py | 8 ++++++-- 6 files changed, 53 insertions(+), 9 deletions(-) diff --git a/docs/architecture/G5-MIGRATION.md b/docs/architecture/G5-MIGRATION.md index f45398d..2519230 100644 --- a/docs/architecture/G5-MIGRATION.md +++ b/docs/architecture/G5-MIGRATION.md @@ -24,7 +24,11 @@ direct 127.0.0.1:9120 → blocked - `PhotoBot` мигрирован на gateway local-tools (`photo.stats`, `photo.space`); - feature flag `HUB_GATEWAY_PHOTO=1` включён; -- код и env имеют backup; hub-bots active, 9 ботов на связи. +- `BookBot` мигрирован на gateway (`book.stats`, `book.recent`, `book.search`); +- feature flag `HUB_GATEWAY_BOOK=1` включён; +- `book.stats` через gateway: 41 книга; +- `book.recent/search` падают на `/opt/books/...` — путь сломан и в прямом режиме; +- hub-bots active, 9 ботов на связи. ## Порядок миграции diff --git a/docs/architecture/REGISTRY/tool_gateway.yaml b/docs/architecture/REGISTRY/tool_gateway.yaml index 8131cbe..b2299b1 100644 --- a/docs/architecture/REGISTRY/tool_gateway.yaml +++ b/docs/architecture/REGISTRY/tool_gateway.yaml @@ -33,6 +33,18 @@ tools: mode: read local: photo_space approval: false + - name: book.stats + mode: read + local: book_stats + approval: false + - name: book.recent + mode: read + local: book_recent + approval: false + - name: book.search + mode: read + local: book_search + approval: false - name: audit.write gateway: http://127.0.0.1:9160/api/sprint/status mode: write diff --git a/docs/architecture/STAGE-5.md b/docs/architecture/STAGE-5.md index 104fe86..35a382f 100644 --- a/docs/architecture/STAGE-5.md +++ b/docs/architecture/STAGE-5.md @@ -27,7 +27,7 @@ GET /v1/models | S5-04 | Memory | done: SQLite namespaced memory + TTL/search, tests 3/3 | | S5-05 | Night Shift | done: timers 00:00/07:30, llm-night-start/stop | | S5-06 | Tool Gateway / agent approvals | done: agent-gw enforced + approval queue; legacy migration = G5 | -| S5-07 | Gate G5 | open | +| S5-07 | Gate G5 | in_progress: Photo+Book migrated; остальные боты direct | ## Tool Gateway runtime diff --git a/ops/ai/tool_gateway_server.py b/ops/ai/tool_gateway_server.py index acabf2b..7f1e7a4 100644 --- a/ops/ai/tool_gateway_server.py +++ b/ops/ai/tool_gateway_server.py @@ -35,7 +35,7 @@ def _photo_local(): return local -def local_call(handler): +def local_call(handler, q=""): if handler == "photo_stats": c, err = _photo_local().ImmichDb().counts() return {"ok": not bool(err), "counts": c, "error": err} @@ -43,6 +43,15 @@ def local_call(handler): 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} + if handler == "book_stats": + c, err = _photo_local().Books().counts() + return {"ok": not bool(err), "counts": c, "error": err} + if handler == "book_recent": + rows, err = _photo_local().Books().recent(8) + return {"ok": not bool(err), "rows": rows, "error": err} + if handler == "book_search": + rows, err = _photo_local().Books().search(q, 5) + return {"ok": not bool(err), "rows": rows, "error": err} raise ValueError("unknown local handler") @@ -182,7 +191,7 @@ class H(BaseHTTPRequestHandler): return self._json(404, {"error": "unknown tool"}) if t.get("mode") == "read" and t.get("local"): try: - body = local_call(t["local"]) + body = local_call(t["local"], (parse_qs(u.query).get("q") or [""])[0]) log({"tool": name, "mode": "local", "code": 200}) return self._json(200, {"tool": name, "body": body}) except Exception as e: diff --git a/ops/bots/hub/bots.py b/ops/bots/hub/bots.py index 139e8bf..fc28965 100644 --- a/ops/bots/hub/bots.py +++ b/ops/bots/hub/bots.py @@ -867,7 +867,12 @@ class BookBot(Bot): self.cmd("find", "поиск книги: `!find стругацкие`")(lambda a: self._find(books, a)) def _stats(self, books): - c, err = books.counts() + if os.environ.get("HUB_GATEWAY_BOOK") == "1": + from .gateway import call + d = call("book.stats") + c, err = d.get("counts"), d.get("error") + else: + c, err = books.counts() if err: return f"Библиотека недоступна: {err}" lines = [f"**Библиотека**\n\n• Книг: {c['books']}\n\nЧитать: https://books.buryans.ru"] @@ -877,7 +882,12 @@ class BookBot(Bot): return "\n".join(lines) def _new(self, books): - rows, err = books.recent(8) + if os.environ.get("HUB_GATEWAY_BOOK") == "1": + from .gateway import call + d = call("book.recent") + rows, err = d.get("rows"), d.get("error") + else: + rows, err = books.recent(8) if err: return f"Не удалось получить список: {err}" if not rows: @@ -890,7 +900,12 @@ class BookBot(Bot): def _find(self, books, term): if not term: return "Что искать? `!find пелевин`" - rows, err = books.search(term) + if os.environ.get("HUB_GATEWAY_BOOK") == "1": + from .gateway import call + d = call("book.search", {"q": term}) + rows, err = d.get("rows"), d.get("error") + else: + rows, err = books.search(term) if err: return f"Поиск не сработал: {err}" if not rows: diff --git a/ops/bots/hub/gateway.py b/ops/bots/hub/gateway.py index ee0a07d..c4f7c11 100644 --- a/ops/bots/hub/gateway.py +++ b/ops/bots/hub/gateway.py @@ -1,12 +1,16 @@ """Tiny client for the Buryans Tool Gateway.""" import json +import urllib.parse 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: +def call(tool, params=None): + url = BASE + "/v1/call?tool=" + urllib.parse.quote(tool) + if params: + url += "&" + urllib.parse.urlencode(params) + with urllib.request.urlopen(url, timeout=25) as r: d = json.loads(r.read().decode()) body = d.get("body") if isinstance(body, str):