openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
153 lines (152 loc) • 4.66 kB
JavaScript
import { r as resolveAgentConfig } from "./agent-scope-config-DmXgFGKP.mjs";
import "./agent-scope-runtime-CDpr-vor.mjs";
//#region extensions/memory-core/src/memory-budget.ts
const PROMOTION_SECTION_HEADING_RE = /^## Promoted From Short-Term Memory \(([^)]+)\)\s*$/;
const PROMOTION_SUBSECTION_HEADING_RE = /^### (?:Global|Project: .+?)\s*$/;
const PROMOTION_ENTRY_MARKER_RE = /^<!--\s*openclaw-memory-promotion:.*-->\s*$/i;
const GENERATED_PROMOTION_ENTRY_MARKER_RE = /^<!--\s*openclaw-memory-promotion:(?:(?!-->).)*-->\s*$/i;
const ATX_HEADING_RE = /^ {0,3}#{1,6}(?:[ \t]|$)/;
const SETEXT_HEADING_UNDERLINE_RE = /^ {0,3}(?:=+|-+)[ \t]*$/;
const DEFAULT_MEMORY_FILE_MAX_CHARS = 1e4;
/**
* Keep promotion output within every consuming agent's per-file bootstrap
* budget. An unconfigured bootstrap limit stays above the promotion writer's
* own ceiling, so only explicit lower limits need to reduce the budget here.
*/
function resolveMemoryPromotionFileMaxChars(e) {
const t = e.cfg?.agents?.defaults?.bootstrapMaxChars;
const n = e.agentIds.length > 0 ? [...new Set(e.agentIds)] : [void 0];
let r = DEFAULT_MEMORY_FILE_MAX_CHARS;
for (const i of n) {
const n = i ? resolveAgentConfig(e.cfg ?? {}, i)?.bootstrapMaxChars ?? t : t;
if (n !== void 0) r = Math.min(r, n);
}
return r;
}
const WRITE_OVERHEAD_RESERVE = 21;
function isGeneratedPromotionBlock(e) {
let t = false;
let n = 1;
while (n < e.length) {
const r = e[n] ?? "";
if (r.trim().length === 0) {
n += 1;
continue;
}
if (PROMOTION_SUBSECTION_HEADING_RE.test(r)) {
n += 1;
while (n < e.length && (e[n] ?? "").trim().length === 0) n += 1;
}
if (!GENERATED_PROMOTION_ENTRY_MARKER_RE.test(e[n] ?? "")) return false;
if (!(e[n + 1] ?? "").startsWith("- ")) return false;
t = true;
n += 2;
}
return t;
}
function startsGeneratedPromotionSubsection(e, t) {
if (!PROMOTION_SUBSECTION_HEADING_RE.test(e[t] ?? "")) return false;
for (let n = t + 1; n < e.length; n += 1) {
const t = e[n] ?? "";
if (t.trim().length === 0) continue;
return PROMOTION_ENTRY_MARKER_RE.test(t);
}
return false;
}
function takeSetextHeadingLines(e) {
let t = e.length;
while (t > 1 && e[t - 1]?.trim().length !== 0) t -= 1;
if (t === e.length) return;
const n = e.slice(t);
if (n.some((e) => PROMOTION_ENTRY_MARKER_RE.test(e))) return;
e.splice(t);
return n;
}
function parseMemoryBlocks(e) {
if (e.length === 0) return [];
const t = e.split(/\r?\n/);
const n = [];
let r = [];
let i;
const flush = () => {
if (r.length === 0) return;
const e = r.join("\n");
if (i && isGeneratedPromotionBlock(r)) n.push({
kind: "promotion",
date: i,
text: e,
entryCount: r.filter((e) => PROMOTION_ENTRY_MARKER_RE.test(e)).length
});
else n.push({
kind: "preserved",
text: e
});
r = [];
i = void 0;
};
for (const [e, n] of t.entries()) {
if (i && SETEXT_HEADING_UNDERLINE_RE.test(n)) {
const e = takeSetextHeadingLines(r);
if (e) {
flush();
r = [...e, n];
continue;
}
}
const a = i && startsGeneratedPromotionSubsection(t, e);
if (ATX_HEADING_RE.test(n) && !a) {
flush();
i = PROMOTION_SECTION_HEADING_RE.exec(n)?.[1];
r = [n];
} else r.push(n);
}
flush();
return n;
}
/** Drop oldest fully generated sections within the loss bound; the caller owns the final fit check. */
function compactMemoryForBudget(e) {
const { existingMemory: t, newSection: n, budgetChars: r } = e;
if (r <= 0) return {
compacted: t,
droppedDates: []
};
const i = Math.max(0, r - WRITE_OVERHEAD_RESERVE);
if (t.length + n.length <= i) return {
compacted: t,
droppedDates: []
};
const a = parseMemoryBlocks(t);
const o = a.flatMap((e, t) => e.kind === "promotion" ? [{
block: e,
index: t
}] : []).toSorted((e, t) => e.block.date.localeCompare(t.block.date));
if (o.length === 0) return {
compacted: t,
droppedDates: []
};
const s = /* @__PURE__ */ new Set();
const c = [];
const l = o.reduce((e, { block: t }) => e + t.entryCount, 0);
const u = Math.max(0, Math.min(1, e.maxPriorEntryLossFraction ?? 1));
let d = 0;
let f = t.length;
const p = a.length > 1 ? 1 : 0;
for (const { block: e, index: t } of o) {
if (f + n.length <= i) break;
if (l > 0 && (d + e.entryCount) / l > u) break;
s.add(t);
c.push(e.date);
d += e.entryCount;
f = Math.max(0, f - e.text.length - p);
}
if (s.size === 0) return {
compacted: t,
droppedDates: []
};
return {
compacted: a.filter((e, t) => !s.has(t)).map((e) => e.text).join("\n"),
droppedDates: c
};
}
//#endregion
export { compactMemoryForBudget as n, resolveMemoryPromotionFileMaxChars as r, DEFAULT_MEMORY_FILE_MAX_CHARS as t };