@mastra/memory 1.36.0-alpha.2 → 1.36.0-alpha.4

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 (26) 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-guides-context-engineering.md +0 -2
  4. package/dist/docs/references/docs-harness-goals.md +1 -3
  5. package/dist/docs/references/docs-memory-observational-memory.md +2 -0
  6. package/dist/docs/references/reference-file-based-agents-memory.md +0 -2
  7. package/dist/index.cjs +1 -1
  8. package/dist/index.js +1 -1
  9. package/dist/processors/index.cjs +1 -1
  10. package/dist/processors/index.js +1 -1
  11. package/dist/processors/observational-memory/observation-strategies/async-buffer.d.ts.map +1 -1
  12. package/dist/processors/observational-memory/observation-strategies/sync.d.ts.map +1 -1
  13. package/dist/processors/observational-memory/observer-agent.d.ts.map +1 -1
  14. package/dist/processors/observational-memory/observer-runner.d.ts.map +1 -1
  15. package/dist/processors/observational-memory/reflector-runner.d.ts.map +1 -1
  16. package/dist/processors/observational-memory/retry.d.ts +23 -0
  17. package/dist/processors/observational-memory/retry.d.ts.map +1 -1
  18. package/dist/processors/observational-memory/temporary-memory.d.ts +5 -1
  19. package/dist/processors/observational-memory/temporary-memory.d.ts.map +1 -1
  20. package/dist/{src-OOB19zXe.cjs → src-B07BdDQ-.cjs} +82 -22
  21. package/dist/src-B07BdDQ-.cjs.map +1 -0
  22. package/dist/{src-BxI-PDpC.js → src-BPHEGTkT.js} +82 -22
  23. package/dist/src-BPHEGTkT.js.map +1 -0
  24. package/package.json +4 -4
  25. package/dist/src-BxI-PDpC.js.map +0 -1
  26. package/dist/src-OOB19zXe.cjs.map +0 -1
@@ -18784,8 +18784,38 @@ const TRANSIENT_MESSAGE_SUBSTRINGS = [
18784
18784
  "request timed out",
18785
18785
  "request timeout",
18786
18786
  "connection reset",
18787
- "connection closed"
18787
+ "connection closed",
18788
+ "finished with finishreason \"other\""
18788
18789
  ];
18790
+ const INCOMPLETE_FINISH_REASONS = /* @__PURE__ */ new Set(["other", "unknown"]);
18791
+ /**
18792
+ * A model reply that ended before the model finished. Carries the AI SDK's
18793
+ * `isRetryable` flag, which `isTransientLLMError` and core's error processors
18794
+ * already recognize.
18795
+ *
18796
+ * @internal
18797
+ */
18798
+ var OmIncompleteResponseError = class extends Error {
18799
+ finishReason;
18800
+ isRetryable = true;
18801
+ constructor(label, finishReason) {
18802
+ super(`${label} response ended early (finishReason: ${finishReason})`);
18803
+ this.finishReason = finishReason;
18804
+ this.name = "OmIncompleteResponseError";
18805
+ }
18806
+ };
18807
+ /**
18808
+ * OM calls are single-step (`maxSteps: 1`). A step that ends with `other` or
18809
+ * `unknown` means the stream closed before the model finished, so its text is
18810
+ * partial. Throw a retryable error so `withRetry` re-runs the whole call
18811
+ * instead of saving it.
18812
+ *
18813
+ * @internal
18814
+ */
18815
+ function assertCompleteModelResponse(output, label) {
18816
+ if (output.finishReason && INCOMPLETE_FINISH_REASONS.has(output.finishReason)) throw new OmIncompleteResponseError(label, output.finishReason);
18817
+ return output;
18818
+ }
18789
18819
  function isRecord$5(value) {
18790
18820
  return typeof value === "object" && value !== null;
18791
18821
  }
@@ -20804,6 +20834,25 @@ function getObserverMessageLabel(msg) {
20804
20834
  if (isSystemReminderMessage(msg)) return "system-reminder";
20805
20835
  return msg.role.charAt(0).toUpperCase() + msg.role.slice(1);
20806
20836
  }
