@xdarkicex/openclaw-memory-libravdb 1.6.17 → 1.6.18
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.d.ts +2 -0
- package/dist/context-engine.js +27 -3
- package/dist/index.js +29 -3
- package/openclaw.plugin.json +1 -1
- package/package.json +1 -1
package/dist/context-engine.d.ts
CHANGED
|
@@ -12,10 +12,12 @@ type OpenClawCompatibleMessage = {
|
|
|
12
12
|
id?: string;
|
|
13
13
|
[key: string]: unknown;
|
|
14
14
|
};
|
|
15
|
+
type OpenClawCompatiblePromptAuthority = "preassembly_may_overflow";
|
|
15
16
|
type OpenClawCompatibleAssembleResult = {
|
|
16
17
|
messages: OpenClawCompatibleMessage[];
|
|
17
18
|
estimatedTokens: number;
|
|
18
19
|
systemPromptAddition: string;
|
|
20
|
+
promptAuthority: OpenClawCompatiblePromptAuthority;
|
|
19
21
|
debug?: AssembleContextInternalResponse["debug"];
|
|
20
22
|
};
|
|
21
23
|
type OpenClawCompatibleCompactResult = {
|
package/dist/context-engine.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { resolveIdentity } from "./identity.js";
|
|
2
2
|
import { resolveUserCollection } from "./memory-scopes.js";
|
|
3
3
|
const APPROX_CHARS_PER_TOKEN = 4;
|
|
4
|
+
const PROMPT_AUTHORITY_PREASSEMBLY_MAY_OVERFLOW = "preassembly_may_overflow";
|
|
4
5
|
const ASSEMBLE_BUDGET_HEADROOM_TOKENS = 256;
|
|
5
6
|
const ASSEMBLE_BUDGET_HEADROOM_FRACTION = 0.2;
|
|
6
7
|
const DEFAULT_COMPACTION_THRESHOLD_FRACTION = 0.8;
|
|
@@ -10,6 +11,7 @@ const QUOTED_PHRASE_RE = /"([^"]{4,})"|'([^']{4,})'/g;
|
|
|
10
11
|
const EXACT_RECALL_SEARCH_K = 32;
|
|
11
12
|
const EXACT_RECALL_MAX_TOKENS = 4;
|
|
12
13
|
const RESERVED_CURRENT_TURN_TOKENS = 150;
|
|
14
|
+
const AFTER_TURN_INGEST_MAX_TOKENS = 2048;
|
|
13
15
|
const COMMON_QUERY_WORDS = new Set([
|
|
14
16
|
"what", "does", "mean", "remember", "recall", "about", "this", "that",
|
|
15
17
|
"the", "and", "for", "with", "from", "your", "have", "been", "were",
|
|
@@ -226,6 +228,18 @@ function trimMessagesToBudget(messages, tokenBudget) {
|
|
|
226
228
|
}
|
|
227
229
|
return [{ ...last, content: truncated }];
|
|
228
230
|
}
|
|
231
|
+
function boundAfterTurnMessagesForIngest(messages, logger, sessionId) {
|
|
232
|
+
const estimatedTokens = approximateMessagesTokens(messages);
|
|
233
|
+
if (estimatedTokens <= AFTER_TURN_INGEST_MAX_TOKENS) {
|
|
234
|
+
return messages;
|
|
235
|
+
}
|
|
236
|
+
const bounded = trimMessagesToBudget(messages, AFTER_TURN_INGEST_MAX_TOKENS)
|
|
237
|
+
.map((message) => normalizeKernelMessage(message));
|
|
238
|
+
logger.warn?.(`LibraVDB afterTurn trimmed oversized ingest payload sessionId=${sessionId} ` +
|
|
239
|
+
`estimatedTokens=${estimatedTokens} maxTokens=${AFTER_TURN_INGEST_MAX_TOKENS} ` +
|
|
240
|
+
`forwardedMessages=${bounded.length}`);
|
|
241
|
+
return bounded;
|
|
242
|
+
}
|
|
229
243
|
function enforceTokenBudgetInvariant(result, tokenBudget) {
|
|
230
244
|
if (typeof tokenBudget !== "number" || !Number.isFinite(tokenBudget) || tokenBudget <= 0) {
|
|
231
245
|
return result;
|
|
@@ -262,11 +276,19 @@ function buildBudgetFallbackContext(messages, tokenBudget) {
|
|
|
262
276
|
messages: fallbackMessages,
|
|
263
277
|
estimatedTokens: approximateMessagesTokens(fallbackMessages),
|
|
264
278
|
systemPromptAddition: "",
|
|
279
|
+
promptAuthority: PROMPT_AUTHORITY_PREASSEMBLY_MAY_OVERFLOW,
|
|
265
280
|
};
|
|
266
281
|
}
|
|
267
282
|
function resolvePredictiveCompactionTokenCount(args) {
|
|
268
|
-
|
|
269
|
-
|
|
283
|
+
const currentTokenCount = normalizeCurrentTokenCount(args.currentTokenCount);
|
|
284
|
+
const sourcePressureEstimate = normalizeCurrentTokenCount(approximateMessagesTokens(args.messages) + approximateTokenCount(args.prompt ?? ""));
|
|
285
|
+
if (currentTokenCount == null) {
|
|
286
|
+
return sourcePressureEstimate ?? 1;
|
|
287
|
+
}
|
|
288
|
+
if (sourcePressureEstimate == null) {
|
|
289
|
+
return currentTokenCount;
|
|
290
|
+
}
|
|
291
|
+
return Math.max(currentTokenCount, sourcePressureEstimate);
|
|
270
292
|
}
|
|
271
293
|
function resolveAfterTurnPredictiveCompactionTokenCount(args) {
|
|
272
294
|
const currentTokenCount = normalizeCurrentTokenCount(args.currentTokenCount);
|
|
@@ -516,6 +538,7 @@ export function normalizeAssembleResult(result) {
|
|
|
516
538
|
messages,
|
|
517
539
|
estimatedTokens: typeof result.estimatedTokens === "number" ? result.estimatedTokens : 0,
|
|
518
540
|
systemPromptAddition: typeof result.systemPromptAddition === "string" ? result.systemPromptAddition : "",
|
|
541
|
+
promptAuthority: PROMPT_AUTHORITY_PREASSEMBLY_MAY_OVERFLOW,
|
|
519
542
|
...(result.debug != null ? { debug: result.debug } : {}),
|
|
520
543
|
};
|
|
521
544
|
}
|
|
@@ -902,6 +925,7 @@ export function buildContextEngineFactory(runtime, cfg, logger = console) {
|
|
|
902
925
|
});
|
|
903
926
|
const afterTurnMessages = selectAfterTurnMessages(args.messages, args.prePromptMessageCount, logger);
|
|
904
927
|
const messages = normalizeKernelMessages(afterTurnMessages);
|
|
928
|
+
const ingestMessages = boundAfterTurnMessagesForIngest(messages, logger, sessionId);
|
|
905
929
|
const msgCount = messages.length;
|
|
906
930
|
logger.info?.(`LibraVDB afterTurn sessionId=${sessionId} userId=${userId} ` +
|
|
907
931
|
`messageCount=${msgCount} totalMessages=${args.messages.length} ` +
|
|
@@ -916,7 +940,7 @@ export function buildContextEngineFactory(runtime, cfg, logger = console) {
|
|
|
916
940
|
sessionId,
|
|
917
941
|
sessionKey: args.sessionKey,
|
|
918
942
|
userId,
|
|
919
|
-
messages,
|
|
943
|
+
messages: ingestMessages,
|
|
920
944
|
isHeartbeat: args.isHeartbeat,
|
|
921
945
|
});
|
|
922
946
|
await performAfterTurnPredictiveCompaction({
|
package/dist/index.js
CHANGED
|
@@ -26516,6 +26516,7 @@ function resolveCliMemoryOperationScope(opts) {
|
|
|
26516
26516
|
|
|
26517
26517
|
// src/context-engine.ts
|
|
26518
26518
|
var APPROX_CHARS_PER_TOKEN = 4;
|
|
26519
|
+
var PROMPT_AUTHORITY_PREASSEMBLY_MAY_OVERFLOW = "preassembly_may_overflow";
|
|
26519
26520
|
var ASSEMBLE_BUDGET_HEADROOM_TOKENS = 256;
|
|
26520
26521
|
var ASSEMBLE_BUDGET_HEADROOM_FRACTION = 0.2;
|
|
26521
26522
|
var DEFAULT_COMPACTION_THRESHOLD_FRACTION = 0.8;
|
|
@@ -26525,6 +26526,7 @@ var QUOTED_PHRASE_RE = /"([^"]{4,})"|'([^']{4,})'/g;
|
|
|
26525
26526
|
var EXACT_RECALL_SEARCH_K = 32;
|
|
26526
26527
|
var EXACT_RECALL_MAX_TOKENS = 4;
|
|
26527
26528
|
var RESERVED_CURRENT_TURN_TOKENS = 150;
|
|
26529
|
+
var AFTER_TURN_INGEST_MAX_TOKENS = 2048;
|
|
26528
26530
|
var COMMON_QUERY_WORDS = /* @__PURE__ */ new Set([
|
|
26529
26531
|
"what",
|
|
26530
26532
|
"does",
|
|
@@ -26749,6 +26751,17 @@ function trimMessagesToBudget(messages, tokenBudget) {
|
|
|
26749
26751
|
}
|
|
26750
26752
|
return [{ ...last, content: truncated }];
|
|
26751
26753
|
}
|
|
26754
|
+
function boundAfterTurnMessagesForIngest(messages, logger, sessionId) {
|
|
26755
|
+
const estimatedTokens = approximateMessagesTokens(messages);
|
|
26756
|
+
if (estimatedTokens <= AFTER_TURN_INGEST_MAX_TOKENS) {
|
|
26757
|
+
return messages;
|
|
26758
|
+
}
|
|
26759
|
+
const bounded = trimMessagesToBudget(messages, AFTER_TURN_INGEST_MAX_TOKENS).map((message) => normalizeKernelMessage(message));
|
|
26760
|
+
logger.warn?.(
|
|
26761
|
+
`LibraVDB afterTurn trimmed oversized ingest payload sessionId=${sessionId} estimatedTokens=${estimatedTokens} maxTokens=${AFTER_TURN_INGEST_MAX_TOKENS} forwardedMessages=${bounded.length}`
|
|
26762
|
+
);
|
|
26763
|
+
return bounded;
|
|
26764
|
+
}
|
|
26752
26765
|
function enforceTokenBudgetInvariant(result, tokenBudget) {
|
|
26753
26766
|
if (typeof tokenBudget !== "number" || !Number.isFinite(tokenBudget) || tokenBudget <= 0) {
|
|
26754
26767
|
return result;
|
|
@@ -26787,11 +26800,22 @@ function buildBudgetFallbackContext(messages, tokenBudget) {
|
|
|
26787
26800
|
return {
|
|
26788
26801
|
messages: fallbackMessages,
|
|
26789
26802
|
estimatedTokens: approximateMessagesTokens(fallbackMessages),
|
|
26790
|
-
systemPromptAddition: ""
|
|
26803
|
+
systemPromptAddition: "",
|
|
26804
|
+
promptAuthority: PROMPT_AUTHORITY_PREASSEMBLY_MAY_OVERFLOW
|
|
26791
26805
|
};
|
|
26792
26806
|
}
|
|
26793
26807
|
function resolvePredictiveCompactionTokenCount(args) {
|
|
26794
|
-
|
|
26808
|
+
const currentTokenCount = normalizeCurrentTokenCount(args.currentTokenCount);
|
|
26809
|
+
const sourcePressureEstimate = normalizeCurrentTokenCount(
|
|
26810
|
+
approximateMessagesTokens(args.messages) + approximateTokenCount(args.prompt ?? "")
|
|
26811
|
+
);
|
|
26812
|
+
if (currentTokenCount == null) {
|
|
26813
|
+
return sourcePressureEstimate ?? 1;
|
|
26814
|
+
}
|
|
26815
|
+
if (sourcePressureEstimate == null) {
|
|
26816
|
+
return currentTokenCount;
|
|
26817
|
+
}
|
|
26818
|
+
return Math.max(currentTokenCount, sourcePressureEstimate);
|
|
26795
26819
|
}
|
|
26796
26820
|
function resolveAfterTurnPredictiveCompactionTokenCount(args) {
|
|
26797
26821
|
const currentTokenCount = normalizeCurrentTokenCount(args.currentTokenCount);
|
|
@@ -27031,6 +27055,7 @@ function normalizeAssembleResult(result) {
|
|
|
27031
27055
|
messages,
|
|
27032
27056
|
estimatedTokens: typeof result.estimatedTokens === "number" ? result.estimatedTokens : 0,
|
|
27033
27057
|
systemPromptAddition: typeof result.systemPromptAddition === "string" ? result.systemPromptAddition : "",
|
|
27058
|
+
promptAuthority: PROMPT_AUTHORITY_PREASSEMBLY_MAY_OVERFLOW,
|
|
27034
27059
|
...result.debug != null ? { debug: result.debug } : {}
|
|
27035
27060
|
};
|
|
27036
27061
|
}
|
|
@@ -27416,6 +27441,7 @@ function buildContextEngineFactory(runtime, cfg, logger = console) {
|
|
|
27416
27441
|
});
|
|
27417
27442
|
const afterTurnMessages = selectAfterTurnMessages(args.messages, args.prePromptMessageCount, logger);
|
|
27418
27443
|
const messages = normalizeKernelMessages(afterTurnMessages);
|
|
27444
|
+
const ingestMessages = boundAfterTurnMessagesForIngest(messages, logger, sessionId);
|
|
27419
27445
|
const msgCount = messages.length;
|
|
27420
27446
|
logger.info?.(
|
|
27421
27447
|
`LibraVDB afterTurn sessionId=${sessionId} userId=${userId} messageCount=${msgCount} totalMessages=${args.messages.length} prePromptMessageCount=${args.prePromptMessageCount ?? "unknown"} heartbeat=${args.isHeartbeat ?? false}`
|
|
@@ -27429,7 +27455,7 @@ function buildContextEngineFactory(runtime, cfg, logger = console) {
|
|
|
27429
27455
|
sessionId,
|
|
27430
27456
|
sessionKey: args.sessionKey,
|
|
27431
27457
|
userId,
|
|
27432
|
-
messages,
|
|
27458
|
+
messages: ingestMessages,
|
|
27433
27459
|
isHeartbeat: args.isHeartbeat
|
|
27434
27460
|
});
|
|
27435
27461
|
await performAfterTurnPredictiveCompaction({
|
package/openclaw.plugin.json
CHANGED