clavarium 0.2.116-beta.1 → 0.2.117-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.
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "clavarium",
3
- "version": "0.2.116-beta.1",
3
+ "version": "0.2.117-beta.1",
4
4
  "lockfileVersion": 3,
5
5
  "requires": true,
6
6
  "packages": {
7
7
  "": {
8
8
  "name": "clavarium",
9
- "version": "0.2.116-beta.1",
9
+ "version": "0.2.117-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.45",
17
+ "clavarium-tether": "0.1.0-next.46",
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.45",
764
- "resolved": "https://registry.npmjs.org/clavarium-tether/-/clavarium-tether-0.1.0-next.45.tgz",
765
- "integrity": "sha512-3lZT2MHeQWcfF8241K2+N+/ZX8wluFe0HqlrUPbTgSMws+RqQh61mEzk0Z9vlHSoG5aGhQ5b8CwK1QqBwGp9nQ==",
763
+ "version": "0.1.0-next.46",
764
+ "resolved": "https://registry.npmjs.org/clavarium-tether/-/clavarium-tether-0.1.0-next.46.tgz",
765
+ "integrity": "sha512-p6B9kM1A4CUN/OyLWf5IpsKvQ5SiTir7P+X2BcnRQBml+S9z/1nS0Xs7vxhk4ekDui/x0Dh5xMOO4IvaCLhaeA==",
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.116-beta.1",
3
+ "version": "0.2.117-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.45",
50
+ "clavarium-tether": "0.1.0-next.46",
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
@@ -86,8 +86,8 @@ export function pcmOfWav(wav) {
86
86
  }
87
87
 
88
88
  /* Text for the ear: what a written message holds and a voice should not read. */
89
- export function forSpeech(text) {
90
- return String(text ?? '')
89
+ export function forSpeech(text, { callOthers = false } = {}) {
90
+ const said = String(text ?? '')
91
91
  .replace(/```[\s\S]*?```/g, ' ')
92
92
  .replace(/`([^`]*)`/g, '$1')
93
93
  .replace(/\[([^\]]+)\]\([^)]+\)/g, '$1')
@@ -102,8 +102,27 @@ export function forSpeech(text) {
102
102
  .replace(/\s+/g, ' ')
103
103
  .trim()
104
104
  .slice(0, 1500);
105
+ /* Nor another assistant's call: « OK Google » said in an example woke the Google Home beside it (31-10).
106
+ Only speakTo() calls it, on purpose. */
107
+ return callOthers ? said : said.replace(/\b(?:ok|okay|hey|dis)[\s,]+(?:google|siri|alexa)\b/giu, 'l\'appel de l\'enceinte');
105
108
  }
106
109
 
110
+ /* THE BOX'S CLOCK: its lights follow the hours of its place. The time zone is the page's that made its code
111
+ (its browser's), else this server's; the box is told its offset from UTC at each connection and every hour
112
+ after, so summer time comes by itself. */
113
+ export function zoneOf(zone) {
114
+ const z = String(zone ?? '').trim();
115
+ try { if (z && /^[A-Za-z_]+(?:\/[A-Za-z0-9_+-]+){0,2}$/.test(z)) { new Intl.DateTimeFormat('en', { timeZone: z }); return z; } } catch { /* unknown */ }
116
+ return '';
117
+ }
118
+ export function offsetOf(zone, now) {
119
+ const z = zoneOf(zone) || Intl.DateTimeFormat().resolvedOptions().timeZone;
120
+ const parts = Object.fromEntries(new Intl.DateTimeFormat('en-US', { timeZone: z, hourCycle: 'h23', year: 'numeric', month: '2-digit', day: '2-digit', hour: '2-digit', minute: '2-digit' }).formatToParts(new Date(now)).map((p) => [p.type, p.value]));
121
+ const local = Date.UTC(+parts.year, +parts.month - 1, +parts.day, +parts.hour, +parts.minute);
122
+ return Math.round((local - Math.floor(now / 60_000) * 60_000) / 60_000);
123
+ }
124
+
125
+ export const RELAY_WITHIN_MS = 20_000;
107
126
  export const DOING_WITHIN_MS = 120_000;
108
127
  /* A LONG RECORDING (a meeting, a class): asked in words, stopped by the button or the wake word. */
109
128
  export const RECORD_ASKED = /\b(?:lance|d[ée]marre|commence|d[ée]bute)\w*\b.{0,25}\benregistrement\b|\benregistre\b.{0,20}\b(?:r[ée]union|conversation|cours|discussion|consultation)\b/i;
@@ -176,11 +195,11 @@ export class Boxes {
176
195
  return this.#all().map(({ keyHash, ...b }) => ({ ...b, online: this.#links.has(b.id), muted: this.#links.get(b.id)?.muted ?? null, volume: b.volume ?? this.#links.get(b.id)?.volume ?? VOLUME_DEFAULT }));
177
196
  }
178
197
 
179
- code(conversation, name) {
198
+ code(conversation, name, zone = '') {
180
199
  if (!this.#exists(conversation)) throw new Refusal(404, 'No such conversation.');
181
200
  const code = newCode();
182
201
  const label = String(name ?? '').trim().slice(0, NAME_MAX);
183
- this.#codes.set(plainCode(code), { conversation, name: label, until: this.#now() + CODE_MINUTES * 60_000 });
202
+ this.#codes.set(plainCode(code), { conversation, name: label, zone: zoneOf(zone), until: this.#now() + CODE_MINUTES * 60_000 });
184
203
  return { code, minutes: CODE_MINUTES };
185
204
  }
186
205
 
@@ -200,6 +219,7 @@ export class Boxes {
200
219
  type: String(body.type ?? '').slice(0, 40), firmware: String(body.firmware ?? '').slice(0, 20),
201
220
  capabilities: Array.isArray(body.capabilities) ? body.capabilities.map(String).slice(0, 20) : [],
202
221
  createdAt: new Date(now).toISOString(), seenAt: null,
222
+ ...(wanted.zone ? { zone: wanted.zone } : {}),
203
223
  };
204
224
  this.#save([...this.#all(), box]);
205
225
  this.#log(`voice box paired: ${name} (${box.conversation})`);
@@ -264,6 +284,20 @@ export class Boxes {
264
284
  return this.#images.get(String(token).replace(/\.bin$/, ''))?.bytes;
265
285
  }
266
286
 
287
+ /* SPEAKING TO ANOTHER DEVICE in the room (a Google Home: « OK Google, baisse la clim »): said as given, its
288
+ call included, then the box listens; what it hears within twenty seconds comes back to the conversation as
289
+ that device's answer, not as the person's words (31-10: the box set the heating that way). */
290
+ async speakTo(id, text) {
291
+ const link = this.#links.get(id);
292
+ if (!link) throw new Refusal(409, 'This voice box is not connected.');
293
+ const words = forSpeech(text, { callOthers: true });
294
+ if (!words) throw new Refusal(400, 'Nothing to say.');
295
+ link.play(await this.#speak(words), 'listen');
296
+ link.relay = this.#now();
297
+ this.#log(`voice box ${link.box().name}: spoke to a device in the room`);
298
+ return { ok: true };
299
+ }
300
+
267
301
  /* LISTENING TO THE INTERNET: a radio, a podcast, a music file, a playlist, read by this server (ffmpeg,
268
302
  any format it knows) and sent on to the box at the pace it is played, a few seconds ahead -- until it
269
303
  ends, stop(), or the person presses the button or says the wake word (the box then starts a sentence). */
@@ -278,7 +312,7 @@ export class Boxes {
278
312
  const box = link.box(); const label = name || new URL(url).host;
279
313
  const stream = { child, queue: [], queued: 0, sent: 0, started: this.#now(), timer: null, ended: false, failed: '', over: [], gain: 1 };
280
314
  this.#streams.set(id, stream);
281
- link.send({ t: 'speak.start', rate: 16_000 });
315
+ link.send({ t: 'speak.start', rate: 16_000, kind: 'stream' }); /* not its own voice: the box still hears its word, near-certain only */
282
316
  child.stdout.on('data', (d) => { stream.queue.push(d); stream.queued += d.length; if (stream.queued > LISTEN_AHEAD * 4) child.stdout.pause(); });
283
317
  child.stderr.on('data', (d) => { stream.failed = `${stream.failed}${d}`.slice(-300); });
284
318
  child.on('error', (e) => { stream.failed = e.code === 'ENOENT' ? 'ffmpeg is not installed on this server' : e.message; stream.ended = true; });
@@ -322,7 +356,13 @@ export class Boxes {
322
356
  }
323
357
 
324
358
  /* Where it is (a home, a family house): grouped by it, named with it. */
325
- place(id, place) { this.#change(id, { place: String(place ?? '').trim().slice(0, NAME_MAX) }); }
359
+ zone(id, zone) {
360
+ const z = zoneOf(zone);
361
+ if (!z) throw new Refusal(400, 'Not a time zone.');
362
+ this.#change(id, { zone: z });
363
+ this.#links.get(id)?.send({ t: 'clock', offset: offsetOf(z, this.#now()) });
364
+ }
365
+ place(id, place) { this.#change(id, { place: String(place ?? '').trim().slice(0, NAME_MAX) }); }
326
366
 
327
367
  move(id, conversation) {
328
368
  if (!this.#exists(conversation)) throw new Refusal(404, 'No such conversation.');
@@ -448,6 +488,8 @@ export class Boxes {
448
488
  const wanted = link.box().volume ?? VOLUME_DEFAULT;
449
489
  if (m.volume !== wanted) { send({ t: 'volume', id: randomUUID(), level: wanted }); m.volume = wanted; }
450
490
  }
491
+ if (m.t === 'hello') send({ t: 'clock', offset: offsetOf(link.box().zone, this.#now()) });
492
+ if (m.t === 'ping' && this.#now() - (link.clockAt ?? 0) > 3_600_000) { link.clockAt = this.#now(); send({ t: 'clock', offset: offsetOf(link.box().zone, this.#now()) }); }
451
493
  if (m.t === 'hello') { link.muted = m.muted === true; link.volume = Number.isFinite(m.volume) ? m.volume : null; this.#change(box.id, { firmware: String(m.firmware ?? '').slice(0, 20) }); seen(); return; }
452
494
  if (m.t === 'record.start' && this.#recordDir) {
453
495
  mkdirSync(this.#recordDir, { recursive: true });
@@ -552,6 +594,10 @@ export class Boxes {
552
594
  link.send({ t: 'record', on: true });
553
595
  return;
554
596
  }
597
+ if (link.relay && this.#now() - link.relay < RELAY_WITHIN_MS) {
598
+ link.relay = 0;
599
+ text = `(réponse entendue de l'appareil, pas de la personne) ${text}`;
600
+ }
555
601
  try { await this.#say(box.conversation, text, { id: box.id, name: box.name, ...(box.place ? { place: box.place } : {}) }); } catch (error) { this.#log(`voice box ${box.name}: ${seconds} s heard and dictated, but not said in ${box.conversation} (${error.message})`); await lost("Pardon, je n'ai pas pu transmettre ta phrase. Tu peux répéter ?"); return; }
556
602
  this.#log(`voice box ${box.name}: ${seconds} s heard, said in ${box.conversation}`);
557
603
  link.heardAt = this.#now();
@@ -582,6 +628,7 @@ export class Boxes {
582
628
  * POST /api/boxes/<id>/rename {name}
583
629
  * POST /api/boxes/<id>/move {conversation}: one conversation at a time
584
630
  * POST /api/boxes/<id>/place {place}: where it is (a home), shown with its name
631
+ * POST /api/boxes/<id>/zone {zone}: its time zone (IANA), for its clock; the page gives its own
585
632
  * POST /api/boxes/<id>/revoke
586
633
  * POST /api/boxes/<id>/volume {level 0-100}
587
634
  * POST /api/boxes/<id>/wifi-scan -> {networks}
@@ -600,7 +647,7 @@ export async function handleBoxes(request, response, boxes, originOk, headers =
600
647
  for await (const piece of request) { text += piece; if (text.length > 8192) return send(413, { error: 'Too long.' }); }
601
648
  const body = text ? JSON.parse(text) : {};
602
649
  if (pathname === '/api/boxes/code') {
603
- const made = boxes.code(String(body.conversation ?? ''), body.name);
650
+ const made = boxes.code(String(body.conversation ?? ''), body.name, body.zone);
604
651
  /* With this page's address: the key, and a link (its QR code) that opens the box's page filled in. */
605
652
  const origin = /^https?:\/\/[^/\s]+$/.test(String(body.origin ?? '')) ? String(body.origin) : '';
606
653
  if (!origin) return send(201, made);
@@ -616,6 +663,7 @@ export async function handleBoxes(request, response, boxes, originOk, headers =
616
663
  case 'rename': boxes.rename(id, body.name); return send(200, { ok: true });
617
664
  case 'move': boxes.move(id, String(body.conversation ?? '')); return send(200, { ok: true });
618
665
  case 'place': boxes.place(id, body.place); return send(200, { ok: true });
666
+ case 'zone': boxes.zone(id, body.zone); return send(200, { ok: true });
619
667
  case 'revoke': boxes.revoke(id); return send(200, { ok: true });
620
668
  case 'volume': return answered(await boxes.setVolume(id, body.level));
621
669
  case 'wifi-scan': { const r = await boxes.command(id, { t: 'wifi.scan' }); return send(200, { networks: Array.isArray(r.networks) ? r.networks : [] }); }
@@ -640,6 +688,7 @@ export function voiceboxConnector(boxes) {
640
688
  const id = t.config?.box;
641
689
  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}.` }; }
642
690
  if (verb === 'voicebox.listen') { await boxes.listen(id, String(a.url), String(a.name ?? '')); return { text: `Playing ${a.name || a.url}: it goes on until it ends, voicebox-stop, or the person presses the button or says the wake word.` }; }
691
+ if (verb === 'voicebox.speak') { await boxes.speakTo(id, String(a.text ?? '')); return { text: 'Said aloud; the box listens now: what it hears in the next 20 s comes back in the conversation, marked as the device\'s answer.' }; }
643
692
  if (verb === 'voicebox.stop') return { text: boxes.stop(id) ? 'Stopped.' : 'Nothing was playing.' };
644
693
  if (verb === 'voicebox.play') { const r = boxes.play(id, Buffer.from(String(a.image ?? ''), 'base64')); return { text: `Played (${r.seconds} s).` }; }
645
694
  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.` }; }
package/web/app.js CHANGED
@@ -4442,7 +4442,7 @@
4442
4442
  row.hidden = true;
4443
4443
  const call = async (op, args) => { try { return { ok: true, data: await post(`${API}/tether/${op}`, args) }; } catch (error) { return { ok: false, text: error.message }; } };
4444
4444
  /* A device that pairs (a voice box): a one-use code for the conversation that asked, made here. */
4445
- const pair = async (room) => { const r = await post(`${API}/boxes/code`, { conversation: String(job.agent).replace(/^conversation:/, ''), name: room }); return { url: location.origin, code: r.code }; };
4445
+ const pair = async (room) => { const r = await post(`${API}/boxes/code`, { conversation: String(job.agent).replace(/^conversation:/, ''), name: room, zone: Intl.DateTimeFormat().resolvedOptions().timeZone }); return { url: location.origin, code: r.code }; };
4446
4446
  try { const { runJob } = await import('/vendor/tether/jobs.js'); await runJob(job, { call, root: place, pair }); }
4447
4447
  catch (error) { place.textContent = `It could not start: ${error.message}`; }
4448
4448
  setTimeout(() => { jobPlaces.delete(job.id); after?.(); }, 4000);
@@ -8864,6 +8864,8 @@
8864
8864
  /* Where it is (a home): typed once, then offered for the next ones. */
8865
8865
  const place = el('input'); place.value = b.place ?? ''; place.maxLength = 40; place.placeholder = 'Place (a home)'; place.setAttribute('list', 'box-places'); place.setAttribute('aria-label', `Place of ${b.name}`);
8866
8866
  place.addEventListener('change', async () => { await post(`${API}/boxes/${b.id}/place`, { place: place.value.trim() }).catch(() => {}); await paintBoxes(); });
8867
+ /* A box paired before it knew its time zone takes this page's: its lights follow the hours here. */
8868
+ if (!b.zone) void post(`${API}/boxes/${b.id}/zone`, { zone: Intl.DateTimeFormat().resolvedOptions().timeZone }).catch(() => {});
8867
8869
  /* Louder, softer: kept here, given to the box now or when it comes back. */
8868
8870
  const vol = el('input'); vol.type = 'range'; vol.min = '0'; vol.max = '100'; vol.step = '5'; vol.value = String(b.volume ?? 80); vol.setAttribute('aria-label', `Volume of ${b.name}`);
8869
8871
  const volSaid = el('small', 'hint', `${vol.value} %`);
@@ -8893,7 +8895,7 @@
8893
8895
  $('box-code').addEventListener('click', async () => {
8894
8896
  if (!$('box-conversation').value) { $('box-conversation').focus(); $('box-code-minutes').textContent = 'Choose the conversation it speaks in first.'; $('box-ready').hidden = false; return; }
8895
8897
  try {
8896
- const r = await post(`${API}/boxes/code`, { conversation: $('box-conversation').value, name: $('box-room').value.trim(), origin: location.origin });
8898
+ const r = await post(`${API}/boxes/code`, { conversation: $('box-conversation').value, name: $('box-room').value.trim(), origin: location.origin, zone: Intl.DateTimeFormat().resolvedOptions().timeZone });
8897
8899
  $('box-code-shown').textContent = r.code; $('box-code-minutes').textContent = `valid ${r.minutes} minutes, once`;
8898
8900
  $('box-key').textContent = r.key ?? '';
8899
8901
  $('box-link').href = r.link ?? '#';