@mastra/memory 1.14.0-alpha.1 → 1.14.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.
@@ -6265,6 +6265,47 @@ Async buffering is enabled by default \u2014 this opt-out is only needed when us
6265
6265
  }
6266
6266
  }
6267
6267
  }
6268
+ /**
6269
+ * Resolve the effective messageTokens for a record.
6270
+ * Only explicit per-record overrides (stored under `_overrides`) win;
6271
+ * the initial config snapshot written by getOrCreateRecord() is ignored
6272
+ * so that later instance-level changes still take effect.
6273
+ *
6274
+ * Overrides that fall below the instance-level buffering floor
6275
+ * (bufferTokens / absolute bufferActivation) are clamped to the
6276
+ * instance threshold to preserve buffering invariants.
6277
+ */
6278
+ getEffectiveMessageTokens(record) {
6279
+ const overrides = record.config?._overrides;
6280
+ const recordTokens = overrides?.observation?.messageTokens;
6281
+ if (recordTokens) {
6282
+ const maxOverride = getMaxThreshold(recordTokens);
6283
+ const bufferTokens = this.observationConfig.bufferTokens;
6284
+ if (bufferTokens && maxOverride <= bufferTokens) {
6285
+ return this.observationConfig.messageTokens;
6286
+ }
6287
+ const bufferActivation = this.observationConfig.bufferActivation;
6288
+ if (bufferActivation && bufferActivation >= 1e3 && maxOverride <= bufferActivation) {
6289
+ return this.observationConfig.messageTokens;
6290
+ }
6291
+ return recordTokens;
6292
+ }
6293
+ return this.observationConfig.messageTokens;
6294
+ }
6295
+ /**
6296
+ * Resolve the effective reflection observationTokens for a record.
6297
+ * Only explicit per-record overrides (stored under `_overrides`) win;
6298
+ * the initial config snapshot is ignored so instance-level changes
6299
+ * still take effect for existing records.
6300
+ */
6301
+ getEffectiveReflectionTokens(record) {
6302
+ const overrides = record.config?._overrides;
6303
+ const recordTokens = overrides?.reflection?.observationTokens;
6304
+ if (recordTokens) {
6305
+ return recordTokens;
6306
+ }
6307
+ return this.reflectionConfig.observationTokens;
6308
+ }
6268
6309
  /**
6269
6310
  * Check whether the unobserved message tokens meet the observation threshold.
6270
6311
  */
@@ -6272,7 +6313,7 @@ Async buffering is enabled by default \u2014 this opt-out is only needed when us
6272
6313
  const { record, unobservedTokens, extraTokens = 0 } = opts;
6273
6314
  const pendingTokens = (record.pendingMessageTokens ?? 0) + unobservedTokens + extraTokens;
6274
6315
  const currentObservationTokens = record.observationTokenCount ?? 0;
6275
- const threshold = calculateDynamicThreshold(this.observationConfig.messageTokens, currentObservationTokens);
6316
+ const threshold = calculateDynamicThreshold(this.getEffectiveMessageTokens(record), currentObservationTokens);
6276
6317
  return pendingTokens >= threshold;
6277
6318
  }
