ai 3.1.33 → 3.1.35

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
@@ -215,7 +215,31 @@ function convertDataContentToUint8Array(content) {
215
215
  throw new InvalidDataContentError({ content });
216
216
  }
217
217
 
218
+ // core/prompt/invalid-message-role-error.ts
219
+ var InvalidMessageRoleError = class extends Error {
220
+ constructor({
221
+ role,
222
+ message = `Invalid message role: '${role}'. Must be one of: "system", "user", "assistant", "tool".`
223
+ }) {
224
+ super(message);
225
+ this.name = "AI_InvalidMessageRoleError";
226
+ this.role = role;
227
+ }
228
+ static isInvalidMessageRoleError(error) {
229
+ return error instanceof Error && error.name === "AI_InvalidMessageRoleError" && typeof error.role === "string";
230
+ }
231
+ toJSON() {
232
+ return {
233
+ name: this.name,
234
+ message: this.message,
235
+ stack: this.stack,
236
+ role: this.role
237
+ };
238
+ }
239
+ };
240
+
218
241
  // core/prompt/convert-to-language-model-prompt.ts
242
+ import { getErrorMessage as getErrorMessage2 } from "@ai-sdk/provider-utils";
219
243
  function convertToLanguageModelPrompt(prompt) {
220
244
  const languageModelMessages = [];
221
245
  if (prompt.system != null) {
@@ -244,7 +268,8 @@ function convertToLanguageModelPrompt(prompt) {
244
268
  return languageModelMessages;
245
269
  }
246
270
  function convertToLanguageModelMessage(message) {
247
- switch (message.role) {
271
+ const role = message.role;
272
+ switch (role) {
248
273
  case "system": {
249
274
  return { role: "system", content: message.content };
250
275
  }
@@ -272,6 +297,47 @@ function convertToLanguageModelMessage(message) {
272
297
  mimeType: part.mimeType
273
298
  };
274
299
  }
300
+ if (typeof part.image === "string") {
301
+ try {
302
+ const url = new URL(part.image);
303
+ switch (url.protocol) {
304
+ case "http:":
305
+ case "https:": {
306
+ return {
307
+ type: "image",
308
+ image: url,
309
+ mimeType: part.mimeType
310
+ };
311
+ }
312
+ case "data:": {
313
+ try {
314
+ const [header, base64Content] = part.image.split(",");
315
+ const mimeType = header.split(";")[0].split(":")[1];
316
+ if (mimeType == null || base64Content == null) {
317
+ throw new Error("Invalid data URL format");
318
+ }
319
+ return {
320
+ type: "image",
321
+ image: convertDataContentToUint8Array(base64Content),
322
+ mimeType
323
+ };
324
+ } catch (error) {
325
+ throw new Error(
326
+ `Error processing data URL: ${getErrorMessage2(
327
+ message
328
+ )}`
329
+ );
330
+ }
331
+ }
332
+ default: {
333
+ throw new Error(
334
+ `Unsupported URL protocol: ${url.protocol}`
335
+ );
336
+ }
337
+ }
338
+ } catch (_ignored) {
339
+ }
340
+ }
275
341
  const imageUint8 = convertDataContentToUint8Array(part.image);
276
342
  return {
277
343
  type: "image",
@@ -303,8 +369,8 @@ function convertToLanguageModelMessage(message) {
303
369
  return message;
304
370
  }
305
371
  default: {
306
- const _exhaustiveCheck = message;
307
- throw new Error(`Unsupported message role: ${_exhaustiveCheck}`);
372
+ const _exhaustiveCheck = role;
373
+ throw new InvalidMessageRoleError({ role: _exhaustiveCheck });
308
374
  }
309
375
  }
310
376
  }
@@ -2123,6 +2189,25 @@ import {
2123
2189
  UnsupportedJSONSchemaError
2124
2190
  } from "@ai-sdk/provider";
2125
2191
 
2192
+ // core/util/cosine-similarity.ts
2193
+ function cosineSimilarity(vector1, vector2) {
2194
+ if (vector1.length !== vector2.length) {
2195
+ throw new Error(
2196
+ `Vectors must have the same length (vector1: ${vector1.length} elements, vector2: ${vector2.length} elements)`
2197
+ );
2198
+ }
2199
+ return dotProduct(vector1, vector2) / (magnitude(vector1) * magnitude(vector2));
2200
+ }
2201
+ function dotProduct(vector1, vector2) {
2202
+ return vector1.reduce(
2203
+ (accumulator, value, index) => accumulator + value * vector2[index],
2204
+ 0
2205
+ );
2206
+ }
2207
+ function magnitude(vector) {
2208
+ return Math.sqrt(dotProduct(vector, vector));
2209
+ }
2210
+
2126
2211
  // streams/ai-stream.ts
2127
2212
  import {
2128
2213
  createParser
@@ -3244,6 +3329,7 @@ export {
3244
3329
  InkeepStream,
3245
3330
  InvalidArgumentError2 as InvalidArgumentError,
3246
3331
  InvalidDataContentError2 as InvalidDataContentError,
3332
+ InvalidMessageRoleError,
3247
3333
  InvalidModelIdError,
3248
3334
  InvalidPromptError2 as InvalidPromptError,
3249
3335
  InvalidResponseDataError,
@@ -3271,6 +3357,7 @@ export {
3271
3357
  convertDataContentToBase64String,
3272
3358
  convertDataContentToUint8Array,
3273
3359
  convertToCoreMessages,
3360
+ cosineSimilarity,
3274
3361
  createCallbacksTransformer,
3275
3362
  createEventStreamTransformer,
3276
3363
  createStreamDataTransformer,