braintrust 3.7.1 → 3.8.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/LICENSE +201 -0
- package/dev/dist/index.d.mts +144 -2
- package/dev/dist/index.d.ts +144 -2
- package/dev/dist/index.js +2432 -430
- package/dev/dist/index.mjs +2307 -305
- package/dist/auto-instrumentations/bundler/esbuild.cjs +377 -10
- package/dist/auto-instrumentations/bundler/esbuild.mjs +2 -2
- package/dist/auto-instrumentations/bundler/rollup.cjs +377 -10
- package/dist/auto-instrumentations/bundler/rollup.mjs +2 -2
- package/dist/auto-instrumentations/bundler/vite.cjs +377 -10
- package/dist/auto-instrumentations/bundler/vite.mjs +2 -2
- package/dist/auto-instrumentations/bundler/webpack-loader.cjs +377 -10
- package/dist/auto-instrumentations/bundler/webpack-loader.d.ts +11 -9
- package/dist/auto-instrumentations/bundler/webpack.cjs +377 -10
- package/dist/auto-instrumentations/bundler/webpack.mjs +2 -2
- package/dist/auto-instrumentations/{chunk-EVUKFMHG.mjs → chunk-ITP7RAUY.mjs} +21 -3
- package/dist/auto-instrumentations/{chunk-NY4CGTN6.mjs → chunk-MD7W27YH.mjs} +5 -1
- package/dist/auto-instrumentations/{chunk-YCKND42U.mjs → chunk-OLBMPZXE.mjs} +378 -11
- package/dist/auto-instrumentations/{chunk-VLEJ5AEK.mjs → chunk-P5YLNB2A.mjs} +21 -3
- package/dist/auto-instrumentations/hook.mjs +393 -16
- package/dist/auto-instrumentations/index.cjs +379 -10
- package/dist/auto-instrumentations/index.d.mts +5 -1
- package/dist/auto-instrumentations/index.d.ts +5 -1
- package/dist/auto-instrumentations/index.mjs +5 -1
- package/dist/auto-instrumentations/loader/cjs-patch.cjs +34 -6
- package/dist/auto-instrumentations/loader/cjs-patch.d.mts +1 -0
- package/dist/auto-instrumentations/loader/cjs-patch.d.ts +1 -0
- package/dist/auto-instrumentations/loader/cjs-patch.mjs +15 -5
- package/dist/auto-instrumentations/loader/esm-hook.mjs +8 -3
- package/dist/auto-instrumentations/loader/get-package-version.cjs +20 -2
- package/dist/auto-instrumentations/loader/get-package-version.mjs +1 -1
- package/dist/browser.d.mts +191 -14
- package/dist/browser.d.ts +191 -14
- package/dist/browser.js +2646 -315
- package/dist/browser.mjs +2646 -315
- package/dist/cli.js +2411 -409
- package/dist/edge-light.d.mts +1 -1
- package/dist/edge-light.d.ts +1 -1
- package/dist/edge-light.js +12604 -10184
- package/dist/edge-light.mjs +12604 -10184
- package/dist/index.d.mts +191 -14
- package/dist/index.d.ts +191 -14
- package/dist/index.js +2823 -492
- package/dist/index.mjs +2646 -315
- package/dist/instrumentation/index.d.mts +7 -0
- package/dist/instrumentation/index.d.ts +7 -0
- package/dist/instrumentation/index.js +2409 -407
- package/dist/instrumentation/index.mjs +2409 -407
- package/dist/workerd.d.mts +1 -1
- package/dist/workerd.d.ts +1 -1
- package/dist/workerd.js +12604 -10184
- package/dist/workerd.mjs +12604 -10184
- package/package.json +44 -44
package/dev/dist/index.js
CHANGED
|
@@ -2102,6 +2102,16 @@ var NullableSavedFunctionId = _v3.z.union([
|
|
|
2102
2102
|
}),
|
|
2103
2103
|
_v3.z.null()
|
|
2104
2104
|
]);
|
|
2105
|
+
var TopicMapGenerationSettings = _v3.z.object({
|
|
2106
|
+
algorithm: _v3.z.enum(["hdbscan", "kmeans"]),
|
|
2107
|
+
dimension_reduction: _v3.z.enum(["umap", "pca", "none"]),
|
|
2108
|
+
sample_size: _v3.z.number().int().gt(0).optional(),
|
|
2109
|
+
n_clusters: _v3.z.number().int().gt(0).optional(),
|
|
2110
|
+
min_cluster_size: _v3.z.number().int().gt(0).optional(),
|
|
2111
|
+
min_samples: _v3.z.number().int().gt(0).optional(),
|
|
2112
|
+
hierarchy_threshold: _v3.z.number().int().gt(0).optional(),
|
|
2113
|
+
naming_model: _v3.z.string().optional()
|
|
2114
|
+
});
|
|
2105
2115
|
var TopicMapData = _v3.z.object({
|
|
2106
2116
|
type: _v3.z.literal("topic_map"),
|
|
2107
2117
|
source_facet: _v3.z.string(),
|
|
@@ -2109,6 +2119,7 @@ var TopicMapData = _v3.z.object({
|
|
|
2109
2119
|
bundle_key: _v3.z.string().optional(),
|
|
2110
2120
|
report_key: _v3.z.string().optional(),
|
|
2111
2121
|
topic_names: _v3.z.record(_v3.z.string()).optional(),
|
|
2122
|
+
generation_settings: TopicMapGenerationSettings.optional(),
|
|
2112
2123
|
distance_threshold: _v3.z.number().optional()
|
|
2113
2124
|
});
|
|
2114
2125
|
var BatchedFacetData = _v3.z.object({
|
|
@@ -2333,6 +2344,7 @@ var Dataset = _v3.z.object({
|
|
|
2333
2344
|
created: _v3.z.union([_v3.z.string(), _v3.z.null()]).optional(),
|
|
2334
2345
|
deleted_at: _v3.z.union([_v3.z.string(), _v3.z.null()]).optional(),
|
|
2335
2346
|
user_id: _v3.z.union([_v3.z.string(), _v3.z.null()]).optional(),
|
|
2347
|
+
tags: _v3.z.union([_v3.z.array(_v3.z.string()), _v3.z.null()]).optional(),
|
|
2336
2348
|
metadata: _v3.z.union([_v3.z.object({}).partial().passthrough(), _v3.z.null()]).optional(),
|
|
2337
2349
|
url_slug: _v3.z.string()
|
|
2338
2350
|
});
|
|
@@ -2415,6 +2427,14 @@ var DatasetEvent = _v3.z.object({
|
|
|
2415
2427
|
_v3.z.null()
|
|
2416
2428
|
]).optional()
|
|
2417
2429
|
});
|
|
2430
|
+
var DatasetSnapshot = _v3.z.object({
|
|
2431
|
+
id: _v3.z.string().uuid(),
|
|
2432
|
+
dataset_id: _v3.z.string().uuid(),
|
|
2433
|
+
name: _v3.z.string(),
|
|
2434
|
+
description: _v3.z.union([_v3.z.string(), _v3.z.null()]),
|
|
2435
|
+
xact_id: _v3.z.string(),
|
|
2436
|
+
created: _v3.z.union([_v3.z.string(), _v3.z.null()])
|
|
2437
|
+
});
|
|
2418
2438
|
var EnvVar = _v3.z.object({
|
|
2419
2439
|
id: _v3.z.string().uuid(),
|
|
2420
2440
|
object_type: _v3.z.enum(["organization", "project", "function"]),
|
|
@@ -3173,6 +3193,8 @@ var TopicAutomationConfig = _v3.z.object({
|
|
|
3173
3193
|
scope: _v3.z.union([SpanScope, TraceScope, GroupScope, _v3.z.null()]).optional(),
|
|
3174
3194
|
data_scope: TopicAutomationDataScope.optional(),
|
|
3175
3195
|
btql_filter: _v3.z.union([_v3.z.string(), _v3.z.null()]).optional(),
|
|
3196
|
+
rerun_seconds: _v3.z.union([_v3.z.number(), _v3.z.null()]).optional(),
|
|
3197
|
+
relabel_overlap_seconds: _v3.z.union([_v3.z.number(), _v3.z.null()]).optional(),
|
|
3176
3198
|
backfill_time_range: _v3.z.union([
|
|
3177
3199
|
_v3.z.string(),
|
|
3178
3200
|
_v3.z.object({ from: _v3.z.string(), to: _v3.z.string() }),
|
|
@@ -3509,7 +3531,8 @@ var User = _v3.z.object({
|
|
|
3509
3531
|
family_name: _v3.z.union([_v3.z.string(), _v3.z.null()]).optional(),
|
|
3510
3532
|
email: _v3.z.union([_v3.z.string(), _v3.z.null()]).optional(),
|
|
3511
3533
|
avatar_url: _v3.z.union([_v3.z.string(), _v3.z.null()]).optional(),
|
|
3512
|
-
created: _v3.z.union([_v3.z.string(), _v3.z.null()]).optional()
|
|
3534
|
+
created: _v3.z.union([_v3.z.string(), _v3.z.null()]).optional(),
|
|
3535
|
+
last_active_at: _v3.z.union([_v3.z.number(), _v3.z.null()]).optional()
|
|
3513
3536
|
});
|
|
3514
3537
|
var ViewDataSearch = _v3.z.union([
|
|
3515
3538
|
_v3.z.object({
|
|
@@ -4669,6 +4692,15 @@ var BRAINTRUST_CURRENT_SPAN_STORE = Symbol.for(
|
|
|
4669
4692
|
"braintrust.currentSpanStore"
|
|
4670
4693
|
);
|
|
4671
4694
|
var ContextManager = class {
|
|
4695
|
+
/**
|
|
4696
|
+
* Returns the value to store in the ALS bound to a TracingChannel's start event.
|
|
4697
|
+
* In default mode this is the Span itself; in OTEL mode it is the OTEL Context
|
|
4698
|
+
* containing the span so that OTEL's own ALS stores a proper Context object.
|
|
4699
|
+
*/
|
|
4700
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
4701
|
+
wrapSpanForStore(span) {
|
|
4702
|
+
return span;
|
|
4703
|
+
}
|
|
4672
4704
|
};
|
|
4673
4705
|
var BraintrustContextManager = class extends ContextManager {
|
|
4674
4706
|
|
|
@@ -9401,18 +9433,22 @@ function ensureSpanStateForEvent(states, config, event, channelName) {
|
|
|
9401
9433
|
function bindCurrentSpanStoreToStart(tracingChannel2, states, config, channelName) {
|
|
9402
9434
|
const state = _internalGetGlobalState();
|
|
9403
9435
|
const startChannel = tracingChannel2.start;
|
|
9404
|
-
const
|
|
9436
|
+
const contextManager = _optionalChain([state, 'optionalAccess', _145 => _145.contextManager]);
|
|
9437
|
+
const currentSpanStore = contextManager ? contextManager[BRAINTRUST_CURRENT_SPAN_STORE] : void 0;
|
|
9405
9438
|
if (!currentSpanStore || !startChannel) {
|
|
9406
9439
|
return void 0;
|
|
9407
9440
|
}
|
|
9408
9441
|
startChannel.bindStore(
|
|
9409
9442
|
currentSpanStore,
|
|
9410
|
-
(event) =>
|
|
9411
|
-
|
|
9412
|
-
|
|
9413
|
-
|
|
9414
|
-
|
|
9415
|
-
|
|
9443
|
+
(event) => {
|
|
9444
|
+
const span = ensureSpanStateForEvent(
|
|
9445
|
+
states,
|
|
9446
|
+
config,
|
|
9447
|
+
event,
|
|
9448
|
+
channelName
|
|
9449
|
+
).span;
|
|
9450
|
+
return contextManager.wrapSpanForStore(span);
|
|
9451
|
+
}
|
|
9416
9452
|
);
|
|
9417
9453
|
return () => {
|
|
9418
9454
|
startChannel.unbindStore(currentSpanStore);
|
|
@@ -9429,6 +9465,26 @@ function logErrorAndEnd(states, event) {
|
|
|
9429
9465
|
spanData.span.end();
|
|
9430
9466
|
states.delete(event);
|
|
9431
9467
|
}
|
|
9468
|
+
function runStreamingCompletionHook(args) {
|
|
9469
|
+
if (!args.config.onComplete) {
|
|
9470
|
+
return;
|
|
9471
|
+
}
|
|
9472
|
+
try {
|
|
9473
|
+
args.config.onComplete({
|
|
9474
|
+
channelName: args.channelName,
|
|
9475
|
+
...args.chunks ? { chunks: args.chunks } : {},
|
|
9476
|
+
endEvent: args.endEvent,
|
|
9477
|
+
...args.metadata !== void 0 ? { metadata: args.metadata } : {},
|
|
9478
|
+
metrics: args.metrics,
|
|
9479
|
+
output: args.output,
|
|
9480
|
+
result: args.result,
|
|
9481
|
+
span: args.span,
|
|
9482
|
+
startTime: args.startTime
|
|
9483
|
+
});
|
|
9484
|
+
} catch (error) {
|
|
9485
|
+
console.error(`Error in onComplete hook for ${args.channelName}:`, error);
|
|
9486
|
+
}
|
|
9487
|
+
}
|
|
9432
9488
|
function traceAsyncChannel(channel2, config) {
|
|
9433
9489
|
const tracingChannel2 = channel2.tracingChannel();
|
|
9434
9490
|
const states = /* @__PURE__ */ new WeakMap();
|
|
@@ -9556,6 +9612,18 @@ function traceStreamingChannel(channel2, config) {
|
|
|
9556
9612
|
} else if (metrics.time_to_first_token === void 0 && chunks.length > 0) {
|
|
9557
9613
|
metrics.time_to_first_token = getCurrentUnixTimestamp() - startTime;
|
|
9558
9614
|
}
|
|
9615
|
+
runStreamingCompletionHook({
|
|
9616
|
+
channelName,
|
|
9617
|
+
chunks,
|
|
9618
|
+
config,
|
|
9619
|
+
endEvent: asyncEndEvent,
|
|
9620
|
+
...metadata !== void 0 ? { metadata } : {},
|
|
9621
|
+
metrics,
|
|
9622
|
+
output,
|
|
9623
|
+
result: asyncEndEvent.result,
|
|
9624
|
+
span,
|
|
9625
|
+
startTime
|
|
9626
|
+
});
|
|
9559
9627
|
span.log({
|
|
9560
9628
|
output,
|
|
9561
9629
|
...metadata !== void 0 ? { metadata } : {},
|
|
@@ -9605,6 +9673,17 @@ function traceStreamingChannel(channel2, config) {
|
|
|
9605
9673
|
asyncEndEvent.result,
|
|
9606
9674
|
asyncEndEvent
|
|
9607
9675
|
)]);
|
|
9676
|
+
runStreamingCompletionHook({
|
|
9677
|
+
channelName,
|
|
9678
|
+
config,
|
|
9679
|
+
endEvent: asyncEndEvent,
|
|
9680
|
+
...normalizeMetadata(metadata) !== void 0 ? { metadata: normalizeMetadata(metadata) } : {},
|
|
9681
|
+
metrics,
|
|
9682
|
+
output,
|
|
9683
|
+
result: asyncEndEvent.result,
|
|
9684
|
+
span,
|
|
9685
|
+
startTime
|
|
9686
|
+
});
|
|
9608
9687
|
span.log({
|
|
9609
9688
|
output,
|
|
9610
9689
|
...normalizeMetadata(metadata) !== void 0 ? { metadata: normalizeMetadata(metadata) } : {},
|
|
@@ -9988,6 +10067,10 @@ var openAIChannels = defineChannels("openai", {
|
|
|
9988
10067
|
responsesParse: channel({
|
|
9989
10068
|
channelName: "responses.parse",
|
|
9990
10069
|
kind: "async"
|
|
10070
|
+
}),
|
|
10071
|
+
responsesCompact: channel({
|
|
10072
|
+
channelName: "responses.compact",
|
|
10073
|
+
kind: "async"
|
|
9991
10074
|
})
|
|
9992
10075
|
});
|
|
9993
10076
|
|
|
@@ -10267,6 +10350,40 @@ var OpenAIPlugin = class extends BasePlugin {
|
|
|
10267
10350
|
aggregateChunks: aggregateResponseStreamEvents
|
|
10268
10351
|
})
|
|
10269
10352
|
);
|
|
10353
|
+
this.unsubscribers.push(
|
|
10354
|
+
traceAsyncChannel(openAIChannels.responsesCompact, {
|
|
10355
|
+
name: "openai.responses.compact",
|
|
10356
|
+
type: "llm" /* LLM */,
|
|
10357
|
+
extractInput: ([params]) => {
|
|
10358
|
+
const { input, ...metadata } = params;
|
|
10359
|
+
return {
|
|
10360
|
+
input: processInputAttachments(input),
|
|
10361
|
+
metadata: { ...metadata, provider: "openai" }
|
|
10362
|
+
};
|
|
10363
|
+
},
|
|
10364
|
+
extractOutput: (result) => {
|
|
10365
|
+
return processImagesInOutput(_optionalChain([result, 'optionalAccess', _172 => _172.output]));
|
|
10366
|
+
},
|
|
10367
|
+
extractMetadata: (result) => {
|
|
10368
|
+
if (!result) {
|
|
10369
|
+
return void 0;
|
|
10370
|
+
}
|
|
10371
|
+
const { output: _output, usage: _usage, ...metadata } = result;
|
|
10372
|
+
return Object.keys(metadata).length > 0 ? metadata : void 0;
|
|
10373
|
+
},
|
|
10374
|
+
extractMetrics: (result, startTime, endEvent) => {
|
|
10375
|
+
const metrics = withCachedMetric(
|
|
10376
|
+
parseMetricsFromUsage(_optionalChain([result, 'optionalAccess', _173 => _173.usage])),
|
|
10377
|
+
result,
|
|
10378
|
+
endEvent
|
|
10379
|
+
);
|
|
10380
|
+
if (startTime) {
|
|
10381
|
+
metrics.time_to_first_token = getCurrentUnixTimestamp() - startTime;
|
|
10382
|
+
}
|
|
10383
|
+
return metrics;
|
|
10384
|
+
}
|
|
10385
|
+
})
|
|
10386
|
+
);
|
|
10270
10387
|
}
|
|
10271
10388
|
onDisable() {
|
|
10272
10389
|
this.unsubscribers = unsubscribeAll(this.unsubscribers);
|
|
@@ -10351,7 +10468,7 @@ function aggregateChatCompletionChunks(chunks, streamResult, endEvent) {
|
|
|
10351
10468
|
...parseMetricsFromUsage(chunk.usage)
|
|
10352
10469
|
};
|
|
10353
10470
|
}
|
|
10354
|
-
const delta = _optionalChain([chunk, 'access',
|
|
10471
|
+
const delta = _optionalChain([chunk, 'access', _174 => _174.choices, 'optionalAccess', _175 => _175[0], 'optionalAccess', _176 => _176.delta]);
|
|
10355
10472
|
if (!delta) {
|
|
10356
10473
|
continue;
|
|
10357
10474
|
}
|
|
@@ -10409,14 +10526,14 @@ function aggregateResponseStreamEvents(chunks, _streamResult, endEvent) {
|
|
|
10409
10526
|
continue;
|
|
10410
10527
|
}
|
|
10411
10528
|
const response = chunk.response;
|
|
10412
|
-
if (_optionalChain([response, 'optionalAccess',
|
|
10529
|
+
if (_optionalChain([response, 'optionalAccess', _177 => _177.output]) !== void 0) {
|
|
10413
10530
|
output = processImagesInOutput(response.output);
|
|
10414
10531
|
}
|
|
10415
10532
|
const { usage: _usage, output: _output, ...rest } = response || {};
|
|
10416
10533
|
if (Object.keys(rest).length > 0) {
|
|
10417
10534
|
metadata = rest;
|
|
10418
10535
|
}
|
|
10419
|
-
metrics = parseMetricsFromUsage(_optionalChain([response, 'optionalAccess',
|
|
10536
|
+
metrics = parseMetricsFromUsage(_optionalChain([response, 'optionalAccess', _178 => _178.usage]));
|
|
10420
10537
|
}
|
|
10421
10538
|
return {
|
|
10422
10539
|
output,
|
|
@@ -10482,7 +10599,7 @@ var AnthropicPlugin = class extends BasePlugin {
|
|
|
10482
10599
|
return message ? { role: message.role, content: message.content } : null;
|
|
10483
10600
|
},
|
|
10484
10601
|
extractMetrics: (message, startTime) => {
|
|
10485
|
-
const metrics = parseMetricsFromUsage2(_optionalChain([message, 'optionalAccess',
|
|
10602
|
+
const metrics = parseMetricsFromUsage2(_optionalChain([message, 'optionalAccess', _179 => _179.usage]));
|
|
10486
10603
|
if (startTime) {
|
|
10487
10604
|
metrics.time_to_first_token = getCurrentUnixTimestamp() - startTime;
|
|
10488
10605
|
}
|
|
@@ -10497,7 +10614,7 @@ var AnthropicPlugin = class extends BasePlugin {
|
|
|
10497
10614
|
const metadata = {};
|
|
10498
10615
|
const metas = ["stop_reason", "stop_sequence"];
|
|
10499
10616
|
for (const m of metas) {
|
|
10500
|
-
if (_optionalChain([message, 'optionalAccess',
|
|
10617
|
+
if (_optionalChain([message, 'optionalAccess', _180 => _180[m]]) !== void 0) {
|
|
10501
10618
|
metadata[m] = message[m];
|
|
10502
10619
|
}
|
|
10503
10620
|
}
|
|
@@ -10541,13 +10658,13 @@ function aggregateAnthropicStreamChunks(chunks) {
|
|
|
10541
10658
|
let metadata = {};
|
|
10542
10659
|
let role;
|
|
10543
10660
|
for (const event of chunks) {
|
|
10544
|
-
switch (_optionalChain([event, 'optionalAccess',
|
|
10661
|
+
switch (_optionalChain([event, 'optionalAccess', _181 => _181.type])) {
|
|
10545
10662
|
case "message_start":
|
|
10546
|
-
if (_optionalChain([event, 'access',
|
|
10663
|
+
if (_optionalChain([event, 'access', _182 => _182.message, 'optionalAccess', _183 => _183.usage])) {
|
|
10547
10664
|
const initialMetrics = parseMetricsFromUsage2(event.message.usage);
|
|
10548
10665
|
metrics = { ...metrics, ...initialMetrics };
|
|
10549
10666
|
}
|
|
10550
|
-
if (typeof _optionalChain([event, 'access',
|
|
10667
|
+
if (typeof _optionalChain([event, 'access', _184 => _184.message, 'optionalAccess', _185 => _185.role]) === "string") {
|
|
10551
10668
|
role = event.message.role;
|
|
10552
10669
|
}
|
|
10553
10670
|
break;
|
|
@@ -10613,7 +10730,7 @@ function aggregateAnthropicStreamChunks(chunks) {
|
|
|
10613
10730
|
})).filter(({ block }) => block !== void 0).sort((left, right) => left.index - right.index).map(({ block }) => block);
|
|
10614
10731
|
let output = fallbackTextDeltas.join("");
|
|
10615
10732
|
if (orderedContent.length > 0) {
|
|
10616
|
-
if (orderedContent.every(isTextContentBlock) && orderedContent.every((block) => !_optionalChain([block, 'access',
|
|
10733
|
+
if (orderedContent.every(isTextContentBlock) && orderedContent.every((block) => !_optionalChain([block, 'access', _186 => _186.citations, 'optionalAccess', _187 => _187.length]))) {
|
|
10617
10734
|
output = orderedContent.map((block) => block.text).join("");
|
|
10618
10735
|
} else {
|
|
10619
10736
|
output = {
|
|
@@ -10640,7 +10757,7 @@ function finalizeContentBlock(index, contentBlocks, contentBlockDeltas, fallback
|
|
|
10640
10757
|
return;
|
|
10641
10758
|
}
|
|
10642
10759
|
const acc = contentBlockDeltas[index];
|
|
10643
|
-
const text = _nullishCoalesce(_optionalChain([acc, 'optionalAccess',
|
|
10760
|
+
const text = _nullishCoalesce(_optionalChain([acc, 'optionalAccess', _188 => _188.textDeltas, 'access', _189 => _189.join, 'call', _190 => _190("")]), () => ( ""));
|
|
10644
10761
|
if (isToolUseContentBlock(contentBlock)) {
|
|
10645
10762
|
if (!text) {
|
|
10646
10763
|
return;
|
|
@@ -10662,7 +10779,7 @@ function finalizeContentBlock(index, contentBlocks, contentBlockDeltas, fallback
|
|
|
10662
10779
|
return;
|
|
10663
10780
|
}
|
|
10664
10781
|
const updated = { ...contentBlock, text };
|
|
10665
|
-
if (_optionalChain([acc, 'optionalAccess',
|
|
10782
|
+
if (_optionalChain([acc, 'optionalAccess', _191 => _191.citations, 'access', _192 => _192.length])) {
|
|
10666
10783
|
updated.citations = acc.citations;
|
|
10667
10784
|
}
|
|
10668
10785
|
contentBlocks[index] = updated;
|
|
@@ -10856,6 +10973,10 @@ var aiSDKChannels = defineChannels("ai", {
|
|
|
10856
10973
|
channelName: "streamText",
|
|
10857
10974
|
kind: "async"
|
|
10858
10975
|
}),
|
|
10976
|
+
streamTextSync: channel({
|
|
10977
|
+
channelName: "streamText.sync",
|
|
10978
|
+
kind: "sync-stream"
|
|
10979
|
+
}),
|
|
10859
10980
|
generateObject: channel({
|
|
10860
10981
|
channelName: "generateObject",
|
|
10861
10982
|
kind: "async"
|
|
@@ -10864,6 +10985,20 @@ var aiSDKChannels = defineChannels("ai", {
|
|
|
10864
10985
|
channelName: "streamObject",
|
|
10865
10986
|
kind: "async"
|
|
10866
10987
|
}),
|
|
10988
|
+
streamObjectSync: channel({
|
|
10989
|
+
channelName: "streamObject.sync",
|
|
10990
|
+
kind: "sync-stream"
|
|
10991
|
+
}),
|
|
10992
|
+
embed: channel(
|
|
10993
|
+
{
|
|
10994
|
+
channelName: "embed",
|
|
10995
|
+
kind: "async"
|
|
10996
|
+
}
|
|
10997
|
+
),
|
|
10998
|
+
embedMany: channel({
|
|
10999
|
+
channelName: "embedMany",
|
|
11000
|
+
kind: "async"
|
|
11001
|
+
}),
|
|
10867
11002
|
agentGenerate: channel({
|
|
10868
11003
|
channelName: "Agent.generate",
|
|
10869
11004
|
kind: "async"
|
|
@@ -10872,6 +11007,10 @@ var aiSDKChannels = defineChannels("ai", {
|
|
|
10872
11007
|
channelName: "Agent.stream",
|
|
10873
11008
|
kind: "async"
|
|
10874
11009
|
}),
|
|
11010
|
+
agentStreamSync: channel({
|
|
11011
|
+
channelName: "Agent.stream.sync",
|
|
11012
|
+
kind: "sync-stream"
|
|
11013
|
+
}),
|
|
10875
11014
|
toolLoopAgentGenerate: channel({
|
|
10876
11015
|
channelName: "ToolLoopAgent.generate",
|
|
10877
11016
|
kind: "async"
|
|
@@ -10920,7 +11059,7 @@ var AISDKPlugin = class extends BasePlugin {
|
|
|
10920
11059
|
traceStreamingChannel(aiSDKChannels.generateText, {
|
|
10921
11060
|
name: "generateText",
|
|
10922
11061
|
type: "llm" /* LLM */,
|
|
10923
|
-
extractInput: ([params], event, span) =>
|
|
11062
|
+
extractInput: ([params], event, span) => prepareAISDKCallInput(params, event, span, denyOutputPaths),
|
|
10924
11063
|
extractOutput: (result, endEvent) => {
|
|
10925
11064
|
finalizeAISDKChildTracing(endEvent);
|
|
10926
11065
|
return processAISDKOutput(
|
|
@@ -10936,7 +11075,7 @@ var AISDKPlugin = class extends BasePlugin {
|
|
|
10936
11075
|
traceStreamingChannel(aiSDKChannels.streamText, {
|
|
10937
11076
|
name: "streamText",
|
|
10938
11077
|
type: "llm" /* LLM */,
|
|
10939
|
-
extractInput: ([params], event, span) =>
|
|
11078
|
+
extractInput: ([params], event, span) => prepareAISDKCallInput(params, event, span, denyOutputPaths),
|
|
10940
11079
|
extractOutput: (result, endEvent) => processAISDKOutput(
|
|
10941
11080
|
result,
|
|
10942
11081
|
resolveDenyOutputPaths(endEvent, denyOutputPaths)
|
|
@@ -10952,11 +11091,25 @@ var AISDKPlugin = class extends BasePlugin {
|
|
|
10952
11091
|
})
|
|
10953
11092
|
})
|
|
10954
11093
|
);
|
|
11094
|
+
this.unsubscribers.push(
|
|
11095
|
+
traceSyncStreamChannel(aiSDKChannels.streamTextSync, {
|
|
11096
|
+
name: "streamText",
|
|
11097
|
+
type: "llm" /* LLM */,
|
|
11098
|
+
extractInput: ([params], event, span) => prepareAISDKCallInput(params, event, span, denyOutputPaths),
|
|
11099
|
+
patchResult: ({ endEvent, result, span, startTime }) => patchAISDKStreamingResult({
|
|
11100
|
+
defaultDenyOutputPaths: denyOutputPaths,
|
|
11101
|
+
endEvent,
|
|
11102
|
+
result,
|
|
11103
|
+
span,
|
|
11104
|
+
startTime
|
|
11105
|
+
})
|
|
11106
|
+
})
|
|
11107
|
+
);
|
|
10955
11108
|
this.unsubscribers.push(
|
|
10956
11109
|
traceStreamingChannel(aiSDKChannels.generateObject, {
|
|
10957
11110
|
name: "generateObject",
|
|
10958
11111
|
type: "llm" /* LLM */,
|
|
10959
|
-
extractInput: ([params], event, span) =>
|
|
11112
|
+
extractInput: ([params], event, span) => prepareAISDKCallInput(params, event, span, denyOutputPaths),
|
|
10960
11113
|
extractOutput: (result, endEvent) => {
|
|
10961
11114
|
finalizeAISDKChildTracing(endEvent);
|
|
10962
11115
|
return processAISDKOutput(
|
|
@@ -10972,7 +11125,7 @@ var AISDKPlugin = class extends BasePlugin {
|
|
|
10972
11125
|
traceStreamingChannel(aiSDKChannels.streamObject, {
|
|
10973
11126
|
name: "streamObject",
|
|
10974
11127
|
type: "llm" /* LLM */,
|
|
10975
|
-
extractInput: ([params], event, span) =>
|
|
11128
|
+
extractInput: ([params], event, span) => prepareAISDKCallInput(params, event, span, denyOutputPaths),
|
|
10976
11129
|
extractOutput: (result, endEvent) => processAISDKOutput(
|
|
10977
11130
|
result,
|
|
10978
11131
|
resolveDenyOutputPaths(endEvent, denyOutputPaths)
|
|
@@ -10988,11 +11141,49 @@ var AISDKPlugin = class extends BasePlugin {
|
|
|
10988
11141
|
})
|
|
10989
11142
|
})
|
|
10990
11143
|
);
|
|
11144
|
+
this.unsubscribers.push(
|
|
11145
|
+
traceSyncStreamChannel(aiSDKChannels.streamObjectSync, {
|
|
11146
|
+
name: "streamObject",
|
|
11147
|
+
type: "llm" /* LLM */,
|
|
11148
|
+
extractInput: ([params], event, span) => prepareAISDKCallInput(params, event, span, denyOutputPaths),
|
|
11149
|
+
patchResult: ({ endEvent, result, span, startTime }) => patchAISDKStreamingResult({
|
|
11150
|
+
defaultDenyOutputPaths: denyOutputPaths,
|
|
11151
|
+
endEvent,
|
|
11152
|
+
result,
|
|
11153
|
+
span,
|
|
11154
|
+
startTime
|
|
11155
|
+
})
|
|
11156
|
+
})
|
|
11157
|
+
);
|
|
11158
|
+
this.unsubscribers.push(
|
|
11159
|
+
traceAsyncChannel(aiSDKChannels.embed, {
|
|
11160
|
+
name: "embed",
|
|
11161
|
+
type: "llm" /* LLM */,
|
|
11162
|
+
extractInput: ([params], event) => prepareAISDKEmbedInput(params, event.self),
|
|
11163
|
+
extractOutput: (result, endEvent) => processAISDKEmbeddingOutput(
|
|
11164
|
+
result,
|
|
11165
|
+
resolveDenyOutputPaths(endEvent, denyOutputPaths)
|
|
11166
|
+
),
|
|
11167
|
+
extractMetrics: (result, _startTime, endEvent) => extractTopLevelAISDKMetrics(result, endEvent)
|
|
11168
|
+
})
|
|
11169
|
+
);
|
|
11170
|
+
this.unsubscribers.push(
|
|
11171
|
+
traceAsyncChannel(aiSDKChannels.embedMany, {
|
|
11172
|
+
name: "embedMany",
|
|
11173
|
+
type: "llm" /* LLM */,
|
|
11174
|
+
extractInput: ([params], event) => prepareAISDKEmbedInput(params, event.self),
|
|
11175
|
+
extractOutput: (result, endEvent) => processAISDKEmbeddingOutput(
|
|
11176
|
+
result,
|
|
11177
|
+
resolveDenyOutputPaths(endEvent, denyOutputPaths)
|
|
11178
|
+
),
|
|
11179
|
+
extractMetrics: (result, _startTime, endEvent) => extractTopLevelAISDKMetrics(result, endEvent)
|
|
11180
|
+
})
|
|
11181
|
+
);
|
|
10991
11182
|
this.unsubscribers.push(
|
|
10992
11183
|
traceStreamingChannel(aiSDKChannels.agentGenerate, {
|
|
10993
11184
|
name: "Agent.generate",
|
|
10994
11185
|
type: "llm" /* LLM */,
|
|
10995
|
-
extractInput: ([params], event, span) =>
|
|
11186
|
+
extractInput: ([params], event, span) => prepareAISDKCallInput(params, event, span, denyOutputPaths),
|
|
10996
11187
|
extractOutput: (result, endEvent) => {
|
|
10997
11188
|
finalizeAISDKChildTracing(endEvent);
|
|
10998
11189
|
return processAISDKOutput(
|
|
@@ -11008,7 +11199,7 @@ var AISDKPlugin = class extends BasePlugin {
|
|
|
11008
11199
|
traceStreamingChannel(aiSDKChannels.agentStream, {
|
|
11009
11200
|
name: "Agent.stream",
|
|
11010
11201
|
type: "llm" /* LLM */,
|
|
11011
|
-
extractInput: ([params], event, span) =>
|
|
11202
|
+
extractInput: ([params], event, span) => prepareAISDKCallInput(params, event, span, denyOutputPaths),
|
|
11012
11203
|
extractOutput: (result, endEvent) => processAISDKOutput(
|
|
11013
11204
|
result,
|
|
11014
11205
|
resolveDenyOutputPaths(endEvent, denyOutputPaths)
|
|
@@ -11024,11 +11215,25 @@ var AISDKPlugin = class extends BasePlugin {
|
|
|
11024
11215
|
})
|
|
11025
11216
|
})
|
|
11026
11217
|
);
|
|
11218
|
+
this.unsubscribers.push(
|
|
11219
|
+
traceSyncStreamChannel(aiSDKChannels.agentStreamSync, {
|
|
11220
|
+
name: "Agent.stream",
|
|
11221
|
+
type: "llm" /* LLM */,
|
|
11222
|
+
extractInput: ([params], event, span) => prepareAISDKCallInput(params, event, span, denyOutputPaths),
|
|
11223
|
+
patchResult: ({ endEvent, result, span, startTime }) => patchAISDKStreamingResult({
|
|
11224
|
+
defaultDenyOutputPaths: denyOutputPaths,
|
|
11225
|
+
endEvent,
|
|
11226
|
+
result,
|
|
11227
|
+
span,
|
|
11228
|
+
startTime
|
|
11229
|
+
})
|
|
11230
|
+
})
|
|
11231
|
+
);
|
|
11027
11232
|
this.unsubscribers.push(
|
|
11028
11233
|
traceStreamingChannel(aiSDKChannels.toolLoopAgentGenerate, {
|
|
11029
11234
|
name: "ToolLoopAgent.generate",
|
|
11030
11235
|
type: "llm" /* LLM */,
|
|
11031
|
-
extractInput: ([params], event, span) =>
|
|
11236
|
+
extractInput: ([params], event, span) => prepareAISDKCallInput(params, event, span, denyOutputPaths),
|
|
11032
11237
|
extractOutput: (result, endEvent) => {
|
|
11033
11238
|
finalizeAISDKChildTracing(endEvent);
|
|
11034
11239
|
return processAISDKOutput(
|
|
@@ -11044,7 +11249,7 @@ var AISDKPlugin = class extends BasePlugin {
|
|
|
11044
11249
|
traceStreamingChannel(aiSDKChannels.toolLoopAgentStream, {
|
|
11045
11250
|
name: "ToolLoopAgent.stream",
|
|
11046
11251
|
type: "llm" /* LLM */,
|
|
11047
|
-
extractInput: ([params], event, span) =>
|
|
11252
|
+
extractInput: ([params], event, span) => prepareAISDKCallInput(params, event, span, denyOutputPaths),
|
|
11048
11253
|
extractOutput: (result, endEvent) => processAISDKOutput(
|
|
11049
11254
|
result,
|
|
11050
11255
|
resolveDenyOutputPaths(endEvent, denyOutputPaths)
|
|
@@ -11063,10 +11268,10 @@ var AISDKPlugin = class extends BasePlugin {
|
|
|
11063
11268
|
}
|
|
11064
11269
|
};
|
|
11065
11270
|
function resolveDenyOutputPaths(event, defaultDenyOutputPaths) {
|
|
11066
|
-
if (Array.isArray(_optionalChain([event, 'optionalAccess',
|
|
11271
|
+
if (Array.isArray(_optionalChain([event, 'optionalAccess', _193 => _193.denyOutputPaths]))) {
|
|
11067
11272
|
return event.denyOutputPaths;
|
|
11068
11273
|
}
|
|
11069
|
-
const firstArgument = _optionalChain([event, 'optionalAccess',
|
|
11274
|
+
const firstArgument = _optionalChain([event, 'optionalAccess', _194 => _194.arguments]) && event.arguments.length > 0 ? event.arguments[0] : void 0;
|
|
11070
11275
|
if (!firstArgument || typeof firstArgument !== "object") {
|
|
11071
11276
|
return defaultDenyOutputPaths;
|
|
11072
11277
|
}
|
|
@@ -11281,7 +11486,7 @@ var convertImageToAttachment = (image, explicitMimeType) => {
|
|
|
11281
11486
|
try {
|
|
11282
11487
|
if (typeof image === "string" && image.startsWith("data:")) {
|
|
11283
11488
|
const [mimeTypeSection, base64Data] = image.split(",");
|
|
11284
|
-
const mimeType = _optionalChain([mimeTypeSection, 'access',
|
|
11489
|
+
const mimeType = _optionalChain([mimeTypeSection, 'access', _195 => _195.match, 'call', _196 => _196(/data:(.*?);/), 'optionalAccess', _197 => _197[1]]);
|
|
11285
11490
|
if (mimeType && base64Data) {
|
|
11286
11491
|
const blob = convertDataToBlob(base64Data, mimeType);
|
|
11287
11492
|
if (blob) {
|
|
@@ -11354,11 +11559,11 @@ var convertDataToAttachment = (data, mimeType, filename) => {
|
|
|
11354
11559
|
}
|
|
11355
11560
|
return null;
|
|
11356
11561
|
};
|
|
11357
|
-
function
|
|
11562
|
+
function processAISDKCallInput(params) {
|
|
11358
11563
|
return processInputAttachmentsSync(params);
|
|
11359
11564
|
}
|
|
11360
|
-
function
|
|
11361
|
-
const { input, outputPromise } =
|
|
11565
|
+
function prepareAISDKCallInput(params, event, span, defaultDenyOutputPaths) {
|
|
11566
|
+
const { input, outputPromise } = processAISDKCallInput(params);
|
|
11362
11567
|
if (outputPromise && input && typeof input === "object") {
|
|
11363
11568
|
outputPromise.then((resolvedData) => {
|
|
11364
11569
|
span.log({
|
|
@@ -11370,7 +11575,7 @@ function prepareAISDKInput(params, event, span, defaultDenyOutputPaths) {
|
|
|
11370
11575
|
}).catch(() => {
|
|
11371
11576
|
});
|
|
11372
11577
|
}
|
|
11373
|
-
const metadata =
|
|
11578
|
+
const metadata = extractMetadataFromCallParams(params, event.self);
|
|
11374
11579
|
const childTracing = prepareAISDKChildTracing(
|
|
11375
11580
|
params,
|
|
11376
11581
|
event.self,
|
|
@@ -11387,6 +11592,12 @@ function prepareAISDKInput(params, event, span, defaultDenyOutputPaths) {
|
|
|
11387
11592
|
metadata
|
|
11388
11593
|
};
|
|
11389
11594
|
}
|
|
11595
|
+
function prepareAISDKEmbedInput(params, self) {
|
|
11596
|
+
return {
|
|
11597
|
+
input: { ...params },
|
|
11598
|
+
metadata: extractMetadataFromEmbedParams(params, self)
|
|
11599
|
+
};
|
|
11600
|
+
}
|
|
11390
11601
|
function extractTopLevelAISDKMetrics(result, event, startTime) {
|
|
11391
11602
|
const metrics = hasModelChildTracing(event) ? {} : extractTokenMetrics(result);
|
|
11392
11603
|
if (startTime) {
|
|
@@ -11395,31 +11606,43 @@ function extractTopLevelAISDKMetrics(result, event, startTime) {
|
|
|
11395
11606
|
return metrics;
|
|
11396
11607
|
}
|
|
11397
11608
|
function hasModelChildTracing(event) {
|
|
11398
|
-
return _optionalChain([event, 'optionalAccess',
|
|
11609
|
+
return _optionalChain([event, 'optionalAccess', _198 => _198.modelWrapped]) === true || _optionalChain([event, 'optionalAccess', _199 => _199.__braintrust_ai_sdk_model_wrapped]) === true;
|
|
11399
11610
|
}
|
|
11400
|
-
function
|
|
11401
|
-
|
|
11611
|
+
function createAISDKIntegrationMetadata() {
|
|
11612
|
+
return {
|
|
11402
11613
|
braintrust: {
|
|
11403
11614
|
integration_name: "ai-sdk",
|
|
11404
11615
|
sdk_language: "typescript"
|
|
11405
11616
|
}
|
|
11406
11617
|
};
|
|
11407
|
-
|
|
11408
|
-
|
|
11409
|
-
|
|
11618
|
+
}
|
|
11619
|
+
function resolveModelFromSelf(self) {
|
|
11620
|
+
return self && typeof self === "object" && "model" in self && self.model ? self.model : self && typeof self === "object" && "settings" in self && _optionalChain([self, 'access', _200 => _200.settings, 'optionalAccess', _201 => _201.model]) ? _optionalChain([self, 'access', _202 => _202.settings, 'optionalAccess', _203 => _203.model]) : void 0;
|
|
11621
|
+
}
|
|
11622
|
+
function extractBaseMetadata(model, self) {
|
|
11623
|
+
const metadata = createAISDKIntegrationMetadata();
|
|
11624
|
+
const { model: modelId, provider } = serializeModelWithProvider(
|
|
11625
|
+
_nullishCoalesce(model, () => ( resolveModelFromSelf(self)))
|
|
11410
11626
|
);
|
|
11411
|
-
if (
|
|
11412
|
-
metadata.model =
|
|
11627
|
+
if (modelId) {
|
|
11628
|
+
metadata.model = modelId;
|
|
11413
11629
|
}
|
|
11414
11630
|
if (provider) {
|
|
11415
11631
|
metadata.provider = provider;
|
|
11416
11632
|
}
|
|
11633
|
+
return metadata;
|
|
11634
|
+
}
|
|
11635
|
+
function extractMetadataFromCallParams(params, self) {
|
|
11636
|
+
const metadata = extractBaseMetadata(params.model, self);
|
|
11417
11637
|
const tools = serializeAISDKToolsForLogging(params.tools);
|
|
11418
11638
|
if (tools) {
|
|
11419
11639
|
metadata.tools = tools;
|
|
11420
11640
|
}
|
|
11421
11641
|
return metadata;
|
|
11422
11642
|
}
|
|
11643
|
+
function extractMetadataFromEmbedParams(params, self) {
|
|
11644
|
+
return extractBaseMetadata(params.model, self);
|
|
11645
|
+
}
|
|
11423
11646
|
function prepareAISDKChildTracing(params, self, parentSpan, denyOutputPaths, aiSDK) {
|
|
11424
11647
|
const cleanup = [];
|
|
11425
11648
|
const patchedModels = /* @__PURE__ */ new WeakSet();
|
|
@@ -11457,7 +11680,7 @@ function prepareAISDKChildTracing(params, self, parentSpan, denyOutputPaths, aiS
|
|
|
11457
11680
|
type: "llm" /* LLM */
|
|
11458
11681
|
},
|
|
11459
11682
|
event: {
|
|
11460
|
-
input:
|
|
11683
|
+
input: processAISDKCallInput(options).input,
|
|
11461
11684
|
metadata: baseMetadata
|
|
11462
11685
|
}
|
|
11463
11686
|
}
|
|
@@ -11471,7 +11694,7 @@ function prepareAISDKChildTracing(params, self, parentSpan, denyOutputPaths, aiS
|
|
|
11471
11694
|
type: "llm" /* LLM */
|
|
11472
11695
|
},
|
|
11473
11696
|
event: {
|
|
11474
|
-
input:
|
|
11697
|
+
input: processAISDKCallInput(options).input,
|
|
11475
11698
|
metadata: baseMetadata
|
|
11476
11699
|
}
|
|
11477
11700
|
});
|
|
@@ -11511,9 +11734,9 @@ function prepareAISDKChildTracing(params, self, parentSpan, denyOutputPaths, aiS
|
|
|
11511
11734
|
case "raw":
|
|
11512
11735
|
if (chunk.rawValue) {
|
|
11513
11736
|
const rawVal = chunk.rawValue;
|
|
11514
|
-
if (_optionalChain([rawVal, 'access',
|
|
11737
|
+
if (_optionalChain([rawVal, 'access', _204 => _204.delta, 'optionalAccess', _205 => _205.content])) {
|
|
11515
11738
|
text += rawVal.delta.content;
|
|
11516
|
-
} else if (_optionalChain([rawVal, 'access',
|
|
11739
|
+
} else if (_optionalChain([rawVal, 'access', _206 => _206.choices, 'optionalAccess', _207 => _207[0], 'optionalAccess', _208 => _208.delta, 'optionalAccess', _209 => _209.content])) {
|
|
11517
11740
|
text += rawVal.choices[0].delta.content;
|
|
11518
11741
|
} else if (typeof rawVal.text === "string") {
|
|
11519
11742
|
text += rawVal.text;
|
|
@@ -11667,14 +11890,14 @@ function prepareAISDKChildTracing(params, self, parentSpan, denyOutputPaths, aiS
|
|
|
11667
11890
|
return {
|
|
11668
11891
|
cleanup: cleanup.length > 0 ? () => {
|
|
11669
11892
|
while (cleanup.length > 0) {
|
|
11670
|
-
_optionalChain([cleanup, 'access',
|
|
11893
|
+
_optionalChain([cleanup, 'access', _210 => _210.pop, 'call', _211 => _211(), 'optionalCall', _212 => _212()]);
|
|
11671
11894
|
}
|
|
11672
11895
|
} : void 0,
|
|
11673
11896
|
modelWrapped
|
|
11674
11897
|
};
|
|
11675
11898
|
}
|
|
11676
11899
|
function finalizeAISDKChildTracing(event) {
|
|
11677
|
-
const cleanup = _optionalChain([event, 'optionalAccess',
|
|
11900
|
+
const cleanup = _optionalChain([event, 'optionalAccess', _213 => _213.__braintrust_ai_sdk_cleanup]);
|
|
11678
11901
|
if (event && typeof cleanup === "function") {
|
|
11679
11902
|
cleanup();
|
|
11680
11903
|
delete event.__braintrust_ai_sdk_cleanup;
|
|
@@ -11780,9 +12003,14 @@ function attachKnownResultPromiseHandlers(result) {
|
|
|
11780
12003
|
"content",
|
|
11781
12004
|
"text",
|
|
11782
12005
|
"object",
|
|
12006
|
+
"value",
|
|
12007
|
+
"values",
|
|
11783
12008
|
"finishReason",
|
|
12009
|
+
"embedding",
|
|
12010
|
+
"embeddings",
|
|
11784
12011
|
"usage",
|
|
11785
12012
|
"totalUsage",
|
|
12013
|
+
"responses",
|
|
11786
12014
|
"steps"
|
|
11787
12015
|
];
|
|
11788
12016
|
for (const field of promiseLikeFields) {
|
|
@@ -11885,10 +12113,10 @@ function buildAISDKChildMetadata(model) {
|
|
|
11885
12113
|
function buildResolvedMetadataPayload(result) {
|
|
11886
12114
|
const gatewayInfo = extractGatewayRoutingInfo(result);
|
|
11887
12115
|
const metadata = {};
|
|
11888
|
-
if (_optionalChain([gatewayInfo, 'optionalAccess',
|
|
12116
|
+
if (_optionalChain([gatewayInfo, 'optionalAccess', _214 => _214.provider])) {
|
|
11889
12117
|
metadata.provider = gatewayInfo.provider;
|
|
11890
12118
|
}
|
|
11891
|
-
if (_optionalChain([gatewayInfo, 'optionalAccess',
|
|
12119
|
+
if (_optionalChain([gatewayInfo, 'optionalAccess', _215 => _215.model])) {
|
|
11892
12120
|
metadata.model = gatewayInfo.model;
|
|
11893
12121
|
}
|
|
11894
12122
|
let finishReason;
|
|
@@ -11909,7 +12137,7 @@ function resolveAISDKModel(model, aiSDK) {
|
|
|
11909
12137
|
if (typeof model !== "string") {
|
|
11910
12138
|
return model;
|
|
11911
12139
|
}
|
|
11912
|
-
const provider = _nullishCoalesce(_nullishCoalesce(globalThis.AI_SDK_DEFAULT_PROVIDER, () => ( _optionalChain([aiSDK, 'optionalAccess',
|
|
12140
|
+
const provider = _nullishCoalesce(_nullishCoalesce(globalThis.AI_SDK_DEFAULT_PROVIDER, () => ( _optionalChain([aiSDK, 'optionalAccess', _216 => _216.gateway]))), () => ( null));
|
|
11913
12141
|
if (provider && typeof provider.languageModel === "function") {
|
|
11914
12142
|
return provider.languageModel(model);
|
|
11915
12143
|
}
|
|
@@ -11930,6 +12158,38 @@ function processAISDKOutput(output, denyOutputPaths) {
|
|
|
11930
12158
|
const merged = extractSerializableOutputFields(output);
|
|
11931
12159
|
return normalizeAISDKLoggedOutput(omit(merged, denyOutputPaths));
|
|
11932
12160
|
}
|
|
12161
|
+
function processAISDKEmbeddingOutput(output, denyOutputPaths) {
|
|
12162
|
+
if (!output || typeof output !== "object") {
|
|
12163
|
+
return output;
|
|
12164
|
+
}
|
|
12165
|
+
const summarized = {};
|
|
12166
|
+
const whitelistedFields = [
|
|
12167
|
+
"usage",
|
|
12168
|
+
"totalUsage",
|
|
12169
|
+
"warnings",
|
|
12170
|
+
"providerMetadata",
|
|
12171
|
+
"experimental_providerMetadata"
|
|
12172
|
+
];
|
|
12173
|
+
for (const field of whitelistedFields) {
|
|
12174
|
+
const value = safeSerializableFieldRead(output, field);
|
|
12175
|
+
if (value !== void 0 && isSerializableOutputValue(value)) {
|
|
12176
|
+
summarized[field] = value;
|
|
12177
|
+
}
|
|
12178
|
+
}
|
|
12179
|
+
const embedding = safeSerializableFieldRead(output, "embedding");
|
|
12180
|
+
if (Array.isArray(embedding)) {
|
|
12181
|
+
summarized.embedding_length = embedding.length;
|
|
12182
|
+
}
|
|
12183
|
+
const embeddings = safeSerializableFieldRead(output, "embeddings");
|
|
12184
|
+
if (Array.isArray(embeddings)) {
|
|
12185
|
+
summarized.embedding_count = embeddings.length;
|
|
12186
|
+
const firstEmbedding = embeddings.find((item) => Array.isArray(item));
|
|
12187
|
+
if (Array.isArray(firstEmbedding)) {
|
|
12188
|
+
summarized.embedding_length = firstEmbedding.length;
|
|
12189
|
+
}
|
|
12190
|
+
}
|
|
12191
|
+
return normalizeAISDKLoggedOutput(omit(summarized, denyOutputPaths));
|
|
12192
|
+
}
|
|
11933
12193
|
function extractTokenMetrics(result) {
|
|
11934
12194
|
const metrics = {};
|
|
11935
12195
|
let usage;
|
|
@@ -11947,7 +12207,7 @@ function extractTokenMetrics(result) {
|
|
|
11947
12207
|
return metrics;
|
|
11948
12208
|
}
|
|
11949
12209
|
const promptTokens = firstNumber(
|
|
11950
|
-
_optionalChain([usage, 'access',
|
|
12210
|
+
_optionalChain([usage, 'access', _217 => _217.inputTokens, 'optionalAccess', _218 => _218.total]),
|
|
11951
12211
|
usage.inputTokens,
|
|
11952
12212
|
usage.promptTokens,
|
|
11953
12213
|
usage.prompt_tokens
|
|
@@ -11956,7 +12216,7 @@ function extractTokenMetrics(result) {
|
|
|
11956
12216
|
metrics.prompt_tokens = promptTokens;
|
|
11957
12217
|
}
|
|
11958
12218
|
const completionTokens = firstNumber(
|
|
11959
|
-
_optionalChain([usage, 'access',
|
|
12219
|
+
_optionalChain([usage, 'access', _219 => _219.outputTokens, 'optionalAccess', _220 => _220.total]),
|
|
11960
12220
|
usage.outputTokens,
|
|
11961
12221
|
usage.completionTokens,
|
|
11962
12222
|
usage.completion_tokens
|
|
@@ -11983,7 +12243,7 @@ function safeResultFieldRead(result, field) {
|
|
|
11983
12243
|
}
|
|
11984
12244
|
function safeSerializableFieldRead(obj, field) {
|
|
11985
12245
|
try {
|
|
11986
|
-
const value = _optionalChain([obj, 'optionalAccess',
|
|
12246
|
+
const value = _optionalChain([obj, 'optionalAccess', _221 => _221[field]]);
|
|
11987
12247
|
if (isPromiseLike(value)) {
|
|
11988
12248
|
void Promise.resolve(value).catch(() => {
|
|
11989
12249
|
});
|
|
@@ -12028,12 +12288,17 @@ function extractGetterValues(obj) {
|
|
|
12028
12288
|
"content",
|
|
12029
12289
|
"text",
|
|
12030
12290
|
"object",
|
|
12291
|
+
"value",
|
|
12292
|
+
"values",
|
|
12293
|
+
"embedding",
|
|
12294
|
+
"embeddings",
|
|
12031
12295
|
"finishReason",
|
|
12032
12296
|
"usage",
|
|
12033
12297
|
"totalUsage",
|
|
12034
12298
|
"toolCalls",
|
|
12035
12299
|
"toolResults",
|
|
12036
12300
|
"warnings",
|
|
12301
|
+
"responses",
|
|
12037
12302
|
"experimental_providerMetadata",
|
|
12038
12303
|
"providerMetadata",
|
|
12039
12304
|
"rawResponse",
|
|
@@ -12072,7 +12337,7 @@ function extractSerializableOutputFields(output) {
|
|
|
12072
12337
|
];
|
|
12073
12338
|
for (const name of directFieldNames) {
|
|
12074
12339
|
try {
|
|
12075
|
-
const value = _optionalChain([output, 'optionalAccess',
|
|
12340
|
+
const value = _optionalChain([output, 'optionalAccess', _222 => _222[name]]);
|
|
12076
12341
|
if (isPromiseLike(value)) {
|
|
12077
12342
|
void Promise.resolve(value).catch(() => {
|
|
12078
12343
|
});
|
|
@@ -12108,8 +12373,8 @@ function isSerializableOutputValue(value) {
|
|
|
12108
12373
|
return true;
|
|
12109
12374
|
}
|
|
12110
12375
|
function serializeModelWithProvider(model) {
|
|
12111
|
-
const modelId = typeof model === "string" ? model : _optionalChain([model, 'optionalAccess',
|
|
12112
|
-
const explicitProvider = typeof model === "object" ? _optionalChain([model, 'optionalAccess',
|
|
12376
|
+
const modelId = typeof model === "string" ? model : _optionalChain([model, 'optionalAccess', _223 => _223.modelId]);
|
|
12377
|
+
const explicitProvider = typeof model === "object" ? _optionalChain([model, 'optionalAccess', _224 => _224.provider]) : void 0;
|
|
12113
12378
|
if (!modelId) {
|
|
12114
12379
|
return { model: modelId, provider: explicitProvider };
|
|
12115
12380
|
}
|
|
@@ -12135,7 +12400,7 @@ function parseGatewayModelString(modelString) {
|
|
|
12135
12400
|
function extractGatewayRoutingInfo(result) {
|
|
12136
12401
|
const steps = safeSerializableFieldRead(result, "steps");
|
|
12137
12402
|
if (Array.isArray(steps) && steps.length > 0) {
|
|
12138
|
-
const routing2 = _optionalChain([steps, 'access',
|
|
12403
|
+
const routing2 = _optionalChain([steps, 'access', _225 => _225[0], 'optionalAccess', _226 => _226.providerMetadata, 'optionalAccess', _227 => _227.gateway, 'optionalAccess', _228 => _228.routing]);
|
|
12139
12404
|
if (routing2) {
|
|
12140
12405
|
return {
|
|
12141
12406
|
provider: routing2.resolvedProvider || routing2.finalProvider,
|
|
@@ -12147,7 +12412,7 @@ function extractGatewayRoutingInfo(result) {
|
|
|
12147
12412
|
result,
|
|
12148
12413
|
"providerMetadata"
|
|
12149
12414
|
);
|
|
12150
|
-
const routing = _optionalChain([providerMetadata, 'optionalAccess',
|
|
12415
|
+
const routing = _optionalChain([providerMetadata, 'optionalAccess', _229 => _229.gateway, 'optionalAccess', _230 => _230.routing]);
|
|
12151
12416
|
if (routing) {
|
|
12152
12417
|
return {
|
|
12153
12418
|
provider: routing.resolvedProvider || routing.finalProvider,
|
|
@@ -12162,8 +12427,8 @@ function extractCostFromResult(result) {
|
|
|
12162
12427
|
let totalCost = 0;
|
|
12163
12428
|
let foundCost = false;
|
|
12164
12429
|
for (const step of steps) {
|
|
12165
|
-
const gateway2 = _optionalChain([step, 'optionalAccess',
|
|
12166
|
-
const stepCost = parseGatewayCost(_optionalChain([gateway2, 'optionalAccess',
|
|
12430
|
+
const gateway2 = _optionalChain([step, 'optionalAccess', _231 => _231.providerMetadata, 'optionalAccess', _232 => _232.gateway]);
|
|
12431
|
+
const stepCost = parseGatewayCost(_optionalChain([gateway2, 'optionalAccess', _233 => _233.cost])) || parseGatewayCost(_optionalChain([gateway2, 'optionalAccess', _234 => _234.marketCost]));
|
|
12167
12432
|
if (stepCost !== void 0 && stepCost > 0) {
|
|
12168
12433
|
totalCost += stepCost;
|
|
12169
12434
|
foundCost = true;
|
|
@@ -12177,8 +12442,8 @@ function extractCostFromResult(result) {
|
|
|
12177
12442
|
result,
|
|
12178
12443
|
"providerMetadata"
|
|
12179
12444
|
);
|
|
12180
|
-
const gateway = _optionalChain([providerMetadata, 'optionalAccess',
|
|
12181
|
-
const directCost = parseGatewayCost(_optionalChain([gateway, 'optionalAccess',
|
|
12445
|
+
const gateway = _optionalChain([providerMetadata, 'optionalAccess', _235 => _235.gateway]);
|
|
12446
|
+
const directCost = parseGatewayCost(_optionalChain([gateway, 'optionalAccess', _236 => _236.cost])) || parseGatewayCost(_optionalChain([gateway, 'optionalAccess', _237 => _237.marketCost]));
|
|
12182
12447
|
if (directCost !== void 0 && directCost > 0) {
|
|
12183
12448
|
return directCost;
|
|
12184
12449
|
}
|
|
@@ -12294,101 +12559,401 @@ var claudeAgentSDKChannels = defineChannels(
|
|
|
12294
12559
|
}
|
|
12295
12560
|
);
|
|
12296
12561
|
|
|
12297
|
-
// src/instrumentation/plugins/claude-agent-sdk-
|
|
12298
|
-
|
|
12299
|
-
|
|
12300
|
-
|
|
12301
|
-
|
|
12302
|
-
|
|
12303
|
-
|
|
12304
|
-
|
|
12305
|
-
|
|
12306
|
-
|
|
12307
|
-
|
|
12308
|
-
"disallowedTools",
|
|
12309
|
-
"additionalDirectories",
|
|
12310
|
-
"permissionMode",
|
|
12311
|
-
"debug",
|
|
12312
|
-
"apiKey",
|
|
12313
|
-
"apiKeySource",
|
|
12314
|
-
"agentName",
|
|
12315
|
-
"instructions"
|
|
12316
|
-
];
|
|
12317
|
-
const filtered = {};
|
|
12318
|
-
for (const key of allowedKeys) {
|
|
12319
|
-
if (options[key] !== void 0) {
|
|
12320
|
-
filtered[key] = options[key];
|
|
12321
|
-
}
|
|
12562
|
+
// src/instrumentation/plugins/claude-agent-sdk-instrumentation-constants.ts
|
|
12563
|
+
var CLAUDE_AGENT_SDK_SKIP_LOCAL_TOOL_HOOKS_OPTION = "__braintrust_skip_local_tool_hooks";
|
|
12564
|
+
|
|
12565
|
+
// src/instrumentation/plugins/claude-agent-sdk-local-tool-context.ts
|
|
12566
|
+
var LOCAL_TOOL_CONTEXT_ASYNC_ITERATOR_PATCHED = Symbol.for(
|
|
12567
|
+
"braintrust.claude_agent_sdk.local_tool_context_async_iterator_patched"
|
|
12568
|
+
);
|
|
12569
|
+
function createLocalToolContextStore() {
|
|
12570
|
+
const maybeIsoWithAsyncLocalStorage = isomorph_default;
|
|
12571
|
+
if (typeof maybeIsoWithAsyncLocalStorage.newAsyncLocalStorage === "function") {
|
|
12572
|
+
return maybeIsoWithAsyncLocalStorage.newAsyncLocalStorage();
|
|
12322
12573
|
}
|
|
12323
|
-
|
|
12574
|
+
let currentStore;
|
|
12575
|
+
return {
|
|
12576
|
+
enterWith(store) {
|
|
12577
|
+
currentStore = store;
|
|
12578
|
+
},
|
|
12579
|
+
getStore() {
|
|
12580
|
+
return currentStore;
|
|
12581
|
+
},
|
|
12582
|
+
run(store, callback) {
|
|
12583
|
+
const previousStore = currentStore;
|
|
12584
|
+
currentStore = store;
|
|
12585
|
+
try {
|
|
12586
|
+
return callback();
|
|
12587
|
+
} finally {
|
|
12588
|
+
currentStore = previousStore;
|
|
12589
|
+
}
|
|
12590
|
+
}
|
|
12591
|
+
};
|
|
12324
12592
|
}
|
|
12325
|
-
|
|
12326
|
-
|
|
12327
|
-
|
|
12328
|
-
}
|
|
12329
|
-
const value = Reflect.get(obj, key);
|
|
12330
|
-
return typeof value === "number" ? value : void 0;
|
|
12593
|
+
var localToolContextStore = createLocalToolContextStore();
|
|
12594
|
+
var fallbackLocalToolParentResolver;
|
|
12595
|
+
function createClaudeLocalToolContext() {
|
|
12596
|
+
return {};
|
|
12331
12597
|
}
|
|
12332
|
-
function
|
|
12333
|
-
|
|
12334
|
-
|
|
12335
|
-
|
|
12336
|
-
|
|
12337
|
-
|
|
12338
|
-
|
|
12339
|
-
|
|
12340
|
-
if (
|
|
12341
|
-
return
|
|
12342
|
-
}
|
|
12343
|
-
const inputTokens = getNumberProperty(usage, "input_tokens");
|
|
12344
|
-
if (inputTokens !== void 0) {
|
|
12345
|
-
metrics.prompt_tokens = inputTokens;
|
|
12346
|
-
}
|
|
12347
|
-
const outputTokens = getNumberProperty(usage, "output_tokens");
|
|
12348
|
-
if (outputTokens !== void 0) {
|
|
12349
|
-
metrics.completion_tokens = outputTokens;
|
|
12350
|
-
}
|
|
12351
|
-
const cacheReadTokens = getNumberProperty(usage, "cache_read_input_tokens") || 0;
|
|
12352
|
-
const cacheCreationTokens = getNumberProperty(usage, "cache_creation_input_tokens") || 0;
|
|
12353
|
-
if (cacheReadTokens > 0 || cacheCreationTokens > 0) {
|
|
12354
|
-
Object.assign(
|
|
12355
|
-
metrics,
|
|
12356
|
-
extractAnthropicCacheTokens(cacheReadTokens, cacheCreationTokens)
|
|
12357
|
-
);
|
|
12358
|
-
}
|
|
12359
|
-
if (Object.keys(metrics).length > 0) {
|
|
12360
|
-
Object.assign(metrics, finalizeAnthropicTokens(metrics));
|
|
12598
|
+
function runWithClaudeLocalToolContext(callback, context) {
|
|
12599
|
+
return localToolContextStore.run(
|
|
12600
|
+
_nullishCoalesce(context, () => ( createClaudeLocalToolContext())),
|
|
12601
|
+
callback
|
|
12602
|
+
);
|
|
12603
|
+
}
|
|
12604
|
+
function ensureClaudeLocalToolContext() {
|
|
12605
|
+
const existing = localToolContextStore.getStore();
|
|
12606
|
+
if (existing) {
|
|
12607
|
+
return existing;
|
|
12361
12608
|
}
|
|
12362
|
-
|
|
12609
|
+
const created = {};
|
|
12610
|
+
localToolContextStore.enterWith(created);
|
|
12611
|
+
return created;
|
|
12363
12612
|
}
|
|
12364
|
-
function
|
|
12365
|
-
|
|
12366
|
-
|
|
12367
|
-
|
|
12368
|
-
|
|
12369
|
-
for (const msg of capturedPromptMessages) {
|
|
12370
|
-
const role = _optionalChain([msg, 'access', _238 => _238.message, 'optionalAccess', _239 => _239.role]);
|
|
12371
|
-
const content = _optionalChain([msg, 'access', _240 => _240.message, 'optionalAccess', _241 => _241.content]);
|
|
12372
|
-
if (role && content !== void 0) {
|
|
12373
|
-
promptMessages.push({ content, role });
|
|
12374
|
-
}
|
|
12375
|
-
}
|
|
12613
|
+
function setClaudeLocalToolParentResolver(resolver) {
|
|
12614
|
+
fallbackLocalToolParentResolver = resolver;
|
|
12615
|
+
const context = ensureClaudeLocalToolContext();
|
|
12616
|
+
if (!context) {
|
|
12617
|
+
return;
|
|
12376
12618
|
}
|
|
12377
|
-
|
|
12378
|
-
return inputParts.length > 0 ? inputParts : void 0;
|
|
12619
|
+
context.resolveLocalToolParent = resolver;
|
|
12379
12620
|
}
|
|
12380
|
-
function
|
|
12381
|
-
return
|
|
12621
|
+
function getClaudeLocalToolParentResolver() {
|
|
12622
|
+
return _nullishCoalesce(_optionalChain([localToolContextStore, 'access', _238 => _238.getStore, 'call', _239 => _239(), 'optionalAccess', _240 => _240.resolveLocalToolParent]), () => ( fallbackLocalToolParentResolver));
|
|
12382
12623
|
}
|
|
12383
|
-
|
|
12384
|
-
|
|
12385
|
-
|
|
12624
|
+
function isAsyncIterable2(value) {
|
|
12625
|
+
return value !== null && typeof value === "object" && Symbol.asyncIterator in value && typeof value[Symbol.asyncIterator] === "function";
|
|
12626
|
+
}
|
|
12627
|
+
function bindClaudeLocalToolContextToAsyncIterable(result, localToolContext) {
|
|
12628
|
+
if (!isAsyncIterable2(result) || Object.isFrozen(result) || Object.isSealed(result)) {
|
|
12629
|
+
return result;
|
|
12386
12630
|
}
|
|
12387
|
-
const
|
|
12388
|
-
|
|
12389
|
-
|
|
12631
|
+
const stream = result;
|
|
12632
|
+
const originalAsyncIterator = stream[Symbol.asyncIterator];
|
|
12633
|
+
if (originalAsyncIterator[LOCAL_TOOL_CONTEXT_ASYNC_ITERATOR_PATCHED]) {
|
|
12634
|
+
return result;
|
|
12390
12635
|
}
|
|
12391
|
-
const
|
|
12636
|
+
const patchedAsyncIterator = function() {
|
|
12637
|
+
return runWithClaudeLocalToolContext(() => {
|
|
12638
|
+
const iterator = Reflect.apply(originalAsyncIterator, this, []);
|
|
12639
|
+
if (!iterator || typeof iterator !== "object") {
|
|
12640
|
+
return iterator;
|
|
12641
|
+
}
|
|
12642
|
+
const patchMethod = (methodName) => {
|
|
12643
|
+
const originalMethod = Reflect.get(iterator, methodName);
|
|
12644
|
+
if (typeof originalMethod !== "function") {
|
|
12645
|
+
return;
|
|
12646
|
+
}
|
|
12647
|
+
Reflect.set(
|
|
12648
|
+
iterator,
|
|
12649
|
+
methodName,
|
|
12650
|
+
(...args) => runWithClaudeLocalToolContext(
|
|
12651
|
+
() => Reflect.apply(
|
|
12652
|
+
originalMethod,
|
|
12653
|
+
iterator,
|
|
12654
|
+
args
|
|
12655
|
+
),
|
|
12656
|
+
localToolContext
|
|
12657
|
+
)
|
|
12658
|
+
);
|
|
12659
|
+
};
|
|
12660
|
+
patchMethod("next");
|
|
12661
|
+
patchMethod("return");
|
|
12662
|
+
patchMethod("throw");
|
|
12663
|
+
return iterator;
|
|
12664
|
+
}, localToolContext);
|
|
12665
|
+
};
|
|
12666
|
+
Object.defineProperty(
|
|
12667
|
+
patchedAsyncIterator,
|
|
12668
|
+
LOCAL_TOOL_CONTEXT_ASYNC_ITERATOR_PATCHED,
|
|
12669
|
+
{
|
|
12670
|
+
configurable: false,
|
|
12671
|
+
enumerable: false,
|
|
12672
|
+
value: true,
|
|
12673
|
+
writable: false
|
|
12674
|
+
}
|
|
12675
|
+
);
|
|
12676
|
+
Reflect.set(stream, Symbol.asyncIterator, patchedAsyncIterator);
|
|
12677
|
+
return result;
|
|
12678
|
+
}
|
|
12679
|
+
|
|
12680
|
+
// src/instrumentation/plugins/claude-agent-sdk-local-tool-spans.ts
|
|
12681
|
+
var LOCAL_TOOL_HANDLER_WRAPPED = Symbol.for(
|
|
12682
|
+
"braintrust.claude_agent_sdk.local_tool_handler_wrapped"
|
|
12683
|
+
);
|
|
12684
|
+
function toErrorMessage(error) {
|
|
12685
|
+
return error instanceof Error ? error.message : String(error);
|
|
12686
|
+
}
|
|
12687
|
+
function isPromiseLike2(value) {
|
|
12688
|
+
return value !== null && typeof value === "object" && "then" in value && typeof value.then === "function";
|
|
12689
|
+
}
|
|
12690
|
+
function getToolUseIdFromExtra(extra) {
|
|
12691
|
+
if (!extra || typeof extra !== "object" || !("_meta" in extra)) {
|
|
12692
|
+
return void 0;
|
|
12693
|
+
}
|
|
12694
|
+
const meta = Reflect.get(extra, "_meta");
|
|
12695
|
+
if (!meta || typeof meta !== "object") {
|
|
12696
|
+
return void 0;
|
|
12697
|
+
}
|
|
12698
|
+
const toolUseId = Reflect.get(meta, "claudecode/toolUseId");
|
|
12699
|
+
return typeof toolUseId === "string" ? toolUseId : void 0;
|
|
12700
|
+
}
|
|
12701
|
+
function wrapLocalClaudeToolHandler(handler, getMetadata) {
|
|
12702
|
+
if (handler[LOCAL_TOOL_HANDLER_WRAPPED]) {
|
|
12703
|
+
return handler;
|
|
12704
|
+
}
|
|
12705
|
+
const wrappedHandler = function wrappedLocalToolHandler(...handlerArgs) {
|
|
12706
|
+
const metadata = getMetadata();
|
|
12707
|
+
const rawToolName = metadata.serverName ? `mcp__${metadata.serverName}__${metadata.toolName}` : metadata.toolName;
|
|
12708
|
+
const toolUseId = getToolUseIdFromExtra(handlerArgs[1]);
|
|
12709
|
+
const localToolParentResolver = getClaudeLocalToolParentResolver();
|
|
12710
|
+
const spanName = metadata.serverName ? `tool: ${metadata.serverName}/${metadata.toolName}` : `tool: ${metadata.toolName}`;
|
|
12711
|
+
const runWithResolvedParent = async () => {
|
|
12712
|
+
const parent = toolUseId && localToolParentResolver ? await localToolParentResolver(toolUseId).catch(() => void 0) : void 0;
|
|
12713
|
+
const span = startSpan({
|
|
12714
|
+
event: {
|
|
12715
|
+
input: handlerArgs[0],
|
|
12716
|
+
metadata: {
|
|
12717
|
+
"claude_agent_sdk.raw_tool_name": rawToolName,
|
|
12718
|
+
"gen_ai.tool.name": metadata.toolName,
|
|
12719
|
+
...toolUseId && { "gen_ai.tool.call.id": toolUseId },
|
|
12720
|
+
...metadata.serverName && {
|
|
12721
|
+
"mcp.server": metadata.serverName
|
|
12722
|
+
}
|
|
12723
|
+
}
|
|
12724
|
+
},
|
|
12725
|
+
name: spanName,
|
|
12726
|
+
...parent && { parent },
|
|
12727
|
+
spanAttributes: { type: "tool" /* TOOL */ }
|
|
12728
|
+
});
|
|
12729
|
+
const runHandler = () => Reflect.apply(handler, this, handlerArgs);
|
|
12730
|
+
const finalizeSuccess = (result) => {
|
|
12731
|
+
span.log({ output: result });
|
|
12732
|
+
span.end();
|
|
12733
|
+
return result;
|
|
12734
|
+
};
|
|
12735
|
+
const finalizeError = (error) => {
|
|
12736
|
+
span.log({ error: toErrorMessage(error) });
|
|
12737
|
+
span.end();
|
|
12738
|
+
throw error;
|
|
12739
|
+
};
|
|
12740
|
+
return withCurrent(span, () => {
|
|
12741
|
+
try {
|
|
12742
|
+
const result = runHandler();
|
|
12743
|
+
if (isPromiseLike2(result)) {
|
|
12744
|
+
return result.then(finalizeSuccess, finalizeError);
|
|
12745
|
+
}
|
|
12746
|
+
return finalizeSuccess(result);
|
|
12747
|
+
} catch (error) {
|
|
12748
|
+
return finalizeError(error);
|
|
12749
|
+
}
|
|
12750
|
+
});
|
|
12751
|
+
};
|
|
12752
|
+
return runWithResolvedParent();
|
|
12753
|
+
};
|
|
12754
|
+
Object.defineProperty(wrappedHandler, LOCAL_TOOL_HANDLER_WRAPPED, {
|
|
12755
|
+
configurable: false,
|
|
12756
|
+
enumerable: false,
|
|
12757
|
+
value: true,
|
|
12758
|
+
writable: false
|
|
12759
|
+
});
|
|
12760
|
+
return wrappedHandler;
|
|
12761
|
+
}
|
|
12762
|
+
function getRegisteredTools(instance) {
|
|
12763
|
+
if (!instance || typeof instance !== "object") {
|
|
12764
|
+
return void 0;
|
|
12765
|
+
}
|
|
12766
|
+
if (!("_registeredTools" in instance)) {
|
|
12767
|
+
return void 0;
|
|
12768
|
+
}
|
|
12769
|
+
const registeredTools = Reflect.get(instance, "_registeredTools");
|
|
12770
|
+
if (registeredTools instanceof Map) {
|
|
12771
|
+
return registeredTools;
|
|
12772
|
+
}
|
|
12773
|
+
if (registeredTools && typeof registeredTools === "object") {
|
|
12774
|
+
return registeredTools;
|
|
12775
|
+
}
|
|
12776
|
+
return void 0;
|
|
12777
|
+
}
|
|
12778
|
+
function wrapLocalMcpServerToolHandlers(serverName, serverConfig) {
|
|
12779
|
+
if (!serverConfig || typeof serverConfig !== "object") {
|
|
12780
|
+
return false;
|
|
12781
|
+
}
|
|
12782
|
+
if (!("instance" in serverConfig)) {
|
|
12783
|
+
return false;
|
|
12784
|
+
}
|
|
12785
|
+
const instance = Reflect.get(serverConfig, "instance");
|
|
12786
|
+
const registeredTools = getRegisteredTools(instance);
|
|
12787
|
+
if (!registeredTools) {
|
|
12788
|
+
return false;
|
|
12789
|
+
}
|
|
12790
|
+
let wrappedAny = false;
|
|
12791
|
+
const wrapHandler = (toolName, registration) => {
|
|
12792
|
+
if (!registration || typeof registration !== "object") {
|
|
12793
|
+
return;
|
|
12794
|
+
}
|
|
12795
|
+
const handler = Reflect.get(registration, "handler");
|
|
12796
|
+
if (typeof handler !== "function") {
|
|
12797
|
+
return;
|
|
12798
|
+
}
|
|
12799
|
+
const wrappedHandler = wrapLocalClaudeToolHandler(handler, () => ({
|
|
12800
|
+
serverName,
|
|
12801
|
+
toolName
|
|
12802
|
+
}));
|
|
12803
|
+
if (wrappedHandler !== handler) {
|
|
12804
|
+
Reflect.set(registration, "handler", wrappedHandler);
|
|
12805
|
+
wrappedAny = true;
|
|
12806
|
+
}
|
|
12807
|
+
};
|
|
12808
|
+
if (registeredTools instanceof Map) {
|
|
12809
|
+
for (const [toolName, registration] of registeredTools.entries()) {
|
|
12810
|
+
wrapHandler(toolName, registration);
|
|
12811
|
+
}
|
|
12812
|
+
return wrappedAny;
|
|
12813
|
+
}
|
|
12814
|
+
for (const [toolName, registration] of Object.entries(registeredTools)) {
|
|
12815
|
+
wrapHandler(toolName, registration);
|
|
12816
|
+
}
|
|
12817
|
+
return wrappedAny;
|
|
12818
|
+
}
|
|
12819
|
+
function collectLocalMcpServerToolHookNames(serverName, serverConfig) {
|
|
12820
|
+
const toolNames = /* @__PURE__ */ new Set();
|
|
12821
|
+
if (!serverConfig || typeof serverConfig !== "object") {
|
|
12822
|
+
return toolNames;
|
|
12823
|
+
}
|
|
12824
|
+
if ("instance" in serverConfig) {
|
|
12825
|
+
const instance = Reflect.get(serverConfig, "instance");
|
|
12826
|
+
const registeredTools = getRegisteredTools(instance);
|
|
12827
|
+
if (registeredTools instanceof Map) {
|
|
12828
|
+
for (const toolName of registeredTools.keys()) {
|
|
12829
|
+
toolNames.add(toolName);
|
|
12830
|
+
toolNames.add(`mcp__${serverName}__${toolName}`);
|
|
12831
|
+
}
|
|
12832
|
+
} else if (registeredTools) {
|
|
12833
|
+
for (const toolName of Object.keys(registeredTools)) {
|
|
12834
|
+
toolNames.add(toolName);
|
|
12835
|
+
toolNames.add(`mcp__${serverName}__${toolName}`);
|
|
12836
|
+
}
|
|
12837
|
+
}
|
|
12838
|
+
}
|
|
12839
|
+
if ("tools" in serverConfig) {
|
|
12840
|
+
const rawTools = Reflect.get(serverConfig, "tools");
|
|
12841
|
+
if (Array.isArray(rawTools)) {
|
|
12842
|
+
for (const tool of rawTools) {
|
|
12843
|
+
if (!tool || typeof tool !== "object") {
|
|
12844
|
+
continue;
|
|
12845
|
+
}
|
|
12846
|
+
const toolName = Reflect.get(tool, "name");
|
|
12847
|
+
if (typeof toolName !== "string") {
|
|
12848
|
+
continue;
|
|
12849
|
+
}
|
|
12850
|
+
toolNames.add(toolName);
|
|
12851
|
+
toolNames.add(`mcp__${serverName}__${toolName}`);
|
|
12852
|
+
}
|
|
12853
|
+
}
|
|
12854
|
+
}
|
|
12855
|
+
return toolNames;
|
|
12856
|
+
}
|
|
12857
|
+
|
|
12858
|
+
// src/instrumentation/plugins/claude-agent-sdk-plugin.ts
|
|
12859
|
+
var ROOT_LLM_PARENT_KEY = "__root__";
|
|
12860
|
+
function llmParentKey(parentToolUseId) {
|
|
12861
|
+
return _nullishCoalesce(parentToolUseId, () => ( ROOT_LLM_PARENT_KEY));
|
|
12862
|
+
}
|
|
12863
|
+
function isSubAgentToolName(toolName) {
|
|
12864
|
+
return toolName === "Agent" || toolName === "Task";
|
|
12865
|
+
}
|
|
12866
|
+
function filterSerializableOptions(options) {
|
|
12867
|
+
const allowedKeys = [
|
|
12868
|
+
"model",
|
|
12869
|
+
"maxTurns",
|
|
12870
|
+
"cwd",
|
|
12871
|
+
"continue",
|
|
12872
|
+
"allowedTools",
|
|
12873
|
+
"disallowedTools",
|
|
12874
|
+
"additionalDirectories",
|
|
12875
|
+
"permissionMode",
|
|
12876
|
+
"debug",
|
|
12877
|
+
"apiKey",
|
|
12878
|
+
"apiKeySource",
|
|
12879
|
+
"agentName",
|
|
12880
|
+
"instructions"
|
|
12881
|
+
];
|
|
12882
|
+
const filtered = {};
|
|
12883
|
+
for (const key of allowedKeys) {
|
|
12884
|
+
if (options[key] !== void 0) {
|
|
12885
|
+
filtered[key] = options[key];
|
|
12886
|
+
}
|
|
12887
|
+
}
|
|
12888
|
+
return filtered;
|
|
12889
|
+
}
|
|
12890
|
+
function getNumberProperty(obj, key) {
|
|
12891
|
+
if (!obj || typeof obj !== "object" || !(key in obj)) {
|
|
12892
|
+
return void 0;
|
|
12893
|
+
}
|
|
12894
|
+
const value = Reflect.get(obj, key);
|
|
12895
|
+
return typeof value === "number" ? value : void 0;
|
|
12896
|
+
}
|
|
12897
|
+
function extractUsageFromMessage(message) {
|
|
12898
|
+
const metrics = {};
|
|
12899
|
+
let usage;
|
|
12900
|
+
if (message.type === "assistant") {
|
|
12901
|
+
usage = _optionalChain([message, 'access', _241 => _241.message, 'optionalAccess', _242 => _242.usage]);
|
|
12902
|
+
} else if (message.type === "result") {
|
|
12903
|
+
usage = message.usage;
|
|
12904
|
+
}
|
|
12905
|
+
if (!usage || typeof usage !== "object") {
|
|
12906
|
+
return metrics;
|
|
12907
|
+
}
|
|
12908
|
+
const inputTokens = getNumberProperty(usage, "input_tokens");
|
|
12909
|
+
if (inputTokens !== void 0) {
|
|
12910
|
+
metrics.prompt_tokens = inputTokens;
|
|
12911
|
+
}
|
|
12912
|
+
const outputTokens = getNumberProperty(usage, "output_tokens");
|
|
12913
|
+
if (outputTokens !== void 0) {
|
|
12914
|
+
metrics.completion_tokens = outputTokens;
|
|
12915
|
+
}
|
|
12916
|
+
const cacheReadTokens = getNumberProperty(usage, "cache_read_input_tokens") || 0;
|
|
12917
|
+
const cacheCreationTokens = getNumberProperty(usage, "cache_creation_input_tokens") || 0;
|
|
12918
|
+
if (cacheReadTokens > 0 || cacheCreationTokens > 0) {
|
|
12919
|
+
Object.assign(
|
|
12920
|
+
metrics,
|
|
12921
|
+
extractAnthropicCacheTokens(cacheReadTokens, cacheCreationTokens)
|
|
12922
|
+
);
|
|
12923
|
+
}
|
|
12924
|
+
if (Object.keys(metrics).length > 0) {
|
|
12925
|
+
Object.assign(metrics, finalizeAnthropicTokens(metrics));
|
|
12926
|
+
}
|
|
12927
|
+
return metrics;
|
|
12928
|
+
}
|
|
12929
|
+
function buildLLMInput(prompt, conversationHistory, capturedPromptMessages) {
|
|
12930
|
+
const promptMessages = [];
|
|
12931
|
+
if (typeof prompt === "string") {
|
|
12932
|
+
promptMessages.push({ content: prompt, role: "user" });
|
|
12933
|
+
} else if (capturedPromptMessages && capturedPromptMessages.length > 0) {
|
|
12934
|
+
for (const msg of capturedPromptMessages) {
|
|
12935
|
+
const role = _optionalChain([msg, 'access', _243 => _243.message, 'optionalAccess', _244 => _244.role]);
|
|
12936
|
+
const content = _optionalChain([msg, 'access', _245 => _245.message, 'optionalAccess', _246 => _246.content]);
|
|
12937
|
+
if (role && content !== void 0) {
|
|
12938
|
+
promptMessages.push({ content, role });
|
|
12939
|
+
}
|
|
12940
|
+
}
|
|
12941
|
+
}
|
|
12942
|
+
const inputParts = [...promptMessages, ...conversationHistory];
|
|
12943
|
+
return inputParts.length > 0 ? inputParts : void 0;
|
|
12944
|
+
}
|
|
12945
|
+
function formatCapturedMessages(messages) {
|
|
12946
|
+
return messages.length > 0 ? messages : [];
|
|
12947
|
+
}
|
|
12948
|
+
async function createLLMSpanForMessages(messages, prompt, conversationHistory, options, startTime, capturedPromptMessages, parentSpan, existingSpan) {
|
|
12949
|
+
if (messages.length === 0) {
|
|
12950
|
+
return void 0;
|
|
12951
|
+
}
|
|
12952
|
+
const lastMessage = messages[messages.length - 1];
|
|
12953
|
+
if (lastMessage.type !== "assistant" || !_optionalChain([lastMessage, 'access', _247 => _247.message, 'optionalAccess', _248 => _248.usage])) {
|
|
12954
|
+
return void 0;
|
|
12955
|
+
}
|
|
12956
|
+
const model = lastMessage.message.model || options.model;
|
|
12392
12957
|
const usage = extractUsageFromMessage(lastMessage);
|
|
12393
12958
|
const input = buildLLMInput(
|
|
12394
12959
|
prompt,
|
|
@@ -12396,26 +12961,31 @@ async function createLLMSpanForMessages(messages, prompt, conversationHistory, o
|
|
|
12396
12961
|
capturedPromptMessages
|
|
12397
12962
|
);
|
|
12398
12963
|
const outputs = messages.map(
|
|
12399
|
-
(m) => _optionalChain([m, 'access',
|
|
12964
|
+
(m) => _optionalChain([m, 'access', _249 => _249.message, 'optionalAccess', _250 => _250.content]) && _optionalChain([m, 'access', _251 => _251.message, 'optionalAccess', _252 => _252.role]) ? { content: m.message.content, role: m.message.role } : void 0
|
|
12400
12965
|
).filter(
|
|
12401
12966
|
(c) => c !== void 0
|
|
12402
12967
|
);
|
|
12403
|
-
const span = startSpan({
|
|
12968
|
+
const span = _nullishCoalesce(existingSpan, () => ( startSpan({
|
|
12404
12969
|
name: "anthropic.messages.create",
|
|
12405
12970
|
parent: parentSpan,
|
|
12406
12971
|
spanAttributes: {
|
|
12407
12972
|
type: "llm" /* LLM */
|
|
12408
12973
|
},
|
|
12409
12974
|
startTime
|
|
12410
|
-
});
|
|
12975
|
+
})));
|
|
12411
12976
|
span.log({
|
|
12412
12977
|
input,
|
|
12413
12978
|
metadata: model ? { model } : void 0,
|
|
12414
12979
|
metrics: usage,
|
|
12415
12980
|
output: outputs
|
|
12416
12981
|
});
|
|
12982
|
+
const spanExport = await span.export();
|
|
12417
12983
|
await span.end();
|
|
12418
|
-
|
|
12984
|
+
const finalMessage = _optionalChain([lastMessage, 'access', _253 => _253.message, 'optionalAccess', _254 => _254.content]) && _optionalChain([lastMessage, 'access', _255 => _255.message, 'optionalAccess', _256 => _256.role]) ? { content: lastMessage.message.content, role: lastMessage.message.role } : void 0;
|
|
12985
|
+
return {
|
|
12986
|
+
finalMessage,
|
|
12987
|
+
spanExport
|
|
12988
|
+
};
|
|
12419
12989
|
}
|
|
12420
12990
|
function getMcpServerMetadata(serverName, mcpServers) {
|
|
12421
12991
|
if (!serverName || !mcpServers) {
|
|
@@ -12459,11 +13029,51 @@ function parseToolName(rawToolName) {
|
|
|
12459
13029
|
toolName: rawToolName
|
|
12460
13030
|
};
|
|
12461
13031
|
}
|
|
12462
|
-
function
|
|
13032
|
+
function isLocalToolUse(rawToolName, mcpServers) {
|
|
13033
|
+
const parsed = parseToolName(rawToolName);
|
|
13034
|
+
if (!parsed.mcpServer || !mcpServers) {
|
|
13035
|
+
return false;
|
|
13036
|
+
}
|
|
13037
|
+
const serverConfig = mcpServers[parsed.mcpServer];
|
|
13038
|
+
if (!serverConfig || typeof serverConfig !== "object") {
|
|
13039
|
+
return false;
|
|
13040
|
+
}
|
|
13041
|
+
return serverConfig.type === "sdk" || "transport" in serverConfig;
|
|
13042
|
+
}
|
|
13043
|
+
function prepareLocalToolHandlersInMcpServers(mcpServers) {
|
|
13044
|
+
const localToolHookNames = /* @__PURE__ */ new Set();
|
|
13045
|
+
if (!mcpServers) {
|
|
13046
|
+
return {
|
|
13047
|
+
hasLocalToolHandlers: false,
|
|
13048
|
+
localToolHookNames
|
|
13049
|
+
};
|
|
13050
|
+
}
|
|
13051
|
+
let hasLocalToolHandlers = false;
|
|
13052
|
+
for (const [serverName, serverConfig] of Object.entries(mcpServers)) {
|
|
13053
|
+
const toolNames = collectLocalMcpServerToolHookNames(
|
|
13054
|
+
serverName,
|
|
13055
|
+
serverConfig
|
|
13056
|
+
);
|
|
13057
|
+
for (const toolName of toolNames) {
|
|
13058
|
+
localToolHookNames.add(toolName);
|
|
13059
|
+
}
|
|
13060
|
+
if (toolNames.size > 0) {
|
|
13061
|
+
hasLocalToolHandlers = true;
|
|
13062
|
+
}
|
|
13063
|
+
if (wrapLocalMcpServerToolHandlers(serverName, serverConfig)) {
|
|
13064
|
+
hasLocalToolHandlers = true;
|
|
13065
|
+
}
|
|
13066
|
+
}
|
|
13067
|
+
return { hasLocalToolHandlers, localToolHookNames };
|
|
13068
|
+
}
|
|
13069
|
+
function createToolTracingHooks(resolveParentSpan, activeToolSpans, mcpServers, localToolHookNames, skipLocalToolHooks, subAgentSpans, endedSubAgentSpans) {
|
|
12463
13070
|
const preToolUse = async (input, toolUseID) => {
|
|
12464
13071
|
if (input.hook_event_name !== "PreToolUse" || !toolUseID) {
|
|
12465
13072
|
return {};
|
|
12466
13073
|
}
|
|
13074
|
+
if (skipLocalToolHooks && (isLocalToolUse(input.tool_name, mcpServers) || localToolHookNames.has(input.tool_name))) {
|
|
13075
|
+
return {};
|
|
13076
|
+
}
|
|
12467
13077
|
if (isSubAgentToolName(input.tool_name)) {
|
|
12468
13078
|
return {};
|
|
12469
13079
|
}
|
|
@@ -12492,23 +13102,26 @@ function createToolTracingHooks(resolveParentSpan, activeToolSpans, mcpServers,
|
|
|
12492
13102
|
if (input.hook_event_name !== "PostToolUse" || !toolUseID) {
|
|
12493
13103
|
return {};
|
|
12494
13104
|
}
|
|
13105
|
+
if (skipLocalToolHooks && (isLocalToolUse(input.tool_name, mcpServers) || localToolHookNames.has(input.tool_name))) {
|
|
13106
|
+
return {};
|
|
13107
|
+
}
|
|
12495
13108
|
const subAgentSpan = subAgentSpans.get(toolUseID);
|
|
12496
13109
|
if (subAgentSpan) {
|
|
12497
13110
|
try {
|
|
12498
13111
|
const response = input.tool_response;
|
|
12499
13112
|
const metadata = {};
|
|
12500
|
-
if (_optionalChain([response, 'optionalAccess',
|
|
13113
|
+
if (_optionalChain([response, 'optionalAccess', _257 => _257.status])) {
|
|
12501
13114
|
metadata["claude_agent_sdk.status"] = response.status;
|
|
12502
13115
|
}
|
|
12503
|
-
if (_optionalChain([response, 'optionalAccess',
|
|
13116
|
+
if (_optionalChain([response, 'optionalAccess', _258 => _258.totalDurationMs])) {
|
|
12504
13117
|
metadata["claude_agent_sdk.duration_ms"] = response.totalDurationMs;
|
|
12505
13118
|
}
|
|
12506
|
-
if (_optionalChain([response, 'optionalAccess',
|
|
13119
|
+
if (_optionalChain([response, 'optionalAccess', _259 => _259.totalToolUseCount]) !== void 0) {
|
|
12507
13120
|
metadata["claude_agent_sdk.tool_use_count"] = response.totalToolUseCount;
|
|
12508
13121
|
}
|
|
12509
13122
|
subAgentSpan.log({
|
|
12510
13123
|
metadata,
|
|
12511
|
-
output: _optionalChain([response, 'optionalAccess',
|
|
13124
|
+
output: _optionalChain([response, 'optionalAccess', _260 => _260.content])
|
|
12512
13125
|
});
|
|
12513
13126
|
} finally {
|
|
12514
13127
|
subAgentSpan.end();
|
|
@@ -12532,6 +13145,9 @@ function createToolTracingHooks(resolveParentSpan, activeToolSpans, mcpServers,
|
|
|
12532
13145
|
if (input.hook_event_name !== "PostToolUseFailure" || !toolUseID) {
|
|
12533
13146
|
return {};
|
|
12534
13147
|
}
|
|
13148
|
+
if (skipLocalToolHooks && (isLocalToolUse(input.tool_name, mcpServers) || localToolHookNames.has(input.tool_name))) {
|
|
13149
|
+
return {};
|
|
13150
|
+
}
|
|
12535
13151
|
const subAgentSpan = subAgentSpans.get(toolUseID);
|
|
12536
13152
|
if (subAgentSpan) {
|
|
12537
13153
|
try {
|
|
@@ -12566,11 +13182,13 @@ function createToolTracingHooks(resolveParentSpan, activeToolSpans, mcpServers,
|
|
|
12566
13182
|
};
|
|
12567
13183
|
return { postToolUse, postToolUseFailure, preToolUse };
|
|
12568
13184
|
}
|
|
12569
|
-
function injectTracingHooks(options, resolveParentSpan, activeToolSpans, subAgentSpans, endedSubAgentSpans) {
|
|
13185
|
+
function injectTracingHooks(options, resolveParentSpan, activeToolSpans, localToolHookNames, skipLocalToolHooks, subAgentSpans, endedSubAgentSpans) {
|
|
12570
13186
|
const { preToolUse, postToolUse, postToolUseFailure } = createToolTracingHooks(
|
|
12571
13187
|
resolveParentSpan,
|
|
12572
13188
|
activeToolSpans,
|
|
12573
13189
|
options.mcpServers,
|
|
13190
|
+
localToolHookNames,
|
|
13191
|
+
skipLocalToolHooks,
|
|
12574
13192
|
subAgentSpans,
|
|
12575
13193
|
endedSubAgentSpans
|
|
12576
13194
|
);
|
|
@@ -12600,7 +13218,8 @@ async function finalizeCurrentMessageGroup(state) {
|
|
|
12600
13218
|
if (state.currentMessages.length === 0) {
|
|
12601
13219
|
return;
|
|
12602
13220
|
}
|
|
12603
|
-
const parentToolUseId = _nullishCoalesce(_optionalChain([state, 'access',
|
|
13221
|
+
const parentToolUseId = _nullishCoalesce(_optionalChain([state, 'access', _261 => _261.currentMessages, 'access', _262 => _262[0], 'optionalAccess', _263 => _263.parent_tool_use_id]), () => ( null));
|
|
13222
|
+
const parentKey = llmParentKey(parentToolUseId);
|
|
12604
13223
|
let parentSpan = await state.span.export();
|
|
12605
13224
|
if (parentToolUseId) {
|
|
12606
13225
|
const subAgentSpan = state.subAgentSpans.get(parentToolUseId);
|
|
@@ -12608,26 +13227,39 @@ async function finalizeCurrentMessageGroup(state) {
|
|
|
12608
13227
|
parentSpan = await subAgentSpan.export();
|
|
12609
13228
|
}
|
|
12610
13229
|
}
|
|
12611
|
-
const
|
|
13230
|
+
const existingLlmSpan = state.activeLlmSpansByParentToolUse.get(parentKey);
|
|
13231
|
+
const llmSpanResult = await createLLMSpanForMessages(
|
|
12612
13232
|
state.currentMessages,
|
|
12613
13233
|
state.originalPrompt,
|
|
12614
13234
|
state.finalResults,
|
|
12615
13235
|
state.options,
|
|
12616
13236
|
state.currentMessageStartTime,
|
|
12617
13237
|
state.capturedPromptMessages,
|
|
12618
|
-
parentSpan
|
|
13238
|
+
parentSpan,
|
|
13239
|
+
existingLlmSpan
|
|
12619
13240
|
);
|
|
12620
|
-
|
|
12621
|
-
|
|
13241
|
+
state.activeLlmSpansByParentToolUse.delete(parentKey);
|
|
13242
|
+
if (llmSpanResult) {
|
|
13243
|
+
if (parentToolUseId) {
|
|
13244
|
+
state.latestLlmParentBySubAgentToolUse.set(
|
|
13245
|
+
parentToolUseId,
|
|
13246
|
+
llmSpanResult.spanExport
|
|
13247
|
+
);
|
|
13248
|
+
} else {
|
|
13249
|
+
state.latestRootLlmParentRef.value = llmSpanResult.spanExport;
|
|
13250
|
+
}
|
|
13251
|
+
if (llmSpanResult.finalMessage) {
|
|
13252
|
+
state.finalResults.push(llmSpanResult.finalMessage);
|
|
13253
|
+
}
|
|
12622
13254
|
}
|
|
12623
13255
|
const lastMessage = state.currentMessages[state.currentMessages.length - 1];
|
|
12624
|
-
if (_optionalChain([lastMessage, 'optionalAccess',
|
|
13256
|
+
if (_optionalChain([lastMessage, 'optionalAccess', _264 => _264.message, 'optionalAccess', _265 => _265.usage])) {
|
|
12625
13257
|
state.accumulatedOutputTokens += getNumberProperty(lastMessage.message.usage, "output_tokens") || 0;
|
|
12626
13258
|
}
|
|
12627
13259
|
state.currentMessages.length = 0;
|
|
12628
13260
|
}
|
|
12629
13261
|
function maybeTrackToolUseContext(state, message) {
|
|
12630
|
-
if (message.type !== "assistant" || !Array.isArray(_optionalChain([message, 'access',
|
|
13262
|
+
if (message.type !== "assistant" || !Array.isArray(_optionalChain([message, 'access', _266 => _266.message, 'optionalAccess', _267 => _267.content]))) {
|
|
12631
13263
|
return;
|
|
12632
13264
|
}
|
|
12633
13265
|
const parentToolUseId = _nullishCoalesce(message.parent_tool_use_id, () => ( null));
|
|
@@ -12679,13 +13311,36 @@ async function ensureSubAgentSpan(pendingSubAgentNames, rootSpan, subAgentSpans,
|
|
|
12679
13311
|
async function handleStreamMessage(state, message) {
|
|
12680
13312
|
maybeTrackToolUseContext(state, message);
|
|
12681
13313
|
await maybeStartSubAgentSpan(state, message);
|
|
12682
|
-
const messageId = _optionalChain([message, 'access',
|
|
13314
|
+
const messageId = _optionalChain([message, 'access', _268 => _268.message, 'optionalAccess', _269 => _269.id]);
|
|
12683
13315
|
if (messageId && messageId !== state.currentMessageId) {
|
|
12684
13316
|
await finalizeCurrentMessageGroup(state);
|
|
12685
13317
|
state.currentMessageId = messageId;
|
|
12686
13318
|
state.currentMessageStartTime = getCurrentUnixTimestamp();
|
|
12687
13319
|
}
|
|
12688
|
-
if (message.type === "assistant" && _optionalChain([message, 'access',
|
|
13320
|
+
if (message.type === "assistant" && _optionalChain([message, 'access', _270 => _270.message, 'optionalAccess', _271 => _271.usage])) {
|
|
13321
|
+
const parentToolUseId = _nullishCoalesce(message.parent_tool_use_id, () => ( null));
|
|
13322
|
+
const parentKey = llmParentKey(parentToolUseId);
|
|
13323
|
+
if (!state.activeLlmSpansByParentToolUse.has(parentKey)) {
|
|
13324
|
+
let llmParentSpan = await state.span.export();
|
|
13325
|
+
if (parentToolUseId) {
|
|
13326
|
+
const subAgentSpan = await ensureSubAgentSpan(
|
|
13327
|
+
state.pendingSubAgentNames,
|
|
13328
|
+
state.span,
|
|
13329
|
+
state.subAgentSpans,
|
|
13330
|
+
parentToolUseId
|
|
13331
|
+
);
|
|
13332
|
+
llmParentSpan = await subAgentSpan.export();
|
|
13333
|
+
}
|
|
13334
|
+
const llmSpan = startSpan({
|
|
13335
|
+
name: "anthropic.messages.create",
|
|
13336
|
+
parent: llmParentSpan,
|
|
13337
|
+
spanAttributes: {
|
|
13338
|
+
type: "llm" /* LLM */
|
|
13339
|
+
},
|
|
13340
|
+
startTime: state.currentMessageStartTime
|
|
13341
|
+
});
|
|
13342
|
+
state.activeLlmSpansByParentToolUse.set(parentKey, llmSpan);
|
|
13343
|
+
}
|
|
12689
13344
|
state.currentMessages.push(message);
|
|
12690
13345
|
}
|
|
12691
13346
|
if (message.type !== "result" || !message.usage) {
|
|
@@ -12694,7 +13349,7 @@ async function handleStreamMessage(state, message) {
|
|
|
12694
13349
|
const finalUsageMetrics = extractUsageFromMessage(message);
|
|
12695
13350
|
if (state.currentMessages.length > 0 && finalUsageMetrics.completion_tokens !== void 0) {
|
|
12696
13351
|
const lastMessage = state.currentMessages[state.currentMessages.length - 1];
|
|
12697
|
-
if (_optionalChain([lastMessage, 'optionalAccess',
|
|
13352
|
+
if (_optionalChain([lastMessage, 'optionalAccess', _272 => _272.message, 'optionalAccess', _273 => _273.usage])) {
|
|
12698
13353
|
const adjustedTokens = finalUsageMetrics.completion_tokens - state.accumulatedOutputTokens;
|
|
12699
13354
|
if (adjustedTokens >= 0) {
|
|
12700
13355
|
lastMessage.message.usage.output_tokens = adjustedTokens;
|
|
@@ -12746,6 +13401,10 @@ async function finalizeQuerySpan(state) {
|
|
|
12746
13401
|
}
|
|
12747
13402
|
}
|
|
12748
13403
|
} finally {
|
|
13404
|
+
for (const llmSpan of state.activeLlmSpansByParentToolUse.values()) {
|
|
13405
|
+
llmSpan.end();
|
|
13406
|
+
}
|
|
13407
|
+
state.activeLlmSpansByParentToolUse.clear();
|
|
12749
13408
|
for (const [id, subAgentSpan] of state.subAgentSpans) {
|
|
12750
13409
|
if (!state.endedSubAgentSpans.has(id)) {
|
|
12751
13410
|
subAgentSpan.end();
|
|
@@ -12791,7 +13450,7 @@ var ClaudeAgentSDKPlugin = class extends BasePlugin {
|
|
|
12791
13450
|
yield message;
|
|
12792
13451
|
}
|
|
12793
13452
|
} finally {
|
|
12794
|
-
_optionalChain([resolvePromptDone, 'optionalCall',
|
|
13453
|
+
_optionalChain([resolvePromptDone, 'optionalCall', _274 => _274()]);
|
|
12795
13454
|
}
|
|
12796
13455
|
})();
|
|
12797
13456
|
}
|
|
@@ -12811,26 +13470,51 @@ var ClaudeAgentSDKPlugin = class extends BasePlugin {
|
|
|
12811
13470
|
console.error("Error extracting input for Claude Agent SDK:", error);
|
|
12812
13471
|
}
|
|
12813
13472
|
const activeToolSpans = /* @__PURE__ */ new Map();
|
|
13473
|
+
const activeLlmSpansByParentToolUse = /* @__PURE__ */ new Map();
|
|
12814
13474
|
const subAgentSpans = /* @__PURE__ */ new Map();
|
|
12815
13475
|
const endedSubAgentSpans = /* @__PURE__ */ new Set();
|
|
12816
13476
|
const toolUseToParent = /* @__PURE__ */ new Map();
|
|
13477
|
+
const latestLlmParentBySubAgentToolUse = /* @__PURE__ */ new Map();
|
|
13478
|
+
const latestRootLlmParentRef = {
|
|
13479
|
+
value: void 0
|
|
13480
|
+
};
|
|
12817
13481
|
const pendingSubAgentNames = /* @__PURE__ */ new Map();
|
|
13482
|
+
const localToolContext = createClaudeLocalToolContext();
|
|
13483
|
+
const { hasLocalToolHandlers, localToolHookNames } = prepareLocalToolHandlersInMcpServers(options.mcpServers);
|
|
13484
|
+
const skipLocalToolHooks = options[CLAUDE_AGENT_SDK_SKIP_LOCAL_TOOL_HOOKS_OPTION] === true || hasLocalToolHandlers;
|
|
13485
|
+
const resolveToolUseParentSpan = async (toolUseID) => {
|
|
13486
|
+
const parentToolUseId = _nullishCoalesce(toolUseToParent.get(toolUseID), () => ( null));
|
|
13487
|
+
const parentKey = llmParentKey(parentToolUseId);
|
|
13488
|
+
const activeLlmSpan = activeLlmSpansByParentToolUse.get(parentKey);
|
|
13489
|
+
if (activeLlmSpan) {
|
|
13490
|
+
return activeLlmSpan.export();
|
|
13491
|
+
}
|
|
13492
|
+
if (parentToolUseId) {
|
|
13493
|
+
const parentLlm = latestLlmParentBySubAgentToolUse.get(parentToolUseId);
|
|
13494
|
+
if (parentLlm) {
|
|
13495
|
+
return parentLlm;
|
|
13496
|
+
}
|
|
13497
|
+
const subAgentSpan = await ensureSubAgentSpan(
|
|
13498
|
+
pendingSubAgentNames,
|
|
13499
|
+
span,
|
|
13500
|
+
subAgentSpans,
|
|
13501
|
+
parentToolUseId
|
|
13502
|
+
);
|
|
13503
|
+
return subAgentSpan.export();
|
|
13504
|
+
}
|
|
13505
|
+
if (latestRootLlmParentRef.value) {
|
|
13506
|
+
return latestRootLlmParentRef.value;
|
|
13507
|
+
}
|
|
13508
|
+
return span.export();
|
|
13509
|
+
};
|
|
13510
|
+
localToolContext.resolveLocalToolParent = resolveToolUseParentSpan;
|
|
13511
|
+
setClaudeLocalToolParentResolver(resolveToolUseParentSpan);
|
|
12818
13512
|
const optionsWithHooks = injectTracingHooks(
|
|
12819
13513
|
options,
|
|
12820
|
-
|
|
12821
|
-
const parentToolUseId = toolUseToParent.get(toolUseID);
|
|
12822
|
-
if (parentToolUseId) {
|
|
12823
|
-
const subAgentSpan = await ensureSubAgentSpan(
|
|
12824
|
-
pendingSubAgentNames,
|
|
12825
|
-
span,
|
|
12826
|
-
subAgentSpans,
|
|
12827
|
-
parentToolUseId
|
|
12828
|
-
);
|
|
12829
|
-
return subAgentSpan.export();
|
|
12830
|
-
}
|
|
12831
|
-
return span.export();
|
|
12832
|
-
},
|
|
13514
|
+
resolveToolUseParentSpan,
|
|
12833
13515
|
activeToolSpans,
|
|
13516
|
+
localToolHookNames,
|
|
13517
|
+
skipLocalToolHooks,
|
|
12834
13518
|
subAgentSpans,
|
|
12835
13519
|
endedSubAgentSpans
|
|
12836
13520
|
);
|
|
@@ -12838,6 +13522,7 @@ var ClaudeAgentSDKPlugin = class extends BasePlugin {
|
|
|
12838
13522
|
event.arguments[0] = params;
|
|
12839
13523
|
spans.set(event, {
|
|
12840
13524
|
accumulatedOutputTokens: 0,
|
|
13525
|
+
activeLlmSpansByParentToolUse,
|
|
12841
13526
|
activeToolSpans,
|
|
12842
13527
|
capturedPromptMessages,
|
|
12843
13528
|
currentMessageId: void 0,
|
|
@@ -12853,7 +13538,10 @@ var ClaudeAgentSDKPlugin = class extends BasePlugin {
|
|
|
12853
13538
|
promptStarted: () => promptStarted,
|
|
12854
13539
|
span,
|
|
12855
13540
|
subAgentSpans,
|
|
12856
|
-
|
|
13541
|
+
latestLlmParentBySubAgentToolUse,
|
|
13542
|
+
latestRootLlmParentRef,
|
|
13543
|
+
toolUseToParent,
|
|
13544
|
+
localToolContext
|
|
12857
13545
|
});
|
|
12858
13546
|
},
|
|
12859
13547
|
end: (event) => {
|
|
@@ -12861,7 +13549,10 @@ var ClaudeAgentSDKPlugin = class extends BasePlugin {
|
|
|
12861
13549
|
if (!state) {
|
|
12862
13550
|
return;
|
|
12863
13551
|
}
|
|
12864
|
-
const eventResult =
|
|
13552
|
+
const eventResult = bindClaudeLocalToolContextToAsyncIterable(
|
|
13553
|
+
event.result,
|
|
13554
|
+
state.localToolContext
|
|
13555
|
+
);
|
|
12865
13556
|
if (eventResult === void 0) {
|
|
12866
13557
|
state.span.end();
|
|
12867
13558
|
spans.delete(event);
|
|
@@ -13023,7 +13714,7 @@ var GoogleGenAIPlugin = class extends BasePlugin {
|
|
|
13023
13714
|
};
|
|
13024
13715
|
tracingChannel2.subscribe(handlers);
|
|
13025
13716
|
this.unsubscribers.push(() => {
|
|
13026
|
-
_optionalChain([unbindCurrentSpanStore, 'optionalCall',
|
|
13717
|
+
_optionalChain([unbindCurrentSpanStore, 'optionalCall', _275 => _275()]);
|
|
13027
13718
|
tracingChannel2.unsubscribe(handlers);
|
|
13028
13719
|
});
|
|
13029
13720
|
}
|
|
@@ -13066,21 +13757,22 @@ function ensureSpanState(states, event, create) {
|
|
|
13066
13757
|
}
|
|
13067
13758
|
function bindCurrentSpanStoreToStart2(tracingChannel2, states, create) {
|
|
13068
13759
|
const state = _internalGetGlobalState();
|
|
13760
|
+
const contextManager = _optionalChain([state, 'optionalAccess', _276 => _276.contextManager]);
|
|
13069
13761
|
const startChannel = tracingChannel2.start;
|
|
13070
|
-
const currentSpanStore =
|
|
13071
|
-
if (!_optionalChain([startChannel, 'optionalAccess',
|
|
13762
|
+
const currentSpanStore = contextManager ? contextManager[BRAINTRUST_CURRENT_SPAN_STORE] : void 0;
|
|
13763
|
+
if (!_optionalChain([startChannel, 'optionalAccess', _277 => _277.bindStore]) || !currentSpanStore) {
|
|
13072
13764
|
return void 0;
|
|
13073
13765
|
}
|
|
13074
|
-
startChannel.bindStore(
|
|
13075
|
-
|
|
13076
|
-
(event) => ensureSpanState(
|
|
13766
|
+
startChannel.bindStore(currentSpanStore, (event) => {
|
|
13767
|
+
const span = ensureSpanState(
|
|
13077
13768
|
states,
|
|
13078
13769
|
event,
|
|
13079
13770
|
() => create(event)
|
|
13080
|
-
).span
|
|
13081
|
-
|
|
13771
|
+
).span;
|
|
13772
|
+
return contextManager.wrapSpanForStore(span);
|
|
13773
|
+
});
|
|
13082
13774
|
return () => {
|
|
13083
|
-
_optionalChain([startChannel, 'access',
|
|
13775
|
+
_optionalChain([startChannel, 'access', _278 => _278.unbindStore, 'optionalCall', _279 => _279(currentSpanStore)]);
|
|
13084
13776
|
};
|
|
13085
13777
|
}
|
|
13086
13778
|
function logErrorAndEndSpan(states, event) {
|
|
@@ -13301,7 +13993,7 @@ function serializePart(part) {
|
|
|
13301
13993
|
return part;
|
|
13302
13994
|
}
|
|
13303
13995
|
function serializeTools(params) {
|
|
13304
|
-
if (!_optionalChain([params, 'access',
|
|
13996
|
+
if (!_optionalChain([params, 'access', _280 => _280.config, 'optionalAccess', _281 => _281.tools])) {
|
|
13305
13997
|
return null;
|
|
13306
13998
|
}
|
|
13307
13999
|
try {
|
|
@@ -13344,7 +14036,7 @@ function extractGenerateContentMetrics(response, startTime) {
|
|
|
13344
14036
|
metrics.end = end;
|
|
13345
14037
|
metrics.duration = end - startTime;
|
|
13346
14038
|
}
|
|
13347
|
-
if (_optionalChain([response, 'optionalAccess',
|
|
14039
|
+
if (_optionalChain([response, 'optionalAccess', _282 => _282.usageMetadata])) {
|
|
13348
14040
|
populateUsageMetrics(metrics, response.usageMetadata);
|
|
13349
14041
|
}
|
|
13350
14042
|
return metrics;
|
|
@@ -13391,7 +14083,7 @@ function aggregateGenerateContentChunks(chunks, startTime, firstTokenTime) {
|
|
|
13391
14083
|
}
|
|
13392
14084
|
if (chunk.candidates && Array.isArray(chunk.candidates)) {
|
|
13393
14085
|
for (const candidate of chunk.candidates) {
|
|
13394
|
-
if (_optionalChain([candidate, 'access',
|
|
14086
|
+
if (_optionalChain([candidate, 'access', _283 => _283.content, 'optionalAccess', _284 => _284.parts])) {
|
|
13395
14087
|
for (const part of candidate.content.parts) {
|
|
13396
14088
|
if (part.text !== void 0) {
|
|
13397
14089
|
if (part.thought) {
|
|
@@ -13422,7 +14114,7 @@ function aggregateGenerateContentChunks(chunks, startTime, firstTokenTime) {
|
|
|
13422
14114
|
parts.push({ text });
|
|
13423
14115
|
}
|
|
13424
14116
|
parts.push(...otherParts);
|
|
13425
|
-
if (parts.length > 0 && _optionalChain([lastResponse, 'optionalAccess',
|
|
14117
|
+
if (parts.length > 0 && _optionalChain([lastResponse, 'optionalAccess', _285 => _285.candidates])) {
|
|
13426
14118
|
const candidates = [];
|
|
13427
14119
|
for (const candidate of lastResponse.candidates) {
|
|
13428
14120
|
const candidateDict = {
|
|
@@ -13473,20 +14165,8 @@ function tryToDict(obj) {
|
|
|
13473
14165
|
return null;
|
|
13474
14166
|
}
|
|
13475
14167
|
|
|
13476
|
-
// src/instrumentation/plugins/openrouter-channels.ts
|
|
13477
|
-
var
|
|
13478
|
-
chatSend: channel({
|
|
13479
|
-
channelName: "chat.send",
|
|
13480
|
-
kind: "async"
|
|
13481
|
-
}),
|
|
13482
|
-
embeddingsGenerate: channel({
|
|
13483
|
-
channelName: "embeddings.generate",
|
|
13484
|
-
kind: "async"
|
|
13485
|
-
}),
|
|
13486
|
-
betaResponsesSend: channel({
|
|
13487
|
-
channelName: "beta.responses.send",
|
|
13488
|
-
kind: "async"
|
|
13489
|
-
}),
|
|
14168
|
+
// src/instrumentation/plugins/openrouter-agent-channels.ts
|
|
14169
|
+
var openRouterAgentChannels = defineChannels("@openrouter/agent", {
|
|
13490
14170
|
callModel: channel({
|
|
13491
14171
|
channelName: "callModel",
|
|
13492
14172
|
kind: "sync-stream"
|
|
@@ -13501,35 +14181,832 @@ var openRouterChannels = defineChannels("@openrouter/sdk", {
|
|
|
13501
14181
|
})
|
|
13502
14182
|
});
|
|
13503
14183
|
|
|
13504
|
-
// src/instrumentation/plugins/openrouter-plugin.ts
|
|
13505
|
-
var
|
|
14184
|
+
// src/instrumentation/plugins/openrouter-agent-plugin.ts
|
|
14185
|
+
var OpenRouterAgentPlugin = class extends BasePlugin {
|
|
13506
14186
|
onEnable() {
|
|
13507
|
-
this.
|
|
14187
|
+
this.subscribeToOpenRouterAgentChannels();
|
|
13508
14188
|
}
|
|
13509
14189
|
onDisable() {
|
|
13510
14190
|
this.unsubscribers = unsubscribeAll(this.unsubscribers);
|
|
13511
14191
|
}
|
|
13512
|
-
|
|
14192
|
+
subscribeToOpenRouterAgentChannels() {
|
|
13513
14193
|
this.unsubscribers.push(
|
|
13514
|
-
|
|
13515
|
-
name: "openrouter.
|
|
14194
|
+
traceSyncStreamChannel(openRouterAgentChannels.callModel, {
|
|
14195
|
+
name: "openrouter.callModel",
|
|
13516
14196
|
type: "llm" /* LLM */,
|
|
13517
14197
|
extractInput: (args) => {
|
|
13518
|
-
const request =
|
|
13519
|
-
const chatGenerationParams = isObject(_optionalChain([request, 'optionalAccess', _281 => _281.chatGenerationParams])) ? request.chatGenerationParams : {};
|
|
13520
|
-
const httpReferer = _optionalChain([request, 'optionalAccess', _282 => _282.httpReferer]);
|
|
13521
|
-
const xTitle = _optionalChain([request, 'optionalAccess', _283 => _283.xTitle]);
|
|
13522
|
-
const { messages, ...metadata } = chatGenerationParams;
|
|
14198
|
+
const request = getOpenRouterCallModelRequestArg(args);
|
|
13523
14199
|
return {
|
|
13524
|
-
input:
|
|
13525
|
-
metadata:
|
|
14200
|
+
input: request ? extractOpenRouterCallModelInput(request) : void 0,
|
|
14201
|
+
metadata: request ? extractOpenRouterCallModelMetadata(request) : { provider: "openrouter" }
|
|
14202
|
+
};
|
|
14203
|
+
},
|
|
14204
|
+
patchResult: ({ endEvent, result, span }) => {
|
|
14205
|
+
return patchOpenRouterCallModelResult({
|
|
14206
|
+
request: getOpenRouterCallModelRequestArg(endEvent.arguments),
|
|
14207
|
+
result,
|
|
14208
|
+
span
|
|
14209
|
+
});
|
|
14210
|
+
}
|
|
14211
|
+
})
|
|
14212
|
+
);
|
|
14213
|
+
this.unsubscribers.push(
|
|
14214
|
+
traceAsyncChannel(openRouterAgentChannels.callModelTurn, {
|
|
14215
|
+
name: "openrouter.beta.responses.send",
|
|
14216
|
+
type: "llm" /* LLM */,
|
|
14217
|
+
extractInput: (args, event) => {
|
|
14218
|
+
const request = getOpenRouterCallModelRequestArg(args);
|
|
14219
|
+
const metadata = request ? extractOpenRouterCallModelMetadata(request) : { provider: "openrouter" };
|
|
14220
|
+
if (isObject(metadata) && "tools" in metadata) {
|
|
14221
|
+
delete metadata.tools;
|
|
14222
|
+
}
|
|
14223
|
+
return {
|
|
14224
|
+
input: request ? extractOpenRouterCallModelInput(request) : void 0,
|
|
14225
|
+
metadata: {
|
|
14226
|
+
...metadata,
|
|
14227
|
+
step: event.step,
|
|
14228
|
+
step_type: event.stepType
|
|
14229
|
+
}
|
|
14230
|
+
};
|
|
14231
|
+
},
|
|
14232
|
+
extractOutput: (result) => extractOpenRouterResponseOutput(result),
|
|
14233
|
+
extractMetadata: (result, event) => {
|
|
14234
|
+
if (!isObject(result)) {
|
|
14235
|
+
return {
|
|
14236
|
+
step: _optionalChain([event, 'optionalAccess', _286 => _286.step]),
|
|
14237
|
+
step_type: _optionalChain([event, 'optionalAccess', _287 => _287.stepType])
|
|
14238
|
+
};
|
|
14239
|
+
}
|
|
14240
|
+
return {
|
|
14241
|
+
...extractOpenRouterResponseMetadata(result) || {},
|
|
14242
|
+
..._optionalChain([event, 'optionalAccess', _288 => _288.step]) !== void 0 ? { step: event.step } : {},
|
|
14243
|
+
..._optionalChain([event, 'optionalAccess', _289 => _289.stepType]) ? { step_type: event.stepType } : {}
|
|
14244
|
+
};
|
|
14245
|
+
},
|
|
14246
|
+
extractMetrics: (result) => isObject(result) ? parseOpenRouterMetricsFromUsage(result.usage) : {}
|
|
14247
|
+
})
|
|
14248
|
+
);
|
|
14249
|
+
this.unsubscribers.push(
|
|
14250
|
+
traceStreamingChannel(openRouterAgentChannels.toolExecute, {
|
|
14251
|
+
name: "openrouter.tool",
|
|
14252
|
+
type: "tool" /* TOOL */,
|
|
14253
|
+
extractInput: (args, event) => ({
|
|
14254
|
+
input: args[0],
|
|
14255
|
+
metadata: {
|
|
14256
|
+
provider: "openrouter",
|
|
14257
|
+
tool_name: event.toolName,
|
|
14258
|
+
...event.toolCallId ? { tool_call_id: event.toolCallId } : {}
|
|
14259
|
+
}
|
|
14260
|
+
}),
|
|
14261
|
+
extractOutput: (result) => result,
|
|
14262
|
+
extractMetrics: () => ({}),
|
|
14263
|
+
aggregateChunks: (chunks) => ({
|
|
14264
|
+
output: chunks.length > 0 ? chunks[chunks.length - 1] : void 0,
|
|
14265
|
+
metrics: {}
|
|
14266
|
+
})
|
|
14267
|
+
})
|
|
14268
|
+
);
|
|
14269
|
+
const callModelChannel = openRouterAgentChannels.callModel.tracingChannel();
|
|
14270
|
+
const callModelHandlers = {
|
|
14271
|
+
start: (event) => {
|
|
14272
|
+
const request = getOpenRouterCallModelRequestArg(event.arguments);
|
|
14273
|
+
if (!request) {
|
|
14274
|
+
return;
|
|
14275
|
+
}
|
|
14276
|
+
patchOpenRouterCallModelRequestTools(request);
|
|
14277
|
+
}
|
|
14278
|
+
};
|
|
14279
|
+
callModelChannel.subscribe(callModelHandlers);
|
|
14280
|
+
this.unsubscribers.push(() => {
|
|
14281
|
+
callModelChannel.unsubscribe(callModelHandlers);
|
|
14282
|
+
});
|
|
14283
|
+
}
|
|
14284
|
+
};
|
|
14285
|
+
function normalizeArgs(args) {
|
|
14286
|
+
if (Array.isArray(args)) {
|
|
14287
|
+
return args;
|
|
14288
|
+
}
|
|
14289
|
+
if (isArrayLike(args)) {
|
|
14290
|
+
return Array.from(args);
|
|
14291
|
+
}
|
|
14292
|
+
return [args];
|
|
14293
|
+
}
|
|
14294
|
+
function isArrayLike(value) {
|
|
14295
|
+
return isObject(value) && "length" in value && typeof value.length === "number" && Number.isInteger(value.length) && value.length >= 0;
|
|
14296
|
+
}
|
|
14297
|
+
function getOpenRouterCallModelRequestArg(args) {
|
|
14298
|
+
const normalizedArgs = normalizeArgs(args);
|
|
14299
|
+
const keyedRequestArg = normalizedArgs.find(
|
|
14300
|
+
(arg) => isObject(arg) && ("input" in arg || "model" in arg || "tools" in arg)
|
|
14301
|
+
);
|
|
14302
|
+
if (isObject(keyedRequestArg)) {
|
|
14303
|
+
return keyedRequestArg;
|
|
14304
|
+
}
|
|
14305
|
+
const firstObjectArg = normalizedArgs.find((arg) => isObject(arg));
|
|
14306
|
+
return isObject(firstObjectArg) ? firstObjectArg : void 0;
|
|
14307
|
+
}
|
|
14308
|
+
var TOKEN_NAME_MAP2 = {
|
|
14309
|
+
promptTokens: "prompt_tokens",
|
|
14310
|
+
inputTokens: "prompt_tokens",
|
|
14311
|
+
completionTokens: "completion_tokens",
|
|
14312
|
+
outputTokens: "completion_tokens",
|
|
14313
|
+
totalTokens: "tokens",
|
|
14314
|
+
prompt_tokens: "prompt_tokens",
|
|
14315
|
+
input_tokens: "prompt_tokens",
|
|
14316
|
+
completion_tokens: "completion_tokens",
|
|
14317
|
+
output_tokens: "completion_tokens",
|
|
14318
|
+
total_tokens: "tokens"
|
|
14319
|
+
};
|
|
14320
|
+
var TOKEN_DETAIL_PREFIX_MAP = {
|
|
14321
|
+
promptTokensDetails: "prompt",
|
|
14322
|
+
inputTokensDetails: "prompt",
|
|
14323
|
+
completionTokensDetails: "completion",
|
|
14324
|
+
outputTokensDetails: "completion",
|
|
14325
|
+
costDetails: "cost",
|
|
14326
|
+
prompt_tokens_details: "prompt",
|
|
14327
|
+
input_tokens_details: "prompt",
|
|
14328
|
+
completion_tokens_details: "completion",
|
|
14329
|
+
output_tokens_details: "completion",
|
|
14330
|
+
cost_details: "cost"
|
|
14331
|
+
};
|
|
14332
|
+
function camelToSnake(value) {
|
|
14333
|
+
return value.replace(/[A-Z]/g, (match) => `_${match.toLowerCase()}`);
|
|
14334
|
+
}
|
|
14335
|
+
function parseOpenRouterMetricsFromUsage(usage) {
|
|
14336
|
+
if (!isObject(usage)) {
|
|
14337
|
+
return {};
|
|
14338
|
+
}
|
|
14339
|
+
const metrics = {};
|
|
14340
|
+
for (const [name, value] of Object.entries(usage)) {
|
|
14341
|
+
if (typeof value === "number") {
|
|
14342
|
+
metrics[TOKEN_NAME_MAP2[name] || camelToSnake(name)] = value;
|
|
14343
|
+
continue;
|
|
14344
|
+
}
|
|
14345
|
+
if (!isObject(value)) {
|
|
14346
|
+
continue;
|
|
14347
|
+
}
|
|
14348
|
+
const prefix = TOKEN_DETAIL_PREFIX_MAP[name];
|
|
14349
|
+
if (!prefix) {
|
|
14350
|
+
continue;
|
|
14351
|
+
}
|
|
14352
|
+
for (const [nestedName, nestedValue] of Object.entries(value)) {
|
|
14353
|
+
if (typeof nestedValue !== "number") {
|
|
14354
|
+
continue;
|
|
14355
|
+
}
|
|
14356
|
+
metrics[`${prefix}_${camelToSnake(nestedName)}`] = nestedValue;
|
|
14357
|
+
}
|
|
14358
|
+
}
|
|
14359
|
+
return metrics;
|
|
14360
|
+
}
|
|
14361
|
+
function extractOpenRouterUsageMetadata(usage) {
|
|
14362
|
+
if (!isObject(usage)) {
|
|
14363
|
+
return void 0;
|
|
14364
|
+
}
|
|
14365
|
+
const metadata = {};
|
|
14366
|
+
if (typeof usage.isByok === "boolean") {
|
|
14367
|
+
metadata.is_byok = usage.isByok;
|
|
14368
|
+
} else if (typeof usage.is_byok === "boolean") {
|
|
14369
|
+
metadata.is_byok = usage.is_byok;
|
|
14370
|
+
}
|
|
14371
|
+
return Object.keys(metadata).length > 0 ? metadata : void 0;
|
|
14372
|
+
}
|
|
14373
|
+
var OMITTED_OPENROUTER_KEYS = /* @__PURE__ */ new Set([
|
|
14374
|
+
"execute",
|
|
14375
|
+
"render",
|
|
14376
|
+
"nextTurnParams",
|
|
14377
|
+
"requireApproval"
|
|
14378
|
+
]);
|
|
14379
|
+
function parseOpenRouterModelString(model) {
|
|
14380
|
+
if (typeof model !== "string") {
|
|
14381
|
+
return { model };
|
|
14382
|
+
}
|
|
14383
|
+
const slashIndex = model.indexOf("/");
|
|
14384
|
+
if (slashIndex > 0 && slashIndex < model.length - 1) {
|
|
14385
|
+
return {
|
|
14386
|
+
provider: model.substring(0, slashIndex),
|
|
14387
|
+
model: model.substring(slashIndex + 1)
|
|
14388
|
+
};
|
|
14389
|
+
}
|
|
14390
|
+
return { model };
|
|
14391
|
+
}
|
|
14392
|
+
function isZodSchema3(value) {
|
|
14393
|
+
return value != null && typeof value === "object" && "_def" in value && typeof value._def === "object";
|
|
14394
|
+
}
|
|
14395
|
+
function serializeZodSchema3(schema) {
|
|
14396
|
+
try {
|
|
14397
|
+
return zodToJsonSchema(schema);
|
|
14398
|
+
} catch (e36) {
|
|
14399
|
+
return {
|
|
14400
|
+
type: "object",
|
|
14401
|
+
description: "Zod schema (conversion failed)"
|
|
14402
|
+
};
|
|
14403
|
+
}
|
|
14404
|
+
}
|
|
14405
|
+
function serializeOpenRouterTool(tool) {
|
|
14406
|
+
if (!isObject(tool)) {
|
|
14407
|
+
return tool;
|
|
14408
|
+
}
|
|
14409
|
+
const serialized = {};
|
|
14410
|
+
for (const [key, value] of Object.entries(tool)) {
|
|
14411
|
+
if (OMITTED_OPENROUTER_KEYS.has(key)) {
|
|
14412
|
+
continue;
|
|
14413
|
+
}
|
|
14414
|
+
if (key === "function" && isObject(value)) {
|
|
14415
|
+
serialized.function = sanitizeOpenRouterLoggedValue(value);
|
|
14416
|
+
continue;
|
|
14417
|
+
}
|
|
14418
|
+
serialized[key] = sanitizeOpenRouterLoggedValue(value);
|
|
14419
|
+
}
|
|
14420
|
+
return serialized;
|
|
14421
|
+
}
|
|
14422
|
+
function serializeOpenRouterToolsForLogging(tools) {
|
|
14423
|
+
if (!Array.isArray(tools)) {
|
|
14424
|
+
return void 0;
|
|
14425
|
+
}
|
|
14426
|
+
return tools.map((tool) => serializeOpenRouterTool(tool));
|
|
14427
|
+
}
|
|
14428
|
+
function sanitizeOpenRouterLoggedValue(value) {
|
|
14429
|
+
if (isZodSchema3(value)) {
|
|
14430
|
+
return serializeZodSchema3(value);
|
|
14431
|
+
}
|
|
14432
|
+
if (typeof value === "function") {
|
|
14433
|
+
return "[Function]";
|
|
14434
|
+
}
|
|
14435
|
+
if (Array.isArray(value)) {
|
|
14436
|
+
return value.map((entry) => sanitizeOpenRouterLoggedValue(entry));
|
|
14437
|
+
}
|
|
14438
|
+
if (!isObject(value)) {
|
|
14439
|
+
return value;
|
|
14440
|
+
}
|
|
14441
|
+
const sanitized = {};
|
|
14442
|
+
for (const [key, entry] of Object.entries(value)) {
|
|
14443
|
+
if (OMITTED_OPENROUTER_KEYS.has(key)) {
|
|
14444
|
+
continue;
|
|
14445
|
+
}
|
|
14446
|
+
if (key === "tools" && Array.isArray(entry)) {
|
|
14447
|
+
sanitized.tools = serializeOpenRouterToolsForLogging(entry);
|
|
14448
|
+
continue;
|
|
14449
|
+
}
|
|
14450
|
+
sanitized[key] = sanitizeOpenRouterLoggedValue(entry);
|
|
14451
|
+
}
|
|
14452
|
+
return sanitized;
|
|
14453
|
+
}
|
|
14454
|
+
function buildOpenRouterMetadata(metadata, httpReferer, appTitle, appCategories, xTitle) {
|
|
14455
|
+
const sanitized = sanitizeOpenRouterLoggedValue(metadata);
|
|
14456
|
+
const metadataRecord = isObject(sanitized) ? sanitized : {};
|
|
14457
|
+
const { model, provider: providerRouting, ...rest } = metadataRecord;
|
|
14458
|
+
const normalizedModel = parseOpenRouterModelString(model);
|
|
14459
|
+
return {
|
|
14460
|
+
...rest,
|
|
14461
|
+
...normalizedModel.model !== void 0 ? { model: normalizedModel.model } : {},
|
|
14462
|
+
...providerRouting !== void 0 ? { providerRouting } : {},
|
|
14463
|
+
...httpReferer !== void 0 ? { httpReferer } : {},
|
|
14464
|
+
...appTitle !== void 0 ? { appTitle } : {},
|
|
14465
|
+
...appCategories !== void 0 ? { appCategories } : {},
|
|
14466
|
+
...xTitle !== void 0 ? { xTitle } : {},
|
|
14467
|
+
provider: normalizedModel.provider || "openrouter"
|
|
14468
|
+
};
|
|
14469
|
+
}
|
|
14470
|
+
function extractOpenRouterCallModelInput(request) {
|
|
14471
|
+
return isObject(request) && "input" in request ? sanitizeOpenRouterLoggedValue(request.input) : void 0;
|
|
14472
|
+
}
|
|
14473
|
+
function extractOpenRouterCallModelMetadata(request) {
|
|
14474
|
+
if (!isObject(request)) {
|
|
14475
|
+
return { provider: "openrouter" };
|
|
14476
|
+
}
|
|
14477
|
+
const { input: _input, ...metadata } = request;
|
|
14478
|
+
return buildOpenRouterMetadata(
|
|
14479
|
+
metadata,
|
|
14480
|
+
void 0,
|
|
14481
|
+
void 0,
|
|
14482
|
+
void 0,
|
|
14483
|
+
void 0
|
|
14484
|
+
);
|
|
14485
|
+
}
|
|
14486
|
+
function extractOpenRouterResponseMetadata(result) {
|
|
14487
|
+
if (!isObject(result)) {
|
|
14488
|
+
return void 0;
|
|
14489
|
+
}
|
|
14490
|
+
const { output: _output, data: _data, usage, ...metadata } = result;
|
|
14491
|
+
const sanitized = sanitizeOpenRouterLoggedValue(metadata);
|
|
14492
|
+
const metadataRecord = isObject(sanitized) ? sanitized : {};
|
|
14493
|
+
const { model, provider, ...rest } = metadataRecord;
|
|
14494
|
+
const normalizedModel = parseOpenRouterModelString(model);
|
|
14495
|
+
const normalizedProvider = (typeof provider === "string" ? provider : void 0) || normalizedModel.provider;
|
|
14496
|
+
const usageMetadata = extractOpenRouterUsageMetadata(usage);
|
|
14497
|
+
const combined = {
|
|
14498
|
+
...rest,
|
|
14499
|
+
...normalizedModel.model !== void 0 ? { model: normalizedModel.model } : {},
|
|
14500
|
+
...usageMetadata || {},
|
|
14501
|
+
...normalizedProvider !== void 0 ? { provider: normalizedProvider } : {}
|
|
14502
|
+
};
|
|
14503
|
+
return Object.keys(combined).length > 0 ? combined : void 0;
|
|
14504
|
+
}
|
|
14505
|
+
function extractOpenRouterResponseOutput(response, fallbackOutput) {
|
|
14506
|
+
if (isObject(response) && "output" in response && response.output !== void 0) {
|
|
14507
|
+
return sanitizeOpenRouterLoggedValue(response.output);
|
|
14508
|
+
}
|
|
14509
|
+
if (fallbackOutput !== void 0) {
|
|
14510
|
+
return sanitizeOpenRouterLoggedValue(fallbackOutput);
|
|
14511
|
+
}
|
|
14512
|
+
return void 0;
|
|
14513
|
+
}
|
|
14514
|
+
var OPENROUTER_WRAPPED_TOOL = Symbol("braintrust.openrouter.wrappedTool");
|
|
14515
|
+
function patchOpenRouterCallModelRequestTools(request) {
|
|
14516
|
+
if (!Array.isArray(request.tools) || request.tools.length === 0) {
|
|
14517
|
+
return void 0;
|
|
14518
|
+
}
|
|
14519
|
+
const originalTools = request.tools;
|
|
14520
|
+
const wrappedTools = originalTools.map((tool) => wrapOpenRouterTool(tool));
|
|
14521
|
+
const didPatch = wrappedTools.some(
|
|
14522
|
+
(tool, index) => tool !== originalTools[index]
|
|
14523
|
+
);
|
|
14524
|
+
if (!didPatch) {
|
|
14525
|
+
return void 0;
|
|
14526
|
+
}
|
|
14527
|
+
request.tools = wrappedTools;
|
|
14528
|
+
return () => {
|
|
14529
|
+
request.tools = originalTools;
|
|
14530
|
+
};
|
|
14531
|
+
}
|
|
14532
|
+
function wrapOpenRouterTool(tool) {
|
|
14533
|
+
if (isWrappedTool(tool) || !tool.function || typeof tool.function !== "object" || typeof tool.function.execute !== "function") {
|
|
14534
|
+
return tool;
|
|
14535
|
+
}
|
|
14536
|
+
const toolName = tool.function.name || "tool";
|
|
14537
|
+
const originalExecute = tool.function.execute;
|
|
14538
|
+
const wrappedTool = {
|
|
14539
|
+
...tool,
|
|
14540
|
+
function: {
|
|
14541
|
+
...tool.function,
|
|
14542
|
+
execute(...args) {
|
|
14543
|
+
return traceToolExecution({
|
|
14544
|
+
args,
|
|
14545
|
+
execute: () => Reflect.apply(originalExecute, this, args),
|
|
14546
|
+
toolCallId: getToolCallId(args[1]),
|
|
14547
|
+
toolName
|
|
14548
|
+
});
|
|
14549
|
+
}
|
|
14550
|
+
}
|
|
14551
|
+
};
|
|
14552
|
+
Object.defineProperty(wrappedTool, OPENROUTER_WRAPPED_TOOL, {
|
|
14553
|
+
value: true,
|
|
14554
|
+
enumerable: false,
|
|
14555
|
+
configurable: false
|
|
14556
|
+
});
|
|
14557
|
+
return wrappedTool;
|
|
14558
|
+
}
|
|
14559
|
+
function isWrappedTool(tool) {
|
|
14560
|
+
return Boolean(tool[OPENROUTER_WRAPPED_TOOL]);
|
|
14561
|
+
}
|
|
14562
|
+
function traceToolExecution(args) {
|
|
14563
|
+
const tracingChannel2 = openRouterAgentChannels.toolExecute.tracingChannel();
|
|
14564
|
+
const input = args.args.length > 0 ? args.args[0] : void 0;
|
|
14565
|
+
const event = {
|
|
14566
|
+
arguments: [input],
|
|
14567
|
+
span_info: {
|
|
14568
|
+
name: args.toolName
|
|
14569
|
+
},
|
|
14570
|
+
toolCallId: args.toolCallId,
|
|
14571
|
+
toolName: args.toolName
|
|
14572
|
+
};
|
|
14573
|
+
tracingChannel2.start.publish(event);
|
|
14574
|
+
try {
|
|
14575
|
+
const result = args.execute();
|
|
14576
|
+
return publishToolResult(tracingChannel2, event, result);
|
|
14577
|
+
} catch (error) {
|
|
14578
|
+
event.error = normalizeError(error);
|
|
14579
|
+
tracingChannel2.error.publish(event);
|
|
14580
|
+
throw error;
|
|
14581
|
+
}
|
|
14582
|
+
}
|
|
14583
|
+
function publishToolResult(tracingChannel2, event, result) {
|
|
14584
|
+
if (isPromiseLike3(result)) {
|
|
14585
|
+
return result.then(
|
|
14586
|
+
(resolved) => {
|
|
14587
|
+
event.result = resolved;
|
|
14588
|
+
tracingChannel2.asyncEnd.publish(event);
|
|
14589
|
+
return resolved;
|
|
14590
|
+
},
|
|
14591
|
+
(error) => {
|
|
14592
|
+
event.error = normalizeError(error);
|
|
14593
|
+
tracingChannel2.error.publish(event);
|
|
14594
|
+
throw error;
|
|
14595
|
+
}
|
|
14596
|
+
);
|
|
14597
|
+
}
|
|
14598
|
+
event.result = result;
|
|
14599
|
+
tracingChannel2.asyncEnd.publish(event);
|
|
14600
|
+
return result;
|
|
14601
|
+
}
|
|
14602
|
+
function getToolCallId(context) {
|
|
14603
|
+
const toolContext = context;
|
|
14604
|
+
return typeof _optionalChain([toolContext, 'optionalAccess', _290 => _290.toolCall, 'optionalAccess', _291 => _291.id]) === "string" ? toolContext.toolCall.id : void 0;
|
|
14605
|
+
}
|
|
14606
|
+
function isPromiseLike3(value) {
|
|
14607
|
+
return !!value && (typeof value === "object" || typeof value === "function") && "then" in value && typeof value.then === "function";
|
|
14608
|
+
}
|
|
14609
|
+
var OPENROUTER_WRAPPED_CALL_MODEL_RESULT = Symbol(
|
|
14610
|
+
"braintrust.openrouter.wrappedCallModelResult"
|
|
14611
|
+
);
|
|
14612
|
+
var OPENROUTER_CALL_MODEL_STREAM_METHODS = [
|
|
14613
|
+
"getFullResponsesStream",
|
|
14614
|
+
"getItemsStream",
|
|
14615
|
+
"getNewMessagesStream",
|
|
14616
|
+
"getReasoningStream",
|
|
14617
|
+
"getTextStream",
|
|
14618
|
+
"getToolCallsStream",
|
|
14619
|
+
"getToolStream"
|
|
14620
|
+
];
|
|
14621
|
+
var OPENROUTER_CALL_MODEL_CONTEXT_METHODS = [
|
|
14622
|
+
"cancel",
|
|
14623
|
+
"getPendingToolCalls",
|
|
14624
|
+
"getState",
|
|
14625
|
+
"getToolCalls",
|
|
14626
|
+
"requiresApproval"
|
|
14627
|
+
];
|
|
14628
|
+
function patchOpenRouterCallModelResult(args) {
|
|
14629
|
+
const { request, result, span } = args;
|
|
14630
|
+
if (!isObject(result) || isWrappedCallModelResult(result)) {
|
|
14631
|
+
return false;
|
|
14632
|
+
}
|
|
14633
|
+
const resultLike = result;
|
|
14634
|
+
const hasInstrumentableMethod = typeof resultLike.getResponse === "function" || typeof resultLike.getText === "function" || OPENROUTER_CALL_MODEL_STREAM_METHODS.some(
|
|
14635
|
+
(methodName) => typeof resultLike[methodName] === "function"
|
|
14636
|
+
);
|
|
14637
|
+
if (!hasInstrumentableMethod) {
|
|
14638
|
+
return false;
|
|
14639
|
+
}
|
|
14640
|
+
Object.defineProperty(resultLike, OPENROUTER_WRAPPED_CALL_MODEL_RESULT, {
|
|
14641
|
+
value: true,
|
|
14642
|
+
enumerable: false,
|
|
14643
|
+
configurable: false
|
|
14644
|
+
});
|
|
14645
|
+
const originalGetResponse = typeof resultLike.getResponse === "function" ? resultLike.getResponse.bind(resultLike) : void 0;
|
|
14646
|
+
const originalGetInitialResponse = typeof resultLike.getInitialResponse === "function" ? resultLike.getInitialResponse.bind(resultLike) : void 0;
|
|
14647
|
+
const originalMakeFollowupRequest = typeof resultLike.makeFollowupRequest === "function" ? resultLike.makeFollowupRequest.bind(resultLike) : void 0;
|
|
14648
|
+
let ended = false;
|
|
14649
|
+
let tracedTurnCount = 0;
|
|
14650
|
+
const endSpanWithResult = async (response, fallbackOutput) => {
|
|
14651
|
+
if (ended) {
|
|
14652
|
+
return;
|
|
14653
|
+
}
|
|
14654
|
+
ended = true;
|
|
14655
|
+
const finalResponse = getFinalOpenRouterCallModelResponse(
|
|
14656
|
+
resultLike,
|
|
14657
|
+
response
|
|
14658
|
+
);
|
|
14659
|
+
if (finalResponse) {
|
|
14660
|
+
const rounds = getOpenRouterCallModelRounds(resultLike);
|
|
14661
|
+
const metadata = extractOpenRouterCallModelResultMetadata(
|
|
14662
|
+
finalResponse,
|
|
14663
|
+
rounds.length + 1
|
|
14664
|
+
);
|
|
14665
|
+
span.log({
|
|
14666
|
+
output: extractOpenRouterResponseOutput(finalResponse, fallbackOutput),
|
|
14667
|
+
...metadata ? { metadata } : {},
|
|
14668
|
+
metrics: aggregateOpenRouterCallModelMetrics(rounds, finalResponse)
|
|
14669
|
+
});
|
|
14670
|
+
span.end();
|
|
14671
|
+
return;
|
|
14672
|
+
}
|
|
14673
|
+
if (fallbackOutput !== void 0) {
|
|
14674
|
+
span.log({
|
|
14675
|
+
output: fallbackOutput
|
|
14676
|
+
});
|
|
14677
|
+
}
|
|
14678
|
+
span.end();
|
|
14679
|
+
};
|
|
14680
|
+
const endSpanWithError = (error) => {
|
|
14681
|
+
if (ended) {
|
|
14682
|
+
return;
|
|
14683
|
+
}
|
|
14684
|
+
ended = true;
|
|
14685
|
+
span.log({
|
|
14686
|
+
error: normalizeError(error).message
|
|
14687
|
+
});
|
|
14688
|
+
span.end();
|
|
14689
|
+
};
|
|
14690
|
+
const finalizeFromResponse = async (fallbackOutput) => {
|
|
14691
|
+
if (!originalGetResponse) {
|
|
14692
|
+
await endSpanWithResult(void 0, fallbackOutput);
|
|
14693
|
+
return;
|
|
14694
|
+
}
|
|
14695
|
+
try {
|
|
14696
|
+
await endSpanWithResult(await originalGetResponse(), fallbackOutput);
|
|
14697
|
+
} catch (e37) {
|
|
14698
|
+
await endSpanWithResult(void 0, fallbackOutput);
|
|
14699
|
+
}
|
|
14700
|
+
};
|
|
14701
|
+
if (originalGetResponse) {
|
|
14702
|
+
resultLike.getResponse = async (...args2) => {
|
|
14703
|
+
return await withCurrent(span, async () => {
|
|
14704
|
+
try {
|
|
14705
|
+
const response = await originalGetResponse(...args2);
|
|
14706
|
+
await endSpanWithResult(response);
|
|
14707
|
+
return response;
|
|
14708
|
+
} catch (error) {
|
|
14709
|
+
endSpanWithError(error);
|
|
14710
|
+
throw error;
|
|
14711
|
+
}
|
|
14712
|
+
});
|
|
14713
|
+
};
|
|
14714
|
+
}
|
|
14715
|
+
if (typeof resultLike.getText === "function") {
|
|
14716
|
+
const originalGetText = resultLike.getText.bind(resultLike);
|
|
14717
|
+
resultLike.getText = async (...args2) => {
|
|
14718
|
+
return await withCurrent(span, async () => {
|
|
14719
|
+
try {
|
|
14720
|
+
const text = await originalGetText(...args2);
|
|
14721
|
+
await finalizeFromResponse(text);
|
|
14722
|
+
return text;
|
|
14723
|
+
} catch (error) {
|
|
14724
|
+
endSpanWithError(error);
|
|
14725
|
+
throw error;
|
|
14726
|
+
}
|
|
14727
|
+
});
|
|
14728
|
+
};
|
|
14729
|
+
}
|
|
14730
|
+
for (const methodName of OPENROUTER_CALL_MODEL_CONTEXT_METHODS) {
|
|
14731
|
+
if (typeof resultLike[methodName] !== "function") {
|
|
14732
|
+
continue;
|
|
14733
|
+
}
|
|
14734
|
+
const originalMethod = resultLike[methodName];
|
|
14735
|
+
resultLike[methodName] = async (...args2) => {
|
|
14736
|
+
return await withCurrent(span, async () => {
|
|
14737
|
+
return await originalMethod.apply(resultLike, args2);
|
|
14738
|
+
});
|
|
14739
|
+
};
|
|
14740
|
+
}
|
|
14741
|
+
for (const methodName of OPENROUTER_CALL_MODEL_STREAM_METHODS) {
|
|
14742
|
+
if (typeof resultLike[methodName] !== "function") {
|
|
14743
|
+
continue;
|
|
14744
|
+
}
|
|
14745
|
+
const originalMethod = resultLike[methodName];
|
|
14746
|
+
resultLike[methodName] = (...args2) => {
|
|
14747
|
+
const stream = withCurrent(
|
|
14748
|
+
span,
|
|
14749
|
+
() => originalMethod.apply(resultLike, args2)
|
|
14750
|
+
);
|
|
14751
|
+
if (!isAsyncIterable3(stream)) {
|
|
14752
|
+
return stream;
|
|
14753
|
+
}
|
|
14754
|
+
return wrapAsyncIterableWithSpan({
|
|
14755
|
+
finalize: finalizeFromResponse,
|
|
14756
|
+
iteratorFactory: () => stream[Symbol.asyncIterator](),
|
|
14757
|
+
onError: endSpanWithError,
|
|
14758
|
+
span
|
|
14759
|
+
});
|
|
14760
|
+
};
|
|
14761
|
+
}
|
|
14762
|
+
if (originalGetInitialResponse) {
|
|
14763
|
+
let initialTurnTraced = false;
|
|
14764
|
+
resultLike.getInitialResponse = async (...args2) => {
|
|
14765
|
+
if (initialTurnTraced) {
|
|
14766
|
+
return await withCurrent(span, async () => {
|
|
14767
|
+
return await originalGetInitialResponse(...args2);
|
|
14768
|
+
});
|
|
14769
|
+
}
|
|
14770
|
+
initialTurnTraced = true;
|
|
14771
|
+
const step = tracedTurnCount + 1;
|
|
14772
|
+
const stepType = tracedTurnCount === 0 ? "initial" : "continue";
|
|
14773
|
+
const response = await traceOpenRouterCallModelTurn({
|
|
14774
|
+
fn: async () => {
|
|
14775
|
+
const nextResponse = await originalGetInitialResponse(...args2);
|
|
14776
|
+
tracedTurnCount++;
|
|
14777
|
+
return nextResponse;
|
|
14778
|
+
},
|
|
14779
|
+
parentSpan: span,
|
|
14780
|
+
request: getOpenRouterResolvedRequest(resultLike, request),
|
|
14781
|
+
step,
|
|
14782
|
+
stepType
|
|
14783
|
+
});
|
|
14784
|
+
return response;
|
|
14785
|
+
};
|
|
14786
|
+
}
|
|
14787
|
+
if (originalMakeFollowupRequest) {
|
|
14788
|
+
resultLike.makeFollowupRequest = async (...args2) => {
|
|
14789
|
+
const currentResponse = args2[0];
|
|
14790
|
+
const toolResults = Array.isArray(args2[1]) ? args2[1] : [];
|
|
14791
|
+
const step = tracedTurnCount + 1;
|
|
14792
|
+
const response = await traceOpenRouterCallModelTurn({
|
|
14793
|
+
fn: async () => {
|
|
14794
|
+
const nextResponse = await originalMakeFollowupRequest(...args2);
|
|
14795
|
+
tracedTurnCount++;
|
|
14796
|
+
return nextResponse;
|
|
14797
|
+
},
|
|
14798
|
+
parentSpan: span,
|
|
14799
|
+
request: buildOpenRouterFollowupRequest(
|
|
14800
|
+
getOpenRouterResolvedRequest(resultLike, request),
|
|
14801
|
+
currentResponse,
|
|
14802
|
+
toolResults
|
|
14803
|
+
),
|
|
14804
|
+
step,
|
|
14805
|
+
stepType: "continue"
|
|
14806
|
+
});
|
|
14807
|
+
return response;
|
|
14808
|
+
};
|
|
14809
|
+
}
|
|
14810
|
+
return true;
|
|
14811
|
+
}
|
|
14812
|
+
async function traceOpenRouterCallModelTurn(args) {
|
|
14813
|
+
const context = {
|
|
14814
|
+
arguments: [args.request],
|
|
14815
|
+
step: args.step,
|
|
14816
|
+
stepType: args.stepType
|
|
14817
|
+
};
|
|
14818
|
+
return await withCurrent(
|
|
14819
|
+
args.parentSpan,
|
|
14820
|
+
() => openRouterAgentChannels.callModelTurn.tracePromise(args.fn, context)
|
|
14821
|
+
);
|
|
14822
|
+
}
|
|
14823
|
+
function isWrappedCallModelResult(value) {
|
|
14824
|
+
return Boolean(
|
|
14825
|
+
isObject(value) && value[OPENROUTER_WRAPPED_CALL_MODEL_RESULT]
|
|
14826
|
+
);
|
|
14827
|
+
}
|
|
14828
|
+
function extractOpenRouterCallModelResultMetadata(response, turnCount) {
|
|
14829
|
+
const combined = {
|
|
14830
|
+
...extractOpenRouterResponseMetadata(response) || {},
|
|
14831
|
+
...turnCount !== void 0 ? { turn_count: turnCount } : {}
|
|
14832
|
+
};
|
|
14833
|
+
return Object.keys(combined).length > 0 ? combined : void 0;
|
|
14834
|
+
}
|
|
14835
|
+
function getFinalOpenRouterCallModelResponse(result, response) {
|
|
14836
|
+
if (isObject(response)) {
|
|
14837
|
+
return response;
|
|
14838
|
+
}
|
|
14839
|
+
return isObject(result.finalResponse) ? result.finalResponse : void 0;
|
|
14840
|
+
}
|
|
14841
|
+
function getOpenRouterCallModelRounds(result) {
|
|
14842
|
+
if (!Array.isArray(result.allToolExecutionRounds)) {
|
|
14843
|
+
return [];
|
|
14844
|
+
}
|
|
14845
|
+
return result.allToolExecutionRounds.filter((round) => isObject(round)).map((round) => ({
|
|
14846
|
+
response: isObject(round.response) ? round.response : void 0,
|
|
14847
|
+
round: typeof round.round === "number" ? round.round : void 0,
|
|
14848
|
+
toolResults: Array.isArray(round.toolResults) ? round.toolResults : []
|
|
14849
|
+
})).filter((round) => round.response !== void 0);
|
|
14850
|
+
}
|
|
14851
|
+
function aggregateOpenRouterCallModelMetrics(rounds, finalResponse) {
|
|
14852
|
+
const metrics = {};
|
|
14853
|
+
const responses = [
|
|
14854
|
+
...rounds.map((round) => round.response).filter(isObject),
|
|
14855
|
+
finalResponse
|
|
14856
|
+
];
|
|
14857
|
+
for (const response of responses) {
|
|
14858
|
+
const responseMetrics = parseOpenRouterMetricsFromUsage(response.usage);
|
|
14859
|
+
for (const [name, value] of Object.entries(responseMetrics)) {
|
|
14860
|
+
metrics[name] = (metrics[name] || 0) + value;
|
|
14861
|
+
}
|
|
14862
|
+
}
|
|
14863
|
+
return metrics;
|
|
14864
|
+
}
|
|
14865
|
+
function buildNextOpenRouterCallModelInput(currentInput, response, toolResults) {
|
|
14866
|
+
const normalizedInput = Array.isArray(currentInput) ? [...currentInput] : currentInput === void 0 ? [] : [currentInput];
|
|
14867
|
+
const responseOutput = Array.isArray(response.output) ? response.output : response.output === void 0 ? [] : [response.output];
|
|
14868
|
+
return [...normalizedInput, ...responseOutput, ...toolResults].map(
|
|
14869
|
+
(entry) => sanitizeOpenRouterLoggedValue(entry)
|
|
14870
|
+
);
|
|
14871
|
+
}
|
|
14872
|
+
function getOpenRouterResolvedRequest(result, request) {
|
|
14873
|
+
if (isObject(result.resolvedRequest)) {
|
|
14874
|
+
return result.resolvedRequest;
|
|
14875
|
+
}
|
|
14876
|
+
return request;
|
|
14877
|
+
}
|
|
14878
|
+
function buildOpenRouterFollowupRequest(request, currentResponse, toolResults) {
|
|
14879
|
+
if (!request) {
|
|
14880
|
+
return void 0;
|
|
14881
|
+
}
|
|
14882
|
+
return {
|
|
14883
|
+
...request,
|
|
14884
|
+
input: buildNextOpenRouterCallModelInput(
|
|
14885
|
+
extractOpenRouterCallModelInput(request),
|
|
14886
|
+
isObject(currentResponse) ? currentResponse : {},
|
|
14887
|
+
toolResults
|
|
14888
|
+
),
|
|
14889
|
+
stream: false
|
|
14890
|
+
};
|
|
14891
|
+
}
|
|
14892
|
+
function wrapAsyncIterableWithSpan(args) {
|
|
14893
|
+
return {
|
|
14894
|
+
[Symbol.asyncIterator]() {
|
|
14895
|
+
const iterator = args.iteratorFactory();
|
|
14896
|
+
return {
|
|
14897
|
+
next(value) {
|
|
14898
|
+
return withCurrent(
|
|
14899
|
+
args.span,
|
|
14900
|
+
() => value === void 0 ? iterator.next() : iterator.next(value)
|
|
14901
|
+
).then(
|
|
14902
|
+
async (result) => {
|
|
14903
|
+
if (result.done) {
|
|
14904
|
+
await args.finalize();
|
|
14905
|
+
}
|
|
14906
|
+
return result;
|
|
14907
|
+
},
|
|
14908
|
+
(error) => {
|
|
14909
|
+
args.onError(error);
|
|
14910
|
+
throw error;
|
|
14911
|
+
}
|
|
14912
|
+
);
|
|
14913
|
+
},
|
|
14914
|
+
return(value) {
|
|
14915
|
+
if (typeof iterator.return !== "function") {
|
|
14916
|
+
return args.finalize().then(() => ({
|
|
14917
|
+
done: true,
|
|
14918
|
+
value
|
|
14919
|
+
}));
|
|
14920
|
+
}
|
|
14921
|
+
return withCurrent(args.span, () => iterator.return(value)).then(
|
|
14922
|
+
async (result) => {
|
|
14923
|
+
await args.finalize();
|
|
14924
|
+
return result;
|
|
14925
|
+
},
|
|
14926
|
+
(error) => {
|
|
14927
|
+
args.onError(error);
|
|
14928
|
+
throw error;
|
|
14929
|
+
}
|
|
14930
|
+
);
|
|
14931
|
+
},
|
|
14932
|
+
throw(error) {
|
|
14933
|
+
args.onError(error);
|
|
14934
|
+
if (typeof iterator.throw !== "function") {
|
|
14935
|
+
return Promise.reject(error);
|
|
14936
|
+
}
|
|
14937
|
+
return withCurrent(args.span, () => iterator.throw(error));
|
|
14938
|
+
},
|
|
14939
|
+
[Symbol.asyncIterator]() {
|
|
14940
|
+
return this;
|
|
14941
|
+
}
|
|
14942
|
+
};
|
|
14943
|
+
}
|
|
14944
|
+
};
|
|
14945
|
+
}
|
|
14946
|
+
function isAsyncIterable3(value) {
|
|
14947
|
+
return !!value && (typeof value === "object" || typeof value === "function") && Symbol.asyncIterator in value && typeof value[Symbol.asyncIterator] === "function";
|
|
14948
|
+
}
|
|
14949
|
+
function normalizeError(error) {
|
|
14950
|
+
return error instanceof Error ? error : new Error(String(error));
|
|
14951
|
+
}
|
|
14952
|
+
|
|
14953
|
+
// src/instrumentation/plugins/openrouter-channels.ts
|
|
14954
|
+
var openRouterChannels = defineChannels("@openrouter/sdk", {
|
|
14955
|
+
chatSend: channel({
|
|
14956
|
+
channelName: "chat.send",
|
|
14957
|
+
kind: "async"
|
|
14958
|
+
}),
|
|
14959
|
+
embeddingsGenerate: channel({
|
|
14960
|
+
channelName: "embeddings.generate",
|
|
14961
|
+
kind: "async"
|
|
14962
|
+
}),
|
|
14963
|
+
betaResponsesSend: channel({
|
|
14964
|
+
channelName: "beta.responses.send",
|
|
14965
|
+
kind: "async"
|
|
14966
|
+
}),
|
|
14967
|
+
callModel: channel({
|
|
14968
|
+
channelName: "callModel",
|
|
14969
|
+
kind: "sync-stream"
|
|
14970
|
+
}),
|
|
14971
|
+
callModelTurn: channel({
|
|
14972
|
+
channelName: "callModel.turn",
|
|
14973
|
+
kind: "async"
|
|
14974
|
+
}),
|
|
14975
|
+
toolExecute: channel({
|
|
14976
|
+
channelName: "tool.execute",
|
|
14977
|
+
kind: "async"
|
|
14978
|
+
})
|
|
14979
|
+
});
|
|
14980
|
+
|
|
14981
|
+
// src/instrumentation/plugins/openrouter-plugin.ts
|
|
14982
|
+
var OpenRouterPlugin = class extends BasePlugin {
|
|
14983
|
+
onEnable() {
|
|
14984
|
+
this.subscribeToOpenRouterChannels();
|
|
14985
|
+
}
|
|
14986
|
+
onDisable() {
|
|
14987
|
+
this.unsubscribers = unsubscribeAll(this.unsubscribers);
|
|
14988
|
+
}
|
|
14989
|
+
subscribeToOpenRouterChannels() {
|
|
14990
|
+
this.unsubscribers.push(
|
|
14991
|
+
traceStreamingChannel(openRouterChannels.chatSend, {
|
|
14992
|
+
name: "openrouter.chat.send",
|
|
14993
|
+
type: "llm" /* LLM */,
|
|
14994
|
+
extractInput: (args) => {
|
|
14995
|
+
const request = getOpenRouterRequestArg(args);
|
|
14996
|
+
const chatGenerationParams = isObject(_optionalChain([request, 'optionalAccess', _292 => _292.chatGenerationParams])) ? request.chatGenerationParams : {};
|
|
14997
|
+
const httpReferer = _optionalChain([request, 'optionalAccess', _293 => _293.httpReferer]);
|
|
14998
|
+
const xTitle = _optionalChain([request, 'optionalAccess', _294 => _294.xTitle]);
|
|
14999
|
+
const { messages, ...metadata } = chatGenerationParams;
|
|
15000
|
+
return {
|
|
15001
|
+
input: messages,
|
|
15002
|
+
metadata: buildOpenRouterMetadata2(metadata, httpReferer, xTitle)
|
|
13526
15003
|
};
|
|
13527
15004
|
},
|
|
13528
15005
|
extractOutput: (result) => {
|
|
13529
15006
|
return isObject(result) ? result.choices : void 0;
|
|
13530
15007
|
},
|
|
13531
15008
|
extractMetrics: (result, startTime) => {
|
|
13532
|
-
const metrics =
|
|
15009
|
+
const metrics = parseOpenRouterMetricsFromUsage2(_optionalChain([result, 'optionalAccess', _295 => _295.usage]));
|
|
13533
15010
|
if (startTime) {
|
|
13534
15011
|
metrics.time_to_first_token = getCurrentUnixTimestamp() - startTime;
|
|
13535
15012
|
}
|
|
@@ -13544,9 +15021,9 @@ var OpenRouterPlugin = class extends BasePlugin {
|
|
|
13544
15021
|
type: "llm" /* LLM */,
|
|
13545
15022
|
extractInput: (args) => {
|
|
13546
15023
|
const request = getOpenRouterRequestArg(args);
|
|
13547
|
-
const requestBody = isObject(_optionalChain([request, 'optionalAccess',
|
|
13548
|
-
const httpReferer = _optionalChain([request, 'optionalAccess',
|
|
13549
|
-
const xTitle = _optionalChain([request, 'optionalAccess',
|
|
15024
|
+
const requestBody = isObject(_optionalChain([request, 'optionalAccess', _296 => _296.requestBody])) ? request.requestBody : {};
|
|
15025
|
+
const httpReferer = _optionalChain([request, 'optionalAccess', _297 => _297.httpReferer]);
|
|
15026
|
+
const xTitle = _optionalChain([request, 'optionalAccess', _298 => _298.xTitle]);
|
|
13550
15027
|
const { input, ...metadata } = requestBody;
|
|
13551
15028
|
return {
|
|
13552
15029
|
input,
|
|
@@ -13561,17 +15038,17 @@ var OpenRouterPlugin = class extends BasePlugin {
|
|
|
13561
15038
|
if (!isObject(result)) {
|
|
13562
15039
|
return void 0;
|
|
13563
15040
|
}
|
|
13564
|
-
const embedding = _optionalChain([result, 'access',
|
|
15041
|
+
const embedding = _optionalChain([result, 'access', _299 => _299.data, 'optionalAccess', _300 => _300[0], 'optionalAccess', _301 => _301.embedding]);
|
|
13565
15042
|
return Array.isArray(embedding) ? { embedding_length: embedding.length } : void 0;
|
|
13566
15043
|
},
|
|
13567
15044
|
extractMetadata: (result) => {
|
|
13568
15045
|
if (!isObject(result)) {
|
|
13569
15046
|
return void 0;
|
|
13570
15047
|
}
|
|
13571
|
-
return
|
|
15048
|
+
return extractOpenRouterResponseMetadata2(result);
|
|
13572
15049
|
},
|
|
13573
15050
|
extractMetrics: (result) => {
|
|
13574
|
-
return isObject(result) ?
|
|
15051
|
+
return isObject(result) ? parseOpenRouterMetricsFromUsage2(result.usage) : {};
|
|
13575
15052
|
}
|
|
13576
15053
|
})
|
|
13577
15054
|
);
|
|
@@ -13581,19 +15058,19 @@ var OpenRouterPlugin = class extends BasePlugin {
|
|
|
13581
15058
|
type: "llm" /* LLM */,
|
|
13582
15059
|
extractInput: (args) => {
|
|
13583
15060
|
const request = getOpenRouterRequestArg(args);
|
|
13584
|
-
const openResponsesRequest = isObject(_optionalChain([request, 'optionalAccess',
|
|
13585
|
-
const httpReferer = _optionalChain([request, 'optionalAccess',
|
|
13586
|
-
const xTitle = _optionalChain([request, 'optionalAccess',
|
|
15061
|
+
const openResponsesRequest = isObject(_optionalChain([request, 'optionalAccess', _302 => _302.openResponsesRequest])) ? request.openResponsesRequest : {};
|
|
15062
|
+
const httpReferer = _optionalChain([request, 'optionalAccess', _303 => _303.httpReferer]);
|
|
15063
|
+
const xTitle = _optionalChain([request, 'optionalAccess', _304 => _304.xTitle]);
|
|
13587
15064
|
const { input, ...metadata } = openResponsesRequest;
|
|
13588
15065
|
return {
|
|
13589
15066
|
input,
|
|
13590
|
-
metadata:
|
|
15067
|
+
metadata: buildOpenRouterMetadata2(metadata, httpReferer, xTitle)
|
|
13591
15068
|
};
|
|
13592
15069
|
},
|
|
13593
|
-
extractOutput: (result) =>
|
|
13594
|
-
extractMetadata: (result) =>
|
|
15070
|
+
extractOutput: (result) => extractOpenRouterResponseOutput2(result),
|
|
15071
|
+
extractMetadata: (result) => extractOpenRouterResponseMetadata2(result),
|
|
13595
15072
|
extractMetrics: (result, startTime) => {
|
|
13596
|
-
const metrics =
|
|
15073
|
+
const metrics = parseOpenRouterMetricsFromUsage2(_optionalChain([result, 'optionalAccess', _305 => _305.usage]));
|
|
13597
15074
|
if (startTime) {
|
|
13598
15075
|
metrics.time_to_first_token = getCurrentUnixTimestamp() - startTime;
|
|
13599
15076
|
}
|
|
@@ -13607,15 +15084,15 @@ var OpenRouterPlugin = class extends BasePlugin {
|
|
|
13607
15084
|
name: "openrouter.callModel",
|
|
13608
15085
|
type: "llm" /* LLM */,
|
|
13609
15086
|
extractInput: (args) => {
|
|
13610
|
-
const request =
|
|
15087
|
+
const request = getOpenRouterCallModelRequestArg2(args);
|
|
13611
15088
|
return {
|
|
13612
|
-
input: request ?
|
|
13613
|
-
metadata: request ?
|
|
15089
|
+
input: request ? extractOpenRouterCallModelInput2(request) : void 0,
|
|
15090
|
+
metadata: request ? extractOpenRouterCallModelMetadata2(request) : { provider: "openrouter" }
|
|
13614
15091
|
};
|
|
13615
15092
|
},
|
|
13616
15093
|
patchResult: ({ endEvent, result, span }) => {
|
|
13617
|
-
return
|
|
13618
|
-
request:
|
|
15094
|
+
return patchOpenRouterCallModelResult2({
|
|
15095
|
+
request: getOpenRouterCallModelRequestArg2(endEvent.arguments),
|
|
13619
15096
|
result,
|
|
13620
15097
|
span
|
|
13621
15098
|
});
|
|
@@ -13627,13 +15104,13 @@ var OpenRouterPlugin = class extends BasePlugin {
|
|
|
13627
15104
|
name: "openrouter.beta.responses.send",
|
|
13628
15105
|
type: "llm" /* LLM */,
|
|
13629
15106
|
extractInput: (args, event) => {
|
|
13630
|
-
const request =
|
|
13631
|
-
const metadata = request ?
|
|
15107
|
+
const request = getOpenRouterCallModelRequestArg2(args);
|
|
15108
|
+
const metadata = request ? extractOpenRouterCallModelMetadata2(request) : { provider: "openrouter" };
|
|
13632
15109
|
if (isObject(metadata) && "tools" in metadata) {
|
|
13633
15110
|
delete metadata.tools;
|
|
13634
15111
|
}
|
|
13635
15112
|
return {
|
|
13636
|
-
input: request ?
|
|
15113
|
+
input: request ? extractOpenRouterCallModelInput2(request) : void 0,
|
|
13637
15114
|
metadata: {
|
|
13638
15115
|
...metadata,
|
|
13639
15116
|
step: event.step,
|
|
@@ -13641,21 +15118,21 @@ var OpenRouterPlugin = class extends BasePlugin {
|
|
|
13641
15118
|
}
|
|
13642
15119
|
};
|
|
13643
15120
|
},
|
|
13644
|
-
extractOutput: (result) =>
|
|
15121
|
+
extractOutput: (result) => extractOpenRouterResponseOutput2(result),
|
|
13645
15122
|
extractMetadata: (result, event) => {
|
|
13646
15123
|
if (!isObject(result)) {
|
|
13647
15124
|
return {
|
|
13648
|
-
step: _optionalChain([event, 'optionalAccess',
|
|
13649
|
-
step_type: _optionalChain([event, 'optionalAccess',
|
|
15125
|
+
step: _optionalChain([event, 'optionalAccess', _306 => _306.step]),
|
|
15126
|
+
step_type: _optionalChain([event, 'optionalAccess', _307 => _307.stepType])
|
|
13650
15127
|
};
|
|
13651
15128
|
}
|
|
13652
15129
|
return {
|
|
13653
|
-
...
|
|
13654
|
-
..._optionalChain([event, 'optionalAccess',
|
|
13655
|
-
..._optionalChain([event, 'optionalAccess',
|
|
15130
|
+
...extractOpenRouterResponseMetadata2(result) || {},
|
|
15131
|
+
..._optionalChain([event, 'optionalAccess', _308 => _308.step]) !== void 0 ? { step: event.step } : {},
|
|
15132
|
+
..._optionalChain([event, 'optionalAccess', _309 => _309.stepType]) ? { step_type: event.stepType } : {}
|
|
13656
15133
|
};
|
|
13657
15134
|
},
|
|
13658
|
-
extractMetrics: (result) => isObject(result) ?
|
|
15135
|
+
extractMetrics: (result) => isObject(result) ? parseOpenRouterMetricsFromUsage2(result.usage) : {}
|
|
13659
15136
|
})
|
|
13660
15137
|
);
|
|
13661
15138
|
this.unsubscribers.push(
|
|
@@ -13681,11 +15158,11 @@ var OpenRouterPlugin = class extends BasePlugin {
|
|
|
13681
15158
|
const callModelChannel = openRouterChannels.callModel.tracingChannel();
|
|
13682
15159
|
const callModelHandlers = {
|
|
13683
15160
|
start: (event) => {
|
|
13684
|
-
const request =
|
|
15161
|
+
const request = getOpenRouterCallModelRequestArg2(event.arguments);
|
|
13685
15162
|
if (!request) {
|
|
13686
15163
|
return;
|
|
13687
15164
|
}
|
|
13688
|
-
|
|
15165
|
+
patchOpenRouterCallModelRequestTools2(request);
|
|
13689
15166
|
}
|
|
13690
15167
|
};
|
|
13691
15168
|
callModelChannel.subscribe(callModelHandlers);
|
|
@@ -13694,20 +15171,20 @@ var OpenRouterPlugin = class extends BasePlugin {
|
|
|
13694
15171
|
});
|
|
13695
15172
|
}
|
|
13696
15173
|
};
|
|
13697
|
-
function
|
|
15174
|
+
function normalizeArgs2(args) {
|
|
13698
15175
|
if (Array.isArray(args)) {
|
|
13699
15176
|
return args;
|
|
13700
15177
|
}
|
|
13701
|
-
if (
|
|
15178
|
+
if (isArrayLike2(args)) {
|
|
13702
15179
|
return Array.from(args);
|
|
13703
15180
|
}
|
|
13704
15181
|
return [args];
|
|
13705
15182
|
}
|
|
13706
|
-
function
|
|
15183
|
+
function isArrayLike2(value) {
|
|
13707
15184
|
return isObject(value) && "length" in value && typeof value.length === "number" && Number.isInteger(value.length) && value.length >= 0;
|
|
13708
15185
|
}
|
|
13709
15186
|
function getOpenRouterRequestArg(args) {
|
|
13710
|
-
const normalizedArgs =
|
|
15187
|
+
const normalizedArgs = normalizeArgs2(args);
|
|
13711
15188
|
const keyedCandidate = normalizedArgs.find(
|
|
13712
15189
|
(arg) => isObject(arg) && ("chatGenerationParams" in arg || "requestBody" in arg || "openResponsesRequest" in arg)
|
|
13713
15190
|
);
|
|
@@ -13717,11 +15194,11 @@ function getOpenRouterRequestArg(args) {
|
|
|
13717
15194
|
const firstObjectArg = normalizedArgs.find((arg) => isObject(arg));
|
|
13718
15195
|
return isObject(firstObjectArg) ? firstObjectArg : void 0;
|
|
13719
15196
|
}
|
|
13720
|
-
function
|
|
13721
|
-
const firstObjectArg =
|
|
15197
|
+
function getOpenRouterCallModelRequestArg2(args) {
|
|
15198
|
+
const firstObjectArg = normalizeArgs2(args).find((arg) => isObject(arg));
|
|
13722
15199
|
return isObject(firstObjectArg) ? firstObjectArg : void 0;
|
|
13723
15200
|
}
|
|
13724
|
-
var
|
|
15201
|
+
var TOKEN_NAME_MAP3 = {
|
|
13725
15202
|
promptTokens: "prompt_tokens",
|
|
13726
15203
|
inputTokens: "prompt_tokens",
|
|
13727
15204
|
completionTokens: "completion_tokens",
|
|
@@ -13733,7 +15210,7 @@ var TOKEN_NAME_MAP2 = {
|
|
|
13733
15210
|
output_tokens: "completion_tokens",
|
|
13734
15211
|
total_tokens: "tokens"
|
|
13735
15212
|
};
|
|
13736
|
-
var
|
|
15213
|
+
var TOKEN_DETAIL_PREFIX_MAP2 = {
|
|
13737
15214
|
promptTokensDetails: "prompt",
|
|
13738
15215
|
inputTokensDetails: "prompt",
|
|
13739
15216
|
completionTokensDetails: "completion",
|
|
@@ -13745,23 +15222,23 @@ var TOKEN_DETAIL_PREFIX_MAP = {
|
|
|
13745
15222
|
output_tokens_details: "completion",
|
|
13746
15223
|
cost_details: "cost"
|
|
13747
15224
|
};
|
|
13748
|
-
function
|
|
15225
|
+
function camelToSnake2(value) {
|
|
13749
15226
|
return value.replace(/[A-Z]/g, (match) => `_${match.toLowerCase()}`);
|
|
13750
15227
|
}
|
|
13751
|
-
function
|
|
15228
|
+
function parseOpenRouterMetricsFromUsage2(usage) {
|
|
13752
15229
|
if (!isObject(usage)) {
|
|
13753
15230
|
return {};
|
|
13754
15231
|
}
|
|
13755
15232
|
const metrics = {};
|
|
13756
15233
|
for (const [name, value] of Object.entries(usage)) {
|
|
13757
15234
|
if (typeof value === "number") {
|
|
13758
|
-
metrics[
|
|
15235
|
+
metrics[TOKEN_NAME_MAP3[name] || camelToSnake2(name)] = value;
|
|
13759
15236
|
continue;
|
|
13760
15237
|
}
|
|
13761
15238
|
if (!isObject(value)) {
|
|
13762
15239
|
continue;
|
|
13763
15240
|
}
|
|
13764
|
-
const prefix =
|
|
15241
|
+
const prefix = TOKEN_DETAIL_PREFIX_MAP2[name];
|
|
13765
15242
|
if (!prefix) {
|
|
13766
15243
|
continue;
|
|
13767
15244
|
}
|
|
@@ -13769,12 +15246,12 @@ function parseOpenRouterMetricsFromUsage(usage) {
|
|
|
13769
15246
|
if (typeof nestedValue !== "number") {
|
|
13770
15247
|
continue;
|
|
13771
15248
|
}
|
|
13772
|
-
metrics[`${prefix}_${
|
|
15249
|
+
metrics[`${prefix}_${camelToSnake2(nestedName)}`] = nestedValue;
|
|
13773
15250
|
}
|
|
13774
15251
|
}
|
|
13775
15252
|
return metrics;
|
|
13776
15253
|
}
|
|
13777
|
-
function
|
|
15254
|
+
function extractOpenRouterUsageMetadata2(usage) {
|
|
13778
15255
|
if (!isObject(usage)) {
|
|
13779
15256
|
return void 0;
|
|
13780
15257
|
}
|
|
@@ -13786,13 +15263,13 @@ function extractOpenRouterUsageMetadata(usage) {
|
|
|
13786
15263
|
}
|
|
13787
15264
|
return Object.keys(metadata).length > 0 ? metadata : void 0;
|
|
13788
15265
|
}
|
|
13789
|
-
var
|
|
15266
|
+
var OMITTED_OPENROUTER_KEYS2 = /* @__PURE__ */ new Set([
|
|
13790
15267
|
"execute",
|
|
13791
15268
|
"render",
|
|
13792
15269
|
"nextTurnParams",
|
|
13793
15270
|
"requireApproval"
|
|
13794
15271
|
]);
|
|
13795
|
-
function
|
|
15272
|
+
function parseOpenRouterModelString2(model) {
|
|
13796
15273
|
if (typeof model !== "string") {
|
|
13797
15274
|
return { model };
|
|
13798
15275
|
}
|
|
@@ -13805,73 +15282,73 @@ function parseOpenRouterModelString(model) {
|
|
|
13805
15282
|
}
|
|
13806
15283
|
return { model };
|
|
13807
15284
|
}
|
|
13808
|
-
function
|
|
15285
|
+
function isZodSchema4(value) {
|
|
13809
15286
|
return value != null && typeof value === "object" && "_def" in value && typeof value._def === "object";
|
|
13810
15287
|
}
|
|
13811
|
-
function
|
|
15288
|
+
function serializeZodSchema4(schema) {
|
|
13812
15289
|
try {
|
|
13813
15290
|
return zodToJsonSchema(schema);
|
|
13814
|
-
} catch (
|
|
15291
|
+
} catch (e38) {
|
|
13815
15292
|
return {
|
|
13816
15293
|
type: "object",
|
|
13817
15294
|
description: "Zod schema (conversion failed)"
|
|
13818
15295
|
};
|
|
13819
15296
|
}
|
|
13820
15297
|
}
|
|
13821
|
-
function
|
|
15298
|
+
function serializeOpenRouterTool2(tool) {
|
|
13822
15299
|
if (!isObject(tool)) {
|
|
13823
15300
|
return tool;
|
|
13824
15301
|
}
|
|
13825
15302
|
const serialized = {};
|
|
13826
15303
|
for (const [key, value] of Object.entries(tool)) {
|
|
13827
|
-
if (
|
|
15304
|
+
if (OMITTED_OPENROUTER_KEYS2.has(key)) {
|
|
13828
15305
|
continue;
|
|
13829
15306
|
}
|
|
13830
15307
|
if (key === "function" && isObject(value)) {
|
|
13831
|
-
serialized.function =
|
|
15308
|
+
serialized.function = sanitizeOpenRouterLoggedValue2(value);
|
|
13832
15309
|
continue;
|
|
13833
15310
|
}
|
|
13834
|
-
serialized[key] =
|
|
15311
|
+
serialized[key] = sanitizeOpenRouterLoggedValue2(value);
|
|
13835
15312
|
}
|
|
13836
15313
|
return serialized;
|
|
13837
15314
|
}
|
|
13838
|
-
function
|
|
15315
|
+
function serializeOpenRouterToolsForLogging2(tools) {
|
|
13839
15316
|
if (!Array.isArray(tools)) {
|
|
13840
15317
|
return void 0;
|
|
13841
15318
|
}
|
|
13842
|
-
return tools.map((tool) =>
|
|
15319
|
+
return tools.map((tool) => serializeOpenRouterTool2(tool));
|
|
13843
15320
|
}
|
|
13844
|
-
function
|
|
13845
|
-
if (
|
|
13846
|
-
return
|
|
15321
|
+
function sanitizeOpenRouterLoggedValue2(value) {
|
|
15322
|
+
if (isZodSchema4(value)) {
|
|
15323
|
+
return serializeZodSchema4(value);
|
|
13847
15324
|
}
|
|
13848
15325
|
if (typeof value === "function") {
|
|
13849
15326
|
return "[Function]";
|
|
13850
15327
|
}
|
|
13851
15328
|
if (Array.isArray(value)) {
|
|
13852
|
-
return value.map((entry) =>
|
|
15329
|
+
return value.map((entry) => sanitizeOpenRouterLoggedValue2(entry));
|
|
13853
15330
|
}
|
|
13854
15331
|
if (!isObject(value)) {
|
|
13855
15332
|
return value;
|
|
13856
15333
|
}
|
|
13857
15334
|
const sanitized = {};
|
|
13858
15335
|
for (const [key, entry] of Object.entries(value)) {
|
|
13859
|
-
if (
|
|
15336
|
+
if (OMITTED_OPENROUTER_KEYS2.has(key)) {
|
|
13860
15337
|
continue;
|
|
13861
15338
|
}
|
|
13862
15339
|
if (key === "tools" && Array.isArray(entry)) {
|
|
13863
|
-
sanitized.tools =
|
|
15340
|
+
sanitized.tools = serializeOpenRouterToolsForLogging2(entry);
|
|
13864
15341
|
continue;
|
|
13865
15342
|
}
|
|
13866
|
-
sanitized[key] =
|
|
15343
|
+
sanitized[key] = sanitizeOpenRouterLoggedValue2(entry);
|
|
13867
15344
|
}
|
|
13868
15345
|
return sanitized;
|
|
13869
15346
|
}
|
|
13870
|
-
function
|
|
13871
|
-
const sanitized =
|
|
15347
|
+
function buildOpenRouterMetadata2(metadata, httpReferer, xTitle) {
|
|
15348
|
+
const sanitized = sanitizeOpenRouterLoggedValue2(metadata);
|
|
13872
15349
|
const metadataRecord = isObject(sanitized) ? sanitized : {};
|
|
13873
15350
|
const { model, provider: providerRouting, ...rest } = metadataRecord;
|
|
13874
|
-
const normalizedModel =
|
|
15351
|
+
const normalizedModel = parseOpenRouterModelString2(model);
|
|
13875
15352
|
return {
|
|
13876
15353
|
...rest,
|
|
13877
15354
|
...normalizedModel.model !== void 0 ? { model: normalizedModel.model } : {},
|
|
@@ -13882,33 +15359,33 @@ function buildOpenRouterMetadata(metadata, httpReferer, xTitle) {
|
|
|
13882
15359
|
};
|
|
13883
15360
|
}
|
|
13884
15361
|
function buildOpenRouterEmbeddingMetadata(metadata, httpReferer, xTitle) {
|
|
13885
|
-
const normalized =
|
|
15362
|
+
const normalized = buildOpenRouterMetadata2(metadata, httpReferer, xTitle);
|
|
13886
15363
|
return typeof normalized.model === "string" ? {
|
|
13887
15364
|
...normalized,
|
|
13888
15365
|
embedding_model: normalized.model
|
|
13889
15366
|
} : normalized;
|
|
13890
15367
|
}
|
|
13891
|
-
function
|
|
13892
|
-
return isObject(request) && "input" in request ?
|
|
15368
|
+
function extractOpenRouterCallModelInput2(request) {
|
|
15369
|
+
return isObject(request) && "input" in request ? sanitizeOpenRouterLoggedValue2(request.input) : void 0;
|
|
13893
15370
|
}
|
|
13894
|
-
function
|
|
15371
|
+
function extractOpenRouterCallModelMetadata2(request) {
|
|
13895
15372
|
if (!isObject(request)) {
|
|
13896
15373
|
return { provider: "openrouter" };
|
|
13897
15374
|
}
|
|
13898
15375
|
const { input: _input, ...metadata } = request;
|
|
13899
|
-
return
|
|
15376
|
+
return buildOpenRouterMetadata2(metadata, void 0, void 0);
|
|
13900
15377
|
}
|
|
13901
|
-
function
|
|
15378
|
+
function extractOpenRouterResponseMetadata2(result) {
|
|
13902
15379
|
if (!isObject(result)) {
|
|
13903
15380
|
return void 0;
|
|
13904
15381
|
}
|
|
13905
15382
|
const { output: _output, data: _data, usage, ...metadata } = result;
|
|
13906
|
-
const sanitized =
|
|
15383
|
+
const sanitized = sanitizeOpenRouterLoggedValue2(metadata);
|
|
13907
15384
|
const metadataRecord = isObject(sanitized) ? sanitized : {};
|
|
13908
15385
|
const { model, provider, ...rest } = metadataRecord;
|
|
13909
|
-
const normalizedModel =
|
|
15386
|
+
const normalizedModel = parseOpenRouterModelString2(model);
|
|
13910
15387
|
const normalizedProvider = (typeof provider === "string" ? provider : void 0) || normalizedModel.provider;
|
|
13911
|
-
const usageMetadata =
|
|
15388
|
+
const usageMetadata = extractOpenRouterUsageMetadata2(usage);
|
|
13912
15389
|
const combined = {
|
|
13913
15390
|
...rest,
|
|
13914
15391
|
...normalizedModel.model !== void 0 ? { model: normalizedModel.model } : {},
|
|
@@ -13917,22 +15394,22 @@ function extractOpenRouterResponseMetadata(result) {
|
|
|
13917
15394
|
};
|
|
13918
15395
|
return Object.keys(combined).length > 0 ? combined : void 0;
|
|
13919
15396
|
}
|
|
13920
|
-
function
|
|
15397
|
+
function extractOpenRouterResponseOutput2(response, fallbackOutput) {
|
|
13921
15398
|
if (isObject(response) && "output" in response && response.output !== void 0) {
|
|
13922
|
-
return
|
|
15399
|
+
return sanitizeOpenRouterLoggedValue2(response.output);
|
|
13923
15400
|
}
|
|
13924
15401
|
if (fallbackOutput !== void 0) {
|
|
13925
|
-
return
|
|
15402
|
+
return sanitizeOpenRouterLoggedValue2(fallbackOutput);
|
|
13926
15403
|
}
|
|
13927
15404
|
return void 0;
|
|
13928
15405
|
}
|
|
13929
|
-
var
|
|
13930
|
-
function
|
|
15406
|
+
var OPENROUTER_WRAPPED_TOOL2 = Symbol("braintrust.openrouter.wrappedTool");
|
|
15407
|
+
function patchOpenRouterCallModelRequestTools2(request) {
|
|
13931
15408
|
if (!Array.isArray(request.tools) || request.tools.length === 0) {
|
|
13932
15409
|
return void 0;
|
|
13933
15410
|
}
|
|
13934
15411
|
const originalTools = request.tools;
|
|
13935
|
-
const wrappedTools = originalTools.map((tool) =>
|
|
15412
|
+
const wrappedTools = originalTools.map((tool) => wrapOpenRouterTool2(tool));
|
|
13936
15413
|
const didPatch = wrappedTools.some(
|
|
13937
15414
|
(tool, index) => tool !== originalTools[index]
|
|
13938
15415
|
);
|
|
@@ -13944,8 +15421,8 @@ function patchOpenRouterCallModelRequestTools(request) {
|
|
|
13944
15421
|
request.tools = originalTools;
|
|
13945
15422
|
};
|
|
13946
15423
|
}
|
|
13947
|
-
function
|
|
13948
|
-
if (
|
|
15424
|
+
function wrapOpenRouterTool2(tool) {
|
|
15425
|
+
if (isWrappedTool2(tool) || !tool.function || typeof tool.function !== "object" || typeof tool.function.execute !== "function") {
|
|
13949
15426
|
return tool;
|
|
13950
15427
|
}
|
|
13951
15428
|
const toolName = tool.function.name || "tool";
|
|
@@ -13955,26 +15432,26 @@ function wrapOpenRouterTool(tool) {
|
|
|
13955
15432
|
function: {
|
|
13956
15433
|
...tool.function,
|
|
13957
15434
|
execute(...args) {
|
|
13958
|
-
return
|
|
15435
|
+
return traceToolExecution2({
|
|
13959
15436
|
args,
|
|
13960
15437
|
execute: () => Reflect.apply(originalExecute, this, args),
|
|
13961
|
-
toolCallId:
|
|
15438
|
+
toolCallId: getToolCallId2(args[1]),
|
|
13962
15439
|
toolName
|
|
13963
15440
|
});
|
|
13964
15441
|
}
|
|
13965
15442
|
}
|
|
13966
15443
|
};
|
|
13967
|
-
Object.defineProperty(wrappedTool,
|
|
15444
|
+
Object.defineProperty(wrappedTool, OPENROUTER_WRAPPED_TOOL2, {
|
|
13968
15445
|
value: true,
|
|
13969
15446
|
enumerable: false,
|
|
13970
15447
|
configurable: false
|
|
13971
15448
|
});
|
|
13972
15449
|
return wrappedTool;
|
|
13973
15450
|
}
|
|
13974
|
-
function
|
|
13975
|
-
return Boolean(tool[
|
|
15451
|
+
function isWrappedTool2(tool) {
|
|
15452
|
+
return Boolean(tool[OPENROUTER_WRAPPED_TOOL2]);
|
|
13976
15453
|
}
|
|
13977
|
-
function
|
|
15454
|
+
function traceToolExecution2(args) {
|
|
13978
15455
|
const tracingChannel2 = openRouterChannels.toolExecute.tracingChannel();
|
|
13979
15456
|
const input = args.args.length > 0 ? args.args[0] : void 0;
|
|
13980
15457
|
const event = {
|
|
@@ -13988,15 +15465,15 @@ function traceToolExecution(args) {
|
|
|
13988
15465
|
tracingChannel2.start.publish(event);
|
|
13989
15466
|
try {
|
|
13990
15467
|
const result = args.execute();
|
|
13991
|
-
return
|
|
15468
|
+
return publishToolResult2(tracingChannel2, event, result);
|
|
13992
15469
|
} catch (error) {
|
|
13993
|
-
event.error =
|
|
15470
|
+
event.error = normalizeError2(error);
|
|
13994
15471
|
tracingChannel2.error.publish(event);
|
|
13995
15472
|
throw error;
|
|
13996
15473
|
}
|
|
13997
15474
|
}
|
|
13998
|
-
function
|
|
13999
|
-
if (
|
|
15475
|
+
function publishToolResult2(tracingChannel2, event, result) {
|
|
15476
|
+
if (isPromiseLike4(result)) {
|
|
14000
15477
|
return result.then(
|
|
14001
15478
|
(resolved) => {
|
|
14002
15479
|
event.result = resolved;
|
|
@@ -14004,7 +15481,7 @@ function publishToolResult(tracingChannel2, event, result) {
|
|
|
14004
15481
|
return resolved;
|
|
14005
15482
|
},
|
|
14006
15483
|
(error) => {
|
|
14007
|
-
event.error =
|
|
15484
|
+
event.error = normalizeError2(error);
|
|
14008
15485
|
tracingChannel2.error.publish(event);
|
|
14009
15486
|
throw error;
|
|
14010
15487
|
}
|
|
@@ -14014,11 +15491,11 @@ function publishToolResult(tracingChannel2, event, result) {
|
|
|
14014
15491
|
tracingChannel2.asyncEnd.publish(event);
|
|
14015
15492
|
return result;
|
|
14016
15493
|
}
|
|
14017
|
-
function
|
|
15494
|
+
function getToolCallId2(context) {
|
|
14018
15495
|
const toolContext = context;
|
|
14019
|
-
return typeof _optionalChain([toolContext, 'optionalAccess',
|
|
15496
|
+
return typeof _optionalChain([toolContext, 'optionalAccess', _310 => _310.toolCall, 'optionalAccess', _311 => _311.id]) === "string" ? toolContext.toolCall.id : void 0;
|
|
14020
15497
|
}
|
|
14021
|
-
function
|
|
15498
|
+
function isPromiseLike4(value) {
|
|
14022
15499
|
return !!value && (typeof value === "object" || typeof value === "function") && "then" in value && typeof value.then === "function";
|
|
14023
15500
|
}
|
|
14024
15501
|
function aggregateOpenRouterChatChunks(chunks) {
|
|
@@ -14030,12 +15507,12 @@ function aggregateOpenRouterChatChunks(chunks) {
|
|
|
14030
15507
|
for (const chunk of chunks) {
|
|
14031
15508
|
metrics = {
|
|
14032
15509
|
...metrics,
|
|
14033
|
-
...
|
|
15510
|
+
...parseOpenRouterMetricsFromUsage2(_optionalChain([chunk, 'optionalAccess', _312 => _312.usage]))
|
|
14034
15511
|
};
|
|
14035
|
-
const choice = _optionalChain([chunk, 'optionalAccess',
|
|
14036
|
-
const delta = _optionalChain([choice, 'optionalAccess',
|
|
15512
|
+
const choice = _optionalChain([chunk, 'optionalAccess', _313 => _313.choices, 'optionalAccess', _314 => _314[0]]);
|
|
15513
|
+
const delta = _optionalChain([choice, 'optionalAccess', _315 => _315.delta]);
|
|
14037
15514
|
if (!delta) {
|
|
14038
|
-
if (_optionalChain([choice, 'optionalAccess',
|
|
15515
|
+
if (_optionalChain([choice, 'optionalAccess', _316 => _316.finish_reason]) !== void 0) {
|
|
14039
15516
|
finishReason = choice.finish_reason;
|
|
14040
15517
|
}
|
|
14041
15518
|
continue;
|
|
@@ -14046,7 +15523,7 @@ function aggregateOpenRouterChatChunks(chunks) {
|
|
|
14046
15523
|
if (typeof delta.content === "string") {
|
|
14047
15524
|
content += delta.content;
|
|
14048
15525
|
}
|
|
14049
|
-
const choiceFinishReason = _nullishCoalesce(_nullishCoalesce(_optionalChain([choice, 'optionalAccess',
|
|
15526
|
+
const choiceFinishReason = _nullishCoalesce(_nullishCoalesce(_optionalChain([choice, 'optionalAccess', _317 => _317.finishReason]), () => ( _optionalChain([choice, 'optionalAccess', _318 => _318.finish_reason]))), () => ( void 0));
|
|
14050
15527
|
const deltaFinishReason = _nullishCoalesce(_nullishCoalesce(delta.finishReason, () => ( delta.finish_reason)), () => ( void 0));
|
|
14051
15528
|
if (choiceFinishReason !== void 0) {
|
|
14052
15529
|
finishReason = choiceFinishReason;
|
|
@@ -14058,11 +15535,11 @@ function aggregateOpenRouterChatChunks(chunks) {
|
|
|
14058
15535
|
continue;
|
|
14059
15536
|
}
|
|
14060
15537
|
for (const toolDelta of toolCallDeltas) {
|
|
14061
|
-
if (!_optionalChain([toolDelta, 'optionalAccess',
|
|
15538
|
+
if (!_optionalChain([toolDelta, 'optionalAccess', _319 => _319.function])) {
|
|
14062
15539
|
continue;
|
|
14063
15540
|
}
|
|
14064
15541
|
const toolIndex = _nullishCoalesce(toolDelta.index, () => ( 0));
|
|
14065
|
-
const existingToolCall = _optionalChain([toolCalls, 'optionalAccess',
|
|
15542
|
+
const existingToolCall = _optionalChain([toolCalls, 'optionalAccess', _320 => _320[toolIndex]]);
|
|
14066
15543
|
if (!existingToolCall || toolDelta.id && existingToolCall.id !== void 0 && existingToolCall.id !== toolDelta.id) {
|
|
14067
15544
|
const nextToolCalls = [...toolCalls || []];
|
|
14068
15545
|
nextToolCalls[toolIndex] = {
|
|
@@ -14109,7 +15586,7 @@ function aggregateOpenRouterChatChunks(chunks) {
|
|
|
14109
15586
|
function aggregateOpenRouterResponseStreamEvents(chunks) {
|
|
14110
15587
|
let finalResponse;
|
|
14111
15588
|
for (const chunk of chunks) {
|
|
14112
|
-
const response = _optionalChain([chunk, 'optionalAccess',
|
|
15589
|
+
const response = _optionalChain([chunk, 'optionalAccess', _321 => _321.response]);
|
|
14113
15590
|
if (!response) {
|
|
14114
15591
|
continue;
|
|
14115
15592
|
}
|
|
@@ -14124,15 +15601,15 @@ function aggregateOpenRouterResponseStreamEvents(chunks) {
|
|
|
14124
15601
|
};
|
|
14125
15602
|
}
|
|
14126
15603
|
return {
|
|
14127
|
-
output:
|
|
14128
|
-
metrics:
|
|
14129
|
-
...
|
|
15604
|
+
output: extractOpenRouterResponseOutput2(finalResponse),
|
|
15605
|
+
metrics: parseOpenRouterMetricsFromUsage2(finalResponse.usage),
|
|
15606
|
+
...extractOpenRouterResponseMetadata2(finalResponse) ? { metadata: extractOpenRouterResponseMetadata2(finalResponse) } : {}
|
|
14130
15607
|
};
|
|
14131
15608
|
}
|
|
14132
|
-
var
|
|
15609
|
+
var OPENROUTER_WRAPPED_CALL_MODEL_RESULT2 = Symbol(
|
|
14133
15610
|
"braintrust.openrouter.wrappedCallModelResult"
|
|
14134
15611
|
);
|
|
14135
|
-
var
|
|
15612
|
+
var OPENROUTER_CALL_MODEL_STREAM_METHODS2 = [
|
|
14136
15613
|
"getFullResponsesStream",
|
|
14137
15614
|
"getItemsStream",
|
|
14138
15615
|
"getNewMessagesStream",
|
|
@@ -14141,26 +15618,26 @@ var OPENROUTER_CALL_MODEL_STREAM_METHODS = [
|
|
|
14141
15618
|
"getToolCallsStream",
|
|
14142
15619
|
"getToolStream"
|
|
14143
15620
|
];
|
|
14144
|
-
var
|
|
15621
|
+
var OPENROUTER_CALL_MODEL_CONTEXT_METHODS2 = [
|
|
14145
15622
|
"cancel",
|
|
14146
15623
|
"getPendingToolCalls",
|
|
14147
15624
|
"getState",
|
|
14148
15625
|
"getToolCalls",
|
|
14149
15626
|
"requiresApproval"
|
|
14150
15627
|
];
|
|
14151
|
-
function
|
|
15628
|
+
function patchOpenRouterCallModelResult2(args) {
|
|
14152
15629
|
const { request, result, span } = args;
|
|
14153
|
-
if (!isObject(result) ||
|
|
15630
|
+
if (!isObject(result) || isWrappedCallModelResult2(result)) {
|
|
14154
15631
|
return false;
|
|
14155
15632
|
}
|
|
14156
15633
|
const resultLike = result;
|
|
14157
|
-
const hasInstrumentableMethod = typeof resultLike.getResponse === "function" || typeof resultLike.getText === "function" ||
|
|
15634
|
+
const hasInstrumentableMethod = typeof resultLike.getResponse === "function" || typeof resultLike.getText === "function" || OPENROUTER_CALL_MODEL_STREAM_METHODS2.some(
|
|
14158
15635
|
(methodName) => typeof resultLike[methodName] === "function"
|
|
14159
15636
|
);
|
|
14160
15637
|
if (!hasInstrumentableMethod) {
|
|
14161
15638
|
return false;
|
|
14162
15639
|
}
|
|
14163
|
-
Object.defineProperty(resultLike,
|
|
15640
|
+
Object.defineProperty(resultLike, OPENROUTER_WRAPPED_CALL_MODEL_RESULT2, {
|
|
14164
15641
|
value: true,
|
|
14165
15642
|
enumerable: false,
|
|
14166
15643
|
configurable: false
|
|
@@ -14175,20 +15652,20 @@ function patchOpenRouterCallModelResult(args) {
|
|
|
14175
15652
|
return;
|
|
14176
15653
|
}
|
|
14177
15654
|
ended = true;
|
|
14178
|
-
const finalResponse =
|
|
15655
|
+
const finalResponse = getFinalOpenRouterCallModelResponse2(
|
|
14179
15656
|
resultLike,
|
|
14180
15657
|
response
|
|
14181
15658
|
);
|
|
14182
15659
|
if (finalResponse) {
|
|
14183
|
-
const rounds =
|
|
14184
|
-
const metadata =
|
|
15660
|
+
const rounds = getOpenRouterCallModelRounds2(resultLike);
|
|
15661
|
+
const metadata = extractOpenRouterCallModelResultMetadata2(
|
|
14185
15662
|
finalResponse,
|
|
14186
15663
|
rounds.length + 1
|
|
14187
15664
|
);
|
|
14188
15665
|
span.log({
|
|
14189
|
-
output:
|
|
15666
|
+
output: extractOpenRouterResponseOutput2(finalResponse, fallbackOutput),
|
|
14190
15667
|
...metadata ? { metadata } : {},
|
|
14191
|
-
metrics:
|
|
15668
|
+
metrics: aggregateOpenRouterCallModelMetrics2(rounds, finalResponse)
|
|
14192
15669
|
});
|
|
14193
15670
|
span.end();
|
|
14194
15671
|
return;
|
|
@@ -14206,7 +15683,7 @@ function patchOpenRouterCallModelResult(args) {
|
|
|
14206
15683
|
}
|
|
14207
15684
|
ended = true;
|
|
14208
15685
|
span.log({
|
|
14209
|
-
error:
|
|
15686
|
+
error: normalizeError2(error).message
|
|
14210
15687
|
});
|
|
14211
15688
|
span.end();
|
|
14212
15689
|
};
|
|
@@ -14217,7 +15694,7 @@ function patchOpenRouterCallModelResult(args) {
|
|
|
14217
15694
|
}
|
|
14218
15695
|
try {
|
|
14219
15696
|
await endSpanWithResult(await originalGetResponse(), fallbackOutput);
|
|
14220
|
-
} catch (
|
|
15697
|
+
} catch (e39) {
|
|
14221
15698
|
await endSpanWithResult(void 0, fallbackOutput);
|
|
14222
15699
|
}
|
|
14223
15700
|
};
|
|
@@ -14250,7 +15727,7 @@ function patchOpenRouterCallModelResult(args) {
|
|
|
14250
15727
|
});
|
|
14251
15728
|
};
|
|
14252
15729
|
}
|
|
14253
|
-
for (const methodName of
|
|
15730
|
+
for (const methodName of OPENROUTER_CALL_MODEL_CONTEXT_METHODS2) {
|
|
14254
15731
|
if (typeof resultLike[methodName] !== "function") {
|
|
14255
15732
|
continue;
|
|
14256
15733
|
}
|
|
@@ -14261,7 +15738,7 @@ function patchOpenRouterCallModelResult(args) {
|
|
|
14261
15738
|
});
|
|
14262
15739
|
};
|
|
14263
15740
|
}
|
|
14264
|
-
for (const methodName of
|
|
15741
|
+
for (const methodName of OPENROUTER_CALL_MODEL_STREAM_METHODS2) {
|
|
14265
15742
|
if (typeof resultLike[methodName] !== "function") {
|
|
14266
15743
|
continue;
|
|
14267
15744
|
}
|
|
@@ -14271,10 +15748,10 @@ function patchOpenRouterCallModelResult(args) {
|
|
|
14271
15748
|
span,
|
|
14272
15749
|
() => originalMethod.apply(resultLike, args2)
|
|
14273
15750
|
);
|
|
14274
|
-
if (!
|
|
15751
|
+
if (!isAsyncIterable4(stream)) {
|
|
14275
15752
|
return stream;
|
|
14276
15753
|
}
|
|
14277
|
-
return
|
|
15754
|
+
return wrapAsyncIterableWithSpan2({
|
|
14278
15755
|
finalize: finalizeFromResponse,
|
|
14279
15756
|
iteratorFactory: () => stream[Symbol.asyncIterator](),
|
|
14280
15757
|
onError: endSpanWithError,
|
|
@@ -14293,14 +15770,14 @@ function patchOpenRouterCallModelResult(args) {
|
|
|
14293
15770
|
initialTurnTraced = true;
|
|
14294
15771
|
const step = tracedTurnCount + 1;
|
|
14295
15772
|
const stepType = tracedTurnCount === 0 ? "initial" : "continue";
|
|
14296
|
-
const response = await
|
|
15773
|
+
const response = await traceOpenRouterCallModelTurn2({
|
|
14297
15774
|
fn: async () => {
|
|
14298
15775
|
const nextResponse = await originalGetInitialResponse(...args2);
|
|
14299
15776
|
tracedTurnCount++;
|
|
14300
15777
|
return nextResponse;
|
|
14301
15778
|
},
|
|
14302
15779
|
parentSpan: span,
|
|
14303
|
-
request:
|
|
15780
|
+
request: getOpenRouterResolvedRequest2(resultLike, request),
|
|
14304
15781
|
step,
|
|
14305
15782
|
stepType
|
|
14306
15783
|
});
|
|
@@ -14312,15 +15789,15 @@ function patchOpenRouterCallModelResult(args) {
|
|
|
14312
15789
|
const currentResponse = args2[0];
|
|
14313
15790
|
const toolResults = Array.isArray(args2[1]) ? args2[1] : [];
|
|
14314
15791
|
const step = tracedTurnCount + 1;
|
|
14315
|
-
const response = await
|
|
15792
|
+
const response = await traceOpenRouterCallModelTurn2({
|
|
14316
15793
|
fn: async () => {
|
|
14317
15794
|
const nextResponse = await originalMakeFollowupRequest(...args2);
|
|
14318
15795
|
tracedTurnCount++;
|
|
14319
15796
|
return nextResponse;
|
|
14320
15797
|
},
|
|
14321
15798
|
parentSpan: span,
|
|
14322
|
-
request:
|
|
14323
|
-
|
|
15799
|
+
request: buildOpenRouterFollowupRequest2(
|
|
15800
|
+
getOpenRouterResolvedRequest2(resultLike, request),
|
|
14324
15801
|
currentResponse,
|
|
14325
15802
|
toolResults
|
|
14326
15803
|
),
|
|
@@ -14332,7 +15809,7 @@ function patchOpenRouterCallModelResult(args) {
|
|
|
14332
15809
|
}
|
|
14333
15810
|
return true;
|
|
14334
15811
|
}
|
|
14335
|
-
async function
|
|
15812
|
+
async function traceOpenRouterCallModelTurn2(args) {
|
|
14336
15813
|
const context = {
|
|
14337
15814
|
arguments: [args.request],
|
|
14338
15815
|
step: args.step,
|
|
@@ -14343,25 +15820,25 @@ async function traceOpenRouterCallModelTurn(args) {
|
|
|
14343
15820
|
() => openRouterChannels.callModelTurn.tracePromise(args.fn, context)
|
|
14344
15821
|
);
|
|
14345
15822
|
}
|
|
14346
|
-
function
|
|
15823
|
+
function isWrappedCallModelResult2(value) {
|
|
14347
15824
|
return Boolean(
|
|
14348
|
-
isObject(value) && value[
|
|
15825
|
+
isObject(value) && value[OPENROUTER_WRAPPED_CALL_MODEL_RESULT2]
|
|
14349
15826
|
);
|
|
14350
15827
|
}
|
|
14351
|
-
function
|
|
15828
|
+
function extractOpenRouterCallModelResultMetadata2(response, turnCount) {
|
|
14352
15829
|
const combined = {
|
|
14353
|
-
...
|
|
15830
|
+
...extractOpenRouterResponseMetadata2(response) || {},
|
|
14354
15831
|
...turnCount !== void 0 ? { turn_count: turnCount } : {}
|
|
14355
15832
|
};
|
|
14356
15833
|
return Object.keys(combined).length > 0 ? combined : void 0;
|
|
14357
15834
|
}
|
|
14358
|
-
function
|
|
15835
|
+
function getFinalOpenRouterCallModelResponse2(result, response) {
|
|
14359
15836
|
if (isObject(response)) {
|
|
14360
15837
|
return response;
|
|
14361
15838
|
}
|
|
14362
15839
|
return isObject(result.finalResponse) ? result.finalResponse : void 0;
|
|
14363
15840
|
}
|
|
14364
|
-
function
|
|
15841
|
+
function getOpenRouterCallModelRounds2(result) {
|
|
14365
15842
|
if (!Array.isArray(result.allToolExecutionRounds)) {
|
|
14366
15843
|
return [];
|
|
14367
15844
|
}
|
|
@@ -14371,48 +15848,48 @@ function getOpenRouterCallModelRounds(result) {
|
|
|
14371
15848
|
toolResults: Array.isArray(round.toolResults) ? round.toolResults : []
|
|
14372
15849
|
})).filter((round) => round.response !== void 0);
|
|
14373
15850
|
}
|
|
14374
|
-
function
|
|
15851
|
+
function aggregateOpenRouterCallModelMetrics2(rounds, finalResponse) {
|
|
14375
15852
|
const metrics = {};
|
|
14376
15853
|
const responses = [
|
|
14377
15854
|
...rounds.map((round) => round.response).filter(isObject),
|
|
14378
15855
|
finalResponse
|
|
14379
15856
|
];
|
|
14380
15857
|
for (const response of responses) {
|
|
14381
|
-
const responseMetrics =
|
|
15858
|
+
const responseMetrics = parseOpenRouterMetricsFromUsage2(response.usage);
|
|
14382
15859
|
for (const [name, value] of Object.entries(responseMetrics)) {
|
|
14383
15860
|
metrics[name] = (metrics[name] || 0) + value;
|
|
14384
15861
|
}
|
|
14385
15862
|
}
|
|
14386
15863
|
return metrics;
|
|
14387
15864
|
}
|
|
14388
|
-
function
|
|
15865
|
+
function buildNextOpenRouterCallModelInput2(currentInput, response, toolResults) {
|
|
14389
15866
|
const normalizedInput = Array.isArray(currentInput) ? [...currentInput] : currentInput === void 0 ? [] : [currentInput];
|
|
14390
15867
|
const responseOutput = Array.isArray(response.output) ? response.output : response.output === void 0 ? [] : [response.output];
|
|
14391
15868
|
return [...normalizedInput, ...responseOutput, ...toolResults].map(
|
|
14392
|
-
(entry) =>
|
|
15869
|
+
(entry) => sanitizeOpenRouterLoggedValue2(entry)
|
|
14393
15870
|
);
|
|
14394
15871
|
}
|
|
14395
|
-
function
|
|
15872
|
+
function getOpenRouterResolvedRequest2(result, request) {
|
|
14396
15873
|
if (isObject(result.resolvedRequest)) {
|
|
14397
15874
|
return result.resolvedRequest;
|
|
14398
15875
|
}
|
|
14399
15876
|
return request;
|
|
14400
15877
|
}
|
|
14401
|
-
function
|
|
15878
|
+
function buildOpenRouterFollowupRequest2(request, currentResponse, toolResults) {
|
|
14402
15879
|
if (!request) {
|
|
14403
15880
|
return void 0;
|
|
14404
15881
|
}
|
|
14405
15882
|
return {
|
|
14406
15883
|
...request,
|
|
14407
|
-
input:
|
|
14408
|
-
|
|
15884
|
+
input: buildNextOpenRouterCallModelInput2(
|
|
15885
|
+
extractOpenRouterCallModelInput2(request),
|
|
14409
15886
|
isObject(currentResponse) ? currentResponse : {},
|
|
14410
15887
|
toolResults
|
|
14411
15888
|
),
|
|
14412
15889
|
stream: false
|
|
14413
15890
|
};
|
|
14414
15891
|
}
|
|
14415
|
-
function
|
|
15892
|
+
function wrapAsyncIterableWithSpan2(args) {
|
|
14416
15893
|
return {
|
|
14417
15894
|
[Symbol.asyncIterator]() {
|
|
14418
15895
|
const iterator = args.iteratorFactory();
|
|
@@ -14466,13 +15943,518 @@ function wrapAsyncIterableWithSpan(args) {
|
|
|
14466
15943
|
}
|
|
14467
15944
|
};
|
|
14468
15945
|
}
|
|
14469
|
-
function
|
|
15946
|
+
function isAsyncIterable4(value) {
|
|
14470
15947
|
return !!value && (typeof value === "object" || typeof value === "function") && Symbol.asyncIterator in value && typeof value[Symbol.asyncIterator] === "function";
|
|
14471
15948
|
}
|
|
14472
|
-
function
|
|
15949
|
+
function normalizeError2(error) {
|
|
14473
15950
|
return error instanceof Error ? error : new Error(String(error));
|
|
14474
15951
|
}
|
|
14475
15952
|
|
|
15953
|
+
// src/instrumentation/plugins/mistral-channels.ts
|
|
15954
|
+
var mistralChannels = defineChannels("@mistralai/mistralai", {
|
|
15955
|
+
chatComplete: channel({
|
|
15956
|
+
channelName: "chat.complete",
|
|
15957
|
+
kind: "async"
|
|
15958
|
+
}),
|
|
15959
|
+
chatStream: channel({
|
|
15960
|
+
channelName: "chat.stream",
|
|
15961
|
+
kind: "async"
|
|
15962
|
+
}),
|
|
15963
|
+
embeddingsCreate: channel({
|
|
15964
|
+
channelName: "embeddings.create",
|
|
15965
|
+
kind: "async"
|
|
15966
|
+
}),
|
|
15967
|
+
fimComplete: channel({
|
|
15968
|
+
channelName: "fim.complete",
|
|
15969
|
+
kind: "async"
|
|
15970
|
+
}),
|
|
15971
|
+
fimStream: channel({
|
|
15972
|
+
channelName: "fim.stream",
|
|
15973
|
+
kind: "async"
|
|
15974
|
+
}),
|
|
15975
|
+
agentsComplete: channel({
|
|
15976
|
+
channelName: "agents.complete",
|
|
15977
|
+
kind: "async"
|
|
15978
|
+
}),
|
|
15979
|
+
agentsStream: channel({
|
|
15980
|
+
channelName: "agents.stream",
|
|
15981
|
+
kind: "async"
|
|
15982
|
+
})
|
|
15983
|
+
});
|
|
15984
|
+
|
|
15985
|
+
// src/instrumentation/plugins/mistral-plugin.ts
|
|
15986
|
+
var MistralPlugin = class extends BasePlugin {
|
|
15987
|
+
onEnable() {
|
|
15988
|
+
this.subscribeToMistralChannels();
|
|
15989
|
+
}
|
|
15990
|
+
onDisable() {
|
|
15991
|
+
this.unsubscribers = unsubscribeAll(this.unsubscribers);
|
|
15992
|
+
}
|
|
15993
|
+
subscribeToMistralChannels() {
|
|
15994
|
+
this.unsubscribers.push(
|
|
15995
|
+
traceStreamingChannel(mistralChannels.chatComplete, {
|
|
15996
|
+
name: "mistral.chat.complete",
|
|
15997
|
+
type: "llm" /* LLM */,
|
|
15998
|
+
extractInput: extractMessagesInputWithMetadata,
|
|
15999
|
+
extractOutput: (result) => {
|
|
16000
|
+
return _optionalChain([result, 'optionalAccess', _322 => _322.choices]);
|
|
16001
|
+
},
|
|
16002
|
+
extractMetadata: (result) => extractMistralResponseMetadata(result),
|
|
16003
|
+
extractMetrics: (result, startTime) => extractMistralMetrics(_optionalChain([result, 'optionalAccess', _323 => _323.usage]), startTime)
|
|
16004
|
+
})
|
|
16005
|
+
);
|
|
16006
|
+
this.unsubscribers.push(
|
|
16007
|
+
traceStreamingChannel(mistralChannels.chatStream, {
|
|
16008
|
+
name: "mistral.chat.stream",
|
|
16009
|
+
type: "llm" /* LLM */,
|
|
16010
|
+
extractInput: extractMessagesInputWithMetadata,
|
|
16011
|
+
extractOutput: extractMistralStreamOutput,
|
|
16012
|
+
extractMetadata: (result) => extractMistralResponseMetadata(result),
|
|
16013
|
+
extractMetrics: (result, startTime) => extractMistralStreamingMetrics(result, startTime),
|
|
16014
|
+
aggregateChunks: aggregateMistralStreamChunks
|
|
16015
|
+
})
|
|
16016
|
+
);
|
|
16017
|
+
this.unsubscribers.push(
|
|
16018
|
+
traceAsyncChannel(mistralChannels.embeddingsCreate, {
|
|
16019
|
+
name: "mistral.embeddings.create",
|
|
16020
|
+
type: "llm" /* LLM */,
|
|
16021
|
+
extractInput: extractEmbeddingInputWithMetadata,
|
|
16022
|
+
extractOutput: (result) => {
|
|
16023
|
+
const embedding = _optionalChain([result, 'optionalAccess', _324 => _324.data, 'optionalAccess', _325 => _325[0], 'optionalAccess', _326 => _326.embedding]);
|
|
16024
|
+
return Array.isArray(embedding) ? { embedding_length: embedding.length } : void 0;
|
|
16025
|
+
},
|
|
16026
|
+
extractMetadata: (result) => extractMistralResponseMetadata(result),
|
|
16027
|
+
extractMetrics: (result) => parseMistralMetricsFromUsage(_optionalChain([result, 'optionalAccess', _327 => _327.usage]))
|
|
16028
|
+
})
|
|
16029
|
+
);
|
|
16030
|
+
this.unsubscribers.push(
|
|
16031
|
+
traceStreamingChannel(mistralChannels.fimComplete, {
|
|
16032
|
+
name: "mistral.fim.complete",
|
|
16033
|
+
type: "llm" /* LLM */,
|
|
16034
|
+
extractInput: extractPromptInputWithMetadata,
|
|
16035
|
+
extractOutput: (result) => {
|
|
16036
|
+
return _optionalChain([result, 'optionalAccess', _328 => _328.choices]);
|
|
16037
|
+
},
|
|
16038
|
+
extractMetadata: (result) => extractMistralResponseMetadata(result),
|
|
16039
|
+
extractMetrics: (result, startTime) => extractMistralMetrics(_optionalChain([result, 'optionalAccess', _329 => _329.usage]), startTime)
|
|
16040
|
+
})
|
|
16041
|
+
);
|
|
16042
|
+
this.unsubscribers.push(
|
|
16043
|
+
traceStreamingChannel(mistralChannels.fimStream, {
|
|
16044
|
+
name: "mistral.fim.stream",
|
|
16045
|
+
type: "llm" /* LLM */,
|
|
16046
|
+
extractInput: extractPromptInputWithMetadata,
|
|
16047
|
+
extractOutput: extractMistralStreamOutput,
|
|
16048
|
+
extractMetadata: (result) => extractMistralResponseMetadata(result),
|
|
16049
|
+
extractMetrics: (result, startTime) => extractMistralStreamingMetrics(result, startTime),
|
|
16050
|
+
aggregateChunks: aggregateMistralStreamChunks
|
|
16051
|
+
})
|
|
16052
|
+
);
|
|
16053
|
+
this.unsubscribers.push(
|
|
16054
|
+
traceStreamingChannel(mistralChannels.agentsComplete, {
|
|
16055
|
+
name: "mistral.agents.complete",
|
|
16056
|
+
type: "llm" /* LLM */,
|
|
16057
|
+
extractInput: extractMessagesInputWithMetadata,
|
|
16058
|
+
extractOutput: (result) => {
|
|
16059
|
+
return _optionalChain([result, 'optionalAccess', _330 => _330.choices]);
|
|
16060
|
+
},
|
|
16061
|
+
extractMetadata: (result) => extractMistralResponseMetadata(result),
|
|
16062
|
+
extractMetrics: (result, startTime) => extractMistralMetrics(_optionalChain([result, 'optionalAccess', _331 => _331.usage]), startTime)
|
|
16063
|
+
})
|
|
16064
|
+
);
|
|
16065
|
+
this.unsubscribers.push(
|
|
16066
|
+
traceStreamingChannel(mistralChannels.agentsStream, {
|
|
16067
|
+
name: "mistral.agents.stream",
|
|
16068
|
+
type: "llm" /* LLM */,
|
|
16069
|
+
extractInput: extractMessagesInputWithMetadata,
|
|
16070
|
+
extractOutput: extractMistralStreamOutput,
|
|
16071
|
+
extractMetadata: (result) => extractMistralResponseMetadata(result),
|
|
16072
|
+
extractMetrics: (result, startTime) => extractMistralStreamingMetrics(result, startTime),
|
|
16073
|
+
aggregateChunks: aggregateMistralStreamChunks
|
|
16074
|
+
})
|
|
16075
|
+
);
|
|
16076
|
+
}
|
|
16077
|
+
};
|
|
16078
|
+
var TOKEN_NAME_MAP4 = {
|
|
16079
|
+
promptTokens: "prompt_tokens",
|
|
16080
|
+
inputTokens: "prompt_tokens",
|
|
16081
|
+
completionTokens: "completion_tokens",
|
|
16082
|
+
outputTokens: "completion_tokens",
|
|
16083
|
+
totalTokens: "tokens",
|
|
16084
|
+
prompt_tokens: "prompt_tokens",
|
|
16085
|
+
input_tokens: "prompt_tokens",
|
|
16086
|
+
completion_tokens: "completion_tokens",
|
|
16087
|
+
output_tokens: "completion_tokens",
|
|
16088
|
+
total_tokens: "tokens",
|
|
16089
|
+
promptAudioSeconds: "prompt_audio_seconds",
|
|
16090
|
+
prompt_audio_seconds: "prompt_audio_seconds"
|
|
16091
|
+
};
|
|
16092
|
+
var TOKEN_DETAIL_PREFIX_MAP3 = {
|
|
16093
|
+
promptTokensDetails: "prompt",
|
|
16094
|
+
inputTokensDetails: "prompt",
|
|
16095
|
+
completionTokensDetails: "completion",
|
|
16096
|
+
outputTokensDetails: "completion",
|
|
16097
|
+
prompt_tokens_details: "prompt",
|
|
16098
|
+
input_tokens_details: "prompt",
|
|
16099
|
+
completion_tokens_details: "completion",
|
|
16100
|
+
output_tokens_details: "completion"
|
|
16101
|
+
};
|
|
16102
|
+
var MISTRAL_REQUEST_METADATA_ALLOWLIST = /* @__PURE__ */ new Set([
|
|
16103
|
+
"agentId",
|
|
16104
|
+
"agent_id",
|
|
16105
|
+
"encodingFormat",
|
|
16106
|
+
"encoding_format",
|
|
16107
|
+
"frequencyPenalty",
|
|
16108
|
+
"frequency_penalty",
|
|
16109
|
+
"maxTokens",
|
|
16110
|
+
"max_tokens",
|
|
16111
|
+
"model",
|
|
16112
|
+
"n",
|
|
16113
|
+
"presencePenalty",
|
|
16114
|
+
"presence_penalty",
|
|
16115
|
+
"randomSeed",
|
|
16116
|
+
"random_seed",
|
|
16117
|
+
"responseFormat",
|
|
16118
|
+
"response_format",
|
|
16119
|
+
"safePrompt",
|
|
16120
|
+
"safe_prompt",
|
|
16121
|
+
"stream",
|
|
16122
|
+
"stop",
|
|
16123
|
+
"temperature",
|
|
16124
|
+
"toolChoice",
|
|
16125
|
+
"tool_choice",
|
|
16126
|
+
"topP",
|
|
16127
|
+
"top_p"
|
|
16128
|
+
]);
|
|
16129
|
+
var MISTRAL_RESPONSE_METADATA_ALLOWLIST = /* @__PURE__ */ new Set([
|
|
16130
|
+
"agentId",
|
|
16131
|
+
"agent_id",
|
|
16132
|
+
"created",
|
|
16133
|
+
"id",
|
|
16134
|
+
"model",
|
|
16135
|
+
"object"
|
|
16136
|
+
]);
|
|
16137
|
+
function camelToSnake3(value) {
|
|
16138
|
+
return value.replace(/[A-Z]/g, (match) => `_${match.toLowerCase()}`);
|
|
16139
|
+
}
|
|
16140
|
+
function normalizeArgs3(args) {
|
|
16141
|
+
if (Array.isArray(args)) {
|
|
16142
|
+
return args;
|
|
16143
|
+
}
|
|
16144
|
+
if (isArrayLike3(args)) {
|
|
16145
|
+
return Array.from(args);
|
|
16146
|
+
}
|
|
16147
|
+
return [args];
|
|
16148
|
+
}
|
|
16149
|
+
function isArrayLike3(value) {
|
|
16150
|
+
return isObject(value) && "length" in value && typeof value.length === "number" && Number.isInteger(value.length) && value.length >= 0;
|
|
16151
|
+
}
|
|
16152
|
+
function getMistralRequestArg(args) {
|
|
16153
|
+
const firstObjectArg = normalizeArgs3(args).find((arg) => isObject(arg));
|
|
16154
|
+
return isObject(firstObjectArg) ? firstObjectArg : void 0;
|
|
16155
|
+
}
|
|
16156
|
+
function addMistralProviderMetadata(metadata) {
|
|
16157
|
+
return {
|
|
16158
|
+
...metadata,
|
|
16159
|
+
provider: "mistral"
|
|
16160
|
+
};
|
|
16161
|
+
}
|
|
16162
|
+
function pickAllowedMetadata(metadata, allowlist) {
|
|
16163
|
+
if (!metadata) {
|
|
16164
|
+
return {};
|
|
16165
|
+
}
|
|
16166
|
+
const picked = {};
|
|
16167
|
+
for (const key of allowlist) {
|
|
16168
|
+
const value = metadata[key];
|
|
16169
|
+
if (value !== void 0) {
|
|
16170
|
+
picked[key] = value;
|
|
16171
|
+
}
|
|
16172
|
+
}
|
|
16173
|
+
return picked;
|
|
16174
|
+
}
|
|
16175
|
+
function extractMistralRequestMetadata(metadata) {
|
|
16176
|
+
return pickAllowedMetadata(metadata, MISTRAL_REQUEST_METADATA_ALLOWLIST);
|
|
16177
|
+
}
|
|
16178
|
+
function isMistralChatCompletionChunk(value) {
|
|
16179
|
+
return isObject(value);
|
|
16180
|
+
}
|
|
16181
|
+
function isMistralChunkChoice(value) {
|
|
16182
|
+
return isObject(value);
|
|
16183
|
+
}
|
|
16184
|
+
function extractMessagesInputWithMetadata(args) {
|
|
16185
|
+
const params = getMistralRequestArg(args);
|
|
16186
|
+
const { messages, ...rawMetadata } = params || {};
|
|
16187
|
+
return {
|
|
16188
|
+
input: processInputAttachments(messages),
|
|
16189
|
+
metadata: addMistralProviderMetadata(
|
|
16190
|
+
extractMistralRequestMetadata(rawMetadata)
|
|
16191
|
+
)
|
|
16192
|
+
};
|
|
16193
|
+
}
|
|
16194
|
+
function extractEmbeddingInputWithMetadata(args) {
|
|
16195
|
+
const params = getMistralRequestArg(args);
|
|
16196
|
+
const { inputs, ...rawMetadata } = params || {};
|
|
16197
|
+
return {
|
|
16198
|
+
input: inputs,
|
|
16199
|
+
metadata: addMistralProviderMetadata(
|
|
16200
|
+
extractMistralRequestMetadata(rawMetadata)
|
|
16201
|
+
)
|
|
16202
|
+
};
|
|
16203
|
+
}
|
|
16204
|
+
function extractPromptInputWithMetadata(args) {
|
|
16205
|
+
const params = getMistralRequestArg(args);
|
|
16206
|
+
const { prompt, ...rawMetadata } = params || {};
|
|
16207
|
+
return {
|
|
16208
|
+
input: prompt,
|
|
16209
|
+
metadata: addMistralProviderMetadata(
|
|
16210
|
+
extractMistralRequestMetadata(rawMetadata)
|
|
16211
|
+
)
|
|
16212
|
+
};
|
|
16213
|
+
}
|
|
16214
|
+
function extractMistralResponseMetadata(result) {
|
|
16215
|
+
if (!isObject(result)) {
|
|
16216
|
+
return void 0;
|
|
16217
|
+
}
|
|
16218
|
+
const { choices: _choices, usage: _usage, data: _data, ...metadata } = result;
|
|
16219
|
+
const picked = pickAllowedMetadata(
|
|
16220
|
+
metadata,
|
|
16221
|
+
MISTRAL_RESPONSE_METADATA_ALLOWLIST
|
|
16222
|
+
);
|
|
16223
|
+
return Object.keys(picked).length > 0 ? picked : void 0;
|
|
16224
|
+
}
|
|
16225
|
+
function extractMistralMetrics(usage, startTime) {
|
|
16226
|
+
const metrics = parseMistralMetricsFromUsage(usage);
|
|
16227
|
+
if (startTime) {
|
|
16228
|
+
metrics.time_to_first_token = getCurrentUnixTimestamp() - startTime;
|
|
16229
|
+
}
|
|
16230
|
+
return metrics;
|
|
16231
|
+
}
|
|
16232
|
+
function extractMistralStreamOutput(result) {
|
|
16233
|
+
return isObject(result) ? result.choices : void 0;
|
|
16234
|
+
}
|
|
16235
|
+
function extractMistralStreamingMetrics(result, startTime) {
|
|
16236
|
+
const metrics = isObject(result) ? parseMistralMetricsFromUsage(result.usage) : {};
|
|
16237
|
+
if (startTime) {
|
|
16238
|
+
metrics.time_to_first_token = getCurrentUnixTimestamp() - startTime;
|
|
16239
|
+
}
|
|
16240
|
+
return metrics;
|
|
16241
|
+
}
|
|
16242
|
+
function extractDeltaText(content) {
|
|
16243
|
+
if (typeof content === "string") {
|
|
16244
|
+
return content;
|
|
16245
|
+
}
|
|
16246
|
+
if (!Array.isArray(content)) {
|
|
16247
|
+
return void 0;
|
|
16248
|
+
}
|
|
16249
|
+
const textParts = content.map((part) => {
|
|
16250
|
+
if (!isObject(part) || part.type !== "text") {
|
|
16251
|
+
return "";
|
|
16252
|
+
}
|
|
16253
|
+
return typeof part.text === "string" ? part.text : "";
|
|
16254
|
+
}).filter((part) => part.length > 0);
|
|
16255
|
+
return textParts.length > 0 ? textParts.join("") : void 0;
|
|
16256
|
+
}
|
|
16257
|
+
function getDeltaToolCalls(delta) {
|
|
16258
|
+
const toolCalls = Array.isArray(delta.toolCalls) && delta.toolCalls || Array.isArray(delta.tool_calls) && delta.tool_calls || [];
|
|
16259
|
+
return toolCalls.filter((toolCall) => isObject(toolCall));
|
|
16260
|
+
}
|
|
16261
|
+
function getToolCallIndex(toolCall) {
|
|
16262
|
+
return typeof toolCall.index === "number" && toolCall.index >= 0 ? toolCall.index : void 0;
|
|
16263
|
+
}
|
|
16264
|
+
function createMergedToolCallDelta(delta) {
|
|
16265
|
+
return {
|
|
16266
|
+
...delta,
|
|
16267
|
+
function: {
|
|
16268
|
+
...delta.function,
|
|
16269
|
+
arguments: typeof _optionalChain([delta, 'access', _332 => _332.function, 'optionalAccess', _333 => _333.arguments]) === "string" ? delta.function.arguments : ""
|
|
16270
|
+
}
|
|
16271
|
+
};
|
|
16272
|
+
}
|
|
16273
|
+
function mergeToolCallDeltaPair(current, delta) {
|
|
16274
|
+
const currentArguments = typeof _optionalChain([current, 'access', _334 => _334.function, 'optionalAccess', _335 => _335.arguments]) === "string" ? current.function.arguments : "";
|
|
16275
|
+
const deltaArguments = typeof _optionalChain([delta, 'access', _336 => _336.function, 'optionalAccess', _337 => _337.arguments]) === "string" ? delta.function.arguments : "";
|
|
16276
|
+
return {
|
|
16277
|
+
...current,
|
|
16278
|
+
...delta,
|
|
16279
|
+
function: {
|
|
16280
|
+
...current.function || {},
|
|
16281
|
+
...delta.function || {},
|
|
16282
|
+
arguments: `${currentArguments}${deltaArguments}`
|
|
16283
|
+
}
|
|
16284
|
+
};
|
|
16285
|
+
}
|
|
16286
|
+
function mergeToolCallDeltas(toolCalls, deltas) {
|
|
16287
|
+
if (deltas.length === 0) {
|
|
16288
|
+
return toolCalls;
|
|
16289
|
+
}
|
|
16290
|
+
const merged = toolCalls ? [...toolCalls] : [];
|
|
16291
|
+
const indexToPosition = /* @__PURE__ */ new Map();
|
|
16292
|
+
const idToPosition = /* @__PURE__ */ new Map();
|
|
16293
|
+
for (const [position, toolCall] of merged.entries()) {
|
|
16294
|
+
const index = getToolCallIndex(toolCall);
|
|
16295
|
+
if (index !== void 0 && !indexToPosition.has(index)) {
|
|
16296
|
+
indexToPosition.set(index, position);
|
|
16297
|
+
}
|
|
16298
|
+
if (typeof toolCall.id === "string" && !idToPosition.has(toolCall.id)) {
|
|
16299
|
+
idToPosition.set(toolCall.id, position);
|
|
16300
|
+
}
|
|
16301
|
+
}
|
|
16302
|
+
for (const delta of deltas) {
|
|
16303
|
+
const deltaIndex = getToolCallIndex(delta);
|
|
16304
|
+
const existingByIndex = deltaIndex !== void 0 ? indexToPosition.get(deltaIndex) : void 0;
|
|
16305
|
+
const existingById = typeof delta.id === "string" ? idToPosition.get(delta.id) : void 0;
|
|
16306
|
+
const existingPosition = _nullishCoalesce(existingByIndex, () => ( existingById));
|
|
16307
|
+
if (existingPosition === void 0) {
|
|
16308
|
+
const newToolCall = createMergedToolCallDelta(delta);
|
|
16309
|
+
merged.push(newToolCall);
|
|
16310
|
+
const newPosition = merged.length - 1;
|
|
16311
|
+
const newIndex = getToolCallIndex(newToolCall);
|
|
16312
|
+
if (newIndex !== void 0 && !indexToPosition.has(newIndex)) {
|
|
16313
|
+
indexToPosition.set(newIndex, newPosition);
|
|
16314
|
+
}
|
|
16315
|
+
if (typeof newToolCall.id === "string" && !idToPosition.has(newToolCall.id)) {
|
|
16316
|
+
idToPosition.set(newToolCall.id, newPosition);
|
|
16317
|
+
}
|
|
16318
|
+
continue;
|
|
16319
|
+
}
|
|
16320
|
+
const mergedToolCall = mergeToolCallDeltaPair(
|
|
16321
|
+
merged[existingPosition],
|
|
16322
|
+
delta
|
|
16323
|
+
);
|
|
16324
|
+
merged[existingPosition] = mergedToolCall;
|
|
16325
|
+
const mergedIndex = getToolCallIndex(mergedToolCall);
|
|
16326
|
+
if (mergedIndex !== void 0 && !indexToPosition.has(mergedIndex)) {
|
|
16327
|
+
indexToPosition.set(mergedIndex, existingPosition);
|
|
16328
|
+
}
|
|
16329
|
+
if (typeof mergedToolCall.id === "string" && !idToPosition.has(mergedToolCall.id)) {
|
|
16330
|
+
idToPosition.set(mergedToolCall.id, existingPosition);
|
|
16331
|
+
}
|
|
16332
|
+
}
|
|
16333
|
+
return merged.length > 0 ? merged : void 0;
|
|
16334
|
+
}
|
|
16335
|
+
function getChoiceFinishReason(choice) {
|
|
16336
|
+
if (typeof choice.finishReason === "string" || choice.finishReason === null) {
|
|
16337
|
+
return choice.finishReason;
|
|
16338
|
+
}
|
|
16339
|
+
if (typeof choice.finish_reason === "string" || choice.finish_reason === null) {
|
|
16340
|
+
return choice.finish_reason;
|
|
16341
|
+
}
|
|
16342
|
+
return void 0;
|
|
16343
|
+
}
|
|
16344
|
+
function parseMistralMetricsFromUsage(usage) {
|
|
16345
|
+
if (!isObject(usage)) {
|
|
16346
|
+
return {};
|
|
16347
|
+
}
|
|
16348
|
+
const metrics = {};
|
|
16349
|
+
for (const [name, value] of Object.entries(usage)) {
|
|
16350
|
+
if (typeof value === "number") {
|
|
16351
|
+
metrics[TOKEN_NAME_MAP4[name] || camelToSnake3(name)] = value;
|
|
16352
|
+
continue;
|
|
16353
|
+
}
|
|
16354
|
+
if (!isObject(value)) {
|
|
16355
|
+
continue;
|
|
16356
|
+
}
|
|
16357
|
+
const prefix = TOKEN_DETAIL_PREFIX_MAP3[name];
|
|
16358
|
+
if (!prefix) {
|
|
16359
|
+
continue;
|
|
16360
|
+
}
|
|
16361
|
+
for (const [nestedName, nestedValue] of Object.entries(value)) {
|
|
16362
|
+
if (typeof nestedValue !== "number") {
|
|
16363
|
+
continue;
|
|
16364
|
+
}
|
|
16365
|
+
metrics[`${prefix}_${camelToSnake3(nestedName)}`] = nestedValue;
|
|
16366
|
+
}
|
|
16367
|
+
}
|
|
16368
|
+
return metrics;
|
|
16369
|
+
}
|
|
16370
|
+
function aggregateMistralStreamChunks(chunks) {
|
|
16371
|
+
const choiceAccumulators = /* @__PURE__ */ new Map();
|
|
16372
|
+
const indexToAccumulatorKey = /* @__PURE__ */ new Map();
|
|
16373
|
+
const positionToAccumulatorKey = /* @__PURE__ */ new Map();
|
|
16374
|
+
let nextAccumulatorOrder = 0;
|
|
16375
|
+
let metrics = {};
|
|
16376
|
+
let metadata;
|
|
16377
|
+
for (const event of chunks) {
|
|
16378
|
+
const chunk = isMistralChatCompletionChunk(_optionalChain([event, 'optionalAccess', _338 => _338.data])) ? event.data : void 0;
|
|
16379
|
+
if (!chunk) {
|
|
16380
|
+
continue;
|
|
16381
|
+
}
|
|
16382
|
+
if (isObject(chunk.usage)) {
|
|
16383
|
+
metrics = {
|
|
16384
|
+
...metrics,
|
|
16385
|
+
...parseMistralMetricsFromUsage(chunk.usage)
|
|
16386
|
+
};
|
|
16387
|
+
}
|
|
16388
|
+
const chunkMetadata = extractMistralResponseMetadata(chunk);
|
|
16389
|
+
if (chunkMetadata) {
|
|
16390
|
+
metadata = { ...metadata || {}, ...chunkMetadata };
|
|
16391
|
+
}
|
|
16392
|
+
for (const [choicePosition, rawChoice] of (chunk.choices || []).entries()) {
|
|
16393
|
+
if (!isMistralChunkChoice(rawChoice)) {
|
|
16394
|
+
continue;
|
|
16395
|
+
}
|
|
16396
|
+
const choice = rawChoice;
|
|
16397
|
+
const choiceIndex = typeof choice.index === "number" && choice.index >= 0 ? choice.index : void 0;
|
|
16398
|
+
let accumulatorKey = choiceIndex !== void 0 ? indexToAccumulatorKey.get(choiceIndex) : void 0;
|
|
16399
|
+
if (!accumulatorKey) {
|
|
16400
|
+
accumulatorKey = positionToAccumulatorKey.get(choicePosition);
|
|
16401
|
+
}
|
|
16402
|
+
if (!accumulatorKey) {
|
|
16403
|
+
const initialIndex = _nullishCoalesce(choiceIndex, () => ( choicePosition));
|
|
16404
|
+
const keyPrefix = choiceIndex !== void 0 ? "index" : "position";
|
|
16405
|
+
accumulatorKey = `${keyPrefix}:${initialIndex}`;
|
|
16406
|
+
choiceAccumulators.set(accumulatorKey, {
|
|
16407
|
+
index: initialIndex,
|
|
16408
|
+
order: nextAccumulatorOrder++
|
|
16409
|
+
});
|
|
16410
|
+
}
|
|
16411
|
+
const accumulator = choiceAccumulators.get(accumulatorKey);
|
|
16412
|
+
if (!accumulator) {
|
|
16413
|
+
continue;
|
|
16414
|
+
}
|
|
16415
|
+
if (choiceIndex !== void 0) {
|
|
16416
|
+
accumulator.index = choiceIndex;
|
|
16417
|
+
indexToAccumulatorKey.set(choiceIndex, accumulatorKey);
|
|
16418
|
+
}
|
|
16419
|
+
positionToAccumulatorKey.set(choicePosition, accumulatorKey);
|
|
16420
|
+
const delta = isObject(choice.delta) ? choice.delta : void 0;
|
|
16421
|
+
if (delta) {
|
|
16422
|
+
if (!accumulator.role && typeof delta.role === "string") {
|
|
16423
|
+
accumulator.role = delta.role;
|
|
16424
|
+
}
|
|
16425
|
+
const deltaText = extractDeltaText(delta.content);
|
|
16426
|
+
if (deltaText) {
|
|
16427
|
+
accumulator.content = `${accumulator.content || ""}${deltaText}`;
|
|
16428
|
+
}
|
|
16429
|
+
accumulator.toolCalls = mergeToolCallDeltas(
|
|
16430
|
+
accumulator.toolCalls,
|
|
16431
|
+
getDeltaToolCalls(delta)
|
|
16432
|
+
);
|
|
16433
|
+
}
|
|
16434
|
+
const choiceFinishReason = getChoiceFinishReason(choice);
|
|
16435
|
+
if (choiceFinishReason !== void 0) {
|
|
16436
|
+
accumulator.finishReason = choiceFinishReason;
|
|
16437
|
+
}
|
|
16438
|
+
}
|
|
16439
|
+
}
|
|
16440
|
+
const output = Array.from(choiceAccumulators.values()).sort(
|
|
16441
|
+
(left, right) => left.index === right.index ? left.order - right.order : left.index - right.index
|
|
16442
|
+
).map((choice) => ({
|
|
16443
|
+
index: choice.index,
|
|
16444
|
+
message: {
|
|
16445
|
+
...choice.role ? { role: choice.role } : {},
|
|
16446
|
+
content: _nullishCoalesce(choice.content, () => ( null)),
|
|
16447
|
+
...choice.toolCalls ? { toolCalls: choice.toolCalls } : {}
|
|
16448
|
+
},
|
|
16449
|
+
...choice.finishReason !== void 0 ? { finishReason: choice.finishReason } : {}
|
|
16450
|
+
}));
|
|
16451
|
+
return {
|
|
16452
|
+
output,
|
|
16453
|
+
metrics,
|
|
16454
|
+
...metadata ? { metadata } : {}
|
|
16455
|
+
};
|
|
16456
|
+
}
|
|
16457
|
+
|
|
14476
16458
|
// src/instrumentation/braintrust-plugin.ts
|
|
14477
16459
|
var BraintrustPlugin = (_class18 = class extends BasePlugin {
|
|
14478
16460
|
|
|
@@ -14482,8 +16464,10 @@ var BraintrustPlugin = (_class18 = class extends BasePlugin {
|
|
|
14482
16464
|
__init67() {this.claudeAgentSDKPlugin = null}
|
|
14483
16465
|
__init68() {this.googleGenAIPlugin = null}
|
|
14484
16466
|
__init69() {this.openRouterPlugin = null}
|
|
16467
|
+
__init70() {this.openRouterAgentPlugin = null}
|
|
16468
|
+
__init71() {this.mistralPlugin = null}
|
|
14485
16469
|
constructor(config = {}) {
|
|
14486
|
-
super();_class18.prototype.__init64.call(this);_class18.prototype.__init65.call(this);_class18.prototype.__init66.call(this);_class18.prototype.__init67.call(this);_class18.prototype.__init68.call(this);_class18.prototype.__init69.call(this);;
|
|
16470
|
+
super();_class18.prototype.__init64.call(this);_class18.prototype.__init65.call(this);_class18.prototype.__init66.call(this);_class18.prototype.__init67.call(this);_class18.prototype.__init68.call(this);_class18.prototype.__init69.call(this);_class18.prototype.__init70.call(this);_class18.prototype.__init71.call(this);;
|
|
14487
16471
|
this.config = config;
|
|
14488
16472
|
}
|
|
14489
16473
|
onEnable() {
|
|
@@ -14512,6 +16496,14 @@ var BraintrustPlugin = (_class18 = class extends BasePlugin {
|
|
|
14512
16496
|
this.openRouterPlugin = new OpenRouterPlugin();
|
|
14513
16497
|
this.openRouterPlugin.enable();
|
|
14514
16498
|
}
|
|
16499
|
+
if (integrations.openrouterAgent !== false) {
|
|
16500
|
+
this.openRouterAgentPlugin = new OpenRouterAgentPlugin();
|
|
16501
|
+
this.openRouterAgentPlugin.enable();
|
|
16502
|
+
}
|
|
16503
|
+
if (integrations.mistral !== false) {
|
|
16504
|
+
this.mistralPlugin = new MistralPlugin();
|
|
16505
|
+
this.mistralPlugin.enable();
|
|
16506
|
+
}
|
|
14515
16507
|
}
|
|
14516
16508
|
onDisable() {
|
|
14517
16509
|
if (this.openaiPlugin) {
|
|
@@ -14538,14 +16530,22 @@ var BraintrustPlugin = (_class18 = class extends BasePlugin {
|
|
|
14538
16530
|
this.openRouterPlugin.disable();
|
|
14539
16531
|
this.openRouterPlugin = null;
|
|
14540
16532
|
}
|
|
16533
|
+
if (this.openRouterAgentPlugin) {
|
|
16534
|
+
this.openRouterAgentPlugin.disable();
|
|
16535
|
+
this.openRouterAgentPlugin = null;
|
|
16536
|
+
}
|
|
16537
|
+
if (this.mistralPlugin) {
|
|
16538
|
+
this.mistralPlugin.disable();
|
|
16539
|
+
this.mistralPlugin = null;
|
|
16540
|
+
}
|
|
14541
16541
|
}
|
|
14542
16542
|
}, _class18);
|
|
14543
16543
|
|
|
14544
16544
|
// src/instrumentation/registry.ts
|
|
14545
|
-
var PluginRegistry = (_class19 = class {constructor() { _class19.prototype.
|
|
14546
|
-
|
|
14547
|
-
|
|
14548
|
-
|
|
16545
|
+
var PluginRegistry = (_class19 = class {constructor() { _class19.prototype.__init72.call(this);_class19.prototype.__init73.call(this);_class19.prototype.__init74.call(this); }
|
|
16546
|
+
__init72() {this.braintrustPlugin = null}
|
|
16547
|
+
__init73() {this.config = {}}
|
|
16548
|
+
__init74() {this.enabled = false}
|
|
14549
16549
|
/**
|
|
14550
16550
|
* Configure which integrations should be enabled.
|
|
14551
16551
|
* This must be called before any SDK imports to take effect.
|
|
@@ -14610,7 +16610,9 @@ var PluginRegistry = (_class19 = class {constructor() { _class19.prototype.__ini
|
|
|
14610
16610
|
aisdk: true,
|
|
14611
16611
|
google: true,
|
|
14612
16612
|
claudeAgentSDK: true,
|
|
14613
|
-
openrouter: true
|
|
16613
|
+
openrouter: true,
|
|
16614
|
+
openrouterAgent: true,
|
|
16615
|
+
mistral: true
|
|
14614
16616
|
};
|
|
14615
16617
|
}
|
|
14616
16618
|
/**
|
|
@@ -14745,7 +16747,7 @@ function isAsync(fn) {
|
|
|
14745
16747
|
function isAsyncGenerator2(fn) {
|
|
14746
16748
|
return fn[Symbol.toStringTag] === "AsyncGenerator";
|
|
14747
16749
|
}
|
|
14748
|
-
function
|
|
16750
|
+
function isAsyncIterable5(obj) {
|
|
14749
16751
|
return typeof obj[Symbol.asyncIterator] === "function";
|
|
14750
16752
|
}
|
|
14751
16753
|
function wrapAsync(asyncFn) {
|
|
@@ -14795,7 +16797,7 @@ function _asyncMap(eachfn, arr, iteratee, callback) {
|
|
|
14795
16797
|
callback(err, results);
|
|
14796
16798
|
});
|
|
14797
16799
|
}
|
|
14798
|
-
function
|
|
16800
|
+
function isArrayLike4(value) {
|
|
14799
16801
|
return value && typeof value.length === "number" && value.length >= 0 && value.length % 1 === 0;
|
|
14800
16802
|
}
|
|
14801
16803
|
var breakLoop = {};
|
|
@@ -14843,7 +16845,7 @@ function createObjectIterator(obj) {
|
|
|
14843
16845
|
};
|
|
14844
16846
|
}
|
|
14845
16847
|
function createIterator(coll) {
|
|
14846
|
-
if (
|
|
16848
|
+
if (isArrayLike4(coll)) {
|
|
14847
16849
|
return createArrayIterator(coll);
|
|
14848
16850
|
}
|
|
14849
16851
|
var iterator = getIterator(coll);
|
|
@@ -14917,7 +16919,7 @@ var eachOfLimit$2 = (limit) => {
|
|
|
14917
16919
|
if (isAsyncGenerator2(obj)) {
|
|
14918
16920
|
return asyncEachOfLimit(obj, limit, iteratee, callback);
|
|
14919
16921
|
}
|
|
14920
|
-
if (
|
|
16922
|
+
if (isAsyncIterable5(obj)) {
|
|
14921
16923
|
return asyncEachOfLimit(obj[Symbol.asyncIterator](), limit, iteratee, callback);
|
|
14922
16924
|
}
|
|
14923
16925
|
var nextElem = createIterator(obj);
|
|
@@ -14989,7 +16991,7 @@ function eachOfGeneric(coll, iteratee, callback) {
|
|
|
14989
16991
|
return eachOfLimit$1(coll, Infinity, iteratee, callback);
|
|
14990
16992
|
}
|
|
14991
16993
|
function eachOf(coll, iteratee, callback) {
|
|
14992
|
-
var eachOfImplementation =
|
|
16994
|
+
var eachOfImplementation = isArrayLike4(coll) ? eachOfArrayLike : eachOfGeneric;
|
|
14993
16995
|
return eachOfImplementation(coll, wrapAsync(iteratee), callback);
|
|
14994
16996
|
}
|
|
14995
16997
|
var eachOf$1 = awaitify(eachOf, 3);
|
|
@@ -15504,7 +17506,7 @@ function filterGeneric(eachfn, coll, iteratee, callback) {
|
|
|
15504
17506
|
});
|
|
15505
17507
|
}
|
|
15506
17508
|
function _filter(eachfn, coll, iteratee, callback) {
|
|
15507
|
-
var filter2 =
|
|
17509
|
+
var filter2 = isArrayLike4(coll) ? filterArray : filterGeneric;
|
|
15508
17510
|
return filter2(eachfn, coll, wrapAsync(iteratee), callback);
|
|
15509
17511
|
}
|
|
15510
17512
|
function filter(coll, iteratee, callback) {
|
|
@@ -15579,7 +17581,7 @@ if (hasNextTick) {
|
|
|
15579
17581
|
}
|
|
15580
17582
|
var nextTick = wrap(_defer);
|
|
15581
17583
|
var _parallel = awaitify((eachfn, tasks, callback) => {
|
|
15582
|
-
var results =
|
|
17584
|
+
var results = isArrayLike4(tasks) ? [] : {};
|
|
15583
17585
|
eachfn(tasks, (task, key, taskCb) => {
|
|
15584
17586
|
wrapAsync(task)((err, ...result) => {
|
|
15585
17587
|
if (result.length < 2) {
|
|
@@ -15846,10 +17848,10 @@ var SpanFetcher = class _SpanFetcher extends ObjectFetcher {
|
|
|
15846
17848
|
}
|
|
15847
17849
|
};
|
|
15848
17850
|
var CachedSpanFetcher = (_class20 = class {
|
|
15849
|
-
|
|
15850
|
-
|
|
17851
|
+
__init75() {this.spanCache = /* @__PURE__ */ new Map()}
|
|
17852
|
+
__init76() {this.allFetched = false}
|
|
15851
17853
|
|
|
15852
|
-
constructor(objectTypeOrFetchFn, objectId, rootSpanId, getState) {;_class20.prototype.
|
|
17854
|
+
constructor(objectTypeOrFetchFn, objectId, rootSpanId, getState) {;_class20.prototype.__init75.call(this);_class20.prototype.__init76.call(this);
|
|
15853
17855
|
if (typeof objectTypeOrFetchFn === "function") {
|
|
15854
17856
|
this.fetchFn = objectTypeOrFetchFn;
|
|
15855
17857
|
} else {
|
|
@@ -15864,7 +17866,7 @@ var CachedSpanFetcher = (_class20 = class {
|
|
|
15864
17866
|
spanType
|
|
15865
17867
|
);
|
|
15866
17868
|
const rows = await fetcher.fetchedData();
|
|
15867
|
-
return rows.filter((row) => _optionalChain([row, 'access',
|
|
17869
|
+
return rows.filter((row) => _optionalChain([row, 'access', _339 => _339.span_attributes, 'optionalAccess', _340 => _340.purpose]) !== "scorer").map((row) => ({
|
|
15868
17870
|
input: row.input,
|
|
15869
17871
|
output: row.output,
|
|
15870
17872
|
metadata: row.metadata,
|
|
@@ -15898,7 +17900,7 @@ var CachedSpanFetcher = (_class20 = class {
|
|
|
15898
17900
|
async fetchSpans(spanType) {
|
|
15899
17901
|
const spans = await this.fetchFn(spanType);
|
|
15900
17902
|
for (const span of spans) {
|
|
15901
|
-
const type = _nullishCoalesce(_optionalChain([span, 'access',
|
|
17903
|
+
const type = _nullishCoalesce(_optionalChain([span, 'access', _341 => _341.span_attributes, 'optionalAccess', _342 => _342.type]), () => ( ""));
|
|
15902
17904
|
const existing = _nullishCoalesce(this.spanCache.get(type), () => ( []));
|
|
15903
17905
|
existing.push(span);
|
|
15904
17906
|
this.spanCache.set(type, existing);
|
|
@@ -15922,17 +17924,17 @@ var LocalTrace = (_class21 = class {
|
|
|
15922
17924
|
|
|
15923
17925
|
|
|
15924
17926
|
|
|
15925
|
-
|
|
15926
|
-
|
|
17927
|
+
__init77() {this.spansFlushed = false}
|
|
17928
|
+
__init78() {this.spansFlushPromise = null}
|
|
15927
17929
|
|
|
15928
|
-
|
|
17930
|
+
__init79() {this.threadCache = /* @__PURE__ */ new Map()}
|
|
15929
17931
|
constructor({
|
|
15930
17932
|
objectType,
|
|
15931
17933
|
objectId,
|
|
15932
17934
|
rootSpanId,
|
|
15933
17935
|
ensureSpansFlushed,
|
|
15934
17936
|
state
|
|
15935
|
-
}) {;_class21.prototype.
|
|
17937
|
+
}) {;_class21.prototype.__init77.call(this);_class21.prototype.__init78.call(this);_class21.prototype.__init79.call(this);
|
|
15936
17938
|
this.objectType = objectType;
|
|
15937
17939
|
this.objectId = objectId;
|
|
15938
17940
|
this.rootSpanId = rootSpanId;
|
|
@@ -15978,11 +17980,11 @@ var LocalTrace = (_class21 = class {
|
|
|
15978
17980
|
const cachedSpans = this.state.spanCache.getByRootSpanId(this.rootSpanId);
|
|
15979
17981
|
if (cachedSpans && cachedSpans.length > 0) {
|
|
15980
17982
|
let spans = cachedSpans.filter(
|
|
15981
|
-
(span) => _optionalChain([span, 'access',
|
|
17983
|
+
(span) => _optionalChain([span, 'access', _343 => _343.span_attributes, 'optionalAccess', _344 => _344.purpose]) !== "scorer"
|
|
15982
17984
|
);
|
|
15983
17985
|
if (spanType && spanType.length > 0) {
|
|
15984
17986
|
spans = spans.filter(
|
|
15985
|
-
(span) => spanType.includes(_nullishCoalesce(_optionalChain([span, 'access',
|
|
17987
|
+
(span) => spanType.includes(_nullishCoalesce(_optionalChain([span, 'access', _345 => _345.span_attributes, 'optionalAccess', _346 => _346.type]), () => ( "")))
|
|
15986
17988
|
);
|
|
15987
17989
|
}
|
|
15988
17990
|
return spans.map((span) => ({
|
|
@@ -16001,7 +18003,7 @@ var LocalTrace = (_class21 = class {
|
|
|
16001
18003
|
* Calls the API with the project_default preprocessor (which falls back to "thread").
|
|
16002
18004
|
*/
|
|
16003
18005
|
async getThread(options) {
|
|
16004
|
-
const cacheKey = _nullishCoalesce(_optionalChain([options, 'optionalAccess',
|
|
18006
|
+
const cacheKey = _nullishCoalesce(_optionalChain([options, 'optionalAccess', _347 => _347.preprocessor]), () => ( "project_default"));
|
|
16005
18007
|
if (!this.threadCache.has(cacheKey)) {
|
|
16006
18008
|
const promise = this.fetchThread(options);
|
|
16007
18009
|
this.threadCache.set(cacheKey, promise);
|
|
@@ -16012,7 +18014,7 @@ var LocalTrace = (_class21 = class {
|
|
|
16012
18014
|
await this.ensureSpansReady();
|
|
16013
18015
|
await this.state.login({});
|
|
16014
18016
|
const result = await invoke({
|
|
16015
|
-
globalFunction: _nullishCoalesce(_optionalChain([options, 'optionalAccess',
|
|
18017
|
+
globalFunction: _nullishCoalesce(_optionalChain([options, 'optionalAccess', _348 => _348.preprocessor]), () => ( "project_default")),
|
|
16016
18018
|
functionType: "preprocessor",
|
|
16017
18019
|
input: {
|
|
16018
18020
|
trace_ref: {
|
|
@@ -16188,10 +18190,10 @@ function validateParametersWithJsonSchema(parameters, schema) {
|
|
|
16188
18190
|
const ajv = new (0, _ajv2.default)({ coerceTypes: true, useDefaults: true, strict: false });
|
|
16189
18191
|
const validate = ajv.compile(schema);
|
|
16190
18192
|
if (!validate(parameters)) {
|
|
16191
|
-
const errorMessages = _optionalChain([validate, 'access',
|
|
18193
|
+
const errorMessages = _optionalChain([validate, 'access', _349 => _349.errors, 'optionalAccess', _350 => _350.map, 'call', _351 => _351((err) => {
|
|
16192
18194
|
const path2 = err.instancePath || "root";
|
|
16193
18195
|
return `${path2}: ${err.message}`;
|
|
16194
|
-
}), 'access',
|
|
18196
|
+
}), 'access', _352 => _352.join, 'call', _353 => _353(", ")]);
|
|
16195
18197
|
throw Error(`Invalid parameters: ${errorMessages}`);
|
|
16196
18198
|
}
|
|
16197
18199
|
return rehydrateRemoteParameters(parameters, schema);
|
|
@@ -16281,7 +18283,7 @@ function callEvaluatorData(data) {
|
|
|
16281
18283
|
baseExperiment
|
|
16282
18284
|
};
|
|
16283
18285
|
}
|
|
16284
|
-
function
|
|
18286
|
+
function isAsyncIterable6(value) {
|
|
16285
18287
|
return typeof value === "object" && value !== null && Symbol.asyncIterator in value && typeof value[Symbol.asyncIterator] === "function";
|
|
16286
18288
|
}
|
|
16287
18289
|
function isIterable(value) {
|
|
@@ -16450,7 +18452,7 @@ async function runEvaluator(experiment, evaluator, progressReporter, filters, st
|
|
|
16450
18452
|
}
|
|
16451
18453
|
async function runEvaluatorInternal(experiment, evaluator, progressReporter, filters, stream, parameters, collectResults, enableCache) {
|
|
16452
18454
|
if (enableCache) {
|
|
16453
|
-
_optionalChain([(_nullishCoalesce(evaluator.state, () => ( _internalGetGlobalState()))), 'optionalAccess',
|
|
18455
|
+
_optionalChain([(_nullishCoalesce(evaluator.state, () => ( _internalGetGlobalState()))), 'optionalAccess', _354 => _354.spanCache, 'optionalAccess', _355 => _355.start, 'call', _356 => _356()]);
|
|
16454
18456
|
}
|
|
16455
18457
|
try {
|
|
16456
18458
|
if (typeof evaluator.data === "string") {
|
|
@@ -16486,7 +18488,7 @@ async function runEvaluatorInternal(experiment, evaluator, progressReporter, fil
|
|
|
16486
18488
|
}
|
|
16487
18489
|
const resolvedDataResult = dataResult instanceof Promise ? await dataResult : dataResult;
|
|
16488
18490
|
const dataIterable = (() => {
|
|
16489
|
-
if (
|
|
18491
|
+
if (isAsyncIterable6(resolvedDataResult)) {
|
|
16490
18492
|
return resolvedDataResult;
|
|
16491
18493
|
}
|
|
16492
18494
|
if (Array.isArray(resolvedDataResult) || isIterable(resolvedDataResult)) {
|
|
@@ -16505,7 +18507,7 @@ async function runEvaluatorInternal(experiment, evaluator, progressReporter, fil
|
|
|
16505
18507
|
const experimentIdPromise = experiment ? (async () => {
|
|
16506
18508
|
try {
|
|
16507
18509
|
return await experiment.id;
|
|
16508
|
-
} catch (
|
|
18510
|
+
} catch (e40) {
|
|
16509
18511
|
return void 0;
|
|
16510
18512
|
}
|
|
16511
18513
|
})() : void 0;
|
|
@@ -16561,7 +18563,7 @@ async function runEvaluatorInternal(experiment, evaluator, progressReporter, fil
|
|
|
16561
18563
|
objectType: parentComponents ? spanObjectTypeV3ToTypedString(
|
|
16562
18564
|
parentComponents.data.object_type
|
|
16563
18565
|
) : "experiment",
|
|
16564
|
-
objectId: await _asyncNullishCoalesce(await _asyncOptionalChain([parentComponents, 'optionalAccess', async
|
|
18566
|
+
objectId: await _asyncNullishCoalesce(await _asyncOptionalChain([parentComponents, 'optionalAccess', async _357 => _357.data, 'access', async _358 => _358.object_id]), async () => ( (experimentIdPromise ? await _asyncNullishCoalesce(await experimentIdPromise, async () => ( "")) : ""))),
|
|
16565
18567
|
rootSpanId: rootSpan.rootSpanId,
|
|
16566
18568
|
ensureSpansFlushed,
|
|
16567
18569
|
state
|
|
@@ -16587,10 +18589,10 @@ async function runEvaluatorInternal(experiment, evaluator, progressReporter, fil
|
|
|
16587
18589
|
span,
|
|
16588
18590
|
parameters: _nullishCoalesce(parameters, () => ( {})),
|
|
16589
18591
|
reportProgress: (event) => {
|
|
16590
|
-
_optionalChain([stream, 'optionalCall',
|
|
18592
|
+
_optionalChain([stream, 'optionalCall', _359 => _359({
|
|
16591
18593
|
...event,
|
|
16592
18594
|
id: rootSpan.id,
|
|
16593
|
-
origin: _optionalChain([baseEvent, 'access',
|
|
18595
|
+
origin: _optionalChain([baseEvent, 'access', _360 => _360.event, 'optionalAccess', _361 => _361.origin]),
|
|
16594
18596
|
name: evaluator.evalName,
|
|
16595
18597
|
object_type: "task"
|
|
16596
18598
|
})]);
|
|
@@ -16754,7 +18756,7 @@ async function runEvaluatorInternal(experiment, evaluator, progressReporter, fil
|
|
|
16754
18756
|
metadata,
|
|
16755
18757
|
scores: mergedScores,
|
|
16756
18758
|
error,
|
|
16757
|
-
origin: _optionalChain([baseEvent, 'access',
|
|
18759
|
+
origin: _optionalChain([baseEvent, 'access', _362 => _362.event, 'optionalAccess', _363 => _363.origin])
|
|
16758
18760
|
});
|
|
16759
18761
|
}
|
|
16760
18762
|
};
|
|
@@ -16789,7 +18791,7 @@ async function runEvaluatorInternal(experiment, evaluator, progressReporter, fil
|
|
|
16789
18791
|
break;
|
|
16790
18792
|
}
|
|
16791
18793
|
scheduledTrials++;
|
|
16792
|
-
_optionalChain([progressReporter, 'access',
|
|
18794
|
+
_optionalChain([progressReporter, 'access', _364 => _364.setTotal, 'optionalCall', _365 => _365(evaluator.evalName, scheduledTrials)]);
|
|
16793
18795
|
q.pushAsync({ datum, trialIndex }).catch((e) => {
|
|
16794
18796
|
if (queueErrors.length < 5) {
|
|
16795
18797
|
queueErrors.push(e);
|
|
@@ -16874,9 +18876,9 @@ async function runEvaluatorInternal(experiment, evaluator, progressReporter, fil
|
|
|
16874
18876
|
);
|
|
16875
18877
|
} finally {
|
|
16876
18878
|
if (enableCache) {
|
|
16877
|
-
const spanCache = _optionalChain([(_nullishCoalesce(evaluator.state, () => ( _internalGetGlobalState()))), 'optionalAccess',
|
|
16878
|
-
_optionalChain([spanCache, 'optionalAccess',
|
|
16879
|
-
_optionalChain([spanCache, 'optionalAccess',
|
|
18879
|
+
const spanCache = _optionalChain([(_nullishCoalesce(evaluator.state, () => ( _internalGetGlobalState()))), 'optionalAccess', _366 => _366.spanCache]);
|
|
18880
|
+
_optionalChain([spanCache, 'optionalAccess', _367 => _367.dispose, 'call', _368 => _368()]);
|
|
18881
|
+
_optionalChain([spanCache, 'optionalAccess', _369 => _369.stop, 'call', _370 => _370()]);
|
|
16880
18882
|
}
|
|
16881
18883
|
}
|
|
16882
18884
|
}
|
|
@@ -17113,7 +19115,7 @@ async function cachedLogin(options) {
|
|
|
17113
19115
|
}
|
|
17114
19116
|
function makeCheckAuthorized(allowedOrgName) {
|
|
17115
19117
|
return async (req, _res, next) => {
|
|
17116
|
-
if (!_optionalChain([req, 'access',
|
|
19118
|
+
if (!_optionalChain([req, 'access', _371 => _371.ctx, 'optionalAccess', _372 => _372.token])) {
|
|
17117
19119
|
return next(_httperrors2.default.call(void 0, 401, "Unauthorized"));
|
|
17118
19120
|
}
|
|
17119
19121
|
try {
|
|
@@ -17126,7 +19128,7 @@ function makeCheckAuthorized(allowedOrgName) {
|
|
|
17126
19128
|
return next(_httperrors2.default.call(void 0, 403, errorMessage));
|
|
17127
19129
|
}
|
|
17128
19130
|
const state = await cachedLogin({
|
|
17129
|
-
apiKey: _optionalChain([req, 'access',
|
|
19131
|
+
apiKey: _optionalChain([req, 'access', _373 => _373.ctx, 'optionalAccess', _374 => _374.token]),
|
|
17130
19132
|
orgName
|
|
17131
19133
|
});
|
|
17132
19134
|
req.ctx.state = state;
|
|
@@ -17286,7 +19288,7 @@ var parametersContainerSchema = _v3.z.object({
|
|
|
17286
19288
|
var staticParametersContainerSchema = _v3.z.object({
|
|
17287
19289
|
type: _v3.z.literal("braintrust.staticParameters"),
|
|
17288
19290
|
schema: staticParametersSchema,
|
|
17289
|
-
source: _v3.z.null()
|
|
19291
|
+
source: _v3.z.null().nullish()
|
|
17290
19292
|
});
|
|
17291
19293
|
var serializedParametersContainerSchema = _v3.z.union([
|
|
17292
19294
|
parametersContainerSchema,
|
|
@@ -17323,10 +19325,10 @@ var Project2 = (_class22 = class {
|
|
|
17323
19325
|
|
|
17324
19326
|
|
|
17325
19327
|
|
|
17326
|
-
|
|
17327
|
-
|
|
17328
|
-
|
|
17329
|
-
constructor(args) {;_class22.prototype.
|
|
19328
|
+
__init80() {this._publishableCodeFunctions = []}
|
|
19329
|
+
__init81() {this._publishablePrompts = []}
|
|
19330
|
+
__init82() {this._publishableParameters = []}
|
|
19331
|
+
constructor(args) {;_class22.prototype.__init80.call(this);_class22.prototype.__init81.call(this);_class22.prototype.__init82.call(this);
|
|
17330
19332
|
_initializeSpanContext();
|
|
17331
19333
|
this.name = "name" in args ? args.name : void 0;
|
|
17332
19334
|
this.id = "id" in args ? args.id : void 0;
|
|
@@ -17380,10 +19382,10 @@ var Project2 = (_class22 = class {
|
|
|
17380
19382
|
}
|
|
17381
19383
|
}, _class22);
|
|
17382
19384
|
var ToolBuilder = (_class23 = class {
|
|
17383
|
-
constructor(project) {;_class23.prototype.
|
|
19385
|
+
constructor(project) {;_class23.prototype.__init83.call(this);
|
|
17384
19386
|
this.project = project;
|
|
17385
19387
|
}
|
|
17386
|
-
|
|
19388
|
+
__init83() {this.taskCounter = 0}
|
|
17387
19389
|
// This type definition is just a catch all so that the implementation can be
|
|
17388
19390
|
// less specific than the two more specific declarations above.
|
|
17389
19391
|
create(opts) {
|
|
@@ -17410,10 +19412,10 @@ var ToolBuilder = (_class23 = class {
|
|
|
17410
19412
|
}
|
|
17411
19413
|
}, _class23);
|
|
17412
19414
|
var ScorerBuilder = (_class24 = class {
|
|
17413
|
-
constructor(project) {;_class24.prototype.
|
|
19415
|
+
constructor(project) {;_class24.prototype.__init84.call(this);
|
|
17414
19416
|
this.project = project;
|
|
17415
19417
|
}
|
|
17416
|
-
|
|
19418
|
+
__init84() {this.taskCounter = 0}
|
|
17417
19419
|
create(opts) {
|
|
17418
19420
|
this.taskCounter++;
|
|
17419
19421
|
let resolvedName = opts.name;
|
|
@@ -17769,9 +19771,9 @@ function serializeRemoteEvalParametersContainer(parameters) {
|
|
|
17769
19771
|
source: null
|
|
17770
19772
|
};
|
|
17771
19773
|
}
|
|
17772
|
-
var ProjectNameIdMap = (_class25 = class {constructor() { _class25.prototype.
|
|
17773
|
-
|
|
17774
|
-
|
|
19774
|
+
var ProjectNameIdMap = (_class25 = class {constructor() { _class25.prototype.__init85.call(this);_class25.prototype.__init86.call(this); }
|
|
19775
|
+
__init85() {this.nameToId = {}}
|
|
19776
|
+
__init86() {this.idToName = {}}
|
|
17775
19777
|
async getId(projectName) {
|
|
17776
19778
|
if (!(projectName in this.nameToId)) {
|
|
17777
19779
|
const response = await _internalGetGlobalState().appConn().post_json("api/project/register", {
|
|
@@ -17876,7 +19878,7 @@ function runDevServer(evaluators, opts) {
|
|
|
17876
19878
|
scores,
|
|
17877
19879
|
stream
|
|
17878
19880
|
} = evalBodySchema.parse(req.body);
|
|
17879
|
-
if (!_optionalChain([req, 'access',
|
|
19881
|
+
if (!_optionalChain([req, 'access', _375 => _375.ctx, 'optionalAccess', _376 => _376.state])) {
|
|
17880
19882
|
res.status(500).json({ error: "Braintrust state not initialized in request" });
|
|
17881
19883
|
return;
|
|
17882
19884
|
}
|
|
@@ -17927,12 +19929,12 @@ function runDevServer(evaluators, opts) {
|
|
|
17927
19929
|
...evaluator,
|
|
17928
19930
|
data: evalData.data,
|
|
17929
19931
|
scores: evaluator.scores.concat(
|
|
17930
|
-
_nullishCoalesce(_optionalChain([scores, 'optionalAccess',
|
|
19932
|
+
_nullishCoalesce(_optionalChain([scores, 'optionalAccess', _377 => _377.map, 'call', _378 => _378(
|
|
17931
19933
|
(score) => makeScorer(
|
|
17932
19934
|
state,
|
|
17933
19935
|
score.name,
|
|
17934
19936
|
score.function_id,
|
|
17935
|
-
_optionalChain([req, 'access',
|
|
19937
|
+
_optionalChain([req, 'access', _379 => _379.ctx, 'optionalAccess', _380 => _380.projectId])
|
|
17936
19938
|
)
|
|
17937
19939
|
)]), () => ( []))
|
|
17938
19940
|
),
|