braintrust 3.10.0 → 3.12.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 (78) hide show
  1. package/README.md +8 -8
  2. package/dev/dist/index.d.mts +51 -3
  3. package/dev/dist/index.d.ts +51 -3
  4. package/dev/dist/index.js +13138 -8915
  5. package/dev/dist/index.mjs +13015 -8792
  6. package/dist/apply-auto-instrumentation.browser.d.mts +2 -0
  7. package/dist/apply-auto-instrumentation.browser.d.ts +2 -0
  8. package/dist/apply-auto-instrumentation.browser.js +18 -0
  9. package/dist/apply-auto-instrumentation.browser.mjs +0 -0
  10. package/dist/apply-auto-instrumentation.d.mts +2 -0
  11. package/dist/apply-auto-instrumentation.d.ts +2 -0
  12. package/dist/apply-auto-instrumentation.js +2534 -0
  13. package/dist/apply-auto-instrumentation.mjs +2534 -0
  14. package/dist/auto-instrumentations/bundler/esbuild.cjs +1775 -737
  15. package/dist/auto-instrumentations/bundler/esbuild.d.mts +9 -5
  16. package/dist/auto-instrumentations/bundler/esbuild.d.ts +9 -5
  17. package/dist/auto-instrumentations/bundler/esbuild.mjs +10 -2
  18. package/dist/auto-instrumentations/bundler/next.cjs +3268 -0
  19. package/dist/auto-instrumentations/bundler/next.d.mts +3 -0
  20. package/dist/auto-instrumentations/bundler/next.d.ts +3 -0
  21. package/dist/auto-instrumentations/bundler/next.mjs +189 -0
  22. package/dist/auto-instrumentations/bundler/rollup.cjs +1775 -737
  23. package/dist/auto-instrumentations/bundler/rollup.d.mts +9 -5
  24. package/dist/auto-instrumentations/bundler/rollup.d.ts +9 -5
  25. package/dist/auto-instrumentations/bundler/rollup.mjs +10 -2
  26. package/dist/auto-instrumentations/bundler/vite.cjs +1775 -737
  27. package/dist/auto-instrumentations/bundler/vite.d.mts +9 -5
  28. package/dist/auto-instrumentations/bundler/vite.d.ts +9 -5
  29. package/dist/auto-instrumentations/bundler/vite.mjs +10 -2
  30. package/dist/auto-instrumentations/bundler/webpack-loader.cjs +1713 -690
  31. package/dist/auto-instrumentations/bundler/webpack-loader.d.ts +3 -3
  32. package/dist/auto-instrumentations/bundler/webpack.cjs +1775 -737
  33. package/dist/auto-instrumentations/bundler/webpack.d.mts +9 -5
  34. package/dist/auto-instrumentations/bundler/webpack.d.ts +9 -5
  35. package/dist/auto-instrumentations/bundler/webpack.mjs +6 -6
  36. package/dist/auto-instrumentations/{chunk-GZNXBBPU.mjs → chunk-2DPA74KK.mjs} +856 -5
  37. package/dist/auto-instrumentations/chunk-73BZUKVI.mjs +300 -0
  38. package/dist/auto-instrumentations/chunk-AFXRW7I7.mjs +18 -0
  39. package/dist/auto-instrumentations/{chunk-ITP7RAUY.mjs → chunk-MWZXZQUO.mjs} +23 -1
  40. package/dist/auto-instrumentations/hook.mjs +1912 -1125
  41. package/dist/auto-instrumentations/index.cjs +615 -4
  42. package/dist/auto-instrumentations/index.d.mts +18 -1
  43. package/dist/auto-instrumentations/index.d.ts +18 -1
  44. package/dist/auto-instrumentations/index.mjs +12 -247
  45. package/dist/auto-instrumentations/loader/cjs-patch.cjs +26 -1
  46. package/dist/auto-instrumentations/loader/cjs-patch.mjs +28 -1
  47. package/dist/auto-instrumentations/loader/esm-hook.mjs +35 -3
  48. package/dist/auto-instrumentations/plugin-D7nDswtC.d.mts +44 -0
  49. package/dist/auto-instrumentations/plugin-D7nDswtC.d.ts +44 -0
  50. package/dist/browser.d.mts +577 -287
  51. package/dist/browser.d.ts +577 -287
  52. package/dist/browser.js +10221 -5524
  53. package/dist/browser.mjs +10221 -5524
  54. package/dist/chunk-BW4DF4CY.js +816 -0
  55. package/dist/chunk-MSLBGITU.mjs +816 -0
  56. package/dist/cli.js +13143 -8892
  57. package/dist/edge-light.d.mts +1 -1
  58. package/dist/edge-light.d.ts +1 -1
  59. package/dist/edge-light.js +10221 -5524
  60. package/dist/edge-light.mjs +10221 -5524
  61. package/dist/index.d.mts +590 -300
  62. package/dist/index.d.ts +590 -300
  63. package/dist/index.js +9309 -5371
  64. package/dist/index.mjs +10340 -6402
  65. package/dist/instrumentation/index.d.mts +7863 -39
  66. package/dist/instrumentation/index.d.ts +7863 -39
  67. package/dist/instrumentation/index.js +10263 -6034
  68. package/dist/instrumentation/index.mjs +10262 -6034
  69. package/dist/workerd.d.mts +1 -1
  70. package/dist/workerd.d.ts +1 -1
  71. package/dist/workerd.js +10221 -5524
  72. package/dist/workerd.mjs +10221 -5524
  73. package/package.json +33 -19
  74. package/util/dist/index.js +6 -0
  75. package/util/dist/index.mjs +6 -0
  76. package/dist/auto-instrumentations/chunk-XWEQQOQH.mjs +0 -106
  77. package/dist/auto-instrumentations/plugin-Df3qKIl2.d.mts +0 -22
  78. package/dist/auto-instrumentations/plugin-Df3qKIl2.d.ts +0 -22
