Files
ai-agent/backend/plugins/whatsnew_bridge.py
gabrielvidalandClaude Opus 5.5 d9cd099143 feat(plugins): What's new leaves the core for the whats-new plugin — provides app.launch opens a plugin's app.modal on opening the app
- app.launch: GET /api/plugins/app-launch (plugins/launch.py) asked once per
  opening (load / foreground) by PluginLaunchHost; answers open a plugin's
  app.modal slot in the host modal (iPhone-safe), closeAction runs on close
- impersonatedBy in an action's AIA_USER_CONTEXT; users.read createdAt;
  init.lang in every frame; plugin page labels through t()
- whatsnew.index bridge for App usage; Settings entries may point at plugin pages
- carryover.whats_new: the core tables -> the plugin's shared files, whatsNew:false -> autoOpen:false
- gone: backend/whatsnew, /api/whats-new*, business/whatsNew, cli/whats_new.py,
  ai-agent whats-new, users.json whatsNew in the API, the User page panel

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

82 lines
2.8 KiB
Python

"""The hub's read of the guests' What's new entries, now that What's new is a
live plugin.
``backend/whatsnew/`` kept the entries and who saw each in ``ai-agent.db``;
since 2026-11-26 the ``whats-new`` plugin owns them. Core code that still
needs the list — App usage's announcement → adoption (``telemetry/views.py``,
read by the ``app-usage`` plugin through ``telemetry.read``) — asks the plugin
that declares ``provides: {"whatsnew.index": "<action>"}``, as the owner.
Degrades quietly: with no such plugin installed, disabled or failing, the
list is ``[]`` — a hub without the plugin simply has no entries. Answers are
kept 30 s (the usage page reads it on each load).
"""
from __future__ import annotations
import logging
import threading
import time
from typing import Any
from plugins.store import OWNER_UID
log = logging.getLogger(__name__)
PROVIDES = "whatsnew.index"
TTL_S = 30.0
_lock = threading.Lock()
_cache: tuple[float, list[dict[str, Any]]] | None = None
def _provider(deps) -> tuple[dict[str, Any], dict[str, Any]] | None:
"""The owner's active plugin providing ``whatsnew.index`` → (rec, action)."""
from plugins.waiting import _providers_for
try:
found = _providers_for(deps, OWNER_UID, PROVIDES)
except Exception: # noqa: BLE001 — no plugin store (tests, early boot)
return None
for rec, _grant in found:
mf = rec.get("manifest") or {}
act = (mf.get("actions") or {}).get((mf.get("provides") or {}).get(PROVIDES) or "")
if act:
return rec, act
return None
def entries(deps) -> list[dict[str, Any]]:
"""The published entries, newest first: ``[{id, title, publishedAt,
seen}]`` (``seen``: how many guests saw it); ``[]`` without the plugin."""
global _cache
now = time.monotonic()
with _lock:
if _cache is not None and now - _cache[0] < TTL_S:
return _cache[1]
hit = _provider(deps)
if hit is None:
return []
rec, act = hit
from plugins.runner import run_script
try:
res = run_script(deps, deps.plugin_store, OWNER_UID, rec, act["script"], payload={},
timeout_sec=int(act.get("timeoutSec") or 10))
except Exception as e: # noqa: BLE001 — a broken plugin never breaks the hub
log.warning("whatsnew.index via %s failed: %s", rec.get("id"), e)
return []
rows = res.get("entries") if isinstance(res, dict) else None
out = [{"id": str(r["id"]), "title": str(r.get("title") or ""),
"publishedAt": r.get("publishedAt"), "seen": int(r.get("seen") or 0)}
for r in (rows if isinstance(rows, list) else []) if isinstance(r, dict) and r.get("id")]
with _lock:
_cache = (now, out)
return out
def invalidate() -> None:
global _cache
with _lock:
_cache = None