@gguf/claw
Version:
WhatsApp gateway CLI (Baileys web) with Pi RPC agent
1,326 lines (1,302 loc) • 44.7 kB
JavaScript
import { createRequire } from "node:module";
import { spawn } from "node:child_process";
import path from "node:path";
import process$1 from "node:process";
import os from "node:os";
import chalk, { Chalk } from "chalk";
import fs from "node:fs";
import { Logger } from "tslog";
import JSON5 from "json5";
import util from "node:util";
//#region src/cli/profile-utils.ts
const PROFILE_NAME_RE = /^[a-z0-9][a-z0-9_-]{0,63}$/i;
function isValidProfileName(value) {
if (!value) return false;
return PROFILE_NAME_RE.test(value);
}
function normalizeProfileName(raw) {
const profile = raw?.trim();
if (!profile) return null;
if (profile.toLowerCase() === "default") return null;
if (!isValidProfileName(profile)) return null;
return profile;
}
//#endregion
//#region src/cli/profile.ts
function takeValue(raw, next) {
if (raw.includes("=")) {
const [, value] = raw.split("=", 2);
return {
value: (value ?? "").trim() || null,
consumedNext: false
};
}
return {
value: (next ?? "").trim() || null,
consumedNext: Boolean(next)
};
}
function parseCliProfileArgs(argv) {
if (argv.length < 2) return {
ok: true,
profile: null,
argv
};
const out = argv.slice(0, 2);
let profile = null;
let sawDev = false;
let sawCommand = false;
const args = argv.slice(2);
for (let i = 0; i < args.length; i += 1) {
const arg = args[i];
if (arg === void 0) continue;
if (sawCommand) {
out.push(arg);
continue;
}
if (arg === "--dev") {
if (profile && profile !== "dev") return {
ok: false,
error: "Cannot combine --dev with --profile"
};
sawDev = true;
profile = "dev";
continue;
}
if (arg === "--profile" || arg.startsWith("--profile=")) {
if (sawDev) return {
ok: false,
error: "Cannot combine --dev with --profile"
};
const next = args[i + 1];
const { value, consumedNext } = takeValue(arg, next);
if (consumedNext) i += 1;
if (!value) return {
ok: false,
error: "--profile requires a value"
};
if (!isValidProfileName(value)) return {
ok: false,
error: "Invalid --profile (use letters, numbers, \"_\", \"-\" only)"
};
profile = value;
continue;
}
if (!arg.startsWith("-")) {
sawCommand = true;
out.push(arg);
continue;
}
out.push(arg);
}
return {
ok: true,
profile,
argv: out
};
}
function resolveProfileStateDir(profile, homedir) {
const suffix = profile.toLowerCase() === "default" ? "" : `-${profile}`;
return path.join(homedir(), `.openclaw${suffix}`);
}
function applyCliProfileEnv(params) {
const env = params.env ?? process.env;
const homedir = params.homedir ?? os.homedir;
const profile = params.profile.trim();
if (!profile) return;
env.OPENCLAW_PROFILE = profile;
const stateDir = env.OPENCLAW_STATE_DIR?.trim() || resolveProfileStateDir(profile, homedir);
if (!env.OPENCLAW_STATE_DIR?.trim()) env.OPENCLAW_STATE_DIR = stateDir;
if (!env.OPENCLAW_CONFIG_PATH?.trim()) env.OPENCLAW_CONFIG_PATH = path.join(stateDir, "openclaw.json");
if (profile === "dev" && !env.OPENCLAW_GATEWAY_PORT?.trim()) env.OPENCLAW_GATEWAY_PORT = "19001";
}
//#endregion
//#region src/plugins/runtime.ts
const createEmptyRegistry = () => ({
plugins: [],
tools: [],
hooks: [],
typedHooks: [],
channels: [],
providers: [],
gatewayHandlers: {},
httpHandlers: [],
httpRoutes: [],
cliRegistrars: [],
services: [],
commands: [],
diagnostics: []
});
const REGISTRY_STATE = Symbol.for("openclaw.pluginRegistryState");
const state = (() => {
const globalState = globalThis;
if (!globalState[REGISTRY_STATE]) globalState[REGISTRY_STATE] = {
registry: createEmptyRegistry(),
key: null
};
return globalState[REGISTRY_STATE];
})();
function setActivePluginRegistry(registry, cacheKey) {
state.registry = registry;
state.key = cacheKey ?? null;
}
function getActivePluginRegistry() {
return state.registry;
}
function requireActivePluginRegistry() {
if (!state.registry) state.registry = createEmptyRegistry();
return state.registry;
}
//#endregion
//#region src/channels/registry.ts
const CHAT_CHANNEL_ORDER = [
"telegram",
"whatsapp",
"discord",
"googlechat",
"slack",
"signal",
"imessage"
];
const CHANNEL_IDS = [...CHAT_CHANNEL_ORDER];
const DEFAULT_CHAT_CHANNEL = "whatsapp";
const CHAT_CHANNEL_META = {
telegram: {
id: "telegram",
label: "Telegram",
selectionLabel: "Telegram (Bot API)",
detailLabel: "Telegram Bot",
docsPath: "/channels/telegram",
docsLabel: "telegram",
blurb: "simplest way to get started — register a bot with @BotFather and get going.",
systemImage: "paperplane",
selectionDocsPrefix: "",
selectionDocsOmitLabel: true,
selectionExtras: ["https://openclaw.ai"]
},
whatsapp: {
id: "whatsapp",
label: "WhatsApp",
selectionLabel: "WhatsApp (QR link)",
detailLabel: "WhatsApp Web",
docsPath: "/channels/whatsapp",
docsLabel: "whatsapp",
blurb: "works with your own number; recommend a separate phone + eSIM.",
systemImage: "message"
},
discord: {
id: "discord",
label: "Discord",
selectionLabel: "Discord (Bot API)",
detailLabel: "Discord Bot",
docsPath: "/channels/discord",
docsLabel: "discord",
blurb: "very well supported right now.",
systemImage: "bubble.left.and.bubble.right"
},
googlechat: {
id: "googlechat",
label: "Google Chat",
selectionLabel: "Google Chat (Chat API)",
detailLabel: "Google Chat",
docsPath: "/channels/googlechat",
docsLabel: "googlechat",
blurb: "Google Workspace Chat app with HTTP webhook.",
systemImage: "message.badge"
},
slack: {
id: "slack",
label: "Slack",
selectionLabel: "Slack (Socket Mode)",
detailLabel: "Slack Bot",
docsPath: "/channels/slack",
docsLabel: "slack",
blurb: "supported (Socket Mode).",
systemImage: "number"
},
signal: {
id: "signal",
label: "Signal",
selectionLabel: "Signal (signal-cli)",
detailLabel: "Signal REST",
docsPath: "/channels/signal",
docsLabel: "signal",
blurb: "signal-cli linked device; more setup (David Reagans: \"Hop on Discord.\").",
systemImage: "antenna.radiowaves.left.and.right"
},
imessage: {
id: "imessage",
label: "iMessage",
selectionLabel: "iMessage (imsg)",
detailLabel: "iMessage",
docsPath: "/channels/imessage",
docsLabel: "imessage",
blurb: "this is still a work in progress.",
systemImage: "message.fill"
}
};
const CHAT_CHANNEL_ALIASES = {
imsg: "imessage",
"google-chat": "googlechat",
gchat: "googlechat"
};
const normalizeChannelKey = (raw) => {
return raw?.trim().toLowerCase() || void 0;
};
function listChatChannels() {
return CHAT_CHANNEL_ORDER.map((id) => CHAT_CHANNEL_META[id]);
}
function getChatChannelMeta(id) {
return CHAT_CHANNEL_META[id];
}
function normalizeChatChannelId(raw) {
const normalized = normalizeChannelKey(raw);
if (!normalized) return null;
const resolved = CHAT_CHANNEL_ALIASES[normalized] ?? normalized;
return CHAT_CHANNEL_ORDER.includes(resolved) ? resolved : null;
}
function normalizeChannelId(raw) {
return normalizeChatChannelId(raw);
}
function normalizeAnyChannelId(raw) {
const key = normalizeChannelKey(raw);
if (!key) return null;
return requireActivePluginRegistry().channels.find((entry) => {
const id = String(entry.plugin.id ?? "").trim().toLowerCase();
if (id && id === key) return true;
return (entry.plugin.meta.aliases ?? []).some((alias) => alias.trim().toLowerCase() === key);
})?.plugin.id ?? null;
}
function formatChannelPrimerLine(meta) {
return `${meta.label}: ${meta.blurb}`;
}
function formatChannelSelectionLine(meta, docsLink) {
const docsPrefix = meta.selectionDocsPrefix ?? "Docs:";
const docsLabel = meta.docsLabel ?? meta.id;
const docs = meta.selectionDocsOmitLabel ? docsLink(meta.docsPath) : docsLink(meta.docsPath, docsLabel);
const extras = (meta.selectionExtras ?? []).filter(Boolean).join(" ");
return `${meta.label} — ${meta.blurb} ${docsPrefix ? `${docsPrefix} ` : ""}${docs}${extras ? ` ${extras}` : ""}`;
}
//#endregion
//#region src/config/paths.ts
/**
* Nix mode detection: When OPENCLAW_NIX_MODE=1, the gateway is running under Nix.
* In this mode:
* - No auto-install flows should be attempted
* - Missing dependencies should produce actionable Nix-specific error messages
* - Config is managed externally (read-only from Nix perspective)
*/
function resolveIsNixMode(env = process.env) {
return env.OPENCLAW_NIX_MODE === "1";
}
const isNixMode = resolveIsNixMode();
const LEGACY_STATE_DIRNAMES = [
".clawdbot",
".moltbot",
".moldbot"
];
const NEW_STATE_DIRNAME = ".openclaw";
const CONFIG_FILENAME = "openclaw.json";
const LEGACY_CONFIG_FILENAMES = [
"clawdbot.json",
"moltbot.json",
"moldbot.json"
];
function legacyStateDirs(homedir = os.homedir) {
return LEGACY_STATE_DIRNAMES.map((dir) => path.join(homedir(), dir));
}
function newStateDir(homedir = os.homedir) {
return path.join(homedir(), NEW_STATE_DIRNAME);
}
function resolveLegacyStateDirs(homedir = os.homedir) {
return legacyStateDirs(homedir);
}
function resolveNewStateDir(homedir = os.homedir) {
return newStateDir(homedir);
}
/**
* State directory for mutable data (sessions, logs, caches).
* Can be overridden via OPENCLAW_STATE_DIR.
* Default: ~/.openclaw
*/
function resolveStateDir(env = process.env, homedir = os.homedir) {
const override = env.OPENCLAW_STATE_DIR?.trim() || env.CLAWDBOT_STATE_DIR?.trim();
if (override) return resolveUserPath(override);
const newDir = newStateDir(homedir);
const legacyDirs = legacyStateDirs(homedir);
if (fs.existsSync(newDir)) return newDir;
const existingLegacy = legacyDirs.find((dir) => {
try {
return fs.existsSync(dir);
} catch {
return false;
}
});
if (existingLegacy) return existingLegacy;
return newDir;
}
function resolveUserPath(input) {
const trimmed = input.trim();
if (!trimmed) return trimmed;
if (trimmed.startsWith("~")) {
const expanded = trimmed.replace(/^~(?=$|[\\/])/, os.homedir());
return path.resolve(expanded);
}
return path.resolve(trimmed);
}
const STATE_DIR = resolveStateDir();
/**
* Config file path (JSON5).
* Can be overridden via OPENCLAW_CONFIG_PATH.
* Default: ~/.openclaw/openclaw.json (or $OPENCLAW_STATE_DIR/openclaw.json)
*/
function resolveCanonicalConfigPath(env = process.env, stateDir = resolveStateDir(env, os.homedir)) {
const override = env.OPENCLAW_CONFIG_PATH?.trim() || env.CLAWDBOT_CONFIG_PATH?.trim();
if (override) return resolveUserPath(override);
return path.join(stateDir, CONFIG_FILENAME);
}
/**
* Resolve the active config path by preferring existing config candidates
* before falling back to the canonical path.
*/
function resolveConfigPathCandidate(env = process.env, homedir = os.homedir) {
const existing = resolveDefaultConfigCandidates(env, homedir).find((candidate) => {
try {
return fs.existsSync(candidate);
} catch {
return false;
}
});
if (existing) return existing;
return resolveCanonicalConfigPath(env, resolveStateDir(env, homedir));
}
/**
* Active config path (prefers existing config files).
*/
function resolveConfigPath(env = process.env, stateDir = resolveStateDir(env, os.homedir), homedir = os.homedir) {
const override = env.OPENCLAW_CONFIG_PATH?.trim();
if (override) return resolveUserPath(override);
const stateOverride = env.OPENCLAW_STATE_DIR?.trim();
const existing = [path.join(stateDir, CONFIG_FILENAME), ...LEGACY_CONFIG_FILENAMES.map((name) => path.join(stateDir, name))].find((candidate) => {
try {
return fs.existsSync(candidate);
} catch {
return false;
}
});
if (existing) return existing;
if (stateOverride) return path.join(stateDir, CONFIG_FILENAME);
const defaultStateDir = resolveStateDir(env, homedir);
if (path.resolve(stateDir) === path.resolve(defaultStateDir)) return resolveConfigPathCandidate(env, homedir);
return path.join(stateDir, CONFIG_FILENAME);
}
const CONFIG_PATH = resolveConfigPathCandidate();
/**
* Resolve default config path candidates across default locations.
* Order: explicit config path → state-dir-derived paths → new default.
*/
function resolveDefaultConfigCandidates(env = process.env, homedir = os.homedir) {
const explicit = env.OPENCLAW_CONFIG_PATH?.trim() || env.CLAWDBOT_CONFIG_PATH?.trim();
if (explicit) return [resolveUserPath(explicit)];
const candidates = [];
const openclawStateDir = env.OPENCLAW_STATE_DIR?.trim() || env.CLAWDBOT_STATE_DIR?.trim();
if (openclawStateDir) {
const resolved = resolveUserPath(openclawStateDir);
candidates.push(path.join(resolved, CONFIG_FILENAME));
candidates.push(...LEGACY_CONFIG_FILENAMES.map((name) => path.join(resolved, name)));
}
const defaultDirs = [newStateDir(homedir), ...legacyStateDirs(homedir)];
for (const dir of defaultDirs) {
candidates.push(path.join(dir, CONFIG_FILENAME));
candidates.push(...LEGACY_CONFIG_FILENAMES.map((name) => path.join(dir, name)));
}
return candidates;
}
const DEFAULT_GATEWAY_PORT = 18789;
/**
* Gateway lock directory (ephemeral).
* Default: os.tmpdir()/openclaw-<uid> (uid suffix when available).
*/
function resolveGatewayLockDir(tmpdir = os.tmpdir) {
const base = tmpdir();
const uid = typeof process.getuid === "function" ? process.getuid() : void 0;
const suffix = uid != null ? `openclaw-${uid}` : "openclaw";
return path.join(base, suffix);
}
const OAUTH_FILENAME = "oauth.json";
/**
* OAuth credentials storage directory.
*
* Precedence:
* - `OPENCLAW_OAUTH_DIR` (explicit override)
* - `$*_STATE_DIR/credentials` (canonical server/default)
*/
function resolveOAuthDir(env = process.env, stateDir = resolveStateDir(env, os.homedir)) {
const override = env.OPENCLAW_OAUTH_DIR?.trim();
if (override) return resolveUserPath(override);
return path.join(stateDir, "credentials");
}
function resolveOAuthPath(env = process.env, stateDir = resolveStateDir(env, os.homedir)) {
return path.join(resolveOAuthDir(env, stateDir), OAUTH_FILENAME);
}
function resolveGatewayPort(cfg, env = process.env) {
const envRaw = env.OPENCLAW_GATEWAY_PORT?.trim() || env.CLAWDBOT_GATEWAY_PORT?.trim();
if (envRaw) {
const parsed = Number.parseInt(envRaw, 10);
if (Number.isFinite(parsed) && parsed > 0) return parsed;
}
const configPort = cfg?.gateway?.port;
if (typeof configPort === "number" && Number.isFinite(configPort)) {
if (configPort > 0) return configPort;
}
return DEFAULT_GATEWAY_PORT;
}
//#endregion
//#region src/logging/config.ts
function readLoggingConfig() {
const configPath = resolveConfigPath();
try {
if (!fs.existsSync(configPath)) return;
const raw = fs.readFileSync(configPath, "utf-8");
const logging = JSON5.parse(raw)?.logging;
if (!logging || typeof logging !== "object" || Array.isArray(logging)) return;
return logging;
} catch {
return;
}
}
//#endregion
//#region src/logging/levels.ts
const ALLOWED_LOG_LEVELS = [
"silent",
"fatal",
"error",
"warn",
"info",
"debug",
"trace"
];
function normalizeLogLevel(level, fallback = "info") {
const candidate = (level ?? fallback).trim();
return ALLOWED_LOG_LEVELS.includes(candidate) ? candidate : fallback;
}
function levelToMinLevel(level) {
return {
fatal: 0,
error: 1,
warn: 2,
info: 3,
debug: 4,
trace: 5,
silent: Number.POSITIVE_INFINITY
}[level];
}
//#endregion
//#region src/logging/state.ts
const loggingState = {
cachedLogger: null,
cachedSettings: null,
cachedConsoleSettings: null,
overrideSettings: null,
consolePatched: false,
forceConsoleToStderr: false,
consoleTimestampPrefix: false,
consoleSubsystemFilter: null,
resolvingConsoleSettings: false,
rawConsole: null
};
//#endregion
//#region src/logging/logger.ts
const DEFAULT_LOG_DIR = "/tmp/openclaw";
const DEFAULT_LOG_FILE = path.join(DEFAULT_LOG_DIR, "openclaw.log");
const LOG_PREFIX = "openclaw";
const LOG_SUFFIX = ".log";
const MAX_LOG_AGE_MS = 1440 * 60 * 1e3;
const requireConfig$1 = createRequire(import.meta.url);
const externalTransports = /* @__PURE__ */ new Set();
function attachExternalTransport(logger, transport) {
logger.attachTransport((logObj) => {
if (!externalTransports.has(transport)) return;
try {
transport(logObj);
} catch {}
});
}
function resolveSettings() {
let cfg = loggingState.overrideSettings ?? readLoggingConfig();
if (!cfg) try {
cfg = requireConfig$1("../config/config.js").loadConfig?.().logging;
} catch {
cfg = void 0;
}
return {
level: normalizeLogLevel(cfg?.level, "info"),
file: cfg?.file ?? defaultRollingPathForToday()
};
}
function settingsChanged(a, b) {
if (!a) return true;
return a.level !== b.level || a.file !== b.file;
}
function isFileLogLevelEnabled(level) {
const settings = loggingState.cachedSettings ?? resolveSettings();
if (!loggingState.cachedSettings) loggingState.cachedSettings = settings;
if (settings.level === "silent") return false;
return levelToMinLevel(level) <= levelToMinLevel(settings.level);
}
function buildLogger(settings) {
fs.mkdirSync(path.dirname(settings.file), { recursive: true });
if (isRollingPath(settings.file)) pruneOldRollingLogs(path.dirname(settings.file));
const logger = new Logger({
name: "openclaw",
minLevel: levelToMinLevel(settings.level),
type: "hidden"
});
logger.attachTransport((logObj) => {
try {
const time = logObj.date?.toISOString?.() ?? (/* @__PURE__ */ new Date()).toISOString();
const line = JSON.stringify({
...logObj,
time
});
fs.appendFileSync(settings.file, `${line}\n`, { encoding: "utf8" });
} catch {}
});
for (const transport of externalTransports) attachExternalTransport(logger, transport);
return logger;
}
function getLogger() {
const settings = resolveSettings();
const cachedLogger = loggingState.cachedLogger;
const cachedSettings = loggingState.cachedSettings;
if (!cachedLogger || settingsChanged(cachedSettings, settings)) {
loggingState.cachedLogger = buildLogger(settings);
loggingState.cachedSettings = settings;
}
return loggingState.cachedLogger;
}
function getChildLogger(bindings, opts) {
const base = getLogger();
const minLevel = opts?.level ? levelToMinLevel(opts.level) : void 0;
const name = bindings ? JSON.stringify(bindings) : void 0;
return base.getSubLogger({
name,
minLevel,
prefix: bindings ? [name ?? ""] : []
});
}
function toPinoLikeLogger(logger, level) {
const buildChild = (bindings) => toPinoLikeLogger(logger.getSubLogger({ name: bindings ? JSON.stringify(bindings) : void 0 }), level);
return {
level,
child: buildChild,
trace: (...args) => logger.trace(...args),
debug: (...args) => logger.debug(...args),
info: (...args) => logger.info(...args),
warn: (...args) => logger.warn(...args),
error: (...args) => logger.error(...args),
fatal: (...args) => logger.fatal(...args)
};
}
function getResolvedLoggerSettings() {
return resolveSettings();
}
function formatLocalDate(date) {
return `${date.getFullYear()}-${String(date.getMonth() + 1).padStart(2, "0")}-${String(date.getDate()).padStart(2, "0")}`;
}
function defaultRollingPathForToday() {
const today = formatLocalDate(/* @__PURE__ */ new Date());
return path.join(DEFAULT_LOG_DIR, `${LOG_PREFIX}-${today}${LOG_SUFFIX}`);
}
function isRollingPath(file) {
const base = path.basename(file);
return base.startsWith(`${LOG_PREFIX}-`) && base.endsWith(LOG_SUFFIX) && base.length === `${LOG_PREFIX}-YYYY-MM-DD${LOG_SUFFIX}`.length;
}
function pruneOldRollingLogs(dir) {
try {
const entries = fs.readdirSync(dir, { withFileTypes: true });
const cutoff = Date.now() - MAX_LOG_AGE_MS;
for (const entry of entries) {
if (!entry.isFile()) continue;
if (!entry.name.startsWith(`${LOG_PREFIX}-`) || !entry.name.endsWith(LOG_SUFFIX)) continue;
const fullPath = path.join(dir, entry.name);
try {
if (fs.statSync(fullPath).mtimeMs < cutoff) fs.rmSync(fullPath, { force: true });
} catch {}
}
} catch {}
}
//#endregion
//#region src/terminal/palette.ts
const LOBSTER_PALETTE = {
accent: "#FF5A2D",
accentBright: "#FF7A3D",
accentDim: "#D14A22",
info: "#FF8A5B",
success: "#2FBF71",
warn: "#FFB020",
error: "#E23D2D",
muted: "#8B7F77"
};
//#endregion
//#region src/terminal/theme.ts
const hasForceColor = typeof process.env.FORCE_COLOR === "string" && process.env.FORCE_COLOR.trim().length > 0 && process.env.FORCE_COLOR.trim() !== "0";
const baseChalk = process.env.NO_COLOR && !hasForceColor ? new Chalk({ level: 0 }) : chalk;
const hex = (value) => baseChalk.hex(value);
const theme = {
accent: hex(LOBSTER_PALETTE.accent),
accentBright: hex(LOBSTER_PALETTE.accentBright),
accentDim: hex(LOBSTER_PALETTE.accentDim),
info: hex(LOBSTER_PALETTE.info),
success: hex(LOBSTER_PALETTE.success),
warn: hex(LOBSTER_PALETTE.warn),
error: hex(LOBSTER_PALETTE.error),
muted: hex(LOBSTER_PALETTE.muted),
heading: baseChalk.bold.hex(LOBSTER_PALETTE.accent),
command: hex(LOBSTER_PALETTE.accentBright),
option: hex(LOBSTER_PALETTE.warn)
};
const isRich = () => Boolean(baseChalk.level > 0);
const colorize = (rich, color, value) => rich ? color(value) : value;
//#endregion
//#region src/globals.ts
let globalVerbose = false;
function setVerbose(v) {
globalVerbose = v;
}
function isVerbose() {
return globalVerbose;
}
function shouldLogVerbose() {
return globalVerbose || isFileLogLevelEnabled("debug");
}
function logVerbose(message) {
if (!shouldLogVerbose()) return;
try {
getLogger().debug({ message }, "verbose");
} catch {}
if (!globalVerbose) return;
console.log(theme.muted(message));
}
function logVerboseConsole(message) {
if (!globalVerbose) return;
console.log(theme.muted(message));
}
const success = theme.success;
const warn = theme.warn;
const info = theme.info;
const danger = theme.error;
//#endregion
//#region src/terminal/progress-line.ts
let activeStream = null;
function registerActiveProgressLine(stream) {
if (!stream.isTTY) return;
activeStream = stream;
}
function clearActiveProgressLine() {
if (!activeStream?.isTTY) return;
activeStream.write("\r\x1B[2K");
}
function unregisterActiveProgressLine(stream) {
if (!activeStream) return;
if (stream && activeStream !== stream) return;
activeStream = null;
}
//#endregion
//#region src/terminal/restore.ts
const RESET_SEQUENCE = "\x1B[0m\x1B[?25h\x1B[?1000l\x1B[?1002l\x1B[?1003l\x1B[?1006l\x1B[?2004l";
function reportRestoreFailure(scope, err, reason) {
const suffix = reason ? ` (${reason})` : "";
const message = `[terminal] restore ${scope} failed${suffix}: ${String(err)}`;
try {
process.stderr.write(`${message}\n`);
} catch (writeErr) {
console.error(`[terminal] restore reporting failed${suffix}: ${String(writeErr)}`);
}
}
function restoreTerminalState(reason) {
try {
clearActiveProgressLine();
} catch (err) {
reportRestoreFailure("progress line", err, reason);
}
const stdin = process.stdin;
if (stdin.isTTY && typeof stdin.setRawMode === "function") {
try {
stdin.setRawMode(false);
} catch (err) {
reportRestoreFailure("raw mode", err, reason);
}
if (typeof stdin.isPaused === "function" && stdin.isPaused()) try {
stdin.resume();
} catch (err) {
reportRestoreFailure("stdin resume", err, reason);
}
}
if (process.stdout.isTTY) try {
process.stdout.write(RESET_SEQUENCE);
} catch (err) {
reportRestoreFailure("stdout reset", err, reason);
}
}
//#endregion
//#region src/runtime.ts
const defaultRuntime = {
log: (...args) => {
clearActiveProgressLine();
console.log(...args);
},
error: (...args) => {
clearActiveProgressLine();
console.error(...args);
},
exit: (code) => {
restoreTerminalState("runtime exit");
process.exit(code);
throw new Error("unreachable");
}
};
//#endregion
//#region src/terminal/ansi.ts
const ANSI_SGR_PATTERN = "\\x1b\\[[0-9;]*m";
const OSC8_PATTERN = "\\x1b\\]8;;.*?\\x1b\\\\|\\x1b\\]8;;\\x1b\\\\";
const ANSI_REGEX = new RegExp(ANSI_SGR_PATTERN, "g");
const OSC8_REGEX = new RegExp(OSC8_PATTERN, "g");
function stripAnsi(input) {
return input.replace(OSC8_REGEX, "").replace(ANSI_REGEX, "");
}
function visibleWidth(input) {
return Array.from(stripAnsi(input)).length;
}
//#endregion
//#region src/logging/console.ts
const requireConfig = createRequire(import.meta.url);
function normalizeConsoleLevel(level) {
if (isVerbose()) return "debug";
return normalizeLogLevel(level, "info");
}
function normalizeConsoleStyle(style) {
if (style === "compact" || style === "json" || style === "pretty") return style;
if (!process.stdout.isTTY) return "compact";
return "pretty";
}
function resolveConsoleSettings() {
let cfg = loggingState.overrideSettings ?? readLoggingConfig();
if (!cfg) if (loggingState.resolvingConsoleSettings) cfg = void 0;
else {
loggingState.resolvingConsoleSettings = true;
try {
cfg = requireConfig("../config/config.js").loadConfig?.().logging;
} catch {
cfg = void 0;
} finally {
loggingState.resolvingConsoleSettings = false;
}
}
return {
level: normalizeConsoleLevel(cfg?.consoleLevel),
style: normalizeConsoleStyle(cfg?.consoleStyle)
};
}
function consoleSettingsChanged(a, b) {
if (!a) return true;
return a.level !== b.level || a.style !== b.style;
}
function getConsoleSettings() {
const settings = resolveConsoleSettings();
const cached = loggingState.cachedConsoleSettings;
if (!cached || consoleSettingsChanged(cached, settings)) loggingState.cachedConsoleSettings = settings;
return loggingState.cachedConsoleSettings;
}
function setConsoleSubsystemFilter(filters) {
if (!filters || filters.length === 0) {
loggingState.consoleSubsystemFilter = null;
return;
}
const normalized = filters.map((value) => value.trim()).filter((value) => value.length > 0);
loggingState.consoleSubsystemFilter = normalized.length > 0 ? normalized : null;
}
function setConsoleTimestampPrefix(enabled) {
loggingState.consoleTimestampPrefix = enabled;
}
function shouldLogSubsystemToConsole(subsystem) {
const filter = loggingState.consoleSubsystemFilter;
if (!filter || filter.length === 0) return true;
return filter.some((prefix) => subsystem === prefix || subsystem.startsWith(`${prefix}/`));
}
const SUPPRESSED_CONSOLE_PREFIXES = [
"Closing session:",
"Opening session:",
"Removing old closed session:",
"Session already closed",
"Session already open"
];
function shouldSuppressConsoleMessage(message) {
if (isVerbose()) return false;
if (SUPPRESSED_CONSOLE_PREFIXES.some((prefix) => message.startsWith(prefix))) return true;
if (message.startsWith("[EventQueue] Slow listener detected") && message.includes("DiscordMessageListener")) return true;
return false;
}
function isEpipeError(err) {
const code = err?.code;
return code === "EPIPE" || code === "EIO";
}
function formatConsoleTimestamp(style) {
const now = (/* @__PURE__ */ new Date()).toISOString();
if (style === "pretty") return now.slice(11, 19);
return now;
}
function hasTimestampPrefix(value) {
return /^(?:\d{2}:\d{2}:\d{2}|\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d+)?Z?)/.test(value);
}
function isJsonPayload(value) {
const trimmed = value.trim();
if (!trimmed.startsWith("{") && !trimmed.startsWith("[")) return false;
try {
JSON.parse(trimmed);
return true;
} catch {
return false;
}
}
/**
* Route console.* calls through file logging while still emitting to stdout/stderr.
* This keeps user-facing output unchanged but guarantees every console call is captured in log files.
*/
function enableConsoleCapture() {
if (loggingState.consolePatched) return;
loggingState.consolePatched = true;
let logger = null;
const getLoggerLazy = () => {
if (!logger) logger = getLogger();
return logger;
};
const original = {
log: console.log,
info: console.info,
warn: console.warn,
error: console.error,
debug: console.debug,
trace: console.trace
};
loggingState.rawConsole = {
log: original.log,
info: original.info,
warn: original.warn,
error: original.error
};
const forward = (level, orig) => (...args) => {
const formatted = util.format(...args);
if (shouldSuppressConsoleMessage(formatted)) return;
const trimmed = stripAnsi(formatted).trimStart();
const timestamp = loggingState.consoleTimestampPrefix && trimmed.length > 0 && !hasTimestampPrefix(trimmed) && !isJsonPayload(trimmed) ? formatConsoleTimestamp(getConsoleSettings().style) : "";
try {
const resolvedLogger = getLoggerLazy();
if (level === "trace") resolvedLogger.trace(formatted);
else if (level === "debug") resolvedLogger.debug(formatted);
else if (level === "info") resolvedLogger.info(formatted);
else if (level === "warn") resolvedLogger.warn(formatted);
else if (level === "error" || level === "fatal") resolvedLogger.error(formatted);
else resolvedLogger.info(formatted);
} catch {}
if (loggingState.forceConsoleToStderr) try {
const line = timestamp ? `${timestamp} ${formatted}` : formatted;
process.stderr.write(`${line}\n`);
} catch (err) {
if (isEpipeError(err)) return;
throw err;
}
else try {
if (!timestamp) {
orig.apply(console, args);
return;
}
if (args.length === 0) {
orig.call(console, timestamp);
return;
}
if (typeof args[0] === "string") {
orig.call(console, `${timestamp} ${args[0]}`, ...args.slice(1));
return;
}
orig.call(console, timestamp, ...args);
} catch (err) {
if (isEpipeError(err)) return;
throw err;
}
};
console.log = forward("info", original.log);
console.info = forward("info", original.info);
console.warn = forward("warn", original.warn);
console.error = forward("error", original.error);
console.debug = forward("debug", original.debug);
console.trace = forward("trace", original.trace);
}
//#endregion
//#region src/logging/subsystem.ts
function shouldLogToConsole(level, settings) {
if (settings.level === "silent") return false;
return levelToMinLevel(level) <= levelToMinLevel(settings.level);
}
function isRichConsoleEnv() {
const term = (process.env.TERM ?? "").toLowerCase();
if (process.env.COLORTERM || process.env.TERM_PROGRAM) return true;
return term.length > 0 && term !== "dumb";
}
function getColorForConsole() {
const hasForceColor = typeof process.env.FORCE_COLOR === "string" && process.env.FORCE_COLOR.trim().length > 0 && process.env.FORCE_COLOR.trim() !== "0";
if (process.env.NO_COLOR && !hasForceColor) return new Chalk({ level: 0 });
return Boolean(process.stdout.isTTY || process.stderr.isTTY) || isRichConsoleEnv() ? new Chalk({ level: 1 }) : new Chalk({ level: 0 });
}
const SUBSYSTEM_COLORS = [
"cyan",
"green",
"yellow",
"blue",
"magenta",
"red"
];
const SUBSYSTEM_COLOR_OVERRIDES = { "gmail-watcher": "blue" };
const SUBSYSTEM_PREFIXES_TO_DROP = [
"gateway",
"channels",
"providers"
];
const SUBSYSTEM_MAX_SEGMENTS = 2;
const CHANNEL_SUBSYSTEM_PREFIXES = new Set(CHAT_CHANNEL_ORDER);
function pickSubsystemColor(color, subsystem) {
const override = SUBSYSTEM_COLOR_OVERRIDES[subsystem];
if (override) return color[override];
let hash = 0;
for (let i = 0; i < subsystem.length; i += 1) hash = hash * 31 + subsystem.charCodeAt(i) | 0;
return color[SUBSYSTEM_COLORS[Math.abs(hash) % SUBSYSTEM_COLORS.length]];
}
function formatSubsystemForConsole(subsystem) {
const parts = subsystem.split("/").filter(Boolean);
const original = parts.join("/") || subsystem;
while (parts.length > 0 && SUBSYSTEM_PREFIXES_TO_DROP.includes(parts[0])) parts.shift();
if (parts.length === 0) return original;
if (CHANNEL_SUBSYSTEM_PREFIXES.has(parts[0])) return parts[0];
if (parts.length > SUBSYSTEM_MAX_SEGMENTS) return parts.slice(-SUBSYSTEM_MAX_SEGMENTS).join("/");
return parts.join("/");
}
function stripRedundantSubsystemPrefixForConsole(message, displaySubsystem) {
if (!displaySubsystem) return message;
if (message.startsWith("[")) {
const closeIdx = message.indexOf("]");
if (closeIdx > 1) {
if (message.slice(1, closeIdx).toLowerCase() === displaySubsystem.toLowerCase()) {
let i = closeIdx + 1;
while (message[i] === " ") i += 1;
return message.slice(i);
}
}
}
if (message.slice(0, displaySubsystem.length).toLowerCase() !== displaySubsystem.toLowerCase()) return message;
const next = message.slice(displaySubsystem.length, displaySubsystem.length + 1);
if (next !== ":" && next !== " ") return message;
let i = displaySubsystem.length;
while (message[i] === " ") i += 1;
if (message[i] === ":") i += 1;
while (message[i] === " ") i += 1;
return message.slice(i);
}
function formatConsoleLine(opts) {
const displaySubsystem = opts.style === "json" ? opts.subsystem : formatSubsystemForConsole(opts.subsystem);
if (opts.style === "json") return JSON.stringify({
time: (/* @__PURE__ */ new Date()).toISOString(),
level: opts.level,
subsystem: displaySubsystem,
message: opts.message,
...opts.meta
});
const color = getColorForConsole();
const prefix = `[${displaySubsystem}]`;
const prefixColor = pickSubsystemColor(color, displaySubsystem);
const levelColor = opts.level === "error" || opts.level === "fatal" ? color.red : opts.level === "warn" ? color.yellow : opts.level === "debug" || opts.level === "trace" ? color.gray : color.cyan;
const displayMessage = stripRedundantSubsystemPrefixForConsole(opts.message, displaySubsystem);
return `${[(() => {
if (opts.style === "pretty") return color.gray((/* @__PURE__ */ new Date()).toISOString().slice(11, 19));
if (loggingState.consoleTimestampPrefix) return color.gray((/* @__PURE__ */ new Date()).toISOString());
return "";
})(), prefixColor(prefix)].filter(Boolean).join(" ")} ${levelColor(displayMessage)}`;
}
function writeConsoleLine(level, line) {
clearActiveProgressLine();
const sanitized = process.platform === "win32" && process.env.GITHUB_ACTIONS === "true" ? line.replace(/[\uD800-\uDBFF][\uDC00-\uDFFF]/g, "?").replace(/[\uD800-\uDFFF]/g, "?") : line;
const sink = loggingState.rawConsole ?? console;
if (loggingState.forceConsoleToStderr || level === "error" || level === "fatal") (sink.error ?? console.error)(sanitized);
else if (level === "warn") (sink.warn ?? console.warn)(sanitized);
else (sink.log ?? console.log)(sanitized);
}
function logToFile(fileLogger, level, message, meta) {
if (level === "silent") return;
const method = fileLogger[level];
if (typeof method !== "function") return;
if (meta && Object.keys(meta).length > 0) method.call(fileLogger, meta, message);
else method.call(fileLogger, message);
}
function createSubsystemLogger(subsystem) {
let fileLogger = null;
const getFileLogger = () => {
if (!fileLogger) fileLogger = getChildLogger({ subsystem });
return fileLogger;
};
const emit = (level, message, meta) => {
const consoleSettings = getConsoleSettings();
let consoleMessageOverride;
let fileMeta = meta;
if (meta && Object.keys(meta).length > 0) {
const { consoleMessage, ...rest } = meta;
if (typeof consoleMessage === "string") consoleMessageOverride = consoleMessage;
fileMeta = Object.keys(rest).length > 0 ? rest : void 0;
}
logToFile(getFileLogger(), level, message, fileMeta);
if (!shouldLogToConsole(level, { level: consoleSettings.level })) return;
if (!shouldLogSubsystemToConsole(subsystem)) return;
const consoleMessage = consoleMessageOverride ?? message;
if (!isVerbose() && subsystem === "agent/embedded" && /(sessionId|runId)=probe-/.test(consoleMessage)) return;
writeConsoleLine(level, formatConsoleLine({
level,
subsystem,
message: consoleSettings.style === "json" ? message : consoleMessage,
style: consoleSettings.style,
meta: fileMeta
}));
};
return {
subsystem,
trace: (message, meta) => emit("trace", message, meta),
debug: (message, meta) => emit("debug", message, meta),
info: (message, meta) => emit("info", message, meta),
warn: (message, meta) => emit("warn", message, meta),
error: (message, meta) => emit("error", message, meta),
fatal: (message, meta) => emit("fatal", message, meta),
raw: (message) => {
logToFile(getFileLogger(), "info", message, { raw: true });
if (shouldLogSubsystemToConsole(subsystem)) {
if (!isVerbose() && subsystem === "agent/embedded" && /(sessionId|runId)=probe-/.test(message)) return;
writeConsoleLine("info", message);
}
},
child: (name) => createSubsystemLogger(`${subsystem}/${name}`)
};
}
function runtimeForLogger(logger, exit = defaultRuntime.exit) {
return {
log: (message) => logger.info(message),
error: (message) => logger.error(message),
exit
};
}
//#endregion
//#region src/utils/boolean.ts
const DEFAULT_TRUTHY = [
"true",
"1",
"yes",
"on"
];
const DEFAULT_FALSY = [
"false",
"0",
"no",
"off"
];
const DEFAULT_TRUTHY_SET = new Set(DEFAULT_TRUTHY);
const DEFAULT_FALSY_SET = new Set(DEFAULT_FALSY);
function parseBooleanValue(value, options = {}) {
if (typeof value === "boolean") return value;
if (typeof value !== "string") return;
const normalized = value.trim().toLowerCase();
if (!normalized) return;
const truthy = options.truthy ?? DEFAULT_TRUTHY;
const falsy = options.falsy ?? DEFAULT_FALSY;
const truthySet = truthy === DEFAULT_TRUTHY ? DEFAULT_TRUTHY_SET : new Set(truthy);
const falsySet = falsy === DEFAULT_FALSY ? DEFAULT_FALSY_SET : new Set(falsy);
if (truthySet.has(normalized)) return true;
if (falsySet.has(normalized)) return false;
}
//#endregion
//#region src/infra/env.ts
const log = createSubsystemLogger("env");
const loggedEnv = /* @__PURE__ */ new Set();
function formatEnvValue(value, redact) {
if (redact) return "<redacted>";
const singleLine = value.replace(/\s+/g, " ").trim();
if (singleLine.length <= 160) return singleLine;
return `${singleLine.slice(0, 160)}…`;
}
function logAcceptedEnvOption(option) {
if (process.env.VITEST || false) return;
if (loggedEnv.has(option.key)) return;
const rawValue = option.value ?? process.env[option.key];
if (!rawValue || !rawValue.trim()) return;
loggedEnv.add(option.key);
log.info(`env: ${option.key}=${formatEnvValue(rawValue, option.redact)} (${option.description})`);
}
function normalizeZaiEnv() {
if (!process.env.ZAI_API_KEY?.trim() && process.env.Z_AI_API_KEY?.trim()) process.env.ZAI_API_KEY = process.env.Z_AI_API_KEY;
}
function isTruthyEnvValue(value) {
return parseBooleanValue(value) === true;
}
function normalizeEnv() {
normalizeZaiEnv();
}
//#endregion
//#region src/infra/warnings.ts
const warningFilterKey = Symbol.for("openclaw.warning-filter");
function shouldIgnoreWarning(warning) {
if (warning.code === "DEP0040" && warning.message?.includes("punycode")) return true;
if (warning.code === "DEP0060" && warning.message?.includes("util._extend")) return true;
if (warning.name === "ExperimentalWarning" && warning.message?.includes("SQLite is an experimental feature")) return true;
return false;
}
function installProcessWarningFilter() {
const globalState = globalThis;
if (globalState[warningFilterKey]?.installed) return;
globalState[warningFilterKey] = { installed: true };
process.on("warning", (warning) => {
if (shouldIgnoreWarning(warning)) return;
process.stderr.write(`${warning.stack ?? warning.toString()}\n`);
});
}
//#endregion
//#region src/process/child-process-bridge.ts
const defaultSignals = process$1.platform === "win32" ? [
"SIGTERM",
"SIGINT",
"SIGBREAK"
] : [
"SIGTERM",
"SIGINT",
"SIGHUP",
"SIGQUIT"
];
function attachChildProcessBridge(child, { signals = defaultSignals, onSignal } = {}) {
const listeners = /* @__PURE__ */ new Map();
for (const signal of signals) {
const listener = () => {
onSignal?.(signal);
try {
child.kill(signal);
} catch {}
};
try {
process$1.on(signal, listener);
listeners.set(signal, listener);
} catch {}
}
const detach = () => {
for (const [signal, listener] of listeners) process$1.off(signal, listener);
listeners.clear();
};
child.once("exit", detach);
child.once("error", detach);
return { detach };
}
//#endregion
//#region src/entry.ts
process$1.title = "openclaw";
installProcessWarningFilter();
normalizeEnv();
if (process$1.argv.includes("--no-color")) {
process$1.env.NO_COLOR = "1";
process$1.env.FORCE_COLOR = "0";
}
const EXPERIMENTAL_WARNING_FLAG = "--disable-warning=ExperimentalWarning";
function hasExperimentalWarningSuppressed(nodeOptions) {
if (!nodeOptions) return false;
return nodeOptions.includes(EXPERIMENTAL_WARNING_FLAG) || nodeOptions.includes("--no-warnings");
}
function ensureExperimentalWarningSuppressed() {
if (isTruthyEnvValue(process$1.env.OPENCLAW_NO_RESPAWN)) return false;
if (isTruthyEnvValue(process$1.env.OPENCLAW_NODE_OPTIONS_READY)) return false;
const nodeOptions = process$1.env.NODE_OPTIONS ?? "";
if (hasExperimentalWarningSuppressed(nodeOptions)) return false;
process$1.env.OPENCLAW_NODE_OPTIONS_READY = "1";
process$1.env.NODE_OPTIONS = `${nodeOptions} ${EXPERIMENTAL_WARNING_FLAG}`.trim();
const child = spawn(process$1.execPath, [...process$1.execArgv, ...process$1.argv.slice(1)], {
stdio: "inherit",
env: process$1.env
});
attachChildProcessBridge(child);
child.once("exit", (code, signal) => {
if (signal) {
process$1.exitCode = 1;
return;
}
process$1.exit(code ?? 1);
});
child.once("error", (error) => {
console.error("[openclaw] Failed to respawn CLI:", error instanceof Error ? error.stack ?? error.message : error);
process$1.exit(1);
});
return true;
}
function normalizeWindowsArgv(argv) {
if (process$1.platform !== "win32") return argv;
if (argv.length < 2) return argv;
const stripControlChars = (value) => {
let out = "";
for (let i = 0; i < value.length; i += 1) {
const code = value.charCodeAt(i);
if (code >= 32 && code !== 127) out += value[i];
}
return out;
};
const normalizeArg = (value) => stripControlChars(value).replace(/^['"]+|['"]+$/g, "").trim();
const normalizeCandidate = (value) => normalizeArg(value).replace(/^\\\\\\?\\/, "");
const execPath = normalizeCandidate(process$1.execPath);
const execPathLower = execPath.toLowerCase();
const execBase = path.basename(execPath).toLowerCase();
const isExecPath = (value) => {
if (!value) return false;
const lower = normalizeCandidate(value).toLowerCase();
return lower === execPathLower || path.basename(lower) === execBase || lower.endsWith("\\node.exe") || lower.endsWith("/node.exe") || lower.includes("node.exe");
};
const next = [...argv];
for (let i = 1; i <= 3 && i < next.length;) {
if (isExecPath(next[i])) {
next.splice(i, 1);
continue;
}
i += 1;
}
const filtered = next.filter((arg, index) => index === 0 || !isExecPath(arg));
if (filtered.length < 3) return filtered;
const cleaned = [...filtered];
for (let i = 2; i < cleaned.length;) {
const arg = cleaned[i];
if (!arg || arg.startsWith("-")) {
i += 1;
continue;
}
if (isExecPath(arg)) {
cleaned.splice(i, 1);
continue;
}
break;
}
return cleaned;
}
process$1.argv = normalizeWindowsArgv(process$1.argv);
if (!ensureExperimentalWarningSuppressed()) {
const parsed = parseCliProfileArgs(process$1.argv);
if (!parsed.ok) {
console.error(`[openclaw] ${parsed.error}`);
process$1.exit(2);
}
if (parsed.profile) {
applyCliProfileEnv({ profile: parsed.profile });
process$1.argv = parsed.argv;
}
import("./run-main-B3krVvc1.js").then(({ runCli }) => runCli(process$1.argv)).catch((error) => {
console.error("[openclaw] Failed to start CLI:", error instanceof Error ? error.stack ?? error.message : error);
process$1.exitCode = 1;
});
}
//#endregion
export { DEFAULT_CHAT_CHANNEL as $, getChildLogger as A, resolveConfigPath as B, setVerbose as C, colorize as D, warn as E, CONFIG_PATH as F, resolveIsNixMode as G, resolveDefaultConfigCandidates as H, DEFAULT_GATEWAY_PORT as I, resolveOAuthDir as J, resolveLegacyStateDirs as K, STATE_DIR as L, getResolvedLoggerSettings as M, toPinoLikeLogger as N, isRich as O, normalizeLogLevel as P, CHAT_CHANNEL_ORDER as Q, isNixMode as R, logVerboseConsole as S, success as T, resolveGatewayLockDir as U, resolveConfigPathCandidate as V, resolveGatewayPort as W, resolveStateDir as X, resolveOAuthPath as Y, CHANNEL_IDS as Z, unregisterActiveProgressLine as _, parseBooleanValue as a, normalizeChannelId as at, isVerbose as b, enableConsoleCapture as c, requireActivePluginRegistry as ct, shouldLogSubsystemToConsole as d, formatChannelPrimerLine as et, visibleWidth as f, registerActiveProgressLine as g, clearActiveProgressLine as h, normalizeEnv as i, normalizeAnyChannelId as it, getLogger as j, theme as k, setConsoleSubsystemFilter as l, setActivePluginRegistry as lt, restoreTerminalState as m, isTruthyEnvValue as n, getChatChannelMeta as nt, createSubsystemLogger as o, normalizeChatChannelId as ot, defaultRuntime as p, resolveNewStateDir as q, logAcceptedEnvOption as r, listChatChannels as rt, runtimeForLogger as s, getActivePluginRegistry as st, installProcessWarningFilter as t, formatChannelSelectionLine as tt, setConsoleTimestampPrefix as u, normalizeProfileName as ut, danger as v, shouldLogVerbose as w, logVerbose as x, info as y, resolveCanonicalConfigPath as z };