@gguf/claw
Version:
Multi-channel AI gateway with extensible messaging integrations
1,274 lines (1,257 loc) • 39.3 kB
JavaScript
import { a as resolveOAuthDir, c as expandHomePrefix, l as resolveEffectiveHomeDir, n as resolveConfigPath, u as resolveRequiredHomeDir } from "./paths-DJmOcr7Q.js";
import path from "node:path";
import fs from "node:fs";
import os from "node:os";
import { Logger } from "tslog";
import JSON5 from "json5";
import chalk, { Chalk } from "chalk";
//#region src/plugins/http-path.ts
function normalizePluginHttpPath(path, fallback) {
const trimmed = path?.trim();
if (!trimmed) {
const fallbackTrimmed = fallback?.trim();
if (!fallbackTrimmed) return null;
return fallbackTrimmed.startsWith("/") ? fallbackTrimmed : `/${fallbackTrimmed}`;
}
return trimmed.startsWith("/") ? trimmed : `/${trimmed}`;
}
//#endregion
//#region src/hooks/internal-hooks.ts
/** Registry of hook handlers by event key */
const handlers = /* @__PURE__ */ new Map();
/**
* Register a hook handler for a specific event type or event:action combination
*
* @param eventKey - Event type (e.g., 'command') or specific action (e.g., 'command:new')
* @param handler - Function to call when the event is triggered
*
* @example
* ```ts
* // Listen to all command events
* registerInternalHook('command', async (event) => {
* console.log('Command:', event.action);
* });
*
* // Listen only to /new commands
* registerInternalHook('command:new', async (event) => {
* await saveSessionToMemory(event);
* });
* ```
*/
function registerInternalHook(eventKey, handler) {
if (!handlers.has(eventKey)) handlers.set(eventKey, []);
handlers.get(eventKey).push(handler);
}
/**
* Trigger a hook event
*
* Calls all handlers registered for:
* 1. The general event type (e.g., 'command')
* 2. The specific event:action combination (e.g., 'command:new')
*
* Handlers are called in registration order. Errors are caught and logged
* but don't prevent other handlers from running.
*
* @param event - The event to trigger
*/
async function triggerInternalHook(event) {
const typeHandlers = handlers.get(event.type) ?? [];
const specificHandlers = handlers.get(`${event.type}:${event.action}`) ?? [];
const allHandlers = [...typeHandlers, ...specificHandlers];
if (allHandlers.length === 0) return;
for (const handler of allHandlers) try {
await handler(event);
} catch (err) {
console.error(`Hook error [${event.type}:${event.action}]:`, err instanceof Error ? err.message : String(err));
}
}
/**
* Create a hook event with common fields filled in
*
* @param type - The event type
* @param action - The action within that type
* @param sessionKey - The session key
* @param context - Additional context
*/
function createInternalHookEvent(type, action, sessionKey, context = {}) {
return {
type,
action,
sessionKey,
context,
timestamp: /* @__PURE__ */ new Date(),
messages: []
};
}
//#endregion
//#region src/infra/tmp-openclaw-dir.ts
const POSIX_OPENCLAW_TMP_DIR = "/tmp/openclaw";
function isNodeErrorWithCode(err, code) {
return typeof err === "object" && err !== null && "code" in err && err.code === code;
}
function resolvePreferredOpenClawTmpDir(options = {}) {
const accessSync = options.accessSync ?? fs.accessSync;
const lstatSync = options.lstatSync ?? fs.lstatSync;
const mkdirSync = options.mkdirSync ?? fs.mkdirSync;
const getuid = options.getuid ?? (() => {
try {
return typeof process.getuid === "function" ? process.getuid() : void 0;
} catch {
return;
}
});
const tmpdir = options.tmpdir ?? os.tmpdir;
const uid = getuid();
const isSecureDirForUser = (st) => {
if (uid === void 0) return true;
if (typeof st.uid === "number" && st.uid !== uid) return false;
if (typeof st.mode === "number" && (st.mode & 18) !== 0) return false;
return true;
};
const fallback = () => {
const base = tmpdir();
const suffix = uid === void 0 ? "openclaw" : `openclaw-${uid}`;
return path.join(base, suffix);
};
try {
const preferred = lstatSync(POSIX_OPENCLAW_TMP_DIR);
if (!preferred.isDirectory() || preferred.isSymbolicLink()) return fallback();
accessSync(POSIX_OPENCLAW_TMP_DIR, fs.constants.W_OK | fs.constants.X_OK);
if (!isSecureDirForUser(preferred)) return fallback();
return POSIX_OPENCLAW_TMP_DIR;
} catch (err) {
if (!isNodeErrorWithCode(err, "ENOENT")) return fallback();
}
try {
accessSync("/tmp", fs.constants.W_OK | fs.constants.X_OK);
mkdirSync(POSIX_OPENCLAW_TMP_DIR, {
recursive: true,
mode: 448
});
try {
const preferred = lstatSync(POSIX_OPENCLAW_TMP_DIR);
if (!preferred.isDirectory() || preferred.isSymbolicLink()) return fallback();
if (!isSecureDirForUser(preferred)) return fallback();
} catch {
return fallback();
}
return POSIX_OPENCLAW_TMP_DIR;
} catch {
return fallback();
}
}
//#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,
streamErrorHandlersInstalled: false,
rawConsole: null
};
//#endregion
//#region src/logging/logger.ts
const DEFAULT_LOG_DIR = resolvePreferredOpenClawTmpDir();
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;
function resolveNodeRequire() {
const getBuiltinModule = process.getBuiltinModule;
if (typeof getBuiltinModule !== "function") return null;
try {
const moduleNamespace = getBuiltinModule("module");
return typeof moduleNamespace.createRequire === "function" ? moduleNamespace.createRequire : null;
} catch {
return null;
}
}
const requireConfig = resolveNodeRequire()?.(import.meta.url) ?? null;
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?.("../config/config.js"))?.loadConfig?.().logging;
} catch {
cfg = void 0;
}
const defaultLevel = process.env.VITEST === "true" && process.env.OPENCLAW_TEST_FILE_LOG !== "1" ? "silent" : "info";
return {
level: normalizeLogLevel(cfg?.level, defaultLevel),
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 registerLogTransport(transport) {
externalTransports.add(transport);
const logger = loggingState.cachedLogger;
if (logger) attachExternalTransport(logger, transport);
return () => {
externalTransports.delete(transport);
};
}
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/infra/plain-object.ts
/**
* Strict plain-object guard (excludes arrays and host objects).
*/
function isPlainObject(value) {
return typeof value === "object" && value !== null && !Array.isArray(value) && Object.prototype.toString.call(value) === "[object Object]";
}
//#endregion
//#region src/utils.ts
async function ensureDir(dir) {
await fs.promises.mkdir(dir, { recursive: true });
}
/**
* Check if a file or directory exists at the given path.
*/
async function pathExists(targetPath) {
try {
await fs.promises.access(targetPath);
return true;
} catch {
return false;
}
}
function clampNumber(value, min, max) {
return Math.max(min, Math.min(max, value));
}
function clampInt(value, min, max) {
return clampNumber(Math.floor(value), min, max);
}
/** Alias for clampNumber (shorter, more common name) */
const clamp = clampNumber;
/**
* Escapes special regex characters in a string so it can be used in a RegExp constructor.
*/
function escapeRegExp(value) {
return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
}
/**
* Safely parse JSON, returning null on error instead of throwing.
*/
function safeParseJson(raw) {
try {
return JSON.parse(raw);
} catch {
return null;
}
}
/**
* Type guard for Record<string, unknown> (less strict than isPlainObject).
* Accepts any non-null object that isn't an array.
*/
function isRecord(value) {
return typeof value === "object" && value !== null && !Array.isArray(value);
}
function normalizeE164(number) {
const digits = number.replace(/^whatsapp:/, "").trim().replace(/[^\d+]/g, "");
if (digits.startsWith("+")) return `+${digits.slice(1)}`;
return `+${digits}`;
}
/**
* "Self-chat mode" heuristic (single phone): the gateway is logged in as the owner's own WhatsApp account,
* and `channels.whatsapp.allowFrom` includes that same number. Used to avoid side-effects that make no sense when the
* "bot" and the human are the same WhatsApp identity (e.g. auto read receipts, @mention JID triggers).
*/
function isSelfChatMode(selfE164, allowFrom) {
if (!selfE164) return false;
if (!Array.isArray(allowFrom) || allowFrom.length === 0) return false;
const normalizedSelf = normalizeE164(selfE164);
return allowFrom.some((n) => {
if (n === "*") return false;
try {
return normalizeE164(String(n)) === normalizedSelf;
} catch {
return false;
}
});
}
function toWhatsappJid(number) {
const withoutPrefix = number.replace(/^whatsapp:/, "").trim();
if (withoutPrefix.includes("@")) return withoutPrefix;
return `${normalizeE164(withoutPrefix).replace(/\D/g, "")}@s.whatsapp.net`;
}
function resolveLidMappingDirs(opts) {
const dirs = /* @__PURE__ */ new Set();
const addDir = (dir) => {
if (!dir) return;
dirs.add(resolveUserPath(dir));
};
addDir(opts?.authDir);
for (const dir of opts?.lidMappingDirs ?? []) addDir(dir);
addDir(resolveOAuthDir());
addDir(path.join(CONFIG_DIR, "credentials"));
return [...dirs];
}
function readLidReverseMapping(lid, opts) {
const mappingFilename = `lid-mapping-${lid}_reverse.json`;
const mappingDirs = resolveLidMappingDirs(opts);
for (const dir of mappingDirs) {
const mappingPath = path.join(dir, mappingFilename);
try {
const data = fs.readFileSync(mappingPath, "utf8");
const phone = JSON.parse(data);
if (phone === null || phone === void 0) continue;
return normalizeE164(String(phone));
} catch {}
}
return null;
}
function jidToE164(jid, opts) {
const match = jid.match(/^(\d+)(?::\d+)?@(s\.whatsapp\.net|hosted)$/);
if (match) return `+${match[1]}`;
const lidMatch = jid.match(/^(\d+)(?::\d+)?@(lid|hosted\.lid)$/);
if (lidMatch) {
const lid = lidMatch[1];
const phone = readLidReverseMapping(lid, opts);
if (phone) return phone;
if (opts?.logMissing ?? shouldLogVerbose()) logVerbose(`LID mapping not found for ${lid}; skipping inbound message`);
}
return null;
}
async function resolveJidToE164(jid, opts) {
if (!jid) return null;
const direct = jidToE164(jid, opts);
if (direct) return direct;
if (!/(@lid|@hosted\.lid)$/.test(jid)) return null;
if (!opts?.lidLookup?.getPNForLID) return null;
try {
const pnJid = await opts.lidLookup.getPNForLID(jid);
if (!pnJid) return null;
return jidToE164(pnJid, opts);
} catch (err) {
if (shouldLogVerbose()) logVerbose(`LID mapping lookup failed for ${jid}: ${String(err)}`);
return null;
}
}
function sleep(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
function isHighSurrogate(codeUnit) {
return codeUnit >= 55296 && codeUnit <= 56319;
}
function isLowSurrogate(codeUnit) {
return codeUnit >= 56320 && codeUnit <= 57343;
}
function sliceUtf16Safe(input, start, end) {
const len = input.length;
let from = start < 0 ? Math.max(len + start, 0) : Math.min(start, len);
let to = end === void 0 ? len : end < 0 ? Math.max(len + end, 0) : Math.min(end, len);
if (to < from) {
const tmp = from;
from = to;
to = tmp;
}
if (from > 0 && from < len) {
if (isLowSurrogate(input.charCodeAt(from)) && isHighSurrogate(input.charCodeAt(from - 1))) from += 1;
}
if (to > 0 && to < len) {
if (isHighSurrogate(input.charCodeAt(to - 1)) && isLowSurrogate(input.charCodeAt(to))) to -= 1;
}
return input.slice(from, to);
}
function truncateUtf16Safe(input, maxLen) {
const limit = Math.max(0, Math.floor(maxLen));
if (input.length <= limit) return input;
return sliceUtf16Safe(input, 0, limit);
}
function resolveUserPath(input) {
const trimmed = input.trim();
if (!trimmed) return trimmed;
if (trimmed.startsWith("~")) {
const expanded = expandHomePrefix(trimmed, {
home: resolveRequiredHomeDir(process.env, os.homedir),
env: process.env,
homedir: os.homedir
});
return path.resolve(expanded);
}
return path.resolve(trimmed);
}
function resolveConfigDir(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 = path.join(resolveRequiredHomeDir(env, homedir), ".openclaw");
try {
if (fs.existsSync(newDir)) return newDir;
} catch {}
return newDir;
}
function resolveHomeDir() {
return resolveEffectiveHomeDir(process.env, os.homedir);
}
function resolveHomeDisplayPrefix() {
const home = resolveHomeDir();
if (!home) return;
if (process.env.OPENCLAW_HOME?.trim()) return {
home,
prefix: "$OPENCLAW_HOME"
};
return {
home,
prefix: "~"
};
}
function shortenHomePath(input) {
if (!input) return input;
const display = resolveHomeDisplayPrefix();
if (!display) return input;
const { home, prefix } = display;
if (input === home) return prefix;
if (input.startsWith(`${home}/`) || input.startsWith(`${home}\\`)) return `${prefix}${input.slice(home.length)}`;
return input;
}
function shortenHomeInString(input) {
if (!input) return input;
const display = resolveHomeDisplayPrefix();
if (!display) return input;
return input.split(display.home).join(display.prefix);
}
function formatTerminalLink(label, url, opts) {
const esc = "\x1B";
const safeLabel = label.replaceAll(esc, "");
const safeUrl = url.replaceAll(esc, "");
if (!(opts?.force === true ? true : opts?.force === false ? false : Boolean(process.stdout.isTTY))) return opts?.fallback ?? `${safeLabel} (${safeUrl})`;
return `\u001b]8;;${safeUrl}\u0007${safeLabel}\u001b]8;;\u0007`;
}
const CONFIG_DIR = resolveConfigDir();
//#endregion
//#region src/plugins/commands.ts
const pluginCommands = /* @__PURE__ */ new Map();
let registryLocked = false;
const MAX_ARGS_LENGTH = 4096;
/**
* Reserved command names that plugins cannot override.
* These are built-in commands from commands-registry.data.ts.
*/
const RESERVED_COMMANDS = new Set([
"help",
"commands",
"status",
"whoami",
"context",
"stop",
"restart",
"reset",
"new",
"compact",
"config",
"debug",
"allowlist",
"activation",
"skill",
"subagents",
"kill",
"steer",
"tell",
"model",
"models",
"queue",
"send",
"bash",
"exec",
"think",
"verbose",
"reasoning",
"elevated",
"usage"
]);
/**
* Validate a command name.
* Returns an error message if invalid, or null if valid.
*/
function validateCommandName(name) {
const trimmed = name.trim().toLowerCase();
if (!trimmed) return "Command name cannot be empty";
if (!/^[a-z][a-z0-9_-]*$/.test(trimmed)) return "Command name must start with a letter and contain only letters, numbers, hyphens, and underscores";
if (RESERVED_COMMANDS.has(trimmed)) return `Command name "${trimmed}" is reserved by a built-in command`;
return null;
}
/**
* Register a plugin command.
* Returns an error if the command name is invalid or reserved.
*/
function registerPluginCommand(pluginId, command) {
if (registryLocked) return {
ok: false,
error: "Cannot register commands while processing is in progress"
};
if (typeof command.handler !== "function") return {
ok: false,
error: "Command handler must be a function"
};
const validationError = validateCommandName(command.name);
if (validationError) return {
ok: false,
error: validationError
};
const key = `/${command.name.toLowerCase()}`;
if (pluginCommands.has(key)) {
const existing = pluginCommands.get(key);
return {
ok: false,
error: `Command "${command.name}" already registered by plugin "${existing.pluginId}"`
};
}
pluginCommands.set(key, {
...command,
pluginId
});
logVerbose(`Registered plugin command: ${key} (plugin: ${pluginId})`);
return { ok: true };
}
/**
* Clear all registered plugin commands.
* Called during plugin reload.
*/
function clearPluginCommands() {
pluginCommands.clear();
}
/**
* Check if a command body matches a registered plugin command.
* Returns the command definition and parsed args if matched.
*
* Note: If a command has `acceptsArgs: false` and the user provides arguments,
* the command will not match. This allows the message to fall through to
* built-in handlers or the agent. Document this behavior to plugin authors.
*/
function matchPluginCommand(commandBody) {
const trimmed = commandBody.trim();
if (!trimmed.startsWith("/")) return null;
const spaceIndex = trimmed.indexOf(" ");
const commandName = spaceIndex === -1 ? trimmed : trimmed.slice(0, spaceIndex);
const args = spaceIndex === -1 ? void 0 : trimmed.slice(spaceIndex + 1).trim();
const key = commandName.toLowerCase();
const command = pluginCommands.get(key);
if (!command) return null;
if (args && !command.acceptsArgs) return null;
return {
command,
args: args || void 0
};
}
/**
* Sanitize command arguments to prevent injection attacks.
* Removes control characters and enforces length limits.
*/
function sanitizeArgs(args) {
if (!args) return;
if (args.length > MAX_ARGS_LENGTH) return args.slice(0, MAX_ARGS_LENGTH);
let sanitized = "";
for (const char of args) {
const code = char.charCodeAt(0);
if (!(code <= 31 && code !== 9 && code !== 10 || code === 127)) sanitized += char;
}
return sanitized;
}
/**
* Execute a plugin command handler.
*
* Note: Plugin authors should still validate and sanitize ctx.args for their
* specific use case. This function provides basic defense-in-depth sanitization.
*/
async function executePluginCommand(params) {
const { command, args, senderId, channel, isAuthorizedSender, commandBody, config } = params;
if (command.requireAuth !== false && !isAuthorizedSender) {
logVerbose(`Plugin command /${command.name} blocked: unauthorized sender ${senderId || "<unknown>"}`);
return { text: "⚠️ This command requires authorization." };
}
const sanitizedArgs = sanitizeArgs(args);
const ctx = {
senderId,
channel,
channelId: params.channelId,
isAuthorizedSender,
args: sanitizedArgs,
commandBody,
config,
from: params.from,
to: params.to,
accountId: params.accountId,
messageThreadId: params.messageThreadId
};
registryLocked = true;
try {
const result = await command.handler(ctx);
logVerbose(`Plugin command /${command.name} executed successfully for ${senderId || "unknown"}`);
return result;
} catch (err) {
const error = err;
logVerbose(`Plugin command /${command.name} error: ${error.message}`);
return { text: "⚠️ Command failed. Please try again later." };
} finally {
registryLocked = false;
}
}
/**
* List all registered plugin commands.
* Used for /help and /commands output.
*/
function listPluginCommands() {
return Array.from(pluginCommands.values()).map((cmd) => ({
name: cmd.name,
description: cmd.description,
pluginId: cmd.pluginId
}));
}
/**
* Get plugin command specs for native command registration (e.g., Telegram).
*/
function getPluginCommandSpecs() {
return Array.from(pluginCommands.values()).map((cmd) => ({
name: cmd.name,
description: cmd.description
}));
}
//#endregion
//#region src/plugins/registry.ts
function createEmptyPluginRegistry() {
return {
plugins: [],
tools: [],
hooks: [],
typedHooks: [],
channels: [],
providers: [],
gatewayHandlers: {},
httpHandlers: [],
httpRoutes: [],
cliRegistrars: [],
services: [],
commands: [],
diagnostics: []
};
}
function createPluginRegistry(registryParams) {
const registry = createEmptyPluginRegistry();
const coreGatewayMethods = new Set(Object.keys(registryParams.coreGatewayHandlers ?? {}));
const pushDiagnostic = (diag) => {
registry.diagnostics.push(diag);
};
const registerTool = (record, tool, opts) => {
const names = opts?.names ?? (opts?.name ? [opts.name] : []);
const optional = opts?.optional === true;
const factory = typeof tool === "function" ? tool : (_ctx) => tool;
if (typeof tool !== "function") names.push(tool.name);
const normalized = names.map((name) => name.trim()).filter(Boolean);
if (normalized.length > 0) record.toolNames.push(...normalized);
registry.tools.push({
pluginId: record.id,
factory,
names: normalized,
optional,
source: record.source
});
};
const registerHook = (record, events, handler, opts, config) => {
const normalizedEvents = (Array.isArray(events) ? events : [events]).map((event) => event.trim()).filter(Boolean);
const entry = opts?.entry ?? null;
const name = entry?.hook.name ?? opts?.name?.trim();
if (!name) {
pushDiagnostic({
level: "warn",
pluginId: record.id,
source: record.source,
message: "hook registration missing name"
});
return;
}
const description = entry?.hook.description ?? opts?.description ?? "";
const hookEntry = entry ? {
...entry,
hook: {
...entry.hook,
name,
description,
source: "openclaw-plugin",
pluginId: record.id
},
metadata: {
...entry.metadata,
events: normalizedEvents
}
} : {
hook: {
name,
description,
source: "openclaw-plugin",
pluginId: record.id,
filePath: record.source,
baseDir: path.dirname(record.source),
handlerPath: record.source
},
frontmatter: {},
metadata: { events: normalizedEvents },
invocation: { enabled: true }
};
record.hookNames.push(name);
registry.hooks.push({
pluginId: record.id,
entry: hookEntry,
events: normalizedEvents,
source: record.source
});
if (!(config?.hooks?.internal?.enabled === true) || opts?.register === false) return;
for (const event of normalizedEvents) registerInternalHook(event, handler);
};
const registerGatewayMethod = (record, method, handler) => {
const trimmed = method.trim();
if (!trimmed) return;
if (coreGatewayMethods.has(trimmed) || registry.gatewayHandlers[trimmed]) {
pushDiagnostic({
level: "error",
pluginId: record.id,
source: record.source,
message: `gateway method already registered: ${trimmed}`
});
return;
}
registry.gatewayHandlers[trimmed] = handler;
record.gatewayMethods.push(trimmed);
};
const registerHttpHandler = (record, handler) => {
record.httpHandlers += 1;
registry.httpHandlers.push({
pluginId: record.id,
handler,
source: record.source
});
};
const registerHttpRoute = (record, params) => {
const normalizedPath = normalizePluginHttpPath(params.path);
if (!normalizedPath) {
pushDiagnostic({
level: "warn",
pluginId: record.id,
source: record.source,
message: "http route registration missing path"
});
return;
}
if (registry.httpRoutes.some((entry) => entry.path === normalizedPath)) {
pushDiagnostic({
level: "error",
pluginId: record.id,
source: record.source,
message: `http route already registered: ${normalizedPath}`
});
return;
}
record.httpHandlers += 1;
registry.httpRoutes.push({
pluginId: record.id,
path: normalizedPath,
handler: params.handler,
source: record.source
});
};
const registerChannel = (record, registration) => {
const normalized = typeof registration.plugin === "object" ? registration : { plugin: registration };
const plugin = normalized.plugin;
const id = typeof plugin?.id === "string" ? plugin.id.trim() : String(plugin?.id ?? "").trim();
if (!id) {
pushDiagnostic({
level: "error",
pluginId: record.id,
source: record.source,
message: "channel registration missing id"
});
return;
}
record.channelIds.push(id);
registry.channels.push({
pluginId: record.id,
plugin,
dock: normalized.dock,
source: record.source
});
};
const registerProvider = (record, provider) => {
const id = typeof provider?.id === "string" ? provider.id.trim() : "";
if (!id) {
pushDiagnostic({
level: "error",
pluginId: record.id,
source: record.source,
message: "provider registration missing id"
});
return;
}
const existing = registry.providers.find((entry) => entry.provider.id === id);
if (existing) {
pushDiagnostic({
level: "error",
pluginId: record.id,
source: record.source,
message: `provider already registered: ${id} (${existing.pluginId})`
});
return;
}
record.providerIds.push(id);
registry.providers.push({
pluginId: record.id,
provider,
source: record.source
});
};
const registerCli = (record, registrar, opts) => {
const commands = (opts?.commands ?? []).map((cmd) => cmd.trim()).filter(Boolean);
record.cliCommands.push(...commands);
registry.cliRegistrars.push({
pluginId: record.id,
register: registrar,
commands,
source: record.source
});
};
const registerService = (record, service) => {
const id = service.id.trim();
if (!id) return;
record.services.push(id);
registry.services.push({
pluginId: record.id,
service,
source: record.source
});
};
const registerCommand = (record, command) => {
const name = command.name.trim();
if (!name) {
pushDiagnostic({
level: "error",
pluginId: record.id,
source: record.source,
message: "command registration missing name"
});
return;
}
const result = registerPluginCommand(record.id, command);
if (!result.ok) {
pushDiagnostic({
level: "error",
pluginId: record.id,
source: record.source,
message: `command registration failed: ${result.error}`
});
return;
}
record.commands.push(name);
registry.commands.push({
pluginId: record.id,
command,
source: record.source
});
};
const registerTypedHook = (record, hookName, handler, opts) => {
record.hookCount += 1;
registry.typedHooks.push({
pluginId: record.id,
hookName,
handler,
priority: opts?.priority,
source: record.source
});
};
const normalizeLogger = (logger) => ({
info: logger.info,
warn: logger.warn,
error: logger.error,
debug: logger.debug
});
const createApi = (record, params) => {
return {
id: record.id,
name: record.name,
version: record.version,
description: record.description,
source: record.source,
config: params.config,
pluginConfig: params.pluginConfig,
runtime: registryParams.runtime,
logger: normalizeLogger(registryParams.logger),
registerTool: (tool, opts) => registerTool(record, tool, opts),
registerHook: (events, handler, opts) => registerHook(record, events, handler, opts, params.config),
registerHttpHandler: (handler) => registerHttpHandler(record, handler),
registerHttpRoute: (params) => registerHttpRoute(record, params),
registerChannel: (registration) => registerChannel(record, registration),
registerProvider: (provider) => registerProvider(record, provider),
registerGatewayMethod: (method, handler) => registerGatewayMethod(record, method, handler),
registerCli: (registrar, opts) => registerCli(record, registrar, opts),
registerService: (service) => registerService(record, service),
registerCommand: (command) => registerCommand(record, command),
resolvePath: (input) => resolveUserPath(input),
on: (hookName, handler, opts) => registerTypedHook(record, hookName, handler, opts)
};
};
return {
registry,
createApi,
pushDiagnostic,
registerTool,
registerChannel,
registerProvider,
registerGatewayMethod,
registerCli,
registerService,
registerCommand,
registerHook,
registerTypedHook
};
}
//#endregion
//#region src/plugins/runtime.ts
const REGISTRY_STATE = Symbol.for("openclaw.pluginRegistryState");
const state = (() => {
const globalState = globalThis;
if (!globalState[REGISTRY_STATE]) globalState[REGISTRY_STATE] = {
registry: createEmptyPluginRegistry(),
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 = createEmptyPluginRegistry();
return state.registry;
}
//#endregion
//#region src/channels/registry.ts
const CHAT_CHANNEL_ORDER = [
"telegram",
"whatsapp",
"discord",
"irc",
"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"
},
irc: {
id: "irc",
label: "IRC",
selectionLabel: "IRC (Server + Nick)",
detailLabel: "IRC",
docsPath: "/channels/irc",
docsLabel: "irc",
blurb: "classic IRC networks with DM/channel routing and pairing controls.",
systemImage: "network"
},
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",
"internet-relay-chat": "irc",
"google-chat": "googlechat",
gchat: "googlechat"
};
const normalizeChannelKey = (raw) => {
return raw?.trim().toLowerCase() || void 0;
};
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;
}
//#endregion
export { getLogger as $, resolveJidToE164 as A, danger as B, formatTerminalLink as C, normalizeE164 as D, jidToE164 as E, sleep as F, setVerbose as G, isVerbose as H, sliceUtf16Safe as I, warn as J, shouldLogVerbose as K, toWhatsappJid as L, safeParseJson as M, shortenHomeInString as N, pathExists as O, shortenHomePath as P, getChildLogger as Q, truncateUtf16Safe as R, escapeRegExp as S, isSelfChatMode as T, logVerbose as U, info as V, logVerboseConsole as W, isRich as X, colorize as Y, theme as Z, CONFIG_DIR as _, normalizeAnyChannelId as a, normalizeLogLevel as at, clampNumber as b, getActivePluginRegistry as c, createInternalHookEvent as ct, createPluginRegistry as d, isFileLogLevelEnabled as et, clearPluginCommands as f, matchPluginCommand as g, listPluginCommands as h, getChatChannelMeta as i, levelToMinLevel as it, resolveUserPath as j, resolveConfigDir as k, requireActivePluginRegistry as l, triggerInternalHook as lt, getPluginCommandSpecs as m, CHAT_CHANNEL_ORDER as n, toPinoLikeLogger as nt, normalizeChannelId as o, readLoggingConfig as ot, executePluginCommand as p, success as q, DEFAULT_CHAT_CHANNEL as r, loggingState as rt, normalizeChatChannelId as s, resolvePreferredOpenClawTmpDir as st, CHANNEL_IDS as t, registerLogTransport as tt, setActivePluginRegistry as u, normalizePluginHttpPath as ut, clamp as v, isRecord as w, ensureDir as x, clampInt as y, isPlainObject as z };