| 1 | // Elffuss Claw demo video: real model (Gemma-4 E2B), builds a real app |
| 2 | // (app.create) that renders in the viewer, brief look at the Mind, and a |
| 3 | // clean outro. |
| 4 | // Doubles as an e2e smoke test: loads the real Gemma-4 model on WebGPU and |
| 5 | // exercises app.create end to end. |
| 6 | import { chromium } from 'playwright'; |
| 7 | import { CHROME_ARGS } from './chrome-args.mjs'; |
| 8 | import { mkdirSync } from 'node:fs'; |
| 9 | const REC = process.env.REC || new URL('../../elffuss-assets/_recording', import.meta.url).pathname; |
| 10 | const OUT = process.env.OUT || REC + '/videos'; |
| 11 | mkdirSync(OUT, { recursive: true }); |
| 12 | const BASE = process.env.BASE || 'https://claw.elffuss.com'; |
| 13 | |
| 14 | async function waitIdle(p, maxMs = 180000) { |
| 15 | const t0 = Date.now(); |
| 16 | while (Date.now() - t0 < maxMs) { |
| 17 | const busy = await p.evaluate(() => document.querySelector('#send')?.disabled).catch(() => false); |
| 18 | const thinking = await p.locator('.msg.thinking').count().catch(() => 0); |
| 19 | if (!busy && !thinking) return true; |
| 20 | await p.waitForTimeout(500); |
| 21 | } |
| 22 | return false; |
| 23 | } |
| 24 | |
| 25 | const PROFILE = process.env.PROFILE || REC + '/profile-claw-gemma'; |
| 26 | mkdirSync(PROFILE, { recursive: true }); |
| 27 | const ctx = await chromium.launchPersistentContext(PROFILE, { |
| 28 | args: ['--autoplay-policy=no-user-gesture-required', '--enable-unsafe-webgpu', ...CHROME_ARGS, |
| 29 | // WebGPU LLM inference issues long GPU dispatches; Chromium's GPU watchdog |
| 30 | // otherwise kills the GPU process mid-inference/-load → "Target page…closed". |
| 31 | '--disable-gpu-watchdog', '--disable-gpu-process-crash-limit'], |
| 32 | viewport: { width: 1440, height: 900 }, |
| 33 | locale: 'en-US', // fuerza navigator.language=en → chrome i18n + modelo en inglés |
| 34 | recordVideo: { dir: OUT, size: { width: 1440, height: 900 } }, |
| 35 | }); |
| 36 | await ctx.addInitScript(() => { |
| 37 | try { |
| 38 | localStorage.setItem('elffuss.welcomed', '1'); |
| 39 | localStorage.setItem('elffuss.model', 'litert:gemma-e2b'); |
| 40 | } catch {} |
| 41 | }); |
| 42 | const p = ctx.pages()[0] || await ctx.newPage(); |
| 43 | |
| 44 | await p.goto(BASE + '/', { waitUntil: 'domcontentloaded' }); |
| 45 | await p.waitForTimeout(1500); |
| 46 | |
| 47 | // El historial real vive en IndexedDB (db 'elffuss', store 'kv', key |
| 48 | // 'history'/'queue'/'lastDone') — NO en localStorage. Un run previo que no |
| 49 | // terminase limpio deja esa cola pendiente y el perfil persistente la |
| 50 | // arrastra al siguiente run (prompt duplicado en el vídeo). Se borra la BD |
| 51 | // entera y se recarga para arrancar en limpio, sin tocar el caché del modelo |
| 52 | // (que vive aparte, en OPFS/Cache Storage). |
| 53 | await p.evaluate(() => new Promise((res) => { |
| 54 | try { |
| 55 | const req = indexedDB.deleteDatabase('elffuss'); |
| 56 | req.onsuccess = req.onerror = req.onblocked = () => res(); |
| 57 | } catch { res(); } |
| 58 | })).catch(() => {}); |
| 59 | await p.goto(BASE + '/', { waitUntil: 'domcontentloaded' }); |
| 60 | await p.waitForTimeout(1500); |
| 61 | console.log('1) Elffuss Claw open (clean state)'); |
| 62 | |
| 63 | console.log('2) loading real Gemma-4 E2B (~2GB if not cached)…'); |
| 64 | let loaded = false; |
| 65 | { |
| 66 | const t0 = Date.now(); |
| 67 | let lastLog = 0; |
| 68 | while (Date.now() - t0 < 480000) { |
| 69 | const cls = await p.locator('#model-status').getAttribute('class').catch(() => ''); |
| 70 | // 'gpu' es el estado LISTO para modelos locales con WebGPU real (no uno |
| 71 | // intermedio) — 'on' solo se usa para proveedores externos. Ver ui.js. |
| 72 | if (cls && /\b(on|gpu)\b/.test(cls)) { loaded = true; break; } |
| 73 | const secs = Math.round((Date.now() - t0) / 1000); |
| 74 | if (secs - lastLog >= 20) { |
| 75 | lastLog = secs; |
| 76 | const statusTxt = await p.evaluate(() => document.querySelector('#model-select')?.selectedOptions?.[0]?.textContent || '').catch(() => ''); |
| 77 | console.log(` … ${secs}s waiting (model-status class="${cls}", selector="${statusTxt}")`); |
| 78 | } |
| 79 | await p.waitForTimeout(1000); |
| 80 | } |
| 81 | } |
| 82 | if (!loaded) { |
| 83 | await p.screenshot({ path: OUT + '/../claw_load_fail.png' }).catch(() => {}); |
| 84 | throw new Error('Gemma did NOT load in time'); |
| 85 | } |
| 86 | console.log('3) Gemma-4 actually loaded, running in your browser'); |
| 87 | |
| 88 | await p.click('#prompt'); |
| 89 | await p.type('#prompt', 'Build me a notes app with a title and content, and save them so they survive a page reload', { delay: 20 }); |
| 90 | await p.press('#prompt', 'Enter'); |
| 91 | console.log('4) asking for a real app (app.create)'); |
| 92 | await waitIdle(p, 150000); |
| 93 | await p.waitForTimeout(2000); |
| 94 | console.log('5) Gemma answered — app created and rendered in the viewer'); |
| 95 | |
| 96 | // puede pedir permiso (guardar la app/memoria) — a veces tarda un poco en |
| 97 | // aparecer TRAS la respuesta visible, así que se espera activamente en vez |
| 98 | // de comprobar una sola vez (si no, queda abierto y sin resolver el resto |
| 99 | // del vídeo). |
| 100 | const permDialog = p.locator('#perm-dialog[open]'); |
| 101 | let permSeen = false; |
| 102 | { |
| 103 | const t0 = Date.now(); |
| 104 | while (Date.now() - t0 < 8000) { |
| 105 | if (await permDialog.count().catch(() => 0)) { permSeen = true; break; } |
| 106 | await p.waitForTimeout(300); |
| 107 | } |
| 108 | } |
| 109 | if (permSeen) { |
| 110 | await p.waitForTimeout(500); // deja ver el diálogo un instante en el vídeo |
| 111 | await p.click('#perm-allow').catch(() => {}); |
| 112 | await p.waitForTimeout(400); |
| 113 | console.log(' (permission accepted)'); |
| 114 | } else { |
| 115 | console.log(' (no permission dialog this time)'); |
| 116 | } |
| 117 | await p.waitForTimeout(1500); |
| 118 | |
| 119 | await p.click('#btn-mind'); |
| 120 | await p.waitForTimeout(4000); |
| 121 | console.log('6) brief look at the Mind (4s)'); |
| 122 | await p.click('#mind-close').catch(() => {}); |
| 123 | await p.waitForTimeout(500); |
| 124 | |
| 125 | await p.evaluate(async () => { try { localStorage.removeItem('elffuss.history'); } catch {} }); |
| 126 | await p.goto(BASE + '/', { waitUntil: 'domcontentloaded' }); |
| 127 | await p.waitForTimeout(3000); |
| 128 | console.log('7) clean outro'); |
| 129 | |
| 130 | const _vid = p.video(); |
| 131 | await ctx.close(); |
| 132 | // guarda el webm directamente como el keeper que consume finalize-demos.sh |
| 133 | if (_vid) { await _vid.saveAs(REC + '/claw-keeper.webm'); console.log('keeper →', REC + '/claw-keeper.webm'); } |
| 134 | console.log('OK — video saved in', OUT); |