openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
64 lines (63 loc) • 3.31 kB
JavaScript
import { c as normalizeOptionalLowercaseString, o as normalizeLowercaseStringOrEmpty } from "./string-coerce-CCIiB4H7.mjs";
import { i as stripInboundMetadata } from "./strip-inbound-meta-BMUpr5S_.mjs";
import { n as listChatCommands, r as listChatCommandsForConfig } from "./commands-registry-list-6-6JFUDI.mjs";
import { n as normalizeCommandBody } from "./commands-registry-normalize-dI3NYF5y.mjs";
import { r as matchPluginCommand } from "./commands-oVh2_3bU.mjs";
import { t as isAbortTrigger } from "./abort-trigger-text-id3HRMGA.mjs";
//#region src/auto-reply/reply/bash-command-input.ts
/** Keeps Markdown image prefixes out of bash shorthand. */
function isBangCommandInput(e) {
const t = e?.trimStart() ?? "";
return t.startsWith("!") && !/^!\[[^\r\n]*\](?:\(|\[)/.test(t);
}
//#endregion
//#region src/auto-reply/command-detection.ts
/** Command detectors used by inbound authorization and control-command routing. */
function normalizeControlCommandBody(e, t) {
return normalizeCommandBody(stripInboundMetadata(e?.trim() ?? ""), t);
}
function hasNormalizedControlCommand(e, t) {
if (!e) return false;
const n = normalizeLowercaseStringOrEmpty(e);
return (t ? listChatCommandsForConfig(t) : listChatCommands()).some((t) => t.textAliases.some((r) => {
const i = normalizeOptionalLowercaseString(r);
return Boolean(i && (n === i || t.acceptsArgs && n.startsWith(i) && /\s/.test(e.charAt(i.length))));
}));
}
/** Returns true when text starts with a configured control command alias. */
function hasControlCommand(e, t, n) {
return hasNormalizedControlCommand(normalizeControlCommandBody(e, n), t);
}
/** Returns true for exact control commands or abort triggers after metadata stripping. */
function isControlCommandMessage(e, t, n) {
const r = normalizeControlCommandBody(e, n);
return hasNormalizedControlCommand(r, t) || isAbortTrigger(r);
}
/** Returns true when a command starts a new transcript rather than resetting in place. */
function isSessionBoundaryCommandText(e, t) {
const n = normalizeControlCommandBody(e, t);
return /^\/(?:new|reset)(?:\s|$)/i.test(n) && !/^\/reset\s+soft(?:\s|$)/i.test(n);
}
/**
* Coarse detection for inline directives/shortcuts (e.g. "hey /status") so channel monitors
* can decide whether to compute CommandAuthorized for a message.
*
* This intentionally errs on the side of false positives; CommandAuthorized only gates
* command/directive execution, not normal chat replies.
*/
function hasInlineCommandTokens(e) {
return /(?:^|\s)[/!][a-z]/i.test(e ?? "");
}
/** Detects command syntax without broadening it for channel authorization metadata. */
function hasCommandSyntax(e, t, n) {
if (isBangCommandInput(e) || isControlCommandMessage(e, t, n) || hasInlineCommandTokens(e)) return true;
const r = e?.match(/(?:^|\s)(\/\s+[a-z][\s\S]*)/i)?.[1];
return r ? matchPluginCommand(r) !== null : false;
}
/** Returns true when a message may need command authorization metadata. */
function shouldComputeCommandAuthorized(e, t, n) {
const r = e?.match(/(?:^|\s)(![\s\S]*)/)?.[1];
return hasCommandSyntax(e, t, n) || isBangCommandInput(r);
}
//#endregion
export { shouldComputeCommandAuthorized as a, isSessionBoundaryCommandText as i, hasInlineCommandTokens as n, isBangCommandInput as o, isControlCommandMessage as r, hasControlCommand as t };