20837
+ /**
20838
+ * Attachment URLs the agent already found undownloadable. Core records them on the user
20839
+ * message (`metadata.mastra.unavailableAttachments`) so they are not fetched again.
20840
+ */
20841
+ function getUnavailableAttachmentUrls(msg) {
20842
+ const mastra = msg.content?.metadata?.mastra;
20843
+ const urls = isRecord$2(mastra) ? mastra.unavailableAttachments : void 0;
20844
+ return new Set(Array.isArray(urls) ? urls.filter((url) => typeof url === "string") : []);
20845
+ }
20846
+ function getObserverAttachmentUrl(part) {
20847
+ const asset = part.type === "image" ? part.image : part.data ?? part.url;
20848
+ if (asset instanceof URL) return asset.toString();
20849
+ if (typeof asset !== "string") return void 0;
20850
+ try {
20851
+ return new URL(asset).toString();
20852
+ } catch {
20853
+ return;
20854
+ }
20855
+ }
20807
20856
  function formatObserverMessage(msg, counter, toolFormatting, options) {
20808
20857
  const maxLen = options?.maxPartLength;
20809
20858
  const maxToolResultTokens = options?.maxToolResultTokens ?? 5e3;
@@ -20812,6 +20861,7 @@ function formatObserverMessage(msg, counter, toolFormatting, options) {
20812
20861
  const role = getObserverMessageLabel(msg);
20813
20862
  const attachments = [];
20814
20863
  const messageCreatedAt = normalizeObserverCreatedAt(msg.createdAt);
20864
+ const unavailableUrls = getUnavailableAttachmentUrls(msg);
20815
20865
  let lines = [];
20816
20866
  const temporalGapText = isTemporalGapMarker(msg) ? getTemporalGapMarkerText(msg) : void 0;
20817
20867
  const pushLine = (title, body, createdAt) => {
@@ -20870,7 +20920,8 @@ function formatObserverMessage(msg, counter, toolFormatting, options) {
20870
20920
  }
20871
20921
  if (partType === "image" || partType === "file") {
20872
20922
  const attachment = part;
20873
- if (shouldIncludeObserverAttachment(attachment, attachmentFilter)) {
20923
+ const attachmentUrl = unavailableUrls.size > 0 ? getObserverAttachmentUrl(attachment) : void 0;
20924
+ if (!(attachmentUrl !== void 0 && unavailableUrls.has(attachmentUrl)) && shouldIncludeObserverAttachment(attachment, attachmentFilter)) {
20874
20925
  const inputAttachment = toObserverInputAttachmentPart(attachment);
20875
20926
  if (inputAttachment) attachments.push(inputAttachment);
20876
20927
  }
@@ -21375,20 +21426,20 @@ function optimizeObservationsForContext(observations) {
21375
21426
  //#endregion
21376
21427
  //#region src/processors/observational-memory/temporary-memory.ts
21377
21428
  function createTemporaryOmMemoryContext(prefix) {
21378
- const options = {
21379
- thread: `${prefix}-${globalThis.crypto.randomUUID()}`,
21380
- resource: prefix,
21381
- options: {
21382
- lastMessages: 10,
21383
- generateTitle: false
21384
- }
21429
+ const memoryOptions = {
21430
+ lastMessages: 10,
21431
+ generateTitle: false
21385
21432
  };
21386
21433
  return {
21387
21434
  memory: new Memory({
21388
21435
  storage: new InMemoryStore(),
21389
- options: options.options
21436
+ options: memoryOptions
21390
21437
  }),
21391
- options
21438
+ newThread: () => ({
21439
+ thread: `${prefix}-${globalThis.crypto.randomUUID()}`,
21440
+ resource: prefix,
21441
+ options: memoryOptions
21442
+ })
21392
21443
  };
21393
21444
  }
21394
21445
  //#endregion
@@ -21533,6 +21584,7 @@ var ObserverRunner = class {
21533
21584
  id: isMultiThread ? "multi-thread-observer" : "observational-memory-observer",
21534
21585
  name: isMultiThread ? "multi-thread-observer" : "Observer",
21535
21586
  maxRetries: 0,
21587
+ errorProcessorDefaults: false,
21536
21588
  instructions: buildObserverSystemPrompt(isMultiThread, this.observationConfig.instruction, this.observationConfig.threadTitle, extractors),
21537
21589
  model: agentModel,
21538
21590
  ...memory ? { memory } : {},
@@ -21621,6 +21673,7 @@ var ObserverRunner = class {
21621
21673
  const temporaryMemory = activeExtractors.filter((extractor) => extractor.mode === "structured").length > 0 ? await createTemporaryOmMemoryContext("structured-observer") : void 0;
21622
21674
  const agent = temporaryMemory ? this.createAgent(resolvedModel.model, false, temporaryMemory.memory, activeExtractors) : this.createAgent(resolvedModel.model, false, void 0, activeExtractors);
21623
21675
  const internalRequestContext = withOmInternalThreadId(options?.requestContext, agent.id);
21676
+ let attemptMemory;
21624
21677
  const attachmentFilter = this.resolveAttachmentFilter(resolvedModel.model, options?.requestContext);
21625
21678
  const observerMessages = [buildObserverRequestMessage(existingObservations, messagesToObserve, {
21626
21679
  ...options,
@@ -21648,14 +21701,16 @@ var ObserverRunner = class {
21648
21701
  },
21649
21702
  callback: (childObservabilityContext) => this.withAbortCheck(async () => {
21650
21703
  try {
21651
- return await (await agent.stream(observerMessages, {
21704
+ attemptMemory = temporaryMemory?.newThread();
21705
+ return assertCompleteModelResponse(await (await agent.stream(observerMessages, {
21706
+ maxSteps: 1,
21652
21707
  modelSettings: { ...this.observationConfig.modelSettings },
21653
21708
  providerOptions: this.observationConfig.providerOptions,
21654
- ...temporaryMemory ? { memory: temporaryMemory.options } : {},
21709
+ ...attemptMemory ? { memory: attemptMemory } : {},
21655
21710
  ...abortSignal ? { abortSignal } : {},
21656
21711
  ...internalRequestContext ? { requestContext: internalRequestContext } : {},
21657
21712
  ...childObservabilityContext
21658
- })).getFullOutput();
21713
+ })).getFullOutput(), "OM observer");
21659
21714
  } catch (error) {
21660
21715
  this.mastra?.getLogger?.().error("OM observer provider call failed", {
21661
21716
  diagnostic: formatOmError(error),
@@ -21694,7 +21749,7 @@ var ObserverRunner = class {
21694
21749
  agent,
21695
21750
  source: "observer",
21696
21751
  extractors: activeExtractors,
21697
- memory: temporaryMemory?.options,
21752
+ memory: attemptMemory,
21698
21753
  priorExtractedValues: options?.priorExtractedValues,
21699
21754
  requestContext: internalRequestContext,
21700
21755
  observabilityContext: options?.observabilityContext,
@@ -21854,13 +21909,14 @@ var ObserverRunner = class {
21854
21909
  },
21855
21910
  callback: (childObservabilityContext) => this.withAbortCheck(async () => {
21856
21911
  try {
21857
- return await (await agent.stream(observerMessages, {
21912
+ return assertCompleteModelResponse(await (await agent.stream(observerMessages, {
21913
+ maxSteps: 1,
21858
21914
  modelSettings: { ...this.observationConfig.modelSettings },
21859
21915
  providerOptions: this.observationConfig.providerOptions,
21860
21916
  ...abortSignal ? { abortSignal } : {},
21861
21917
  ...internalRequestContext ? { requestContext: internalRequestContext } : {},
21862
21918
  ...childObservabilityContext
21863
- })).getFullOutput();
21919
+ })).getFullOutput(), "OM multi-thread observer");
21864
21920
  } catch (error) {
21865
21921
  this.mastra?.getLogger?.().error("OM multi-thread observer provider call failed", {
21866
21922
  diagnostic: formatOmError(error),
@@ -28864,6 +28920,7 @@ var ReflectorRunner = class {
28864
28920
  instructions: buildReflectorSystemPrompt(this.reflectionConfig.instruction, extractors),
28865
28921
  model: agentModel,
28866
28922
  maxRetries: 0,
28923
+ errorProcessorDefaults: false,
28867
28924
  ...memory ? { memory } : {},
28868
28925
  ...this.mastra ? { mastra: this.mastra } : {}
28869
28926
  });
@@ -28904,6 +28961,7 @@ var ReflectorRunner = class {
28904
28961
  const temporaryMemory = activeExtractors.filter((extractor) => extractor.mode === "structured").length > 0 ? await createTemporaryOmMemoryContext("structured-reflector") : void 0;
28905
28962
  const agent = temporaryMemory ? this.createAgent(resolvedModel.model, temporaryMemory.memory, activeExtractors) : this.createAgent(resolvedModel.model, void 0, activeExtractors);
28906
28963
  const internalRequestContext = withOmInternalThreadId(requestContext, agent.id);
28964
+ let attemptMemory;
28907
28965
  const targetThreshold = observationTokensThreshold ?? getMaxThreshold(this.reflectionConfig.observationTokens);
28908
28966
  let totalUsage = {
28909
28967
  inputTokens: 0,
@@ -28946,10 +29004,12 @@ var ReflectorRunner = class {
28946
29004
  callback: (childObservabilityContext) => withAbortCheck(async () => {
28947
29005
  chunkCount = 0;
28948
29006
  try {
28949
- return await (await agent.stream(prompt, {
29007
+ attemptMemory = temporaryMemory?.newThread();
29008
+ return assertCompleteModelResponse(await (await agent.stream(prompt, {
29009
+ maxSteps: 1,
28950
29010
  modelSettings: { ...this.reflectionConfig.modelSettings },
28951
29011
  providerOptions: this.reflectionConfig.providerOptions,
28952
- ...temporaryMemory ? { memory: temporaryMemory.options } : {},
29012
+ ...attemptMemory ? { memory: attemptMemory } : {},
28953
29013
  ...abortSignal ? { abortSignal } : {},
28954
29014
  ...internalRequestContext ? { requestContext: internalRequestContext } : {},
28955
29015
  ...childObservabilityContext,
@@ -28971,7 +29031,7 @@ var ReflectorRunner = class {
28971
29031
  omError(`[OM:callReflector] onError after ${chunkCount} chunks`, error);
28972
29032
  }
28973
29033
  } : {}
28974
- })).getFullOutput();
29034
+ })).getFullOutput(), "OM reflector");
28975
29035
  } catch (error) {
28976
29036
  if (abortSignal?.aborted || !isOmModelExecutionFailure(error)) throw error;
28977
29037
  throw new OmModelExecutionError("reflector-model", error);
@@ -29045,7 +29105,7 @@ var ReflectorRunner = class {
29045
29105
  agent,
29046
29106
  source: "reflector",
29047
29107
  extractors: activeExtractors,
29048
- memory: temporaryMemory?.options,
29108
+ memory: attemptMemory,
29049
29109
  priorExtractedValues,
29050
29110
  requestContext: internalRequestContext,
29051
29111
  observabilityContext,
@@ -35276,4 +35336,4 @@ Notes:
35276
35336
  //#endregion
35277
35337
  export { formatMessagesForObserver as A, OBSERVATION_CONTEXT_PROMPT_THREAD as B, SUMMARIZE_THREAD_DEFAULTS as C, buildObserverPrompt as D, OBSERVER_SYSTEM_PROMPT as E, parseAnchorId as F, StaleKnowledgeSemanticIndexError as G, getObservationContextPrompt as H, stripEphemeralAnchorIds as I, SUBCONSCIOUS_ACTIVITY_STATE_ID as J, SubconsciousRemindExtractor as K, OBSERVATIONAL_MEMORY_DEFAULTS as L, optimizeObservationsForContext as M, parseObserverOutput as N, buildObserverSystemPrompt as O, injectAnchorIds as P, Extractor as Q, OBSERVATION_CONTEXT_INSTRUCTIONS as R, deepMergeWorkingMemory as S, TokenCounter as T, Subconscious as U, OBSERVATION_CONTINUATION_HINT as V, KnowledgeSemanticIndexCoordinator as W, publishSubconsciousActivity as X, buildSubconsciousActivitySnapshot as Y, renderSubconsciousActivity as Z, reconcileObservationGroupsFromReflection as _, extractWorkingMemoryContent as a, wrapInObservationGroup as b, getObservationsAsOf as c, WORKING_MEMORY_STATE_ID as d, WORKING_MEMORY_STATE_PROCESSOR_ID as f, parseObservationGroups as g, deriveObservationGroupProvenance as h, WorkingMemory as i, hasCurrentTaskSection as j, extractCurrentTask as k, ObservationalMemoryProcessor as l, combineObservationGroupRanges as m, MessageHistory$1 as n, extractWorkingMemoryTags as o, WorkingMemoryStateProcessor as p, ModelByInputTokens as q, SemanticRecall as r, removeWorkingMemoryTags as s, Memory as t, ObservationalMemory as u, renderObservationGroupsForReflection as v, summarizeConversation as w, WorkingMemoryExtractor as x, stripObservationGroups as y, OBSERVATION_CONTEXT_PROMPT as z };
35278
35338
 
35279
- //# sourceMappingURL=src-BxI-PDpC.js.map
35339
+ //# sourceMappingURL=src-BPHEGTkT.js.map