UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

364 lines (363 loc) • 16.5 kB
#!/usr/bin/env node import { t as disableExitUnsafeCompilers } from "./node-exit-safe-compilers-B7V3uapp.mjs"; import { u as isRootHelpInvocation } from "./argv-BGeGfpwR.mjs"; import { r as isForegroundGatewayRunArgv } from "./gateway-run-argv-DFDqy1EF.mjs"; import "./detached-async-context-D4YGqfxh.mjs"; import { t as resolveCliArgvInvocation } from "./argv-invocation-g2c1uOA9.mjs"; import { n as fsSafeEnvInput } from "./fs-safe-env-wxBiM84C.mjs"; import { n as parseCliProfileArgs, t as applyCliProfileEnv } from "./profile-BzWhRtSt.mjs"; import { n as tryRunUpdateAdmissionBeforeStartup, t as isUpdateAdmissionInvocation } from "./run-main-update-admission-Dz08aQyb.mjs"; import { i as normalizeEnv } from "./env-Ca5Je5YP.mjs"; import { i as createGatewayDispatchStartupTrace, n as configureGatewayStartupTraceConsoleFormatting, o as prepareGatewayStartupTraceConsoleFormatting } from "./startup-trace-DQN0c5Oa.mjs"; import { t as isMainModule } from "./is-main-DYSvvM1W.mjs"; import { n as parseCliContainerArgs, r as resolveCliContainerTarget } from "./container-target-71yNSyL1.mjs"; import { t as tryOutputPrecomputedCommandHelp } from "./precomputed-help-CdY_OP_L.mjs"; import { a as isNativeHookRelayArgv, i as isForegroundGmailRunArgv, n as runCliRespawnPlan, o as isTerminalInteractiveRespawnArgv, r as runRespawnChildWithSignalBridge, s as shouldKeepNativeHookRelayInProcess, t as buildCliRespawnPlan } from "./entry.respawn-Bw43jm0m.mjs"; import { t as normalizeWindowsArgv } from "./windows-argv-YUCPtVGt.mjs"; import { r as resolveOpenClawCompileCacheDirectory, t as enableOwnedNodeCompileCache } from "./node-compile-cache-env-CpY9xPJG.mjs"; import { t as attachChildProcessBridge } from "./child-process-bridge-CFJsa4sQ.mjs"; import { t as installDistEsmResolveFastPath } from "./entry.esm-resolve-fast-path-gJ7_FrDl.mjs"; import { t as tryHandleRootVersionFastPath } from "./entry.version-fast-path-DOZ5f9cC.mjs"; import { i as ensureOpenClawExecMarkerOnProcess } from "./openclaw-exec-env-eyUZROLC.mjs"; import { t as installProcessWarningFilter } from "./warning-filter-DPiX2pYu.mjs"; import { i as requestNodeHostLauncherBootstrap, n as isNodeHostLauncherChild, t as getManagedNodeHostStatePath } from "./launcher-client-DNBTHwUk.mjs"; import { r as defaultRuntime } from "./runtime-BRg2NubB.mjs"; import { getCompileCacheDir } from "node:module"; import { existsSync } from "node:fs"; import process from "node:process"; import { fileURLToPath } from "node:url"; import path from "node:path"; import { format as format$1 } from "node:util"; import { spawn } from "node:child_process"; //#region src/entry.compile-cache.ts const COMPILE_CACHE_DISABLED_RESPAWNED_ENV = "OPENCLAW_COMPILE_CACHE_DISABLED_RESPAWNED"; function resolveEntryInstallRoot(entryFile) { const entryDir = path.dirname(entryFile); const entryParent = path.basename(entryDir); return entryParent === "dist" || entryParent === "src" ? path.dirname(entryDir) : entryDir; } function isSourceCheckoutInstallRoot(installRoot) { return existsSync(path.join(installRoot, ".git")) || existsSync(path.join(installRoot, "src", "entry.ts")); } function isNodeCompileCacheDisabled(env) { return env?.NODE_DISABLE_COMPILE_CACHE !== void 0; } function isNodeCompileCacheRequested(env) { return env?.NODE_COMPILE_CACHE !== void 0 && !isNodeCompileCacheDisabled(env); } function shouldEnableOpenClawCompileCache(params) { return !isNodeCompileCacheDisabled(params.env ?? process.env) && !isSourceCheckoutInstallRoot(params.installRoot); } function buildOpenClawCompileCacheRespawnPlan(params) { const env = params.env ?? process.env; const argv = process.argv; const platform = process.platform; if (platform !== "win32" && isForegroundGatewayRunArgv(argv)) return; if (isForegroundGmailRunArgv(argv) || shouldKeepNativeHookRelayInProcess(argv, platform)) return; if (!isSourceCheckoutInstallRoot(params.installRoot)) return; if (env[COMPILE_CACHE_DISABLED_RESPAWNED_ENV] === "1") return; if (!params.compileCacheDir && !isNodeCompileCacheRequested(env)) return; const nextEnv = { ...env, NODE_DISABLE_COMPILE_CACHE: "1", [COMPILE_CACHE_DISABLED_RESPAWNED_ENV]: "1" }; delete nextEnv.NODE_COMPILE_CACHE; return { command: process.execPath, args: [ ...process.execArgv, params.currentFile, ...argv.slice(2) ], env: nextEnv, detachForProcessTree: platform !== "win32" && !isTerminalInteractiveRespawnArgv(argv) }; } async function respawnWithoutOpenClawCompileCacheIfNeeded(params) { const plan = buildOpenClawCompileCacheRespawnPlan({ currentFile: params.currentFile, installRoot: params.installRoot, compileCacheDir: getCompileCacheDir?.(), env: params.env }); if (!plan) return false; const writeError = await params.prepareWriteError?.(); runOpenClawCompileCacheRespawnPlan(plan, writeError ? { spawn, attachChildProcessBridge, exit: process.exit.bind(process), writeError } : void 0); return true; } function runOpenClawCompileCacheRespawnPlan(plan, runtime = { spawn, attachChildProcessBridge, exit: process.exit.bind(process), writeError: (message) => { process.stderr.write(message); } }) { return runRespawnChildWithSignalBridge({ command: plan.command, args: plan.args, env: plan.env, detachForProcessTree: plan.detachForProcessTree, runtime, onError: (error) => { return runtime.writeError(`[openclaw] Failed to respawn CLI without compile cache: ${error instanceof Error ? error.stack ?? error.message : String(error)}\n`); } }); } function enableOpenClawCompileCache(params) { if (!shouldEnableOpenClawCompileCache(params)) return; try { const directory = resolveOpenClawCompileCacheDirectory(params); if (directory) enableOwnedNodeCompileCache(directory); } catch {} } //#endregion //#region src/entry.ts const inheritedRuntimeEnv = { ...process.env }; const ENTRY_WRAPPER_PAIRS = [ { wrapperBasename: "openclaw.mjs", entryBasename: "entry.js" }, { wrapperBasename: "openclaw.mjs", entryBasename: "entry.mjs" }, { wrapperBasename: "openclaw.js", entryBasename: "entry.js" } ]; async function writeCapturedCliArgumentError(message) { const { loadCliDotEnv } = await import("./dotenv-DpAMwI7j.mjs"); loadCliDotEnv({ quiet: true }); await configureGatewayStartupTraceConsoleFormatting(gatewayEntryStartupTrace); const { enableConsoleCapture } = await import("./logging-CTrAthQk.mjs"); enableConsoleCapture(); const [{ formatCliJsonFailure }, { isJsonOutputModeActive }] = await Promise.all([import("./failure-output-BgDOxbAf.mjs"), import("./json-output-mode-DJvhLoPt.mjs")]); if (isJsonOutputModeActive(process.argv)) defaultRuntime.writeJson(formatCliJsonFailure(message)); console.error(`[openclaw] ${message}`); } async function prepareCliDiagnosticBlockWriter() { const loadWriter = async () => { const { loadCliDotEnv } = await import("./dotenv-DpAMwI7j.mjs"); loadCliDotEnv({ quiet: true }); await configureGatewayStartupTraceConsoleFormatting(gatewayEntryStartupTrace); const { formatConsoleDiagnosticBlock } = await import("./json-console-line-COTb7cDP.mjs"); return (message, error) => { const formatted = error === void 0 ? message : format$1(message, error); process.stderr.write(formatConsoleDiagnosticBlock({ level: "error", message: formatted.endsWith("\n") ? formatted : `${formatted}\n` })); }; }; return gatewayEntryStartupTrace.enabled ? loadWriter() : async (message, error) => (await loadWriter())(message, error); } async function flushEntryStartupTraceForEarlyReturn(argv) { if (!gatewayEntryStartupTrace.enabled) return; const { loadCliDotEnvForEarlyDiagnostic } = await import("./dotenv-DpAMwI7j.mjs"); await loadCliDotEnvForEarlyDiagnostic(argv); await configureGatewayStartupTraceConsoleFormatting(gatewayEntryStartupTrace); } function shouldForceReadOnlyAuthStore(argv) { const tokens = argv.slice(2).filter((token) => token.length > 0 && !token.startsWith("-")); for (let index = 0; index < tokens.length - 1; index += 1) if (tokens[index] === "secrets" && tokens[index + 1] === "audit") return true; return false; } const gatewayEntryStartupTrace = createGatewayDispatchStartupTrace(process.argv, "entry"); const isEntryMain = isMainModule({ currentFile: fileURLToPath(import.meta.url), wrapperEntryPairs: [...ENTRY_WRAPPER_PAIRS] }); if (isEntryMain) disableExitUnsafeCompilers(); if (!isEntryMain) {} else if (isUpdateAdmissionInvocation(resolveCliArgvInvocation(process.argv))) await tryRunUpdateAdmissionBeforeStartup(resolveCliArgvInvocation(process.argv)); else { const entryFile = fileURLToPath(import.meta.url); const installRoot = resolveEntryInstallRoot(entryFile); installDistEsmResolveFastPath(import.meta.url); ensureOpenClawExecMarkerOnProcess(); installProcessWarningFilter(); normalizeEnv(); process.argv = normalizeWindowsArgv(process.argv); const earlyProfile = parseCliProfileArgs(process.argv); if (earlyProfile.ok && earlyProfile.profile) applyCliProfileEnv({ profile: earlyProfile.profile }); const startupEnv = { ...fsSafeEnvInput(process.env) }; const { assertSupportedRuntime, isCurrentRuntimeSupported } = await import("./runtime-guard-BsfkDhfP.mjs"); if (!await isCurrentRuntimeSupported()) { const { loadCliDotEnv } = await import("./dotenv-DpAMwI7j.mjs"); loadCliDotEnv({ quiet: true }); await configureGatewayStartupTraceConsoleFormatting(gatewayEntryStartupTrace); } await assertSupportedRuntime(void 0, void 0, process.argv, false, inheritedRuntimeEnv); const { runNodeHostLauncher } = await import(new URL("../node-host-launcher.mjs", import.meta.url).href); if (await runNodeHostLauncher({ entryPath: entryFile, packageRoot: installRoot })) process.exit(process.exitCode ?? 0); gatewayEntryStartupTrace.mark("bootstrap"); if (!(!isNodeHostLauncherChild() && await respawnWithoutOpenClawCompileCacheIfNeeded({ currentFile: entryFile, installRoot, env: startupEnv, prepareWriteError: async () => { const writeError = await prepareCliDiagnosticBlockWriter(); return (message) => writeError(message); } }))) { enableOpenClawCompileCache({ installRoot }); if (shouldForceReadOnlyAuthStore(process.argv)) process.env.OPENCLAW_AUTH_STORE_READONLY = "1"; if (process.argv.includes("--no-color")) { process.env.NO_COLOR = "1"; process.env.FORCE_COLOR = "0"; } async function ensureCliRespawnReady() { const plan = buildCliRespawnPlan({ env: startupEnv }); if (!plan) return false; if (isNodeHostLauncherChild()) { await requestNodeHostLauncherBootstrap({ execArgv: plan.argv.slice(0, plan.argv.length - process.argv.length + 1), env: plan.env }); process.exit(0); } const writeError = await prepareCliDiagnosticBlockWriter(); runCliRespawnPlan(plan, void 0, writeError); return true; } if (!await ensureCliRespawnReady()) { await assertSupportedRuntime(void 0, void 0, process.argv, true, inheritedRuntimeEnv); process.title = "openclaw"; const parsedContainer = parseCliContainerArgs(process.argv); if (!parsedContainer.ok) { await writeCapturedCliArgumentError(parsedContainer.error); process.exit(2); } const parsed = parseCliProfileArgs(parsedContainer.argv); if (!parsed.ok) { await writeCapturedCliArgumentError(parsed.error); process.exit(2); } const containerTargetName = resolveCliContainerTarget(process.argv); if (parsed.profile) { applyCliProfileEnv({ profile: parsed.profile }); process.argv = parsed.argv; } if (containerTargetName && parsed.profile) { await writeCapturedCliArgumentError("--container cannot be combined with --profile/--dev"); process.exit(2); } gatewayEntryStartupTrace.mark("argv"); if (!tryHandleRootVersionFastPath(process.argv)) { const run = async (finalize) => { const { withCliProcessScope } = await import("./runtime-cleanup-scope-DZdWq84C.mjs"); return withCliProcessScope(() => runMainOrRootHelp(process.argv, { finalize })); }; const managedNodeStatePath = getManagedNodeHostStatePath(); if (managedNodeStatePath) { const { withExistingOpenClawStateSchema } = await import("./openclaw-state-db-schema-policy-CZKo1yPm.mjs"); await withExistingOpenClawStateSchema({ path: managedNodeStatePath }, async () => { const { openOpenClawStateDatabase, closeOpenClawStateDatabaseByPathAsync } = await import("./openclaw-state-db-BoNab8eI.mjs"); openOpenClawStateDatabase({ path: managedNodeStatePath }); await run(async () => { await closeOpenClawStateDatabaseByPathAsync(managedNodeStatePath); }); }); } else await run(); } } } } async function tryHandleRootHelpFastPath(argv) { const env = process.env; if (env.OPENCLAW_DISABLE_CLI_STARTUP_HELP_FAST_PATH === "1" || resolveCliContainerTarget(argv, env)) return false; if (!isRootHelpInvocation(argv)) return false; try { const { loadRootHelpRenderOptionsForConfigSensitivePlugins } = await import("./root-help-live-config-DLFpCf-w.mjs"); const liveRootHelpOptions = await loadRootHelpRenderOptionsForConfigSensitivePlugins(env); if (!liveRootHelpOptions) { const { outputPrecomputedRootHelpText } = await import("./root-help-metadata-D_5fa834.mjs"); if (outputPrecomputedRootHelpText()) return true; } const { outputRootHelp } = await import("./root-help-CfdEl8X5.mjs"); await outputRootHelp(liveRootHelpOptions ?? void 0); return true; } catch (error) { const detail = error instanceof Error ? error.stack ?? error.message : String(error); await (await prepareCliDiagnosticBlockWriter())(`[openclaw] Failed to display help: ${detail}\n`); return process.exit(1); } } async function tryHandlePrecomputedCommandHelpFastPath(argv) { if (resolveCliContainerTarget(argv)) return false; try { return await tryOutputPrecomputedCommandHelp(argv); } catch { return false; } } async function prepareCliFailureHandler(argv, commandStarted) { const [{ loadCliDotEnvForEarlyDiagnostic }, { enableConsoleCapture }, { formatCliFailureLines, formatCliJsonFailure }, { isJsonOutputModeActive }, configureTrace] = await Promise.all([ import("./dotenv-DpAMwI7j.mjs"), import("./logging-CTrAthQk.mjs"), import("./failure-output-BgDOxbAf.mjs"), import("./json-output-mode-DJvhLoPt.mjs"), prepareGatewayStartupTraceConsoleFormatting(gatewayEntryStartupTrace) ]); return async (error) => { await loadCliDotEnvForEarlyDiagnostic(argv); configureTrace(); enableConsoleCapture(); if (isJsonOutputModeActive(argv)) defaultRuntime.writeJson(formatCliJsonFailure(error)); for (const line of formatCliFailureLines({ title: commandStarted() ? "The CLI command failed." : "Could not start the CLI.", error, argv })) console.error(line); process.exitCode = 1; }; } async function runMainOrRootHelp(argv, deps = {}) { let commandStarted = false; let failureHandler; const { runCliWithExitFinalization, requestExitAfterOneShotOutput } = await import("./one-shot-exit-BNnqpGfV.mjs"); await runCliWithExitFinalization({ finalize: deps.finalize, run: async () => { if (isNativeHookRelayArgv(argv) && !argv.includes("--help") && !argv.includes("-h")) { const { runNativeHookRelayCliFromArgv } = await import("./native-hook-relay-cli-B2dWW4SW.mjs"); const exitCode = await runNativeHookRelayCliFromArgv(argv); process.exitCode = exitCode; requestExitAfterOneShotOutput(defaultRuntime, exitCode); return; } if (await tryHandleRootHelpFastPath(argv)) { await flushEntryStartupTraceForEarlyReturn(argv); requestExitAfterOneShotOutput(defaultRuntime); return; } if (await tryHandlePrecomputedCommandHelpFastPath(argv)) { await flushEntryStartupTraceForEarlyReturn(argv); requestExitAfterOneShotOutput(defaultRuntime); return; } const { runCli } = await gatewayEntryStartupTrace.measure("run-main-import", deps.loadRunCli ?? (() => import("./cli/run-main.js"))); failureHandler = await prepareCliFailureHandler(argv, () => commandStarted); commandStarted = true; await runCli(argv, { additionalStartupTrace: gatewayEntryStartupTrace, runtimeRecoveryEnv: inheritedRuntimeEnv, retainConsoleRoutingUntilProcessExit: true }); }, onError: async (error) => (failureHandler ?? await prepareCliFailureHandler(argv, () => commandStarted))(error) }); } //#endregion export { runMainOrRootHelp, tryHandlePrecomputedCommandHelpFastPath, tryHandleRootHelpFastPath };