clavarium 0.2.175-next.1 → 0.2.176-next.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.
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "clavarium",
3
- "version": "0.2.175-next.1",
3
+ "version": "0.2.176-next.1",
4
4
  "lockfileVersion": 3,
5
5
  "requires": true,
6
6
  "packages": {
7
7
  "": {
8
8
  "name": "clavarium",
9
- "version": "0.2.175-next.1",
9
+ "version": "0.2.176-next.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.41",
17
+ "clavarium-tether": "0.1.0-next.42",
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.41",
764
- "resolved": "https://registry.npmjs.org/clavarium-tether/-/clavarium-tether-0.1.0-next.41.tgz",
765
- "integrity": "sha512-TYoKd13BWVdNY7yLwX6v9yqSstEXp+s2AMKR9n04aeHop6EdW0bGm1Frm5vLFGCzOlWn8gzSky/pYtJjZy9Weg==",
763
+ "version": "0.1.0-next.42",
764
+ "resolved": "https://registry.npmjs.org/clavarium-tether/-/clavarium-tether-0.1.0-next.42.tgz",
765
+ "integrity": "sha512-ms19DnB+9f6x8C0H5Qp6XCJIk/TA8oIT4UisAtiyEUlfa0tHVYITaAh7jO11tDRvvk2lnh439EBRgGVeKdFJIA==",
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.175-next.1",
3
+ "version": "0.2.176-next.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.41",
50
+ "clavarium-tether": "0.1.0-next.42",
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;
@@ -71,12 +74,12 @@ export function forSpeech(text) {
71
74
 
72
75
  export class Boxes {
73
76
  #file; #codes = new Map(); #links = new Map(); #tries = []; #now;
74
- #exists; #hear; #say; #speak; #log; #changed;
77
+ #exists; #hear; #say; #speak; #log; #changed; #images = new Map(); #origin;
75
78
 
76
79
  /* file: a JsonFile holding [{ id, name, conversation, keyHash, firmware, createdAt, seenAt }].
77
80
  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;
81
+ constructor({ file, exists = () => true, hear, say, speak, changed = () => {}, origin = () => null, now = () => Date.now(), log = () => {} }) {
82
+ 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
83
  }
81
84
 
82
85
  #all() { const v = this.#file.get(); return Array.isArray(v) ? v : []; }
@@ -144,6 +147,31 @@ export class Boxes {
144
147
  return a;
145
148
  }
146
149
 
150
+ /* UPDATED OVER ITS WI-FI: the image is offered at a one-use address on this server (under /c/,
151
+ no sign-on: the token is the right), the box told to fetch it with its sha256; it answers
152
+ once installed, then restarts. */
153
+ async update(id, image, version = '') {
154
+ if (!this.#links.has(id)) throw new Refusal(409, 'This voice box is not connected: it is updated when it is online.');
155
+ const bytes = Buffer.isBuffer(image) ? image : Buffer.from(String(image ?? ''), 'base64');
156
+ 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).');
157
+ const origin = this.#origin();
158
+ if (!origin) throw new Refusal(409, 'This server does not know its public address: the box could not fetch the image.');
159
+ const token = randomBytes(24).toString('base64url');
160
+ this.#images.set(token, { bytes, until: this.#now() + FIRMWARE_MINUTES * 60_000 });
161
+ const sha256 = createHash('sha256').update(bytes).digest('hex');
162
+ try {
163
+ const a = await this.command(id, { t: 'ota', url: `${origin}${FIRMWARE_PATH}${token}.bin`, sha256 }, 5 * 60_000);
164
+ if (a?.ok === false) throw new Refusal(502, `The box did not install it: ${a.detail ?? 'no reason given'}.`);
165
+ this.#log(`voice box ${this.#all().find((b) => b.id === id)?.name ?? id}: updated${version ? ` to ${version}` : ''}, restarting`);
166
+ return { ok: true };
167
+ } finally { this.#images.delete(token); }
168
+ }
169
+ image(token) {
170
+ const now = this.#now();
171
+ for (const [k, v] of this.#images) if (v.until < now) this.#images.delete(k);
172
+ return this.#images.get(String(token).replace(/\.bin$/, ''))?.bytes;
173
+ }
174
+
147
175
  /* Where it is (a home, a family house): grouped by it, named with it. */
148
176
  place(id, place) { this.#change(id, { place: String(place ?? '').trim().slice(0, NAME_MAX) }); }
149
177
 
@@ -344,6 +372,7 @@ export function voiceboxConnector(boxes) {
344
372
  async run(verb, t, a = {}) {
345
373
  const id = t.config?.box;
346
374
  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}.` }; }
375
+ 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
376
  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
377
  throw new Error(`${verb} is not for a voice box`);
349
378
  },
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 }) });