openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
63 lines (62 loc) • 2.78 kB
JavaScript
import { c as normalizeOptionalLowercaseString } from "./string-coerce-CCIiB4H7.mjs";
import { n as normalizeMessageChannel } from "./message-channel-core-BbYZT1a-.mjs";
import { r as listRuntimeVisibleChannelPlugins } from "./runtime-visible-channels-CE896D4p.mjs";
//#region src/infra/outbound/channel-target-prefix.ts
const TARGET_KIND_PREFIXES = [
"channel",
"conversation",
"dm",
"group",
"room",
"thread",
"user"
];
const TARGET_KIND_PATTERN = new RegExp(`^(${TARGET_KIND_PREFIXES.join("|")}):`, "i");
/** Removes a selected channel/provider prefix from an outbound target string. */
function stripTargetProviderPrefix(e, ...t) {
const n = e.trim();
const r = n.toLowerCase();
for (const e of t) {
const t = normalizeOptionalLowercaseString(e);
if (t && r.startsWith(`${t}:`)) return n.slice(t.length + 1).trim();
}
return n;
}
/** Removes generic target-kind prefixes such as room:, thread:, or user:. */
function stripOutboundTargetKindPrefix(e, t = TARGET_KIND_PREFIXES) {
if (t === TARGET_KIND_PREFIXES) return e.replace(TARGET_KIND_PATTERN, "").trim();
const n = t.map((e) => normalizeOptionalLowercaseString(e)).filter((e) => Boolean(e)).join("|");
return n ? e.replace(new RegExp(`^(${n}):`, "i"), "").trim() : e.trim();
}
/** Strips plugin topic suffixes while preserving ordinary colon-containing targets. */
function stripTargetTopicSuffix(e, t = {}) {
const n = e.trim();
return (t.allowNumericShorthand ? /^(-?\d+):(\d+)$/.exec(n) : null)?.[1] ?? n.replace(/:topic:.*$/i, "").trim();
}
function resolveChannelTargetProviderPrefix(e) {
const t = /^\s*([a-z][a-z0-9_-]*):/i.exec(e ?? "");
const n = normalizeOptionalLowercaseString(t?.[1]);
if (!n || TARGET_KIND_PREFIXES.includes(n)) return;
for (const e of listRuntimeVisibleChannelPlugins()) {
const t = normalizeOptionalLowercaseString(e.id);
const r = e.messaging?.targetPrefixes ?? [];
if (t && r.some((e) => normalizeOptionalLowercaseString(e) === n)) return {
prefix: n,
channel: t
};
}
}
/** Resolves the channel implied by a plugin-owned target prefix, if any. */
function resolveTargetPrefixedChannel(e) {
return resolveChannelTargetProviderPrefix(e)?.channel;
}
/** Rejects targets whose plugin-owned prefix belongs to a different selected channel. */
function validateTargetProviderPrefix(e) {
const t = normalizeMessageChannel(e.channel);
if (!t || t === "last") return;
const n = resolveChannelTargetProviderPrefix(e.to);
if (!n || n.channel === t) return;
return /* @__PURE__ */ new Error(`Target prefix "${n.prefix}:" belongs to ${n.channel}, not ${t}.`);
}
//#endregion
export { validateTargetProviderPrefix as a, stripTargetTopicSuffix as i, stripOutboundTargetKindPrefix as n, stripTargetProviderPrefix as r, resolveTargetPrefixedChannel as t };