@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
  import { AsyncLocalStorage } from 'async_hooks';
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 = {
@@ -3103,29 +3066,6 @@ function firstTokenCount(...values) {
3103
3066
  }
3104
3067
  return void 0;
3105
3068
  }
3106
- function flatUsageSemantics(provider) {
3107
- const normalizedProvider = typeof provider === "string" ? provider.trim().toLowerCase() : void 0;
3108
- if (normalizedProvider === "anthropic" || (normalizedProvider == null ? void 0 : normalizedProvider.startsWith("anthropic.")) || normalizedProvider === "vertex.anthropic" || (normalizedProvider == null ? void 0 : normalizedProvider.startsWith("vertex.anthropic."))) {
3109
- return {
3110
- inputTokenSemantics: "exclusive",
3111
- outputTokenSemantics: "total"
3112
- };
3113
- }
3114
- if (normalizedProvider === "openai" || (normalizedProvider == null ? void 0 : normalizedProvider.startsWith("openai.")) || normalizedProvider === "azure" || (normalizedProvider == null ? void 0 : normalizedProvider.startsWith("azure."))) {
3115
- return {
3116
- inputTokenSemantics: "total",
3117
- outputTokenSemantics: "total"
3118
- };
3119
- }
3120
- if (normalizedProvider === "google" || (normalizedProvider == null ? void 0 : normalizedProvider.startsWith("google."))) {
3121
- return {
3122
- inputTokenSemantics: "total",
3123
- // Google v5 reports candidate output and thoughts separately.
3124
- outputTokenSemantics: "exclusive"
3125
- };
3126
- }
3127
- return {};
3128
- }
3129
3069
  var CACHE_WRITE_METADATA_KEYS = [
3130
3070
  ["anthropic", "cacheCreationInputTokens"],
3131
3071
  ["openai", "cacheWriteTokens"],
@@ -3142,7 +3082,7 @@ function cacheWriteTokensFromProviderMetadata(providerMetadata) {
3142
3082
  })
3143
3083
  );
3144
3084
  }
3145
- function extractUsageMetricsFromUsage(usage, provider, providerMetadata) {
3085
+ function extractUsageMetricsFromUsage(usage, _provider, providerMetadata) {
3146
3086
  if (!isRecord(usage)) return {};
3147
3087
  const inputTokenValue = usage["inputTokens"];
3148
3088
  const outputTokenValue = usage["outputTokens"];
@@ -3210,7 +3150,7 @@ function extractUsageMetricsFromUsage(usage, provider, providerMetadata) {
3210
3150
  cacheReadInputTokens,
3211
3151
  cachedInputTokens: cacheReadInputTokens,
3212
3152
  cacheWriteInputTokens,
3213
- ...hasStructuredUsage ? {} : flatUsageSemantics(provider)
3153
+ ...hasStructuredUsage ? { usageSource: "ai-sdk-structured" } : {}
3214
3154
  };
3215
3155
  }
3216
3156
 
@@ -3562,7 +3502,8 @@ var RaindropTelemetryIntegration = class {
3562
3502
  attrInt("ai.usage.totalTokens", usageMetrics.totalTokens),
3563
3503
  attrInt("ai.usage.reasoningTokens", usageMetrics.reasoningTokens),
3564
3504
  attrInt("ai.usage.cachedInputTokens", usageMetrics.cachedInputTokens),
3565
- ...buildModelUsageAttributes(usageMetrics)
3505
+ attrString(MODEL_USAGE_ATTRIBUTES.usageSource, usageMetrics.usageSource),
3506
+ ...buildRawModelUsageAttributes(usageMetrics)
3566
3507
  );
3567
3508
  }
3568
3509
  this.emitProviderExecutedToolSpans(event, state);
@@ -4497,9 +4438,7 @@ function extractRaindropMetadata(metadata) {
4497
4438
  } catch (e) {
4498
4439
  }
4499
4440
  } else if (featureFlags && typeof featureFlags === "object") {
4500
- result.featureFlags = normalizeFeatureFlags(
4501
- featureFlags
4502
- );
4441
+ result.featureFlags = normalizeFeatureFlags(featureFlags);
4503
4442
  }
