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