@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.d.cts CHANGED
@@ -396,6 +396,13 @@ interface ReplayOptions {
396
396
  environment?: ReplayEnvironment;
397
397
  /** Group ID to associate this replay with an experiment group for live streaming in Studio. */
398
398
  experimentGroupId?: string;
399
+ /**
400
+ * Dataset this replay runs against. When set, the resulting experiment is
401
+ * durably attributed to the dataset (stored on the test run), so it appears
402
+ * under the dataset's experiments even if the trace lineage can't be
403
+ * reconstructed. Validated server-side against the org.
404
+ */
405
+ datasetId?: string;
399
406
  /**
400
407
  * Reshape recorded inputs before they are spread into `fn`.
401
408
  *
@@ -749,6 +756,25 @@ interface SpanOptions {
749
756
  * only the marked descendants return their recorded output.
750
757
  */
751
758
  mockOnReplay?: boolean;
759
+ /**
760
+ * Record a serializable view of a non-serializable result (e.g. a live
761
+ * stream object) as the span output.
762
+ *
763
+ * When set, the wrapped function's raw return value is handed back to the
764
+ * caller unchanged (so streaming and first-byte latency are untouched),
765
+ * but instead of serializing that raw value, the span records
766
+ * `await finalize(result)`. Use this to trace functions that return a live
767
+ * stream consumed by the caller (Vercel AI SDK `streamText`, a
768
+ * `ReadableStream`, an SSE response) while still capturing a serializable,
769
+ * replayable output such as `{ text, usage, toolCalls }`.
770
+ *
771
+ * Reading from a multi-consumer stream result (like the AI SDK's, which
772
+ * tees internally) does not disturb the caller's own consumption. For the
773
+ * Vercel AI SDK shape, pass the prebuilt `finalizers.aiSdk` helper.
774
+ *
775
+ * Ignored for async-generator results, which are captured automatically.
776
+ */
777
+ finalize?: (result: any) => unknown | Promise<unknown>;
752
778
  }
753
779
 
754
780
  /**
@@ -1063,7 +1089,7 @@ declare class BitfabFunction {
1063
1089
  /**
1064
1090
  * SDK version from package.json (injected at build time)
1065
1091
  */
1066
- declare const __version__ = "0.19.1";
1092
+ declare const __version__ = "0.21.0";
1067
1093
 
1068
1094
  /**
1069
1095
  * Constants for the Bitfab SDK.
@@ -1073,4 +1099,62 @@ declare const __version__ = "0.19.1";
1073
1099
  */
1074
1100
  declare const DEFAULT_SERVICE_URL = "https://bitfab.ai";
1075
1101
 
