@anuma/sdk 3.13.0 → 3.13.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.
@@ -17563,6 +17563,31 @@ function createMemoryEngineTool(storageCtx, embeddingOptions, searchOptions, cal
17563
17563
  // src/expo/useChatStorage.ts
17564
17564
  init_constants();
17565
17565
 
17566
+ // src/lib/memoryVault/embeddingCacheRegistry.ts
17567
+ init_useEncryption();
17568
+ init_lruCache();
17569
+ var registry = /* @__PURE__ */ new WeakMap();
17570
+ function buildKey(walletAddress, model) {
17571
+ return JSON.stringify([walletAddress ?? null, model]);
17572
+ }
17573
+ function getVaultEmbeddingCache(database, walletAddress, model) {
17574
+ let perDb = registry.get(database);
17575
+ if (!perDb) {
17576
+ perDb = /* @__PURE__ */ new Map();
17577
+ registry.set(database, perDb);
17578
+ }
17579
+ const key = buildKey(walletAddress, model);
17580
+ let cache = perDb.get(key);
17581
+ if (!cache) {
17582
+ cache = createVaultEmbeddingCache();
17583
+ perDb.set(key, cache);
17584
+ }
17585
+ return cache;
17586
+ }
17587
+ onClearAllEncryptionState(() => {
17588
+ registry = /* @__PURE__ */ new WeakMap();
17589
+ });
17590
+
17566
17591
  // src/lib/memoryVault/index.ts
17567
17592
  init_searchTool();
17568
17593
 
@@ -19024,14 +19049,17 @@ function useChatStorage(options) {
19024
19049
  },
19025
19050
  [vaultCtx]
19026
19051
  );
19027
- const vaultEmbeddingCacheRef = (0, import_react4.useRef)(createVaultEmbeddingCache());
19052
+ const vaultEmbeddingCache = (0, import_react4.useMemo)(
19053
+ () => getVaultEmbeddingCache(database, walletAddress, embeddingModel),
19054
+ [database, walletAddress, embeddingModel]
19055
+ );
19028
19056
  const chunkVectorCacheRef = (0, import_react4.useRef)(createChunkVectorCache());
