@mastra/memory 1.41.3-alpha.0 → 1.41.3-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.
Files changed (90) hide show
  1. package/dist/docs/SKILL.md +1 -1
  2. package/dist/docs/assets/SOURCE_MAP.json +71 -71
  3. package/dist/docs/references/docs-memory-observational-memory.md +3 -3
  4. package/dist/docs/references/docs-memory-working-memory.md +33 -0
  5. package/dist/hooks.cjs +0 -2
  6. package/dist/hooks.js +0 -2
  7. package/dist/index.cjs +1 -1
  8. package/dist/index.d.ts +26 -3
  9. package/dist/index.js +1 -1
  10. package/dist/processors/index.cjs +1 -1
  11. package/dist/processors/index.js +1 -1
  12. package/dist/processors/working-memory-instruction-compat.d.ts +54 -0
  13. package/dist/{src-ibq9d-g9.cjs → src-BC-wnTHt.cjs} +239 -43
  14. package/dist/{src-CTZXQELy.js → src-CRmfe-8x.js} +239 -43
  15. package/package.json +6 -5
  16. package/dist/hooks.cjs.map +0 -1
  17. package/dist/hooks.d.ts.map +0 -1
  18. package/dist/hooks.js.map +0 -1
  19. package/dist/index.d.ts.map +0 -1
  20. package/dist/persistence-projection.test-utils.d.ts.map +0 -1
  21. package/dist/processors/index.d.ts.map +0 -1
  22. package/dist/processors/observational-memory/activation-ttl.d.ts.map +0 -1
  23. package/dist/processors/observational-memory/anchor-ids.d.ts.map +0 -1
  24. package/dist/processors/observational-memory/auto-model.d.ts.map +0 -1
  25. package/dist/processors/observational-memory/buffering-coordinator.d.ts.map +0 -1
  26. package/dist/processors/observational-memory/built-in-extractors.d.ts.map +0 -1
  27. package/dist/processors/observational-memory/commit-queue.d.ts.map +0 -1
  28. package/dist/processors/observational-memory/constants.d.ts.map +0 -1
  29. package/dist/processors/observational-memory/date-utils.d.ts.map +0 -1
  30. package/dist/processors/observational-memory/debug.d.ts.map +0 -1
  31. package/dist/processors/observational-memory/error.d.ts.map +0 -1
  32. package/dist/processors/observational-memory/extracted-values.d.ts.map +0 -1
  33. package/dist/processors/observational-memory/extraction-runner.d.ts.map +0 -1
  34. package/dist/processors/observational-memory/extractor.d.ts.map +0 -1
  35. package/dist/processors/observational-memory/hooks.d.ts.map +0 -1
  36. package/dist/processors/observational-memory/index.d.ts.map +0 -1
  37. package/dist/processors/observational-memory/internal-request-context.d.ts.map +0 -1
  38. package/dist/processors/observational-memory/markers.d.ts.map +0 -1
  39. package/dist/processors/observational-memory/measure-image-buffer.d.ts.map +0 -1
  40. package/dist/processors/observational-memory/message-utils.d.ts.map +0 -1
  41. package/dist/processors/observational-memory/model-by-input-tokens.d.ts.map +0 -1
  42. package/dist/processors/observational-memory/model-context.d.ts.map +0 -1
  43. package/dist/processors/observational-memory/observation-groups.d.ts.map +0 -1
  44. package/dist/processors/observational-memory/observation-strategies/async-buffer.d.ts.map +0 -1
  45. package/dist/processors/observational-memory/observation-strategies/base.d.ts.map +0 -1
  46. package/dist/processors/observational-memory/observation-strategies/index.d.ts.map +0 -1
  47. package/dist/processors/observational-memory/observation-strategies/resource-scoped.d.ts.map +0 -1
  48. package/dist/processors/observational-memory/observation-strategies/sync.d.ts.map +0 -1
  49. package/dist/processors/observational-memory/observation-strategies/thread-title.d.ts.map +0 -1
  50. package/dist/processors/observational-memory/observation-strategies/types.d.ts.map +0 -1
  51. package/dist/processors/observational-memory/observation-turn/index.d.ts.map +0 -1
  52. package/dist/processors/observational-memory/observation-turn/load-memory-context.d.ts.map +0 -1
  53. package/dist/processors/observational-memory/observation-turn/safe-buffer-prefix.d.ts.map +0 -1
  54. package/dist/processors/observational-memory/observation-turn/step.d.ts.map +0 -1
  55. package/dist/processors/observational-memory/observation-turn/turn.d.ts.map +0 -1
  56. package/dist/processors/observational-memory/observation-turn/types.d.ts.map +0 -1
  57. package/dist/processors/observational-memory/observation-utils.d.ts.map +0 -1
  58. package/dist/processors/observational-memory/observational-memory.d.ts.map +0 -1
  59. package/dist/processors/observational-memory/observer-agent.d.ts.map +0 -1
  60. package/dist/processors/observational-memory/observer-runner.d.ts.map +0 -1
  61. package/dist/processors/observational-memory/operation-registry.d.ts.map +0 -1
  62. package/dist/processors/observational-memory/processor.d.ts.map +0 -1
  63. package/dist/processors/observational-memory/record-lineage.d.ts.map +0 -1
  64. package/dist/processors/observational-memory/reflector-agent.d.ts.map +0 -1
  65. package/dist/processors/observational-memory/reflector-runner.d.ts.map +0 -1
  66. package/dist/processors/observational-memory/repro-capture.d.ts.map +0 -1
  67. package/dist/processors/observational-memory/retry.d.ts.map +0 -1
  68. package/dist/processors/observational-memory/string-utils.d.ts.map +0 -1
  69. package/dist/processors/observational-memory/summarize.d.ts.map +0 -1
  70. package/dist/processors/observational-memory/temporal-markers.d.ts.map +0 -1
  71. package/dist/processors/observational-memory/temporary-memory.d.ts.map +0 -1
  72. package/dist/processors/observational-memory/thresholds.d.ts.map +0 -1
  73. package/dist/processors/observational-memory/token-counter.d.ts.map +0 -1
  74. package/dist/processors/observational-memory/tool-argument-helpers.d.ts.map +0 -1
  75. package/dist/processors/observational-memory/tool-result-helpers.d.ts.map +0 -1
  76. package/dist/processors/observational-memory/tracing.d.ts.map +0 -1
  77. package/dist/processors/observational-memory/transform-hooks.d.ts.map +0 -1
  78. package/dist/processors/observational-memory/types.d.ts.map +0 -1
  79. package/dist/processors/observational-memory/wait-with-abort.d.ts.map +0 -1
  80. package/dist/processors/observational-memory/working-memory-extractor.d.ts.map +0 -1
  81. package/dist/processors/working-memory-state/index.d.ts.map +0 -1
  82. package/dist/processors/working-memory-state/processor.d.ts.map +0 -1
  83. package/dist/removed-subconscious.d.ts.map +0 -1
  84. package/dist/src-CTZXQELy.js.map +0 -1
  85. package/dist/src-ibq9d-g9.cjs.map +0 -1
  86. package/dist/system-reminders.d.ts.map +0 -1
  87. package/dist/tools/om-observations.d.ts.map +0 -1
  88. package/dist/tools/om-search-context.d.ts.map +0 -1
  89. package/dist/tools/om-tools.d.ts.map +0 -1
  90. package/dist/tools/working-memory.d.ts.map +0 -1
