clavarium 0.2.245-next.1 → 0.2.246-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.
- package/config/launch/claude-code.sh +40 -19
- package/config/launch/codex.sh +37 -0
- package/config/launch/json-set.mjs +5 -2
- package/config/launch/mcp-relay.mjs +98 -0
- package/config/launch/mcp-sock.sh +31 -0
- package/config/launch/screen-watch.mjs +148 -0
- package/config/launch/toml-block.mjs +26 -0
- package/config/launch/tools-host.mjs +93 -0
- package/config/launch/vibe.sh +40 -0
- package/config/machine/clavarium-beat +44 -0
- package/config/machine/clavarium-beat-one +40 -0
- package/config/machine/clavarium-enter +60 -0
- package/config/tools.json +173 -13
- package/deploy/clavarium-host +385 -31
- package/deploy/clavarium-tunnel +14 -3
- package/deploy/clavarium.mjs +4 -0
- package/deploy/git-read +57 -5
- package/deploy/install.sh +20 -9
- package/deploy/maintenance.html +5 -2
- package/deploy/multipass-refresh.mjs +104 -0
- package/deploy/online.mjs +251 -8
- package/deploy/relay.sh +53 -27
- package/deploy/tunnel.mjs +7 -5
- package/deploy/updater.mjs +176 -25
- package/npm-shrinkwrap.json +18 -18
- package/package.json +6 -5
- package/server/agenda.mjs +140 -0
- package/server/api.mjs +391 -28
- package/server/apikeys.mjs +1372 -0
- package/server/beat.mjs +189 -0
- package/server/binders.mjs +42 -0
- package/server/boxcollect.mjs +276 -0
- package/server/boxes.mjs +350 -29
- package/server/bridge.mjs +13 -4
- package/server/browsers.mjs +364 -0
- package/server/capacity.mjs +37 -10
- package/server/carry.mjs +143 -0
- package/server/catalog.mjs +48 -0
- package/server/contactview.mjs +122 -0
- package/server/conversations.mjs +1272 -49
- package/server/copywork.mjs +34 -0
- package/server/demandes.mjs +251 -0
- package/server/desktop.mjs +102 -0
- package/server/devices.mjs +1 -0
- package/server/dictation-engines.mjs +91 -0
- package/server/dockets.mjs +58 -7
- package/server/door.mjs +27 -5
- package/server/events.mjs +4 -0
- package/server/features.mjs +366 -0
- package/server/files.mjs +43 -1
- package/server/gitaccess.mjs +12 -3
- package/server/gitchecks.mjs +17 -3
- package/server/gitkeeper.mjs +45 -10
- package/server/gitpush.mjs +286 -71
- package/server/gitrunner.mjs +144 -10
- package/server/grants.mjs +47 -11
- package/server/guide.mjs +31 -10
- package/server/health.mjs +138 -12
- package/server/jobs.mjs +138 -0
- package/server/linkguard.mjs +191 -0
- package/server/machinelist.mjs +58 -0
- package/server/mailbox.mjs +8 -0
- package/server/mailref.mjs +90 -0
- package/server/mailview.mjs +204 -0
- package/server/main.mjs +446 -52
- package/server/mcpoauth.mjs +338 -0
- package/server/mcpx.mjs +186 -0
- package/server/network.mjs +125 -13
- package/server/profile.mjs +129 -0
- package/server/relayjoin.mjs +67 -0
- package/server/resources.mjs +6 -0
- package/server/revive.mjs +73 -0
- package/server/rooms.mjs +496 -0
- package/server/secrets.mjs +25 -12
- package/server/serverlogs.mjs +30 -4
- package/server/settingswords.mjs +382 -0
- package/server/signin.mjs +25 -8
- package/server/signinhelp.mjs +358 -0
- package/server/speech.mjs +40 -4
- package/server/spoken.mjs +60 -0
- package/server/sshconsole.mjs +99 -0
- package/server/sshmachine.mjs +76 -0
- package/server/store.mjs +15 -1
- package/server/suggest.mjs +5 -1
- package/server/talk.mjs +231 -55
- package/server/tethers.mjs +223 -13
- package/server/toolbox.mjs +30 -7
- package/server/tools.mjs +40 -6
- package/server/updates.mjs +7 -2
- package/server/vm.mjs +215 -46
- package/server/vmwatch.mjs +130 -24
- package/server/voice.mjs +165 -51
- package/server/wakelearn.mjs +214 -0
- package/server/wakeups.mjs +42 -2
- package/server/watchdog.mjs +102 -0
- package/web/app.js +5588 -1250
- package/web/desk.js +531 -0
- package/web/device.html +15 -2
- package/web/features.js +480 -0
- package/web/index.html +884 -490
- package/web/kit.js +1142 -0
- package/web/links.js +2 -2
- package/web/mail-view.js +386 -0
- package/web/manifest.webmanifest +4 -0
- package/web/style.css +1563 -273
- package/web/voice-core.js +48 -0
- package/web/voice.js +13 -5
|
@@ -17,33 +17,54 @@ 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
|
-
|
|
20
|
+
# ITS FIRST QUESTIONS ANSWERED BEFORE THEY ARE ASKED (02-11): the welcome (theme, tips) taken as seen, and its
|
|
21
|
+
# work folder trusted -- in its own file, as it records them itself; nothing to answer in the terminal.
|
|
22
|
+
node "$(dirname "$0")/json-set.mjs" .claude.json '{"hasCompletedOnboarding": true, "projects": {"/work": {"hasTrustDialogAccepted": true}}}' \
|
|
23
|
+
|| echo "launch: Claude Code may ask its first questions in the terminal" >&2
|
|
24
|
+
# ITS TOOLS (MCP), EACH THROUGH THE SHARED HOST (11-11, #350; the relay since 02-11): one program for all its
|
|
25
|
+
# tools (mcp-sock.sh -> tools-host.mjs), a tool updated on the machine taken up between two of its requests,
|
|
26
|
+
# the session never restarted. Declared once, by Claude Code itself (its other servers kept); an older
|
|
27
|
+
# declaration (the relay alone) is replaced.
|
|
28
|
+
# Looked for in its settings first, since asking Claude Code takes seconds. Nothing stops the start.
|
|
29
|
+
relay="$(dirname "$0")/mcp-sock.sh"
|
|
30
|
+
tool() { # NAME FOLDER COMMAND ARGS...
|
|
31
|
+
name="$1"; folder="$2"; shift 2
|
|
32
|
+
if grep -q "\"$folder\"" "$HOME/.claude.json" 2>/dev/null && grep -q "mcp-sock.sh" "$HOME/.claude.json" 2>/dev/null \
|
|
33
|
+
&& 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-sock.sh")&&a.includes(JSON.stringify(process.argv[3]))?0:1)' "$HOME/.claude.json" "$name" "$1" 2>/dev/null; then return 0; fi
|
|
34
|
+
claude mcp remove --scope user "$name" >/dev/null 2>&1
|
|
35
|
+
claude mcp add --scope user "$name" -- sh "$relay" "$@" >/dev/null 2>&1 \
|
|
36
|
+
|| echo "launch: $name was not given to Claude Code as tools: the command $name still works" >&2
|
|
37
|
+
}
|
|
38
|
+
# Parley, the chat of the conversation, when it has one: the version installed on the machine (parley-mcp),
|
|
39
|
+
# else the copy beside the chat.
|
|
24
40
|
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
|
|
41
|
+
if [ -f "$chat/server.mjs" ]; then
|
|
42
|
+
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
43
|
fi
|
|
29
|
-
#
|
|
30
|
-
# reminders it sets itself. The same as the command "docket", installed in every machine.
|
|
44
|
+
# Its docket, with its chat: what woke it, to say done or failed; the reminders it sets itself.
|
|
31
45
|
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.
|
|
46
|
+
if [ -f "$chat/server.mjs" ] && command -v docket-mcp >/dev/null 2>&1; then tool docket "$docket" docket-mcp --dir "$docket"; fi
|
|
47
|
+
# Its tether: the accesses it was given (a mailbox, a calendar, a folder, a machine over SSH).
|
|
38
48
|
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
|
|
49
|
+
if command -v tether-mcp >/dev/null 2>&1; then tool tether "$tether" tether-mcp --dir "$tether"; fi
|
|
43
50
|
# ITS PULSE: Claude Code's own hooks say to the chat that it acts, and when its turn ends, so that
|
|
44
51
|
# the person sees "Working..." while it works without a word (parley pulse; nothing posted).
|
|
45
52
|
if [ -f "$chat/server.mjs" ]; then
|
|
46
53
|
node "$(dirname "$0")/claude-pulse.mjs" "$chat" \
|
|
47
54
|
|| echo "launch: the chat will not say when Claude Code works without a word" >&2
|
|
48
55
|
fi
|
|
56
|
+
# WHAT ITS SCREEN ASKS (02-11), read by the generic screen-watch -- Claude Code's own words, here and only
|
|
57
|
+
# here: not signed in (at its start, or signed out on the way: « Login expired », 15-11), its sign-in started by itself, its link sent to the person's chat, the code they paste
|
|
58
|
+
# there typed back (02-11, tried on a real Claude Code); an update installed told to the agent, which restarts
|
|
59
|
+
# when it suits it (never in the middle of its work). A tool that changes its words: only these lines change.
|
|
60
|
+
# Since 15-11 (#362) its sign-in rules are those of config/tools.json ("signin.screen", --tool): the lines
|
|
61
|
+
# below are the fallback of an older server, and its notice. The link may go to another conversation (screen-watch.mjs).
|
|
62
|
+
node "$(dirname "$0")/screen-watch.mjs" \
|
|
63
|
+
--tool claude-code \
|
|
64
|
+
'type:(Not logged in|Login expired|OAuth token (has )?expired|Invalid API key)[^\n]{0,24}/login=/login' \
|
|
65
|
+
'keys:Select login method:=Enter' \
|
|
66
|
+
'link:https://(claude\.ai|claude\.com|console\.anthropic\.com)/\S*oauth\S*' \
|
|
67
|
+
'code:Paste code here' \
|
|
68
|
+
'notice:(update|updated|installed)[^\n]{0,80}restart=Your tool was updated: when nothing runs, touch /work/.parley/restart-asked to take it up.' \
|
|
69
|
+
</dev/null >/dev/null 2>&1 &
|
|
49
70
|
exec "$@"
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
#!/bin/sh
|
|
2
|
+
# THE LAUNCHER OF CODEX (OpenAI), run by Clavarium in its machine at each start (config/tools.json,
|
|
3
|
+
# "launch"), from the read-only /tools. Arguments: the rights chosen by the owner (full, ask), then the
|
|
4
|
+
# command to run, as Clavarium made it. All that is Codex's own is here, and only here (02-11).
|
|
5
|
+
mode="$1"; shift
|
|
6
|
+
case "$mode" in full|ask) ;; *) echo "launch: the first argument is full or ask" >&2; exit 64 ;; esac
|
|
7
|
+
[ $# -gt 0 ] || { echo "launch: no command to run" >&2; exit 64; }
|
|
8
|
+
here="$(dirname "$0")"; relay="$here/mcp-sock.sh"
|
|
9
|
+
chat="${PARLEY_DIR:-/work/.parley}"; docket="/work/.docket"; tether="/work/.tether"
|
|
10
|
+
# ITS SETTINGS (~/.codex/config.toml), in a block of their own, the rest kept: its work folder trusted (no
|
|
11
|
+
# question at its first start), and its tools (MCP) each through the relay -- a tool updated on the
|
|
12
|
+
# machine taken up between two requests, never a restart.
|
|
13
|
+
block='[projects."/work"]
|
|
14
|
+
trust_level = "trusted"'
|
|
15
|
+
if [ -f "$chat/server.mjs" ] && command -v parley-mcp >/dev/null 2>&1; then block="$block
|
|
16
|
+
[mcp_servers.parley]
|
|
17
|
+
command = \"sh\"
|
|
18
|
+
args = [\"$relay\", \"parley-mcp\", \"--dir\", \"$chat\"]"; fi
|
|
19
|
+
if [ -f "$chat/server.mjs" ] && command -v docket-mcp >/dev/null 2>&1; then block="$block
|
|
20
|
+
[mcp_servers.docket]
|
|
21
|
+
command = \"sh\"
|
|
22
|
+
args = [\"$relay\", \"docket-mcp\", \"--dir\", \"$docket\"]"; fi
|
|
23
|
+
if command -v tether-mcp >/dev/null 2>&1; then block="$block
|
|
24
|
+
[mcp_servers.tether]
|
|
25
|
+
command = \"sh\"
|
|
26
|
+
args = [\"$relay\", \"tether-mcp\", \"--dir\", \"$tether\"]"; fi
|
|
27
|
+
node "$here/toml-block.mjs" .codex/config.toml tools "$block" \
|
|
28
|
+
|| echo "launch: Codex may ask its first questions, and has not its tools (MCP)" >&2
|
|
29
|
+
# WHAT ITS SCREEN ASKS, read by the generic screen-watch -- Codex's own words: its sign-in link (and the
|
|
30
|
+
# code beside it) sent to the person's chat; an update told to the agent, which restarts when it suits it.
|
|
31
|
+
node "$here/screen-watch.mjs" \
|
|
32
|
+
--tool codex \
|
|
33
|
+
'link:https://auth\.openai\.com/\S+' \
|
|
34
|
+
'link:https://chatgpt\.com/\S*auth\S*' \
|
|
35
|
+
'notice:update available[^\n]{0,120}=Your tool has an update: when nothing runs, touch /work/.parley/restart-asked to take it up.' \
|
|
36
|
+
</dev/null >/dev/null 2>&1 &
|
|
37
|
+
exec "$@"
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
/* json-set FILE KEYS: sets KEYS (a JSON object; null takes a key out) in the
|
|
2
|
-
* JSON file FILE of the home, every other key kept
|
|
2
|
+
* JSON file FILE of the home, every other key kept -- inside objects too (02-11:
|
|
3
|
+
* {"projects": {"/work": {...}}} keeps the other projects and their other keys). Written only when it
|
|
3
4
|
* changes, readable by its owner alone. A file that is not a JSON object is
|
|
4
5
|
* not touched (exit 3): a person's settings are never overwritten by a guess.
|
|
5
6
|
*
|
|
@@ -21,7 +22,9 @@ try { data = JSON.parse(readFileSync(at, 'utf8')); } catch (error) {
|
|
|
21
22
|
}
|
|
22
23
|
if (!data || typeof data !== 'object' || Array.isArray(data)) { console.error(`json-set: ${file} is not a JSON object: left as it is`); process.exit(3); }
|
|
23
24
|
const was = JSON.stringify(data);
|
|
24
|
-
|
|
25
|
+
const plain = (v) => v && typeof v === 'object' && !Array.isArray(v);
|
|
26
|
+
const merge = (into, from) => { for (const [k, v] of Object.entries(from)) { if (v === null) delete into[k]; else if (plain(v) && plain(into[k])) merge(into[k], v); else into[k] = v; } };
|
|
27
|
+
merge(data, keys);
|
|
25
28
|
if (JSON.stringify(data) !== was) {
|
|
26
29
|
mkdirSync(dirname(at), { recursive: true, mode: 0o700 });
|
|
27
30
|
writeFileSync(`${at}.new`, `${JSON.stringify(data, null, 2)}\n`, { mode: 0o600 });
|
|
@@ -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
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
#!/bin/sh
|
|
2
|
+
# A TOOL OF THE CONVERSATION, THROUGH THE SHARED HOST (11-11, card #350): what the agent starts for each of its
|
|
3
|
+
# tools (MCP) -- the same arguments as mcp-relay.mjs, COMMAND ARGS... -- but instead of a Node program of its
|
|
4
|
+
# own, a few lines of sh and netcat: the session goes to tools-host.mjs, one program for all the tools of this
|
|
5
|
+
# user. Anything missing (netcat, the host not starting): the relay of before, as if nothing changed.
|
|
6
|
+
here="$(dirname "$0")"
|
|
7
|
+
fallback() { exec node "$here/mcp-relay.mjs" "$@"; }
|
|
8
|
+
command -v nc >/dev/null 2>&1 || fallback "$@"
|
|
9
|
+
dir="${XDG_RUNTIME_DIR:-/tmp}/clavarium-tools-$(id -u)"
|
|
10
|
+
# Its folder: this user's alone (700), or the relay of before.
|
|
11
|
+
mkdir -p "$dir" 2>/dev/null && [ -O "$dir" ] && chmod 700 "$dir" 2>/dev/null || fallback "$@"
|
|
12
|
+
sock="$dir/host.sock"
|
|
13
|
+
up() { [ -S "$sock" ] && nc -zU "$sock" 2>/dev/null; }
|
|
14
|
+
if ! up; then
|
|
15
|
+
(
|
|
16
|
+
flock 9 # the host itself never holds the lock (9>&- below): two tools starting at once both get in
|
|
17
|
+
if ! up; then
|
|
18
|
+
# its own session (setsid): an agent ending its tools never ends the host the others use
|
|
19
|
+
ns=""; command -v setsid >/dev/null 2>&1 && ns=setsid
|
|
20
|
+
$ns nohup node "$here/tools-host.mjs" "$dir" >>"$dir/host.log" 2>&1 </dev/null 9>&- &
|
|
21
|
+
i=0; while [ $i -lt 30 ] && ! up; do sleep 0.1; i=$((i + 1)); done
|
|
22
|
+
fi
|
|
23
|
+
) 9>"$dir/lock"
|
|
24
|
+
fi
|
|
25
|
+
up || fallback "$@"
|
|
26
|
+
# Its first line: which tool, with which arguments (JSON); then the session, both ways.
|
|
27
|
+
json() { printf '%s' "$1" | sed -e 's/\\/\\\\/g' -e 's/"/\\"/g'; }
|
|
28
|
+
head="{\"command\":\"$(json "$1")\",\"args\":["; shift
|
|
29
|
+
sep=""; for a in "$@"; do head="$head$sep\"$(json "$a")\""; sep=","; done
|
|
30
|
+
head="$head]}"
|
|
31
|
+
{ printf '%s\n' "$head"; exec cat; } | exec nc -NU "$sock"
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
/* screen-watch RULE... : a generic reader of the tool's terminal (02-11). Every few seconds it reads the
|
|
2
|
+
* bottom of the screen of the tool (tmux) and acts on what a launcher said to look for -- nothing here
|
|
3
|
+
* knows a tool: its launcher (claude-code.sh, a codex.sh...) gives the rules, so a tool that changes its
|
|
4
|
+
* words is followed by changing its launcher alone.
|
|
5
|
+
*
|
|
6
|
+
* A rule: KIND:PATTERN[=VALUE], PATTERN a JavaScript regular expression (flag i).
|
|
7
|
+
* link:PATTERN a link on the screen (the match): sent in the chat, to be opened by the person
|
|
8
|
+
* (a sign-in, say); once per link.
|
|
9
|
+
* notice:PATTERN=TEXT something the agent should know (an update installed, to be taken up by a
|
|
10
|
+
* restart): TEXT given to the agent, as a reminder it is woken by (docket); once per screen.
|
|
11
|
+
* The agent decides when it suits it -- never restarted in the middle of its work (02-11).
|
|
12
|
+
* keys:PATTERN=K a question with one answer always: the keys K typed (Enter, Down, Up, Escape, 1, y...),
|
|
13
|
+
* once per screen. Only what its launcher wrote: nothing else is ever typed.
|
|
14
|
+
* type:PATTERN=TEXT the same with a few words (a command such as /login), then Enter; once per screen.
|
|
15
|
+
* code:PATTERN the tool waits for a code the person gets after signing in. Nothing here acts on it: the
|
|
16
|
+
* server reads the same rule, offers a field in the chat and types what is pasted there (#405).
|
|
17
|
+
* A chat message is never typed as a code.
|
|
18
|
+
* Only the last LINES lines are read: what the tool prints of its own work above never counts.
|
|
19
|
+
*
|
|
20
|
+
* THE RULES FROM THE TOOLS' CONFIGURATION (15-11, #362): `--tool ID` reads the rules of that tool from
|
|
21
|
+
* screen-rules.json beside this file (written by the server from config/tools.json, "signin.screen"); its
|
|
22
|
+
* link/keys/type/code rules then replace those its launcher gave, which stay as the fallback of a launcher
|
|
23
|
+
* or a server older than this. Its notice rules are its launcher's always.
|
|
24
|
+
*
|
|
25
|
+
* A SIGN-IN LINK HANDED TO ANOTHER CONVERSATION (15-11, #362). The link is not told in the chat at once: it is
|
|
26
|
+
* written in the chat folder (signin-ask.json) and the server says where it goes (signin-route.json):
|
|
27
|
+
* companion another conversation, with a browser already signed in, opens it; the person only clicks
|
|
28
|
+
* Authorize there. The code it reads is typed here by the server; nothing is told here.
|
|
29
|
+
* person the link told in the chat, as before (no companion, or it failed, or the person asked).
|
|
30
|
+
* done the server typed the code.
|
|
31
|
+
* No answer within HAND_MS (an older server): told to the person. The link is never written anywhere else.
|
|
32
|
+
*/
|
|
33
|
+
import { spawnSync } from 'node:child_process';
|
|
34
|
+
import { randomUUID } from 'node:crypto';
|
|
35
|
+
import { existsSync, readFileSync, renameSync, writeFileSync } from 'node:fs';
|
|
36
|
+
import { dirname, join } from 'node:path';
|
|
37
|
+
import { fileURLToPath } from 'node:url';
|
|
38
|
+
|
|
39
|
+
const LINES = 12; const EVERY_MS = 3000;
|
|
40
|
+
const KEYS = new Set(['Enter', 'Down', 'Up', 'Left', 'Right', 'Escape', 'Tab', 'y', 'n', '1', '2', '3']);
|
|
41
|
+
export function rulesOf(args) {
|
|
42
|
+
return args.flatMap((a) => {
|
|
43
|
+
const m = /^(link|notice|keys|type|code):(.+)$/s.exec(a); if (!m) return [];
|
|
44
|
+
let pattern = m[2]; let value = null;
|
|
45
|
+
if (m[1] === 'type') { const at = pattern.lastIndexOf('='); if (at < 1) return []; value = pattern.slice(at + 1).slice(0, 80); pattern = pattern.slice(0, at); if (!value.trim()) return []; }
|
|
46
|
+
if (m[1] === 'notice') { const at = pattern.lastIndexOf('='); if (at < 1) return []; value = pattern.slice(at + 1).trim().slice(0, 300); pattern = pattern.slice(0, at); if (!value) return []; }
|
|
47
|
+
if (m[1] === 'keys') { const at = pattern.lastIndexOf('='); if (at < 1) return []; value = pattern.slice(at + 1).split(',').filter((k) => KEYS.has(k)); pattern = pattern.slice(0, at); if (!value.length) return []; }
|
|
48
|
+
try { return [{ kind: m[1], re: new RegExp(pattern, 'i'), value }]; } catch { return []; }
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
/* What the person pastes in the sign-in field, or the companion gives back, that may be typed as a code: no space, 10 to 300 signs. */
|
|
52
|
+
export const CODE = /^[A-Za-z0-9_#.~+/=-]{10,300}$/;
|
|
53
|
+
export const RULES_FILE = 'screen-rules.json';
|
|
54
|
+
const SIGNIN_KINDS = new Set(['link', 'keys', 'type', 'code']);
|
|
55
|
+
/* The rules of this start: those of the tool's configuration when there are any (`file`: screen-rules.json
|
|
56
|
+
read, { toolId: [rule, ...] }), its launcher's notices kept; else all its launcher gave (15-11). */
|
|
57
|
+
export function rulesFor(args, file = null) {
|
|
58
|
+
const at = args.indexOf('--tool'); const tool = at >= 0 ? args[at + 1] : null;
|
|
59
|
+
const given = rulesOf(at >= 0 ? args.filter((_, i) => i !== at && i !== at + 1) : args);
|
|
60
|
+
const own = tool && file && typeof file === 'object' && Array.isArray(file[tool]) ? rulesOf(file[tool].filter((r) => typeof r === 'string')).filter((r) => SIGNIN_KINDS.has(r.kind)) : [];
|
|
61
|
+
return own.length ? [...own, ...given.filter((r) => r.kind === 'notice')] : given;
|
|
62
|
+
}
|
|
63
|
+
/* -> what to do on this screen: [{ kind, match, value }], each once (seen: a Set kept between looks). */
|
|
64
|
+
export function actionsOn(screen, rules, seen) {
|
|
65
|
+
/* A long link the tool's screen broke over several lines (full lines, no space): put back in one. */
|
|
66
|
+
const raw = String(screen).replace(/\s+$/u, '').split('\n').map((l) => l.replace(/\s+$/u, ''));
|
|
67
|
+
const width = Math.max(0, ...raw.map((l) => l.length));
|
|
68
|
+
const joined = [];
|
|
69
|
+
for (const l of raw) { const prev = joined.at(-1); if (prev !== undefined && width > 40 && prev.length >= width - 1 && /^\S+$/.test(prev.split(' ').at(-1)) && /^\S+$/.test(l) && /https?:\/\//.test(prev)) joined[joined.length - 1] = prev + l; else joined.push(l); }
|
|
70
|
+
const bottom = joined.slice(-LINES).join('\n');
|
|
71
|
+
const out = [];
|
|
72
|
+
for (const r of rules) {
|
|
73
|
+
const m = r.re.exec(bottom); if (!m) continue;
|
|
74
|
+
const key = `${r.kind}:${r.kind === 'link' ? m[0] : bottom}`;
|
|
75
|
+
if (seen.has(key)) continue;
|
|
76
|
+
/* A notice once an hour at most: its own words on the screen never bring it back in a loop. */
|
|
77
|
+
if (r.kind === 'notice' && Date.now() - (r.last ?? 0) < 60 * 60_000) { seen.add(key); continue; }
|
|
78
|
+
if (r.kind === 'notice') r.last = Date.now();
|
|
79
|
+
/* With a link, the lines around it (a code to type there, often): the person sees what the tool said. */
|
|
80
|
+
const lines = bottom.split('\n'); const i = lines.findIndex((l) => l.includes(m[0].split('\n')[0]));
|
|
81
|
+
const around = r.kind === 'link' && i >= 0 ? lines.slice(Math.max(0, i - 2), i + 4).map((l) => l.trim()).filter(Boolean).join('\n') : '';
|
|
82
|
+
seen.add(key); out.push({ kind: r.kind, match: m[0], value: r.value, around });
|
|
83
|
+
}
|
|
84
|
+
if (seen.size > 200) for (const k of [...seen].slice(0, 100)) seen.delete(k);
|
|
85
|
+
return out;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/* Where a sign-in link goes, step by step (15-11, #362): what to do, as a list of effects the caller runs --
|
|
89
|
+
{ do: 'ask', ask } (write signin-ask.json), 'tell-link' { a } (the link in the person's chat). `field: true` in
|
|
90
|
+
the ask: this watcher reads no code from the chat (#405; an older one does, see SigninHelp.oldWatcherArmed).
|
|
91
|
+
`route()`: signin-route.json as read, or null. */
|
|
92
|
+
export const HAND_MS = 20_000;
|
|
93
|
+
export class Relay {
|
|
94
|
+
#ask = null; #now; #id; #route; #chat;
|
|
95
|
+
constructor({ route = () => null, chat = () => true, now = () => Date.now(), id = () => randomUUID() } = {}) { this.#route = route; this.#chat = chat; this.#now = now; this.#id = id; }
|
|
96
|
+
/* Told to the person: the ask says so too (`told`), so that a server that reads it late hands it to nobody. */
|
|
97
|
+
#person() { const a = this.#ask; a.route = 'person'; return [{ do: 'ask', ask: { ...a.file, told: true } }, { do: 'tell-link', a: a.a }]; }
|
|
98
|
+
link(a) {
|
|
99
|
+
/* No chat folder: nowhere to hand it over, nobody but the person (and the chat is off anyway). */
|
|
100
|
+
if (!this.#chat()) return [{ do: 'tell-link', a }];
|
|
101
|
+
const id = this.#id(); const at = this.#now();
|
|
102
|
+
this.#ask = { id, at, a, route: null, file: { id, at: new Date(at).toISOString(), link: a.match, around: a.around ?? '', field: true } };
|
|
103
|
+
return [{ do: 'ask', ask: this.#ask.file }];
|
|
104
|
+
}
|
|
105
|
+
step() {
|
|
106
|
+
const a = this.#ask; if (!a || a.route === 'person') return [];
|
|
107
|
+
const r = this.#route(); const mine = r && r.id === a.id ? r.route : null;
|
|
108
|
+
if (mine === 'done') { this.#ask = null; return []; }
|
|
109
|
+
if (mine === 'person') return this.#person();
|
|
110
|
+
if (mine === 'companion') { a.route = 'companion'; return []; }
|
|
111
|
+
if (a.route === null && this.#now() - a.at > HAND_MS) return this.#person();
|
|
112
|
+
return [];
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
const main = process.argv[1]?.endsWith('screen-watch.mjs');
|
|
117
|
+
if (main) {
|
|
118
|
+
let file = null; try { file = JSON.parse(readFileSync(join(dirname(fileURLToPath(import.meta.url)), RULES_FILE), 'utf8')); } catch { /* none: its launcher's rules */ }
|
|
119
|
+
const rules = rulesFor(process.argv.slice(2), file);
|
|
120
|
+
const tmux = ['-L', process.env.SCREEN_WATCH_SOCKET ?? 'clavarium'];
|
|
121
|
+
const pane = process.env.SCREEN_WATCH_PANE ?? '=main:';
|
|
122
|
+
const chat = process.env.PARLEY_DIR ?? '/work/.parley';
|
|
123
|
+
const seen = new Set();
|
|
124
|
+
/* The sign-in link's way (Relay): asked in the chat folder, routed by the server. */
|
|
125
|
+
const readRoute = () => { try { return JSON.parse(readFileSync(`${chat}/signin-route.json`, 'utf8')); } catch { return null; } };
|
|
126
|
+
const relay = new Relay({ route: readRoute, chat: () => existsSync(chat) });
|
|
127
|
+
const run = (effects) => {
|
|
128
|
+
for (const e of effects) {
|
|
129
|
+
if (e.do === 'ask') { try { writeFileSync(`${chat}/.signin-ask.json.new`, JSON.stringify(e.ask)); renameSync(`${chat}/.signin-ask.json.new`, `${chat}/signin-ask.json`); } catch { /* not written: no answer comes, told to the person after HAND_MS */ } }
|
|
130
|
+
if (e.do === 'tell-link') spawnSync('parley', ['tell', `Your tool asks you to open this link: ${e.a.match}${e.a.around && e.a.around !== e.a.match ? `\n\n\`\`\`\n${e.a.around}\n\`\`\`` : ''}`, '--dir', chat], { timeout: 10_000 });
|
|
131
|
+
}
|
|
132
|
+
};
|
|
133
|
+
const look = () => {
|
|
134
|
+
run(relay.step());
|
|
135
|
+
const r = spawnSync('tmux', [...tmux, 'capture-pane', '-p', '-t', pane], { encoding: 'utf8', timeout: 5000 });
|
|
136
|
+
if (r.status !== 0) return;
|
|
137
|
+
for (const a of actionsOn(r.stdout, rules, seen)) {
|
|
138
|
+
if (a.kind === 'link') run(relay.link(a));
|
|
139
|
+
if (a.kind === 'notice') spawnSync('docket', ['remind', '--in', '1m', a.value, '--dir', process.env.DOCKET_DIR ?? '/work/.docket'], { timeout: 10_000 });
|
|
140
|
+
if (a.kind === 'keys') for (const k of a.value) spawnSync('tmux', [...tmux, 'send-keys', '-t', pane, k], { timeout: 5000 });
|
|
141
|
+
if (a.kind === 'type') { spawnSync('tmux', [...tmux, 'send-keys', '-t', pane, '-l', '--', a.value], { timeout: 5000 }); spawnSync('tmux', [...tmux, 'send-keys', '-t', pane, 'Enter'], { timeout: 5000 }); }
|
|
142
|
+
}
|
|
143
|
+
};
|
|
144
|
+
/* Its life is the tool's: started just before the launcher becomes the tool (exec), it ends with it. */
|
|
145
|
+
const tool = process.ppid;
|
|
146
|
+
const alive = () => { try { process.kill(tool, 0); return true; } catch { return false; } };
|
|
147
|
+
if (rules.length) setInterval(() => { if (!alive()) process.exit(0); look(); }, EVERY_MS);
|
|
148
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/* toml-block FILE NAME [TEXT]: keeps, in the TOML file FILE of the home, one block of its own --
|
|
2
|
+
* "# >>> clavarium NAME" ... "# <<< clavarium NAME" -- written anew with TEXT (its tables), every other
|
|
3
|
+
* line kept as the person or the tool wrote it. No TEXT: the block taken out. Written only when it
|
|
4
|
+
* changes, readable by its owner alone (02-11: Codex, Vibe -- their settings are TOML).
|
|
5
|
+
* Only tables go in a block (a key outside a table would land in the table above it).
|
|
6
|
+
*/
|
|
7
|
+
import { readFileSync, writeFileSync, mkdirSync, renameSync } from 'node:fs';
|
|
8
|
+
import { dirname, join, isAbsolute } from 'node:path';
|
|
9
|
+
import { homedir } from 'node:os';
|
|
10
|
+
|
|
11
|
+
const [file, name, text = ''] = process.argv.slice(2);
|
|
12
|
+
if (!file || !name || isAbsolute(file) || file.split('/').includes('..') || !/^[a-z0-9-]+$/.test(name)) { console.error('usage: toml-block <file in the home> <name> [text]'); process.exit(64); }
|
|
13
|
+
const at = join(homedir(), file);
|
|
14
|
+
let was = '';
|
|
15
|
+
try { was = readFileSync(at, 'utf8'); } catch (error) { if (error.code !== 'ENOENT') { console.error(`toml-block: ${file} cannot be read`); process.exit(3); } }
|
|
16
|
+
const open = `# >>> clavarium ${name}`; const close = `# <<< clavarium ${name}`;
|
|
17
|
+
const lines = was.split('\n'); const kept = []; let inside = false;
|
|
18
|
+
for (const l of lines) { if (l === open) { inside = true; continue; } if (l === close) { inside = false; continue; } if (!inside) kept.push(l); }
|
|
19
|
+
let out = kept.join('\n').replace(/\n+$/, '');
|
|
20
|
+
if (text.trim()) out = `${out ? `${out}\n\n` : ''}${open}\n${text.trim()}\n${close}`;
|
|
21
|
+
out = out ? `${out}\n` : '';
|
|
22
|
+
if (out !== was) {
|
|
23
|
+
mkdirSync(dirname(at), { recursive: true, mode: 0o700 });
|
|
24
|
+
writeFileSync(`${at}.new`, out, { mode: 0o600 });
|
|
25
|
+
renameSync(`${at}.new`, at);
|
|
26
|
+
}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
/* ONE PROGRAM FOR ALL THE TOOLS OF A CONVERSATION (11-11, card #350). Each tool (Parley, Docket, Tether...) was
|
|
2
|
+
* given to the agent as its own Node program, under its own relay: two Node programs a tool, ~435 MB for three.
|
|
3
|
+
* Here they share one: each session of a tool runs in a worker thread of this host, the same file started the
|
|
4
|
+
* same way (its own argv, stdin and stdout), so a tool runs unchanged. ~100 MB for three.
|
|
5
|
+
*
|
|
6
|
+
* The agent still starts one command a tool (mcp-sock.sh, a few lines of sh and netcat): it connects to this
|
|
7
|
+
* host's socket, says on its first line which tool and with which arguments, then the session flows through.
|
|
8
|
+
* The relay is the same as before (mcp-relay.mjs): a version installed under a session is taken up between two
|
|
9
|
+
* requests -- its worker replaced, the session told its tools may have changed.
|
|
10
|
+
*
|
|
11
|
+
* The socket lives in a folder of this user alone (700): in a machine shared by several conversations, one
|
|
12
|
+
* conversation never reaches another's tools. Nobody connected for 10 minutes: the host stops; the next tool
|
|
13
|
+
* started starts it again. */
|
|
14
|
+
import { createServer } from 'node:net';
|
|
15
|
+
import { Worker } from 'node:worker_threads';
|
|
16
|
+
import { spawn } from 'node:child_process';
|
|
17
|
+
import { PassThrough } from 'node:stream';
|
|
18
|
+
import { createInterface } from 'node:readline';
|
|
19
|
+
import { existsSync, realpathSync, rmSync, statSync, readFileSync } from 'node:fs';
|
|
20
|
+
import { delimiter, join, isAbsolute } from 'node:path';
|
|
21
|
+
import { fileURLToPath } from 'node:url';
|
|
22
|
+
import { relay, packageOf } from './mcp-relay.mjs';
|
|
23
|
+
|
|
24
|
+
const say = (text) => process.stderr.write(`${new Date().toISOString()} tools-host: ${text}\n`);
|
|
25
|
+
|
|
26
|
+
/* The script a command runs, when it is a Node script (node FILE, or a command whose file is one): its path. */
|
|
27
|
+
export function scriptOf(command, args, path = process.env.PATH ?? '') {
|
|
28
|
+
if (/(^|\/)node$/.test(command)) return args[0] ? { file: args[0], args: args.slice(1) } : null;
|
|
29
|
+
const found = isAbsolute(command) ? command : path.split(delimiter).map((d) => join(d, command)).find((f) => { try { return statSync(f).isFile(); } catch { return false; } });
|
|
30
|
+
if (!found) return null;
|
|
31
|
+
let file; try { file = realpathSync(found); } catch { return null; }
|
|
32
|
+
if (/\.(mjs|cjs|js)$/.test(file)) return { file, args };
|
|
33
|
+
try { if (/^#!.*\bnode\b/.test(readFileSync(file, 'utf8').slice(0, 120))) return { file, args }; } catch { /* unreadable */ }
|
|
34
|
+
return null;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/* A worker that looks like a child process to the relay: stdin, stdout, 'exit', kill. Not a Node script: a real
|
|
38
|
+
child, as before. */
|
|
39
|
+
export function workerSpawner(command, args, opts = {}) {
|
|
40
|
+
const script = scriptOf(command, args);
|
|
41
|
+
if (!script) return spawn(command, args, opts);
|
|
42
|
+
const w = new Worker(script.file, { argv: script.args, stdin: true, stdout: true, env: { ...(opts.env ?? process.env) } });
|
|
43
|
+
w.on('error', (error) => say(`${script.file}: ${error?.stack ?? error}`));
|
|
44
|
+
const child = {
|
|
45
|
+
stdin: w.stdin, stdout: w.stdout,
|
|
46
|
+
on(event, f) { if (event === 'exit') w.on('exit', f); return child; },
|
|
47
|
+
kill() { void w.terminate(); },
|
|
48
|
+
};
|
|
49
|
+
return child;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/* One session: its first line names the tool { command, args }; the rest is the session, relayed to a worker. */
|
|
53
|
+
export function session(socket, { spawner = workerSpawner } = {}) {
|
|
54
|
+
const input = new PassThrough();
|
|
55
|
+
let header = null;
|
|
56
|
+
const lines = createInterface({ input: socket });
|
|
57
|
+
lines.on('line', (line) => {
|
|
58
|
+
if (header) { input.write(`${line}\n`); return; }
|
|
59
|
+
try { header = JSON.parse(line); } catch { header = null; }
|
|
60
|
+
if (!header || typeof header.command !== 'string' || !Array.isArray(header.args)) { socket.end(); return; }
|
|
61
|
+
const script = scriptOf(header.command, header.args);
|
|
62
|
+
const target = script?.file ?? header.command;
|
|
63
|
+
relay({ command: header.command, args: header.args.map(String), packageJson: packageOf(target) ?? '/nonexistent/package.json',
|
|
64
|
+
input, output: socket, spawner, log: say });
|
|
65
|
+
});
|
|
66
|
+
lines.on('close', () => input.end());
|
|
67
|
+
socket.on('error', () => { /* the agent went: its worker ends with its input */ });
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export function host(dir, { idleMs = 10 * 60_000 } = {}) {
|
|
71
|
+
const sock = join(dir, 'host.sock');
|
|
72
|
+
rmSync(sock, { force: true });
|
|
73
|
+
let open = 0; let idle = null;
|
|
74
|
+
const server = createServer((socket) => {
|
|
75
|
+
open++; clearTimeout(idle);
|
|
76
|
+
socket.on('close', () => { open--; if (open === 0) idle = setTimeout(() => { server.close(); rmSync(sock, { force: true }); process.exit(0); }, idleMs); });
|
|
77
|
+
session(socket);
|
|
78
|
+
});
|
|
79
|
+
server.listen(sock, () => say(`listening (${process.pid})`));
|
|
80
|
+
idle = setTimeout(() => { if (open === 0) { server.close(); rmSync(sock, { force: true }); process.exit(0); } }, idleMs);
|
|
81
|
+
return server;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
const HERE = fileURLToPath(import.meta.url);
|
|
85
|
+
const main = (() => { try { return realpathSync(process.argv[1] ?? '') === realpathSync(HERE); } catch { return false; } })();
|
|
86
|
+
if (main) {
|
|
87
|
+
const dir = process.argv[2];
|
|
88
|
+
if (!dir || !existsSync(dir)) { say('a folder of its own, given first'); process.exit(64); }
|
|
89
|
+
/* Its folder: this user's, closed to others -- or nothing is served. */
|
|
90
|
+
const st = statSync(dir);
|
|
91
|
+
if (st.uid !== process.getuid() || (st.mode & 0o077) !== 0) { say(`${dir} is not closed to others: nothing served`); process.exit(77); }
|
|
92
|
+
host(dir);
|
|
93
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
#!/bin/sh
|
|
2
|
+
# THE LAUNCHER OF VIBE (Mistral), run by Clavarium in its machine at each start (config/tools.json,
|
|
3
|
+
# "launch"), from the read-only /tools. Arguments: the rights chosen by the owner (full, ask), then the
|
|
4
|
+
# command to run, as Clavarium made it. All that is Vibe's own is here, and only here (02-11).
|
|
5
|
+
mode="$1"; shift
|
|
6
|
+
case "$mode" in full|ask) ;; *) echo "launch: the first argument is full or ask" >&2; exit 64 ;; esac
|
|
7
|
+
[ $# -gt 0 ] || { echo "launch: no command to run" >&2; exit 64; }
|
|
8
|
+
here="$(dirname "$0")"; relay="$here/mcp-sock.sh"
|
|
9
|
+
chat="${PARLEY_DIR:-/work/.parley}"; docket="/work/.docket"; tether="/work/.tether"
|
|
10
|
+
# ITS KEY: a secret named MISTRAL_API_KEY given to the conversation is in its environment; Vibe reads it
|
|
11
|
+
# there, and asks nothing. Without it, Vibe asks for it in its terminal at its first start.
|
|
12
|
+
# ITS TOOLS (MCP) in its settings (~/.vibe/config.toml), a block of their own, each through the relay.
|
|
13
|
+
block=''
|
|
14
|
+
if [ -f "$chat/server.mjs" ] && command -v parley-mcp >/dev/null 2>&1; then block="$block
|
|
15
|
+
[[mcp_servers]]
|
|
16
|
+
name = \"parley\"
|
|
17
|
+
transport = \"stdio\"
|
|
18
|
+
command = \"sh\"
|
|
19
|
+
args = [\"$relay\", \"parley-mcp\", \"--dir\", \"$chat\"]"; fi
|
|
20
|
+
if [ -f "$chat/server.mjs" ] && command -v docket-mcp >/dev/null 2>&1; then block="$block
|
|
21
|
+
[[mcp_servers]]
|
|
22
|
+
name = \"docket\"
|
|
23
|
+
transport = \"stdio\"
|
|
24
|
+
command = \"sh\"
|
|
25
|
+
args = [\"$relay\", \"docket-mcp\", \"--dir\", \"$docket\"]"; fi
|
|
26
|
+
if command -v tether-mcp >/dev/null 2>&1; then block="$block
|
|
27
|
+
[[mcp_servers]]
|
|
28
|
+
name = \"tether\"
|
|
29
|
+
transport = \"stdio\"
|
|
30
|
+
command = \"sh\"
|
|
31
|
+
args = [\"$relay\", \"tether-mcp\", \"--dir\", \"$tether\"]"; fi
|
|
32
|
+
node "$here/toml-block.mjs" .vibe/config.toml tools "$block" \
|
|
33
|
+
|| echo "launch: Vibe has not its tools (MCP)" >&2
|
|
34
|
+
# WHAT ITS SCREEN ASKS, read by the generic screen-watch -- Vibe's own words: an update told to the
|
|
35
|
+
# agent, which restarts when it suits it.
|
|
36
|
+
node "$here/screen-watch.mjs" \
|
|
37
|
+
--tool vibe \
|
|
38
|
+
'notice:(new version|update) (is )?available[^\n]{0,120}=Your tool has an update: when nothing runs, touch /work/.parley/restart-asked to take it up.' \
|
|
39
|
+
</dev/null >/dev/null 2>&1 &
|
|
40
|
+
exec "$@"
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
#!/bin/sh
|
|
2
|
+
# THE HEARTBEAT OF A MACHINE (step 6), run as root by its timer (clavarium-beat.timer, every minute or so):
|
|
3
|
+
# for the conversation of a machine of its own, or for each conversation living in a shared one, the look its
|
|
4
|
+
# server made by a command every two minutes (health.mjs, PROBE) is made here, as that conversation's own user,
|
|
5
|
+
# and written into its /work (beat-one). Its server reads it from its own side: no command into the machine.
|
|
6
|
+
#
|
|
7
|
+
# Root reads nothing that a conversation wrote: only its own settings (beat.conf, written at install) and its
|
|
8
|
+
# own counters (/var/lib/clavarium/beat). Each conversation is given a bounded time; a dead /work only stops
|
|
9
|
+
# its beat, and its server then looks the usual way.
|
|
10
|
+
set -u
|
|
11
|
+
lib=/usr/local/lib/clavarium
|
|
12
|
+
state=/var/lib/clavarium/beat
|
|
13
|
+
mode=""; conversation=""; version=""
|
|
14
|
+
[ -r "$lib/beat.conf" ] || exit 0
|
|
15
|
+
while IFS='=' read -r k v; do
|
|
16
|
+
case "$k" in mode) mode=$v ;; conversation) conversation=$v ;; version) version=$v ;; esac
|
|
17
|
+
done < "$lib/beat.conf"
|
|
18
|
+
printf '%s' "$version" | grep -Eq '^[0-9A-Za-z.+-]{1,64}$' || exit 0
|
|
19
|
+
ok_name() { printf '%s' "$1" | grep -Eq '^[a-z]([a-z0-9-]{0,30}[a-z0-9])?$'; }
|
|
20
|
+
mkdir -p "$state"; chmod 700 "$state"
|
|
21
|
+
# Its next number: a beat that does not grow is not a new one (read by its server).
|
|
22
|
+
next() {
|
|
23
|
+
n=$(head -c 20 "$state/$1.seq" 2>/dev/null) || n=0
|
|
24
|
+
case "$n" in ''|*[!0-9]*) n=0 ;; esac
|
|
25
|
+
n=$((n + 1)); printf '%s\n' "$n" > "$state/$1.seq"; printf '%s' "$n"
|
|
26
|
+
}
|
|
27
|
+
case "$mode" in
|
|
28
|
+
own)
|
|
29
|
+
ok_name "$conversation" || exit 0
|
|
30
|
+
n=$(next "$conversation")
|
|
31
|
+
cd / && timeout -k 5 45 runuser -u ubuntu -- env HOME=/home/ubuntu sh "$lib/beat-one" "$conversation" "$n" "$version" </dev/null >/dev/null 2>&1 || true ;;
|
|
32
|
+
shared)
|
|
33
|
+
# Only a conversation whose work is shown here now (the host mounts it at /srv/clavarium/NAME/work);
|
|
34
|
+
# at most 64 of them, one after another.
|
|
35
|
+
i=0
|
|
36
|
+
for d in /srv/clavarium/*/; do
|
|
37
|
+
[ "$i" -lt 64 ] || break
|
|
38
|
+
c=$(basename "$d"); ok_name "$c" || continue
|
|
39
|
+
mountpoint -q "/srv/clavarium/$c/work" 2>/dev/null || continue
|
|
40
|
+
i=$((i + 1)); n=$(next "$c")
|
|
41
|
+
timeout -k 5 45 /usr/local/sbin/clavarium-enter "$c" -- sh "$lib/beat-one" "$c" "$n" "$version" </dev/null >/dev/null 2>&1 || true
|
|
42
|
+
done ;;
|
|
43
|
+
esac
|
|
44
|
+
exit 0
|