19029
19057
  (0, import_react4.useEffect)(() => {
19030
19058
  return onClearAllEncryptionState(() => {
19031
- vaultEmbeddingCacheRef.current.clear();
19059
+ vaultEmbeddingCache.clear();
19032
19060
  chunkVectorCacheRef.current.clear();
19033
19061
  });
19034
- }, []);
19062
+ }, [vaultEmbeddingCache]);
19035
19063
  const createRecallTool2 = (0, import_react4.useCallback)(
19036
19064
  (toolOptions, callbacks) => {
19037
19065
  if (!getToken) {
@@ -19048,7 +19076,7 @@ function useChatStorage(options) {
19048
19076
  model: embeddingModel,
19049
19077
  maskInput: maskEmbeddingInput
19050
19078
  },
19051
- vaultCache: vaultEmbeddingCacheRef.current,
19079
+ vaultCache: vaultEmbeddingCache,
19052
19080
  chunkCache: chunkVectorCacheRef.current
19053
19081
  // entityCtx is intentionally omitted on Expo for now — the
19054
19082
  // W5 graph lane is a no-op without it (recall falls through
@@ -19066,7 +19094,8 @@ function useChatStorage(options) {
19066
19094
  baseUrl,
19067
19095
  embeddingModel,
19068
19096
  currentConversationId,
19069
- maskEmbeddingInput
19097
+ maskEmbeddingInput,
19098
+ vaultEmbeddingCache
19070
19099
  ]
19071
19100
  );
19072
19101
  const recallFn = (0, import_react4.useCallback)(
@@ -19091,7 +19120,7 @@ function useChatStorage(options) {
19091
19120
  model: embeddingModel,
19092
19121
  maskInput: maskEmbeddingInput
19093
19122
  },
19094
- vaultCache: vaultEmbeddingCacheRef.current,
19123
+ vaultCache: vaultEmbeddingCache,
19095
19124
  chunkCache: chunkVectorCacheRef.current
19096
19125
  },
19097
19126
  resolvedOptions
@@ -19104,7 +19133,8 @@ function useChatStorage(options) {
19104
19133
  baseUrl,
19105
19134
  embeddingModel,
19106
19135
  currentConversationId,
19107
- maskEmbeddingInput
19136
+ maskEmbeddingInput,
19137
+ vaultEmbeddingCache
19108
19138
  ]
19109
19139
  );
19110
19140
  const getVaultMemories = (0, import_react4.useCallback)(
@@ -19588,6 +19618,31 @@ function useChatStorage(options) {
19588
19618
  };
19589
19619
  }
19590
19620
  const { callRedactor, callPiiRedaction, maskForCall } = resolvePiiForCall(convId);
19621
+ const effectiveApiType = resolveApiType(requestApiType ?? apiType ?? "auto", model);
19622
+ const isServerToolsFunction = typeof serverToolsFilter === "function";
19623
+ const fetchServerToolsSettled = async (token) => {
19624
+ try {
19625
+ return {
19626
+ tools: await getServerTools({
19627
+ baseUrl,
19628
+ cacheExpirationMs: serverToolsConfig?.cacheExpirationMs,
19629
+ getToken: token,
19630
+ cache: serverToolsConfig?.cache
19631
+ })
19632
+ };
19633
+ } catch (error) {
19634
+ return { error };
19635
+ }
19636
+ };
19637
+ const embedForToolsSettled = async (token) => {
19638
+ try {
19639
+ return { embedding: await embedToolText(contentForStorage, maskForCall, token) };
19640
+ } catch (error) {
19641
+ return { error };
19642
+ }
19643
+ };
19644
+ const serverToolsPromise = getTokenRef.current && !(Array.isArray(serverToolsFilter) && serverToolsFilter.length === 0) ? fetchServerToolsSettled(getTokenRef.current) : null;
19645
+ const userMessageEmbeddingPromise = getTokenRef.current && contentForStorage.length >= MIN_CONTENT_LENGTH_FOR_TOOLS && (isServerToolsFunction && !(serverToolsConfig?.deferLoading?.enabled && effectiveApiType === "responses") || !!clientTools?.length) ? embedForToolsSettled(getTokenRef.current) : null;
19591
19646
  let messagesToSend;
19592
19647
  const storedMessages = await getMessages(convId);
19593
19648
  const knownToolCallEventIds = /* @__PURE__ */ new Set();
@@ -19662,29 +19717,26 @@ function useChatStorage(options) {
19662
19717
  };
19663
19718
  }
19664
19719
  const startTime = Date.now();
19665
- const effectiveApiType = resolveApiType(requestApiType ?? apiType ?? "auto", model);
19666
19720
  let mergedTools = clientTools;
19667
19721
  let userMessageEmbedding;
19668
19722
  let userMessageEmbeddingFailed = false;
19723
+ let serverFilterEmbeddingFailed = false;
19669
19724
  let filteredServerTools = [];