@@ -30381,6 +30381,65 @@ var WorkingMemoryExtractor = class extends Extractor {
30381
30381
  }, false, true);
30382
30382
  }
30383
30383
  };
30384
+ // src/processors/working-memory-instruction-compat.ts
30385
+ /**
30386
+ * Stand-in for core's `WorkingMemory` input processor on `@mastra/core` versions whose processor renders its own
30387
+ * built-in instruction text and has no instruction provider (every core before `getWorkingMemoryInstruction`
30388
+ * was added to `MastraMemory`, including 1.86.0-alpha.1 through 1.86.0-alpha.5). `Memory.getInputProcessors()`
30389
+ * swaps it in for the auto-created processor on those cores, so `Memory` subclass instruction overrides reach
30390
+ * the model there too (#29752).
30391
+ *
30392
+ * Loading and caching match core's `WorkingMemory.processInput` exactly; only the instruction comes from the
30393
+ * memory instance. Keep it in sync with packages/core/src/processors/memory/working-memory.ts.
30394
+ */
30395
+ var WorkingMemoryInstructionCompat = class {
30396
+ options;
30397
+ id = "working-memory";
30398
+ name = "WorkingMemory";
30399
+ constructor(options) {
30400
+ this.options = options;
30401
+ }
30402
+ async processInput(args) {
30403
+ const { messageList, requestContext } = args;
30404
+ const memoryContext = (0, _mastra_core_memory.parseMemoryRequestContext)(requestContext);
30405
+ const threadId = memoryContext?.thread?.id;
30406
+ const resourceId = memoryContext?.resourceId;
30407
+ if (!memoryContext || !threadId && !resourceId) return messageList;
30408
+ const scope = this.options.scope || "resource";
30409
+ const memoryRunState = memoryContext.runState?.();
30410
+ let workingMemoryData = null;
30411
+ if (scope === "thread" && threadId) {
30412
+ const stored = (memoryRunState?.thread ?? await this.options.storage.getThreadById({ threadId }))?.metadata?.workingMemory;
30413
+ workingMemoryData = typeof stored === "string" && stored ? stored : null;
30414
+ } else if (scope === "resource" && resourceId) {
30415
+ const loadWorkingMemory = async () => {
30416
+ const resource = await this.options.storage.getResourceById({ resourceId });
30417
+ return typeof resource?.workingMemory === "string" && resource.workingMemory ? resource.workingMemory : null;
30418
+ };
30419
+ workingMemoryData = memoryRunState ? await memoryRunState.load(`working-memory:resource:${resourceId}`, loadWorkingMemory) : await loadWorkingMemory();
30420
+ }
30421
+ const loadTemplate = () => this.options.getWorkingMemoryTemplate({ memoryConfig: memoryContext.memoryConfig });
30422
+ const template = (memoryRunState ? await memoryRunState.load("working-memory:template", loadTemplate) : await loadTemplate()) || this.options.template || {
30423
+ format: "markdown",
30424
+ content: this.options.defaultTemplate
30425
+ };
30426
+ const workingMemoryConfig = memoryContext.memoryConfig?.workingMemory;
30427
+ const isConfiguredReadOnly = Boolean(this.options.readOnly || memoryContext.memoryConfig?.readOnly);
30428
+ const isAppManaged = this.options.agentManaged === false || typeof workingMemoryConfig === "object" && workingMemoryConfig.agentManaged === false;
30429
+ const instruction = this.options.renderInstruction(isConfiguredReadOnly || isAppManaged ? {
30430
+ kind: "read-only",
30431
+ template,
30432
+ data: workingMemoryData,
30433
+ autoUpdated: !isConfiguredReadOnly
30434
+ } : {
30435
+ kind: this.options.useVNext ? "vnext" : "tool",
30436
+ template,
30437
+ data: workingMemoryData
30438
+ });
30439
+ if (instruction) messageList.addSystem(instruction, "memory");
30440
+ return messageList;
30441
+ }
30442
+ };
30384
30443
  // src/removed-subconscious.ts
