openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
102 lines (101 loc) • 3.34 kB
JavaScript
//#region src/gateway/boot-echo-guard.ts
const MIN_ECHO_CHARS = 80;
const ECHO_WINDOW_LENGTHS = [MIN_ECHO_CHARS, 81];
const HASH_BASES = [16777619, 1540483477];
const SECOND_HASH_BITS = 2 ** 20;
const SURROGATE_CODE_UNIT = /[\uD800-\uDFFF]/;
function isHighSurrogate(e) {
return e >= 55296 && e <= 56319;
}
function isLowSurrogate(e) {
return e >= 56320 && e <= 57343;
}
function isWindowEdge(e, t) {
return t === 0 || t >= e.length || !(isHighSurrogate(e.charCodeAt(t - 1)) && isLowSurrogate(e.charCodeAt(t)));
}
function includesSafeWindow(e, t) {
for (let n = e.indexOf(t); n >= 0; n = e.indexOf(t, n + 1)) if (isWindowEdge(e, n) && isWindowEdge(e, n + t.length)) return true;
return false;
}
/** Visits each surrogate-safe window hash in O(text.length) time; stops when visit returns true. */
function someWindowHash(e, t, n) {
if (e.length < t) return false;
const [r, i] = HASH_BASES;
let a = 0;
let o = 0;
let s = 1;
let c = 1;
for (let n = 0; n < t; n++) {
const t = e.charCodeAt(n);
a = Math.imul(a, r) + t | 0;
o = Math.imul(o, i) + t | 0;
if (n > 0) {
s = Math.imul(s, r);
c = Math.imul(c, i);
}
}
for (let l = 0;; l++) {
const u = l + t;
if (isWindowEdge(e, l) && isWindowEdge(e, u) && n(l, (a >>> 0) * SECOND_HASH_BITS + (o & SECOND_HASH_BITS - 1))) return true;
if (u >= e.length) return false;
const d = e.charCodeAt(l);
const f = e.charCodeAt(u);
a = Math.imul(a - Math.imul(d, s) | 0, r) + f | 0;
o = Math.imul(o - Math.imul(d, c) | 0, i) + f | 0;
}
}
function hasSortedHash(e, t) {
let n = 0;
let r = e.length - 1;
while (n <= r) {
const i = n + r >>> 1;
const a = e[i];
if (a === t) return true;
if (a !== void 0 && a < t) n = i + 1;
else r = i - 1;
}
return false;
}
function sharesEchoWindow(e, t) {
const [n, r] = e.length <= t.length ? [e, t] : [t, e];
return (SURROGATE_CODE_UNIT.test(n) ? ECHO_WINDOW_LENGTHS : [MIN_ECHO_CHARS]).some((e) => {
const t = new Float64Array(Math.max(n.length - e + 1, 0));
let i = 0;
someWindowHash(n, e, (e, n) => {
t[i++] = n;
return false;
});
const a = t.subarray(0, i).toSorted();
return someWindowHash(r, e, (t, i) => hasSortedHash(a, i) && includesSafeWindow(n, r.slice(t, t + e)));
});
}
const bootContextBySessionKey = /* @__PURE__ */ new Map();
function normalizeEchoComparisonText(e) {
return e.replace(/\s+/gu, " ").trim();
}
function setBootEchoContextForSession(e, t) {
if (!e || !t) return;
bootContextBySessionKey.set(e, { bootPrompt: t });
}
function clearBootEchoContextForSession(e) {
if (!e) return;
bootContextBySessionKey.delete(e);
}
function getBootEchoContextForSession(e) {
if (!e) return;
return bootContextBySessionKey.get(e)?.bootPrompt;
}
function containsSubstantialBootEcho(e, t) {
const n = normalizeEchoComparisonText(e);
if (n.length < MIN_ECHO_CHARS) return false;
const r = normalizeEchoComparisonText(t);
if (r.length < MIN_ECHO_CHARS) return false;
return sharesEchoWindow(n, r);
}
/** Empty output lets the delivery owner discard substantial boot-prompt echoes. */
function stripBootEchoFromOutboundText(e, t) {
if (!t) return e;
return containsSubstantialBootEcho(e, t) ? "" : e;
}
//#endregion
export { stripBootEchoFromOutboundText as i, getBootEchoContextForSession as n, setBootEchoContextForSession as r, clearBootEchoContextForSession as t };