@raindrop-ai/ai-sdk 0.3.0 → 0.3.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.
@@ -1,4 +1,4 @@
1
- // ../core/dist/chunk-YUIXRQWN.js
1
+ // ../core/dist/chunk-3V4MIA75.js
2
2
  function normalizeFeatureFlags(input) {
3
3
  if (Array.isArray(input)) {
4
4
  return Object.fromEntries(input.map((name) => [name, "true"]));
@@ -1130,6 +1130,7 @@ var EventShipper = class {
1130
1130
  }
1131
1131
  };
1132
1132
  var MODEL_USAGE_ATTRIBUTES = {
1133
+ usageSource: "raindrop.usage.source",
1133
1134
  providerName: "gen_ai.provider.name",
1134
1135
  requestModel: "gen_ai.request.model",
1135
1136
  responseModel: "gen_ai.response.model",
@@ -1138,20 +1139,22 @@ var MODEL_USAGE_ATTRIBUTES = {
1138
1139
  reasoningTokens: "gen_ai.usage.reasoning_tokens",
1139
1140
  cacheReadInputTokens: "gen_ai.usage.cache_read_input_tokens",
1140
1141
  cacheWriteInputTokens: "gen_ai.usage.cache_write_input_tokens",
1141
- nonCachedInputTokens: "raindrop.usage.input_tokens.non_cached",
1142
- nonReasoningOutputTokens: "raindrop.usage.output_tokens.non_reasoning",
1143
- reasoningOutputTokens: "raindrop.usage.output_tokens.reasoning",
1144
- cacheReadTokens: "raindrop.usage.input_tokens.cache_read",
1145
- cacheWriteTokens: "raindrop.usage.input_tokens.cache_write"
1146
- };
1142
+ providerReportedCost: "gen_ai.usage.provider_reported_cost"};
1147
1143
  var MODEL_PROVIDER_NAMES = [
1144
+ ["google.vertex.anthropic", "google-vertex"],
1145
+ ["google.vertex_ai", "google-vertex"],
1148
1146
  ["google.vertex", "google-vertex"],
1147
+ ["gcp.vertex_ai", "google-vertex"],
1148
+ ["gcp.gemini", "google"],
1149
+ ["google_vertexai", "google-vertex"],
1149
1150
  ["vertex.anthropic", "google-vertex"],
1150
1151
  ["amazon-bedrock", "amazon-bedrock"],
1152
+ ["aws.bedrock", "amazon-bedrock"],
1151
1153
  ["bedrock", "amazon-bedrock"],
1152
1154
  ["anthropic", "anthropic"],
1153
1155
  ["openai", "openai"],
1154
1156
  ["google", "google"],
1157
+ ["az.ai.openai", "azure"],
1155
1158
  ["azure", "azure"],
1156
1159
  ["openrouter", "openrouter"]
1157
1160
  ];
@@ -1162,60 +1165,20 @@ function canonicalModelProvider(provider) {
1162
1165
  const match = MODEL_PROVIDER_NAMES.find(
1163
1166
  ([family]) => normalizedProvider === family || normalizedProvider.startsWith(`${family}.`)
1164
1167
  );
1165
- return (_a = match == null ? void 0 : match[1]) != null ? _a : provider;
1168
+ return (_a = match == null ? void 0 : match[1]) != null ? _a : normalizedProvider;
1166
1169
  }
1167
1170
  function tokenCount(value) {
1168
1171
  return typeof value === "number" && Number.isSafeInteger(value) && value >= 0 ? value : void 0;
1169
1172
  }
1170
- function exclusiveTokens({
1171
- explicit,
1172
- total,
1173
- parts,
1174
- semantics
1175
- }) {
1176
- if (explicit !== void 0) return explicit;
1177
- if (total === void 0) return void 0;
1178
- if (semantics === "exclusive") return total;
1179
- const observedParts = parts.filter((part) => part !== void 0 && part > 0);
1180
- if (semantics === void 0 && observedParts.length > 0) return void 0;
1181
- const exclusive = total - observedParts.reduce((sum, part) => sum + part, 0);
1182
- return exclusive >= 0 ? exclusive : void 0;
1183
- }
1184
- function buildModelUsageAttributes(usage) {
1185
- const inputTokens = tokenCount(usage.inputTokens);
1186
- const outputTokens = tokenCount(usage.outputTokens);
1187
- const explicitNonCachedInputTokens = tokenCount(usage.nonCachedInputTokens);
1188
- const explicitNonReasoningOutputTokens = tokenCount(usage.nonReasoningOutputTokens);
1189
- const reasoningTokens = tokenCount(usage.reasoningTokens);
1190
- const cacheReadInputTokens = tokenCount(usage.cacheReadInputTokens);
1191
- const cacheWriteInputTokens = tokenCount(usage.cacheWriteInputTokens);
1192
- const nonCachedInputTokens = exclusiveTokens({
1193
- explicit: explicitNonCachedInputTokens,
1194
- total: inputTokens,
1195
- parts: [cacheReadInputTokens, cacheWriteInputTokens],
1196
- semantics: usage.inputTokenSemantics
1197
- });
1198
- const nonReasoningOutputTokens = exclusiveTokens({
1199
- explicit: explicitNonReasoningOutputTokens,
1200
- total: outputTokens,
1201
- parts: [reasoningTokens],
1202
- semantics: usage.outputTokenSemantics
1203
- });
1204
- const exclusivePoolAttributes = nonCachedInputTokens !== void 0 && nonReasoningOutputTokens !== void 0 ? [
1205
- // backend consumes this complete raindrop.* set as one atomic billing-pool contract.
1206
- attrInt(MODEL_USAGE_ATTRIBUTES.nonCachedInputTokens, nonCachedInputTokens),
1207
- attrInt(MODEL_USAGE_ATTRIBUTES.nonReasoningOutputTokens, nonReasoningOutputTokens),
1208
- attrInt(MODEL_USAGE_ATTRIBUTES.reasoningOutputTokens, reasoningTokens),
1209
- attrInt(MODEL_USAGE_ATTRIBUTES.cacheReadTokens, cacheReadInputTokens),
1210
- attrInt(MODEL_USAGE_ATTRIBUTES.cacheWriteTokens, cacheWriteInputTokens)
1211
- ] : [];
1173
+ function buildRawModelUsageAttributes(usage) {
1174
+ const providerReportedCost = typeof usage.providerReportedCost === "number" && Number.isFinite(usage.providerReportedCost) && usage.providerReportedCost >= 0 ? usage.providerReportedCost : void 0;
1212
1175
  return [
1213
- attrInt(MODEL_USAGE_ATTRIBUTES.inputTokens, inputTokens),
1214
- attrInt(MODEL_USAGE_ATTRIBUTES.outputTokens, outputTokens),
1215
- attrInt(MODEL_USAGE_ATTRIBUTES.reasoningTokens, reasoningTokens),
1216
- attrInt(MODEL_USAGE_ATTRIBUTES.cacheReadInputTokens, cacheReadInputTokens),
1217
- attrInt(MODEL_USAGE_ATTRIBUTES.cacheWriteInputTokens, cacheWriteInputTokens),
1218
- ...exclusivePoolAttributes
1176
+ attrInt(MODEL_USAGE_ATTRIBUTES.inputTokens, tokenCount(usage.inputTokens)),
1177
+ attrInt(MODEL_USAGE_ATTRIBUTES.outputTokens, tokenCount(usage.outputTokens)),
1178
+ attrInt(MODEL_USAGE_ATTRIBUTES.reasoningTokens, tokenCount(usage.reasoningTokens)),
1179
+ attrInt(MODEL_USAGE_ATTRIBUTES.cacheReadInputTokens, tokenCount(usage.cacheReadInputTokens)),
1180
+ attrInt(MODEL_USAGE_ATTRIBUTES.cacheWriteInputTokens, tokenCount(usage.cacheWriteInputTokens)),
1181
+ attrDouble(MODEL_USAGE_ATTRIBUTES.providerReportedCost, providerReportedCost)
1219
1182
  ].filter((attribute) => attribute !== void 0);
1220
1183
  }
1221
1184
  var STRING_ALIASES = {
@@ -1853,7 +1816,7 @@ async function* asyncGeneratorWithCurrent(span, gen) {
1853
1816
  // package.json
1854
1817
  var package_default = {
1855
1818
  name: "@raindrop-ai/ai-sdk",
1856
- version: "0.3.0"};
1819
+ version: "0.3.1"};
1857
1820
 
1858
1821
  // src/internal/version.ts
1859
1822
  var libraryName = package_default.name;
@@ -3097,29 +3060,6 @@ function firstTokenCount(...values) {
3097
3060
  }
3098
3061
  return void 0;
3099
3062
  }
3100
- function flatUsageSemantics(provider) {
3101
- const normalizedProvider = typeof provider === "string" ? provider.trim().toLowerCase() : void 0;
3102
- if (normalizedProvider === "anthropic" || (normalizedProvider == null ? void 0 : normalizedProvider.startsWith("anthropic.")) || normalizedProvider === "vertex.anthropic" || (normalizedProvider == null ? void 0 : normalizedProvider.startsWith("vertex.anthropic."))) {
3103
- return {
3104
- inputTokenSemantics: "exclusive",
3105
- outputTokenSemantics: "total"
3106
- };
3107
- }
3108
- if (normalizedProvider === "openai" || (normalizedProvider == null ? void 0 : normalizedProvider.startsWith("openai.")) || normalizedProvider === "azure" || (normalizedProvider == null ? void 0 : normalizedProvider.startsWith("azure."))) {
3109
- return {
3110
- inputTokenSemantics: "total",
3111
- outputTokenSemantics: "total"
3112
- };
3113
- }
3114
- if (normalizedProvider === "google" || (normalizedProvider == null ? void 0 : normalizedProvider.startsWith("google."))) {
3115
- return {
3116
- inputTokenSemantics: "total",
3117
- // Google v5 reports candidate output and thoughts separately.
3118
- outputTokenSemantics: "exclusive"
3119
- };
3120
- }
3121
- return {};
3122
- }
3123
3063
  var CACHE_WRITE_METADATA_KEYS = [
3124
3064
  ["anthropic", "cacheCreationInputTokens"],
3125
3065
  ["openai", "cacheWriteTokens"],
@@ -3136,7 +3076,7 @@ function cacheWriteTokensFromProviderMetadata(providerMetadata) {
3136
3076
  })
3137
3077
  );
3138
3078
  }
