@anuma/sdk 6.37.0 → 6.39.0

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.
@@ -28093,22 +28093,35 @@ async function vaultMemoryRawToStored(raw, walletAddress, signMessage, embeddedW
28093
28093
  return stored;
28094
28094
  }
28095
28095
  async function getAllVaultMemoriesOp(ctx, options) {
28096
- const conditions = [
28096
+ const results = await ctx.vaultMemoryCollection.query(...vaultListConditions(ctx, options), ...vaultListOrder(options)).unsafeFetchRaw();
28097
+ return mapInBatches(
28098
+ results,
28099
+ (raw) => vaultMemoryRawToStored(raw, ctx.walletAddress, ctx.signMessage, ctx.embeddedWalletSigner)
28100
+ );
28101
+ }
28102
+ function vaultListConditions(ctx, options) {
28103
+ return [
28097
28104
  ...baseVaultConditions(ctx, options),
28098
28105
  ...options?.scopes?.length ? [import_watermelondb19.Q.where("scope", import_watermelondb19.Q.oneOf(options.scopes))] : [],
28099
28106
  ...visibilityConditions(options?.visibility),
28100
28107
  ...levelConditions(options?.levels),
28101
28108
  ...options?.kinds?.length ? [import_watermelondb19.Q.where("kind", import_watermelondb19.Q.oneOf(options.kinds))] : [],
28102
28109
  ...options?.folderId !== void 0 ? [import_watermelondb19.Q.where("folder_id", options.folderId)] : [],
28103
- ...options?.factTypes?.length ? [import_watermelondb19.Q.where("fact_type", import_watermelondb19.Q.oneOf(options.factTypes))] : [],
28110
+ ...options?.factTypes?.length ? [import_watermelondb19.Q.where("fact_type", import_watermelondb19.Q.oneOf(options.factTypes))] : []
28111
+ ];
28112
+ }
28113
+ function vaultListOrder(options) {
28114
+ return [
28104
28115
  import_watermelondb19.Q.sortBy(options?.since ? "updated_at" : "created_at", import_watermelondb19.Q.desc),
28105
28116
  ...options?.limit !== null && options?.limit !== void 0 && options.limit > 0 ? [import_watermelondb19.Q.take(options.limit)] : []
28106
28117
  ];
28107
- const results = await ctx.vaultMemoryCollection.query(...conditions).unsafeFetchRaw();
28108
- return mapInBatches(
28109
- results,
28110
- (raw) => vaultMemoryRawToStored(raw, ctx.walletAddress, ctx.signMessage, ctx.embeddedWalletSigner)
28111
- );
28118
+ }
28119
+ async function getVaultMemoryProjectionsOp(ctx, options) {
28120
+ const results = await ctx.vaultMemoryCollection.query(...vaultListConditions(ctx, options), ...vaultListOrder(options)).unsafeFetchRaw();
28121
+ return results.map((raw) => ({
28122
+ ...vaultMemoryRawToRankable(raw),
28123
+ topicsUserManaged: raw.topics_user_managed === true || raw.topics_user_managed === 1
28124
+ }));
28112
28125
  }
28113
28126
  function vaultMemoryRawToRankable(raw) {
28114
28127
  return {
@@ -33944,7 +33957,6 @@ async function recall(query, ctx, options = {}) {
33944
33957
  }
33945
33958
  const needsChunkEmbedding = types.includes("chunk") && ctx.storageCtx;
33946
33959
  const wantsTemporal = types.includes("fact") && (ctx.factSource || ctx.vaultCtx);
33947
- const graphRefiner = flags.traverse && options.graphRefine && options.decomposeOptions ? createLlmNeighborRefiner(options.decomposeOptions) : void 0;
33948
33960
  const queryEmbedTotalTimeoutMs = options.queryEmbedTotalTimeoutMs ?? DEFAULT_QUERY_EMBED_TOTAL_TIMEOUT_MS;
33949
33961
  const prepStart = nowMs2();
33950
33962
  let sharedEmbedMs = 0;
@@ -33973,13 +33985,12 @@ async function recall(query, ctx, options = {}) {
33973
33985
  safeLane(
33974
33986
  "graph",
33975
33987
  () => graphLaneFailed = true,
33976
- () => ctx.factSource ? ctx.factSource.graphRanking(query, flags.traverse, options) : buildGraphLaneRanking(query, ctx, flags.traverse, {
33977
- ...options.maxHops !== void 0 && { maxHops: options.maxHops },
33978
- ...options.entityFanout !== void 0 && { entityFanout: options.entityFanout },
33979
- ...options.nodeBudget !== void 0 && { nodeBudget: options.nodeBudget },
33980
- ...options.rrfK !== void 0 && { rrfK: options.rrfK },
33981
- ...graphRefiner && { refineNeighbors: graphRefiner }
33982
- })
33988
+ () => ctx.factSource ? ctx.factSource.graphRanking(query, flags.traverse, options) : buildGraphLaneRanking(
33989
+ query,
33990
+ ctx,
33991
+ flags.traverse,
33992
+ graphTraversalOptions(options, flags.traverse)
33993
+ )
33983
33994
  ),
33984
33995
  wantsTemporal ? safeLane(
33985
33996
  "temporal",
@@ -34198,6 +34209,16 @@ function toFactMemory(r, now) {
34198
34209
  updatedAt: r.updatedAt ?? /* @__PURE__ */ new Date(0)
34199
34210
  };
34200
34211
  }
34212
+ function graphTraversalOptions(options, traverse) {
34213
+ const refineNeighbors = traverse && options.graphRefine && options.decomposeOptions ? createLlmNeighborRefiner(options.decomposeOptions) : void 0;
34214
+ return {
34215
+ ...options.maxHops !== void 0 && { maxHops: options.maxHops },
34216
+ ...options.entityFanout !== void 0 && { entityFanout: options.entityFanout },
34217
+ ...options.nodeBudget !== void 0 && { nodeBudget: options.nodeBudget },
34218
+ ...options.rrfK !== void 0 && { rrfK: options.rrfK },
34219
+ ...refineNeighbors && { refineNeighbors }
34220
+ };
34221
+ }
34201
34222
  async function buildGraphLaneRanking(query, ctx, traverse = false, traversalOptions = {}) {
34202
34223
  const entityCtx = ctx.entityCtx ?? ctx.vaultCtx?.entityCtx;
34203
34224
  if (!entityCtx) return [];
@@ -34363,24 +34384,33 @@ function isDumpQuery(query) {
34363
34384
  return false;
34364
34385
  }
34365
34386
  async function listRecentFacts(ctx, limit, toolOptions) {
34387
+ const store = toolOptions?.memoryStore;
34366
34388
  const vaultCtx = ctx.vaultCtx;
34367
- if (!vaultCtx) return [];
34368
- const ordered = await getVaultRankingProjectionsOp(vaultCtx, {
34389
+ if (!vaultCtx && !store) return [];
34390
+ const read = (ids) => store ? store.list({ memoryIds: ids }) : getVaultMemoriesByIdsOp(vaultCtx, ids);
34391
+ const filters = {
34369
34392
  ...toolOptions?.scopes && { scopes: toolOptions.scopes },
34370
- ...toolOptions?.memoryIds !== void 0 && { memoryIds: toolOptions.memoryIds },
34393
+ ...toolOptions?.memoryIds !== void 0 && { memoryIds: toolOptions.memoryIds }
34394
+ };
34395
+ const project = (size) => store ? store.listProjections({ ...filters, limit: size }) : getVaultRankingProjectionsOp(vaultCtx, {
34396
+ ...filters,
34371
34397
  ...toolOptions?.folderId !== void 0 && { folderId: toolOptions.folderId }
34372
34398
  });
34373
34399
  const readable = [];
34374
- for (let i = 0; i < ordered.length && readable.length < limit; i += limit) {
34375
- const batch = ordered.slice(i, i + limit).map((p) => p.uniqueId);
34376
- const byId = new Map(
34377
- (await getVaultMemoriesByIdsOp(vaultCtx, batch)).map((m) => [m.uniqueId, m])
34378
- );
34379
- for (const id of batch) {
34380
- const m = byId.get(id);
34381
- if (m && !isEncrypted(m.content)) readable.push(m);
34382
- if (readable.length >= limit) break;
34400
+ let start = 0;
34401
+ for (let size = limit; readable.length < limit; size *= 2) {
34402
+ const ordered = await project(size);
34403
+ for (let i = start; i < ordered.length && readable.length < limit; i += limit) {
34404
+ const batch = ordered.slice(i, i + limit).map((p) => p.uniqueId);
34405
+ const byId = new Map((await read(batch)).map((m) => [m.uniqueId, m]));
34406
+ for (const id of batch) {
34407
+ const m = byId.get(id);
34408
+ if (m && !isEncrypted(m.content)) readable.push(m);
34409
+ if (readable.length >= limit) break;
34410
+ }
34383
34411
  }
34412
+ if (!store || ordered.length < size) break;
34413
+ start = ordered.length;
34384
34414
  }
34385
34415
  return readable.map((m) => ({
34386
34416
  id: m.uniqueId,
@@ -34397,6 +34427,8 @@ async function listRecentFacts(ctx, limit, toolOptions) {
34397
34427
  }));
34398
34428
  }
34399
34429
  function createRecallTool(ctx, toolOptions, callbacks) {
34430
+ if (toolOptions?.memoryStore && toolOptions.folderId !== void 0)
34431
+ throw new Error("Folders are not supported with a memoryStore");
34400
34432
  const defaultTypes = toolOptions?.types ?? ["fact", "chunk"];
34401
34433
  const defaultLimit = toolOptions?.limit ?? DEFAULT_LIMIT2;
34402
34434
  const defaultBudget = toolOptions?.budget ?? DEFAULT_BUDGET2;
@@ -34468,7 +34500,7 @@ function createRecallTool(ctx, toolOptions, callbacks) {
34468
34500
  let surfaced = 0;
34469
34501
  try {
34470
34502
  let recallDegraded = [];
34471
- const wantsRecent = args.sort === "recent" && defaultTypes.includes("fact") && ctx.vaultCtx !== void 0;
34503
+ const wantsRecent = args.sort === "recent" && defaultTypes.includes("fact") && (ctx.vaultCtx !== void 0 || toolOptions?.memoryStore !== void 0);
34472
34504
  let result;
34473
34505
  if (wantsRecent) {
34474
34506
  result = { memories: await listRecentFacts(ctx, effectiveLimit, toolOptions) };
@@ -34945,6 +34977,7 @@ function createLocalMemoryStore(options) {
34945
34977
  };
34946
34978
  const vaultCache = options.vaultCache ?? createVaultEmbeddingCache();
34947
34979
  const ownedBy = userId !== void 0 ? [import_watermelondb24.Q.where("user_id", userId)] : [];
34980
+ const linksOwnedBy = userId === void 0 ? [] : options.allowUnscopedRows ? [import_watermelondb24.Q.or(import_watermelondb24.Q.where("user_id", userId), import_watermelondb24.Q.where("user_id", null))] : [import_watermelondb24.Q.where("user_id", userId)];
34948
34981
  const watchedColumns = [
34949
34982
  "content",
34950
34983
  "scope",
@@ -34977,6 +35010,7 @@ function createLocalMemoryStore(options) {
34977
35010
  });
34978
35011
  };
34979
35012
  return {
35013
+ masksEmbeddingInput: options.embeddingOptions.maskInput !== void 0,
34980
35014
  list: (listOptions) => getAllVaultMemoriesOp(vaultCtx, listOptions),
34981
35015
  get: (id) => getVaultMemoryOp(vaultCtx, id),
34982
35016
  listArchived: async () => {
@@ -34987,6 +35021,29 @@ function createLocalMemoryStore(options) {
34987
35021
  },
34988
35022
  memoriesByTopics: (names) => getMemoriesByEntityNamesOp(entityCtx, names),
34989
35023
  topicsByMemories: (memoryIds) => getEntitiesByMemoryIdsOp(entityCtx, memoryIds),
35024
+ listTopics: async () => {
35025
+ const links = await entityCtx.memoryEntityCollection.query(...linksOwnedBy).fetch();
35026
+ const members = /* @__PURE__ */ new Map();
35027
+ for (const link of links) {
35028
+ const entityId = String(link.entityId);
35029
+ const set = members.get(entityId) ?? /* @__PURE__ */ new Set();
35030
+ set.add(String(link.memoryId));
35031
+ members.set(entityId, set);
35032
+ }
35033
+ const entities = userId === void 0 ? await entityCtx.entityCollection.query().fetch() : members.size ? await entityCtx.entityCollection.query(import_watermelondb24.Q.where("id", import_watermelondb24.Q.oneOf([...members.keys()]))).fetch() : [];
35034
+ const byName = /* @__PURE__ */ new Map();
35035
+ for (const entity of entities) {
35036
+ const count = members.get(entity.id)?.size ?? 0;
35037
+ const existing = byName.get(entity.canonicalName);
35038
+ byName.set(entity.canonicalName, {
35039
+ name: entity.canonicalName,
35040
+ kind: existing?.kind ?? entity.kind ?? null,
35041
+ memoryCount: (existing?.memoryCount ?? 0) + count
35042
+ });
35043
+ }
35044
+ return [...byName.values()].sort((a, b) => a.name.localeCompare(b.name));
35045
+ },
35046
+ listProjections: (listOptions) => getVaultMemoryProjectionsOp(vaultCtx, listOptions),
34990
35047
  create: async (input) => {
34991
35048
  const created = await createVaultMemoryOp(vaultCtx, input);
34992
35049
  if (input.embedding === void 0) embedInBackground(created);
@@ -35035,6 +35092,22 @@ function createLocalMemoryStore(options) {
35035
35092
  { vaultCtx, embeddingOptions: options.embeddingOptions, vaultCache },
35036
35093
  retainOptions
35037
35094
  ),
35095
+ factSource: {
35096
+ search: (query, searchOptions) => searchVaultMemoriesWithSize(
35097
+ query,
35098
+ vaultCtx,
35099
+ options.embeddingOptions,
35100
+ vaultCache,
35101
+ searchOptions
35102
+ ),
35103
+ graphRanking: (query, traverse, recallOptions) => buildGraphLaneRanking(
35104
+ query,
35105
+ { vaultCtx, entityCtx, embeddingOptions: options.embeddingOptions },
35106
+ traverse,
35107
+ graphTraversalOptions(recallOptions, traverse)
35108
+ ),
35109
+ temporalRanking: (query, now) => buildTemporalLaneRanking(query, vaultCtx, now)
35110
+ },
35038
35111
  subscribe: (onChange, subscribeOptions) => {
35039
35112
  const afterFirst = () => {
35040
35113
  let first = true;
@@ -35048,7 +35121,6 @@ function createLocalMemoryStore(options) {
35048
35121
  );
35049
35122
  const subscriptions = [memories.subscribe(afterFirst())];
35050
35123
  if (subscribeOptions?.topics) {
35051
- const linksOwnedBy = userId === void 0 ? [] : options.allowUnscopedRows ? [import_watermelondb24.Q.or(import_watermelondb24.Q.where("user_id", userId), import_watermelondb24.Q.where("user_id", null))] : [import_watermelondb24.Q.where("user_id", userId)];
35052
35124
  subscriptions.push(
35053
35125
  entityCtx.memoryEntityCollection.query(...linksOwnedBy).observe().subscribe(afterFirst())
35054
35126
  );
@@ -36731,19 +36803,20 @@ function createMemoryVaultSearchTool(vaultCtx, embeddingOptions, cache, searchOp
36731
36803
  const budget = wantsDecompose ? "high" : searchOptions?.rerank ? "mid" : "low";
36732
36804
  const folderId = searchOptions?.folderId ?? argFolderId;
36733
36805
  if (searchOptions?.useFusion === false) {
36734
- const { results: legacy, embeddingsUnavailable } = await searchVaultMemoriesWithSize(
36806
+ const legacyOptions = {
36807
+ limit: requestLimit,
36808
+ minSimilarity,
36809
+ useFusion: false,
36810
+ ...tuningForward,
36811
+ ...folderId !== void 0 && { folderId },
36812
+ ...searchOptions?.scopes && { scopes: searchOptions.scopes }
36813
+ };
36814
+ const { results: legacy, embeddingsUnavailable } = "retain" in vaultCtx ? await vaultCtx.factSource.search(query, legacyOptions) : await searchVaultMemoriesWithSize(
36735
36815
  query,
36736
36816
  vaultCtx,
36737
36817
  embeddingOptions,
36738
36818
  cache,
36739
- {
36740
- limit: requestLimit,
36741
- minSimilarity,
36742
- useFusion: false,
36743
- ...tuningForward,
36744
- ...folderId !== void 0 && { folderId },
36745
- ...searchOptions?.scopes && { scopes: searchOptions.scopes }
36746
- }
36819
+ legacyOptions
36747
36820
  );
36748
36821
  if (legacy.length === 0) {
36749
36822
  return embeddingsUnavailable ? EMBEDDINGS_DEGRADED_EMPTY_NO_LEXICAL : "No relevant memories found in the vault.";
@@ -36762,7 +36835,7 @@ function createMemoryVaultSearchTool(vaultCtx, embeddingOptions, cache, searchOp
36762
36835
  let recallDegraded = [];
36763
36836
  const result = await recall(
36764
36837
  query,
36765
- { vaultCtx, embeddingOptions, vaultCache: cache },
36838
+ "retain" in vaultCtx ? { factSource: vaultCtx.factSource, embeddingOptions } : { vaultCtx, embeddingOptions, vaultCache: cache },
36766
36839
  {
36767
36840
  onDiagnostics: (d) => {
36768
36841
  recallDegraded = d.degraded;
@@ -36842,8 +36915,10 @@ function describeWriteOutcome(outcome) {
36842
36915
  }
36843
36916
  }
36844
36917
  function createMemoryVaultTool(vaultCtx, options, embeddingOptions, cache) {
36918
+ const store = "retain" in vaultCtx ? vaultCtx : void 0;
36919
+ const ctx = "retain" in vaultCtx ? void 0 : vaultCtx;
36845
36920
  const hasOnSave = !!options?.onSave;
36846
- const folderNames = options?.folderMap ? Array.from(options.folderMap.keys()) : [];
36921
+ const folderNames = !store && options?.folderMap ? Array.from(options.folderMap.keys()) : [];
36847
36922
  return {
36848
36923
  type: "function",
36849
36924
  function: {
@@ -36888,7 +36963,7 @@ function createMemoryVaultTool(vaultCtx, options, embeddingOptions, cache) {
36888
36963
  let previousContent;
36889
36964
  const scope = options?.scope ?? "private";
36890
36965
  if (isUpdate) {
36891
- const existing = await getVaultMemoryOp(vaultCtx, id);
36966
+ const existing = store ? await store.get(id) : await getVaultMemoryOp(ctx, id);
36892
36967
  if (!existing) {
36893
36968
  return `Error: Memory with ID "${id}" not found. Creating a new memory instead would require a separate call without an ID.`;
36894
36969
  }
@@ -36909,9 +36984,18 @@ function createMemoryVaultTool(vaultCtx, options, embeddingOptions, cache) {
36909
36984
  return isUpdate ? `Memory update was cancelled by the user. The memory "${id}" was not modified.` : "Memory save was cancelled by the user. No memory was created.";
36910
36985
  }
36911
36986
  }
36912
- if (isUpdate) {
36987
+ if (isUpdate && store) {
36988
+ const updated = await store.update(id, {
36989
+ content,
36990
+ ...factType !== void 0 && { factType }
36991
+ });
36992
+ if (!updated) {
36993
+ return `Error: Failed to update memory "${id}".`;
36994
+ }
36995
+ return `Memory updated successfully (ID: ${updated.uniqueId}).`;
36996
+ } else if (isUpdate) {
36913
36997
  const folderId = folderName ? options?.folderMap?.get(folderName) : void 0;
36914
- const updated = await updateVaultMemoryOp(vaultCtx, id, {
36998
+ const updated = await updateVaultMemoryOp(ctx, id, {
36915
36999
  content,
36916
37000
  embedding: null,
36917
37001
  folderId,
@@ -36926,7 +37010,7 @@ function createMemoryVaultTool(vaultCtx, options, embeddingOptions, cache) {
36926
37010
  content,
36927
37011
  embeddingOptions,
36928
37012
  cache,
36929
- vaultCtx,
37013
+ ctx,
36930
37014
  id,
36931
37015
  updated.updatedAt
36932
37016
  ).catch(() => {
@@ -36946,18 +37030,22 @@ function createMemoryVaultTool(vaultCtx, options, embeddingOptions, cache) {
36946
37030
  await notifyWritten(options, input, outcome);
36947
37031
  return describeWriteOutcome(outcome);
36948
37032
  }
36949
- const created = await createVaultMemoryOp(vaultCtx, {
37033
+ const created = store ? await store.create({
37034
+ content,
37035
+ scope,
37036
+ ...factType !== void 0 && { factType }
37037
+ }) : await createVaultMemoryOp(ctx, {
36950
37038
  content,
36951
37039
  scope,
36952
37040
  folderId,
36953
37041
  ...factType !== void 0 && { factType }
36954
37042
  });
36955
- if (embeddingOptions && cache) {
37043
+ if (ctx && embeddingOptions && cache) {
36956
37044
  eagerEmbedContent(
36957
37045
  content,
36958
37046
  embeddingOptions,
36959
37047
  cache,
36960
- vaultCtx,
37048
+ ctx,
36961
37049
  created.uniqueId,
36962
37050
  created.updatedAt
36963
37051
  ).catch(() => {