@wrongstack/core 0.256.1 → 0.257.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.
@@ -1004,12 +1004,15 @@ function getCachedEstimate(key, compute) {
1004
1004
  const existing = ESTIMATE_CACHE.get(key);
1005
1005
  if (existing !== void 0) return existing;
1006
1006
  if (ESTIMATE_CACHE.size >= ESTIMATE_CACHE_MAX_SIZE) {
1007
- const keys = [...ESTIMATE_CACHE.keys()];
1008
- for (let i = 0; i < Math.floor(ESTIMATE_CACHE_MAX_SIZE / 4); i++) {
1009
- ESTIMATE_CACHE.delete(expectDefined(keys[i]));
1007
+ let evicted = 0;
1008
+ const maxEvict = Math.floor(ESTIMATE_CACHE_MAX_SIZE / 4);
1009
+ for (const k of ESTIMATE_CACHE.keys()) {
1010
+ if (evicted >= maxEvict) break;
1011
+ ESTIMATE_CACHE.delete(k);
1012
+ evicted++;
1010
1013
  }
1011
1014
  }
1012
- const estimate = compute();
1015
+ const estimate = compute(key);
1013
1016
  ESTIMATE_CACHE.set(key, estimate);
1014
1017
  return estimate;
1015
1018
  }
@@ -1018,13 +1021,11 @@ function estimateToolInputTokens(input) {
1018
1021
  if (input === null || typeof input !== "object") {
1019
1022
  return RoughTokenEstimate(String(input));
1020
1023
  }
1021
- const key = JSON.stringify(input);
1022
- return getCachedEstimate(key, () => RoughTokenEstimate(key));
1024
+ return getCachedEstimate(JSON.stringify(input), (key) => RoughTokenEstimate(key));
1023
1025
  }
1024
1026
  function estimateToolResultTokens(content) {
1025
1027
  if (typeof content === "string") return RoughTokenEstimate(content);
1026
- const key = JSON.stringify(content);
1027
- return getCachedEstimate(key, () => RoughTokenEstimate(key));
1028
+ return getCachedEstimate(JSON.stringify(content), (key) => RoughTokenEstimate(key));
1028
1029
  }
1029
1030
  function estimateTextTokens(text) {
1030
1031
  return RoughTokenEstimate(text);