@mastra/core 0.15.2 → 0.15.3-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.
Files changed (163) hide show
  1. package/dist/agent/agent.types.d.ts +2 -0
  2. package/dist/agent/agent.types.d.ts.map +1 -1
  3. package/dist/agent/index.cjs +8 -8
  4. package/dist/agent/index.d.ts +0 -2
  5. package/dist/agent/index.d.ts.map +1 -1
  6. package/dist/agent/index.js +1 -1
  7. package/dist/agent/input-processor/index.cjs +6 -6
  8. package/dist/agent/input-processor/index.js +1 -1
  9. package/dist/ai-tracing/base.d.ts +3 -1
  10. package/dist/ai-tracing/base.d.ts.map +1 -1
  11. package/dist/ai-tracing/default.d.ts +1 -9
  12. package/dist/ai-tracing/default.d.ts.map +1 -1
  13. package/dist/ai-tracing/exporters/console.d.ts +10 -0
  14. package/dist/ai-tracing/exporters/console.d.ts.map +1 -0
  15. package/dist/ai-tracing/exporters/default.d.ts +97 -0
  16. package/dist/ai-tracing/exporters/default.d.ts.map +1 -0
  17. package/dist/ai-tracing/exporters/index.d.ts +6 -0
  18. package/dist/ai-tracing/exporters/index.d.ts.map +1 -0
  19. package/dist/ai-tracing/index.cjs +31 -27
  20. package/dist/ai-tracing/index.d.ts +1 -0
  21. package/dist/ai-tracing/index.d.ts.map +1 -1
  22. package/dist/ai-tracing/index.js +1 -1
  23. package/dist/ai-tracing/no-op.d.ts +8 -0
  24. package/dist/ai-tracing/no-op.d.ts.map +1 -1
  25. package/dist/ai-tracing/types.d.ts +33 -0
  26. package/dist/ai-tracing/types.d.ts.map +1 -1
  27. package/dist/{chunk-EQLCC3M7.cjs → chunk-4DKPMUAC.cjs} +646 -101
  28. package/dist/chunk-4DKPMUAC.cjs.map +1 -0
  29. package/dist/{chunk-QLRALF4I.js → chunk-5MCNXLGT.js} +6 -4
  30. package/dist/chunk-5MCNXLGT.js.map +1 -0
  31. package/dist/{chunk-RMEG4MOG.cjs → chunk-6VROHRAR.cjs} +45 -30
  32. package/dist/chunk-6VROHRAR.cjs.map +1 -0
  33. package/dist/{chunk-SNYSVGIU.cjs → chunk-6Z7D7CA3.cjs} +144 -113
  34. package/dist/chunk-6Z7D7CA3.cjs.map +1 -0
  35. package/dist/{chunk-HRPTZGDT.js → chunk-BRNBKCHE.js} +4 -4
  36. package/dist/chunk-BRNBKCHE.js.map +1 -0
  37. package/dist/{chunk-DAJYN7HG.cjs → chunk-CDLIHAX2.cjs} +4 -4
  38. package/dist/{chunk-DAJYN7HG.cjs.map → chunk-CDLIHAX2.cjs.map} +1 -1
  39. package/dist/{chunk-VKJWTAHZ.js → chunk-E3LAPNKY.js} +3 -3
  40. package/dist/{chunk-VKJWTAHZ.js.map → chunk-E3LAPNKY.js.map} +1 -1
  41. package/dist/{chunk-FCFQE5BD.js → chunk-FGCA6CCM.js} +130 -99
  42. package/dist/chunk-FGCA6CCM.js.map +1 -0
  43. package/dist/{chunk-FFGJPMKP.js → chunk-FLXWZUIG.js} +45 -30
  44. package/dist/chunk-FLXWZUIG.js.map +1 -0
  45. package/dist/{chunk-GVMFAUQR.cjs → chunk-GEPX327P.cjs} +12 -2
  46. package/dist/chunk-GEPX327P.cjs.map +1 -0
  47. package/dist/{chunk-DPE6K23N.cjs → chunk-HRDADYVX.cjs} +47 -8
  48. package/dist/chunk-HRDADYVX.cjs.map +1 -0
  49. package/dist/{chunk-UKQI74TN.cjs → chunk-K6UMYGK5.cjs} +17 -2
  50. package/dist/chunk-K6UMYGK5.cjs.map +1 -0
  51. package/dist/{chunk-XJFIB2FO.js → chunk-MKWJKRSX.js} +3 -3
  52. package/dist/{chunk-XJFIB2FO.js.map → chunk-MKWJKRSX.js.map} +1 -1
  53. package/dist/{chunk-7EXGDKNQ.cjs → chunk-QBNRMJAN.cjs} +4 -4
  54. package/dist/{chunk-7EXGDKNQ.cjs.map → chunk-QBNRMJAN.cjs.map} +1 -1
  55. package/dist/{chunk-6NYFECSO.js → chunk-TINMY4WA.js} +43 -4
  56. package/dist/chunk-TINMY4WA.js.map +1 -0
  57. package/dist/{chunk-G6WYC4SF.cjs → chunk-VBAWR62U.cjs} +7 -7
  58. package/dist/chunk-VBAWR62U.cjs.map +1 -0
  59. package/dist/{chunk-7TH2KSEC.js → chunk-WOTBMZCN.js} +645 -101
  60. package/dist/chunk-WOTBMZCN.js.map +1 -0
  61. package/dist/{chunk-ASRKKIW7.cjs → chunk-XEY3CWQW.cjs} +10 -8
  62. package/dist/chunk-XEY3CWQW.cjs.map +1 -0
  63. package/dist/{chunk-Y44DK4T5.js → chunk-YGW2WEJ5.js} +17 -2
  64. package/dist/chunk-YGW2WEJ5.js.map +1 -0
  65. package/dist/{chunk-IKOGUY3M.js → chunk-Z73WO76N.js} +4 -2
  66. package/dist/chunk-Z73WO76N.js.map +1 -0
  67. package/dist/index.cjs +52 -44
  68. package/dist/index.js +11 -10
  69. package/dist/index.js.map +1 -1
  70. package/dist/llm/model/model.d.ts.map +1 -1
  71. package/dist/llm/model/model.loop.d.ts +1 -1
  72. package/dist/llm/model/model.loop.d.ts.map +1 -1
  73. package/dist/loop/index.cjs +2 -2
  74. package/dist/loop/index.js +1 -1
  75. package/dist/loop/types.d.ts +1 -0
  76. package/dist/loop/types.d.ts.map +1 -1
  77. package/dist/mastra/index.cjs +2 -2
  78. package/dist/mastra/index.d.ts +8 -0
  79. package/dist/mastra/index.d.ts.map +1 -1
  80. package/dist/mastra/index.js +1 -1
  81. package/dist/memory/index.cjs +4 -4
  82. package/dist/memory/index.js +1 -1
  83. package/dist/network/index.cjs +2 -2
  84. package/dist/network/index.js +1 -1
  85. package/dist/network/vNext/index.cjs +15 -13
  86. package/dist/network/vNext/index.cjs.map +1 -1
  87. package/dist/network/vNext/index.d.ts.map +1 -1
  88. package/dist/network/vNext/index.js +4 -2
  89. package/dist/network/vNext/index.js.map +1 -1
  90. package/dist/processors/index.cjs +8 -8
  91. package/dist/processors/index.js +2 -2
  92. package/dist/relevance/index.cjs +4 -4
  93. package/dist/relevance/index.js +1 -1
  94. package/dist/scores/index.cjs +282 -88
  95. package/dist/scores/index.cjs.map +1 -1
  96. package/dist/scores/index.d.ts +1 -1
  97. package/dist/scores/index.js +279 -85
  98. package/dist/scores/index.js.map +1 -1
  99. package/dist/scores/run-experiment/index.d.ts +59 -0
  100. package/dist/scores/run-experiment/index.d.ts.map +1 -0
  101. package/dist/scores/run-experiment/scorerAccumulator.d.ts +12 -0
  102. package/dist/scores/run-experiment/scorerAccumulator.d.ts.map +1 -0
  103. package/dist/storage/base.d.ts +9 -0
  104. package/dist/storage/base.d.ts.map +1 -1
  105. package/dist/storage/domains/observability/base.d.ts +9 -0
  106. package/dist/storage/domains/observability/base.d.ts.map +1 -1
  107. package/dist/storage/domains/observability/index.d.ts +1 -0
  108. package/dist/storage/domains/observability/index.d.ts.map +1 -1
  109. package/dist/storage/domains/observability/inmemory.d.ts +5 -0
  110. package/dist/storage/domains/observability/inmemory.d.ts.map +1 -1
  111. package/dist/storage/index.cjs +24 -6
  112. package/dist/storage/index.cjs.map +1 -1
  113. package/dist/storage/index.js +20 -3
  114. package/dist/storage/index.js.map +1 -1
  115. package/dist/stream/index.cjs +3 -3
  116. package/dist/stream/index.js +1 -1
  117. package/dist/test-utils/llm-mock.cjs +2 -2
  118. package/dist/test-utils/llm-mock.js +1 -1
  119. package/dist/tools/tool-builder/builder.d.ts.map +1 -1
  120. package/dist/tools/types.d.ts +2 -2
  121. package/dist/tools/types.d.ts.map +1 -1
  122. package/dist/utils.cjs +16 -16
  123. package/dist/utils.js +1 -1
  124. package/dist/vector/embed.d.ts +3 -0
  125. package/dist/vector/embed.d.ts.map +1 -0
  126. package/dist/vector/index.cjs +10 -2
  127. package/dist/vector/index.d.ts +1 -0
  128. package/dist/vector/index.d.ts.map +1 -1
  129. package/dist/vector/index.js +1 -1
  130. package/dist/vector/vector.d.ts +3 -0
  131. package/dist/vector/vector.d.ts.map +1 -1
  132. package/dist/workflows/default.d.ts +25 -6
  133. package/dist/workflows/default.d.ts.map +1 -1
  134. package/dist/workflows/evented/index.cjs +10 -10
  135. package/dist/workflows/evented/index.js +1 -1
  136. package/dist/workflows/evented/workflow.d.ts.map +1 -1
  137. package/dist/workflows/execution-engine.d.ts +1 -0
  138. package/dist/workflows/execution-engine.d.ts.map +1 -1
  139. package/dist/workflows/index.cjs +10 -10
  140. package/dist/workflows/index.js +1 -1
  141. package/dist/workflows/legacy/index.cjs +22 -22
  142. package/dist/workflows/legacy/index.js +1 -1
  143. package/dist/workflows/workflow.d.ts +7 -0
  144. package/dist/workflows/workflow.d.ts.map +1 -1
  145. package/package.json +4 -4
  146. package/dist/chunk-6NYFECSO.js.map +0 -1
  147. package/dist/chunk-7TH2KSEC.js.map +0 -1
  148. package/dist/chunk-ASRKKIW7.cjs.map +0 -1
  149. package/dist/chunk-DPE6K23N.cjs.map +0 -1
  150. package/dist/chunk-EQLCC3M7.cjs.map +0 -1
  151. package/dist/chunk-FCFQE5BD.js.map +0 -1
  152. package/dist/chunk-FFGJPMKP.js.map +0 -1
  153. package/dist/chunk-G6WYC4SF.cjs.map +0 -1
  154. package/dist/chunk-GVMFAUQR.cjs.map +0 -1
  155. package/dist/chunk-HRPTZGDT.js.map +0 -1
  156. package/dist/chunk-IKOGUY3M.js.map +0 -1
  157. package/dist/chunk-QLRALF4I.js.map +0 -1
  158. package/dist/chunk-RMEG4MOG.cjs.map +0 -1
  159. package/dist/chunk-SNYSVGIU.cjs.map +0 -1
  160. package/dist/chunk-UKQI74TN.cjs.map +0 -1
  161. package/dist/chunk-Y44DK4T5.js.map +0 -1
  162. package/dist/scores/run-experiment.d.ts +0 -35
  163. package/dist/scores/run-experiment.d.ts.map +0 -1
