@bitfab/sdk 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
@@ -394,7 +394,8 @@ async function replay(httpClient, serviceUrl, traceFunctionKey, fn, options) {
394
394
  options?.codeChangeFiles,
395
395
  options?.environment !== void 0,
396
396
  // includeDbBranchLease
397
- options?.experimentGroupId
397
+ options?.experimentGroupId,
398
+ options?.datasetId
398
399
  );
399
400
  const mockStrategy = options?.mock ?? "none";
400
401
  const maxConcurrency = options?.maxConcurrency ?? 10;
@@ -481,6 +482,7 @@ __export(index_exports, {
481
482
  ReplayEnvironment: () => ReplayEnvironment,
482
483
  SUPPORTED_PROVIDERS: () => SUPPORTED_PROVIDERS,
483
484
  __version__: () => __version__,
485
+ finalizers: () => finalizers,
484
486
  flushTraces: () => flushTraces,
485
487
  getCurrentSpan: () => getCurrentSpan,
486
488
  getCurrentTrace: () => getCurrentTrace
@@ -488,7 +490,7 @@ __export(index_exports, {
488
490
  module.exports = __toCommonJS(index_exports);
489
491
 
490
492
  // src/version.generated.ts
491
- var __version__ = "0.19.1";
493
+ var __version__ = "0.21.0";
492
494
 
493
495
  // src/constants.ts
494
496
  var DEFAULT_SERVICE_URL = "https://bitfab.ai";
@@ -766,7 +768,7 @@ var HttpClient = class {
766
768
  * Start a replay session by fetching historical traces.
767
769
  * Blocking call — creates a test run and returns lightweight item references.
768
770
  */
769
- async startReplay(traceFunctionKey, limit, traceIds, codeChangeDescription, codeChangeFiles, includeDbBranchLease, experimentGroupId) {
771
+ async startReplay(traceFunctionKey, limit, traceIds, codeChangeDescription, codeChangeFiles, includeDbBranchLease, experimentGroupId, datasetId) {
770
772
  const payload = { traceFunctionKey };
771
773
  if (limit !== void 0) {
772
774
  payload.limit = limit;
@@ -786,6 +788,9 @@ var HttpClient = class {
786
788
  if (experimentGroupId !== void 0) {
787
789
  payload.experimentGroupId = experimentGroupId;
788
790
  }
791
+ if (datasetId !== void 0) {
792
+ payload.datasetId = datasetId;
793
+ }
789
794
  const timeout = includeDbBranchLease ? 18e4 : 3e4;
790
795
  return this.request("/api/sdk/replay/start", payload, {
791
796
  timeout
@@ -3143,11 +3148,11 @@ var Bitfab = class {
3143
3148
  startedAt,
3144
3149
  spanType: options.type ?? "custom"
3145
3150
  };
3146
- const sendSpan = async (params) => {
3151
+ const sendSpan = async (params, spanOpts) => {
3147
3152
  const replayCtx = getReplayContext();
3148
3153
  const persistenceCollector = isRootSpan ? replayCtx?.pendingPersistence : void 0;
3149
3154
  let resolvePersistence;
3150
- if (persistenceCollector) {
3155
+ if (persistenceCollector && !spanOpts?.skipPersistenceRegistration) {
3151
3156
  persistenceCollector.push(
3152
3157
  new Promise((resolve) => {
3153
3158
  resolvePersistence = resolve;
@@ -3247,11 +3252,41 @@ var Bitfab = class {
3247
3252
  }
3248
3253
  }
3249
3254
  }
3255
+ const recordSpan = (result) => {
3256
+ if (options.finalize) {
3257
+ const replayCtx = getReplayContext();
3258
+ const persistenceCollector = isRootSpan ? replayCtx?.pendingPersistence : void 0;
3259
+ let resolvePersistence;
3260
+ if (persistenceCollector) {
3261
+ persistenceCollector.push(
3262
+ new Promise((resolve) => {
3263
+ resolvePersistence = resolve;
3264
+ })
3265
+ );
3266
+ }
3267
+ void Promise.resolve().then(() => options.finalize(result)).then(
3268
+ (output) => sendSpan(
3269
+ { result: output },
3270
+ { skipPersistenceRegistration: true }
3271
+ )
3272
+ ).catch(
3273
+ (error) => sendSpan(
3274
+ {
3275
+ result: void 0,
3276
+ error: error instanceof Error ? `finalize failed: ${error.message}` : `finalize failed: ${String(error)}`
3277
+ },
3278
+ { skipPersistenceRegistration: true }
3279
+ )
3280
+ ).finally(() => resolvePersistence?.());
3281
+ } else {
3282
+ void sendSpan({ result });
3283
+ }
3284
+ };
3250
3285
  const executeWithContext = () => {
3251
3286
  const result = fn(...args);
3252
3287
  if (result instanceof Promise) {
3253
3288
  return result.then((resolvedResult) => {
3254
- void sendSpan({ result: resolvedResult });
3289
+ recordSpan(resolvedResult);
3255
3290
  return resolvedResult;
3256
3291
  }).catch((error) => {
3257
3292
  void sendSpan({
@@ -3264,7 +3299,7 @@ var Bitfab = class {
3264
3299
  if (isAsyncGenerator(result)) {
3265
3300
  return wrapAsyncGenerator(result, newStack, sendSpan);
3266
3301
  }
3267
- void sendSpan({ result });
3302
+ recordSpan(result);
3268
3303
  return result;
3269
3304
  };
3270
3305
  return runWithSpanStack(newStack, executeWithContext);
@@ -3547,6 +3582,54 @@ var BitfabFunction = class {
3547
3582
  );
3548
3583
  }
3549
3584
  };
3585
+
3586
+ // src/finalizers.ts
3587
+ async function settle(value) {
3588
+ try {
3589
+ return await value;
3590
+ } catch {
3591
+ return void 0;
3592
+ }
3593
+ }
3594
+ async function aiSdk(result) {
3595
+ const r = result ?? {};
3596
+ const [text, usage, totalUsage, finishReason, toolCalls, toolResults] = await Promise.all([
3597
+ settle(r.text),
3598
+ settle(r.usage),
3599
+ settle(r.totalUsage),
3600
+ settle(r.finishReason),
3601
+ settle(r.toolCalls),
3602
+ settle(r.toolResults)
3603
+ ]);
3604
+ return {
3605
+ text,
3606
+ usage: totalUsage ?? usage,
3607
+ finishReason,
3608
+ toolCalls,
3609
+ toolResults
3610
+ };
3611
+ }
3612
+ async function readableStream(stream, onLive) {
3613
+ const [live, copy] = stream.tee();
3614
+ onLive(live);
3615
+ const chunks = [];
3616
+ const reader = copy.getReader();
3617
+ try {
3618
+ for (; ; ) {
3619
+ const { done, value } = await reader.read();
3620
+ if (done) {
3621
+ break;
3622
+ }
3623
+ chunks.push(value);
3624
+ }
3625
+ } catch {
3626
+ }
3627
+ return { chunks };
3628
+ }
3629
+ var finalizers = {
3630
+ aiSdk,
3631
+ readableStream
3632
+ };
3550
3633
  // Annotate the CommonJS export names for ESM import in node:
3551
3634
  0 && (module.exports = {
3552
3635
  Bitfab,
@@ -3560,6 +3643,7 @@ var BitfabFunction = class {
3560
3643
  ReplayEnvironment,
3561
3644
  SUPPORTED_PROVIDERS,
3562
3645
  __version__,
3646
+ finalizers,
3563
3647
  flushTraces,
3564
3648
  getCurrentSpan,
3565
3649
  getCurrentTrace