@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.
@@ -26412,22 +26412,35 @@ async function vaultMemoryRawToStored(raw, walletAddress, signMessage, embeddedW
26412
26412
  return stored;
26413
26413
  }
26414
26414
  async function getAllVaultMemoriesOp(ctx, options) {
26415
- const conditions = [
26415
+ const results = await ctx.vaultMemoryCollection.query(...vaultListConditions(ctx, options), ...vaultListOrder(options)).unsafeFetchRaw();
26416
+ return mapInBatches(
26417
+ results,
26418
+ (raw) => vaultMemoryRawToStored(raw, ctx.walletAddress, ctx.signMessage, ctx.embeddedWalletSigner)
26419
+ );
26420
+ }
26421
+ function vaultListConditions(ctx, options) {
26422
+ return [
26416
26423
  ...baseVaultConditions(ctx, options),
26417
26424
  ...options?.scopes?.length ? [import_watermelondb19.Q.where("scope", import_watermelondb19.Q.oneOf(options.scopes))] : [],
26418
26425
  ...visibilityConditions(options?.visibility),
26419
26426
  ...levelConditions(options?.levels),
26420
26427
  ...options?.kinds?.length ? [import_watermelondb19.Q.where("kind", import_watermelondb19.Q.oneOf(options.kinds))] : [],
26421
26428
  ...options?.folderId !== void 0 ? [import_watermelondb19.Q.where("folder_id", options.folderId)] : [],
26422
- ...options?.factTypes?.length ? [import_watermelondb19.Q.where("fact_type", import_watermelondb19.Q.oneOf(options.factTypes))] : [],
26429
+ ...options?.factTypes?.length ? [import_watermelondb19.Q.where("fact_type", import_watermelondb19.Q.oneOf(options.factTypes))] : []
26430
+ ];
26431
+ }
26432
+ function vaultListOrder(options) {
26433
+ return [
26423
26434
  import_watermelondb19.Q.sortBy(options?.since ? "updated_at" : "created_at", import_watermelondb19.Q.desc),
26424
26435
  ...options?.limit !== null && options?.limit !== void 0 && options.limit > 0 ? [import_watermelondb19.Q.take(options.limit)] : []
26425
26436
  ];
26426
- const results = await ctx.vaultMemoryCollection.query(...conditions).unsafeFetchRaw();
26427
- return mapInBatches(
26428
- results,
26429
- (raw) => vaultMemoryRawToStored(raw, ctx.walletAddress, ctx.signMessage, ctx.embeddedWalletSigner)
26430
- );
26437
+ }
26438
+ async function getVaultMemoryProjectionsOp(ctx, options) {
26439
+ const results = await ctx.vaultMemoryCollection.query(...vaultListConditions(ctx, options), ...vaultListOrder(options)).unsafeFetchRaw();
26440
+ return results.map((raw) => ({
26441
+ ...vaultMemoryRawToRankable(raw),
26442
+ topicsUserManaged: raw.topics_user_managed === true || raw.topics_user_managed === 1
26443
+ }));
26431
26444
  }
26432
26445
  function vaultMemoryRawToRankable(raw) {
26433
26446
  return {
@@ -32229,7 +32242,6 @@ async function recall(query, ctx, options = {}) {
32229
32242
  }
32230
32243
  const needsChunkEmbedding = types.includes("chunk") && ctx.storageCtx;
32231
32244
  const wantsTemporal = types.includes("fact") && (ctx.factSource || ctx.vaultCtx);
32232
- const graphRefiner = flags.traverse && options.graphRefine && options.decomposeOptions ? createLlmNeighborRefiner(options.decomposeOptions) : void 0;
32233
32245
  const queryEmbedTotalTimeoutMs = options.queryEmbedTotalTimeoutMs ?? DEFAULT_QUERY_EMBED_TOTAL_TIMEOUT_MS;
32234
32246
  const prepStart = nowMs2();
32235
32247
  let sharedEmbedMs = 0;
@@ -32258,13 +32270,12 @@ async function recall(query, ctx, options = {}) {
32258
32270
  safeLane(
32259
32271
  "graph",
32260
32272
  () => graphLaneFailed = true,
32261
- () => ctx.factSource ? ctx.factSource.graphRanking(query, flags.traverse, options) : buildGraphLaneRanking(query, ctx, flags.traverse, {
32262
- ...options.maxHops !== void 0 && { maxHops: options.maxHops },
32263
- ...options.entityFanout !== void 0 && { entityFanout: options.entityFanout },
32264
- ...options.nodeBudget !== void 0 && { nodeBudget: options.nodeBudget },
32265
- ...options.rrfK !== void 0 && { rrfK: options.rrfK },
32266
- ...graphRefiner && { refineNeighbors: graphRefiner }
32267
- })
32273
+ () => ctx.factSource ? ctx.factSource.graphRanking(query, flags.traverse, options) : buildGraphLaneRanking(
32274
+ query,
32275
+ ctx,
32276
+ flags.traverse,
32277
+ graphTraversalOptions(options, flags.traverse)
32278
+ )
32268
32279
  ),
32269
32280
  wantsTemporal ? safeLane(
32270
32281
  "temporal",
@@ -32483,6 +32494,16 @@ function toFactMemory(r, now) {
32483
32494
  updatedAt: r.updatedAt ?? /* @__PURE__ */ new Date(0)
32484
32495
  };
32485
32496
  }
32497
+ function graphTraversalOptions(options, traverse) {
32498
+ const refineNeighbors = traverse && options.graphRefine && options.decomposeOptions ? createLlmNeighborRefiner(options.decomposeOptions) : void 0;
32499
+ return {
32500
+ ...options.maxHops !== void 0 && { maxHops: options.maxHops },
32501
+ ...options.entityFanout !== void 0 && { entityFanout: options.entityFanout },
32502
+ ...options.nodeBudget !== void 0 && { nodeBudget: options.nodeBudget },
32503
+ ...options.rrfK !== void 0 && { rrfK: options.rrfK },
32504
+ ...refineNeighbors && { refineNeighbors }
32505
+ };
32506
+ }
32486
32507
  async function buildGraphLaneRanking(query, ctx, traverse = false, traversalOptions = {}) {
32487
32508
  const entityCtx = ctx.entityCtx ?? ctx.vaultCtx?.entityCtx;
32488
32509
  if (!entityCtx) return [];
@@ -32648,24 +32669,33 @@ function isDumpQuery(query) {
32648
32669
  return false;
32649
32670
  }
32650
32671
  async function listRecentFacts(ctx, limit, toolOptions) {
32672
+ const store = toolOptions?.memoryStore;
32651
32673
  const vaultCtx = ctx.vaultCtx;
32652
- if (!vaultCtx) return [];
32653
- const ordered = await getVaultRankingProjectionsOp(vaultCtx, {
32674
+ if (!vaultCtx && !store) return [];
32675
+ const read = (ids) => store ? store.list({ memoryIds: ids }) : getVaultMemoriesByIdsOp(vaultCtx, ids);
32676
+ const filters = {
32654
32677
  ...toolOptions?.scopes && { scopes: toolOptions.scopes },
32655
- ...toolOptions?.memoryIds !== void 0 && { memoryIds: toolOptions.memoryIds },
32678
+ ...toolOptions?.memoryIds !== void 0 && { memoryIds: toolOptions.memoryIds }
32679
+ };
32680
+ const project = (size) => store ? store.listProjections({ ...filters, limit: size }) : getVaultRankingProjectionsOp(vaultCtx, {
32681
+ ...filters,
32656
32682
  ...toolOptions?.folderId !== void 0 && { folderId: toolOptions.folderId }
32657
32683
  });
32658
32684
  const readable = [];
32659
- for (let i = 0; i < ordered.length && readable.length < limit; i += limit) {
32660
- const batch = ordered.slice(i, i + limit).map((p) => p.uniqueId);
32661
- const byId = new Map(
32662
- (await getVaultMemoriesByIdsOp(vaultCtx, batch)).map((m) => [m.uniqueId, m])
32663
- );
32664
- for (const id of batch) {
32665
- const m = byId.get(id);
32666
- if (m && !isEncrypted(m.content)) readable.push(m);
32667
- if (readable.length >= limit) break;
32685
+ let start = 0;
32686
+ for (let size = limit; readable.length < limit; size *= 2) {
32687
+ const ordered = await project(size);
32688
+ for (let i = start; i < ordered.length && readable.length < limit; i += limit) {
32689
+ const batch = ordered.slice(i, i + limit).map((p) => p.uniqueId);
32690
+ const byId = new Map((await read(batch)).map((m) => [m.uniqueId, m]));
32691
+ for (const id of batch) {
32692
+ const m = byId.get(id);
32693
+ if (m && !isEncrypted(m.content)) readable.push(m);
32694
+ if (readable.length >= limit) break;
32695
+ }
32668
32696
  }
32697
+ if (!store || ordered.length < size) break;
32698
+ start = ordered.length;
32669
32699
  }
32670
32700
  return readable.map((m) => ({
32671
32701
  id: m.uniqueId,
@@ -32682,6 +32712,8 @@ async function listRecentFacts(ctx, limit, toolOptions) {
32682
32712
  }));
32683
32713
  }
32684
32714
  function createRecallTool(ctx, toolOptions, callbacks) {
32715
+ if (toolOptions?.memoryStore && toolOptions.folderId !== void 0)
32716
+ throw new Error("Folders are not supported with a memoryStore");
32685
32717
  const defaultTypes = toolOptions?.types ?? ["fact", "chunk"];
32686
32718
  const defaultLimit = toolOptions?.limit ?? DEFAULT_LIMIT2;
32687
32719
  const defaultBudget = toolOptions?.budget ?? DEFAULT_BUDGET2;
@@ -32753,7 +32785,7 @@ function createRecallTool(ctx, toolOptions, callbacks) {
32753
32785
  let surfaced = 0;
32754
32786
  try {
32755
32787
  let recallDegraded = [];
32756
- const wantsRecent = args.sort === "recent" && defaultTypes.includes("fact") && ctx.vaultCtx !== void 0;
32788
+ const wantsRecent = args.sort === "recent" && defaultTypes.includes("fact") && (ctx.vaultCtx !== void 0 || toolOptions?.memoryStore !== void 0);
32757
32789
  let result;
32758
32790
  if (wantsRecent) {
32759
32791
  result = { memories: await listRecentFacts(ctx, effectiveLimit, toolOptions) };
@@ -33222,6 +33254,7 @@ function createLocalMemoryStore(options) {
33222
33254
  };
33223
33255
  const vaultCache = options.vaultCache ?? createVaultEmbeddingCache();
33224
33256
  const ownedBy = userId !== void 0 ? [import_watermelondb24.Q.where("user_id", userId)] : [];
33257
+ 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)];
33225
33258
  const watchedColumns = [
33226
33259
  "content",
33227
33260
  "scope",
@@ -33254,6 +33287,7 @@ function createLocalMemoryStore(options) {
33254
33287
  });
33255
33288
  };
33256
33289
  return {
33290
+ masksEmbeddingInput: options.embeddingOptions.maskInput !== void 0,
33257
33291
  list: (listOptions) => getAllVaultMemoriesOp(vaultCtx, listOptions),
33258
33292
  get: (id) => getVaultMemoryOp(vaultCtx, id),
33259
33293
  listArchived: async () => {
@@ -33264,6 +33298,29 @@ function createLocalMemoryStore(options) {
33264
33298
  },
33265
33299
  memoriesByTopics: (names) => getMemoriesByEntityNamesOp(entityCtx, names),
33266
33300
  topicsByMemories: (memoryIds) => getEntitiesByMemoryIdsOp(entityCtx, memoryIds),
33301
+ listTopics: async () => {
33302
+ const links = await entityCtx.memoryEntityCollection.query(...linksOwnedBy).fetch();
33303
+ const members = /* @__PURE__ */ new Map();
33304
+ for (const link of links) {
33305
+ const entityId = String(link.entityId);
33306
+ const set = members.get(entityId) ?? /* @__PURE__ */ new Set();
33307
+ set.add(String(link.memoryId));
33308
+ members.set(entityId, set);
33309
+ }
33310
+ 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() : [];
33311
+ const byName = /* @__PURE__ */ new Map();
33312
+ for (const entity of entities) {
33313
+ const count = members.get(entity.id)?.size ?? 0;
33314
+ const existing = byName.get(entity.canonicalName);
33315
+ byName.set(entity.canonicalName, {
33316
+ name: entity.canonicalName,
33317
+ kind: existing?.kind ?? entity.kind ?? null,
33318
+ memoryCount: (existing?.memoryCount ?? 0) + count
33319
+ });
33320
+ }
33321
+ return [...byName.values()].sort((a, b) => a.name.localeCompare(b.name));
33322
+ },
33323
+ listProjections: (listOptions) => getVaultMemoryProjectionsOp(vaultCtx, listOptions),
33267
33324
  create: async (input) => {
33268
33325
  const created = await createVaultMemoryOp(vaultCtx, input);
33269
33326
  if (input.embedding === void 0) embedInBackground(created);
@@ -33312,6 +33369,22 @@ function createLocalMemoryStore(options) {
33312
33369
  { vaultCtx, embeddingOptions: options.embeddingOptions, vaultCache },
33313
33370
  retainOptions
33314
33371
  ),
33372
+ factSource: {
33373
+ search: (query, searchOptions) => searchVaultMemoriesWithSize(
33374
+ query,
33375
+ vaultCtx,
33376
+ options.embeddingOptions,
33377
+ vaultCache,
33378
+ searchOptions
33379
+ ),
33380
+ graphRanking: (query, traverse, recallOptions) => buildGraphLaneRanking(
33381
+ query,
33382
+ { vaultCtx, entityCtx, embeddingOptions: options.embeddingOptions },
33383
+ traverse,
33384
+ graphTraversalOptions(recallOptions, traverse)
33385
+ ),
33386
+ temporalRanking: (query, now) => buildTemporalLaneRanking(query, vaultCtx, now)
33387
+ },
33315
33388
  subscribe: (onChange, subscribeOptions) => {
33316
33389
  const afterFirst = () => {
33317
33390
  let first = true;
@@ -33325,7 +33398,6 @@ function createLocalMemoryStore(options) {
33325
33398
  );
33326
33399
  const subscriptions = [memories.subscribe(afterFirst())];
33327
33400
  if (subscribeOptions?.topics) {
33328
- 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)];
33329
33401
  subscriptions.push(
33330
33402
  entityCtx.memoryEntityCollection.query(...linksOwnedBy).observe().subscribe(afterFirst())
33331
33403
  );
@@ -35003,19 +35075,20 @@ function createMemoryVaultSearchTool(vaultCtx, embeddingOptions, cache, searchOp
35003
35075
  const budget = wantsDecompose ? "high" : searchOptions?.rerank ? "mid" : "low";
35004
35076
  const folderId = searchOptions?.folderId ?? argFolderId;
35005
35077
  if (searchOptions?.useFusion === false) {
35006
- const { results: legacy, embeddingsUnavailable } = await searchVaultMemoriesWithSize(
35078
+ const legacyOptions = {
35079
+ limit: requestLimit,
35080
+ minSimilarity,
35081
+ useFusion: false,
35082
+ ...tuningForward,
35083
+ ...folderId !== void 0 && { folderId },
35084
+ ...searchOptions?.scopes && { scopes: searchOptions.scopes }
35085
+ };
35086
+ const { results: legacy, embeddingsUnavailable } = "retain" in vaultCtx ? await vaultCtx.factSource.search(query, legacyOptions) : await searchVaultMemoriesWithSize(
35007
35087
  query,
35008
35088
  vaultCtx,
35009
35089
  embeddingOptions,
35010
35090
  cache,
35011
- {
35012
- limit: requestLimit,
35013
- minSimilarity,
35014
- useFusion: false,
35015
- ...tuningForward,
35016
- ...folderId !== void 0 && { folderId },
35017
- ...searchOptions?.scopes && { scopes: searchOptions.scopes }
35018
- }
35091
+ legacyOptions
35019
35092
  );
35020
35093
  if (legacy.length === 0) {
35021
35094
  return embeddingsUnavailable ? EMBEDDINGS_DEGRADED_EMPTY_NO_LEXICAL : "No relevant memories found in the vault.";
@@ -35034,7 +35107,7 @@ function createMemoryVaultSearchTool(vaultCtx, embeddingOptions, cache, searchOp
35034
35107
  let recallDegraded = [];
35035
35108
  const result = await recall(
35036
35109
  query,
35037
- { vaultCtx, embeddingOptions, vaultCache: cache },
35110
+ "retain" in vaultCtx ? { factSource: vaultCtx.factSource, embeddingOptions } : { vaultCtx, embeddingOptions, vaultCache: cache },
35038
35111
  {
35039
35112
  onDiagnostics: (d) => {
35040
35113
  recallDegraded = d.degraded;
@@ -35114,8 +35187,10 @@ function describeWriteOutcome(outcome) {
35114
35187
  }
35115
35188
  }
35116
35189
  function createMemoryVaultTool(vaultCtx, options, embeddingOptions, cache) {
35190
+ const store = "retain" in vaultCtx ? vaultCtx : void 0;
35191
+ const ctx = "retain" in vaultCtx ? void 0 : vaultCtx;
35117
35192
  const hasOnSave = !!options?.onSave;
35118
- const folderNames = options?.folderMap ? Array.from(options.folderMap.keys()) : [];
35193
+ const folderNames = !store && options?.folderMap ? Array.from(options.folderMap.keys()) : [];
35119
35194
  return {
35120
35195
  type: "function",
35121
35196
  function: {
@@ -35160,7 +35235,7 @@ function createMemoryVaultTool(vaultCtx, options, embeddingOptions, cache) {
35160
35235
  let previousContent;
35161
35236
  const scope = options?.scope ?? "private";
35162
35237
  if (isUpdate) {
35163
- const existing = await getVaultMemoryOp(vaultCtx, id);
35238
+ const existing = store ? await store.get(id) : await getVaultMemoryOp(ctx, id);
35164
35239
  if (!existing) {
35165
35240
  return `Error: Memory with ID "${id}" not found. Creating a new memory instead would require a separate call without an ID.`;
35166
35241
  }
@@ -35181,9 +35256,18 @@ function createMemoryVaultTool(vaultCtx, options, embeddingOptions, cache) {
35181
35256
  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.";
35182
35257
  }
35183
35258
  }
35184
- if (isUpdate) {
35259
+ if (isUpdate && store) {
35260
+ const updated = await store.update(id, {
35261
+ content,
35262
+ ...factType !== void 0 && { factType }
35263
+ });
35264
+ if (!updated) {
35265
+ return `Error: Failed to update memory "${id}".`;
35266
+ }
35267
+ return `Memory updated successfully (ID: ${updated.uniqueId}).`;
35268
+ } else if (isUpdate) {
35185
35269
  const folderId = folderName ? options?.folderMap?.get(folderName) : void 0;
35186
- const updated = await updateVaultMemoryOp(vaultCtx, id, {
35270
+ const updated = await updateVaultMemoryOp(ctx, id, {
35187
35271
  content,
35188
35272
  embedding: null,
35189
35273
  folderId,
@@ -35198,7 +35282,7 @@ function createMemoryVaultTool(vaultCtx, options, embeddingOptions, cache) {
35198
35282
  content,
35199
35283
  embeddingOptions,
35200
35284
  cache,
35201
- vaultCtx,
35285
+ ctx,
35202
35286
  id,
35203
35287
  updated.updatedAt
35204
35288
  ).catch(() => {
@@ -35218,18 +35302,22 @@ function createMemoryVaultTool(vaultCtx, options, embeddingOptions, cache) {
35218
35302
  await notifyWritten(options, input, outcome);
35219
35303
  return describeWriteOutcome(outcome);
35220
35304
  }
35221
- const created = await createVaultMemoryOp(vaultCtx, {
35305
+ const created = store ? await store.create({
35306
+ content,
35307
+ scope,
35308
+ ...factType !== void 0 && { factType }
35309
+ }) : await createVaultMemoryOp(ctx, {
35222
35310
  content,
35223
35311
  scope,
35224
35312
  folderId,
35225
35313
  ...factType !== void 0 && { factType }
35226
35314
  });
35227
- if (embeddingOptions && cache) {
35315
+ if (ctx && embeddingOptions && cache) {
35228
35316
  eagerEmbedContent(
35229
35317
  content,
35230
35318
  embeddingOptions,
35231
35319
  cache,
35232
- vaultCtx,
35320
+ ctx,
35233
35321
  created.uniqueId,
35234
35322
  created.updatedAt
35235
35323
  ).catch(() => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@anuma/sdk",
3
- "version": "6.37.0",
3
+ "version": "6.39.0",
4
4
  "description": "",
5
5
  "main": "./dist/index.cjs",
6
6
  "module": "./dist/index.mjs",