openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
202 lines (201 loc) • 6.83 kB
JavaScript
import { c as normalizeOptionalLowercaseString, l as normalizeOptionalString, o as normalizeLowercaseStringOrEmpty } from "./string-coerce-CCIiB4H7.mjs";
import { T as parseStrictInteger } from "./number-coercion-D6u-4YD8.mjs";
import { f as normalizeStringEntries } from "./string-normalization-DLmfYZXI.mjs";
//#region src/channels/allowlist-match.ts
/** Formats match metadata for diagnostics without leaking channel-specific text. */
function formatAllowlistMatchMeta(e) {
return `matchKey=${e?.matchKey ?? "none"} matchSource=${e?.matchSource ?? "none"}`;
}
/** Compiles normalized allowlist entries and records wildcard presence. */
function compileAllowlist(e) {
const t = new Set(e.filter(Boolean));
return {
set: t,
wildcard: t.has("*")
};
}
function resolveAllowlistCandidates(e) {
for (const t of e.candidates) {
if (!t.value) continue;
if (e.compiledAllowlist.set.has(t.value)) return {
allowed: true,
matchKey: t.value,
matchSource: t.source
};
}
return { allowed: false };
}
/** Applies wildcard and empty-list semantics before candidate matching. */
function resolveCompiledAllowlistMatch(e) {
if (e.compiledAllowlist.set.size === 0) return { allowed: false };
if (e.compiledAllowlist.wildcard) return {
allowed: true,
matchKey: "*",
matchSource: "wildcard"
};
return resolveAllowlistCandidates(e);
}
/** Convenience wrapper for callers that do not need to reuse a compiled list. */
function resolveAllowlistMatchByCandidates(e) {
return resolveCompiledAllowlistMatch({
compiledAllowlist: compileAllowlist(e.allowList),
candidates: e.candidates
});
}
/** Matches simple sender id/name allowlists used by legacy channel config. */
function resolveAllowlistMatchSimple(e) {
const t = normalizeLowercaseStringOrEmpty(e.senderId);
const n = normalizeOptionalLowercaseString(e.senderName);
return resolveCompiledAllowlistMatch({
compiledAllowlist: compileAllowlist(e.allowFrom.map((e) => normalizeLowercaseStringOrEmpty(String(e)))),
candidates: [{
value: t,
source: "id"
}, {
value: e.allowNameMatching === true ? n : void 0,
source: "name"
}]
});
}
//#endregion
//#region src/channels/plugins/chat-target-prefixes.ts
function isAllowedParsedChatSender(e) {
const t = normalizeStringEntries(e.allowFrom);
const n = e.normalizeSender(e.sender);
const r = e.allowConversationTargets === true;
const i = r ? e.chatId ?? void 0 : void 0;
const a = r ? normalizeOptionalString(e.chatGuid) : void 0;
const o = r ? normalizeOptionalString(e.chatIdentifier) : void 0;
const s = t.map((t) => {
if (t === "*") return t;
const n = e.parseAllowTarget(t);
if (n.kind === "chat_id") return `chat_id:${n.chatId}`;
if (n.kind === "chat_guid") return `chat_guid:${n.chatGuid}`;
if (n.kind === "chat_identifier") return `chat_identifier:${n.chatIdentifier}`;
return `handle:${n.handle}`;
});
return resolveAllowlistMatchByCandidates({
allowList: s,
candidates: [
{
value: n ? `handle:${n}` : void 0,
source: "handle"
},
{
value: i !== void 0 ? `chat_id:${i}` : void 0,
source: "chat_id"
},
{
value: a ? `chat_guid:${a}` : void 0,
source: "chat_guid"
},
{
value: o ? `chat_identifier:${o}` : void 0,
source: "chat_identifier"
}
]
}).allowed;
}
function stripPrefix(e, t) {
return e.slice(t.length).trim();
}
function resolveServicePrefixedTarget(e) {
for (const { prefix: t, service: n } of e.servicePrefixes) {
if (!e.lower.startsWith(t)) continue;
const r = stripPrefix(e.trimmed, t);
if (!r) throw new Error(`${t} target is required`);
const i = normalizeLowercaseStringOrEmpty(r);
if (e.isChatTarget(i)) return e.parseTarget(r);
return {
kind: "handle",
to: r,
service: n
};
}
return null;
}
function resolveServicePrefixedChatTarget(e) {
const t = [
...e.chatIdPrefixes,
...e.chatGuidPrefixes,
...e.chatIdentifierPrefixes,
...e.extraChatPrefixes ?? []
];
return resolveServicePrefixedTarget({
...e,
isChatTarget: (e) => t.some((t) => e.startsWith(t))
});
}
/** Reject malformed prefixed values instead of treating them as sender handles. */
function parseChatTargetPrefixesOrThrow(e) {
return parseChatTargetPrefixes(e, true);
}
function parseChatTargetPrefixes(e, t) {
for (const [n, r] of [
["chat_id", e.chatIdPrefixes],
["chat_guid", e.chatGuidPrefixes],
["chat_identifier", e.chatIdentifierPrefixes]
]) for (const i of r) {
if (!e.lower.startsWith(i)) continue;
const r = stripPrefix(e.trimmed, i);
if (n === "chat_id") {
const e = parseStrictInteger(r);
if (e !== void 0) return {
kind: n,
chatId: e
};
} else if (r) return n === "chat_guid" ? {
kind: n,
chatGuid: r
} : {
kind: n,
chatIdentifier: r
};
if (t) throw new Error(n === "chat_id" ? `Invalid chat_id: ${r}` : `${n} is required`);
}
return null;
}
function resolveServicePrefixedAllowTarget(e) {
for (const { prefix: t } of e.servicePrefixes) {
if (!e.lower.startsWith(t)) continue;
const n = stripPrefix(e.trimmed, t);
if (!n) return {
kind: "handle",
handle: ""
};
return e.parseAllowTarget(n);
}
return null;
}
function resolveServicePrefixedOrChatAllowTarget(e) {
const t = resolveServicePrefixedAllowTarget(e);
if (t) return t;
return parseChatAllowTargetPrefixes(e);
}
function createAllowedChatSenderMatcher(e) {
return (t) => isAllowedParsedChatSender({
...t,
allowConversationTargets: t.allowConversationTargets ?? e.allowConversationTargets ?? false,
normalizeSender: e.normalizeSender,
parseAllowTarget: e.parseAllowTarget
});
}
/** Ignore malformed prefixes while checking allowlist entries. */
function parseChatAllowTargetPrefixes(e) {
return parseChatTargetPrefixes(e, false);
}
/** Remove one of the known provider prefixes from a free-form target string. */
function stripChannelTargetPrefix(e, ...t) {
const n = e.trim();
for (const e of t) {
const t = `${normalizeLowercaseStringOrEmpty(e)}:`;
if (normalizeLowercaseStringOrEmpty(n).startsWith(t)) return n.slice(t.length).trim();
}
return n;
}
/** Remove generic target-kind prefixes such as `user:` or `group:`. */
function stripTargetKindPrefix(e) {
return e.replace(/^(user|channel|group|conversation|room|dm):/i, "").trim();
}
//#endregion
export { resolveServicePrefixedAllowTarget as a, resolveServicePrefixedTarget as c, compileAllowlist as d, formatAllowlistMatchMeta as f, resolveCompiledAllowlistMatch as g, resolveAllowlistMatchSimple as h, parseChatTargetPrefixesOrThrow as i, stripChannelTargetPrefix as l, resolveAllowlistMatchByCandidates as m, isAllowedParsedChatSender as n, resolveServicePrefixedChatTarget as o, resolveAllowlistCandidates as p, parseChatAllowTargetPrefixes as r, resolveServicePrefixedOrChatAllowTarget as s, createAllowedChatSenderMatcher as t, stripTargetKindPrefix as u };