aienos.com
Source of www.aienos.com, served from my own hardware
Brought by drakestapleton
aienos.com / public / js / aien-universe.js
| 1 | /* |
| 2 | * aien-universe.js: /aien/ as the iMac shows Aien. Generated by |
| 3 | * om/aien-universe/universe.om (Omega) with the shared contract of |
| 4 | * om/allen-page/contract.om; do not edit. |
| 5 | * |
| 6 | * The page is the sky. The native scene (allen-scene.js, aienux da55660 |
| 7 | * compiled to WebAssembly) plays the two black holes merging into Aien in |
| 8 | * the middle, with the native chirp (allen-audio.js) on the first gesture. |
| 9 | * Visitors type in one glass oval under him; on Enter the letters float up |
| 10 | * into him. His reply is one bubble beside him that fades once it has been |
| 11 | * read: the time grows with its length, and hover, focus or a tap hold it. |
| 12 | * There is never more than one bubble, no transcript and no scrolling page. |
| 13 | * Things that are not chat open as glass panes (allen-panes.js): programs |
| 14 | * and cards from a reply, the speed test and the math engine. |
| 15 | * |
| 16 | * allen.js stays the transport and keeps writing every reply into the |
| 17 | * screen reader log; this script only reads that log, moves cards into |
| 18 | * panes and mirrors the newest reply into the bubble. Every string it shows |
| 19 | * goes through textContent. |
| 20 | */ |
| 21 | (() => { |
| 22 | 'use strict'; |
| 23 | const byId = id => document.getElementById(id); |
| 24 | const root = byId('allen'); |
| 25 | const sky = byId('aien-sky'); |
| 26 | if (!root || !sky) return; |
| 27 | const log = byId('allen-log'); |
| 28 | const form = byId('allen-form'); |
| 29 | const input = byId('allen-input'); |
| 30 | const examples = byId('allen-examples'); |
| 31 | const bubble = byId('aien-bubble'); |
| 32 | const bubbleWho = byId('aien-bubble-who'); |
| 33 | const bubbleText = byId('aien-bubble-text'); |
| 34 | const bubbleActs = byId('aien-bubble-acts'); |
| 35 | const panesHost = byId('aien-panes'); |
| 36 | const menu = byId('aien-menu'); |
| 37 | const replayButton = byId('aien-replay'); |
| 38 | const soundButton = byId('aien-sound'); |
| 39 | const speedButton = byId('aien-speed'); |
| 40 | const mathButton = byId('aien-math'); |
| 41 | const speedTable = menu.querySelector('dl.allen-speed'); |
| 42 | const speedHome = speedTable.nextElementSibling; |
| 43 | const MATH = '/aien/api/math'; |
| 44 | /* Reading time. 200 words a minute at 6 characters a word is 50 ms a |
| 45 | character (WolframLanguageEvaluator: UnitConvert[Quantity[1, "Minutes"]/(200*6), |
| 46 | "Milliseconds"] -> 50 ms), after a 2500 ms glance, at most 30 s. */ |
| 47 | const READ_BASE = 2500; |
| 48 | const READ_CHAR = 50; |
| 49 | const READ_MAX = 30000; |
| 50 | const FADE_MS = 900; |
| 51 | /* Panes this page opened that stay open at once; the oldest closes first. */ |
| 52 | const PANES_SHOWN = 3; |
| 53 | /* The opening is the iMac's own order: the tube (native dusk, 4200 ms), |
| 54 | then the two holes merge (native night: merger ends at 12300 ms) and |
| 55 | become Aien (formed at 15300 ms). For the web the night's first 6800 ms |
| 56 | (stars, lens, shadows) play 6.8 times faster, 1000 ms, then the spiral, |
| 57 | merger and forming run at the native rate. With sound on the night runs |
| 58 | at the native rate throughout, so the chirp stays on the merger. |
| 59 | WolframLanguageEvaluator: {6800/1000, 4200 + 1000 + (15300 - 6800)} -> {34/5, 13700} ms. */ |
| 60 | const TUBE_MS = 4200; |
| 61 | const WARP_END = 6800; |
| 62 | const WARP_RATE = 6.8; |
| 63 | const FORMED_MS = 15300; |
| 64 | let scene = null; |
| 65 | let formed = false; |
| 66 | let stage = 'none'; |
| 67 | let warpFrom = 0; |
| 68 | let watching = 0; |
| 69 | let fallback = 0; |
| 70 | const still = matchMedia('(prefers-reduced-motion: reduce)'); |
| 71 | function formUp() { |
| 72 | if (formed) return; |
| 73 | formed = true; |
| 74 | stage = 'formed'; |
| 75 | clearTimeout(fallback); |
| 76 | root.dataset.formed = 'yes'; |
| 77 | mirror(); |
| 78 | } |
| 79 | function sceneTime() { |
| 80 | const at = Number(sky.dataset.sceneTime); |
| 81 | return Number.isFinite(at) && at >= 0 ? at : 0; |
| 82 | } |
| 83 | function sceneCall(name, arg) { |
| 84 | if (!scene) return; |
| 85 | try { |
| 86 | scene[name](arg); |
| 87 | } catch (_) { |
| 88 | /* The sky is decoration; the conversation never depends on it. */ |
| 89 | } |
| 90 | } |
| 91 | /* The night: the two holes. Warped while no sound is armed. */ |
| 92 | function night() { |
| 93 | sceneCall('enter', 'birth'); |
| 94 | root.dataset.opening = 'night'; |
| 95 | if (armed) { |
| 96 | stage = 'night'; |
| 97 | if (audio) audio.resume(); |
| 98 | } else { |
| 99 | stage = 'warp'; |
| 100 | warpFrom = performance.now(); |
| 101 | } |
| 102 | clearTimeout(fallback); |
| 103 | fallback = setTimeout(formUp, FORMED_MS + 1500); |
| 104 | } |
| 105 | /* Hand the clock back to the native loop at the native rate. */ |
| 106 | function unwarp(at) { |
| 107 | stage = 'night'; |
| 108 | sceneCall('frame', at); |
| 109 | sceneCall('mode', root.dataset.busy === 'true' ? 'thinking' : 'idle'); |
| 110 | } |
| 111 | function watch(now) { |
| 112 | watching = 0; |
| 113 | if (!scene || stage === 'formed' || stage === 'none') return; |
| 114 | if (stage === 'tube' && sceneTime() >= TUBE_MS) night(); |
| 115 | else if (stage === 'warp') { |
| 116 | const at = (now - warpFrom) * WARP_RATE; |
| 117 | if (at >= WARP_END) unwarp(WARP_END); |
| 118 | else sceneCall('frame', at); |
| 119 | } |
| 120 | if (stage !== 'night' && stage !== 'formed') watching = requestAnimationFrame(watch); |
| 121 | } |
| 122 | function opening() { |
| 123 | formed = false; |
| 124 | root.dataset.formed = 'no'; |
| 125 | root.dataset.opening = 'tube'; |
| 126 | stage = 'tube'; |
| 127 | clearTimeout(fallback); |
| 128 | fallback = setTimeout(formUp, TUBE_MS + FORMED_MS + 3000); |
| 129 | if (!watching) watching = requestAnimationFrame(watch); |
| 130 | } |
| 131 | function startScene() { |
| 132 | if (!window.AllenScene) { |
| 133 | root.dataset.sky = 'off'; |
| 134 | formUp(); |
| 135 | return; |
| 136 | } |
| 137 | try { |
| 138 | scene = window.AllenScene.mount(sky, { opening: still.matches ? 'idle' : 'dusk', interactive: false }); |
| 139 | } catch (_) { |
| 140 | scene = null; |
| 141 | root.dataset.sky = 'off'; |
| 142 | formUp(); |
| 143 | return; |
| 144 | } |
| 145 | if (scene.ready && typeof scene.ready.then === 'function') { |
| 146 | scene.ready.then(() => { |
| 147 | root.dataset.sky = 'on'; |
| 148 | }, () => { |
| 149 | root.dataset.sky = 'off'; |
| 150 | formUp(); |
| 151 | }); |
| 152 | } |
| 153 | sky.addEventListener('allen-scene-complete', event => { |
| 154 | if (event.detail && event.detail.kind === 'birth') formUp(); |
| 155 | }); |
| 156 | sky.addEventListener('allen-scene-error', () => { |
| 157 | root.dataset.sky = 'off'; |
| 158 | formUp(); |
| 159 | }); |
| 160 | if (still.matches) formUp(); |
| 161 | else opening(); |
| 162 | } |
| 163 | const audio = window.AllenAudio || null; |
| 164 | let armed = false; |
| 165 | /* Browsers start sound only inside a gesture. The first click or key on |
| 166 | the page arms the native chirp: during the tube it waits, paused, for |
| 167 | the night to begin; during the night it joins where the scene is, and a |
| 168 | warped night drops to the native rate so the chirp lands on the merger. */ |
| 169 | function firstGesture() { |
| 170 | if (armed || !audio || audio.muted()) return; |
| 171 | armed = true; |
| 172 | try { |
| 173 | if (stage === 'tube') { |
| 174 | audio.enter({ kind: 'night', nightMs: 0 }); |
| 175 | audio.pause(); |
| 176 | } else if (stage === 'warp') { |
| 177 | const at = Math.min(WARP_END, (performance.now() - warpFrom) * WARP_RATE); |
| 178 | unwarp(at); |
| 179 | audio.enter({ kind: 'night', nightMs: at }); |
| 180 | } else if (stage === 'night') { |
| 181 | audio.enter({ kind: 'night', nightMs: sceneTime() }); |
| 182 | } |
| 183 | } catch (_) {} |
| 184 | } |
| 185 | function showSound() { |
| 186 | const on = !(audio && audio.muted()); |
| 187 | soundButton.textContent = on ? 'Sound: on' : 'Sound: off'; |
| 188 | soundButton.setAttribute('aria-pressed', on ? 'true' : 'false'); |
| 189 | soundButton.hidden = !audio; |
| 190 | } |
| 191 | /* Replay plays the whole opening again, tube first, with the sound armed |
| 192 | by this click. */ |
| 193 | function replay() { |
| 194 | menu.open = false; |
| 195 | hideBubble(true); |
| 196 | if (!scene || still.matches) return; |
| 197 | if (audio) audio.stop(); |
| 198 | armed = false; |
| 199 | sceneCall('enter', 'dusk'); |
| 200 | opening(); |
| 201 | firstGesture(); |
| 202 | } |
| 203 | /* One bubble, never a scroll: a long reply turns pages inside the same |
| 204 | bubble, each held for its own reading time. */ |
| 205 | const PAGE_CHARS = 180; |
| 206 | let watched = null; |
| 207 | let fadeTimer = 0; |
| 208 | let goneTimer = 0; |
| 209 | let pending = 0; |
| 210 | let held = false; |
| 211 | let fullText = ''; |
| 212 | let pageAt = 0; |
| 213 | let armedFor = ''; |
| 214 | /* The message whose bubble was let go; it does not come back. */ |
| 215 | let dismissed = null; |
| 216 | function clearTimers() { |
| 217 | clearTimeout(fadeTimer); |
| 218 | clearTimeout(goneTimer); |
| 219 | fadeTimer = 0; |
| 220 | goneTimer = 0; |
| 221 | armedFor = ''; |
| 222 | } |
| 223 | function hideBubble(now) { |
| 224 | dismissed = watched; |
| 225 | clearTimers(); |
| 226 | if (bubble.hidden) return; |
| 227 | if (now) { |
| 228 | bubble.hidden = true; |
| 229 | bubble.dataset.state = 'gone'; |
| 230 | return; |
| 231 | } |
| 232 | bubble.dataset.state = 'fading'; |
| 233 | goneTimer = setTimeout(() => { |
| 234 | bubble.hidden = true; |
| 235 | bubble.dataset.state = 'gone'; |
| 236 | }, FADE_MS); |
| 237 | } |
| 238 | function readMs(text) { |
| 239 | return Math.min(READ_MAX, READ_BASE + READ_CHAR * text.length); |
| 240 | } |
| 241 | function pagesOf(text) { |
| 242 | const pages = []; |
| 243 | let rest = text.trim(); |
| 244 | while (rest.length > PAGE_CHARS) { |
| 245 | let cut = rest.lastIndexOf(' ', PAGE_CHARS); |
| 246 | const para = rest.lastIndexOf('\n', PAGE_CHARS); |
| 247 | if (para > PAGE_CHARS / 3) cut = para; |
| 248 | else if (cut < PAGE_CHARS / 3) cut = PAGE_CHARS; |
| 249 | pages.push(rest.slice(0, cut).trim()); |
| 250 | rest = rest.slice(cut).trim(); |
| 251 | } |
| 252 | if (rest) pages.push(rest); |
| 253 | return pages; |
| 254 | } |
| 255 | function paint() { |
| 256 | const pages = pagesOf(fullText); |
| 257 | if (pageAt > pages.length - 1) pageAt = Math.max(0, pages.length - 1); |
| 258 | const page = pages[pageAt] || ''; |
| 259 | const last = pageAt >= pages.length - 1; |
| 260 | if (bubbleText.textContent !== page) bubbleText.textContent = page; |
| 261 | bubble.dataset.more = last ? 'no' : 'yes'; |
| 262 | bubbleActs.hidden = !last; |
| 263 | return { page, last }; |
| 264 | } |
| 265 | /* A finished page turns after its reading time; the last page fades once |
| 266 | the reply is complete, unless it asks a question with buttons. */ |
| 267 | function schedule() { |
| 268 | if (bubble.hidden || bubble.dataset.state === 'fading') return; |
| 269 | const { page, last } = paint(); |
| 270 | if (held) { |
| 271 | clearTimers(); |
| 272 | return; |
| 273 | } |
| 274 | let plan = ''; |
| 275 | if (!last) plan = `turn ${pageAt}`; |
| 276 | else if (root.dataset.busy !== 'true' && !bubbleActs.querySelector('button:not([disabled])')) plan = `fade ${pageAt} ${page.length}`; |
| 277 | if (plan === armedFor) return; |
| 278 | clearTimers(); |
| 279 | armedFor = plan; |
| 280 | if (!plan) return; |
| 281 | fadeTimer = setTimeout(() => { |
| 282 | armedFor = ''; |
| 283 | fadeTimer = 0; |
| 284 | if (!plan.startsWith('turn')) { |
| 285 | hideBubble(false); |
| 286 | return; |
| 287 | } |
| 288 | pageAt += 1; |
| 289 | if (!still.matches && bubbleText.animate) bubbleText.animate([{ opacity: 0 }, { opacity: 1 }], { duration: 220, easing: 'ease-out' }); |
| 290 | schedule(); |
| 291 | }, readMs(page)); |
| 292 | } |
| 293 | function showBubble(who, text) { |
| 294 | fullText = text; |
| 295 | if (bubbleWho.textContent !== who) bubbleWho.textContent = who; |
| 296 | if (bubble.hidden || bubble.dataset.state === 'fading') { |
| 297 | clearTimers(); |
| 298 | bubble.hidden = false; |
| 299 | } |
| 300 | bubble.dataset.state = 'shown'; |
| 301 | schedule(); |
| 302 | } |
| 303 | function bodyText(body) { |
| 304 | const parts = []; |
| 305 | for (const child of body.children) { |
| 306 | if (!child.matches('p.allen-p, p.allen-h, p.allen-err, p.allen-note')) continue; |
| 307 | const text = child.textContent.trim(); |
| 308 | if (text) parts.push(text); |
| 309 | } |
| 310 | return parts.join('\n'); |
| 311 | } |
| 312 | /* The newest message from Aien or Drake becomes the bubble. Cards and code |
| 313 | move into panes as they appear, so allen.js keeps streaming into them; |
| 314 | choice buttons move into the bubble so they stay clickable. */ |
| 315 | function adopt(item, body) { |
| 316 | for (const child of [...body.children]) { |
| 317 | if (child.matches('.allen-choices')) { |
| 318 | bubbleActs.append(child); |
| 319 | } else if (child.matches('.allen-code, .allen-card, .allen-consent, .allen-out, .allen-frame-box')) { |
| 320 | paneFor(child, item); |
| 321 | } |
| 322 | } |
| 323 | } |
| 324 | function mirror() { |
| 325 | pending = 0; |
| 326 | const items = log.querySelectorAll('li.allen-msg-allen, li.allen-msg-drake'); |
| 327 | const item = items.length ? items[items.length - 1] : null; |
| 328 | if (!item) return; |
| 329 | const body = item.querySelector('.allen-body'); |
| 330 | if (!body) return; |
| 331 | if (item !== watched) { |
| 332 | watched = item; |
| 333 | bubbleActs.replaceChildren(); |
| 334 | fullText = ''; |
| 335 | pageAt = 0; |
| 336 | clearTimers(); |
| 337 | } |
| 338 | adopt(item, body); |
| 339 | if (item === dismissed) return; |
| 340 | if (!formed) return; |
| 341 | const text = bodyText(body); |
| 342 | const who = item.classList.contains('allen-msg-drake') ? 'Drake' : 'Aien'; |
| 343 | if (text || bubbleActs.childElementCount) showBubble(who, text); |
| 344 | } |
| 345 | function soon() { |
| 346 | if (!pending) pending = requestAnimationFrame(mirror); |
| 347 | } |
| 348 | let panes = null; |
| 349 | const mine = []; |
| 350 | let serial = 0; |
| 351 | function freshId(stem) { |
| 352 | serial += 1; |
| 353 | return `${stem}${Date.now().toString(36)}${serial}`; |
| 354 | } |
| 355 | function keep(entry) { |
| 356 | mine.push(entry); |
| 357 | while (mine.length > PANES_SHOWN) { |
| 358 | const old = mine.shift(); |
| 359 | try { |
| 360 | old.close(); |
| 361 | } catch (_) {} |
| 362 | } |
| 363 | } |
| 364 | function forget(id) { |
| 365 | const at = mine.findIndex(entry => entry.id === id); |
| 366 | if (at >= 0) mine.splice(at, 1); |
| 367 | if (id === speedId) { |
| 368 | speedId = ''; |
| 369 | speedHome.before(speedTable); |
| 370 | } |
| 371 | } |
| 372 | function startPanes() { |
| 373 | if (!window.AllenPanes) return; |
| 374 | try { |
| 375 | panes = window.AllenPanes.mount(panesHost, { onClose: forget, returnFocus: input }); |
| 376 | } catch (_) { |
| 377 | panes = null; |
| 378 | } |
| 379 | } |
| 380 | function titleFor(node) { |
| 381 | const lang = node.querySelector('.allen-lang'); |
| 382 | const name = lang ? lang.textContent.trim().toLowerCase() : ''; |
| 383 | if (name === 'omega' || name === 'om') return 'Omega program'; |
| 384 | if (name === 'html' || name === 'htm') return 'App'; |
| 385 | if (node.matches('.allen-consent')) return 'Remember me'; |
| 386 | if (node.matches('.allen-code')) return name ? `Code (${name})` : 'Code'; |
| 387 | const head = node.querySelector('h3, h4, strong, p'); |
| 388 | const text = head ? head.textContent.trim() : ''; |
| 389 | return text ? text.slice(0, 60) : 'Aien'; |
| 390 | } |
| 391 | function paneFor(node, item) { |
| 392 | if (!panes) return; |
| 393 | const opened = panes.openNode({ node, id: freshId('c'), title: titleFor(node), focus: false }); |
| 394 | if (opened && opened.ok) keep({ id: opened.id, close: opened.close, item }); |
| 395 | } |
| 396 | function paneEvents(events) { |
| 397 | const ids = new Map(); |
| 398 | for (const one of events) { |
| 399 | if (!one || typeof one.event !== 'string' || !one.data || typeof one.data !== 'object') continue; |
| 400 | const data = { ...one.data }; |
| 401 | if (typeof data.id === 'string') { |
| 402 | if (!ids.has(data.id)) ids.set(data.id, freshId('m')); |
| 403 | data.id = ids.get(data.id); |
| 404 | } |
| 405 | const done = panes.event(one.event, data); |
| 406 | if (one.event === 'pane.open' && done && done.ok) { |
| 407 | const id = done.id; |
| 408 | keep({ id, close: () => panes.event('pane.close', { v: data.v, id }) }); |
| 409 | } |
| 410 | } |
| 411 | } |
| 412 | async function compute(expression, out) { |
| 413 | const said = text => { |
| 414 | if (out) out.textContent = text; |
| 415 | }; |
| 416 | if (!panes) return said('Glass panes need a newer browser.'); |
| 417 | said('Computing with the Omega math engine...'); |
| 418 | let reply = null; |
| 419 | let response = null; |
| 420 | try { |
| 421 | response = await fetch(MATH, { |
| 422 | method: 'POST', |
| 423 | headers: { 'Content-Type': 'application/json', Accept: 'application/json' }, |
| 424 | body: JSON.stringify({ expression, title: expression.slice(0, 200) }), |
| 425 | cache: 'no-store', |
| 426 | credentials: 'omit' |
| 427 | }); |
| 428 | reply = await response.json(); |
| 429 | } catch (_) { |
| 430 | return said('The math engine did not answer. Try again in a minute.'); |
| 431 | } |
| 432 | if (!response.ok || !reply || !Array.isArray(reply.events)) { |
| 433 | const why = reply && typeof reply.message === 'string' ? reply.message.slice(0, 300) : `The math engine answered ${response.status}.`; |
| 434 | return said(why); |
| 435 | } |
| 436 | paneEvents(reply.events); |
| 437 | said(typeof reply.result === 'string' ? reply.result.slice(0, 600) : ''); |
| 438 | } |
| 439 | let mathId = ''; |
| 440 | function openMath(first) { |
| 441 | if (!panes) return; |
| 442 | if (mathId && mine.some(entry => entry.id === mathId)) { |
| 443 | const field = panesHost.querySelector('.aien-mathform input'); |
| 444 | if (first && field) { |
| 445 | field.value = first; |
| 446 | compute(first, panesHost.querySelector('.aien-mathform output')); |
| 447 | } |
| 448 | if (field) field.focus(); |
| 449 | return; |
| 450 | } |
| 451 | const box = document.createElement('form'); |
| 452 | box.className = 'aien-mathform'; |
| 453 | const label = document.createElement('label'); |
| 454 | label.textContent = 'An expression for the Omega math engine'; |
| 455 | const field = document.createElement('input'); |
| 456 | field.type = 'text'; |
| 457 | field.maxLength = 4096; |
| 458 | field.autocomplete = 'off'; |
| 459 | field.spellcheck = false; |
| 460 | field.placeholder = 'Integrate[x^2, {x, 0, 1}]'; |
| 461 | label.append(field); |
| 462 | const go = document.createElement('button'); |
| 463 | go.type = 'submit'; |
| 464 | go.textContent = 'Compute'; |
| 465 | const out = document.createElement('output'); |
| 466 | const note = document.createElement('p'); |
| 467 | note.className = 'aien-note'; |
| 468 | note.textContent = 'Exact algebra, calculus and numbers, computed on my Spark by Aien\'s Omega math engine. Each result opens its own pane with its provenance.'; |
| 469 | box.append(label, go, out, note); |
| 470 | box.addEventListener('submit', event => { |
| 471 | event.preventDefault(); |
| 472 | const expression = field.value.trim(); |
| 473 | if (expression) compute(expression, out); |
| 474 | }); |
| 475 | const opened = panes.openNode({ node: box, id: freshId('math'), title: 'Math', focus: false }); |
| 476 | if (!opened || !opened.ok) return; |
| 477 | mathId = opened.id; |
| 478 | keep({ id: opened.id, close: opened.close }); |
| 479 | if (first) { |
| 480 | field.value = first; |
| 481 | compute(first, out); |
| 482 | } |
| 483 | field.focus(); |
| 484 | } |
| 485 | /* The speed test is InferLab's AienSpeed component when the page has it; |
| 486 | until then the pane holds the live table allen.js fills from the stream |
| 487 | of each reply, with one button that asks Aien something to time. */ |
| 488 | let speedId = ''; |
| 489 | function openSpeed() { |
| 490 | if (!panes) return; |
| 491 | if (speedId) return; |
| 492 | const box = document.createElement('div'); |
| 493 | box.className = 'aien-speedpane'; |
| 494 | let mounted = false; |
| 495 | if (window.AienSpeed && typeof window.AienSpeed.mount === 'function') { |
| 496 | try { |
| 497 | window.AienSpeed.mount(box, {}); |
| 498 | mounted = true; |
| 499 | } catch (_) { |
| 500 | box.replaceChildren(); |
| 501 | } |
| 502 | } |
| 503 | if (!mounted) { |
| 504 | const note = document.createElement('p'); |
| 505 | note.className = 'aien-note'; |
| 506 | note.textContent = 'Measured by this page from the live stream of Aien\'s latest reply, on the live model on my DGX Spark.'; |
| 507 | const go = document.createElement('button'); |
| 508 | go.type = 'button'; |
| 509 | go.className = 'aien-tool'; |
| 510 | go.textContent = 'Time a reply now'; |
| 511 | go.addEventListener('click', () => { |
| 512 | if (window.allen && root.dataset.busy !== 'true') window.allen.send('In about 120 words, tell me how two black holes merge.'); |
| 513 | }); |
| 514 | box.append(speedTable, note, go); |
| 515 | } |
| 516 | const opened = panes.openNode({ node: box, id: freshId('speed'), title: 'Speed test', focus: false }); |
| 517 | if (!opened || !opened.ok) { |
| 518 | speedHome.before(speedTable); |
| 519 | return; |
| 520 | } |
| 521 | speedId = opened.id; |
| 522 | keep({ id: opened.id, close: opened.close }); |
| 523 | } |
| 524 | /* Commands typed into the oval: /math <expression> and /speed open panes |
| 525 | instead of going to the model. Capture runs before allen.js's own submit. */ |
| 526 | form.addEventListener('submit', event => { |
| 527 | const text = input.value.trim(); |
| 528 | const math = /^\/math\s+([\s\S]+)$/i.exec(text); |
| 529 | const speed = /^\/speed\s*$/i.test(text); |
| 530 | if (!math && !speed) return; |
| 531 | event.preventDefault(); |
| 532 | event.stopImmediatePropagation(); |
| 533 | sceneCall('key', text); |
| 534 | input.value = ''; |
| 535 | grow(); |
| 536 | if (math) openMath(math[1].trim()); |
| 537 | else openSpeed(); |
| 538 | }, true); |
| 539 | function grow() { |
| 540 | input.style.height = 'auto'; |
| 541 | input.style.height = `${Math.min(input.scrollHeight, 132)}px`; |
| 542 | } |
| 543 | input.addEventListener('input', grow); |
| 544 | /* Typing anywhere on the page types to Aien, as on the iMac. */ |
| 545 | document.addEventListener('keydown', event => { |
| 546 | if (event.defaultPrevented || event.ctrlKey || event.metaKey || event.altKey) return; |
| 547 | const target = event.target; |
| 548 | if (target !== document.body && target !== document.documentElement && target !== sky) return; |
| 549 | if (event.key.length === 1) input.focus({ preventScroll: true }); |
| 550 | }); |
| 551 | function wire() { |
| 552 | const chat = window.allen; |
| 553 | if (!chat || typeof chat.on !== 'function') return; |
| 554 | chat.on('send', detail => { |
| 555 | root.dataset.spoke = 'yes'; |
| 556 | held = false; |
| 557 | hideBubble(false); |
| 558 | if (!detail.silent) sceneCall('key', detail.text); |
| 559 | sceneCall('mode', 'thinking'); |
| 560 | }); |
| 561 | chat.on('start', () => sceneCall('mode', 'thinking')); |
| 562 | chat.on('token', detail => { |
| 563 | sceneCall('speak', detail.text); |
| 564 | soon(); |
| 565 | }); |
| 566 | chat.on('human', detail => sceneCall('speak', detail.text)); |
| 567 | const settle = () => { |
| 568 | sceneCall('mode', 'idle'); |
| 569 | soon(); |
| 570 | requestAnimationFrame(schedule); |
| 571 | }; |
| 572 | chat.on('reply', settle); |
| 573 | chat.on('done', settle); |
| 574 | chat.on('error', settle); |
| 575 | } |
| 576 | function start() { |
| 577 | startScene(); |
| 578 | startPanes(); |
| 579 | showSound(); |
| 580 | new MutationObserver(soon).observe(log, { childList: true, subtree: true, characterData: true }); |
| 581 | for (const kind of ['pointerdown', 'keydown']) document.addEventListener(kind, firstGesture, { capture: true, passive: true }); |
| 582 | bubble.addEventListener('pointerenter', event => { |
| 583 | if (event.pointerType === 'touch') return; |
| 584 | held = true; |
| 585 | clearTimers(); |
| 586 | bubble.dataset.state = 'shown'; |
| 587 | }); |
| 588 | bubble.addEventListener('pointerleave', event => { |
| 589 | if (event.pointerType === 'touch') return; |
| 590 | held = bubble.contains(document.activeElement); |
| 591 | schedule(); |
| 592 | }); |
| 593 | bubble.addEventListener('focusin', () => { |
| 594 | held = true; |
| 595 | clearTimers(); |
| 596 | bubble.dataset.state = 'shown'; |
| 597 | }); |
| 598 | bubble.addEventListener('focusout', () => { |
| 599 | held = false; |
| 600 | schedule(); |
| 601 | }); |
| 602 | /* A tap holds the bubble on touch screens; a second tap lets it go. */ |
| 603 | bubble.addEventListener('click', event => { |
| 604 | if (event.target.closest('button, a')) return; |
| 605 | held = !held; |
| 606 | if (held) { |
| 607 | clearTimers(); |
| 608 | bubble.dataset.state = 'shown'; |
| 609 | } else schedule(); |
| 610 | }); |
| 611 | /* Escape lets the bubble go at once, unless a pane took the key. */ |
| 612 | document.addEventListener('keydown', event => { |
| 613 | if (event.key !== 'Escape' || event.defaultPrevented || bubble.hidden) return; |
| 614 | held = false; |
| 615 | hideBubble(false); |
| 616 | }); |
| 617 | replayButton.addEventListener('click', replay); |
| 618 | soundButton.addEventListener('click', () => { |
| 619 | if (!audio) return; |
| 620 | audio.setMuted(!audio.muted()); |
| 621 | showSound(); |
| 622 | }); |
| 623 | speedButton.addEventListener('click', openSpeed); |
| 624 | mathButton.addEventListener('click', () => openMath('')); |
| 625 | if (/^#r=/.test(location.hash)) openSpeed(); |
| 626 | if (window.allen) wire(); |
| 627 | else document.addEventListener('allen-ready', wire, { once: true }); |
| 628 | soon(); |
| 629 | root.dataset.universe = 'on'; |
| 630 | } |
| 631 | if (document.readyState === 'loading') document.addEventListener('DOMContentLoaded', start, { once: true }); |
| 632 | else start(); |
| 633 | })(); |