3139
- function extractUsageMetricsFromUsage(usage, provider, providerMetadata) {
3079
+ function extractUsageMetricsFromUsage(usage, _provider, providerMetadata) {
3140
3080
  if (!isRecord(usage)) return {};
3141
3081
  const inputTokenValue = usage["inputTokens"];
3142
3082
  const outputTokenValue = usage["outputTokens"];
@@ -3204,7 +3144,7 @@ function extractUsageMetricsFromUsage(usage, provider, providerMetadata) {
3204
3144
  cacheReadInputTokens,
3205
3145
  cachedInputTokens: cacheReadInputTokens,
3206
3146
  cacheWriteInputTokens,
3207
- ...hasStructuredUsage ? {} : flatUsageSemantics(provider)
3147
+ ...hasStructuredUsage ? { usageSource: "ai-sdk-structured" } : {}
3208
3148
  };
3209
3149
  }
3210
3150
 
@@ -3556,7 +3496,8 @@ var RaindropTelemetryIntegration = class {
3556
3496
  attrInt("ai.usage.totalTokens", usageMetrics.totalTokens),
3557
3497
  attrInt("ai.usage.reasoningTokens", usageMetrics.reasoningTokens),
3558
3498
  attrInt("ai.usage.cachedInputTokens", usageMetrics.cachedInputTokens),
3559
- ...buildModelUsageAttributes(usageMetrics)
3499
+ attrString(MODEL_USAGE_ATTRIBUTES.usageSource, usageMetrics.usageSource),
3500
+ ...buildRawModelUsageAttributes(usageMetrics)
3560
3501
  );
3561
3502
  }
