openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
123 lines (122 loc) • 3.55 kB
JavaScript
import { c as stripInlineDirectiveTagsForDisplay } from "./directive-tags-batch-D0CxS-zY.mjs";
import { n as canReuseMarkdownOwnershipForAppend, r as findCodeOwnership } from "./code-regions-CfNY0afj.mjs";
//#region src/utils/directive-tags-stream.ts
function hasDirectiveCodePrefixOpportunity(e, t) {
if (!t) return false;
const n = e.length - t.length;
const r = Math.max(0, n - 4);
let i = -1;
for (const t of e.slice(r).matchAll(/(?:\r\n|\n|\r)[^\S\r\n]*\S/g)) if (r + t.index + t[0].length > n) i = r + t.index;
if (i === -1) return false;
const a = e.lastIndexOf("[[");
return a !== -1 && i > a;
}
function findDirectiveCodePrefix(e, t) {
if (!hasDirectiveCodePrefixOpportunity(e, t)) return;
const { regions: n, retainStart: r, completedParagraphs: i } = findCodeOwnership(e);
let a = 0;
let o = 0;
let s = 0;
for (let t = e.indexOf("[["); t !== -1; t = e.indexOf("[[", t + 1)) {
let e = n[a];
while (e && e.end <= t) e = n[++a];
let c = i[o];
while (c && c.end <= t) c = i[++o];
if (!e || e.start > t || e.end < t + 2) return;
if (e.block) {
if (e.end > r) return;
s = e.end;
continue;
}
if (!c || c.hasReferenceCandidate || c.start > e.start || c.end < e.end) return;
s = c.end;
}
return s ? {
end: s,
checkedRawLength: e.length
} : void 0;
}
/** Prose appends cannot open directive syntax or change Markdown code ownership. */
const APPEND_SAFE_DELTA = /^[^[`~]*$/u;
/** Removing an audio directive can change Markdown ownership before reply tags are parsed. */
const AUDIO_DIRECTIVE = /\[\[\s*audio_as_voice\s*\]\]/iu;
function hasUnsettledDirectiveMarker(e) {
const t = e.lastIndexOf("[[");
return t !== -1 && e.lastIndexOf("]]") < t;
}
/** Retain only literal directives whose Markdown ownership cannot change on append. */
const inlineDirectiveDisplayTextFilter = {
transform: (e) => stripInlineDirectiveTagsForDisplay(e).text,
create: () => {
let e;
let t = false;
let n = "";
let r = "";
let i = "";
let a = true;
let o = true;
let s = false;
let c = false;
let l;
const projector = (u) => {
const d = u.delta;
if (d === null) {
r = u.text;
i = stripInlineDirectiveTagsForDisplay(r).text;
e = i === r ? findDirectiveCodePrefix(r, r) : void 0;
t = r.includes("[[");
o = !hasUnsettledDirectiveMarker(r);
s = hasUnsettledDirectiveMarker(i);
c = AUDIO_DIRECTIVE.test(r);
a = i === r;
l = void 0;
n = r.slice(-1);
return {
text: i,
delta: null
};
}
if (d === "") return {
text: i,
delta: ""
};
if ((e !== void 0 || o && !s) && r.length + d.length === u.text.length && APPEND_SAFE_DELTA.test(d) && !c && (!t || canReuseMarkdownOwnershipForAppend(r, d, l ??= findCodeOwnership(r)))) {
r = u.text;
n = d.slice(-1);
if (a) {
i = r;
return u;
}
i += d;
return {
text: i,
delta: d
};
}
const f = i;
if ((n + d).includes("[[")) {
t = true;
e = void 0;
}
n = d.slice(-1);
r = u.text;
if (t && !e) {
i = stripInlineDirectiveTagsForDisplay(r).text;
e = i === r ? findDirectiveCodePrefix(r, d) : void 0;
} else i = r;
o = !hasUnsettledDirectiveMarker(r);
s = hasUnsettledDirectiveMarker(i);
c = AUDIO_DIRECTIVE.test(r);
a = i === r;
l = void 0;
if (a) return u;
return {
text: i,
delta: i === f ? "" : i.startsWith(f) ? i.slice(f.length) : null
};
};
return projector;
}
};
//#endregion
export { inlineDirectiveDisplayTextFilter as n, findDirectiveCodePrefix as t };