clavarium 0.2.105-beta.1 → 0.2.106-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.105-beta.1",
3
+ "version": "0.2.106-beta.1",
4
4
  "lockfileVersion": 3,
5
5
  "requires": true,
6
6
  "packages": {
7
7
  "": {
8
8
  "name": "clavarium",
9
- "version": "0.2.105-beta.1",
9
+ "version": "0.2.106-beta.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.105-beta.1",
3
+ "version": "0.2.106-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.",
package/server/api.mjs CHANGED
@@ -310,7 +310,7 @@ export async function handleApi(request, response, conversations, headers, resou
310
310
  }));
311
311
  return send(200, history.together(query.get('span') ?? '1h', machines, { memoryMb: a?.memory?.totalMb, cores: room?.cpu?.cores, diskMb: a?.disk?.sizeMb })), true;
312
312
  }
313
- if (pathname === '/api/server/logs' && serverLogs) return send(200, await serverLogs.read(query.get('source') ?? 'clavarium', query.get('span') ?? '1')), true;
313
+ if (pathname === '/api/server/logs' && serverLogs) { const r = await serverLogs.start(query.get('source') ?? 'clavarium', query.get('span') ?? '10m'); return send(r.pending ? 202 : 200, r), true; }
314
314
  if (pathname === '/api/machines/disks' && machinesMove) return send(200, await machinesMove.disks()), true;
315
315
  if (pathname === '/api/machines/places' && machinesMove) return send(200, await machinesMove.places()), true;
316
316
  if (pathname === '/api/machines/moving' && machinesMove) return send(200, await machinesMove.moving()), true;
package/server/boxes.mjs CHANGED
@@ -23,7 +23,6 @@ export const CODE_MINUTES = 10;
23
23
  export const PAIR_PATH = '/c/api/box/pair';
24
24
  export const LINK_PATH = '/c/api/box/ws';
25
25
  export const SENTENCE_MAX = 15 * 32_000 + 32_000; /* 15 s of 16 kHz 16-bit mono, and some slack */
26
- export const ANSWER_MINUTES = 5; /* after a sentence, what the tool says is spoken */
27
26
  export const TRIES_PER_MINUTE = 10;
28
27
  export const NAME_MAX = 40;
