@mastra/memory 1.17.6-alpha.1 → 1.18.0-alpha.3

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 (32) hide show
  1. package/CHANGELOG.md +23 -0
  2. package/dist/{chunk-WNLFJKTX.cjs → chunk-ET2TVAT3.cjs} +84 -33
  3. package/dist/chunk-ET2TVAT3.cjs.map +1 -0
  4. package/dist/{chunk-QZGJY67D.js → chunk-XVVCS6R6.js} +84 -33
  5. package/dist/chunk-XVVCS6R6.js.map +1 -0
  6. package/dist/docs/SKILL.md +1 -1
  7. package/dist/docs/assets/SOURCE_MAP.json +39 -39
  8. package/dist/docs/references/docs-memory-observational-memory.md +56 -12
  9. package/dist/docs/references/docs-memory-semantic-recall.md +68 -6
  10. package/dist/docs/references/reference-memory-observational-memory.md +11 -1
  11. package/dist/index.cjs +49 -20
  12. package/dist/index.cjs.map +1 -1
  13. package/dist/index.d.ts +2 -1
  14. package/dist/index.d.ts.map +1 -1
  15. package/dist/index.js +40 -11
  16. package/dist/index.js.map +1 -1
  17. package/dist/{observational-memory-HAJ3K5JJ.js → observational-memory-2PRVG6BF.js} +3 -3
  18. package/dist/{observational-memory-HAJ3K5JJ.js.map → observational-memory-2PRVG6BF.js.map} +1 -1
  19. package/dist/{observational-memory-BJF72NKJ.cjs → observational-memory-UJUAQKJB.cjs} +26 -26
  20. package/dist/{observational-memory-BJF72NKJ.cjs.map → observational-memory-UJUAQKJB.cjs.map} +1 -1
  21. package/dist/processors/index.cjs +24 -24
  22. package/dist/processors/index.js +1 -1
  23. package/dist/processors/observational-memory/observation-turn/load-memory-context.d.ts +9 -0
  24. package/dist/processors/observational-memory/observation-turn/load-memory-context.d.ts.map +1 -0
  25. package/dist/processors/observational-memory/observation-turn/turn.d.ts.map +1 -1
  26. package/dist/processors/observational-memory/observational-memory.d.ts.map +1 -1
  27. package/dist/processors/observational-memory/processor.d.ts.map +1 -1
  28. package/dist/processors/observational-memory/types.d.ts +35 -4
  29. package/dist/processors/observational-memory/types.d.ts.map +1 -1
  30. package/package.json +5 -5
  31. package/dist/chunk-QZGJY67D.js.map +0 -1
  32. package/dist/chunk-WNLFJKTX.cjs.map +0 -1
@@ -2022,6 +2022,22 @@ ObservationStrategy.create = ((om, opts) => {
2022
2022
  if (deps.scope === "resource" && opts.resourceId) return new ResourceScopedObservationStrategy(deps, opts);
2023
2023
  return new SyncObservationStrategy(deps, opts);
2024
2024
  });
