@ai-sdk/google 3.0.0-beta.67 → 3.0.0-beta.68

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,14 @@
1
1
  # @ai-sdk/google
2
2
 
3
+ ## 3.0.0-beta.68
4
+
5
+ ### Patch Changes
6
+
7
+ - 3bd2689: feat: extended token usage
8
+ - Updated dependencies [3bd2689]
9
+ - @ai-sdk/provider@3.0.0-beta.26
10
+ - @ai-sdk/provider-utils@4.0.0-beta.45
11
+
3
12
  ## 3.0.0-beta.67
4
13
 
5
14
  ### Patch Changes
package/dist/index.js CHANGED
@@ -30,7 +30,7 @@ module.exports = __toCommonJS(src_exports);
30
30
  var import_provider_utils13 = require("@ai-sdk/provider-utils");
31
31
 
32
32
  // src/version.ts
33
- var VERSION = true ? "3.0.0-beta.67" : "0.0.0-test";
33
+ var VERSION = true ? "3.0.0-beta.68" : "0.0.0-test";
34
34
 
35
35
  // src/google-generative-ai-embedding-model.ts
36
36
  var import_provider = require("@ai-sdk/provider");
@@ -207,6 +207,45 @@ var googleGenerativeAISingleEmbeddingResponseSchema = (0, import_provider_utils3
207
207
  var import_provider_utils6 = require("@ai-sdk/provider-utils");
208
208
  var import_v45 = require("zod/v4");
209
209
 
210
+ // src/convert-google-generative-ai-usage.ts
211
+ function convertGoogleGenerativeAIUsage(usage) {
212
+ var _a, _b, _c, _d;
213
+ if (usage == null) {
214
+ return {
215
+ inputTokens: {
216
+ total: void 0,
217
+ noCache: void 0,
218
+ cacheRead: void 0,
219
+ cacheWrite: void 0
220
+ },
221
+ outputTokens: {
222
+ total: void 0,
223
+ text: void 0,
224
+ reasoning: void 0
225
+ },
226
+ raw: void 0
227
+ };
228
+ }
229
+ const promptTokens = (_a = usage.promptTokenCount) != null ? _a : 0;
230
+ const candidatesTokens = (_b = usage.candidatesTokenCount) != null ? _b : 0;
231
+ const cachedContentTokens = (_c = usage.cachedContentTokenCount) != null ? _c : 0;
232
+ const thoughtsTokens = (_d = usage.thoughtsTokenCount) != null ? _d : 0;
233
+ return {
234
+ inputTokens: {
235
+ total: promptTokens,
236
+ noCache: promptTokens - cachedContentTokens,
237
+ cacheRead: cachedContentTokens,
238
+ cacheWrite: void 0
239
+ },
240
+ outputTokens: {
241
+ total: candidatesTokens + thoughtsTokens,
242
+ text: candidatesTokens,
243
+ reasoning: thoughtsTokens
244
+ },
245
+ raw: usage
246
+ };
247
+ }
248
+
210
249
  // src/convert-json-schema-to-openapi-schema.ts
211
250
  function convertJSONSchemaToOpenAPISchema(jsonSchema) {
212
251
  if (jsonSchema == null || isEmptyObjectSchema(jsonSchema)) {
@@ -893,7 +932,7 @@ var GoogleGenerativeAILanguageModel = class {
893
932
  };
894
933
  }
895
934
  async doGenerate(options) {
896
- var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m;
935
+ var _a, _b, _c, _d, _e, _f, _g, _h;
897
936
  const { args, warnings } = await this.getArgs(options);
898
937
  const body = JSON.stringify(args);
899
938
  const mergedHeaders = (0, import_provider_utils6.combineHeaders)(
@@ -979,20 +1018,14 @@ var GoogleGenerativeAILanguageModel = class {
979
1018
  finishReason: candidate.finishReason,
980
1019
  hasToolCalls: content.some((part) => part.type === "tool-call")
981
1020
  }),
982
- usage: {
983
- inputTokens: (_e = usageMetadata == null ? void 0 : usageMetadata.promptTokenCount) != null ? _e : void 0,
984
- outputTokens: (_f = usageMetadata == null ? void 0 : usageMetadata.candidatesTokenCount) != null ? _f : void 0,
985
- totalTokens: (_g = usageMetadata == null ? void 0 : usageMetadata.totalTokenCount) != null ? _g : void 0,
986
- reasoningTokens: (_h = usageMetadata == null ? void 0 : usageMetadata.thoughtsTokenCount) != null ? _h : void 0,
987
- cachedInputTokens: (_i = usageMetadata == null ? void 0 : usageMetadata.cachedContentTokenCount) != null ? _i : void 0
988
- },
1021
+ usage: convertGoogleGenerativeAIUsage(usageMetadata),
989
1022
  warnings,
990
1023
  providerMetadata: {
991
1024
  google: {
992
- promptFeedback: (_j = response.promptFeedback) != null ? _j : null,
993
- groundingMetadata: (_k = candidate.groundingMetadata) != null ? _k : null,
994
- urlContextMetadata: (_l = candidate.urlContextMetadata) != null ? _l : null,
995
- safetyRatings: (_m = candidate.safetyRatings) != null ? _m : null,
1025
+ promptFeedback: (_e = response.promptFeedback) != null ? _e : null,
1026
+ groundingMetadata: (_f = candidate.groundingMetadata) != null ? _f : null,
1027
+ urlContextMetadata: (_g = candidate.urlContextMetadata) != null ? _g : null,
1028
+ safetyRatings: (_h = candidate.safetyRatings) != null ? _h : null,
996
1029
  usageMetadata: usageMetadata != null ? usageMetadata : null
997
1030
  }
998
1031
  },
@@ -1023,11 +1056,7 @@ var GoogleGenerativeAILanguageModel = class {
1023
1056
  fetch: this.config.fetch
1024
1057
  });
1025
1058
  let finishReason = "unknown";
1026
- const usage = {
1027
- inputTokens: void 0,
1028
- outputTokens: void 0,
1029
- totalTokens: void 0
1030
- };
1059
+ let usage = void 0;
1031
1060
  let providerMetadata = void 0;
1032
1061
  const generateId3 = this.config.generateId;
1033
1062
  let hasToolCalls = false;
@@ -1043,7 +1072,7 @@ var GoogleGenerativeAILanguageModel = class {
1043
1072
  controller.enqueue({ type: "stream-start", warnings });
1044
1073
  },
1045
1074
  transform(chunk, controller) {
1046
- var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l;
1075
+ var _a, _b, _c, _d, _e, _f, _g;
1047
1076
  if (options.includeRawChunks) {
1048
1077
  controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
1049
1078
  }
@@ -1054,13 +1083,9 @@ var GoogleGenerativeAILanguageModel = class {
1054
1083
  const value = chunk.value;
1055
1084
  const usageMetadata = value.usageMetadata;
1056
1085
  if (usageMetadata != null) {
1057
- usage.inputTokens = (_a = usageMetadata.promptTokenCount) != null ? _a : void 0;
1058
- usage.outputTokens = (_b = usageMetadata.candidatesTokenCount) != null ? _b : void 0;
1059
- usage.totalTokens = (_c = usageMetadata.totalTokenCount) != null ? _c : void 0;
1060
- usage.reasoningTokens = (_d = usageMetadata.thoughtsTokenCount) != null ? _d : void 0;
1061
- usage.cachedInputTokens = (_e = usageMetadata.cachedContentTokenCount) != null ? _e : void 0;
1086
+ usage = usageMetadata;
1062
1087
  }
1063
- const candidate = (_f = value.candidates) == null ? void 0 : _f[0];
1088
+ const candidate = (_a = value.candidates) == null ? void 0 : _a[0];
1064
1089
  if (candidate == null) {
1065
1090
  return;
1066
1091
  }
@@ -1078,9 +1103,9 @@ var GoogleGenerativeAILanguageModel = class {
1078
1103
  }
1079
1104
  }
1080
1105
  if (content != null) {
1081
- const parts = (_g = content.parts) != null ? _g : [];
1106
+ const parts = (_b = content.parts) != null ? _b : [];
1082
1107
  for (const part of parts) {
1083
- if ("executableCode" in part && ((_h = part.executableCode) == null ? void 0 : _h.code)) {
1108
+ if ("executableCode" in part && ((_c = part.executableCode) == null ? void 0 : _c.code)) {
1084
1109
  const toolCallId = generateId3();
1085
1110
  lastCodeExecutionToolCallId = toolCallId;
1086
1111
  controller.enqueue({
@@ -1212,10 +1237,10 @@ var GoogleGenerativeAILanguageModel = class {
1212
1237
  });
1213
1238
  providerMetadata = {
1214
1239
  google: {
1215
- promptFeedback: (_i = value.promptFeedback) != null ? _i : null,
1216
- groundingMetadata: (_j = candidate.groundingMetadata) != null ? _j : null,
1217
- urlContextMetadata: (_k = candidate.urlContextMetadata) != null ? _k : null,
1218
- safetyRatings: (_l = candidate.safetyRatings) != null ? _l : null
1240
+ promptFeedback: (_d = value.promptFeedback) != null ? _d : null,
1241
+ groundingMetadata: (_e = candidate.groundingMetadata) != null ? _e : null,
1242
+ urlContextMetadata: (_f = candidate.urlContextMetadata) != null ? _f : null,
1243
+ safetyRatings: (_g = candidate.safetyRatings) != null ? _g : null
1219
1244
  }
1220
1245
  };
1221
1246
  if (usageMetadata != null) {
@@ -1239,7 +1264,7 @@ var GoogleGenerativeAILanguageModel = class {
1239
1264
  controller.enqueue({
1240
1265
  type: "finish",
1241
1266
  finishReason,
1242
- usage,
1267
+ usage: convertGoogleGenerativeAIUsage(usage),
1243
1268
  providerMetadata
1244
1269
  });
1245
1270
  }