@xdarkicex/openclaw-memory-libravdb 1.9.10-beta.10 → 1.9.10-beta.12

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.
@@ -16,6 +16,11 @@ const EXACT_RECALL_MAX_TOKENS = 4;
16
16
  const RESERVED_CURRENT_TURN_TOKENS = 150;
17
17
  const AFTER_TURN_INGEST_MAX_TOKENS = 2048;
18
18
  const OPENCLAW_LEADING_TIMESTAMP_PREFIX_RE = /^\[[A-Za-z]{3} \d{4}-\d{2}-\d{2} \d{2}:\d{2}[^\]]*\] */;
19
+ const OPENCLAW_CONTEXT_PREFIX_RE = /^\[OpenClaw context: [^\]]*\][\r\n]*/;
20
+ const SELECTED_CONTEXT_HEADER = "Conversation context (untrusted, chronological, selected for current message):";
21
+ const RETRIEVAL_QUERY_MAX_CHARS = 1000;
22
+ const SELECTED_CONTEXT_TURN_RE = /^#\d+\s+[A-Za-z]{3}\s+\d{4}-\d{2}-\d{2}\s+\d{2}:\d{2}(?::\d{2})?\s+\S+\s+([^:\n]{1,80}):\s*(.*)$/;
23
+ const ASSISTANT_SPEAKER_RE = /\b(?:assistant|openclaw|z3robot|bot)\b/i;
19
24
  const OPENCLAW_METADATA_HEADERS = [
20
25
  "Conversation info (untrusted metadata):",
21
26
  "Sender (untrusted metadata):",
@@ -196,6 +201,55 @@ function normalizeKernelContent(content, options = {}) {
196
201
  retainContext: options.retainOpenClawContext === true,
197
202
  });
198
203
  }
