ai 4.1.65 → 4.1.66

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
@@ -7357,37 +7357,36 @@ var MCPClient = class {
7357
7357
  };
7358
7358
 
7359
7359
  // core/util/cosine-similarity.ts
7360
- function cosineSimilarity(vector1, vector2, options = {
7361
- throwErrorForEmptyVectors: false
7362
- }) {
7363
- const { throwErrorForEmptyVectors } = options;
7360
+ function cosineSimilarity(vector1, vector2, options) {
7364
7361
  if (vector1.length !== vector2.length) {
7365
- throw new Error(
7366
- `Vectors must have the same length (vector1: ${vector1.length} elements, vector2: ${vector2.length} elements)`
7367
- );
7368
- }
7369
- if (throwErrorForEmptyVectors && vector1.length === 0) {
7370
7362
  throw new InvalidArgumentError({
7371
- parameter: "vector1",
7372
- value: vector1,
7373
- message: "Vectors cannot be empty"
7363
+ parameter: "vector1,vector2",
7364
+ value: { vector1Length: vector1.length, vector2Length: vector2.length },
7365
+ message: `Vectors must have the same length`
7374
7366
  });
7375
7367
  }
7376
- const magnitude1 = magnitude(vector1);
7377
- const magnitude2 = magnitude(vector2);
7378
- if (magnitude1 === 0 || magnitude2 === 0) {
7368
+ const n = vector1.length;
7369
+ if (n === 0) {
7370
+ if (options == null ? void 0 : options.throwErrorForEmptyVectors) {
7371
+ throw new InvalidArgumentError({
7372
+ parameter: "vector1",
7373
+ value: vector1,
7374
+ message: "Vectors cannot be empty"
7375
+ });
7376
+ }
7379
7377
  return 0;
7380
7378
  }
7381
- return dotProduct(vector1, vector2) / (magnitude1 * magnitude2);
7382
- }
7383
- function dotProduct(vector1, vector2) {
7384
- return vector1.reduce(
7385
- (accumulator, value, index) => accumulator + value * vector2[index],
7386
- 0
7387
- );
7388
- }
7389
- function magnitude(vector) {
7390
- return Math.sqrt(dotProduct(vector, vector));
7379
+ let magnitudeSquared1 = 0;
7380
+ let magnitudeSquared2 = 0;
7381
+ let dotProduct = 0;
7382
+ for (let i = 0; i < n; i++) {
7383
+ const value1 = vector1[i];
7384
+ const value2 = vector2[i];
7385
+ magnitudeSquared1 += value1 * value1;
7386
+ magnitudeSquared2 += value2 * value2;
7387
+ dotProduct += value1 * value2;
7388
+ }
7389
+ return magnitudeSquared1 === 0 || magnitudeSquared2 === 0 ? 0 : dotProduct / (Math.sqrt(magnitudeSquared1) * Math.sqrt(magnitudeSquared2));
7391
7390
  }
7392
7391
 
7393
7392
  // core/util/simulate-readable-stream.ts