clavarium 0.2.427-next.1 → 0.2.429-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/api.mjs +9 -0
- package/server/conversations.mjs +44 -17
- package/server/jobs.mjs +69 -0
- package/server/main.mjs +5 -1
- package/server/store.mjs +15 -1
- package/web/app.js +52 -2
- package/web/index.html +1 -1
- package/web/style.css +12 -0
package/npm-shrinkwrap.json
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "clavarium",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.429-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.429-next.1",
|
|
10
10
|
"license": "MIT",
|
|
11
11
|
"dependencies": {
|
|
12
12
|
"@xterm/addon-fit": "^0.11.0",
|
package/package.json
CHANGED
package/server/api.mjs
CHANGED
|
@@ -126,6 +126,7 @@
|
|
|
126
126
|
* POST /api/wifi/handover: the network handed over to NetworkManager (back as it was if it does not work)
|
|
127
127
|
* GET /api/meshcentral: the remote screen (MeshCentral); POST /api/meshcentral/enable (turned on, in the background) /disable
|
|
128
128
|
* GET /api/events?kind=&conversation=&q=&before=&limit= what happened, newest first
|
|
129
|
+
* GET /api/jobs what it is doing: { running, done } (jobs.mjs), its updates of the day among the done
|
|
129
130
|
* GET /api/history?of=server|<name>&span=1h|24h|7d the curves of memory, CPU and disk (and the reserve, for the server)
|
|
130
131
|
* GET /api/history/all?span=1h|24h|7d Settings, Usage: the server and every machine (asleep too) on one time axis,
|
|
131
132
|
* in GB and cores: {t, capacity, server, itself, machines: [{name, alloc, asleep, ...}]}
|
|
@@ -406,6 +407,14 @@ export async function handleApi(request, response, conversations, headers, resou
|
|
|
406
407
|
/* Who may sign in (operators.mjs): none in local mode. */
|
|
407
408
|
const reactOf = /^\/api\/conversations\/([^/]+)\/health-react$/.exec(pathname);
|
|
408
409
|
if (reactOf && reacting) return send(200, reacting.settings(decodeURIComponent(reactOf[1]))), true;
|
|
410
|
+
if (pathname === '/api/jobs' && conversations.jobs) {
|
|
411
|
+
const said = conversations.jobs.list(conversations.list?.() ?? []);
|
|
412
|
+
const day = Date.now() - 24 * 3600_000;
|
|
413
|
+
const updated = (updates?.about().history ?? []).filter((h) => h.at && Date.parse(h.at) >= day)
|
|
414
|
+
.map((h) => ({ what: `Clavarium ${h.action === 'rollback' ? 'back to' : 'updated to'} ${h.to ?? '?'}`, endedAt: h.at, result: h.result === 'done' ? 'done' : 'failed', ...(h.result === 'done' ? {} : { why: h.why ?? h.result ?? '' }) }));
|
|
415
|
+
const done = [...said.done, ...updated].sort((a, b) => String(b.endedAt).localeCompare(String(a.endedAt)));
|
|
416
|
+
return send(200, { running: said.running, done }), true;
|
|
417
|
+
}
|
|
409
418
|
if (pathname === '/api/events' && events) {
|
|
410
419
|
const ask = { kind: query.get('kind') ?? '', conversation: query.get('conversation') ?? '', q: query.get('q') ?? '', before: query.get('before') ?? '', limit: Math.min(1000, Number(query.get('limit')) || 200) };
|
|
411
420
|
const said = events.list(ask);
|
package/server/conversations.mjs
CHANGED
|
@@ -21,6 +21,8 @@ import { join } from 'node:path';
|
|
|
21
21
|
export const SESSION = 'main';
|
|
22
22
|
export const STOP_DELAY_MS = 60_000;
|
|
23
23
|
const STEP_MS = 15 * 60_000;
|
|
24
|
+
/* A copy reading its original's memory: nothing for this long, and it is ended (and why said). */
|
|
25
|
+
export const COPY_QUIET_MS = Number(process.env.CLAVARIUM_COPY_QUIET_MS) || 3 * 60_000;
|
|
24
26
|
/* A size in words: 840 KB, 12 MB, 1.4 GB. */
|
|
25
27
|
const sizeWords = (bytes) => (bytes >= 1024 ** 3 ? `${(bytes / 1024 ** 3).toFixed(1)} GB` : bytes >= 1024 ** 2 ? `${Math.round(bytes / 1024 ** 2)} MB` : `${Math.round(bytes / 1024)} KB`);
|
|
26
28
|
/* The door of a conversation in a shared machine (#221), put in the machine by the server. */
|
|
@@ -317,7 +319,8 @@ export class Conversations {
|
|
|
317
319
|
return there.map((x) => x.machineMb).find(Number.isInteger) ?? Math.max(...there.map((x) => Number(x.memoryMb) || 0));
|
|
318
320
|
}
|
|
319
321
|
|
|
320
|
-
|
|
322
|
+
/* `after`: a promise its build waits for (a copy reads its original first); resolving false, no build. */
|
|
323
|
+
create({ name: given, title: read, tool, memoryMb, cpus, diskGb, workOnDisk, group, machine, machineMb, after }) {
|
|
321
324
|
const { name, title } = this.#naming({ name: given, title: read });
|
|
322
325
|
/* IN A SHARED MACHINE (#221), named: its work is always kept here (mounted into its place there). */
|
|
323
326
|
if (machine !== undefined && (typeof machine !== 'string' || !isConversationName(machine))) throw new Refusal(400, 'A machine name is 1 to 32 lower-case letters, digits or hyphens; it starts with a letter.');
|
|
@@ -351,7 +354,7 @@ export class Conversations {
|
|
|
351
354
|
const { order, groups } = this.#order();
|
|
352
355
|
this.#store.put(conversation);
|
|
353
356
|
this.#keepOrder({ order: [name, ...order], groups: group && !groups.includes(group) ? [...groups, group] : groups });
|
|
354
|
-
this.built = this.#build(name, chosen, memory);
|
|
357
|
+
this.built = after === undefined ? this.#build(name, chosen, memory) : Promise.resolve(after).then((go) => (go === false || !this.#store.get(name) ? undefined : this.#build(name, chosen, memory)));
|
|
355
358
|
return conversation;
|
|
356
359
|
}
|
|
357
360
|
|
|
@@ -945,9 +948,12 @@ export class Conversations {
|
|
|
945
948
|
/* In its machine: the manifest written aside, then one archive -- the manifest first, then what is kept
|
|
946
949
|
(a path it has not made yet left out), the cache left out. */
|
|
947
950
|
/* (Its paths are plain -- letters, digits, . _ / - -- checked by tools.mjs: given as words.) */
|
|
948
|
-
const script = '
|
|
951
|
+
const script = 'm=$(mktemp -d) || exit 2; printf "%s" "$1" > "$m/clavarium-conversation.json"; '
|
|
949
952
|
+ 'ex=""; for a in $2; do ex="$ex --exclude=./$a"; done; ks=""; for a in $3; do if [ -e "$HOME/$a" ]; then ks="$ks ./$a"; fi; done; '
|
|
950
|
-
+ 'tar -czf - -C "$m" clavarium-conversation.json -C "$HOME" $ex $ks
|
|
953
|
+
+ 'rc=0; tar -czf - --warning=no-file-changed -C "$m" clavarium-conversation.json -C "$HOME" $ex $ks || rc=$?; rm -rf "$m"; '
|
|
954
|
+
/* 1: a file changed while read (its tool writing its log as it is read, 14-11: a live Claude Code): the
|
|
955
|
+
archive is whole all the same -- what was there, read; only a real error (2) fails. */
|
|
956
|
+
+ '[ "$rc" -le 1 ]';
|
|
951
957
|
const argv = this.#vms.execArgv(name, ['sh', '-c', script, 'export', manifest, tool.state.skip.join(' '), tool.state.keep.join(' ')]);
|
|
952
958
|
const child = spawn(argv[0], argv.slice(1), { stdio: ['ignore', 'pipe', 'pipe'] });
|
|
953
959
|
/* What it said when it failed: kept short, for the message that says why (a copy, a move). */
|
|
@@ -1010,30 +1016,26 @@ export class Conversations {
|
|
|
1010
1016
|
const memory = shared ? Math.max(tool.memoryMb, Math.min(Number(c.memoryMb) || tool.memoryMb, Number(size) || SHARED_MB)) : Number(c.memoryMb) || tool.memoryMb;
|
|
1011
1017
|
const label = typeof title === 'string' && title.trim() ? title.trim() : `${c.title ?? c.name} (copy)`;
|
|
1012
1018
|
await this.admit({ title: label, tool: c.tool, memoryMb: memory, ...(shared ? { machine, machineMb } : {}) });
|
|
1013
|
-
|
|
1019
|
+
/* Its tool's memory is read FIRST, its original then put back to sleep, and only then its machine built: the
|
|
1020
|
+
two never run at once (14-11: a new 4 GB machine and the woken original together starved the server). */
|
|
1021
|
+
let open; const gate = new Promise((ok) => { open = ok; });
|
|
1022
|
+
const made = this.create({ title: label, tool: c.tool, memoryMb: memory, ...(shared ? { machine, machineMb } : { cpus: c.cpus, diskGb: c.diskGb }), ...(c.group ? { group: c.group } : {}), after: gate });
|
|
1014
1023
|
/* Before its build can end (it awaits first): held, never usable half made. */
|
|
1015
1024
|
this.#holding.add(made.name);
|
|
1016
1025
|
this.#store.patch(made.name, { copyOf: name });
|
|
1017
1026
|
const building = this.built;
|
|
1018
|
-
this.copied = this.#copying(name, made.name, c, tool, building);
|
|
1027
|
+
this.copied = this.#copying(name, made.name, c, tool, building, open);
|
|
1019
1028
|
return this.#store.get(made.name);
|
|
1020
1029
|
}
|
|
1021
1030
|
|
|
1022
|
-
async #copying(from, to, c, tool, building) {
|
|
1031
|
+
async #copying(from, to, c, tool, building, open = () => {}) {
|
|
1023
1032
|
const dir = mkdtempSync(join(tmpdir(), 'clv-copy-'));
|
|
1024
1033
|
const carried = join(dir, 'state.tar.gz');
|
|
1025
1034
|
let woke = false;
|
|
1026
1035
|
try {
|
|
1027
|
-
await building;
|
|
1028
|
-
const built = this.#store.get(to);
|
|
1029
|
-
if (!built) return undefined; /* erased meanwhile */
|
|
1030
|
-
if (built.phase === 'failed') throw new Error(built.error ?? 'its machine could not be made.');
|
|
1031
1036
|
/* Said as it goes, once a second at most: a long copy is seen moving (13-11, "on ne sait pas si ça marche"). */
|
|
1032
1037
|
let said = 0;
|
|
1033
1038
|
const say = (step) => { const now = Date.now(); if (now - said < 1000) return; said = now; if (this.#store.get(to)) this.#store.patch(to, { step }); };
|
|
1034
|
-
this.#store.patch(to, { step: 'Copying its work' });
|
|
1035
|
-
const work = await this.copyWork(from, to, (p) => say(`Copying its work: ${p.files} file${p.files === 1 ? '' : 's'}, ${sizeWords(p.bytes)}`));
|
|
1036
|
-
if (work?.files !== undefined) this.#store.patch(to, { step: `Copying its work: ${work.files} file${work.files === 1 ? '' : 's'}, ${sizeWords(work.bytes)}, done` });
|
|
1037
1039
|
if (tool.state) {
|
|
1038
1040
|
this.#store.patch(to, { step: `Copying ${tool.label}'s memory` });
|
|
1039
1041
|
const state = await this.#vms.state(from, { fresh: true }).catch(() => undefined);
|
|
@@ -1057,16 +1059,40 @@ export class Conversations {
|
|
|
1057
1059
|
this.#store.patch(to, { step: `Copying ${tool.label}'s memory` });
|
|
1058
1060
|
const out = await this.exportState(from);
|
|
1059
1061
|
child = out.child;
|
|
1060
|
-
let read = 0;
|
|
1061
|
-
out.stream.on('data', (d) => { read += d.length; say(`Copying ${tool.label}'s memory: ${sizeWords(read)} read`); });
|
|
1062
|
+
let read = 0; let lastAt = Date.now(); let stalled = false;
|
|
1063
|
+
out.stream.on('data', (d) => { read += d.length; lastAt = Date.now(); say(`Copying ${tool.label}'s memory: ${sizeWords(read)} read`); });
|
|
1064
|
+
/* Nothing comes for a while: said, with how long; for too long, the reading is ended and why said --
|
|
1065
|
+
never an endless wait nobody can read (14-11: stuck at "36 KB read"). */
|
|
1066
|
+
const watch = setInterval(() => {
|
|
1067
|
+
const quiet = Math.round((Date.now() - lastAt) / 1000);
|
|
1068
|
+
if (quiet >= COPY_QUIET_MS / 1000) { stalled = true; child.kill('SIGKILL'); return; }
|
|
1069
|
+
if (quiet >= 15) say(`Copying ${tool.label}'s memory: ${sizeWords(read)} read, nothing more for ${quiet} s`);
|
|
1070
|
+
}, 5000);
|
|
1062
1071
|
const failed = new Promise((ok) => child.on('error', () => ok(-1)));
|
|
1063
1072
|
const file = createWriteStream(carried, { mode: 0o600 });
|
|
1064
1073
|
out.stream.pipe(file);
|
|
1065
|
-
code = await Promise.race([new Promise((ok) => child.on('close', ok)), failed]);
|
|
1074
|
+
code = await Promise.race([new Promise((ok) => child.on('close', ok)), failed]).finally(() => clearInterval(watch));
|
|
1075
|
+
if (stalled) {
|
|
1076
|
+
const now = await this.#vms.state(from, { fresh: true }).catch(() => undefined);
|
|
1077
|
+
throw new Error(`its original stopped sending its tool's memory (${sizeWords(read)} read, then nothing for ${COPY_QUIET_MS / 60_000} min; its machine is ${now ?? 'in a state that could not be read'})`);
|
|
1078
|
+
}
|
|
1066
1079
|
await new Promise((ok) => (file.writableFinished ? ok() : file.on('finish', ok)));
|
|
1067
1080
|
if (code !== 255) break;
|
|
1068
1081
|
}
|
|
1069
1082
|
if (code !== 0) throw new Error(`its tool's memory could not be read (exit ${code}${firstLine(child.said) ? `: ${firstLine(child.said)}` : ''}).`);
|
|
1083
|
+
/* Read: its original back to sleep at once, before the copy's machine is made. */
|
|
1084
|
+
if (woke) { await this.sleep(from).catch(() => {}); woke = false; }
|
|
1085
|
+
this.#borrowed.delete(from);
|
|
1086
|
+
}
|
|
1087
|
+
open(true);
|
|
1088
|
+
await building;
|
|
1089
|
+
const built = this.#store.get(to);
|
|
1090
|
+
if (!built) return undefined; /* erased meanwhile */
|
|
1091
|
+
if (built.phase === 'failed') throw new Error(built.error ?? 'its machine could not be made.');
|
|
1092
|
+
this.#store.patch(to, { step: 'Copying its work' });
|
|
1093
|
+
const work = await this.copyWork(from, to, (p) => say(`Copying its work: ${p.files} file${p.files === 1 ? '' : 's'}, ${sizeWords(p.bytes)}`));
|
|
1094
|
+
if (work?.files !== undefined) this.#store.patch(to, { step: `Copying its work: ${work.files} file${work.files === 1 ? '' : 's'}, ${sizeWords(work.bytes)}, done` });
|
|
1095
|
+
if (tool.state) {
|
|
1070
1096
|
if ((await this.#vms.state(to, { fresh: true }).catch(() => undefined)) !== 'Running') {
|
|
1071
1097
|
const started = await this.#vms.start(to);
|
|
1072
1098
|
if (started.code !== 0) throw new Error(`its machine did not start: ${firstLine(started.stderr) || `exit ${started.code}`}.`);
|
|
@@ -1082,6 +1108,7 @@ export class Conversations {
|
|
|
1082
1108
|
this.ready(to);
|
|
1083
1109
|
return undefined;
|
|
1084
1110
|
} catch (error) {
|
|
1111
|
+
open(false);
|
|
1085
1112
|
this.#holding.delete(to);
|
|
1086
1113
|
if (this.#store.get(to)) this.#store.patch(to, { phase: 'failed', step: undefined, progress: undefined, buildingSince: undefined, error: `Not copied: ${String(error?.message ?? error).replace(/\.?$/, '.')} Its original is untouched; erase this copy.` });
|
|
1087
1114
|
return undefined;
|
package/server/jobs.mjs
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
/* WHAT CLAVARIUM IS DOING (the « under way » badge, 14-11): every conversation in a busy phase -- built, copied,
|
|
2
|
+
* moved, restarted -- is a job under way, its step said as it goes; when it leaves that phase the job ends, done
|
|
3
|
+
* or failed with why, and is kept a day. Nothing is asked of the machines: it is read off the conversations' own
|
|
4
|
+
* record as it changes (Store's onChange), so it adds no wait.
|
|
5
|
+
*
|
|
6
|
+
* seen(before, after) a conversation's record changed (before or after undefined: made or removed)
|
|
7
|
+
* list(now?) { running: [...], done: [...] } newest first, done within KEEP_MS
|
|
8
|
+
*/
|
|
9
|
+
import { JsonFile } from './store.mjs';
|
|
10
|
+
|
|
11
|
+
export const BUSY = ['building', 'moving', 'restarting'];
|
|
12
|
+
const KEEP_MS = 24 * 3600_000;
|
|
13
|
+
const DONE_MAX = 50;
|
|
14
|
+
|
|
15
|
+
const titleOf = (c) => c?.title ?? c?.name ?? '?';
|
|
16
|
+
|
|
17
|
+
/* Its words: what it is, from the phase and the record (a copy names its original, a move its machine). */
|
|
18
|
+
export function jobWords(c) {
|
|
19
|
+
if (c.phase === 'moving') return `Moving ${titleOf(c)}${c.sharedMachine ? ` into « ${c.sharedMachine} »` : ''}`;
|
|
20
|
+
if (c.phase === 'restarting') return `Restarting ${titleOf(c)}`;
|
|
21
|
+
if (c.copyOf) return `Copy ${titleOf(c)}${c.sharedMachine ? ` in « ${c.sharedMachine} »` : ''}`;
|
|
22
|
+
return `Building ${titleOf(c)}${c.sharedMachine ? ` in « ${c.sharedMachine} »` : ''}`;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export class Jobs {
|
|
26
|
+
#open = new Map();
|
|
27
|
+
#file;
|
|
28
|
+
#done;
|
|
29
|
+
#now;
|
|
30
|
+
|
|
31
|
+
constructor({ path, now = () => Date.now() } = {}) {
|
|
32
|
+
this.#now = now;
|
|
33
|
+
this.#file = path ? new JsonFile(path, []) : undefined;
|
|
34
|
+
const kept = this.#file?.get();
|
|
35
|
+
this.#done = Array.isArray(kept) ? kept : [];
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
seen(before, after) {
|
|
39
|
+
const name = after?.name ?? before?.name;
|
|
40
|
+
if (!name) return;
|
|
41
|
+
const busy = after && BUSY.includes(after.phase);
|
|
42
|
+
if (busy && !this.#open.has(name)) this.#open.set(name, { conversation: name, what: jobWords(after), startedAt: new Date(this.#now()).toISOString() });
|
|
43
|
+
else if (busy) this.#open.get(name).what = jobWords(after);
|
|
44
|
+
if (busy || !this.#open.has(name)) return;
|
|
45
|
+
const job = this.#open.get(name);
|
|
46
|
+
this.#open.delete(name);
|
|
47
|
+
const ok = after?.phase === 'ready';
|
|
48
|
+
this.#end({ ...job, endedAt: new Date(this.#now()).toISOString(), result: after ? (ok ? 'done' : 'failed') : 'removed', ...(after && !ok && after.error ? { why: String(after.error).slice(0, 300) } : {}) });
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
#end(job) {
|
|
52
|
+
const since = this.#now() - KEEP_MS;
|
|
53
|
+
this.#done = [job, ...this.#done.filter((j) => Date.parse(j.endedAt) >= since)].slice(0, DONE_MAX);
|
|
54
|
+
this.#file?.set(this.#done);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/* `records`: the conversations as they are now, for their live step. */
|
|
58
|
+
list(records = []) {
|
|
59
|
+
const since = this.#now() - KEEP_MS;
|
|
60
|
+
/* Read off the records: one busy since before a restart of the server is under way too, and ends here. */
|
|
61
|
+
for (const c of records) if (BUSY.includes(c.phase) && !this.#open.has(c.name)) this.seen(undefined, c);
|
|
62
|
+
const byName = new Map(records.map((c) => [c.name, c]));
|
|
63
|
+
const running = [...this.#open.values()].map((j) => {
|
|
64
|
+
const c = byName.get(j.conversation);
|
|
65
|
+
return { ...j, ...(c ? { what: jobWords(c), step: c.step ?? '', progress: c.progress } : {}) };
|
|
66
|
+
});
|
|
67
|
+
return { running, done: this.#done.filter((j) => Date.parse(j.endedAt) >= since) };
|
|
68
|
+
}
|
|
69
|
+
}
|
package/server/main.mjs
CHANGED
|
@@ -93,6 +93,7 @@ import { ApiKeys, handleExternal, handleApiKeys, statusOf } from './apikeys.mjs'
|
|
|
93
93
|
import { RelayJoin, handleRelayJoin, RELAY_JOIN_PATH } from './relayjoin.mjs';
|
|
94
94
|
import { WebSocketServer } from 'ws';
|
|
95
95
|
import { copyWorkFolder } from './copywork.mjs';
|
|
96
|
+
import { Jobs } from './jobs.mjs';
|
|
96
97
|
|
|
97
98
|
const ROOT = fileURLToPath(new URL('..', import.meta.url));
|
|
98
99
|
const MAINTENANCE = 'deploy/maintenance.html';
|
|
@@ -312,12 +313,15 @@ export async function startHome({ port, host, dataDir, mntDir = dataDir, gitRese
|
|
|
312
313
|
let repairTerminal = async () => { throw new Error('not ready'); };
|
|
313
314
|
const asHost = runner(host);
|
|
314
315
|
/* A conversation in a shared machine (#221) says which: the machine its VM operations reach. */
|
|
315
|
-
const
|
|
316
|
+
const jobs = new Jobs({ path: `${dataDir}/jobs.json` });
|
|
317
|
+
const conversationStore = new Store(`${dataDir}/conversations.json`, { onChange: (before, after) => jobs.seen(before, after) });
|
|
316
318
|
const vms = new Vms(asHost, host, {
|
|
317
319
|
machineOf: (name) => { const m = conversationStore.get(name)?.sharedMachine; return typeof m === 'string' && m ? m : undefined; },
|
|
318
320
|
userOf: (name) => { const u = conversationStore.get(name)?.machineUser; return u && Number.isInteger(u.uid) && Number.isInteger(u.gid) ? u : undefined; },
|
|
319
321
|
});
|
|
320
322
|
const all = conversations ?? new Conversations({ store: conversationStore, layout: new JsonFile(`${dataDir}/layout.json`, {}), vms, tools, ...(stopDelayMs === undefined ? {} : { stopDelayMs }) });
|
|
323
|
+
/* What it is doing (jobs.mjs): read by GET /api/jobs. */
|
|
324
|
+
if (!all.jobs) all.jobs = jobs;
|
|
321
325
|
/* Who may do what with what: ONE table, shared by everything below. */
|
|
322
326
|
const grants = new Grants(new Store(`${dataDir}/grants.json`));
|
|
323
327
|
/* Folder-style boxes the owner drops assets into, to tidy the views and hand a whole box at once (groups.mjs). */
|
package/server/store.mjs
CHANGED
|
@@ -10,9 +10,12 @@ import { dirname } from 'node:path';
|
|
|
10
10
|
export class Store {
|
|
11
11
|
#path;
|
|
12
12
|
#items;
|
|
13
|
+
/* Told of each change, its record before and after (undefined: made or removed): jobs.mjs reads it. */
|
|
14
|
+
onChange;
|
|
13
15
|
|
|
14
|
-
constructor(path) {
|
|
16
|
+
constructor(path, { onChange } = {}) {
|
|
15
17
|
this.#path = path;
|
|
18
|
+
this.onChange = onChange;
|
|
16
19
|
mkdirSync(dirname(path), { recursive: true });
|
|
17
20
|
try { this.#items = JSON.parse(readFileSync(path, 'utf8')); } catch { this.#items = []; }
|
|
18
21
|
if (!Array.isArray(this.#items)) this.#items = [];
|
|
@@ -23,21 +26,32 @@ export class Store {
|
|
|
23
26
|
|
|
24
27
|
put(conversation) {
|
|
25
28
|
const i = this.#items.findIndex((x) => x.name === conversation.name);
|
|
29
|
+
const before = i >= 0 ? { ...this.#items[i] } : undefined;
|
|
26
30
|
if (i >= 0) this.#items[i] = { ...conversation }; else this.#items.push({ ...conversation });
|
|
27
31
|
this.#save();
|
|
32
|
+
this.#told(before, conversation);
|
|
28
33
|
}
|
|
29
34
|
|
|
30
35
|
patch(name, changes) {
|
|
31
36
|
const c = this.#items.find((x) => x.name === name);
|
|
32
37
|
if (!c) return undefined;
|
|
38
|
+
const before = { ...c };
|
|
33
39
|
Object.assign(c, changes);
|
|
34
40
|
this.#save();
|
|
41
|
+
this.#told(before, c);
|
|
35
42
|
return { ...c };
|
|
36
43
|
}
|
|
37
44
|
|
|
38
45
|
remove(name) {
|
|
46
|
+
const before = this.get(name);
|
|
39
47
|
this.#items = this.#items.filter((x) => x.name !== name);
|
|
40
48
|
this.#save();
|
|
49
|
+
if (before) this.#told(before, undefined);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
#told(before, after) {
|
|
53
|
+
if (!this.onChange) return;
|
|
54
|
+
try { this.onChange(before, after ? { ...after } : undefined); } catch { /* a listener never breaks a write */ }
|
|
41
55
|
}
|
|
42
56
|
|
|
43
57
|
#save() {
|
package/web/app.js
CHANGED
|
@@ -1978,10 +1978,56 @@
|
|
|
1978
1978
|
} catch { /* a server without VMs has no list: the page is one terminal */ }
|
|
1979
1979
|
/* The panel is open: what the answerer did meanwhile is said there. */
|
|
1980
1980
|
if (!DEVICE && panelShown) { void paintAnswerer(); paintWakeups(); paintMachine(conversations.find((x) => x.name === current)); }
|
|
1981
|
+
if (!DEVICE && home) void paintJobs();
|
|
1981
1982
|
/* A start told step by step is asked about more often: its tool appears soon after its last step. */
|
|
1982
1983
|
if (home) pollTimer = setTimeout(refresh, conversations.some(holds) || conversations.some((c) => c.machine) ? 1500 : conversations.some((c) => c.phase === 'building' || c.phase === 'moving' || c.phase === 'restarting') ? 2500 : 10000);
|
|
1983
1984
|
}
|
|
1984
1985
|
|
|
1986
|
+
/* ---------- WHAT IT IS DOING (jobs.mjs, 14-11) ----------
|
|
1987
|
+
A badge by the list's title while something runs (« ● 2 under way »), or when one failed since the list
|
|
1988
|
+
was last opened (« ● 1 failed », red); nothing otherwise. Tapped: what runs, its step; then the day's ends,
|
|
1989
|
+
done or failed with why. A line opens its conversation. Read with the list, nothing asked of the machines. */
|
|
1990
|
+
let jobsNow = { running: [], done: [] };
|
|
1991
|
+
const JOBS_SEEN = 'clavarium.jobsSeen';
|
|
1992
|
+
const jobsSeen = () => { try { return localStorage.getItem(JOBS_SEEN) ?? ''; } catch { return ''; } };
|
|
1993
|
+
async function paintJobs() {
|
|
1994
|
+
try { const r = await fetch(`${API}/jobs`); if (!r.ok) return; jobsNow = await r.json(); } catch { return; }
|
|
1995
|
+
const failed = jobsNow.done.filter((j) => j.result === 'failed' && String(j.endedAt) > jobsSeen()).length;
|
|
1996
|
+
const badge = $('jobs-open');
|
|
1997
|
+
const n = jobsNow.running.length;
|
|
1998
|
+
badge.hidden = !n && !failed;
|
|
1999
|
+
badge.dataset.tone = !n && failed ? 'bad' : '';
|
|
2000
|
+
badge.textContent = n ? `${n} under way` : `${failed} failed`;
|
|
2001
|
+
badge.title = n ? jobsNow.running.map((j) => j.what).join(' · ') : 'Tap to see what failed';
|
|
2002
|
+
if (!$('jobs').hidden) paintJobsList();
|
|
2003
|
+
}
|
|
2004
|
+
function paintJobsList() {
|
|
2005
|
+
const line = (j, mark, result) => {
|
|
2006
|
+
const b = document.createElement('button'); b.type = 'button'; b.className = 'job'; b.dataset.result = result;
|
|
2007
|
+
const when = j.endedAt ? new Date(j.endedAt).toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' }) : '';
|
|
2008
|
+
b.append(Object.assign(document.createElement('span'), { textContent: mark }), Object.assign(document.createElement('span'), { textContent: j.what }),
|
|
2009
|
+
Object.assign(document.createElement('small'), { textContent: result === 'running' ? (j.step || 'Starting') : [j.why, when].filter(Boolean).join(' · ') }));
|
|
2010
|
+
if (j.conversation && conversations.some((c) => c.name === j.conversation)) b.addEventListener('click', () => { closeJobs(); select(j.conversation, { step: true }); });
|
|
2011
|
+
else b.disabled = true;
|
|
2012
|
+
return b;
|
|
2013
|
+
};
|
|
2014
|
+
const head = (text) => Object.assign(document.createElement('h3'), { textContent: text });
|
|
2015
|
+
const parts = [];
|
|
2016
|
+
if (jobsNow.running.length) parts.push(head('Under way'), ...jobsNow.running.map((j) => line(j, '⟳', 'running')));
|
|
2017
|
+
if (jobsNow.done.length) parts.push(head('Today'), ...jobsNow.done.map((j) => line(j, j.result === 'done' ? '✓' : '✗', j.result === 'done' ? 'done' : 'failed')));
|
|
2018
|
+
if (!parts.length) parts.push(Object.assign(document.createElement('p'), { className: 'hint', textContent: 'Nothing under way.' }));
|
|
2019
|
+
$('jobs').replaceChildren(...parts);
|
|
2020
|
+
}
|
|
2021
|
+
function closeJobs() { $('jobs').hidden = true; $('jobs-open').setAttribute('aria-expanded', 'false'); }
|
|
2022
|
+
$('jobs-open').addEventListener('click', () => {
|
|
2023
|
+
if (!$('jobs').hidden) { closeJobs(); return; }
|
|
2024
|
+
paintJobsList(); $('jobs').hidden = false; $('jobs-open').setAttribute('aria-expanded', 'true');
|
|
2025
|
+
try { localStorage.setItem(JOBS_SEEN, new Date().toISOString()); } catch { /* kept for this visit only */ }
|
|
2026
|
+
if (!jobsNow.running.length) $('jobs-open').hidden = true;
|
|
2027
|
+
});
|
|
2028
|
+
document.addEventListener('click', (event) => { if (!$('jobs').hidden && !event.target.closest('#jobs, #jobs-open')) closeJobs(); });
|
|
2029
|
+
document.addEventListener('keydown', (event) => { if (event.key === 'Escape' && !$('jobs').hidden) closeJobs(); });
|
|
2030
|
+
|
|
1985
2031
|
/* ---------- A NEW CONVERSATION: a page of its own, as Settings ----------
|
|
1986
2032
|
"+ New" opens it beside the list (the whole screen on a phone), with an
|
|
1987
2033
|
address of its own (?open=new); Back, Escape, Cancel and its "<" link
|
|
@@ -3387,8 +3433,12 @@
|
|
|
3387
3433
|
box.hidden = !wanted;
|
|
3388
3434
|
/* Where the tool talks through the app, the chat is the whole screen: one box to write in. */
|
|
3389
3435
|
/* Its chat off, on this server's own page: said, with the button that turns it on (a device's page keeps the plain screen). */
|
|
3390
|
-
|
|
3391
|
-
|
|
3436
|
+
/* Not known yet -- the list not read, its chat not asked, or it is being built or moved: nothing said
|
|
3437
|
+
rather than a wrong screen (14-11: « chat is off », then « Its screen: connecting », then the chat). */
|
|
3438
|
+
const here = conversations.find((x) => x.name === current);
|
|
3439
|
+
const unknown = !DEVICE && (!here || here.phase !== 'ready' || talkOf !== current);
|
|
3440
|
+
const offer = !unknown && !DEVICE && !wanted && !noTerminal();
|
|
3441
|
+
$('simple-plain').hidden = wanted || offer || unknown;
|
|
3392
3442
|
$('chat-turn').hidden = !offer;
|
|
3393
3443
|
if (offer) {
|
|
3394
3444
|
const possible = !(talkOf === current && talk.possible === false);
|
package/web/index.html
CHANGED
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
<link rel="stylesheet" href="/vendor/diff2html.css">
|
|
15
15
|
<div id="app" data-place="conversations">
|
|
16
16
|
<aside id="list" hidden>
|
|
17
|
-
<div class="top"><b id="list-title" title="Conversations">Clavarium</b><span class="top-acts"><button type="button" id="list-pin" class="icon-btn" aria-pressed="false" aria-label="Keep the list open" title="Keep the list open: it no longer folds when a conversation is opened"><svg class="ic" viewBox="0 0 24 24" aria-hidden="true"><path d="M12 17v5"/><path d="M9 10.76a2 2 0 0 1-1.11 1.79l-1.78.9A2 2 0 0 0 5 15.24V16a1 1 0 0 0 1 1h12a1 1 0 0 0 1-1v-.76a2 2 0 0 0-1.11-1.79l-1.78-.9A2 2 0 0 1 15 10.76V7a1 1 0 0 1 1-1 2 2 0 0 0 0-4H8a2 2 0 0 0 0 4 1 1 0 0 1 1 1z"/></svg></button><button type="button" id="install-top" class="icon-btn" hidden aria-label="Install as an app" title="Install as an app: its own icon, full screen, no browser bar"><svg class="ic" viewBox="0 0 24 24" aria-hidden="true"><path d="M12 3v12"/><path d="m7 10 5 5 5-5"/><path d="M5 21h14"/></svg></button><button type="button" id="search-open" class="icon-btn" aria-label="Search conversations" title="Search (/ or Ctrl+K)" aria-expanded="false" aria-controls="search"><svg class="ic" viewBox="0 0 24 24" aria-hidden="true"><circle cx="11" cy="11" r="7"/><path d="m20 20-3.5-3.5"/></svg></button><button type="button" id="new" class="icon-btn new-btn" aria-label="New conversation" title="New conversation"><svg class="ic" viewBox="0 0 24 24" aria-hidden="true"><path d="M12 5v14"/><path d="M5 12h14"/></svg></button></span></div>
|
|
17
|
+
<div class="top"><b id="list-title" title="Conversations">Clavarium</b><!-- WHAT IT IS DOING (jobs.mjs, 14-11): shown only while something runs, or failed unseen. --><button type="button" id="jobs-open" class="jobs-badge" hidden aria-expanded="false" aria-controls="jobs"></button><span class="top-acts"><button type="button" id="list-pin" class="icon-btn" aria-pressed="false" aria-label="Keep the list open" title="Keep the list open: it no longer folds when a conversation is opened"><svg class="ic" viewBox="0 0 24 24" aria-hidden="true"><path d="M12 17v5"/><path d="M9 10.76a2 2 0 0 1-1.11 1.79l-1.78.9A2 2 0 0 0 5 15.24V16a1 1 0 0 0 1 1h12a1 1 0 0 0 1-1v-.76a2 2 0 0 0-1.11-1.79l-1.78-.9A2 2 0 0 1 15 10.76V7a1 1 0 0 1 1-1 2 2 0 0 0 0-4H8a2 2 0 0 0 0 4 1 1 0 0 1 1 1z"/></svg></button><button type="button" id="install-top" class="icon-btn" hidden aria-label="Install as an app" title="Install as an app: its own icon, full screen, no browser bar"><svg class="ic" viewBox="0 0 24 24" aria-hidden="true"><path d="M12 3v12"/><path d="m7 10 5 5 5-5"/><path d="M5 21h14"/></svg></button><button type="button" id="search-open" class="icon-btn" aria-label="Search conversations" title="Search (/ or Ctrl+K)" aria-expanded="false" aria-controls="search"><svg class="ic" viewBox="0 0 24 24" aria-hidden="true"><circle cx="11" cy="11" r="7"/><path d="m20 20-3.5-3.5"/></svg></button><button type="button" id="new" class="icon-btn new-btn" aria-label="New conversation" title="New conversation"><svg class="ic" viewBox="0 0 24 24" aria-hidden="true"><path d="M12 5v14"/><path d="M5 12h14"/></svg></button></span><div id="jobs" class="jobs-pop" role="dialog" aria-label="Under way" hidden></div></div>
|
|
18
18
|
<!-- SEARCH, discreet: opened by the magnifier, "/" or Ctrl+K; filters as it is typed. -->
|
|
19
19
|
<div id="search" role="search" hidden><input id="search-field" type="search" placeholder="Search" aria-label="Search conversations: name, folder, identifier, tool" autocomplete="off" spellcheck="false" maxlength="60" aria-controls="items"></div>
|
|
20
20
|
<!-- Needs you and Recent: copies of rows of the list below, by when they were last used (app.js, paint). -->
|
package/web/style.css
CHANGED
|
@@ -1425,6 +1425,18 @@ select.pill { appearance: auto; }
|
|
|
1425
1425
|
/* ---------- the list: search, folders, moved by hand ---------- */
|
|
1426
1426
|
#list .top-acts { display: flex; align-items: center; gap: 6px; }
|
|
1427
1427
|
#list-title { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; min-width: 0; }
|
|
1428
|
+
/* What it is doing (14-11): a small badge by the title, its list folded under it. */
|
|
1429
|
+
#list .top { position: relative; }
|
|
1430
|
+
.jobs-badge { display: inline-flex; align-items: center; gap: 6px; flex: none; margin-left: 8px; margin-right: auto; padding: 2px 10px; min-height: 0; border: 1px solid var(--brass); border-radius: 999px; background: transparent; color: var(--brass-ink); font-size: .75rem; font-weight: 600; white-space: nowrap; }
|
|
1431
|
+
.jobs-badge::before { content: ""; width: 7px; height: 7px; border-radius: 50%; background: currentColor; }
|
|
1432
|
+
.jobs-badge[data-tone="bad"] { border-color: var(--danger); color: var(--danger); }
|
|
1433
|
+
.jobs-pop { position: absolute; z-index: 30; top: 100%; left: 8px; right: 8px; margin-top: 4px; min-width: 260px; max-height: 60vh; overflow: auto; background: var(--panel); border: 1px solid var(--line); border-radius: 10px; box-shadow: 0 10px 30px rgb(0 0 0 / .35); padding: 10px 12px; display: flex; flex-direction: column; gap: 8px; }
|
|
1434
|
+
.jobs-pop h3 { margin: 2px 0 0; font-size: .8125rem; }
|
|
1435
|
+
.jobs-pop .job { display: grid; grid-template-columns: 16px 1fr; gap: 2px 8px; text-align: left; background: transparent; border: 0; padding: 4px 0; color: var(--ink); font: inherit; font-size: .8125rem; cursor: pointer; }
|
|
1436
|
+
.jobs-pop .job small { grid-column: 2; color: var(--ink2); overflow-wrap: anywhere; }
|
|
1437
|
+
.jobs-pop .job[data-result="done"] > span:first-child { color: var(--ok); }
|
|
1438
|
+
.jobs-pop .job[data-result="failed"] { color: var(--danger); }
|
|
1439
|
+
.jobs-pop .hint { margin: 0; }
|
|
1428
1440
|
.icon-btn { display: inline-flex; align-items: center; justify-content: center; width: 34px; height: 34px; padding: 0; background: transparent; border-color: transparent; color: var(--ink2); }
|
|
1429
1441
|
.icon-btn:hover, .icon-btn[aria-expanded="true"] { color: inherit; border-color: var(--line); }
|
|
1430
1442
|
.icon-btn .ic { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; }
|