@mastra/observability 1.9.1-alpha.1 → 1.9.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
@@ -18085,6 +18085,7 @@ var PricingRegistry = class _PricingRegistry {
18085
18085
  constructor(pricingModels) {
18086
18086
  this.pricingModels = pricingModels;
18087
18087
  }
18088
+ pricingModels;
18088
18089
  static globalRegistry = null;
18089
18090
  static fromText(pricingModelText) {
18090
18091
  return new _PricingRegistry(parsePricingModelText(pricingModelText));
@@ -18101,12 +18102,11 @@ var PricingRegistry = class _PricingRegistry {
18101
18102
  return _PricingRegistry.globalRegistry;
18102
18103
  }
18103
18104
  get(args) {
18104
- const key = makePricingKey(args);
18105
- const exact = this.pricingModels.get(key);
18106
- if (exact) return exact;
18107
- const dashedKey = makePricingKey({ provider: args.provider, model: args.model.replace(/\./g, "-") });
18108
- if (dashedKey !== key) {
18109
- return this.pricingModels.get(dashedKey) ?? null;
18105
+ const variants = getModelVariants(args.model);
18106
+ for (const variant of variants) {
18107
+ const key = makePricingKey({ provider: args.provider, model: variant });
18108
+ const match = this.pricingModels.get(key);
18109
+ if (match) return match;
18110
18110
  }
18111
18111
  return null;
18112
18112
  }
@@ -18196,6 +18196,18 @@ function normalizeProvider(provider) {
18196
18196
  const dotIndex = normalized.indexOf(".");
18197
18197
  return dotIndex !== -1 ? normalized.substring(0, dotIndex) : normalized;
18198
18198
  }
18199
+ function getModelVariants(model) {
18200
+ const dashed = model.replace(/\./g, "-");
18201
+ return [model, dashed, stripDateSuffix(model), stripDateSuffix(dashed)];
18202
+ }
18203
+ function stripDateSuffix(model) {
18204
+ let stripped = model.replace(/-20\d{2}-\d{2}-\d{2}$/, "");
18205
+ if (stripped !== model) return stripped;
18206
+ stripped = model.replace(/-20\d{6}(-[a-z]+)?$/, "$1");
18207
+ if (stripped !== model) return stripped;
18208
+ stripped = model.replace(/-\d{2}-20\d{2}$/, "");
18209
+ return stripped;
18210
+ }
18199
18211
 
18200
18212
  // src/metrics/types.ts
18201
18213
  var PricingMeter = {
@@ -18626,15 +18638,150 @@ function sumDefinedValues(obj, keys) {
18626
18638
  }
18627
18639
 
18628
18640
  // src/model-tracing.ts
18641
+ function formatPreviewLabel(label, fallback) {
18642
+ return typeof label === "string" && label.length > 0 ? label : fallback;
18643
+ }
18644
+ function summarizePart(part) {
18645
+ if (typeof part === "string") {
18646
+ return part;
18647
+ }
18648
+ if (!part || typeof part !== "object") {
18649
+ return "";
18650
+ }
18651
+ if ("text" in part && typeof part.text === "string") {
18652
+ return part.text;
18653
+ }
18654
+ if ("parts" in part && Array.isArray(part.parts)) {
18655
+ return part.parts.map(summarizePart).filter(Boolean).join("");
18656
+ }
18657
+ if ("inlineData" in part && part.inlineData && typeof part.inlineData === "object") {
18658
+ return `[${formatPreviewLabel(part.inlineData.mimeType, "binary")}]`;
18659
+ }
18660
+ if ("image_url" in part) {
18661
+ return "[image]";
18662
+ }
18663
+ if ("functionCall" in part && part.functionCall && typeof part.functionCall === "object") {
18664
+ return `[tool: ${formatPreviewLabel(part.functionCall.name, "unknown")}]`;
18665
+ }
18666
+ if ("function_call" in part && part.function_call && typeof part.function_call === "object") {
18667
+ return `[tool: ${formatPreviewLabel(part.function_call.name, "unknown")}]`;
18668
+ }
18669
+ if ("function" in part && part.function && typeof part.function === "object") {
18670
+ return `[tool: ${formatPreviewLabel(part.function.name, "unknown")}]`;
18671
+ }
18672
+ if ("toolName" in part) {
18673
+ return `[tool: ${formatPreviewLabel(part.toolName, "unknown")}]`;
18674
+ }
18675
+ if ("type" in part && typeof part.type === "string") {
18676
+ switch (part.type) {
18677
+ case "image":
18678
+ return "[image]";
18679
+ case "file":
18680
+ return "[file]";
18681
+ case "reasoning":
18682
+ return "[reasoning]";
18683
+ case "tool-call":
18684
+ return `[tool: ${formatPreviewLabel(part.toolName, "unknown")}]`;
18685
+ case "tool-result":
18686
+ return "[tool-result]";
18687
+ default:
18688
+ return `[${part.type}]`;
18689
+ }
18690
+ }
18691
+ if ("content" in part && typeof part.content === "string") {
18692
+ return part.content;
18693
+ }
18694
+ return "[object]";
18695
+ }
18696
+ function summarizeMessageContent(content) {
18697
+ if (typeof content === "string") {
18698
+ return content;
18699
+ }
18700
+ if (Array.isArray(content)) {
18701
+ return content.map(summarizePart).filter(Boolean).join("");
18702
+ }
18703
+ if (content && typeof content === "object") {
18704
+ if ("parts" in content && Array.isArray(content.parts)) {
18705
+ return content.parts.map(summarizePart).filter(Boolean).join("");
18706
+ }
18707
+ return summarizePart(content);
18708
+ }
18709
+ if (content == null) {
18710
+ return "";
18711
+ }
18712
+ return String(content);
18713
+ }
18714
+ function appendToolPreview(preview, toolCalls) {
18715
+ if (!Array.isArray(toolCalls) || toolCalls.length === 0) {
18716
+ return preview;
18717
+ }
18718
+ const toolPreview = toolCalls.map((toolCall) => summarizePart(toolCall)).filter(Boolean).join(" ");
18719
+ if (!toolPreview) {
18720
+ return preview;
18721
+ }
18722
+ return preview ? `${preview} ${toolPreview}` : toolPreview;
18723
+ }
18724
+ function appendPreview(preview, addition) {
18725
+ if (!addition) {
18726
+ return preview;
18727
+ }
18728
+ return preview ? `${preview} ${addition}` : addition;
18729
+ }
18730
+ function normalizeMessages(messages) {
18731
+ return messages.map((message) => {
18732
+ if (!message || typeof message !== "object") {
18733
+ return { role: "user", content: summarizeMessageContent(message) };
18734
+ }
18735
+ const role = typeof message.role === "string" ? message.role : "user";
18736
+ const baseContent = summarizeMessageContent(message.content);
18737
+ const contentWithToolArrays = appendToolPreview(
18738
+ appendToolPreview(baseContent, message.toolCalls),
18739
+ message.tool_calls
18740
+ );
18741
+ const functionCall = message.functionCall;
18742
+ const functionCallPreview = functionCall === void 0 ? "" : summarizePart({ functionCall });
18743
+ const functionCallSnakeCase = message.function_call;
18744
+ const functionCallSnakeCasePreview = functionCallSnakeCase === void 0 ? "" : summarizePart({ function_call: functionCallSnakeCase });
18745
+ const contentWithFunctionCall = appendPreview(
18746
+ appendPreview(contentWithToolArrays, functionCallPreview),
18747
+ functionCallSnakeCasePreview
18748
+ );
18749
+ return { role, content: contentWithFunctionCall };
18750
+ });
18751
+ }
18752
+ function summarizeRequestBody(body) {
18753
+ if (body == null) {
18754
+ return void 0;
18755
+ }
18756
+ if (typeof body !== "object") {
18757
+ return typeof body === "string" ? body : String(body);
18758
+ }
18759
+ if (Array.isArray(body.messages)) {
18760
+ return normalizeMessages(body.messages);
18761
+ }
18762
+ if (Array.isArray(body.contents)) {
18763
+ return body.contents.map((item) => ({
18764
+ role: typeof item?.role === "string" ? item.role : "user",
18765
+ content: Array.isArray(item?.parts) ? item.parts.map(summarizePart).filter(Boolean).join("") : ""
18766
+ }));
18767
+ }
18768
+ const summary = {};
18769
+ if (typeof body.model === "string") {
18770
+ summary.model = body.model;
18771
+ }
18772
+ const bodyKeys = Object.keys(body).filter((key) => key !== "body");
18773
+ if (bodyKeys.length > 0) {
18774
+ summary.keys = bodyKeys;
18775
+ }
18776
+ return Object.keys(summary).length > 0 ? summary : "[request body]";
18777
+ }
18629
18778
  function extractStepInput(request) {
18630
18779
  if (!request) return void 0;
18631
18780
  const { body } = request;
18632
18781
  if (body == null) return request;
18633
18782
  try {
18634
18783
  const parsed = typeof body === "string" ? JSON.parse(body) : body;
18635
- if (Array.isArray(parsed?.messages)) return parsed.messages;
18636
- if (Array.isArray(parsed?.contents)) return parsed.contents;
18637
- return parsed;
18784
+ return summarizeRequestBody(parsed);
18638
18785
  } catch {
18639
18786
  return request;
18640
18787
  }
@@ -19221,6 +19368,10 @@ var BaseSpan = class {
19221
19368
  }
19222
19369
  const metadata = this.metadata ?? {};
19223
19370
  const getMetadataString = (key) => typeof metadata[key] === "string" ? metadata[key] : void 0;
19371
+ const getSpanMetadataString = (span, key) => {
19372
+ const m = span?.metadata;
19373
+ return m && typeof m[key] === "string" ? m[key] : void 0;
19374
+ };
19224
19375
  const parentSpan = this.getParentSpan(false);
19225
19376
  let rootSpan = this;
19226
19377
  while (rootSpan.parent) {
@@ -19234,12 +19385,15 @@ var BaseSpan = class {
19234
19385
  entityType: this.entityType,
19235
19386
  entityId: this.entityId,
19236
19387
  entityName: this.entityName,
19388
+ entityVersionId: getMetadataString("entityVersionId"),
19237
19389
  parentEntityType: parentSpan?.entityType,
19238
19390
  parentEntityId: parentSpan?.entityId,
19239
19391
  parentEntityName: parentSpan?.entityName,
19392
+ parentEntityVersionId: getSpanMetadataString(parentSpan, "entityVersionId"),
19240
19393
  rootEntityType: rootSpan.entityType,
19241
19394
  rootEntityId: rootSpan.entityId,
19242
19395
  rootEntityName: rootSpan.entityName,
19396
+ rootEntityVersionId: getSpanMetadataString(rootSpan, "entityVersionId"),
19243
19397
  userId: getMetadataString("userId"),
19244
19398
  organizationId: getMetadataString("organizationId"),
19245
19399
  resourceId: getMetadataString("resourceId"),
@@ -19376,7 +19530,10 @@ var DefaultSpan = class extends BaseSpan {
19376
19530
  domain: error48.domain,
19377
19531
  message: error48.message,
19378
19532
  name: error48.name,
19379
- stack: error48.stack
19533
+ // Prefer the original cause's stack when available. MastraError wraps
19534
+ // thrown errors, so its own stack points to the wrapping site rather
19535
+ // than where the underlying error was thrown.
19536
+ stack: error48.cause instanceof Error && error48.cause.stack || error48.stack
19380
19537
  } : {
19381
19538
  message: error48.message,
19382
19539
  name: error48.name,