@bitfab/sdk 0.43.1 → 0.44.1

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/dist/index.cjs CHANGED
@@ -42,7 +42,7 @@ var __version__, __packageName__;
42
42
  var init_version_generated = __esm({
43
43
  "src/version.generated.ts"() {
44
44
  "use strict";
45
- __version__ = "0.43.1";
45
+ __version__ = "0.44.1";
46
46
  __packageName__ = "@bitfab/sdk";
47
47
  }
48
48
  });
@@ -164,7 +164,7 @@ var init_compress = __esm({
164
164
  });
165
165
 
166
166
  // src/errors.ts
167
- var BitfabError;
167
+ var BitfabError, MixedTracingError;
168
168
  var init_errors = __esm({
169
169
  "src/errors.ts"() {
170
170
  "use strict";
@@ -177,6 +177,12 @@ var init_errors = __esm({
177
177
  this.name = "BitfabError";
178
178
  }
179
179
  };
180
+ MixedTracingError = class extends Error {
181
+ constructor(message) {
182
+ super(message);
183
+ this.name = "MixedTracingError";
184
+ }
185
+ };
180
186
  }
181
187
  });
182
188
 
@@ -2713,7 +2719,9 @@ async function processItem(httpClient, serverItem, fn, testRunId, mockStrategy,
2713
2719
  tokens: null,
2714
2720
  model: serverItem.originalModel ?? serverItem.model ?? null,
2715
2721
  dbSnapshotRef: dbSnapshotRef ?? null,
2716
- dbBranchTimings: dbBranchTimings ?? null
2722
+ dbBranchTimings: dbBranchTimings ?? null,
2723
+ traceOutline: null,
2724
+ originalTraceOutline: null
2717
2725
  };
2718
2726
  }
2719
2727
  try {
@@ -2797,7 +2805,9 @@ async function processItem(httpClient, serverItem, fn, testRunId, mockStrategy,
2797
2805
  tokens: null,
2798
2806
  model: originalModel,
2799
2807
  dbSnapshotRef: dbSnapshotRef ?? null,
2800
- dbBranchTimings: dbBranchTimings ?? null
2808
+ dbBranchTimings: dbBranchTimings ?? null,
2809
+ traceOutline: null,
2810
+ originalTraceOutline: null
2801
2811
  };
2802
2812
  }
2803
2813
  async function waitForReplayPersistence(httpClient, testRunId, replayedTraceIds) {
@@ -3052,7 +3062,9 @@ async function replay(httpClient, serviceUrl, traceFunctionKey, fn, options, reg
3052
3062
  tokens: item.tokens,
3053
3063
  model: item.model,
3054
3064
  dbSnapshotRef: item.dbSnapshotRef,
3055
- dbBranchTimings: item.dbBranchTimings
3065
+ dbBranchTimings: item.dbBranchTimings,
3066
+ traceOutline: item.traceOutline,
3067
+ originalTraceOutline: item.originalTraceOutline
3056
3068
  }
3057
3069
  });
3058
3070
  } catch {
@@ -3132,7 +3144,9 @@ async function replay(httpClient, serviceUrl, traceFunctionKey, fn, options, reg
3132
3144
  }
3133
3145
  if (mapped !== void 0) {
3134
3146
  item.tokens = replayTokens?.[mapped] ?? null;
3147
+ item.traceOutline = completeResult.traceOutlines?.[mapped] ?? null;
3135
3148
  }
3149
+ item.originalTraceOutline = completeResult.originalTraceOutlines?.[item.originalTraceId] ?? null;
3136
3150
  }
