@anuma/sdk 3.0.0 → 3.1.1

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.
@@ -22,7 +22,6 @@ import {
22
22
  createConversationOp,
23
23
  createMemoryVaultSearchTool,
24
24
  createMessageOp,
25
- createSseClient,
26
25
  createSupersedingMemoryOp,
27
26
  createVaultEmbeddingCache,
28
27
  createVaultMemoriesBatchOp,
@@ -108,7 +107,7 @@ import {
108
107
  updateVaultMemoryOp,
109
108
  upsertMessageOp,
110
109
  vaultMemoryToStored
111
- } from "./chunk-QQOIC37O.mjs";
110
+ } from "./chunk-4GKKHVF7.mjs";
112
111
  import {
113
112
  createStreamingDeAnonymizer,
114
113
  resolvePiiRedactor
@@ -3348,6 +3347,7 @@ async function updateVaultFolderOp(ctx, id, opts) {
3348
3347
  if (record.isDeleted) return null;
3349
3348
  const scopeChanged = opts.scope !== void 0 && opts.scope !== record.scope;
3350
3349
  const updated = await ctx.database.write(async () => {
3350
+ const memories = scopeChanged ? await ctx.vaultMemoryCollection.query(import_watermelondb15.Q.where("folder_id", id), import_watermelondb15.Q.where("is_deleted", false)).fetch() : [];
3351
3351
  const updates = [];
3352
3352
  updates.push(
3353
3353
  record.prepareUpdate((r) => {
@@ -3355,15 +3355,12 @@ async function updateVaultFolderOp(ctx, id, opts) {
3355
3355
  if (opts.scope !== void 0) r._setRaw("scope", opts.scope);
3356
3356
  })
3357
3357
  );
3358
- if (scopeChanged) {
3359
- const memories = await ctx.vaultMemoryCollection.query(import_watermelondb15.Q.where("folder_id", id), import_watermelondb15.Q.where("is_deleted", false)).fetch();
3360
- for (const memory of memories) {
3361
- updates.push(
3362
- memory.prepareUpdate((r) => {
3363
- r._setRaw("scope", opts.scope);
3364
- })
3365
- );
3366
- }
3358
+ for (const memory of memories) {
3359
+ updates.push(
3360
+ memory.prepareUpdate((r) => {
3361
+ r._setRaw("scope", opts.scope);
3362
+ })
3363
+ );
3367
3364
  }
3368
3365
  await ctx.database.batch(...updates);
3369
3366
  return ctx.vaultFolderCollection.find(id);
@@ -5501,9 +5498,11 @@ function createChunkVectorCache(maxSize = DEFAULT_CHUNK_CACHE_SIZE) {
5501
5498
  return new LRUCache(maxSize);
5502
5499
  }
5503
5500
 
5504
- // src/lib/memory/recallTool.ts
5501
+ // src/lib/memory/recallConstants.ts
5505
5502
  var RECALL_TOOL_NAME = "recall_memory";
5506
5503
  var RECALL_MAX_LIMIT = 50;
5504
+
5505
+ // src/lib/memory/recallTool.ts
5507
5506
  var DEFAULT_LIMIT = 8;
5508
5507
  var DEFAULT_BUDGET = "low";
5509
5508
  function formatEventTime(start, end, kind) {
@@ -6062,7 +6061,7 @@ function getCachedServerTools() {
6062
6061
  if (!cached) return null;
6063
6062
  const parsed = JSON.parse(cached);
6064
6063
  if (parsed.version !== CACHE_VERSION) {
6065
- clearServerToolsCache();
6064
+ removeLocalStorageCache();
6066
6065
  return null;
6067
6066
  }
6068
6067
  return parsed;
@@ -6074,37 +6073,45 @@ function isCacheExpired(cache, expirationMs = DEFAULT_CACHE_EXPIRATION_MS) {
6074
6073
  if (!cache) return true;
6075
6074
  return Date.now() - cache.timestamp > expirationMs;
6076
6075
  }
6077
- function cacheServerTools(tools, checksum) {
6078
- if (typeof localStorage === "undefined") return;
6079
- const cacheData = {
6076
+ function buildCacheEntry(tools, checksum) {
6077
+ return {
6080
6078
  tools,
6081
6079
  timestamp: Date.now(),
6082
6080
  version: CACHE_VERSION,
6083
6081
  ...checksum && { checksum }
6084
6082
  };
6083
+ }
6084
+ function writeLocalStorageCache(entry) {
6085
+ if (typeof localStorage === "undefined") return;
6085
6086
  try {
6086
- localStorage.setItem(SERVER_TOOLS_CACHE_KEY, JSON.stringify(cacheData));
6087
+ localStorage.setItem(SERVER_TOOLS_CACHE_KEY, JSON.stringify(entry));
6087
6088
  } catch (error) {
6088
6089
  getLogger().warn("[serverTools] Failed to cache tools:", error);
6089
6090
  }
6090
6091
  }
6091
- function clearServerToolsCache() {
6092
+ function removeLocalStorageCache() {
6092
6093
  if (typeof localStorage === "undefined") return;
6093
6094
  localStorage.removeItem(SERVER_TOOLS_CACHE_KEY);
6094
6095
  }
6095
- function getToolsChecksum() {
6096
- const cached = getCachedServerTools();
6097
- return cached?.checksum;
6096
+ var localStorageToolsCache = {
6097
+ get: getCachedServerTools,
6098
+ set: writeLocalStorageCache,
6099
+ clear: removeLocalStorageCache
6100
+ };
6101
+ function clearServerToolsCache(cache = localStorageToolsCache) {
6102
+ return cache.clear?.();
6103
+ }
6104
+ function getToolsChecksum(cache = localStorageToolsCache) {
6105
+ const cached = cache.get();
6106
+ return cached instanceof Promise ? cached.then((c) => c?.checksum) : cached?.checksum;
6098
6107
  }
6099
- function shouldRefreshTools(responseChecksum) {
6108
+ function shouldRefreshTools(responseChecksum, cache = localStorageToolsCache) {
6100
6109
  if (!responseChecksum) {
6101
6110
  return false;
6102
6111
  }
6103
- const cachedChecksum = getToolsChecksum();
6104
- if (!cachedChecksum) {
6105
- return true;
6106
- }
6107
- return cachedChecksum !== responseChecksum;
6112
+ const decide = (cachedChecksum2) => !cachedChecksum2 || cachedChecksum2 !== responseChecksum;
6113
+ const cachedChecksum = getToolsChecksum(cache);
6114
+ return cachedChecksum instanceof Promise ? cachedChecksum.then(decide) : decide(cachedChecksum);
6108
6115
  }
6109
6116
  async function fetchServerToolsFromApi(baseUrl, token) {
6110
6117
  const response = await fetch(`${baseUrl}/api/v1/tools`, {
@@ -6126,9 +6133,23 @@ async function getServerTools(options) {
6126
6133
  cacheExpirationMs = DEFAULT_CACHE_EXPIRATION_MS,
6127
6134
  forceRefresh = false,
6128
6135
  getToken,
6129
- apiKey
6136
+ apiKey,
6137
+ cache = localStorageToolsCache
6130
6138
  } = options;
6131
- const cached = getCachedServerTools();
6139
+ const persistCache = async (entry) => {
6140
+ try {
6141
+ await cache.set(entry);
6142
+ } catch (error) {
6143
+ getLogger().warn("[serverTools] Cache write failed (non-fatal):", error);
6144
+ }
6145
+ };
6146
+ let rawCached = null;
6147
+ try {
6148
+ rawCached = await cache.get();
6149
+ } catch (error) {
6150
+ getLogger().warn("[serverTools] Cache read failed; falling through to server fetch:", error);
6151
+ }
6152
+ const cached = rawCached && rawCached.version === CACHE_VERSION ? rawCached : null;
6132
6153
  const cacheValid = !isCacheExpired(cached, cacheExpirationMs);
6133
6154
  if (cached && cacheValid && !forceRefresh) {
6134
6155
  return cached.tools;
@@ -6149,7 +6170,7 @@ async function getServerTools(options) {
6149
6170
  }
6150
6171
  const data = await response.json();
6151
6172
  const { tools: tools2, checksum: checksum2 } = convertServerToolsResponse(data);
6152
- cacheServerTools(tools2, checksum2);
6173
+ await persistCache(buildCacheEntry(tools2, checksum2));
6153
6174
  return tools2;
6154
6175
  }
6155
6176
  if (!getToken) {
@@ -6162,7 +6183,7 @@ async function getServerTools(options) {
6162
6183
  return cached?.tools ?? [];
6163
6184
  }
6164
6185
  const { tools, checksum } = await fetchServerToolsFromApi(effectiveBaseUrl, token);
6165
- cacheServerTools(tools, checksum);
6186
+ await persistCache(buildCacheEntry(tools, checksum));
6166
6187
  return tools;
6167
6188
  } catch (error) {
6168
6189
  getLogger().error("[serverTools] Failed to fetch server tools:", error);
@@ -6546,13 +6567,14 @@ async function selectServerToolsForPrompt(options) {
6546
6567
  baseUrl,
6547
6568
  embeddingModel,
6548
6569
  cacheExpirationMs,
6570
+ cache,
6549
6571
  deferLoading
6550
6572
  } = options;
6551
6573
  if (serverToolsFilter === void 0) return [];
6552
6574
  if (Array.isArray(serverToolsFilter) && serverToolsFilter.length === 0) return [];
6553
6575
  let allServerTools;
6554
6576
  try {
6555
- allServerTools = await getServerTools({ baseUrl, cacheExpirationMs, getToken });
6577
+ allServerTools = await getServerTools({ baseUrl, cacheExpirationMs, getToken, cache });
6556
6578
  } catch {
6557
6579
  return [];
6558
6580
  }
@@ -7887,6 +7909,141 @@ function createCryptoPricePreProcessor(options = {}) {
7887
7909
  };
7888
7910
  }
7889
7911
 
7912
+ // src/client/core/serverSentEvents.gen.ts
7913
+ var createSseClient = ({
7914
+ onRequest,
7915
+ onSseError,
7916
+ onSseEvent,
7917
+ responseTransformer,
7918
+ responseValidator,
7919
+ sseDefaultRetryDelay,
7920
+ sseMaxRetryAttempts,
7921
+ sseMaxRetryDelay,
7922
+ sseSleepFn,
7923
+ url,
7924
+ ...options
7925
+ }) => {
7926
+ let lastEventId;
7927
+ const sleep3 = sseSleepFn ?? ((ms) => new Promise((resolve) => setTimeout(resolve, ms)));
7928
+ const createStream = async function* () {
7929
+ let retryDelay = sseDefaultRetryDelay ?? 3e3;
7930
+ let attempt = 0;
7931
+ const signal = options.signal ?? new AbortController().signal;
7932
+ while (true) {
7933
+ if (signal.aborted) break;
7934
+ attempt++;
7935
+ const headers = options.headers instanceof Headers ? options.headers : new Headers(options.headers);
7936
+ if (lastEventId !== void 0) {
7937
+ headers.set("Last-Event-ID", lastEventId);
7938
+ }
7939
+ try {
7940
+ const requestInit = {
7941
+ redirect: "follow",
7942
+ ...options,
7943
+ body: options.serializedBody,
7944
+ headers,
7945
+ signal
7946
+ };
7947
+ let request = new Request(url, requestInit);
7948
+ if (onRequest) {
7949
+ request = await onRequest(url, requestInit);
7950
+ }
7951
+ const _fetch = options.fetch ?? globalThis.fetch;
7952
+ const response = await _fetch(request);
7953
+ if (!response.ok)
7954
+ throw new Error(
7955
+ `SSE failed: ${response.status} ${response.statusText}`
7956
+ );
7957
+ if (!response.body) throw new Error("No body in SSE response");
7958
+ const reader = response.body.pipeThrough(new TextDecoderStream()).getReader();
7959
+ let buffer = "";
7960
+ const abortHandler = () => {
7961
+ try {
7962
+ reader.cancel();
7963
+ } catch {
7964
+ }
7965
+ };
7966
+ signal.addEventListener("abort", abortHandler);
7967
+ try {
7968
+ while (true) {
7969
+ const { done, value } = await reader.read();
7970
+ if (done) break;
7971
+ buffer += value;
7972
+ const chunks = buffer.split("\n\n");
7973
+ buffer = chunks.pop() ?? "";
7974
+ for (const chunk of chunks) {
7975
+ const lines = chunk.split("\n");
7976
+ const dataLines = [];
7977
+ let eventName;
7978
+ for (const line of lines) {
7979
+ if (line.startsWith("data:")) {
7980
+ dataLines.push(line.replace(/^data:\s*/, ""));
7981
+ } else if (line.startsWith("event:")) {
7982
+ eventName = line.replace(/^event:\s*/, "");
7983
+ } else if (line.startsWith("id:")) {
7984
+ lastEventId = line.replace(/^id:\s*/, "");
7985
+ } else if (line.startsWith("retry:")) {
7986
+ const parsed = Number.parseInt(
7987
+ line.replace(/^retry:\s*/, ""),
7988
+ 10
7989
+ );
7990
+ if (!Number.isNaN(parsed)) {
7991
+ retryDelay = parsed;
7992
+ }
7993
+ }
7994
+ }
7995
+ let data;
7996
+ let parsedJson = false;
7997
+ if (dataLines.length) {
7998
+ const rawData = dataLines.join("\n");
7999
+ try {
8000
+ data = JSON.parse(rawData);
8001
+ parsedJson = true;
8002
+ } catch {
8003
+ data = rawData;
8004
+ }
8005
+ }
8006
+ if (parsedJson) {
8007
+ if (responseValidator) {
8008
+ await responseValidator(data);
8009
+ }
8010
+ if (responseTransformer) {
8011
+ data = await responseTransformer(data);
8012
+ }
8013
+ }
8014
+ onSseEvent?.({
8015
+ data,
8016
+ event: eventName,
8017
+ id: lastEventId,
8018
+ retry: retryDelay
8019
+ });
8020
+ if (dataLines.length) {
8021
+ yield data;
8022
+ }
8023
+ }
8024
+ }
8025
+ } finally {
8026
+ signal.removeEventListener("abort", abortHandler);
8027
+ reader.releaseLock();
8028
+ }
8029
+ break;
8030
+ } catch (error) {
8031
+ onSseError?.(error);
8032
+ if (sseMaxRetryAttempts !== void 0 && attempt >= sseMaxRetryAttempts) {
8033
+ break;
8034
+ }
8035
+ const backoff = Math.min(
8036
+ retryDelay * 2 ** (attempt - 1),
8037
+ sseMaxRetryDelay ?? 3e4
8038
+ );
8039
+ await sleep3(backoff);
8040
+ }
8041
+ }
8042
+ };
8043
+ const stream = createStream();
8044
+ return { stream };
8045
+ };
8046
+
7890
8047
  // src/lib/chat/runHooks.ts
7891
8048
  function composeHooks(hooks) {
7892
8049
  const handlers = hooks.filter((h) => h !== void 0 && h !== null);
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  recall
3
- } from "./chunk-QQOIC37O.mjs";
3
+ } from "./chunk-4GKKHVF7.mjs";
4
4
  import "./chunk-AHJT66EO.mjs";
5
5
  import "./chunk-3RPGXXPZ.mjs";
6
6
  export {
@@ -0,0 +1,25 @@
1
+ // src/clientConfig.ts
2
+ var DEFAULT_BASE_URL = "https://portal.anuma-dev.ai";
3
+ function readGlobal(key) {
4
+ if (typeof globalThis === "undefined") return void 0;
5
+ const value = globalThis[key];
6
+ return typeof value === "string" && value.length > 0 ? value : void 0;
7
+ }
8
+ function resolveBaseUrl() {
9
+ const fromProcess = typeof process !== "undefined" ? process.env.API_URL || process.env.NEXT_PUBLIC_API_URL || process.env.EXPO_PUBLIC_API_URL : void 0;
10
+ const fromGlobal = readGlobal("API_URL") ?? readGlobal("NEXT_PUBLIC_API_URL") ?? readGlobal("EXPO_PUBLIC_API_URL");
11
+ return (fromProcess || fromGlobal || DEFAULT_BASE_URL).replace(/\/+$/, "");
12
+ }
13
+ var BASE_URL = resolveBaseUrl();
14
+ var MCP_R2_DOMAIN = "4cf0e0ea50b97e72386fcf2f92a2d4e8.r2.cloudflarestorage.com";
15
+ var createClientConfig = (config) => ({
16
+ ...config,
17
+ baseUrl: BASE_URL
18
+ });
19
+
20
+ export {
21
+ resolveBaseUrl,
22
+ BASE_URL,
23
+ MCP_R2_DOMAIN,
24
+ createClientConfig
25
+ };
@@ -0,0 +1,12 @@
1
+ import {
2
+ BASE_URL,
3
+ MCP_R2_DOMAIN,
4
+ createClientConfig,
5
+ resolveBaseUrl
6
+ } from "./chunk-4ETHWSXJ.mjs";
7
+ export {
8
+ BASE_URL,
9
+ MCP_R2_DOMAIN,
10
+ createClientConfig,
11
+ resolveBaseUrl
12
+ };