braintrust 3.11.0 → 3.12.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +8 -8
- package/dev/dist/index.d.mts +1 -1
- package/dev/dist/index.d.ts +1 -1
- package/dev/dist/index.js +2607 -316
- package/dev/dist/index.mjs +2388 -97
- package/dist/apply-auto-instrumentation.browser.d.mts +2 -0
- package/dist/apply-auto-instrumentation.browser.d.ts +2 -0
- package/dist/apply-auto-instrumentation.browser.js +18 -0
- package/dist/apply-auto-instrumentation.browser.mjs +0 -0
- package/dist/apply-auto-instrumentation.d.mts +2 -0
- package/dist/apply-auto-instrumentation.d.ts +2 -0
- package/dist/apply-auto-instrumentation.js +2534 -0
- package/dist/apply-auto-instrumentation.mjs +2534 -0
- package/dist/auto-instrumentations/bundler/esbuild.cjs +1802 -1283
- package/dist/auto-instrumentations/bundler/esbuild.d.mts +9 -5
- package/dist/auto-instrumentations/bundler/esbuild.d.ts +9 -5
- package/dist/auto-instrumentations/bundler/esbuild.mjs +10 -2
- package/dist/auto-instrumentations/bundler/next.cjs +3268 -0
- package/dist/auto-instrumentations/bundler/next.d.mts +3 -0
- package/dist/auto-instrumentations/bundler/next.d.ts +3 -0
- package/dist/auto-instrumentations/bundler/next.mjs +189 -0
- package/dist/auto-instrumentations/bundler/rollup.cjs +1802 -1283
- package/dist/auto-instrumentations/bundler/rollup.d.mts +9 -5
- package/dist/auto-instrumentations/bundler/rollup.d.ts +9 -5
- package/dist/auto-instrumentations/bundler/rollup.mjs +10 -2
- package/dist/auto-instrumentations/bundler/vite.cjs +1802 -1283
- package/dist/auto-instrumentations/bundler/vite.d.mts +9 -5
- package/dist/auto-instrumentations/bundler/vite.d.ts +9 -5
- package/dist/auto-instrumentations/bundler/vite.mjs +10 -2
- package/dist/auto-instrumentations/bundler/webpack-loader.cjs +1860 -1308
- package/dist/auto-instrumentations/bundler/webpack-loader.d.ts +3 -3
- package/dist/auto-instrumentations/bundler/webpack.cjs +1802 -1283
- package/dist/auto-instrumentations/bundler/webpack.d.mts +9 -5
- package/dist/auto-instrumentations/bundler/webpack.d.ts +9 -5
- package/dist/auto-instrumentations/bundler/webpack.mjs +6 -6
- package/dist/auto-instrumentations/{chunk-DIV5TO4S.mjs → chunk-2DPA74KK.mjs} +337 -1
- package/dist/auto-instrumentations/chunk-73BZUKVI.mjs +300 -0
- package/dist/auto-instrumentations/chunk-AFXRW7I7.mjs +18 -0
- package/dist/auto-instrumentations/hook.mjs +1712 -1460
- package/dist/auto-instrumentations/index.cjs +93 -0
- package/dist/auto-instrumentations/index.d.mts +5 -1
- package/dist/auto-instrumentations/index.d.ts +5 -1
- package/dist/auto-instrumentations/index.mjs +6 -247
- package/dist/auto-instrumentations/loader/esm-hook.mjs +19 -2
- package/dist/auto-instrumentations/plugin-D7nDswtC.d.mts +44 -0
- package/dist/auto-instrumentations/plugin-D7nDswtC.d.ts +44 -0
- package/dist/browser.d.mts +120 -31
- package/dist/browser.d.ts +120 -31
- package/dist/browser.js +2395 -123
- package/dist/browser.mjs +2395 -123
- package/dist/chunk-BW4DF4CY.js +816 -0
- package/dist/chunk-MSLBGITU.mjs +816 -0
- package/dist/cli.js +2407 -96
- package/dist/edge-light.d.mts +1 -1
- package/dist/edge-light.d.ts +1 -1
- package/dist/edge-light.js +2395 -123
- package/dist/edge-light.mjs +2395 -123
- package/dist/index.d.mts +120 -31
- package/dist/index.d.ts +120 -31
- package/dist/index.js +3362 -1849
- package/dist/index.mjs +2505 -992
- package/dist/instrumentation/index.d.mts +7860 -48
- package/dist/instrumentation/index.d.ts +7860 -48
- package/dist/instrumentation/index.js +2395 -95
- package/dist/instrumentation/index.mjs +2394 -95
- package/dist/workerd.d.mts +1 -1
- package/dist/workerd.d.ts +1 -1
- package/dist/workerd.js +2395 -123
- package/dist/workerd.mjs +2395 -123
- package/package.json +23 -3
- package/util/dist/index.js +6 -0
- package/util/dist/index.mjs +6 -0
- package/dist/auto-instrumentations/chunk-G6ZWXGZB.mjs +0 -116
- package/dist/auto-instrumentations/plugin-Df3qKIl2.d.mts +0 -22
- package/dist/auto-instrumentations/plugin-Df3qKIl2.d.ts +0 -22
package/dev/dist/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { newObj[key] = obj[key]; } } } newObj.default = obj; return newObj; } } function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } async function _asyncNullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return await rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; } async function _asyncOptionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = await fn(value); } else if (op === 'call' || op === 'optionalCall') { value = await fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; } var _class; var _class2; var _class3; var _class4; var _class5; var _class6; var _class7; var _class8; var _class9; var _class10; var _class11; var _class12; var _class13; var _class14; var _class15; var _class16; var _class17; var _class18; var _class19; var _class20; var _class21; var _class22; var _class23; var _class24; var _class25; var _class26; var _class27;// src/node/config.ts
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { newObj[key] = obj[key]; } } } newObj.default = obj; return newObj; } } function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } async function _asyncNullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return await rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; } async function _asyncOptionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = await fn(value); } else if (op === 'call' || op === 'optionalCall') { value = await fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; } var _class; var _class2; var _class3; var _class4; var _class5; var _class6; var _class7; var _class8; var _class9; var _class10; var _class11; var _class12; var _class13; var _class14; var _class15; var _class16; var _class17; var _class18; var _class19; var _class20; var _class21; var _class22; var _class23; var _class24; var _class25; var _class26; var _class27; var _class28; var _class29; var _class30; var _class31; var _class32;// src/node/config.ts
|
|
2
2
|
var _nodeasync_hooks = require('node:async_hooks');
|
|
3
3
|
var _nodediagnostics_channel = require('node:diagnostics_channel'); var diagnostics_channel = _interopRequireWildcard(_nodediagnostics_channel);
|
|
4
4
|
var _nodepath = require('node:path'); var path = _interopRequireWildcard(_nodepath);
|
|
@@ -406,11 +406,11 @@ function truncateToByteLimit(s, byteLimit = 65536) {
|
|
|
406
406
|
return new TextDecoder().decode(truncated);
|
|
407
407
|
}
|
|
408
408
|
async function getRepoInfo(settings) {
|
|
409
|
-
if (settings
|
|
409
|
+
if (!settings || settings.collect === "none") {
|
|
410
410
|
return void 0;
|
|
411
411
|
}
|
|
412
412
|
const repo = await repoInfo();
|
|
413
|
-
if (!repo ||
|
|
413
|
+
if (!repo || settings.collect === "all") {
|
|
414
414
|
return repo;
|
|
415
415
|
}
|
|
416
416
|
let sanitized = {};
|
|
@@ -1228,6 +1228,11 @@ function isPromiseLike(value) {
|
|
|
1228
1228
|
|
|
1229
1229
|
// util/object_util.ts
|
|
1230
1230
|
var SET_UNION_FIELDS = /* @__PURE__ */ new Set(["tags"]);
|
|
1231
|
+
var FORBIDDEN_MERGE_KEYS = /* @__PURE__ */ new Set([
|
|
1232
|
+
"__proto__",
|
|
1233
|
+
"constructor",
|
|
1234
|
+
"prototype"
|
|
1235
|
+
]);
|
|
1231
1236
|
function mergeDictsWithPaths({
|
|
1232
1237
|
mergeInto,
|
|
1233
1238
|
mergeFrom,
|
|
@@ -1250,6 +1255,7 @@ function mergeDictsWithPathsHelper({
|
|
|
1250
1255
|
mergePaths
|
|
1251
1256
|
}) {
|
|
1252
1257
|
Object.entries(mergeFrom).forEach(([k, mergeFromV]) => {
|
|
1258
|
+
if (FORBIDDEN_MERGE_KEYS.has(k)) return;
|
|
1253
1259
|
const fullPath = path2.concat([k]);
|
|
1254
1260
|
const fullPathSerialized = JSON.stringify(fullPath);
|
|
1255
1261
|
const mergeIntoV = recordFind(mergeInto, k);
|
|
@@ -5306,6 +5312,13 @@ var HTTPConnection = class _HTTPConnection {
|
|
|
5306
5312
|
debugLogger.debug(
|
|
5307
5313
|
`Retrying API request ${object_type} ${JSON.stringify(args)} ${e.status} ${e.text}`
|
|
5308
5314
|
);
|
|
5315
|
+
const sleepTimeS = HTTP_RETRY_BASE_SLEEP_TIME_S * 2 ** i;
|
|
5316
|
+
debugLogger.info(
|
|
5317
|
+
`Sleeping for ${sleepTimeS}s before retrying API request`
|
|
5318
|
+
);
|
|
5319
|
+
await new Promise(
|
|
5320
|
+
(resolve) => setTimeout(resolve, sleepTimeS * 1e3)
|
|
5321
|
+
);
|
|
5309
5322
|
continue;
|
|
5310
5323
|
}
|
|
5311
5324
|
throw e;
|
|
@@ -5828,20 +5841,7 @@ function startSpanParentArgs(args) {
|
|
|
5828
5841
|
`Mismatch between expected span parent object type ${args.parentObjectType} and provided type ${parentComponents.data.object_type}`
|
|
5829
5842
|
);
|
|
5830
5843
|
}
|
|
5831
|
-
|
|
5832
|
-
args.state,
|
|
5833
|
-
parentComponents
|
|
5834
|
-
);
|
|
5835
|
-
const computeParentObjectId = async () => {
|
|
5836
|
-
const parentComponentsObjectId = await parentComponentsObjectIdLambda();
|
|
5837
|
-
if (await args.parentObjectId.get() !== parentComponentsObjectId) {
|
|
5838
|
-
throw new Error(
|
|
5839
|
-
`Mismatch between expected span parent object id ${await args.parentObjectId.get()} and provided id ${parentComponentsObjectId}`
|
|
5840
|
-
);
|
|
5841
|
-
}
|
|
5842
|
-
return await args.parentObjectId.get();
|
|
5843
|
-
};
|
|
5844
|
-
argParentObjectId = new LazyValue(computeParentObjectId);
|
|
5844
|
+
argParentObjectId = args.parentObjectId;
|
|
5845
5845
|
if (parentComponents.data.row_id) {
|
|
5846
5846
|
argParentSpanIds = {
|
|
5847
5847
|
spanId: parentComponents.data.span_id,
|
|
@@ -6170,6 +6170,7 @@ function now() {
|
|
|
6170
6170
|
}
|
|
6171
6171
|
var DEFAULT_FLUSH_BACKPRESSURE_BYTES = 10 * 1024 * 1024;
|
|
6172
6172
|
var BACKGROUND_LOGGER_BASE_SLEEP_TIME_S = 1;
|
|
6173
|
+
var HTTP_RETRY_BASE_SLEEP_TIME_S = 1;
|
|
6173
6174
|
var HTTPBackgroundLogger = (_class10 = class _HTTPBackgroundLogger {
|
|
6174
6175
|
|
|
6175
6176
|
|
|
@@ -6800,17 +6801,10 @@ function init(projectOrOptions, optionalOptions) {
|
|
|
6800
6801
|
if (repoInfo2) {
|
|
6801
6802
|
return repoInfo2;
|
|
6802
6803
|
}
|
|
6803
|
-
|
|
6804
|
-
|
|
6805
|
-
|
|
6806
|
-
|
|
6807
|
-
};
|
|
6808
|
-
if (gitMetadataSettings) {
|
|
6809
|
-
mergedGitMetadataSettings = mergeGitMetadataSettings(
|
|
6810
|
-
mergedGitMetadataSettings,
|
|
6811
|
-
gitMetadataSettings
|
|
6812
|
-
);
|
|
6813
|
-
}
|
|
6804
|
+
const mergedGitMetadataSettings = state.gitMetadataSettings == null ? _nullishCoalesce(gitMetadataSettings, () => ( { collect: "none" })) : mergeGitMetadataSettings(
|
|
6805
|
+
state.gitMetadataSettings,
|
|
6806
|
+
_nullishCoalesce(gitMetadataSettings, () => ( { collect: "all" }))
|
|
6807
|
+
);
|
|
6814
6808
|
return await isomorph_default.getRepoInfo(mergedGitMetadataSettings);
|
|
6815
6809
|
})();
|
|
6816
6810
|
if (repoInfoArg) {
|
|
@@ -7192,6 +7186,55 @@ async function computeLoggerMetadata(state, {
|
|
|
7192
7186
|
};
|
|
7193
7187
|
}
|
|
7194
7188
|
}
|
|
7189
|
+
function initLogger(options = {}) {
|
|
7190
|
+
const {
|
|
7191
|
+
projectName,
|
|
7192
|
+
projectId,
|
|
7193
|
+
asyncFlush: asyncFlushArg,
|
|
7194
|
+
appUrl,
|
|
7195
|
+
apiKey,
|
|
7196
|
+
orgName,
|
|
7197
|
+
forceLogin,
|
|
7198
|
+
debugLogLevel,
|
|
7199
|
+
fetch: fetch2,
|
|
7200
|
+
state: stateArg
|
|
7201
|
+
} = options || {};
|
|
7202
|
+
const asyncFlush = asyncFlushArg === void 0 ? true : asyncFlushArg;
|
|
7203
|
+
const computeMetadataArgs = {
|
|
7204
|
+
project_name: projectName,
|
|
7205
|
+
project_id: projectId
|
|
7206
|
+
};
|
|
7207
|
+
const linkArgs = {
|
|
7208
|
+
org_name: orgName,
|
|
7209
|
+
app_url: appUrl,
|
|
7210
|
+
project_name: projectName,
|
|
7211
|
+
project_id: projectId
|
|
7212
|
+
};
|
|
7213
|
+
const state = _nullishCoalesce(stateArg, () => ( _globalState));
|
|
7214
|
+
state.setDebugLogLevel(debugLogLevel);
|
|
7215
|
+
state.enforceQueueSizeLimit(true);
|
|
7216
|
+
const lazyMetadata = new LazyValue(
|
|
7217
|
+
async () => {
|
|
7218
|
+
await state.login({
|
|
7219
|
+
orgName,
|
|
7220
|
+
apiKey,
|
|
7221
|
+
appUrl,
|
|
7222
|
+
forceLogin,
|
|
7223
|
+
fetch: fetch2
|
|
7224
|
+
});
|
|
7225
|
+
return computeLoggerMetadata(state, computeMetadataArgs);
|
|
7226
|
+
}
|
|
7227
|
+
);
|
|
7228
|
+
const ret = new Logger(state, lazyMetadata, {
|
|
7229
|
+
asyncFlush,
|
|
7230
|
+
computeMetadataArgs,
|
|
7231
|
+
linkArgs
|
|
7232
|
+
});
|
|
7233
|
+
if (_nullishCoalesce(options.setCurrent, () => ( true))) {
|
|
7234
|
+
state.currentLogger = ret;
|
|
7235
|
+
}
|
|
7236
|
+
return ret;
|
|
7237
|
+
}
|
|
7195
7238
|
async function login(options = {}) {
|
|
7196
7239
|
const { forceLogin = false } = options || {};
|
|
7197
7240
|
if (!_internalGetGlobalState()) {
|
|
@@ -12877,11 +12920,11 @@ function resolveDenyOutputPaths(event, defaultDenyOutputPaths) {
|
|
|
12877
12920
|
if (Array.isArray(_optionalChain([event, 'optionalAccess', _212 => _212.denyOutputPaths]))) {
|
|
12878
12921
|
return event.denyOutputPaths;
|
|
12879
12922
|
}
|
|
12880
|
-
const
|
|
12881
|
-
if (!
|
|
12923
|
+
const firstArgument2 = _optionalChain([event, 'optionalAccess', _213 => _213.arguments]) && event.arguments.length > 0 ? event.arguments[0] : void 0;
|
|
12924
|
+
if (!firstArgument2 || typeof firstArgument2 !== "object") {
|
|
12882
12925
|
return defaultDenyOutputPaths;
|
|
12883
12926
|
}
|
|
12884
|
-
const runtimeDenyOutputPaths =
|
|
12927
|
+
const runtimeDenyOutputPaths = firstArgument2[RUNTIME_DENY_OUTPUT_PATHS];
|
|
12885
12928
|
if (Array.isArray(runtimeDenyOutputPaths) && runtimeDenyOutputPaths.every((path2) => typeof path2 === "string")) {
|
|
12886
12929
|
return runtimeDenyOutputPaths;
|
|
12887
12930
|
}
|
|
@@ -16540,6 +16583,467 @@ function cleanMetrics2(metrics) {
|
|
|
16540
16583
|
return cleaned;
|
|
16541
16584
|
}
|
|
16542
16585
|
|
|
16586
|
+
// src/instrumentation/plugins/openai-agents-channels.ts
|
|
16587
|
+
var openAIAgentsCoreChannels = defineChannels("@openai/agents-core", {
|
|
16588
|
+
onTraceStart: channel({
|
|
16589
|
+
channelName: "tracing.processor.onTraceStart",
|
|
16590
|
+
kind: "async"
|
|
16591
|
+
}),
|
|
16592
|
+
onTraceEnd: channel({
|
|
16593
|
+
channelName: "tracing.processor.onTraceEnd",
|
|
16594
|
+
kind: "async"
|
|
16595
|
+
}),
|
|
16596
|
+
onSpanStart: channel({
|
|
16597
|
+
channelName: "tracing.processor.onSpanStart",
|
|
16598
|
+
kind: "async"
|
|
16599
|
+
}),
|
|
16600
|
+
onSpanEnd: channel({
|
|
16601
|
+
channelName: "tracing.processor.onSpanEnd",
|
|
16602
|
+
kind: "async"
|
|
16603
|
+
})
|
|
16604
|
+
});
|
|
16605
|
+
|
|
16606
|
+
// src/instrumentation/plugins/openai-agents-trace-processor.ts
|
|
16607
|
+
function isSpanData(spanData, type) {
|
|
16608
|
+
return spanData.type === type;
|
|
16609
|
+
}
|
|
16610
|
+
function spanTypeFromAgents(span) {
|
|
16611
|
+
const spanType = span.spanData.type;
|
|
16612
|
+
if (spanType === "function" || spanType === "guardrail" || spanType === "mcp_tools") {
|
|
16613
|
+
return "tool" /* TOOL */;
|
|
16614
|
+
}
|
|
16615
|
+
if (spanType === "generation" || spanType === "response" || spanType === "transcription" || spanType === "speech") {
|
|
16616
|
+
return "llm" /* LLM */;
|
|
16617
|
+
}
|
|
16618
|
+
return "task" /* TASK */;
|
|
16619
|
+
}
|
|
16620
|
+
function spanNameFromAgents(span) {
|
|
16621
|
+
const spanData = span.spanData;
|
|
16622
|
+
if ("name" in spanData && spanData.name) {
|
|
16623
|
+
return spanData.name;
|
|
16624
|
+
}
|
|
16625
|
+
switch (spanData.type) {
|
|
16626
|
+
case "generation":
|
|
16627
|
+
return "Generation";
|
|
16628
|
+
case "response":
|
|
16629
|
+
return "Response";
|
|
16630
|
+
case "handoff":
|
|
16631
|
+
return "Handoff";
|
|
16632
|
+
case "mcp_tools":
|
|
16633
|
+
return isSpanData(spanData, "mcp_tools") && spanData.server ? `List Tools (${spanData.server})` : "MCP List Tools";
|
|
16634
|
+
case "transcription":
|
|
16635
|
+
return "Transcription";
|
|
16636
|
+
case "speech":
|
|
16637
|
+
return "Speech";
|
|
16638
|
+
case "speech_group":
|
|
16639
|
+
return "Speech Group";
|
|
16640
|
+
default:
|
|
16641
|
+
return "Unknown";
|
|
16642
|
+
}
|
|
16643
|
+
}
|
|
16644
|
+
function getTimeElapsed(end, start) {
|
|
16645
|
+
if (!start || !end) {
|
|
16646
|
+
return void 0;
|
|
16647
|
+
}
|
|
16648
|
+
const startTime = new Date(start).getTime();
|
|
16649
|
+
const endTime = new Date(end).getTime();
|
|
16650
|
+
if (Number.isNaN(startTime) || Number.isNaN(endTime)) {
|
|
16651
|
+
return void 0;
|
|
16652
|
+
}
|
|
16653
|
+
return (endTime - startTime) / 1e3;
|
|
16654
|
+
}
|
|
16655
|
+
function getNumberProperty2(obj, key) {
|
|
16656
|
+
if (!isObject(obj) || !(key in obj)) {
|
|
16657
|
+
return void 0;
|
|
16658
|
+
}
|
|
16659
|
+
const value = obj[key];
|
|
16660
|
+
return typeof value === "number" ? value : void 0;
|
|
16661
|
+
}
|
|
16662
|
+
function parseUsageMetrics(usage) {
|
|
16663
|
+
const metrics = {};
|
|
16664
|
+
if (!isObject(usage)) {
|
|
16665
|
+
return metrics;
|
|
16666
|
+
}
|
|
16667
|
+
const promptTokens = _nullishCoalesce(_nullishCoalesce(_nullishCoalesce(getNumberProperty2(usage, "prompt_tokens"), () => ( getNumberProperty2(usage, "input_tokens"))), () => ( getNumberProperty2(usage, "promptTokens"))), () => ( getNumberProperty2(usage, "inputTokens")));
|
|
16668
|
+
const completionTokens = _nullishCoalesce(_nullishCoalesce(_nullishCoalesce(getNumberProperty2(usage, "completion_tokens"), () => ( getNumberProperty2(usage, "output_tokens"))), () => ( getNumberProperty2(usage, "completionTokens"))), () => ( getNumberProperty2(usage, "outputTokens")));
|
|
16669
|
+
const totalTokens = _nullishCoalesce(getNumberProperty2(usage, "total_tokens"), () => ( getNumberProperty2(usage, "totalTokens")));
|
|
16670
|
+
if (promptTokens !== void 0) {
|
|
16671
|
+
metrics.prompt_tokens = promptTokens;
|
|
16672
|
+
}
|
|
16673
|
+
if (completionTokens !== void 0) {
|
|
16674
|
+
metrics.completion_tokens = completionTokens;
|
|
16675
|
+
}
|
|
16676
|
+
if (totalTokens !== void 0) {
|
|
16677
|
+
metrics.tokens = totalTokens;
|
|
16678
|
+
} else if (promptTokens !== void 0 && completionTokens !== void 0) {
|
|
16679
|
+
metrics.tokens = promptTokens + completionTokens;
|
|
16680
|
+
}
|
|
16681
|
+
const inputDetails = usage.input_tokens_details;
|
|
16682
|
+
const cachedTokens = getNumberProperty2(inputDetails, "cached_tokens");
|
|
16683
|
+
const cacheWriteTokens = getNumberProperty2(
|
|
16684
|
+
inputDetails,
|
|
16685
|
+
"cache_write_tokens"
|
|
16686
|
+
);
|
|
16687
|
+
if (cachedTokens !== void 0) {
|
|
16688
|
+
metrics.prompt_cached_tokens = cachedTokens;
|
|
16689
|
+
}
|
|
16690
|
+
if (cacheWriteTokens !== void 0) {
|
|
16691
|
+
metrics.prompt_cache_creation_tokens = cacheWriteTokens;
|
|
16692
|
+
}
|
|
16693
|
+
return metrics;
|
|
16694
|
+
}
|
|
16695
|
+
var OpenAIAgentsTraceProcessor = (_class19 = class _OpenAIAgentsTraceProcessor {
|
|
16696
|
+
static __initStatic() {this.DEFAULT_MAX_TRACES = 1e4}
|
|
16697
|
+
|
|
16698
|
+
|
|
16699
|
+
__init65() {this.traceSpans = /* @__PURE__ */ new Map()}
|
|
16700
|
+
__init66() {this.traceOrder = []}
|
|
16701
|
+
__init67() {this._traceSpans = this.traceSpans}
|
|
16702
|
+
constructor(options = {}) {;_class19.prototype.__init65.call(this);_class19.prototype.__init66.call(this);_class19.prototype.__init67.call(this);
|
|
16703
|
+
this.logger = options.logger;
|
|
16704
|
+
this.maxTraces = _nullishCoalesce(options.maxTraces, () => ( _OpenAIAgentsTraceProcessor.DEFAULT_MAX_TRACES));
|
|
16705
|
+
}
|
|
16706
|
+
evictOldestTrace() {
|
|
16707
|
+
const oldestTraceId = this.traceOrder.shift();
|
|
16708
|
+
if (oldestTraceId) {
|
|
16709
|
+
this.traceSpans.delete(oldestTraceId);
|
|
16710
|
+
}
|
|
16711
|
+
}
|
|
16712
|
+
onTraceStart(trace) {
|
|
16713
|
+
if (!_optionalChain([trace, 'optionalAccess', _368 => _368.traceId])) {
|
|
16714
|
+
return Promise.resolve();
|
|
16715
|
+
}
|
|
16716
|
+
if (this.traceOrder.length >= this.maxTraces) {
|
|
16717
|
+
this.evictOldestTrace();
|
|
16718
|
+
}
|
|
16719
|
+
const current = currentSpan();
|
|
16720
|
+
const span = current && current !== NOOP_SPAN ? current.startSpan({
|
|
16721
|
+
name: trace.name,
|
|
16722
|
+
type: "task" /* TASK */
|
|
16723
|
+
}) : this.logger ? this.logger.startSpan({
|
|
16724
|
+
name: trace.name,
|
|
16725
|
+
type: "task" /* TASK */
|
|
16726
|
+
}) : startSpan({
|
|
16727
|
+
name: trace.name,
|
|
16728
|
+
type: "task" /* TASK */
|
|
16729
|
+
});
|
|
16730
|
+
span.log({
|
|
16731
|
+
input: "Agent workflow started",
|
|
16732
|
+
metadata: {
|
|
16733
|
+
group_id: trace.groupId,
|
|
16734
|
+
...trace.metadata || {}
|
|
16735
|
+
}
|
|
16736
|
+
});
|
|
16737
|
+
this.traceSpans.set(trace.traceId, {
|
|
16738
|
+
rootSpan: span,
|
|
16739
|
+
childSpans: /* @__PURE__ */ new Map(),
|
|
16740
|
+
metadata: {
|
|
16741
|
+
firstInput: null,
|
|
16742
|
+
lastOutput: null
|
|
16743
|
+
}
|
|
16744
|
+
});
|
|
16745
|
+
this.traceOrder.push(trace.traceId);
|
|
16746
|
+
return Promise.resolve();
|
|
16747
|
+
}
|
|
16748
|
+
async onTraceEnd(trace) {
|
|
16749
|
+
const traceData = this.traceSpans.get(_optionalChain([trace, 'optionalAccess', _369 => _369.traceId]));
|
|
16750
|
+
if (!traceData) {
|
|
16751
|
+
return;
|
|
16752
|
+
}
|
|
16753
|
+
try {
|
|
16754
|
+
traceData.rootSpan.log({
|
|
16755
|
+
input: traceData.metadata.firstInput,
|
|
16756
|
+
output: traceData.metadata.lastOutput
|
|
16757
|
+
});
|
|
16758
|
+
traceData.rootSpan.end();
|
|
16759
|
+
await traceData.rootSpan.flush();
|
|
16760
|
+
} finally {
|
|
16761
|
+
this.traceSpans.delete(trace.traceId);
|
|
16762
|
+
const orderIndex = this.traceOrder.indexOf(trace.traceId);
|
|
16763
|
+
if (orderIndex > -1) {
|
|
16764
|
+
this.traceOrder.splice(orderIndex, 1);
|
|
16765
|
+
}
|
|
16766
|
+
}
|
|
16767
|
+
}
|
|
16768
|
+
onSpanStart(span) {
|
|
16769
|
+
if (!_optionalChain([span, 'optionalAccess', _370 => _370.spanId]) || !span.traceId) {
|
|
16770
|
+
return Promise.resolve();
|
|
16771
|
+
}
|
|
16772
|
+
const traceData = this.traceSpans.get(span.traceId);
|
|
16773
|
+
if (!traceData) {
|
|
16774
|
+
return Promise.resolve();
|
|
16775
|
+
}
|
|
16776
|
+
const parentSpan = span.parentId ? traceData.childSpans.get(span.parentId) : traceData.rootSpan;
|
|
16777
|
+
if (!parentSpan) {
|
|
16778
|
+
return Promise.resolve();
|
|
16779
|
+
}
|
|
16780
|
+
const childSpan = parentSpan.startSpan({
|
|
16781
|
+
name: spanNameFromAgents(span),
|
|
16782
|
+
type: spanTypeFromAgents(span)
|
|
16783
|
+
});
|
|
16784
|
+
traceData.childSpans.set(span.spanId, childSpan);
|
|
16785
|
+
return Promise.resolve();
|
|
16786
|
+
}
|
|
16787
|
+
onSpanEnd(span) {
|
|
16788
|
+
if (!_optionalChain([span, 'optionalAccess', _371 => _371.spanId]) || !span.traceId) {
|
|
16789
|
+
return Promise.resolve();
|
|
16790
|
+
}
|
|
16791
|
+
const traceData = this.traceSpans.get(span.traceId);
|
|
16792
|
+
if (!traceData) {
|
|
16793
|
+
return Promise.resolve();
|
|
16794
|
+
}
|
|
16795
|
+
const braintrustSpan = traceData.childSpans.get(span.spanId);
|
|
16796
|
+
if (!braintrustSpan) {
|
|
16797
|
+
return Promise.resolve();
|
|
16798
|
+
}
|
|
16799
|
+
const logData = this.extractLogData(span);
|
|
16800
|
+
braintrustSpan.log({
|
|
16801
|
+
error: span.error,
|
|
16802
|
+
...logData
|
|
16803
|
+
});
|
|
16804
|
+
braintrustSpan.end();
|
|
16805
|
+
traceData.childSpans.delete(span.spanId);
|
|
16806
|
+
const input = logData.input;
|
|
16807
|
+
const output = logData.output;
|
|
16808
|
+
if (traceData.metadata.firstInput === null && input != null) {
|
|
16809
|
+
traceData.metadata.firstInput = input;
|
|
16810
|
+
}
|
|
16811
|
+
if (output != null) {
|
|
16812
|
+
traceData.metadata.lastOutput = output;
|
|
16813
|
+
}
|
|
16814
|
+
return Promise.resolve();
|
|
16815
|
+
}
|
|
16816
|
+
async shutdown() {
|
|
16817
|
+
if (this.logger && typeof this.logger.flush === "function") {
|
|
16818
|
+
await this.logger.flush();
|
|
16819
|
+
}
|
|
16820
|
+
}
|
|
16821
|
+
async forceFlush() {
|
|
16822
|
+
if (this.logger && typeof this.logger.flush === "function") {
|
|
16823
|
+
await this.logger.flush();
|
|
16824
|
+
}
|
|
16825
|
+
}
|
|
16826
|
+
extractLogData(span) {
|
|
16827
|
+
const spanData = span.spanData;
|
|
16828
|
+
switch (spanData.type) {
|
|
16829
|
+
case "agent":
|
|
16830
|
+
return this.extractAgentLogData(spanData);
|
|
16831
|
+
case "response":
|
|
16832
|
+
return this.extractResponseLogData(spanData, span);
|
|
16833
|
+
case "function":
|
|
16834
|
+
return this.extractFunctionLogData(spanData);
|
|
16835
|
+
case "handoff":
|
|
16836
|
+
return this.extractHandoffLogData(spanData);
|
|
16837
|
+
case "guardrail":
|
|
16838
|
+
return this.extractGuardrailLogData(spanData);
|
|
16839
|
+
case "generation":
|
|
16840
|
+
return this.extractGenerationLogData(spanData, span);
|
|
16841
|
+
case "custom":
|
|
16842
|
+
return this.extractCustomLogData(spanData);
|
|
16843
|
+
case "mcp_tools":
|
|
16844
|
+
return this.extractMCPListToolsLogData(spanData);
|
|
16845
|
+
case "transcription":
|
|
16846
|
+
return this.extractTranscriptionLogData(spanData);
|
|
16847
|
+
case "speech":
|
|
16848
|
+
return this.extractSpeechLogData(spanData);
|
|
16849
|
+
case "speech_group":
|
|
16850
|
+
return this.extractSpeechGroupLogData(spanData);
|
|
16851
|
+
default:
|
|
16852
|
+
return {};
|
|
16853
|
+
}
|
|
16854
|
+
}
|
|
16855
|
+
extractAgentLogData(spanData) {
|
|
16856
|
+
return {
|
|
16857
|
+
metadata: {
|
|
16858
|
+
tools: spanData.tools,
|
|
16859
|
+
handoffs: spanData.handoffs,
|
|
16860
|
+
output_type: spanData.output_type
|
|
16861
|
+
}
|
|
16862
|
+
};
|
|
16863
|
+
}
|
|
16864
|
+
extractResponseLogData(spanData, span) {
|
|
16865
|
+
const response = spanData._response;
|
|
16866
|
+
const output = isObject(response) ? response.output : void 0;
|
|
16867
|
+
const usage = isObject(response) ? response.usage : void 0;
|
|
16868
|
+
const metrics = {
|
|
16869
|
+
...this.extractTimingMetrics(span),
|
|
16870
|
+
...parseUsageMetrics(usage)
|
|
16871
|
+
};
|
|
16872
|
+
return {
|
|
16873
|
+
input: spanData._input,
|
|
16874
|
+
output,
|
|
16875
|
+
metadata: isObject(response) ? this.omitKeys(response, ["output", "usage"]) : {},
|
|
16876
|
+
metrics
|
|
16877
|
+
};
|
|
16878
|
+
}
|
|
16879
|
+
extractFunctionLogData(spanData) {
|
|
16880
|
+
return {
|
|
16881
|
+
input: spanData.input,
|
|
16882
|
+
output: spanData.output
|
|
16883
|
+
};
|
|
16884
|
+
}
|
|
16885
|
+
extractHandoffLogData(spanData) {
|
|
16886
|
+
return {
|
|
16887
|
+
metadata: {
|
|
16888
|
+
from_agent: spanData.from_agent,
|
|
16889
|
+
to_agent: spanData.to_agent
|
|
16890
|
+
}
|
|
16891
|
+
};
|
|
16892
|
+
}
|
|
16893
|
+
extractGuardrailLogData(spanData) {
|
|
16894
|
+
return {
|
|
16895
|
+
metadata: {
|
|
16896
|
+
triggered: spanData.triggered
|
|
16897
|
+
}
|
|
16898
|
+
};
|
|
16899
|
+
}
|
|
16900
|
+
extractGenerationLogData(spanData, span) {
|
|
16901
|
+
return {
|
|
16902
|
+
input: spanData.input,
|
|
16903
|
+
output: spanData.output,
|
|
16904
|
+
metadata: {
|
|
16905
|
+
model: spanData.model,
|
|
16906
|
+
model_config: spanData.model_config
|
|
16907
|
+
},
|
|
16908
|
+
metrics: {
|
|
16909
|
+
...this.extractTimingMetrics(span),
|
|
16910
|
+
...parseUsageMetrics(spanData.usage)
|
|
16911
|
+
}
|
|
16912
|
+
};
|
|
16913
|
+
}
|
|
16914
|
+
extractCustomLogData(spanData) {
|
|
16915
|
+
return spanData.data || {};
|
|
16916
|
+
}
|
|
16917
|
+
extractMCPListToolsLogData(spanData) {
|
|
16918
|
+
return {
|
|
16919
|
+
output: spanData.result,
|
|
16920
|
+
metadata: {
|
|
16921
|
+
server: spanData.server
|
|
16922
|
+
}
|
|
16923
|
+
};
|
|
16924
|
+
}
|
|
16925
|
+
extractTranscriptionLogData(spanData) {
|
|
16926
|
+
return {
|
|
16927
|
+
input: spanData.input,
|
|
16928
|
+
output: spanData.output,
|
|
16929
|
+
metadata: {
|
|
16930
|
+
model: spanData.model,
|
|
16931
|
+
model_config: spanData.model_config
|
|
16932
|
+
}
|
|
16933
|
+
};
|
|
16934
|
+
}
|
|
16935
|
+
extractSpeechLogData(spanData) {
|
|
16936
|
+
return {
|
|
16937
|
+
input: spanData.input,
|
|
16938
|
+
output: spanData.output,
|
|
16939
|
+
metadata: {
|
|
16940
|
+
model: spanData.model,
|
|
16941
|
+
model_config: spanData.model_config
|
|
16942
|
+
}
|
|
16943
|
+
};
|
|
16944
|
+
}
|
|
16945
|
+
extractSpeechGroupLogData(spanData) {
|
|
16946
|
+
return {
|
|
16947
|
+
input: spanData.input
|
|
16948
|
+
};
|
|
16949
|
+
}
|
|
16950
|
+
extractTimingMetrics(span) {
|
|
16951
|
+
const timeToFirstToken = getTimeElapsed(
|
|
16952
|
+
_nullishCoalesce(span.endedAt, () => ( void 0)),
|
|
16953
|
+
_nullishCoalesce(span.startedAt, () => ( void 0))
|
|
16954
|
+
);
|
|
16955
|
+
return timeToFirstToken === void 0 ? {} : { time_to_first_token: timeToFirstToken };
|
|
16956
|
+
}
|
|
16957
|
+
omitKeys(value, keys) {
|
|
16958
|
+
const result = {};
|
|
16959
|
+
for (const [key, fieldValue] of Object.entries(value)) {
|
|
16960
|
+
if (!keys.includes(key)) {
|
|
16961
|
+
result[key] = fieldValue;
|
|
16962
|
+
}
|
|
16963
|
+
}
|
|
16964
|
+
return result;
|
|
16965
|
+
}
|
|
16966
|
+
}, _class19.__initStatic(), _class19);
|
|
16967
|
+
|
|
16968
|
+
// src/instrumentation/plugins/openai-agents-plugin.ts
|
|
16969
|
+
function firstArgument(args) {
|
|
16970
|
+
if (Array.isArray(args)) {
|
|
16971
|
+
return args[0];
|
|
16972
|
+
}
|
|
16973
|
+
if (isObject(args) && "length" in args && typeof args.length === "number" && Number.isInteger(args.length) && args.length >= 0) {
|
|
16974
|
+
return Array.from(args)[0];
|
|
16975
|
+
}
|
|
16976
|
+
return void 0;
|
|
16977
|
+
}
|
|
16978
|
+
function isOpenAIAgentsTrace(value) {
|
|
16979
|
+
return isObject(value) && value.type === "trace" && typeof value.traceId === "string";
|
|
16980
|
+
}
|
|
16981
|
+
function isOpenAIAgentsSpan(value) {
|
|
16982
|
+
return isObject(value) && value.type === "trace.span" && typeof value.traceId === "string" && typeof value.spanId === "string";
|
|
16983
|
+
}
|
|
16984
|
+
var OpenAIAgentsPlugin = (_class20 = class extends BasePlugin {constructor(...args4) { super(...args4); _class20.prototype.__init68.call(this); }
|
|
16985
|
+
__init68() {this.processor = new OpenAIAgentsTraceProcessor()}
|
|
16986
|
+
onEnable() {
|
|
16987
|
+
this.subscribeToTraceLifecycle();
|
|
16988
|
+
}
|
|
16989
|
+
onDisable() {
|
|
16990
|
+
this.unsubscribers = unsubscribeAll(this.unsubscribers);
|
|
16991
|
+
void this.processor.shutdown();
|
|
16992
|
+
}
|
|
16993
|
+
subscribeToTraceLifecycle() {
|
|
16994
|
+
const traceStartChannel = openAIAgentsCoreChannels.onTraceStart.tracingChannel();
|
|
16995
|
+
const traceStartHandlers = {
|
|
16996
|
+
start: (event) => {
|
|
16997
|
+
const trace = firstArgument(event.arguments);
|
|
16998
|
+
if (isOpenAIAgentsTrace(trace)) {
|
|
16999
|
+
void this.processor.onTraceStart(trace);
|
|
17000
|
+
}
|
|
17001
|
+
}
|
|
17002
|
+
};
|
|
17003
|
+
traceStartChannel.subscribe(traceStartHandlers);
|
|
17004
|
+
this.unsubscribers.push(
|
|
17005
|
+
() => traceStartChannel.unsubscribe(traceStartHandlers)
|
|
17006
|
+
);
|
|
17007
|
+
const traceEndChannel = openAIAgentsCoreChannels.onTraceEnd.tracingChannel();
|
|
17008
|
+
const traceEndHandlers = {
|
|
17009
|
+
start: (event) => {
|
|
17010
|
+
const trace = firstArgument(event.arguments);
|
|
17011
|
+
if (isOpenAIAgentsTrace(trace)) {
|
|
17012
|
+
void this.processor.onTraceEnd(trace);
|
|
17013
|
+
}
|
|
17014
|
+
}
|
|
17015
|
+
};
|
|
17016
|
+
traceEndChannel.subscribe(traceEndHandlers);
|
|
17017
|
+
this.unsubscribers.push(
|
|
17018
|
+
() => traceEndChannel.unsubscribe(traceEndHandlers)
|
|
17019
|
+
);
|
|
17020
|
+
const spanStartChannel = openAIAgentsCoreChannels.onSpanStart.tracingChannel();
|
|
17021
|
+
const spanStartHandlers = {
|
|
17022
|
+
start: (event) => {
|
|
17023
|
+
const span = firstArgument(event.arguments);
|
|
17024
|
+
if (isOpenAIAgentsSpan(span)) {
|
|
17025
|
+
void this.processor.onSpanStart(span);
|
|
17026
|
+
}
|
|
17027
|
+
}
|
|
17028
|
+
};
|
|
17029
|
+
spanStartChannel.subscribe(spanStartHandlers);
|
|
17030
|
+
this.unsubscribers.push(
|
|
17031
|
+
() => spanStartChannel.unsubscribe(spanStartHandlers)
|
|
17032
|
+
);
|
|
17033
|
+
const spanEndChannel = openAIAgentsCoreChannels.onSpanEnd.tracingChannel();
|
|
17034
|
+
const spanEndHandlers = {
|
|
17035
|
+
start: (event) => {
|
|
17036
|
+
const span = firstArgument(event.arguments);
|
|
17037
|
+
if (isOpenAIAgentsSpan(span)) {
|
|
17038
|
+
void this.processor.onSpanEnd(span);
|
|
17039
|
+
}
|
|
17040
|
+
}
|
|
17041
|
+
};
|
|
17042
|
+
spanEndChannel.subscribe(spanEndHandlers);
|
|
17043
|
+
this.unsubscribers.push(() => spanEndChannel.unsubscribe(spanEndHandlers));
|
|
17044
|
+
}
|
|
17045
|
+
}, _class20);
|
|
17046
|
+
|
|
16543
17047
|
// src/instrumentation/plugins/google-genai-channels.ts
|
|
16544
17048
|
var googleGenAIChannels = defineChannels("@google/genai", {
|
|
16545
17049
|
generateContent: channel({
|
|
@@ -16651,7 +17155,7 @@ var GoogleGenAIPlugin = class extends BasePlugin {
|
|
|
16651
17155
|
};
|
|
16652
17156
|
tracingChannel2.subscribe(handlers);
|
|
16653
17157
|
this.unsubscribers.push(() => {
|
|
16654
|
-
_optionalChain([unbindCurrentSpanStore, 'optionalCall',
|
|
17158
|
+
_optionalChain([unbindCurrentSpanStore, 'optionalCall', _372 => _372()]);
|
|
16655
17159
|
tracingChannel2.unsubscribe(handlers);
|
|
16656
17160
|
});
|
|
16657
17161
|
}
|
|
@@ -16748,7 +17252,7 @@ var GoogleGenAIPlugin = class extends BasePlugin {
|
|
|
16748
17252
|
};
|
|
16749
17253
|
tracingChannel2.subscribe(handlers);
|
|
16750
17254
|
this.unsubscribers.push(() => {
|
|
16751
|
-
_optionalChain([unbindCurrentSpanStore, 'optionalCall',
|
|
17255
|
+
_optionalChain([unbindCurrentSpanStore, 'optionalCall', _373 => _373()]);
|
|
16752
17256
|
tracingChannel2.unsubscribe(handlers);
|
|
16753
17257
|
});
|
|
16754
17258
|
}
|
|
@@ -16764,10 +17268,10 @@ function ensureSpanState(states, event, create) {
|
|
|
16764
17268
|
}
|
|
16765
17269
|
function bindCurrentSpanStoreToStart2(tracingChannel2, states, create) {
|
|
16766
17270
|
const state = _internalGetGlobalState();
|
|
16767
|
-
const contextManager = _optionalChain([state, 'optionalAccess',
|
|
17271
|
+
const contextManager = _optionalChain([state, 'optionalAccess', _374 => _374.contextManager]);
|
|
16768
17272
|
const startChannel = tracingChannel2.start;
|
|
16769
17273
|
const currentSpanStore = contextManager ? contextManager[BRAINTRUST_CURRENT_SPAN_STORE] : void 0;
|
|
16770
|
-
if (!_optionalChain([startChannel, 'optionalAccess',
|
|
17274
|
+
if (!_optionalChain([startChannel, 'optionalAccess', _375 => _375.bindStore]) || !currentSpanStore) {
|
|
16771
17275
|
return void 0;
|
|
16772
17276
|
}
|
|
16773
17277
|
startChannel.bindStore(currentSpanStore, (event) => {
|
|
@@ -16779,7 +17283,7 @@ function bindCurrentSpanStoreToStart2(tracingChannel2, states, create) {
|
|
|
16779
17283
|
return contextManager.wrapSpanForStore(span);
|
|
16780
17284
|
});
|
|
16781
17285
|
return () => {
|
|
16782
|
-
_optionalChain([startChannel, 'access',
|
|
17286
|
+
_optionalChain([startChannel, 'access', _376 => _376.unbindStore, 'optionalCall', _377 => _377(currentSpanStore)]);
|
|
16783
17287
|
};
|
|
16784
17288
|
}
|
|
16785
17289
|
function logErrorAndEndSpan(states, event) {
|
|
@@ -17014,7 +17518,7 @@ function serializePart(part) {
|
|
|
17014
17518
|
}
|
|
17015
17519
|
function serializeGenerateContentTools(params) {
|
|
17016
17520
|
const config = params.config ? tryToDict(params.config) : null;
|
|
17017
|
-
const tools = _optionalChain([config, 'optionalAccess',
|
|
17521
|
+
const tools = _optionalChain([config, 'optionalAccess', _378 => _378.tools]);
|
|
17018
17522
|
if (!Array.isArray(tools)) {
|
|
17019
17523
|
return null;
|
|
17020
17524
|
}
|
|
@@ -17073,7 +17577,7 @@ function extractGenerateContentMetrics(response, startTime) {
|
|
|
17073
17577
|
metrics.end = end;
|
|
17074
17578
|
metrics.duration = end - startTime;
|
|
17075
17579
|
}
|
|
17076
|
-
if (_optionalChain([response, 'optionalAccess',
|
|
17580
|
+
if (_optionalChain([response, 'optionalAccess', _379 => _379.usageMetadata])) {
|
|
17077
17581
|
populateUsageMetrics(metrics, response.usageMetadata);
|
|
17078
17582
|
}
|
|
17079
17583
|
return metrics;
|
|
@@ -17086,7 +17590,7 @@ function extractEmbedContentMetrics(response, startTime) {
|
|
|
17086
17590
|
metrics.end = end;
|
|
17087
17591
|
metrics.duration = end - startTime;
|
|
17088
17592
|
}
|
|
17089
|
-
if (_optionalChain([response, 'optionalAccess',
|
|
17593
|
+
if (_optionalChain([response, 'optionalAccess', _380 => _380.usageMetadata])) {
|
|
17090
17594
|
populateUsageMetrics(metrics, response.usageMetadata);
|
|
17091
17595
|
}
|
|
17092
17596
|
const embeddingTokenCount = extractEmbedPromptTokenCount(response);
|
|
@@ -17100,11 +17604,11 @@ function extractEmbedPromptTokenCount(response) {
|
|
|
17100
17604
|
if (!response) {
|
|
17101
17605
|
return void 0;
|
|
17102
17606
|
}
|
|
17103
|
-
const usagePromptTokens = _optionalChain([response, 'access',
|
|
17607
|
+
const usagePromptTokens = _optionalChain([response, 'access', _381 => _381.usageMetadata, 'optionalAccess', _382 => _382.promptTokenCount]);
|
|
17104
17608
|
if (typeof usagePromptTokens === "number" && Number.isFinite(usagePromptTokens)) {
|
|
17105
17609
|
return usagePromptTokens;
|
|
17106
17610
|
}
|
|
17107
|
-
const usageTotalTokens = _optionalChain([response, 'access',
|
|
17611
|
+
const usageTotalTokens = _optionalChain([response, 'access', _383 => _383.usageMetadata, 'optionalAccess', _384 => _384.totalTokenCount]);
|
|
17108
17612
|
if (typeof usageTotalTokens === "number" && Number.isFinite(usageTotalTokens)) {
|
|
17109
17613
|
return usageTotalTokens;
|
|
17110
17614
|
}
|
|
@@ -17115,8 +17619,8 @@ function extractEmbedPromptTokenCount(response) {
|
|
|
17115
17619
|
let total = 0;
|
|
17116
17620
|
let sawAny = false;
|
|
17117
17621
|
for (const embedding of embeddings) {
|
|
17118
|
-
const embeddingStats = tryToDict(_optionalChain([tryToDict, 'call',
|
|
17119
|
-
const tokenCount = _optionalChain([embeddingStats, 'optionalAccess',
|
|
17622
|
+
const embeddingStats = tryToDict(_optionalChain([tryToDict, 'call', _385 => _385(embedding), 'optionalAccess', _386 => _386.statistics]));
|
|
17623
|
+
const tokenCount = _optionalChain([embeddingStats, 'optionalAccess', _387 => _387.tokenCount]);
|
|
17120
17624
|
if (typeof tokenCount === "number" && Number.isFinite(tokenCount)) {
|
|
17121
17625
|
total += tokenCount;
|
|
17122
17626
|
sawAny = true;
|
|
@@ -17132,7 +17636,7 @@ function summarizeEmbedContentOutput(response) {
|
|
|
17132
17636
|
if (embeddings.length === 0) {
|
|
17133
17637
|
return void 0;
|
|
17134
17638
|
}
|
|
17135
|
-
const firstValues = _optionalChain([embeddings, 'access',
|
|
17639
|
+
const firstValues = _optionalChain([embeddings, 'access', _388 => _388[0], 'optionalAccess', _389 => _389.values]);
|
|
17136
17640
|
if (!Array.isArray(firstValues)) {
|
|
17137
17641
|
return void 0;
|
|
17138
17642
|
}
|
|
@@ -17187,7 +17691,7 @@ function aggregateGenerateContentChunks(chunks, startTime, firstTokenTime) {
|
|
|
17187
17691
|
}
|
|
17188
17692
|
if (chunk.candidates && Array.isArray(chunk.candidates)) {
|
|
17189
17693
|
for (const candidate of chunk.candidates) {
|
|
17190
|
-
if (_optionalChain([candidate, 'access',
|
|
17694
|
+
if (_optionalChain([candidate, 'access', _390 => _390.content, 'optionalAccess', _391 => _391.parts])) {
|
|
17191
17695
|
for (const part of candidate.content.parts) {
|
|
17192
17696
|
if (part.text !== void 0) {
|
|
17193
17697
|
if (part.thought) {
|
|
@@ -17218,7 +17722,7 @@ function aggregateGenerateContentChunks(chunks, startTime, firstTokenTime) {
|
|
|
17218
17722
|
parts.push({ text });
|
|
17219
17723
|
}
|
|
17220
17724
|
parts.push(...otherParts);
|
|
17221
|
-
if (parts.length > 0 && _optionalChain([lastResponse, 'optionalAccess',
|
|
17725
|
+
if (parts.length > 0 && _optionalChain([lastResponse, 'optionalAccess', _392 => _392.candidates])) {
|
|
17222
17726
|
const candidates = [];
|
|
17223
17727
|
for (const candidate of lastResponse.candidates) {
|
|
17224
17728
|
const candidateDict = {
|
|
@@ -17275,7 +17779,7 @@ function extractResponseMetadata(response) {
|
|
|
17275
17779
|
if (Array.isArray(responseDict.candidates)) {
|
|
17276
17780
|
for (const candidate of responseDict.candidates) {
|
|
17277
17781
|
const candidateDict = tryToDict(candidate);
|
|
17278
|
-
if (_optionalChain([candidateDict, 'optionalAccess',
|
|
17782
|
+
if (_optionalChain([candidateDict, 'optionalAccess', _393 => _393.groundingMetadata]) !== void 0) {
|
|
17279
17783
|
candidateGroundingMetadata.push(candidateDict.groundingMetadata);
|
|
17280
17784
|
}
|
|
17281
17785
|
}
|
|
@@ -17356,9 +17860,9 @@ var HuggingFacePlugin = class extends BasePlugin {
|
|
|
17356
17860
|
name: "huggingface.chat_completion",
|
|
17357
17861
|
type: "llm" /* LLM */,
|
|
17358
17862
|
extractInput: extractChatInputWithMetadata,
|
|
17359
|
-
extractOutput: (result) => _optionalChain([result, 'optionalAccess',
|
|
17863
|
+
extractOutput: (result) => _optionalChain([result, 'optionalAccess', _394 => _394.choices]),
|
|
17360
17864
|
extractMetadata: (result) => extractResponseMetadata2(result),
|
|
17361
|
-
extractMetrics: (result) => parseMetricsFromUsage(_optionalChain([result, 'optionalAccess',
|
|
17865
|
+
extractMetrics: (result) => parseMetricsFromUsage(_optionalChain([result, 'optionalAccess', _395 => _395.usage]))
|
|
17362
17866
|
}),
|
|
17363
17867
|
traceSyncStreamChannel(huggingFaceChannels.chatCompletionStream, {
|
|
17364
17868
|
name: "huggingface.chat_completion_stream",
|
|
@@ -17376,7 +17880,7 @@ var HuggingFacePlugin = class extends BasePlugin {
|
|
|
17376
17880
|
extractInput: extractTextGenerationInputWithMetadata,
|
|
17377
17881
|
extractOutput: (result) => isObject(result) ? { generated_text: result.generated_text } : result,
|
|
17378
17882
|
extractMetadata: extractTextGenerationMetadata,
|
|
17379
|
-
extractMetrics: (result) => extractTextGenerationMetrics(_nullishCoalesce(_optionalChain([result, 'optionalAccess',
|
|
17883
|
+
extractMetrics: (result) => extractTextGenerationMetrics(_nullishCoalesce(_optionalChain([result, 'optionalAccess', _396 => _396.details]), () => ( null)))
|
|
17380
17884
|
}),
|
|
17381
17885
|
traceSyncStreamChannel(huggingFaceChannels.textGenerationStream, {
|
|
17382
17886
|
name: "huggingface.text_generation_stream",
|
|
@@ -17495,7 +17999,7 @@ function extractTextGenerationMetrics(details) {
|
|
|
17495
17999
|
return metrics;
|
|
17496
18000
|
}
|
|
17497
18001
|
function extractTextGenerationMetadata(result) {
|
|
17498
|
-
if (!isObject(_optionalChain([result, 'optionalAccess',
|
|
18002
|
+
if (!isObject(_optionalChain([result, 'optionalAccess', _397 => _397.details]))) {
|
|
17499
18003
|
return void 0;
|
|
17500
18004
|
}
|
|
17501
18005
|
return typeof result.details.finish_reason === "string" ? {
|
|
@@ -17541,7 +18045,7 @@ function patchChatCompletionStream(args) {
|
|
|
17541
18045
|
const lastChunk = chunks.at(-1);
|
|
17542
18046
|
const responseMetadata = extractResponseMetadata2(lastChunk);
|
|
17543
18047
|
const metrics = {
|
|
17544
|
-
...parseMetricsFromUsage(_optionalChain([lastChunk, 'optionalAccess',
|
|
18048
|
+
...parseMetricsFromUsage(_optionalChain([lastChunk, 'optionalAccess', _398 => _398.usage])),
|
|
17545
18049
|
...firstChunkTime !== void 0 ? { time_to_first_token: firstChunkTime - startTime } : {}
|
|
17546
18050
|
};
|
|
17547
18051
|
span.log({
|
|
@@ -17579,8 +18083,8 @@ function patchTextGenerationStream(args) {
|
|
|
17579
18083
|
output: aggregateTextGenerationStreamChunks(chunks),
|
|
17580
18084
|
...streamMetadata ? { metadata: streamMetadata } : {},
|
|
17581
18085
|
metrics: {
|
|
17582
|
-
...extractTextGenerationMetrics(_nullishCoalesce(_optionalChain([lastChunk, 'optionalAccess',
|
|
17583
|
-
...parseMetricsFromUsage(_optionalChain([lastChunk, 'optionalAccess',
|
|
18086
|
+
...extractTextGenerationMetrics(_nullishCoalesce(_optionalChain([lastChunk, 'optionalAccess', _399 => _399.details]), () => ( null))),
|
|
18087
|
+
...parseMetricsFromUsage(_optionalChain([lastChunk, 'optionalAccess', _400 => _400.usage])),
|
|
17584
18088
|
...firstChunkTime !== void 0 ? { time_to_first_token: firstChunkTime - startTime } : {}
|
|
17585
18089
|
}
|
|
17586
18090
|
});
|
|
@@ -17609,14 +18113,14 @@ function aggregateChatCompletionChunks2(chunks) {
|
|
|
17609
18113
|
}));
|
|
17610
18114
|
const delta = isObject(choice.delta) ? choice.delta : void 0;
|
|
17611
18115
|
const message = isObject(choice.message) ? choice.message : void 0;
|
|
17612
|
-
if (typeof _optionalChain([delta, 'optionalAccess',
|
|
18116
|
+
if (typeof _optionalChain([delta, 'optionalAccess', _401 => _401.content]) === "string") {
|
|
17613
18117
|
existing.content += delta.content;
|
|
17614
|
-
} else if (typeof _optionalChain([message, 'optionalAccess',
|
|
18118
|
+
} else if (typeof _optionalChain([message, 'optionalAccess', _402 => _402.content]) === "string") {
|
|
17615
18119
|
existing.content = message.content;
|
|
17616
18120
|
}
|
|
17617
|
-
if (typeof _optionalChain([delta, 'optionalAccess',
|
|
18121
|
+
if (typeof _optionalChain([delta, 'optionalAccess', _403 => _403.role]) === "string") {
|
|
17618
18122
|
existing.role = delta.role;
|
|
17619
|
-
} else if (typeof _optionalChain([message, 'optionalAccess',
|
|
18123
|
+
} else if (typeof _optionalChain([message, 'optionalAccess', _404 => _404.role]) === "string") {
|
|
17620
18124
|
existing.role = message.role;
|
|
17621
18125
|
}
|
|
17622
18126
|
if (choice.finish_reason !== void 0) {
|
|
@@ -17644,7 +18148,7 @@ function aggregateChatCompletionChunks2(chunks) {
|
|
|
17644
18148
|
};
|
|
17645
18149
|
}
|
|
17646
18150
|
function getChatToolCallDeltas(value) {
|
|
17647
|
-
if (!Array.isArray(_optionalChain([value, 'optionalAccess',
|
|
18151
|
+
if (!Array.isArray(_optionalChain([value, 'optionalAccess', _405 => _405.tool_calls]))) {
|
|
17648
18152
|
return void 0;
|
|
17649
18153
|
}
|
|
17650
18154
|
const toolCalls = value.tool_calls.filter((toolCall) => isObject(toolCall));
|
|
@@ -17709,7 +18213,7 @@ function aggregateTextGenerationStreamChunks(chunks) {
|
|
|
17709
18213
|
for (const chunk of chunks) {
|
|
17710
18214
|
if (typeof chunk.generated_text === "string") {
|
|
17711
18215
|
generatedText = chunk.generated_text;
|
|
17712
|
-
} else if (typeof _optionalChain([chunk, 'access',
|
|
18216
|
+
} else if (typeof _optionalChain([chunk, 'access', _406 => _406.token, 'optionalAccess', _407 => _407.text]) === "string" && !chunk.token.special) {
|
|
17713
18217
|
generatedText += chunk.token.text;
|
|
17714
18218
|
} else if (Array.isArray(chunk.choices)) {
|
|
17715
18219
|
for (const choice of chunk.choices) {
|
|
@@ -17733,17 +18237,17 @@ function aggregateTextGenerationStreamChunks(chunks) {
|
|
|
17733
18237
|
function extractTextGenerationStreamMetadata(chunks) {
|
|
17734
18238
|
for (let index = chunks.length - 1; index >= 0; index--) {
|
|
17735
18239
|
const chunk = chunks[index];
|
|
17736
|
-
if (isObject(_optionalChain([chunk, 'optionalAccess',
|
|
18240
|
+
if (isObject(_optionalChain([chunk, 'optionalAccess', _408 => _408.details])) && typeof chunk.details.finish_reason === "string") {
|
|
17737
18241
|
return {
|
|
17738
18242
|
finish_reason: chunk.details.finish_reason
|
|
17739
18243
|
};
|
|
17740
18244
|
}
|
|
17741
|
-
if (!Array.isArray(_optionalChain([chunk, 'optionalAccess',
|
|
18245
|
+
if (!Array.isArray(_optionalChain([chunk, 'optionalAccess', _409 => _409.choices]))) {
|
|
17742
18246
|
continue;
|
|
17743
18247
|
}
|
|
17744
18248
|
for (let choiceIndex = chunk.choices.length - 1; choiceIndex >= 0; choiceIndex--) {
|
|
17745
18249
|
const choice = chunk.choices[choiceIndex];
|
|
17746
|
-
if (_optionalChain([choice, 'optionalAccess',
|
|
18250
|
+
if (_optionalChain([choice, 'optionalAccess', _410 => _410.finish_reason]) !== void 0) {
|
|
17747
18251
|
return { finish_reason: choice.finish_reason };
|
|
17748
18252
|
}
|
|
17749
18253
|
}
|
|
@@ -17819,14 +18323,14 @@ var OpenRouterAgentPlugin = class extends BasePlugin {
|
|
|
17819
18323
|
extractMetadata: (result, event) => {
|
|
17820
18324
|
if (!isObject(result)) {
|
|
17821
18325
|
return {
|
|
17822
|
-
step: _optionalChain([event, 'optionalAccess',
|
|
17823
|
-
step_type: _optionalChain([event, 'optionalAccess',
|
|
18326
|
+
step: _optionalChain([event, 'optionalAccess', _411 => _411.step]),
|
|
18327
|
+
step_type: _optionalChain([event, 'optionalAccess', _412 => _412.stepType])
|
|
17824
18328
|
};
|
|
17825
18329
|
}
|
|
17826
18330
|
return {
|
|
17827
18331
|
...extractOpenRouterResponseMetadata(result) || {},
|
|
17828
|
-
..._optionalChain([event, 'optionalAccess',
|
|
17829
|
-
..._optionalChain([event, 'optionalAccess',
|
|
18332
|
+
..._optionalChain([event, 'optionalAccess', _413 => _413.step]) !== void 0 ? { step: event.step } : {},
|
|
18333
|
+
..._optionalChain([event, 'optionalAccess', _414 => _414.stepType]) ? { step_type: event.stepType } : {}
|
|
17830
18334
|
};
|
|
17831
18335
|
},
|
|
17832
18336
|
extractMetrics: (result) => isObject(result) ? parseOpenRouterMetricsFromUsage(result.usage) : {}
|
|
@@ -18187,7 +18691,7 @@ function publishToolResult(tracingChannel2, event, result) {
|
|
|
18187
18691
|
}
|
|
18188
18692
|
function getToolCallId(context) {
|
|
18189
18693
|
const toolContext = context;
|
|
18190
|
-
return typeof _optionalChain([toolContext, 'optionalAccess',
|
|
18694
|
+
return typeof _optionalChain([toolContext, 'optionalAccess', _415 => _415.toolCall, 'optionalAccess', _416 => _416.id]) === "string" ? toolContext.toolCall.id : void 0;
|
|
18191
18695
|
}
|
|
18192
18696
|
var OPENROUTER_WRAPPED_CALL_MODEL_RESULT = /* @__PURE__ */ Symbol(
|
|
18193
18697
|
"braintrust.openrouter.wrappedCallModelResult"
|
|
@@ -18585,9 +19089,9 @@ var OpenRouterPlugin = class extends BasePlugin {
|
|
|
18585
19089
|
type: "llm" /* LLM */,
|
|
18586
19090
|
extractInput: (args) => {
|
|
18587
19091
|
const request = getOpenRouterRequestArg(args);
|
|
18588
|
-
const chatGenerationParams = isObject(_optionalChain([request, 'optionalAccess',
|
|
18589
|
-
const httpReferer = _optionalChain([request, 'optionalAccess',
|
|
18590
|
-
const xTitle = _optionalChain([request, 'optionalAccess',
|
|
19092
|
+
const chatGenerationParams = isObject(_optionalChain([request, 'optionalAccess', _417 => _417.chatGenerationParams])) ? request.chatGenerationParams : {};
|
|
19093
|
+
const httpReferer = _optionalChain([request, 'optionalAccess', _418 => _418.httpReferer]);
|
|
19094
|
+
const xTitle = _optionalChain([request, 'optionalAccess', _419 => _419.xTitle]);
|
|
18591
19095
|
const { messages, ...metadata } = chatGenerationParams;
|
|
18592
19096
|
return {
|
|
18593
19097
|
input: messages,
|
|
@@ -18598,7 +19102,7 @@ var OpenRouterPlugin = class extends BasePlugin {
|
|
|
18598
19102
|
return isObject(result) ? result.choices : void 0;
|
|
18599
19103
|
},
|
|
18600
19104
|
extractMetrics: (result, startTime) => {
|
|
18601
|
-
const metrics = parseOpenRouterMetricsFromUsage2(_optionalChain([result, 'optionalAccess',
|
|
19105
|
+
const metrics = parseOpenRouterMetricsFromUsage2(_optionalChain([result, 'optionalAccess', _420 => _420.usage]));
|
|
18602
19106
|
if (startTime) {
|
|
18603
19107
|
metrics.time_to_first_token = getCurrentUnixTimestamp() - startTime;
|
|
18604
19108
|
}
|
|
@@ -18613,9 +19117,9 @@ var OpenRouterPlugin = class extends BasePlugin {
|
|
|
18613
19117
|
type: "llm" /* LLM */,
|
|
18614
19118
|
extractInput: (args) => {
|
|
18615
19119
|
const request = getOpenRouterRequestArg(args);
|
|
18616
|
-
const requestBody = isObject(_optionalChain([request, 'optionalAccess',
|
|
18617
|
-
const httpReferer = _optionalChain([request, 'optionalAccess',
|
|
18618
|
-
const xTitle = _optionalChain([request, 'optionalAccess',
|
|
19120
|
+
const requestBody = isObject(_optionalChain([request, 'optionalAccess', _421 => _421.requestBody])) ? request.requestBody : {};
|
|
19121
|
+
const httpReferer = _optionalChain([request, 'optionalAccess', _422 => _422.httpReferer]);
|
|
19122
|
+
const xTitle = _optionalChain([request, 'optionalAccess', _423 => _423.xTitle]);
|
|
18619
19123
|
const { input, ...metadata } = requestBody;
|
|
18620
19124
|
return {
|
|
18621
19125
|
input,
|
|
@@ -18630,7 +19134,7 @@ var OpenRouterPlugin = class extends BasePlugin {
|
|
|
18630
19134
|
if (!isObject(result)) {
|
|
18631
19135
|
return void 0;
|
|
18632
19136
|
}
|
|
18633
|
-
const embedding = _optionalChain([result, 'access',
|
|
19137
|
+
const embedding = _optionalChain([result, 'access', _424 => _424.data, 'optionalAccess', _425 => _425[0], 'optionalAccess', _426 => _426.embedding]);
|
|
18634
19138
|
return Array.isArray(embedding) ? { embedding_length: embedding.length } : void 0;
|
|
18635
19139
|
},
|
|
18636
19140
|
extractMetadata: (result) => {
|
|
@@ -18650,9 +19154,9 @@ var OpenRouterPlugin = class extends BasePlugin {
|
|
|
18650
19154
|
type: "llm" /* LLM */,
|
|
18651
19155
|
extractInput: (args) => {
|
|
18652
19156
|
const request = getOpenRouterRequestArg(args);
|
|
18653
|
-
const requestBody = isObject(_optionalChain([request, 'optionalAccess',
|
|
18654
|
-
const httpReferer = _optionalChain([request, 'optionalAccess',
|
|
18655
|
-
const xTitle = _nullishCoalesce(_optionalChain([request, 'optionalAccess',
|
|
19157
|
+
const requestBody = isObject(_optionalChain([request, 'optionalAccess', _427 => _427.requestBody])) ? request.requestBody : {};
|
|
19158
|
+
const httpReferer = _optionalChain([request, 'optionalAccess', _428 => _428.httpReferer]);
|
|
19159
|
+
const xTitle = _nullishCoalesce(_optionalChain([request, 'optionalAccess', _429 => _429.xTitle]), () => ( _optionalChain([request, 'optionalAccess', _430 => _430.appTitle])));
|
|
18656
19160
|
const { documents, query, ...metadata } = requestBody;
|
|
18657
19161
|
return {
|
|
18658
19162
|
input: {
|
|
@@ -18678,9 +19182,9 @@ var OpenRouterPlugin = class extends BasePlugin {
|
|
|
18678
19182
|
type: "llm" /* LLM */,
|
|
18679
19183
|
extractInput: (args) => {
|
|
18680
19184
|
const request = getOpenRouterRequestArg(args);
|
|
18681
|
-
const openResponsesRequest = isObject(_optionalChain([request, 'optionalAccess',
|
|
18682
|
-
const httpReferer = _optionalChain([request, 'optionalAccess',
|
|
18683
|
-
const xTitle = _optionalChain([request, 'optionalAccess',
|
|
19185
|
+
const openResponsesRequest = isObject(_optionalChain([request, 'optionalAccess', _431 => _431.openResponsesRequest])) ? request.openResponsesRequest : {};
|
|
19186
|
+
const httpReferer = _optionalChain([request, 'optionalAccess', _432 => _432.httpReferer]);
|
|
19187
|
+
const xTitle = _optionalChain([request, 'optionalAccess', _433 => _433.xTitle]);
|
|
18684
19188
|
const { input, ...metadata } = openResponsesRequest;
|
|
18685
19189
|
return {
|
|
18686
19190
|
input,
|
|
@@ -18690,7 +19194,7 @@ var OpenRouterPlugin = class extends BasePlugin {
|
|
|
18690
19194
|
extractOutput: (result) => extractOpenRouterResponseOutput2(result),
|
|
18691
19195
|
extractMetadata: (result) => extractOpenRouterResponseMetadata2(result),
|
|
18692
19196
|
extractMetrics: (result, startTime) => {
|
|
18693
|
-
const metrics = parseOpenRouterMetricsFromUsage2(_optionalChain([result, 'optionalAccess',
|
|
19197
|
+
const metrics = parseOpenRouterMetricsFromUsage2(_optionalChain([result, 'optionalAccess', _434 => _434.usage]));
|
|
18694
19198
|
if (startTime) {
|
|
18695
19199
|
metrics.time_to_first_token = getCurrentUnixTimestamp() - startTime;
|
|
18696
19200
|
}
|
|
@@ -18742,14 +19246,14 @@ var OpenRouterPlugin = class extends BasePlugin {
|
|
|
18742
19246
|
extractMetadata: (result, event) => {
|
|
18743
19247
|
if (!isObject(result)) {
|
|
18744
19248
|
return {
|
|
18745
|
-
step: _optionalChain([event, 'optionalAccess',
|
|
18746
|
-
step_type: _optionalChain([event, 'optionalAccess',
|
|
19249
|
+
step: _optionalChain([event, 'optionalAccess', _435 => _435.step]),
|
|
19250
|
+
step_type: _optionalChain([event, 'optionalAccess', _436 => _436.stepType])
|
|
18747
19251
|
};
|
|
18748
19252
|
}
|
|
18749
19253
|
return {
|
|
18750
19254
|
...extractOpenRouterResponseMetadata2(result) || {},
|
|
18751
|
-
..._optionalChain([event, 'optionalAccess',
|
|
18752
|
-
..._optionalChain([event, 'optionalAccess',
|
|
19255
|
+
..._optionalChain([event, 'optionalAccess', _437 => _437.step]) !== void 0 ? { step: event.step } : {},
|
|
19256
|
+
..._optionalChain([event, 'optionalAccess', _438 => _438.stepType]) ? { step_type: event.stepType } : {}
|
|
18753
19257
|
};
|
|
18754
19258
|
},
|
|
18755
19259
|
extractMetrics: (result) => isObject(result) ? parseOpenRouterMetricsFromUsage2(result.usage) : {}
|
|
@@ -19129,7 +19633,7 @@ function publishToolResult2(tracingChannel2, event, result) {
|
|
|
19129
19633
|
}
|
|
19130
19634
|
function getToolCallId2(context) {
|
|
19131
19635
|
const toolContext = context;
|
|
19132
|
-
return typeof _optionalChain([toolContext, 'optionalAccess',
|
|
19636
|
+
return typeof _optionalChain([toolContext, 'optionalAccess', _439 => _439.toolCall, 'optionalAccess', _440 => _440.id]) === "string" ? toolContext.toolCall.id : void 0;
|
|
19133
19637
|
}
|
|
19134
19638
|
function aggregateOpenRouterChatChunks(chunks) {
|
|
19135
19639
|
let role;
|
|
@@ -19145,12 +19649,12 @@ function aggregateOpenRouterChatChunks(chunks) {
|
|
|
19145
19649
|
for (const chunk of chunks) {
|
|
19146
19650
|
metrics = {
|
|
19147
19651
|
...metrics,
|
|
19148
|
-
...parseOpenRouterMetricsFromUsage2(_optionalChain([chunk, 'optionalAccess',
|
|
19652
|
+
...parseOpenRouterMetricsFromUsage2(_optionalChain([chunk, 'optionalAccess', _441 => _441.usage]))
|
|
19149
19653
|
};
|
|
19150
|
-
const choice = _optionalChain([chunk, 'optionalAccess',
|
|
19151
|
-
const delta = _optionalChain([choice, 'optionalAccess',
|
|
19654
|
+
const choice = _optionalChain([chunk, 'optionalAccess', _442 => _442.choices, 'optionalAccess', _443 => _443[0]]);
|
|
19655
|
+
const delta = _optionalChain([choice, 'optionalAccess', _444 => _444.delta]);
|
|
19152
19656
|
if (!delta) {
|
|
19153
|
-
if (_optionalChain([choice, 'optionalAccess',
|
|
19657
|
+
if (_optionalChain([choice, 'optionalAccess', _445 => _445.finish_reason]) !== void 0) {
|
|
19154
19658
|
finishReason = choice.finish_reason;
|
|
19155
19659
|
}
|
|
19156
19660
|
continue;
|
|
@@ -19176,7 +19680,7 @@ function aggregateOpenRouterChatChunks(chunks) {
|
|
|
19176
19680
|
...delta.reasoning_details
|
|
19177
19681
|
];
|
|
19178
19682
|
}
|
|
19179
|
-
const choiceFinishReason = _nullishCoalesce(_nullishCoalesce(_optionalChain([choice, 'optionalAccess',
|
|
19683
|
+
const choiceFinishReason = _nullishCoalesce(_nullishCoalesce(_optionalChain([choice, 'optionalAccess', _446 => _446.finishReason]), () => ( _optionalChain([choice, 'optionalAccess', _447 => _447.finish_reason]))), () => ( void 0));
|
|
19180
19684
|
const deltaFinishReason = _nullishCoalesce(_nullishCoalesce(delta.finishReason, () => ( delta.finish_reason)), () => ( void 0));
|
|
19181
19685
|
if (choiceFinishReason !== void 0) {
|
|
19182
19686
|
finishReason = choiceFinishReason;
|
|
@@ -19188,11 +19692,11 @@ function aggregateOpenRouterChatChunks(chunks) {
|
|
|
19188
19692
|
continue;
|
|
19189
19693
|
}
|
|
19190
19694
|
for (const toolDelta of toolCallDeltas) {
|
|
19191
|
-
if (!_optionalChain([toolDelta, 'optionalAccess',
|
|
19695
|
+
if (!_optionalChain([toolDelta, 'optionalAccess', _448 => _448.function])) {
|
|
19192
19696
|
continue;
|
|
19193
19697
|
}
|
|
19194
19698
|
const toolIndex = _nullishCoalesce(toolDelta.index, () => ( 0));
|
|
19195
|
-
const existingToolCall = _optionalChain([toolCalls, 'optionalAccess',
|
|
19699
|
+
const existingToolCall = _optionalChain([toolCalls, 'optionalAccess', _449 => _449[toolIndex]]);
|
|
19196
19700
|
if (!existingToolCall || toolDelta.id && existingToolCall.id !== void 0 && existingToolCall.id !== toolDelta.id) {
|
|
19197
19701
|
const nextToolCalls = [...toolCalls || []];
|
|
19198
19702
|
nextToolCalls[toolIndex] = {
|
|
@@ -19242,7 +19746,7 @@ function aggregateOpenRouterChatChunks(chunks) {
|
|
|
19242
19746
|
function aggregateOpenRouterResponseStreamEvents(chunks) {
|
|
19243
19747
|
let finalResponse;
|
|
19244
19748
|
for (const chunk of chunks) {
|
|
19245
|
-
const response = _optionalChain([chunk, 'optionalAccess',
|
|
19749
|
+
const response = _optionalChain([chunk, 'optionalAccess', _450 => _450.response]);
|
|
19246
19750
|
if (!response) {
|
|
19247
19751
|
continue;
|
|
19248
19752
|
}
|
|
@@ -19672,10 +20176,10 @@ var MistralPlugin = class extends BasePlugin {
|
|
|
19672
20176
|
type: "llm" /* LLM */,
|
|
19673
20177
|
extractInput: extractMessagesInputWithMetadata,
|
|
19674
20178
|
extractOutput: (result) => {
|
|
19675
|
-
return _optionalChain([result, 'optionalAccess',
|
|
20179
|
+
return _optionalChain([result, 'optionalAccess', _451 => _451.choices]);
|
|
19676
20180
|
},
|
|
19677
20181
|
extractMetadata: (result) => extractMistralResponseMetadata(result),
|
|
19678
|
-
extractMetrics: (result, startTime) => extractMistralMetrics(_optionalChain([result, 'optionalAccess',
|
|
20182
|
+
extractMetrics: (result, startTime) => extractMistralMetrics(_optionalChain([result, 'optionalAccess', _452 => _452.usage]), startTime)
|
|
19679
20183
|
})
|
|
19680
20184
|
);
|
|
19681
20185
|
this.unsubscribers.push(
|
|
@@ -19695,11 +20199,11 @@ var MistralPlugin = class extends BasePlugin {
|
|
|
19695
20199
|
type: "llm" /* LLM */,
|
|
19696
20200
|
extractInput: extractEmbeddingInputWithMetadata,
|
|
19697
20201
|
extractOutput: (result) => {
|
|
19698
|
-
const embedding = _optionalChain([result, 'optionalAccess',
|
|
20202
|
+
const embedding = _optionalChain([result, 'optionalAccess', _453 => _453.data, 'optionalAccess', _454 => _454[0], 'optionalAccess', _455 => _455.embedding]);
|
|
19699
20203
|
return Array.isArray(embedding) ? { embedding_length: embedding.length } : void 0;
|
|
19700
20204
|
},
|
|
19701
20205
|
extractMetadata: (result) => extractMistralResponseMetadata(result),
|
|
19702
|
-
extractMetrics: (result) => parseMistralMetricsFromUsage(_optionalChain([result, 'optionalAccess',
|
|
20206
|
+
extractMetrics: (result) => parseMistralMetricsFromUsage(_optionalChain([result, 'optionalAccess', _456 => _456.usage]))
|
|
19703
20207
|
})
|
|
19704
20208
|
);
|
|
19705
20209
|
this.unsubscribers.push(
|
|
@@ -19709,7 +20213,7 @@ var MistralPlugin = class extends BasePlugin {
|
|
|
19709
20213
|
extractInput: extractClassifierInputWithMetadata,
|
|
19710
20214
|
extractOutput: extractClassifierOutput,
|
|
19711
20215
|
extractMetadata: (result) => extractMistralResponseMetadata(result),
|
|
19712
|
-
extractMetrics: (result) => parseMistralMetricsFromUsage(_optionalChain([result, 'optionalAccess',
|
|
20216
|
+
extractMetrics: (result) => parseMistralMetricsFromUsage(_optionalChain([result, 'optionalAccess', _457 => _457.usage]))
|
|
19713
20217
|
})
|
|
19714
20218
|
);
|
|
19715
20219
|
this.unsubscribers.push(
|
|
@@ -19719,7 +20223,7 @@ var MistralPlugin = class extends BasePlugin {
|
|
|
19719
20223
|
extractInput: extractClassifierInputWithMetadata,
|
|
19720
20224
|
extractOutput: extractClassifierOutput,
|
|
19721
20225
|
extractMetadata: (result) => extractMistralResponseMetadata(result),
|
|
19722
|
-
extractMetrics: (result) => parseMistralMetricsFromUsage(_optionalChain([result, 'optionalAccess',
|
|
20226
|
+
extractMetrics: (result) => parseMistralMetricsFromUsage(_optionalChain([result, 'optionalAccess', _458 => _458.usage]))
|
|
19723
20227
|
})
|
|
19724
20228
|
);
|
|
19725
20229
|
this.unsubscribers.push(
|
|
@@ -19729,7 +20233,7 @@ var MistralPlugin = class extends BasePlugin {
|
|
|
19729
20233
|
extractInput: extractClassifierInputWithMetadata,
|
|
19730
20234
|
extractOutput: extractClassifierOutput,
|
|
19731
20235
|
extractMetadata: (result) => extractMistralResponseMetadata(result),
|
|
19732
|
-
extractMetrics: (result) => parseMistralMetricsFromUsage(_optionalChain([result, 'optionalAccess',
|
|
20236
|
+
extractMetrics: (result) => parseMistralMetricsFromUsage(_optionalChain([result, 'optionalAccess', _459 => _459.usage]))
|
|
19733
20237
|
})
|
|
19734
20238
|
);
|
|
19735
20239
|
this.unsubscribers.push(
|
|
@@ -19739,7 +20243,7 @@ var MistralPlugin = class extends BasePlugin {
|
|
|
19739
20243
|
extractInput: extractClassifierInputWithMetadata,
|
|
19740
20244
|
extractOutput: extractClassifierOutput,
|
|
19741
20245
|
extractMetadata: (result) => extractMistralResponseMetadata(result),
|
|
19742
|
-
extractMetrics: (result) => parseMistralMetricsFromUsage(_optionalChain([result, 'optionalAccess',
|
|
20246
|
+
extractMetrics: (result) => parseMistralMetricsFromUsage(_optionalChain([result, 'optionalAccess', _460 => _460.usage]))
|
|
19743
20247
|
})
|
|
19744
20248
|
);
|
|
19745
20249
|
this.unsubscribers.push(
|
|
@@ -19748,10 +20252,10 @@ var MistralPlugin = class extends BasePlugin {
|
|
|
19748
20252
|
type: "llm" /* LLM */,
|
|
19749
20253
|
extractInput: extractPromptInputWithMetadata,
|
|
19750
20254
|
extractOutput: (result) => {
|
|
19751
|
-
return _optionalChain([result, 'optionalAccess',
|
|
20255
|
+
return _optionalChain([result, 'optionalAccess', _461 => _461.choices]);
|
|
19752
20256
|
},
|
|
19753
20257
|
extractMetadata: (result) => extractMistralResponseMetadata(result),
|
|
19754
|
-
extractMetrics: (result, startTime) => extractMistralMetrics(_optionalChain([result, 'optionalAccess',
|
|
20258
|
+
extractMetrics: (result, startTime) => extractMistralMetrics(_optionalChain([result, 'optionalAccess', _462 => _462.usage]), startTime)
|
|
19755
20259
|
})
|
|
19756
20260
|
);
|
|
19757
20261
|
this.unsubscribers.push(
|
|
@@ -19771,10 +20275,10 @@ var MistralPlugin = class extends BasePlugin {
|
|
|
19771
20275
|
type: "llm" /* LLM */,
|
|
19772
20276
|
extractInput: extractMessagesInputWithMetadata,
|
|
19773
20277
|
extractOutput: (result) => {
|
|
19774
|
-
return _optionalChain([result, 'optionalAccess',
|
|
20278
|
+
return _optionalChain([result, 'optionalAccess', _463 => _463.choices]);
|
|
19775
20279
|
},
|
|
19776
20280
|
extractMetadata: (result) => extractMistralResponseMetadata(result),
|
|
19777
|
-
extractMetrics: (result, startTime) => extractMistralMetrics(_optionalChain([result, 'optionalAccess',
|
|
20281
|
+
extractMetrics: (result, startTime) => extractMistralMetrics(_optionalChain([result, 'optionalAccess', _464 => _464.usage]), startTime)
|
|
19778
20282
|
})
|
|
19779
20283
|
);
|
|
19780
20284
|
this.unsubscribers.push(
|
|
@@ -20029,11 +20533,11 @@ function mergeMistralContentParts(left, right) {
|
|
|
20029
20533
|
const merged = [...(left || []).map((part) => structuredClone(part))];
|
|
20030
20534
|
for (const part of right) {
|
|
20031
20535
|
const lastPart = merged[merged.length - 1];
|
|
20032
|
-
if (part.type === "text" && _optionalChain([lastPart, 'optionalAccess',
|
|
20536
|
+
if (part.type === "text" && _optionalChain([lastPart, 'optionalAccess', _465 => _465.type]) === "text" && typeof lastPart.text === "string" && typeof part.text === "string") {
|
|
20033
20537
|
lastPart.text += part.text;
|
|
20034
20538
|
continue;
|
|
20035
20539
|
}
|
|
20036
|
-
if (part.type === "thinking" && _optionalChain([lastPart, 'optionalAccess',
|
|
20540
|
+
if (part.type === "thinking" && _optionalChain([lastPart, 'optionalAccess', _466 => _466.type]) === "thinking" && Array.isArray(lastPart.thinking) && Array.isArray(part.thinking)) {
|
|
20037
20541
|
lastPart.thinking = mergeMistralTextSegments(
|
|
20038
20542
|
lastPart.thinking,
|
|
20039
20543
|
part.thinking
|
|
@@ -20088,13 +20592,13 @@ function createMergedToolCallDelta(delta) {
|
|
|
20088
20592
|
...delta,
|
|
20089
20593
|
function: {
|
|
20090
20594
|
...delta.function,
|
|
20091
|
-
arguments: typeof _optionalChain([delta, 'access',
|
|
20595
|
+
arguments: typeof _optionalChain([delta, 'access', _467 => _467.function, 'optionalAccess', _468 => _468.arguments]) === "string" ? delta.function.arguments : ""
|
|
20092
20596
|
}
|
|
20093
20597
|
};
|
|
20094
20598
|
}
|
|
20095
20599
|
function mergeToolCallDeltaPair(current, delta) {
|
|
20096
|
-
const currentArguments = typeof _optionalChain([current, 'access',
|
|
20097
|
-
const deltaArguments = typeof _optionalChain([delta, 'access',
|
|
20600
|
+
const currentArguments = typeof _optionalChain([current, 'access', _469 => _469.function, 'optionalAccess', _470 => _470.arguments]) === "string" ? current.function.arguments : "";
|
|
20601
|
+
const deltaArguments = typeof _optionalChain([delta, 'access', _471 => _471.function, 'optionalAccess', _472 => _472.arguments]) === "string" ? delta.function.arguments : "";
|
|
20098
20602
|
return {
|
|
20099
20603
|
...current,
|
|
20100
20604
|
...delta,
|
|
@@ -20197,7 +20701,7 @@ function aggregateMistralStreamChunks(chunks) {
|
|
|
20197
20701
|
let metrics = {};
|
|
20198
20702
|
let metadata;
|
|
20199
20703
|
for (const event of chunks) {
|
|
20200
|
-
const chunk = isMistralChatCompletionChunk(_optionalChain([event, 'optionalAccess',
|
|
20704
|
+
const chunk = isMistralChatCompletionChunk(_optionalChain([event, 'optionalAccess', _473 => _473.data])) ? event.data : void 0;
|
|
20201
20705
|
if (!chunk) {
|
|
20202
20706
|
continue;
|
|
20203
20707
|
}
|
|
@@ -20291,9 +20795,9 @@ var googleADKChannels = defineChannels("@google/adk", {
|
|
|
20291
20795
|
});
|
|
20292
20796
|
|
|
20293
20797
|
// src/instrumentation/plugins/google-adk-plugin.ts
|
|
20294
|
-
var GoogleADKPlugin = (
|
|
20295
|
-
|
|
20296
|
-
|
|
20798
|
+
var GoogleADKPlugin = (_class21 = class extends BasePlugin {constructor(...args5) { super(...args5); _class21.prototype.__init69.call(this);_class21.prototype.__init70.call(this); }
|
|
20799
|
+
__init69() {this.activeRunnerSpans = /* @__PURE__ */ new Map()}
|
|
20800
|
+
__init70() {this.activeAgentSpans = /* @__PURE__ */ new Map()}
|
|
20297
20801
|
onEnable() {
|
|
20298
20802
|
this.subscribeToRunnerRunAsync();
|
|
20299
20803
|
this.subscribeToAgentRunAsync();
|
|
@@ -20388,7 +20892,7 @@ var GoogleADKPlugin = (_class19 = class extends BasePlugin {constructor(...args4
|
|
|
20388
20892
|
};
|
|
20389
20893
|
tracingChannel2.subscribe(handlers);
|
|
20390
20894
|
this.unsubscribers.push(() => {
|
|
20391
|
-
_optionalChain([unbindCurrentSpanStore, 'optionalCall',
|
|
20895
|
+
_optionalChain([unbindCurrentSpanStore, 'optionalCall', _474 => _474()]);
|
|
20392
20896
|
tracingChannel2.unsubscribe(handlers);
|
|
20393
20897
|
});
|
|
20394
20898
|
}
|
|
@@ -20491,7 +20995,7 @@ var GoogleADKPlugin = (_class19 = class extends BasePlugin {constructor(...args4
|
|
|
20491
20995
|
};
|
|
20492
20996
|
tracingChannel2.subscribe(handlers);
|
|
20493
20997
|
this.unsubscribers.push(() => {
|
|
20494
|
-
_optionalChain([unbindCurrentSpanStore, 'optionalCall',
|
|
20998
|
+
_optionalChain([unbindCurrentSpanStore, 'optionalCall', _475 => _475()]);
|
|
20495
20999
|
tracingChannel2.unsubscribe(handlers);
|
|
20496
21000
|
});
|
|
20497
21001
|
}
|
|
@@ -20563,7 +21067,7 @@ var GoogleADKPlugin = (_class19 = class extends BasePlugin {constructor(...args4
|
|
|
20563
21067
|
tracingChannel2.unsubscribe(handlers);
|
|
20564
21068
|
});
|
|
20565
21069
|
}
|
|
20566
|
-
},
|
|
21070
|
+
}, _class21);
|
|
20567
21071
|
function ensureState(states, event, create) {
|
|
20568
21072
|
const existing = states.get(event);
|
|
20569
21073
|
if (existing) {
|
|
@@ -20638,10 +21142,10 @@ function bindAsyncIterableToCurrentSpan(stream, span) {
|
|
|
20638
21142
|
}
|
|
20639
21143
|
function bindCurrentSpanStoreToStart3(tracingChannel2, states, create) {
|
|
20640
21144
|
const state = _internalGetGlobalState();
|
|
20641
|
-
const contextManager = _optionalChain([state, 'optionalAccess',
|
|
21145
|
+
const contextManager = _optionalChain([state, 'optionalAccess', _476 => _476.contextManager]);
|
|
20642
21146
|
const startChannel = tracingChannel2.start;
|
|
20643
21147
|
const currentSpanStore = contextManager ? contextManager[BRAINTRUST_CURRENT_SPAN_STORE] : void 0;
|
|
20644
|
-
if (!_optionalChain([startChannel, 'optionalAccess',
|
|
21148
|
+
if (!_optionalChain([startChannel, 'optionalAccess', _477 => _477.bindStore]) || !currentSpanStore) {
|
|
20645
21149
|
return void 0;
|
|
20646
21150
|
}
|
|
20647
21151
|
startChannel.bindStore(currentSpanStore, (event) => {
|
|
@@ -20653,7 +21157,7 @@ function bindCurrentSpanStoreToStart3(tracingChannel2, states, create) {
|
|
|
20653
21157
|
return contextManager.wrapSpanForStore(span);
|
|
20654
21158
|
});
|
|
20655
21159
|
return () => {
|
|
20656
|
-
_optionalChain([startChannel, 'access',
|
|
21160
|
+
_optionalChain([startChannel, 'access', _478 => _478.unbindStore, 'optionalCall', _479 => _479(currentSpanStore)]);
|
|
20657
21161
|
};
|
|
20658
21162
|
}
|
|
20659
21163
|
function extractRunnerContextKey(paramsOrContext) {
|
|
@@ -20666,9 +21170,9 @@ function extractRunnerContextKey(paramsOrContext) {
|
|
|
20666
21170
|
return extractInvocationContextKey(invocationContext);
|
|
20667
21171
|
}
|
|
20668
21172
|
function extractInvocationContextKey(parentContext) {
|
|
20669
|
-
const session = _optionalChain([parentContext, 'optionalAccess',
|
|
20670
|
-
const userId = _optionalChain([session, 'optionalAccess',
|
|
20671
|
-
const sessionId = _optionalChain([session, 'optionalAccess',
|
|
21173
|
+
const session = _optionalChain([parentContext, 'optionalAccess', _480 => _480.session]);
|
|
21174
|
+
const userId = _optionalChain([session, 'optionalAccess', _481 => _481.userId]);
|
|
21175
|
+
const sessionId = _optionalChain([session, 'optionalAccess', _482 => _482.id]);
|
|
20672
21176
|
if (typeof userId !== "string" || typeof sessionId !== "string") {
|
|
20673
21177
|
return void 0;
|
|
20674
21178
|
}
|
|
@@ -20725,27 +21229,27 @@ function extractRunnerMetadata(paramsOrContext) {
|
|
|
20725
21229
|
metadata["google_adk.session_id"] = directSessionId;
|
|
20726
21230
|
}
|
|
20727
21231
|
const session = "session" in paramsOrContext ? paramsOrContext.session : void 0;
|
|
20728
|
-
if (metadata["google_adk.user_id"] === void 0 && typeof _optionalChain([session, 'optionalAccess',
|
|
21232
|
+
if (metadata["google_adk.user_id"] === void 0 && typeof _optionalChain([session, 'optionalAccess', _483 => _483.userId]) === "string") {
|
|
20729
21233
|
metadata["google_adk.user_id"] = session.userId;
|
|
20730
21234
|
}
|
|
20731
|
-
if (metadata["google_adk.session_id"] === void 0 && typeof _optionalChain([session, 'optionalAccess',
|
|
21235
|
+
if (metadata["google_adk.session_id"] === void 0 && typeof _optionalChain([session, 'optionalAccess', _484 => _484.id]) === "string") {
|
|
20732
21236
|
metadata["google_adk.session_id"] = session.id;
|
|
20733
21237
|
}
|
|
20734
21238
|
return metadata;
|
|
20735
21239
|
}
|
|
20736
21240
|
function extractAgentName(agent, parentContext) {
|
|
20737
|
-
if (typeof _optionalChain([agent, 'optionalAccess',
|
|
21241
|
+
if (typeof _optionalChain([agent, 'optionalAccess', _485 => _485.name]) === "string" && agent.name.length > 0) {
|
|
20738
21242
|
return agent.name;
|
|
20739
21243
|
}
|
|
20740
21244
|
if (!parentContext) {
|
|
20741
21245
|
return void 0;
|
|
20742
21246
|
}
|
|
20743
21247
|
const contextAgent = parentContext.agent;
|
|
20744
|
-
return typeof _optionalChain([contextAgent, 'optionalAccess',
|
|
21248
|
+
return typeof _optionalChain([contextAgent, 'optionalAccess', _486 => _486.name]) === "string" && contextAgent.name.length > 0 ? contextAgent.name : void 0;
|
|
20745
21249
|
}
|
|
20746
21250
|
function extractModelName(agent, parentContext) {
|
|
20747
|
-
const modelAgent = _nullishCoalesce(agent, () => ( _optionalChain([parentContext, 'optionalAccess',
|
|
20748
|
-
if (!_optionalChain([modelAgent, 'optionalAccess',
|
|
21251
|
+
const modelAgent = _nullishCoalesce(agent, () => ( _optionalChain([parentContext, 'optionalAccess', _487 => _487.agent])));
|
|
21252
|
+
if (!_optionalChain([modelAgent, 'optionalAccess', _488 => _488.model])) {
|
|
20749
21253
|
return;
|
|
20750
21254
|
}
|
|
20751
21255
|
if (typeof modelAgent.model === "string") {
|
|
@@ -20758,30 +21262,30 @@ function extractModelName(agent, parentContext) {
|
|
|
20758
21262
|
}
|
|
20759
21263
|
function extractToolCallId(req) {
|
|
20760
21264
|
const toolContext = req.toolContext;
|
|
20761
|
-
return _optionalChain([toolContext, 'optionalAccess',
|
|
21265
|
+
return _optionalChain([toolContext, 'optionalAccess', _489 => _489.functionCallId]);
|
|
20762
21266
|
}
|
|
20763
21267
|
function extractToolName2(req, tool) {
|
|
20764
|
-
if (typeof _optionalChain([tool, 'optionalAccess',
|
|
21268
|
+
if (typeof _optionalChain([tool, 'optionalAccess', _490 => _490.name]) === "string" && tool.name.length > 0) {
|
|
20765
21269
|
return tool.name;
|
|
20766
21270
|
}
|
|
20767
21271
|
const toolContext = req.toolContext;
|
|
20768
|
-
const invocationContext = _optionalChain([toolContext, 'optionalAccess',
|
|
20769
|
-
const invocationTool = _optionalChain([invocationContext, 'optionalAccess',
|
|
20770
|
-
const toolName = _optionalChain([invocationTool, 'optionalAccess',
|
|
21272
|
+
const invocationContext = _optionalChain([toolContext, 'optionalAccess', _491 => _491.invocationContext]);
|
|
21273
|
+
const invocationTool = _optionalChain([invocationContext, 'optionalAccess', _492 => _492.tool]);
|
|
21274
|
+
const toolName = _optionalChain([invocationTool, 'optionalAccess', _493 => _493.name]);
|
|
20771
21275
|
return typeof toolName === "string" && toolName.length > 0 ? toolName : void 0;
|
|
20772
21276
|
}
|
|
20773
21277
|
function extractToolAgentName(req) {
|
|
20774
21278
|
const toolContext = req.toolContext;
|
|
20775
|
-
const directName = _optionalChain([toolContext, 'optionalAccess',
|
|
21279
|
+
const directName = _optionalChain([toolContext, 'optionalAccess', _494 => _494.agentName]);
|
|
20776
21280
|
if (typeof directName === "string" && directName.length > 0) {
|
|
20777
21281
|
return directName;
|
|
20778
21282
|
}
|
|
20779
|
-
const invocationContext = _optionalChain([toolContext, 'optionalAccess',
|
|
21283
|
+
const invocationContext = _optionalChain([toolContext, 'optionalAccess', _495 => _495.invocationContext]);
|
|
20780
21284
|
return extractAgentName(void 0, invocationContext);
|
|
20781
21285
|
}
|
|
20782
21286
|
function findToolParentSpan(req, activeAgentSpans, activeRunnerSpans) {
|
|
20783
21287
|
const toolContext = req.toolContext;
|
|
20784
|
-
const invocationContext = _optionalChain([toolContext, 'optionalAccess',
|
|
21288
|
+
const invocationContext = _optionalChain([toolContext, 'optionalAccess', _496 => _496.invocationContext]);
|
|
20785
21289
|
const contextKey = extractInvocationContextKey(invocationContext);
|
|
20786
21290
|
const agentName = extractToolAgentName(req);
|
|
20787
21291
|
if (contextKey && agentName) {
|
|
@@ -21163,7 +21667,7 @@ function extractCohereResponseMetadata(result) {
|
|
|
21163
21667
|
RESPONSE_METADATA_ALLOWLIST2
|
|
21164
21668
|
);
|
|
21165
21669
|
const meta = isObject(result.meta) ? result.meta : void 0;
|
|
21166
|
-
const apiVersion = isObject(_optionalChain([meta, 'optionalAccess',
|
|
21670
|
+
const apiVersion = isObject(_optionalChain([meta, 'optionalAccess', _497 => _497.apiVersion])) && typeof meta.apiVersion.version === "string" && meta.apiVersion.version || isObject(_optionalChain([meta, 'optionalAccess', _498 => _498.api_version])) && typeof meta.api_version.version === "string" && meta.api_version.version;
|
|
21167
21671
|
const metadata = {
|
|
21168
21672
|
...responseMetadata,
|
|
21169
21673
|
...apiVersion ? { api_version: apiVersion } : {}
|
|
@@ -21324,13 +21828,13 @@ function getToolCallIndex2(toolCall, fallbackIndex) {
|
|
|
21324
21828
|
return typeof toolCall.index === "number" && Number.isInteger(toolCall.index) ? toolCall.index : fallbackIndex;
|
|
21325
21829
|
}
|
|
21326
21830
|
function appendToolCallDelta(existing, incoming) {
|
|
21327
|
-
const currentArguments = isObject(_optionalChain([existing, 'optionalAccess',
|
|
21831
|
+
const currentArguments = isObject(_optionalChain([existing, 'optionalAccess', _499 => _499.function])) && typeof existing.function.arguments === "string" ? existing.function.arguments : "";
|
|
21328
21832
|
const incomingArguments = isObject(incoming.function) && typeof incoming.function.arguments === "string" ? incoming.function.arguments : "";
|
|
21329
21833
|
return {
|
|
21330
21834
|
...existing,
|
|
21331
21835
|
...incoming,
|
|
21332
21836
|
function: {
|
|
21333
|
-
...isObject(_optionalChain([existing, 'optionalAccess',
|
|
21837
|
+
...isObject(_optionalChain([existing, 'optionalAccess', _500 => _500.function])) ? existing.function : {},
|
|
21334
21838
|
...isObject(incoming.function) ? incoming.function : {},
|
|
21335
21839
|
...incomingArguments ? { arguments: `${currentArguments}${incomingArguments}` } : {}
|
|
21336
21840
|
}
|
|
@@ -21586,7 +22090,7 @@ function aggregateCohereChatStreamChunks(chunks) {
|
|
|
21586
22090
|
...mergedToolCalls.length > 0 ? { toolCalls: mergedToolCalls } : {}
|
|
21587
22091
|
};
|
|
21588
22092
|
} else if (mergedToolCalls.length > 0 || role || mergedText.length > 0 || aggregatedContentBlocks.length > 0) {
|
|
21589
|
-
const textContent = mergedText.length > 0 ? mergedText : _optionalChain([aggregatedContentBlocks, 'access',
|
|
22093
|
+
const textContent = mergedText.length > 0 ? mergedText : _optionalChain([aggregatedContentBlocks, 'access', _501 => _501[0], 'optionalAccess', _502 => _502.type]) === "text" ? aggregatedContentBlocks[0].text : void 0;
|
|
21590
22094
|
output = {
|
|
21591
22095
|
...role ? { role } : {},
|
|
21592
22096
|
...textContent ? { content: textContent } : {},
|
|
@@ -21635,7 +22139,7 @@ var GroqPlugin = class extends BasePlugin {
|
|
|
21635
22139
|
metadata: { ...metadata, provider: "groq" }
|
|
21636
22140
|
};
|
|
21637
22141
|
},
|
|
21638
|
-
extractOutput: (result) => _optionalChain([result, 'optionalAccess',
|
|
22142
|
+
extractOutput: (result) => _optionalChain([result, 'optionalAccess', _503 => _503.choices]),
|
|
21639
22143
|
extractMetrics: (result, startTime) => {
|
|
21640
22144
|
const metrics = parseGroqMetrics(result);
|
|
21641
22145
|
if (startTime) {
|
|
@@ -21658,7 +22162,7 @@ var GroqPlugin = class extends BasePlugin {
|
|
|
21658
22162
|
};
|
|
21659
22163
|
},
|
|
21660
22164
|
extractOutput: (result) => {
|
|
21661
|
-
const embedding = _optionalChain([result, 'optionalAccess',
|
|
22165
|
+
const embedding = _optionalChain([result, 'optionalAccess', _504 => _504.data, 'optionalAccess', _505 => _505[0], 'optionalAccess', _506 => _506.embedding]);
|
|
21662
22166
|
return Array.isArray(embedding) ? { embedding_length: embedding.length } : void 0;
|
|
21663
22167
|
},
|
|
21664
22168
|
extractMetrics: (result) => parseGroqMetrics(result)
|
|
@@ -21670,8 +22174,8 @@ var GroqPlugin = class extends BasePlugin {
|
|
|
21670
22174
|
}
|
|
21671
22175
|
};
|
|
21672
22176
|
function parseGroqMetrics(result) {
|
|
21673
|
-
const metrics = parseMetricsFromUsage(_optionalChain([result, 'optionalAccess',
|
|
21674
|
-
const xGroq = _optionalChain([result, 'optionalAccess',
|
|
22177
|
+
const metrics = parseMetricsFromUsage(_optionalChain([result, 'optionalAccess', _507 => _507.usage]));
|
|
22178
|
+
const xGroq = _optionalChain([result, 'optionalAccess', _508 => _508.x_groq]);
|
|
21675
22179
|
if (!xGroq || typeof xGroq !== "object") {
|
|
21676
22180
|
return metrics;
|
|
21677
22181
|
}
|
|
@@ -21695,7 +22199,7 @@ function aggregateGroqChatCompletionChunks(chunks, streamResult, endEvent) {
|
|
|
21695
22199
|
);
|
|
21696
22200
|
const reasoning = aggregateGroqReasoning(chunks);
|
|
21697
22201
|
if (reasoning !== void 0) {
|
|
21698
|
-
const message = _optionalChain([aggregated, 'access',
|
|
22202
|
+
const message = _optionalChain([aggregated, 'access', _509 => _509.output, 'access', _510 => _510[0], 'optionalAccess', _511 => _511.message]);
|
|
21699
22203
|
if (message) {
|
|
21700
22204
|
message.reasoning = reasoning;
|
|
21701
22205
|
}
|
|
@@ -21708,8 +22212,8 @@ function aggregateGroqChatCompletionChunks(chunks, streamResult, endEvent) {
|
|
|
21708
22212
|
function aggregateGroqReasoning(chunks) {
|
|
21709
22213
|
let reasoning = "";
|
|
21710
22214
|
for (const chunk of chunks) {
|
|
21711
|
-
const delta = _optionalChain([chunk, 'access',
|
|
21712
|
-
const deltaReasoning = _optionalChain([delta, 'optionalAccess',
|
|
22215
|
+
const delta = _optionalChain([chunk, 'access', _512 => _512.choices, 'optionalAccess', _513 => _513[0], 'optionalAccess', _514 => _514.delta]);
|
|
22216
|
+
const deltaReasoning = _optionalChain([delta, 'optionalAccess', _515 => _515.reasoning]);
|
|
21713
22217
|
if (typeof deltaReasoning === "string") {
|
|
21714
22218
|
reasoning += deltaReasoning;
|
|
21715
22219
|
}
|
|
@@ -21766,8 +22270,8 @@ var GenkitPlugin = class extends BasePlugin {
|
|
|
21766
22270
|
type: "llm" /* LLM */,
|
|
21767
22271
|
extractInput: ([input]) => extractGenerateInput(input),
|
|
21768
22272
|
extractOutput: extractGenerateOutput,
|
|
21769
|
-
extractMetadata: (result, event) => extractGenerateResponseMetadata(result, _optionalChain([event, 'optionalAccess',
|
|
21770
|
-
extractMetrics: (result) => parseGenkitUsageMetrics(_optionalChain([result, 'optionalAccess',
|
|
22273
|
+
extractMetadata: (result, event) => extractGenerateResponseMetadata(result, _optionalChain([event, 'optionalAccess', _516 => _516.arguments, 'optionalAccess', _517 => _517[0]])),
|
|
22274
|
+
extractMetrics: (result) => parseGenkitUsageMetrics(_optionalChain([result, 'optionalAccess', _518 => _518.usage]))
|
|
21771
22275
|
})
|
|
21772
22276
|
);
|
|
21773
22277
|
this.unsubscribers.push(
|
|
@@ -21784,7 +22288,7 @@ var GenkitPlugin = class extends BasePlugin {
|
|
|
21784
22288
|
type: "function" /* FUNCTION */,
|
|
21785
22289
|
extractInput: ([params]) => extractEmbedInput(params),
|
|
21786
22290
|
extractOutput: (result) => summarizeEmbeddingResult(result),
|
|
21787
|
-
extractMetadata: (_result, event) => extractEmbedMetadata(_optionalChain([event, 'optionalAccess',
|
|
22291
|
+
extractMetadata: (_result, event) => extractEmbedMetadata(_optionalChain([event, 'optionalAccess', _519 => _519.arguments, 'optionalAccess', _520 => _520[0]])),
|
|
21788
22292
|
extractMetrics: () => ({})
|
|
21789
22293
|
})
|
|
21790
22294
|
);
|
|
@@ -21794,7 +22298,7 @@ var GenkitPlugin = class extends BasePlugin {
|
|
|
21794
22298
|
type: "function" /* FUNCTION */,
|
|
21795
22299
|
extractInput: ([params]) => extractEmbedManyInput(params),
|
|
21796
22300
|
extractOutput: summarizeEmbeddingResult,
|
|
21797
|
-
extractMetadata: (_result, event) => extractEmbedMetadata(_optionalChain([event, 'optionalAccess',
|
|
22301
|
+
extractMetadata: (_result, event) => extractEmbedMetadata(_optionalChain([event, 'optionalAccess', _521 => _521.arguments, 'optionalAccess', _522 => _522[0]])),
|
|
21798
22302
|
extractMetrics: () => ({})
|
|
21799
22303
|
})
|
|
21800
22304
|
);
|
|
@@ -21845,7 +22349,7 @@ var GenkitPlugin = class extends BasePlugin {
|
|
|
21845
22349
|
};
|
|
21846
22350
|
tracingChannel2.subscribe(handlers);
|
|
21847
22351
|
this.unsubscribers.push(() => {
|
|
21848
|
-
_optionalChain([unbindCurrentSpanStore, 'optionalCall',
|
|
22352
|
+
_optionalChain([unbindCurrentSpanStore, 'optionalCall', _523 => _523()]);
|
|
21849
22353
|
tracingChannel2.unsubscribe(handlers);
|
|
21850
22354
|
});
|
|
21851
22355
|
}
|
|
@@ -21893,7 +22397,7 @@ var GenkitPlugin = class extends BasePlugin {
|
|
|
21893
22397
|
};
|
|
21894
22398
|
tracingChannel2.subscribe(handlers);
|
|
21895
22399
|
this.unsubscribers.push(() => {
|
|
21896
|
-
_optionalChain([unbindCurrentSpanStore, 'optionalCall',
|
|
22400
|
+
_optionalChain([unbindCurrentSpanStore, 'optionalCall', _524 => _524()]);
|
|
21897
22401
|
tracingChannel2.unsubscribe(handlers);
|
|
21898
22402
|
});
|
|
21899
22403
|
}
|
|
@@ -21960,10 +22464,10 @@ function ensureActionSpanState(states, event, create) {
|
|
|
21960
22464
|
}
|
|
21961
22465
|
function bindActionCurrentSpanStoreToStart(tracingChannel2, states, create) {
|
|
21962
22466
|
const state = _internalGetGlobalState();
|
|
21963
|
-
const contextManager = _optionalChain([state, 'optionalAccess',
|
|
22467
|
+
const contextManager = _optionalChain([state, 'optionalAccess', _525 => _525.contextManager]);
|
|
21964
22468
|
const startChannel = tracingChannel2.start;
|
|
21965
22469
|
const currentSpanStore = contextManager ? contextManager[BRAINTRUST_CURRENT_SPAN_STORE] : void 0;
|
|
21966
|
-
if (!_optionalChain([startChannel, 'optionalAccess',
|
|
22470
|
+
if (!_optionalChain([startChannel, 'optionalAccess', _526 => _526.bindStore]) || !currentSpanStore) {
|
|
21967
22471
|
return void 0;
|
|
21968
22472
|
}
|
|
21969
22473
|
startChannel.bindStore(currentSpanStore, (event) => {
|
|
@@ -21975,7 +22479,7 @@ function bindActionCurrentSpanStoreToStart(tracingChannel2, states, create) {
|
|
|
21975
22479
|
return state2 ? contextManager.wrapSpanForStore(state2.span) : currentSpanStore.getStore();
|
|
21976
22480
|
});
|
|
21977
22481
|
return () => {
|
|
21978
|
-
_optionalChain([startChannel, 'access',
|
|
22482
|
+
_optionalChain([startChannel, 'access', _527 => _527.unbindStore, 'optionalCall', _528 => _528(currentSpanStore)]);
|
|
21979
22483
|
};
|
|
21980
22484
|
}
|
|
21981
22485
|
function normalizeInput(input) {
|
|
@@ -22022,25 +22526,25 @@ function extractGenerateOutput(result) {
|
|
|
22022
22526
|
}
|
|
22023
22527
|
function extractGenerateResponseMetadata(result, input) {
|
|
22024
22528
|
const normalized = input ? normalizeInput(input) : void 0;
|
|
22025
|
-
const request = isObject(_optionalChain([result, 'optionalAccess',
|
|
22529
|
+
const request = isObject(_optionalChain([result, 'optionalAccess', _529 => _529.request])) ? _optionalChain([result, 'optionalAccess', _530 => _530.request]) : isObject(normalized) ? normalized : void 0;
|
|
22026
22530
|
return {
|
|
22027
22531
|
...genkitProviderMetadata(),
|
|
22028
22532
|
...pickDefined({
|
|
22029
|
-
model: modelName(_nullishCoalesce(_optionalChain([result, 'optionalAccess',
|
|
22030
|
-
finishReason: _optionalChain([result, 'optionalAccess',
|
|
22031
|
-
finishMessage: _optionalChain([result, 'optionalAccess',
|
|
22533
|
+
model: modelName(_nullishCoalesce(_optionalChain([result, 'optionalAccess', _531 => _531.model]), () => ( _optionalChain([request, 'optionalAccess', _532 => _532.model])))),
|
|
22534
|
+
finishReason: _optionalChain([result, 'optionalAccess', _533 => _533.finishReason]),
|
|
22535
|
+
finishMessage: _optionalChain([result, 'optionalAccess', _534 => _534.finishMessage])
|
|
22032
22536
|
})
|
|
22033
22537
|
};
|
|
22034
22538
|
}
|
|
22035
22539
|
function extractEmbedInput(params) {
|
|
22036
22540
|
return {
|
|
22037
|
-
input: processInputAttachments(_optionalChain([params, 'optionalAccess',
|
|
22541
|
+
input: processInputAttachments(_optionalChain([params, 'optionalAccess', _535 => _535.content])),
|
|
22038
22542
|
metadata: extractEmbedMetadata(params)
|
|
22039
22543
|
};
|
|
22040
22544
|
}
|
|
22041
22545
|
function extractEmbedManyInput(params) {
|
|
22042
22546
|
return {
|
|
22043
|
-
input: processInputAttachments(_optionalChain([params, 'optionalAccess',
|
|
22547
|
+
input: processInputAttachments(_optionalChain([params, 'optionalAccess', _536 => _536.content])),
|
|
22044
22548
|
metadata: extractEmbedMetadata(params)
|
|
22045
22549
|
};
|
|
22046
22550
|
}
|
|
@@ -22048,7 +22552,7 @@ function extractEmbedMetadata(params) {
|
|
|
22048
22552
|
return {
|
|
22049
22553
|
...genkitProviderMetadata(),
|
|
22050
22554
|
...pickDefined({
|
|
22051
|
-
model: modelName(_optionalChain([params, 'optionalAccess',
|
|
22555
|
+
model: modelName(_optionalChain([params, 'optionalAccess', _537 => _537.embedder]))
|
|
22052
22556
|
})
|
|
22053
22557
|
};
|
|
22054
22558
|
}
|
|
@@ -22056,7 +22560,7 @@ function summarizeEmbeddingResult(result) {
|
|
|
22056
22560
|
if (Array.isArray(result)) {
|
|
22057
22561
|
return {
|
|
22058
22562
|
embedding_count: result.length,
|
|
22059
|
-
dimensions: Array.isArray(result[0]) || Array.isArray(_optionalChain([result, 'access',
|
|
22563
|
+
dimensions: Array.isArray(result[0]) || Array.isArray(_optionalChain([result, 'access', _538 => _538[0], 'optionalAccess', _539 => _539.embedding])) ? result[0].length || _optionalChain([result, 'access', _540 => _540[0], 'optionalAccess', _541 => _541.embedding, 'optionalAccess', _542 => _542.length]) : void 0
|
|
22060
22564
|
};
|
|
22061
22565
|
}
|
|
22062
22566
|
if (isObject(result) && Array.isArray(result.embeddings)) {
|
|
@@ -22083,7 +22587,7 @@ function patchGenerateStreamResult(result, span, startTime) {
|
|
|
22083
22587
|
finishSpan(async () => {
|
|
22084
22588
|
const streamedText = chunks.map((chunk) => safeGet(chunk, "text")).join("");
|
|
22085
22589
|
const response = await result.response;
|
|
22086
|
-
const metrics = parseGenkitUsageMetrics(_optionalChain([response, 'optionalAccess',
|
|
22590
|
+
const metrics = parseGenkitUsageMetrics(_optionalChain([response, 'optionalAccess', _543 => _543.usage]));
|
|
22087
22591
|
if (firstChunkTime !== void 0) {
|
|
22088
22592
|
metrics.time_to_first_token = firstChunkTime - startTime;
|
|
22089
22593
|
}
|
|
@@ -22176,22 +22680,22 @@ function extractActionMetadata(self) {
|
|
|
22176
22680
|
}
|
|
22177
22681
|
function extractActionSpanMetadata(args) {
|
|
22178
22682
|
const options = extractRunInNewSpanOptions(args);
|
|
22179
|
-
const labels = isObject(_optionalChain([options, 'optionalAccess',
|
|
22180
|
-
const metadata = isObject(_optionalChain([options, 'optionalAccess',
|
|
22181
|
-
const actionType = stringValue(_optionalChain([labels, 'optionalAccess',
|
|
22182
|
-
const name = stringValue(_optionalChain([metadata, 'optionalAccess',
|
|
22683
|
+
const labels = isObject(_optionalChain([options, 'optionalAccess', _544 => _544.labels])) ? options.labels : void 0;
|
|
22684
|
+
const metadata = isObject(_optionalChain([options, 'optionalAccess', _545 => _545.metadata])) ? options.metadata : void 0;
|
|
22685
|
+
const actionType = stringValue(_optionalChain([labels, 'optionalAccess', _546 => _546["genkit:metadata:subtype"]]));
|
|
22686
|
+
const name = stringValue(_optionalChain([metadata, 'optionalAccess', _547 => _547.name]));
|
|
22183
22687
|
if (!actionType || !name) {
|
|
22184
22688
|
return void 0;
|
|
22185
22689
|
}
|
|
22186
22690
|
return {
|
|
22187
22691
|
actionType,
|
|
22188
|
-
key: stringValue(_optionalChain([labels, 'optionalAccess',
|
|
22692
|
+
key: stringValue(_optionalChain([labels, 'optionalAccess', _548 => _548["genkit:key"]])),
|
|
22189
22693
|
name
|
|
22190
22694
|
};
|
|
22191
22695
|
}
|
|
22192
22696
|
function extractActionSpanInput(args) {
|
|
22193
22697
|
const options = extractRunInNewSpanOptions(args);
|
|
22194
|
-
if (!isObject(_optionalChain([options, 'optionalAccess',
|
|
22698
|
+
if (!isObject(_optionalChain([options, 'optionalAccess', _549 => _549.metadata]))) {
|
|
22195
22699
|
return void 0;
|
|
22196
22700
|
}
|
|
22197
22701
|
return options.metadata.input;
|
|
@@ -22204,7 +22708,7 @@ function shouldTraceAction(metadata, runStepName) {
|
|
|
22204
22708
|
if (runStepName) {
|
|
22205
22709
|
return true;
|
|
22206
22710
|
}
|
|
22207
|
-
switch (_optionalChain([metadata, 'optionalAccess',
|
|
22711
|
+
switch (_optionalChain([metadata, 'optionalAccess', _550 => _550.actionType])) {
|
|
22208
22712
|
case "model":
|
|
22209
22713
|
case "background-model":
|
|
22210
22714
|
case "embedder":
|
|
@@ -22214,8 +22718,8 @@ function shouldTraceAction(metadata, runStepName) {
|
|
|
22214
22718
|
}
|
|
22215
22719
|
}
|
|
22216
22720
|
function actionSpanName(metadata, runStepName) {
|
|
22217
|
-
const actionType = _optionalChain([metadata, 'optionalAccess',
|
|
22218
|
-
const name = _optionalChain([metadata, 'optionalAccess',
|
|
22721
|
+
const actionType = _optionalChain([metadata, 'optionalAccess', _551 => _551.actionType]);
|
|
22722
|
+
const name = _optionalChain([metadata, 'optionalAccess', _552 => _552.name]);
|
|
22219
22723
|
if (actionType && name) {
|
|
22220
22724
|
return `genkit.${actionType}: ${name}`;
|
|
22221
22725
|
}
|
|
@@ -22228,7 +22732,7 @@ function actionSpanName(metadata, runStepName) {
|
|
|
22228
22732
|
return "genkit.action";
|
|
22229
22733
|
}
|
|
22230
22734
|
function actionSpanType(metadata) {
|
|
22231
|
-
switch (_optionalChain([metadata, 'optionalAccess',
|
|
22735
|
+
switch (_optionalChain([metadata, 'optionalAccess', _553 => _553.actionType])) {
|
|
22232
22736
|
case "tool":
|
|
22233
22737
|
case "tool.v2":
|
|
22234
22738
|
return "tool" /* TOOL */;
|
|
@@ -22243,9 +22747,9 @@ function actionMetadataForLog(metadata, runStepName) {
|
|
|
22243
22747
|
return {
|
|
22244
22748
|
...genkitProviderMetadata(),
|
|
22245
22749
|
...pickDefined({
|
|
22246
|
-
"genkit.action_type": _optionalChain([metadata, 'optionalAccess',
|
|
22247
|
-
"genkit.action_name": _optionalChain([metadata, 'optionalAccess',
|
|
22248
|
-
"genkit.action_key": _optionalChain([metadata, 'optionalAccess',
|
|
22750
|
+
"genkit.action_type": _optionalChain([metadata, 'optionalAccess', _554 => _554.actionType]),
|
|
22751
|
+
"genkit.action_name": _optionalChain([metadata, 'optionalAccess', _555 => _555.name]),
|
|
22752
|
+
"genkit.action_key": _optionalChain([metadata, 'optionalAccess', _556 => _556.key]),
|
|
22249
22753
|
"genkit.run_name": runStepName
|
|
22250
22754
|
})
|
|
22251
22755
|
};
|
|
@@ -22708,10 +23212,10 @@ function injectTracingHooks2(config, state) {
|
|
|
22708
23212
|
const existingHooks = _nullishCoalesce(config.hooks, () => ( {}));
|
|
22709
23213
|
const onSessionEnd = async (input, invocation) => {
|
|
22710
23214
|
try {
|
|
22711
|
-
await _optionalChain([existingHooks, 'access',
|
|
23215
|
+
await _optionalChain([existingHooks, 'access', _557 => _557.onSessionEnd, 'optionalCall', _558 => _558(input, invocation)]);
|
|
22712
23216
|
} finally {
|
|
22713
23217
|
handleSessionEnd(state, input.reason, input.error);
|
|
22714
|
-
_optionalChain([state, 'access',
|
|
23218
|
+
_optionalChain([state, 'access', _559 => _559.unsubscribeEvents, 'optionalCall', _560 => _560()]);
|
|
22715
23219
|
}
|
|
22716
23220
|
};
|
|
22717
23221
|
config.hooks = {
|
|
@@ -22750,7 +23254,7 @@ function makeSessionHandlers(sessionStates, configArgIndex, includeProviderMetad
|
|
|
22750
23254
|
if (config.model) {
|
|
22751
23255
|
metadata["github_copilot.model"] = config.model;
|
|
22752
23256
|
}
|
|
22753
|
-
if (includeProviderMetadata && _optionalChain([config, 'access',
|
|
23257
|
+
if (includeProviderMetadata && _optionalChain([config, 'access', _561 => _561.provider, 'optionalAccess', _562 => _562.type])) {
|
|
22754
23258
|
metadata["github_copilot.provider_type"] = config.provider.type;
|
|
22755
23259
|
}
|
|
22756
23260
|
if (Object.keys(metadata).length > 0) {
|
|
@@ -22832,45 +23336,1755 @@ var GitHubCopilotPlugin = class extends BasePlugin {
|
|
|
22832
23336
|
}
|
|
22833
23337
|
};
|
|
22834
23338
|
|
|
22835
|
-
// src/instrumentation/
|
|
22836
|
-
|
|
22837
|
-
|
|
23339
|
+
// src/instrumentation/plugins/flue-channels.ts
|
|
23340
|
+
var flueChannels = defineChannels("@flue/runtime", {
|
|
23341
|
+
createContext: channel({
|
|
23342
|
+
channelName: "createFlueContext",
|
|
23343
|
+
kind: "sync-stream"
|
|
23344
|
+
}),
|
|
23345
|
+
openSession: channel({
|
|
23346
|
+
channelName: "Harness.openSession",
|
|
23347
|
+
kind: "async"
|
|
23348
|
+
}),
|
|
23349
|
+
contextEvent: channel({
|
|
23350
|
+
channelName: "context.event",
|
|
23351
|
+
kind: "sync-stream"
|
|
23352
|
+
}),
|
|
23353
|
+
prompt: channel({
|
|
23354
|
+
channelName: "session.prompt",
|
|
23355
|
+
kind: "async"
|
|
23356
|
+
}),
|
|
23357
|
+
skill: channel({
|
|
23358
|
+
channelName: "session.skill",
|
|
23359
|
+
kind: "async"
|
|
23360
|
+
}),
|
|
23361
|
+
task: channel({
|
|
23362
|
+
channelName: "session.task",
|
|
23363
|
+
kind: "async"
|
|
23364
|
+
}),
|
|
23365
|
+
compact: channel({
|
|
23366
|
+
channelName: "session.compact",
|
|
23367
|
+
kind: "async"
|
|
23368
|
+
})
|
|
23369
|
+
});
|
|
23370
|
+
|
|
23371
|
+
// src/wrappers/flue.ts
|
|
23372
|
+
var WRAPPED_FLUE_CONTEXT = /* @__PURE__ */ Symbol.for("braintrust.flue.wrapped-context");
|
|
23373
|
+
var WRAPPED_FLUE_HARNESS = /* @__PURE__ */ Symbol.for("braintrust.flue.wrapped-harness");
|
|
23374
|
+
var WRAPPED_FLUE_SESSION = /* @__PURE__ */ Symbol.for("braintrust.flue.wrapped-session");
|
|
23375
|
+
var SUBSCRIBED_FLUE_CONTEXT_EVENTS = /* @__PURE__ */ Symbol.for(
|
|
23376
|
+
"braintrust.flue.subscribed-context-events"
|
|
23377
|
+
);
|
|
23378
|
+
function patchFlueContextInPlace(ctx) {
|
|
23379
|
+
const context = ctx;
|
|
23380
|
+
if (context[WRAPPED_FLUE_CONTEXT]) {
|
|
23381
|
+
return ctx;
|
|
23382
|
+
}
|
|
23383
|
+
const originalInit = context.init.bind(context);
|
|
23384
|
+
try {
|
|
23385
|
+
Object.defineProperty(context, WRAPPED_FLUE_CONTEXT, {
|
|
23386
|
+
configurable: false,
|
|
23387
|
+
enumerable: false,
|
|
23388
|
+
value: true
|
|
23389
|
+
});
|
|
23390
|
+
Object.defineProperty(context, "init", {
|
|
23391
|
+
configurable: true,
|
|
23392
|
+
value: async function wrappedFlueInit(options) {
|
|
23393
|
+
const harness = await originalInit(options);
|
|
23394
|
+
return wrapFlueHarness(harness);
|
|
23395
|
+
},
|
|
23396
|
+
writable: true
|
|
23397
|
+
});
|
|
23398
|
+
} catch (e50) {
|
|
23399
|
+
}
|
|
23400
|
+
return ctx;
|
|
22838
23401
|
}
|
|
22839
|
-
|
|
22840
|
-
|
|
22841
|
-
|
|
22842
|
-
__init68() {this.openAICodexPlugin = null}
|
|
22843
|
-
__init69() {this.anthropicPlugin = null}
|
|
22844
|
-
__init70() {this.aiSDKPlugin = null}
|
|
22845
|
-
__init71() {this.claudeAgentSDKPlugin = null}
|
|
22846
|
-
__init72() {this.cursorSDKPlugin = null}
|
|
22847
|
-
__init73() {this.googleGenAIPlugin = null}
|
|
22848
|
-
__init74() {this.huggingFacePlugin = null}
|
|
22849
|
-
__init75() {this.openRouterPlugin = null}
|
|
22850
|
-
__init76() {this.openRouterAgentPlugin = null}
|
|
22851
|
-
__init77() {this.mistralPlugin = null}
|
|
22852
|
-
__init78() {this.googleADKPlugin = null}
|
|
22853
|
-
__init79() {this.coherePlugin = null}
|
|
22854
|
-
__init80() {this.groqPlugin = null}
|
|
22855
|
-
__init81() {this.genkitPlugin = null}
|
|
22856
|
-
__init82() {this.gitHubCopilotPlugin = null}
|
|
22857
|
-
constructor(config = {}) {
|
|
22858
|
-
super();_class20.prototype.__init67.call(this);_class20.prototype.__init68.call(this);_class20.prototype.__init69.call(this);_class20.prototype.__init70.call(this);_class20.prototype.__init71.call(this);_class20.prototype.__init72.call(this);_class20.prototype.__init73.call(this);_class20.prototype.__init74.call(this);_class20.prototype.__init75.call(this);_class20.prototype.__init76.call(this);_class20.prototype.__init77.call(this);_class20.prototype.__init78.call(this);_class20.prototype.__init79.call(this);_class20.prototype.__init80.call(this);_class20.prototype.__init81.call(this);_class20.prototype.__init82.call(this);;
|
|
22859
|
-
this.config = config;
|
|
23402
|
+
function subscribeFlueContextEvents(ctx, options = {}) {
|
|
23403
|
+
if (!ctx || typeof ctx !== "object" || typeof ctx.subscribeEvent !== "function") {
|
|
23404
|
+
return void 0;
|
|
22860
23405
|
}
|
|
22861
|
-
|
|
22862
|
-
|
|
22863
|
-
|
|
22864
|
-
|
|
22865
|
-
|
|
23406
|
+
const context = ctx;
|
|
23407
|
+
const captureTurnSpans = _nullishCoalesce(options.captureTurnSpans, () => ( true));
|
|
23408
|
+
const existingSubscription = context[SUBSCRIBED_FLUE_CONTEXT_EVENTS];
|
|
23409
|
+
if (existingSubscription) {
|
|
23410
|
+
if (existingSubscription.captureTurnSpans || !captureTurnSpans) {
|
|
23411
|
+
return void 0;
|
|
22866
23412
|
}
|
|
22867
|
-
|
|
22868
|
-
|
|
22869
|
-
|
|
23413
|
+
try {
|
|
23414
|
+
existingSubscription.unsubscribe();
|
|
23415
|
+
} catch (e51) {
|
|
22870
23416
|
}
|
|
22871
|
-
|
|
22872
|
-
|
|
22873
|
-
|
|
23417
|
+
}
|
|
23418
|
+
try {
|
|
23419
|
+
const unsubscribe = ctx.subscribeEvent((event) => {
|
|
23420
|
+
flueChannels.contextEvent.traceSync(() => void 0, {
|
|
23421
|
+
arguments: [event],
|
|
23422
|
+
captureTurnSpans,
|
|
23423
|
+
context: ctx
|
|
23424
|
+
});
|
|
23425
|
+
});
|
|
23426
|
+
if (existingSubscription) {
|
|
23427
|
+
existingSubscription.captureTurnSpans = captureTurnSpans;
|
|
23428
|
+
existingSubscription.unsubscribe = unsubscribe;
|
|
23429
|
+
} else {
|
|
23430
|
+
Object.defineProperty(context, SUBSCRIBED_FLUE_CONTEXT_EVENTS, {
|
|
23431
|
+
configurable: false,
|
|
23432
|
+
enumerable: false,
|
|
23433
|
+
value: {
|
|
23434
|
+
captureTurnSpans,
|
|
23435
|
+
unsubscribe
|
|
23436
|
+
}
|
|
23437
|
+
});
|
|
23438
|
+
}
|
|
23439
|
+
return unsubscribe;
|
|
23440
|
+
} catch (e52) {
|
|
23441
|
+
return void 0;
|
|
23442
|
+
}
|
|
23443
|
+
}
|
|
23444
|
+
function wrapFlueHarness(harness) {
|
|
23445
|
+
if (!isPlausibleFlueHarness(harness)) {
|
|
23446
|
+
return harness;
|
|
23447
|
+
}
|
|
23448
|
+
const target = harness;
|
|
23449
|
+
if (target[WRAPPED_FLUE_HARNESS]) {
|
|
23450
|
+
return harness;
|
|
23451
|
+
}
|
|
23452
|
+
const originalSession = target.session.bind(target);
|
|
23453
|
+
try {
|
|
23454
|
+
Object.defineProperty(target, WRAPPED_FLUE_HARNESS, {
|
|
23455
|
+
configurable: false,
|
|
23456
|
+
enumerable: false,
|
|
23457
|
+
value: true
|
|
23458
|
+
});
|
|
23459
|
+
Object.defineProperty(target, "session", {
|
|
23460
|
+
configurable: true,
|
|
23461
|
+
value: async function wrappedFlueHarnessSession(name, options) {
|
|
23462
|
+
const session = await originalSession(name, options);
|
|
23463
|
+
return patchFlueSessionInPlace(session);
|
|
23464
|
+
},
|
|
23465
|
+
writable: true
|
|
23466
|
+
});
|
|
23467
|
+
const sessions = target.sessions;
|
|
23468
|
+
if (sessions && typeof sessions === "object") {
|
|
23469
|
+
patchFlueSessionFactory(sessions, "get");
|
|
23470
|
+
patchFlueSessionFactory(sessions, "create");
|
|
23471
|
+
}
|
|
23472
|
+
} catch (e53) {
|
|
23473
|
+
}
|
|
23474
|
+
return harness;
|
|
23475
|
+
}
|
|
23476
|
+
function patchFlueSessionInPlace(session) {
|
|
23477
|
+
if (session[WRAPPED_FLUE_SESSION]) {
|
|
23478
|
+
return session;
|
|
23479
|
+
}
|
|
23480
|
+
try {
|
|
23481
|
+
Object.defineProperty(session, WRAPPED_FLUE_SESSION, {
|
|
23482
|
+
configurable: false,
|
|
23483
|
+
enumerable: false,
|
|
23484
|
+
value: true
|
|
23485
|
+
});
|
|
23486
|
+
patchCallHandleMethod(session, "prompt", flueChannels.prompt);
|
|
23487
|
+
patchCallHandleMethod(session, "skill", flueChannels.skill);
|
|
23488
|
+
patchCallHandleMethod(session, "task", flueChannels.task);
|
|
23489
|
+
patchCompact(session);
|
|
23490
|
+
} catch (e54) {
|
|
23491
|
+
}
|
|
23492
|
+
return session;
|
|
23493
|
+
}
|
|
23494
|
+
function patchFlueSessionFactory(sessions, method) {
|
|
23495
|
+
const original = sessions[method];
|
|
23496
|
+
if (typeof original !== "function") {
|
|
23497
|
+
return;
|
|
23498
|
+
}
|
|
23499
|
+
const bound = original.bind(sessions);
|
|
23500
|
+
Object.defineProperty(sessions, method, {
|
|
23501
|
+
configurable: true,
|
|
23502
|
+
value: async function wrappedFlueSessionFactory(name, options) {
|
|
23503
|
+
const session = await bound(name, options);
|
|
23504
|
+
return patchFlueSessionInPlace(session);
|
|
23505
|
+
},
|
|
23506
|
+
writable: true
|
|
23507
|
+
});
|
|
23508
|
+
}
|
|
23509
|
+
function patchCallHandleMethod(session, method, channel2) {
|
|
23510
|
+
const original = session[method];
|
|
23511
|
+
if (typeof original !== "function") {
|
|
23512
|
+
return;
|
|
23513
|
+
}
|
|
23514
|
+
const bound = original.bind(session);
|
|
23515
|
+
Object.defineProperty(session, method, {
|
|
23516
|
+
configurable: true,
|
|
23517
|
+
value(input, options) {
|
|
23518
|
+
const args = [input, options];
|
|
23519
|
+
const { originalResult, traced: traced2 } = traceFlueOperation(channel2, {
|
|
23520
|
+
context: {
|
|
23521
|
+
arguments: args,
|
|
23522
|
+
operation: method,
|
|
23523
|
+
session
|
|
23524
|
+
},
|
|
23525
|
+
run: () => bound(input, options)
|
|
23526
|
+
});
|
|
23527
|
+
return preserveCallHandle(originalResult, traced2);
|
|
23528
|
+
},
|
|
23529
|
+
writable: true
|
|
23530
|
+
});
|
|
23531
|
+
}
|
|
23532
|
+
function patchCompact(session) {
|
|
23533
|
+
const original = session.compact;
|
|
23534
|
+
if (typeof original !== "function") {
|
|
23535
|
+
return;
|
|
23536
|
+
}
|
|
23537
|
+
const bound = original.bind(session);
|
|
23538
|
+
Object.defineProperty(session, "compact", {
|
|
23539
|
+
configurable: true,
|
|
23540
|
+
value() {
|
|
23541
|
+
const context = {
|
|
23542
|
+
arguments: [],
|
|
23543
|
+
operation: "compact",
|
|
23544
|
+
session
|
|
23545
|
+
};
|
|
23546
|
+
return flueChannels.compact.tracePromise(() => bound(), context);
|
|
23547
|
+
},
|
|
23548
|
+
writable: true
|
|
23549
|
+
});
|
|
23550
|
+
}
|
|
23551
|
+
function traceFlueOperation(channel2, args) {
|
|
23552
|
+
const tracingChannel2 = channel2.tracingChannel();
|
|
23553
|
+
const context = args.context;
|
|
23554
|
+
let originalResult;
|
|
23555
|
+
let traced2;
|
|
23556
|
+
const run = () => {
|
|
23557
|
+
try {
|
|
23558
|
+
originalResult = args.run();
|
|
23559
|
+
_optionalChain([tracingChannel2, 'access', _563 => _563.end, 'optionalAccess', _564 => _564.publish, 'call', _565 => _565(context)]);
|
|
23560
|
+
} catch (error) {
|
|
23561
|
+
context.error = normalizeError3(error);
|
|
23562
|
+
_optionalChain([tracingChannel2, 'access', _566 => _566.error, 'optionalAccess', _567 => _567.publish, 'call', _568 => _568(context)]);
|
|
23563
|
+
_optionalChain([tracingChannel2, 'access', _569 => _569.end, 'optionalAccess', _570 => _570.publish, 'call', _571 => _571(context)]);
|
|
23564
|
+
throw error;
|
|
23565
|
+
}
|
|
23566
|
+
traced2 = Promise.resolve(originalResult).then(
|
|
23567
|
+
(result) => {
|
|
23568
|
+
context.result = result;
|
|
23569
|
+
_optionalChain([tracingChannel2, 'access', _572 => _572.asyncStart, 'optionalAccess', _573 => _573.publish, 'call', _574 => _574(context)]);
|
|
23570
|
+
_optionalChain([tracingChannel2, 'access', _575 => _575.asyncEnd, 'optionalAccess', _576 => _576.publish, 'call', _577 => _577(context)]);
|
|
23571
|
+
return result;
|
|
23572
|
+
},
|
|
23573
|
+
(error) => {
|
|
23574
|
+
context.error = normalizeError3(error);
|
|
23575
|
+
_optionalChain([tracingChannel2, 'access', _578 => _578.error, 'optionalAccess', _579 => _579.publish, 'call', _580 => _580(context)]);
|
|
23576
|
+
_optionalChain([tracingChannel2, 'access', _581 => _581.asyncStart, 'optionalAccess', _582 => _582.publish, 'call', _583 => _583(context)]);
|
|
23577
|
+
_optionalChain([tracingChannel2, 'access', _584 => _584.asyncEnd, 'optionalAccess', _585 => _585.publish, 'call', _586 => _586(context)]);
|
|
23578
|
+
throw error;
|
|
23579
|
+
}
|
|
23580
|
+
);
|
|
23581
|
+
};
|
|
23582
|
+
if (_optionalChain([tracingChannel2, 'access', _587 => _587.start, 'optionalAccess', _588 => _588.runStores])) {
|
|
23583
|
+
tracingChannel2.start.runStores(context, run);
|
|
23584
|
+
} else {
|
|
23585
|
+
_optionalChain([tracingChannel2, 'access', _589 => _589.start, 'optionalAccess', _590 => _590.publish, 'call', _591 => _591(context)]);
|
|
23586
|
+
run();
|
|
23587
|
+
}
|
|
23588
|
+
return { originalResult, traced: traced2 };
|
|
23589
|
+
}
|
|
23590
|
+
function normalizeError3(error) {
|
|
23591
|
+
return error instanceof Error ? error : new Error(String(error));
|
|
23592
|
+
}
|
|
23593
|
+
function preserveCallHandle(originalHandle, traced2) {
|
|
23594
|
+
if (!isFlueCallHandle(originalHandle)) {
|
|
23595
|
+
return traced2;
|
|
23596
|
+
}
|
|
23597
|
+
const handle = originalHandle;
|
|
23598
|
+
const wrapped = {
|
|
23599
|
+
get signal() {
|
|
23600
|
+
return handle.signal;
|
|
23601
|
+
},
|
|
23602
|
+
abort(reason) {
|
|
23603
|
+
return handle.abort(reason);
|
|
23604
|
+
},
|
|
23605
|
+
then(onfulfilled, onrejected) {
|
|
23606
|
+
return traced2.then(onfulfilled, onrejected);
|
|
23607
|
+
}
|
|
23608
|
+
};
|
|
23609
|
+
return wrapped;
|
|
23610
|
+
}
|
|
23611
|
+
function isPlausibleFlueHarness(value) {
|
|
23612
|
+
return !!value && typeof value === "object" && typeof value.session === "function";
|
|
23613
|
+
}
|
|
23614
|
+
function isFlueCallHandle(value) {
|
|
23615
|
+
return !!value && typeof value === "object" && typeof value.then === "function" && typeof value.abort === "function" && "signal" in value;
|
|
23616
|
+
}
|
|
23617
|
+
|
|
23618
|
+
// src/instrumentation/plugins/flue-plugin.ts
|
|
23619
|
+
var FluePlugin = (_class22 = class extends BasePlugin {constructor(...args6) { super(...args6); _class22.prototype.__init71.call(this);_class22.prototype.__init72.call(this);_class22.prototype.__init73.call(this);_class22.prototype.__init74.call(this);_class22.prototype.__init75.call(this);_class22.prototype.__init76.call(this);_class22.prototype.__init77.call(this); }
|
|
23620
|
+
__init71() {this.activeOperationsById = /* @__PURE__ */ new Map()}
|
|
23621
|
+
__init72() {this.activeOperationsByScope = /* @__PURE__ */ new Map()}
|
|
23622
|
+
__init73() {this.compactionsByScope = /* @__PURE__ */ new Map()}
|
|
23623
|
+
__init74() {this.pendingOperationsByKey = /* @__PURE__ */ new Map()}
|
|
23624
|
+
__init75() {this.tasksById = /* @__PURE__ */ new Map()}
|
|
23625
|
+
__init76() {this.toolsById = /* @__PURE__ */ new Map()}
|
|
23626
|
+
__init77() {this.turnsByScope = /* @__PURE__ */ new Map()}
|
|
23627
|
+
onEnable() {
|
|
23628
|
+
this.subscribeToContextCreation();
|
|
23629
|
+
this.subscribeToSessionCreation();
|
|
23630
|
+
this.subscribeToContextEvents();
|
|
23631
|
+
this.subscribeToSessionOperations();
|
|
23632
|
+
}
|
|
23633
|
+
onDisable() {
|
|
23634
|
+
for (const unsubscribe of this.unsubscribers) {
|
|
23635
|
+
unsubscribe();
|
|
23636
|
+
}
|
|
23637
|
+
this.unsubscribers = [];
|
|
23638
|
+
this.activeOperationsById.clear();
|
|
23639
|
+
this.activeOperationsByScope.clear();
|
|
23640
|
+
this.compactionsByScope.clear();
|
|
23641
|
+
this.pendingOperationsByKey.clear();
|
|
23642
|
+
this.tasksById.clear();
|
|
23643
|
+
this.toolsById.clear();
|
|
23644
|
+
this.turnsByScope.clear();
|
|
23645
|
+
}
|
|
23646
|
+
subscribeToContextCreation() {
|
|
23647
|
+
const channel2 = flueChannels.createContext.tracingChannel();
|
|
23648
|
+
const handlers = {
|
|
23649
|
+
end: (event) => {
|
|
23650
|
+
const ctx = event.result;
|
|
23651
|
+
if (!ctx) {
|
|
23652
|
+
return;
|
|
23653
|
+
}
|
|
23654
|
+
subscribeFlueContextEvents(ctx, { captureTurnSpans: false });
|
|
23655
|
+
patchFlueContextInPlace(ctx);
|
|
23656
|
+
},
|
|
23657
|
+
error: () => {
|
|
23658
|
+
}
|
|
23659
|
+
};
|
|
23660
|
+
channel2.subscribe(handlers);
|
|
23661
|
+
this.unsubscribers.push(() => {
|
|
23662
|
+
channel2.unsubscribe(handlers);
|
|
23663
|
+
});
|
|
23664
|
+
}
|
|
23665
|
+
subscribeToSessionCreation() {
|
|
23666
|
+
const channel2 = flueChannels.openSession.tracingChannel();
|
|
23667
|
+
const handlers = {
|
|
23668
|
+
asyncEnd: (event) => {
|
|
23669
|
+
if (event.result) {
|
|
23670
|
+
patchFlueSessionInPlace(
|
|
23671
|
+
event.result
|
|
23672
|
+
);
|
|
23673
|
+
}
|
|
23674
|
+
if (event.harness) {
|
|
23675
|
+
wrapFlueHarness(event.harness);
|
|
23676
|
+
}
|
|
23677
|
+
},
|
|
23678
|
+
error: () => {
|
|
23679
|
+
}
|
|
23680
|
+
};
|
|
23681
|
+
channel2.subscribe(handlers);
|
|
23682
|
+
this.unsubscribers.push(() => {
|
|
23683
|
+
channel2.unsubscribe(handlers);
|
|
23684
|
+
});
|
|
23685
|
+
}
|
|
23686
|
+
subscribeToSessionOperations() {
|
|
23687
|
+
this.subscribeToSessionOperation(flueChannels.prompt);
|
|
23688
|
+
this.subscribeToSessionOperation(flueChannels.skill);
|
|
23689
|
+
this.subscribeToSessionOperation(flueChannels.task);
|
|
23690
|
+
this.subscribeToCompact();
|
|
23691
|
+
}
|
|
23692
|
+
subscribeToSessionOperation(channel2) {
|
|
23693
|
+
const tracingChannel2 = channel2.tracingChannel();
|
|
23694
|
+
const states = /* @__PURE__ */ new WeakMap();
|
|
23695
|
+
const ensureState2 = (event) => {
|
|
23696
|
+
const existing = states.get(event);
|
|
23697
|
+
if (existing) {
|
|
23698
|
+
return existing;
|
|
23699
|
+
}
|
|
23700
|
+
const state = this.startOperationState({
|
|
23701
|
+
args: event.arguments,
|
|
23702
|
+
moduleVersion: typeof event.moduleVersion === "string" ? event.moduleVersion : void 0,
|
|
23703
|
+
operation: event.operation,
|
|
23704
|
+
session: event.session
|
|
23705
|
+
});
|
|
23706
|
+
states.set(event, state);
|
|
23707
|
+
return state;
|
|
23708
|
+
};
|
|
23709
|
+
const unbindCurrentSpanStore = this.bindCurrentSpanStoreToOperationStart(
|
|
23710
|
+
tracingChannel2,
|
|
23711
|
+
ensureState2
|
|
23712
|
+
);
|
|
23713
|
+
const handlers = {
|
|
23714
|
+
start: (event) => {
|
|
23715
|
+
ensureState2(event);
|
|
23716
|
+
},
|
|
23717
|
+
asyncEnd: (event) => {
|
|
23718
|
+
this.endOperationState(states.get(event), event.result);
|
|
23719
|
+
states.delete(event);
|
|
23720
|
+
},
|
|
23721
|
+
error: (event) => {
|
|
23722
|
+
const state = states.get(event);
|
|
23723
|
+
if (state && event.error) {
|
|
23724
|
+
safeLog3(state.span, { error: errorToString(event.error) });
|
|
23725
|
+
this.finishOperationState(state);
|
|
23726
|
+
}
|
|
23727
|
+
states.delete(event);
|
|
23728
|
+
}
|
|
23729
|
+
};
|
|
23730
|
+
tracingChannel2.subscribe(handlers);
|
|
23731
|
+
this.unsubscribers.push(() => {
|
|
23732
|
+
_optionalChain([unbindCurrentSpanStore, 'optionalCall', _592 => _592()]);
|
|
23733
|
+
tracingChannel2.unsubscribe(handlers);
|
|
23734
|
+
});
|
|
23735
|
+
}
|
|
23736
|
+
subscribeToCompact() {
|
|
23737
|
+
const tracingChannel2 = flueChannels.compact.tracingChannel();
|
|
23738
|
+
const states = /* @__PURE__ */ new WeakMap();
|
|
23739
|
+
const ensureState2 = (event) => {
|
|
23740
|
+
const existing = states.get(event);
|
|
23741
|
+
if (existing) {
|
|
23742
|
+
return existing;
|
|
23743
|
+
}
|
|
23744
|
+
const state = this.startOperationState({
|
|
23745
|
+
args: [],
|
|
23746
|
+
moduleVersion: typeof event.moduleVersion === "string" ? event.moduleVersion : void 0,
|
|
23747
|
+
operation: event.operation,
|
|
23748
|
+
session: event.session
|
|
23749
|
+
});
|
|
23750
|
+
states.set(event, state);
|
|
23751
|
+
return state;
|
|
23752
|
+
};
|
|
23753
|
+
const unbindCurrentSpanStore = this.bindCurrentSpanStoreToOperationStart(
|
|
23754
|
+
tracingChannel2,
|
|
23755
|
+
ensureState2
|
|
23756
|
+
);
|
|
23757
|
+
const handlers = {
|
|
23758
|
+
start: (event) => {
|
|
23759
|
+
ensureState2(event);
|
|
23760
|
+
},
|
|
23761
|
+
asyncEnd: (event) => {
|
|
23762
|
+
this.endOperationState(states.get(event), void 0);
|
|
23763
|
+
states.delete(event);
|
|
23764
|
+
},
|
|
23765
|
+
error: (event) => {
|
|
23766
|
+
const state = states.get(event);
|
|
23767
|
+
if (state && event.error) {
|
|
23768
|
+
safeLog3(state.span, { error: errorToString(event.error) });
|
|
23769
|
+
this.finishOperationState(state);
|
|
23770
|
+
}
|
|
23771
|
+
states.delete(event);
|
|
23772
|
+
}
|
|
23773
|
+
};
|
|
23774
|
+
tracingChannel2.subscribe(handlers);
|
|
23775
|
+
this.unsubscribers.push(() => {
|
|
23776
|
+
_optionalChain([unbindCurrentSpanStore, 'optionalCall', _593 => _593()]);
|
|
23777
|
+
tracingChannel2.unsubscribe(handlers);
|
|
23778
|
+
});
|
|
23779
|
+
}
|
|
23780
|
+
subscribeToContextEvents() {
|
|
23781
|
+
const channel2 = flueChannels.contextEvent.tracingChannel();
|
|
23782
|
+
const handlers = {
|
|
23783
|
+
start: (event) => {
|
|
23784
|
+
const flueEvent = event.arguments[0];
|
|
23785
|
+
if (!flueEvent) {
|
|
23786
|
+
return;
|
|
23787
|
+
}
|
|
23788
|
+
try {
|
|
23789
|
+
this.handleFlueEvent(flueEvent, {
|
|
23790
|
+
captureTurnSpans: event.captureTurnSpans !== false
|
|
23791
|
+
});
|
|
23792
|
+
} catch (error) {
|
|
23793
|
+
logInstrumentationError3("Flue event", error);
|
|
23794
|
+
}
|
|
23795
|
+
},
|
|
23796
|
+
error: () => {
|
|
23797
|
+
}
|
|
23798
|
+
};
|
|
23799
|
+
channel2.subscribe(handlers);
|
|
23800
|
+
this.unsubscribers.push(() => {
|
|
23801
|
+
channel2.unsubscribe(handlers);
|
|
23802
|
+
});
|
|
23803
|
+
}
|
|
23804
|
+
bindCurrentSpanStoreToOperationStart(tracingChannel2, ensureState2) {
|
|
23805
|
+
const state = _internalGetGlobalState();
|
|
23806
|
+
const startChannel = tracingChannel2.start;
|
|
23807
|
+
const contextManager = _optionalChain([state, 'optionalAccess', _594 => _594.contextManager]);
|
|
23808
|
+
const currentSpanStore = contextManager ? contextManager[BRAINTRUST_CURRENT_SPAN_STORE] : void 0;
|
|
23809
|
+
if (!currentSpanStore || !startChannel) {
|
|
23810
|
+
return void 0;
|
|
23811
|
+
}
|
|
23812
|
+
startChannel.bindStore(currentSpanStore, (event) => {
|
|
23813
|
+
const operationState = ensureState2(event);
|
|
23814
|
+
return contextManager.wrapSpanForStore(operationState.span);
|
|
23815
|
+
});
|
|
23816
|
+
return () => {
|
|
23817
|
+
startChannel.unbindStore(currentSpanStore);
|
|
23818
|
+
};
|
|
23819
|
+
}
|
|
23820
|
+
startOperationState(args) {
|
|
23821
|
+
const sessionName = getSessionName(args.session);
|
|
23822
|
+
const metadata = {
|
|
23823
|
+
...extractOperationInputMetadata(args.operation, args.args),
|
|
23824
|
+
...extractSessionMetadata(args.session),
|
|
23825
|
+
"flue.operation": args.operation,
|
|
23826
|
+
provider: "flue",
|
|
23827
|
+
...args.moduleVersion ? { "flue.version": args.moduleVersion } : {}
|
|
23828
|
+
};
|
|
23829
|
+
const span = startSpan({
|
|
23830
|
+
name: `flue.session.${args.operation}`,
|
|
23831
|
+
spanAttributes: { type: "task" /* TASK */ }
|
|
23832
|
+
});
|
|
23833
|
+
const state = {
|
|
23834
|
+
metadata,
|
|
23835
|
+
operation: args.operation,
|
|
23836
|
+
sessionName,
|
|
23837
|
+
span,
|
|
23838
|
+
startTime: getCurrentUnixTimestamp()
|
|
23839
|
+
};
|
|
23840
|
+
safeLog3(span, {
|
|
23841
|
+
input: extractOperationInput(args.operation, args.args),
|
|
23842
|
+
metadata
|
|
23843
|
+
});
|
|
23844
|
+
this.pendingOperationQueue(operationKey(sessionName, args.operation)).push(
|
|
23845
|
+
state
|
|
23846
|
+
);
|
|
23847
|
+
addOperationToScope(
|
|
23848
|
+
this.activeOperationsByScope,
|
|
23849
|
+
_nullishCoalesce(sessionName, () => ( "unknown")),
|
|
23850
|
+
state
|
|
23851
|
+
);
|
|
23852
|
+
return state;
|
|
23853
|
+
}
|
|
23854
|
+
endOperationState(state, result) {
|
|
23855
|
+
if (!state) {
|
|
23856
|
+
return;
|
|
23857
|
+
}
|
|
23858
|
+
const metadata = {
|
|
23859
|
+
...state.metadata,
|
|
23860
|
+
...extractPromptResponseMetadata(result)
|
|
23861
|
+
};
|
|
23862
|
+
const metrics = {
|
|
23863
|
+
...buildDurationMetrics3(state.startTime),
|
|
23864
|
+
...metricsFromUsage(_optionalChain([result, 'optionalAccess', _595 => _595.usage]))
|
|
23865
|
+
};
|
|
23866
|
+
safeLog3(state.span, {
|
|
23867
|
+
metadata,
|
|
23868
|
+
metrics,
|
|
23869
|
+
output: extractOperationOutput(result)
|
|
23870
|
+
});
|
|
23871
|
+
this.finishCompactionsForOperation(state);
|
|
23872
|
+
this.finishOperationState(state);
|
|
23873
|
+
}
|
|
23874
|
+
finishOperationState(state) {
|
|
23875
|
+
removePendingOperation(this.pendingOperationsByKey, state);
|
|
23876
|
+
if (state.operationId) {
|
|
23877
|
+
this.activeOperationsById.delete(state.operationId);
|
|
23878
|
+
}
|
|
23879
|
+
removeScopedOperation(this.activeOperationsByScope, state);
|
|
23880
|
+
state.span.end();
|
|
23881
|
+
}
|
|
23882
|
+
handleFlueEvent(event, options) {
|
|
23883
|
+
switch (event.type) {
|
|
23884
|
+
case "operation_start":
|
|
23885
|
+
this.handleOperationStart(event);
|
|
23886
|
+
return;
|
|
23887
|
+
case "operation":
|
|
23888
|
+
this.handleOperation(event);
|
|
23889
|
+
return;
|
|
23890
|
+
case "text_delta":
|
|
23891
|
+
if (!options.captureTurnSpans) {
|
|
23892
|
+
return;
|
|
23893
|
+
}
|
|
23894
|
+
this.ensureTurnState(event).text.push(
|
|
23895
|
+
typeof event.text === "string" ? event.text : ""
|
|
23896
|
+
);
|
|
23897
|
+
return;
|
|
23898
|
+
case "thinking_start":
|
|
23899
|
+
if (!options.captureTurnSpans) {
|
|
23900
|
+
return;
|
|
23901
|
+
}
|
|
23902
|
+
this.handleThinkingStart(event);
|
|
23903
|
+
return;
|
|
23904
|
+
case "thinking_delta":
|
|
23905
|
+
if (!options.captureTurnSpans) {
|
|
23906
|
+
return;
|
|
23907
|
+
}
|
|
23908
|
+
this.handleThinkingDelta(event);
|
|
23909
|
+
return;
|
|
23910
|
+
case "thinking_end":
|
|
23911
|
+
if (!options.captureTurnSpans) {
|
|
23912
|
+
return;
|
|
23913
|
+
}
|
|
23914
|
+
this.handleThinkingEnd(event);
|
|
23915
|
+
return;
|
|
23916
|
+
case "turn":
|
|
23917
|
+
if (!options.captureTurnSpans) {
|
|
23918
|
+
return;
|
|
23919
|
+
}
|
|
23920
|
+
this.handleTurn(event);
|
|
23921
|
+
return;
|
|
23922
|
+
case "tool_start":
|
|
23923
|
+
this.handleToolStart(event, options);
|
|
23924
|
+
return;
|
|
23925
|
+
case "tool_call":
|
|
23926
|
+
this.handleToolCall(event);
|
|
23927
|
+
return;
|
|
23928
|
+
case "task_start":
|
|
23929
|
+
this.handleTaskStart(event);
|
|
23930
|
+
return;
|
|
23931
|
+
case "task":
|
|
23932
|
+
this.handleTask(event);
|
|
23933
|
+
return;
|
|
23934
|
+
case "compaction_start":
|
|
23935
|
+
this.handleCompactionStart(event);
|
|
23936
|
+
return;
|
|
23937
|
+
case "compaction":
|
|
23938
|
+
this.handleCompaction(event);
|
|
23939
|
+
return;
|
|
23940
|
+
default:
|
|
23941
|
+
return;
|
|
23942
|
+
}
|
|
23943
|
+
}
|
|
23944
|
+
handleOperationStart(event) {
|
|
23945
|
+
if (!isInstrumentedOperation(event.operationKind)) {
|
|
23946
|
+
return;
|
|
23947
|
+
}
|
|
23948
|
+
const state = this.takePendingOperationForEvent(event);
|
|
23949
|
+
if (!state) {
|
|
23950
|
+
return;
|
|
23951
|
+
}
|
|
23952
|
+
state.operationId = event.operationId;
|
|
23953
|
+
this.activeOperationsById.set(event.operationId, state);
|
|
23954
|
+
addScopedOperation(this.activeOperationsByScope, event, state);
|
|
23955
|
+
state.metadata = {
|
|
23956
|
+
...state.metadata,
|
|
23957
|
+
...extractEventMetadata(event),
|
|
23958
|
+
"flue.operation_id": event.operationId
|
|
23959
|
+
};
|
|
23960
|
+
safeLog3(state.span, { metadata: state.metadata });
|
|
23961
|
+
}
|
|
23962
|
+
handleOperation(event) {
|
|
23963
|
+
const state = event.operationId ? this.activeOperationsById.get(event.operationId) : void 0;
|
|
23964
|
+
if (!state) {
|
|
23965
|
+
return;
|
|
23966
|
+
}
|
|
23967
|
+
const metadata = {
|
|
23968
|
+
...state.metadata,
|
|
23969
|
+
...extractEventMetadata(event),
|
|
23970
|
+
...typeof event.durationMs === "number" ? { "flue.duration_ms": event.durationMs } : {},
|
|
23971
|
+
...event.isError !== void 0 ? { "flue.is_error": event.isError } : {}
|
|
23972
|
+
};
|
|
23973
|
+
const metrics = metricsFromUsage(event.usage);
|
|
23974
|
+
safeLog3(state.span, {
|
|
23975
|
+
...event.error ? { error: errorToString(event.error) } : {},
|
|
23976
|
+
metadata,
|
|
23977
|
+
...Object.keys(metrics).length ? { metrics } : {}
|
|
23978
|
+
});
|
|
23979
|
+
}
|
|
23980
|
+
ensureTurnState(event) {
|
|
23981
|
+
const scope = scopeKey(event);
|
|
23982
|
+
const existing = this.turnsByScope.get(scope);
|
|
23983
|
+
if (existing) {
|
|
23984
|
+
return existing;
|
|
23985
|
+
}
|
|
23986
|
+
const parent = this.parentSpanForEvent(event);
|
|
23987
|
+
const metadata = {
|
|
23988
|
+
...extractEventMetadata(event),
|
|
23989
|
+
provider: "flue"
|
|
23990
|
+
};
|
|
23991
|
+
const span = startFlueSpan(parent, {
|
|
23992
|
+
name: "flue.turn",
|
|
23993
|
+
spanAttributes: { type: "llm" /* LLM */ }
|
|
23994
|
+
});
|
|
23995
|
+
const state = {
|
|
23996
|
+
metadata,
|
|
23997
|
+
span,
|
|
23998
|
+
hasThinking: false,
|
|
23999
|
+
startTime: getCurrentUnixTimestamp(),
|
|
24000
|
+
text: [],
|
|
24001
|
+
thinking: [],
|
|
24002
|
+
toolCalls: []
|
|
24003
|
+
};
|
|
24004
|
+
safeLog3(span, { metadata });
|
|
24005
|
+
this.turnsByScope.set(scope, state);
|
|
24006
|
+
return state;
|
|
24007
|
+
}
|
|
24008
|
+
handleTurn(event) {
|
|
24009
|
+
const scope = scopeKey(event);
|
|
24010
|
+
const state = this.ensureTurnState(event);
|
|
24011
|
+
const text = state.text.join("");
|
|
24012
|
+
const reasoning = _nullishCoalesce(state.finalThinking, () => ( state.thinking.join("")));
|
|
24013
|
+
const outputReasoning = reasoning || (state.hasThinking ? "[reasoning stream present; content unavailable]" : void 0);
|
|
24014
|
+
const metadata = {
|
|
24015
|
+
...state.metadata,
|
|
24016
|
+
...extractEventMetadata(event),
|
|
24017
|
+
...event.model ? { model: event.model, "flue.model": event.model } : {},
|
|
24018
|
+
...event.stopReason ? { "flue.stop_reason": event.stopReason } : {},
|
|
24019
|
+
...event.isError !== void 0 ? { "flue.is_error": event.isError } : {},
|
|
24020
|
+
provider: "flue"
|
|
24021
|
+
};
|
|
24022
|
+
safeLog3(state.span, {
|
|
24023
|
+
...event.error ? { error: errorToString(event.error) } : {},
|
|
24024
|
+
metadata,
|
|
24025
|
+
metrics: {
|
|
24026
|
+
...durationMsMetrics(event.durationMs),
|
|
24027
|
+
...metricsFromUsage(event.usage)
|
|
24028
|
+
},
|
|
24029
|
+
output: toAssistantOutput(
|
|
24030
|
+
text,
|
|
24031
|
+
event.stopReason,
|
|
24032
|
+
outputReasoning,
|
|
24033
|
+
state.toolCalls
|
|
24034
|
+
)
|
|
24035
|
+
});
|
|
24036
|
+
state.span.end();
|
|
24037
|
+
this.turnsByScope.delete(scope);
|
|
24038
|
+
}
|
|
24039
|
+
handleThinkingDelta(event) {
|
|
24040
|
+
const delta = event.delta;
|
|
24041
|
+
if (typeof delta !== "string" || !delta) {
|
|
24042
|
+
return;
|
|
24043
|
+
}
|
|
24044
|
+
const state = this.ensureTurnState(event);
|
|
24045
|
+
state.hasThinking = true;
|
|
24046
|
+
state.metadata["flue.thinking"] = true;
|
|
24047
|
+
state.thinking.push(delta);
|
|
24048
|
+
}
|
|
24049
|
+
handleThinkingStart(event) {
|
|
24050
|
+
const state = this.ensureTurnState(event);
|
|
24051
|
+
state.hasThinking = true;
|
|
24052
|
+
state.metadata["flue.thinking"] = true;
|
|
24053
|
+
}
|
|
24054
|
+
handleThinkingEnd(event) {
|
|
24055
|
+
const state = this.ensureTurnState(event);
|
|
24056
|
+
state.hasThinking = true;
|
|
24057
|
+
state.metadata["flue.thinking"] = true;
|
|
24058
|
+
if (typeof event.content === "string" && event.content) {
|
|
24059
|
+
state.finalThinking = event.content;
|
|
24060
|
+
}
|
|
24061
|
+
}
|
|
24062
|
+
handleToolStart(event, options) {
|
|
24063
|
+
const toolCallId = event.toolCallId;
|
|
24064
|
+
if (!toolCallId) {
|
|
24065
|
+
return;
|
|
24066
|
+
}
|
|
24067
|
+
const parent = this.parentSpanForEvent(event);
|
|
24068
|
+
const scope = scopeKey(event);
|
|
24069
|
+
let turnState = this.turnsByScope.get(scope);
|
|
24070
|
+
if (!turnState && parent && options.captureTurnSpans) {
|
|
24071
|
+
turnState = this.ensureTurnState(event);
|
|
24072
|
+
}
|
|
24073
|
+
const metadata = {
|
|
24074
|
+
...extractEventMetadata(event),
|
|
24075
|
+
...event.toolName ? { "flue.tool_name": event.toolName } : {},
|
|
24076
|
+
"flue.tool_call_id": toolCallId,
|
|
24077
|
+
provider: "flue"
|
|
24078
|
+
};
|
|
24079
|
+
const span = startFlueSpan(parent, {
|
|
24080
|
+
name: `tool: ${_nullishCoalesce(event.toolName, () => ( "unknown"))}`,
|
|
24081
|
+
spanAttributes: { type: "tool" /* TOOL */ }
|
|
24082
|
+
});
|
|
24083
|
+
if (turnState) {
|
|
24084
|
+
turnState.toolCalls.push({
|
|
24085
|
+
args: event.args,
|
|
24086
|
+
toolCallId,
|
|
24087
|
+
toolName: event.toolName
|
|
24088
|
+
});
|
|
24089
|
+
}
|
|
24090
|
+
safeLog3(span, {
|
|
24091
|
+
input: event.args,
|
|
24092
|
+
metadata
|
|
24093
|
+
});
|
|
24094
|
+
this.toolsById.set(toolKey(event), {
|
|
24095
|
+
metadata,
|
|
24096
|
+
span,
|
|
24097
|
+
startTime: getCurrentUnixTimestamp()
|
|
24098
|
+
});
|
|
24099
|
+
}
|
|
24100
|
+
handleToolCall(event) {
|
|
24101
|
+
const key = toolKey(event);
|
|
24102
|
+
const state = _nullishCoalesce(this.toolsById.get(key), () => ( this.startSyntheticToolState(event, _nullishCoalesce(event.toolName, () => ( "unknown")))));
|
|
24103
|
+
const metadata = {
|
|
24104
|
+
...state.metadata,
|
|
24105
|
+
...extractEventMetadata(event),
|
|
24106
|
+
...event.toolName ? { "flue.tool_name": event.toolName } : {},
|
|
24107
|
+
...event.toolCallId ? { "flue.tool_call_id": event.toolCallId } : {},
|
|
24108
|
+
...event.isError !== void 0 ? { "flue.is_error": event.isError } : {}
|
|
24109
|
+
};
|
|
24110
|
+
safeLog3(state.span, {
|
|
24111
|
+
...event.isError ? { error: errorToString(event.result) } : {},
|
|
24112
|
+
metadata,
|
|
24113
|
+
metrics: durationMsMetrics(event.durationMs),
|
|
24114
|
+
output: event.result
|
|
24115
|
+
});
|
|
24116
|
+
state.span.end();
|
|
24117
|
+
this.toolsById.delete(key);
|
|
24118
|
+
}
|
|
24119
|
+
handleTaskStart(event) {
|
|
24120
|
+
const parent = this.parentSpanForEvent(event);
|
|
24121
|
+
const metadata = {
|
|
24122
|
+
...extractEventMetadata(event),
|
|
24123
|
+
...event.role ? { "flue.role": event.role } : {},
|
|
24124
|
+
...event.cwd ? { "flue.cwd": event.cwd } : {},
|
|
24125
|
+
"flue.task_id": event.taskId,
|
|
24126
|
+
provider: "flue"
|
|
24127
|
+
};
|
|
24128
|
+
const span = startFlueSpan(parent, {
|
|
24129
|
+
name: "flue.task",
|
|
24130
|
+
spanAttributes: { type: "task" /* TASK */ }
|
|
24131
|
+
});
|
|
24132
|
+
safeLog3(span, {
|
|
24133
|
+
input: event.prompt,
|
|
24134
|
+
metadata
|
|
24135
|
+
});
|
|
24136
|
+
this.tasksById.set(event.taskId, {
|
|
24137
|
+
metadata,
|
|
24138
|
+
span,
|
|
24139
|
+
startTime: getCurrentUnixTimestamp()
|
|
24140
|
+
});
|
|
24141
|
+
}
|
|
24142
|
+
handleTask(event) {
|
|
24143
|
+
const state = this.tasksById.get(event.taskId);
|
|
24144
|
+
if (!state) {
|
|
24145
|
+
return;
|
|
24146
|
+
}
|
|
24147
|
+
safeLog3(state.span, {
|
|
24148
|
+
...event.isError ? { error: errorToString(event.result) } : {},
|
|
24149
|
+
metadata: {
|
|
24150
|
+
...state.metadata,
|
|
24151
|
+
...extractEventMetadata(event),
|
|
24152
|
+
...event.isError !== void 0 ? { "flue.is_error": event.isError } : {}
|
|
24153
|
+
},
|
|
24154
|
+
metrics: durationMsMetrics(event.durationMs),
|
|
24155
|
+
output: event.result
|
|
24156
|
+
});
|
|
24157
|
+
state.span.end();
|
|
24158
|
+
this.tasksById.delete(event.taskId);
|
|
24159
|
+
}
|
|
24160
|
+
handleCompactionStart(event) {
|
|
24161
|
+
const operationState = this.operationStateForEvent(event);
|
|
24162
|
+
const parent = _nullishCoalesce(_optionalChain([operationState, 'optionalAccess', _596 => _596.span]), () => ( this.parentSpanForEvent(event)));
|
|
24163
|
+
const metadata = {
|
|
24164
|
+
...extractEventMetadata(event),
|
|
24165
|
+
...event.reason ? { "flue.compaction_reason": event.reason } : {},
|
|
24166
|
+
provider: "flue"
|
|
24167
|
+
};
|
|
24168
|
+
const input = {
|
|
24169
|
+
...typeof event.estimatedTokens === "number" ? { estimatedTokens: event.estimatedTokens } : {},
|
|
24170
|
+
...event.reason ? { reason: event.reason } : {}
|
|
24171
|
+
};
|
|
24172
|
+
const span = startFlueSpan(parent, {
|
|
24173
|
+
name: "flue.compaction",
|
|
24174
|
+
spanAttributes: { type: "task" /* TASK */ }
|
|
24175
|
+
});
|
|
24176
|
+
safeLog3(span, {
|
|
24177
|
+
input,
|
|
24178
|
+
metadata
|
|
24179
|
+
});
|
|
24180
|
+
this.compactionsByScope.set(scopeKey(event), {
|
|
24181
|
+
input,
|
|
24182
|
+
metadata,
|
|
24183
|
+
operationState,
|
|
24184
|
+
span,
|
|
24185
|
+
startTime: getCurrentUnixTimestamp()
|
|
24186
|
+
});
|
|
24187
|
+
}
|
|
24188
|
+
handleCompaction(event) {
|
|
24189
|
+
const key = scopeKey(event);
|
|
24190
|
+
const state = _nullishCoalesce(this.compactionsByScope.get(key), () => ( this.findCompactionState(event)));
|
|
24191
|
+
if (!state) {
|
|
24192
|
+
return;
|
|
24193
|
+
}
|
|
24194
|
+
safeLog3(state.span, {
|
|
24195
|
+
metadata: {
|
|
24196
|
+
...state.metadata,
|
|
24197
|
+
...extractEventMetadata(event),
|
|
24198
|
+
...typeof event.messagesBefore === "number" ? { "flue.messages_before": event.messagesBefore } : {},
|
|
24199
|
+
...typeof event.messagesAfter === "number" ? { "flue.messages_after": event.messagesAfter } : {}
|
|
24200
|
+
},
|
|
24201
|
+
metrics: {
|
|
24202
|
+
...durationMsMetrics(event.durationMs),
|
|
24203
|
+
...metricsFromUsage(event.usage)
|
|
24204
|
+
},
|
|
24205
|
+
output: {
|
|
24206
|
+
messagesAfter: event.messagesAfter,
|
|
24207
|
+
messagesBefore: event.messagesBefore
|
|
24208
|
+
}
|
|
24209
|
+
});
|
|
24210
|
+
state.span.end();
|
|
24211
|
+
this.deleteCompactionState(state);
|
|
24212
|
+
}
|
|
24213
|
+
findCompactionState(event) {
|
|
24214
|
+
const operationState = this.operationStateForEvent(event);
|
|
24215
|
+
for (const state of this.compactionsByScope.values()) {
|
|
24216
|
+
if (operationState && state.operationState === operationState) {
|
|
24217
|
+
return state;
|
|
24218
|
+
}
|
|
24219
|
+
}
|
|
24220
|
+
return void 0;
|
|
24221
|
+
}
|
|
24222
|
+
finishCompactionsForOperation(operationState) {
|
|
24223
|
+
for (const state of [...this.compactionsByScope.values()]) {
|
|
24224
|
+
if (state.operationState !== operationState) {
|
|
24225
|
+
continue;
|
|
24226
|
+
}
|
|
24227
|
+
safeLog3(state.span, {
|
|
24228
|
+
input: state.input,
|
|
24229
|
+
metadata: state.metadata,
|
|
24230
|
+
metrics: {
|
|
24231
|
+
...buildDurationMetrics3(state.startTime)
|
|
24232
|
+
},
|
|
24233
|
+
output: { completed: true }
|
|
24234
|
+
});
|
|
24235
|
+
state.span.end();
|
|
24236
|
+
this.deleteCompactionState(state);
|
|
24237
|
+
}
|
|
24238
|
+
}
|
|
24239
|
+
deleteCompactionState(state) {
|
|
24240
|
+
for (const [key, candidate] of this.compactionsByScope) {
|
|
24241
|
+
if (candidate !== state) {
|
|
24242
|
+
continue;
|
|
24243
|
+
}
|
|
24244
|
+
this.compactionsByScope.delete(key);
|
|
24245
|
+
return;
|
|
24246
|
+
}
|
|
24247
|
+
}
|
|
24248
|
+
startSyntheticToolState(event, toolName) {
|
|
24249
|
+
const parent = this.parentSpanForEvent(event);
|
|
24250
|
+
const metadata = {
|
|
24251
|
+
...extractEventMetadata(event),
|
|
24252
|
+
...event.toolCallId ? { "flue.tool_call_id": event.toolCallId } : {},
|
|
24253
|
+
"flue.tool_name": toolName,
|
|
24254
|
+
provider: "flue"
|
|
24255
|
+
};
|
|
24256
|
+
const span = startFlueSpan(parent, {
|
|
24257
|
+
name: `tool: ${toolName}`,
|
|
24258
|
+
spanAttributes: { type: "tool" /* TOOL */ }
|
|
24259
|
+
});
|
|
24260
|
+
safeLog3(span, { metadata });
|
|
24261
|
+
return { metadata, span, startTime: getCurrentUnixTimestamp() };
|
|
24262
|
+
}
|
|
24263
|
+
operationStateForEvent(event) {
|
|
24264
|
+
if (event.operationId) {
|
|
24265
|
+
const operation = _nullishCoalesce(this.activeOperationsById.get(event.operationId), () => ( this.promotePendingOperationForEvent(event)));
|
|
24266
|
+
if (operation) {
|
|
24267
|
+
return operation;
|
|
24268
|
+
}
|
|
24269
|
+
}
|
|
24270
|
+
return _nullishCoalesce(this.activeOperationForEventScope(event), () => ( this.pendingOperationForEventScope(event)));
|
|
24271
|
+
}
|
|
24272
|
+
parentSpanForEvent(event) {
|
|
24273
|
+
if (event.operationId) {
|
|
24274
|
+
const operation = this.operationStateForEvent(event);
|
|
24275
|
+
if (operation) {
|
|
24276
|
+
return operation.span;
|
|
24277
|
+
}
|
|
24278
|
+
}
|
|
24279
|
+
if (event.taskId) {
|
|
24280
|
+
return _optionalChain([this, 'access', _597 => _597.tasksById, 'access', _598 => _598.get, 'call', _599 => _599(event.taskId), 'optionalAccess', _600 => _600.span]);
|
|
24281
|
+
}
|
|
24282
|
+
return _optionalChain([this, 'access', _601 => _601.operationStateForEvent, 'call', _602 => _602(event), 'optionalAccess', _603 => _603.span]);
|
|
24283
|
+
}
|
|
24284
|
+
promotePendingOperationForEvent(event) {
|
|
24285
|
+
if (!event.operationId) {
|
|
24286
|
+
return void 0;
|
|
24287
|
+
}
|
|
24288
|
+
const scopePrefixes = operationScopePrefixes(event);
|
|
24289
|
+
for (const [candidateKey, candidateQueue] of this.pendingOperationsByKey) {
|
|
24290
|
+
if (!candidateQueue.length || !operationKeyMatchesScopes(candidateKey, scopePrefixes)) {
|
|
24291
|
+
continue;
|
|
24292
|
+
}
|
|
24293
|
+
const state = candidateQueue.shift();
|
|
24294
|
+
if (!state) {
|
|
24295
|
+
return void 0;
|
|
24296
|
+
}
|
|
24297
|
+
state.operationId = event.operationId;
|
|
24298
|
+
this.activeOperationsById.set(event.operationId, state);
|
|
24299
|
+
addScopedOperation(this.activeOperationsByScope, event, state);
|
|
24300
|
+
state.metadata = {
|
|
24301
|
+
...state.metadata,
|
|
24302
|
+
...extractEventMetadata(event),
|
|
24303
|
+
"flue.operation_id": event.operationId
|
|
24304
|
+
};
|
|
24305
|
+
safeLog3(state.span, { metadata: state.metadata });
|
|
24306
|
+
return state;
|
|
24307
|
+
}
|
|
24308
|
+
return void 0;
|
|
24309
|
+
}
|
|
24310
|
+
activeOperationForEventScope(event) {
|
|
24311
|
+
for (const scope of operationScopeNames(event)) {
|
|
24312
|
+
const operations = this.activeOperationsByScope.get(scope);
|
|
24313
|
+
if (_optionalChain([operations, 'optionalAccess', _604 => _604.length])) {
|
|
24314
|
+
return operations[operations.length - 1];
|
|
24315
|
+
}
|
|
24316
|
+
}
|
|
24317
|
+
return void 0;
|
|
24318
|
+
}
|
|
24319
|
+
pendingOperationForEventScope(event) {
|
|
24320
|
+
const scopePrefixes = operationScopePrefixes(event);
|
|
24321
|
+
for (const [candidateKey, candidateQueue] of this.pendingOperationsByKey) {
|
|
24322
|
+
if (!candidateQueue.length || !operationKeyMatchesScopes(candidateKey, scopePrefixes)) {
|
|
24323
|
+
continue;
|
|
24324
|
+
}
|
|
24325
|
+
return candidateQueue[0];
|
|
24326
|
+
}
|
|
24327
|
+
return void 0;
|
|
24328
|
+
}
|
|
24329
|
+
takePendingOperationForEvent(event) {
|
|
24330
|
+
const key = operationKey(event.session, event.operationKind);
|
|
24331
|
+
const queue2 = this.pendingOperationsByKey.get(key);
|
|
24332
|
+
if (_optionalChain([queue2, 'optionalAccess', _605 => _605.length])) {
|
|
24333
|
+
return queue2.shift();
|
|
24334
|
+
}
|
|
24335
|
+
for (const [candidateKey, candidateQueue] of this.pendingOperationsByKey) {
|
|
24336
|
+
if (candidateKey.endsWith(`::${event.operationKind}`) && candidateQueue.length) {
|
|
24337
|
+
return candidateQueue.shift();
|
|
24338
|
+
}
|
|
24339
|
+
}
|
|
24340
|
+
return void 0;
|
|
24341
|
+
}
|
|
24342
|
+
pendingOperationQueue(key) {
|
|
24343
|
+
const existing = this.pendingOperationsByKey.get(key);
|
|
24344
|
+
if (existing) {
|
|
24345
|
+
return existing;
|
|
24346
|
+
}
|
|
24347
|
+
const queue2 = [];
|
|
24348
|
+
this.pendingOperationsByKey.set(key, queue2);
|
|
24349
|
+
return queue2;
|
|
24350
|
+
}
|
|
24351
|
+
}, _class22);
|
|
24352
|
+
function isInstrumentedOperation(operation) {
|
|
24353
|
+
return operation === "prompt" || operation === "skill" || operation === "task" || operation === "compact";
|
|
24354
|
+
}
|
|
24355
|
+
function getSessionName(session) {
|
|
24356
|
+
return typeof _optionalChain([session, 'optionalAccess', _606 => _606.name]) === "string" ? session.name : void 0;
|
|
24357
|
+
}
|
|
24358
|
+
function operationKey(sessionName, operation) {
|
|
24359
|
+
return `${_nullishCoalesce(sessionName, () => ( "unknown"))}::${operation}`;
|
|
24360
|
+
}
|
|
24361
|
+
function operationScopePrefixes(event) {
|
|
24362
|
+
const scopes = /* @__PURE__ */ new Set();
|
|
24363
|
+
for (const scope of operationScopeNames(event)) {
|
|
24364
|
+
scopes.add(`${scope}::`);
|
|
24365
|
+
}
|
|
24366
|
+
return scopes;
|
|
24367
|
+
}
|
|
24368
|
+
function operationKeyMatchesScopes(key, scopes) {
|
|
24369
|
+
for (const scope of scopes) {
|
|
24370
|
+
if (key.startsWith(scope)) {
|
|
24371
|
+
return true;
|
|
24372
|
+
}
|
|
24373
|
+
}
|
|
24374
|
+
return false;
|
|
24375
|
+
}
|
|
24376
|
+
function operationScopeNames(event) {
|
|
24377
|
+
const scopes = /* @__PURE__ */ new Set();
|
|
24378
|
+
if (event.session) {
|
|
24379
|
+
scopes.add(event.session);
|
|
24380
|
+
}
|
|
24381
|
+
if (event.parentSession) {
|
|
24382
|
+
scopes.add(event.parentSession);
|
|
24383
|
+
}
|
|
24384
|
+
if (!scopes.size) {
|
|
24385
|
+
scopes.add("unknown");
|
|
24386
|
+
}
|
|
24387
|
+
return scopes;
|
|
24388
|
+
}
|
|
24389
|
+
function addScopedOperation(operationsByScope, event, state) {
|
|
24390
|
+
for (const scope of operationScopeNames(event)) {
|
|
24391
|
+
addOperationToScope(operationsByScope, scope, state);
|
|
24392
|
+
}
|
|
24393
|
+
}
|
|
24394
|
+
function addOperationToScope(operationsByScope, scope, state) {
|
|
24395
|
+
const operations = operationsByScope.get(scope);
|
|
24396
|
+
if (operations) {
|
|
24397
|
+
if (!operations.includes(state)) {
|
|
24398
|
+
operations.push(state);
|
|
24399
|
+
}
|
|
24400
|
+
} else {
|
|
24401
|
+
operationsByScope.set(scope, [state]);
|
|
24402
|
+
}
|
|
24403
|
+
}
|
|
24404
|
+
function removeScopedOperation(operationsByScope, state) {
|
|
24405
|
+
for (const [scope, operations] of operationsByScope) {
|
|
24406
|
+
const index = operations.indexOf(state);
|
|
24407
|
+
if (index === -1) {
|
|
24408
|
+
continue;
|
|
24409
|
+
}
|
|
24410
|
+
operations.splice(index, 1);
|
|
24411
|
+
if (operations.length === 0) {
|
|
24412
|
+
operationsByScope.delete(scope);
|
|
24413
|
+
}
|
|
24414
|
+
}
|
|
24415
|
+
}
|
|
24416
|
+
function removePendingOperation(pendingOperationsByKey, state) {
|
|
24417
|
+
for (const [key, queue2] of pendingOperationsByKey) {
|
|
24418
|
+
const index = queue2.indexOf(state);
|
|
24419
|
+
if (index === -1) {
|
|
24420
|
+
continue;
|
|
24421
|
+
}
|
|
24422
|
+
queue2.splice(index, 1);
|
|
24423
|
+
if (queue2.length === 0) {
|
|
24424
|
+
pendingOperationsByKey.delete(key);
|
|
24425
|
+
}
|
|
24426
|
+
return;
|
|
24427
|
+
}
|
|
24428
|
+
}
|
|
24429
|
+
function extractSessionMetadata(session) {
|
|
24430
|
+
const sessionName = getSessionName(session);
|
|
24431
|
+
return sessionName ? { "flue.session": sessionName } : {};
|
|
24432
|
+
}
|
|
24433
|
+
function extractEventMetadata(event) {
|
|
24434
|
+
return {
|
|
24435
|
+
...event.runId ? { "flue.run_id": event.runId } : {},
|
|
24436
|
+
...typeof event.eventIndex === "number" ? { "flue.event_index": event.eventIndex } : {},
|
|
24437
|
+
...event.session ? { "flue.session": event.session } : {},
|
|
24438
|
+
...event.parentSession ? { "flue.parent_session": event.parentSession } : {},
|
|
24439
|
+
...event.harness ? { "flue.harness": event.harness } : {},
|
|
24440
|
+
...event.taskId ? { "flue.task_id": event.taskId } : {},
|
|
24441
|
+
...event.operationId ? { "flue.operation_id": event.operationId } : {}
|
|
24442
|
+
};
|
|
24443
|
+
}
|
|
24444
|
+
function extractOperationInput(operation, args) {
|
|
24445
|
+
switch (operation) {
|
|
24446
|
+
case "prompt":
|
|
24447
|
+
case "task":
|
|
24448
|
+
return args[0];
|
|
24449
|
+
case "skill":
|
|
24450
|
+
return {
|
|
24451
|
+
args: _optionalChain([getOptionObject, 'call', _607 => _607(args[1]), 'optionalAccess', _608 => _608.args]),
|
|
24452
|
+
name: args[0]
|
|
24453
|
+
};
|
|
24454
|
+
case "compact":
|
|
24455
|
+
return void 0;
|
|
24456
|
+
}
|
|
24457
|
+
}
|
|
24458
|
+
function extractOperationInputMetadata(operation, args) {
|
|
24459
|
+
const options = getOptionObject(args[1]);
|
|
24460
|
+
return {
|
|
24461
|
+
...operation === "skill" && typeof args[0] === "string" ? { "flue.skill_name": args[0] } : {},
|
|
24462
|
+
..._optionalChain([options, 'optionalAccess', _609 => _609.model]) ? { model: options.model, "flue.model": options.model } : {},
|
|
24463
|
+
..._optionalChain([options, 'optionalAccess', _610 => _610.role]) ? { "flue.role": options.role } : {},
|
|
24464
|
+
..._optionalChain([options, 'optionalAccess', _611 => _611.thinkingLevel]) ? { "flue.thinking_level": options.thinkingLevel } : {},
|
|
24465
|
+
...typeof _optionalChain([options, 'optionalAccess', _612 => _612.cwd]) === "string" ? { "flue.cwd": options.cwd } : {},
|
|
24466
|
+
...Array.isArray(_optionalChain([options, 'optionalAccess', _613 => _613.tools])) ? {
|
|
24467
|
+
"flue.tools_count": options.tools.length,
|
|
24468
|
+
tools: summarizeTools(options.tools)
|
|
24469
|
+
} : {},
|
|
24470
|
+
...Array.isArray(_optionalChain([options, 'optionalAccess', _614 => _614.images])) ? { "flue.images_count": options.images.length } : {},
|
|
24471
|
+
..._optionalChain([options, 'optionalAccess', _615 => _615.result]) || _optionalChain([options, 'optionalAccess', _616 => _616.schema]) ? { "flue.result_schema": true } : {}
|
|
24472
|
+
};
|
|
24473
|
+
}
|
|
24474
|
+
function getOptionObject(value) {
|
|
24475
|
+
return isObject(value) ? value : void 0;
|
|
24476
|
+
}
|
|
24477
|
+
function summarizeTools(tools) {
|
|
24478
|
+
return tools.flatMap((tool) => {
|
|
24479
|
+
if (!isObject(tool)) {
|
|
24480
|
+
return [];
|
|
24481
|
+
}
|
|
24482
|
+
const name = typeof tool.name === "string" ? tool.name : void 0;
|
|
24483
|
+
if (!name) {
|
|
24484
|
+
return [];
|
|
24485
|
+
}
|
|
24486
|
+
return [
|
|
24487
|
+
{
|
|
24488
|
+
function: {
|
|
24489
|
+
description: typeof tool.description === "string" ? tool.description : void 0,
|
|
24490
|
+
name,
|
|
24491
|
+
parameters: tool.parameters
|
|
24492
|
+
},
|
|
24493
|
+
type: "function"
|
|
24494
|
+
}
|
|
24495
|
+
];
|
|
24496
|
+
});
|
|
24497
|
+
}
|
|
24498
|
+
function extractPromptResponseMetadata(result) {
|
|
24499
|
+
const modelId = _optionalChain([result, 'optionalAccess', _617 => _617.model]) && typeof result.model.id === "string" ? result.model.id : void 0;
|
|
24500
|
+
return modelId ? {
|
|
24501
|
+
model: modelId,
|
|
24502
|
+
"flue.model": modelId
|
|
24503
|
+
} : {};
|
|
24504
|
+
}
|
|
24505
|
+
function extractOperationOutput(result) {
|
|
24506
|
+
if (!result) {
|
|
24507
|
+
return void 0;
|
|
24508
|
+
}
|
|
24509
|
+
if ("data" in result) {
|
|
24510
|
+
return result.data;
|
|
24511
|
+
}
|
|
24512
|
+
if ("text" in result) {
|
|
24513
|
+
return result.text;
|
|
24514
|
+
}
|
|
24515
|
+
return result;
|
|
24516
|
+
}
|
|
24517
|
+
function metricsFromUsage(usage) {
|
|
24518
|
+
return {
|
|
24519
|
+
...typeof _optionalChain([usage, 'optionalAccess', _618 => _618.input]) === "number" ? { prompt_tokens: usage.input } : {},
|
|
24520
|
+
...typeof _optionalChain([usage, 'optionalAccess', _619 => _619.output]) === "number" ? { completion_tokens: usage.output } : {},
|
|
24521
|
+
...typeof _optionalChain([usage, 'optionalAccess', _620 => _620.cacheRead]) === "number" ? { prompt_cached_tokens: usage.cacheRead } : {},
|
|
24522
|
+
...typeof _optionalChain([usage, 'optionalAccess', _621 => _621.cacheWrite]) === "number" ? { prompt_cache_creation_tokens: usage.cacheWrite } : {},
|
|
24523
|
+
...typeof _optionalChain([usage, 'optionalAccess', _622 => _622.totalTokens]) === "number" ? { tokens: usage.totalTokens } : {},
|
|
24524
|
+
...typeof _optionalChain([usage, 'optionalAccess', _623 => _623.cost, 'optionalAccess', _624 => _624.total]) === "number" ? { estimated_cost: usage.cost.total } : {}
|
|
24525
|
+
};
|
|
24526
|
+
}
|
|
24527
|
+
function buildDurationMetrics3(startTime) {
|
|
24528
|
+
return {
|
|
24529
|
+
duration_ms: Math.max(0, (getCurrentUnixTimestamp() - startTime) * 1e3)
|
|
24530
|
+
};
|
|
24531
|
+
}
|
|
24532
|
+
function durationMsMetrics(durationMs) {
|
|
24533
|
+
return typeof durationMs === "number" ? { duration_ms: durationMs } : {};
|
|
24534
|
+
}
|
|
24535
|
+
function scopeKey(event) {
|
|
24536
|
+
if (event.operationId) {
|
|
24537
|
+
return `operation:${event.operationId}`;
|
|
24538
|
+
}
|
|
24539
|
+
if (event.taskId) {
|
|
24540
|
+
return `task:${event.taskId}`;
|
|
24541
|
+
}
|
|
24542
|
+
if (event.session) {
|
|
24543
|
+
return `session:${event.session}`;
|
|
24544
|
+
}
|
|
24545
|
+
return "flue:unknown";
|
|
24546
|
+
}
|
|
24547
|
+
function toolKey(event) {
|
|
24548
|
+
return `${scopeKey(event)}::tool:${_nullishCoalesce(event.toolCallId, () => ( "unknown"))}`;
|
|
24549
|
+
}
|
|
24550
|
+
function toAssistantOutput(text, finishReason, reasoning, toolCalls) {
|
|
24551
|
+
return [
|
|
24552
|
+
{
|
|
24553
|
+
finish_reason: _nullishCoalesce(finishReason, () => ( "stop")),
|
|
24554
|
+
index: 0,
|
|
24555
|
+
message: {
|
|
24556
|
+
content: text,
|
|
24557
|
+
...reasoning ? { reasoning } : {},
|
|
24558
|
+
role: "assistant",
|
|
24559
|
+
..._optionalChain([toolCalls, 'optionalAccess', _625 => _625.length]) ? {
|
|
24560
|
+
tool_calls: toolCalls.map((toolCall) => ({
|
|
24561
|
+
function: {
|
|
24562
|
+
arguments: toolCall.args === void 0 ? "{}" : JSON.stringify(toolCall.args),
|
|
24563
|
+
name: _nullishCoalesce(toolCall.toolName, () => ( "unknown"))
|
|
24564
|
+
},
|
|
24565
|
+
...toolCall.toolCallId ? { id: toolCall.toolCallId } : {},
|
|
24566
|
+
type: "function"
|
|
24567
|
+
}))
|
|
24568
|
+
} : {}
|
|
24569
|
+
}
|
|
24570
|
+
}
|
|
24571
|
+
];
|
|
24572
|
+
}
|
|
24573
|
+
function startFlueSpan(parent, args) {
|
|
24574
|
+
return parent ? withCurrent(parent, () => startSpan(args)) : startSpan(args);
|
|
24575
|
+
}
|
|
24576
|
+
function safeLog3(span, event) {
|
|
24577
|
+
try {
|
|
24578
|
+
span.log(event);
|
|
24579
|
+
} catch (error) {
|
|
24580
|
+
logInstrumentationError3("Flue span log", error);
|
|
24581
|
+
}
|
|
24582
|
+
}
|
|
24583
|
+
function errorToString(error) {
|
|
24584
|
+
if (error instanceof Error) {
|
|
24585
|
+
return error.message;
|
|
24586
|
+
}
|
|
24587
|
+
if (typeof error === "string") {
|
|
24588
|
+
return error;
|
|
24589
|
+
}
|
|
24590
|
+
try {
|
|
24591
|
+
return JSON.stringify(error);
|
|
24592
|
+
} catch (e55) {
|
|
24593
|
+
return String(error);
|
|
24594
|
+
}
|
|
24595
|
+
}
|
|
24596
|
+
function logInstrumentationError3(label, error) {
|
|
24597
|
+
console.error(`Error in ${label} instrumentation:`, error);
|
|
24598
|
+
}
|
|
24599
|
+
|
|
24600
|
+
// src/wrappers/langchain/callback-handler.ts
|
|
24601
|
+
var BRAINTRUST_LANGCHAIN_CALLBACK_HANDLER_NAME = "BraintrustCallbackHandler";
|
|
24602
|
+
var BraintrustLangChainCallbackHandler = (_class23 = class {
|
|
24603
|
+
__init78() {this.name = BRAINTRUST_LANGCHAIN_CALLBACK_HANDLER_NAME}
|
|
24604
|
+
__init79() {this.spans = /* @__PURE__ */ new Map()}
|
|
24605
|
+
__init80() {this.skippedRuns = /* @__PURE__ */ new Set()}
|
|
24606
|
+
|
|
24607
|
+
|
|
24608
|
+
|
|
24609
|
+
__init81() {this.startTimes = /* @__PURE__ */ new Map()}
|
|
24610
|
+
__init82() {this.firstTokenTimes = /* @__PURE__ */ new Map()}
|
|
24611
|
+
__init83() {this.ttftMs = /* @__PURE__ */ new Map()}
|
|
24612
|
+
constructor(options) {;_class23.prototype.__init78.call(this);_class23.prototype.__init79.call(this);_class23.prototype.__init80.call(this);_class23.prototype.__init81.call(this);_class23.prototype.__init82.call(this);_class23.prototype.__init83.call(this);
|
|
24613
|
+
this.parent = _optionalChain([options, 'optionalAccess', _626 => _626.parent]);
|
|
24614
|
+
this.options = {
|
|
24615
|
+
debug: _nullishCoalesce(_optionalChain([options, 'optionalAccess', _627 => _627.debug]), () => ( false)),
|
|
24616
|
+
excludeMetadataProps: _nullishCoalesce(_optionalChain([options, 'optionalAccess', _628 => _628.excludeMetadataProps]), () => ( /^(l[sc]_|langgraph_|__pregel_|checkpoint_ns)/)),
|
|
24617
|
+
logger: _optionalChain([options, 'optionalAccess', _629 => _629.logger])
|
|
24618
|
+
};
|
|
24619
|
+
}
|
|
24620
|
+
startSpan({
|
|
24621
|
+
runId,
|
|
24622
|
+
parentRunId,
|
|
24623
|
+
...args
|
|
24624
|
+
}) {
|
|
24625
|
+
if (this.spans.has(runId)) {
|
|
24626
|
+
return;
|
|
24627
|
+
}
|
|
24628
|
+
if (!parentRunId) {
|
|
24629
|
+
this.rootRunId = runId;
|
|
24630
|
+
}
|
|
24631
|
+
const tags = _optionalChain([args, 'access', _630 => _630.event, 'optionalAccess', _631 => _631.tags]);
|
|
24632
|
+
const spanAttributes = args.spanAttributes || {};
|
|
24633
|
+
spanAttributes.type = args.type || spanAttributes.type || "task";
|
|
24634
|
+
args.type = spanAttributes.type;
|
|
24635
|
+
const currentParent = _nullishCoalesce((typeof this.parent === "function" ? this.parent() : this.parent), () => ( currentSpan()));
|
|
24636
|
+
let parentSpan;
|
|
24637
|
+
if (parentRunId && this.spans.has(parentRunId)) {
|
|
24638
|
+
parentSpan = this.spans.get(parentRunId);
|
|
24639
|
+
} else if (!Object.is(currentParent, NOOP_SPAN)) {
|
|
24640
|
+
parentSpan = currentParent;
|
|
24641
|
+
} else if (this.options.logger) {
|
|
24642
|
+
parentSpan = this.options.logger;
|
|
24643
|
+
} else {
|
|
24644
|
+
parentSpan = { startSpan };
|
|
24645
|
+
}
|
|
24646
|
+
args.event = {
|
|
24647
|
+
...args.event,
|
|
24648
|
+
tags: void 0,
|
|
24649
|
+
metadata: {
|
|
24650
|
+
...tags ? { tags } : {},
|
|
24651
|
+
..._optionalChain([args, 'access', _632 => _632.event, 'optionalAccess', _633 => _633.metadata]),
|
|
24652
|
+
braintrust: {
|
|
24653
|
+
integration_name: "langchain-js",
|
|
24654
|
+
sdk_language: "javascript"
|
|
24655
|
+
},
|
|
24656
|
+
run_id: runId,
|
|
24657
|
+
parent_run_id: parentRunId,
|
|
24658
|
+
...this.options.debug ? { runId, parentRunId } : {}
|
|
24659
|
+
}
|
|
24660
|
+
};
|
|
24661
|
+
let span = parentSpan.startSpan(args);
|
|
24662
|
+
if (!Object.is(this.options.logger, NOOP_SPAN) && Object.is(span, NOOP_SPAN)) {
|
|
24663
|
+
span = initLogger().startSpan(args);
|
|
24664
|
+
}
|
|
24665
|
+
this.spans.set(runId, span);
|
|
24666
|
+
}
|
|
24667
|
+
endSpan({
|
|
24668
|
+
runId,
|
|
24669
|
+
parentRunId,
|
|
24670
|
+
tags,
|
|
24671
|
+
metadata,
|
|
24672
|
+
...args
|
|
24673
|
+
}) {
|
|
24674
|
+
if (!this.spans.has(runId)) {
|
|
24675
|
+
return;
|
|
24676
|
+
}
|
|
24677
|
+
if (this.skippedRuns.has(runId)) {
|
|
24678
|
+
this.skippedRuns.delete(runId);
|
|
24679
|
+
return;
|
|
24680
|
+
}
|
|
24681
|
+
const span = this.spans.get(runId);
|
|
24682
|
+
this.spans.delete(runId);
|
|
24683
|
+
if (runId === this.rootRunId) {
|
|
24684
|
+
this.rootRunId = void 0;
|
|
24685
|
+
}
|
|
24686
|
+
span.log({ ...args, metadata: { tags, ...metadata } });
|
|
24687
|
+
span.end();
|
|
24688
|
+
}
|
|
24689
|
+
async handleLLMStart(llm, prompts, runId, parentRunId, extraParams, tags, metadata, runName) {
|
|
24690
|
+
this.startSpan({
|
|
24691
|
+
runId,
|
|
24692
|
+
parentRunId,
|
|
24693
|
+
name: _nullishCoalesce(_nullishCoalesce(runName, () => ( getSerializedName(llm))), () => ( "LLM")),
|
|
24694
|
+
type: "llm",
|
|
24695
|
+
event: {
|
|
24696
|
+
input: prompts,
|
|
24697
|
+
tags,
|
|
24698
|
+
metadata: {
|
|
24699
|
+
serialized: llm,
|
|
24700
|
+
name: runName,
|
|
24701
|
+
metadata,
|
|
24702
|
+
...extraParams
|
|
24703
|
+
}
|
|
24704
|
+
}
|
|
24705
|
+
});
|
|
24706
|
+
}
|
|
24707
|
+
async handleLLMError(err, runId, parentRunId, tags) {
|
|
24708
|
+
this.endSpan({ runId, parentRunId, error: err, tags });
|
|
24709
|
+
}
|
|
24710
|
+
async handleLLMEnd(output, runId, parentRunId, tags) {
|
|
24711
|
+
const metrics = getMetricsFromResponse(output);
|
|
24712
|
+
const modelName2 = getModelNameFromResponse(output);
|
|
24713
|
+
const ttft = this.ttftMs.get(runId);
|
|
24714
|
+
if (ttft !== void 0) {
|
|
24715
|
+
metrics.time_to_first_token = ttft;
|
|
24716
|
+
}
|
|
24717
|
+
this.startTimes.delete(runId);
|
|
24718
|
+
this.firstTokenTimes.delete(runId);
|
|
24719
|
+
this.ttftMs.delete(runId);
|
|
24720
|
+
this.endSpan({
|
|
24721
|
+
runId,
|
|
24722
|
+
parentRunId,
|
|
24723
|
+
output,
|
|
24724
|
+
metrics,
|
|
24725
|
+
tags,
|
|
24726
|
+
metadata: {
|
|
24727
|
+
model: modelName2
|
|
24728
|
+
}
|
|
24729
|
+
});
|
|
24730
|
+
}
|
|
24731
|
+
async handleChatModelStart(llm, messages, runId, parentRunId, extraParams, tags, metadata, runName) {
|
|
24732
|
+
this.startTimes.set(runId, Date.now());
|
|
24733
|
+
this.firstTokenTimes.delete(runId);
|
|
24734
|
+
this.ttftMs.delete(runId);
|
|
24735
|
+
this.startSpan({
|
|
24736
|
+
runId,
|
|
24737
|
+
parentRunId,
|
|
24738
|
+
name: _nullishCoalesce(_nullishCoalesce(runName, () => ( getSerializedName(llm))), () => ( "Chat Model")),
|
|
24739
|
+
type: "llm",
|
|
24740
|
+
event: {
|
|
24741
|
+
input: messages,
|
|
24742
|
+
tags,
|
|
24743
|
+
metadata: {
|
|
24744
|
+
serialized: llm,
|
|
24745
|
+
name: runName,
|
|
24746
|
+
metadata,
|
|
24747
|
+
...extraParams
|
|
24748
|
+
}
|
|
24749
|
+
}
|
|
24750
|
+
});
|
|
24751
|
+
}
|
|
24752
|
+
async handleChainStart(chain, inputs, runId, parentRunId, tags, metadata, runType, runName) {
|
|
24753
|
+
if (_optionalChain([tags, 'optionalAccess', _634 => _634.includes, 'call', _635 => _635("langsmith:hidden")])) {
|
|
24754
|
+
this.skippedRuns.add(runId);
|
|
24755
|
+
return;
|
|
24756
|
+
}
|
|
24757
|
+
this.startSpan({
|
|
24758
|
+
runId,
|
|
24759
|
+
parentRunId,
|
|
24760
|
+
name: _nullishCoalesce(_nullishCoalesce(runName, () => ( getSerializedName(chain))), () => ( "Chain")),
|
|
24761
|
+
event: {
|
|
24762
|
+
input: inputs,
|
|
24763
|
+
tags,
|
|
24764
|
+
metadata: {
|
|
24765
|
+
serialized: chain,
|
|
24766
|
+
name: runName,
|
|
24767
|
+
metadata,
|
|
24768
|
+
run_type: runType
|
|
24769
|
+
}
|
|
24770
|
+
}
|
|
24771
|
+
});
|
|
24772
|
+
}
|
|
24773
|
+
async handleChainError(err, runId, parentRunId, tags, kwargs) {
|
|
24774
|
+
this.endSpan({ runId, parentRunId, error: err, tags, metadata: kwargs });
|
|
24775
|
+
}
|
|
24776
|
+
async handleChainEnd(outputs, runId, parentRunId, tags, kwargs) {
|
|
24777
|
+
this.endSpan({
|
|
24778
|
+
runId,
|
|
24779
|
+
parentRunId,
|
|
24780
|
+
tags,
|
|
24781
|
+
output: outputs,
|
|
24782
|
+
metadata: { ...kwargs }
|
|
24783
|
+
});
|
|
24784
|
+
}
|
|
24785
|
+
async handleToolStart(tool, input, runId, parentRunId, tags, metadata, runName) {
|
|
24786
|
+
this.startSpan({
|
|
24787
|
+
runId,
|
|
24788
|
+
parentRunId,
|
|
24789
|
+
name: _nullishCoalesce(_nullishCoalesce(runName, () => ( getSerializedName(tool))), () => ( "Tool")),
|
|
24790
|
+
type: "llm",
|
|
24791
|
+
event: {
|
|
24792
|
+
input: safeJsonParse(input),
|
|
24793
|
+
tags,
|
|
24794
|
+
metadata: {
|
|
24795
|
+
metadata,
|
|
24796
|
+
serialized: tool,
|
|
24797
|
+
input_str: input,
|
|
24798
|
+
input: safeJsonParse(input),
|
|
24799
|
+
name: runName
|
|
24800
|
+
}
|
|
24801
|
+
}
|
|
24802
|
+
});
|
|
24803
|
+
}
|
|
24804
|
+
async handleToolError(err, runId, parentRunId, tags) {
|
|
24805
|
+
this.endSpan({ runId, parentRunId, error: err, tags });
|
|
24806
|
+
}
|
|
24807
|
+
async handleToolEnd(output, runId, parentRunId, tags) {
|
|
24808
|
+
this.endSpan({ runId, parentRunId, output, tags });
|
|
24809
|
+
}
|
|
24810
|
+
async handleAgentAction(action, runId, parentRunId, tags) {
|
|
24811
|
+
this.startSpan({
|
|
24812
|
+
runId,
|
|
24813
|
+
parentRunId,
|
|
24814
|
+
type: "llm",
|
|
24815
|
+
name: typeof action.tool === "string" ? action.tool : "Agent",
|
|
24816
|
+
event: {
|
|
24817
|
+
input: action,
|
|
24818
|
+
tags
|
|
24819
|
+
}
|
|
24820
|
+
});
|
|
24821
|
+
}
|
|
24822
|
+
async handleAgentEnd(action, runId, parentRunId, tags) {
|
|
24823
|
+
this.endSpan({ runId, parentRunId, output: action, tags });
|
|
24824
|
+
}
|
|
24825
|
+
async handleRetrieverStart(retriever, query, runId, parentRunId, tags, metadata, name) {
|
|
24826
|
+
this.startSpan({
|
|
24827
|
+
runId,
|
|
24828
|
+
parentRunId,
|
|
24829
|
+
name: _nullishCoalesce(_nullishCoalesce(name, () => ( getSerializedName(retriever))), () => ( "Retriever")),
|
|
24830
|
+
type: "function",
|
|
24831
|
+
event: {
|
|
24832
|
+
input: query,
|
|
24833
|
+
tags,
|
|
24834
|
+
metadata: {
|
|
24835
|
+
serialized: retriever,
|
|
24836
|
+
metadata,
|
|
24837
|
+
name
|
|
24838
|
+
}
|
|
24839
|
+
}
|
|
24840
|
+
});
|
|
24841
|
+
}
|
|
24842
|
+
async handleRetrieverEnd(documents, runId, parentRunId, tags) {
|
|
24843
|
+
this.endSpan({ runId, parentRunId, output: documents, tags });
|
|
24844
|
+
}
|
|
24845
|
+
async handleRetrieverError(err, runId, parentRunId, tags) {
|
|
24846
|
+
this.endSpan({ runId, parentRunId, error: err, tags });
|
|
24847
|
+
}
|
|
24848
|
+
async handleLLMNewToken(_token, _idx, runId, _parentRunId, _tags) {
|
|
24849
|
+
if (!this.firstTokenTimes.has(runId)) {
|
|
24850
|
+
const now2 = Date.now();
|
|
24851
|
+
this.firstTokenTimes.set(runId, now2);
|
|
24852
|
+
const start = this.startTimes.get(runId);
|
|
24853
|
+
if (start !== void 0) {
|
|
24854
|
+
this.ttftMs.set(runId, (now2 - start) / 1e3);
|
|
24855
|
+
}
|
|
24856
|
+
}
|
|
24857
|
+
}
|
|
24858
|
+
}, _class23);
|
|
24859
|
+
function getSerializedName(serialized) {
|
|
24860
|
+
if (typeof serialized.name === "string") {
|
|
24861
|
+
return serialized.name;
|
|
24862
|
+
}
|
|
24863
|
+
const lastIdPart = _optionalChain([serialized, 'access', _636 => _636.id, 'optionalAccess', _637 => _637.at, 'call', _638 => _638(-1)]);
|
|
24864
|
+
return typeof lastIdPart === "string" ? lastIdPart : void 0;
|
|
24865
|
+
}
|
|
24866
|
+
function cleanObject(obj) {
|
|
24867
|
+
return Object.fromEntries(
|
|
24868
|
+
Object.entries(obj).filter(([, value]) => {
|
|
24869
|
+
if (typeof value !== "number") {
|
|
24870
|
+
return false;
|
|
24871
|
+
}
|
|
24872
|
+
return Number.isFinite(value);
|
|
24873
|
+
})
|
|
24874
|
+
);
|
|
24875
|
+
}
|
|
24876
|
+
function walkGenerations(response) {
|
|
24877
|
+
const result = [];
|
|
24878
|
+
const generations = response.generations || [];
|
|
24879
|
+
for (const batch of generations) {
|
|
24880
|
+
if (Array.isArray(batch)) {
|
|
24881
|
+
for (const generation of batch) {
|
|
24882
|
+
if (isRecord(generation)) {
|
|
24883
|
+
result.push(generation);
|
|
24884
|
+
}
|
|
24885
|
+
}
|
|
24886
|
+
} else if (isRecord(batch)) {
|
|
24887
|
+
result.push(batch);
|
|
24888
|
+
}
|
|
24889
|
+
}
|
|
24890
|
+
return result;
|
|
24891
|
+
}
|
|
24892
|
+
function getModelNameFromResponse(response) {
|
|
24893
|
+
for (const generation of walkGenerations(response)) {
|
|
24894
|
+
const message = generation.message;
|
|
24895
|
+
if (!isRecord(message)) {
|
|
24896
|
+
continue;
|
|
24897
|
+
}
|
|
24898
|
+
const responseMetadata = message.response_metadata;
|
|
24899
|
+
if (!isRecord(responseMetadata)) {
|
|
24900
|
+
continue;
|
|
24901
|
+
}
|
|
24902
|
+
const modelName3 = _nullishCoalesce(responseMetadata.model_name, () => ( responseMetadata.model));
|
|
24903
|
+
if (typeof modelName3 === "string") {
|
|
24904
|
+
return modelName3;
|
|
24905
|
+
}
|
|
24906
|
+
}
|
|
24907
|
+
const llmOutput = response.llmOutput || {};
|
|
24908
|
+
const modelName2 = _nullishCoalesce(llmOutput.model_name, () => ( llmOutput.model));
|
|
24909
|
+
return typeof modelName2 === "string" ? modelName2 : void 0;
|
|
24910
|
+
}
|
|
24911
|
+
function getMetricsFromResponse(response) {
|
|
24912
|
+
for (const generation of walkGenerations(response)) {
|
|
24913
|
+
const message = generation.message;
|
|
24914
|
+
if (!isRecord(message)) {
|
|
24915
|
+
continue;
|
|
24916
|
+
}
|
|
24917
|
+
const usageMetadata = message.usage_metadata;
|
|
24918
|
+
if (!isRecord(usageMetadata)) {
|
|
24919
|
+
continue;
|
|
24920
|
+
}
|
|
24921
|
+
const inputTokenDetails = usageMetadata.input_token_details;
|
|
24922
|
+
return cleanObject({
|
|
24923
|
+
total_tokens: usageMetadata.total_tokens,
|
|
24924
|
+
prompt_tokens: usageMetadata.input_tokens,
|
|
24925
|
+
completion_tokens: usageMetadata.output_tokens,
|
|
24926
|
+
prompt_cache_creation_tokens: isRecord(inputTokenDetails) ? inputTokenDetails.cache_creation : void 0,
|
|
24927
|
+
prompt_cached_tokens: isRecord(inputTokenDetails) ? inputTokenDetails.cache_read : void 0
|
|
24928
|
+
});
|
|
24929
|
+
}
|
|
24930
|
+
const llmOutput = response.llmOutput || {};
|
|
24931
|
+
const tokenUsage = isRecord(llmOutput.tokenUsage) ? llmOutput.tokenUsage : isRecord(llmOutput.estimatedTokens) ? llmOutput.estimatedTokens : {};
|
|
24932
|
+
return cleanObject({
|
|
24933
|
+
total_tokens: tokenUsage.totalTokens,
|
|
24934
|
+
prompt_tokens: tokenUsage.promptTokens,
|
|
24935
|
+
completion_tokens: tokenUsage.completionTokens
|
|
24936
|
+
});
|
|
24937
|
+
}
|
|
24938
|
+
function safeJsonParse(input) {
|
|
24939
|
+
try {
|
|
24940
|
+
return JSON.parse(input);
|
|
24941
|
+
} catch (e56) {
|
|
24942
|
+
return input;
|
|
24943
|
+
}
|
|
24944
|
+
}
|
|
24945
|
+
function isRecord(value) {
|
|
24946
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
24947
|
+
}
|
|
24948
|
+
|
|
24949
|
+
// src/instrumentation/plugins/langchain-channels.ts
|
|
24950
|
+
var langChainChannels = defineChannels("@langchain/core", {
|
|
24951
|
+
configure: channel({
|
|
24952
|
+
channelName: "CallbackManager.configure",
|
|
24953
|
+
kind: "sync-stream"
|
|
24954
|
+
}),
|
|
24955
|
+
configureSync: channel({
|
|
24956
|
+
channelName: "CallbackManager._configureSync",
|
|
24957
|
+
kind: "sync-stream"
|
|
24958
|
+
})
|
|
24959
|
+
});
|
|
24960
|
+
|
|
24961
|
+
// src/instrumentation/plugins/langchain-plugin.ts
|
|
24962
|
+
var LangChainPlugin = (_class24 = class extends BasePlugin {constructor(...args7) { super(...args7); _class24.prototype.__init84.call(this); }
|
|
24963
|
+
__init84() {this.injectedManagers = /* @__PURE__ */ new WeakSet()}
|
|
24964
|
+
onEnable() {
|
|
24965
|
+
this.subscribeToConfigure(langChainChannels.configure);
|
|
24966
|
+
this.subscribeToConfigure(langChainChannels.configureSync);
|
|
24967
|
+
}
|
|
24968
|
+
onDisable() {
|
|
24969
|
+
for (const unsubscribe of this.unsubscribers) {
|
|
24970
|
+
unsubscribe();
|
|
24971
|
+
}
|
|
24972
|
+
this.unsubscribers = [];
|
|
24973
|
+
this.injectedManagers = /* @__PURE__ */ new WeakSet();
|
|
24974
|
+
}
|
|
24975
|
+
subscribeToConfigure(channel2) {
|
|
24976
|
+
const tracingChannel2 = channel2.tracingChannel();
|
|
24977
|
+
const handlers = {
|
|
24978
|
+
start: (event) => {
|
|
24979
|
+
injectHandlerIntoArguments(event.arguments);
|
|
24980
|
+
},
|
|
24981
|
+
end: (event) => {
|
|
24982
|
+
this.injectHandler(event.result);
|
|
24983
|
+
}
|
|
24984
|
+
};
|
|
24985
|
+
tracingChannel2.subscribe(handlers);
|
|
24986
|
+
this.unsubscribers.push(() => {
|
|
24987
|
+
tracingChannel2.unsubscribe(handlers);
|
|
24988
|
+
});
|
|
24989
|
+
}
|
|
24990
|
+
injectHandler(result) {
|
|
24991
|
+
if (!isCallbackManager(result)) {
|
|
24992
|
+
return;
|
|
24993
|
+
}
|
|
24994
|
+
if (this.injectedManagers.has(result) || hasBraintrustHandler(result)) {
|
|
24995
|
+
return;
|
|
24996
|
+
}
|
|
24997
|
+
try {
|
|
24998
|
+
result.addHandler(new BraintrustLangChainCallbackHandler(), true);
|
|
24999
|
+
this.injectedManagers.add(result);
|
|
25000
|
+
} catch (e57) {
|
|
25001
|
+
}
|
|
25002
|
+
}
|
|
25003
|
+
}, _class24);
|
|
25004
|
+
function isCallbackManager(value) {
|
|
25005
|
+
if (typeof value !== "object" || value === null) {
|
|
25006
|
+
return false;
|
|
25007
|
+
}
|
|
25008
|
+
const maybeManager = value;
|
|
25009
|
+
return typeof maybeManager.addHandler === "function";
|
|
25010
|
+
}
|
|
25011
|
+
function hasBraintrustHandler(manager) {
|
|
25012
|
+
return _nullishCoalesce(_optionalChain([manager, 'access', _639 => _639.handlers, 'optionalAccess', _640 => _640.some, 'call', _641 => _641((handler) => {
|
|
25013
|
+
if (typeof handler !== "object" || handler === null) {
|
|
25014
|
+
return false;
|
|
25015
|
+
}
|
|
25016
|
+
const name = Reflect.get(handler, "name");
|
|
25017
|
+
return name === BRAINTRUST_LANGCHAIN_CALLBACK_HANDLER_NAME;
|
|
25018
|
+
})]), () => ( false));
|
|
25019
|
+
}
|
|
25020
|
+
function injectHandlerIntoArguments(args) {
|
|
25021
|
+
if (!isWritableArgumentsObject(args)) {
|
|
25022
|
+
return;
|
|
25023
|
+
}
|
|
25024
|
+
const inheritedHandlers = Reflect.get(args, "0");
|
|
25025
|
+
const handler = new BraintrustLangChainCallbackHandler();
|
|
25026
|
+
if (inheritedHandlers === void 0 || inheritedHandlers === null) {
|
|
25027
|
+
Reflect.set(args, "0", [handler]);
|
|
25028
|
+
return;
|
|
25029
|
+
}
|
|
25030
|
+
if (Array.isArray(inheritedHandlers)) {
|
|
25031
|
+
if (!inheritedHandlers.some(isBraintrustHandler)) {
|
|
25032
|
+
inheritedHandlers.push(handler);
|
|
25033
|
+
}
|
|
25034
|
+
}
|
|
25035
|
+
}
|
|
25036
|
+
function isWritableArgumentsObject(args) {
|
|
25037
|
+
return typeof args === "object" && args !== null;
|
|
25038
|
+
}
|
|
25039
|
+
function isBraintrustHandler(handler) {
|
|
25040
|
+
if (typeof handler !== "object" || handler === null) {
|
|
25041
|
+
return false;
|
|
25042
|
+
}
|
|
25043
|
+
return Reflect.get(handler, "name") === BRAINTRUST_LANGCHAIN_CALLBACK_HANDLER_NAME;
|
|
25044
|
+
}
|
|
25045
|
+
|
|
25046
|
+
// src/instrumentation/braintrust-plugin.ts
|
|
25047
|
+
function getIntegrationConfig(integrations, key) {
|
|
25048
|
+
return integrations[key];
|
|
25049
|
+
}
|
|
25050
|
+
var BraintrustPlugin = (_class25 = class extends BasePlugin {
|
|
25051
|
+
|
|
25052
|
+
__init85() {this.openaiPlugin = null}
|
|
25053
|
+
__init86() {this.openAICodexPlugin = null}
|
|
25054
|
+
__init87() {this.anthropicPlugin = null}
|
|
25055
|
+
__init88() {this.aiSDKPlugin = null}
|
|
25056
|
+
__init89() {this.claudeAgentSDKPlugin = null}
|
|
25057
|
+
__init90() {this.cursorSDKPlugin = null}
|
|
25058
|
+
__init91() {this.openAIAgentsPlugin = null}
|
|
25059
|
+
__init92() {this.googleGenAIPlugin = null}
|
|
25060
|
+
__init93() {this.huggingFacePlugin = null}
|
|
25061
|
+
__init94() {this.openRouterPlugin = null}
|
|
25062
|
+
__init95() {this.openRouterAgentPlugin = null}
|
|
25063
|
+
__init96() {this.mistralPlugin = null}
|
|
25064
|
+
__init97() {this.googleADKPlugin = null}
|
|
25065
|
+
__init98() {this.coherePlugin = null}
|
|
25066
|
+
__init99() {this.groqPlugin = null}
|
|
25067
|
+
__init100() {this.genkitPlugin = null}
|
|
25068
|
+
__init101() {this.gitHubCopilotPlugin = null}
|
|
25069
|
+
__init102() {this.fluePlugin = null}
|
|
25070
|
+
__init103() {this.langChainPlugin = null}
|
|
25071
|
+
constructor(config = {}) {
|
|
25072
|
+
super();_class25.prototype.__init85.call(this);_class25.prototype.__init86.call(this);_class25.prototype.__init87.call(this);_class25.prototype.__init88.call(this);_class25.prototype.__init89.call(this);_class25.prototype.__init90.call(this);_class25.prototype.__init91.call(this);_class25.prototype.__init92.call(this);_class25.prototype.__init93.call(this);_class25.prototype.__init94.call(this);_class25.prototype.__init95.call(this);_class25.prototype.__init96.call(this);_class25.prototype.__init97.call(this);_class25.prototype.__init98.call(this);_class25.prototype.__init99.call(this);_class25.prototype.__init100.call(this);_class25.prototype.__init101.call(this);_class25.prototype.__init102.call(this);_class25.prototype.__init103.call(this);;
|
|
25073
|
+
this.config = config;
|
|
25074
|
+
}
|
|
25075
|
+
onEnable() {
|
|
25076
|
+
const integrations = this.config.integrations || {};
|
|
25077
|
+
if (integrations.openai !== false) {
|
|
25078
|
+
this.openaiPlugin = new OpenAIPlugin();
|
|
25079
|
+
this.openaiPlugin.enable();
|
|
25080
|
+
}
|
|
25081
|
+
if (integrations.openaiCodexSDK !== false) {
|
|
25082
|
+
this.openAICodexPlugin = new OpenAICodexPlugin();
|
|
25083
|
+
this.openAICodexPlugin.enable();
|
|
25084
|
+
}
|
|
25085
|
+
if (integrations.anthropic !== false) {
|
|
25086
|
+
this.anthropicPlugin = new AnthropicPlugin();
|
|
25087
|
+
this.anthropicPlugin.enable();
|
|
22874
25088
|
}
|
|
22875
25089
|
if (integrations.aisdk !== false && integrations.vercel !== false) {
|
|
22876
25090
|
this.aiSDKPlugin = new AISDKPlugin();
|
|
@@ -22884,6 +25098,10 @@ var BraintrustPlugin = (_class20 = class extends BasePlugin {
|
|
|
22884
25098
|
this.cursorSDKPlugin = new CursorSDKPlugin();
|
|
22885
25099
|
this.cursorSDKPlugin.enable();
|
|
22886
25100
|
}
|
|
25101
|
+
if (integrations.openAIAgents !== false) {
|
|
25102
|
+
this.openAIAgentsPlugin = new OpenAIAgentsPlugin();
|
|
25103
|
+
this.openAIAgentsPlugin.enable();
|
|
25104
|
+
}
|
|
22887
25105
|
if (integrations.googleGenAI !== false && integrations.google !== false) {
|
|
22888
25106
|
this.googleGenAIPlugin = new GoogleGenAIPlugin();
|
|
22889
25107
|
this.googleGenAIPlugin.enable();
|
|
@@ -22924,6 +25142,14 @@ var BraintrustPlugin = (_class20 = class extends BasePlugin {
|
|
|
22924
25142
|
this.gitHubCopilotPlugin = new GitHubCopilotPlugin();
|
|
22925
25143
|
this.gitHubCopilotPlugin.enable();
|
|
22926
25144
|
}
|
|
25145
|
+
if (getIntegrationConfig(integrations, "flue") !== false) {
|
|
25146
|
+
this.fluePlugin = new FluePlugin();
|
|
25147
|
+
this.fluePlugin.enable();
|
|
25148
|
+
}
|
|
25149
|
+
if (integrations.langchain !== false && integrations.langgraph !== false) {
|
|
25150
|
+
this.langChainPlugin = new LangChainPlugin();
|
|
25151
|
+
this.langChainPlugin.enable();
|
|
25152
|
+
}
|
|
22927
25153
|
}
|
|
22928
25154
|
onDisable() {
|
|
22929
25155
|
if (this.openaiPlugin) {
|
|
@@ -22950,6 +25176,10 @@ var BraintrustPlugin = (_class20 = class extends BasePlugin {
|
|
|
22950
25176
|
this.cursorSDKPlugin.disable();
|
|
22951
25177
|
this.cursorSDKPlugin = null;
|
|
22952
25178
|
}
|
|
25179
|
+
if (this.openAIAgentsPlugin) {
|
|
25180
|
+
this.openAIAgentsPlugin.disable();
|
|
25181
|
+
this.openAIAgentsPlugin = null;
|
|
25182
|
+
}
|
|
22953
25183
|
if (this.googleGenAIPlugin) {
|
|
22954
25184
|
this.googleGenAIPlugin.disable();
|
|
22955
25185
|
this.googleGenAIPlugin = null;
|
|
@@ -22990,8 +25220,103 @@ var BraintrustPlugin = (_class20 = class extends BasePlugin {
|
|
|
22990
25220
|
this.gitHubCopilotPlugin.disable();
|
|
22991
25221
|
this.gitHubCopilotPlugin = null;
|
|
22992
25222
|
}
|
|
25223
|
+
if (this.fluePlugin) {
|
|
25224
|
+
this.fluePlugin.disable();
|
|
25225
|
+
this.fluePlugin = null;
|
|
25226
|
+
}
|
|
25227
|
+
if (this.langChainPlugin) {
|
|
25228
|
+
this.langChainPlugin.disable();
|
|
25229
|
+
this.langChainPlugin = null;
|
|
25230
|
+
}
|
|
22993
25231
|
}
|
|
22994
|
-
},
|
|
25232
|
+
}, _class25);
|
|
25233
|
+
|
|
25234
|
+
// src/instrumentation/config.ts
|
|
25235
|
+
var envIntegrationAliases = {
|
|
25236
|
+
openai: "openai",
|
|
25237
|
+
"openai-codex": "openaiCodexSDK",
|
|
25238
|
+
"openai-codex-sdk": "openaiCodexSDK",
|
|
25239
|
+
openaicodexsdk: "openaiCodexSDK",
|
|
25240
|
+
codex: "openaiCodexSDK",
|
|
25241
|
+
"codex-sdk": "openaiCodexSDK",
|
|
25242
|
+
anthropic: "anthropic",
|
|
25243
|
+
aisdk: "aisdk",
|
|
25244
|
+
"ai-sdk": "aisdk",
|
|
25245
|
+
"vercel-ai": "aisdk",
|
|
25246
|
+
vercel: "vercel",
|
|
25247
|
+
claudeagentsdk: "claudeAgentSDK",
|
|
25248
|
+
"claude-agent-sdk": "claudeAgentSDK",
|
|
25249
|
+
cursor: "cursor",
|
|
25250
|
+
"cursor-sdk": "cursorSDK",
|
|
25251
|
+
cursorsdk: "cursorSDK",
|
|
25252
|
+
flue: "flue",
|
|
25253
|
+
"flue-runtime": "flue",
|
|
25254
|
+
"openai-agents": "openAIAgents",
|
|
25255
|
+
openaiagents: "openAIAgents",
|
|
25256
|
+
"openai-agents-core": "openAIAgents",
|
|
25257
|
+
openaiagentscore: "openAIAgents",
|
|
25258
|
+
google: "google",
|
|
25259
|
+
"google-genai": "googleGenAI",
|
|
25260
|
+
googlegenai: "googleGenAI",
|
|
25261
|
+
huggingface: "huggingface",
|
|
25262
|
+
openrouter: "openrouter",
|
|
25263
|
+
openrouteragent: "openrouterAgent",
|
|
25264
|
+
"openrouter-agent": "openrouterAgent",
|
|
25265
|
+
mistral: "mistral",
|
|
25266
|
+
googleadk: "googleADK",
|
|
25267
|
+
"google-adk": "googleADK",
|
|
25268
|
+
cohere: "cohere",
|
|
25269
|
+
groq: "groq",
|
|
25270
|
+
"groq-sdk": "groq",
|
|
25271
|
+
genkit: "genkit",
|
|
25272
|
+
"firebase-genkit": "genkit",
|
|
25273
|
+
githubcopilot: "gitHubCopilot",
|
|
25274
|
+
"github-copilot": "gitHubCopilot",
|
|
25275
|
+
"copilot-sdk": "gitHubCopilot",
|
|
25276
|
+
langchain: "langchain",
|
|
25277
|
+
"langchain-js": "langchain",
|
|
25278
|
+
"@langchain": "langchain",
|
|
25279
|
+
langgraph: "langgraph"
|
|
25280
|
+
};
|
|
25281
|
+
function getDefaultInstrumentationIntegrations() {
|
|
25282
|
+
return {
|
|
25283
|
+
openai: true,
|
|
25284
|
+
openaiCodexSDK: true,
|
|
25285
|
+
anthropic: true,
|
|
25286
|
+
vercel: true,
|
|
25287
|
+
aisdk: true,
|
|
25288
|
+
google: true,
|
|
25289
|
+
googleGenAI: true,
|
|
25290
|
+
googleADK: true,
|
|
25291
|
+
huggingface: true,
|
|
25292
|
+
claudeAgentSDK: true,
|
|
25293
|
+
cursor: true,
|
|
25294
|
+
cursorSDK: true,
|
|
25295
|
+
flue: true,
|
|
25296
|
+
openAIAgents: true,
|
|
25297
|
+
openrouter: true,
|
|
25298
|
+
openrouterAgent: true,
|
|
25299
|
+
mistral: true,
|
|
25300
|
+
cohere: true,
|
|
25301
|
+
groq: true,
|
|
25302
|
+
genkit: true,
|
|
25303
|
+
gitHubCopilot: true,
|
|
25304
|
+
langchain: true,
|
|
25305
|
+
langgraph: true
|
|
25306
|
+
};
|
|
25307
|
+
}
|
|
25308
|
+
function readDisabledInstrumentationEnvConfig(disabledList) {
|
|
25309
|
+
const integrations = {};
|
|
25310
|
+
if (disabledList) {
|
|
25311
|
+
for (const value of disabledList.split(",")) {
|
|
25312
|
+
const sdk = value.trim().toLowerCase();
|
|
25313
|
+
if (sdk.length > 0) {
|
|
25314
|
+
integrations[_nullishCoalesce(envIntegrationAliases[sdk], () => ( sdk))] = false;
|
|
25315
|
+
}
|
|
25316
|
+
}
|
|
25317
|
+
}
|
|
25318
|
+
return { integrations };
|
|
25319
|
+
}
|
|
22995
25320
|
|
|
22996
25321
|
// src/instrumentation/registry.ts
|
|
22997
25322
|
var REGISTRY_STATE_KEY = /* @__PURE__ */ Symbol.for("braintrust.registry");
|
|
@@ -22999,10 +25324,10 @@ function getSharedState() {
|
|
|
22999
25324
|
const state = globalThis[/* @__PURE__ */ Symbol.for("braintrust-state")];
|
|
23000
25325
|
return state && typeof state === "object" ? state : void 0;
|
|
23001
25326
|
}
|
|
23002
|
-
var PluginRegistry = (
|
|
23003
|
-
|
|
23004
|
-
|
|
23005
|
-
|
|
25327
|
+
var PluginRegistry = (_class26 = class {constructor() { _class26.prototype.__init104.call(this);_class26.prototype.__init105.call(this);_class26.prototype.__init106.call(this); }
|
|
25328
|
+
__init104() {this.braintrustPlugin = null}
|
|
25329
|
+
__init105() {this.config = {}}
|
|
25330
|
+
__init106() {this.enabled = false}
|
|
23006
25331
|
/**
|
|
23007
25332
|
* Configure which integrations should be enabled.
|
|
23008
25333
|
* This must be called before any SDK imports to take effect.
|
|
@@ -23071,52 +25396,18 @@ var PluginRegistry = (_class21 = class {constructor() { _class21.prototype.__ini
|
|
|
23071
25396
|
* Get default configuration (all integrations enabled).
|
|
23072
25397
|
*/
|
|
23073
25398
|
getDefaultConfig() {
|
|
23074
|
-
return
|
|
23075
|
-
openai: true,
|
|
23076
|
-
openaiCodexSDK: true,
|
|
23077
|
-
anthropic: true,
|
|
23078
|
-
vercel: true,
|
|
23079
|
-
aisdk: true,
|
|
23080
|
-
google: true,
|
|
23081
|
-
googleGenAI: true,
|
|
23082
|
-
googleADK: true,
|
|
23083
|
-
huggingface: true,
|
|
23084
|
-
claudeAgentSDK: true,
|
|
23085
|
-
cursor: true,
|
|
23086
|
-
cursorSDK: true,
|
|
23087
|
-
openrouter: true,
|
|
23088
|
-
openrouterAgent: true,
|
|
23089
|
-
mistral: true,
|
|
23090
|
-
cohere: true,
|
|
23091
|
-
groq: true,
|
|
23092
|
-
genkit: true,
|
|
23093
|
-
gitHubCopilot: true
|
|
23094
|
-
};
|
|
25399
|
+
return getDefaultInstrumentationIntegrations();
|
|
23095
25400
|
}
|
|
23096
25401
|
/**
|
|
23097
25402
|
* Read configuration from environment variables.
|
|
23098
25403
|
* Supports: BRAINTRUST_DISABLE_INSTRUMENTATION=openai,anthropic,...
|
|
23099
25404
|
*/
|
|
23100
25405
|
readEnvConfig() {
|
|
23101
|
-
|
|
23102
|
-
|
|
23103
|
-
|
|
23104
|
-
const disabled = disabledList.split(",").map((s) => s.trim().toLowerCase()).filter((s) => s.length > 0);
|
|
23105
|
-
for (const sdk of disabled) {
|
|
23106
|
-
if (sdk === "cursor-sdk") {
|
|
23107
|
-
integrations.cursorSDK = false;
|
|
23108
|
-
} else if (sdk === "githubcopilot" || sdk === "github-copilot" || sdk === "copilot-sdk") {
|
|
23109
|
-
integrations.gitHubCopilot = false;
|
|
23110
|
-
} else if (sdk === "openai-codex-sdk") {
|
|
23111
|
-
integrations.openaiCodexSDK = false;
|
|
23112
|
-
} else {
|
|
23113
|
-
integrations[sdk] = false;
|
|
23114
|
-
}
|
|
23115
|
-
}
|
|
23116
|
-
}
|
|
23117
|
-
return { integrations };
|
|
25406
|
+
return readDisabledInstrumentationEnvConfig(
|
|
25407
|
+
isomorph_default.getEnv("BRAINTRUST_DISABLE_INSTRUMENTATION")
|
|
25408
|
+
);
|
|
23118
25409
|
}
|
|
23119
|
-
},
|
|
25410
|
+
}, _class26);
|
|
23120
25411
|
var registry = new PluginRegistry();
|
|
23121
25412
|
|
|
23122
25413
|
// src/node/config.ts
|
|
@@ -24332,11 +26623,11 @@ var SpanFetcher = class _SpanFetcher extends ObjectFetcher {
|
|
|
24332
26623
|
return this._state;
|
|
24333
26624
|
}
|
|
24334
26625
|
};
|
|
24335
|
-
var CachedSpanFetcher = (
|
|
24336
|
-
|
|
24337
|
-
|
|
26626
|
+
var CachedSpanFetcher = (_class27 = class {
|
|
26627
|
+
__init107() {this.spanCache = /* @__PURE__ */ new Map()}
|
|
26628
|
+
__init108() {this.allFetched = false}
|
|
24338
26629
|
|
|
24339
|
-
constructor(objectTypeOrFetchFn, objectId, rootSpanId, getState) {;
|
|
26630
|
+
constructor(objectTypeOrFetchFn, objectId, rootSpanId, getState) {;_class27.prototype.__init107.call(this);_class27.prototype.__init108.call(this);
|
|
24340
26631
|
if (typeof objectTypeOrFetchFn === "function") {
|
|
24341
26632
|
this.fetchFn = objectTypeOrFetchFn;
|
|
24342
26633
|
} else {
|
|
@@ -24351,7 +26642,7 @@ var CachedSpanFetcher = (_class22 = class {
|
|
|
24351
26642
|
spanType
|
|
24352
26643
|
);
|
|
24353
26644
|
const rows = await fetcher.fetchedData();
|
|
24354
|
-
return rows.filter((row) => _optionalChain([row, 'access',
|
|
26645
|
+
return rows.filter((row) => _optionalChain([row, 'access', _642 => _642.span_attributes, 'optionalAccess', _643 => _643.purpose]) !== "scorer").map((row) => ({
|
|
24355
26646
|
input: row.input,
|
|
24356
26647
|
output: row.output,
|
|
24357
26648
|
metadata: row.metadata,
|
|
@@ -24385,7 +26676,7 @@ var CachedSpanFetcher = (_class22 = class {
|
|
|
24385
26676
|
async fetchSpans(spanType) {
|
|
24386
26677
|
const spans = await this.fetchFn(spanType);
|
|
24387
26678
|
for (const span of spans) {
|
|
24388
|
-
const type = _nullishCoalesce(_optionalChain([span, 'access',
|
|
26679
|
+
const type = _nullishCoalesce(_optionalChain([span, 'access', _644 => _644.span_attributes, 'optionalAccess', _645 => _645.type]), () => ( ""));
|
|
24389
26680
|
const existing = _nullishCoalesce(this.spanCache.get(type), () => ( []));
|
|
24390
26681
|
existing.push(span);
|
|
24391
26682
|
this.spanCache.set(type, existing);
|
|
@@ -24402,24 +26693,24 @@ var CachedSpanFetcher = (_class22 = class {
|
|
|
24402
26693
|
}
|
|
24403
26694
|
return result;
|
|
24404
26695
|
}
|
|
24405
|
-
},
|
|
24406
|
-
var LocalTrace = (
|
|
26696
|
+
}, _class27);
|
|
26697
|
+
var LocalTrace = (_class28 = class {
|
|
24407
26698
|
|
|
24408
26699
|
|
|
24409
26700
|
|
|
24410
26701
|
|
|
24411
26702
|
|
|
24412
|
-
|
|
24413
|
-
|
|
26703
|
+
__init109() {this.spansFlushed = false}
|
|
26704
|
+
__init110() {this.spansFlushPromise = null}
|
|
24414
26705
|
|
|
24415
|
-
|
|
26706
|
+
__init111() {this.threadCache = /* @__PURE__ */ new Map()}
|
|
24416
26707
|
constructor({
|
|
24417
26708
|
objectType,
|
|
24418
26709
|
objectId,
|
|
24419
26710
|
rootSpanId,
|
|
24420
26711
|
ensureSpansFlushed,
|
|
24421
26712
|
state
|
|
24422
|
-
}) {;
|
|
26713
|
+
}) {;_class28.prototype.__init109.call(this);_class28.prototype.__init110.call(this);_class28.prototype.__init111.call(this);
|
|
24423
26714
|
this.objectType = objectType;
|
|
24424
26715
|
this.objectId = objectId;
|
|
24425
26716
|
this.rootSpanId = rootSpanId;
|
|
@@ -24465,11 +26756,11 @@ var LocalTrace = (_class23 = class {
|
|
|
24465
26756
|
const cachedSpans = this.state.spanCache.getByRootSpanId(this.rootSpanId);
|
|
24466
26757
|
if (cachedSpans && cachedSpans.length > 0) {
|
|
24467
26758
|
let spans = cachedSpans.filter(
|
|
24468
|
-
(span) => _optionalChain([span, 'access',
|
|
26759
|
+
(span) => _optionalChain([span, 'access', _646 => _646.span_attributes, 'optionalAccess', _647 => _647.purpose]) !== "scorer"
|
|
24469
26760
|
);
|
|
24470
26761
|
if (spanType && spanType.length > 0) {
|
|
24471
26762
|
spans = spans.filter(
|
|
24472
|
-
(span) => spanType.includes(_nullishCoalesce(_optionalChain([span, 'access',
|
|
26763
|
+
(span) => spanType.includes(_nullishCoalesce(_optionalChain([span, 'access', _648 => _648.span_attributes, 'optionalAccess', _649 => _649.type]), () => ( "")))
|
|
24473
26764
|
);
|
|
24474
26765
|
}
|
|
24475
26766
|
return spans.map((span) => ({
|
|
@@ -24488,7 +26779,7 @@ var LocalTrace = (_class23 = class {
|
|
|
24488
26779
|
* Calls the API with the project_default preprocessor (which falls back to "thread").
|
|
24489
26780
|
*/
|
|
24490
26781
|
async getThread(options) {
|
|
24491
|
-
const cacheKey = _nullishCoalesce(_optionalChain([options, 'optionalAccess',
|
|
26782
|
+
const cacheKey = _nullishCoalesce(_optionalChain([options, 'optionalAccess', _650 => _650.preprocessor]), () => ( "project_default"));
|
|
24492
26783
|
if (!this.threadCache.has(cacheKey)) {
|
|
24493
26784
|
const promise = this.fetchThread(options);
|
|
24494
26785
|
this.threadCache.set(cacheKey, promise);
|
|
@@ -24499,7 +26790,7 @@ var LocalTrace = (_class23 = class {
|
|
|
24499
26790
|
await this.ensureSpansReady();
|
|
24500
26791
|
await this.state.login({});
|
|
24501
26792
|
const result = await invoke({
|
|
24502
|
-
globalFunction: _nullishCoalesce(_optionalChain([options, 'optionalAccess',
|
|
26793
|
+
globalFunction: _nullishCoalesce(_optionalChain([options, 'optionalAccess', _651 => _651.preprocessor]), () => ( "project_default")),
|
|
24503
26794
|
functionType: "preprocessor",
|
|
24504
26795
|
input: {
|
|
24505
26796
|
trace_ref: {
|
|
@@ -24530,7 +26821,7 @@ var LocalTrace = (_class23 = class {
|
|
|
24530
26821
|
}
|
|
24531
26822
|
await this.spansFlushPromise;
|
|
24532
26823
|
}
|
|
24533
|
-
},
|
|
26824
|
+
}, _class28);
|
|
24534
26825
|
|
|
24535
26826
|
// src/reporters/progress.ts
|
|
24536
26827
|
var SimpleProgressReporter = class {
|
|
@@ -24675,10 +26966,10 @@ function validateParametersWithJsonSchema(parameters, schema) {
|
|
|
24675
26966
|
const ajv = new (0, _ajv2.default)({ coerceTypes: true, useDefaults: true, strict: false });
|
|
24676
26967
|
const validate = ajv.compile(schema);
|
|
24677
26968
|
if (!validate(parameters)) {
|
|
24678
|
-
const errorMessages = _optionalChain([validate, 'access',
|
|
26969
|
+
const errorMessages = _optionalChain([validate, 'access', _652 => _652.errors, 'optionalAccess', _653 => _653.map, 'call', _654 => _654((err) => {
|
|
24679
26970
|
const path2 = err.instancePath || "root";
|
|
24680
26971
|
return `${path2}: ${err.message}`;
|
|
24681
|
-
}), 'access',
|
|
26972
|
+
}), 'access', _655 => _655.join, 'call', _656 => _656(", ")]);
|
|
24682
26973
|
throw Error(`Invalid parameters: ${errorMessages}`);
|
|
24683
26974
|
}
|
|
24684
26975
|
return rehydrateRemoteParameters(parameters, schema);
|
|
@@ -25018,7 +27309,7 @@ async function runEvaluator(experiment, evaluator, progressReporter, filters, st
|
|
|
25018
27309
|
}
|
|
25019
27310
|
async function runEvaluatorInternal(experiment, evaluator, progressReporter, filters, stream, parameters, collectResults, enableCache) {
|
|
25020
27311
|
if (enableCache) {
|
|
25021
|
-
_optionalChain([(_nullishCoalesce(evaluator.state, () => ( _internalGetGlobalState()))), 'optionalAccess',
|
|
27312
|
+
_optionalChain([(_nullishCoalesce(evaluator.state, () => ( _internalGetGlobalState()))), 'optionalAccess', _657 => _657.spanCache, 'optionalAccess', _658 => _658.start, 'call', _659 => _659()]);
|
|
25022
27313
|
}
|
|
25023
27314
|
try {
|
|
25024
27315
|
if (typeof evaluator.data === "string") {
|
|
@@ -25073,7 +27364,7 @@ async function runEvaluatorInternal(experiment, evaluator, progressReporter, fil
|
|
|
25073
27364
|
const experimentIdPromise = experiment ? (async () => {
|
|
25074
27365
|
try {
|
|
25075
27366
|
return await experiment.id;
|
|
25076
|
-
} catch (
|
|
27367
|
+
} catch (e58) {
|
|
25077
27368
|
return void 0;
|
|
25078
27369
|
}
|
|
25079
27370
|
})() : void 0;
|
|
@@ -25129,7 +27420,7 @@ async function runEvaluatorInternal(experiment, evaluator, progressReporter, fil
|
|
|
25129
27420
|
objectType: parentComponents ? spanObjectTypeV3ToTypedString(
|
|
25130
27421
|
parentComponents.data.object_type
|
|
25131
27422
|
) : "experiment",
|
|
25132
|
-
objectId: await _asyncNullishCoalesce(await _asyncOptionalChain([parentComponents, 'optionalAccess', async
|
|
27423
|
+
objectId: await _asyncNullishCoalesce(await _asyncOptionalChain([parentComponents, 'optionalAccess', async _660 => _660.data, 'access', async _661 => _661.object_id]), async () => ( (experimentIdPromise ? await _asyncNullishCoalesce(await experimentIdPromise, async () => ( "")) : ""))),
|
|
25133
27424
|
rootSpanId: rootSpan.rootSpanId,
|
|
25134
27425
|
ensureSpansFlushed,
|
|
25135
27426
|
state
|
|
@@ -25159,10 +27450,10 @@ async function runEvaluatorInternal(experiment, evaluator, progressReporter, fil
|
|
|
25159
27450
|
span,
|
|
25160
27451
|
parameters: _nullishCoalesce(parameters, () => ( {})),
|
|
25161
27452
|
reportProgress: (event) => {
|
|
25162
|
-
_optionalChain([stream, 'optionalCall',
|
|
27453
|
+
_optionalChain([stream, 'optionalCall', _662 => _662({
|
|
25163
27454
|
...event,
|
|
25164
27455
|
id: rootSpan.id,
|
|
25165
|
-
origin: _optionalChain([baseEvent, 'access',
|
|
27456
|
+
origin: _optionalChain([baseEvent, 'access', _663 => _663.event, 'optionalAccess', _664 => _664.origin]),
|
|
25166
27457
|
name: evaluator.evalName,
|
|
25167
27458
|
object_type: "task"
|
|
25168
27459
|
})]);
|
|
@@ -25350,7 +27641,7 @@ async function runEvaluatorInternal(experiment, evaluator, progressReporter, fil
|
|
|
25350
27641
|
tags: tags.length ? tags : void 0,
|
|
25351
27642
|
metadata,
|
|
25352
27643
|
error,
|
|
25353
|
-
origin: _optionalChain([baseEvent, 'access',
|
|
27644
|
+
origin: _optionalChain([baseEvent, 'access', _665 => _665.event, 'optionalAccess', _666 => _666.origin])
|
|
25354
27645
|
};
|
|
25355
27646
|
collectedResults.push({
|
|
25356
27647
|
...baseResult,
|
|
@@ -25390,7 +27681,7 @@ async function runEvaluatorInternal(experiment, evaluator, progressReporter, fil
|
|
|
25390
27681
|
break;
|
|
25391
27682
|
}
|
|
25392
27683
|
scheduledTrials++;
|
|
25393
|
-
_optionalChain([progressReporter, 'access',
|
|
27684
|
+
_optionalChain([progressReporter, 'access', _667 => _667.setTotal, 'optionalCall', _668 => _668(evaluator.evalName, scheduledTrials)]);
|
|
25394
27685
|
q.pushAsync({ datum, trialIndex }).catch((e) => {
|
|
25395
27686
|
if (queueErrors.length < 5) {
|
|
25396
27687
|
queueErrors.push(e);
|
|
@@ -25475,9 +27766,9 @@ async function runEvaluatorInternal(experiment, evaluator, progressReporter, fil
|
|
|
25475
27766
|
);
|
|
25476
27767
|
} finally {
|
|
25477
27768
|
if (enableCache) {
|
|
25478
|
-
const spanCache = _optionalChain([(_nullishCoalesce(evaluator.state, () => ( _internalGetGlobalState()))), 'optionalAccess',
|
|
25479
|
-
_optionalChain([spanCache, 'optionalAccess',
|
|
25480
|
-
_optionalChain([spanCache, 'optionalAccess',
|
|
27769
|
+
const spanCache = _optionalChain([(_nullishCoalesce(evaluator.state, () => ( _internalGetGlobalState()))), 'optionalAccess', _669 => _669.spanCache]);
|
|
27770
|
+
_optionalChain([spanCache, 'optionalAccess', _670 => _670.dispose, 'call', _671 => _671()]);
|
|
27771
|
+
_optionalChain([spanCache, 'optionalAccess', _672 => _672.stop, 'call', _673 => _673()]);
|
|
25481
27772
|
}
|
|
25482
27773
|
}
|
|
25483
27774
|
}
|
|
@@ -25717,7 +28008,7 @@ async function cachedLogin(options) {
|
|
|
25717
28008
|
}
|
|
25718
28009
|
function makeCheckAuthorized(allowedOrgName) {
|
|
25719
28010
|
return async (req, _res, next) => {
|
|
25720
|
-
if (!_optionalChain([req, 'access',
|
|
28011
|
+
if (!_optionalChain([req, 'access', _674 => _674.ctx, 'optionalAccess', _675 => _675.token])) {
|
|
25721
28012
|
return next(_httperrors2.default.call(void 0, 401, "Unauthorized"));
|
|
25722
28013
|
}
|
|
25723
28014
|
try {
|
|
@@ -25730,7 +28021,7 @@ function makeCheckAuthorized(allowedOrgName) {
|
|
|
25730
28021
|
return next(_httperrors2.default.call(void 0, 403, errorMessage2));
|
|
25731
28022
|
}
|
|
25732
28023
|
const state = await cachedLogin({
|
|
25733
|
-
apiKey: _optionalChain([req, 'access',
|
|
28024
|
+
apiKey: _optionalChain([req, 'access', _676 => _676.ctx, 'optionalAccess', _677 => _677.token]),
|
|
25734
28025
|
orgName
|
|
25735
28026
|
});
|
|
25736
28027
|
req.ctx.state = state;
|
|
@@ -25922,17 +28213,17 @@ var ProjectBuilder = class {
|
|
|
25922
28213
|
}
|
|
25923
28214
|
};
|
|
25924
28215
|
var projects = new ProjectBuilder();
|
|
25925
|
-
var Project2 = (
|
|
28216
|
+
var Project2 = (_class29 = class {
|
|
25926
28217
|
|
|
25927
28218
|
|
|
25928
28219
|
|
|
25929
28220
|
|
|
25930
28221
|
|
|
25931
28222
|
|
|
25932
|
-
|
|
25933
|
-
|
|
25934
|
-
|
|
25935
|
-
constructor(args) {;
|
|
28223
|
+
__init112() {this._publishableCodeFunctions = []}
|
|
28224
|
+
__init113() {this._publishablePrompts = []}
|
|
28225
|
+
__init114() {this._publishableParameters = []}
|
|
28226
|
+
constructor(args) {;_class29.prototype.__init112.call(this);_class29.prototype.__init113.call(this);_class29.prototype.__init114.call(this);
|
|
25936
28227
|
_initializeSpanContext();
|
|
25937
28228
|
this.name = "name" in args ? args.name : void 0;
|
|
25938
28229
|
this.id = "id" in args ? args.id : void 0;
|
|
@@ -25984,12 +28275,12 @@ var Project2 = (_class24 = class {
|
|
|
25984
28275
|
functions: functionDefinitions
|
|
25985
28276
|
});
|
|
25986
28277
|
}
|
|
25987
|
-
},
|
|
25988
|
-
var ToolBuilder = (
|
|
25989
|
-
constructor(project) {;
|
|
28278
|
+
}, _class29);
|
|
28279
|
+
var ToolBuilder = (_class30 = class {
|
|
28280
|
+
constructor(project) {;_class30.prototype.__init115.call(this);
|
|
25990
28281
|
this.project = project;
|
|
25991
28282
|
}
|
|
25992
|
-
|
|
28283
|
+
__init115() {this.taskCounter = 0}
|
|
25993
28284
|
// This type definition is just a catch all so that the implementation can be
|
|
25994
28285
|
// less specific than the two more specific declarations above.
|
|
25995
28286
|
create(opts) {
|
|
@@ -26014,12 +28305,12 @@ var ToolBuilder = (_class25 = class {
|
|
|
26014
28305
|
this.project.addCodeFunction(tool);
|
|
26015
28306
|
return tool;
|
|
26016
28307
|
}
|
|
26017
|
-
},
|
|
26018
|
-
var ScorerBuilder = (
|
|
26019
|
-
constructor(project) {;
|
|
28308
|
+
}, _class30);
|
|
28309
|
+
var ScorerBuilder = (_class31 = class {
|
|
28310
|
+
constructor(project) {;_class31.prototype.__init116.call(this);
|
|
26020
28311
|
this.project = project;
|
|
26021
28312
|
}
|
|
26022
|
-
|
|
28313
|
+
__init116() {this.taskCounter = 0}
|
|
26023
28314
|
create(opts) {
|
|
26024
28315
|
this.taskCounter++;
|
|
26025
28316
|
let resolvedName = opts.name;
|
|
@@ -26073,7 +28364,7 @@ var ScorerBuilder = (_class26 = class {
|
|
|
26073
28364
|
this.project.addPrompt(codePrompt);
|
|
26074
28365
|
}
|
|
26075
28366
|
}
|
|
26076
|
-
},
|
|
28367
|
+
}, _class31);
|
|
26077
28368
|
var CodeFunction = class {
|
|
26078
28369
|
constructor(project, opts) {
|
|
26079
28370
|
this.project = project;
|
|
@@ -26375,9 +28666,9 @@ function serializeRemoteEvalParametersContainer(parameters) {
|
|
|
26375
28666
|
source: null
|
|
26376
28667
|
};
|
|
26377
28668
|
}
|
|
26378
|
-
var ProjectNameIdMap = (
|
|
26379
|
-
|
|
26380
|
-
|
|
28669
|
+
var ProjectNameIdMap = (_class32 = class {constructor() { _class32.prototype.__init117.call(this);_class32.prototype.__init118.call(this); }
|
|
28670
|
+
__init117() {this.nameToId = {}}
|
|
28671
|
+
__init118() {this.idToName = {}}
|
|
26381
28672
|
async getId(projectName) {
|
|
26382
28673
|
if (!(projectName in this.nameToId)) {
|
|
26383
28674
|
const response = await _internalGetGlobalState().appConn().post_json("api/project/register", {
|
|
@@ -26410,7 +28701,7 @@ var ProjectNameIdMap = (_class27 = class {constructor() { _class27.prototype.__i
|
|
|
26410
28701
|
}
|
|
26411
28702
|
return this.getId(project.name);
|
|
26412
28703
|
}
|
|
26413
|
-
},
|
|
28704
|
+
}, _class32);
|
|
26414
28705
|
|
|
26415
28706
|
// dev/server.ts
|
|
26416
28707
|
function runDevServer(evaluators, opts) {
|
|
@@ -26485,7 +28776,7 @@ function runDevServer(evaluators, opts) {
|
|
|
26485
28776
|
scores,
|
|
26486
28777
|
stream
|
|
26487
28778
|
} = evalBodySchema.parse(req.body);
|
|
26488
|
-
if (!_optionalChain([req, 'access',
|
|
28779
|
+
if (!_optionalChain([req, 'access', _678 => _678.ctx, 'optionalAccess', _679 => _679.state])) {
|
|
26489
28780
|
res.status(500).json({ error: "Braintrust state not initialized in request" });
|
|
26490
28781
|
return;
|
|
26491
28782
|
}
|
|
@@ -26536,12 +28827,12 @@ function runDevServer(evaluators, opts) {
|
|
|
26536
28827
|
...evaluator,
|
|
26537
28828
|
data: evalData.data,
|
|
26538
28829
|
scores: (_nullishCoalesce(evaluator.scores, () => ( []))).concat(
|
|
26539
|
-
_nullishCoalesce(_optionalChain([scores, 'optionalAccess',
|
|
28830
|
+
_nullishCoalesce(_optionalChain([scores, 'optionalAccess', _680 => _680.map, 'call', _681 => _681(
|
|
26540
28831
|
(score) => makeScorer(
|
|
26541
28832
|
state,
|
|
26542
28833
|
score.name,
|
|
26543
28834
|
score.function_id,
|
|
26544
|
-
_optionalChain([req, 'access',
|
|
28835
|
+
_optionalChain([req, 'access', _682 => _682.ctx, 'optionalAccess', _683 => _683.projectId])
|
|
26545
28836
|
)
|
|
26546
28837
|
)]), () => ( []))
|
|
26547
28838
|
),
|