braintrust 3.23.1 → 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.
Files changed (51) hide show
  1. package/dev/dist/index.d.mts +11 -0
  2. package/dev/dist/index.d.ts +11 -0
  3. package/dev/dist/index.js +2881 -1518
  4. package/dev/dist/index.mjs +2298 -935
  5. package/dist/apply-auto-instrumentation.js +218 -195
  6. package/dist/apply-auto-instrumentation.mjs +30 -7
  7. package/dist/auto-instrumentations/bundler/esbuild.cjs +618 -444
  8. package/dist/auto-instrumentations/bundler/esbuild.mjs +2 -2
  9. package/dist/auto-instrumentations/bundler/next.cjs +618 -444
  10. package/dist/auto-instrumentations/bundler/next.mjs +3 -3
  11. package/dist/auto-instrumentations/bundler/rollup.cjs +618 -444
  12. package/dist/auto-instrumentations/bundler/rollup.mjs +2 -2
  13. package/dist/auto-instrumentations/bundler/vite.cjs +618 -444
  14. package/dist/auto-instrumentations/bundler/vite.d.mts +1 -1
  15. package/dist/auto-instrumentations/bundler/vite.d.ts +1 -1
  16. package/dist/auto-instrumentations/bundler/vite.mjs +2 -2
  17. package/dist/auto-instrumentations/bundler/webpack-loader.cjs +618 -444
  18. package/dist/auto-instrumentations/bundler/webpack.cjs +618 -444
  19. package/dist/auto-instrumentations/bundler/webpack.mjs +3 -3
  20. package/dist/auto-instrumentations/{chunk-KIMLYPRW.mjs → chunk-7P6563SW.mjs} +1 -1
  21. package/dist/auto-instrumentations/{chunk-YXLNSAMJ.mjs → chunk-CZ24KNHT.mjs} +591 -424
  22. package/dist/auto-instrumentations/{chunk-EXY7QCJD.mjs → chunk-JPVCUKTY.mjs} +30 -21
  23. package/dist/auto-instrumentations/hook.mjs +778 -457
  24. package/dist/auto-instrumentations/index.cjs +590 -424
  25. package/dist/auto-instrumentations/index.mjs +1 -1
  26. package/dist/browser.d.mts +138 -9
  27. package/dist/browser.d.ts +138 -9
  28. package/dist/browser.js +2700 -1011
  29. package/dist/browser.mjs +2700 -1011
  30. package/dist/{chunk-RBXD2KYN.mjs → chunk-2SANLSWX.mjs} +1577 -513
  31. package/dist/{chunk-36IPYKMG.mjs → chunk-ABR2QWDP.mjs} +704 -438
  32. package/dist/{chunk-B6ZQIAK3.js → chunk-GSIDVFE6.js} +2361 -1297
  33. package/dist/{chunk-CDIKAHDZ.js → chunk-P25IOOU4.js} +706 -440
  34. package/dist/cli.js +2307 -940
  35. package/dist/edge-light.js +2700 -1011
  36. package/dist/edge-light.mjs +2700 -1011
  37. package/dist/index.d.mts +138 -9
  38. package/dist/index.d.ts +138 -9
  39. package/dist/index.js +955 -551
  40. package/dist/index.mjs +493 -89
  41. package/dist/instrumentation/index.d.mts +135 -9
  42. package/dist/instrumentation/index.d.ts +135 -9
  43. package/dist/instrumentation/index.js +2554 -1009
  44. package/dist/instrumentation/index.mjs +2554 -1009
  45. package/dist/vitest-evals-reporter.js +17 -16
  46. package/dist/vitest-evals-reporter.mjs +3 -2
  47. package/dist/workerd.js +2700 -1011
  48. package/dist/workerd.mjs +2700 -1011
  49. package/package.json +4 -4
  50. package/util/dist/index.js +4 -0
  51. package/util/dist/index.mjs +4 -0
package/dist/index.mjs CHANGED
@@ -139,8 +139,9 @@ import {
139
139
  wrapMastraAgent,
140
140
  wrapTraced,
141
141
  zodToJsonSchema
142
- } from "./chunk-RBXD2KYN.mjs";
142
+ } from "./chunk-2SANLSWX.mjs";
143
143
  import {
144
+ INSTRUMENTATION_NAMES,
144
145
  __export,
145
146
  aiSDKChannels,
146
147
  anthropicChannels,
@@ -153,6 +154,7 @@ import {
153
154
  googleADKChannels,
154
155
  googleGenAIChannels,
155
156
  groqChannels,
157
+ harnessAgentChannels,
156
158
  huggingFaceChannels,
157
159
  isomorph_default,
158
160
  langSmithChannels,
@@ -162,8 +164,9 @@ import {
162
164
  openRouterAgentChannels,
163
165
  openRouterChannels,
164
166
  piCodingAgentChannels,
165
- strandsAgentSDKChannels
166
- } from "./chunk-36IPYKMG.mjs";
167
+ strandsAgentSDKChannels,
168
+ withSpanInstrumentationName
169
+ } from "./chunk-ABR2QWDP.mjs";
167
170
 
168
171
  // src/exports.ts
169
172
  var exports_exports = {};
@@ -905,6 +908,26 @@ function wrapAISDK(aiSDK, options = {}) {
905
908
  }
906
909
  });
907
910
  }
