ai 3.1.33 → 3.1.34

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,6 +215,29 @@ 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
219
242
  function convertToLanguageModelPrompt(prompt) {
220
243
  const languageModelMessages = [];
@@ -244,7 +267,8 @@ function convertToLanguageModelPrompt(prompt) {
244
267
  return languageModelMessages;
245
268
  }
246
269
  function convertToLanguageModelMessage(message) {
247
- switch (message.role) {
270
+ const role = message.role;
271
+ switch (role) {
248
272
  case "system": {
249
273
  return { role: "system", content: message.content };
250
274
  }
@@ -303,8 +327,8 @@ function convertToLanguageModelMessage(message) {
303
327
  return message;
304
328
  }
305
329
  default: {
306
- const _exhaustiveCheck = message;
307
- throw new Error(`Unsupported message role: ${_exhaustiveCheck}`);
330
+ const _exhaustiveCheck = role;
331
+ throw new InvalidMessageRoleError({ role: _exhaustiveCheck });
308
332
  }
309
333
  }
310
334
  }
@@ -2123,6 +2147,25 @@ import {
2123
2147
  UnsupportedJSONSchemaError
2124
2148
  } from "@ai-sdk/provider";
2125
2149
 
2150
+ // core/util/cosine-similarity.ts
2151
+ function cosineSimilarity(vector1, vector2) {
2152
+ if (vector1.length !== vector2.length) {
2153
+ throw new Error(
2154
+ `Vectors must have the same length (vector1: ${vector1.length} elements, vector2: ${vector2.length} elements)`
2155
+ );
2156
+ }
2157
+ return dotProduct(vector1, vector2) / (magnitude(vector1) * magnitude(vector2));
2158
+ }
2159
+ function dotProduct(vector1, vector2) {
2160
+ return vector1.reduce(
2161
+ (accumulator, value, index) => accumulator + value * vector2[index],
2162
+ 0
2163
+ );
2164
+ }
2165
+ function magnitude(vector) {
2166
+ return Math.sqrt(dotProduct(vector, vector));
2167
+ }
2168
+
2126
2169
  // streams/ai-stream.ts
2127
2170
  import {
2128
2171
  createParser
@@ -3244,6 +3287,7 @@ export {
3244
3287
  InkeepStream,
3245
3288
  InvalidArgumentError2 as InvalidArgumentError,
3246
3289
  InvalidDataContentError2 as InvalidDataContentError,
3290
+ InvalidMessageRoleError,
3247
3291
  InvalidModelIdError,
3248
3292
  InvalidPromptError2 as InvalidPromptError,
3249
3293
  InvalidResponseDataError,
@@ -3271,6 +3315,7 @@ export {
3271
3315
  convertDataContentToBase64String,
3272
3316
  convertDataContentToUint8Array,
3273
3317
  convertToCoreMessages,
3318
+ cosineSimilarity,
3274
3319
  createCallbacksTransformer,
3275
3320
  createEventStreamTransformer,
3276
3321
  createStreamDataTransformer,