@dudousxd/nestjs-agent-rag 0.14.1 → 0.14.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.cjs CHANGED
@@ -279,6 +279,7 @@ var MemoryVectorStore = class {
279
279
  __name(this, "MemoryVectorStore");
280
280
  }
281
281
  records = /* @__PURE__ */ new Map();
282
+ replacing = /* @__PURE__ */ new Map();
282
283
  /**
283
284
  * Telemetry self-description. No `collection`: there is no namespace to name — the corpus IS this
284
285
  * instance, and two `MemoryVectorStore`s in one process are two corpora with nothing to tell them
@@ -294,6 +295,23 @@ var MemoryVectorStore = class {
294
295
  this.records.set(record.id, record);
295
296
  }
296
297
  }
298
+ /** Replaces take turns per document. A `remove` during one isn't queued, and may be undone by it. */
299
+ async replaceDocument(documentId, records) {
300
+ const previous = this.replacing.get(documentId) ?? Promise.resolve();
301
+ const run = previous.catch(() => {
302
+ }).then(async () => {
303
+ await this.remove(documentId);
304
+ await this.upsert(records);
305
+ });
306
+ this.replacing.set(documentId, run);
307
+ try {
308
+ await run;
309
+ } finally {
310
+ if (this.replacing.get(documentId) === run) {
311
+ this.replacing.delete(documentId);
312
+ }
313
+ }
314
+ }
297
315
  async remove(documentId) {
298
316
  for (const id of this.records.keys()) {
299
317
  if (documentIdOf(id) === documentId) {
@@ -1408,7 +1426,7 @@ var RedisVectorStore = class {
1408
1426
  String(REPLACE_LOCK_TTL_MS)
1409
1427
  ]) === null) {
1410
1428
  if (Date.now() >= deadline) {
1411
- throw new Error(`RedisVectorStore: timed out after ${REPLACE_LOCK_WAIT_MS}ms waiting to replace document "${documentId}"`);
1429
+ throw new Error(`RedisVectorStore: timed out after ${REPLACE_LOCK_WAIT_MS}ms waiting to change document "${documentId}"`);
1412
1430
  }
1413
1431
  await new Promise((resolve) => setTimeout(resolve, delay));
1414
1432
  delay = Math.min(delay * 2, 250);
@@ -1416,7 +1434,7 @@ var RedisVectorStore = class {
1416
1434
  let lost = false;
1417
1435
  const assertHeld = /* @__PURE__ */ __name(() => {
1418
1436
  if (lost) {
1419
- throw new Error(`RedisVectorStore: lost the lock while replacing document "${documentId}"; call again to repair it`);
1437
+ throw new Error(`RedisVectorStore: lost the lock while changing document "${documentId}"; call again to repair it`);
1420
1438
  }
1421
1439
  }, "assertHeld");
1422
1440
  const renewal = setInterval(() => {
@@ -1476,13 +1494,15 @@ var RedisVectorStore = class {
1476
1494
  * than N scans). Neither depends on a field that older chunks lack, so neither has a migration.
1477
1495
  */
1478
1496
  async remove(documentId) {
1479
- const keys = await this.chunkKeys(documentId);
1480
- if (keys.length > 0) {
1481
- await this.client.sendCommand([
1482
- "DEL",
1483
- ...keys
1484
- ]);
1485
- }
1497
+ await this.withDocumentLock(documentId, async () => {
1498
+ const keys = await this.chunkKeys(documentId);
1499
+ if (keys.length > 0) {
1500
+ await this.client.sendCommand([
1501
+ "DEL",
1502
+ ...keys
1503
+ ]);
1504
+ }
1505
+ });
1486
1506
  }
1487
1507
  /**
1488
1508
  * Rewrite the metadata of every chunk of `documentId` in place, leaving `text` and `embedding`