30385
30444
  /**
30386
30445
  * Subconscious was removed. These stubs keep code written against older `@mastra/memory`
@@ -30514,13 +30573,57 @@ function getUnobservedPartsPreservingToolCallPairs(message) {
30514
30573
  if (id) unobservedResultIds.add(id);
30515
30574
  }
30516
30575
  if (unobservedResultIds.size === 0) return unobservedParts;
30517
- const preservedCalls = parts.slice(0, endMarkerIndex).filter((part) => {
30576
+ const preserved = [];
30577
+ let pendingReasoning = [];
30578
+ let toolSinceReasoning = false;
30579
+ let keptAnything = false;
30580
+ for (let i = 0; i < endMarkerIndex; i++) {
30581
+ const part = parts[i];
30518
30582
  const id = getToolInvocationId(part, "call");
30519
- return id !== void 0 && unobservedResultIds.has(id);
30520
- });
30521
- return preservedCalls.length > 0 ? [...preservedCalls, ...unobservedParts] : unobservedParts;
30583
+ if (id !== void 0 && unobservedResultIds.has(id)) {
30584
+ for (const index of pendingReasoning) preserved.push(parts[index]);
30585
+ pendingReasoning = [];
30586
+ toolSinceReasoning = true;
30587
+ preserved.push(part);
30588
+ keptAnything = true;
30589
+ continue;
30590
+ }
30591
+ if (part?.type === "reasoning" && hasReplayableReasoningMetadata(part)) {
30592
+ if (toolSinceReasoning) pendingReasoning = [];
30593
+ toolSinceReasoning = false;
30594
+ pendingReasoning.push(i);
30595
+ continue;
30596
+ }
30597
+ if (part?.type === "tool-invocation") {
30598
+ toolSinceReasoning = true;
30599
+ continue;
30600
+ }
30601
+ if (part?.type === "step-start") {
30602
+ if (toolSinceReasoning) pendingReasoning = [];
30603
+ continue;
30604
+ }
30605
+ if (part?.type === "text") continue;
30606
+ pendingReasoning = [];
30607
+ }
30608
+ return keptAnything ? [...preserved, ...unobservedParts] : unobservedParts;
30522
30609
  }
30523
30610
  /**
30611
+ * Reasoning part metadata a provider can replay (a signature, redacted payload, item id).
30612
+ * Mirrors `hasReplayableReasoningMetadata` in `@mastra/core`'s message-list utils.
30613
+ */
30614
+ function hasReplayableReasoningMetadata(part) {
30615
+ const metadata = part.providerMetadata;
30616
+ if (!metadata || typeof metadata !== "object") return false;
30617
+ return Object.values(metadata).some((value) => !!value && typeof value === "object" && REPLAYABLE_REASONING_KEYS.some((key) => key in value));
30618
+ }
30619
+ const REPLAYABLE_REASONING_KEYS = [
30620
+ "signature",
30621
+ "redactedData",
30622
+ "itemId",
30623
+ "reasoningEncryptedContent",
30624
+ "thoughtSignature"
30625
+ ];
30626
+ /**
30524
30627
  * Get the messages Observational Memory is allowed to work with.
30525
30628
  *
30526
30629
  * Messages supplied through the `context` option and the synthetic `om-continuation`
@@ -40777,6 +40880,26 @@ function isTransientSignalMessage(message) {
40777
40880
  const metadata = message.content.metadata;
40778
40881
  return isRecord(metadata) && isRecord(metadata.signal) && !Array.isArray(metadata.signal) && metadata.signal.transient === true;
40779
40882
  }
40883
+ const DEFAULT_WORKING_MEMORY_TEMPLATE = `
40884
+ # User Information
40885
+ - **First Name**:
40886
+ - **Last Name**:
40887
+ - **Location**:
40888
+ - **Occupation**:
40889
+ - **Interests**:
40890
+ - **Goals**:
40891
+ - **Events**:
40892
+ - **Facts**:
40893
+ - **Projects**:
40894
+ `;
40895
+ /**
40896
+ * True when the installed `@mastra/core` builds its WorkingMemory processor with the memory instance as the
40897
+ * instruction provider. Cores from before `MastraMemory.getWorkingMemoryInstruction` existed (up to and
40898
+ * including 1.86.0-alpha.5) don't, and render their own built-in text. Checked per call, not at import.
40899
+ */
40900
+ function coreRendersWorkingMemoryInstructionThroughMemory() {
40901
+ return "getWorkingMemoryInstruction" in _mastra_core_memory.MastraMemory.prototype;
40902
+ }
40780
40903
  function normalizeObservationalMemoryConfig(config) {
40781
40904
  if (config === true) return { model: "auto" };
40782
40905
  if (config === false || config === void 0) return void 0;
@@ -41651,7 +41774,11 @@ ${workingMemory}`;
41651
41774
  updateMessageToHideWorkingMemoryV2(message) {
41652
41775
  const newMessage = { ...message };
41653
41776
  if (message.content && typeof message.content === "object" && !Array.isArray(message.content)) newMessage.content = { ...message.content };
41654
- if (typeof newMessage.content?.content === "string" && newMessage.content.content.length > 0) newMessage.content.content = removeWorkingMemoryTags(newMessage.content.content).trim();
41777
+ const hideTags = (text) => {
41778
+ const cleaned = removeWorkingMemoryTags(text);
41779
+ return cleaned === text ? text : cleaned.trim();
41780
+ };
41781
+ if (typeof newMessage.content?.content === "string" && newMessage.content.content.length > 0) newMessage.content.content = hideTags(newMessage.content.content);
41655
41782
  if (Array.isArray(newMessage.content?.parts)) {
41656
41783
  if (Array.isArray(newMessage.content.toolInvocations)) newMessage.content.toolInvocations = removeWorkingMemoryToolInvocations(newMessage.content.toolInvocations);
41657
41784
  const lastOriginalPart = newMessage.content.parts.at(-1);
@@ -41661,7 +41788,7 @@ ${workingMemory}`;
41661
41788
  const text = typeof part.text === "string" ? part.text : "";
41662
41789
  return {
41663
41790
  ...part,
41664
- text: removeWorkingMemoryTags(text).trim()
41791
+ text: hideTags(text)
41665
41792
  };
41666
41793
  }
