@mininglamp-oss/cc-channel-octo 1.0.1 → 1.0.2-dev.0af5b14
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/CHANGELOG.md +45 -1
- package/README.md +2 -2
- package/dist/agent-bridge.d.ts +12 -0
- package/dist/agent-bridge.js +24 -19
- package/dist/agent-bridge.js.map +1 -1
- package/dist/bot-manager.d.ts +103 -0
- package/dist/bot-manager.js +184 -0
- package/dist/bot-manager.js.map +1 -0
- package/dist/cli.d.ts +109 -0
- package/dist/cli.js +467 -0
- package/dist/cli.js.map +1 -0
- package/dist/config-watcher.d.ts +48 -0
- package/dist/config-watcher.js +143 -0
- package/dist/config-watcher.js.map +1 -0
- package/dist/config.d.ts +16 -0
- package/dist/config.js +14 -0
- package/dist/config.js.map +1 -1
- package/dist/configure.d.ts +11 -0
- package/dist/configure.js +106 -0
- package/dist/configure.js.map +1 -0
- package/dist/gateway.d.ts +6 -1
- package/dist/gateway.js +5 -0
- package/dist/gateway.js.map +1 -1
- package/dist/group-context.d.ts +17 -0
- package/dist/group-context.js +70 -0
- package/dist/group-context.js.map +1 -1
- package/dist/index.d.ts +16 -1
- package/dist/index.js +241 -191
- package/dist/index.js.map +1 -1
- package/dist/mention-utils.d.ts +10 -1
- package/dist/mention-utils.js +16 -2
- package/dist/mention-utils.js.map +1 -1
- package/dist/session-router.d.ts +25 -0
- package/dist/session-router.js +72 -1
- package/dist/session-router.js.map +1 -1
- package/dist/stream-relay.d.ts +1 -1
- package/dist/stream-relay.js +2 -2
- package/dist/stream-relay.js.map +1 -1
- package/package.json +2 -2
package/dist/cli.js
ADDED
|
@@ -0,0 +1,467 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* cc-channel-octo CLI — a process supervisor for the gateway.
|
|
4
|
+
*
|
|
5
|
+
* The gateway itself (`index.ts`) only runs in the foreground. This thin
|
|
6
|
+
* supervisor backgrounds it, tracks a single process-wide PID file, and stops
|
|
7
|
+
* it gracefully (SIGTERM, then SIGKILL on timeout). It does NOT replace the
|
|
8
|
+
* per-bot `gateway.lock` (which prevents two processes serving the same bot) —
|
|
9
|
+
* the PID file lives at the baseDir root, a sibling of every bot subtree.
|
|
10
|
+
*
|
|
11
|
+
* cc-channel-octo start [--foreground]
|
|
12
|
+
* cc-channel-octo stop [--timeout=<seconds>]
|
|
13
|
+
* cc-channel-octo restart
|
|
14
|
+
* cc-channel-octo status
|
|
15
|
+
*
|
|
16
|
+
* POSIX only (macOS/Linux): stop relies on SIGTERM/SIGKILL. On Windows, run the
|
|
17
|
+
* gateway under a service manager instead — Node has no SIGTERM semantics there.
|
|
18
|
+
*/
|
|
19
|
+
import { spawn, execFileSync } from 'node:child_process';
|
|
20
|
+
import { openSync, readFileSync, writeFileSync, existsSync, mkdirSync, unlinkSync, realpathSync } from 'node:fs';
|
|
21
|
+
import { dirname, join } from 'node:path';
|
|
22
|
+
import { fileURLToPath, pathToFileURL } from 'node:url';
|
|
23
|
+
import { setTimeout as sleep } from 'node:timers/promises';
|
|
24
|
+
import { DEFAULT_CONFIG_PATH } from './config.js';
|
|
25
|
+
import { configure } from './configure.js';
|
|
26
|
+
/**
|
|
27
|
+
* Resolve the supervisor's fixed paths. baseDir defaults to the directory of
|
|
28
|
+
* the global config (`~/.cc-channel-octo`); tests inject a temp dir. indexEntry
|
|
29
|
+
* is the compiled gateway entrypoint, a sibling of this file.
|
|
30
|
+
*/
|
|
31
|
+
export function resolveSupervisorPaths(baseDir) {
|
|
32
|
+
const base = baseDir ?? dirname(DEFAULT_CONFIG_PATH);
|
|
33
|
+
return {
|
|
34
|
+
baseDir: base,
|
|
35
|
+
pidFile: join(base, 'cc-channel-octo.pid'),
|
|
36
|
+
logFile: join(base, 'logs', 'gateway.log'),
|
|
37
|
+
indexEntry: fileURLToPath(new URL('./index.js', import.meta.url)),
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Extract a package version from raw package.json text. Returns 'unknown'
|
|
42
|
+
* rather than throwing if the text is malformed or has no non-empty string
|
|
43
|
+
* `version`. Pure (no I/O) so the fallback paths are unit-testable.
|
|
44
|
+
*/
|
|
45
|
+
export function parseVersion(raw) {
|
|
46
|
+
try {
|
|
47
|
+
const pkg = JSON.parse(raw);
|
|
48
|
+
if (pkg && typeof pkg === 'object' && 'version' in pkg) {
|
|
49
|
+
const v = pkg.version;
|
|
50
|
+
if (typeof v === 'string' && v.length > 0)
|
|
51
|
+
return v;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
catch {
|
|
55
|
+
/* fall through to 'unknown' */
|
|
56
|
+
}
|
|
57
|
+
return 'unknown';
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* The package version, read at runtime from package.json — which lives at the
|
|
61
|
+
* package root, one level up from this module in both src/ (src/cli.ts) and the
|
|
62
|
+
* compiled output (dist/cli.js). Returns 'unknown' if the file can't be read.
|
|
63
|
+
*/
|
|
64
|
+
export function readVersion() {
|
|
65
|
+
try {
|
|
66
|
+
return parseVersion(readFileSync(new URL('../package.json', import.meta.url), 'utf-8'));
|
|
67
|
+
}
|
|
68
|
+
catch {
|
|
69
|
+
return 'unknown';
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
export function parseArgs(argv) {
|
|
73
|
+
const [cmd = '', ...rest] = argv;
|
|
74
|
+
let foreground = false;
|
|
75
|
+
let timeoutSec = 10;
|
|
76
|
+
let version;
|
|
77
|
+
let gatewayUrl;
|
|
78
|
+
let apiKey;
|
|
79
|
+
let model;
|
|
80
|
+
let apiUrl;
|
|
81
|
+
for (let i = 0; i < rest.length; i++) {
|
|
82
|
+
const a = rest[i];
|
|
83
|
+
if (a === '--foreground' || a === '-f') {
|
|
84
|
+
foreground = true;
|
|
85
|
+
}
|
|
86
|
+
else if (a.startsWith('--timeout=')) {
|
|
87
|
+
const n = Number.parseInt(a.slice('--timeout='.length), 10);
|
|
88
|
+
if (Number.isFinite(n) && n > 0)
|
|
89
|
+
timeoutSec = n;
|
|
90
|
+
}
|
|
91
|
+
else if (a === '--gateway-url') {
|
|
92
|
+
const next = rest[++i];
|
|
93
|
+
if (next === undefined || next.startsWith('--')) {
|
|
94
|
+
throw new Error('configure: --gateway-url requires a value');
|
|
95
|
+
}
|
|
96
|
+
gatewayUrl = next;
|
|
97
|
+
}
|
|
98
|
+
else if (a.startsWith('--gateway-url=')) {
|
|
99
|
+
gatewayUrl = a.slice('--gateway-url='.length);
|
|
100
|
+
}
|
|
101
|
+
else if (a === '--api-key') {
|
|
102
|
+
const next = rest[++i];
|
|
103
|
+
if (next === undefined || next.startsWith('--')) {
|
|
104
|
+
throw new Error('configure: --api-key requires a value');
|
|
105
|
+
}
|
|
106
|
+
apiKey = next;
|
|
107
|
+
}
|
|
108
|
+
else if (a.startsWith('--api-key=')) {
|
|
109
|
+
apiKey = a.slice('--api-key='.length);
|
|
110
|
+
}
|
|
111
|
+
else if (a === '--model') {
|
|
112
|
+
const next = rest[++i];
|
|
113
|
+
if (next === undefined || next.startsWith('--')) {
|
|
114
|
+
throw new Error('configure: --model requires a value');
|
|
115
|
+
}
|
|
116
|
+
model = next;
|
|
117
|
+
}
|
|
118
|
+
else if (a.startsWith('--model=')) {
|
|
119
|
+
model = a.slice('--model='.length);
|
|
120
|
+
}
|
|
121
|
+
else if (a === '--api-url') {
|
|
122
|
+
const next = rest[++i];
|
|
123
|
+
if (next === undefined || next.startsWith('--')) {
|
|
124
|
+
throw new Error('configure: --api-url requires a value');
|
|
125
|
+
}
|
|
126
|
+
apiUrl = next;
|
|
127
|
+
}
|
|
128
|
+
else if (a.startsWith('--api-url=')) {
|
|
129
|
+
apiUrl = a.slice('--api-url='.length);
|
|
130
|
+
}
|
|
131
|
+
else if (!a.startsWith('-') && version === undefined) {
|
|
132
|
+
version = a;
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
return { cmd, foreground, timeoutMs: timeoutSec * 1000, version, gatewayUrl, apiKey, model, apiUrl };
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* Liveness probe via signal 0. EPERM means the process exists but is owned by
|
|
139
|
+
* another user — still "alive" for our purposes; ESRCH means it's gone.
|
|
140
|
+
*/
|
|
141
|
+
export function isAlive(pid) {
|
|
142
|
+
if (!Number.isInteger(pid) || pid <= 0)
|
|
143
|
+
return false;
|
|
144
|
+
try {
|
|
145
|
+
process.kill(pid, 0);
|
|
146
|
+
return true;
|
|
147
|
+
}
|
|
148
|
+
catch (err) {
|
|
149
|
+
return err.code === 'EPERM';
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
export function procStartTime(pid) {
|
|
153
|
+
if (!Number.isInteger(pid) || pid <= 0)
|
|
154
|
+
return null;
|
|
155
|
+
try {
|
|
156
|
+
const out = execFileSync('ps', ['-p', String(pid), '-o', 'lstart='], {
|
|
157
|
+
encoding: 'utf-8',
|
|
158
|
+
stdio: ['ignore', 'pipe', 'ignore'],
|
|
159
|
+
}).trim();
|
|
160
|
+
return out.length > 0 ? out : null;
|
|
161
|
+
}
|
|
162
|
+
catch {
|
|
163
|
+
return null;
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
167
|
+
* Read the PID record. Accepts the current JSON form (`{"pid":N,"id":"…"}`) and
|
|
168
|
+
* the legacy bare-integer form (written by pre-ownership-token releases), which
|
|
169
|
+
* yields a null identity so callers fall back to liveness-only handling.
|
|
170
|
+
*/
|
|
171
|
+
export function readPidRecord(pidFile) {
|
|
172
|
+
if (!existsSync(pidFile))
|
|
173
|
+
return null;
|
|
174
|
+
const raw = readFileSync(pidFile, 'utf-8').trim();
|
|
175
|
+
if (!raw)
|
|
176
|
+
return null;
|
|
177
|
+
if (/^\d+$/.test(raw)) {
|
|
178
|
+
const pid = Number.parseInt(raw, 10);
|
|
179
|
+
return Number.isInteger(pid) && pid > 0 ? { pid, id: null } : null;
|
|
180
|
+
}
|
|
181
|
+
try {
|
|
182
|
+
const parsed = JSON.parse(raw);
|
|
183
|
+
if (parsed && typeof parsed === 'object' && 'pid' in parsed) {
|
|
184
|
+
const pidVal = parsed.pid;
|
|
185
|
+
const idVal = parsed.id;
|
|
186
|
+
if (typeof pidVal === 'number' && Number.isInteger(pidVal) && pidVal > 0) {
|
|
187
|
+
return { pid: pidVal, id: typeof idVal === 'string' ? idVal : null };
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
catch {
|
|
192
|
+
/* fall through to null */
|
|
193
|
+
}
|
|
194
|
+
return null;
|
|
195
|
+
}
|
|
196
|
+
/** The numeric PID from the file (no ownership check), or null. */
|
|
197
|
+
export function readPid(pidFile) {
|
|
198
|
+
return readPidRecord(pidFile)?.pid ?? null;
|
|
199
|
+
}
|
|
200
|
+
export function writePid(pidFile, pid, id) {
|
|
201
|
+
writeFileSync(pidFile, `${JSON.stringify({ pid, id })}\n`, { mode: 0o600 });
|
|
202
|
+
}
|
|
203
|
+
export function removePid(pidFile) {
|
|
204
|
+
try {
|
|
205
|
+
unlinkSync(pidFile);
|
|
206
|
+
}
|
|
207
|
+
catch {
|
|
208
|
+
/* already gone — fine */
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
/**
|
|
212
|
+
* PID of the running gateway we own, or null. A process counts as ours only if
|
|
213
|
+
* it is alive AND its current OS identity matches the one recorded at start —
|
|
214
|
+
* the guard that stops `stop`/`restart`/`upgrade` from signaling a process that
|
|
215
|
+
* merely inherited the PID after an unclean crash + reuse. Identity mismatch
|
|
216
|
+
* means the file is stale (PID recycled), so it is removed. Legacy files (no
|
|
217
|
+
* recorded identity) and unreadable live identities fall back to liveness only,
|
|
218
|
+
* matching the pre-ownership-token behavior rather than refusing to stop.
|
|
219
|
+
*/
|
|
220
|
+
export function resolveOwnedPid(paths, procId) {
|
|
221
|
+
const rec = readPidRecord(paths.pidFile);
|
|
222
|
+
if (rec === null)
|
|
223
|
+
return null;
|
|
224
|
+
if (!isAlive(rec.pid)) {
|
|
225
|
+
removePid(paths.pidFile);
|
|
226
|
+
return null;
|
|
227
|
+
}
|
|
228
|
+
if (rec.id === null)
|
|
229
|
+
return rec.pid; // legacy file — nothing to verify against
|
|
230
|
+
const liveId = procId(rec.pid);
|
|
231
|
+
if (liveId === null)
|
|
232
|
+
return rec.pid; // identity unreadable — don't refuse to act
|
|
233
|
+
if (liveId === rec.id)
|
|
234
|
+
return rec.pid; // verified ours
|
|
235
|
+
removePid(paths.pidFile); // PID was recycled by an unrelated process
|
|
236
|
+
return null;
|
|
237
|
+
}
|
|
238
|
+
async function cmdStart(paths, foreground, procId) {
|
|
239
|
+
if (foreground) {
|
|
240
|
+
const child = spawn(process.execPath, [paths.indexEntry], {
|
|
241
|
+
stdio: 'inherit',
|
|
242
|
+
env: process.env,
|
|
243
|
+
});
|
|
244
|
+
return new Promise((resolve) => {
|
|
245
|
+
child.on('exit', (code) => resolve(code ?? 0));
|
|
246
|
+
});
|
|
247
|
+
}
|
|
248
|
+
const running = resolveOwnedPid(paths, procId);
|
|
249
|
+
if (running !== null) {
|
|
250
|
+
console.log(`cc-channel-octo: already running (pid ${running})`);
|
|
251
|
+
return 0;
|
|
252
|
+
}
|
|
253
|
+
mkdirSync(dirname(paths.logFile), { recursive: true });
|
|
254
|
+
const fd = openSync(paths.logFile, 'a');
|
|
255
|
+
const child = spawn(process.execPath, [paths.indexEntry], {
|
|
256
|
+
detached: true,
|
|
257
|
+
stdio: ['ignore', fd, fd],
|
|
258
|
+
env: process.env,
|
|
259
|
+
});
|
|
260
|
+
child.unref();
|
|
261
|
+
if (child.pid === undefined) {
|
|
262
|
+
console.error('cc-channel-octo: failed to spawn gateway');
|
|
263
|
+
return 1;
|
|
264
|
+
}
|
|
265
|
+
// Record the child's start-time identity alongside its PID so a later
|
|
266
|
+
// stop/restart can confirm it's still our process and not a PID-reuse victim.
|
|
267
|
+
writePid(paths.pidFile, child.pid, procId(child.pid));
|
|
268
|
+
// Confirm it didn't exit immediately (bad config, taken lock, …).
|
|
269
|
+
await sleep(400);
|
|
270
|
+
if (!isAlive(child.pid)) {
|
|
271
|
+
removePid(paths.pidFile);
|
|
272
|
+
console.error(`cc-channel-octo: gateway exited on startup; see ${paths.logFile}`);
|
|
273
|
+
return 1;
|
|
274
|
+
}
|
|
275
|
+
console.log(`cc-channel-octo: started (pid ${child.pid}), logs at ${paths.logFile}`);
|
|
276
|
+
return 0;
|
|
277
|
+
}
|
|
278
|
+
async function cmdStop(paths, timeoutMs, procId) {
|
|
279
|
+
const pid = resolveOwnedPid(paths, procId);
|
|
280
|
+
if (pid === null) {
|
|
281
|
+
// resolveOwnedPid already removed a stale/recycled file.
|
|
282
|
+
console.log('cc-channel-octo: not running');
|
|
283
|
+
return 0;
|
|
284
|
+
}
|
|
285
|
+
try {
|
|
286
|
+
process.kill(pid, 'SIGTERM');
|
|
287
|
+
}
|
|
288
|
+
catch {
|
|
289
|
+
removePid(paths.pidFile);
|
|
290
|
+
console.log('cc-channel-octo: not running');
|
|
291
|
+
return 0;
|
|
292
|
+
}
|
|
293
|
+
const deadline = Date.now() + timeoutMs;
|
|
294
|
+
while (Date.now() < deadline) {
|
|
295
|
+
if (!isAlive(pid)) {
|
|
296
|
+
removePid(paths.pidFile);
|
|
297
|
+
console.log(`cc-channel-octo: stopped (pid ${pid})`);
|
|
298
|
+
return 0;
|
|
299
|
+
}
|
|
300
|
+
await sleep(200);
|
|
301
|
+
}
|
|
302
|
+
try {
|
|
303
|
+
process.kill(pid, 'SIGKILL');
|
|
304
|
+
}
|
|
305
|
+
catch {
|
|
306
|
+
/* exited between the last check and now — fine */
|
|
307
|
+
}
|
|
308
|
+
removePid(paths.pidFile);
|
|
309
|
+
console.log(`cc-channel-octo: force-killed (pid ${pid}) after ${timeoutMs / 1000}s`);
|
|
310
|
+
return 0;
|
|
311
|
+
}
|
|
312
|
+
function cmdStatus(paths, procId) {
|
|
313
|
+
const pid = resolveOwnedPid(paths, procId);
|
|
314
|
+
if (pid !== null) {
|
|
315
|
+
console.log(`cc-channel-octo: running (pid ${pid}), logs at ${paths.logFile}`);
|
|
316
|
+
}
|
|
317
|
+
else {
|
|
318
|
+
console.log('cc-channel-octo: stopped');
|
|
319
|
+
}
|
|
320
|
+
return 0;
|
|
321
|
+
}
|
|
322
|
+
/** npm package name installed globally for the gateway. */
|
|
323
|
+
const NPM_PKG = '@mininglamp-oss/cc-channel-octo';
|
|
324
|
+
/**
|
|
325
|
+
* Semantic-version whitelist. The version reaches us from the daemon → fleet
|
|
326
|
+
* upgrade order (an untrusted boundary) and is interpolated into an npm spec,
|
|
327
|
+
* so reject anything outside `[0-9A-Za-z.-+]` to prevent argument/shell
|
|
328
|
+
* injection even though we spawn npm without a shell.
|
|
329
|
+
*/
|
|
330
|
+
const VERSION_RE = /^[0-9A-Za-z.\-+]+$/;
|
|
331
|
+
/**
|
|
332
|
+
* Build the `npm install -g <pkg>@<version>` argument vector. A blank/omitted
|
|
333
|
+
* version installs `@latest`. Pure (no I/O) so the injection guard is unit
|
|
334
|
+
* testable. Throws on an unsafe version string.
|
|
335
|
+
*/
|
|
336
|
+
export function buildUpgradeArgs(version) {
|
|
337
|
+
const v = version && version.trim() ? version.trim() : 'latest';
|
|
338
|
+
if (v !== 'latest' && !VERSION_RE.test(v)) {
|
|
339
|
+
throw new Error(`unsafe version: ${v}`);
|
|
340
|
+
}
|
|
341
|
+
return ['install', '-g', `${NPM_PKG}@${v}`];
|
|
342
|
+
}
|
|
343
|
+
/**
|
|
344
|
+
* Self-update: `npm install -g @mininglamp-oss/cc-channel-octo@<version>` then
|
|
345
|
+
* restart the gateway so the new code is live. Invoked by the daemon to drive
|
|
346
|
+
* a fleet upgrade order (mirrors openclaw's daemon-driven plugin install).
|
|
347
|
+
*/
|
|
348
|
+
async function cmdUpgrade(paths, timeoutMs, procId, version) {
|
|
349
|
+
let args;
|
|
350
|
+
try {
|
|
351
|
+
args = buildUpgradeArgs(version);
|
|
352
|
+
}
|
|
353
|
+
catch (err) {
|
|
354
|
+
console.error(`cc-channel-octo: ${err.message}`);
|
|
355
|
+
return 2;
|
|
356
|
+
}
|
|
357
|
+
console.log(`cc-channel-octo: upgrading via npm ${args.join(' ')}`);
|
|
358
|
+
const code = await new Promise((resolve) => {
|
|
359
|
+
const child = spawn('npm', args, { stdio: 'inherit', env: process.env });
|
|
360
|
+
child.on('error', (err) => {
|
|
361
|
+
console.error(`cc-channel-octo: failed to spawn npm: ${err.message}`);
|
|
362
|
+
resolve(1);
|
|
363
|
+
});
|
|
364
|
+
child.on('exit', (c) => resolve(c ?? 1));
|
|
365
|
+
});
|
|
366
|
+
if (code !== 0) {
|
|
367
|
+
console.error(`cc-channel-octo: npm install failed (exit ${code})`);
|
|
368
|
+
return code;
|
|
369
|
+
}
|
|
370
|
+
// Restart so the freshly installed code is running.
|
|
371
|
+
await cmdStop(paths, timeoutMs, procId);
|
|
372
|
+
return cmdStart(paths, false, procId);
|
|
373
|
+
}
|
|
374
|
+
function usage() {
|
|
375
|
+
return `cc-channel-octo ${readVersion()} — gateway process supervisor
|
|
376
|
+
|
|
377
|
+
Usage:
|
|
378
|
+
cc-channel-octo start [--foreground] start the gateway in the background
|
|
379
|
+
cc-channel-octo stop [--timeout=<s>] gracefully stop (SIGTERM, then SIGKILL)
|
|
380
|
+
cc-channel-octo restart stop (if running) then start
|
|
381
|
+
cc-channel-octo status show running state
|
|
382
|
+
cc-channel-octo upgrade [<version>] npm install -g the gateway (default latest) then restart
|
|
383
|
+
cc-channel-octo configure --gateway-url <url> [--api-key <key>] [--model <model>] [--api-url <octo-server-url>] write LLM gateway + key (+ optional model / Octo server url) to config (key also via CC_OCTO_CONFIGURE_API_KEY)
|
|
384
|
+
cc-channel-octo version print the version
|
|
385
|
+
|
|
386
|
+
Paths (under ~/.cc-channel-octo):
|
|
387
|
+
pid : cc-channel-octo.pid
|
|
388
|
+
log : logs/gateway.log
|
|
389
|
+
|
|
390
|
+
POSIX only (macOS/Linux). On Windows, run under a service manager.`;
|
|
391
|
+
}
|
|
392
|
+
export async function run(argv, baseDir, procId = procStartTime) {
|
|
393
|
+
let parsed;
|
|
394
|
+
try {
|
|
395
|
+
parsed = parseArgs(argv);
|
|
396
|
+
}
|
|
397
|
+
catch (err) {
|
|
398
|
+
// Usage error (e.g. a flag missing its value): report it as a usage
|
|
399
|
+
// failure (exit 2), not an unhandled internal error from the top-level catch.
|
|
400
|
+
console.error(`cc-channel-octo: ${err.message}`);
|
|
401
|
+
return 2;
|
|
402
|
+
}
|
|
403
|
+
const { cmd, foreground, timeoutMs, version, gatewayUrl, apiKey, model, apiUrl } = parsed;
|
|
404
|
+
const paths = resolveSupervisorPaths(baseDir);
|
|
405
|
+
switch (cmd) {
|
|
406
|
+
case 'start':
|
|
407
|
+
return cmdStart(paths, foreground, procId);
|
|
408
|
+
case 'stop':
|
|
409
|
+
return cmdStop(paths, timeoutMs, procId);
|
|
410
|
+
case 'restart':
|
|
411
|
+
await cmdStop(paths, timeoutMs, procId);
|
|
412
|
+
return cmdStart(paths, false, procId);
|
|
413
|
+
case 'status':
|
|
414
|
+
return cmdStatus(paths, procId);
|
|
415
|
+
case 'upgrade':
|
|
416
|
+
return cmdUpgrade(paths, timeoutMs, procId, version);
|
|
417
|
+
case 'configure': {
|
|
418
|
+
const resolvedApiKey = apiKey ?? process.env.CC_OCTO_CONFIGURE_API_KEY ?? '';
|
|
419
|
+
const configPath = baseDir ? join(baseDir, 'config.json') : undefined;
|
|
420
|
+
try {
|
|
421
|
+
configure(gatewayUrl ?? '', resolvedApiKey, configPath, { model, apiUrl });
|
|
422
|
+
console.log('cc-channel-octo: configured gateway + api key');
|
|
423
|
+
return 0;
|
|
424
|
+
}
|
|
425
|
+
catch (err) {
|
|
426
|
+
console.error(`cc-channel-octo: ${err.message}`);
|
|
427
|
+
return 2;
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
case 'version':
|
|
431
|
+
case '--version':
|
|
432
|
+
case '-v':
|
|
433
|
+
console.log(readVersion());
|
|
434
|
+
return 0;
|
|
435
|
+
case 'help':
|
|
436
|
+
case '--help':
|
|
437
|
+
case '-h':
|
|
438
|
+
console.log(usage());
|
|
439
|
+
return 0;
|
|
440
|
+
case '':
|
|
441
|
+
// Backward compat: bare `cc-channel-octo` (e.g. `npx cc-channel-octo`)
|
|
442
|
+
// runs the gateway in the foreground, matching the pre-supervisor bin
|
|
443
|
+
// (`dist/index.js`) behavior documented in README/CHANGELOG. Daemon-style
|
|
444
|
+
// process management is opt-in via the `start`/`stop`/`restart`/`status`
|
|
445
|
+
// subcommands.
|
|
446
|
+
return cmdStart(paths, true, procId);
|
|
447
|
+
default:
|
|
448
|
+
console.error(`cc-channel-octo: unknown command '${cmd}'\n`);
|
|
449
|
+
console.error(usage());
|
|
450
|
+
return 2;
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
// Run only when invoked as a script (production / linked bin), not when
|
|
454
|
+
// imported (tests). When called via the `bin` symlink, Node resolves
|
|
455
|
+
// import.meta.url to the real file but leaves process.argv[1] as the symlink
|
|
456
|
+
// path — so resolve argv[1] to its realpath before comparing, or the linked
|
|
457
|
+
// command would silently no-op.
|
|
458
|
+
const entrypoint = process.argv[1];
|
|
459
|
+
if (entrypoint && import.meta.url === pathToFileURL(realpathSync(entrypoint)).href) {
|
|
460
|
+
run(process.argv.slice(2))
|
|
461
|
+
.then((code) => process.exit(code))
|
|
462
|
+
.catch((err) => {
|
|
463
|
+
console.error('cc-channel-octo:', String(err));
|
|
464
|
+
process.exit(1);
|
|
465
|
+
});
|
|
466
|
+
}
|
|
467
|
+
//# sourceMappingURL=cli.js.map
|
package/dist/cli.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cli.js","sourceRoot":"","sources":["../src/cli.ts"],"names":[],"mappings":";AACA;;;;;;;;;;;;;;;;GAgBG;AAEH,OAAO,EAAE,KAAK,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AACzD,OAAO,EAAE,QAAQ,EAAE,YAAY,EAAE,aAAa,EAA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,IAAI,CAAC,CAAC,CAAC,CAAC;IAClB,CAAC,CAAC,CAAC;AACP,CAAC"}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/** Minimal manager surface the watcher needs (eases testing). */
|
|
2
|
+
export interface Reconcilable {
|
|
3
|
+
runningKeys(): string[];
|
|
4
|
+
addBot(configId: string): Promise<void>;
|
|
5
|
+
removeBot(configId: string): Promise<void>;
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* Reconcile the running set toward `desiredConfigIds` once. Removes first, then
|
|
9
|
+
* adds, so a config that swaps a bot for another with an overlapping resource
|
|
10
|
+
* (lock/dir) frees it before the replacement claims it. Add/remove failures are
|
|
11
|
+
* logged and swallowed so one bad bot doesn't abort the whole reconcile; the
|
|
12
|
+
* BotManager queue keeps the set consistent. Pure-ish (no fs) for unit testing.
|
|
13
|
+
*
|
|
14
|
+
* `isStale` is checked before each add/remove: a newer config event makes the
|
|
15
|
+
* in-flight reconcile abandon its remaining (now outdated) actions, so a bot
|
|
16
|
+
* removed by a later edit is never started by an earlier, slower reconcile
|
|
17
|
+
* (plan C6 generation guard).
|
|
18
|
+
*/
|
|
19
|
+
export declare function reconcile(manager: Reconcilable, desiredConfigIds: readonly string[], log?: (msg: string) => void, isStale?: () => boolean): Promise<void>;
|
|
20
|
+
export interface WatcherHandle {
|
|
21
|
+
/** Force an immediate reconcile (bypasses debounce). For tests / initial sync. */
|
|
22
|
+
applyNow(): Promise<void>;
|
|
23
|
+
close(): void;
|
|
24
|
+
}
|
|
25
|
+
export interface WatchOptions {
|
|
26
|
+
/** Absolute path to the global config.json. */
|
|
27
|
+
configPath: string;
|
|
28
|
+
manager: Reconcilable;
|
|
29
|
+
/**
|
|
30
|
+
* Produce the desired configId list from disk. MUST throw on any invalid
|
|
31
|
+
* config so the watcher can keep the current set (plan D). Typically wraps
|
|
32
|
+
* loadConfig + resolveBotConfigs and maps to `bots[].id`.
|
|
33
|
+
*/
|
|
34
|
+
loadDesired: () => readonly string[];
|
|
35
|
+
/** Debounce window in ms (coalesce a burst of writes). Default 200. */
|
|
36
|
+
debounceMs?: number;
|
|
37
|
+
log?: (msg: string) => void;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Start watching `configPath`'s directory and reconcile on change.
|
|
41
|
+
*
|
|
42
|
+
* Serialization & staleness: a single `chain` promise serializes apply runs,
|
|
43
|
+
* and each run re-reads the latest config at execution time, so a burst of
|
|
44
|
+
* events collapses to "apply the newest state" rather than replaying each
|
|
45
|
+
* intermediate edit. The BotManager's own queue further serializes the
|
|
46
|
+
* resulting add/remove calls.
|
|
47
|
+
*/
|
|
48
|
+
export declare function watchConfig(opts: WatchOptions): WatcherHandle;
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Config watcher — drives BotManager from config.json changes (#157).
|
|
3
|
+
*
|
|
4
|
+
* Watches the global config's directory (not the file directly: an atomic
|
|
5
|
+
* temp+rename swaps the inode, and a file-targeted fs.watch would stop firing
|
|
6
|
+
* after the first rename). On any change it schedules a debounced, serialized
|
|
7
|
+
* `applyLatestConfig`, which RE-READS the latest config inside the task (never
|
|
8
|
+
* a precomputed diff — plan C6) and reconciles the running set toward it.
|
|
9
|
+
*
|
|
10
|
+
* Robustness (plan D): the desired set is produced by a single `loadDesired`
|
|
11
|
+
* call; if it throws for ANY reason (half-written file, JSON error, missing
|
|
12
|
+
* token, duplicate id, unsafe apiUrl, broken per-bot config), the current
|
|
13
|
+
* running set is left untouched — a bad edit never tears down healthy bots.
|
|
14
|
+
*/
|
|
15
|
+
import { watch } from 'node:fs';
|
|
16
|
+
import { dirname, basename } from 'node:path';
|
|
17
|
+
import { diffBotSets } from './bot-manager.js';
|
|
18
|
+
/**
|
|
19
|
+
* Reconcile the running set toward `desiredConfigIds` once. Removes first, then
|
|
20
|
+
* adds, so a config that swaps a bot for another with an overlapping resource
|
|
21
|
+
* (lock/dir) frees it before the replacement claims it. Add/remove failures are
|
|
22
|
+
* logged and swallowed so one bad bot doesn't abort the whole reconcile; the
|
|
23
|
+
* BotManager queue keeps the set consistent. Pure-ish (no fs) for unit testing.
|
|
24
|
+
*
|
|
25
|
+
* `isStale` is checked before each add/remove: a newer config event makes the
|
|
26
|
+
* in-flight reconcile abandon its remaining (now outdated) actions, so a bot
|
|
27
|
+
* removed by a later edit is never started by an earlier, slower reconcile
|
|
28
|
+
* (plan C6 generation guard).
|
|
29
|
+
*/
|
|
30
|
+
export async function reconcile(manager, desiredConfigIds, log = () => { }, isStale = () => false) {
|
|
31
|
+
const { toAdd, toRemove } = diffBotSets(desiredConfigIds, manager.runningKeys());
|
|
32
|
+
for (const id of toRemove) {
|
|
33
|
+
if (isStale()) {
|
|
34
|
+
log(`[hot-reload] reconcile superseded by a newer config, stopping`);
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
try {
|
|
38
|
+
await manager.removeBot(id);
|
|
39
|
+
log(`[hot-reload] removed bot ${id}`);
|
|
40
|
+
}
|
|
41
|
+
catch (err) {
|
|
42
|
+
log(`[hot-reload] removeBot ${id} failed: ${errMsg(err)}`);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
for (const id of toAdd) {
|
|
46
|
+
if (isStale()) {
|
|
47
|
+
log(`[hot-reload] reconcile superseded by a newer config, stopping`);
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
try {
|
|
51
|
+
await manager.addBot(id);
|
|
52
|
+
log(`[hot-reload] added bot ${id}`);
|
|
53
|
+
}
|
|
54
|
+
catch (err) {
|
|
55
|
+
log(`[hot-reload] addBot ${id} failed: ${errMsg(err)}`);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
function errMsg(err) {
|
|
60
|
+
return err instanceof Error ? err.message : String(err);
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Start watching `configPath`'s directory and reconcile on change.
|
|
64
|
+
*
|
|
65
|
+
* Serialization & staleness: a single `chain` promise serializes apply runs,
|
|
66
|
+
* and each run re-reads the latest config at execution time, so a burst of
|
|
67
|
+
* events collapses to "apply the newest state" rather than replaying each
|
|
68
|
+
* intermediate edit. The BotManager's own queue further serializes the
|
|
69
|
+
* resulting add/remove calls.
|
|
70
|
+
*/
|
|
71
|
+
export function watchConfig(opts) {
|
|
72
|
+
const { configPath, manager, loadDesired } = opts;
|
|
73
|
+
const debounceMs = opts.debounceMs ?? 200;
|
|
74
|
+
const log = opts.log ?? (() => { });
|
|
75
|
+
const dir = dirname(configPath);
|
|
76
|
+
const file = basename(configPath);
|
|
77
|
+
let chain = Promise.resolve();
|
|
78
|
+
let timer;
|
|
79
|
+
let closed = false;
|
|
80
|
+
// Generation guard (plan C6). `latestGen` is bumped the moment a file event is
|
|
81
|
+
// observed (in schedule), NOT when the debounced apply finally runs — so an
|
|
82
|
+
// in-flight reconcile is invalidated immediately on a new event, even during
|
|
83
|
+
// the debounce window, instead of continuing to add/connect a bot a later
|
|
84
|
+
// edit already removed. Each apply captures the gen current when it starts and
|
|
85
|
+
// bails (isStale) as soon as a newer event has bumped past it.
|
|
86
|
+
let latestGen = 0;
|
|
87
|
+
const apply = () => {
|
|
88
|
+
const myGen = latestGen;
|
|
89
|
+
// Re-read latest desired set INSIDE the serialized task (plan C6). Any
|
|
90
|
+
// failure (half-write / invalid config) leaves the running set untouched.
|
|
91
|
+
chain = chain.then(async () => {
|
|
92
|
+
if (closed)
|
|
93
|
+
return;
|
|
94
|
+
let desired;
|
|
95
|
+
try {
|
|
96
|
+
desired = loadDesired();
|
|
97
|
+
}
|
|
98
|
+
catch (err) {
|
|
99
|
+
log(`[hot-reload] config invalid, keeping current bots: ${errMsg(err)}`);
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
await reconcile(manager, desired, log, () => closed || myGen !== latestGen);
|
|
103
|
+
});
|
|
104
|
+
return chain;
|
|
105
|
+
};
|
|
106
|
+
const schedule = () => {
|
|
107
|
+
if (closed)
|
|
108
|
+
return;
|
|
109
|
+
// Invalidate any in-flight reconcile right now (not at debounce expiry): a
|
|
110
|
+
// slow reconcile must see this newer event before it acts on stale desired.
|
|
111
|
+
latestGen++;
|
|
112
|
+
if (timer)
|
|
113
|
+
clearTimeout(timer);
|
|
114
|
+
timer = setTimeout(() => {
|
|
115
|
+
timer = undefined;
|
|
116
|
+
void apply();
|
|
117
|
+
}, debounceMs);
|
|
118
|
+
timer.unref?.();
|
|
119
|
+
};
|
|
120
|
+
let watcher;
|
|
121
|
+
try {
|
|
122
|
+
watcher = watch(dir, (_event, changed) => {
|
|
123
|
+
// Only react to our config file (the dir may hold per-bot subdirs etc.).
|
|
124
|
+
// changed is null on some platforms — be permissive and reconcile then.
|
|
125
|
+
if (changed === null || changed === file)
|
|
126
|
+
schedule();
|
|
127
|
+
});
|
|
128
|
+
watcher.on('error', (err) => log(`[hot-reload] watcher error: ${errMsg(err)}`));
|
|
129
|
+
}
|
|
130
|
+
catch (err) {
|
|
131
|
+
log(`[hot-reload] failed to start watcher on ${dir}: ${errMsg(err)}`);
|
|
132
|
+
}
|
|
133
|
+
return {
|
|
134
|
+
applyNow: apply,
|
|
135
|
+
close: () => {
|
|
136
|
+
closed = true;
|
|
137
|
+
if (timer)
|
|
138
|
+
clearTimeout(timer);
|
|
139
|
+
watcher?.close();
|
|
140
|
+
},
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
//# sourceMappingURL=config-watcher.js.map
|