@dudousxd/nestjs-agent-rag 0.10.1 → 0.10.2

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.js CHANGED
@@ -357,6 +357,27 @@ function cosineSimilarity(a, b) {
357
357
  __name(cosineSimilarity, "cosineSimilarity");
358
358
 
359
359
  // src/pg-vector-store.ts
360
+ var NUL_BYTE = String.fromCharCode(0);
361
+ function stripNulBytes(value) {
362
+ if (typeof value === "string") {
363
+ return value.includes(NUL_BYTE) ? value.split(NUL_BYTE).join("") : value;
364
+ }
365
+ if (Array.isArray(value)) {
366
+ return value.map((item) => stripNulBytes(item));
367
+ }
368
+ if (value !== null && typeof value === "object") {
369
+ const proto = Object.getPrototypeOf(value);
370
+ if (proto !== Object.prototype && proto !== null) {
371
+ return value;
372
+ }
373
+ return Object.fromEntries(Object.entries(value).map(([key, entry]) => [
374
+ stripNulBytes(key),
375
+ stripNulBytes(entry)
376
+ ]));
377
+ }
378
+ return value;
379
+ }
380
+ __name(stripNulBytes, "stripNulBytes");
360
381
  var PgVectorStore = class {
361
382
  static {
362
383
  __name(this, "PgVectorStore");
@@ -391,6 +412,10 @@ var PgVectorStore = class {
391
412
  }
392
413
  async upsert(records) {
393
414
  for (const record of records) {
415
+ const id = stripNulBytes(record.id);
416
+ const text = stripNulBytes(record.text);
417
+ const source = record.source !== void 0 ? stripNulBytes(record.source) : void 0;
418
+ const metadata = record.metadata !== void 0 ? stripNulBytes(record.metadata) : void 0;
394
419
  await this.client.query(`INSERT INTO ${this.table} (id, text, source, metadata, embedding)
395
420
  VALUES ($1, $2, $3, $4, $5)
396
421
  ON CONFLICT (id) DO UPDATE SET
@@ -398,17 +423,17 @@ var PgVectorStore = class {
398
423
  source = EXCLUDED.source,
399
424
  metadata = EXCLUDED.metadata,
400
425
  embedding = EXCLUDED.embedding`, [
401
- record.id,
402
- record.text,
403
- record.source ?? null,
404
- record.metadata !== void 0 ? JSON.stringify(record.metadata) : null,
426
+ id,
427
+ text,
428
+ source ?? null,
429
+ metadata !== void 0 ? JSON.stringify(metadata) : null,
405
430
  toVectorLiteral(record.embedding)
406
431
  ]);
407
432
  }
408
433
  }
409
434
  async remove(documentId) {
410
435
  await this.client.query(`DELETE FROM ${this.table} WHERE ${DOCUMENT_ID_FROM_CHUNK} = $1`, [
411
- documentId
436
+ stripNulBytes(documentId)
412
437
  ]);
413
438
  }
414
439
  /**
@@ -427,12 +452,12 @@ var PgVectorStore = class {
427
452
  if (isEmptyMetadataPatch(patch)) {
428
453
  return 0;
429
454
  }
430
- const { set, remove } = splitMetadataPatch(patch);
455
+ const { set, remove } = splitMetadataPatch(stripNulBytes(patch));
431
456
  const rows = await this.client.query(`UPDATE ${this.table}
432
457
  SET metadata = (COALESCE(metadata, '{}'::jsonb) || $2::jsonb) - $3::text[]
433
458
  WHERE ${DOCUMENT_ID_FROM_CHUNK} = $1
434
459
  RETURNING id`, [
435
- documentId,
460
+ stripNulBytes(documentId),
436
461
  JSON.stringify(set),
437
462
  remove
438
463
  ]);
@@ -465,7 +490,7 @@ var PgVectorStore = class {
465
490
  */
466
491
  async listChunks(documentId, options) {
467
492
  const params = [
468
- documentId
493
+ stripNulBytes(documentId)
469
494
  ];
470
495
  let sql = `SELECT id, ${CHUNK_INDEX_FROM_ID} AS chunk_index, text, metadata
471
496
  FROM ${this.table}
@@ -507,7 +532,7 @@ var PgVectorStore = class {
507
532
  return;
508
533
  }
509
534
  await this.client.query(`DELETE FROM ${this.table} WHERE ${DOCUMENT_ID_FROM_CHUNK} = ANY($1::text[])`, [
510
- documentIds
535
+ stripNulBytes(documentIds)
511
536
  ]);
512
537
  }
513
538
  /**
@@ -566,11 +591,12 @@ function buildWhere(filter, startIndex) {
566
591
  params: []
567
592
  };
568
593
  }
594
+ const stripped = stripNulBytes(filter);
569
595
  const clauses = [];
570
596
  const params = [];
571
597
  const scalar = {};
572
598
  let index = startIndex;
573
- for (const [key, value] of Object.entries(filter)) {
599
+ for (const [key, value] of Object.entries(stripped)) {
574
600
  if (Array.isArray(value)) {
575
601
  if (value.length === 0) {
576
602
  clauses.push("false");