41667
41794
  return part;
@@ -41704,10 +41831,14 @@ ${workingMemory}`;
41704
41831
  if (scope === "resource" && !resourceId) throw new Error("Memory error: Resource-scoped working memory is enabled but no resourceId was provided. Either provide a resourceId or explicitly set workingMemory.scope to 'thread'.");
41705
41832
  if (scope === "resource" && resourceId) {
41706
41833
  const loadWorkingMemory = async () => {
41707
- return (await (await this.getMemoryStore()).getResourceById({ resourceId }))?.workingMemory || null;
41834
+ const resource = await (await this.getMemoryStore()).getResourceById({ resourceId });
41835
+ return typeof resource?.workingMemory === "string" && resource.workingMemory ? resource.workingMemory : null;
41708
41836
  };
41709
41837
  workingMemoryData = runState ? await runState.load(`working-memory:resource:${resourceId}`, loadWorkingMemory) : await loadWorkingMemory();
41710
- } else workingMemoryData = (runState?.threadLoaded ? runState.thread : await this.getThreadById({ threadId }))?.metadata?.workingMemory;
41838
+ } else {
41839
+ const stored = (runState?.threadLoaded ? runState.thread : await this.getThreadById({ threadId }))?.metadata?.workingMemory;
41840
+ workingMemoryData = typeof stored === "string" && stored ? stored : null;
41841
+ }
41711
41842
  if (!workingMemoryData) return null;
41712
41843
  return workingMemoryData;
41713
41844
  }
@@ -41754,18 +41885,17 @@ ${workingMemory}`;
41754
41885
  });
