@mastra/server 1.78.0-alpha.1 → 1.78.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.
@@ -3,7 +3,6 @@ import type { ZodType } from '../schema.types.js';
3
3
  import { OpenAISchemaCompatLayer } from './openai.js';
4
4
  export declare class OpenAIReasoningSchemaCompatLayer extends OpenAISchemaCompatLayer {
5
5
  getSchemaTarget(): Targets | undefined;
6
- isReasoningModel(): boolean;
7
6
  shouldApply(): boolean;
8
7
  processZodType(value: ZodType): ZodType;
9
8
  }
@@ -6,6 +6,10 @@ import type { Schema } from '../json-schema/index.js';
6
6
  import { SchemaCompatLayer } from '../schema-compatibility.js';
7
7
  import type { PublicSchema, ZodType } from '../schema.types.js';
8
8
  import type { StandardSchemaWithJSON } from '../standard-schema/standard-schema.types.js';
9
+ export declare function isOpenAICompatibleModel(model: {
10
+ provider?: string;
11
+ modelId?: string;
12
+ }): boolean;
9
13
  export declare class OpenAISchemaCompatLayer extends SchemaCompatLayer {
10
14
  #private;
11
15
  getSchemaTarget(): Targets | undefined;
@@ -4637,7 +4637,7 @@ function splitLines(text) {
4637
4637
  return result;
4638
4638
  }
4639
4639
  //#endregion
4640
- //#region ../memory/dist/src-DWoBX5f9.js
4640
+ //#region ../memory/dist/src-CnLuefUv.js
4641
4641
  var __defProp$3 = Object.defineProperty;
4642
4642
  var __exportAll = (all, no_symbols) => {
4643
4643
  let target = {};
@@ -28315,6 +28315,7 @@ async function summarizeConversation(opts) {
28315
28315
  providerOptions: OBSERVATIONAL_MEMORY_DEFAULTS.observation.providerOptions,
28316
28316
  maxTokensPerBatch: OBSERVATIONAL_MEMORY_DEFAULTS.observation.maxTokensPerBatch,
28317
28317
  bufferOnIdle: false,
28318
+ observeOnContextOverflow: false,
28318
28319
  observeAttachments: "auto",
28319
28320
  instruction: opts.instructions,
28320
28321
  threadTitle: false,
@@ -35745,6 +35746,7 @@ var ObservationalMemory = class ObservationalMemory {
35745
35746
  maxTokensPerBatch: config.observation?.maxTokensPerBatch ?? OBSERVATIONAL_MEMORY_DEFAULTS.observation.maxTokensPerBatch,
35746
35747
  bufferTokens: asyncBufferingDisabled ? void 0 : resolveBufferTokens(config.observation?.bufferTokens ?? OBSERVATIONAL_MEMORY_DEFAULTS.observation.bufferTokens, config.observation?.messageTokens ?? OBSERVATIONAL_MEMORY_DEFAULTS.observation.messageTokens),
35747
35748
  bufferOnIdle: config.observation?.bufferOnIdle ?? false,
35749
+ observeOnContextOverflow: config.observation?.observeOnContextOverflow ?? true,
35748
35750
  bufferActivation: asyncBufferingDisabled ? void 0 : config.observation?.bufferActivation ?? OBSERVATIONAL_MEMORY_DEFAULTS.observation.bufferActivation,
35749
35751
  activateAfterIdle: parseActivationTTLConfig(observationActivateAfterIdle, observationActivateAfterIdlePath),
35750
35752
  activateOnProviderChange: config.observation?.activateOnProviderChange ?? config.activateOnProviderChange ?? false,
@@ -37765,7 +37767,9 @@ ${formattedMessages}
37765
37767
  recordInsideLock = freshRecord;
37766
37768
  generationBefore = freshRecord.generationCount;
37767
37769
  const unobservedMessages = messages ? this.getUnobservedMessages(messages, freshRecord) : await this.loadMessagesFromStorage(threadId, resourceId, freshRecord.lastObservedAt ? new Date(freshRecord.lastObservedAt) : void 0);
37768
- if (!this.meetsObservationThreshold({
37770
+ if (opts.force) {
37771
+ if (unobservedMessages.length === 0) return;
37772
+ } else if (!this.meetsObservationThreshold({
37769
37773
  record: freshRecord,
37770
37774
  unobservedTokens: await this.tokenCounter.countMessagesAsync(unobservedMessages)
37771
37775
  })) return;
@@ -38072,6 +38076,53 @@ ${formattedMessages}
38072
38076
  });
38073
38077
  }
38074
38078
  };
38079
+ /**
38080
+ * Detects provider errors that reject a request for exceeding the model's context window.
38081
+ *
38082
+ * Providers report this with different codes and wording, so this matches the known shapes
38083
+ * (OpenAI and OpenAI-compatible APIs, Anthropic, Google, Bedrock, xAI, Mistral, local servers)
38084
+ * against the error's message, code, response body and causes.
38085
+ */
38086
+ const CONTEXT_OVERFLOW_PATTERNS = [
38087
+ /context[_ ]length[_ ]exceeded/i,
38088
+ /maximum context length/i,
38089
+ /maximum prompt length/i,
38090
+ /prompt is too long/i,
38091
+ /input is too long/i,
38092
+ /exceed\w*\b[^.]{0,40}\bcontext (?:window|length|limit|size)/i,
38093
+ /input token count[^.]{0,40}exceeds the maximum/i,
38094
+ /reduce the length of the (?:messages|prompt|input)/i
38095
+ ];
38096
+ const MAX_CAUSE_DEPTH = 4;
38097
+ function collectErrorText(error, depth = 0) {
38098
+ if (depth > MAX_CAUSE_DEPTH || error == null) return [];
38099
+ if (typeof error === "string") return [error];
38100
+ if (typeof error !== "object") return [];
38101
+ const record = error;
38102
+ const texts = [];
38103
+ for (const key of [
38104
+ "message",
38105
+ "code",
38106
+ "type",
38107
+ "responseBody"
38108
+ ]) {
38109
+ const value = record[key];
38110
+ if (typeof value === "string") texts.push(value);
38111
+ }
38112
+ if (record.data && typeof record.data === "object") texts.push(...collectErrorText(record.data.error ?? record.data, depth + 1));
38113
+ if (record.error && typeof record.error === "object") texts.push(...collectErrorText(record.error, depth + 1));
38114
+ texts.push(...collectErrorText(record.cause, depth + 1));
38115
+ return texts;
38116
+ }
38117
+ function isRateLimited(error, depth = 0) {
38118
+ if (depth > MAX_CAUSE_DEPTH || error == null || typeof error !== "object") return false;
38119
+ const record = error;
38120
+ return record.statusCode === 429 || isRateLimited(record.cause, depth + 1);
38121
+ }
38122
+ function isContextOverflowError(error) {
38123
+ if (isRateLimited(error)) return false;
38124
+ return collectErrorText(error).some((text) => CONTEXT_OVERFLOW_PATTERNS.some((pattern) => pattern.test(text)));
38125
+ }
38075
38126
  const TEMPORAL_GAP_REMINDER_TYPE = "temporal-gap";
38076
38127
  function getTemporalGapReminderText(gapText, timestamp) {
38077
38128
  return `${gapText} — ${formatTemporalTimestamp(new Date(timestamp))}`;
@@ -38179,6 +38230,8 @@ function getOmObservabilityContext(args) {
38179
38230
  }
38180
38231
  /** Key used to store gateway detection result in per-processor state. */
38181
38232
  const GATEWAY_STATE_KEY = "__isGatewayModel";
38233
+ /** Step number OM last recovered from a context-overflow error, so each step retries at most once. */
38234
+ const CONTEXT_OVERFLOW_STEP_KEY = "__omContextOverflowStep";
38182
38235
  /** Check if the model is routed through a Mastra gateway (duck-type check to avoid cross-package instanceof issues). */
38183
38236
  function isMastraGatewayModel(model) {
38184
38237
  return typeof model === "object" && model !== null && "gatewayId" in model && model.gatewayId === "mastra";
@@ -38418,6 +38471,42 @@ var ObservationalMemoryProcessor = class {
38418
38471
  return messageList;
38419
38472
  });
38420
38473
  }
38474
+ async processAPIError(args) {
38475
+ const { error, messageList, requestContext, stepNumber, state } = args;
38476
+ if (!this.engine.getObservationConfig().observeOnContextOverflow) return;
38477
+ if (state[GATEWAY_STATE_KEY]) return;
38478
+ if (state[CONTEXT_OVERFLOW_STEP_KEY] === stepNumber) return;
38479
+ if (!isContextOverflowError(error)) return;
38480
+ const context = this.engine.getThreadContext(requestContext, messageList);
38481
+ if (!context) return;
38482
+ if (parseMemoryRequestContext(requestContext)?.memoryConfig?.readOnly) return;
38483
+ state[CONTEXT_OVERFLOW_STEP_KEY] = stepNumber;
38484
+ omDebug(`[OM:processAPIError] context overflow at step ${stepNumber} — observing pending messages`);
38485
+ const { threadId, resourceId } = context;
38486
+ await this.engine.waitForBuffering(threadId, resourceId);
38487
+ const messages = getObservableMessages(messageList);
38488
+ const { activated } = await this.engine.activate({
38489
+ threadId,
38490
+ resourceId,
38491
+ messages,
38492
+ messageList,
38493
+ writer: args.writer
38494
+ });
38495
+ const { observed } = await this.engine.getTokenCounter().runWithModelContext(state.__omActorModelContext, () => this.engine.observe({
38496
+ threadId,
38497
+ resourceId,
38498
+ messages: getObservableMessages(messageList),
38499
+ messageList,
38500
+ force: true,
38501
+ agent: args.agent,
38502
+ sendSignal: args.sendSignal,
38503
+ sendStateSignal: args.sendStateSignal,
38504
+ requestContext,
38505
+ writer: args.writer,
38506
+ observabilityContext: getOmObservabilityContext(args)
38507
+ }));
38508
+ return { retry: activated || observed };
38509
+ }
38421
38510
  get config() {
38422
38511
  return this.engine.config;
38423
38512
  }
@@ -39850,6 +39939,7 @@ ${workingMemory}`;
39850
39939
  providerOptions: omConfig.observation.providerOptions,
39851
39940
  bufferTokens: omConfig.observation.bufferTokens,
39852
39941
  bufferOnIdle: omConfig.observation.bufferOnIdle,
39942
+ observeOnContextOverflow: omConfig.observation.observeOnContextOverflow,
39853
39943
  bufferActivation: omConfig.observation.bufferActivation,
39854
39944
  blockAfter: omConfig.observation.blockAfter,
39855
39945
  previousObserverTokens: omConfig.observation.previousObserverTokens,
@@ -40928,6 +41018,17 @@ Notes:
40928
41018
  return processors;
40929
41019
  }
40930
41020
  /**
41021
+ * Extends the base implementation to add ObservationalMemory as an error processor.
41022
+ * OM uses processAPIError to observe pending messages and retry when the provider
41023
+ * rejects a request for exceeding the model's context window.
41024
+ */
41025
+ async getErrorProcessors(configuredProcessors = [], context) {
41026
+ const processors = await super.getErrorProcessors(configuredProcessors, context);
41027
+ const om = await this.createOMProcessor(configuredProcessors, context);
41028
+ if (om) processors.push(om);
41029
+ return processors;
41030
+ }
41031
+ /**
40931
41032
  * Creates an ObservationalMemory processor wrapping the shared engine.
40932
41033
  * Returns null if OM is not configured, not supported, or already present
40933
41034
  * in the user's configured processors.
@@ -51758,4 +51859,4 @@ const agentBuilderWorkflows = {
51758
51859
  //#endregion
51759
51860
  export { agentBuilderWorkflows };
51760
51861
 
51761
- //# sourceMappingURL=dist-aAfueiQI.js.map
51862
+ //# sourceMappingURL=dist-0eJzATDs.js.map