@mastra/memory 1.38.0-alpha.0 → 1.38.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.
@@ -19825,21 +19825,22 @@ function safeSlice(str, end) {
19825
19825
  return str.slice(0, safeEnd);
19826
19826
  }
19827
19827
  //#endregion
19828
- //#region ../../../../../setup-pnpm/node_modules/.bin/store/v11/links/@/tokenx/1.3.0/09093cdaeebfe25c39ba9052d6277b45fc99abf5e14e859f60f338f5c5160afd/node_modules/tokenx/dist/index.mjs
19828
+ //#region ../../../../../setup-pnpm/node_modules/.bin/store/v11/links/@/tokenx/1.6.0/394289cb1bed8c542941f77748eea1a31bd3419018d0fb2ac7029ece2458cdd1/node_modules/tokenx/dist/index.mjs
19829
19829
  const PATTERNS = {
19830
19830
  whitespace: /^\s+$/,
19831
- cjk: /[\u4E00-\u9FFF\u3400-\u4DBF\u3000-\u303F\uFF00-\uFFEF\u30A0-\u30FF\u2E80-\u2EFF\u31C0-\u31EF\u3200-\u32FF\u3300-\u33FF\uAC00-\uD7AF\u1100-\u11FF\u3130-\u318F\uA960-\uA97F\uD7B0-\uD7FF]/,
19832
- numeric: /^\d+(?:[.,]\d+)*$/,
19833
- punctuation: /[.,!?;(){}[\]<>:/\\|@#$%^&*+=`~_-]/,
19834
- alphanumeric: /^[a-zA-Z0-9\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u00FF]+$/
19831
+ structuredWhitespace: /\n\s/,
19832
+ cjk: /[\u4E00-\u9FFF\u3400-\u4DBF\u3000-\u30FF\uFF00-\uFFEF\u2E80-\u2EFF\u31C0-\u31EF\u3200-\u32FF\u3300-\u33FF\uAC00-\uD7AF\u1100-\u11FF\u3130-\u318F\uA960-\uA97F\uD7B0-\uD7FF]/,
19833
+ numeric: /^\d+$/,
19834
+ punctuation: /[.,!?;(){}[\]<>:/\\|@#$%^&*+=`~_"-]/
19835
19835
  };
19836
- const TOKEN_SPLIT_PATTERN = /* @__PURE__ */ new RegExp(`(\\s+|${PATTERNS.punctuation.source}+)`);
19836
+ const TOKEN_SPLIT_PATTERN = new RegExp(`(\\s+|${PATTERNS.punctuation.source}+)`);
19837
19837
  const DEFAULT_CHARS_PER_TOKEN = 6;
19838
19838
  const SHORT_TOKEN_THRESHOLD = 3;
19839
+ const KANA_CHARS_PER_TOKEN = 1.35;
19839
19840
  const DEFAULT_LANGUAGE_CONFIGS = [
19840
19841
  {
19841
19842
  pattern: /[äöüßẞ]/i,
19842
- averageCharsPerToken: 3
19843
+ averageCharsPerToken: 2.6
19843
19844
  },
19844
19845
  {
19845
19846
  pattern: /[éèêëàâîïôûùüÿçœæáíóúñ]/i,
@@ -19847,39 +19848,75 @@ const DEFAULT_LANGUAGE_CONFIGS = [
19847
19848
  },
19848
19849
  {
19849
19850
  pattern: /[ąćęłńóśźżěščřžýůúďťň]/i,
19850
- averageCharsPerToken: 3.5
19851
+ averageCharsPerToken: 2.5
19852
+ },
19853
+ {
19854
+ pattern: /[\u0430-\u044F\u0451]/i,
19855
+ averageCharsPerToken: 4
19856
+ },
19857
+ {
19858
+ pattern: /[\u03AC-\u03CE]/i,
19859
+ averageCharsPerToken: 2.75
19860
+ },
19861
+ {
19862
+ pattern: /^\p{Extended_Pictographic}[\p{Extended_Pictographic}\p{Emoji_Component}]*$/u,
19863
+ averageCharsPerToken: .75
19851
19864
  }
19852
19865
  ];
19853
19866
  /**
19854
- * Estimates the number of tokens in a text string using heuristic rules.
19867
+ * Walks a text as (segment, estimated token count) pairs. The segments
19868
+ * concatenate back to the original text; whitespace segments count zero
19869
+ * tokens unless they carry structure (indentation or blank lines).
19855
19870
  */
19856
- function estimateTokenCount(text, options = {}) {
19857
- if (!text) return 0;
19858
- const { defaultCharsPerToken = DEFAULT_CHARS_PER_TOKEN, languageConfigs = DEFAULT_LANGUAGE_CONFIGS } = options;
19859
- const segments = text.split(TOKEN_SPLIT_PATTERN).filter(Boolean);
19860
- let tokenCount = 0;
19861
- for (const segment of segments) tokenCount += estimateSegmentTokens(segment, languageConfigs, defaultCharsPerToken);
19862
- return tokenCount;
19871
+ function* walkSegments(text, options = {}) {
19872
+ if (!text) return;
19873
+ const resolvedOptions = resolveOptions(options);
19874
+ for (const segment of text.split(TOKEN_SPLIT_PATTERN)) if (segment) yield {
19875
+ segment,
19876
+ tokenCount: estimateSegmentTokens(segment, resolvedOptions)
19877
+ };
19863
19878
  }
19864
- function estimateSegmentTokens(segment, languageConfigs, defaultCharsPerToken) {
19865
- if (PATTERNS.whitespace.test(segment)) return 0;
19866
- if (PATTERNS.cjk.test(segment)) return getCharacterCount(segment);
19867
- if (PATTERNS.numeric.test(segment)) return 1;
19879
+ function resolveOptions(options) {
19880
+ return {
19881
+ defaultCharsPerToken: options.defaultCharsPerToken ?? DEFAULT_CHARS_PER_TOKEN,
19882
+ languageConfigs: options.languageConfigs ?? DEFAULT_LANGUAGE_CONFIGS
19883
+ };
19884
+ }
19885
+ function estimateSegmentTokens(segment, { languageConfigs, defaultCharsPerToken }) {
19886
+ if (PATTERNS.whitespace.test(segment)) return PATTERNS.structuredWhitespace.test(segment) ? 1 : 0;
19887
+ const languageCharsPerToken = getLanguageSpecificCharsPerToken(segment, languageConfigs);
19888
+ if (languageCharsPerToken !== void 0) return Math.ceil(getCharacterCount(segment) / languageCharsPerToken);
19889
+ if (PATTERNS.cjk.test(segment)) return estimateCjkTokens(segment);
19890
+ if (PATTERNS.numeric.test(segment)) return Math.ceil(segment.length / 3);
19868
19891
  if (segment.length <= SHORT_TOKEN_THRESHOLD) return 1;
19869
- if (PATTERNS.punctuation.test(segment)) return segment.length > 1 ? Math.ceil(segment.length / 2) : 1;
19870
- if (PATTERNS.alphanumeric.test(segment)) {
19871
- const charsPerToken$1 = getLanguageSpecificCharsPerToken(segment, languageConfigs) ?? defaultCharsPerToken;
19872
- return Math.ceil(segment.length / charsPerToken$1);
19873
- }
19874
- const charsPerToken = getLanguageSpecificCharsPerToken(segment, languageConfigs) ?? defaultCharsPerToken;
19875
- return Math.ceil(segment.length / charsPerToken);
19892
+ if (PATTERNS.punctuation.test(segment)) return Math.ceil(segment.length / 2);
19893
+ return Math.ceil(segment.length / defaultCharsPerToken);
19876
19894
  }
19877
19895
  function getLanguageSpecificCharsPerToken(segment, languageConfigs) {
19878
- for (const config of languageConfigs) if (config.pattern.test(segment)) return config.averageCharsPerToken;
19896
+ for (const config of languageConfigs) if (segment.search(config.pattern) !== -1) return config.averageCharsPerToken;
19879
19897
  }
19880
19898
  function getCharacterCount(text) {
19881
19899
  return Array.from(text).length;
19882
19900
  }
19901
+ function estimateCjkTokens(segment) {
19902
+ let kanaCount = 0;
19903
+ let otherCount = 0;
19904
+ for (const character of segment) {
19905
+ const codePoint = character.codePointAt(0);
19906
+ if (codePoint >= 12352 && codePoint <= 12543) kanaCount++;
19907
+ else otherCount++;
19908
+ }
19909
+ return otherCount + Math.ceil(kanaCount / KANA_CHARS_PER_TOKEN);
19910
+ }
19911
+ /**
19912
+ * Estimates the number of tokens in a text string using heuristic rules
19913
+ */
19914
+ function estimateTokenCount(text, options = {}) {
19915
+ if (!text) return 0;
19916
+ let tokenCount = 0;
19917
+ for (const segmentEstimate of walkSegments(text, options)) tokenCount += segmentEstimate.tokenCount;
19918
+ return tokenCount;
19919
+ }
19883
19920
  //#endregion
19884
19921
  //#region src/processors/observational-memory/tool-argument-helpers.ts
19885
19922
  const DEFAULT_OBSERVER_TOOL_ARGUMENT_MAX_TOKENS = 2e3;
@@ -22400,7 +22437,7 @@ const IMAGE_FILE_EXTENSIONS = /* @__PURE__ */ new Set([
22400
22437
  "heif",
22401
22438
  "avif"
22402
22439
  ]);
22403
- const TOKEN_ESTIMATE_CACHE_VERSION = 7;
22440
+ const TOKEN_ESTIMATE_CACHE_VERSION = 8;
22404
22441
  /**
22405
22442
  * Cache `source` marker for token estimates supplied by the caller via
22406
22443
  * `part.providerMetadata.mastra.tokenEstimate`. Pipelines that strip the real
@@ -37006,4 +37043,4 @@ Object.defineProperty(exports, "wrapInObservationGroup", {
37006
37043
  }
37007
37044
  });
37008
37045
 
37009
- //# sourceMappingURL=src-BfhwVNr7.cjs.map
37046
+ //# sourceMappingURL=src-DZmiwOWq.cjs.map