@dudousxd/nestjs-agent-rag 0.14.0 → 0.14.1
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/README.md +2 -1
- package/dist/index.cjs +108 -3
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +25 -13
- package/dist/index.d.ts +25 -13
- package/dist/index.js +108 -3
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -481,7 +481,8 @@ array is refused so a request can't widen a search. A corpus shared by everyone
|
|
|
481
481
|
|
|
482
482
|
**Saving doesn't index.** Embedding is slow and paid for, so call `index` from a job or wherever the
|
|
483
483
|
text changed. It embeds first, so a provider failure keeps the old chunks. On `PgVectorStore` the swap is
|
|
484
|
-
one transaction
|
|
484
|
+
one transaction. On Redis, a search can briefly see both versions, but concurrent calls wait for each
|
|
485
|
+
other, so they can't leave a mix behind. On stores without `replaceDocument`, it is two statements.
|
|
485
486
|
|
|
486
487
|
**Deleting.** With Drizzle, use `.returning()` and call `remove`. With MikroORM, add
|
|
487
488
|
`provideRagDeleteSync(ProjectDocument, DocumentsRag)` from `@dudousxd/nestjs-agent-store-mikro-orm` to
|
package/dist/index.cjs
CHANGED
|
@@ -767,6 +767,10 @@ var PgVectorStore = class {
|
|
|
767
767
|
/**
|
|
768
768
|
* Swaps a document's chunks in one transaction, so a crash can't lose it and a search sees the old
|
|
769
769
|
* version or the new one. A client without `transaction` falls back to two statements.
|
|
770
|
+
*
|
|
771
|
+
* Concurrent swaps of one document take turns on an advisory lock. Without it the second `DELETE`
|
|
772
|
+
* can't see the rows the first just committed, so the first's tail survives next to the second's
|
|
773
|
+
* chunks, and overlapping writes can deadlock.
|
|
770
774
|
*/
|
|
771
775
|
async replaceDocument(documentId, records) {
|
|
772
776
|
const swap = /* @__PURE__ */ __name(async (client) => {
|
|
@@ -776,7 +780,16 @@ var PgVectorStore = class {
|
|
|
776
780
|
await this.upsertWith(client, records);
|
|
777
781
|
}, "swap");
|
|
778
782
|
const transaction = this.client.transaction?.bind(this.client);
|
|
779
|
-
|
|
783
|
+
if (transaction === void 0) {
|
|
784
|
+
await swap(this.client);
|
|
785
|
+
return;
|
|
786
|
+
}
|
|
787
|
+
await transaction(async (client) => {
|
|
788
|
+
await client.query("SELECT pg_advisory_xact_lock(hashtextextended($1, 0))", [
|
|
789
|
+
`${this.table}:${stripNulBytes(documentId)}`
|
|
790
|
+
]);
|
|
791
|
+
await swap(client);
|
|
792
|
+
});
|
|
780
793
|
}
|
|
781
794
|
async upsertWith(client, records) {
|
|
782
795
|
const byId = /* @__PURE__ */ new Map();
|
|
@@ -1191,6 +1204,11 @@ function toVectorLiteral(embedding) {
|
|
|
1191
1204
|
__name(toVectorLiteral, "toVectorLiteral");
|
|
1192
1205
|
|
|
1193
1206
|
// src/redis-vector-store.ts
|
|
1207
|
+
var import_node_crypto = require("crypto");
|
|
1208
|
+
var REPLACE_LOCK_TTL_MS = 3e4;
|
|
1209
|
+
var REPLACE_LOCK_WAIT_MS = 12e4;
|
|
1210
|
+
var RENEW_LOCK_SCRIPT = "if redis.call('GET', KEYS[1]) == ARGV[1] then return redis.call('PEXPIRE', KEYS[1], ARGV[2]) end return 0";
|
|
1211
|
+
var RELEASE_LOCK_SCRIPT = "if redis.call('GET', KEYS[1]) == ARGV[1] then return redis.call('DEL', KEYS[1]) end return 0";
|
|
1194
1212
|
var RedisVectorSchemaMismatchError = class extends Error {
|
|
1195
1213
|
static {
|
|
1196
1214
|
__name(this, "RedisVectorSchemaMismatchError");
|
|
@@ -1352,6 +1370,92 @@ var RedisVectorStore = class {
|
|
|
1352
1370
|
}
|
|
1353
1371
|
}
|
|
1354
1372
|
/**
|
|
1373
|
+
* Replaces a document's chunks, one writer at a time per document. Redis can't swap them in one step
|
|
1374
|
+
* without scanning the keyspace inside a script, so a search may briefly see old and new chunks
|
|
1375
|
+
* together, but concurrent calls can't leave them mixed. The new chunks go in before the old ones
|
|
1376
|
+
* come out, so a crash leaves the document searchable and the next call repairs it.
|
|
1377
|
+
*/
|
|
1378
|
+
async replaceDocument(documentId, records) {
|
|
1379
|
+
await this.withDocumentLock(documentId, async (assertHeld) => {
|
|
1380
|
+
await this.upsert(records);
|
|
1381
|
+
assertHeld();
|
|
1382
|
+
const current = new Set(records.map((record) => `${this.prefix}${record.id}`));
|
|
1383
|
+
const stale = (await this.chunkKeys(documentId)).filter((key) => !current.has(key));
|
|
1384
|
+
assertHeld();
|
|
1385
|
+
if (stale.length > 0) {
|
|
1386
|
+
await this.client.sendCommand([
|
|
1387
|
+
"DEL",
|
|
1388
|
+
...stale
|
|
1389
|
+
]);
|
|
1390
|
+
}
|
|
1391
|
+
});
|
|
1392
|
+
}
|
|
1393
|
+
/**
|
|
1394
|
+
* A lease, so a crashed writer can't block the document forever. It is renewed while the work runs,
|
|
1395
|
+
* which keeps a large document from outliving it.
|
|
1396
|
+
*/
|
|
1397
|
+
async withDocumentLock(documentId, work) {
|
|
1398
|
+
const key = `${this.index}:replace-lock:${documentId}`;
|
|
1399
|
+
const token = (0, import_node_crypto.randomUUID)();
|
|
1400
|
+
const deadline = Date.now() + REPLACE_LOCK_WAIT_MS;
|
|
1401
|
+
let delay = 10;
|
|
1402
|
+
while (await this.client.sendCommand([
|
|
1403
|
+
"SET",
|
|
1404
|
+
key,
|
|
1405
|
+
token,
|
|
1406
|
+
"NX",
|
|
1407
|
+
"PX",
|
|
1408
|
+
String(REPLACE_LOCK_TTL_MS)
|
|
1409
|
+
]) === null) {
|
|
1410
|
+
if (Date.now() >= deadline) {
|
|
1411
|
+
throw new Error(`RedisVectorStore: timed out after ${REPLACE_LOCK_WAIT_MS}ms waiting to replace document "${documentId}"`);
|
|
1412
|
+
}
|
|
1413
|
+
await new Promise((resolve) => setTimeout(resolve, delay));
|
|
1414
|
+
delay = Math.min(delay * 2, 250);
|
|
1415
|
+
}
|
|
1416
|
+
let lost = false;
|
|
1417
|
+
const assertHeld = /* @__PURE__ */ __name(() => {
|
|
1418
|
+
if (lost) {
|
|
1419
|
+
throw new Error(`RedisVectorStore: lost the lock while replacing document "${documentId}"; call again to repair it`);
|
|
1420
|
+
}
|
|
1421
|
+
}, "assertHeld");
|
|
1422
|
+
const renewal = setInterval(() => {
|
|
1423
|
+
void Promise.resolve(this.client.sendCommand([
|
|
1424
|
+
"EVAL",
|
|
1425
|
+
RENEW_LOCK_SCRIPT,
|
|
1426
|
+
"1",
|
|
1427
|
+
key,
|
|
1428
|
+
token,
|
|
1429
|
+
String(REPLACE_LOCK_TTL_MS)
|
|
1430
|
+
])).then(
|
|
1431
|
+
(renewed) => {
|
|
1432
|
+
if (Number(renewed) === 0) {
|
|
1433
|
+
lost = true;
|
|
1434
|
+
}
|
|
1435
|
+
},
|
|
1436
|
+
// A failed renewal isn't a lost lock yet: the lease may still have time left.
|
|
1437
|
+
() => {
|
|
1438
|
+
}
|
|
1439
|
+
);
|
|
1440
|
+
}, REPLACE_LOCK_TTL_MS / 3);
|
|
1441
|
+
renewal.unref?.();
|
|
1442
|
+
try {
|
|
1443
|
+
const result = await work(assertHeld);
|
|
1444
|
+
assertHeld();
|
|
1445
|
+
return result;
|
|
1446
|
+
} finally {
|
|
1447
|
+
clearInterval(renewal);
|
|
1448
|
+
await Promise.resolve(this.client.sendCommand([
|
|
1449
|
+
"EVAL",
|
|
1450
|
+
RELEASE_LOCK_SCRIPT,
|
|
1451
|
+
"1",
|
|
1452
|
+
key,
|
|
1453
|
+
token
|
|
1454
|
+
])).catch(() => {
|
|
1455
|
+
});
|
|
1456
|
+
}
|
|
1457
|
+
}
|
|
1458
|
+
/**
|
|
1355
1459
|
* Single-document deletion, by `SCAN … MATCH`.
|
|
1356
1460
|
*
|
|
1357
1461
|
* This deliberately stays a keyspace scan even though the rest of this class no longer needs one.
|
|
@@ -2905,8 +3009,9 @@ var RagIndex = class {
|
|
|
2905
3009
|
}
|
|
2906
3010
|
/**
|
|
2907
3011
|
* Replaces the row's chunks and returns how many were written. Embeddings come first: if the
|
|
2908
|
-
* provider fails, the previous chunks are still there. The swap is one transaction on Postgres
|
|
2909
|
-
*
|
|
3012
|
+
* provider fails, the previous chunks are still there. The swap is one transaction on Postgres. Redis
|
|
3013
|
+
* adds the new chunks before dropping the old, so a search can briefly see both. On a store without
|
|
3014
|
+
* `replaceDocument`, a crash mid-swap loses the document until the next `index`.
|
|
2910
3015
|
*/
|
|
2911
3016
|
async index(row) {
|
|
2912
3017
|
const { definition, deps } = this;
|