1
1
/* eslint-disable @typescript-eslint/no-dynamic-delete */
2
2
/* eslint-disable complexity */
3
- import type { IntegrationFn } from '@sentry/core' ;
3
+ import type { Client , IntegrationFn } from '@sentry/core' ;
4
4
import { defineIntegration , SEMANTIC_ATTRIBUTE_SENTRY_OP , spanToJSON } from '@sentry/core' ;
5
5
import { generateInstrumentOnce } from '../../../otel/instrument' ;
6
6
import { addOriginToSpan } from '../../../utils/addOriginToSpan' ;
7
+ import type { modulesIntegration } from '../../modules' ;
7
8
import {
8
9
AI_MODEL_ID_ATTRIBUTE ,
9
10
AI_MODEL_PROVIDER_ATTRIBUTE ,
@@ -23,6 +24,15 @@ import type { VercelAiOptions } from './types';
23
24
24
25
export const instrumentVercelAi = generateInstrumentOnce ( INTEGRATION_NAME , ( ) => new SentryVercelAiInstrumentation ( { } ) ) ;
25
26
27
+ /**
28
+ * Determines if the integration should be forced based on environment and package availability.
29
+ * Returns true if the 'ai' package is available.
30
+ */
31
+ function shouldForceIntegration ( client : Client ) : boolean {
32
+ const modules = client . getIntegrationByName < ReturnType < typeof modulesIntegration > > ( 'Modules' ) ;
33
+ return ! ! modules ?. getModules ?.( ) ?. ai ;
34
+ }
35
+
26
36
const _vercelAIIntegration = ( ( options : VercelAiOptions = { } ) => {
27
37
let instrumentation : undefined | SentryVercelAiInstrumentation ;
28
38
@@ -190,7 +200,11 @@ const _vercelAIIntegration = ((options: VercelAiOptions = {}) => {
190
200
} ) ;
191
201
}
192
202
193
- if ( options . force ) {
203
+ // Auto-detect if we should force the integration when running with 'ai' package available
204
+ // Note that this can only be detected if the 'Modules' integration is available, and running in CJS mode
205
+ const shouldForce = options . force ?? shouldForceIntegration ( client ) ;
206
+
207
+ if ( shouldForce ) {
194
208
registerProcessors ( ) ;
195
209
} else {
196
210
instrumentation ?. callWhenPatched ( registerProcessors ) ;
@@ -213,6 +227,9 @@ const _vercelAIIntegration = ((options: VercelAiOptions = {}) => {
213
227
* });
214
228
* ```
215
229
*
230
+ * The integration automatically detects when to force registration in CommonJS environments
231
+ * when the 'ai' package is available. You can still manually set the `force` option if needed.
232
+ *
216
233
* By default this integration adds tracing support to all `ai` function calls. If you need to disable
217
234
* collecting spans for a specific call, you can do so by setting `experimental_telemetry.isEnabled` to
218
235
* `false` in the first argument of the function call.
0 commit comments