@xdarkicex/openclaw-memory-libravdb 1.9.0 → 1.9.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/context-engine.js +55 -4
- package/dist/index.js +51 -3
- package/openclaw.plugin.json +1 -1
- package/package.json +1 -1
package/dist/context-engine.js
CHANGED
|
@@ -918,8 +918,53 @@ function buildBudgetFallbackContext(messages, tokenBudget) {
|
|
|
918
918
|
}
|
|
919
919
|
const DAEMON_AUTHORED_CONTEXT_RE = /<authored_context\b[^>]*>([\s\S]*?)<\/authored_context>/gi;
|
|
920
920
|
const DAEMON_AUTHORED_CONTEXT_GUIDANCE_RE = /^\s*Treat the authored entries below as active project rules and identity context\.?\s*$/i;
|
|
921
|
+
const COMPACTED_SESSION_CONTEXT_RE = /<compacted_session_context\b([^>]*)>([\s\S]*?)<\/compacted_session_context>/gi;
|
|
922
|
+
const COMPACTED_SESSION_RENDER_LEDGER_RE = /(?:^|\n)(?:Artifacts:|Constraints:|Open Next Steps:|Extracted context anchors:)(?:\n|$)/;
|
|
921
923
|
function sanitizeDaemonSystemPromptAddition(text) {
|
|
922
|
-
return demoteDaemonAuthoredContextBlocks(sanitizeToolCallPatterns(text));
|
|
924
|
+
return demoteDaemonAuthoredContextBlocks(sanitizeToolCallPatterns(canonicalizeCompactedSessionContextBlocks(text)));
|
|
925
|
+
}
|
|
926
|
+
function canonicalizeCompactedSessionContextBlocks(text) {
|
|
927
|
+
return text.replace(COMPACTED_SESSION_CONTEXT_RE, (match, attrs, inner) => {
|
|
928
|
+
const trimmed = String(inner).trim();
|
|
929
|
+
const firstLine = trimmed.split(/\r?\n/, 1)[0]?.trim();
|
|
930
|
+
if (!firstLine?.startsWith("{")) {
|
|
931
|
+
return match;
|
|
932
|
+
}
|
|
933
|
+
const rest = trimmed.slice(firstLine.length).trim();
|
|
934
|
+
if (!COMPACTED_SESSION_RENDER_LEDGER_RE.test(rest)) {
|
|
935
|
+
return match;
|
|
936
|
+
}
|
|
937
|
+
try {
|
|
938
|
+
JSON.parse(firstLine);
|
|
939
|
+
}
|
|
940
|
+
catch {
|
|
941
|
+
return match;
|
|
942
|
+
}
|
|
943
|
+
// Keep JSON state line + first (latest, most complete) render ledger.
|
|
944
|
+
// Strip subsequent repeated render ledgers from older compaction cycles.
|
|
945
|
+
// Record boundary: second occurrence of any render-ledger heading.
|
|
946
|
+
// Use the same heading set as COMPACTED_SESSION_RENDER_LEDGER_RE.
|
|
947
|
+
const HEADING_RE = /(?:^|\n)(?:Artifacts:|Constraints:|Open Next Steps:|Extracted context anchors:)/g;
|
|
948
|
+
let headingMatch;
|
|
949
|
+
let headingCount = 0;
|
|
950
|
+
const seen = new Set();
|
|
951
|
+
let cutIdx = rest.length;
|
|
952
|
+
while ((headingMatch = HEADING_RE.exec(rest)) !== null) {
|
|
953
|
+
const heading = headingMatch[0].replace(/^\n/, "");
|
|
954
|
+
if (seen.has(heading)) {
|
|
955
|
+
// Repeat heading — second ledger starts here.
|
|
956
|
+
cutIdx = headingMatch.index;
|
|
957
|
+
break;
|
|
958
|
+
}
|
|
959
|
+
seen.add(heading);
|
|
960
|
+
headingCount++;
|
|
961
|
+
}
|
|
962
|
+
const keptRest = rest.slice(0, cutIdx).trim();
|
|
963
|
+
if (!keptRest) {
|
|
964
|
+
return `<compacted_session_context${attrs}>\n${firstLine}\n</compacted_session_context>`;
|
|
965
|
+
}
|
|
966
|
+
return `<compacted_session_context${attrs}>\n${firstLine}\n${keptRest}\n</compacted_session_context>`;
|
|
967
|
+
});
|
|
923
968
|
}
|
|
924
969
|
function demoteDaemonAuthoredContextBlocks(text) {
|
|
925
970
|
return text.replace(DAEMON_AUTHORED_CONTEXT_RE, (_match, inner) => {
|
|
@@ -1372,9 +1417,13 @@ function ensureReplaySafeUserTurn(assembled, sourceMessages, logger, tokenBudget
|
|
|
1372
1417
|
* Normalizes a compact result into the OpenClaw-compatible assemble result format.
|
|
1373
1418
|
*/
|
|
1374
1419
|
export function normalizeAssembleResult(result, sourceMessages) {
|
|
1375
|
-
|
|
1376
|
-
?
|
|
1420
|
+
const rawSystemPromptAddition = typeof result.systemPromptAddition === "string"
|
|
1421
|
+
? result.systemPromptAddition
|
|
1422
|
+
: "";
|
|
1423
|
+
let systemPromptAddition = rawSystemPromptAddition
|
|
1424
|
+
? sanitizeDaemonSystemPromptAddition(rawSystemPromptAddition)
|
|
1377
1425
|
: "";
|
|
1426
|
+
const systemPromptWasReduced = rawSystemPromptAddition.length > systemPromptAddition.length;
|
|
1378
1427
|
const messages = [];
|
|
1379
1428
|
const extractedMemoryItems = [];
|
|
1380
1429
|
const pushMemoryItem = (args) => {
|
|
@@ -1465,7 +1514,9 @@ export function normalizeAssembleResult(result, sourceMessages) {
|
|
|
1465
1514
|
}
|
|
1466
1515
|
return {
|
|
1467
1516
|
messages,
|
|
1468
|
-
estimatedTokens:
|
|
1517
|
+
estimatedTokens: systemPromptWasReduced
|
|
1518
|
+
? approximateTokenCount(systemPromptAddition) + approximateMessagesTokens(messages)
|
|
1519
|
+
: typeof result.estimatedTokens === "number" ? result.estimatedTokens : 0,
|
|
1469
1520
|
systemPromptAddition,
|
|
1470
1521
|
promptAuthority: PROMPT_AUTHORITY_PREASSEMBLY_MAY_OVERFLOW,
|
|
1471
1522
|
...(result.debug != null ? { debug: result.debug } : {}),
|
package/dist/index.js
CHANGED
|
@@ -36253,8 +36253,54 @@ function buildBudgetFallbackContext(messages, tokenBudget) {
|
|
|
36253
36253
|
}
|
|
36254
36254
|
var DAEMON_AUTHORED_CONTEXT_RE = /<authored_context\b[^>]*>([\s\S]*?)<\/authored_context>/gi;
|
|
36255
36255
|
var DAEMON_AUTHORED_CONTEXT_GUIDANCE_RE = /^\s*Treat the authored entries below as active project rules and identity context\.?\s*$/i;
|
|
36256
|
+
var COMPACTED_SESSION_CONTEXT_RE = /<compacted_session_context\b([^>]*)>([\s\S]*?)<\/compacted_session_context>/gi;
|
|
36257
|
+
var COMPACTED_SESSION_RENDER_LEDGER_RE = /(?:^|\n)(?:Artifacts:|Constraints:|Open Next Steps:|Extracted context anchors:)(?:\n|$)/;
|
|
36256
36258
|
function sanitizeDaemonSystemPromptAddition(text) {
|
|
36257
|
-
return demoteDaemonAuthoredContextBlocks(
|
|
36259
|
+
return demoteDaemonAuthoredContextBlocks(
|
|
36260
|
+
sanitizeToolCallPatterns(canonicalizeCompactedSessionContextBlocks(text))
|
|
36261
|
+
);
|
|
36262
|
+
}
|
|
36263
|
+
function canonicalizeCompactedSessionContextBlocks(text) {
|
|
36264
|
+
return text.replace(COMPACTED_SESSION_CONTEXT_RE, (match, attrs, inner) => {
|
|
36265
|
+
const trimmed = String(inner).trim();
|
|
36266
|
+
const firstLine = trimmed.split(/\r?\n/, 1)[0]?.trim();
|
|
36267
|
+
if (!firstLine?.startsWith("{")) {
|
|
36268
|
+
return match;
|
|
36269
|
+
}
|
|
36270
|
+
const rest = trimmed.slice(firstLine.length).trim();
|
|
36271
|
+
if (!COMPACTED_SESSION_RENDER_LEDGER_RE.test(rest)) {
|
|
36272
|
+
return match;
|
|
36273
|
+
}
|
|
36274
|
+
try {
|
|
36275
|
+
JSON.parse(firstLine);
|
|
36276
|
+
} catch {
|
|
36277
|
+
return match;
|
|
36278
|
+
}
|
|
36279
|
+
const HEADING_RE = /(?:^|\n)(?:Artifacts:|Constraints:|Open Next Steps:|Extracted context anchors:)/g;
|
|
36280
|
+
let headingMatch;
|
|
36281
|
+
let headingCount = 0;
|
|
36282
|
+
const seen = /* @__PURE__ */ new Set();
|
|
36283
|
+
let cutIdx = rest.length;
|
|
36284
|
+
while ((headingMatch = HEADING_RE.exec(rest)) !== null) {
|
|
36285
|
+
const heading = headingMatch[0].replace(/^\n/, "");
|
|
36286
|
+
if (seen.has(heading)) {
|
|
36287
|
+
cutIdx = headingMatch.index;
|
|
36288
|
+
break;
|
|
36289
|
+
}
|
|
36290
|
+
seen.add(heading);
|
|
36291
|
+
headingCount++;
|
|
36292
|
+
}
|
|
36293
|
+
const keptRest = rest.slice(0, cutIdx).trim();
|
|
36294
|
+
if (!keptRest) {
|
|
36295
|
+
return `<compacted_session_context${attrs}>
|
|
36296
|
+
${firstLine}
|
|
36297
|
+
</compacted_session_context>`;
|
|
36298
|
+
}
|
|
36299
|
+
return `<compacted_session_context${attrs}>
|
|
36300
|
+
${firstLine}
|
|
36301
|
+
${keptRest}
|
|
36302
|
+
</compacted_session_context>`;
|
|
36303
|
+
});
|
|
36258
36304
|
}
|
|
36259
36305
|
function demoteDaemonAuthoredContextBlocks(text) {
|
|
36260
36306
|
return text.replace(DAEMON_AUTHORED_CONTEXT_RE, (_match, inner) => {
|
|
@@ -36622,7 +36668,9 @@ function ensureReplaySafeUserTurn(assembled, sourceMessages, logger, tokenBudget
|
|
|
36622
36668
|
};
|
|
36623
36669
|
}
|
|
36624
36670
|
function normalizeAssembleResult(result, sourceMessages) {
|
|
36625
|
-
|
|
36671
|
+
const rawSystemPromptAddition = typeof result.systemPromptAddition === "string" ? result.systemPromptAddition : "";
|
|
36672
|
+
let systemPromptAddition = rawSystemPromptAddition ? sanitizeDaemonSystemPromptAddition(rawSystemPromptAddition) : "";
|
|
36673
|
+
const systemPromptWasReduced = rawSystemPromptAddition.length > systemPromptAddition.length;
|
|
36626
36674
|
const messages = [];
|
|
36627
36675
|
const extractedMemoryItems = [];
|
|
36628
36676
|
const pushMemoryItem = (args) => {
|
|
@@ -36712,7 +36760,7 @@ ${extractedMemoryItems.join("\n")}
|
|
|
36712
36760
|
}
|
|
36713
36761
|
return {
|
|
36714
36762
|
messages,
|
|
36715
|
-
estimatedTokens: typeof result.estimatedTokens === "number" ? result.estimatedTokens : 0,
|
|
36763
|
+
estimatedTokens: systemPromptWasReduced ? approximateTokenCount(systemPromptAddition) + approximateMessagesTokens(messages) : typeof result.estimatedTokens === "number" ? result.estimatedTokens : 0,
|
|
36716
36764
|
systemPromptAddition,
|
|
36717
36765
|
promptAuthority: PROMPT_AUTHORITY_PREASSEMBLY_MAY_OVERFLOW,
|
|
36718
36766
|
...result.debug != null ? { debug: result.debug } : {}
|
package/openclaw.plugin.json
CHANGED