|
1 | | -import { describe, expect, it, vi } from 'vitest'; |
| 1 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; |
| 2 | +import { getCurrentScope, getGlobalScope, getIsolationScope, setCurrentClient, startSpan } from '../../../src'; |
| 3 | +import { addVercelAiProcessors } from '../../../src/tracing/vercel-ai'; |
| 4 | +import { AI_OPERATION_ID_ATTRIBUTE } from '../../../src/tracing/vercel-ai/vercel-ai-attributes'; |
2 | 5 | import { instrumentWorkersAiClient } from '../../../src/tracing/workers-ai'; |
| 6 | +import { _INTERNAL_clearAiProviderSkips } from '../../../src/utils/ai/providerSkip'; |
| 7 | +import { spanToJSON } from '../../../src/utils/spanUtils'; |
| 8 | +import { getDefaultTestClientOptions, TestClient } from '../../mocks/client'; |
3 | 9 |
|
4 | 10 | describe('instrumentWorkersAiClient', () => { |
5 | 11 | it('passes through non-run methods bound to the original client', () => { |
@@ -28,4 +34,85 @@ describe('instrumentWorkersAiClient', () => { |
28 | 34 | expect(client.run).toHaveBeenCalledWith('@cf/meta/llama-3.1-8b-instruct', { prompt: 'Hello' }); |
29 | 35 | expect(result).toEqual({ response: 'Paris' }); |
30 | 36 | }); |
| 37 | + |
| 38 | + describe('when the Vercel AI SDK drives the binding', () => { |
| 39 | + let spans: string[]; |
| 40 | + |
| 41 | + /** Set up a client with the Vercel AI processors registered, recording every ended span. */ |
| 42 | + function setupClient(): void { |
| 43 | + getCurrentScope().clear(); |
| 44 | + getIsolationScope().clear(); |
| 45 | + getGlobalScope().clear(); |
| 46 | + |
| 47 | + spans = []; |
| 48 | + const client = new TestClient(getDefaultTestClientOptions({ tracesSampleRate: 1 })); |
| 49 | + client.on('spanEnd', span => { |
| 50 | + spans.push(spanToJSON(span).description ?? ''); |
| 51 | + }); |
| 52 | + setCurrentClient(client); |
| 53 | + addVercelAiProcessors(client); |
| 54 | + } |
| 55 | + |
| 56 | + beforeEach(() => { |
| 57 | + _INTERNAL_clearAiProviderSkips(); |
| 58 | + setupClient(); |
| 59 | + }); |
| 60 | + |
| 61 | + afterEach(() => { |
| 62 | + _INTERNAL_clearAiProviderSkips(); |
| 63 | + }); |
| 64 | + |
| 65 | + /** |
| 66 | + * Emit the span the `ai` SDK creates for a model call. Its `spanStart` handler is what marks |
| 67 | + * Workers AI as skipped, exactly as it would at runtime. |
| 68 | + */ |
| 69 | + async function withVercelAiModelCall(callback: () => Promise<unknown>): Promise<void> { |
| 70 | + await startSpan( |
| 71 | + { name: 'ai.streamText.doStream', attributes: { [AI_OPERATION_ID_ATTRIBUTE]: 'ai.streamText.doStream' } }, |
| 72 | + async () => { |
| 73 | + await callback(); |
| 74 | + }, |
| 75 | + ); |
| 76 | + } |
| 77 | + |
| 78 | + it('does not create a duplicate span for the nested `run` call', async () => { |
| 79 | + const client = { run: vi.fn().mockResolvedValue({ response: 'Paris' }) }; |
| 80 | + const instrumented = instrumentWorkersAiClient(client); |
| 81 | + |
| 82 | + await withVercelAiModelCall(() => instrumented.run('@cf/meta/llama-3.1-8b-instruct', { prompt: 'Hello' })); |
| 83 | + |
| 84 | + // The call is forwarded, but no duplicate `gen_ai.chat` span is emitted — only the |
| 85 | + // Vercel AI model-call span remains. |
| 86 | + expect(client.run).toHaveBeenCalledWith('@cf/meta/llama-3.1-8b-instruct', { prompt: 'Hello' }); |
| 87 | + expect(spans).not.toContain('chat @cf/meta/llama-3.1-8b-instruct'); |
| 88 | + expect(spans).toEqual(['streamText.doStream']); |
| 89 | + }); |
| 90 | + |
| 91 | + it('still creates a span for a direct `run` call made before any Vercel AI call', async () => { |
| 92 | + const client = { run: vi.fn().mockResolvedValue({ response: 'Paris' }) }; |
| 93 | + const instrumented = instrumentWorkersAiClient(client); |
| 94 | + |
| 95 | + await startSpan({ name: 'root' }, () => instrumented.run('@cf/meta/llama-3.1-8b-instruct', { prompt: 'Hello' })); |
| 96 | + |
| 97 | + expect(spans).toContain('chat @cf/meta/llama-3.1-8b-instruct'); |
| 98 | + }); |
| 99 | + |
| 100 | + it('clears the skip between clients so a later isolate reuse is unaffected', async () => { |
| 101 | + const client = { run: vi.fn().mockResolvedValue({ response: 'Paris' }) }; |
| 102 | + const instrumented = instrumentWorkersAiClient(client); |
| 103 | + |
| 104 | + // First request: the `ai` SDK runs and marks Workers AI as skipped. |
| 105 | + await withVercelAiModelCall(() => instrumented.run('@cf/meta/llama-3.1-8b-instruct', { prompt: 'Hello' })); |
| 106 | + expect(spans).not.toContain('chat @cf/meta/llama-3.1-8b-instruct'); |
| 107 | + |
| 108 | + // Second request on the same isolate: `_setupIntegrations` resets the registry, so a direct |
| 109 | + // `env.AI.run` call must get its span back. Without the reset this would stay suppressed. |
| 110 | + _INTERNAL_clearAiProviderSkips(); |
| 111 | + setupClient(); |
| 112 | + |
| 113 | + await startSpan({ name: 'root' }, () => instrumented.run('@cf/meta/llama-3.1-8b-instruct', { prompt: 'Hello' })); |
| 114 | + |
| 115 | + expect(spans).toContain('chat @cf/meta/llama-3.1-8b-instruct'); |
| 116 | + }); |
| 117 | + }); |
31 | 118 | }); |
0 commit comments