refactor: share orchestrator cli core
This commit is contained in:
@@ -1,212 +1,5 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
import path from 'node:path';
|
||||
import process from 'node:process';
|
||||
import { spawnSync } from 'node:child_process';
|
||||
|
||||
const ROOT_DIR = path.resolve(import.meta.dirname, '..');
|
||||
const DEFAULT_STATE_PATH = path.join(ROOT_DIR, 'memory', 'watchdog-state.json');
|
||||
const DEFAULT_EVIDENCE_DIR = path.join(ROOT_DIR, 'state', 'long-task-watchdog');
|
||||
const DEFAULT_EVENT_DIR = path.join(ROOT_DIR, 'state', 'long-task-watchdog-events');
|
||||
const DEFAULT_QUEUE_DIR = path.join(ROOT_DIR, 'state', 'operator-notify-queue');
|
||||
const DEFAULT_SPOOL_DIR = path.join(ROOT_DIR, 'state', 'operator-notify-dispatch-spool');
|
||||
const DEFAULT_RECEIPT_DIR = path.join(ROOT_DIR, 'state', 'operator-notify-bridge-receipts');
|
||||
const DEFAULT_WATCHDOG_SCRIPT = path.join(ROOT_DIR, 'scripts', 'long_task_watchdog.mjs');
|
||||
const DEFAULT_DISPATCHER_SCRIPT = path.join(ROOT_DIR, 'scripts', 'operator_notify_dispatcher.mjs');
|
||||
const DEFAULT_SUPERVISOR_SCRIPT = path.join(ROOT_DIR, 'scripts', 'operator_notify_bridge_supervisor.mjs');
|
||||
|
||||
function parseArgs(argv) {
|
||||
const args = {
|
||||
state: DEFAULT_STATE_PATH,
|
||||
evidenceDir: DEFAULT_EVIDENCE_DIR,
|
||||
eventDir: DEFAULT_EVENT_DIR,
|
||||
queueDir: DEFAULT_QUEUE_DIR,
|
||||
spoolDir: DEFAULT_SPOOL_DIR,
|
||||
receiptDir: DEFAULT_RECEIPT_DIR,
|
||||
watchdogScript: DEFAULT_WATCHDOG_SCRIPT,
|
||||
dispatcherScript: DEFAULT_DISPATCHER_SCRIPT,
|
||||
supervisorScript: DEFAULT_SUPERVISOR_SCRIPT,
|
||||
senderCommand: null,
|
||||
senderMode: null,
|
||||
openclawBin: 'openclaw',
|
||||
now: null,
|
||||
compact: false,
|
||||
writeState: false,
|
||||
claim: false,
|
||||
dryRun: false,
|
||||
help: false,
|
||||
};
|
||||
|
||||
for (let i = 0; i < argv.length; i += 1) {
|
||||
const token = argv[i];
|
||||
if (token === '--compact') { args.compact = true; continue; }
|
||||
if (token === '--write-state') { args.writeState = true; continue; }
|
||||
if (token === '--claim') { args.claim = true; continue; }
|
||||
if (token === '--dry-run') { args.dryRun = true; continue; }
|
||||
if (token === '--help' || token === '-h') { args.help = true; continue; }
|
||||
|
||||
const pairs = [
|
||||
['--state', 'state'],
|
||||
['--evidence-dir', 'evidenceDir'],
|
||||
['--event-dir', 'eventDir'],
|
||||
['--queue-dir', 'queueDir'],
|
||||
['--spool-dir', 'spoolDir'],
|
||||
['--receipt-dir', 'receiptDir'],
|
||||
['--watchdog-script', 'watchdogScript'],
|
||||
['--dispatcher-script', 'dispatcherScript'],
|
||||
['--supervisor-script', 'supervisorScript'],
|
||||
['--sender-command', 'senderCommand'],
|
||||
['--sender-mode', 'senderMode'],
|
||||
['--openclaw-bin', 'openclawBin'],
|
||||
['--now', 'now'],
|
||||
];
|
||||
|
||||
let matched = false;
|
||||
for (const [flag, key] of pairs) {
|
||||
if (token === flag) {
|
||||
args[key] = argv[i + 1] ?? args[key];
|
||||
i += 1;
|
||||
matched = true;
|
||||
break;
|
||||
}
|
||||
if (token.startsWith(`${flag}=`)) {
|
||||
args[key] = token.slice(flag.length + 1) || args[key];
|
||||
matched = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (matched) continue;
|
||||
}
|
||||
|
||||
return args;
|
||||
}
|
||||
|
||||
function printHelp() {
|
||||
process.stdout.write([
|
||||
'Usage:',
|
||||
' node scripts/watchdog_auto_notify_orchestrator.mjs [--write-state] [--claim] [--dry-run] [--sender-command <shell>] [--sender-mode shim|openclaw-cli] [--openclaw-bin <path>] [--now <iso>] [--compact]',
|
||||
'',
|
||||
'Runs the full watchdog auto-notify chain in order:',
|
||||
' runner -> queue -> dispatcher -> bridge -> sender -> ack|blocked',
|
||||
'',
|
||||
'If --sender-mode is given and --sender-command is omitted, a sender-binding command is constructed automatically.',
|
||||
].join('\n') + '\n');
|
||||
}
|
||||
|
||||
function buildSenderCommand(args) {
|
||||
if (args.senderCommand) return args.senderCommand;
|
||||
if (!args.senderMode) return null;
|
||||
const cmd = [
|
||||
JSON.stringify(process.execPath),
|
||||
JSON.stringify(path.join(ROOT_DIR, 'scripts', 'operator_notify_sender_binding.mjs')),
|
||||
'--mode', JSON.stringify(args.senderMode),
|
||||
'--openclaw-bin', JSON.stringify(args.openclawBin),
|
||||
'--compact',
|
||||
];
|
||||
return cmd.join(' ');
|
||||
}
|
||||
|
||||
function runNodeScript(scriptPath, scriptArgs) {
|
||||
return spawnSync(process.execPath, [scriptPath, ...scriptArgs], {
|
||||
cwd: ROOT_DIR,
|
||||
encoding: 'utf8',
|
||||
});
|
||||
}
|
||||
|
||||
function parseJsonOutput(label, result) {
|
||||
const stdout = result.stdout ?? '';
|
||||
try {
|
||||
return stdout.trim() ? JSON.parse(stdout) : null;
|
||||
} catch (error) {
|
||||
throw new Error(`${label} emitted non-JSON stdout: ${error instanceof Error ? error.message : String(error)}`);
|
||||
}
|
||||
}
|
||||
|
||||
function ensureSuccess(label, result) {
|
||||
if (result.status !== 0) {
|
||||
throw new Error(`${label} failed with status ${result.status ?? 'null'}: ${(result.stderr ?? '').trim() || '(no stderr)'}`);
|
||||
}
|
||||
}
|
||||
|
||||
function main() {
|
||||
const args = parseArgs(process.argv.slice(2));
|
||||
if (args.help) {
|
||||
printHelp();
|
||||
process.exit(0);
|
||||
}
|
||||
|
||||
const senderCommand = buildSenderCommand(args);
|
||||
|
||||
try {
|
||||
const watchdogArgs = [
|
||||
'--state', path.resolve(args.state),
|
||||
'--evidence-dir', path.resolve(args.evidenceDir),
|
||||
'--event-dir', path.resolve(args.eventDir),
|
||||
'--notification-dir', path.resolve(args.queueDir),
|
||||
'--compact',
|
||||
];
|
||||
if (args.writeState) watchdogArgs.push('--write-state');
|
||||
if (args.now) watchdogArgs.push('--now', args.now);
|
||||
const watchdog = runNodeScript(path.resolve(args.watchdogScript), watchdogArgs);
|
||||
ensureSuccess('watchdog runner', watchdog);
|
||||
const watchdogPayload = parseJsonOutput('watchdog runner', watchdog);
|
||||
|
||||
const dispatcherArgs = [
|
||||
'--queue-dir', path.resolve(args.queueDir),
|
||||
'--spool-dir', path.resolve(args.spoolDir),
|
||||
'--compact',
|
||||
];
|
||||
if (args.claim) dispatcherArgs.push('--claim');
|
||||
if (args.now) dispatcherArgs.push('--now', args.now);
|
||||
const dispatcher = runNodeScript(path.resolve(args.dispatcherScript), dispatcherArgs);
|
||||
ensureSuccess('dispatcher', dispatcher);
|
||||
const dispatcherPayload = parseJsonOutput('dispatcher', dispatcher);
|
||||
|
||||
const supervisorArgs = [
|
||||
'--queue-dir', path.resolve(args.queueDir),
|
||||
'--spool-dir', path.resolve(args.spoolDir),
|
||||
'--receipt-dir', path.resolve(args.receiptDir),
|
||||
'--dispatcher-script', path.resolve(args.dispatcherScript),
|
||||
'--compact',
|
||||
];
|
||||
if (args.dryRun) supervisorArgs.push('--dry-run');
|
||||
if (senderCommand) supervisorArgs.push('--sender-command', senderCommand);
|
||||
if (args.now) supervisorArgs.push('--now', args.now);
|
||||
const supervisor = runNodeScript(path.resolve(args.supervisorScript), supervisorArgs);
|
||||
ensureSuccess('bridge supervisor', supervisor);
|
||||
const supervisorPayload = parseJsonOutput('bridge supervisor', supervisor);
|
||||
|
||||
const response = {
|
||||
ok: true,
|
||||
tool: 'watchdog_auto_notify_orchestrator',
|
||||
version: 'mvp-v1',
|
||||
now: args.now ?? null,
|
||||
executionOrder: [
|
||||
'runner',
|
||||
'queue',
|
||||
'dispatcher',
|
||||
'bridge',
|
||||
senderCommand ? 'sender' : 'sender_unconfigured',
|
||||
'ack_or_blocked_or_pending',
|
||||
],
|
||||
orchestration: {
|
||||
script: path.resolve(import.meta.filename),
|
||||
senderCommandConfigured: Boolean(senderCommand),
|
||||
senderMode: args.senderMode ?? null,
|
||||
dryRun: args.dryRun,
|
||||
},
|
||||
result: {
|
||||
watchdog: watchdogPayload?.result ?? null,
|
||||
dispatcher: dispatcherPayload?.result ?? null,
|
||||
supervisor: supervisorPayload?.result ?? null,
|
||||
},
|
||||
};
|
||||
|
||||
process.stdout.write(`${JSON.stringify(response, null, args.compact ? 0 : 2)}\n`);
|
||||
} catch (error) {
|
||||
process.stderr.write(`${error instanceof Error ? error.message : String(error)}\n`);
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
import { main } from '../src/adapters/orchestrator-cli.mjs';
|
||||
|
||||
main();
|
||||
|
||||
Reference in New Issue
Block a user