UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

53 lines (52 loc) • 2.33 kB
import { i as truncateWithMarker } from "./utf16-slice-CuDyEXgS.mjs"; import { a as mapNestedStrings } from "./plugin-text-transforms-CMotGmZ0.mjs"; import { n as markInboundContextLabel } from "./inbound-context-marker-CxvFQl8I.mjs"; import { n as resolvePromptHistoryLimit } from "./history-limit-D9prPWZZ.mjs"; import { t as normalizeInboundTextNewlines } from "./inbound-text-Bmx57Z44.mjs"; //#region src/auto-reply/reply/channel-prompt-context.ts /** Appends channel-supplied prompt context to the user-role body under a marked label. */ /** * The fixed marker lets strippers recognize OpenClaw-injected context; it is not * a trust guardrail. Trust guidance travels with each entry instead * (`buildChannelMetadata` wraps entries in `wrapExternalContent`, whose SECURITY * NOTICE carries the do-not-obey clause). */ function appendChannelPromptContext(e, t) { if (!t?.length) return e; const n = t.map(normalizeInboundTextNewlines).filter(Boolean); if (n.length === 0) return e; return [e, [markInboundContextLabel("Context:"), ...n].join("\n")].filter(Boolean).join("\n\n"); } const MAX_CONTEXT_JSON_STRING_CHARS = 2e3; function neutralizeMarkdownFences(e) { return e.replaceAll("```", "`​``"); } function sanitizeContextJsonString(e) { return neutralizeMarkdownFences(truncateWithMarker(e, MAX_CONTEXT_JSON_STRING_CHARS, { marker: "…[truncated]", reserve: 14, trimEnd: true })); } function formatContextJsonBlock(e, t) { return [ e, "```json", JSON.stringify(mapNestedStrings(t, sanitizeContextJsonString)), "```" ].join("\n"); } /** Preserve a platform-selected window; legacy pending buffers keep their defensive tail cap. */ function selectInboundHistoryContext(e) { const t = e.InboundHistory ?? []; const n = e.SessionTranscriptContext?.historyKind === "recent"; const r = e.SessionTranscriptContext?.historyLimit; const i = n && typeof r === "number" && Number.isSafeInteger(r) && r >= 0 ? resolvePromptHistoryLimit(r, 20) : 20; return { boundedHistory: i > 0 ? t.slice(-i) : [], historyLabel: n ? "Recent chat history:" : "Chat history since last reply:", truncated: t.length > i }; } //#endregion export { selectInboundHistoryContext as a, neutralizeMarkdownFences as i, appendChannelPromptContext as n, formatContextJsonBlock as r, MAX_CONTEXT_JSON_STRING_CHARS as t };