@@ -1,6 +1,6 @@
1
1
  'use strict';
2
2
 
3
- var chunkSNYSVGIU_cjs = require('../chunk-SNYSVGIU.cjs');
3
+ var chunk6Z7D7CA3_cjs = require('../chunk-6Z7D7CA3.cjs');
4
4
  var chunkC73WLCY3_cjs = require('../chunk-C73WLCY3.cjs');
5
5
  var zod = require('zod');
6
6
  var crypto = require('crypto');
@@ -161,7 +161,7 @@ var MastraScorer = class _MastraScorer {
161
161
  }
162
162
  toMastraWorkflow() {
163
163
  const workflowSteps = this.steps.map((scorerStep) => {
164
- return chunkSNYSVGIU_cjs.createStep({
164
+ return chunk6Z7D7CA3_cjs.createStep({
165
165
  id: scorerStep.name,
166
166
  description: `Scorer step: ${scorerStep.name}`,
167
167
  inputSchema: zod.z.any(),
@@ -194,7 +194,7 @@ var MastraScorer = class _MastraScorer {
194
194
  }
195
195
  });
196
196
  });
197
- const workflow = chunkSNYSVGIU_cjs.createWorkflow({
197
+ const workflow = chunk6Z7D7CA3_cjs.createWorkflow({
198
198
  id: `scorer-${this.config.name}`,
199
199
  description: this.config.description,
200
200
  inputSchema: zod.z.object({
@@ -249,7 +249,7 @@ var MastraScorer = class _MastraScorer {
249
249
  }
250
250
  });
251
251
  }
252
- const judge = new chunkSNYSVGIU_cjs.Agent({ name: "judge", model, instructions });
252
+ const judge = new chunk6Z7D7CA3_cjs.Agent({ name: "judge", model, instructions });
253
253
  if (scorerStep.name === "generateScore") {
254
254
  let result;
255
255
  if (model.specificationVersion === "v2") {
@@ -313,16 +313,132 @@ function createScorer(config) {
313
313
  });
314
314
  }
315
315
 
316
- // src/scores/run-experiment.ts
317
- var runExperiment = async ({
318
- data,
319
- scorers,
320
- target,
321
- onItemComplete,
322
- concurrency = 1
323
- }) => {
316
+ // src/scores/run-experiment/scorerAccumulator.ts
317
+ var ScoreAccumulator = class {
318
+ flatScores = {};
319
+ workflowScores = {};
320
+ stepScores = {};
321
+ addScores(scorerResults) {
322
+ const isTargetWorkflowAndHasStepScores = "steps" in scorerResults;
323
+ if (isTargetWorkflowAndHasStepScores) {
324
+ this.addNestedScores(scorerResults);
325
+ } else {
326
+ this.addFlatScores(scorerResults);
327
+ }
328
+ }
329
+ addFlatScores(scorerResults) {
330
+ for (const [scorerName, result] of Object.entries(scorerResults)) {
331
+ if (!this.flatScores[scorerName]) {
332
+ this.flatScores[scorerName] = [];
333
+ }
334
+ this.flatScores[scorerName].push(result.score);
335
+ }
336
+ }
337
+ addNestedScores(scorerResults) {
338
+ if ("workflow" in scorerResults && scorerResults.workflow) {
339
+ for (const [scorerName, result] of Object.entries(scorerResults.workflow)) {
340
+ if (!this.workflowScores[scorerName]) {
341
+ this.workflowScores[scorerName] = [];
342
+ }
343
+ this.workflowScores[scorerName].push(result.score);
344
+ }
345
+ }
346
+ if ("steps" in scorerResults && scorerResults.steps) {
347
+ for (const [stepId, stepResults] of Object.entries(scorerResults.steps)) {
348
+ if (!this.stepScores[stepId]) {
349
+ this.stepScores[stepId] = {};
350
+ }
351
+ for (const [scorerName, result] of Object.entries(stepResults)) {
352
+ if (!this.stepScores[stepId][scorerName]) {
353
+ this.stepScores[stepId][scorerName] = [];
354
+ }
355
+ this.stepScores[stepId][scorerName].push(result.score);
356
+ }
357
+ }
358
+ }
359
+ }
360
+ addStepScores(stepScorerResults) {
361
+ for (const [stepId, stepResults] of Object.entries(stepScorerResults)) {
362
+ if (!this.stepScores[stepId]) {
363
+ this.stepScores[stepId] = {};
364
+ }
365
+ for (const [scorerName, result] of Object.entries(stepResults)) {
366
+ if (!this.stepScores[stepId][scorerName]) {
367
+ this.stepScores[stepId][scorerName] = [];
368
+ }
369
+ this.stepScores[stepId][scorerName].push(result.score);
370
+ }
371
+ }
372
+ }
373
+ getAverageScores() {
374
+ const result = {};
375
+ for (const [scorerName, scoreArray] of Object.entries(this.flatScores)) {
376
+ result[scorerName] = this.getAverageScore(scoreArray);
377
+ }
378
+ if (Object.keys(this.workflowScores).length > 0) {
379
+ result.workflow = {};
380
+ for (const [scorerName, scoreArray] of Object.entries(this.workflowScores)) {
381
+ result.workflow[scorerName] = this.getAverageScore(scoreArray);
382
+ }
383
+ }
384
+ if (Object.keys(this.stepScores).length > 0) {
385
+ result.steps = {};
386
+ for (const [stepId, stepScorers] of Object.entries(this.stepScores)) {
387
+ result.steps[stepId] = {};
388
+ for (const [scorerName, scoreArray] of Object.entries(stepScorers)) {
389
+ result.steps[stepId][scorerName] = this.getAverageScore(scoreArray);
390
+ }
391
+ }
392
+ }
393
+ return result;
394
+ }
395
+ getAverageScore(scoreArray) {
396
+ if (scoreArray.length > 0) {
397
+ return scoreArray.reduce((a, b) => a + b, 0) / scoreArray.length;
398
+ } else {
399
+ return 0;
400
+ }
401
+ }
402
+ };
403
+
404
+ // src/scores/run-experiment/index.ts
405
+ async function runExperiment(config) {
406
+ const { data, scorers, target, onItemComplete, concurrency = 1 } = config;
407
+ validateExperimentInputs(data, scorers, target);
324
408
  let totalItems = 0;
325
- const scoreAccumulators = {};
409
+ const scoreAccumulator = new ScoreAccumulator();
410
+ const pMap = (await import('p-map')).default;
411
+ await pMap(
412
+ data,
413
+ async (item) => {
414
+ const targetResult = await executeTarget(target, item);
415
+ const scorerResults = await runScorers(scorers, targetResult, item);
416
+ scoreAccumulator.addScores(scorerResults);
417
+ if (onItemComplete) {
418
+ await onItemComplete({
419
+ item,
420
+ targetResult,
421
+ scorerResults
422
+ });
423
+ }
424
+ totalItems++;
425
+ },
426
+ { concurrency }
427
+ );
428
+ return {
429
+ scores: scoreAccumulator.getAverageScores(),
430
+ summary: {
431
+ totalItems
432
+ }
433
+ };
434
+ }
435
+ function isWorkflow(target) {
436
+ return target instanceof chunk6Z7D7CA3_cjs.Workflow;
437
+ }
438
+ function isWorkflowScorerConfig(scorers) {
439
+ return typeof scorers === "object" && !Array.isArray(scorers) && ("workflow" in scorers || "steps" in scorers);
440
+ }
441
+ function validateExperimentInputs(data, scorers, target) {
326
442
  if (data.length === 0) {
327
443
  throw new chunkC73WLCY3_cjs.MastraError({
328
444
  domain: "SCORER",
@@ -331,106 +447,184 @@ var runExperiment = async ({
331
447
  text: "Failed to run experiment: Data array is empty"
332
448
  });
333
449
  }
334
- if (scorers.length === 0) {
450
+ for (let i = 0; i < data.length; i++) {
451
+ const item = data[i];
452
+ if (!item || typeof item !== "object" || !("input" in item)) {
453
+ throw new chunkC73WLCY3_cjs.MastraError({
454
+ domain: "SCORER",
455
+ id: "INVALID_DATA_ITEM",
456
+ category: "USER",
457
+ text: `Invalid data item at index ${i}: must have 'input' properties`
458
+ });
459
+ }
460
+ }
461
+ if (Array.isArray(scorers)) {
462
+ if (scorers.length === 0) {
463
+ throw new chunkC73WLCY3_cjs.MastraError({
464
+ domain: "SCORER",
465
+ id: "NO_SCORERS_PROVIDED",
466
+ category: "USER",
467
+ text: "At least one scorer must be provided"
468
+ });
469
+ }
470
+ } else if (isWorkflow(target) && isWorkflowScorerConfig(scorers)) {
471
+ const hasScorers = scorers.workflow && scorers.workflow.length > 0 || scorers.steps && Object.keys(scorers.steps).length > 0;
472
+ if (!hasScorers) {
473
+ throw new chunkC73WLCY3_cjs.MastraError({
474
+ domain: "SCORER",
475
+ id: "NO_SCORERS_PROVIDED",
476
+ category: "USER",
477
+ text: "At least one workflow or step scorer must be provided"
478
+ });
479
+ }
480
+ } else if (!isWorkflow(target) && !Array.isArray(scorers)) {
335
481
  throw new chunkC73WLCY3_cjs.MastraError({
336
482
  domain: "SCORER",
337
- id: "RUN_EXPERIMENT_FAILED_NO_SCORERS_PROVIDED",
483
+ id: "INVALID_AGENT_SCORERS",
338
484
  category: "USER",
339
- text: "Failed to run experiment: No scorers provided"
485
+ text: "Agent scorers must be an array of scorers"
340
486
  });
341
487
  }
342
- if (!target) {
343
- throw new chunkC73WLCY3_cjs.MastraError({
344
- domain: "SCORER",
345
- id: "RUN_EXPERIMENT_FAILED_NO_TARGET_PROVIDED",
346
- category: "USER",
347
- text: "Failed to run experiment: No target provided"
488
+ }
489
+ async function executeTarget(target, item) {
490
+ try {
491
+ if (isWorkflow(target)) {
492
+ return await executeWorkflow(target, item);
493
+ } else {
494
+ return await executeAgent(target, item);
495
+ }
496
+ } catch (error) {
497
+ throw new chunkC73WLCY3_cjs.MastraError(
498
+ {
499
+ domain: "SCORER",
500
+ id: "RUN_EXPERIMENT_TARGET_FAILED_TO_GENERATE_RESULT",
501
+ category: "USER",
502
+ text: "Failed to run experiment: Error generating result from target",
503
+ details: {
504
+ item: JSON.stringify(item)
505
+ }
506
+ },
507
+ error
508
+ );
509
+ }
510
+ }
511
+ async function executeWorkflow(target, item) {
512
+ const run = target.createRun({ disableScorers: true });
513
+ const workflowResult = await run.start({
514
+ inputData: item.input,
515
+ runtimeContext: item.runtimeContext
516
+ });
517
+ return {
518
+ scoringData: {
519
+ input: item.input,
520
+ output: workflowResult.status === "success" ? workflowResult.result : void 0,
521
+ stepResults: workflowResult.steps
522
+ }
523
+ };
524
+ }
525
+ async function executeAgent(agent, item) {
526
+ const model = await agent.getModel();
527
+ if (model.specificationVersion === "v2") {
528
+ return await agent.generateVNext(item.input, {
529
+ scorers: {},
530
+ returnScorerData: true,
531
+ runtimeContext: item.runtimeContext
532
+ });
533
+ } else {
534
+ return await agent.generate(item.input, {
535
+ scorers: {},
536
+ returnScorerData: true,
537
+ runtimeContext: item.runtimeContext
348
538
  });
349
539
  }
350
- const pMap = (await import('p-map')).default;
351
- await pMap(
352
- data,
353
- async (item) => {
354
- let targetResult;
540
+ }
541
+ async function runScorers(scorers, targetResult, item) {
542
+ const scorerResults = {};
543
+ if (Array.isArray(scorers)) {
544
+ for (const scorer of scorers) {
355
545
  try {
356
- const model = await target.getModel();
357
- if (model.specificationVersion === "v2") {
358
- targetResult = await target.generateVNext(item.input, {
359
- scorers: {},
360
- returnScorerData: true,
361
- runtimeContext: item.runtimeContext
362
- });
363
- } else {
364
- targetResult = await target.generate(item.input, {
365
- scorers: {},
366
- returnScorerData: true,
367
- runtimeContext: item.runtimeContext
368
- });
369
- }
546
+ const score = await scorer.run({
547
+ input: targetResult.scoringData?.input,
548
+ output: targetResult.scoringData?.output,
549
+ groundTruth: item.groundTruth,
550
+ runtimeContext: item.runtimeContext
551
+ });
552
+ scorerResults[scorer.name] = score;
370
553
  } catch (error) {
371
554
  throw new chunkC73WLCY3_cjs.MastraError(
372
555
  {
373
556
  domain: "SCORER",
374
- id: "RUN_EXPERIMENT_TARGET_FAILED_TO_GENERATE_RESULT",
557
+ id: "RUN_EXPERIMENT_SCORER_FAILED_TO_SCORE_RESULT",
375
558
  category: "USER",
376
- text: "Failed to run experiment: Error generating result from target",
559
+ text: `Failed to run experiment: Error running scorer ${scorer.name}`,
377
560
  details: {
561
+ scorerName: scorer.name,
378
562
  item: JSON.stringify(item)
379
563
  }
380
564
  },
381
565
  error
382
566
  );
383
567
  }
384
- const scorerResults = {};
385
- for (const scorer of scorers) {
386
- try {
387
- const score = await scorer.run({
388
- input: targetResult.scoringData?.input,
389
- output: targetResult.scoringData?.output,
390
- groundTruth: item.groundTruth,
391
- runtimeContext: item.runtimeContext
392
- });
393
- scorerResults[scorer.name] = score;
394
- } catch (error) {
395
- throw new chunkC73WLCY3_cjs.MastraError(
396
- {
397
- domain: "SCORER",
398
- id: "RUN_EXPERIMENT_SCORER_FAILED_TO_SCORE_RESULT",
399
- category: "USER",
400
- text: `Failed to run experiment: Error running scorer ${scorer.name}`,
401
- details: {
402
- scorerName: scorer.name,
403
- item: JSON.stringify(item)
404
- }
405
- },
406
- error
407
- );
408
- }
568
+ }
569
+ } else {
570
+ if (scorers.workflow) {
571
+ const workflowScorerResults = {};
572
+ for (const scorer of scorers.workflow) {
573
+ const score = await scorer.run({
574
+ input: targetResult.scoringData.input,
575
+ output: targetResult.scoringData.output,
576
+ groundTruth: item.groundTruth,
577
+ runtimeContext: item.runtimeContext
578
+ });
579
+ workflowScorerResults[scorer.name] = score;
580
+ }
581
+ if (Object.keys(workflowScorerResults).length > 0) {
582
+ scorerResults.workflow = workflowScorerResults;
409
583
  }
410
- for (const [scorerName, result] of Object.entries(scorerResults)) {
411
- if (!scoreAccumulators[scorerName]) {
412
- scoreAccumulators[scorerName] = [];
584
+ }
585
+ if (scorers.steps) {
586
+ const stepScorerResults = {};
587
+ for (const [stepId, stepScorers] of Object.entries(scorers.steps)) {
588
+ const stepResult = targetResult.scoringData.stepResults?.[stepId];
589
+ if (stepResult?.status === "success" && stepResult.payload && stepResult.output) {
590
+ const stepResults = {};
591
+ for (const scorer of stepScorers) {
592
+ try {
593
+ const score = await scorer.run({
594
+ input: stepResult.payload,
595
+ output: stepResult.output,
596
+ groundTruth: item.groundTruth,
597
+ runtimeContext: item.runtimeContext
598
+ });
599
+ stepResults[scorer.name] = score;
600
+ } catch (error) {
601
+ throw new chunkC73WLCY3_cjs.MastraError(
602
+ {
603
+ domain: "SCORER",
604
+ id: "RUN_EXPERIMENT_SCORER_FAILED_TO_SCORE_STEP_RESULT",
605
+ category: "USER",
606
+ text: `Failed to run experiment: Error running scorer ${scorer.name} on step ${stepId}`,
607
+ details: {
608
+ scorerName: scorer.name,
609
+ stepId
610
+ }
611
+ },
612
+ error
613
+ );
614
+ }
615
+ }
616
+ if (Object.keys(stepResults).length > 0) {
617
+ stepScorerResults[stepId] = stepResults;
618
+ }
413
619
  }
414
- scoreAccumulators[scorerName].push(result.score);
415
620
  }
416
- if (onItemComplete) {
417
- onItemComplete({ item, targetResult, scorerResults });
621
+ if (Object.keys(stepScorerResults).length > 0) {
622
+ scorerResults.steps = stepScorerResults;
418
623
  }
419
- totalItems++;
420
- },
421
- { concurrency }
422
- );
423
- const averageScores = {};
424
- for (const [scorerName, scores] of Object.entries(scoreAccumulators)) {
425
- averageScores[scorerName] = scores.reduce((a, b) => a + b, 0) / scores.length;
426
- }
427
- return {
428
- scores: averageScores,
429
- summary: {
430
- totalItems
431
624
  }
432
- };
433
- };
625
+ }
626
+ return scorerResults;
627
+ }
434
628
 
435
629
  exports.MastraScorer = MastraScorer;
436
630
  exports.createScorer = createScorer;