Files
ai-agent/backend/settings/unsend.py
gabrielvidalandClaude Opus 5.5 b46a1e433f feat(composer): edit the un-send sweep's curve and the window's length in Settings → Composer
- 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>
2026-11-05 00:42:51 +01:00

72 lines
2.7 KiB
Python

"""
The composer's un-send sweep curve — the owner's, for every user.
The owner edits it in Settings → Composer (``@gabvdl/ui``'s ``CurveEditor``);
it lives in their settings store (``ui-state`` ``ai-agent-settings`` →
``state.unsendCurve``, ``null`` = the library's default). Guests don't edit it:
their composers read it **live** from ``/api/me`` (``unsendCurve``), like the
owner's shared shortcuts (``settings/shortcuts.py``), and an owner save that
changes it pings their clients to refetch (``fingerprint``). The window's
*length* is each user's own (``state.unsendMs``), so it isn't here.
"""
from __future__ import annotations
import json
import math
SETTINGS_KEY = "ai-agent-settings"
# A curve is a handful of keyframes; anything longer isn't one the editor made.
MAX_POINTS = 32
def _num(v) -> bool:
return isinstance(v, (int, float)) and not isinstance(v, bool) and math.isfinite(v)
def clean_curve(curve) -> dict | None:
"""``curve`` as a well-formed ``AnimationCurve`` (the frontend's
``parseCurve`` rules: ≥ 2 points, x non-decreasing from 0 to 1, one
cubic-bézier per segment with its x handles in 0 → 1), else ``None``."""
if not isinstance(curve, dict):
return None
points, easings = curve.get("points"), curve.get("easings")
if not isinstance(points, list) or not isinstance(easings, list):
return None
if not 2 <= len(points) <= MAX_POINTS or len(easings) != len(points) - 1:
return None
out_p, prev = [], -math.inf
for p in points:
if not isinstance(p, dict) or not _num(p.get("x")) or not _num(p.get("y")):
return None
x, y = float(p["x"]), float(p["y"])
if x < prev or not 0 <= x <= 1 or abs(y) > 10:
return None
prev = x
out_p.append({"x": x, "y": y})
if out_p[0]["x"] != 0 or out_p[-1]["x"] != 1:
return None
out_e = []
for e in easings:
if not isinstance(e, list) or len(e) != 4 or not all(_num(v) for v in e):
return None
if not (0 <= e[0] <= 1 and 0 <= e[2] <= 1) or abs(e[1]) > 10 or abs(e[3]) > 10:
return None
out_e.append([float(v) for v in e])
return {"points": out_p, "easings": out_e}
def curve_of(value: str | None) -> dict | None:
"""The un-send curve a settings blob holds (``None``: the default)."""
try:
blob = json.loads(value) if value else None
except ValueError:
return None
state = blob.get("state") if isinstance(blob, dict) else None
return clean_curve(state.get("unsendCurve")) if isinstance(state, dict) else None
def owner_curve(deps) -> dict | None:
"""The owner's un-send curve — what every guest's composer sweeps on."""
return curve_of(deps.ui_state.get(SETTINGS_KEY))