interplane.Get Omega
Read in your language

aienos.com

Source of www.aienos.com, served from my own hardware

Brought by drakestapleton

branch main Branches Tags History

aienos.com / om / aien-universe / universe.om

125 lines · 25112 bytes Raw Blame History
1note /aien/ universe controller generator: emits public/js/aien-universe.js on fd 1.
2note Composed after om/allen-page/contract.om by check.sh, never compiled on its own: ~N~ tokens
3note in the texts expand the shared contract fields (ids 0 to 15 and 62 to 72, the math route 61),
4note so the skeleton of allen.om, the chat script and this controller name every id once.
5note Each text holds one part of the browser script, in order; main writes them and checks that
6note no token was left open.
7
8note JavaScript head
9text js0 "/*\n * aien-universe.js: /aien/ as the iMac shows Aien. Generated by\n * om/aien-universe/universe.om (Omega) with the shared contract of\n * om/allen-page/contract.om; do not edit.\n *\n * The page is the sky. The native scene (allen-scene.js, aienux da55660\n * compiled to WebAssembly) plays the two black holes merging into Aien in\n * the middle, with the native chirp (allen-audio.js) on the first gesture.\n * Visitors type in one glass oval under him; on Enter the letters float up\n * into him. His reply is one bubble beside him that fades once it has been\n * read: the time grows with its length, and hover, focus or a tap hold it.\n * There is never more than one bubble, no transcript and no scrolling page.\n * Things that are not chat open as glass panes (allen-panes.js): programs\n * and cards from a reply, the speed test and the math engine.\n *\n * allen.js stays the transport and keeps writing every reply into the\n * screen reader log; this script only reads that log, moves cards into\n * panes and mirrors the newest reply into the bubble. Every string it shows\n * goes through textContent.\n */\n(() => {\n 'use strict';\n const byId = id => document.getElementById(id);\n const root = byId('~0~');\n const sky = byId('~62~');\n if (!root || !sky) return;\n const log = byId('~1~');\n const form = byId('~2~');\n const input = byId('~3~');\n const examples = byId('~15~');\n const bubble = byId('~63~');\n const bubbleWho = byId('~72~');\n const bubbleText = byId('~64~');\n const bubbleActs = byId('~65~');\n const panesHost = byId('~66~');\n const menu = byId('~67~');\n const replayButton = byId('~68~');\n const soundButton = byId('~69~');\n const speedButton = byId('~70~');\n const mathButton = byId('~71~');\n const speedTable = menu.querySelector('dl.allen-speed');\n const speedHome = speedTable.nextElementSibling;\n const MATH = '~61~';\n /* Reading time. 200 words a minute at 6 characters a word is 50 ms a\n character (WolframLanguageEvaluator: UnitConvert[Quantity[1, \"Minutes\"]/(200*6),\n \"Milliseconds\"] -> 50 ms), after a 2500 ms glance, at most 30 s. */\n const READ_BASE = 2500;\n const READ_CHAR = 50;\n const READ_MAX = 30000;\n const FADE_MS = 900;\n /* Panes this page opened that stay open at once; the oldest closes first. */\n const PANES_SHOWN = 3;\n /* The opening is the iMac's own order: the tube (native dusk, 4200 ms),\n then the two holes merge (native night: merger ends at 12300 ms) and\n become Aien (formed at 15300 ms). For the web the night's first 6800 ms\n (stars, lens, shadows) play 6.8 times faster, 1000 ms, then the spiral,\n merger and forming run at the native rate. With sound on the night runs\n at the native rate throughout, so the chirp stays on the merger.\n WolframLanguageEvaluator: {6800/1000, 4200 + 1000 + (15300 - 6800)} -> {34/5, 13700} ms. */\n const TUBE_MS = 4200;\n const WARP_END = 6800;\n const WARP_RATE = 6.8;\n const FORMED_MS = 15300;\n"
10
11note JavaScript scene
12text js1 " let scene = null;\n let formed = false;\n let stage = 'none';\n let warpFrom = 0;\n let watching = 0;\n let fallback = 0;\n const still = matchMedia('(prefers-reduced-motion: reduce)');\n function formUp() {\n if (formed) return;\n formed = true;\n stage = 'formed';\n clearTimeout(fallback);\n root.dataset.formed = 'yes';\n mirror();\n }\n function sceneTime() {\n const at = Number(sky.dataset.sceneTime);\n return Number.isFinite(at) && at >= 0 ? at : 0;\n }\n function sceneCall(name, arg) {\n if (!scene) return;\n try {\n scene[name](arg);\n } catch (_) {\n /* The sky is decoration; the conversation never depends on it. */\n }\n }\n /* The night: the two holes. Warped while no sound is armed. */\n function night() {\n sceneCall('enter', 'birth');\n root.dataset.opening = 'night';\n if (armed) {\n stage = 'night';\n if (audio) audio.resume();\n } else {\n stage = 'warp';\n warpFrom = performance.now();\n }\n clearTimeout(fallback);\n fallback = setTimeout(formUp, FORMED_MS + 1500);\n }\n /* Hand the clock back to the native loop at the native rate. */\n function unwarp(at) {\n stage = 'night';\n sceneCall('frame', at);\n sceneCall('mode', root.dataset.busy === 'true' ? 'thinking' : 'idle');\n }\n function watch(now) {\n watching = 0;\n if (!scene || stage === 'formed' || stage === 'none') return;\n if (stage === 'tube' && sceneTime() >= TUBE_MS) night();\n else if (stage === 'warp') {\n const at = (now - warpFrom) * WARP_RATE;\n if (at >= WARP_END) unwarp(WARP_END);\n else sceneCall('frame', at);\n }\n if (stage !== 'night' && stage !== 'formed') watching = requestAnimationFrame(watch);\n }\n function opening() {\n formed = false;\n root.dataset.formed = 'no';\n root.dataset.opening = 'tube';\n stage = 'tube';\n clearTimeout(fallback);\n fallback = setTimeout(formUp, TUBE_MS + FORMED_MS + 3000);\n if (!watching) watching = requestAnimationFrame(watch);\n }\n function startScene() {\n if (!window.AllenScene) {\n root.dataset.sky = 'off';\n formUp();\n return;\n }\n try {\n scene = window.AllenScene.mount(sky, { opening: still.matches ? 'idle' : 'dusk', interactive: false });\n } catch (_) {\n scene = null;\n root.dataset.sky = 'off';\n formUp();\n return;\n }\n if (scene.ready && typeof scene.ready.then === 'function') {\n scene.ready.then(() => {\n root.dataset.sky = 'on';\n }, () => {\n root.dataset.sky = 'off';\n formUp();\n });\n }\n sky.addEventListener('allen-scene-complete', event => {\n if (event.detail && event.detail.kind === 'birth') formUp();\n });\n sky.addEventListener('allen-scene-error', () => {\n root.dataset.sky = 'off';\n formUp();\n });\n if (still.matches) formUp();\n else opening();\n }\n"
13
14note JavaScript sound
15text js2 " const audio = window.AllenAudio || null;\n let armed = false;\n /* Browsers start sound only inside a gesture. The first click or key on\n the page arms the native chirp: during the tube it waits, paused, for\n the night to begin; during the night it joins where the scene is, and a\n warped night drops to the native rate so the chirp lands on the merger. */\n function firstGesture() {\n if (armed || !audio || audio.muted()) return;\n armed = true;\n try {\n if (stage === 'tube') {\n audio.enter({ kind: 'night', nightMs: 0 });\n audio.pause();\n } else if (stage === 'warp') {\n const at = Math.min(WARP_END, (performance.now() - warpFrom) * WARP_RATE);\n unwarp(at);\n audio.enter({ kind: 'night', nightMs: at });\n } else if (stage === 'night') {\n audio.enter({ kind: 'night', nightMs: sceneTime() });\n }\n } catch (_) {}\n }\n function showSound() {\n const on = !(audio && audio.muted());\n soundButton.textContent = on ? 'Sound: on' : 'Sound: off';\n soundButton.setAttribute('aria-pressed', on ? 'true' : 'false');\n soundButton.hidden = !audio;\n }\n /* Replay plays the whole opening again, tube first, with the sound armed\n by this click. */\n function replay() {\n menu.open = false;\n hideBubble(true);\n if (!scene || still.matches) return;\n if (audio) audio.stop();\n armed = false;\n sceneCall('enter', 'dusk');\n opening();\n firstGesture();\n }\n"
16
17note JavaScript bubble
18text js3 " /* One bubble, never a scroll: a long reply turns pages inside the same\n bubble, each held for its own reading time. */\n const PAGE_CHARS = 180;\n let watched = null;\n let fadeTimer = 0;\n let goneTimer = 0;\n let pending = 0;\n let held = false;\n let fullText = '';\n let pageAt = 0;\n let armedFor = '';\n /* The message whose bubble was let go; it does not come back. */\n let dismissed = null;\n function clearTimers() {\n clearTimeout(fadeTimer);\n clearTimeout(goneTimer);\n fadeTimer = 0;\n goneTimer = 0;\n armedFor = '';\n }\n function hideBubble(now) {\n dismissed = watched;\n clearTimers();\n if (bubble.hidden) return;\n if (now) {\n bubble.hidden = true;\n bubble.dataset.state = 'gone';\n return;\n }\n bubble.dataset.state = 'fading';\n goneTimer = setTimeout(() => {\n bubble.hidden = true;\n bubble.dataset.state = 'gone';\n }, FADE_MS);\n }\n function readMs(text) {\n return Math.min(READ_MAX, READ_BASE + READ_CHAR * text.length);\n }\n function pagesOf(text) {\n const pages = [];\n let rest = text.trim();\n while (rest.length > PAGE_CHARS) {\n let cut = rest.lastIndexOf(' ', PAGE_CHARS);\n const para = rest.lastIndexOf('\\n', PAGE_CHARS);\n if (para > PAGE_CHARS / 3) cut = para;\n else if (cut < PAGE_CHARS / 3) cut = PAGE_CHARS;\n pages.push(rest.slice(0, cut).trim());\n rest = rest.slice(cut).trim();\n }\n if (rest) pages.push(rest);\n return pages;\n }\n function paint() {\n const pages = pagesOf(fullText);\n if (pageAt > pages.length - 1) pageAt = Math.max(0, pages.length - 1);\n const page = pages[pageAt] || '';\n const last = pageAt >= pages.length - 1;\n if (bubbleText.textContent !== page) bubbleText.textContent = page;\n bubble.dataset.more = last ? 'no' : 'yes';\n bubbleActs.hidden = !last;\n return { page, last };\n }\n /* A finished page turns after its reading time; the last page fades once\n the reply is complete, unless it asks a question with buttons. */\n function schedule() {\n if (bubble.hidden || bubble.dataset.state === 'fading') return;\n const { page, last } = paint();\n if (held) {\n clearTimers();\n return;\n }\n let plan = '';\n if (!last) plan = `turn ${pageAt}`;\n else if (root.dataset.busy !== 'true' && !bubbleActs.querySelector('button:not([disabled])')) plan = `fade ${pageAt} ${page.length}`;\n if (plan === armedFor) return;\n clearTimers();\n armedFor = plan;\n if (!plan) return;\n fadeTimer = setTimeout(() => {\n armedFor = '';\n fadeTimer = 0;\n if (!plan.startsWith('turn')) {\n hideBubble(false);\n return;\n }\n pageAt += 1;\n if (!still.matches && bubbleText.animate) bubbleText.animate([{ opacity: 0 }, { opacity: 1 }], { duration: 220, easing: 'ease-out' });\n schedule();\n }, readMs(page));\n }\n function showBubble(who, text) {\n fullText = text;\n if (bubbleWho.textContent !== who) bubbleWho.textContent = who;\n if (bubble.hidden || bubble.dataset.state === 'fading') {\n clearTimers();\n bubble.hidden = false;\n }\n bubble.dataset.state = 'shown';\n schedule();\n }\n function bodyText(body) {\n const parts = [];\n for (const child of body.children) {\n if (!child.matches('p.allen-p, p.allen-h, p.allen-err, p.allen-note')) continue;\n const text = child.textContent.trim();\n if (text) parts.push(text);\n }\n return parts.join('\\n');\n }\n"
19
20note JavaScript mirror
21text js4 " /* The newest message from Aien or Drake becomes the bubble. Cards and code\n move into panes as they appear, so allen.js keeps streaming into them;\n choice buttons move into the bubble so they stay clickable. */\n function adopt(item, body) {\n for (const child of [...body.children]) {\n if (child.matches('.allen-choices')) {\n bubbleActs.append(child);\n } else if (child.matches('.allen-code, .allen-card, .allen-consent, .allen-out, .allen-frame-box')) {\n paneFor(child, item);\n }\n }\n }\n function mirror() {\n pending = 0;\n const items = log.querySelectorAll('li.allen-msg-allen, li.allen-msg-drake');\n const item = items.length ? items[items.length - 1] : null;\n if (!item) return;\n const body = item.querySelector('.allen-body');\n if (!body) return;\n if (item !== watched) {\n watched = item;\n bubbleActs.replaceChildren();\n fullText = '';\n pageAt = 0;\n clearTimers();\n }\n adopt(item, body);\n if (item === dismissed) return;\n if (!formed) return;\n const text = bodyText(body);\n const who = item.classList.contains('allen-msg-drake') ? 'Drake' : 'Aien';\n if (text || bubbleActs.childElementCount) showBubble(who, text);\n }\n function soon() {\n if (!pending) pending = requestAnimationFrame(mirror);\n }\n"
22
23note JavaScript panes
24text js5 " let panes = null;\n const mine = [];\n let serial = 0;\n function freshId(stem) {\n serial += 1;\n return `${stem}${Date.now().toString(36)}${serial}`;\n }\n function keep(entry) {\n mine.push(entry);\n while (mine.length > PANES_SHOWN) {\n const old = mine.shift();\n try {\n old.close();\n } catch (_) {}\n }\n }\n function forget(id) {\n const at = mine.findIndex(entry => entry.id === id);\n if (at >= 0) mine.splice(at, 1);\n if (id === speedId) {\n speedId = '';\n speedHome.before(speedTable);\n }\n }\n function startPanes() {\n if (!window.AllenPanes) return;\n try {\n panes = window.AllenPanes.mount(panesHost, { onClose: forget, returnFocus: input });\n } catch (_) {\n panes = null;\n }\n }\n function titleFor(node) {\n const lang = node.querySelector('.allen-lang');\n const name = lang ? lang.textContent.trim().toLowerCase() : '';\n if (name === 'omega' || name === 'om') return 'Omega program';\n if (name === 'html' || name === 'htm') return 'App';\n if (node.matches('.allen-consent')) return 'Remember me';\n if (node.matches('.allen-code')) return name ? `Code (${name})` : 'Code';\n const head = node.querySelector('h3, h4, strong, p');\n const text = head ? head.textContent.trim() : '';\n return text ? text.slice(0, 60) : 'Aien';\n }\n function paneFor(node, item) {\n if (!panes) return;\n const opened = panes.openNode({ node, id: freshId('c'), title: titleFor(node), focus: false });\n if (opened && opened.ok) keep({ id: opened.id, close: opened.close, item });\n }\n function paneEvents(events) {\n const ids = new Map();\n for (const one of events) {\n if (!one || typeof one.event !== 'string' || !one.data || typeof one.data !== 'object') continue;\n const data = { ...one.data };\n if (typeof data.id === 'string') {\n if (!ids.has(data.id)) ids.set(data.id, freshId('m'));\n data.id = ids.get(data.id);\n }\n const done = panes.event(one.event, data);\n if (one.event === 'pane.open' && done && done.ok) {\n const id = done.id;\n keep({ id, close: () => panes.event('pane.close', { v: data.v, id }) });\n }\n }\n }\n"
25
26note JavaScript math
27text js6 " async function compute(expression, out) {\n const said = text => {\n if (out) out.textContent = text;\n };\n if (!panes) return said('Glass panes need a newer browser.');\n said('Computing with the Omega math engine...');\n let reply = null;\n let response = null;\n try {\n response = await fetch(MATH, {\n method: 'POST',\n headers: { 'Content-Type': 'application/json', Accept: 'application/json' },\n body: JSON.stringify({ expression, title: expression.slice(0, 200) }),\n cache: 'no-store',\n credentials: 'omit'\n });\n reply = await response.json();\n } catch (_) {\n return said('The math engine did not answer. Try again in a minute.');\n }\n if (!response.ok || !reply || !Array.isArray(reply.events)) {\n const why = reply && typeof reply.message === 'string' ? reply.message.slice(0, 300) : `The math engine answered ${response.status}.`;\n return said(why);\n }\n paneEvents(reply.events);\n said(typeof reply.result === 'string' ? reply.result.slice(0, 600) : '');\n }\n let mathId = '';\n function openMath(first) {\n if (!panes) return;\n if (mathId && mine.some(entry => entry.id === mathId)) {\n const field = panesHost.querySelector('.aien-mathform input');\n if (first && field) {\n field.value = first;\n compute(first, panesHost.querySelector('.aien-mathform output'));\n }\n if (field) field.focus();\n return;\n }\n const box = document.createElement('form');\n box.className = 'aien-mathform';\n const label = document.createElement('label');\n label.textContent = 'An expression for the Omega math engine';\n const field = document.createElement('input');\n field.type = 'text';\n field.maxLength = 4096;\n field.autocomplete = 'off';\n field.spellcheck = false;\n field.placeholder = 'Integrate[x^2, {x, 0, 1}]';\n label.append(field);\n const go = document.createElement('button');\n go.type = 'submit';\n go.textContent = 'Compute';\n const out = document.createElement('output');\n const note = document.createElement('p');\n note.className = 'aien-note';\n 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.';\n box.append(label, go, out, note);\n box.addEventListener('submit', event => {\n event.preventDefault();\n const expression = field.value.trim();\n if (expression) compute(expression, out);\n });\n const opened = panes.openNode({ node: box, id: freshId('math'), title: 'Math', focus: false });\n if (!opened || !opened.ok) return;\n mathId = opened.id;\n keep({ id: opened.id, close: opened.close });\n if (first) {\n field.value = first;\n compute(first, out);\n }\n field.focus();\n }\n"
28
29note JavaScript speed
30text js7 " /* The speed test is InferLab's AienSpeed component when the page has it;\n until then the pane holds the live table allen.js fills from the stream\n of each reply, with one button that asks Aien something to time. */\n let speedId = '';\n function openSpeed() {\n if (!panes) return;\n if (speedId) return;\n const box = document.createElement('div');\n box.className = 'aien-speedpane';\n let mounted = false;\n if (window.AienSpeed && typeof window.AienSpeed.mount === 'function') {\n try {\n window.AienSpeed.mount(box, {});\n mounted = true;\n } catch (_) {\n box.replaceChildren();\n }\n }\n if (!mounted) {\n const note = document.createElement('p');\n note.className = 'aien-note';\n note.textContent = 'Measured by this page from the live stream of Aien\\'s latest reply, on the live model on my DGX Spark.';\n const go = document.createElement('button');\n go.type = 'button';\n go.className = 'aien-tool';\n go.textContent = 'Time a reply now';\n go.addEventListener('click', () => {\n if (window.allen && root.dataset.busy !== 'true') window.allen.send('In about 120 words, tell me how two black holes merge.');\n });\n box.append(speedTable, note, go);\n }\n const opened = panes.openNode({ node: box, id: freshId('speed'), title: 'Speed test', focus: false });\n if (!opened || !opened.ok) {\n speedHome.before(speedTable);\n return;\n }\n speedId = opened.id;\n keep({ id: opened.id, close: opened.close });\n }\n"
31
32note JavaScript input
33text js8 " /* Commands typed into the oval: /math <expression> and /speed open panes\n instead of going to the model. Capture runs before allen.js's own submit. */\n form.addEventListener('submit', event => {\n const text = input.value.trim();\n const math = /^\\/math\\s+([\\s\\S]+)$/i.exec(text);\n const speed = /^\\/speed\\s*$/i.test(text);\n if (!math && !speed) return;\n event.preventDefault();\n event.stopImmediatePropagation();\n sceneCall('key', text);\n input.value = '';\n grow();\n if (math) openMath(math[1].trim());\n else openSpeed();\n }, true);\n function grow() {\n input.style.height = 'auto';\n input.style.height = `${Math.min(input.scrollHeight, 132)}px`;\n }\n input.addEventListener('input', grow);\n /* Typing anywhere on the page types to Aien, as on the iMac. */\n document.addEventListener('keydown', event => {\n if (event.defaultPrevented || event.ctrlKey || event.metaKey || event.altKey) return;\n const target = event.target;\n if (target !== document.body && target !== document.documentElement && target !== sky) return;\n if (event.key.length === 1) input.focus({ preventScroll: true });\n });\n"
34
35note JavaScript wire
36text js9 " function wire() {\n const chat = window.allen;\n if (!chat || typeof chat.on !== 'function') return;\n chat.on('send', detail => {\n root.dataset.spoke = 'yes';\n held = false;\n hideBubble(false);\n if (!detail.silent) sceneCall('key', detail.text);\n sceneCall('mode', 'thinking');\n });\n chat.on('start', () => sceneCall('mode', 'thinking'));\n chat.on('token', detail => {\n sceneCall('speak', detail.text);\n soon();\n });\n chat.on('human', detail => sceneCall('speak', detail.text));\n const settle = () => {\n sceneCall('mode', 'idle');\n soon();\n requestAnimationFrame(schedule);\n };\n chat.on('reply', settle);\n chat.on('done', settle);\n chat.on('error', settle);\n }\n"
37
38note JavaScript start
39text js10 " function start() {\n startScene();\n startPanes();\n showSound();\n new MutationObserver(soon).observe(log, { childList: true, subtree: true, characterData: true });\n for (const kind of ['pointerdown', 'keydown']) document.addEventListener(kind, firstGesture, { capture: true, passive: true });\n bubble.addEventListener('pointerenter', event => {\n if (event.pointerType === 'touch') return;\n held = true;\n clearTimers();\n bubble.dataset.state = 'shown';\n });\n bubble.addEventListener('pointerleave', event => {\n if (event.pointerType === 'touch') return;\n held = bubble.contains(document.activeElement);\n schedule();\n });\n bubble.addEventListener('focusin', () => {\n held = true;\n clearTimers();\n bubble.dataset.state = 'shown';\n });\n bubble.addEventListener('focusout', () => {\n held = false;\n schedule();\n });\n /* A tap holds the bubble on touch screens; a second tap lets it go. */\n bubble.addEventListener('click', event => {\n if (event.target.closest('button, a')) return;\n held = !held;\n if (held) {\n clearTimers();\n bubble.dataset.state = 'shown';\n } else schedule();\n });\n /* Escape lets the bubble go at once, unless a pane took the key. */\n document.addEventListener('keydown', event => {\n if (event.key !== 'Escape' || event.defaultPrevented || bubble.hidden) return;\n held = false;\n hideBubble(false);\n });\n replayButton.addEventListener('click', replay);\n soundButton.addEventListener('click', () => {\n if (!audio) return;\n audio.setMuted(!audio.muted());\n showSound();\n });\n speedButton.addEventListener('click', openSpeed);\n mathButton.addEventListener('click', () => openMath(''));\n if (/^#r=/.test(location.hash)) openSpeed();\n if (window.allen) wire();\n else document.addEventListener('allen-ready', wire, { once: true });\n soon();\n root.dataset.universe = 'on';\n }\n if (document.readyState === 'loading') document.addEventListener('DOMContentLoaded', start, { once: true });\n else start();\n})();\n"
40
41fn main to int
42put target 0 1
43js_head
44js_scene
45js_sound
46js_bubble
47js_mirror
48js_panes
49js_math
50js_speed
51js_input
52js_wire
53js_start
54if eq get marker 0 1
55trap 3
56end
57flush
58ret 0
59end
60
61fn js_head to void
62for b ulong len js0
63byteput get js0 b
64end
65end
66
67fn js_scene to void
68for b ulong len js1
69byteput get js1 b
70end
71end
72
73fn js_sound to void
74for b ulong len js2
75byteput get js2 b
76end
77end
78
79fn js_bubble to void
80for b ulong len js3
81byteput get js3 b
82end
83end
84
85fn js_mirror to void
86for b ulong len js4
87byteput get js4 b
88end
89end
90
91fn js_panes to void
92for b ulong len js5
93byteput get js5 b
94end
95end
96
97fn js_math to void
98for b ulong len js6
99byteput get js6 b
100end
101end
102
103fn js_speed to void
104for b ulong len js7
105byteput get js7 b
106end
107end
108
109fn js_input to void
110for b ulong len js8
111byteput get js8 b
112end
113end
114
115fn js_wire to void
116for b ulong len js9
117byteput get js9 b
118end
119end
120
121fn js_start to void
122for b ulong len js10
123byteput get js10 b
124end
125end