@@ -14,6 +14,8 @@ export { InstrumentationConfig } from '@apm-js-collab/code-transformer';
14
14
  */
15
15
  declare const openaiConfigs: InstrumentationConfig[];
16
16
 
17
+ declare const openAICodexConfigs: InstrumentationConfig[];
18
+
17
19
  /**
18
20
  * Instrumentation configurations for the Anthropic SDK.
19
21
  *
@@ -55,6 +57,8 @@ declare const claudeAgentSDKConfigs: InstrumentationConfig[];
55
57
 
56
58
  declare const cursorSDKConfigs: InstrumentationConfig[];
57
59
 
60
+ declare const openAIAgentsCoreConfigs: InstrumentationConfig[];
61
+
58
62
  /**
59
63
  * Instrumentation configurations for the Google GenAI SDK.
60
64
  *
@@ -93,4 +97,17 @@ declare const cohereConfigs: InstrumentationConfig[];
93
97
 
94
98
  declare const groqConfigs: InstrumentationConfig[];
95
99
 
96
- export { aiSDKConfigs, anthropicConfigs, claudeAgentSDKConfigs, cohereConfigs, cursorSDKConfigs, googleADKConfigs, googleGenAIConfigs, groqConfigs, huggingFaceConfigs, mistralConfigs, openRouterAgentConfigs, openRouterConfigs, openaiConfigs };
100
+ /**
101
+ * Instrumentation configurations for Genkit's JavaScript SDK.
102
+ *
103
+ * Genkit's public instance methods live on the GenkitAI base class in
104
+ * @genkit-ai/ai. The top-level `genkit` package subclasses that class, so
105
+ * targeting these methods instruments regular `genkit({ ... })` instances.
106
+ */
107
+ declare const genkitConfigs: InstrumentationConfig[];
108
+
109
+ declare const gitHubCopilotConfigs: InstrumentationConfig[];
110
+
111
+ declare const langchainConfigs: InstrumentationConfig[];
112
+
113
+ export { aiSDKConfigs, anthropicConfigs, claudeAgentSDKConfigs, cohereConfigs, cursorSDKConfigs, genkitConfigs, gitHubCopilotConfigs, googleADKConfigs, googleGenAIConfigs, groqConfigs, huggingFaceConfigs, langchainConfigs, mistralConfigs, openAIAgentsCoreConfigs, openAICodexConfigs, openRouterAgentConfigs, openRouterConfigs, openaiConfigs };
@@ -14,6 +14,8 @@ export { InstrumentationConfig } from '@apm-js-collab/code-transformer';
14
14
  */
