@gguf/claw
Version:
WhatsApp gateway CLI (Baileys web) with Pi RPC agent
342 lines (339 loc) • 10.2 kB
JavaScript
import { n as isTruthyEnvValue } from "./entry.js";
import { r as resolveActionArgs } from "./helpers-uGhPZ_kK.js";
//#region src/cli/argv.ts
const HELP_FLAGS = new Set(["-h", "--help"]);
const VERSION_FLAGS = new Set([
"-v",
"-V",
"--version"
]);
const FLAG_TERMINATOR = "--";
function hasHelpOrVersion(argv) {
return argv.some((arg) => HELP_FLAGS.has(arg) || VERSION_FLAGS.has(arg));
}
function isValueToken(arg) {
if (!arg) return false;
if (arg === FLAG_TERMINATOR) return false;
if (!arg.startsWith("-")) return true;
return /^-\d+(?:\.\d+)?$/.test(arg);
}
function parsePositiveInt(value) {
const parsed = Number.parseInt(value, 10);
if (Number.isNaN(parsed) || parsed <= 0) return;
return parsed;
}
function hasFlag(argv, name) {
const args = argv.slice(2);
for (const arg of args) {
if (arg === FLAG_TERMINATOR) break;
if (arg === name) return true;
}
return false;
}
function getFlagValue(argv, name) {
const args = argv.slice(2);
for (let i = 0; i < args.length; i += 1) {
const arg = args[i];
if (arg === FLAG_TERMINATOR) break;
if (arg === name) {
const next = args[i + 1];
return isValueToken(next) ? next : null;
}
if (arg.startsWith(`${name}=`)) {
const value = arg.slice(name.length + 1);
return value ? value : null;
}
}
}
function getVerboseFlag(argv, options) {
if (hasFlag(argv, "--verbose")) return true;
if (options?.includeDebug && hasFlag(argv, "--debug")) return true;
return false;
}
function getPositiveIntFlagValue(argv, name) {
const raw = getFlagValue(argv, name);
if (raw === null || raw === void 0) return raw;
return parsePositiveInt(raw);
}
function getCommandPath(argv, depth = 2) {
const args = argv.slice(2);
const path = [];
for (let i = 0; i < args.length; i += 1) {
const arg = args[i];
if (!arg) continue;
if (arg === "--") break;
if (arg.startsWith("-")) continue;
path.push(arg);
if (path.length >= depth) break;
}
return path;
}
function getPrimaryCommand(argv) {
const [primary] = getCommandPath(argv, 1);
return primary ?? null;
}
function buildParseArgv(params) {
const baseArgv = params.rawArgs && params.rawArgs.length > 0 ? params.rawArgs : params.fallbackArgv && params.fallbackArgv.length > 0 ? params.fallbackArgv : process.argv;
const programName = params.programName ?? "";
const normalizedArgv = programName && baseArgv[0] === programName ? baseArgv.slice(1) : baseArgv[0]?.endsWith("openclaw") ? baseArgv.slice(1) : baseArgv;
const executable = (normalizedArgv[0]?.split(/[/\\]/).pop() ?? "").toLowerCase();
if (normalizedArgv.length >= 2 && (isNodeExecutable(executable) || isBunExecutable(executable))) return normalizedArgv;
return [
"node",
programName || "openclaw",
...normalizedArgv
];
}
const nodeExecutablePattern = /^node-\d+(?:\.\d+)*(?:\.exe)?$/;
function isNodeExecutable(executable) {
return executable === "node" || executable === "node.exe" || executable === "nodejs" || executable === "nodejs.exe" || nodeExecutablePattern.test(executable);
}
function isBunExecutable(executable) {
return executable === "bun" || executable === "bun.exe";
}
//#endregion
//#region src/cli/program/register.subclis.ts
const shouldRegisterPrimaryOnly = (argv) => {
if (isTruthyEnvValue(process.env.OPENCLAW_DISABLE_LAZY_SUBCOMMANDS)) return false;
if (hasHelpOrVersion(argv)) return false;
return true;
};
const shouldEagerRegisterSubcommands = (_argv) => {
return isTruthyEnvValue(process.env.OPENCLAW_DISABLE_LAZY_SUBCOMMANDS);
};
const loadConfig = async () => {
return (await import("./config-jKcyRiMo.js")).loadConfig();
};
const entries = [
{
name: "acp",
description: "Agent Control Protocol tools",
register: async (program) => {
(await import("./acp-cli-DU_cVWbN.js")).registerAcpCli(program);
}
},
{
name: "gateway",
description: "Gateway control",
register: async (program) => {
(await import("./gateway-cli-BCC0T5iY.js")).registerGatewayCli(program);
}
},
{
name: "daemon",
description: "Gateway service (legacy alias)",
register: async (program) => {
(await import("./daemon-cli-D6FF4tmP.js")).registerDaemonCli(program);
}
},
{
name: "logs",
description: "Gateway logs",
register: async (program) => {
(await import("./logs-cli-nVwK5g60.js")).registerLogsCli(program);
}
},
{
name: "system",
description: "System events, heartbeat, and presence",
register: async (program) => {
(await import("./system-cli-B4FEIE5d.js")).registerSystemCli(program);
}
},
{
name: "models",
description: "Model configuration",
register: async (program) => {
(await import("./models-cli-n9a8pESx.js")).registerModelsCli(program);
}
},
{
name: "approvals",
description: "Exec approvals",
register: async (program) => {
(await import("./exec-approvals-cli-DD_z4fL9.js")).registerExecApprovalsCli(program);
}
},
{
name: "nodes",
description: "Node commands",
register: async (program) => {
(await import("./nodes-cli-ChBsNXzz.js")).registerNodesCli(program);
}
},
{
name: "devices",
description: "Device pairing + token management",
register: async (program) => {
(await import("./devices-cli-BVc_Ic5O.js")).registerDevicesCli(program);
}
},
{
name: "node",
description: "Node control",
register: async (program) => {
(await import("./node-cli-AOO0HsM1.js")).registerNodeCli(program);
}
},
{
name: "sandbox",
description: "Sandbox tools",
register: async (program) => {
(await import("./sandbox-cli-CnS9JJlO.js")).registerSandboxCli(program);
}
},
{
name: "tui",
description: "Terminal UI",
register: async (program) => {
(await import("./tui-cli-CVdJ28TX.js")).registerTuiCli(program);
}
},
{
name: "cron",
description: "Cron scheduler",
register: async (program) => {
(await import("./cron-cli-Bn_e3WGb.js")).registerCronCli(program);
}
},
{
name: "dns",
description: "DNS helpers",
register: async (program) => {
(await import("./dns-cli-B79COhmu.js")).registerDnsCli(program);
}
},
{
name: "docs",
description: "Docs helpers",
register: async (program) => {
(await import("./docs-cli-BM55jf4Y.js")).registerDocsCli(program);
}
},
{
name: "hooks",
description: "Hooks tooling",
register: async (program) => {
(await import("./hooks-cli-B-28F__A.js")).registerHooksCli(program);
}
},
{
name: "webhooks",
description: "Webhook helpers",
register: async (program) => {
(await import("./webhooks-cli-DkQGQ2ae.js")).registerWebhooksCli(program);
}
},
{
name: "pairing",
description: "Pairing helpers",
register: async (program) => {
const { registerPluginCliCommands } = await import("./cli-wFmvNjpg.js");
registerPluginCliCommands(program, await loadConfig());
(await import("./pairing-cli-DbP9KqPL.js")).registerPairingCli(program);
}
},
{
name: "plugins",
description: "Plugin management",
register: async (program) => {
(await import("./plugins-cli-VyQWn_LW.js")).registerPluginsCli(program);
const { registerPluginCliCommands } = await import("./cli-wFmvNjpg.js");
registerPluginCliCommands(program, await loadConfig());
}
},
{
name: "channels",
description: "Channel management",
register: async (program) => {
(await import("./channels-cli-CBGINubh.js")).registerChannelsCli(program);
}
},
{
name: "directory",
description: "Directory commands",
register: async (program) => {
(await import("./directory-cli-cc9ivzEM.js")).registerDirectoryCli(program);
}
},
{
name: "security",
description: "Security helpers",
register: async (program) => {
(await import("./security-cli-BGd4NO4l.js")).registerSecurityCli(program);
}
},
{
name: "skills",
description: "Skills management",
register: async (program) => {
(await import("./skills-cli-b0ZmGlmh.js")).registerSkillsCli(program);
}
},
{
name: "update",
description: "CLI update helpers",
register: async (program) => {
(await import("./update-cli-C5vwquH_.js")).registerUpdateCli(program);
}
},
{
name: "completion",
description: "Generate shell completion script",
register: async (program) => {
(await import("./completion-cli-BoF86Oeq.js")).registerCompletionCli(program);
}
}
];
function getSubCliEntries() {
return entries;
}
function removeCommand(program, command) {
const commands = program.commands;
const index = commands.indexOf(command);
if (index >= 0) commands.splice(index, 1);
}
async function registerSubCliByName(program, name) {
const entry = entries.find((candidate) => candidate.name === name);
if (!entry) return false;
const existing = program.commands.find((cmd) => cmd.name() === entry.name);
if (existing) removeCommand(program, existing);
await entry.register(program);
return true;
}
function registerLazyCommand(program, entry) {
const placeholder = program.command(entry.name).description(entry.description);
placeholder.allowUnknownOption(true);
placeholder.allowExcessArguments(true);
placeholder.action(async (...actionArgs) => {
removeCommand(program, placeholder);
await entry.register(program);
const actionCommand = actionArgs.at(-1);
const rawArgs = (actionCommand?.parent ?? program).rawArgs;
const actionArgsList = resolveActionArgs(actionCommand);
const fallbackArgv = actionCommand?.name() ? [actionCommand.name(), ...actionArgsList] : actionArgsList;
const parseArgv = buildParseArgv({
programName: program.name(),
rawArgs,
fallbackArgv
});
await program.parseAsync(parseArgv);
});
}
function registerSubCliCommands(program, argv = process.argv) {
if (shouldEagerRegisterSubcommands(argv)) {
for (const entry of entries) entry.register(program);
return;
}
const primary = getPrimaryCommand(argv);
if (primary && shouldRegisterPrimaryOnly(argv)) {
const entry = entries.find((candidate) => candidate.name === primary);
if (entry) {
registerLazyCommand(program, entry);
return;
}
}
for (const candidate of entries) registerLazyCommand(program, candidate);
}
//#endregion
export { getFlagValue as a, getVerboseFlag as c, getCommandPath as i, hasFlag as l, registerSubCliByName as n, getPositiveIntFlagValue as o, registerSubCliCommands as r, getPrimaryCommand as s, getSubCliEntries as t, hasHelpOrVersion as u };