vitest-evals 0.10.0 → 0.12.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.
Files changed (79) hide show
  1. package/README.md +30 -1
  2. package/bin/vitest-evals.js +8 -0
  3. package/dist/cli.d.mts +13 -0
  4. package/dist/cli.d.ts +13 -0
  5. package/dist/cli.js +83 -0
  6. package/dist/cli.js.map +1 -0
  7. package/dist/cli.mjs +55 -0
  8. package/dist/cli.mjs.map +1 -0
  9. package/dist/harness.d.mts +78 -236
  10. package/dist/harness.d.ts +78 -236
  11. package/dist/harness.js +292 -46
  12. package/dist/harness.js.map +1 -1
  13. package/dist/harness.mjs +297 -46
  14. package/dist/harness.mjs.map +1 -1
  15. package/dist/index.d.mts +5 -3
  16. package/dist/index.d.ts +5 -3
  17. package/dist/index.js +331 -52
  18. package/dist/index.js.map +1 -1
  19. package/dist/index.mjs +335 -52
  20. package/dist/index.mjs.map +1 -1
  21. package/dist/internal/scoring.d.mts +1 -1
  22. package/dist/internal/scoring.d.ts +1 -1
  23. package/dist/internal/structuredOutputScorer.d.mts +1 -1
  24. package/dist/internal/structuredOutputScorer.d.ts +1 -1
  25. package/dist/internal/toolCallScorer.d.mts +1 -1
  26. package/dist/internal/toolCallScorer.d.ts +1 -1
  27. package/dist/internal/toolCallScorer.js +34 -12
  28. package/dist/internal/toolCallScorer.js.map +1 -1
  29. package/dist/internal/toolCallScorer.mjs +48 -12
  30. package/dist/internal/toolCallScorer.mjs.map +1 -1
  31. package/dist/judges/factualityJudge.d.mts +2 -1
  32. package/dist/judges/factualityJudge.d.ts +2 -1
  33. package/dist/judges/factualityJudge.js +4 -14
  34. package/dist/judges/factualityJudge.js.map +1 -1
  35. package/dist/judges/factualityJudge.mjs +18 -14
  36. package/dist/judges/factualityJudge.mjs.map +1 -1
  37. package/dist/judges/index.d.mts +1 -0
  38. package/dist/judges/index.d.ts +1 -0
  39. package/dist/judges/index.js +285 -33
  40. package/dist/judges/index.js.map +1 -1
  41. package/dist/judges/index.mjs +299 -33
  42. package/dist/judges/index.mjs.map +1 -1
  43. package/dist/judges/judgeHarness.d.mts +2 -1
  44. package/dist/judges/judgeHarness.d.ts +2 -1
  45. package/dist/judges/judgeHarness.js +284 -32
  46. package/dist/judges/judgeHarness.js.map +1 -1
  47. package/dist/judges/judgeHarness.mjs +298 -32
  48. package/dist/judges/judgeHarness.mjs.map +1 -1
  49. package/dist/judges/structuredOutputJudge.d.mts +1 -0
  50. package/dist/judges/structuredOutputJudge.d.ts +1 -0
  51. package/dist/judges/toolCallJudge.d.mts +1 -0
  52. package/dist/judges/toolCallJudge.d.ts +1 -0
  53. package/dist/judges/toolCallJudge.js +34 -12
  54. package/dist/judges/toolCallJudge.js.map +1 -1
  55. package/dist/judges/toolCallJudge.mjs +48 -12
  56. package/dist/judges/toolCallJudge.mjs.map +1 -1
  57. package/dist/judges/types.d.mts +2 -1
  58. package/dist/judges/types.d.ts +2 -1
  59. package/dist/legacy/scorers/index.js +34 -12
  60. package/dist/legacy/scorers/index.js.map +1 -1
  61. package/dist/legacy/scorers/index.mjs +48 -12
  62. package/dist/legacy/scorers/index.mjs.map +1 -1
  63. package/dist/legacy/scorers/toolCallScorer.js +34 -12
  64. package/dist/legacy/scorers/toolCallScorer.js.map +1 -1
  65. package/dist/legacy/scorers/toolCallScorer.mjs +48 -12
  66. package/dist/legacy/scorers/toolCallScorer.mjs.map +1 -1
  67. package/dist/legacy.js +40 -18
  68. package/dist/legacy.js.map +1 -1
  69. package/dist/legacy.mjs +54 -18
  70. package/dist/legacy.mjs.map +1 -1
  71. package/dist/replay.d.mts +1 -1
  72. package/dist/replay.d.ts +1 -1
  73. package/dist/reporter.d.mts +5 -0
  74. package/dist/reporter.d.ts +5 -0
  75. package/dist/reporter.js +30 -7
  76. package/dist/reporter.js.map +1 -1
  77. package/dist/reporter.mjs +44 -7
  78. package/dist/reporter.mjs.map +1 -1
  79. package/package.json +9 -1
