@bitfab/sdk 0.20.0 → 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.20.0";
13
+ var __version__ = "0.21.0";
14
14
 
15
15
  // src/constants.ts
16
16
  var DEFAULT_SERVICE_URL = "https://bitfab.ai";
@@ -2654,11 +2654,11 @@ var Bitfab = class {
2654
2654
  startedAt,
2655
2655
  spanType: options.type ?? "custom"
2656
2656
  };
2657
- const sendSpan = async (params) => {
2657
+ const sendSpan = async (params, spanOpts) => {
2658
2658
  const replayCtx = getReplayContext();
2659
2659
  const persistenceCollector = isRootSpan ? replayCtx?.pendingPersistence : void 0;
2660
2660
  let resolvePersistence;
2661
- if (persistenceCollector) {
2661
+ if (persistenceCollector && !spanOpts?.skipPersistenceRegistration) {
2662
2662
  persistenceCollector.push(
2663
2663
  new Promise((resolve) => {
2664
2664
  resolvePersistence = resolve;
@@ -2758,11 +2758,41 @@ var Bitfab = class {
2758
2758
  }
2759
2759
  }
2760
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
+ };
2761
2791
  const executeWithContext = () => {
2762
2792
  const result = fn(...args);
2763
2793
  if (result instanceof Promise) {
2764
2794
  return result.then((resolvedResult) => {
2765
- void sendSpan({ result: resolvedResult });
2795
+ recordSpan(resolvedResult);
2766
2796
  return resolvedResult;
2767
2797
  }).catch((error) => {
2768
2798
  void sendSpan({
@@ -2775,7 +2805,7 @@ var Bitfab = class {
2775
2805
  if (isAsyncGenerator(result)) {
2776
2806
  return wrapAsyncGenerator(result, newStack, sendSpan);
2777
2807
  }
2778
- void sendSpan({ result });
2808
+ recordSpan(result);
2779
2809
  return result;
2780
2810
  };
2781
2811
  return runWithSpanStack(newStack, executeWithContext);
@@ -3059,6 +3089,54 @@ var BitfabFunction = class {
3059
3089
  }
3060
3090
  };
3061
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
+
3062
3140
  export {
3063
3141
  __version__,
3064
3142
  DEFAULT_SERVICE_URL,
@@ -3071,6 +3149,7 @@ export {
3071
3149
  getCurrentSpan,
3072
3150
  getCurrentTrace,
3073
3151
  Bitfab,
3074
- BitfabFunction
3152
+ BitfabFunction,
3153
+ finalizers
3075
3154
  };
3076
- //# sourceMappingURL=chunk-IUZIGC6T.js.map
3155
+ //# sourceMappingURL=chunk-UO3CIQ7R.js.map