@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.
@@ -1042,7 +1042,7 @@ function imageSize(input) {
1042
1042
  throw new TypeError(`unsupported file type: ${type}`);
1043
1043
  }
1044
1044
 
1045
- // ../memory/dist/chunk-MPBMHIAQ.js
1045
+ // ../memory/dist/chunk-7742VTN5.js
1046
1046
  var OM_DEBUG_LOG = process.env.OM_DEBUG ? path.join(process.cwd(), "om-debug.log") : null;
1047
1047
  function omDebug(msg) {
1048
1048
  if (!OM_DEBUG_LOG) return;
@@ -2099,9 +2099,11 @@ var ObservationStrategy = class _ObservationStrategy {
2099
2099
  }
2100
2100
  async streamMarker(marker) {
2101
2101
  if (this.opts.writer) {
2102
- await this.opts.writer.custom(marker).catch(() => {
2102
+ await this.opts.writer.custom({ ...marker, transient: true }).catch(() => {
2103
2103
  });
2104
2104
  }
2105
+ const markerThreadId = marker.data?.threadId ?? this.opts.threadId;
2106
+ await this.persistMarkerToStorage(marker, markerThreadId, this.opts.resourceId);
2105
2107
  }
2106
2108
  getObservationMarkerConfig() {
2107
2109
  return {
@@ -2607,7 +2609,7 @@ var AsyncBufferObservationStrategy = class extends ObservationStrategy {
2607
2609
  }
2608
2610
  }
2609
2611
  async emitEndMarkers(_cycleId, processed) {
2610
- if (!processed.observations || !this.opts.writer) return;
2612
+ if (!processed.observations) return;
2611
2613
  const { record, threadId, messages } = this.opts;
2612
2614
  const tokensBuffered = await this.tokenCounter.countMessagesAsync(messages);
2613
2615
  const updatedRecord = await this.storage.getObservationalMemory(record.threadId, record.resourceId);
@@ -2623,12 +2625,13 @@ var AsyncBufferObservationStrategy = class extends ObservationStrategy {
2623
2625
  threadId,
2624
2626
  observations: processed.observations
2625
2627
  });
2626
- void this.opts.writer.custom(endMarker).catch(() => {
2627
- });
2628
+ if (this.opts.writer) {
2629
+ void this.opts.writer.custom({ ...endMarker, transient: true }).catch(() => {
2630
+ });
2631
+ }
2628
2632
  await this.persistMarkerToStorage(endMarker, threadId, record.resourceId ?? void 0);
2629
2633
  }
2630
2634
  async emitFailedMarkers(_cycleId, error) {
2631
- if (!this.opts.writer) return;
2632
2635
  const { record, threadId, messages } = this.opts;
2633
2636
  const tokensAttempted = await this.tokenCounter.countMessagesAsync(messages);
2634
2637
  const failedMarker = createBufferingFailedMarker({
@@ -2640,8 +2643,10 @@ var AsyncBufferObservationStrategy = class extends ObservationStrategy {
2640
2643
  recordId: record.id,
2641
2644
  threadId
2642
2645
  });
2643
- void this.opts.writer.custom(failedMarker).catch(() => {
2644
- });
2646
+ if (this.opts.writer) {
2647
+ void this.opts.writer.custom({ ...failedMarker, transient: true }).catch(() => {
2648
+ });
2649
+ }
2645
2650
  await this.persistMarkerToStorage(failedMarker, threadId, record.resourceId ?? void 0);
2646
2651
  }
2647
2652
  };
@@ -3104,7 +3109,11 @@ var ObservationStep = class {
3104
3109
  }
3105
3110
  const allMsgsForToolCheck = messageList.get.all.db();
3106
3111
  const lastMessage = allMsgsForToolCheck[allMsgsForToolCheck.length - 1];
3107
- const latestStepParts = getLatestStepParts(lastMessage?.content?.parts ?? []);
3112
+ const pendingStepMessages = [...messageList.get.input.db(), ...messageList.get.response.db()];
3113
+ const latestStepParts = [
3114
+ ...getLatestStepParts(lastMessage?.content?.parts ?? []),
3115
+ ...pendingStepMessages.flatMap((msg) => getLatestStepParts(msg.content?.parts ?? []))
3116
+ ];
3108
3117
  const hasIncompleteToolCalls = latestStepParts.some(
3109
3118
  (part) => part?.type === "tool-invocation" && part.toolInvocation?.state === "call"
3110
3119
  );
@@ -3291,6 +3300,31 @@ var ObservationStep = class {
3291
3300
  writer: this.turn.writer,
3292
3301
  observabilityContext: this.turn.observabilityContext
3293
3302
  });
3303
+ if (obsResult.observed) {
3304
+ const observedMessageIds = new Set(obsResult.record.observedMessageIds ?? []);
3305
+ const liveMessages = messageList.get.all.db();
3306
+ let latestObservedIndex = -1;
3307
+ for (let i = liveMessages.length - 1; i >= 0; i--) {
3308
+ const message = liveMessages[i];
3309
+ if (message && observedMessageIds.has(message.id)) {
3310
+ latestObservedIndex = i;
3311
+ break;
3312
+ }
3313
+ }
3314
+ const messageToSeal = latestObservedIndex >= 0 ? liveMessages[latestObservedIndex] : void 0;
3315
+ const messagesToSeal = messageToSeal ? [messageToSeal] : [];
3316
+ om.sealMessagesForBuffering(messagesToSeal);
3317
+ try {
3318
+ await this.turn.hooks?.onSyncObservationComplete?.();
3319
+ } catch (error) {
3320
+ omDebug(
3321
+ `[OM:observe] onSyncObservationComplete hook failed: ${error instanceof Error ? error.message : String(error)}`
3322
+ );
3323
+ }
3324
+ if (messagesToSeal.length > 0) {
3325
+ await om.persistMessages(messagesToSeal, threadId, resourceId);
3326
+ }
3327
+ }
3294
3328
  return {
3295
3329
  succeeded: obsResult.observed,
3296
3330
  record: obsResult.record,
@@ -3316,7 +3350,7 @@ var ObservationTurn = class {
3316
3350
  observabilityContext;
3317
3351
  /** Current actor model for this step. Updated by the processor before prepare(). */
3318
3352
  actorModelContext;
3319
- /** Optional processor-provided hooks for turn/step lifecycle integration. */
3353
+ /** Processor-provided hooks for turn/step lifecycle integration. */
3320
3354
  hooks;
3321
3355
  constructor(opts) {
3322
3356
  this.om = opts.om;
@@ -3324,7 +3358,7 @@ var ObservationTurn = class {
3324
3358
  this.resourceId = opts.resourceId;
3325
3359
  this.messageList = opts.messageList;
3326
3360
  this.observabilityContext = opts.observabilityContext;
3327
- this.hooks = opts.hooks;
3361
+ this.hooks = opts.hooks ?? {};
3328
3362
  }
3329
3363
  om;
3330
3364
  threadId;
@@ -3344,6 +3378,10 @@ var ObservationTurn = class {
3344
3378
  get currentStep() {
3345
3379
  return this._currentStep;
3346
3380
  }
3381
+ addHooks(hooks) {
3382
+ if (!hooks) return;
3383
+ Object.assign(this.hooks, hooks);
3384
+ }
3347
3385
  /**
3348
3386
  * Load context and cache the record. Call once at the start of the turn.
3349
3387
  *
@@ -5401,8 +5439,9 @@ var ReflectorRunner = class {
5401
5439
  recordId: streamContext.recordId,
5402
5440
  threadId: streamContext.threadId
5403
5441
  });
5404
- await streamContext.writer.custom(failedMarker).catch(() => {
5442
+ await streamContext.writer.custom({ ...failedMarker, transient: true }).catch(() => {
5405
5443
  });
5444
+ await this.persistMarkerToStorage(failedMarker, streamContext.threadId, streamContext.resourceId);
5406
5445
  const retryCycleId = crypto.randomUUID();
5407
5446
  streamContext.cycleId = retryCycleId;
5408
5447
  const startMarker = createObservationStartMarker({
@@ -5415,8 +5454,9 @@ var ReflectorRunner = class {
5415
5454
  config: this.getObservationMarkerConfig()
5416
5455
  });
5417
5456
  streamContext.startedAt = startMarker.data.startedAt;
5418
- await streamContext.writer.custom(startMarker).catch(() => {
5457
+ await streamContext.writer.custom({ ...startMarker, transient: true }).catch(() => {
5419
5458
  });
5459
+ await this.persistMarkerToStorage(startMarker, streamContext.threadId, streamContext.resourceId);
5420
5460
  }
5421
5461
  currentLevel = Math.min(currentLevel + 1, maxLevel);
5422
5462
  }
@@ -5453,7 +5493,7 @@ var ReflectorRunner = class {
5453
5493
  recordId: record.id,
5454
5494
  threadId: record.threadId ?? ""
5455
5495
  });
5456
- void writer.custom(failedMarker).catch(() => {
5496
+ void writer.custom({ ...failedMarker, transient: true }).catch(() => {
5457
5497
  });
5458
5498
  await this.persistMarkerToStorage(failedMarker, record.threadId ?? "", record.resourceId ?? void 0);
5459
5499
  }
@@ -5511,8 +5551,13 @@ var ReflectorRunner = class {
5511
5551
  threadIds: record.threadId ? [record.threadId] : [],
5512
5552
  config: this.getObservationMarkerConfig(currentRecord)
5513
5553
  });
5514
- void writer.custom(startMarker).catch(() => {
5554
+ void writer.custom({ ...startMarker, transient: true }).catch(() => {
5515
5555
  });
5556
+ await this.persistMarkerToStorage(
5557
+ startMarker,
5558
+ currentRecord.threadId ?? "",
5559
+ currentRecord.resourceId ?? void 0
5560
+ );
5516
5561
  }
5517
5562
  const compressionStartLevel = await this.getCompressionStartLevel(requestContext);
5518
5563
  const reflectResult = await this.call(
@@ -5551,7 +5596,7 @@ var ReflectorRunner = class {
5551
5596
  threadId: currentRecord.threadId ?? "",
5552
5597
  observations: reflectResult.observations
5553
5598
  });
5554
- void writer.custom(endMarker).catch(() => {
5599
+ void writer.custom({ ...endMarker, transient: true }).catch(() => {
5555
5600
  });
5556
5601
  await this.persistMarkerToStorage(endMarker, currentRecord.threadId ?? "", currentRecord.resourceId ?? void 0);
5557
5602
  }
@@ -5650,7 +5695,7 @@ ${unreflectedContent}` : freshRecord.bufferedReflection;
5650
5695
  currentModel: activationMetadata?.currentModel,
5651
5696
  config: this.getObservationMarkerConfig(freshRecord)
5652
5697
  });
5653
- void writer.custom(activationMarker).catch(() => {
5698
+ void writer.custom({ ...activationMarker, transient: true }).catch(() => {
5654
5699
  });
5655
5700
  await this.persistMarkerToMessage(
5656
5701
  activationMarker,
@@ -5789,8 +5834,9 @@ ${unreflectedContent}` : freshRecord.bufferedReflection;
5789
5834
  threadIds: [threadId],
5790
5835
  config: this.getObservationMarkerConfig(record)
5791
5836
  });
5792
- await writer.custom(startMarker).catch(() => {
5837
+ await writer.custom({ ...startMarker, transient: true }).catch(() => {
5793
5838
  });
5839
+ await this.persistMarkerToStorage(startMarker, threadId, record.resourceId ?? void 0);
5794
5840
  }
5795
5841
  this.emitDebugEvent({
5796
5842
  type: "reflection_triggered",
@@ -5805,7 +5851,8 @@ ${unreflectedContent}` : freshRecord.bufferedReflection;
5805
5851
  cycleId,
5806
5852
  startedAt,
5807
5853
  recordId: record.id,
5808
- threadId
5854
+ threadId,
5855
+ resourceId: record.resourceId ?? void 0
5809
5856
  } : void 0;
5810
5857
  let reflectionUsage;
5811
5858
  let reflectionError;
@@ -5840,8 +5887,9 @@ ${unreflectedContent}` : freshRecord.bufferedReflection;
5840
5887
  recordId: record.id,
5841
5888
  threadId
5842
5889
  });
5843
- await writer.custom(endMarker).catch(() => {
5890
+ await writer.custom({ ...endMarker, transient: true }).catch(() => {
5844
5891
  });
5892
+ await this.persistMarkerToStorage(endMarker, threadId, record.resourceId ?? void 0);
5845
5893
  }
5846
5894
  this.emitDebugEvent({
5847
5895
  type: "reflection_complete",
@@ -5864,8 +5912,9 @@ ${unreflectedContent}` : freshRecord.bufferedReflection;
5864
5912
  recordId: record.id,
5865
5913
  threadId
5866
5914
  });
5867
- await writer.custom(failedMarker).catch(() => {
5915
+ await writer.custom({ ...failedMarker, transient: true }).catch(() => {
5868
5916
  });
5917
+ await this.persistMarkerToStorage(failedMarker, threadId, record.resourceId ?? void 0);
5869
5918
  }
5870
5919
  reflectionError = error instanceof Error ? error : new Error(String(error));
5871
5920
  if (abortSignal?.aborted) {
@@ -8792,17 +8841,18 @@ ${grouped}` : grouped;
8792
8841
  const cycleId = `buffer-obs-${Date.now()}-${Math.random().toString(36).slice(2, 11)}`;
8793
8842
  const startedAt = (/* @__PURE__ */ new Date()).toISOString();
8794
8843
  const tokensToBuffer = await this.tokenCounter.countMessagesAsync(messagesToBuffer);
8844
+ const startMarker = createBufferingStartMarker({
8845
+ cycleId,
8846
+ operationType: "observation",
8847
+ tokensToBuffer,
8848
+ recordId: freshRecord.id,
8849
+ threadId,
8850
+ threadIds: [threadId],
8851
+ config: this.getObservationMarkerConfig()
8852
+ });
8853
+ await this.persistMarkerToStorage(startMarker, threadId, freshRecord.resourceId ?? void 0);
8795
8854
  if (writer) {
8796
- const startMarker = createBufferingStartMarker({
8797
- cycleId,
8798
- operationType: "observation",
8799
- tokensToBuffer,
8800
- recordId: freshRecord.id,
8801
- threadId,
8802
- threadIds: [threadId],
8803
- config: this.getObservationMarkerConfig()
8804
- });
8805
- void writer.custom(startMarker).catch(() => {
8855
+ void writer.custom({ ...startMarker, transient: true }).catch(() => {
8806
8856
  });
8807
8857
  }
8808
8858
  omDebug(
@@ -9462,18 +9512,19 @@ ${grouped}` : grouped;
9462
9512
  }
9463
9513
  const cycleId = `buffer-obs-${Date.now()}-${Math.random().toString(36).slice(2, 11)}`;
9464
9514
  const startedAt = (/* @__PURE__ */ new Date()).toISOString();
9515
+ const startMarker = createBufferingStartMarker({
9516
+ cycleId,
9517
+ operationType: "observation",
9518
+ tokensToBuffer: newTokens,
9519
+ recordId: record.id,
9520
+ threadId,
9521
+ threadIds: [threadId],
9522
+ config: this.getObservationMarkerConfig()
9523
+ });
9524
+ await this.persistMarkerToStorage(startMarker, threadId, record.resourceId ?? void 0);
9465
9525
  const writer = opts.writer;
9466
9526
  if (writer) {
9467
- const startMarker = createBufferingStartMarker({
9468
- cycleId,
9469
- operationType: "observation",
9470
- tokensToBuffer: newTokens,
9471
- recordId: record.id,
9472
- threadId,
9473
- threadIds: [threadId],
9474
- config: this.getObservationMarkerConfig()
9475
- });
9476
- void writer.custom(startMarker).catch(() => {
9527
+ void writer.custom({ ...startMarker, transient: true }).catch(() => {
9477
9528
  });
9478
9529
  }
9479
9530
  await ObservationStrategy.create(this, {
@@ -9666,7 +9717,7 @@ ${grouped}` : grouped;
9666
9717
  currentModel,
9667
9718
  config: this.getObservationMarkerConfig()
9668
9719
  });
9669
- void opts.writer.custom(activationMarker).catch(() => {
9720
+ void opts.writer.custom({ ...activationMarker, transient: true }).catch(() => {
9670
9721
  });
9671
9722
  await this.persistMarkerToMessage(
9672
9723
  activationMarker,
@@ -10113,7 +10164,8 @@ var ObservationalMemoryProcessor = class {
10113
10164
  messageList,
10114
10165
  observabilityContext: getOmObservabilityContext(args),
10115
10166
  hooks: {
10116
- onBufferChunkSealed: rotateResponseMessageId
10167
+ onBufferChunkSealed: rotateResponseMessageId,
10168
+ onSyncObservationComplete: rotateResponseMessageId
10117
10169
  }
10118
10170
  });
10119
10171
  this.turn.writer = writer;
@@ -10124,6 +10176,10 @@ var ObservationalMemoryProcessor = class {
10124
10176
  }
10125
10177
  state.__omTurn = this.turn;
10126
10178
  }
10179
+ this.turn.addHooks({
10180
+ onBufferChunkSealed: rotateResponseMessageId,
10181
+ onSyncObservationComplete: rotateResponseMessageId
10182
+ });
10127
10183
  const observabilityContext = getOmObservabilityContext(args);
10128
10184
  state.__omObservabilityContext = observabilityContext;
10129
10185
  this.turn.observabilityContext = observabilityContext;
@@ -10286,5 +10342,5 @@ exports.stripEphemeralAnchorIds = stripEphemeralAnchorIds;
10286
10342
  exports.stripObservationGroups = stripObservationGroups;
10287
10343
  exports.truncateStringByTokens = truncateStringByTokens;
10288
10344
  exports.wrapInObservationGroup = wrapInObservationGroup;
10289
- //# sourceMappingURL=chunk-HIK5MY74.cjs.map
10290
- //# sourceMappingURL=chunk-HIK5MY74.cjs.map
10345
+ //# sourceMappingURL=chunk-53QVLUCB.cjs.map
10346
+ //# sourceMappingURL=chunk-53QVLUCB.cjs.map