UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

103 lines (102 loc) • 3.94 kB
import { c as normalizeOptionalLowercaseString, o as normalizeLowercaseStringOrEmpty } from "./string-coerce-CCIiB4H7.mjs"; import { t as getChatCommands } from "./commands-registry.data-CoxL3Db3.mjs"; //#region src/auto-reply/commands-registry-normalize.ts let cachedTextAliases; const ARGUMENT_PRESERVING_COMMAND_KEYS = /* @__PURE__ */ new Set([ "acp", "btw", "dashboard", "followup", "goal", "loop", "steer" ]); const TARGETED_COMMAND_BODY_RE = /^\/([^\s@]+)@([A-Za-z0-9_]+)(?=$|\s|[.!?!?…,,。;;::'"’”)\]}])([\s\S]*)$/u; function appendMultilineTail(e, t, n) { if (!t) return e; if (!n || n.command.key === "skill" || n.command.key === "learn") return `${e}\n${e.split(/\s+/, 3).length - 1 >= (n?.command.key === "skill" ? 2 : 1) && n?.command.key !== "learn" ? t : t.trimStart()}`; if (n.command.key === "reset") { const n = t.replace(/\s+/g, " ").trim(); return n ? `${e} ${n}` : e; } return e; } function getTextAliases() { if (cachedTextAliases) return cachedTextAliases; const e = /* @__PURE__ */ new Map(); for (const t of getChatCommands()) { const n = t.textAliases[0] ?? `/${t.key}`; const r = Boolean(t.acceptsArgs); for (const i of t.textAliases) if (!e.has(i)) e.set(i, { command: t, canonical: n, acceptsArgs: r }); } cachedTextAliases = e; return e; } /** Normalizes command text to canonical aliases, removing bot mentions when appropriate. */ function normalizeCommandBody(e, t) { const n = t?.preserveArguments ? e.trimStart() : e.trim(); if (!n.startsWith("/")) return n; const r = n.match(/^\/[^\s@:]+/u)?.[0]?.toLowerCase(); const i = r ? getTextAliases().get(r) : void 0; const a = t?.preserveArguments || i !== void 0 && ARGUMENT_PRESERVING_COMMAND_KEYS.has(i.command.key); const o = a ? -1 : n.indexOf("\n"); const s = o === -1 ? n : n.slice(0, o).trim(); const c = o === -1 ? void 0 : n.slice(o + 1); const l = s.replace(/^\/([^\s:]+)\s*:([\s\S]*)$/, (e, t, n) => { const r = a ? n : n.trimStart(); return r ? `/${t}${/^\s/.test(r) ? "" : " "}${r}` : `/${t}`; }); const u = normalizeOptionalLowercaseString(t?.botUsername); const d = l.match(TARGETED_COMMAND_BODY_RE); const f = d?.[2]?.toLowerCase(); const p = u !== void 0 && f === u; const m = u === void 0 && t?.targetedCommandMode === "pre-identity"; const h = d && (p || m) ? `/${d[1]}${d[3] ?? ""}` : l; const g = normalizeLowercaseStringOrEmpty(h); const _ = getTextAliases(); const v = _.get(g); if (v) return appendMultilineTail(v.canonical, c, v); const y = h.match(/^\/([^\s]+)(?:\s+([\s\S]+))?$/); if (!y) return appendMultilineTail(h, c); const [, b, x] = y; const S = `/${b?.toLowerCase()}`; const C = _.get(S); if (!C) return appendMultilineTail(h, c); if (x && !C.acceptsArgs) return h; const w = x?.trimStart(); return appendMultilineTail(a ? `${C.canonical}${h.slice(S.length)}` : w ? `${C.canonical} ${w}` : C.canonical, c, C); } /** Resolves a raw text command to the matching normalized alias when known. */ function maybeResolveTextAlias(e, t) { const n = normalizeCommandBody(e).trim(); if (!n.startsWith("/")) return null; const r = n.toLowerCase(); const i = getTextAliases(); const a = r.match(/^\/([^\s:]+)(?:\s|$)/); if (!a) return null; const o = `/${a[1]}`; const s = i.get(o); if (!s) return null; const c = r.slice(o.length); return !c || s.acceptsArgs || /^\s*:\s*$/.test(c) ? o : null; } /** Resolves a raw text command into its command definition and raw argument tail. */ function resolveTextCommand(e, t) { const n = normalizeCommandBody(e).trim(); const r = maybeResolveTextAlias(n, t); if (!r) return null; const i = getTextAliases().get(r); if (!i) return null; if (!i.acceptsArgs) return { command: i.command }; const a = n.slice(r.length).trim(); return { command: i.command, args: a || void 0 }; } //#endregion export { normalizeCommandBody as n, resolveTextCommand as r, maybeResolveTextAlias as t };