Elffuss BraidHub

utopia-ia/elffuss-claw public

main · 5.7 KiBRawPermalink
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.
6import { chromium } from 'playwright';
7import { CHROME_ARGS } from './chrome-args.mjs';
8import { mkdirSync } from 'node:fs';
9const REC = process.env.REC || new URL('../../elffuss-assets/_recording', import.meta.url).pathname;
10const OUT = process.env.OUT || REC + '/videos';
11mkdirSync(OUT, { recursive: true });
12const BASE = process.env.BASE || 'https://claw.elffuss.com';
13
14async 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
25const PROFILE = process.env.PROFILE || REC + '/profile-claw-gemma';
26mkdirSync(PROFILE, { recursive: true });
27const 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});
36await ctx.addInitScript(() => {
37 try {
38 localStorage.setItem('elffuss.welcomed', '1');
39 localStorage.setItem('elffuss.model', 'litert:gemma-e2b');
40 } catch {}
41});
42const p = ctx.pages()[0] || await ctx.newPage();
43
44await p.goto(BASE + '/', { waitUntil: 'domcontentloaded' });
45await 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).
53await 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(() => {});
59await p.goto(BASE + '/', { waitUntil: 'domcontentloaded' });
60await p.waitForTimeout(1500);
61console.log('1) Elffuss Claw open (clean state)');
62
63console.log('2) loading real Gemma-4 E2B (~2GB if not cached)…');
64let 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}
82if (!loaded) {
83 await p.screenshot({ path: OUT + '/../claw_load_fail.png' }).catch(() => {});
84 throw new Error('Gemma did NOT load in time');
85}
86console.log('3) Gemma-4 actually loaded, running in your browser');
87
88await p.click('#prompt');
89await p.type('#prompt', 'Build me a notes app with a title and content, and save them so they survive a page reload', { delay: 20 });
90await p.press('#prompt', 'Enter');
91console.log('4) asking for a real app (app.create)');
92await waitIdle(p, 150000);
93await p.waitForTimeout(2000);
94console.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).
100const permDialog = p.locator('#perm-dialog[open]');
101let 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}
109if (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}
117await p.waitForTimeout(1500);
118
119await p.click('#btn-mind');
120await p.waitForTimeout(4000);
121console.log('6) brief look at the Mind (4s)');
122await p.click('#mind-close').catch(() => {});
123await p.waitForTimeout(500);
124
125await p.evaluate(async () => { try { localStorage.removeItem('elffuss.history'); } catch {} });
126await p.goto(BASE + '/', { waitUntil: 'domcontentloaded' });
127await p.waitForTimeout(3000);
128console.log('7) clean outro');
129
130const _vid = p.video();
131await ctx.close();
132// guarda el webm directamente como el keeper que consume finalize-demos.sh
133if (_vid) { await _vid.saveAs(REC + '/claw-keeper.webm'); console.log('keeper →', REC + '/claw-keeper.webm'); }
134console.log('OK — video saved in', OUT);