@mastra/memory 1.29.0-alpha.2 → 1.29.1-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.
@@ -17914,6 +17914,46 @@ async function applyExtractorHooks(opts) {
17914
17914
  };
17915
17915
  }
17916
17916
  //#endregion
17917
+ //#region src/processors/observational-memory/error.ts
17918
+ function isRecord$4(value) {
17919
+ return typeof value === "object" && value !== null;
17920
+ }
17921
+ /** Keep provider diagnostics in streamed/persisted markers, not whole API request/response objects. */
17922
+ function formatOmError(error) {
17923
+ const details = /* @__PURE__ */ new Set();
17924
+ const visited = /* @__PURE__ */ new Set();
17925
+ const add = (value) => {
17926
+ if (typeof value === "string" && value.trim()) details.add(value.trim().slice(0, 2e3));
17927
+ };
17928
+ function collectErrorDetails(value, depth) {
17929
+ if (depth > 5) return;
17930
+ if (!isRecord$4(value)) {
17931
+ if (value !== void 0) add(String(value));
17932
+ return;
17933
+ }
17934
+ if (visited.has(value)) return;
17935
+ visited.add(value);
17936
+ add(value.message);
17937
+ if (typeof value.statusCode === "number") add(`HTTP ${value.statusCode}`);
17938
+ if (typeof value.responseBody === "string") try {
17939
+ const body = JSON.parse(value.responseBody);
17940
+ if (isRecord$4(body)) {
17941
+ add(body.message);
17942
+ add(body.detail);
17943
+ if (isRecord$4(body.error)) add(body.error.message);
17944
+ else add(body.error);
17945
+ }
17946
+ } catch {}
17947
+ if (value.cause !== void 0) collectErrorDetails(value.cause, depth + 1);
17948
+ if (value.error !== void 0) collectErrorDetails(value.error, depth + 1);
17949
+ }
17950
+ try {
17951
+ collectErrorDetails(error, 0);
17952
+ } catch {}
17953
+ const message = [...details].join(": ") || "Unknown error";
17954
+ return message.length > 2e3 ? `${message.slice(0, 1997)}...` : message;
17955
+ }
17956
+ //#endregion
17917
17957
  //#region src/processors/observational-memory/extraction-runner.ts
