Files
ai-agent/backend/drafts/routes.py
gabrielvidalandClaude Opus 5.5 8aabb5e3b8 feat(drafts): composer drafts kept on the server, per profile, and a Drafts page
Every composer's live draft (text, tags, uploaded files) and its Save-as-draft
shelf now live on the hub (backend drafts/, /api/drafts, per user — guests
too), synced over SSE: a prompt started on the phone is waiting at the desk.
The home composer keeps one draft per profile (spawn:<profileId>), each reply
box its own (resume:<sid>). A change made on another device is taken in only
while the composer isn't being typed in (@gabvdl/ui 0.47 draftStore).

/drafts (Files → Drafts with the profile switch; a guest's Me → Drafts) lists
the unsent prompts and saved drafts — Open puts one back in its composer,
Copy, Delete with undo. The old localStorage drafts are imported once.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-06 16:46:05 +01:00

66 lines
2.2 KiB
Python

"""``/api/drafts`` — the caller's composer drafts (``drafts/store.py``).
Everyone has their own: the owner's under ``owner``, a guest's under their
id. Each change publishes SSE ``{"type": "drafts", "user", "key",
"updatedAt", "device", "deleted"?}`` — a guest's stream only carries their
own (``core/routes.py``), the owner's tabs skip the ones for another user.
"""
from __future__ import annotations
import time
from fastapi import APIRouter, HTTPException
from pydantic import BaseModel
import schemas
from core.http import _r
from core.state import State
from drafts.store import clean_device, valid_key
from users.caller import CallerDep
router = APIRouter()
class DraftBody(BaseModel):
value: str
device: str | None = None
def _key(key: str) -> str:
if not valid_key(key):
raise HTTPException(400, "bad key")
return key
@router.get("/api/drafts", responses=_r(schemas.DraftsResponse))
def list_drafts(deps: State, caller: CallerDep):
return {"drafts": deps.draft_store.list(caller.id)}
@router.get("/api/drafts/{key}", responses=_r(schemas.DraftRecord))
def get_draft(deps: State, caller: CallerDep, key: str):
rec = deps.draft_store.get(caller.id, _key(key))
if rec is None:
raise HTTPException(404, "no such draft")
return rec
@router.put("/api/drafts/{key}", responses=_r(schemas.DraftRecord))
def put_draft(deps: State, caller: CallerDep, key: str, body: DraftBody):
try:
rec = deps.draft_store.put(caller.id, _key(key), body.value, clean_device(body.device))
except ValueError as e:
raise HTTPException(413 if str(e) == "too big" else 400, str(e)) from e
deps.hub.publish({"type": "drafts", "user": caller.id, "key": rec["key"],
"updatedAt": rec["updatedAt"], "device": rec["device"]})
return rec
@router.delete("/api/drafts/{key}", responses=_r(schemas.OkResponse))
def delete_draft(deps: State, caller: CallerDep, key: str, device: str | None = None):
if deps.draft_store.delete(caller.id, _key(key)):
deps.hub.publish({"type": "drafts", "user": caller.id, "key": key,
"updatedAt": int(time.time() * 1000),
"device": clean_device(device), "deleted": True})
return {"ok": True}