3137
3151
  if (completedCount > 0 && missing.length === completedCount) {
3138
3152
  const serverCount = completeResult.traceCount !== void 0 ? ` The server persisted ${completeResult.traceCount} trace(s) for this run.` : "";
@@ -3255,6 +3269,7 @@ __export(index_exports, {
3255
3269
  DatasetsClient: () => DatasetsClient,
3256
3270
  DbBranchReplayError: () => DbBranchReplayError,
3257
3271
  HttpClient: () => HttpClient,
3272
+ MixedTracingError: () => MixedTracingError,
3258
3273
  NO_MOCK_OVERRIDE: () => NO_MOCK_OVERRIDE,
3259
3274
  ReplayError: () => ReplayError,
3260
3275
  SUPPORTED_PROVIDERS: () => SUPPORTED_PROVIDERS,
@@ -4328,6 +4343,50 @@ async function runFunctionWithBaml(bamlSource, inputs, providers, envVars) {
4328
4343
  };
4329
4344
  }
4330
4345
 
4346
+ // src/captureSurface.ts
4347
+ init_errors();
4348
+ var DEFAULT_SURFACE = "opt-in";
4349
+ var SURFACE_API = {
4350
+ "opt-in": "withSpan()",
4351
+ "opt-out": "withTrace()"
4352
+ };
4353
+ var MIXED_SURFACE_REMEDY = {
4354
+ "opt-in": "Inside a withTrace/trace subtree, configure a discovered call with node()/withNode() instead, or trace this workflow with withSpan() only.",
4355
+ "opt-out": "Wrap the caller with withTrace()/trace() as well, or wrap this function with withSpan()."
4356
+ };
4357
+ function resolveSurface(requested, parentSurface) {
4358
+ if (requested === "neutral") {
4359
+ return void 0;
4360
+ }
4361
+ if (requested === "inherit") {
4362
+ return parentSurface ?? DEFAULT_SURFACE;
4363
+ }
4364
+ return requested;
4365
+ }
4366
+ function mixedTracingError(enteredApi, entered, enclosing, traceFunctionKey) {
4367
+ const subject = traceFunctionKey === void 0 ? "" : ` for "${traceFunctionKey}"`;
4368
+ return new MixedTracingError(
4369
+ `opt-in and opt-out tracing can't be mixed in one call stack: ${enteredApi} (${entered})${subject} was entered inside a ${SURFACE_API[enclosing]} call (${enclosing}). ${MIXED_SURFACE_REMEDY[entered]}`
4370
+ );
4371
+ }
4372
+ function assertSurfacesCompatible(requested, resolved, parentSurface, traceFunctionKey) {
4373
+ if (requested === "inherit" || requested === "neutral") {
4374
+ return;
4375
+ }
4376
+ if (resolved === void 0 || parentSurface === void 0) {
4377
+ return;
4378
+ }
4379
+ if (parentSurface === resolved) {
4380
+ return;
4381
+ }
4382
+ throw mixedTracingError(
4383
+ SURFACE_API[resolved],
4384
+ resolved,
4385
+ parentSurface,
4386
+ traceFunctionKey
4387
+ );
4388
+ }
4389
+
4331
4390
  // src/client.ts
4332
4391
  init_constants();
4333
4392
 
@@ -4474,6 +4533,7 @@ function buildSnapshotRef(config, sdkWallClockBeforeFn) {
4474
4533
  }
4475
4534
 
4476
4535
  // src/client.ts
4536
+ init_errors();
4477
4537
  init_http();
4478
4538
 
4479
4539
  // src/langgraph.ts
@@ -5863,18 +5923,6 @@ var BitfabVercelAiHandler = class {
5863
5923
 
5864
5924
  // src/client.ts
5865
5925
  init_warnOnce();
5866
- var MIXED_SURFACE_REMEDY = {
5867
- "opt-in": "Inside a withTrace/trace subtree, configure a discovered call with node()/withNode() instead, or trace this workflow with withSpan() only.",
5868
- "opt-out": "Wrap the caller with withTrace()/trace() as well, or wrap this function with withSpan()."
5869
- };
5870
- function warnMixedTracingSurfaces(traceFunctionKey, entered, enclosing) {
5871
- const enteredApi = entered === "opt-in" ? "withSpan()" : "withTrace()";
5872
- const enclosingApi = enclosing === "opt-in" ? "withSpan()" : "withTrace()";
5873
- warnOnce(
5874
- `mixed-tracing-surface:${traceFunctionKey}:${entered}`,
5875
- `opt-in and opt-out tracing are mixed in one call stack: ${enteredApi} (${entered}) for "${traceFunctionKey}" was entered inside a ${enclosingApi} call (${enclosing}). Both are recorded, and the spans nest as written. ${MIXED_SURFACE_REMEDY[entered]}`
5876
- );
5877
- }
5878
5926
  var activeTraceStates = /* @__PURE__ */ new Map();
5879
5927
  var asyncLocalStorage = null;
5880
5928
  var SPAN_CONTEXT_STORAGE_SYMBOL = /* @__PURE__ */ Symbol.for("bitfab.spanContextStorage");
@@ -5904,6 +5952,10 @@ function getSpanStack() {
5904
5952
  }
5905
5953
  return browserSpanStack;
5906
5954
  }
5955
+ function enclosingSurface() {
5956
+ const stack = getSpanStack();
5957
+ return stack[stack.length - 1]?.surface;
5958
+ }
5907
5959
  function runWithSpanStack(stack, fn) {
5908
5960
  if (asyncLocalStorage) {
5909
5961
  return asyncLocalStorage.run(stack, fn);
@@ -6390,6 +6442,9 @@ var Bitfab = class {
6390
6442
  const nodeConfiguration = { ...configuration, functionName };
6391
6443
  return function(...args) {
6392
6444
  if (!__bitfabAutoTraceActive()) {
6445
+ if (enclosingSurface() === "opt-in") {
6446
+ throw mixedTracingError("node()", "opt-out", "opt-in");
6447
+ }
6393
6448
  return fn.apply(this, args);
6394
6449
  }
6395
6450
  return runWithAutoTraceNodeConfiguration(
@@ -7091,20 +7146,19 @@ var Bitfab = class {
7091
7146
  const spanId = randomUuid();
7092
7147
  const parentSpanId = parentContext?.spanId ?? null;
7093
7148
  const isRootSpan = parentSpanId === null;
7094
- const requestedSurface = options.surface ?? "opt-in";
7095
- const surface = requestedSurface === "inherit" ? parentContext?.surface ?? "opt-in" : requestedSurface;
7096
- if (requestedSurface !== "inherit" && parentContext?.surface !== void 0 && parentContext.surface !== surface) {
7097
- warnMixedTracingSurfaces(
7098
- traceFunctionKey,
7099
- surface,
7100
- parentContext.surface
7101
- );
7102
- }
7149
+ const requestedSurface = options.surface ?? DEFAULT_SURFACE;
7150
+ const surface = resolveSurface(requestedSurface, parentContext?.surface);
7151
+ assertSurfacesCompatible(
7152
+ requestedSurface,
7153
+ surface,
7154
+ parentContext?.surface,
7155
+ traceFunctionKey
7156
+ );
7103
7157
  const newContext = {
7104
7158
  traceId,
7105
7159
  spanId,
7106
7160
  contexts: [],
7107
- surface
7161
+ ...surface !== void 0 && { surface }
7108
7162
  };
7109
7163
  newStack = [...currentStack, newContext];
7110
7164
  const inputs = args;
@@ -7387,6 +7441,9 @@ var Bitfab = class {
7387
7441
  if (registeredTraceId) {
7388
7442
  activeTraceStates.delete(registeredTraceId);
7389
7443
  }
7444
+ if (setupError instanceof MixedTracingError) {
7445
+ throw setupError;
7446
+ }
7390
7447
  if (getReplayContext() || inSeedScope()) {
7391
7448
  throw setupError;
7392
7449
  }
@@ -7786,9 +7843,14 @@ var Bitfab = class {
7786
7843
  const wrappedKey = fn._bitfabTraceFunctionKey;
7787
7844
  let target = fn;
7788
7845
  if (wrappedKey === void 0) {
7846
+ const seedRootOptions = {
7847
+ name: traceFunctionKey,
7848
+ type: "agent",
7849
+ surface: "neutral"
7850
+ };
7789
7851
  target = this.withSpan(
7790
7852
  traceFunctionKey,
7791
- { name: traceFunctionKey, type: "agent" },
7853
+ seedRootOptions,
7792
7854
  fn
7793
7855
  );
7794
7856
  } else if (wrappedKey !== traceFunctionKey) {
@@ -7830,11 +7892,12 @@ var Bitfab = class {
7830
7892
  const wrappedKey = fn._bitfabTraceFunctionKey;
7831
7893
  let replayFn = fn;
7832
7894
  if (wrappedKey === void 0) {
7833
- replayFn = this.withSpan(
7834
- traceFunctionKey,
7835
- { name: traceFunctionKey, type: "agent" },
7836
- fn
7837
- );
7895
+ const replayRootOptions = {
7896
+ name: traceFunctionKey,
7897
+ type: "agent",
7898
+ surface: "neutral"
7899
+ };
7900
+ replayFn = this.withSpan(traceFunctionKey, replayRootOptions, fn);
7838
7901
  } else if (wrappedKey !== traceFunctionKey) {
7839
7902
  throw new BitfabError(
7840
7903
  `Function is wrapped with trace function key '${wrappedKey}' but replay was called with '${traceFunctionKey}'. Pass matching keys, or pass the unwrapped function to replay it under the explicit key.`
@@ -8147,6 +8210,7 @@ function resolveTraceFunctionKey(registration) {
8147
8210
  DatasetsClient,
8148
8211
  DbBranchReplayError,
8149
8212
  HttpClient,
8213
+ MixedTracingError,
8150
8214
  NO_MOCK_OVERRIDE,
8151
8215
  ReplayError,
8152
8216
  SUPPORTED_PROVIDERS,