41755
41886
  if (!workingMemoryTemplate) return null;
41756
41887
  const workingMemoryConfig = config.workingMemory;
41757
- if (config?.readOnly || workingMemoryConfig.agentManaged === false) return this.getReadOnlyWorkingMemoryInstruction({
41888
+ const args = config?.readOnly || workingMemoryConfig.agentManaged === false ? {
41889
+ kind: "read-only",
41758
41890
  template: workingMemoryTemplate,
41759
41891
  data: workingMemoryData,
41760
41892
  autoUpdated: !config?.readOnly
41761
- });
41762
- return this.isVNextWorkingMemoryConfig(memoryConfig) ? this.__experimental_getWorkingMemoryToolInstructionVNext({
41763
- template: workingMemoryTemplate,
41764
- data: workingMemoryData
41765
- }) : this.getWorkingMemoryToolInstruction({
41893
+ } : {
41894
+ kind: "version" in workingMemoryConfig && workingMemoryConfig.version === "vnext" ? "vnext" : "tool",
41766
41895
  template: workingMemoryTemplate,
41767
41896
  data: workingMemoryData
41768
- });
41897
+ };
41898
+ return (this.getWorkingMemoryInstruction(args) ?? this.renderBuiltInWorkingMemoryInstruction(args)) || null;
41769
41899
  }
41770
41900
  /** Fetch current stored copies of messages in a resumed turn, bypassing run-state history caches. */
