import path from 'node:path'; import process from 'node:process'; import { runWatchdogAdapter } from './watchdog.mjs'; import { runDispatcherAdapter } from './dispatcher.mjs'; import { runBridgeSupervisorAdapter } from './bridge-supervisor.mjs'; export const DEFAULT_OPENCLAW_BIN = 'openclaw'; export function createDefaultOrchestratorExecutionArgs({ packageRoot }) { return { state: path.join(packageRoot, 'memory', 'watchdog-state.json'), evidenceDir: path.join(packageRoot, 'state', 'long-task-watchdog'), eventDir: path.join(packageRoot, 'state', 'long-task-watchdog-events'), queueDir: path.join(packageRoot, 'state', 'operator-notify-queue'), spoolDir: path.join(packageRoot, 'state', 'operator-notify-dispatch-spool'), receiptDir: path.join(packageRoot, 'state', 'operator-notify-bridge-receipts'), watchdogScript: path.join(packageRoot, 'scripts', 'long_task_watchdog.mjs'), dispatcherScript: path.join(packageRoot, 'scripts', 'operator_notify_dispatcher.mjs'), supervisorScript: path.join(packageRoot, 'scripts', 'operator_notify_bridge_supervisor.mjs'), senderCommand: null, senderMode: null, openclawBin: DEFAULT_OPENCLAW_BIN, now: null, compact: false, writeState: false, claim: false, dryRun: false, help: false, }; } export function buildSenderCommand(args, { senderBindingScript }) { if (args.senderCommand) return args.senderCommand; if (!args.senderMode) return null; const cmd = [ JSON.stringify(process.execPath), JSON.stringify(senderBindingScript), '--mode', JSON.stringify(args.senderMode), '--openclaw-bin', JSON.stringify(args.openclawBin), '--compact', ]; return cmd.join(' '); } export function runOrchestratorExecution(args, { senderBindingScript } = {}) { const senderCommand = buildSenderCommand(args, { senderBindingScript }); const watchdogPayload = runWatchdogAdapter({ scriptPath: path.resolve(args.watchdogScript), state: args.state, evidenceDir: args.evidenceDir, eventDir: args.eventDir, notificationDir: args.queueDir, now: args.now, compact: true, writeState: args.writeState, }); const dispatcherPayload = runDispatcherAdapter({ scriptPath: path.resolve(args.dispatcherScript), queueDir: args.queueDir, spoolDir: args.spoolDir, now: args.now, compact: true, claim: args.claim, }); const supervisorPayload = runBridgeSupervisorAdapter({ scriptPath: path.resolve(args.supervisorScript), queueDir: args.queueDir, spoolDir: args.spoolDir, receiptDir: args.receiptDir, dispatcherScript: path.resolve(args.dispatcherScript), senderCommand, now: args.now, compact: true, dryRun: args.dryRun, }); return { 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: { senderCommandConfigured: Boolean(senderCommand), senderMode: args.senderMode ?? null, dryRun: args.dryRun, }, result: { watchdog: watchdogPayload?.result ?? null, dispatcher: dispatcherPayload?.result ?? null, supervisor: supervisorPayload?.result ?? null, }, }; }