2025
+
2026
+ // src/processors/observational-memory/observation-turn/load-memory-context.ts
2027
+ async function loadMemoryContextMessages({
2028
+ memory,
2029
+ messageList,
2030
+ threadId,
2031
+ resourceId
2032
+ }) {
2033
+ const ctx = await memory.getContext({ threadId, resourceId });
2034
+ for (const msg of ctx.messages) {
2035
+ if (msg.role !== "system") {
2036
+ messageList.add(msg, "memory");
2037
+ }
2038
+ }
2039
+ return ctx;
2040
+ }
2025
2041
  var ObservationStep = class {
2026
2042
  constructor(turn, stepNumber) {
2027
2043
  this.turn = turn;
@@ -2389,12 +2405,12 @@ var ObservationTurn = class {
2389
2405
  this._generationCountAtStart = this._record.generationCount;
2390
2406
  this.memory = memory;
2391
2407
  if (memory) {
2392
- const ctx = await memory.getContext({ threadId: this.threadId, resourceId: this.resourceId });
2393
- for (const msg of ctx.messages) {
2394
- if (msg.role !== "system") {
2395
- this.messageList.add(msg, "memory");
2396
- }
2397
- }
2408
+ const ctx = await loadMemoryContextMessages({
2409
+ memory,
2410
+ messageList: this.messageList,
2411
+ threadId: this.threadId,
2412
+ resourceId: this.resourceId
2413
+ });
2398
2414
  this._context = {
2399
2415
  messages: ctx.messages,
2400
2416
  systemMessage: ctx.systemMessage,
@@ -6643,7 +6659,7 @@ function getCurrentModel2(model) {
6643
6659
  return formatModelContext2(model?.provider, model?.modelId);
6644
6660
  }
6645
6661
  function parseActivationTTL(value, fieldPath) {
6646
- if (value === void 0) {
6662
+ if (value === void 0 || value === false) {
6647
6663
  return void 0;
6648
6664
  }
6649
6665
  if (typeof value === "number") {
@@ -6791,6 +6807,8 @@ Async buffering is enabled by default \u2014 this opt-out is only needed when us
6791
6807
  );
6792
6808
  }
6793
6809
  }
6810
+ const observationActivateAfterIdle = config.observation?.activateAfterIdle ?? config.activateAfterIdle;
6811
+ const observationActivateAfterIdlePath = config.observation?.activateAfterIdle !== void 0 ? "observation.activateAfterIdle" : "activateAfterIdle";
6794
6812
  this.observationConfig = {
6795
6813
  model: observationModel,
6796
6814
  // When shared budget, store as range: min = base threshold, max = total budget
@@ -6808,8 +6826,8 @@ Async buffering is enabled by default \u2014 this opt-out is only needed when us
6808
6826
  config.observation?.messageTokens ?? OBSERVATIONAL_MEMORY_DEFAULTS.observation.messageTokens
6809
6827
  ),
6810
6828
  bufferActivation: asyncBufferingDisabled ? void 0 : config.observation?.bufferActivation ?? OBSERVATIONAL_MEMORY_DEFAULTS.observation.bufferActivation,
6811
- activateAfterIdle: parseActivationTTL(config.activateAfterIdle, "activateAfterIdle"),
6812
- activateOnProviderChange: config.activateOnProviderChange ?? false,
6829
+ activateAfterIdle: parseActivationTTL(observationActivateAfterIdle, observationActivateAfterIdlePath),
6830
+ activateOnProviderChange: config.observation?.activateOnProviderChange ?? config.activateOnProviderChange ?? false,
6813
6831
  blockAfter: asyncBufferingDisabled ? void 0 : resolveBlockAfter(
6814
6832
  config.observation?.blockAfter ?? (config.observation?.bufferTokens ?? OBSERVATIONAL_MEMORY_DEFAULTS.observation.bufferTokens ? 1.2 : void 0),
6815
6833
  config.observation?.messageTokens ?? OBSERVATIONAL_MEMORY_DEFAULTS.observation.messageTokens
@@ -6828,8 +6846,8 @@ Async buffering is enabled by default \u2014 this opt-out is only needed when us
6828
6846
  },
6829
6847
  providerOptions: config.reflection?.providerOptions ?? OBSERVATIONAL_MEMORY_DEFAULTS.reflection.providerOptions,
6830
6848
  bufferActivation: asyncBufferingDisabled ? void 0 : config?.reflection?.bufferActivation ?? OBSERVATIONAL_MEMORY_DEFAULTS.reflection.bufferActivation,
6831
- activateAfterIdle: parseActivationTTL(config.activateAfterIdle, "activateAfterIdle"),
6832
- activateOnProviderChange: config.activateOnProviderChange ?? false,
6849
+ activateAfterIdle: parseActivationTTL(config.reflection?.activateAfterIdle, "reflection.activateAfterIdle"),
6850
+ activateOnProviderChange: config.reflection?.activateOnProviderChange ?? false,
6833
6851
  blockAfter: asyncBufferingDisabled ? void 0 : resolveBlockAfter(
6834
6852
  config.reflection?.blockAfter ?? (config.reflection?.bufferActivation ?? OBSERVATIONAL_MEMORY_DEFAULTS.reflection.bufferActivation ? 1.2 : void 0),
6835
6853
  config.reflection?.observationTokens ?? OBSERVATIONAL_MEMORY_DEFAULTS.reflection.observationTokens
@@ -9216,6 +9234,33 @@ var GATEWAY_STATE_KEY = "__isGatewayModel";
9216
9234
  function isMastraGatewayModel(model) {
9217
9235
  return typeof model === "object" && model !== null && "gatewayId" in model && model.gatewayId === "mastra";
9218
9236
  }
9237
+ function injectObservationContextMessages({
9238
+ messageList,
9239
+ systemMessages,
9240
+ continuationMessage,
9241
+ threadId,
9242
+ resourceId
9243
+ }) {
9244
+ if (!systemMessages?.length) {
9245
+ return;
9246
+ }
9247
+ messageList.clearSystemMessages("observational-memory");
9248
+ for (const msg of systemMessages) {
9249
+ messageList.addSystem(msg, "observational-memory");
9250
+ }
9251
+ const contMsg = continuationMessage ?? {
9252
+ id: "om-continuation",
9253
+ role: "user",
9254
+ createdAt: /* @__PURE__ */ new Date(0),
9255
+ content: {
9256
+ format: 2,
9257
+ parts: [{ type: "text", text: `<system-reminder>${OBSERVATION_CONTINUATION_HINT}</system-reminder>` }]
9258
+ },
9259
+ threadId,
9260
+ resourceId
9261
+ };
9262
+ messageList.add(contMsg, "memory");
9263
+ }
9219
9264
  var ObservationalMemoryProcessor = class {
9220
9265
  id = "observational-memory";
9221
9266
  name = "Observational Memory";
@@ -9270,6 +9315,25 @@ var ObservationalMemoryProcessor = class {
9270
9315
  const preMessagesSnapshot = reproCaptureEnabled ? safeCaptureJson(messageList.get.all.db()) : null;
9271
9316
  const preSerializedMessageList = reproCaptureEnabled ? safeCaptureJson(messageList.serialize()) : null;
9272
9317
  if (readOnly) {
9318
+ const ctx = await loadMemoryContextMessages({
9319
+ memory: this.memory,
9320
+ messageList,
9321
+ threadId,
9322
+ resourceId
9323
+ });
9324
+ const systemMessages = ctx.hasObservations && ctx.omRecord ? await this.engine.buildContextSystemMessages({
9325
+ threadId,
9326
+ resourceId,
9327
+ record: ctx.omRecord,
9328
+ unobservedContextBlocks: ctx.otherThreadsContext
9329
+ }) : void 0;
9330
+ injectObservationContextMessages({
9331
+ messageList,
9332
+ systemMessages,
9333
+ continuationMessage: ctx.continuationMessage,
9334
+ threadId,
9335
+ resourceId
9336
+ });
9273
9337
  return messageList;
9274
9338
  }
9275
9339
  const activeTurn = state.__omTurn ?? this.turn;
@@ -9325,26 +9389,13 @@ var ObservationalMemoryProcessor = class {
9325
9389
  }
9326
9390
  throw err;
9327
9391
  }
9328
- if (ctx.systemMessage) {
9329
- messageList.clearSystemMessages("observational-memory");
9330
- for (const msg of ctx.systemMessage) {
9331
- messageList.addSystem(msg, "observational-memory");
9332
- }
9333
- const contMsg = this.turn.context.continuation ?? {
9334
- id: "om-continuation",
9335
- role: "user",
9336
- createdAt: /* @__PURE__ */ new Date(0),
9337
- content: {
9338
- format: 2,
9339
- parts: [
9340
- { type: "text", text: `<system-reminder>${OBSERVATION_CONTINUATION_HINT}</system-reminder>` }
9341
- ]
9342
- },
9343
- threadId,
9344
- resourceId
9345
- };
9346
- messageList.add(contMsg, "memory");
9347
- }
9392
+ injectObservationContextMessages({
9393
+ messageList,
9394
+ systemMessages: ctx.systemMessage,
9395
+ continuationMessage: this.turn.context.continuation,
9396
+ threadId,
9397
+ resourceId
9398
+ });
9348
9399
  const freshRecord = await this.engine.getOrCreateRecord(threadId, resourceId);
9349
9400
  await this.engine.emitProgress({
9350
9401
  record: freshRecord,
@@ -9445,5 +9496,5 @@ function getObservationsAsOf(activeObservations, asOf) {
9445
9496
  }
9446
9497
 
9447
9498
  export { ModelByInputTokens, OBSERVER_SYSTEM_PROMPT, ObservationalMemory, ObservationalMemoryProcessor, TokenCounter, buildObserverPrompt, buildObserverSystemPrompt, combineObservationGroupRanges, deriveObservationGroupProvenance, extractCurrentTask, formatMessagesForObserver, formatToolResultForObserver, getObservationsAsOf, hasCurrentTaskSection, injectAnchorIds, optimizeObservationsForContext, parseAnchorId, parseObservationGroups, parseObserverOutput, reconcileObservationGroupsFromReflection, renderObservationGroupsForReflection, resolveToolResultValue, stripEphemeralAnchorIds, stripObservationGroups, truncateStringByTokens, wrapInObservationGroup };
9448
- //# sourceMappingURL=chunk-QZGJY67D.js.map
9449
- //# sourceMappingURL=chunk-QZGJY67D.js.map
9499
+ //# sourceMappingURL=chunk-XVVCS6R6.js.map
9500
+ //# sourceMappingURL=chunk-XVVCS6R6.js.map