41771
41901
  async getPersistedMessages(threadId, ids) {
@@ -42012,19 +42142,8 @@ ${workingMemory}`;
42012
42142
  } : void 0
42013
42143
  });
42014
42144
  }
42015
- defaultWorkingMemoryTemplate = `
42016
- # User Information
42017
- - **First Name**:
42018
- - **Last Name**:
42019
- - **Location**:
42020
- - **Occupation**:
42021
- - **Interests**:
42022
- - **Goals**:
42023
- - **Events**:
42024
- - **Facts**:
42025
- - **Projects**:
42026
- `;
42027
- getWorkingMemoryToolInstruction({ template, data }) {
42145
+ defaultWorkingMemoryTemplate = DEFAULT_WORKING_MEMORY_TEMPLATE;
42146
+ builtInWorkingMemoryToolInstruction({ template, data }) {
42028
42147
  const emptyWorkingMemoryTemplateObject = template.format === "json" ? (0, _mastra_core_utils.generateEmptyFromSchema)(template.content) : null;
42029
42148
  const hasEmptyWorkingMemoryTemplateObject = emptyWorkingMemoryTemplateObject && Object.keys(emptyWorkingMemoryTemplateObject).length > 0;
42030
42149
  return `WORKING_MEMORY_SYSTEM_INSTRUCTION:
@@ -42060,7 +42179,8 @@ Notes:
42060
42179
  - IMPORTANT: You MUST call updateWorkingMemory in every response to a prompt where you received relevant information.
42061
42180
  - IMPORTANT: Preserve the ${template.format === "json" ? "JSON" : "Markdown"} formatting structure above while updating the content.`;
42062
42181
  }
42063
- __experimental_getWorkingMemoryToolInstructionVNext({ template, data }) {
42182
+ builtInWorkingMemoryToolInstructionVNext({ template, data }) {
42183
+ const templateContent = typeof template.content === "string" ? template.content : JSON.stringify(template.content);
42064
42184
  return `WORKING_MEMORY_SYSTEM_INSTRUCTION:
42065
42185
  Store and update any conversation-relevant information by calling the updateWorkingMemory tool.
42066
42186
 
@@ -42073,7 +42193,7 @@ Guidelines:
42073
42193
  6. Information not being relevant to the current conversation is not a valid reason to replace or remove working memory information. Your working memory spans across multiple conversations and may be needed again later, even if it's not currently relevant.
42074
42194
 
42075
42195
  <working_memory_template>
42076
- ${template.content}
42196
+ ${templateContent}
42077
42197
  </working_memory_template>
42078
42198
 
42079
42199
  <working_memory_data>
@@ -42082,7 +42202,7 @@ ${data || "No working memory data available."}
42082
42202
 
42083
42203
  Notes:
42084
42204
  - Update memory whenever referenced information changes
42085
- ${template.content !== this.defaultWorkingMemoryTemplate ? `- Only store information if it's in the working memory template, do not store other information unless the user asks you to remember it, as that non-template information may be irrelevant` : `- If you're unsure whether to store something, store it (eg if the user tells you information about themselves, call updateWorkingMemory immediately to update it)
42205
+ ${templateContent !== DEFAULT_WORKING_MEMORY_TEMPLATE ? `- Only store information if it's in the working memory template, do not store other information unless the user asks you to remember it, as that non-template information may be irrelevant` : `- If you're unsure whether to store something, store it (eg if the user tells you information about themselves, call updateWorkingMemory immediately to update it)
42086
42206
  `}
42087
42207
  - This system is here so that you can maintain the conversation when your context window is very short. Update your working memory because you may need it to maintain the conversation without the full conversation history
42088
42208
  - REMEMBER: the way you update your working memory is by calling the updateWorkingMemory tool with the ${template.format === "json" ? "JSON" : "Markdown"} content. The system will store it for you. The user will not see it.
@@ -42090,12 +42210,7 @@ ${template.content !== this.defaultWorkingMemoryTemplate ? `- Only store informa
42090
42210
  - IMPORTANT: Preserve the ${template.format === "json" ? "JSON" : "Markdown"} formatting structure above while updating the content.
42091
42211
  `;
42092
42212
  }
42093
- /**
42094
- * Generate read-only working memory instructions.
42095
- * This provides the working memory context without any tool update instructions.
42096
- * Used when memory is in readOnly mode, or with `autoUpdated` when the app updates it (agentManaged: false).
42097
- */
42098
- getReadOnlyWorkingMemoryInstruction({ data, autoUpdated = false }) {
42213
+ builtInReadOnlyWorkingMemoryInstruction({ data, autoUpdated = false }) {
42099
42214
  return `WORKING_MEMORY_SYSTEM_INSTRUCTION (${autoUpdated ? "AUTO-UPDATED" : "READ-ONLY"}):
42100
42215
  The following is your working memory - persistent information about the user and conversation collected over previous interactions. This data is provided for context to help you maintain continuity.
42101
42216
 
@@ -42112,6 +42227,46 @@ Notes:
42112
42227
  - This system is here so that you can maintain the conversation when your context window is very short
42113
42228
  - The user will not see the working memory data directly`;
42114
42229
  }
42230
+ /** Instruction for agents that update working memory with the updateWorkingMemory tool. Override to customize it. */
42231
+ getWorkingMemoryToolInstruction(args) {
42232
+ return this.builtInWorkingMemoryToolInstruction(args);
42233
+ }
42234
+ /** Instruction for `version: 'vnext'` template working memory. Override to customize it. */
42235
+ __experimental_getWorkingMemoryToolInstructionVNext(args) {
42236
+ return this.builtInWorkingMemoryToolInstructionVNext(args);
42237
+ }
42238
+ /**
42239
+ * Generate read-only working memory instructions.
42240
+ * This provides the working memory context without any tool update instructions.
42241
+ * Used when memory is in readOnly mode, or with `autoUpdated` when the app updates it (agentManaged: false).
42242
+ * Override to customize it.
42243
+ */
42244
+ getReadOnlyWorkingMemoryInstruction(args) {
42245
+ return this.builtInReadOnlyWorkingMemoryInstruction(args);
42246
+ }
42247
+ /**
42248
+ * Renders the working memory instruction an agent receives, using the instruction methods above,
42249
+ * so a subclass that overrides them changes what the model sees. Return `undefined` to use the
42250
+ * built-in instruction, or an empty string to send none.
42251
+ */
42252
+ getWorkingMemoryInstruction(args) {
42253
+ switch (args.kind) {
42254
+ case "read-only": return this.getReadOnlyWorkingMemoryInstruction(args);
42255
+ case "vnext": return this.__experimental_getWorkingMemoryToolInstructionVNext(args);
42256
+ case "tool": return this.getWorkingMemoryToolInstruction(args);
42257
+ }
42258
+ }
42259
+ /**
42260
+ * The built-in text, ignoring subclass overrides of the instruction methods. Used when
42261
+ * getWorkingMemoryInstruction returns `undefined`, matching the agent processor's own fallback.
42262
+ */
42263
+ renderBuiltInWorkingMemoryInstruction(args) {
42264
+ switch (args.kind) {
42265
+ case "read-only": return this.builtInReadOnlyWorkingMemoryInstruction(args);
42266
+ case "vnext": return this.builtInWorkingMemoryToolInstructionVNext(args);
42267
+ case "tool": return this.builtInWorkingMemoryToolInstruction(args);
42268
+ }
42269
+ }
42115
42270
  isVNextWorkingMemoryConfig(config) {
42116
42271
  if (!config?.workingMemory) return false;
42117
42272
  const isMDWorkingMemory = !(`schema` in config.workingMemory) && (typeof config.workingMemory.template === `string` || config.workingMemory.template) && config.workingMemory;
@@ -42811,8 +42966,11 @@ Notes:
42811
42966
  };
42812
42967
  }
42813
42968
  const moved = /* @__PURE__ */ new Set();
42814
- for (;;) {
42815
- const pending = (await vector.query({
42969
+ const deadline = Date.now() + 12e4;
42970
+ let retryDelayMs = 1e3;
42971
+ let complete = false;
42972
+ migration: while (Date.now() < deadline) {
42973
+ const stale = await vector.query({
42816
42974
  indexName,
42817
42975
  ...readParams,
42818
42976
  topK: 20,
@@ -42820,9 +42978,21 @@ Notes:
42820
42978
  thread_id: threadId,
42821
42979
  resource_id: { $ne: resourceId }
42822
42980
  }
42823
- })).filter((result) => !moved.has(result.id));
42824
- if (pending.length === 0) break;
42981
+ });
42982
+ if (stale.length === 0) {
42983
+ complete = true;
42984
+ break;
42985
+ }
42986
+ const pending = stale.filter((result) => !moved.has(result.id));
42987
+ if (pending.length === 0) {
42988
+ const delayMs = Math.min(retryDelayMs, Math.max(0, deadline - Date.now()));
42989
+ await new Promise((resolve) => setTimeout(resolve, delayMs));
42990
+ retryDelayMs = Math.min(retryDelayMs * 2, 3e4);
42991
+ continue;
42992
+ }
42993
+ retryDelayMs = 1e3;
42825
42994
  for (const result of pending) {
42995
+ if (Date.now() >= deadline) break migration;
42826
42996
  moved.add(result.id);
42827
42997
  if (!result.metadata) {
42828
42998
  this.logger.warn("Skipping observation vector without metadata during thread transfer", {
@@ -42844,6 +43014,7 @@ Notes:
42844
43014
  });
42845
43015
  }
42846
43016
  }
43017
+ if (!complete) throw new Error(`Observation vector migration in index "${indexName}" did not complete within 120 seconds (${moved.size} vector IDs processed). The store may still be returning stale results; retry the transfer.`);
42847
43018
  }
42848
43019
  }
42849
43020
  /**
@@ -43044,6 +43215,10 @@ Notes:
43044
43215
  */
43045
43216
  async getInputProcessors(configuredProcessors = [], context) {
43046
43217
  const processors = await super.getInputProcessors(configuredProcessors, context);
43218
+ if (!coreRendersWorkingMemoryInstructionThroughMemory()) {
43219
+ const index = processors.findIndex((p) => p.id === "working-memory" && !configuredProcessors.includes(p));
43220
+ if (index !== -1) processors[index] = await this.createWorkingMemoryInstructionCompatProcessor(context);
43221
+ }
43047
43222
  const om = await this.createOMProcessor(configuredProcessors, context);
43048
43223
  if (om) processors.push(om);
43049
43224
  const wm = await this.createWorkingMemoryStateProcessor(configuredProcessors, context);
@@ -43092,6 +43267,29 @@ Notes:
43092
43267
  return new ObservationalMemoryProcessor(engine, this, { temporalMarkers: effectiveConfig.temporalMarkers });
43093
43268
  }
43094
43269
  /**
43270
+ * Builds the WorkingMemory input processor for cores without an instruction provider, from the same
43271
+ * config core's `MastraMemory.getInputProcessors()` uses for the processor it replaces.
43272
+ */
43273
+ async createWorkingMemoryInstructionCompatProcessor(context) {
43274
+ const runtimeMemory = context?.get("MastraMemory");
43275
+ const effectiveConfig = runtimeMemory?.memoryConfig ? this.getMergedThreadConfig(runtimeMemory.memoryConfig) : this.threadConfig;
43276
+ const workingMemory = typeof effectiveConfig.workingMemory === "object" ? effectiveConfig.workingMemory : void 0;
43277
+ return new WorkingMemoryInstructionCompat({
43278
+ storage: await this.getMemoryStore(),
43279
+ template: workingMemory?.template ? {
43280
+ format: "markdown",
43281
+ content: workingMemory.template
43282
+ } : void 0,
43283
+ scope: workingMemory?.scope,
43284
+ useVNext: workingMemory !== void 0 && "version" in workingMemory && workingMemory.version === "vnext",
43285
+ readOnly: effectiveConfig.readOnly,
43286
+ agentManaged: workingMemory?.agentManaged,
43287
+ defaultTemplate: DEFAULT_WORKING_MEMORY_TEMPLATE,
43288
+ getWorkingMemoryTemplate: (args) => this.getWorkingMemoryTemplate(args),
43289
+ renderInstruction: (args) => this.getWorkingMemoryInstruction(args) ?? this.renderBuiltInWorkingMemoryInstruction(args)
43290
+ });
43291
+ }
43292
+ /**
43095
43293
  * Creates a WorkingMemoryStateProcessor when working memory is enabled and the
43096
43294
  * `useStateSignals` opt-in is set. Returns null otherwise or if the processor
43097
43295
  * is already present in the user's configured processors.
@@ -43383,5 +43581,3 @@ Object.defineProperty(exports, "wrapInObservationGroup", {
43383
43581
  return wrapInObservationGroup;
43384
43582
  }
43385
43583
  });
43386
-
43387
- //# sourceMappingURL=src-ibq9d-g9.cjs.map