@mastra/evals 1.1.2 → 1.2.0-alpha.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +50 -2
- package/LICENSE.md +15 -0
- package/dist/chunk-EVBNIL5M.js +606 -0
- package/dist/chunk-EVBNIL5M.js.map +1 -0
- package/dist/chunk-XRUR5PBK.cjs +632 -0
- package/dist/chunk-XRUR5PBK.cjs.map +1 -0
- package/dist/docs/SKILL.md +20 -19
- package/dist/docs/assets/SOURCE_MAP.json +1 -1
- package/dist/docs/references/docs-evals-built-in-scorers.md +2 -1
- package/dist/docs/references/docs-evals-overview.md +11 -16
- package/dist/docs/references/reference-evals-answer-relevancy.md +25 -25
- package/dist/docs/references/reference-evals-answer-similarity.md +33 -35
- package/dist/docs/references/reference-evals-bias.md +24 -24
- package/dist/docs/references/reference-evals-completeness.md +19 -20
- package/dist/docs/references/reference-evals-content-similarity.md +20 -20
- package/dist/docs/references/reference-evals-context-precision.md +36 -36
- package/dist/docs/references/reference-evals-context-relevance.md +136 -141
- package/dist/docs/references/reference-evals-faithfulness.md +24 -24
- package/dist/docs/references/reference-evals-hallucination.md +52 -69
- package/dist/docs/references/reference-evals-keyword-coverage.md +18 -18
- package/dist/docs/references/reference-evals-noise-sensitivity.md +167 -177
- package/dist/docs/references/reference-evals-prompt-alignment.md +111 -116
- package/dist/docs/references/reference-evals-scorer-utils.md +285 -105
- package/dist/docs/references/reference-evals-textual-difference.md +18 -18
- package/dist/docs/references/reference-evals-tone-consistency.md +19 -19
- package/dist/docs/references/reference-evals-tool-call-accuracy.md +165 -165
- package/dist/docs/references/reference-evals-toxicity.md +21 -21
- package/dist/docs/references/reference-evals-trajectory-accuracy.md +613 -0
- package/dist/scorers/code/index.d.ts +1 -0
- package/dist/scorers/code/index.d.ts.map +1 -1
- package/dist/scorers/code/trajectory/index.d.ts +147 -0
- package/dist/scorers/code/trajectory/index.d.ts.map +1 -0
- package/dist/scorers/llm/answer-similarity/index.d.ts +2 -2
- package/dist/scorers/llm/context-precision/index.d.ts +2 -2
- package/dist/scorers/llm/context-relevance/index.d.ts +1 -1
- package/dist/scorers/llm/faithfulness/index.d.ts +1 -1
- package/dist/scorers/llm/hallucination/index.d.ts +2 -2
- package/dist/scorers/llm/index.d.ts +1 -0
- package/dist/scorers/llm/index.d.ts.map +1 -1
- package/dist/scorers/llm/noise-sensitivity/index.d.ts +1 -1
- package/dist/scorers/llm/prompt-alignment/index.d.ts +5 -5
- package/dist/scorers/llm/tool-call-accuracy/index.d.ts +1 -1
- package/dist/scorers/llm/toxicity/index.d.ts +1 -1
- package/dist/scorers/llm/trajectory/index.d.ts +58 -0
- package/dist/scorers/llm/trajectory/index.d.ts.map +1 -0
- package/dist/scorers/llm/trajectory/prompts.d.ts +20 -0
- package/dist/scorers/llm/trajectory/prompts.d.ts.map +1 -0
- package/dist/scorers/prebuilt/index.cjs +638 -59
- package/dist/scorers/prebuilt/index.cjs.map +1 -1
- package/dist/scorers/prebuilt/index.js +578 -2
- package/dist/scorers/prebuilt/index.js.map +1 -1
- package/dist/scorers/utils.cjs +41 -17
- package/dist/scorers/utils.d.ts +171 -1
- package/dist/scorers/utils.d.ts.map +1 -1
- package/dist/scorers/utils.js +1 -1
- package/package.json +14 -11
- package/dist/chunk-OEOE7ZHN.js +0 -195
- package/dist/chunk-OEOE7ZHN.js.map +0 -1
- package/dist/chunk-W3U7MMDX.cjs +0 -212
- package/dist/chunk-W3U7MMDX.cjs.map +0 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,53 @@
|
|
|
1
1
|
# @mastra/evals
|
|
2
2
|
|
|
3
|
+
## 1.2.0-alpha.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- **Trajectory scorers**: Added scorers for evaluating agent and workflow execution paths. ([#14697](https://github.com/mastra-ai/mastra/pull/14697))
|
|
8
|
+
- `createTrajectoryScorerCode` — unified scorer that evaluates accuracy, efficiency, blacklist violations, and tool failure patterns in a single pass. Supports per-item expectations from datasets with static defaults. Nested `ExpectedStep.children` configs allow recursive evaluation with different rules per hierarchy level.
|
|
9
|
+
- `createTrajectoryAccuracyScorerCode` — deterministic accuracy scorer with strict, relaxed, and unordered ordering modes.
|
|
10
|
+
- `createTrajectoryAccuracyScorerLLM` — LLM-based scorer for semantic trajectory evaluation.
|
|
11
|
+
|
|
12
|
+
**Utility functions:**
|
|
13
|
+
- `extractTrajectory` / `extractWorkflowTrajectory` — Convert agent runs and workflow executions into structured trajectories
|
|
14
|
+
- `extractTrajectoryFromTrace` — Build hierarchical trajectories from observability trace spans, including nested agent/tool calls
|
|
15
|
+
- `compareTrajectories` — Compare actual vs. expected trajectories with configurable ordering and data matching. Accepts `ExpectedStep[]` for simpler expected step definitions
|
|
16
|
+
- `checkTrajectoryEfficiency` — Evaluate step counts, token usage, and duration against budgets
|
|
17
|
+
- `checkTrajectoryBlacklist` — Detect forbidden tools or tool sequences
|
|
18
|
+
- `analyzeToolFailures` — Detect retry patterns, fallbacks, and argument corrections
|
|
19
|
+
|
|
20
|
+
**Example — unified scorer with defaults:**
|
|
21
|
+
|
|
22
|
+
```ts
|
|
23
|
+
import { createTrajectoryScorerCode } from '@mastra/evals/scorers';
|
|
24
|
+
|
|
25
|
+
const scorer = createTrajectoryScorerCode({
|
|
26
|
+
defaults: {
|
|
27
|
+
ordering: 'strict',
|
|
28
|
+
steps: [
|
|
29
|
+
{ name: 'validate-input' },
|
|
30
|
+
{
|
|
31
|
+
name: 'research-agent',
|
|
32
|
+
stepType: 'agent_run',
|
|
33
|
+
children: {
|
|
34
|
+
ordering: 'unordered',
|
|
35
|
+
steps: [{ name: 'search' }, { name: 'summarize' }],
|
|
36
|
+
},
|
|
37
|
+
},
|
|
38
|
+
{ name: 'save-result' },
|
|
39
|
+
],
|
|
40
|
+
maxSteps: 10,
|
|
41
|
+
blacklistedTools: ['deleteAll'],
|
|
42
|
+
},
|
|
43
|
+
});
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
### Patch Changes
|
|
47
|
+
|
|
48
|
+
- Updated dependencies [[`dc9fc19`](https://github.com/mastra-ai/mastra/commit/dc9fc19da4437f6b508cc355f346a8856746a76b), [`260fe12`](https://github.com/mastra-ai/mastra/commit/260fe1295fe7354e39d6def2775e0797a7a277f0)]:
|
|
49
|
+
- @mastra/core@1.18.0-alpha.1
|
|
50
|
+
|
|
3
51
|
## 1.1.2
|
|
4
52
|
|
|
5
53
|
### Patch Changes
|
|
@@ -2369,7 +2417,7 @@
|
|
|
2369
2417
|
- 876b8a2: Rename difference metric to textual difference metric
|
|
2370
2418
|
- 1bbec77: Reorganized evals into nlp and llm
|
|
2371
2419
|
- 35764f4: Added workflow for eval tests
|
|
2372
|
-
- 8769a62: Split core into
|
|
2420
|
+
- 8769a62: Split core into separate entry files
|
|
2373
2421
|
- aea3c13: Fix evals export for llm and nlp
|
|
2374
2422
|
- 4f1d1a1: Enforce types ann cleanup package.json
|
|
2375
2423
|
- 202d404: Added instructions when generating evals
|
|
@@ -2783,7 +2831,7 @@
|
|
|
2783
2831
|
### Patch Changes
|
|
2784
2832
|
|
|
2785
2833
|
- 9625602: Use mastra core splitted bundles in other packages
|
|
2786
|
-
- 8769a62: Split core into
|
|
2834
|
+
- 8769a62: Split core into separate entry files
|
|
2787
2835
|
- Updated dependencies [30322ce]
|
|
2788
2836
|
- Updated dependencies [78eec7c]
|
|
2789
2837
|
- Updated dependencies [9625602]
|
package/LICENSE.md
CHANGED
|
@@ -1,3 +1,18 @@
|
|
|
1
|
+
Portions of this software are licensed as follows:
|
|
2
|
+
|
|
3
|
+
- All content that resides under any directory named "ee/" within this
|
|
4
|
+
repository, including but not limited to:
|
|
5
|
+
- `packages/core/src/auth/ee/`
|
|
6
|
+
- `packages/server/src/server/auth/ee/`
|
|
7
|
+
is licensed under the license defined in `ee/LICENSE`.
|
|
8
|
+
|
|
9
|
+
- All third-party components incorporated into the Mastra Software are
|
|
10
|
+
licensed under the original license provided by the owner of the
|
|
11
|
+
applicable component.
|
|
12
|
+
|
|
13
|
+
- Content outside of the above-mentioned directories or restrictions is
|
|
14
|
+
available under the "Apache License 2.0" as defined below.
|
|
15
|
+
|
|
1
16
|
# Apache License 2.0
|
|
2
17
|
|
|
3
18
|
Copyright (c) 2025 Kepler Software, Inc.
|
|
@@ -0,0 +1,606 @@
|
|
|
1
|
+
import { RequestContext } from '@mastra/core/request-context';
|
|
2
|
+
export { extractTrajectory } from '@mastra/core/evals';
|
|
3
|
+
|
|
4
|
+
// src/scorers/utils.ts
|
|
5
|
+
function getTextContentFromMastraDBMessage(message) {
|
|
6
|
+
if (typeof message.content.content === "string" && message.content.content !== "") {
|
|
7
|
+
return message.content.content;
|
|
8
|
+
}
|
|
9
|
+
if (message.content.parts && Array.isArray(message.content.parts)) {
|
|
10
|
+
const textParts = message.content.parts.filter((p) => p.type === "text");
|
|
11
|
+
return textParts.length > 0 ? textParts[textParts.length - 1]?.text || "" : "";
|
|
12
|
+
}
|
|
13
|
+
return "";
|
|
14
|
+
}
|
|
15
|
+
var roundToTwoDecimals = (num) => {
|
|
16
|
+
return Math.round((num + Number.EPSILON) * 100) / 100;
|
|
17
|
+
};
|
|
18
|
+
function isCloserTo(value, target1, target2) {
|
|
19
|
+
return Math.abs(value - target1) < Math.abs(value - target2);
|
|
20
|
+
}
|
|
21
|
+
var createTestRun = (input, output, additionalContext, requestContext) => {
|
|
22
|
+
return {
|
|
23
|
+
input: [{ role: "user", content: input }],
|
|
24
|
+
output: { role: "assistant", text: output },
|
|
25
|
+
additionalContext: additionalContext ?? {},
|
|
26
|
+
requestContext: requestContext ?? {}
|
|
27
|
+
};
|
|
28
|
+
};
|
|
29
|
+
var getUserMessageFromRunInput = (input) => {
|
|
30
|
+
const message = input?.inputMessages.find(({ role }) => role === "user");
|
|
31
|
+
return message ? getTextContentFromMastraDBMessage(message) : void 0;
|
|
32
|
+
};
|
|
33
|
+
var getSystemMessagesFromRunInput = (input) => {
|
|
34
|
+
const systemMessages = [];
|
|
35
|
+
if (input?.systemMessages) {
|
|
36
|
+
systemMessages.push(
|
|
37
|
+
...input.systemMessages.map((msg) => {
|
|
38
|
+
if (typeof msg.content === "string") {
|
|
39
|
+
return msg.content;
|
|
40
|
+
} else if (Array.isArray(msg.content)) {
|
|
41
|
+
return msg.content.filter((part) => part.type === "text").map((part) => part.text || "").join(" ");
|
|
42
|
+
}
|
|
43
|
+
return "";
|
|
44
|
+
}).filter((content) => content)
|
|
45
|
+
);
|
|
46
|
+
}
|
|
47
|
+
if (input?.taggedSystemMessages) {
|
|
48
|
+
Object.values(input.taggedSystemMessages).forEach((messages) => {
|
|
49
|
+
messages.forEach((msg) => {
|
|
50
|
+
if (typeof msg.content === "string") {
|
|
51
|
+
systemMessages.push(msg.content);
|
|
52
|
+
}
|
|
53
|
+
});
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
return systemMessages;
|
|
57
|
+
};
|
|
58
|
+
var getCombinedSystemPrompt = (input) => {
|
|
59
|
+
const systemMessages = getSystemMessagesFromRunInput(input);
|
|
60
|
+
return systemMessages.join("\n\n");
|
|
61
|
+
};
|
|
62
|
+
var getAssistantMessageFromRunOutput = (output) => {
|
|
63
|
+
const message = output?.find(({ role }) => role === "assistant");
|
|
64
|
+
return message ? getTextContentFromMastraDBMessage(message) : void 0;
|
|
65
|
+
};
|
|
66
|
+
var getReasoningFromRunOutput = (output) => {
|
|
67
|
+
if (!output) return void 0;
|
|
68
|
+
const message = output.find(({ role }) => role === "assistant");
|
|
69
|
+
if (!message) return void 0;
|
|
70
|
+
if (message.content.reasoning) {
|
|
71
|
+
return message.content.reasoning;
|
|
72
|
+
}
|
|
73
|
+
const reasoningParts = message.content.parts?.filter((p) => p.type === "reasoning");
|
|
74
|
+
if (reasoningParts && reasoningParts.length > 0) {
|
|
75
|
+
const reasoningTexts = reasoningParts.map((p) => {
|
|
76
|
+
if (p.details && Array.isArray(p.details)) {
|
|
77
|
+
return p.details.filter((d) => d.type === "text").map((d) => d.text).join("");
|
|
78
|
+
}
|
|
79
|
+
return p.reasoning || "";
|
|
80
|
+
}).filter(Boolean);
|
|
81
|
+
return reasoningTexts.length > 0 ? reasoningTexts.join("\n") : void 0;
|
|
82
|
+
}
|
|
83
|
+
return void 0;
|
|
84
|
+
};
|
|
85
|
+
var createToolInvocation = ({
|
|
86
|
+
toolCallId,
|
|
87
|
+
toolName,
|
|
88
|
+
args,
|
|
89
|
+
result,
|
|
90
|
+
state = "result"
|
|
91
|
+
}) => {
|
|
92
|
+
return {
|
|
93
|
+
toolCallId,
|
|
94
|
+
toolName,
|
|
95
|
+
args,
|
|
96
|
+
result,
|
|
97
|
+
state
|
|
98
|
+
};
|
|
99
|
+
};
|
|
100
|
+
function createTestMessage({
|
|
101
|
+
content,
|
|
102
|
+
role,
|
|
103
|
+
id = "test-message",
|
|
104
|
+
toolInvocations = []
|
|
105
|
+
}) {
|
|
106
|
+
return {
|
|
107
|
+
id,
|
|
108
|
+
role,
|
|
109
|
+
content: {
|
|
110
|
+
format: 2,
|
|
111
|
+
parts: [{ type: "text", text: content }],
|
|
112
|
+
content,
|
|
113
|
+
...toolInvocations.length > 0 && {
|
|
114
|
+
toolInvocations: toolInvocations.map((ti) => ({
|
|
115
|
+
toolCallId: ti.toolCallId,
|
|
116
|
+
toolName: ti.toolName,
|
|
117
|
+
args: ti.args,
|
|
118
|
+
result: ti.result,
|
|
119
|
+
state: ti.state
|
|
120
|
+
}))
|
|
121
|
+
}
|
|
122
|
+
},
|
|
123
|
+
createdAt: /* @__PURE__ */ new Date()
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
var createAgentTestRun = ({
|
|
127
|
+
inputMessages = [],
|
|
128
|
+
output,
|
|
129
|
+
rememberedMessages = [],
|
|
130
|
+
systemMessages = [],
|
|
131
|
+
taggedSystemMessages = {},
|
|
132
|
+
requestContext = new RequestContext(),
|
|
133
|
+
runId = crypto.randomUUID()
|
|
134
|
+
}) => {
|
|
135
|
+
return {
|
|
136
|
+
input: {
|
|
137
|
+
inputMessages,
|
|
138
|
+
rememberedMessages,
|
|
139
|
+
systemMessages,
|
|
140
|
+
taggedSystemMessages
|
|
141
|
+
},
|
|
142
|
+
output,
|
|
143
|
+
requestContext,
|
|
144
|
+
runId
|
|
145
|
+
};
|
|
146
|
+
};
|
|
147
|
+
var createTrajectoryTestRun = ({
|
|
148
|
+
inputMessages = [],
|
|
149
|
+
trajectory,
|
|
150
|
+
rememberedMessages = [],
|
|
151
|
+
systemMessages = [],
|
|
152
|
+
taggedSystemMessages = {},
|
|
153
|
+
requestContext = new RequestContext(),
|
|
154
|
+
runId = crypto.randomUUID(),
|
|
155
|
+
expectedTrajectory
|
|
156
|
+
}) => {
|
|
157
|
+
return {
|
|
158
|
+
input: {
|
|
159
|
+
inputMessages,
|
|
160
|
+
rememberedMessages,
|
|
161
|
+
systemMessages,
|
|
162
|
+
taggedSystemMessages
|
|
163
|
+
},
|
|
164
|
+
output: trajectory,
|
|
165
|
+
expectedTrajectory,
|
|
166
|
+
requestContext,
|
|
167
|
+
runId
|
|
168
|
+
};
|
|
169
|
+
};
|
|
170
|
+
function extractToolCalls(output) {
|
|
171
|
+
const toolCalls = [];
|
|
172
|
+
const toolCallInfos = [];
|
|
173
|
+
for (let messageIndex = 0; messageIndex < output.length; messageIndex++) {
|
|
174
|
+
const message = output[messageIndex];
|
|
175
|
+
if (message?.content?.toolInvocations) {
|
|
176
|
+
for (let invocationIndex = 0; invocationIndex < message.content.toolInvocations.length; invocationIndex++) {
|
|
177
|
+
const invocation = message.content.toolInvocations[invocationIndex];
|
|
178
|
+
if (invocation && invocation.toolName && (invocation.state === "result" || invocation.state === "call")) {
|
|
179
|
+
toolCalls.push(invocation.toolName);
|
|
180
|
+
toolCallInfos.push({
|
|
181
|
+
toolName: invocation.toolName,
|
|
182
|
+
toolCallId: invocation.toolCallId || `${messageIndex}-${invocationIndex}`,
|
|
183
|
+
messageIndex,
|
|
184
|
+
invocationIndex
|
|
185
|
+
});
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
return { tools: toolCalls, toolCallInfos };
|
|
191
|
+
}
|
|
192
|
+
var extractInputMessages = (runInput) => {
|
|
193
|
+
return runInput?.inputMessages?.map((msg) => getTextContentFromMastraDBMessage(msg)) || [];
|
|
194
|
+
};
|
|
195
|
+
var extractAgentResponseMessages = (runOutput) => {
|
|
196
|
+
return runOutput.filter((msg) => msg.role === "assistant").map((msg) => getTextContentFromMastraDBMessage(msg));
|
|
197
|
+
};
|
|
198
|
+
function extractToolResults(output) {
|
|
199
|
+
const results = [];
|
|
200
|
+
for (const message of output) {
|
|
201
|
+
const toolInvocations = message?.content?.toolInvocations;
|
|
202
|
+
if (!toolInvocations) continue;
|
|
203
|
+
for (const invocation of toolInvocations) {
|
|
204
|
+
if (invocation.state === "result" && invocation.result !== void 0) {
|
|
205
|
+
results.push({
|
|
206
|
+
toolName: invocation.toolName,
|
|
207
|
+
toolCallId: invocation.toolCallId || "",
|
|
208
|
+
args: invocation.args || {},
|
|
209
|
+
result: invocation.result
|
|
210
|
+
});
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
return results;
|
|
215
|
+
}
|
|
216
|
+
function compareTrajectories(actual, expected, options = {}) {
|
|
217
|
+
const { compareStepData = false, allowRepeatedSteps = true } = options;
|
|
218
|
+
const trajectoryStepKeys = [
|
|
219
|
+
"toolArgs",
|
|
220
|
+
"toolResult",
|
|
221
|
+
"agentId",
|
|
222
|
+
"modelId",
|
|
223
|
+
"durationMs",
|
|
224
|
+
"success",
|
|
225
|
+
"promptTokens",
|
|
226
|
+
"completionTokens"
|
|
227
|
+
];
|
|
228
|
+
const hasTrajectorySteps = expected.steps.length > 0 && expected.steps.some((s) => trajectoryStepKeys.some((k) => k in s));
|
|
229
|
+
let normalizedExpected;
|
|
230
|
+
if (hasTrajectorySteps) {
|
|
231
|
+
normalizedExpected = {
|
|
232
|
+
steps: expected.steps.map((s) => {
|
|
233
|
+
const stepData = getStepData(s);
|
|
234
|
+
const data = {};
|
|
235
|
+
if (stepData.input !== void 0) data.input = stepData.input;
|
|
236
|
+
if (stepData.output !== void 0) data.output = stepData.output;
|
|
237
|
+
return {
|
|
238
|
+
name: s.name,
|
|
239
|
+
stepType: s.stepType,
|
|
240
|
+
...Object.keys(data).length > 0 ? { data } : {}
|
|
241
|
+
};
|
|
242
|
+
})
|
|
243
|
+
};
|
|
244
|
+
} else {
|
|
245
|
+
normalizedExpected = expected;
|
|
246
|
+
}
|
|
247
|
+
let ordering = "relaxed";
|
|
248
|
+
if (options.ordering) {
|
|
249
|
+
ordering = options.ordering;
|
|
250
|
+
} else if (options.strictOrder) {
|
|
251
|
+
ordering = "strict";
|
|
252
|
+
}
|
|
253
|
+
if (normalizedExpected.steps.length === 0) {
|
|
254
|
+
return {
|
|
255
|
+
score: actual.steps.length === 0 ? 1 : 0,
|
|
256
|
+
matchedSteps: 0,
|
|
257
|
+
totalExpectedSteps: 0,
|
|
258
|
+
totalActualSteps: actual.steps.length,
|
|
259
|
+
missingSteps: [],
|
|
260
|
+
extraSteps: actual.steps.map((s) => s.name),
|
|
261
|
+
outOfOrderSteps: [],
|
|
262
|
+
repeatedSteps: []
|
|
263
|
+
};
|
|
264
|
+
}
|
|
265
|
+
const actualNames = actual.steps.map((s) => s.name);
|
|
266
|
+
const nameCounts = /* @__PURE__ */ new Map();
|
|
267
|
+
for (const name of actualNames) {
|
|
268
|
+
nameCounts.set(name, (nameCounts.get(name) || 0) + 1);
|
|
269
|
+
}
|
|
270
|
+
const repeatedSteps = [...nameCounts.entries()].filter(([_, count]) => count > 1).map(([name]) => name);
|
|
271
|
+
if (ordering === "strict") {
|
|
272
|
+
return compareStrictOrder(actual, normalizedExpected, { compareStepData, allowRepeatedSteps, repeatedSteps });
|
|
273
|
+
}
|
|
274
|
+
if (ordering === "unordered") {
|
|
275
|
+
return compareUnorderedPresence(actual, normalizedExpected, {
|
|
276
|
+
compareStepData,
|
|
277
|
+
allowRepeatedSteps,
|
|
278
|
+
repeatedSteps
|
|
279
|
+
});
|
|
280
|
+
}
|
|
281
|
+
return compareRelaxedOrder(actual, normalizedExpected, { compareStepData, allowRepeatedSteps, repeatedSteps });
|
|
282
|
+
}
|
|
283
|
+
function compareStrictOrder(actual, expected, opts) {
|
|
284
|
+
const actualNames = actual.steps.map((s) => s.name);
|
|
285
|
+
const expectedNames = expected.steps.map((s) => s.name);
|
|
286
|
+
let matchedSteps = 0;
|
|
287
|
+
const outOfOrderSteps = [];
|
|
288
|
+
const matchedExpectedIndices = /* @__PURE__ */ new Set();
|
|
289
|
+
const maxLen = Math.max(actualNames.length, expectedNames.length);
|
|
290
|
+
for (let i = 0; i < maxLen; i++) {
|
|
291
|
+
const actualName = actualNames[i];
|
|
292
|
+
const expectedName = expectedNames[i];
|
|
293
|
+
if (actualName === expectedName) {
|
|
294
|
+
if (opts.compareStepData && actual.steps[i] && expected.steps[i]) {
|
|
295
|
+
if (expectedStepMatches(actual.steps[i], expected.steps[i], true)) {
|
|
296
|
+
matchedSteps++;
|
|
297
|
+
matchedExpectedIndices.add(i);
|
|
298
|
+
}
|
|
299
|
+
} else if (actual.steps[i] && expected.steps[i]) {
|
|
300
|
+
if (expectedStepMatches(actual.steps[i], expected.steps[i], false)) {
|
|
301
|
+
matchedSteps++;
|
|
302
|
+
matchedExpectedIndices.add(i);
|
|
303
|
+
}
|
|
304
|
+
} else {
|
|
305
|
+
matchedSteps++;
|
|
306
|
+
matchedExpectedIndices.add(i);
|
|
307
|
+
}
|
|
308
|
+
} else if (actualName && expectedNames.includes(actualName)) {
|
|
309
|
+
outOfOrderSteps.push(actualName);
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
const missingSteps = expectedNames.filter((_, i) => !matchedExpectedIndices.has(i));
|
|
313
|
+
const extraSteps = actualNames.filter((name) => !expectedNames.includes(name));
|
|
314
|
+
let score = matchedSteps / expected.steps.length;
|
|
315
|
+
if (actualNames.length > expectedNames.length) {
|
|
316
|
+
const extraPenalty = (actualNames.length - expectedNames.length) / expectedNames.length;
|
|
317
|
+
score = Math.max(0, score - extraPenalty * 0.5);
|
|
318
|
+
}
|
|
319
|
+
if (!opts.allowRepeatedSteps && opts.repeatedSteps.length > 0) {
|
|
320
|
+
score = Math.max(0, score - opts.repeatedSteps.length * 0.1);
|
|
321
|
+
}
|
|
322
|
+
return {
|
|
323
|
+
score: roundToTwoDecimals(Math.max(0, Math.min(1, score))),
|
|
324
|
+
matchedSteps,
|
|
325
|
+
totalExpectedSteps: expected.steps.length,
|
|
326
|
+
totalActualSteps: actual.steps.length,
|
|
327
|
+
missingSteps,
|
|
328
|
+
extraSteps,
|
|
329
|
+
outOfOrderSteps,
|
|
330
|
+
repeatedSteps: opts.repeatedSteps
|
|
331
|
+
};
|
|
332
|
+
}
|
|
333
|
+
function compareRelaxedOrder(actual, expected, opts) {
|
|
334
|
+
const actualNames = actual.steps.map((s) => s.name);
|
|
335
|
+
const expectedNames = expected.steps.map((s) => s.name);
|
|
336
|
+
let matchedSteps = 0;
|
|
337
|
+
let lastMatchedIndex = -1;
|
|
338
|
+
const outOfOrderSteps = [];
|
|
339
|
+
const matchedExpectedIndices = /* @__PURE__ */ new Set();
|
|
340
|
+
for (let i = 0; i < expectedNames.length; i++) {
|
|
341
|
+
const expectedName = expectedNames[i];
|
|
342
|
+
let found = false;
|
|
343
|
+
for (let j = lastMatchedIndex + 1; j < actualNames.length; j++) {
|
|
344
|
+
if (actualNames[j] === expectedName) {
|
|
345
|
+
if (actual.steps[j] && expected.steps[i]) {
|
|
346
|
+
if (expectedStepMatches(actual.steps[j], expected.steps[i], opts.compareStepData)) {
|
|
347
|
+
matchedSteps++;
|
|
348
|
+
lastMatchedIndex = j;
|
|
349
|
+
matchedExpectedIndices.add(i);
|
|
350
|
+
found = true;
|
|
351
|
+
break;
|
|
352
|
+
}
|
|
353
|
+
} else {
|
|
354
|
+
matchedSteps++;
|
|
355
|
+
lastMatchedIndex = j;
|
|
356
|
+
matchedExpectedIndices.add(i);
|
|
357
|
+
found = true;
|
|
358
|
+
break;
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
if (!found) {
|
|
363
|
+
if (actualNames.includes(expectedName)) {
|
|
364
|
+
outOfOrderSteps.push(expectedName);
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
const missingSteps = expectedNames.filter((_, i) => !matchedExpectedIndices.has(i));
|
|
369
|
+
const expectedSet = new Set(expectedNames);
|
|
370
|
+
const extraSteps = actualNames.filter((name) => !expectedSet.has(name));
|
|
371
|
+
let score = matchedSteps / expected.steps.length;
|
|
372
|
+
if (!opts.allowRepeatedSteps && opts.repeatedSteps.length > 0) {
|
|
373
|
+
score = Math.max(0, score - opts.repeatedSteps.length * 0.1);
|
|
374
|
+
}
|
|
375
|
+
return {
|
|
376
|
+
score: roundToTwoDecimals(Math.max(0, Math.min(1, score))),
|
|
377
|
+
matchedSteps,
|
|
378
|
+
totalExpectedSteps: expected.steps.length,
|
|
379
|
+
totalActualSteps: actual.steps.length,
|
|
380
|
+
missingSteps,
|
|
381
|
+
extraSteps,
|
|
382
|
+
outOfOrderSteps,
|
|
383
|
+
repeatedSteps: opts.repeatedSteps
|
|
384
|
+
};
|
|
385
|
+
}
|
|
386
|
+
function getStepData(step) {
|
|
387
|
+
switch (step.stepType) {
|
|
388
|
+
case "tool_call":
|
|
389
|
+
case "mcp_tool_call":
|
|
390
|
+
return { input: step.toolArgs, output: step.toolResult };
|
|
391
|
+
case "workflow_step":
|
|
392
|
+
return { output: step.output };
|
|
393
|
+
default:
|
|
394
|
+
return {};
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
function expectedStepMatches(actual, expected, compareData) {
|
|
398
|
+
if (actual.name !== expected.name) return false;
|
|
399
|
+
if (expected.stepType && actual.stepType !== expected.stepType) return false;
|
|
400
|
+
if (compareData && expected.data) {
|
|
401
|
+
const actualData = getStepData(actual);
|
|
402
|
+
for (const [key, value] of Object.entries(expected.data)) {
|
|
403
|
+
const actualField = key === "input" ? actualData.input : key === "output" ? actualData.output : void 0;
|
|
404
|
+
if (actualField === void 0) return false;
|
|
405
|
+
try {
|
|
406
|
+
if (JSON.stringify(actualField) !== JSON.stringify(value)) return false;
|
|
407
|
+
} catch {
|
|
408
|
+
return false;
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
return true;
|
|
413
|
+
}
|
|
414
|
+
function compareUnorderedPresence(actual, expected, opts) {
|
|
415
|
+
const actualNames = actual.steps.map((s) => s.name);
|
|
416
|
+
const expectedNames = expected.steps.map((s) => s.name);
|
|
417
|
+
let matchedSteps = 0;
|
|
418
|
+
const matchedExpectedIndices = /* @__PURE__ */ new Set();
|
|
419
|
+
if (opts.compareStepData) {
|
|
420
|
+
const usedIndices = /* @__PURE__ */ new Set();
|
|
421
|
+
for (let i = 0; i < expected.steps.length; i++) {
|
|
422
|
+
const expectedStep = expected.steps[i];
|
|
423
|
+
for (let j = 0; j < actual.steps.length; j++) {
|
|
424
|
+
if (!usedIndices.has(j) && expectedStepMatches(actual.steps[j], expectedStep, true)) {
|
|
425
|
+
matchedSteps++;
|
|
426
|
+
matchedExpectedIndices.add(i);
|
|
427
|
+
usedIndices.add(j);
|
|
428
|
+
break;
|
|
429
|
+
}
|
|
430
|
+
}
|
|
431
|
+
}
|
|
432
|
+
} else {
|
|
433
|
+
const usedIndices = /* @__PURE__ */ new Set();
|
|
434
|
+
for (let i = 0; i < expected.steps.length; i++) {
|
|
435
|
+
const expectedStep = expected.steps[i];
|
|
436
|
+
for (let j = 0; j < actual.steps.length; j++) {
|
|
437
|
+
if (!usedIndices.has(j) && expectedStepMatches(actual.steps[j], expectedStep, false)) {
|
|
438
|
+
matchedSteps++;
|
|
439
|
+
matchedExpectedIndices.add(i);
|
|
440
|
+
usedIndices.add(j);
|
|
441
|
+
break;
|
|
442
|
+
}
|
|
443
|
+
}
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
const missingSteps = expectedNames.filter((_, i) => !matchedExpectedIndices.has(i));
|
|
447
|
+
const expectedSet = new Set(expectedNames);
|
|
448
|
+
const extraSteps = actualNames.filter((name) => !expectedSet.has(name));
|
|
449
|
+
let score = matchedSteps / expected.steps.length;
|
|
450
|
+
if (!opts.allowRepeatedSteps && opts.repeatedSteps.length > 0) {
|
|
451
|
+
score = Math.max(0, score - opts.repeatedSteps.length * 0.1);
|
|
452
|
+
}
|
|
453
|
+
return {
|
|
454
|
+
score: roundToTwoDecimals(Math.max(0, Math.min(1, score))),
|
|
455
|
+
matchedSteps,
|
|
456
|
+
totalExpectedSteps: expected.steps.length,
|
|
457
|
+
totalActualSteps: actual.steps.length,
|
|
458
|
+
missingSteps,
|
|
459
|
+
extraSteps,
|
|
460
|
+
outOfOrderSteps: [],
|
|
461
|
+
// ordering not checked in unordered mode
|
|
462
|
+
repeatedSteps: opts.repeatedSteps
|
|
463
|
+
};
|
|
464
|
+
}
|
|
465
|
+
function checkTrajectoryEfficiency(trajectory, options = {}) {
|
|
466
|
+
const { maxSteps, maxTotalTokens, maxTotalDurationMs, noRedundantCalls = true } = options;
|
|
467
|
+
const totalSteps = trajectory.steps.length;
|
|
468
|
+
let totalTokens = 0;
|
|
469
|
+
for (const step of trajectory.steps) {
|
|
470
|
+
if (step.stepType === "model_generation") {
|
|
471
|
+
totalTokens += (step.promptTokens ?? 0) + (step.completionTokens ?? 0);
|
|
472
|
+
}
|
|
473
|
+
}
|
|
474
|
+
const totalDurationMs = trajectory.totalDurationMs ?? trajectory.steps.reduce((sum, s) => sum + (s.durationMs ?? 0), 0);
|
|
475
|
+
const redundantCalls = [];
|
|
476
|
+
if (noRedundantCalls) {
|
|
477
|
+
for (let i = 1; i < trajectory.steps.length; i++) {
|
|
478
|
+
const prev = trajectory.steps[i - 1];
|
|
479
|
+
const curr = trajectory.steps[i];
|
|
480
|
+
if (prev.name === curr.name && prev.stepType === curr.stepType && (prev.stepType === "tool_call" || prev.stepType === "mcp_tool_call")) {
|
|
481
|
+
const prevArgs = prev.toolArgs;
|
|
482
|
+
const currArgs = curr.toolArgs;
|
|
483
|
+
try {
|
|
484
|
+
if (JSON.stringify(prevArgs) === JSON.stringify(currArgs)) {
|
|
485
|
+
redundantCalls.push({ name: curr.name, index: i });
|
|
486
|
+
}
|
|
487
|
+
} catch {
|
|
488
|
+
}
|
|
489
|
+
}
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
const overStepBudget = maxSteps !== void 0 && totalSteps > maxSteps;
|
|
493
|
+
const overTokenBudget = maxTotalTokens !== void 0 && totalTokens > maxTotalTokens;
|
|
494
|
+
const overDurationBudget = maxTotalDurationMs !== void 0 && totalDurationMs > maxTotalDurationMs;
|
|
495
|
+
const dimensions = [];
|
|
496
|
+
if (maxSteps !== void 0) {
|
|
497
|
+
dimensions.push(overStepBudget ? Math.max(0, 1 - (totalSteps - maxSteps) / maxSteps) : 1);
|
|
498
|
+
}
|
|
499
|
+
if (maxTotalTokens !== void 0) {
|
|
500
|
+
dimensions.push(overTokenBudget ? Math.max(0, 1 - (totalTokens - maxTotalTokens) / maxTotalTokens) : 1);
|
|
501
|
+
}
|
|
502
|
+
if (maxTotalDurationMs !== void 0) {
|
|
503
|
+
dimensions.push(
|
|
504
|
+
overDurationBudget ? Math.max(0, 1 - (totalDurationMs - maxTotalDurationMs) / maxTotalDurationMs) : 1
|
|
505
|
+
);
|
|
506
|
+
}
|
|
507
|
+
if (noRedundantCalls) {
|
|
508
|
+
dimensions.push(redundantCalls.length === 0 ? 1 : Math.max(0, 1 - redundantCalls.length * 0.2));
|
|
509
|
+
}
|
|
510
|
+
const score = dimensions.length > 0 ? dimensions.reduce((a, b) => a + b, 0) / dimensions.length : 1;
|
|
511
|
+
return {
|
|
512
|
+
score: roundToTwoDecimals(Math.max(0, Math.min(1, score))),
|
|
513
|
+
totalSteps,
|
|
514
|
+
overStepBudget,
|
|
515
|
+
totalTokens,
|
|
516
|
+
overTokenBudget,
|
|
517
|
+
totalDurationMs,
|
|
518
|
+
overDurationBudget,
|
|
519
|
+
redundantCalls
|
|
520
|
+
};
|
|
521
|
+
}
|
|
522
|
+
function checkTrajectoryBlacklist(trajectory, options = {}) {
|
|
523
|
+
const { blacklistedTools = [], blacklistedSequences = [] } = options;
|
|
524
|
+
const violatedTools = [];
|
|
525
|
+
const violatedSequences = [];
|
|
526
|
+
const stepNames = trajectory.steps.map((s) => s.name);
|
|
527
|
+
for (const forbidden of blacklistedTools) {
|
|
528
|
+
if (stepNames.includes(forbidden)) {
|
|
529
|
+
violatedTools.push(forbidden);
|
|
530
|
+
}
|
|
531
|
+
}
|
|
532
|
+
for (const sequence of blacklistedSequences) {
|
|
533
|
+
if (sequence.length === 0) continue;
|
|
534
|
+
for (let i = 0; i <= stepNames.length - sequence.length; i++) {
|
|
535
|
+
let match = true;
|
|
536
|
+
for (let j = 0; j < sequence.length; j++) {
|
|
537
|
+
if (stepNames[i + j] !== sequence[j]) {
|
|
538
|
+
match = false;
|
|
539
|
+
break;
|
|
540
|
+
}
|
|
541
|
+
}
|
|
542
|
+
if (match) {
|
|
543
|
+
violatedSequences.push(sequence);
|
|
544
|
+
break;
|
|
545
|
+
}
|
|
546
|
+
}
|
|
547
|
+
}
|
|
548
|
+
const hasViolations = violatedTools.length > 0 || violatedSequences.length > 0;
|
|
549
|
+
return {
|
|
550
|
+
score: hasViolations ? 0 : 1,
|
|
551
|
+
violatedTools,
|
|
552
|
+
violatedSequences
|
|
553
|
+
};
|
|
554
|
+
}
|
|
555
|
+
function analyzeToolFailures(trajectory, options = {}) {
|
|
556
|
+
const { maxRetriesPerTool = 2 } = options;
|
|
557
|
+
const patterns = [];
|
|
558
|
+
let totalRetries = 0;
|
|
559
|
+
const toolCallSteps = trajectory.steps.filter((s) => s.stepType === "tool_call" || s.stepType === "mcp_tool_call");
|
|
560
|
+
if (toolCallSteps.length === 0) {
|
|
561
|
+
return { score: 1, patterns: [], totalRetries: 0, excessiveRetryTools: [] };
|
|
562
|
+
}
|
|
563
|
+
let i = 0;
|
|
564
|
+
while (i < toolCallSteps.length) {
|
|
565
|
+
const currentTool = toolCallSteps[i];
|
|
566
|
+
let retryCount = 0;
|
|
567
|
+
let j = i + 1;
|
|
568
|
+
while (j < toolCallSteps.length && toolCallSteps[j].name === currentTool.name) {
|
|
569
|
+
const prevStep = toolCallSteps[j - 1];
|
|
570
|
+
if (prevStep.success === false) {
|
|
571
|
+
retryCount++;
|
|
572
|
+
}
|
|
573
|
+
j++;
|
|
574
|
+
}
|
|
575
|
+
if (retryCount > 0) {
|
|
576
|
+
const nextDifferentTool = j < toolCallSteps.length ? toolCallSteps[j] : void 0;
|
|
577
|
+
const lastRetry = toolCallSteps[j - 1];
|
|
578
|
+
const lastSuccess = lastRetry.success !== false;
|
|
579
|
+
patterns.push({
|
|
580
|
+
toolName: currentTool.name,
|
|
581
|
+
retryCount,
|
|
582
|
+
fellBackToAlternative: nextDifferentTool !== void 0 && !lastSuccess,
|
|
583
|
+
alternativeTool: nextDifferentTool !== void 0 && !lastSuccess ? nextDifferentTool.name : void 0,
|
|
584
|
+
eventuallySucceeded: lastSuccess
|
|
585
|
+
});
|
|
586
|
+
totalRetries += retryCount;
|
|
587
|
+
}
|
|
588
|
+
i = j;
|
|
589
|
+
}
|
|
590
|
+
const excessiveRetryTools = patterns.filter((p) => p.retryCount > maxRetriesPerTool).map((p) => p.toolName);
|
|
591
|
+
let score = 1;
|
|
592
|
+
if (toolCallSteps.length > 0) {
|
|
593
|
+
const excessRetries = patterns.reduce((sum, p) => sum + Math.max(0, p.retryCount - maxRetriesPerTool), 0);
|
|
594
|
+
score = Math.max(0, 1 - excessRetries * 0.2);
|
|
595
|
+
}
|
|
596
|
+
return {
|
|
597
|
+
score: roundToTwoDecimals(Math.max(0, Math.min(1, score))),
|
|
598
|
+
patterns,
|
|
599
|
+
totalRetries,
|
|
600
|
+
excessiveRetryTools
|
|
601
|
+
};
|
|
602
|
+
}
|
|
603
|
+
|
|
604
|
+
export { analyzeToolFailures, checkTrajectoryBlacklist, checkTrajectoryEfficiency, compareTrajectories, createAgentTestRun, createTestMessage, createTestRun, createToolInvocation, createTrajectoryTestRun, extractAgentResponseMessages, extractInputMessages, extractToolCalls, extractToolResults, getAssistantMessageFromRunOutput, getCombinedSystemPrompt, getReasoningFromRunOutput, getSystemMessagesFromRunInput, getTextContentFromMastraDBMessage, getUserMessageFromRunInput, isCloserTo, roundToTwoDecimals };
|
|
605
|
+
//# sourceMappingURL=chunk-EVBNIL5M.js.map
|
|
606
|
+
//# sourceMappingURL=chunk-EVBNIL5M.js.map
|