baychat 0.57.0 → 0.58.0
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/.claude-plugin/plugin.json +1 -1
- package/README.md +2 -1
- package/dist/mcp-todos.js +49 -0
- package/dist/mcp.js +5 -5
- package/dist/protocol-content.js +1 -1
- package/dist/relay/autostart-here.js +141 -0
- package/dist/relay/autostart.js +11 -60
- package/dist/relay/commands.js +9 -28
- package/dist/relay/launcher/capabilities.js +8 -1
- package/dist/relay/windows-task-xml.js +67 -0
- package/dist/relay/wsl-keeper.js +278 -0
- package/dist/tool-defs-outputs.js +73 -0
- package/dist/tool-defs.js +8 -18
- package/dist/tool-hints.js +2 -0
- package/package.json +1 -1
package/dist/relay/autostart.js
CHANGED
|
@@ -68,6 +68,7 @@ const fs = __importStar(require("fs"));
|
|
|
68
68
|
const os = __importStar(require("os"));
|
|
69
69
|
const path = __importStar(require("path"));
|
|
70
70
|
const util_1 = require("util");
|
|
71
|
+
const windows_task_xml_1 = require("./windows-task-xml");
|
|
71
72
|
const execFileAsync = (0, util_1.promisify)(child_process_1.execFile);
|
|
72
73
|
/** The reverse-DNS name macOS and Windows both want, and the systemd unit's stem.
|
|
73
74
|
* One constant so a rename cannot half-happen: a plist labelled one thing and
|
|
@@ -163,8 +164,8 @@ const launchd = {
|
|
|
163
164
|
<string>${exports.SERVICE_LABEL}</string>
|
|
164
165
|
<key>ProgramArguments</key>
|
|
165
166
|
<array>
|
|
166
|
-
<string>${escapeXml(node)}</string>
|
|
167
|
-
<string>${escapeXml(entry)}</string>
|
|
167
|
+
<string>${(0, windows_task_xml_1.escapeXml)(node)}</string>
|
|
168
|
+
<string>${(0, windows_task_xml_1.escapeXml)(entry)}</string>
|
|
168
169
|
<string>relay</string>
|
|
169
170
|
<string>start</string>
|
|
170
171
|
<string>--foreground</string>
|
|
@@ -181,9 +182,9 @@ const launchd = {
|
|
|
181
182
|
<string>${exports.MAC_SEARCH_PATH}</string>
|
|
182
183
|
</dict>
|
|
183
184
|
<key>StandardOutPath</key>
|
|
184
|
-
<string>${escapeXml(launchdLogPath())}</string>
|
|
185
|
+
<string>${(0, windows_task_xml_1.escapeXml)(launchdLogPath())}</string>
|
|
185
186
|
<key>StandardErrorPath</key>
|
|
186
|
-
<string>${escapeXml(launchdLogPath())}</string>
|
|
187
|
+
<string>${(0, windows_task_xml_1.escapeXml)(launchdLogPath())}</string>
|
|
187
188
|
<key>ProcessType</key>
|
|
188
189
|
<string>Background</string>
|
|
189
190
|
</dict>
|
|
@@ -314,51 +315,12 @@ function prepareLaunchdLog() {
|
|
|
314
315
|
*/
|
|
315
316
|
const scheduledTask = {
|
|
316
317
|
name: "Task Scheduler",
|
|
317
|
-
unit: (cmd) =>
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
<LogonTrigger>
|
|
324
|
-
<Enabled>true</Enabled>
|
|
325
|
-
<UserId>${escapeXml(windowsUserId())}</UserId>
|
|
326
|
-
</LogonTrigger>
|
|
327
|
-
</Triggers>
|
|
328
|
-
<Principals>
|
|
329
|
-
<Principal id="Author">
|
|
330
|
-
<UserId>${escapeXml(windowsUserId())}</UserId>
|
|
331
|
-
<LogonType>InteractiveToken</LogonType>
|
|
332
|
-
<RunLevel>LeastPrivilege</RunLevel>
|
|
333
|
-
</Principal>
|
|
334
|
-
</Principals>
|
|
335
|
-
<Settings>
|
|
336
|
-
<MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy>
|
|
337
|
-
<DisallowStartIfOnBatteries>false</DisallowStartIfOnBatteries>
|
|
338
|
-
<StopIfGoingOnBatteries>false</StopIfGoingOnBatteries>
|
|
339
|
-
<StartWhenAvailable>true</StartWhenAvailable>
|
|
340
|
-
<RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable>
|
|
341
|
-
<IdleSettings>
|
|
342
|
-
<StopOnIdleEnd>false</StopOnIdleEnd>
|
|
343
|
-
<RestartOnIdle>false</RestartOnIdle>
|
|
344
|
-
</IdleSettings>
|
|
345
|
-
<AllowHardTerminate>true</AllowHardTerminate>
|
|
346
|
-
<Enabled>true</Enabled>
|
|
347
|
-
<Hidden>true</Hidden>
|
|
348
|
-
<ExecutionTimeLimit>PT0S</ExecutionTimeLimit>
|
|
349
|
-
<RestartOnFailure>
|
|
350
|
-
<Interval>PT1M</Interval>
|
|
351
|
-
<Count>999</Count>
|
|
352
|
-
</RestartOnFailure>
|
|
353
|
-
</Settings>
|
|
354
|
-
<Actions Context="Author">
|
|
355
|
-
<Exec>
|
|
356
|
-
<Command>${escapeXml(path.win32.join(process.env.SystemRoot || "C:\\Windows", "System32", "wscript.exe"))}</Command>
|
|
357
|
-
<Arguments>//B //NoLogo ${escapeXml(`"${windowsLauncherPath()}" "${cmd.node}" "${cmd.entry}"`)} relay start --foreground</Arguments>
|
|
358
|
-
</Exec>
|
|
359
|
-
</Actions>
|
|
360
|
-
</Task>
|
|
361
|
-
`,
|
|
318
|
+
unit: (cmd) => (0, windows_task_xml_1.windowsTaskXml)({
|
|
319
|
+
description: "BayChat relay — wakes local agent sessions on new messages",
|
|
320
|
+
userId: windowsUserId(),
|
|
321
|
+
command: path.win32.join(process.env.SystemRoot || "C:\\Windows", "System32", "wscript.exe"),
|
|
322
|
+
args: `//B //NoLogo "${windowsLauncherPath()}" "${cmd.node}" "${cmd.entry}" relay start --foreground`,
|
|
323
|
+
}),
|
|
362
324
|
// Written beside the credentials rather than in %TEMP%: schtasks reads it once,
|
|
363
325
|
// but keeping it means `relay status` can show what was actually registered.
|
|
364
326
|
unitPath: () => path.join(configHome(), "relay-task.xml"),
|
|
@@ -421,17 +383,6 @@ function windowsUserId() {
|
|
|
421
383
|
function configHome() {
|
|
422
384
|
return process.env.BAYCHAT_CONFIG_DIR || path.join(os.homedir(), ".baychat");
|
|
423
385
|
}
|
|
424
|
-
/** The five characters that would otherwise end the element early. Paths can
|
|
425
|
-
* legally contain `&` on every platform this ships to, and a home directory with
|
|
426
|
-
* one in it would produce a plist launchd rejects with no useful message. */
|
|
427
|
-
function escapeXml(value) {
|
|
428
|
-
return value
|
|
429
|
-
.replace(/&/g, "&")
|
|
430
|
-
.replace(/</g, "<")
|
|
431
|
-
.replace(/>/g, ">")
|
|
432
|
-
.replace(/"/g, """)
|
|
433
|
-
.replace(/'/g, "'");
|
|
434
|
-
}
|
|
435
386
|
const BY_PLATFORM = {
|
|
436
387
|
linux: systemd,
|
|
437
388
|
darwin: launchd,
|
package/dist/relay/commands.js
CHANGED
|
@@ -57,6 +57,7 @@ const adapters_1 = require("./adapters");
|
|
|
57
57
|
const profiles_1 = require("./profiles");
|
|
58
58
|
const provider_env_1 = require("./provider-env");
|
|
59
59
|
const autostart_1 = require("./autostart");
|
|
60
|
+
const autostart_here_1 = require("./autostart-here");
|
|
60
61
|
const runtime_binary_1 = require("../runtime-binary");
|
|
61
62
|
const terminal_pane_1 = require("./terminal-pane");
|
|
62
63
|
const owner_pid_1 = require("./owner-pid");
|
|
@@ -131,6 +132,8 @@ async function cmdRelayStart(opts = {}) {
|
|
|
131
132
|
// DEFAULT Bay's, exactly as before; every other Bay login gets its own daemon in a child
|
|
132
133
|
// process scoped to that Bay. A per-Bay child supervises nothing and stops with its parent.
|
|
133
134
|
const child = (0, bay_relays_1.isBayRelayChild)();
|
|
135
|
+
if (!child)
|
|
136
|
+
void (0, autostart_here_1.ensureStartsWithWindows)((line) => console.log((0, log_line_1.relayLogLine)(line)));
|
|
134
137
|
const supervisor = child
|
|
135
138
|
? undefined
|
|
136
139
|
: new bay_relays_1.BayRelaySupervisor({
|
|
@@ -168,32 +171,7 @@ async function cmdRelayStart(opts = {}) {
|
|
|
168
171
|
}
|
|
169
172
|
return;
|
|
170
173
|
}
|
|
171
|
-
|
|
172
|
-
console.log("Relay is already running. `baychat relay status` for details.");
|
|
173
|
-
return;
|
|
174
|
-
}
|
|
175
|
-
const autostart = (0, autostart_1.autostartForPlatform)();
|
|
176
|
-
if (!autostart) {
|
|
177
|
-
console.log(`No automatic startup is available on ${process.platform}. Run the daemon`);
|
|
178
|
-
console.log("yourself with:");
|
|
179
|
-
console.log(" baychat relay start --foreground");
|
|
180
|
-
return;
|
|
181
|
-
}
|
|
182
|
-
try {
|
|
183
|
-
await autostart.install((0, autostart_1.relayCommand)());
|
|
184
|
-
console.log(`Relay started and enabled at boot (${autostart.name}).`);
|
|
185
|
-
}
|
|
186
|
-
catch (err) {
|
|
187
|
-
if (!(err instanceof autostart_1.LingerUnavailable))
|
|
188
|
-
throw err;
|
|
189
|
-
// The relay IS installed and running; only the boot-time half is missing.
|
|
190
|
-
// Reported rather than swallowed, because "enabled at boot" would be a lie
|
|
191
|
-
// and the user would not find out until the machine next rebooted unattended.
|
|
192
|
-
console.log(`Relay started and enabled at login (${autostart.name}).`);
|
|
193
|
-
console.log(`Note: ${err.message}.`);
|
|
194
|
-
}
|
|
195
|
-
console.log(" baychat relay status — see sessions, cursor, pending deliveries");
|
|
196
|
-
console.log(" baychat relay stop — stop it");
|
|
174
|
+
await (0, autostart_here_1.startRelayInBackground)();
|
|
197
175
|
}
|
|
198
176
|
/**
|
|
199
177
|
* Best-effort relay setup, run as the last step of `baychat connect`.
|
|
@@ -213,7 +191,7 @@ async function ensureRelayInstalled() {
|
|
|
213
191
|
}
|
|
214
192
|
if (await (0, socket_1.probeSocket)((0, socket_1.socketPath)()))
|
|
215
193
|
return "Relay: running. Join a session to connect incoming messages.";
|
|
216
|
-
const autostart = (0,
|
|
194
|
+
const autostart = (0, autostart_here_1.autostartHere)();
|
|
217
195
|
if (!autostart) {
|
|
218
196
|
return `Relay: no automatic startup on ${process.platform}. Run \`baychat relay start --foreground\` to wake sessions instantly.`;
|
|
219
197
|
}
|
|
@@ -311,6 +289,8 @@ async function cmdRelayStatus() {
|
|
|
311
289
|
console.log(` last error: ${status.lastError}`);
|
|
312
290
|
if (status.autoUpdate)
|
|
313
291
|
console.log(` auto-update: ${(0, describe_1.describeAutoUpdate)(status.autoUpdate, Date.now())}`);
|
|
292
|
+
for (const line of await (0, autostart_here_1.startsWithWindowsLines)())
|
|
293
|
+
console.log(line);
|
|
314
294
|
for (const line of await bayRelayLines())
|
|
315
295
|
console.log(line);
|
|
316
296
|
// Computed before the session list so a line can report its own last failure.
|
|
@@ -450,7 +430,8 @@ async function cmdRelayStop() {
|
|
|
450
430
|
// relay is back within the minute — systemd's Restart=always, launchd's
|
|
451
431
|
// KeepAlive and the scheduled task's RestartOnFailure all exist to make that
|
|
452
432
|
// happen, and all three would make `relay stop` look like it silently failed.
|
|
453
|
-
|
|
433
|
+
(0, autostart_here_1.rememberStoppedByHand)();
|
|
434
|
+
const autostart = (0, autostart_here_1.autostartHere)();
|
|
454
435
|
if (autostart && fs.existsSync(autostart.unitPath())) {
|
|
455
436
|
try {
|
|
456
437
|
await autostart.uninstall();
|
|
@@ -37,6 +37,7 @@ exports.RUNTIME_BINARY = exports.PHONE_ALLOW_RUNTIMES = void 0;
|
|
|
37
37
|
exports.buildCapabilities = buildCapabilities;
|
|
38
38
|
exports.legacyCapabilities = legacyCapabilities;
|
|
39
39
|
const os = __importStar(require("os"));
|
|
40
|
+
const machine_name_1 = require("../../machine-name");
|
|
40
41
|
/** The runtimes whose trust the relay can write, so the only ones the phone may allow (spot-request.ts). */
|
|
41
42
|
exports.PHONE_ALLOW_RUNTIMES = ["claude", "codex"];
|
|
42
43
|
/** The executable each launch runtime is resolved by. */
|
|
@@ -87,13 +88,19 @@ function buildCapabilities(policy, deps) {
|
|
|
87
88
|
}
|
|
88
89
|
const phoneOpen = spots.some((s) => s.phoneMayAllow);
|
|
89
90
|
return {
|
|
90
|
-
machine: (deps
|
|
91
|
+
machine: computerName(deps),
|
|
91
92
|
os: deps.platform ?? process.platform,
|
|
92
93
|
multiplexer: deps.multiplexer,
|
|
93
94
|
spots,
|
|
94
95
|
...(phoneOpen ? { installedRuntimes: exports.PHONE_ALLOW_RUNTIMES.filter((r) => resolutionOf(r).ok) } : {}),
|
|
95
96
|
};
|
|
96
97
|
}
|
|
98
|
+
/** The same name the computer's login has (machine-name.ts): a WSL relay is "<host> (WSL)", never the Windows host's own. */
|
|
99
|
+
function computerName(deps) {
|
|
100
|
+
const host = (deps.hostname ?? os.hostname()).replace(/\.local$/, "");
|
|
101
|
+
const wsl = deps.wsl ?? (deps.platform === undefined && (0, machine_name_1.isWsl)());
|
|
102
|
+
return (host ? (0, machine_name_1.machineName)(host, wsl) : "").slice(0, MACHINE_MAX) || "computer";
|
|
103
|
+
}
|
|
97
104
|
/** The report as a server before CLI 0.41 takes it: its strict schema refuses the whole report for one unknown
|
|
98
105
|
* key, which would take every folder of this computer off Start. */
|
|
99
106
|
function legacyCapabilities(caps) {
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.windowsTaskXml = windowsTaskXml;
|
|
4
|
+
exports.escapeXml = escapeXml;
|
|
5
|
+
/**
|
|
6
|
+
* The task document, shared by the native Windows relay and the WSL keeper (./wsl-keeper.ts).
|
|
7
|
+
* RestartOnFailure and `ExecutionTimeLimit` PT0S are what the `/sc onlogon` flags cannot say:
|
|
8
|
+
* without them the relay would stay dead after a crash, and stop by itself every third day.
|
|
9
|
+
*/
|
|
10
|
+
function windowsTaskXml(task) {
|
|
11
|
+
return `<?xml version="1.0" encoding="UTF-16"?>
|
|
12
|
+
<Task version="1.2" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
|
|
13
|
+
<RegistrationInfo>
|
|
14
|
+
<Description>${escapeXml(task.description)}</Description>
|
|
15
|
+
</RegistrationInfo>
|
|
16
|
+
<Triggers>
|
|
17
|
+
<LogonTrigger>
|
|
18
|
+
<Enabled>true</Enabled>
|
|
19
|
+
<UserId>${escapeXml(task.userId)}</UserId>
|
|
20
|
+
</LogonTrigger>
|
|
21
|
+
</Triggers>
|
|
22
|
+
<Principals>
|
|
23
|
+
<Principal id="Author">
|
|
24
|
+
<UserId>${escapeXml(task.userId)}</UserId>
|
|
25
|
+
<LogonType>InteractiveToken</LogonType>
|
|
26
|
+
<RunLevel>LeastPrivilege</RunLevel>
|
|
27
|
+
</Principal>
|
|
28
|
+
</Principals>
|
|
29
|
+
<Settings>
|
|
30
|
+
<MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy>
|
|
31
|
+
<DisallowStartIfOnBatteries>false</DisallowStartIfOnBatteries>
|
|
32
|
+
<StopIfGoingOnBatteries>false</StopIfGoingOnBatteries>
|
|
33
|
+
<StartWhenAvailable>true</StartWhenAvailable>
|
|
34
|
+
<RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable>
|
|
35
|
+
<IdleSettings>
|
|
36
|
+
<StopOnIdleEnd>false</StopOnIdleEnd>
|
|
37
|
+
<RestartOnIdle>false</RestartOnIdle>
|
|
38
|
+
</IdleSettings>
|
|
39
|
+
<AllowHardTerminate>true</AllowHardTerminate>
|
|
40
|
+
<Enabled>true</Enabled>
|
|
41
|
+
<Hidden>true</Hidden>
|
|
42
|
+
<ExecutionTimeLimit>PT0S</ExecutionTimeLimit>
|
|
43
|
+
<RestartOnFailure>
|
|
44
|
+
<Interval>PT1M</Interval>
|
|
45
|
+
<Count>999</Count>
|
|
46
|
+
</RestartOnFailure>
|
|
47
|
+
</Settings>
|
|
48
|
+
<Actions Context="Author">
|
|
49
|
+
<Exec>
|
|
50
|
+
<Command>${escapeXml(task.command)}</Command>
|
|
51
|
+
<Arguments>${escapeXml(task.args)}</Arguments>
|
|
52
|
+
</Exec>
|
|
53
|
+
</Actions>
|
|
54
|
+
</Task>
|
|
55
|
+
`;
|
|
56
|
+
}
|
|
57
|
+
/** The five characters that would otherwise end the element early. Paths can
|
|
58
|
+
* legally contain `&` on every platform this ships to, and a home directory with
|
|
59
|
+
* one in it would produce a plist launchd rejects with no useful message. */
|
|
60
|
+
function escapeXml(value) {
|
|
61
|
+
return value
|
|
62
|
+
.replace(/&/g, "&")
|
|
63
|
+
.replace(/</g, "<")
|
|
64
|
+
.replace(/>/g, ">")
|
|
65
|
+
.replace(/"/g, """)
|
|
66
|
+
.replace(/'/g, "'");
|
|
67
|
+
}
|
|
@@ -0,0 +1,278 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
+
exports.WindowsUnreachable = exports.WSL_RELAY_LAUNCHER = void 0;
|
|
37
|
+
exports.wslKeeper = wslKeeper;
|
|
38
|
+
exports.holdScript = holdScript;
|
|
39
|
+
exports.parseWindowsEnv = parseWindowsEnv;
|
|
40
|
+
exports.distroFromUnc = distroFromUnc;
|
|
41
|
+
const child_process_1 = require("child_process");
|
|
42
|
+
const fs = __importStar(require("fs"));
|
|
43
|
+
const os = __importStar(require("os"));
|
|
44
|
+
const path = __importStar(require("path"));
|
|
45
|
+
const util_1 = require("util");
|
|
46
|
+
const autostart_1 = require("./autostart");
|
|
47
|
+
const windows_task_xml_1 = require("./windows-task-xml");
|
|
48
|
+
const execFileAsync = (0, util_1.promisify)(child_process_1.execFile);
|
|
49
|
+
/**
|
|
50
|
+
* The hidden launcher (Windows Script Host JScript, as for the native Windows relay) that starts
|
|
51
|
+
* wsl.exe. Unlike that one it quotes only an argument with a space: wsl.exe hands what follows
|
|
52
|
+
* `--exec` to Linux as written, so a quoted "/bin/sleep" is a command Linux can't find (exit 127,
|
|
53
|
+
* seen on this PC, 3 Dec 2026).
|
|
54
|
+
*/
|
|
55
|
+
exports.WSL_RELAY_LAUNCHER = `var shell = new ActiveXObject("WScript.Shell");
|
|
56
|
+
var command = [];
|
|
57
|
+
for (var index = 0; index < WScript.Arguments.length; index++) {
|
|
58
|
+
var argument = WScript.Arguments(index);
|
|
59
|
+
if (/["\\r\\n]/.test(argument)) throw new Error("Invalid relay startup argument");
|
|
60
|
+
command.push(/\\s/.test(argument) ? '"' + argument + '"' : argument);
|
|
61
|
+
}
|
|
62
|
+
WScript.Quit(shell.Run(command.join(" "), 0, true));
|
|
63
|
+
`;
|
|
64
|
+
const WINDOWS_UNREACHABLE = "Ubuntu can't reach Windows, so the relay won't start with Windows. To fix it: add the two lines " +
|
|
65
|
+
"[interop] and enabled=true to /etc/wsl.conf, run wsl --shutdown in PowerShell, then run " +
|
|
66
|
+
"baychat relay start in Ubuntu again";
|
|
67
|
+
/** WSL's interop is off or broken: no Windows program can be started from Ubuntu. */
|
|
68
|
+
class WindowsUnreachable extends Error {
|
|
69
|
+
constructor() {
|
|
70
|
+
super(WINDOWS_UNREACHABLE);
|
|
71
|
+
this.name = "WindowsUnreachable";
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
exports.WindowsUnreachable = WindowsUnreachable;
|
|
75
|
+
/** The relay runs (systemd) but will not start with Windows: the smaller promise, said. */
|
|
76
|
+
class RunsOnlyWhileUbuntuIsOpen extends autostart_1.LingerUnavailable {
|
|
77
|
+
constructor() {
|
|
78
|
+
super();
|
|
79
|
+
this.message = WINDOWS_UNREACHABLE;
|
|
80
|
+
this.name = "RunsOnlyWhileUbuntuIsOpen";
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
function wslKeeper(deps = realWslDeps()) {
|
|
84
|
+
const localCopy = path.posix.join(deps.configHome, "relay-wsl-task.xml");
|
|
85
|
+
const holder = path.posix.join(deps.configHome, "wsl-hold.sh");
|
|
86
|
+
const holderPid = path.posix.join(deps.configHome, "wsl-hold.pid");
|
|
87
|
+
const schtasks = (side, args) => deps.run(path.posix.join(side.system32, "schtasks.exe"), args);
|
|
88
|
+
const taskNameFor = (side) => deps.taskName ?? `${autostart_1.SERVICE_LABEL}.wsl.${side.distro.replace(/[^A-Za-z0-9._-]/g, "-")}`;
|
|
89
|
+
async function windowsSide() {
|
|
90
|
+
const env = parseWindowsEnv(await runCmdSet(deps));
|
|
91
|
+
const root = env.SYSTEMROOT || "C:\\Windows";
|
|
92
|
+
if (!env.LOCALAPPDATA || !env.USERNAME)
|
|
93
|
+
throw new WindowsUnreachable();
|
|
94
|
+
const folderWin = `${env.LOCALAPPDATA}\\BayChatRelay`;
|
|
95
|
+
const system32Win = `${root}\\System32`;
|
|
96
|
+
const [folder, system32] = await Promise.all([folderWin, system32Win].map(async (p) => (await deps.run("wslpath", ["-u", p])).trim()));
|
|
97
|
+
const distro = deps.env.WSL_DISTRO_NAME || distroFromUnc((await deps.run("wslpath", ["-w", "/"])).trim());
|
|
98
|
+
const userId = env.USERDOMAIN ? `${env.USERDOMAIN}\\${env.USERNAME}` : env.USERNAME;
|
|
99
|
+
return { folder: folder, folderWin, system32: system32, system32Win, userId, distro };
|
|
100
|
+
}
|
|
101
|
+
function taskXml(side, cmd, systemdOn) {
|
|
102
|
+
const inside = systemdOn ? ["/bin/sh", holder] : [cmd.node, cmd.entry, "relay", "start", "--foreground"];
|
|
103
|
+
const argv = [`${side.folderWin}\\wsl-relay-launcher.js`, `${side.system32Win}\\wsl.exe`, "-d", side.distro, "-u", deps.user, "--exec", ...inside];
|
|
104
|
+
return (0, windows_task_xml_1.windowsTaskXml)({
|
|
105
|
+
description: systemdOn ? "BayChat relay — keeps Ubuntu running so the relay inside it stays up" : "BayChat relay — wakes local agent sessions on new messages",
|
|
106
|
+
userId: side.userId,
|
|
107
|
+
command: `${side.system32Win}\\wscript.exe`,
|
|
108
|
+
args: `//B //NoLogo ${argv.map((a) => `"${a}"`).join(" ")}`,
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
/** Ending the task ends its wscript, not the wsl.exe under it: the holder is stopped by its PID. */
|
|
112
|
+
function stopHolder() {
|
|
113
|
+
const pid = Number(deps.readFile(holderPid)?.trim());
|
|
114
|
+
const cmdline = Number.isInteger(pid) && pid > 1 ? deps.readFile(`/proc/${pid}/cmdline`) : undefined;
|
|
115
|
+
if (cmdline === "/bin/sleep\0infinity\0")
|
|
116
|
+
deps.kill(pid);
|
|
117
|
+
deps.rm(holderPid);
|
|
118
|
+
}
|
|
119
|
+
/** Register (or replace) the logon task; `run` also starts it now. */
|
|
120
|
+
async function registerTask(cmd, opts) {
|
|
121
|
+
const side = await windowsSide();
|
|
122
|
+
const name = taskNameFor(side);
|
|
123
|
+
// UTF-16LE with a BOM: what the XML declaration claims and what schtasks wants.
|
|
124
|
+
const xml = Buffer.from("\u{feff}" + taskXml(side, cmd, deps.systemdBooted()), "utf16le");
|
|
125
|
+
deps.mkdir(side.folder);
|
|
126
|
+
deps.writeFile(path.posix.join(side.folder, "wsl-relay-launcher.js"), exports.WSL_RELAY_LAUNCHER);
|
|
127
|
+
deps.writeFile(path.posix.join(side.folder, `${name}.xml`), xml);
|
|
128
|
+
deps.mkdir(deps.configHome);
|
|
129
|
+
deps.writeFile(localCopy, xml);
|
|
130
|
+
deps.writeFile(holder, holdScript(holderPid));
|
|
131
|
+
await asWindowsAnswer(schtasks(side, ["/create", "/tn", name, "/xml", `${side.folderWin}\\${name}.xml`, "/f"]));
|
|
132
|
+
if (opts.run)
|
|
133
|
+
await asWindowsAnswer(schtasks(side, ["/run", "/tn", name]));
|
|
134
|
+
}
|
|
135
|
+
const keeper = {
|
|
136
|
+
get name() {
|
|
137
|
+
return deps.systemdBooted() ? "systemd, kept running by a Windows logon task" : "a Windows logon task";
|
|
138
|
+
},
|
|
139
|
+
unit: (cmd) => (deps.systemdBooted() ? deps.systemd.unit(cmd) : ""),
|
|
140
|
+
unitPath: () => (deps.systemdBooted() ? deps.systemd.unitPath() : localCopy),
|
|
141
|
+
async install(cmd) {
|
|
142
|
+
const systemdOn = deps.systemdBooted();
|
|
143
|
+
let lingerNote;
|
|
144
|
+
if (systemdOn) {
|
|
145
|
+
try {
|
|
146
|
+
await deps.systemd.install(cmd);
|
|
147
|
+
}
|
|
148
|
+
catch (err) {
|
|
149
|
+
if (!(err instanceof autostart_1.LingerUnavailable))
|
|
150
|
+
throw err;
|
|
151
|
+
lingerNote = err;
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
try {
|
|
155
|
+
await registerTask(cmd, { run: true });
|
|
156
|
+
}
|
|
157
|
+
catch (err) {
|
|
158
|
+
if (systemdOn && err instanceof WindowsUnreachable)
|
|
159
|
+
throw new RunsOnlyWhileUbuntuIsOpen();
|
|
160
|
+
throw err;
|
|
161
|
+
}
|
|
162
|
+
if (lingerNote)
|
|
163
|
+
throw lingerNote;
|
|
164
|
+
},
|
|
165
|
+
/** Ends and deletes the task, then the unit. Throws only when Windows can't be reached. */
|
|
166
|
+
async uninstall() {
|
|
167
|
+
let unreachable;
|
|
168
|
+
try {
|
|
169
|
+
const side = await windowsSide();
|
|
170
|
+
const name = taskNameFor(side);
|
|
171
|
+
// Deleting a task does not stop the wsl.exe it started.
|
|
172
|
+
await schtasks(side, ["/end", "/tn", name]).catch(() => undefined);
|
|
173
|
+
await schtasks(side, ["/delete", "/tn", name, "/f"]).catch(() => undefined);
|
|
174
|
+
deps.rm(path.posix.join(side.folder, `${name}.xml`));
|
|
175
|
+
deps.rm(path.posix.join(side.folder, "wsl-relay-launcher.js"));
|
|
176
|
+
}
|
|
177
|
+
catch (err) {
|
|
178
|
+
if (!(err instanceof WindowsUnreachable))
|
|
179
|
+
throw err;
|
|
180
|
+
unreachable = err;
|
|
181
|
+
}
|
|
182
|
+
if (deps.systemdBooted())
|
|
183
|
+
await deps.systemd.uninstall();
|
|
184
|
+
stopHolder();
|
|
185
|
+
deps.rm(localCopy);
|
|
186
|
+
deps.rm(holder);
|
|
187
|
+
if (unreachable)
|
|
188
|
+
throw unreachable;
|
|
189
|
+
},
|
|
190
|
+
/** Is the logon task registered? Throws WindowsUnreachable when that can't be known. */
|
|
191
|
+
async startsWithWindows() {
|
|
192
|
+
const side = await windowsSide();
|
|
193
|
+
return schtasks(side, ["/query", "/tn", taskNameFor(side)]).then(() => true, () => false);
|
|
194
|
+
},
|
|
195
|
+
registerTask,
|
|
196
|
+
/** With systemd the task only holds Ubuntu open, so starting it is always safe. */
|
|
197
|
+
get holdsUbuntuOpenOnly() {
|
|
198
|
+
return deps.systemdBooted();
|
|
199
|
+
},
|
|
200
|
+
};
|
|
201
|
+
return keeper;
|
|
202
|
+
}
|
|
203
|
+
/** What the task runs inside Ubuntu when systemd runs the relay: nothing but a PID and a wait. */
|
|
204
|
+
function holdScript(pidFile) {
|
|
205
|
+
return `#!/bin/sh
|
|
206
|
+
# Keeps this WSL distro running so the BayChat relay (a systemd unit) stays up.
|
|
207
|
+
# A Windows logon task starts it; \`baychat relay stop\` ends it by this PID.
|
|
208
|
+
echo $$ > ${shellQuoted(pidFile)}
|
|
209
|
+
exec /bin/sleep infinity
|
|
210
|
+
`;
|
|
211
|
+
}
|
|
212
|
+
/** A literal for /bin/sh: a `$`, `"` or space in the config path stays part of the path. */
|
|
213
|
+
function shellQuoted(value) {
|
|
214
|
+
return `'${value.replace(/'/g, `'\\''`)}'`;
|
|
215
|
+
}
|
|
216
|
+
/** `cmd.exe /c set`: by name, then by its full path (a systemd unit's PATH has no Windows folders). */
|
|
217
|
+
async function runCmdSet(deps) {
|
|
218
|
+
for (const cmd of ["cmd.exe", "/mnt/c/Windows/System32/cmd.exe"]) {
|
|
219
|
+
try {
|
|
220
|
+
return await deps.run(cmd, ["/d", "/c", "set"]);
|
|
221
|
+
}
|
|
222
|
+
catch (err) {
|
|
223
|
+
if (err.code !== "ENOENT")
|
|
224
|
+
throw new WindowsUnreachable();
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
throw new WindowsUnreachable();
|
|
228
|
+
}
|
|
229
|
+
/** A Windows program's failure in its own words; a Windows that can't be started is unreachable. */
|
|
230
|
+
async function asWindowsAnswer(call) {
|
|
231
|
+
try {
|
|
232
|
+
await call;
|
|
233
|
+
}
|
|
234
|
+
catch (err) {
|
|
235
|
+
const code = err.code;
|
|
236
|
+
if (code === "ENOENT" || code === "ENOEXEC" || code === "EACCES")
|
|
237
|
+
throw new WindowsUnreachable();
|
|
238
|
+
const said = (err.stderr || err.message).trim();
|
|
239
|
+
throw new Error(`Windows did not accept the relay's startup task: ${said}`);
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
/** `set`'s output as a map; Windows names are case-insensitive, so the keys are upper-cased. */
|
|
243
|
+
function parseWindowsEnv(out) {
|
|
244
|
+
const env = {};
|
|
245
|
+
for (const line of out.split(/\r?\n/)) {
|
|
246
|
+
const at = line.indexOf("=");
|
|
247
|
+
if (at > 0)
|
|
248
|
+
env[line.slice(0, at).toUpperCase()] = line.slice(at + 1).trim();
|
|
249
|
+
}
|
|
250
|
+
return env;
|
|
251
|
+
}
|
|
252
|
+
/** What `wslpath -w /` says (`\\\\wsl.localhost\\Ubuntu\\` or `\\\\wsl$\\Ubuntu\\`) → "Ubuntu". */
|
|
253
|
+
function distroFromUnc(unc) {
|
|
254
|
+
return unc.replace(/^\\\\[^\\]+\\/, "").replace(/\\.*$/, "");
|
|
255
|
+
}
|
|
256
|
+
function realWslDeps() {
|
|
257
|
+
return {
|
|
258
|
+
run: async (file, args) => (await execFileAsync(file, args, { timeout: 30_000, windowsHide: true })).stdout,
|
|
259
|
+
systemdBooted: () => fs.existsSync("/run/systemd/system"),
|
|
260
|
+
writeFile: (file, data) => fs.writeFileSync(file, data),
|
|
261
|
+
mkdir: (dir) => fs.mkdirSync(dir, { recursive: true }),
|
|
262
|
+
rm: (file) => fs.rmSync(file, { force: true }),
|
|
263
|
+
readFile: (file) => {
|
|
264
|
+
try {
|
|
265
|
+
return fs.readFileSync(file, "utf8");
|
|
266
|
+
}
|
|
267
|
+
catch {
|
|
268
|
+
// Absent: no holder was started, or it has ended.
|
|
269
|
+
return undefined;
|
|
270
|
+
}
|
|
271
|
+
},
|
|
272
|
+
kill: (pid) => process.kill(pid, "SIGTERM"),
|
|
273
|
+
env: process.env,
|
|
274
|
+
user: os.userInfo().username,
|
|
275
|
+
configHome: process.env.BAYCHAT_CONFIG_DIR || path.join(os.homedir(), ".baychat"),
|
|
276
|
+
systemd: autostart_1.AUTOSTART_IMPLEMENTATIONS.systemd,
|
|
277
|
+
};
|
|
278
|
+
}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// The better-outputs parts of the canonical BayChat MCP tool definitions (docs/features/BETTER_OUTPUTS.md, protocol
|
|
3
|
+
// 2.45): what `send_message`'s `card` may be, the picture and approve-each-one arguments of `create_poll`, and the
|
|
4
|
+
// to-do list tools. Split out on 30 Nov 2026 so tool-defs.ts does not grow past its size limit (CLAUDE.md § 11).
|
|
5
|
+
//
|
|
6
|
+
// The same rules as tool-defs.ts, whose header says why: this file is copied byte for byte into apps/api
|
|
7
|
+
// (`npm run sync-tool-defs --workspace=apps/api`, parity-tested), so it imports NOTHING but `zod` and its siblings,
|
|
8
|
+
// uses only zod APIs that behave the same in v3 and v4, and its strings are fixed and behaviour-bearing.
|
|
9
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
10
|
+
exports.TODO_TOOL_DEFS = exports.POLL_OUTPUT_ARGS = exports.POLL_OUTPUTS_DESCRIPTION = exports.CARD_DESCRIPTION = exports.SENDABLE_CARD_TYPES = void 0;
|
|
11
|
+
const zod_1 = require("zod");
|
|
12
|
+
const tool_defs_notes_1 = require("./tool-defs-notes");
|
|
13
|
+
/** The cards an agent may send with `send_message` (decision, poll and to-do cards have their own tools). */
|
|
14
|
+
exports.SENDABLE_CARD_TYPES = ["card.calendar", "card.email", "card.chart", "card.page", "card.app", "card.plan", "card.board"];
|
|
15
|
+
/** `send_message`'s `card` argument, described. */
|
|
16
|
+
exports.CARD_DESCRIPTION = "Render as a card. card.calendar: title, start, end (ISO 8601), location, attendees " +
|
|
17
|
+
"[{name,address}], notes. card.email: subject, from {name,address} (display only), " +
|
|
18
|
+
"snippet, body, timestamp. card.chart: title, kind bar|line, labels[2-31], " +
|
|
19
|
+
"series[1-3]{name?, values[] (one per label), highlight?[] (indexes)}, unit?, note?, " +
|
|
20
|
+
"key? (same key again = its next version, v2). Data only: BayChat draws it. " +
|
|
21
|
+
"card.page: title, body (markdown, ≤20,000 chars; no HTML, images show as links), key?, " +
|
|
22
|
+
"subtitle? (≤120), cover? (an image's attachment id: one sent in this chat, or one you uploaded for it with " +
|
|
23
|
+
"create_upload_url and have not sent). " +
|
|
24
|
+
"card.plan: title, columns[1-7]{heading (≤32), tiles[1-8]{text (≤80), by? (who added it: a person or agent in " +
|
|
25
|
+
"the chat, by name or id)}}, key?. card.board: boardId (a board of this chat's project), note? (≤200): its lanes " +
|
|
26
|
+
"are read live, never sent. " +
|
|
27
|
+
"card.app: title, description? (≤300), html (ONE self-contained page, ≤500 KB), key?: runs " +
|
|
28
|
+
"sandboxed when someone opens it. No network (no fetch), no forms, no storage; scripts only " +
|
|
29
|
+
"inline or from cdnjs (react, react-dom, Chart.js, d3, three.js, lodash.js, marked). " +
|
|
30
|
+
"Chart, page, plan, app: changes? (≤200, what this version changed) and answers? (the comment id you answer, " +
|
|
31
|
+
"from your artifact's own thread). Chart, page, plan: credits?[1-3]{who (a person or agent in the chat), did " +
|
|
32
|
+
"(≤60, 'added the map')}, shown as who did what, as told by you. " +
|
|
33
|
+
"A card OR attachments, never both.";
|
|
34
|
+
/** `create_poll`'s better-outputs sentences, appended to its description. */
|
|
35
|
+
exports.POLL_OUTPUTS_DESCRIPTION = " pictures: one image attachment id per answer, in order (sent in this chat, or uploaded for it and not sent): " +
|
|
36
|
+
"a pick with pictures; the one winner is marked when it closes. approveBy: ONE PERSON in the chat (never an " +
|
|
37
|
+
"agent) whose Yes each answer waits for: only they vote, a vote is their Yes, and the card says Ready when " +
|
|
38
|
+
"every answer has it.";
|
|
39
|
+
/** `create_poll`'s better-outputs arguments. */
|
|
40
|
+
exports.POLL_OUTPUT_ARGS = {
|
|
41
|
+
pictures: zod_1.z.array(zod_1.z.string()).optional().describe("One image attachment id per answer, in order."),
|
|
42
|
+
approveBy: zod_1.z.string().optional().describe("The person (name or id) whose Yes each answer waits for."),
|
|
43
|
+
};
|
|
44
|
+
/** The to-do list tools (protocol 2.45), registered by tool-defs.ts after the poll tools. */
|
|
45
|
+
exports.TODO_TOOL_DEFS = [
|
|
46
|
+
{
|
|
47
|
+
name: "create_todo",
|
|
48
|
+
access: "write",
|
|
49
|
+
title: "Make a to-do list the room ticks",
|
|
50
|
+
description: "Post a to-do list (checklist, tasks) the room ticks together: a title and 1–30 short items, drawn " +
|
|
51
|
+
"as a card where each tick shows who did it. Not for a project's work (that is the board's: " +
|
|
52
|
+
"add_board_card). Not in a chat between two Bays. The ticks are in get_messages (the card's todo: " +
|
|
53
|
+
"done[{index, by}]); tick with tick_todo. Returns { todoId, messageId }.",
|
|
54
|
+
inputSchema: {
|
|
55
|
+
conversationId: zod_1.z.string().describe(tool_defs_notes_1.CONVERSATION_ID),
|
|
56
|
+
title: zod_1.z.string().describe("The list's title, ≤120."),
|
|
57
|
+
items: zod_1.z.array(zod_1.z.string()).describe("1–30 items, ≤120 each, in order."),
|
|
58
|
+
},
|
|
59
|
+
},
|
|
60
|
+
{
|
|
61
|
+
name: "tick_todo",
|
|
62
|
+
access: "write",
|
|
63
|
+
title: "Tick an item of a to-do list",
|
|
64
|
+
description: "Tick an item of a to-do list as done, or untick it (done: false). todoId comes from the card's " +
|
|
65
|
+
"cardPayload.todoId; index counts from ZERO. A state, not a toggle: repeating it changes nothing. " +
|
|
66
|
+
"Tick what you did yourself, or what you were asked to.",
|
|
67
|
+
inputSchema: {
|
|
68
|
+
todoId: zod_1.z.string().describe("From the to-do card's cardPayload.todoId."),
|
|
69
|
+
index: zod_1.z.number().describe("Zero-based item."),
|
|
70
|
+
done: zod_1.z.boolean().describe("true ticks it, false unticks it."),
|
|
71
|
+
},
|
|
72
|
+
},
|
|
73
|
+
];
|