openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
44 lines (43 loc) • 2.11 kB
JavaScript
import { o as normalizeLowercaseStringOrEmpty } from "./string-coerce-CCIiB4H7.mjs";
//#region src/agents/embedded-agent-helpers/messaging-dedupe.ts
const MIN_DUPLICATE_TEXT_LENGTH = 10;
const MIN_SUBSTRING_DUPLICATE_RATIO = .5;
function normalizeTextForComparison(e) {
return normalizeLowercaseStringOrEmpty(e).replace(/\p{Emoji_Presentation}|\p{Extended_Pictographic}/gu, "").replace(/\s+/g, " ").trim();
}
function hasTextBeyondSentTexts(e, t) {
let n = e;
for (const e of t.toSorted((e, t) => t.length - e.length)) if (e.length >= MIN_DUPLICATE_TEXT_LENGTH) n = n.replaceAll(e, " ");
return /[\p{L}\p{N}]/u.test(n);
}
function isMessagingToolDuplicateNormalized(e, t) {
if (t.length === 0) return false;
if (!e || e.length < MIN_DUPLICATE_TEXT_LENGTH) return false;
return t.some((n) => {
if (!n || n.length < MIN_DUPLICATE_TEXT_LENGTH) return false;
if (e.includes(n)) {
if (e.startsWith(n) && hasTextBeyondSentTexts(e.slice(n.length), t)) return false;
return n.length >= e.length * MIN_SUBSTRING_DUPLICATE_RATIO;
}
return n.includes(e) && e.length >= n.length * MIN_SUBSTRING_DUPLICATE_RATIO;
});
}
function isMessagingToolDuplicate(e, t) {
if (t.length === 0) return false;
const n = normalizeTextForComparison(e);
if (!n || n.length < MIN_DUPLICATE_TEXT_LENGTH) return false;
return isMessagingToolDuplicateNormalized(n, t.map(normalizeTextForComparison));
}
function resolveCurrentSourceMessagingToolPartial(e, t) {
const n = e.currentSourceMessagingToolHeldPartial;
const r = n && t.evtType === "text_delta" && !t.text.startsWith(n) ? `${n}${t.visibleDelta || t.text}` : t.text;
const i = e.currentSourceMessagingToolSentTextsNormalized.length ? normalizeTextForComparison(r) : "";
const a = Boolean(i) && e.currentSourceMessagingToolSentTextsNormalized.some((e) => e === i || e.startsWith(i));
e.currentSourceMessagingToolHeldPartial = a ? r : void 0;
return {
hold: a,
text: r
};
}
//#endregion
export { resolveCurrentSourceMessagingToolPartial as i, isMessagingToolDuplicateNormalized as n, normalizeTextForComparison as r, isMessagingToolDuplicate as t };