openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
409 lines (408 loc) • 12.6 kB
JavaScript
import { l as normalizeOptionalString } from "./string-coerce-CCIiB4H7.mjs";
import "./src-H3RMXyFL.mjs";
import { t as truncateCodePoints } from "./code-points-DU5oEp0F.mjs";
import { t as expectDefined } from "./expect-DDakjvvQ.mjs";
import { t as findMarkdownCodeRegions } from "./markdown-code-regions-CUsfah9M.mjs";
import { n as indexTextParts, r as isInsideCode, t as createTextPartCodeRegionResolver } from "./code-ranges-2XaqsF-S.mjs";
import { n as trimTextPreservingCode } from "./trim-code-text-Dr7f3RLr.mjs";
import { g as skipWhitespace, m as skipHorizontalWhitespace } from "./grammar-BX_b9bJA.mjs";
//#region src/utils/inline-reply-tags.ts
const REPLY_HEAD = /\[\[\s*(reply_to_current|reply_to\s*:)/iy;
function isWhitespace(e) {
return /\s/u.test(e);
}
/** Reads increasing marker offsets without rescanning nested candidates' shared suffixes. */
function createInlineReplyTagReader(e) {
let t = -1;
let n = -1;
let r = -1;
const i = skipHorizontalWhitespace(e, 0);
const closingBracket = (r) => {
if (t < r) {
const i = e.indexOf("]", r);
t = i < 0 ? e.length : i;
n = t;
while (n > 0 && isWhitespace(e.charAt(n - 1))) n -= 1;
}
return t;
};
const firstLineFeed = (t) => {
if (r < t) {
const n = e.indexOf("\n", t);
r = n < 0 ? e.length : n;
}
return r;
};
return (t, r = false) => {
REPLY_HEAD.lastIndex = t;
const a = REPLY_HEAD.exec(e);
if (!a) return null;
const o = REPLY_HEAD.lastIndex;
const s = a[1]?.endsWith(":") === true;
const c = skipWhitespace(e, o);
if (!s) {
if (e.startsWith("]]", c)) return {
start: t,
end: c + 2,
complete: true
};
} else {
const r = closingBracket(c);
if (e.startsWith("]]", r)) {
if (c < r && firstLineFeed(c) >= n) return {
start: t,
end: r + 2,
id: e.slice(c, n),
complete: true
};
if (c === r) {
for (let n = o; n < r; n += 1) if (e.charAt(n) !== "\n") return {
start: t,
end: r + 2,
id: "",
complete: true
};
}
}
}
if (!r || t !== i) return null;
if (s) {
if (c === e.length) return {
start: t,
end: e.length,
complete: false
};
const n = closingBracket(c);
if (n === e.length || e.charAt(n + 1) === "]") return null;
for (let t = c; t < n; t += 1) if (e.charAt(t) === "\r" || e.charAt(t) === "\n") return null;
return {
start: t,
end: skipHorizontalWhitespace(e, n + 1),
complete: false
};
}
const l = skipHorizontalWhitespace(e, o);
if (e.charAt(l) === "]" && e.charAt(l + 1) !== "]") return {
start: t,
end: skipHorizontalWhitespace(e, l + 1),
complete: false
};
if (l === e.length || l > o && !isWhitespace(e.charAt(l))) return {
start: t,
end: l,
complete: false
};
return null;
};
}
//#endregion
//#region src/utils/directive-tags-batch.ts
const AUDIO_TAG_RE = /\[\[\s*audio_as_voice\s*\]\]/gi;
const DELIVERY_AUDIO_TAG_RE = /\[\[\s*audio_as_voice\s*\]\]/iuy;
const MAX_REPLY_DIRECTIVE_ID_LENGTH = 256;
const UNSAFE_REPLY_DIRECTIVE_CHARS_RE = /[\p{Cc}[\]]/gu;
const NO_INLINE_DIRECTIVES = {
audioAsVoice: false,
replyToCurrent: false,
hasAudioTag: false,
hasReplyTag: false
};
const REMOVED_DIRECTIVE_MARKER_SEED = "";
function replaceOutsideCodeRegions(e, t, n) {
let r;
return e.replace(t, (...t) => {
r ??= e.includes("[[") ? findMarkdownCodeRegions(e) : [];
const i = String(t[0]);
const a = t.at(-2);
return typeof a === "number" && isInsideCode(a + i.indexOf("[["), r) ? i : n(i, t.slice(1, -2), Number(a), e);
});
}
function replaceReplyTagsOutsideCodeRegions(e, t) {
const n = createInlineReplyTagReader(e);
let r;
let i = 0;
let a = 0;
let o = "";
while (a < e.length) {
const s = e.indexOf("[[", a);
if (s < 0) break;
const c = n(s);
a = c ? c.end : s + 1;
if (!c || isInsideCode(s, r ??= findMarkdownCodeRegions(e))) continue;
o += e.slice(i, s);
o += t(e.slice(s, c.end), [c.id], s, e);
i = c.end;
}
return o + e.slice(i);
}
function applyNativeTextEdits(e, t) {
const n = indexTextParts(e);
const r = [...e];
let i = 0;
for (const e of n.spans) {
let a = e.start;
let o = "";
while (i < t.length) {
const r = expectDefined(t[i], "native text edit");
if (r.end <= e.start) {
i++;
continue;
}
if (r.start > e.end) break;
o += n.text.slice(a, Math.max(a, Math.min(r.start, e.end)));
if (r.start >= e.start) o += r.text;
a = Math.min(e.end, Math.max(a, r.end));
if (r.end <= e.end) i++;
else break;
}
r[e.index] = o + n.text.slice(a, e.end);
}
let a = n.spans[0]?.index;
i = 0;
for (let e = 1; e < n.spans.length; e++) {
const o = expectDefined(n.spans[e - 1], "preceding native part");
const s = expectDefined(n.spans[e], "following native part");
while (t[i] && expectDefined(t[i], "boundary text edit").end <= o.end) i++;
const c = t[i];
if (a !== void 0 && c && c.start <= o.end && c.end > o.end) {
r[a] = expectDefined(r[a], "native edit owner") + expectDefined(r[s.index], "native edit continuation");
r[s.index] = "";
} else a = s.index;
}
return r;
}
/** Replace one syntax stage with full-message code ownership and native edit positions. */
function replaceOutsideCodeRegionParts(e, t, n) {
return replaceTextParts(e, (e, n) => replaceOutsideCodeRegions(e, t, n), n);
}
function replaceTextParts(e, t, n) {
const r = indexTextParts(e);
const i = [];
let a = 0;
t(r.text, (e, t, o, s) => {
while (r.spans[a + 1] && expectDefined(r.spans[a + 1], "next text part").start <= o) a++;
const c = n(e, t, o, s, expectDefined(r.spans[a], "directive start part").index);
if (c !== e) i.push({
start: o,
end: o + e.length,
text: c
});
return c;
});
return i.length ? applyNativeTextEdits(e, i) : [...e];
}
function isLineBreak(e) {
return e === "\n" || e === "\r";
}
function isBlank(e) {
return e === " " || e === " ";
}
/**
* Joins the text around each removed directive without touching other whitespace.
* Output is built from pieces with incremental tail facts so many directives stay linear.
*/
function closeRemovedDirectiveGaps(e, t) {
if (!e.includes(t)) return e;
const n = e.trimEnd().length;
const r = [];
let i;
const push = (e) => {
r.push(e);
for (let t = e.length - 1; t >= 0; t -= 1) if (!isBlank(e[t])) {
i = e[t];
return;
}
};
const trimTrailingBlanks = () => {
while (r.length > 0) {
const e = r[r.length - 1] ?? "";
let t = e.length;
while (t > 0 && isBlank(e[t - 1])) t -= 1;
if (t > 0) {
r[r.length - 1] = e.slice(0, t);
return;
}
r.pop();
}
};
const trailingBreaks = () => {
let e = 0;
for (let t = r.length - 1; t >= 0; t -= 1) {
const n = r[t] ?? "";
for (let t = n.length - 1; t >= 0; t -= 1) if (n[t] === "\n") {
e += 1;
if (e >= 2) return e;
} else if (n[t] !== "\r") return e;
}
return e;
};
let a = 0;
for (;;) {
const o = e.indexOf(t, a);
if (o < 0) {
push(e.slice(a));
return r.join("");
}
let s = o + t.length;
while (e.startsWith(t, s)) s += t.length;
push(e.slice(a, o));
let c = s;
while (isBlank(e[c])) c += 1;
const l = i === void 0 || isLineBreak(i);
const u = c >= e.length || isLineBreak(e[c]);
if (l && u) {
trimTrailingBlanks();
let t = trailingBreaks();
while (isLineBreak(e[c])) {
const n = e.startsWith("\r\n", c) ? 2 : 1;
if (t < 2) {
push(e.slice(c, c + n));
t += 1;
}
c += n;
}
} else if (l || c >= n) c = s;
else {
trimTrailingBlanks();
if (!u) push(" ");
}
a = c;
}
}
function trimDirectiveMessageBoundaries(e, t, n) {
let r = n;
const inCode = (t) => (r ??= findMarkdownCodeRegions(e)).some((e) => e.start <= t && t < e.end);
let i = /^(?:\r?\n)*/u.exec(e)?.[0].length ?? 0;
if (/^[ \t]\S/u.test(e.slice(i, i + 2)) && !inCode(i)) i += 1;
let a = e.length;
if (t === "trim") {
const t = e.trimEnd().length;
a = t < a && (r ??= findMarkdownCodeRegions(e)).some((n) => n.start <= t && n.end === e.length) ? a : Math.max(t, i);
}
return e.slice(i, a);
}
function stripInlineDirectiveTagsForDisplay(e) {
if (!e) return {
text: e,
changed: false
};
const t = replaceReplyTagsOutsideCodeRegions(replaceOutsideCodeRegions(e, AUDIO_TAG_RE, () => ""), () => "");
return {
text: t,
changed: t !== e
};
}
function sanitizeReplyDirectiveId(e) {
const t = e?.trim();
if (!t) return;
const n = t.replace(UNSAFE_REPLY_DIRECTIVE_CHARS_RE, "").trim();
if (!n) return;
return n.length <= MAX_REPLY_DIRECTIVE_ID_LENGTH ? n : truncateCodePoints(n, MAX_REPLY_DIRECTIVE_ID_LENGTH);
}
function collectDeliveryDirectiveEdits(e) {
if (!e.includes("[[")) return [];
const t = createInlineReplyTagReader(e);
let n;
const r = [];
let i = 0;
let a = 0;
let o = 0;
while (a < e.length) {
const s = e.indexOf("[[", a);
if (s < 0) break;
let c = s;
while (c > o && /\s/u.test(e.charAt(c - 1))) c -= 1;
DELIVERY_AUDIO_TAG_RE.lastIndex = s;
const l = DELIVERY_AUDIO_TAG_RE.exec(e);
const u = l ? null : t(s, true);
if (!l && !u) {
a = s + 1;
continue;
}
const d = Boolean(l) || u?.complete;
a = l ? DELIVERY_AUDIO_TAG_RE.lastIndex : expectDefined(u, "reply tag").end;
if (d) while (a < e.length && /\s/u.test(e.charAt(a))) a += 1;
o = a;
if (isInsideCode(s, n ??= findMarkdownCodeRegions(e))) continue;
const f = n.some((e) => e.block && e.start > s && e.start <= a);
i = f ? c + e.slice(c, a).trimEnd().length : a;
r.push({
start: c,
end: i,
text: !f && d ? " " : ""
});
}
if (i === 0) return [];
return r;
}
function stripInlineDirectivePartsForDelivery(e, t) {
const n = collectDeliveryDirectiveEdits(indexTextParts(e).text);
if (!n.length) return e.map((e) => ({
text: e,
changed: false
}));
const r = applyNativeTextEdits(e, n);
const i = r.length > 1 ? createTextPartCodeRegionResolver(r) : void 0;
return r.map((n, r) => ({
text: n === e[r] ? n : trimTextPreservingCode(n, t?.preserveTrailingWhitespace ? "start" : "both", i?.(r)),
changed: n !== e[r]
}));
}
function stripInlineDirectiveTagsForDelivery(e, t) {
return expectDefined(stripInlineDirectivePartsForDelivery([e], t)[0], "single delivery directive part");
}
function parseInlineDirectives(e, t = {}) {
if (!e) return {
text: "",
...NO_INLINE_DIRECTIVES
};
if (!e.includes("[[")) return {
text: trimDirectiveMessageBoundaries(e, t.preserveTrailingWhitespace ? "preserve" : "trim"),
...NO_INLINE_DIRECTIVES
};
return expectDefined(parseInlineDirectiveParts([e], t)[0], "single inline directive part");
}
function parseInlineDirectiveParts(e, t = {}) {
const { currentMessageId: n, stripAudioTag: r = true, stripReplyTags: i = true, preserveTrailingWhitespace: a = false, onAudioDirective: o } = t;
const s = e.map(() => ({
audioAsVoice: false,
hasAudioTag: false,
hasReplyTag: false,
sawCurrent: false
}));
let c = REMOVED_DIRECTIVE_MARKER_SEED;
while (e.some((e) => e.includes(c))) c += REMOVED_DIRECTIVE_MARKER_SEED;
const l = replaceTextParts(replaceOutsideCodeRegionParts(e, AUDIO_TAG_RE, (e, t, n, i, a) => {
const l = expectDefined(s[a], "audio directive part");
l.audioAsVoice = l.hasAudioTag = true;
o?.();
return r ? c : e;
}).map((e) => closeRemovedDirectiveGaps(e, c)), replaceReplyTagsOutsideCodeRegions, (e, t, n, r, a) => {
const o = expectDefined(s[a], "reply directive part");
const l = typeof t[0] === "string" ? t[0] : void 0;
o.hasReplyTag = true;
if (l === void 0) o.sawCurrent = true;
else {
const e = sanitizeReplyDirectiveId(l);
if (e) o.lastExplicitId = e;
}
return i ? c : e;
}).map((e) => closeRemovedDirectiveGaps(e, c));
const u = e.length > 1 ? createTextPartCodeRegionResolver(l) : void 0;
return s.map((t, r) => {
const i = expectDefined(l[r], "parsed native text");
const o = t.hasAudioTag || t.hasReplyTag || i !== e[r] ? trimDirectiveMessageBoundaries(i, a ? "preserve" : "trim", u?.(r)) : i;
if (!t.hasAudioTag && !t.hasReplyTag) return Object.assign({ text: o }, NO_INLINE_DIRECTIVES);
return {
text: o,
audioAsVoice: t.audioAsVoice,
replyToId: t.lastExplicitId ?? (t.sawCurrent ? normalizeOptionalString(n) : void 0),
replyToExplicitId: t.lastExplicitId,
replyToCurrent: t.sawCurrent,
hasAudioTag: t.hasAudioTag,
hasReplyTag: t.hasReplyTag
};
});
}
//#endregion
export { sanitizeReplyDirectiveId as a, stripInlineDirectiveTagsForDisplay as c, replaceOutsideCodeRegions as i, parseInlineDirectives as n, stripInlineDirectivePartsForDelivery as o, replaceOutsideCodeRegionParts as r, stripInlineDirectiveTagsForDelivery as s, parseInlineDirectiveParts as t };