@anuma/sdk 7.0.3 → 7.0.4-next.20261015012621

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.
package/dist/index.cjs CHANGED
@@ -2089,6 +2089,22 @@ 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
+
2092
2108
  // src/lib/memoryEngine/generate.ts
2093
2109
  var EMBED_MAX_ATTEMPTS = 4;
2094
2110
  var DEFAULT_EMBEDDING_REQUEST_TIMEOUT_MS = 15e3;
@@ -2177,15 +2193,18 @@ async function generateEmbedding(text2, options) {
2177
2193
  if (total === void 0) return embedOne(text2, options, void 0);
2178
2194
  return withDeadline((signal) => embedOne(text2, options, signal), total, "embedding");
2179
2195
  }
2180
- var inFlightByCache = /* @__PURE__ */ new WeakMap();
2196
+ var { inFlightByCache, cachedEmbeddingModels } = getEmbeddingCacheState();
2181
2197
  async function embedOne(text2, options, outer) {
2182
2198
  const { cache } = options;
2183
2199
  if (!cache) return Array.from(await requestEmbedding(text2, options, outer));
2200
+ const model = options.model ?? DEFAULT_API_EMBEDDING_MODEL;
2184
2201
  const cached = cache.get(text2);
2185
- if (cached) return Array.from(cached);
2202
+ const cachedModel = cached && cachedEmbeddingModels.get(cached);
2203
+ if (cached && (cachedModel === void 0 || cachedModel === model)) return Array.from(cached);
2186
2204
  const inFlight = inFlightByCache.get(cache) ?? /* @__PURE__ */ new Map();
2187
2205
  inFlightByCache.set(cache, inFlight);
2188
- const pending = inFlight.get(text2);
2206
+ const requestKey = JSON.stringify([model, text2]);
2207
+ const pending = inFlight.get(requestKey);
2189
2208
  if (pending) {
2190
2209
  try {
2191
2210
  return Array.from(await pending);
@@ -2194,13 +2213,14 @@ async function embedOne(text2, options, outer) {
2194
2213
  }
2195
2214
  }
2196
2215
  const request = requestEmbedding(text2, options, outer);
2197
- inFlight.set(text2, request);
2216
+ inFlight.set(requestKey, request);
2198
2217
  try {
2199
2218
  const embedding = await request;
2219
+ cachedEmbeddingModels.set(embedding, model);
2200
2220
  cache.set(text2, embedding);
2201
2221
  return Array.from(embedding);
2202
2222
  } finally {
2203
- if (inFlight.get(text2) === request) inFlight.delete(text2);
2223
+ if (inFlight.get(requestKey) === request) inFlight.delete(requestKey);
2204
2224
  }
2205
2225
  }
2206
2226
  async function requestEmbedding(text2, options, outer) {
@@ -2283,13 +2303,15 @@ async function embedMany(texts, options, outer) {
2283
2303
  const { baseUrl = BASE_URL, model, batchSize, cache } = options;
2284
2304
  const timeoutMs = options.timeoutMs ?? DEFAULT_EMBEDDING_REQUEST_TIMEOUT_MS;
2285
2305
  const chunkSize = batchSize ?? DEFAULT_EMBEDDING_BATCH_SIZE;
2306
+ const embeddingModel = model ?? DEFAULT_API_EMBEDDING_MODEL;
2286
2307
  const results = new Array(texts.length).fill(null);
2287
2308
  const uncachedIndices = [];
2288
2309
  const uncachedTexts = [];
2289
2310
  for (let i = 0; i < texts.length; i++) {
2290
2311
  if (cache) {
2291
2312
  const cached = cache.get(texts[i]);
2292
- if (cached) {
2313
+ const cachedModel = cached && cachedEmbeddingModels.get(cached);
2314
+ if (cached && (cachedModel === void 0 || cachedModel === embeddingModel)) {
2293
2315
  results[i] = Array.from(cached);
2294
2316
  continue;
2295
2317
  }
@@ -2305,7 +2327,6 @@ async function embedMany(texts, options, outer) {
2305
2327
  ...taskTypeHeader(options.taskType)
2306
2328
  };
2307
2329
  if (outer?.aborted) throw outer.reason;
2308
- const embeddingModel = model ?? DEFAULT_API_EMBEDDING_MODEL;
2309
2330
  let newEmbeddings;
2310
2331
  if (uncachedTexts.length <= chunkSize) {
2311
2332
  newEmbeddings = await generateEmbeddingsBatch(
@@ -2353,6 +2374,7 @@ async function embedMany(texts, options, outer) {
2353
2374
  const f32Embedding = Float32Array.from(newEmbeddings[i]);
2354
2375
  results[uncachedIndices[i]] = Array.from(f32Embedding);
2355
2376
  if (cache) {
2377
+ cachedEmbeddingModels.set(f32Embedding, embeddingModel);
2356
2378
  cache.set(uncachedTexts[i], f32Embedding);
2357
2379
  }
2358
2380
  }
package/dist/index.mjs CHANGED
@@ -1885,6 +1885,22 @@ 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
+
1888
1904
  // src/lib/memoryEngine/generate.ts
1889
1905
  var EMBED_MAX_ATTEMPTS = 4;
1890
1906
  var DEFAULT_EMBEDDING_REQUEST_TIMEOUT_MS = 15e3;
@@ -1973,15 +1989,18 @@ async function generateEmbedding(text2, options) {
1973
1989
  if (total === void 0) return embedOne(text2, options, void 0);
1974
1990
  return withDeadline((signal) => embedOne(text2, options, signal), total, "embedding");
1975
1991
  }
1976
- var inFlightByCache = /* @__PURE__ */ new WeakMap();
1992
+ var { inFlightByCache, cachedEmbeddingModels } = getEmbeddingCacheState();
1977
1993
  async function embedOne(text2, options, outer) {
1978
1994
  const { cache } = options;
1979
1995
  if (!cache) return Array.from(await requestEmbedding(text2, options, outer));
1996
+ const model = options.model ?? DEFAULT_API_EMBEDDING_MODEL;
1980
1997
  const cached = cache.get(text2);
1981
- if (cached) return Array.from(cached);
1998
+ const cachedModel = cached && cachedEmbeddingModels.get(cached);
1999
+ if (cached && (cachedModel === void 0 || cachedModel === model)) return Array.from(cached);
1982
2000
  const inFlight = inFlightByCache.get(cache) ?? /* @__PURE__ */ new Map();
1983
2001
  inFlightByCache.set(cache, inFlight);
1984
- const pending = inFlight.get(text2);
2002
+ const requestKey = JSON.stringify([model, text2]);
2003
+ const pending = inFlight.get(requestKey);
1985
2004
  if (pending) {
1986
2005
  try {
1987
2006
  return Array.from(await pending);
@@ -1990,13 +2009,14 @@ async function embedOne(text2, options, outer) {
1990
2009
  }
1991
2010
  }
1992
2011
  const request = requestEmbedding(text2, options, outer);
1993
- inFlight.set(text2, request);
2012
+ inFlight.set(requestKey, request);
1994
2013
  try {
1995
2014
  const embedding = await request;
2015
+ cachedEmbeddingModels.set(embedding, model);
1996
2016
  cache.set(text2, embedding);
1997
2017
  return Array.from(embedding);
1998
2018
  } finally {
1999
- if (inFlight.get(text2) === request) inFlight.delete(text2);
2019
+ if (inFlight.get(requestKey) === request) inFlight.delete(requestKey);
2000
2020
  }
2001
2021
  }
2002
2022
  async function requestEmbedding(text2, options, outer) {
@@ -2079,13 +2099,15 @@ async function embedMany(texts, options, outer) {
2079
2099
  const { baseUrl = BASE_URL, model, batchSize, cache } = options;
2080
2100
  const timeoutMs = options.timeoutMs ?? DEFAULT_EMBEDDING_REQUEST_TIMEOUT_MS;
2081
2101
  const chunkSize = batchSize ?? DEFAULT_EMBEDDING_BATCH_SIZE;
2102
+ const embeddingModel = model ?? DEFAULT_API_EMBEDDING_MODEL;
2082
2103
  const results = new Array(texts.length).fill(null);
2083
2104
  const uncachedIndices = [];
2084
2105
  const uncachedTexts = [];
2085
2106
  for (let i = 0; i < texts.length; i++) {
2086
2107
  if (cache) {
2087
2108
  const cached = cache.get(texts[i]);
2088
- if (cached) {
2109
+ const cachedModel = cached && cachedEmbeddingModels.get(cached);
2110
+ if (cached && (cachedModel === void 0 || cachedModel === embeddingModel)) {
2089
2111
  results[i] = Array.from(cached);
2090
2112
  continue;
2091
2113
  }
@@ -2101,7 +2123,6 @@ async function embedMany(texts, options, outer) {
2101
2123
  ...taskTypeHeader(options.taskType)
2102
2124
  };
2103
2125
  if (outer?.aborted) throw outer.reason;
2104
- const embeddingModel = model ?? DEFAULT_API_EMBEDDING_MODEL;
2105
2126
  let newEmbeddings;
2106
2127
  if (uncachedTexts.length <= chunkSize) {
2107
2128
  newEmbeddings = await generateEmbeddingsBatch(
@@ -2149,6 +2170,7 @@ async function embedMany(texts, options, outer) {
2149
2170
  const f32Embedding = Float32Array.from(newEmbeddings[i]);
2150
2171
  results[uncachedIndices[i]] = Array.from(f32Embedding);
2151
2172
  if (cache) {
2173
+ cachedEmbeddingModels.set(f32Embedding, embeddingModel);
2152
2174
  cache.set(uncachedTexts[i], f32Embedding);
2153
2175
  }
2154
2176
  }
@@ -180,6 +180,7 @@ interface EntityOperationsContext {
180
180
  * (server / SQLite, where the migration backfill is authoritative).
181
181
  */
182
182
  allowUnscopedRows?: boolean;
183
+ canWrite?: () => Promise<boolean>;
183
184
  }
184
185
 
185
186
  /**
@@ -180,6 +180,7 @@ interface EntityOperationsContext {
180
180
  * (server / SQLite, where the migration backfill is authoritative).
181
181
  */
182
182
  allowUnscopedRows?: boolean;
183
+ canWrite?: () => Promise<boolean>;
183
184
  }
184
185
 
185
186
  /**