@mastra/evals 1.10.2-alpha.0 → 1.10.2-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.
@@ -351,8 +351,10 @@ const getReasoningFromRunOutput = (output) => {
351
351
  * @param options.toolCallId - Unique identifier for the tool call
352
352
  * @param options.toolName - Name of the tool being called
353
353
  * @param options.args - Arguments passed to the tool
354
- * @param options.result - Result returned by the tool
354
+ * @param options.result - Result returned by the tool (absent for a thrown call)
355
355
  * @param options.state - State of the invocation (default: 'result')
356
+ * @param options.errorText - Error text recorded when a tool threw (`state: 'output-error'`)
357
+ * @param options.isError - Set alongside `state: 'result'` when the executor reported a failure
356
358
  * @returns A tool invocation object
357
359
  *
358
360
  * @example
@@ -363,15 +365,26 @@ const getReasoningFromRunOutput = (output) => {
363
365
  * args: { location: 'London' },
364
366
  * result: { temperature: 20, condition: 'sunny' },
365
367
  * });
368
+ *
369
+ * // A tool that threw
370
+ * const thrown = createToolInvocation({
371
+ * toolCallId: 'call-456',
372
+ * toolName: 'saveTool',
373
+ * args: { value: 'x' },
374
+ * state: 'output-error',
375
+ * errorText: 'Save failed',
376
+ * });
366
377
  * ```
367
378
  */
368
- const createToolInvocation = ({ toolCallId, toolName, args, result, state = "result" }) => {
379
+ const createToolInvocation = ({ toolCallId, toolName, args, result, state = "result", errorText, isError }) => {
369
380
  return {
370
381
  toolCallId,
371
382
  toolName,
372
383
  args,
373
384
  result,
374
- state
385
+ state,
386
+ ...errorText !== void 0 && { errorText },
387
+ ...isError !== void 0 && { isError }
375
388
  };
376
389
  };
377
390
  /**
@@ -405,15 +418,34 @@ const createToolInvocation = ({ toolCallId, toolName, args, result, state = "res
405
418
  * state: 'result',
406
419
  * }],
407
420
  * });
421
+ *
422
+ * // With native V2 parts — the only form that can carry a thrown call
423
+ * const messageWithThrownTool = createTestMessage({
424
+ * content: 'That failed.',
425
+ * role: 'assistant',
426
+ * parts: [
427
+ * { type: 'text', text: 'That failed.' },
428
+ * {
429
+ * type: 'tool-invocation',
430
+ * toolInvocation: createToolInvocation({
431
+ * toolCallId: 'call-2',
432
+ * toolName: 'saveTool',
433
+ * args: { value: 'x' },
434
+ * state: 'output-error',
435
+ * errorText: 'Save failed',
436
+ * }),
437
+ * },
438
+ * ],
439
+ * });
408
440
  * ```
409
441
  */
