Skip to content

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.

wip sweep 100728

nirholase22cac05f2a7
diff --git a/scripts/_snap-modals.tmp.mjs b/scripts/_snap-modals.tmp.mjs
new file mode 100644
index 000000000..d3d735c48
--- /dev/null
+++ b/scripts/_snap-modals.tmp.mjs
@@ -0,0 +1,65 @@
+import { chromium } from 'playwright';
+import { readFile } from 'node:fs/promises';
+
+const glb = await readFile('/workspaces/three.ws/public/avatars/default.glb');
+const browser = await chromium.launch();
+const ctx = await browser.newContext({ viewport: { width: 1440, height: 1100 }, deviceScaleFactor: 2 });
+const page = await ctx.newPage();
+page.on('pageerror', (e) => console.error('PE:', e.message));
+
+const b64 = Buffer.from(glb).toString('base64');
+await page.addInitScript((b64) => {
+ if (location.pathname !== '/create-review') return;
+ window.__seed = (async () => {
+ const bin = atob(b64), bytes = new Uint8Array(bin.length);
+ for (let i = 0; i < bin.length; i++) bytes[i] = bin.charCodeAt(i);
+ const blob = new Blob([bytes], { type: 'model/gltf-binary' });
+ const id = '19b160abcdef';
+ const db = await new Promise((res, rej) => {
+ const r = indexedDB.open('three-ws-guest', 1);
+ r.onupgradeneeded = () => { if (!r.result.objectStoreNames.contains('avatars')) r.result.createObjectStore('avatars'); };
+ r.onsuccess = () => res(r.result); r.onerror = () => rej(r.error);
+ });
+ await new Promise((res, rej) => {
+ const tx = db.transaction('avatars', 'readwrite');
+ tx.objectStore('avatars').put({ blob, meta: { source: 'avaturn' }, id, name: `Avatar #${id.slice(0,6)}`, size: blob.size, createdAt: Date.now() }, 'pending');
+ tx.oncomplete = res; tx.onerror = () => rej(tx.error);
+ });
+ db.close();
+ return true;
+ })();
+}, b64);
+
+await page.goto('http://localhost:3000/create-review', { waitUntil: 'load' });
+try { await page.evaluate(() => window.__seed); } catch {}
+
+// Wait longer so the page settles before any reload
+await page.waitForTimeout(1500);
+await page.reload({ waitUntil: 'load' });
+
+await page.waitForSelector('#mv-container canvas', { timeout: 20000 });
+await page.waitForFunction(() => document.getElementById('viewer-loading')?.classList.contains('is-hidden'));
+await page.waitForTimeout(3500);
+
+await page.fill('#f-name', 'Nicholas');
+
+async function shot(feat, file, dwell = 700) {
+ await page.click(`[data-feature="${feat}"]`);
+ await page.waitForSelector('.fm-backdrop');
+ await page.waitForTimeout(dwell);
+ const b = await page.locator('.fm-dialog').boundingBox();
+ await page.screenshot({ path: file, clip: { x: Math.max(0, b.x - 24), y: Math.max(0, b.y - 24), width: b.width + 48, height: b.height + 48 } });
+ await page.keyboard.press('Escape');
+ await page.waitForSelector('.fm-backdrop', { state: 'detached' });
+}
+
+await shot('identity', '/tmp/cr-final-identity.png', 1000);
+await shot('paid', '/tmp/cr-final-paid.png');
+await shot('embed', '/tmp/cr-final-embed.png', 1000);
+await shot('reputation', '/tmp/cr-final-reputation.png');
+await shot('download', '/tmp/cr-final-download.png');
+
+await page.screenshot({ path: '/tmp/cr-final-page.png' });
+
+await browser.close();
+console.log('saved');
diff --git a/scripts/screenshot-app-next.mjs b/scripts/screenshot-app-next.mjs
new file mode 100644
index 000000000..60a295d3b
--- /dev/null
+++ b/scripts/screenshot-app-next.mjs
@@ -0,0 +1,74 @@
+import { chromium } from 'playwright';
+
+const browser = await chromium.launch({
+ args: ['--use-gl=swiftshader', '--enable-unsafe-swiftshader'],
+});
+const page = await browser.newPage({ viewport: { width: 1440, height: 900 } });
+
+page.on('pageerror', (e) => console.log('PAGEERROR:', e.message.slice(0, 200)));
+page.on('console', (m) => {
+ if (m.type() === 'error') console.log('[err]', m.text().slice(0, 200));
+});
+
+const baseUrl = process.argv[2] || 'http://localhost:3000/app-next';
+console.log('→', baseUrl);
+
+await page.goto(baseUrl, { waitUntil: 'domcontentloaded', timeout: 30000 });
+
+await page.waitForFunction(
+ () =>
+ window.VIEWER?.viewer?.content &&
+ window.VIEWER?.viewer?.animationManager?.getAnimationDefs?.()?.length > 0,
+ { timeout: 20000 },
+);
+
+await page.evaluate(async () => {
+ const mgr = window.VIEWER.viewer.animationManager;
+ await mgr.ensureLoaded('av-waving').catch(() => {});
+ mgr.play('av-waving');
+});
+await page.waitForTimeout(2200);
+await page.screenshot({ path: '/tmp/app-next-hero.png' });
+console.log('→ /tmp/app-next-hero.png');
+
+// Animation sheet
+await page.evaluate(() => document.getElementById('nxt-anim-btn').click());
+await page.waitForTimeout(900);
+await page.screenshot({ path: '/tmp/app-next-sheet.png' });
+console.log('→ /tmp/app-next-sheet.png');
+await page.keyboard.press('Escape');
+await page.waitForTimeout(300);
+
+// Share popover
+await page.evaluate(() => document.getElementById('nxt-share-btn').click());
+await page.waitForTimeout(400);
+await page.screenshot({ path: '/tmp/app-next-share.png' });
+console.log('→ /tmp/app-next-share.png');
+await page.keyboard.press('Escape');
+await page.waitForTimeout(200);
+// Force close share
+await page.evaluate(() => document.getElementById('nxt-share-popover').setAttribute('hidden', ''));
+
+// Camera preset — wide
+await page.evaluate(() => document.querySelector('[data-preset="wide"]').click());
+await page.waitForTimeout(900);
+await page.screenshot({ path: '/tmp/app-next-wide.png' });
+console.log('→ /tmp/app-next-wide.png');
+
+// Back to body shot, then trigger a chat
+await page.evaluate(() => document.querySelector('[data-preset="body"]').click());
+await page.waitForTimeout(700);
+await page.evaluate(() => document.querySelector('.nxt-chat-chip')?.click());
+await page.waitForTimeout(2500);
+await page.screenshot({ path: '/tmp/app-next-chat.png' });
+console.log('→ /tmp/app-next-chat.png');
+
+// Help overlay
+await page.keyboard.down('Shift');
+await page.keyboard.press('/');
+await page.keyboard.up('Shift');
+await page.waitForTimeout(400);
+await page.screenshot({ path: '/tmp/app-next-help.png' });
+console.log('→ /tmp/app-next-help.png');
+
+await browser.close();
diff --git a/src/mocap-studio.js b/src/mocap-studio.js
new file mode 100644
index 000000000..acd8b86bb
--- /dev/null
+++ b/src/mocap-studio.js
@@ -0,0 +1,426 @@
+// mocap-studio.js — record, save, and replay face-mocap clips on a three.ws avatar.
+//
+// Pipeline:
+// 1. Load an avatar (handle-resolved, or the user's primary public avatar).
+// 2. Initialize FaceMocap (MediaPipe FaceLandmarker @ ~30Hz, GPU delegate).
+// 3. Calibrate to a neutral pose (subtracts resting-face baseline).
+// 4. Record / stop — uses FaceMocap's built-in clip buffer.
+// 5. Replay any saved clip via FaceMocap.playback().
+// 6. Save to /api/mocap/clips with metadata.
+// 7. List + replay anyone's public clips, plus your own private ones.
+
+import { Viewer } from './viewer.js';
+import { FaceMocap } from './face-mocap.js';
+import { IdleAnimation } from './idle-animation.js';
+
+const $ = (sel) => document.querySelector(sel);
+
+const stage = $('#stage');
+const pip = $('#webcam-pip');
+const recPill = $('#rec-pill');
+const recTime = $('#rec-time');
+const timer = $('#timer');
+
+const startCamBtn = $('#start-cam');
+const calibrateBtn = $('#calibrate');
+const recordBtn = $('#record');
+const replayBtn = $('#replay');
+const discardBtn = $('#discard');
+const saveBtn = $('#save-clip');
+const downloadBtn = $('#download-clip');
+const nameInput = $('#clip-name');
+const descInput = $('#clip-desc');
+const tagsInput = $('#clip-tags');
+const visibilityInput = $('#clip-visibility');
+const handleInput = $('#avatar-handle');
+const loadAvatarBtn = $('#load-avatar');
+const avatarInfo = $('#avatar-info');
+const clipListEl = $('#clip-list');
+const authWarning = $('#auth-warning');
+
+const state = {
+ viewer: null,
+ avatar: null, // { id, modelUrl, name, handle }
+ mocap: null,
+ idle: null,
+ lastClip: null,
+ recording: false,
+ tStart: 0,
+ rafId: 0,
+ playback: null,
+ signedIn: false,
+};
+
+// ── Bring up the Three.js scene ────────────────────────────────────────────
+const viewer = new Viewer(stage, { kiosk: true });
+viewer.renderer?.setClearAlpha(0);
+if (viewer.scene) viewer.scene.background = null;
+state.viewer = viewer;
+
+// Reasonable defaults
+handleInput.value = '';
+nameInput.value = '';
+
+// ── Auth state ────────────────────────────────────────────────────────────
+detectAuth().then((ok) => {
+ state.signedIn = ok;
+ authWarning.style.display = ok ? 'none' : 'block';
+});
+
+// ── Boot: try to load user's primary public avatar ────────────────────────
+bootAvatar().catch((err) => {
+ avatarInfo.textContent = err?.message || 'No avatar loaded — enter a handle above.';
+});
+
+refreshClipList();
+
+// ── Button wiring ─────────────────────────────────────────────────────────
+loadAvatarBtn.addEventListener('click', () => loadByHandle());
+handleInput.addEventListener('keydown', (e) => {
+ if (e.key === 'Enter') {
+ e.preventDefault();
+ loadByHandle();
+ }
+});
+
+startCamBtn.addEventListener('click', () => startMocap().catch(showErr));
+calibrateBtn.addEventListener('click', () => {
+ if (!state.mocap) return;
+ const ok = state.mocap.calibrate();
+ calibrateBtn.textContent = ok ? 'Recalibrate' : 'Calibrate (no face yet)';
+ setTimeout(() => (calibrateBtn.textContent = 'Calibrate neutral'), 1400);
+});
+recordBtn.addEventListener('click', () => {
+ if (state.recording) stopRecording();
+ else startRecording();
+});
+replayBtn.addEventListener('click', () => {
+ if (state.lastClip) replay(state.lastClip);
+});
+discardBtn.addEventListener('click', () => {
+ state.lastClip = null;
+ updateButtons();
+ timer.textContent = '00.0s';
+});
+saveBtn.addEventListener('click', () => saveClip().catch(showErr));
+downloadBtn.addEventListener('click', () => downloadClip());
+
+// ── Functions ──────────────────────────────────────────────────────────────
+
+async function detectAuth() {
+ try {
+ const r = await fetch('/api/auth/profile', { credentials: 'include' });
+ if (!r.ok) return false;
+ const data = await r.json().catch(() => null);
+ return !!(data && (data.user || data.id));
+ } catch {
+ return false;
+ }
+}
+
+async function bootAvatar() {
+ const params = new URLSearchParams(location.search);
+ const handle = params.get('handle') || params.get('h');
+ if (handle) {
+ handleInput.value = handle.replace(/^@/, '');
+ return loadByHandle();
+ }
+ // Try the signed-in user's username.
+ try {
+ const r = await fetch('/api/auth/profile', { credentials: 'include' });
+ if (r.ok) {
+ const data = await r.json();
+ const user = data.user || data;
+ if (user?.username) {
+ handleInput.value = user.username;
+ return loadByHandle();
+ }
+ }
+ } catch {}
+ avatarInfo.textContent = 'No avatar loaded — enter a handle above to load one.';
+}
+
+async function loadByHandle() {
+ const raw = (handleInput.value || '').trim().replace(/^@/, '').toLowerCase();
+ if (!/^[a-z0-9_-]{3,30}$/.test(raw)) {
+ avatarInfo.textContent = `Invalid handle: ${raw || '(empty)'}`;
+ return;
+ }
+ avatarInfo.textContent = `Loading @${raw}…`;
+ try {
+ const r = await fetch(`/api/users/${encodeURIComponent(raw)}/avatar`);
+ if (!r.ok) throw new Error(`@${raw} has no public avatar`);
+ const data = await r.json();
+ const avatar = data.avatar;
+ state.avatar = {
+ id: avatar.id,
+ modelUrl: avatar.model_url,
+ name: data.user.display_name || data.user.username,
+ handle: data.user.username,
+ };
+ await viewer.load(avatar.model_url, '', new Map());
+ // Re-attach idle + face mocap to the freshly loaded root.
+ if (state.idle) state.idle.dispose();
+ state.idle = new IdleAnimation({ getRoot: () => viewer.content, seed: state.avatar.id });
+ if (!viewer._afterAnimateHooks) viewer._afterAnimateHooks = [];
+ viewer._afterAnimateHooks.push((dt) => state.idle?.update(dt));
+ if (state.mocap) reattachMocap();
+ avatarInfo.textContent = `${state.avatar.name} · @${state.avatar.handle}`;
+ } catch (err) {
+ avatarInfo.textContent = err?.message || 'Failed to load.';
+ }
+}
+
+async function startMocap() {
+ if (state.mocap) return;
+ if (!viewer.content) throw new Error('Load an avatar first.');
+ startCamBtn.disabled = true;
+ startCamBtn.textContent = 'Starting…';
+ const mocap = new FaceMocap();
+ await mocap.init();
+ const video = await mocap.startWebcam();
+ pip.srcObject = video.srcObject;
+ pip.classList.add('live');
+ // Idle pauses saccade + blink while we drive the head.
+ state.idle?.setChannels({ saccade: false, blink: false });
+ reattachMocapRoot(mocap);
+ mocap.start();
+ state.mocap = mocap;
+ // Hook into viewer's per-frame loop for FaceMocap.update().
+ viewer._afterAnimateHooks.push(() => state.mocap?.update());
+ startCamBtn.textContent = 'Camera live';
+ startCamBtn.disabled = true;
+ calibrateBtn.disabled = false;
+ recordBtn.disabled = false;
+}
+
+function reattachMocap() {
+ if (!state.mocap || !viewer.content) return;
+ reattachMocapRoot(state.mocap);
+}
+function reattachMocapRoot(mocap) {
+ let head = null;
+ let neck = null;
+ viewer.content.traverse((node) => {
+ if (!node.isBone) return;
+ const canon = node.name
+ .replace(/^mixamorig:?/i, '')
+ .replace(/^[A-Za-z0-9]+[_:]/, '')
+ .toLowerCase();
+ if (!head && canon === 'head') head = node;
+ else if (!neck && canon === 'neck') neck = node;
+ });
+ mocap.attach(viewer.content, head || neck);
+}
+
+function startRecording() {
+ if (!state.mocap) return;
+ if (state.playback) {
+ state.playback.stop();
+ state.playback = null;
+ }
+ state.mocap.startRecording();
+ state.recording = true;
+ state.tStart = performance.now();
+ recPill.classList.add('on');
+ recordBtn.textContent = 'Stop';
+ recordBtn.classList.add('danger');
+ timer.classList.add('recording');
+ loopTimer();
+}
+
+function stopRecording() {
+ if (!state.mocap || !state.recording) return;
+ state.recording = false;
+ const clip = state.mocap.stopRecording();
+ state.lastClip = clip;
+ recPill.classList.remove('on');
+ timer.classList.remove('recording');
+ recordBtn.textContent = 'Record';
+ if (state.rafId) cancelAnimationFrame(state.rafId);
+ timer.textContent = formatTime(clip.duration || 0);
+ updateButtons();
+}
+
+function loopTimer() {
+ if (!state.recording) return;
+ const dt = (performance.now() - state.tStart) / 1000;
+ timer.textContent = formatTime(dt);
+ recTime.textContent = `${dt.toFixed(1)}s`;
+ state.rafId = requestAnimationFrame(loopTimer);
+}
+
+function replay(clip) {
+ if (!state.mocap) return;
+ if (state.playback) state.playback.stop();
+ state.playback = state.mocap.playback(clip, {
+ onEnd: () => {
+ state.playback = null;
+ },
+ });
+}
+
+function updateButtons() {
+ const has = !!state.lastClip;
+ saveBtn.disabled = !has || !state.signedIn;
+ downloadBtn.disabled = !has;
+ replayBtn.disabled = !has || !state.mocap;
+ discardBtn.disabled = !has;
+}
+
+async function saveClip() {
+ if (!state.lastClip) return;
+ const name = (nameInput.value || '').trim();
+ if (!name) {
+ alert('Give the clip a name first.');
+ nameInput.focus();
+ return;
+ }
+ const tags = (tagsInput.value || '')
+ .split(',')
+ .map((s) => s.trim().toLowerCase())
+ .filter(Boolean)
+ .slice(0, 20);
+ const payload = {
+ name,
+ description: (descInput.value || '').trim() || undefined,
+ tags,
+ visibility: visibilityInput.value,
+ avatar_id: state.avatar?.id,
+ clip: state.lastClip,
+ };
+ saveBtn.disabled = true;
+ saveBtn.textContent = 'Saving…';
+ try {
+ const r = await fetch('/api/mocap/clips', {
+ method: 'POST',
+ credentials: 'include',
+ headers: { 'content-type': 'application/json' },
+ body: JSON.stringify(payload),
+ });
+ if (!r.ok) {
+ const body = await r.json().catch(() => null);
+ throw new Error(body?.message || `save failed (${r.status})`);
+ }
+ saveBtn.textContent = 'Saved';
+ nameInput.value = '';
+ descInput.value = '';
+ tagsInput.value = '';
+ setTimeout(() => {
+ saveBtn.textContent = 'Save clip';
+ updateButtons();
+ }, 1400);
+ refreshClipList();
+ } catch (err) {
+ saveBtn.disabled = false;
+ saveBtn.textContent = 'Save clip';
+ alert(err?.message || 'Save failed.');
+ }
+}
+
+function downloadClip() {
+ if (!state.lastClip) return;
+ const blob = new Blob([JSON.stringify(state.lastClip, null, 2)], {
+ type: 'application/json',
+ });
+ const url = URL.createObjectURL(blob);
+ const a = document.createElement('a');
+ a.href = url;
+ a.download = `three-ws-mocap-${Date.now()}.json`;
+ document.body.appendChild(a);
+ a.click();
+ a.remove();
+ setTimeout(() => URL.revokeObjectURL(url), 5000);
+}
+
+async function refreshClipList() {
+ clipListEl.innerHTML = '<div class="empty">Loading…</div>';
+ try {
+ const r = await fetch('/api/mocap/clips?include_public=true&limit=30', {
+ credentials: 'include',
+ });
+ if (!r.ok) throw new Error(`list failed (${r.status})`);
+ const { items } = await r.json();
+ if (!items || items.length === 0) {
+ clipListEl.innerHTML = '<div class="empty">No clips saved yet. Record one above.</div>';
+ return;
+ }
+ clipListEl.innerHTML = '';
+ for (const c of items) {
+ const el = document.createElement('div');
+ el.className = 'clip';
+ el.innerHTML = `
+ <div class="meta">
+ <div class="n">${escapeHtml(c.name)} <span class="v-pill ${escapeAttr(c.visibility)}">${escapeHtml(c.visibility)}</span></div>
+ <div class="d">${escapeHtml(c.kind)} · ${formatTime((c.duration_ms || 0) / 1000)} · ${c.frame_count} frames${c.owner === 'self' ? '' : ' · public'}</div>
+ </div>
+ <div class="actions">
+ <button data-act="play" data-id="${escapeAttr(c.id)}">Replay</button>
+ ${c.owner === 'self' ? `<button data-act="delete" data-id="${escapeAttr(c.id)}">Delete</button>` : ''}
+ </div>
+ `;
+ clipListEl.appendChild(el);
+ }
+ clipListEl.querySelectorAll('button[data-act]').forEach((btn) => {
+ btn.addEventListener('click', () => {
+ const id = btn.dataset.id;
+ if (btn.dataset.act === 'play') playSavedClip(id);
+ else if (btn.dataset.act === 'delete') deleteSavedClip(id);
+ });
+ });
+ } catch (err) {
+ clipListEl.innerHTML = `<div class="empty">${escapeHtml(err?.message || 'Could not load clips.')}</div>`;
+ }
+}
+
+async function playSavedClip(id) {
+ if (!state.mocap) {
+ alert('Start the camera first so the playback driver is attached.');
+ return;
+ }
+ try {
+ const r = await fetch(`/api/mocap/clips/${encodeURIComponent(id)}`, { credentials: 'include' });
+ if (!r.ok) throw new Error(`fetch failed (${r.status})`);
+ const { clip } = await r.json();
+ replay({ format: clip.format, duration: clip.duration, frames: clip.frames });
+ } catch (err) {
+ alert(err?.message || 'Could not load clip.');
+ }
+}
+
+async function deleteSavedClip(id) {
+ if (!confirm('Delete this clip?')) return;
+ try {
+ const r = await fetch(`/api/mocap/clips/${encodeURIComponent(id)}`, {
+ method: 'DELETE',
+ credentials: 'include',
+ });
+ if (!r.ok) throw new Error(`delete failed (${r.status})`);
+ refreshClipList();
+ } catch (err) {
+ alert(err?.message || 'Could not delete.');
+ }
+}
+
+function formatTime(seconds) {
+ const s = Math.max(0, Number(seconds) || 0);
+ return `${s.toFixed(1)}s`;
+}
+
+function showErr(err) {
+ console.error('[mocap-studio]', err);
+ alert(err?.message || String(err));
+ startCamBtn.disabled = false;
+ startCamBtn.textContent = 'Start camera';
+}
+
+function escapeHtml(s) {
+ if (s == null) return '';
+ return String(s)
+ .replace(/&/g, '&amp;')
+ .replace(/</g, '&lt;')
+ .replace(/>/g, '&gt;')
+ .replace(/"/g, '&quot;')
+ .replace(/'/g, '&#39;');
+}
+function escapeAttr(s) { return escapeHtml(s); }