15
15
  declare const openaiConfigs: InstrumentationConfig[];
16
16
 
17
+ declare const openAICodexConfigs: InstrumentationConfig[];
18
+
17
19
  /**
18
20
  * Instrumentation configurations for the Anthropic SDK.
19
21
  *
@@ -55,6 +57,8 @@ declare const claudeAgentSDKConfigs: InstrumentationConfig[];
55
57
 
56
58
  declare const cursorSDKConfigs: InstrumentationConfig[];
57
59
 
60
+ declare const openAIAgentsCoreConfigs: InstrumentationConfig[];
61
+
58
62
  /**
59
63
  * Instrumentation configurations for the Google GenAI SDK.
60
64
  *
@@ -93,4 +97,17 @@ declare const cohereConfigs: InstrumentationConfig[];
93
97
 
94
98
  declare const groqConfigs: InstrumentationConfig[];
95
99
 
96
- export { aiSDKConfigs, anthropicConfigs, claudeAgentSDKConfigs, cohereConfigs, cursorSDKConfigs, googleADKConfigs, googleGenAIConfigs, groqConfigs, huggingFaceConfigs, mistralConfigs, openRouterAgentConfigs, openRouterConfigs, openaiConfigs };
100
+ /**
101
+ * Instrumentation configurations for Genkit's JavaScript SDK.
102
+ *
103
+ * Genkit's public instance methods live on the GenkitAI base class in
104
+ * @genkit-ai/ai. The top-level `genkit` package subclasses that class, so
105
+ * targeting these methods instruments regular `genkit({ ... })` instances.
106
+ */
107
+ declare const genkitConfigs: InstrumentationConfig[];
108
+
109
+ declare const gitHubCopilotConfigs: InstrumentationConfig[];
110
+
111
+ declare const langchainConfigs: InstrumentationConfig[];
112
+
113
+ export { aiSDKConfigs, anthropicConfigs, claudeAgentSDKConfigs, cohereConfigs, cursorSDKConfigs, genkitConfigs, gitHubCopilotConfigs, googleADKConfigs, googleGenAIConfigs, groqConfigs, huggingFaceConfigs, langchainConfigs, mistralConfigs, openAIAgentsCoreConfigs, openAICodexConfigs, openRouterAgentConfigs, openRouterConfigs, openaiConfigs };
@@ -1,274 +1,39 @@
1
1
  import {
2
2
  aiSDKConfigs,
3
3
  anthropicConfigs,
4
- channel,
5
4
  claudeAgentSDKConfigs,
6
5
  cohereConfigs,
7
6
  cursorSDKConfigs,
8
- defineChannels,
7
+ genkitConfigs,
8
+ gitHubCopilotConfigs,
9
+ googleADKConfigs,
9
10
  googleGenAIConfigs,
10
11
  groqConfigs,
11
12
  huggingFaceConfigs,
13
+ langchainConfigs,
12
14
  mistralConfigs,
15
+ openAIAgentsCoreConfigs,
16
+ openAICodexConfigs,
13
17
  openRouterAgentConfigs,
14
18
  openRouterConfigs,
15
19
  openaiConfigs
16
- } from "./chunk-GZNXBBPU.mjs";
17
-
18
- // src/instrumentation/plugins/google-adk-channels.ts
19
- var googleADKChannels = defineChannels("@google/adk", {
20
- runnerRunAsync: channel({
21
- channelName: "runner.runAsync",
22
- kind: "sync-stream"
23
- }),
24
- agentRunAsync: channel({
25
- channelName: "agent.runAsync",
26
- kind: "sync-stream"
27
- }),
28
- toolRunAsync: channel({
29
- channelName: "tool.runAsync",
30
- kind: "async"
31
- })
32
- });
33
-
34
- // src/auto-instrumentations/configs/google-adk.ts
35
- var googleADKVersionRange = ">=0.1.0";
36
- var googleADKBundledIndexV06VersionRange = ">=0.6.1 <0.7.0";
37
- var googleADKBundledIndexV1VersionRange = ">=1.0.0 <2.0.0";
38
- var googleADKConfigs = [
39
- // --- Runner.runAsync --- async generator, kind "Sync" + sync-stream channel
40
- // Runner.runAsync — ESM individual module file
41
- {
42
- channelName: googleADKChannels.runnerRunAsync.channelName,
43
- module: {
44
- name: "@google/adk",
45
- versionRange: googleADKVersionRange,
46
- filePath: "dist/esm/runner/runner.js"
47
- },
48
- functionQuery: {
49
- className: "Runner",
50
- methodName: "runAsync",
51
- kind: "Sync"
52
- }
53
- },
54
- // Runner.runAsync — bundled CJS/ESM indexes
55
- // The bundled entrypoints minify class names, so target the 12th sync
56
- // `runAsync` method in file order rather than a class name. This mapping is
57
- // only validated against the current 0.6.x bundle layout, so keep the range
58
- // tight until we verify newer bundled outputs.
59
- {
60
- channelName: googleADKChannels.runnerRunAsync.channelName,
61
- module: {
62
- name: "@google/adk",
63
- versionRange: googleADKBundledIndexV06VersionRange,
64
- filePath: "dist/cjs/index.js"
65
- },
66
- functionQuery: {
67
- methodName: "runAsync",
68
- kind: "Sync",
69
- index: 11
70
- }
71
- },
72
- {
73
- channelName: googleADKChannels.runnerRunAsync.channelName,
74
- module: {
75
- name: "@google/adk",
76
- versionRange: googleADKBundledIndexV06VersionRange,
77
- filePath: "dist/esm/index.js"
78
- },
79
- functionQuery: {
80
- methodName: "runAsync",
81
- kind: "Sync",
82
- index: 11
83
- }
84
- },
85
- // The 1.x bundled entrypoints still inline the runtime into index.js, but
86
- // the minified method order changed. These indices are verified against 1.0.0.
87
- {
88
- channelName: googleADKChannels.runnerRunAsync.channelName,
89
- module: {
90
- name: "@google/adk",
91
- versionRange: googleADKBundledIndexV1VersionRange,
92
- filePath: "dist/cjs/index.js"
93
- },
94
- functionQuery: {
95
- methodName: "runAsync",
96
- kind: "Sync",
97
- index: 12
98
- }
99
- },
100
- {
101
- channelName: googleADKChannels.runnerRunAsync.channelName,
102
- module: {
103
- name: "@google/adk",
104
- versionRange: googleADKBundledIndexV1VersionRange,
105
- filePath: "dist/esm/index.js"
106
- },
107
- functionQuery: {
108
- methodName: "runAsync",
109
- kind: "Sync",
110
- index: 12
111
- }
112
- },
113
- // --- BaseAgent.runAsync --- async generator, kind "Sync" + sync-stream channel
114
- // BaseAgent.runAsync — ESM individual module file
115
- {
116
- channelName: googleADKChannels.agentRunAsync.channelName,
117
- module: {
118
- name: "@google/adk",
119
- versionRange: googleADKVersionRange,
120
- filePath: "dist/esm/agents/base_agent.js"
121
- },
122
- functionQuery: {
123
- className: "BaseAgent",
124
- methodName: "runAsync",
125
- kind: "Sync"
126
- }
127
- },
128
- // BaseAgent.runAsync — bundled CJS/ESM indexes
129
- // The bundled entrypoints minify class names, so target the first sync
130
- // `runAsync` method in file order rather than a class name. This mapping is
131
- // only validated against the current 0.6.x bundle layout, so keep the range
132
- // tight until we verify newer bundled outputs.
133
- {
134
- channelName: googleADKChannels.agentRunAsync.channelName,
135
- module: {
136
- name: "@google/adk",
137
- versionRange: googleADKBundledIndexV06VersionRange,
138
- filePath: "dist/cjs/index.js"
139
- },
140
- functionQuery: {
141
- methodName: "runAsync",
142
- kind: "Sync",
143
- index: 0
144
- }
145
- },
146
- {
147
- channelName: googleADKChannels.agentRunAsync.channelName,
148
- module: {
149
- name: "@google/adk",
150
- versionRange: googleADKBundledIndexV06VersionRange,
151
- filePath: "dist/esm/index.js"
152
- },
153
- functionQuery: {
154
- methodName: "runAsync",
155
- kind: "Sync",
156
- index: 0
157
- }
158
- },
159
- // The 1.x bundled entrypoints keep BaseAgent.runAsync as the first bundled
160
- // async-generator runAsync method in file order.
161
- {
162
- channelName: googleADKChannels.agentRunAsync.channelName,
163
- module: {
164
- name: "@google/adk",
165
- versionRange: googleADKBundledIndexV1VersionRange,
166
- filePath: "dist/cjs/index.js"
167
- },
168
- functionQuery: {
169
- methodName: "runAsync",
170
- kind: "Sync",
171
- index: 0
172
- }
173
- },
174
- {
175
- channelName: googleADKChannels.agentRunAsync.channelName,
176
- module: {
177
- name: "@google/adk",
178
- versionRange: googleADKBundledIndexV1VersionRange,
179
- filePath: "dist/esm/index.js"
180
- },
181
- functionQuery: {
182
- methodName: "runAsync",
183
- kind: "Sync",
184
- index: 0
185
- }
186
- },
187
- // --- FunctionTool.runAsync --- regular async, kind "Async"
188
- // FunctionTool.runAsync — ESM individual module file
189
- {
190
- channelName: googleADKChannels.toolRunAsync.channelName,
191
- module: {
192
- name: "@google/adk",
193
- versionRange: googleADKVersionRange,
194
- filePath: "dist/esm/tools/function_tool.js"
195
- },
196
- functionQuery: {
197
- className: "FunctionTool",
198
- methodName: "runAsync",
199
- kind: "Async"
200
- }
201
- },
202
- // FunctionTool.runAsync — bundled CJS/ESM indexes
203
- // The bundled entrypoints minify class names, so target the first async
204
- // `runAsync` method in file order rather than a class name. This mapping is
205
- // only validated against the current 0.6.x bundle layout, so keep the range
206
- // tight until we verify newer bundled outputs.
207
- {
208
- channelName: googleADKChannels.toolRunAsync.channelName,
209
- module: {
210
- name: "@google/adk",
211
- versionRange: googleADKBundledIndexV06VersionRange,
212
- filePath: "dist/cjs/index.js"
213
- },
214
- functionQuery: {
215
- methodName: "runAsync",
216
- kind: "Async",
217
- index: 1
218
- }
219
- },
220
- {
221
- channelName: googleADKChannels.toolRunAsync.channelName,
222
- module: {
223
- name: "@google/adk",
224
- versionRange: googleADKBundledIndexV06VersionRange,
225
- filePath: "dist/esm/index.js"
226
- },
227
- functionQuery: {
228
- methodName: "runAsync",
229
- kind: "Async",
230
- index: 1
231
- }
232
- },
233
- // The 1.x bundle moves FunctionTool.runAsync behind one helper method.
234
- {
235
- channelName: googleADKChannels.toolRunAsync.channelName,
236
- module: {
237
- name: "@google/adk",
238
- versionRange: googleADKBundledIndexV1VersionRange,
239
- filePath: "dist/cjs/index.js"
240
- },
241
- functionQuery: {
242
- methodName: "runAsync",
243
- kind: "Async",
244
- index: 1
245
- }
246
- },
247
- {
248
- channelName: googleADKChannels.toolRunAsync.channelName,
249
- module: {
250
- name: "@google/adk",
251
- versionRange: googleADKBundledIndexV1VersionRange,
252
- filePath: "dist/esm/index.js"
253
- },
254
- functionQuery: {
255
- methodName: "runAsync",
256
- kind: "Async",
257
- index: 1
258
- }
259
- }
260
- ];
20
+ } from "./chunk-2DPA74KK.mjs";
261
21
  export {
262
22
  aiSDKConfigs,
263
23
  anthropicConfigs,
264
24
  claudeAgentSDKConfigs,
265
25
  cohereConfigs,
266
26
  cursorSDKConfigs,
27
+ genkitConfigs,
28
+ gitHubCopilotConfigs,
267
29
  googleADKConfigs,
268
30
  googleGenAIConfigs,
269
31
  groqConfigs,
270
32
  huggingFaceConfigs,
33
+ langchainConfigs,
271
34
  mistralConfigs,
35
+ openAIAgentsCoreConfigs,
36
+ openAICodexConfigs,
272
37
  openRouterAgentConfigs,
273
38
  openRouterConfigs,
274
39
  openaiConfigs
@@ -93,6 +93,27 @@ function getPackageName(baseDir) {
93
93
  return void 0;
94
94
  }
95
95
 
96
+ // src/auto-instrumentations/loader/openai-api-promise-patch.ts
97
+ var OPENAI_API_PROMISE_PATCH = `
98
+ ;(function __btPatchAPIPromise() {
99
+ if (typeof APIPromise === "undefined" || APIPromise.prototype.__btParsePatched) return;
100
+ APIPromise.prototype.__btParsePatched = true;
101
+ var _origThen = APIPromise.prototype.then;
102
+ APIPromise.prototype.then = function __btThen(onfulfilled, onrejected) {
103
+ if (!this.__btParseWrapped && Object.prototype.hasOwnProperty.call(this, "parseResponse")) {
104
+ this.__btParseWrapped = true;
105
+ var _origParse = this.parseResponse;
106
+ var _cached;
107
+ this.parseResponse = function() {
108
+ if (!_cached) _cached = _origParse.apply(this, arguments);
109
+ return _cached;
110
+ };
111
+ }
112
+ return _origThen.call(this, onfulfilled, onrejected);
113
+ };
114
+ })();
115
+ `;
116
+
96
117
  // src/auto-instrumentations/loader/cjs-patch.ts
97
118
  var ModulePatch = class {
98
119
  packages;
@@ -124,8 +145,12 @@ var ModulePatch = class {
124
145
  if (!self.packages.has(packageName)) {
125
146
  return self.originalCompile.apply(this, args);
126
147
  }
127
- const version = getPackageVersion(resolvedModule.basedir);
128
148
  const normalizedModulePath = resolvedModule.path.replace(/\\/g, "/");
149
+ if (packageName === "openai" && normalizedModulePath.includes("api-promise")) {
150
+ args[0] = content + OPENAI_API_PROMISE_PATCH;
151
+ return self.originalCompile.apply(this, args);
152
+ }
153
+ const version = getPackageVersion(resolvedModule.basedir);
129
154
  const transformer = self.instrumentator.getTransformer(
130
155
  packageName,
131
156
  version,
@@ -10,6 +10,29 @@ import {
10
10
  import * as NodeModule from "module";
11
11
  import { sep } from "path";
12
12
  import moduleDetailsFromPath from "module-details-from-path";
13
+
14
+ // src/auto-instrumentations/loader/openai-api-promise-patch.ts
15
+ var OPENAI_API_PROMISE_PATCH = `
16
+ ;(function __btPatchAPIPromise() {
17
+ if (typeof APIPromise === "undefined" || APIPromise.prototype.__btParsePatched) return;
18
+ APIPromise.prototype.__btParsePatched = true;
19
+ var _origThen = APIPromise.prototype.then;
20
+ APIPromise.prototype.then = function __btThen(onfulfilled, onrejected) {
21
+ if (!this.__btParseWrapped && Object.prototype.hasOwnProperty.call(this, "parseResponse")) {
22
+ this.__btParseWrapped = true;
23
+ var _origParse = this.parseResponse;
24
+ var _cached;
25
+ this.parseResponse = function() {
26
+ if (!_cached) _cached = _origParse.apply(this, arguments);
27
+ return _cached;
28
+ };
29
+ }
30
+ return _origThen.call(this, onfulfilled, onrejected);
31
+ };
32
+ })();
33
+ `;
34
+
35
+ // src/auto-instrumentations/loader/cjs-patch.ts
13
36
  var ModulePatch = class {
14
37
  packages;
15
38
  instrumentator;
@@ -40,8 +63,12 @@ var ModulePatch = class {
40
63
  if (!self.packages.has(packageName)) {
41
64
  return self.originalCompile.apply(this, args);
42
65
  }
43
- const version = getPackageVersion(resolvedModule.basedir);
44
66
  const normalizedModulePath = resolvedModule.path.replace(/\\/g, "/");
67
+ if (packageName === "openai" && normalizedModulePath.includes("api-promise")) {
68
+ args[0] = content + OPENAI_API_PROMISE_PATCH;
69
+ return self.originalCompile.apply(this, args);
70
+ }
71
+ const version = getPackageVersion(resolvedModule.basedir);
45
72
  const transformer = self.instrumentator.getTransformer(
46
73
  packageName,
47
74
  version,
@@ -1,12 +1,13 @@
1
1
  import {
2
+ OPENAI_API_PROMISE_PATCH,
2
3
  getPackageName,
3
4
  getPackageVersion
4
- } from "../chunk-ITP7RAUY.mjs";
5
+ } from "../chunk-MWZXZQUO.mjs";
5
6
 
6
7
  // src/auto-instrumentations/loader/esm-hook.mts
7
8
  import { readFile } from "fs/promises";
8
9
  import { fileURLToPath } from "url";
9
- import { sep } from "path";
10
+ import { extname, sep } from "path";
10
11
  import {
11
12
  create
12
13
  } from "@apm-js-collab/code-transformer";
@@ -14,6 +15,23 @@ import moduleDetailsFromPath from "module-details-from-path";
14
15
  var instrumentator;
15
16
  var packages;
16
17
  var transformers = /* @__PURE__ */ new Map();
18
+ function getModuleType(url, format) {
19
+ if (format === "module") {
20
+ return "esm";
21
+ }
22
+ if (format === "commonjs") {
23
+ return "cjs";
24
+ }
25
+ const pathname = url.startsWith("file:") ? fileURLToPath(url) : url;
26
+ const ext = extname(pathname);
27
+ if (ext === ".mjs") {
28
+ return "esm";
29
+ }
30
+ if (ext === ".cjs") {
31
+ return "cjs";
32
+ }
33
+ return "unknown";
34
+ }
17
35
  async function initialize(data = {}) {
18
36
  const configs = data.instrumentations || [];
19
37
  instrumentator = create(configs);
@@ -42,8 +60,22 @@ async function resolve(specifier, context, nextResolve) {
42
60
  }
43
61
  return url;
44
62
  }
63
+ function isOpenAIApiPromise(url) {
64
+ return url.includes("/openai") && url.includes("api-promise");
65
+ }
45
66
  async function load(url, context, nextLoad) {
46
67
  const result = await nextLoad(url, context);
68
+ if (isOpenAIApiPromise(url)) {
69
+ if (result.format === "commonjs") {
70
+ const parsedUrl = new URL(result.responseURL ?? url);
71
+ result.source ??= await readFile(parsedUrl);
72
+ }
73
+ if (result.source) {
74
+ result.source = result.source.toString("utf8") + OPENAI_API_PROMISE_PATCH;
75
+ result.shortCircuit = true;
76
+ }
77
+ return result;
78
+ }
47
79
  if (!transformers.has(url)) {
48
80
  return result;
49
81
  }
@@ -55,7 +87,7 @@ async function load(url, context, nextLoad) {
55
87
  if (code) {
56
88
  const transformer = transformers.get(url);
57
89
  try {
58
- const moduleType = result.format === "module" ? "esm" : result.format === "commonjs" ? "cjs" : "unknown";
90
+ const moduleType = getModuleType(url, result.format);
59
91
  const transformedCode = transformer.transform(
60
92
  code.toString("utf8"),
61
93
  moduleType
@@ -0,0 +1,44 @@
1
+ import { InstrumentationConfig } from '@apm-js-collab/code-transformer';
2
+
3
+ interface LegacyBundlerPluginOptions {
4
+ /**
5
+ * Enable debug logging
6
+ */
7
+ debug?: boolean;
8
+ /**
9
+ * Additional instrumentation configs to apply
10
+ */
11
+ instrumentations?: InstrumentationConfig[];
12
+ /**
13
+ * Whether to bundle for browser environments.
14
+ *
15
+ * When true, uses 'dc-browser' for browser-compatible diagnostics_channel polyfill.
16
+ * When false, uses Node.js built-in 'diagnostics_channel' and 'async_hooks'.
17
+ * Defaults to true (assumes browser build).
18
+ */
19
+ browser?: boolean;
20
+ }
21
+ interface BundlerPluginOptions {
22
+ /**
23
+ * Enable debug logging
24
+ */
25
+ debug?: boolean;
26
+ /**
27
+ * Additional instrumentation configs to apply
28
+ */
29
+ instrumentations?: InstrumentationConfig[];
30
+ /**
31
+ * Use the `diagnostics_channel` compatibility shim in patched code instead
32
+ * of Node.js's built-in `diagnostics_channel` module.
33
+ *
34
+ * Enable this for browser, edge, or worker bundles where Node's
35
+ * `diagnostics_channel` module is unavailable. Leave it disabled for Node.js
36
+ * bundles so transformed SDK code publishes on the native `diagnostics_channel`
37
+ * registry.
38
+ *
39
+ * @default false
40
+ */
41
+ useDiagnosticChannelCompatShim?: boolean;
42
+ }
43
+
44
+ export type { BundlerPluginOptions as B, LegacyBundlerPluginOptions as L };
@@ -0,0 +1,44 @@
1
+ import { InstrumentationConfig } from '@apm-js-collab/code-transformer';
2
+
3
+ interface LegacyBundlerPluginOptions {
4
+ /**
5
+ * Enable debug logging
6
+ */
7
+ debug?: boolean;
8
+ /**
9
+ * Additional instrumentation configs to apply
10
+ */
11
+ instrumentations?: InstrumentationConfig[];
12
+ /**
13
+ * Whether to bundle for browser environments.
14
+ *
15
+ * When true, uses 'dc-browser' for browser-compatible diagnostics_channel polyfill.
16
+ * When false, uses Node.js built-in 'diagnostics_channel' and 'async_hooks'.
17
+ * Defaults to true (assumes browser build).
18
+ */
19
+ browser?: boolean;
20
+ }
21
+ interface BundlerPluginOptions {
22
+ /**
23
+ * Enable debug logging
24
+ */
25
+ debug?: boolean;
26
+ /**
27
+ * Additional instrumentation configs to apply
28
+ */
29
+ instrumentations?: InstrumentationConfig[];
30
+ /**
31
+ * Use the `diagnostics_channel` compatibility shim in patched code instead
32
+ * of Node.js's built-in `diagnostics_channel` module.
33
+ *
34
+ * Enable this for browser, edge, or worker bundles where Node's
35
+ * `diagnostics_channel` module is unavailable. Leave it disabled for Node.js
36
+ * bundles so transformed SDK code publishes on the native `diagnostics_channel`
37
+ * registry.
38
+ *
39
+ * @default false
40
+ */
41
+ useDiagnosticChannelCompatShim?: boolean;
42
+ }
43
+
44
+ export type { BundlerPluginOptions as B, LegacyBundlerPluginOptions as L };