@anuma/sdk 9.1.4 → 9.1.5

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.
@@ -15652,15 +15652,16 @@ async function refreshEncryptionKeyIfMatches(walletAddress, probeCiphertext, sig
15652
15652
  if (!parsed) {
15653
15653
  return false;
15654
15654
  }
15655
+ const startEpoch = sessionEpoch;
15655
15656
  const cached = refreshCandidatesByAddress.get(walletAddress);
15656
15657
  if (cached) {
15657
- return tryCommitCandidatesIfProbeMatches(walletAddress, parsed, cached);
15658
+ return tryCommitCandidatesIfProbeMatches(walletAddress, parsed, cached, startEpoch);
15658
15659
  }
15659
15660
  let derivePromise = pendingCandidateDerives.get(walletAddress);
15660
15661
  if (!derivePromise) {
15661
15662
  derivePromise = (async () => {
15662
15663
  const candidates2 = await deriveRefreshCandidates(signMessage, embeddedWalletSigner);
15663
- if (candidates2) {
15664
+ if (candidates2 && sessionEpoch === startEpoch) {
15664
15665
  refreshCandidatesByAddress.set(walletAddress, candidates2);
15665
15666
  }
15666
15667
  return candidates2;
@@ -15672,10 +15673,10 @@ async function refreshEncryptionKeyIfMatches(walletAddress, probeCiphertext, sig
15672
15673
  pendingCandidateDerives.set(walletAddress, derivePromise);
15673
15674
  }
15674
15675
  const candidates = await derivePromise;
15675
- if (!candidates) {
15676
+ if (!candidates || sessionEpoch !== startEpoch) {
15676
15677
  return false;
15677
15678
  }
15678
- return tryCommitCandidatesIfProbeMatches(walletAddress, parsed, candidates);
15679
+ return tryCommitCandidatesIfProbeMatches(walletAddress, parsed, candidates, startEpoch);
15679
15680
  }
15680
15681
  function parsePrefixedCiphertext(probeCiphertext) {
15681
15682
  const version = probeCiphertext.startsWith("enc:v3:") ? "v3" : probeCiphertext.startsWith("enc:v2:") ? "v2" : null;
@@ -15718,7 +15719,7 @@ async function deriveRefreshCandidates(signMessage, embeddedWalletSigner) {
15718
15719
  }
15719
15720
  return { legacy: legacyKey, current: currentKey };
15720
15721
  }
15721
- async function tryCommitCandidatesIfProbeMatches(walletAddress, parsed, candidates) {
15722
+ async function tryCommitCandidatesIfProbeMatches(walletAddress, parsed, candidates, startEpoch) {
15722
15723
  const candidateHex = parsed.version === "v2" ? candidates.legacy : candidates.current;
15723
15724
  const keyBytes = hexToBytes(candidateHex);
15724
15725
  const candidateCryptoKey = await crypto.subtle.importKey(
@@ -15736,6 +15737,9 @@ async function tryCommitCandidatesIfProbeMatches(walletAddress, parsed, candidat
15736
15737
  );
15737
15738
  return false;
15738
15739
  }
15740
+ if (sessionEpoch !== startEpoch) {
15741
+ return false;
15742
+ }
15739
15743
  setStoredKey(walletAddress, { legacy: candidates.legacy, current: candidates.current });
15740
15744
  cryptoKeyCache.delete(`${walletAddress}:v2`);
15741
15745
  cryptoKeyCache.delete(`${walletAddress}:v3`);
@@ -29821,6 +29825,104 @@ function validate(parsed, originalQuery) {
29821
29825
  return { mode, subQueries };
29822
29826
  }
29823
29827
 
29828
+ // src/lib/memoryVault/vaultPreparation.ts
29829
+ function createVaultPreparation() {
29830
+ return { corpora: /* @__PURE__ */ new WeakMap(), rows: /* @__PURE__ */ new WeakMap() };
29831
+ }
29832
+ var pendingRows = /* @__PURE__ */ new WeakMap();
29833
+ function loadVaultCorpus(ctx, options, preparation) {
29834
+ if (!preparation) return getAllVaultMemoriesOp(ctx, options);
29835
+ const corpusCache = preparation.corpora;
29836
+ let pending = corpusCache.get(ctx);
29837
+ if (!pending) {
29838
+ pending = /* @__PURE__ */ new Map();
29839
+ corpusCache.set(ctx, pending);
29840
+ }
29841
+ const key = JSON.stringify(options ?? {});
29842
+ let request = pending.get(key);
29843
+ if (!request) {
29844
+ const entries = pending;
29845
+ request = getAllVaultMemoriesOp(ctx, options).catch((error) => {
29846
+ entries.delete(key);
29847
+ throw error;
29848
+ });
29849
+ pending.set(key, request);
29850
+ }
29851
+ return request;
29852
+ }
29853
+ function rowRequestsFor(registry, ctx, maskInput) {
29854
+ let byMask = registry.get(ctx);
29855
+ if (!byMask) {
29856
+ byMask = /* @__PURE__ */ new Map();
29857
+ registry.set(ctx, byMask);
29858
+ }
29859
+ let requests = byMask.get(maskInput);
29860
+ if (!requests) {
29861
+ requests = /* @__PURE__ */ new Map();
29862
+ byMask.set(maskInput, requests);
29863
+ }
29864
+ return requests;
29865
+ }
29866
+ function embedVaultRows(ctx, options, rows, preparation) {
29867
+ const pending = rowRequestsFor(pendingRows, ctx, options.maskInput);
29868
+ const prepared = preparation && rowRequestsFor(preparation.rows, ctx, options.maskInput);
29869
+ const model = options.model ?? DEFAULT_API_EMBEDDING_MODEL;
29870
+ const keys = rows.map(
29871
+ (row) => JSON.stringify([
29872
+ row.uniqueId,
29873
+ row.updatedAt?.getTime(),
29874
+ row.content,
29875
+ model,
29876
+ options.baseUrl,
29877
+ options.timeoutMs,
29878
+ options.tokenTimeoutMs,
29879
+ options.totalTimeoutMs
29880
+ ])
29881
+ );
29882
+ const missing = /* @__PURE__ */ new Map();
29883
+ rows.forEach((row, index) => {
29884
+ if (!pending.has(keys[index]) && !prepared?.has(keys[index])) missing.set(keys[index], row);
29885
+ });
29886
+ if (missing.size > 0) {
29887
+ const entries = pending;
29888
+ const batch = Promise.resolve().then(
29889
+ () => generateEmbeddings(
29890
+ [...missing.values()].map((row) => row.content),
29891
+ options
29892
+ )
29893
+ );
29894
+ [...missing].forEach(([key, row], index) => {
29895
+ const request = batch.then((vectors) => {
29896
+ const vector = vectors[index];
29897
+ updateVaultMemoryEmbeddingOp(ctx, row.uniqueId, JSON.stringify(vector), model, {
29898
+ content: row.content,
29899
+ updatedAt: row.updatedAt?.getTime()
29900
+ }).catch(() => {
29901
+ });
29902
+ return vector;
29903
+ }).finally(() => {
29904
+ entries.delete(key);
29905
+ if (entries.size === 0) pendingRows.get(ctx)?.delete(options.maskInput);
29906
+ });
29907
+ pending.set(key, request);
29908
+ });
29909
+ }
29910
+ return Promise.all(
29911
+ keys.map((key) => {
29912
+ const cached = prepared?.get(key);
29913
+ if (cached) return cached;
29914
+ const request = pending.get(key);
29915
+ if (!prepared) return request;
29916
+ const retained = request.catch((error) => {
29917
+ prepared.delete(key);
29918
+ throw error;
29919
+ });
29920
+ prepared.set(key, retained);
29921
+ return retained;
29922
+ })
29923
+ );
29924
+ }
29925
+
29824
29926
  // src/lib/memoryVault/vectorVersion.ts
29825
29927
  var vectorRowVersion = /* @__PURE__ */ new WeakMap();
29826
29928
  function contentFingerprint(content) {
@@ -30383,11 +30485,11 @@ async function rankComposite(originalQuery, originalQueryEmbedding, subQueries,
30383
30485
  return combined.slice(0, limit);
30384
30486
  }
30385
30487
  async function preEmbedVaultMemories(vaultCtx, embeddingOptions, cache, options) {
30386
- const memories = await getAllVaultMemoriesOp(vaultCtx, options);
30488
+ const memories = await loadVaultCorpus(vaultCtx, options);
30387
30489
  const currentModel = embeddingOptions.model ?? DEFAULT_API_EMBEDDING_MODEL;
30388
30490
  const uncachedTexts = [];
30389
30491
  const uncachedKeys = [];
30390
- const uncachedIds = [];
30492
+ const uncachedRows = [];
30391
30493
  const uncachedVersions = [];
30392
30494
  for (const m of memories) {
30393
30495
  const content = m.content;
@@ -30406,12 +30508,12 @@ async function preEmbedVaultMemories(vaultCtx, embeddingOptions, cache, options)
30406
30508
  }
30407
30509
  uncachedTexts.push(m.content);
30408
30510
  uncachedKeys.push(m.uniqueId);
30409
- uncachedIds.push(m.uniqueId);
30511
+ uncachedRows.push(m);
30410
30512
  uncachedVersions.push(m.updatedAt);
30411
30513
  }
30412
30514
  }
30413
30515
  if (uncachedTexts.length > 0) {
30414
- const embeddings = await generateEmbeddings(uncachedTexts, embeddingOptions);
30516
+ const embeddings = await embedVaultRows(vaultCtx, embeddingOptions, uncachedRows);
30415
30517
  for (let i = 0; i < uncachedKeys.length; i++) {
30416
30518
  cacheRowVector(
30417
30519
  cache,
@@ -30420,13 +30522,6 @@ async function preEmbedVaultMemories(vaultCtx, embeddingOptions, cache, options)
30420
30522
  uncachedVersions[i],
30421
30523
  uncachedTexts[i]
30422
30524
  );
30423
- updateVaultMemoryEmbeddingOp(
30424
- vaultCtx,
30425
- uncachedIds[i],
30426
- JSON.stringify(embeddings[i]),
30427
- currentModel
30428
- ).catch(() => {
30429
- });
30430
30525
  }
30431
30526
  }
30432
30527
  }
@@ -30568,10 +30663,7 @@ async function buildProjectedCorpus(query, vaultCtx, embeddingOptions, cache, qu
30568
30663
  let laneVecs;
30569
30664
  if (opts.embedStats) opts.embedStats.rowsEmbedded += laneRows.length;
30570
30665
  try {
30571
- laneVecs = await generateEmbeddings(
30572
- laneRows.map((m) => m.content),
30573
- embeddingOptions
30574
- );
30666
+ laneVecs = await embedVaultRows(vaultCtx, embeddingOptions, laneRows);
30575
30667
  } catch (err) {
30576
30668
  laneEmbedFailed = true;
30577
30669
  getLogger().warn(
@@ -30585,13 +30677,6 @@ async function buildProjectedCorpus(query, vaultCtx, embeddingOptions, cache, qu
30585
30677
  cacheRowVector(cache, m.uniqueId, vec, m.updatedAt, m.content);
30586
30678
  resolvedThisCall.add(m.uniqueId);
30587
30679
  vectored.push({ uniqueId: m.uniqueId, embedding: vec, updatedAt: m.updatedAt });
30588
- updateVaultMemoryEmbeddingOp(
30589
- vaultCtx,
30590
- m.uniqueId,
30591
- JSON.stringify(vecs[i]),
30592
- currentModel
30593
- ).catch(() => {
30594
- });
30595
30680
  });
30596
30681
  }
30597
30682
  }
@@ -30783,9 +30868,10 @@ async function prepareVaultCandidates(query, vaultCtx, embeddingOptions, cache,
30783
30868
  };
30784
30869
  }
30785
30870
  } else {
30786
- const loaded = await getAllVaultMemoriesOp(
30871
+ const loaded = await loadVaultCorpus(
30787
30872
  vaultCtx,
30788
- Object.keys(queryOpts).length > 0 ? queryOpts : void 0
30873
+ Object.keys(queryOpts).length > 0 ? queryOpts : void 0,
30874
+ searchOptions?.vaultPreparation
30789
30875
  );
30790
30876
  vaultSize = loaded.length;
30791
30877
  rowsDecrypted = loaded.length;
@@ -30858,7 +30944,12 @@ async function prepareVaultCandidates(query, vaultCtx, embeddingOptions, cache,
30858
30944
  let newEmbeddings;
30859
30945
  embedStats.rowsEmbedded += uncachedTexts.length;
30860
30946
  try {
30861
- newEmbeddings = await generateEmbeddings(uncachedTexts, embeddingOptions);
30947
+ newEmbeddings = await embedVaultRows(
30948
+ vaultCtx,
30949
+ embeddingOptions,
30950
+ uncachedIndices.map((index) => memories[index]),
30951
+ searchOptions?.vaultPreparation
30952
+ );
30862
30953
  } catch (err) {
30863
30954
  embeddingFailure = true;
30864
30955
  getLogger().warn(
@@ -30874,13 +30965,6 @@ async function prepareVaultCandidates(query, vaultCtx, embeddingOptions, cache,
30874
30965
  memories[uncachedIndices[j]].updatedAt,
30875
30966
  memories[uncachedIndices[j]].content
30876
30967
  );
30877
- updateVaultMemoryEmbeddingOp(
30878
- vaultCtx,
30879
- memories[uncachedIndices[j]].uniqueId,
30880
- JSON.stringify(newEmbeddings[j]),
30881
- currentModel
30882
- ).catch(() => {
30883
- });
30884
30968
  }
30885
30969
  }
30886
30970
  }
@@ -33447,6 +33531,7 @@ async function recall(query, ctx, options = {}) {
33447
33531
  },
33448
33532
  ...options.rrfK !== void 0 && { rrfK: options.rrfK },
33449
33533
  ...options.decryptLast !== void 0 && { decryptLast: options.decryptLast },
33534
+ ...options.vaultPreparation && { vaultPreparation: options.vaultPreparation },
33450
33535
  ...subQueries && { subQueries },
33451
33536
  ...options.scopes && { scopes: options.scopes },
33452
33537
  ...options.folderId !== void 0 && { folderId: options.folderId },
@@ -34869,13 +34954,20 @@ async function synthesizeProfile(ctx, options = {}) {
34869
34954
  }
34870
34955
  }
34871
34956
  }
34957
+ const vaultPreparation = createVaultPreparation();
34872
34958
  const settled = await Promise.allSettled(
34873
34959
  facets.map(async (facet) => {
34874
34960
  const prior = previous?.sections.find((s) => s.key === facet.key);
34875
34961
  if (prior && !staleKeys.has(facet.key)) {
34876
34962
  return prior;
34877
34963
  }
34878
- return synthesizeFacet(facet, recallCtx, options, fallbackPriors.get(facet.key));
34964
+ return synthesizeFacet(
34965
+ facet,
34966
+ recallCtx,
34967
+ options,
34968
+ fallbackPriors.get(facet.key),
34969
+ vaultPreparation
34970
+ );
34879
34971
  })
34880
34972
  );
34881
34973
  const sections = settled.map((r, i) => {
@@ -35029,7 +35121,7 @@ async function attributeFacts(ctx, candidates, facets) {
35029
35121
  }
35030
35122
  return keys;
35031
35123
  }
35032
- async function synthesizeFacet(facet, ctx, options, prior) {
35124
+ async function synthesizeFacet(facet, ctx, options, prior, vaultPreparation) {
35033
35125
  const scopes = options.scopes ?? DEFAULT_SCOPES;
35034
35126
  const limit = options.limit ?? DEFAULT_FACET_RECALL_LIMIT;
35035
35127
  const factTypeWeights = { ...DEFAULT_PROFILE_FACT_TYPE_WEIGHTS, ...options.factTypeWeights };
@@ -35046,6 +35138,7 @@ async function synthesizeFacet(facet, ctx, options, prior) {
35046
35138
  };
35047
35139
  }
35048
35140
  const recalled = await recall(facet.query, ctx, {
35141
+ vaultPreparation,
35049
35142
  scopes,
35050
35143
  limit: hasReviewGate ? RECALL_MAX_LIMIT : limit,
35051
35144
  types: ["fact"],
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@anuma/sdk",
3
- "version": "9.1.4",
3
+ "version": "9.1.5",
4
4
  "description": "",
5
5
  "main": "./dist/index.cjs",
6
6
  "module": "./dist/index.mjs",