@mastra/evals 1.7.0 → 1.8.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.
@@ -1,4 +1,4 @@
1
- import { analyzeToolFailures, checkTrajectoryBlacklist, checkTrajectoryEfficiency, compareTrajectories, extractAgentResponseMessages, extractToolCalls, getAssistantMessageFromRunOutput, getCombinedSystemPrompt, getTextContentFromMastraDBMessage, getUserMessageFromRunInput, isScorerRunInputForAgent, isScorerRunOutputForAgent, roundToTwoDecimals } from "../utils.js";
1
+ import { analyzeToolFailures, checkTrajectoryBlacklist, checkTrajectoryEfficiency, compareTrajectories, extractAgentResponseMessages, extractToolCalls, getAssistantMessageFromRunOutput, getCombinedSystemPrompt, getConversationHistoryFromRunInput, getTextContentFromMastraDBMessage, getUserMessageFromRunInput, isScorerRunInputForAgent, isScorerRunOutputForAgent, roundToTwoDecimals } from "../utils.js";
2
2
  import { a as excludes, c as maxToolCalls, d as toolOrder, f as usedNoTools, i as equals, l as noToolErrors, n as checks, o as includes, r as didNotCall, s as matches, t as calledTool, u as similarity } from "../../checks-64AonnEK.js";
3
3
  import { createScorer } from "@mastra/core/evals";
4
4
  import nlp from "compromise";
@@ -2538,19 +2538,30 @@ Evaluation Guidelines:
2538
2538
  - Be objective and focus on alignment rather than response quality
2539
2539
 
2540
2540
  Score each dimension from 0.0 (completely misaligned) to 1.0 (perfectly aligned).`;
2541
- function createAnalyzePrompt$2({ userPrompt, systemPrompt, agentResponse, evaluationMode }) {
2542
- let promptContext = "";
2541
+ function createConversationHistoryContext(conversationHistory) {
2542
+ if (!conversationHistory) return "";
2543
+ return `Conversation History (prior turns, for context only):
2544
+ ${conversationHistory}
2545
+
2546
+ `;
2547
+ }
2548
+ const CONVERSATION_HISTORY_GUIDANCE = `
2549
+ Note on conversation history:
2550
+ - The conversation history is provided only to resolve the meaning of the current prompt. Use it to interpret short or referential prompts (e.g. "A", "yes", "the second one") against what was previously asked or offered.
2551
+ - Evaluate only the agent response to the current prompt. Do not score earlier turns, and do not credit or penalise the agent for anything it said in them.`;
2552
+ function createAnalyzePrompt$2({ userPrompt, systemPrompt, agentResponse, evaluationMode, conversationHistory }) {
2553
+ let promptContext = createConversationHistoryContext(conversationHistory);
2543
2554
  let evaluationTarget = "";
2544
2555
  if (evaluationMode === "user") {
2545
- promptContext = `User Prompt:
2556
+ promptContext += `User Prompt:
2546
2557
  ${userPrompt}`;
2547
2558
  evaluationTarget = "the user's prompt";
2548
2559
  } else if (evaluationMode === "system") {
2549
- promptContext = `System Prompt:
2560
+ promptContext += `System Prompt:
2550
2561
  ${systemPrompt}`;
2551
2562
  evaluationTarget = "the system's behavioral guidelines and constraints";
2552
2563
  } else {
2553
- promptContext = `User Prompt:
2564
+ promptContext += `User Prompt:
2554
2565
  ${userPrompt}
2555
2566
 
2556
2567
  System Prompt:
@@ -2563,6 +2574,7 @@ ${promptContext}
2563
2574
 
2564
2575
  Agent Response:
2565
2576
  ${agentResponse}
2577
+ ${conversationHistory ? CONVERSATION_HISTORY_GUIDANCE : ""}
2566
2578
 
2567
2579
  Evaluate the following aspects:
2568
2580
 
@@ -2685,10 +2697,10 @@ Agent Response: "def factorial(n):
2685
2697
  "overallAssessment": "The response perfectly aligns with the prompt, providing a correct Python factorial function with the requested error handling for negative numbers"
2686
2698
  }`;
2687
2699
  }
2688
- function createReasonPrompt$1({ userPrompt, systemPrompt, score, scale, analysis, evaluationMode }) {
2700
+ function createReasonPrompt$1({ userPrompt, systemPrompt, score, scale, analysis, evaluationMode, conversationHistory }) {
2689
2701
  const fulfilledCount = analysis.requirementsFulfillment.requirements.filter((r) => r.isFulfilled).length;
2690
2702
  const totalRequirements = analysis.requirementsFulfillment.requirements.length;
2691
- const promptContext = evaluationMode === "system" ? `System Prompt:\n${systemPrompt}` : evaluationMode === "user" ? `User Prompt:\n${userPrompt}` : `User Prompt:\n${userPrompt}\n\nSystem Prompt:\n${systemPrompt}`;
2703
+ const promptContext = createConversationHistoryContext(conversationHistory) + (evaluationMode === "system" ? `System Prompt:\n${systemPrompt}` : evaluationMode === "user" ? `User Prompt:\n${userPrompt}` : `User Prompt:\n${userPrompt}\n\nSystem Prompt:\n${systemPrompt}`);
2692
2704
  return `Explain the prompt alignment score based on how well the agent's response addresses the ${evaluationMode === "system" ? "system behavioral guidelines and constraints" : evaluationMode === "user" ? "user's prompt" : "both user's prompt and system guidelines"}.
2693
2705
 
2694
2706
  ${promptContext}
@@ -2835,9 +2847,14 @@ const SCORING_WEIGHTS = {
2835
2847
  SYSTEM_WEIGHT: .3
2836
2848
  }
2837
2849
  };
2850
+ function normalizeConversationHistoryOptions(includeConversationHistory) {
2851
+ if (!includeConversationHistory) return void 0;
2852
+ return includeConversationHistory === true ? {} : includeConversationHistory;
2853
+ }
2838
2854
  function createPromptAlignmentScorerLLM({ model, options }) {
2839
2855
  const scale = options?.scale || 1;
2840
2856
  const evaluationMode = options?.evaluationMode || "both";
2857
+ const historyOptions = normalizeConversationHistoryOptions(options?.includeConversationHistory);
2841
2858
  return createScorer({
2842
2859
  id: "prompt-alignment-scorer",
2843
2860
  name: "Prompt Alignment (LLM)",
@@ -2861,7 +2878,8 @@ function createPromptAlignmentScorerLLM({ model, options }) {
2861
2878
  userPrompt,
2862
2879
  systemPrompt,
2863
2880
  agentResponse,
2864
- evaluationMode
2881
+ evaluationMode,
2882
+ conversationHistory: historyOptions && getConversationHistoryFromRunInput(run.input, historyOptions)
2865
2883
  });
2866
2884
  }
2867
2885
  }).generateScore(({ results }) => {
@@ -2897,7 +2915,8 @@ function createPromptAlignmentScorerLLM({ model, options }) {
2897
2915
  score,
2898
2916
  scale,
2899
2917
  analysis,
2900
- evaluationMode
2918
+ evaluationMode,
2919
+ conversationHistory: historyOptions && getConversationHistoryFromRunInput(run.input, historyOptions)
2901
2920
  });
2902
2921
  }
2903
2922
  });