| 1 | // Elffuss Code demo video: the file tree stays visible the whole time, real |
| 2 | // tool-calling runs in sequence (read → search/locate → edit), then a brief |
| 3 | // (~4s) look at the Mind, and an outro with logo+claim. |
| 4 | // Doubles as an e2e smoke test: loads the real Gemma-4 model on WebGPU and |
| 5 | // exercises code.read / code.search / code.edit 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://code.elffuss.com'; |
| 13 | |
| 14 | async function waitIdle(p, maxMs = 180000) { |
| 15 | const t0 = Date.now(); |
| 16 | while (Date.now() - t0 < maxMs) { |
| 17 | const sending = await p.locator('#btn-send.sending').count().catch(() => 0); |
| 18 | if (!sending) return true; |
| 19 | await p.waitForTimeout(500); |
| 20 | } |
| 21 | return false; |
| 22 | } |
| 23 | |
| 24 | const PROFILE = process.env.PROFILE || REC + '/profile-code-gemma'; |
| 25 | mkdirSync(PROFILE, { recursive: true }); |
| 26 | const ctx = await chromium.launchPersistentContext(PROFILE, { |
| 27 | args: ['--autoplay-policy=no-user-gesture-required', '--enable-unsafe-webgpu', ...CHROME_ARGS, |
| 28 | // WebGPU LLM inference issues long GPU dispatches; Chromium's GPU watchdog |
| 29 | // otherwise kills the GPU process mid-inference/-load → "Target page…closed". |
| 30 | '--disable-gpu-watchdog', '--disable-gpu-process-crash-limit'], |
| 31 | viewport: { width: 1440, height: 900 }, |
| 32 | locale: 'en-US', // fuerza navigator.language=en → chrome i18n + modelo en inglés |
| 33 | recordVideo: { dir: OUT, size: { width: 1440, height: 900 } }, |
| 34 | }); |
| 35 | await ctx.addInitScript(() => { try { localStorage.setItem('elffusscode.model', 'litert:gemma-e2b'); } catch {} }); |
| 36 | const p = ctx.pages()[0] || await ctx.newPage(); |
| 37 | |
| 38 | await p.goto(BASE + '/?test-opfs', { waitUntil: 'domcontentloaded' }); |
| 39 | await p.waitForTimeout(2000); |
| 40 | await p.evaluate(async () => { |
| 41 | const root = await navigator.storage.getDirectory(); |
| 42 | for await (const name of root.keys()) await root.removeEntry(name, { recursive: true }).catch(() => {}); |
| 43 | const w = async (name, txt) => { const fh = await root.getFileHandle(name, { create: true }); const s = await fh.createWritable(); await s.write(txt); await s.close(); }; |
| 44 | await w('README.md', '# calculator\nSimple numeric utilities.'); |
| 45 | await w('calc.py', |
| 46 | 'def average(numbers):\n' + |
| 47 | ' return sum(numbers) / len(numbers) - 1 # why -1?\n\n' + |
| 48 | 'def is_prime(n):\n' + |
| 49 | ' if n < 2:\n' + |
| 50 | ' return False\n' + |
| 51 | ' for i in range(2, n):\n' + |
| 52 | ' if n % i == 0:\n' + |
| 53 | ' return False\n' + |
| 54 | ' return True\n'); |
| 55 | const ide = await import('/js/ide.js'); |
| 56 | await ide.refreshTree(root); |
| 57 | }); |
| 58 | await p.waitForTimeout(500); |
| 59 | console.log('1) project seeded (README.md + calc.py with a real bug) — tree visible'); |
| 60 | |
| 61 | console.log('2) loading real Gemma-4 E2B (~2GB if not cached)…'); |
| 62 | let loaded = false; |
| 63 | { |
| 64 | const t0 = Date.now(); |
| 65 | while (Date.now() - t0 < 300000) { |
| 66 | const cls = await p.locator('#model-dot').getAttribute('class').catch(() => ''); |
| 67 | if (cls && /\bon\b/.test(cls)) { loaded = true; break; } |
| 68 | await p.waitForTimeout(1000); |
| 69 | } |
| 70 | } |
| 71 | if (!loaded) throw new Error('Gemma did NOT load in time'); |
| 72 | console.log('3) Gemma-4 actually loaded, running in your browser'); |
| 73 | |
| 74 | await p.click('#prompt'); |
| 75 | await p.type('#prompt', 'What does calc.py do?', { delay: 25 }); |
| 76 | await p.press('#prompt', 'Enter'); |
| 77 | console.log('4) turn 1 — should read the file (code.read) before answering'); |
| 78 | await waitIdle(p, 120000); |
| 79 | await p.waitForTimeout(1200); |
| 80 | |
| 81 | await p.click('#prompt'); |
| 82 | await p.type('#prompt', 'There is a real bug in average(): it subtracts 1 too many from the result. Find it in the project and fix it with a minimal edit.', { delay: 22 }); |
| 83 | await p.press('#prompt', 'Enter'); |
| 84 | console.log('5) turn 2 — should search/locate and edit (code.search + code.edit)'); |
| 85 | await waitIdle(p, 150000); |
| 86 | await p.waitForTimeout(1500); |
| 87 | console.log('6) Gemma answered — tree still visible'); |
| 88 | |
| 89 | const fileRow = p.locator('#tree .file', { hasText: 'calc.py' }); |
| 90 | if (await fileRow.count()) { |
| 91 | await fileRow.first().click({ force: true }); |
| 92 | await p.waitForTimeout(1800); |
| 93 | console.log('7) calc.py open in the editor — the real fix is visible'); |
| 94 | } |
| 95 | await p.waitForTimeout(1500); |
| 96 | |
| 97 | await p.click('#activity img'); |
| 98 | await p.waitForTimeout(4000); |
| 99 | console.log('8) brief look at the Mind (4s)'); |
| 100 | await p.click('#mind-close').catch(() => {}); |
| 101 | await p.waitForTimeout(500); |
| 102 | |
| 103 | await p.evaluate(async () => (await import('/js/db.js')).del('kv', 'project')); |
| 104 | await p.goto(BASE + '/', { waitUntil: 'domcontentloaded' }); |
| 105 | await p.waitForTimeout(4000); |
| 106 | console.log('9) outro with logo + claim'); |
| 107 | |
| 108 | const _vid = p.video(); |
| 109 | await ctx.close(); |
| 110 | // guarda el webm directamente como el keeper que consume finalize-demos.sh |
| 111 | if (_vid) { await _vid.saveAs(REC + '/code-keeper.webm'); console.log('keeper →', REC + '/code-keeper.webm'); } |
| 112 | console.log('OK — video saved in', OUT); |