@mastra/observability 1.5.1-alpha.0 → 1.5.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.
package/dist/index.js CHANGED
@@ -17423,6 +17423,9 @@ var MetricsContextImpl = class {
17423
17423
  };
17424
17424
 
17425
17425
  // src/usage.ts
17426
+ function isDefined(value) {
17427
+ return value != null;
17428
+ }
17426
17429
  function extractUsageMetrics(usage, providerMetadata) {
17427
17430
  if (!usage) {
17428
17431
  return {};
@@ -17431,31 +17434,38 @@ function extractUsageMetrics(usage, providerMetadata) {
17431
17434
  const outputDetails = {};
17432
17435
  let inputTokens = usage.inputTokens;
17433
17436
  const outputTokens = usage.outputTokens;
17434
- if (usage.cachedInputTokens) {
17437
+ const aiSdkDetails = usage.inputTokenDetails;
17438
+ if (isDefined(aiSdkDetails?.cacheReadTokens)) {
17439
+ inputDetails.cacheRead = aiSdkDetails.cacheReadTokens;
17440
+ }
17441
+ if (isDefined(aiSdkDetails?.cacheWriteTokens)) {
17442
+ inputDetails.cacheWrite = aiSdkDetails.cacheWriteTokens;
17443
+ }
17444
+ if (!isDefined(inputDetails.cacheRead) && isDefined(usage.cachedInputTokens)) {
17435
17445
  inputDetails.cacheRead = usage.cachedInputTokens;
17436
17446
  }
17437
- if (usage.reasoningTokens) {
17447
+ if (isDefined(usage.reasoningTokens)) {
17438
17448
  outputDetails.reasoning = usage.reasoningTokens;
17439
17449
  }
17440
17450
  const anthropic = providerMetadata?.anthropic;
17441
17451
  if (anthropic) {
17442
- if (anthropic.cacheReadInputTokens) {
17452
+ if (!isDefined(inputDetails.cacheRead) && isDefined(anthropic.cacheReadInputTokens)) {
17443
17453
  inputDetails.cacheRead = anthropic.cacheReadInputTokens;
17444
17454
  }
17445
- if (anthropic.cacheCreationInputTokens) {
17455
+ if (!isDefined(inputDetails.cacheWrite) && isDefined(anthropic.cacheCreationInputTokens)) {
17446
17456
  inputDetails.cacheWrite = anthropic.cacheCreationInputTokens;
17447
17457
  }
17448
- if (anthropic.cacheReadInputTokens || anthropic.cacheCreationInputTokens) {
17458
+ if (isDefined(inputDetails.cacheRead) || isDefined(inputDetails.cacheWrite)) {
17449
17459
  inputDetails.text = usage.inputTokens;
17450
- inputTokens = (usage.inputTokens ?? 0) + (anthropic.cacheReadInputTokens ?? 0) + (anthropic.cacheCreationInputTokens ?? 0);
17460
+ inputTokens = (usage.inputTokens ?? 0) + (inputDetails.cacheRead ?? 0) + (inputDetails.cacheWrite ?? 0);
17451
17461
  }
17452
17462
  }
17453
17463
  const google = providerMetadata?.google;
17454
17464
  if (google?.usageMetadata) {
17455
- if (google.usageMetadata.cachedContentTokenCount) {
17465
+ if (!isDefined(inputDetails.cacheRead) && isDefined(google.usageMetadata.cachedContentTokenCount)) {
17456
17466
  inputDetails.cacheRead = google.usageMetadata.cachedContentTokenCount;
17457
17467
  }
17458
- if (google.usageMetadata.thoughtsTokenCount) {
17468
+ if (isDefined(google.usageMetadata.thoughtsTokenCount)) {
17459
17469
  outputDetails.reasoning = google.usageMetadata.thoughtsTokenCount;
17460
17470
  }
17461
17471
  }