braintrust 3.19.0 → 3.21.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (88) hide show
  1. package/NOTICE +20 -0
  2. package/README.md +2 -0
  3. package/dev/dist/index.d.mts +309 -199
  4. package/dev/dist/index.d.ts +309 -199
  5. package/dev/dist/index.js +2833 -987
  6. package/dev/dist/index.mjs +2294 -448
  7. package/dist/apply-auto-instrumentation.js +781 -199
  8. package/dist/apply-auto-instrumentation.mjs +588 -6
  9. package/dist/auto-instrumentations/bundler/esbuild.cjs +594 -2
  10. package/dist/auto-instrumentations/bundler/esbuild.d.mts +2 -2
  11. package/dist/auto-instrumentations/bundler/esbuild.d.ts +2 -2
  12. package/dist/auto-instrumentations/bundler/esbuild.mjs +3 -3
  13. package/dist/auto-instrumentations/bundler/next.cjs +594 -2
  14. package/dist/auto-instrumentations/bundler/next.mjs +4 -4
  15. package/dist/auto-instrumentations/bundler/rollup.cjs +594 -2
  16. package/dist/auto-instrumentations/bundler/rollup.d.mts +2 -2
  17. package/dist/auto-instrumentations/bundler/rollup.d.ts +2 -2
  18. package/dist/auto-instrumentations/bundler/rollup.mjs +3 -3
  19. package/dist/auto-instrumentations/bundler/vite.cjs +594 -2
  20. package/dist/auto-instrumentations/bundler/vite.d.mts +2 -2
  21. package/dist/auto-instrumentations/bundler/vite.d.ts +2 -2
  22. package/dist/auto-instrumentations/bundler/vite.mjs +3 -3
  23. package/dist/auto-instrumentations/bundler/webpack-loader.cjs +593 -9
  24. package/dist/auto-instrumentations/bundler/webpack-loader.d.ts +68 -1
  25. package/dist/auto-instrumentations/bundler/webpack.cjs +594 -2
  26. package/dist/auto-instrumentations/bundler/webpack.d.mts +2 -2
  27. package/dist/auto-instrumentations/bundler/webpack.d.ts +2 -2
  28. package/dist/auto-instrumentations/bundler/webpack.mjs +4 -4
  29. package/dist/auto-instrumentations/{chunk-W4E36GIW.mjs → chunk-BRQX23KL.mjs} +16 -0
  30. package/dist/auto-instrumentations/chunk-M6DLIJ2Z.mjs +784 -0
  31. package/dist/auto-instrumentations/{chunk-K74TZGGM.mjs → chunk-T6J4C7LX.mjs} +1 -1
  32. package/dist/auto-instrumentations/{chunk-S4374IP6.mjs → chunk-TKRPRPGD.mjs} +4 -6
  33. package/dist/auto-instrumentations/chunk-VT6DDNKM.mjs +856 -0
  34. package/dist/auto-instrumentations/hook.mjs +749 -45
  35. package/dist/auto-instrumentations/index.cjs +16 -0
  36. package/dist/auto-instrumentations/index.d.mts +2 -3
  37. package/dist/auto-instrumentations/index.d.ts +2 -3
  38. package/dist/auto-instrumentations/index.mjs +1 -1
  39. package/dist/auto-instrumentations/loader/cjs-patch.cjs +578 -4
  40. package/dist/auto-instrumentations/loader/cjs-patch.d.mts +1 -1
  41. package/dist/auto-instrumentations/loader/cjs-patch.d.ts +1 -1
  42. package/dist/auto-instrumentations/loader/cjs-patch.mjs +3 -7
  43. package/dist/auto-instrumentations/loader/esm-hook.mjs +2 -6
  44. package/dist/auto-instrumentations/{plugin-D7nDswtC.d.mts → plugin-D0KHwSJv.d.mts} +1 -1
  45. package/dist/auto-instrumentations/{plugin-D7nDswtC.d.ts → plugin-G6fgxk1b.d.ts} +1 -1
  46. package/dist/auto-instrumentations/types-RNPaKi9o.d.mts +70 -0
  47. package/dist/auto-instrumentations/types-RNPaKi9o.d.ts +70 -0
  48. package/dist/browser.d.mts +1369 -848
  49. package/dist/browser.d.ts +1369 -848
  50. package/dist/browser.js +2735 -473
  51. package/dist/browser.mjs +2735 -473
  52. package/dist/{chunk-LPC4W2WX.js → chunk-BFGIH2ZJ.js} +4 -0
  53. package/dist/{chunk-37RLJF2U.js → chunk-FY7DAKA5.js} +3149 -1273
  54. package/dist/{chunk-Y7W7WP6H.mjs → chunk-KMGUTPB7.mjs} +4 -0
  55. package/dist/{chunk-VEQ2PCMC.mjs → chunk-O2P765XK.mjs} +2399 -523
  56. package/dist/cli.js +2267 -412
  57. package/dist/edge-light.d.mts +1 -1
  58. package/dist/edge-light.d.ts +1 -1
  59. package/dist/edge-light.js +2735 -473
  60. package/dist/edge-light.mjs +2735 -473
  61. package/dist/index.d.mts +1848 -1327
  62. package/dist/index.d.ts +1848 -1327
  63. package/dist/index.js +754 -352
  64. package/dist/index.mjs +448 -46
  65. package/dist/instrumentation/index.d.mts +119 -21
  66. package/dist/instrumentation/index.d.ts +119 -21
  67. package/dist/instrumentation/index.js +2345 -512
  68. package/dist/instrumentation/index.mjs +2344 -512
  69. package/dist/vitest-evals-reporter.js +16 -16
  70. package/dist/vitest-evals-reporter.mjs +2 -2
  71. package/dist/workerd.d.mts +1 -1
  72. package/dist/workerd.d.ts +1 -1
  73. package/dist/workerd.js +2735 -473
  74. package/dist/workerd.mjs +2735 -473
  75. package/licenses/import-in-the-middle/LICENSE +201 -0
  76. package/licenses/import-in-the-middle/LICENSE-3rdparty.csv +4 -0
  77. package/licenses/import-in-the-middle/NOTICE +34 -0
  78. package/licenses/orchestrion-js/LICENSE +202 -0
  79. package/licenses/orchestrion-js/LICENSE-3rdparty.csv +8 -0
  80. package/licenses/orchestrion-js/NOTICE +3 -0
  81. package/licenses/require-in-the-middle/LICENSE +23 -0
  82. package/package.json +13 -4
  83. package/util/dist/index.d.mts +1 -0
  84. package/util/dist/index.d.ts +1 -0
  85. package/util/dist/index.js +4 -4
  86. package/util/dist/index.mjs +2 -2
  87. package/dist/auto-instrumentations/chunk-J57YF7WS.mjs +0 -208
  88. package/dist/auto-instrumentations/chunk-QFMACSOL.mjs +0 -280
