braintrust 3.28.0 → 3.29.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.
Files changed (50) hide show
  1. package/README.md +1 -1
  2. package/dev/dist/index.d.mts +356 -7
  3. package/dev/dist/index.d.ts +356 -7
  4. package/dev/dist/index.js +1815 -1548
  5. package/dev/dist/index.mjs +1054 -787
  6. package/dist/apply-auto-instrumentation.js +297 -264
  7. package/dist/apply-auto-instrumentation.mjs +84 -51
  8. package/dist/auto-instrumentations/bundler/esbuild.cjs +43 -5
  9. package/dist/auto-instrumentations/bundler/esbuild.mjs +2 -2
  10. package/dist/auto-instrumentations/bundler/next.cjs +43 -5
  11. package/dist/auto-instrumentations/bundler/next.mjs +3 -3
  12. package/dist/auto-instrumentations/bundler/rollup.cjs +43 -5
  13. package/dist/auto-instrumentations/bundler/rollup.mjs +2 -2
  14. package/dist/auto-instrumentations/bundler/vite.cjs +43 -5
  15. package/dist/auto-instrumentations/bundler/vite.mjs +2 -2
  16. package/dist/auto-instrumentations/bundler/webpack-loader.cjs +43 -5
  17. package/dist/auto-instrumentations/bundler/webpack.cjs +43 -5
  18. package/dist/auto-instrumentations/bundler/webpack.mjs +3 -3
  19. package/dist/auto-instrumentations/{chunk-NP7V4XB2.mjs → chunk-AOIYCVEL.mjs} +32 -2
  20. package/dist/auto-instrumentations/{chunk-26PKVUKB.mjs → chunk-DKTGDNA7.mjs} +12 -4
  21. package/dist/auto-instrumentations/{chunk-HD35AM3M.mjs → chunk-OMCZ3MV2.mjs} +1 -1
  22. package/dist/auto-instrumentations/hook.mjs +952 -133
  23. package/dist/auto-instrumentations/index.cjs +32 -2
  24. package/dist/auto-instrumentations/index.mjs +1 -1
  25. package/dist/browser.d.mts +760 -22
  26. package/dist/browser.d.ts +760 -22
  27. package/dist/browser.js +1661 -848
  28. package/dist/browser.mjs +1661 -848
  29. package/dist/{chunk-BBE7SNRV.js → chunk-6Z5S7VOU.js} +143 -23
  30. package/dist/{chunk-ZHUHZWFY.mjs → chunk-7FA6VP2S.mjs} +140 -20
  31. package/dist/{chunk-UPFNQCGB.mjs → chunk-M6XPNJC4.mjs} +1144 -931
  32. package/dist/{chunk-OBBWQW6K.js → chunk-XLLGRXGR.js} +2335 -2122
  33. package/dist/cli.js +5134 -1090
  34. package/dist/edge-light.js +1661 -848
  35. package/dist/edge-light.mjs +1661 -848
  36. package/dist/index.d.mts +760 -22
  37. package/dist/index.d.ts +760 -22
  38. package/dist/index.js +1054 -567
  39. package/dist/index.mjs +514 -27
  40. package/dist/instrumentation/index.d.mts +548 -9
  41. package/dist/instrumentation/index.d.ts +548 -9
  42. package/dist/instrumentation/index.js +1975 -765
  43. package/dist/instrumentation/index.mjs +1975 -765
  44. package/dist/vitest-evals-reporter.js +16 -16
  45. package/dist/vitest-evals-reporter.mjs +2 -2
  46. package/dist/workerd.js +1661 -848
  47. package/dist/workerd.mjs +1661 -848
  48. package/package.json +1 -1
  49. package/util/dist/index.d.mts +42 -0
  50. package/util/dist/index.d.ts +42 -0
