openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
88 lines (87 loc) • 3.95 kB
JavaScript
import { t as redactIdentifier } from "./node-crypto-B6yuTjmm.mjs";
import { r as logVerbose } from "./globals-CgepmiqE.mjs";
import { n as isRestartEnabled, t as isCommandFlagEnabled } from "./commands.flags-bgyYi4vf.mjs";
import { l as resolveCommandTurnContext, o as isNativeCommandTurn } from "./command-turn-context-BaN4kAo-.mjs";
import { r as formatCommandOwnerHint } from "./doctor-command-owner-Dul2IrrD.mjs";
//#region src/auto-reply/reply/command-gates.ts
/** Builds the standard terminal response shared by chat command handlers. */
function commandReply(e) {
return {
shouldContinue: false,
reply: typeof e === "string" ? { text: e } : e
};
}
function renderCommandJsonBlock(e, t) {
return `${e}\n\`\`\`json\n${JSON.stringify(t, null, 2)}\n\`\`\``;
}
/** Returns command arguments only when the complete slash-command token matches. */
function matchCommandPrefix(e, t, n = {}) {
return e === t ? "" : e.startsWith(`${t} `) || n.allowColon && e.startsWith(`${t}:`) ? e.slice(t.length + 1).trim() : null;
}
/** Keeps matching, text-command enablement, and sender authorization in one owner. */
function defineAuthorizedTextCommand(e, t) {
return async (n, r) => {
if (!r) return null;
const i = e.match(n.command.commandBodyNormalized, n);
if (i === null) return null;
const a = rejectUnauthorizedCommand(n, e.label);
if (a) return e.silentUnauthorized ? { shouldContinue: false } : a;
return (typeof e.ownerOnly === "function" ? e.ownerOnly(n, i) : e.ownerOnly) ? rejectNonOwnerCommand(n, e.label) ?? t(n, i) : t(n, i);
};
}
function defineGatewayControlCommand(e, t) {
return defineAuthorizedTextCommand({
label: e,
match: (t) => t === e ? true : null,
ownerOnly: true,
silentUnauthorized: true
}, async (n) => {
if (!isRestartEnabled(n.cfg)) return commandReply(`⚠️ ${e} is disabled (commands.restart=false).`);
await n.opts?.turnAdoptionLifecycle?.onAdopted();
return rejectNonOwnerCommand(n, e) ?? t(n);
});
}
function rejectUnauthorizedCommand(e, t) {
if (e.command.isAuthorizedSender) return null;
logVerbose(`Ignoring ${t} from unauthorized sender: ${redactIdentifier(e.command.senderId)}`);
if (isNativeCommandTurn(resolveCommandTurnContext(e.ctx))) return commandReply("You are not authorized to use this command.");
return { shouldContinue: false };
}
function rejectNonOwnerCommand(e, t) {
if (e.command.senderIsOwner) {
try {
e.command.assertOwnerCurrent?.();
} catch {
return commandReply("Your owner authority changed; send a new request.");
}
return null;
}
logVerbose(`Ignoring ${t} from non-owner sender: ${redactIdentifier(e.command.senderId)}`);
if (!e.command.isAuthorizedSender) return rejectUnauthorizedCommand(e, t);
return commandReply(`You are not authorized to use this owner-only command. ${formatCommandOwnerHint({
cfg: e.cfg,
channel: e.command.channel,
id: e.command.senderId
})}`);
}
function requireGatewayClientScope(e, t) {
const n = e.ctx.GatewayClientScopes;
if (!Array.isArray(n)) return null;
if (t.allowedScopes.some((e) => n.includes(e))) return null;
logVerbose(`Ignoring ${t.label} from gateway client missing scope: ${t.allowedScopes.join(" or ")}`);
return commandReply(t.missingText);
}
function buildDisabledCommandReply(e) {
const t = e.disabledVerb ?? "is";
const n = e.docsUrl ? ` Docs: ${e.docsUrl}` : "";
return { text: `⚠️ ${e.label} ${t} disabled. Set commands.${e.configKey}=true to enable.${n}` };
}
function requireCommandFlagEnabled(e, t) {
if (isCommandFlagEnabled(e, t.configKey)) return null;
return {
shouldContinue: false,
reply: buildDisabledCommandReply(t)
};
}
//#endregion
export { matchCommandPrefix as a, renderCommandJsonBlock as c, defineGatewayControlCommand as i, requireCommandFlagEnabled as l, commandReply as n, rejectNonOwnerCommand as o, defineAuthorizedTextCommand as r, rejectUnauthorizedCommand as s, buildDisabledCommandReply as t, requireGatewayClientScope as u };