1076
- export { type ActiveSpanContext, type AdaptContext, type AdaptInputsFn, type AllowedEnvVars, type BamlExecutionResult, Bitfab, BitfabClaudeAgentHandler, type BitfabConfig, BitfabError, BitfabFunction, BitfabLangGraphCallbackHandler as BitfabLangChainCallbackHandler, BitfabLangGraphCallbackHandler, BitfabOpenAITracingProcessor, type CodeChangeFile, type CurrentSpan, type CurrentTrace, DEFAULT_SERVICE_URL, type DbSnapshotConfig, type DbSnapshotProvider, type DbSnapshotRef, type DetachedTrace, type MockStrategy, type ProviderDefinition, ReplayEnvironment, type ReplayEnvironmentSnapshot, type ReplayItem, type ReplayOptions, type ReplayResult, SUPPORTED_PROVIDERS, type SpanOptions, type SpanType, type TokenUsage, type TraceResponse, type TracingProcessor, type WrapBAMLOptions, type WrappedBamlFn, __version__, flushTraces, getCurrentSpan, getCurrentTrace };
1102
+ /**
1103
+ * Prebuilt `finalize` helpers for `withSpan({ finalize }, fn)`.
1104
+ *
1105
+ * A streaming function returns a live stream object that the caller consumes
1106
+ * directly (SSE, a UI message stream). `withSpan` hands that object back
1107
+ * unchanged; a `finalize` function tells it what serializable view to record
1108
+ * as the span output instead of the raw, non-serializable stream.
1109
+ */
1110
+ /**
1111
+ * Drain a Vercel AI SDK streaming result into a serializable, replayable
1112
+ * span output: `{ text, usage, finishReason, toolCalls, toolResults }`.
1113
+ *
1114
+ * Pass it straight to `withSpan`:
1115
+ *
1116
+ * ```ts
1117
+ * import { finalizers } from "@bitfab/sdk"
1118
+ *
1119
+ * const traced = bitfab.withSpan(
1120
+ * "chat-turn",
1121
+ * { type: "agent", finalize: finalizers.aiSdk },
1122
+ * () => streamText({ model, messages }),
1123
+ * )
1124
+ * const result = traced() // caller still gets the live StreamTextResult
1125
+ * return result.toUIMessageStreamResponse()
1126
+ * ```
1127
+ *
1128
+ * Never throws: any field that is absent or rejects is recorded as
1129
+ * `undefined` so finalize never drops the span.
1130
+ */
1131
+ declare function aiSdk(result: unknown): Promise<Record<string, unknown>>;
1132
+ /**
1133
+ * Collect a `ReadableStream`'s chunks into an array for the span output,
1134
+ * via a `tee()` so the caller's branch is untouched. The caller MUST use
1135
+ * the returned stream, not the original, since a stream can only be read
1136
+ * once:
1137
+ *
1138
+ * ```ts
1139
+ * let live: ReadableStream
1140
+ * const traced = bitfab.withSpan(
1141
+ * "render",
1142
+ * { finalize: (r) => finalizers.readableStream(r, (s) => { live = s }) },
1143
+ * () => makeReadableStream(),
1144
+ * )
1145
+ * traced()
1146
+ * return new Response(live!)
1147
+ * ```
1148
+ *
1149
+ * Prefer `aiSdk` for the Vercel AI SDK, whose result tees internally and
1150
+ * needs no caller rewiring.
1151
+ */
1152
+ declare function readableStream(stream: ReadableStream, onLive: (live: ReadableStream) => void): Promise<{
1153
+ chunks: unknown[];
1154
+ }>;
1155
+ declare const finalizers: {
1156
+ aiSdk: typeof aiSdk;
1157
+ readableStream: typeof readableStream;
1158
+ };
1159
+
1160
+ export { type ActiveSpanContext, type AdaptContext, type AdaptInputsFn, type AllowedEnvVars, type BamlExecutionResult, Bitfab, BitfabClaudeAgentHandler, type BitfabConfig, BitfabError, BitfabFunction, BitfabLangGraphCallbackHandler as BitfabLangChainCallbackHandler, BitfabLangGraphCallbackHandler, BitfabOpenAITracingProcessor, type CodeChangeFile, type CurrentSpan, type CurrentTrace, DEFAULT_SERVICE_URL, type DbSnapshotConfig, type DbSnapshotProvider, type DbSnapshotRef, type DetachedTrace, type MockStrategy, type ProviderDefinition, ReplayEnvironment, type ReplayEnvironmentSnapshot, type ReplayItem, type ReplayOptions, type ReplayResult, SUPPORTED_PROVIDERS, type SpanOptions, type SpanType, type TokenUsage, type TraceResponse, type TracingProcessor, type WrapBAMLOptions, type WrappedBamlFn, __version__, finalizers, flushTraces, getCurrentSpan, getCurrentTrace };
package/dist/index.d.ts CHANGED
@@ -396,6 +396,13 @@ interface ReplayOptions {
396
396
  environment?: ReplayEnvironment;
397
397
  /** Group ID to associate this replay with an experiment group for live streaming in Studio. */
398
398
  experimentGroupId?: string;
399
+ /**
400
+ * Dataset this replay runs against. When set, the resulting experiment is
401
+ * durably attributed to the dataset (stored on the test run), so it appears
402
+ * under the dataset's experiments even if the trace lineage can't be
403
+ * reconstructed. Validated server-side against the org.
404
+ */
405
+ datasetId?: string;
399
406
  /**
400
407
  * Reshape recorded inputs before they are spread into `fn`.
401
408
  *
@@ -749,6 +756,25 @@ interface SpanOptions {
749
756
  * only the marked descendants return their recorded output.
750
757
  */
751
758
  mockOnReplay?: boolean;
759
+ /**
760
+ * Record a serializable view of a non-serializable result (e.g. a live
761
+ * stream object) as the span output.
762
+ *
763
+ * When set, the wrapped function's raw return value is handed back to the
764
+ * caller unchanged (so streaming and first-byte latency are untouched),
765
+ * but instead of serializing that raw value, the span records
766
+ * `await finalize(result)`. Use this to trace functions that return a live
767
+ * stream consumed by the caller (Vercel AI SDK `streamText`, a
768
+ * `ReadableStream`, an SSE response) while still capturing a serializable,
769
+ * replayable output such as `{ text, usage, toolCalls }`.
770
+ *
771
+ * Reading from a multi-consumer stream result (like the AI SDK's, which
772
+ * tees internally) does not disturb the caller's own consumption. For the
773
+ * Vercel AI SDK shape, pass the prebuilt `finalizers.aiSdk` helper.
774
+ *
775
+ * Ignored for async-generator results, which are captured automatically.
776
+ */
777
+ finalize?: (result: any) => unknown | Promise<unknown>;
752
778
  }
