@mastra/memory 1.18.2-alpha.1 → 1.18.3-alpha.0

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/CHANGELOG.md CHANGED
@@ -1,5 +1,52 @@
1
1
  # @mastra/memory
2
2
 
3
+ ## 1.18.3-alpha.0
4
+
5
+ ### Patch Changes
6
+
7
+ - feat(memory): start background buffering of unobserved messages when agent goes idle ([#16694](https://github.com/mastra-ai/mastra/pull/16694))
8
+
9
+ In OM buffering mode, when the agent goes idle (turn.end()), any unobserved messages are now buffered in the background via a fire-and-forget buffer() call. This ensures observations are computed proactively rather than waiting for the next turn's step.prepare().
10
+
11
+ ## 1.18.2
12
+
13
+ ### Patch Changes
14
+
15
+ - Fixed thread deletion so it also clears thread-scoped observational memory. ([#16628](https://github.com/mastra-ai/mastra/pull/16628))
16
+
17
+ - Fixed Observational Memory missing the observation threshold for messages with large file parts. Previously `TokenCounter`'s local/sync counting path only stringified the file descriptor (`type`, `mimeType`, `filename`) for non-image files, so a 100KB PDF looked like ~8 tokens to OM and the conversation kept replaying the full unobserved history past every reasonable threshold. ([#16562](https://github.com/mastra-ai/mastra/pull/16562))
18
+
19
+ `TokenCounter` now estimates non-image file part tokens locally from the attachment's byte size and mime type using a per-provider heuristic, mirroring the existing local image-token estimator:
20
+ - Anthropic PDFs ≈ `bytes / 3` (floor 1500)
21
+ - Google PDFs ≈ `bytes / 20` (floor 258)
22
+ - OpenAI / unknown PDFs ≈ `bytes / 4` (floor 500)
23
+ - Text-ish mime types (`text/*`, JSON, XML, YAML) ≈ `bytes / 4`
24
+ - Other binary ≈ `bytes / 4`
25
+
26
+ URL-only file parts (no body to size) keep the previous descriptor-only local estimate. `countMessagesAsync()` continues to prefer provider token-count endpoints for supported providers; this change only improves the local fallback used when no provider endpoint is available.
27
+
28
+ ```ts
29
+ // Before: this PDF counted as ~8 tokens locally regardless of size, so OM never triggered.
30
+ const part = {
31
+ type: 'file',
32
+ data: largePdfBase64,
33
+ mimeType: 'application/pdf',
34
+ filename: 'report.pdf',
35
+ };
36
+ // counter.countMessage(message) ≈ 8
37
+
38
+ // After: estimated locally from byte size on the active provider.
39
+ // counter.countMessage(message) ≈ tens of thousands of tokens
40
+ // → OM threshold trips as expected.
41
+ ```
42
+
43
+ The internal token-estimate cache version was bumped, which invalidates persisted estimates from older `@mastra/memory` releases on the next read; entries are recomputed automatically.
44
+
45
+ Fixes https://github.com/mastra-ai/mastra/issues/16522
46
+
47
+ - Updated dependencies [[`b661349`](https://github.com/mastra-ai/mastra/commit/b661349281514691db78941a9044e6e4f1cde7a7), [`816b974`](https://github.com/mastra-ai/mastra/commit/816b974b424e4a1bfae3af30cc41263b6f1c0344), [`271c044`](https://github.com/mastra-ai/mastra/commit/271c044f6b79ff38cfa3409f4385fbd26a0f3185), [`bad08e9`](https://github.com/mastra-ai/mastra/commit/bad08e99c5291884c3ac76743c78c74f53a302c2), [`816b974`](https://github.com/mastra-ai/mastra/commit/816b974b424e4a1bfae3af30cc41263b6f1c0344), [`b32ba5f`](https://github.com/mastra-ai/mastra/commit/b32ba5fde524b46a4ff1bdf38e30d62a2bb29b04), [`75c7c38`](https://github.com/mastra-ai/mastra/commit/75c7c38a4e9af9821931539dd339f57fcc6414e3)]:
48
+ - @mastra/core@1.35.0
49
+
3
50
  ## 1.18.2-alpha.1
4
51
 
5
52
  ### Patch Changes
@@ -2447,7 +2447,12 @@ var ObservationTurn = class {
2447
2447
  return this._currentStep;
2448
2448
  }
2449
2449
  /**
2450
- * Finalize the turn: save any remaining messages and return the latest record state.
2450
+ * Finalize the turn: save any remaining messages and return the current cached record.
2451
+ *
2452
+ * When async observation buffering is enabled and there are unobserved messages,
2453
+ * a background buffer operation is kicked off so that observations are computed
2454
+ * proactively while the agent is idle, rather than waiting for the next turn.
2455
+ * The returned record does not wait for that background buffering pass to finish.
2451
2456
  */
2452
2457
  async end() {
2453
2458
  if (this._ended) throw new Error("Turn already ended");
@@ -2458,6 +2463,24 @@ var ObservationTurn = class {
2458
2463
  if (unsavedMessages.length > 0) {
2459
2464
  await this.om.persistMessages(unsavedMessages, this.threadId, this.resourceId);
2460
2465
  }
2466
+ if (this.om.buffering.isAsyncObservationEnabled()) {
2467
+ const allMessages = this.messageList.get.all.db();
2468
+ const record = this._record;
2469
+ const unobservedMessages = this.om.getUnobservedMessages(allMessages, record);
2470
+ if (unobservedMessages.length > 0) {
2471
+ void this.om.buffer({
2472
+ threadId: this.threadId,
2473
+ resourceId: this.resourceId,
2474
+ messages: unobservedMessages,
2475
+ record,
2476
+ writer: this.writer,
2477
+ requestContext: this.requestContext,
2478
+ observabilityContext: this.observabilityContext
2479
+ }).catch((err) => {
2480
+ omDebug(`[OM:turn.end] idle buffer failed: ${err?.message}`);
2481
+ });
2482
+ }
2483
+ }
2461
2484
  return { record: this._record };
2462
2485
  }
2463
2486
  /**
@@ -9737,5 +9760,5 @@ exports.stripEphemeralAnchorIds = stripEphemeralAnchorIds;
9737
9760
  exports.stripObservationGroups = stripObservationGroups;
9738
9761
  exports.truncateStringByTokens = truncateStringByTokens;
9739
9762
  exports.wrapInObservationGroup = wrapInObservationGroup;
9740
- //# sourceMappingURL=chunk-4AQHFADP.cjs.map
9741
- //# sourceMappingURL=chunk-4AQHFADP.cjs.map
9763
+ //# sourceMappingURL=chunk-BK3AYI7X.cjs.map
9764
+ //# sourceMappingURL=chunk-BK3AYI7X.cjs.map