@mastra/server 1.23.0-alpha.3 → 1.23.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.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,12 @@
1
1
  # @mastra/server
2
2
 
3
+ ## 1.23.0-alpha.4
4
+
5
+ ### Patch Changes
6
+
7
+ - Updated dependencies [[`fff91cf`](https://github.com/mastra-ai/mastra/commit/fff91cf914de0e731578aacebffdeebef82f0440)]:
8
+ - @mastra/core@1.23.0-alpha.4
9
+
3
10
  ## 1.23.0-alpha.3
4
11
 
5
12
  ### Patch Changes
@@ -1954,7 +1954,7 @@ var ObservationStrategy = class _ObservationStrategy {
1954
1954
  static create;
1955
1955
  /**
1956
1956
  * Run the full observation lifecycle.
1957
- * @returns `true` if a full observation cycle completed; `false` if skipped (stale lock) or async-buffer failure was swallowed.
1957
+ * @returns Result with `observed` flag and optional `usage` from the observer LLM call.
1958
1958
  * @throws On sync/resource-scoped observer failure after failed markers (same as pre–Option-A contract).
1959
1959
  */
1960
1960
  async run() {
@@ -1964,7 +1964,7 @@ var ObservationStrategy = class _ObservationStrategy {
1964
1964
  if (this.needsLock) {
1965
1965
  const fresh = await this.storage.getObservationalMemory(record.threadId, record.resourceId);
1966
1966
  if (fresh?.lastObservedAt && record.lastObservedAt && fresh.lastObservedAt > record.lastObservedAt) {
1967
- return false;
1967
+ return { observed: false };
1968
1968
  }
1969
1969
  }
1970
1970
  const { messages, existingObservations } = await this.prepare();
@@ -1985,7 +1985,7 @@ var ObservationStrategy = class _ObservationStrategy {
1985
1985
  observabilityContext: this.opts.observabilityContext
1986
1986
  });
1987
1987
  }
1988
- return true;
1988
+ return { observed: true, usage: output.usage };
1989
1989
  } catch (error) {
1990
1990
  await this.emitFailedMarkers(cycleId, error);
1991
1991
  if (!this.rethrowOnFailure) {
@@ -2004,7 +2004,7 @@ var ObservationStrategy = class _ObservationStrategy {
2004
2004
  });
2005
2005
  if (abortSignal?.aborted) throw error;
2006
2006
  omError("[OM] Observation failed", error);
2007
- return false;
2007
+ return { observed: false };
2008
2008
  }
2009
2009
  omError("[OM] Observation failed", error);
2010
2010
  throw error;
@@ -5188,7 +5188,7 @@ var ReflectorRunner = class {
5188
5188
  /**
5189
5189
  * Start an async buffered reflection in the background.
5190
5190
  */
5191
- startAsyncBufferedReflection(record, observationTokens, lockKey, writer, requestContext, observabilityContext) {
5191
+ startAsyncBufferedReflection(record, observationTokens, lockKey, writer, requestContext, observabilityContext, reflectionHooks) {
5192
5192
  const bufferKey = this.buffering.getReflectionBufferKey(lockKey);
5193
5193
  if (this.buffering.isAsyncBufferingInProgress(bufferKey)) {
5194
5194
  return;
@@ -5198,7 +5198,10 @@ var ReflectorRunner = class {
5198
5198
  this.storage.setBufferingReflectionFlag(record.id, true).catch((err) => {
5199
5199
  omError("[OM] Failed to set buffering reflection flag", err);
5200
5200
  });
5201
- const asyncOp = this.doAsyncBufferedReflection(record, bufferKey, writer, requestContext, observabilityContext).catch(async (error) => {
5201
+ reflectionHooks?.onReflectionStart?.();
5202
+ const asyncOp = this.doAsyncBufferedReflection(record, bufferKey, writer, requestContext, observabilityContext).then((usage) => {
5203
+ reflectionHooks?.onReflectionEnd?.({ usage });
5204
+ }).catch(async (error) => {
5202
5205
  if (writer) {
5203
5206
  const failedMarker = createBufferingFailedMarker({
5204
5207
  cycleId: `reflect-buf-${Date.now()}-${Math.random().toString(36).slice(2, 11)}`,
@@ -5214,6 +5217,10 @@ var ReflectorRunner = class {
5214
5217
  await this.persistMarkerToStorage(failedMarker, record.threadId ?? "", record.resourceId ?? void 0);
5215
5218
  }
5216
5219
  omError("[OM] Async buffered reflection failed", error);
5220
+ reflectionHooks?.onReflectionEnd?.({
5221
+ usage: void 0,
5222
+ error: error instanceof Error ? error : new Error(String(error))
5223
+ });
5217
5224
  BufferingCoordinator.lastBufferedBoundary.delete(bufferKey);
5218
5225
  }).finally(() => {
5219
5226
  BufferingCoordinator.asyncBufferingOps.delete(bufferKey);
@@ -5307,6 +5314,7 @@ var ReflectorRunner = class {
5307
5314
  });
5308
5315
  await this.persistMarkerToStorage(endMarker, currentRecord.threadId ?? "", currentRecord.resourceId ?? void 0);
5309
5316
  }
5317
+ return reflectResult.usage;
5310
5318
  }
5311
5319
  /**
5312
5320
  * Try to activate buffered reflection when threshold is reached.
@@ -5427,7 +5435,8 @@ ${unreflectedContent}` : freshRecord.bufferedReflection;
5427
5435
  lockKey,
5428
5436
  writer,
5429
5437
  requestContext,
5430
- observabilityContext
5438
+ observabilityContext,
5439
+ reflectionHooks
5431
5440
  );
5432
5441
  }
5433
5442
  }
@@ -5461,7 +5470,8 @@ ${unreflectedContent}` : freshRecord.bufferedReflection;
5461
5470
  lockKey,
5462
5471
  writer,
5463
5472
  requestContext,
5464
- observabilityContext
5473
+ observabilityContext,
5474
+ reflectionHooks
5465
5475
  );
5466
5476
  return;
5467
5477
  }
@@ -5500,6 +5510,8 @@ ${unreflectedContent}` : freshRecord.bufferedReflection;
5500
5510
  recordId: record.id,
5501
5511
  threadId
5502
5512
  } : void 0;
5513
+ let reflectionUsage;
5514
+ let reflectionError;
5503
5515
  try {
5504
5516
  const compressionStartLevel = await this.getCompressionStartLevel(requestContext);
5505
5517
  const reflectResult = await this.call(
@@ -5513,6 +5525,7 @@ ${unreflectedContent}` : freshRecord.bufferedReflection;
5513
5525
  requestContext,
5514
5526
  observabilityContext
5515
5527
  );
5528
+ reflectionUsage = reflectResult.usage;
5516
5529
  const reflectionTokenCount = this.tokenCounter.countObservations(reflectResult.observations);
5517
5530
  await this.storage.createReflectionGeneration({
5518
5531
  currentRecord: record,
@@ -5557,13 +5570,14 @@ ${unreflectedContent}` : freshRecord.bufferedReflection;
5557
5570
  await writer.custom(failedMarker).catch(() => {
5558
5571
  });
5559
5572
  }
5573
+ reflectionError = error instanceof Error ? error : new Error(String(error));
5560
5574
  if (abortSignal?.aborted) {
5561
5575
  throw error;
5562
5576
  }
5563
5577
  omError("[OM] Reflection failed", error);
5564
5578
  } finally {
5565
5579
  await this.storage.setReflectingFlag(record.id, false);
5566
- reflectionHooks?.onReflectionEnd?.();
5580
+ reflectionHooks?.onReflectionEnd?.({ usage: reflectionUsage, error: reflectionError });
5567
5581
  unregisterOp(record.id, "reflecting");
5568
5582
  }
5569
5583
  }
@@ -8906,6 +8920,7 @@ ${grouped}` : grouped;
8906
8920
  const lockKey = this.buffering.getLockKey(threadId, resourceId);
8907
8921
  const reflectionHooks = hooks ? { onReflectionStart: hooks.onReflectionStart, onReflectionEnd: hooks.onReflectionEnd } : void 0;
8908
8922
  let observed = false;
8923
+ let observationUsage;
8909
8924
  let generationBefore = -1;
8910
8925
  await this.withLock(lockKey, async () => {
8911
8926
  const freshRecord = await this.getOrCreateRecord(threadId, resourceId);
@@ -8922,8 +8937,9 @@ ${grouped}` : grouped;
8922
8937
  return;
8923
8938
  }
8924
8939
  hooks?.onObservationStart?.();
8940
+ let observationError;
8925
8941
  try {
8926
- observed = await ObservationStrategy.create(this, {
8942
+ const result = await ObservationStrategy.create(this, {
8927
8943
  record: freshRecord,
8928
8944
  threadId,
8929
8945
  resourceId,
@@ -8933,8 +8949,13 @@ ${grouped}` : grouped;
8933
8949
  writer: opts.writer,
8934
8950
  observabilityContext: opts.observabilityContext
8935
8951
  }).run();
8952
+ observed = result.observed;
8953
+ observationUsage = result.usage;
8954
+ } catch (error) {
8955
+ observationError = error instanceof Error ? error : new Error(String(error));
8956
+ throw error;
8936
8957
  } finally {
8937
- hooks?.onObservationEnd?.();
8958
+ hooks?.onObservationEnd?.({ usage: observationUsage, error: observationError });
8938
8959
  }
8939
8960
  });
8940
8961
  const record = await this.getOrCreateRecord(threadId, resourceId);
@@ -8955,7 +8976,7 @@ ${grouped}` : grouped;
8955
8976
  async reflect(threadId, resourceId, prompt, requestContext, observabilityContext) {
8956
8977
  const record = await this.getOrCreateRecord(threadId, resourceId);
8957
8978
  if (!record.activeObservations) {
8958
- return { reflected: false, record };
8979
+ return { reflected: false, record, usage: void 0 };
8959
8980
  }
8960
8981
  await this.storage.setReflectingFlag(record.id, true);
8961
8982
  registerOp(record.id, "reflecting");
@@ -8980,11 +9001,11 @@ ${grouped}` : grouped;
8980
9001
  tokenCount: reflectionTokenCount
8981
9002
  });
8982
9003
  const updatedRecord = await this.getOrCreateRecord(threadId, resourceId);
8983
- return { reflected: true, record: updatedRecord };
9004
+ return { reflected: true, record: updatedRecord, usage: reflectResult.usage };
8984
9005
  } catch (error) {
8985
9006
  omError("[OM] reflect() failed", error);
8986
9007
  const latestRecord = await this.getOrCreateRecord(threadId, resourceId);
8987
- return { reflected: false, record: latestRecord };
9008
+ return { reflected: false, record: latestRecord, usage: void 0 };
8988
9009
  } finally {
8989
9010
  await this.storage.setReflectingFlag(record.id, false);
8990
9011
  unregisterOp(record.id, "reflecting");
@@ -9552,5 +9573,5 @@ function getObservationsAsOf(activeObservations, asOf) {
9552
9573
  }
9553
9574
 
9554
9575
  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 };
9555
- //# sourceMappingURL=chunk-U6BPUJR3.js.map
9556
- //# sourceMappingURL=chunk-U6BPUJR3.js.map
9576
+ //# sourceMappingURL=chunk-FLGIOJA5.js.map
9577
+ //# sourceMappingURL=chunk-FLGIOJA5.js.map