@mastra/memory 1.17.6-alpha.1 → 1.18.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.
@@ -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,
@@ -9216,6 +9232,33 @@ var GATEWAY_STATE_KEY = "__isGatewayModel";
9216
9232
  function isMastraGatewayModel(model) {
9217
9233
  return typeof model === "object" && model !== null && "gatewayId" in model && model.gatewayId === "mastra";
9218
9234
  }
9235
+ function injectObservationContextMessages({
9236
+ messageList,
9237
+ systemMessages,
9238
+ continuationMessage,
9239
+ threadId,
9240
+ resourceId
9241
+ }) {
9242
+ if (!systemMessages?.length) {
9243
+ return;
9244
+ }
9245
+ messageList.clearSystemMessages("observational-memory");
9246
+ for (const msg of systemMessages) {
9247
+ messageList.addSystem(msg, "observational-memory");
9248
+ }
9249
+ const contMsg = continuationMessage ?? {
9250
+ id: "om-continuation",
9251
+ role: "user",
9252
+ createdAt: /* @__PURE__ */ new Date(0),
9253
+ content: {
9254
+ format: 2,
9255
+ parts: [{ type: "text", text: `<system-reminder>${OBSERVATION_CONTINUATION_HINT}</system-reminder>` }]
9256
+ },
9257
+ threadId,
9258
+ resourceId
9259
+ };
9260
+ messageList.add(contMsg, "memory");
9261
+ }
9219
9262
  var ObservationalMemoryProcessor = class {
9220
9263
  id = "observational-memory";
9221
9264
  name = "Observational Memory";
@@ -9270,6 +9313,25 @@ var ObservationalMemoryProcessor = class {
9270
9313
  const preMessagesSnapshot = reproCaptureEnabled ? safeCaptureJson(messageList.get.all.db()) : null;
9271
9314
  const preSerializedMessageList = reproCaptureEnabled ? safeCaptureJson(messageList.serialize()) : null;
9272
9315
  if (readOnly) {
9316
+ const ctx = await loadMemoryContextMessages({
9317
+ memory: this.memory,
9318
+ messageList,
9319
+ threadId,
9320
+ resourceId
9321
+ });
9322
+ const systemMessages = ctx.hasObservations && ctx.omRecord ? await this.engine.buildContextSystemMessages({
9323
+ threadId,
9324
+ resourceId,
9325
+ record: ctx.omRecord,
9326
+ unobservedContextBlocks: ctx.otherThreadsContext
9327
+ }) : void 0;
9328
+ injectObservationContextMessages({
9329
+ messageList,
9330
+ systemMessages,
9331
+ continuationMessage: ctx.continuationMessage,
9332
+ threadId,
9333
+ resourceId
9334
+ });
9273
9335
  return messageList;
9274
9336
  }
9275
9337
  const activeTurn = state.__omTurn ?? this.turn;
@@ -9325,26 +9387,13 @@ var ObservationalMemoryProcessor = class {
9325
9387
  }
9326
9388
  throw err;
9327
9389
  }
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
- }
9390
+ injectObservationContextMessages({
9391
+ messageList,
9392
+ systemMessages: ctx.systemMessage,
9393
+ continuationMessage: this.turn.context.continuation,
9394
+ threadId,
9395
+ resourceId
9396
+ });
9348
9397
  const freshRecord = await this.engine.getOrCreateRecord(threadId, resourceId);
9349
9398
  await this.engine.emitProgress({
9350
9399
  record: freshRecord,
@@ -9445,5 +9494,5 @@ function getObservationsAsOf(activeObservations, asOf) {
9445
9494
  }
9446
9495
 
9447
9496
  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
9497
+ //# sourceMappingURL=chunk-PBZHHKPE.js.map
9498
+ //# sourceMappingURL=chunk-PBZHHKPE.js.map