@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.d.cts CHANGED
@@ -407,8 +407,9 @@ declare function describeSharedSource(legs: readonly unknown[]): Omit<RetrievalD
407
407
  * (`removeWhere` refusing an empty filter, an empty-array filter deleting nothing) are behavioural,
408
408
  * not incidental to Redis.
409
409
  */
410
- declare class MemoryVectorStore implements VectorStore {
410
+ declare class MemoryVectorStore implements AtomicReplaceVectorStore {
411
411
  private readonly records;
412
+ private readonly replacing;
412
413
  /**
413
414
  * Telemetry self-description. No `collection`: there is no namespace to name — the corpus IS this
414
415
  * instance, and two `MemoryVectorStore`s in one process are two corpora with nothing to tell them
@@ -416,6 +417,8 @@ declare class MemoryVectorStore implements VectorStore {
416
417
  */
417
418
  describeRetrieval(): RetrievalDescriptor;
418
419
  upsert(records: VectorRecord[]): Promise<void>;
420
+ /** Replaces take turns per document. A `remove` during one isn't queued, and may be undone by it. */
421
+ replaceDocument(documentId: string, records: VectorRecord[]): Promise<void>;
419
422
  remove(documentId: string): Promise<void>;
420
423
  /**
421
424
  * Merge `patch` into every chunk of `documentId`, leaving `text` and `embedding` untouched. The
package/dist/index.d.ts CHANGED
@@ -407,8 +407,9 @@ declare function describeSharedSource(legs: readonly unknown[]): Omit<RetrievalD
407
407
  * (`removeWhere` refusing an empty filter, an empty-array filter deleting nothing) are behavioural,
408
408
  * not incidental to Redis.
409
409
  */
410
- declare class MemoryVectorStore implements VectorStore {
410
+ declare class MemoryVectorStore implements AtomicReplaceVectorStore {
411
411
  private readonly records;
412
+ private readonly replacing;
412
413
  /**
413
414
  * Telemetry self-description. No `collection`: there is no namespace to name — the corpus IS this
414
415
  * instance, and two `MemoryVectorStore`s in one process are two corpora with nothing to tell them
@@ -416,6 +417,8 @@ declare class MemoryVectorStore implements VectorStore {
416
417
  */
417
418
  describeRetrieval(): RetrievalDescriptor;
418
419
  upsert(records: VectorRecord[]): Promise<void>;
420
+ /** Replaces take turns per document. A `remove` during one isn't queued, and may be undone by it. */
421
+ replaceDocument(documentId: string, records: VectorRecord[]): Promise<void>;
419
422
  remove(documentId: string): Promise<void>;
420
423
  /**
421
424
  * Merge `patch` into every chunk of `documentId`, leaving `text` and `embedding` untouched. The
package/dist/index.js CHANGED
@@ -191,6 +191,7 @@ var MemoryVectorStore = class {
191
191
  __name(this, "MemoryVectorStore");
192
192
  }
193
193
  records = /* @__PURE__ */ new Map();
194
+ replacing = /* @__PURE__ */ new Map();
194
195
  /**
195
196
  * Telemetry self-description. No `collection`: there is no namespace to name — the corpus IS this
196
197
  * instance, and two `MemoryVectorStore`s in one process are two corpora with nothing to tell them
@@ -206,6 +207,23 @@ var MemoryVectorStore = class {
206
207
  this.records.set(record.id, record);
207
208
  }
208
209
  }
210
+ /** Replaces take turns per document. A `remove` during one isn't queued, and may be undone by it. */
211
+ async replaceDocument(documentId, records) {
212
+ const previous = this.replacing.get(documentId) ?? Promise.resolve();
213
+ const run = previous.catch(() => {
214
+ }).then(async () => {
215
+ await this.remove(documentId);
216
+ await this.upsert(records);
217
+ });
218
+ this.replacing.set(documentId, run);
219
+ try {
220
+ await run;
221
+ } finally {
222
+ if (this.replacing.get(documentId) === run) {
223
+ this.replacing.delete(documentId);
224
+ }
225
+ }
226
+ }
209
227
  async remove(documentId) {
210
228
  for (const id of this.records.keys()) {
211
229
  if (documentIdOf(id) === documentId) {
@@ -1320,7 +1338,7 @@ var RedisVectorStore = class {
1320
1338
  String(REPLACE_LOCK_TTL_MS)
1321
1339
  ]) === null) {
1322
1340
  if (Date.now() >= deadline) {
1323
- throw new Error(`RedisVectorStore: timed out after ${REPLACE_LOCK_WAIT_MS}ms waiting to replace document "${documentId}"`);
1341
+ throw new Error(`RedisVectorStore: timed out after ${REPLACE_LOCK_WAIT_MS}ms waiting to change document "${documentId}"`);
1324
1342
  }
1325
1343
  await new Promise((resolve) => setTimeout(resolve, delay));
1326
1344
  delay = Math.min(delay * 2, 250);
@@ -1328,7 +1346,7 @@ var RedisVectorStore = class {
1328
1346
  let lost = false;
1329
1347
  const assertHeld = /* @__PURE__ */ __name(() => {
1330
1348
  if (lost) {
1331
- throw new Error(`RedisVectorStore: lost the lock while replacing document "${documentId}"; call again to repair it`);
1349
+ throw new Error(`RedisVectorStore: lost the lock while changing document "${documentId}"; call again to repair it`);
1332
1350
  }
1333
1351
  }, "assertHeld");
1334
1352
  const renewal = setInterval(() => {
@@ -1388,13 +1406,15 @@ var RedisVectorStore = class {
1388
1406
  * than N scans). Neither depends on a field that older chunks lack, so neither has a migration.
1389
1407
  */
1390
1408
  async remove(documentId) {
1391
- const keys = await this.chunkKeys(documentId);
1392
- if (keys.length > 0) {
1393
- await this.client.sendCommand([
1394
- "DEL",
1395
- ...keys
1396
- ]);
1397
- }
1409
+ await this.withDocumentLock(documentId, async () => {
1410
+ const keys = await this.chunkKeys(documentId);
1411
+ if (keys.length > 0) {
1412
+ await this.client.sendCommand([
1413
+ "DEL",
1414
+ ...keys
1415
+ ]);
1416
+ }
1417
+ });
1398
1418
  }
1399
1419
  /**
1400
1420
  * Rewrite the metadata of every chunk of `documentId` in place, leaving `text` and `embedding`