ai 4.1.65 → 4.2.0

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,25 @@
1
1
  # ai
2
2
 
3
+ ## 4.2.0
4
+
5
+ ### Minor Changes
6
+
7
+ - 5bc638d: AI SDK 4.2
8
+
9
+ ### Patch Changes
10
+
11
+ - Updated dependencies [5bc638d]
12
+ - @ai-sdk/provider@1.1.0
13
+ - @ai-sdk/provider-utils@2.2.0
14
+ - @ai-sdk/react@1.2.0
15
+ - @ai-sdk/ui-utils@1.2.0
16
+
17
+ ## 4.1.66
18
+
19
+ ### Patch Changes
20
+
21
+ - 5d0fc29: chore (ai): improve cosine similarity calculation
22
+
3
23
  ## 4.1.65
4
24
 
5
25
  ### Patch Changes
package/dist/index.d.mts CHANGED
@@ -3832,10 +3832,14 @@ declare function experimental_createProviderRegistry(providers: Record<string, P
3832
3832
  *
3833
3833
  * @returns The cosine similarity between vector1 and vector2.
3834
3834
  * @returns 0 if either vector is the zero vector.
3835
+ *
3835
3836
  * @throws {InvalidArgumentError} If throwErrorForEmptyVectors is true and vectors are empty.
3836
- * @throws {Error} If the vectors do not have the same length.
3837
+ * @throws {InvalidArgumentError} If the vectors do not have the same length.
3837
3838
  */
3838
3839
  declare function cosineSimilarity(vector1: number[], vector2: number[], options?: {
3840
+ /**
3841
+ * @deprecated will be removed in 5.0
3842
+ */
3839
3843
  throwErrorForEmptyVectors?: boolean;
3840
3844
  }): number;
3841
3845
 
package/dist/index.d.ts CHANGED
@@ -3832,10 +3832,14 @@ declare function experimental_createProviderRegistry(providers: Record<string, P
3832
3832
  *
3833
3833
  * @returns The cosine similarity between vector1 and vector2.
3834
3834
  * @returns 0 if either vector is the zero vector.
3835
+ *
3835
3836
  * @throws {InvalidArgumentError} If throwErrorForEmptyVectors is true and vectors are empty.
3836
- * @throws {Error} If the vectors do not have the same length.
3837
+ * @throws {InvalidArgumentError} If the vectors do not have the same length.
3837
3838
  */
3838
3839
  declare function cosineSimilarity(vector1: number[], vector2: number[], options?: {
3840
+ /**
3841
+ * @deprecated will be removed in 5.0
3842
+ */
3839
3843
  throwErrorForEmptyVectors?: boolean;
3840
3844
  }): number;
3841
3845
 
package/dist/index.js CHANGED
@@ -7406,37 +7406,36 @@ var MCPClient = class {
7406
7406
  };
7407
7407
 
7408
7408
  // core/util/cosine-similarity.ts
7409
- function cosineSimilarity(vector1, vector2, options = {
7410
- throwErrorForEmptyVectors: false
7411
- }) {
7412
- const { throwErrorForEmptyVectors } = options;
7409
+ function cosineSimilarity(vector1, vector2, options) {
7413
7410
  if (vector1.length !== vector2.length) {
7414
- throw new Error(
7415
- `Vectors must have the same length (vector1: ${vector1.length} elements, vector2: ${vector2.length} elements)`
7416
- );
7417
- }
7418
- if (throwErrorForEmptyVectors && vector1.length === 0) {
7419
7411
  throw new InvalidArgumentError({
7420
- parameter: "vector1",
7421
- value: vector1,
7422
- message: "Vectors cannot be empty"
7412
+ parameter: "vector1,vector2",
7413
+ value: { vector1Length: vector1.length, vector2Length: vector2.length },
7414
+ message: `Vectors must have the same length`
7423
7415
  });
7424
7416
  }
7425
- const magnitude1 = magnitude(vector1);
7426
- const magnitude2 = magnitude(vector2);
7427
- if (magnitude1 === 0 || magnitude2 === 0) {
7417
+ const n = vector1.length;
7418
+ if (n === 0) {
7419
+ if (options == null ? void 0 : options.throwErrorForEmptyVectors) {
7420
+ throw new InvalidArgumentError({
7421
+ parameter: "vector1",
7422
+ value: vector1,
7423
+ message: "Vectors cannot be empty"
7424
+ });
7425
+ }
7428
7426
  return 0;
7429
7427
  }
7430
- return dotProduct(vector1, vector2) / (magnitude1 * magnitude2);
7431
- }
7432
- function dotProduct(vector1, vector2) {
7433
- return vector1.reduce(
7434
- (accumulator, value, index) => accumulator + value * vector2[index],
7435
- 0
7436
- );
7437
- }
7438
- function magnitude(vector) {
7439
- return Math.sqrt(dotProduct(vector, vector));
7428
+ let magnitudeSquared1 = 0;
7429
+ let magnitudeSquared2 = 0;
7430
+ let dotProduct = 0;
7431
+ for (let i = 0; i < n; i++) {
7432
+ const value1 = vector1[i];
7433
+ const value2 = vector2[i];
7434
+ magnitudeSquared1 += value1 * value1;
7435
+ magnitudeSquared2 += value2 * value2;
7436
+ dotProduct += value1 * value2;
7437
+ }
7438
+ return magnitudeSquared1 === 0 || magnitudeSquared2 === 0 ? 0 : dotProduct / (Math.sqrt(magnitudeSquared1) * Math.sqrt(magnitudeSquared2));
7440
7439
  }
7441
7440
 
7442
7441
  // core/util/simulate-readable-stream.ts