clavarium 0.2.219-next.1 → 0.2.222-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.219-next.1",
3
+ "version": "0.2.222-next.1",
4
4
  "lockfileVersion": 3,
5
5
  "requires": true,
6
6
  "packages": {
7
7
  "": {
8
8
  "name": "clavarium",
9
- "version": "0.2.219-next.1",
9
+ "version": "0.2.222-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.219-next.1",
3
+ "version": "0.2.222-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
@@ -132,6 +132,24 @@ export function sentencesOf(text) {
132
132
  return out.filter(Boolean);
133
133
  }
134
134
 
135
+ /* The words said while an answer is awaited (« Hmm… », « ça prend plus de temps ») -- off (01-11: heard all
136
+ the time while the box misheard, they made it worse; back once the sound path is sound). */
137
+ export const WAITING_WORDS = false;
138
+ /* WHAT THE BOX MEASURED, its sentence ended (firmware 0.1.38): how long, when a voice was heard, the bar it
139
+ used -- beside the message, with what reached this side. */
140
+ export function endOf(ep) {
141
+ if (!ep || typeof ep !== 'object') return '';
142
+ const n = (v) => (Number.isFinite(v) ? v : null);
143
+ const sec = (v) => `${v.toFixed(1).replace('.', ',')} s`;
144
+ const parts = [];
145
+ if (n(ep.s) !== null) parts.push(`écoute ${sec(ep.s)}`);
146
+ parts.push(n(ep.heard_at) !== null && ep.heard_at >= 0 ? `voix dès ${sec(ep.heard_at)}` : 'aucune voix détectée');
147
+ if (n(ep.bar) !== null) parts.push(`seuil ${Math.round(ep.bar)}`);
148
+ if (n(ep.voice) !== null && ep.voice > 0) parts.push(`voix ${Math.round(ep.voice)}`);
149
+ if (n(ep.mean) !== null) parts.push(`moyenne ${Math.round(ep.mean)}`);
150
+ if (n(ep.room) !== null) parts.push(`pièce ${Math.round(ep.room)}`);
151
+ return `boîtier : ${parts.join(', ')}`;
152
+ }
135
153
  export const THINKING_AFTER_MS = 3_500;
136
154
  export const LONGER_AFTER_MS = 12_000;
137
155
  export const THINKING = ['Hmm…', 'Voyons…', 'Hmm, voyons voir…'];
@@ -144,7 +162,24 @@ export function whyOf(m) {
144
162
  return `réveil : ${reason}${score}${cut}`;
145
163
  }
146
164
 
165
+ /* HOW LOUD WHAT THE BOX SENT WAS (01-11): beside the message, so a sentence that came empty (a wake, then
166
+ silence: Whisper then invents « Sous-titrage ») is seen at once, and where it went wrong told apart --
167
+ the box sent nothing, or the dictation misheard. */
168
+ export function levelOf(pcm) {
169
+ const n = pcm.length >> 1; if (!n) return 0;
170
+ let sum = 0; for (let i = 0; i < n; i++) { const v = pcm.readInt16LE(i * 2); sum += v * v; }
171
+ return Math.round(Math.sqrt(sum / n));
172
+ }
173
+ export const QUIET_LEVEL = 150;
174
+ export function soundOf(pcm) {
175
+ const level = levelOf(pcm);
176
+ return `son ${(pcm.length / 32_000).toFixed(1).replace('.', ',')} s, niveau ${level}${level < QUIET_LEVEL ? ' (presque silencieux)' : ''}`;
177
+ }
178
+
147
179
  export const DOING_WITHIN_MS = 120_000;
180
+ export const TEST_SOUND = 'Test du son. Un, deux, trois. Si tu m\'entends, le son marche.';
181
+ export const TEST_MIC = 'Test du micro. Parle-moi, je t\'écoute.';
182
+ export const TEST_MIC_MS = 45_000;
148
183
  /* A LONG RECORDING (a meeting, a class): asked in words, stopped by the button or the wake word. */
149
184
  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;
150
185
  export const RECORD_MAX_BYTES = 4 * 3600 * 32_000; /* four hours */
@@ -288,6 +323,34 @@ export class Boxes {
288
323
  return a;
289
324
  }
290
325
 
326
+ /* TESTED FROM ITS CARD (01-11): the sound -- it says a test sentence -- and the microphone -- it listens
327
+ once, and what it heard is shown on the page, not sent to the conversation. */
328
+ async testSound(id) {
329
+ const link = this.#links.get(id);
330
+ if (!link) throw new Refusal(409, 'This voice box is not connected.');
331
+ link.play(await this.#speak(TEST_SOUND));
332
+ return { ok: true };
333
+ }
334
+ async testMic(id) {
335
+ const link = this.#links.get(id);
336
+ if (!link) throw new Refusal(409, 'This voice box is not connected.');
337
+ link.testing = { until: this.#now() + TEST_MIC_MS, state: 'listening', text: '' };
338
+ link.play(await this.#speak(TEST_MIC), 'listen');
339
+ return { ok: true };
340
+ }
341
+ testResult(id) {
342
+ const t = this.#links.get(id)?.testing;
343
+ if (!t) return { state: 'none', text: '' };
344
+ if (t.state === 'listening' && this.#now() > t.until) t.state = 'nothing';
345
+ return { state: t.state, text: t.text };
346
+ }
347
+ #tested(link, state, text = '') {
348
+ const t = link.testing;
349
+ if (!t || t.state !== 'listening' || this.#now() > t.until) return false;
350
+ t.state = state; t.text = text;
351
+ return true;
352
+ }
353
+
291
354
  /* A sound played on a box (a sample, a recorded message), as its answers are. */
292
355
  play(id, wav) {
293
356
  const link = this.#links.get(id);
@@ -478,7 +541,9 @@ export class Boxes {
478
541
  /* A question: the box listens again right after, no wake word needed. So does a box in CONVERSATION:
479
542
  asked for ("mode conversation") -- never by itself any more (01-11);
480
543
  it ends when nobody answers, or on "fin de la conversation". */
481
- const question = !doing && (asked || /\?\s*$/.test(words));
544
+ /* Only a question asked on purpose (parley ask) opens listening; one that merely ends in « ? » does not
545
+ any more (01-11: each follow-up listen took in the room and came back as invented words). */
546
+ const question = !doing && asked;
482
547
  for (const l of boxes) { l.answeredAt = this.#now(); clearTimeout(l.thinkingA); clearTimeout(l.thinkingB); }
483
548
  for (const l of boxes) {
484
549
  /* An answer -- to a sentence heard within a minute -- may open the conversation; something said
@@ -567,16 +632,29 @@ export class Boxes {
567
632
  }
568
633
  if (m.t === 'event' && m.name === 'wake.own') { this.#log(`voice box ${box.name}: heard its own voice say the wake word, ignored`); return; }
569
634
  if (m.t === 'event' && m.name === 'noise.dropped') { this.#log(`voice box ${box.name}: a sound near full scale was cut by its noise guard`); return; }
635
+ /* The box listens for a sentence while this side still records: the two disagree (a reconnection, a
636
+ lost record.end). The recording is closed here, or the sentence would go into it and arrive empty. */
637
+ if (m.t === 'listen.start' && link.recording) {
638
+ const r = link.recording; link.recording = null;
639
+ this.#log(`voice box ${box.name}: a sentence while recording here, the recording closed`);
640
+ r.out.end(() => { void this.#recorded(link, r); });
641
+ }
570
642
  if (m.t === 'listen.start') link.why = whyOf(m);
571
643
  if (m.t === 'listen.start') { if (this.stop(box.id)) this.#log(`voice box ${box.name}: stopped playing, someone speaks`); sentence = []; size = 0; return; }
572
644
  if (m.t === 'listen.end' && sentence && m.reason === 'nobody') {
573
645
  /* Listened again, nobody spoke: a conversation ends there, in silence. */
574
646
  sentence = null;
647
+ if (this.#tested(link, 'nothing')) { link.send({ t: 'cue', name: 'done' }); return; }
575
648
  if (link.chat) this.#log(`voice box ${box.name}: conversation ended, nobody answered`);
576
649
  link.chat = false; link.recent = [];
577
650
  link.send({ t: 'cue', name: 'done' });
578
651
  return;
579
652
  }
653
+ if (m.t === 'listen.end' && m.ep) {
654
+ const end = endOf(m.ep);
655
+ link.why = `${link.why ? `${link.why} ; ` : ''}fin : ${String(m.reason ?? '').slice(0, 20)} ; ${end}`;
656
+ this.#log(`voice box ${box.name}: listen ended (${String(m.reason ?? '').slice(0, 20)}), ${end}`);
657
+ }
580
658
  if (m.t === 'listen.end' && sentence) {
581
659
  const pcm = Buffer.concat(sentence);
582
660
  sentence = null;
@@ -628,12 +706,12 @@ export class Boxes {
628
706
  async #heard(link, pcm) {
629
707
  const box0 = link.box();
630
708
  const seconds = (pcm.length / 32_000).toFixed(1);
631
- if (pcm.length < 16_000) { this.#log(`voice box ${box0.name}: ${seconds} s heard, too short for a sentence`); link.send({ t: 'cue', name: 'done' }); return; } /* under half a second: a press, not a sentence */
709
+ if (pcm.length < 16_000) { this.#log(`voice box ${box0.name}: ${seconds} s heard, too short for a sentence`); this.#tested(link, 'nothing'); link.send({ t: 'cue', name: 'done' }); return; } /* under half a second: a press, not a sentence */
632
710
  link.send({ t: 'cue', name: 'thinking' });
633
711
  let text;
634
712
  /* A sentence that goes nowhere is said so, in words, rather than with a beep nobody can read (31-10). */
635
713
  const lost = async (words) => {
636
- try { link.play(await this.#speak(words), 'listen'); } catch { link.send({ t: 'cue', name: 'error' }); }
714
+ try { link.play(await this.#speak(words)); } catch { link.send({ t: 'cue', name: 'error' }); } /* not listened after: a loop of « pardon » on room noise (01-11) */
637
715
  };
638
716
  try {
639
717
  const said = [];
@@ -650,8 +728,14 @@ export class Boxes {
650
728
  };
651
729
  for (const piece of piecesOf(pcm)) said.push(String(await hear(wavOf(piece)) ?? '').trim());
652
730
  text = said.filter(Boolean).join(' ');
653
- } catch (error) { this.#log(`voice box ${box0.name}: ${seconds} s heard, dictation failed (${error.message})`); await lost('Pardon, la dictée ne répond pas. Tu peux répéter ?'); return; }
654
- if (!text) { this.#log(`voice box ${box0.name}: ${seconds} s heard, nothing recognised (silence?)`); await lost("Pardon, je n'ai rien compris. Tu peux répéter ?"); return; }
731
+ } catch (error) { this.#log(`voice box ${box0.name}: ${seconds} s heard, dictation failed (${error.message})`); if (this.#tested(link, 'failed', error.message)) { link.send({ t: 'cue', name: 'error' }); return; } await lost('Pardon, la dictée ne répond pas. Tu peux répéter ?'); return; }
732
+ if (link.testing?.state === 'listening' && this.#now() <= link.testing.until) {
733
+ this.#tested(link, text ? 'heard' : 'nothing', text);
734
+ this.#log(`voice box ${box0.name}: microphone test, ${seconds} s heard`);
735
+ link.send({ t: 'cue', name: 'done' });
736
+ return;
737
+ }
738
+ if (!text) { this.#log(`voice box ${box0.name}: ${seconds} s heard, nothing recognised (${soundOf(pcm)})`); await lost("Pardon, je n'ai rien compris. Tu peux répéter ?"); return; }
655
739
  const box = link.box();
656
740
  /* Only a short order starts a recording ("lance l'enregistrement de la réunion"): a sentence that merely
657
741
  mentions recording does not (01-11). */
@@ -665,10 +749,10 @@ export class Boxes {
665
749
  link.relay = 0;
666
750
  text = `(réponse entendue de l'appareil, pas de la personne) ${text}`;
667
751
  }
668
- try { await this.#say(box.conversation, text, { id: box.id, name: box.name, ...(box.place ? { place: box.place } : {}), ...(link.why ? { why: link.why } : {}) }); } 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; }
752
+ try { await this.#say(box.conversation, text, { id: box.id, name: box.name, ...(box.place ? { place: box.place } : {}), why: `${link.why ? `${link.why} ; ` : ''}${soundOf(pcm)}` }); } 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; }
669
753
  this.#log(`voice box ${box.name}: ${seconds} s heard, said in ${box.conversation}`);
670
754
  link.heardAt = this.#now();
671
- this.#waitFor(link, text);
755
+ if (WAITING_WORDS) this.#waitFor(link, text);
672
756
  link.recent = [...(link.recent ?? []), link.heardAt];
673
757
  if (/\bmode conversation\b/i.test(text)) link.chat = true;
674
758
  if (/\b(?:fin|arr[eê]te|stop)\w*\b.{0,20}\b(?:conversation|[ée]couter)\b/i.test(text)) { link.chat = false; link.recent = []; }
@@ -697,6 +781,9 @@ export class Boxes {
697
781
  * POST /api/boxes/<id>/move {conversation}: one conversation at a time
698
782
  * POST /api/boxes/<id>/place {place}: where it is (a home), shown with its name
699
783
  * POST /api/boxes/<id>/zone {zone}: its time zone (IANA), for its clock; the page gives its own
784
+ * POST /api/boxes/<id>/test-sound it says a test sentence
785
+ * POST /api/boxes/<id>/test-mic it listens once; what it heard stays here, not in the conversation
786
+ * POST /api/boxes/<id>/test-result -> {state: listening|heard|nothing|failed|none, text}
700
787
  * POST /api/boxes/<id>/revoke
701
788
  * POST /api/boxes/<id>/volume {level 0-100}
702
789
  * POST /api/boxes/<id>/wifi-scan -> {networks}
@@ -742,6 +829,9 @@ export async function handleBoxes(request, response, boxes, originOk, headers =
742
829
  case 'wifi-forget': return answered(await boxes.command(id, { t: 'wifi.forget', ssid: String(body.ssid ?? '').slice(0, 32) }));
743
830
  case 'wifi-join': return answered(await boxes.command(id, { t: 'wifi.join', ssid: String(body.ssid ?? ''), password: String(body.password ?? '') }, 90_000));
744
831
  case 'alert': return answered(await boxes.command(id, { t: 'alert', text: String(body.text ?? '').slice(0, 300) }, 10 * 60_000));
832
+ case 'test-sound': return send(200, await boxes.testSound(id));
833
+ case 'test-mic': return send(200, await boxes.testMic(id));
834
+ case 'test-result': return send(200, boxes.testResult(id));
745
835
  case 'reboot': return answered(await boxes.command(id, { t: 'reboot' }));
746
836
  default: return false;
747
837
  }
package/web/app.js CHANGED
@@ -8874,6 +8874,35 @@
8874
8874
  const volRow = el('label', 'box-volume'); volRow.append('Volume ', vol, volSaid);
8875
8875
  const acts = el('div', 'action-row'); acts.append(whereRow, place, volRow, rename, remove);
8876
8876
  card.append(head, acts);
8877
+ /* TESTED FROM HERE (01-11): its sound -- it says a test sentence -- and its microphone -- it listens
8878
+ once, and what it heard shows here, not in the conversation. */
8879
+ if (b.online) {
8880
+ const test = el('div', 'action-row box-test');
8881
+ const sound = el('button', '', 'Test the sound'); sound.type = 'button';
8882
+ const mic = el('button', '', 'Test the microphone'); mic.type = 'button';
8883
+ const heard = el('small', 'hint box-test-heard', '');
8884
+ sound.addEventListener('click', async () => {
8885
+ sound.disabled = true; heard.textContent = 'It says a test sentence…';
8886
+ try { await post(`${API}/boxes/${b.id}/test-sound`, {}); heard.textContent = 'Sent: you should hear « Test du son ».'; }
8887
+ catch (error) { heard.textContent = `Not played: ${error.message}`; }
8888
+ sound.disabled = false;
8889
+ });
8890
+ mic.addEventListener('click', async () => {
8891
+ mic.disabled = true; heard.textContent = 'It asks you to speak, then listens…';
8892
+ try { await post(`${API}/boxes/${b.id}/test-mic`, {}); }
8893
+ catch (error) { heard.textContent = `Not started: ${error.message}`; mic.disabled = false; return; }
8894
+ for (let i = 0; i < 50; i++) {
8895
+ await new Promise((r) => setTimeout(r, 1000));
8896
+ const r = await post(`${API}/boxes/${b.id}/test-result`, {}).catch(() => null);
8897
+ if (!r || r.state === 'listening') continue;
8898
+ heard.textContent = r.state === 'heard' ? `Heard: « ${r.text} »` : r.state === 'failed' ? `The dictation failed: ${r.text}` : 'Nothing heard.';
8899
+ break;
8900
+ }
8901
+ mic.disabled = false;
8902
+ });
8903
+ test.append(sound, mic, heard);
8904
+ card.append(test);
8905
+ }
8877
8906
  /* ITS WI-FI NETWORKS (01-11): the box knows several and goes to the strongest it hears; one is added
8878
8907
  here before it moves, from the networks it hears itself -- no phone, no button. */
8879
8908
  if (b.online) {