braintrust 3.6.0 → 3.7.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/dev/dist/index.js +1016 -317
- package/dev/dist/index.mjs +914 -215
- package/dist/auto-instrumentations/bundler/esbuild.cjs +34 -6
- package/dist/auto-instrumentations/bundler/esbuild.mjs +2 -2
- package/dist/auto-instrumentations/bundler/rollup.cjs +34 -6
- package/dist/auto-instrumentations/bundler/rollup.mjs +2 -2
- package/dist/auto-instrumentations/bundler/vite.cjs +34 -6
- package/dist/auto-instrumentations/bundler/vite.mjs +2 -2
- package/dist/auto-instrumentations/bundler/webpack-loader.cjs +955 -0
- package/dist/auto-instrumentations/bundler/webpack-loader.d.ts +53 -0
- package/dist/auto-instrumentations/bundler/webpack.cjs +34 -6
- package/dist/auto-instrumentations/bundler/webpack.mjs +2 -2
- package/dist/auto-instrumentations/{chunk-F7WAXFNM.mjs → chunk-AKEXR4AL.mjs} +34 -6
- package/dist/auto-instrumentations/{chunk-WOUC73KB.mjs → chunk-ZK2IYER2.mjs} +1 -1
- package/dist/auto-instrumentations/hook.mjs +65 -11
- package/dist/auto-instrumentations/index.cjs +34 -6
- package/dist/auto-instrumentations/index.mjs +1 -1
- package/dist/browser.js +926 -227
- package/dist/browser.mjs +926 -227
- package/dist/cli.js +919 -216
- package/dist/edge-light.js +955 -282
- package/dist/edge-light.mjs +955 -282
- package/dist/index.js +1184 -485
- package/dist/index.mjs +978 -279
- package/dist/instrumentation/index.js +883 -210
- package/dist/instrumentation/index.mjs +883 -210
- package/dist/workerd.js +955 -282
- package/dist/workerd.mjs +955 -282
- package/package.json +5 -1
package/dev/dist/index.js
CHANGED
|
@@ -24,25 +24,51 @@ function patchTracingChannel(tracingChannelFn) {
|
|
|
24
24
|
if (TracingChannel.prototype.tracePromise) {
|
|
25
25
|
TracingChannel.prototype.tracePromise = function(fn, context = {}, thisArg, ...args) {
|
|
26
26
|
const { start, end, asyncStart, asyncEnd, error } = this;
|
|
27
|
-
function
|
|
27
|
+
function publishRejected(err) {
|
|
28
28
|
context.error = err;
|
|
29
29
|
_optionalChain([error, 'optionalAccess', _14 => _14.publish, 'call', _15 => _15(context)]);
|
|
30
30
|
_optionalChain([asyncStart, 'optionalAccess', _16 => _16.publish, 'call', _17 => _17(context)]);
|
|
31
31
|
_optionalChain([asyncEnd, 'optionalAccess', _18 => _18.publish, 'call', _19 => _19(context)]);
|
|
32
|
-
return Promise.reject(err);
|
|
33
32
|
}
|
|
34
|
-
function
|
|
33
|
+
function publishResolved(result) {
|
|
35
34
|
context.result = result;
|
|
36
35
|
_optionalChain([asyncStart, 'optionalAccess', _20 => _20.publish, 'call', _21 => _21(context)]);
|
|
37
36
|
_optionalChain([asyncEnd, 'optionalAccess', _22 => _22.publish, 'call', _23 => _23(context)]);
|
|
38
|
-
return result;
|
|
39
37
|
}
|
|
40
38
|
return start.runStores(context, () => {
|
|
41
39
|
try {
|
|
42
40
|
const result = Reflect.apply(fn, thisArg, args);
|
|
43
41
|
_optionalChain([end, 'optionalAccess', _24 => _24.publish, 'call', _25 => _25(context)]);
|
|
44
42
|
if (result && (typeof result === "object" || typeof result === "function") && typeof result.then === "function") {
|
|
45
|
-
|
|
43
|
+
if (result.constructor === Promise) {
|
|
44
|
+
return result.then(
|
|
45
|
+
(res) => {
|
|
46
|
+
publishResolved(res);
|
|
47
|
+
return res;
|
|
48
|
+
},
|
|
49
|
+
(err) => {
|
|
50
|
+
publishRejected(err);
|
|
51
|
+
return Promise.reject(err);
|
|
52
|
+
}
|
|
53
|
+
);
|
|
54
|
+
}
|
|
55
|
+
void result.then(
|
|
56
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
57
|
+
(resolved) => {
|
|
58
|
+
try {
|
|
59
|
+
publishResolved(resolved);
|
|
60
|
+
} catch (e2) {
|
|
61
|
+
}
|
|
62
|
+
},
|
|
63
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
64
|
+
(err) => {
|
|
65
|
+
try {
|
|
66
|
+
publishRejected(err);
|
|
67
|
+
} catch (e3) {
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
);
|
|
71
|
+
return result;
|
|
46
72
|
}
|
|
47
73
|
context.result = result;
|
|
48
74
|
_optionalChain([asyncStart, 'optionalAccess', _26 => _26.publish, 'call', _27 => _27(context)]);
|
|
@@ -279,7 +305,7 @@ async function currentRepo() {
|
|
|
279
305
|
} else {
|
|
280
306
|
return null;
|
|
281
307
|
}
|
|
282
|
-
} catch (
|
|
308
|
+
} catch (e4) {
|
|
283
309
|
return null;
|
|
284
310
|
}
|
|
285
311
|
}
|
|
@@ -312,7 +338,7 @@ async function getBaseBranch(remote = void 0) {
|
|
|
312
338
|
throw new Error(`Could not find HEAD branch in remote ${remoteName}`);
|
|
313
339
|
}
|
|
314
340
|
branch = match[1];
|
|
315
|
-
} catch (
|
|
341
|
+
} catch (e5) {
|
|
316
342
|
branch = "main";
|
|
317
343
|
}
|
|
318
344
|
}
|
|
@@ -335,7 +361,7 @@ async function getBaseBranchAncestor(remote = void 0) {
|
|
|
335
361
|
`${remoteName}/${baseBranch}`
|
|
336
362
|
]);
|
|
337
363
|
return ancestor.trim();
|
|
338
|
-
} catch (
|
|
364
|
+
} catch (e6) {
|
|
339
365
|
return void 0;
|
|
340
366
|
}
|
|
341
367
|
}
|
|
@@ -362,7 +388,7 @@ async function getPastNAncestors(n = 1e3, remote = void 0) {
|
|
|
362
388
|
async function attempt(fn) {
|
|
363
389
|
try {
|
|
364
390
|
return await fn();
|
|
365
|
-
} catch (
|
|
391
|
+
} catch (e7) {
|
|
366
392
|
return void 0;
|
|
367
393
|
}
|
|
368
394
|
}
|
|
@@ -983,7 +1009,7 @@ function tryMakeUuid3(s) {
|
|
|
983
1009
|
throw new Error();
|
|
984
1010
|
}
|
|
985
1011
|
return { bytes: new Uint8Array(ret), isUUID: true };
|
|
986
|
-
} catch (
|
|
1012
|
+
} catch (e8) {
|
|
987
1013
|
return { bytes: void 0, isUUID: false };
|
|
988
1014
|
}
|
|
989
1015
|
}
|
|
@@ -1147,7 +1173,7 @@ var SpanComponentsV3 = class _SpanComponentsV3 {
|
|
|
1147
1173
|
}
|
|
1148
1174
|
}
|
|
1149
1175
|
return _SpanComponentsV3.fromJsonObj(jsonObj);
|
|
1150
|
-
} catch (
|
|
1176
|
+
} catch (e9) {
|
|
1151
1177
|
throw new Error(INVALID_ENCODING_ERRMSG3);
|
|
1152
1178
|
}
|
|
1153
1179
|
}
|
|
@@ -1506,7 +1532,7 @@ function tryMakeHexTraceId(s) {
|
|
|
1506
1532
|
}
|
|
1507
1533
|
return { bytes, isHex: true };
|
|
1508
1534
|
}
|
|
1509
|
-
} catch (
|
|
1535
|
+
} catch (e10) {
|
|
1510
1536
|
}
|
|
1511
1537
|
return { bytes: void 0, isHex: false };
|
|
1512
1538
|
}
|
|
@@ -1522,7 +1548,7 @@ function tryMakeHexSpanId(s) {
|
|
|
1522
1548
|
}
|
|
1523
1549
|
return { bytes, isHex: true };
|
|
1524
1550
|
}
|
|
1525
|
-
} catch (
|
|
1551
|
+
} catch (e11) {
|
|
1526
1552
|
}
|
|
1527
1553
|
return { bytes: void 0, isHex: false };
|
|
1528
1554
|
}
|
|
@@ -1671,7 +1697,7 @@ var SpanComponentsV4 = class _SpanComponentsV4 {
|
|
|
1671
1697
|
}
|
|
1672
1698
|
}
|
|
1673
1699
|
return _SpanComponentsV4.fromJsonObj(jsonObj);
|
|
1674
|
-
} catch (
|
|
1700
|
+
} catch (e12) {
|
|
1675
1701
|
throw new Error(INVALID_ENCODING_ERRMSG_V4);
|
|
1676
1702
|
}
|
|
1677
1703
|
}
|
|
@@ -3612,7 +3638,7 @@ function getMustacheVars(prompt) {
|
|
|
3612
3638
|
return _mustache2.default.parse(prompt).filter(
|
|
3613
3639
|
(span) => span[0] === "name" || span[0] === "&"
|
|
3614
3640
|
);
|
|
3615
|
-
} catch (
|
|
3641
|
+
} catch (e13) {
|
|
3616
3642
|
return [];
|
|
3617
3643
|
}
|
|
3618
3644
|
}
|
|
@@ -4371,13 +4397,13 @@ var SpanCache = (_class6 = class {
|
|
|
4371
4397
|
cache.fileHandle.close().catch(() => {
|
|
4372
4398
|
});
|
|
4373
4399
|
cache.fileHandle = null;
|
|
4374
|
-
} catch (
|
|
4400
|
+
} catch (e14) {
|
|
4375
4401
|
}
|
|
4376
4402
|
}
|
|
4377
4403
|
if (cache.cacheFilePath && canUseSpanCache() && isomorph_default.unlinkSync) {
|
|
4378
4404
|
try {
|
|
4379
4405
|
isomorph_default.unlinkSync(cache.cacheFilePath);
|
|
4380
|
-
} catch (
|
|
4406
|
+
} catch (e15) {
|
|
4381
4407
|
}
|
|
4382
4408
|
}
|
|
4383
4409
|
}
|
|
@@ -4474,10 +4500,10 @@ var SpanCache = (_class6 = class {
|
|
|
4474
4500
|
} else {
|
|
4475
4501
|
spanMap.set(record.spanId, record.data);
|
|
4476
4502
|
}
|
|
4477
|
-
} catch (
|
|
4503
|
+
} catch (e16) {
|
|
4478
4504
|
}
|
|
4479
4505
|
}
|
|
4480
|
-
} catch (
|
|
4506
|
+
} catch (e17) {
|
|
4481
4507
|
}
|
|
4482
4508
|
}
|
|
4483
4509
|
for (const record of this.writeBuffer) {
|
|
@@ -4549,7 +4575,7 @@ var SpanCache = (_class6 = class {
|
|
|
4549
4575
|
if (this.cacheFilePath && canUseSpanCache() && isomorph_default.unlinkSync) {
|
|
4550
4576
|
try {
|
|
4551
4577
|
isomorph_default.unlinkSync(this.cacheFilePath);
|
|
4552
|
-
} catch (
|
|
4578
|
+
} catch (e18) {
|
|
4553
4579
|
}
|
|
4554
4580
|
this.cacheFilePath = null;
|
|
4555
4581
|
}
|
|
@@ -5130,7 +5156,7 @@ var HTTPConnection = class _HTTPConnection {
|
|
|
5130
5156
|
try {
|
|
5131
5157
|
const resp = await this.get("ping");
|
|
5132
5158
|
return resp.status === 200;
|
|
5133
|
-
} catch (
|
|
5159
|
+
} catch (e19) {
|
|
5134
5160
|
return false;
|
|
5135
5161
|
}
|
|
5136
5162
|
}
|
|
@@ -8373,7 +8399,7 @@ function isURL(url) {
|
|
|
8373
8399
|
try {
|
|
8374
8400
|
const parsedUrl = new URL(url.trim());
|
|
8375
8401
|
return parsedUrl.protocol === "http:" || parsedUrl.protocol === "https:";
|
|
8376
|
-
} catch (
|
|
8402
|
+
} catch (e20) {
|
|
8377
8403
|
return false;
|
|
8378
8404
|
}
|
|
8379
8405
|
}
|
|
@@ -9759,7 +9785,7 @@ function convertDataToBlob(data, mediaType) {
|
|
|
9759
9785
|
} else if (typeof Buffer !== "undefined" && data instanceof Buffer) {
|
|
9760
9786
|
return new Blob([data], { type: mediaType });
|
|
9761
9787
|
}
|
|
9762
|
-
} catch (
|
|
9788
|
+
} catch (e21) {
|
|
9763
9789
|
return null;
|
|
9764
9790
|
}
|
|
9765
9791
|
return null;
|
|
@@ -10468,7 +10494,7 @@ var AnthropicPlugin = class extends BasePlugin {
|
|
|
10468
10494
|
this.unsubscribers.push(
|
|
10469
10495
|
traceStreamingChannel(anthropicChannels.betaMessagesCreate, {
|
|
10470
10496
|
...anthropicConfig,
|
|
10471
|
-
name: "anthropic.
|
|
10497
|
+
name: "anthropic.messages.create"
|
|
10472
10498
|
})
|
|
10473
10499
|
);
|
|
10474
10500
|
}
|
|
@@ -10491,9 +10517,12 @@ function parseMetricsFromUsage2(usage) {
|
|
|
10491
10517
|
return metrics;
|
|
10492
10518
|
}
|
|
10493
10519
|
function aggregateAnthropicStreamChunks(chunks) {
|
|
10494
|
-
const
|
|
10520
|
+
const fallbackTextDeltas = [];
|
|
10521
|
+
const contentBlocks = {};
|
|
10522
|
+
const contentBlockDeltas = {};
|
|
10495
10523
|
let metrics = {};
|
|
10496
10524
|
let metadata = {};
|
|
10525
|
+
let role;
|
|
10497
10526
|
for (const event of chunks) {
|
|
10498
10527
|
switch (_optionalChain([event, 'optionalAccess', _179 => _179.type])) {
|
|
10499
10528
|
case "message_start":
|
|
@@ -10501,15 +10530,43 @@ function aggregateAnthropicStreamChunks(chunks) {
|
|
|
10501
10530
|
const initialMetrics = parseMetricsFromUsage2(event.message.usage);
|
|
10502
10531
|
metrics = { ...metrics, ...initialMetrics };
|
|
10503
10532
|
}
|
|
10533
|
+
if (typeof _optionalChain([event, 'access', _182 => _182.message, 'optionalAccess', _183 => _183.role]) === "string") {
|
|
10534
|
+
role = event.message.role;
|
|
10535
|
+
}
|
|
10536
|
+
break;
|
|
10537
|
+
case "content_block_start":
|
|
10538
|
+
if (event.content_block) {
|
|
10539
|
+
contentBlocks[event.index] = event.content_block;
|
|
10540
|
+
contentBlockDeltas[event.index] = [];
|
|
10541
|
+
}
|
|
10504
10542
|
break;
|
|
10505
10543
|
case "content_block_delta":
|
|
10506
|
-
if (_optionalChain([event, 'access',
|
|
10544
|
+
if (_optionalChain([event, 'access', _184 => _184.delta, 'optionalAccess', _185 => _185.type]) === "text_delta") {
|
|
10507
10545
|
const text = event.delta.text;
|
|
10508
10546
|
if (text) {
|
|
10509
|
-
|
|
10547
|
+
if (contentBlocks[event.index] !== void 0 || contentBlockDeltas[event.index] !== void 0) {
|
|
10548
|
+
contentBlockDeltas[event.index] ??= [];
|
|
10549
|
+
contentBlockDeltas[event.index].push(text);
|
|
10550
|
+
} else {
|
|
10551
|
+
fallbackTextDeltas.push(text);
|
|
10552
|
+
}
|
|
10553
|
+
}
|
|
10554
|
+
} else if (_optionalChain([event, 'access', _186 => _186.delta, 'optionalAccess', _187 => _187.type]) === "input_json_delta") {
|
|
10555
|
+
const partialJson = event.delta.partial_json;
|
|
10556
|
+
if (partialJson) {
|
|
10557
|
+
contentBlockDeltas[event.index] ??= [];
|
|
10558
|
+
contentBlockDeltas[event.index].push(partialJson);
|
|
10510
10559
|
}
|
|
10511
10560
|
}
|
|
10512
10561
|
break;
|
|
10562
|
+
case "content_block_stop":
|
|
10563
|
+
finalizeContentBlock(
|
|
10564
|
+
event.index,
|
|
10565
|
+
contentBlocks,
|
|
10566
|
+
contentBlockDeltas,
|
|
10567
|
+
fallbackTextDeltas
|
|
10568
|
+
);
|
|
10569
|
+
break;
|
|
10513
10570
|
case "message_delta":
|
|
10514
10571
|
if (event.usage) {
|
|
10515
10572
|
const finalMetrics = parseMetricsFromUsage2(event.usage);
|
|
@@ -10521,7 +10578,21 @@ function aggregateAnthropicStreamChunks(chunks) {
|
|
|
10521
10578
|
break;
|
|
10522
10579
|
}
|
|
10523
10580
|
}
|
|
10524
|
-
const
|
|
10581
|
+
const orderedContent = Object.entries(contentBlocks).map(([index, block]) => ({
|
|
10582
|
+
block,
|
|
10583
|
+
index: Number(index)
|
|
10584
|
+
})).filter(({ block }) => block !== void 0).sort((left, right) => left.index - right.index).map(({ block }) => block);
|
|
10585
|
+
let output = fallbackTextDeltas.join("");
|
|
10586
|
+
if (orderedContent.length > 0) {
|
|
10587
|
+
if (orderedContent.every(isTextContentBlock)) {
|
|
10588
|
+
output = orderedContent.map((block) => block.text).join("");
|
|
10589
|
+
} else {
|
|
10590
|
+
output = {
|
|
10591
|
+
...role ? { role } : {},
|
|
10592
|
+
content: orderedContent
|
|
10593
|
+
};
|
|
10594
|
+
}
|
|
10595
|
+
}
|
|
10525
10596
|
const finalized = finalizeAnthropicTokens(metrics);
|
|
10526
10597
|
const filteredMetrics = Object.fromEntries(
|
|
10527
10598
|
Object.entries(finalized).filter(
|
|
@@ -10534,6 +10605,49 @@ function aggregateAnthropicStreamChunks(chunks) {
|
|
|
10534
10605
|
metadata
|
|
10535
10606
|
};
|
|
10536
10607
|
}
|
|
10608
|
+
function finalizeContentBlock(index, contentBlocks, contentBlockDeltas, fallbackTextDeltas) {
|
|
10609
|
+
const contentBlock = contentBlocks[index];
|
|
10610
|
+
if (!contentBlock) {
|
|
10611
|
+
return;
|
|
10612
|
+
}
|
|
10613
|
+
const text = _nullishCoalesce(_optionalChain([contentBlockDeltas, 'access', _188 => _188[index], 'optionalAccess', _189 => _189.join, 'call', _190 => _190("")]), () => ( ""));
|
|
10614
|
+
if (isToolUseContentBlock(contentBlock)) {
|
|
10615
|
+
if (!text) {
|
|
10616
|
+
return;
|
|
10617
|
+
}
|
|
10618
|
+
try {
|
|
10619
|
+
contentBlocks[index] = {
|
|
10620
|
+
...contentBlock,
|
|
10621
|
+
input: JSON.parse(text)
|
|
10622
|
+
};
|
|
10623
|
+
} catch (e22) {
|
|
10624
|
+
fallbackTextDeltas.push(text);
|
|
10625
|
+
delete contentBlocks[index];
|
|
10626
|
+
}
|
|
10627
|
+
return;
|
|
10628
|
+
}
|
|
10629
|
+
if (isTextContentBlock(contentBlock)) {
|
|
10630
|
+
if (!text) {
|
|
10631
|
+
delete contentBlocks[index];
|
|
10632
|
+
return;
|
|
10633
|
+
}
|
|
10634
|
+
contentBlocks[index] = {
|
|
10635
|
+
...contentBlock,
|
|
10636
|
+
text
|
|
10637
|
+
};
|
|
10638
|
+
return;
|
|
10639
|
+
}
|
|
10640
|
+
if (text) {
|
|
10641
|
+
fallbackTextDeltas.push(text);
|
|
10642
|
+
}
|
|
10643
|
+
delete contentBlocks[index];
|
|
10644
|
+
}
|
|
10645
|
+
function isTextContentBlock(contentBlock) {
|
|
10646
|
+
return contentBlock.type === "text";
|
|
10647
|
+
}
|
|
10648
|
+
function isToolUseContentBlock(contentBlock) {
|
|
10649
|
+
return contentBlock.type === "tool_use";
|
|
10650
|
+
}
|
|
10537
10651
|
function isAnthropicBase64ContentBlock(input) {
|
|
10538
10652
|
return (input.type === "image" || input.type === "document") && isObject(input.source) && input.source.type === "base64";
|
|
10539
10653
|
}
|
|
@@ -10660,7 +10774,7 @@ function isZodSchema(value) {
|
|
|
10660
10774
|
function serializeZodSchema(schema) {
|
|
10661
10775
|
try {
|
|
10662
10776
|
return zodToJsonSchema(schema);
|
|
10663
|
-
} catch (
|
|
10777
|
+
} catch (e23) {
|
|
10664
10778
|
return {
|
|
10665
10779
|
type: "object",
|
|
10666
10780
|
description: "Zod schema (conversion failed)"
|
|
@@ -10960,7 +11074,7 @@ function extractTopLevelAISDKMetrics(result, event, startTime) {
|
|
|
10960
11074
|
return metrics;
|
|
10961
11075
|
}
|
|
10962
11076
|
function hasModelChildTracing(event) {
|
|
10963
|
-
return _optionalChain([event, 'optionalAccess',
|
|
11077
|
+
return _optionalChain([event, 'optionalAccess', _191 => _191.__braintrust_ai_sdk_model_wrapped]) === true;
|
|
10964
11078
|
}
|
|
10965
11079
|
function extractMetadataFromParams(params, self) {
|
|
10966
11080
|
const metadata = {
|
|
@@ -10969,7 +11083,7 @@ function extractMetadataFromParams(params, self) {
|
|
|
10969
11083
|
sdk_language: "typescript"
|
|
10970
11084
|
}
|
|
10971
11085
|
};
|
|
10972
|
-
const agentModel = self && typeof self === "object" && "model" in self && self.model ? self.model : self && typeof self === "object" && "settings" in self && _optionalChain([self, 'access',
|
|
11086
|
+
const agentModel = self && typeof self === "object" && "model" in self && self.model ? self.model : self && typeof self === "object" && "settings" in self && _optionalChain([self, 'access', _192 => _192.settings, 'optionalAccess', _193 => _193.model]) ? _optionalChain([self, 'access', _194 => _194.settings, 'optionalAccess', _195 => _195.model]) : void 0;
|
|
10973
11087
|
const { model, provider } = serializeModelWithProvider(
|
|
10974
11088
|
_nullishCoalesce(params.model, () => ( agentModel))
|
|
10975
11089
|
);
|
|
@@ -11071,9 +11185,9 @@ function prepareAISDKChildTracing(params, self, parentSpan, denyOutputPaths) {
|
|
|
11071
11185
|
case "raw":
|
|
11072
11186
|
if (chunk.rawValue) {
|
|
11073
11187
|
const rawVal = chunk.rawValue;
|
|
11074
|
-
if (_optionalChain([rawVal, 'access',
|
|
11188
|
+
if (_optionalChain([rawVal, 'access', _196 => _196.delta, 'optionalAccess', _197 => _197.content])) {
|
|
11075
11189
|
text += rawVal.delta.content;
|
|
11076
|
-
} else if (_optionalChain([rawVal, 'access',
|
|
11190
|
+
} else if (_optionalChain([rawVal, 'access', _198 => _198.choices, 'optionalAccess', _199 => _199[0], 'optionalAccess', _200 => _200.delta, 'optionalAccess', _201 => _201.content])) {
|
|
11077
11191
|
text += rawVal.choices[0].delta.content;
|
|
11078
11192
|
} else if (typeof rawVal.text === "string") {
|
|
11079
11193
|
text += rawVal.text;
|
|
@@ -11210,14 +11324,14 @@ function prepareAISDKChildTracing(params, self, parentSpan, denyOutputPaths) {
|
|
|
11210
11324
|
return {
|
|
11211
11325
|
cleanup: cleanup.length > 0 ? () => {
|
|
11212
11326
|
while (cleanup.length > 0) {
|
|
11213
|
-
_optionalChain([cleanup, 'access',
|
|
11327
|
+
_optionalChain([cleanup, 'access', _202 => _202.pop, 'call', _203 => _203(), 'optionalCall', _204 => _204()]);
|
|
11214
11328
|
}
|
|
11215
11329
|
} : void 0,
|
|
11216
11330
|
modelWrapped
|
|
11217
11331
|
};
|
|
11218
11332
|
}
|
|
11219
11333
|
function finalizeAISDKChildTracing(event) {
|
|
11220
|
-
const cleanup = _optionalChain([event, 'optionalAccess',
|
|
11334
|
+
const cleanup = _optionalChain([event, 'optionalAccess', _205 => _205.__braintrust_ai_sdk_cleanup]);
|
|
11221
11335
|
if (event && typeof cleanup === "function") {
|
|
11222
11336
|
cleanup();
|
|
11223
11337
|
delete event.__braintrust_ai_sdk_cleanup;
|
|
@@ -11282,7 +11396,7 @@ async function processAISDKStreamingOutput(result, denyOutputPaths) {
|
|
|
11282
11396
|
if ("text" in result && typeof result.text === "string") {
|
|
11283
11397
|
outputRecord.text = result.text;
|
|
11284
11398
|
}
|
|
11285
|
-
} catch (
|
|
11399
|
+
} catch (e24) {
|
|
11286
11400
|
}
|
|
11287
11401
|
try {
|
|
11288
11402
|
if ("object" in result) {
|
|
@@ -11291,7 +11405,7 @@ async function processAISDKStreamingOutput(result, denyOutputPaths) {
|
|
|
11291
11405
|
outputRecord.object = resolvedObject;
|
|
11292
11406
|
}
|
|
11293
11407
|
}
|
|
11294
|
-
} catch (
|
|
11408
|
+
} catch (e25) {
|
|
11295
11409
|
}
|
|
11296
11410
|
return outputRecord;
|
|
11297
11411
|
}
|
|
@@ -11309,10 +11423,10 @@ function buildAISDKChildMetadata(model) {
|
|
|
11309
11423
|
function buildResolvedMetadataPayload(result) {
|
|
11310
11424
|
const gatewayInfo = extractGatewayRoutingInfo(result);
|
|
11311
11425
|
const metadata = {};
|
|
11312
|
-
if (_optionalChain([gatewayInfo, 'optionalAccess',
|
|
11426
|
+
if (_optionalChain([gatewayInfo, 'optionalAccess', _206 => _206.provider])) {
|
|
11313
11427
|
metadata.provider = gatewayInfo.provider;
|
|
11314
11428
|
}
|
|
11315
|
-
if (_optionalChain([gatewayInfo, 'optionalAccess',
|
|
11429
|
+
if (_optionalChain([gatewayInfo, 'optionalAccess', _207 => _207.model])) {
|
|
11316
11430
|
metadata.model = gatewayInfo.model;
|
|
11317
11431
|
}
|
|
11318
11432
|
if (result.finishReason !== void 0) {
|
|
@@ -11347,7 +11461,7 @@ function processAISDKOutput(output, denyOutputPaths) {
|
|
|
11347
11461
|
}
|
|
11348
11462
|
function extractTokenMetrics(result) {
|
|
11349
11463
|
const metrics = {};
|
|
11350
|
-
let usage = _optionalChain([result, 'optionalAccess',
|
|
11464
|
+
let usage = _optionalChain([result, 'optionalAccess', _208 => _208.totalUsage]) || _optionalChain([result, 'optionalAccess', _209 => _209.usage]);
|
|
11351
11465
|
if (!usage && result) {
|
|
11352
11466
|
try {
|
|
11353
11467
|
if ("totalUsage" in result && typeof result.totalUsage !== "function") {
|
|
@@ -11355,14 +11469,14 @@ function extractTokenMetrics(result) {
|
|
|
11355
11469
|
} else if ("usage" in result && typeof result.usage !== "function") {
|
|
11356
11470
|
usage = result.usage;
|
|
11357
11471
|
}
|
|
11358
|
-
} catch (
|
|
11472
|
+
} catch (e26) {
|
|
11359
11473
|
}
|
|
11360
11474
|
}
|
|
11361
11475
|
if (!usage) {
|
|
11362
11476
|
return metrics;
|
|
11363
11477
|
}
|
|
11364
11478
|
const promptTokens = firstNumber(
|
|
11365
|
-
_optionalChain([usage, 'access',
|
|
11479
|
+
_optionalChain([usage, 'access', _210 => _210.inputTokens, 'optionalAccess', _211 => _211.total]),
|
|
11366
11480
|
usage.inputTokens,
|
|
11367
11481
|
usage.promptTokens,
|
|
11368
11482
|
usage.prompt_tokens
|
|
@@ -11371,7 +11485,7 @@ function extractTokenMetrics(result) {
|
|
|
11371
11485
|
metrics.prompt_tokens = promptTokens;
|
|
11372
11486
|
}
|
|
11373
11487
|
const completionTokens = firstNumber(
|
|
11374
|
-
_optionalChain([usage, 'access',
|
|
11488
|
+
_optionalChain([usage, 'access', _212 => _212.outputTokens, 'optionalAccess', _213 => _213.total]),
|
|
11375
11489
|
usage.outputTokens,
|
|
11376
11490
|
usage.completionTokens,
|
|
11377
11491
|
usage.completion_tokens
|
|
@@ -11438,7 +11552,7 @@ function extractGetterValues(obj) {
|
|
|
11438
11552
|
if (obj && name in obj && isSerializableOutputValue(obj[name])) {
|
|
11439
11553
|
getterValues[name] = obj[name];
|
|
11440
11554
|
}
|
|
11441
|
-
} catch (
|
|
11555
|
+
} catch (e27) {
|
|
11442
11556
|
}
|
|
11443
11557
|
}
|
|
11444
11558
|
return getterValues;
|
|
@@ -11457,11 +11571,11 @@ function extractSerializableOutputFields(output) {
|
|
|
11457
11571
|
];
|
|
11458
11572
|
for (const name of directFieldNames) {
|
|
11459
11573
|
try {
|
|
11460
|
-
const value = _optionalChain([output, 'optionalAccess',
|
|
11574
|
+
const value = _optionalChain([output, 'optionalAccess', _214 => _214[name]]);
|
|
11461
11575
|
if (isSerializableOutputValue(value)) {
|
|
11462
11576
|
serialized[name] = value;
|
|
11463
11577
|
}
|
|
11464
|
-
} catch (
|
|
11578
|
+
} catch (e28) {
|
|
11465
11579
|
}
|
|
11466
11580
|
}
|
|
11467
11581
|
return {
|
|
@@ -11485,8 +11599,8 @@ function isSerializableOutputValue(value) {
|
|
|
11485
11599
|
return true;
|
|
11486
11600
|
}
|
|
11487
11601
|
function serializeModelWithProvider(model) {
|
|
11488
|
-
const modelId = typeof model === "string" ? model : _optionalChain([model, 'optionalAccess',
|
|
11489
|
-
const explicitProvider = typeof model === "object" ? _optionalChain([model, 'optionalAccess',
|
|
11602
|
+
const modelId = typeof model === "string" ? model : _optionalChain([model, 'optionalAccess', _215 => _215.modelId]);
|
|
11603
|
+
const explicitProvider = typeof model === "object" ? _optionalChain([model, 'optionalAccess', _216 => _216.provider]) : void 0;
|
|
11490
11604
|
if (!modelId) {
|
|
11491
11605
|
return { model: modelId, provider: explicitProvider };
|
|
11492
11606
|
}
|
|
@@ -11510,8 +11624,8 @@ function parseGatewayModelString(modelString) {
|
|
|
11510
11624
|
return { model: modelString };
|
|
11511
11625
|
}
|
|
11512
11626
|
function extractGatewayRoutingInfo(result) {
|
|
11513
|
-
if (_optionalChain([result, 'optionalAccess',
|
|
11514
|
-
const routing2 = _optionalChain([result, 'access',
|
|
11627
|
+
if (_optionalChain([result, 'optionalAccess', _217 => _217.steps]) && Array.isArray(result.steps) && result.steps.length > 0) {
|
|
11628
|
+
const routing2 = _optionalChain([result, 'access', _218 => _218.steps, 'access', _219 => _219[0], 'optionalAccess', _220 => _220.providerMetadata, 'optionalAccess', _221 => _221.gateway, 'optionalAccess', _222 => _222.routing]);
|
|
11515
11629
|
if (routing2) {
|
|
11516
11630
|
return {
|
|
11517
11631
|
provider: routing2.resolvedProvider || routing2.finalProvider,
|
|
@@ -11519,7 +11633,7 @@ function extractGatewayRoutingInfo(result) {
|
|
|
11519
11633
|
};
|
|
11520
11634
|
}
|
|
11521
11635
|
}
|
|
11522
|
-
const routing = _optionalChain([result, 'optionalAccess',
|
|
11636
|
+
const routing = _optionalChain([result, 'optionalAccess', _223 => _223.providerMetadata, 'optionalAccess', _224 => _224.gateway, 'optionalAccess', _225 => _225.routing]);
|
|
11523
11637
|
if (routing) {
|
|
11524
11638
|
return {
|
|
11525
11639
|
provider: routing.resolvedProvider || routing.finalProvider,
|
|
@@ -11529,12 +11643,12 @@ function extractGatewayRoutingInfo(result) {
|
|
|
11529
11643
|
return null;
|
|
11530
11644
|
}
|
|
11531
11645
|
function extractCostFromResult(result) {
|
|
11532
|
-
if (_optionalChain([result, 'optionalAccess',
|
|
11646
|
+
if (_optionalChain([result, 'optionalAccess', _226 => _226.steps]) && Array.isArray(result.steps) && result.steps.length > 0) {
|
|
11533
11647
|
let totalCost = 0;
|
|
11534
11648
|
let foundCost = false;
|
|
11535
11649
|
for (const step of result.steps) {
|
|
11536
|
-
const gateway2 = _optionalChain([step, 'optionalAccess',
|
|
11537
|
-
const stepCost = parseGatewayCost(_optionalChain([gateway2, 'optionalAccess',
|
|
11650
|
+
const gateway2 = _optionalChain([step, 'optionalAccess', _227 => _227.providerMetadata, 'optionalAccess', _228 => _228.gateway]);
|
|
11651
|
+
const stepCost = parseGatewayCost(_optionalChain([gateway2, 'optionalAccess', _229 => _229.cost])) || parseGatewayCost(_optionalChain([gateway2, 'optionalAccess', _230 => _230.marketCost]));
|
|
11538
11652
|
if (stepCost !== void 0 && stepCost > 0) {
|
|
11539
11653
|
totalCost += stepCost;
|
|
11540
11654
|
foundCost = true;
|
|
@@ -11544,8 +11658,8 @@ function extractCostFromResult(result) {
|
|
|
11544
11658
|
return totalCost;
|
|
11545
11659
|
}
|
|
11546
11660
|
}
|
|
11547
|
-
const gateway = _optionalChain([result, 'optionalAccess',
|
|
11548
|
-
const directCost = parseGatewayCost(_optionalChain([gateway, 'optionalAccess',
|
|
11661
|
+
const gateway = _optionalChain([result, 'optionalAccess', _231 => _231.providerMetadata, 'optionalAccess', _232 => _232.gateway]);
|
|
11662
|
+
const directCost = parseGatewayCost(_optionalChain([gateway, 'optionalAccess', _233 => _233.cost])) || parseGatewayCost(_optionalChain([gateway, 'optionalAccess', _234 => _234.marketCost]));
|
|
11549
11663
|
if (directCost !== void 0 && directCost > 0) {
|
|
11550
11664
|
return directCost;
|
|
11551
11665
|
}
|
|
@@ -11656,12 +11770,15 @@ var claudeAgentSDKChannels = defineChannels(
|
|
|
11656
11770
|
{
|
|
11657
11771
|
query: channel({
|
|
11658
11772
|
channelName: "query",
|
|
11659
|
-
kind: "
|
|
11773
|
+
kind: "sync-stream"
|
|
11660
11774
|
})
|
|
11661
11775
|
}
|
|
11662
11776
|
);
|
|
11663
11777
|
|
|
11664
11778
|
// src/instrumentation/plugins/claude-agent-sdk-plugin.ts
|
|
11779
|
+
function isSubAgentToolName(toolName) {
|
|
11780
|
+
return toolName === "Agent" || toolName === "Task";
|
|
11781
|
+
}
|
|
11665
11782
|
function filterSerializableOptions(options) {
|
|
11666
11783
|
const allowedKeys = [
|
|
11667
11784
|
"model",
|
|
@@ -11697,7 +11814,7 @@ function extractUsageFromMessage(message) {
|
|
|
11697
11814
|
const metrics = {};
|
|
11698
11815
|
let usage;
|
|
11699
11816
|
if (message.type === "assistant") {
|
|
11700
|
-
usage = _optionalChain([message, 'access',
|
|
11817
|
+
usage = _optionalChain([message, 'access', _235 => _235.message, 'optionalAccess', _236 => _236.usage]);
|
|
11701
11818
|
} else if (message.type === "result") {
|
|
11702
11819
|
usage = message.usage;
|
|
11703
11820
|
}
|
|
@@ -11715,55 +11832,409 @@ function extractUsageFromMessage(message) {
|
|
|
11715
11832
|
const cacheReadTokens = getNumberProperty(usage, "cache_read_input_tokens") || 0;
|
|
11716
11833
|
const cacheCreationTokens = getNumberProperty(usage, "cache_creation_input_tokens") || 0;
|
|
11717
11834
|
if (cacheReadTokens > 0 || cacheCreationTokens > 0) {
|
|
11718
|
-
|
|
11719
|
-
|
|
11720
|
-
cacheCreationTokens
|
|
11835
|
+
Object.assign(
|
|
11836
|
+
metrics,
|
|
11837
|
+
extractAnthropicCacheTokens(cacheReadTokens, cacheCreationTokens)
|
|
11721
11838
|
);
|
|
11722
|
-
Object.assign(metrics, cacheTokens);
|
|
11723
11839
|
}
|
|
11724
11840
|
if (Object.keys(metrics).length > 0) {
|
|
11725
11841
|
Object.assign(metrics, finalizeAnthropicTokens(metrics));
|
|
11726
11842
|
}
|
|
11727
11843
|
return metrics;
|
|
11728
11844
|
}
|
|
11729
|
-
function buildLLMInput(prompt, conversationHistory) {
|
|
11730
|
-
const
|
|
11731
|
-
|
|
11732
|
-
|
|
11733
|
-
|
|
11734
|
-
|
|
11845
|
+
function buildLLMInput(prompt, conversationHistory, capturedPromptMessages) {
|
|
11846
|
+
const promptMessages = [];
|
|
11847
|
+
if (typeof prompt === "string") {
|
|
11848
|
+
promptMessages.push({ content: prompt, role: "user" });
|
|
11849
|
+
} else if (capturedPromptMessages && capturedPromptMessages.length > 0) {
|
|
11850
|
+
for (const msg of capturedPromptMessages) {
|
|
11851
|
+
const role = _optionalChain([msg, 'access', _237 => _237.message, 'optionalAccess', _238 => _238.role]);
|
|
11852
|
+
const content = _optionalChain([msg, 'access', _239 => _239.message, 'optionalAccess', _240 => _240.content]);
|
|
11853
|
+
if (role && content !== void 0) {
|
|
11854
|
+
promptMessages.push({ content, role });
|
|
11855
|
+
}
|
|
11856
|
+
}
|
|
11857
|
+
}
|
|
11858
|
+
const inputParts = [...promptMessages, ...conversationHistory];
|
|
11735
11859
|
return inputParts.length > 0 ? inputParts : void 0;
|
|
11736
11860
|
}
|
|
11737
|
-
|
|
11738
|
-
|
|
11861
|
+
function formatCapturedMessages(messages) {
|
|
11862
|
+
return messages.length > 0 ? messages : [];
|
|
11863
|
+
}
|
|
11864
|
+
async function createLLMSpanForMessages(messages, prompt, conversationHistory, options, startTime, capturedPromptMessages, parentSpan) {
|
|
11865
|
+
if (messages.length === 0) {
|
|
11866
|
+
return void 0;
|
|
11867
|
+
}
|
|
11739
11868
|
const lastMessage = messages[messages.length - 1];
|
|
11740
|
-
if (lastMessage.type !== "assistant" || !_optionalChain([lastMessage, 'access',
|
|
11869
|
+
if (lastMessage.type !== "assistant" || !_optionalChain([lastMessage, 'access', _241 => _241.message, 'optionalAccess', _242 => _242.usage])) {
|
|
11741
11870
|
return void 0;
|
|
11742
11871
|
}
|
|
11743
11872
|
const model = lastMessage.message.model || options.model;
|
|
11744
11873
|
const usage = extractUsageFromMessage(lastMessage);
|
|
11745
|
-
const input = buildLLMInput(
|
|
11874
|
+
const input = buildLLMInput(
|
|
11875
|
+
prompt,
|
|
11876
|
+
conversationHistory,
|
|
11877
|
+
capturedPromptMessages
|
|
11878
|
+
);
|
|
11746
11879
|
const outputs = messages.map(
|
|
11747
|
-
(m) => _optionalChain([m, 'access',
|
|
11880
|
+
(m) => _optionalChain([m, 'access', _243 => _243.message, 'optionalAccess', _244 => _244.content]) && _optionalChain([m, 'access', _245 => _245.message, 'optionalAccess', _246 => _246.role]) ? { content: m.message.content, role: m.message.role } : void 0
|
|
11748
11881
|
).filter(
|
|
11749
11882
|
(c) => c !== void 0
|
|
11750
11883
|
);
|
|
11751
11884
|
const span = startSpan({
|
|
11752
11885
|
name: "anthropic.messages.create",
|
|
11886
|
+
parent: parentSpan,
|
|
11753
11887
|
spanAttributes: {
|
|
11754
11888
|
type: "llm" /* LLM */
|
|
11755
11889
|
},
|
|
11756
|
-
startTime
|
|
11757
|
-
parent: parentSpan
|
|
11890
|
+
startTime
|
|
11758
11891
|
});
|
|
11759
11892
|
span.log({
|
|
11760
11893
|
input,
|
|
11761
|
-
output: outputs,
|
|
11762
11894
|
metadata: model ? { model } : void 0,
|
|
11763
|
-
metrics: usage
|
|
11895
|
+
metrics: usage,
|
|
11896
|
+
output: outputs
|
|
11764
11897
|
});
|
|
11765
11898
|
await span.end();
|
|
11766
|
-
return _optionalChain([lastMessage, 'access',
|
|
11899
|
+
return _optionalChain([lastMessage, 'access', _247 => _247.message, 'optionalAccess', _248 => _248.content]) && _optionalChain([lastMessage, 'access', _249 => _249.message, 'optionalAccess', _250 => _250.role]) ? { content: lastMessage.message.content, role: lastMessage.message.role } : void 0;
|
|
11900
|
+
}
|
|
11901
|
+
function getMcpServerMetadata(serverName, mcpServers) {
|
|
11902
|
+
if (!serverName || !mcpServers) {
|
|
11903
|
+
return {};
|
|
11904
|
+
}
|
|
11905
|
+
const serverConfig = mcpServers[serverName];
|
|
11906
|
+
if (!serverConfig) {
|
|
11907
|
+
return {};
|
|
11908
|
+
}
|
|
11909
|
+
const metadata = {};
|
|
11910
|
+
if (serverConfig.type) {
|
|
11911
|
+
metadata["mcp.type"] = serverConfig.type;
|
|
11912
|
+
} else if (typeof serverConfig === "object" && "transport" in serverConfig) {
|
|
11913
|
+
metadata["mcp.type"] = "sdk";
|
|
11914
|
+
}
|
|
11915
|
+
if (serverConfig.url) {
|
|
11916
|
+
metadata["mcp.url"] = serverConfig.url;
|
|
11917
|
+
}
|
|
11918
|
+
if (serverConfig.command) {
|
|
11919
|
+
metadata["mcp.command"] = serverConfig.command;
|
|
11920
|
+
if (serverConfig.args) {
|
|
11921
|
+
metadata["mcp.args"] = serverConfig.args.join(" ");
|
|
11922
|
+
}
|
|
11923
|
+
}
|
|
11924
|
+
return metadata;
|
|
11925
|
+
}
|
|
11926
|
+
function parseToolName(rawToolName) {
|
|
11927
|
+
const mcpMatch = rawToolName.match(/^mcp__([^_]+)__(.+)$/);
|
|
11928
|
+
if (mcpMatch) {
|
|
11929
|
+
const [, mcpServer, toolName] = mcpMatch;
|
|
11930
|
+
return {
|
|
11931
|
+
displayName: `tool: ${mcpServer}/${toolName}`,
|
|
11932
|
+
mcpServer,
|
|
11933
|
+
rawToolName,
|
|
11934
|
+
toolName
|
|
11935
|
+
};
|
|
11936
|
+
}
|
|
11937
|
+
return {
|
|
11938
|
+
displayName: `tool: ${rawToolName}`,
|
|
11939
|
+
rawToolName,
|
|
11940
|
+
toolName: rawToolName
|
|
11941
|
+
};
|
|
11942
|
+
}
|
|
11943
|
+
function createToolTracingHooks(resolveParentSpan, activeToolSpans, mcpServers, subAgentSpans, endedSubAgentSpans) {
|
|
11944
|
+
const preToolUse = async (input, toolUseID) => {
|
|
11945
|
+
if (input.hook_event_name !== "PreToolUse" || !toolUseID) {
|
|
11946
|
+
return {};
|
|
11947
|
+
}
|
|
11948
|
+
if (isSubAgentToolName(input.tool_name)) {
|
|
11949
|
+
return {};
|
|
11950
|
+
}
|
|
11951
|
+
const parsed = parseToolName(input.tool_name);
|
|
11952
|
+
const toolSpan = startSpan({
|
|
11953
|
+
event: {
|
|
11954
|
+
input: input.tool_input,
|
|
11955
|
+
metadata: {
|
|
11956
|
+
"claude_agent_sdk.cwd": input.cwd,
|
|
11957
|
+
"claude_agent_sdk.raw_tool_name": parsed.rawToolName,
|
|
11958
|
+
"claude_agent_sdk.session_id": input.session_id,
|
|
11959
|
+
"gen_ai.tool.call.id": toolUseID,
|
|
11960
|
+
"gen_ai.tool.name": parsed.toolName,
|
|
11961
|
+
...parsed.mcpServer && { "mcp.server": parsed.mcpServer },
|
|
11962
|
+
...getMcpServerMetadata(parsed.mcpServer, mcpServers)
|
|
11963
|
+
}
|
|
11964
|
+
},
|
|
11965
|
+
name: parsed.displayName,
|
|
11966
|
+
parent: await resolveParentSpan(toolUseID),
|
|
11967
|
+
spanAttributes: { type: "tool" /* TOOL */ }
|
|
11968
|
+
});
|
|
11969
|
+
activeToolSpans.set(toolUseID, toolSpan);
|
|
11970
|
+
return {};
|
|
11971
|
+
};
|
|
11972
|
+
const postToolUse = async (input, toolUseID) => {
|
|
11973
|
+
if (input.hook_event_name !== "PostToolUse" || !toolUseID) {
|
|
11974
|
+
return {};
|
|
11975
|
+
}
|
|
11976
|
+
const subAgentSpan = subAgentSpans.get(toolUseID);
|
|
11977
|
+
if (subAgentSpan) {
|
|
11978
|
+
try {
|
|
11979
|
+
const response = input.tool_response;
|
|
11980
|
+
const metadata = {};
|
|
11981
|
+
if (_optionalChain([response, 'optionalAccess', _251 => _251.status])) {
|
|
11982
|
+
metadata["claude_agent_sdk.status"] = response.status;
|
|
11983
|
+
}
|
|
11984
|
+
if (_optionalChain([response, 'optionalAccess', _252 => _252.totalDurationMs])) {
|
|
11985
|
+
metadata["claude_agent_sdk.duration_ms"] = response.totalDurationMs;
|
|
11986
|
+
}
|
|
11987
|
+
if (_optionalChain([response, 'optionalAccess', _253 => _253.totalToolUseCount]) !== void 0) {
|
|
11988
|
+
metadata["claude_agent_sdk.tool_use_count"] = response.totalToolUseCount;
|
|
11989
|
+
}
|
|
11990
|
+
subAgentSpan.log({
|
|
11991
|
+
metadata,
|
|
11992
|
+
output: _optionalChain([response, 'optionalAccess', _254 => _254.content])
|
|
11993
|
+
});
|
|
11994
|
+
} finally {
|
|
11995
|
+
subAgentSpan.end();
|
|
11996
|
+
endedSubAgentSpans.add(toolUseID);
|
|
11997
|
+
}
|
|
11998
|
+
return {};
|
|
11999
|
+
}
|
|
12000
|
+
const toolSpan = activeToolSpans.get(toolUseID);
|
|
12001
|
+
if (!toolSpan) {
|
|
12002
|
+
return {};
|
|
12003
|
+
}
|
|
12004
|
+
try {
|
|
12005
|
+
toolSpan.log({ output: input.tool_response });
|
|
12006
|
+
} finally {
|
|
12007
|
+
toolSpan.end();
|
|
12008
|
+
activeToolSpans.delete(toolUseID);
|
|
12009
|
+
}
|
|
12010
|
+
return {};
|
|
12011
|
+
};
|
|
12012
|
+
const postToolUseFailure = async (input, toolUseID) => {
|
|
12013
|
+
if (input.hook_event_name !== "PostToolUseFailure" || !toolUseID) {
|
|
12014
|
+
return {};
|
|
12015
|
+
}
|
|
12016
|
+
const subAgentSpan = subAgentSpans.get(toolUseID);
|
|
12017
|
+
if (subAgentSpan) {
|
|
12018
|
+
try {
|
|
12019
|
+
subAgentSpan.log({ error: input.error });
|
|
12020
|
+
} finally {
|
|
12021
|
+
subAgentSpan.end();
|
|
12022
|
+
endedSubAgentSpans.add(toolUseID);
|
|
12023
|
+
}
|
|
12024
|
+
return {};
|
|
12025
|
+
}
|
|
12026
|
+
const toolSpan = activeToolSpans.get(toolUseID);
|
|
12027
|
+
if (!toolSpan) {
|
|
12028
|
+
return {};
|
|
12029
|
+
}
|
|
12030
|
+
const parsed = parseToolName(input.tool_name);
|
|
12031
|
+
try {
|
|
12032
|
+
toolSpan.log({
|
|
12033
|
+
error: input.error,
|
|
12034
|
+
metadata: {
|
|
12035
|
+
"claude_agent_sdk.is_interrupt": input.is_interrupt,
|
|
12036
|
+
"claude_agent_sdk.session_id": input.session_id,
|
|
12037
|
+
"gen_ai.tool.call.id": toolUseID,
|
|
12038
|
+
"gen_ai.tool.name": parsed.toolName,
|
|
12039
|
+
...parsed.mcpServer && { "mcp.server": parsed.mcpServer }
|
|
12040
|
+
}
|
|
12041
|
+
});
|
|
12042
|
+
} finally {
|
|
12043
|
+
toolSpan.end();
|
|
12044
|
+
activeToolSpans.delete(toolUseID);
|
|
12045
|
+
}
|
|
12046
|
+
return {};
|
|
12047
|
+
};
|
|
12048
|
+
return { postToolUse, postToolUseFailure, preToolUse };
|
|
12049
|
+
}
|
|
12050
|
+
function injectTracingHooks(options, resolveParentSpan, activeToolSpans, subAgentSpans, endedSubAgentSpans) {
|
|
12051
|
+
const { preToolUse, postToolUse, postToolUseFailure } = createToolTracingHooks(
|
|
12052
|
+
resolveParentSpan,
|
|
12053
|
+
activeToolSpans,
|
|
12054
|
+
options.mcpServers,
|
|
12055
|
+
subAgentSpans,
|
|
12056
|
+
endedSubAgentSpans
|
|
12057
|
+
);
|
|
12058
|
+
const existingHooks = _nullishCoalesce(options.hooks, () => ( {}));
|
|
12059
|
+
return {
|
|
12060
|
+
...options,
|
|
12061
|
+
hooks: {
|
|
12062
|
+
...existingHooks,
|
|
12063
|
+
PostToolUse: [
|
|
12064
|
+
..._nullishCoalesce(existingHooks.PostToolUse, () => ( [])),
|
|
12065
|
+
{ hooks: [postToolUse] }
|
|
12066
|
+
],
|
|
12067
|
+
PostToolUseFailure: [
|
|
12068
|
+
..._nullishCoalesce(existingHooks.PostToolUseFailure, () => ( [])),
|
|
12069
|
+
{
|
|
12070
|
+
hooks: [postToolUseFailure]
|
|
12071
|
+
}
|
|
12072
|
+
],
|
|
12073
|
+
PreToolUse: [
|
|
12074
|
+
..._nullishCoalesce(existingHooks.PreToolUse, () => ( [])),
|
|
12075
|
+
{ hooks: [preToolUse] }
|
|
12076
|
+
]
|
|
12077
|
+
}
|
|
12078
|
+
};
|
|
12079
|
+
}
|
|
12080
|
+
async function finalizeCurrentMessageGroup(state) {
|
|
12081
|
+
if (state.currentMessages.length === 0) {
|
|
12082
|
+
return;
|
|
12083
|
+
}
|
|
12084
|
+
const parentToolUseId = _nullishCoalesce(_optionalChain([state, 'access', _255 => _255.currentMessages, 'access', _256 => _256[0], 'optionalAccess', _257 => _257.parent_tool_use_id]), () => ( null));
|
|
12085
|
+
let parentSpan = await state.span.export();
|
|
12086
|
+
if (parentToolUseId) {
|
|
12087
|
+
const subAgentSpan = state.subAgentSpans.get(parentToolUseId);
|
|
12088
|
+
if (subAgentSpan) {
|
|
12089
|
+
parentSpan = await subAgentSpan.export();
|
|
12090
|
+
}
|
|
12091
|
+
}
|
|
12092
|
+
const finalMessage = await createLLMSpanForMessages(
|
|
12093
|
+
state.currentMessages,
|
|
12094
|
+
state.originalPrompt,
|
|
12095
|
+
state.finalResults,
|
|
12096
|
+
state.options,
|
|
12097
|
+
state.currentMessageStartTime,
|
|
12098
|
+
state.capturedPromptMessages,
|
|
12099
|
+
parentSpan
|
|
12100
|
+
);
|
|
12101
|
+
if (finalMessage) {
|
|
12102
|
+
state.finalResults.push(finalMessage);
|
|
12103
|
+
}
|
|
12104
|
+
const lastMessage = state.currentMessages[state.currentMessages.length - 1];
|
|
12105
|
+
if (_optionalChain([lastMessage, 'optionalAccess', _258 => _258.message, 'optionalAccess', _259 => _259.usage])) {
|
|
12106
|
+
state.accumulatedOutputTokens += getNumberProperty(lastMessage.message.usage, "output_tokens") || 0;
|
|
12107
|
+
}
|
|
12108
|
+
state.currentMessages.length = 0;
|
|
12109
|
+
}
|
|
12110
|
+
function maybeTrackToolUseContext(state, message) {
|
|
12111
|
+
if (message.type !== "assistant" || !Array.isArray(_optionalChain([message, 'access', _260 => _260.message, 'optionalAccess', _261 => _261.content]))) {
|
|
12112
|
+
return;
|
|
12113
|
+
}
|
|
12114
|
+
const parentToolUseId = _nullishCoalesce(message.parent_tool_use_id, () => ( null));
|
|
12115
|
+
for (const block of message.message.content) {
|
|
12116
|
+
if (typeof block !== "object" || block === null || !("type" in block) || block.type !== "tool_use" || !("id" in block) || typeof block.id !== "string") {
|
|
12117
|
+
continue;
|
|
12118
|
+
}
|
|
12119
|
+
state.toolUseToParent.set(block.id, parentToolUseId);
|
|
12120
|
+
if (block.name === "Task" && typeof block.input === "object" && block.input !== null && "subagent_type" in block.input && typeof block.input.subagent_type === "string") {
|
|
12121
|
+
state.pendingSubAgentNames.set(block.id, block.input.subagent_type);
|
|
12122
|
+
}
|
|
12123
|
+
}
|
|
12124
|
+
}
|
|
12125
|
+
async function maybeStartSubAgentSpan(state, message) {
|
|
12126
|
+
if (!("parent_tool_use_id" in message)) {
|
|
12127
|
+
return;
|
|
12128
|
+
}
|
|
12129
|
+
const parentToolUseId = message.parent_tool_use_id;
|
|
12130
|
+
if (!parentToolUseId) {
|
|
12131
|
+
return;
|
|
12132
|
+
}
|
|
12133
|
+
await ensureSubAgentSpan(
|
|
12134
|
+
state.pendingSubAgentNames,
|
|
12135
|
+
state.span,
|
|
12136
|
+
state.subAgentSpans,
|
|
12137
|
+
parentToolUseId
|
|
12138
|
+
);
|
|
12139
|
+
}
|
|
12140
|
+
async function ensureSubAgentSpan(pendingSubAgentNames, rootSpan, subAgentSpans, parentToolUseId) {
|
|
12141
|
+
const existingSpan = subAgentSpans.get(parentToolUseId);
|
|
12142
|
+
if (existingSpan) {
|
|
12143
|
+
return existingSpan;
|
|
12144
|
+
}
|
|
12145
|
+
const agentName = pendingSubAgentNames.get(parentToolUseId);
|
|
12146
|
+
const spanName = agentName ? `Agent: ${agentName}` : "Agent: sub-agent";
|
|
12147
|
+
const subAgentSpan = startSpan({
|
|
12148
|
+
event: {
|
|
12149
|
+
metadata: {
|
|
12150
|
+
...agentName && { "claude_agent_sdk.agent_type": agentName }
|
|
12151
|
+
}
|
|
12152
|
+
},
|
|
12153
|
+
name: spanName,
|
|
12154
|
+
parent: await rootSpan.export(),
|
|
12155
|
+
spanAttributes: { type: "task" /* TASK */ }
|
|
12156
|
+
});
|
|
12157
|
+
subAgentSpans.set(parentToolUseId, subAgentSpan);
|
|
12158
|
+
return subAgentSpan;
|
|
12159
|
+
}
|
|
12160
|
+
async function handleStreamMessage(state, message) {
|
|
12161
|
+
maybeTrackToolUseContext(state, message);
|
|
12162
|
+
await maybeStartSubAgentSpan(state, message);
|
|
12163
|
+
const messageId = _optionalChain([message, 'access', _262 => _262.message, 'optionalAccess', _263 => _263.id]);
|
|
12164
|
+
if (messageId && messageId !== state.currentMessageId) {
|
|
12165
|
+
await finalizeCurrentMessageGroup(state);
|
|
12166
|
+
state.currentMessageId = messageId;
|
|
12167
|
+
state.currentMessageStartTime = getCurrentUnixTimestamp();
|
|
12168
|
+
}
|
|
12169
|
+
if (message.type === "assistant" && _optionalChain([message, 'access', _264 => _264.message, 'optionalAccess', _265 => _265.usage])) {
|
|
12170
|
+
state.currentMessages.push(message);
|
|
12171
|
+
}
|
|
12172
|
+
if (message.type !== "result" || !message.usage) {
|
|
12173
|
+
return;
|
|
12174
|
+
}
|
|
12175
|
+
const finalUsageMetrics = extractUsageFromMessage(message);
|
|
12176
|
+
if (state.currentMessages.length > 0 && finalUsageMetrics.completion_tokens !== void 0) {
|
|
12177
|
+
const lastMessage = state.currentMessages[state.currentMessages.length - 1];
|
|
12178
|
+
if (_optionalChain([lastMessage, 'optionalAccess', _266 => _266.message, 'optionalAccess', _267 => _267.usage])) {
|
|
12179
|
+
const adjustedTokens = finalUsageMetrics.completion_tokens - state.accumulatedOutputTokens;
|
|
12180
|
+
if (adjustedTokens >= 0) {
|
|
12181
|
+
lastMessage.message.usage.output_tokens = adjustedTokens;
|
|
12182
|
+
}
|
|
12183
|
+
const resultUsage = message.usage;
|
|
12184
|
+
if (resultUsage && typeof resultUsage === "object") {
|
|
12185
|
+
const cacheReadTokens = getNumberProperty(
|
|
12186
|
+
resultUsage,
|
|
12187
|
+
"cache_read_input_tokens"
|
|
12188
|
+
);
|
|
12189
|
+
if (cacheReadTokens !== void 0) {
|
|
12190
|
+
lastMessage.message.usage.cache_read_input_tokens = cacheReadTokens;
|
|
12191
|
+
}
|
|
12192
|
+
const cacheCreationTokens = getNumberProperty(
|
|
12193
|
+
resultUsage,
|
|
12194
|
+
"cache_creation_input_tokens"
|
|
12195
|
+
);
|
|
12196
|
+
if (cacheCreationTokens !== void 0) {
|
|
12197
|
+
lastMessage.message.usage.cache_creation_input_tokens = cacheCreationTokens;
|
|
12198
|
+
}
|
|
12199
|
+
}
|
|
12200
|
+
}
|
|
12201
|
+
}
|
|
12202
|
+
const metadata = {};
|
|
12203
|
+
if (message.num_turns !== void 0) {
|
|
12204
|
+
metadata.num_turns = message.num_turns;
|
|
12205
|
+
}
|
|
12206
|
+
if (message.session_id !== void 0) {
|
|
12207
|
+
metadata.session_id = message.session_id;
|
|
12208
|
+
}
|
|
12209
|
+
if (Object.keys(metadata).length > 0) {
|
|
12210
|
+
state.span.log({ metadata });
|
|
12211
|
+
}
|
|
12212
|
+
}
|
|
12213
|
+
async function finalizeQuerySpan(state) {
|
|
12214
|
+
try {
|
|
12215
|
+
await finalizeCurrentMessageGroup(state);
|
|
12216
|
+
state.span.log({
|
|
12217
|
+
output: state.finalResults.length > 0 ? state.finalResults[state.finalResults.length - 1] : void 0
|
|
12218
|
+
});
|
|
12219
|
+
if (state.capturedPromptMessages) {
|
|
12220
|
+
if (state.promptStarted()) {
|
|
12221
|
+
await state.promptDone;
|
|
12222
|
+
}
|
|
12223
|
+
if (state.capturedPromptMessages.length > 0) {
|
|
12224
|
+
state.span.log({
|
|
12225
|
+
input: formatCapturedMessages(state.capturedPromptMessages)
|
|
12226
|
+
});
|
|
12227
|
+
}
|
|
12228
|
+
}
|
|
12229
|
+
} finally {
|
|
12230
|
+
for (const [id, subAgentSpan] of state.subAgentSpans) {
|
|
12231
|
+
if (!state.endedSubAgentSpans.has(id)) {
|
|
12232
|
+
subAgentSpan.end();
|
|
12233
|
+
}
|
|
12234
|
+
}
|
|
12235
|
+
state.subAgentSpans.clear();
|
|
12236
|
+
state.span.end();
|
|
12237
|
+
}
|
|
11767
12238
|
}
|
|
11768
12239
|
var ClaudeAgentSDKPlugin = class extends BasePlugin {
|
|
11769
12240
|
onEnable() {
|
|
@@ -11775,19 +12246,36 @@ var ClaudeAgentSDKPlugin = class extends BasePlugin {
|
|
|
11775
12246
|
}
|
|
11776
12247
|
this.unsubscribers = [];
|
|
11777
12248
|
}
|
|
11778
|
-
/**
|
|
11779
|
-
* Subscribe to the query channel for agent interactions.
|
|
11780
|
-
* Handles streaming responses and traces both the top-level agent task
|
|
11781
|
-
* and individual LLM calls.
|
|
11782
|
-
*/
|
|
11783
12249
|
subscribeToQuery() {
|
|
11784
12250
|
const channel2 = claudeAgentSDKChannels.query.tracingChannel();
|
|
11785
12251
|
const spans = /* @__PURE__ */ new WeakMap();
|
|
11786
12252
|
const handlers = {
|
|
11787
12253
|
start: (event) => {
|
|
11788
|
-
const params = event.arguments[0];
|
|
11789
|
-
const
|
|
11790
|
-
const options = _nullishCoalesce(
|
|
12254
|
+
const params = _nullishCoalesce(event.arguments[0], () => ( {}));
|
|
12255
|
+
const originalPrompt = params.prompt;
|
|
12256
|
+
const options = _nullishCoalesce(params.options, () => ( {}));
|
|
12257
|
+
const promptIsAsyncIterable = isAsyncIterable(originalPrompt);
|
|
12258
|
+
let promptStarted = false;
|
|
12259
|
+
let capturedPromptMessages;
|
|
12260
|
+
let resolvePromptDone;
|
|
12261
|
+
const promptDone = new Promise((resolve) => {
|
|
12262
|
+
resolvePromptDone = resolve;
|
|
12263
|
+
});
|
|
12264
|
+
if (promptIsAsyncIterable) {
|
|
12265
|
+
capturedPromptMessages = [];
|
|
12266
|
+
const promptStream = originalPrompt;
|
|
12267
|
+
params.prompt = (async function* () {
|
|
12268
|
+
promptStarted = true;
|
|
12269
|
+
try {
|
|
12270
|
+
for await (const message of promptStream) {
|
|
12271
|
+
capturedPromptMessages.push(message);
|
|
12272
|
+
yield message;
|
|
12273
|
+
}
|
|
12274
|
+
} finally {
|
|
12275
|
+
_optionalChain([resolvePromptDone, 'optionalCall', _268 => _268()]);
|
|
12276
|
+
}
|
|
12277
|
+
})();
|
|
12278
|
+
}
|
|
11791
12279
|
const span = startSpan({
|
|
11792
12280
|
name: "Claude Agent",
|
|
11793
12281
|
spanAttributes: {
|
|
@@ -11797,163 +12285,115 @@ var ClaudeAgentSDKPlugin = class extends BasePlugin {
|
|
|
11797
12285
|
const startTime = getCurrentUnixTimestamp();
|
|
11798
12286
|
try {
|
|
11799
12287
|
span.log({
|
|
11800
|
-
input: typeof
|
|
11801
|
-
type: "streaming",
|
|
11802
|
-
description: "AsyncIterable<ClaudeAgentSDKMessage>"
|
|
11803
|
-
},
|
|
12288
|
+
input: typeof originalPrompt === "string" ? originalPrompt : promptIsAsyncIterable ? void 0 : originalPrompt !== void 0 ? String(originalPrompt) : void 0,
|
|
11804
12289
|
metadata: filterSerializableOptions(options)
|
|
11805
12290
|
});
|
|
11806
12291
|
} catch (error) {
|
|
11807
12292
|
console.error("Error extracting input for Claude Agent SDK:", error);
|
|
11808
12293
|
}
|
|
12294
|
+
const activeToolSpans = /* @__PURE__ */ new Map();
|
|
12295
|
+
const subAgentSpans = /* @__PURE__ */ new Map();
|
|
12296
|
+
const endedSubAgentSpans = /* @__PURE__ */ new Set();
|
|
12297
|
+
const toolUseToParent = /* @__PURE__ */ new Map();
|
|
12298
|
+
const pendingSubAgentNames = /* @__PURE__ */ new Map();
|
|
12299
|
+
const optionsWithHooks = injectTracingHooks(
|
|
12300
|
+
options,
|
|
12301
|
+
async (toolUseID) => {
|
|
12302
|
+
const parentToolUseId = toolUseToParent.get(toolUseID);
|
|
12303
|
+
if (parentToolUseId) {
|
|
12304
|
+
const subAgentSpan = await ensureSubAgentSpan(
|
|
12305
|
+
pendingSubAgentNames,
|
|
12306
|
+
span,
|
|
12307
|
+
subAgentSpans,
|
|
12308
|
+
parentToolUseId
|
|
12309
|
+
);
|
|
12310
|
+
return subAgentSpan.export();
|
|
12311
|
+
}
|
|
12312
|
+
return span.export();
|
|
12313
|
+
},
|
|
12314
|
+
activeToolSpans,
|
|
12315
|
+
subAgentSpans,
|
|
12316
|
+
endedSubAgentSpans
|
|
12317
|
+
);
|
|
12318
|
+
params.options = optionsWithHooks;
|
|
12319
|
+
event.arguments[0] = params;
|
|
11809
12320
|
spans.set(event, {
|
|
11810
|
-
|
|
11811
|
-
|
|
11812
|
-
|
|
11813
|
-
currentMessages: [],
|
|
12321
|
+
accumulatedOutputTokens: 0,
|
|
12322
|
+
activeToolSpans,
|
|
12323
|
+
capturedPromptMessages,
|
|
11814
12324
|
currentMessageId: void 0,
|
|
11815
12325
|
currentMessageStartTime: startTime,
|
|
11816
|
-
|
|
12326
|
+
currentMessages: [],
|
|
12327
|
+
endedSubAgentSpans,
|
|
12328
|
+
finalResults: [],
|
|
12329
|
+
options: optionsWithHooks,
|
|
12330
|
+
originalPrompt,
|
|
12331
|
+
pendingSubAgentNames,
|
|
12332
|
+
processing: Promise.resolve(),
|
|
12333
|
+
promptDone,
|
|
12334
|
+
promptStarted: () => promptStarted,
|
|
12335
|
+
span,
|
|
12336
|
+
subAgentSpans,
|
|
12337
|
+
toolUseToParent
|
|
11817
12338
|
});
|
|
11818
12339
|
},
|
|
11819
|
-
|
|
11820
|
-
const
|
|
11821
|
-
if (!
|
|
12340
|
+
end: (event) => {
|
|
12341
|
+
const state = spans.get(event);
|
|
12342
|
+
if (!state) {
|
|
11822
12343
|
return;
|
|
11823
12344
|
}
|
|
11824
12345
|
const eventResult = event.result;
|
|
11825
12346
|
if (eventResult === void 0) {
|
|
11826
|
-
|
|
12347
|
+
state.span.end();
|
|
11827
12348
|
spans.delete(event);
|
|
11828
12349
|
return;
|
|
11829
12350
|
}
|
|
11830
12351
|
if (isAsyncIterable(eventResult)) {
|
|
11831
12352
|
patchStreamIfNeeded(eventResult, {
|
|
11832
|
-
onChunk:
|
|
11833
|
-
|
|
11834
|
-
|
|
11835
|
-
const prompt = _optionalChain([params, 'optionalAccess', _242 => _242.prompt]);
|
|
11836
|
-
const options = _nullishCoalesce(_optionalChain([params, 'optionalAccess', _243 => _243.options]), () => ( {}));
|
|
11837
|
-
const messageId = _optionalChain([message, 'access', _244 => _244.message, 'optionalAccess', _245 => _245.id]);
|
|
11838
|
-
if (messageId && messageId !== spanData.currentMessageId) {
|
|
11839
|
-
if (spanData.currentMessages.length > 0) {
|
|
11840
|
-
const finalMessage = await createLLMSpanForMessages(
|
|
11841
|
-
spanData.currentMessages,
|
|
11842
|
-
prompt,
|
|
11843
|
-
spanData.conversationHistory,
|
|
11844
|
-
options,
|
|
11845
|
-
spanData.currentMessageStartTime,
|
|
11846
|
-
await spanData.span.export()
|
|
11847
|
-
);
|
|
11848
|
-
if (finalMessage) {
|
|
11849
|
-
spanData.conversationHistory.push(finalMessage);
|
|
11850
|
-
}
|
|
11851
|
-
const lastMessage = spanData.currentMessages[spanData.currentMessages.length - 1];
|
|
11852
|
-
if (_optionalChain([lastMessage, 'optionalAccess', _246 => _246.message, 'optionalAccess', _247 => _247.usage])) {
|
|
11853
|
-
const outputTokens = getNumberProperty(
|
|
11854
|
-
lastMessage.message.usage,
|
|
11855
|
-
"output_tokens"
|
|
11856
|
-
) || 0;
|
|
11857
|
-
spanData.accumulatedOutputTokens += outputTokens;
|
|
11858
|
-
}
|
|
11859
|
-
spanData.currentMessages = [];
|
|
11860
|
-
}
|
|
11861
|
-
spanData.currentMessageId = messageId;
|
|
11862
|
-
spanData.currentMessageStartTime = currentTime;
|
|
11863
|
-
}
|
|
11864
|
-
if (message.type === "assistant" && _optionalChain([message, 'access', _248 => _248.message, 'optionalAccess', _249 => _249.usage])) {
|
|
11865
|
-
spanData.currentMessages.push(message);
|
|
11866
|
-
}
|
|
11867
|
-
if (message.type === "result" && message.usage) {
|
|
11868
|
-
const finalUsageMetrics = extractUsageFromMessage(message);
|
|
11869
|
-
if (spanData.currentMessages.length > 0 && finalUsageMetrics.completion_tokens !== void 0) {
|
|
11870
|
-
const lastMessage = spanData.currentMessages[spanData.currentMessages.length - 1];
|
|
11871
|
-
if (_optionalChain([lastMessage, 'optionalAccess', _250 => _250.message, 'optionalAccess', _251 => _251.usage])) {
|
|
11872
|
-
const adjustedTokens = finalUsageMetrics.completion_tokens - spanData.accumulatedOutputTokens;
|
|
11873
|
-
if (adjustedTokens >= 0) {
|
|
11874
|
-
lastMessage.message.usage.output_tokens = adjustedTokens;
|
|
11875
|
-
}
|
|
11876
|
-
}
|
|
11877
|
-
}
|
|
11878
|
-
const result_metadata = {};
|
|
11879
|
-
if (message.num_turns !== void 0) {
|
|
11880
|
-
result_metadata.num_turns = message.num_turns;
|
|
11881
|
-
}
|
|
11882
|
-
if (message.session_id !== void 0) {
|
|
11883
|
-
result_metadata.session_id = message.session_id;
|
|
11884
|
-
}
|
|
11885
|
-
if (Object.keys(result_metadata).length > 0) {
|
|
11886
|
-
spanData.span.log({
|
|
11887
|
-
metadata: result_metadata
|
|
11888
|
-
});
|
|
11889
|
-
}
|
|
11890
|
-
}
|
|
11891
|
-
},
|
|
11892
|
-
onComplete: async () => {
|
|
11893
|
-
try {
|
|
11894
|
-
const params = event.arguments[0];
|
|
11895
|
-
const prompt = _optionalChain([params, 'optionalAccess', _252 => _252.prompt]);
|
|
11896
|
-
const options = _nullishCoalesce(_optionalChain([params, 'optionalAccess', _253 => _253.options]), () => ( {}));
|
|
11897
|
-
if (spanData.currentMessages.length > 0) {
|
|
11898
|
-
const finalMessage = await createLLMSpanForMessages(
|
|
11899
|
-
spanData.currentMessages,
|
|
11900
|
-
prompt,
|
|
11901
|
-
spanData.conversationHistory,
|
|
11902
|
-
options,
|
|
11903
|
-
spanData.currentMessageStartTime,
|
|
11904
|
-
await spanData.span.export()
|
|
11905
|
-
);
|
|
11906
|
-
if (finalMessage) {
|
|
11907
|
-
spanData.conversationHistory.push(finalMessage);
|
|
11908
|
-
}
|
|
11909
|
-
}
|
|
11910
|
-
spanData.span.log({
|
|
11911
|
-
output: spanData.conversationHistory.length > 0 ? spanData.conversationHistory[spanData.conversationHistory.length - 1] : void 0
|
|
11912
|
-
});
|
|
11913
|
-
} catch (error) {
|
|
12353
|
+
onChunk: (message) => {
|
|
12354
|
+
maybeTrackToolUseContext(state, message);
|
|
12355
|
+
state.processing = state.processing.then(() => handleStreamMessage(state, message)).catch((error) => {
|
|
11914
12356
|
console.error(
|
|
11915
|
-
"Error
|
|
12357
|
+
"Error processing Claude Agent SDK stream chunk:",
|
|
11916
12358
|
error
|
|
11917
12359
|
);
|
|
11918
|
-
}
|
|
11919
|
-
|
|
12360
|
+
});
|
|
12361
|
+
},
|
|
12362
|
+
onComplete: () => {
|
|
12363
|
+
void state.processing.then(() => finalizeQuerySpan(state)).finally(() => {
|
|
11920
12364
|
spans.delete(event);
|
|
11921
|
-
}
|
|
12365
|
+
});
|
|
11922
12366
|
},
|
|
11923
12367
|
onError: (error) => {
|
|
11924
|
-
|
|
11925
|
-
|
|
12368
|
+
void state.processing.then(() => {
|
|
12369
|
+
state.span.log({
|
|
12370
|
+
error: error.message
|
|
12371
|
+
});
|
|
12372
|
+
}).then(() => finalizeQuerySpan(state)).finally(() => {
|
|
12373
|
+
spans.delete(event);
|
|
11926
12374
|
});
|
|
11927
|
-
spanData.span.end();
|
|
11928
|
-
spans.delete(event);
|
|
11929
12375
|
}
|
|
11930
12376
|
});
|
|
11931
|
-
|
|
11932
|
-
|
|
11933
|
-
|
|
11934
|
-
|
|
11935
|
-
|
|
11936
|
-
|
|
11937
|
-
|
|
11938
|
-
|
|
11939
|
-
|
|
11940
|
-
);
|
|
11941
|
-
} finally {
|
|
11942
|
-
spanData.span.end();
|
|
11943
|
-
spans.delete(event);
|
|
11944
|
-
}
|
|
12377
|
+
return;
|
|
12378
|
+
}
|
|
12379
|
+
try {
|
|
12380
|
+
state.span.log({ output: eventResult });
|
|
12381
|
+
} catch (error) {
|
|
12382
|
+
console.error("Error extracting output for Claude Agent SDK:", error);
|
|
12383
|
+
} finally {
|
|
12384
|
+
state.span.end();
|
|
12385
|
+
spans.delete(event);
|
|
11945
12386
|
}
|
|
11946
12387
|
},
|
|
11947
12388
|
error: (event) => {
|
|
11948
|
-
const
|
|
11949
|
-
if (!
|
|
12389
|
+
const state = spans.get(event);
|
|
12390
|
+
if (!state || !event.error) {
|
|
11950
12391
|
return;
|
|
11951
12392
|
}
|
|
11952
|
-
|
|
11953
|
-
span.log({
|
|
12393
|
+
state.span.log({
|
|
11954
12394
|
error: event.error.message
|
|
11955
12395
|
});
|
|
11956
|
-
span.end();
|
|
12396
|
+
state.span.end();
|
|
11957
12397
|
spans.delete(event);
|
|
11958
12398
|
}
|
|
11959
12399
|
};
|
|
@@ -11977,6 +12417,18 @@ var googleGenAIChannels = defineChannels("@google/genai", {
|
|
|
11977
12417
|
});
|
|
11978
12418
|
|
|
11979
12419
|
// src/instrumentation/plugins/google-genai-plugin.ts
|
|
12420
|
+
var GOOGLE_GENAI_INTERNAL_CONTEXT = {
|
|
12421
|
+
caller_filename: "<node-internal>",
|
|
12422
|
+
caller_functionname: "<node-internal>",
|
|
12423
|
+
caller_lineno: 0
|
|
12424
|
+
};
|
|
12425
|
+
function createWrapperParityEvent(args) {
|
|
12426
|
+
return {
|
|
12427
|
+
context: GOOGLE_GENAI_INTERNAL_CONTEXT,
|
|
12428
|
+
input: args.input,
|
|
12429
|
+
metadata: args.metadata
|
|
12430
|
+
};
|
|
12431
|
+
}
|
|
11980
12432
|
var GoogleGenAIPlugin = class extends BasePlugin {
|
|
11981
12433
|
onEnable() {
|
|
11982
12434
|
this.subscribeToGoogleGenAIChannels();
|
|
@@ -11985,51 +12437,282 @@ var GoogleGenAIPlugin = class extends BasePlugin {
|
|
|
11985
12437
|
this.unsubscribers = unsubscribeAll(this.unsubscribers);
|
|
11986
12438
|
}
|
|
11987
12439
|
subscribeToGoogleGenAIChannels() {
|
|
11988
|
-
this.
|
|
11989
|
-
|
|
11990
|
-
|
|
11991
|
-
|
|
11992
|
-
|
|
11993
|
-
|
|
11994
|
-
|
|
11995
|
-
|
|
11996
|
-
|
|
11997
|
-
|
|
11998
|
-
|
|
11999
|
-
|
|
12000
|
-
|
|
12001
|
-
|
|
12002
|
-
|
|
12003
|
-
|
|
12004
|
-
|
|
12005
|
-
|
|
12006
|
-
|
|
12440
|
+
this.subscribeToGenerateContentChannel();
|
|
12441
|
+
this.subscribeToGenerateContentStreamChannel();
|
|
12442
|
+
}
|
|
12443
|
+
subscribeToGenerateContentChannel() {
|
|
12444
|
+
const tracingChannel2 = googleGenAIChannels.generateContent.tracingChannel();
|
|
12445
|
+
const states = /* @__PURE__ */ new WeakMap();
|
|
12446
|
+
const unbindCurrentSpanStore = bindCurrentSpanStoreToStart2(
|
|
12447
|
+
tracingChannel2,
|
|
12448
|
+
states,
|
|
12449
|
+
(event) => {
|
|
12450
|
+
const params = event.arguments[0];
|
|
12451
|
+
const input = serializeInput(params);
|
|
12452
|
+
const metadata = extractMetadata(params);
|
|
12453
|
+
const span = startSpan({
|
|
12454
|
+
name: "generate_content",
|
|
12455
|
+
spanAttributes: {
|
|
12456
|
+
type: "llm" /* LLM */
|
|
12457
|
+
},
|
|
12458
|
+
event: createWrapperParityEvent({ input, metadata })
|
|
12459
|
+
});
|
|
12460
|
+
return {
|
|
12461
|
+
span,
|
|
12462
|
+
startTime: getCurrentUnixTimestamp()
|
|
12463
|
+
};
|
|
12464
|
+
}
|
|
12007
12465
|
);
|
|
12008
|
-
|
|
12009
|
-
|
|
12010
|
-
|
|
12011
|
-
|
|
12012
|
-
extractInput: ([params]) => {
|
|
12466
|
+
const handlers = {
|
|
12467
|
+
start: (event) => {
|
|
12468
|
+
ensureSpanState(states, event, () => {
|
|
12469
|
+
const params = event.arguments[0];
|
|
12013
12470
|
const input = serializeInput(params);
|
|
12014
12471
|
const metadata = extractMetadata(params);
|
|
12472
|
+
const span = startSpan({
|
|
12473
|
+
name: "generate_content",
|
|
12474
|
+
spanAttributes: {
|
|
12475
|
+
type: "llm" /* LLM */
|
|
12476
|
+
},
|
|
12477
|
+
event: createWrapperParityEvent({ input, metadata })
|
|
12478
|
+
});
|
|
12015
12479
|
return {
|
|
12016
|
-
|
|
12017
|
-
|
|
12480
|
+
span,
|
|
12481
|
+
startTime: getCurrentUnixTimestamp()
|
|
12018
12482
|
};
|
|
12019
|
-
}
|
|
12020
|
-
|
|
12021
|
-
|
|
12022
|
-
|
|
12023
|
-
|
|
12024
|
-
return
|
|
12025
|
-
},
|
|
12026
|
-
aggregateChunks: (chunks, _result, _endEvent, startTime) => {
|
|
12027
|
-
return aggregateGenerateContentChunks(chunks, startTime);
|
|
12483
|
+
});
|
|
12484
|
+
},
|
|
12485
|
+
asyncEnd: (event) => {
|
|
12486
|
+
const spanState = states.get(event);
|
|
12487
|
+
if (!spanState) {
|
|
12488
|
+
return;
|
|
12028
12489
|
}
|
|
12029
|
-
|
|
12030
|
-
|
|
12490
|
+
try {
|
|
12491
|
+
spanState.span.log({
|
|
12492
|
+
metrics: cleanMetrics(
|
|
12493
|
+
extractGenerateContentMetrics(
|
|
12494
|
+
event.result,
|
|
12495
|
+
spanState.startTime
|
|
12496
|
+
)
|
|
12497
|
+
),
|
|
12498
|
+
output: event.result
|
|
12499
|
+
});
|
|
12500
|
+
} finally {
|
|
12501
|
+
spanState.span.end();
|
|
12502
|
+
states.delete(event);
|
|
12503
|
+
}
|
|
12504
|
+
},
|
|
12505
|
+
error: (event) => {
|
|
12506
|
+
logErrorAndEndSpan(states, event);
|
|
12507
|
+
}
|
|
12508
|
+
};
|
|
12509
|
+
tracingChannel2.subscribe(handlers);
|
|
12510
|
+
this.unsubscribers.push(() => {
|
|
12511
|
+
_optionalChain([unbindCurrentSpanStore, 'optionalCall', _269 => _269()]);
|
|
12512
|
+
tracingChannel2.unsubscribe(handlers);
|
|
12513
|
+
});
|
|
12514
|
+
}
|
|
12515
|
+
subscribeToGenerateContentStreamChannel() {
|
|
12516
|
+
const tracingChannel2 = googleGenAIChannels.generateContentStream.tracingChannel();
|
|
12517
|
+
const handlers = {
|
|
12518
|
+
start: (event) => {
|
|
12519
|
+
const streamEvent = event;
|
|
12520
|
+
const params = event.arguments[0];
|
|
12521
|
+
streamEvent.googleGenAIInput = serializeInput(params);
|
|
12522
|
+
streamEvent.googleGenAIMetadata = extractMetadata(params);
|
|
12523
|
+
},
|
|
12524
|
+
asyncEnd: (event) => {
|
|
12525
|
+
const streamEvent = event;
|
|
12526
|
+
patchGoogleGenAIStreamingResult({
|
|
12527
|
+
input: streamEvent.googleGenAIInput,
|
|
12528
|
+
metadata: streamEvent.googleGenAIMetadata,
|
|
12529
|
+
result: streamEvent.result
|
|
12530
|
+
});
|
|
12531
|
+
},
|
|
12532
|
+
error: () => {
|
|
12533
|
+
}
|
|
12534
|
+
};
|
|
12535
|
+
tracingChannel2.subscribe(handlers);
|
|
12536
|
+
this.unsubscribers.push(() => {
|
|
12537
|
+
tracingChannel2.unsubscribe(handlers);
|
|
12538
|
+
});
|
|
12031
12539
|
}
|
|
12032
12540
|
};
|
|
12541
|
+
function ensureSpanState(states, event, create) {
|
|
12542
|
+
const existing = states.get(event);
|
|
12543
|
+
if (existing) {
|
|
12544
|
+
return existing;
|
|
12545
|
+
}
|
|
12546
|
+
const created = create();
|
|
12547
|
+
states.set(event, created);
|
|
12548
|
+
return created;
|
|
12549
|
+
}
|
|
12550
|
+
function bindCurrentSpanStoreToStart2(tracingChannel2, states, create) {
|
|
12551
|
+
const state = _internalGetGlobalState();
|
|
12552
|
+
const startChannel = tracingChannel2.start;
|
|
12553
|
+
const currentSpanStore = _optionalChain([state, 'optionalAccess', _270 => _270.contextManager]) ? state.contextManager[BRAINTRUST_CURRENT_SPAN_STORE] : void 0;
|
|
12554
|
+
if (!_optionalChain([startChannel, 'optionalAccess', _271 => _271.bindStore]) || !currentSpanStore) {
|
|
12555
|
+
return void 0;
|
|
12556
|
+
}
|
|
12557
|
+
startChannel.bindStore(
|
|
12558
|
+
currentSpanStore,
|
|
12559
|
+
(event) => ensureSpanState(
|
|
12560
|
+
states,
|
|
12561
|
+
event,
|
|
12562
|
+
() => create(event)
|
|
12563
|
+
).span
|
|
12564
|
+
);
|
|
12565
|
+
return () => {
|
|
12566
|
+
_optionalChain([startChannel, 'access', _272 => _272.unbindStore, 'optionalCall', _273 => _273(currentSpanStore)]);
|
|
12567
|
+
};
|
|
12568
|
+
}
|
|
12569
|
+
function logErrorAndEndSpan(states, event) {
|
|
12570
|
+
const spanState = states.get(event);
|
|
12571
|
+
if (!spanState) {
|
|
12572
|
+
return;
|
|
12573
|
+
}
|
|
12574
|
+
spanState.span.log({
|
|
12575
|
+
error: event.error.message
|
|
12576
|
+
});
|
|
12577
|
+
spanState.span.end();
|
|
12578
|
+
states.delete(event);
|
|
12579
|
+
}
|
|
12580
|
+
function patchGoogleGenAIStreamingResult(args) {
|
|
12581
|
+
const { input, metadata, result } = args;
|
|
12582
|
+
if (!input || !metadata || !result || typeof result !== "object" || typeof result.next !== "function") {
|
|
12583
|
+
return false;
|
|
12584
|
+
}
|
|
12585
|
+
const chunks = [];
|
|
12586
|
+
let firstTokenTime = null;
|
|
12587
|
+
let finalized = false;
|
|
12588
|
+
let span = null;
|
|
12589
|
+
let startTime = null;
|
|
12590
|
+
const ensureSpan = () => {
|
|
12591
|
+
if (!span) {
|
|
12592
|
+
span = startSpan({
|
|
12593
|
+
name: "generate_content_stream",
|
|
12594
|
+
spanAttributes: {
|
|
12595
|
+
type: "llm" /* LLM */
|
|
12596
|
+
},
|
|
12597
|
+
event: {
|
|
12598
|
+
input,
|
|
12599
|
+
metadata
|
|
12600
|
+
}
|
|
12601
|
+
});
|
|
12602
|
+
startTime = getCurrentUnixTimestamp();
|
|
12603
|
+
}
|
|
12604
|
+
return span;
|
|
12605
|
+
};
|
|
12606
|
+
const finalize = (options) => {
|
|
12607
|
+
if (finalized || !span) {
|
|
12608
|
+
return;
|
|
12609
|
+
}
|
|
12610
|
+
finalized = true;
|
|
12611
|
+
if (options.result) {
|
|
12612
|
+
const { end, ...metricsWithoutEnd } = options.result.metrics;
|
|
12613
|
+
span.log({
|
|
12614
|
+
metrics: cleanMetrics(metricsWithoutEnd),
|
|
12615
|
+
output: options.result.aggregated
|
|
12616
|
+
});
|
|
12617
|
+
span.end(typeof end === "number" ? { endTime: end } : void 0);
|
|
12618
|
+
return;
|
|
12619
|
+
}
|
|
12620
|
+
if (options.error !== void 0) {
|
|
12621
|
+
span.log({
|
|
12622
|
+
error: options.error instanceof Error ? options.error.message : String(options.error)
|
|
12623
|
+
});
|
|
12624
|
+
}
|
|
12625
|
+
span.end();
|
|
12626
|
+
};
|
|
12627
|
+
const patchIterator = (iterator) => {
|
|
12628
|
+
if (typeof iterator !== "object" || iterator === null || "__braintrustGoogleGenAIPatched" in iterator) {
|
|
12629
|
+
return iterator;
|
|
12630
|
+
}
|
|
12631
|
+
const iteratorRecord = iterator;
|
|
12632
|
+
const originalNext = typeof iteratorRecord.next === "function" ? iteratorRecord.next.bind(iterator) : void 0;
|
|
12633
|
+
const originalReturn = typeof iteratorRecord.return === "function" ? iteratorRecord.return.bind(iterator) : void 0;
|
|
12634
|
+
const originalThrow = typeof iteratorRecord.throw === "function" ? iteratorRecord.throw.bind(iterator) : void 0;
|
|
12635
|
+
const asyncIteratorMethod = iteratorRecord[Symbol.asyncIterator];
|
|
12636
|
+
const originalAsyncIterator = typeof asyncIteratorMethod === "function" ? asyncIteratorMethod.bind(iterator) : void 0;
|
|
12637
|
+
Object.defineProperty(iteratorRecord, "__braintrustGoogleGenAIPatched", {
|
|
12638
|
+
configurable: true,
|
|
12639
|
+
enumerable: false,
|
|
12640
|
+
value: true,
|
|
12641
|
+
writable: false
|
|
12642
|
+
});
|
|
12643
|
+
if (originalNext) {
|
|
12644
|
+
iteratorRecord.next = async (...nextArgs) => {
|
|
12645
|
+
ensureSpan();
|
|
12646
|
+
try {
|
|
12647
|
+
const nextResult = await originalNext(
|
|
12648
|
+
...nextArgs
|
|
12649
|
+
);
|
|
12650
|
+
if (!nextResult.done && nextResult.value) {
|
|
12651
|
+
if (firstTokenTime === null) {
|
|
12652
|
+
firstTokenTime = getCurrentUnixTimestamp();
|
|
12653
|
+
}
|
|
12654
|
+
chunks.push(nextResult.value);
|
|
12655
|
+
}
|
|
12656
|
+
if (nextResult.done && startTime !== null) {
|
|
12657
|
+
finalize({
|
|
12658
|
+
result: aggregateGenerateContentChunks(
|
|
12659
|
+
chunks,
|
|
12660
|
+
startTime,
|
|
12661
|
+
firstTokenTime
|
|
12662
|
+
)
|
|
12663
|
+
});
|
|
12664
|
+
}
|
|
12665
|
+
return nextResult;
|
|
12666
|
+
} catch (error) {
|
|
12667
|
+
finalize({ error });
|
|
12668
|
+
throw error;
|
|
12669
|
+
}
|
|
12670
|
+
};
|
|
12671
|
+
}
|
|
12672
|
+
if (originalReturn) {
|
|
12673
|
+
iteratorRecord.return = async (...returnArgs) => {
|
|
12674
|
+
ensureSpan();
|
|
12675
|
+
try {
|
|
12676
|
+
return await originalReturn(
|
|
12677
|
+
...returnArgs
|
|
12678
|
+
);
|
|
12679
|
+
} finally {
|
|
12680
|
+
if (startTime !== null) {
|
|
12681
|
+
finalize({
|
|
12682
|
+
result: chunks.length > 0 ? aggregateGenerateContentChunks(
|
|
12683
|
+
chunks,
|
|
12684
|
+
startTime,
|
|
12685
|
+
firstTokenTime
|
|
12686
|
+
) : void 0
|
|
12687
|
+
});
|
|
12688
|
+
} else {
|
|
12689
|
+
finalize({});
|
|
12690
|
+
}
|
|
12691
|
+
}
|
|
12692
|
+
};
|
|
12693
|
+
}
|
|
12694
|
+
if (originalThrow) {
|
|
12695
|
+
iteratorRecord.throw = async (...throwArgs) => {
|
|
12696
|
+
ensureSpan();
|
|
12697
|
+
try {
|
|
12698
|
+
return await originalThrow(
|
|
12699
|
+
...throwArgs
|
|
12700
|
+
);
|
|
12701
|
+
} catch (error) {
|
|
12702
|
+
finalize({ error });
|
|
12703
|
+
throw error;
|
|
12704
|
+
}
|
|
12705
|
+
};
|
|
12706
|
+
}
|
|
12707
|
+
iteratorRecord[Symbol.asyncIterator] = () => {
|
|
12708
|
+
const asyncIterator = originalAsyncIterator ? originalAsyncIterator() : iterator;
|
|
12709
|
+
return patchIterator(asyncIterator);
|
|
12710
|
+
};
|
|
12711
|
+
return iterator;
|
|
12712
|
+
};
|
|
12713
|
+
patchIterator(result);
|
|
12714
|
+
return true;
|
|
12715
|
+
}
|
|
12033
12716
|
function serializeInput(params) {
|
|
12034
12717
|
const input = {
|
|
12035
12718
|
model: params.model,
|
|
@@ -12038,11 +12721,13 @@ function serializeInput(params) {
|
|
|
12038
12721
|
if (params.config) {
|
|
12039
12722
|
const config = tryToDict(params.config);
|
|
12040
12723
|
if (config) {
|
|
12041
|
-
const
|
|
12042
|
-
|
|
12043
|
-
|
|
12044
|
-
|
|
12045
|
-
|
|
12724
|
+
const filteredConfig = {};
|
|
12725
|
+
Object.keys(config).forEach((key) => {
|
|
12726
|
+
if (key !== "tools") {
|
|
12727
|
+
filteredConfig[key] = config[key];
|
|
12728
|
+
}
|
|
12729
|
+
});
|
|
12730
|
+
input.config = filteredConfig;
|
|
12046
12731
|
}
|
|
12047
12732
|
}
|
|
12048
12733
|
return input;
|
|
@@ -12100,7 +12785,7 @@ function serializePart(part) {
|
|
|
12100
12785
|
return part;
|
|
12101
12786
|
}
|
|
12102
12787
|
function serializeTools(params) {
|
|
12103
|
-
if (!_optionalChain([params, 'access',
|
|
12788
|
+
if (!_optionalChain([params, 'access', _274 => _274.config, 'optionalAccess', _275 => _275.tools])) {
|
|
12104
12789
|
return null;
|
|
12105
12790
|
}
|
|
12106
12791
|
try {
|
|
@@ -12110,7 +12795,7 @@ function serializeTools(params) {
|
|
|
12110
12795
|
}
|
|
12111
12796
|
return tool;
|
|
12112
12797
|
});
|
|
12113
|
-
} catch (
|
|
12798
|
+
} catch (e29) {
|
|
12114
12799
|
return null;
|
|
12115
12800
|
}
|
|
12116
12801
|
}
|
|
@@ -12129,15 +12814,21 @@ function extractMetadata(params) {
|
|
|
12129
12814
|
});
|
|
12130
12815
|
}
|
|
12131
12816
|
}
|
|
12817
|
+
const tools = serializeTools(params);
|
|
12818
|
+
if (tools) {
|
|
12819
|
+
metadata.tools = tools;
|
|
12820
|
+
}
|
|
12132
12821
|
return metadata;
|
|
12133
12822
|
}
|
|
12134
12823
|
function extractGenerateContentMetrics(response, startTime) {
|
|
12135
12824
|
const metrics = {};
|
|
12136
|
-
if (startTime) {
|
|
12825
|
+
if (startTime !== void 0) {
|
|
12137
12826
|
const end = getCurrentUnixTimestamp();
|
|
12827
|
+
metrics.start = startTime;
|
|
12828
|
+
metrics.end = end;
|
|
12138
12829
|
metrics.duration = end - startTime;
|
|
12139
12830
|
}
|
|
12140
|
-
if (_optionalChain([response, 'optionalAccess',
|
|
12831
|
+
if (_optionalChain([response, 'optionalAccess', _276 => _276.usageMetadata])) {
|
|
12141
12832
|
populateUsageMetrics(metrics, response.usageMetadata);
|
|
12142
12833
|
}
|
|
12143
12834
|
return metrics;
|
|
@@ -12159,19 +12850,18 @@ function populateUsageMetrics(metrics, usage) {
|
|
|
12159
12850
|
metrics.completion_reasoning_tokens = usage.thoughtsTokenCount;
|
|
12160
12851
|
}
|
|
12161
12852
|
}
|
|
12162
|
-
function aggregateGenerateContentChunks(chunks, startTime) {
|
|
12163
|
-
const
|
|
12164
|
-
|
|
12165
|
-
|
|
12166
|
-
|
|
12167
|
-
|
|
12168
|
-
|
|
12169
|
-
if (
|
|
12170
|
-
firstTokenTime = getCurrentUnixTimestamp();
|
|
12853
|
+
function aggregateGenerateContentChunks(chunks, startTime, firstTokenTime) {
|
|
12854
|
+
const end = getCurrentUnixTimestamp();
|
|
12855
|
+
const metrics = {
|
|
12856
|
+
start: startTime,
|
|
12857
|
+
end,
|
|
12858
|
+
duration: end - startTime
|
|
12859
|
+
};
|
|
12860
|
+
if (firstTokenTime !== null) {
|
|
12171
12861
|
metrics.time_to_first_token = firstTokenTime - startTime;
|
|
12172
12862
|
}
|
|
12173
12863
|
if (chunks.length === 0) {
|
|
12174
|
-
return {
|
|
12864
|
+
return { aggregated: {}, metrics };
|
|
12175
12865
|
}
|
|
12176
12866
|
let text = "";
|
|
12177
12867
|
let thoughtText = "";
|
|
@@ -12185,7 +12875,7 @@ function aggregateGenerateContentChunks(chunks, startTime) {
|
|
|
12185
12875
|
}
|
|
12186
12876
|
if (chunk.candidates && Array.isArray(chunk.candidates)) {
|
|
12187
12877
|
for (const candidate of chunk.candidates) {
|
|
12188
|
-
if (_optionalChain([candidate, 'access',
|
|
12878
|
+
if (_optionalChain([candidate, 'access', _277 => _277.content, 'optionalAccess', _278 => _278.parts])) {
|
|
12189
12879
|
for (const part of candidate.content.parts) {
|
|
12190
12880
|
if (part.text !== void 0) {
|
|
12191
12881
|
if (part.thought) {
|
|
@@ -12207,7 +12897,7 @@ function aggregateGenerateContentChunks(chunks, startTime) {
|
|
|
12207
12897
|
}
|
|
12208
12898
|
}
|
|
12209
12899
|
}
|
|
12210
|
-
const
|
|
12900
|
+
const aggregated = {};
|
|
12211
12901
|
const parts = [];
|
|
12212
12902
|
if (thoughtText) {
|
|
12213
12903
|
parts.push({ text: thoughtText, thought: true });
|
|
@@ -12216,7 +12906,7 @@ function aggregateGenerateContentChunks(chunks, startTime) {
|
|
|
12216
12906
|
parts.push({ text });
|
|
12217
12907
|
}
|
|
12218
12908
|
parts.push(...otherParts);
|
|
12219
|
-
if (parts.length > 0 && _optionalChain([lastResponse, 'optionalAccess',
|
|
12909
|
+
if (parts.length > 0 && _optionalChain([lastResponse, 'optionalAccess', _279 => _279.candidates])) {
|
|
12220
12910
|
const candidates = [];
|
|
12221
12911
|
for (const candidate of lastResponse.candidates) {
|
|
12222
12912
|
const candidateDict = {
|
|
@@ -12233,16 +12923,25 @@ function aggregateGenerateContentChunks(chunks, startTime) {
|
|
|
12233
12923
|
}
|
|
12234
12924
|
candidates.push(candidateDict);
|
|
12235
12925
|
}
|
|
12236
|
-
|
|
12926
|
+
aggregated.candidates = candidates;
|
|
12237
12927
|
}
|
|
12238
12928
|
if (usageMetadata) {
|
|
12239
|
-
|
|
12929
|
+
aggregated.usageMetadata = usageMetadata;
|
|
12240
12930
|
populateUsageMetrics(metrics, usageMetadata);
|
|
12241
12931
|
}
|
|
12242
12932
|
if (text) {
|
|
12243
|
-
|
|
12933
|
+
aggregated.text = text;
|
|
12934
|
+
}
|
|
12935
|
+
return { aggregated, metrics };
|
|
12936
|
+
}
|
|
12937
|
+
function cleanMetrics(metrics) {
|
|
12938
|
+
const cleaned = {};
|
|
12939
|
+
for (const [key, value] of Object.entries(metrics)) {
|
|
12940
|
+
if (value !== null && value !== void 0) {
|
|
12941
|
+
cleaned[key] = value;
|
|
12942
|
+
}
|
|
12244
12943
|
}
|
|
12245
|
-
return
|
|
12944
|
+
return cleaned;
|
|
12246
12945
|
}
|
|
12247
12946
|
function tryToDict(obj) {
|
|
12248
12947
|
if (obj === null || obj === void 0) {
|
|
@@ -12375,7 +13074,7 @@ function isZodSchema2(value) {
|
|
|
12375
13074
|
function serializeZodSchema2(schema) {
|
|
12376
13075
|
try {
|
|
12377
13076
|
return zodToJsonSchema(schema);
|
|
12378
|
-
} catch (
|
|
13077
|
+
} catch (e30) {
|
|
12379
13078
|
return {
|
|
12380
13079
|
type: "object",
|
|
12381
13080
|
description: "Zod schema (conversion failed)"
|
|
@@ -12597,7 +13296,7 @@ function patchOpenRouterCallModelResult(span, result, request) {
|
|
|
12597
13296
|
}
|
|
12598
13297
|
try {
|
|
12599
13298
|
await endSpanWithResult(await originalGetResponse(), fallbackOutput);
|
|
12600
|
-
} catch (
|
|
13299
|
+
} catch (e31) {
|
|
12601
13300
|
await endSpanWithResult(void 0, fallbackOutput);
|
|
12602
13301
|
}
|
|
12603
13302
|
};
|
|
@@ -12813,7 +13512,7 @@ function publishToolResult(tracingChannel2, event, result) {
|
|
|
12813
13512
|
}
|
|
12814
13513
|
function getToolCallId(context) {
|
|
12815
13514
|
const toolContext = context;
|
|
12816
|
-
return typeof _optionalChain([toolContext, 'optionalAccess',
|
|
13515
|
+
return typeof _optionalChain([toolContext, 'optionalAccess', _280 => _280.toolCall, 'optionalAccess', _281 => _281.id]) === "string" ? toolContext.toolCall.id : void 0;
|
|
12817
13516
|
}
|
|
12818
13517
|
function extractOpenRouterCallModelResultMetadata(response, turnCount) {
|
|
12819
13518
|
const combined = {
|
|
@@ -12997,9 +13696,9 @@ var OpenRouterPlugin = class extends BasePlugin {
|
|
|
12997
13696
|
type: "llm" /* LLM */,
|
|
12998
13697
|
extractInput: (args) => {
|
|
12999
13698
|
const request = getOpenRouterRequestArg(args);
|
|
13000
|
-
const chatGenerationParams = isObject(_optionalChain([request, 'optionalAccess',
|
|
13001
|
-
const httpReferer = _optionalChain([request, 'optionalAccess',
|
|
13002
|
-
const xTitle = _optionalChain([request, 'optionalAccess',
|
|
13699
|
+
const chatGenerationParams = isObject(_optionalChain([request, 'optionalAccess', _282 => _282.chatGenerationParams])) ? request.chatGenerationParams : {};
|
|
13700
|
+
const httpReferer = _optionalChain([request, 'optionalAccess', _283 => _283.httpReferer]);
|
|
13701
|
+
const xTitle = _optionalChain([request, 'optionalAccess', _284 => _284.xTitle]);
|
|
13003
13702
|
const { messages, ...metadata } = chatGenerationParams;
|
|
13004
13703
|
return {
|
|
13005
13704
|
input: messages,
|
|
@@ -13010,7 +13709,7 @@ var OpenRouterPlugin = class extends BasePlugin {
|
|
|
13010
13709
|
return isObject(result) ? result.choices : void 0;
|
|
13011
13710
|
},
|
|
13012
13711
|
extractMetrics: (result, startTime) => {
|
|
13013
|
-
const metrics = parseOpenRouterMetricsFromUsage(_optionalChain([result, 'optionalAccess',
|
|
13712
|
+
const metrics = parseOpenRouterMetricsFromUsage(_optionalChain([result, 'optionalAccess', _285 => _285.usage]));
|
|
13014
13713
|
if (startTime) {
|
|
13015
13714
|
metrics.time_to_first_token = getCurrentUnixTimestamp() - startTime;
|
|
13016
13715
|
}
|
|
@@ -13025,9 +13724,9 @@ var OpenRouterPlugin = class extends BasePlugin {
|
|
|
13025
13724
|
type: "llm" /* LLM */,
|
|
13026
13725
|
extractInput: (args) => {
|
|
13027
13726
|
const request = getOpenRouterRequestArg(args);
|
|
13028
|
-
const requestBody = isObject(_optionalChain([request, 'optionalAccess',
|
|
13029
|
-
const httpReferer = _optionalChain([request, 'optionalAccess',
|
|
13030
|
-
const xTitle = _optionalChain([request, 'optionalAccess',
|
|
13727
|
+
const requestBody = isObject(_optionalChain([request, 'optionalAccess', _286 => _286.requestBody])) ? request.requestBody : {};
|
|
13728
|
+
const httpReferer = _optionalChain([request, 'optionalAccess', _287 => _287.httpReferer]);
|
|
13729
|
+
const xTitle = _optionalChain([request, 'optionalAccess', _288 => _288.xTitle]);
|
|
13031
13730
|
const { input, ...metadata } = requestBody;
|
|
13032
13731
|
return {
|
|
13033
13732
|
input,
|
|
@@ -13042,7 +13741,7 @@ var OpenRouterPlugin = class extends BasePlugin {
|
|
|
13042
13741
|
if (!isObject(result)) {
|
|
13043
13742
|
return void 0;
|
|
13044
13743
|
}
|
|
13045
|
-
const embedding = _optionalChain([result, 'access',
|
|
13744
|
+
const embedding = _optionalChain([result, 'access', _289 => _289.data, 'optionalAccess', _290 => _290[0], 'optionalAccess', _291 => _291.embedding]);
|
|
13046
13745
|
return Array.isArray(embedding) ? { embedding_length: embedding.length } : void 0;
|
|
13047
13746
|
},
|
|
13048
13747
|
extractMetadata: (result) => {
|
|
@@ -13062,9 +13761,9 @@ var OpenRouterPlugin = class extends BasePlugin {
|
|
|
13062
13761
|
type: "llm" /* LLM */,
|
|
13063
13762
|
extractInput: (args) => {
|
|
13064
13763
|
const request = getOpenRouterRequestArg(args);
|
|
13065
|
-
const openResponsesRequest = isObject(_optionalChain([request, 'optionalAccess',
|
|
13066
|
-
const httpReferer = _optionalChain([request, 'optionalAccess',
|
|
13067
|
-
const xTitle = _optionalChain([request, 'optionalAccess',
|
|
13764
|
+
const openResponsesRequest = isObject(_optionalChain([request, 'optionalAccess', _292 => _292.openResponsesRequest])) ? request.openResponsesRequest : {};
|
|
13765
|
+
const httpReferer = _optionalChain([request, 'optionalAccess', _293 => _293.httpReferer]);
|
|
13766
|
+
const xTitle = _optionalChain([request, 'optionalAccess', _294 => _294.xTitle]);
|
|
13068
13767
|
const { input, ...metadata } = openResponsesRequest;
|
|
13069
13768
|
return {
|
|
13070
13769
|
input,
|
|
@@ -13074,7 +13773,7 @@ var OpenRouterPlugin = class extends BasePlugin {
|
|
|
13074
13773
|
extractOutput: (result) => extractOpenRouterResponseOutput(result),
|
|
13075
13774
|
extractMetadata: (result) => extractOpenRouterResponseMetadata(result),
|
|
13076
13775
|
extractMetrics: (result, startTime) => {
|
|
13077
|
-
const metrics = parseOpenRouterMetricsFromUsage(_optionalChain([result, 'optionalAccess',
|
|
13776
|
+
const metrics = parseOpenRouterMetricsFromUsage(_optionalChain([result, 'optionalAccess', _295 => _295.usage]));
|
|
13078
13777
|
if (startTime) {
|
|
13079
13778
|
metrics.time_to_first_token = getCurrentUnixTimestamp() - startTime;
|
|
13080
13779
|
}
|
|
@@ -13175,12 +13874,12 @@ function aggregateOpenRouterChatChunks(chunks) {
|
|
|
13175
13874
|
for (const chunk of chunks) {
|
|
13176
13875
|
metrics = {
|
|
13177
13876
|
...metrics,
|
|
13178
|
-
...parseOpenRouterMetricsFromUsage(_optionalChain([chunk, 'optionalAccess',
|
|
13877
|
+
...parseOpenRouterMetricsFromUsage(_optionalChain([chunk, 'optionalAccess', _296 => _296.usage]))
|
|
13179
13878
|
};
|
|
13180
|
-
const choice = _optionalChain([chunk, 'optionalAccess',
|
|
13181
|
-
const delta = _optionalChain([choice, 'optionalAccess',
|
|
13879
|
+
const choice = _optionalChain([chunk, 'optionalAccess', _297 => _297.choices, 'optionalAccess', _298 => _298[0]]);
|
|
13880
|
+
const delta = _optionalChain([choice, 'optionalAccess', _299 => _299.delta]);
|
|
13182
13881
|
if (!delta) {
|
|
13183
|
-
if (_optionalChain([choice, 'optionalAccess',
|
|
13882
|
+
if (_optionalChain([choice, 'optionalAccess', _300 => _300.finish_reason]) !== void 0) {
|
|
13184
13883
|
finishReason = choice.finish_reason;
|
|
13185
13884
|
}
|
|
13186
13885
|
continue;
|
|
@@ -13191,7 +13890,7 @@ function aggregateOpenRouterChatChunks(chunks) {
|
|
|
13191
13890
|
if (typeof delta.content === "string") {
|
|
13192
13891
|
content += delta.content;
|
|
13193
13892
|
}
|
|
13194
|
-
const choiceFinishReason = _nullishCoalesce(_nullishCoalesce(_optionalChain([choice, 'optionalAccess',
|
|
13893
|
+
const choiceFinishReason = _nullishCoalesce(_nullishCoalesce(_optionalChain([choice, 'optionalAccess', _301 => _301.finishReason]), () => ( _optionalChain([choice, 'optionalAccess', _302 => _302.finish_reason]))), () => ( void 0));
|
|
13195
13894
|
const deltaFinishReason = _nullishCoalesce(_nullishCoalesce(delta.finishReason, () => ( delta.finish_reason)), () => ( void 0));
|
|
13196
13895
|
if (choiceFinishReason !== void 0) {
|
|
13197
13896
|
finishReason = choiceFinishReason;
|
|
@@ -13203,11 +13902,11 @@ function aggregateOpenRouterChatChunks(chunks) {
|
|
|
13203
13902
|
continue;
|
|
13204
13903
|
}
|
|
13205
13904
|
for (const toolDelta of toolCallDeltas) {
|
|
13206
|
-
if (!_optionalChain([toolDelta, 'optionalAccess',
|
|
13905
|
+
if (!_optionalChain([toolDelta, 'optionalAccess', _303 => _303.function])) {
|
|
13207
13906
|
continue;
|
|
13208
13907
|
}
|
|
13209
13908
|
const toolIndex = _nullishCoalesce(toolDelta.index, () => ( 0));
|
|
13210
|
-
const existingToolCall = _optionalChain([toolCalls, 'optionalAccess',
|
|
13909
|
+
const existingToolCall = _optionalChain([toolCalls, 'optionalAccess', _304 => _304[toolIndex]]);
|
|
13211
13910
|
if (!existingToolCall || toolDelta.id && existingToolCall.id !== void 0 && existingToolCall.id !== toolDelta.id) {
|
|
13212
13911
|
const nextToolCalls = [...toolCalls || []];
|
|
13213
13912
|
nextToolCalls[toolIndex] = {
|
|
@@ -13254,7 +13953,7 @@ function aggregateOpenRouterChatChunks(chunks) {
|
|
|
13254
13953
|
function aggregateOpenRouterResponseStreamEvents(chunks) {
|
|
13255
13954
|
let finalResponse;
|
|
13256
13955
|
for (const chunk of chunks) {
|
|
13257
|
-
const response = _optionalChain([chunk, 'optionalAccess',
|
|
13956
|
+
const response = _optionalChain([chunk, 'optionalAccess', _305 => _305.response]);
|
|
13258
13957
|
if (!response) {
|
|
13259
13958
|
continue;
|
|
13260
13959
|
}
|
|
@@ -14666,7 +15365,7 @@ var CachedSpanFetcher = (_class20 = class {
|
|
|
14666
15365
|
spanType
|
|
14667
15366
|
);
|
|
14668
15367
|
const rows = await fetcher.fetchedData();
|
|
14669
|
-
return rows.filter((row) => _optionalChain([row, 'access',
|
|
15368
|
+
return rows.filter((row) => _optionalChain([row, 'access', _306 => _306.span_attributes, 'optionalAccess', _307 => _307.purpose]) !== "scorer").map((row) => ({
|
|
14670
15369
|
input: row.input,
|
|
14671
15370
|
output: row.output,
|
|
14672
15371
|
metadata: row.metadata,
|
|
@@ -14700,7 +15399,7 @@ var CachedSpanFetcher = (_class20 = class {
|
|
|
14700
15399
|
async fetchSpans(spanType) {
|
|
14701
15400
|
const spans = await this.fetchFn(spanType);
|
|
14702
15401
|
for (const span of spans) {
|
|
14703
|
-
const type = _nullishCoalesce(_optionalChain([span, 'access',
|
|
15402
|
+
const type = _nullishCoalesce(_optionalChain([span, 'access', _308 => _308.span_attributes, 'optionalAccess', _309 => _309.type]), () => ( ""));
|
|
14704
15403
|
const existing = _nullishCoalesce(this.spanCache.get(type), () => ( []));
|
|
14705
15404
|
existing.push(span);
|
|
14706
15405
|
this.spanCache.set(type, existing);
|
|
@@ -14780,11 +15479,11 @@ var LocalTrace = (_class21 = class {
|
|
|
14780
15479
|
const cachedSpans = this.state.spanCache.getByRootSpanId(this.rootSpanId);
|
|
14781
15480
|
if (cachedSpans && cachedSpans.length > 0) {
|
|
14782
15481
|
let spans = cachedSpans.filter(
|
|
14783
|
-
(span) => _optionalChain([span, 'access',
|
|
15482
|
+
(span) => _optionalChain([span, 'access', _310 => _310.span_attributes, 'optionalAccess', _311 => _311.purpose]) !== "scorer"
|
|
14784
15483
|
);
|
|
14785
15484
|
if (spanType && spanType.length > 0) {
|
|
14786
15485
|
spans = spans.filter(
|
|
14787
|
-
(span) => spanType.includes(_nullishCoalesce(_optionalChain([span, 'access',
|
|
15486
|
+
(span) => spanType.includes(_nullishCoalesce(_optionalChain([span, 'access', _312 => _312.span_attributes, 'optionalAccess', _313 => _313.type]), () => ( "")))
|
|
14788
15487
|
);
|
|
14789
15488
|
}
|
|
14790
15489
|
return spans.map((span) => ({
|
|
@@ -14803,7 +15502,7 @@ var LocalTrace = (_class21 = class {
|
|
|
14803
15502
|
* Calls the API with the project_default preprocessor (which falls back to "thread").
|
|
14804
15503
|
*/
|
|
14805
15504
|
async getThread(options) {
|
|
14806
|
-
const cacheKey = _nullishCoalesce(_optionalChain([options, 'optionalAccess',
|
|
15505
|
+
const cacheKey = _nullishCoalesce(_optionalChain([options, 'optionalAccess', _314 => _314.preprocessor]), () => ( "project_default"));
|
|
14807
15506
|
if (!this.threadCache.has(cacheKey)) {
|
|
14808
15507
|
const promise = this.fetchThread(options);
|
|
14809
15508
|
this.threadCache.set(cacheKey, promise);
|
|
@@ -14814,7 +15513,7 @@ var LocalTrace = (_class21 = class {
|
|
|
14814
15513
|
await this.ensureSpansReady();
|
|
14815
15514
|
await this.state.login({});
|
|
14816
15515
|
const result = await invoke({
|
|
14817
|
-
globalFunction: _nullishCoalesce(_optionalChain([options, 'optionalAccess',
|
|
15516
|
+
globalFunction: _nullishCoalesce(_optionalChain([options, 'optionalAccess', _315 => _315.preprocessor]), () => ( "project_default")),
|
|
14818
15517
|
functionType: "preprocessor",
|
|
14819
15518
|
input: {
|
|
14820
15519
|
trace_ref: {
|
|
@@ -14990,10 +15689,10 @@ function validateParametersWithJsonSchema(parameters, schema) {
|
|
|
14990
15689
|
const ajv = new (0, _ajv2.default)({ coerceTypes: true, useDefaults: true, strict: false });
|
|
14991
15690
|
const validate = ajv.compile(schema);
|
|
14992
15691
|
if (!validate(parameters)) {
|
|
14993
|
-
const errorMessages = _optionalChain([validate, 'access',
|
|
15692
|
+
const errorMessages = _optionalChain([validate, 'access', _316 => _316.errors, 'optionalAccess', _317 => _317.map, 'call', _318 => _318((err) => {
|
|
14994
15693
|
const path2 = err.instancePath || "root";
|
|
14995
15694
|
return `${path2}: ${err.message}`;
|
|
14996
|
-
}), 'access',
|
|
15695
|
+
}), 'access', _319 => _319.join, 'call', _320 => _320(", ")]);
|
|
14997
15696
|
throw Error(`Invalid parameters: ${errorMessages}`);
|
|
14998
15697
|
}
|
|
14999
15698
|
return parameters;
|
|
@@ -15231,7 +15930,7 @@ async function runEvaluator(experiment, evaluator, progressReporter, filters, st
|
|
|
15231
15930
|
}
|
|
15232
15931
|
async function runEvaluatorInternal(experiment, evaluator, progressReporter, filters, stream, parameters, collectResults, enableCache) {
|
|
15233
15932
|
if (enableCache) {
|
|
15234
|
-
_optionalChain([(_nullishCoalesce(evaluator.state, () => ( _internalGetGlobalState()))), 'optionalAccess',
|
|
15933
|
+
_optionalChain([(_nullishCoalesce(evaluator.state, () => ( _internalGetGlobalState()))), 'optionalAccess', _321 => _321.spanCache, 'optionalAccess', _322 => _322.start, 'call', _323 => _323()]);
|
|
15235
15934
|
}
|
|
15236
15935
|
try {
|
|
15237
15936
|
if (typeof evaluator.data === "string") {
|
|
@@ -15286,7 +15985,7 @@ async function runEvaluatorInternal(experiment, evaluator, progressReporter, fil
|
|
|
15286
15985
|
const experimentIdPromise = experiment ? (async () => {
|
|
15287
15986
|
try {
|
|
15288
15987
|
return await experiment.id;
|
|
15289
|
-
} catch (
|
|
15988
|
+
} catch (e32) {
|
|
15290
15989
|
return void 0;
|
|
15291
15990
|
}
|
|
15292
15991
|
})() : void 0;
|
|
@@ -15342,7 +16041,7 @@ async function runEvaluatorInternal(experiment, evaluator, progressReporter, fil
|
|
|
15342
16041
|
objectType: parentComponents ? spanObjectTypeV3ToTypedString(
|
|
15343
16042
|
parentComponents.data.object_type
|
|
15344
16043
|
) : "experiment",
|
|
15345
|
-
objectId: await _asyncNullishCoalesce(await _asyncOptionalChain([parentComponents, 'optionalAccess', async
|
|
16044
|
+
objectId: await _asyncNullishCoalesce(await _asyncOptionalChain([parentComponents, 'optionalAccess', async _324 => _324.data, 'access', async _325 => _325.object_id]), async () => ( (experimentIdPromise ? await _asyncNullishCoalesce(await experimentIdPromise, async () => ( "")) : ""))),
|
|
15346
16045
|
rootSpanId: rootSpan.rootSpanId,
|
|
15347
16046
|
ensureSpansFlushed,
|
|
15348
16047
|
state
|
|
@@ -15368,10 +16067,10 @@ async function runEvaluatorInternal(experiment, evaluator, progressReporter, fil
|
|
|
15368
16067
|
span,
|
|
15369
16068
|
parameters: _nullishCoalesce(parameters, () => ( {})),
|
|
15370
16069
|
reportProgress: (event) => {
|
|
15371
|
-
_optionalChain([stream, 'optionalCall',
|
|
16070
|
+
_optionalChain([stream, 'optionalCall', _326 => _326({
|
|
15372
16071
|
...event,
|
|
15373
16072
|
id: rootSpan.id,
|
|
15374
|
-
origin: _optionalChain([baseEvent, 'access',
|
|
16073
|
+
origin: _optionalChain([baseEvent, 'access', _327 => _327.event, 'optionalAccess', _328 => _328.origin]),
|
|
15375
16074
|
name: evaluator.evalName,
|
|
15376
16075
|
object_type: "task"
|
|
15377
16076
|
})]);
|
|
@@ -15535,7 +16234,7 @@ async function runEvaluatorInternal(experiment, evaluator, progressReporter, fil
|
|
|
15535
16234
|
metadata,
|
|
15536
16235
|
scores: mergedScores,
|
|
15537
16236
|
error,
|
|
15538
|
-
origin: _optionalChain([baseEvent, 'access',
|
|
16237
|
+
origin: _optionalChain([baseEvent, 'access', _329 => _329.event, 'optionalAccess', _330 => _330.origin])
|
|
15539
16238
|
});
|
|
15540
16239
|
}
|
|
15541
16240
|
};
|
|
@@ -15570,7 +16269,7 @@ async function runEvaluatorInternal(experiment, evaluator, progressReporter, fil
|
|
|
15570
16269
|
break;
|
|
15571
16270
|
}
|
|
15572
16271
|
scheduledTrials++;
|
|
15573
|
-
_optionalChain([progressReporter, 'access',
|
|
16272
|
+
_optionalChain([progressReporter, 'access', _331 => _331.setTotal, 'optionalCall', _332 => _332(evaluator.evalName, scheduledTrials)]);
|
|
15574
16273
|
q.pushAsync({ datum, trialIndex }).catch((e) => {
|
|
15575
16274
|
if (queueErrors.length < 5) {
|
|
15576
16275
|
queueErrors.push(e);
|
|
@@ -15655,9 +16354,9 @@ async function runEvaluatorInternal(experiment, evaluator, progressReporter, fil
|
|
|
15655
16354
|
);
|
|
15656
16355
|
} finally {
|
|
15657
16356
|
if (enableCache) {
|
|
15658
|
-
const spanCache = _optionalChain([(_nullishCoalesce(evaluator.state, () => ( _internalGetGlobalState()))), 'optionalAccess',
|
|
15659
|
-
_optionalChain([spanCache, 'optionalAccess',
|
|
15660
|
-
_optionalChain([spanCache, 'optionalAccess',
|
|
16357
|
+
const spanCache = _optionalChain([(_nullishCoalesce(evaluator.state, () => ( _internalGetGlobalState()))), 'optionalAccess', _333 => _333.spanCache]);
|
|
16358
|
+
_optionalChain([spanCache, 'optionalAccess', _334 => _334.dispose, 'call', _335 => _335()]);
|
|
16359
|
+
_optionalChain([spanCache, 'optionalAccess', _336 => _336.stop, 'call', _337 => _337()]);
|
|
15661
16360
|
}
|
|
15662
16361
|
}
|
|
15663
16362
|
}
|
|
@@ -15894,7 +16593,7 @@ async function cachedLogin(options) {
|
|
|
15894
16593
|
}
|
|
15895
16594
|
function makeCheckAuthorized(allowedOrgName) {
|
|
15896
16595
|
return async (req, _res, next) => {
|
|
15897
|
-
if (!_optionalChain([req, 'access',
|
|
16596
|
+
if (!_optionalChain([req, 'access', _338 => _338.ctx, 'optionalAccess', _339 => _339.token])) {
|
|
15898
16597
|
return next(_httperrors2.default.call(void 0, 401, "Unauthorized"));
|
|
15899
16598
|
}
|
|
15900
16599
|
try {
|
|
@@ -15907,7 +16606,7 @@ function makeCheckAuthorized(allowedOrgName) {
|
|
|
15907
16606
|
return next(_httperrors2.default.call(void 0, 403, errorMessage));
|
|
15908
16607
|
}
|
|
15909
16608
|
const state = await cachedLogin({
|
|
15910
|
-
apiKey: _optionalChain([req, 'access',
|
|
16609
|
+
apiKey: _optionalChain([req, 'access', _340 => _340.ctx, 'optionalAccess', _341 => _341.token]),
|
|
15911
16610
|
orgName
|
|
15912
16611
|
});
|
|
15913
16612
|
req.ctx.state = state;
|
|
@@ -16656,7 +17355,7 @@ function runDevServer(evaluators, opts) {
|
|
|
16656
17355
|
scores,
|
|
16657
17356
|
stream
|
|
16658
17357
|
} = evalBodySchema.parse(req.body);
|
|
16659
|
-
if (!_optionalChain([req, 'access',
|
|
17358
|
+
if (!_optionalChain([req, 'access', _342 => _342.ctx, 'optionalAccess', _343 => _343.state])) {
|
|
16660
17359
|
res.status(500).json({ error: "Braintrust state not initialized in request" });
|
|
16661
17360
|
return;
|
|
16662
17361
|
}
|
|
@@ -16707,12 +17406,12 @@ function runDevServer(evaluators, opts) {
|
|
|
16707
17406
|
...evaluator,
|
|
16708
17407
|
data: evalData.data,
|
|
16709
17408
|
scores: evaluator.scores.concat(
|
|
16710
|
-
_nullishCoalesce(_optionalChain([scores, 'optionalAccess',
|
|
17409
|
+
_nullishCoalesce(_optionalChain([scores, 'optionalAccess', _344 => _344.map, 'call', _345 => _345(
|
|
16711
17410
|
(score) => makeScorer(
|
|
16712
17411
|
state,
|
|
16713
17412
|
score.name,
|
|
16714
17413
|
score.function_id,
|
|
16715
|
-
_optionalChain([req, 'access',
|
|
17414
|
+
_optionalChain([req, 'access', _346 => _346.ctx, 'optionalAccess', _347 => _347.projectId])
|
|
16716
17415
|
)
|
|
16717
17416
|
)]), () => ( []))
|
|
16718
17417
|
),
|