204
+ function normalizeRetrievalQuery(primaryText, fallbackText = "") {
205
+ const primary = normalizeRetrievalCandidate(primaryText);
206
+ if (primary)
207
+ return primary;
208
+ return normalizeRetrievalCandidate(fallbackText);
209
+ }
210
+ function normalizeRetrievalCandidate(text) {
211
+ const normalized = text
212
+ .replace(OPENCLAW_CONTEXT_PREFIX_RE, "")
213
+ .replace(OPENCLAW_LEADING_TIMESTAMP_PREFIX_RE, "")
214
+ .replace(/\r\n/g, "\n")
215
+ .trim();
216
+ if (!normalized)
217
+ return "";
218
+ const selected = extractLatestSelectedContextUtterance(normalized);
219
+ const candidate = selected || normalized;
220
+ return capRetrievalQuery(candidate.replace(/\s+/g, " ").trim());
221
+ }
222
+ function extractLatestSelectedContextUtterance(text) {
223
+ if (!text.startsWith(SELECTED_CONTEXT_HEADER))
224
+ return "";
225
+ const turns = [];
226
+ for (const rawLine of text.slice(SELECTED_CONTEXT_HEADER.length).split("\n")) {
227
+ const line = rawLine.trim();
228
+ if (!line)
229
+ continue;
230
+ const match = line.match(SELECTED_CONTEXT_TURN_RE);
231
+ if (match) {
232
+ turns.push({ speaker: match[1].trim(), text: [match[2].trim()] });
233
+ continue;
234
+ }
235
+ if (turns.length > 0) {
236
+ turns[turns.length - 1].text.push(line);
237
+ }
238
+ }
239
+ for (let i = turns.length - 1; i >= 0; i--) {
240
+ const turn = turns[i];
241
+ const content = turn.text.join(" ").trim();
242
+ if (content && !ASSISTANT_SPEAKER_RE.test(turn.speaker)) {
243
+ return content;
244
+ }
245
+ }
246
+ return "";
247
+ }
248
+ function capRetrievalQuery(text) {
249
+ if (text.length <= RETRIEVAL_QUERY_MAX_CHARS)
250
+ return text;
251
+ return text.slice(text.length - RETRIEVAL_QUERY_MAX_CHARS);
252
+ }
199
253
  /**
200
254
  * Symbol-keyed hook that drains all pending async ingestion queues.
201
255
  * Tests import this symbol to access the drain function. Using a
@@ -1342,7 +1396,6 @@ export function buildContextEngineFactory(runtime, cfg, logger = console) {
1342
1396
  // Then run the standard metadata envelope stripping.
1343
1397
  return stripOpenClawUntrustedMetadataEnvelope(stripped);
1344
1398
  }
1345
- const OPENCLAW_CONTEXT_PREFIX_RE = /^\[OpenClaw context: [^\]]*\][\r\n]*/;
1346
1399
  function formatRetrievedMemory(predictions) {
1347
1400
  if (!predictions?.length)
1348
1401
  return "";
@@ -1721,6 +1774,8 @@ export function buildContextEngineFactory(runtime, cfg, logger = console) {
1721
1774
  const isPostToolContinuation = lastUserIndex >= 0 && lastUserIndex < messages.length - 1
1722
1775
  && hasLiveToolProtocolAfterLastUser(messages, lastUserIndex);
1723
1776
  const lastUserMessage = findLastReplaySafeUserMessage(messages);
1777
+ const latestUserContent = typeof lastUserMessage?.content === "string" ? lastUserMessage.content : "";
1778
+ const retrievalQuery = normalizeRetrievalQuery(strippedPrompt, latestUserContent);
1724
1779
  const reservedCurrentTurnTokens = lastUserMessage
1725
1780
  ? approximateMessageTokens(lastUserMessage)
1726
1781
  : RESERVED_CURRENT_TURN_TOKENS;
@@ -1856,7 +1911,7 @@ export function buildContextEngineFactory(runtime, cfg, logger = console) {
1856
1911
  ]);
1857
1912
  const maxMemories = cfg.beforeTurnMaxMemories ?? 5;
1858
1913
  const clamped = btResult.predictions && btResult.predictions.length > maxMemories
1859
- ? selectTopByRelevance(btResult.predictions, strippedPrompt, maxMemories)
1914
+ ? selectTopByRelevance(btResult.predictions, retrievalQuery, maxMemories)
1860
1915
  : btResult.predictions;
1861
1916
  turnCache.set(sessionId, `${messages.length}:${beforeTurnQueryHint}`, { predictions: clamped });
1862
1917
  beforeTurnPredictions = clamped;
@@ -1873,7 +1928,7 @@ export function buildContextEngineFactory(runtime, cfg, logger = console) {
1873
1928
  sessionId,
1874
1929
  sessionKey: args.sessionKey,
1875
1930
  userId,
1876
- prompt: strippedPrompt,
1931
+ prompt: retrievalQuery,
1877
1932
  messages,
1878
1933
  tokenBudget: args.tokenBudget,
1879
1934
  config: buildAssemblyConfig(args.tokenBudget),
@@ -1894,7 +1949,7 @@ export function buildContextEngineFactory(runtime, cfg, logger = console) {
1894
1949
  ? { ...assembled, systemPromptAddition: appendSystemPromptAddition(assembled.systemPromptAddition, continuityContext) }
1895
1950
  : assembled;
1896
1951
  enforced = enforceTokenBudgetInvariant(await augmentWithExactRecall(withContinuity, {
1897
- queryText: strippedPrompt || (messages[messages.length - 1]?.content ?? ""),
1952
+ queryText: retrievalQuery,
1898
1953
  userId,
1899
1954
  sessionId,
1900
1955
  tokenBudget: args.tokenBudget,
package/dist/index.js CHANGED
@@ -35491,6 +35491,11 @@ var EXACT_RECALL_MAX_TOKENS = 4;
35491
35491
  var RESERVED_CURRENT_TURN_TOKENS = 150;
35492
35492
  var AFTER_TURN_INGEST_MAX_TOKENS = 2048;
35493
35493
  var OPENCLAW_LEADING_TIMESTAMP_PREFIX_RE = /^\[[A-Za-z]{3} \d{4}-\d{2}-\d{2} \d{2}:\d{2}[^\]]*\] */;
35494
+ var OPENCLAW_CONTEXT_PREFIX_RE = /^\[OpenClaw context: [^\]]*\][\r\n]*/;
35495
+ var SELECTED_CONTEXT_HEADER = "Conversation context (untrusted, chronological, selected for current message):";
35496
+ var RETRIEVAL_QUERY_MAX_CHARS = 1e3;
35497
+ var SELECTED_CONTEXT_TURN_RE = /^#\d+\s+[A-Za-z]{3}\s+\d{4}-\d{2}-\d{2}\s+\d{2}:\d{2}(?::\d{2})?\s+\S+\s+([^:\n]{1,80}):\s*(.*)$/;
35498
+ var ASSISTANT_SPEAKER_RE = /\b(?:assistant|openclaw|z3robot|bot)\b/i;
35494
35499
  var OPENCLAW_METADATA_HEADERS = [
35495
35500
  "Conversation info (untrusted metadata):",
35496
35501
  "Sender (untrusted metadata):",
@@ -35656,6 +35661,46 @@ function normalizeKernelContent(content, options = {}) {
35656
35661
  retainContext: options.retainOpenClawContext === true
35657
35662
  });
35658
35663
  }
35664
+ function normalizeRetrievalQuery(primaryText, fallbackText = "") {
35665
+ const primary = normalizeRetrievalCandidate(primaryText);
35666
+ if (primary) return primary;
35667
+ return normalizeRetrievalCandidate(fallbackText);
35668
+ }
35669
+ function normalizeRetrievalCandidate(text) {
35670
+ const normalized = text.replace(OPENCLAW_CONTEXT_PREFIX_RE, "").replace(OPENCLAW_LEADING_TIMESTAMP_PREFIX_RE, "").replace(/\r\n/g, "\n").trim();
35671
+ if (!normalized) return "";
35672
+ const selected = extractLatestSelectedContextUtterance(normalized);
35673
+ const candidate = selected || normalized;
35674
+ return capRetrievalQuery(candidate.replace(/\s+/g, " ").trim());
35675
+ }
35676
+ function extractLatestSelectedContextUtterance(text) {
35677
+ if (!text.startsWith(SELECTED_CONTEXT_HEADER)) return "";
35678
+ const turns = [];
35679
+ for (const rawLine of text.slice(SELECTED_CONTEXT_HEADER.length).split("\n")) {
35680
+ const line = rawLine.trim();
35681
+ if (!line) continue;
35682
+ const match = line.match(SELECTED_CONTEXT_TURN_RE);
35683
+ if (match) {
35684
+ turns.push({ speaker: match[1].trim(), text: [match[2].trim()] });
35685
+ continue;
35686
+ }
35687
+ if (turns.length > 0) {
35688
+ turns[turns.length - 1].text.push(line);
35689
+ }
35690
+ }
35691
+ for (let i = turns.length - 1; i >= 0; i--) {
35692
+ const turn = turns[i];
35693
+ const content = turn.text.join(" ").trim();
35694
+ if (content && !ASSISTANT_SPEAKER_RE.test(turn.speaker)) {
35695
+ return content;
35696
+ }
35697
+ }
35698
+ return "";
35699
+ }
35700
+ function capRetrievalQuery(text) {
35701
+ if (text.length <= RETRIEVAL_QUERY_MAX_CHARS) return text;
35702
+ return text.slice(text.length - RETRIEVAL_QUERY_MAX_CHARS);
35703
+ }
35659
35704
  var FLUSH_ASYNC_INGESTION = /* @__PURE__ */ Symbol("flushAsyncIngestion");
35660
35705
  var maxOptimizationMemoCacheSize = 5e4;
35661
35706
  var metadataEnvelopeCache = /* @__PURE__ */ new Map();
@@ -36593,7 +36638,6 @@ function buildContextEngineFactory(runtime, cfg, logger = console) {
36593
36638
  const stripped = text.replace(OPENCLAW_CONTEXT_PREFIX_RE, "").trimStart();
36594
36639
  return stripOpenClawUntrustedMetadataEnvelope(stripped);
36595
36640
  }
36596
- const OPENCLAW_CONTEXT_PREFIX_RE = /^\[OpenClaw context: [^\]]*\][\r\n]*/;
36597
36641
  function formatRetrievedMemory(predictions) {
36598
36642
  if (!predictions?.length) return "";
36599
36643
  if (cfg.beforeTurnDebug) {
@@ -36950,6 +36994,8 @@ function buildContextEngineFactory(runtime, cfg, logger = console) {
36950
36994
  const lastUserIndex = findLastUserMessageIndex(messages);
36951
36995
  const isPostToolContinuation = lastUserIndex >= 0 && lastUserIndex < messages.length - 1 && hasLiveToolProtocolAfterLastUser(messages, lastUserIndex);
36952
36996
  const lastUserMessage = findLastReplaySafeUserMessage(messages);
36997
+ const latestUserContent = typeof lastUserMessage?.content === "string" ? lastUserMessage.content : "";
36998
+ const retrievalQuery = normalizeRetrievalQuery(strippedPrompt, latestUserContent);
36953
36999
  const reservedCurrentTurnTokens = lastUserMessage ? approximateMessageTokens(lastUserMessage) : RESERVED_CURRENT_TURN_TOKENS;
36954
37000
  const currentContextTokens = resolvePredictiveCompactionTokenCount({
36955
37001
  currentTokenCount: args.currentTokenCount,
@@ -37084,7 +37130,7 @@ function buildContextEngineFactory(runtime, cfg, logger = console) {
37084
37130
  )
37085
37131
  ]);
37086
37132
  const maxMemories = cfg.beforeTurnMaxMemories ?? 5;
37087
- const clamped = btResult.predictions && btResult.predictions.length > maxMemories ? selectTopByRelevance(btResult.predictions, strippedPrompt, maxMemories) : btResult.predictions;
37133
+ const clamped = btResult.predictions && btResult.predictions.length > maxMemories ? selectTopByRelevance(btResult.predictions, retrievalQuery, maxMemories) : btResult.predictions;
37088
37134
  turnCache.set(sessionId, `${messages.length}:${beforeTurnQueryHint}`, { predictions: clamped });
37089
37135
  beforeTurnPredictions = clamped;
37090
37136
  clearBeforeTurnCircuit(sessionId);
@@ -37101,7 +37147,7 @@ function buildContextEngineFactory(runtime, cfg, logger = console) {
37101
37147
  sessionId,
37102
37148
  sessionKey: args.sessionKey,
37103
37149
  userId,
37104
- prompt: strippedPrompt,
37150
+ prompt: retrievalQuery,
37105
37151
  messages,
37106
37152
  tokenBudget: args.tokenBudget,
37107
37153
  config: buildAssemblyConfig(args.tokenBudget),
@@ -37123,7 +37169,7 @@ function buildContextEngineFactory(runtime, cfg, logger = console) {
37123
37169
  const withContinuity = continuityContext ? { ...assembled, systemPromptAddition: appendSystemPromptAddition(assembled.systemPromptAddition, continuityContext) } : assembled;
37124
37170
  enforced = enforceTokenBudgetInvariant(
37125
37171
  await augmentWithExactRecall(withContinuity, {
37126
- queryText: strippedPrompt || (messages[messages.length - 1]?.content ?? ""),
37172
+ queryText: retrievalQuery,
37127
37173
  userId,
37128
37174
  sessionId,
37129
37175
  tokenBudget: args.tokenBudget,
@@ -2,7 +2,7 @@
2
2
  "id": "libravdb-memory",
3
3
  "name": "LibraVDB Memory",
4
4
  "description": "Persistent vector memory with three-tier hybrid scoring",
5
- "version": "1.9.10-beta.10",
5
+ "version": "1.9.10-beta.12",
6
6
  "kind": [
7
7
  "memory",
8
8
  "context-engine"
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xdarkicex/openclaw-memory-libravdb",
3
- "version": "1.9.10-beta.10",
3
+ "version": "1.9.10-beta.12",
4
4
  "type": "module",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",