|
| 1 | +import { describe, expect, it } from 'vitest'; |
| 2 | +import { addVercelAiProcessors } from '../../../src/tracing/vercel-ai'; |
| 3 | +import type { SpanJSON } from '../../../src/types-hoist/span'; |
| 4 | +import { getDefaultTestClientOptions, TestClient } from '../../mocks/client'; |
| 5 | + |
| 6 | +describe('vercel-ai cached tokens', () => { |
| 7 | + it('should add cached input tokens to total input tokens', () => { |
| 8 | + const options = getDefaultTestClientOptions({ tracesSampleRate: 1.0 }); |
| 9 | + const client = new TestClient(options); |
| 10 | + client.init(); |
| 11 | + addVercelAiProcessors(client); |
| 12 | + |
| 13 | + const mockSpan: SpanJSON = { |
| 14 | + description: 'test', |
| 15 | + span_id: 'test-span-id', |
| 16 | + trace_id: 'test-trace-id', |
| 17 | + start_timestamp: 1000, |
| 18 | + timestamp: 2000, |
| 19 | + origin: 'auto.vercelai.otel', |
| 20 | + data: { |
| 21 | + 'ai.usage.promptTokens': 100, |
| 22 | + 'ai.usage.cachedInputTokens': 50, |
| 23 | + }, |
| 24 | + }; |
| 25 | + |
| 26 | + const event = { |
| 27 | + type: 'transaction' as const, |
| 28 | + spans: [mockSpan], |
| 29 | + }; |
| 30 | + |
| 31 | + const eventProcessor = client['_eventProcessors'].find(processor => processor.id === 'VercelAiEventProcessor'); |
| 32 | + expect(eventProcessor).toBeDefined(); |
| 33 | + |
| 34 | + const processedEvent = eventProcessor!(event, {}); |
| 35 | + |
| 36 | + expect(processedEvent?.spans?.[0]?.data?.['gen_ai.usage.input_tokens']).toBe(150); |
| 37 | + expect(processedEvent?.spans?.[0]?.data?.['gen_ai.usage.input_tokens.cached']).toBe(50); |
| 38 | + }); |
| 39 | +}); |
0 commit comments