- 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>
119 lines
5.0 KiB
Python
119 lines
5.0 KiB
Python
"""What opens on its own when someone opens the app — ``provides:
|
|
{"app.launch": "<action>"}``, asked by ``GET /api/plugins/app-launch``.
|
|
|
|
The app asks once per **opening** — a load, or the app coming back to the
|
|
foreground (a phone reopening the PWA) — never mid-use (``PluginLaunchHost``).
|
|
Each enabled, approved plugin active for the caller (their own installs, and
|
|
for a guest the owner's managed plugins granted to them) that declares
|
|
``provides.app.launch`` runs that action as the caller with::
|
|
|
|
{"trigger": "app.launch", "reason": "load" | "foreground", "path": "/…"}
|
|
|
|
and answers ``{}`` (nothing) or::
|
|
|
|
{"modal": {"slot": "<one of its app.modal slot ids>", "init": {…} (≤ 16 KB),
|
|
"closeAction"?: "<one of its actions>"}}
|
|
|
|
The app opens those slots in its modal one after the other (``openPluginModal``
|
|
— a host-drawn modal, so it shows on an iPhone, where plugin overlays don't).
|
|
``closeAction``: the app runs that action as the caller once the modal closes,
|
|
whichever way (the host's ✕, Escape, the scrim, or the frame's
|
|
``modal.close``), with ``{"trigger": "app.launch.closed", "init", "result"}``
|
|
— ``result`` is the frame's (``modal.close(obj)`` / its last
|
|
``modal.setResult``), ``null`` when it had none. The What's new carousel marks
|
|
the whole run seen that way, as the core's did on Skip, ✕ or Done.
|
|
|
|
The owner looking through a guest's eyes (Settings → Users → Impersonate)
|
|
gets the guest's answer — their context carries ``impersonatedBy`` so a plugin
|
|
can keep a preview from counting as the guest's visit.
|
|
|
|
One broken or slow plugin never hides the others' (each runs at most 10 s).
|
|
Nothing is cached: the answer depends on state the plugin keeps (what a guest
|
|
saw), and the app asks a few times a day.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
import logging
|
|
import re
|
|
from typing import Any
|
|
|
|
from plugins.store import OWNER_UID
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
PROVIDES = "app.launch"
|
|
REASONS = frozenset({"load", "foreground"})
|
|
MAX_INIT = 16 * 1024
|
|
TIMEOUT_S = 10
|
|
|
|
_SLOT_RE = re.compile(r"^[a-zA-Z_][a-zA-Z0-9_-]{0,47}$")
|
|
|
|
|
|
def _app_path(raw: Any) -> str:
|
|
p = str(raw or "/").strip()[:300]
|
|
if not p.startswith("/") or p.startswith("//") or "\\" in p or re.search(r"[\x00-\x1f]", p):
|
|
return "/"
|
|
return p
|
|
|
|
|
|
def clean_answer(rec: dict[str, Any], raw: Any) -> dict[str, Any] | None:
|
|
"""A provider's answer as the app may act on it, or None: the slot must be
|
|
one of the plugin's own ``app.modal`` slots, the init a small JSON object,
|
|
the close action one it declares."""
|
|
if not isinstance(raw, dict) or not isinstance(raw.get("modal"), dict):
|
|
return None
|
|
m = raw["modal"]
|
|
mf = rec.get("manifest") or {}
|
|
slot = m.get("slot")
|
|
slots = {s.get("id") for s in ((mf.get("ui") or {}).get("slots") or [])
|
|
if isinstance(s, dict) and s.get("where") == "app.modal"}
|
|
if not isinstance(slot, str) or not _SLOT_RE.match(slot) or slot not in slots:
|
|
log.warning("app.launch: %s answered slot %r, not one of its app.modal slots", rec.get("id"), slot)
|
|
return None
|
|
init = m.get("init") if m.get("init") is not None else {}
|
|
if not isinstance(init, dict):
|
|
return None
|
|
try:
|
|
if len(json.dumps(init)) > MAX_INIT:
|
|
log.warning("app.launch: %s's init is over 16 KB", rec.get("id"))
|
|
return None
|
|
except (TypeError, ValueError):
|
|
return None
|
|
out: dict[str, Any] = {"pluginId": rec["id"], "slotId": slot, "init": init}
|
|
close = m.get("closeAction")
|
|
if isinstance(close, str) and close in (mf.get("actions") or {}):
|
|
out["closeAction"] = close
|
|
return out
|
|
|
|
|
|
def launch_for(deps, caller, reason: str = "load", path: str = "/") -> list[dict[str, Any]]:
|
|
"""Every modal the caller's active plugins want opened now, in plugin id
|
|
order (``[]`` for nothing)."""
|
|
from plugins.runner import run_script
|
|
from plugins.waiting import _providers_for
|
|
|
|
uid = OWNER_UID if caller.is_owner else caller.id
|
|
payload = {"trigger": PROVIDES, "reason": reason if reason in REASONS else "load",
|
|
"path": _app_path(path)}
|
|
context = {"impersonatedBy": caller.impersonator} if getattr(caller, "impersonator", None) else None
|
|
out: list[dict[str, Any]] = []
|
|
found = sorted(_providers_for(deps, uid, PROVIDES), key=lambda p: p[0].get("id") or "")
|
|
for rec, grant in found:
|
|
mf = rec.get("manifest") or {}
|
|
act = (mf.get("actions") or {}).get((mf.get("provides") or {}).get(PROVIDES) or "")
|
|
if not act:
|
|
continue
|
|
try:
|
|
raw = run_script(deps, deps.plugin_store, uid, rec, act["script"], payload=payload,
|
|
timeout_sec=min(TIMEOUT_S, int(act.get("timeoutSec") or TIMEOUT_S)),
|
|
grant=grant, context=context)
|
|
except Exception as e: # noqa: BLE001 — one broken plugin never hides the rest
|
|
log.warning("app.launch plugin %s failed: %s", rec.get("id"), e)
|
|
continue
|
|
item = clean_answer(rec, raw)
|
|
if item:
|
|
out.append(item)
|
|
return out
|