3562
3503
  this.emitProviderExecutedToolSpans(event, state);
@@ -4504,9 +4445,7 @@ function extractRaindropMetadata(metadata) {
4504
4445
  } catch (e) {
4505
4446
  }
4506
4447
  } else if (featureFlags && typeof featureFlags === "object") {
4507
- result.featureFlags = normalizeFeatureFlags(
4508
- featureFlags
4509
- );
4448
+ result.featureFlags = normalizeFeatureFlags(featureFlags);
4510
4449
  }
4511
4450
  return result;
4512
4451
  }
@@ -4717,12 +4656,7 @@ function setupOperation(params) {
4717
4656
  ...attrsFromTelemetryMetadata(telemetry == null ? void 0 : telemetry.metadata),
4718
4657
  ...attrsFromHeaders(isRecord(arg) ? arg["headers"] : void 0),
4719
4658
  ...attrsFromSettings(arg),
4720
- ...(telemetry == null ? void 0 : telemetry.recordInputs) === false ? [] : [
4721
- attrString(
4722
- "ai.prompt",
4723
- safeJsonWithUint8(normalizePromptAttr(arg))
4724
- )
4725
- ]
4659
+ ...(telemetry == null ? void 0 : telemetry.recordInputs) === false ? [] : [attrString("ai.prompt", safeJsonWithUint8(normalizePromptAttr(arg)))]
4726
4660
  ]