753
779
 
754
780
  /**
@@ -1063,7 +1089,7 @@ declare class BitfabFunction {
1063
1089
  /**
1064
1090
  * SDK version from package.json (injected at build time)
1065
1091
  */
1066
- declare const __version__ = "0.19.1";
1092
+ declare const __version__ = "0.21.0";
1067
1093
 
1068
1094
  /**
1069
1095
  * Constants for the Bitfab SDK.
@@ -1073,4 +1099,62 @@ declare const __version__ = "0.19.1";
1073
1099
  */
1074
1100
  declare const DEFAULT_SERVICE_URL = "https://bitfab.ai";
1075
1101
 
1076
- export { type ActiveSpanContext, type AdaptContext, type AdaptInputsFn, type AllowedEnvVars, type BamlExecutionResult, Bitfab, BitfabClaudeAgentHandler, type BitfabConfig, BitfabError, BitfabFunction, BitfabLangGraphCallbackHandler as BitfabLangChainCallbackHandler, BitfabLangGraphCallbackHandler, BitfabOpenAITracingProcessor, type CodeChangeFile, type CurrentSpan, type CurrentTrace, DEFAULT_SERVICE_URL, type DbSnapshotConfig, type DbSnapshotProvider, type DbSnapshotRef, type DetachedTrace, type MockStrategy, type ProviderDefinition, ReplayEnvironment, type ReplayEnvironmentSnapshot, type ReplayItem, type ReplayOptions, type ReplayResult, SUPPORTED_PROVIDERS, type SpanOptions, type SpanType, type TokenUsage, type TraceResponse, type TracingProcessor, type WrapBAMLOptions, type WrappedBamlFn, __version__, flushTraces, getCurrentSpan, getCurrentTrace };
1102
+ /**
1103
+ * Prebuilt `finalize` helpers for `withSpan({ finalize }, fn)`.
1104
+ *
1105
+ * A streaming function returns a live stream object that the caller consumes
1106
+ * directly (SSE, a UI message stream). `withSpan` hands that object back
1107
+ * unchanged; a `finalize` function tells it what serializable view to record
1108
+ * as the span output instead of the raw, non-serializable stream.
1109
+ */
1110
+ /**
1111
+ * Drain a Vercel AI SDK streaming result into a serializable, replayable
1112
+ * span output: `{ text, usage, finishReason, toolCalls, toolResults }`.
1113
+ *
1114
+ * Pass it straight to `withSpan`:
1115
+ *
1116
+ * ```ts
1117
+ * import { finalizers } from "@bitfab/sdk"
1118
+ *
1119
+ * const traced = bitfab.withSpan(
1120
+ * "chat-turn",
1121
+ * { type: "agent", finalize: finalizers.aiSdk },
1122
+ * () => streamText({ model, messages }),
1123
+ * )
1124
+ * const result = traced() // caller still gets the live StreamTextResult
1125
+ * return result.toUIMessageStreamResponse()
1126
+ * ```
1127
+ *
1128
+ * Never throws: any field that is absent or rejects is recorded as
1129
+ * `undefined` so finalize never drops the span.
1130
+ */
1131
+ declare function aiSdk(result: unknown): Promise<Record<string, unknown>>;
1132
+ /**
1133
+ * Collect a `ReadableStream`'s chunks into an array for the span output,
1134
+ * via a `tee()` so the caller's branch is untouched. The caller MUST use
1135
+ * the returned stream, not the original, since a stream can only be read
1136
+ * once:
1137
+ *
1138
+ * ```ts
1139
+ * let live: ReadableStream
1140
+ * const traced = bitfab.withSpan(
1141
+ * "render",
1142
+ * { finalize: (r) => finalizers.readableStream(r, (s) => { live = s }) },
1143
+ * () => makeReadableStream(),
1144
+ * )
1145
+ * traced()
1146
+ * return new Response(live!)
1147
+ * ```
1148
+ *
1149
+ * Prefer `aiSdk` for the Vercel AI SDK, whose result tees internally and
1150
+ * needs no caller rewiring.
1151
+ */
1152
+ declare function readableStream(stream: ReadableStream, onLive: (live: ReadableStream) => void): Promise<{
1153
+ chunks: unknown[];
1154
+ }>;
1155
+ declare const finalizers: {
1156
+ aiSdk: typeof aiSdk;
1157
+ readableStream: typeof readableStream;
1158
+ };
1159
+
1160
+ export { type ActiveSpanContext, type AdaptContext, type AdaptInputsFn, type AllowedEnvVars, type BamlExecutionResult, Bitfab, BitfabClaudeAgentHandler, type BitfabConfig, BitfabError, BitfabFunction, BitfabLangGraphCallbackHandler as BitfabLangChainCallbackHandler, BitfabLangGraphCallbackHandler, BitfabOpenAITracingProcessor, type CodeChangeFile, type CurrentSpan, type CurrentTrace, DEFAULT_SERVICE_URL, type DbSnapshotConfig, type DbSnapshotProvider, type DbSnapshotRef, type DetachedTrace, type MockStrategy, type ProviderDefinition, ReplayEnvironment, type ReplayEnvironmentSnapshot, type ReplayItem, type ReplayOptions, type ReplayResult, SUPPORTED_PROVIDERS, type SpanOptions, type SpanType, type TokenUsage, type TraceResponse, type TracingProcessor, type WrapBAMLOptions, type WrappedBamlFn, __version__, finalizers, flushTraces, getCurrentSpan, getCurrentTrace };
package/dist/index.js CHANGED
@@ -8,10 +8,11 @@ import {
8
8
  ReplayEnvironment,
9
9
  SUPPORTED_PROVIDERS,
10
10
  __version__,
11
+ finalizers,
11
12
  flushTraces,
12
13
  getCurrentSpan,
13
14
  getCurrentTrace
14
- } from "./chunk-WZ72P5SX.js";
15
+ } from "./chunk-UO3CIQ7R.js";
15
16
  import {
16
17
  BitfabError
17
18
  } from "./chunk-EQI6ZJC3.js";