911
+ function isHarnessAgentInstance(instance) {
912
+ try {
913
+ const visited = /* @__PURE__ */ new Set();
914
+ let prototype = Object.getPrototypeOf(instance);
915
+ while (prototype !== null && !visited.has(prototype)) {
916
+ visited.add(prototype);
917
+ const constructor = Object.getOwnPropertyDescriptor(
918
+ prototype,
919
+ "constructor"
920
+ )?.value;
921
+ const constructorName = typeof constructor === "function" ? Object.getOwnPropertyDescriptor(constructor, "name")?.value : void 0;
922
+ if (constructorName === "HarnessAgent") {
923
+ return true;
924
+ }
925
+ prototype = Object.getPrototypeOf(prototype);
926
+ }
927
+ } catch {
928
+ }
929
+ return false;
930
+ }
908
931
  var wrapAgentClass = (AgentClass, options = {}) => {
909
932
  const typedAgentClass = AgentClass;
910
933
  return new Proxy(typedAgentClass, {
@@ -914,9 +937,52 @@ var wrapAgentClass = (AgentClass, options = {}) => {
914
937
  args,
915
938
  newTarget
916
939
  );
940
+ const harnessAgent = isHarnessAgentInstance(instance) ? instance : null;
917
941
  return new Proxy(instance, {
918
942
  get(instanceTarget, prop, instanceReceiver) {
919
943
  const original = Reflect.get(instanceTarget, prop, instanceTarget);
944
+ if (harnessAgent && typeof original === "function") {
945
+ switch (prop) {
946
+ case "createSession":
947
+ return wrapHarnessAgentCreateSession(
948
+ // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
949
+ original,
950
+ harnessAgent
951
+ );
952
+ case "generate":
953
+ return wrapHarnessAgentGenerate(
954
+ original,
955
+ harnessAgent,
956
+ harnessAgentChannels.generate,
957
+ "HarnessAgent.generate",
958
+ options
959
+ );
960
+ case "stream":
961
+ return wrapHarnessAgentStream(
962
+ original,
963
+ harnessAgent,
964
+ harnessAgentChannels.stream,
965
+ "HarnessAgent.stream",
966
+ options
967
+ );
968
+ case "continueGenerate":
969
+ return wrapHarnessAgentGenerate(
970
+ original,
971
+ harnessAgent,
972
+ harnessAgentChannels.continueGenerate,
973
+ "HarnessAgent.continueGenerate",
974
+ options
975
+ );
976
+ case "continueStream":
977
+ return wrapHarnessAgentStream(
978
+ original,
979
+ harnessAgent,
980
+ harnessAgentChannels.continueStream,
981
+ "HarnessAgent.continueStream",
982
+ options
983
+ );
984
+ }
985
+ }
920
986
  if (prop === "generate" && typeof original === "function" && instanceTarget.constructor.name !== "WorkflowAgent") {
921
987
  return wrapAgentGenerate(original, instanceTarget, options);
922
988
  }
@@ -932,6 +998,39 @@ var wrapAgentClass = (AgentClass, options = {}) => {
932
998
  }
933
999
  });
934
1000
  };
1001
+ var wrapHarnessAgentCreateSession = (createSession, instance) => {
1002
+ const wrapper = function(params) {
1003
+ return harnessAgentChannels.createSession.tracePromise(
1004
+ () => params === void 0 ? createSession.call(instance) : createSession.call(instance, params),
1005
+ createAISDKChannelContext(params ?? {}, { self: instance })
1006
+ );
1007
+ };
1008
+ Object.defineProperty(wrapper, "name", {
1009
+ value: "HarnessAgent.createSession",
1010
+ writable: false
1011
+ });
1012
+ return wrapper;
1013
+ };
1014
+ var wrapHarnessAgentGenerate = (generate, instance, channel, name, options) => makeGenerateTextWrapper(
1015
+ channel,
1016
+ name,
1017
+ generate.bind(instance),
1018
+ {
1019
+ self: instance,
1020
+ spanType: "task" /* TASK */
1021
+ },
1022
+ options
1023
+ );
1024
+ var wrapHarnessAgentStream = (stream, instance, channel, name, options) => makeStreamWrapper(
1025
+ channel,
1026
+ name,
1027
+ stream.bind(instance),
1028
+ {
1029
+ self: instance,
1030
+ spanType: "task" /* TASK */
1031
+ },
1032
+ options
1033
+ );
935
1034
  var wrapAgentGenerate = (generate, instance, options = {}) => {
936
1035
  const defaultName = `${instance.constructor.name}.generate`;
937
1036
  return async (params) => makeGenerateTextWrapper(
@@ -1158,6 +1257,7 @@ var BraintrustLanguageModelWrapper = class {
1158
1257
  this.supportsUrl = (url) => this.model.supportsUrl(url);
1159
1258
  }
1160
1259
  }
1260
+ model;
1161
1261
  supportsUrl;
1162
1262
  get specificationVersion() {
1163
1263
  return this.model.specificationVersion;
@@ -1180,12 +1280,17 @@ var BraintrustLanguageModelWrapper = class {
1180
1280
  // For the first cut, do not support custom span_info arguments. We can
1181
1281
  // propagate those via async local storage
1182
1282
  async doGenerate(options) {
1183
- const span = startSpan({
1184
- name: "Chat Completion",
1185
- spanAttributes: {
1186
- type: "llm"
1187
- }
1188
- });
1283
+ const span = startSpan(
1284
+ withSpanInstrumentationName(
1285
+ {
1286
+ name: "Chat Completion",
1287
+ spanAttributes: {
1288
+ type: "llm"
1289
+ }
1290
+ },
1291
+ INSTRUMENTATION_NAMES.AI_SDK
1292
+ )
1293
+ );
1189
1294
  const { prompt, mode, ...rest } = options;
1190
1295
  const startTime = getCurrentUnixTimestamp();
1191
1296
  try {
@@ -1216,12 +1321,17 @@ var BraintrustLanguageModelWrapper = class {
1216
1321
  async doStream(options) {
1217
1322
  const { prompt, mode, ...rest } = options;
1218
1323
  const startTime = getCurrentUnixTimestamp();
1219
- const span = startSpan({
1220
- name: "Chat Completion",
1221
- spanAttributes: {
1222
- type: "llm"
1223
- }
1224
- });
1324
+ const span = startSpan(
1325
+ withSpanInstrumentationName(
1326
+ {
1327
+ name: "Chat Completion",
1328
+ spanAttributes: {
1329
+ type: "llm"
1330
+ }
1331
+ },
1332
+ INSTRUMENTATION_NAMES.AI_SDK
1333
+ )
1334
+ );
1225
1335
  span.log({
1226
1336
  input: postProcessPrompt(prompt),
1227
1337
  metadata: {
@@ -1576,7 +1686,9 @@ function BraintrustMiddleware(config = {}) {
1576
1686
  }
1577
1687
  }
1578
1688
  };
1579
- const span = startSpan(spanArgs);
1689
+ const span = startSpan(
1690
+ withSpanInstrumentationName(spanArgs, INSTRUMENTATION_NAMES.AI_SDK)
1691
+ );
1580
1692
  try {
1581
1693
  const result = await doGenerate();
1582
1694
  const metadata = {};
@@ -1638,7 +1750,9 @@ function BraintrustMiddleware(config = {}) {
1638
1750
  }
1639
1751
  }
1640
1752
  };
1641
- const span = startSpan(spanArgs);
1753
+ const span = startSpan(
1754
+ withSpanInstrumentationName(spanArgs, INSTRUMENTATION_NAMES.AI_SDK)
1755
+ );
1642
1756
  try {
1643
1757
  const { stream, ...rest } = await doStream();
1644
1758
  const textChunks = [];
@@ -1740,6 +1854,7 @@ function BraintrustMiddleware(config = {}) {
1740
1854
  var EVE_TRACE_STATE_KEY = "braintrust.eve.tracing";
1741
1855
  var MAX_EVE_CACHE_ENTRIES = 1e4;
1742
1856
  var MAX_STORED_LLM_INPUTS = 100;
1857
+ var MAX_STORED_REASONING_BLOCKS = 100;
1743
1858
  var MAX_STORED_SPAN_REFERENCES = 1e4;
1744
1859
  var MAX_STORED_STEP_STARTS = 1e4;
1745
1860
  function braintrustEveHook(options) {
@@ -1778,6 +1893,7 @@ var ResumedEveSpan = class {
1778
1893
  this.reference = reference;
1779
1894
  this.endTime = reference.endTime;
1780
1895
  }
1896
+ reference;
1781
1897
  endTime;
1782
1898
  get rootSpanId() {
1783
1899
  return this.reference.rootSpanId;
@@ -1810,6 +1926,7 @@ var EveBridge = class {
1810
1926
  constructor(state) {
1811
1927
  this.state = state;
1812
1928
  }
1929
+ state;
1813
1930
  eventQueuesBySession = /* @__PURE__ */ new Map();
1814
1931
  completedToolKeys = new LRUCache({
1815
1932
  max: MAX_EVE_CACHE_ENTRIES
@@ -1842,7 +1959,12 @@ var EveBridge = class {
1842
1959
  };
1843
1960
  const span = withCurrent(
1844
1961
  NOOP_SPAN,
1845
- () => _internalStartSpanWithInitialMerge({ ...args, startTime })
1962
+ () => _internalStartSpanWithInitialMerge(
1963
+ withSpanInstrumentationName(
1964
+ { ...args, startTime },
1965
+ INSTRUMENTATION_NAMES.EVE
1966
+ )
1967
+ )
1846
1968
  );
1847
1969
  if (typeof rowId !== "string") {
1848
1970
  return span;
@@ -1988,6 +2110,9 @@ var EveBridge = class {
1988
2110
  case "step.started":
1989
2111
  await this.handleStepStarted(event, ctx, hookMetadata);
1990
2112
  return true;
2113
+ case "reasoning.completed":
2114
+ this.handleReasoningCompleted(event, ctx);
2115
+ return true;
1991
2116
  case "message.completed":
1992
2117
  this.handleMessageCompleted(event, ctx);
1993
2118
  return true;
@@ -2079,6 +2204,7 @@ var EveBridge = class {
2079
2204
  key,
2080
2205
  metadata,
2081
2206
  metrics: {},
2207
+ sessionId,
2082
2208
  span,
2083
2209
  stepsByIndex: /* @__PURE__ */ new Map(),
2084
2210
  turnId: event.data.turnId
@@ -2109,6 +2235,12 @@ var EveBridge = class {
2109
2235
  const endTime = eventTime(event);
2110
2236
  existing.span.end(endTime === void 0 ? void 0 : { endTime });
2111
2237
  this.markStepEnded(event.data.turnId, event.data.stepIndex);
2238
+ clearStoredEveReasoning(
2239
+ this.state,
2240
+ sessionId,
2241
+ event.data.turnId,
2242
+ event.data.stepIndex
2243
+ );
2112
2244
  }
2113
2245
  const stepOrdinal = this.stepOrdinal(event);
2114
2246
  const metadata = { ...turn.metadata };
@@ -2118,6 +2250,13 @@ var EveBridge = class {
2118
2250
  event.data.turnId,
2119
2251
  event.data.stepIndex
2120
2252
  );
2253
+ const reasoning = readStoredEveReasoning(
2254
+ this.state,
2255
+ sessionId,
2256
+ event.data.turnId,
2257
+ event.data.stepIndex
2258
+ );
2259
+ const output = mergeEveReasoning(void 0, reasoning);
2121
2260
  const { rowId: eventId, spanId } = await generateEveIds(
2122
2261
  "step",
2123
2262
  sessionId,
@@ -2135,32 +2274,52 @@ var EveBridge = class {
2135
2274
  spanId,
2136
2275
  startTime: eventTime(event)
2137
2276
  });
2138
- span.log({ ...input !== void 0 ? { input } : {}, metadata });
2277
+ span.log({
2278
+ ...input !== void 0 ? { input } : {},
2279
+ metadata
2280
+ });
2139
2281
  turn.stepsByIndex.set(event.data.stepIndex, {
2140
2282
  ...input !== void 0 ? { input } : {},
2141
2283
  metadata,
2142
2284
  metrics: {},
2285
+ ...output !== void 0 ? { output } : {},
2286
+ reasoning,
2143
2287
  span
2144
2288
  });
2145
2289
  }
2290
+ handleReasoningCompleted(event, ctx) {
2291
+ const sessionId = sessionIdFromContext(ctx);
2292
+ if (!sessionId) {
2293
+ return;
2294
+ }
2295
+ const reasoning = storeEveReasoning(this.state, sessionId, event);
2296
+ const step = this.stepForEvent(event, ctx);
2297
+ if (step) {
2298
+ step.reasoning = reasoning;
2299
+ step.output = mergeEveReasoning(step.output, reasoning);
2300
+ }
2301
+ }
2146
2302
  handleMessageCompleted(event, ctx) {
2147
2303
  const step = this.stepForEvent(event, ctx);
2148
2304
  if (!step) {
2149
2305
  return;
2150
2306
  }
2151
- const existingMessage = Array.isArray(step.output) && isObject(step.output[0]) ? step.output[0].message : void 0;
2307
+ const existingMessage = eveOutputMessage(step.output);
2152
2308
  const existingToolCalls = isObject(existingMessage) ? existingMessage.tool_calls : void 0;
2153
- step.output = [
2154
- {
2155
- finish_reason: normalizedFinishReason(event.data.finishReason),
2156
- index: 0,
2157
- message: {
2158
- content: event.data.message,
2159
- role: "assistant",
2160
- ...Array.isArray(existingToolCalls) ? { tool_calls: existingToolCalls } : {}
2309
+ step.output = mergeEveReasoning(
2310
+ [
2311
+ {
2312
+ finish_reason: normalizedFinishReason(event.data.finishReason),
2313
+ index: 0,
2314
+ message: {
2315
+ content: event.data.message,
2316
+ role: "assistant",
2317
+ ...Array.isArray(existingToolCalls) ? { tool_calls: existingToolCalls } : {}
2318
+ }
2161
2319
  }
2162
- }
2163
- ];
2320
+ ],
2321
+ step.reasoning
2322
+ );
2164
2323
  const turn = this.turnForEvent(event, ctx);
2165
2324
  if (turn && event.data.finishReason !== "tool-calls") {
2166
2325
  turn.output = event.data.message;
@@ -2169,16 +2328,19 @@ var EveBridge = class {
2169
2328
  handleResultCompleted(event, ctx) {
2170
2329
  const step = this.stepForEvent(event, ctx);
2171
2330
  if (step) {
2172
- step.output = [
2173
- {
2174
- finish_reason: "stop",
2175
- index: 0,
2176
- message: {
2177
- content: event.data.result,
2178
- role: "assistant"
2331
+ step.output = mergeEveReasoning(
2332
+ [
2333
+ {
2334
+ finish_reason: "stop",
2335
+ index: 0,
2336
+ message: {
2337
+ content: event.data.result,
2338
+ role: "assistant"
2339
+ }
2179
2340
  }
2180
- }
2181
- ];
2341
+ ],
2342
+ step.reasoning
2343
+ );
2182
2344
  }
2183
2345
  const turn = this.turnForEvent(event, ctx);
2184
2346
  if (turn) {
@@ -2228,18 +2390,20 @@ var EveBridge = class {
2228
2390
  type: "function"
2229
2391
  });
2230
2392
  }
2231
- step.output = [
2232
- {
2233
- finish_reason: "tool_calls",
2234
- index: 0,
2235
- message: {
2236
- content: null,
2237
- role: "assistant",
2238
- tool_calls: [...toolCallsById.values()]
2393
+ step.output = mergeEveReasoning(
2394
+ [
2395
+ {
2396
+ finish_reason: "tool_calls",
2397
+ index: 0,
2398
+ message: {
2399
+ content: null,
2400
+ role: "assistant",
2401
+ tool_calls: [...toolCallsById.values()]
2402
+ }
2239
2403
  }
2240
- }
2241
- ];
2242
- step.span.log({ metadata: step.metadata, output: step.output });
2404
+ ],
2405
+ step.reasoning
2406
+ );
2243
2407
  }
2244
2408
  async handleActionResult(event, ctx, hookMetadata) {
2245
2409
  if (isToolResult(event.data.result)) {
@@ -2417,6 +2581,7 @@ var EveBridge = class {
2417
2581
  ...costUsd !== void 0 ? { estimated_cost: costUsd } : {}
2418
2582
  };
2419
2583
  step.metrics = { ...step.metrics, ...metrics };
2584
+ const sessionId = sessionIdFromContext(ctx);
2420
2585
  if (Array.isArray(step.output) && isObject(step.output[0])) {
2421
2586
  const finishReason = step.output[0].finish_reason;
2422
2587
  if (typeof finishReason !== "string") {
@@ -2441,6 +2606,14 @@ var EveBridge = class {
2441
2606
  turn.stepsByIndex.delete(event.data.stepIndex);
2442
2607
  }
2443
2608
  this.markStepEnded(event.data.turnId, event.data.stepIndex);
2609
+ if (sessionId) {
2610
+ clearStoredEveReasoning(
2611
+ this.state,
2612
+ sessionId,
2613
+ event.data.turnId,
2614
+ event.data.stepIndex
2615
+ );
2616
+ }
2444
2617
  }
2445
2618
  handleStepFailed(event, ctx) {
2446
2619
  const step = this.stepForEvent(event, ctx);
@@ -2458,6 +2631,15 @@ var EveBridge = class {
2458
2631
  const turn = this.turnForEvent(event, ctx);
2459
2632
  turn?.stepsByIndex.delete(event.data.stepIndex);
2460
2633
  this.markStepEnded(event.data.turnId, event.data.stepIndex);
2634
+ const sessionId = sessionIdFromContext(ctx);
2635
+ if (sessionId) {
2636
+ clearStoredEveReasoning(
2637
+ this.state,
2638
+ sessionId,
2639
+ event.data.turnId,
2640
+ event.data.stepIndex
2641
+ );
2642
+ }
2461
2643
  }
2462
2644
  handleTurnCompleted(event, ctx) {
2463
2645
  const turn = this.turnForEvent(event, ctx);
@@ -2555,6 +2737,7 @@ var EveBridge = class {
2555
2737
  key,
2556
2738
  metadata,
2557
2739
  metrics: {},
2740
+ sessionId,
2558
2741
  span,
2559
2742
  stepsByIndex: /* @__PURE__ */ new Map(),
2560
2743
  turnId: event.data.turnId
@@ -2817,6 +3000,9 @@ var EveBridge = class {
2817
3000
  const normalized = normalizeEveTraceState(current);
2818
3001
  return {
2819
3002
  ...normalized,
3003
+ reasoningBlocks: normalized.reasoningBlocks.filter(
3004
+ (entry) => !entry.key.startsWith(`${turn.sessionId}\0${turn.turnId}\0`)
3005
+ ),
2820
3006
  stepStarts: normalized.stepStarts.filter(
2821
3007
  (entry) => entry.turnId !== turn.turnId
2822
3008
  )
@@ -2856,6 +3042,7 @@ function emptyEveTraceState() {
2856
3042
  return {
2857
3043
  llmInputs: [],
2858
3044
  metadata: {},
3045
+ reasoningBlocks: [],
2859
3046
  spanReferences: [],
2860
3047
  stepStarts: []
2861
3048
  };
@@ -2906,6 +3093,21 @@ function normalizeEveTraceState(state) {
2906
3093
  const input = entry["input"];
2907
3094
  return typeof key === "string" && isCapturedModelInput(input) ? [{ input, key }] : [];
2908
3095
  }).slice(-MAX_STORED_LLM_INPUTS) : [];
3096
+ const reasoningBlocks = Array.isArray(state["reasoningBlocks"]) ? state["reasoningBlocks"].flatMap((entry) => {
3097
+ if (!isObject(entry)) {
3098
+ return [];
3099
+ }
3100
+ const content = entry["content"];
3101
+ const eventAt = entry["eventAt"];
3102
+ const key = entry["key"];
3103
+ return typeof content === "string" && (eventAt === void 0 || typeof eventAt === "string") && typeof key === "string" ? [
3104
+ {
3105
+ content,
3106
+ ...typeof eventAt === "string" ? { eventAt } : {},
3107
+ key
3108
+ }
3109
+ ] : [];
3110
+ }).slice(-MAX_STORED_REASONING_BLOCKS) : [];
2909
3111
  const stepStarts = Array.isArray(state["stepStarts"]) ? state["stepStarts"].flatMap((entry) => {
2910
3112
  if (!isObject(entry)) {
2911
3113
  return [];
@@ -2916,7 +3118,13 @@ function normalizeEveTraceState(state) {
2916
3118
  const turnId = entry["turnId"];
2917
3119
  return typeof ordinal === "number" && Number.isInteger(ordinal) && ordinal >= 0 && typeof open === "boolean" && typeof stepIndex === "number" && Number.isInteger(stepIndex) && typeof turnId === "string" ? [{ open, ordinal, stepIndex, turnId }] : [];
2918
3120
  }).slice(-MAX_STORED_STEP_STARTS) : [];
2919
- return { llmInputs, metadata: { ...metadata }, spanReferences, stepStarts };
3121
+ return {
3122
+ llmInputs,
3123
+ metadata: { ...metadata },
3124
+ reasoningBlocks,
3125
+ spanReferences,
3126
+ stepStarts
3127
+ };
2920
3128
  }
2921
3129
  function readEveTraceState(state) {
2922
3130
  try {
@@ -2925,23 +3133,85 @@ function readEveTraceState(state) {
2925
3133
  return emptyEveTraceState();
2926
3134
  }
2927
3135
  }
2928
- function captureEveModelInput(state, input) {
2929
- if (!isObject(input)) {
2930
- return;
2931
- }
2932
- const session = input["session"];
2933
- const turn = input["turn"];
2934
- const step = input["step"];
2935
- if (!isObject(session) || !isObject(turn) || !isObject(step)) {
2936
- return;
2937
- }
2938
- const sessionId = session["id"];
2939
- const turnId = turn["id"];
2940
- const stepIndex = step["index"];
2941
- if (typeof sessionId !== "string" || typeof turnId !== "string" || typeof stepIndex !== "number" || !Number.isInteger(stepIndex)) {
2942
- return;
3136
+ function storeEveReasoning(state, sessionId, event) {
3137
+ const eventAt = event.meta?.at;
3138
+ const key = llmInputKey(sessionId, event.data.turnId, event.data.stepIndex);
3139
+ let stored = [];
3140
+ state.update((current) => {
3141
+ const normalized = normalizeEveTraceState(current);
3142
+ const alreadyStored = normalized.reasoningBlocks.some(
3143
+ (entry) => entry.content === event.data.reasoning && entry.eventAt === eventAt && entry.key === key
3144
+ );
3145
+ const reasoningBlocks = alreadyStored ? normalized.reasoningBlocks : [
3146
+ ...normalized.reasoningBlocks,
3147
+ {
3148
+ content: event.data.reasoning,
3149
+ ...eventAt ? { eventAt } : {},
3150
+ key
3151
+ }
3152
+ ].slice(-MAX_STORED_REASONING_BLOCKS);
3153
+ stored = reasoningBlocks.flatMap(
3154
+ (entry) => entry.key === key ? [
3155
+ {
3156
+ content: entry.content,
3157
+ ...entry.eventAt ? { eventAt: entry.eventAt } : {}
3158
+ }
3159
+ ] : []
3160
+ );
3161
+ return alreadyStored ? normalized : { ...normalized, reasoningBlocks };
3162
+ });
3163
+ return stored;
3164
+ }
3165
+ function readStoredEveReasoning(state, sessionId, turnId, stepIndex) {
3166
+ const key = llmInputKey(sessionId, turnId, stepIndex);
3167
+ return readEveTraceState(state).reasoningBlocks.flatMap(
3168
+ (entry) => entry.key === key ? [
3169
+ {
3170
+ content: entry.content,
3171
+ ...entry.eventAt ? { eventAt: entry.eventAt } : {}
3172
+ }
3173
+ ] : []
3174
+ );
3175
+ }
3176
+ function clearStoredEveReasoning(state, sessionId, turnId, stepIndex) {
3177
+ const key = llmInputKey(sessionId, turnId, stepIndex);
3178
+ state.update((current) => {
3179
+ const normalized = normalizeEveTraceState(current);
3180
+ return {
3181
+ ...normalized,
3182
+ reasoningBlocks: normalized.reasoningBlocks.filter(
3183
+ (entry) => entry.key !== key
3184
+ )
3185
+ };
3186
+ });
3187
+ }
3188
+ function eveOutputMessage(output) {
3189
+ return Array.isArray(output) && isObject(output[0]) ? output[0]["message"] : void 0;
3190
+ }
3191
+ function mergeEveReasoning(output, reasoning) {
3192
+ if (reasoning.length === 0) {
3193
+ return output;
2943
3194
  }
2944
- const captured = capturedModelInput(input["modelInput"]);
3195
+ const choice = Array.isArray(output) && isObject(output[0]) ? output[0] : {};
3196
+ const message = isObject(choice["message"]) ? choice["message"] : {};
3197
+ return [
3198
+ {
3199
+ ...choice,
3200
+ index: typeof choice["index"] === "number" ? choice["index"] : 0,
3201
+ message: {
3202
+ ...message,
3203
+ content: "content" in message ? message["content"] : null,
3204
+ reasoning: reasoning.map((block) => ({ content: block.content })),
3205
+ role: typeof message["role"] === "string" ? message["role"] : "assistant"
3206
+ }
3207
+ }
3208
+ ];
3209
+ }
3210
+ function captureEveModelInput(state, input) {
3211
+ const sessionId = input.session.id;
3212
+ const turnId = input.turn.id;
3213
+ const stepIndex = input.step.index;
3214
+ const captured = capturedModelInput(input.modelInput);
2945
3215
  if (!captured) {
2946
3216
  return;
2947
3217
  }
@@ -2982,18 +3252,14 @@ function consumeCapturedEveModelInput(state, sessionId, turnId, stepIndex) {
2982
3252
  }
2983
3253
  }
2984
3254
  function capturedModelInput(modelInput) {
2985
- if (!isObject(modelInput)) {
2986
- return void 0;
2987
- }
2988
- const messages = modelInput["messages"];
2989
- if (!Array.isArray(messages)) {
2990
- return void 0;
2991
- }
2992
- const instructions = modelInput["instructions"];
2993
- const value = [
2994
- ...instructions !== void 0 ? [{ content: instructions, role: "system" }] : [],
2995
- ...messages
2996
- ];
3255
+ const { instructions, messages } = modelInput;
3256
+ const value = [];
3257
+ if (typeof instructions === "string") {
3258
+ value.push({ content: instructions, role: "system" });
3259
+ } else if (instructions) {
3260
+ value.push(...instructions.map(capturedEveModelMessage));
3261
+ }
3262
+ value.push(...messages.map(capturedEveModelMessage));
2997
3263
  try {
2998
3264
  const cloned = JSON.parse(JSON.stringify(value));
2999
3265
  if (!Array.isArray(cloned)) {
@@ -3004,8 +3270,123 @@ function capturedModelInput(modelInput) {
3004
3270
  return void 0;
3005
3271
  }
3006
3272
  }
3273
+ function capturedEveModelMessage(message) {
3274
+ const { content, role } = message;
3275
+ if (typeof content === "string") {
3276
+ return { content, role };
3277
+ }
3278
+ return { content: content.map(capturedEveModelContentPart), role };
3279
+ }
3280
+ function capturedEveModelContentPart(part) {
3281
+ switch (part.type) {
3282
+ case "text":
3283
+ case "reasoning":
3284
+ return { text: part.text, type: part.type };
3285
+ case "image":
3286
+ return {
3287
+ image: part.image,
3288
+ ...part.mediaType !== void 0 ? { mediaType: part.mediaType } : {},
3289
+ type: "image"
3290
+ };
3291
+ case "file":
3292
+ case "reasoning-file":
3293
+ return {
3294
+ data: part.data,
3295
+ ...part.type === "file" && part.filename !== void 0 ? { filename: part.filename } : {},
3296
+ mediaType: part.mediaType,
3297
+ type: part.type
3298
+ };
3299
+ case "custom":
3300
+ return {
3301
+ ..."kind" in part ? { kind: part.kind } : {},
3302
+ type: "custom"
3303
+ };
3304
+ case "tool-call":
3305
+ return {
3306
+ input: part.input,
3307
+ ...part.providerExecuted !== void 0 ? { providerExecuted: part.providerExecuted } : {},
3308
+ toolCallId: part.toolCallId,
3309
+ toolName: part.toolName,
3310
+ type: "tool-call"
3311
+ };
3312
+ case "tool-result": {
3313
+ const output = part.output;
3314
+ let capturedOutput;
3315
+ switch (output.type) {
3316
+ case "text":
3317
+ case "error-text":
3318
+ capturedOutput = { type: output.type, value: output.value };
3319
+ break;
3320
+ case "json":
3321
+ case "error-json":
3322
+ capturedOutput = { type: output.type, value: output.value };
3323
+ break;
3324
+ case "execution-denied":
3325
+ capturedOutput = {
3326
+ ...output.reason !== void 0 ? { reason: output.reason } : {},
3327
+ type: "execution-denied"
3328
+ };
3329
+ break;
3330
+ case "content":
3331
+ capturedOutput = {
3332
+ type: "content",
3333
+ value: output.value.map(capturedEveModelContentPart)
3334
+ };
3335
+ break;
3336
+ }
3337
+ return {
3338
+ output: capturedOutput,
3339
+ toolCallId: part.toolCallId,
3340
+ toolName: part.toolName,
3341
+ type: "tool-result"
3342
+ };
3343
+ }
3344
+ case "tool-approval-request":
3345
+ return {
3346
+ approvalId: part.approvalId,
3347
+ ...part.isAutomatic !== void 0 ? { isAutomatic: part.isAutomatic } : {},
3348
+ ...part.signature !== void 0 ? { signature: part.signature } : {},
3349
+ toolCallId: part.toolCallId,
3350
+ type: "tool-approval-request"
3351
+ };
3352
+ case "tool-approval-response":
3353
+ return {
3354
+ approvalId: part.approvalId,
3355
+ approved: part.approved,
3356
+ ...part.providerExecuted !== void 0 ? { providerExecuted: part.providerExecuted } : {},
3357
+ ...part.reason !== void 0 ? { reason: part.reason } : {},
3358
+ type: "tool-approval-response"
3359
+ };
3360
+ case "file-data":
3361
+ case "image-data":
3362
+ return {
3363
+ data: part.data,
3364
+ ...part.type === "file-data" && part.filename !== void 0 ? { filename: part.filename } : {},
3365
+ mediaType: part.mediaType,
3366
+ type: part.type
3367
+ };
3368
+ case "file-url":
3369
+ case "image-url":
3370
+ return {
3371
+ ...part.type === "file-url" && part.mediaType !== void 0 ? { mediaType: part.mediaType } : {},
3372
+ type: part.type,
3373
+ url: part.url
3374
+ };
3375
+ case "file-id":
3376
+ case "image-file-id":
3377
+ return { fileId: part.fileId, type: part.type };
3378
+ case "file-reference":
3379
+ case "image-file-reference":
3380
+ return {
3381
+ providerReference: part.providerReference,
3382
+ type: part.type
3383
+ };
3384
+ }
3385
+ }
3007
3386
  function isCapturedModelInput(input) {
3008
- return Array.isArray(input);
3387
+ return Array.isArray(input) && input.every(
3388
+ (message) => isObject(message) && (message["role"] === "system" || message["role"] === "user" || message["role"] === "assistant" || message["role"] === "tool") && (typeof message["content"] === "string" || Array.isArray(message["content"]) && message["content"].every(isObject))
3389
+ );
3009
3390
  }
3010
3391
  function llmInputKey(sessionId, turnId, stepIndex) {
3011
3392
  return `${sessionId}\0${turnId}\0${stepIndex}`;
@@ -6142,6 +6523,8 @@ var BaseNode = class {
6142
6523
  this.graph = graph;
6143
6524
  this.id = id;
6144
6525
  }
6526
+ graph;
6527
+ id;
6145
6528
  __type = "node";
6146
6529
  dependencies = [];
6147
6530
  addDependency(dependency) {
@@ -6175,6 +6558,7 @@ var PromptNode = class extends BaseNode {
6175
6558
  super(graph, id);
6176
6559
  this.prompt = prompt;
6177
6560
  }
6561
+ prompt;
6178
6562
  async build(context) {
6179
6563
  return {
6180
6564
  type: "function",
@@ -6187,6 +6571,7 @@ var GateNode = class extends BaseNode {
6187
6571
  super(graph, id);
6188
6572
  this.condition = condition;
6189
6573
  }
6574
+ condition;
6190
6575
  async build(context) {
6191
6576
  return {
6192
6577
  type: "gate",
@@ -6211,6 +6596,7 @@ var PromptTemplateNode = class extends BaseNode {
6211
6596
  super(graph, id);
6212
6597
  this.prompt = prompt;
6213
6598
  }
6599
+ prompt;
6214
6600
  async build(context) {
6215
6601
  return {
6216
6602
  type: "prompt_template",
@@ -6223,6 +6609,7 @@ var LiteralNode = class extends BaseNode {
6223
6609
  super(graph, id);
6224
6610
  this.value = value;
6225
6611
  }
6612
+ value;
6226
6613
  async build(context) {
6227
6614
  return {
6228
6615
  type: "literal",
@@ -7316,6 +7703,10 @@ var SpanFetcher = class _SpanFetcher extends ObjectFetcher {
7316
7703
  this._state = _state;
7317
7704
  this.spanTypeFilter = spanTypeFilter;
7318
7705
  }
7706
+ _objectId;
7707
+ rootSpanId;
7708
+ _state;
7709
+ spanTypeFilter;
7319
7710
  static buildFilter(rootSpanId, spanTypeFilter, includeScorers = false) {
7320
7711
  const children = [
7321
7712
  // Base filter: root_span_id = 'value'
@@ -7766,6 +8157,8 @@ var EvalResultWithSummary = class {
7766
8157
  this.summary = summary;
7767
8158
  this.results = results;
7768
8159
  }
8160
+ summary;
8161
+ results;
7769
8162
  /**
7770
8163
  * @deprecated Use `summary` instead.
7771
8164
  */
@@ -8483,26 +8876,30 @@ async function runEvaluatorInternal(experiment, evaluator, progressReporter, fil
8483
8876
  }
8484
8877
  }
8485
8878
  })();
8879
+ let timeoutId;
8880
+ let abortHandler;
8881
+ const cleanupCancellation = () => {
8882
+ if (timeoutId !== void 0) {
8883
+ clearTimeout(timeoutId);
8884
+ timeoutId = void 0;
8885
+ }
8886
+ if (abortHandler && evaluator.signal) {
8887
+ evaluator.signal.removeEventListener("abort", abortHandler);
8888
+ abortHandler = void 0;
8889
+ }
8890
+ };
8486
8891
  const cancel = async () => {
8487
8892
  await new Promise((_, reject2) => {
8488
8893
  if (cancelled) {
8489
8894
  reject2(new InternalAbortError("Evaluator already cancelled"));
8490
8895
  return;
8491
8896
  }
8492
- let timeoutId;
8493
- let abortHandler;
8494
8897
  const rejectOnce = (error) => {
8495
8898
  if (cancelled) {
8496
8899
  return;
8497
8900
  }
8498
8901
  cancelled = true;
8499
- if (timeoutId) {
8500
- clearTimeout(timeoutId);
8501
- timeoutId = void 0;
8502
- }
8503
- if (abortHandler && evaluator.signal) {
8504
- evaluator.signal.removeEventListener("abort", abortHandler);
8505
- }
8902
+ cleanupCancellation();
8506
8903
  reject2(error);
8507
8904
  };
8508
8905
  if (evaluator.timeout) {
@@ -8542,6 +8939,7 @@ async function runEvaluatorInternal(experiment, evaluator, progressReporter, fil
8542
8939
  }
8543
8940
  throw e;
8544
8941
  } finally {
8942
+ cleanupCancellation();
8545
8943
  if (!collectResults) {
8546
8944
  collectedResults.length = 0;
8547
8945
  }
@@ -9184,6 +9582,7 @@ var ToolBuilder = class {
9184
9582
  constructor(project) {
9185
9583
  this.project = project;
9186
9584
  }
9585
+ project;
9187
9586
  taskCounter = 0;
9188
9587
  // This type definition is just a catch all so that the implementation can be
9189
9588
  // less specific than the two more specific declarations above.
@@ -9214,6 +9613,7 @@ var ScorerBuilder = class {
9214
9613
  constructor(project) {
9215
9614
  this.project = project;
9216
9615
  }
9616
+ project;
9217
9617
  taskCounter = 0;
9218
9618
  create(opts) {
9219
9619
  this.taskCounter++;
@@ -9273,6 +9673,7 @@ var ClassifierBuilder = class {
9273
9673
  constructor(project) {
9274
9674
  this.project = project;
9275
9675
  }
9676
+ project;
9276
9677
  taskCounter = 0;
9277
9678
  create(opts) {
9278
9679
  this.taskCounter++;
@@ -9308,6 +9709,7 @@ var CodeFunction = class {
9308
9709
  throw new Error("parameters are required if return type is defined");
9309
9710
  }
9310
9711
  }
9712
+ project;
9311
9713
  handler;
9312
9714
  name;
9313
9715
  slug;
@@ -9395,6 +9797,7 @@ var PromptBuilder = class {
9395
9797
  constructor(project) {
9396
9798
  this.project = project;
9397
9799
  }
9800
+ project;
9398
9801
  create(opts) {
9399
9802
  const toolFunctions = [];
9400
9803
  const rawTools = [];
@@ -9471,6 +9874,7 @@ var ParametersBuilder = class {
9471
9874
  constructor(project) {
9472
9875
  this.project = project;
9473
9876
  }
9877
+ project;
9474
9878
  create(opts) {
9475
9879
  const slug = opts.slug ?? slugify(opts.name, { lower: true, strict: true });
9476
9880
  const codeParameters = new CodeParameters(this.project, {