@mastra/memory 1.28.3 → 1.29.0-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.
Files changed (39) hide show
  1. package/dist/docs/SKILL.md +1 -1
  2. package/dist/docs/assets/SOURCE_MAP.json +1 -1
  3. package/dist/docs/references/docs-memory-message-history.md +1 -1
  4. package/dist/docs/references/docs-memory-multi-user-threads.md +1 -1
  5. package/dist/docs/references/docs-memory-observational-memory.md +41 -1
  6. package/dist/docs/references/docs-memory-overview.md +4 -4
  7. package/dist/docs/references/reference-memory-observational-memory.md +1 -1
  8. package/dist/index.cjs +1 -1
  9. package/dist/index.d.ts.map +1 -1
  10. package/dist/index.js +1 -1
  11. package/dist/processors/index.cjs +1 -1
  12. package/dist/processors/index.js +1 -1
  13. package/dist/processors/observational-memory/index.d.ts +1 -1
  14. package/dist/processors/observational-memory/index.d.ts.map +1 -1
  15. package/dist/processors/observational-memory/observation-strategies/async-buffer.d.ts.map +1 -1
  16. package/dist/processors/observational-memory/observation-strategies/base.d.ts.map +1 -1
  17. package/dist/processors/observational-memory/observation-strategies/resource-scoped.d.ts.map +1 -1
  18. package/dist/processors/observational-memory/observation-strategies/sync.d.ts.map +1 -1
  19. package/dist/processors/observational-memory/observation-strategies/types.d.ts +3 -1
  20. package/dist/processors/observational-memory/observation-strategies/types.d.ts.map +1 -1
  21. package/dist/processors/observational-memory/observation-turn/step.d.ts.map +1 -1
  22. package/dist/processors/observational-memory/observational-memory.d.ts +3 -3
  23. package/dist/processors/observational-memory/observational-memory.d.ts.map +1 -1
  24. package/dist/processors/observational-memory/observer-runner.d.ts +60 -46
  25. package/dist/processors/observational-memory/observer-runner.d.ts.map +1 -1
  26. package/dist/processors/observational-memory/processor.d.ts.map +1 -1
  27. package/dist/processors/observational-memory/reflector-runner.d.ts +5 -1
  28. package/dist/processors/observational-memory/reflector-runner.d.ts.map +1 -1
  29. package/dist/processors/observational-memory/transform-hooks.d.ts +15 -0
  30. package/dist/processors/observational-memory/transform-hooks.d.ts.map +1 -0
  31. package/dist/processors/observational-memory/types.d.ts +71 -2
  32. package/dist/processors/observational-memory/types.d.ts.map +1 -1
  33. package/dist/{src-D4Wm6M4v.cjs → src-CG7RR0Ep.cjs} +135 -26
  34. package/dist/src-CG7RR0Ep.cjs.map +1 -0
  35. package/dist/{src-BFP4tRqs.js → src-DuA6bRSI.js} +135 -26
  36. package/dist/src-DuA6bRSI.js.map +1 -0
  37. package/package.json +4 -4
  38. package/dist/src-BFP4tRqs.js.map +0 -1
  39. package/dist/src-D4Wm6M4v.cjs.map +0 -1
@@ -19811,6 +19811,37 @@ async function withOmTracingSpan({ phase, model, inputTokens, requestContext, ob
19811
19811
  });
19812
19812
  }
19813
19813
  //#endregion
