@xdarkicex/openclaw-memory-libravdb 1.6.17 → 1.6.19
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/README.md +3 -0
- package/dist/context-engine.d.ts +5 -0
- package/dist/context-engine.js +67 -4
- package/dist/index.js +62 -4
- package/openclaw.plugin.json +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -49,6 +49,9 @@ brew services start libravdbd
|
|
|
49
49
|
**Linux (APT)**
|
|
50
50
|
|
|
51
51
|
```bash
|
|
52
|
+
curl -fsSL https://xDarkicex.github.io/apt-libravdbd/gpg.key | sudo gpg --dearmor -o /etc/apt/trusted.gpg.d/libravdbd.gpg
|
|
53
|
+
echo "deb https://xDarkicex.github.io/apt-libravdbd stable main" | sudo tee /etc/apt/sources.list.d/libravdbd.list
|
|
54
|
+
sudo apt update
|
|
52
55
|
sudo apt install libravdbd
|
|
53
56
|
systemctl --user enable --now libravdbd
|
|
54
57
|
```
|
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 = {
|
|
@@ -88,6 +90,9 @@ export declare function buildContextEngineFactory(runtime: PluginRuntime, cfg: P
|
|
|
88
90
|
targetSize?: number;
|
|
89
91
|
tokenBudget?: number;
|
|
90
92
|
currentTokenCount?: number;
|
|
93
|
+
compactionTarget?: "budget" | "threshold";
|
|
94
|
+
runtimeContext?: Record<string, unknown>;
|
|
95
|
+
abortSignal?: AbortSignal;
|
|
91
96
|
}): Promise<OpenClawCompatibleCompactResult>;
|
|
92
97
|
afterTurn(args: {
|
|
93
98
|
sessionId: string;
|
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",
|
|
@@ -171,6 +173,13 @@ function resolvePredictiveCompactionTarget(params) {
|
|
|
171
173
|
? belowThresholdTarget
|
|
172
174
|
: Math.max(1, currentTokenCount - 1);
|
|
173
175
|
}
|
|
176
|
+
function readRuntimeNumber(runtimeContext, key) {
|
|
177
|
+
const value = runtimeContext?.[key];
|
|
178
|
+
return typeof value === "number" && Number.isFinite(value) ? value : undefined;
|
|
179
|
+
}
|
|
180
|
+
function isManualCompactionRequested(runtimeContext) {
|
|
181
|
+
return runtimeContext?.manualCompaction === true;
|
|
182
|
+
}
|
|
174
183
|
function logPredictiveCompactionAttempt(params) {
|
|
175
184
|
params.logger.info?.(`LibraVDB predictive compaction trigger phase=${params.phase} sessionId=${params.sessionId} ` +
|
|
176
185
|
`currentTokenCount=${params.currentTokenCount} threshold=${params.threshold} ` +
|
|
@@ -226,6 +235,18 @@ function trimMessagesToBudget(messages, tokenBudget) {
|
|
|
226
235
|
}
|
|
227
236
|
return [{ ...last, content: truncated }];
|
|
228
237
|
}
|
|
238
|
+
function boundAfterTurnMessagesForIngest(messages, logger, sessionId) {
|
|
239
|
+
const estimatedTokens = approximateMessagesTokens(messages);
|
|
240
|
+
if (estimatedTokens <= AFTER_TURN_INGEST_MAX_TOKENS) {
|
|
241
|
+
return messages;
|
|
242
|
+
}
|
|
243
|
+
const bounded = trimMessagesToBudget(messages, AFTER_TURN_INGEST_MAX_TOKENS)
|
|
244
|
+
.map((message) => normalizeKernelMessage(message));
|
|
245
|
+
logger.warn?.(`LibraVDB afterTurn trimmed oversized ingest payload sessionId=${sessionId} ` +
|
|
246
|
+
`estimatedTokens=${estimatedTokens} maxTokens=${AFTER_TURN_INGEST_MAX_TOKENS} ` +
|
|
247
|
+
`forwardedMessages=${bounded.length}`);
|
|
248
|
+
return bounded;
|
|
249
|
+
}
|
|
229
250
|
function enforceTokenBudgetInvariant(result, tokenBudget) {
|
|
230
251
|
if (typeof tokenBudget !== "number" || !Number.isFinite(tokenBudget) || tokenBudget <= 0) {
|
|
231
252
|
return result;
|
|
@@ -262,11 +283,19 @@ function buildBudgetFallbackContext(messages, tokenBudget) {
|
|
|
262
283
|
messages: fallbackMessages,
|
|
263
284
|
estimatedTokens: approximateMessagesTokens(fallbackMessages),
|
|
264
285
|
systemPromptAddition: "",
|
|
286
|
+
promptAuthority: PROMPT_AUTHORITY_PREASSEMBLY_MAY_OVERFLOW,
|
|
265
287
|
};
|
|
266
288
|
}
|
|
267
289
|
function resolvePredictiveCompactionTokenCount(args) {
|
|
268
|
-
|
|
269
|
-
|
|
290
|
+
const currentTokenCount = normalizeCurrentTokenCount(args.currentTokenCount);
|
|
291
|
+
const sourcePressureEstimate = normalizeCurrentTokenCount(approximateMessagesTokens(args.messages) + approximateTokenCount(args.prompt ?? ""));
|
|
292
|
+
if (currentTokenCount == null) {
|
|
293
|
+
return sourcePressureEstimate ?? 1;
|
|
294
|
+
}
|
|
295
|
+
if (sourcePressureEstimate == null) {
|
|
296
|
+
return currentTokenCount;
|
|
297
|
+
}
|
|
298
|
+
return Math.max(currentTokenCount, sourcePressureEstimate);
|
|
270
299
|
}
|
|
271
300
|
function resolveAfterTurnPredictiveCompactionTokenCount(args) {
|
|
272
301
|
const currentTokenCount = normalizeCurrentTokenCount(args.currentTokenCount);
|
|
@@ -516,6 +545,7 @@ export function normalizeAssembleResult(result) {
|
|
|
516
545
|
messages,
|
|
517
546
|
estimatedTokens: typeof result.estimatedTokens === "number" ? result.estimatedTokens : 0,
|
|
518
547
|
systemPromptAddition: typeof result.systemPromptAddition === "string" ? result.systemPromptAddition : "",
|
|
548
|
+
promptAuthority: PROMPT_AUTHORITY_PREASSEMBLY_MAY_OVERFLOW,
|
|
519
549
|
...(result.debug != null ? { debug: result.debug } : {}),
|
|
520
550
|
};
|
|
521
551
|
}
|
|
@@ -892,7 +922,39 @@ export function buildContextEngineFactory(runtime, cfg, logger = console) {
|
|
|
892
922
|
}
|
|
893
923
|
},
|
|
894
924
|
async compact(args) {
|
|
895
|
-
|
|
925
|
+
const tokenBudget = normalizeTokenBudget(args.tokenBudget) ??
|
|
926
|
+
normalizeTokenBudget(readRuntimeNumber(args.runtimeContext, "tokenBudget"));
|
|
927
|
+
const currentTokenCount = normalizeCurrentTokenCount(args.currentTokenCount) ??
|
|
928
|
+
normalizeCurrentTokenCount(readRuntimeNumber(args.runtimeContext, "currentTokenCount"));
|
|
929
|
+
const forceCompaction = args.force === true || isManualCompactionRequested(args.runtimeContext);
|
|
930
|
+
const threshold = getDynamicCompactThreshold(tokenBudget);
|
|
931
|
+
if (!forceCompaction &&
|
|
932
|
+
currentTokenCount != null &&
|
|
933
|
+
threshold != null &&
|
|
934
|
+
currentTokenCount < threshold) {
|
|
935
|
+
return {
|
|
936
|
+
ok: true,
|
|
937
|
+
compacted: false,
|
|
938
|
+
reason: "below threshold",
|
|
939
|
+
result: {
|
|
940
|
+
tokensBefore: currentTokenCount,
|
|
941
|
+
details: {
|
|
942
|
+
threshold,
|
|
943
|
+
targetTokens: args.compactionTarget === "threshold" ? threshold : tokenBudget,
|
|
944
|
+
},
|
|
945
|
+
},
|
|
946
|
+
};
|
|
947
|
+
}
|
|
948
|
+
const runArgs = {
|
|
949
|
+
...args,
|
|
950
|
+
force: forceCompaction || args.force,
|
|
951
|
+
...(tokenBudget != null ? { tokenBudget } : {}),
|
|
952
|
+
...(currentTokenCount != null ? { currentTokenCount } : {}),
|
|
953
|
+
...(args.compactionTarget === "threshold" && threshold != null
|
|
954
|
+
? { targetSize: threshold }
|
|
955
|
+
: {}),
|
|
956
|
+
};
|
|
957
|
+
return await runCompaction(runArgs);
|
|
896
958
|
},
|
|
897
959
|
async afterTurn(args) {
|
|
898
960
|
const sessionId = requireSessionId(args.sessionId, "afterTurn");
|
|
@@ -902,6 +964,7 @@ export function buildContextEngineFactory(runtime, cfg, logger = console) {
|
|
|
902
964
|
});
|
|
903
965
|
const afterTurnMessages = selectAfterTurnMessages(args.messages, args.prePromptMessageCount, logger);
|
|
904
966
|
const messages = normalizeKernelMessages(afterTurnMessages);
|
|
967
|
+
const ingestMessages = boundAfterTurnMessagesForIngest(messages, logger, sessionId);
|
|
905
968
|
const msgCount = messages.length;
|
|
906
969
|
logger.info?.(`LibraVDB afterTurn sessionId=${sessionId} userId=${userId} ` +
|
|
907
970
|
`messageCount=${msgCount} totalMessages=${args.messages.length} ` +
|
|
@@ -916,7 +979,7 @@ export function buildContextEngineFactory(runtime, cfg, logger = console) {
|
|
|
916
979
|
sessionId,
|
|
917
980
|
sessionKey: args.sessionKey,
|
|
918
981
|
userId,
|
|
919
|
-
messages,
|
|
982
|
+
messages: ingestMessages,
|
|
920
983
|
isHeartbeat: args.isHeartbeat,
|
|
921
984
|
});
|
|
922
985
|
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",
|
|
@@ -26700,6 +26702,13 @@ function resolvePredictiveCompactionTarget(params) {
|
|
|
26700
26702
|
const belowThresholdTarget = Math.max(1, threshold - 1);
|
|
26701
26703
|
return belowThresholdTarget < currentTokenCount ? belowThresholdTarget : Math.max(1, currentTokenCount - 1);
|
|
26702
26704
|
}
|
|
26705
|
+
function readRuntimeNumber(runtimeContext, key) {
|
|
26706
|
+
const value = runtimeContext?.[key];
|
|
26707
|
+
return typeof value === "number" && Number.isFinite(value) ? value : void 0;
|
|
26708
|
+
}
|
|
26709
|
+
function isManualCompactionRequested(runtimeContext) {
|
|
26710
|
+
return runtimeContext?.manualCompaction === true;
|
|
26711
|
+
}
|
|
26703
26712
|
function logPredictiveCompactionAttempt(params) {
|
|
26704
26713
|
params.logger.info?.(
|
|
26705
26714
|
`LibraVDB predictive compaction trigger phase=${params.phase} sessionId=${params.sessionId} currentTokenCount=${params.currentTokenCount} threshold=${params.threshold} targetSize=${params.targetSize} tokenBudget=${params.tokenBudget ?? "unknown"}`
|
|
@@ -26749,6 +26758,17 @@ function trimMessagesToBudget(messages, tokenBudget) {
|
|
|
26749
26758
|
}
|
|
26750
26759
|
return [{ ...last, content: truncated }];
|
|
26751
26760
|
}
|
|
26761
|
+
function boundAfterTurnMessagesForIngest(messages, logger, sessionId) {
|
|
26762
|
+
const estimatedTokens = approximateMessagesTokens(messages);
|
|
26763
|
+
if (estimatedTokens <= AFTER_TURN_INGEST_MAX_TOKENS) {
|
|
26764
|
+
return messages;
|
|
26765
|
+
}
|
|
26766
|
+
const bounded = trimMessagesToBudget(messages, AFTER_TURN_INGEST_MAX_TOKENS).map((message) => normalizeKernelMessage(message));
|
|
26767
|
+
logger.warn?.(
|
|
26768
|
+
`LibraVDB afterTurn trimmed oversized ingest payload sessionId=${sessionId} estimatedTokens=${estimatedTokens} maxTokens=${AFTER_TURN_INGEST_MAX_TOKENS} forwardedMessages=${bounded.length}`
|
|
26769
|
+
);
|
|
26770
|
+
return bounded;
|
|
26771
|
+
}
|
|
26752
26772
|
function enforceTokenBudgetInvariant(result, tokenBudget) {
|
|
26753
26773
|
if (typeof tokenBudget !== "number" || !Number.isFinite(tokenBudget) || tokenBudget <= 0) {
|
|
26754
26774
|
return result;
|
|
@@ -26787,11 +26807,22 @@ function buildBudgetFallbackContext(messages, tokenBudget) {
|
|
|
26787
26807
|
return {
|
|
26788
26808
|
messages: fallbackMessages,
|
|
26789
26809
|
estimatedTokens: approximateMessagesTokens(fallbackMessages),
|
|
26790
|
-
systemPromptAddition: ""
|
|
26810
|
+
systemPromptAddition: "",
|
|
26811
|
+
promptAuthority: PROMPT_AUTHORITY_PREASSEMBLY_MAY_OVERFLOW
|
|
26791
26812
|
};
|
|
26792
26813
|
}
|
|
26793
26814
|
function resolvePredictiveCompactionTokenCount(args) {
|
|
26794
|
-
|
|
26815
|
+
const currentTokenCount = normalizeCurrentTokenCount(args.currentTokenCount);
|
|
26816
|
+
const sourcePressureEstimate = normalizeCurrentTokenCount(
|
|
26817
|
+
approximateMessagesTokens(args.messages) + approximateTokenCount(args.prompt ?? "")
|
|
26818
|
+
);
|
|
26819
|
+
if (currentTokenCount == null) {
|
|
26820
|
+
return sourcePressureEstimate ?? 1;
|
|
26821
|
+
}
|
|
26822
|
+
if (sourcePressureEstimate == null) {
|
|
26823
|
+
return currentTokenCount;
|
|
26824
|
+
}
|
|
26825
|
+
return Math.max(currentTokenCount, sourcePressureEstimate);
|
|
26795
26826
|
}
|
|
26796
26827
|
function resolveAfterTurnPredictiveCompactionTokenCount(args) {
|
|
26797
26828
|
const currentTokenCount = normalizeCurrentTokenCount(args.currentTokenCount);
|
|
@@ -27031,6 +27062,7 @@ function normalizeAssembleResult(result) {
|
|
|
27031
27062
|
messages,
|
|
27032
27063
|
estimatedTokens: typeof result.estimatedTokens === "number" ? result.estimatedTokens : 0,
|
|
27033
27064
|
systemPromptAddition: typeof result.systemPromptAddition === "string" ? result.systemPromptAddition : "",
|
|
27065
|
+
promptAuthority: PROMPT_AUTHORITY_PREASSEMBLY_MAY_OVERFLOW,
|
|
27034
27066
|
...result.debug != null ? { debug: result.debug } : {}
|
|
27035
27067
|
};
|
|
27036
27068
|
}
|
|
@@ -27406,7 +27438,32 @@ function buildContextEngineFactory(runtime, cfg, logger = console) {
|
|
|
27406
27438
|
}
|
|
27407
27439
|
},
|
|
27408
27440
|
async compact(args) {
|
|
27409
|
-
|
|
27441
|
+
const tokenBudget = normalizeTokenBudget(args.tokenBudget) ?? normalizeTokenBudget(readRuntimeNumber(args.runtimeContext, "tokenBudget"));
|
|
27442
|
+
const currentTokenCount = normalizeCurrentTokenCount(args.currentTokenCount) ?? normalizeCurrentTokenCount(readRuntimeNumber(args.runtimeContext, "currentTokenCount"));
|
|
27443
|
+
const forceCompaction = args.force === true || isManualCompactionRequested(args.runtimeContext);
|
|
27444
|
+
const threshold = getDynamicCompactThreshold(tokenBudget);
|
|
27445
|
+
if (!forceCompaction && currentTokenCount != null && threshold != null && currentTokenCount < threshold) {
|
|
27446
|
+
return {
|
|
27447
|
+
ok: true,
|
|
27448
|
+
compacted: false,
|
|
27449
|
+
reason: "below threshold",
|
|
27450
|
+
result: {
|
|
27451
|
+
tokensBefore: currentTokenCount,
|
|
27452
|
+
details: {
|
|
27453
|
+
threshold,
|
|
27454
|
+
targetTokens: args.compactionTarget === "threshold" ? threshold : tokenBudget
|
|
27455
|
+
}
|
|
27456
|
+
}
|
|
27457
|
+
};
|
|
27458
|
+
}
|
|
27459
|
+
const runArgs = {
|
|
27460
|
+
...args,
|
|
27461
|
+
force: forceCompaction || args.force,
|
|
27462
|
+
...tokenBudget != null ? { tokenBudget } : {},
|
|
27463
|
+
...currentTokenCount != null ? { currentTokenCount } : {},
|
|
27464
|
+
...args.compactionTarget === "threshold" && threshold != null ? { targetSize: threshold } : {}
|
|
27465
|
+
};
|
|
27466
|
+
return await runCompaction(runArgs);
|
|
27410
27467
|
},
|
|
27411
27468
|
async afterTurn(args) {
|
|
27412
27469
|
const sessionId = requireSessionId(args.sessionId, "afterTurn");
|
|
@@ -27416,6 +27473,7 @@ function buildContextEngineFactory(runtime, cfg, logger = console) {
|
|
|
27416
27473
|
});
|
|
27417
27474
|
const afterTurnMessages = selectAfterTurnMessages(args.messages, args.prePromptMessageCount, logger);
|
|
27418
27475
|
const messages = normalizeKernelMessages(afterTurnMessages);
|
|
27476
|
+
const ingestMessages = boundAfterTurnMessagesForIngest(messages, logger, sessionId);
|
|
27419
27477
|
const msgCount = messages.length;
|
|
27420
27478
|
logger.info?.(
|
|
27421
27479
|
`LibraVDB afterTurn sessionId=${sessionId} userId=${userId} messageCount=${msgCount} totalMessages=${args.messages.length} prePromptMessageCount=${args.prePromptMessageCount ?? "unknown"} heartbeat=${args.isHeartbeat ?? false}`
|
|
@@ -27429,7 +27487,7 @@ function buildContextEngineFactory(runtime, cfg, logger = console) {
|
|
|
27429
27487
|
sessionId,
|
|
27430
27488
|
sessionKey: args.sessionKey,
|
|
27431
27489
|
userId,
|
|
27432
|
-
messages,
|
|
27490
|
+
messages: ingestMessages,
|
|
27433
27491
|
isHeartbeat: args.isHeartbeat
|
|
27434
27492
|
});
|
|
27435
27493
|
await performAfterTurnPredictiveCompaction({
|
package/openclaw.plugin.json
CHANGED