|
| 1 | +/** |
| 2 | + * This is a copy of the Vercel AI integration from the node SDK. |
| 3 | + * |
| 4 | + * The only difference is that it does not use `@opentelemetry/instrumentation` |
| 5 | + * because Cloudflare Workers do not support it. |
| 6 | + * |
| 7 | + * Therefore, we cannot automatically patch setting `experimental_telemetry: { isEnabled: true }` |
| 8 | + * and users have to manually set this to get spans. |
| 9 | + */ |
| 10 | + |
| 11 | +import type { IntegrationFn } from '@sentry/core'; |
| 12 | +import { addVercelAiProcessors, defineIntegration } from '@sentry/core'; |
| 13 | + |
| 14 | +const INTEGRATION_NAME = 'VercelAI'; |
| 15 | + |
| 16 | +const _vercelAIIntegration = (() => { |
| 17 | + return { |
| 18 | + name: INTEGRATION_NAME, |
| 19 | + setup(client) { |
| 20 | + addVercelAiProcessors(client); |
| 21 | + }, |
| 22 | + }; |
| 23 | +}) satisfies IntegrationFn; |
| 24 | + |
| 25 | +/** |
| 26 | + * Adds Sentry tracing instrumentation for the [ai](https://www.npmjs.com/package/ai) library. |
| 27 | + * This integration is not enabled by default, you need to manually add it. |
| 28 | + * |
| 29 | + * For more information, see the [`ai` documentation](https://sdk.vercel.ai/docs/ai-sdk-core/telemetry). |
| 30 | + * |
| 31 | + * You need to enable collecting spans for a specific call by setting |
| 32 | + * `experimental_telemetry.isEnabled` to `true` in the first argument of the function call. |
| 33 | + * |
| 34 | + * ```javascript |
| 35 | + * const result = await generateText({ |
| 36 | + * model: openai('gpt-4-turbo'), |
| 37 | + * experimental_telemetry: { isEnabled: true }, |
| 38 | + * }); |
| 39 | + * ``` |
| 40 | + * |
| 41 | + * If you want to collect inputs and outputs for a specific call, you must specifically opt-in to each |
| 42 | + * function call by setting `experimental_telemetry.recordInputs` and `experimental_telemetry.recordOutputs` |
| 43 | + * to `true`. |
| 44 | + * |
| 45 | + * ```javascript |
| 46 | + * const result = await generateText({ |
| 47 | + * model: openai('gpt-4-turbo'), |
| 48 | + * experimental_telemetry: { isEnabled: true, recordInputs: true, recordOutputs: true }, |
| 49 | + * }); |
| 50 | + */ |
| 51 | +export const vercelAIIntegration = defineIntegration(_vercelAIIntegration); |
0 commit comments