4504
4443
  return result;
4505
4444
  }
@@ -4710,12 +4649,7 @@ function setupOperation(params) {
4710
4649
  ...attrsFromTelemetryMetadata(telemetry == null ? void 0 : telemetry.metadata),
4711
4650
  ...attrsFromHeaders(isRecord(arg) ? arg["headers"] : void 0),
4712
4651
  ...attrsFromSettings(arg),
4713
- ...(telemetry == null ? void 0 : telemetry.recordInputs) === false ? [] : [
4714
- attrString(
4715
- "ai.prompt",
4716
- safeJsonWithUint8(normalizePromptAttr(arg))
4717
- )
4718
- ]
4652
+ ...(telemetry == null ? void 0 : telemetry.recordInputs) === false ? [] : [attrString("ai.prompt", safeJsonWithUint8(normalizePromptAttr(arg)))]
4719
4653
  ]
4720
4654
  }) : void 0;
4721
4655
  const rootParentForChildren = rootSpan ? { traceIdB64: rootSpan.ids.traceIdB64, spanIdB64: rootSpan.ids.spanIdB64 } : inheritedParent;
@@ -5065,7 +4999,10 @@ function wrapAISDK(aiSDK, deps) {
5065
4999
  }
5066
5000
  const useNative = deps.options.nativeTelemetry === true;
