@mastra/core 0.15.3-alpha.9 → 0.16.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 +77 -0
- package/dist/agent/index.cjs +8 -8
- package/dist/agent/index.d.ts.map +1 -1
- package/dist/agent/index.js +1 -1
- package/dist/agent/input-processor/index.cjs +6 -6
- package/dist/agent/input-processor/index.js +1 -1
- package/dist/{chunk-K6UMYGK5.cjs → chunk-3CJXUAA2.cjs} +2 -2
- package/dist/{chunk-K6UMYGK5.cjs.map → chunk-3CJXUAA2.cjs.map} +1 -1
- package/dist/{chunk-AWES6LTC.cjs → chunk-7NKIBBCV.cjs} +4 -4
- package/dist/{chunk-AWES6LTC.cjs.map → chunk-7NKIBBCV.cjs.map} +1 -1
- package/dist/chunk-ABJOUEVA.cjs +10 -0
- package/dist/chunk-ABJOUEVA.cjs.map +1 -0
- package/dist/{chunk-VZYIKGJ6.cjs → chunk-AHJY7RHO.cjs} +4 -4
- package/dist/{chunk-VZYIKGJ6.cjs.map → chunk-AHJY7RHO.cjs.map} +1 -1
- package/dist/{chunk-AEE54ND4.js → chunk-AJXRKF6P.js} +13 -7
- package/dist/chunk-AJXRKF6P.js.map +1 -0
- package/dist/{chunk-Z637KSJF.cjs → chunk-BHB4PDNV.cjs} +4 -4
- package/dist/{chunk-Z637KSJF.cjs.map → chunk-BHB4PDNV.cjs.map} +1 -1
- package/dist/{chunk-PDODMDSQ.js → chunk-FAGDTY7M.js} +3 -3
- package/dist/{chunk-PDODMDSQ.js.map → chunk-FAGDTY7M.js.map} +1 -1
- package/dist/{chunk-QQO4SF3C.js → chunk-FCNVKNBE.js} +16 -6
- package/dist/chunk-FCNVKNBE.js.map +1 -0
- package/dist/{chunk-FTAXL6TR.cjs → chunk-FTDWZOPH.cjs} +25 -15
- package/dist/chunk-FTDWZOPH.cjs.map +1 -0
- package/dist/chunk-IHVB4C5U.cjs +4 -0
- package/dist/{chunk-X4RMXTXF.cjs.map → chunk-IHVB4C5U.cjs.map} +1 -1
- package/dist/{chunk-ZHFYYSVY.cjs → chunk-JBCB54LR.cjs} +17 -11
- package/dist/chunk-JBCB54LR.cjs.map +1 -0
- package/dist/{chunk-YGW2WEJ5.js → chunk-JTU7FZ5O.js} +2 -2
- package/dist/{chunk-YGW2WEJ5.js.map → chunk-JTU7FZ5O.js.map} +1 -1
- package/dist/{chunk-EZCMCWSC.cjs → chunk-KPEYRIL4.cjs} +130 -60
- package/dist/chunk-KPEYRIL4.cjs.map +1 -0
- package/dist/chunk-NLNKQD2T.js +7 -0
- package/dist/chunk-NLNKQD2T.js.map +1 -0
- package/dist/{chunk-65ZPZGFH.cjs → chunk-OX7VSHIN.cjs} +4 -4
- package/dist/{chunk-65ZPZGFH.cjs.map → chunk-OX7VSHIN.cjs.map} +1 -1
- package/dist/{chunk-4VU6A5XE.js → chunk-QQIBOVFQ.js} +3 -3
- package/dist/{chunk-4VU6A5XE.js.map → chunk-QQIBOVFQ.js.map} +1 -1
- package/dist/{chunk-AVPW677Z.js → chunk-RRBS3FVU.js} +116 -46
- package/dist/chunk-RRBS3FVU.js.map +1 -0
- package/dist/chunk-S43VACTO.js +3 -0
- package/dist/{chunk-GXSERFAG.js.map → chunk-S43VACTO.js.map} +1 -1
- package/dist/{chunk-R7K2QO7M.js → chunk-TLJPVRO5.js} +4 -4
- package/dist/{chunk-R7K2QO7M.js.map → chunk-TLJPVRO5.js.map} +1 -1
- package/dist/chunk-UM2UEDFU.js +665 -0
- package/dist/chunk-UM2UEDFU.js.map +1 -0
- package/dist/chunk-XDSZO4OG.cjs +673 -0
- package/dist/chunk-XDSZO4OG.cjs.map +1 -0
- package/dist/{chunk-VZS4UVKF.js → chunk-YFAHGS45.js} +3 -3
- package/dist/{chunk-VZS4UVKF.js.map → chunk-YFAHGS45.js.map} +1 -1
- package/dist/index.cjs +31 -31
- package/dist/index.js +8 -8
- package/dist/loop/index.cjs +2 -2
- package/dist/loop/index.js +1 -1
- package/dist/loop/workflow/llm-execution.d.ts +4 -4
- package/dist/loop/workflow/outer-llm-step.d.ts +2 -2
- package/dist/loop/workflow/schema.d.ts +2 -2
- package/dist/mastra/hooks.d.ts +3 -1
- package/dist/mastra/hooks.d.ts.map +1 -1
- package/dist/mastra/index.cjs +2 -2
- package/dist/mastra/index.js +1 -1
- package/dist/network/index.cjs +2 -2
- package/dist/network/index.js +1 -1
- package/dist/network/vNext/index.cjs +15 -15
- package/dist/network/vNext/index.js +2 -2
- package/dist/processors/index.cjs +8 -8
- package/dist/processors/index.js +2 -2
- package/dist/relevance/index.cjs +4 -4
- package/dist/relevance/index.js +1 -1
- package/dist/scores/hooks.d.ts +3 -1
- package/dist/scores/hooks.d.ts.map +1 -1
- package/dist/scores/index.cjs +29 -638
- package/dist/scores/index.cjs.map +1 -1
- package/dist/scores/index.js +1 -636
- package/dist/scores/index.js.map +1 -1
- package/dist/scores/types.d.ts +80 -0
- package/dist/scores/types.d.ts.map +1 -1
- package/dist/server/index.cjs +2 -2
- package/dist/server/index.js +1 -1
- package/dist/storage/base.d.ts +2 -2
- package/dist/storage/base.d.ts.map +1 -1
- package/dist/storage/domains/memory/inmemory.d.ts.map +1 -1
- package/dist/storage/index.cjs +8 -6
- package/dist/storage/index.cjs.map +1 -1
- package/dist/storage/index.js +4 -2
- package/dist/storage/index.js.map +1 -1
- package/dist/stream/base/output.d.ts.map +1 -1
- package/dist/stream/index.cjs +3 -3
- package/dist/stream/index.js +1 -1
- package/dist/telemetry/index.cjs +7 -7
- package/dist/telemetry/index.js +1 -1
- package/dist/tools/index.d.ts +1 -0
- package/dist/tools/index.d.ts.map +1 -1
- package/dist/tools/ui-types.d.ts +39 -0
- package/dist/tools/ui-types.d.ts.map +1 -0
- package/dist/tts/index.cjs +2 -2
- package/dist/tts/index.js +1 -1
- package/dist/voice/index.cjs +4 -4
- package/dist/voice/index.js +1 -1
- package/dist/workflows/constants.cjs +6 -2
- package/dist/workflows/constants.d.ts +1 -0
- package/dist/workflows/constants.d.ts.map +1 -1
- package/dist/workflows/constants.js +1 -1
- package/dist/workflows/default.d.ts +6 -3
- package/dist/workflows/default.d.ts.map +1 -1
- package/dist/workflows/evented/execution-engine.d.ts +1 -0
- package/dist/workflows/evented/execution-engine.d.ts.map +1 -1
- package/dist/workflows/evented/index.cjs +10 -10
- package/dist/workflows/evented/index.js +1 -1
- package/dist/workflows/evented/step-executor.d.ts.map +1 -1
- package/dist/workflows/execution-engine.d.ts +1 -0
- package/dist/workflows/execution-engine.d.ts.map +1 -1
- package/dist/workflows/index.cjs +10 -10
- package/dist/workflows/index.js +1 -1
- package/dist/workflows/legacy/index.cjs +22 -22
- package/dist/workflows/legacy/index.js +1 -1
- package/dist/workflows/step.d.ts +2 -1
- package/dist/workflows/step.d.ts.map +1 -1
- package/dist/workflows/types.d.ts +13 -1
- package/dist/workflows/types.d.ts.map +1 -1
- package/dist/workflows/workflow.d.ts +9 -1
- package/dist/workflows/workflow.d.ts.map +1 -1
- package/package.json +4 -4
- package/dist/chunk-AEE54ND4.js.map +0 -1
- package/dist/chunk-AVPW677Z.js.map +0 -1
- package/dist/chunk-EZCMCWSC.cjs.map +0 -1
- package/dist/chunk-FTAXL6TR.cjs.map +0 -1
- package/dist/chunk-GK5V7YTQ.js +0 -6
- package/dist/chunk-GK5V7YTQ.js.map +0 -1
- package/dist/chunk-GXSERFAG.js +0 -3
- package/dist/chunk-NFXTYMWZ.cjs +0 -8
- package/dist/chunk-NFXTYMWZ.cjs.map +0 -1
- package/dist/chunk-QQO4SF3C.js.map +0 -1
- package/dist/chunk-X4RMXTXF.cjs +0 -4
- package/dist/chunk-ZHFYYSVY.cjs.map +0 -1
|
@@ -0,0 +1,673 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var chunkKPEYRIL4_cjs = require('./chunk-KPEYRIL4.cjs');
|
|
4
|
+
var chunkC73WLCY3_cjs = require('./chunk-C73WLCY3.cjs');
|
|
5
|
+
var zod = require('zod');
|
|
6
|
+
var crypto = require('crypto');
|
|
7
|
+
|
|
8
|
+
var scoringExtractStepResultSchema = zod.z.record(zod.z.string(), zod.z.any()).optional();
|
|
9
|
+
var scoringValueSchema = zod.z.number();
|
|
10
|
+
var scoreResultSchema = zod.z.object({
|
|
11
|
+
result: zod.z.record(zod.z.string(), zod.z.any()).optional(),
|
|
12
|
+
score: scoringValueSchema,
|
|
13
|
+
prompt: zod.z.string().optional()
|
|
14
|
+
});
|
|
15
|
+
var saveScorePayloadSchema = zod.z.object({
|
|
16
|
+
runId: zod.z.string(),
|
|
17
|
+
scorerId: zod.z.string(),
|
|
18
|
+
entityId: zod.z.string(),
|
|
19
|
+
score: zod.z.number(),
|
|
20
|
+
input: zod.z.any().optional(),
|
|
21
|
+
output: zod.z.any(),
|
|
22
|
+
source: zod.z.enum(["LIVE", "TEST"]),
|
|
23
|
+
entityType: zod.z.enum(["AGENT", "WORKFLOW"]).optional(),
|
|
24
|
+
traceId: zod.z.string().optional(),
|
|
25
|
+
scorer: zod.z.record(zod.z.string(), zod.z.any()).optional(),
|
|
26
|
+
preprocessStepResult: zod.z.record(zod.z.string(), zod.z.any()).optional(),
|
|
27
|
+
extractStepResult: zod.z.record(zod.z.string(), zod.z.any()).optional(),
|
|
28
|
+
analyzeStepResult: zod.z.record(zod.z.string(), zod.z.any()).optional(),
|
|
29
|
+
reason: zod.z.string().optional(),
|
|
30
|
+
metadata: zod.z.record(zod.z.string(), zod.z.any()).optional(),
|
|
31
|
+
preprocessPrompt: zod.z.string().optional(),
|
|
32
|
+
extractPrompt: zod.z.string().optional(),
|
|
33
|
+
generateScorePrompt: zod.z.string().optional(),
|
|
34
|
+
generateReasonPrompt: zod.z.string().optional(),
|
|
35
|
+
analyzePrompt: zod.z.string().optional(),
|
|
36
|
+
additionalContext: zod.z.record(zod.z.string(), zod.z.any()).optional(),
|
|
37
|
+
runtimeContext: zod.z.record(zod.z.string(), zod.z.any()).optional(),
|
|
38
|
+
entity: zod.z.record(zod.z.string(), zod.z.any()).optional(),
|
|
39
|
+
resourceId: zod.z.string().optional(),
|
|
40
|
+
threadId: zod.z.string().optional()
|
|
41
|
+
});
|
|
42
|
+
var MastraScorer = class _MastraScorer {
|
|
43
|
+
constructor(config, steps = [], originalPromptObjects = /* @__PURE__ */ new Map()) {
|
|
44
|
+
this.config = config;
|
|
45
|
+
this.steps = steps;
|
|
46
|
+
this.originalPromptObjects = originalPromptObjects;
|
|
47
|
+
}
|
|
48
|
+
get name() {
|
|
49
|
+
return this.config.name;
|
|
50
|
+
}
|
|
51
|
+
get description() {
|
|
52
|
+
return this.config.description;
|
|
53
|
+
}
|
|
54
|
+
get judge() {
|
|
55
|
+
return this.config.judge;
|
|
56
|
+
}
|
|
57
|
+
preprocess(stepDef) {
|
|
58
|
+
const isPromptObj = this.isPromptObject(stepDef);
|
|
59
|
+
if (isPromptObj) {
|
|
60
|
+
const promptObj = stepDef;
|
|
61
|
+
this.originalPromptObjects.set("preprocess", promptObj);
|
|
62
|
+
}
|
|
63
|
+
return new _MastraScorer(
|
|
64
|
+
this.config,
|
|
65
|
+
[
|
|
66
|
+
...this.steps,
|
|
67
|
+
{
|
|
68
|
+
name: "preprocess",
|
|
69
|
+
definition: stepDef,
|
|
70
|
+
isPromptObject: isPromptObj
|
|
71
|
+
}
|
|
72
|
+
],
|
|
73
|
+
new Map(this.originalPromptObjects)
|
|
74
|
+
);
|
|
75
|
+
}
|
|
76
|
+
analyze(stepDef) {
|
|
77
|
+
const isPromptObj = this.isPromptObject(stepDef);
|
|
78
|
+
if (isPromptObj) {
|
|
79
|
+
const promptObj = stepDef;
|
|
80
|
+
this.originalPromptObjects.set("analyze", promptObj);
|
|
81
|
+
}
|
|
82
|
+
return new _MastraScorer(
|
|
83
|
+
this.config,
|
|
84
|
+
[
|
|
85
|
+
...this.steps,
|
|
86
|
+
{
|
|
87
|
+
name: "analyze",
|
|
88
|
+
definition: isPromptObj ? void 0 : stepDef,
|
|
89
|
+
isPromptObject: isPromptObj
|
|
90
|
+
}
|
|
91
|
+
],
|
|
92
|
+
new Map(this.originalPromptObjects)
|
|
93
|
+
);
|
|
94
|
+
}
|
|
95
|
+
generateScore(stepDef) {
|
|
96
|
+
const isPromptObj = this.isPromptObject(stepDef);
|
|
97
|
+
if (isPromptObj) {
|
|
98
|
+
const promptObj = stepDef;
|
|
99
|
+
this.originalPromptObjects.set("generateScore", promptObj);
|
|
100
|
+
}
|
|
101
|
+
return new _MastraScorer(
|
|
102
|
+
this.config,
|
|
103
|
+
[
|
|
104
|
+
...this.steps,
|
|
105
|
+
{
|
|
106
|
+
name: "generateScore",
|
|
107
|
+
definition: isPromptObj ? void 0 : stepDef,
|
|
108
|
+
isPromptObject: isPromptObj
|
|
109
|
+
}
|
|
110
|
+
],
|
|
111
|
+
new Map(this.originalPromptObjects)
|
|
112
|
+
);
|
|
113
|
+
}
|
|
114
|
+
generateReason(stepDef) {
|
|
115
|
+
const isPromptObj = this.isPromptObject(stepDef);
|
|
116
|
+
if (isPromptObj) {
|
|
117
|
+
const promptObj = stepDef;
|
|
118
|
+
this.originalPromptObjects.set("generateReason", promptObj);
|
|
119
|
+
}
|
|
120
|
+
return new _MastraScorer(
|
|
121
|
+
this.config,
|
|
122
|
+
[
|
|
123
|
+
...this.steps,
|
|
124
|
+
{
|
|
125
|
+
name: "generateReason",
|
|
126
|
+
definition: isPromptObj ? void 0 : stepDef,
|
|
127
|
+
isPromptObject: isPromptObj
|
|
128
|
+
}
|
|
129
|
+
],
|
|
130
|
+
new Map(this.originalPromptObjects)
|
|
131
|
+
);
|
|
132
|
+
}
|
|
133
|
+
get hasGenerateScore() {
|
|
134
|
+
return this.steps.some((step) => step.name === "generateScore");
|
|
135
|
+
}
|
|
136
|
+
async run(input) {
|
|
137
|
+
if (!this.hasGenerateScore) {
|
|
138
|
+
throw new chunkC73WLCY3_cjs.MastraError({
|
|
139
|
+
id: "MASTR_SCORER_FAILED_TO_RUN_MISSING_GENERATE_SCORE",
|
|
140
|
+
domain: "SCORER" /* SCORER */,
|
|
141
|
+
category: "USER" /* USER */,
|
|
142
|
+
text: `Cannot execute pipeline without generateScore() step`,
|
|
143
|
+
details: {
|
|
144
|
+
scorerId: this.config.name,
|
|
145
|
+
steps: this.steps.map((s) => s.name).join(", ")
|
|
146
|
+
}
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
const { tracingContext } = input;
|
|
150
|
+
let runId = input.runId;
|
|
151
|
+
if (!runId) {
|
|
152
|
+
runId = crypto.randomUUID();
|
|
153
|
+
}
|
|
154
|
+
const run = { ...input, runId };
|
|
155
|
+
const workflow = this.toMastraWorkflow();
|
|
156
|
+
const workflowRun = await workflow.createRunAsync();
|
|
157
|
+
const workflowResult = await workflowRun.start({
|
|
158
|
+
inputData: {
|
|
159
|
+
run
|
|
160
|
+
},
|
|
161
|
+
tracingContext
|
|
162
|
+
});
|
|
163
|
+
if (workflowResult.status === "failed") {
|
|
164
|
+
throw new chunkC73WLCY3_cjs.MastraError({
|
|
165
|
+
id: "MASTR_SCORER_FAILED_TO_RUN_WORKFLOW_FAILED",
|
|
166
|
+
domain: "SCORER" /* SCORER */,
|
|
167
|
+
category: "USER" /* USER */,
|
|
168
|
+
text: `Scorer Run Failed: ${workflowResult.error}`,
|
|
169
|
+
details: {
|
|
170
|
+
scorerId: this.config.name,
|
|
171
|
+
steps: this.steps.map((s) => s.name).join(", ")
|
|
172
|
+
}
|
|
173
|
+
});
|
|
174
|
+
}
|
|
175
|
+
return this.transformToScorerResult({ workflowResult, originalInput: run });
|
|
176
|
+
}
|
|
177
|
+
isPromptObject(stepDef) {
|
|
178
|
+
if (typeof stepDef === "object" && "description" in stepDef && "createPrompt" in stepDef && !("outputSchema" in stepDef)) {
|
|
179
|
+
return true;
|
|
180
|
+
}
|
|
181
|
+
const isOtherPromptObject = typeof stepDef === "object" && "description" in stepDef && "outputSchema" in stepDef && "createPrompt" in stepDef;
|
|
182
|
+
return isOtherPromptObject;
|
|
183
|
+
}
|
|
184
|
+
getSteps() {
|
|
185
|
+
return this.steps.map((step) => ({
|
|
186
|
+
name: step.name,
|
|
187
|
+
type: step.isPromptObject ? "prompt" : "function",
|
|
188
|
+
description: step.definition.description
|
|
189
|
+
}));
|
|
190
|
+
}
|
|
191
|
+
toMastraWorkflow() {
|
|
192
|
+
const workflowSteps = this.steps.map((scorerStep) => {
|
|
193
|
+
return chunkKPEYRIL4_cjs.createStep({
|
|
194
|
+
id: scorerStep.name,
|
|
195
|
+
description: `Scorer step: ${scorerStep.name}`,
|
|
196
|
+
inputSchema: zod.z.any(),
|
|
197
|
+
outputSchema: zod.z.any(),
|
|
198
|
+
execute: async ({ inputData, getInitData, tracingContext }) => {
|
|
199
|
+
const { accumulatedResults = {}, generatedPrompts = {} } = inputData;
|
|
200
|
+
const { run } = getInitData();
|
|
201
|
+
const context = this.createScorerContext(scorerStep.name, run, accumulatedResults);
|
|
202
|
+
let stepResult;
|
|
203
|
+
let newGeneratedPrompts = generatedPrompts;
|
|
204
|
+
if (scorerStep.isPromptObject) {
|
|
205
|
+
const { result, prompt } = await this.executePromptStep(scorerStep, tracingContext, context);
|
|
206
|
+
stepResult = result;
|
|
207
|
+
newGeneratedPrompts = {
|
|
208
|
+
...generatedPrompts,
|
|
209
|
+
[`${scorerStep.name}Prompt`]: prompt
|
|
210
|
+
};
|
|
211
|
+
} else {
|
|
212
|
+
stepResult = await this.executeFunctionStep(scorerStep, context);
|
|
213
|
+
}
|
|
214
|
+
const newAccumulatedResults = {
|
|
215
|
+
...accumulatedResults,
|
|
216
|
+
[`${scorerStep.name}StepResult`]: stepResult
|
|
217
|
+
};
|
|
218
|
+
return {
|
|
219
|
+
stepResult,
|
|
220
|
+
accumulatedResults: newAccumulatedResults,
|
|
221
|
+
generatedPrompts: newGeneratedPrompts
|
|
222
|
+
};
|
|
223
|
+
}
|
|
224
|
+
});
|
|
225
|
+
});
|
|
226
|
+
const workflow = chunkKPEYRIL4_cjs.createWorkflow({
|
|
227
|
+
id: `scorer-${this.config.name}`,
|
|
228
|
+
description: this.config.description,
|
|
229
|
+
inputSchema: zod.z.object({
|
|
230
|
+
run: zod.z.any()
|
|
231
|
+
// ScorerRun
|
|
232
|
+
}),
|
|
233
|
+
outputSchema: zod.z.object({
|
|
234
|
+
run: zod.z.any(),
|
|
235
|
+
score: zod.z.number(),
|
|
236
|
+
reason: zod.z.string().optional(),
|
|
237
|
+
preprocessResult: zod.z.any().optional(),
|
|
238
|
+
analyzeResult: zod.z.any().optional(),
|
|
239
|
+
preprocessPrompt: zod.z.string().optional(),
|
|
240
|
+
analyzePrompt: zod.z.string().optional(),
|
|
241
|
+
generateScorePrompt: zod.z.string().optional(),
|
|
242
|
+
generateReasonPrompt: zod.z.string().optional()
|
|
243
|
+
})
|
|
244
|
+
});
|
|
245
|
+
let chainedWorkflow = workflow;
|
|
246
|
+
for (const step of workflowSteps) {
|
|
247
|
+
chainedWorkflow = chainedWorkflow.then(step);
|
|
248
|
+
}
|
|
249
|
+
return chainedWorkflow.commit();
|
|
250
|
+
}
|
|
251
|
+
createScorerContext(stepName, run, accumulatedResults) {
|
|
252
|
+
if (stepName === "generateReason") {
|
|
253
|
+
const score = accumulatedResults.generateScoreStepResult;
|
|
254
|
+
return { run, results: accumulatedResults, score };
|
|
255
|
+
}
|
|
256
|
+
return { run, results: accumulatedResults };
|
|
257
|
+
}
|
|
258
|
+
async executeFunctionStep(scorerStep, context) {
|
|
259
|
+
return await scorerStep.definition(context);
|
|
260
|
+
}
|
|
261
|
+
async executePromptStep(scorerStep, tracingContext, context) {
|
|
262
|
+
const originalStep = this.originalPromptObjects.get(scorerStep.name);
|
|
263
|
+
if (!originalStep) {
|
|
264
|
+
throw new Error(`Step "${scorerStep.name}" is not a prompt object`);
|
|
265
|
+
}
|
|
266
|
+
const prompt = await originalStep.createPrompt(context);
|
|
267
|
+
const model = originalStep.judge?.model ?? this.config.judge?.model;
|
|
268
|
+
const instructions = originalStep.judge?.instructions ?? this.config.judge?.instructions;
|
|
269
|
+
if (!model || !instructions) {
|
|
270
|
+
throw new chunkC73WLCY3_cjs.MastraError({
|
|
271
|
+
id: "MASTR_SCORER_FAILED_TO_RUN_MISSING_MODEL_OR_INSTRUCTIONS",
|
|
272
|
+
domain: "SCORER" /* SCORER */,
|
|
273
|
+
category: "USER" /* USER */,
|
|
274
|
+
text: `Step "${scorerStep.name}" requires a model and instructions`,
|
|
275
|
+
details: {
|
|
276
|
+
scorerId: this.config.name,
|
|
277
|
+
step: scorerStep.name
|
|
278
|
+
}
|
|
279
|
+
});
|
|
280
|
+
}
|
|
281
|
+
const judge = new chunkKPEYRIL4_cjs.Agent({ name: "judge", model, instructions });
|
|
282
|
+
if (scorerStep.name === "generateScore") {
|
|
283
|
+
let result;
|
|
284
|
+
if (model.specificationVersion === "v2") {
|
|
285
|
+
result = await judge.generateVNext(prompt, {
|
|
286
|
+
output: zod.z.object({ score: zod.z.number() }),
|
|
287
|
+
tracingContext
|
|
288
|
+
});
|
|
289
|
+
} else {
|
|
290
|
+
result = await judge.generate(prompt, {
|
|
291
|
+
output: zod.z.object({ score: zod.z.number() }),
|
|
292
|
+
tracingContext
|
|
293
|
+
});
|
|
294
|
+
}
|
|
295
|
+
return { result: result.object.score, prompt };
|
|
296
|
+
} else if (scorerStep.name === "generateReason") {
|
|
297
|
+
let result;
|
|
298
|
+
if (model.specificationVersion === "v2") {
|
|
299
|
+
result = await judge.generateVNext(prompt, { tracingContext });
|
|
300
|
+
} else {
|
|
301
|
+
result = await judge.generate(prompt, { tracingContext });
|
|
302
|
+
}
|
|
303
|
+
return { result: result.text, prompt };
|
|
304
|
+
} else {
|
|
305
|
+
const promptStep = originalStep;
|
|
306
|
+
let result;
|
|
307
|
+
if (model.specificationVersion === "v2") {
|
|
308
|
+
result = await judge.generateVNext(prompt, {
|
|
309
|
+
output: promptStep.outputSchema,
|
|
310
|
+
tracingContext
|
|
311
|
+
});
|
|
312
|
+
} else {
|
|
313
|
+
result = await judge.generate(prompt, {
|
|
314
|
+
output: promptStep.outputSchema,
|
|
315
|
+
tracingContext
|
|
316
|
+
});
|
|
317
|
+
}
|
|
318
|
+
return { result: result.object, prompt };
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
transformToScorerResult({
|
|
322
|
+
workflowResult,
|
|
323
|
+
originalInput
|
|
324
|
+
}) {
|
|
325
|
+
const finalStepResult = workflowResult.result;
|
|
326
|
+
const accumulatedResults = finalStepResult?.accumulatedResults || {};
|
|
327
|
+
const generatedPrompts = finalStepResult?.generatedPrompts || {};
|
|
328
|
+
return {
|
|
329
|
+
...originalInput,
|
|
330
|
+
score: accumulatedResults.generateScoreStepResult,
|
|
331
|
+
generateScorePrompt: generatedPrompts.generateScorePrompt,
|
|
332
|
+
reason: accumulatedResults.generateReasonStepResult,
|
|
333
|
+
generateReasonPrompt: generatedPrompts.generateReasonPrompt,
|
|
334
|
+
preprocessStepResult: accumulatedResults.preprocessStepResult,
|
|
335
|
+
preprocessPrompt: generatedPrompts.preprocessPrompt,
|
|
336
|
+
analyzeStepResult: accumulatedResults.analyzeStepResult,
|
|
337
|
+
analyzePrompt: generatedPrompts.analyzePrompt
|
|
338
|
+
};
|
|
339
|
+
}
|
|
340
|
+
};
|
|
341
|
+
function createScorer(config) {
|
|
342
|
+
return new MastraScorer({
|
|
343
|
+
name: config.name,
|
|
344
|
+
description: config.description,
|
|
345
|
+
judge: config.judge
|
|
346
|
+
});
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
// src/scores/run-experiment/scorerAccumulator.ts
|
|
350
|
+
var ScoreAccumulator = class {
|
|
351
|
+
flatScores = {};
|
|
352
|
+
workflowScores = {};
|
|
353
|
+
stepScores = {};
|
|
354
|
+
addScores(scorerResults) {
|
|
355
|
+
const isTargetWorkflowAndHasStepScores = "steps" in scorerResults;
|
|
356
|
+
if (isTargetWorkflowAndHasStepScores) {
|
|
357
|
+
this.addNestedScores(scorerResults);
|
|
358
|
+
} else {
|
|
359
|
+
this.addFlatScores(scorerResults);
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
addFlatScores(scorerResults) {
|
|
363
|
+
for (const [scorerName, result] of Object.entries(scorerResults)) {
|
|
364
|
+
if (!this.flatScores[scorerName]) {
|
|
365
|
+
this.flatScores[scorerName] = [];
|
|
366
|
+
}
|
|
367
|
+
this.flatScores[scorerName].push(result.score);
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
addNestedScores(scorerResults) {
|
|
371
|
+
if ("workflow" in scorerResults && scorerResults.workflow) {
|
|
372
|
+
for (const [scorerName, result] of Object.entries(scorerResults.workflow)) {
|
|
373
|
+
if (!this.workflowScores[scorerName]) {
|
|
374
|
+
this.workflowScores[scorerName] = [];
|
|
375
|
+
}
|
|
376
|
+
this.workflowScores[scorerName].push(result.score);
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
if ("steps" in scorerResults && scorerResults.steps) {
|
|
380
|
+
for (const [stepId, stepResults] of Object.entries(scorerResults.steps)) {
|
|
381
|
+
if (!this.stepScores[stepId]) {
|
|
382
|
+
this.stepScores[stepId] = {};
|
|
383
|
+
}
|
|
384
|
+
for (const [scorerName, result] of Object.entries(stepResults)) {
|
|
385
|
+
if (!this.stepScores[stepId][scorerName]) {
|
|
386
|
+
this.stepScores[stepId][scorerName] = [];
|
|
387
|
+
}
|
|
388
|
+
this.stepScores[stepId][scorerName].push(result.score);
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
addStepScores(stepScorerResults) {
|
|
394
|
+
for (const [stepId, stepResults] of Object.entries(stepScorerResults)) {
|
|
395
|
+
if (!this.stepScores[stepId]) {
|
|
396
|
+
this.stepScores[stepId] = {};
|
|
397
|
+
}
|
|
398
|
+
for (const [scorerName, result] of Object.entries(stepResults)) {
|
|
399
|
+
if (!this.stepScores[stepId][scorerName]) {
|
|
400
|
+
this.stepScores[stepId][scorerName] = [];
|
|
401
|
+
}
|
|
402
|
+
this.stepScores[stepId][scorerName].push(result.score);
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
getAverageScores() {
|
|
407
|
+
const result = {};
|
|
408
|
+
for (const [scorerName, scoreArray] of Object.entries(this.flatScores)) {
|
|
409
|
+
result[scorerName] = this.getAverageScore(scoreArray);
|
|
410
|
+
}
|
|
411
|
+
if (Object.keys(this.workflowScores).length > 0) {
|
|
412
|
+
result.workflow = {};
|
|
413
|
+
for (const [scorerName, scoreArray] of Object.entries(this.workflowScores)) {
|
|
414
|
+
result.workflow[scorerName] = this.getAverageScore(scoreArray);
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
if (Object.keys(this.stepScores).length > 0) {
|
|
418
|
+
result.steps = {};
|
|
419
|
+
for (const [stepId, stepScorers] of Object.entries(this.stepScores)) {
|
|
420
|
+
result.steps[stepId] = {};
|
|
421
|
+
for (const [scorerName, scoreArray] of Object.entries(stepScorers)) {
|
|
422
|
+
result.steps[stepId][scorerName] = this.getAverageScore(scoreArray);
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
}
|
|
426
|
+
return result;
|
|
427
|
+
}
|
|
428
|
+
getAverageScore(scoreArray) {
|
|
429
|
+
if (scoreArray.length > 0) {
|
|
430
|
+
return scoreArray.reduce((a, b) => a + b, 0) / scoreArray.length;
|
|
431
|
+
} else {
|
|
432
|
+
return 0;
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
};
|
|
436
|
+
|
|
437
|
+
// src/scores/run-experiment/index.ts
|
|
438
|
+
async function runExperiment(config) {
|
|
439
|
+
const { data, scorers, target, onItemComplete, concurrency = 1 } = config;
|
|
440
|
+
validateExperimentInputs(data, scorers, target);
|
|
441
|
+
let totalItems = 0;
|
|
442
|
+
const scoreAccumulator = new ScoreAccumulator();
|
|
443
|
+
const pMap = (await import('p-map')).default;
|
|
444
|
+
await pMap(
|
|
445
|
+
data,
|
|
446
|
+
async (item) => {
|
|
447
|
+
const targetResult = await executeTarget(target, item);
|
|
448
|
+
const scorerResults = await runScorers(scorers, targetResult, item);
|
|
449
|
+
scoreAccumulator.addScores(scorerResults);
|
|
450
|
+
if (onItemComplete) {
|
|
451
|
+
await onItemComplete({
|
|
452
|
+
item,
|
|
453
|
+
targetResult,
|
|
454
|
+
scorerResults
|
|
455
|
+
});
|
|
456
|
+
}
|
|
457
|
+
totalItems++;
|
|
458
|
+
},
|
|
459
|
+
{ concurrency }
|
|
460
|
+
);
|
|
461
|
+
return {
|
|
462
|
+
scores: scoreAccumulator.getAverageScores(),
|
|
463
|
+
summary: {
|
|
464
|
+
totalItems
|
|
465
|
+
}
|
|
466
|
+
};
|
|
467
|
+
}
|
|
468
|
+
function isWorkflow(target) {
|
|
469
|
+
return target instanceof chunkKPEYRIL4_cjs.Workflow;
|
|
470
|
+
}
|
|
471
|
+
function isWorkflowScorerConfig(scorers) {
|
|
472
|
+
return typeof scorers === "object" && !Array.isArray(scorers) && ("workflow" in scorers || "steps" in scorers);
|
|
473
|
+
}
|
|
474
|
+
function validateExperimentInputs(data, scorers, target) {
|
|
475
|
+
if (data.length === 0) {
|
|
476
|
+
throw new chunkC73WLCY3_cjs.MastraError({
|
|
477
|
+
domain: "SCORER",
|
|
478
|
+
id: "RUN_EXPERIMENT_FAILED_NO_DATA_PROVIDED",
|
|
479
|
+
category: "USER",
|
|
480
|
+
text: "Failed to run experiment: Data array is empty"
|
|
481
|
+
});
|
|
482
|
+
}
|
|
483
|
+
for (let i = 0; i < data.length; i++) {
|
|
484
|
+
const item = data[i];
|
|
485
|
+
if (!item || typeof item !== "object" || !("input" in item)) {
|
|
486
|
+
throw new chunkC73WLCY3_cjs.MastraError({
|
|
487
|
+
domain: "SCORER",
|
|
488
|
+
id: "INVALID_DATA_ITEM",
|
|
489
|
+
category: "USER",
|
|
490
|
+
text: `Invalid data item at index ${i}: must have 'input' properties`
|
|
491
|
+
});
|
|
492
|
+
}
|
|
493
|
+
}
|
|
494
|
+
if (Array.isArray(scorers)) {
|
|
495
|
+
if (scorers.length === 0) {
|
|
496
|
+
throw new chunkC73WLCY3_cjs.MastraError({
|
|
497
|
+
domain: "SCORER",
|
|
498
|
+
id: "NO_SCORERS_PROVIDED",
|
|
499
|
+
category: "USER",
|
|
500
|
+
text: "At least one scorer must be provided"
|
|
501
|
+
});
|
|
502
|
+
}
|
|
503
|
+
} else if (isWorkflow(target) && isWorkflowScorerConfig(scorers)) {
|
|
504
|
+
const hasScorers = scorers.workflow && scorers.workflow.length > 0 || scorers.steps && Object.keys(scorers.steps).length > 0;
|
|
505
|
+
if (!hasScorers) {
|
|
506
|
+
throw new chunkC73WLCY3_cjs.MastraError({
|
|
507
|
+
domain: "SCORER",
|
|
508
|
+
id: "NO_SCORERS_PROVIDED",
|
|
509
|
+
category: "USER",
|
|
510
|
+
text: "At least one workflow or step scorer must be provided"
|
|
511
|
+
});
|
|
512
|
+
}
|
|
513
|
+
} else if (!isWorkflow(target) && !Array.isArray(scorers)) {
|
|
514
|
+
throw new chunkC73WLCY3_cjs.MastraError({
|
|
515
|
+
domain: "SCORER",
|
|
516
|
+
id: "INVALID_AGENT_SCORERS",
|
|
517
|
+
category: "USER",
|
|
518
|
+
text: "Agent scorers must be an array of scorers"
|
|
519
|
+
});
|
|
520
|
+
}
|
|
521
|
+
}
|
|
522
|
+
async function executeTarget(target, item) {
|
|
523
|
+
try {
|
|
524
|
+
if (isWorkflow(target)) {
|
|
525
|
+
return await executeWorkflow(target, item);
|
|
526
|
+
} else {
|
|
527
|
+
return await executeAgent(target, item);
|
|
528
|
+
}
|
|
529
|
+
} catch (error) {
|
|
530
|
+
throw new chunkC73WLCY3_cjs.MastraError(
|
|
531
|
+
{
|
|
532
|
+
domain: "SCORER",
|
|
533
|
+
id: "RUN_EXPERIMENT_TARGET_FAILED_TO_GENERATE_RESULT",
|
|
534
|
+
category: "USER",
|
|
535
|
+
text: "Failed to run experiment: Error generating result from target",
|
|
536
|
+
details: {
|
|
537
|
+
item: JSON.stringify(item)
|
|
538
|
+
}
|
|
539
|
+
},
|
|
540
|
+
error
|
|
541
|
+
);
|
|
542
|
+
}
|
|
543
|
+
}
|
|
544
|
+
async function executeWorkflow(target, item) {
|
|
545
|
+
const run = target.createRun({ disableScorers: true });
|
|
546
|
+
const workflowResult = await run.start({
|
|
547
|
+
inputData: item.input,
|
|
548
|
+
runtimeContext: item.runtimeContext
|
|
549
|
+
});
|
|
550
|
+
return {
|
|
551
|
+
scoringData: {
|
|
552
|
+
input: item.input,
|
|
553
|
+
output: workflowResult.status === "success" ? workflowResult.result : void 0,
|
|
554
|
+
stepResults: workflowResult.steps
|
|
555
|
+
}
|
|
556
|
+
};
|
|
557
|
+
}
|
|
558
|
+
async function executeAgent(agent, item) {
|
|
559
|
+
const model = await agent.getModel();
|
|
560
|
+
if (model.specificationVersion === "v2") {
|
|
561
|
+
return await agent.generateVNext(item.input, {
|
|
562
|
+
scorers: {},
|
|
563
|
+
returnScorerData: true,
|
|
564
|
+
runtimeContext: item.runtimeContext
|
|
565
|
+
});
|
|
566
|
+
} else {
|
|
567
|
+
return await agent.generate(item.input, {
|
|
568
|
+
scorers: {},
|
|
569
|
+
returnScorerData: true,
|
|
570
|
+
runtimeContext: item.runtimeContext
|
|
571
|
+
});
|
|
572
|
+
}
|
|
573
|
+
}
|
|
574
|
+
async function runScorers(scorers, targetResult, item) {
|
|
575
|
+
const scorerResults = {};
|
|
576
|
+
if (Array.isArray(scorers)) {
|
|
577
|
+
for (const scorer of scorers) {
|
|
578
|
+
try {
|
|
579
|
+
const score = await scorer.run({
|
|
580
|
+
input: targetResult.scoringData?.input,
|
|
581
|
+
output: targetResult.scoringData?.output,
|
|
582
|
+
groundTruth: item.groundTruth,
|
|
583
|
+
runtimeContext: item.runtimeContext,
|
|
584
|
+
tracingContext: item.tracingContext
|
|
585
|
+
});
|
|
586
|
+
scorerResults[scorer.name] = score;
|
|
587
|
+
} catch (error) {
|
|
588
|
+
throw new chunkC73WLCY3_cjs.MastraError(
|
|
589
|
+
{
|
|
590
|
+
domain: "SCORER",
|
|
591
|
+
id: "RUN_EXPERIMENT_SCORER_FAILED_TO_SCORE_RESULT",
|
|
592
|
+
category: "USER",
|
|
593
|
+
text: `Failed to run experiment: Error running scorer ${scorer.name}`,
|
|
594
|
+
details: {
|
|
595
|
+
scorerName: scorer.name,
|
|
596
|
+
item: JSON.stringify(item)
|
|
597
|
+
}
|
|
598
|
+
},
|
|
599
|
+
error
|
|
600
|
+
);
|
|
601
|
+
}
|
|
602
|
+
}
|
|
603
|
+
} else {
|
|
604
|
+
if (scorers.workflow) {
|
|
605
|
+
const workflowScorerResults = {};
|
|
606
|
+
for (const scorer of scorers.workflow) {
|
|
607
|
+
const score = await scorer.run({
|
|
608
|
+
input: targetResult.scoringData.input,
|
|
609
|
+
output: targetResult.scoringData.output,
|
|
610
|
+
groundTruth: item.groundTruth,
|
|
611
|
+
runtimeContext: item.runtimeContext,
|
|
612
|
+
tracingContext: item.tracingContext
|
|
613
|
+
});
|
|
614
|
+
workflowScorerResults[scorer.name] = score;
|
|
615
|
+
}
|
|
616
|
+
if (Object.keys(workflowScorerResults).length > 0) {
|
|
617
|
+
scorerResults.workflow = workflowScorerResults;
|
|
618
|
+
}
|
|
619
|
+
}
|
|
620
|
+
if (scorers.steps) {
|
|
621
|
+
const stepScorerResults = {};
|
|
622
|
+
for (const [stepId, stepScorers] of Object.entries(scorers.steps)) {
|
|
623
|
+
const stepResult = targetResult.scoringData.stepResults?.[stepId];
|
|
624
|
+
if (stepResult?.status === "success" && stepResult.payload && stepResult.output) {
|
|
625
|
+
const stepResults = {};
|
|
626
|
+
for (const scorer of stepScorers) {
|
|
627
|
+
try {
|
|
628
|
+
const score = await scorer.run({
|
|
629
|
+
input: stepResult.payload,
|
|
630
|
+
output: stepResult.output,
|
|
631
|
+
groundTruth: item.groundTruth,
|
|
632
|
+
runtimeContext: item.runtimeContext,
|
|
633
|
+
tracingContext: item.tracingContext
|
|
634
|
+
});
|
|
635
|
+
stepResults[scorer.name] = score;
|
|
636
|
+
} catch (error) {
|
|
637
|
+
throw new chunkC73WLCY3_cjs.MastraError(
|
|
638
|
+
{
|
|
639
|
+
domain: "SCORER",
|
|
640
|
+
id: "RUN_EXPERIMENT_SCORER_FAILED_TO_SCORE_STEP_RESULT",
|
|
641
|
+
category: "USER",
|
|
642
|
+
text: `Failed to run experiment: Error running scorer ${scorer.name} on step ${stepId}`,
|
|
643
|
+
details: {
|
|
644
|
+
scorerName: scorer.name,
|
|
645
|
+
stepId
|
|
646
|
+
}
|
|
647
|
+
},
|
|
648
|
+
error
|
|
649
|
+
);
|
|
650
|
+
}
|
|
651
|
+
}
|
|
652
|
+
if (Object.keys(stepResults).length > 0) {
|
|
653
|
+
stepScorerResults[stepId] = stepResults;
|
|
654
|
+
}
|
|
655
|
+
}
|
|
656
|
+
}
|
|
657
|
+
if (Object.keys(stepScorerResults).length > 0) {
|
|
658
|
+
scorerResults.steps = stepScorerResults;
|
|
659
|
+
}
|
|
660
|
+
}
|
|
661
|
+
}
|
|
662
|
+
return scorerResults;
|
|
663
|
+
}
|
|
664
|
+
|
|
665
|
+
exports.MastraScorer = MastraScorer;
|
|
666
|
+
exports.createScorer = createScorer;
|
|
667
|
+
exports.runExperiment = runExperiment;
|
|
668
|
+
exports.saveScorePayloadSchema = saveScorePayloadSchema;
|
|
669
|
+
exports.scoreResultSchema = scoreResultSchema;
|
|
670
|
+
exports.scoringExtractStepResultSchema = scoringExtractStepResultSchema;
|
|
671
|
+
exports.scoringValueSchema = scoringValueSchema;
|
|
672
|
+
//# sourceMappingURL=chunk-XDSZO4OG.cjs.map
|
|
673
|
+
//# sourceMappingURL=chunk-XDSZO4OG.cjs.map
|