openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
183 lines (182 loc) • 6.58 kB
JavaScript
import { a as asOptionalRecord } from "./record-coerce-CVPuDqcc.mjs";
import { n as estimateStringChars } from "./cjk-chars-CgnWl5qA.mjs";
import { r as truncateUtf16Safe } from "./utf16-slice-CuDyEXgS.mjs";
import { _ as normalizeInputProvenance } from "./input-provenance-DupvvURj.mjs";
import { l as readTranscriptSenderIdentity } from "./user-turn-transcript.metadata-GFtsH_a_.mjs";
import { _ as sanitizeTranscriptMessage } from "./session-accessor.sqlite-transcript-store-DRtj0wNT.mjs";
//#region src/agents/exec-auto-review-transcript.ts
const TRANSCRIPT_LIMITS = {
userAssistantChars: 4e3,
toolChars: 1e3,
nonUserEntries: 40,
totalChars: 24e3
};
const OMITTED_FIELD = /^(?:details|media|(?:input_|output_)?(?:images?|image_url|audio|video)|(?:input_|output_)(?:file|document)|(?:image|audio|video|file|document)[_-]?(?:base64|bytes|data)|inline[_-]?data|base64|b64_json|blob|buffer|bytes|(?:source)?session[_-]?key|(?:origin)?session[_-]?id|(?:sender|peer|profile)[_-]?id)$/i;
function omitPayloads(e, t) {
if (OMITTED_FIELD.test(e)) return;
if (typeof t === "string" && /^data:[^\s,]*,/i.test(t.trimStart())) return "[media omitted]";
const n = asOptionalRecord(t);
return n && (/^(?:(?:input_|output_)?(?:image|image_url|audio|video|file|document)|media|base64|buffer)$/i.test(String(n.type)) || n.encoding === "base64" || [
"mimeType",
"mime_type",
"mediaType",
"media_type",
"contentType",
"content_type"
].some((e) => typeof n[e] === "string") && (n.data !== void 0 || n.content !== void 0)) ? void 0 : t;
}
function textOnly(e) {
let t = e;
if (/^\s*[[{]/.test(t)) try {
t = JSON.stringify(JSON.parse(t), omitPayloads) ?? "[media omitted]";
} catch {}
return t.replace(/\bdata:[^\s,]*,[^\s"'<>]*/gi, "[media omitted]");
}
function userOrigin(e) {
const t = normalizeInputProvenance(Reflect.get(e, "provenance"));
if (t?.kind === "inter_session" || t?.kind === "internal_system") return t.kind;
const n = asOptionalRecord(Reflect.get(e, "__openclaw"));
const r = readTranscriptSenderIdentity(n?.senderIdentity);
if (n?.senderIsOwner === true || r?.type === "profile") return "operator";
return r?.type === "observation" ? "channel" : "unknown";
}
/** Projects current live messages into bounded reviewer evidence. */
function buildExecAutoReviewTranscript(e) {
const t = e.messages.map((t) => t.role === "user" ? e.userTurnOrigins?.get(t) ?? t : t);
const n = /* @__PURE__ */ new Set();
for (const e of t) {
if (e.role !== "user") continue;
const t = asOptionalRecord(Reflect.get(e, "__openclaw"));
const r = readTranscriptSenderIdentity(t?.senderIdentity);
const i = normalizeInputProvenance(Reflect.get(e, "provenance"));
for (const e of [
t?.senderId,
r?.id,
i?.sourceSessionKey,
i?.originSessionId
]) if (typeof e === "string" && e) {
n.add(e);
n.add(JSON.stringify(e).slice(1, -1));
}
}
const r = [...n].toSorted((e, t) => t.length - e.length);
const scrubIdentifiers = (e) => r.reduce((e, t) => e.replaceAll(t, "[identity omitted]"), e);
const i = [];
const a = /* @__PURE__ */ new Map();
const add = (e) => {
const t = e.kind === "user" || e.kind === "assistant" ? TRANSCRIPT_LIMITS.userAssistantChars : TRANSCRIPT_LIMITS.toolChars;
const n = scrubIdentifiers(textOnly(e.text));
if (e.toolCallId) {
let t = a.get(e.toolCallId);
if (!t) {
t = `tool-${a.size + 1}`;
a.set(e.toolCallId, t);
}
e.toolCallId = t;
}
const r = e.toolName && scrubIdentifiers(e.toolName);
i.push({
...e,
text: truncateUtf16Safe(n, t),
...r === void 0 ? {} : { toolName: truncateUtf16Safe(r, 256) },
...n.length > t || (r?.length ?? 0) > 256 ? { truncated: true } : {}
});
};
for (const e of t) {
const t = sanitizeTranscriptMessage(e);
if (t.role === "user" || t.role === "toolResult") {
const n = typeof t.content === "string" ? t.content : t.content.flatMap((e) => e.type === "text" ? [textOnly(e.text)] : []).join("\n");
if (n) add(t.role === "user" ? {
kind: "user",
text: n,
origin: userOrigin(e)
} : {
kind: "tool_result",
text: n,
toolName: t.toolName,
toolCallId: t.toolCallId
});
} else if (t.role === "assistant") {
for (const e of t.content) if (e.type === "text" && e.text) add({
kind: "assistant",
text: e.text
});
else if (e.type === "toolCall") add({
kind: "tool_call",
text: JSON.stringify(e.arguments, omitPayloads) ?? "[media omitted]",
toolName: e.name,
toolCallId: e.id
});
}
}
const o = estimateStringChars(JSON.stringify({
entries: [],
omittedEntries: i.length,
truncated: false
}));
const s = Math.max(0, TRANSCRIPT_LIMITS.totalChars - o);
const c = i.map((e) => ({
entry: e,
cost: estimateStringChars(JSON.stringify(e)) + 1
}));
const l = /* @__PURE__ */ new Set();
let u = 0;
let d = 0;
const f = [.../* @__PURE__ */ new Set([c.find(({ entry: e }) => e.kind === "user"), c.findLast(({ entry: e }) => e.kind === "user")])].filter((e) => e !== void 0);
for (const e of f) {
const { entry: t } = e;
const n = Math.floor(s / f.length);
if (e.cost > n) {
const r = t.text;
t.truncated = true;
let i = 0;
let a = r.length;
while (i < a) {
const e = Math.ceil((i + a) / 2);
t.text = truncateUtf16Safe(r, e);
if (estimateStringChars(JSON.stringify(t)) + 1 <= n) i = e;
else a = e - 1;
}
t.text = truncateUtf16Safe(r, i);
e.cost = estimateStringChars(JSON.stringify(t)) + 1;
}
l.add(e);
u += e.cost;
}
const p = /* @__PURE__ */ new Map();
const m = /* @__PURE__ */ new Map();
for (const e of c) {
const { entry: t } = e;
if (t.toolCallId && t.kind === "tool_call") p.set(t.toolCallId, e);
else if (t.toolCallId && t.kind === "tool_result") {
const n = p.get(t.toolCallId);
if (n !== void 0) {
m.set(e, n);
m.set(n, e);
p.delete(t.toolCallId);
}
}
}
const h = /* @__PURE__ */ new Set();
for (const e of c.toReversed()) {
if (l.has(e) || h.has(e)) continue;
const t = m.get(e);
const n = t === void 0 ? [e] : [e, t];
n.forEach((e) => h.add(e));
const r = n.filter(({ entry: e }) => e.kind !== "user").length;
const i = n.reduce((e, t) => e + t.cost, 0);
if (u + i > s || d + r > TRANSCRIPT_LIMITS.nonUserEntries) continue;
n.forEach((e) => l.add(e));
u += i;
d += r;
}
const g = c.filter((e) => l.has(e)).map(({ entry: e }) => e);
const _ = i.length - g.length;
return {
entries: g,
omittedEntries: _,
truncated: _ > 0 || g.some((e) => e.truncated === true)
};
}
//#endregion
export { buildExecAutoReviewTranscript as t };