@anuma/sdk 7.0.1 → 7.0.2

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.
@@ -896,13 +896,35 @@ async function generateEmbedding(text, options) {
896
896
  if (total === void 0) return embedOne(text, options, void 0);
897
897
  return withDeadline((signal) => embedOne(text, options, signal), total, "embedding");
898
898
  }
899
+ var inFlightByCache = /* @__PURE__ */ new WeakMap();
899
900
  async function embedOne(text, options, outer) {
900
- const { baseUrl = BASE_URL, model, cache } = options;
901
- const timeoutMs = options.timeoutMs ?? DEFAULT_EMBEDDING_REQUEST_TIMEOUT_MS;
902
- if (cache) {
903
- const cached = cache.get(text);
904
- if (cached) return Array.from(cached);
901
+ const { cache } = options;
902
+ if (!cache) return Array.from(await requestEmbedding(text, options, outer));
903
+ const cached = cache.get(text);
904
+ if (cached) return Array.from(cached);
905
+ const inFlight = inFlightByCache.get(cache) ?? /* @__PURE__ */ new Map();
906
+ inFlightByCache.set(cache, inFlight);
907
+ const pending = inFlight.get(text);
908
+ if (pending) {
909
+ try {
910
+ return Array.from(await pending);
911
+ } catch (err) {
912
+ if (outer?.aborted) throw err;
913
+ }
914
+ }
915
+ const request = requestEmbedding(text, options, outer);
916
+ inFlight.set(text, request);
917
+ try {
918
+ const embedding = await request;
919
+ cache.set(text, embedding);
920
+ return Array.from(embedding);
921
+ } finally {
922
+ if (inFlight.get(text) === request) inFlight.delete(text);
905
923
  }
924
+ }
925
+ async function requestEmbedding(text, options, outer) {
926
+ const { baseUrl = BASE_URL, model } = options;
927
+ const timeoutMs = options.timeoutMs ?? DEFAULT_EMBEDDING_REQUEST_TIMEOUT_MS;
906
928
  const headers = {
907
929
  ...await resolveAuthHeaders(options, outer),
908
930
  ...taskTypeHeader(options.taskType)
@@ -937,11 +959,7 @@ async function embedOne(text, options, outer) {
937
959
  totalTokens: response.data.usage.total_tokens ?? 0
938
960
  });
939
961
  }
940
- const f32Embedding = Float32Array.from(embedding);
941
- if (cache) {
942
- cache.set(text, f32Embedding);
943
- }
944
- return Array.from(f32Embedding);
962
+ return Float32Array.from(embedding);
945
963
  }
946
964
  var DEFAULT_EMBEDDING_BATCH_SIZE = 100;
947
965
  var DEFAULT_EMBEDDING_BATCH_CONCURRENCY = 3;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@anuma/sdk",
3
- "version": "7.0.1",
3
+ "version": "7.0.2",
4
4
  "description": "",
5
5
  "main": "./dist/index.cjs",
6
6
  "module": "./dist/index.mjs",