clavarium 0.2.261-next.1 → 0.2.263-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/config/launch/claude-code.sh +21 -19
- package/config/launch/mcp-relay.mjs +98 -0
- package/npm-shrinkwrap.json +2 -2
- package/package.json +1 -1
- package/server/guide.mjs +2 -0
- package/server/main.mjs +15 -1
- package/server/talk.mjs +10 -0
|
@@ -17,29 +17,31 @@ esac
|
|
|
17
17
|
[ $# -gt 0 ] || { echo "launch: no command to run" >&2; exit 64; }
|
|
18
18
|
node "$(dirname "$0")/json-set.mjs" .claude/settings.json "$keys" \
|
|
19
19
|
|| echo "launch: the settings of Claude Code were not changed: it may ask before acting" >&2
|
|
20
|
-
#
|
|
21
|
-
#
|
|
22
|
-
#
|
|
23
|
-
# in its settings first, since asking Claude Code takes seconds. Nothing stops the start.
|
|
20
|
+
# ITS TOOLS (MCP), EACH THROUGH THE RELAY (02-11): a tool updated on the machine is taken up between two of
|
|
21
|
+
# its requests -- whatever the tool, the session never restarted, nothing for the agent to do. Declared
|
|
22
|
+
# once, by Claude Code itself (its other servers kept); an older declaration without the relay is replaced.
|
|
23
|
+
# Looked for in its settings first, since asking Claude Code takes seconds. Nothing stops the start.
|
|
24
|
+
relay="$(dirname "$0")/mcp-relay.mjs"
|
|
25
|
+
tool() { # NAME FOLDER COMMAND ARGS...
|
|
26
|
+
name="$1"; folder="$2"; shift 2
|
|
27
|
+
if grep -q "\"$folder\"" "$HOME/.claude.json" 2>/dev/null && grep -q "mcp-relay.mjs" "$HOME/.claude.json" 2>/dev/null \
|
|
28
|
+
&& node -e 'const d=require(process.argv[1]);const s=d.mcpServers?.[process.argv[2]];const a=JSON.stringify(s?.args??[]);process.exit(a.includes("mcp-relay.mjs")&&a.includes(JSON.stringify(process.argv[3]))?0:1)' "$HOME/.claude.json" "$name" "$1" 2>/dev/null; then return 0; fi
|
|
29
|
+
claude mcp remove --scope user "$name" >/dev/null 2>&1
|
|
30
|
+
claude mcp add --scope user "$name" -- node "$relay" "$@" >/dev/null 2>&1 \
|
|
31
|
+
|| echo "launch: $name was not given to Claude Code as tools: the command $name still works" >&2
|
|
32
|
+
}
|
|
33
|
+
# Parley, the chat of the conversation, when it has one: the version installed on the machine (parley-mcp),
|
|
34
|
+
# else the copy beside the chat.
|
|
24
35
|
chat="${PARLEY_DIR:-/work/.parley}"
|
|
25
|
-
if [ -f "$chat/server.mjs" ]
|
|
26
|
-
|
|
27
|
-
|| echo "launch: Parley was not given to Claude Code as tools: the command parley still works" >&2
|
|
36
|
+
if [ -f "$chat/server.mjs" ]; then
|
|
37
|
+
if command -v parley-mcp >/dev/null 2>&1; then tool parley "$chat" parley-mcp --dir "$chat"; else tool parley "$chat" node "$chat/server.mjs" --dir "$chat"; fi
|
|
28
38
|
fi
|
|
29
|
-
#
|
|
30
|
-
# reminders it sets itself. The same as the command "docket", installed in every machine.
|
|
39
|
+
# Its docket, with its chat: what woke it, to say done or failed; the reminders it sets itself.
|
|
31
40
|
docket="/work/.docket"
|
|
32
|
-
if [ -f "$chat/server.mjs" ] && command -v docket-mcp >/dev/null 2>&1
|
|
33
|
-
|
|
34
|
-
|| echo "launch: Docket was not given to Claude Code as tools: the command docket still works" >&2
|
|
35
|
-
fi
|
|
36
|
-
# ITS TETHER AS ITS OWN TOOLS (MCP): the accesses it was given -- a mailbox, a calendar, an address
|
|
37
|
-
# book, an online folder, a machine over SSH. The server holds the keys; it acts within its rights.
|
|
41
|
+
if [ -f "$chat/server.mjs" ] && command -v docket-mcp >/dev/null 2>&1; then tool docket "$docket" docket-mcp --dir "$docket"; fi
|
|
42
|
+
# Its tether: the accesses it was given (a mailbox, a calendar, a folder, a machine over SSH).
|
|
38
43
|
tether="/work/.tether"
|
|
39
|
-
if command -v tether-mcp >/dev/null 2>&1
|
|
40
|
-
claude mcp add --scope user tether -- tether-mcp --dir "$tether" >/dev/null 2>&1 \
|
|
41
|
-
|| echo "launch: Tether was not given to Claude Code as tools: the command tether still works" >&2
|
|
42
|
-
fi
|
|
44
|
+
if command -v tether-mcp >/dev/null 2>&1; then tool tether "$tether" tether-mcp --dir "$tether"; fi
|
|
43
45
|
# ITS PULSE: Claude Code's own hooks say to the chat that it acts, and when its turn ends, so that
|
|
44
46
|
# the person sees "Working..." while it works without a word (parley pulse; nothing posted).
|
|
45
47
|
if [ -f "$chat/server.mjs" ]; then
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
/* TAKEN UP WITHOUT A RESTART (02-11), FOR EVERY TOOL OF EVERY CONVERSATION: the launcher gives Claude Code
|
|
2
|
+
* each MCP server through this relay -- `node mcp-relay.mjs COMMAND ARGS...` -- so a tool updated on the
|
|
3
|
+
* machine is taken up between two requests, whatever the tool, with nothing for the agent to do. Its
|
|
4
|
+
* version is the package.json of the package its command comes from.
|
|
5
|
+
*
|
|
6
|
+
* (Below, the relay as each Clavarium tool also carries it.)
|
|
7
|
+
* An MCP server lives as long as the session that started it: a new
|
|
8
|
+
* version installed under it was not seen until the session itself was restarted ("on ne peut pas mettre à
|
|
9
|
+
* jour un composant sans redémarrer la session ?"). So the server runs as a child of this relay; between two
|
|
10
|
+
* requests, when the version installed on disk is no longer the child's, the relay starts the new one, says
|
|
11
|
+
* hello to it as the session did, and tells the session its tools may have changed. Nothing is lost: a
|
|
12
|
+
* request in flight is answered by the child it went to, the next goes to the new one. */
|
|
13
|
+
import { spawn } from 'node:child_process';
|
|
14
|
+
import { readFileSync } from 'node:fs';
|
|
15
|
+
import { createInterface } from 'node:readline';
|
|
16
|
+
|
|
17
|
+
/* -> the version installed now, read from package.json (null if unreadable: keep the child). */
|
|
18
|
+
export const installedVersion = (packageJson) => { try { return String(JSON.parse(readFileSync(packageJson, 'utf8')).version ?? '') || null; } catch { return null; } };
|
|
19
|
+
|
|
20
|
+
export function relay({ command = process.execPath, args, packageJson, input = process.stdin, output = process.stdout, spawner = spawn, version = installedVersion, log = () => {} }) {
|
|
21
|
+
let child = null; let running = null; let hello = null; let helloDone = false;
|
|
22
|
+
const pending = new Set(); const queue = [];
|
|
23
|
+
const send = (m) => output.write(`${JSON.stringify(m)}\n`);
|
|
24
|
+
const start = () => {
|
|
25
|
+
running = version(packageJson);
|
|
26
|
+
const c = spawner(command, args, { stdio: ['pipe', 'pipe', 'inherit'], env: { ...process.env, MCP_RELAYED: '1' } });
|
|
27
|
+
child = c;
|
|
28
|
+
createInterface({ input: c.stdout }).on('line', (line) => {
|
|
29
|
+
let m; try { m = JSON.parse(line); } catch { return; }
|
|
30
|
+
if (m && typeof m.id === 'string' && m.id.startsWith('relay:')) return; /* its answer to our own hello */
|
|
31
|
+
if (m && m.id !== undefined) pending.delete(m.id);
|
|
32
|
+
output.write(`${line}\n`);
|
|
33
|
+
if (pending.size === 0) flush();
|
|
34
|
+
});
|
|
35
|
+
c.on('exit', () => {
|
|
36
|
+
if (child !== c) return;
|
|
37
|
+
child = null;
|
|
38
|
+
/* It stopped under a request: answered, never left waiting; the next one starts it again. */
|
|
39
|
+
for (const id of pending) send({ jsonrpc: '2.0', id, error: { code: -32603, message: 'The tool stopped; try again.' } });
|
|
40
|
+
pending.clear(); flush();
|
|
41
|
+
});
|
|
42
|
+
return c;
|
|
43
|
+
};
|
|
44
|
+
const greet = (c) => {
|
|
45
|
+
if (!hello) return;
|
|
46
|
+
c.stdin.write(`${JSON.stringify({ ...hello, id: `relay:${Date.now()}` })}\n`);
|
|
47
|
+
c.stdin.write(`${JSON.stringify({ jsonrpc: '2.0', method: 'notifications/initialized' })}\n`);
|
|
48
|
+
};
|
|
49
|
+
/* Between requests: the new version if there is one (once nothing is in flight), then what waited. */
|
|
50
|
+
const needsNew = () => { if (!child) return true; if (!helloDone) return false; const now = version(packageJson); return Boolean(now && now !== running); };
|
|
51
|
+
const flush = () => {
|
|
52
|
+
while (queue.length) {
|
|
53
|
+
if (needsNew()) {
|
|
54
|
+
if (pending.size > 0) return;
|
|
55
|
+
const old = child; const was = running;
|
|
56
|
+
const c = start(); greet(c);
|
|
57
|
+
log(`relay: ${was} -> ${running}`);
|
|
58
|
+
try { old?.stdin.end(); old?.kill(); } catch { /* gone already */ }
|
|
59
|
+
if (helloDone) send({ jsonrpc: '2.0', method: 'notifications/tools/list_changed' });
|
|
60
|
+
}
|
|
61
|
+
const m = queue.shift();
|
|
62
|
+
if (m.id !== undefined) pending.add(m.id);
|
|
63
|
+
child.stdin.write(`${JSON.stringify(m)}\n`);
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
start();
|
|
67
|
+
createInterface({ input }).on('line', (line) => {
|
|
68
|
+
let m; try { m = JSON.parse(line); } catch { return; }
|
|
69
|
+
if (!m || typeof m !== 'object') return;
|
|
70
|
+
if (m.method === 'initialize') { hello = m; }
|
|
71
|
+
if (m.method === 'notifications/initialized') helloDone = true;
|
|
72
|
+
queue.push(m);
|
|
73
|
+
flush();
|
|
74
|
+
});
|
|
75
|
+
input.on('end', () => { try { child?.stdin.end(); } catch { /* gone */ } });
|
|
76
|
+
return { get child() { return child; }, get version() { return running; } };
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/* The package a command comes from: its file followed to the end of its links, then up to a package.json. */
|
|
80
|
+
import { realpathSync, existsSync, statSync } from 'node:fs';
|
|
81
|
+
import { delimiter, dirname, join, isAbsolute } from 'node:path';
|
|
82
|
+
import { fileURLToPath } from 'node:url';
|
|
83
|
+
export function packageOf(command, path = process.env.PATH ?? '') {
|
|
84
|
+
const found = isAbsolute(command) ? command : path.split(delimiter).map((d) => join(d, command)).find((f) => { try { return statSync(f).isFile(); } catch { return false; } });
|
|
85
|
+
if (!found) return null;
|
|
86
|
+
let at; try { at = dirname(realpathSync(found)); } catch { return null; }
|
|
87
|
+
for (let i = 0; i < 6; i++) { if (existsSync(join(at, 'package.json'))) return join(at, 'package.json'); const up = dirname(at); if (up === at) break; at = up; }
|
|
88
|
+
return null;
|
|
89
|
+
}
|
|
90
|
+
const HERE = fileURLToPath(import.meta.url);
|
|
91
|
+
const main = (() => { try { return realpathSync(process.argv[1] ?? '') === realpathSync(HERE); } catch { return false; } })();
|
|
92
|
+
if (main) {
|
|
93
|
+
const [command, ...args] = process.argv.slice(2);
|
|
94
|
+
if (!command) { process.stderr.write('mcp-relay: COMMAND [ARGS...]\n'); process.exit(64); }
|
|
95
|
+
/* A node script given by its path (node /x/server.mjs): its package is that of the script. */
|
|
96
|
+
const target = /(^|\/)node$/.test(command) && args[0] ? args[0] : command;
|
|
97
|
+
relay({ command, args, packageJson: packageOf(target) ?? '/nonexistent/package.json' });
|
|
98
|
+
}
|
package/npm-shrinkwrap.json
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "clavarium",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.263-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.263-next.1",
|
|
10
10
|
"license": "MIT",
|
|
11
11
|
"dependencies": {
|
|
12
12
|
"@xterm/addon-fit": "^0.11.0",
|
package/package.json
CHANGED
package/server/guide.mjs
CHANGED
|
@@ -121,6 +121,8 @@ export function ownText({ name, tool, workKept, resources = [], chat, online, se
|
|
|
121
121
|
if (chat) out.push('- The chat shows Markdown (lists, tables, "> [!IMPORTANT]") and pictures of /work: .');
|
|
122
122
|
/* The thread is kept whole: after a restart, its end is read, and further back on demand. */
|
|
123
123
|
if (chat && reread) out.push(`- After a restart: ${plain(reread, 60)}, the end of the chat; it says how to read further back.`);
|
|
124
|
+
/* 02-11: the one command a tool may ask of the server. */
|
|
125
|
+
if (chat) out.push('- To be restarted (new tools to take up, or anything stuck): finish what runs, then `touch /work/.parley/restart-asked`; the server restarts you once your turn ends, and your work picks up where it was.');
|
|
124
126
|
/* Its docket (dockets.mjs): what wakes it waits there until it says done or failed. */
|
|
125
127
|
if (chat) out.push('- What wakes you (a file arrived, the clock, a call) waits in your docket: when told "run docket", run it, do each, then docket done ID or docket failed ID "why". To be reminded later: docket remind --at "2026-10-23 09:00" (or --in 2h, --cron "0 9 * * 1") "what for". docket --help says all.');
|
|
126
128
|
/* Its lists (binders.mjs): kept on the server, asked for through .binder; a person approves. */
|
package/server/main.mjs
CHANGED
|
@@ -524,6 +524,20 @@ export async function startHome({ port, host, dataDir, mntDir = dataDir, gitRese
|
|
|
524
524
|
try { if (talk.event(name, { kind: 'tools-updated', text: 'Tools updated: restart it to use them all. Its work picks up where it was.', topic: 'Tools', actions: ['restart'] })) toolbox.told(name); } catch { /* its chat is off */ }
|
|
525
525
|
}
|
|
526
526
|
});
|
|
527
|
+
/* A TOOL THAT ASKS TO BE RESTARTED (02-11: to take up its tools, after an update): restarted by this server
|
|
528
|
+
once its turn has ended (its pulse no longer busy), said in its chat; once per ten minutes at most. */
|
|
529
|
+
const restartedAt = new Map();
|
|
530
|
+
const restartTimer = setInterval(() => {
|
|
531
|
+
for (const name of all.awake()) {
|
|
532
|
+
if (!talk.restartAsked(name)) continue;
|
|
533
|
+
if (talk.pulse(name)?.busy) continue;
|
|
534
|
+
talk.restartTaken(name);
|
|
535
|
+
if (Date.now() - (restartedAt.get(name) ?? 0) < 10 * 60_000) continue;
|
|
536
|
+
restartedAt.set(name, Date.now());
|
|
537
|
+
try { talk.event(name, { kind: 'restarted', text: 'Restarted as it asked: its work picks up where it was.', topic: 'Restarted' }); } catch { /* its chat is off */ }
|
|
538
|
+
void all.restart(name).catch(() => {});
|
|
539
|
+
}
|
|
540
|
+
}, 15_000); restartTimer.unref?.();
|
|
527
541
|
/* The health of each machine, and folders that came loose put back: see health.mjs. */
|
|
528
542
|
const health = new Health({ vms, conversations: all, resources: folders, seen: new Store(`${dataDir}/health-seen.json`), ...(healthSettleMs === undefined ? {} : { settleMs: healthSettleMs }) });
|
|
529
543
|
const stopHealth = health.start(healthEveryMs);
|
|
@@ -625,7 +639,7 @@ export async function startHome({ port, host, dataDir, mntDir = dataDir, gitRese
|
|
|
625
639
|
speak: (text) => speech.speak(text),
|
|
626
640
|
log: (line) => events.add({ kind: 'server', text: line }) });
|
|
627
641
|
const { server, listening } = serve(port, all, folders, { gate, originOk, shortcuts, voice, sharing, door, secrets, network, signin, mailboxes, gitReview, platform, answerer, wakeups, alerts, talk, gitAccess, gitKeeper, shelf, health, setup, guide, capacity, updates, instance, certificate, team, reacting, machinesMove, serverLogs, history, events, wifi, binders, meshcentral, tethers, accounts, groups, boxes, bundles: new Bundles({ dir: `${dataDir}/imports`, folder: (name) => folders.folder(name) }), repair: (name) => repairTerminal(name) });
|
|
628
|
-
server.on('close', () => { closing = true; stopBinders(); stopTethers(); clearInterval(historyTimer); clearInterval(secretsTimer); history.save(); stopVmWatch(); stopShelf(); stopHealth(); stopUpdates(); stopWatch(); clearInterval(mailTimer); clearInterval(expiryTimer); clearInterval(answerTimer); clearInterval(wakeTimer); clearInterval(docketTimer); dockets.stop(); clearInterval(gitTimer); talk.stop(); });
|
|
642
|
+
server.on('close', () => { closing = true; stopBinders(); stopTethers(); clearInterval(historyTimer); clearInterval(secretsTimer); history.save(); stopVmWatch(); stopShelf(); stopHealth(); stopUpdates(); stopWatch(); clearInterval(mailTimer); clearInterval(restartTimer); clearInterval(expiryTimer); clearInterval(answerTimer); clearInterval(wakeTimer); clearInterval(docketTimer); dockets.stop(); clearInterval(gitTimer); talk.stop(); });
|
|
629
643
|
let closing = false;
|
|
630
644
|
boxes.attach(server, WebSocketServer);
|
|
631
645
|
for (const c of all.list()) talk.speakers(c.name, boxes.speakersOf(c.name));
|
package/server/talk.mjs
CHANGED
|
@@ -140,6 +140,8 @@ export const SYSTEM_MAX = 50;
|
|
|
140
140
|
branch: the owner allowed each one. The owner may move a kind to another
|
|
141
141
|
level (Settings); nothing else of a line changes with it. */
|
|
142
142
|
export const LEVELS = Object.freeze(['needs-you', 'news', 'trace']);
|
|
143
|
+
/* The file a tool puts beside its chat to be restarted (see restartAsked). */
|
|
144
|
+
export const RESTART_ASKED = 'restart-asked';
|
|
143
145
|
export const EVENT_LEVELS = Object.freeze({
|
|
144
146
|
sent: 'news',
|
|
145
147
|
refused: 'needs-you',
|
|
@@ -384,6 +386,14 @@ export class Talk {
|
|
|
384
386
|
/* Is its chat on? (cheap: the record alone) */
|
|
385
387
|
isOn(name) { return this.#store.get(name)?.on === true; }
|
|
386
388
|
|
|
389
|
+
/* ITS TOOL ASKS TO BE RESTARTED (02-11): a file it puts in its chat's folder (RESTART_ASKED); the server
|
|
390
|
+
restarts it once its turn has ended -- the one command a tool may ask of the server, nothing typed. */
|
|
391
|
+
restartAsked(name) {
|
|
392
|
+
if (this.#store.get(name)?.on !== true) return false;
|
|
393
|
+
try { return lstatSync(join(this.#dir(name), RESTART_ASKED), { throwIfNoEntry: false })?.isFile() === true; } catch { return false; }
|
|
394
|
+
}
|
|
395
|
+
restartTaken(name) { try { rmSync(join(this.#dir(name), RESTART_ASKED), { force: true }); } catch { /* gone already */ } }
|
|
396
|
+
|
|
387
397
|
/* Its tool's pulse (Parley): { busy, at }, or null. Where the chat is off: null. */
|
|
388
398
|
pulse(name) {
|
|
389
399
|
if (this.#store.get(name)?.on !== true) return null;
|