clavarium 0.2.107-beta.1 → 0.2.108-beta.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/npm-shrinkwrap.json +6 -6
- package/package.json +2 -2
- package/server/boxes.mjs +66 -3
- package/server/main.mjs +9 -1
package/npm-shrinkwrap.json
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "clavarium",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.108-beta.1",
|
|
4
4
|
"lockfileVersion": 3,
|
|
5
5
|
"requires": true,
|
|
6
6
|
"packages": {
|
|
7
7
|
"": {
|
|
8
8
|
"name": "clavarium",
|
|
9
|
-
"version": "0.2.
|
|
9
|
+
"version": "0.2.108-beta.1",
|
|
10
10
|
"license": "MIT",
|
|
11
11
|
"dependencies": {
|
|
12
12
|
"@xterm/addon-fit": "^0.11.0",
|
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
"clavarium-binder": "0.1.43-next.1",
|
|
15
15
|
"clavarium-docket": "0.1.1",
|
|
16
16
|
"clavarium-parley": "0.17.33-next.1",
|
|
17
|
-
"clavarium-tether": "0.1.0-next.
|
|
17
|
+
"clavarium-tether": "0.1.0-next.43",
|
|
18
18
|
"mermaid": "11.17.2",
|
|
19
19
|
"node-pty": "^1.1.0",
|
|
20
20
|
"pdfjs-dist": "6.4.299",
|
|
@@ -760,9 +760,9 @@
|
|
|
760
760
|
}
|
|
761
761
|
},
|
|
762
762
|
"node_modules/clavarium-tether": {
|
|
763
|
-
"version": "0.1.0-next.
|
|
764
|
-
"resolved": "https://registry.npmjs.org/clavarium-tether/-/clavarium-tether-0.1.0-next.
|
|
765
|
-
"integrity": "sha512-
|
|
763
|
+
"version": "0.1.0-next.43",
|
|
764
|
+
"resolved": "https://registry.npmjs.org/clavarium-tether/-/clavarium-tether-0.1.0-next.43.tgz",
|
|
765
|
+
"integrity": "sha512-vAoj19cWZiXMpDkRnhV1P+oNWv6dGL65eur/2PbxNiP5i9uWI5tipRoQfnff5Xkqd5o+E4vbRnWvzlHAJTTX5A==",
|
|
766
766
|
"license": "MIT",
|
|
767
767
|
"bin": {
|
|
768
768
|
"tether": "bin/cli.mjs",
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "clavarium",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.108-beta.1",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"description": "A conversation is a VM with a terminal in it. Everything else is a folder.",
|
|
@@ -47,7 +47,7 @@
|
|
|
47
47
|
"clavarium-binder": "0.1.43-next.1",
|
|
48
48
|
"clavarium-docket": "0.1.1",
|
|
49
49
|
"clavarium-parley": "0.17.33-next.1",
|
|
50
|
-
"clavarium-tether": "0.1.0-next.
|
|
50
|
+
"clavarium-tether": "0.1.0-next.43",
|
|
51
51
|
"mermaid": "11.17.2",
|
|
52
52
|
"node-pty": "^1.1.0",
|
|
53
53
|
"pdfjs-dist": "6.4.299",
|
package/server/boxes.mjs
CHANGED
|
@@ -22,6 +22,9 @@ import { Refusal } from './conversations.mjs';
|
|
|
22
22
|
export const CODE_MINUTES = 10;
|
|
23
23
|
export const PAIR_PATH = '/c/api/box/pair';
|
|
24
24
|
export const LINK_PATH = '/c/api/box/ws';
|
|
25
|
+
/* A firmware a box downloads to update itself: a one-use address, kept a few minutes. */
|
|
26
|
+
export const FIRMWARE_PATH = '/c/api/box/firmware/';
|
|
27
|
+
export const FIRMWARE_MINUTES = 10;
|
|
25
28
|
export const SENTENCE_MAX = 15 * 32_000 + 32_000; /* 15 s of 16 kHz 16-bit mono, and some slack */
|
|
26
29
|
export const TRIES_PER_MINUTE = 10;
|
|
27
30
|
export const NAME_MAX = 40;
|
|
@@ -56,6 +59,30 @@ export function wavOf(pcm, rate = 16_000) {
|
|
|
56
59
|
return Buffer.concat([h, pcm]);
|
|
57
60
|
}
|
|
58
61
|
|
|
62
|
+
/* A .wav (16-bit PCM, any rate, mono or stereo) as a box plays it: 16 kHz mono, a minute at most. */
|
|
63
|
+
export const PLAY_SECONDS = 70;
|
|
64
|
+
export function pcmOfWav(wav) {
|
|
65
|
+
const b = Buffer.isBuffer(wav) ? wav : Buffer.from(wav);
|
|
66
|
+
if (b.toString('ascii', 0, 4) !== 'RIFF' || b.toString('ascii', 8, 12) !== 'WAVE') throw new Refusal(400, 'Not a .wav file.');
|
|
67
|
+
let at = 12; let fmt = null; let data = null;
|
|
68
|
+
while (at + 8 <= b.length) {
|
|
69
|
+
const id = b.toString('ascii', at, at + 4); const size = b.readUInt32LE(at + 4);
|
|
70
|
+
if (id === 'fmt ') fmt = { format: b.readUInt16LE(at + 8), channels: b.readUInt16LE(at + 10), rate: b.readUInt32LE(at + 12), bits: b.readUInt16LE(at + 22) };
|
|
71
|
+
if (id === 'data') { data = b.subarray(at + 8, Math.min(b.length, at + 8 + size)); break; }
|
|
72
|
+
at += 8 + size + (size & 1);
|
|
73
|
+
}
|
|
74
|
+
if (!fmt || !data || fmt.format !== 1 || fmt.bits !== 16 || !fmt.channels || !fmt.rate) throw new Refusal(400, 'A .wav of 16-bit PCM, please.');
|
|
75
|
+
const frames = Math.floor(data.length / (2 * fmt.channels));
|
|
76
|
+
const n = Math.min(Math.floor(frames * 16_000 / fmt.rate), PLAY_SECONDS * 16_000);
|
|
77
|
+
const out = Buffer.alloc(n * 2);
|
|
78
|
+
const sample = (f) => { let sum = 0; for (let c = 0; c < fmt.channels; c++) sum += data.readInt16LE((f * fmt.channels + c) * 2); return sum / fmt.channels; };
|
|
79
|
+
for (let i = 0; i < n; i++) {
|
|
80
|
+
const x = i * fmt.rate / 16_000; const j = Math.floor(x); const k = Math.min(j + 1, frames - 1);
|
|
81
|
+
out.writeInt16LE(Math.max(-32768, Math.min(32767, Math.round(sample(j) + (sample(k) - sample(j)) * (x - j)))), i * 2);
|
|
82
|
+
}
|
|
83
|
+
return out;
|
|
84
|
+
}
|
|
85
|
+
|
|
59
86
|
/* Text for the ear: what a written message holds and a voice should not read. */
|
|
60
87
|
export function forSpeech(text) {
|
|
61
88
|
return String(text ?? '')
|
|
@@ -71,12 +98,12 @@ export function forSpeech(text) {
|
|
|
71
98
|
|
|
72
99
|
export class Boxes {
|
|
73
100
|
#file; #codes = new Map(); #links = new Map(); #tries = []; #now;
|
|
74
|
-
#exists; #hear; #say; #speak; #log; #changed;
|
|
101
|
+
#exists; #hear; #say; #speak; #log; #changed; #images = new Map(); #origin;
|
|
75
102
|
|
|
76
103
|
/* file: a JsonFile holding [{ id, name, conversation, keyHash, firmware, createdAt, seenAt }].
|
|
77
104
|
exists(conversation), hear(wav) -> text, say(conversation, text, room), speak(text) -> PCM 16 kHz. */
|
|
78
|
-
constructor({ file, exists = () => true, hear, say, speak, changed = () => {}, now = () => Date.now(), log = () => {} }) {
|
|
79
|
-
this.#file = file; this.#changed = changed; this.#exists = exists; this.#hear = hear; this.#say = say; this.#speak = speak; this.#now = now; this.#log = log;
|
|
105
|
+
constructor({ file, exists = () => true, hear, say, speak, changed = () => {}, origin = () => null, now = () => Date.now(), log = () => {} }) {
|
|
106
|
+
this.#file = file; this.#changed = changed; this.#origin = origin; this.#exists = exists; this.#hear = hear; this.#say = say; this.#speak = speak; this.#now = now; this.#log = log;
|
|
80
107
|
}
|
|
81
108
|
|
|
82
109
|
#all() { const v = this.#file.get(); return Array.isArray(v) ? v : []; }
|
|
@@ -144,6 +171,40 @@ export class Boxes {
|
|
|
144
171
|
return a;
|
|
145
172
|
}
|
|
146
173
|
|
|
174
|
+
/* A sound played on a box (a sample, a recorded message), as its answers are. */
|
|
175
|
+
play(id, wav) {
|
|
176
|
+
const link = this.#links.get(id);
|
|
177
|
+
if (!link) throw new Refusal(409, 'This voice box is not connected.');
|
|
178
|
+
const pcm = pcmOfWav(wav);
|
|
179
|
+
link.play(pcm);
|
|
180
|
+
return { seconds: Math.round(pcm.length / 32_000) };
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/* UPDATED OVER ITS WI-FI: the image is offered at a one-use address on this server (under /c/,
|
|
184
|
+
no sign-on: the token is the right), the box told to fetch it with its sha256; it answers
|
|
185
|
+
once installed, then restarts. */
|
|
186
|
+
async update(id, image, version = '') {
|
|
187
|
+
if (!this.#links.has(id)) throw new Refusal(409, 'This voice box is not connected: it is updated when it is online.');
|
|
188
|
+
const bytes = Buffer.isBuffer(image) ? image : Buffer.from(String(image ?? ''), 'base64');
|
|
189
|
+
if (bytes.length < 1024 || bytes[0] !== 0xe9) throw new Refusal(400, 'Not an ESP32 program image (it starts with 0xE9; a merged image is for a cable flash only).');
|
|
190
|
+
const origin = this.#origin();
|
|
191
|
+
if (!origin) throw new Refusal(409, 'This server does not know its public address: the box could not fetch the image.');
|
|
192
|
+
const token = randomBytes(24).toString('base64url');
|
|
193
|
+
this.#images.set(token, { bytes, until: this.#now() + FIRMWARE_MINUTES * 60_000 });
|
|
194
|
+
const sha256 = createHash('sha256').update(bytes).digest('hex');
|
|
195
|
+
try {
|
|
196
|
+
const a = await this.command(id, { t: 'ota', url: `${origin}${FIRMWARE_PATH}${token}.bin`, sha256 }, 5 * 60_000);
|
|
197
|
+
if (a?.ok === false) throw new Refusal(502, `The box did not install it: ${a.detail ?? 'no reason given'}.`);
|
|
198
|
+
this.#log(`voice box ${this.#all().find((b) => b.id === id)?.name ?? id}: updated${version ? ` to ${version}` : ''}, restarting`);
|
|
199
|
+
return { ok: true };
|
|
200
|
+
} finally { this.#images.delete(token); }
|
|
201
|
+
}
|
|
202
|
+
image(token) {
|
|
203
|
+
const now = this.#now();
|
|
204
|
+
for (const [k, v] of this.#images) if (v.until < now) this.#images.delete(k);
|
|
205
|
+
return this.#images.get(String(token).replace(/\.bin$/, ''))?.bytes;
|
|
206
|
+
}
|
|
207
|
+
|
|
147
208
|
/* Where it is (a home, a family house): grouped by it, named with it. */
|
|
148
209
|
place(id, place) { this.#change(id, { place: String(place ?? '').trim().slice(0, NAME_MAX) }); }
|
|
149
210
|
|
|
@@ -344,6 +405,8 @@ export function voiceboxConnector(boxes) {
|
|
|
344
405
|
async run(verb, t, a = {}) {
|
|
345
406
|
const id = t.config?.box;
|
|
346
407
|
if (verb === 'voicebox.volume') { const r = await boxes.setVolume(id, a.level); return { text: r?.offline ? `Volume ${a.level} kept: the box is offline, it gets it when it comes back.` : `Volume set to ${a.level}.` }; }
|
|
408
|
+
if (verb === 'voicebox.play') { const r = boxes.play(id, Buffer.from(String(a.image ?? ''), 'base64')); return { text: `Played (${r.seconds} s).` }; }
|
|
409
|
+
if (verb === 'voicebox.update') { await boxes.update(id, a.image, a.version); return { text: `Installed${a.version ? ` (${a.version})` : ''}: the box restarts and comes back in a moment.` }; }
|
|
347
410
|
if (verb === 'voicebox.alert') { await boxes.command(id, { t: 'alert', text: String(a.text ?? '').slice(0, 300) }, 10 * 60_000); return { text: 'The alert was seen: someone pressed its button.' }; }
|
|
348
411
|
throw new Error(`${verb} is not for a voice box`);
|
|
349
412
|
},
|
package/server/main.mjs
CHANGED
|
@@ -76,7 +76,7 @@ import { Events } from './events.mjs';
|
|
|
76
76
|
import { Bundles } from './bundle.mjs';
|
|
77
77
|
import { migrateData } from './dataformat.mjs';
|
|
78
78
|
import { VmWatch } from './vmwatch.mjs';
|
|
79
|
-
import { Boxes, handleBoxes, voiceboxConnector, syncTargets, PAIR_PATH as BOX_PAIR_PATH } from './boxes.mjs';
|
|
79
|
+
import { Boxes, handleBoxes, voiceboxConnector, syncTargets, PAIR_PATH as BOX_PAIR_PATH, FIRMWARE_PATH as BOX_FIRMWARE_PATH } from './boxes.mjs';
|
|
80
80
|
import { Speech } from './speech.mjs';
|
|
81
81
|
import { WebSocketServer } from 'ws';
|
|
82
82
|
|
|
@@ -198,6 +198,13 @@ function serve(port, conversations, resources, { gate = () => 'local', originOk,
|
|
|
198
198
|
return;
|
|
199
199
|
}
|
|
200
200
|
/* A voice box pairing: under /c/, no sign-on; the one-use code is the right (boxes.mjs). */
|
|
201
|
+
/* A firmware a box fetches to update itself: the one-use token in the address is the right. */
|
|
202
|
+
if (boxes && pathname.startsWith(BOX_FIRMWARE_PATH) && request.method === 'GET') {
|
|
203
|
+
const bytes = boxes.image(pathname.slice(BOX_FIRMWARE_PATH.length));
|
|
204
|
+
if (!bytes) { response.writeHead(404, HEADERS).end(); return; }
|
|
205
|
+
response.writeHead(200, { ...HEADERS, 'content-type': 'application/octet-stream', 'content-length': bytes.length, 'cache-control': 'no-store' }).end(bytes);
|
|
206
|
+
return;
|
|
207
|
+
}
|
|
201
208
|
if (boxes && pathname === BOX_PAIR_PATH) {
|
|
202
209
|
const say = (status, body) => response.writeHead(status, { ...HEADERS, 'content-type': 'application/json; charset=utf-8' }).end(JSON.stringify(body));
|
|
203
210
|
if (request.method !== 'POST') { say(405, { error: 'Call this address with POST.' }); return; }
|
|
@@ -602,6 +609,7 @@ export async function startHome({ port, host, dataDir, mntDir = dataDir, gitRese
|
|
|
602
609
|
/* Said as the person's own message, the room named (one speaker for now). */
|
|
603
610
|
say: (name, text, by) => talk.say(name, text, undefined, undefined, 'voice', { by }),
|
|
604
611
|
/* Each conversation's thread knows its boxes: their names, and the ids its agent answers. */
|
|
612
|
+
origin: () => publicOrigin ?? null,
|
|
605
613
|
changed: () => { for (const c of all.list()) talk.speakers(c.name, boxes.speakersOf(c.name)); try { syncTargets(tethers.store(), boxes.all()); } catch (error) { console.error(`voice boxes in Tether: ${error.message}`); } },
|
|
606
614
|
speak: (text) => speech.speak(text),
|
|
607
615
|
log: (line) => events.add({ kind: 'server', text: line }) });
|