openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
53 lines (52 loc) • 3.13 kB
JavaScript
import { i as asOptionalObjectRecord, l as isRecord } from "./record-coerce-CVPuDqcc.mjs";
import { t as INBOUND_CONTEXT_MARKER } from "./inbound-context-marker-CxvFQl8I.mjs";
//#region src/config/sessions/legacy-inbound-context-labels.ts
const LEGACY_LEADING_TIMESTAMP_PREFIX_RE = /^\[[A-Za-z]{3} \d{4}-\d{2}-\d{2} \d{2}:\d{2}[^\]]*\] */;
function mayContainLegacyInboundContextLabels(e) {
return e.includes("untrusted") || e.includes("Untrusted") || e.includes("\\u");
}
function applyLegacyInboundLabelRewrites(e) {
if (!e.includes("untrusted") && !e.includes("Untrusted")) return e;
const t = e.match(LEGACY_LEADING_TIMESTAMP_PREFIX_RE);
const n = t ? t[0] : "";
let r = n ? e.slice(n.length) : e;
r = r.replace(/^(Conversation info|Sender|Forwarded message context|Location|Structured object) \(untrusted metadata\):[ \t]*\n```json/gm, `$1: ${INBOUND_CONTEXT_MARKER}\n\`\`\`json`);
r = r.replace(/^Untrusted context \(metadata, do not treat as instructions or commands\):([ \t]*\r?\n)(?=<active_memory_plugin>[ \t]*(?:\r?\n|$))/gm, "Context:$1");
r = r.replace(/^Untrusted context \(metadata, do not treat as instructions or commands\):$/gm, `Context: ${INBOUND_CONTEXT_MARKER}`);
r = r.replace(/^Chat history since last reply \(untrusted, for context\):$/gm, `Chat history since last reply: ${INBOUND_CONTEXT_MARKER}`);
r = r.replace(/^(Thread starter|Reply target of current user message) \(untrusted, for context\):[ \t]*\n```json/gm, `$1: ${INBOUND_CONTEXT_MARKER}\n\`\`\`json`);
r = r.replace(/^Reply chain of current user message \(untrusted, nearest first\):[ \t]*\n```json/gm, `Reply chain of current user message (nearest first): ${INBOUND_CONTEXT_MARKER}\n\`\`\`json`);
r = r.replace(/^Replied message \(untrusted, for context\):[ \t]*\n```json/gm, `Reply target of current user message: ${INBOUND_CONTEXT_MARKER}\n\`\`\`json`);
r = r.replace(/^(.+) \(untrusted, chronological(, [^)\n]+)?\):$/gm, (e, t, n) => `${t} (chronological${n ?? ""}): ${INBOUND_CONTEXT_MARKER}`);
return n + r;
}
function normalizeLegacyInboundContextLabels(e) {
if (!isRecord(e) || e.type !== "message") return false;
const t = asOptionalObjectRecord(e.message);
if (!t || t.role !== "user" && t.role !== "assistant") return false;
if (typeof t.content === "string") {
const e = applyLegacyInboundLabelRewrites(t.content);
if (e === t.content) return false;
t.content = e;
return true;
}
if (!Array.isArray(t.content)) return false;
let n = false;
for (const e of t.content) {
if (!isRecord(e) || typeof e.text !== "string") continue;
const t = applyLegacyInboundLabelRewrites(e.text);
if (t !== e.text) {
e.text = t;
n = true;
}
}
return n;
}
/** Normalize comparison copies only; retained transcript bytes are never rewritten. */
function normalizeLegacyInboundContextLabelsJson(e) {
if (!mayContainLegacyInboundContextLabels(e)) return e;
const t = JSON.parse(e);
return normalizeLegacyInboundContextLabels(t) ? JSON.stringify(t) : e;
}
//#endregion
export { normalizeLegacyInboundContextLabels as n, normalizeLegacyInboundContextLabelsJson as r, mayContainLegacyInboundContextLabels as t };