@@ -211,47 +211,49 @@ export class SentryVercelAiInstrumentation extends InstrumentationBase {
211211 this . _callbacks . forEach ( callback => callback ( ) ) ;
212212 this . _callbacks = [ ] ;
213213
214- const generatePatch = ( originalMethod : ( ...args : MethodArgs ) => unknown ) => {
215- return ( ...args : MethodArgs ) => {
216- const existingExperimentalTelemetry = args [ 0 ] . experimental_telemetry || { } ;
217- const isEnabled = existingExperimentalTelemetry . isEnabled ;
218-
219- const client = getCurrentScope ( ) . getClient ( ) ;
220- const integration = client ?. getIntegrationByName < VercelAiIntegration > ( INTEGRATION_NAME ) ;
221- const integrationOptions = integration ?. options ;
222- const shouldRecordInputsAndOutputs = integration ? Boolean ( client ?. getOptions ( ) . sendDefaultPii ) : false ;
223-
224- const { recordInputs, recordOutputs } = determineRecordingSettings (
225- integrationOptions ,
226- existingExperimentalTelemetry ,
227- isEnabled ,
228- shouldRecordInputsAndOutputs ,
229- ) ;
230-
231- args [ 0 ] . experimental_telemetry = {
232- ...existingExperimentalTelemetry ,
233- isEnabled : isEnabled !== undefined ? isEnabled : true ,
234- recordInputs,
235- recordOutputs,
236- } ;
237-
238- return handleCallbackErrors (
239- ( ) => originalMethod . apply ( this , args ) ,
240- error => {
241- // This error bubbles up to unhandledrejection handler (if not handled before),
242- // where we do not know the active span anymore
243- // So to circumvent this, we set the active span on the error object
244- // which is picked up by the unhandledrejection handler
245- if ( error && typeof error === 'object' ) {
246- addNonEnumerableProperty ( error , '_sentry_active_span' , getActiveSpan ( ) ) ;
247- }
248- } ,
249- ( ) => { } ,
250- result => {
251- checkResultForToolErrors ( result ) ;
252- } ,
253- ) ;
254- } ;
214+ const generatePatch = < T extends ( ...args : MethodArgs ) => unknown > ( originalMethod : T ) : T => {
215+ return new Proxy ( originalMethod , {
216+ apply : ( target , thisArg , args : MethodArgs ) => {
217+ const existingExperimentalTelemetry = args [ 0 ] . experimental_telemetry || { } ;
218+ const isEnabled = existingExperimentalTelemetry . isEnabled ;
219+
220+ const client = getCurrentScope ( ) . getClient ( ) ;
221+ const integration = client ?. getIntegrationByName < VercelAiIntegration > ( INTEGRATION_NAME ) ;
222+ const integrationOptions = integration ?. options ;
223+ const shouldRecordInputsAndOutputs = integration ? Boolean ( client ?. getOptions ( ) . sendDefaultPii ) : false ;
224+
225+ const { recordInputs, recordOutputs } = determineRecordingSettings (
226+ integrationOptions ,
227+ existingExperimentalTelemetry ,
228+ isEnabled ,
229+ shouldRecordInputsAndOutputs ,
230+ ) ;
231+
232+ args [ 0 ] . experimental_telemetry = {
233+ ...existingExperimentalTelemetry ,
234+ isEnabled : isEnabled !== undefined ? isEnabled : true ,
235+ recordInputs,
236+ recordOutputs,
237+ } ;
238+
239+ return handleCallbackErrors (
240+ ( ) => originalMethod . apply ( this , args ) ,
241+ error => {
242+ // This error bubbles up to unhandledrejection handler (if not handled before),
243+ // where we do not know the active span anymore
244+ // So to circumvent this, we set the active span on the error object
245+ // which is picked up by the unhandledrejection handler
246+ if ( error && typeof error === 'object' ) {
247+ addNonEnumerableProperty ( error , '_sentry_active_span' , getActiveSpan ( ) ) ;
248+ }
249+ } ,
250+ ( ) => { } ,
251+ result => {
252+ checkResultForToolErrors ( result ) ;
253+ } ,
254+ ) ;
255+ } ,
256+ } ) ;
255257 } ;
256258
257259 // Is this an ESM module?
0 commit comments