@@ -23,15 +23,16 @@ type MethodArgs = [MethodFirstArg, ...unknown[]];
2323type PatchedModuleExports = Record < ( typeof INSTRUMENTED_METHODS ) [ number ] , ( ...args : MethodArgs ) => unknown > &
2424 Record < string , unknown > ;
2525
26- export let sentryVercelAiPatched = false ;
27-
2826/**
2927 * This detects is added by the Sentry Vercel AI Integration to detect if the integration should
3028 * be enabled.
3129 *
3230 * It also patches the `ai` module to enable Vercel AI telemetry automatically for all methods.
3331 */
3432export class SentryVercelAiInstrumentation extends InstrumentationBase {
33+ private _isPatched = false ;
34+ private _callbacks : ( ( ) => void ) [ ] = [ ] ;
35+
3536 public constructor ( config : InstrumentationConfig = { } ) {
3637 super ( '@sentry/instrumentation-vercel-ai' , SDK_VERSION , config ) ;
3738 }
@@ -44,11 +45,26 @@ export class SentryVercelAiInstrumentation extends InstrumentationBase {
4445 return module ;
4546 }
4647
48+ /**
49+ * Call the provided callback when the module is patched.
50+ * If it has already been patched, the callback will be called immediately.
51+ */
52+ public callWhenPatched ( callback : ( ) => void ) : void {
53+ if ( this . _isPatched ) {
54+ callback ( ) ;
55+ } else {
56+ this . _callbacks . push ( callback ) ;
57+ }
58+ }
59+
4760 /**
4861 * Patches module exports to enable Vercel AI telemetry.
4962 */
5063 private _patch ( moduleExports : PatchedModuleExports ) : unknown {
51- sentryVercelAiPatched = true ;
64+ this . _isPatched = true ;
65+
66+ this . _callbacks . forEach ( callback => callback ( ) ) ;
67+ this . _callbacks = [ ] ;
5268
5369 function generatePatch ( name : string ) {
5470 return ( ...args : MethodArgs ) => {
0 commit comments