clavarium-docket 0.1.1-next.1 → 0.1.2-next.2
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/package.json +1 -1
- package/server/docket.mjs +5 -2
- package/server/relay.mjs +71 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "clavarium-docket",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.2-next.2",
|
|
4
4
|
"description": "Clavarium Docket: events an AI agent or a script sees and acknowledges, and reminders it sets itself (a command and an MCP server, and a library for the application). No dependency.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"homepage": "https://github.com/clavarium/clavarium-docket#readme",
|
package/server/docket.mjs
CHANGED
|
@@ -25,7 +25,7 @@ import { randomUUID, createHash } from 'node:crypto';
|
|
|
25
25
|
import { createInterface } from 'node:readline';
|
|
26
26
|
import { zoneOf, isZone, parseWhen, parseIn, parseCron, nextCron, lastCron, wall } from './time.mjs';
|
|
27
27
|
|
|
28
|
-
export const VERSION = '0.1.
|
|
28
|
+
export const VERSION = '0.1.2-next.2';
|
|
29
29
|
export const FOLDER = '.docket';
|
|
30
30
|
export const TEXT_MAX = 500;
|
|
31
31
|
export const NOTE_MAX = 1000;
|
|
@@ -233,5 +233,8 @@ if (main) {
|
|
|
233
233
|
const at = process.argv.indexOf('--dir');
|
|
234
234
|
const dir = findFolder({ dir: at >= 0 ? process.argv[at + 1] : undefined });
|
|
235
235
|
if (!dir) { process.stderr.write(`No ${FOLDER} folder: give --dir FOLDER.\n`); process.exit(2); }
|
|
236
|
-
|
|
236
|
+
/* Under a relay (02-11): a new version installed is taken up between two requests, the session never
|
|
237
|
+
restarted. --child: the server itself, as the relay starts it. */
|
|
238
|
+
if (process.argv.includes('--child') || process.env.DOCKET_NO_RELAY === '1' || process.env.MCP_RELAYED === '1') serve(dir);
|
|
239
|
+
else void import('./relay.mjs').then(({ relay }) => relay({ args: [HERE, ...process.argv.slice(2), '--child'], packageJson: join(dirname(HERE), '..', 'package.json') })).catch(() => serve(dir));
|
|
237
240
|
}
|
package/server/relay.mjs
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
/* TAKEN UP WITHOUT A RESTART (02-11). An MCP server lives as long as the session that started it: a new
|
|
2
|
+
* version installed under it was not seen until the session itself was restarted ("on ne peut pas mettre à
|
|
3
|
+
* jour un composant sans redémarrer la session ?"). So the server runs as a child of this relay; between two
|
|
4
|
+
* requests, when the version installed on disk is no longer the child's, the relay starts the new one, says
|
|
5
|
+
* hello to it as the session did, and tells the session its tools may have changed. Nothing is lost: a
|
|
6
|
+
* request in flight is answered by the child it went to, the next goes to the new one. */
|
|
7
|
+
import { spawn } from 'node:child_process';
|
|
8
|
+
import { readFileSync } from 'node:fs';
|
|
9
|
+
import { createInterface } from 'node:readline';
|
|
10
|
+
|
|
11
|
+
/* -> the version installed now, read from package.json (null if unreadable: keep the child). */
|
|
12
|
+
export const installedVersion = (packageJson) => { try { return String(JSON.parse(readFileSync(packageJson, 'utf8')).version ?? '') || null; } catch { return null; } };
|
|
13
|
+
|
|
14
|
+
export function relay({ command = process.execPath, args, packageJson, input = process.stdin, output = process.stdout, spawner = spawn, version = installedVersion, log = () => {} }) {
|
|
15
|
+
let child = null; let running = null; let hello = null; let helloDone = false;
|
|
16
|
+
const pending = new Set(); const queue = [];
|
|
17
|
+
const send = (m) => output.write(`${JSON.stringify(m)}\n`);
|
|
18
|
+
const start = () => {
|
|
19
|
+
running = version(packageJson);
|
|
20
|
+
const c = spawner(command, args, { stdio: ['pipe', 'pipe', 'inherit'] });
|
|
21
|
+
child = c;
|
|
22
|
+
createInterface({ input: c.stdout }).on('line', (line) => {
|
|
23
|
+
let m; try { m = JSON.parse(line); } catch { return; }
|
|
24
|
+
if (m && typeof m.id === 'string' && m.id.startsWith('relay:')) return; /* its answer to our own hello */
|
|
25
|
+
if (m && m.id !== undefined) pending.delete(m.id);
|
|
26
|
+
output.write(`${line}\n`);
|
|
27
|
+
if (pending.size === 0) flush();
|
|
28
|
+
});
|
|
29
|
+
c.on('exit', () => {
|
|
30
|
+
if (child !== c) return;
|
|
31
|
+
child = null;
|
|
32
|
+
/* It stopped under a request: answered, never left waiting; the next one starts it again. */
|
|
33
|
+
for (const id of pending) send({ jsonrpc: '2.0', id, error: { code: -32603, message: 'The tool stopped; try again.' } });
|
|
34
|
+
pending.clear(); flush();
|
|
35
|
+
});
|
|
36
|
+
return c;
|
|
37
|
+
};
|
|
38
|
+
const greet = (c) => {
|
|
39
|
+
if (!hello) return;
|
|
40
|
+
c.stdin.write(`${JSON.stringify({ ...hello, id: `relay:${Date.now()}` })}\n`);
|
|
41
|
+
c.stdin.write(`${JSON.stringify({ jsonrpc: '2.0', method: 'notifications/initialized' })}\n`);
|
|
42
|
+
};
|
|
43
|
+
/* Between requests: the new version if there is one (once nothing is in flight), then what waited. */
|
|
44
|
+
const needsNew = () => { if (!child) return true; if (!helloDone) return false; const now = version(packageJson); return Boolean(now && now !== running); };
|
|
45
|
+
const flush = () => {
|
|
46
|
+
while (queue.length) {
|
|
47
|
+
if (needsNew()) {
|
|
48
|
+
if (pending.size > 0) return;
|
|
49
|
+
const old = child; const was = running;
|
|
50
|
+
const c = start(); greet(c);
|
|
51
|
+
log(`relay: ${was} -> ${running}`);
|
|
52
|
+
try { old?.stdin.end(); old?.kill(); } catch { /* gone already */ }
|
|
53
|
+
if (helloDone) send({ jsonrpc: '2.0', method: 'notifications/tools/list_changed' });
|
|
54
|
+
}
|
|
55
|
+
const m = queue.shift();
|
|
56
|
+
if (m.id !== undefined) pending.add(m.id);
|
|
57
|
+
child.stdin.write(`${JSON.stringify(m)}\n`);
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
start();
|
|
61
|
+
createInterface({ input }).on('line', (line) => {
|
|
62
|
+
let m; try { m = JSON.parse(line); } catch { return; }
|
|
63
|
+
if (!m || typeof m !== 'object') return;
|
|
64
|
+
if (m.method === 'initialize') { hello = m; }
|
|
65
|
+
if (m.method === 'notifications/initialized') helloDone = true;
|
|
66
|
+
queue.push(m);
|
|
67
|
+
flush();
|
|
68
|
+
});
|
|
69
|
+
input.on('end', () => { try { child?.stdin.end(); } catch { /* gone */ } });
|
|
70
|
+
return { get child() { return child; }, get version() { return running; } };
|
|
71
|
+
}
|