baychat 0.21.5 → 0.23.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/README.md +75 -1
- package/dist/approve-hook.js +5 -2
- package/dist/commands.js +6 -4
- package/dist/connect-acp.js +165 -0
- package/dist/connect-cursor.js +65 -0
- package/dist/connect-dsh.js +45 -0
- package/dist/connect-plan.js +16 -1
- package/dist/connect.js +17 -0
- package/dist/doctor-command.js +7 -0
- package/dist/doctor.js +93 -1
- package/dist/dsh-config.js +147 -0
- package/dist/index.js +27 -0
- package/dist/relay/acp/agents.js +231 -0
- package/dist/relay/acp/approval.js +67 -0
- package/dist/relay/acp/client.js +265 -0
- package/dist/relay/acp/commands.js +69 -0
- package/dist/relay/acp/cursor.js +271 -0
- package/dist/relay/acp/daemon-glue.js +198 -0
- package/dist/relay/acp/dump-config.js +31 -0
- package/dist/relay/acp/modes.js +36 -0
- package/dist/relay/acp/permissions.js +104 -0
- package/dist/relay/acp/policy.js +138 -0
- package/dist/relay/acp/presence.js +87 -0
- package/dist/relay/acp/prompt.js +68 -0
- package/dist/relay/acp/runner.js +364 -0
- package/dist/relay/acp/sdk.js +19 -0
- package/dist/relay/acp/turn-queue.js +163 -0
- package/dist/relay/acp/types.js +5 -0
- package/dist/relay/commands.js +28 -0
- package/dist/relay/daemon.js +195 -0
- package/dist/relay/mailbox.js +1 -0
- package/dist/relay/profiles.js +22 -0
- package/dist/relay/registry.js +46 -1
- package/dist/relay/session-commands.js +89 -0
- package/dist/relay/session-reply.js +25 -0
- package/dist/relay/terminal-pane.js +197 -0
- package/dist/relay/types.js +0 -9
- package/dist/runtime-binary.js +17 -0
- package/dist/runtimes.js +13 -0
- package/dist/session-command.js +16 -3
- package/dist/skill-bootstrap.js +16 -8
- package/dist/start-command.js +235 -0
- package/dist/update-command.js +201 -0
- package/package.json +6 -2
|
@@ -0,0 +1,147 @@
|
|
|
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.mergeBayChatEntry = mergeBayChatEntry;
|
|
37
|
+
exports.writeDshMcpEntry = writeDshMcpEntry;
|
|
38
|
+
const fs = __importStar(require("fs"));
|
|
39
|
+
const os = __importStar(require("os"));
|
|
40
|
+
const path = __importStar(require("path"));
|
|
41
|
+
const crypto_1 = require("crypto");
|
|
42
|
+
// dsh composes every profile from layers, and `~/.dsh/cordis.patch.yml` applies to all of them —
|
|
43
|
+
// its equivalent of `~/.codex/config.toml`. The entry below was hand-wired and RUN on
|
|
44
|
+
// 2026-09-18 (docs/planning/2026-09-18-deepseek-harness-step-by-step.md).
|
|
45
|
+
//
|
|
46
|
+
// WHY A MARKED TEXT BLOCK AND NOT A YAML MERGE. The client's file may contain dsh's `!!js` tags,
|
|
47
|
+
// which a generic YAML parser rejects or, worse, round-trips wrongly. We never parse their file:
|
|
48
|
+
// we own the lines between two markers and nothing else, and we back the file up before writing.
|
|
49
|
+
//
|
|
50
|
+
// The token is a literal, as it is in Codex's config — the file is 0600. `connect` rewrites the
|
|
51
|
+
// block on every run, so a re-issued token replaces the old one rather than leaving it behind.
|
|
52
|
+
const START = "# >>> baychat (managed by `baychat connect dsh` — edits between these lines are overwritten)";
|
|
53
|
+
const END = "# <<< baychat";
|
|
54
|
+
const CORRUPT_BLOCK_MESSAGE = "~/.dsh/cordis.patch.yml has a damaged BayChat block (a start marker without its end, or " +
|
|
55
|
+
"duplicates). Remove the lines between '# >>> baychat' and '# <<< baychat' by hand, then run " +
|
|
56
|
+
"`baychat connect dsh` again.";
|
|
57
|
+
/**
|
|
58
|
+
* Refuse to interpolate anything that could break out of the block's YAML.
|
|
59
|
+
*
|
|
60
|
+
* Device tokens are `bay_u_` + hex and the MCP url is ours, so neither check should ever fire in
|
|
61
|
+
* practice — but a value we did not generate should never be trusted to hold still inside a
|
|
62
|
+
* double-quoted YAML scalar just because it happens to today.
|
|
63
|
+
*/
|
|
64
|
+
function validateEntry(entry) {
|
|
65
|
+
if (!/^[A-Za-z0-9_]+$/.test(entry.token)) {
|
|
66
|
+
throw new Error("Refusing to write a BayChat MCP entry: the device token contains characters other than letters, digits and underscore.");
|
|
67
|
+
}
|
|
68
|
+
if (/["'\s]/.test(entry.url)) {
|
|
69
|
+
throw new Error("Refusing to write a BayChat MCP entry: the MCP URL contains a quote, newline or whitespace.");
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Find the single well-formed managed block in `existing`, or `null` when there is none at all.
|
|
74
|
+
*
|
|
75
|
+
* RULING R22: a START with no END after it, an END with no START, or more than one of either
|
|
76
|
+
* marker is CORRUPT — the file was hand-edited into a shape we cannot safely replace. Silently
|
|
77
|
+
* appending a second block on top of that would leave the old one, stale token included, in the
|
|
78
|
+
* file forever, so this throws instead of guessing.
|
|
79
|
+
*/
|
|
80
|
+
function findManagedBlock(existing) {
|
|
81
|
+
const firstStart = existing.indexOf(START);
|
|
82
|
+
const lastStart = existing.lastIndexOf(START);
|
|
83
|
+
const firstEnd = existing.indexOf(END);
|
|
84
|
+
const lastEnd = existing.lastIndexOf(END);
|
|
85
|
+
const hasStart = firstStart >= 0;
|
|
86
|
+
const hasEnd = firstEnd >= 0;
|
|
87
|
+
if (!hasStart && !hasEnd)
|
|
88
|
+
return null;
|
|
89
|
+
if (hasStart !== hasEnd)
|
|
90
|
+
throw new Error(CORRUPT_BLOCK_MESSAGE);
|
|
91
|
+
if (firstStart !== lastStart || firstEnd !== lastEnd)
|
|
92
|
+
throw new Error(CORRUPT_BLOCK_MESSAGE);
|
|
93
|
+
if (firstEnd < firstStart)
|
|
94
|
+
throw new Error(CORRUPT_BLOCK_MESSAGE);
|
|
95
|
+
return { from: firstStart, to: firstEnd };
|
|
96
|
+
}
|
|
97
|
+
function mergeBayChatEntry(existing, entry) {
|
|
98
|
+
validateEntry(entry);
|
|
99
|
+
const block = [
|
|
100
|
+
START,
|
|
101
|
+
"- insert:",
|
|
102
|
+
" - id: mcp-baychat",
|
|
103
|
+
" name: '@deepseek-ai/dsh-mcp-client'",
|
|
104
|
+
" config:",
|
|
105
|
+
" serverName: baychat",
|
|
106
|
+
" transport: streamable-http",
|
|
107
|
+
` url: ${entry.url}`,
|
|
108
|
+
" headers:",
|
|
109
|
+
` Authorization: "Bearer ${entry.token}"`,
|
|
110
|
+
END,
|
|
111
|
+
"",
|
|
112
|
+
].join("\n");
|
|
113
|
+
const found = findManagedBlock(existing);
|
|
114
|
+
if (found) {
|
|
115
|
+
const after = existing.slice(found.to + END.length).replace(/^\n/, "");
|
|
116
|
+
return `${existing.slice(0, found.from)}${block}${after}`;
|
|
117
|
+
}
|
|
118
|
+
const head = existing === "" || existing.endsWith("\n") ? existing : `${existing}\n`;
|
|
119
|
+
return `${head}${block}`;
|
|
120
|
+
}
|
|
121
|
+
function writeDshMcpEntry(entry, home = os.homedir()) {
|
|
122
|
+
const file = path.join(home, ".dsh", "cordis.patch.yml");
|
|
123
|
+
fs.mkdirSync(path.dirname(file), { recursive: true });
|
|
124
|
+
const existing = fs.existsSync(file) ? fs.readFileSync(file, "utf8") : "";
|
|
125
|
+
// Computed BEFORE anything is written: a corrupt block, or an unsafe entry, throws here and
|
|
126
|
+
// leaves the file — and any backup — untouched.
|
|
127
|
+
const next = mergeBayChatEntry(existing, entry);
|
|
128
|
+
let backup;
|
|
129
|
+
if (existing) {
|
|
130
|
+
backup = `${file}.baychat-backup`;
|
|
131
|
+
fs.writeFileSync(backup, existing, { mode: 0o600 });
|
|
132
|
+
}
|
|
133
|
+
// Atomic: write to a temp file in the same directory, then rename over the target. A reader —
|
|
134
|
+
// or a crash mid-write — never sees a half-written config. Same pattern as `writeModePatches`
|
|
135
|
+
// in relay/acp/agents.ts and `writeHermesMcp` in hermes-mcp.ts.
|
|
136
|
+
const temp = `${file}.${process.pid}.${(0, crypto_1.randomUUID)()}.tmp`;
|
|
137
|
+
try {
|
|
138
|
+
fs.writeFileSync(temp, next, { mode: 0o600, flag: "wx" });
|
|
139
|
+
fs.chmodSync(temp, 0o600); // `mode` above is filtered by the umask
|
|
140
|
+
fs.renameSync(temp, file);
|
|
141
|
+
}
|
|
142
|
+
catch (err) {
|
|
143
|
+
fs.rmSync(temp, { force: true });
|
|
144
|
+
throw err;
|
|
145
|
+
}
|
|
146
|
+
return { file, backup };
|
|
147
|
+
}
|
package/dist/index.js
CHANGED
|
@@ -4,6 +4,8 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
4
4
|
const commands_1 = require("./commands");
|
|
5
5
|
const approve_hook_1 = require("./approve-hook");
|
|
6
6
|
const session_name_1 = require("./session-name");
|
|
7
|
+
const start_command_1 = require("./start-command");
|
|
8
|
+
const update_command_1 = require("./update-command");
|
|
7
9
|
const session_command_1 = require("./session-command");
|
|
8
10
|
const doctor_command_1 = require("./doctor-command");
|
|
9
11
|
const hermes_1 = require("./hermes");
|
|
@@ -31,6 +33,16 @@ Exit 0: step succeeded; 2: awaiting approval or expired; 1: error.
|
|
|
31
33
|
const HELP = `baychat — BayChat connector CLI for agent sessions (Claude Code, Codex)
|
|
32
34
|
|
|
33
35
|
Usage:
|
|
36
|
+
baychat Start a session. This is the whole thing: it runs
|
|
37
|
+
your agent somewhere BayChat can reach, so /clear
|
|
38
|
+
and /new from the app work. Sign in once with
|
|
39
|
+
\`baychat login\` first
|
|
40
|
+
baychat start [name] [--runtime <runtime>]
|
|
41
|
+
The same, with a name for the session
|
|
42
|
+
baychat update Bring this machine onto the current version —
|
|
43
|
+
the package, every connected client's config, and
|
|
44
|
+
the relay daemon (which otherwise keeps running
|
|
45
|
+
old code). \`baychat\` does this for you once a day
|
|
34
46
|
baychat join [name] [group] [--sessions | --private | --group <title>] [--runtime <runtime>]
|
|
35
47
|
baychat join --session <name> --group <title> --runtime codex
|
|
36
48
|
baychat skill --runtime codex|claude Read the current packaged skill workflow
|
|
@@ -47,6 +59,11 @@ Usage:
|
|
|
47
59
|
phone, and your client's MCP config is written for
|
|
48
60
|
you. Then run /baychat <name> in any session to
|
|
49
61
|
join as that session. No config blocks, no editing
|
|
62
|
+
baychat connect dsh|cursor-cli [--folder <path>] [--max-mode <mode>] [--mode <mode>] [--session <name>]
|
|
63
|
+
An agent the relay runs by itself (DeepSeek Harness,
|
|
64
|
+
Cursor's CLI): you talk to it from the app. The
|
|
65
|
+
folder and the highest mode are set here, never
|
|
66
|
+
from a chat
|
|
50
67
|
baychat login [--start | --finish | --token <PAT>] [--base <url>]
|
|
51
68
|
Log this laptop in to BayChat (QR) and add the
|
|
52
69
|
BayChat MCP server to Claude Code. Creates a
|
|
@@ -170,6 +187,12 @@ function numberFlag(args, name) {
|
|
|
170
187
|
}
|
|
171
188
|
async function main() {
|
|
172
189
|
const [command, ...args] = process.argv.slice(2);
|
|
190
|
+
// BARE `baychat` STARTS A SESSION. The bar is "download BayChat, start a session, nothing
|
|
191
|
+
// else", and a usage dump printed at somebody who typed the product's own name is the
|
|
192
|
+
// "something else". `--help` still prints the help, and `start` refuses with one sentence
|
|
193
|
+
// when this machine is not signed in, so nothing is launched from an unusable state.
|
|
194
|
+
if (command === undefined)
|
|
195
|
+
return (0, start_command_1.cmdStart)([]);
|
|
173
196
|
// Setup help must be side-effect free. Do not apply this to chat commands:
|
|
174
197
|
// a sent message may legitimately contain the literal text "--help".
|
|
175
198
|
if (["login", "link", "connect", "pair", "join", "skill"].includes(command) &&
|
|
@@ -184,6 +207,10 @@ async function main() {
|
|
|
184
207
|
return 0;
|
|
185
208
|
case "join":
|
|
186
209
|
return (0, session_command_1.cmdJoinSession)(args);
|
|
210
|
+
case "start":
|
|
211
|
+
return (0, start_command_1.cmdStart)(args);
|
|
212
|
+
case "update":
|
|
213
|
+
return (0, update_command_1.cmdUpdate)();
|
|
187
214
|
case "skill": {
|
|
188
215
|
const runtime = args[1];
|
|
189
216
|
if (args.length !== 2 ||
|
|
@@ -0,0 +1,231 @@
|
|
|
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.ACP_AGENTS = exports.DSH_LOCAL_TOOLS = void 0;
|
|
37
|
+
exports.acpAgent = acpAgent;
|
|
38
|
+
exports.describeAgentMode = describeAgentMode;
|
|
39
|
+
exports.otherAgentRefusal = otherAgentRefusal;
|
|
40
|
+
exports.binaryIdentified = binaryIdentified;
|
|
41
|
+
exports.unidentifiedBinary = unidentifiedBinary;
|
|
42
|
+
exports.connectCommandFor = connectCommandFor;
|
|
43
|
+
exports.matchesTestedVersion = matchesTestedVersion;
|
|
44
|
+
exports.modeAllowedHere = modeAllowedHere;
|
|
45
|
+
exports.acpPatchDir = acpPatchDir;
|
|
46
|
+
exports.patchPath = patchPath;
|
|
47
|
+
exports.writeModePatch = writeModePatch;
|
|
48
|
+
const fs = __importStar(require("fs"));
|
|
49
|
+
const path = __importStar(require("path"));
|
|
50
|
+
const config_1 = require("../../config");
|
|
51
|
+
const modes_1 = require("./modes");
|
|
52
|
+
const cursor_1 = require("./cursor");
|
|
53
|
+
const permissions_1 = require("./permissions");
|
|
54
|
+
// Every tool plugin in dsh's `acp` profile (`--profile acp --dump-config`, 0.1.5-rc.2) [run].
|
|
55
|
+
// MCP clients are mounted per ACP session by dsh-acp itself and are untouched by these.
|
|
56
|
+
// Confirmed against docs/planning/2026-09-19-acp-research/dump-acp-default.yml — the tool-* ids
|
|
57
|
+
// there match this list exactly, in the same order.
|
|
58
|
+
exports.DSH_LOCAL_TOOLS = [
|
|
59
|
+
"tool-bash", "tool-pwsh", "tool-jobs", "tool-fs", "tool-fs-search", "tool-skill",
|
|
60
|
+
"tool-subagent-control", "tool-subagent-list-agents", "tool-subagent", "tool-subagent-fork",
|
|
61
|
+
"tool-workflow", "tool-todo", "tool-goal", "tool-ralph", "tool-web",
|
|
62
|
+
];
|
|
63
|
+
const disable = (ids, why) => [`# Written by BayChat. ${why}`, ...ids.flatMap((id) => [`- id: ${id}`, " disabled: true"]), ""].join("\n");
|
|
64
|
+
const dsh = {
|
|
65
|
+
id: "dsh",
|
|
66
|
+
label: "DeepSeek Harness",
|
|
67
|
+
bin: "dsh",
|
|
68
|
+
patches: {
|
|
69
|
+
chat: disable(exports.DSH_LOCAL_TOOLS, "chat mode: no local tools at all."),
|
|
70
|
+
read: disable(exports.DSH_LOCAL_TOOLS.filter((id) => id !== "tool-fs" && id !== "tool-fs-search"), "read mode: file read and search only — no shell, no subagents, no web."),
|
|
71
|
+
},
|
|
72
|
+
patchFile: (mode) => `dsh-${mode}.yml`, // shared by every dsh session: its patches name no session
|
|
73
|
+
launch(mode, patchDir, session) {
|
|
74
|
+
const args = ["--profile", "acp"];
|
|
75
|
+
if (this.patches[mode])
|
|
76
|
+
args.push("--patch", patchPath(this, mode, session, patchDir));
|
|
77
|
+
// `danger-full-access` is deliberately unreachable from here: no mode maps to it.
|
|
78
|
+
return { args, env: { DSH_PERMISSION_MODE: mode === "full" ? "workspace-write" : "read-only" } };
|
|
79
|
+
},
|
|
80
|
+
// dsh's Windows sandbox reports itself `partial` (Everyone-writable objects, NTFS hard links)
|
|
81
|
+
// [source], so the modes that RELY on it are withheld there. `chat` needs no sandbox and
|
|
82
|
+
// `read`'s write fence is an in-process check.
|
|
83
|
+
modesFor: (platform) => (platform === "win32" ? ["chat", "read"] : ["chat", "read", "ask", "full"]),
|
|
84
|
+
classifyError(message) {
|
|
85
|
+
if (/no API key|MISSING_CREDENTIAL|INVALID_CREDENTIAL/i.test(message))
|
|
86
|
+
return "missing-key";
|
|
87
|
+
if (/mcp-client\(baychat\)/i.test(message))
|
|
88
|
+
return "baychat-unreachable";
|
|
89
|
+
if (/already active|session is active/i.test(message))
|
|
90
|
+
return "busy";
|
|
91
|
+
if (/session cwd does not match/i.test(message))
|
|
92
|
+
return "session";
|
|
93
|
+
return undefined;
|
|
94
|
+
},
|
|
95
|
+
decidePermission: permissions_1.decideDshPermission,
|
|
96
|
+
keyHint: "DeepSeek has no API key on that computer. There, run `dsh web` and add the key on the Models page.",
|
|
97
|
+
installHint: "Install it with `npm install -g @deepseek-ai/dsh`, then run `baychat connect dsh` again.",
|
|
98
|
+
connectCommand: "baychat connect dsh",
|
|
99
|
+
credentialSeen: (home) => Boolean(process.env.DEEPSEEK_API_KEY) ||
|
|
100
|
+
fs.existsSync(path.join(process.env.DSH_HOME ?? path.join(home, ".dsh"), ".credentials.yaml")),
|
|
101
|
+
// Experimental and not security-audited (R11/R17).
|
|
102
|
+
safetyNote: "DeepSeek recommends a disposable VM or container",
|
|
103
|
+
confidence: "run",
|
|
104
|
+
checked: "2026-09-19",
|
|
105
|
+
testedVersion: "0.1.5-rc.2",
|
|
106
|
+
};
|
|
107
|
+
exports.ACP_AGENTS = [dsh, cursor_1.cursor];
|
|
108
|
+
function acpAgent(id) {
|
|
109
|
+
return exports.ACP_AGENTS.find((row) => row.id === id);
|
|
110
|
+
}
|
|
111
|
+
/** How `mode` is described for a session of this agent: its own words if it has them, else the shared ones. */
|
|
112
|
+
function describeAgentMode(row, mode) {
|
|
113
|
+
return row?.modeDescriptions?.[mode] ?? (0, modes_1.describeMode)(mode);
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* The refusal for giving `row` a session name another agent's relay-run session already holds,
|
|
117
|
+
* or undefined when the name is free or already this agent's. A session id, a binary and a mode
|
|
118
|
+
* belong to the agent that made them; handing them to a different agent is never a re-register.
|
|
119
|
+
*/
|
|
120
|
+
function otherAgentRefusal(row, session, heldBy) {
|
|
121
|
+
if (heldBy === undefined || heldBy === row.id)
|
|
122
|
+
return undefined;
|
|
123
|
+
return `"${session}" is already run by the relay for ${acpAgent(heldBy)?.label ?? heldBy}. Choose another name (${row.connectCommand} --session <name>).`;
|
|
124
|
+
}
|
|
125
|
+
/** Is `version` (a binary's `--version` first line) this agent's? True for a row with no pattern. */
|
|
126
|
+
function binaryIdentified(row, version) {
|
|
127
|
+
return row.versionPattern?.test(version) ?? true;
|
|
128
|
+
}
|
|
129
|
+
/** Said when the binary found under the agent's name is not the agent. Names the fix. */
|
|
130
|
+
function unidentifiedBinary(row, binaryPath, version) {
|
|
131
|
+
return `${binaryPath} does not look like ${row.label}'s CLI (\`--version\` said "${version}"). ${row.installHint}`;
|
|
132
|
+
}
|
|
133
|
+
/** The setup command to name for `agent` — a neutral one when this relay does not know it. */
|
|
134
|
+
function connectCommandFor(agent) {
|
|
135
|
+
return (agent !== undefined ? acpAgent(agent)?.connectCommand : undefined) ?? "baychat connect <agent>";
|
|
136
|
+
}
|
|
137
|
+
/** A version-string character: digits, letters, dot, plus, hyphen — the semver/rc alphabet. */
|
|
138
|
+
const VERSION_TOKEN_CHAR = /[0-9A-Za-z.+-]/;
|
|
139
|
+
/**
|
|
140
|
+
* Does `version` (raw `--version` output) contain `tested` as a WHOLE version token, not merely
|
|
141
|
+
* as a substring?
|
|
142
|
+
*
|
|
143
|
+
* `"0.1.5-rc.20".includes("0.1.5-rc.2")` is `true` — a plain substring check silently passes a
|
|
144
|
+
* different release and the mismatch warning this exists for never shows. So `tested` must be
|
|
145
|
+
* bounded: the character before it is the start of the string, something that is not part of a
|
|
146
|
+
* version token, or a leading `v` (allowed unconditionally — a version prefix, not a token
|
|
147
|
+
* character to fence against); the character after it is the end of the string or something that
|
|
148
|
+
* is not part of a version token. `--version` output varies by CLI — `"0.1.5-rc.2"`,
|
|
149
|
+
* `"dsh 0.1.5-rc.2"`, `"v0.1.5-rc.2"`, `"0.1.5-rc.2 (linux-x64)"` — and this matches all of them
|
|
150
|
+
* while refusing `"0.1.5-rc.20"`, `"10.1.5-rc.2"`, `"0.1.5-rc.2.1"` and `"0.1.5-rc.2-beta"`.
|
|
151
|
+
*/
|
|
152
|
+
function matchesTestedVersion(version, tested) {
|
|
153
|
+
if (!tested)
|
|
154
|
+
return false;
|
|
155
|
+
let from = 0;
|
|
156
|
+
for (;;) {
|
|
157
|
+
const at = version.indexOf(tested, from);
|
|
158
|
+
if (at === -1)
|
|
159
|
+
return false;
|
|
160
|
+
const before = at === 0 ? undefined : version[at - 1];
|
|
161
|
+
const after = version[at + tested.length];
|
|
162
|
+
const boundedBefore = before === undefined || before === "v" || !VERSION_TOKEN_CHAR.test(before);
|
|
163
|
+
const boundedAfter = after === undefined || !VERSION_TOKEN_CHAR.test(after);
|
|
164
|
+
if (boundedBefore && boundedAfter)
|
|
165
|
+
return true;
|
|
166
|
+
from = at + 1;
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
/**
|
|
170
|
+
* May a session of this agent run in `mode`, in a folder whose ceiling is `maxMode`, on this
|
|
171
|
+
* platform? The ONE rule: `/mode` from the chat and `acp-register` from `connect` both ask it.
|
|
172
|
+
*/
|
|
173
|
+
function modeAllowedHere(row, mode, maxMode, platform) {
|
|
174
|
+
if ((0, modes_1.modeRank)(mode) > (0, modes_1.modeRank)(maxMode)) {
|
|
175
|
+
return { ok: false, reason: `${mode} is above the highest mode allowed for that folder (${maxMode}). That ceiling is set on the computer, never from a chat: run \`${row.connectCommand}\` there to change it.` };
|
|
176
|
+
}
|
|
177
|
+
if (!row.modesFor(platform).includes(mode)) {
|
|
178
|
+
return { ok: false, reason: `${mode} is not offered on that computer's operating system, because ${row.label}'s sandbox cannot fully enforce it there.` };
|
|
179
|
+
}
|
|
180
|
+
return { ok: true };
|
|
181
|
+
}
|
|
182
|
+
/** Where mode patches live: `~/.baychat/acp/`. */
|
|
183
|
+
function acpPatchDir() {
|
|
184
|
+
return path.join((0, config_1.configDir)(), "acp");
|
|
185
|
+
}
|
|
186
|
+
function patchPath(row, mode, session, dir) {
|
|
187
|
+
return path.join(dir, row.patchFile(mode, session));
|
|
188
|
+
}
|
|
189
|
+
/**
|
|
190
|
+
* (Re)write the overlay a turn of `session` in `mode` reads, if the agent needs one there.
|
|
191
|
+
* Idempotent; called by `connect` and before every turn, so an upgrade that changes an overlay,
|
|
192
|
+
* or a `/mode-*` that changes which one applies, takes effect at the next turn.
|
|
193
|
+
*
|
|
194
|
+
* NEVER TRUNCATE IN PLACE. dsh's overlays are shared by its sessions, which drain concurrently,
|
|
195
|
+
* so one session's rewrite can land while another's freshly spawned agent is reading the same
|
|
196
|
+
* file — and an empty `chat` patch is an agent with every local tool. An overlay that is already
|
|
197
|
+
* right is left alone; one that is not is written beside it and renamed over it, so a reader sees
|
|
198
|
+
* the old file or the new, never half.
|
|
199
|
+
*/
|
|
200
|
+
function writeModePatch(row, mode, session, dir) {
|
|
201
|
+
const content = row.patches[mode];
|
|
202
|
+
if (content === undefined)
|
|
203
|
+
return;
|
|
204
|
+
const file = patchPath(row, mode, session, dir);
|
|
205
|
+
fs.mkdirSync(path.dirname(file), { recursive: true, mode: 0o700 });
|
|
206
|
+
const onDisk = readOrUndefined(file);
|
|
207
|
+
const isCurrent = row.patchIsCurrent ?? ((current, wanted) => current === wanted);
|
|
208
|
+
if (onDisk !== undefined && isCurrent(onDisk, content)) {
|
|
209
|
+
fs.chmodSync(file, 0o600); // permissions only — the content and the inode stay as they are
|
|
210
|
+
return;
|
|
211
|
+
}
|
|
212
|
+
const tmp = `${file}.${process.pid}.${Math.random().toString(36).slice(2)}.tmp`;
|
|
213
|
+
try {
|
|
214
|
+
fs.writeFileSync(tmp, content, { mode: 0o600, flag: "wx" });
|
|
215
|
+
fs.chmodSync(tmp, 0o600); // `mode` is filtered by the umask
|
|
216
|
+
fs.renameSync(tmp, file);
|
|
217
|
+
}
|
|
218
|
+
catch (err) {
|
|
219
|
+
fs.rmSync(tmp, { force: true });
|
|
220
|
+
throw err;
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
/** A read error (absent, unreadable) means "write it". */
|
|
224
|
+
function readOrUndefined(file) {
|
|
225
|
+
try {
|
|
226
|
+
return fs.readFileSync(file, "utf8");
|
|
227
|
+
}
|
|
228
|
+
catch {
|
|
229
|
+
return undefined;
|
|
230
|
+
}
|
|
231
|
+
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.askPhone = askPhone;
|
|
4
|
+
const api_1 = require("../../api");
|
|
5
|
+
const approve_hook_1 = require("../../approve-hook");
|
|
6
|
+
const HOLD_SECONDS = 25; // under the usual 60s proxy idle timeout — same as approve-hook
|
|
7
|
+
const MAX_CONSECUTIVE_FAILURES = 3;
|
|
8
|
+
const RETRY_DELAY_MS = 2_000;
|
|
9
|
+
const ALLOW_INDEX = 0; // the ONLY index that means yes
|
|
10
|
+
/**
|
|
11
|
+
* Put a decision card on the owner's phone and wait for the tap.
|
|
12
|
+
*
|
|
13
|
+
* NEVER THROWS, and every path that is not an explicit "Allow this once" is a refusal: an
|
|
14
|
+
* unreachable server, a status we do not know, a withdrawn card, the deadline. The card itself
|
|
15
|
+
* never expires server-side; our deadline exists so a turn cannot hold a process forever, and
|
|
16
|
+
* reaching it REFUSES. Silence is never a yes.
|
|
17
|
+
*/
|
|
18
|
+
async function askPhone(params, deps = {}) {
|
|
19
|
+
const request = deps.request ?? api_1.apiRequest;
|
|
20
|
+
const now = deps.now ?? Date.now;
|
|
21
|
+
const sleep = deps.sleep ?? ((ms) => new Promise((r) => setTimeout(r, ms)));
|
|
22
|
+
const waitSeconds = params.waitSeconds ?? approve_hook_1.DEFAULT_WAIT_SECONDS;
|
|
23
|
+
const deadline = now() + waitSeconds * 1000;
|
|
24
|
+
const hold = () => Math.max(1, Math.min(HOLD_SECONDS, Math.ceil((deadline - now()) / 1000)));
|
|
25
|
+
const no = (reason) => ({ allowed: false, reason });
|
|
26
|
+
try {
|
|
27
|
+
let reply = await request(params.auth, "POST", "/api/device-api/approvals", {
|
|
28
|
+
session: params.session,
|
|
29
|
+
conversationId: params.conversationId,
|
|
30
|
+
question: params.question,
|
|
31
|
+
options: [...approve_hook_1.APPROVE_OPTIONS],
|
|
32
|
+
wait: hold(),
|
|
33
|
+
});
|
|
34
|
+
const requestId = typeof reply.requestId === "string" ? reply.requestId : "";
|
|
35
|
+
if (!requestId)
|
|
36
|
+
return no("BayChat did not return an approval id");
|
|
37
|
+
let failures = 0;
|
|
38
|
+
for (;;) {
|
|
39
|
+
if (reply.status === "answered") {
|
|
40
|
+
return reply.answerIndex === ALLOW_INDEX
|
|
41
|
+
? { allowed: true, reason: "approved in BayChat" }
|
|
42
|
+
: no(`answered "${reply.answer ?? approve_hook_1.APPROVE_OPTIONS[1]}" in BayChat`);
|
|
43
|
+
}
|
|
44
|
+
if (reply.status === "cancelled")
|
|
45
|
+
return no("the decision was withdrawn in BayChat");
|
|
46
|
+
if (reply.status !== "waiting")
|
|
47
|
+
return no(`BayChat answered with a status this relay does not understand ("${String(reply.status)}")`);
|
|
48
|
+
if (deadline - now() <= 0)
|
|
49
|
+
return no(`nobody answered within ${waitSeconds}s — the card is still in BayChat, and this request was refused`);
|
|
50
|
+
try {
|
|
51
|
+
reply = await request(params.auth, "GET", `/api/device-api/approvals/${encodeURIComponent(requestId)}?wait=${hold()}`);
|
|
52
|
+
failures = 0;
|
|
53
|
+
}
|
|
54
|
+
catch (err) {
|
|
55
|
+
if (err instanceof api_1.ApiError && err.status < 500 && err.status !== 429)
|
|
56
|
+
return no(`BayChat refused the wait — ${err.message}`);
|
|
57
|
+
failures += 1;
|
|
58
|
+
if (failures >= MAX_CONSECUTIVE_FAILURES)
|
|
59
|
+
return no(`BayChat became unreachable while waiting — ${err instanceof Error ? err.message : String(err)}`);
|
|
60
|
+
await sleep(RETRY_DELAY_MS);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
catch (err) {
|
|
65
|
+
return no(`BayChat could not be asked — ${err instanceof Error ? err.message : String(err)}`);
|
|
66
|
+
}
|
|
67
|
+
}
|