package/dist/index.mjs CHANGED
@@ -74,7 +74,6 @@ import {
74
74
  currentLogger,
75
75
  currentSpan,
76
76
  currentWorkflowAgentWrapperSpan,
77
- debugLogger,
78
77
  deepCopyEvent,
79
78
  deserializePlainStringAsJSON,
80
79
  devNullWritableStream,
@@ -144,7 +143,7 @@ import {
144
143
  wrapMastraAgent,
145
144
  wrapTraced,
146
145
  zodToJsonSchema
147
- } from "./chunk-UPFNQCGB.mjs";
146
+ } from "./chunk-M6XPNJC4.mjs";
148
147
  import {
149
148
  INSTRUMENTATION_NAMES,
150
149
  __export,
@@ -156,6 +155,7 @@ import {
156
155
  cloudflareThinkChannels,
157
156
  cohereChannels,
158
157
  cursorSDKChannels,
158
+ debugLogger,
159
159
  genkitChannels,
160
160
  gitHubCopilotChannels,
161
161
  googleADKChannels,
@@ -177,7 +177,7 @@ import {
177
177
  strandsAgentSDKChannels,
178
178
  voyageAIChannels,
179
179
  withSpanInstrumentationName
180
- } from "./chunk-ZHUHZWFY.mjs";
180
+ } from "./chunk-7FA6VP2S.mjs";
181
181
 
182
182
  // src/exports.ts
183
183
  var exports_exports = {};
@@ -905,6 +905,13 @@ function wrapAISDK(aiSDK, options = {}) {
905
905
  switch (prop) {
906
906
  case "generateText":
907
907
  return wrapGenerateText(typedAISDK.generateText, options, typedAISDK);
908
+ case "generateImage":
909
+ case "experimental_generateImage":
910
+ return typeof original === "function" ? wrapGenerateImage(
911
+ original,
912
+ options,
913
+ typedAISDK
914
+ ) : original;
908
915
  case "streamText":
909
916
  return wrapStreamText(typedAISDK.streamText, options, typedAISDK);
910
917
  case "generateObject":
@@ -1141,6 +1148,32 @@ var wrapGenerateObject = (generateObject, options = {}, aiSDK) => {
1141
1148
  options
1142
1149
  );
1143
1150
  };
1151
+ var wrapGenerateImage = (generateImage, options = {}, aiSDK) => {
1152
+ return makeGenerateImageWrapper(generateImage, { aiSDK }, options);
1153
+ };
1154
+ var makeGenerateImageWrapper = (generateImage, contextOptions = {}, options = {}) => {
1155
+ const wrapper = async function(allParams) {
1156
+ const { span_info, ...params } = allParams;
1157
+ const tracedParams = { ...params };
1158
+ return aiSDKChannels.generateImage.tracePromise(
1159
+ () => generateImage(tracedParams),
1160
+ createAISDKChannelContext(tracedParams, {
1161
+ aiSDK: contextOptions.aiSDK,
1162
+ denyOutputPaths: options.denyOutputPaths,
1163
+ self: contextOptions.self,
1164
+ span_info: mergeSpanInfo(span_info, {
1165
+ name: "generateImage",
1166
+ spanType: contextOptions.spanType
1167
+ })
1168
+ })
1169
+ );
1170
+ };
1171
+ Object.defineProperty(wrapper, "name", {
1172
+ value: "generateImage",
1173
+ writable: false
1174
+ });
1175
+ return wrapper;
1176
+ };
1144
1177
  var makeEmbedWrapper = (channel, name, embed, contextOptions = {}, options = {}) => {
1145
1178
  const wrapper = async function(allParams) {
1146
1179
  const { span_info, ...params } = allParams;
@@ -1891,7 +1924,7 @@ function braintrustEveHook(options) {
1891
1924
  }
1892
1925
  };
1893
1926
  }
1894
- function braintrustEveInstrumentation(options) {
1927
+ function createLegacyEveInstrumentation(options) {
1895
1928
  const state = options.defineState(EVE_TRACE_STATE_KEY, emptyEveTraceState);
1896
1929
  return {
1897
1930
  events: {
@@ -3279,8 +3312,10 @@ function capturedModelInput(modelInput) {
3279
3312
  const value = [];
3280
3313
  if (typeof instructions === "string") {
3281
3314
  value.push({ content: instructions, role: "system" });
3282
- } else if (instructions) {
3315
+ } else if (Array.isArray(instructions)) {
3283
3316
  value.push(...instructions.map(capturedEveModelMessage));
3317
+ } else if (instructions) {
3318
+ value.push(capturedEveModelMessage(instructions));
3284
3319
  }
3285
3320
  value.push(...messages.map(capturedEveModelMessage));
3286
3321
  try {
@@ -3524,6 +3559,464 @@ async function deterministicEveId(...parts) {
3524
3559
  return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20)}`;
3525
3560
  }
3526
3561
 
3562
+ // src/instrumentation/plugins/eve-provider.ts
3563
+ var MAX_EVE_PROVIDER_CACHE_ENTRIES = 1e4;
3564
+ function createEveInstrumentationProvider(options = {}) {
3565
+ const bridge = new EveProviderBridge(options.metadata);
3566
+ return {
3567
+ capture: "content",
3568
+ events: {
3569
+ "action.completed": (event, context) => bridge.handleActionTerminal(event, context),
3570
+ "action.failed": (event, context) => bridge.handleActionTerminal(event, context),
3571
+ "action.started": (event, context) => bridge.handleActionStarted(event, context),
3572
+ "model.call.completed": (event, context) => bridge.handleModelTerminal(event, context),
3573
+ "model.call.failed": (event, context) => bridge.handleModelTerminal(event, context),
3574
+ "model.call.started": (event, context) => bridge.handleModelStarted(event, context),
3575
+ "step.attempt.completed": (event) => bridge.handleStepAttemptTerminal(event),
3576
+ "step.attempt.failed": (event) => bridge.handleStepAttemptTerminal(event),
3577
+ "turn.cancelled": (event, context) => bridge.handleTurnTerminal(event, context),
3578
+ "turn.completed": (event, context) => bridge.handleTurnTerminal(event, context),
3579
+ "turn.failed": (event, context) => bridge.handleTurnTerminal(event, context),
3580
+ "turn.started": (event, context) => bridge.handleTurnStarted(event, context)
3581
+ },
3582
+ flush,
3583
+ setup: options.setup
3584
+ };
3585
+ }
3586
+ var EveProviderBridge = class {
3587
+ constructor(metadata) {
3588
+ this.metadata = metadata;
3589
+ }
3590
+ metadata;
3591
+ activeActions = /* @__PURE__ */ new Map();
3592
+ activeModels = /* @__PURE__ */ new Map();
3593
+ modelsByAttempt = /* @__PURE__ */ new Map();
3594
+ settledOperations = new LRUCache({
3595
+ max: MAX_EVE_PROVIDER_CACHE_ENTRIES
3596
+ });
3597
+ turns = new LRUCache({
3598
+ max: MAX_EVE_PROVIDER_CACHE_ENTRIES
3599
+ });
3600
+ async handleTurnStarted(event, context) {
3601
+ await this.contain("turn start", async () => {
3602
+ if (this.settledOperations.has(event.idempotencyKey)) {
3603
+ context.state.set({ skip: true });
3604
+ return;
3605
+ }
3606
+ const { rowId, spanId } = await generateEveIds(
3607
+ "turn",
3608
+ event.idempotencyKey
3609
+ );
3610
+ const key = turnKey2(event.sessionId, event.turnId);
3611
+ const stored = storedSpan(context);
3612
+ if (stored && "exported" in stored && stored.rootSpanId) {
3613
+ this.turns.set(key, { rootSpanId: stored.rootSpanId, spanId });
3614
+ return;
3615
+ }
3616
+ let rootSpanId = spanId;
3617
+ let parentSpanId;
3618
+ if (event.parentLineage) {
3619
+ const parentTurnKey = turnKey2(
3620
+ event.parentLineage.sessionId,
3621
+ event.parentLineage.turnId
3622
+ );
3623
+ const parentTurn = this.turns.get(parentTurnKey);
3624
+ rootSpanId = parentTurn?.rootSpanId ?? // Eve propagates the root session through every nested subagent. Use
3625
+ // it instead of the immediate parent session when no ancestor ran
3626
+ // in-process.
3627
+ (await generateEveIds(
3628
+ "turn",
3629
+ turnIdempotencyKey(
3630
+ event.rootSessionId,
3631
+ event.parentLineage.turnId
3632
+ )
3633
+ )).spanId;
3634
+ parentSpanId = (await generateEveIds(
3635
+ "subagent",
3636
+ actionIdempotencyKey(
3637
+ event.parentLineage.sessionId,
3638
+ event.parentLineage.turnId,
3639
+ event.parentLineage.callId
3640
+ )
3641
+ )).spanId;
3642
+ }
3643
+ const metadata = this.spanMetadata(event.sessionId);
3644
+ const span = await this.startSpan(
3645
+ context,
3646
+ {
3647
+ event: { id: rowId, metadata },
3648
+ name: "eve.turn",
3649
+ parentSpanIds: parentSpanId ? { rootSpanId, spanId: parentSpanId } : { parentSpanIds: [], rootSpanId },
3650
+ spanAttributes: { type: "task" /* TASK */ },
3651
+ spanId
3652
+ },
3653
+ rootSpanId
3654
+ );
3655
+ span?.log({ metadata });
3656
+ this.turns.set(key, {
3657
+ rootSpanId,
3658
+ spanId
3659
+ });
3660
+ });
3661
+ }
3662
+ async handleTurnTerminal(event, context) {
3663
+ await this.contain("turn terminal", async () => {
3664
+ if (this.settledOperations.has(event.idempotencyKey)) {
3665
+ context.state.set(void 0);
3666
+ return;
3667
+ }
3668
+ const error = event.type === "turn.failed" ? event.error : void 0;
3669
+ this.drainActionsForTurn(event.sessionId, event.turnId, error);
3670
+ const stored = storedSpan(context);
3671
+ if (stored && "exported" in stored) {
3672
+ updateSpan({
3673
+ exported: stored.exported,
3674
+ ...error !== void 0 ? { error } : {},
3675
+ metrics: { end: Date.now() / 1e3 }
3676
+ });
3677
+ }
3678
+ this.settledOperations.set(event.idempotencyKey, true);
3679
+ context.state.set(void 0);
3680
+ this.turns.delete(turnKey2(event.sessionId, event.turnId));
3681
+ });
3682
+ }
3683
+ async handleModelStarted(event, context) {
3684
+ await this.contain("model start", async () => {
3685
+ if (this.settledOperations.has(event.idempotencyKey)) {
3686
+ context.state.set({ skip: true });
3687
+ return;
3688
+ }
3689
+ const parent = await this.parentForScope(event.scope);
3690
+ const { rowId, spanId } = await generateEveIds(
3691
+ "step",
3692
+ event.idempotencyKey
3693
+ );
3694
+ const input = event.input ? capturedModelInput(event.input) : void 0;
3695
+ const metadata = {
3696
+ ...this.spanMetadata(event.scope.sessionId),
3697
+ model: event.model.modelId,
3698
+ provider: event.model.provider
3699
+ };
3700
+ const span = await this.startSpan(context, {
3701
+ event: {
3702
+ id: rowId,
3703
+ ...input !== void 0 ? { input } : {},
3704
+ metadata
3705
+ },
3706
+ name: "eve.step",
3707
+ parentSpanIds: parent,
3708
+ spanAttributes: { type: "llm" /* LLM */ },
3709
+ spanId
3710
+ });
3711
+ if (!span) return;
3712
+ span.log({ ...input !== void 0 ? { input } : {}, metadata });
3713
+ this.activeModels.set(event.idempotencyKey, {
3714
+ span,
3715
+ turnKey: turnKey2(event.scope.sessionId, event.scope.turnId)
3716
+ });
3717
+ const keys = this.modelsByAttempt.get(event.scope.attemptId) ?? /* @__PURE__ */ new Set();
3718
+ keys.add(event.idempotencyKey);
3719
+ this.modelsByAttempt.set(event.scope.attemptId, keys);
3720
+ });
3721
+ }
3722
+ async handleModelTerminal(event, context) {
3723
+ await this.contain("model terminal", async () => {
3724
+ if (this.settledOperations.has(event.idempotencyKey)) {
3725
+ context.state.set(void 0);
3726
+ return;
3727
+ }
3728
+ const active = this.activeModels.get(event.idempotencyKey);
3729
+ if (active) {
3730
+ const span = active.span;
3731
+ if (event.type === "model.call.failed") {
3732
+ if (event.error !== void 0) span.log({ error: event.error });
3733
+ } else {
3734
+ span.log({
3735
+ metrics: usageMetrics(event.usage),
3736
+ output: modelOutput(event)
3737
+ });
3738
+ }
3739
+ span.end();
3740
+ } else {
3741
+ const stored = storedSpan(context);
3742
+ if (stored && "exported" in stored) {
3743
+ updateSpan({
3744
+ exported: stored.exported,
3745
+ ...event.type === "model.call.failed" ? event.error !== void 0 ? { error: event.error } : {} : { output: modelOutput(event) },
3746
+ metrics: {
3747
+ ...event.type === "model.call.completed" ? usageMetrics(event.usage) : {},
3748
+ end: Date.now() / 1e3
3749
+ }
3750
+ });
3751
+ }
3752
+ }
3753
+ this.settledOperations.set(event.idempotencyKey, true);
3754
+ this.forgetModel(event.scope.attemptId, event.idempotencyKey);
3755
+ context.state.set(void 0);
3756
+ });
3757
+ }
3758
+ async handleActionStarted(event, context) {
3759
+ await this.contain("action start", async () => {
3760
+ if (event.kind === "load-skill" || this.settledOperations.has(event.idempotencyKey)) {
3761
+ context.state.set({ skip: true });
3762
+ return;
3763
+ }
3764
+ const parent = await this.parentForScope(event.scope);
3765
+ const { rowId, spanId } = await generateEveIds(
3766
+ event.kind === "subagent-call" ? "subagent" : "tool",
3767
+ event.idempotencyKey
3768
+ );
3769
+ const metadata = {
3770
+ ...this.spanMetadata(event.scope.sessionId),
3771
+ "eve.action_kind": event.kind
3772
+ };
3773
+ const span = await this.startSpan(context, {
3774
+ event: {
3775
+ id: rowId,
3776
+ ...event.input !== void 0 ? { input: event.input } : {},
3777
+ metadata
3778
+ },
3779
+ name: event.name,
3780
+ parentSpanIds: parent,
3781
+ spanAttributes: { type: "tool" /* TOOL */ },
3782
+ spanId
3783
+ });
3784
+ if (!span) return;
3785
+ span.log({
3786
+ ...event.input !== void 0 ? { input: event.input } : {},
3787
+ metadata
3788
+ });
3789
+ this.activeActions.set(event.idempotencyKey, {
3790
+ span,
3791
+ turnKey: turnKey2(event.scope.sessionId, event.scope.turnId)
3792
+ });
3793
+ });
3794
+ }
3795
+ async handleActionTerminal(event, context) {
3796
+ await this.contain("action terminal", async () => {
3797
+ if (this.settledOperations.has(event.idempotencyKey)) {
3798
+ context.state.set(void 0);
3799
+ return;
3800
+ }
3801
+ const stored = storedSpan(context);
3802
+ if (stored?.skip) {
3803
+ context.state.set(void 0);
3804
+ return;
3805
+ }
3806
+ const active = this.activeActions.get(event.idempotencyKey);
3807
+ const end = finiteTimestamp(event.acceptedAtMs) ?? Date.now();
3808
+ if (active) {
3809
+ const span = active.span;
3810
+ if (event.type === "action.failed") {
3811
+ span.log({
3812
+ error: event.error ?? new Error(event.errorCode ?? `Eve action ${event.outcome}`)
3813
+ });
3814
+ } else if (event.output.type === "error") {
3815
+ span.log({
3816
+ error: event.output.error ?? new Error("Eve action returned an error")
3817
+ });
3818
+ } else {
3819
+ span.log({ output: event.output.output });
3820
+ }
3821
+ span.end({ endTime: end / 1e3 });
3822
+ } else {
3823
+ const stored2 = storedSpan(context);
3824
+ if (stored2 && "exported" in stored2) {
3825
+ updateSpan({
3826
+ exported: stored2.exported,
3827
+ ...event.type === "action.failed" ? {
3828
+ error: event.error ?? new Error(event.errorCode ?? `Eve action ${event.outcome}`)
3829
+ } : event.output.type === "error" ? {
3830
+ error: event.output.error ?? new Error("Eve action returned an error")
3831
+ } : { output: event.output.output },
3832
+ metrics: { end: end / 1e3 }
3833
+ });
3834
+ }
3835
+ }
3836
+ this.settledOperations.set(event.idempotencyKey, true);
3837
+ this.activeActions.delete(event.idempotencyKey);
3838
+ context.state.set(void 0);
3839
+ });
3840
+ }
3841
+ handleStepAttemptTerminal(event) {
3842
+ void this.contain("step attempt terminal", () => {
3843
+ const keys = this.modelsByAttempt.get(event.scope.attemptId);
3844
+ if (!keys) return;
3845
+ for (const key of keys) {
3846
+ const active = this.activeModels.get(key);
3847
+ if (!active) continue;
3848
+ if (event.type === "step.attempt.failed" && event.error !== void 0) {
3849
+ active.span.log({ error: event.error });
3850
+ }
3851
+ active.span.end();
3852
+ this.activeModels.delete(key);
3853
+ }
3854
+ this.modelsByAttempt.delete(event.scope.attemptId);
3855
+ });
3856
+ }
3857
+ async parentForScope(scope) {
3858
+ const key = turnKey2(scope.sessionId, scope.turnId);
3859
+ const known = this.turns.get(key);
3860
+ if (known) {
3861
+ return { rootSpanId: known.rootSpanId, spanId: known.spanId };
3862
+ }
3863
+ const [{ spanId }, { spanId: rootSpanId }] = await Promise.all([
3864
+ generateEveIds("turn", turnIdempotencyKey(scope.sessionId, scope.turnId)),
3865
+ generateEveIds(
3866
+ "turn",
3867
+ turnIdempotencyKey(
3868
+ scope.rootSessionId ?? scope.sessionId,
3869
+ scope.turnId
3870
+ )
3871
+ )
3872
+ ]);
3873
+ return { rootSpanId, spanId };
3874
+ }
3875
+ async startSpan(context, args, rootSpanId) {
3876
+ const span = withCurrent(
3877
+ NOOP_SPAN,
3878
+ () => _internalStartSpanWithInitialMerge(
3879
+ withSpanInstrumentationName(args ?? {}, INSTRUMENTATION_NAMES.EVE)
3880
+ )
3881
+ );
3882
+ try {
3883
+ context.state.set({
3884
+ exported: await span.export(),
3885
+ ...rootSpanId ? { rootSpanId } : {}
3886
+ });
3887
+ } catch (error) {
3888
+ debugLogger.warn("Error exporting Eve provider span:", error);
3889
+ }
3890
+ return span;
3891
+ }
3892
+ drainActionsForTurn(sessionId, turnId, error) {
3893
+ const key = turnKey2(sessionId, turnId);
3894
+ for (const [idempotencyKey, active] of this.activeActions) {
3895
+ if (active.turnKey !== key) continue;
3896
+ if (error !== void 0) active.span.log({ error });
3897
+ active.span.end();
3898
+ this.activeActions.delete(idempotencyKey);
3899
+ }
3900
+ }
3901
+ forgetModel(attemptId, idempotencyKey) {
3902
+ this.activeModels.delete(idempotencyKey);
3903
+ const keys = this.modelsByAttempt.get(attemptId);
3904
+ keys?.delete(idempotencyKey);
3905
+ if (keys?.size === 0) this.modelsByAttempt.delete(attemptId);
3906
+ }
3907
+ spanMetadata(sessionId) {
3908
+ return {
3909
+ ...this.metadata ?? {},
3910
+ "eve.session_id": sessionId
3911
+ };
3912
+ }
3913
+ async contain(operation, fn) {
3914
+ try {
3915
+ await fn();
3916
+ } catch (error) {
3917
+ debugLogger.warn(`Error in Eve provider ${operation}:`, error);
3918
+ }
3919
+ }
3920
+ };
3921
+ function storedSpan(context) {
3922
+ const value = context.state.get();
3923
+ if (!isObject(value)) return void 0;
3924
+ if (value["skip"] === true) return { skip: true };
3925
+ return typeof value["exported"] === "string" ? {
3926
+ exported: value["exported"],
3927
+ ...typeof value["rootSpanId"] === "string" && value["rootSpanId"].length > 0 ? { rootSpanId: value["rootSpanId"] } : {}
3928
+ } : void 0;
3929
+ }
3930
+ function modelOutput(event) {
3931
+ const content = event.content ?? [];
3932
+ let text = "";
3933
+ const reasoning = [];
3934
+ const toolCalls2 = [];
3935
+ for (const part of content) {
3936
+ if (part.type === "text") {
3937
+ text += part.text;
3938
+ } else if (part.type === "reasoning" && part.text.trim().length > 0) {
3939
+ reasoning.push({ content: part.text });
3940
+ } else if (part.type === "tool-call") {
3941
+ toolCalls2.push({
3942
+ function: {
3943
+ arguments: safeJsonStringify(part.input),
3944
+ name: part.toolName
3945
+ },
3946
+ id: part.callId,
3947
+ type: "function"
3948
+ });
3949
+ }
3950
+ }
3951
+ return [
3952
+ {
3953
+ finish_reason: normalizeFinishReason2(event.finishReason),
3954
+ index: 0,
3955
+ message: {
3956
+ content: text || null,
3957
+ ...reasoning.length > 0 ? { reasoning } : {},
3958
+ role: "assistant",
3959
+ ...toolCalls2.length > 0 ? { tool_calls: toolCalls2 } : {}
3960
+ }
3961
+ }
3962
+ ];
3963
+ }
3964
+ function usageMetrics(usage) {
3965
+ const promptTokens = nonNegativeNumber(usage.inputTokens);
3966
+ const completionTokens = nonNegativeNumber(usage.outputTokens);
3967
+ const cachedTokens = nonNegativeNumber(
3968
+ usage.inputTokenDetails?.cacheReadTokens
3969
+ );
3970
+ const cacheCreationTokens = nonNegativeNumber(
3971
+ usage.inputTokenDetails?.cacheWriteTokens
3972
+ );
3973
+ return {
3974
+ ...promptTokens !== void 0 ? { prompt_tokens: promptTokens } : {},
3975
+ ...completionTokens !== void 0 ? { completion_tokens: completionTokens } : {},
3976
+ ...promptTokens !== void 0 && completionTokens !== void 0 ? { tokens: promptTokens + completionTokens } : {},
3977
+ ...cachedTokens !== void 0 ? { prompt_cached_tokens: cachedTokens } : {},
3978
+ ...cacheCreationTokens !== void 0 ? { prompt_cache_creation_tokens: cacheCreationTokens } : {}
3979
+ };
3980
+ }
3981
+ function nonNegativeNumber(value) {
3982
+ return typeof value === "number" && Number.isFinite(value) && value >= 0 ? value : void 0;
3983
+ }
3984
+ function finiteTimestamp(value) {
3985
+ return typeof value === "number" && Number.isFinite(value) ? value : void 0;
3986
+ }
3987
+ function normalizeFinishReason2(value) {
3988
+ if (value === "content-filter") return "content_filter";
3989
+ if (value === "tool-calls") return "tool_calls";
3990
+ return value;
3991
+ }
3992
+ function safeJsonStringify(value) {
3993
+ try {
3994
+ return JSON.stringify(value) ?? "null";
3995
+ } catch {
3996
+ return "null";
3997
+ }
3998
+ }
3999
+ function turnKey2(sessionId, turnId) {
4000
+ return `${sessionId}:${turnId}`;
4001
+ }
4002
+ function turnIdempotencyKey(sessionId, turnId) {
4003
+ return `turn:${sessionId}:${turnId}`;
4004
+ }
4005
+ function actionIdempotencyKey(sessionId, turnId, callId) {
4006
+ return `action:${sessionId}:${turnId}:${callId}`;
4007
+ }
4008
+
4009
+ // src/instrumentation/plugins/eve-instrumentation.ts
4010
+ var EVE_INSTRUMENTATION_PROVIDER = /* @__PURE__ */ Symbol.for("eve.instrumentation.provider");
4011
+ function braintrustEveInstrumentation(options) {
4012
+ const definition = "defineState" in options ? createLegacyEveInstrumentation(options) : createEveInstrumentationProvider(options);
4013
+ const declaration = {
4014
+ ...definition,
4015
+ [EVE_INSTRUMENTATION_PROVIDER]: true
4016
+ };
4017
+ return declaration;
4018
+ }
4019
+
3527
4020
  // src/typed-instrumentation-helpers.ts
3528
4021
  var TypedApplyProxy = Proxy;
3529
4022
 
@@ -3705,11 +4198,11 @@ function wrapClaudeAgentQuery(queryFn, defaultThis) {
3705
4198
  }
3706
4199
  };
3707
4200
  const invocationTarget = thisArg === proxy || thisArg === void 0 ? defaultThis ?? thisArg : thisArg;
3708
- return claudeAgentSDKChannels.query.traceSync(
3709
- () => Reflect.apply(target, invocationTarget, [wrappedParams]),
3710
- // The channel carries no extra context fields, but the generated
3711
- // StartOf<> type for Record<string, never> is overly strict here.
3712
- { arguments: [wrappedParams] }
4201
+ return claudeAgentSDKChannels.query.invoke(
4202
+ target,
4203
+ invocationTarget,
4204
+ [wrappedParams],
4205
+ {}
3713
4206
  );
3714
4207
  }
3715
4208
  });
@@ -4324,13 +4817,11 @@ function wrapAgentInstance(agent) {
4324
4817
  if (prop === "stream" && typeof value === "function") {
4325
4818
  return function(args, options) {
4326
4819
  const callArgs = [args, options];
4327
- return strandsAgentSDKChannels.agentStream.traceSync(
4328
- () => Reflect.apply(value, target, callArgs),
4329
- {
4330
- agent: proxy,
4331
- arguments: callArgs,
4332
- self: proxy
4333
- }
4820
+ return strandsAgentSDKChannels.agentStream.invoke(
4821
+ value,
4822
+ target,
4823
+ callArgs,
4824
+ { agent: proxy }
4334
4825
  );
4335
4826
  };
4336
4827
  }
@@ -4361,12 +4852,12 @@ function wrapMultiAgentInstance(orchestrator, kind) {
4361
4852
  return function(input, options) {
4362
4853
  const callArgs = [input, options];
4363
4854
  const channel = kind === "graph" ? strandsAgentSDKChannels.graphStream : strandsAgentSDKChannels.swarmStream;
4364
- return channel.traceSync(
4365
- () => Reflect.apply(value, target, callArgs),
4855
+ return channel.invoke(
4856
+ value,
4857
+ target,
4858
+ callArgs,
4366
4859
  {
4367
- arguments: callArgs,
4368
- orchestrator: proxy,
4369
- self: proxy
4860
+ orchestrator: proxy
4370
4861
  }
4371
4862
  );
4372
4863
  };
@@ -6318,9 +6809,6 @@ var VitestContextManager = class {
6318
6809
  getCurrentContext() {
6319
6810
  return this.contextStorage.getStore();
6320
6811
  }
6321
- setContext(context) {
6322
- this.contextStorage.enterWith(context);
6323
- }
6324
6812
  runInContext(context, callback) {
6325
6813
  return this.contextStorage.run(context, callback);
6326
6814
  }
@@ -6665,8 +7153,7 @@ function wrapDescribe(originalDescribe, config, afterAll) {
6665
7153
  if (config.onProgress) {
6666
7154
  config.onProgress({ type: "suite_start", suiteName });
6667
7155
  }
6668
- contextManager.setContext(lazyContext);
6669
- factory();
7156
+ contextManager.runInContext(lazyContext, factory);
6670
7157
  if (afterAll) {
6671
7158
  afterAll(async () => {
6672
7159
  await flushExperimentWithSync(context, config);