openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
113 lines (112 loc) • 5.18 kB
JavaScript
import { l as isRecord } from "./record-coerce-CVPuDqcc.mjs";
import { i as truncateWithMarker, r as truncateUtf16Safe } from "./utf16-slice-CuDyEXgS.mjs";
import { s as redactSensitiveText } from "./redact-Mj7pppxC.mjs";
import { i as isSilentReplyText } from "./tokens-CcE6ESvH.mjs";
import { t as stripMixedSilentReplyTokens } from "./mixed-silent-reply-tokens-5vt94DeG.mjs";
import { s as stripHeartbeatToken } from "./heartbeat-DHK32_6H.mjs";
import { _ as stripInternalRuntimeContext } from "./internal-runtime-context-BEUlDNuI.mjs";
import { i as stripInboundMetadata } from "./strip-inbound-meta-BMUpr5S_.mjs";
//#region src/auto-reply/reply/display-text-sanitize.ts
/** Removes internal runtime metadata before showing text to users. */
function stripInternalMetadataForDisplay(e) {
return stripInboundMetadata(stripInternalRuntimeContext(e));
}
//#endregion
//#region src/agents/agent-run-terminal-receipt.ts
const AGENT_RUN_ROUTE_CHANGE_MAX_CHARS = 320;
function normalizeAgentRunTerminalReceipt(e) {
const t = e;
return t && typeof t.runId === "string" && typeof t.sessionId === "string" && typeof t.turnId === "string" && t.requested && t.effective && Array.isArray(t.successfulToolNames) ? t : void 0;
}
function formatAgentRunModelRef(e) {
const t = redactSensitiveText(`${e.provider}/${e.model}`, { mode: "tools" }).replace(/\s+/gu, " ").trim();
return t ? truncateUtf16Safe(t, 128) : void 0;
}
/** Normalizes the producer-owned route fact before lifecycle or prompt use. */
function normalizeAgentRunRouteChange(e) {
const t = typeof e === "string" ? redactSensitiveText(e, { mode: "tools" }).replace(/\s+/gu, " ").trim() : "";
return t ? truncateUtf16Safe(t, AGENT_RUN_ROUTE_CHANGE_MAX_CHARS) : void 0;
}
/** Formats the bounded, secret-free route fact owned by a terminal receipt. */
function formatAgentRunRouteChange(e, t) {
if (e?.runId !== t || !e.rerouted || e.terminalDisposition !== "visible") return;
const n = formatAgentRunModelRef(e.requested);
const r = formatAgentRunModelRef({
...e.effective,
model: e.effective.responseModel || e.effective.model
});
return n && r ? `Model route changed: ${n} → ${r}.` : void 0;
}
//#endregion
//#region src/agents/agent-run-terminal-reply.ts
const AGENT_RUN_TERMINAL_REPLY_MAX_CHARS = 4096;
function isMessageToolNotCalledTerminalReply(e) {
return e?.disposition === "empty" && e.code === "message-tool-not-called";
}
/** Sanitizes and caps known-visible text without classifying silence. */
function sanitizeAgentRunTerminalReply(e) {
const t = stripInternalMetadataForDisplay(e).trim();
const n = truncateWithMarker(t, AGENT_RUN_TERMINAL_REPLY_MAX_CHARS, {
marker: "…",
reserve: 1,
trimEnd: true
});
if (!n) return { disposition: "empty" };
return {
disposition: "visible",
text: Buffer.from(n, "utf16le").toString("utf16le")
};
}
/** Projects payloads through the shared message-presentation acknowledgement rule. */
function filterAgentRunTerminalPayloadsForPresentation(e) {
if (!e.payloads) return e.payloads;
return e.payloads.filter((e) => {
const t = e.text;
if (!t) return true;
const n = stripHeartbeatToken(t, { mode: "message" });
const r = e.mediaUrls?.some((e) => e.trim()) === true || Boolean(e.mediaUrl?.trim());
return !(n.shouldSkip && !r);
});
}
/** Returns full prepared text, or undefined for an explicitly silent reply. */
function prepareAgentRunReplyText(e) {
if (isSilentReplyText(e)) return;
return stripMixedSilentReplyTokens(e) ?? e.replace(/[\r\n]\s*NO_REPLY\s*$/iu, "");
}
function prepareAgentRunTerminalReply(e) {
const t = prepareAgentRunReplyText(e.rawText ?? "");
return {
text: t ?? "",
terminalReply: t === void 0 ? { disposition: "silent" } : sanitizeAgentRunTerminalReply(t)
};
}
/** Normalizes lifecycle/RPC evidence without allowing raw or unbounded text through. */
function normalizeAgentRunTerminalReplySnapshot(e) {
if (!isRecord(e)) return;
const t = e.disposition;
if (t === "silent") return { disposition: t };
if (t === "empty") {
if (e.code === "message-tool-not-called") return {
disposition: t,
code: "message-tool-not-called"
};
return { disposition: t };
}
if (t !== "visible") return;
const n = e.text;
if (typeof n !== "string") return;
const r = sanitizeAgentRunTerminalReply(n);
if (r.disposition === "visible") {
const t = normalizeAgentRunRouteChange(e.modelRouteChange);
if (t) r.modelRouteChange = t;
}
return r;
}
/** Reply evidence merges independently from sticky timeout/cancellation precedence. */
function mergeAgentRunTerminalReplySnapshot(e, t) {
if (!t || isMessageToolNotCalledTerminalReply(e)) return e;
if (!e || isMessageToolNotCalledTerminalReply(t) || e.disposition === "empty") return t;
return t.disposition === "empty" ? e : t;
}
//#endregion
export { prepareAgentRunTerminalReply as a, normalizeAgentRunRouteChange as c, prepareAgentRunReplyText as i, normalizeAgentRunTerminalReceipt as l, mergeAgentRunTerminalReplySnapshot as n, sanitizeAgentRunTerminalReply as o, normalizeAgentRunTerminalReplySnapshot as r, formatAgentRunRouteChange as s, filterAgentRunTerminalPayloadsForPresentation as t, stripInternalMetadataForDisplay as u };