@mastra/memory 1.36.0-alpha.1 → 1.36.0-alpha.2

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.
@@ -3733,7 +3733,7 @@ function trimStartOfStream() {
3733
3733
  };
3734
3734
  }
3735
3735
  //#endregion
3736
- //#region ../_vendored/ai_v5/dist/dist-DqVGDtnX.js
3736
+ //#region ../_vendored/ai_v5/dist/dist-CwCWl16R.js
3737
3737
  var marker$2$4 = "vercel.ai.error";
3738
3738
  var symbol$2$4 = Symbol.for(marker$2$4);
3739
3739
  var _a$2$4;
@@ -9020,7 +9020,7 @@ var object$1 = ({ schema: inputSchema }) => {
9020
9020
  };
9021
9021
  };
9022
9022
  //#endregion
9023
- //#region ../_vendored/ai_v6/dist/dist-BcG-LFmj.js
9023
+ //#region ../_vendored/ai_v6/dist/dist-B5Rt0qDx.js
9024
9024
  var marker$2 = "vercel.ai.error";
9025
9025
  var symbol$2 = Symbol.for(marker$2);
9026
9026
  var _a$2;
@@ -11565,7 +11565,7 @@ var _b14;
11565
11565
  }
11566
11566
  });
11567
11567
  //#endregion
11568
- //#region ../_vendored/ai_v6/dist/dist-t-eoX9xC.js
11568
+ //#region ../_vendored/ai_v6/dist/dist-BgUIUei9.js
11569
11569
  function getContext() {
11570
11570
  return { headers: {} };
11571
11571
  }
