bitfab 0.19.1 → 0.21.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/dist/index.cjs CHANGED
@@ -403,7 +403,8 @@ async function replay(httpClient, serviceUrl, traceFunctionKey, fn, options) {
403
403
  options?.codeChangeFiles,
404
404
  options?.environment !== void 0,
405
405
  // includeDbBranchLease
406
- options?.experimentGroupId
406
+ options?.experimentGroupId,
407
+ options?.datasetId
407
408
  );
408
409
  const mockStrategy = options?.mock ?? "none";
409
410
  const maxConcurrency = options?.maxConcurrency ?? 10;
@@ -490,6 +491,7 @@ __export(index_exports, {
490
491
  ReplayEnvironment: () => ReplayEnvironment,
491
492
  SUPPORTED_PROVIDERS: () => SUPPORTED_PROVIDERS,
492
493
  __version__: () => __version__,
494
+ finalizers: () => finalizers,
493
495
  flushTraces: () => flushTraces,
494
496
  getCurrentSpan: () => getCurrentSpan,
495
497
  getCurrentTrace: () => getCurrentTrace
@@ -497,7 +499,7 @@ __export(index_exports, {
497
499
  module.exports = __toCommonJS(index_exports);
498
500
 
499
501
  // src/version.generated.ts
500
- var __version__ = "0.19.1";
502
+ var __version__ = "0.21.0";
501
503
 
502
504
  // src/constants.ts
503
505
  var DEFAULT_SERVICE_URL = "https://bitfab.ai";
@@ -775,7 +777,7 @@ var HttpClient = class {
775
777
  * Start a replay session by fetching historical traces.
776
778
  * Blocking call — creates a test run and returns lightweight item references.
777
779
  */
778
- async startReplay(traceFunctionKey, limit, traceIds, codeChangeDescription, codeChangeFiles, includeDbBranchLease, experimentGroupId) {
780
+ async startReplay(traceFunctionKey, limit, traceIds, codeChangeDescription, codeChangeFiles, includeDbBranchLease, experimentGroupId, datasetId) {
779
781
  const payload = { traceFunctionKey };
780
782
  if (limit !== void 0) {
781
783
  payload.limit = limit;
@@ -795,6 +797,9 @@ var HttpClient = class {
795
797
  if (experimentGroupId !== void 0) {
796
798
  payload.experimentGroupId = experimentGroupId;
797
799
  }
800
+ if (datasetId !== void 0) {
801
+ payload.datasetId = datasetId;
802
+ }
798
803
  const timeout = includeDbBranchLease ? 18e4 : 3e4;
799
804
  return this.request("/api/sdk/replay/start", payload, {
800
805
  timeout
@@ -3152,11 +3157,11 @@ var Bitfab = class {
3152
3157
  startedAt,
3153
3158
  spanType: options.type ?? "custom"
3154
3159
  };
3155
- const sendSpan = async (params) => {
3160
+ const sendSpan = async (params, spanOpts) => {
3156
3161
  const replayCtx = getReplayContext();
3157
3162
  const persistenceCollector = isRootSpan ? replayCtx?.pendingPersistence : void 0;
3158
3163
  let resolvePersistence;
3159
- if (persistenceCollector) {
3164
+ if (persistenceCollector && !spanOpts?.skipPersistenceRegistration) {
3160
3165
  persistenceCollector.push(
3161
3166
  new Promise((resolve) => {
3162
3167
  resolvePersistence = resolve;
@@ -3256,11 +3261,41 @@ var Bitfab = class {
3256
3261
  }
3257
3262
  }
3258
3263
  }
3264
+ const recordSpan = (result) => {
3265
+ if (options.finalize) {
3266
+ const replayCtx = getReplayContext();
3267
+ const persistenceCollector = isRootSpan ? replayCtx?.pendingPersistence : void 0;
3268
+ let resolvePersistence;
3269
+ if (persistenceCollector) {
3270
+ persistenceCollector.push(
3271
+ new Promise((resolve) => {
3272
+ resolvePersistence = resolve;
3273
+ })
3274
+ );
3275
+ }
3276
+ void Promise.resolve().then(() => options.finalize(result)).then(
3277
+ (output) => sendSpan(
3278
+ { result: output },
3279
+ { skipPersistenceRegistration: true }
3280
+ )
3281
+ ).catch(
3282
+ (error) => sendSpan(
3283
+ {
3284
+ result: void 0,
3285
+ error: error instanceof Error ? `finalize failed: ${error.message}` : `finalize failed: ${String(error)}`
3286
+ },
3287
+ { skipPersistenceRegistration: true }
3288
+ )
3289
+ ).finally(() => resolvePersistence?.());
3290
+ } else {
3291
+ void sendSpan({ result });
3292
+ }
3293
+ };
3259
3294
  const executeWithContext = () => {
3260
3295
  const result = fn(...args);
3261
3296
  if (result instanceof Promise) {
3262
3297
  return result.then((resolvedResult) => {
3263
- void sendSpan({ result: resolvedResult });
3298
+ recordSpan(resolvedResult);
3264
3299
  return resolvedResult;
3265
3300
  }).catch((error) => {
3266
3301
  void sendSpan({
@@ -3273,7 +3308,7 @@ var Bitfab = class {
3273
3308
  if (isAsyncGenerator(result)) {
3274
3309
  return wrapAsyncGenerator(result, newStack, sendSpan);
3275
3310
  }
3276
- void sendSpan({ result });
3311
+ recordSpan(result);
3277
3312
  return result;
3278
3313
  };
3279
3314
  return runWithSpanStack(newStack, executeWithContext);
@@ -3556,6 +3591,54 @@ var BitfabFunction = class {
3556
3591
  );
3557
3592
  }
3558
3593
  };
3594
+
3595
+ // src/finalizers.ts
3596
+ async function settle(value) {
3597
+ try {
3598
+ return await value;
3599
+ } catch {
3600
+ return void 0;
3601
+ }
3602
+ }
3603
+ async function aiSdk(result) {
3604
+ const r = result ?? {};
3605
+ const [text, usage, totalUsage, finishReason, toolCalls, toolResults] = await Promise.all([
3606
+ settle(r.text),
3607
+ settle(r.usage),
3608
+ settle(r.totalUsage),
3609
+ settle(r.finishReason),
3610
+ settle(r.toolCalls),
3611
+ settle(r.toolResults)
3612
+ ]);
3613
+ return {
3614
+ text,
3615
+ usage: totalUsage ?? usage,
3616
+ finishReason,
3617
+ toolCalls,
3618
+ toolResults
3619
+ };
3620
+ }
3621
+ async function readableStream(stream, onLive) {
3622
+ const [live, copy] = stream.tee();
3623
+ onLive(live);
3624
+ const chunks = [];
3625
+ const reader = copy.getReader();
3626
+ try {
3627
+ for (; ; ) {
3628
+ const { done, value } = await reader.read();
3629
+ if (done) {
3630
+ break;
3631
+ }
3632
+ chunks.push(value);
3633
+ }
3634
+ } catch {
3635
+ }
3636
+ return { chunks };
3637
+ }
3638
+ var finalizers = {
3639
+ aiSdk,
3640
+ readableStream
3641
+ };
3559
3642
  // Annotate the CommonJS export names for ESM import in node:
3560
3643
  0 && (module.exports = {
3561
3644
  Bitfab,
@@ -3569,6 +3652,7 @@ var BitfabFunction = class {
3569
3652
  ReplayEnvironment,
3570
3653
  SUPPORTED_PROVIDERS,
3571
3654
  __version__,
3655
+ finalizers,
3572
3656
  flushTraces,
3573
3657
  getCurrentSpan,
3574
3658
  getCurrentTrace