@mastra/evals 1.10.9 → 1.10.10

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.
@@ -1414,17 +1414,19 @@ STRICT EVALUATION CRITERIA:
1414
1414
  - If the user asked for "A", only tools that provide "A" should be marked appropriate
1415
1415
  - Additional tools the agent decided to call without being asked should be marked inappropriate
1416
1416
 
1417
- Evaluate each tool that was called, or if no tools were called, evaluate whether that was the right decision.
1417
+ If tools were called, return exactly one evaluation per entry in TOOLS THE AGENT ACTUALLY CALLED, including repeated calls, in the same order. Repeated calls with different arguments can each be appropriate when they address the request. If no tools were called, evaluate whether that was the right decision.
1418
1418
  `;
1419
1419
  };
1420
- const createReasonPrompt$4 = ({ userInput, score, evaluations, missingTools }) => {
1420
+ const createReasonPrompt$4 = ({ userInput, score, actualTools, matchedEvaluations, missingTools }) => {
1421
1421
  return `
1422
1422
  Explain this tool selection evaluation in ONE SENTENCE.
1423
1423
 
1424
1424
  User Request: "${userInput}"
1425
1425
  Score: ${score}/1
1426
- Tools Evaluated: ${JSON.stringify(evaluations)}
1426
+ Actual Tool Calls: ${JSON.stringify(actualTools)}
1427
+ Matched Tool Evaluations: ${JSON.stringify(matchedEvaluations)}
1427
1428
  Missing Tools: ${JSON.stringify(missingTools)}
1429
+ For called tools, the score counts appropriate matched evaluations divided by actual tool calls plus missing tools. Calls without evaluations lower the score. With no calls, the score is 0 if tools were missing and 1 otherwise.
1428
1430
 
1429
1431
  Provide a single, concise sentence explaining why this score was given.
1430
1432
  `;
@@ -1457,6 +1459,16 @@ const analyzeOutputSchema$6 = {
1457
1459
  },
1458
1460
  "required": ["evaluations"]
1459
1461
  };
1462
+ function matchToolEvaluations(actualTools, evaluations) {
1463
+ const remainingCalls = /* @__PURE__ */ new Map();
1464
+ for (const toolName of actualTools) remainingCalls.set(toolName, (remainingCalls.get(toolName) ?? 0) + 1);
1465
+ return evaluations.filter((evaluation) => {
1466
+ const remaining = remainingCalls.get(evaluation.toolCalled) ?? 0;
1467
+ if (remaining === 0) return false;
1468
+ remainingCalls.set(evaluation.toolCalled, remaining - 1);
1469
+ return true;
1470
+ });
1471
+ }
1460
1472
  function createToolCallAccuracyScorerLLM({ model, availableTools }) {
1461
1473
  const toolDefinitions = availableTools.map((tool) => `${tool.id}: ${tool.description}`).join("\n");
1462
1474
  return createScorer({
@@ -1494,20 +1506,24 @@ function createToolCallAccuracyScorerLLM({ model, availableTools }) {
1494
1506
  }
1495
1507
  }).generateScore(({ results }) => {
1496
1508
  const evaluations = results.analyzeStepResult?.evaluations || [];
1497
- if (evaluations.length === 0) return (results.analyzeStepResult?.missingTools || []).length > 0 ? 0 : 1;
1498
- const appropriateToolCalls = evaluations.filter((e) => e.wasAppropriate).length;
1499
- const totalToolCalls = evaluations.length;
1500
- return roundToTwoDecimals(appropriateToolCalls / totalToolCalls);
1509
+ const missingTools = results.analyzeStepResult?.missingTools || [];
1510
+ const actualTools = results.preprocessStepResult?.actualTools || [];
1511
+ const totalToolCalls = actualTools.length;
1512
+ if (totalToolCalls === 0) return missingTools.length > 0 ? 0 : 1;
1513
+ const appropriateToolCalls = matchToolEvaluations(actualTools, evaluations).filter((evaluation) => evaluation.wasAppropriate).length;
1514
+ return roundToTwoDecimals(Math.min(1, appropriateToolCalls / (totalToolCalls + missingTools.length)));
1501
1515
  }).generateReason({
1502
1516
  description: "Generate human-readable explanation of tool selection evaluation",
1503
1517
  createPrompt: ({ run, results, score }) => {
1504
1518
  const userInput = getUserMessageFromRunInput(run.input) ?? "";
1505
1519
  const evaluations = results.analyzeStepResult?.evaluations || [];
1506
1520
  const missingTools = results.analyzeStepResult?.missingTools || [];
1521
+ const actualTools = results.preprocessStepResult?.actualTools || [];
1507
1522
  return createReasonPrompt$4({
1508
1523
  userInput,
1509
1524
  score,
1510
- evaluations,
1525
+ actualTools,
1526
+ matchedEvaluations: matchToolEvaluations(actualTools, evaluations),
1511
1527
  missingTools
1512
1528
  });
1513
1529
  }