braintrust 3.24.0 → 3.25.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dev/dist/index.d.mts +5 -0
- package/dev/dist/index.d.ts +5 -0
- package/dev/dist/index.js +2614 -1567
- package/dev/dist/index.mjs +2037 -990
- package/dist/apply-auto-instrumentation.js +200 -199
- package/dist/apply-auto-instrumentation.mjs +8 -7
- package/dist/auto-instrumentations/bundler/esbuild.cjs +596 -462
- package/dist/auto-instrumentations/bundler/esbuild.mjs +2 -2
- package/dist/auto-instrumentations/bundler/next.cjs +596 -462
- package/dist/auto-instrumentations/bundler/next.mjs +3 -3
- package/dist/auto-instrumentations/bundler/rollup.cjs +596 -462
- package/dist/auto-instrumentations/bundler/rollup.mjs +2 -2
- package/dist/auto-instrumentations/bundler/vite.cjs +596 -462
- package/dist/auto-instrumentations/bundler/vite.mjs +2 -2
- package/dist/auto-instrumentations/bundler/webpack-loader.cjs +596 -462
- package/dist/auto-instrumentations/bundler/webpack.cjs +596 -462
- package/dist/auto-instrumentations/bundler/webpack.mjs +3 -3
- package/dist/auto-instrumentations/{chunk-BURMPO7L.mjs → chunk-7P6563SW.mjs} +1 -1
- package/dist/auto-instrumentations/{chunk-F43DNLPD.mjs → chunk-CZ24KNHT.mjs} +569 -442
- package/dist/auto-instrumentations/{chunk-GSZHTAQW.mjs → chunk-JPVCUKTY.mjs} +30 -21
- package/dist/auto-instrumentations/hook.mjs +648 -476
- package/dist/auto-instrumentations/index.cjs +568 -442
- package/dist/auto-instrumentations/index.mjs +1 -1
- package/dist/browser.d.mts +7 -0
- package/dist/browser.d.ts +7 -0
- package/dist/browser.js +2022 -971
- package/dist/browser.mjs +2022 -971
- package/dist/{chunk-XE5FS7QY.mjs → chunk-2SANLSWX.mjs} +1375 -597
- package/dist/{chunk-7F6GCRHH.mjs → chunk-ABR2QWDP.mjs} +702 -456
- package/dist/{chunk-SU6EHKJV.js → chunk-GSIDVFE6.js} +2176 -1398
- package/dist/{chunk-7AUY2XWX.js → chunk-P25IOOU4.js} +704 -458
- package/dist/cli.js +2005 -958
- package/dist/edge-light.js +2022 -971
- package/dist/edge-light.mjs +2022 -971
- package/dist/index.d.mts +7 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.js +447 -402
- package/dist/index.mjs +72 -27
- package/dist/instrumentation/index.d.mts +5 -0
- package/dist/instrumentation/index.d.ts +5 -0
- package/dist/instrumentation/index.js +1990 -981
- package/dist/instrumentation/index.mjs +1990 -981
- package/dist/vitest-evals-reporter.js +16 -16
- package/dist/vitest-evals-reporter.mjs +2 -2
- package/dist/workerd.js +2022 -971
- package/dist/workerd.mjs +2022 -971
- package/package.json +2 -2
|
@@ -1,14 +1,17 @@
|
|
|
1
1
|
import {
|
|
2
|
+
INSTRUMENTATION_NAMES,
|
|
2
3
|
aiSDKChannels,
|
|
3
4
|
anthropicChannels,
|
|
4
5
|
bedrockRuntimeChannels,
|
|
5
6
|
claudeAgentSDKChannels,
|
|
6
7
|
cohereChannels,
|
|
7
8
|
cursorSDKChannels,
|
|
9
|
+
detectSpanOriginEnvironment,
|
|
8
10
|
flueChannels,
|
|
9
11
|
genkitChannels,
|
|
10
12
|
genkitCoreChannels,
|
|
11
13
|
getDefaultInstrumentationIntegrations,
|
|
14
|
+
getSpanInstrumentationName,
|
|
12
15
|
gitHubCopilotChannels,
|
|
13
16
|
googleADKChannels,
|
|
14
17
|
googleGenAIChannels,
|
|
@@ -20,6 +23,7 @@ import {
|
|
|
20
23
|
isomorph_default,
|
|
21
24
|
langChainChannels,
|
|
22
25
|
langSmithChannels,
|
|
26
|
+
mergeSpanOriginContext,
|
|
23
27
|
mistralChannels,
|
|
24
28
|
openAIAgentsCoreChannels,
|
|
25
29
|
openAIChannels,
|
|
@@ -31,8 +35,9 @@ import {
|
|
|
31
35
|
readDisabledInstrumentationEnvConfig,
|
|
32
36
|
smithyClientChannels,
|
|
33
37
|
smithyCoreChannels,
|
|
34
|
-
strandsAgentSDKChannels
|
|
35
|
-
|
|
38
|
+
strandsAgentSDKChannels,
|
|
39
|
+
withSpanInstrumentationName
|
|
40
|
+
} from "./chunk-ABR2QWDP.mjs";
|
|
36
41
|
|
|
37
42
|
// src/id-gen.ts
|
|
38
43
|
import { v4 as uuidv4 } from "uuid";
|
|
@@ -4753,97 +4758,6 @@ var SpanCache = class {
|
|
|
4753
4758
|
}
|
|
4754
4759
|
};
|
|
4755
4760
|
|
|
4756
|
-
// src/span-origin.ts
|
|
4757
|
-
var SDK_VERSION = true ? "3.24.0" : "0.0.0";
|
|
4758
|
-
function detectSpanOriginEnvironment(explicit) {
|
|
4759
|
-
if (explicit) return explicit;
|
|
4760
|
-
const envType = isomorph_default.getEnv("BRAINTRUST_ENVIRONMENT_TYPE");
|
|
4761
|
-
const envName = isomorph_default.getEnv("BRAINTRUST_ENVIRONMENT_NAME");
|
|
4762
|
-
if (envType || envName) {
|
|
4763
|
-
return {
|
|
4764
|
-
...envType ? { type: envType } : {},
|
|
4765
|
-
...envName ? { name: envName } : {}
|
|
4766
|
-
};
|
|
4767
|
-
}
|
|
4768
|
-
const ci = firstPresent([
|
|
4769
|
-
["GITHUB_ACTIONS", "github_actions"],
|
|
4770
|
-
["GITLAB_CI", "gitlab_ci"],
|
|
4771
|
-
["CIRCLECI", "circleci"],
|
|
4772
|
-
["BUILDKITE", "buildkite"],
|
|
4773
|
-
["JENKINS_URL", "jenkins"],
|
|
4774
|
-
["JENKINS_HOME", "jenkins"],
|
|
4775
|
-
["TF_BUILD", "azure_pipelines"],
|
|
4776
|
-
["TEAMCITY_VERSION", "teamcity"],
|
|
4777
|
-
["TRAVIS", "travis"],
|
|
4778
|
-
["BITBUCKET_BUILD_NUMBER", "bitbucket"]
|
|
4779
|
-
]);
|
|
4780
|
-
if (ci) return { type: "ci", name: ci };
|
|
4781
|
-
if (isomorph_default.getEnv("CI")) return { type: "ci", name: "ci" };
|
|
4782
|
-
const earlyServer = firstPresent([
|
|
4783
|
-
["VERCEL", "vercel"],
|
|
4784
|
-
["NETLIFY", "netlify"]
|
|
4785
|
-
]);
|
|
4786
|
-
if (earlyServer) return { type: "server", name: earlyServer };
|
|
4787
|
-
if (isomorph_default.getEnv("ECS_CONTAINER_METADATA_URI") || isomorph_default.getEnv("ECS_CONTAINER_METADATA_URI_V4")) {
|
|
4788
|
-
return { type: "server", name: "ecs" };
|
|
4789
|
-
}
|
|
4790
|
-
const awsExecutionEnv = isomorph_default.getEnv("AWS_EXECUTION_ENV");
|
|
4791
|
-
if (awsExecutionEnv?.startsWith("AWS_ECS_")) {
|
|
4792
|
-
return { type: "server", name: "ecs" };
|
|
4793
|
-
}
|
|
4794
|
-
if (awsExecutionEnv?.startsWith("AWS_Lambda_")) {
|
|
4795
|
-
return { type: "server", name: "aws_lambda" };
|
|
4796
|
-
}
|
|
4797
|
-
if (isomorph_default.getEnv("AWS_LAMBDA_FUNCTION_NAME")) {
|
|
4798
|
-
return { type: "server", name: "aws_lambda" };
|
|
4799
|
-
}
|
|
4800
|
-
const server = firstPresent([
|
|
4801
|
-
["K_SERVICE", "cloud_run"],
|
|
4802
|
-
["FUNCTION_TARGET", "gcp_functions"],
|
|
4803
|
-
["KUBERNETES_SERVICE_HOST", "kubernetes"],
|
|
4804
|
-
["DYNO", "heroku"],
|
|
4805
|
-
["FLY_APP_NAME", "fly"],
|
|
4806
|
-
["RAILWAY_ENVIRONMENT", "railway"],
|
|
4807
|
-
["RENDER_SERVICE_NAME", "render"]
|
|
4808
|
-
]);
|
|
4809
|
-
if (server) return { type: "server", name: server };
|
|
4810
|
-
return deploymentModeEnvironment("NODE_ENV", isomorph_default.getEnv("NODE_ENV"));
|
|
4811
|
-
}
|
|
4812
|
-
function makeSpanOrigin(instrumentationName, environment) {
|
|
4813
|
-
return {
|
|
4814
|
-
name: "braintrust.sdk.javascript",
|
|
4815
|
-
version: SDK_VERSION,
|
|
4816
|
-
instrumentation: { name: instrumentationName },
|
|
4817
|
-
...environment ? { environment } : {}
|
|
4818
|
-
};
|
|
4819
|
-
}
|
|
4820
|
-
function mergeSpanOriginContext(context, instrumentationName, environment) {
|
|
4821
|
-
const next = { ...context ?? {} };
|
|
4822
|
-
const current = isObject2(next.span_origin) ? { ...next.span_origin } : {};
|
|
4823
|
-
next.span_origin = {
|
|
4824
|
-
...makeSpanOrigin(instrumentationName, environment),
|
|
4825
|
-
...current
|
|
4826
|
-
};
|
|
4827
|
-
return next;
|
|
4828
|
-
}
|
|
4829
|
-
function firstPresent(entries) {
|
|
4830
|
-
return entries.find(([key]) => Boolean(isomorph_default.getEnv(key)))?.[1];
|
|
4831
|
-
}
|
|
4832
|
-
function deploymentModeEnvironment(_key, value) {
|
|
4833
|
-
if (!value) return void 0;
|
|
4834
|
-
const normalized = value.toLowerCase();
|
|
4835
|
-
if (normalized === "production" || normalized === "staging") {
|
|
4836
|
-
return { type: "server", name: normalized };
|
|
4837
|
-
}
|
|
4838
|
-
if (normalized === "development" || normalized === "local") {
|
|
4839
|
-
return { type: "local", name: normalized };
|
|
4840
|
-
}
|
|
4841
|
-
return { name: value };
|
|
4842
|
-
}
|
|
4843
|
-
function isObject2(value) {
|
|
4844
|
-
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
4845
|
-
}
|
|
4846
|
-
|
|
4847
4761
|
// src/logger.ts
|
|
4848
4762
|
var BRAINTRUST_ATTACHMENT = BraintrustAttachmentReference.shape.type.value;
|
|
4849
4763
|
var EXTERNAL_ATTACHMENT = ExternalAttachmentReference.shape.type.value;
|
|
@@ -5120,6 +5034,7 @@ var BraintrustState = class _BraintrustState {
|
|
|
5120
5034
|
});
|
|
5121
5035
|
this.spanCache = new SpanCache({ disabled: loginParams.disableSpanCache });
|
|
5122
5036
|
this.spanOriginEnvironment = detectSpanOriginEnvironment();
|
|
5037
|
+
this._internalSetTraceContextSigningSecret(loginParams.apiKey);
|
|
5123
5038
|
}
|
|
5124
5039
|
loginParams;
|
|
5125
5040
|
id;
|
|
@@ -5157,6 +5072,18 @@ var BraintrustState = class _BraintrustState {
|
|
|
5157
5072
|
_contextManager = null;
|
|
5158
5073
|
_otelFlushCallback = null;
|
|
5159
5074
|
spanOriginEnvironment;
|
|
5075
|
+
traceContextSigningSecret;
|
|
5076
|
+
/** @internal */
|
|
5077
|
+
_internalSetTraceContextSigningSecret(secret) {
|
|
5078
|
+
const normalizedSecret = secret?.trim();
|
|
5079
|
+
if (normalizedSecret) {
|
|
5080
|
+
this.traceContextSigningSecret = normalizedSecret;
|
|
5081
|
+
}
|
|
5082
|
+
}
|
|
5083
|
+
/** @internal */
|
|
5084
|
+
_internalGetTraceContextSigningSecret() {
|
|
5085
|
+
return (this.traceContextSigningSecret ?? this.loginToken ?? isomorph_default.getEnv("BRAINTRUST_API_KEY"))?.trim();
|
|
5086
|
+
}
|
|
5160
5087
|
resetLoginInfo() {
|
|
5161
5088
|
this.appUrl = null;
|
|
5162
5089
|
this.appPublicUrl = null;
|
|
@@ -5217,6 +5144,7 @@ var BraintrustState = class _BraintrustState {
|
|
|
5217
5144
|
this.gitMetadataSettings = other.gitMetadataSettings;
|
|
5218
5145
|
this.debugLogLevel = other.debugLogLevel;
|
|
5219
5146
|
this.debugLogLevelConfigured = other.debugLogLevelConfigured;
|
|
5147
|
+
this.traceContextSigningSecret = other.traceContextSigningSecret;
|
|
5220
5148
|
setGlobalDebugLogLevel(
|
|
5221
5149
|
this.debugLogLevelConfigured ? this.debugLogLevel ?? false : void 0
|
|
5222
5150
|
);
|
|
@@ -5304,6 +5232,7 @@ var BraintrustState = class _BraintrustState {
|
|
|
5304
5232
|
return this.debugLogLevelConfigured;
|
|
5305
5233
|
}
|
|
5306
5234
|
async login(loginParams) {
|
|
5235
|
+
this._internalSetTraceContextSigningSecret(loginParams.apiKey);
|
|
5307
5236
|
this.setDebugLogLevel(loginParams.debugLogLevel);
|
|
5308
5237
|
if (this.apiUrl && !loginParams.forceLogin) {
|
|
5309
5238
|
return;
|
|
@@ -7283,6 +7212,7 @@ function init(projectOrOptions, optionalOptions) {
|
|
|
7283
7212
|
throw new Error("Cannot open and update an experiment at the same time");
|
|
7284
7213
|
}
|
|
7285
7214
|
const state = stateArg ?? _globalState;
|
|
7215
|
+
state._internalSetTraceContextSigningSecret(apiKey);
|
|
7286
7216
|
state.enforceQueueSizeLimit(false);
|
|
7287
7217
|
if (open2) {
|
|
7288
7218
|
if (isEmpty2(experiment)) {
|
|
@@ -7791,6 +7721,7 @@ function initLogger(options = {}) {
|
|
|
7791
7721
|
project_id: projectId
|
|
7792
7722
|
};
|
|
7793
7723
|
const state = stateArg ?? _globalState;
|
|
7724
|
+
state._internalSetTraceContextSigningSecret(apiKey);
|
|
7794
7725
|
state.setDebugLogLevel(debugLogLevel);
|
|
7795
7726
|
state.spanOriginEnvironment = detectSpanOriginEnvironment(environment);
|
|
7796
7727
|
state.enforceQueueSizeLimit(true);
|
|
@@ -8854,7 +8785,9 @@ function deepCopyEvent(event) {
|
|
|
8854
8785
|
const ATTACHMENT_MARKER_KEY = "_bt_internal_saved_attachment_marker";
|
|
8855
8786
|
const attachmentMarker = ++deepCopyEventMarkerCounter;
|
|
8856
8787
|
const serialized = JSON.stringify(event, (_k, v) => {
|
|
8857
|
-
if (v instanceof
|
|
8788
|
+
if (v instanceof Error) {
|
|
8789
|
+
return v.message;
|
|
8790
|
+
} else if (v instanceof SpanImpl || v instanceof NoopSpan) {
|
|
8858
8791
|
return `<span>`;
|
|
8859
8792
|
} else if (v instanceof Experiment2) {
|
|
8860
8793
|
return `<experiment>`;
|
|
@@ -9522,6 +9455,7 @@ var SpanImpl = class _SpanImpl {
|
|
|
9522
9455
|
constructor(args) {
|
|
9523
9456
|
this._state = args.state;
|
|
9524
9457
|
this._propagatedState = args.propagatedState;
|
|
9458
|
+
const instrumentationName = getSpanInstrumentationName(args) ?? INSTRUMENTATION_NAMES.BRAINTRUST_JS_LOGGER;
|
|
9525
9459
|
const spanAttributes = args.spanAttributes ?? {};
|
|
9526
9460
|
const rawEvent = args.event ?? {};
|
|
9527
9461
|
const type = args.type ?? (args.parentSpanIds ? void 0 : args.defaultRootType);
|
|
@@ -9553,7 +9487,7 @@ var SpanImpl = class _SpanImpl {
|
|
|
9553
9487
|
},
|
|
9554
9488
|
context: mergeSpanOriginContext(
|
|
9555
9489
|
{ ...callerLocation },
|
|
9556
|
-
|
|
9490
|
+
instrumentationName,
|
|
9557
9491
|
this._state.spanOriginEnvironment
|
|
9558
9492
|
),
|
|
9559
9493
|
span_attributes: {
|
|
@@ -11592,15 +11526,25 @@ function hasChoices(value) {
|
|
|
11592
11526
|
function normalizeMetadata(metadata) {
|
|
11593
11527
|
return isObject(metadata) ? metadata : void 0;
|
|
11594
11528
|
}
|
|
11595
|
-
function startSpanForEvent(config, event, channelName) {
|
|
11529
|
+
function startSpanForEvent(config, event, channelName, instrumentationName) {
|
|
11596
11530
|
const { name, spanAttributes, spanInfoMetadata } = buildStartSpanArgs(
|
|
11597
11531
|
config,
|
|
11598
11532
|
event
|
|
11599
11533
|
);
|
|
11600
|
-
const
|
|
11601
|
-
|
|
11602
|
-
|
|
11603
|
-
|
|
11534
|
+
const spanArgs = withSpanInstrumentationName(
|
|
11535
|
+
{
|
|
11536
|
+
name,
|
|
11537
|
+
spanAttributes
|
|
11538
|
+
},
|
|
11539
|
+
instrumentationName
|
|
11540
|
+
);
|
|
11541
|
+
let span;
|
|
11542
|
+
try {
|
|
11543
|
+
span = config.startSpan?.(spanArgs) ?? startSpan(spanArgs);
|
|
11544
|
+
} catch (error) {
|
|
11545
|
+
debugLogger.error(`Error starting span for ${channelName}:`, error);
|
|
11546
|
+
span = startSpan(spanArgs);
|
|
11547
|
+
}
|
|
11604
11548
|
const startTime = getCurrentUnixTimestamp();
|
|
11605
11549
|
try {
|
|
11606
11550
|
const { input, metadata } = config.extractInput(
|
|
@@ -11631,7 +11575,7 @@ function shouldTraceEvent(config, event, channelName) {
|
|
|
11631
11575
|
return true;
|
|
11632
11576
|
}
|
|
11633
11577
|
}
|
|
11634
|
-
function ensureSpanStateForEvent(states, config, event, channelName) {
|
|
11578
|
+
function ensureSpanStateForEvent(states, config, event, channelName, instrumentationName) {
|
|
11635
11579
|
const key = event;
|
|
11636
11580
|
const existing = states.get(key);
|
|
11637
11581
|
if (existing) {
|
|
@@ -11640,11 +11584,16 @@ function ensureSpanStateForEvent(states, config, event, channelName) {
|
|
|
11640
11584
|
if (!shouldTraceEvent(config, event, channelName)) {
|
|
11641
11585
|
return void 0;
|
|
11642
11586
|
}
|
|
11643
|
-
const created = startSpanForEvent(
|
|
11587
|
+
const created = startSpanForEvent(
|
|
11588
|
+
config,
|
|
11589
|
+
event,
|
|
11590
|
+
channelName,
|
|
11591
|
+
instrumentationName
|
|
11592
|
+
);
|
|
11644
11593
|
states.set(key, created);
|
|
11645
11594
|
return created;
|
|
11646
11595
|
}
|
|
11647
|
-
function bindCurrentSpanStoreToStart(tracingChannel2, states, config, channelName) {
|
|
11596
|
+
function bindCurrentSpanStoreToStart(tracingChannel2, states, config, channelName, instrumentationName) {
|
|
11648
11597
|
const state = _internalGetGlobalState();
|
|
11649
11598
|
const startChannel = tracingChannel2.start;
|
|
11650
11599
|
const contextManager = state?.contextManager;
|
|
@@ -11662,7 +11611,8 @@ function bindCurrentSpanStoreToStart(tracingChannel2, states, config, channelNam
|
|
|
11662
11611
|
states,
|
|
11663
11612
|
config,
|
|
11664
11613
|
event,
|
|
11665
|
-
channelName
|
|
11614
|
+
channelName,
|
|
11615
|
+
instrumentationName
|
|
11666
11616
|
);
|
|
11667
11617
|
return spanState ? contextManager.wrapSpanForStore(spanState.span) : currentSpanStore.getStore();
|
|
11668
11618
|
}
|
|
@@ -11671,15 +11621,21 @@ function bindCurrentSpanStoreToStart(tracingChannel2, states, config, channelNam
|
|
|
11671
11621
|
startChannel.unbindStore(currentSpanStore);
|
|
11672
11622
|
};
|
|
11673
11623
|
}
|
|
11674
|
-
function logErrorAndEnd(states, event) {
|
|
11624
|
+
function logErrorAndEnd(states, event, channelName) {
|
|
11675
11625
|
const spanData = states.get(event);
|
|
11676
11626
|
if (!spanData) {
|
|
11677
11627
|
return;
|
|
11678
11628
|
}
|
|
11679
|
-
|
|
11680
|
-
error: event.error
|
|
11681
|
-
})
|
|
11682
|
-
|
|
11629
|
+
try {
|
|
11630
|
+
spanData.span.log({ error: event.error });
|
|
11631
|
+
} catch (error) {
|
|
11632
|
+
debugLogger.error(`Error logging failure for ${channelName}:`, error);
|
|
11633
|
+
}
|
|
11634
|
+
try {
|
|
11635
|
+
spanData.span.end();
|
|
11636
|
+
} catch (error) {
|
|
11637
|
+
debugLogger.error(`Error ending span for ${channelName}:`, error);
|
|
11638
|
+
}
|
|
11683
11639
|
states.delete(event);
|
|
11684
11640
|
}
|
|
11685
11641
|
function runStreamingCompletionHook(args) {
|
|
@@ -11729,7 +11685,8 @@ function traceAsyncChannel(channel, config) {
|
|
|
11729
11685
|
tracingChannel2,
|
|
11730
11686
|
states,
|
|
11731
11687
|
config,
|
|
11732
|
-
channelName
|
|
11688
|
+
channelName,
|
|
11689
|
+
channel.instrumentationName
|
|
11733
11690
|
);
|
|
11734
11691
|
const handlers = {
|
|
11735
11692
|
start: (event) => {
|
|
@@ -11740,7 +11697,8 @@ function traceAsyncChannel(channel, config) {
|
|
|
11740
11697
|
states,
|
|
11741
11698
|
config,
|
|
11742
11699
|
event,
|
|
11743
|
-
channelName
|
|
11700
|
+
channelName,
|
|
11701
|
+
channel.instrumentationName
|
|
11744
11702
|
);
|
|
11745
11703
|
},
|
|
11746
11704
|
asyncEnd: (event) => {
|
|
@@ -11777,7 +11735,7 @@ function traceAsyncChannel(channel, config) {
|
|
|
11777
11735
|
}
|
|
11778
11736
|
},
|
|
11779
11737
|
error: (event) => {
|
|
11780
|
-
logErrorAndEnd(states, event);
|
|
11738
|
+
logErrorAndEnd(states, event, channelName);
|
|
11781
11739
|
}
|
|
11782
11740
|
};
|
|
11783
11741
|
tracingChannel2.subscribe(handlers);
|
|
@@ -11794,7 +11752,8 @@ function traceStreamingChannel(channel, config) {
|
|
|
11794
11752
|
tracingChannel2,
|
|
11795
11753
|
states,
|
|
11796
11754
|
config,
|
|
11797
|
-
channelName
|
|
11755
|
+
channelName,
|
|
11756
|
+
channel.instrumentationName
|
|
11798
11757
|
);
|
|
11799
11758
|
const handlers = {
|
|
11800
11759
|
start: (event) => {
|
|
@@ -11805,7 +11764,8 @@ function traceStreamingChannel(channel, config) {
|
|
|
11805
11764
|
states,
|
|
11806
11765
|
config,
|
|
11807
11766
|
event,
|
|
11808
|
-
channelName
|
|
11767
|
+
channelName,
|
|
11768
|
+
channel.instrumentationName
|
|
11809
11769
|
);
|
|
11810
11770
|
},
|
|
11811
11771
|
asyncEnd: (event) => {
|
|
@@ -11824,6 +11784,7 @@ function traceStreamingChannel(channel, config) {
|
|
|
11824
11784
|
}
|
|
11825
11785
|
},
|
|
11826
11786
|
onComplete: (chunks) => {
|
|
11787
|
+
let completion2;
|
|
11827
11788
|
try {
|
|
11828
11789
|
let output;
|
|
11829
11790
|
let metrics;
|
|
@@ -11854,18 +11815,11 @@ function traceStreamingChannel(channel, config) {
|
|
|
11854
11815
|
} else if (metrics.time_to_first_token === void 0 && chunks.length > 0) {
|
|
11855
11816
|
metrics.time_to_first_token = getCurrentUnixTimestamp() - startTime;
|
|
11856
11817
|
}
|
|
11857
|
-
|
|
11858
|
-
channelName,
|
|
11859
|
-
chunks,
|
|
11860
|
-
config,
|
|
11861
|
-
endEvent: asyncEndEvent,
|
|
11818
|
+
completion2 = {
|
|
11862
11819
|
...metadata !== void 0 ? { metadata } : {},
|
|
11863
11820
|
metrics,
|
|
11864
|
-
output
|
|
11865
|
-
|
|
11866
|
-
span,
|
|
11867
|
-
startTime
|
|
11868
|
-
});
|
|
11821
|
+
output
|
|
11822
|
+
};
|
|
11869
11823
|
span.log({
|
|
11870
11824
|
output,
|
|
11871
11825
|
...metadata !== void 0 ? { metadata } : {},
|
|
@@ -11877,11 +11831,49 @@ function traceStreamingChannel(channel, config) {
|
|
|
11877
11831
|
error
|
|
11878
11832
|
);
|
|
11879
11833
|
} finally {
|
|
11880
|
-
|
|
11834
|
+
try {
|
|
11835
|
+
span.end();
|
|
11836
|
+
} catch (error) {
|
|
11837
|
+
debugLogger.error(
|
|
11838
|
+
`Error ending span for ${channelName}:`,
|
|
11839
|
+
error
|
|
11840
|
+
);
|
|
11841
|
+
}
|
|
11881
11842
|
states.delete(event);
|
|
11882
11843
|
}
|
|
11844
|
+
if (completion2) {
|
|
11845
|
+
runStreamingCompletionHook({
|
|
11846
|
+
channelName,
|
|
11847
|
+
chunks,
|
|
11848
|
+
config,
|
|
11849
|
+
endEvent: asyncEndEvent,
|
|
11850
|
+
...completion2.metadata !== void 0 ? { metadata: completion2.metadata } : {},
|
|
11851
|
+
metrics: completion2.metrics,
|
|
11852
|
+
output: completion2.output,
|
|
11853
|
+
result: asyncEndEvent.result,
|
|
11854
|
+
span,
|
|
11855
|
+
startTime
|
|
11856
|
+
});
|
|
11857
|
+
}
|
|
11883
11858
|
},
|
|
11884
11859
|
onError: (error) => {
|
|
11860
|
+
try {
|
|
11861
|
+
span.log({ error });
|
|
11862
|
+
} catch (loggingError) {
|
|
11863
|
+
debugLogger.error(
|
|
11864
|
+
`Error logging failure for ${channelName}:`,
|
|
11865
|
+
loggingError
|
|
11866
|
+
);
|
|
11867
|
+
}
|
|
11868
|
+
try {
|
|
11869
|
+
span.end();
|
|
11870
|
+
} catch (endingError) {
|
|
11871
|
+
debugLogger.error(
|
|
11872
|
+
`Error ending span for ${channelName}:`,
|
|
11873
|
+
endingError
|
|
11874
|
+
);
|
|
11875
|
+
}
|
|
11876
|
+
states.delete(event);
|
|
11885
11877
|
runStreamingErrorHook({
|
|
11886
11878
|
channelName,
|
|
11887
11879
|
config,
|
|
@@ -11890,11 +11882,6 @@ function traceStreamingChannel(channel, config) {
|
|
|
11890
11882
|
span,
|
|
11891
11883
|
startTime
|
|
11892
11884
|
});
|
|
11893
|
-
span.log({
|
|
11894
|
-
error: error.message
|
|
11895
|
-
});
|
|
11896
|
-
span.end();
|
|
11897
|
-
states.delete(event);
|
|
11898
11885
|
}
|
|
11899
11886
|
});
|
|
11900
11887
|
return;
|
|
@@ -11909,6 +11896,7 @@ function traceStreamingChannel(channel, config) {
|
|
|
11909
11896
|
states.delete(event);
|
|
11910
11897
|
return;
|
|
11911
11898
|
}
|
|
11899
|
+
let completion;
|
|
11912
11900
|
try {
|
|
11913
11901
|
const output = config.extractOutput(
|
|
11914
11902
|
asyncEndEvent.result,
|
|
@@ -11923,17 +11911,11 @@ function traceStreamingChannel(channel, config) {
|
|
|
11923
11911
|
asyncEndEvent.result,
|
|
11924
11912
|
asyncEndEvent
|
|
11925
11913
|
);
|
|
11926
|
-
|
|
11927
|
-
channelName,
|
|
11928
|
-
config,
|
|
11929
|
-
endEvent: asyncEndEvent,
|
|
11914
|
+
completion = {
|
|
11930
11915
|
...normalizeMetadata(metadata) !== void 0 ? { metadata: normalizeMetadata(metadata) } : {},
|
|
11931
11916
|
metrics,
|
|
11932
|
-
output
|
|
11933
|
-
|
|
11934
|
-
span,
|
|
11935
|
-
startTime
|
|
11936
|
-
});
|
|
11917
|
+
output
|
|
11918
|
+
};
|
|
11937
11919
|
span.log({
|
|
11938
11920
|
output,
|
|
11939
11921
|
...normalizeMetadata(metadata) !== void 0 ? { metadata: normalizeMetadata(metadata) } : {},
|
|
@@ -11942,12 +11924,30 @@ function traceStreamingChannel(channel, config) {
|
|
|
11942
11924
|
} catch (error) {
|
|
11943
11925
|
debugLogger.error(`Error extracting output for ${channelName}:`, error);
|
|
11944
11926
|
} finally {
|
|
11945
|
-
|
|
11927
|
+
try {
|
|
11928
|
+
span.end();
|
|
11929
|
+
} catch (error) {
|
|
11930
|
+
debugLogger.error(`Error ending span for ${channelName}:`, error);
|
|
11931
|
+
}
|
|
11946
11932
|
states.delete(event);
|
|
11947
11933
|
}
|
|
11934
|
+
if (completion) {
|
|
11935
|
+
runStreamingCompletionHook({
|
|
11936
|
+
channelName,
|
|
11937
|
+
config,
|
|
11938
|
+
endEvent: asyncEndEvent,
|
|
11939
|
+
...completion.metadata !== void 0 ? { metadata: completion.metadata } : {},
|
|
11940
|
+
metrics: completion.metrics,
|
|
11941
|
+
output: completion.output,
|
|
11942
|
+
result: asyncEndEvent.result,
|
|
11943
|
+
span,
|
|
11944
|
+
startTime
|
|
11945
|
+
});
|
|
11946
|
+
}
|
|
11948
11947
|
},
|
|
11949
11948
|
error: (event) => {
|
|
11950
11949
|
const spanData = states.get(event);
|
|
11950
|
+
logErrorAndEnd(states, event, channelName);
|
|
11951
11951
|
if (spanData) {
|
|
11952
11952
|
runStreamingErrorHook({
|
|
11953
11953
|
channelName,
|
|
@@ -11958,7 +11958,6 @@ function traceStreamingChannel(channel, config) {
|
|
|
11958
11958
|
startTime: spanData.startTime
|
|
11959
11959
|
});
|
|
11960
11960
|
}
|
|
11961
|
-
logErrorAndEnd(states, event);
|
|
11962
11961
|
}
|
|
11963
11962
|
};
|
|
11964
11963
|
tracingChannel2.subscribe(handlers);
|
|
@@ -11975,7 +11974,8 @@ function traceSyncStreamChannel(channel, config) {
|
|
|
11975
11974
|
tracingChannel2,
|
|
11976
11975
|
states,
|
|
11977
11976
|
config,
|
|
11978
|
-
channelName
|
|
11977
|
+
channelName,
|
|
11978
|
+
channel.instrumentationName
|
|
11979
11979
|
);
|
|
11980
11980
|
const handlers = {
|
|
11981
11981
|
start: (event) => {
|
|
@@ -11986,7 +11986,8 @@ function traceSyncStreamChannel(channel, config) {
|
|
|
11986
11986
|
states,
|
|
11987
11987
|
config,
|
|
11988
11988
|
event,
|
|
11989
|
-
channelName
|
|
11989
|
+
channelName,
|
|
11990
|
+
channel.instrumentationName
|
|
11990
11991
|
);
|
|
11991
11992
|
},
|
|
11992
11993
|
end: (event) => {
|
|
@@ -12080,7 +12081,7 @@ function traceSyncStreamChannel(channel, config) {
|
|
|
12080
12081
|
handleResolvedResult(endEvent.result);
|
|
12081
12082
|
},
|
|
12082
12083
|
error: (event) => {
|
|
12083
|
-
logErrorAndEnd(states, event);
|
|
12084
|
+
logErrorAndEnd(states, event, channelName);
|
|
12084
12085
|
}
|
|
12085
12086
|
};
|
|
12086
12087
|
tracingChannel2.subscribe(handlers);
|
|
@@ -12357,6 +12358,267 @@ function serializeAISDKToolsForLogging(tools) {
|
|
|
12357
12358
|
return serialized;
|
|
12358
12359
|
}
|
|
12359
12360
|
|
|
12361
|
+
// src/wrappers/ai-sdk/harness-agent-context.ts
|
|
12362
|
+
var BRAINTRUST_TURN_CONTEXT_KEY = "__braintrust_trace_context";
|
|
12363
|
+
var sessionTurnParents = /* @__PURE__ */ new WeakMap();
|
|
12364
|
+
var wrapperTurnParents = /* @__PURE__ */ new WeakMap();
|
|
12365
|
+
var patchedLifecycleMethods = /* @__PURE__ */ new WeakMap();
|
|
12366
|
+
var harnessTurnParentStore;
|
|
12367
|
+
function serializedTurnContext(value) {
|
|
12368
|
+
if (!isObject(value)) {
|
|
12369
|
+
return void 0;
|
|
12370
|
+
}
|
|
12371
|
+
const context = value[BRAINTRUST_TURN_CONTEXT_KEY];
|
|
12372
|
+
if (!isObject(context) || typeof context.parent !== "string" || typeof context.sessionId !== "string" || typeof context.signature !== "string") {
|
|
12373
|
+
return void 0;
|
|
12374
|
+
}
|
|
12375
|
+
const expectedSignature = signTurnContext({
|
|
12376
|
+
parent: context.parent,
|
|
12377
|
+
sessionId: context.sessionId
|
|
12378
|
+
});
|
|
12379
|
+
if (!expectedSignature || !isomorph_default.timingSafeEqual?.(context.signature, expectedSignature)) {
|
|
12380
|
+
return void 0;
|
|
12381
|
+
}
|
|
12382
|
+
const parent = SpanComponentsV4.fromStr(context.parent);
|
|
12383
|
+
if (!parent.data.row_id || !parent.data.root_span_id || !parent.data.span_id) {
|
|
12384
|
+
return void 0;
|
|
12385
|
+
}
|
|
12386
|
+
return {
|
|
12387
|
+
parent: context.parent,
|
|
12388
|
+
sessionId: context.sessionId
|
|
12389
|
+
};
|
|
12390
|
+
}
|
|
12391
|
+
function signTurnContext(context) {
|
|
12392
|
+
const secret = _internalGetGlobalState()._internalGetTraceContextSigningSecret();
|
|
12393
|
+
return secret ? isomorph_default.hmacSha256?.(
|
|
12394
|
+
secret,
|
|
12395
|
+
JSON.stringify([BRAINTRUST_TURN_CONTEXT_KEY, context])
|
|
12396
|
+
) : void 0;
|
|
12397
|
+
}
|
|
12398
|
+
function continuationParentFromCreateSessionParams(params) {
|
|
12399
|
+
if (!isObject(params)) {
|
|
12400
|
+
return void 0;
|
|
12401
|
+
}
|
|
12402
|
+
const continuation = params.continueFrom ?? (isObject(params.resumeFrom) ? params.resumeFrom.continueFrom : void 0);
|
|
12403
|
+
const context = serializedTurnContext(continuation);
|
|
12404
|
+
if (!context) {
|
|
12405
|
+
return void 0;
|
|
12406
|
+
}
|
|
12407
|
+
if (params.sessionId !== context.sessionId) {
|
|
12408
|
+
return void 0;
|
|
12409
|
+
}
|
|
12410
|
+
return context.parent;
|
|
12411
|
+
}
|
|
12412
|
+
function exportSpanSynchronously(span) {
|
|
12413
|
+
const parentInfo = span.getParentInfo();
|
|
12414
|
+
if (!parentInfo) {
|
|
12415
|
+
return void 0;
|
|
12416
|
+
}
|
|
12417
|
+
const objectId = parentInfo.objectId.getSync().value;
|
|
12418
|
+
if (!objectId && !parentInfo.computeObjectMetadataArgs) {
|
|
12419
|
+
return void 0;
|
|
12420
|
+
}
|
|
12421
|
+
return new SpanComponentsV4({
|
|
12422
|
+
object_type: parentInfo.objectType,
|
|
12423
|
+
...objectId ? { object_id: objectId } : {
|
|
12424
|
+
compute_object_metadata_args: parentInfo.computeObjectMetadataArgs ?? {}
|
|
12425
|
+
},
|
|
12426
|
+
row_id: span.id,
|
|
12427
|
+
root_span_id: span.rootSpanId,
|
|
12428
|
+
span_id: span.spanId
|
|
12429
|
+
}).toStr();
|
|
12430
|
+
}
|
|
12431
|
+
function exportedParent(parent) {
|
|
12432
|
+
return typeof parent === "string" ? parent : exportSpanSynchronously(parent);
|
|
12433
|
+
}
|
|
12434
|
+
function addSerializedContext(args) {
|
|
12435
|
+
if (!isObject(args.continuation) || args.sessionId === void 0) {
|
|
12436
|
+
return;
|
|
12437
|
+
}
|
|
12438
|
+
const parent = exportedParent(args.parent);
|
|
12439
|
+
if (!parent) {
|
|
12440
|
+
return;
|
|
12441
|
+
}
|
|
12442
|
+
const context = {
|
|
12443
|
+
parent,
|
|
12444
|
+
sessionId: args.sessionId
|
|
12445
|
+
};
|
|
12446
|
+
const signature = signTurnContext(context);
|
|
12447
|
+
if (!signature) {
|
|
12448
|
+
return;
|
|
12449
|
+
}
|
|
12450
|
+
Object.defineProperty(args.continuation, BRAINTRUST_TURN_CONTEXT_KEY, {
|
|
12451
|
+
configurable: true,
|
|
12452
|
+
enumerable: true,
|
|
12453
|
+
value: {
|
|
12454
|
+
...context,
|
|
12455
|
+
signature
|
|
12456
|
+
},
|
|
12457
|
+
writable: true
|
|
12458
|
+
});
|
|
12459
|
+
}
|
|
12460
|
+
function lifecycleMethodDescriptor(session, method) {
|
|
12461
|
+
let owner = session;
|
|
12462
|
+
while (owner) {
|
|
12463
|
+
const descriptor = Object.getOwnPropertyDescriptor(owner, method);
|
|
12464
|
+
if (descriptor) {
|
|
12465
|
+
return { descriptor, owner };
|
|
12466
|
+
}
|
|
12467
|
+
owner = Object.getPrototypeOf(owner);
|
|
12468
|
+
}
|
|
12469
|
+
return void 0;
|
|
12470
|
+
}
|
|
12471
|
+
function patchLifecycleMethod(session, method) {
|
|
12472
|
+
let resolvedDescriptor;
|
|
12473
|
+
try {
|
|
12474
|
+
resolvedDescriptor = lifecycleMethodDescriptor(session, method);
|
|
12475
|
+
} catch {
|
|
12476
|
+
return;
|
|
12477
|
+
}
|
|
12478
|
+
if (!resolvedDescriptor || !("value" in resolvedDescriptor.descriptor) || typeof resolvedDescriptor.descriptor.value !== "function") {
|
|
12479
|
+
return;
|
|
12480
|
+
}
|
|
12481
|
+
const { descriptor, owner } = resolvedDescriptor;
|
|
12482
|
+
const patched = patchedLifecycleMethods.get(owner);
|
|
12483
|
+
if (patched?.has(method)) {
|
|
12484
|
+
return;
|
|
12485
|
+
}
|
|
12486
|
+
const original = descriptor.value;
|
|
12487
|
+
const replacement = function(...args) {
|
|
12488
|
+
const result = Reflect.apply(original, this, args);
|
|
12489
|
+
const addContext = (state) => {
|
|
12490
|
+
try {
|
|
12491
|
+
const parent = sessionTurnParents.get(this);
|
|
12492
|
+
if (!parent) {
|
|
12493
|
+
return;
|
|
12494
|
+
}
|
|
12495
|
+
const continuation = method === "suspendTurn" ? state : isObject(state) ? state.continueFrom : void 0;
|
|
12496
|
+
addSerializedContext({
|
|
12497
|
+
continuation,
|
|
12498
|
+
parent,
|
|
12499
|
+
sessionId: typeof this.sessionId === "string" ? this.sessionId : void 0
|
|
12500
|
+
});
|
|
12501
|
+
} catch {
|
|
12502
|
+
}
|
|
12503
|
+
};
|
|
12504
|
+
try {
|
|
12505
|
+
if (isObject(result) && typeof result.then === "function") {
|
|
12506
|
+
void Promise.resolve(result).then(addContext, () => {
|
|
12507
|
+
});
|
|
12508
|
+
} else {
|
|
12509
|
+
addContext(result);
|
|
12510
|
+
}
|
|
12511
|
+
} catch {
|
|
12512
|
+
}
|
|
12513
|
+
return result;
|
|
12514
|
+
};
|
|
12515
|
+
try {
|
|
12516
|
+
Object.defineProperty(owner, method, {
|
|
12517
|
+
...descriptor,
|
|
12518
|
+
value: replacement
|
|
12519
|
+
});
|
|
12520
|
+
const ownerMethods = patched ?? /* @__PURE__ */ new Set();
|
|
12521
|
+
ownerMethods.add(method);
|
|
12522
|
+
if (!patched) {
|
|
12523
|
+
patchedLifecycleMethods.set(owner, ownerMethods);
|
|
12524
|
+
}
|
|
12525
|
+
} catch {
|
|
12526
|
+
}
|
|
12527
|
+
}
|
|
12528
|
+
function patchLifecycleMethods(session) {
|
|
12529
|
+
patchLifecycleMethod(session, "suspendTurn");
|
|
12530
|
+
patchLifecycleMethod(session, "detach");
|
|
12531
|
+
patchLifecycleMethod(session, "stop");
|
|
12532
|
+
}
|
|
12533
|
+
function registerHarnessTurnSpan(args) {
|
|
12534
|
+
if (!isObject(args.session)) {
|
|
12535
|
+
return;
|
|
12536
|
+
}
|
|
12537
|
+
const session = args.session;
|
|
12538
|
+
sessionTurnParents.set(session, args.span);
|
|
12539
|
+
wrapperTurnParents.set(args.span, args.span);
|
|
12540
|
+
patchLifecycleMethods(session);
|
|
12541
|
+
}
|
|
12542
|
+
function harnessContinuationParent(session) {
|
|
12543
|
+
return isObject(session) ? sessionTurnParents.get(session) : void 0;
|
|
12544
|
+
}
|
|
12545
|
+
function captureHarnessCreateSessionParent(params) {
|
|
12546
|
+
try {
|
|
12547
|
+
return continuationParentFromCreateSessionParams(params);
|
|
12548
|
+
} catch {
|
|
12549
|
+
return void 0;
|
|
12550
|
+
}
|
|
12551
|
+
}
|
|
12552
|
+
function registerHarnessSessionParent(session, parent) {
|
|
12553
|
+
if (!parent || !isObject(session)) {
|
|
12554
|
+
return;
|
|
12555
|
+
}
|
|
12556
|
+
const harnessSession = session;
|
|
12557
|
+
sessionTurnParents.set(harnessSession, parent);
|
|
12558
|
+
patchLifecycleMethods(harnessSession);
|
|
12559
|
+
}
|
|
12560
|
+
function currentHarnessTurnParent() {
|
|
12561
|
+
return harnessTurnParentStore?.getStore() ?? wrapperTurnParents.get(currentSpan());
|
|
12562
|
+
}
|
|
12563
|
+
function bindHarnessTurnParentToStart(tracingChannel2, parentFromEvent) {
|
|
12564
|
+
const startChannel = tracingChannel2.start;
|
|
12565
|
+
if (!startChannel) {
|
|
12566
|
+
return () => {
|
|
12567
|
+
};
|
|
12568
|
+
}
|
|
12569
|
+
harnessTurnParentStore ??= isomorph_default.newAsyncLocalStorage();
|
|
12570
|
+
const store = harnessTurnParentStore;
|
|
12571
|
+
startChannel.bindStore(
|
|
12572
|
+
store,
|
|
12573
|
+
(event) => parentFromEvent(event) ?? store.getStore()
|
|
12574
|
+
);
|
|
12575
|
+
return () => {
|
|
12576
|
+
startChannel.unbindStore(store);
|
|
12577
|
+
};
|
|
12578
|
+
}
|
|
12579
|
+
function startHarnessTurnChildSpan(parent, args) {
|
|
12580
|
+
const spanArgs = withSpanInstrumentationName(
|
|
12581
|
+
args,
|
|
12582
|
+
INSTRUMENTATION_NAMES.AI_SDK
|
|
12583
|
+
);
|
|
12584
|
+
return typeof parent === "string" ? startSpan({ ...spanArgs, parent }) : parent.startSpan(spanArgs);
|
|
12585
|
+
}
|
|
12586
|
+
function updateHarnessTurn(parent, update = {}) {
|
|
12587
|
+
if (!parent) {
|
|
12588
|
+
return;
|
|
12589
|
+
}
|
|
12590
|
+
const log2 = (event) => {
|
|
12591
|
+
if (typeof parent === "string") {
|
|
12592
|
+
updateSpan({ exported: parent, ...event });
|
|
12593
|
+
} else {
|
|
12594
|
+
parent.log(event);
|
|
12595
|
+
}
|
|
12596
|
+
};
|
|
12597
|
+
try {
|
|
12598
|
+
if (Object.prototype.hasOwnProperty.call(update, "output")) {
|
|
12599
|
+
log2({ output: null });
|
|
12600
|
+
}
|
|
12601
|
+
log2(update);
|
|
12602
|
+
} catch {
|
|
12603
|
+
}
|
|
12604
|
+
}
|
|
12605
|
+
function endHarnessTurn(parent) {
|
|
12606
|
+
const endTime = getCurrentUnixTimestamp();
|
|
12607
|
+
if (!parent) {
|
|
12608
|
+
return endTime;
|
|
12609
|
+
}
|
|
12610
|
+
try {
|
|
12611
|
+
const event = { metrics: { end: endTime } };
|
|
12612
|
+
if (typeof parent === "string") {
|
|
12613
|
+
updateSpan({ exported: parent, ...event });
|
|
12614
|
+
} else {
|
|
12615
|
+
parent.log(event);
|
|
12616
|
+
}
|
|
12617
|
+
} catch {
|
|
12618
|
+
}
|
|
12619
|
+
return endTime;
|
|
12620
|
+
}
|
|
12621
|
+
|
|
12360
12622
|
// src/wrappers/ai-sdk/workflow-agent-context.ts
|
|
12361
12623
|
var workflowAgentWrapperSpans = /* @__PURE__ */ new Map();
|
|
12362
12624
|
function registerWorkflowAgentWrapperSpan(span) {
|
|
@@ -12460,6 +12722,17 @@ var AISDKPlugin = class extends BasePlugin {
|
|
|
12460
12722
|
subscribeToAISDK() {
|
|
12461
12723
|
const denyOutputPaths = this.config.denyOutputPaths || DEFAULT_DENY_OUTPUT_PATHS;
|
|
12462
12724
|
this.unsubscribers.push(subscribeToAISDKV7TelemetryDispatcher());
|
|
12725
|
+
this.unsubscribers.push(subscribeToHarnessAgentCreateSession());
|
|
12726
|
+
this.unsubscribers.push(
|
|
12727
|
+
subscribeToHarnessContinuation(
|
|
12728
|
+
harnessAgentChannels.continueGenerate,
|
|
12729
|
+
denyOutputPaths
|
|
12730
|
+
),
|
|
12731
|
+
subscribeToHarnessContinuation(
|
|
12732
|
+
harnessAgentChannels.continueStream,
|
|
12733
|
+
denyOutputPaths
|
|
12734
|
+
)
|
|
12735
|
+
);
|
|
12463
12736
|
this.unsubscribers.push(
|
|
12464
12737
|
traceStreamingChannel(aiSDKChannels.generateText, {
|
|
12465
12738
|
name: "generateText",
|
|
@@ -12655,8 +12928,9 @@ var AISDKPlugin = class extends BasePlugin {
|
|
|
12655
12928
|
this.unsubscribers.push(
|
|
12656
12929
|
traceStreamingChannel(harnessAgentChannels.generate, {
|
|
12657
12930
|
name: "HarnessAgent.generate",
|
|
12931
|
+
startSpan: _internalStartSpanWithInitialMerge,
|
|
12658
12932
|
type: "task" /* TASK */,
|
|
12659
|
-
extractInput: ([params], event) => prepareAISDKHarnessAgentInput(params, event.self),
|
|
12933
|
+
extractInput: ([params], event, span) => prepareAISDKHarnessAgentInput(params, event.self, span),
|
|
12660
12934
|
extractOutput: (result, endEvent) => processAISDKOutput(
|
|
12661
12935
|
result,
|
|
12662
12936
|
resolveDenyOutputPaths(endEvent, denyOutputPaths)
|
|
@@ -12668,8 +12942,9 @@ var AISDKPlugin = class extends BasePlugin {
|
|
|
12668
12942
|
this.unsubscribers.push(
|
|
12669
12943
|
traceStreamingChannel(harnessAgentChannels.stream, {
|
|
12670
12944
|
name: "HarnessAgent.stream",
|
|
12945
|
+
startSpan: _internalStartSpanWithInitialMerge,
|
|
12671
12946
|
type: "task" /* TASK */,
|
|
12672
|
-
extractInput: ([params], event) => prepareAISDKHarnessAgentInput(params, event.self),
|
|
12947
|
+
extractInput: ([params], event, span) => prepareAISDKHarnessAgentInput(params, event.self, span),
|
|
12673
12948
|
extractOutput: (result, endEvent) => processAISDKOutput(
|
|
12674
12949
|
result,
|
|
12675
12950
|
resolveDenyOutputPaths(endEvent, denyOutputPaths)
|
|
@@ -12694,8 +12969,10 @@ var AISDKPlugin = class extends BasePlugin {
|
|
|
12694
12969
|
this.unsubscribers.push(
|
|
12695
12970
|
traceStreamingChannel(harnessAgentChannels.continueGenerate, {
|
|
12696
12971
|
name: "HarnessAgent.continueGenerate",
|
|
12972
|
+
shouldTrace: (args) => !harnessContinuationParent(harnessSessionFromArguments(args)),
|
|
12973
|
+
startSpan: _internalStartSpanWithInitialMerge,
|
|
12697
12974
|
type: "task" /* TASK */,
|
|
12698
|
-
extractInput: ([params], event) => prepareAISDKHarnessAgentInput(params, event.self),
|
|
12975
|
+
extractInput: ([params], event, span) => prepareAISDKHarnessAgentInput(params, event.self, span),
|
|
12699
12976
|
extractOutput: (result, endEvent) => processAISDKOutput(
|
|
12700
12977
|
result,
|
|
12701
12978
|
resolveDenyOutputPaths(endEvent, denyOutputPaths)
|
|
@@ -12707,8 +12984,10 @@ var AISDKPlugin = class extends BasePlugin {
|
|
|
12707
12984
|
this.unsubscribers.push(
|
|
12708
12985
|
traceStreamingChannel(harnessAgentChannels.continueStream, {
|
|
12709
12986
|
name: "HarnessAgent.continueStream",
|
|
12987
|
+
shouldTrace: (args) => !harnessContinuationParent(harnessSessionFromArguments(args)),
|
|
12988
|
+
startSpan: _internalStartSpanWithInitialMerge,
|
|
12710
12989
|
type: "task" /* TASK */,
|
|
12711
|
-
extractInput: ([params], event) => prepareAISDKHarnessAgentInput(params, event.self),
|
|
12990
|
+
extractInput: ([params], event, span) => prepareAISDKHarnessAgentInput(params, event.self, span),
|
|
12712
12991
|
extractOutput: (result, endEvent) => processAISDKOutput(
|
|
12713
12992
|
result,
|
|
12714
12993
|
resolveDenyOutputPaths(endEvent, denyOutputPaths)
|
|
@@ -12800,6 +13079,8 @@ var AISDKPlugin = class extends BasePlugin {
|
|
|
12800
13079
|
defaultDenyOutputPaths: denyOutputPaths,
|
|
12801
13080
|
endEvent,
|
|
12802
13081
|
onComplete: () => unregisterWorkflowAgentWrapperSpan(span),
|
|
13082
|
+
onCancel: () => unregisterWorkflowAgentWrapperSpan(span),
|
|
13083
|
+
onError: () => unregisterWorkflowAgentWrapperSpan(span),
|
|
12803
13084
|
result,
|
|
12804
13085
|
span,
|
|
12805
13086
|
startTime
|
|
@@ -12808,25 +13089,172 @@ var AISDKPlugin = class extends BasePlugin {
|
|
|
12808
13089
|
);
|
|
12809
13090
|
}
|
|
12810
13091
|
};
|
|
12811
|
-
function
|
|
12812
|
-
const channel =
|
|
12813
|
-
const
|
|
13092
|
+
function subscribeToHarnessAgentCreateSession() {
|
|
13093
|
+
const channel = harnessAgentChannels.createSession.tracingChannel();
|
|
13094
|
+
const parents = /* @__PURE__ */ new WeakMap();
|
|
12814
13095
|
const handlers = {
|
|
12815
|
-
|
|
12816
|
-
const
|
|
12817
|
-
if (
|
|
12818
|
-
|
|
13096
|
+
start: (event) => {
|
|
13097
|
+
const parent = captureHarnessCreateSessionParent(event.arguments?.[0]);
|
|
13098
|
+
if (parent) {
|
|
13099
|
+
parents.set(event, parent);
|
|
12819
13100
|
}
|
|
12820
|
-
|
|
12821
|
-
|
|
12822
|
-
|
|
12823
|
-
|
|
12824
|
-
|
|
13101
|
+
},
|
|
13102
|
+
asyncEnd: (event) => {
|
|
13103
|
+
registerHarnessSessionParent(event.result, parents.get(event));
|
|
13104
|
+
parents.delete(event);
|
|
13105
|
+
},
|
|
13106
|
+
error: (event) => {
|
|
13107
|
+
parents.delete(event);
|
|
12825
13108
|
}
|
|
12826
13109
|
};
|
|
12827
13110
|
channel.subscribe(handlers);
|
|
12828
|
-
return () =>
|
|
12829
|
-
|
|
13111
|
+
return () => channel.unsubscribe(handlers);
|
|
13112
|
+
}
|
|
13113
|
+
function harnessContinuationParentFromEvent(event) {
|
|
13114
|
+
try {
|
|
13115
|
+
return harnessContinuationParent(event.arguments?.[0]?.session);
|
|
13116
|
+
} catch {
|
|
13117
|
+
return void 0;
|
|
13118
|
+
}
|
|
13119
|
+
}
|
|
13120
|
+
function subscribeToHarnessContinuation(continuationChannel, defaultDenyOutputPaths) {
|
|
13121
|
+
const channel = continuationChannel.tracingChannel();
|
|
13122
|
+
const parents = /* @__PURE__ */ new WeakMap();
|
|
13123
|
+
const startTimes = /* @__PURE__ */ new WeakMap();
|
|
13124
|
+
const unbindParentStore = bindHarnessTurnParentToStart(
|
|
13125
|
+
channel,
|
|
13126
|
+
harnessContinuationParentFromEvent
|
|
13127
|
+
);
|
|
13128
|
+
const handlers = {
|
|
13129
|
+
start: (event) => {
|
|
13130
|
+
const parent = harnessContinuationParentFromEvent(event);
|
|
13131
|
+
if (!parent) {
|
|
13132
|
+
return;
|
|
13133
|
+
}
|
|
13134
|
+
parents.set(event, parent);
|
|
13135
|
+
startTimes.set(event, getCurrentUnixTimestamp());
|
|
13136
|
+
try {
|
|
13137
|
+
const params = event.arguments?.[0];
|
|
13138
|
+
if (params) {
|
|
13139
|
+
prepareAISDKHarnessAgentInput(params, event.self);
|
|
13140
|
+
}
|
|
13141
|
+
} catch (error) {
|
|
13142
|
+
debugLogger.error(
|
|
13143
|
+
"Error preparing Harness continuation telemetry:",
|
|
13144
|
+
error
|
|
13145
|
+
);
|
|
13146
|
+
}
|
|
13147
|
+
},
|
|
13148
|
+
asyncEnd: (event) => {
|
|
13149
|
+
const parent = parents.get(event);
|
|
13150
|
+
const startTime = startTimes.get(event) ?? getCurrentUnixTimestamp();
|
|
13151
|
+
parents.delete(event);
|
|
13152
|
+
startTimes.delete(event);
|
|
13153
|
+
if (!parent) {
|
|
13154
|
+
return;
|
|
13155
|
+
}
|
|
13156
|
+
const endEvent = event;
|
|
13157
|
+
const span = {
|
|
13158
|
+
end: () => endHarnessTurn(parent),
|
|
13159
|
+
log: (update) => updateHarnessTurn(
|
|
13160
|
+
parent,
|
|
13161
|
+
Object.prototype.hasOwnProperty.call(update, "error") && !Object.prototype.hasOwnProperty.call(update, "output") ? { ...update, output: null } : update
|
|
13162
|
+
)
|
|
13163
|
+
};
|
|
13164
|
+
try {
|
|
13165
|
+
if (isAsyncIterable(endEvent.result)) {
|
|
13166
|
+
patchStreamIfNeeded(endEvent.result, {
|
|
13167
|
+
onComplete: (chunks) => {
|
|
13168
|
+
try {
|
|
13169
|
+
const { metadata, metrics, output } = aggregateAISDKChunks(
|
|
13170
|
+
chunks,
|
|
13171
|
+
endEvent.result,
|
|
13172
|
+
endEvent
|
|
13173
|
+
);
|
|
13174
|
+
span.log({
|
|
13175
|
+
...metadata ? { metadata } : {},
|
|
13176
|
+
metrics,
|
|
13177
|
+
output
|
|
13178
|
+
});
|
|
13179
|
+
} catch (error) {
|
|
13180
|
+
debugLogger.error(
|
|
13181
|
+
"Error aggregating Harness continuation stream:",
|
|
13182
|
+
error
|
|
13183
|
+
);
|
|
13184
|
+
} finally {
|
|
13185
|
+
span.end();
|
|
13186
|
+
}
|
|
13187
|
+
},
|
|
13188
|
+
onError: (error) => {
|
|
13189
|
+
span.log({ error: toLoggedError(error), output: null });
|
|
13190
|
+
span.end();
|
|
13191
|
+
}
|
|
13192
|
+
});
|
|
13193
|
+
return;
|
|
13194
|
+
}
|
|
13195
|
+
if (patchAISDKStreamingResult({
|
|
13196
|
+
defaultDenyOutputPaths,
|
|
13197
|
+
endEvent,
|
|
13198
|
+
result: endEvent.result,
|
|
13199
|
+
resolvePromiseUsage: true,
|
|
13200
|
+
span,
|
|
13201
|
+
startTime
|
|
13202
|
+
})) {
|
|
13203
|
+
return;
|
|
13204
|
+
}
|
|
13205
|
+
finalizeAISDKChildTracing(endEvent);
|
|
13206
|
+
span.log({
|
|
13207
|
+
metrics: extractTokenMetrics(endEvent.result),
|
|
13208
|
+
output: processAISDKOutput(
|
|
13209
|
+
endEvent.result,
|
|
13210
|
+
resolveDenyOutputPaths(endEvent, defaultDenyOutputPaths)
|
|
13211
|
+
)
|
|
13212
|
+
});
|
|
13213
|
+
span.end();
|
|
13214
|
+
} catch (error) {
|
|
13215
|
+
debugLogger.error("Error tracing Harness continuation:", error);
|
|
13216
|
+
span.end();
|
|
13217
|
+
}
|
|
13218
|
+
},
|
|
13219
|
+
error: (event) => {
|
|
13220
|
+
const parent = parents.get(event);
|
|
13221
|
+
parents.delete(event);
|
|
13222
|
+
startTimes.delete(event);
|
|
13223
|
+
if (!parent) {
|
|
13224
|
+
return;
|
|
13225
|
+
}
|
|
13226
|
+
updateHarnessTurn(parent, {
|
|
13227
|
+
error: toLoggedError(event.error),
|
|
13228
|
+
output: null
|
|
13229
|
+
});
|
|
13230
|
+
endHarnessTurn(parent);
|
|
13231
|
+
}
|
|
13232
|
+
};
|
|
13233
|
+
channel.subscribe(handlers);
|
|
13234
|
+
return () => {
|
|
13235
|
+
unbindParentStore();
|
|
13236
|
+
channel.unsubscribe(handlers);
|
|
13237
|
+
};
|
|
13238
|
+
}
|
|
13239
|
+
function subscribeToAISDKV7TelemetryDispatcher() {
|
|
13240
|
+
const channel = aiSDKChannels.v7CreateTelemetryDispatcher.tracingChannel();
|
|
13241
|
+
const telemetry = braintrustAISDKTelemetry();
|
|
13242
|
+
const handlers = {
|
|
13243
|
+
end: (event) => {
|
|
13244
|
+
const telemetryOptions = event.arguments?.[0]?.telemetry;
|
|
13245
|
+
if (telemetryOptions?.isEnabled === false) {
|
|
13246
|
+
return;
|
|
13247
|
+
}
|
|
13248
|
+
patchAISDKV7TelemetryDispatcher(
|
|
13249
|
+
event.result,
|
|
13250
|
+
telemetry,
|
|
13251
|
+
telemetryOptions
|
|
13252
|
+
);
|
|
13253
|
+
}
|
|
13254
|
+
};
|
|
13255
|
+
channel.subscribe(handlers);
|
|
13256
|
+
return () => {
|
|
13257
|
+
channel.unsubscribe(handlers);
|
|
12830
13258
|
};
|
|
12831
13259
|
}
|
|
12832
13260
|
function patchAISDKV7TelemetryDispatcher(dispatcher, telemetry, telemetryOptions) {
|
|
@@ -13260,7 +13688,13 @@ function prepareAISDKCallInput(params, event, span, defaultDenyOutputPaths, chil
|
|
|
13260
13688
|
metadata
|
|
13261
13689
|
};
|
|
13262
13690
|
}
|
|
13263
|
-
function prepareAISDKHarnessAgentInput(params, self) {
|
|
13691
|
+
function prepareAISDKHarnessAgentInput(params, self, span) {
|
|
13692
|
+
if (span) {
|
|
13693
|
+
registerHarnessTurnSpan({
|
|
13694
|
+
session: params.session,
|
|
13695
|
+
span
|
|
13696
|
+
});
|
|
13697
|
+
}
|
|
13264
13698
|
if (isObject(self)) {
|
|
13265
13699
|
try {
|
|
13266
13700
|
if (isObject(self.settings)) {
|
|
@@ -13299,6 +13733,10 @@ function prepareAISDKHarnessAgentInput(params, self) {
|
|
|
13299
13733
|
metadata
|
|
13300
13734
|
};
|
|
13301
13735
|
}
|
|
13736
|
+
function harnessSessionFromArguments(args) {
|
|
13737
|
+
const params = args[0];
|
|
13738
|
+
return isObject(params) ? params.session : void 0;
|
|
13739
|
+
}
|
|
13302
13740
|
function prepareAISDKWorkflowAgentStreamInput(params, event, span, defaultDenyOutputPaths) {
|
|
13303
13741
|
registerWorkflowAgentWrapperSpan(span);
|
|
13304
13742
|
const { input } = processAISDKWorkflowAgentCallInput(params);
|
|
@@ -13746,19 +14184,22 @@ function prepareAISDKChildTracing(params, self, parentSpan, denyOutputPaths, aiS
|
|
|
13746
14184
|
activeEntry.openSpans.delete(span);
|
|
13747
14185
|
}
|
|
13748
14186
|
},
|
|
13749
|
-
|
|
13750
|
-
|
|
13751
|
-
|
|
13752
|
-
|
|
14187
|
+
withSpanInstrumentationName(
|
|
14188
|
+
{
|
|
14189
|
+
name: "doGenerate",
|
|
14190
|
+
spanAttributes: {
|
|
14191
|
+
type: "llm" /* LLM */
|
|
14192
|
+
},
|
|
14193
|
+
event: buildAISDKModelStartEvent(
|
|
14194
|
+
callOptions,
|
|
14195
|
+
activeEntry.baseMetadata,
|
|
14196
|
+
{
|
|
14197
|
+
workflowAgent: activeEntry.childTracingOptions.workflowAgent
|
|
14198
|
+
}
|
|
14199
|
+
)
|
|
13753
14200
|
},
|
|
13754
|
-
|
|
13755
|
-
|
|
13756
|
-
activeEntry.baseMetadata,
|
|
13757
|
-
{
|
|
13758
|
-
workflowAgent: activeEntry.childTracingOptions.workflowAgent
|
|
13759
|
-
}
|
|
13760
|
-
)
|
|
13761
|
-
}
|
|
14201
|
+
INSTRUMENTATION_NAMES.AI_SDK
|
|
14202
|
+
)
|
|
13762
14203
|
);
|
|
13763
14204
|
};
|
|
13764
14205
|
if (originalDoStream) {
|
|
@@ -13768,19 +14209,24 @@ function prepareAISDKChildTracing(params, self, parentSpan, denyOutputPaths, aiS
|
|
|
13768
14209
|
return Reflect.apply(originalDoStream, resolvedModel, [callOptions]);
|
|
13769
14210
|
}
|
|
13770
14211
|
closeOpenAISDKModelPatchSpans(activeEntry);
|
|
13771
|
-
const span = activeEntry.parentSpan.startSpan(
|
|
13772
|
-
|
|
13773
|
-
spanAttributes: {
|
|
13774
|
-
type: "llm" /* LLM */
|
|
13775
|
-
},
|
|
13776
|
-
event: buildAISDKModelStartEvent(
|
|
13777
|
-
callOptions,
|
|
13778
|
-
activeEntry.baseMetadata,
|
|
14212
|
+
const span = activeEntry.parentSpan.startSpan(
|
|
14213
|
+
withSpanInstrumentationName(
|
|
13779
14214
|
{
|
|
13780
|
-
|
|
13781
|
-
|
|
14215
|
+
name: "doStream",
|
|
14216
|
+
spanAttributes: {
|
|
14217
|
+
type: "llm" /* LLM */
|
|
14218
|
+
},
|
|
14219
|
+
event: buildAISDKModelStartEvent(
|
|
14220
|
+
callOptions,
|
|
14221
|
+
activeEntry.baseMetadata,
|
|
14222
|
+
{
|
|
14223
|
+
workflowAgent: activeEntry.childTracingOptions.workflowAgent
|
|
14224
|
+
}
|
|
14225
|
+
)
|
|
14226
|
+
},
|
|
14227
|
+
INSTRUMENTATION_NAMES.AI_SDK
|
|
13782
14228
|
)
|
|
13783
|
-
|
|
14229
|
+
);
|
|
13784
14230
|
activeEntry.openSpans.add(span);
|
|
13785
14231
|
const streamStartTime = getCurrentUnixTimestamp();
|
|
13786
14232
|
const result = await withCurrent(
|
|
@@ -13952,12 +14398,17 @@ function prepareAISDKChildTracing(params, self, parentSpan, denyOutputPaths, aiS
|
|
|
13952
14398
|
}
|
|
13953
14399
|
if (isAsyncGenerator(result)) {
|
|
13954
14400
|
return (async function* () {
|
|
13955
|
-
const span = activeEntry.parentSpan.startSpan(
|
|
13956
|
-
|
|
13957
|
-
|
|
13958
|
-
|
|
13959
|
-
|
|
13960
|
-
|
|
14401
|
+
const span = activeEntry.parentSpan.startSpan(
|
|
14402
|
+
withSpanInstrumentationName(
|
|
14403
|
+
{
|
|
14404
|
+
name: activeEntry.name,
|
|
14405
|
+
spanAttributes: {
|
|
14406
|
+
type: "tool" /* TOOL */
|
|
14407
|
+
}
|
|
14408
|
+
},
|
|
14409
|
+
INSTRUMENTATION_NAMES.AI_SDK
|
|
14410
|
+
)
|
|
14411
|
+
);
|
|
13961
14412
|
span.log({ input: serializeToolExecutionInput(args) });
|
|
13962
14413
|
try {
|
|
13963
14414
|
let lastValue;
|
|
@@ -13981,12 +14432,15 @@ function prepareAISDKChildTracing(params, self, parentSpan, denyOutputPaths, aiS
|
|
|
13981
14432
|
span.log({ output: awaitedResult });
|
|
13982
14433
|
return awaitedResult;
|
|
13983
14434
|
},
|
|
13984
|
-
|
|
13985
|
-
|
|
13986
|
-
|
|
13987
|
-
|
|
13988
|
-
|
|
13989
|
-
|
|
14435
|
+
withSpanInstrumentationName(
|
|
14436
|
+
{
|
|
14437
|
+
name: activeEntry.name,
|
|
14438
|
+
spanAttributes: {
|
|
14439
|
+
type: "tool" /* TOOL */
|
|
14440
|
+
}
|
|
14441
|
+
},
|
|
14442
|
+
INSTRUMENTATION_NAMES.AI_SDK
|
|
14443
|
+
)
|
|
13990
14444
|
);
|
|
13991
14445
|
};
|
|
13992
14446
|
cleanup.push(() => {
|
|
@@ -14145,6 +14599,8 @@ function patchAISDKStreamingResult(args) {
|
|
|
14145
14599
|
defaultDenyOutputPaths,
|
|
14146
14600
|
endEvent,
|
|
14147
14601
|
onComplete,
|
|
14602
|
+
onCancel,
|
|
14603
|
+
onError,
|
|
14148
14604
|
result,
|
|
14149
14605
|
resolvePromiseUsage,
|
|
14150
14606
|
span,
|
|
@@ -14156,14 +14612,43 @@ function patchAISDKStreamingResult(args) {
|
|
|
14156
14612
|
const resultRecord = result;
|
|
14157
14613
|
attachKnownResultPromiseHandlers(resultRecord);
|
|
14158
14614
|
let finalized = false;
|
|
14159
|
-
const finalize = () => {
|
|
14615
|
+
const finalize = (outcome) => {
|
|
14160
14616
|
if (finalized) {
|
|
14161
14617
|
return;
|
|
14162
14618
|
}
|
|
14163
14619
|
finalized = true;
|
|
14164
|
-
|
|
14165
|
-
|
|
14166
|
-
|
|
14620
|
+
try {
|
|
14621
|
+
finalizeAISDKChildTracing(endEvent);
|
|
14622
|
+
} catch (error) {
|
|
14623
|
+
debugLogger.error("Error finalizing AI SDK child tracing:", error);
|
|
14624
|
+
}
|
|
14625
|
+
try {
|
|
14626
|
+
span.end();
|
|
14627
|
+
} catch (error) {
|
|
14628
|
+
debugLogger.error("Error ending AI SDK streaming span:", error);
|
|
14629
|
+
}
|
|
14630
|
+
if (outcome && "error" in outcome) {
|
|
14631
|
+
try {
|
|
14632
|
+
onError?.(outcome.error);
|
|
14633
|
+
} catch (error) {
|
|
14634
|
+
debugLogger.error("Error in AI SDK streaming error hook:", error);
|
|
14635
|
+
}
|
|
14636
|
+
} else if (outcome) {
|
|
14637
|
+
try {
|
|
14638
|
+
onComplete?.(outcome);
|
|
14639
|
+
} catch (error) {
|
|
14640
|
+
debugLogger.error("Error in AI SDK streaming completion hook:", error);
|
|
14641
|
+
}
|
|
14642
|
+
} else {
|
|
14643
|
+
try {
|
|
14644
|
+
onCancel?.();
|
|
14645
|
+
} catch (error) {
|
|
14646
|
+
debugLogger.error(
|
|
14647
|
+
"Error in AI SDK streaming cancellation hook:",
|
|
14648
|
+
error
|
|
14649
|
+
);
|
|
14650
|
+
}
|
|
14651
|
+
}
|
|
14167
14652
|
};
|
|
14168
14653
|
let outputLogged = false;
|
|
14169
14654
|
const logStreamingOutput = async (firstChunkTime) => {
|
|
@@ -14197,15 +14682,29 @@ function patchAISDKStreamingResult(args) {
|
|
|
14197
14682
|
resolveDenyOutputPaths(endEvent, defaultDenyOutputPaths)
|
|
14198
14683
|
);
|
|
14199
14684
|
const metadata = buildResolvedMetadataPayload(result).metadata;
|
|
14200
|
-
|
|
14201
|
-
|
|
14202
|
-
|
|
14203
|
-
|
|
14204
|
-
|
|
14685
|
+
try {
|
|
14686
|
+
span.log({
|
|
14687
|
+
output,
|
|
14688
|
+
...metadata ? { metadata } : {},
|
|
14689
|
+
metrics
|
|
14690
|
+
});
|
|
14691
|
+
} catch (error) {
|
|
14692
|
+
debugLogger.error("Error logging AI SDK streaming output:", error);
|
|
14693
|
+
}
|
|
14694
|
+
finalize({ metrics, output });
|
|
14205
14695
|
} catch (error) {
|
|
14206
|
-
|
|
14207
|
-
|
|
14208
|
-
|
|
14696
|
+
const loggedError = toLoggedError(error);
|
|
14697
|
+
try {
|
|
14698
|
+
span.log({ error: loggedError });
|
|
14699
|
+
} catch (loggingError) {
|
|
14700
|
+
debugLogger.error(
|
|
14701
|
+
"Error logging AI SDK streaming failure:",
|
|
14702
|
+
loggingError
|
|
14703
|
+
);
|
|
14704
|
+
}
|
|
14705
|
+
finalize({
|
|
14706
|
+
error: error instanceof Error ? error : new Error(String(loggedError))
|
|
14707
|
+
});
|
|
14209
14708
|
}
|
|
14210
14709
|
};
|
|
14211
14710
|
const createAsyncIterableHooks = () => {
|
|
@@ -14222,11 +14721,16 @@ function patchAISDKStreamingResult(args) {
|
|
|
14222
14721
|
onError: (error) => {
|
|
14223
14722
|
if (!outputLogged) {
|
|
14224
14723
|
outputLogged = true;
|
|
14225
|
-
|
|
14226
|
-
error
|
|
14227
|
-
})
|
|
14724
|
+
try {
|
|
14725
|
+
span.log({ error });
|
|
14726
|
+
} catch (loggingError) {
|
|
14727
|
+
debugLogger.error(
|
|
14728
|
+
"Error logging AI SDK stream failure:",
|
|
14729
|
+
loggingError
|
|
14730
|
+
);
|
|
14731
|
+
}
|
|
14228
14732
|
}
|
|
14229
|
-
finalize();
|
|
14733
|
+
finalize({ error });
|
|
14230
14734
|
},
|
|
14231
14735
|
onCancel: finalize
|
|
14232
14736
|
};
|
|
@@ -14312,8 +14816,18 @@ function patchAISDKStreamingResult(args) {
|
|
|
14312
14816
|
}
|
|
14313
14817
|
controller.enqueue(value);
|
|
14314
14818
|
} catch (error) {
|
|
14315
|
-
|
|
14316
|
-
|
|
14819
|
+
const loggedError = toLoggedError(error);
|
|
14820
|
+
try {
|
|
14821
|
+
span.log({ error: loggedError });
|
|
14822
|
+
} catch (loggingError) {
|
|
14823
|
+
debugLogger.error(
|
|
14824
|
+
"Error logging AI SDK base stream failure:",
|
|
14825
|
+
loggingError
|
|
14826
|
+
);
|
|
14827
|
+
}
|
|
14828
|
+
finalize({
|
|
14829
|
+
error: error instanceof Error ? error : new Error(String(loggedError))
|
|
14830
|
+
});
|
|
14317
14831
|
controller.error(error);
|
|
14318
14832
|
}
|
|
14319
14833
|
},
|
|
@@ -15112,14 +15626,17 @@ function braintrustAISDKTelemetry() {
|
|
|
15112
15626
|
console.error(`Error in Braintrust AI SDK telemetry ${name}:`, error);
|
|
15113
15627
|
}
|
|
15114
15628
|
};
|
|
15115
|
-
const startChildSpan = (operationKey, name, type, event) => {
|
|
15629
|
+
const startChildSpan = (operationKey, name, type, event, parentOverride) => {
|
|
15116
15630
|
const parent = operations.get(operationKey)?.span;
|
|
15117
|
-
const spanArgs =
|
|
15118
|
-
|
|
15119
|
-
|
|
15120
|
-
|
|
15121
|
-
|
|
15122
|
-
|
|
15631
|
+
const spanArgs = withSpanInstrumentationName(
|
|
15632
|
+
{
|
|
15633
|
+
name,
|
|
15634
|
+
spanAttributes: { type },
|
|
15635
|
+
...event ? { event } : {}
|
|
15636
|
+
},
|
|
15637
|
+
INSTRUMENTATION_NAMES.AI_SDK
|
|
15638
|
+
);
|
|
15639
|
+
const span = parentOverride ? startHarnessTurnChildSpan(parentOverride, spanArgs) : parent ? parent.startSpan(spanArgs) : startSpan(spanArgs);
|
|
15123
15640
|
const state = operations.get(operationKey);
|
|
15124
15641
|
if (state && type === "llm" /* LLM */) {
|
|
15125
15642
|
state.hadModelChild = true;
|
|
@@ -15399,14 +15916,30 @@ function braintrustAISDKTelemetry() {
|
|
|
15399
15916
|
const workflowAgent = operationName === "WorkflowAgent.stream";
|
|
15400
15917
|
const wrapperSpan = workflowAgent ? currentWorkflowAgentWrapperSpan() : void 0;
|
|
15401
15918
|
const ownsSpan = !wrapperSpan;
|
|
15402
|
-
const
|
|
15403
|
-
|
|
15404
|
-
|
|
15405
|
-
|
|
15919
|
+
const harnessTurnParent = currentHarnessTurnParent();
|
|
15920
|
+
const span = ownsSpan ? harnessTurnParent ? startHarnessTurnChildSpan(
|
|
15921
|
+
harnessTurnParent,
|
|
15922
|
+
withSpanInstrumentationName(
|
|
15923
|
+
{
|
|
15924
|
+
name: operationName,
|
|
15925
|
+
spanAttributes: { type: "function" /* FUNCTION */ }
|
|
15926
|
+
},
|
|
15927
|
+
INSTRUMENTATION_NAMES.AI_SDK
|
|
15928
|
+
)
|
|
15929
|
+
) : startSpan(
|
|
15930
|
+
withSpanInstrumentationName(
|
|
15931
|
+
{
|
|
15932
|
+
name: operationName,
|
|
15933
|
+
spanAttributes: { type: "function" /* FUNCTION */ }
|
|
15934
|
+
},
|
|
15935
|
+
INSTRUMENTATION_NAMES.AI_SDK
|
|
15936
|
+
)
|
|
15937
|
+
) : wrapperSpan;
|
|
15406
15938
|
const operationKey = createOperationKey(event, operationName);
|
|
15407
15939
|
registerOperation({
|
|
15408
15940
|
callId: event.callId,
|
|
15409
15941
|
hadModelChild: false,
|
|
15942
|
+
harnessTurnParent,
|
|
15410
15943
|
loggedInput: false,
|
|
15411
15944
|
operationName,
|
|
15412
15945
|
operationKey,
|
|
@@ -15647,7 +16180,8 @@ function braintrustAISDKTelemetry() {
|
|
|
15647
16180
|
toolCallId,
|
|
15648
16181
|
...typeof event.toolCall.toolName === "string" ? { toolName: event.toolCall.toolName } : {}
|
|
15649
16182
|
}
|
|
15650
|
-
}
|
|
16183
|
+
},
|
|
16184
|
+
state?.harnessTurnParent
|
|
15651
16185
|
);
|
|
15652
16186
|
toolSpans.set(toolCallId, { operationKey, span });
|
|
15653
16187
|
});
|
|
@@ -16223,15 +16757,20 @@ var FlueObserveBridge = class {
|
|
|
16223
16757
|
safeLog(existing.span, { input, metadata });
|
|
16224
16758
|
return;
|
|
16225
16759
|
}
|
|
16226
|
-
const span = startSpan(
|
|
16227
|
-
|
|
16228
|
-
|
|
16229
|
-
|
|
16230
|
-
|
|
16231
|
-
|
|
16232
|
-
|
|
16233
|
-
|
|
16234
|
-
|
|
16760
|
+
const span = startSpan(
|
|
16761
|
+
withSpanInstrumentationName(
|
|
16762
|
+
{
|
|
16763
|
+
name: `workflow:${workflowName}`,
|
|
16764
|
+
spanAttributes: { type: "task" /* TASK */ },
|
|
16765
|
+
startTime: eventTime(event.startedAt ?? event.timestamp),
|
|
16766
|
+
event: {
|
|
16767
|
+
input,
|
|
16768
|
+
metadata
|
|
16769
|
+
}
|
|
16770
|
+
},
|
|
16771
|
+
INSTRUMENTATION_NAMES.FLUE
|
|
16772
|
+
)
|
|
16773
|
+
);
|
|
16235
16774
|
this.runsById.set(event.runId, { metadata, span });
|
|
16236
16775
|
}
|
|
16237
16776
|
handleRunResume(event, ctx) {
|
|
@@ -16251,12 +16790,17 @@ var FlueObserveBridge = class {
|
|
|
16251
16790
|
safeLog(existing.span, { metadata });
|
|
16252
16791
|
return;
|
|
16253
16792
|
}
|
|
16254
|
-
const span = startSpan(
|
|
16255
|
-
|
|
16256
|
-
|
|
16257
|
-
|
|
16258
|
-
|
|
16259
|
-
|
|
16793
|
+
const span = startSpan(
|
|
16794
|
+
withSpanInstrumentationName(
|
|
16795
|
+
{
|
|
16796
|
+
name: `workflow:${workflowName}`,
|
|
16797
|
+
spanAttributes: { type: "task" /* TASK */ },
|
|
16798
|
+
startTime: eventTime(event.startedAt ?? event.timestamp),
|
|
16799
|
+
event: { metadata }
|
|
16800
|
+
},
|
|
16801
|
+
INSTRUMENTATION_NAMES.FLUE
|
|
16802
|
+
)
|
|
16803
|
+
);
|
|
16260
16804
|
this.runsById.set(event.runId, { metadata, span });
|
|
16261
16805
|
}
|
|
16262
16806
|
handleRunEnd(event) {
|
|
@@ -16895,7 +17439,14 @@ function stateMatchesRun(state, runId) {
|
|
|
16895
17439
|
return state.metadata["flue.run_id"] === runId;
|
|
16896
17440
|
}
|
|
16897
17441
|
function startFlueSpan(parent, args) {
|
|
16898
|
-
return parent ? withCurrent(
|
|
17442
|
+
return parent ? withCurrent(
|
|
17443
|
+
parent,
|
|
17444
|
+
() => startSpan(
|
|
17445
|
+
withSpanInstrumentationName(args, INSTRUMENTATION_NAMES.FLUE)
|
|
17446
|
+
)
|
|
17447
|
+
) : startSpan(
|
|
17448
|
+
withSpanInstrumentationName(args, INSTRUMENTATION_NAMES.FLUE)
|
|
17449
|
+
);
|
|
16899
17450
|
}
|
|
16900
17451
|
function runWithCurrentSpanStore(span, next) {
|
|
16901
17452
|
const state = _internalGetGlobalState();
|
|
@@ -16985,9 +17536,13 @@ var BraintrustLangChainCallbackHandler = class {
|
|
|
16985
17536
|
...this.options.debug ? { runId, parentRunId } : {}
|
|
16986
17537
|
}
|
|
16987
17538
|
};
|
|
16988
|
-
|
|
17539
|
+
const spanArgs = withSpanInstrumentationName(
|
|
17540
|
+
args,
|
|
17541
|
+
INSTRUMENTATION_NAMES.LANGCHAIN
|
|
17542
|
+
);
|
|
17543
|
+
let span = parentSpan.startSpan(spanArgs);
|
|
16989
17544
|
if (!Object.is(this.options.logger, NOOP_SPAN) && Object.is(span, NOOP_SPAN)) {
|
|
16990
|
-
span = initLogger().startSpan(
|
|
17545
|
+
span = initLogger().startSpan(spanArgs);
|
|
16991
17546
|
}
|
|
16992
17547
|
this.spans.set(runId, span);
|
|
16993
17548
|
}
|
|
@@ -17826,10 +18381,15 @@ function startCodexRun(event, operation) {
|
|
|
17826
18381
|
provider: "openai",
|
|
17827
18382
|
...event.moduleVersion ? { "openai_codex.version": event.moduleVersion } : {}
|
|
17828
18383
|
};
|
|
17829
|
-
const span = startSpan(
|
|
17830
|
-
|
|
17831
|
-
|
|
17832
|
-
|
|
18384
|
+
const span = startSpan(
|
|
18385
|
+
withSpanInstrumentationName(
|
|
18386
|
+
{
|
|
18387
|
+
name: "OpenAI Codex",
|
|
18388
|
+
spanAttributes: { type: "task" /* TASK */ }
|
|
18389
|
+
},
|
|
18390
|
+
INSTRUMENTATION_NAMES.OPENAI_CODEX
|
|
18391
|
+
)
|
|
18392
|
+
);
|
|
17833
18393
|
const startTime = getCurrentUnixTimestamp();
|
|
17834
18394
|
safeLog2(span, {
|
|
17835
18395
|
input: sanitizedInput,
|
|
@@ -17978,7 +18538,12 @@ async function createCompletedItemSpan(state, item) {
|
|
|
17978
18538
|
if (!spanArgs) {
|
|
17979
18539
|
return;
|
|
17980
18540
|
}
|
|
17981
|
-
const span = startSpan(
|
|
18541
|
+
const span = startSpan(
|
|
18542
|
+
withSpanInstrumentationName(
|
|
18543
|
+
spanArgs.start,
|
|
18544
|
+
INSTRUMENTATION_NAMES.OPENAI_CODEX
|
|
18545
|
+
)
|
|
18546
|
+
);
|
|
17982
18547
|
safeLog2(span, spanArgs.end);
|
|
17983
18548
|
span.end();
|
|
17984
18549
|
}
|
|
@@ -18020,15 +18585,20 @@ async function ensureActiveLlmSpan(state) {
|
|
|
18020
18585
|
...state.metadata["openai_codex.thread_id"] ? { "openai_codex.thread_id": state.metadata["openai_codex.thread_id"] } : {},
|
|
18021
18586
|
"openai_codex.llm_sequence": sequence
|
|
18022
18587
|
};
|
|
18023
|
-
const span = startSpan(
|
|
18024
|
-
|
|
18025
|
-
|
|
18026
|
-
|
|
18027
|
-
|
|
18028
|
-
|
|
18029
|
-
|
|
18030
|
-
|
|
18031
|
-
|
|
18588
|
+
const span = startSpan(
|
|
18589
|
+
withSpanInstrumentationName(
|
|
18590
|
+
{
|
|
18591
|
+
event: {
|
|
18592
|
+
...sequence === 1 ? { input: state.input } : {},
|
|
18593
|
+
metadata
|
|
18594
|
+
},
|
|
18595
|
+
name: "OpenAI Codex LLM",
|
|
18596
|
+
parent: await state.span.export(),
|
|
18597
|
+
spanAttributes: { type: "llm" /* LLM */ }
|
|
18598
|
+
},
|
|
18599
|
+
INSTRUMENTATION_NAMES.OPENAI_CODEX
|
|
18600
|
+
)
|
|
18601
|
+
);
|
|
18032
18602
|
state.activeLlmSpan = {
|
|
18033
18603
|
anonymousMessages: [],
|
|
18034
18604
|
anonymousReasoning: [],
|
|
@@ -18074,7 +18644,15 @@ async function startCodexItemSpan(state, item) {
|
|
|
18074
18644
|
if (!spanArgs) {
|
|
18075
18645
|
return;
|
|
18076
18646
|
}
|
|
18077
|
-
state.activeItemSpans.set(
|
|
18647
|
+
state.activeItemSpans.set(
|
|
18648
|
+
itemId,
|
|
18649
|
+
startSpan(
|
|
18650
|
+
withSpanInstrumentationName(
|
|
18651
|
+
spanArgs.start,
|
|
18652
|
+
INSTRUMENTATION_NAMES.OPENAI_CODEX
|
|
18653
|
+
)
|
|
18654
|
+
)
|
|
18655
|
+
);
|
|
18078
18656
|
}
|
|
18079
18657
|
function updateCodexItem(state, item) {
|
|
18080
18658
|
if (item.type === "agent_message" && typeof item.text === "string") {
|
|
@@ -18454,12 +19032,17 @@ var AnthropicPlugin = class extends BasePlugin {
|
|
|
18454
19032
|
return;
|
|
18455
19033
|
}
|
|
18456
19034
|
const params = event.arguments[0] ?? {};
|
|
18457
|
-
const span = startSpan(
|
|
18458
|
-
|
|
18459
|
-
|
|
18460
|
-
|
|
18461
|
-
|
|
18462
|
-
|
|
19035
|
+
const span = startSpan(
|
|
19036
|
+
withSpanInstrumentationName(
|
|
19037
|
+
{
|
|
19038
|
+
name: "anthropic.beta.messages.toolRunner",
|
|
19039
|
+
spanAttributes: {
|
|
19040
|
+
type: "task" /* TASK */
|
|
19041
|
+
}
|
|
19042
|
+
},
|
|
19043
|
+
INSTRUMENTATION_NAMES.ANTHROPIC
|
|
19044
|
+
)
|
|
19045
|
+
);
|
|
18463
19046
|
span.log({
|
|
18464
19047
|
input: processAttachmentsInInput(
|
|
18465
19048
|
coalesceInput(params.messages ?? [], params.system)
|
|
@@ -18600,19 +19183,22 @@ function wrapAnthropicToolRunnerTool(tool, index, state) {
|
|
|
18600
19183
|
return finalizeError(error);
|
|
18601
19184
|
}
|
|
18602
19185
|
},
|
|
18603
|
-
|
|
18604
|
-
|
|
18605
|
-
|
|
18606
|
-
|
|
18607
|
-
|
|
18608
|
-
|
|
19186
|
+
withSpanInstrumentationName(
|
|
19187
|
+
{
|
|
19188
|
+
event: {
|
|
19189
|
+
input: getAnthropicToolRunnerInput(args),
|
|
19190
|
+
metadata: {
|
|
19191
|
+
"gen_ai.tool.name": toolName,
|
|
19192
|
+
provider: "anthropic"
|
|
19193
|
+
}
|
|
19194
|
+
},
|
|
19195
|
+
name: `tool: ${toolName}`,
|
|
19196
|
+
spanAttributes: {
|
|
19197
|
+
type: "tool" /* TOOL */
|
|
18609
19198
|
}
|
|
18610
19199
|
},
|
|
18611
|
-
|
|
18612
|
-
|
|
18613
|
-
type: "tool" /* TOOL */
|
|
18614
|
-
}
|
|
18615
|
-
}
|
|
19200
|
+
INSTRUMENTATION_NAMES.ANTHROPIC
|
|
19201
|
+
)
|
|
18616
19202
|
);
|
|
18617
19203
|
},
|
|
18618
19204
|
writable: runDescriptor?.writable ?? true
|
|
@@ -19222,22 +19808,27 @@ function wrapLocalClaudeToolHandler(handler, getMetadata) {
|
|
|
19222
19808
|
const spanName = metadata.serverName ? `tool: ${metadata.serverName}/${metadata.toolName}` : `tool: ${metadata.toolName}`;
|
|
19223
19809
|
const runWithResolvedParent = async () => {
|
|
19224
19810
|
const parent = toolUseId && localToolParentResolver ? await localToolParentResolver(toolUseId).catch(() => void 0) : void 0;
|
|
19225
|
-
const span = startSpan(
|
|
19226
|
-
|
|
19227
|
-
|
|
19228
|
-
|
|
19229
|
-
|
|
19230
|
-
|
|
19231
|
-
|
|
19232
|
-
|
|
19233
|
-
|
|
19234
|
-
|
|
19235
|
-
|
|
19236
|
-
|
|
19237
|
-
|
|
19238
|
-
|
|
19239
|
-
|
|
19240
|
-
|
|
19811
|
+
const span = startSpan(
|
|
19812
|
+
withSpanInstrumentationName(
|
|
19813
|
+
{
|
|
19814
|
+
event: {
|
|
19815
|
+
input: handlerArgs[0],
|
|
19816
|
+
metadata: {
|
|
19817
|
+
"claude_agent_sdk.raw_tool_name": rawToolName,
|
|
19818
|
+
"gen_ai.tool.name": metadata.toolName,
|
|
19819
|
+
...toolUseId && { "gen_ai.tool.call.id": toolUseId },
|
|
19820
|
+
...metadata.serverName && {
|
|
19821
|
+
"mcp.server": metadata.serverName
|
|
19822
|
+
}
|
|
19823
|
+
}
|
|
19824
|
+
},
|
|
19825
|
+
name: spanName,
|
|
19826
|
+
...parent && { parent },
|
|
19827
|
+
spanAttributes: { type: "tool" /* TOOL */ }
|
|
19828
|
+
},
|
|
19829
|
+
INSTRUMENTATION_NAMES.CLAUDE_AGENT_SDK
|
|
19830
|
+
)
|
|
19831
|
+
);
|
|
19241
19832
|
const runHandler = () => Reflect.apply(handler, this, handlerArgs);
|
|
19242
19833
|
const finalizeSuccess = (result) => {
|
|
19243
19834
|
span.log({ output: result });
|
|
@@ -19562,14 +20153,19 @@ async function createLLMSpanForMessages(messages, promptMessages, conversationHi
|
|
|
19562
20153
|
).filter(
|
|
19563
20154
|
(c) => c !== void 0
|
|
19564
20155
|
);
|
|
19565
|
-
const span = existingSpan ?? startSpan(
|
|
19566
|
-
|
|
19567
|
-
|
|
19568
|
-
|
|
19569
|
-
|
|
19570
|
-
|
|
19571
|
-
|
|
19572
|
-
|
|
20156
|
+
const span = existingSpan ?? startSpan(
|
|
20157
|
+
withSpanInstrumentationName(
|
|
20158
|
+
{
|
|
20159
|
+
name: "anthropic.messages.create",
|
|
20160
|
+
parent: parentSpan,
|
|
20161
|
+
spanAttributes: {
|
|
20162
|
+
type: "llm" /* LLM */
|
|
20163
|
+
},
|
|
20164
|
+
startTime
|
|
20165
|
+
},
|
|
20166
|
+
INSTRUMENTATION_NAMES.CLAUDE_AGENT_SDK
|
|
20167
|
+
)
|
|
20168
|
+
);
|
|
19573
20169
|
span.log({
|
|
19574
20170
|
input,
|
|
19575
20171
|
metadata: model ? { model } : void 0,
|
|
@@ -19672,25 +20268,30 @@ function createToolTracingHooks(resolveParentSpan, activeToolSpans, mcpServers,
|
|
|
19672
20268
|
return {};
|
|
19673
20269
|
}
|
|
19674
20270
|
const parsed = parseToolName(input.tool_name);
|
|
19675
|
-
const toolSpan = startSpan(
|
|
19676
|
-
|
|
19677
|
-
|
|
19678
|
-
|
|
19679
|
-
|
|
19680
|
-
|
|
19681
|
-
|
|
19682
|
-
|
|
19683
|
-
|
|
19684
|
-
|
|
19685
|
-
|
|
19686
|
-
|
|
19687
|
-
|
|
19688
|
-
|
|
19689
|
-
|
|
19690
|
-
|
|
19691
|
-
|
|
19692
|
-
|
|
19693
|
-
|
|
20271
|
+
const toolSpan = startSpan(
|
|
20272
|
+
withSpanInstrumentationName(
|
|
20273
|
+
{
|
|
20274
|
+
event: {
|
|
20275
|
+
input: input.tool_input,
|
|
20276
|
+
metadata: {
|
|
20277
|
+
"claude_agent_sdk.cwd": input.cwd,
|
|
20278
|
+
"claude_agent_sdk.raw_tool_name": parsed.rawToolName,
|
|
20279
|
+
"claude_agent_sdk.session_id": input.session_id,
|
|
20280
|
+
"gen_ai.tool.call.id": toolUseID,
|
|
20281
|
+
"gen_ai.tool.name": parsed.toolName,
|
|
20282
|
+
...parsed.mcpServer && { "mcp.server": parsed.mcpServer },
|
|
20283
|
+
...getMcpServerMetadata(parsed.mcpServer, mcpServers)
|
|
20284
|
+
}
|
|
20285
|
+
},
|
|
20286
|
+
name: parsed.displayName,
|
|
20287
|
+
parent: await resolveParentSpan(toolUseID, {
|
|
20288
|
+
agentId: input.agent_id
|
|
20289
|
+
}),
|
|
20290
|
+
spanAttributes: { type: "tool" /* TOOL */ }
|
|
20291
|
+
},
|
|
20292
|
+
INSTRUMENTATION_NAMES.CLAUDE_AGENT_SDK
|
|
20293
|
+
)
|
|
20294
|
+
);
|
|
19694
20295
|
activeToolSpans.set(toolUseID, toolSpan);
|
|
19695
20296
|
return {};
|
|
19696
20297
|
};
|
|
@@ -20056,14 +20657,19 @@ async function ensureSubAgentSpan(subAgentDetailsByToolUseId, rootSpan, activeTo
|
|
|
20056
20657
|
const spanName = formatSubAgentSpanName(details);
|
|
20057
20658
|
const parentToolSpan = activeToolSpans.get(parentToolUseId);
|
|
20058
20659
|
const parentSpan = parentToolSpan ? await parentToolSpan.export() : await rootSpan.export();
|
|
20059
|
-
const subAgentSpan = startSpan(
|
|
20060
|
-
|
|
20061
|
-
|
|
20062
|
-
|
|
20063
|
-
|
|
20064
|
-
|
|
20065
|
-
|
|
20066
|
-
|
|
20660
|
+
const subAgentSpan = startSpan(
|
|
20661
|
+
withSpanInstrumentationName(
|
|
20662
|
+
{
|
|
20663
|
+
event: {
|
|
20664
|
+
metadata: subAgentDetailsToMetadata(details)
|
|
20665
|
+
},
|
|
20666
|
+
name: spanName,
|
|
20667
|
+
parent: parentSpan,
|
|
20668
|
+
spanAttributes: { type: "task" /* TASK */ }
|
|
20669
|
+
},
|
|
20670
|
+
INSTRUMENTATION_NAMES.CLAUDE_AGENT_SDK
|
|
20671
|
+
)
|
|
20672
|
+
);
|
|
20067
20673
|
subAgentSpans.set(parentToolUseId, subAgentSpan);
|
|
20068
20674
|
return subAgentSpan;
|
|
20069
20675
|
}
|
|
@@ -20084,14 +20690,19 @@ async function ensureActiveLlmSpanForParentToolUse(rootSpan, activeLlmSpansByPar
|
|
|
20084
20690
|
);
|
|
20085
20691
|
llmParentSpan = await subAgentSpan.export();
|
|
20086
20692
|
}
|
|
20087
|
-
const llmSpan = startSpan(
|
|
20088
|
-
|
|
20089
|
-
|
|
20090
|
-
|
|
20091
|
-
|
|
20092
|
-
|
|
20093
|
-
|
|
20094
|
-
|
|
20693
|
+
const llmSpan = startSpan(
|
|
20694
|
+
withSpanInstrumentationName(
|
|
20695
|
+
{
|
|
20696
|
+
name: "anthropic.messages.create",
|
|
20697
|
+
parent: llmParentSpan,
|
|
20698
|
+
spanAttributes: {
|
|
20699
|
+
type: "llm" /* LLM */
|
|
20700
|
+
},
|
|
20701
|
+
startTime
|
|
20702
|
+
},
|
|
20703
|
+
INSTRUMENTATION_NAMES.CLAUDE_AGENT_SDK
|
|
20704
|
+
)
|
|
20705
|
+
);
|
|
20095
20706
|
activeLlmSpansByParentToolUse.set(parentKey, llmSpan);
|
|
20096
20707
|
return llmSpan;
|
|
20097
20708
|
}
|
|
@@ -20360,12 +20971,17 @@ var ClaudeAgentSDKPlugin = class extends BasePlugin {
|
|
|
20360
20971
|
}
|
|
20361
20972
|
})();
|
|
20362
20973
|
}
|
|
20363
|
-
const span = startSpan(
|
|
20364
|
-
|
|
20365
|
-
|
|
20366
|
-
|
|
20367
|
-
|
|
20368
|
-
|
|
20974
|
+
const span = startSpan(
|
|
20975
|
+
withSpanInstrumentationName(
|
|
20976
|
+
{
|
|
20977
|
+
name: "Claude Agent",
|
|
20978
|
+
spanAttributes: {
|
|
20979
|
+
type: "task" /* TASK */
|
|
20980
|
+
}
|
|
20981
|
+
},
|
|
20982
|
+
INSTRUMENTATION_NAMES.CLAUDE_AGENT_SDK
|
|
20983
|
+
)
|
|
20984
|
+
);
|
|
20369
20985
|
const startTime = getCurrentUnixTimestamp();
|
|
20370
20986
|
try {
|
|
20371
20987
|
span.log({
|
|
@@ -20579,10 +21195,15 @@ var CursorSDKPlugin = class extends BasePlugin {
|
|
|
20579
21195
|
provider: "cursor",
|
|
20580
21196
|
...event.moduleVersion ? { "cursor_sdk.version": event.moduleVersion } : {}
|
|
20581
21197
|
};
|
|
20582
|
-
const span = startSpan(
|
|
20583
|
-
|
|
20584
|
-
|
|
20585
|
-
|
|
21198
|
+
const span = startSpan(
|
|
21199
|
+
withSpanInstrumentationName(
|
|
21200
|
+
{
|
|
21201
|
+
name: "Cursor Agent",
|
|
21202
|
+
spanAttributes: { type: "task" /* TASK */ }
|
|
21203
|
+
},
|
|
21204
|
+
INSTRUMENTATION_NAMES.CURSOR_SDK
|
|
21205
|
+
)
|
|
21206
|
+
);
|
|
20586
21207
|
const startTime = getCurrentUnixTimestamp();
|
|
20587
21208
|
safeLog3(span, {
|
|
20588
21209
|
input: sanitizeUserMessage(message),
|
|
@@ -20644,10 +21265,15 @@ var CursorSDKPlugin = class extends BasePlugin {
|
|
|
20644
21265
|
provider: "cursor",
|
|
20645
21266
|
...event.moduleVersion ? { "cursor_sdk.version": event.moduleVersion } : {}
|
|
20646
21267
|
};
|
|
20647
|
-
const span = startSpan(
|
|
20648
|
-
|
|
20649
|
-
|
|
20650
|
-
|
|
21268
|
+
const span = startSpan(
|
|
21269
|
+
withSpanInstrumentationName(
|
|
21270
|
+
{
|
|
21271
|
+
name: "Cursor Agent",
|
|
21272
|
+
spanAttributes: { type: "task" /* TASK */ }
|
|
21273
|
+
},
|
|
21274
|
+
INSTRUMENTATION_NAMES.CURSOR_SDK
|
|
21275
|
+
)
|
|
21276
|
+
);
|
|
20651
21277
|
const startTime = getCurrentUnixTimestamp();
|
|
20652
21278
|
safeLog3(span, {
|
|
20653
21279
|
input: sanitizeUserMessage(message),
|
|
@@ -21094,29 +21720,39 @@ async function startToolSpan(state, args) {
|
|
|
21094
21720
|
if (args.truncated?.result !== void 0) {
|
|
21095
21721
|
metadata["cursor_sdk.tool.result_truncated"] = args.truncated.result;
|
|
21096
21722
|
}
|
|
21097
|
-
const span = startSpan(
|
|
21098
|
-
|
|
21099
|
-
|
|
21100
|
-
|
|
21101
|
-
|
|
21102
|
-
|
|
21103
|
-
|
|
21104
|
-
|
|
21105
|
-
|
|
21723
|
+
const span = startSpan(
|
|
21724
|
+
withSpanInstrumentationName(
|
|
21725
|
+
{
|
|
21726
|
+
event: {
|
|
21727
|
+
input: args.args,
|
|
21728
|
+
metadata
|
|
21729
|
+
},
|
|
21730
|
+
name: `tool: ${name}`,
|
|
21731
|
+
parent: await state.span.export(),
|
|
21732
|
+
spanAttributes: { type: "tool" /* TOOL */ }
|
|
21733
|
+
},
|
|
21734
|
+
INSTRUMENTATION_NAMES.CURSOR_SDK
|
|
21735
|
+
)
|
|
21736
|
+
);
|
|
21106
21737
|
let subAgentSpan;
|
|
21107
21738
|
if (isSubAgentToolName(name)) {
|
|
21108
|
-
subAgentSpan = startSpan(
|
|
21109
|
-
|
|
21110
|
-
|
|
21111
|
-
|
|
21112
|
-
|
|
21113
|
-
|
|
21114
|
-
|
|
21115
|
-
|
|
21116
|
-
|
|
21117
|
-
|
|
21118
|
-
|
|
21119
|
-
|
|
21739
|
+
subAgentSpan = startSpan(
|
|
21740
|
+
withSpanInstrumentationName(
|
|
21741
|
+
{
|
|
21742
|
+
event: {
|
|
21743
|
+
input: args.args,
|
|
21744
|
+
metadata: {
|
|
21745
|
+
"cursor_sdk.subagent.tool_call_id": args.callId,
|
|
21746
|
+
"gen_ai.tool.name": name
|
|
21747
|
+
}
|
|
21748
|
+
},
|
|
21749
|
+
name: formatSubAgentSpanName2(args.toolCall, args.args),
|
|
21750
|
+
parent: await span.export(),
|
|
21751
|
+
spanAttributes: { type: "task" /* TASK */ }
|
|
21752
|
+
},
|
|
21753
|
+
INSTRUMENTATION_NAMES.CURSOR_SDK
|
|
21754
|
+
)
|
|
21755
|
+
);
|
|
21120
21756
|
}
|
|
21121
21757
|
return { span, subAgentSpan };
|
|
21122
21758
|
}
|
|
@@ -21488,16 +22124,19 @@ var OpenAIAgentsTraceProcessor = class _OpenAIAgentsTraceProcessor {
|
|
|
21488
22124
|
this.evictOldestTrace();
|
|
21489
22125
|
}
|
|
21490
22126
|
const current = currentSpan();
|
|
21491
|
-
const
|
|
21492
|
-
|
|
21493
|
-
|
|
21494
|
-
|
|
21495
|
-
|
|
21496
|
-
|
|
21497
|
-
|
|
21498
|
-
|
|
21499
|
-
|
|
21500
|
-
|
|
22127
|
+
const spanArgs = withSpanInstrumentationName(
|
|
22128
|
+
{
|
|
22129
|
+
name: trace.name,
|
|
22130
|
+
type: "task" /* TASK */
|
|
22131
|
+
},
|
|
22132
|
+
INSTRUMENTATION_NAMES.OPENAI_AGENTS
|
|
22133
|
+
);
|
|
22134
|
+
const span = current && current !== NOOP_SPAN ? current.startSpan(spanArgs) : this.logger ? this.logger.startSpan(spanArgs) : startSpan(
|
|
22135
|
+
withSpanInstrumentationName(
|
|
22136
|
+
spanArgs,
|
|
22137
|
+
INSTRUMENTATION_NAMES.OPENAI_AGENTS
|
|
22138
|
+
)
|
|
22139
|
+
);
|
|
21501
22140
|
span.log({
|
|
21502
22141
|
input: "Agent workflow started",
|
|
21503
22142
|
metadata: {
|
|
@@ -21548,10 +22187,15 @@ var OpenAIAgentsTraceProcessor = class _OpenAIAgentsTraceProcessor {
|
|
|
21548
22187
|
if (!parentSpan) {
|
|
21549
22188
|
return Promise.resolve();
|
|
21550
22189
|
}
|
|
21551
|
-
const childSpan = parentSpan.startSpan(
|
|
21552
|
-
|
|
21553
|
-
|
|
21554
|
-
|
|
22190
|
+
const childSpan = parentSpan.startSpan(
|
|
22191
|
+
withSpanInstrumentationName(
|
|
22192
|
+
{
|
|
22193
|
+
name: spanNameFromAgents(span),
|
|
22194
|
+
type: spanTypeFromAgents(span)
|
|
22195
|
+
},
|
|
22196
|
+
INSTRUMENTATION_NAMES.OPENAI_AGENTS
|
|
22197
|
+
)
|
|
22198
|
+
);
|
|
21555
22199
|
traceData.childSpans.set(span.spanId, childSpan);
|
|
21556
22200
|
return Promise.resolve();
|
|
21557
22201
|
}
|
|
@@ -21851,13 +22495,18 @@ var GoogleGenAIPlugin = class extends BasePlugin {
|
|
|
21851
22495
|
const params = event.arguments[0];
|
|
21852
22496
|
const input = serializeGenerateContentInput(params);
|
|
21853
22497
|
const metadata = extractGenerateContentMetadata(params);
|
|
21854
|
-
const span = startSpan(
|
|
21855
|
-
|
|
21856
|
-
|
|
21857
|
-
|
|
21858
|
-
|
|
21859
|
-
|
|
21860
|
-
|
|
22498
|
+
const span = startSpan(
|
|
22499
|
+
withSpanInstrumentationName(
|
|
22500
|
+
{
|
|
22501
|
+
name: "generate_content",
|
|
22502
|
+
spanAttributes: {
|
|
22503
|
+
type: "llm" /* LLM */
|
|
22504
|
+
},
|
|
22505
|
+
event: createWrapperParityEvent({ input, metadata })
|
|
22506
|
+
},
|
|
22507
|
+
INSTRUMENTATION_NAMES.GOOGLE_GENAI
|
|
22508
|
+
)
|
|
22509
|
+
);
|
|
21861
22510
|
return {
|
|
21862
22511
|
span,
|
|
21863
22512
|
startTime: getCurrentUnixTimestamp()
|
|
@@ -21870,13 +22519,18 @@ var GoogleGenAIPlugin = class extends BasePlugin {
|
|
|
21870
22519
|
const params = event.arguments[0];
|
|
21871
22520
|
const input = serializeGenerateContentInput(params);
|
|
21872
22521
|
const metadata = extractGenerateContentMetadata(params);
|
|
21873
|
-
const span = startSpan(
|
|
21874
|
-
|
|
21875
|
-
|
|
21876
|
-
|
|
21877
|
-
|
|
21878
|
-
|
|
21879
|
-
|
|
22522
|
+
const span = startSpan(
|
|
22523
|
+
withSpanInstrumentationName(
|
|
22524
|
+
{
|
|
22525
|
+
name: "generate_content",
|
|
22526
|
+
spanAttributes: {
|
|
22527
|
+
type: "llm" /* LLM */
|
|
22528
|
+
},
|
|
22529
|
+
event: createWrapperParityEvent({ input, metadata })
|
|
22530
|
+
},
|
|
22531
|
+
INSTRUMENTATION_NAMES.GOOGLE_GENAI
|
|
22532
|
+
)
|
|
22533
|
+
);
|
|
21880
22534
|
return {
|
|
21881
22535
|
span,
|
|
21882
22536
|
startTime: getCurrentUnixTimestamp()
|
|
@@ -21952,13 +22606,18 @@ var GoogleGenAIPlugin = class extends BasePlugin {
|
|
|
21952
22606
|
const params = event.arguments[0];
|
|
21953
22607
|
const input = serializeEmbedContentInput(params);
|
|
21954
22608
|
const metadata = extractEmbedContentMetadata(params);
|
|
21955
|
-
const span = startSpan(
|
|
21956
|
-
|
|
21957
|
-
|
|
21958
|
-
|
|
21959
|
-
|
|
21960
|
-
|
|
21961
|
-
|
|
22609
|
+
const span = startSpan(
|
|
22610
|
+
withSpanInstrumentationName(
|
|
22611
|
+
{
|
|
22612
|
+
name: "embed_content",
|
|
22613
|
+
spanAttributes: {
|
|
22614
|
+
type: "llm" /* LLM */
|
|
22615
|
+
},
|
|
22616
|
+
event: createWrapperParityEvent({ input, metadata })
|
|
22617
|
+
},
|
|
22618
|
+
INSTRUMENTATION_NAMES.GOOGLE_GENAI
|
|
22619
|
+
)
|
|
22620
|
+
);
|
|
21962
22621
|
return {
|
|
21963
22622
|
span,
|
|
21964
22623
|
startTime: getCurrentUnixTimestamp()
|
|
@@ -21971,13 +22630,18 @@ var GoogleGenAIPlugin = class extends BasePlugin {
|
|
|
21971
22630
|
const params = event.arguments[0];
|
|
21972
22631
|
const input = serializeEmbedContentInput(params);
|
|
21973
22632
|
const metadata = extractEmbedContentMetadata(params);
|
|
21974
|
-
const span = startSpan(
|
|
21975
|
-
|
|
21976
|
-
|
|
21977
|
-
|
|
21978
|
-
|
|
21979
|
-
|
|
21980
|
-
|
|
22633
|
+
const span = startSpan(
|
|
22634
|
+
withSpanInstrumentationName(
|
|
22635
|
+
{
|
|
22636
|
+
name: "embed_content",
|
|
22637
|
+
spanAttributes: {
|
|
22638
|
+
type: "llm" /* LLM */
|
|
22639
|
+
},
|
|
22640
|
+
event: createWrapperParityEvent({ input, metadata })
|
|
22641
|
+
},
|
|
22642
|
+
INSTRUMENTATION_NAMES.GOOGLE_GENAI
|
|
22643
|
+
)
|
|
22644
|
+
);
|
|
21981
22645
|
return {
|
|
21982
22646
|
span,
|
|
21983
22647
|
startTime: getCurrentUnixTimestamp()
|
|
@@ -22088,16 +22752,21 @@ function patchGoogleGenAIStreamingResult(args) {
|
|
|
22088
22752
|
const requestStartTime = startTime ?? getCurrentUnixTimestamp();
|
|
22089
22753
|
const ensureSpan = () => {
|
|
22090
22754
|
if (!span) {
|
|
22091
|
-
span = startSpan(
|
|
22092
|
-
|
|
22093
|
-
|
|
22094
|
-
|
|
22095
|
-
|
|
22096
|
-
|
|
22097
|
-
|
|
22098
|
-
|
|
22099
|
-
|
|
22100
|
-
|
|
22755
|
+
span = startSpan(
|
|
22756
|
+
withSpanInstrumentationName(
|
|
22757
|
+
{
|
|
22758
|
+
name: "generate_content_stream",
|
|
22759
|
+
spanAttributes: {
|
|
22760
|
+
type: "llm" /* LLM */
|
|
22761
|
+
},
|
|
22762
|
+
event: {
|
|
22763
|
+
input,
|
|
22764
|
+
metadata
|
|
22765
|
+
}
|
|
22766
|
+
},
|
|
22767
|
+
INSTRUMENTATION_NAMES.GOOGLE_GENAI
|
|
22768
|
+
)
|
|
22769
|
+
);
|
|
22101
22770
|
}
|
|
22102
22771
|
return span;
|
|
22103
22772
|
};
|
|
@@ -25732,12 +26401,17 @@ var GoogleADKPlugin = class extends BasePlugin {
|
|
|
25732
26401
|
const createState = (event) => {
|
|
25733
26402
|
const params = event.arguments[0] ?? {};
|
|
25734
26403
|
const contextKey = extractRunnerContextKey(params);
|
|
25735
|
-
const span = startSpan(
|
|
25736
|
-
|
|
25737
|
-
|
|
25738
|
-
|
|
25739
|
-
|
|
25740
|
-
|
|
26404
|
+
const span = startSpan(
|
|
26405
|
+
withSpanInstrumentationName(
|
|
26406
|
+
{
|
|
26407
|
+
name: "Google ADK Runner",
|
|
26408
|
+
spanAttributes: {
|
|
26409
|
+
type: "task" /* TASK */
|
|
26410
|
+
}
|
|
26411
|
+
},
|
|
26412
|
+
INSTRUMENTATION_NAMES.GOOGLE_ADK
|
|
26413
|
+
)
|
|
26414
|
+
);
|
|
25741
26415
|
const startTime = getCurrentUnixTimestamp();
|
|
25742
26416
|
try {
|
|
25743
26417
|
const metadata = extractRunnerMetadata(params);
|
|
@@ -25823,18 +26497,23 @@ var GoogleADKPlugin = class extends BasePlugin {
|
|
|
25823
26497
|
this.activeRunnerSpans
|
|
25824
26498
|
);
|
|
25825
26499
|
const contextKey = extractInvocationContextKey(parentContext);
|
|
25826
|
-
const span = startSpan(
|
|
25827
|
-
|
|
25828
|
-
|
|
25829
|
-
|
|
25830
|
-
|
|
25831
|
-
|
|
25832
|
-
|
|
25833
|
-
|
|
25834
|
-
|
|
25835
|
-
|
|
25836
|
-
|
|
25837
|
-
|
|
26500
|
+
const span = startSpan(
|
|
26501
|
+
withSpanInstrumentationName(
|
|
26502
|
+
{
|
|
26503
|
+
name: agentName ? `Agent: ${agentName}` : "Google ADK Agent",
|
|
26504
|
+
spanAttributes: {
|
|
26505
|
+
type: "task" /* TASK */
|
|
26506
|
+
},
|
|
26507
|
+
...runnerParentSpan ? {
|
|
26508
|
+
parentSpanIds: {
|
|
26509
|
+
spanId: runnerParentSpan.spanId,
|
|
26510
|
+
rootSpanId: runnerParentSpan.rootSpanId
|
|
26511
|
+
}
|
|
26512
|
+
} : {}
|
|
26513
|
+
},
|
|
26514
|
+
INSTRUMENTATION_NAMES.GOOGLE_ADK
|
|
26515
|
+
)
|
|
26516
|
+
);
|
|
25838
26517
|
const startTime = getCurrentUnixTimestamp();
|
|
25839
26518
|
try {
|
|
25840
26519
|
const metadata = {
|
|
@@ -25927,22 +26606,27 @@ var GoogleADKPlugin = class extends BasePlugin {
|
|
|
25927
26606
|
this.activeAgentSpans,
|
|
25928
26607
|
this.activeRunnerSpans
|
|
25929
26608
|
);
|
|
25930
|
-
const createSpan = () => startSpan(
|
|
25931
|
-
|
|
25932
|
-
|
|
25933
|
-
|
|
25934
|
-
|
|
25935
|
-
|
|
25936
|
-
|
|
25937
|
-
|
|
25938
|
-
|
|
25939
|
-
|
|
25940
|
-
|
|
25941
|
-
|
|
26609
|
+
const createSpan = () => startSpan(
|
|
26610
|
+
withSpanInstrumentationName(
|
|
26611
|
+
{
|
|
26612
|
+
name: toolName ? `tool: ${toolName}` : "Google ADK Tool",
|
|
26613
|
+
spanAttributes: {
|
|
26614
|
+
type: "tool" /* TOOL */
|
|
26615
|
+
},
|
|
26616
|
+
event: {
|
|
26617
|
+
input: req.args,
|
|
26618
|
+
metadata: {
|
|
26619
|
+
provider: "google-adk",
|
|
26620
|
+
...toolName && { "google_adk.tool_name": toolName },
|
|
26621
|
+
...extractToolCallId(req) && {
|
|
26622
|
+
"google_adk.tool_call_id": extractToolCallId(req)
|
|
26623
|
+
}
|
|
26624
|
+
}
|
|
25942
26625
|
}
|
|
25943
|
-
}
|
|
25944
|
-
|
|
25945
|
-
|
|
26626
|
+
},
|
|
26627
|
+
INSTRUMENTATION_NAMES.GOOGLE_ADK
|
|
26628
|
+
)
|
|
26629
|
+
);
|
|
25946
26630
|
const span = parentSpan ? withCurrent(parentSpan, () => createSpan()) : createSpan();
|
|
25947
26631
|
const startTime = getCurrentUnixTimestamp();
|
|
25948
26632
|
states.set(event, { span, startTime });
|
|
@@ -27916,12 +28600,17 @@ function startActionSpanState(args) {
|
|
|
27916
28600
|
if (!shouldTraceAction(args.metadata, args.runStepName)) {
|
|
27917
28601
|
return void 0;
|
|
27918
28602
|
}
|
|
27919
|
-
const span = startSpan(
|
|
27920
|
-
|
|
27921
|
-
|
|
27922
|
-
|
|
27923
|
-
|
|
27924
|
-
|
|
28603
|
+
const span = startSpan(
|
|
28604
|
+
withSpanInstrumentationName(
|
|
28605
|
+
{
|
|
28606
|
+
name: actionSpanName(args.metadata, args.runStepName),
|
|
28607
|
+
spanAttributes: {
|
|
28608
|
+
type: actionSpanType(args.metadata)
|
|
28609
|
+
}
|
|
28610
|
+
},
|
|
28611
|
+
INSTRUMENTATION_NAMES.GENKIT
|
|
28612
|
+
)
|
|
28613
|
+
);
|
|
27925
28614
|
const startTime = getCurrentUnixTimestamp();
|
|
27926
28615
|
span.log({
|
|
27927
28616
|
input: args.input,
|
|
@@ -28370,11 +29059,16 @@ async function handleTurnStart(state, agentId) {
|
|
|
28370
29059
|
return;
|
|
28371
29060
|
}
|
|
28372
29061
|
const parentId = await getParentIdForAgent(state, agentId);
|
|
28373
|
-
const span = startSpan(
|
|
28374
|
-
|
|
28375
|
-
|
|
28376
|
-
|
|
28377
|
-
|
|
29062
|
+
const span = startSpan(
|
|
29063
|
+
withSpanInstrumentationName(
|
|
29064
|
+
{
|
|
29065
|
+
name: "Copilot Turn",
|
|
29066
|
+
parent: parentId,
|
|
29067
|
+
spanAttributes: { type: "task" /* TASK */ }
|
|
29068
|
+
},
|
|
29069
|
+
INSTRUMENTATION_NAMES.GITHUB_COPILOT
|
|
29070
|
+
)
|
|
29071
|
+
);
|
|
28378
29072
|
const pendingUserMessage = state.pendingUserMessages.get(key);
|
|
28379
29073
|
if (pendingUserMessage) {
|
|
28380
29074
|
span.log({ input: pendingUserMessage });
|
|
@@ -28405,11 +29099,16 @@ async function handleUsage(state, agentId, usage) {
|
|
|
28405
29099
|
const parentId = turn ? await turn.id : await state.session.id;
|
|
28406
29100
|
const content = state.currentMessageContent.get(key);
|
|
28407
29101
|
const { metrics, metadata } = extractMetricsFromUsage(usage);
|
|
28408
|
-
const llmSpan = startSpan(
|
|
28409
|
-
|
|
28410
|
-
|
|
28411
|
-
|
|
28412
|
-
|
|
29102
|
+
const llmSpan = startSpan(
|
|
29103
|
+
withSpanInstrumentationName(
|
|
29104
|
+
{
|
|
29105
|
+
name: "github.copilot.llm",
|
|
29106
|
+
parent: parentId,
|
|
29107
|
+
spanAttributes: { type: "llm" /* LLM */ }
|
|
29108
|
+
},
|
|
29109
|
+
INSTRUMENTATION_NAMES.GITHUB_COPILOT
|
|
29110
|
+
)
|
|
29111
|
+
);
|
|
28413
29112
|
llmSpan.log({
|
|
28414
29113
|
output: content ?? void 0,
|
|
28415
29114
|
metadata,
|
|
@@ -28435,11 +29134,16 @@ async function handleToolStart(state, agentId, toolCallId, toolName, args, mcpSe
|
|
|
28435
29134
|
}
|
|
28436
29135
|
const parentId = await getToolParentId(state, agentId);
|
|
28437
29136
|
const displayName = mcpServerName ? `tool: ${mcpServerName}/${toolName}` : `tool: ${toolName}`;
|
|
28438
|
-
const span = startSpan(
|
|
28439
|
-
|
|
28440
|
-
|
|
28441
|
-
|
|
28442
|
-
|
|
29137
|
+
const span = startSpan(
|
|
29138
|
+
withSpanInstrumentationName(
|
|
29139
|
+
{
|
|
29140
|
+
name: displayName,
|
|
29141
|
+
parent: parentId,
|
|
29142
|
+
spanAttributes: { type: "tool" /* TOOL */ }
|
|
29143
|
+
},
|
|
29144
|
+
INSTRUMENTATION_NAMES.GITHUB_COPILOT
|
|
29145
|
+
)
|
|
29146
|
+
);
|
|
28443
29147
|
const metadata = {
|
|
28444
29148
|
"gen_ai.tool.name": toolName,
|
|
28445
29149
|
"gen_ai.tool.call.id": toolCallId
|
|
@@ -28475,11 +29179,16 @@ async function handleSubagentStarted(state, agentId, toolCallId, agentDisplayNam
|
|
|
28475
29179
|
}
|
|
28476
29180
|
const tool = state.activeTools.get(toolCallId);
|
|
28477
29181
|
const parentId = tool ? await tool.id : await state.session.id;
|
|
28478
|
-
const span = startSpan(
|
|
28479
|
-
|
|
28480
|
-
|
|
28481
|
-
|
|
28482
|
-
|
|
29182
|
+
const span = startSpan(
|
|
29183
|
+
withSpanInstrumentationName(
|
|
29184
|
+
{
|
|
29185
|
+
name: `Agent: ${agentDisplayName}`,
|
|
29186
|
+
parent: parentId,
|
|
29187
|
+
spanAttributes: { type: "task" /* TASK */ }
|
|
29188
|
+
},
|
|
29189
|
+
INSTRUMENTATION_NAMES.GITHUB_COPILOT
|
|
29190
|
+
)
|
|
29191
|
+
);
|
|
28483
29192
|
span.log({
|
|
28484
29193
|
metadata: {
|
|
28485
29194
|
"github_copilot.agent_name": agentDisplayName,
|
|
@@ -28705,10 +29414,15 @@ function makeSessionHandlers(sessionStates, configArgIndex, includeProviderMetad
|
|
|
28705
29414
|
if (!config || typeof config !== "object") {
|
|
28706
29415
|
return;
|
|
28707
29416
|
}
|
|
28708
|
-
const sessionSpan = startSpan(
|
|
28709
|
-
|
|
28710
|
-
|
|
28711
|
-
|
|
29417
|
+
const sessionSpan = startSpan(
|
|
29418
|
+
withSpanInstrumentationName(
|
|
29419
|
+
{
|
|
29420
|
+
name: "Copilot Session",
|
|
29421
|
+
spanAttributes: { type: "task" /* TASK */ }
|
|
29422
|
+
},
|
|
29423
|
+
INSTRUMENTATION_NAMES.GITHUB_COPILOT
|
|
29424
|
+
)
|
|
29425
|
+
);
|
|
28712
29426
|
const metadata = {};
|
|
28713
29427
|
if (config.model) {
|
|
28714
29428
|
metadata["github_copilot.model"] = config.model;
|
|
@@ -29042,19 +29756,24 @@ var LangSmithPlugin = class extends BasePlugin {
|
|
|
29042
29756
|
const traceId = stringValue2(ownValue(run, "trace_id")) ?? id;
|
|
29043
29757
|
const parentId = stringValue2(ownValue(run, "parent_run_id")) ?? stringValue2(ownValue(ownValue(run, "parent_run"), "id"));
|
|
29044
29758
|
const startTime = timestampSeconds(ownValue(run, "start_time"));
|
|
29045
|
-
return startSpan(
|
|
29046
|
-
|
|
29047
|
-
|
|
29048
|
-
|
|
29049
|
-
|
|
29050
|
-
|
|
29051
|
-
|
|
29052
|
-
|
|
29053
|
-
|
|
29054
|
-
|
|
29055
|
-
|
|
29056
|
-
|
|
29057
|
-
|
|
29759
|
+
return startSpan(
|
|
29760
|
+
withSpanInstrumentationName(
|
|
29761
|
+
{
|
|
29762
|
+
name: stringValue2(ownValue(run, "name")) ?? "LangSmith run",
|
|
29763
|
+
spanId: id,
|
|
29764
|
+
parentSpanIds: {
|
|
29765
|
+
parentSpanIds: parentId ? [parentId] : [],
|
|
29766
|
+
rootSpanId: traceId
|
|
29767
|
+
},
|
|
29768
|
+
spanAttributes: {
|
|
29769
|
+
type: mapRunType(ownValue(run, "run_type"))
|
|
29770
|
+
},
|
|
29771
|
+
...startTime === void 0 ? {} : { startTime },
|
|
29772
|
+
event: { id }
|
|
29773
|
+
},
|
|
29774
|
+
INSTRUMENTATION_NAMES.LANGSMITH
|
|
29775
|
+
)
|
|
29776
|
+
);
|
|
29058
29777
|
}
|
|
29059
29778
|
logRun(span, run, previous, includeOutput) {
|
|
29060
29779
|
const inputs = preferOwnValue(run, previous, "inputs");
|
|
@@ -29385,14 +30104,19 @@ function startPiPromptRun(event, onFinalize) {
|
|
|
29385
30104
|
...session.model?.id || agent.state?.model?.id ? { model: session.model?.id ?? agent.state?.model?.id } : {},
|
|
29386
30105
|
...event.moduleVersion ? { "pi_coding_agent.version": event.moduleVersion } : {}
|
|
29387
30106
|
};
|
|
29388
|
-
const span = startSpan(
|
|
29389
|
-
|
|
29390
|
-
|
|
29391
|
-
|
|
29392
|
-
|
|
29393
|
-
|
|
29394
|
-
|
|
29395
|
-
|
|
30107
|
+
const span = startSpan(
|
|
30108
|
+
withSpanInstrumentationName(
|
|
30109
|
+
{
|
|
30110
|
+
event: {
|
|
30111
|
+
input: extractPromptInput(event.arguments[0], event.arguments[1]),
|
|
30112
|
+
metadata
|
|
30113
|
+
},
|
|
30114
|
+
name: "AgentSession.prompt",
|
|
30115
|
+
spanAttributes: { type: "task" /* TASK */ }
|
|
30116
|
+
},
|
|
30117
|
+
INSTRUMENTATION_NAMES.PI_CODING_AGENT
|
|
30118
|
+
)
|
|
30119
|
+
);
|
|
29396
30120
|
const streamPatchState = installPiStreamPatch(agent);
|
|
29397
30121
|
const options = event.arguments[1];
|
|
29398
30122
|
const promptText = event.arguments[0];
|
|
@@ -29562,15 +30286,20 @@ async function startPiLlmSpan(state, model, context, options) {
|
|
|
29562
30286
|
...extractToolMetadata(context.tools),
|
|
29563
30287
|
"pi_coding_agent.operation": "agent.streamFn"
|
|
29564
30288
|
};
|
|
29565
|
-
const span = startSpan(
|
|
29566
|
-
|
|
29567
|
-
|
|
29568
|
-
|
|
29569
|
-
|
|
29570
|
-
|
|
29571
|
-
|
|
29572
|
-
|
|
29573
|
-
|
|
30289
|
+
const span = startSpan(
|
|
30290
|
+
withSpanInstrumentationName(
|
|
30291
|
+
{
|
|
30292
|
+
event: {
|
|
30293
|
+
input: processInputAttachments(normalizePiContextInput(context)),
|
|
30294
|
+
metadata
|
|
30295
|
+
},
|
|
30296
|
+
name: getLlmSpanName(model),
|
|
30297
|
+
parent: await state.span.export(),
|
|
30298
|
+
spanAttributes: { type: "llm" /* LLM */ }
|
|
30299
|
+
},
|
|
30300
|
+
INSTRUMENTATION_NAMES.PI_CODING_AGENT
|
|
30301
|
+
)
|
|
30302
|
+
);
|
|
29574
30303
|
const llmState = {
|
|
29575
30304
|
finalized: false,
|
|
29576
30305
|
metadata,
|
|
@@ -29738,15 +30467,20 @@ async function startPiToolSpan(state, event) {
|
|
|
29738
30467
|
"pi_coding_agent.tool.name": event.toolName
|
|
29739
30468
|
};
|
|
29740
30469
|
try {
|
|
29741
|
-
const span = startSpan(
|
|
29742
|
-
|
|
29743
|
-
|
|
29744
|
-
|
|
29745
|
-
|
|
29746
|
-
|
|
29747
|
-
|
|
29748
|
-
|
|
29749
|
-
|
|
30470
|
+
const span = startSpan(
|
|
30471
|
+
withSpanInstrumentationName(
|
|
30472
|
+
{
|
|
30473
|
+
event: {
|
|
30474
|
+
input: event.args,
|
|
30475
|
+
metadata
|
|
30476
|
+
},
|
|
30477
|
+
name: event.toolName || "tool",
|
|
30478
|
+
parent: await state.span.export(),
|
|
30479
|
+
spanAttributes: { type: "tool" /* TOOL */ }
|
|
30480
|
+
},
|
|
30481
|
+
INSTRUMENTATION_NAMES.PI_CODING_AGENT
|
|
30482
|
+
)
|
|
30483
|
+
);
|
|
29750
30484
|
state.activeToolSpans.set(event.toolCallId, {
|
|
29751
30485
|
restoreAutoInstrumentation,
|
|
29752
30486
|
span
|
|
@@ -30330,22 +31064,32 @@ function startAgentStream(event, activeChildParents) {
|
|
|
30330
31064
|
);
|
|
30331
31065
|
const span = parentSpan ? withCurrent(
|
|
30332
31066
|
parentSpan,
|
|
30333
|
-
() => startSpan(
|
|
30334
|
-
|
|
30335
|
-
|
|
30336
|
-
|
|
31067
|
+
() => startSpan(
|
|
31068
|
+
withSpanInstrumentationName(
|
|
31069
|
+
{
|
|
31070
|
+
event: {
|
|
31071
|
+
input,
|
|
31072
|
+
metadata
|
|
31073
|
+
},
|
|
31074
|
+
name: formatAgentSpanName(agent),
|
|
31075
|
+
spanAttributes: { type: "task" /* TASK */ }
|
|
31076
|
+
},
|
|
31077
|
+
INSTRUMENTATION_NAMES.STRANDS_AGENT_SDK
|
|
31078
|
+
)
|
|
31079
|
+
)
|
|
31080
|
+
) : startSpan(
|
|
31081
|
+
withSpanInstrumentationName(
|
|
31082
|
+
{
|
|
31083
|
+
event: {
|
|
31084
|
+
input,
|
|
31085
|
+
metadata
|
|
31086
|
+
},
|
|
31087
|
+
name: formatAgentSpanName(agent),
|
|
31088
|
+
spanAttributes: { type: "task" /* TASK */ }
|
|
30337
31089
|
},
|
|
30338
|
-
|
|
30339
|
-
|
|
30340
|
-
|
|
30341
|
-
) : startSpan({
|
|
30342
|
-
event: {
|
|
30343
|
-
input,
|
|
30344
|
-
metadata
|
|
30345
|
-
},
|
|
30346
|
-
name: formatAgentSpanName(agent),
|
|
30347
|
-
spanAttributes: { type: "task" /* TASK */ }
|
|
30348
|
-
});
|
|
31090
|
+
INSTRUMENTATION_NAMES.STRANDS_AGENT_SDK
|
|
31091
|
+
)
|
|
31092
|
+
);
|
|
30349
31093
|
return {
|
|
30350
31094
|
activeTools: /* @__PURE__ */ new Map(),
|
|
30351
31095
|
attachmentCache,
|
|
@@ -30367,22 +31111,32 @@ function startMultiAgentStream(event, operation, activeChildParents) {
|
|
|
30367
31111
|
const input = processStrandsInputAttachments(event.arguments[0]);
|
|
30368
31112
|
const span = parentSpan ? withCurrent(
|
|
30369
31113
|
parentSpan,
|
|
30370
|
-
() => startSpan(
|
|
30371
|
-
|
|
30372
|
-
|
|
30373
|
-
|
|
31114
|
+
() => startSpan(
|
|
31115
|
+
withSpanInstrumentationName(
|
|
31116
|
+
{
|
|
31117
|
+
event: {
|
|
31118
|
+
input,
|
|
31119
|
+
metadata
|
|
31120
|
+
},
|
|
31121
|
+
name: operation === "Graph.stream" ? "Strands Graph" : "Strands Swarm",
|
|
31122
|
+
spanAttributes: { type: "task" /* TASK */ }
|
|
31123
|
+
},
|
|
31124
|
+
INSTRUMENTATION_NAMES.STRANDS_AGENT_SDK
|
|
31125
|
+
)
|
|
31126
|
+
)
|
|
31127
|
+
) : startSpan(
|
|
31128
|
+
withSpanInstrumentationName(
|
|
31129
|
+
{
|
|
31130
|
+
event: {
|
|
31131
|
+
input,
|
|
31132
|
+
metadata
|
|
31133
|
+
},
|
|
31134
|
+
name: operation === "Graph.stream" ? "Strands Graph" : "Strands Swarm",
|
|
31135
|
+
spanAttributes: { type: "task" /* TASK */ }
|
|
30374
31136
|
},
|
|
30375
|
-
|
|
30376
|
-
|
|
30377
|
-
|
|
30378
|
-
) : startSpan({
|
|
30379
|
-
event: {
|
|
30380
|
-
input,
|
|
30381
|
-
metadata
|
|
30382
|
-
},
|
|
30383
|
-
name: operation === "Graph.stream" ? "Strands Graph" : "Strands Swarm",
|
|
30384
|
-
spanAttributes: { type: "task" /* TASK */ }
|
|
30385
|
-
});
|
|
31137
|
+
INSTRUMENTATION_NAMES.STRANDS_AGENT_SDK
|
|
31138
|
+
)
|
|
31139
|
+
);
|
|
30386
31140
|
return {
|
|
30387
31141
|
activeNodes: /* @__PURE__ */ new Map(),
|
|
30388
31142
|
finalized: false,
|
|
@@ -30484,17 +31238,22 @@ function startModelSpan(state, event) {
|
|
|
30484
31238
|
};
|
|
30485
31239
|
const span = withCurrent(
|
|
30486
31240
|
state.span,
|
|
30487
|
-
() => startSpan(
|
|
30488
|
-
|
|
30489
|
-
|
|
30490
|
-
event
|
|
30491
|
-
|
|
30492
|
-
|
|
30493
|
-
|
|
30494
|
-
|
|
30495
|
-
|
|
30496
|
-
|
|
30497
|
-
|
|
31241
|
+
() => startSpan(
|
|
31242
|
+
withSpanInstrumentationName(
|
|
31243
|
+
{
|
|
31244
|
+
event: {
|
|
31245
|
+
input: Array.isArray(event.agent?.messages) ? processStrandsInputAttachments(
|
|
31246
|
+
event.agent.messages,
|
|
31247
|
+
state.attachmentCache
|
|
31248
|
+
) : void 0,
|
|
31249
|
+
metadata
|
|
31250
|
+
},
|
|
31251
|
+
name: formatModelSpanName(model),
|
|
31252
|
+
spanAttributes: { type: "llm" /* LLM */ }
|
|
31253
|
+
},
|
|
31254
|
+
INSTRUMENTATION_NAMES.STRANDS_AGENT_SDK
|
|
31255
|
+
)
|
|
31256
|
+
)
|
|
30498
31257
|
);
|
|
30499
31258
|
state.activeModel = {
|
|
30500
31259
|
metadata,
|
|
@@ -30546,20 +31305,25 @@ function startToolSpan2(state, event) {
|
|
|
30546
31305
|
const name = extractToolName3(toolUse, event.tool);
|
|
30547
31306
|
const span = withCurrent(
|
|
30548
31307
|
state.span,
|
|
30549
|
-
() => startSpan(
|
|
30550
|
-
|
|
30551
|
-
|
|
30552
|
-
|
|
30553
|
-
|
|
30554
|
-
|
|
30555
|
-
|
|
30556
|
-
|
|
30557
|
-
|
|
30558
|
-
|
|
30559
|
-
|
|
30560
|
-
|
|
30561
|
-
|
|
30562
|
-
|
|
31308
|
+
() => startSpan(
|
|
31309
|
+
withSpanInstrumentationName(
|
|
31310
|
+
{
|
|
31311
|
+
event: {
|
|
31312
|
+
input: toolUse?.input,
|
|
31313
|
+
metadata: {
|
|
31314
|
+
"gen_ai.tool.call.id": toolUse?.toolUseId,
|
|
31315
|
+
"gen_ai.tool.name": name,
|
|
31316
|
+
"strands.operation": "tool.call",
|
|
31317
|
+
"strands.tool.name": name,
|
|
31318
|
+
provider: "strands"
|
|
31319
|
+
}
|
|
31320
|
+
},
|
|
31321
|
+
name: `tool: ${name}`,
|
|
31322
|
+
spanAttributes: { type: "tool" /* TOOL */ }
|
|
31323
|
+
},
|
|
31324
|
+
INSTRUMENTATION_NAMES.STRANDS_AGENT_SDK
|
|
31325
|
+
)
|
|
31326
|
+
)
|
|
30563
31327
|
);
|
|
30564
31328
|
state.activeTools.set(key, {
|
|
30565
31329
|
span,
|
|
@@ -30630,11 +31394,16 @@ function startNodeSpan(state, event, activeChildParents) {
|
|
|
30630
31394
|
};
|
|
30631
31395
|
const span = withCurrent(
|
|
30632
31396
|
state.span,
|
|
30633
|
-
() => startSpan(
|
|
30634
|
-
|
|
30635
|
-
|
|
30636
|
-
|
|
30637
|
-
|
|
31397
|
+
() => startSpan(
|
|
31398
|
+
withSpanInstrumentationName(
|
|
31399
|
+
{
|
|
31400
|
+
event: { metadata },
|
|
31401
|
+
name: `node: ${nodeId}`,
|
|
31402
|
+
spanAttributes: { type: "task" /* TASK */ }
|
|
31403
|
+
},
|
|
31404
|
+
INSTRUMENTATION_NAMES.STRANDS_AGENT_SDK
|
|
31405
|
+
)
|
|
31406
|
+
)
|
|
30638
31407
|
);
|
|
30639
31408
|
const nodeState = {
|
|
30640
31409
|
...child ? { child } : {},
|
|
@@ -31571,17 +32340,20 @@ var BraintrustObservabilityExporter = class {
|
|
|
31571
32340
|
}
|
|
31572
32341
|
onStart(exported) {
|
|
31573
32342
|
if (this.spans.has(exported.id)) return;
|
|
31574
|
-
const args =
|
|
31575
|
-
|
|
31576
|
-
|
|
31577
|
-
|
|
31578
|
-
|
|
31579
|
-
|
|
31580
|
-
|
|
31581
|
-
|
|
31582
|
-
|
|
31583
|
-
|
|
31584
|
-
|
|
32343
|
+
const args = withSpanInstrumentationName(
|
|
32344
|
+
{
|
|
32345
|
+
name: exported.name,
|
|
32346
|
+
spanAttributes: { type: spanTypeFor(exported.type) },
|
|
32347
|
+
startTime: epochSeconds(exported.startTime),
|
|
32348
|
+
// Use the Mastra span id as the Braintrust row id so that
|
|
32349
|
+
// `logFeedback({ id: <mastra span id> })` (and Mastra's score events)
|
|
32350
|
+
// attach to the right row. Without this, `SpanImpl` auto-generates a
|
|
32351
|
+
// row id (`this._id = eventId ?? idGenerator.getSpanId()`) that no
|
|
32352
|
+
// external caller could know.
|
|
32353
|
+
event: { id: exported.id }
|
|
32354
|
+
},
|
|
32355
|
+
INSTRUMENTATION_NAMES.MASTRA
|
|
32356
|
+
);
|
|
31585
32357
|
const parentRecord = exported.parentSpanId ? this.spans.get(exported.parentSpanId) : void 0;
|
|
31586
32358
|
if (!this.capturedParent) {
|
|
31587
32359
|
const probe = currentSpan();
|
|
@@ -31967,6 +32739,12 @@ function configureNode() {
|
|
|
31967
32739
|
isomorph_default.gzip = promisify(zlib.gzip);
|
|
31968
32740
|
isomorph_default.gunzip = promisify(zlib.gunzip);
|
|
31969
32741
|
isomorph_default.hash = (data) => crypto.createHash("sha256").update(data).digest("hex");
|
|
32742
|
+
isomorph_default.hmacSha256 = (key, data) => crypto.createHmac("sha256", key).update(data).digest("hex");
|
|
32743
|
+
isomorph_default.timingSafeEqual = (left, right) => {
|
|
32744
|
+
const leftBuffer = Buffer.from(left);
|
|
32745
|
+
const rightBuffer = Buffer.from(right);
|
|
32746
|
+
return leftBuffer.length === rightBuffer.length && crypto.timingSafeEqual(leftBuffer, rightBuffer);
|
|
32747
|
+
};
|
|
31970
32748
|
_internalSetInitialState();
|
|
31971
32749
|
const disabled = readDisabledInstrumentationEnvConfig(
|
|
31972
32750
|
isomorph_default.getEnv("BRAINTRUST_DISABLE_INSTRUMENTATION")
|