17918
17958
  function isAbortError$1(error, abortSignal) {
17919
17959
  return abortSignal?.aborted === true || error instanceof DOMException && error.name === "AbortError" || error instanceof Error && error.name === "AbortError";
@@ -18914,11 +18954,11 @@ function formatObserverAttachmentPlaceholder(part, counter) {
18914
18954
  const label = resolveObserverAttachmentLabel(part);
18915
18955
  return label ? `[${attachmentType} #${attachmentId}: ${label}]` : `[${attachmentType} #${attachmentId}]`;
18916
18956
  }
18917
- function isRecord$4(value) {
18957
+ function isRecord$3(value) {
18918
18958
  return !!value && typeof value === "object";
18919
18959
  }
18920
18960
  function mapToolResultBlockToAttachment(block) {
18921
- if (!isRecord$4(block) || typeof block.type !== "string") return;
18961
+ if (!isRecord$3(block) || typeof block.type !== "string") return;
18922
18962
  const mediaType = typeof block.mediaType === "string" ? block.mediaType : void 0;
18923
18963
  const filename = typeof block.filename === "string" ? block.filename : void 0;
18924
18964
  switch (block.type) {
@@ -18979,7 +19019,7 @@ function mapToolResultBlockToAttachment(block) {
18979
19019
  }
18980
19020
  }
18981
19021
  function extractToolResultAttachments(result, counter, attachmentFilter) {
18982
- if (!isRecord$4(result) || result.type !== "content" || !Array.isArray(result.value)) return {
19022
+ if (!isRecord$3(result) || result.type !== "content" || !Array.isArray(result.value)) return {
18983
19023
  resultWithoutAttachments: result,
18984
19024
  attachments: []
18985
19025
  };
@@ -18993,7 +19033,7 @@ function extractToolResultAttachments(result, counter, attachmentFilter) {
18993
19033
  if (shouldIncludeObserverAttachment(attachment, attachmentFilter)) attachments.push(toObserverInputAttachmentPart(attachment));
18994
19034
  const placeholder = formatObserverAttachmentPlaceholder(attachment, counter);
18995
19035
  return {
18996
- type: isRecord$4(block) ? block.type : void 0,
19036
+ type: isRecord$3(block) ? block.type : void 0,
18997
19037
  placeholder
18998
19038
  };
18999
19039
  });
@@ -19614,17 +19654,17 @@ const TRANSIENT_MESSAGE_SUBSTRINGS = [
19614
19654
  "connection reset",
19615
19655
  "connection closed"
19616
19656
  ];
19617
- function isRecord$3(value) {
19657
+ function isRecord$2(value) {
19618
19658
  return typeof value === "object" && value !== null;
19619
19659
  }
19620
19660
  function isAbortError(error) {
19621
- if (!isRecord$3(error)) return false;
19661
+ if (!isRecord$2(error)) return false;
19622
19662
  if (error.name === "AbortError") return true;
19623
19663
  if (typeof error.code === "string" && error.code === "ABORT_ERR") return true;
19624
19664
  return false;
19625
19665
  }
19626
19666
  function hasTransientMessage(value) {
19627
- if (!isRecord$3(value)) return false;
19667
+ if (!isRecord$2(value)) return false;
19628
19668
  const message = typeof value.message === "string" ? value.message.toLowerCase() : "";
19629
19669
  if (message && TRANSIENT_MESSAGE_SUBSTRINGS.some((sub) => message.includes(sub))) return true;
19630
19670
  if (typeof value.code === "string" && value.code.toUpperCase().startsWith("UND_ERR_")) return true;
@@ -19635,13 +19675,13 @@ function isRetryableHttpStatus(status) {
19635
19675
  return status >= 500 && status <= 599;
19636
19676
  }
19637
19677
  function hasRetryableHttpStatus(value) {
19638
- if (!isRecord$3(value)) return false;
19678
+ if (!isRecord$2(value)) return false;
19639
19679
  if (typeof value.statusCode === "number" && isRetryableHttpStatus(value.statusCode)) return true;
19640
19680
  if (typeof value.code === "number" && isRetryableHttpStatus(value.code)) return true;
19641
19681
  return false;
19642
19682
  }
19643
19683
  function hasIsRetryableFlag(value) {
19644
- if (!isRecord$3(value)) return false;
19684
+ if (!isRecord$2(value)) return false;
19645
19685
  return value.isRetryable === true;
19646
19686
  }
19647
19687
  /**
@@ -19659,14 +19699,14 @@ function isTransientLLMError(error) {
19659
19699
  if (isAbortError(error)) return false;
19660
19700
  const visited = /* @__PURE__ */ new WeakSet();
19661
19701
  function visit(candidate) {
19662
- if (isRecord$3(candidate)) {
19702
+ if (isRecord$2(candidate)) {
19663
19703
  if (visited.has(candidate)) return false;
19664
19704
  visited.add(candidate);
19665
19705
  }
19666
19706
  if (hasTransientMessage(candidate)) return true;
19667
19707
  if (hasRetryableHttpStatus(candidate)) return true;
19668
19708
  if (hasIsRetryableFlag(candidate)) return true;
19669
- if (isRecord$3(candidate)) {
19709
+ if (isRecord$2(candidate)) {
19670
19710
  if (visit(candidate.cause)) return true;
19671
19711
  if (visit(candidate.error)) return true;
19672
19712
  }
@@ -19939,7 +19979,19 @@ var ObserverRunner = class {
19939
19979
  }
19940
19980
  async callObserver(existingObservations, messagesToObserve, abortSignal, options) {
19941
19981
  const inputTokens = this.tokenCounter.countMessages(messagesToObserve);
19942
- const resolvedModel = options?.model ? { model: options.model } : this.resolveModel(inputTokens);
19982
+ const resolvedModel = (() => {
19983
+ try {
19984
+ return options?.model ? { model: options.model } : this.resolveModel(inputTokens);
19985
+ } catch (error) {
19986
+ this.mastra?.getLogger?.().error("OM observer model resolution failed", {
19987
+ diagnostic: formatOmError(error),
19988
+ inputTokens,
19989
+ threadId: messagesToObserve[0]?.threadId,
19990
+ hasRequestContext: Boolean(options?.requestContext)
19991
+ });
19992
+ throw error;
19993
+ }
19994
+ })();
19943
19995
  const activeExtractors = await resolveExtractors(filterObserverExtractors(this.observationConfig.extractors, options?.skipContinuationHints), {
19944
19996
  source: "observer",
19945
19997
  threadId: messagesToObserve[0]?.threadId,
@@ -19977,14 +20029,28 @@ var ObserverRunner = class {
19977
20029
  ...resolvedModel.routingThresholds ? { omRoutingThresholds: resolvedModel.routingThresholds } : {}
19978
20030
  },
19979
20031
  callback: (childObservabilityContext) => this.withAbortCheck(async () => {
19980
- return (await agent.stream(observerMessages, {
19981
- modelSettings: { ...this.observationConfig.modelSettings },
19982
- providerOptions: this.observationConfig.providerOptions,
19983
- ...temporaryMemory ? { memory: temporaryMemory.options } : {},
19984
- ...abortSignal ? { abortSignal } : {},
19985
- ...internalRequestContext ? { requestContext: internalRequestContext } : {},
19986
- ...childObservabilityContext
19987
- })).getFullOutput();
20032
+ try {
20033
+ return await (await agent.stream(observerMessages, {
20034
+ modelSettings: { ...this.observationConfig.modelSettings },
20035
+ providerOptions: this.observationConfig.providerOptions,
20036
+ ...temporaryMemory ? { memory: temporaryMemory.options } : {},
20037
+ ...abortSignal ? { abortSignal } : {},
20038
+ ...internalRequestContext ? { requestContext: internalRequestContext } : {},
20039
+ ...childObservabilityContext
20040
+ })).getFullOutput();
20041
+ } catch (error) {
20042
+ this.mastra?.getLogger?.().error("OM observer provider call failed", {
20043
+ diagnostic: formatOmError(error),
20044
+ model: typeof resolvedModel.model === "function" ? "(dynamic-model)" : this.extractModelRouterId(resolvedModel.model, internalRequestContext),
20045
+ inputTokens,
20046
+ threadId: messagesToObserve[0]?.threadId,
20047
+ observedMessageCount: messagesToObserve.length,
20048
+ providerOptionProviders: Object.keys(this.observationConfig.providerOptions ?? {}),
20049
+ hasRequestContext: Boolean(internalRequestContext),
20050
+ aborted: abortSignal?.aborted ?? false
20051
+ });
20052
+ throw error;
20053
+ }
19988
20054
  }, abortSignal)
19989
20055
  }), {
19990
20056
  label: "observer",
@@ -20081,7 +20147,22 @@ var ObserverRunner = class {
20081
20147
  async callMultiThreadObserver(existingObservations, messagesByThread, threadOrder, abortSignal, requestContext, priorMetadataByThread, observabilityContext, model) {
20082
20148
  if (threadOrder.length === 0) return { results: /* @__PURE__ */ new Map() };
20083
20149
  const inputTokens = Array.from(messagesByThread.values()).reduce((total, messages) => total + this.tokenCounter.countMessages(messages), 0);
20084
- const resolvedModel = model ? { model } : this.resolveModel(inputTokens);
20150
+ const resolvedModel = (() => {
20151
+ try {
20152
+ return model ? { model } : this.resolveModel(inputTokens);
20153
+ } catch (error) {
20154
+ this.mastra?.getLogger?.().error("OM multi-thread observer model resolution failed", {
20155
+ diagnostic: formatOmError(error),
20156
+ inputTokens,
20157
+ threadIds: {
20158
+ sample: threadOrder.slice(0, 10).map((threadId) => threadId.slice(0, 128)),
20159
+ total: threadOrder.length
20160
+ },
20161
+ hasRequestContext: Boolean(requestContext)
20162
+ });
20163
+ throw error;
20164
+ }
20165
+ })();
20085
20166
  const firstThreadMessages = messagesByThread.get(threadOrder[0] ?? "") ?? [];
20086
20167
  const activeExtractors = await resolveExtractors(this.observationConfig.extractors ?? [], {
20087
20168
  source: "observer",
@@ -20151,13 +20232,29 @@ var ObserverRunner = class {
20151
20232
  ...resolvedModel.routingThresholds ? { omRoutingThresholds: resolvedModel.routingThresholds } : {}
20152
20233
  },
20153
20234
  callback: (childObservabilityContext) => this.withAbortCheck(async () => {
20154
- return (await agent.stream(observerMessages, {
20155
- modelSettings: { ...this.observationConfig.modelSettings },
20156
- providerOptions: this.observationConfig.providerOptions,
20157
- ...abortSignal ? { abortSignal } : {},
20158
- ...internalRequestContext ? { requestContext: internalRequestContext } : {},
20159
- ...childObservabilityContext
20160
- })).getFullOutput();
20235
+ try {
20236
+ return await (await agent.stream(observerMessages, {
20237
+ modelSettings: { ...this.observationConfig.modelSettings },
20238
+ providerOptions: this.observationConfig.providerOptions,
20239
+ ...abortSignal ? { abortSignal } : {},
20240
+ ...internalRequestContext ? { requestContext: internalRequestContext } : {},
20241
+ ...childObservabilityContext
20242
+ })).getFullOutput();
20243
+ } catch (error) {
20244
+ this.mastra?.getLogger?.().error("OM multi-thread observer provider call failed", {
20245
+ diagnostic: formatOmError(error),
20246
+ model: typeof resolvedModel.model === "function" ? "(dynamic-model)" : this.extractModelRouterId(resolvedModel.model, internalRequestContext),
20247
+ inputTokens,
20248
+ threadIds: {
20249
+ sample: threadOrder.slice(0, 10).map((threadId) => threadId.slice(0, 128)),
20250
+ total: threadOrder.length
20251
+ },
20252
+ providerOptionProviders: Object.keys(this.observationConfig.providerOptions ?? {}),
20253
+ hasRequestContext: Boolean(internalRequestContext),
20254
+ aborted: abortSignal?.aborted ?? false
20255
+ });
20256
+ throw error;
20257
+ }
20161
20258
  }, abortSignal)
20162
20259
  }), {
20163
20260
  label: "observer-multi-thread",
@@ -23431,45 +23528,6 @@ function didProviderChange(actorModel, lastModel) {
23431
23528
  return false;
23432
23529
  }
23433
23530
  //#endregion
23434
- //#region src/processors/observational-memory/error.ts
23435
- function isRecord$2(value) {
23436
- return typeof value === "object" && value !== null;
23437
- }
23438
- /** Keep provider diagnostics in streamed/persisted markers, not whole API request/response objects. */
23439
- function formatOmError(error) {
23440
- const details = /* @__PURE__ */ new Set();
23441
- const visited = /* @__PURE__ */ new Set();
23442
- const add = (value) => {
23443
- if (typeof value === "string" && value.trim()) details.add(value.trim().slice(0, 2e3));
23444
- };
23445
- function collectErrorDetails(value, depth) {
23446
- if (depth > 5) return;
23447
- if (!isRecord$2(value)) {
23448
- if (value !== void 0) add(String(value));
23449
- return;
23450
- }
23451
- if (visited.has(value)) return;
23452
- visited.add(value);
23453
- add(value.message);
23454
- if (typeof value.statusCode === "number") add(`HTTP ${value.statusCode}`);
23455
- if (typeof value.responseBody === "string") try {
23456
- const body = JSON.parse(value.responseBody);
23457
- if (isRecord$2(body)) {
23458
- add(body.message);
23459
- if (isRecord$2(body.error)) add(body.error.message);
23460
- else add(body.error);
23461
- }
23462
- } catch {}
23463
- if (value.cause !== void 0) collectErrorDetails(value.cause, depth + 1);
23464
- if (value.error !== void 0) collectErrorDetails(value.error, depth + 1);
23465
- }
23466
- try {
23467
- collectErrorDetails(error, 0);
23468
- } catch {}
23469
- const message = [...details].join(": ") || "Unknown error";
23470
- return message.length > 2e3 ? `${message.slice(0, 1997)}...` : message;
23471
- }
23472
- //#endregion
23473
23531
  //#region src/processors/observational-memory/markers.ts
23474
23532
  /**
23475
23533
  * Create a start marker for when observation begins.
@@ -32424,4 +32482,4 @@ Object.defineProperty(exports, "wrapInObservationGroup", {
32424
32482
  }
32425
32483
  });
32426
32484
 
32427
- //# sourceMappingURL=src-DjCR8GJe.cjs.map
32485
+ //# sourceMappingURL=src-CkIFee1t.cjs.map