@@ -22201,6 +22201,7 @@ function serializeNonImageFilePartForTokenCounting(part) {
22201
22201
  function isValidCacheEntry(entry, expectedKey, expectedSource) {
22202
22202
  return Boolean(entry && entry.v === TOKEN_ESTIMATE_CACHE_VERSION && entry.source === expectedSource && entry.key === expectedKey && Number.isFinite(entry.tokens));
22203
22203
  }
22204
+ const modelContextStorage = new async_hooks.AsyncLocalStorage();
22204
22205
  function parseModelContext(model) {
22205
22206
  if (!model) return void 0;
22206
22207
  if (typeof model === "object") return model.provider || model.modelId ? {
@@ -22822,7 +22823,6 @@ async function fetchGoogleAttachmentTokenEstimate(modelId, part) {
22822
22823
  var TokenCounter = class TokenCounter {
22823
22824
  cacheSource;
22824
22825
  defaultModelContext;
22825
- modelContextStorage = new async_hooks.AsyncLocalStorage();
22826
22826
  inFlightAttachmentCounts = /* @__PURE__ */ new Map();
22827
22827
  multimodalToolResultCounts = /* @__PURE__ */ new WeakMap();
22828
22828
  static TOKENS_PER_MESSAGE = 3.8;
@@ -22832,10 +22832,12 @@ var TokenCounter = class TokenCounter {
22832
22832
  this.defaultModelContext = parseModelContext(options?.model);
22833
22833
  }
22834
22834
  runWithModelContext(model, fn) {
22835
- return this.modelContextStorage.run(parseModelContext(model), fn);
22835
+ const contexts = new Map(modelContextStorage.getStore());
22836
+ contexts.set(this, parseModelContext(model));
22837
+ return modelContextStorage.run(contexts, fn);
22836
22838
  }
22837
22839
  getModelContext() {
22838
- return this.modelContextStorage.getStore() ?? this.defaultModelContext;
22840
+ return modelContextStorage.getStore()?.get(this) ?? this.defaultModelContext;
22839
22841
  }
22840
22842
  /**
22841
22843
  * Count tokens in a plain string
@@ -28039,7 +28041,7 @@ var ObservationStep = class {
28039
28041
  if (obsResult.succeeded) {
28040
28042
  observed = true;
28041
28043
  didThresholdCleanup = true;
28042
- const observedIds = obsResult.activatedMessageIds ?? obsResult.record.observedMessageIds ?? [];
28044
+ const observedIds = obsResult.cleanupMessageIds;
28043
28045
  const minRemaining = resolveRetentionFloor(om.getObservationConfig().bufferActivation ?? 1, statusSnapshot.threshold);
28044
28046
  await om.cleanupMessages({
28045
28047
  threadId,
@@ -28106,8 +28108,8 @@ var ObservationStep = class {
28106
28108
  }
28107
28109
  /**
28108
28110
  * Run the full threshold observation pipeline:
28109
- * waitForBuffering → re-check → activate → reflect → observe (sync fallback when
28110
- * buffered activation did not happen)
28111
+ * waitForBuffering → re-check → activate buffered chunks → reflect → observe
28112
+ * (sync fallback when pending tokens are still at or above the threshold)
28111
28113
  */
28112
28114
  async runThresholdObservation() {
28113
28115
  const { threadId, resourceId, messageList } = this.turn;
@@ -28115,59 +28117,76 @@ var ObservationStep = class {
28115
28117
  await om.waitForBuffering(threadId, resourceId);
28116
28118
  await this.turn.refreshRecord();
28117
28119
  const observableMessages = this.seededResponseMessage ? getObservableMessages(messageList).filter((msg) => msg.id !== this.turn.responseMessageId) : getObservableMessages(messageList);
28118
- const freshStatus = await om.getStatus({
28120
+ let status = await om.getStatus({
28119
28121
  threadId,
28120
28122
  resourceId,
28121
28123
  record: this.turn.record,
28122
28124
  messages: observableMessages
28123
28125
  });
28124
- if (!freshStatus.shouldObserve) return {
28126
+ if (!status.shouldObserve) return {
28125
28127
  succeeded: false,
28126
- record: freshStatus.record
28128
+ record: status.record,
28129
+ cleanupMessageIds: []
28127
28130
  };
28128
- if (freshStatus.canActivate) {
28131
+ let pendingMessages = observableMessages;
28132
+ const activatedMessageIds = [];
28133
+ let activated = false;
28134
+ while (status.shouldObserve && status.canActivate) {
28129
28135
  const activation = await om.activate({
28130
28136
  threadId,
28131
28137
  resourceId,
28132
28138
  record: this.turn.record,
28133
- messages: observableMessages,
28139
+ messages: pendingMessages,
28140
+ pendingTokens: status.pendingTokens,
28134
28141
  currentModel: this.turn.actorModelContext,
28135
28142
  writer: this.turn.writer,
28136
28143
  messageList
28137
28144
  });
28138
28145
  this.turn.setRecord(activation.record);
28139
- if (activation.activated) {
28140
- const postActivationRecord = activation.record;
28141
- await om.reflector.maybeReflect({
28142
- record: postActivationRecord,
28143
- observationTokens: postActivationRecord.observationTokenCount ?? 0,
28146
+ if (!activation.activated) break;
28147
+ activated = true;
28148
+ const ids = new Set(activation.activatedMessageIds ?? []);
28149
+ activatedMessageIds.push(...ids);
28150
+ pendingMessages = pendingMessages.filter((msg) => !ids.has(msg.id));
28151
+ status = await om.getStatus({
28152
+ threadId,
28153
+ resourceId,
28154
+ record: this.turn.record,
28155
+ messages: pendingMessages
28156
+ });
28157
+ }
28158
+ if (activated) {
28159
+ const postActivationRecord = this.turn.record;
28160
+ await om.reflector.maybeReflect({
28161
+ record: postActivationRecord,
28162
+ observationTokens: postActivationRecord.observationTokenCount ?? 0,
28163
+ threadId,
28164
+ writer: this.turn.writer,
28165
+ messageList,
28166
+ currentModel: this.turn.actorModelContext,
28167
+ requestContext: this.turn.requestContext,
28168
+ observabilityContext: this.turn.observabilityContext,
28169
+ lastActivityAt: getLastActivityFromMessages(getObservableMessages(messageList)),
28170
+ reflectionHooks: om.composeHooks(void 0, {
28144
28171
  threadId,
28145
- writer: this.turn.writer,
28146
- messageList,
28147
- currentModel: this.turn.actorModelContext,
28148
- requestContext: this.turn.requestContext,
28149
- observabilityContext: this.turn.observabilityContext,
28150
- lastActivityAt: getLastActivityFromMessages(getObservableMessages(messageList)),
28151
- reflectionHooks: om.composeHooks(void 0, {
28152
- threadId,
28153
- resourceId,
28154
- trigger: "turn-sync"
28155
- }),
28172
+ resourceId,
28156
28173
  trigger: "turn-sync"
28157
- });
28158
- return {
28159
- succeeded: true,
28160
- record: activation.record,
28161
- activatedMessageIds: activation.activatedMessageIds
28162
- };
28163
- }
28174
+ }),
28175
+ trigger: "turn-sync"
28176
+ });
28177
+ if (!status.shouldObserve) return {
28178
+ succeeded: true,
28179
+ record: postActivationRecord,
28180
+ activatedMessageIds,
28181
+ cleanupMessageIds: activatedMessageIds
28182
+ };
28164
28183
  }
28165
28184
  let obsResult;
28166
28185
  try {
28167
28186
  obsResult = await om.observe({
28168
28187
  threadId,
28169
28188
  resourceId,
28170
- messages: observableMessages,
28189
+ messages: pendingMessages,
28171
28190
  messageList,
28172
28191
  trigger: "turn-sync",
28173
28192
  agent: this.turn.agent,
@@ -28180,8 +28199,10 @@ var ObservationStep = class {
28180
28199
  } catch (error) {
28181
28200
  if (om.config.observation.failurePolicy !== "continue" || !isOmModelExecutionError(error) || error.failureKind !== "observer-model") throw error;
28182
28201
  return {
28183
- succeeded: false,
28184
- record: freshStatus.record
28202
+ succeeded: activated,
28203
+ record: this.turn.record,
28204
+ activatedMessageIds: activated ? activatedMessageIds : void 0,
28205
+ cleanupMessageIds: activatedMessageIds
28185
28206
  };
28186
28207
  }
28187
28208
  if (obsResult.observed) {
@@ -28208,8 +28229,10 @@ var ObservationStep = class {
28208
28229
  if (messagesToSeal.length > 0) await om.persistMessages(messagesToSeal, threadId, resourceId);
28209
28230
  }
28210
28231
  return {
28211
- succeeded: obsResult.observed,
28232
+ succeeded: activated || obsResult.observed,
28212
28233
  record: obsResult.record,
28234
+ activatedMessageIds: activated ? activatedMessageIds : void 0,
28235
+ cleanupMessageIds: [.../* @__PURE__ */ new Set([...activatedMessageIds, ...obsResult.observed ? obsResult.record.observedMessageIds ?? [] : []])],
28213
28236
  observerExchange: om.observer.lastExchange
28214
28237
  };
28215
28238
  }
@@ -31897,6 +31920,7 @@ ${formattedMessages}
31897
31920
  let activateAfterIdleExpiredMs;
31898
31921
  let previousModel;
31899
31922
  let currentModel;
31923
+ let livePendingTokens = opts.pendingTokens;
31900
31924
  if (opts.checkThreshold) {
31901
31925
  const thresholdMessages = opts.messages ?? await this.loadMessagesFromStorage(threadId, resourceId, record.lastObservedAt ? new Date(record.lastObservedAt) : void 0);
31902
31926
  const activateAfterIdle = resolveActivationTTL(this.observationConfig.activateAfterIdle, opts.currentModel);
@@ -31925,6 +31949,7 @@ ${formattedMessages}
31925
31949
  activated: false,
31926
31950
  record
31927
31951
  };
31952
+ livePendingTokens ??= status.pendingTokens;
31928
31953
  }
31929
31954
  }
31930
31955
  if (record.isBufferingObservation) {
@@ -31948,8 +31973,9 @@ ${formattedMessages}
31948
31973
  const messageTokensThreshold = getMaxThreshold(this.getEffectiveMessageTokens(freshRecord));
31949
31974
  const activationRatio = resolveActivationRatio(this.observationConfig.bufferActivation ?? .7, messageTokensThreshold);
31950
31975
  const totalChunkMessageTokens = freshChunks.reduce((sum, c) => sum + (c.messageTokens ?? 0), 0);
31951
- const currentPendingTokens = freshRecord.pendingMessageTokens || totalChunkMessageTokens;
31976
+ const currentPendingTokens = livePendingTokens ?? (freshRecord.pendingMessageTokens || totalChunkMessageTokens);
31952
31977
  const forceMaxActivation = !!(this.observationConfig.blockAfter && currentPendingTokens >= this.observationConfig.blockAfter);
31978
+ if (freshRecord.pendingMessageTokens !== currentPendingTokens) await this.storage.setPendingMessageTokens(freshRecord.id, currentPendingTokens);
31953
31979
  const activationResult = await this.storage.swapBufferedToActive({
31954
31980
  id: freshRecord.id,
31955
31981
  activationRatio,
@@ -35564,4 +35590,4 @@ Object.defineProperty(exports, "wrapInObservationGroup", {
35564
35590
  }
35565
35591
  });
35566
35592
 
35567
- //# sourceMappingURL=src-Ch_8ufgu.cjs.map
35593
+ //# sourceMappingURL=src-OOB19zXe.cjs.map