clavarium 0.2.178-next.1 → 0.2.182-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.178-next.1",
3
+ "version": "0.2.182-next.1",
4
4
  "lockfileVersion": 3,
5
5
  "requires": true,
6
6
  "packages": {
7
7
  "": {
8
8
  "name": "clavarium",
9
- "version": "0.2.178-next.1",
9
+ "version": "0.2.182-next.1",
10
10
  "license": "MIT",
11
11
  "dependencies": {
12
12
  "@xterm/addon-fit": "^0.11.0",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "clavarium",
3
- "version": "0.2.178-next.1",
3
+ "version": "0.2.182-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.",
package/server/boxes.mjs CHANGED
@@ -25,7 +25,7 @@ export const LINK_PATH = '/c/api/box/ws';
25
25
  /* A firmware a box downloads to update itself: a one-use address, kept a few minutes. */
26
26
  export const FIRMWARE_PATH = '/c/api/box/firmware/';
27
27
  export const FIRMWARE_MINUTES = 10;
28
- export const SENTENCE_MAX = 15 * 32_000 + 32_000; /* 15 s of 16 kHz 16-bit mono, and some slack */
28
+ export const SENTENCE_MAX = 60 * 32_000 + 32_000; /* 60 s of 16 kHz 16-bit mono, and some slack */
29
29
  export const TRIES_PER_MINUTE = 10;
30
30
  export const NAME_MAX = 40;
31
31
  /* The volume a box is given when it connects, until someone sets another (its own default was soft). */
@@ -91,11 +91,15 @@ export function forSpeech(text) {
91
91
  .replace(/\[([^\]]+)\]\([^)]+\)/g, '$1')
92
92
  .replace(/https?:\/\/\S+/g, ' ')
93
93
  .replace(/[*_#>|~]+/g, ' ')
94
+ /* Times as said: "14h30" -> "14 heures 30", "14h" -> "14 heures" (read "14 H" otherwise, 31-10). */
95
+ .replace(/\b([01]?\d|2[0-3])\s?[hH](?:\s?([0-5]\d))?\b/g, (_, h, m) => `${h} heure${h === '1' || h === '0' ? '' : 's'}${m ? ` ${m}` : ''}`)
94
96
  .replace(/\s+/g, ' ')
95
97
  .trim()
96
98
  .slice(0, 1500);
97
99
  }
98
100
 
101
+ export const DOING_WITHIN_MS = 120_000;
102
+
99
103
  export class Boxes {
100
104
  #file; #codes = new Map(); #links = new Map(); #tries = []; #now;
101
105
  #exists; #hear; #say; #speak; #log; #changed; #images = new Map(); #origin;
@@ -248,14 +252,24 @@ export class Boxes {
248
252
  /* The tool said or asked something in a conversation: said aloud only by the boxes it is
249
253
  addressed to (`to`: box ids, #215). Anything else stays in the thread, silent -- a box
250
254
  read a whole technical message aloud once (31-10), which nobody wants. */
251
- async agentSaid(conversation, text, to = null) {
255
+ async agentSaid(conversation, text, to = null, { doing = false, asked = false } = {}) {
252
256
  const words = forSpeech(text);
253
- if (!words || !Array.isArray(to) || to.length === 0) return;
254
- const boxes = [...this.#links.values()].filter((l) => l.box().conversation === conversation && to.includes(l.box().id));
257
+ if (!words) return;
258
+ let boxes;
259
+ if (doing) {
260
+ /* "I'm looking in the calendar": once per sentence heard, on the box that heard it, within two minutes. */
261
+ boxes = [...this.#links.values()].filter((l) => l.box().conversation === conversation && l.heardAt && !l.doingSaid && this.#now() - l.heardAt < DOING_WITHIN_MS);
262
+ for (const l of boxes) l.doingSaid = true;
263
+ } else {
264
+ if (!Array.isArray(to) || to.length === 0) return;
265
+ boxes = [...this.#links.values()].filter((l) => l.box().conversation === conversation && to.includes(l.box().id));
266
+ }
255
267
  if (boxes.length === 0) return;
268
+ /* A question: the box listens again right after, no wake word needed. */
269
+ const then = !doing && (asked || /\?\s*$/.test(words)) ? 'listen' : null;
256
270
  let pcm;
257
271
  try { pcm = await this.#speak(words); } catch (error) { this.#log(`voice box: no voice for the answer (${error.message})`); for (const l of boxes) l.send({ t: 'cue', name: 'error' }); return; }
258
- for (const l of boxes) l.play(pcm);
272
+ for (const l of boxes) l.play(pcm, then);
259
273
  }
260
274
 
261
275
  /* A box connected with its key: speaks protocol v1 on `socket` (a ws WebSocket). */
@@ -265,10 +279,10 @@ export class Boxes {
265
279
  const link = {
266
280
  socket, send, waiting: new Map(), heardAt: 0, muted: null, volume: null,
267
281
  box: () => this.#all().find((b) => b.id === box.id) ?? box,
268
- play: (pcm) => {
282
+ play: (pcm, then = null) => {
269
283
  send({ t: 'speak.start', rate: 16_000 });
270
284
  for (let at = 0; at < pcm.length; at += 640) if (socket.readyState === 1) socket.send(pcm.subarray(at, at + 640));
271
- send({ t: 'speak.end' });
285
+ send({ t: 'speak.end', ...(then ? { then } : {}) });
272
286
  },
273
287
  };
274
288
  this.#links.set(box.id, link);
@@ -325,6 +339,7 @@ export class Boxes {
325
339
  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})`); link.send({ t: 'cue', name: 'error' }); return; }
326
340
  this.#log(`voice box ${box.name}: ${seconds} s heard, said in ${box.conversation}`);
327
341
  link.heardAt = this.#now();
342
+ link.doingSaid = false;
328
343
  }
329
344
 
330
345
  /* Wires the box link on an HTTP server: the key in Authorization, else 401. */
package/server/main.mjs CHANGED
@@ -476,7 +476,7 @@ export async function startHome({ port, host, dataDir, mntDir = dataDir, gitRese
476
476
  /* A conversation with a tool on a screen that is not its terminal: see talk.mjs. */
477
477
  /* Voice boxes: what the tool says in their conversation is read aloud (boxes.mjs). */
478
478
  let boxes;
479
- const talk = new Talk({ spoke: (name, text, to) => { void boxes?.agentSaid(name, text, to); }, store: talkStore, words: new Store(`${dataDir}/talk-words.json`), keptDir: `${dataDir}/talk-kept`, vms, conversations: all, resources: folders, toolbox, warm: (name) => bridge?.warm(name), ...(talkEveryMs ? { listenEveryMs: talkEveryMs } : {}),
479
+ const talk = new Talk({ spoke: (name, text, to, how) => { void boxes?.agentSaid(name, text, to, how); }, store: talkStore, words: new Store(`${dataDir}/talk-words.json`), keptDir: `${dataDir}/talk-kept`, vms, conversations: all, resources: folders, toolbox, warm: (name) => bridge?.warm(name), ...(talkEveryMs ? { listenEveryMs: talkEveryMs } : {}),
480
480
  ...(rereadAfterMs === undefined ? {} : { rereadAfterMs }),
481
481
  /* A line typed down the terminal already open when there is one, and
482
482
  what is already known of a machine used instead of asking (talk.mjs). */
@@ -602,10 +602,10 @@ export async function startHome({ port, host, dataDir, mntDir = dataDir, gitRese
602
602
  void team.refresh().catch((error) => console.error(`operators: ${error.message}`));
603
603
  const watching = setInterval(() => { void team.watch().catch(() => {}); }, 60_000); watching.unref?.();
604
604
  }
605
- const speech = new Speech({ dir: `${dataDir}/voices`, log: (line) => events.add({ kind: 'server', text: line }) });
605
+ const speech = new Speech({ dir: `${dataDir}/voices`, log: (line) => events.add({ kind: 'server', text: line }), secret: (name) => { try { return secrets.reveal(name) ?? undefined; } catch { return undefined; } } });
606
606
  boxes = new Boxes({ file: new JsonFile(`${dataDir}/boxes.json`, []),
607
607
  exists: (name) => all.list().some((c) => c.name === name),
608
- hear: (wav) => voice.transcribe(wav),
608
+ hear: (wav) => voice.transcribe(wav), /* the language of the dictation settings, or found by the engine: no language is assumed (a box has no browser to tell one) */
609
609
  /* Said as the person's own message, the room named (one speaker for now). */
610
610
  say: (name, text, by) => talk.say(name, text, undefined, undefined, 'voice', { by }),
611
611
  /* Each conversation's thread knows its boxes: their names, and the ids its agent answers. */
package/server/speech.mjs CHANGED
@@ -35,11 +35,31 @@ export function toPcm16k(samples, from) {
35
35
  return out;
36
36
  }
37
37
 
38
+ /* A NICER VOICE, ONLINE: ElevenLabs, when the owner put its key among the secrets (by this name).
39
+ The text then leaves this server for theirs; without the key, or when it fails (quota, network),
40
+ the voice of this server answers instead. The voice: chosen by ear on the real box (31-10). */
41
+ export const ELEVEN_SECRET = 'ELEVENLABS_API_KEY';
42
+ export const ELEVEN_VOICE = 'onwK4e9ZLuTAKqWW03F9'; /* "Daniel" */
43
+ export const ELEVEN_MODEL = 'eleven_multilingual_v2';
44
+
38
45
  export class Speech {
39
- #dir; #lang; #engine = null; #loading = null; #fetch; #log;
46
+ #dir; #lang; #engine = null; #loading = null; #fetch; #log; #secret;
47
+
48
+ constructor({ dir, lang = 'fr', fetcher = fetch, log = () => {}, secret = () => undefined }) {
49
+ this.#dir = dir; this.#lang = VOICES[lang] ? lang : 'fr'; this.#fetch = fetcher; this.#log = log; this.#secret = secret;
50
+ }
40
51
 
41
- constructor({ dir, lang = 'fr', fetcher = fetch, log = () => {} }) {
42
- this.#dir = dir; this.#lang = VOICES[lang] ? lang : 'fr'; this.#fetch = fetcher; this.#log = log;
52
+ async #eleven(text) {
53
+ const key = this.#secret(ELEVEN_SECRET);
54
+ if (!key) return null;
55
+ try {
56
+ const r = await this.#fetch(`https://api.elevenlabs.io/v1/text-to-speech/${ELEVEN_VOICE}?output_format=pcm_16000`, {
57
+ method: 'POST', headers: { 'xi-api-key': key, 'content-type': 'application/json' },
58
+ body: JSON.stringify({ text, model_id: ELEVEN_MODEL }), signal: AbortSignal.timeout(20_000),
59
+ });
60
+ if (!r.ok) { this.#log(`speech: ElevenLabs answered ${r.status}, this server's voice instead`); return null; }
61
+ return Buffer.from(await r.arrayBuffer());
62
+ } catch (error) { this.#log(`speech: ElevenLabs unreachable (${error.message}), this server's voice instead`); return null; }
43
63
  }
44
64
 
45
65
  get voice() { return VOICES[this.#lang]; }
@@ -81,6 +101,8 @@ export class Speech {
81
101
 
82
102
  /* Text -> PCM 16 kHz. */
83
103
  async speak(text) {
104
+ const online = await this.#eleven(text);
105
+ if (online && online.length) return online;
84
106
  const engine = await this.#load();
85
107
  const audio = engine.generate({ text, sid: 0, speed: 1.0 });
86
108
  return toPcm16k(audio.samples, audio.sampleRate);
package/server/talk.mjs CHANGED
@@ -290,7 +290,9 @@ export class Talk {
290
290
  if (event.type === 'person-reacted') { reacted(event); return; }
291
291
  if (event.type === 'person-wrote') { this.#want(name); return; }
292
292
  /* What the tool says or asks: a voice box of this conversation may read it aloud (boxes.mjs). */
293
- if (event.type === 'agent-said' || event.type === 'agent-asked') this.#spoke(name, event.message?.text ?? '', Array.isArray(event.message?.to) ? event.message.to : null);
293
+ if (event.type === 'agent-said' || event.type === 'agent-asked') this.#spoke(name, event.message?.text ?? '', Array.isArray(event.message?.to) ? event.message.to : null, { asked: event.type === 'agent-asked' });
294
+ /* What it is doing: said by a box that was just spoken to, so nobody waits in silence. */
295
+ if (event.type === 'agent-doing') this.#spoke(name, event.message?.text ?? '', null, { doing: true });
294
296
  const words = texts(this.#lastLang, this.settings().own);
295
297
  if (event.type === 'agent-asked') this.#alert({ title: fill(words['alert-asks'], { name }), body: event.message.text ?? '', url: `/?c=${name}` });
296
298
  if (event.type === 'agent-status' && event.message.state === 'waiting') this.#alert({ title: fill(words['alert-waits'], { name }), body: event.message.text ?? '', url: `/?c=${name}` });