ai 6.0.118 → 6.0.119

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/CHANGELOG.md CHANGED
@@ -1,5 +1,12 @@
1
1
  # ai
2
2
 
3
+ ## 6.0.119
4
+
5
+ ### Patch Changes
6
+
7
+ - ab286f1: fix(ai): doStream should reflect transformed values
8
+ - d68b122: feat(ai): add missing usage attributes
9
+
3
10
  ## 6.0.118
4
11
 
5
12
  ### Patch Changes
package/dist/index.js CHANGED
@@ -1230,7 +1230,7 @@ var import_provider_utils3 = require("@ai-sdk/provider-utils");
1230
1230
  var import_provider_utils4 = require("@ai-sdk/provider-utils");
1231
1231
 
1232
1232
  // src/version.ts
1233
- var VERSION = true ? "6.0.118" : "0.0.0-test";
1233
+ var VERSION = true ? "6.0.119" : "0.0.0-test";
1234
1234
 
1235
1235
  // src/util/download/download.ts
1236
1236
  var download = async ({
@@ -4206,7 +4206,7 @@ async function generateText({
4206
4206
  }),
4207
4207
  tracer,
4208
4208
  fn: async (span) => {
4209
- var _a21, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m;
4209
+ var _a21, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t;
4210
4210
  const initialMessages = initialPrompt.messages;
4211
4211
  const responseMessages = [];
4212
4212
  const { approvedToolApprovals, deniedToolApprovals } = collectToolApprovals({ messages: initialMessages });
@@ -4423,6 +4423,7 @@ async function generateText({
4423
4423
  headers: (_g2 = result.response) == null ? void 0 : _g2.headers,
4424
4424
  body: (_h2 = result.response) == null ? void 0 : _h2.body
4425
4425
  };
4426
+ const usage = asLanguageModelUsage(result.usage);
4426
4427
  span2.setAttributes(
4427
4428
  await selectTelemetryAttributes({
4428
4429
  telemetry,
@@ -4446,9 +4447,16 @@ async function generateText({
4446
4447
  "ai.response.providerMetadata": JSON.stringify(
4447
4448
  result.providerMetadata
4448
4449
  ),
4449
- // TODO rename telemetry attributes to inputTokens and outputTokens
4450
- "ai.usage.promptTokens": result.usage.inputTokens.total,
4451
- "ai.usage.completionTokens": result.usage.outputTokens.total,
4450
+ "ai.usage.inputTokens": result.usage.inputTokens.total,
4451
+ "ai.usage.inputTokenDetails.noCacheTokens": result.usage.inputTokens.noCache,
4452
+ "ai.usage.inputTokenDetails.cacheReadTokens": result.usage.inputTokens.cacheRead,
4453
+ "ai.usage.inputTokenDetails.cacheWriteTokens": result.usage.inputTokens.cacheWrite,
4454
+ "ai.usage.outputTokens": result.usage.outputTokens.total,
4455
+ "ai.usage.outputTokenDetails.textTokens": result.usage.outputTokens.text,
4456
+ "ai.usage.outputTokenDetails.reasoningTokens": result.usage.outputTokens.reasoning,
4457
+ "ai.usage.totalTokens": usage.totalTokens,
4458
+ "ai.usage.reasoningTokens": result.usage.outputTokens.reasoning,
4459
+ "ai.usage.cachedInputTokens": result.usage.inputTokens.cacheRead,
4452
4460
  // standardized gen-ai llm span attributes:
4453
4461
  "gen_ai.response.finish_reasons": [
4454
4462
  result.finishReason.unified
@@ -4649,10 +4657,7 @@ async function generateText({
4649
4657
  },
4650
4658
  "ai.response.providerMetadata": JSON.stringify(
4651
4659
  currentModelResponse.providerMetadata
4652
- ),
4653
- // TODO rename telemetry attributes to inputTokens and outputTokens
4654
- "ai.usage.promptTokens": currentModelResponse.usage.inputTokens.total,
4655
- "ai.usage.completionTokens": currentModelResponse.usage.outputTokens.total
4660
+ )
4656
4661
  }
4657
4662
  })
4658
4663
  );
@@ -4669,6 +4674,23 @@ async function generateText({
4669
4674
  cachedInputTokens: void 0
4670
4675
  }
4671
4676
  );
4677
+ span.setAttributes(
4678
+ await selectTelemetryAttributes({
4679
+ telemetry,
4680
+ attributes: {
4681
+ "ai.usage.inputTokens": totalUsage.inputTokens,
4682
+ "ai.usage.inputTokenDetails.noCacheTokens": (_n = totalUsage.inputTokenDetails) == null ? void 0 : _n.noCacheTokens,
4683
+ "ai.usage.inputTokenDetails.cacheReadTokens": (_o = totalUsage.inputTokenDetails) == null ? void 0 : _o.cacheReadTokens,
4684
+ "ai.usage.inputTokenDetails.cacheWriteTokens": (_p = totalUsage.inputTokenDetails) == null ? void 0 : _p.cacheWriteTokens,
4685
+ "ai.usage.outputTokens": totalUsage.outputTokens,
4686
+ "ai.usage.outputTokenDetails.textTokens": (_q = totalUsage.outputTokenDetails) == null ? void 0 : _q.textTokens,
4687
+ "ai.usage.outputTokenDetails.reasoningTokens": (_r = totalUsage.outputTokenDetails) == null ? void 0 : _r.reasoningTokens,
4688
+ "ai.usage.totalTokens": totalUsage.totalTokens,
4689
+ "ai.usage.reasoningTokens": (_s = totalUsage.outputTokenDetails) == null ? void 0 : _s.reasoningTokens,
4690
+ "ai.usage.cachedInputTokens": (_t = totalUsage.inputTokenDetails) == null ? void 0 : _t.cacheReadTokens
4691
+ }
4692
+ })
4693
+ );
4672
4694
  await notify({
4673
4695
  event: {
4674
4696
  stepNumber: lastStep.stepNumber,
@@ -6767,6 +6789,7 @@ var DefaultStreamTextResult = class {
6767
6789
  }
6768
6790
  },
6769
6791
  async flush(controller) {
6792
+ var _a21, _b, _c, _d, _e, _f, _g;
6770
6793
  try {
6771
6794
  if (recordedSteps.length === 0) {
6772
6795
  const error = (abortSignal == null ? void 0 : abortSignal.aborted) ? abortSignal.reason : new NoOutputGeneratedError({
@@ -6830,18 +6853,23 @@ var DefaultStreamTextResult = class {
6830
6853
  },
6831
6854
  "ai.response.toolCalls": {
6832
6855
  output: () => {
6833
- var _a21;
6834
- return ((_a21 = finalStep.toolCalls) == null ? void 0 : _a21.length) ? JSON.stringify(finalStep.toolCalls) : void 0;
6856
+ var _a22;
6857
+ return ((_a22 = finalStep.toolCalls) == null ? void 0 : _a22.length) ? JSON.stringify(finalStep.toolCalls) : void 0;
6835
6858
  }
6836
6859
  },
6837
6860
  "ai.response.providerMetadata": JSON.stringify(
6838
6861
  finalStep.providerMetadata
6839
6862
  ),
6840
6863
  "ai.usage.inputTokens": totalUsage.inputTokens,
6864
+ "ai.usage.inputTokenDetails.noCacheTokens": (_a21 = totalUsage.inputTokenDetails) == null ? void 0 : _a21.noCacheTokens,
6865
+ "ai.usage.inputTokenDetails.cacheReadTokens": (_b = totalUsage.inputTokenDetails) == null ? void 0 : _b.cacheReadTokens,
6866
+ "ai.usage.inputTokenDetails.cacheWriteTokens": (_c = totalUsage.inputTokenDetails) == null ? void 0 : _c.cacheWriteTokens,
6841
6867
  "ai.usage.outputTokens": totalUsage.outputTokens,
6868
+ "ai.usage.outputTokenDetails.textTokens": (_d = totalUsage.outputTokenDetails) == null ? void 0 : _d.textTokens,
6869
+ "ai.usage.outputTokenDetails.reasoningTokens": (_e = totalUsage.outputTokenDetails) == null ? void 0 : _e.reasoningTokens,
6842
6870
  "ai.usage.totalTokens": totalUsage.totalTokens,
6843
- "ai.usage.reasoningTokens": totalUsage.reasoningTokens,
6844
- "ai.usage.cachedInputTokens": totalUsage.cachedInputTokens
6871
+ "ai.usage.reasoningTokens": (_f = totalUsage.outputTokenDetails) == null ? void 0 : _f.reasoningTokens,
6872
+ "ai.usage.cachedInputTokens": (_g = totalUsage.inputTokenDetails) == null ? void 0 : _g.cacheReadTokens
6845
6873
  }
6846
6874
  })
6847
6875
  );
@@ -7453,6 +7481,7 @@ var DefaultStreamTextResult = class {
7453
7481
  },
7454
7482
  // invoke onFinish callback and resolve toolResults promise when the stream is about to close:
7455
7483
  async flush(controller) {
7484
+ var _a22, _b2, _c2, _d2, _e2, _f2, _g2;
7456
7485
  const stepToolCallsJson = stepToolCalls.length > 0 ? JSON.stringify(stepToolCalls) : void 0;
7457
7486
  try {
7458
7487
  doStreamSpan.setAttributes(
@@ -7460,29 +7489,22 @@ var DefaultStreamTextResult = class {
7460
7489
  telemetry,
7461
7490
  attributes: {
7462
7491
  "ai.response.finishReason": stepFinishReason,
7463
- "ai.response.text": {
7464
- output: () => activeText
7465
- },
7466
- "ai.response.reasoning": {
7467
- output: () => {
7468
- const reasoningParts = recordedContent.filter(
7469
- (c) => c.type === "reasoning"
7470
- );
7471
- return reasoningParts.length > 0 ? reasoningParts.map((r) => r.text).join("\n") : void 0;
7472
- }
7473
- },
7474
7492
  "ai.response.toolCalls": {
7475
7493
  output: () => stepToolCallsJson
7476
7494
  },
7477
7495
  "ai.response.id": stepResponse.id,
7478
7496
  "ai.response.model": stepResponse.modelId,
7479
7497
  "ai.response.timestamp": stepResponse.timestamp.toISOString(),
7480
- "ai.response.providerMetadata": JSON.stringify(stepProviderMetadata),
7481
7498
  "ai.usage.inputTokens": stepUsage.inputTokens,
7499
+ "ai.usage.inputTokenDetails.noCacheTokens": (_a22 = stepUsage.inputTokenDetails) == null ? void 0 : _a22.noCacheTokens,
7500
+ "ai.usage.inputTokenDetails.cacheReadTokens": (_b2 = stepUsage.inputTokenDetails) == null ? void 0 : _b2.cacheReadTokens,
7501
+ "ai.usage.inputTokenDetails.cacheWriteTokens": (_c2 = stepUsage.inputTokenDetails) == null ? void 0 : _c2.cacheWriteTokens,
7482
7502
  "ai.usage.outputTokens": stepUsage.outputTokens,
7503
+ "ai.usage.outputTokenDetails.textTokens": (_d2 = stepUsage.outputTokenDetails) == null ? void 0 : _d2.textTokens,
7504
+ "ai.usage.outputTokenDetails.reasoningTokens": (_e2 = stepUsage.outputTokenDetails) == null ? void 0 : _e2.reasoningTokens,
7483
7505
  "ai.usage.totalTokens": stepUsage.totalTokens,
7484
- "ai.usage.reasoningTokens": stepUsage.reasoningTokens,
7485
- "ai.usage.cachedInputTokens": stepUsage.cachedInputTokens,
7506
+ "ai.usage.reasoningTokens": (_f2 = stepUsage.outputTokenDetails) == null ? void 0 : _f2.reasoningTokens,
7507
+ "ai.usage.cachedInputTokens": (_g2 = stepUsage.inputTokenDetails) == null ? void 0 : _g2.cacheReadTokens,
7486
7508
  // standardized gen-ai llm span attributes:
7487
7509
  "gen_ai.response.finish_reasons": [
7488
7510
  stepFinishReason
@@ -7495,8 +7517,6 @@ var DefaultStreamTextResult = class {
7495
7517
  })
7496
7518
  );
7497
7519
  } catch (error) {
7498
- } finally {
7499
- doStreamSpan.end();
7500
7520
  }
7501
7521
  controller.enqueue({
7502
7522
  type: "finish-step",
@@ -7514,6 +7534,28 @@ var DefaultStreamTextResult = class {
7514
7534
  stepUsage
7515
7535
  );
7516
7536
  await stepFinish.promise;
7537
+ const processedStep = recordedSteps[recordedSteps.length - 1];
7538
+ try {
7539
+ doStreamSpan.setAttributes(
7540
+ await selectTelemetryAttributes({
7541
+ telemetry,
7542
+ attributes: {
7543
+ "ai.response.text": {
7544
+ output: () => processedStep.text
7545
+ },
7546
+ "ai.response.reasoning": {
7547
+ output: () => processedStep.reasoningText
7548
+ },
7549
+ "ai.response.providerMetadata": JSON.stringify(
7550
+ processedStep.providerMetadata
7551
+ )
7552
+ }
7553
+ })
7554
+ );
7555
+ } catch (error) {
7556
+ } finally {
7557
+ doStreamSpan.end();
7558
+ }
7517
7559
  const clientToolCalls = stepToolCalls.filter(
7518
7560
  (toolCall) => toolCall.providerExecuted !== true
7519
7561
  );