19670
- const isServerToolsFunction = typeof serverToolsFilter === "function";
19671
19725
  if (getTokenRef.current && !(Array.isArray(serverToolsFilter) && serverToolsFilter.length === 0)) {
19672
19726
  try {
19673
- const allServerTools = await getServerTools({
19674
- baseUrl,
19675
- cacheExpirationMs: serverToolsConfig?.cacheExpirationMs,
19676
- getToken: getTokenRef.current,
19677
- cache: serverToolsConfig?.cache
19678
- });
19727
+ const settledTools = await (serverToolsPromise ?? fetchServerToolsSettled(getTokenRef.current));
19728
+ if ("error" in settledTools) throw settledTools.error;
19729
+ const allServerTools = settledTools.tools;
19679
19730
  if (serverToolsConfig?.deferLoading?.enabled && effectiveApiType === "responses") {
19680
19731
  filteredServerTools = allServerTools;
19681
19732
  } else if (isServerToolsFunction) {
19682
19733
  if (contentForStorage.length >= MIN_CONTENT_LENGTH_FOR_TOOLS) {
19683
- userMessageEmbedding = await embedToolText(
19684
- contentForStorage,
19685
- maskForCall,
19686
- getTokenRef.current
19687
- );
19734
+ const settledEmbedding = await (userMessageEmbeddingPromise ?? embedForToolsSettled(getTokenRef.current));
19735
+ if ("error" in settledEmbedding) {
19736
+ serverFilterEmbeddingFailed = true;
19737
+ throw settledEmbedding.error;
19738
+ }
19739
+ userMessageEmbedding = settledEmbedding.embedding;
19688
19740
  const toolNames = serverToolsFilter(userMessageEmbedding, allServerTools);
19689
19741
  filteredServerTools = filterServerTools(allServerTools, toolNames);
19690
19742
  }
@@ -19708,14 +19760,11 @@ function useChatStorage(options) {
19708
19760
  if (clientTools?.length) {
19709
19761
  try {
19710
19762
  if (!userMessageEmbedding && getTokenRef.current && contentForStorage.length >= MIN_CONTENT_LENGTH_FOR_TOOLS) {
19711
- try {
19712
- userMessageEmbedding = await embedToolText(
19713
- contentForStorage,
19714
- maskForCall,
19715
- getTokenRef.current
19716
- );
19717
- } catch {
19763
+ const settledEmbedding = !userMessageEmbeddingPromise || serverFilterEmbeddingFailed ? await embedForToolsSettled(getTokenRef.current) : await userMessageEmbeddingPromise;
19764
+ if ("error" in settledEmbedding) {
19718
19765
  userMessageEmbeddingFailed = true;
19766
+ } else {
19767
+ userMessageEmbedding = settledEmbedding.embedding;
19719
19768
  }
19720
19769
  }
19721
19770
  if (typeof clientToolsFilter === "function") {
@@ -8829,6 +8829,29 @@ function createMemoryEngineTool(storageCtx, embeddingOptions, searchOptions, cal
8829
8829
  };
8830
8830
  }
8831
8831
 
8832
+ // src/lib/memoryVault/embeddingCacheRegistry.ts
8833
+ var registry = /* @__PURE__ */ new WeakMap();
8834
+ function buildKey(walletAddress, model) {
8835
+ return JSON.stringify([walletAddress ?? null, model]);
8836
+ }
8837
+ function getVaultEmbeddingCache(database, walletAddress, model) {
8838
+ let perDb = registry.get(database);
8839
+ if (!perDb) {
8840
+ perDb = /* @__PURE__ */ new Map();
8841
+ registry.set(database, perDb);
8842
+ }
8843
+ const key = buildKey(walletAddress, model);
8844
+ let cache = perDb.get(key);
8845
+ if (!cache) {
8846
+ cache = createVaultEmbeddingCache();
8847
+ perDb.set(key, cache);
8848
+ }
8849
+ return cache;
8850
+ }
8851
+ onClearAllEncryptionState(() => {
8852
+ registry = /* @__PURE__ */ new WeakMap();
8853
+ });
8854
+
8832
8855
  // src/lib/memoryVault/tool.ts
8833
8856
  var MANUAL_FACT_TYPES = [
8834
8857
  "identity",
@@ -10275,14 +10298,17 @@ function useChatStorage(options) {
10275
10298
  },
10276
10299
  [vaultCtx]
10277
10300
  );
10278
- const vaultEmbeddingCacheRef = useRef4(createVaultEmbeddingCache());
10301
+ const vaultEmbeddingCache = useMemo(
10302
+ () => getVaultEmbeddingCache(database, walletAddress, embeddingModel),
10303
+ [database, walletAddress, embeddingModel]
10304
+ );
10279
10305
  const chunkVectorCacheRef = useRef4(createChunkVectorCache());
10280
10306
  useEffect4(() => {
10281
10307
  return onClearAllEncryptionState(() => {
10282
- vaultEmbeddingCacheRef.current.clear();
10308
+ vaultEmbeddingCache.clear();
10283
10309
  chunkVectorCacheRef.current.clear();
10284
10310
  });
10285
- }, []);
10311
+ }, [vaultEmbeddingCache]);
10286
10312
  const createRecallTool2 = useCallback4(
10287
10313
  (toolOptions, callbacks) => {
10288
10314
  if (!getToken) {
@@ -10299,7 +10325,7 @@ function useChatStorage(options) {
10299
10325
  model: embeddingModel,
10300
10326
  maskInput: maskEmbeddingInput
10301
10327
  },
10302
- vaultCache: vaultEmbeddingCacheRef.current,
10328
+ vaultCache: vaultEmbeddingCache,
10303
10329
  chunkCache: chunkVectorCacheRef.current
10304
10330
  // entityCtx is intentionally omitted on Expo for now — the
10305
10331
  // W5 graph lane is a no-op without it (recall falls through
@@ -10317,7 +10343,8 @@ function useChatStorage(options) {
10317
10343
  baseUrl,
10318
10344
  embeddingModel,
10319
10345
  currentConversationId,
10320
- maskEmbeddingInput
10346
+ maskEmbeddingInput,
10347
+ vaultEmbeddingCache
10321
10348
  ]
10322
10349
  );
10323
10350
  const recallFn = useCallback4(
@@ -10342,7 +10369,7 @@ function useChatStorage(options) {
10342
10369
  model: embeddingModel,
10343
10370
  maskInput: maskEmbeddingInput
10344
10371
  },
10345
- vaultCache: vaultEmbeddingCacheRef.current,
10372
+ vaultCache: vaultEmbeddingCache,
10346
10373
  chunkCache: chunkVectorCacheRef.current
10347
10374
  },
10348
10375
  resolvedOptions
@@ -10355,7 +10382,8 @@ function useChatStorage(options) {
10355
10382
  baseUrl,
10356
10383
  embeddingModel,
10357
10384
  currentConversationId,
10358
- maskEmbeddingInput
10385
+ maskEmbeddingInput,
10386
+ vaultEmbeddingCache
10359
10387
  ]
10360
10388
  );
10361
10389
  const getVaultMemories = useCallback4(
@@ -10839,6 +10867,31 @@ function useChatStorage(options) {
10839
10867
  };
10840
10868
  }
10841
10869
  const { callRedactor, callPiiRedaction, maskForCall } = resolvePiiForCall(convId);
10870
+ const effectiveApiType = resolveApiType(requestApiType ?? apiType ?? "auto", model);
10871
+ const isServerToolsFunction = typeof serverToolsFilter === "function";
10872
+ const fetchServerToolsSettled = async (token) => {
10873
+ try {
10874
+ return {
10875
+ tools: await getServerTools({
10876
+ baseUrl,
10877
+ cacheExpirationMs: serverToolsConfig?.cacheExpirationMs,
10878
+ getToken: token,
10879
+ cache: serverToolsConfig?.cache
10880
+ })
10881
+ };
10882
+ } catch (error) {
10883
+ return { error };
10884
+ }
10885
+ };
10886
+ const embedForToolsSettled = async (token) => {
10887
+ try {
10888
+ return { embedding: await embedToolText(contentForStorage, maskForCall, token) };
10889
+ } catch (error) {
10890
+ return { error };
10891
+ }
10892
+ };
10893
+ const serverToolsPromise = getTokenRef.current && !(Array.isArray(serverToolsFilter) && serverToolsFilter.length === 0) ? fetchServerToolsSettled(getTokenRef.current) : null;
10894
+ const userMessageEmbeddingPromise = getTokenRef.current && contentForStorage.length >= MIN_CONTENT_LENGTH_FOR_TOOLS && (isServerToolsFunction && !(serverToolsConfig?.deferLoading?.enabled && effectiveApiType === "responses") || !!clientTools?.length) ? embedForToolsSettled(getTokenRef.current) : null;
10842
10895
  let messagesToSend;
10843
10896
  const storedMessages = await getMessages(convId);
10844
10897
  const knownToolCallEventIds = /* @__PURE__ */ new Set();
@@ -10913,29 +10966,26 @@ function useChatStorage(options) {
10913
10966
  };
10914
10967
  }
10915
10968
  const startTime = Date.now();
10916
- const effectiveApiType = resolveApiType(requestApiType ?? apiType ?? "auto", model);
10917
10969
  let mergedTools = clientTools;
10918
10970
  let userMessageEmbedding;
10919
10971
  let userMessageEmbeddingFailed = false;
10972
+ let serverFilterEmbeddingFailed = false;
10920
10973
  let filteredServerTools = [];
10921
- const isServerToolsFunction = typeof serverToolsFilter === "function";
10922
10974
  if (getTokenRef.current && !(Array.isArray(serverToolsFilter) && serverToolsFilter.length === 0)) {
10923
10975
  try {
10924
- const allServerTools = await getServerTools({
10925
- baseUrl,
10926
- cacheExpirationMs: serverToolsConfig?.cacheExpirationMs,
10927
- getToken: getTokenRef.current,
10928
- cache: serverToolsConfig?.cache
10929
- });
10976
+ const settledTools = await (serverToolsPromise ?? fetchServerToolsSettled(getTokenRef.current));
10977
+ if ("error" in settledTools) throw settledTools.error;
10978
+ const allServerTools = settledTools.tools;
10930
10979
  if (serverToolsConfig?.deferLoading?.enabled && effectiveApiType === "responses") {
10931
10980
  filteredServerTools = allServerTools;
10932
10981
  } else if (isServerToolsFunction) {
10933
10982
  if (contentForStorage.length >= MIN_CONTENT_LENGTH_FOR_TOOLS) {
10934
- userMessageEmbedding = await embedToolText(
10935
- contentForStorage,
10936
- maskForCall,
10937
- getTokenRef.current
10938
- );
10983
+ const settledEmbedding = await (userMessageEmbeddingPromise ?? embedForToolsSettled(getTokenRef.current));
10984
+ if ("error" in settledEmbedding) {
10985
+ serverFilterEmbeddingFailed = true;
10986
+ throw settledEmbedding.error;
10987
+ }
10988
+ userMessageEmbedding = settledEmbedding.embedding;
10939
10989
  const toolNames = serverToolsFilter(userMessageEmbedding, allServerTools);
10940
10990
  filteredServerTools = filterServerTools(allServerTools, toolNames);
10941
10991
  }
@@ -10959,14 +11009,11 @@ function useChatStorage(options) {
10959
11009
  if (clientTools?.length) {
10960
11010
  try {
10961
11011
  if (!userMessageEmbedding && getTokenRef.current && contentForStorage.length >= MIN_CONTENT_LENGTH_FOR_TOOLS) {
10962
- try {
10963
- userMessageEmbedding = await embedToolText(
10964
- contentForStorage,
10965
- maskForCall,
10966
- getTokenRef.current
10967
- );
10968
- } catch {
11012
+ const settledEmbedding = !userMessageEmbeddingPromise || serverFilterEmbeddingFailed ? await embedForToolsSettled(getTokenRef.current) : await userMessageEmbeddingPromise;
11013
+ if ("error" in settledEmbedding) {
10969
11014
  userMessageEmbeddingFailed = true;
11015
+ } else {
11016
+ userMessageEmbedding = settledEmbedding.embedding;
10970
11017
  }
10971
11018
  }
10972
11019
  if (typeof clientToolsFilter === "function") {