6278
6319
  /**
@@ -7421,7 +7462,7 @@ ${grouped}` : grouped;
7421
7462
  const projectedMessageRemoval = calculateProjectedMessageRemoval(
7422
7463
  bufferedChunks,
7423
7464
  this.observationConfig.bufferActivation ?? 1,
7424
- getMaxThreshold(this.observationConfig.messageTokens),
7465
+ getMaxThreshold(this.getEffectiveMessageTokens(record)),
7425
7466
  pendingTokens
7426
7467
  );
7427
7468
  let obsBufferStatus = "idle";
@@ -7510,7 +7551,7 @@ ${grouped}` : grouped;
7510
7551
  otherThreadTokens = otherContext ? this.tokenCounter.countString(otherContext) : 0;
7511
7552
  }
7512
7553
  const pendingTokens = Math.max(0, contextWindowTokens + otherThreadTokens);
7513
- const threshold = calculateDynamicThreshold(this.observationConfig.messageTokens, currentObservationTokens);
7554
+ const threshold = calculateDynamicThreshold(this.getEffectiveMessageTokens(record), currentObservationTokens);
7514
7555
  const bufferedChunks = getBufferedChunks(record);
7515
7556
  const bufferedChunkCount = bufferedChunks.length;
7516
7557
  const bufferedChunkTokens = bufferedChunks.reduce((sum, chunk) => sum + (chunk.messageTokens ?? 0), 0);
@@ -7527,11 +7568,12 @@ ${grouped}` : grouped;
7527
7568
  );
7528
7569
  }
7529
7570
  const shouldObserve = pendingTokens >= threshold;
7530
- const reflectThreshold = getMaxThreshold(this.reflectionConfig.observationTokens);
7571
+ const reflectThreshold = getMaxThreshold(this.getEffectiveReflectionTokens(record));
7531
7572
  const shouldReflect = currentObservationTokens >= reflectThreshold;
7532
7573
  const canActivate = bufferedChunkCount > 0;
7533
- const isSharedBudget = typeof this.observationConfig.messageTokens !== "number";
7534
- const totalBudget = isSharedBudget ? this.observationConfig.messageTokens.max : 0;
7574
+ const effectiveMessageTokens = this.getEffectiveMessageTokens(record);
7575
+ const isSharedBudget = typeof effectiveMessageTokens !== "number";
7576
+ const totalBudget = isSharedBudget ? effectiveMessageTokens.max : 0;
7535
7577
  const effectiveObservationTokensThreshold = isSharedBudget ? Math.max(totalBudget - threshold, 1e3) : reflectThreshold;
7536
7578
  const unbufferedPendingTokens = Math.max(0, pendingTokens - bufferedChunkTokens);
7537
7579
  return {
@@ -7846,7 +7888,7 @@ ${grouped}` : grouped;
7846
7888
  if (!freshChunks.length) {
7847
7889
  return { activated: false, record };
7848
7890
  }
7849
- const messageTokensThreshold = getMaxThreshold(this.observationConfig.messageTokens);
7891
+ const messageTokensThreshold = getMaxThreshold(this.getEffectiveMessageTokens(freshRecord));
7850
7892
  const bufferActivation = this.observationConfig.bufferActivation ?? 0.7;
7851
7893
  const activationRatio = resolveActivationRatio(bufferActivation, messageTokensThreshold);
7852
7894
  const totalChunkMessageTokens = freshChunks.reduce((sum, c) => sum + (c.messageTokens ?? 0), 0);
@@ -7995,7 +8037,7 @@ ${grouped}` : grouped;
7995
8037
  await this.storage.setReflectingFlag(record.id, true);
7996
8038
  registerOp(record.id, "reflecting");
7997
8039
  try {
7998
- const reflectThreshold = getMaxThreshold(this.reflectionConfig.observationTokens);
8040
+ const reflectThreshold = getMaxThreshold(this.getEffectiveReflectionTokens(record));
7999
8041
  const reflectResult = await this.reflector.call(
8000
8042
  record.activeObservations,
8001
8043
  prompt,
@@ -8040,6 +8082,34 @@ ${grouped}` : grouped;
8040
8082
  const ids = this.getStorageIds(threadId, resourceId);
8041
8083
  return this.storage.getObservationalMemory(ids.threadId, ids.resourceId);
8042
8084
  }
8085
+ /**
8086
+ * Update per-record config overrides for observation and/or reflection thresholds.
8087
+ * The provided config is deep-merged into the record's `_overrides` key,
8088
+ * so you only need to specify the fields you want to change.
8089
+ *
8090
+ * Overrides that violate buffering invariants (e.g. messageTokens below
8091
+ * bufferTokens) are silently ignored at read time — the helpers fall back
8092
+ * to the instance-level config.
8093
+ *
8094
+ * @example
8095
+ * ```ts
8096
+ * await om.updateRecordConfig('thread-1', undefined, {
8097
+ * observation: { messageTokens: 2000 },
8098
+ * reflection: { observationTokens: 8000 },
8099
+ * });
8100
+ * ```
8101
+ */
8102
+ async updateRecordConfig(threadId, resourceId, config) {
8103
+ const ids = this.getStorageIds(threadId, resourceId);
8104
+ const record = await this.storage.getObservationalMemory(ids.threadId, ids.resourceId);
8105
+ if (!record) {
8106
+ throw new Error(`No observational memory record found for thread ${ids.threadId}`);
8107
+ }
8108
+ await this.storage.updateObservationalMemoryConfig({
8109
+ id: record.id,
8110
+ config: { _overrides: config }
8111
+ });
8112
+ }
8043
8113
  /**
8044
8114
  * Get observation history (previous generations)
8045
8115
  */
@@ -8616,5 +8686,5 @@ exports.stripEphemeralAnchorIds = stripEphemeralAnchorIds;
8616
8686
  exports.stripObservationGroups = stripObservationGroups;
8617
8687
  exports.truncateStringByTokens = truncateStringByTokens;
8618
8688
  exports.wrapInObservationGroup = wrapInObservationGroup;
8619
- //# sourceMappingURL=chunk-X7E3WPF2.cjs.map
8620
- //# sourceMappingURL=chunk-X7E3WPF2.cjs.map
8689
+ //# sourceMappingURL=chunk-ZVRO2GUN.cjs.map
8690
+ //# sourceMappingURL=chunk-ZVRO2GUN.cjs.map