- Settings → Composer (owner + guests): an "Un-send window" slider, 1–5 s, each user's own (settings.unsendMs). The outbox stamps it on each held reply (holdMs) so a reload re-arms for that send's own window; the spawn's undo window keeps a held send's length if the setting moves mid-window. - Owner only: "Un-send sweep", @gabvdl/ui's CurveEditor (keyframes + bézier handles, presets Default / Ease out / Snappy / Steady / Linear, Reset) with a replica composer under the real UnsendOverlay playing in sync. Lazy chunk: the root chunk sits under the PWA precache cap. - The owner's curve is everyone's: settings.unsendCurve (null = library default), served to guests live on /api/me → unsendCurve (backend settings/unsend.py validates it; a change pings guests' clients via the shortcut fingerprint). RichInput gets it through unsendCurve in the spawn composer and the reply box. - @gabvdl/ui ^0.46.1; OpenAPI + orval regenerated (AnimationCurve, CurvePoint). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
142 lines
5.9 KiB
Python
142 lines
5.9 KiB
Python
"""
|
|
The owner's prompt shortcuts, as every guest sees them.
|
|
|
|
There is one shortcut library: the owner's, in their settings store
|
|
(``ui-state`` ``ai-agent-settings`` → ``state.promptShortcuts``, written by
|
|
Settings → Shortcuts & setup). Each shortcut says who gets it — the runners
|
|
(``shared`` = every one, else ``runners``) and ``guests``. A line that names
|
|
lab things a guest's project doesn't have gets a separate guests-only
|
|
shortcut (no runner, ``guests``) rather than a second wording.
|
|
|
|
A guest's composer is that guest slice, **read live** on every ``/api/me``
|
|
(:func:`guest_shortcuts`), plus the guest's own personal shortcuts and their
|
|
hide / on-by-default preferences, both in their own settings store. Nothing is
|
|
copied any more: before v4 a guest got a one-time copy of a separate "starting
|
|
shortcuts" list, which froze the first time they saved any setting.
|
|
|
|
Upgrades happen on read, so an open tab still running older code can't undo
|
|
them: :func:`upgrade_owner` folds the old guest list (``users.json``
|
|
``guestDefaults.promptShortcuts``) into the library by id, and
|
|
:func:`upgrade_guest` drops a guest's frozen copies of it, keeping what they
|
|
added themselves.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import hashlib
|
|
import json
|
|
|
|
from users.store import clean_shortcuts
|
|
|
|
# The frontend's settings store (`settings.ts`: name + persist `version`).
|
|
SETTINGS_KEY, SETTINGS_VERSION = "ai-agent-settings", 4
|
|
|
|
def _parse(value: str | None) -> dict | None:
|
|
try:
|
|
blob = json.loads(value) if value else None
|
|
except ValueError:
|
|
return None
|
|
return blob if isinstance(blob, dict) and isinstance(blob.get("state"), dict) else None
|
|
|
|
|
|
def _version(blob: dict) -> int:
|
|
v = blob.get("version")
|
|
return v if isinstance(v, int) else 0
|
|
|
|
|
|
def guest_slice(library: list) -> list[dict]:
|
|
"""The shortcuts of ``library`` that guests get, in its order. A child
|
|
only stays linked when guests get it too (the composer ignores unknown
|
|
ids anyway; this keeps the payload honest)."""
|
|
picked = [sc for sc in library if isinstance(sc, dict) and sc.get("guests") is True]
|
|
ids = {sc.get("id") for sc in picked}
|
|
out = []
|
|
for sc in picked:
|
|
sc = dict(sc)
|
|
if sc.get("children"):
|
|
sc["children"] = [c for c in sc["children"] if c in ids]
|
|
out.append(sc)
|
|
return clean_shortcuts(out)
|
|
|
|
|
|
def _has_audience(lib) -> bool:
|
|
return isinstance(lib, list) and any(isinstance(sc, dict) and "guests" in sc for sc in lib)
|
|
|
|
|
|
def guest_shortcuts(deps) -> list[dict]:
|
|
"""What every guest's composer offers from the owner's library. An owner
|
|
who never opened the app since v4 still has the old separate guest list:
|
|
that one, then. A library that already says who gets what counts as
|
|
upgraded whatever its version — a tab still on pre-v4 code rewrites the
|
|
blob as v3 while keeping every field."""
|
|
blob = _parse(deps.ui_state.get(SETTINGS_KEY))
|
|
lib = (blob or {}).get("state", {}).get("promptShortcuts")
|
|
if blob is None or (_version(blob) < SETTINGS_VERSION and not _has_audience(lib)):
|
|
return deps.users.defaults()["promptShortcuts"]
|
|
return guest_slice(lib or [])
|
|
|
|
|
|
def fingerprint(value: str | None) -> str:
|
|
"""A hash of what guests get from a settings blob — the shortcut slice and
|
|
the un-send curve (``settings/unsend.py``): the owner's save only pings the
|
|
guests' clients when it changed (not on a theme switch)."""
|
|
from settings.unsend import curve_of
|
|
blob = _parse(value)
|
|
lib = (blob or {}).get("state", {}).get("promptShortcuts") or []
|
|
shared = {"shortcuts": guest_slice(lib), "unsendCurve": curve_of(value)}
|
|
return hashlib.sha1(json.dumps(shared, sort_keys=True).encode()).hexdigest()
|
|
|
|
|
|
def upgrade_owner(value: str | None, legacy: list[dict]) -> str | None:
|
|
"""Fold the pre-v4 guest list into the owner's library, so guests keep
|
|
exactly what they had: a guest shortcut the owner has word for word just
|
|
gains ``guests``; any other joins as a separate guests-only shortcut (no
|
|
runner offers it; id ``guest-<id>`` when the owner's library already uses
|
|
the id). The persist ``version`` stays as it was — the frontend's own
|
|
migration does the rest and stamps v4."""
|
|
blob = _parse(value)
|
|
if blob is None or _version(blob) >= SETTINGS_VERSION:
|
|
return value
|
|
lib = blob["state"].get("promptShortcuts")
|
|
if not isinstance(lib, list):
|
|
return value
|
|
if _has_audience(lib):
|
|
return value
|
|
lib = [dict(sc) if isinstance(sc, dict) else sc for sc in lib]
|
|
by_id = {sc.get("id"): sc for sc in lib if isinstance(sc, dict)}
|
|
for g in legacy:
|
|
mine = by_id.get(g["id"])
|
|
if mine is not None and all(mine.get(k) == g.get(k) for k in ("label", "guideline")):
|
|
mine["guests"] = True
|
|
continue
|
|
gid = g["id"] if mine is None else f"guest-{g['id']}"
|
|
if gid in by_id:
|
|
continue
|
|
add = {**g, "id": gid, "guests": True, "shared": False, "runners": []}
|
|
lib.append(add)
|
|
by_id[gid] = add
|
|
blob["state"]["promptShortcuts"] = lib
|
|
return json.dumps(blob)
|
|
|
|
|
|
def empty_guest() -> str:
|
|
"""A guest's settings before their first save: no personal shortcuts."""
|
|
return json.dumps({"state": {"promptShortcuts": [], "shortcutPrefs": {}},
|
|
"version": SETTINGS_VERSION})
|
|
|
|
|
|
def upgrade_guest(value: str | None, legacy: list[dict]) -> str | None:
|
|
"""A guest's pre-v4 shortcuts were a copy of the old guest list; from v4
|
|
their list holds only their *personal* ones. Drop the copies (by id —
|
|
the owner's version now arrives live), keep anything they added."""
|
|
blob = _parse(value)
|
|
if blob is None or _version(blob) >= SETTINGS_VERSION:
|
|
return value
|
|
lib = blob["state"].get("promptShortcuts")
|
|
if not isinstance(lib, list):
|
|
return value
|
|
copied = {g["id"] for g in legacy}
|
|
blob["state"]["promptShortcuts"] = [
|
|
sc for sc in lib if not (isinstance(sc, dict) and sc.get("id") in copied)]
|
|
return json.dumps(blob)
|