29
28
  /* Where a box in setup answers, on its own Wi-Fi: ESP-IDF's access point address, the same on
@@ -171,14 +170,13 @@ export class Boxes {
171
170
  });
172
171
  }
173
172
 
174
- /* The tool said or asked something in a conversation. Addressed (`to`: box ids, #215): said by
175
- those boxes of this conversation. Not addressed: said by its boxes that were just talked to. */
173
+ /* The tool said or asked something in a conversation: said aloud only by the boxes it is
174
+ addressed to (`to`: box ids, #215). Anything else stays in the thread, silent -- a box
175
+ read a whole technical message aloud once (31-10), which nobody wants. */
176
176
  async agentSaid(conversation, text, to = null) {
177
177
  const words = forSpeech(text);
178
- if (!words) return;
179
- const now = this.#now();
180
- const mine = [...this.#links.values()].filter((l) => l.box().conversation === conversation);
181
- const boxes = Array.isArray(to) && to.length ? mine.filter((l) => to.includes(l.box().id)) : mine.filter((l) => l.heardAt > now - ANSWER_MINUTES * 60_000);
178
+ if (!words || !Array.isArray(to) || to.length === 0) return;
179
+ const boxes = [...this.#links.values()].filter((l) => l.box().conversation === conversation && to.includes(l.box().id));
182
180
  if (boxes.length === 0) return;
183
181
  let pcm;
184
182
  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; }
@@ -214,6 +212,7 @@ export class Boxes {
214
212
  try { m = JSON.parse(String(data)); } catch { return; }
215
213
  if (!m || typeof m !== 'object') return;
216
214
  if (m.t === 'ping') { send({ t: 'pong' }); return; }
215
+ if (m.t === 'hello' && typeof m.problem === 'string' && m.problem) this.#log(`voice box ${box.name}: back online after ${Number(m.tries) || '?'} tries; it had: ${m.problem.slice(0, 160)}`);
217
216
  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; }
218
217
  if (m.t === 'listen.start') { sentence = []; size = 0; return; }
219
218
  if (m.t === 'listen.end' && sentence) {
@@ -22,9 +22,34 @@ export function mask(text) {
22
22
  return String(text).replace(NAMED, (all, word, between) => `${word}${between}[masked]`).replace(LONG, '[masked]');
23
23
  }
24
24
 
25
+ /* A read longer than this is answered "still reading": the front cuts a request that waits
26
+ much longer (found 31-10 on a big journal: even 10 minutes took too long there). */
27
+ export const WAIT_MS = 12_000;
28
+ export const KEPT_MS = 120_000;
29
+
25
30
  export class ServerLogs {
26
- #host;
27
- constructor({ host }) { this.#host = host; }
31
+ #host; #reads = new Map(); #now;
32
+ constructor({ host, now = () => Date.now() }) { this.#host = host; this.#now = now; }
33
+
34
+ /* The same read asked again while it runs is the same read; once done, kept a moment for
35
+ the page that comes back for it. Resolves { pending: true } past `wait`. */
36
+ async start(source, span, wait = WAIT_MS) {
37
+ const key = `${source}:${span}`;
38
+ const now = this.#now();
39
+ for (const [k, r] of this.#reads) if (r.done && now - r.done > KEPT_MS) this.#reads.delete(k);
40
+ let r = this.#reads.get(key);
41
+ if (!r) {
42
+ r = { done: 0 };
43
+ r.promise = this.read(source, span).then((v) => { r.value = v; }, (e) => { r.error = e; }).finally(() => { r.done = this.#now(); });
44
+ this.#reads.set(key, r);
45
+ }
46
+ await Promise.race([r.promise, new Promise((ok) => { setTimeout(ok, wait).unref?.(); })]);
47
+ if (!r.done) return { pending: true, source, span: String(span) };
48
+ this.#reads.delete(key);
49
+ if (r.error) throw r.error;
50
+ return r.value;
51
+ }
52
+
28
53
  async read(source, span) {
29
54
  if (!SOURCES.includes(source)) throw new Refusal(400, `Not a source of logs: say one of ${SOURCES.join(', ')}.`);
30
55
  if (!SPANS.includes(String(span))) throw new Refusal(400, 'Not a span: 10 minutes (10m), 1, 6, 24 or 168 hours, or since the server started (boot).');
package/web/app.js CHANGED
@@ -6075,8 +6075,15 @@
6075
6075
  const source = $('logs-source').value; const span = $('logs-span').value;
6076
6076
  $('logs-said').textContent = 'Reading...'; $('logs-show').disabled = true;
6077
6077
  try {
6078
- const r = await fetch(`${API}/server/logs?source=${encodeURIComponent(source)}&span=${encodeURIComponent(span)}`);
6079
- const body = await r.json().catch(() => ({}));
6078
+ /* A big journal takes a while: the server says "still reading" (202), and is asked again. */
6079
+ let r; let body = {};
6080
+ for (let i = 0; i < 30; i++) {
6081
+ r = await fetch(`${API}/server/logs?source=${encodeURIComponent(source)}&span=${encodeURIComponent(span)}`);
6082
+ body = await r.json().catch(() => ({}));
6083
+ if (r.status === 202 || (r.status === 503 && body.down === true)) { $('logs-said').textContent = `Reading... (a big journal: ${(i + 1) * 12} s)`; continue; }
6084
+ break;
6085
+ }
6086
+ if (r.status === 202) throw new Error('The logs take too long to read: try a shorter span.');
6080
6087
  if (!r.ok) throw new Error(body.error ?? `The logs could not be read (${r.status}).`);
6081
6088
  logsShown = body;
6082
6089
  $('logs-text').textContent = body.text || '';