@@ -27,6 +28,7 @@ export {
27
28
  ReplayEnvironment,
28
29
  SUPPORTED_PROVIDERS,
29
30
  __version__,
31
+ finalizers,
30
32
  flushTraces,
31
33
  getCurrentSpan,
32
34
  getCurrentTrace
package/dist/node.cjs CHANGED
@@ -401,7 +401,8 @@ async function replay(httpClient, serviceUrl, traceFunctionKey, fn, options) {
401
401
  options?.codeChangeFiles,
402
402
  options?.environment !== void 0,
403
403
  // includeDbBranchLease
404
- options?.experimentGroupId
404
+ options?.experimentGroupId,
405
+ options?.datasetId
405
406
  );
406
407
  const mockStrategy = options?.mock ?? "none";
407
408
  const maxConcurrency = options?.maxConcurrency ?? 10;
@@ -488,6 +489,7 @@ __export(node_exports, {
488
489
  ReplayEnvironment: () => ReplayEnvironment,
489
490
  SUPPORTED_PROVIDERS: () => SUPPORTED_PROVIDERS,
490
491
  __version__: () => __version__,
492
+ finalizers: () => finalizers,
491
493
  flushTraces: () => flushTraces,
492
494
  getCurrentSpan: () => getCurrentSpan,
493
495
  getCurrentTrace: () => getCurrentTrace
@@ -502,7 +504,7 @@ registerAsyncLocalStorageClass(
502
504
  );
503
505
 
504
506
  // src/version.generated.ts
505
- var __version__ = "0.19.1";
507
+ var __version__ = "0.21.0";
506
508
 
507
509
  // src/constants.ts
508
510
  var DEFAULT_SERVICE_URL = "https://bitfab.ai";
@@ -780,7 +782,7 @@ var HttpClient = class {
780
782
  * Start a replay session by fetching historical traces.
781
783
  * Blocking call — creates a test run and returns lightweight item references.
782
784
  */
783
- async startReplay(traceFunctionKey, limit, traceIds, codeChangeDescription, codeChangeFiles, includeDbBranchLease, experimentGroupId) {
785
+ async startReplay(traceFunctionKey, limit, traceIds, codeChangeDescription, codeChangeFiles, includeDbBranchLease, experimentGroupId, datasetId) {
784
786
  const payload = { traceFunctionKey };
785
787
  if (limit !== void 0) {
786
788
  payload.limit = limit;
@@ -800,6 +802,9 @@ var HttpClient = class {
800
802
  if (experimentGroupId !== void 0) {
801
803
  payload.experimentGroupId = experimentGroupId;
802
804
  }
805
+ if (datasetId !== void 0) {
806
+ payload.datasetId = datasetId;
807
+ }
803
808
  const timeout = includeDbBranchLease ? 18e4 : 3e4;
804
809
  return this.request("/api/sdk/replay/start", payload, {
805
810
  timeout
@@ -3157,11 +3162,11 @@ var Bitfab = class {
3157
3162
  startedAt,
3158
3163
  spanType: options.type ?? "custom"
3159
3164
  };
3160
- const sendSpan = async (params) => {
3165
+ const sendSpan = async (params, spanOpts) => {
3161
3166
  const replayCtx = getReplayContext();
3162
3167
  const persistenceCollector = isRootSpan ? replayCtx?.pendingPersistence : void 0;
3163
3168
  let resolvePersistence;
3164
- if (persistenceCollector) {
3169
+ if (persistenceCollector && !spanOpts?.skipPersistenceRegistration) {
3165
3170
  persistenceCollector.push(
3166
3171
  new Promise((resolve) => {
3167
3172
  resolvePersistence = resolve;
@@ -3261,11 +3266,41 @@ var Bitfab = class {
3261
3266
  }
3262
3267
  }
3263
3268
  }
3269
+ const recordSpan = (result) => {
3270
+ if (options.finalize) {
3271
+ const replayCtx = getReplayContext();
3272
+ const persistenceCollector = isRootSpan ? replayCtx?.pendingPersistence : void 0;
3273
+ let resolvePersistence;
3274
+ if (persistenceCollector) {
3275
+ persistenceCollector.push(
3276
+ new Promise((resolve) => {
3277
+ resolvePersistence = resolve;
3278
+ })
3279
+ );
3280
+ }
3281
+ void Promise.resolve().then(() => options.finalize(result)).then(
3282
+ (output) => sendSpan(
3283
+ { result: output },
3284
+ { skipPersistenceRegistration: true }
3285
+ )
3286
+ ).catch(
3287
+ (error) => sendSpan(
3288
+ {
3289
+ result: void 0,
3290
+ error: error instanceof Error ? `finalize failed: ${error.message}` : `finalize failed: ${String(error)}`
3291
+ },
3292
+ { skipPersistenceRegistration: true }
3293
+ )
3294
+ ).finally(() => resolvePersistence?.());
3295
+ } else {
3296
+ void sendSpan({ result });
3297
+ }
3298
+ };
3264
3299
  const executeWithContext = () => {
3265
3300
  const result = fn(...args);
3266
3301
  if (result instanceof Promise) {
3267
3302
  return result.then((resolvedResult) => {
3268
- void sendSpan({ result: resolvedResult });
3303
+ recordSpan(resolvedResult);
3269
3304
  return resolvedResult;
3270
3305
  }).catch((error) => {
3271
3306
  void sendSpan({
@@ -3278,7 +3313,7 @@ var Bitfab = class {
3278
3313
  if (isAsyncGenerator(result)) {
3279
3314
  return wrapAsyncGenerator(result, newStack, sendSpan);
3280
3315
  }
3281
- void sendSpan({ result });
3316
+ recordSpan(result);
3282
3317
  return result;
3283
3318
  };
3284
3319
  return runWithSpanStack(newStack, executeWithContext);
@@ -3562,6 +3597,54 @@ var BitfabFunction = class {
3562
3597
  }
3563
3598
  };
3564
3599
 
3600
+ // src/finalizers.ts
3601
+ async function settle(value) {
3602
+ try {
3603
+ return await value;
3604
+ } catch {
3605
+ return void 0;
3606
+ }
3607
+ }
3608
+ async function aiSdk(result) {
3609
+ const r = result ?? {};
3610
+ const [text, usage, totalUsage, finishReason, toolCalls, toolResults] = await Promise.all([
3611
+ settle(r.text),
3612
+ settle(r.usage),
3613
+ settle(r.totalUsage),
3614
+ settle(r.finishReason),
3615
+ settle(r.toolCalls),
3616
+ settle(r.toolResults)
3617
+ ]);
3618
+ return {
3619
+ text,
3620
+ usage: totalUsage ?? usage,
3621
+ finishReason,
3622
+ toolCalls,
3623
+ toolResults
3624
+ };
3625
+ }
3626
+ async function readableStream(stream, onLive) {
3627
+ const [live, copy] = stream.tee();
3628
+ onLive(live);
3629
+ const chunks = [];
3630
+ const reader = copy.getReader();
3631
+ try {
3632
+ for (; ; ) {
3633
+ const { done, value } = await reader.read();
3634
+ if (done) {
3635
+ break;
3636
+ }
3637
+ chunks.push(value);
3638
+ }
3639
+ } catch {
3640
+ }
3641
+ return { chunks };
3642
+ }
3643
+ var finalizers = {
3644
+ aiSdk,
3645
+ readableStream
3646
+ };
3647
+
3565
3648
  // src/node.ts
3566
3649
  init_asyncStorage();
3567
3650
  assertAsyncStorageRegistered();
@@ -3578,6 +3661,7 @@ assertAsyncStorageRegistered();
3578
3661
  ReplayEnvironment,
3579
3662
  SUPPORTED_PROVIDERS,
3580
3663
  __version__,
3664
+ finalizers,
3581
3665
  flushTraces,
3582
3666
  getCurrentSpan,
3583
3667
  getCurrentTrace