4727
4661
  }) : void 0;
4728
4662
  const rootParentForChildren = rootSpan ? { traceIdB64: rootSpan.ids.traceIdB64, spanIdB64: rootSpan.ids.spanIdB64 } : inheritedParent;
@@ -5072,7 +5006,10 @@ function wrapAISDK(aiSDK, deps) {
5072
5006
  }
5073
5007
  const useNative = deps.options.nativeTelemetry === true;
5074
5008
  if (useNative) {
5075
- const wrapTimeCtx = resolveContext(deps.options.context, { operation: "wrap", args: void 0 });
5009
+ const wrapTimeCtx = resolveContext(deps.options.context, {
5010
+ operation: "wrap",
5011
+ args: void 0
5012
+ });
5076
5013
  const integration = new RaindropTelemetryIntegration({
5077
5014
  traceShipper: deps.traceShipper,
5078
5015
  eventShipper: deps.eventShipper,
@@ -5317,7 +5254,13 @@ function wrapAgentGenerate(generate, instance, agentSettings, className, aiSDK,
5317
5254
  ...(telemetry == null ? void 0 : telemetry.recordInputs) === false ? [] : [
5318
5255
  attrString(
5319
5256
  "ai.prompt",
5320
- safeJsonWithUint8(normalizePromptAttr({ system: (_d = mergedArgs["system"]) != null ? _d : mergedArgs["instructions"], prompt: mergedArgs["prompt"], messages: mergedArgs["messages"] }))
5257
+ safeJsonWithUint8(
5258
+ normalizePromptAttr({
5259
+ system: (_d = mergedArgs["system"]) != null ? _d : mergedArgs["instructions"],
5260
+ prompt: mergedArgs["prompt"],
5261
+ messages: mergedArgs["messages"]
5262
+ })
5263
+ )
5321
5264
  )
5322
5265
  ]
5323
5266
  ]
@@ -5531,7 +5474,13 @@ function wrapAgentStream(stream, instance, agentSettings, className, aiSDK, deps
5531
5474
  ...(telemetry == null ? void 0 : telemetry.recordInputs) === false ? [] : [
5532
5475
  attrString(
5533
5476
  "ai.prompt",
5534
- safeJsonWithUint8(normalizePromptAttr({ system: (_d = mergedArgs["system"]) != null ? _d : mergedArgs["instructions"], prompt: mergedArgs["prompt"], messages: mergedArgs["messages"] }))
5477
+ safeJsonWithUint8(
5478
+ normalizePromptAttr({
5479
+ system: (_d = mergedArgs["system"]) != null ? _d : mergedArgs["instructions"],
5480
+ prompt: mergedArgs["prompt"],
5481
+ messages: mergedArgs["messages"]
5482
+ })
5483
+ )
5535
5484
  )
5536
5485
  ]
5537
5486
  ]
@@ -5908,9 +5857,19 @@ function wrapModel(args, aiSDK, outerOperationId, ctx) {
5908
5857
  for (const part of content) {
5909
5858
  if (isRecord(part)) {
5910
5859
  if ((part["type"] === "reasoning" || part["type"] === "thinking") && typeof ((_a = part["text"]) != null ? _a : part["thinking"]) === "string") {
5911
- sendLocalDebuggerLiveEvent({ traceId, type: "reasoning_delta", content: (_b = part["text"]) != null ? _b : part["thinking"], metadata: liveMeta });
5860
+ sendLocalDebuggerLiveEvent({
5861
+ traceId,
5862
+ type: "reasoning_delta",
5863
+ content: (_b = part["text"]) != null ? _b : part["thinking"],
5864
+ metadata: liveMeta
5865
+ });
5912
5866
  } else if (part["type"] === "text" && typeof part["text"] === "string") {
5913
- sendLocalDebuggerLiveEvent({ traceId, type: "text_delta", content: part["text"], metadata: liveMeta });
5867
+ sendLocalDebuggerLiveEvent({
5868
+ traceId,
5869
+ type: "text_delta",
5870
+ content: part["text"],
5871
+ metadata: liveMeta
5872
+ });
5914
5873
  }
5915
5874
  }
5916
5875
  }
@@ -5991,7 +5950,10 @@ function wrapModel(args, aiSDK, outerOperationId, ctx) {
5991
5950
  attributes: [
5992
5951
  ...((_a = ctx.telemetry) == null ? void 0 : _a.recordOutputs) === false ? [] : [
5993
5952
  attrString("ai.response.finishReason", finishReason),
5994
- attrString("ai.response.text", activeText.length ? capText2(activeText) : void 0),
5953
+ attrString(
5954
+ "ai.response.text",
5955
+ activeText.length ? capText2(activeText) : void 0
5956
+ ),
5995
5957
  attrString(
5996
5958
  "ai.response.toolCalls",
5997
5959
  safeJsonWithUint8(toolCallsLocal.length ? toolCallsLocal : void 0)
@@ -6009,7 +5971,8 @@ function wrapModel(args, aiSDK, outerOperationId, ctx) {
6009
5971
  ...finishReason ? [attrStringArray("gen_ai.response.finish_reasons", [finishReason])] : [],
6010
5972
  attrString("gen_ai.response.id", responseId),
6011
5973
  attrString(MODEL_USAGE_ATTRIBUTES.responseModel, finalResponseModelId),
6012
- ...buildModelUsageAttributes(usageMetrics),
5974
+ attrString(MODEL_USAGE_ATTRIBUTES.usageSource, usageMetrics.usageSource),
5975
+ ...buildRawModelUsageAttributes(usageMetrics),
6013
5976
  ...msToFirstChunk !== void 0 ? [attrInt("ai.stream.msToFirstChunk", msToFirstChunk)] : [],
6014
5977
  ...msToFinish !== void 0 ? [attrInt("ai.stream.msToFinish", msToFinish)] : [],
6015
5978
  ...avgOutTokensPerSecond !== void 0 ? [attrDouble("ai.stream.avgOutputTokensPerSecond", avgOutTokensPerSecond)] : [],
@@ -6063,19 +6026,44 @@ function wrapModel(args, aiSDK, outerOperationId, ctx) {
6063
6026
  if ("providerMetadata" in value) providerMetadata = value["providerMetadata"];
6064
6027
  if (localDebuggerEnabled() && ctx.rootParentForChildren) {
6065
6028
  const traceId = ctx.rootParentForChildren.traceIdB64;
6066
- const liveMeta = getLocalDebuggerMetadata({ ...ctx.context, eventId: ctx.eventId });
6029
+ const liveMeta = getLocalDebuggerMetadata({
6030
+ ...ctx.context,
6031
+ eventId: ctx.eventId
6032
+ });
6067
6033
  if (type === "text-delta") {
6068
6034
  const text = typeof value["delta"] === "string" ? value["delta"] : typeof value["textDelta"] === "string" ? value["textDelta"] : typeof value["text"] === "string" ? value["text"] : void 0;
6069
- if (typeof text === "string" && text) sendLocalDebuggerLiveEvent({ traceId, type: "text_delta", content: text, metadata: liveMeta });
6035
+ if (typeof text === "string" && text)
6036
+ sendLocalDebuggerLiveEvent({
6037
+ traceId,
6038
+ type: "text_delta",
6039
+ content: text,
6040
+ metadata: liveMeta
6041
+ });
6070
6042
  } else if (type === "reasoning" || type === "reasoning-delta") {
6071
6043
  const text = typeof value["delta"] === "string" ? value["delta"] : typeof value["text"] === "string" ? value["text"] : typeof value["thinking"] === "string" ? value["thinking"] : void 0;
6072
- if (typeof text === "string" && text) sendLocalDebuggerLiveEvent({ traceId, type: "reasoning_delta", content: text, metadata: liveMeta });
6044
+ if (typeof text === "string" && text)
6045
+ sendLocalDebuggerLiveEvent({
6046
+ traceId,
6047
+ type: "reasoning_delta",
6048
+ content: text,
6049
+ metadata: liveMeta
6050
+ });
6073
6051
  } else if (type === "tool-call") {
6074
6052
  const toolName = typeof value["toolName"] === "string" ? value["toolName"] : "tool";
6075
- sendLocalDebuggerLiveEvent({ traceId, type: "tool_start", content: toolName, metadata: { ...liveMeta, args: value["args"] } });
6053
+ sendLocalDebuggerLiveEvent({
6054
+ traceId,
6055
+ type: "tool_start",
6056
+ content: toolName,
6057
+ metadata: { ...liveMeta, args: value["args"] }
6058
+ });
6076
6059
  } else if (type === "tool-result") {
6077
6060
  const toolName = typeof value["toolName"] === "string" ? value["toolName"] : "tool";
6078
- sendLocalDebuggerLiveEvent({ traceId, type: "tool_result", content: toolName, metadata: { ...liveMeta, result: value["result"] } });
6061
+ sendLocalDebuggerLiveEvent({
6062
+ traceId,
6063
+ type: "tool_result",
6064
+ content: toolName,
6065
+ metadata: { ...liveMeta, result: value["result"] }
6066
+ });
6079
6067
  }
6080
6068
  }
6081
6069
  }
@@ -6155,11 +6143,7 @@ function endDoGenerateSpan(span, result, modelInfo, ctx) {
6155
6143
  } else {
6156
6144
  responseTimestampIso = (/* @__PURE__ */ new Date()).toISOString();
6157
6145
  }
6158
- const usageMetrics = extractUsageMetricsFromUsage(
6159
- usage,
6160
- modelInfo.provider,
6161
- providerMetadata
6162
- );
6146
+ const usageMetrics = extractUsageMetricsFromUsage(usage, modelInfo.provider, providerMetadata);
6163
6147
  const { inputTokens, outputTokens } = usageMetrics;
6164
6148
  ctx.traceShipper.endSpan(span, {
6165
6149
  attributes: [
@@ -6180,7 +6164,8 @@ function endDoGenerateSpan(span, result, modelInfo, ctx) {
6180
6164
  ...finishReason ? [attrStringArray("gen_ai.response.finish_reasons", [finishReason])] : [],
6181
6165
  attrString("gen_ai.response.id", responseId),
6182
6166
  attrString(MODEL_USAGE_ATTRIBUTES.responseModel, responseModelId),
6183
- ...buildModelUsageAttributes(usageMetrics)
6167
+ attrString(MODEL_USAGE_ATTRIBUTES.usageSource, usageMetrics.usageSource),
6168
+ ...buildRawModelUsageAttributes(usageMetrics)
6184
6169
  ]
6185
6170
  });
6186
6171
  }