braintrust 3.25.0 → 3.26.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.
- package/NOTICE +6 -0
- package/dev/dist/index.d.mts +130 -2
- package/dev/dist/index.d.ts +130 -2
- package/dev/dist/index.js +2455 -1116
- package/dev/dist/index.mjs +1599 -260
- package/dist/apply-auto-instrumentation.js +439 -398
- package/dist/apply-auto-instrumentation.mjs +242 -201
- package/dist/auto-instrumentations/bundler/esbuild.cjs +853 -337
- package/dist/auto-instrumentations/bundler/esbuild.d.mts +4 -4
- package/dist/auto-instrumentations/bundler/esbuild.d.ts +4 -4
- package/dist/auto-instrumentations/bundler/esbuild.mjs +5 -8
- package/dist/auto-instrumentations/bundler/next.cjs +852 -356
- package/dist/auto-instrumentations/bundler/next.mjs +6 -29
- package/dist/auto-instrumentations/bundler/rollup.cjs +853 -337
- package/dist/auto-instrumentations/bundler/rollup.d.mts +4 -4
- package/dist/auto-instrumentations/bundler/rollup.d.ts +4 -4
- package/dist/auto-instrumentations/bundler/rollup.mjs +5 -8
- package/dist/auto-instrumentations/bundler/vite.cjs +853 -337
- package/dist/auto-instrumentations/bundler/vite.d.mts +4 -4
- package/dist/auto-instrumentations/bundler/vite.d.ts +4 -4
- package/dist/auto-instrumentations/bundler/vite.mjs +5 -8
- package/dist/auto-instrumentations/bundler/webpack-loader.cjs +1006 -266
- package/dist/auto-instrumentations/bundler/webpack-loader.d.ts +20 -12
- package/dist/auto-instrumentations/bundler/webpack.cjs +853 -337
- package/dist/auto-instrumentations/bundler/webpack.d.mts +4 -4
- package/dist/auto-instrumentations/bundler/webpack.d.ts +4 -4
- package/dist/auto-instrumentations/bundler/webpack.mjs +5 -4
- package/dist/auto-instrumentations/{chunk-JPVCUKTY.mjs → chunk-6E22MYSW.mjs} +165 -73
- package/dist/auto-instrumentations/{chunk-VT6DDNKM.mjs → chunk-AKKPLXTP.mjs} +631 -194
- package/dist/auto-instrumentations/chunk-NRE4QUQR.mjs +509 -0
- package/dist/auto-instrumentations/{chunk-7P6563SW.mjs → chunk-V5LEODRX.mjs} +2 -6
- package/dist/auto-instrumentations/{chunk-M6DLIJ2Z.mjs → chunk-XYN63IG5.mjs} +124 -194
- package/dist/auto-instrumentations/{chunk-CZ24KNHT.mjs → chunk-XZHHE4Y3.mjs} +77 -68
- package/dist/auto-instrumentations/hook.mjs +247 -182
- package/dist/auto-instrumentations/index.cjs +557 -48
- package/dist/auto-instrumentations/index.d.mts +6 -2
- package/dist/auto-instrumentations/index.d.ts +6 -2
- package/dist/auto-instrumentations/index.mjs +6 -1
- package/dist/auto-instrumentations/loader/cjs-patch.cjs +492 -195
- package/dist/auto-instrumentations/loader/cjs-patch.d.mts +2 -2
- package/dist/auto-instrumentations/loader/cjs-patch.d.ts +2 -2
- package/dist/auto-instrumentations/loader/cjs-patch.mjs +3 -2
- package/dist/auto-instrumentations/loader/esm-hook.mjs +1 -1
- package/dist/auto-instrumentations/plugin-B-aZh7EP.d.ts +30 -0
- package/dist/auto-instrumentations/plugin-Bnj4E_6B.d.mts +30 -0
- package/dist/auto-instrumentations/{types-RNPaKi9o.d.mts → types-C1fVeiCp.d.mts} +1 -1
- package/dist/auto-instrumentations/{types-RNPaKi9o.d.ts → types-C1fVeiCp.d.ts} +1 -1
- package/dist/browser.d.mts +817 -197
- package/dist/browser.d.ts +817 -197
- package/dist/browser.js +1883 -265
- package/dist/browser.mjs +1883 -265
- package/dist/{chunk-ABR2QWDP.mjs → chunk-PN7EWK66.mjs} +581 -133
- package/dist/{chunk-GSIDVFE6.js → chunk-VRZZQPAA.js} +1670 -736
- package/dist/{chunk-P25IOOU4.js → chunk-VYNNEKSA.js} +586 -138
- package/dist/{chunk-2SANLSWX.mjs → chunk-XIZOM2HO.mjs} +1052 -118
- package/dist/cli.js +1601 -263
- package/dist/edge-light.d.mts +1 -1
- package/dist/edge-light.d.ts +1 -1
- package/dist/edge-light.js +1883 -268
- package/dist/edge-light.mjs +1883 -268
- package/dist/index.d.mts +817 -197
- package/dist/index.d.ts +817 -197
- package/dist/index.js +695 -435
- package/dist/index.mjs +264 -4
- package/dist/instrumentation/index.d.mts +130 -6
- package/dist/instrumentation/index.d.ts +130 -6
- package/dist/instrumentation/index.js +1510 -80
- package/dist/instrumentation/index.mjs +1510 -80
- package/dist/vitest-evals-reporter.js +16 -16
- package/dist/vitest-evals-reporter.mjs +2 -2
- package/dist/workerd.d.mts +1 -1
- package/dist/workerd.d.ts +1 -1
- package/dist/workerd.js +1883 -268
- package/dist/workerd.mjs +1883 -268
- package/licenses/node-diagnostics-channel/LICENSE +19 -0
- package/package.json +34 -35
- package/dist/auto-instrumentations/plugin-D0KHwSJv.d.mts +0 -44
- package/dist/auto-instrumentations/plugin-G6fgxk1b.d.ts +0 -44
|
@@ -1,81 +1,525 @@
|
|
|
1
|
-
// src/
|
|
2
|
-
var
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
1
|
+
// src/global-instrumentation-hooks.ts
|
|
2
|
+
var GLOBAL_INSTRUMENTATION_HOOKS_KEY = "__braintrust_instrumentation_hooks";
|
|
3
|
+
var GLOBAL_INSTRUMENTATION_HOOKS_PROTOCOL_VERSION = 1;
|
|
4
|
+
var GLOBAL_INSTRUMENTATION_HOOKS_REGISTRY_BRAND = "braintrust.global-instrumentation-hooks.registry";
|
|
5
|
+
var GLOBAL_INSTRUMENTATION_HOOK_BRAND = "braintrust.global-instrumentation-hooks.hook";
|
|
6
|
+
var registryBrand = Symbol.for(GLOBAL_INSTRUMENTATION_HOOKS_REGISTRY_BRAND);
|
|
7
|
+
var hookBrand = Symbol.for(GLOBAL_INSTRUMENTATION_HOOK_BRAND);
|
|
8
|
+
var errorReporter;
|
|
9
|
+
function setGlobalHookErrorReporter(reporter) {
|
|
10
|
+
const previousReporter = errorReporter;
|
|
11
|
+
errorReporter = reporter;
|
|
12
|
+
return () => {
|
|
13
|
+
if (errorReporter === reporter) {
|
|
14
|
+
errorReporter = previousReporter;
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
function reportError(error) {
|
|
19
|
+
try {
|
|
20
|
+
errorReporter?.(error);
|
|
21
|
+
} catch {
|
|
9
22
|
}
|
|
10
|
-
|
|
11
|
-
|
|
23
|
+
}
|
|
24
|
+
function setContextValue(context, key, value) {
|
|
25
|
+
try {
|
|
26
|
+
context[key] = value;
|
|
27
|
+
} catch (error) {
|
|
28
|
+
reportError(error);
|
|
12
29
|
}
|
|
13
|
-
}
|
|
14
|
-
|
|
30
|
+
}
|
|
31
|
+
function wrapStoreRun(store, message, next, transform) {
|
|
32
|
+
return () => {
|
|
33
|
+
let context;
|
|
34
|
+
try {
|
|
35
|
+
context = transform ? transform(message) : message;
|
|
36
|
+
} catch (error) {
|
|
37
|
+
reportError(error);
|
|
38
|
+
return next();
|
|
39
|
+
}
|
|
40
|
+
let called = false;
|
|
41
|
+
let result;
|
|
42
|
+
let providerError;
|
|
43
|
+
let providerThrew = false;
|
|
44
|
+
const runNext = () => {
|
|
45
|
+
if (called) {
|
|
46
|
+
reportError(
|
|
47
|
+
new Error(
|
|
48
|
+
"Instrumentation store invoked its callback more than once"
|
|
49
|
+
)
|
|
50
|
+
);
|
|
51
|
+
if (providerThrew) {
|
|
52
|
+
throw providerError;
|
|
53
|
+
}
|
|
54
|
+
return result;
|
|
55
|
+
}
|
|
56
|
+
called = true;
|
|
57
|
+
try {
|
|
58
|
+
result = next();
|
|
59
|
+
return result;
|
|
60
|
+
} catch (error) {
|
|
61
|
+
providerThrew = true;
|
|
62
|
+
providerError = error;
|
|
63
|
+
throw error;
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
try {
|
|
67
|
+
store.run(context, runNext);
|
|
68
|
+
} catch (error) {
|
|
69
|
+
if (!providerThrew || error !== providerError) {
|
|
70
|
+
reportError(error);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
if (!called) {
|
|
74
|
+
reportError(
|
|
75
|
+
new Error("Instrumentation store did not invoke its callback")
|
|
76
|
+
);
|
|
77
|
+
return runNext();
|
|
78
|
+
}
|
|
79
|
+
if (providerThrew) {
|
|
80
|
+
throw providerError;
|
|
81
|
+
}
|
|
82
|
+
return result;
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
var HookChannel = class {
|
|
15
86
|
constructor(name) {
|
|
16
87
|
this.name = name;
|
|
17
88
|
}
|
|
18
89
|
name;
|
|
19
|
-
|
|
20
|
-
|
|
90
|
+
subscribers = [];
|
|
91
|
+
stores = /* @__PURE__ */ new Map();
|
|
92
|
+
get hasSubscribers() {
|
|
93
|
+
return this.subscribers.length > 0 || this.stores.size > 0;
|
|
21
94
|
}
|
|
22
|
-
|
|
23
|
-
|
|
95
|
+
subscribe(subscription) {
|
|
96
|
+
if (typeof subscription !== "function") {
|
|
97
|
+
throw new TypeError("subscription must be a function");
|
|
98
|
+
}
|
|
99
|
+
this.subscribers = [...this.subscribers, subscription];
|
|
100
|
+
}
|
|
101
|
+
unsubscribe(subscription) {
|
|
102
|
+
const index = this.subscribers.indexOf(subscription);
|
|
103
|
+
if (index === -1) {
|
|
104
|
+
return false;
|
|
105
|
+
}
|
|
106
|
+
this.subscribers = [
|
|
107
|
+
...this.subscribers.slice(0, index),
|
|
108
|
+
...this.subscribers.slice(index + 1)
|
|
109
|
+
];
|
|
110
|
+
return true;
|
|
24
111
|
}
|
|
25
|
-
bindStore(
|
|
112
|
+
bindStore(store, transform) {
|
|
113
|
+
if (!store || typeof store.run !== "function") {
|
|
114
|
+
throw new TypeError("store must have a run method");
|
|
115
|
+
}
|
|
116
|
+
this.stores.set(
|
|
117
|
+
store,
|
|
118
|
+
transform
|
|
119
|
+
);
|
|
26
120
|
}
|
|
27
|
-
unbindStore(
|
|
28
|
-
return
|
|
121
|
+
unbindStore(store) {
|
|
122
|
+
return this.stores.delete(store);
|
|
29
123
|
}
|
|
30
|
-
publish(
|
|
124
|
+
publish(message) {
|
|
125
|
+
const subscribers = this.subscribers;
|
|
126
|
+
for (const subscriber of subscribers) {
|
|
127
|
+
try {
|
|
128
|
+
subscriber(message, this.name);
|
|
129
|
+
} catch (error) {
|
|
130
|
+
reportError(error);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
31
133
|
}
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
134
|
+
runStores(message, fn, thisArg, ...args) {
|
|
135
|
+
let run = () => {
|
|
136
|
+
this.publish(message);
|
|
137
|
+
return Reflect.apply(fn, thisArg, args);
|
|
138
|
+
};
|
|
139
|
+
for (const [store, transform] of this.stores.entries()) {
|
|
140
|
+
run = wrapStoreRun(store, message, run, transform);
|
|
141
|
+
}
|
|
142
|
+
return run();
|
|
35
143
|
}
|
|
36
144
|
};
|
|
37
|
-
var
|
|
145
|
+
var traceEvents = [
|
|
146
|
+
"start",
|
|
147
|
+
"end",
|
|
148
|
+
"asyncStart",
|
|
149
|
+
"asyncEnd",
|
|
150
|
+
"error"
|
|
151
|
+
];
|
|
152
|
+
var TracingHook = class {
|
|
38
153
|
start;
|
|
39
154
|
end;
|
|
40
155
|
asyncStart;
|
|
41
156
|
asyncEnd;
|
|
42
157
|
error;
|
|
43
158
|
constructor(nameOrChannels) {
|
|
159
|
+
Object.defineProperty(this, hookBrand, {
|
|
160
|
+
configurable: false,
|
|
161
|
+
enumerable: false,
|
|
162
|
+
value: GLOBAL_INSTRUMENTATION_HOOKS_PROTOCOL_VERSION,
|
|
163
|
+
writable: false
|
|
164
|
+
});
|
|
44
165
|
if (typeof nameOrChannels === "string") {
|
|
45
|
-
this.start = new
|
|
46
|
-
this.end = new
|
|
47
|
-
this.asyncStart = new
|
|
48
|
-
|
|
49
|
-
);
|
|
50
|
-
this.asyncEnd = new DefaultChannel(`tracing:${nameOrChannels}:asyncEnd`);
|
|
51
|
-
this.error = new DefaultChannel(`tracing:${nameOrChannels}:error`);
|
|
166
|
+
this.start = new HookChannel(`tracing:${nameOrChannels}:start`);
|
|
167
|
+
this.end = new HookChannel(`tracing:${nameOrChannels}:end`);
|
|
168
|
+
this.asyncStart = new HookChannel(`tracing:${nameOrChannels}:asyncStart`);
|
|
169
|
+
this.asyncEnd = new HookChannel(`tracing:${nameOrChannels}:asyncEnd`);
|
|
170
|
+
this.error = new HookChannel(`tracing:${nameOrChannels}:error`);
|
|
52
171
|
return;
|
|
53
172
|
}
|
|
54
|
-
this.start = nameOrChannels.start ?? new
|
|
55
|
-
this.end = nameOrChannels.end ?? new
|
|
56
|
-
this.asyncStart = nameOrChannels.asyncStart ?? new
|
|
57
|
-
this.asyncEnd = nameOrChannels.asyncEnd ?? new
|
|
58
|
-
this.error = nameOrChannels.error ?? new
|
|
173
|
+
this.start = nameOrChannels.start ?? new HookChannel("tracing:start");
|
|
174
|
+
this.end = nameOrChannels.end ?? new HookChannel("tracing:end");
|
|
175
|
+
this.asyncStart = nameOrChannels.asyncStart ?? new HookChannel("tracing:asyncStart");
|
|
176
|
+
this.asyncEnd = nameOrChannels.asyncEnd ?? new HookChannel("tracing:asyncEnd");
|
|
177
|
+
this.error = nameOrChannels.error ?? new HookChannel("tracing:error");
|
|
59
178
|
}
|
|
60
179
|
get hasSubscribers() {
|
|
61
180
|
return this.start.hasSubscribers || this.end.hasSubscribers || this.asyncStart.hasSubscribers || this.asyncEnd.hasSubscribers || this.error.hasSubscribers;
|
|
62
181
|
}
|
|
63
|
-
subscribe(
|
|
182
|
+
subscribe(handlers) {
|
|
183
|
+
for (const eventName of traceEvents) {
|
|
184
|
+
const handler = handlers[eventName];
|
|
185
|
+
if (handler) {
|
|
186
|
+
this[eventName].subscribe(handler);
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
unsubscribe(handlers) {
|
|
191
|
+
let done = true;
|
|
192
|
+
for (const eventName of traceEvents) {
|
|
193
|
+
const handler = handlers[eventName];
|
|
194
|
+
if (handler && !this[eventName].unsubscribe(handler)) {
|
|
195
|
+
done = false;
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
return done;
|
|
199
|
+
}
|
|
200
|
+
traceSync(fn, message = {}, thisArg, ...args) {
|
|
201
|
+
if (!this.hasSubscribers) {
|
|
202
|
+
return Reflect.apply(fn, thisArg, args);
|
|
203
|
+
}
|
|
204
|
+
const context = message;
|
|
205
|
+
return this.start.runStores(message, () => {
|
|
206
|
+
try {
|
|
207
|
+
const result = Reflect.apply(fn, thisArg, args);
|
|
208
|
+
setContextValue(context, "result", result);
|
|
209
|
+
return result;
|
|
210
|
+
} catch (error) {
|
|
211
|
+
setContextValue(context, "error", error);
|
|
212
|
+
this.error.publish(message);
|
|
213
|
+
throw error;
|
|
214
|
+
} finally {
|
|
215
|
+
this.end.publish(message);
|
|
216
|
+
}
|
|
217
|
+
});
|
|
218
|
+
}
|
|
219
|
+
tracePromise(fn, message = {}, thisArg, ...args) {
|
|
220
|
+
if (!this.hasSubscribers) {
|
|
221
|
+
return Reflect.apply(fn, thisArg, args);
|
|
222
|
+
}
|
|
223
|
+
const context = message;
|
|
224
|
+
return this.start.runStores(message, () => {
|
|
225
|
+
let result;
|
|
226
|
+
try {
|
|
227
|
+
result = Reflect.apply(fn, thisArg, args);
|
|
228
|
+
} catch (error) {
|
|
229
|
+
setContextValue(context, "error", error);
|
|
230
|
+
this.error.publish(message);
|
|
231
|
+
this.end.publish(message);
|
|
232
|
+
throw error;
|
|
233
|
+
}
|
|
234
|
+
this.end.publish(message);
|
|
235
|
+
if (!result || typeof result !== "object" && typeof result !== "function") {
|
|
236
|
+
setContextValue(context, "result", result);
|
|
237
|
+
this.asyncStart.publish(message);
|
|
238
|
+
this.asyncEnd.publish(message);
|
|
239
|
+
return result;
|
|
240
|
+
}
|
|
241
|
+
let terminalPublished = false;
|
|
242
|
+
const finishUnobservedResult = (error) => {
|
|
243
|
+
reportError(error);
|
|
244
|
+
if (!terminalPublished) {
|
|
245
|
+
terminalPublished = true;
|
|
246
|
+
setContextValue(context, "result", result);
|
|
247
|
+
this.asyncStart.publish(message);
|
|
248
|
+
this.asyncEnd.publish(message);
|
|
249
|
+
}
|
|
250
|
+
return result;
|
|
251
|
+
};
|
|
252
|
+
let then;
|
|
253
|
+
try {
|
|
254
|
+
then = result.then;
|
|
255
|
+
} catch (error) {
|
|
256
|
+
return finishUnobservedResult(error);
|
|
257
|
+
}
|
|
258
|
+
if (typeof then !== "function") {
|
|
259
|
+
setContextValue(context, "result", result);
|
|
260
|
+
this.asyncStart.publish(message);
|
|
261
|
+
this.asyncEnd.publish(message);
|
|
262
|
+
return result;
|
|
263
|
+
}
|
|
264
|
+
const resolve = (resolved) => {
|
|
265
|
+
if (!terminalPublished) {
|
|
266
|
+
terminalPublished = true;
|
|
267
|
+
setContextValue(context, "result", resolved);
|
|
268
|
+
this.asyncStart.publish(message);
|
|
269
|
+
this.asyncEnd.publish(message);
|
|
270
|
+
}
|
|
271
|
+
return resolved;
|
|
272
|
+
};
|
|
273
|
+
let rejectionThrown = false;
|
|
274
|
+
let rejectionError;
|
|
275
|
+
const reject = (error) => {
|
|
276
|
+
if (!terminalPublished) {
|
|
277
|
+
terminalPublished = true;
|
|
278
|
+
setContextValue(context, "error", error);
|
|
279
|
+
this.error.publish(message);
|
|
280
|
+
this.asyncStart.publish(message);
|
|
281
|
+
this.asyncEnd.publish(message);
|
|
282
|
+
}
|
|
283
|
+
rejectionThrown = true;
|
|
284
|
+
rejectionError = error;
|
|
285
|
+
throw error;
|
|
286
|
+
};
|
|
287
|
+
let isPlainPromise;
|
|
288
|
+
try {
|
|
289
|
+
isPlainPromise = result instanceof Promise && result.constructor === Promise;
|
|
290
|
+
} catch (error) {
|
|
291
|
+
return finishUnobservedResult(error);
|
|
292
|
+
}
|
|
293
|
+
try {
|
|
294
|
+
if (isPlainPromise) {
|
|
295
|
+
return Reflect.apply(then, result, [resolve, reject]);
|
|
296
|
+
}
|
|
297
|
+
Reflect.apply(then, result, [
|
|
298
|
+
resolve,
|
|
299
|
+
(error) => {
|
|
300
|
+
try {
|
|
301
|
+
reject(error);
|
|
302
|
+
} catch {
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
]);
|
|
306
|
+
} catch (error) {
|
|
307
|
+
if (rejectionThrown && error === rejectionError) {
|
|
308
|
+
return result;
|
|
309
|
+
}
|
|
310
|
+
return finishUnobservedResult(error);
|
|
311
|
+
}
|
|
312
|
+
return result;
|
|
313
|
+
});
|
|
64
314
|
}
|
|
65
|
-
|
|
315
|
+
traceCallback(fn, position = -1, message = {}, thisArg, ...args) {
|
|
316
|
+
if (!this.hasSubscribers) {
|
|
317
|
+
return Reflect.apply(fn, thisArg, args);
|
|
318
|
+
}
|
|
319
|
+
const context = message;
|
|
320
|
+
const callArgs = args.length > 0 ? args : context.arguments ?? args;
|
|
321
|
+
const callback = Array.prototype.at.call(callArgs, position);
|
|
322
|
+
if (typeof callback !== "function") {
|
|
323
|
+
return Reflect.apply(fn, thisArg, args);
|
|
324
|
+
}
|
|
325
|
+
const { asyncStart, asyncEnd, error: errorChannel } = this;
|
|
326
|
+
function wrappedCallback(error, result) {
|
|
327
|
+
if (error) {
|
|
328
|
+
setContextValue(context, "error", error);
|
|
329
|
+
errorChannel.publish(message);
|
|
330
|
+
} else {
|
|
331
|
+
setContextValue(context, "result", result);
|
|
332
|
+
}
|
|
333
|
+
return asyncStart.runStores(message, () => {
|
|
334
|
+
try {
|
|
335
|
+
return Reflect.apply(callback, this, arguments);
|
|
336
|
+
} finally {
|
|
337
|
+
asyncEnd.publish(message);
|
|
338
|
+
}
|
|
339
|
+
});
|
|
340
|
+
}
|
|
341
|
+
Array.prototype.splice.call(callArgs, position, 1, wrappedCallback);
|
|
342
|
+
return this.start.runStores(message, () => {
|
|
343
|
+
try {
|
|
344
|
+
return Reflect.apply(fn, thisArg, args);
|
|
345
|
+
} catch (error) {
|
|
346
|
+
setContextValue(context, "error", error);
|
|
347
|
+
this.error.publish(message);
|
|
348
|
+
throw error;
|
|
349
|
+
} finally {
|
|
350
|
+
this.end.publish(message);
|
|
351
|
+
}
|
|
352
|
+
});
|
|
353
|
+
}
|
|
354
|
+
};
|
|
355
|
+
var inertChannel = Object.freeze({
|
|
356
|
+
name: "braintrust:inert",
|
|
357
|
+
hasSubscribers: false,
|
|
358
|
+
subscribe() {
|
|
359
|
+
},
|
|
360
|
+
unsubscribe() {
|
|
361
|
+
return false;
|
|
362
|
+
},
|
|
363
|
+
bindStore() {
|
|
364
|
+
},
|
|
365
|
+
unbindStore() {
|
|
66
366
|
return false;
|
|
367
|
+
},
|
|
368
|
+
publish() {
|
|
369
|
+
},
|
|
370
|
+
runStores(_message, fn, thisArg, ...args) {
|
|
371
|
+
return Reflect.apply(fn, thisArg, args);
|
|
67
372
|
}
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
373
|
+
});
|
|
374
|
+
var inertTracingHook = new TracingHook({
|
|
375
|
+
start: inertChannel,
|
|
376
|
+
end: inertChannel,
|
|
377
|
+
asyncStart: inertChannel,
|
|
378
|
+
asyncEnd: inertChannel,
|
|
379
|
+
error: inertChannel
|
|
380
|
+
});
|
|
381
|
+
function isHookChannel(value) {
|
|
382
|
+
if (typeof value !== "object" && typeof value !== "function" || value === null) {
|
|
383
|
+
return false;
|
|
71
384
|
}
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
return
|
|
385
|
+
try {
|
|
386
|
+
const channel2 = value;
|
|
387
|
+
return typeof channel2.hasSubscribers === "boolean" && typeof channel2.subscribe === "function" && typeof channel2.unsubscribe === "function" && typeof channel2.bindStore === "function" && typeof channel2.unbindStore === "function" && typeof channel2.publish === "function" && typeof channel2.runStores === "function";
|
|
388
|
+
} catch {
|
|
389
|
+
return false;
|
|
75
390
|
}
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
391
|
+
}
|
|
392
|
+
function hasTracingHookShape(value) {
|
|
393
|
+
if (typeof value !== "object" && typeof value !== "function" || value === null) {
|
|
394
|
+
return false;
|
|
395
|
+
}
|
|
396
|
+
try {
|
|
397
|
+
const hook = value;
|
|
398
|
+
return typeof hook.hasSubscribers === "boolean" && typeof hook.subscribe === "function" && typeof hook.unsubscribe === "function" && typeof hook.traceSync === "function" && typeof hook.tracePromise === "function" && typeof hook.traceCallback === "function" && isHookChannel(hook.start) && isHookChannel(hook.end) && isHookChannel(hook.asyncStart) && isHookChannel(hook.asyncEnd) && isHookChannel(hook.error);
|
|
399
|
+
} catch {
|
|
400
|
+
return false;
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
function isCompatibleTracingHook(value) {
|
|
404
|
+
try {
|
|
405
|
+
return value[hookBrand] === GLOBAL_INSTRUMENTATION_HOOKS_PROTOCOL_VERSION && hasTracingHookShape(value);
|
|
406
|
+
} catch {
|
|
407
|
+
return false;
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
function isCompatibleHookRegistry(value) {
|
|
411
|
+
try {
|
|
412
|
+
return value instanceof Map && value[registryBrand] === GLOBAL_INSTRUMENTATION_HOOKS_PROTOCOL_VERSION;
|
|
413
|
+
} catch {
|
|
414
|
+
return false;
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
function getHookRegistry() {
|
|
418
|
+
let descriptor;
|
|
419
|
+
try {
|
|
420
|
+
descriptor = Object.getOwnPropertyDescriptor(
|
|
421
|
+
globalThis,
|
|
422
|
+
GLOBAL_INSTRUMENTATION_HOOKS_KEY
|
|
423
|
+
);
|
|
424
|
+
} catch (error) {
|
|
425
|
+
reportError(error);
|
|
426
|
+
return void 0;
|
|
427
|
+
}
|
|
428
|
+
if (descriptor && "value" in descriptor && isCompatibleHookRegistry(descriptor.value) && (descriptor.configurable || descriptor.enumerable === false)) {
|
|
429
|
+
if (descriptor.configurable || descriptor.writable) {
|
|
430
|
+
try {
|
|
431
|
+
Object.defineProperty(globalThis, GLOBAL_INSTRUMENTATION_HOOKS_KEY, {
|
|
432
|
+
configurable: false,
|
|
433
|
+
enumerable: false,
|
|
434
|
+
value: descriptor.value,
|
|
435
|
+
writable: false
|
|
436
|
+
});
|
|
437
|
+
} catch (error) {
|
|
438
|
+
reportError(error);
|
|
439
|
+
return void 0;
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
return descriptor.value;
|
|
443
|
+
}
|
|
444
|
+
if (descriptor && !descriptor.configurable) {
|
|
445
|
+
reportError(new Error("Incompatible global instrumentation hook registry"));
|
|
446
|
+
return void 0;
|
|
447
|
+
}
|
|
448
|
+
const registry2 = /* @__PURE__ */ new Map();
|
|
449
|
+
try {
|
|
450
|
+
Object.defineProperty(registry2, registryBrand, {
|
|
451
|
+
configurable: false,
|
|
452
|
+
enumerable: false,
|
|
453
|
+
value: GLOBAL_INSTRUMENTATION_HOOKS_PROTOCOL_VERSION,
|
|
454
|
+
writable: false
|
|
455
|
+
});
|
|
456
|
+
} catch (error) {
|
|
457
|
+
reportError(error);
|
|
458
|
+
return void 0;
|
|
459
|
+
}
|
|
460
|
+
try {
|
|
461
|
+
Object.defineProperty(globalThis, GLOBAL_INSTRUMENTATION_HOOKS_KEY, {
|
|
462
|
+
configurable: false,
|
|
463
|
+
enumerable: false,
|
|
464
|
+
value: registry2,
|
|
465
|
+
writable: false
|
|
466
|
+
});
|
|
467
|
+
return registry2;
|
|
468
|
+
} catch (error) {
|
|
469
|
+
reportError(error);
|
|
470
|
+
return void 0;
|
|
471
|
+
}
|
|
472
|
+
}
|
|
473
|
+
function newGlobalTracingChannel(nameOrChannels) {
|
|
474
|
+
if (typeof nameOrChannels !== "string") {
|
|
475
|
+
return new TracingHook(nameOrChannels);
|
|
476
|
+
}
|
|
477
|
+
const registry2 = getHookRegistry();
|
|
478
|
+
if (!registry2) {
|
|
479
|
+
return inertTracingHook;
|
|
480
|
+
}
|
|
481
|
+
let existing;
|
|
482
|
+
try {
|
|
483
|
+
existing = Map.prototype.get.call(registry2, nameOrChannels);
|
|
484
|
+
} catch (error) {
|
|
485
|
+
reportError(error);
|
|
486
|
+
return inertTracingHook;
|
|
487
|
+
}
|
|
488
|
+
if (isCompatibleTracingHook(existing)) {
|
|
489
|
+
return existing;
|
|
490
|
+
}
|
|
491
|
+
if (existing !== void 0) {
|
|
492
|
+
reportError(
|
|
493
|
+
new Error(`Invalid global instrumentation hook: ${nameOrChannels}`)
|
|
494
|
+
);
|
|
495
|
+
try {
|
|
496
|
+
Map.prototype.delete.call(registry2, nameOrChannels);
|
|
497
|
+
} catch (error) {
|
|
498
|
+
reportError(error);
|
|
499
|
+
return inertTracingHook;
|
|
500
|
+
}
|
|
501
|
+
}
|
|
502
|
+
const hook = new TracingHook(nameOrChannels);
|
|
503
|
+
try {
|
|
504
|
+
Map.prototype.set.call(registry2, nameOrChannels, hook);
|
|
505
|
+
} catch (error) {
|
|
506
|
+
reportError(error);
|
|
507
|
+
return inertTracingHook;
|
|
508
|
+
}
|
|
509
|
+
return hook;
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
// src/isomorph.ts
|
|
513
|
+
var DefaultAsyncLocalStorage = class {
|
|
514
|
+
constructor() {
|
|
515
|
+
}
|
|
516
|
+
enterWith(_) {
|
|
517
|
+
}
|
|
518
|
+
run(_, callback) {
|
|
519
|
+
return callback();
|
|
520
|
+
}
|
|
521
|
+
getStore() {
|
|
522
|
+
return void 0;
|
|
79
523
|
}
|
|
80
524
|
};
|
|
81
525
|
var iso = {
|
|
@@ -88,7 +532,7 @@ var iso = {
|
|
|
88
532
|
getCallerLocation: () => void 0,
|
|
89
533
|
newAsyncLocalStorage: () => new DefaultAsyncLocalStorage(),
|
|
90
534
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
91
|
-
newTracingChannel: (nameOrChannels) =>
|
|
535
|
+
newTracingChannel: (nameOrChannels) => newGlobalTracingChannel(nameOrChannels),
|
|
92
536
|
processOn: (_0, _1) => {
|
|
93
537
|
},
|
|
94
538
|
basename: (filepath) => filepath.split(/[\\/]/).pop() || filepath,
|
|
@@ -428,6 +872,9 @@ var debugLogger = {
|
|
|
428
872
|
return createDebugLogger(state);
|
|
429
873
|
}
|
|
430
874
|
};
|
|
875
|
+
setGlobalHookErrorReporter((error) => {
|
|
876
|
+
debugLogger.error("Global instrumentation hook error:", error);
|
|
877
|
+
});
|
|
431
878
|
|
|
432
879
|
// src/queue.ts
|
|
433
880
|
var DEFAULT_QUEUE_SIZE = 15e3;
|
|
@@ -2079,7 +2526,8 @@ var ApiKey = z6.object({
|
|
|
2079
2526
|
user_email: z6.union([z6.string(), z6.null()]).optional(),
|
|
2080
2527
|
user_given_name: z6.union([z6.string(), z6.null()]).optional(),
|
|
2081
2528
|
user_family_name: z6.union([z6.string(), z6.null()]).optional(),
|
|
2082
|
-
org_id: z6.union([z6.string(), z6.null()]).optional()
|
|
2529
|
+
org_id: z6.union([z6.string(), z6.null()]).optional(),
|
|
2530
|
+
expires_at: z6.union([z6.string(), z6.null()]).optional()
|
|
2083
2531
|
});
|
|
2084
2532
|
var TriggeredFunctionState = z6.object({
|
|
2085
2533
|
triggered_xact_id: z6.string(),
|
|
@@ -2199,6 +2647,7 @@ var TopicMapData = z6.object({
|
|
|
2199
2647
|
topic_names: z6.record(z6.string()).optional(),
|
|
2200
2648
|
generation_settings: TopicMapGenerationSettings.optional(),
|
|
2201
2649
|
disable_reconciliation: z6.boolean().optional(),
|
|
2650
|
+
reconcile_mode: z6.enum(["evolve", "names_only"]).optional(),
|
|
2202
2651
|
distance_threshold: z6.number().optional(),
|
|
2203
2652
|
btql_filter: z6.string().optional(),
|
|
2204
2653
|
automation_btql_filter: z6.string().optional()
|
|
@@ -2779,7 +3228,12 @@ var ModelParams = z6.union([
|
|
|
2779
3228
|
}).partial().passthrough()
|
|
2780
3229
|
]);
|
|
2781
3230
|
var PromptOptionsNullish = z6.union([
|
|
2782
|
-
z6.object({
|
|
3231
|
+
z6.object({
|
|
3232
|
+
model: z6.string(),
|
|
3233
|
+
params: ModelParams,
|
|
3234
|
+
position: z6.string(),
|
|
3235
|
+
endpoint_name: z6.union([z6.string(), z6.null()])
|
|
3236
|
+
}).partial(),
|
|
2783
3237
|
z6.null()
|
|
2784
3238
|
]);
|
|
2785
3239
|
var PromptParserNullish = z6.union([
|
|
@@ -2793,11 +3247,25 @@ var PromptParserNullish = z6.union([
|
|
|
2793
3247
|
}),
|
|
2794
3248
|
z6.null()
|
|
2795
3249
|
]);
|
|
3250
|
+
var PreprocessorSavedFunctionId = z6.union([
|
|
3251
|
+
z6.object({
|
|
3252
|
+
type: z6.literal("function"),
|
|
3253
|
+
id: z6.string(),
|
|
3254
|
+
version: z6.string().optional()
|
|
3255
|
+
}),
|
|
3256
|
+
z6.object({
|
|
3257
|
+
type: z6.literal("global"),
|
|
3258
|
+
name: z6.string(),
|
|
3259
|
+
function_type: z6.literal("preprocessor").optional().default("preprocessor")
|
|
3260
|
+
}),
|
|
3261
|
+
z6.null()
|
|
3262
|
+
]);
|
|
2796
3263
|
var PromptDataNullish = z6.union([
|
|
2797
3264
|
z6.object({
|
|
2798
3265
|
prompt: PromptBlockDataNullish,
|
|
2799
3266
|
options: PromptOptionsNullish,
|
|
2800
3267
|
parser: PromptParserNullish,
|
|
3268
|
+
preprocessor: PreprocessorSavedFunctionId,
|
|
2801
3269
|
tool_functions: z6.union([z6.array(SavedFunctionId), z6.null()]),
|
|
2802
3270
|
template_format: z6.union([
|
|
2803
3271
|
z6.enum(["mustache", "nunjucks", "none"]),
|
|
@@ -2999,6 +3467,7 @@ var PromptData = z6.object({
|
|
|
2999
3467
|
prompt: PromptBlockDataNullish,
|
|
3000
3468
|
options: PromptOptionsNullish,
|
|
3001
3469
|
parser: PromptParserNullish,
|
|
3470
|
+
preprocessor: PreprocessorSavedFunctionId,
|
|
3002
3471
|
tool_functions: z6.union([z6.array(SavedFunctionId), z6.null()]),
|
|
3003
3472
|
template_format: z6.union([
|
|
3004
3473
|
z6.enum(["mustache", "nunjucks", "none"]),
|
|
@@ -3161,7 +3630,8 @@ var InvokeFunction = FunctionId.and(
|
|
|
3161
3630
|
mode: StreamingMode,
|
|
3162
3631
|
strict: z6.union([z6.boolean(), z6.null()]),
|
|
3163
3632
|
mcp_auth: z6.record(z6.object({ oauth_token: z6.string() }).partial()),
|
|
3164
|
-
overrides: z6.union([z6.object({}).partial().passthrough(), z6.null()])
|
|
3633
|
+
overrides: z6.union([z6.object({}).partial().passthrough(), z6.null()]),
|
|
3634
|
+
endpoint_name: z6.union([z6.string(), z6.null()])
|
|
3165
3635
|
}).partial()
|
|
3166
3636
|
);
|
|
3167
3637
|
var MCPServer = z6.object({
|
|
@@ -3225,6 +3695,24 @@ var Organization = z6.object({
|
|
|
3225
3695
|
created: z6.union([z6.string(), z6.null()]).optional(),
|
|
3226
3696
|
image_rendering_mode: ImageRenderingMode.optional()
|
|
3227
3697
|
});
|
|
3698
|
+
var RetentionObjectType = z6.enum([
|
|
3699
|
+
"project_logs",
|
|
3700
|
+
"experiment",
|
|
3701
|
+
"dataset"
|
|
3702
|
+
]);
|
|
3703
|
+
var OrgAutomation = z6.object({
|
|
3704
|
+
id: z6.string().uuid(),
|
|
3705
|
+
org_id: z6.string().uuid(),
|
|
3706
|
+
user_id: z6.union([z6.string(), z6.null()]).optional(),
|
|
3707
|
+
created: z6.union([z6.string(), z6.null()]).optional(),
|
|
3708
|
+
name: z6.string(),
|
|
3709
|
+
description: z6.union([z6.string(), z6.null()]).optional(),
|
|
3710
|
+
config: z6.object({
|
|
3711
|
+
event_type: z6.literal("retention"),
|
|
3712
|
+
object_type: RetentionObjectType,
|
|
3713
|
+
retention_days: z6.number().int().gte(0)
|
|
3714
|
+
})
|
|
3715
|
+
});
|
|
3228
3716
|
var ProjectSettings = z6.union([
|
|
3229
3717
|
z6.object({
|
|
3230
3718
|
comparison_key: z6.union([z6.string(), z6.null()]),
|
|
@@ -3251,6 +3739,7 @@ var ProjectSettings = z6.union([
|
|
|
3251
3739
|
z6.null()
|
|
3252
3740
|
]),
|
|
3253
3741
|
disable_realtime_queries: z6.union([z6.boolean(), z6.null()]),
|
|
3742
|
+
monitor_charts_use_metrics_start: z6.union([z6.boolean(), z6.null()]),
|
|
3254
3743
|
default_preprocessor: NullableSavedFunctionId
|
|
3255
3744
|
}).partial(),
|
|
3256
3745
|
z6.null()
|
|
@@ -3265,11 +3754,6 @@ var Project = z6.object({
|
|
|
3265
3754
|
user_id: z6.union([z6.string(), z6.null()]).optional(),
|
|
3266
3755
|
settings: ProjectSettings.optional()
|
|
3267
3756
|
});
|
|
3268
|
-
var RetentionObjectType = z6.enum([
|
|
3269
|
-
"project_logs",
|
|
3270
|
-
"experiment",
|
|
3271
|
-
"dataset"
|
|
3272
|
-
]);
|
|
3273
3757
|
var TopicAutomationFacetModel = z6.union([
|
|
3274
3758
|
z6.enum(["brain-facet-latest", "brain-facet-1", "brain-facet-2"]),
|
|
3275
3759
|
z6.null()
|
|
@@ -3380,7 +3864,7 @@ var ProjectAutomation = z6.object({
|
|
|
3380
3864
|
z6.object({
|
|
3381
3865
|
event_type: z6.literal("retention"),
|
|
3382
3866
|
object_type: RetentionObjectType,
|
|
3383
|
-
retention_days: z6.number().gte(0)
|
|
3867
|
+
retention_days: z6.number().int().gte(0)
|
|
3384
3868
|
}),
|
|
3385
3869
|
z6.object({
|
|
3386
3870
|
event_type: z6.literal("environment_update"),
|
|
@@ -3552,7 +4036,12 @@ var Prompt = z6.object({
|
|
|
3552
4036
|
metadata: z6.union([z6.object({}).partial().passthrough(), z6.null()]).optional(),
|
|
3553
4037
|
function_type: FunctionTypeEnumNullish.optional()
|
|
3554
4038
|
});
|
|
3555
|
-
var PromptOptions = z6.object({
|
|
4039
|
+
var PromptOptions = z6.object({
|
|
4040
|
+
model: z6.string(),
|
|
4041
|
+
params: ModelParams,
|
|
4042
|
+
position: z6.string(),
|
|
4043
|
+
endpoint_name: z6.union([z6.string(), z6.null()])
|
|
4044
|
+
}).partial();
|
|
3556
4045
|
var PromptSessionEvent = z6.object({
|
|
3557
4046
|
id: z6.string(),
|
|
3558
4047
|
_xact_id: z6.string(),
|
|
@@ -3635,7 +4124,8 @@ var RunEval = z6.object({
|
|
|
3635
4124
|
stop_token: z6.union([z6.string(), z6.null()]).optional(),
|
|
3636
4125
|
extra_messages: z6.string().optional(),
|
|
3637
4126
|
tags: z6.array(z6.string()).optional(),
|
|
3638
|
-
mcp_auth: z6.record(z6.object({ oauth_token: z6.string() }).partial()).optional()
|
|
4127
|
+
mcp_auth: z6.record(z6.object({ oauth_token: z6.string() }).partial()).optional(),
|
|
4128
|
+
endpoint_name: z6.union([z6.string(), z6.null()]).optional()
|
|
3639
4129
|
});
|
|
3640
4130
|
var ServiceToken = z6.object({
|
|
3641
4131
|
id: z6.string().uuid(),
|
|
@@ -3645,7 +4135,8 @@ var ServiceToken = z6.object({
|
|
|
3645
4135
|
service_account_id: z6.union([z6.string(), z6.null()]).optional(),
|
|
3646
4136
|
service_account_email: z6.union([z6.string(), z6.null()]).optional(),
|
|
3647
4137
|
service_account_name: z6.union([z6.string(), z6.null()]).optional(),
|
|
3648
|
-
org_id: z6.union([z6.string(), z6.null()]).optional()
|
|
4138
|
+
org_id: z6.union([z6.string(), z6.null()]).optional(),
|
|
4139
|
+
expires_at: z6.union([z6.string(), z6.null()]).optional()
|
|
3649
4140
|
});
|
|
3650
4141
|
var SpanIFrame = z6.object({
|
|
3651
4142
|
id: z6.string().uuid(),
|
|
@@ -3767,6 +4258,13 @@ var ViewOptions = z6.union([
|
|
|
3767
4258
|
}),
|
|
3768
4259
|
z6.null()
|
|
3769
4260
|
]),
|
|
4261
|
+
pointSizeMetric: z6.union([
|
|
4262
|
+
z6.object({
|
|
4263
|
+
type: z6.enum(["none", "score", "metric", "metadata"]),
|
|
4264
|
+
value: z6.string()
|
|
4265
|
+
}),
|
|
4266
|
+
z6.null()
|
|
4267
|
+
]),
|
|
3770
4268
|
xAxisAggregation: z6.union([z6.string(), z6.null()]),
|
|
3771
4269
|
chartAnnotations: z6.union([
|
|
3772
4270
|
z6.array(z6.object({ id: z6.string(), text: z6.string() })),
|
|
@@ -4882,6 +5380,8 @@ var INSTRUMENTATION_NAMES = {
|
|
|
4882
5380
|
BEDROCK_RUNTIME: "bedrock-runtime",
|
|
4883
5381
|
BRAINTRUST_JS_LOGGER: "braintrust-js-logger",
|
|
4884
5382
|
CLAUDE_AGENT_SDK: "claude-agent-sdk",
|
|
5383
|
+
CLOUDFLARE_AI_CHAT: "cloudflare-ai-chat",
|
|
5384
|
+
CLOUDFLARE_AGENTS: "cloudflare-agents",
|
|
4885
5385
|
COHERE: "cohere",
|
|
4886
5386
|
CURSOR_SDK: "cursor-sdk",
|
|
4887
5387
|
EVE: "eve",
|
|
@@ -4907,7 +5407,7 @@ var INSTRUMENTATION_NAMES = {
|
|
|
4907
5407
|
var INTERNAL_SPAN_INSTRUMENTATION_NAME = /* @__PURE__ */ Symbol.for(
|
|
4908
5408
|
"braintrust.spanInstrumentationName"
|
|
4909
5409
|
);
|
|
4910
|
-
var SDK_VERSION = true ? "3.
|
|
5410
|
+
var SDK_VERSION = true ? "3.26.0" : "0.0.0";
|
|
4911
5411
|
function withSpanInstrumentationName(args, instrumentationName) {
|
|
4912
5412
|
return {
|
|
4913
5413
|
...args,
|
|
@@ -5102,12 +5602,13 @@ function applyMaskingToField(maskingFunction, data, fieldName) {
|
|
|
5102
5602
|
var INITIAL_SPAN_WRITE_AS_MERGE = /* @__PURE__ */ Symbol(
|
|
5103
5603
|
"braintrust.initial-span-write-as-merge"
|
|
5104
5604
|
);
|
|
5605
|
+
var INTERNAL_SPAN_CONTEXT = /* @__PURE__ */ Symbol("braintrust.internal-span-context");
|
|
5105
5606
|
var BRAINTRUST_CURRENT_SPAN_STORE = /* @__PURE__ */ Symbol.for(
|
|
5106
5607
|
"braintrust.currentSpanStore"
|
|
5107
5608
|
);
|
|
5108
5609
|
var ContextManager = class {
|
|
5109
5610
|
/**
|
|
5110
|
-
* Returns the value to store in the ALS bound to a
|
|
5611
|
+
* Returns the value to store in the ALS bound to a global hook's start event.
|
|
5111
5612
|
* In default mode this is the Span itself; in OTEL mode it is the OTEL Context
|
|
5112
5613
|
* containing the span so that OTEL's own ALS stores a proper Context object.
|
|
5113
5614
|
*/
|
|
@@ -7651,6 +8152,12 @@ function _internalStartSpanWithInitialMerge(args) {
|
|
|
7651
8152
|
[INITIAL_SPAN_WRITE_AS_MERGE]: true
|
|
7652
8153
|
}).span;
|
|
7653
8154
|
}
|
|
8155
|
+
function _internalStartSpanWithContext(args, context) {
|
|
8156
|
+
return startSpanAndIsLogger({
|
|
8157
|
+
...args,
|
|
8158
|
+
[INTERNAL_SPAN_CONTEXT]: context
|
|
8159
|
+
}).span;
|
|
8160
|
+
}
|
|
7654
8161
|
async function flush(options) {
|
|
7655
8162
|
const state = options?.state ?? _globalState;
|
|
7656
8163
|
return await state.bgLogger().flush();
|
|
@@ -8444,7 +8951,7 @@ var SpanImpl = class _SpanImpl {
|
|
|
8444
8951
|
start: args.startTime ?? getCurrentUnixTimestamp()
|
|
8445
8952
|
},
|
|
8446
8953
|
context: mergeSpanOriginContext(
|
|
8447
|
-
{ ...callerLocation },
|
|
8954
|
+
{ ...callerLocation, ...args[INTERNAL_SPAN_CONTEXT] },
|
|
8448
8955
|
instrumentationName,
|
|
8449
8956
|
this._state.spanOriginEnvironment
|
|
8450
8957
|
),
|
|
@@ -20303,11 +20810,11 @@ function summarizeEmbedContentOutput(response) {
|
|
|
20303
20810
|
};
|
|
20304
20811
|
}
|
|
20305
20812
|
function populateUsageMetrics(metrics, usage) {
|
|
20306
|
-
if (usage.promptTokenCount !== void 0) {
|
|
20307
|
-
metrics.prompt_tokens = usage.promptTokenCount;
|
|
20813
|
+
if (usage.promptTokenCount !== void 0 || usage.toolUsePromptTokenCount !== void 0) {
|
|
20814
|
+
metrics.prompt_tokens = (usage.promptTokenCount ?? 0) + (usage.toolUsePromptTokenCount ?? 0);
|
|
20308
20815
|
}
|
|
20309
|
-
if (usage.candidatesTokenCount !== void 0) {
|
|
20310
|
-
metrics.completion_tokens = usage.candidatesTokenCount;
|
|
20816
|
+
if (usage.candidatesTokenCount !== void 0 || usage.thoughtsTokenCount !== void 0) {
|
|
20817
|
+
metrics.completion_tokens = (usage.candidatesTokenCount ?? 0) + (usage.thoughtsTokenCount ?? 0);
|
|
20311
20818
|
}
|
|
20312
20819
|
if (usage.totalTokenCount !== void 0) {
|
|
20313
20820
|
metrics.tokens = usage.totalTokenCount;
|
|
@@ -20318,13 +20825,26 @@ function populateUsageMetrics(metrics, usage) {
|
|
|
20318
20825
|
if (usage.thoughtsTokenCount !== void 0) {
|
|
20319
20826
|
metrics.completion_reasoning_tokens = usage.thoughtsTokenCount;
|
|
20320
20827
|
}
|
|
20828
|
+
for (const detail of usage.promptTokensDetails ?? []) {
|
|
20829
|
+
if (detail.modality === "AUDIO" && detail.tokenCount !== void 0) {
|
|
20830
|
+
metrics.prompt_audio_tokens = (metrics.prompt_audio_tokens ?? 0) + detail.tokenCount;
|
|
20831
|
+
}
|
|
20832
|
+
}
|
|
20833
|
+
for (const detail of usage.candidatesTokensDetails ?? []) {
|
|
20834
|
+
if (detail.modality === "AUDIO" && detail.tokenCount !== void 0) {
|
|
20835
|
+
metrics.completion_audio_tokens = (metrics.completion_audio_tokens ?? 0) + detail.tokenCount;
|
|
20836
|
+
}
|
|
20837
|
+
if (detail.modality === "IMAGE" && detail.tokenCount !== void 0) {
|
|
20838
|
+
metrics.completion_image_tokens = (metrics.completion_image_tokens ?? 0) + detail.tokenCount;
|
|
20839
|
+
}
|
|
20840
|
+
}
|
|
20321
20841
|
}
|
|
20322
20842
|
function populateInteractionUsageMetrics(metrics, usage) {
|
|
20323
20843
|
if (typeof usage.total_input_tokens === "number") {
|
|
20324
20844
|
metrics.prompt_tokens = usage.total_input_tokens;
|
|
20325
20845
|
}
|
|
20326
20846
|
if (typeof usage.total_output_tokens === "number") {
|
|
20327
|
-
metrics.completion_tokens = usage.total_output_tokens;
|
|
20847
|
+
metrics.completion_tokens = usage.total_output_tokens + (typeof usage.total_thought_tokens === "number" ? usage.total_thought_tokens : 0);
|
|
20328
20848
|
}
|
|
20329
20849
|
if (typeof usage.total_tokens === "number") {
|
|
20330
20850
|
metrics.tokens = usage.total_tokens;
|
|
@@ -20335,6 +20855,24 @@ function populateInteractionUsageMetrics(metrics, usage) {
|
|
|
20335
20855
|
if (typeof usage.total_thought_tokens === "number") {
|
|
20336
20856
|
metrics.completion_reasoning_tokens = usage.total_thought_tokens;
|
|
20337
20857
|
}
|
|
20858
|
+
for (const detail of Array.isArray(usage.input_tokens_by_modality) ? usage.input_tokens_by_modality : []) {
|
|
20859
|
+
if (typeof detail?.modality === "string" && detail.modality.toLowerCase() === "audio" && typeof detail.tokens === "number") {
|
|
20860
|
+
metrics.prompt_audio_tokens = (metrics.prompt_audio_tokens ?? 0) + detail.tokens;
|
|
20861
|
+
}
|
|
20862
|
+
}
|
|
20863
|
+
for (const detail of Array.isArray(usage.output_tokens_by_modality) ? usage.output_tokens_by_modality : []) {
|
|
20864
|
+
if (typeof detail?.modality !== "string" || typeof detail.tokens !== "number") {
|
|
20865
|
+
continue;
|
|
20866
|
+
}
|
|
20867
|
+
switch (detail.modality.toLowerCase()) {
|
|
20868
|
+
case "audio":
|
|
20869
|
+
metrics.completion_audio_tokens = (metrics.completion_audio_tokens ?? 0) + detail.tokens;
|
|
20870
|
+
break;
|
|
20871
|
+
case "image":
|
|
20872
|
+
metrics.completion_image_tokens = (metrics.completion_image_tokens ?? 0) + detail.tokens;
|
|
20873
|
+
break;
|
|
20874
|
+
}
|
|
20875
|
+
}
|
|
20338
20876
|
}
|
|
20339
20877
|
function aggregateGenerateContentChunks(chunks, startTime, firstTokenTime) {
|
|
20340
20878
|
const end = getCurrentUnixTimestamp();
|
|
@@ -21073,6 +21611,299 @@ function extractTextGenerationStreamMetadata(chunks) {
|
|
|
21073
21611
|
return void 0;
|
|
21074
21612
|
}
|
|
21075
21613
|
|
|
21614
|
+
// src/instrumentation/plugins/huggingface-transformers-channels.ts
|
|
21615
|
+
var SUPPORTED_TASKS = /* @__PURE__ */ new Set([
|
|
21616
|
+
"text-generation",
|
|
21617
|
+
"text2text-generation",
|
|
21618
|
+
"summarization",
|
|
21619
|
+
"feature-extraction",
|
|
21620
|
+
"question-answering"
|
|
21621
|
+
]);
|
|
21622
|
+
var pipelineInfo = /* @__PURE__ */ new WeakMap();
|
|
21623
|
+
function isSupportedHuggingFaceTransformersTask(task) {
|
|
21624
|
+
return typeof task === "string" && SUPPORTED_TASKS.has(task);
|
|
21625
|
+
}
|
|
21626
|
+
function registerHuggingFaceTransformersPipeline(pipeline, task, model) {
|
|
21627
|
+
if (typeof task !== "string") {
|
|
21628
|
+
return;
|
|
21629
|
+
}
|
|
21630
|
+
const info = { task };
|
|
21631
|
+
if (typeof model === "string") {
|
|
21632
|
+
info.model = model;
|
|
21633
|
+
}
|
|
21634
|
+
pipelineInfo.set(pipeline, info);
|
|
21635
|
+
}
|
|
21636
|
+
function getHuggingFaceTransformersPipelineInfo(pipeline) {
|
|
21637
|
+
return pipeline ? pipelineInfo.get(pipeline) : void 0;
|
|
21638
|
+
}
|
|
21639
|
+
var huggingFaceTransformersChannels = defineChannels(
|
|
21640
|
+
"@huggingface/transformers",
|
|
21641
|
+
{
|
|
21642
|
+
pipeline: channel({
|
|
21643
|
+
channelName: "pipeline",
|
|
21644
|
+
kind: "async"
|
|
21645
|
+
}),
|
|
21646
|
+
pipelineCall: channel({
|
|
21647
|
+
channelName: "pipeline.call",
|
|
21648
|
+
kind: "async"
|
|
21649
|
+
})
|
|
21650
|
+
},
|
|
21651
|
+
{ instrumentationName: INSTRUMENTATION_NAMES.HUGGINGFACE }
|
|
21652
|
+
);
|
|
21653
|
+
|
|
21654
|
+
// src/instrumentation/plugins/huggingface-transformers-plugin.ts
|
|
21655
|
+
var REQUEST_METADATA_KEYS = [
|
|
21656
|
+
"temperature",
|
|
21657
|
+
"top_p",
|
|
21658
|
+
"max_tokens",
|
|
21659
|
+
"stop"
|
|
21660
|
+
];
|
|
21661
|
+
var HuggingFaceTransformersPlugin = class extends BasePlugin {
|
|
21662
|
+
onEnable() {
|
|
21663
|
+
this.subscribeToPipelineFactory();
|
|
21664
|
+
this.unsubscribers.push(
|
|
21665
|
+
traceAsyncChannel(huggingFaceTransformersChannels.pipelineCall, {
|
|
21666
|
+
name: (_args, event) => {
|
|
21667
|
+
const task = getTask(event);
|
|
21668
|
+
const operation = task?.replaceAll("-", "_") ?? "unknown";
|
|
21669
|
+
return `huggingface.transformers.${operation}`;
|
|
21670
|
+
},
|
|
21671
|
+
type: "llm" /* LLM */,
|
|
21672
|
+
shouldTrace: (_args, event) => isSupportedHuggingFaceTransformersTask(
|
|
21673
|
+
getTask(event)
|
|
21674
|
+
),
|
|
21675
|
+
extractInput: (args, event) => ({
|
|
21676
|
+
input: extractInput(
|
|
21677
|
+
getTask(event),
|
|
21678
|
+
args
|
|
21679
|
+
),
|
|
21680
|
+
metadata: extractMetadata(
|
|
21681
|
+
event,
|
|
21682
|
+
args
|
|
21683
|
+
)
|
|
21684
|
+
}),
|
|
21685
|
+
extractOutput: (result, event) => extractOutput(
|
|
21686
|
+
getTask(event),
|
|
21687
|
+
result
|
|
21688
|
+
),
|
|
21689
|
+
extractMetrics: () => ({})
|
|
21690
|
+
})
|
|
21691
|
+
);
|
|
21692
|
+
}
|
|
21693
|
+
onDisable() {
|
|
21694
|
+
this.unsubscribers = unsubscribeAll(this.unsubscribers);
|
|
21695
|
+
}
|
|
21696
|
+
subscribeToPipelineFactory() {
|
|
21697
|
+
const channel2 = huggingFaceTransformersChannels.pipeline.tracingChannel();
|
|
21698
|
+
const handlers = {
|
|
21699
|
+
asyncEnd: (event) => {
|
|
21700
|
+
if (typeof event.result !== "function") {
|
|
21701
|
+
return;
|
|
21702
|
+
}
|
|
21703
|
+
registerHuggingFaceTransformersPipeline(
|
|
21704
|
+
event.result,
|
|
21705
|
+
event.arguments?.[0],
|
|
21706
|
+
event.arguments?.[1]
|
|
21707
|
+
);
|
|
21708
|
+
}
|
|
21709
|
+
};
|
|
21710
|
+
channel2.subscribe(handlers);
|
|
21711
|
+
this.unsubscribers.push(() => channel2.unsubscribe(handlers));
|
|
21712
|
+
}
|
|
21713
|
+
};
|
|
21714
|
+
function getTask(event) {
|
|
21715
|
+
const self = event.self;
|
|
21716
|
+
const registeredTask = getHuggingFaceTransformersPipelineInfo(self)?.task;
|
|
21717
|
+
if (registeredTask !== void 0) {
|
|
21718
|
+
return registeredTask;
|
|
21719
|
+
}
|
|
21720
|
+
if (typeof self?.task === "string") {
|
|
21721
|
+
return self.task;
|
|
21722
|
+
}
|
|
21723
|
+
return void 0;
|
|
21724
|
+
}
|
|
21725
|
+
function extractMetadata(event, args) {
|
|
21726
|
+
const metadata = {
|
|
21727
|
+
provider: "huggingface"
|
|
21728
|
+
};
|
|
21729
|
+
const registeredModel = getHuggingFaceTransformersPipelineInfo(
|
|
21730
|
+
event.self
|
|
21731
|
+
)?.model;
|
|
21732
|
+
const model = registeredModel ?? modelIdentifier(event.self);
|
|
21733
|
+
if (model) {
|
|
21734
|
+
metadata.model = model;
|
|
21735
|
+
}
|
|
21736
|
+
const task = getTask(event);
|
|
21737
|
+
const options = task === "question-answering" ? args[2] : args[1];
|
|
21738
|
+
if (isObject(options)) {
|
|
21739
|
+
for (const key of REQUEST_METADATA_KEYS) {
|
|
21740
|
+
if (options[key] !== void 0) {
|
|
21741
|
+
metadata[key] = options[key];
|
|
21742
|
+
}
|
|
21743
|
+
}
|
|
21744
|
+
}
|
|
21745
|
+
return metadata;
|
|
21746
|
+
}
|
|
21747
|
+
function modelIdentifier(pipeline) {
|
|
21748
|
+
const model = pipeline?.model;
|
|
21749
|
+
if (!isObject(model)) {
|
|
21750
|
+
return void 0;
|
|
21751
|
+
}
|
|
21752
|
+
const config = isObject(model.config) ? model.config : void 0;
|
|
21753
|
+
for (const value of [
|
|
21754
|
+
config?._name_or_path,
|
|
21755
|
+
config?.name_or_path,
|
|
21756
|
+
config?.model_id,
|
|
21757
|
+
config?.modelId,
|
|
21758
|
+
model.name
|
|
21759
|
+
]) {
|
|
21760
|
+
if (typeof value === "string" && value.length > 0) {
|
|
21761
|
+
return value;
|
|
21762
|
+
}
|
|
21763
|
+
}
|
|
21764
|
+
return void 0;
|
|
21765
|
+
}
|
|
21766
|
+
function extractInput(task, args) {
|
|
21767
|
+
switch (task) {
|
|
21768
|
+
case "feature-extraction":
|
|
21769
|
+
return args[0];
|
|
21770
|
+
case "question-answering": {
|
|
21771
|
+
const question = args[0];
|
|
21772
|
+
const context = args[1];
|
|
21773
|
+
if (Array.isArray(question) && Array.isArray(context) && question.every((value) => typeof value === "string") && context.every((value) => typeof value === "string")) {
|
|
21774
|
+
return question.map((value, index) => [
|
|
21775
|
+
{
|
|
21776
|
+
role: "user",
|
|
21777
|
+
content: `Context:
|
|
21778
|
+
${context[index] ?? ""}
|
|
21779
|
+
|
|
21780
|
+
Question:
|
|
21781
|
+
${value}`
|
|
21782
|
+
}
|
|
21783
|
+
]);
|
|
21784
|
+
}
|
|
21785
|
+
if (typeof question === "string" && typeof context === "string") {
|
|
21786
|
+
return [
|
|
21787
|
+
{
|
|
21788
|
+
role: "user",
|
|
21789
|
+
content: `Context:
|
|
21790
|
+
${context}
|
|
21791
|
+
|
|
21792
|
+
Question:
|
|
21793
|
+
${question}`
|
|
21794
|
+
}
|
|
21795
|
+
];
|
|
21796
|
+
}
|
|
21797
|
+
return { context, question };
|
|
21798
|
+
}
|
|
21799
|
+
}
|
|
21800
|
+
const input = args[0];
|
|
21801
|
+
if (task === "text-generation" && isChat(input)) {
|
|
21802
|
+
return input;
|
|
21803
|
+
}
|
|
21804
|
+
if (task === "text-generation" && Array.isArray(input) && input.every(isChat)) {
|
|
21805
|
+
return input;
|
|
21806
|
+
}
|
|
21807
|
+
if (typeof input === "string") {
|
|
21808
|
+
return [{ role: "user", content: input }];
|
|
21809
|
+
}
|
|
21810
|
+
if (Array.isArray(input) && input.every((value) => typeof value === "string")) {
|
|
21811
|
+
return input.map((value) => [
|
|
21812
|
+
{
|
|
21813
|
+
role: "user",
|
|
21814
|
+
content: value
|
|
21815
|
+
}
|
|
21816
|
+
]);
|
|
21817
|
+
}
|
|
21818
|
+
return input;
|
|
21819
|
+
}
|
|
21820
|
+
function isChat(value) {
|
|
21821
|
+
return Array.isArray(value) && value.length > 0 && value.every(
|
|
21822
|
+
(message) => isObject(message) && typeof message.role === "string" && "content" in message
|
|
21823
|
+
);
|
|
21824
|
+
}
|
|
21825
|
+
function extractOutput(task, result) {
|
|
21826
|
+
switch (task) {
|
|
21827
|
+
case "feature-extraction":
|
|
21828
|
+
return summarizeEmbedding(result);
|
|
21829
|
+
case "question-answering":
|
|
21830
|
+
return choicesFromAnswers(result);
|
|
21831
|
+
default:
|
|
21832
|
+
return choicesFromGenerations(result);
|
|
21833
|
+
}
|
|
21834
|
+
}
|
|
21835
|
+
function summarizeEmbedding(result) {
|
|
21836
|
+
if (!isObject(result) || !Array.isArray(result.dims)) {
|
|
21837
|
+
return void 0;
|
|
21838
|
+
}
|
|
21839
|
+
const { dims } = result;
|
|
21840
|
+
if (dims.length === 0 || !dims.every((dimension) => typeof dimension === "number")) {
|
|
21841
|
+
return void 0;
|
|
21842
|
+
}
|
|
21843
|
+
if (dims.length === 1) {
|
|
21844
|
+
return { embedding_length: dims[0] };
|
|
21845
|
+
}
|
|
21846
|
+
if (dims.length === 2) {
|
|
21847
|
+
return {
|
|
21848
|
+
embedding_count: dims[0],
|
|
21849
|
+
embedding_length: dims[1]
|
|
21850
|
+
};
|
|
21851
|
+
}
|
|
21852
|
+
return {
|
|
21853
|
+
embedding_batch_count: dims[0],
|
|
21854
|
+
embedding_count: dims[1],
|
|
21855
|
+
embedding_length: dims.at(-1) ?? 0
|
|
21856
|
+
};
|
|
21857
|
+
}
|
|
21858
|
+
function choicesFromAnswers(result) {
|
|
21859
|
+
const answers = Array.isArray(result) ? result.flat() : [result];
|
|
21860
|
+
const choices = answers.flatMap((answer, index) => {
|
|
21861
|
+
if (!isObject(answer) || typeof answer.answer !== "string") {
|
|
21862
|
+
return [];
|
|
21863
|
+
}
|
|
21864
|
+
return [
|
|
21865
|
+
{
|
|
21866
|
+
index,
|
|
21867
|
+
finish_reason: "stop",
|
|
21868
|
+
message: {
|
|
21869
|
+
role: "assistant",
|
|
21870
|
+
content: answer.answer
|
|
21871
|
+
}
|
|
21872
|
+
}
|
|
21873
|
+
];
|
|
21874
|
+
});
|
|
21875
|
+
return choices.length > 0 ? choices : void 0;
|
|
21876
|
+
}
|
|
21877
|
+
function choicesFromGenerations(result) {
|
|
21878
|
+
const generations = Array.isArray(result) ? result.flat() : [result];
|
|
21879
|
+
const choices = generations.flatMap((generation, index) => {
|
|
21880
|
+
if (!isObject(generation)) {
|
|
21881
|
+
return [];
|
|
21882
|
+
}
|
|
21883
|
+
const generated = generation.generated_text ?? generation.summary_text;
|
|
21884
|
+
let content;
|
|
21885
|
+
if (typeof generated === "string") {
|
|
21886
|
+
content = generated;
|
|
21887
|
+
} else if (isChat(generated)) {
|
|
21888
|
+
content = generated.at(-1)?.content;
|
|
21889
|
+
}
|
|
21890
|
+
if (typeof content !== "string") {
|
|
21891
|
+
return [];
|
|
21892
|
+
}
|
|
21893
|
+
return [
|
|
21894
|
+
{
|
|
21895
|
+
index,
|
|
21896
|
+
finish_reason: "stop",
|
|
21897
|
+
message: {
|
|
21898
|
+
role: "assistant",
|
|
21899
|
+
content
|
|
21900
|
+
}
|
|
21901
|
+
}
|
|
21902
|
+
];
|
|
21903
|
+
});
|
|
21904
|
+
return choices.length > 0 ? choices : void 0;
|
|
21905
|
+
}
|
|
21906
|
+
|
|
21076
21907
|
// src/instrumentation/plugins/openrouter-agent-channels.ts
|
|
21077
21908
|
var openRouterAgentChannels = defineChannels(
|
|
21078
21909
|
"@openrouter/agent",
|
|
@@ -24238,15 +25069,32 @@ function aggregateUsageFromEvents(events) {
|
|
|
24238
25069
|
if (usage.thoughtsTokenCount !== void 0) {
|
|
24239
25070
|
aggregated.thoughtsTokenCount = (aggregated.thoughtsTokenCount ?? 0) + usage.thoughtsTokenCount;
|
|
24240
25071
|
}
|
|
25072
|
+
if (usage.toolUsePromptTokenCount !== void 0) {
|
|
25073
|
+
aggregated.toolUsePromptTokenCount = (aggregated.toolUsePromptTokenCount ?? 0) + usage.toolUsePromptTokenCount;
|
|
25074
|
+
}
|
|
25075
|
+
for (const detailsField of [
|
|
25076
|
+
"promptTokensDetails",
|
|
25077
|
+
"cacheTokensDetails",
|
|
25078
|
+
"candidatesTokensDetails",
|
|
25079
|
+
"toolUsePromptTokensDetails"
|
|
25080
|
+
]) {
|
|
25081
|
+
const details = usage[detailsField];
|
|
25082
|
+
if (details !== void 0) {
|
|
25083
|
+
aggregated[detailsField] = [
|
|
25084
|
+
...aggregated[detailsField] ?? [],
|
|
25085
|
+
...details
|
|
25086
|
+
];
|
|
25087
|
+
}
|
|
25088
|
+
}
|
|
24241
25089
|
}
|
|
24242
25090
|
return hasUsage ? aggregated : void 0;
|
|
24243
25091
|
}
|
|
24244
25092
|
function populateUsageMetrics2(metrics, usage) {
|
|
24245
|
-
if (usage.promptTokenCount !== void 0) {
|
|
24246
|
-
metrics.prompt_tokens = usage.promptTokenCount;
|
|
25093
|
+
if (usage.promptTokenCount !== void 0 || usage.toolUsePromptTokenCount !== void 0) {
|
|
25094
|
+
metrics.prompt_tokens = (usage.promptTokenCount ?? 0) + (usage.toolUsePromptTokenCount ?? 0);
|
|
24247
25095
|
}
|
|
24248
|
-
if (usage.candidatesTokenCount !== void 0) {
|
|
24249
|
-
metrics.completion_tokens = usage.candidatesTokenCount;
|
|
25096
|
+
if (usage.candidatesTokenCount !== void 0 || usage.thoughtsTokenCount !== void 0) {
|
|
25097
|
+
metrics.completion_tokens = (usage.candidatesTokenCount ?? 0) + (usage.thoughtsTokenCount ?? 0);
|
|
24250
25098
|
}
|
|
24251
25099
|
if (usage.totalTokenCount !== void 0) {
|
|
24252
25100
|
metrics.tokens = usage.totalTokenCount;
|
|
@@ -24257,6 +25105,19 @@ function populateUsageMetrics2(metrics, usage) {
|
|
|
24257
25105
|
if (usage.thoughtsTokenCount !== void 0) {
|
|
24258
25106
|
metrics.completion_reasoning_tokens = usage.thoughtsTokenCount;
|
|
24259
25107
|
}
|
|
25108
|
+
for (const detail of usage.promptTokensDetails ?? []) {
|
|
25109
|
+
if (detail.modality === "AUDIO" && detail.tokenCount !== void 0) {
|
|
25110
|
+
metrics.prompt_audio_tokens = (metrics.prompt_audio_tokens ?? 0) + detail.tokenCount;
|
|
25111
|
+
}
|
|
25112
|
+
}
|
|
25113
|
+
for (const detail of usage.candidatesTokensDetails ?? []) {
|
|
25114
|
+
if (detail.modality === "AUDIO" && detail.tokenCount !== void 0) {
|
|
25115
|
+
metrics.completion_audio_tokens = (metrics.completion_audio_tokens ?? 0) + detail.tokenCount;
|
|
25116
|
+
}
|
|
25117
|
+
if (detail.modality === "IMAGE" && detail.tokenCount !== void 0) {
|
|
25118
|
+
metrics.completion_image_tokens = (metrics.completion_image_tokens ?? 0) + detail.tokenCount;
|
|
25119
|
+
}
|
|
25120
|
+
}
|
|
24260
25121
|
}
|
|
24261
25122
|
function cleanMetrics4(metrics) {
|
|
24262
25123
|
const cleaned = {};
|
|
@@ -28622,7 +29483,7 @@ var LangSmithPlugin = class extends BasePlugin {
|
|
|
28622
29483
|
const inputs = preferOwnValue(run, previous, "inputs");
|
|
28623
29484
|
const outputs = preferOwnValue(run, previous, "outputs");
|
|
28624
29485
|
const error = errorMessage(preferOwnValue(run, previous, "error"));
|
|
28625
|
-
const metadata =
|
|
29486
|
+
const metadata = extractMetadata2(run, previous);
|
|
28626
29487
|
const tags = extractTags(preferOwnValue(run, previous, "tags"));
|
|
28627
29488
|
const metrics = extractMetrics(run, previous);
|
|
28628
29489
|
span.log({
|
|
@@ -28727,7 +29588,7 @@ function mapRunType(runType) {
|
|
|
28727
29588
|
return "task" /* TASK */;
|
|
28728
29589
|
}
|
|
28729
29590
|
}
|
|
28730
|
-
function
|
|
29591
|
+
function extractMetadata2(run, previous) {
|
|
28731
29592
|
const extra = preferOwnValue(run, previous, "extra");
|
|
28732
29593
|
const rawMetadata = ownValue(extra, "metadata");
|
|
28733
29594
|
if (!isRecord2(rawMetadata)) {
|
|
@@ -30100,6 +30961,15 @@ function handleMultiAgentStreamEvent(state, event, activeChildParents) {
|
|
|
30100
30961
|
logInstrumentationError5("Strands Agent SDK multi-agent event", error);
|
|
30101
30962
|
}
|
|
30102
30963
|
}
|
|
30964
|
+
function buildModelSpanInput(event, attachmentCache) {
|
|
30965
|
+
const systemPrompt = typeof event.agent?.systemPrompt === "string" && event.agent.systemPrompt.length > 0 ? event.agent.systemPrompt : void 0;
|
|
30966
|
+
const processedMessages = Array.isArray(event.agent?.messages) ? processStrandsInputAttachments(event.agent.messages, attachmentCache) : void 0;
|
|
30967
|
+
if (!systemPrompt) {
|
|
30968
|
+
return processedMessages;
|
|
30969
|
+
}
|
|
30970
|
+
const systemMessage = { role: "system", content: systemPrompt };
|
|
30971
|
+
return Array.isArray(processedMessages) ? [systemMessage, ...processedMessages] : [systemMessage];
|
|
30972
|
+
}
|
|
30103
30973
|
function startModelSpan(state, event) {
|
|
30104
30974
|
if (state.activeModel) {
|
|
30105
30975
|
finalizeModelSpan(state);
|
|
@@ -30117,10 +30987,7 @@ function startModelSpan(state, event) {
|
|
|
30117
30987
|
withSpanInstrumentationName(
|
|
30118
30988
|
{
|
|
30119
30989
|
event: {
|
|
30120
|
-
input:
|
|
30121
|
-
event.agent.messages,
|
|
30122
|
-
state.attachmentCache
|
|
30123
|
-
) : void 0,
|
|
30990
|
+
input: buildModelSpanInput(event, state.attachmentCache),
|
|
30124
30991
|
metadata
|
|
30125
30992
|
},
|
|
30126
30993
|
name: formatModelSpanName(model),
|
|
@@ -30760,6 +31627,532 @@ function logInstrumentationError5(context, error) {
|
|
|
30760
31627
|
debugLogger.debug(`${context}:`, error);
|
|
30761
31628
|
}
|
|
30762
31629
|
|
|
31630
|
+
// src/instrumentation/plugins/cloudflare-ai-chat-channels.ts
|
|
31631
|
+
var cloudflareAIChatChannels = defineChannels(
|
|
31632
|
+
"@cloudflare/ai-chat",
|
|
31633
|
+
{
|
|
31634
|
+
runExclusiveChatTurn: channel({
|
|
31635
|
+
channelName: "AIChatAgent._runExclusiveChatTurn",
|
|
31636
|
+
kind: "async"
|
|
31637
|
+
}),
|
|
31638
|
+
onChatResponse: channel({
|
|
31639
|
+
channelName: "AIChatAgent.onChatResponse",
|
|
31640
|
+
kind: "sync-stream"
|
|
31641
|
+
})
|
|
31642
|
+
},
|
|
31643
|
+
{ instrumentationName: INSTRUMENTATION_NAMES.CLOUDFLARE_AI_CHAT }
|
|
31644
|
+
);
|
|
31645
|
+
|
|
31646
|
+
// src/instrumentation/plugins/cloudflare-ai-chat-instrumentation.ts
|
|
31647
|
+
var wrappedResponseHooks = /* @__PURE__ */ new WeakSet();
|
|
31648
|
+
function instrumentCloudflareAIChatResponseHook(agent) {
|
|
31649
|
+
try {
|
|
31650
|
+
const original = Reflect.get(agent, "onChatResponse");
|
|
31651
|
+
if (typeof original !== "function" || wrappedResponseHooks.has(original)) {
|
|
31652
|
+
return;
|
|
31653
|
+
}
|
|
31654
|
+
const wrapped = function(result) {
|
|
31655
|
+
const args = [result];
|
|
31656
|
+
return cloudflareAIChatChannels.onChatResponse.traceSync(
|
|
31657
|
+
() => Reflect.apply(original, this, args),
|
|
31658
|
+
{
|
|
31659
|
+
arguments: args,
|
|
31660
|
+
self: this
|
|
31661
|
+
}
|
|
31662
|
+
);
|
|
31663
|
+
};
|
|
31664
|
+
wrappedResponseHooks.add(wrapped);
|
|
31665
|
+
defineWrappedMethod(agent, "onChatResponse", original, wrapped);
|
|
31666
|
+
} catch (error) {
|
|
31667
|
+
debugLogger.debug(
|
|
31668
|
+
"Failed to wrap @cloudflare/ai-chat response hook:",
|
|
31669
|
+
error
|
|
31670
|
+
);
|
|
31671
|
+
}
|
|
31672
|
+
}
|
|
31673
|
+
function defineWrappedMethod(target, property, original, wrapped) {
|
|
31674
|
+
const ownDescriptor = Object.getOwnPropertyDescriptor(target, property);
|
|
31675
|
+
Object.defineProperty(target, property, {
|
|
31676
|
+
configurable: ownDescriptor?.configurable ?? true,
|
|
31677
|
+
enumerable: ownDescriptor?.enumerable ?? false,
|
|
31678
|
+
value: wrapped,
|
|
31679
|
+
writable: ownDescriptor && "writable" in ownDescriptor ? ownDescriptor.writable : true
|
|
31680
|
+
});
|
|
31681
|
+
try {
|
|
31682
|
+
Object.defineProperty(wrapped, "name", {
|
|
31683
|
+
configurable: true,
|
|
31684
|
+
value: original.name
|
|
31685
|
+
});
|
|
31686
|
+
} catch {
|
|
31687
|
+
}
|
|
31688
|
+
}
|
|
31689
|
+
|
|
31690
|
+
// src/instrumentation/plugins/cloudflare-ai-chat-plugin.ts
|
|
31691
|
+
var BLOCKED_KEYS = /* @__PURE__ */ new Set(["__proto__", "constructor", "prototype"]);
|
|
31692
|
+
var RESPONSE_STATE_RETENTION_MS = 6e4;
|
|
31693
|
+
var CloudflareAIChatPlugin = class extends BasePlugin {
|
|
31694
|
+
activeStates = /* @__PURE__ */ new Set();
|
|
31695
|
+
activeTurns = /* @__PURE__ */ new WeakMap();
|
|
31696
|
+
eventStates = /* @__PURE__ */ new WeakMap();
|
|
31697
|
+
onEnable() {
|
|
31698
|
+
this.subscribeToResponseHook();
|
|
31699
|
+
this.subscribeToTurnRunner();
|
|
31700
|
+
}
|
|
31701
|
+
onDisable() {
|
|
31702
|
+
for (const unsubscribe of this.unsubscribers) {
|
|
31703
|
+
unsubscribe();
|
|
31704
|
+
}
|
|
31705
|
+
this.unsubscribers = [];
|
|
31706
|
+
for (const state of this.activeStates) {
|
|
31707
|
+
this.cleanupState(state);
|
|
31708
|
+
}
|
|
31709
|
+
this.activeStates.clear();
|
|
31710
|
+
}
|
|
31711
|
+
subscribeToTurnRunner() {
|
|
31712
|
+
const tracingChannel = cloudflareAIChatChannels.runExclusiveChatTurn.tracingChannel();
|
|
31713
|
+
const unbindCurrentSpanStore = this.bindCurrentSpanStore(tracingChannel);
|
|
31714
|
+
const handlers = {
|
|
31715
|
+
start: (event) => {
|
|
31716
|
+
this.ensureEventState(event);
|
|
31717
|
+
},
|
|
31718
|
+
asyncEnd: (event) => {
|
|
31719
|
+
this.finishEvent(event);
|
|
31720
|
+
},
|
|
31721
|
+
error: (event) => {
|
|
31722
|
+
this.finishEvent(event, event.error);
|
|
31723
|
+
}
|
|
31724
|
+
};
|
|
31725
|
+
tracingChannel.subscribe(handlers);
|
|
31726
|
+
this.unsubscribers.push(() => {
|
|
31727
|
+
unbindCurrentSpanStore?.();
|
|
31728
|
+
tracingChannel.unsubscribe(handlers);
|
|
31729
|
+
});
|
|
31730
|
+
}
|
|
31731
|
+
subscribeToResponseHook() {
|
|
31732
|
+
const tracingChannel = cloudflareAIChatChannels.onChatResponse.tracingChannel();
|
|
31733
|
+
const handlers = {
|
|
31734
|
+
start: (event) => {
|
|
31735
|
+
let state;
|
|
31736
|
+
try {
|
|
31737
|
+
const agent = asObject(event.self);
|
|
31738
|
+
const result = event.arguments[0];
|
|
31739
|
+
state = agent ? this.findResponseState(
|
|
31740
|
+
agent,
|
|
31741
|
+
stringValue3(ownValue2(result, "requestId"))
|
|
31742
|
+
) : void 0;
|
|
31743
|
+
if (!state) {
|
|
31744
|
+
return;
|
|
31745
|
+
}
|
|
31746
|
+
state.responseObserved = true;
|
|
31747
|
+
const output = serializeMessage(ownValue2(result, "message"));
|
|
31748
|
+
const status = stringValue3(ownValue2(result, "status"));
|
|
31749
|
+
const error = ownValue2(result, "error");
|
|
31750
|
+
const input = ownValue2(result, "continuation") === true ? state.input : serializeMessages(readProperty(agent, "messages"))?.filter(
|
|
31751
|
+
(message) => typeof output?.id !== "string" || message.id !== output.id
|
|
31752
|
+
);
|
|
31753
|
+
state.span.log({
|
|
31754
|
+
...input !== void 0 ? { input } : {},
|
|
31755
|
+
...output !== void 0 ? { output } : {},
|
|
31756
|
+
...status === "error" && error !== void 0 ? { error } : {}
|
|
31757
|
+
});
|
|
31758
|
+
} catch (error) {
|
|
31759
|
+
debugLogger.debug(
|
|
31760
|
+
"Failed to process @cloudflare/ai-chat response hook:",
|
|
31761
|
+
error
|
|
31762
|
+
);
|
|
31763
|
+
} finally {
|
|
31764
|
+
if (state?.settled) {
|
|
31765
|
+
this.cleanupState(state);
|
|
31766
|
+
}
|
|
31767
|
+
}
|
|
31768
|
+
}
|
|
31769
|
+
};
|
|
31770
|
+
tracingChannel.subscribe(handlers);
|
|
31771
|
+
this.unsubscribers.push(() => tracingChannel.unsubscribe(handlers));
|
|
31772
|
+
}
|
|
31773
|
+
bindCurrentSpanStore(tracingChannel) {
|
|
31774
|
+
const globalState = _internalGetGlobalState();
|
|
31775
|
+
const contextManager = globalState?.contextManager;
|
|
31776
|
+
const startChannel = tracingChannel.start;
|
|
31777
|
+
const currentSpanStore = contextManager ? contextManager[BRAINTRUST_CURRENT_SPAN_STORE] : void 0;
|
|
31778
|
+
if (!startChannel || !currentSpanStore || !contextManager) {
|
|
31779
|
+
return void 0;
|
|
31780
|
+
}
|
|
31781
|
+
startChannel.bindStore(currentSpanStore, (event) => {
|
|
31782
|
+
const state = this.ensureEventState(event);
|
|
31783
|
+
return state ? contextManager.wrapSpanForStore(state.span) : currentSpanStore.getStore();
|
|
31784
|
+
});
|
|
31785
|
+
return () => startChannel.unbindStore(currentSpanStore);
|
|
31786
|
+
}
|
|
31787
|
+
ensureEventState(event) {
|
|
31788
|
+
const eventKey = event;
|
|
31789
|
+
const known = this.eventStates.get(eventKey);
|
|
31790
|
+
if (known) {
|
|
31791
|
+
return known;
|
|
31792
|
+
}
|
|
31793
|
+
try {
|
|
31794
|
+
const agent = asObject(event.self);
|
|
31795
|
+
const requestId = stringValue3(event.arguments[0]);
|
|
31796
|
+
const key = requestId ?? /* @__PURE__ */ Symbol("cloudflare-ai-chat-turn");
|
|
31797
|
+
const activeForAgent = agent ? this.getActiveTurns(agent) : void 0;
|
|
31798
|
+
const existing = activeForAgent?.get(key);
|
|
31799
|
+
const state = existing && !existing.settled ? existing : this.createTurnState(agent, key);
|
|
31800
|
+
if (existing && !existing.settled) {
|
|
31801
|
+
state.depth += 1;
|
|
31802
|
+
}
|
|
31803
|
+
this.eventStates.set(eventKey, state);
|
|
31804
|
+
this.bindTurnCallback(event, agent, state);
|
|
31805
|
+
if (agent) {
|
|
31806
|
+
instrumentCloudflareAIChatResponseHook(agent);
|
|
31807
|
+
}
|
|
31808
|
+
return state;
|
|
31809
|
+
} catch (error) {
|
|
31810
|
+
debugLogger.debug(
|
|
31811
|
+
"Failed to start @cloudflare/ai-chat instrumentation:",
|
|
31812
|
+
error
|
|
31813
|
+
);
|
|
31814
|
+
return void 0;
|
|
31815
|
+
}
|
|
31816
|
+
}
|
|
31817
|
+
createTurnState(agent, key) {
|
|
31818
|
+
const span = startSpan(
|
|
31819
|
+
withSpanInstrumentationName(
|
|
31820
|
+
{
|
|
31821
|
+
name: "AIChatAgent.onChatMessage",
|
|
31822
|
+
spanAttributes: { type: "task" /* TASK */ }
|
|
31823
|
+
},
|
|
31824
|
+
INSTRUMENTATION_NAMES.CLOUDFLARE_AI_CHAT
|
|
31825
|
+
)
|
|
31826
|
+
);
|
|
31827
|
+
const state = {
|
|
31828
|
+
agent,
|
|
31829
|
+
depth: 1,
|
|
31830
|
+
ended: false,
|
|
31831
|
+
key,
|
|
31832
|
+
responseObserved: false,
|
|
31833
|
+
settled: false,
|
|
31834
|
+
span
|
|
31835
|
+
};
|
|
31836
|
+
this.activeStates.add(state);
|
|
31837
|
+
if (agent) {
|
|
31838
|
+
this.getActiveTurns(agent).set(key, state);
|
|
31839
|
+
}
|
|
31840
|
+
return state;
|
|
31841
|
+
}
|
|
31842
|
+
bindTurnCallback(event, agent, state) {
|
|
31843
|
+
const callback = event.arguments[1];
|
|
31844
|
+
if (typeof callback !== "function") {
|
|
31845
|
+
return;
|
|
31846
|
+
}
|
|
31847
|
+
try {
|
|
31848
|
+
event.arguments[1] = function(...args) {
|
|
31849
|
+
return withCurrent(state.span, () => {
|
|
31850
|
+
try {
|
|
31851
|
+
const input = serializeMessages(readProperty(agent, "messages"));
|
|
31852
|
+
if (input !== void 0 && state.input === void 0) {
|
|
31853
|
+
state.input = input;
|
|
31854
|
+
state.span.log({ input });
|
|
31855
|
+
}
|
|
31856
|
+
} catch (error) {
|
|
31857
|
+
debugLogger.debug(
|
|
31858
|
+
"Failed to capture @cloudflare/ai-chat messages:",
|
|
31859
|
+
error
|
|
31860
|
+
);
|
|
31861
|
+
}
|
|
31862
|
+
return Reflect.apply(callback, this, args);
|
|
31863
|
+
});
|
|
31864
|
+
};
|
|
31865
|
+
} catch (error) {
|
|
31866
|
+
debugLogger.debug(
|
|
31867
|
+
"Failed to bind @cloudflare/ai-chat turn callback context:",
|
|
31868
|
+
error
|
|
31869
|
+
);
|
|
31870
|
+
}
|
|
31871
|
+
}
|
|
31872
|
+
finishEvent(event, error) {
|
|
31873
|
+
const eventKey = event;
|
|
31874
|
+
const state = this.eventStates.get(eventKey);
|
|
31875
|
+
if (!state || state.ended) {
|
|
31876
|
+
return;
|
|
31877
|
+
}
|
|
31878
|
+
this.eventStates.delete(eventKey);
|
|
31879
|
+
if (error !== void 0 && state.pendingError === void 0) {
|
|
31880
|
+
state.pendingError = error;
|
|
31881
|
+
}
|
|
31882
|
+
state.depth -= 1;
|
|
31883
|
+
if (state.depth === 0) {
|
|
31884
|
+
state.settled = true;
|
|
31885
|
+
this.endState(state);
|
|
31886
|
+
if (state.responseObserved || error !== void 0) {
|
|
31887
|
+
this.cleanupState(state);
|
|
31888
|
+
} else {
|
|
31889
|
+
state.cleanupTimer = setTimeout(
|
|
31890
|
+
() => this.cleanupState(state),
|
|
31891
|
+
RESPONSE_STATE_RETENTION_MS
|
|
31892
|
+
);
|
|
31893
|
+
if (typeof state.cleanupTimer === "object" && state.cleanupTimer !== null && "unref" in state.cleanupTimer && typeof state.cleanupTimer.unref === "function") {
|
|
31894
|
+
state.cleanupTimer.unref();
|
|
31895
|
+
}
|
|
31896
|
+
}
|
|
31897
|
+
}
|
|
31898
|
+
}
|
|
31899
|
+
endState(state) {
|
|
31900
|
+
if (state.ended) {
|
|
31901
|
+
return;
|
|
31902
|
+
}
|
|
31903
|
+
state.ended = true;
|
|
31904
|
+
try {
|
|
31905
|
+
if (state.pendingError !== void 0) {
|
|
31906
|
+
state.span.log({ error: state.pendingError });
|
|
31907
|
+
}
|
|
31908
|
+
} finally {
|
|
31909
|
+
state.span.end();
|
|
31910
|
+
}
|
|
31911
|
+
}
|
|
31912
|
+
cleanupState(state) {
|
|
31913
|
+
if (state.cleanupTimer !== void 0) {
|
|
31914
|
+
clearTimeout(state.cleanupTimer);
|
|
31915
|
+
state.cleanupTimer = void 0;
|
|
31916
|
+
}
|
|
31917
|
+
try {
|
|
31918
|
+
this.endState(state);
|
|
31919
|
+
} finally {
|
|
31920
|
+
this.activeStates.delete(state);
|
|
31921
|
+
if (state.agent) {
|
|
31922
|
+
const activeForAgent = this.activeTurns.get(state.agent);
|
|
31923
|
+
if (activeForAgent?.get(state.key) === state) {
|
|
31924
|
+
activeForAgent.delete(state.key);
|
|
31925
|
+
}
|
|
31926
|
+
}
|
|
31927
|
+
}
|
|
31928
|
+
}
|
|
31929
|
+
getActiveTurns(agent) {
|
|
31930
|
+
let turns = this.activeTurns.get(agent);
|
|
31931
|
+
if (!turns) {
|
|
31932
|
+
turns = /* @__PURE__ */ new Map();
|
|
31933
|
+
this.activeTurns.set(agent, turns);
|
|
31934
|
+
}
|
|
31935
|
+
return turns;
|
|
31936
|
+
}
|
|
31937
|
+
findResponseState(agent, requestId) {
|
|
31938
|
+
const turns = this.activeTurns.get(agent);
|
|
31939
|
+
if (!turns) {
|
|
31940
|
+
return void 0;
|
|
31941
|
+
}
|
|
31942
|
+
if (requestId !== void 0) {
|
|
31943
|
+
return turns.get(requestId);
|
|
31944
|
+
}
|
|
31945
|
+
const active = [...turns.values()];
|
|
31946
|
+
return active.length === 1 ? active[0] : void 0;
|
|
31947
|
+
}
|
|
31948
|
+
};
|
|
31949
|
+
function asObject(value) {
|
|
31950
|
+
return value !== null && (typeof value === "object" || typeof value === "function") ? value : void 0;
|
|
31951
|
+
}
|
|
31952
|
+
function readProperty(value, key) {
|
|
31953
|
+
const object = asObject(value);
|
|
31954
|
+
return object ? Reflect.get(object, key) : void 0;
|
|
31955
|
+
}
|
|
31956
|
+
function ownValue2(value, key) {
|
|
31957
|
+
const object = asObject(value);
|
|
31958
|
+
if (!object) {
|
|
31959
|
+
return void 0;
|
|
31960
|
+
}
|
|
31961
|
+
const descriptor = Object.getOwnPropertyDescriptor(object, key);
|
|
31962
|
+
return descriptor && "value" in descriptor ? descriptor.value : void 0;
|
|
31963
|
+
}
|
|
31964
|
+
function stringValue3(value) {
|
|
31965
|
+
return typeof value === "string" ? value : void 0;
|
|
31966
|
+
}
|
|
31967
|
+
function serializeMessages(value) {
|
|
31968
|
+
if (!Array.isArray(value)) {
|
|
31969
|
+
return void 0;
|
|
31970
|
+
}
|
|
31971
|
+
return value.flatMap((message) => {
|
|
31972
|
+
const serialized = serializeMessage(message);
|
|
31973
|
+
return serialized === void 0 ? [] : [serialized];
|
|
31974
|
+
});
|
|
31975
|
+
}
|
|
31976
|
+
function serializeMessage(value) {
|
|
31977
|
+
const message = asObject(value);
|
|
31978
|
+
if (!message) {
|
|
31979
|
+
return void 0;
|
|
31980
|
+
}
|
|
31981
|
+
const serialized = [];
|
|
31982
|
+
const id = ownValue2(message, "id");
|
|
31983
|
+
const role = ownValue2(message, "role");
|
|
31984
|
+
const parts = ownValue2(message, "parts");
|
|
31985
|
+
if (typeof id === "string") {
|
|
31986
|
+
serialized.push(["id", id]);
|
|
31987
|
+
}
|
|
31988
|
+
if (typeof role === "string") {
|
|
31989
|
+
serialized.push(["role", role]);
|
|
31990
|
+
}
|
|
31991
|
+
const sanitizedParts = sanitizeLoggedValue2(parts);
|
|
31992
|
+
if (sanitizedParts !== void 0) {
|
|
31993
|
+
serialized.push(["parts", sanitizedParts]);
|
|
31994
|
+
}
|
|
31995
|
+
return serialized.length > 0 ? Object.fromEntries(serialized) : void 0;
|
|
31996
|
+
}
|
|
31997
|
+
function sanitizeLoggedValue2(value, seen = /* @__PURE__ */ new WeakSet(), depth = 0) {
|
|
31998
|
+
if (value === null || typeof value === "string" || typeof value === "boolean") {
|
|
31999
|
+
return value;
|
|
32000
|
+
}
|
|
32001
|
+
if (typeof value === "number") {
|
|
32002
|
+
return Number.isFinite(value) ? value : void 0;
|
|
32003
|
+
}
|
|
32004
|
+
const object = asObject(value);
|
|
32005
|
+
if (!object || depth >= 20 || typeof value === "function") {
|
|
32006
|
+
return void 0;
|
|
32007
|
+
}
|
|
32008
|
+
if (seen.has(object)) {
|
|
32009
|
+
return "[Circular]";
|
|
32010
|
+
}
|
|
32011
|
+
seen.add(object);
|
|
32012
|
+
if (Array.isArray(value)) {
|
|
32013
|
+
return value.map((entry) => sanitizeLoggedValue2(entry, seen, depth + 1));
|
|
32014
|
+
}
|
|
32015
|
+
const entries = [];
|
|
32016
|
+
for (const [key, descriptor] of Object.entries(
|
|
32017
|
+
Object.getOwnPropertyDescriptors(object)
|
|
32018
|
+
)) {
|
|
32019
|
+
if (BLOCKED_KEYS.has(key) || !descriptor.enumerable || !("value" in descriptor)) {
|
|
32020
|
+
continue;
|
|
32021
|
+
}
|
|
32022
|
+
const sanitized = sanitizeLoggedValue2(descriptor.value, seen, depth + 1);
|
|
32023
|
+
if (sanitized !== void 0) {
|
|
32024
|
+
entries.push([key, sanitized]);
|
|
32025
|
+
}
|
|
32026
|
+
}
|
|
32027
|
+
return Object.fromEntries(entries);
|
|
32028
|
+
}
|
|
32029
|
+
|
|
32030
|
+
// src/instrumentation/plugins/cloudflare-agents-channels.ts
|
|
32031
|
+
var cloudflareAgentsChannels = defineChannels(
|
|
32032
|
+
"agents",
|
|
32033
|
+
{
|
|
32034
|
+
runAgentTool: channel({
|
|
32035
|
+
channelName: "Agent.runAgentTool",
|
|
32036
|
+
kind: "async"
|
|
32037
|
+
})
|
|
32038
|
+
},
|
|
32039
|
+
{ instrumentationName: INSTRUMENTATION_NAMES.CLOUDFLARE_AGENTS }
|
|
32040
|
+
);
|
|
32041
|
+
|
|
32042
|
+
// src/instrumentation/plugins/cloudflare-agents-plugin.ts
|
|
32043
|
+
var CLOUDFLARE_WORKERS_CONTEXT = {
|
|
32044
|
+
span_origin: {
|
|
32045
|
+
environment: { type: "server", name: "cloudflare_workers" }
|
|
32046
|
+
}
|
|
32047
|
+
};
|
|
32048
|
+
var CloudflareAgentsPlugin = class extends BasePlugin {
|
|
32049
|
+
onEnable() {
|
|
32050
|
+
const channel2 = cloudflareAgentsChannels.runAgentTool.tracingChannel();
|
|
32051
|
+
const spans = /* @__PURE__ */ new WeakMap();
|
|
32052
|
+
const handlers = {
|
|
32053
|
+
start: (event) => {
|
|
32054
|
+
try {
|
|
32055
|
+
const agentClass = event.arguments[0];
|
|
32056
|
+
const options = event.arguments[1];
|
|
32057
|
+
if (ownValue3(options, "detached")) {
|
|
32058
|
+
return;
|
|
32059
|
+
}
|
|
32060
|
+
const name = ownValue3(agentClass, "name");
|
|
32061
|
+
if (typeof name !== "string" || name.length === 0) {
|
|
32062
|
+
debugLogger.warn(
|
|
32063
|
+
"Skipping Cloudflare Agents runAgentTool span because the child agent class has no name."
|
|
32064
|
+
);
|
|
32065
|
+
return;
|
|
32066
|
+
}
|
|
32067
|
+
const span = _internalStartSpanWithContext(
|
|
32068
|
+
withSpanInstrumentationName(
|
|
32069
|
+
{
|
|
32070
|
+
name,
|
|
32071
|
+
spanAttributes: { type: "tool" /* TOOL */ },
|
|
32072
|
+
event: {
|
|
32073
|
+
input: ownValue3(options, "input")
|
|
32074
|
+
}
|
|
32075
|
+
},
|
|
32076
|
+
INSTRUMENTATION_NAMES.CLOUDFLARE_AGENTS
|
|
32077
|
+
),
|
|
32078
|
+
CLOUDFLARE_WORKERS_CONTEXT
|
|
32079
|
+
);
|
|
32080
|
+
spans.set(event, span);
|
|
32081
|
+
} catch (error) {
|
|
32082
|
+
logInstrumentationError6("start", error);
|
|
32083
|
+
}
|
|
32084
|
+
},
|
|
32085
|
+
asyncEnd: (event) => {
|
|
32086
|
+
const span = spans.get(event);
|
|
32087
|
+
if (!span) {
|
|
32088
|
+
return;
|
|
32089
|
+
}
|
|
32090
|
+
spans.delete(event);
|
|
32091
|
+
try {
|
|
32092
|
+
const status = ownValue3(event.result, "status");
|
|
32093
|
+
if (status === "completed") {
|
|
32094
|
+
span.log({ output: ownValue3(event.result, "output") });
|
|
32095
|
+
} else {
|
|
32096
|
+
const error = ownValue3(event.result, "error");
|
|
32097
|
+
if (typeof error === "string") {
|
|
32098
|
+
span.log({ error });
|
|
32099
|
+
}
|
|
32100
|
+
}
|
|
32101
|
+
} catch (error) {
|
|
32102
|
+
logInstrumentationError6("completion", error);
|
|
32103
|
+
} finally {
|
|
32104
|
+
safelyEndSpan(span);
|
|
32105
|
+
}
|
|
32106
|
+
},
|
|
32107
|
+
error: (event) => {
|
|
32108
|
+
const span = spans.get(event);
|
|
32109
|
+
if (!span) {
|
|
32110
|
+
return;
|
|
32111
|
+
}
|
|
32112
|
+
spans.delete(event);
|
|
32113
|
+
try {
|
|
32114
|
+
span.log({ error: event.error });
|
|
32115
|
+
} catch (error) {
|
|
32116
|
+
logInstrumentationError6("rejection", error);
|
|
32117
|
+
} finally {
|
|
32118
|
+
safelyEndSpan(span);
|
|
32119
|
+
}
|
|
32120
|
+
}
|
|
32121
|
+
};
|
|
32122
|
+
channel2.subscribe(handlers);
|
|
32123
|
+
this.unsubscribers.push(() => channel2.unsubscribe(handlers));
|
|
32124
|
+
}
|
|
32125
|
+
onDisable() {
|
|
32126
|
+
for (const unsubscribe of this.unsubscribers) {
|
|
32127
|
+
unsubscribe();
|
|
32128
|
+
}
|
|
32129
|
+
this.unsubscribers = [];
|
|
32130
|
+
}
|
|
32131
|
+
};
|
|
32132
|
+
function ownValue3(value, key) {
|
|
32133
|
+
if (!isObjectLike2(value)) {
|
|
32134
|
+
return void 0;
|
|
32135
|
+
}
|
|
32136
|
+
const descriptor = Object.getOwnPropertyDescriptor(value, key);
|
|
32137
|
+
return descriptor && "value" in descriptor ? descriptor.value : void 0;
|
|
32138
|
+
}
|
|
32139
|
+
function isObjectLike2(value) {
|
|
32140
|
+
return typeof value === "object" && value !== null || typeof value === "function";
|
|
32141
|
+
}
|
|
32142
|
+
function safelyEndSpan(span) {
|
|
32143
|
+
try {
|
|
32144
|
+
span.end();
|
|
32145
|
+
} catch (error) {
|
|
32146
|
+
logInstrumentationError6("span end", error);
|
|
32147
|
+
}
|
|
32148
|
+
}
|
|
32149
|
+
function logInstrumentationError6(operation, error) {
|
|
32150
|
+
debugLogger.error(
|
|
32151
|
+
`Failed to process Cloudflare Agents ${operation} instrumentation:`,
|
|
32152
|
+
error
|
|
32153
|
+
);
|
|
32154
|
+
}
|
|
32155
|
+
|
|
30763
32156
|
// src/instrumentation/braintrust-plugin.ts
|
|
30764
32157
|
var BraintrustPlugin = class extends BasePlugin {
|
|
30765
32158
|
config;
|
|
@@ -30772,6 +32165,7 @@ var BraintrustPlugin = class extends BasePlugin {
|
|
|
30772
32165
|
openAIAgentsPlugin = null;
|
|
30773
32166
|
googleGenAIPlugin = null;
|
|
30774
32167
|
huggingFacePlugin = null;
|
|
32168
|
+
huggingFaceTransformersPlugin = null;
|
|
30775
32169
|
openRouterPlugin = null;
|
|
30776
32170
|
openRouterAgentPlugin = null;
|
|
30777
32171
|
mistralPlugin = null;
|
|
@@ -30786,6 +32180,8 @@ var BraintrustPlugin = class extends BasePlugin {
|
|
|
30786
32180
|
langSmithPlugin = null;
|
|
30787
32181
|
piCodingAgentPlugin = null;
|
|
30788
32182
|
strandsAgentSDKPlugin = null;
|
|
32183
|
+
cloudflareAIChatPlugin = null;
|
|
32184
|
+
cloudflareAgentsPlugin = null;
|
|
30789
32185
|
constructor(config = {}) {
|
|
30790
32186
|
super();
|
|
30791
32187
|
this.config = config;
|
|
@@ -30827,6 +32223,8 @@ var BraintrustPlugin = class extends BasePlugin {
|
|
|
30827
32223
|
if (integrations.huggingface !== false) {
|
|
30828
32224
|
this.huggingFacePlugin = new HuggingFacePlugin();
|
|
30829
32225
|
this.huggingFacePlugin.enable();
|
|
32226
|
+
this.huggingFaceTransformersPlugin = new HuggingFaceTransformersPlugin();
|
|
32227
|
+
this.huggingFaceTransformersPlugin.enable();
|
|
30830
32228
|
}
|
|
30831
32229
|
if (integrations.openrouter !== false) {
|
|
30832
32230
|
this.openRouterPlugin = new OpenRouterPlugin();
|
|
@@ -30872,6 +32270,14 @@ var BraintrustPlugin = class extends BasePlugin {
|
|
|
30872
32270
|
this.strandsAgentSDKPlugin = new StrandsAgentSDKPlugin();
|
|
30873
32271
|
this.strandsAgentSDKPlugin.enable();
|
|
30874
32272
|
}
|
|
32273
|
+
if (integrations.cloudflareAIChat !== false) {
|
|
32274
|
+
this.cloudflareAIChatPlugin = new CloudflareAIChatPlugin();
|
|
32275
|
+
this.cloudflareAIChatPlugin.enable();
|
|
32276
|
+
}
|
|
32277
|
+
if (integrations.cloudflareAgents !== false) {
|
|
32278
|
+
this.cloudflareAgentsPlugin = new CloudflareAgentsPlugin();
|
|
32279
|
+
this.cloudflareAgentsPlugin.enable();
|
|
32280
|
+
}
|
|
30875
32281
|
if (integrations.flue !== false) {
|
|
30876
32282
|
this.fluePlugin = new FluePlugin();
|
|
30877
32283
|
this.fluePlugin.enable();
|
|
@@ -30924,6 +32330,10 @@ var BraintrustPlugin = class extends BasePlugin {
|
|
|
30924
32330
|
this.huggingFacePlugin.disable();
|
|
30925
32331
|
this.huggingFacePlugin = null;
|
|
30926
32332
|
}
|
|
32333
|
+
if (this.huggingFaceTransformersPlugin) {
|
|
32334
|
+
this.huggingFaceTransformersPlugin.disable();
|
|
32335
|
+
this.huggingFaceTransformersPlugin = null;
|
|
32336
|
+
}
|
|
30927
32337
|
if (this.openRouterPlugin) {
|
|
30928
32338
|
this.openRouterPlugin.disable();
|
|
30929
32339
|
this.openRouterPlugin = null;
|
|
@@ -30968,6 +32378,14 @@ var BraintrustPlugin = class extends BasePlugin {
|
|
|
30968
32378
|
this.strandsAgentSDKPlugin.disable();
|
|
30969
32379
|
this.strandsAgentSDKPlugin = null;
|
|
30970
32380
|
}
|
|
32381
|
+
if (this.cloudflareAIChatPlugin) {
|
|
32382
|
+
this.cloudflareAIChatPlugin.disable();
|
|
32383
|
+
this.cloudflareAIChatPlugin = null;
|
|
32384
|
+
}
|
|
32385
|
+
if (this.cloudflareAgentsPlugin) {
|
|
32386
|
+
this.cloudflareAgentsPlugin.disable();
|
|
32387
|
+
this.cloudflareAgentsPlugin = null;
|
|
32388
|
+
}
|
|
30971
32389
|
if (this.fluePlugin) {
|
|
30972
32390
|
this.fluePlugin.disable();
|
|
30973
32391
|
this.fluePlugin = null;
|
|
@@ -32651,6 +34069,9 @@ var envIntegrationAliases = {
|
|
|
32651
34069
|
strandsagentsdk: "strandsAgentSDK",
|
|
32652
34070
|
"strands-agent-sdk": "strandsAgentSDK",
|
|
32653
34071
|
"@strands-agents/sdk": "strandsAgentSDK",
|
|
34072
|
+
agents: "cloudflareAgents",
|
|
34073
|
+
"cloudflare-agents": "cloudflareAgents",
|
|
34074
|
+
cloudflareagents: "cloudflareAgents",
|
|
32654
34075
|
anthropic: "anthropic",
|
|
32655
34076
|
aisdk: "aisdk",
|
|
32656
34077
|
"ai-sdk": "aisdk",
|
|
@@ -32658,6 +34079,9 @@ var envIntegrationAliases = {
|
|
|
32658
34079
|
vercel: "vercel",
|
|
32659
34080
|
claudeagentsdk: "claudeAgentSDK",
|
|
32660
34081
|
"claude-agent-sdk": "claudeAgentSDK",
|
|
34082
|
+
cloudflareaichat: "cloudflareAIChat",
|
|
34083
|
+
"cloudflare-ai-chat": "cloudflareAIChat",
|
|
34084
|
+
"@cloudflare/ai-chat": "cloudflareAIChat",
|
|
32661
34085
|
cursor: "cursor",
|
|
32662
34086
|
"cursor-sdk": "cursorSDK",
|
|
32663
34087
|
cursorsdk: "cursorSDK",
|
|
@@ -32672,6 +34096,8 @@ var envIntegrationAliases = {
|
|
|
32672
34096
|
"google-genai": "googleGenAI",
|
|
32673
34097
|
googlegenai: "googleGenAI",
|
|
32674
34098
|
huggingface: "huggingface",
|
|
34099
|
+
"@huggingface/transformers": "huggingface",
|
|
34100
|
+
transformers: "huggingface",
|
|
32675
34101
|
openrouter: "openrouter",
|
|
32676
34102
|
openrouteragent: "openrouterAgent",
|
|
32677
34103
|
"openrouter-agent": "openrouterAgent",
|
|
@@ -32710,6 +34136,7 @@ function getDefaultInstrumentationIntegrations() {
|
|
|
32710
34136
|
googleADK: true,
|
|
32711
34137
|
huggingface: true,
|
|
32712
34138
|
claudeAgentSDK: true,
|
|
34139
|
+
cloudflareAIChat: true,
|
|
32713
34140
|
cursor: true,
|
|
32714
34141
|
cursorSDK: true,
|
|
32715
34142
|
flue: true,
|
|
@@ -32729,7 +34156,8 @@ function getDefaultInstrumentationIntegrations() {
|
|
|
32729
34156
|
langgraph: true,
|
|
32730
34157
|
langsmith: true,
|
|
32731
34158
|
piCodingAgent: true,
|
|
32732
|
-
strandsAgentSDK: true
|
|
34159
|
+
strandsAgentSDK: true,
|
|
34160
|
+
cloudflareAgents: true
|
|
32733
34161
|
};
|
|
32734
34162
|
}
|
|
32735
34163
|
function readDisabledInstrumentationEnvConfig(disabledList) {
|
|
@@ -32747,7 +34175,9 @@ function readDisabledInstrumentationEnvConfig(disabledList) {
|
|
|
32747
34175
|
}
|
|
32748
34176
|
|
|
32749
34177
|
// src/instrumentation/registry.ts
|
|
32750
|
-
var REGISTRY_STATE_KEY = /* @__PURE__ */ Symbol.for(
|
|
34178
|
+
var REGISTRY_STATE_KEY = /* @__PURE__ */ Symbol.for(
|
|
34179
|
+
`braintrust.registry.global-hooks.v${GLOBAL_INSTRUMENTATION_HOOKS_PROTOCOL_VERSION}`
|
|
34180
|
+
);
|
|
32751
34181
|
function getSharedState() {
|
|
32752
34182
|
const state = globalThis[/* @__PURE__ */ Symbol.for("braintrust-state")];
|
|
32753
34183
|
return state && typeof state === "object" ? state : void 0;
|