@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.
@@ -10,7 +10,7 @@ import {
10
10
  } from "./chunk-EQI6ZJC3.js";
11
11
 
12
12
  // src/version.generated.ts
13
- var __version__ = "0.19.1";
13
+ var __version__ = "0.21.0";
14
14
 
15
15
  // src/constants.ts
16
16
  var DEFAULT_SERVICE_URL = "https://bitfab.ai";
@@ -287,7 +287,7 @@ var HttpClient = class {
287
287
  * Start a replay session by fetching historical traces.
288
288
  * Blocking call — creates a test run and returns lightweight item references.
289
289
  */
290
- async startReplay(traceFunctionKey, limit, traceIds, codeChangeDescription, codeChangeFiles, includeDbBranchLease, experimentGroupId) {
290
+ async startReplay(traceFunctionKey, limit, traceIds, codeChangeDescription, codeChangeFiles, includeDbBranchLease, experimentGroupId, datasetId) {
291
291
  const payload = { traceFunctionKey };
292
292
  if (limit !== void 0) {
293
293
  payload.limit = limit;
@@ -307,6 +307,9 @@ var HttpClient = class {
307
307
  if (experimentGroupId !== void 0) {
308
308
  payload.experimentGroupId = experimentGroupId;
309
309
  }
310
+ if (datasetId !== void 0) {
311
+ payload.datasetId = datasetId;
312
+ }
310
313
  const timeout = includeDbBranchLease ? 18e4 : 3e4;
311
314
  return this.request("/api/sdk/replay/start", payload, {
312
315
  timeout
@@ -2651,11 +2654,11 @@ var Bitfab = class {
2651
2654
  startedAt,
2652
2655
  spanType: options.type ?? "custom"
2653
2656
  };
2654
- const sendSpan = async (params) => {
2657
+ const sendSpan = async (params, spanOpts) => {
2655
2658
  const replayCtx = getReplayContext();
2656
2659
  const persistenceCollector = isRootSpan ? replayCtx?.pendingPersistence : void 0;
2657
2660
  let resolvePersistence;
2658
- if (persistenceCollector) {
2661
+ if (persistenceCollector && !spanOpts?.skipPersistenceRegistration) {
2659
2662
  persistenceCollector.push(
2660
2663
  new Promise((resolve) => {
2661
2664
  resolvePersistence = resolve;
@@ -2755,11 +2758,41 @@ var Bitfab = class {
2755
2758
  }
2756
2759
  }
2757
2760
  }
2761
+ const recordSpan = (result) => {
2762
+ if (options.finalize) {
2763
+ const replayCtx = getReplayContext();
2764
+ const persistenceCollector = isRootSpan ? replayCtx?.pendingPersistence : void 0;
2765
+ let resolvePersistence;
2766
+ if (persistenceCollector) {
2767
+ persistenceCollector.push(
2768
+ new Promise((resolve) => {
2769
+ resolvePersistence = resolve;
2770
+ })
2771
+ );
2772
+ }
2773
+ void Promise.resolve().then(() => options.finalize(result)).then(
2774
+ (output) => sendSpan(
2775
+ { result: output },
2776
+ { skipPersistenceRegistration: true }
2777
+ )
2778
+ ).catch(
2779
+ (error) => sendSpan(
2780
+ {
2781
+ result: void 0,
2782
+ error: error instanceof Error ? `finalize failed: ${error.message}` : `finalize failed: ${String(error)}`
2783
+ },
2784
+ { skipPersistenceRegistration: true }
2785
+ )
2786
+ ).finally(() => resolvePersistence?.());
2787
+ } else {
2788
+ void sendSpan({ result });
2789
+ }
2790
+ };
2758
2791
  const executeWithContext = () => {
2759
2792
  const result = fn(...args);
2760
2793
  if (result instanceof Promise) {
2761
2794
  return result.then((resolvedResult) => {
2762
- void sendSpan({ result: resolvedResult });
2795
+ recordSpan(resolvedResult);
2763
2796
  return resolvedResult;
2764
2797
  }).catch((error) => {
2765
2798
  void sendSpan({
@@ -2772,7 +2805,7 @@ var Bitfab = class {
2772
2805
  if (isAsyncGenerator(result)) {
2773
2806
  return wrapAsyncGenerator(result, newStack, sendSpan);
2774
2807
  }
2775
- void sendSpan({ result });
2808
+ recordSpan(result);
2776
2809
  return result;
2777
2810
  };
2778
2811
  return runWithSpanStack(newStack, executeWithContext);
@@ -2991,7 +3024,7 @@ var Bitfab = class {
2991
3024
  `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.`
2992
3025
  );
2993
3026
  }
2994
- const { replay: doReplay } = await import("./replay-QAWGVRCZ.js");
3027
+ const { replay: doReplay } = await import("./replay-V6RPJYXZ.js");
2995
3028
  return doReplay(
2996
3029
  this.httpClient,
2997
3030
  this.serviceUrl,
@@ -3056,6 +3089,54 @@ var BitfabFunction = class {
3056
3089
  }
3057
3090
  };
3058
3091
 
3092
+ // src/finalizers.ts
3093
+ async function settle(value) {
3094
+ try {
3095
+ return await value;
3096
+ } catch {
3097
+ return void 0;
3098
+ }
3099
+ }
3100
+ async function aiSdk(result) {
3101
+ const r = result ?? {};
3102
+ const [text, usage, totalUsage, finishReason, toolCalls, toolResults] = await Promise.all([
3103
+ settle(r.text),
3104
+ settle(r.usage),
3105
+ settle(r.totalUsage),
3106
+ settle(r.finishReason),
3107
+ settle(r.toolCalls),
3108
+ settle(r.toolResults)
3109
+ ]);
3110
+ return {
3111
+ text,
3112
+ usage: totalUsage ?? usage,
3113
+ finishReason,
3114
+ toolCalls,
3115
+ toolResults
3116
+ };
3117
+ }
3118
+ async function readableStream(stream, onLive) {
3119
+ const [live, copy] = stream.tee();
3120
+ onLive(live);
3121
+ const chunks = [];
3122
+ const reader = copy.getReader();
3123
+ try {
3124
+ for (; ; ) {
3125
+ const { done, value } = await reader.read();
3126
+ if (done) {
3127
+ break;
3128
+ }
3129
+ chunks.push(value);
3130
+ }
3131
+ } catch {
3132
+ }
3133
+ return { chunks };
3134
+ }
3135
+ var finalizers = {
3136
+ aiSdk,
3137
+ readableStream
3138
+ };
3139
+
3059
3140
  export {
3060
3141
  __version__,
3061
3142
  DEFAULT_SERVICE_URL,
@@ -3068,6 +3149,7 @@ export {
3068
3149
  getCurrentSpan,
3069
3150
  getCurrentTrace,
3070
3151
  Bitfab,
3071
- BitfabFunction
3152
+ BitfabFunction,
3153
+ finalizers
3072
3154
  };
3073
- //# sourceMappingURL=chunk-WZ72P5SX.js.map
3155
+ //# sourceMappingURL=chunk-UO3CIQ7R.js.map