@mastra/memory 1.20.0 → 1.20.1-alpha.1

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.
Files changed (40) hide show
  1. package/CHANGELOG.md +25 -0
  2. package/dist/_types/@internal_ai-sdk-v4/dist/index.d.ts +2 -160
  3. package/dist/{chunk-BCWFD5J7.js → chunk-KMJQDCCZ.js} +144 -15
  4. package/dist/chunk-KMJQDCCZ.js.map +1 -0
  5. package/dist/{chunk-SMKIROLC.cjs → chunk-P4BYW7GY.cjs} +143 -14
  6. package/dist/chunk-P4BYW7GY.cjs.map +1 -0
  7. package/dist/docs/SKILL.md +1 -1
  8. package/dist/docs/assets/SOURCE_MAP.json +39 -39
  9. package/dist/docs/references/docs-agents-agent-approval.md +14 -0
  10. package/dist/docs/references/docs-memory-observational-memory.md +19 -0
  11. package/dist/docs/references/docs-memory-semantic-recall.md +1 -1
  12. package/dist/docs/references/reference-memory-observational-memory.md +2 -0
  13. package/dist/docs/references/reference-vectors-pg.md +2 -0
  14. package/dist/index.cjs +199 -203
  15. package/dist/index.cjs.map +1 -1
  16. package/dist/index.d.ts.map +1 -1
  17. package/dist/index.js +190 -194
  18. package/dist/index.js.map +1 -1
  19. package/dist/{observational-memory-LGCWMUEM.cjs → observational-memory-CDLGZL67.cjs} +26 -26
  20. package/dist/{observational-memory-LGCWMUEM.cjs.map → observational-memory-CDLGZL67.cjs.map} +1 -1
  21. package/dist/{observational-memory-E6F7NKJP.js → observational-memory-TJFOX3YN.js} +3 -3
  22. package/dist/{observational-memory-E6F7NKJP.js.map → observational-memory-TJFOX3YN.js.map} +1 -1
  23. package/dist/processors/index.cjs +24 -24
  24. package/dist/processors/index.js +1 -1
  25. package/dist/processors/observational-memory/observation-strategies/types.d.ts +4 -2
  26. package/dist/processors/observational-memory/observation-strategies/types.d.ts.map +1 -1
  27. package/dist/processors/observational-memory/observation-turn/turn.d.ts +5 -1
  28. package/dist/processors/observational-memory/observation-turn/turn.d.ts.map +1 -1
  29. package/dist/processors/observational-memory/observational-memory.d.ts +5 -1
  30. package/dist/processors/observational-memory/observational-memory.d.ts.map +1 -1
  31. package/dist/processors/observational-memory/processor.d.ts.map +1 -1
  32. package/dist/processors/observational-memory/temporal-markers.d.ts.map +1 -1
  33. package/dist/processors/observational-memory/token-counter.d.ts +1 -0
  34. package/dist/processors/observational-memory/token-counter.d.ts.map +1 -1
  35. package/dist/processors/observational-memory/types.d.ts +11 -0
  36. package/dist/processors/observational-memory/types.d.ts.map +1 -1
  37. package/dist/tools/working-memory.d.ts.map +1 -1
  38. package/package.json +8 -8
  39. package/dist/chunk-BCWFD5J7.js.map +0 -1
  40. package/dist/chunk-SMKIROLC.cjs.map +0 -1
