-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathcore.js
More file actions
416 lines (391 loc) · 16.4 KB
/
Copy pathcore.js
File metadata and controls
416 lines (391 loc) · 16.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
/*
* ai-visualizer: give your AI agent a face.
* Copyright (C) 2026 Jared Rhodenizer
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
/* ============================================================
ai-visualizer core — the shared plumbing every face rides on.
A face is one self-contained page in faces/<name>/index.html.
It includes this script, calls AV.init(opts), then reads these
fields every animation frame after calling AV.tick(dtMs):
AV.state "idle" | "listening" | "thinking" | "speaking"
AV.level 0..1 raw voice loudness (speaking only)
AV.env 0..1 smoothed speech envelope (attack/release eased,
adaptively normalized — use this for motion)
AV.samples Float32Array(64), 0..1 normalized waveform ring
AV.alert bool, optional attention signal
AV.micLevel 0..1 your microphone (only if init({mic:true}))
AV.name display name from config ("JARVIS" by default)
AV.label the dotted chip label ("J.A.R.V.I.S.")
AV.badge optional handle from config ("" by default)
Modes:
live served by server.py — rides the real signal bus
demo ?demo=1, or the page opened as a plain file — a scripted
voice-turn loop (idle, listening, thinking, speaking) with
synthesized audio, so every face performs with no voice
line installed
shot ?shot=<state>&t=ms — pins one state and runs the frame
loop deterministically, then sets document.title to
"ready" (screenshot/verification harness)
The thinking sound: assets/thinking.wav plays while the state is
"thinking", exactly like a voice line would play it. If the bus
says the voice line is already playing its own (.voice_loading_pid),
this player stays quiet — you never hear it twice. The speaker
button (bottom left) toggles it; browsers may require one click on
the page before audio is allowed.
============================================================ */
"use strict";
const AV = (() => {
const Q = new URLSearchParams(location.search);
const SHOT = Q.get("shot");
const SHOT_T = parseInt(Q.get("t") || "4000", 10);
const DEMO = Q.get("demo") === "1" || location.protocol === "file:" || !!SHOT;
// where core.js lives -> where assets/ lives (works over http and file://)
const ROOT = new URL(".", document.currentScript.src);
const A = {
state: "idle", level: 0, env: 0, alert: false, micLevel: 0,
samples: new Float32Array(64),
name: "JARVIS", label: "J.A.R.V.I.S.", badge: "",
demo: DEMO, shot: SHOT, faces: [],
_sndOn: true, _mic: false, _readyCbs: [], _ready: false,
};
function dotted(name) {
const up = String(name).toUpperCase();
if (/^[A-Z0-9]{2,10}$/.test(up)) return up.split("").join(".") + ".";
return up;
}
/* -------------------------------- config -------------------------------- */
function applyConfig(cfg) {
if (cfg.name) { A.name = String(cfg.name); A.label = dotted(A.name); }
A.badge = String(cfg.badge || "");
if (cfg.thinking_sound === false) A._sndWant = false;
A.faces = cfg.faces || [];
A._ready = true;
A._readyCbs.forEach(cb => cb(A));
A._readyCbs = [];
}
A.ready = cb => { A._ready ? cb(A) : A._readyCbs.push(cb); };
/* ------------------------------ bus polling ------------------------------ */
let raw = { state: "idle", level: 0, samples: null, alert: false,
loading: false };
if (!DEMO) {
setInterval(async () => {
try {
const r = await fetch("/state", { cache: "no-store" });
raw = await r.json();
} catch (e) { /* server gone: hold last state */ }
}, 120);
}
/* ------------------------------ demo driver ------------------------------ */
// A scripted voice turn: the face performs everything with no voice line.
const SCRIPT = [["idle", 6000], ["listening", 3500], ["thinking", 4200],
["speaking", 8500]];
let demoT = 0, demoClock = 0;
const PIN = SHOT || Q.get("state"); // ?state=speaking pins the demo
function demoUpdate(dt) {
demoClock += dt;
let st = PIN || "idle";
if (!PIN) {
demoT = (demoT + dt) % SCRIPT.reduce((a, s) => a + s[1], 0);
let t = demoT;
for (const [name, len] of SCRIPT) {
if (t < len) { st = name; break; }
t -= len;
}
}
const tt = demoClock / 1000;
const speaking = st === "speaking";
const cadence = speaking
? Math.max(0, Math.sin(tt * 2.1) * 0.6 + Math.sin(tt * 0.9) * 0.5)
: 0;
const samples = new Array(64);
for (let i = 0; i < 64; i++) {
// drifting per-sample color so the synthetic voice has a moving
// spectrum, not a steady tone — spectrum-driven faces dance
const m = 0.3 + 0.7 * Math.abs(Math.sin(i * 0.23 + tt * 1.7))
* Math.abs(Math.sin(tt * 2.9 + i * 0.05));
samples[i] = speaking
? (Math.sin(i * 0.55 + tt * 9) * 0.6 + Math.sin(i * 1.7 - tt * 13)
* 0.4) * 9000 * (0.15 + 0.85 * cadence) * m
: 0;
}
raw = { state: st, level: speaking ? Math.min(1, cadence) : 0,
samples, alert: false, loading: false };
if (st === "listening")
A.micLevel = 0.25 + 0.55 * Math.abs(Math.sin(tt * 2.7))
* Math.abs(Math.sin(tt * 0.61));
}
/* ----------------------- envelope + samples easing ----------------------- */
let peak = 0.05, sPeak = 200;
function tick(dt) {
if (DEMO) demoUpdate(dt);
A.state = raw.state || "idle";
A.alert = !!raw.alert;
// Empty unless the voice line was told to publish usage. A face that
// wants to draw it reads AV.rateLimits; every other face ignores it.
A.rateLimits = raw.rate_limits || {};
A.level = raw.level || 0;
// adaptive envelope: normalize against a decaying peak, then ease
// (attack 50ms, release 350ms) — motion code rides AV.env
const dts = dt / 1000;
peak = Math.max(A.level, 0.05, peak - 0.5 * peak * dts);
const target = Math.min(1, A.level / peak);
const tau = target > A.env ? 50 : 350;
A.env += (target - A.env) * Math.min(1, dt / tau);
// waveform ring: rectify, normalize against its own decaying peak,
// blend toward the newest frame so the ring flows instead of flickers
const s = raw.samples;
A.rawSamples = s && s.length ? s : null; // signed, int16-scale floats
if (s && s.length) {
let mx = 0;
for (let i = 0; i < s.length; i++) mx = Math.max(mx, Math.abs(s[i]));
sPeak = Math.max(mx, 200, sPeak * 0.98);
const n = s.length;
for (let i = 0; i < 64; i++) {
const v = Math.abs(s[Math.min(n - 1, Math.round(i * (n - 1) / 63))])
/ sPeak;
A.samples[i] = A.samples[i] * 0.45 + Math.min(1, v) * 0.55;
}
} else {
for (let i = 0; i < 64; i++) A.samples[i] *= Math.max(0, 1 - dts * 6);
}
if (A.state !== "speaking" && !DEMO)
for (let i = 0; i < 64; i++) A.samples[i] *= Math.max(0, 1 - dts * 6);
if (A._mic && A._micAnalyser) micRead();
soundUpdate();
}
/* --------------------------------- mic ---------------------------------- */
let micPeak = 0.02;
function micRead() {
const an = A._micAnalyser;
const buf = A._micBuf;
an.getFloatTimeDomainData(buf);
let sum = 0;
for (let i = 0; i < buf.length; i++) sum += buf[i] * buf[i];
const rms = Math.sqrt(sum / buf.length);
micPeak = Math.max(rms, 0.02, micPeak * 0.999);
A.micLevel = Math.min(1, rms / micPeak);
}
async function micStart() {
try {
const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
const ctx = new AudioContext();
const src = ctx.createMediaStreamSource(stream);
const an = ctx.createAnalyser();
an.fftSize = 512;
src.connect(an);
A._micAnalyser = an;
A._micBuf = new Float32Array(an.fftSize);
const kick = () => ctx.state === "suspended" && ctx.resume();
addEventListener("click", kick); addEventListener("keydown", kick);
} catch (e) { /* no mic permission: level stays 0, faces degrade */ }
}
/* ----------------------------- thinking sound ---------------------------- */
let audio = null, sndBtn = null, playing = false;
A._sndWant = true;
function soundInit() {
if (SHOT) return;
try { A._sndOn = localStorage.getItem("av_sound") !== "0"; }
catch (e) { A._sndOn = true; }
audio = new Audio(new URL("assets/thinking.wav", ROOT).href);
audio.volume = 0.35;
sndBtn = document.createElement("div");
// hidden until the mouse moves, so it never collides with a face's
// chrome and never shows on camera or in an OBS source
sndBtn.style.cssText =
"position:fixed;left:64px;bottom:14px;z-index:50;cursor:pointer;" +
"font:12px 'SF Mono',Menlo,Consolas,monospace;letter-spacing:.2em;" +
"color:#5a6a72;opacity:0;transition:opacity .4s;user-select:none;" +
"pointer-events:none";
sndBtn.title = "thinking sound on/off";
let hideT = null;
addEventListener("mousemove", () => {
sndBtn.style.opacity = ".65";
sndBtn.style.pointerEvents = "auto";
clearTimeout(hideT);
hideT = setTimeout(() => {
sndBtn.style.opacity = "0";
sndBtn.style.pointerEvents = "none";
}, 3000);
});
sndBtn.onclick = () => {
A._sndOn = !A._sndOn;
try { localStorage.setItem("av_sound", A._sndOn ? "1" : "0"); }
catch (e) {}
if (!A._sndOn) stopSound();
paintBtn();
};
paintBtn();
document.body.appendChild(sndBtn);
}
function paintBtn() {
if (sndBtn) sndBtn.textContent = A._sndOn ? "SND ON" : "SND OFF";
}
function stopSound() {
if (audio && playing) { audio.pause(); audio.currentTime = 0; }
playing = false;
}
function soundUpdate() {
if (!audio || !A._sndWant) return;
const want = A._sndOn && A.state === "thinking" && !raw.loading;
if (want && !playing) {
playing = true;
audio.currentTime = 0;
audio.play().catch(() => { playing = false; });
} else if (!want && playing) {
stopSound();
}
}
/* ------------------------------ shot harness ----------------------------- */
// Runs the face's frame() deterministically (a synchronous burst of t ms).
// A headless browser resizes the window and finishes loading images AFTER
// the first burst, so the burst re-runs on resize and on two late timers
// (the last one flags "ready"), then keeps painting at frame pace so the
// late capture always sees a fresh composite.
A.shotRun = (frame) => {
const burst = () => { for (let t = 0; t < SHOT_T; t += 16.6) frame(16.6); };
burst();
addEventListener("resize", burst);
setTimeout(burst, 450);
setTimeout(burst, 900);
setTimeout(() => { burst(); document.title = "ready"; }, 3000);
// fat 100ms steps: assets that finish loading after the last burst
// still reach their steady state within a few paints
const loop = () => { frame(100); requestAnimationFrame(loop); };
requestAnimationFrame(loop);
};
/* ---------------------------------- init --------------------------------- */
A.init = (opts = {}) => {
A._mic = !!opts.mic;
if (A._mic && !DEMO) micStart();
if (opts.sound !== false) soundInit(); else A._sndWant = false;
if (DEMO) {
applyConfig({ name: Q.get("name") || "JARVIS" });
} else {
fetch("/config", { cache: "no-store" })
.then(r => r.json()).then(applyConfig)
.catch(() => applyConfig({}));
}
return A;
};
A.tick = tick;
/* ----------------------------- render helpers ---------------------------- */
const U = {};
U.dim = (c, f) => {
f = Math.max(0, Math.min(1, f));
return `rgb(${c[0] * f | 0},${c[1] * f | 0},${c[2] * f | 0})`;
};
U.rgba = (c, a) => `rgba(${c[0]},${c[1]},${c[2]},${a})`;
// How long until a usage window resets, in the shortest honest unit.
U.relTime = (ep) => {
const d = ep - Date.now() / 1000;
if (!(d > 0)) return "";
if (d < 3600) return Math.round(d / 60) + "m";
if (d < 86400) return Math.round(d / 3600) + "h";
return Math.round(d / 86400) + "d";
};
// The plan-usage windows, formatted ONCE for every face that draws them.
// Lives here rather than in each face because four copies of one format
// drift apart silently, and the first symptom is two faces disagreeing
// about the same number.
//
// Returns [] when the voice line publishes no usage, so a face can call
// it unconditionally and simply draw nothing when there is nothing to say.
// A window that is KNOWN but has no percentage yet still returns a row:
// hiding it entirely was the original bug, and a row that says "no number
// yet" is information where a missing row is just confusing.
U.usageRows = () => {
const rl = A.rateLimits || {};
const out = [];
for (const [label, w] of [["5H", rl.five_hour], ["7D", rl.seven_day]]) {
if (!w) continue;
const known = w.utilization != null;
const pct = known ? Math.round(w.utilization * 100) : null;
const rel = w.resets_at ? U.relTime(w.resets_at) : "";
out.push({
label, pct, known,
hot: known && pct >= 80,
text: (known ? pct + "%" : "\u2014") + (rel ? " " + rel : "")
});
}
return out;
};
U.mix = (c1, c2, t) => [c1[0] + (c2[0] - c1[0]) * t | 0,
c1[1] + (c2[1] - c1[1]) * t | 0,
c1[2] + (c2[2] - c1[2]) * t | 0];
// soft additive glow sprite (canvas), cached by the caller
U.makeGlow = (rgb, size) => {
const c = document.createElement("canvas");
c.width = c.height = size;
const g = c.getContext("2d");
const grd = g.createRadialGradient(size / 2, size / 2, 0,
size / 2, size / 2, size / 2);
grd.addColorStop(0, `rgba(${rgb[0]},${rgb[1]},${rgb[2]},1)`);
grd.addColorStop(.25, `rgba(${rgb[0]},${rgb[1]},${rgb[2]},.55)`);
grd.addColorStop(1, "rgba(0,0,0,0)");
g.fillStyle = grd;
g.fillRect(0, 0, size, size);
return c;
};
// the one-field bloom rule: draw everything luminous into one field
// canvas, bloom the WHOLE field (two downscale taps), composite
// additively — bloom applied per-element reads as pencil lines
U.bloomBlit = (dst, field, w, h) => {
if (!field._b4 || field._b4.width !== w >> 2) {
field._b4 = document.createElement("canvas");
field._b4.width = Math.max(1, w >> 2);
field._b4.height = Math.max(1, h >> 2);
field._b8 = document.createElement("canvas");
field._b8.width = Math.max(1, w >> 3);
field._b8.height = Math.max(1, h >> 3);
}
const g4 = field._b4.getContext("2d"), g8 = field._b8.getContext("2d");
g4.clearRect(0, 0, field._b4.width, field._b4.height);
g4.drawImage(field, 0, 0, field._b4.width, field._b4.height);
g8.clearRect(0, 0, field._b8.width, field._b8.height);
g8.drawImage(field, 0, 0, field._b8.width, field._b8.height);
const prev = dst.globalCompositeOperation;
dst.globalCompositeOperation = "lighter";
dst.drawImage(field, 0, 0);
dst.drawImage(field._b4, 0, 0, w, h);
dst.drawImage(field._b8, 0, 0, w, h);
dst.globalCompositeOperation = prev;
};
// text that resolves out of glyph noise, left to right
U.Descrambler = class {
constructor(text, perChar = 50, hold = null) {
this.text = text; this.per = perChar; this.hold = hold;
this.t = 0; this.done = false;
this.chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789#$%&";
}
render(dt) {
this.t += dt;
const n = this.t / this.per | 0;
let out = "";
for (let i = 0; i < this.text.length; i++) {
const ch = this.text[i];
out += (i < n || ch === " ") ? ch
: this.chars[Math.random() * this.chars.length | 0];
}
if (this.hold != null && this.t > this.per * this.text.length + this.hold)
this.done = true;
return out;
}
};
A.util = U;
return A;
})();