From 0a130b2c251dfa318a06abfdd2f8b6976ea1ea5a Mon Sep 17 00:00:00 2001 From: Richie McIlroy <33632126+richiemcilroy@users.noreply.github.com> Date: Sun, 29 Nov 2026 03:29:55 +0000 Subject: [PATCH 1/2] feat(web): plan automatic demos for Cap Pro POST /api/desktop/demos/plan takes the moments of a recording and a few frames and returns a beat sheet for the desktop to compile. Whatever the model names that the recording does not have is dropped before it leaves. --- apps/web/__tests__/unit/demo-plan.test.ts | 143 ++++++++++++ apps/web/app/api/desktop/[...route]/demos.ts | 111 ++++++++++ apps/web/app/api/desktop/[...route]/route.ts | 2 + apps/web/lib/demos/beat-sheet.ts | 217 +++++++++++++++++++ apps/web/lib/demos/plan.ts | 74 +++++++ apps/web/lib/demos/prompt.ts | 88 ++++++++ 6 files changed, 635 insertions(+) create mode 100644 apps/web/__tests__/unit/demo-plan.test.ts create mode 100644 apps/web/app/api/desktop/[...route]/demos.ts create mode 100644 apps/web/lib/demos/beat-sheet.ts create mode 100644 apps/web/lib/demos/plan.ts create mode 100644 apps/web/lib/demos/prompt.ts diff --git a/apps/web/__tests__/unit/demo-plan.test.ts b/apps/web/__tests__/unit/demo-plan.test.ts new file mode 100644 index 00000000000..ee0ed5c8651 --- /dev/null +++ b/apps/web/__tests__/unit/demo-plan.test.ts @@ -0,0 +1,143 @@ +import { describe, expect, it, vi } from "vitest"; + +const generateText = vi.hoisted(() => vi.fn()); +vi.mock("ai", () => ({ generateText })); + +const sheet = await import("@/lib/demos/beat-sheet"); +const { buildDemoPrompt } = await import("@/lib/demos/prompt"); +const { planDemo, DemoPlanUnavailableError } = await import("@/lib/demos/plan"); + +const moment = (id: number, start: number, clip = 0) => ({ + id, + clip, + kind: "click" as const, + start, + end: start + 2, + at: start + 0.5, + focus: { x: 0.2, y: 0.2, w: 0.3, h: 0.3 }, + weight: 1, +}); + +const request = sheet.demoPlanRequestSchema.parse({ + goal: "Show the export", + signals: { + version: 1, + clips: [{ clip: 0, duration: 60, frameWidth: 3200, frameHeight: 2000 }], + moments: [moment(1, 2), moment(2, 10), moment(3, 20)], + }, + frames: [{ moment: 1, when: "result", jpegBase64: "AAAA" }], +}); + +describe("parseBeatSheet", () => { + it("reads a fenced answer and keeps only moments the recording has", () => { + const plan = sheet.parseBeatSheet( + '```json\n{"title":"T","look":{"theme":"dark","backdrop":["#101014","#22223a"]},"beats":[{"type":"card","text":"Hello"},{"type":"clip","moments":[3,1,99,1],"shot":"element"},{"type":"clip","moments":[42]}]}\n```', + request.signals.moments, + ); + expect(plan.look).toEqual({ + style: "demo", + theme: "dark", + backdrop: ["#101014", "#22223a"], + motion: "flat", + music: true, + }); + expect(plan.beats).toEqual([ + { type: "card", text: "Hello" }, + { type: "clip", moments: [1, 3], shot: "element", pace: "auto" }, + ]); + }); + + it("falls back on a shot or a focus it does not understand", () => { + const plan = sheet.parseBeatSheet( + '{"beats":[{"type":"clip","moments":[2],"shot":"dolly","focus":{"x":0.9,"y":0.9,"w":0.5,"h":0.5}}]}', + request.signals.moments, + ); + expect(plan.beats).toEqual([ + { type: "clip", moments: [2], shot: "auto", pace: "auto" }, + ]); + }); + + it("shows a moment only the first time it is named", () => { + const plan = sheet.parseBeatSheet( + '{"beats":[{"type":"clip","moments":[1,2]},{"type":"clip","moments":[2,3]},{"type":"clip","moments":[1]}]}', + request.signals.moments, + ); + expect( + plan.beats.map((beat) => beat.type === "clip" && beat.moments), + ).toEqual([[1, 2], [3]]); + }); + + it("refuses an answer with no usable clip", () => { + expect(() => + sheet.parseBeatSheet( + '{"beats":[{"type":"card","text":"Only words"}]}', + request.signals.moments, + ), + ).toThrow(sheet.DemoPlanInvalidError); + expect(() => + sheet.parseBeatSheet("no plan here", request.signals.moments), + ).toThrow(sheet.DemoPlanInvalidError); + }); +}); + +describe("demoPlanRequestSchema", () => { + it("refuses frames that are not base64 and too many frames", () => { + const frame = { moment: 1, when: "result", jpegBase64: "AAAA" }; + expect( + sheet.demoPlanRequestSchema.safeParse({ + ...request, + frames: [{ ...frame, jpegBase64: "