19814
+ //#region src/processors/observational-memory/transform-hooks.ts
19815
+ /**
19816
+ * Run the `beforeObservation` transform hook. Returns the (possibly replaced)
19817
+ * message array; `undefined` from the hook means passthrough.
19818
+ */
19819
+ async function applyBeforeObservation(hooks, messages, context) {
19820
+ if (!hooks?.beforeObservation) return messages;
19821
+ const result = await hooks.beforeObservation({
19822
+ ...context,
19823
+ messages
19824
+ });
19825
+ if (!result) return messages;
19826
+ omDebug(`[OM:hooks] beforeObservation replaced messages: ${messages.length} -> ${result.messages.length}`);
19827
+ return result.messages;
19828
+ }
19829
+ /**
19830
+ * Run one of the text transform hooks. Returns the (possibly replaced)
19831
+ * observation text; `undefined` from the hook means passthrough.
19832
+ */
19833
+ async function applyTextTransform(hooks, name, observations, context) {
19834
+ const hook = hooks?.[name];
19835
+ if (!hook) return observations;
19836
+ const result = await hook({
19837
+ ...context,
19838
+ observations
19839
+ });
19840
+ if (!result) return observations;
19841
+ omDebug(`[OM:hooks] ${name} replaced observations: ${observations.length} -> ${result.observations.length} chars`);
19842
+ return result.observations;
19843
+ }
19844
+ //#endregion
19814
19845
  //#region src/processors/observational-memory/observer-runner.ts
