@anuma/sdk 7.0.3 → 7.0.4-next.20261015010856

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.
@@ -16574,6 +16574,10 @@ __export(index_exports, {
16574
16574
  });
16575
16575
  module.exports = __toCommonJS(index_exports);
16576
16576
 
16577
+ // src/lib/chat/toolResultMessage.ts
16578
+ var FOOTER = "\n\nBased on these results, continue with the task.";
16579
+ var TOOL_RESULT_FOOTER_LINE = FOOTER.trim();
16580
+
16577
16581
  // src/lib/tools/toolCatalog.ts
16578
16582
  var TOOL_CATALOG = {
16579
16583
  gmail_search_messages: { label: "Search emails", provider: "gmail", connector: "Gmail" },
@@ -16750,7 +16754,32 @@ ${neutralised}
16750
16754
  }
16751
16755
 
16752
16756
  // src/lib/chat/toolResults.ts
16757
+ var TOOL_RESULTS_PREFIX = "[Tool Execution Results]";
16758
+ var TOOL_LINE = /^Tool "([^"]+)" returned: (.*)$/;
16753
16759
  var TOOL_RESULT_ORIGIN = "tool_result";
16760
+ function isToolResultsRow(row) {
16761
+ if (row.origin === TOOL_RESULT_ORIGIN) return true;
16762
+ if (typeof row.origin === "string") return false;
16763
+ if (row.role !== "user" || !row.content.startsWith(TOOL_RESULTS_PREFIX)) return false;
16764
+ return parseToolResultSegments(row.content).length > 0;
16765
+ }
16766
+ function parseToolResultSegments(content) {
16767
+ const segments = [];
16768
+ for (const raw of content.split("\n")) {
16769
+ const line = raw.trim();
16770
+ const match = TOOL_LINE.exec(line);
16771
+ if (match) {
16772
+ segments.push({ name: match[1], line });
16773
+ continue;
16774
+ }
16775
+ const current = segments[segments.length - 1];
16776
+ if (!current || line === "" || line === TOOL_RESULT_FOOTER_LINE) continue;
16777
+ if (line === TOOL_RESULTS_PREFIX) continue;
16778
+ current.line = `${current.line}
16779
+ ${line}`;
16780
+ }
16781
+ return segments;
16782
+ }
16754
16783
 
16755
16784
  // src/lib/db/chat/operations.ts
16756
16785
  var import_watermelondb2 = __toESM(require_watermelondb());
@@ -18373,8 +18402,9 @@ async function getConversationsByProjectOp(ctx, projectId) {
18373
18402
  )
18374
18403
  );
18375
18404
  }
