bitfab 0.53.0 → 0.53.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.
@@ -19,19 +19,23 @@ import {
19
19
  serializeValue,
20
20
  toJsonSafe,
21
21
  toJsonSafeReport
22
- } from "./chunk-SJKWUQ3G.js";
22
+ } from "./chunk-MMT5MWG5.js";
23
23
  import {
24
24
  BitfabError,
25
25
  DEFAULT_SERVICE_URL,
26
26
  HttpClient,
27
27
  MixedTracingError,
28
+ PolicyRefresh,
29
+ ROOT_TRACE_FUNCTION_KEY_FIELD,
30
+ SimulationPlan,
28
31
  callerTraceMetadata,
29
32
  getReplayContext,
33
+ makeSpanOrigin,
30
34
  readEnv,
31
35
  recordCallerTraceMetadata,
32
36
  retireTraceMetadata,
33
37
  warnOnce
34
- } from "./chunk-6IMGAODW.js";
38
+ } from "./chunk-ASMHSKFS.js";
35
39
  import {
36
40
  __privateAdd,
37
41
  __privateGet,
@@ -340,6 +344,7 @@ var BitfabClaudeAgentHandler = class {
340
344
  trace_id: spanInfo.traceId,
341
345
  started_at: spanInfo.startedAt,
342
346
  ended_at: spanInfo.endedAt ?? nowIso(),
347
+ span_origin: makeSpanOrigin("claude-agent-sdk"),
343
348
  span_data: spanData
344
349
  };
345
350
  if (spanInfo.parentId !== null) {
@@ -1825,6 +1830,7 @@ var BitfabLangGraphCallbackHandler = class {
1825
1830
  trace_id: spanInfo.traceId,
1826
1831
  started_at: spanInfo.startedAt,
1827
1832
  ended_at: spanInfo.endedAt ?? nowIso2(),
1833
+ span_origin: makeSpanOrigin("langgraph"),
1828
1834
  span_data: spanData
1829
1835
  };
1830
1836
  if (spanInfo.parentId !== null) {
@@ -2284,7 +2290,8 @@ var BitfabLangGraphIntegration = class {
2284
2290
  captureWhen: "nested",
2285
2291
  mockOnReplay: shouldMock,
2286
2292
  finalize: finalizeToolResult,
2287
- surface: "inherit"
2293
+ surface: "inherit",
2294
+ instrumentation: "langgraph"
2288
2295
  },
2289
2296
  async (_args) => await originalInvoke(input, ...rest)
2290
2297
  );
@@ -2327,7 +2334,12 @@ var BitfabLangGraphIntegration = class {
2327
2334
  wrapInvoke(fn) {
2328
2335
  return this.client.withSpan(
2329
2336
  this.traceFunctionKey,
2330
- { name: this.traceFunctionKey, type: "agent", surface: "inherit" },
2337
+ {
2338
+ name: this.traceFunctionKey,
2339
+ type: "agent",
2340
+ surface: "inherit",
2341
+ instrumentation: "langgraph"
2342
+ },
2331
2343
  fn
2332
2344
  );
2333
2345
  }
@@ -2380,7 +2392,8 @@ var BitfabOpenAIAgentHandler = class {
2380
2392
  const options_ = {
2381
2393
  type: "agent",
2382
2394
  finalize,
2383
- surface: "inherit"
2395
+ surface: "inherit",
2396
+ instrumentation: "openai-agents"
2384
2397
  };
2385
2398
  const traced = this.withSpanFn(
2386
2399
  this.traceFunctionKey,
@@ -2671,6 +2684,7 @@ var BitfabOpenAITracingProcessor = class {
2671
2684
  * Build span payload for the external spans API.
2672
2685
  */
2673
2686
  buildSpanPayload(serializedSpan, errors) {
2687
+ serializedSpan.span_origin = makeSpanOrigin("openai-agents");
2674
2688
  const payload = {
2675
2689
  id: randomUuid(),
2676
2690
  type: "openai",
@@ -2800,7 +2814,8 @@ var BitfabVercelAiHandler = class {
2800
2814
  {
2801
2815
  type: "llm",
2802
2816
  finalize: (result) => summarizeGenerate(result ?? {}, label),
2803
- surface: "inherit"
2817
+ surface: "inherit",
2818
+ instrumentation: "vercel-ai"
2804
2819
  },
2805
2820
  () => doGenerate()
2806
2821
  );
@@ -2818,7 +2833,12 @@ var BitfabVercelAiHandler = class {
2818
2833
  // The wrapped fn returns immediately with the live stream, so the span
2819
2834
  // output cannot be read from the return value. `finalize` instead
2820
2835
  // awaits the summary the accumulator resolves once the stream drains.
2821
- { type: "llm", finalize: () => summary, surface: "inherit" },
2836
+ {
2837
+ type: "llm",
2838
+ finalize: () => summary,
2839
+ surface: "inherit",
2840
+ instrumentation: "vercel-ai"
2841
+ },
2822
2842
  async () => {
2823
2843
  const result = await doStream();
2824
2844
  const stream = result.stream.pipeThrough(
@@ -2835,6 +2855,19 @@ var BitfabVercelAiHandler = class {
2835
2855
 
2836
2856
  // src/client.ts
2837
2857
  var activeTraceStates = /* @__PURE__ */ new Map();
2858
+ function rootTraceFunctionKeyOf(payload) {
2859
+ const captured = payload[ROOT_TRACE_FUNCTION_KEY_FIELD];
2860
+ if (typeof captured === "string") {
2861
+ return captured;
2862
+ }
2863
+ const traceId = payload.traceId;
2864
+ const live = typeof traceId === "string" ? activeTraceStates.get(traceId)?.traceFunctionKey : void 0;
2865
+ if (typeof live === "string") {
2866
+ return live;
2867
+ }
2868
+ const own = payload.traceFunctionKey;
2869
+ return typeof own === "string" ? own : void 0;
2870
+ }
2838
2871
  var asyncLocalStorage = null;
2839
2872
  var SPAN_CONTEXT_STORAGE_SYMBOL = /* @__PURE__ */ Symbol.for("bitfab.spanContextStorage");
2840
2873
  var initializeAsyncContext = () => {
@@ -3239,6 +3272,14 @@ var Bitfab = class {
3239
3272
  serviceUrl: this.serviceUrl,
3240
3273
  timeout: this.timeout
3241
3274
  });
3275
+ this.simulationPlan = new SimulationPlan(
3276
+ this.httpClient,
3277
+ config.simulationPlan ?? true
3278
+ );
3279
+ this.httpClient.externalSpanTransform = (payload, submit) => this.applySimulationPlan(payload, submit);
3280
+ this.httpClient.externalTraceTransform = (payload, submit) => this.simulationPlan.sendTrace(payload, submit);
3281
+ this.httpClient.releaseHeldExternalSpans = (timeoutMs) => this.simulationPlan.release(timeoutMs);
3282
+ this.httpClient.stopSimulationPlan = () => this.simulationPlan.stop();
3242
3283
  this.datasets = new DatasetsClient(this.httpClient);
3243
3284
  this.traces = new TracesClient(this.httpClient);
3244
3285
  this.labels = new LabelsClient(this.httpClient);
@@ -3389,6 +3430,7 @@ var Bitfab = class {
3389
3430
  }
3390
3431
  createAutoTraceRoot(traceFunctionKey, name, options, fn) {
3391
3432
  const self = this;
3433
+ self.simulationPlan.refresh();
3392
3434
  const maxDepth = autoTraceLimit(
3393
3435
  options.maxDepth,
3394
3436
  DEFAULT_AUTO_TRACE_MAX_DEPTH
@@ -3404,7 +3446,8 @@ var Bitfab = class {
3404
3446
  name: options.name,
3405
3447
  nameFor: rootNameFor,
3406
3448
  type: options.type ?? "custom",
3407
- surface: "opt-out"
3449
+ surface: "opt-out",
3450
+ instrumentation: "trace"
3408
3451
  };
3409
3452
  const buildTracedRoot = (spanOptions) => this.withSpan(
3410
3453
  traceFunctionKey,
@@ -3415,6 +3458,7 @@ var Bitfab = class {
3415
3458
  traceFunctionKey
3416
3459
  );
3417
3460
  self.refreshAutoTraceCapturePolicy(traceFunctionKey);
3461
+ self.simulationPlan.refresh();
3418
3462
  let spansUsed = 0;
3419
3463
  let truncated = false;
3420
3464
  const warnTruncated = () => {
@@ -3491,6 +3535,7 @@ var Bitfab = class {
3491
3535
  type: nodeConfiguration?.type ?? "function",
3492
3536
  captureWhen: "nested",
3493
3537
  surface: "opt-out",
3538
+ instrumentation: "trace",
3494
3539
  functionId: definition.id,
3495
3540
  captureContent: nodeConfiguration !== void 0 || capturePolicy === void 0 || capturePolicy.has(definition.id),
3496
3541
  autoTraceDefinition: definition,
@@ -3522,6 +3567,7 @@ var Bitfab = class {
3522
3567
  type: "function",
3523
3568
  captureWhen: "nested",
3524
3569
  surface: "opt-out",
3570
+ instrumentation: "trace",
3525
3571
  captureContent: true,
3526
3572
  ...link !== void 0 && { nestedTrace: link }
3527
3573
  };
@@ -3587,38 +3633,36 @@ var Bitfab = class {
3587
3633
  Object.defineProperty(autoTraceRoot, "_bitfabWrappedFn", { value: fn });
3588
3634
  return autoTraceRoot;
3589
3635
  }
3636
+ applySimulationPlan(payload, submit) {
3637
+ this.simulationPlan.send(payload, rootTraceFunctionKeyOf(payload), submit);
3638
+ }
3590
3639
  refreshAutoTraceCapturePolicy(traceFunctionKey) {
3591
- const now = Date.now();
3592
- const state = this.autoTracePolicyRefreshes.get(traceFunctionKey) ?? {
3593
- refreshAfter: 0
3594
- };
3595
- if (state.inFlight || now < state.refreshAfter) {
3596
- return;
3597
- }
3598
- const request = this.httpClient.getAutoTracePolicy(
3599
- traceFunctionKey,
3600
- AUTO_TRACE_PROTOCOL
3601
- ).then((policy) => {
3602
- if (policy.protocol !== AUTO_TRACE_PROTOCOL) {
3603
- state.refreshAfter = Date.now() + AUTO_TRACE_POLICY_RETRY_MS;
3604
- return;
3605
- }
3606
- const functionIds = Array.isArray(policy.functionIds) ? policy.functionIds.filter(
3607
- (id) => typeof id === "string" && id.startsWith(`${AUTO_TRACE_PROTOCOL}:`)
3608
- ).slice(0, DEFAULT_AUTO_TRACE_MAX_SPANS) : [];
3609
- __setBitfabAutoTraceCapturePolicy(
3610
- this,
3611
- traceFunctionKey,
3612
- policy.revision === null ? void 0 : functionIds
3613
- );
3614
- state.refreshAfter = Date.now() + AUTO_TRACE_POLICY_REFRESH_MS;
3615
- }).catch(() => {
3616
- state.refreshAfter = Date.now() + AUTO_TRACE_POLICY_RETRY_MS;
3617
- }).finally(() => {
3618
- state.inFlight = void 0;
3619
- });
3620
- state.inFlight = request;
3640
+ const state = this.autoTracePolicyRefreshes.get(traceFunctionKey) ?? new PolicyRefresh();
3621
3641
  this.autoTracePolicyRefreshes.set(traceFunctionKey, state);
3642
+ state.run(
3643
+ () => this.httpClient.getAutoTracePolicy(
3644
+ traceFunctionKey,
3645
+ AUTO_TRACE_PROTOCOL
3646
+ ),
3647
+ (policy) => {
3648
+ if (policy.protocol !== AUTO_TRACE_PROTOCOL) {
3649
+ state.hold(AUTO_TRACE_POLICY_RETRY_MS);
3650
+ return;
3651
+ }
3652
+ const functionIds = Array.isArray(policy.functionIds) ? policy.functionIds.filter(
3653
+ (id) => typeof id === "string" && id.startsWith(`${AUTO_TRACE_PROTOCOL}:`)
3654
+ ).slice(0, DEFAULT_AUTO_TRACE_MAX_SPANS) : [];
3655
+ __setBitfabAutoTraceCapturePolicy(
3656
+ this,
3657
+ traceFunctionKey,
3658
+ policy.revision === null ? void 0 : functionIds
3659
+ );
3660
+ state.hold(AUTO_TRACE_POLICY_REFRESH_MS);
3661
+ },
3662
+ () => {
3663
+ state.hold(AUTO_TRACE_POLICY_RETRY_MS);
3664
+ }
3665
+ );
3622
3666
  }
3623
3667
  /**
3624
3668
  * Flush and permanently close this client's tracing resources: its pending
@@ -4102,6 +4146,7 @@ var Bitfab = class {
4102
4146
  * @returns A wrapped function with the same signature that creates spans for inputs and outputs
4103
4147
  */
4104
4148
  withSpan(traceFunctionKey, optionsOrFn, maybeFn) {
4149
+ this.simulationPlan.refresh();
4105
4150
  const options = typeof optionsOrFn === "function" ? {} : optionsOrFn;
4106
4151
  const fn = typeof optionsOrFn === "function" ? optionsOrFn : maybeFn;
4107
4152
  const self = this;
@@ -4193,8 +4238,10 @@ var Bitfab = class {
4193
4238
  const testRunId = replayCtxAtStart?.testRunId ?? options.testRunId;
4194
4239
  if (isRootSpan && !activeTraceStates.has(traceId)) {
4195
4240
  const dbSnapshotRef = buildSnapshotRef(self.dbSnapshot, startedAt);
4241
+ self.simulationPlan.refresh();
4196
4242
  activeTraceStates.set(traceId, {
4197
4243
  traceId,
4244
+ traceFunctionKey,
4198
4245
  startedAt,
4199
4246
  contexts: [],
4200
4247
  ...testRunId !== void 0 && { testRunId },
@@ -4209,8 +4256,10 @@ var Bitfab = class {
4209
4256
  registeredTraceId = traceId;
4210
4257
  }
4211
4258
  const functionName = fn.name !== "" ? fn.name : void 0;
4259
+ const rootTraceFunctionKey = activeTraceStates.get(traceId)?.traceFunctionKey ?? traceFunctionKey;
4212
4260
  const baseSpanParams = {
4213
4261
  traceFunctionKey,
4262
+ rootTraceFunctionKey,
4214
4263
  functionName,
4215
4264
  spanName: options.name ?? options.nameFor?.(this) ?? qualifiedSpanName(this, functionName) ?? traceFunctionKey,
4216
4265
  traceId,
@@ -4219,6 +4268,7 @@ var Bitfab = class {
4219
4268
  inputs,
4220
4269
  startedAt,
4221
4270
  spanType: options.type ?? "custom",
4271
+ instrumentation: options.instrumentation ?? "span",
4222
4272
  functionId: options.functionId,
4223
4273
  captureContent: options.captureContent ?? true,
4224
4274
  autoTraceDefinition: options.autoTraceDefinition,
@@ -4634,6 +4684,7 @@ var Bitfab = class {
4634
4684
  * @returns A BitfabFunction instance for wrapping functions
4635
4685
  */
4636
4686
  getFunction(traceFunctionKey) {
4687
+ this.simulationPlan.refresh();
4637
4688
  return new BitfabFunction(this, traceFunctionKey);
4638
4689
  }
4639
4690
  /**
@@ -4721,6 +4772,7 @@ var Bitfab = class {
4721
4772
  trace_id: params.traceId,
4722
4773
  started_at: params.startedAt,
4723
4774
  ended_at: params.endedAt,
4775
+ span_origin: makeSpanOrigin(params.instrumentation),
4724
4776
  span_data: {
4725
4777
  name: params.spanName,
4726
4778
  type: params.spanType,
@@ -4781,6 +4833,9 @@ var Bitfab = class {
4781
4833
  source: "typescript-sdk-function",
4782
4834
  sourceTraceId: params.traceId,
4783
4835
  traceFunctionKey: params.traceFunctionKey,
4836
+ ...params.rootTraceFunctionKey !== void 0 && {
4837
+ [ROOT_TRACE_FUNCTION_KEY_FIELD]: params.rootTraceFunctionKey
4838
+ },
4784
4839
  rawSpan: externalSpan,
4785
4840
  ...params.testRunId && { testRunId: params.testRunId },
4786
4841
  ...params.mocked && { mocked: true },
@@ -4849,6 +4904,7 @@ var Bitfab = class {
4849
4904
  }
4850
4905
  activeTraceStates.set(traceId, {
4851
4906
  traceId,
4907
+ traceFunctionKey,
4852
4908
  startedAt,
4853
4909
  contexts: [],
4854
4910
  ingestionType: "seeded",
@@ -4858,6 +4914,7 @@ var Bitfab = class {
4858
4914
  try {
4859
4915
  this.sendWrapperSpan({
4860
4916
  traceFunctionKey,
4917
+ rootTraceFunctionKey: traceFunctionKey,
4861
4918
  spanName: options.spanName ?? traceFunctionKey,
4862
4919
  traceId,
4863
4920
  spanId: randomUuid(),
@@ -4867,6 +4924,7 @@ var Bitfab = class {
4867
4924
  startedAt,
4868
4925
  endedAt: startedAt,
4869
4926
  spanType: options.spanType ?? "agent",
4927
+ instrumentation: "span",
4870
4928
  captureContent: true
4871
4929
  });
4872
4930
  this.sendTraceCompletion({
@@ -4912,6 +4970,7 @@ var Bitfab = class {
4912
4970
  }
4913
4971
  activeTraceStates.set(traceId, {
4914
4972
  traceId,
4973
+ traceFunctionKey,
4915
4974
  startedAt: nowIsoTimestamp(),
4916
4975
  contexts: [],
4917
4976
  ingestionType: "seeded",
@@ -4924,7 +4983,7 @@ var Bitfab = class {
4924
4983
  await runWithSeedContext({ traceId }, async () => target(...args));
4925
4984
  } finally {
4926
4985
  try {
4927
- const { flushTraces: flushTraces2 } = await import("./http-SEN3PUXX.js");
4986
+ const { flushTraces: flushTraces2 } = await import("./http-2OOKOZQ3.js");
4928
4987
  await flushTraces2(3e4);
4929
4988
  } finally {
4930
4989
  unrecorded = activeTraceStates.delete(traceId);
@@ -4983,7 +5042,7 @@ var Bitfab = class {
4983
5042
  `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.`
4984
5043
  );
4985
5044
  }
4986
- const { replay: doReplay } = await import("./replay-Y5RIJOWE.js");
5045
+ const { replay: doReplay } = await import("./replay-KJKIGXRH.js");
4987
5046
  return doReplay(
4988
5047
  this.httpClient,
4989
5048
  this.serviceUrl,
@@ -5267,7 +5326,7 @@ async function seedFromRegistry(registry, pipeline, cases, options = {}) {
5267
5326
  sessionId: seedCase.sessionId
5268
5327
  })
5269
5328
  );
5270
- const { flushTraces: flushTraces2 } = await import("./http-SEN3PUXX.js");
5329
+ const { flushTraces: flushTraces2 } = await import("./http-2OOKOZQ3.js");
5271
5330
  await flushTraces2(3e4);
5272
5331
  return { pipeline, traceFunctionKey, traceIds };
5273
5332
  }
@@ -5304,4 +5363,4 @@ export {
5304
5363
  reseedFromRegistry,
5305
5364
  seedFromRegistry
5306
5365
  };
5307
- //# sourceMappingURL=chunk-SCXR43UF.js.map
5366
+ //# sourceMappingURL=chunk-ZCOZHNAA.js.map