410
- function createTestMessage({ content, role, id = "test-message", toolInvocations = [] }) {
442
+ function createTestMessage({ content, role, id = "test-message", toolInvocations = [], parts }) {
411
443
  return {
412
444
  id,
413
445
  role,
414
446
  content: {
415
447
  format: 2,
416
- parts: [{
448
+ parts: parts ?? [{
417
449
  type: "text",
418
450
  text: content
419
451
  }],
@@ -502,10 +534,56 @@ const createTrajectoryTestRun = ({ inputMessages = [], trajectory, rememberedMes
502
534
  };
503
535
  };
504
536
  /**
537
+ * Merges the two places a message can store tool invocations into one list.
538
+ *
539
+ * A native message may carry `content.parts` tool-invocation entries, the legacy
540
+ * `content.toolInvocations` array, or both. Neither form is a superset of the
541
+ * other: thrown calls are recorded in parts as `state: 'output-error'` and can be
542
+ * missing from the legacy array, while older persisted messages may only carry the
543
+ * legacy array. Reading one form exclusively therefore hides real calls.
544
+ *
545
+ * Parts win when the same `toolCallId` appears in both, since they hold the real
546
+ * terminal state. Legacy-only calls are retained before their next shared anchor so
547
+ * compatible call order survives instead of being appended.
548
+ *
549
+ * Mirrors `extractTrajectory` in `@mastra/core` (`packages/core/src/evals/types.ts`)
550
+ * so tool-use checks and trajectory scorers agree on what a run actually called.
551
+ * Keep the two in step.
552
+ *
553
+ * @param message - A single message from the scorer run output
554
+ * @returns The merged tool invocations for that message
555
+ */
556
+ function mergeToolInvocations(message) {
557
+ const legacy = message?.content?.toolInvocations;
558
+ const fromParts = message?.content?.parts?.filter((p) => p.type === "tool-invocation").map((p) => p.toolInvocation).filter(Boolean) ?? [];
559
+ const partCallIds = new Set(fromParts.map((invocation) => invocation.toolCallId).filter(Boolean));
560
+ const legacyInvocations = legacy ?? [];
561
+ const legacyPositions = new Map(legacyInvocations.map((invocation, index) => [invocation?.toolCallId, index]));
562
+ const toolInvocations = [];
563
+ let legacyIndex = 0;
564
+ for (const invocation of fromParts) {
565
+ const sharedIndex = invocation.toolCallId ? legacyPositions.get(invocation.toolCallId) : void 0;
566
+ if (sharedIndex !== void 0 && sharedIndex >= legacyIndex) {
567
+ for (; legacyIndex < sharedIndex; legacyIndex++) {
568
+ const previous = legacyInvocations[legacyIndex];
569
+ if (previous && !partCallIds.has(previous.toolCallId)) toolInvocations.push(previous);
570
+ }
571
+ legacyIndex++;
572
+ }
573
+ toolInvocations.push(invocation);
574
+ }
575
+ for (; legacyIndex < legacyInvocations.length; legacyIndex++) {
576
+ const invocation = legacyInvocations[legacyIndex];
577
+ if (invocation && !partCallIds.has(invocation.toolCallId)) toolInvocations.push(invocation);
578
+ }
579
+ return toolInvocations;
580
+ }
581
+ /**
505
582
  * Extracts all tool calls from a scorer run output.
506
583
  *
507
- * Iterates through all messages and their tool invocations to collect
508
- * information about tools that were called (with state 'result' or 'call').
584
+ * Iterates through all messages and their tool invocations to collect information
585
+ * about tools that were called. A call counts regardless of outcome, so the states
586
+ * `'result'`, `'call'`, and `'output-error'` (a tool that threw) are all included.
509
587
  *
510
588
  * @param output - The scorer run output (array of MastraDBMessage)
511
589
  * @returns An object containing tool names and detailed tool call info
@@ -527,13 +605,11 @@ function extractToolCalls(output) {
527
605
  const toolCallInfos = [];
528
606
  for (let messageIndex = 0; messageIndex < output.length; messageIndex++) {
529
607
  const message = output[messageIndex];
530
- const legacy = message?.content?.toolInvocations;
531
- const fromParts = legacy ? void 0 : message?.content?.parts?.filter((p) => p.type === "tool-invocation").map((p) => p.toolInvocation);
532
- const toolInvocations = legacy ?? fromParts;
533
- if (!toolInvocations?.length) continue;
608
+ const toolInvocations = mergeToolInvocations(message);
609
+ if (!toolInvocations.length) continue;
534
610
  for (let invocationIndex = 0; invocationIndex < toolInvocations.length; invocationIndex++) {
535
611
  const invocation = toolInvocations[invocationIndex];
536
- if (invocation && invocation.toolName && (invocation.state === "result" || invocation.state === "call")) {
612
+ if (invocation && invocation.toolName && (invocation.state === "result" || invocation.state === "call" || invocation.state === "output-error")) {
537
613
  toolCalls.push(invocation.toolName);
538
614
  toolCallInfos.push({
539
615
  toolName: invocation.toolName,
@@ -615,10 +691,8 @@ const extractAgentResponseMessages = (runOutput) => {
615
691
  function extractToolResults(output) {
616
692
  const results = [];
617
693
  for (const message of output) {
618
- const legacy = message?.content?.toolInvocations;
619
- const fromParts = legacy ? void 0 : message?.content?.parts?.filter((p) => p.type === "tool-invocation").map((p) => p.toolInvocation);
620
- const toolInvocations = legacy ?? fromParts;
621
- if (!toolInvocations?.length) continue;
694
+ const toolInvocations = mergeToolInvocations(message);
695
+ if (!toolInvocations.length) continue;
622
696
  for (const invocation of toolInvocations) if (invocation.state === "result" && invocation.result !== void 0) results.push({
623
697
  toolName: invocation.toolName,
624
698
  toolCallId: invocation.toolCallId || "",
@@ -1000,6 +1074,6 @@ function analyzeToolFailures(trajectory, options = {}) {
1000
1074
  };
1001
1075
  }
1002
1076
  //#endregion
1003
- export { analyzeToolFailures, checkTrajectoryBlacklist, checkTrajectoryEfficiency, compareTrajectories, createAgentTestRun, createTestMessage, createTestRun, createToolInvocation, createTrajectoryTestRun, extractAgentResponseMessages, extractInputMessages, extractToolCalls, extractToolResults, extractTrajectory, getAssistantMessageFromRunOutput, getCombinedSystemPrompt, getConversationHistoryFromRunInput, getReasoningFromRunOutput, getSystemMessagesFromRunInput, getTextContentFromMastraDBMessage, getUserMessageFromRunInput, isCloserTo, isScorerRunInputForAgent, isScorerRunOutputForAgent, roundToTwoDecimals };
1077
+ export { analyzeToolFailures, checkTrajectoryBlacklist, checkTrajectoryEfficiency, compareTrajectories, createAgentTestRun, createTestMessage, createTestRun, createToolInvocation, createTrajectoryTestRun, extractAgentResponseMessages, extractInputMessages, extractToolCalls, extractToolResults, extractTrajectory, getAssistantMessageFromRunOutput, getCombinedSystemPrompt, getConversationHistoryFromRunInput, getReasoningFromRunOutput, getSystemMessagesFromRunInput, getTextContentFromMastraDBMessage, getUserMessageFromRunInput, isCloserTo, isScorerRunInputForAgent, isScorerRunOutputForAgent, mergeToolInvocations, roundToTwoDecimals };
1004
1078
 
1005
1079
  //# sourceMappingURL=utils.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"utils.js","names":[],"sources":["../../src/scorers/utils.ts"],"sourcesContent":["import type { MastraDBMessage } from '@mastra/core/agent';\nimport type {\n ExpectedStep,\n ScorerRunInputForAgent,\n ScorerRunOutputForAgent,\n ScoringInput,\n TrajectoryExpectation,\n TrajectoryStep,\n Trajectory,\n} from '@mastra/core/evals';\nimport { RequestContext } from '@mastra/core/request-context';\n\nexport type ScorerRunInputForLLMJudge =\n | ScorerRunInputForAgent\n | string\n | {\n inputMessages?: unknown[];\n messages?: unknown[];\n prompt?: string;\n text?: string;\n content?: unknown;\n input?: unknown;\n user?: unknown;\n [key: string]: unknown;\n };\n\nexport type ScorerRunOutputForLLMJudge =\n | ScorerRunOutputForAgent\n | string\n | unknown[]\n | {\n text?: string;\n content?: unknown;\n role?: string;\n [key: string]: unknown;\n };\n\n/**\n * Extracts text content from a MastraDBMessage or ModelMessage-like object.\n *\n * @param message - The message to extract text from\n * @returns The extracted text content, or an empty string if no text is found\n *\n * @example\n * ```ts\n * const message: MastraDBMessage = {\n * id: 'msg-1',\n * role: 'assistant',\n * content: { format: 2, parts: [{ type: 'text', text: 'Hello!' }] },\n * createdAt: new Date(),\n * };\n * const text = getTextContentFromMastraDBMessage(message); // 'Hello!'\n * ```\n */\nexport function getTextContentFromMastraDBMessage(message: MastraDBMessage): string {\n const content = message.content as any;\n\n if (typeof content === 'string') {\n return content;\n }\n if (Array.isArray(content)) {\n const textParts = content.filter(p => p.type === 'text');\n return textParts.length > 0 ? textParts[textParts.length - 1]?.text || '' : '';\n }\n if (typeof content?.content === 'string' && content.content !== '') {\n return content.content;\n }\n if (typeof content?.text === 'string' && content.text !== '') {\n return content.text;\n }\n if (content?.parts && Array.isArray(content.parts)) {\n // Return only the last text part like AI SDK does\n const textParts = content.parts.filter((p: any) => p.type === 'text');\n return textParts.length > 0 ? textParts[textParts.length - 1]?.text || '' : '';\n }\n return '';\n}\n\nconst isRecord = (value: unknown): value is Record<string, any> => {\n return typeof value === 'object' && value !== null;\n};\n\nconst getTextFromValue = (value: unknown): string | undefined => {\n if (typeof value === 'string') return value === '' ? undefined : value;\n if (Array.isArray(value)) {\n const textParts = value\n .filter(part => isRecord(part) && part.type === 'text' && typeof part.text === 'string')\n .map(part => part.text);\n return textParts.length > 0 ? textParts[textParts.length - 1] : undefined;\n }\n if (!isRecord(value)) return undefined;\n\n const fromParts = Array.isArray(value.parts) ? getTextFromValue(value.parts) : undefined;\n\n return (\n getTextFromValue(value.content) ??\n (typeof value.text === 'string' && value.text !== '' ? value.text : undefined) ??\n (typeof value.body === 'string' && value.body !== '' ? value.body : undefined) ??\n fromParts\n );\n};\n\nexport const isScorerRunInputForAgent = (input: unknown): input is ScorerRunInputForAgent => {\n return (\n isRecord(input) &&\n Array.isArray(input.inputMessages) &&\n Array.isArray(input.rememberedMessages) &&\n Array.isArray(input.systemMessages) &&\n isRecord(input.taggedSystemMessages)\n );\n};\n\nconst isMastraDBMessageLike = (message: unknown): message is MastraDBMessage => {\n return (\n isRecord(message) &&\n typeof message.id === 'string' &&\n typeof message.role === 'string' &&\n 'content' in message &&\n 'createdAt' in message\n );\n};\n\nexport const isScorerRunOutputForAgent = (output: unknown): output is ScorerRunOutputForAgent => {\n return Array.isArray(output) && output.every(isMastraDBMessageLike);\n};\n\n/**\n * Resolves the effective role of a message, accounting for agent signal messages.\n *\n * Messages delivered through the agent subscription / signal API are persisted with\n * `role: 'signal'` and carry their semantic role (e.g. `user`) on `type` and on\n * `content.metadata.signal.{type,tagName}`. Treat those as their underlying role so\n * helpers like `getUserMessageFromRunInput` can find them.\n */\nconst getEffectiveMessageRole = (message: Record<string, any>): string | undefined => {\n if (message.role !== 'signal') return typeof message.role === 'string' ? message.role : undefined;\n\n const signalMeta =\n isRecord(message.content) && isRecord(message.content.metadata) ? message.content.metadata.signal : undefined;\n\n const tagName = isRecord(signalMeta) && typeof signalMeta.tagName === 'string' ? signalMeta.tagName : undefined;\n const signalType = isRecord(signalMeta) && typeof signalMeta.type === 'string' ? signalMeta.type : undefined;\n const topLevelType = typeof message.type === 'string' ? message.type : undefined;\n\n return tagName ?? signalType ?? topLevelType;\n};\n\nconst getTextFromMessages = (messages: unknown, role: string): string | undefined => {\n if (!Array.isArray(messages)) return undefined;\n\n const message = messages.find(message => isRecord(message) && getEffectiveMessageRole(message) === role);\n return message ? getTextFromValue(message) : undefined;\n};\n\n/**\n * Rounds a number to two decimal places.\n *\n * Uses `Number.EPSILON` to handle floating-point precision issues.\n *\n * @param num - The number to round\n * @returns The number rounded to two decimal places\n *\n * @example\n * ```ts\n * roundToTwoDecimals(0.1 + 0.2); // 0.3\n * roundToTwoDecimals(1.005); // 1.01\n * ```\n */\nexport const roundToTwoDecimals = (num: number) => {\n return Math.round((num + Number.EPSILON) * 100) / 100;\n};\n\n/**\n * Determines if a value is closer to the first target than the second.\n *\n * @param value - The value to compare\n * @param target1 - The first target value\n * @param target2 - The second target value\n * @returns `true` if `value` is closer to `target1` than `target2`\n *\n * @example\n * ```ts\n * isCloserTo(0.6, 1, 0); // true (0.6 is closer to 1)\n * isCloserTo(0.3, 1, 0); // false (0.3 is closer to 0)\n * ```\n */\nexport function isCloserTo(value: number, target1: number, target2: number): boolean {\n return Math.abs(value - target1) < Math.abs(value - target2);\n}\n\n/**\n * Represents a test case for scorer evaluation.\n */\nexport type TestCase = {\n /** The input text to evaluate */\n input: string;\n /** The output text to evaluate */\n output: string;\n /** The expected result of the evaluation */\n expectedResult: {\n /** The expected score */\n score: number;\n /** The optional expected reason */\n reason?: string;\n };\n};\n\n/**\n * Represents a test case with additional context for scorer evaluation.\n */\nexport type TestCaseWithContext = TestCase & {\n /** Additional context strings for the evaluation */\n context: string[];\n};\n\n/**\n * Creates a scoring input object for testing purposes.\n *\n * @param input - The user input text\n * @param output - The assistant output text\n * @param additionalContext - Optional additional context data\n * @param requestContext - Optional request context data\n * @returns A ScoringInput object ready for use in scorer tests\n *\n * @example\n * ```ts\n * const run = createTestRun(\n * 'What is 2+2?',\n * 'The answer is 4.',\n * { topic: 'math' }\n * );\n * ```\n */\nexport const createTestRun = (\n input: string,\n output: string,\n additionalContext?: Record<string, any>,\n requestContext?: Record<string, any>,\n): ScoringInput => {\n return {\n input: [{ role: 'user', content: input }],\n output: { role: 'assistant', text: output },\n additionalContext: additionalContext ?? {},\n requestContext: requestContext ?? {},\n };\n};\n\n/**\n * Extracts the user message text from a scorer run input.\n *\n * Accepts the agent shape (`{ inputMessages }`), `ModelMessage[]`\n * (`{ messages }`), workflow input (`{ prompt }`), and a bare string.\n *\n * @param input - The scorer run input\n * @returns The user message text, or `undefined` if none can be extracted\n *\n * @example\n * ```ts\n * const scorer = createScorer({ ... })\n * .preprocess(({ run }) => {\n * const userText = getUserMessageFromRunInput(run.input);\n * return { userText };\n * });\n * ```\n */\nexport const getUserMessageFromRunInput = (input?: unknown): string | undefined => {\n if (typeof input === 'string') return input;\n if (!isRecord(input)) return undefined;\n\n return (\n getTextFromMessages(input.inputMessages, 'user') ??\n getTextFromMessages(input.messages, 'user') ??\n (typeof input.prompt === 'string' ? input.prompt : undefined) ??\n (typeof input.text === 'string' ? input.text : undefined) ??\n getTextFromValue(input.content) ??\n getTextFromValue(input.input) ??\n getTextFromValue(input.user)\n );\n};\n\nconst DEFAULT_CONVERSATION_HISTORY_MESSAGES = 10;\n\n/**\n * Renders the remembered conversation history from an agent scorer run input as a transcript.\n *\n * Only agent run inputs carry `rememberedMessages`; any other input shape (a bare string,\n * a plain `{ prompt }` object, `ModelMessage[]`) returns `undefined` so callers can safely\n * fall back to single-turn behaviour.\n *\n * @param input - The scorer run input\n * @param options.maxMessages - How many of the most recent remembered messages to keep (default 10)\n * @returns A newline separated `role: text` transcript, or `undefined` if there is no history\n *\n * @example\n * ```ts\n * const scorer = createScorer({ ... })\n * .preprocess(({ run }) => {\n * const history = getConversationHistoryFromRunInput(run.input, { maxMessages: 4 });\n * return { history };\n * });\n * ```\n */\nexport const getConversationHistoryFromRunInput = (\n input?: unknown,\n options?: { maxMessages?: number },\n): string | undefined => {\n if (!isScorerRunInputForAgent(input)) return undefined;\n\n const maxMessages = options?.maxMessages ?? DEFAULT_CONVERSATION_HISTORY_MESSAGES;\n if (maxMessages <= 0) return undefined;\n\n const lines = input.rememberedMessages\n .slice(-maxMessages)\n .map(message => {\n if (!isRecord(message)) return undefined;\n\n const role = getEffectiveMessageRole(message);\n const text = getTextContentFromMastraDBMessage(message as MastraDBMessage).trim();\n if (!role || !text) return undefined;\n\n return `${role}: ${text}`;\n })\n .filter((line): line is string => Boolean(line));\n\n return lines.length > 0 ? lines.join('\\n') : undefined;\n};\n\n/**\n * Extracts all system messages from a scorer run input.\n *\n * Collects text from both standard system messages and tagged system messages\n * (specialized system prompts like memory instructions).\n *\n * @param input - The scorer run input containing system messages\n * @returns An array of system message strings\n *\n * @example\n * ```ts\n * const scorer = createScorer({ ... })\n * .preprocess(({ run }) => {\n * const systemMessages = getSystemMessagesFromRunInput(run.input);\n * return { systemPrompt: systemMessages.join('\\n') };\n * });\n * ```\n */\nexport const getSystemMessagesFromRunInput = (input?: unknown): string[] => {\n const systemMessages: string[] = [];\n if (!isRecord(input)) return systemMessages;\n\n // Add standard system messages\n if (Array.isArray(input.systemMessages)) {\n systemMessages.push(\n ...input.systemMessages\n .map(msg => {\n // Handle different content types - extract text if it's an array of parts\n if (typeof msg.content === 'string') {\n return msg.content;\n } else if (Array.isArray(msg.content)) {\n // Extract text from parts array\n return msg.content\n .filter((part: any) => part.type === 'text')\n .map((part: any) => part.text || '')\n .join(' ');\n }\n return '';\n })\n .filter(content => content),\n );\n }\n\n const addSystemMessages = (messages: unknown) => {\n if (!Array.isArray(messages)) return;\n\n systemMessages.push(\n ...messages\n .filter(message => isRecord(message) && message.role === 'system')\n .map(message => getTextFromValue(message))\n .filter((content): content is string => Boolean(content)),\n );\n };\n\n addSystemMessages(input.inputMessages);\n addSystemMessages(input.messages);\n\n // Add tagged system messages (these are specialized system prompts)\n if (isRecord(input.taggedSystemMessages)) {\n Object.values(input.taggedSystemMessages).forEach(messages => {\n if (!Array.isArray(messages)) return;\n messages.forEach(msg => {\n const content = getTextFromValue(msg);\n if (content) {\n systemMessages.push(content);\n }\n });\n });\n }\n\n return systemMessages;\n};\n\n/**\n * Combines all system messages into a single prompt string.\n *\n * Joins all system messages (standard and tagged) with double newlines.\n *\n * @param input - The scorer run input containing system messages\n * @returns A combined system prompt string\n *\n * @example\n * ```ts\n * const scorer = createScorer({ ... })\n * .preprocess(({ run }) => {\n * const systemPrompt = getCombinedSystemPrompt(run.input);\n * return { systemPrompt };\n * });\n * ```\n */\nexport const getCombinedSystemPrompt = (input?: unknown): string => {\n const systemMessages = getSystemMessagesFromRunInput(input);\n return systemMessages.join('\\n\\n');\n};\n\n/**\n * Extracts the assistant message text from a scorer run output.\n *\n * Accepts the agent shape (`MastraDBMessage[]` / `ModelMessage[]`), workflow\n * output (`{ text }`), task output (`{ content }`), a single assistant message\n * object, and a bare string.\n *\n * For array outputs the final assistant message is used, so multi-step agent\n * responses are scored on the last response rather than an intermediate one.\n *\n * @param output - The scorer run output\n * @returns The assistant message text, or `undefined` if none can be extracted\n *\n * @example\n * ```ts\n * const scorer = createScorer({ ... })\n * .preprocess(({ run }) => {\n * const response = getAssistantMessageFromRunOutput(run.output);\n * return { response };\n * });\n * ```\n */\nexport const getAssistantMessageFromRunOutput = (output?: unknown) => {\n if (typeof output === 'string') return output;\n if (Array.isArray(output)) {\n const assistantMessages = output.filter(\n message => isRecord(message) && getEffectiveMessageRole(message) === 'assistant',\n );\n // Prefer the last assistant message that carries text; a trailing assistant\n // message may hold only tool-call or data parts.\n for (let i = assistantMessages.length - 1; i >= 0; i--) {\n const text = getTextFromValue(assistantMessages[i]);\n if (text) return text;\n }\n return undefined;\n }\n if (!isRecord(output)) return undefined;\n\n const isAssistantOutput = output.role === undefined || output.role === 'assistant';\n\n if (isAssistantOutput && typeof output.text === 'string') return output.text;\n if (isAssistantOutput && typeof output.content === 'string') return output.content;\n if (isAssistantOutput && (isRecord(output.content) || Array.isArray(output.content))) {\n return (\n getTextContentFromMastraDBMessage(output as MastraDBMessage) ||\n getTextContentFromMastraDBMessage(output.content as MastraDBMessage) ||\n undefined\n );\n }\n if (output.role === 'assistant') return getTextContentFromMastraDBMessage(output as MastraDBMessage) || undefined;\n\n return undefined;\n};\n\n/**\n * Extracts reasoning text from a scorer run output.\n *\n * This function extracts reasoning content from assistant messages, which is\n * produced by reasoning models like `deepseek-reasoner`. The reasoning can be\n * stored in two places:\n * 1. `content.reasoning` - a string field on the message content\n * 2. `content.parts` - as parts with `type: 'reasoning'` containing `details`\n *\n * @param output - The scorer run output (array of MastraDBMessage)\n * @returns The reasoning text, or `undefined` if no reasoning is present\n *\n * @example\n * ```ts\n * const reasoningScorer = createScorer({\n * id: 'reasoning-scorer',\n * name: 'Reasoning Quality',\n * description: 'Evaluates the quality of model reasoning',\n * type: 'agent',\n * })\n * .preprocess(({ run }) => {\n * const reasoning = getReasoningFromRunOutput(run.output);\n * const response = getAssistantMessageFromRunOutput(run.output);\n * return { reasoning, response };\n * })\n * .generateScore(({ results }) => {\n * // Score based on reasoning quality\n * return results.preprocessStepResult?.reasoning ? 1 : 0;\n * });\n * ```\n */\nexport const getReasoningFromRunOutput = (output?: ScorerRunOutputForAgent): string | undefined => {\n if (!output) return undefined;\n\n const message = output.find(({ role }) => role === 'assistant');\n if (!message) return undefined;\n\n // Check for reasoning in content.reasoning (string format)\n if (message.content.reasoning) {\n return message.content.reasoning;\n }\n\n // Check for reasoning in parts with type 'reasoning'\n // Reasoning models store reasoning in parts as { type: 'reasoning', details: [{ type: 'text', text: '...' }] }\n const reasoningParts = message.content.parts?.filter((p: any) => p.type === 'reasoning');\n if (reasoningParts && reasoningParts.length > 0) {\n const reasoningTexts = reasoningParts\n .map((p: any) => {\n // The reasoning text can be in p.reasoning or in p.details[].text\n if (p.details && Array.isArray(p.details)) {\n return p.details\n .filter((d: any) => d.type === 'text')\n .map((d: any) => d.text)\n .join('');\n }\n return p.reasoning || '';\n })\n .filter(Boolean);\n\n return reasoningTexts.length > 0 ? reasoningTexts.join('\\n') : undefined;\n }\n\n return undefined;\n};\n\n/**\n * Creates a tool invocation object for testing purposes.\n *\n * @param options - The tool invocation configuration\n * @param options.toolCallId - Unique identifier for the tool call\n * @param options.toolName - Name of the tool being called\n * @param options.args - Arguments passed to the tool\n * @param options.result - Result returned by the tool\n * @param options.state - State of the invocation (default: 'result')\n * @returns A tool invocation object\n *\n * @example\n * ```ts\n * const invocation = createToolInvocation({\n * toolCallId: 'call-123',\n * toolName: 'weatherTool',\n * args: { location: 'London' },\n * result: { temperature: 20, condition: 'sunny' },\n * });\n * ```\n */\nexport const createToolInvocation = ({\n toolCallId,\n toolName,\n args,\n result,\n state = 'result',\n}: {\n toolCallId: string;\n toolName: string;\n args: Record<string, any>;\n result: Record<string, any>;\n state?: 'call' | 'partial-call' | 'result';\n}): { toolCallId: string; toolName: string; args: Record<string, any>; result: Record<string, any>; state: string } => {\n return {\n toolCallId,\n toolName,\n args,\n result,\n state,\n };\n};\n\n/**\n * Creates a MastraDBMessage object for testing purposes.\n *\n * Supports optional tool invocations for testing tool call scenarios.\n *\n * @param options - The message configuration\n * @param options.content - The text content of the message\n * @param options.role - The role of the message sender ('user', 'assistant', or 'system')\n * @param options.id - Optional message ID (default: 'test-message')\n * @param options.toolInvocations - Optional array of tool invocations\n * @returns A MastraDBMessage object\n *\n * @example\n * ```ts\n * const message = createTestMessage({\n * content: 'Hello, how can I help?',\n * role: 'assistant',\n * });\n *\n * // With tool invocations\n * const messageWithTools = createTestMessage({\n * content: 'Let me check the weather.',\n * role: 'assistant',\n * toolInvocations: [{\n * toolCallId: 'call-1',\n * toolName: 'weatherTool',\n * args: { location: 'Paris' },\n * result: { temp: 22 },\n * state: 'result',\n * }],\n * });\n * ```\n */\nexport function createTestMessage({\n content,\n role,\n id = 'test-message',\n toolInvocations = [],\n}: {\n content: string;\n role: 'user' | 'assistant' | 'system';\n id?: string;\n toolInvocations?: Array<{\n toolCallId: string;\n toolName: string;\n args: Record<string, any>;\n result: Record<string, any>;\n state: any;\n }>;\n}): MastraDBMessage {\n return {\n id,\n role,\n content: {\n format: 2,\n parts: [{ type: 'text', text: content }],\n content,\n ...(toolInvocations.length > 0 && {\n toolInvocations: toolInvocations.map(ti => ({\n toolCallId: ti.toolCallId,\n toolName: ti.toolName,\n args: ti.args,\n result: ti.result,\n state: ti.state,\n })),\n }),\n },\n createdAt: new Date(),\n };\n}\n\n/**\n * Creates a complete agent test run object for testing scorers.\n *\n * Provides a convenient way to construct the full run object that scorers receive,\n * including input messages, output, system messages, and request context.\n *\n * @param options - The test run configuration\n * @param options.inputMessages - Array of input messages (default: [])\n * @param options.output - The output messages (required)\n * @param options.rememberedMessages - Array of remembered messages from memory (default: [])\n * @param options.systemMessages - Array of system messages (default: [])\n * @param options.taggedSystemMessages - Tagged system messages map (default: {})\n * @param options.requestContext - Request context (default: new RequestContext())\n * @param options.runId - Unique run ID (default: random UUID)\n * @returns A complete test run object\n *\n * @example\n * ```ts\n * const testRun = createAgentTestRun({\n * inputMessages: [createTestMessage({ content: 'Hello', role: 'user' })],\n * output: [createTestMessage({ content: 'Hi there!', role: 'assistant' })],\n * });\n *\n * const result = await scorer.run({\n * input: testRun.input,\n * output: testRun.output,\n * });\n * ```\n */\nexport const createAgentTestRun = ({\n inputMessages = [],\n output,\n rememberedMessages = [],\n systemMessages = [],\n taggedSystemMessages = {},\n requestContext = new RequestContext(),\n runId = crypto.randomUUID(),\n}: {\n inputMessages?: ScorerRunInputForAgent['inputMessages'];\n output: ScorerRunOutputForAgent;\n rememberedMessages?: ScorerRunInputForAgent['rememberedMessages'];\n systemMessages?: ScorerRunInputForAgent['systemMessages'];\n taggedSystemMessages?: ScorerRunInputForAgent['taggedSystemMessages'];\n requestContext?: RequestContext;\n runId?: string;\n}): {\n input: ScorerRunInputForAgent;\n output: ScorerRunOutputForAgent;\n requestContext: RequestContext;\n runId: string;\n} => {\n return {\n input: {\n inputMessages,\n rememberedMessages,\n systemMessages,\n taggedSystemMessages,\n },\n output,\n requestContext,\n runId,\n };\n};\n\n/**\n * Creates a test run for trajectory scorers where `output` is a `Trajectory`\n * (pre-extracted by the `runEvals` pipeline).\n *\n * @example\n * ```ts\n * const testRun = createTrajectoryTestRun({\n * inputMessages: [createTestMessage({ content: 'Do X', role: 'user', id: 'u1' })],\n * trajectory: {\n * steps: [\n * { stepType: 'tool_call', name: 'search', toolArgs: { q: 'test' } },\n * ],\n * },\n * });\n * ```\n */\nexport const createTrajectoryTestRun = ({\n inputMessages = [],\n trajectory,\n rememberedMessages = [],\n systemMessages = [],\n taggedSystemMessages = {},\n requestContext = new RequestContext(),\n runId = crypto.randomUUID(),\n expectedTrajectory,\n}: {\n inputMessages?: ScorerRunInputForAgent['inputMessages'];\n trajectory: Trajectory;\n rememberedMessages?: ScorerRunInputForAgent['rememberedMessages'];\n systemMessages?: ScorerRunInputForAgent['systemMessages'];\n taggedSystemMessages?: ScorerRunInputForAgent['taggedSystemMessages'];\n requestContext?: RequestContext;\n runId?: string;\n expectedTrajectory?: TrajectoryExpectation;\n}): {\n input: ScorerRunInputForAgent;\n output: Trajectory;\n requestContext: RequestContext;\n runId: string;\n expectedTrajectory?: TrajectoryExpectation;\n} => {\n return {\n input: {\n inputMessages,\n rememberedMessages,\n systemMessages,\n taggedSystemMessages,\n },\n output: trajectory,\n expectedTrajectory,\n requestContext,\n runId,\n };\n};\n\n/**\n * Information about a tool call extracted from scorer output.\n */\nexport type ToolCallInfo = {\n /** Name of the tool that was called */\n toolName: string;\n /** Unique identifier for the tool call */\n toolCallId: string;\n /** Index of the message containing this tool call */\n messageIndex: number;\n /** Index of the invocation within the message's tool invocations */\n invocationIndex: number;\n};\n\n/**\n * Extracts all tool calls from a scorer run output.\n *\n * Iterates through all messages and their tool invocations to collect\n * information about tools that were called (with state 'result' or 'call').\n *\n * @param output - The scorer run output (array of MastraDBMessage)\n * @returns An object containing tool names and detailed tool call info\n *\n * @example\n * ```ts\n * const scorer = createScorer({ ... })\n * .preprocess(({ run }) => {\n * const { tools, toolCallInfos } = extractToolCalls(run.output);\n * return {\n * toolsUsed: tools,\n * toolCount: tools.length,\n * };\n * });\n * ```\n */\nexport function extractToolCalls(output: ScorerRunOutputForAgent): { tools: string[]; toolCallInfos: ToolCallInfo[] } {\n const toolCalls: string[] = [];\n const toolCallInfos: ToolCallInfo[] = [];\n\n for (let messageIndex = 0; messageIndex < output.length; messageIndex++) {\n const message = output[messageIndex];\n // Prefer the legacy toolInvocations array when present; fall back to\n // V2 content.parts for messages that only store tool calls there.\n const legacy = message?.content?.toolInvocations;\n const fromParts = legacy\n ? undefined\n : message?.content?.parts\n ?.filter((p): p is Extract<typeof p, { type: 'tool-invocation' }> => p.type === 'tool-invocation')\n .map(p => p.toolInvocation);\n const toolInvocations = legacy ?? fromParts;\n\n if (!toolInvocations?.length) continue;\n\n for (let invocationIndex = 0; invocationIndex < toolInvocations.length; invocationIndex++) {\n const invocation = toolInvocations[invocationIndex];\n if (invocation && invocation.toolName && (invocation.state === 'result' || invocation.state === 'call')) {\n toolCalls.push(invocation.toolName);\n toolCallInfos.push({\n toolName: invocation.toolName,\n toolCallId: invocation.toolCallId || `${messageIndex}-${invocationIndex}`,\n messageIndex,\n invocationIndex,\n });\n }\n }\n }\n\n return { tools: toolCalls, toolCallInfos };\n}\n\n/**\n * Extracts text content from all input messages.\n *\n * @param runInput - The scorer run input\n * @returns An array of text strings from each input message\n *\n * @example\n * ```ts\n * const scorer = createScorer({ ... })\n * .preprocess(({ run }) => {\n * const messages = extractInputMessages(run.input);\n * return { allUserMessages: messages.join('\\n') };\n * });\n * ```\n */\nexport const extractInputMessages = (runInput: ScorerRunInputForAgent | undefined): string[] => {\n return runInput?.inputMessages?.map(msg => getTextContentFromMastraDBMessage(msg)) || [];\n};\n\n/**\n * Extracts text content from all assistant response messages.\n *\n * Filters for messages with role 'assistant' and extracts their text content.\n *\n * @param runOutput - The scorer run output (array of MastraDBMessage)\n * @returns An array of text strings from each assistant message\n *\n * @example\n * ```ts\n * const scorer = createScorer({ ... })\n * .preprocess(({ run }) => {\n * const responses = extractAgentResponseMessages(run.output);\n * return { allResponses: responses.join('\\n') };\n * });\n * ```\n */\nexport const extractAgentResponseMessages = (runOutput: ScorerRunOutputForAgent): string[] => {\n return runOutput.filter(msg => msg.role === 'assistant').map(msg => getTextContentFromMastraDBMessage(msg));\n};\n\n/**\n * Information about a tool result extracted from scorer output.\n */\nexport type ToolResultInfo = {\n /** Name of the tool that was called */\n toolName: string;\n /** Unique identifier for the tool call */\n toolCallId: string;\n /** Arguments passed to the tool */\n args: Record<string, any>;\n /** Result returned by the tool */\n result: any;\n};\n\n/**\n * Extracts tool results from a scorer run output.\n *\n * Returns structured objects that can be used with the hallucination scorer's\n * `getContext` hook or for other scorer logic.\n *\n * @param output - The scorer run output (array of MastraDBMessage)\n * @returns An array of ToolResultInfo objects\n *\n * @example\n * ```ts\n * import { extractToolResults } from '@mastra/evals/scorers';\n * import { createHallucinationScorer } from '@mastra/evals/scorers/prebuilt';\n *\n * const scorer = createHallucinationScorer({\n * model: openai('gpt-4o'),\n * options: {\n * getContext: (run) => {\n * const toolResults = extractToolResults(run.output);\n * return toolResults.map(t => JSON.stringify({ tool: t.toolName, result: t.result }));\n * },\n * },\n * });\n * ```\n */\nexport function extractToolResults(output: ScorerRunOutputForAgent): ToolResultInfo[] {\n const results: ToolResultInfo[] = [];\n\n for (const message of output) {\n // Prefer the legacy toolInvocations array when present; fall back to\n // V2 content.parts for messages that only store tool calls there.\n const legacy = message?.content?.toolInvocations;\n const fromParts = legacy\n ? undefined\n : message?.content?.parts\n ?.filter((p): p is Extract<typeof p, { type: 'tool-invocation' }> => p.type === 'tool-invocation')\n .map(p => p.toolInvocation);\n const toolInvocations = legacy ?? fromParts;\n\n if (!toolInvocations?.length) continue;\n\n for (const invocation of toolInvocations) {\n if (invocation.state === 'result' && invocation.result !== undefined) {\n results.push({\n toolName: invocation.toolName,\n toolCallId: invocation.toolCallId || '',\n args: invocation.args || {},\n result: invocation.result,\n });\n }\n }\n }\n\n return results;\n}\n\n// Re-export extractTrajectory from core — it's called automatically by runEvals\n// for trajectory scorers, but users may still want it for custom use cases.\nexport { extractTrajectory } from '@mastra/core/evals';\n\n/**\n * Compares two trajectories and returns detailed comparison results.\n *\n * This is the core comparison logic used by trajectory scorers. It supports\n * strict and non-strict ordering, optional step data comparison, and loop detection.\n *\n * @param actual - The trajectory the agent actually took\n * @param expected - The expected trajectory to compare against\n * @param options - Comparison configuration options\n * @returns Detailed comparison results including match scores and diagnostics\n *\n * @example\n * ```ts\n * const result = compareTrajectories(\n * { steps: [{ stepType: 'tool_call', name: 'search' }, { stepType: 'tool_call', name: 'summarize' }] },\n * { steps: [{ stepType: 'tool_call', name: 'search' }, { stepType: 'tool_call', name: 'summarize' }] },\n * { ordering: 'strict' }\n * );\n * // result.score = 1.0\n * ```\n */\nexport function compareTrajectories(\n actual: Trajectory,\n expected: Trajectory | { steps: ExpectedStep[] },\n options: {\n ordering?: 'strict' | 'relaxed' | 'unordered';\n allowRepeatedSteps?: boolean;\n } = {},\n): TrajectoryComparisonResult {\n const { allowRepeatedSteps = true, ordering = 'relaxed' } = options;\n\n // Normalize expected to ExpectedStep[]. TrajectoryStep and ExpectedStep share\n // the same field names, so TrajectoryStep[] can be used directly as ExpectedStep[].\n // The only structural difference is `children` (TrajectoryStep[] vs TrajectoryExpectation),\n // but compareTrajectories doesn't recurse into children.\n const normalizedExpected: { steps: ExpectedStep[] } = {\n steps: expected.steps as ExpectedStep[],\n };\n\n if (normalizedExpected.steps.length === 0) {\n return {\n score: actual.steps.length === 0 ? 1 : 0,\n matchedSteps: 0,\n totalExpectedSteps: 0,\n totalActualSteps: actual.steps.length,\n missingSteps: [],\n extraSteps: actual.steps.map((s: TrajectoryStep) => s.name),\n outOfOrderSteps: [],\n repeatedSteps: [],\n };\n }\n\n const actualNames = actual.steps.map((s: TrajectoryStep) => s.name);\n\n // Detect repeated steps\n const nameCounts = new Map<string, number>();\n for (const name of actualNames) {\n nameCounts.set(name, (nameCounts.get(name) || 0) + 1);\n }\n const repeatedSteps = [...nameCounts.entries()]\n .filter(([_, count]: [string, number]) => count > 1)\n .map(([name]: [string, number]) => name);\n\n if (ordering === 'strict') {\n return compareStrictOrder(actual, normalizedExpected, { allowRepeatedSteps, repeatedSteps });\n }\n\n if (ordering === 'unordered') {\n return compareUnorderedPresence(actual, normalizedExpected, { allowRepeatedSteps, repeatedSteps });\n }\n\n return compareRelaxedOrder(actual, normalizedExpected, { allowRepeatedSteps, repeatedSteps });\n}\n\n/**\n * Result of comparing two trajectories.\n */\nexport type TrajectoryComparisonResult = {\n /** Overall match score from 0 to 1 */\n score: number;\n /** Number of expected steps that were matched */\n matchedSteps: number;\n /** Total number of expected steps */\n totalExpectedSteps: number;\n /** Total number of actual steps taken */\n totalActualSteps: number;\n /** Expected steps that were not found in the actual trajectory */\n missingSteps: string[];\n /** Actual steps that were not in the expected trajectory */\n extraSteps: string[];\n /** Steps that appear but not in the expected position */\n outOfOrderSteps: string[];\n /** Steps that were repeated (appeared more than once) */\n repeatedSteps: string[];\n};\n\nfunction compareStrictOrder(\n actual: Trajectory,\n expected: { steps: ExpectedStep[] },\n opts: { allowRepeatedSteps: boolean; repeatedSteps: string[] },\n): TrajectoryComparisonResult {\n const actualNames: string[] = actual.steps.map((s: TrajectoryStep) => s.name);\n const expectedNames: string[] = expected.steps.map((s: ExpectedStep) => s.name);\n\n // Strict: exact same sequence\n let matchedSteps = 0;\n const outOfOrderSteps: string[] = [];\n const matchedExpectedIndices = new Set<number>();\n const maxLen = Math.max(actualNames.length, expectedNames.length);\n\n for (let i = 0; i < maxLen; i++) {\n const actualName = actualNames[i];\n const expectedName = expectedNames[i];\n if (actualName === expectedName) {\n if (actual.steps[i] && expected.steps[i]) {\n if (expectedStepMatches(actual.steps[i]!, expected.steps[i]!)) {\n matchedSteps++;\n matchedExpectedIndices.add(i);\n }\n } else {\n matchedSteps++;\n matchedExpectedIndices.add(i);\n }\n } else if (actualName && expectedNames.includes(actualName)) {\n outOfOrderSteps.push(actualName);\n }\n }\n\n // Missing steps = expected steps that were not matched (accounts for stepType/data mismatches)\n const missingSteps: string[] = expectedNames.filter((_: string, i: number) => !matchedExpectedIndices.has(i));\n const extraSteps: string[] = actualNames.filter((name: string) => !expectedNames.includes(name));\n\n let score = matchedSteps / expected.steps.length;\n\n // Penalize extra steps in strict mode\n if (actualNames.length > expectedNames.length) {\n const extraPenalty = (actualNames.length - expectedNames.length) / expectedNames.length;\n score = Math.max(0, score - extraPenalty * 0.5);\n }\n\n // Penalize repeated steps if not allowed\n if (!opts.allowRepeatedSteps && opts.repeatedSteps.length > 0) {\n score = Math.max(0, score - opts.repeatedSteps.length * 0.1);\n }\n\n return {\n score: roundToTwoDecimals(Math.max(0, Math.min(1, score))),\n matchedSteps,\n totalExpectedSteps: expected.steps.length,\n totalActualSteps: actual.steps.length,\n missingSteps,\n extraSteps,\n outOfOrderSteps,\n repeatedSteps: opts.repeatedSteps,\n };\n}\n\nfunction compareRelaxedOrder(\n actual: Trajectory,\n expected: { steps: ExpectedStep[] },\n opts: { allowRepeatedSteps: boolean; repeatedSteps: string[] },\n): TrajectoryComparisonResult {\n const actualNames: string[] = actual.steps.map((s: TrajectoryStep) => s.name);\n const expectedNames: string[] = expected.steps.map((s: ExpectedStep) => s.name);\n\n // Relaxed: expected steps must appear in order but extra steps are allowed\n let matchedSteps = 0;\n let lastMatchedIndex = -1;\n const outOfOrderSteps: string[] = [];\n const matchedExpectedIndices = new Set<number>();\n\n for (let i = 0; i < expectedNames.length; i++) {\n const expectedName = expectedNames[i];\n let found = false;\n\n for (let j = lastMatchedIndex + 1; j < actualNames.length; j++) {\n if (actualNames[j] === expectedName) {\n if (actual.steps[j] && expected.steps[i]) {\n if (expectedStepMatches(actual.steps[j]!, expected.steps[i]!)) {\n matchedSteps++;\n lastMatchedIndex = j;\n matchedExpectedIndices.add(i);\n found = true;\n break;\n }\n } else {\n matchedSteps++;\n lastMatchedIndex = j;\n matchedExpectedIndices.add(i);\n found = true;\n break;\n }\n }\n }\n\n if (!found) {\n // Check if the step exists but is out of order\n if (actualNames.includes(expectedName!)) {\n outOfOrderSteps.push(expectedName!);\n }\n }\n }\n\n // Missing steps = expected steps that were not matched (by name + stepType + data, not just name)\n const missingSteps = expectedNames.filter((_, i) => !matchedExpectedIndices.has(i));\n const expectedSet = new Set(expectedNames);\n const extraSteps = actualNames.filter(name => !expectedSet.has(name));\n\n let score = matchedSteps / expected.steps.length;\n\n // Penalize repeated steps if not allowed\n if (!opts.allowRepeatedSteps && opts.repeatedSteps.length > 0) {\n score = Math.max(0, score - opts.repeatedSteps.length * 0.1);\n }\n\n return {\n score: roundToTwoDecimals(Math.max(0, Math.min(1, score))),\n matchedSteps,\n totalExpectedSteps: expected.steps.length,\n totalActualSteps: actual.steps.length,\n missingSteps,\n extraSteps,\n outOfOrderSteps,\n repeatedSteps: opts.repeatedSteps,\n };\n}\n\n/**\n * Fields on each ExpectedStep variant that are comparable data (not structural).\n * Used by `expectedStepMatches` to know which fields to compare when `compareData` is true.\n */\nconst COMPARABLE_FIELDS_BY_TYPE: Record<string, string[]> = {\n tool_call: ['toolArgs', 'toolResult', 'success'],\n mcp_tool_call: ['toolArgs', 'toolResult', 'mcpServer', 'success'],\n model_generation: ['modelId', 'promptTokens', 'completionTokens', 'finishReason'],\n agent_run: ['agentId'],\n workflow_step: ['stepId', 'status', 'output'],\n workflow_run: ['workflowId', 'status'],\n workflow_conditional: ['conditionCount', 'selectedSteps'],\n workflow_parallel: ['branchCount', 'parallelSteps'],\n workflow_loop: ['loopType', 'totalIterations'],\n workflow_sleep: ['sleepDurationMs', 'sleepType'],\n workflow_wait_event: ['eventName', 'eventReceived'],\n processor_run: ['processorId'],\n};\n\n/**\n * Check if an actual TrajectoryStep matches an ExpectedStep.\n * Matches by name, optionally by stepType, and auto-compares any variant-specific\n * fields that are present on the expected step.\n */\nfunction expectedStepMatches(actual: TrajectoryStep, expected: ExpectedStep): boolean {\n if (actual.name !== expected.name) return false;\n if (expected.stepType && actual.stepType !== expected.stepType) return false;\n\n if (expected.stepType) {\n const fields = COMPARABLE_FIELDS_BY_TYPE[expected.stepType] ?? [];\n for (const field of fields) {\n const expectedVal = (expected as any)[field];\n if (expectedVal === undefined) continue; // field not specified in expectation, skip\n const actualVal = (actual as any)[field];\n if (actualVal === undefined) return false;\n try {\n if (JSON.stringify(actualVal) !== JSON.stringify(expectedVal)) return false;\n } catch {\n return false;\n }\n }\n }\n\n return true;\n}\n\nfunction compareUnorderedPresence(\n actual: Trajectory,\n expected: { steps: ExpectedStep[] },\n opts: { allowRepeatedSteps: boolean; repeatedSteps: string[] },\n): TrajectoryComparisonResult {\n const actualNames: string[] = actual.steps.map((s: TrajectoryStep) => s.name);\n const expectedNames: string[] = expected.steps.map((s: ExpectedStep) => s.name);\n\n let matchedSteps = 0;\n const matchedExpectedIndices = new Set<number>();\n const usedIndices = new Set<number>();\n for (let i = 0; i < expected.steps.length; i++) {\n const expectedStep = expected.steps[i]!;\n for (let j = 0; j < actual.steps.length; j++) {\n if (!usedIndices.has(j) && expectedStepMatches(actual.steps[j]!, expectedStep)) {\n matchedSteps++;\n matchedExpectedIndices.add(i);\n usedIndices.add(j);\n break;\n }\n }\n }\n\n // Missing steps = expected steps that were not matched (accounts for stepType/data mismatches)\n const missingSteps = expectedNames.filter((_, i) => !matchedExpectedIndices.has(i));\n const expectedSet = new Set(expectedNames);\n const extraSteps = actualNames.filter(name => !expectedSet.has(name));\n\n let score = matchedSteps / expected.steps.length;\n\n // Penalize repeated steps if not allowed\n if (!opts.allowRepeatedSteps && opts.repeatedSteps.length > 0) {\n score = Math.max(0, score - opts.repeatedSteps.length * 0.1);\n }\n\n return {\n score: roundToTwoDecimals(Math.max(0, Math.min(1, score))),\n matchedSteps,\n totalExpectedSteps: expected.steps.length,\n totalActualSteps: actual.steps.length,\n missingSteps,\n extraSteps,\n outOfOrderSteps: [], // ordering not checked in unordered mode\n repeatedSteps: opts.repeatedSteps,\n };\n}\n\n// ─── Efficiency evaluation ───\n\n/**\n * Result of checking trajectory efficiency.\n */\nexport type TrajectoryEfficiencyResult = {\n /** Overall efficiency score from 0 to 1 */\n score: number;\n /** Total number of steps taken */\n totalSteps: number;\n /** Whether the step budget was exceeded */\n overStepBudget: boolean;\n /** Total tokens used across model_generation steps */\n totalTokens: number;\n /** Whether the token budget was exceeded */\n overTokenBudget: boolean;\n /** Total duration in milliseconds */\n totalDurationMs: number;\n /** Whether the duration budget was exceeded */\n overDurationBudget: boolean;\n /** Redundant calls detected (same tool + same args consecutively) */\n redundantCalls: Array<{ name: string; index: number }>;\n};\n\n/**\n * Evaluate trajectory efficiency against budgets and redundancy checks.\n * Throws when a configured budget is invalid or its required measurements are incomplete or invalid.\n */\nexport function checkTrajectoryEfficiency(\n trajectory: Trajectory,\n options: {\n maxSteps?: number;\n maxTotalTokens?: number;\n maxTotalDurationMs?: number;\n noRedundantCalls?: boolean;\n } = {},\n): TrajectoryEfficiencyResult {\n const { maxSteps, maxTotalTokens, maxTotalDurationMs, noRedundantCalls = true } = options;\n\n const isMeasurement = (value: number | undefined): value is number =>\n typeof value === 'number' && Number.isFinite(value) && value >= 0;\n for (const [name, limit] of Object.entries({ maxSteps, maxTotalTokens, maxTotalDurationMs })) {\n if (limit !== undefined && !isMeasurement(limit)) {\n throw new Error(`Invalid ${name} budget: expected a finite nonnegative number`);\n }\n }\n\n const totalSteps = trajectory.steps.length;\n\n // Count each model generation, including nested agents, without counting container aggregates.\n let totalTokens = 0;\n let modelGenerations = 0;\n const remaining = [...trajectory.steps];\n while (remaining.length > 0) {\n const step = remaining.pop()!;\n if (step.stepType === 'model_generation') {\n modelGenerations++;\n if (\n maxTotalTokens !== undefined &&\n (!isMeasurement(step.promptTokens) || !isMeasurement(step.completionTokens))\n ) {\n throw new Error(\n `Cannot evaluate token budget: model generation \"${step.name}\" has missing or invalid token counts`,\n );\n }\n totalTokens += (step.promptTokens ?? 0) + (step.completionTokens ?? 0);\n }\n if (step.children) {\n for (const child of step.children) remaining.push(child);\n }\n }\n if (maxTotalTokens !== undefined && (modelGenerations === 0 || !isMeasurement(totalTokens))) {\n throw new Error('Cannot evaluate token budget: missing model-generation measurements or invalid total tokens');\n }\n\n // A parent duration already covers its children; never add both.\n const totalDurationMs =\n trajectory.totalDurationMs ?? trajectory.steps.reduce((sum, s) => sum + (s.durationMs ?? 0), 0);\n if (maxTotalDurationMs !== undefined) {\n const hasDuration =\n trajectory.totalDurationMs !== undefined\n ? isMeasurement(trajectory.totalDurationMs)\n : trajectory.steps.length > 0 && trajectory.steps.every(step => isMeasurement(step.durationMs));\n if (!hasDuration || !isMeasurement(totalDurationMs)) {\n throw new Error('Cannot evaluate duration budget: missing or invalid duration measurements');\n }\n }\n\n // Detect redundant calls (same tool name + same args in consecutive calls)\n const redundantCalls: Array<{ name: string; index: number }> = [];\n if (noRedundantCalls) {\n for (let i = 1; i < trajectory.steps.length; i++) {\n const prev = trajectory.steps[i - 1]!;\n const curr = trajectory.steps[i]!;\n if (\n prev.name === curr.name &&\n prev.stepType === curr.stepType &&\n (prev.stepType === 'tool_call' || prev.stepType === 'mcp_tool_call')\n ) {\n const prevArgs = (prev as TrajectoryStep & { toolArgs?: Record<string, unknown> }).toolArgs;\n const currArgs = (curr as TrajectoryStep & { toolArgs?: Record<string, unknown> }).toolArgs;\n try {\n if (JSON.stringify(prevArgs) === JSON.stringify(currArgs)) {\n redundantCalls.push({ name: curr.name, index: i });\n }\n } catch {\n // If serialization fails, don't flag as redundant\n }\n }\n }\n }\n\n const overStepBudget = maxSteps !== undefined && totalSteps > maxSteps;\n const overTokenBudget = maxTotalTokens !== undefined && totalTokens > maxTotalTokens;\n const overDurationBudget = maxTotalDurationMs !== undefined && totalDurationMs > maxTotalDurationMs;\n\n // Calculate score: each dimension contributes equally\n const dimensions: number[] = [];\n\n if (maxSteps !== undefined) {\n dimensions.push(overStepBudget ? Math.max(0, 1 - (totalSteps - maxSteps) / maxSteps) : 1);\n }\n if (maxTotalTokens !== undefined) {\n dimensions.push(overTokenBudget ? Math.max(0, 1 - (totalTokens - maxTotalTokens) / maxTotalTokens) : 1);\n }\n if (maxTotalDurationMs !== undefined) {\n dimensions.push(\n overDurationBudget ? Math.max(0, 1 - (totalDurationMs - maxTotalDurationMs) / maxTotalDurationMs) : 1,\n );\n }\n if (noRedundantCalls) {\n dimensions.push(redundantCalls.length === 0 ? 1 : Math.max(0, 1 - redundantCalls.length * 0.2));\n }\n\n const score = dimensions.length > 0 ? dimensions.reduce((a, b) => a + b, 0) / dimensions.length : 1;\n\n return {\n score: roundToTwoDecimals(Math.max(0, Math.min(1, score))),\n totalSteps,\n overStepBudget,\n totalTokens,\n overTokenBudget,\n totalDurationMs,\n overDurationBudget,\n redundantCalls,\n };\n}\n\n// ─── Blacklist evaluation ───\n\n/**\n * Result of checking trajectory against a blacklist.\n */\nexport type TrajectoryBlacklistResult = {\n /** Score: 1.0 if clean, 0.0 if any violation found */\n score: number;\n /** Individual blacklisted tools that were found */\n violatedTools: string[];\n /** Blacklisted sequences that were found */\n violatedSequences: string[][];\n};\n\n/**\n * Check if a trajectory violates any blacklist rules.\n * Returns score 0.0 if any violation is found (hard fail).\n */\nexport function checkTrajectoryBlacklist(\n trajectory: Trajectory,\n options: {\n blacklistedTools?: string[];\n blacklistedSequences?: string[][];\n } = {},\n): TrajectoryBlacklistResult {\n const { blacklistedTools = [], blacklistedSequences = [] } = options;\n const violatedTools: string[] = [];\n const violatedSequences: string[][] = [];\n\n const stepNames = trajectory.steps.map(s => s.name);\n\n // Check blacklisted tools\n for (const forbidden of blacklistedTools) {\n if (stepNames.includes(forbidden)) {\n violatedTools.push(forbidden);\n }\n }\n\n // Check blacklisted sequences (contiguous subsequences)\n for (const sequence of blacklistedSequences) {\n if (sequence.length === 0) continue;\n for (let i = 0; i <= stepNames.length - sequence.length; i++) {\n let match = true;\n for (let j = 0; j < sequence.length; j++) {\n if (stepNames[i + j] !== sequence[j]) {\n match = false;\n break;\n }\n }\n if (match) {\n violatedSequences.push(sequence);\n break; // Only report each sequence once\n }\n }\n }\n\n const hasViolations = violatedTools.length > 0 || violatedSequences.length > 0;\n\n return {\n score: hasViolations ? 0 : 1,\n violatedTools,\n violatedSequences,\n };\n}\n\n// ─── Tool failure analysis ───\n\n/**\n * A detected tool failure pattern in the trajectory.\n */\nexport type ToolFailurePattern = {\n /** The tool name that experienced failure */\n toolName: string;\n /** Number of consecutive retries (same tool, same or similar args) */\n retryCount: number;\n /** Whether the agent fell back to a different tool after failures */\n fellBackToAlternative: boolean;\n /** The alternative tool used, if any */\n alternativeTool?: string;\n /** Whether any retry eventually succeeded */\n eventuallySucceeded: boolean;\n};\n\n/**\n * Result of analyzing tool failure patterns in a trajectory.\n */\nexport type ToolFailureAnalysisResult = {\n /** Score from 0 to 1 (lower = more failures/retries) */\n score: number;\n /** Tool failure patterns detected */\n patterns: ToolFailurePattern[];\n /** Total number of retries across all tools */\n totalRetries: number;\n /** Tools that exceeded the retry threshold */\n excessiveRetryTools: string[];\n};\n\n/**\n * Analyze tool failure and retry patterns in a trajectory.\n */\nexport function analyzeToolFailures(\n trajectory: Trajectory,\n options: {\n maxRetriesPerTool?: number;\n } = {},\n): ToolFailureAnalysisResult {\n const { maxRetriesPerTool = 2 } = options;\n const patterns: ToolFailurePattern[] = [];\n let totalRetries = 0;\n\n const toolCallSteps = trajectory.steps.filter(s => s.stepType === 'tool_call' || s.stepType === 'mcp_tool_call');\n\n if (toolCallSteps.length === 0) {\n return { score: 1, patterns: [], totalRetries: 0, excessiveRetryTools: [] };\n }\n\n // Group consecutive calls to the same tool as potential retry sequences\n let i = 0;\n while (i < toolCallSteps.length) {\n const currentTool = toolCallSteps[i]!;\n let retryCount = 0;\n let j = i + 1;\n\n // Count consecutive calls to the same tool\n // (toolCallSteps is pre-filtered to tool_call/mcp_tool_call, so no stepType checks needed)\n while (j < toolCallSteps.length && toolCallSteps[j]!.name === currentTool.name) {\n const prevStep = toolCallSteps[j - 1]! as TrajectoryStep & { success?: boolean };\n if (prevStep.success === false) {\n retryCount++;\n }\n j++;\n }\n\n if (retryCount > 0) {\n // Check if agent fell back to a different tool after retries\n const nextDifferentTool = j < toolCallSteps.length ? toolCallSteps[j] : undefined;\n const lastRetry = toolCallSteps[j - 1]! as TrajectoryStep & { success?: boolean };\n const lastSuccess = lastRetry.success !== false;\n\n patterns.push({\n toolName: currentTool.name,\n retryCount,\n fellBackToAlternative: nextDifferentTool !== undefined && !lastSuccess,\n alternativeTool: nextDifferentTool !== undefined && !lastSuccess ? nextDifferentTool.name : undefined,\n eventuallySucceeded: lastSuccess,\n });\n\n totalRetries += retryCount;\n }\n\n i = j;\n }\n\n // Score: penalize excessive retries\n const excessiveRetryTools = patterns.filter(p => p.retryCount > maxRetriesPerTool).map(p => p.toolName);\n\n let score = 1;\n if (toolCallSteps.length > 0) {\n // Each retry beyond the threshold costs more\n const excessRetries = patterns.reduce((sum, p) => sum + Math.max(0, p.retryCount - maxRetriesPerTool), 0);\n score = Math.max(0, 1 - excessRetries * 0.2);\n }\n\n return {\n score: roundToTwoDecimals(Math.max(0, Math.min(1, score))),\n patterns,\n totalRetries,\n excessiveRetryTools,\n };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAsDA,SAAgB,kCAAkC,SAAkC;CAClF,MAAM,UAAU,QAAQ;CAExB,IAAI,OAAO,YAAY,UACrB,OAAO;CAET,IAAI,MAAM,QAAQ,OAAO,GAAG;EAC1B,MAAM,YAAY,QAAQ,QAAO,MAAK,EAAE,SAAS,MAAM;EACvD,OAAO,UAAU,SAAS,IAAI,UAAU,UAAU,SAAS,EAAE,EAAE,QAAQ,KAAK;CAC9E;CACA,IAA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Ac,IAAI,EACP,CAAC,YAAY;GAE1C,SAAS,KAAK;IACZ,UAAU,YAAY;IACtB;IACA,uBAAuB,sBAAsB,KAAA,KAAa,CAAC;IAC3D,iBAAiB,sBAAsB,KAAA,KAAa,CAAC,cAAc,kBAAkB,OAAO,KAAA;IAC5F,qBAAqB;GACvB,CAAC;GAED,gBAAgB;EAClB;EAEA,IAAI;CACN;CAGA,MAAM,sBAAsB,SAAS,QAAO,MAAK,EAAE,aAAa,iBAAiB,CAAC,CAAC,KAAI,MAAK,EAAE,QAAQ;CAEtG,IAAI,QAAQ;CACZ,IAAI,cAAc,SAAS,GAAG;EAE5B,MAAM,gBAAgB,SAAS,QAAQ,KAAK,MAAM,MAAM,KAAK,IAAI,GAAG,EAAE,aAAa,iBAAiB,GAAG,CAAC;EACxG,QAAQ,KAAK,IAAI,GAAG,IAAI,gBAAgB,EAAG;CAC7C;CAEA,OAAO;EACL,OAAO,mBAAmB,KAAK,IAAI,GAAG,KAAK,IAAI,GAAG,KAAK,CAAC,CAAC;EACzD;EACA;EACA;CACF;AACF"}
1
+ {"version":3,"file":"utils.js","names":[],"sources":["../../src/scorers/utils.ts"],"sourcesContent":["import type { MastraDBMessage } from '@mastra/core/agent';\nimport type {\n ExpectedStep,\n ScorerRunInputForAgent,\n ScorerRunOutputForAgent,\n ScoringInput,\n TrajectoryExpectation,\n TrajectoryStep,\n Trajectory,\n} from '@mastra/core/evals';\nimport { RequestContext } from '@mastra/core/request-context';\n\nexport type ScorerRunInputForLLMJudge =\n | ScorerRunInputForAgent\n | string\n | {\n inputMessages?: unknown[];\n messages?: unknown[];\n prompt?: string;\n text?: string;\n content?: unknown;\n input?: unknown;\n user?: unknown;\n [key: string]: unknown;\n };\n\nexport type ScorerRunOutputForLLMJudge =\n | ScorerRunOutputForAgent\n | string\n | unknown[]\n | {\n text?: string;\n content?: unknown;\n role?: string;\n [key: string]: unknown;\n };\n\n/**\n * Extracts text content from a MastraDBMessage or ModelMessage-like object.\n *\n * @param message - The message to extract text from\n * @returns The extracted text content, or an empty string if no text is found\n *\n * @example\n * ```ts\n * const message: MastraDBMessage = {\n * id: 'msg-1',\n * role: 'assistant',\n * content: { format: 2, parts: [{ type: 'text', text: 'Hello!' }] },\n * createdAt: new Date(),\n * };\n * const text = getTextContentFromMastraDBMessage(message); // 'Hello!'\n * ```\n */\nexport function getTextContentFromMastraDBMessage(message: MastraDBMessage): string {\n const content = message.content as any;\n\n if (typeof content === 'string') {\n return content;\n }\n if (Array.isArray(content)) {\n const textParts = content.filter(p => p.type === 'text');\n return textParts.length > 0 ? textParts[textParts.length - 1]?.text || '' : '';\n }\n if (typeof content?.content === 'string' && content.content !== '') {\n return content.content;\n }\n if (typeof content?.text === 'string' && content.text !== '') {\n return content.text;\n }\n if (content?.parts && Array.isArray(content.parts)) {\n // Return only the last text part like AI SDK does\n const textParts = content.parts.filter((p: any) => p.type === 'text');\n return textParts.length > 0 ? textParts[textParts.length - 1]?.text || '' : '';\n }\n return '';\n}\n\nconst isRecord = (value: unknown): value is Record<string, any> => {\n return typeof value === 'object' && value !== null;\n};\n\nconst getTextFromValue = (value: unknown): string | undefined => {\n if (typeof value === 'string') return value === '' ? undefined : value;\n if (Array.isArray(value)) {\n const textParts = value\n .filter(part => isRecord(part) && part.type === 'text' && typeof part.text === 'string')\n .map(part => part.text);\n return textParts.length > 0 ? textParts[textParts.length - 1] : undefined;\n }\n if (!isRecord(value)) return undefined;\n\n const fromParts = Array.isArray(value.parts) ? getTextFromValue(value.parts) : undefined;\n\n return (\n getTextFromValue(value.content) ??\n (typeof value.text === 'string' && value.text !== '' ? value.text : undefined) ??\n (typeof value.body === 'string' && value.body !== '' ? value.body : undefined) ??\n fromParts\n );\n};\n\nexport const isScorerRunInputForAgent = (input: unknown): input is ScorerRunInputForAgent => {\n return (\n isRecord(input) &&\n Array.isArray(input.inputMessages) &&\n Array.isArray(input.rememberedMessages) &&\n Array.isArray(input.systemMessages) &&\n isRecord(input.taggedSystemMessages)\n );\n};\n\nconst isMastraDBMessageLike = (message: unknown): message is MastraDBMessage => {\n return (\n isRecord(message) &&\n typeof message.id === 'string' &&\n typeof message.role === 'string' &&\n 'content' in message &&\n 'createdAt' in message\n );\n};\n\nexport const isScorerRunOutputForAgent = (output: unknown): output is ScorerRunOutputForAgent => {\n return Array.isArray(output) && output.every(isMastraDBMessageLike);\n};\n\n/**\n * Resolves the effective role of a message, accounting for agent signal messages.\n *\n * Messages delivered through the agent subscription / signal API are persisted with\n * `role: 'signal'` and carry their semantic role (e.g. `user`) on `type` and on\n * `content.metadata.signal.{type,tagName}`. Treat those as their underlying role so\n * helpers like `getUserMessageFromRunInput` can find them.\n */\nconst getEffectiveMessageRole = (message: Record<string, any>): string | undefined => {\n if (message.role !== 'signal') return typeof message.role === 'string' ? message.role : undefined;\n\n const signalMeta =\n isRecord(message.content) && isRecord(message.content.metadata) ? message.content.metadata.signal : undefined;\n\n const tagName = isRecord(signalMeta) && typeof signalMeta.tagName === 'string' ? signalMeta.tagName : undefined;\n const signalType = isRecord(signalMeta) && typeof signalMeta.type === 'string' ? signalMeta.type : undefined;\n const topLevelType = typeof message.type === 'string' ? message.type : undefined;\n\n return tagName ?? signalType ?? topLevelType;\n};\n\nconst getTextFromMessages = (messages: unknown, role: string): string | undefined => {\n if (!Array.isArray(messages)) return undefined;\n\n const message = messages.find(message => isRecord(message) && getEffectiveMessageRole(message) === role);\n return message ? getTextFromValue(message) : undefined;\n};\n\n/**\n * Rounds a number to two decimal places.\n *\n * Uses `Number.EPSILON` to handle floating-point precision issues.\n *\n * @param num - The number to round\n * @returns The number rounded to two decimal places\n *\n * @example\n * ```ts\n * roundToTwoDecimals(0.1 + 0.2); // 0.3\n * roundToTwoDecimals(1.005); // 1.01\n * ```\n */\nexport const roundToTwoDecimals = (num: number) => {\n return Math.round((num + Number.EPSILON) * 100) / 100;\n};\n\n/**\n * Determines if a value is closer to the first target than the second.\n *\n * @param value - The value to compare\n * @param target1 - The first target value\n * @param target2 - The second target value\n * @returns `true` if `value` is closer to `target1` than `target2`\n *\n * @example\n * ```ts\n * isCloserTo(0.6, 1, 0); // true (0.6 is closer to 1)\n * isCloserTo(0.3, 1, 0); // false (0.3 is closer to 0)\n * ```\n */\nexport function isCloserTo(value: number, target1: number, target2: number): boolean {\n return Math.abs(value - target1) < Math.abs(value - target2);\n}\n\n/**\n * Represents a test case for scorer evaluation.\n */\nexport type TestCase = {\n /** The input text to evaluate */\n input: string;\n /** The output text to evaluate */\n output: string;\n /** The expected result of the evaluation */\n expectedResult: {\n /** The expected score */\n score: number;\n /** The optional expected reason */\n reason?: string;\n };\n};\n\n/**\n * Represents a test case with additional context for scorer evaluation.\n */\nexport type TestCaseWithContext = TestCase & {\n /** Additional context strings for the evaluation */\n context: string[];\n};\n\n/**\n * Creates a scoring input object for testing purposes.\n *\n * @param input - The user input text\n * @param output - The assistant output text\n * @param additionalContext - Optional additional context data\n * @param requestContext - Optional request context data\n * @returns A ScoringInput object ready for use in scorer tests\n *\n * @example\n * ```ts\n * const run = createTestRun(\n * 'What is 2+2?',\n * 'The answer is 4.',\n * { topic: 'math' }\n * );\n * ```\n */\nexport const createTestRun = (\n input: string,\n output: string,\n additionalContext?: Record<string, any>,\n requestContext?: Record<string, any>,\n): ScoringInput => {\n return {\n input: [{ role: 'user', content: input }],\n output: { role: 'assistant', text: output },\n additionalContext: additionalContext ?? {},\n requestContext: requestContext ?? {},\n };\n};\n\n/**\n * Extracts the user message text from a scorer run input.\n *\n * Accepts the agent shape (`{ inputMessages }`), `ModelMessage[]`\n * (`{ messages }`), workflow input (`{ prompt }`), and a bare string.\n *\n * @param input - The scorer run input\n * @returns The user message text, or `undefined` if none can be extracted\n *\n * @example\n * ```ts\n * const scorer = createScorer({ ... })\n * .preprocess(({ run }) => {\n * const userText = getUserMessageFromRunInput(run.input);\n * return { userText };\n * });\n * ```\n */\nexport const getUserMessageFromRunInput = (input?: unknown): string | undefined => {\n if (typeof input === 'string') return input;\n if (!isRecord(input)) return undefined;\n\n return (\n getTextFromMessages(input.inputMessages, 'user') ??\n getTextFromMessages(input.messages, 'user') ??\n (typeof input.prompt === 'string' ? input.prompt : undefined) ??\n (typeof input.text === 'string' ? input.text : undefined) ??\n getTextFromValue(input.content) ??\n getTextFromValue(input.input) ??\n getTextFromValue(input.user)\n );\n};\n\nconst DEFAULT_CONVERSATION_HISTORY_MESSAGES = 10;\n\n/**\n * Renders the remembered conversation history from an agent scorer run input as a transcript.\n *\n * Only agent run inputs carry `rememberedMessages`; any other input shape (a bare string,\n * a plain `{ prompt }` object, `ModelMessage[]`) returns `undefined` so callers can safely\n * fall back to single-turn behaviour.\n *\n * @param input - The scorer run input\n * @param options.maxMessages - How many of the most recent remembered messages to keep (default 10)\n * @returns A newline separated `role: text` transcript, or `undefined` if there is no history\n *\n * @example\n * ```ts\n * const scorer = createScorer({ ... })\n * .preprocess(({ run }) => {\n * const history = getConversationHistoryFromRunInput(run.input, { maxMessages: 4 });\n * return { history };\n * });\n * ```\n */\nexport const getConversationHistoryFromRunInput = (\n input?: unknown,\n options?: { maxMessages?: number },\n): string | undefined => {\n if (!isScorerRunInputForAgent(input)) return undefined;\n\n const maxMessages = options?.maxMessages ?? DEFAULT_CONVERSATION_HISTORY_MESSAGES;\n if (maxMessages <= 0) return undefined;\n\n const lines = input.rememberedMessages\n .slice(-maxMessages)\n .map(message => {\n if (!isRecord(message)) return undefined;\n\n const role = getEffectiveMessageRole(message);\n const text = getTextContentFromMastraDBMessage(message as MastraDBMessage).trim();\n if (!role || !text) return undefined;\n\n return `${role}: ${text}`;\n })\n .filter((line): line is string => Boolean(line));\n\n return lines.length > 0 ? lines.join('\\n') : undefined;\n};\n\n/**\n * Extracts all system messages from a scorer run input.\n *\n * Collects text from both standard system messages and tagged system messages\n * (specialized system prompts like memory instructions).\n *\n * @param input - The scorer run input containing system messages\n * @returns An array of system message strings\n *\n * @example\n * ```ts\n * const scorer = createScorer({ ... })\n * .preprocess(({ run }) => {\n * const systemMessages = getSystemMessagesFromRunInput(run.input);\n * return { systemPrompt: systemMessages.join('\\n') };\n * });\n * ```\n */\nexport const getSystemMessagesFromRunInput = (input?: unknown): string[] => {\n const systemMessages: string[] = [];\n if (!isRecord(input)) return systemMessages;\n\n // Add standard system messages\n if (Array.isArray(input.systemMessages)) {\n systemMessages.push(\n ...input.systemMessages\n .map(msg => {\n // Handle different content types - extract text if it's an array of parts\n if (typeof msg.content === 'string') {\n return msg.content;\n } else if (Array.isArray(msg.content)) {\n // Extract text from parts array\n return msg.content\n .filter((part: any) => part.type === 'text')\n .map((part: any) => part.text || '')\n .join(' ');\n }\n return '';\n })\n .filter(content => content),\n );\n }\n\n const addSystemMessages = (messages: unknown) => {\n if (!Array.isArray(messages)) return;\n\n systemMessages.push(\n ...messages\n .filter(message => isRecord(message) && message.role === 'system')\n .map(message => getTextFromValue(message))\n .filter((content): content is string => Boolean(content)),\n );\n };\n\n addSystemMessages(input.inputMessages);\n addSystemMessages(input.messages);\n\n // Add tagged system messages (these are specialized system prompts)\n if (isRecord(input.taggedSystemMessages)) {\n Object.values(input.taggedSystemMessages).forEach(messages => {\n if (!Array.isArray(messages)) return;\n messages.forEach(msg => {\n const content = getTextFromValue(msg);\n if (content) {\n systemMessages.push(content);\n }\n });\n });\n }\n\n return systemMessages;\n};\n\n/**\n * Combines all system messages into a single prompt string.\n *\n * Joins all system messages (standard and tagged) with double newlines.\n *\n * @param input - The scorer run input containing system messages\n * @returns A combined system prompt string\n *\n * @example\n * ```ts\n * const scorer = createScorer({ ... })\n * .preprocess(({ run }) => {\n * const systemPrompt = getCombinedSystemPrompt(run.input);\n * return { systemPrompt };\n * });\n * ```\n */\nexport const getCombinedSystemPrompt = (input?: unknown): string => {\n const systemMessages = getSystemMessagesFromRunInput(input);\n return systemMessages.join('\\n\\n');\n};\n\n/**\n * Extracts the assistant message text from a scorer run output.\n *\n * Accepts the agent shape (`MastraDBMessage[]` / `ModelMessage[]`), workflow\n * output (`{ text }`), task output (`{ content }`), a single assistant message\n * object, and a bare string.\n *\n * For array outputs the final assistant message is used, so multi-step agent\n * responses are scored on the last response rather than an intermediate one.\n *\n * @param output - The scorer run output\n * @returns The assistant message text, or `undefined` if none can be extracted\n *\n * @example\n * ```ts\n * const scorer = createScorer({ ... })\n * .preprocess(({ run }) => {\n * const response = getAssistantMessageFromRunOutput(run.output);\n * return { response };\n * });\n * ```\n */\nexport const getAssistantMessageFromRunOutput = (output?: unknown) => {\n if (typeof output === 'string') return output;\n if (Array.isArray(output)) {\n const assistantMessages = output.filter(\n message => isRecord(message) && getEffectiveMessageRole(message) === 'assistant',\n );\n // Prefer the last assistant message that carries text; a trailing assistant\n // message may hold only tool-call or data parts.\n for (let i = assistantMessages.length - 1; i >= 0; i--) {\n const text = getTextFromValue(assistantMessages[i]);\n if (text) return text;\n }\n return undefined;\n }\n if (!isRecord(output)) return undefined;\n\n const isAssistantOutput = output.role === undefined || output.role === 'assistant';\n\n if (isAssistantOutput && typeof output.text === 'string') return output.text;\n if (isAssistantOutput && typeof output.content === 'string') return output.content;\n if (isAssistantOutput && (isRecord(output.content) || Array.isArray(output.content))) {\n return (\n getTextContentFromMastraDBMessage(output as MastraDBMessage) ||\n getTextContentFromMastraDBMessage(output.content as MastraDBMessage) ||\n undefined\n );\n }\n if (output.role === 'assistant') return getTextContentFromMastraDBMessage(output as MastraDBMessage) || undefined;\n\n return undefined;\n};\n\n/**\n * Extracts reasoning text from a scorer run output.\n *\n * This function extracts reasoning content from assistant messages, which is\n * produced by reasoning models like `deepseek-reasoner`. The reasoning can be\n * stored in two places:\n * 1. `content.reasoning` - a string field on the message content\n * 2. `content.parts` - as parts with `type: 'reasoning'` containing `details`\n *\n * @param output - The scorer run output (array of MastraDBMessage)\n * @returns The reasoning text, or `undefined` if no reasoning is present\n *\n * @example\n * ```ts\n * const reasoningScorer = createScorer({\n * id: 'reasoning-scorer',\n * name: 'Reasoning Quality',\n * description: 'Evaluates the quality of model reasoning',\n * type: 'agent',\n * })\n * .preprocess(({ run }) => {\n * const reasoning = getReasoningFromRunOutput(run.output);\n * const response = getAssistantMessageFromRunOutput(run.output);\n * return { reasoning, response };\n * })\n * .generateScore(({ results }) => {\n * // Score based on reasoning quality\n * return results.preprocessStepResult?.reasoning ? 1 : 0;\n * });\n * ```\n */\nexport const getReasoningFromRunOutput = (output?: ScorerRunOutputForAgent): string | undefined => {\n if (!output) return undefined;\n\n const message = output.find(({ role }) => role === 'assistant');\n if (!message) return undefined;\n\n // Check for reasoning in content.reasoning (string format)\n if (message.content.reasoning) {\n return message.content.reasoning;\n }\n\n // Check for reasoning in parts with type 'reasoning'\n // Reasoning models store reasoning in parts as { type: 'reasoning', details: [{ type: 'text', text: '...' }] }\n const reasoningParts = message.content.parts?.filter((p: any) => p.type === 'reasoning');\n if (reasoningParts && reasoningParts.length > 0) {\n const reasoningTexts = reasoningParts\n .map((p: any) => {\n // The reasoning text can be in p.reasoning or in p.details[].text\n if (p.details && Array.isArray(p.details)) {\n return p.details\n .filter((d: any) => d.type === 'text')\n .map((d: any) => d.text)\n .join('');\n }\n return p.reasoning || '';\n })\n .filter(Boolean);\n\n return reasoningTexts.length > 0 ? reasoningTexts.join('\\n') : undefined;\n }\n\n return undefined;\n};\n\n/**\n * Creates a tool invocation object for testing purposes.\n *\n * @param options - The tool invocation configuration\n * @param options.toolCallId - Unique identifier for the tool call\n * @param options.toolName - Name of the tool being called\n * @param options.args - Arguments passed to the tool\n * @param options.result - Result returned by the tool (absent for a thrown call)\n * @param options.state - State of the invocation (default: 'result')\n * @param options.errorText - Error text recorded when a tool threw (`state: 'output-error'`)\n * @param options.isError - Set alongside `state: 'result'` when the executor reported a failure\n * @returns A tool invocation object\n *\n * @example\n * ```ts\n * const invocation = createToolInvocation({\n * toolCallId: 'call-123',\n * toolName: 'weatherTool',\n * args: { location: 'London' },\n * result: { temperature: 20, condition: 'sunny' },\n * });\n *\n * // A tool that threw\n * const thrown = createToolInvocation({\n * toolCallId: 'call-456',\n * toolName: 'saveTool',\n * args: { value: 'x' },\n * state: 'output-error',\n * errorText: 'Save failed',\n * });\n * ```\n */\nexport const createToolInvocation = ({\n toolCallId,\n toolName,\n args,\n result,\n state = 'result',\n errorText,\n isError,\n}: {\n toolCallId: string;\n toolName: string;\n args: Record<string, any>;\n result?: Record<string, any>;\n state?: 'call' | 'partial-call' | 'result' | 'output-error';\n errorText?: string;\n isError?: boolean;\n}): {\n toolCallId: string;\n toolName: string;\n args: Record<string, any>;\n result: Record<string, any> | undefined;\n state: 'call' | 'partial-call' | 'result' | 'output-error';\n errorText?: string;\n isError?: boolean;\n} => {\n return {\n toolCallId,\n toolName,\n args,\n result,\n state,\n ...(errorText !== undefined && { errorText }),\n ...(isError !== undefined && { isError }),\n };\n};\n\n/**\n * Creates a MastraDBMessage object for testing purposes.\n *\n * Supports optional tool invocations for testing tool call scenarios.\n *\n * @param options - The message configuration\n * @param options.content - The text content of the message\n * @param options.role - The role of the message sender ('user', 'assistant', or 'system')\n * @param options.id - Optional message ID (default: 'test-message')\n * @param options.toolInvocations - Optional array of tool invocations\n * @returns A MastraDBMessage object\n *\n * @example\n * ```ts\n * const message = createTestMessage({\n * content: 'Hello, how can I help?',\n * role: 'assistant',\n * });\n *\n * // With tool invocations\n * const messageWithTools = createTestMessage({\n * content: 'Let me check the weather.',\n * role: 'assistant',\n * toolInvocations: [{\n * toolCallId: 'call-1',\n * toolName: 'weatherTool',\n * args: { location: 'Paris' },\n * result: { temp: 22 },\n * state: 'result',\n * }],\n * });\n *\n * // With native V2 parts — the only form that can carry a thrown call\n * const messageWithThrownTool = createTestMessage({\n * content: 'That failed.',\n * role: 'assistant',\n * parts: [\n * { type: 'text', text: 'That failed.' },\n * {\n * type: 'tool-invocation',\n * toolInvocation: createToolInvocation({\n * toolCallId: 'call-2',\n * toolName: 'saveTool',\n * args: { value: 'x' },\n * state: 'output-error',\n * errorText: 'Save failed',\n * }),\n * },\n * ],\n * });\n * ```\n */\nexport function createTestMessage({\n content,\n role,\n id = 'test-message',\n toolInvocations = [],\n parts,\n}: {\n content: string;\n role: 'user' | 'assistant' | 'system';\n id?: string;\n toolInvocations?: Array<{\n toolCallId: string;\n toolName: string;\n args: Record<string, any>;\n result?: Record<string, any>;\n state: any;\n }>;\n /**\n * Native V2 message parts. Replaces the default single text part when provided.\n * Needed to express invocations the legacy array cannot carry, such as a thrown\n * call (`state: 'output-error'` with `errorText`).\n */\n parts?: MastraDBMessage['content']['parts'];\n}): MastraDBMessage {\n return {\n id,\n role,\n content: {\n format: 2,\n parts: parts ?? [{ type: 'text', text: content }],\n content,\n ...(toolInvocations.length > 0 && {\n toolInvocations: toolInvocations.map(ti => ({\n toolCallId: ti.toolCallId,\n toolName: ti.toolName,\n args: ti.args,\n result: ti.result,\n state: ti.state,\n })),\n }),\n },\n createdAt: new Date(),\n };\n}\n\n/**\n * Creates a complete agent test run object for testing scorers.\n *\n * Provides a convenient way to construct the full run object that scorers receive,\n * including input messages, output, system messages, and request context.\n *\n * @param options - The test run configuration\n * @param options.inputMessages - Array of input messages (default: [])\n * @param options.output - The output messages (required)\n * @param options.rememberedMessages - Array of remembered messages from memory (default: [])\n * @param options.systemMessages - Array of system messages (default: [])\n * @param options.taggedSystemMessages - Tagged system messages map (default: {})\n * @param options.requestContext - Request context (default: new RequestContext())\n * @param options.runId - Unique run ID (default: random UUID)\n * @returns A complete test run object\n *\n * @example\n * ```ts\n * const testRun = createAgentTestRun({\n * inputMessages: [createTestMessage({ content: 'Hello', role: 'user' })],\n * output: [createTestMessage({ content: 'Hi there!', role: 'assistant' })],\n * });\n *\n * const result = await scorer.run({\n * input: testRun.input,\n * output: testRun.output,\n * });\n * ```\n */\nexport const createAgentTestRun = ({\n inputMessages = [],\n output,\n rememberedMessages = [],\n systemMessages = [],\n taggedSystemMessages = {},\n requestContext = new RequestContext(),\n runId = crypto.randomUUID(),\n}: {\n inputMessages?: ScorerRunInputForAgent['inputMessages'];\n output: ScorerRunOutputForAgent;\n rememberedMessages?: ScorerRunInputForAgent['rememberedMessages'];\n systemMessages?: ScorerRunInputForAgent['systemMessages'];\n taggedSystemMessages?: ScorerRunInputForAgent['taggedSystemMessages'];\n requestContext?: RequestContext;\n runId?: string;\n}): {\n input: ScorerRunInputForAgent;\n output: ScorerRunOutputForAgent;\n requestContext: RequestContext;\n runId: string;\n} => {\n return {\n input: {\n inputMessages,\n rememberedMessages,\n systemMessages,\n taggedSystemMessages,\n },\n output,\n requestContext,\n runId,\n };\n};\n\n/**\n * Creates a test run for trajectory scorers where `output` is a `Trajectory`\n * (pre-extracted by the `runEvals` pipeline).\n *\n * @example\n * ```ts\n * const testRun = createTrajectoryTestRun({\n * inputMessages: [createTestMessage({ content: 'Do X', role: 'user', id: 'u1' })],\n * trajectory: {\n * steps: [\n * { stepType: 'tool_call', name: 'search', toolArgs: { q: 'test' } },\n * ],\n * },\n * });\n * ```\n */\nexport const createTrajectoryTestRun = ({\n inputMessages = [],\n trajectory,\n rememberedMessages = [],\n systemMessages = [],\n taggedSystemMessages = {},\n requestContext = new RequestContext(),\n runId = crypto.randomUUID(),\n expectedTrajectory,\n}: {\n inputMessages?: ScorerRunInputForAgent['inputMessages'];\n trajectory: Trajectory;\n rememberedMessages?: ScorerRunInputForAgent['rememberedMessages'];\n systemMessages?: ScorerRunInputForAgent['systemMessages'];\n taggedSystemMessages?: ScorerRunInputForAgent['taggedSystemMessages'];\n requestContext?: RequestContext;\n runId?: string;\n expectedTrajectory?: TrajectoryExpectation;\n}): {\n input: ScorerRunInputForAgent;\n output: Trajectory;\n requestContext: RequestContext;\n runId: string;\n expectedTrajectory?: TrajectoryExpectation;\n} => {\n return {\n input: {\n inputMessages,\n rememberedMessages,\n systemMessages,\n taggedSystemMessages,\n },\n output: trajectory,\n expectedTrajectory,\n requestContext,\n runId,\n };\n};\n\n/**\n * Information about a tool call extracted from scorer output.\n */\nexport type ToolCallInfo = {\n /** Name of the tool that was called */\n toolName: string;\n /** Unique identifier for the tool call */\n toolCallId: string;\n /** Index of the message containing this tool call */\n messageIndex: number;\n /** Index of the invocation within the message's tool invocations */\n invocationIndex: number;\n};\n\n/**\n * Merges the two places a message can store tool invocations into one list.\n *\n * A native message may carry `content.parts` tool-invocation entries, the legacy\n * `content.toolInvocations` array, or both. Neither form is a superset of the\n * other: thrown calls are recorded in parts as `state: 'output-error'` and can be\n * missing from the legacy array, while older persisted messages may only carry the\n * legacy array. Reading one form exclusively therefore hides real calls.\n *\n * Parts win when the same `toolCallId` appears in both, since they hold the real\n * terminal state. Legacy-only calls are retained before their next shared anchor so\n * compatible call order survives instead of being appended.\n *\n * Mirrors `extractTrajectory` in `@mastra/core` (`packages/core/src/evals/types.ts`)\n * so tool-use checks and trajectory scorers agree on what a run actually called.\n * Keep the two in step.\n *\n * @param message - A single message from the scorer run output\n * @returns The merged tool invocations for that message\n */\nexport function mergeToolInvocations(message: MastraDBMessage | undefined) {\n const legacy = message?.content?.toolInvocations;\n const fromParts =\n message?.content?.parts\n ?.filter((p): p is Extract<typeof p, { type: 'tool-invocation' }> => p.type === 'tool-invocation')\n .map(p => p.toolInvocation)\n .filter(Boolean) ?? [];\n const partCallIds = new Set(fromParts.map(invocation => invocation.toolCallId).filter(Boolean));\n const legacyInvocations = legacy ?? [];\n const legacyPositions = new Map(legacyInvocations.map((invocation, index) => [invocation?.toolCallId, index]));\n const toolInvocations: typeof fromParts = [];\n let legacyIndex = 0;\n for (const invocation of fromParts) {\n const sharedIndex = invocation.toolCallId ? legacyPositions.get(invocation.toolCallId) : undefined;\n if (sharedIndex !== undefined && sharedIndex >= legacyIndex) {\n // Retain legacy-only calls before their next shared anchor.\n for (; legacyIndex < sharedIndex; legacyIndex++) {\n const previous = legacyInvocations[legacyIndex];\n if (previous && !partCallIds.has(previous.toolCallId)) toolInvocations.push(previous);\n }\n legacyIndex++;\n }\n toolInvocations.push(invocation);\n }\n for (; legacyIndex < legacyInvocations.length; legacyIndex++) {\n const invocation = legacyInvocations[legacyIndex];\n if (invocation && !partCallIds.has(invocation.toolCallId)) toolInvocations.push(invocation);\n }\n return toolInvocations;\n}\n\n/**\n * Extracts all tool calls from a scorer run output.\n *\n * Iterates through all messages and their tool invocations to collect information\n * about tools that were called. A call counts regardless of outcome, so the states\n * `'result'`, `'call'`, and `'output-error'` (a tool that threw) are all included.\n *\n * @param output - The scorer run output (array of MastraDBMessage)\n * @returns An object containing tool names and detailed tool call info\n *\n * @example\n * ```ts\n * const scorer = createScorer({ ... })\n * .preprocess(({ run }) => {\n * const { tools, toolCallInfos } = extractToolCalls(run.output);\n * return {\n * toolsUsed: tools,\n * toolCount: tools.length,\n * };\n * });\n * ```\n */\nexport function extractToolCalls(output: ScorerRunOutputForAgent): { tools: string[]; toolCallInfos: ToolCallInfo[] } {\n const toolCalls: string[] = [];\n const toolCallInfos: ToolCallInfo[] = [];\n\n for (let messageIndex = 0; messageIndex < output.length; messageIndex++) {\n const message = output[messageIndex];\n const toolInvocations = mergeToolInvocations(message);\n\n if (!toolInvocations.length) continue;\n\n for (let invocationIndex = 0; invocationIndex < toolInvocations.length; invocationIndex++) {\n const invocation = toolInvocations[invocationIndex];\n // A thrown call (`output-error`) still counts: the tool ran. Partial calls do not.\n // Same state set core's `extractTrajectory` accepts.\n if (\n invocation &&\n invocation.toolName &&\n (invocation.state === 'result' || invocation.state === 'call' || invocation.state === 'output-error')\n ) {\n toolCalls.push(invocation.toolName);\n toolCallInfos.push({\n toolName: invocation.toolName,\n toolCallId: invocation.toolCallId || `${messageIndex}-${invocationIndex}`,\n messageIndex,\n invocationIndex,\n });\n }\n }\n }\n\n return { tools: toolCalls, toolCallInfos };\n}\n\n/**\n * Extracts text content from all input messages.\n *\n * @param runInput - The scorer run input\n * @returns An array of text strings from each input message\n *\n * @example\n * ```ts\n * const scorer = createScorer({ ... })\n * .preprocess(({ run }) => {\n * const messages = extractInputMessages(run.input);\n * return { allUserMessages: messages.join('\\n') };\n * });\n * ```\n */\nexport const extractInputMessages = (runInput: ScorerRunInputForAgent | undefined): string[] => {\n return runInput?.inputMessages?.map(msg => getTextContentFromMastraDBMessage(msg)) || [];\n};\n\n/**\n * Extracts text content from all assistant response messages.\n *\n * Filters for messages with role 'assistant' and extracts their text content.\n *\n * @param runOutput - The scorer run output (array of MastraDBMessage)\n * @returns An array of text strings from each assistant message\n *\n * @example\n * ```ts\n * const scorer = createScorer({ ... })\n * .preprocess(({ run }) => {\n * const responses = extractAgentResponseMessages(run.output);\n * return { allResponses: responses.join('\\n') };\n * });\n * ```\n */\nexport const extractAgentResponseMessages = (runOutput: ScorerRunOutputForAgent): string[] => {\n return runOutput.filter(msg => msg.role === 'assistant').map(msg => getTextContentFromMastraDBMessage(msg));\n};\n\n/**\n * Information about a tool result extracted from scorer output.\n */\nexport type ToolResultInfo = {\n /** Name of the tool that was called */\n toolName: string;\n /** Unique identifier for the tool call */\n toolCallId: string;\n /** Arguments passed to the tool */\n args: Record<string, any>;\n /** Result returned by the tool */\n result: any;\n};\n\n/**\n * Extracts tool results from a scorer run output.\n *\n * Returns structured objects that can be used with the hallucination scorer's\n * `getContext` hook or for other scorer logic.\n *\n * @param output - The scorer run output (array of MastraDBMessage)\n * @returns An array of ToolResultInfo objects\n *\n * @example\n * ```ts\n * import { extractToolResults } from '@mastra/evals/scorers';\n * import { createHallucinationScorer } from '@mastra/evals/scorers/prebuilt';\n *\n * const scorer = createHallucinationScorer({\n * model: openai('gpt-4o'),\n * options: {\n * getContext: (run) => {\n * const toolResults = extractToolResults(run.output);\n * return toolResults.map(t => JSON.stringify({ tool: t.toolName, result: t.result }));\n * },\n * },\n * });\n * ```\n */\nexport function extractToolResults(output: ScorerRunOutputForAgent): ToolResultInfo[] {\n const results: ToolResultInfo[] = [];\n\n for (const message of output) {\n const toolInvocations = mergeToolInvocations(message);\n\n if (!toolInvocations.length) continue;\n\n for (const invocation of toolInvocations) {\n // A thrown call carries `errorText` rather than a result, so it yields no entry here.\n if (invocation.state === 'result' && invocation.result !== undefined) {\n results.push({\n toolName: invocation.toolName,\n toolCallId: invocation.toolCallId || '',\n args: invocation.args || {},\n result: invocation.result,\n });\n }\n }\n }\n\n return results;\n}\n\n// Re-export extractTrajectory from core — it's called automatically by runEvals\n// for trajectory scorers, but users may still want it for custom use cases.\nexport { extractTrajectory } from '@mastra/core/evals';\n\n/**\n * Compares two trajectories and returns detailed comparison results.\n *\n * This is the core comparison logic used by trajectory scorers. It supports\n * strict and non-strict ordering, optional step data comparison, and loop detection.\n *\n * @param actual - The trajectory the agent actually took\n * @param expected - The expected trajectory to compare against\n * @param options - Comparison configuration options\n * @returns Detailed comparison results including match scores and diagnostics\n *\n * @example\n * ```ts\n * const result = compareTrajectories(\n * { steps: [{ stepType: 'tool_call', name: 'search' }, { stepType: 'tool_call', name: 'summarize' }] },\n * { steps: [{ stepType: 'tool_call', name: 'search' }, { stepType: 'tool_call', name: 'summarize' }] },\n * { ordering: 'strict' }\n * );\n * // result.score = 1.0\n * ```\n */\nexport function compareTrajectories(\n actual: Trajectory,\n expected: Trajectory | { steps: ExpectedStep[] },\n options: {\n ordering?: 'strict' | 'relaxed' | 'unordered';\n allowRepeatedSteps?: boolean;\n } = {},\n): TrajectoryComparisonResult {\n const { allowRepeatedSteps = true, ordering = 'relaxed' } = options;\n\n // Normalize expected to ExpectedStep[]. TrajectoryStep and ExpectedStep share\n // the same field names, so TrajectoryStep[] can be used directly as ExpectedStep[].\n // The only structural difference is `children` (TrajectoryStep[] vs TrajectoryExpectation),\n // but compareTrajectories doesn't recurse into children.\n const normalizedExpected: { steps: ExpectedStep[] } = {\n steps: expected.steps as ExpectedStep[],\n };\n\n if (normalizedExpected.steps.length === 0) {\n return {\n score: actual.steps.length === 0 ? 1 : 0,\n matchedSteps: 0,\n totalExpectedSteps: 0,\n totalActualSteps: actual.steps.length,\n missingSteps: [],\n extraSteps: actual.steps.map((s: TrajectoryStep) => s.name),\n outOfOrderSteps: [],\n repeatedSteps: [],\n };\n }\n\n const actualNames = actual.steps.map((s: TrajectoryStep) => s.name);\n\n // Detect repeated steps\n const nameCounts = new Map<string, number>();\n for (const name of actualNames) {\n nameCounts.set(name, (nameCounts.get(name) || 0) + 1);\n }\n const repeatedSteps = [...nameCounts.entries()]\n .filter(([_, count]: [string, number]) => count > 1)\n .map(([name]: [string, number]) => name);\n\n if (ordering === 'strict') {\n return compareStrictOrder(actual, normalizedExpected, { allowRepeatedSteps, repeatedSteps });\n }\n\n if (ordering === 'unordered') {\n return compareUnorderedPresence(actual, normalizedExpected, { allowRepeatedSteps, repeatedSteps });\n }\n\n return compareRelaxedOrder(actual, normalizedExpected, { allowRepeatedSteps, repeatedSteps });\n}\n\n/**\n * Result of comparing two trajectories.\n */\nexport type TrajectoryComparisonResult = {\n /** Overall match score from 0 to 1 */\n score: number;\n /** Number of expected steps that were matched */\n matchedSteps: number;\n /** Total number of expected steps */\n totalExpectedSteps: number;\n /** Total number of actual steps taken */\n totalActualSteps: number;\n /** Expected steps that were not found in the actual trajectory */\n missingSteps: string[];\n /** Actual steps that were not in the expected trajectory */\n extraSteps: string[];\n /** Steps that appear but not in the expected position */\n outOfOrderSteps: string[];\n /** Steps that were repeated (appeared more than once) */\n repeatedSteps: string[];\n};\n\nfunction compareStrictOrder(\n actual: Trajectory,\n expected: { steps: ExpectedStep[] },\n opts: { allowRepeatedSteps: boolean; repeatedSteps: string[] },\n): TrajectoryComparisonResult {\n const actualNames: string[] = actual.steps.map((s: TrajectoryStep) => s.name);\n const expectedNames: string[] = expected.steps.map((s: ExpectedStep) => s.name);\n\n // Strict: exact same sequence\n let matchedSteps = 0;\n const outOfOrderSteps: string[] = [];\n const matchedExpectedIndices = new Set<number>();\n const maxLen = Math.max(actualNames.length, expectedNames.length);\n\n for (let i = 0; i < maxLen; i++) {\n const actualName = actualNames[i];\n const expectedName = expectedNames[i];\n if (actualName === expectedName) {\n if (actual.steps[i] && expected.steps[i]) {\n if (expectedStepMatches(actual.steps[i]!, expected.steps[i]!)) {\n matchedSteps++;\n matchedExpectedIndices.add(i);\n }\n } else {\n matchedSteps++;\n matchedExpectedIndices.add(i);\n }\n } else if (actualName && expectedNames.includes(actualName)) {\n outOfOrderSteps.push(actualName);\n }\n }\n\n // Missing steps = expected steps that were not matched (accounts for stepType/data mismatches)\n const missingSteps: string[] = expectedNames.filter((_: string, i: number) => !matchedExpectedIndices.has(i));\n const extraSteps: string[] = actualNames.filter((name: string) => !expectedNames.includes(name));\n\n let score = matchedSteps / expected.steps.length;\n\n // Penalize extra steps in strict mode\n if (actualNames.length > expectedNames.length) {\n const extraPenalty = (actualNames.length - expectedNames.length) / expectedNames.length;\n score = Math.max(0, score - extraPenalty * 0.5);\n }\n\n // Penalize repeated steps if not allowed\n if (!opts.allowRepeatedSteps && opts.repeatedSteps.length > 0) {\n score = Math.max(0, score - opts.repeatedSteps.length * 0.1);\n }\n\n return {\n score: roundToTwoDecimals(Math.max(0, Math.min(1, score))),\n matchedSteps,\n totalExpectedSteps: expected.steps.length,\n totalActualSteps: actual.steps.length,\n missingSteps,\n extraSteps,\n outOfOrderSteps,\n repeatedSteps: opts.repeatedSteps,\n };\n}\n\nfunction compareRelaxedOrder(\n actual: Trajectory,\n expected: { steps: ExpectedStep[] },\n opts: { allowRepeatedSteps: boolean; repeatedSteps: string[] },\n): TrajectoryComparisonResult {\n const actualNames: string[] = actual.steps.map((s: TrajectoryStep) => s.name);\n const expectedNames: string[] = expected.steps.map((s: ExpectedStep) => s.name);\n\n // Relaxed: expected steps must appear in order but extra steps are allowed\n let matchedSteps = 0;\n let lastMatchedIndex = -1;\n const outOfOrderSteps: string[] = [];\n const matchedExpectedIndices = new Set<number>();\n\n for (let i = 0; i < expectedNames.length; i++) {\n const expectedName = expectedNames[i];\n let found = false;\n\n for (let j = lastMatchedIndex + 1; j < actualNames.length; j++) {\n if (actualNames[j] === expectedName) {\n if (actual.steps[j] && expected.steps[i]) {\n if (expectedStepMatches(actual.steps[j]!, expected.steps[i]!)) {\n matchedSteps++;\n lastMatchedIndex = j;\n matchedExpectedIndices.add(i);\n found = true;\n break;\n }\n } else {\n matchedSteps++;\n lastMatchedIndex = j;\n matchedExpectedIndices.add(i);\n found = true;\n break;\n }\n }\n }\n\n if (!found) {\n // Check if the step exists but is out of order\n if (actualNames.includes(expectedName!)) {\n outOfOrderSteps.push(expectedName!);\n }\n }\n }\n\n // Missing steps = expected steps that were not matched (by name + stepType + data, not just name)\n const missingSteps = expectedNames.filter((_, i) => !matchedExpectedIndices.has(i));\n const expectedSet = new Set(expectedNames);\n const extraSteps = actualNames.filter(name => !expectedSet.has(name));\n\n let score = matchedSteps / expected.steps.length;\n\n // Penalize repeated steps if not allowed\n if (!opts.allowRepeatedSteps && opts.repeatedSteps.length > 0) {\n score = Math.max(0, score - opts.repeatedSteps.length * 0.1);\n }\n\n return {\n score: roundToTwoDecimals(Math.max(0, Math.min(1, score))),\n matchedSteps,\n totalExpectedSteps: expected.steps.length,\n totalActualSteps: actual.steps.length,\n missingSteps,\n extraSteps,\n outOfOrderSteps,\n repeatedSteps: opts.repeatedSteps,\n };\n}\n\n/**\n * Fields on each ExpectedStep variant that are comparable data (not structural).\n * Used by `expectedStepMatches` to know which fields to compare when `compareData` is true.\n */\nconst COMPARABLE_FIELDS_BY_TYPE: Record<string, string[]> = {\n tool_call: ['toolArgs', 'toolResult', 'success'],\n mcp_tool_call: ['toolArgs', 'toolResult', 'mcpServer', 'success'],\n model_generation: ['modelId', 'promptTokens', 'completionTokens', 'finishReason'],\n agent_run: ['agentId'],\n workflow_step: ['stepId', 'status', 'output'],\n workflow_run: ['workflowId', 'status'],\n workflow_conditional: ['conditionCount', 'selectedSteps'],\n workflow_parallel: ['branchCount', 'parallelSteps'],\n workflow_loop: ['loopType', 'totalIterations'],\n workflow_sleep: ['sleepDurationMs', 'sleepType'],\n workflow_wait_event: ['eventName', 'eventReceived'],\n processor_run: ['processorId'],\n};\n\n/**\n * Check if an actual TrajectoryStep matches an ExpectedStep.\n * Matches by name, optionally by stepType, and auto-compares any variant-specific\n * fields that are present on the expected step.\n */\nfunction expectedStepMatches(actual: TrajectoryStep, expected: ExpectedStep): boolean {\n if (actual.name !== expected.name) return false;\n if (expected.stepType && actual.stepType !== expected.stepType) return false;\n\n if (expected.stepType) {\n const fields = COMPARABLE_FIELDS_BY_TYPE[expected.stepType] ?? [];\n for (const field of fields) {\n const expectedVal = (expected as any)[field];\n if (expectedVal === undefined) continue; // field not specified in expectation, skip\n const actualVal = (actual as any)[field];\n if (actualVal === undefined) return false;\n try {\n if (JSON.stringify(actualVal) !== JSON.stringify(expectedVal)) return false;\n } catch {\n return false;\n }\n }\n }\n\n return true;\n}\n\nfunction compareUnorderedPresence(\n actual: Trajectory,\n expected: { steps: ExpectedStep[] },\n opts: { allowRepeatedSteps: boolean; repeatedSteps: string[] },\n): TrajectoryComparisonResult {\n const actualNames: string[] = actual.steps.map((s: TrajectoryStep) => s.name);\n const expectedNames: string[] = expected.steps.map((s: ExpectedStep) => s.name);\n\n let matchedSteps = 0;\n const matchedExpectedIndices = new Set<number>();\n const usedIndices = new Set<number>();\n for (let i = 0; i < expected.steps.length; i++) {\n const expectedStep = expected.steps[i]!;\n for (let j = 0; j < actual.steps.length; j++) {\n if (!usedIndices.has(j) && expectedStepMatches(actual.steps[j]!, expectedStep)) {\n matchedSteps++;\n matchedExpectedIndices.add(i);\n usedIndices.add(j);\n break;\n }\n }\n }\n\n // Missing steps = expected steps that were not matched (accounts for stepType/data mismatches)\n const missingSteps = expectedNames.filter((_, i) => !matchedExpectedIndices.has(i));\n const expectedSet = new Set(expectedNames);\n const extraSteps = actualNames.filter(name => !expectedSet.has(name));\n\n let score = matchedSteps / expected.steps.length;\n\n // Penalize repeated steps if not allowed\n if (!opts.allowRepeatedSteps && opts.repeatedSteps.length > 0) {\n score = Math.max(0, score - opts.repeatedSteps.length * 0.1);\n }\n\n return {\n score: roundToTwoDecimals(Math.max(0, Math.min(1, score))),\n matchedSteps,\n totalExpectedSteps: expected.steps.length,\n totalActualSteps: actual.steps.length,\n missingSteps,\n extraSteps,\n outOfOrderSteps: [], // ordering not checked in unordered mode\n repeatedSteps: opts.repeatedSteps,\n };\n}\n\n// ─── Efficiency evaluation ───\n\n/**\n * Result of checking trajectory efficiency.\n */\nexport type TrajectoryEfficiencyResult = {\n /** Overall efficiency score from 0 to 1 */\n score: number;\n /** Total number of steps taken */\n totalSteps: number;\n /** Whether the step budget was exceeded */\n overStepBudget: boolean;\n /** Total tokens used across model_generation steps */\n totalTokens: number;\n /** Whether the token budget was exceeded */\n overTokenBudget: boolean;\n /** Total duration in milliseconds */\n totalDurationMs: number;\n /** Whether the duration budget was exceeded */\n overDurationBudget: boolean;\n /** Redundant calls detected (same tool + same args consecutively) */\n redundantCalls: Array<{ name: string; index: number }>;\n};\n\n/**\n * Evaluate trajectory efficiency against budgets and redundancy checks.\n * Throws when a configured budget is invalid or its required measurements are incomplete or invalid.\n */\nexport function checkTrajectoryEfficiency(\n trajectory: Trajectory,\n options: {\n maxSteps?: number;\n maxTotalTokens?: number;\n maxTotalDurationMs?: number;\n noRedundantCalls?: boolean;\n } = {},\n): TrajectoryEfficiencyResult {\n const { maxSteps, maxTotalTokens, maxTotalDurationMs, noRedundantCalls = true } = options;\n\n const isMeasurement = (value: number | undefined): value is number =>\n typeof value === 'number' && Number.isFinite(value) && value >= 0;\n for (const [name, limit] of Object.entries({ maxSteps, maxTotalTokens, maxTotalDurationMs })) {\n if (limit !== undefined && !isMeasurement(limit)) {\n throw new Error(`Invalid ${name} budget: expected a finite nonnegative number`);\n }\n }\n\n const totalSteps = trajectory.steps.length;\n\n // Count each model generation, including nested agents, without counting container aggregates.\n let totalTokens = 0;\n let modelGenerations = 0;\n const remaining = [...trajectory.steps];\n while (remaining.length > 0) {\n const step = remaining.pop()!;\n if (step.stepType === 'model_generation') {\n modelGenerations++;\n if (\n maxTotalTokens !== undefined &&\n (!isMeasurement(step.promptTokens) || !isMeasurement(step.completionTokens))\n ) {\n throw new Error(\n `Cannot evaluate token budget: model generation \"${step.name}\" has missing or invalid token counts`,\n );\n }\n totalTokens += (step.promptTokens ?? 0) + (step.completionTokens ?? 0);\n }\n if (step.children) {\n for (const child of step.children) remaining.push(child);\n }\n }\n if (maxTotalTokens !== undefined && (modelGenerations === 0 || !isMeasurement(totalTokens))) {\n throw new Error('Cannot evaluate token budget: missing model-generation measurements or invalid total tokens');\n }\n\n // A parent duration already covers its children; never add both.\n const totalDurationMs =\n trajectory.totalDurationMs ?? trajectory.steps.reduce((sum, s) => sum + (s.durationMs ?? 0), 0);\n if (maxTotalDurationMs !== undefined) {\n const hasDuration =\n trajectory.totalDurationMs !== undefined\n ? isMeasurement(trajectory.totalDurationMs)\n : trajectory.steps.length > 0 && trajectory.steps.every(step => isMeasurement(step.durationMs));\n if (!hasDuration || !isMeasurement(totalDurationMs)) {\n throw new Error('Cannot evaluate duration budget: missing or invalid duration measurements');\n }\n }\n\n // Detect redundant calls (same tool name + same args in consecutive calls)\n const redundantCalls: Array<{ name: string; index: number }> = [];\n if (noRedundantCalls) {\n for (let i = 1; i < trajectory.steps.length; i++) {\n const prev = trajectory.steps[i - 1]!;\n const curr = trajectory.steps[i]!;\n if (\n prev.name === curr.name &&\n prev.stepType === curr.stepType &&\n (prev.stepType === 'tool_call' || prev.stepType === 'mcp_tool_call')\n ) {\n const prevArgs = (prev as TrajectoryStep & { toolArgs?: Record<string, unknown> }).toolArgs;\n const currArgs = (curr as TrajectoryStep & { toolArgs?: Record<string, unknown> }).toolArgs;\n try {\n if (JSON.stringify(prevArgs) === JSON.stringify(currArgs)) {\n redundantCalls.push({ name: curr.name, index: i });\n }\n } catch {\n // If serialization fails, don't flag as redundant\n }\n }\n }\n }\n\n const overStepBudget = maxSteps !== undefined && totalSteps > maxSteps;\n const overTokenBudget = maxTotalTokens !== undefined && totalTokens > maxTotalTokens;\n const overDurationBudget = maxTotalDurationMs !== undefined && totalDurationMs > maxTotalDurationMs;\n\n // Calculate score: each dimension contributes equally\n const dimensions: number[] = [];\n\n if (maxSteps !== undefined) {\n dimensions.push(overStepBudget ? Math.max(0, 1 - (totalSteps - maxSteps) / maxSteps) : 1);\n }\n if (maxTotalTokens !== undefined) {\n dimensions.push(overTokenBudget ? Math.max(0, 1 - (totalTokens - maxTotalTokens) / maxTotalTokens) : 1);\n }\n if (maxTotalDurationMs !== undefined) {\n dimensions.push(\n overDurationBudget ? Math.max(0, 1 - (totalDurationMs - maxTotalDurationMs) / maxTotalDurationMs) : 1,\n );\n }\n if (noRedundantCalls) {\n dimensions.push(redundantCalls.length === 0 ? 1 : Math.max(0, 1 - redundantCalls.length * 0.2));\n }\n\n const score = dimensions.length > 0 ? dimensions.reduce((a, b) => a + b, 0) / dimensions.length : 1;\n\n return {\n score: roundToTwoDecimals(Math.max(0, Math.min(1, score))),\n totalSteps,\n overStepBudget,\n totalTokens,\n overTokenBudget,\n totalDurationMs,\n overDurationBudget,\n redundantCalls,\n };\n}\n\n// ─── Blacklist evaluation ───\n\n/**\n * Result of checking trajectory against a blacklist.\n */\nexport type TrajectoryBlacklistResult = {\n /** Score: 1.0 if clean, 0.0 if any violation found */\n score: number;\n /** Individual blacklisted tools that were found */\n violatedTools: string[];\n /** Blacklisted sequences that were found */\n violatedSequences: string[][];\n};\n\n/**\n * Check if a trajectory violates any blacklist rules.\n * Returns score 0.0 if any violation is found (hard fail).\n */\nexport function checkTrajectoryBlacklist(\n trajectory: Trajectory,\n options: {\n blacklistedTools?: string[];\n blacklistedSequences?: string[][];\n } = {},\n): TrajectoryBlacklistResult {\n const { blacklistedTools = [], blacklistedSequences = [] } = options;\n const violatedTools: string[] = [];\n const violatedSequences: string[][] = [];\n\n const stepNames = trajectory.steps.map(s => s.name);\n\n // Check blacklisted tools\n for (const forbidden of blacklistedTools) {\n if (stepNames.includes(forbidden)) {\n violatedTools.push(forbidden);\n }\n }\n\n // Check blacklisted sequences (contiguous subsequences)\n for (const sequence of blacklistedSequences) {\n if (sequence.length === 0) continue;\n for (let i = 0; i <= stepNames.length - sequence.length; i++) {\n let match = true;\n for (let j = 0; j < sequence.length; j++) {\n if (stepNames[i + j] !== sequence[j]) {\n match = false;\n break;\n }\n }\n if (match) {\n violatedSequences.push(sequence);\n break; // Only report each sequence once\n }\n }\n }\n\n const hasViolations = violatedTools.length > 0 || violatedSequences.length > 0;\n\n return {\n score: hasViolations ? 0 : 1,\n violatedTools,\n violatedSequences,\n };\n}\n\n// ─── Tool failure analysis ───\n\n/**\n * A detected tool failure pattern in the trajectory.\n */\nexport type ToolFailurePattern = {\n /** The tool name that experienced failure */\n toolName: string;\n /** Number of consecutive retries (same tool, same or similar args) */\n retryCount: number;\n /** Whether the agent fell back to a different tool after failures */\n fellBackToAlternative: boolean;\n /** The alternative tool used, if any */\n alternativeTool?: string;\n /** Whether any retry eventually succeeded */\n eventuallySucceeded: boolean;\n};\n\n/**\n * Result of analyzing tool failure patterns in a trajectory.\n */\nexport type ToolFailureAnalysisResult = {\n /** Score from 0 to 1 (lower = more failures/retries) */\n score: number;\n /** Tool failure patterns detected */\n patterns: ToolFailurePattern[];\n /** Total number of retries across all tools */\n totalRetries: number;\n /** Tools that exceeded the retry threshold */\n excessiveRetryTools: string[];\n};\n\n/**\n * Analyze tool failure and retry patterns in a trajectory.\n */\nexport function analyzeToolFailures(\n trajectory: Trajectory,\n options: {\n maxRetriesPerTool?: number;\n } = {},\n): ToolFailureAnalysisResult {\n const { maxRetriesPerTool = 2 } = options;\n const patterns: ToolFailurePattern[] = [];\n let totalRetries = 0;\n\n const toolCallSteps = trajectory.steps.filter(s => s.stepType === 'tool_call' || s.stepType === 'mcp_tool_call');\n\n if (toolCallSteps.length === 0) {\n return { score: 1, patterns: [], totalRetries: 0, excessiveRetryTools: [] };\n }\n\n // Group consecutive calls to the same tool as potential retry sequences\n let i = 0;\n while (i < toolCallSteps.length) {\n const currentTool = toolCallSteps[i]!;\n let retryCount = 0;\n let j = i + 1;\n\n // Count consecutive calls to the same tool\n // (toolCallSteps is pre-filtered to tool_call/mcp_tool_call, so no stepType checks needed)\n while (j < toolCallSteps.length && toolCallSteps[j]!.name === currentTool.name) {\n const prevStep = toolCallSteps[j - 1]! as TrajectoryStep & { success?: boolean };\n if (prevStep.success === false) {\n retryCount++;\n }\n j++;\n }\n\n if (retryCount > 0) {\n // Check if agent fell back to a different tool after retries\n const nextDifferentTool = j < toolCallSteps.length ? toolCallSteps[j] : undefined;\n const lastRetry = toolCallSteps[j - 1]! as TrajectoryStep & { success?: boolean };\n const lastSuccess = lastRetry.success !== false;\n\n patterns.push({\n toolName: currentTool.name,\n retryCount,\n fellBackToAlternative: nextDifferentTool !== undefined && !lastSuccess,\n alternativeTool: nextDifferentTool !== undefined && !lastSuccess ? nextDifferentTool.name : undefined,\n eventuallySucceeded: lastSuccess,\n });\n\n totalRetries += retryCount;\n }\n\n i = j;\n }\n\n // Score: penalize excessive retries\n const excessiveRetryTools = patterns.filter(p => p.retryCount > maxRetriesPerTool).map(p => p.toolName);\n\n let score = 1;\n if (toolCallSteps.length > 0) {\n // Each retry beyond the threshold costs more\n const excessRetries = patterns.reduce((sum, p) => sum + Math.max(0, p.retryCount - maxRetriesPerTool), 0);\n score = Math.max(0, 1 - excessRetries * 0.2);\n }\n\n return {\n score: roundToTwoDecimals(Math.max(0, Math.min(1, score))),\n patterns,\n totalRetries,\n excessiveRetryTools,\n };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAsDA,SAAgB,kCAAkC,SAAkC;CAClF,MAAM,UAAU,QAAQ;CAExB,IAAI,OAAO,YAAY,UACrB,OAAO;CAET,IAAI,MAAM,QAAQ,OAAO,GAAG;EAC1B,MAAM,YAAY,QAAQ,QAAO,MAAK,EAAE,SAAS,MAAM;EACvD,OAAO,UAAU,SAAS,IAAI,UAAU,UAAU,SAAS,EAAE,EAAE,QAAQ,KAAK;CAC9E;CACA,IAAI,OAAO,SAAS,YAAY,YAAY,QAAQ,YAAY,IAC9D,OAAO,QAAQ;CAEjB,IAAI,OAAO,SAAS,SAAS,YAAY,QAAQ,SAAS,IACxD,OAAO,QAAQ;CAEjB,IAAI,SAAS,SAAS,MAAM,QAAQ,QAAQ,KAAK,GAAG;EAElD,MAAM,YAAY,QAAQ,MAAM,QA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,GAAG,KAAK,IAAI,GAAG,KAAK,CAAC,CAAC;EACzD;EACA;EACA;CACF;AACF"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mastra/evals",
3
- "version": "1.10.2-alpha.0",
3
+ "version": "1.10.2-alpha.1",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "files": [
@@ -114,11 +114,11 @@
114
114
  "vitest": "4.1.10",
115
115
  "zod": "^4.4.3",
116
116
  "@internal/ai-sdk-v5": "0.0.79",
117
- "@internal/types-builder": "0.0.107",
118
117
  "@internal/llm-recorder": "0.0.68",
118
+ "@internal/lint": "0.0.132",
119
119
  "@internal/test-utils": "0.0.68",
120
- "@mastra/core": "1.67.0-alpha.1",
121
- "@internal/lint": "0.0.132"
120
+ "@internal/types-builder": "0.0.107",
121
+ "@mastra/core": "1.67.0-alpha.3"
122
122
  },
123
123
  "engines": {
124
124
  "node": ">=22.13.0"
@@ -1 +0,0 @@
1
- {"version":3,"file":"checks-64AonnEK.js","names":[],"sources":["../src/scorers/code/checks/index.ts"],"sourcesContent":["import { createScorer } from '@mastra/core/evals';\nimport stringSimilarity from 'string-similarity';\nimport { extractToolCalls, getTextContentFromMastraDBMessage } from '../../utils';\n\n// ─── Output Text Checks ───────────────────────────────────────────────────────\n\nexport interface IncludesOptions {\n /** Case-insensitive match (default: true) */\n ignoreCase?: boolean;\n}\n\n/**\n * Scores 1 if the agent's output text contains the expected substring, 0 otherwise.\n *\n * @example\n * ```ts\n * import { checks } from '@mastra/evals';\n * const scorer = checks.includes('sunny');\n * ```\n */\nexport function includes(expected: string, options: IncludesOptions = {}) {\n const { ignoreCase = true } = options;\n return createScorer({\n id: 'check-includes',\n name: 'Includes Check',\n description: `Checks if output includes \"${expected}\"`,\n type: 'agent',\n })\n .preprocess(async ({ run }) => {\n let output = run.output\n .filter(m => m.role === 'assistant')\n .map(m => getTextContentFromMastraDBMessage(m))\n .join(' ');\n let target = expected;\n if (ignoreCase) {\n output = output.toLowerCase();\n target = target.toLowerCase();\n }\n return { output, target, found: output.includes(target) };\n })\n .generateScore(({ results }) => {\n return results.preprocessStepResult?.found ? 1 : 0;\n });\n}\n\n/**\n * Scores 1 if the agent's output text does NOT contain the substring, 0 otherwise.\n *\n * @example\n * ```ts\n * import { checks } from '@mastra/evals';\n * const scorer = checks.excludes('error');\n * ```\n */\nexport function excludes(unwanted: string, options: IncludesOptions = {}) {\n const { ignoreCase = true } = options;\n return createScorer({\n id: 'check-excludes',\n name: 'Excludes Check',\n description: `Checks that output does not include \"${unwanted}\"`,\n type: 'agent',\n })\n .preprocess(async ({ run }) => {\n let output = run.output\n .filter(m => m.role === 'assistant')\n .map(m => getTextContentFromMastraDBMessage(m))\n .join(' ');\n let target = unwanted;\n if (ignoreCase) {\n output = output.toLowerCase();\n target = target.toLowerCase();\n }\n return { output, target, excluded: !output.includes(target) };\n })\n .generateScore(({ results }) => {\n return results.preprocessStepResult?.excluded ? 1 : 0;\n });\n}\n\n/**\n * Scores 1 if the output text exactly equals the expected string (after optional normalization).\n *\n * @example\n * ```ts\n * import { checks } from '@mastra/evals';\n * const scorer = checks.equals('Hello, world!');\n * ```\n */\nexport function equals(expected: string, options: IncludesOptions = {}) {\n const { ignoreCase = true } = options;\n return createScorer({\n id: 'check-equals',\n name: 'Equals Check',\n description: `Checks if output equals \"${expected}\"`,\n type: 'agent',\n })\n .preprocess(async ({ run }) => {\n let output = run.output\n .filter(m => m.role === 'assistant')\n .map(m => getTextContentFromMastraDBMessage(m))\n .join('');\n let target = expected;\n if (ignoreCase) {\n output = output.toLowerCase();\n target = target.toLowerCase();\n }\n return { output, target, isEqual: output === target };\n })\n .generateScore(({ results }) => {\n return results.preprocessStepResult?.isEqual ? 1 : 0;\n });\n}\n\nexport interface MatchesOptions {\n /** If true, the output must match the pattern exactly (anchored). Default: false (substring match). */\n exact?: boolean;\n}\n\n/**\n * Scores 1 if the output matches the given regular expression, 0 otherwise.\n *\n * @example\n * ```ts\n * import { checks } from '@mastra/evals';\n * const scorer = checks.matches(/\\d{1,3}°[FC]/);\n * ```\n */\nexport function matches(pattern: RegExp, options: MatchesOptions = {}) {\n const { exact = false } = options;\n return createScorer({\n id: 'check-matches',\n name: 'Matches Check',\n description: `Checks if output matches pattern ${pattern}`,\n type: 'agent',\n })\n .preprocess(async ({ run }) => {\n const output = run.output\n .filter(m => m.role === 'assistant')\n .map(m => getTextContentFromMastraDBMessage(m))\n .join('');\n const regex = exact ? new RegExp(`^${pattern.source}$`, pattern.flags) : pattern;\n const matched = regex.test(output);\n return { output, pattern: pattern.toString(), matched };\n })\n .generateScore(({ results }) => {\n return results.preprocessStepResult?.matched ? 1 : 0;\n });\n}\n\nexport interface SimilarityOptions {\n /** Minimum similarity threshold (0-1) to score 1. Default: 0.7 */\n threshold?: number;\n /** Case-insensitive comparison (default: true) */\n ignoreCase?: boolean;\n}\n\n/**\n * Returns the string similarity score (0-1) between the output and an expected string.\n * Useful for fuzzy matching when exact equality is too strict.\n *\n * @example\n * ```ts\n * import { checks } from '@mastra/evals';\n * const scorer = checks.similarity('Sunny, 72°F');\n * ```\n */\nexport function similarity(expected: string, options: SimilarityOptions = {}) {\n const { ignoreCase = true, threshold } = options;\n return createScorer({\n id: 'check-similarity',\n name: 'Similarity Check',\n description: `Checks string similarity to \"${expected}\"`,\n type: 'agent',\n })\n .preprocess(async ({ run }) => {\n let output = run.output\n .filter(m => m.role === 'assistant')\n .map(m => getTextContentFromMastraDBMessage(m))\n .join(' ');\n let target = expected;\n if (ignoreCase) {\n output = output.toLowerCase();\n target = target.toLowerCase();\n }\n const score = stringSimilarity.compareTwoStrings(output, target);\n return { output, target, score, threshold };\n })\n .generateScore(({ results }) => {\n const score = results.preprocessStepResult?.score ?? 0;\n const t = results.preprocessStepResult?.threshold;\n return t !== undefined ? (score >= t ? 1 : 0) : score;\n });\n}\n\n// ─── Tool Call Checks ─────────────────────────────────────────────────────────\n\nexport interface CalledToolOptions {\n /** Minimum number of times the tool must be called. Default: 1 */\n times?: number;\n}\n\n/**\n * Scores 1 if the agent called the specified tool (at least `times` times).\n *\n * @example\n * ```ts\n * import { checks } from '@mastra/evals';\n * const scorer = checks.calledTool('get_weather');\n * const twice = checks.calledTool('search', { times: 2 });\n * ```\n */\nexport function calledTool(toolName: string, options: CalledToolOptions = {}) {\n const { times = 1 } = options;\n return createScorer({\n id: 'check-called-tool',\n name: 'Called Tool Check',\n description: `Checks that \"${toolName}\" was called${times > 1 ? ` at least ${times} times` : ''}`,\n type: 'agent',\n })\n .preprocess(async ({ run }) => {\n const { tools } = extractToolCalls(run.output);\n const count = tools.filter(t => t === toolName).length;\n return { toolName, expectedTimes: times, actualCount: count, passed: count >= times };\n })\n .generateScore(({ results }) => {\n return results.preprocessStepResult?.passed ? 1 : 0;\n });\n}\n\n/**\n * Scores 1 if the agent did NOT call the specified tool.\n *\n * @example\n * ```ts\n * import { checks } from '@mastra/evals';\n * const scorer = checks.didNotCall('delete_user');\n * ```\n */\nexport function didNotCall(toolName: string) {\n return createScorer({\n id: 'check-did-not-call',\n name: 'Did Not Call Check',\n description: `Checks that \"${toolName}\" was NOT called`,\n type: 'agent',\n })\n .preprocess(async ({ run }) => {\n const { tools } = extractToolCalls(run.output);\n const count = tools.filter(t => t === toolName).length;\n return { toolName, count, passed: count === 0 };\n })\n .generateScore(({ results }) => {\n return results.preprocessStepResult?.passed ? 1 : 0;\n });\n}\n\n/**\n * Scores 1 if the tools were called in the specified order (relaxed: allows other calls in between).\n *\n * @example\n * ```ts\n * import { checks } from '@mastra/evals';\n * const scorer = checks.toolOrder(['search', 'summarize', 'respond']);\n * ```\n */\nexport function toolOrder(expectedOrder: string[]) {\n return createScorer({\n id: 'check-tool-order',\n name: 'Tool Order Check',\n description: `Checks tool call order: [${expectedOrder.join(' → ')}]`,\n type: 'agent',\n })\n .preprocess(async ({ run }) => {\n const { tools } = extractToolCalls(run.output);\n // Check that expectedOrder appears as a subsequence of tools\n let orderIndex = 0;\n for (const tool of tools) {\n if (orderIndex < expectedOrder.length && tool === expectedOrder[orderIndex]) {\n orderIndex++;\n }\n }\n const passed = orderIndex === expectedOrder.length;\n return { actualTools: tools, expectedOrder, passed };\n })\n .generateScore(({ results }) => {\n return results.preprocessStepResult?.passed ? 1 : 0;\n });\n}\n\n/**\n * Scores 1 if the agent used no more than `max` tool calls.\n *\n * @example\n * ```ts\n * import { checks } from '@mastra/evals';\n * const scorer = checks.maxToolCalls(5);\n * ```\n */\nexport function maxToolCalls(max: number) {\n return createScorer({\n id: 'check-max-tool-calls',\n name: 'Max Tool Calls Check',\n description: `Checks that no more than ${max} tool calls were made`,\n type: 'agent',\n })\n .preprocess(async ({ run }) => {\n const { tools } = extractToolCalls(run.output);\n return { count: tools.length, max, passed: tools.length <= max };\n })\n .generateScore(({ results }) => {\n return results.preprocessStepResult?.passed ? 1 : 0;\n });\n}\n\n/**\n * Scores 1 if the agent made no tool calls at all.\n *\n * @example\n * ```ts\n * import { checks } from '@mastra/evals';\n * const scorer = checks.usedNoTools();\n * ```\n */\nexport function usedNoTools() {\n return createScorer({\n id: 'check-used-no-tools',\n name: 'Used No Tools Check',\n description: 'Checks that no tools were called',\n type: 'agent',\n })\n .preprocess(async ({ run }) => {\n const { tools } = extractToolCalls(run.output);\n return { count: tools.length, passed: tools.length === 0 };\n })\n .generateScore(({ results }) => {\n return results.preprocessStepResult?.passed ? 1 : 0;\n });\n}\n\n/**\n * Scores 1 if none of the tool invocations resulted in an error state.\n * Checks for tool invocations with state other than 'result' (i.e., missing results).\n *\n * @example\n * ```ts\n * import { checks } from '@mastra/evals';\n * const scorer = checks.noToolErrors();\n * ```\n */\nexport function noToolErrors() {\n return createScorer({\n id: 'check-no-tool-errors',\n name: 'No Tool Errors Check',\n description: 'Checks that no tool calls resulted in errors',\n type: 'agent',\n })\n .preprocess(async ({ run }) => {\n const invocations = extractRawInvocations(run.output);\n const errorCount = invocations.filter(inv => inv.state === 'call' || (inv.result && inv.result.error)).length;\n return { errorCount, totalCalls: invocations.length, passed: errorCount === 0 };\n })\n .generateScore(({ results }) => {\n return results.preprocessStepResult?.passed ? 1 : 0;\n });\n}\n\n// ─── Internal helpers ──────────────────────────────────────────────────────────\n\nfunction extractRawInvocations(output: Parameters<typeof extractToolCalls>[0]) {\n const invocations: any[] = [];\n for (const message of output) {\n const legacy = message?.content?.toolInvocations;\n const fromParts = legacy\n ? undefined\n : (message?.content as any)?.parts\n ?.filter((p: any) => p.type === 'tool-invocation')\n .map((p: any) => p.toolInvocation);\n for (const inv of legacy ?? fromParts ?? []) {\n if (inv) invocations.push(inv);\n }\n }\n return invocations;\n}\n\n// ─── Convenience namespace ────────────────────────────────────────────────────\n\n/**\n * Quick Checks — composable micro-scorers for common assertions.\n *\n * These are zero-LLM, zero-ceremony scorers that plug into the existing\n * `scorers: [...]` array anywhere scorers are used. Internally they are\n * standard `createScorer()` instances with the same observability, storage,\n * and pipeline integration as any other scorer.\n *\n * @example\n * ```ts\n * import { checks } from '@mastra/evals';\n *\n * await runEvals({\n * data: [...],\n * target: myAgent,\n * scorers: [\n * checks.includes('sunny'),\n * checks.calledTool('get_weather'),\n * checks.toolOrder(['search', 'summarize']),\n * checks.noToolErrors(),\n * ],\n * });\n * ```\n */\nexport const checks = {\n includes,\n excludes,\n equals,\n matches,\n similarity,\n calledTool,\n didNotCall,\n toolOrder,\n maxToolCalls,\n usedNoTools,\n noToolErrors,\n};\n"],"mappings":";;;;;;;;;;;;;AAoBA,SAAgB,SAAS,UAAkB,UAA2B,CAAC,GAAG;CACxE,MAAM,EAAE,aAAa,SAAS;CAC9B,OAAO,aAAa;EAClB,IAAI;EACJ,MAAM;EACN,aAAa,8BAA8B,SAAS;EACpD,MAAM;CACR,CAAC,CAAC,CACC,WAAW,OAAO,EAAE,UAAU;EAC7B,IAAI,SAAS,IAAI,OACd,QAAO,MAAK,EAAE,SAAS,WAAW,CAAC,CACnC,KAAI,MAAK,kCAAkC,CAAC,CAAC,CAAC,CAC9C,KAAK,GAAG;EACX,IAAI,SAAS;EACb,IAAI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