@womp/mcp-ui 0.1.20 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -131,12 +131,12 @@
131
131
  overflow: hidden;
132
132
  background: radial-gradient(120% 90% at 50% 35%, var(--stage-a), var(--stage-b));
133
133
  }
134
- .stage iframe {
134
+ .stage canvas {
135
135
  display: block;
136
136
  width: 100%;
137
137
  height: 100%;
138
- border: 0;
139
138
  }
139
+ .stage canvas[hidden] { display: none; }
140
140
  .preview {
141
141
  position: absolute;
142
142
  inset: 0;
@@ -483,7 +483,7 @@
483
483
  </div>
484
484
 
485
485
  <div class="stage">
486
- <iframe id="viewer" title="3D preview of your camera" allow="fullscreen" hidden></iframe>
486
+ <canvas id="canvas" aria-label="3D preview of your camera" hidden></canvas>
487
487
  <div class="preview" id="fallback" hidden></div>
488
488
  <div class="status" id="status" role="status">Connecting&hellip;</div>
489
489
  </div>
@@ -1,25 +1,25 @@
1
+ import { createViewer, type Script, type Viewer, type ViewerError } from "@womp/kakapo-web";
1
2
  import { $, callTool, createCard, openExternal, pushModelContext, toggleFullscreen } from "../shared/bridge";
2
3
 
3
4
  /**
4
5
  * The camera card — the OpenAI MCP's first project (swan-backend
5
6
  * docs/OPENAI_MCP.md). Brand-neutral: nothing here may say Womp.
6
7
  *
7
- * The 3D view is Carlos's hosted viewer (Kakapo on WebGPU) in a nested frame,
8
- * pointed at the scene the server renders for the design's current revision.
9
- * Two directions of change meet here:
8
+ * The 3D view is kakapo-web (Kakapo on WebGPU), bundled into the card: its
9
+ * viewer draws on the stage's canvas, from the scene the server renders for the
10
+ * design's current revision. The scene is all it fetches: its fonts and
11
+ * environment map are built in. Two directions of change meet here:
10
12
  *
11
- * - The user drags a slider in the viewer's Customize panel. The viewer only
12
- * answers `kakapo:getParams`, so the card asks every 1.5s, and a difference
13
- * is saved with `sync_camera_design` (app-only). The viewer already shows it;
14
- * no reload.
13
+ * - The user turns a shape knob in the card. The viewer shows it at once
14
+ * (`setParams`), and the change is saved with `sync_camera_design`
15
+ * (app-only).
15
16
  * - The model calls `update_camera_design`. That renders no card, so this card
16
17
  * polls `get_camera_design` and moves the viewer to the new revision when it
17
18
  * changed for any reason other than its own write.
18
19
  *
19
20
  * Colours are not viewer knobs (they live in the scene's materials), so a swatch
20
- * saves and moves the viewer to the new revision. A viewer that says it can
21
- * (`kakapo:setScene` in its ready message) swaps the revision in place, keeping
22
- * its camera and re-modelling only what changed; any other is reloaded. Each part
21
+ * saves and moves the viewer to the new revision: `setScene` swaps it in place,
22
+ * keeping its camera and re-modelling only what changed. Each part
23
23
  * is solid or a gradient of 2-3 colours, turned 45° at a time by the arrow; the
24
24
  * colours come from the palette, the native colour picker, or the eyedropper
25
25
  * where the browser has one.
@@ -27,9 +27,8 @@ import { $, callTool, createCard, openExternal, pushModelContext, toggleFullscre
27
27
  * Inline, the card is the render, the two colours, the price, Customize and
28
28
  * Order. Customize goes fullscreen, where the same card grows every control:
29
29
  * gradients, the picker, and the shape knobs under plain names. The card owns
30
- * those controls; the viewer is asked to hide its own panel and Print button
31
- * (`embed=1`). Until it takes `kakapo:setParams`, a knob turned here saves and
32
- * swaps in the new revision, the way a colour does.
30
+ * those controls; the viewer only draws. Without a viewer, a knob turned here
31
+ * saves and the drawing shows the new revision, the way a colour does.
33
32
  *
34
33
  * Order gets the quote from `checkout_camera` and opens its checkout link on
35
34
  * the web, all from here; paying happens there. In the chat, "order it" makes
@@ -56,8 +55,6 @@ interface DesignOption {
56
55
  interface Design {
57
56
  designId: string;
58
57
  rev: number;
59
- viewerUrl: string;
60
- viewerOrigin: string;
61
58
  sceneUrl: string;
62
59
  options: DesignOption[];
63
60
  /** Each part's colours: one is solid, more is a gradient. */
@@ -94,32 +91,20 @@ const EyeDropper = (window as Window & { EyeDropper?: EyeDropperCtor }).EyeDropp
94
91
  /** Marks this card's own writes, so its polling does not reload the viewer for them. */
95
92
  const writer = `card-${Math.random().toString(36).slice(2, 10)}`;
96
93
 
97
- const viewer = $("viewer") as HTMLIFrameElement;
94
+ const canvas = $("canvas") as HTMLCanvasElement;
98
95
  const fallback = $("fallback");
99
96
 
100
97
  let design: Design | undefined;
101
98
  /** Revision the viewer is showing. */
102
99
  let loadedRev = 0;
103
- let viewerReady = false;
104
- /** The design the viewer frame was loaded for. */
100
+ let viewer: Viewer | undefined;
101
+ /** The design the viewer was created for. */
105
102
  let viewerDesignId: string | undefined;
106
- /** Whether the viewer takes a new revision by message instead of a reload. */
107
- let viewerCanSwap = false;
108
- /** Whether the viewer takes knob values by message, so a knob needs no new scene. */
109
- let viewerCanSetParams = false;
110
- /** Where to post to the viewer: its origin, or "*" when it speaks as "null" (an opaque host). */
111
- let viewerTarget = "*";
112
- /** The scene URL a swap is waiting on, and the reload that stands in if it never lands. */
113
- let pendingSwap: string | undefined;
114
- let swapTimer: number | undefined;
115
- /** Waiting for the viewer's next params message. */
116
- const paramsWaiters: (() => void)[] = [];
117
- /** Last knob values read back from the viewer. */
103
+ /** Counts viewer starts, so a slow one that a newer start overtook is dropped. */
104
+ let viewerRequest = 0;
118
105
  let viewerParams: Record<string, number> | undefined;
119
106
  let webgpu: boolean | undefined;
120
- let syncTimer: number | undefined;
121
107
  let syncing: Promise<void> | undefined;
122
- let readyTimer: number | undefined;
123
108
  let pollTimer: number | undefined;
124
109
  let lastActivity = Date.now();
125
110
  let statusTimer: number | undefined;
@@ -142,6 +127,11 @@ const { app } = createCard("Camera", {
142
127
  if (typeof data.designId !== "string") return;
143
128
  adopt(data as Design);
144
129
  },
130
+ // The host is removing the card: release the GPU device and stop asking the server.
131
+ onTeardown: () => {
132
+ clearTimeout(pollTimer);
133
+ closeViewer();
134
+ },
145
135
  });
146
136
 
147
137
  /** A design arrived from the tool call that rendered this card. */
@@ -182,49 +172,78 @@ async function loadViewer(note?: string) {
182
172
  setStatus("");
183
173
  return;
184
174
  }
185
- if (viewerReady && viewerCanSwap && viewerDesignId === design.designId) swapScene(design.sceneUrl, note);
186
- else reloadViewer(note);
175
+ if (viewer && viewerDesignId === design.designId) await moveViewer(viewer, design.sceneUrl, note);
176
+ else await startViewer(note);
187
177
  }