package/dist/harness.d.ts CHANGED
@@ -1,173 +1,35 @@
1
- /** Primitive scalar values allowed in normalized JSON-safe eval data. */
2
- type JsonPrimitive = string | number | boolean | null;
3
- /** JSON-safe value shape used by normalized sessions, artifacts, and errors. */
4
- type JsonValue = JsonPrimitive | JsonValue[] | {
5
- [key: string]: JsonValue;
1
+ import { JsonValue, HarnessRun, GenAiOperationName, ToolCallRecord, NormalizedSpanEvent, NormalizedSpan, NormalizedTrace, NormalizedMessage, UsageSummary, TimingSummary, NormalizedSpanAttributes, HarnessRunError, NormalizedSession } from '@vitest-evals/core';
2
+ export { GenAiOperationName, GenAiOutputType, GenAiProviderName, GenAiSemanticAttributeKey, GenAiSemanticAttributes, GenAiTokenType, GenAiToolType, HarnessRun, HarnessRunError, JsonPrimitive, JsonValue, NormalizedMessage, NormalizedSession, NormalizedSpan, NormalizedSpanAttributeKey, NormalizedSpanAttributes, NormalizedSpanEvent, NormalizedTrace, OpenTelemetrySemanticAttributeKey, OpenTelemetrySemanticAttributes, TimingSummary, ToolCallRecord, UsageSummary, assistantMessages, failedSpans, latestAssistantMessageContent, messagesByRole, spans, spansByKind, systemMessages, toolCalls, toolMessages, userMessages } from '@vitest-evals/core';
3
+
4
+ /** Options for converting normalized tool calls into trace spans. */
5
+ type CreateToolCallSpansOptions = {
6
+ /** Trace id to attach to each generated tool span. */
7
+ traceId?: string;
8
+ /** Parent span id to attach to each generated tool span. */
9
+ parentId?: string;
10
+ /** Prefix used to create internal span ids instead of reusing tool-call ids. */
11
+ spanIdPrefix?: string;
6
12
  };
7
- /**
8
- * Normalized record for one tool call observed during a harness run.
9
- *
10
- * @example
11
- * ```ts
12
- * const call: ToolCallRecord = {
13
- * name: "lookupInvoice",
14
- * arguments: { invoiceId: "inv_123" },
15
- * result: { refundable: true },
16
- * };
17
- * ```
18
- */
19
- type ToolCallRecord = {
20
- /** Provider or runtime tool-call id when one is available. */
21
- id?: string;
22
- /** Tool name as exposed to the agent or application runtime. */
13
+ /** Options for attaching a fallback run trace to a harness result. */
14
+ type EnsureRunTraceOptions = {
15
+ /** Human-readable run or harness name. */
23
16
  name: string;
24
- /** JSON-safe tool arguments after provider/runtime normalization. */
25
- arguments?: Record<string, JsonValue>;
26
- /** JSON-safe tool result returned by the application tool. */
27
- result?: JsonValue;
28
- /** Normalized tool error when execution failed. */
29
- error?: {
30
- message: string;
31
- type?: string;
32
- [key: string]: JsonValue | undefined;
33
- };
34
- /** ISO timestamp for the start of tool execution. */
35
- startedAt?: string;
36
- /** ISO timestamp for the end of tool execution. */
37
- finishedAt?: string;
38
- /** Tool execution duration in milliseconds. */
39
- durationMs?: number;
40
- /** Extra JSON-safe tool metadata for reporters and custom judges. */
41
- metadata?: Record<string, JsonValue>;
42
- };
43
- /**
44
- * Normalized message recorded in a harness session transcript.
45
- *
46
- * @example
47
- * ```ts
48
- * const message: NormalizedMessage = {
49
- * role: "assistant",
50
- * content: { status: "approved" },
51
- * toolCalls: [{ name: "lookupInvoice" }],
52
- * };
53
- * ```
54
- */
55
- type NormalizedMessage = {
56
- /** Transcript role for the normalized message. */
57
- role: "system" | "user" | "assistant" | "tool";
58
- /** JSON-safe message content. */
59
- content?: JsonValue;
60
- /** Tool calls associated with this message. */
61
- toolCalls?: ToolCallRecord[];
62
- /** Extra JSON-safe message metadata. */
63
- metadata?: Record<string, JsonValue>;
64
- };
65
- /**
66
- * Provider usage summary attached to a normalized harness run.
67
- *
68
- * @example
69
- * ```ts
70
- * const usage: UsageSummary = {
71
- * provider: "openai",
72
- * model: "gpt-4o-mini",
73
- * inputTokens: 212,
74
- * outputTokens: 48,
75
- * totalTokens: 260,
76
- * };
77
- * ```
78
- */
79
- type UsageSummary = {
80
- /** Provider that served the application run. */
81
- provider?: string;
82
- /** Model used for the application run. */
83
- model?: string;
84
- /** Input, prompt, or request tokens consumed by the run. */
85
- inputTokens?: number;
86
- /** Output or completion tokens produced by the run. */
87
- outputTokens?: number;
88
- /** Reasoning tokens reported by providers that expose them. */
89
- reasoningTokens?: number;
90
- /** Total token count reported by the provider or adapter. */
91
- totalTokens?: number;
92
- /** Count of tool calls observed during the run. */
93
- toolCalls?: number;
94
- /** Retry count observed during the run. */
95
- retries?: number;
96
- /** Provider-specific JSON-safe usage details. Cost estimates belong here. */
97
- metadata?: Record<string, JsonValue>;
98
- };
99
- /** Timing summary attached to a normalized harness run. */
100
- type TimingSummary = {
101
- /** End-to-end run duration in milliseconds. */
102
- totalMs?: number;
103
- /** Extra JSON-safe timing metadata. */
104
- metadata?: Record<string, JsonValue>;
105
- };
106
- /**
107
- * JSON-serializable transcript produced by the system under test.
108
- *
109
- * @example
110
- * ```ts
111
- * const session: NormalizedSession = {
112
- * provider: "openai",
113
- * model: "gpt-4o-mini",
114
- * messages: [
115
- * { role: "user", content: "Refund invoice inv_123" },
116
- * { role: "assistant", content: { status: "approved" } },
117
- * ],
118
- * };
119
- * ```
120
- */
121
- type NormalizedSession = {
122
- /** Ordered normalized transcript messages. */
123
- messages: NormalizedMessage[];
124
- /** Provider that produced the session when known. */
125
- provider?: string;
126
- /** Model that produced the session when known. */
127
- model?: string;
128
- /** Extra JSON-safe session metadata. */
129
- metadata?: Record<string, JsonValue>;
17
+ /** Wall-clock start time for the harness run. */
18
+ startedAt: Date;
19
+ /** Wall-clock finish time for the harness run. */
20
+ finishedAt: Date;
21
+ /** Optional trace id. A generated id is used when omitted. */
22
+ id?: string;
23
+ /** GenAI operation name to place on the root run span. */
24
+ operationName?: GenAiOperationName;
25
+ /** Optional JSON-safe source marker for the trace metadata. */
26
+ source?: string;
130
27
  };
131
28
  type OutputField<TOutput extends JsonValue | undefined> = undefined extends TOutput ? {
132
29
  output?: TOutput;
133
30
  } : {
134
31
  output: TOutput;
135
32
  };
136
- /**
137
- * Normalized result returned by every harness execution.
138
- *
139
- * @example
140
- * ```ts
141
- * const run: HarnessRun<{ status: "approved" }> = {
142
- * output: { status: "approved" },
143
- * session: {
144
- * messages: [
145
- * { role: "user", content: "Refund invoice inv_123" },
146
- * { role: "assistant", content: { status: "approved" } },
147
- * ],
148
- * },
149
- * usage: { totalTokens: 260 },
150
- * errors: [],
151
- * };
152
- * ```
153
- */
154
- type HarnessRun<TOutput extends JsonValue | undefined = JsonValue | undefined> = OutputField<TOutput> & {
155
- /** Normalized transcript and provider/session metadata. */
156
- session: NormalizedSession;
157
- /** Stable provider usage units such as tokens, tools, and retries. */
158
- usage: UsageSummary;
159
- /** Optional timing summary for the run. */
160
- timings?: TimingSummary;
161
- /** JSON-safe run artifacts captured by the harness or test context. */
162
- artifacts?: Record<string, JsonValue>;
163
- /** Normalized errors captured during execution. */
164
- errors: Array<Record<string, JsonValue>>;
165
- };
166
- /** Error value with an attached partial or complete normalized harness run. */
167
- type HarnessRunError = Error & {
168
- /** Attached normalized harness run recovered by `getHarnessRunFromError(...)`. */
169
- vitestEvalsRun: HarnessRun;
170
- };
171
33
  /** Per-run metadata shape accepted by harnesses and eval tests. */
172
34
  type HarnessMetadata = Record<string, unknown>;
173
35
  /**
@@ -232,6 +94,27 @@ type SimpleToolCallRecord = Omit<ToolCallRecord, "arguments" | "result" | "error
232
94
  /** Raw tool metadata accepted by `createHarness(...)` before normalization. */
233
95
  metadata?: Record<string, unknown>;
234
96
  };
97
+ /** Lightweight span event accepted by `createHarness(...)` results. */
98
+ type SimpleSpanEvent = Omit<NormalizedSpanEvent, "attributes"> & {
99
+ /** Raw event attributes accepted by `createHarness(...)` before normalization. */
100
+ attributes?: Record<string, unknown>;
101
+ };
102
+ /** Lightweight span record accepted by `createHarness(...)` results. */
103
+ type SimpleSpanRecord = Omit<NormalizedSpan, "attributes" | "error" | "events"> & {
104
+ /** Raw span attributes accepted by `createHarness(...)` before normalization. */
105
+ attributes?: Record<string, unknown>;
106
+ /** Raw span error accepted by `createHarness(...)` before normalization. */
107
+ error?: unknown;
108
+ /** Raw span events accepted by `createHarness(...)` before normalization. */
109
+ events?: SimpleSpanEvent[];
110
+ };
111
+ /** Lightweight trace record accepted by `createHarness(...)` results. */
112
+ type SimpleTraceRecord = Omit<NormalizedTrace, "metadata" | "spans"> & {
113
+ /** Raw trace metadata accepted by `createHarness(...)` before normalization. */
114
+ metadata?: Record<string, unknown>;
115
+ /** Lightweight spans to normalize into the trace. */
116
+ spans: SimpleSpanRecord[];
117
+ };
235
118
  /**
236
119
  * Lightweight result shape normalized by `createHarness(...)`.
237
120
  *
@@ -255,6 +138,8 @@ type SimpleHarnessResult<TOutput extends JsonValue | undefined = JsonValue | und
255
138
  timings?: TimingSummary;
256
139
  /** Raw artifact values to normalize and merge into the run. */
257
140
  artifacts?: Record<string, unknown>;
141
+ /** Lightweight traces and spans to normalize into the run. */
142
+ traces?: SimpleTraceRecord[];
258
143
  /** Raw session metadata to normalize into the session. */
259
144
  metadata?: Record<string, unknown>;
260
145
  /** Raw errors to normalize into the run. */
@@ -355,87 +240,44 @@ declare function createHarness<TInput = unknown, TOutput extends JsonValue | und
355
240
  */
356
241
  declare function normalizeHarnessRun<TInput = unknown, TMetadata extends HarnessMetadata = HarnessMetadata, TOutput extends JsonValue | undefined = JsonValue | undefined>(input: TInput, result: HarnessResultLike<TOutput>, context?: HarnessContext<TMetadata>): HarnessRun<TOutput>;
357
242
  /**
358
- * Flattens every recorded tool call from a normalized session.
359
- *
360
- * @param session - Normalized session produced by a harness run.
361
- *
362
- * @example
363
- * ```ts
364
- * const names = toolCalls(result.session).map((call) => call.name);
365
- *
366
- * expect(names).toEqual(["lookupInvoice", "createRefund"]);
367
- * ```
368
- */
369
- declare function toolCalls(session: NormalizedSession): ToolCallRecord[];
370
- /**
371
- * Filters normalized session messages by role.
372
- *
373
- * @param session - Normalized session produced by a harness run.
374
- * @param role - Message role to keep.
375
- *
376
- * @example
377
- * ```ts
378
- * const assistantText = messagesByRole(result.session, "assistant")
379
- * .map((message) => message.content)
380
- * .join("\n");
381
- * ```
382
- */
383
- declare function messagesByRole(session: NormalizedSession, role: NormalizedMessage["role"]): NormalizedMessage[];
384
- /**
385
- * Returns every normalized system message from a session.
386
- *
387
- * @param session - Normalized session produced by a harness run.
388
- *
389
- * @example
390
- * ```ts
391
- * const systemPrompts = systemMessages(result.session);
392
- * ```
393
- */
394
- declare function systemMessages(session: NormalizedSession): NormalizedMessage[];
395
- /**
396
- * Returns every normalized user message from a session.
243
+ * Builds a JSON-safe failed run for errors that happen before a harness can return.
397
244
  *
398
- * @param session - Normalized session produced by a harness run.
399
- *
400
- * @example
401
- * ```ts
402
- * const firstPrompt = userMessages(result.session)[0]?.content;
403
- * ```
404
- */
405
- declare function userMessages(session: NormalizedSession): NormalizedMessage[];
406
- /**
407
- * Returns every normalized assistant message from a session.
408
- *
409
- * @param session - Normalized session produced by a harness run.
410
- *
411
- * @example
412
- * ```ts
413
- * const finalAnswer = assistantMessages(result.session).at(-1)?.content;
414
- * ```
245
+ * @param input - Original input passed to the harness.
246
+ * @param error - Error thrown by setup or execution.
247
+ * @param options - Optional artifacts to preserve on the failed run.
415
248
  */
416
- declare function assistantMessages(session: NormalizedSession): NormalizedMessage[];
249
+ declare function createFailedHarnessRun(input: unknown, error: unknown, options?: {
250
+ artifacts?: Record<string, JsonValue>;
251
+ }): HarnessRun;
252
+ /** Normalizes arbitrary span errors while preserving object-shaped messages. */
253
+ declare function normalizeSpanError(error: unknown): NormalizedSpan["error"] | undefined;
254
+ /** Normalizes raw span attributes into the JSON-safe span attribute shape. */
255
+ declare function normalizeSpanAttributes(attributes: Record<string, unknown>): NormalizedSpanAttributes | undefined;
256
+ /** Builds common OpenTelemetry GenAI usage attributes from a usage summary. */
257
+ declare function createGenAiUsageAttributes(usage: UsageSummary | undefined, options?: {
258
+ provider?: string;
259
+ }): {
260
+ "gen_ai.provider.name": string | undefined;
261
+ "gen_ai.request.model": string | undefined;
262
+ "gen_ai.response.model": string | undefined;
263
+ "gen_ai.usage.input_tokens": number | undefined;
264
+ "gen_ai.usage.output_tokens": number | undefined;
265
+ "gen_ai.usage.reasoning.output_tokens": number | undefined;
266
+ };
417
267
  /**
418
- * Returns the latest assistant message content, ignoring empty text messages.
419
- *
420
- * @param session - Normalized session produced by a harness run.
268
+ * Converts normalized tool-call records into trace spans.
421
269
  *
422
- * @example
423
- * ```ts
424
- * const finalAnswer = latestAssistantMessageContent(result.session);
425
- * ```
270
+ * Tool-call ids are preserved as GenAI attributes. Pass `spanIdPrefix` when the
271
+ * spans belong to a known trace so span ids stay internally unique.
426
272
  */
427
- declare function latestAssistantMessageContent(session: NormalizedSession): JsonValue | undefined;
273
+ declare function createToolCallSpans(calls: ToolCallRecord[], options?: CreateToolCallSpansOptions): NormalizedSpan[];
428
274
  /**
429
- * Returns every normalized tool message from a session.
430
- *
431
- * @param session - Normalized session produced by a harness run.
275
+ * Attaches a fallback run trace when a harness result does not already contain spans.
432
276
  *
433
- * @example
434
- * ```ts
435
- * const toolOutputs = toolMessages(result.session).map((message) => message.content);
436
- * ```
277
+ * This keeps custom harnesses inspectable while first-party harness packages
278
+ * remain free to attach richer native traces.
437
279
  */
438
- declare function toolMessages(session: NormalizedSession): NormalizedMessage[];
280
+ declare function ensureRunTrace(run: HarnessRun, options: EnsureRunTraceOptions): NormalizedTrace | undefined;
439
281
  /**
440
282
  * Attaches a partial or complete harness run to an arbitrary thrown error.
441
283
  *
@@ -476,4 +318,4 @@ declare function resolveHarnessRunErrors(result: unknown): Array<Record<string,
476
318
  /** Serializes an arbitrary thrown value into the normalized error shape. */
477
319
  declare function serializeError(error: unknown): Record<string, JsonValue>;
478
320
 
479
- export { type CreateHarnessOptions, type CreateHarnessRunArgs, type Harness, type HarnessContext, type HarnessMetadata, type HarnessResultLike, type HarnessRun, type HarnessRunError, type JsonPrimitive, type JsonValue, type MaybePromise, type NormalizedMessage, type NormalizedSession, type SimpleHarnessResult, type SimpleToolCallRecord, type TimingSummary, type ToolCallRecord, type UsageSummary, assistantMessages, attachHarnessRunToError, createHarness, getHarnessRunFromError, hasCallableMethod, isHarnessRun, isNormalizedSession, latestAssistantMessageContent, messagesByRole, normalizeContent, normalizeHarnessRun, normalizeMetadata, normalizeRecord, resolveHarnessRunErrors, serializeError, systemMessages, toJsonValue, toolCalls, toolMessages, userMessages };
321
+ export { type CreateHarnessOptions, type CreateHarnessRunArgs, type CreateToolCallSpansOptions, type EnsureRunTraceOptions, type Harness, type HarnessContext, type HarnessMetadata, type HarnessResultLike, type MaybePromise, type SimpleHarnessResult, type SimpleSpanEvent, type SimpleSpanRecord, type SimpleToolCallRecord, type SimpleTraceRecord, attachHarnessRunToError, createFailedHarnessRun, createGenAiUsageAttributes, createHarness, createToolCallSpans, ensureRunTrace, getHarnessRunFromError, hasCallableMethod, isHarnessRun, isNormalizedSession, normalizeContent, normalizeHarnessRun, normalizeMetadata, normalizeRecord, normalizeSpanAttributes, normalizeSpanError, resolveHarnessRunErrors, serializeError, toJsonValue };