@mastra/evals 1.10.12-alpha.1 → 1.10.12-alpha.3

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.
@@ -3,7 +3,7 @@ name: mastra-evals
3
3
  description: Documentation for @mastra/evals. Use when working with @mastra/evals APIs, configuration, or implementation.
4
4
  metadata:
5
5
  package: "@mastra/evals"
6
- version: "1.10.12-alpha.1"
6
+ version: "1.10.12-alpha.3"
7
7
  ---
8
8
 
9
9
  ## When to use
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "1.10.12-alpha.1",
2
+ "version": "1.10.12-alpha.3",
3
3
  "package": "@mastra/evals",
4
4
  "exports": {},
5
5
  "modules": {}
@@ -40,14 +40,14 @@ The `.run()` method returns a result in the following shape:
40
40
  The scorer extracts and analyzes several types of elements:
41
41
 
42
42
  - Nouns: Key objects, concepts, and entities
43
- - Verbs: Actions and states (converted to infinitive form)
43
+ - Verbs: Actions and states (converted to infinitive form, English only)
44
44
  - Topics: Main subjects and themes
45
45
  - Terms: Individual substantial words
46
46
 
47
47
  The extraction process includes:
48
48
 
49
49
  - Normalization of text (removing diacritics, converting to lowercase)
50
- - Handling of word boundaries
50
+ - Handling of word boundaries in any script, including scripts written without spaces such as Japanese, Chinese, and Thai
51
51
  - Special handling of short words (3 characters or less)
52
52
  - Deduplication of elements
53
53
 
