clavarium 0.2.206-next.1 → 0.2.208-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.206-next.1",
3
+ "version": "0.2.208-next.1",
4
4
  "lockfileVersion": 3,
5
5
  "requires": true,
6
6
  "packages": {
7
7
  "": {
8
8
  "name": "clavarium",
9
- "version": "0.2.206-next.1",
9
+ "version": "0.2.208-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.206-next.1",
3
+ "version": "0.2.208-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,9 @@ export function sentencesOf(text) {
132
132
  return out.filter(Boolean);
133
133
  }
134
134
 
135
+ export const THINKING_AFTER_MS = 3_500;
136
+ export const LONGER_AFTER_MS = 12_000;
137
+ export const THINKING = ['Hmm…', 'Voyons…', 'Hmm, voyons voir…'];
135
138
  export const DOING_WITHIN_MS = 120_000;
136
139
  /* A LONG RECORDING (a meeting, a class): asked in words, stopped by the button or the wake word. */
137
140
  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;
@@ -200,11 +203,12 @@ export function piecesOf(pcm, { least = 8, most = 14 } = {}) {
200
203
 
201
204
  export class Boxes {
202
205
  #file; #codes = new Map(); #links = new Map(); #tries = []; #now;
203
- #exists; #hear; #say; #speak; #log; #changed; #images = new Map(); #origin; #spawn; #fetch; #streams = new Map(); #recordDir; #parts; #keep;
206
+ #exists; #hear; #say; #speak; #log; #changed; #images = new Map(); #origin; #spawn; #fetch; #streams = new Map(); #recordDir; #parts; #keep; #react;
204
207
 
205
208
  /* file: a JsonFile holding [{ id, name, conversation, keyHash, firmware, createdAt, seenAt }].
206
209
  exists(conversation), hear(wav) -> text, say(conversation, text, room), speak(text) -> PCM 16 kHz. */
207
- constructor({ file, exists = () => true, hear, say, speak, changed = () => {}, origin = () => null, now = () => Date.now(), log = () => {}, spawn = spawnProcess, fetcher = fetch, recordDir = null, parts = null, keep = null }) {
210
+ constructor({ file, exists = () => true, hear, say, speak, changed = () => {}, origin = () => null, now = () => Date.now(), log = () => {}, spawn = spawnProcess, fetcher = fetch, recordDir = null, parts = null, keep = null, react = async () => null }) {
211
+ this.#react = react;
208
212
  this.#spawn = spawn; this.#fetch = fetcher; this.#recordDir = recordDir; this.#parts = parts; this.#keep = keep;
209
213
  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;
210
214
  }
@@ -429,6 +433,23 @@ export class Boxes {
429
433
  /* The tool said or asked something in a conversation: said aloud only by the boxes it is
430
434
  addressed to (`to`: box ids, #215). Anything else stays in the thread, silent -- a box
431
435
  read a whole technical message aloud once (31-10), which nobody wants. */
436
+ /* WHILE THE ANSWER IS THOUGHT (01-11): no long silence after a sentence. A short « hmm » if nothing has
437
+ come after a little while, then a word that it takes longer; whatever the assistant says first (an answer,
438
+ what it is doing) ends the wait. A longer question (many words) is told sooner that it takes a moment. */
439
+ #waitFor(link, text = '') {
440
+ clearTimeout(link.thinkingA); clearTimeout(link.thinkingB);
441
+ const said = link.heardAt;
442
+ const still = () => link.heardAt === said && !((link.answeredAt ?? 0) >= said);
443
+ const long = String(text).length > 160; /* a long question: told sooner that it takes a moment */
444
+ const say = async (words) => { if (!still() || link.recording || this.#streams.has(link.box().id)) return; try { link.play(await this.#speak(words)); } catch { /* the light says it */ } };
445
+ /* Made at once, used if the answer is still not there: a reaction to THIS sentence, in its language. */
446
+ let made = null;
447
+ this.#react(text).then((t) => { made = t; }).catch(() => {});
448
+ link.thinkingA = setTimeout(() => { void say(made || THINKING[Math.floor(Math.random() * THINKING.length)]); }, THINKING_AFTER_MS);
449
+ link.thinkingB = setTimeout(() => { void say(long ? 'Ta question est riche, je regarde, un instant.' : 'Ça me demande un peu plus de temps, je regarde.'); }, long ? LONGER_AFTER_MS * 0.6 : LONGER_AFTER_MS);
450
+ link.thinkingA.unref?.(); link.thinkingB.unref?.();
451
+ }
452
+
432
453
  async agentSaid(conversation, text, to = null, { doing = false, asked = false } = {}) {
433
454
  /* What it is doing is written for a screen ("Tâche de fond : bêta 0.2.113…"): read aloud, it was words
434
455
  from nowhere (31-10). Aloud, only a short plain one; otherwise a word that it is at work. */
@@ -449,6 +470,7 @@ export class Boxes {
449
470
  asked for ("mode conversation"), or by itself after two sentences within three minutes (31-10);
450
471
  it ends when nobody answers, or on "fin de la conversation". */
451
472
  const question = !doing && (asked || /\?\s*$/.test(words));
473
+ for (const l of boxes) { l.answeredAt = this.#now(); clearTimeout(l.thinkingA); clearTimeout(l.thinkingB); }
452
474
  for (const l of boxes) {
453
475
  /* An answer -- to a sentence heard within a minute -- may open the conversation; something said
454
476
  of itself (a news item, a reminder) does not: the box listened after it for nothing (31-10). */
@@ -633,6 +655,7 @@ export class Boxes {
633
655
  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; }
634
656
  this.#log(`voice box ${box.name}: ${seconds} s heard, said in ${box.conversation}`);
635
657
  link.heardAt = this.#now();
658
+ this.#waitFor(link, text);
636
659
  link.recent = [...(link.recent ?? []), link.heardAt];
637
660
  if (/\bmode conversation\b/i.test(text)) link.chat = true;
638
661
  if (/\b(?:fin|arr[eê]te|stop)\w*\b.{0,20}\b(?:conversation|[ée]couter)\b/i.test(text)) { link.chat = false; link.recent = []; }
package/server/main.mjs CHANGED
@@ -606,6 +606,7 @@ export async function startHome({ port, host, dataDir, mntDir = dataDir, gitRese
606
606
  boxes = new Boxes({ file: new JsonFile(`${dataDir}/boxes.json`, []),
607
607
  exists: (name) => all.list().some((c) => c.name === name),
608
608
  /* A long recording (a meeting): transcribed in parts, kept beside the conversation for its assistant. */
609
+ react: (said) => voice.reaction(said),
609
610
  recordDir: `${dataDir}/recordings`, parts: (wav) => voice.transcribeParts(wav), keep: (name, file, text) => talk.keepText(name, file, text),
610
611
  hear: (wav) => voice.transcribe(wav, undefined, { oneVoice: true }), /* the language of the dictation settings, or found by the engine: no language is assumed (a box has no browser to tell one) */
611
612
  /* Said as the person's own message, the room named (one speaker for now). */
package/server/voice.mjs CHANGED
@@ -191,6 +191,26 @@ export async function transcribeParts(config, audio, fetcher = fetch) {
191
191
  return text ? [{ speaker: null, start: 0, text }] : [];
192
192
  }
193
193
 
194
+ /* A SHORT REACTION while the answer is thought (a voice box, 01-11): a few words in the person's language that
195
+ acknowledge what was said without answering it -- « bonne question, voyons… ». Only where the dictation's
196
+ service also offers a fast chat model (OVH AI Endpoints); null otherwise, or when it is not quick. */
197
+ export const REACTION_MODEL = 'Mistral-Small-3.2-24B-Instruct-2506';
198
+ export async function quickReaction(config, said, fetcher = fetch) {
199
+ if (!config?.key || !DIARIZES.test(String(config.url))) return null;
200
+ const base = String(config.url).replace(/\/audio\/transcriptions\/?$/, '');
201
+ try {
202
+ const r = await fetcher(`${base}/chat/completions`, {
203
+ method: 'POST', redirect: 'error', headers: { 'content-type': 'application/json', authorization: `Bearer ${config.key}` }, signal: AbortSignal.timeout(3000),
204
+ body: JSON.stringify({ model: REACTION_MODEL, temperature: 0.8, max_tokens: 24, messages: [
205
+ { role: 'system', content: 'Someone just spoke to a voice assistant, which needs a few seconds to answer. Write ONE very short spoken reaction (2 to 7 words) in the SAME language as what they said, natural and warm, that acknowledges it without answering it and without promising anything: e.g. "Bonne question, voyons…", "Ah, intéressant, je regarde.", "Hmm, let me see…". No quotes, no emoji. Answer with the reaction only.' },
206
+ { role: 'user', content: String(said).slice(0, 600) }] }),
207
+ });
208
+ if (!r.ok) return null;
209
+ const t = String((await r.json())?.choices?.[0]?.message?.content ?? '').replace(/["«»]/g, '').trim();
210
+ return t && t.length <= 60 && !/\n/.test(t) ? t : null;
211
+ } catch { return null; }
212
+ }
213
+
194
214
  /* ---------- THE ENGINE CHOSEN IN SETTINGS ----------
195
215
  * Settings > Dictation: a provider fills in the address and the model (both
196
216
  * may be changed); the key is one of the SECRETS already kept (Assets >
@@ -324,6 +344,10 @@ export class Dictation {
324
344
  /* An expired key is never sent: said in one line instead (still offered, so the line is seen). */
325
345
  #fresh(c) { if (c?.secret && this.#expired(c.secret)) throw new Refusal(409, KEY_EXPIRED); }
326
346
  /* `device`: the language of the device that dictates (a tag such as fr-FR), if it said. */
347
+ async reaction(said) {
348
+ const config = this.current();
349
+ return config ? quickReaction(config, said, this.#fetcher) : null;
350
+ }
327
351
  async transcribeParts(audio) {
328
352
  this.#fresh(this.#saved());
329
353
  const config = this.current();