18376
- async function getMessagesOp(ctx, convId) {
18377
- const results = await ctx.messagesCollection.query(import_watermelondb2.Q.where("conversation_id", convId), import_watermelondb2.Q.sortBy("message_id", import_watermelondb2.Q.asc)).unsafeFetchRaw();
18405
+ async function getMessagesOp(ctx, convId, opts) {
18406
+ const fetched = await ctx.messagesCollection.query(import_watermelondb2.Q.where("conversation_id", convId), import_watermelondb2.Q.sortBy("message_id", import_watermelondb2.Q.asc)).unsafeFetchRaw();
18407
+ const results = opts?.rawFilter ? fetched.filter(opts.rawFilter) : fetched;
18378
18408
  return Promise.all(
18379
18409
  results.map(
18380
18410
  (raw) => messageRawToStored(raw, ctx.walletAddress, ctx.signMessage, ctx.embeddedWalletSigner)
@@ -20498,6 +20528,37 @@ async function chunkAndEmbedMessage(ctx, messageId, options) {
20498
20528
  }));
20499
20529
  return updateMessageChunksOp(ctx, messageId, messageChunks, embeddingModel);
20500
20530
  }
20531
+ var MIN_SEALED_EMBEDDING_HEX = 256;
20532
+ function rawHasEmbedding(value) {
20533
+ if (Array.isArray(value)) return value.length > 0;
20534
+ if (typeof value !== "string" || value.length === 0) return false;
20535
+ if (isEncrypted(value)) {
20536
+ return value.length - value.indexOf(":", 4) - 1 >= MIN_SEALED_EMBEDDING_HEX;
20537
+ }
20538
+ try {
20539
+ const parsed = JSON.parse(value);
20540
+ return Array.isArray(parsed) && parsed.length > 0;
20541
+ } catch {
20542
+ return false;
20543
+ }
20544
+ }
20545
+ function sealedVersion(value) {
20546
+ if (typeof value !== "string" || !isEncrypted(value)) return null;
20547
+ return value.startsWith("enc:v2:") ? "v2" : "v3";
20548
+ }
20549
+ function mayNeedChunkEmbedding(raw, embeddingModel, filter) {
20550
+ const role = raw.role;
20551
+ if (role === "system") return false;
20552
+ if (filter?.roles && !filter.roles.includes(role ?? "")) return false;
20553
+ if (isNonEmbeddableOrigin(raw.origin, filter?.reembedDiscarded)) {
20554
+ return false;
20555
+ }
20556
+ const model = raw.embedding_model;
20557
+ if (typeof model === "string" && model !== embeddingModel) return true;
20558
+ if (rawHasEmbedding(raw.chunks)) return false;
20559
+ if (rawHasEmbedding(raw.vector) && !filter?.rechunkExisting) return false;
20560
+ return true;
20561
+ }
20501
20562
  async function chunkAndEmbedAllMessages(ctx, options, filter) {
20502
20563
  const embeddingModel = options.model ?? DEFAULT_API_EMBEDDING_MODEL;
20503
20564
  const { chunkSize = DEFAULT_CHUNK_SIZE } = options;
@@ -20510,11 +20571,31 @@ async function chunkAndEmbedAllMessages(ctx, options, filter) {
20510
20571
  let considered = 0;
20511
20572
  let sealedRowsSeen = 0;
20512
20573
  let rowsSeen = 0;
20574
+ const keptSealed = /* @__PURE__ */ new Map();
20575
+ const candidatesStillSealed = /* @__PURE__ */ new Map();
20576
+ const droppedSealed = /* @__PURE__ */ new Map();
20577
+ const rawFilter = (raw) => {
20578
+ rowsSeen++;
20579
+ const keep = mayNeedChunkEmbedding(raw, embeddingModel, filter);
20580
+ const version = sealedVersion(raw.content);
20581
+ if (!version) return keep;
20582
+ if (keep) {
20583
+ keptSealed.set(version, (keptSealed.get(version) ?? 0) + 1);
20584
+ } else {
20585
+ const dropped = droppedSealed.get(version);
20586
+ if (dropped) dropped.count++;
20587
+ else droppedSealed.set(version, { count: 1, probeId: raw.id });
20588
+ }
20589
+ return keep;
20590
+ };
20513
20591
  for (const conv of targetConversations) {
20514
- const messages = await getMessagesOp(ctx, conv.conversationId);
20592
+ const messages = await getMessagesOp(ctx, conv.conversationId, { rawFilter });
20515
20593
  for (const message of messages) {
20516
- rowsSeen++;
20517
- if (isEncrypted(message.content)) sealedRowsSeen++;
20594
+ const stillSealed = sealedVersion(message.content);
20595
+ if (stillSealed) {
20596
+ sealedRowsSeen++;
20597
+ candidatesStillSealed.set(stillSealed, (candidatesStillSealed.get(stillSealed) ?? 0) + 1);
20598
+ }
20518
20599
  const isStale = message.embeddingModel !== void 0 && message.embeddingModel !== null && message.embeddingModel !== embeddingModel;
20519
20600
  if (message.chunks && message.chunks.length > 0 && !isStale) continue;
20520
20601
  const hasVector = message.vector && message.vector.length > 0;
@@ -20527,6 +20608,7 @@ async function chunkAndEmbedAllMessages(ctx, options, filter) {
20527
20608
  stillEncrypted++;
20528
20609
  continue;
20529
20610
  }
20611
+ if (isToolResultsRow(message)) continue;
20530
20612
  if (message.content.length < minLength) continue;
20531
20613
  if (shouldChunkMessage(message.content, chunkSize)) {
20532
20614
  longMessages.push({
@@ -20538,6 +20620,12 @@ async function chunkAndEmbedAllMessages(ctx, options, filter) {
20538
20620
  }
20539
20621
  }
20540
20622
  }
20623
+ for (const [version, dropped] of droppedSealed) {
20624
+ const keyOpenedACandidate = (keptSealed.get(version) ?? 0) > (candidatesStillSealed.get(version) ?? 0);
20625
+ if (keyOpenedACandidate) continue;
20626
+ const probe = await getMessageOp(ctx, dropped.probeId);
20627
+ if (!probe || isEncrypted(probe.content)) sealedRowsSeen += dropped.count;
20628
+ }
20541
20629
  if (stillEncrypted > 0 || sealedRowsSeen > 0) {
20542
20630
  getLogger().error(
20543
20631
  "memoryEngine: messages still encrypted (key unavailable?) \u2014 excluded from embedding",
@@ -27712,6 +27800,11 @@ function parseTopics(value) {
27712
27800
  }
27713
27801
 
27714
27802
  // src/lib/db/entities/operations.ts
27803
+ async function assertCanWrite(ctx) {
27804
+ if (ctx.canWrite && !await ctx.canWrite()) {
27805
+ throw new Error("Memory source is no longer eligible");
27806
+ }
27807
+ }
27715
27808
  function entityToStored(e) {
27716
27809
  return {
27717
27810
  uniqueId: e.id,
@@ -27852,6 +27945,7 @@ async function linkMemoryEntitiesOp(ctx, memoryId, entityInputs, options) {
27852
27945
  let skipped = false;
27853
27946
  let entities = [];
27854
27947
  await ctx.database.write(async () => {
27948
+ await assertCanWrite(ctx);
27855
27949
  const {
27856
27950
  entities: byName,
27857
27951
  operations: entityOps,
@@ -27927,6 +28021,7 @@ async function replaceMemoryEntities(ctx, memoryId, entityInputs, options) {
27927
28021
  let skipped = false;
27928
28022
  let entities = [];
27929
28023
  await ctx.database.write(async () => {
28024
+ await assertCanWrite(ctx);
27930
28025
  const vaultRow = await findVaultRowInWrite(ctx, memoryId);
27931
28026
  if (options.guarded && autoLinkBlocked(vaultRow)) {
27932
28027
  skipped = true;
@@ -27987,6 +28082,7 @@ async function unlinkMemoryEntitiesOp(ctx, memoryIds) {
27987
28082
  const links = await ctx.memoryEntityCollection.query(import_watermelondb18.Q.where("memory_id", import_watermelondb18.Q.oneOf(Array.from(memoryIds)))).fetch();
27988
28083
  if (links.length === 0) return;
27989
28084
  await ctx.database.write(async () => {
28085
+ await assertCanWrite(ctx);
27990
28086
  await ctx.database.batch(...links.map((l) => l.prepareDestroyPermanently()));
27991
28087
  });
27992
28088
  }
@@ -27995,6 +28091,7 @@ async function unlinkAllMemoryEntitiesForUserOp(ctx, userId) {
27995
28091
  const links = await ctx.memoryEntityCollection.query(import_watermelondb18.Q.where("user_id", userId)).fetch();
27996
28092
  if (links.length === 0) return;
27997
28093
  await ctx.database.write(async () => {
28094
+ await assertCanWrite(ctx);
27998
28095
  await ctx.database.batch(...links.map((l) => l.prepareDestroyPermanently()));
27999
28096
  });
28000
28097
  }
@@ -28159,6 +28256,11 @@ function visibilityConditions(requested) {
28159
28256
  function isKinded(record) {
28160
28257
  return record.kind !== null && record.kind !== void 0;
28161
28258
  }
28259
+ async function assertCanWrite2(ctx) {
28260
+ if (ctx.canWrite && !await ctx.canWrite()) {
28261
+ throw new Error("Memory source is no longer eligible");
28262
+ }
28263
+ }
28162
28264
  function assertValidKindLevel(kind, level) {
28163
28265
  const hasKind = kind !== null && kind !== void 0;
28164
28266
  if (hasKind && !MEMORY_KINDS.includes(kind)) {
@@ -28339,8 +28441,7 @@ async function createVaultMemoryOp(ctx, opts) {
28339
28441
  ctx.embeddedWalletSigner
28340
28442
  ) : opts.content;
28341
28443
  const created = await ctx.database.write(async () => {
28342
- if (ctx.canWrite && !await ctx.canWrite())
28343
- throw new Error("Memory source is no longer eligible");
28444
+ await assertCanWrite2(ctx);
28344
28445
  return ctx.vaultMemoryCollection.create(
28345
28446
  (record) => populateNewVaultMemory(record, ctx, opts, encryptedContent, encryptedKindValue)
28346
28447
  );
@@ -28359,8 +28460,7 @@ async function createSupersedingMemoryOp(ctx, opts, targetId) {
28359
28460
  ) : opts.content;
28360
28461
  let createdRecord = null;
28361
28462
  await ctx.database.write(async () => {
28362
- if (ctx.canWrite && !await ctx.canWrite())
28363
- throw new Error("Memory source is no longer eligible");
28463
+ await assertCanWrite2(ctx);
28364
28464
  let target;
28365
28465
  try {
28366
28466
  target = await ctx.vaultMemoryCollection.find(targetId);
@@ -28452,8 +28552,7 @@ async function createVaultMemoriesBatchOp(ctx, optionsArray) {
28452
28552
  })
28453
28553
  );
28454
28554
  const created = await ctx.database.write(async () => {
28455
- if (ctx.canWrite && !await ctx.canWrite())
28456
- throw new Error("Memory source is no longer eligible");
28555
+ await assertCanWrite2(ctx);
28457
28556
  const prepared = optionsArray.map(
28458
28557
  (opts, i) => ctx.vaultMemoryCollection.prepareCreate(
28459
28558
  (record) => populateNewVaultMemory(record, ctx, opts, encryptedContents[i], encryptedKindValues[i])
@@ -28744,8 +28843,7 @@ async function updateVaultMemoryOp(ctx, id, opts) {
28744
28843
  const record = probe;
28745
28844
  const originalUpdatedAt = record.updatedAt.getTime();
28746
28845
  await ctx.database.write(async () => {
28747
- if (ctx.canWrite && !await ctx.canWrite())
28748
- throw new Error("Memory source is no longer eligible");
28846
+ await assertCanWrite2(ctx);
28749
28847
  if (record.isDeleted || record.supersededBy || !isOwnedByCtxUser(ctx, record) || opts.freeFormOnly && isKinded(record)) {
28750
28848
  stale = true;
28751
28849
  return;
@@ -28856,6 +28954,7 @@ async function setMemoryEntitiesOp(ctx, memoryId, entities) {
28856
28954
  let stale = false;
28857
28955
  const originalUpdatedAt = record.updatedAt.getTime();
28858
28956
  await ctx.database.write(async (writer) => {
28957
+ await assertCanWrite2(ctx);
28859
28958
  if (record.isDeleted || !isOwnedByCtxUser(ctx, record)) {
28860
28959
  stale = true;
28861
28960
  return;
@@ -28897,6 +28996,7 @@ async function clearMemoryTopicsOverrideOp(ctx, memoryId, options) {
28897
28996
  const originalUpdatedAt = record.updatedAt.getTime();
28898
28997
  let cleared = false;
28899
28998
  await ctx.database.write(async () => {
28999
+ await assertCanWrite2(ctx);
28900
29000
  if (options?.unlessTopicsRecorded && parseTopics(record.topics) !== null) return;
28901
29001
  cleared = true;
28902
29002
  await record.update((r) => {
@@ -28920,6 +29020,7 @@ async function setMemoryVisibilityOp(ctx, id, opts) {
28920
29020
  if (record.isDeleted || !isOwnedByCtxUser(ctx, record)) return null;
28921
29021
  let stale = false;
28922
29022
  await ctx.database.write(async () => {
29023
+ await assertCanWrite2(ctx);
28923
29024
  if (record.isDeleted || !isOwnedByCtxUser(ctx, record)) {
28924
29025
  stale = true;
28925
29026
  return;
@@ -28948,6 +29049,7 @@ async function deleteVaultMemoryOp(ctx, id) {
28948
29049
  if (record.isDeleted || !isOwnedByCtxUser(ctx, record)) return false;
28949
29050
  let stale = false;
28950
29051
  await ctx.database.write(async () => {
29052
+ await assertCanWrite2(ctx);
28951
29053
  if (record.isDeleted || !isOwnedByCtxUser(ctx, record)) {
28952
29054
  stale = true;
28953
29055
  return;
@@ -28985,8 +29087,7 @@ async function supersedeVaultMemoryOp(ctx, id, supersededById) {
28985
29087
  }
28986
29088
  let stale = false;
28987
29089
  await ctx.database.write(async () => {
28988
- if (ctx.canWrite && !await ctx.canWrite())
28989
- throw new Error("Memory source is no longer eligible");
29090
+ await assertCanWrite2(ctx);
28990
29091
  if (record.isDeleted || record.supersededBy || !isOwnedByCtxUser(ctx, record)) {
28991
29092
  stale = true;
28992
29093
  return;
@@ -29148,6 +29249,7 @@ async function stampTopicsExtractedAtOp(ctx, memoryIds, extractedAt, version = T
29148
29249
  for (let i = 0; i < uniqueIds.length; i += CHUNK) {
29149
29250
  const chunkIds = uniqueIds.slice(i, i + CHUNK);
29150
29251
  await ctx.database.write(async () => {
29252
+ await assertCanWrite2(ctx);
29151
29253
  const records = [];
29152
29254
  for (const id of chunkIds) {
29153
29255
  try {
@@ -29181,6 +29283,7 @@ async function relinkMemoryTopicsOp(ctx, memoryIds) {
29181
29283
  for (const id of Array.from(new Set(memoryIds))) {
29182
29284
  try {
29183
29285
  await ctx.database.write(async (writer) => {
29286
+ await assertCanWrite2(ctx);
29184
29287
  let record;
29185
29288
  try {
29186
29289
  record = await ctx.vaultMemoryCollection.find(id);
@@ -29211,6 +29314,7 @@ async function backfillMemoryTopicsOp(ctx, memoryIds) {
29211
29314
  for (let i = 0; i < uniqueIds.length; i += CHUNK) {
29212
29315
  const chunkIds = uniqueIds.slice(i, i + CHUNK);
29213
29316
  await ctx.database.write(async () => {
29317
+ await assertCanWrite2(ctx);
29214
29318
  const writes = [];
29215
29319
  for (const id of chunkIds) {
29216
29320
  let record;
@@ -29243,6 +29347,7 @@ async function deleteAllVaultMemoriesForUserOp(ctx, userId) {
29243
29347
  const records = await ctx.vaultMemoryCollection.query(import_watermelondb19.Q.where("user_id", userId), import_watermelondb19.Q.where("is_deleted", false)).fetch();
29244
29348
  if (records.length === 0) return 0;
29245
29349
  await ctx.database.write(async () => {
29350
+ await assertCanWrite2(ctx);
29246
29351
  const prepared = records.map(
29247
29352
  (record) => record.prepareUpdate((r) => {
29248
29353
  r._setRaw("is_deleted", true);
@@ -29297,6 +29402,7 @@ async function archiveVaultMemoryOp(ctx, id, opts) {
29297
29402
  let stale = false;
29298
29403
  const archivedAtValue = opts?.now ?? Date.now();
29299
29404
  await ctx.database.write(async () => {
29405
+ await assertCanWrite2(ctx);
29300
29406
  if (record.isDeleted || !isOwnedByCtxUser(ctx, record) || record.archivedAt !== null) {
29301
29407
  stale = true;
29302
29408
  return;
@@ -29320,6 +29426,7 @@ async function restoreVaultMemoryOp(ctx, id) {
29320
29426
  if (record.isDeleted || !isOwnedByCtxUser(ctx, record)) return false;
29321
29427
  let stale = false;
29322
29428
  await ctx.database.write(async () => {
29429
+ await assertCanWrite2(ctx);
29323
29430
  if (record.isDeleted || !isOwnedByCtxUser(ctx, record)) {
29324
29431
  stale = true;
29325
29432
  return;
@@ -29340,6 +29447,7 @@ async function hardDeleteDecayedOp(ctx, id, opts) {
29340
29447
  const now = opts.now ?? Date.now();
29341
29448
  let stale = false;
29342
29449
  await ctx.database.write(async () => {
29450
+ await assertCanWrite2(ctx);
29343
29451
  const archivedAt = record.archivedAt;
29344
29452
  if (record.isDeleted || !isOwnedByCtxUser(ctx, record) || archivedAt === null || now - archivedAt <= opts.hardDeleteWindowMs) {
29345
29453
  stale = true;
@@ -29379,6 +29487,7 @@ async function updateVaultMemoryEmbeddingOp(ctx, id, embedding, embeddingModel,
29379
29487
  }
29380
29488
  let written = false;
29381
29489
  await ctx.database.write(async () => {
29490
+ await assertCanWrite2(ctx);
29382
29491
  if (record.isDeleted || !isOwnedByCtxUser(ctx, record)) return;
29383
29492
  const originalUpdatedAt = record.updatedAt.getTime();
29384
29493
  if (originalUpdatedAt !== readUpdatedAt || record._getRaw("content") !== readStoredContent)
@@ -33071,6 +33180,13 @@ function restoreCandidates(candidates, redactor, ownNames) {
33071
33180
  ).map((c) => c.candidate);
33072
33181
  }
33073
33182
  async function extractAndRetain(messages, retainCtx, options) {
33183
+ const { memoryStore, signal } = options;
33184
+ if (memoryStore === null) throw new Error("Memory store is not ready");
33185
+ if (memoryStore && options.folderId !== void 0 && options.folderId !== null)
33186
+ throw new Error("Remote memory extraction does not support folders");
33187
+ if (memoryStore && options.extract.piiRedaction && !memoryStore.masksEmbeddingInput)
33188
+ throw new Error("PII-redacted extraction requires a memory store that masks embedding inputs");
33189
+ signal?.throwIfAborted();
33074
33190
  const minConfidence = options.minConfidence ?? DEFAULT_MIN_CONFIDENCE;
33075
33191
  const resolvedConsolidatePii = options.consolidateOptions?.piiRedaction ?? options.extract.piiRedaction;
33076
33192
  const consolidateOptions = options.consolidateOptions !== void 0 ? {
@@ -33104,6 +33220,7 @@ async function extractAndRetain(messages, retainCtx, options) {
33104
33220
  }
33105
33221
  });
33106
33222
  const extractMs = Date.now() - tExtract;
33223
+ signal?.throwIfAborted();
33107
33224
  const filtered = candidates.filter((c) => c.confidence >= minConfidence);
33108
33225
  const log = getLogger();
33109
33226
  const screened = screenCandidatesForInjection(filtered);
@@ -33151,16 +33268,33 @@ async function extractAndRetain(messages, retainCtx, options) {
33151
33268
  const results = [];
33152
33269
  const quarantinedInfo = [];
33153
33270
  let failedWrites = 0;
33271
+ let auditRows;
33154
33272
  const tRetain = Date.now();
33155
33273
  for (const { candidate, isQuarantined, reason, signature } of toRetain) {
33156
33274
  try {
33275
+ signal?.throwIfAborted();
33157
33276
  if (isQuarantined) {
33158
- const existingId = await findQuarantinedDuplicateOp(
33159
- retainCtx.vaultCtx,
33160
- candidate.content,
33161
- candidate.sourceMessageIds,
33162
- { scope: options.scope ?? "private", folderId: options.folderId ?? null }
33163
- ).catch(() => null);
33277
+ let existingId;
33278
+ if (memoryStore) {
33279
+ auditRows ?? (auditRows = memoryStore.list({
33280
+ scopes: [options.scope ?? "private"],
33281
+ includeArchived: true,
33282
+ includeQuarantined: true,
33283
+ includeSuperseded: true
33284
+ }));
33285
+ const sourceKey = [...new Set(candidate.sourceMessageIds)].sort().join("\0");
33286
+ const normalize = (value) => value.normalize("NFKC").trim().replace(/\s+/g, " ").toLowerCase();
33287
+ existingId = (await auditRows).find(
33288
+ (row) => row.trustTier === "quarantined" && row.scope === (options.scope ?? "private") && [...new Set(row.sourceChunkIds ?? [])].sort().join("\0") === sourceKey && normalize(row.content) === normalize(candidate.content)
33289
+ )?.uniqueId ?? null;
33290
+ } else {
33291
+ existingId = await findQuarantinedDuplicateOp(
33292
+ retainCtx.vaultCtx,
33293
+ candidate.content,
33294
+ candidate.sourceMessageIds,
33295
+ { scope: options.scope ?? "private", folderId: options.folderId ?? null }
33296
+ ).catch(() => null);
33297
+ }
33164
33298
  if (existingId) {
33165
33299
  quarantinedInfo.push({
33166
33300
  candidate,
@@ -33171,18 +33305,26 @@ async function extractAndRetain(messages, retainCtx, options) {
33171
33305
  continue;
33172
33306
  }
33173
33307
  }
33174
- const result = await retain(candidate.content, retainCtx, {
33308
+ const retainOptions = {
33175
33309
  source: "auto-extracted",
33176
33310
  sourceChunkIds: candidate.sourceMessageIds,
33177
33311
  respectTombstones: true,
33178
33312
  ...options.scope !== void 0 && { scope: options.scope },
33179
- ...options.folderId !== void 0 && { folderId: options.folderId },
33180
33313
  ...consolidateOptions !== void 0 && { consolidateOptions },
33181
33314
  ...candidate.eventTime !== null && { eventTime: candidate.eventTime },
33182
33315
  factType: candidate.type,
33183
33316
  ...isQuarantined && { trustTier: "quarantined", enableAutoMerge: false }
33317
+ };
33318
+ const result = memoryStore ? await memoryStore.retain(candidate.content, {
33319
+ ...retainOptions,
33320
+ canWrite: retainCtx.vaultCtx.canWrite,
33321
+ signal
33322
+ }) : await retain(candidate.content, retainCtx, {
33323
+ ...retainOptions,
33324
+ ...options.folderId !== void 0 && { folderId: options.folderId }
33184
33325
  });
33185
33326
  if (isQuarantined) {
33327
+ auditRows = void 0;
33186
33328
  const info = {
33187
33329
  candidate,
33188
33330
  memoryId: result.memoryId,
@@ -33201,9 +33343,16 @@ async function extractAndRetain(messages, retainCtx, options) {
33201
33343
  }
33202
33344
  succeededCandidates.push(candidate);
33203
33345
  results.push(result);
33204
- if (result.action !== "suppressed" && options.entityCtx && candidate.entities.length > 0) {
33346
+ if (result.action !== "suppressed" && candidate.entities.length > 0) {
33205
33347
  try {
33206
- if (!await isMemoryTopicsUserManaged(options.entityCtx, result.memoryId)) {
33348
+ signal?.throwIfAborted();
33349
+ if (memoryStore) {
33350
+ await memoryStore.addTopics(result.memoryId, candidate.entities, {
33351
+ unlessUserManaged: true,
33352
+ canWrite: retainCtx.vaultCtx.canWrite,
33353
+ signal
33354
+ });
33355
+ } else if (options.entityCtx && !await isMemoryTopicsUserManaged(options.entityCtx, result.memoryId)) {
33207
33356
  await linkMemoryEntitiesOp(options.entityCtx, result.memoryId, candidate.entities, {
33208
33357
  unlessTopicsUserManaged: true
33209
33358
  });
@@ -33453,7 +33602,7 @@ function createAutoExtractor(options) {
33453
33602
  };
33454
33603
  const consolidateOptions = options.consolidate ? { ...options.consolidate, ...subPassAuth(options.consolidate.baseUrl) } : void 0;
33455
33604
  const injectionClassifierOptions = options.injectionClassifier ? { ...options.injectionClassifier, ...subPassAuth(options.injectionClassifier.baseUrl) } : void 0;
33456
- if (options.retainCtx.vaultCtx.entityCtx && !options.entityCtx) {
33605
+ if (!options.memoryStore && options.retainCtx.vaultCtx.entityCtx && !options.entityCtx) {
33457
33606
  getLogger().warn(
33458
33607
  "[memory/extract] retainCtx.vaultCtx.entityCtx is set but extractor was created without `entityCtx` \u2014 W5 graph lane will receive no writes"
33459
33608
  );
@@ -33498,6 +33647,8 @@ function createAutoExtractor(options) {
33498
33647
  try {
33499
33648
  const { candidates, results, failedCount, outcome, failure, funnel, timings, model } = await extractAndRetain(window2, retainCtx, {
33500
33649
  extract,
33650
+ memoryStore: options.memoryStore,
33651
+ signal: options.signal,
33501
33652
  ...options.minConfidence !== void 0 && { minConfidence: options.minConfidence },
33502
33653
  ...options.entityCtx !== void 0 && { entityCtx: options.entityCtx },
33503
33654
  ...options.scope !== void 0 && { scope: options.scope },
@@ -35625,6 +35776,7 @@ function createLocalMemoryStore(options) {
35625
35776
  database,
35626
35777
  entityCollection: database.get("entity"),
35627
35778
  memoryEntityCollection: database.get("memory_entity"),
35779
+ canWrite: options.canWrite,
35628
35780
  ...userId !== void 0 && { userId },
35629
35781
  ...options.allowUnscopedRows !== void 0 && {
35630
35782
  allowUnscopedRows: options.allowUnscopedRows
@@ -35636,10 +35788,21 @@ function createLocalMemoryStore(options) {
35636
35788
  walletAddress: options.walletAddress,
35637
35789
  signMessage: options.signMessage,
35638
35790
  embeddedWalletSigner: options.embeddedWalletSigner,
35791
+ canWrite: options.canWrite,
35639
35792
  ...userId !== void 0 && { userId },
35640
35793
  ...options.singleTenant !== void 0 && { singleTenant: options.singleTenant },
35641
35794
  entityCtx
35642
35795
  };
35796
+ const assertWrite = async (writeOptions) => {
35797
+ writeOptions?.signal?.throwIfAborted();
35798
+ if (writeOptions?.canWrite && !await writeOptions.canWrite()) {
35799
+ throw new Error("Memory source is no longer eligible");
35800
+ }
35801
+ if (options.canWrite && !await options.canWrite()) {
35802
+ throw new Error("Memory source is no longer eligible");
35803
+ }
35804
+ writeOptions?.signal?.throwIfAborted();
35805
+ };
35643
35806
  const vaultCache = options.vaultCache ?? createVaultEmbeddingCache();
35644
35807
  const ownedBy = userId !== void 0 ? [import_watermelondb24.Q.where("user_id", userId)] : [];
35645
35808
  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)];
@@ -35751,9 +35914,25 @@ function createLocalMemoryStore(options) {
35751
35914
  archive: (id) => archiveVaultMemoryOp(vaultCtx, id),
35752
35915
  restore: (id) => restoreVaultMemoryOp(vaultCtx, id),
35753
35916
  setTopics: (memoryId, topics) => setMemoryEntitiesOp(vaultCtx, memoryId, topics),
35754
- addTopics: (memoryId, topics) => database.write(async (writer) => {
35917
+ addTopics: (memoryId, topics, writeOptions) => database.write(async (writer) => {
35918
+ await assertWrite(writeOptions);
35755
35919
  const owned = await vaultCtx.vaultMemoryCollection.query(import_watermelondb24.Q.where("id", memoryId), import_watermelondb24.Q.where("is_deleted", false), ...ownedBy).fetchCount();
35756
- return owned > 0 ? writer.callWriter(() => linkMemoryEntitiesOp(entityCtx, memoryId, topics)) : [];
35920
+ return owned > 0 ? writer.callWriter(
35921
+ () => linkMemoryEntitiesOp(
35922
+ {
35923
+ ...entityCtx,
35924
+ canWrite: async () => {
35925
+ await assertWrite(writeOptions);
35926
+ return true;
35927
+ }
35928
+ },
35929
+ memoryId,
35930
+ topics,
35931
+ {
35932
+ unlessTopicsUserManaged: writeOptions?.unlessUserManaged
35933
+ }
35934
+ )
35935
+ ) : [];
35757
35936
  }),
35758
35937
  setVisibility: (id, visibility, visibilityOptions) => setMemoryVisibilityOp(vaultCtx, id, { visibility, ...visibilityOptions }),
35759
35938
  recall: (query, recallOptions) => recall(
@@ -35763,7 +35942,17 @@ function createLocalMemoryStore(options) {
35763
35942
  ),
35764
35943
  retain: (content, retainOptions) => retain(
35765
35944
  content,
35766
- { vaultCtx, embeddingOptions: options.embeddingOptions, vaultCache },
35945
+ {
35946
+ vaultCtx: {
35947
+ ...vaultCtx,
35948
+ canWrite: async () => {
35949
+ await assertWrite(retainOptions);
35950
+ return true;
35951
+ }
35952
+ },
35953
+ embeddingOptions: options.embeddingOptions,
35954
+ vaultCache
35955
+ },
35767
35956
  retainOptions
35768
35957
  ),
35769
35958
  factSource: {
@@ -36606,6 +36795,7 @@ function createDurableAutoExtractor(options) {
36606
36795
  };
36607
36796
  const folderId = options.folderId ?? null;
36608
36797
  let disposed = false;
36798
+ let activeBatch = null;
36609
36799
  let running = false;
36610
36800
  let wakeRequested = false;
36611
36801
  let timer;
@@ -36642,14 +36832,18 @@ function createDurableAutoExtractor(options) {
36642
36832
  }, delay);
36643
36833
  }
36644
36834
  async function extractBatch(messages, conversationId, ids, jobScope, jobFolderId) {
36835
+ if (disposed) throw new Error("Extraction session was disposed");
36645
36836
  let watchdog;
36646
36837
  let cancelled = false;
36838
+ const controller = new AbortController();
36839
+ activeBatch = controller;
36647
36840
  const model = options.modelForScope?.(jobScope) ?? options.extract.model;
36648
36841
  try {
36649
36842
  return await Promise.race([
36650
36843
  new Promise((resolve, reject) => {
36651
36844
  const worker = createAutoExtractor({
36652
36845
  ...options,
36846
+ signal: controller.signal,
36653
36847
  retainCtx: {
36654
36848
  ...options.retainCtx,
36655
36849
  vaultCtx: {
@@ -36678,6 +36872,8 @@ function createDurableAutoExtractor(options) {
36678
36872
  ]);
36679
36873
  } finally {
36680
36874
  cancelled = true;
36875
+ controller.abort();
36876
+ if (activeBatch === controller) activeBatch = null;
36681
36877
  if (watchdog !== void 0) clearTimeout(watchdog);
36682
36878
  }
36683
36879
  }
@@ -36689,6 +36885,7 @@ function createDurableAutoExtractor(options) {
36689
36885
  for (const message of loaded)
36690
36886
  if (message && message.messageId > 0) sequences.set(message.uniqueId, message.messageId);
36691
36887
  await database.write(async () => {
36888
+ if (disposed) return;
36692
36889
  const current = idsOf(job);
36693
36890
  const acknowledged = ids.filter((id) => current.includes(id));
36694
36891
  if (!acknowledged.length) return;
@@ -36861,6 +37058,7 @@ function createDurableAutoExtractor(options) {
36861
37058
  jobScope,
36862
37059
  jobFolderId
36863
37060
  );
37061
+ if (disposed) throw new Error("Extraction session was disposed");
36864
37062
  options.onTurnComplete?.(result);
36865
37063
  return result;
36866
37064
  };
@@ -36897,6 +37095,7 @@ function createDurableAutoExtractor(options) {
36897
37095
  );
36898
37096
  scheduleAfterSuccess = true;
36899
37097
  } catch (error) {
37098
+ if (disposed) break;
36900
37099
  attempts.set(job.id, attempted + 1);
36901
37100
  retry = true;
36902
37101
  reportError(error, conversationId);
@@ -36919,6 +37118,14 @@ function createDurableAutoExtractor(options) {
36919
37118
  schedule(retry ? options.retryDelayMs ?? 3e4 : 0);
36920
37119
  }
36921
37120
  }
37121
+ function dispose() {
37122
+ disposed = true;
37123
+ activeBatch?.abort(options.signal?.reason);
37124
+ if (timer !== void 0) clearTimeout(timer);
37125
+ options.signal?.removeEventListener("abort", dispose);
37126
+ }
37127
+ if (options.signal?.aborted) dispose();
37128
+ else options.signal?.addEventListener("abort", dispose, { once: true });
36922
37129
  schedule(0);
36923
37130
  return {
36924
37131
  processTurn(messages, conversationId) {
@@ -36998,10 +37205,7 @@ function createDurableAutoExtractor(options) {
36998
37205
  return true;
36999
37206
  },
37000
37207
  isProcessing: () => running,
37001
- dispose() {
37002
- disposed = true;
37003
- if (timer !== void 0) clearTimeout(timer);
37004
- }
37208
+ dispose
37005
37209
  };
37006
37210
  }
37007
37211