@mastra/server 1.28.0-alpha.0 → 1.28.0-alpha.1

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.
@@ -1040,7 +1040,7 @@ function imageSize(input) {
1040
1040
  throw new TypeError(`unsupported file type: ${type}`);
1041
1041
  }
1042
1042
 
1043
- // ../memory/dist/chunk-MPBMHIAQ.js
1043
+ // ../memory/dist/chunk-7742VTN5.js
1044
1044
  var OM_DEBUG_LOG = process.env.OM_DEBUG ? join(process.cwd(), "om-debug.log") : null;
1045
1045
  function omDebug(msg) {
1046
1046
  if (!OM_DEBUG_LOG) return;
@@ -2097,9 +2097,11 @@ var ObservationStrategy = class _ObservationStrategy {
2097
2097
  }
2098
2098
  async streamMarker(marker) {
2099
2099
  if (this.opts.writer) {
2100
- await this.opts.writer.custom(marker).catch(() => {
2100
+ await this.opts.writer.custom({ ...marker, transient: true }).catch(() => {
2101
2101
  });
2102
2102
  }
2103
+ const markerThreadId = marker.data?.threadId ?? this.opts.threadId;
2104
+ await this.persistMarkerToStorage(marker, markerThreadId, this.opts.resourceId);
2103
2105
  }
2104
2106
  getObservationMarkerConfig() {
2105
2107
  return {
@@ -2605,7 +2607,7 @@ var AsyncBufferObservationStrategy = class extends ObservationStrategy {
2605
2607
  }
2606
2608
  }
2607
2609
  async emitEndMarkers(_cycleId, processed) {
2608
- if (!processed.observations || !this.opts.writer) return;
2610
+ if (!processed.observations) return;
2609
2611
  const { record, threadId, messages } = this.opts;
2610
2612
  const tokensBuffered = await this.tokenCounter.countMessagesAsync(messages);
2611
2613
  const updatedRecord = await this.storage.getObservationalMemory(record.threadId, record.resourceId);
@@ -2621,12 +2623,13 @@ var AsyncBufferObservationStrategy = class extends ObservationStrategy {
2621
2623
  threadId,
2622
2624
  observations: processed.observations
2623
2625
  });
2624
- void this.opts.writer.custom(endMarker).catch(() => {
2625
- });
2626
+ if (this.opts.writer) {
2627
+ void this.opts.writer.custom({ ...endMarker, transient: true }).catch(() => {
2628
+ });
2629
+ }
2626
2630
  await this.persistMarkerToStorage(endMarker, threadId, record.resourceId ?? void 0);
2627
2631
  }
2628
2632
  async emitFailedMarkers(_cycleId, error) {
2629
- if (!this.opts.writer) return;
2630
2633
  const { record, threadId, messages } = this.opts;
2631
2634
  const tokensAttempted = await this.tokenCounter.countMessagesAsync(messages);
2632
2635
  const failedMarker = createBufferingFailedMarker({
@@ -2638,8 +2641,10 @@ var AsyncBufferObservationStrategy = class extends ObservationStrategy {
2638
2641
  recordId: record.id,
2639
2642
  threadId
2640
2643
  });
2641
- void this.opts.writer.custom(failedMarker).catch(() => {
2642
- });
2644
+ if (this.opts.writer) {
2645
+ void this.opts.writer.custom({ ...failedMarker, transient: true }).catch(() => {
2646
+ });
2647
+ }
2643
2648
  await this.persistMarkerToStorage(failedMarker, threadId, record.resourceId ?? void 0);
2644
2649
  }
2645
2650
  };
@@ -3102,7 +3107,11 @@ var ObservationStep = class {
3102
3107
  }
3103
3108
  const allMsgsForToolCheck = messageList.get.all.db();
3104
3109
  const lastMessage = allMsgsForToolCheck[allMsgsForToolCheck.length - 1];
3105
- const latestStepParts = getLatestStepParts(lastMessage?.content?.parts ?? []);
3110
+ const pendingStepMessages = [...messageList.get.input.db(), ...messageList.get.response.db()];
3111
+ const latestStepParts = [
3112
+ ...getLatestStepParts(lastMessage?.content?.parts ?? []),
3113
+ ...pendingStepMessages.flatMap((msg) => getLatestStepParts(msg.content?.parts ?? []))
3114
+ ];
3106
3115
  const hasIncompleteToolCalls = latestStepParts.some(
3107
3116
  (part) => part?.type === "tool-invocation" && part.toolInvocation?.state === "call"
3108
3117
  );
@@ -3289,6 +3298,31 @@ var ObservationStep = class {
3289
3298
  writer: this.turn.writer,
3290
3299
  observabilityContext: this.turn.observabilityContext
3291
3300
  });
3301
+ if (obsResult.observed) {
3302
+ const observedMessageIds = new Set(obsResult.record.observedMessageIds ?? []);
3303
+ const liveMessages = messageList.get.all.db();
3304
+ let latestObservedIndex = -1;
3305
+ for (let i = liveMessages.length - 1; i >= 0; i--) {
3306
+ const message = liveMessages[i];
3307
+ if (message && observedMessageIds.has(message.id)) {
3308
+ latestObservedIndex = i;
3309
+ break;
3310
+ }
3311
+ }
3312
+ const messageToSeal = latestObservedIndex >= 0 ? liveMessages[latestObservedIndex] : void 0;
3313
+ const messagesToSeal = messageToSeal ? [messageToSeal] : [];
3314
+ om.sealMessagesForBuffering(messagesToSeal);
3315
+ try {
3316
+ await this.turn.hooks?.onSyncObservationComplete?.();
3317
+ } catch (error) {
3318
+ omDebug(
3319
+ `[OM:observe] onSyncObservationComplete hook failed: ${error instanceof Error ? error.message : String(error)}`
3320
+ );
3321
+ }
3322
+ if (messagesToSeal.length > 0) {
3323
+ await om.persistMessages(messagesToSeal, threadId, resourceId);
3324
+ }
3325
+ }
3292
3326
  return {
3293
3327
  succeeded: obsResult.observed,
3294
3328
  record: obsResult.record,
@@ -3314,7 +3348,7 @@ var ObservationTurn = class {
3314
3348
  observabilityContext;
3315
3349
  /** Current actor model for this step. Updated by the processor before prepare(). */
3316
3350
  actorModelContext;
3317
- /** Optional processor-provided hooks for turn/step lifecycle integration. */
3351
+ /** Processor-provided hooks for turn/step lifecycle integration. */
3318
3352
  hooks;
3319
3353
  constructor(opts) {
3320
3354
  this.om = opts.om;
@@ -3322,7 +3356,7 @@ var ObservationTurn = class {
3322
3356
  this.resourceId = opts.resourceId;
3323
3357
  this.messageList = opts.messageList;
3324
3358
  this.observabilityContext = opts.observabilityContext;
3325
- this.hooks = opts.hooks;
3359
+ this.hooks = opts.hooks ?? {};
3326
3360
  }
3327
3361
  om;
3328
3362
  threadId;
@@ -3342,6 +3376,10 @@ var ObservationTurn = class {
3342
3376
  get currentStep() {
3343
3377
  return this._currentStep;
3344
3378
  }
3379
+ addHooks(hooks) {
3380
+ if (!hooks) return;
3381
+ Object.assign(this.hooks, hooks);
3382
+ }
3345
3383
  /**
3346
3384
  * Load context and cache the record. Call once at the start of the turn.
3347
3385
  *
@@ -5399,8 +5437,9 @@ var ReflectorRunner = class {
5399
5437
  recordId: streamContext.recordId,
5400
5438
  threadId: streamContext.threadId
5401
5439
  });
5402
- await streamContext.writer.custom(failedMarker).catch(() => {
5440
+ await streamContext.writer.custom({ ...failedMarker, transient: true }).catch(() => {
5403
5441
  });
5442
+ await this.persistMarkerToStorage(failedMarker, streamContext.threadId, streamContext.resourceId);
5404
5443
  const retryCycleId = crypto.randomUUID();
5405
5444
  streamContext.cycleId = retryCycleId;
5406
5445
  const startMarker = createObservationStartMarker({
@@ -5413,8 +5452,9 @@ var ReflectorRunner = class {
5413
5452
  config: this.getObservationMarkerConfig()
5414
5453
  });
5415
5454
  streamContext.startedAt = startMarker.data.startedAt;
5416
- await streamContext.writer.custom(startMarker).catch(() => {
5455
+ await streamContext.writer.custom({ ...startMarker, transient: true }).catch(() => {
5417
5456
  });
5457
+ await this.persistMarkerToStorage(startMarker, streamContext.threadId, streamContext.resourceId);
5418
5458
  }
5419
5459
  currentLevel = Math.min(currentLevel + 1, maxLevel);
5420
5460
  }
@@ -5451,7 +5491,7 @@ var ReflectorRunner = class {
5451
5491
  recordId: record.id,
5452
5492
  threadId: record.threadId ?? ""
5453
5493
  });
5454
- void writer.custom(failedMarker).catch(() => {
5494
+ void writer.custom({ ...failedMarker, transient: true }).catch(() => {
5455
5495
  });
5456
5496
  await this.persistMarkerToStorage(failedMarker, record.threadId ?? "", record.resourceId ?? void 0);
5457
5497
  }
@@ -5509,8 +5549,13 @@ var ReflectorRunner = class {
5509
5549
  threadIds: record.threadId ? [record.threadId] : [],
5510
5550
  config: this.getObservationMarkerConfig(currentRecord)
5511
5551
  });
5512
- void writer.custom(startMarker).catch(() => {
5552
+ void writer.custom({ ...startMarker, transient: true }).catch(() => {
5513
5553
  });
5554
+ await this.persistMarkerToStorage(
5555
+ startMarker,
5556
+ currentRecord.threadId ?? "",
5557
+ currentRecord.resourceId ?? void 0
5558
+ );
5514
5559
  }
5515
5560
  const compressionStartLevel = await this.getCompressionStartLevel(requestContext);
5516
5561
  const reflectResult = await this.call(
@@ -5549,7 +5594,7 @@ var ReflectorRunner = class {
5549
5594
  threadId: currentRecord.threadId ?? "",
5550
5595
  observations: reflectResult.observations
5551
5596
  });
5552
- void writer.custom(endMarker).catch(() => {
5597
+ void writer.custom({ ...endMarker, transient: true }).catch(() => {
5553
5598
  });
5554
5599
  await this.persistMarkerToStorage(endMarker, currentRecord.threadId ?? "", currentRecord.resourceId ?? void 0);
5555
5600
  }
@@ -5648,7 +5693,7 @@ ${unreflectedContent}` : freshRecord.bufferedReflection;
5648
5693
  currentModel: activationMetadata?.currentModel,
5649
5694
  config: this.getObservationMarkerConfig(freshRecord)
5650
5695
  });
5651
- void writer.custom(activationMarker).catch(() => {
5696
+ void writer.custom({ ...activationMarker, transient: true }).catch(() => {
5652
5697
  });
5653
5698
  await this.persistMarkerToMessage(
5654
5699
  activationMarker,
@@ -5787,8 +5832,9 @@ ${unreflectedContent}` : freshRecord.bufferedReflection;
5787
5832
  threadIds: [threadId],
5788
5833
  config: this.getObservationMarkerConfig(record)
5789
5834
  });
5790
- await writer.custom(startMarker).catch(() => {
5835
+ await writer.custom({ ...startMarker, transient: true }).catch(() => {
5791
5836
  });
5837
+ await this.persistMarkerToStorage(startMarker, threadId, record.resourceId ?? void 0);
5792
5838
  }
5793
5839
  this.emitDebugEvent({
5794
5840
  type: "reflection_triggered",
@@ -5803,7 +5849,8 @@ ${unreflectedContent}` : freshRecord.bufferedReflection;
5803
5849
  cycleId,
5804
5850
  startedAt,
5805
5851
  recordId: record.id,
5806
- threadId
5852
+ threadId,
5853
+ resourceId: record.resourceId ?? void 0
5807
5854
  } : void 0;
5808
5855
  let reflectionUsage;
5809
5856
  let reflectionError;
@@ -5838,8 +5885,9 @@ ${unreflectedContent}` : freshRecord.bufferedReflection;
5838
5885
  recordId: record.id,
5839
5886
  threadId
5840
5887
  });
5841
- await writer.custom(endMarker).catch(() => {
5888
+ await writer.custom({ ...endMarker, transient: true }).catch(() => {
5842
5889
  });
5890
+ await this.persistMarkerToStorage(endMarker, threadId, record.resourceId ?? void 0);
5843
5891
  }
5844
5892
  this.emitDebugEvent({
5845
5893
  type: "reflection_complete",
@@ -5862,8 +5910,9 @@ ${unreflectedContent}` : freshRecord.bufferedReflection;
5862
5910
  recordId: record.id,
5863
5911
  threadId
5864
5912
  });
5865
- await writer.custom(failedMarker).catch(() => {
5913
+ await writer.custom({ ...failedMarker, transient: true }).catch(() => {
5866
5914
  });
5915
+ await this.persistMarkerToStorage(failedMarker, threadId, record.resourceId ?? void 0);
5867
5916
  }
5868
5917
  reflectionError = error instanceof Error ? error : new Error(String(error));
5869
5918
  if (abortSignal?.aborted) {
@@ -8790,17 +8839,18 @@ ${grouped}` : grouped;
8790
8839
  const cycleId = `buffer-obs-${Date.now()}-${Math.random().toString(36).slice(2, 11)}`;
8791
8840
  const startedAt = (/* @__PURE__ */ new Date()).toISOString();
8792
8841
  const tokensToBuffer = await this.tokenCounter.countMessagesAsync(messagesToBuffer);
8842
+ const startMarker = createBufferingStartMarker({
8843
+ cycleId,
8844
+ operationType: "observation",
8845
+ tokensToBuffer,
8846
+ recordId: freshRecord.id,
8847
+ threadId,
8848
+ threadIds: [threadId],
8849
+ config: this.getObservationMarkerConfig()
8850
+ });
8851
+ await this.persistMarkerToStorage(startMarker, threadId, freshRecord.resourceId ?? void 0);
8793
8852
  if (writer) {
8794
- const startMarker = createBufferingStartMarker({
8795
- cycleId,
8796
- operationType: "observation",
8797
- tokensToBuffer,
8798
- recordId: freshRecord.id,
8799
- threadId,
8800
- threadIds: [threadId],
8801
- config: this.getObservationMarkerConfig()
8802
- });
8803
- void writer.custom(startMarker).catch(() => {
8853
+ void writer.custom({ ...startMarker, transient: true }).catch(() => {
8804
8854
  });
8805
8855
  }
8806
8856
  omDebug(
@@ -9460,18 +9510,19 @@ ${grouped}` : grouped;
9460
9510
  }
9461
9511
  const cycleId = `buffer-obs-${Date.now()}-${Math.random().toString(36).slice(2, 11)}`;
9462
9512
  const startedAt = (/* @__PURE__ */ new Date()).toISOString();
9513
+ const startMarker = createBufferingStartMarker({
9514
+ cycleId,
9515
+ operationType: "observation",
9516
+ tokensToBuffer: newTokens,
9517
+ recordId: record.id,
9518
+ threadId,
9519
+ threadIds: [threadId],
9520
+ config: this.getObservationMarkerConfig()
9521
+ });
9522
+ await this.persistMarkerToStorage(startMarker, threadId, record.resourceId ?? void 0);
9463
9523
  const writer = opts.writer;
9464
9524
  if (writer) {
9465
- const startMarker = createBufferingStartMarker({
9466
- cycleId,
9467
- operationType: "observation",
9468
- tokensToBuffer: newTokens,
9469
- recordId: record.id,
9470
- threadId,
9471
- threadIds: [threadId],
9472
- config: this.getObservationMarkerConfig()
9473
- });
9474
- void writer.custom(startMarker).catch(() => {
9525
+ void writer.custom({ ...startMarker, transient: true }).catch(() => {
9475
9526
  });
9476
9527
  }
9477
9528
  await ObservationStrategy.create(this, {
@@ -9664,7 +9715,7 @@ ${grouped}` : grouped;
9664
9715
  currentModel,
9665
9716
  config: this.getObservationMarkerConfig()
9666
9717
  });
9667
- void opts.writer.custom(activationMarker).catch(() => {
9718
+ void opts.writer.custom({ ...activationMarker, transient: true }).catch(() => {
9668
9719
  });
9669
9720
  await this.persistMarkerToMessage(
9670
9721
  activationMarker,
@@ -10111,7 +10162,8 @@ var ObservationalMemoryProcessor = class {
10111
10162
  messageList,
10112
10163
  observabilityContext: getOmObservabilityContext(args),
10113
10164
  hooks: {
10114
- onBufferChunkSealed: rotateResponseMessageId
10165
+ onBufferChunkSealed: rotateResponseMessageId,
10166
+ onSyncObservationComplete: rotateResponseMessageId
10115
10167
  }
10116
10168
  });
10117
10169
  this.turn.writer = writer;
@@ -10122,6 +10174,10 @@ var ObservationalMemoryProcessor = class {
10122
10174
  }
10123
10175
  state.__omTurn = this.turn;
10124
10176
  }
10177
+ this.turn.addHooks({
10178
+ onBufferChunkSealed: rotateResponseMessageId,
10179
+ onSyncObservationComplete: rotateResponseMessageId
10180
+ });
10125
10181
  const observabilityContext = getOmObservabilityContext(args);
10126
10182
  state.__omObservabilityContext = observabilityContext;
10127
10183
  this.turn.observabilityContext = observabilityContext;
@@ -10257,5 +10313,5 @@ function getObservationsAsOf(activeObservations, asOf) {
10257
10313
  }
10258
10314
 
10259
10315
  export { ModelByInputTokens, OBSERVER_SYSTEM_PROMPT, ObservationalMemory, ObservationalMemoryProcessor, TokenCounter, buildObserverPrompt, buildObserverSystemPrompt, combineObservationGroupRanges, deriveObservationGroupProvenance, e, estimateTokenCount, extractCurrentTask, formatMessagesForObserver, formatToolResultForObserver, getObservationsAsOf, hasCurrentTaskSection, injectAnchorIds, optimizeObservationsForContext, parseAnchorId, parseObservationGroups, parseObserverOutput, reconcileObservationGroupsFromReflection, renderObservationGroupsForReflection, resolveToolResultValue, stripEphemeralAnchorIds, stripObservationGroups, truncateStringByTokens, wrapInObservationGroup };
10260
- //# sourceMappingURL=chunk-BXJTD5TI.js.map
10261
- //# sourceMappingURL=chunk-BXJTD5TI.js.map
10316
+ //# sourceMappingURL=chunk-HU57X24H.js.map
10317
+ //# sourceMappingURL=chunk-HU57X24H.js.map