|
| 1 | +import { expect, test } from '@playwright/test'; |
| 2 | +import { |
| 3 | + GEN_AI_INPUT_MESSAGES, |
| 4 | + GEN_AI_OPERATION_NAME, |
| 5 | + GEN_AI_OUTPUT_MESSAGES, |
| 6 | + GEN_AI_PROVIDER_NAME, |
| 7 | + GEN_AI_REQUEST_MODEL, |
| 8 | + GEN_AI_USAGE_INPUT_TOKENS, |
| 9 | + SENTRY_ORIGIN, |
| 10 | + URL_FULL, |
| 11 | +} from '@sentry/conventions/attributes'; |
| 12 | +import { GEN_AI_EVALUATE, HTTP_SERVER } from '@sentry/conventions/op'; |
| 13 | +import { collectStreamedSpans, getSpanOp, SerializedStreamedSpan } from '@sentry-internal/test-utils'; |
| 14 | + |
| 15 | +const APP = 'node-mastra'; |
| 16 | +const TICKET = 'My checkout page shows a blank screen after I click Pay.'; |
| 17 | + |
| 18 | +const isEvaluateSpan = (span: SerializedStreamedSpan): boolean => getSpanOp(span) === GEN_AI_EVALUATE; |
| 19 | +const isClassifyServerSpan = (span: SerializedStreamedSpan): boolean => |
| 20 | + getSpanOp(span) === HTTP_SERVER && String(span.attributes?.[URL_FULL]?.value ?? '').includes('/classify'); |
| 21 | + |
| 22 | +test('captures a Mastra classifier (Jev) evaluation as a gen_ai.evaluate span', async ({ baseURL }) => { |
| 23 | + const spansPromise = collectStreamedSpans( |
| 24 | + APP, |
| 25 | + spansOfTrace => spansOfTrace.some(isEvaluateSpan) && spansOfTrace.some(isClassifyServerSpan), |
| 26 | + ); |
| 27 | + |
| 28 | + const res = await fetch(`${baseURL}/classify`, { |
| 29 | + method: 'POST', |
| 30 | + headers: { 'Content-Type': 'application/json' }, |
| 31 | + body: JSON.stringify({ ticket: TICKET }), |
| 32 | + }); |
| 33 | + expect(res.status).toBe(200); |
| 34 | + await res.json(); |
| 35 | + |
| 36 | + const spans = await spansPromise; |
| 37 | + const serverSpan = spans.find(isClassifyServerSpan)!; |
| 38 | + |
| 39 | + // Only the Mastra exporter reports the evaluation: `Classifier` calls `doEvaluate()` directly, so the |
| 40 | + // Vercel AI integration does not see it. |
| 41 | + const evaluateSpans = spans.filter(isEvaluateSpan); |
| 42 | + expect(evaluateSpans.map(span => ({ name: span.name, origin: span.attributes?.[SENTRY_ORIGIN]?.value }))).toEqual([ |
| 43 | + { name: 'evaluate typesafe/jev-1.13', origin: 'auto.ai.mastra' }, |
| 44 | + ]); |
| 45 | + |
| 46 | + const evaluateSpan = evaluateSpans[0]!; |
| 47 | + expect(evaluateSpan.parent_span_id).toBe(serverSpan.span_id); |
| 48 | + expect(evaluateSpan.attributes?.[GEN_AI_OPERATION_NAME]?.value).toBe('evaluate'); |
| 49 | + expect(evaluateSpan.attributes?.[GEN_AI_REQUEST_MODEL]?.value).toBe('typesafe/jev-1.13'); |
| 50 | + expect(evaluateSpan.attributes?.[GEN_AI_PROVIDER_NAME]?.value).toBe('openrouter'); |
| 51 | + expect(evaluateSpan.attributes?.[GEN_AI_USAGE_INPUT_TOKENS]?.value).toBeGreaterThan(0); |
| 52 | + expect(JSON.parse(String(evaluateSpan.attributes?.[GEN_AI_INPUT_MESSAGES]?.value))).toEqual([ |
| 53 | + { |
| 54 | + type: 'evaluation', |
| 55 | + state: { ticket: TICKET }, |
| 56 | + questions: { |
| 57 | + is_bug: { type: 'boolean', instructions: 'Is the customer reporting a software defect?' }, |
| 58 | + }, |
| 59 | + }, |
| 60 | + ]); |
| 61 | + // The probability comes from the live model, so only its shape is stable. |
| 62 | + expect(JSON.parse(String(evaluateSpan.attributes?.[GEN_AI_OUTPUT_MESSAGES]?.value))).toEqual([ |
| 63 | + { type: 'evaluation', answers: { is_bug: { type: 'boolean', probability: expect.any(Number) } } }, |
| 64 | + ]); |
| 65 | +}); |
0 commit comments