openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
107 lines (106 loc) • 4.27 kB
JavaScript
import { n as sliceUtf16Safe } from "./utf16-slice-CuDyEXgS.mjs";
import { f as resolveAssistantEventPhase } from "./chat-message-content-DNUdbbJ8.mjs";
import { D as stripAssistantMediaDirectivesForDisplay } from "./chat-display-projection.canvas-XAAIxHM8.mjs";
import { c as stripInlineDirectiveTagsForDisplay } from "./directive-tags-batch-D0CxS-zY.mjs";
import { n as splitMediaOutput, t as isRelativeAssistantMediaReference } from "./parse-output-BsYrumRW.mjs";
import { _ as stripInternalRuntimeContext, n as INTERNAL_RUNTIME_CONTEXT_END, t as INTERNAL_RUNTIME_CONTEXT_BEGIN } from "./internal-runtime-context-BEUlDNuI.mjs";
import { n as createConditionalTextProjector, r as createTextProjection } from "./text-projection-DwmQz6_O.mjs";
import { n as inlineDirectiveDisplayTextFilter } from "./directive-tags-C1p_t1Fd.mjs";
import { n as splitTrailingDirective } from "./streaming-directives-C1RBdaQG.mjs";
//#region src/gateway/live-chat-projector.ts
const MAX_LIVE_CHAT_BUFFER_CHARS = 5e5;
/** Cap live display text without letting later snapshots resurrect the retired prefix. */
function capLiveAssistantText(e) {
const { text: t, scope: n } = e;
const r = t.length > MAX_LIVE_CHAT_BUFFER_CHARS ? sliceUtf16Safe(t, -5e5) : t;
if (n) {
const e = t.length - r.length;
const i = Math.max(0, e - n.prefix.length);
n.boundaryNewlines = i > n.separatorLength ? 0 : Math.max(0, n.boundaryNewlines - i);
n.separatorLength = Math.max(0, n.separatorLength - i);
n.prefix = sliceUtf16Safe(n.prefix, e);
}
return r;
}
/** Removes runtime-only context/directive tags from the merged live assistant buffer. */
function normalizeLiveAssistantBufferedText(e, t) {
const n = stripInternalRuntimeContext(stripInlineDirectiveTagsForDisplay(e).text);
return stripAssistantMediaDirectivesForDisplay(t?.final ? n : stripPendingLiveAssistantTail(n), t?.managedMediaUrls ?? []);
}
function stripPendingLiveAssistantTail(e) {
const t = splitTrailingDirective(e);
const n = t.tail ? splitMediaOutput(t.tail, { extractAudioDirectives: false }) : void 0;
return n?.mediaUrls?.length && n.mediaUrls.every((e) => !isRelativeAssistantMediaReference(e)) ? e : t.text;
}
const pendingLiveAssistantTailFilter = {
transform: stripPendingLiveAssistantTail,
create: () => {
let e = "";
let t = false;
let n = true;
let r = 0;
return createConditionalTextProjector(stripPendingLiveAssistantTail, (i) => {
for (const a of i.delta ?? i.text) {
if (e === "[" && a === "[") t = true;
else if (e === "]" && a === "]") t = false;
e = a;
if (a === "\n") {
n = true;
r = 0;
} else if (n && r < 5) {
if (r === 0 && /\s/u.test(a)) continue;
n = a.toUpperCase() === "MEDIA"[r];
if (n) r += 1;
}
}
return t || e === "[" || n && r > 0;
});
}
};
/** One run-owned display chain; replacements rebuild every syntax and visibility probe. */
function createLiveAssistantTextProjection(e) {
const t = [...e?.managedMediaUrls ?? []];
const n = createTextProjection([
inlineDirectiveDisplayTextFilter,
{
activationTokens: [
INTERNAL_RUNTIME_CONTEXT_BEGIN,
INTERNAL_RUNTIME_CONTEXT_END,
"runtime-generated"
],
transform: stripInternalRuntimeContext
},
...e?.final ? [] : [pendingLiveAssistantTailFilter],
...t.length ? [{
activationTokens: ["MEDIA:"],
transform: (e) => stripAssistantMediaDirectivesForDisplay(e, t)
}] : []
]);
const present = (e, t = false) => {
return {
text: e.text,
suppress: !e.text,
delta: t ? null : e.delta
};
};
return {
get source() {
return n.source;
},
append: (e, t) => present(n.append(e, t)),
replace: (e) => present(n.replace(e), true)
};
}
/** Projects already-prepared text without interpreting reply control tokens. */
function projectLiveAssistantBufferedText(e) {
return {
text: e,
suppress: !e
};
}
/** Returns true when an assistant event phase should not appear in live chat. */
function shouldSuppressAssistantEventForLiveChat(e) {
return resolveAssistantEventPhase(e) === "commentary";
}
//#endregion
export { shouldSuppressAssistantEventForLiveChat as a, projectLiveAssistantBufferedText as i, createLiveAssistantTextProjection as n, normalizeLiveAssistantBufferedText as r, capLiveAssistantText as t };