188
178
 
189
- /** Hand the viewer another revision of the design on screen. */
190
- function swapScene(url: string, note?: string) {
191
- pendingSwap = url;
179
+ async function moveViewer(current: Viewer, url: string, note?: string) {
192
180
  setStatus(note ?? "Updating the 3D view…");
193
- viewer.contentWindow?.postMessage({ type: "kakapo:setScene", url }, viewerTarget);
194
- clearTimeout(swapTimer);
195
- swapTimer = window.setTimeout(() => {
196
- if (pendingSwap === url) reloadViewer(note);
197
- }, 15_000);
181
+ try {
182
+ if (!(await current.setScene(url)) || current !== viewer) return;
183
+ viewerParams = flatten(current.getScripts());
184
+ setStatus("");
185
+ } catch {
186
+ if (current === viewer) await startViewer(note);
187
+ }
198
188
  }
199
189
 
200
- function reloadViewer(note?: string) {
190
+ async function startViewer(note?: string) {
201
191
  if (!design) return;
202
- viewerReady = false;
203
- viewerCanSwap = false;
204
- viewerCanSetParams = false;
205
- viewerParams = undefined;
206
- viewerDesignId = design.designId;
207
- pendingSwap = undefined;
208
- clearTimeout(swapTimer);
209
- viewer.hidden = false;
210
- // The drawing stands in until the viewer says it is ready.
192
+ const d = design;
193
+ const request = ++viewerRequest;
194
+ closeViewer();
195
+ canvas.hidden = false;
211
196
  drawPreview(false);
212
- // The card owns the controls: ask the viewer to drop its own panel and Print button.
213
- const src = new URL(design.viewerUrl);
214
- src.searchParams.set("embed", "1");
215
- viewer.src = src.toString();
216
197
  setStatus(note ?? "Loading the 3D viewer…");
217
- clearTimeout(readyTimer);
218
- readyTimer = window.setTimeout(() => {
219
- if (viewerReady) return;
220
- setStatus("The 3D viewer hasn't loaded — this app may not allow it here.");
221
- report("viewer-timeout");
222
- }, 25_000);
198
+ let modelled = false;
199
+ try {
200
+ const next = await createViewer(canvas, {
201
+ sceneUrl: d.sceneUrl,
202
+ onProgress: ({ built, total }) => {
203
+ if (modelled || request !== viewerRequest) return;
204
+ modelled = built === total;
205
+ if (modelled) {
206
+ fallback.hidden = true;
207
+ setStatus("");
208
+ }
209
+ },
210
+ onError: (e) => {
211
+ if (e.code === "device-lost") {
212
+ showFallback();
213
+ setStatus("The 3D view stopped. Reopen the card to restart it.");
214
+ }
215
+ report(`viewer-${e.code}`, e.message);
216
+ },
217
+ });
218
+ if (request !== viewerRequest) {
219
+ next.destroy();
220
+ return;
221
+ }
222
+ viewer = next;
223
+ viewerDesignId = d.designId;
224
+ viewerParams = flatten(next.getScripts());
225
+ report("viewer-ready");
226
+ } catch (e) {
227
+ if (request !== viewerRequest) return;
228
+ const err = e as ViewerError;
229
+ closeViewer();
230
+ if (err.code === "no-webgpu") webgpu = false;
231
+ showFallback();
232
+ setStatus(err.code === "no-webgpu" ? "" : "The 3D view couldn't load. You can still change the design and order it.");
233
+ report(`viewer-${err.code ?? "failed"}`, String(err.message ?? err));
234
+ }
235
+ }
236
+
237
+ function closeViewer() {
238
+ viewer?.destroy();
239
+ viewer = undefined;
240
+ viewerDesignId = undefined;
241
+ viewerParams = undefined;
223
242
  }
224
243
 
225
244
  function showFallback() {
226
245
  if (!design) return;
227
- viewer.hidden = true;
246
+ canvas.hidden = true;
228
247
  drawPreview(true);
229
248
  }
230
249
 
@@ -279,96 +298,16 @@ function cameraSvg(d: Design) {
279
298
  );
280
299
  }
281
300
 
282
- /** The viewer reports `scripts: [{ nodeId, name, params: { knob: value } }]`; knob names are unique across them. */
283
- function flatten(scripts: unknown): Record<string, number> {
301
+ function flatten(scripts: Script[]): Record<string, number> {
284
302
  const out: Record<string, number> = {};
285
- if (!Array.isArray(scripts)) return out;
286
303
  for (const script of scripts) {
287
- const params = (script as { params?: Record<string, unknown> })?.params ?? {};
288
- for (const [name, value] of Object.entries(params)) {
289
- if (typeof value === "number" && Number.isFinite(value)) out[name] = value;
304
+ for (const p of script.params) {
305
+ if (typeof p.value === "number" && Number.isFinite(p.value)) out[p.name] = p.value;
290
306
  }
291
307
  }
292
308
  return out;
293
309
  }
294
310
 
295
- /**
296
- * Where the viewer's messages come from. A host that frames the card on an
297
- * opaque origin (a sandbox without allow-same-origin) passes that sandbox down
298
- * to the viewer's frame, which then speaks as "null" — still our frame, as
299
- * `event.source` proves.
300
- */
301
- const fromViewer = (event: MessageEvent, d: Design) =>
302
- event.source === viewer.contentWindow && (event.origin === d.viewerOrigin || event.origin === "null");
303
-
304
- window.addEventListener("message", (event) => {
305
- if (!design || !fromViewer(event, design)) return;
306
- const msg = event.data as { type?: string; scripts?: unknown; messages?: unknown; url?: string; error?: string };
307
- if (msg?.type === "kakapo:ready") {
308
- viewerReady = true;
309
- viewerTarget = event.origin === "null" ? "*" : event.origin;
310
- viewerCanSwap = Array.isArray(msg.messages) && msg.messages.includes("kakapo:setScene");
311
- viewerCanSetParams = Array.isArray(msg.messages) && msg.messages.includes("kakapo:setParams");
312
- fallback.hidden = true;
313
- clearTimeout(readyTimer);
314
- viewerParams = flatten(msg.scripts);
315
- setStatus("");
316
- report("viewer-ready");
317
- } else if (msg?.type === "kakapo:sceneSet") {
318
- if (msg.url !== pendingSwap) return; // an earlier swap, answering late
319
- pendingSwap = undefined;
320
- clearTimeout(swapTimer);
321
- if (msg.error) {
322
- reloadViewer();
323
- return;
324
- }
325
- // The revision's knob values are the viewer's now: the baseline for spotting the user's next change.
326
- viewerParams = flatten(msg.scripts);
327
- setStatus("");
328
- } else if (msg?.type === "kakapo:params") {
329
- onViewerParams(flatten(msg.scripts));
330
- for (const done of paramsWaiters.splice(0)) done();
331
- } else if (msg?.type === "kakapo:print") {
332
- // A viewer that ignores embed=1 still shows Print: treat it as Order.
333
- onViewerParams(flatten(msg.scripts));
334
- void order();
335
- }
336
- });
337
-
338
- // The viewer has no change event, so read its knobs on an interval while visible.
339
- window.setInterval(() => {
340
- if (!design || !viewerReady || document.visibilityState !== "visible") return;
341
- viewer.contentWindow?.postMessage({ type: "kakapo:getParams" }, viewerTarget);
342
- }, 1_500);
343
-
344
- /**
345
- * The viewer's knobs as they are now rather than at the last poll, for a save
346
- * the user just asked for: one made with values up to 1.5s old would put back
347
- * a slider they had just moved.
348
- */
349
- function freshViewerParams(): Promise<void> {
350
- if (!viewerReady) return Promise.resolve();
351
- return new Promise((resolve) => {
352
- paramsWaiters.push(resolve);
353
- viewer.contentWindow?.postMessage({ type: "kakapo:getParams" }, viewerTarget);
354
- window.setTimeout(resolve, 500);
355
- });
356
- }
357
-
358
- function onViewerParams(params: Record<string, number>) {
359
- if (!viewerParams) {
360
- viewerParams = params;
361
- return;
362
- }
363
- const before = viewerParams;
364
- if (!Object.keys(params).some((k) => params[k] !== before[k])) return;
365
- viewerParams = params;
366
- lastActivity = Date.now();
367
- setStatus("Saving…");
368
- clearTimeout(syncTimer);
369
- syncTimer = window.setTimeout(() => void flushSync(), 700);
370
- }
371
-
372
311
  // ---- saving -------------------------------------------------------------------
373
312
 
374
313
  /** What the card changes besides the viewer's knobs. */
@@ -382,8 +321,6 @@ interface Finish {
382
321
  * changed). Serialised, so two quick changes cannot race each other's baseRev.
383
322
  */
384
323
  async function flushSync(finish?: Finish) {
385
- clearTimeout(syncTimer);
386
- syncTimer = undefined;
387
324
  while (syncing) await syncing;
388
325
  if (!design) return;
389
326
  const baseRev = design.rev;
@@ -452,15 +389,9 @@ async function poll() {
452
389
  const next = res.structuredContent as Design | undefined;
453
390
  if (!res.isError && next && next.rev > design.rev) {
454
391
  lastActivity = Date.now();
455
- if (syncTimer !== undefined) {
456
- // A slider move is waiting to be saved: save it first, so the reload
457
- // below shows the chat's change AND the user's.
458
- await flushSync();
459
- } else {
460
- design = next;
461
- render();
462
- if (next.rev > loadedRev) void loadViewer("Updated from the chat…");
463
- }
392
+ design = next;
393
+ render();
394
+ if (next.rev > loadedRev) void loadViewer("Updated from the chat…");
464
395
  }
465
396
  }
466
397
  } catch {
@@ -552,7 +483,7 @@ function render() {
552
483
 
553
484
  $("price").textContent = design.price.label;
554
485
  $("price-inline").textContent = design.price.label;
555
- if (!fallback.hidden) drawPreview(viewer.hidden);
486
+ if (!fallback.hidden) drawPreview(canvas.hidden);
556
487
  }
557
488
 
558
489
  /** An eyedropper (Lucide `pipette`, ISC). */
@@ -677,11 +608,10 @@ async function setKnob(name: string, value: number) {
677
608
  design = { ...design, options: design.options.map((o) => (o.name === name ? { ...o, value } : o)) };
678
609
  render();
679
610
  setStatus("Saving…");
680
- await freshViewerParams();
681
611
  viewerParams = { ...(viewerParams ?? {}), [name]: value };
682
- if (viewerCanSetParams) {
612
+ if (viewer) {
683
613
  // The viewer shows it at once; the save only records it.
684
- viewer.contentWindow?.postMessage({ type: "kakapo:setParams", params: { [name]: value } }, viewerTarget);
614
+ viewer.setParams({ [name]: value });
685
615
  await flushSync();
686
616
  } else {
687
617
  // An empty finish still means "the viewer is behind": swap in the saved revision.
@@ -769,7 +699,6 @@ async function setPaint(slot: Slot, paint: string[]) {
769
699
  design = { ...design, colors: { ...design.colors, [slot]: paint } };
770
700
  render();
771
701
  setStatus("Saving…");
772
- await freshViewerParams();
773
702
  await flushSync({ colors: { [slot]: paint } });
774
703
  }
775
704
 
@@ -779,7 +708,6 @@ async function setAngle(slot: Slot, angle: number) {
779
708
  design = { ...design, gradientAngles: { ...design.gradientAngles, [slot]: angle } };
780
709
  render();
781
710
  setStatus("Saving…");
782
- await freshViewerParams();
783
711
  await flushSync({ gradientAngles: { [slot]: angle } });
784
712
  }
785
713
 
@@ -827,8 +755,7 @@ async function order() {
827
755
  button.disabled = true;
828
756
  setStatus("Opening checkout…");
829
757
  try {
830
- await freshViewerParams();
831
- if (syncTimer !== undefined || syncing) await flushSync();
758
+ await syncing;
832
759
  const res = await callTool(app, "checkout_camera", { designId: design.designId });
833
760
  const quote = res.structuredContent as Quote | undefined;
834
761
  if (res.isError || typeof quote?.checkoutUrl !== "string") {
@@ -0,0 +1,136 @@
1
+ <!doctype html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="UTF-8" />
5
+ <meta name="viewport" content="width=device-width, initial-scale=1" />
6
+ <title>Order</title>
7
+ <style>
8
+ :root {
9
+ color-scheme: light dark;
10
+ }
11
+ body {
12
+ margin: 0;
13
+ font-family: var(--font-sans, system-ui, -apple-system, "Segoe UI", sans-serif);
14
+ color: var(--color-text-primary, #1b1b1a);
15
+ }
16
+
17
+ .card {
18
+ display: grid;
19
+ gap: 14px;
20
+ max-width: 520px;
21
+ padding: 14px;
22
+ border-radius: var(--border-radius-lg, 14px);
23
+ background: var(--color-background-secondary, #f6f6f4);
24
+ border: 1px solid var(--color-border-primary, rgba(0, 0, 0, 0.1));
25
+ box-sizing: border-box;
26
+ }
27
+
28
+ .head {
29
+ display: flex;
30
+ align-items: center;
31
+ gap: 12px;
32
+ min-width: 0;
33
+ }
34
+ /* The quote card's picture of the design, from its colours alone. */
35
+ .thumb {
36
+ position: relative;
37
+ flex: none;
38
+ width: 56px;
39
+ height: 56px;
40
+ border-radius: 12px;
41
+ border: 1px solid rgba(0, 0, 0, 0.14);
42
+ background: var(--color-background-tertiary, #e8e8e4);
43
+ box-sizing: border-box;
44
+ }
45
+ .thumb .cap {
46
+ position: absolute;
47
+ top: 9px;
48
+ right: 9px;
49
+ width: 14px;
50
+ height: 14px;
51
+ border-radius: 50%;
52
+ border: 1px solid rgba(0, 0, 0, 0.2);
53
+ }
54
+ .thumb .cap:not([style]) { display: none; }
55
+ .titles { min-width: 0; }
56
+ .titles h2 {
57
+ margin: 0;
58
+ font-size: var(--font-text-md-size, 15px);
59
+ }
60
+ .titles p {
61
+ margin: 2px 0 0;
62
+ font-size: var(--font-text-sm-size, 13px);
63
+ color: var(--color-text-secondary, rgba(27, 27, 26, 0.62));
64
+ }
65
+
66
+ /* Paid → Printing → Shipped → Delivered: a bar per step, filled once reached. */
67
+ .tracker {
68
+ display: grid;
69
+ grid-template-columns: repeat(4, 1fr);
70
+ gap: 6px;
71
+ margin: 0;
72
+ padding: 0;
73
+ list-style: none;
74
+ }
75
+ .tracker li {
76
+ display: grid;
77
+ gap: 6px;
78
+ font-size: var(--font-text-xs-size, 12px);
79
+ color: var(--color-text-secondary, rgba(27, 27, 26, 0.62));
80
+ }
81
+ .tracker li::before {
82
+ content: "";
83
+ height: 4px;
84
+ border-radius: 2px;
85
+ background: var(--color-border-primary, rgba(0, 0, 0, 0.12));
86
+ }
87
+ .tracker li.done::before { background: var(--color-text-primary, #1b1b1a); }
88
+ .tracker li.now {
89
+ font-weight: 600;
90
+ color: var(--color-text-primary, #1b1b1a);
91
+ }
92
+
93
+ button.action {
94
+ font: inherit;
95
+ font-size: var(--font-text-sm-size, 13px);
96
+ font-weight: 600;
97
+ border-radius: var(--border-radius-full, 999px);
98
+ padding: 9px 14px;
99
+ cursor: pointer;
100
+ border: 1px solid var(--color-border-primary, rgba(0, 0, 0, 0.14));
101
+ background: transparent;
102
+ color: var(--color-text-primary, #1b1b1a);
103
+ }
104
+
105
+ .status {
106
+ font-size: var(--font-text-xs-size, 11px);
107
+ color: var(--color-text-secondary, rgba(27, 27, 26, 0.62));
108
+ }
109
+ .status:empty { display: none; }
110
+ [hidden] { display: none !important; }
111
+ </style>
112
+ </head>
113
+ <body>
114
+ <div class="card">
115
+ <div class="head" id="head" hidden>
116
+ <div class="thumb" id="thumb" aria-hidden="true"><span class="cap" id="cap"></span></div>
117
+ <div class="titles">
118
+ <h2 id="title"></h2>
119
+ <p id="line"></p>
120
+ </div>
121
+ </div>
122
+
123
+ <ol class="tracker" id="tracker" aria-label="Order progress" hidden>
124
+ <li data-step="paid">Paid</li>
125
+ <li data-step="printing">Printing</li>
126
+ <li data-step="shipped">Shipped</li>
127
+ <li data-step="delivered">Delivered</li>
128
+ </ol>
129
+
130
+ <button class="action" id="track" type="button" hidden></button>
131
+
132
+ <div class="status" id="status">Connecting&hellip;</div>
133
+ </div>
134
+ <script type="module" src="./main.ts"></script>
135
+ </body>
136
+ </html>