@mastra/memory 1.6.2 → 1.7.0-alpha.1

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.
@@ -11,14 +11,12 @@ var xxhash = require('xxhash-wasm');
11
11
  var crypto$1 = require('crypto');
12
12
  var async_hooks = require('async_hooks');
13
13
  var imageSize = require('image-size');
14
- var lite = require('js-tiktoken/lite');
15
- var o200k_base = require('js-tiktoken/ranks/o200k_base');
14
+ var tokenx = require('tokenx');
16
15
 
17
16
  function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
18
17
 
19
18
  var xxhash__default = /*#__PURE__*/_interopDefault(xxhash);
20
19
  var imageSize__default = /*#__PURE__*/_interopDefault(imageSize);
21
- var o200k_base__default = /*#__PURE__*/_interopDefault(o200k_base);
22
20
 
23
21
  // src/processors/observational-memory/observational-memory.ts
24
22
 
@@ -1598,14 +1596,6 @@ function calculateProjectedMessageRemoval(chunks, bufferActivation, messageToken
1598
1596
  }
1599
1597
  return bestBoundaryMessageTokens;
1600
1598
  }
1601
- var GLOBAL_TIKTOKEN_KEY = "__mastraTiktoken";
1602
- function getDefaultEncoder() {
1603
- const cached = globalThis[GLOBAL_TIKTOKEN_KEY];
1604
- if (cached) return cached;
1605
- const encoder = new lite.Tiktoken(o200k_base__default.default);
1606
- globalThis[GLOBAL_TIKTOKEN_KEY] = encoder;
1607
- return encoder;
1608
- }
1609
1599
  var IMAGE_FILE_EXTENSIONS = /* @__PURE__ */ new Set([
1610
1600
  "png",
1611
1601
  "jpg",
@@ -1619,7 +1609,7 @@ var IMAGE_FILE_EXTENSIONS = /* @__PURE__ */ new Set([
1619
1609
  "heif",
1620
1610
  "avif"
1621
1611
  ]);
1622
- var TOKEN_ESTIMATE_CACHE_VERSION = 5;
1612
+ var TOKEN_ESTIMATE_CACHE_VERSION = 6;
1623
1613
  var DEFAULT_IMAGE_ESTIMATOR = {
1624
1614
  baseTokens: 85,
1625
1615
  tileTokens: 170,
@@ -1686,13 +1676,8 @@ function buildEstimateKey(kind, text) {
1686
1676
  const payloadHash = crypto$1.createHash("sha1").update(text).digest("hex");
1687
1677
  return `${kind}:${payloadHash}`;
1688
1678
  }
1689
- function resolveEncodingId(encoding) {
1690
- if (!encoding) return "o200k_base";
1691
- try {
1692
- return `custom:${crypto$1.createHash("sha1").update(JSON.stringify(encoding)).digest("hex")}`;
1693
- } catch {
1694
- return "custom:unknown";
1695
- }
1679
+ function resolveEstimatorId() {
1680
+ return "tokenx";
1696
1681
  }
1697
1682
  function isTokenEstimateEntry(value) {
1698
1683
  if (!value || typeof value !== "object") return false;
@@ -2391,20 +2376,17 @@ async function fetchGoogleAttachmentTokenEstimate(modelId, part) {
2391
2376
  }
2392
2377
  }
2393
2378
  var TokenCounter = class _TokenCounter {
2394
- encoder;
2395
2379
  cacheSource;
2396
2380
  defaultModelContext;
2397
2381
  modelContextStorage = new async_hooks.AsyncLocalStorage();
2398
2382
  inFlightAttachmentCounts = /* @__PURE__ */ new Map();
2399
2383
  // Per-message overhead: accounts for role tokens, message framing, and separators.
2400
- // Empirically derived from OpenAI's token counting guide (3 tokens per message base +
2401
- // fractional overhead from name/role encoding). 3.8 is a practical average across models.
2384
+ // 3.8 remains a practical average across providers for OM thresholding.
2402
2385
  static TOKENS_PER_MESSAGE = 3.8;
2403
2386
  // Conversation-level overhead: system prompt framing, reply priming tokens, etc.
2404
2387
  static TOKENS_PER_CONVERSATION = 24;
2405
- constructor(encoding, options) {
2406
- this.encoder = encoding ? new lite.Tiktoken(encoding) : getDefaultEncoder();
2407
- this.cacheSource = `v${TOKEN_ESTIMATE_CACHE_VERSION}:${resolveEncodingId(encoding)}`;
2388
+ constructor(options) {
2389
+ this.cacheSource = `v${TOKEN_ESTIMATE_CACHE_VERSION}:${resolveEstimatorId()}`;
2408
2390
  this.defaultModelContext = parseModelContext(options?.model);
2409
2391
  }
2410
2392
  runWithModelContext(model, fn) {
@@ -2418,7 +2400,7 @@ var TokenCounter = class _TokenCounter {
2418
2400
  */
2419
2401
  countString(text) {
2420
2402
  if (!text) return 0;
2421
- return this.encoder.encode(text, "all").length;
2403
+ return tokenx.estimateTokenCount(text);
2422
2404
  }
2423
2405
  readOrPersistPartEstimate(part, kind, payload) {
2424
2406
  const key = buildEstimateKey(kind, payload);
@@ -3314,7 +3296,7 @@ Async buffering is enabled by default \u2014 this opt-out is only needed when us
3314
3296
  ),
3315
3297
  instruction: config.reflection?.instruction
3316
3298
  };
3317
- this.tokenCounter = new TokenCounter(void 0, {
3299
+ this.tokenCounter = new TokenCounter({
3318
3300
  model: typeof observationModel === "string" ? observationModel : void 0
3319
3301
  });
3320
3302
  this.onDebugEvent = config.onDebugEvent;
@@ -6739,5 +6721,5 @@ exports.formatMessagesForObserver = formatMessagesForObserver;
6739
6721
  exports.hasCurrentTaskSection = hasCurrentTaskSection;
6740
6722
  exports.optimizeObservationsForContext = optimizeObservationsForContext;
6741
6723
  exports.parseObserverOutput = parseObserverOutput;
6742
- //# sourceMappingURL=chunk-5W5463NI.cjs.map
6743
- //# sourceMappingURL=chunk-5W5463NI.cjs.map
6724
+ //# sourceMappingURL=chunk-SHID74TI.cjs.map
6725
+ //# sourceMappingURL=chunk-SHID74TI.cjs.map