@@ -22,8 +22,582 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
22
22
  mod
23
23
  ));
24
24
 
25
+ // src/auto-instrumentations/orchestrion-js/matcher.ts
26
+ var import_semifies = __toESM(require("semifies"));
27
+
28
+ // src/auto-instrumentations/orchestrion-js/transformer.ts
29
+ var import_esquery2 = __toESM(require("esquery"));
30
+ var import_astring = require("astring");
31
+ var import_meriyah2 = require("meriyah");
32
+ var import_source_map = require("source-map");
33
+
34
+ // src/auto-instrumentations/orchestrion-js/transforms.ts
35
+ var import_esquery = __toESM(require("esquery"));
36
+ var import_meriyah = require("meriyah");
37
+ var tracingChannelPredicate = (node) => node.declarations?.[0]?.id?.properties?.[0]?.value?.name === "tr_ch_apm_tracingChannel";
38
+ var CHANNEL_REGEX = /[^\w]/g;
39
+ function formatChannelVariable(channelName) {
40
+ return `tr_ch_apm$${channelName.replace(CHANNEL_REGEX, "_")}`;
41
+ }
42
+ var transforms = {
43
+ tracingChannelImport({ dcModule, moduleType }, node) {
44
+ if (node.body.some(tracingChannelPredicate)) {
45
+ return;
46
+ }
47
+ const options = { module: moduleType === "esm" };
48
+ const index = node.body.findIndex(
49
+ (child) => child.directive === "use strict"
50
+ );
51
+ const dc = moduleType === "esm" ? `import tr_ch_apm_dc from "${dcModule}"` : `const tr_ch_apm_dc = ${"require"}("${dcModule}")`;
52
+ const tracingChannel = "const { tracingChannel: tr_ch_apm_tracingChannel } = tr_ch_apm_dc";
53
+ const hasSubscribers = `const tr_ch_apm_hasSubscribers = ch => ch.start.hasSubscribers
54
+ || ch.end.hasSubscribers
55
+ || ch.asyncStart.hasSubscribers
56
+ || ch.asyncEnd.hasSubscribers
57
+ || ch.error.hasSubscribers`;
58
+ node.body.splice(
59
+ index + 1,
60
+ 0,
61
+ (0, import_meriyah.parse)(dc, options).body[0],
62
+ (0, import_meriyah.parse)(tracingChannel, options).body[0],
63
+ (0, import_meriyah.parse)(hasSubscribers, options).body[0]
64
+ );
65
+ },
66
+ tracingChannelDeclaration(state, node) {
67
+ const {
68
+ channelName,
69
+ module: { name }
70
+ } = state;
71
+ const channelVariable = formatChannelVariable(channelName);
72
+ if (node.body.some(
73
+ (child) => child.declarations?.[0]?.id?.name === channelVariable
74
+ )) {
75
+ return;
76
+ }
77
+ transforms.tracingChannelImport(state, node, null, []);
78
+ const index = node.body.findIndex(tracingChannelPredicate);
79
+ const code = `
80
+ const ${channelVariable} = tr_ch_apm_tracingChannel("orchestrion:${name}:${channelName}")
81
+ `;
82
+ node.body.splice(index + 1, 0, (0, import_meriyah.parse)(code).body[0]);
83
+ },
84
+ traceCallback: traceAny,
85
+ tracePromise: traceAny,
86
+ traceSync: traceAny
87
+ };
88
+ function traceAny(state, node, _parent, ancestry) {
89
+ const program = ancestry[ancestry.length - 1];
90
+ if (node.type === "ClassDeclaration" || node.type === "ClassExpression") {
91
+ traceInstanceMethod(state, node, program);
92
+ } else {
93
+ traceFunction(state, node, program);
94
+ }
95
+ }
96
+ function traceFunction(state, node, program) {
97
+ transforms.tracingChannelDeclaration(state, program, null, []);
98
+ const { functionQuery } = state;
99
+ const methodName = "methodName" in functionQuery ? functionQuery.methodName : void 0;
100
+ const privateMethodName = "privateMethodName" in functionQuery ? functionQuery.privateMethodName : void 0;
101
+ const functionName = "functionName" in functionQuery ? functionQuery.functionName : void 0;
102
+ const isConstructor = methodName === "constructor" || !methodName && !privateMethodName && !functionName;
103
+ const type = isConstructor ? "ArrowFunctionExpression" : "FunctionExpression";
104
+ node.body = wrap(
105
+ state,
106
+ {
107
+ type,
108
+ params: node.params,
109
+ body: node.body,
110
+ async: node.async,
111
+ expression: false,
112
+ generator: node.generator
113
+ },
114
+ program
115
+ );
116
+ node.generator = false;
117
+ node.async = false;
118
+ wrapSuper(node);
119
+ }
120
+ function traceInstanceMethod(state, node, program) {
121
+ const { functionQuery, operator } = state;
122
+ const { methodName } = functionQuery;
123
+ if (!methodName) {
124
+ return;
125
+ }
126
+ const classBody = node.body;
127
+ if (classBody.body.some(({ key }) => key.name === methodName)) {
128
+ return;
129
+ }
130
+ let ctor = classBody.body.find(({ kind }) => kind === "constructor");
131
+ transforms.tracingChannelDeclaration(state, program, null, []);
132
+ if (!ctor) {
133
+ ctor = (0, import_meriyah.parse)(
134
+ node.superClass ? "class A extends Object { constructor (...args) { super(...args) } }" : "class A { constructor () {} }"
135
+ ).body[0].body.body[0];
136
+ classBody.body.unshift(ctor);
137
+ }
138
+ const ctorBody = (0, import_meriyah.parse)(`
139
+ const __apm$${methodName} = this["${methodName}"]
140
+ this["${methodName}"] = function () {}
141
+ `).body;
142
+ const fn = ctorBody[1].expression.right;
143
+ fn.async = operator === "tracePromise";
144
+ fn.body = wrap(
145
+ state,
146
+ { type: "Identifier", name: `__apm$${methodName}` },
147
+ program
148
+ );
149
+ wrapSuper(fn);
150
+ ctor.value.body.body.push(...ctorBody);
151
+ }
152
+ function wrap(state, node, program) {
153
+ const { operator, moduleVersion } = state;
154
+ const wrapper = operator === "traceCallback" ? wrapCallback(state) : operator === "tracePromise" ? wrapPromise(state) : wrapSync(state);
155
+ const block = wrapper.body[0].body;
156
+ const common = (0, import_meriyah.parse)(
157
+ node.type === "ArrowFunctionExpression" ? `
158
+ const __apm$ctx = {
159
+ arguments,
160
+ moduleVersion: ${JSON.stringify(moduleVersion)}
161
+ };
162
+ const __apm$traced = () => {
163
+ const __apm$wrapped = () => {};
164
+ return __apm$wrapped(...arguments);
165
+ };
166
+ ` : `
167
+ const __apm$ctx = {
168
+ arguments,
169
+ self: this,
170
+ moduleVersion: ${JSON.stringify(moduleVersion)}
171
+ };
172
+ const __apm$traced = () => {
173
+ const __apm$wrapped = () => {};
174
+ return __apm$wrapped.apply(this, arguments);
175
+ };
176
+ `
177
+ ).body;
178
+ block.body.unshift(...common);
179
+ import_esquery.default.query(block, "[id.name=__apm$wrapped]")[0].init = node;
180
+ return block;
181
+ }
182
+ function wrapSuper(node) {
183
+ const members = /* @__PURE__ */ new Set();
184
+ import_esquery.default.traverse(
185
+ node.body,
186
+ import_esquery.default.parse("[object.type=Super]"),
187
+ (node2, parent) => {
188
+ const { name } = node2.property;
189
+ let child;
190
+ if (parent.callee) {
191
+ const { expression } = (0, import_meriyah.parse)(`__apm$super['${name}'].call(this)`).body[0];
192
+ parent.callee = child = expression.callee;
193
+ parent.arguments.unshift(...expression.arguments);
194
+ } else {
195
+ parent.expression = child = (0, import_meriyah.parse)(`__apm$super['${name}']`).body[0];
196
+ }
197
+ child.computed = parent.callee.computed;
198
+ child.optional = parent.callee.optional;
199
+ members.add(name);
200
+ }
201
+ );
202
+ for (const name of members) {
203
+ const member = (0, import_meriyah.parse)(`
204
+ class Wrapper {
205
+ wrapper () {
206
+ __apm$super['${name}'] = super['${name}']
207
+ }
208
+ }
209
+ `).body[0].body.body[0].value.body.body[0];
210
+ node.body.body.unshift(member);
211
+ }
212
+ if (members.size > 0) {
213
+ node.body.body.unshift((0, import_meriyah.parse)("const __apm$super = {}").body[0]);
214
+ }
215
+ }
216
+ function wrapCallback(state) {
217
+ const {
218
+ channelName,
219
+ functionQuery: { callbackIndex = -1 }
220
+ } = state;
221
+ const channelVariable = formatChannelVariable(channelName);
222
+ return (0, import_meriyah.parse)(`
223
+ function wrapper () {
224
+ const __apm$cb = Array.prototype.at.call(arguments, ${callbackIndex});
225
+
226
+ if (!${channelVariable}.start.hasSubscribers) return __apm$traced();
227
+
228
+ function __apm$wrappedCb(err, res) {
229
+ if (err) {
230
+ __apm$ctx.error = err;
231
+ ${channelVariable}.error.publish(__apm$ctx);
232
+ } else {
233
+ __apm$ctx.result = res;
234
+ }
235
+
236
+ ${channelVariable}.asyncStart.runStores(__apm$ctx, () => {
237
+ try {
238
+ if (__apm$cb) {
239
+ return __apm$cb.apply(this, arguments);
240
+ }
241
+ } finally {
242
+ ${channelVariable}.asyncEnd.publish(__apm$ctx);
243
+ }
244
+ });
245
+ }
246
+
247
+ if (typeof __apm$cb !== 'function') {
248
+ return __apm$traced();
249
+ }
250
+ Array.prototype.splice.call(arguments, ${callbackIndex}, 1, __apm$wrappedCb);
251
+
252
+ return ${channelVariable}.start.runStores(__apm$ctx, () => {
253
+ try {
254
+ return __apm$traced();
255
+ } catch (err) {
256
+ __apm$ctx.error = err;
257
+ ${channelVariable}.error.publish(__apm$ctx);
258
+ throw err;
259
+ } finally {
260
+ __apm$ctx.self ??= this;
261
+ ${channelVariable}.end.publish(__apm$ctx);
262
+ }
263
+ });
264
+ }
265
+ `);
266
+ }
267
+ function wrapPromise(state) {
268
+ const { channelName } = state;
269
+ const channelVariable = formatChannelVariable(channelName);
270
+ return (0, import_meriyah.parse)(`
271
+ function wrapper () {
272
+ if (!tr_ch_apm_hasSubscribers(${channelVariable})) return __apm$traced();
273
+
274
+ return ${channelVariable}.start.runStores(__apm$ctx, () => {
275
+ try {
276
+ let promise = __apm$traced();
277
+ if (typeof promise?.then !== 'function') {
278
+ __apm$ctx.result = promise;
279
+ return promise;
280
+ }
281
+ // Mirror Node.js core diagnostics_channel behaviour: for native Promise
282
+ // instances, chain normally (safe since there is no subclass API to
283
+ // preserve). For Promise subclasses and other thenables, side-chain the
284
+ // callbacks for event publishing and return the original so that any
285
+ // subclass-specific methods (e.g. APIPromise.withResponse()) remain
286
+ // accessible to the caller.
287
+ if (promise instanceof Promise && promise.constructor === Promise) {
288
+ return promise.then(
289
+ result => {
290
+ __apm$ctx.result = result;
291
+ ${channelVariable}.asyncStart.publish(__apm$ctx);
292
+ ${channelVariable}.asyncEnd.publish(__apm$ctx);
293
+ return result;
294
+ },
295
+ err => {
296
+ __apm$ctx.error = err;
297
+ ${channelVariable}.error.publish(__apm$ctx);
298
+ ${channelVariable}.asyncStart.publish(__apm$ctx);
299
+ ${channelVariable}.asyncEnd.publish(__apm$ctx);
300
+ throw err;
301
+ }
302
+ );
303
+ }
304
+ promise.then(
305
+ result => {
306
+ __apm$ctx.result = result;
307
+ ${channelVariable}.asyncStart.publish(__apm$ctx);
308
+ ${channelVariable}.asyncEnd.publish(__apm$ctx);
309
+ },
310
+ err => {
311
+ __apm$ctx.error = err;
312
+ ${channelVariable}.error.publish(__apm$ctx);
313
+ ${channelVariable}.asyncStart.publish(__apm$ctx);
314
+ ${channelVariable}.asyncEnd.publish(__apm$ctx);
315
+ }
316
+ );
317
+ return promise;
318
+ } catch (err) {
319
+ __apm$ctx.error = err;
320
+ ${channelVariable}.error.publish(__apm$ctx);
321
+ throw err;
322
+ } finally {
323
+ __apm$ctx.self ??= this;
324
+ ${channelVariable}.end.publish(__apm$ctx);
325
+ }
326
+ });
327
+ }
328
+ `);
329
+ }
330
+ function wrapSync(state) {
331
+ const { channelName } = state;
332
+ const channelVariable = formatChannelVariable(channelName);
333
+ return (0, import_meriyah.parse)(`
334
+ function wrapper () {
335
+ if (!tr_ch_apm_hasSubscribers(${channelVariable})) return __apm$traced();
336
+
337
+ return ${channelVariable}.start.runStores(__apm$ctx, () => {
338
+ try {
339
+ const result = __apm$traced();
340
+ __apm$ctx.result = result;
341
+ return result;
342
+ } catch (err) {
343
+ __apm$ctx.error = err;
344
+ ${channelVariable}.error.publish(__apm$ctx);
345
+ throw err;
346
+ } finally {
347
+ __apm$ctx.self ??= this;
348
+ ${channelVariable}.end.publish(__apm$ctx);
349
+ }
350
+ });
351
+ }
352
+ `);
353
+ }
354
+
355
+ // src/auto-instrumentations/orchestrion-js/transformer.ts
356
+ var Transformer = class {
357
+ moduleName;
358
+ version;
359
+ filePath;
360
+ configs = [];
361
+ dcModule;
362
+ constructor(moduleName, version, filePath, configs, dcModule) {
363
+ this.moduleName = moduleName;
364
+ this.version = version;
365
+ this.filePath = filePath;
366
+ this.configs = configs;
367
+ this.dcModule = dcModule;
368
+ }
369
+ /**
370
+ * Instruments `code` by injecting diagnostics_channel tracing around the
371
+ * target functions defined by this transformer's configs.
372
+ */
373
+ transform(code, moduleType) {
374
+ if (Buffer.isBuffer(code)) {
375
+ code = code.toString();
376
+ }
377
+ if (!code) {
378
+ return { code };
379
+ }
380
+ let ast;
381
+ let aliases = {};
382
+ let injectionCount = 0;
383
+ for (const config of this.configs) {
384
+ const { astQuery, functionQuery } = config;
385
+ if (!ast) {
386
+ const options = {
387
+ loc: true,
388
+ ranges: true,
389
+ raw: true,
390
+ module: moduleType === "esm"
391
+ };
392
+ try {
393
+ ast = (0, import_meriyah2.parse)(code, options);
394
+ } catch {
395
+ ast = (0, import_meriyah2.parse)(code, { ...options, module: !options.module });
396
+ }
397
+ if (moduleType === "esm") {
398
+ aliases = this.collectExportAliases(ast);
399
+ }
400
+ }
401
+ const resolvedFunctionQuery = this.resolveExportAlias(
402
+ functionQuery,
403
+ aliases
404
+ );
405
+ const query = astQuery || this.fromFunctionQuery(resolvedFunctionQuery);
406
+ const state = {
407
+ ...config,
408
+ dcModule: this.dcModule,
409
+ moduleType,
410
+ moduleVersion: this.version,
411
+ functionQuery: resolvedFunctionQuery,
412
+ operator: this.getOperator(resolvedFunctionQuery.kind)
413
+ };
414
+ import_esquery2.default.traverse(ast, import_esquery2.default.parse(query), (...args) => {
415
+ injectionCount++;
416
+ this.visit(state, ...args);
417
+ });
418
+ }
419
+ if (injectionCount === 0 && this.configs.length > 0) {
420
+ const names = this.configs.map(({ astQuery, functionQuery }) => {
421
+ const resolvedQuery = this.resolveExportAlias(functionQuery, aliases);
422
+ const queryName = (q) => q.methodName || q.privateMethodName || q.functionName || q.propertyName || astQuery || "unknown";
423
+ const originalName = queryName(functionQuery);
424
+ const originalAlias = functionQuery.className || functionQuery.functionName;
425
+ const resolvedAlias = resolvedQuery.className || resolvedQuery.functionName;
426
+ if (originalAlias && originalAlias !== resolvedAlias) {
427
+ return `${originalAlias} (local name: ${resolvedAlias})`;
428
+ }
429
+ return originalName;
430
+ });
431
+ throw new Error(
432
+ `Failed to find injection points for: ${JSON.stringify(names)}`
433
+ );
434
+ }
435
+ if (ast) {
436
+ const file = `${this.moduleName}/${this.filePath}`;
437
+ const sourceMap = new import_source_map.SourceMapGenerator({ file });
438
+ const transformedCode = (0, import_astring.generate)(ast, { sourceMap });
439
+ const map = sourceMap.toString();
440
+ return { code: transformedCode, map };
441
+ }
442
+ return { code };
443
+ }
444
+ free() {
445
+ }
446
+ visit(state, ...args) {
447
+ const transform = transforms[state.operator];
448
+ const { index = 0 } = state.functionQuery;
449
+ const [node] = args;
450
+ const type = node.init?.type || node.type;
451
+ if (type !== "ClassDeclaration" && type !== "ClassExpression") {
452
+ if (node.type === "VariableDeclarator") {
453
+ return;
454
+ }
455
+ state.functionIndex = state.functionIndex === void 0 ? 0 : state.functionIndex + 1;
456
+ if (index !== null && index !== state.functionIndex) {
457
+ return;
458
+ }
459
+ }
460
+ transform(state, ...args);
461
+ }
462
+ getOperator(kind) {
463
+ switch (kind) {
464
+ case "Async":
465
+ return "tracePromise";
466
+ case "Callback":
467
+ return "traceCallback";
468
+ case "Sync":
469
+ return "traceSync";
470
+ }
471
+ }
472
+ collectExportAliases(ast) {
473
+ const aliases = {};
474
+ for (const node of ast.body) {
475
+ if (node.type === "ExportNamedDeclaration" && !node.source) {
476
+ for (const spec of node.specifiers) {
477
+ if (spec.exported && spec.local) {
478
+ const exportedName = spec.exported.name ?? spec.exported.value;
479
+ const localName = spec.local.name ?? spec.local.value;
480
+ if (exportedName && localName) {
481
+ aliases[exportedName] = localName;
482
+ }
483
+ }
484
+ }
485
+ }
486
+ }
487
+ return aliases;
488
+ }
489
+ resolveExportAlias(functionQuery, aliases) {
490
+ if (!("isExportAlias" in functionQuery) || !functionQuery.isExportAlias) {
491
+ return functionQuery;
492
+ }
493
+ if ("className" in functionQuery && aliases[functionQuery.className]) {
494
+ return {
495
+ ...functionQuery,
496
+ className: aliases[functionQuery.className]
497
+ };
498
+ }
499
+ if ("functionName" in functionQuery && aliases[functionQuery.functionName]) {
500
+ return {
501
+ ...functionQuery,
502
+ functionName: aliases[functionQuery.functionName]
503
+ };
504
+ }
505
+ return functionQuery;
506
+ }
507
+ functionQueryLabel(functionQuery) {
508
+ if ("methodName" in functionQuery) {
509
+ return functionQuery.methodName;
510
+ }
511
+ if ("privateMethodName" in functionQuery) {
512
+ return functionQuery.privateMethodName;
513
+ }
514
+ if ("functionName" in functionQuery) {
515
+ return functionQuery.functionName;
516
+ }
517
+ if ("propertyName" in functionQuery) {
518
+ return functionQuery.propertyName;
519
+ }
520
+ return "unknown";
521
+ }
522
+ fromFunctionQuery(functionQuery) {
523
+ const queries = [];
524
+ if ("className" in functionQuery) {
525
+ const { className } = functionQuery;
526
+ const methodName = this.functionQueryLabel(functionQuery);
527
+ const keyType = "privateMethodName" in functionQuery ? "PrivateIdentifier" : "Identifier";
528
+ queries.push(
529
+ `[id.name="${className}"]`,
530
+ `[id.name="${className}"] > ClassExpression`,
531
+ `[id.name="${className}"] > ClassBody > [key.name="${methodName}"][key.type=${keyType}] > [async]`,
532
+ `[id.name="${className}"] > ClassExpression > ClassBody > [key.name="${methodName}"][key.type=${keyType}] > [async]`
533
+ );
534
+ } else if ("methodName" in functionQuery) {
535
+ const { methodName } = functionQuery;
536
+ queries.push(
537
+ `ClassBody > [key.name="${methodName}"][key.type=Identifier] > [async]`,
538
+ `Property[key.name="${methodName}"][key.type=Identifier] > [async]`
539
+ );
540
+ }
541
+ if ("functionName" in functionQuery) {
542
+ const { functionName } = functionQuery;
543
+ queries.push(`FunctionDeclaration[id.name="${functionName}"][async]`);
544
+ }
545
+ if ("objectName" in functionQuery) {
546
+ const { objectName, propertyName } = functionQuery;
547
+ const objectSelector = objectName === "this" ? "left.object.type=ThisExpression" : `left.object.name="${objectName}"`;
548
+ queries.push(
549
+ `AssignmentExpression[${objectSelector}][left.property.name="${propertyName}"] > [async]`
550
+ );
551
+ }
552
+ return queries.join(", ");
553
+ }
554
+ };
555
+
556
+ // src/auto-instrumentations/orchestrion-js/matcher.ts
557
+ var InstrumentationMatcher = class {
558
+ configs = [];
559
+ dcModule;
560
+ transformers = {};
561
+ constructor(configs, dcModule) {
562
+ this.configs = configs;
563
+ this.dcModule = dcModule || "diagnostics_channel";
564
+ }
565
+ /**
566
+ * Returns a Transformer for the given module/file/version, or undefined if no
567
+ * registered config matches.
568
+ */
569
+ getTransformer(moduleName, version, filePath) {
570
+ filePath = filePath.replace(/\\/g, "/");
571
+ const id = `${moduleName}/${filePath}@${version}`;
572
+ if (this.transformers[id]) {
573
+ return this.transformers[id];
574
+ }
575
+ const configs = this.configs.filter(
576
+ ({ module: mod }) => mod.name === moduleName && mod.filePath === filePath && (0, import_semifies.default)(version, mod.versionRange)
577
+ );
578
+ if (configs.length === 0) {
579
+ return void 0;
580
+ }
581
+ this.transformers[id] = new Transformer(
582
+ moduleName,
583
+ version,
584
+ filePath,
585
+ configs,
586
+ this.dcModule
587
+ );
588
+ return this.transformers[id];
589
+ }
590
+ free() {
591
+ this.transformers = {};
592
+ }
593
+ };
594
+
595
+ // src/auto-instrumentations/orchestrion-js/index.ts
596
+ function create(configs, dcModule) {
597
+ return new InstrumentationMatcher(configs, dcModule);
598
+ }
599
+
25
600
  // src/auto-instrumentations/bundler/webpack-loader.ts