@@ -3966,8 +3966,9 @@ function extractElements(doc) {
3966
3966
  const verbs = doc.verbs().toInfinitive().out("array") || [];
3967
3967
  const topics = doc.topics().out("array") || [];
3968
3968
  const terms = doc.terms().out("array") || [];
3969
+ const wordSegmenter = new Intl.Segmenter("en", { granularity: "word" });
3969
3970
  const cleanAndSplitTerm = (term) => {
3970
- return normalizeString(term).replace(/([a-z])([A-Z])/g, "$1 $2").replace(/[^a-z0-9]+/g, " ").trim().split(/\s+/).filter((word) => word.length > 0);
3971
+ return (normalizeString(term).normalize("NFC").replace(/([a-z])([A-Z])/g, "$1 $2").match(/[a-z0-9]+|(?:(?![\p{scx=Latin}\p{scx=Common}])[\p{L}\p{Nl}\p{No}]\p{M}*)+|\p{Nd}+/gu) ?? []).flatMap((run) => Array.from(wordSegmenter.segment(run), (part) => part.segment));
3971
3972
  };
3972
3973
  const processedTerms = [
3973
3974
  ...nouns.flatMap(cleanAndSplitTerm),
@@ -4040,23 +4041,31 @@ function createCompletenessScorer() {
4040
4041
  function calculateRatio(input, output) {
4041
4042
  if (input === output) return 1;
4042
4043
  if (input.length === 0 || output.length === 0) return 0;
4043
- const matches = longestCommonSubsequence(input, output);
4044
+ const matches = longestCommonSubsequence(input.split(""), output.split(""));
4044
4045
  const total = input.length + output.length;
4045
4046
  return total > 0 ? 2 * matches / total : 0;
4046
4047
  }
4047
4048
  /**
4048
- * Finds the length of the longest common subsequence between two strings
4049
+ * Finds the length of the longest common subsequence between two lists of characters or words
4049
4050
  * Only the previous row of the DP table is needed, so memory stays linear
4050
4051
  */
4051
- function longestCommonSubsequence(str1, str2) {
4052
- let previous = new Uint32Array(str2.length + 1);
4053
- let current = new Uint32Array(str2.length + 1);
4054
- for (let i = 1; i <= str1.length; i++) {
4055
- const char = str1[i - 1];
4056
- for (let j = 1; j <= str2.length; j++) current[j] = char === str2[j - 1] ? previous[j - 1] + 1 : Math.max(previous[j], current[j - 1]);
4052
+ function longestCommonSubsequence(first, second) {
4053
+ const codes = /* @__PURE__ */ new Map();
4054
+ const encode = (items) => new Uint32Array(items.map((item) => {
4055
+ const code = codes.get(item) ?? codes.size;
4056
+ codes.set(item, code);
4057
+ return code;
4058
+ }));
4059
+ const firstCodes = encode(first);
4060
+ const secondCodes = encode(second);
4061
+ let previous = new Uint32Array(secondCodes.length + 1);
4062
+ let current = new Uint32Array(secondCodes.length + 1);
4063
+ for (let i = 1; i <= firstCodes.length; i++) {
4064
+ const code = firstCodes[i - 1];
4065
+ for (let j = 1; j <= secondCodes.length; j++) current[j] = code === secondCodes[j - 1] ? previous[j - 1] + 1 : Math.max(previous[j], current[j - 1]);
4057
4066
  [previous, current] = [current, previous];
4058
4067
  }
4059
- return previous[str2.length];
4068
+ return previous[secondCodes.length];
4060
4069
  }
4061
4070
  /**
4062
4071
  * Counts the number of differences between two strings
@@ -4079,22 +4088,9 @@ function countChanges(input, output) {
4079
4088
  if (inputWords.length === 0 && outputWords.length === 0) return 0;
4080
4089
  if (inputWords.length === 0) return outputWords.length;
4081
4090
  if (outputWords.length === 0) return inputWords.length;
4082
- const matchingWords = findCommonWords(inputWords, outputWords);
4091
+ const matchingWords = longestCommonSubsequence(inputWords, outputWords);
4083
4092
  return Math.max(inputWords.length, outputWords.length) - matchingWords;
4084
4093
  }
4085
- /**
4086
- * Finds the number of common words between two arrays using a greedy matching approach
4087
- */
4088
- function findCommonWords(arr1, arr2) {
4089
- let matches = 0;
4090
- const used = /* @__PURE__ */ new Set();
4091
- for (let i = 0; i < arr1.length; i++) for (let j = 0; j < arr2.length; j++) if (!used.has(j) && arr1[i] === arr2[j]) {
4092
- matches++;
4093
- used.add(j);
4094
- break;
4095
- }
4096
- return matches;
4097
- }
4098
4094
  function createTextualDifferenceScorer() {
4099
4095
  return (0, _mastra_core_evals.createScorer)({
4100
4096
  id: "textual-difference-scorer",
@@ -4231,7 +4227,14 @@ function createToneScorer(config = {}) {
4231
4227
  difference: sentimentDiff
4232
4228
  };
4233
4229
  }
4234
- const sentiments = (agentMessage.match(/[^.!?]+[.!?]+/g) || [agentMessage]).map((s) => sentiment$1.analyze(s).comparative);
4230
+ const sentiments = (agentMessage.match(/[^.!?\n]+(?:\.(?=\d|\p{Ll}{2}|\p{Lu}{2})[^.!?\n]*)*[.!?]*/gu) || []).flatMap((sentence) => {
4231
+ const { score, tokens } = sentiment$1.analyze(sentence);
4232
+ const words = tokens.filter((token) => /\p{L}/u.test(token)).length;
4233
+ const isCitationOrTag = !score && (/^\s*(\[.*\]|<.*>)\s*$/.test(sentence) || words < 3 && /^\s*\(.*\)\s*$/.test(sentence));
4234
+ if (!words || isCitationOrTag) return [];
4235
+ return score / (words > 2 || /[.!?]$/.test(sentence) ? tokens.length : Math.max(tokens.length, 10));
4236
+ });
4237
+ if (sentiments.length === 0) sentiments.push(responseSentiment.comparative);
4235
4238
  const avgSentiment = sentiments.reduce((a, b) => a + b, 0) / sentiments.length;
4236
4239
  const variance = sentiments.reduce((sum, s) => sum + Math.pow(s - avgSentiment, 2), 0) / sentiments.length;
4237
4240
  return {
@@ -3961,8 +3961,9 @@ function extractElements(doc) {
3961
3961
  const verbs = doc.verbs().toInfinitive().out("array") || [];
3962
3962
  const topics = doc.topics().out("array") || [];
3963
3963
  const terms = doc.terms().out("array") || [];
3964
+ const wordSegmenter = new Intl.Segmenter("en", { granularity: "word" });
3964
3965
  const cleanAndSplitTerm = (term) => {
3965
- return normalizeString(term).replace(/([a-z])([A-Z])/g, "$1 $2").replace(/[^a-z0-9]+/g, " ").trim().split(/\s+/).filter((word) => word.length > 0);
3966
+ return (normalizeString(term).normalize("NFC").replace(/([a-z])([A-Z])/g, "$1 $2").match(/[a-z0-9]+|(?:(?![\p{scx=Latin}\p{scx=Common}])[\p{L}\p{Nl}\p{No}]\p{M}*)+|\p{Nd}+/gu) ?? []).flatMap((run) => Array.from(wordSegmenter.segment(run), (part) => part.segment));
3966
3967
  };
3967
3968
  const processedTerms = [
3968
3969
  ...nouns.flatMap(cleanAndSplitTerm),
@@ -4035,23 +4036,31 @@ function createCompletenessScorer() {
4035
4036
  function calculateRatio(input, output) {
4036
4037
  if (input === output) return 1;
4037
4038
  if (input.length === 0 || output.length === 0) return 0;
4038
- const matches = longestCommonSubsequence(input, output);
4039
+ const matches = longestCommonSubsequence(input.split(""), output.split(""));
4039
4040
  const total = input.length + output.length;
4040
4041
  return total > 0 ? 2 * matches / total : 0;
4041
4042
  }
4042
4043
  /**
4043
- * Finds the length of the longest common subsequence between two strings
4044
+ * Finds the length of the longest common subsequence between two lists of characters or words
4044
4045
  * Only the previous row of the DP table is needed, so memory stays linear
4045
4046
  */
4046
- function longestCommonSubsequence(str1, str2) {
4047
- let previous = new Uint32Array(str2.length + 1);
4048
- let current = new Uint32Array(str2.length + 1);
4049
- for (let i = 1; i <= str1.length; i++) {
4050
- const char = str1[i - 1];
4051
- for (let j = 1; j <= str2.length; j++) current[j] = char === str2[j - 1] ? previous[j - 1] + 1 : Math.max(previous[j], current[j - 1]);
4047
+ function longestCommonSubsequence(first, second) {
4048
+ const codes = /* @__PURE__ */ new Map();
4049
+ const encode = (items) => new Uint32Array(items.map((item) => {
4050
+ const code = codes.get(item) ?? codes.size;
4051
+ codes.set(item, code);
4052
+ return code;
4053
+ }));
4054
+ const firstCodes = encode(first);
4055
+ const secondCodes = encode(second);
4056
+ let previous = new Uint32Array(secondCodes.length + 1);
4057
+ let current = new Uint32Array(secondCodes.length + 1);
4058
+ for (let i = 1; i <= firstCodes.length; i++) {
4059
+ const code = firstCodes[i - 1];
4060
+ for (let j = 1; j <= secondCodes.length; j++) current[j] = code === secondCodes[j - 1] ? previous[j - 1] + 1 : Math.max(previous[j], current[j - 1]);
4052
4061
  [previous, current] = [current, previous];
4053
4062
  }
4054
- return previous[str2.length];
4063
+ return previous[secondCodes.length];
4055
4064
  }
4056
4065
  /**
4057
4066
  * Counts the number of differences between two strings
@@ -4074,22 +4083,9 @@ function countChanges(input, output) {
4074
4083
  if (inputWords.length === 0 && outputWords.length === 0) return 0;
4075
4084
  if (inputWords.length === 0) return outputWords.length;
4076
4085
  if (outputWords.length === 0) return inputWords.length;
4077
- const matchingWords = findCommonWords(inputWords, outputWords);
4086
+ const matchingWords = longestCommonSubsequence(inputWords, outputWords);
4078
4087
  return Math.max(inputWords.length, outputWords.length) - matchingWords;
4079
4088
  }
4080
- /**
4081
- * Finds the number of common words between two arrays using a greedy matching approach
4082
- */
4083
- function findCommonWords(arr1, arr2) {
4084
- let matches = 0;
4085
- const used = /* @__PURE__ */ new Set();
4086
- for (let i = 0; i < arr1.length; i++) for (let j = 0; j < arr2.length; j++) if (!used.has(j) && arr1[i] === arr2[j]) {
4087
- matches++;
4088
- used.add(j);
4089
- break;
4090
- }
4091
- return matches;
4092
- }
4093
4089
  function createTextualDifferenceScorer() {
4094
4090
  return createScorer({
4095
4091
  id: "textual-difference-scorer",
@@ -4226,7 +4222,14 @@ function createToneScorer(config = {}) {
4226
4222
  difference: sentimentDiff
4227
4223
  };
4228
4224
  }
4229
- const sentiments = (agentMessage.match(/[^.!?]+[.!?]+/g) || [agentMessage]).map((s) => sentiment.analyze(s).comparative);
4225
+ const sentiments = (agentMessage.match(/[^.!?\n]+(?:\.(?=\d|\p{Ll}{2}|\p{Lu}{2})[^.!?\n]*)*[.!?]*/gu) || []).flatMap((sentence) => {
4226
+ const { score, tokens } = sentiment.analyze(sentence);
4227
+ const words = tokens.filter((token) => /\p{L}/u.test(token)).length;
4228
+ const isCitationOrTag = !score && (/^\s*(\[.*\]|<.*>)\s*$/.test(sentence) || words < 3 && /^\s*\(.*\)\s*$/.test(sentence));
4229
+ if (!words || isCitationOrTag) return [];
4230
+ return score / (words > 2 || /[.!?]$/.test(sentence) ? tokens.length : Math.max(tokens.length, 10));
4231
+ });
4232
+ if (sentiments.length === 0) sentiments.push(responseSentiment.comparative);
4230
4233
  const avgSentiment = sentiments.reduce((a, b) => a + b, 0) / sentiments.length;
4231
4234
  const variance = sentiments.reduce((sum, s) => sum + Math.pow(s - avgSentiment, 2), 0) / sentiments.length;
4232
4235
  return {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mastra/evals",
3
- "version": "1.10.12-alpha.1",
3
+ "version": "1.10.12-alpha.3",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "files": [
@@ -118,7 +118,7 @@
118
118
  "@internal/llm-recorder": "0.0.87",
119
119
  "@internal/test-utils": "0.0.87",
120
120
  "@internal/types-builder": "0.0.126",
121
- "@mastra/core": "1.86.0-alpha.6"
121
+ "@mastra/core": "1.86.0-alpha.8"
122
122
  },
123
123
  "engines": {
124
124
  "node": ">=22.13.0"