clavarium 0.2.303-next.1 → 0.2.305-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.
- package/npm-shrinkwrap.json +2 -2
- package/package.json +1 -1
- package/server/boxes.mjs +4 -3
- package/server/speech.mjs +28 -2
package/npm-shrinkwrap.json
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "clavarium",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.305-next.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.305-next.1",
|
|
10
10
|
"license": "MIT",
|
|
11
11
|
"dependencies": {
|
|
12
12
|
"@xterm/addon-fit": "^0.11.0",
|
package/package.json
CHANGED
package/server/boxes.mjs
CHANGED
|
@@ -29,7 +29,7 @@ export const FIRMWARE_PATH = '/c/api/box/firmware/';
|
|
|
29
29
|
export const FIRMWARE_MINUTES = 10;
|
|
30
30
|
/* THE WAKE WORD HEARD AGAIN (03-11): « Mésange » and what a dictation makes of it (mes anges, mais sange...). */
|
|
31
31
|
export const CLIP_MAX_BYTES = 16_000 * 2 * 3;
|
|
32
|
-
export const WAKE_HEARD = /\bm[ae]i?s+\s?an?ge?s?\b|\bmes anges?\b|\bmesange|\bmaisange|\bmessage?s?\b/;
|
|
32
|
+
export const WAKE_HEARD = /\bm[ae]i?s+\s?an?ge?s?\b|\bmes anges?\b|\bmesange|\bmaisange|\bmessage?s?\b|\bmessenge?s?\b/; /* « Messenges »: Whisper's reading of the server's own voice saying it */
|
|
33
33
|
export const WAKE_DROP = process.env.CLAVARIUM_WAKE_DROP === '1';
|
|
34
34
|
const plainWords = (t) => String(t).toLowerCase().normalize('NFD').replace(/[\u0300-\u036f]/g, '').replace(/[^a-z\s]/g, ' ').replace(/\s+/g, ' ').trim();
|
|
35
35
|
export const SENTENCE_MAX = 300 * 32_000 + 32_000; /* 5 min of 16 kHz 16-bit mono, and some slack */
|
|
@@ -668,7 +668,7 @@ export class Boxes {
|
|
|
668
668
|
this.#log(`voice box ${box.name}: a sentence while recording here, the recording closed`);
|
|
669
669
|
r.out.end(() => { void this.#recorded(link, r); });
|
|
670
670
|
}
|
|
671
|
-
if (m.t === 'listen.start') { link.why = whyOf(m);
|
|
671
|
+
if (m.t === 'listen.start') { link.why = whyOf(m); link.wakeCheck = null; } /* its clip, if any, comes after listen.start: an older check never carries over */
|
|
672
672
|
if (m.t === 'listen.start') { if (this.stop(box.id)) this.#log(`voice box ${box.name}: stopped playing, someone speaks`); sentence = []; size = 0; return; }
|
|
673
673
|
if (m.t === 'listen.end' && sentence && m.reason === 'nobody') {
|
|
674
674
|
/* Listened again, nobody spoke: a conversation ends there, in silence. */
|
|
@@ -792,7 +792,8 @@ export class Boxes {
|
|
|
792
792
|
turned on (WAKE_DROP), after its results were looked at -- a real call must never be lost to it. */
|
|
793
793
|
const check = link.wakeCheck ? await link.wakeCheck.catch(() => null) : null; link.wakeCheck = null;
|
|
794
794
|
if (check) {
|
|
795
|
-
|
|
795
|
+
/* First in the line: Parley keeps 120 characters of `why`, and the end stats after it can pass that. */
|
|
796
|
+
link.why = `mot ${check.ok ? '\u2713' : '\u2717'} « ${check.heard.slice(0, 30)} »${link.why ? ` · ${link.why}` : ''}`;
|
|
796
797
|
this.#log(`voice box ${box.name}: wake word heard again: ${check.ok ? 'yes' : 'NO'} (« ${check.heard.slice(0, 40)} »)`);
|
|
797
798
|
if (!check.ok && WAKE_DROP) { link.send({ t: 'cue', name: 'done' }); return; }
|
|
798
799
|
}
|
package/server/speech.mjs
CHANGED
|
@@ -43,10 +43,24 @@ export const ELEVEN_VOICE = 'onwK4e9ZLuTAKqWW03F9'; /* "Daniel" */
|
|
|
43
43
|
export const ELEVEN_MODEL = 'eleven_multilingual_v2';
|
|
44
44
|
export const SPEED = 1.1; /* a little quicker than the voices' own pace: found slow on the real box (31-10) */
|
|
45
45
|
|
|
46
|
+
/* WHY ELEVENLABS SAID NO, from its answer's body ({detail: {status|code, message}}): credits used up and a key
|
|
47
|
+
refused are not fixed the same way (04-11: a 401 every sentence, the quota spent). */
|
|
48
|
+
export function elevenRefusal(status, body) {
|
|
49
|
+
const d = body?.detail ?? {};
|
|
50
|
+
const what = `${d.status ?? ''} ${d.code ?? ''} ${d.message ?? ''}`.toLowerCase();
|
|
51
|
+
if (/quota|credit|character_limit/.test(what)) return 'credits used up';
|
|
52
|
+
if (/missing_permission/.test(what)) return 'key lacks the text-to-speech permission';
|
|
53
|
+
if (status === 401 || /invalid_api_key|unauthori/.test(what)) return 'key refused';
|
|
54
|
+
if (status === 429) return 'too many requests';
|
|
55
|
+
return `answered ${status}`;
|
|
56
|
+
}
|
|
57
|
+
/* After a refusal, ElevenLabs is left alone this long (the same key), then asked again: said once, not each sentence. */
|
|
58
|
+
export const ELEVEN_REST_MS = 60 * 60_000;
|
|
59
|
+
|
|
46
60
|
/* A moment before the main voice is asked again (a sentence it missed). */
|
|
47
61
|
export const AGAIN_MS = 700;
|
|
48
62
|
export class Speech {
|
|
49
|
-
#dir; #lang; #engine = null; #loading = null; #fetch; #log; #secret; #pocket; #pocketDown = 0;
|
|
63
|
+
#dir; #lang; #engine = null; #loading = null; #fetch; #log; #secret; #pocket; #pocketDown = 0; #elevenOff = null;
|
|
50
64
|
|
|
51
65
|
/* `pocket`: the default voice (pocket.mjs), else null. Then ElevenLabs when its key is set, then Piper. */
|
|
52
66
|
constructor({ dir, lang = 'fr', fetcher = fetch, log = () => {}, secret = () => undefined, pocket = null }) {
|
|
@@ -56,12 +70,24 @@ export class Speech {
|
|
|
56
70
|
async #eleven(text) {
|
|
57
71
|
const key = this.#secret(ELEVEN_SECRET);
|
|
58
72
|
if (!key) return null;
|
|
73
|
+
/* Refused for this very key not long ago: not asked, not said again. A new key is tried at once. */
|
|
74
|
+
if (this.#elevenOff?.key === key && Date.now() < this.#elevenOff.until) return null;
|
|
59
75
|
try {
|
|
60
76
|
const r = await this.#fetch(`https://api.elevenlabs.io/v1/text-to-speech/${ELEVEN_VOICE}?output_format=pcm_16000`, {
|
|
61
77
|
method: 'POST', headers: { 'xi-api-key': key, 'content-type': 'application/json' },
|
|
62
78
|
body: JSON.stringify({ text, model_id: ELEVEN_MODEL, voice_settings: { stability: 0.5, similarity_boost: 0.75, speed: SPEED } }), signal: AbortSignal.timeout(20_000),
|
|
63
79
|
});
|
|
64
|
-
if (!r.ok) {
|
|
80
|
+
if (!r.ok) {
|
|
81
|
+
const why = elevenRefusal(r.status, await r.json().catch(() => undefined));
|
|
82
|
+
if (r.status === 401 || r.status === 402 || r.status === 429) {
|
|
83
|
+
const again = this.#elevenOff?.key === key && this.#elevenOff.why === why;
|
|
84
|
+
this.#elevenOff = { key, why, until: Date.now() + ELEVEN_REST_MS };
|
|
85
|
+
if (again) return null;
|
|
86
|
+
}
|
|
87
|
+
this.#log(`speech: ElevenLabs: ${why}, this server's voice instead`);
|
|
88
|
+
return null;
|
|
89
|
+
}
|
|
90
|
+
this.#elevenOff = null;
|
|
65
91
|
return Buffer.from(await r.arrayBuffer());
|
|
66
92
|
} catch (error) { this.#log(`speech: ElevenLabs unreachable (${error.message}), this server's voice instead`); return null; }
|
|
67
93
|
}
|