Open-source platform for 3D AI agents. Turn text or a photo into a rigged, animated GLB avatar, give it an LLM brain, memory and a wallet, and embed it anywhere with one web component. Remote MCP server, x402 payments, Three.js, browser-native.
JavaScript 3d-generationai-agentsanimationavatardigital-humangenerative-aiglbgltfimage-to-3dllmmcpmcp-servermodel-context-protocolsolanatext-to-3dthreejsweb-componentswebglwebxrx402
glance(sw): serve the Adaptive Card template from the worker alongside its data
nirholas50fd2d213d09
diff --git a/public/glance-sw.js b/public/glance-sw.jsindex d3a4a38d6..549ed116d 100644--- a/public/glance-sw.js+++ b/public/glance-sw.js@@ -6,12 +6,14 @@//// This is the Windows 11 widgets board half of Glance. The manifest's// `widgets` member declares the widget and points at the Adaptive Card-// template (/api/glance/template); the host then hands this worker the widget-// lifecycle and expects it to supply the data:+// template (/api/glance/template), but it fetches neither: it hands this+// worker the widget lifecycle and expects it to supply BOTH the card and the+// data it binds://-// widgetinstall the user pinned the widget -> render it now+// activate this worker took over -> re-render pinned slots+// widgetinstall the user pinned the widget -> register refresh, render// widgetresume the board woke up -> render from cache, refresh-// widgetclick a custom verb fired -> refresh on demand+// widgetclick the host nudged an instance -> refresh on demand// widgetuninstall the user removed it -> drop the cached payload// periodicsync the platform granted a tick -> refresh every pinned widget//@@ -24,8 +26,9 @@const GLANCE_TAG = 'agent-glance';const GLANCE_CACHE = 'threews-glance';const GLANCE_STATE_KEY = '/_glance/state';+const GLANCE_TEMPLATE_KEY = '/_glance/template';const GLANCE_DATA_URL = '/api/glance/mine';-const GLANCE_REFRESH_TAG = 'glance-refresh';+const GLANCE_TEMPLATE_URL = '/api/glance/template';// A widget board is a background surface: a request that hangs there burns the// platform's refresh budget and leaves the slot stale anyway.const GLANCE_TIMEOUT_MS = 8000;@@ -86,28 +89,60 @@ function glancePayload(body, opts = {}) {};}-async function readCachedPayload() {+async function readCachedText(key) {try {const cache = await caches.open(GLANCE_CACHE);- const hit = await cache.match(GLANCE_STATE_KEY);- return hit ? await hit.json() : null;+ const hit = await cache.match(key);+ return hit ? await hit.text() : null;} catch {return null;}}-async function writeCachedPayload(payload) {+async function writeCachedText(key, body) {try {const cache = await caches.open(GLANCE_CACHE);- await cache.put(- GLANCE_STATE_KEY,- new Response(JSON.stringify(payload), { headers: { 'content-type': 'application/json' } }),- );+ await cache.put(key, new Response(body, { headers: { 'content-type': 'application/json' } }));} catch {/* a cache write failure must never break the render */}}+async function readCachedPayload() {+ const text = await readCachedText(GLANCE_STATE_KEY);+ if (!text) return null;+ try {+ return JSON.parse(text);+ } catch {+ return null;+ }+}++async function writeCachedPayload(payload) {+ return writeCachedText(GLANCE_STATE_KEY, JSON.stringify(payload));+}++/**+ * The Adaptive Card the board renders the payload into. `msAcTemplate` is the+ * URL of the card, not the card itself, and the host fetches nothing on the+ * worker's behalf: the worker owns that fetch and hands `updateByTag` the card+ * text. It is static per deploy, so the last copy is kept beside the payload+ * and a board that wakes up offline still has something to render into.+ * @param {object|null} definition the widgetDefinition, absent on a click event+ */+async function loadTemplate(definition) {+ const url = (definition && definition.msAcTemplate) || GLANCE_TEMPLATE_URL;+ try {+ const res = await fetch(url, { signal: AbortSignal.timeout(GLANCE_TIMEOUT_MS) });+ if (!res.ok) throw new Error(`glance template ${res.status}`);+ const text = await res.text();+ await writeCachedText(GLANCE_TEMPLATE_KEY, text);+ return text;+ } catch {+ return readCachedText(GLANCE_TEMPLATE_KEY);+ }+}+/*** Fetch the live card, falling back to the last one this device saw. Returns* a payload every time: a widget slot always has something in it.@@ -136,18 +171,57 @@ function offlineHeadline(cached) {/** Push the payload into every instance of the widget the host has pinned. */async function renderGlanceWidget(widget) {- const payload = await loadPayload();- const tag = (widget && widget.definition && widget.definition.tag) || GLANCE_TAG;+ const definition = (widget && widget.definition) || null;+ const tag = (definition && definition.tag) || GLANCE_TAG;+ const [payload, template] = await Promise.all([loadPayload(), loadTemplate(definition)]);if (!self.widgets) return payload;- await self.widgets.updateByTag(tag, {- template: JSON.stringify(widget && widget.definition ? widget.definition.msAcTemplate : ''),- data: JSON.stringify(payload),- });+ // No card means nothing to bind the data to. Leave whatever the slot already+ // holds rather than blanking it; the next resume tries the fetch again.+ if (!template) return payload;+ await self.widgets.updateByTag(tag, { template, data: JSON.stringify(payload) });return payload;}+/**+ * The board refreshes nothing by itself: `update` in the manifest is a request,+ * and the worker is what honours it. The sync is registered under the widget's+ * own tag so the `periodicsync` handler can find the widget that woke it.+ */+async function registerGlanceSync(definition) {+ const sync = self.registration && self.registration.periodicSync;+ if (!sync || !definition || !('update' in definition)) return;+ try {+ const tags = await sync.getTags();+ if (tags.includes(definition.tag)) return;+ // The manifest counts seconds, the Periodic Background Sync API counts+ // milliseconds. The platform still clamps this to its own floor.+ await sync.register(definition.tag, { minInterval: Number(definition.update) * 1000 });+ } catch {+ /* the permission is the user's to grant; resume and click still refresh */+ }+}++self.addEventListener('activate', (event) => {+ // A widget pinned before this worker activated would otherwise sit empty+ // until the board next resumed it. Scoped to hosts that have the API, so a+ // browser with no widgets board pays nothing for this.+ event.waitUntil(+ (async () => {+ if (!self.widgets) return;+ await loadTemplate(null);+ const pinned = await self.widgets.matchAll({ tag: GLANCE_TAG });+ if (pinned.length) await renderGlanceWidget(pinned[0]);+ })(),+ );+});+self.addEventListener('widgetinstall', (event) => {- event.waitUntil(renderGlanceWidget(event.widget));+ event.waitUntil(+ (async () => {+ await registerGlanceSync(event.widget && event.widget.definition);+ await renderGlanceWidget(event.widget);+ })(),+ );});self.addEventListener('widgetresume', (event) => {@@ -155,14 +229,28 @@ self.addEventListener('widgetresume', (event) => {});self.addEventListener('widgetclick', (event) => {- // `refresh` is the only verb the template declares; anything else is a host- // lifecycle nudge and re-rendering is the right answer for both.+ // The card's two actions are `Action.OpenUrl`, which the host opens itself+ // without telling the worker, so anything arriving here is a lifecycle nudge+ // and re-rendering is the right answer for all of them. `event.widget` is an+ // instance rather than a definition-bearing widget, which the render handles.event.waitUntil(renderGlanceWidget(event.widget));});self.addEventListener('widgetuninstall', (event) => {event.waitUntil((async () => {+ const definition = event.widget && event.widget.definition;+ const instances = (event.widget && event.widget.instances) || [];+ const sync = self.registration && self.registration.periodicSync;+ // The last instance leaving is what ends the refresh; a board with a+ // second copy pinned still wants one.+ if (sync && definition && instances.length <= 1) {+ try {+ await sync.unregister(definition.tag);+ } catch {+ /* never registered, nothing to undo */+ }+ }try {const cache = await caches.open(GLANCE_CACHE);await cache.delete(GLANCE_STATE_KEY);@@ -174,12 +262,15 @@ self.addEventListener('widgetuninstall', (event) => {});self.addEventListener('periodicsync', (event) => {- if (event.tag !== GLANCE_REFRESH_TAG) return;+ // Other workers in this scope register their own syncs; this one is ours.+ if (event.tag !== GLANCE_TAG) return;event.waitUntil((async () => {if (!self.widgets) return;const pinned = await self.widgets.matchAll({ tag: GLANCE_TAG });- await Promise.all(pinned.map((widget) => renderGlanceWidget(widget)));+ // `updateByTag` reaches every pinned instance, so one render is the+ // whole board.+ if (pinned.length) await renderGlanceWidget(pinned[0]);})(),);});@@ -188,3 +279,4 @@ self.addEventListener('periodicsync', (event) => {// fake `self`) and for debugging from the service worker console.self.__threewsGlancePayload = glancePayload;self.__threewsGlanceRender = renderGlanceWidget;+self.__threewsGlanceTemplate = loadTemplate;