5067
5001
  if (useNative) {
5068
- const wrapTimeCtx = resolveContext(deps.options.context, { operation: "wrap", args: void 0 });
5002
+ const wrapTimeCtx = resolveContext(deps.options.context, {
5003
+ operation: "wrap",
5004
+ args: void 0
5005
+ });
5069
5006
  const integration = new RaindropTelemetryIntegration({
5070
5007
  traceShipper: deps.traceShipper,
5071
5008
  eventShipper: deps.eventShipper,
@@ -5310,7 +5247,13 @@ function wrapAgentGenerate(generate, instance, agentSettings, className, aiSDK,
5310
5247
  ...(telemetry == null ? void 0 : telemetry.recordInputs) === false ? [] : [
5311
5248
  attrString(
5312
5249
  "ai.prompt",
5313
- safeJsonWithUint8(normalizePromptAttr({ system: (_d = mergedArgs["system"]) != null ? _d : mergedArgs["instructions"], prompt: mergedArgs["prompt"], messages: mergedArgs["messages"] }))
5250
+ safeJsonWithUint8(
5251
+ normalizePromptAttr({
5252
+ system: (_d = mergedArgs["system"]) != null ? _d : mergedArgs["instructions"],
5253
+ prompt: mergedArgs["prompt"],
5254
+ messages: mergedArgs["messages"]
5255
+ })
5256
+ )
5314
5257
  )
5315
5258
  ]
5316
5259
  ]
@@ -5524,7 +5467,13 @@ function wrapAgentStream(stream, instance, agentSettings, className, aiSDK, deps
5524
5467
  ...(telemetry == null ? void 0 : telemetry.recordInputs) === false ? [] : [
5525
5468
  attrString(
5526
5469
  "ai.prompt",
5527
- safeJsonWithUint8(normalizePromptAttr({ system: (_d = mergedArgs["system"]) != null ? _d : mergedArgs["instructions"], prompt: mergedArgs["prompt"], messages: mergedArgs["messages"] }))
5470
+ safeJsonWithUint8(
5471
+ normalizePromptAttr({
5472
+ system: (_d = mergedArgs["system"]) != null ? _d : mergedArgs["instructions"],
5473
+ prompt: mergedArgs["prompt"],
5474
+ messages: mergedArgs["messages"]
5475
+ })
5476
+ )
5528
5477
  )
5529
5478
  ]
5530
5479
  ]
@@ -5901,9 +5850,19 @@ function wrapModel(args, aiSDK, outerOperationId, ctx) {
5901
5850
  for (const part of content) {
5902
5851
  if (isRecord(part)) {
5903
5852
  if ((part["type"] === "reasoning" || part["type"] === "thinking") && typeof ((_a = part["text"]) != null ? _a : part["thinking"]) === "string") {
5904
- sendLocalDebuggerLiveEvent({ traceId, type: "reasoning_delta", content: (_b = part["text"]) != null ? _b : part["thinking"], metadata: liveMeta });
5853
+ sendLocalDebuggerLiveEvent({
5854
+ traceId,
5855
+ type: "reasoning_delta",
5856
+ content: (_b = part["text"]) != null ? _b : part["thinking"],
5857
+ metadata: liveMeta
5858
+ });
5905
5859
  } else if (part["type"] === "text" && typeof part["text"] === "string") {
5906
- sendLocalDebuggerLiveEvent({ traceId, type: "text_delta", content: part["text"], metadata: liveMeta });
5860
+ sendLocalDebuggerLiveEvent({
5861
+ traceId,
5862
+ type: "text_delta",
5863
+ content: part["text"],
5864
+ metadata: liveMeta
5865
+ });
5907
5866
  }
5908
5867
  }
5909
5868
  }
@@ -5984,7 +5943,10 @@ function wrapModel(args, aiSDK, outerOperationId, ctx) {
5984
5943
  attributes: [
5985
5944
  ...((_a = ctx.telemetry) == null ? void 0 : _a.recordOutputs) === false ? [] : [
5986
5945
  attrString("ai.response.finishReason", finishReason),
5987
- attrString("ai.response.text", activeText.length ? capText2(activeText) : void 0),
5946
+ attrString(
5947
+ "ai.response.text",
5948
+ activeText.length ? capText2(activeText) : void 0
5949
+ ),
5988
5950
  attrString(
5989
5951
  "ai.response.toolCalls",
5990
5952
  safeJsonWithUint8(toolCallsLocal.length ? toolCallsLocal : void 0)
@@ -6002,7 +5964,8 @@ function wrapModel(args, aiSDK, outerOperationId, ctx) {
6002
5964
  ...finishReason ? [attrStringArray("gen_ai.response.finish_reasons", [finishReason])] : [],
6003
5965
  attrString("gen_ai.response.id", responseId),
6004
5966
  attrString(MODEL_USAGE_ATTRIBUTES.responseModel, finalResponseModelId),
6005
- ...buildModelUsageAttributes(usageMetrics),
5967
+ attrString(MODEL_USAGE_ATTRIBUTES.usageSource, usageMetrics.usageSource),
5968
+ ...buildRawModelUsageAttributes(usageMetrics),
6006
5969
  ...msToFirstChunk !== void 0 ? [attrInt("ai.stream.msToFirstChunk", msToFirstChunk)] : [],
6007
5970
  ...msToFinish !== void 0 ? [attrInt("ai.stream.msToFinish", msToFinish)] : [],
6008
5971
  ...avgOutTokensPerSecond !== void 0 ? [attrDouble("ai.stream.avgOutputTokensPerSecond", avgOutTokensPerSecond)] : [],
@@ -6056,19 +6019,44 @@ function wrapModel(args, aiSDK, outerOperationId, ctx) {
6056
6019
  if ("providerMetadata" in value) providerMetadata = value["providerMetadata"];
6057
6020
  if (localDebuggerEnabled() && ctx.rootParentForChildren) {
6058
6021
  const traceId = ctx.rootParentForChildren.traceIdB64;
6059
- const liveMeta = getLocalDebuggerMetadata({ ...ctx.context, eventId: ctx.eventId });
6022
+ const liveMeta = getLocalDebuggerMetadata({
6023
+ ...ctx.context,
6024
+ eventId: ctx.eventId
6025
+ });
6060
6026
  if (type === "text-delta") {
6061
6027
  const text = typeof value["delta"] === "string" ? value["delta"] : typeof value["textDelta"] === "string" ? value["textDelta"] : typeof value["text"] === "string" ? value["text"] : void 0;
6062
- if (typeof text === "string" && text) sendLocalDebuggerLiveEvent({ traceId, type: "text_delta", content: text, metadata: liveMeta });
6028
+ if (typeof text === "string" && text)
6029
+ sendLocalDebuggerLiveEvent({
6030
+ traceId,
6031
+ type: "text_delta",
6032
+ content: text,
6033
+ metadata: liveMeta
6034
+ });
6063
6035
  } else if (type === "reasoning" || type === "reasoning-delta") {
6064
6036
  const text = typeof value["delta"] === "string" ? value["delta"] : typeof value["text"] === "string" ? value["text"] : typeof value["thinking"] === "string" ? value["thinking"] : void 0;
6065
- if (typeof text === "string" && text) sendLocalDebuggerLiveEvent({ traceId, type: "reasoning_delta", content: text, metadata: liveMeta });
6037
+ if (typeof text === "string" && text)
6038
+ sendLocalDebuggerLiveEvent({
6039
+ traceId,
6040
+ type: "reasoning_delta",
6041
+ content: text,
6042
+ metadata: liveMeta
6043
+ });
6066
6044
  } else if (type === "tool-call") {
6067
6045
  const toolName = typeof value["toolName"] === "string" ? value["toolName"] : "tool";
6068
- sendLocalDebuggerLiveEvent({ traceId, type: "tool_start", content: toolName, metadata: { ...liveMeta, args: value["args"] } });
6046
+ sendLocalDebuggerLiveEvent({
6047
+ traceId,
6048
+ type: "tool_start",
6049
+ content: toolName,
6050
+ metadata: { ...liveMeta, args: value["args"] }
6051
+ });
6069
6052
  } else if (type === "tool-result") {
6070
6053
  const toolName = typeof value["toolName"] === "string" ? value["toolName"] : "tool";
6071
- sendLocalDebuggerLiveEvent({ traceId, type: "tool_result", content: toolName, metadata: { ...liveMeta, result: value["result"] } });
6054
+ sendLocalDebuggerLiveEvent({
6055
+ traceId,
6056
+ type: "tool_result",
6057
+ content: toolName,
6058
+ metadata: { ...liveMeta, result: value["result"] }
6059
+ });
6072
6060
  }
6073
6061
  }
6074
6062
  }
@@ -6148,11 +6136,7 @@ function endDoGenerateSpan(span, result, modelInfo, ctx) {
6148
6136
  } else {
6149
6137
  responseTimestampIso = (/* @__PURE__ */ new Date()).toISOString();
6150
6138
  }
6151
- const usageMetrics = extractUsageMetricsFromUsage(
6152
- usage,
6153
- modelInfo.provider,
6154
- providerMetadata
6155
- );
6139
+ const usageMetrics = extractUsageMetricsFromUsage(usage, modelInfo.provider, providerMetadata);
6156
6140
  const { inputTokens, outputTokens } = usageMetrics;
6157
6141
  ctx.traceShipper.endSpan(span, {
6158
6142
  attributes: [
@@ -6173,7 +6157,8 @@ function endDoGenerateSpan(span, result, modelInfo, ctx) {
6173
6157
  ...finishReason ? [attrStringArray("gen_ai.response.finish_reasons", [finishReason])] : [],
6174
6158
  attrString("gen_ai.response.id", responseId),
6175
6159
  attrString(MODEL_USAGE_ATTRIBUTES.responseModel, responseModelId),
6176
- ...buildModelUsageAttributes(usageMetrics)
6160
+ attrString(MODEL_USAGE_ATTRIBUTES.usageSource, usageMetrics.usageSource),
6161
+ ...buildRawModelUsageAttributes(usageMetrics)
6177
6162
  ]
6178
6163
  });
6179
6164
  }
@@ -6270,7 +6255,7 @@ function mergeAttachments(...groups) {
6270
6255
  // package.json
6271
6256
  var package_default = {
6272
6257
  name: "@raindrop-ai/ai-sdk",
6273
- version: "0.3.0"};
6258
+ version: "0.3.1"};
6274
6259
 
6275
6260
  // src/internal/version.ts
6276
6261
  var libraryName = package_default.name;