26
- var import_code_transformer = require("@apm-js-collab/code-transformer");
27
601
  var import_path = require("path");
28
602
  var import_fs = require("fs");
29
603
  var import_module_details_from_path = __toESM(require("module-details-from-path"));
@@ -312,6 +886,10 @@ var aiSDKChannels = defineChannels("ai", {
312
886
  channelName: "ToolLoopAgent.stream",
313
887
  kind: "async"
314
888
  }),
889
+ workflowAgentStream: channel({
890
+ channelName: "WorkflowAgent.stream",
891
+ kind: "async"
892
+ }),
315
893
  v7CreateTelemetryDispatcher: channel({
316
894
  channelName: "createTelemetryDispatcher",
317
895
  kind: "sync-stream"
@@ -510,6 +1088,18 @@ var aiSDKConfigs = [
510
1088
  kind: "Sync"
511
1089
  }
512
1090
  },
1091
+ {
1092
+ channelName: aiSDKChannels.v7CreateTelemetryDispatcher.channelName,
1093
+ module: {
1094
+ name: "ai",
1095
+ versionRange: ">=7.0.0-0 <8.0.0",
1096
+ filePath: "dist/internal/index.js"
1097
+ },
1098
+ functionQuery: {
1099
+ functionName: "createTelemetryDispatcher",
1100
+ kind: "Sync"
1101
+ }
1102
+ },
513
1103
  // streamObject - async function (v3 only, before the sync refactor in v4)
514
1104
  {
515
1105
  channelName: aiSDKChannels.streamObject.channelName,
@@ -3269,20 +3859,16 @@ function getMatcher(options) {
3269
3859
  if (matcherCache.has(configHash)) {
3270
3860
  return matcherCache.get(configHash);
3271
3861
  }
3272
- for (const [hash, matcher2] of matcherCache.entries()) {
3862
+ for (const hash of matcherCache.keys()) {
3273
3863
  if (hash !== configHash) {
3274
- matcher2.free();
3275
3864
  matcherCache.delete(hash);
3276
3865
  }
3277
3866
  }
3278
- const matcher = (0, import_code_transformer.create)(allInstrumentations, dcModule ?? null);
3867
+ const matcher = create(allInstrumentations, dcModule ?? null);
3279
3868
  matcherCache.set(configHash, matcher);
3280
3869
  return matcher;
3281
3870
  }
3282
3871
  process.on("exit", () => {
3283
- for (const matcher of matcherCache.values()) {
3284
- matcher.free();
3285
- }
3286
3872
  matcherCache.clear();
3287
3873
  });
3288
3874
  function codeTransformerLoader(code, inputSourceMap) {
@@ -3327,8 +3913,6 @@ function codeTransformerLoader(code, inputSourceMap) {
3327
3913
  error
3328
3914
  );
3329
3915
  callback(null, code, inputSourceMap);
3330
- } finally {
3331
- transformer.free();
3332
3916
  }
3333
3917
  }
3334
3918
  module.exports = codeTransformerLoader;