@anuma/sdk 7.0.3-next.20261014212810 → 7.0.4

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.
@@ -2158,8 +2158,9 @@ async function updateConversationPinnedOp(ctx, id, pinned) {
2158
2158
  }
2159
2159
  return false;
2160
2160
  }
2161
- async function getMessagesOp(ctx, convId) {
2162
- const results = await ctx.messagesCollection.query(Q.where("conversation_id", convId), Q.sortBy("message_id", Q.asc)).unsafeFetchRaw();
2161
+ async function getMessagesOp(ctx, convId, opts) {
2162
+ const fetched = await ctx.messagesCollection.query(Q.where("conversation_id", convId), Q.sortBy("message_id", Q.asc)).unsafeFetchRaw();
2163
+ const results = opts?.rawFilter ? fetched.filter(opts.rawFilter) : fetched;
2163
2164
  return Promise.all(
2164
2165
  results.map(
2165
2166
  (raw) => messageRawToStored(raw, ctx.walletAddress, ctx.signMessage, ctx.embeddedWalletSigner)
@@ -3915,22 +3916,6 @@ var postApiV1Embeddings = (options) => (options.client ?? client).post({
3915
3916
  });
3916
3917
  var getApiV1Models = (options) => (options?.client ?? client).get({ url: "/api/v1/models", ...options });
3917
3918
 
3918
- // src/lib/memoryEngine/embeddingCacheState.ts
3919
- var EMBEDDING_CACHE_STATE_KEY = /* @__PURE__ */ Symbol.for("@anuma/sdk/embedding-cache/v1");
3920
- var cacheGlobal = globalThis;
3921
- var fallbackState = {
3922
- scopedViews: /* @__PURE__ */ new WeakMap(),
3923
- inFlightByCache: /* @__PURE__ */ new WeakMap(),
3924
- cachedEmbeddingModels: /* @__PURE__ */ new WeakMap()
3925
- };
3926
- function getEmbeddingCacheState() {
3927
- try {
3928
- return cacheGlobal[EMBEDDING_CACHE_STATE_KEY] ?? (cacheGlobal[EMBEDDING_CACHE_STATE_KEY] = fallbackState);
3929
- } catch {
3930
- return fallbackState;
3931
- }
3932
- }
3933
-
3934
3919
  // src/lib/memoryEngine/generate.ts
3935
3920
  var EMBED_MAX_ATTEMPTS = 4;
3936
3921
  var DEFAULT_EMBEDDING_REQUEST_TIMEOUT_MS = 15e3;
@@ -4022,18 +4007,15 @@ async function generateEmbedding(text14, options) {
4022
4007
  if (total === void 0) return embedOne(text14, options, void 0);
4023
4008
  return withDeadline((signal) => embedOne(text14, options, signal), total, "embedding");
4024
4009
  }
4025
- var { inFlightByCache, cachedEmbeddingModels } = getEmbeddingCacheState();
4010
+ var inFlightByCache = /* @__PURE__ */ new WeakMap();
4026
4011
  async function embedOne(text14, options, outer) {
4027
4012
  const { cache } = options;
4028
4013
  if (!cache) return Array.from(await requestEmbedding(text14, options, outer));
4029
- const model = options.model ?? DEFAULT_API_EMBEDDING_MODEL;
4030
4014
  const cached = cache.get(text14);
4031
- const cachedModel = cached && cachedEmbeddingModels.get(cached);
4032
- if (cached && (cachedModel === void 0 || cachedModel === model)) return Array.from(cached);
4015
+ if (cached) return Array.from(cached);
4033
4016
  const inFlight = inFlightByCache.get(cache) ?? /* @__PURE__ */ new Map();
4034
4017
  inFlightByCache.set(cache, inFlight);
4035
- const requestKey = JSON.stringify([model, text14]);
4036
- const pending = inFlight.get(requestKey);
4018
+ const pending = inFlight.get(text14);
4037
4019
  if (pending) {
4038
4020
  try {
4039
4021
  return Array.from(await pending);
@@ -4042,14 +4024,13 @@ async function embedOne(text14, options, outer) {
4042
4024
  }
4043
4025
  }
4044
4026
  const request = requestEmbedding(text14, options, outer);
4045
- inFlight.set(requestKey, request);
4027
+ inFlight.set(text14, request);
4046
4028
  try {
4047
4029
  const embedding2 = await request;
4048
- cachedEmbeddingModels.set(embedding2, model);
4049
4030
  cache.set(text14, embedding2);
4050
4031
  return Array.from(embedding2);
4051
4032
  } finally {
4052
- if (inFlight.get(requestKey) === request) inFlight.delete(requestKey);
4033
+ if (inFlight.get(text14) === request) inFlight.delete(text14);
4053
4034
  }
4054
4035
  }
4055
4036
  async function requestEmbedding(text14, options, outer) {
@@ -4132,15 +4113,13 @@ async function embedMany(texts, options, outer) {
4132
4113
  const { baseUrl = BASE_URL, model, batchSize, cache } = options;
4133
4114
  const timeoutMs = options.timeoutMs ?? DEFAULT_EMBEDDING_REQUEST_TIMEOUT_MS;
4134
4115
  const chunkSize = batchSize ?? DEFAULT_EMBEDDING_BATCH_SIZE;
4135
- const embeddingModel = model ?? DEFAULT_API_EMBEDDING_MODEL;
4136
4116
  const results = new Array(texts.length).fill(null);
4137
4117
  const uncachedIndices = [];
4138
4118
  const uncachedTexts = [];
4139
4119
  for (let i = 0; i < texts.length; i++) {
4140
4120
  if (cache) {
4141
4121
  const cached = cache.get(texts[i]);
4142
- const cachedModel = cached && cachedEmbeddingModels.get(cached);
4143
- if (cached && (cachedModel === void 0 || cachedModel === embeddingModel)) {
4122
+ if (cached) {
4144
4123
  results[i] = Array.from(cached);
4145
4124
  continue;
4146
4125
  }
@@ -4156,6 +4135,7 @@ async function embedMany(texts, options, outer) {
4156
4135
  ...taskTypeHeader(options.taskType)
4157
4136
  };
4158
4137
  if (outer?.aborted) throw outer.reason;
4138
+ const embeddingModel = model ?? DEFAULT_API_EMBEDDING_MODEL;
4159
4139
  let newEmbeddings;
4160
4140
  if (uncachedTexts.length <= chunkSize) {
4161
4141
  newEmbeddings = await generateEmbeddingsBatch(
@@ -4203,7 +4183,6 @@ async function embedMany(texts, options, outer) {
4203
4183
  const f32Embedding = Float32Array.from(newEmbeddings[i]);
4204
4184
  results[uncachedIndices[i]] = Array.from(f32Embedding);
4205
4185
  if (cache) {
4206
- cachedEmbeddingModels.set(f32Embedding, embeddingModel);
4207
4186
  cache.set(uncachedTexts[i], f32Embedding);
4208
4187
  }
4209
4188
  }
@@ -4328,6 +4307,37 @@ async function chunkAndEmbedMessage(ctx, messageId, options) {
4328
4307
  }));
4329
4308
  return updateMessageChunksOp(ctx, messageId, messageChunks, embeddingModel);
4330
4309
  }
4310
+ var MIN_SEALED_EMBEDDING_HEX = 256;
4311
+ function rawHasEmbedding(value) {
4312
+ if (Array.isArray(value)) return value.length > 0;
4313
+ if (typeof value !== "string" || value.length === 0) return false;
4314
+ if (isEncrypted(value)) {
4315
+ return value.length - value.indexOf(":", 4) - 1 >= MIN_SEALED_EMBEDDING_HEX;
4316
+ }
4317
+ try {
4318
+ const parsed = JSON.parse(value);
4319
+ return Array.isArray(parsed) && parsed.length > 0;
4320
+ } catch {
4321
+ return false;
4322
+ }
4323
+ }
4324
+ function sealedVersion(value) {
4325
+ if (typeof value !== "string" || !isEncrypted(value)) return null;
4326
+ return value.startsWith("enc:v2:") ? "v2" : "v3";
4327
+ }
4328
+ function mayNeedChunkEmbedding(raw, embeddingModel, filter) {
4329
+ const role = raw.role;
4330
+ if (role === "system") return false;
4331
+ if (filter?.roles && !filter.roles.includes(role ?? "")) return false;
4332
+ if (isNonEmbeddableOrigin(raw.origin, filter?.reembedDiscarded)) {
4333
+ return false;
4334
+ }
4335
+ const model = raw.embedding_model;
4336
+ if (typeof model === "string" && model !== embeddingModel) return true;
4337
+ if (rawHasEmbedding(raw.chunks)) return false;
4338
+ if (rawHasEmbedding(raw.vector) && !filter?.rechunkExisting) return false;
4339
+ return true;
4340
+ }
4331
4341
  async function chunkAndEmbedAllMessages(ctx, options, filter) {
4332
4342
  const embeddingModel = options.model ?? DEFAULT_API_EMBEDDING_MODEL;
4333
4343
  const { chunkSize = DEFAULT_CHUNK_SIZE } = options;
@@ -4340,11 +4350,31 @@ async function chunkAndEmbedAllMessages(ctx, options, filter) {
4340
4350
  let considered = 0;
4341
4351
  let sealedRowsSeen = 0;
4342
4352
  let rowsSeen = 0;
4353
+ const keptSealed = /* @__PURE__ */ new Map();
4354
+ const candidatesStillSealed = /* @__PURE__ */ new Map();
4355
+ const droppedSealed = /* @__PURE__ */ new Map();
4356
+ const rawFilter = (raw) => {
4357
+ rowsSeen++;
4358
+ const keep = mayNeedChunkEmbedding(raw, embeddingModel, filter);
4359
+ const version = sealedVersion(raw.content);
4360
+ if (!version) return keep;
4361
+ if (keep) {
4362
+ keptSealed.set(version, (keptSealed.get(version) ?? 0) + 1);
4363
+ } else {
4364
+ const dropped = droppedSealed.get(version);
4365
+ if (dropped) dropped.count++;
4366
+ else droppedSealed.set(version, { count: 1, probeId: raw.id });
4367
+ }
4368
+ return keep;
4369
+ };
4343
4370
  for (const conv of targetConversations) {
4344
- const messages = await getMessagesOp(ctx, conv.conversationId);
4371
+ const messages = await getMessagesOp(ctx, conv.conversationId, { rawFilter });
4345
4372
  for (const message of messages) {
4346
- rowsSeen++;
4347
- if (isEncrypted(message.content)) sealedRowsSeen++;
4373
+ const stillSealed = sealedVersion(message.content);
4374
+ if (stillSealed) {
4375
+ sealedRowsSeen++;
4376
+ candidatesStillSealed.set(stillSealed, (candidatesStillSealed.get(stillSealed) ?? 0) + 1);
4377
+ }
4348
4378
  const isStale = message.embeddingModel !== void 0 && message.embeddingModel !== null && message.embeddingModel !== embeddingModel;
4349
4379
  if (message.chunks && message.chunks.length > 0 && !isStale) continue;
4350
4380
  const hasVector = message.vector && message.vector.length > 0;
@@ -4357,6 +4387,7 @@ async function chunkAndEmbedAllMessages(ctx, options, filter) {
4357
4387
  stillEncrypted++;
4358
4388
  continue;
4359
4389
  }
4390
+ if (isToolResultsRow(message)) continue;
4360
4391
  if (message.content.length < minLength) continue;
4361
4392
  if (shouldChunkMessage(message.content, chunkSize)) {
4362
4393
  longMessages.push({
@@ -4368,6 +4399,12 @@ async function chunkAndEmbedAllMessages(ctx, options, filter) {
4368
4399
  }
4369
4400
  }
4370
4401
  }
4402
+ for (const [version, dropped] of droppedSealed) {
4403
+ const keyOpenedACandidate = (keptSealed.get(version) ?? 0) > (candidatesStillSealed.get(version) ?? 0);
4404
+ if (keyOpenedACandidate) continue;
4405
+ const probe = await getMessageOp(ctx, dropped.probeId);
4406
+ if (!probe || isEncrypted(probe.content)) sealedRowsSeen += dropped.count;
4407
+ }
4371
4408
  if (stillEncrypted > 0 || sealedRowsSeen > 0) {
4372
4409
  getLogger().error(
4373
4410
  "memoryEngine: messages still encrypted (key unavailable?) \u2014 excluded from embedding",
@@ -9573,7 +9610,7 @@ var MaskScopedEmbeddingCache = class extends Map {
9573
9610
  return this;
9574
9611
  }
9575
9612
  };
9576
- var { scopedViews } = getEmbeddingCacheState();
9613
+ var scopedViews = /* @__PURE__ */ new WeakMap();
9577
9614
  function maskScopedEmbeddingCache(cache, masked) {
9578
9615
  const views = scopedViews.get(cache) ?? {};
9579
9616
  scopedViews.set(cache, views);
@@ -17737,48 +17774,44 @@ async function recall(query, ctx, options = {}) {
17737
17774
  embeddingOptions,
17738
17775
  ctx.vaultCache,
17739
17776
  searchOptions
17740
- )))(
17741
- query,
17742
- {
17743
- limit: types.includes("chunk") ? Math.max(limit * 2, 16) : limit,
17744
- minSimilarity: vaultMinScore,
17745
- useFusion: true,
17746
- rerank: flags.rerank,
17747
- ...options.rerankTopN !== void 0 && { rerankTopN: options.rerankTopN },
17748
- ...options.ceWeight !== void 0 && { ceWeight: options.ceWeight },
17749
- ...options.rerankLoadTimeoutMs !== void 0 && {
17750
- rerankLoadTimeoutMs: options.rerankLoadTimeoutMs
17751
- },
17752
- ...options.recencyAlpha !== void 0 && { recencyAlpha: options.recencyAlpha },
17753
- ...options.recency && { recency: options.recency },
17754
- ...options.mmr !== void 0 && { mmr: options.mmr },
17755
- ...options.supersessionBoost !== void 0 && {
17756
- supersessionBoost: options.supersessionBoost
17757
- },
17758
- ...options.supersessionWindow !== void 0 && {
17759
- supersessionWindow: options.supersessionWindow
17760
- },
17761
- ...options.proofCountAlpha !== void 0 && {
17762
- proofCountAlpha: options.proofCountAlpha
17763
- },
17764
- ...options.bm25AdmissionDivisor !== void 0 && {
17765
- bm25AdmissionDivisor: options.bm25AdmissionDivisor
17766
- },
17767
- ...options.rrfK !== void 0 && { rrfK: options.rrfK },
17768
- ...options.decryptLast !== void 0 && { decryptLast: options.decryptLast },
17769
- ...subQueries && { subQueries },
17770
- ...options.scopes && { scopes: options.scopes },
17771
- ...options.folderId !== void 0 && { folderId: options.folderId },
17772
- ...options.factTypes?.length && { factTypes: options.factTypes },
17773
- ...options.memoryIds !== void 0 && { memoryIds: options.memoryIds },
17774
- ...options.factTypeWeights && { factTypeWeights: options.factTypeWeights },
17775
- ...entityRanking.length > 0 && { entityRanking },
17776
- ...temporalRanking.length > 0 && { temporalRanking },
17777
- ...needsChunkEmbedding && { queryEmbedding: queryEmbedding ?? [] },
17778
- queryEmbedTotalTimeoutMs
17777
+ )))(query, {
17778
+ limit: types.includes("chunk") ? Math.max(limit * 2, 16) : limit,
17779
+ minSimilarity: vaultMinScore,
17780
+ useFusion: true,
17781
+ rerank: flags.rerank,
17782
+ ...options.rerankTopN !== void 0 && { rerankTopN: options.rerankTopN },
17783
+ ...options.ceWeight !== void 0 && { ceWeight: options.ceWeight },
17784
+ ...options.rerankLoadTimeoutMs !== void 0 && {
17785
+ rerankLoadTimeoutMs: options.rerankLoadTimeoutMs
17779
17786
  },
17780
- options.embeddingCache
17781
- );
17787
+ ...options.recencyAlpha !== void 0 && { recencyAlpha: options.recencyAlpha },
17788
+ ...options.recency && { recency: options.recency },
17789
+ ...options.mmr !== void 0 && { mmr: options.mmr },
17790
+ ...options.supersessionBoost !== void 0 && {
17791
+ supersessionBoost: options.supersessionBoost
17792
+ },
17793
+ ...options.supersessionWindow !== void 0 && {
17794
+ supersessionWindow: options.supersessionWindow
17795
+ },
17796
+ ...options.proofCountAlpha !== void 0 && {
17797
+ proofCountAlpha: options.proofCountAlpha
17798
+ },
17799
+ ...options.bm25AdmissionDivisor !== void 0 && {
17800
+ bm25AdmissionDivisor: options.bm25AdmissionDivisor
17801
+ },
17802
+ ...options.rrfK !== void 0 && { rrfK: options.rrfK },
17803
+ ...options.decryptLast !== void 0 && { decryptLast: options.decryptLast },
17804
+ ...subQueries && { subQueries },
17805
+ ...options.scopes && { scopes: options.scopes },
17806
+ ...options.folderId !== void 0 && { folderId: options.folderId },
17807
+ ...options.factTypes?.length && { factTypes: options.factTypes },
17808
+ ...options.memoryIds !== void 0 && { memoryIds: options.memoryIds },
17809
+ ...options.factTypeWeights && { factTypeWeights: options.factTypeWeights },
17810
+ ...entityRanking.length > 0 && { entityRanking },
17811
+ ...temporalRanking.length > 0 && { temporalRanking },
17812
+ ...needsChunkEmbedding && { queryEmbedding: queryEmbedding ?? [] },
17813
+ queryEmbedTotalTimeoutMs
17814
+ });
17782
17815
  factResults.push(
17783
17816
  ...dedupeBy(
17784
17817
  results,
@@ -18917,19 +18950,10 @@ function createLocalMemoryStore(options) {
18917
18950
  retainOptions
18918
18951
  ),
18919
18952
  factSource: {
18920
- search: (query, searchOptions, embeddingCache) => searchVaultMemoriesWithSize(
18953
+ search: (query, searchOptions) => searchVaultMemoriesWithSize(
18921
18954
  query,
18922
18955
  vaultCtx,
18923
- {
18924
- ...options.embeddingOptions,
18925
- taskType: "memory_recall_embed",
18926
- ...embeddingCache && !options.embeddingOptions.cache && {
18927
- cache: maskScopedEmbeddingCache(
18928
- embeddingCache,
18929
- Boolean(options.embeddingOptions.maskInput)
18930
- )
18931
- }
18932
- },
18956
+ { ...options.embeddingOptions, taskType: "memory_recall_embed" },
18933
18957
  vaultCache,
18934
18958
  searchOptions
18935
18959
  ),
@@ -19559,7 +19583,7 @@ function stored(row) {
19559
19583
  function createRemoteMemoryPipeline(options) {
19560
19584
  const { persistence, embeddingOptions } = options;
19561
19585
  const model = embeddingOptions.model ?? DEFAULT_API_EMBEDDING_MODEL;
19562
- const operation = (mode, queryOptions = embeddingOptions) => {
19586
+ const operation = (mode) => {
19563
19587
  const rejectUndecryptable = (failed) => {
19564
19588
  if (!failed.length) return;
19565
19589
  if (mode === "retain")
@@ -19590,7 +19614,7 @@ function createRemoteMemoryPipeline(options) {
19590
19614
  const start = Date.now();
19591
19615
  const budgetMs = search.queryEmbedTotalTimeoutMs ?? 8e3;
19592
19616
  const embed = (texts) => generateEmbeddings(texts, {
19593
- ...queryOptions,
19617
+ ...embeddingOptions,
19594
19618
  taskType: mode === "retain" ? "memory_retain_embed" : "memory_recall_embed",
19595
19619
  totalTimeoutMs: budgetMs
19596
19620
  });
@@ -19610,11 +19634,7 @@ function createRemoteMemoryPipeline(options) {
19610
19634
  }
19611
19635
  };
19612
19636
  const [primary, facetEmbeddings] = await Promise.all([
19613
- search.queryEmbedding ?? (mode === "recall" && queryOptions.cache ? generateEmbedding(query, {
19614
- ...queryOptions,
19615
- taskType: "memory_recall_embed",
19616
- totalTimeoutMs: budgetMs
19617
- }) : embed([query]).then((vectors2) => vectors2[0])),
19637
+ search.queryEmbedding ?? embed([query]).then((vectors2) => vectors2[0]),
19618
19638
  embedFacets()
19619
19639
  ]);
19620
19640
  const queryEmbedding = storedVector(primary);
@@ -19838,17 +19858,15 @@ function createRemoteMemoryPipeline(options) {
19838
19858
  const factSource = {
19839
19859
  graphRanking: options.graphRanking,
19840
19860
  temporalRanking: options.temporalRanking,
19841
- search: async (text14, searchOptions, embeddingCache) => {
19842
- const queryOptions = {
19843
- ...embeddingOptions,
19844
- taskType: "memory_recall_embed",
19845
- ...embeddingCache && !embeddingOptions.cache && {
19846
- cache: maskScopedEmbeddingCache(embeddingCache, Boolean(embeddingOptions.maskInput))
19847
- }
19848
- };
19849
- const { prepare } = operation("recall", queryOptions);
19861
+ search: async (text14, searchOptions) => {
19862
+ const { prepare } = operation("recall");
19850
19863
  const prepared = await prepare(text14, searchOptions);
19851
- const ranked = await rankPreparedVaultCandidates(text14, prepared, queryOptions, searchOptions);
19864
+ const ranked = await rankPreparedVaultCandidates(
19865
+ text14,
19866
+ prepared,
19867
+ { ...embeddingOptions, taskType: "memory_recall_embed" },
19868
+ searchOptions
19869
+ );
19852
19870
  return {
19853
19871
  ...ranked,
19854
19872
  rankedOnCosine: prepared.memories.length > 0 && !prepared.embeddingsUnavailable,
package/dist/index.cjs CHANGED
@@ -2089,22 +2089,6 @@ function taskTypeHeader(taskType) {
2089
2089
  return taskType ? { [TASK_TYPE_HEADER]: taskType } : {};
2090
2090
  }
2091
2091
 
2092
- // src/lib/memoryEngine/embeddingCacheState.ts
2093
- var EMBEDDING_CACHE_STATE_KEY = /* @__PURE__ */ Symbol.for("@anuma/sdk/embedding-cache/v1");
2094
- var cacheGlobal = globalThis;
2095
- var fallbackState = {
2096
- scopedViews: /* @__PURE__ */ new WeakMap(),
2097
- inFlightByCache: /* @__PURE__ */ new WeakMap(),
2098
- cachedEmbeddingModels: /* @__PURE__ */ new WeakMap()
2099
- };
2100
- function getEmbeddingCacheState() {
2101
- try {
2102
- return cacheGlobal[EMBEDDING_CACHE_STATE_KEY] ?? (cacheGlobal[EMBEDDING_CACHE_STATE_KEY] = fallbackState);
2103
- } catch {
2104
- return fallbackState;
2105
- }
2106
- }
2107
-
2108
2092
  // src/lib/memoryEngine/generate.ts
2109
2093
  var EMBED_MAX_ATTEMPTS = 4;
2110
2094
  var DEFAULT_EMBEDDING_REQUEST_TIMEOUT_MS = 15e3;
@@ -2193,18 +2177,15 @@ async function generateEmbedding(text2, options) {
2193
2177
  if (total === void 0) return embedOne(text2, options, void 0);
2194
2178
  return withDeadline((signal) => embedOne(text2, options, signal), total, "embedding");
2195
2179
  }
2196
- var { inFlightByCache, cachedEmbeddingModels } = getEmbeddingCacheState();
2180
+ var inFlightByCache = /* @__PURE__ */ new WeakMap();
2197
2181
  async function embedOne(text2, options, outer) {
2198
2182
  const { cache } = options;
2199
2183
  if (!cache) return Array.from(await requestEmbedding(text2, options, outer));
2200
- const model = options.model ?? DEFAULT_API_EMBEDDING_MODEL;
2201
2184
  const cached = cache.get(text2);
2202
- const cachedModel = cached && cachedEmbeddingModels.get(cached);
2203
- if (cached && (cachedModel === void 0 || cachedModel === model)) return Array.from(cached);
2185
+ if (cached) return Array.from(cached);
2204
2186
  const inFlight = inFlightByCache.get(cache) ?? /* @__PURE__ */ new Map();
2205
2187
  inFlightByCache.set(cache, inFlight);
2206
- const requestKey = JSON.stringify([model, text2]);
2207
- const pending = inFlight.get(requestKey);
2188
+ const pending = inFlight.get(text2);
2208
2189
  if (pending) {
2209
2190
  try {
2210
2191
  return Array.from(await pending);
@@ -2213,14 +2194,13 @@ async function embedOne(text2, options, outer) {
2213
2194
  }
2214
2195
  }
2215
2196
  const request = requestEmbedding(text2, options, outer);
2216
- inFlight.set(requestKey, request);
2197
+ inFlight.set(text2, request);
2217
2198
  try {
2218
2199
  const embedding = await request;
2219
- cachedEmbeddingModels.set(embedding, model);
2220
2200
  cache.set(text2, embedding);
2221
2201
  return Array.from(embedding);
2222
2202
  } finally {
2223
- if (inFlight.get(requestKey) === request) inFlight.delete(requestKey);
2203
+ if (inFlight.get(text2) === request) inFlight.delete(text2);
2224
2204
  }
2225
2205
  }
2226
2206
  async function requestEmbedding(text2, options, outer) {
@@ -2303,15 +2283,13 @@ async function embedMany(texts, options, outer) {
2303
2283
  const { baseUrl = BASE_URL, model, batchSize, cache } = options;
2304
2284
  const timeoutMs = options.timeoutMs ?? DEFAULT_EMBEDDING_REQUEST_TIMEOUT_MS;
2305
2285
  const chunkSize = batchSize ?? DEFAULT_EMBEDDING_BATCH_SIZE;
2306
- const embeddingModel = model ?? DEFAULT_API_EMBEDDING_MODEL;
2307
2286
  const results = new Array(texts.length).fill(null);
2308
2287
  const uncachedIndices = [];
2309
2288
  const uncachedTexts = [];
2310
2289
  for (let i = 0; i < texts.length; i++) {
2311
2290
  if (cache) {
2312
2291
  const cached = cache.get(texts[i]);
2313
- const cachedModel = cached && cachedEmbeddingModels.get(cached);
2314
- if (cached && (cachedModel === void 0 || cachedModel === embeddingModel)) {
2292
+ if (cached) {
2315
2293
  results[i] = Array.from(cached);
2316
2294
  continue;
2317
2295
  }
@@ -2327,6 +2305,7 @@ async function embedMany(texts, options, outer) {
2327
2305
  ...taskTypeHeader(options.taskType)
2328
2306
  };
2329
2307
  if (outer?.aborted) throw outer.reason;
2308
+ const embeddingModel = model ?? DEFAULT_API_EMBEDDING_MODEL;
2330
2309
  let newEmbeddings;
2331
2310
  if (uncachedTexts.length <= chunkSize) {
2332
2311
  newEmbeddings = await generateEmbeddingsBatch(
@@ -2374,7 +2353,6 @@ async function embedMany(texts, options, outer) {
2374
2353
  const f32Embedding = Float32Array.from(newEmbeddings[i]);
2375
2354
  results[uncachedIndices[i]] = Array.from(f32Embedding);
2376
2355
  if (cache) {
2377
- cachedEmbeddingModels.set(f32Embedding, embeddingModel);
2378
2356
  cache.set(uncachedTexts[i], f32Embedding);
2379
2357
  }
2380
2358
  }
package/dist/index.mjs CHANGED
@@ -1885,22 +1885,6 @@ function taskTypeHeader(taskType) {
1885
1885
  return taskType ? { [TASK_TYPE_HEADER]: taskType } : {};
1886
1886
  }
1887
1887
 
1888
- // src/lib/memoryEngine/embeddingCacheState.ts
1889
- var EMBEDDING_CACHE_STATE_KEY = /* @__PURE__ */ Symbol.for("@anuma/sdk/embedding-cache/v1");
1890
- var cacheGlobal = globalThis;
1891
- var fallbackState = {
1892
- scopedViews: /* @__PURE__ */ new WeakMap(),
1893
- inFlightByCache: /* @__PURE__ */ new WeakMap(),
1894
- cachedEmbeddingModels: /* @__PURE__ */ new WeakMap()
1895
- };
1896
- function getEmbeddingCacheState() {
1897
- try {
1898
- return cacheGlobal[EMBEDDING_CACHE_STATE_KEY] ?? (cacheGlobal[EMBEDDING_CACHE_STATE_KEY] = fallbackState);
1899
- } catch {
1900
- return fallbackState;
1901
- }
1902
- }
1903
-
1904
1888
  // src/lib/memoryEngine/generate.ts
1905
1889
  var EMBED_MAX_ATTEMPTS = 4;
1906
1890
  var DEFAULT_EMBEDDING_REQUEST_TIMEOUT_MS = 15e3;
@@ -1989,18 +1973,15 @@ async function generateEmbedding(text2, options) {
1989
1973
  if (total === void 0) return embedOne(text2, options, void 0);
1990
1974
  return withDeadline((signal) => embedOne(text2, options, signal), total, "embedding");
1991
1975
  }
1992
- var { inFlightByCache, cachedEmbeddingModels } = getEmbeddingCacheState();
1976
+ var inFlightByCache = /* @__PURE__ */ new WeakMap();
1993
1977
  async function embedOne(text2, options, outer) {
1994
1978
  const { cache } = options;
1995
1979
  if (!cache) return Array.from(await requestEmbedding(text2, options, outer));
1996
- const model = options.model ?? DEFAULT_API_EMBEDDING_MODEL;
1997
1980
  const cached = cache.get(text2);
1998
- const cachedModel = cached && cachedEmbeddingModels.get(cached);
1999
- if (cached && (cachedModel === void 0 || cachedModel === model)) return Array.from(cached);
1981
+ if (cached) return Array.from(cached);
2000
1982
  const inFlight = inFlightByCache.get(cache) ?? /* @__PURE__ */ new Map();
2001
1983
  inFlightByCache.set(cache, inFlight);
2002
- const requestKey = JSON.stringify([model, text2]);
2003
- const pending = inFlight.get(requestKey);
1984
+ const pending = inFlight.get(text2);
2004
1985
  if (pending) {
2005
1986
  try {
2006
1987
  return Array.from(await pending);
@@ -2009,14 +1990,13 @@ async function embedOne(text2, options, outer) {
2009
1990
  }
2010
1991
  }
2011
1992
  const request = requestEmbedding(text2, options, outer);
2012
- inFlight.set(requestKey, request);
1993
+ inFlight.set(text2, request);
2013
1994
  try {
2014
1995
  const embedding = await request;
2015
- cachedEmbeddingModels.set(embedding, model);
2016
1996
  cache.set(text2, embedding);
2017
1997
  return Array.from(embedding);
2018
1998
  } finally {
2019
- if (inFlight.get(requestKey) === request) inFlight.delete(requestKey);
1999
+ if (inFlight.get(text2) === request) inFlight.delete(text2);
2020
2000
  }
2021
2001
  }
2022
2002
  async function requestEmbedding(text2, options, outer) {
@@ -2099,15 +2079,13 @@ async function embedMany(texts, options, outer) {
2099
2079
  const { baseUrl = BASE_URL, model, batchSize, cache } = options;
2100
2080
  const timeoutMs = options.timeoutMs ?? DEFAULT_EMBEDDING_REQUEST_TIMEOUT_MS;
2101
2081
  const chunkSize = batchSize ?? DEFAULT_EMBEDDING_BATCH_SIZE;
2102
- const embeddingModel = model ?? DEFAULT_API_EMBEDDING_MODEL;
2103
2082
  const results = new Array(texts.length).fill(null);
2104
2083
  const uncachedIndices = [];
2105
2084
  const uncachedTexts = [];
2106
2085
  for (let i = 0; i < texts.length; i++) {
2107
2086
  if (cache) {
2108
2087
  const cached = cache.get(texts[i]);
2109
- const cachedModel = cached && cachedEmbeddingModels.get(cached);
2110
- if (cached && (cachedModel === void 0 || cachedModel === embeddingModel)) {
2088
+ if (cached) {
2111
2089
  results[i] = Array.from(cached);
2112
2090
  continue;
2113
2091
  }
@@ -2123,6 +2101,7 @@ async function embedMany(texts, options, outer) {
2123
2101
  ...taskTypeHeader(options.taskType)
2124
2102
  };
2125
2103
  if (outer?.aborted) throw outer.reason;
2104
+ const embeddingModel = model ?? DEFAULT_API_EMBEDDING_MODEL;
2126
2105
  let newEmbeddings;
2127
2106
  if (uncachedTexts.length <= chunkSize) {
2128
2107
  newEmbeddings = await generateEmbeddingsBatch(
@@ -2170,7 +2149,6 @@ async function embedMany(texts, options, outer) {
2170
2149
  const f32Embedding = Float32Array.from(newEmbeddings[i]);
2171
2150
  results[uncachedIndices[i]] = Array.from(f32Embedding);
2172
2151
  if (cache) {
2173
- cachedEmbeddingModels.set(f32Embedding, embeddingModel);
2174
2152
  cache.set(uncachedTexts[i], f32Embedding);
2175
2153
  }
2176
2154
  }