ai 3.2.17 → 3.2.19

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/dist/index.mjs CHANGED
@@ -83,6 +83,7 @@ async function embed({
83
83
  abortSignal,
84
84
  headers
85
85
  }) {
86
+ var _a;
86
87
  const retry = retryWithExponentialBackoff({ maxRetries });
87
88
  const modelResponse = await retry(
88
89
  () => model.doEmbed({ values: [value], abortSignal, headers })
@@ -90,6 +91,7 @@ async function embed({
90
91
  return new EmbedResult({
91
92
  value,
92
93
  embedding: modelResponse.embeddings[0],
94
+ usage: (_a = modelResponse.usage) != null ? _a : { tokens: NaN },
93
95
  rawResponse: modelResponse.rawResponse
94
96
  });
95
97
  }
@@ -97,6 +99,7 @@ var EmbedResult = class {
97
99
  constructor(options) {
98
100
  this.value = options.value;
99
101
  this.embedding = options.embedding;
102
+ this.usage = options.usage;
100
103
  this.rawResponse = options.rawResponse;
101
104
  }
102
105
  };
@@ -121,6 +124,7 @@ async function embedMany({
121
124
  abortSignal,
122
125
  headers
123
126
  }) {
127
+ var _a, _b, _c;
124
128
  const retry = retryWithExponentialBackoff({ maxRetries });
125
129
  const maxEmbeddingsPerCall = model.maxEmbeddingsPerCall;
126
130
  if (maxEmbeddingsPerCall == null) {
@@ -129,23 +133,27 @@ async function embedMany({
129
133
  );
130
134
  return new EmbedManyResult({
131
135
  values,
132
- embeddings: modelResponse.embeddings
136
+ embeddings: modelResponse.embeddings,
137
+ usage: (_a = modelResponse.usage) != null ? _a : { tokens: NaN }
133
138
  });
134
139
  }
135
140
  const valueChunks = splitArray(values, maxEmbeddingsPerCall);
136
141
  const embeddings = [];
142
+ let tokens = 0;
137
143
  for (const chunk of valueChunks) {
138
144
  const modelResponse = await retry(
139
145
  () => model.doEmbed({ values: chunk, abortSignal, headers })
140
146
  );
141
147
  embeddings.push(...modelResponse.embeddings);
148
+ tokens += (_c = (_b = modelResponse.usage) == null ? void 0 : _b.tokens) != null ? _c : NaN;
142
149
  }
143
- return new EmbedManyResult({ values, embeddings });
150
+ return new EmbedManyResult({ values, embeddings, usage: { tokens } });
144
151
  }
145
152
  var EmbedManyResult = class {
146
153
  constructor(options) {
147
154
  this.values = options.values;
148
155
  this.embeddings = options.embeddings;
156
+ this.usage = options.usage;
149
157
  }
150
158
  };
151
159
 
@@ -153,15 +161,6 @@ var EmbedManyResult = class {
153
161
  import { NoObjectGeneratedError } from "@ai-sdk/provider";
154
162
  import { safeParseJSON } from "@ai-sdk/provider-utils";
155
163
 
156
- // core/generate-text/token-usage.ts
157
- function calculateTokenUsage(usage) {
158
- return {
159
- promptTokens: usage.promptTokens,
160
- completionTokens: usage.completionTokens,
161
- totalTokens: usage.promptTokens + usage.completionTokens
162
- };
163
- }
164
-
165
164
  // core/util/detect-image-mimetype.ts
166
165
  var mimeTypeSignatures = [
167
166
  { mimeType: "image/gif", bytes: [71, 73, 70] },
@@ -512,12 +511,31 @@ function prepareCallSettings({
512
511
  };
513
512
  }
514
513
 
514
+ // core/types/token-usage.ts
515
+ function calculateCompletionTokenUsage(usage) {
516
+ return {
517
+ promptTokens: usage.promptTokens,
518
+ completionTokens: usage.completionTokens,
519
+ totalTokens: usage.promptTokens + usage.completionTokens
520
+ };
521
+ }
522
+
515
523
  // core/util/convert-zod-to-json-schema.ts
516
524
  import zodToJsonSchema from "zod-to-json-schema";
517
525
  function convertZodToJSONSchema(zodSchema) {
518
526
  return zodToJsonSchema(zodSchema);
519
527
  }
520
528
 
529
+ // core/util/prepare-response-headers.ts
530
+ function prepareResponseHeaders(init, { contentType }) {
531
+ var _a;
532
+ const headers = new Headers((_a = init == null ? void 0 : init.headers) != null ? _a : {});
533
+ if (!headers.has("Content-Type")) {
534
+ headers.set("Content-Type", contentType);
535
+ }
536
+ return headers;
537
+ }
538
+
521
539
  // core/generate-object/inject-json-schema-into-system.ts
522
540
  var DEFAULT_SCHEMA_PREFIX = "JSON schema:";
523
541
  var DEFAULT_SCHEMA_SUFFIX = "You MUST answer with a JSON object that matches the JSON schema above.";
@@ -537,16 +555,6 @@ function injectJsonSchemaIntoSystem({
537
555
  ].filter((line) => line != null).join("\n");
538
556
  }
539
557
 
540
- // core/util/prepare-response-headers.ts
541
- function prepareResponseHeaders(init, { contentType }) {
542
- var _a;
543
- const headers = new Headers((_a = init == null ? void 0 : init.headers) != null ? _a : {});
544
- if (!headers.has("Content-Type")) {
545
- headers.set("Content-Type", contentType);
546
- }
547
- return headers;
548
- }
549
-
550
558
  // core/generate-object/generate-object.ts
551
559
  async function generateObject({
552
560
  model,
@@ -676,7 +684,7 @@ async function generateObject({
676
684
  return new GenerateObjectResult({
677
685
  object: parseResult.value,
678
686
  finishReason,
679
- usage: calculateTokenUsage(usage),
687
+ usage: calculateCompletionTokenUsage(usage),
680
688
  warnings,
681
689
  rawResponse,
682
690
  logprobs
@@ -922,7 +930,7 @@ var StreamObjectResult = class {
922
930
  textDelta: delta
923
931
  });
924
932
  }
925
- usage = calculateTokenUsage(chunk.usage);
933
+ usage = calculateCompletionTokenUsage(chunk.usage);
926
934
  controller.enqueue({ ...chunk, usage });
927
935
  resolveUsage(usage);
928
936
  const validationResult = safeValidateTypes({
@@ -1213,7 +1221,7 @@ async function generateText({
1213
1221
  toolCalls: currentToolCalls,
1214
1222
  toolResults: currentToolResults,
1215
1223
  finishReason: currentModelResponse.finishReason,
1216
- usage: calculateTokenUsage(currentModelResponse.usage),
1224
+ usage: calculateCompletionTokenUsage(currentModelResponse.usage),
1217
1225
  warnings: currentModelResponse.warnings,
1218
1226
  rawResponse: currentModelResponse.rawResponse,
1219
1227
  logprobs: currentModelResponse.logprobs,
@@ -1371,7 +1379,7 @@ function runToolsTransformation({
1371
1379
  type: "finish",
1372
1380
  finishReason: chunk.finishReason,
1373
1381
  logprobs: chunk.logprobs,
1374
- usage: calculateTokenUsage(chunk.usage)
1382
+ usage: calculateCompletionTokenUsage(chunk.usage)
1375
1383
  });
1376
1384
  break;
1377
1385
  }