|
| 1 | +import { |
| 2 | + GEN_AI_INPUT_MESSAGES, |
| 3 | + GEN_AI_OPERATION_NAME, |
| 4 | + GEN_AI_OUTPUT_MESSAGES, |
| 5 | + GEN_AI_PROVIDER_NAME, |
| 6 | + GEN_AI_REQUEST_MODEL, |
| 7 | + GEN_AI_RESPONSE_MODEL, |
| 8 | + GEN_AI_USAGE_INPUT_TOKENS, |
| 9 | + GEN_AI_USAGE_OUTPUT_TOKENS, |
| 10 | + GEN_AI_USAGE_TOTAL_TOKENS, |
| 11 | + SENTRY_OP, |
| 12 | + SENTRY_ORIGIN, |
| 13 | +} from '@sentry/conventions/attributes'; |
| 14 | +import { afterAll, describe, expect } from 'vitest'; |
| 15 | +import { cleanupChildProcesses, createEsmAndCjsTests } from '../../../utils/runner'; |
| 16 | + |
| 17 | +describe('TypeSafe integration', () => { |
| 18 | + afterAll(() => { |
| 19 | + cleanupChildProcesses(); |
| 20 | + }); |
| 21 | + |
| 22 | + describe.each([ |
| 23 | + ['automatic', 'scenario.mjs', 'instrument.mjs', 'instrument-no-recording.mjs'], |
| 24 | + ['manual', 'scenario-manual.mjs', 'instrument-manual.mjs', 'instrument-manual-no-recording.mjs'], |
| 25 | + ])('%s instrumentation', (_, scenarioFile, instrumentFile, noRecordingInstrumentFile) => { |
| 26 | + createEsmAndCjsTests( |
| 27 | + __dirname, |
| 28 | + scenarioFile, |
| 29 | + instrumentFile, |
| 30 | + (createRunner, test) => { |
| 31 | + test('creates evaluate spans for systemOne', async () => { |
| 32 | + await createRunner() |
| 33 | + .unordered() |
| 34 | + .expect({ |
| 35 | + span: container => { |
| 36 | + const evaluateSpans = container.items.filter( |
| 37 | + span => span.attributes[SENTRY_OP]?.value === 'gen_ai.evaluate', |
| 38 | + ); |
| 39 | + expect(evaluateSpans).toHaveLength(4); |
| 40 | + for (const span of evaluateSpans) { |
| 41 | + expect(span.attributes[SENTRY_ORIGIN]?.value).toBe('auto.ai.typesafe'); |
| 42 | + expect(span.attributes[GEN_AI_OPERATION_NAME]?.value).toBe('evaluate'); |
| 43 | + expect(span.attributes[GEN_AI_PROVIDER_NAME]?.value).toBe('typesafe'); |
| 44 | + } |
| 45 | + |
| 46 | + const defaultModelSpan = evaluateSpans.find(span => span.name === 'evaluate jev-latest')!; |
| 47 | + expect(defaultModelSpan).toBeDefined(); |
| 48 | + expect(defaultModelSpan.status).toBe('ok'); |
| 49 | + expect(defaultModelSpan.attributes[GEN_AI_REQUEST_MODEL]?.value).toBe('jev-latest'); |
| 50 | + expect(defaultModelSpan.attributes[GEN_AI_RESPONSE_MODEL]?.value).toBe('jev-1.13.0'); |
| 51 | + expect(defaultModelSpan.attributes[GEN_AI_USAGE_INPUT_TOKENS]?.value).toBe(275); |
| 52 | + expect(defaultModelSpan.attributes[GEN_AI_USAGE_OUTPUT_TOKENS]?.value).toBe(20); |
| 53 | + expect(defaultModelSpan.attributes[GEN_AI_USAGE_TOTAL_TOKENS]?.value).toBe(295); |
| 54 | + expect(JSON.parse(defaultModelSpan.attributes[GEN_AI_INPUT_MESSAGES]?.value as string)).toEqual([ |
| 55 | + { |
| 56 | + type: 'evaluation', |
| 57 | + state: 'I cannot log in, and I also want a refund for last month.', |
| 58 | + questions: { |
| 59 | + authIssue: { type: 'noul', instructions: 'Is there a login problem?' }, |
| 60 | + department: { |
| 61 | + type: 'choice', |
| 62 | + instructions: 'Which team should handle this?', |
| 63 | + criteria: { billing: 'Charges and refunds', technical: 'Bugs and outages' }, |
| 64 | + }, |
| 65 | + urgency: { |
| 66 | + type: 'score', |
| 67 | + instructions: 'How urgent is this ticket?', |
| 68 | + criteria: ['low', 'medium', 'high'], |
| 69 | + }, |
| 70 | + }, |
| 71 | + }, |
| 72 | + ]); |
| 73 | + expect(JSON.parse(defaultModelSpan.attributes[GEN_AI_OUTPUT_MESSAGES]?.value as string)).toEqual([ |
| 74 | + { |
| 75 | + type: 'evaluation', |
| 76 | + answers: { |
| 77 | + authIssue: { type: 'noul', noul: 0.98 }, |
| 78 | + department: { |
| 79 | + type: 'choice', |
| 80 | + choice: 'billing', |
| 81 | + confidence: 0.28, |
| 82 | + probabilities: { billing: 0.64, technical: 0.36 }, |
| 83 | + }, |
| 84 | + urgency: { |
| 85 | + type: 'score', |
| 86 | + score: 1.58, |
| 87 | + confidence: 0.9, |
| 88 | + legend: { 0: 'low', 1: 'medium', 2: 'high' }, |
| 89 | + probabilities: { 0: 0, 1: 0.42, 2: 0.58 }, |
| 90 | + }, |
| 91 | + }, |
| 92 | + }, |
| 93 | + ]); |
| 94 | + |
| 95 | + const asResponseSpan = evaluateSpans.find(span => span.name === 'evaluate jev-1.13')!; |
| 96 | + expect(asResponseSpan).toBeDefined(); |
| 97 | + expect(asResponseSpan.status).toBe('ok'); |
| 98 | + expect(asResponseSpan.attributes[GEN_AI_RESPONSE_MODEL]?.value).toBe('jev-1.13.0'); |
| 99 | + |
| 100 | + const errorSpan = evaluateSpans.find(span => span.name === 'evaluate error-model')!; |
| 101 | + expect(errorSpan).toBeDefined(); |
| 102 | + expect(errorSpan.status).toBe('error'); |
| 103 | + expect(errorSpan.attributes[GEN_AI_RESPONSE_MODEL]).toBeUndefined(); |
| 104 | + expect(errorSpan.attributes[GEN_AI_OUTPUT_MESSAGES]).toBeUndefined(); |
| 105 | + |
| 106 | + const validationErrorSpan = evaluateSpans.find(span => span.name === 'evaluate validation-error')!; |
| 107 | + expect(validationErrorSpan).toBeDefined(); |
| 108 | + expect(validationErrorSpan.status).toBe('error'); |
| 109 | + expect(validationErrorSpan.attributes[GEN_AI_OUTPUT_MESSAGES]).toBeUndefined(); |
| 110 | + }, |
| 111 | + }) |
| 112 | + .start() |
| 113 | + .completed(); |
| 114 | + }); |
| 115 | + }, |
| 116 | + { |
| 117 | + additionalDependencies: { |
| 118 | + '@typesafe-ai/sdk': '^0.6.0', |
| 119 | + }, |
| 120 | + }, |
| 121 | + ); |
| 122 | + |
| 123 | + createEsmAndCjsTests( |
| 124 | + __dirname, |
| 125 | + scenarioFile, |
| 126 | + noRecordingInstrumentFile, |
| 127 | + (createRunner, test) => { |
| 128 | + test('does not record inputs or outputs when recording is off', async () => { |
| 129 | + await createRunner() |
| 130 | + .unordered() |
| 131 | + .expect({ |
| 132 | + span: container => { |
| 133 | + const evaluateSpans = container.items.filter( |
| 134 | + span => span.attributes[SENTRY_OP]?.value === 'gen_ai.evaluate', |
| 135 | + ); |
| 136 | + expect(evaluateSpans).toHaveLength(4); |
| 137 | + expect(evaluateSpans.filter(span => span.status === 'error')).toHaveLength(2); |
| 138 | + for (const span of evaluateSpans) { |
| 139 | + expect(span.attributes[GEN_AI_INPUT_MESSAGES]).toBeUndefined(); |
| 140 | + expect(span.attributes[GEN_AI_OUTPUT_MESSAGES]).toBeUndefined(); |
| 141 | + // State, questions and answers must not come back through another attribute (e.g. an error message). |
| 142 | + // Only the attributes are checked (timestamps could match a number), and `probabilities` only |
| 143 | + // occurs in answers. |
| 144 | + expect(JSON.stringify(span.attributes)).not.toMatch( |
| 145 | + /cannot log in|Charges and refunds|probabilities/, |
| 146 | + ); |
| 147 | + } |
| 148 | + }, |
| 149 | + }) |
| 150 | + .start() |
| 151 | + .completed(); |
| 152 | + }); |
| 153 | + }, |
| 154 | + { |
| 155 | + additionalDependencies: { |
| 156 | + '@typesafe-ai/sdk': '^0.6.0', |
| 157 | + }, |
| 158 | + }, |
| 159 | + ); |
| 160 | + }); |
| 161 | +}); |
0 commit comments