@dudousxd/nestjs-agent-rag 0.10.1 → 0.10.3
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.cjs +36 -10
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +36 -10
- package/dist/index.js.map +1 -1
- package/package.json +3 -3
package/dist/index.cjs
CHANGED
|
@@ -409,6 +409,27 @@ function cosineSimilarity(a, b) {
|
|
|
409
409
|
__name(cosineSimilarity, "cosineSimilarity");
|
|
410
410
|
|
|
411
411
|
// src/pg-vector-store.ts
|
|
412
|
+
var NUL_BYTE = String.fromCharCode(0);
|
|
413
|
+
function stripNulBytes(value) {
|
|
414
|
+
if (typeof value === "string") {
|
|
415
|
+
return value.includes(NUL_BYTE) ? value.split(NUL_BYTE).join("") : value;
|
|
416
|
+
}
|
|
417
|
+
if (Array.isArray(value)) {
|
|
418
|
+
return value.map((item) => stripNulBytes(item));
|
|
419
|
+
}
|
|
420
|
+
if (value !== null && typeof value === "object") {
|
|
421
|
+
const proto = Object.getPrototypeOf(value);
|
|
422
|
+
if (proto !== Object.prototype && proto !== null) {
|
|
423
|
+
return value;
|
|
424
|
+
}
|
|
425
|
+
return Object.fromEntries(Object.entries(value).map(([key, entry]) => [
|
|
426
|
+
stripNulBytes(key),
|
|
427
|
+
stripNulBytes(entry)
|
|
428
|
+
]));
|
|
429
|
+
}
|
|
430
|
+
return value;
|
|
431
|
+
}
|
|
432
|
+
__name(stripNulBytes, "stripNulBytes");
|
|
412
433
|
var PgVectorStore = class {
|
|
413
434
|
static {
|
|
414
435
|
__name(this, "PgVectorStore");
|
|
@@ -443,6 +464,10 @@ var PgVectorStore = class {
|
|
|
443
464
|
}
|
|
444
465
|
async upsert(records) {
|
|
445
466
|
for (const record of records) {
|
|
467
|
+
const id = stripNulBytes(record.id);
|
|
468
|
+
const text = stripNulBytes(record.text);
|
|
469
|
+
const source = record.source !== void 0 ? stripNulBytes(record.source) : void 0;
|
|
470
|
+
const metadata = record.metadata !== void 0 ? stripNulBytes(record.metadata) : void 0;
|
|
446
471
|
await this.client.query(`INSERT INTO ${this.table} (id, text, source, metadata, embedding)
|
|
447
472
|
VALUES ($1, $2, $3, $4, $5)
|
|
448
473
|
ON CONFLICT (id) DO UPDATE SET
|
|
@@ -450,17 +475,17 @@ var PgVectorStore = class {
|
|
|
450
475
|
source = EXCLUDED.source,
|
|
451
476
|
metadata = EXCLUDED.metadata,
|
|
452
477
|
embedding = EXCLUDED.embedding`, [
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
478
|
+
id,
|
|
479
|
+
text,
|
|
480
|
+
source ?? null,
|
|
481
|
+
metadata !== void 0 ? JSON.stringify(metadata) : null,
|
|
457
482
|
toVectorLiteral(record.embedding)
|
|
458
483
|
]);
|
|
459
484
|
}
|
|
460
485
|
}
|
|
461
486
|
async remove(documentId) {
|
|
462
487
|
await this.client.query(`DELETE FROM ${this.table} WHERE ${DOCUMENT_ID_FROM_CHUNK} = $1`, [
|
|
463
|
-
documentId
|
|
488
|
+
stripNulBytes(documentId)
|
|
464
489
|
]);
|
|
465
490
|
}
|
|
466
491
|
/**
|
|
@@ -479,12 +504,12 @@ var PgVectorStore = class {
|
|
|
479
504
|
if (isEmptyMetadataPatch(patch)) {
|
|
480
505
|
return 0;
|
|
481
506
|
}
|
|
482
|
-
const { set, remove } = splitMetadataPatch(patch);
|
|
507
|
+
const { set, remove } = splitMetadataPatch(stripNulBytes(patch));
|
|
483
508
|
const rows = await this.client.query(`UPDATE ${this.table}
|
|
484
509
|
SET metadata = (COALESCE(metadata, '{}'::jsonb) || $2::jsonb) - $3::text[]
|
|
485
510
|
WHERE ${DOCUMENT_ID_FROM_CHUNK} = $1
|
|
486
511
|
RETURNING id`, [
|
|
487
|
-
documentId,
|
|
512
|
+
stripNulBytes(documentId),
|
|
488
513
|
JSON.stringify(set),
|
|
489
514
|
remove
|
|
490
515
|
]);
|
|
@@ -517,7 +542,7 @@ var PgVectorStore = class {
|
|
|
517
542
|
*/
|
|
518
543
|
async listChunks(documentId, options) {
|
|
519
544
|
const params = [
|
|
520
|
-
documentId
|
|
545
|
+
stripNulBytes(documentId)
|
|
521
546
|
];
|
|
522
547
|
let sql = `SELECT id, ${CHUNK_INDEX_FROM_ID} AS chunk_index, text, metadata
|
|
523
548
|
FROM ${this.table}
|
|
@@ -559,7 +584,7 @@ var PgVectorStore = class {
|
|
|
559
584
|
return;
|
|
560
585
|
}
|
|
561
586
|
await this.client.query(`DELETE FROM ${this.table} WHERE ${DOCUMENT_ID_FROM_CHUNK} = ANY($1::text[])`, [
|
|
562
|
-
documentIds
|
|
587
|
+
stripNulBytes(documentIds)
|
|
563
588
|
]);
|
|
564
589
|
}
|
|
565
590
|
/**
|
|
@@ -618,11 +643,12 @@ function buildWhere(filter, startIndex) {
|
|
|
618
643
|
params: []
|
|
619
644
|
};
|
|
620
645
|
}
|
|
646
|
+
const stripped = stripNulBytes(filter);
|
|
621
647
|
const clauses = [];
|
|
622
648
|
const params = [];
|
|
623
649
|
const scalar = {};
|
|
624
650
|
let index = startIndex;
|
|
625
|
-
for (const [key, value] of Object.entries(
|
|
651
|
+
for (const [key, value] of Object.entries(stripped)) {
|
|
626
652
|
if (Array.isArray(value)) {
|
|
627
653
|
if (value.length === 0) {
|
|
628
654
|
clauses.push("false");
|