@anuma/sdk 9.1.3 → 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.
- package/dist/expo/index.cjs +221 -121
- package/dist/expo/index.d.mts +13 -0
- package/dist/expo/index.d.ts +13 -0
- package/dist/expo/index.mjs +221 -121
- package/dist/memory/context.d.mts +123 -0
- package/dist/memory/context.d.ts +123 -0
- package/dist/react/index.cjs +221 -121
- package/dist/react/index.d.mts +13 -0
- package/dist/react/index.d.ts +13 -0
- package/dist/react/index.mjs +221 -121
- package/dist/server/index.cjs +132 -39
- package/dist/server/index.d.mts +9 -0
- package/dist/server/index.d.ts +9 -0
- package/dist/server/index.mjs +132 -39
- package/package.json +1 -1
package/dist/server/index.cjs
CHANGED
|
@@ -17323,15 +17323,16 @@ async function refreshEncryptionKeyIfMatches(walletAddress, probeCiphertext, sig
|
|
|
17323
17323
|
if (!parsed) {
|
|
17324
17324
|
return false;
|
|
17325
17325
|
}
|
|
17326
|
+
const startEpoch = sessionEpoch;
|
|
17326
17327
|
const cached = refreshCandidatesByAddress.get(walletAddress);
|
|
17327
17328
|
if (cached) {
|
|
17328
|
-
return tryCommitCandidatesIfProbeMatches(walletAddress, parsed, cached);
|
|
17329
|
+
return tryCommitCandidatesIfProbeMatches(walletAddress, parsed, cached, startEpoch);
|
|
17329
17330
|
}
|
|
17330
17331
|
let derivePromise = pendingCandidateDerives.get(walletAddress);
|
|
17331
17332
|
if (!derivePromise) {
|
|
17332
17333
|
derivePromise = (async () => {
|
|
17333
17334
|
const candidates2 = await deriveRefreshCandidates(signMessage, embeddedWalletSigner);
|
|
17334
|
-
if (candidates2) {
|
|
17335
|
+
if (candidates2 && sessionEpoch === startEpoch) {
|
|
17335
17336
|
refreshCandidatesByAddress.set(walletAddress, candidates2);
|
|
17336
17337
|
}
|
|
17337
17338
|
return candidates2;
|
|
@@ -17343,10 +17344,10 @@ async function refreshEncryptionKeyIfMatches(walletAddress, probeCiphertext, sig
|
|
|
17343
17344
|
pendingCandidateDerives.set(walletAddress, derivePromise);
|
|
17344
17345
|
}
|
|
17345
17346
|
const candidates = await derivePromise;
|
|
17346
|
-
if (!candidates) {
|
|
17347
|
+
if (!candidates || sessionEpoch !== startEpoch) {
|
|
17347
17348
|
return false;
|
|
17348
17349
|
}
|
|
17349
|
-
return tryCommitCandidatesIfProbeMatches(walletAddress, parsed, candidates);
|
|
17350
|
+
return tryCommitCandidatesIfProbeMatches(walletAddress, parsed, candidates, startEpoch);
|
|
17350
17351
|
}
|
|
17351
17352
|
function parsePrefixedCiphertext(probeCiphertext) {
|
|
17352
17353
|
const version = probeCiphertext.startsWith("enc:v3:") ? "v3" : probeCiphertext.startsWith("enc:v2:") ? "v2" : null;
|
|
@@ -17389,7 +17390,7 @@ async function deriveRefreshCandidates(signMessage, embeddedWalletSigner) {
|
|
|
17389
17390
|
}
|
|
17390
17391
|
return { legacy: legacyKey, current: currentKey };
|
|
17391
17392
|
}
|
|
17392
|
-
async function tryCommitCandidatesIfProbeMatches(walletAddress, parsed, candidates) {
|
|
17393
|
+
async function tryCommitCandidatesIfProbeMatches(walletAddress, parsed, candidates, startEpoch) {
|
|
17393
17394
|
const candidateHex = parsed.version === "v2" ? candidates.legacy : candidates.current;
|
|
17394
17395
|
const keyBytes = hexToBytes(candidateHex);
|
|
17395
17396
|
const candidateCryptoKey = await crypto.subtle.importKey(
|
|
@@ -17407,6 +17408,9 @@ async function tryCommitCandidatesIfProbeMatches(walletAddress, parsed, candidat
|
|
|
17407
17408
|
);
|
|
17408
17409
|
return false;
|
|
17409
17410
|
}
|
|
17411
|
+
if (sessionEpoch !== startEpoch) {
|
|
17412
|
+
return false;
|
|
17413
|
+
}
|
|
17410
17414
|
setStoredKey(walletAddress, { legacy: candidates.legacy, current: candidates.current });
|
|
17411
17415
|
cryptoKeyCache.delete(`${walletAddress}:v2`);
|
|
17412
17416
|
cryptoKeyCache.delete(`${walletAddress}:v3`);
|
|
@@ -31550,6 +31554,104 @@ function validate(parsed, originalQuery) {
|
|
|
31550
31554
|
return { mode, subQueries };
|
|
31551
31555
|
}
|
|
31552
31556
|
|
|
31557
|
+
// src/lib/memoryVault/vaultPreparation.ts
|
|
31558
|
+
function createVaultPreparation() {
|
|
31559
|
+
return { corpora: /* @__PURE__ */ new WeakMap(), rows: /* @__PURE__ */ new WeakMap() };
|
|
31560
|
+
}
|
|
31561
|
+
var pendingRows = /* @__PURE__ */ new WeakMap();
|
|
31562
|
+
function loadVaultCorpus(ctx, options, preparation) {
|
|
31563
|
+
if (!preparation) return getAllVaultMemoriesOp(ctx, options);
|
|
31564
|
+
const corpusCache = preparation.corpora;
|
|
31565
|
+
let pending = corpusCache.get(ctx);
|
|
31566
|
+
if (!pending) {
|
|
31567
|
+
pending = /* @__PURE__ */ new Map();
|
|
31568
|
+
corpusCache.set(ctx, pending);
|
|
31569
|
+
}
|
|
31570
|
+
const key = JSON.stringify(options ?? {});
|
|
31571
|
+
let request = pending.get(key);
|
|
31572
|
+
if (!request) {
|
|
31573
|
+
const entries = pending;
|
|
31574
|
+
request = getAllVaultMemoriesOp(ctx, options).catch((error) => {
|
|
31575
|
+
entries.delete(key);
|
|
31576
|
+
throw error;
|
|
31577
|
+
});
|
|
31578
|
+
pending.set(key, request);
|
|
31579
|
+
}
|
|
31580
|
+
return request;
|
|
31581
|
+
}
|
|
31582
|
+
function rowRequestsFor(registry, ctx, maskInput) {
|
|
31583
|
+
let byMask = registry.get(ctx);
|
|
31584
|
+
if (!byMask) {
|
|
31585
|
+
byMask = /* @__PURE__ */ new Map();
|
|
31586
|
+
registry.set(ctx, byMask);
|
|
31587
|
+
}
|
|
31588
|
+
let requests = byMask.get(maskInput);
|
|
31589
|
+
if (!requests) {
|
|
31590
|
+
requests = /* @__PURE__ */ new Map();
|
|
31591
|
+
byMask.set(maskInput, requests);
|
|
31592
|
+
}
|
|
31593
|
+
return requests;
|
|
31594
|
+
}
|
|
31595
|
+
function embedVaultRows(ctx, options, rows, preparation) {
|
|
31596
|
+
const pending = rowRequestsFor(pendingRows, ctx, options.maskInput);
|
|
31597
|
+
const prepared = preparation && rowRequestsFor(preparation.rows, ctx, options.maskInput);
|
|
31598
|
+
const model = options.model ?? DEFAULT_API_EMBEDDING_MODEL;
|
|
31599
|
+
const keys = rows.map(
|
|
31600
|
+
(row) => JSON.stringify([
|
|
31601
|
+
row.uniqueId,
|
|
31602
|
+
row.updatedAt?.getTime(),
|
|
31603
|
+
row.content,
|
|
31604
|
+
model,
|
|
31605
|
+
options.baseUrl,
|
|
31606
|
+
options.timeoutMs,
|
|
31607
|
+
options.tokenTimeoutMs,
|
|
31608
|
+
options.totalTimeoutMs
|
|
31609
|
+
])
|
|
31610
|
+
);
|
|
31611
|
+
const missing = /* @__PURE__ */ new Map();
|
|
31612
|
+
rows.forEach((row, index) => {
|
|
31613
|
+
if (!pending.has(keys[index]) && !prepared?.has(keys[index])) missing.set(keys[index], row);
|
|
31614
|
+
});
|
|
31615
|
+
if (missing.size > 0) {
|
|
31616
|
+
const entries = pending;
|
|
31617
|
+
const batch = Promise.resolve().then(
|
|
31618
|
+
() => generateEmbeddings(
|
|
31619
|
+
[...missing.values()].map((row) => row.content),
|
|
31620
|
+
options
|
|
31621
|
+
)
|
|
31622
|
+
);
|
|
31623
|
+
[...missing].forEach(([key, row], index) => {
|
|
31624
|
+
const request = batch.then((vectors) => {
|
|
31625
|
+
const vector = vectors[index];
|
|
31626
|
+
updateVaultMemoryEmbeddingOp(ctx, row.uniqueId, JSON.stringify(vector), model, {
|
|
31627
|
+
content: row.content,
|
|
31628
|
+
updatedAt: row.updatedAt?.getTime()
|
|
31629
|
+
}).catch(() => {
|
|
31630
|
+
});
|
|
31631
|
+
return vector;
|
|
31632
|
+
}).finally(() => {
|
|
31633
|
+
entries.delete(key);
|
|
31634
|
+
if (entries.size === 0) pendingRows.get(ctx)?.delete(options.maskInput);
|
|
31635
|
+
});
|
|
31636
|
+
pending.set(key, request);
|
|
31637
|
+
});
|
|
31638
|
+
}
|
|
31639
|
+
return Promise.all(
|
|
31640
|
+
keys.map((key) => {
|
|
31641
|
+
const cached = prepared?.get(key);
|
|
31642
|
+
if (cached) return cached;
|
|
31643
|
+
const request = pending.get(key);
|
|
31644
|
+
if (!prepared) return request;
|
|
31645
|
+
const retained = request.catch((error) => {
|
|
31646
|
+
prepared.delete(key);
|
|
31647
|
+
throw error;
|
|
31648
|
+
});
|
|
31649
|
+
prepared.set(key, retained);
|
|
31650
|
+
return retained;
|
|
31651
|
+
})
|
|
31652
|
+
);
|
|
31653
|
+
}
|
|
31654
|
+
|
|
31553
31655
|
// src/lib/memoryVault/vectorVersion.ts
|
|
31554
31656
|
var vectorRowVersion = /* @__PURE__ */ new WeakMap();
|
|
31555
31657
|
function contentFingerprint(content) {
|
|
@@ -32112,11 +32214,11 @@ async function rankComposite(originalQuery, originalQueryEmbedding, subQueries,
|
|
|
32112
32214
|
return combined.slice(0, limit);
|
|
32113
32215
|
}
|
|
32114
32216
|
async function preEmbedVaultMemories(vaultCtx, embeddingOptions, cache, options) {
|
|
32115
|
-
const memories = await
|
|
32217
|
+
const memories = await loadVaultCorpus(vaultCtx, options);
|
|
32116
32218
|
const currentModel = embeddingOptions.model ?? DEFAULT_API_EMBEDDING_MODEL;
|
|
32117
32219
|
const uncachedTexts = [];
|
|
32118
32220
|
const uncachedKeys = [];
|
|
32119
|
-
const
|
|
32221
|
+
const uncachedRows = [];
|
|
32120
32222
|
const uncachedVersions = [];
|
|
32121
32223
|
for (const m of memories) {
|
|
32122
32224
|
const content = m.content;
|
|
@@ -32135,12 +32237,12 @@ async function preEmbedVaultMemories(vaultCtx, embeddingOptions, cache, options)
|
|
|
32135
32237
|
}
|
|
32136
32238
|
uncachedTexts.push(m.content);
|
|
32137
32239
|
uncachedKeys.push(m.uniqueId);
|
|
32138
|
-
|
|
32240
|
+
uncachedRows.push(m);
|
|
32139
32241
|
uncachedVersions.push(m.updatedAt);
|
|
32140
32242
|
}
|
|
32141
32243
|
}
|
|
32142
32244
|
if (uncachedTexts.length > 0) {
|
|
32143
|
-
const embeddings = await
|
|
32245
|
+
const embeddings = await embedVaultRows(vaultCtx, embeddingOptions, uncachedRows);
|
|
32144
32246
|
for (let i = 0; i < uncachedKeys.length; i++) {
|
|
32145
32247
|
cacheRowVector(
|
|
32146
32248
|
cache,
|
|
@@ -32149,13 +32251,6 @@ async function preEmbedVaultMemories(vaultCtx, embeddingOptions, cache, options)
|
|
|
32149
32251
|
uncachedVersions[i],
|
|
32150
32252
|
uncachedTexts[i]
|
|
32151
32253
|
);
|
|
32152
|
-
updateVaultMemoryEmbeddingOp(
|
|
32153
|
-
vaultCtx,
|
|
32154
|
-
uncachedIds[i],
|
|
32155
|
-
JSON.stringify(embeddings[i]),
|
|
32156
|
-
currentModel
|
|
32157
|
-
).catch(() => {
|
|
32158
|
-
});
|
|
32159
32254
|
}
|
|
32160
32255
|
}
|
|
32161
32256
|
}
|
|
@@ -32297,10 +32392,7 @@ async function buildProjectedCorpus(query, vaultCtx, embeddingOptions, cache, qu
|
|
|
32297
32392
|
let laneVecs;
|
|
32298
32393
|
if (opts.embedStats) opts.embedStats.rowsEmbedded += laneRows.length;
|
|
32299
32394
|
try {
|
|
32300
|
-
laneVecs = await
|
|
32301
|
-
laneRows.map((m) => m.content),
|
|
32302
|
-
embeddingOptions
|
|
32303
|
-
);
|
|
32395
|
+
laneVecs = await embedVaultRows(vaultCtx, embeddingOptions, laneRows);
|
|
32304
32396
|
} catch (err) {
|
|
32305
32397
|
laneEmbedFailed = true;
|
|
32306
32398
|
getLogger().warn(
|
|
@@ -32314,13 +32406,6 @@ async function buildProjectedCorpus(query, vaultCtx, embeddingOptions, cache, qu
|
|
|
32314
32406
|
cacheRowVector(cache, m.uniqueId, vec, m.updatedAt, m.content);
|
|
32315
32407
|
resolvedThisCall.add(m.uniqueId);
|
|
32316
32408
|
vectored.push({ uniqueId: m.uniqueId, embedding: vec, updatedAt: m.updatedAt });
|
|
32317
|
-
updateVaultMemoryEmbeddingOp(
|
|
32318
|
-
vaultCtx,
|
|
32319
|
-
m.uniqueId,
|
|
32320
|
-
JSON.stringify(vecs[i]),
|
|
32321
|
-
currentModel
|
|
32322
|
-
).catch(() => {
|
|
32323
|
-
});
|
|
32324
32409
|
});
|
|
32325
32410
|
}
|
|
32326
32411
|
}
|
|
@@ -32512,9 +32597,10 @@ async function prepareVaultCandidates(query, vaultCtx, embeddingOptions, cache,
|
|
|
32512
32597
|
};
|
|
32513
32598
|
}
|
|
32514
32599
|
} else {
|
|
32515
|
-
const loaded = await
|
|
32600
|
+
const loaded = await loadVaultCorpus(
|
|
32516
32601
|
vaultCtx,
|
|
32517
|
-
Object.keys(queryOpts).length > 0 ? queryOpts : void 0
|
|
32602
|
+
Object.keys(queryOpts).length > 0 ? queryOpts : void 0,
|
|
32603
|
+
searchOptions?.vaultPreparation
|
|
32518
32604
|
);
|
|
32519
32605
|
vaultSize = loaded.length;
|
|
32520
32606
|
rowsDecrypted = loaded.length;
|
|
@@ -32587,7 +32673,12 @@ async function prepareVaultCandidates(query, vaultCtx, embeddingOptions, cache,
|
|
|
32587
32673
|
let newEmbeddings;
|
|
32588
32674
|
embedStats.rowsEmbedded += uncachedTexts.length;
|
|
32589
32675
|
try {
|
|
32590
|
-
newEmbeddings = await
|
|
32676
|
+
newEmbeddings = await embedVaultRows(
|
|
32677
|
+
vaultCtx,
|
|
32678
|
+
embeddingOptions,
|
|
32679
|
+
uncachedIndices.map((index) => memories[index]),
|
|
32680
|
+
searchOptions?.vaultPreparation
|
|
32681
|
+
);
|
|
32591
32682
|
} catch (err) {
|
|
32592
32683
|
embeddingFailure = true;
|
|
32593
32684
|
getLogger().warn(
|
|
@@ -32603,13 +32694,6 @@ async function prepareVaultCandidates(query, vaultCtx, embeddingOptions, cache,
|
|
|
32603
32694
|
memories[uncachedIndices[j]].updatedAt,
|
|
32604
32695
|
memories[uncachedIndices[j]].content
|
|
32605
32696
|
);
|
|
32606
|
-
updateVaultMemoryEmbeddingOp(
|
|
32607
|
-
vaultCtx,
|
|
32608
|
-
memories[uncachedIndices[j]].uniqueId,
|
|
32609
|
-
JSON.stringify(newEmbeddings[j]),
|
|
32610
|
-
currentModel
|
|
32611
|
-
).catch(() => {
|
|
32612
|
-
});
|
|
32613
32697
|
}
|
|
32614
32698
|
}
|
|
32615
32699
|
}
|
|
@@ -35190,6 +35274,7 @@ async function recall(query, ctx, options = {}) {
|
|
|
35190
35274
|
},
|
|
35191
35275
|
...options.rrfK !== void 0 && { rrfK: options.rrfK },
|
|
35192
35276
|
...options.decryptLast !== void 0 && { decryptLast: options.decryptLast },
|
|
35277
|
+
...options.vaultPreparation && { vaultPreparation: options.vaultPreparation },
|
|
35193
35278
|
...subQueries && { subQueries },
|
|
35194
35279
|
...options.scopes && { scopes: options.scopes },
|
|
35195
35280
|
...options.folderId !== void 0 && { folderId: options.folderId },
|
|
@@ -36622,13 +36707,20 @@ async function synthesizeProfile(ctx, options = {}) {
|
|
|
36622
36707
|
}
|
|
36623
36708
|
}
|
|
36624
36709
|
}
|
|
36710
|
+
const vaultPreparation = createVaultPreparation();
|
|
36625
36711
|
const settled = await Promise.allSettled(
|
|
36626
36712
|
facets.map(async (facet) => {
|
|
36627
36713
|
const prior = previous?.sections.find((s) => s.key === facet.key);
|
|
36628
36714
|
if (prior && !staleKeys.has(facet.key)) {
|
|
36629
36715
|
return prior;
|
|
36630
36716
|
}
|
|
36631
|
-
return synthesizeFacet(
|
|
36717
|
+
return synthesizeFacet(
|
|
36718
|
+
facet,
|
|
36719
|
+
recallCtx,
|
|
36720
|
+
options,
|
|
36721
|
+
fallbackPriors.get(facet.key),
|
|
36722
|
+
vaultPreparation
|
|
36723
|
+
);
|
|
36632
36724
|
})
|
|
36633
36725
|
);
|
|
36634
36726
|
const sections = settled.map((r, i) => {
|
|
@@ -36782,7 +36874,7 @@ async function attributeFacts(ctx, candidates, facets) {
|
|
|
36782
36874
|
}
|
|
36783
36875
|
return keys;
|
|
36784
36876
|
}
|
|
36785
|
-
async function synthesizeFacet(facet, ctx, options, prior) {
|
|
36877
|
+
async function synthesizeFacet(facet, ctx, options, prior, vaultPreparation) {
|
|
36786
36878
|
const scopes = options.scopes ?? DEFAULT_SCOPES;
|
|
36787
36879
|
const limit = options.limit ?? DEFAULT_FACET_RECALL_LIMIT;
|
|
36788
36880
|
const factTypeWeights = { ...DEFAULT_PROFILE_FACT_TYPE_WEIGHTS, ...options.factTypeWeights };
|
|
@@ -36799,6 +36891,7 @@ async function synthesizeFacet(facet, ctx, options, prior) {
|
|
|
36799
36891
|
};
|
|
36800
36892
|
}
|
|
36801
36893
|
const recalled = await recall(facet.query, ctx, {
|
|
36894
|
+
vaultPreparation,
|
|
36802
36895
|
scopes,
|
|
36803
36896
|
limit: hasReviewGate ? RECALL_MAX_LIMIT : limit,
|
|
36804
36897
|
types: ["fact"],
|
package/dist/server/index.d.mts
CHANGED
|
@@ -3365,6 +3365,13 @@ interface RecencyOptions {
|
|
|
3365
3365
|
noDateMultiplier?: number;
|
|
3366
3366
|
}
|
|
3367
3367
|
|
|
3368
|
+
type VaultCorpusCache = WeakMap<VaultMemoryOperationsContext, Map<string, Promise<StoredVaultMemory[]>>>;
|
|
3369
|
+
type RowRequests = Map<EmbeddingOptions["maskInput"], Map<string, Promise<number[]>>>;
|
|
3370
|
+
interface VaultPreparation {
|
|
3371
|
+
corpora: VaultCorpusCache;
|
|
3372
|
+
rows: WeakMap<VaultMemoryOperationsContext, RowRequests>;
|
|
3373
|
+
}
|
|
3374
|
+
|
|
3368
3375
|
declare const DEFAULT_VAULT_CACHE_SIZE = 5000;
|
|
3369
3376
|
/**
|
|
3370
3377
|
* Create a VaultEmbeddingCache backed by an LRU with a default cap of
|
|
@@ -3382,6 +3389,7 @@ type VaultEmbeddingCache = Map<string, Float32Array>;
|
|
|
3382
3389
|
* Options for the vault search tool.
|
|
3383
3390
|
*/
|
|
3384
3391
|
interface MemoryVaultSearchOptions {
|
|
3392
|
+
/** @internal */ vaultPreparation?: VaultPreparation;
|
|
3385
3393
|
onCandidateCount?: (count: number) => void;
|
|
3386
3394
|
/** Maximum number of results to return (default: 5) */
|
|
3387
3395
|
limit?: number;
|
|
@@ -4059,6 +4067,7 @@ interface RankedMemory {
|
|
|
4059
4067
|
updatedAt: Date;
|
|
4060
4068
|
}
|
|
4061
4069
|
interface RecallOptions {
|
|
4070
|
+
/** @internal */ vaultPreparation?: VaultPreparation;
|
|
4062
4071
|
/** Embedding cache shared with the rest of the turn, so the query is embedded once across lanes and the send. */
|
|
4063
4072
|
embeddingCache?: Map<string, Float32Array>;
|
|
4064
4073
|
/** Which kinds to search. Default: ['fact']. */
|
package/dist/server/index.d.ts
CHANGED
|
@@ -3365,6 +3365,13 @@ interface RecencyOptions {
|
|
|
3365
3365
|
noDateMultiplier?: number;
|
|
3366
3366
|
}
|
|
3367
3367
|
|
|
3368
|
+
type VaultCorpusCache = WeakMap<VaultMemoryOperationsContext, Map<string, Promise<StoredVaultMemory[]>>>;
|
|
3369
|
+
type RowRequests = Map<EmbeddingOptions["maskInput"], Map<string, Promise<number[]>>>;
|
|
3370
|
+
interface VaultPreparation {
|
|
3371
|
+
corpora: VaultCorpusCache;
|
|
3372
|
+
rows: WeakMap<VaultMemoryOperationsContext, RowRequests>;
|
|
3373
|
+
}
|
|
3374
|
+
|
|
3368
3375
|
declare const DEFAULT_VAULT_CACHE_SIZE = 5000;
|
|
3369
3376
|
/**
|
|
3370
3377
|
* Create a VaultEmbeddingCache backed by an LRU with a default cap of
|
|
@@ -3382,6 +3389,7 @@ type VaultEmbeddingCache = Map<string, Float32Array>;
|
|
|
3382
3389
|
* Options for the vault search tool.
|
|
3383
3390
|
*/
|
|
3384
3391
|
interface MemoryVaultSearchOptions {
|
|
3392
|
+
/** @internal */ vaultPreparation?: VaultPreparation;
|
|
3385
3393
|
onCandidateCount?: (count: number) => void;
|
|
3386
3394
|
/** Maximum number of results to return (default: 5) */
|
|
3387
3395
|
limit?: number;
|
|
@@ -4059,6 +4067,7 @@ interface RankedMemory {
|
|
|
4059
4067
|
updatedAt: Date;
|
|
4060
4068
|
}
|
|
4061
4069
|
interface RecallOptions {
|
|
4070
|
+
/** @internal */ vaultPreparation?: VaultPreparation;
|
|
4062
4071
|
/** Embedding cache shared with the rest of the turn, so the query is embedded once across lanes and the send. */
|
|
4063
4072
|
embeddingCache?: Map<string, Float32Array>;
|
|
4064
4073
|
/** Which kinds to search. Default: ['fact']. */
|
package/dist/server/index.mjs
CHANGED
|
@@ -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
|
|
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
|
|
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
|
-
|
|
30511
|
+
uncachedRows.push(m);
|
|
30410
30512
|
uncachedVersions.push(m.updatedAt);
|
|
30411
30513
|
}
|
|
30412
30514
|
}
|
|
30413
30515
|
if (uncachedTexts.length > 0) {
|
|
30414
|
-
const embeddings = await
|
|
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
|
|
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
|
|
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
|
|
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(
|
|
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"],
|