@@ -2411,6 +2411,8 @@ var ObservationTurn = class {
2411
2411
  requestContext;
2412
2412
  /** Optional observability context for nested OM spans. */
2413
2413
  observabilityContext;
2414
+ /** Optional signal sender for processor-originated notifications. */
2415
+ sendSignal;
2414
2416
  /** Current actor model for this step. Updated by the processor before prepare(). */
2415
2417
  actorModelContext;
2416
2418
  /** Processor-provided hooks for turn/step lifecycle integration. */
@@ -2420,6 +2422,8 @@ var ObservationTurn = class {
2420
2422
  this.threadId = opts.threadId;
2421
2423
  this.resourceId = opts.resourceId;
2422
2424
  this.messageList = opts.messageList;
2425
+ this.sendSignal = opts.sendSignal;
2426
+ this.requestContext = opts.requestContext;
2423
2427
  this.observabilityContext = opts.observabilityContext;
2424
2428
  this.hooks = opts.hooks ?? {};
2425
2429
  }
@@ -2509,7 +2513,9 @@ var ObservationTurn = class {
2509
2513
  if (unsavedMessages.length > 0) {
2510
2514
  await this.om.persistMessages(unsavedMessages, this.threadId, this.resourceId);
2511
2515
  }
2512
- if (this.om.buffering.isAsyncObservationEnabled()) {
2516
+ const asyncObservationEnabled = this.om.buffering.isAsyncObservationEnabled();
2517
+ const bufferOnIdle = this.om.getObservationConfig().bufferOnIdle;
2518
+ if (asyncObservationEnabled && bufferOnIdle) {
2513
2519
  const allMessages = this.messageList.get.all.db();
2514
2520
  const record = this._record;
2515
2521
  const unobservedMessages = this.om.getUnobservedMessages(allMessages, record);
@@ -2520,8 +2526,11 @@ var ObservationTurn = class {
2520
2526
  messages: unobservedMessages,
2521
2527
  record,
2522
2528
  writer: this.writer,
2529
+ sendSignal: this.sendSignal,
2523
2530
  requestContext: this.requestContext,
2524
- observabilityContext: this.observabilityContext
2531
+ currentModel: this.actorModelContext,
2532
+ observabilityContext: this.observabilityContext,
2533
+ skipMinimumTokenCheck: true
2525
2534
  }).catch((err) => {
2526
2535
  omDebug(`[OM:turn.end] idle buffer failed: ${err?.message}`);
2527
2536
  });
@@ -5753,7 +5762,7 @@ function ensureMessageMastraMetadata(message) {
5753
5762
  return typedMessage.metadata.mastra;
5754
5763
  }
5755
5764
  function buildEstimateKey(kind, text) {
5756
- const payloadHash = crypto$1.createHash("sha1").update(text).digest("hex");
5765
+ const payloadHash = crypto$1.createHash("sha256").update(text).digest("hex");
5757
5766
  return `${kind}:${payloadHash}`;
5758
5767
  }
5759
5768
  function resolveEstimatorId() {
@@ -6296,7 +6305,7 @@ function getAttachmentFingerprint(asset) {
6296
6305
  }
6297
6306
  const base64 = encodeAttachmentBase64(asset);
6298
6307
  if (base64) {
6299
- return { contentHash: crypto$1.createHash("sha1").update(base64).digest("hex") };
6308
+ return { contentHash: crypto$1.createHash("sha256").update(base64).digest("hex") };
6300
6309
  }
6301
6310
  return {};
6302
6311
  }
@@ -6575,6 +6584,116 @@ var TokenCounter = class _TokenCounter {
6575
6584
  resolveToolResultForTokenCounting(part, invocationResult) {
6576
6585
  return resolveToolResultValue(part, invocationResult);
6577
6586
  }
6587
+ countMultimodalToolResultContent(part, toolResult) {
6588
+ if (!toolResult || typeof toolResult !== "object") {
6589
+ return void 0;
6590
+ }
6591
+ const output = toolResult;
6592
+ const content = output.type === "content" && Array.isArray(output.value) ? output.value : output.content;
6593
+ if (!Array.isArray(content)) {
6594
+ return void 0;
6595
+ }
6596
+ let hasAttachment = false;
6597
+ let tokens = 0;
6598
+ const cacheParts = [];
6599
+ const countJsonContentPart = (contentPart) => {
6600
+ const formatted = formatToolResultForObserver(contentPart);
6601
+ tokens += this.countString(formatted);
6602
+ cacheParts.push({ type: "json", valueHash: crypto$1.createHash("sha256").update(formatted).digest("hex") });
6603
+ };
6604
+ for (const item of content) {
6605
+ if (!item || typeof item !== "object") {
6606
+ continue;
6607
+ }
6608
+ const contentPart = item;
6609
+ const partType = contentPart.type;
6610
+ if (partType === "text") {
6611
+ const text = typeof contentPart.text === "string" ? contentPart.text : String(contentPart.value ?? "");
6612
+ tokens += this.countString(text);
6613
+ cacheParts.push({ type: "text", textHash: crypto$1.createHash("sha256").update(text).digest("hex") });
6614
+ continue;
6615
+ }
6616
+ if (partType === "image" || partType === "image-data" || partType === "media" && String(contentPart.mediaType ?? "").startsWith("image/")) {
6617
+ if (typeof contentPart.data !== "string") {
6618
+ countJsonContentPart(contentPart);
6619
+ continue;
6620
+ }
6621
+ hasAttachment = true;
6622
+ const imagePart = {
6623
+ type: "image",
6624
+ image: contentPart.data,
6625
+ mimeType: contentPart.mediaType ?? contentPart.mimeType,
6626
+ providerOptions: contentPart.providerOptions,
6627
+ providerMetadata: contentPart.providerMetadata
6628
+ };
6629
+ const clientEstimate = getClientPartTokenEstimate(imagePart);
6630
+ if (clientEstimate) {
6631
+ tokens += clientEstimate.tokens;
6632
+ cacheParts.push({
6633
+ type: "image-data-client-estimate",
6634
+ key: clientEstimate.key,
6635
+ tokens: clientEstimate.tokens
6636
+ });
6637
+ continue;
6638
+ }
6639
+ const estimate = this.estimateImageTokens(imagePart);
6640
+ tokens += estimate.tokens;
6641
+ cacheParts.push({ type: "image-data", estimate: JSON.parse(estimate.cachePayload) });
6642
+ continue;
6643
+ }
6644
+ if (partType === "audio" || partType === "file-data" || partType === "media") {
6645
+ if (typeof contentPart.data !== "string") {
6646
+ countJsonContentPart(contentPart);
6647
+ continue;
6648
+ }
6649
+ hasAttachment = true;
6650
+ const filePart = {
6651
+ type: "file",
6652
+ data: contentPart.data,
6653
+ mimeType: contentPart.mediaType ?? contentPart.mimeType,
6654
+ filename: contentPart.filename,
6655
+ providerOptions: contentPart.providerOptions,
6656
+ providerMetadata: contentPart.providerMetadata
6657
+ };
6658
+ const clientEstimate = getClientPartTokenEstimate(filePart);
6659
+ if (clientEstimate) {
6660
+ tokens += clientEstimate.tokens;
6661
+ cacheParts.push({
6662
+ type: "file-data-client-estimate",
6663
+ key: clientEstimate.key,
6664
+ tokens: clientEstimate.tokens
6665
+ });
6666
+ continue;
6667
+ }
6668
+ if (isImageLikeFilePart(filePart)) {
6669
+ const estimate = this.estimateImageLikeFileTokens(filePart);
6670
+ tokens += estimate.tokens;
6671
+ cacheParts.push({ type: "image-like-file-data", estimate: JSON.parse(estimate.cachePayload) });
6672
+ continue;
6673
+ }
6674
+ const byteEstimate = estimateNonImageFileTokens(this.getModelContext(), filePart);
6675
+ if (byteEstimate) {
6676
+ tokens += byteEstimate.tokens;
6677
+ cacheParts.push({ type: "file-data", estimate: JSON.parse(byteEstimate.cachePayload) });
6678
+ continue;
6679
+ }
6680
+ const descriptor = serializeNonImageFilePartForTokenCounting(filePart);
6681
+ tokens += this.countString(descriptor);
6682
+ cacheParts.push({ type: "file-data-descriptor", descriptor });
6683
+ continue;
6684
+ }
6685
+ countJsonContentPart(contentPart);
6686
+ }
6687
+ if (!hasAttachment) {
6688
+ return void 0;
6689
+ }
6690
+ return this.readOrPersistFixedPartEstimate(
6691
+ part,
6692
+ "tool-result-multimodal-content",
6693
+ JSON.stringify({ type: "content", value: cacheParts }),
6694
+ tokens
6695
+ );
6696
+ }
6578
6697
  estimateImageAssetTokens(part, asset, kind) {
6579
6698
  const modelContext = this.getModelContext();
6580
6699
  const provider = resolveProviderId(modelContext);
@@ -6833,12 +6952,17 @@ var TokenCounter = class _TokenCounter {
6833
6952
  invocation.result
6834
6953
  );
6835
6954
  if (resultForCounting !== void 0) {
6836
- const formattedResult = formatToolResultForObserver(resultForCounting);
6837
- tokens += this.readOrPersistPartEstimate(
6838
- part,
6839
- usingStoredModelOutput ? "tool-result-model-output-json" : "tool-result-json",
6840
- formattedResult
6841
- );
6955
+ const contentTokens = this.countMultimodalToolResultContent(part, resultForCounting);
6956
+ if (contentTokens !== void 0) {
6957
+ tokens += contentTokens;
6958
+ } else {
6959
+ const formattedResult = formatToolResultForObserver(resultForCounting);
6960
+ tokens += this.readOrPersistPartEstimate(
6961
+ part,
6962
+ usingStoredModelOutput ? "tool-result-model-output-json" : "tool-result-json",
6963
+ formattedResult
6964
+ );
6965
+ }
6842
6966
  if (typeof resultForCounting !== "string") {
6843
6967
  overheadDelta -= 12;
6844
6968
  }
@@ -7193,6 +7317,7 @@ Async buffering is enabled by default \u2014 this opt-out is only needed when us
7193
7317
  config.observation?.bufferTokens ?? chunk3Q3LOVQP_cjs.OBSERVATIONAL_MEMORY_DEFAULTS.observation.bufferTokens,
7194
7318
  config.observation?.messageTokens ?? chunk3Q3LOVQP_cjs.OBSERVATIONAL_MEMORY_DEFAULTS.observation.messageTokens
7195
7319
  ),
7320
+ bufferOnIdle: config.observation?.bufferOnIdle ?? false,
7196
7321
  bufferActivation: asyncBufferingDisabled ? void 0 : config.observation?.bufferActivation ?? chunk3Q3LOVQP_cjs.OBSERVATIONAL_MEMORY_DEFAULTS.observation.bufferActivation,
7197
7322
  activateAfterIdle: parseActivationTTL(observationActivateAfterIdle, observationActivateAfterIdlePath),
7198
7323
  activateOnProviderChange: config.observation?.activateOnProviderChange ?? config.activateOnProviderChange ?? false,
@@ -9004,7 +9129,7 @@ ${grouped}` : grouped;
9004
9129
  const bufferTokens = this.observationConfig.bufferTokens ?? 5e3;
9005
9130
  const minNewTokens = bufferTokens / 2;
9006
9131
  const newTokens = await this.tokenCounter.countMessagesAsync(candidateMessages);
9007
- if (newTokens < minNewTokens || candidateMessages.length === 0) {
9132
+ if (candidateMessages.length === 0 || !opts.skipMinimumTokenCheck && newTokens < minNewTokens) {
9008
9133
  return { buffered: false, record };
9009
9134
  }
9010
9135
  if (opts.beforeBuffer) {
@@ -9037,7 +9162,9 @@ ${grouped}` : grouped;
9037
9162
  cycleId,
9038
9163
  startedAt,
9039
9164
  writer,
9165
+ sendSignal: opts.sendSignal,
9040
9166
  requestContext,
9167
+ currentModel: opts.currentModel,
9041
9168
  observabilityContext
9042
9169
  }).run();
9043
9170
  if (isOmReproCaptureEnabled()) {
@@ -9571,7 +9698,8 @@ async function insertTemporalGapMarkers({
9571
9698
  }
9572
9699
  await sendSignal?.({
9573
9700
  id: `__temporal_gap_${crypto.randomUUID()}`,
9574
- type: "system-reminder",
9701
+ type: "reactive",
9702
+ tagName: "system-reminder",
9575
9703
  contents: getTemporalGapReminderText(gapText, timestamp),
9576
9704
  createdAt: new Date(timestamp - 1),
9577
9705
  acceptedAt: new Date(timestamp),
@@ -9723,6 +9851,7 @@ var ObservationalMemoryProcessor = class {
9723
9851
  }
9724
9852
  });
9725
9853
  this.turn.writer = writer;
9854
+ this.turn.sendSignal = args.sendSignal;
9726
9855
  this.turn.requestContext = requestContext;
9727
9856
  await this.turn.start(this.memory);
9728
9857
  if (stepNumber === 0 && this.temporalMarkers) {
@@ -9883,5 +10012,5 @@ exports.stripEphemeralAnchorIds = stripEphemeralAnchorIds;
9883
10012
  exports.stripObservationGroups = stripObservationGroups;
9884
10013
  exports.truncateStringByTokens = truncateStringByTokens;
9885
10014
  exports.wrapInObservationGroup = wrapInObservationGroup;
9886
- //# sourceMappingURL=chunk-SMKIROLC.cjs.map
9887
- //# sourceMappingURL=chunk-SMKIROLC.cjs.map
10015
+ //# sourceMappingURL=chunk-P4BYW7GY.cjs.map
10016
+ //# sourceMappingURL=chunk-P4BYW7GY.cjs.map