19815
19846
  function filterObserverExtractors(extractors, skipContinuationHints) {
19816
19847
  const configuredExtractors = extractors ?? [];
@@ -19823,6 +19854,7 @@ var ObserverRunner = class {
19823
19854
  resolveModel;
19824
19855
  tokenCounter;
19825
19856
  memory;
19857
+ hooks;
19826
19858
  mastra;
19827
19859
  /** Captured prompt/response from the last observer call (for repro capture). */
19828
19860
  lastExchange;
@@ -19833,6 +19865,7 @@ var ObserverRunner = class {
19833
19865
  this.tokenCounter = opts.tokenCounter;
19834
19866
  this.mastra = opts.mastra;
19835
19867
  this.memory = opts.memory;
19868
+ this.hooks = opts.hooks;
19836
19869
  }
19837
19870
  __registerMastra(mastra) {
19838
19871
  this.mastra = mastra;
@@ -19886,9 +19919,25 @@ var ObserverRunner = class {
19886
19919
  return result;
19887
19920
  }
19888
19921
  /**
19889
- * Call the Observer agent for a single thread.
19922
+ * Call the Observer agent for a single thread, running the config-level
19923
+ * `beforeObservation` / `afterObservation` transform hooks around it.
19890
19924
  */
19891
19925
  async call(existingObservations, messagesToObserve, abortSignal, options) {
19926
+ const context = {
19927
+ threadId: options?.threadId ?? messagesToObserve[0]?.threadId,
19928
+ resourceId: options?.resourceId,
19929
+ trigger: options?.trigger
19930
+ };
19931
+ const messages = await applyBeforeObservation(this.hooks, messagesToObserve, context);
19932
+ if (messages.length === 0) {
19933
+ omDebug("[OM:callObserver] beforeObservation returned no messages, skipping observer call");
19934
+ return { observations: "" };
19935
+ }
19936
+ const result = await this.callObserver(existingObservations, messages, abortSignal, options);
19937
+ result.observations = await applyTextTransform(this.hooks, "afterObservation", result.observations, context);
19938
+ return result;
19939
+ }
19940
+ async callObserver(existingObservations, messagesToObserve, abortSignal, options) {
19892
19941
  const inputTokens = this.tokenCounter.countMessages(messagesToObserve);
19893
19942
  const resolvedModel = options?.model ? { model: options.model } : this.resolveModel(inputTokens);
19894
19943
  const activeExtractors = await resolveExtractors(filterObserverExtractors(this.observationConfig.extractors, options?.skipContinuationHints), {
@@ -20007,7 +20056,30 @@ var ObserverRunner = class {
20007
20056
  /**
20008
20057
  * Call the Observer agent for multiple threads in a single batched request.
20009
20058
  */
20010
- async callMultiThread(existingObservations, messagesByThread, threadOrder, abortSignal, requestContext, priorMetadataByThread, observabilityContext, model) {
20059
+ async callMultiThread(existingObservations, allMessagesByThread, allThreadOrder, abortSignal, requestContext, priorMetadataByThread, observabilityContext, model, hookContext) {
20060
+ const contextFor = (threadId) => ({
20061
+ threadId,
20062
+ ...hookContext
20063
+ });
20064
+ const messagesByThread = /* @__PURE__ */ new Map();
20065
+ for (const threadId of allThreadOrder) {
20066
+ const filtered = await applyBeforeObservation(this.hooks, allMessagesByThread.get(threadId) ?? [], contextFor(threadId));
20067
+ if (filtered.length > 0) messagesByThread.set(threadId, filtered);
20068
+ }
20069
+ const threadOrder = allThreadOrder.filter((threadId) => messagesByThread.has(threadId));
20070
+ const output = await this.callMultiThreadObserver(existingObservations, messagesByThread, threadOrder, abortSignal, requestContext, priorMetadataByThread, observabilityContext, model);
20071
+ for (const threadId of allThreadOrder) {
20072
+ const threadResult = output.results.get(threadId);
20073
+ if (!threadResult) {
20074
+ output.results.set(threadId, { observations: "" });
20075
+ continue;
20076
+ }
20077
+ threadResult.observations = await applyTextTransform(this.hooks, "afterObservation", threadResult.observations, contextFor(threadId));
20078
+ }
20079
+ return output;
20080
+ }
20081
+ async callMultiThreadObserver(existingObservations, messagesByThread, threadOrder, abortSignal, requestContext, priorMetadataByThread, observabilityContext, model) {
20082
+ if (threadOrder.length === 0) return { results: /* @__PURE__ */ new Map() };
20011
20083
  const inputTokens = Array.from(messagesByThread.values()).reduce((total, messages) => total + this.tokenCounter.countMessages(messages), 0);
20012
20084
  const resolvedModel = model ? { model } : this.resolveModel(inputTokens);
20013
20085
  const firstThreadMessages = messagesByThread.get(threadOrder[0] ?? "") ?? [];
@@ -20026,7 +20098,7 @@ var ObserverRunner = class {
20026
20098
  totalTokens: 0
20027
20099
  };
20028
20100
  for (const threadId of threadOrder) {
20029
- const threadResult = await this.call(existingObservations, messagesByThread.get(threadId) ?? [], abortSignal, {
20101
+ const threadResult = await this.callObserver(existingObservations, messagesByThread.get(threadId) ?? [], abortSignal, {
20030
20102
  requestContext,
20031
20103
  observabilityContext,
20032
20104
  priorCurrentTask: priorMetadataByThread?.get(threadId)?.currentTask,
@@ -24083,6 +24155,7 @@ var ObservationStrategy = class ObservationStrategy {
24083
24155
  sendSignal: this.opts.sendSignal,
24084
24156
  sendStateSignal: this.opts.sendStateSignal,
24085
24157
  reflectionHooks,
24158
+ trigger: this.opts.trigger,
24086
24159
  requestContext,
24087
24160
  observabilityContext: this.opts.observabilityContext
24088
24161
  });
@@ -24363,7 +24436,9 @@ var SyncObservationStrategy = class extends ObservationStrategy {
24363
24436
  priorSuggestedResponse: omMeta?.suggestedResponse,
24364
24437
  priorThreadTitle: omMeta?.threadTitle,
24365
24438
  priorExtractedValues: this.priorExtractedValues,
24439
+ threadId: this.opts.threadId,
24366
24440
  resourceId: this.opts.resourceId,
24441
+ trigger: this.opts.trigger,
24367
24442
  mainAgent: this.opts.agent
24368
24443
  });
24369
24444
  const hookedValues = await applyExtractorHooks({
@@ -24548,7 +24623,9 @@ var AsyncBufferObservationStrategy = class extends ObservationStrategy {
24548
24623
  requestContext: this.opts.requestContext,
24549
24624
  observabilityContext: this.opts.observabilityContext,
24550
24625
  priorExtractedValues: this.priorExtractedValues,
24626
+ threadId: this.opts.threadId,
24551
24627
  resourceId: this.opts.resourceId,
24628
+ trigger: this.opts.trigger,
24552
24629
  mainAgent: this.opts.agent
24553
24630
  });
24554
24631
  const hookedValues = await applyExtractorHooks({
@@ -24872,7 +24949,10 @@ var ResourceScopedObservationStrategy = class extends ObservationStrategy {
24872
24949
  }
24873
24950
  if (currentBatch.threadIds.length > 0) batches.push(currentBatch);
24874
24951
  const batchResults = await Promise.all(batches.map(async (batch) => {
24875
- return this.deps.observer.callMultiThread(_existingObservations, batch.threadMap, batch.threadIds, this.opts.abortSignal, this.opts.requestContext, this.priorMetadataByThread, this.opts.observabilityContext);
24952
+ return this.deps.observer.callMultiThread(_existingObservations, batch.threadMap, batch.threadIds, this.opts.abortSignal, this.opts.requestContext, this.priorMetadataByThread, this.opts.observabilityContext, void 0, {
24953
+ resourceId: this.opts.resourceId,
24954
+ trigger: this.opts.trigger
24955
+ });
24876
24956
  }));
24877
24957
  for (const batchResult of batchResults) {
24878
24958
  for (const [threadId, result] of batchResult.results) this.multiThreadResults.set(threadId, result);
@@ -25210,7 +25290,8 @@ var ObservationStep = class {
25210
25290
  threadId,
25211
25291
  resourceId,
25212
25292
  trigger: "turn-sync"
25213
- })
25293
+ }),
25294
+ trigger: "turn-sync"
25214
25295
  });
25215
25296
  await this.turn.refreshRecord();
25216
25297
  if (this.turn.record.generationCount > preReflectGeneration) reflected = true;
@@ -25413,7 +25494,8 @@ var ObservationStep = class {
25413
25494
  threadId,
25414
25495
  resourceId,
25415
25496
  trigger: "turn-sync"
25416
- })
25497
+ }),
25498
+ trigger: "turn-sync"
25417
25499
  });
25418
25500
  return {
25419
25501
  succeeded: true,
@@ -26050,6 +26132,7 @@ var ReflectorRunner = class {
26050
26132
  getCompressionStartLevel;
26051
26133
  memory;
26052
26134
  onReflectionCommitted;
26135
+ hooks;
26053
26136
  mastra;
26054
26137
  /**
26055
26138
  * Per-lock-key observation-token count of the last unproductive synchronous
@@ -26081,6 +26164,7 @@ var ReflectorRunner = class {
26081
26164
  this.mastra = opts.mastra;
26082
26165
  this.memory = opts.memory;
26083
26166
  this.onReflectionCommitted = opts.onReflectionCommitted;
26167
+ this.hooks = opts.hooks;
26084
26168
  }
26085
26169
  __registerMastra(mastra) {
26086
26170
  this.mastra = mastra;
@@ -26303,7 +26387,7 @@ var ReflectorRunner = class {
26303
26387
  /**
26304
26388
  * Start an async buffered reflection in the background.
26305
26389
  */
26306
- startAsyncBufferedReflection(record, observationTokens, lockKey, writer, requestContext, observabilityContext, reflectionHooks, priorExtractedValues, mainAgent, sendSignal) {
26390
+ startAsyncBufferedReflection(record, observationTokens, lockKey, writer, requestContext, observabilityContext, reflectionHooks, priorExtractedValues, mainAgent, sendSignal, trigger) {
26307
26391
  const bufferKey = this.buffering.getReflectionBufferKey(lockKey);
26308
26392
  if (this.buffering.isAsyncBufferingInProgress(bufferKey)) return;
26309
26393
  BufferingCoordinator.lastBufferedBoundary.set(bufferKey, observationTokens);
@@ -26316,7 +26400,7 @@ var ReflectorRunner = class {
26316
26400
  let reflectionError;
26317
26401
  try {
26318
26402
  await reflectionHooks?.onReflectionStart?.();
26319
- outcome = await this.doAsyncBufferedReflection(record, bufferKey, writer, requestContext, observabilityContext, priorExtractedValues, mainAgent, sendSignal);
26403
+ outcome = await this.doAsyncBufferedReflection(record, bufferKey, writer, requestContext, observabilityContext, priorExtractedValues, mainAgent, sendSignal, trigger);
26320
26404
  } catch (error) {
26321
26405
  reflectionError = error instanceof Error ? error : new Error(String(error));
26322
26406
  if (writer) try {
@@ -26362,7 +26446,7 @@ var ReflectorRunner = class {
26362
26446
  * Perform async buffered reflection — reflects observations and stores to bufferedReflection.
26363
26447
  * Does NOT create a new generation or update activeObservations.
26364
26448
  */
26365
- async doAsyncBufferedReflection(record, _bufferKey, writer, requestContext, observabilityContext, priorExtractedValues, mainAgent, sendSignal) {
26449
+ async doAsyncBufferedReflection(record, _bufferKey, writer, requestContext, observabilityContext, priorExtractedValues, mainAgent, sendSignal, trigger) {
26366
26450
  const currentRecord = await this.storage.getObservationalMemory(record.threadId, record.resourceId) ?? record;
26367
26451
  const observationTokens = currentRecord.observationTokenCount ?? 0;
26368
26452
  const reflectThreshold = getMaxThreshold(this.getEffectiveReflectionTokens(currentRecord));
@@ -26375,7 +26459,12 @@ var ReflectorRunner = class {
26375
26459
  const avgTokensPerLine = totalLines > 0 ? observationTokens / totalLines : 0;
26376
26460
  const activationPointTokens = reflectThreshold * bufferActivation;
26377
26461
  const linesToReflect = avgTokensPerLine > 0 ? Math.min(Math.floor(activationPointTokens / avgTokensPerLine), totalLines) : totalLines;
26378
- const activeObservations = allLines.slice(0, linesToReflect).join("\n");
26462
+ const hookContext = {
26463
+ threadId: currentRecord.threadId ?? void 0,
26464
+ resourceId: currentRecord.resourceId ?? void 0,
26465
+ trigger
26466
+ };
26467
+ const activeObservations = await applyTextTransform(this.hooks, "beforeReflection", allLines.slice(0, linesToReflect).join("\n"), hookContext);
26379
26468
  const reflectedObservationLineCount = linesToReflect;
26380
26469
  const sliceTokenEstimate = Math.round(avgTokensPerLine * linesToReflect);
26381
26470
  const compressionTarget = Math.round(sliceTokenEstimate * .75);
@@ -26399,6 +26488,7 @@ var ReflectorRunner = class {
26399
26488
  }
26400
26489
  const compressionStartLevel = await this.getCompressionStartLevel(requestContext);
26401
26490
  const reflectResult = await this.call(activeObservations, void 0, void 0, compressionTarget, void 0, true, compressionStartLevel, requestContext, priorExtractedValues, observabilityContext, void 0, mainAgent, sendSignal);
26491
+ reflectResult.observations = await applyTextTransform(this.hooks, "afterReflection", reflectResult.observations, hookContext);
26402
26492
  await persistThreadExtractedValues(this.storage, this.reflectionConfig.extractors, currentRecord.threadId ?? void 0, reflectResult.extractedValues);
26403
26493
  const reflectionTokenCount = this.tokenCounter.countObservations(reflectResult.observations);
26404
26494
  omDebug(`[OM:reflect] doAsyncBufferedReflection: reflector returned ${reflectionTokenCount} tokens (${reflectResult.observations?.length} chars), saving to recordId=${currentRecord.id}`);
@@ -26545,7 +26635,7 @@ var ReflectorRunner = class {
26545
26635
  * @internal Used by observation strategies. Do not call directly.
26546
26636
  */
26547
26637
  async maybeReflect(opts) {
26548
- const { record, observationTokens, writer, abortSignal, mainAgent, sendSignal, sendStateSignal, messageList, currentModel, reflectionHooks, requestContext, observabilityContext, lastActivityAt, threadId: requestedThreadId } = opts;
26638
+ const { record, observationTokens, writer, abortSignal, mainAgent, sendSignal, sendStateSignal, messageList, currentModel, reflectionHooks, trigger, requestContext, observabilityContext, lastActivityAt, threadId: requestedThreadId } = opts;
26549
26639
  const lockKey = this.buffering.getLockKey(record.threadId, record.resourceId);
26550
26640
  const reflectThreshold = getMaxThreshold(this.getEffectiveReflectionTokens(record));
26551
26641
  const priorExtractedValues = await getThreadExtractedValues(this.storage, this.reflectionConfig.extractors, requestedThreadId ?? record.threadId);
@@ -26563,7 +26653,7 @@ var ReflectorRunner = class {
26563
26653
  if (record.bufferedReflection) return false;
26564
26654
  const activationPoint = reflectThreshold * this.reflectionConfig.bufferActivation;
26565
26655
  return observationTokens >= activationPoint;
26566
- })()) this.startAsyncBufferedReflection(record, observationTokens, lockKey, writer, requestContext, observabilityContext, reflectionHooks, priorExtractedValues, mainAgent, sendSignal);
26656
+ })()) this.startAsyncBufferedReflection(record, observationTokens, lockKey, writer, requestContext, observabilityContext, reflectionHooks, priorExtractedValues, mainAgent, sendSignal, trigger);
26567
26657
  }
26568
26658
  const activateAfterIdle = resolveActivationTTL(this.reflectionConfig.activateAfterIdle, currentModel);
26569
26659
  const ttlExpiredMs = activateAfterIdle !== void 0 && lastActivityAt !== void 0 ? Date.now() - lastActivityAt : void 0;
@@ -26612,7 +26702,7 @@ var ReflectorRunner = class {
26612
26702
  return;
26613
26703
  }
26614
26704
  omDebug(`[OM:reflect] async activation failed, no blockAfter or below it (obsTokens=${observationTokens}, blockAfter=${this.reflectionConfig.blockAfter}) — starting background reflection`);
26615
- this.startAsyncBufferedReflection(record, observationTokens, lockKey, writer, requestContext, observabilityContext, reflectionHooks, priorExtractedValues, mainAgent, sendSignal);
26705
+ this.startAsyncBufferedReflection(record, observationTokens, lockKey, writer, requestContext, observabilityContext, reflectionHooks, priorExtractedValues, mainAgent, sendSignal, trigger);
26616
26706
  return;
26617
26707
  }
26618
26708
  }
@@ -26673,7 +26763,13 @@ var ReflectorRunner = class {
26673
26763
  throw error;
26674
26764
  }
26675
26765
  const compressionStartLevel = await this.getCompressionStartLevel(requestContext);
26676
- const reflectResult = await this.call(record.activeObservations, void 0, streamContext, reflectThreshold, abortSignal, void 0, compressionStartLevel, requestContext, priorExtractedValues, observabilityContext, void 0, mainAgent, sendSignal);
26766
+ const hookContext = {
26767
+ threadId: requestedThreadId ?? record.threadId ?? void 0,
26768
+ resourceId: record.resourceId ?? void 0,
26769
+ trigger
26770
+ };
26771
+ const reflectResult = await this.call(await applyTextTransform(this.hooks, "beforeReflection", record.activeObservations, hookContext), void 0, streamContext, reflectThreshold, abortSignal, void 0, compressionStartLevel, requestContext, priorExtractedValues, observabilityContext, void 0, mainAgent, sendSignal);
26772
+ reflectResult.observations = await applyTextTransform(this.hooks, "afterReflection", reflectResult.observations, hookContext);
26677
26773
  reflectionUsage = reflectResult.usage;
26678
26774
  reflectionProviderMetadata = reflectResult.providerMetadata;
26679
26775
  await persistThreadExtractedValues(this.storage, this.reflectionConfig.extractors, record.threadId ?? void 0, reflectResult.extractedValues);
@@ -27424,7 +27520,8 @@ var ObservationalMemory = class ObservationalMemory {
27424
27520
  resolveModel: (inputTokens) => this.resolveObservationModel(inputTokens),
27425
27521
  tokenCounter: this.tokenCounter,
27426
27522
  mastra: config.mastra,
27427
- memory: this.memory
27523
+ memory: this.memory,
27524
+ hooks: this.hooks
27428
27525
  });
27429
27526
  this.buffering = new BufferingCoordinator({
27430
27527
  observationConfig: this.observationConfig,
@@ -27445,7 +27542,8 @@ var ObservationalMemory = class ObservationalMemory {
27445
27542
  resolveModel: (inputTokens) => this.resolveReflectionModel(inputTokens),
27446
27543
  mastra: config.mastra,
27447
27544
  memory: this.memory,
27448
- onReflectionCommitted: config.onReflectionCommitted
27545
+ onReflectionCommitted: config.onReflectionCommitted,
27546
+ hooks: this.hooks
27449
27547
  });
27450
27548
  this.validateBufferConfig();
27451
27549
  omDebug(`[OM:init] new ObservationalMemory instance created — scope=${this.scope}, messageTokens=${JSON.stringify(this.observationConfig.messageTokens)}, obsAsyncEnabled=${this.buffering.isAsyncObservationEnabled()}, bufferTokens=${this.observationConfig.bufferTokens}, bufferActivation=${this.observationConfig.bufferActivation}, blockAfter=${this.observationConfig.blockAfter}, reflectionTokens=${this.reflectionConfig.observationTokens}, refAsyncEnabled=${this.buffering.isAsyncReflectionEnabled()}, refAsyncActivation=${this.reflectionConfig.bufferActivation}, refBlockAfter=${this.reflectionConfig.blockAfter}`);
@@ -28301,7 +28399,8 @@ ${formattedMessages}
28301
28399
  startedAt,
28302
28400
  writer,
28303
28401
  requestContext,
28304
- observabilityContext
28402
+ observabilityContext,
28403
+ trigger: "async-buffer"
28305
28404
  }).run());
28306
28405
  const maxTs = this.getMaxMessageTimestamp(messagesToBuffer);
28307
28406
  const cursor = new Date(maxTs.getTime() + 1);
@@ -28922,7 +29021,8 @@ ${formattedMessages}
28922
29021
  sendStateSignal: opts.sendStateSignal,
28923
29022
  requestContext,
28924
29023
  currentModel: opts.currentModel,
28925
- observabilityContext
29024
+ observabilityContext,
29025
+ trigger: "async-buffer"
28926
29026
  }).run());
28927
29027
  if (isOmReproCaptureEnabled()) writeObserverExchangeReproCapture({
28928
29028
  threadId,
@@ -29236,10 +29336,11 @@ ${formattedMessages}
29236
29336
  async observe(opts) {
29237
29337
  const { threadId, resourceId, messages, requestContext } = opts;
29238
29338
  const lockKey = this.buffering.getLockKey(threadId, resourceId);
29339
+ const trigger = opts.trigger ?? "manual";
29239
29340
  const hooks = this.composeHooks(opts.hooks, {
29240
29341
  threadId,
29241
29342
  resourceId,
29242
- trigger: opts.trigger ?? "manual"
29343
+ trigger
29243
29344
  });
29244
29345
  const reflectionHooks = hooks ? {
29245
29346
  onReflectionStart: hooks.onReflectionStart,
@@ -29270,6 +29371,7 @@ ${formattedMessages}
29270
29371
  messages: unobservedMessages,
29271
29372
  messageList: opts.messageList,
29272
29373
  reflectionHooks,
29374
+ trigger,
29273
29375
  agent: opts.agent,
29274
29376
  sendSignal: opts.sendSignal,
29275
29377
  sendStateSignal: opts.sendStateSignal,
@@ -29336,11 +29438,12 @@ ${formattedMessages}
29336
29438
  }
29337
29439
  await this.storage.setReflectingFlag(record.id, true);
29338
29440
  registerOp(record.id, "reflecting");
29339
- const hooks = this.composeHooks(void 0, {
29441
+ const hookContext = {
29340
29442
  threadId,
29341
29443
  resourceId,
29342
29444
  trigger: "manual"
29343
- });
29445
+ };
29446
+ const hooks = this.composeHooks(void 0, hookContext);
29344
29447
  let reflectionUsage;
29345
29448
  let reflectionProviderMetadata;
29346
29449
  let reflectionError;
@@ -29357,7 +29460,8 @@ ${formattedMessages}
29357
29460
  const previousOmMetadata = (0, _mastra_core_memory.getThreadOMMetadata)(thread?.metadata);
29358
29461
  const priorExtractedValues = getPriorExtractedValues(previousOmMetadata, this.reflectionConfig.extractors);
29359
29462
  const reflectThreshold = getMaxThreshold(this.getEffectiveReflectionTokens(record));
29360
- const reflectResult = await this.reflector.call(record.activeObservations, prompt, void 0, reflectThreshold, void 0, void 0, void 0, requestContext, priorExtractedValues, observabilityContext, void 0);
29463
+ const reflectResult = await this.reflector.call(await applyTextTransform(this.hooks, "beforeReflection", record.activeObservations, hookContext), prompt, void 0, reflectThreshold, void 0, void 0, void 0, requestContext, priorExtractedValues, observabilityContext, void 0);
29464
+ reflectResult.observations = await applyTextTransform(this.hooks, "afterReflection", reflectResult.observations, hookContext);
29361
29465
  const reflectionTokenCount = this.tokenCounter.countObservations(reflectResult.observations);
29362
29466
  await this.storage.createReflectionGeneration({
29363
29467
  currentRecord: record,
@@ -30255,7 +30359,7 @@ var Memory = class Memory extends _mastra_core_memory.MastraMemory {
30255
30359
  });
30256
30360
  }
30257
30361
  async recall(args) {
30258
- const { threadId, resourceId, perPage: perPageArg, page, orderBy, threadConfig, vectorSearchString, includeSystemReminders, filter } = args;
30362
+ const { threadId, resourceId, perPage: perPageArg, page, orderBy, threadConfig, vectorSearchString, includeSystemReminders, filter, includeTotal } = args;
30259
30363
  const config = this.getMergedThreadConfig(threadConfig || {});
30260
30364
  const semanticRecallEnabled = Boolean(config.semanticRecall);
30261
30365
  const span = this.createMemorySpan("recall", args.observabilityContext, {
@@ -30342,6 +30446,7 @@ var Memory = class Memory extends _mastra_core_memory.MastraMemory {
30342
30446
  page,
30343
30447
  orderBy: effectiveOrderBy,
30344
30448
  filter,
30449
+ ...includeTotal !== void 0 ? { includeTotal } : {},
30345
30450
  ...filteredVectorResults?.length ? { include: filteredVectorResults.map((r) => ({
30346
30451
  id: r.metadata?.message_id,
30347
30452
  threadId: r.metadata?.thread_id,
@@ -30954,6 +31059,7 @@ ${workingMemory}`;
30954
31059
  direction: "ASC"
30955
31060
  },
30956
31061
  perPage: false,
31062
+ includeTotal: false,
30957
31063
  filter: dateFilter
30958
31064
  })).messages;
30959
31065
  };
@@ -30967,6 +31073,7 @@ ${workingMemory}`;
30967
31073
  direction: "ASC"
30968
31074
  },
30969
31075
  perPage: false,
31076
+ includeTotal: false,
30970
31077
  filter: dateFilter
30971
31078
  })).messages;
30972
31079
  };
@@ -30982,7 +31089,8 @@ ${workingMemory}`;
30982
31089
  field: "createdAt",
30983
31090
  direction: "DESC"
30984
31091
  },
30985
- perPage: typeof lastMessages === "number" ? lastMessages : void 0
31092
+ perPage: typeof lastMessages === "number" ? lastMessages : void 0,
31093
+ includeTotal: false
30986
31094
  })).messages.reverse();
30987
31095
  }
30988
31096
  return {
@@ -31344,7 +31452,8 @@ Notes:
31344
31452
  threadId,
31345
31453
  resourceId,
31346
31454
  perPage: SUMMARIZE_THREAD_DEFAULTS.pageSize,
31347
- page
31455
+ page,
31456
+ includeTotal: false
31348
31457
  });
31349
31458
  if (batch.length === 0) break;
31350
31459
  let reachedLimit = false;
@@ -32276,4 +32385,4 @@ Object.defineProperty(exports, "wrapInObservationGroup", {
32276
32385
  }
32277
32386
  });
32278
32387
 
32279
- //# sourceMappingURL=src-D4Wm6M4v.cjs.map
32388
+ //# sourceMappingURL=src-CG7RR0Ep.cjs.map