@bitfab/sdk 0.14.0 → 0.16.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/node.cjs CHANGED
@@ -76,25 +76,6 @@ var init_asyncStorage = __esm({
76
76
  }
77
77
  });
78
78
 
79
- // src/version.generated.ts
80
- var __version__;
81
- var init_version_generated = __esm({
82
- "src/version.generated.ts"() {
83
- "use strict";
84
- __version__ = "0.14.0";
85
- }
86
- });
87
-
88
- // src/constants.ts
89
- var DEFAULT_SERVICE_URL;
90
- var init_constants = __esm({
91
- "src/constants.ts"() {
92
- "use strict";
93
- init_version_generated();
94
- DEFAULT_SERVICE_URL = "https://bitfab.ai";
95
- }
96
- });
97
-
98
79
  // src/errors.ts
99
80
  var BitfabError;
100
81
  var init_errors = __esm({
@@ -110,343 +91,6 @@ var init_errors = __esm({
110
91
  }
111
92
  });
112
93
 
113
- // src/http.ts
114
- function awaitOnExit(promise) {
115
- pendingTracePromises.add(promise);
116
- void promise.finally(() => {
117
- pendingTracePromises.delete(promise);
118
- }).catch(() => {
119
- });
120
- return promise;
121
- }
122
- async function flushTraces(timeoutMs = 5e3) {
123
- if (pendingTracePromises.size === 0) {
124
- return;
125
- }
126
- await Promise.race([
127
- Promise.allSettled(Array.from(pendingTracePromises)),
128
- new Promise((resolve) => setTimeout(resolve, timeoutMs))
129
- ]);
130
- }
131
- var pendingTracePromises, HttpClient;
132
- var init_http = __esm({
133
- "src/http.ts"() {
134
- "use strict";
135
- init_constants();
136
- init_errors();
137
- pendingTracePromises = /* @__PURE__ */ new Set();
138
- if (typeof process !== "undefined" && process.versions != null && process.versions.node != null) {
139
- let isFlushing = false;
140
- process.on("beforeExit", () => {
141
- if (pendingTracePromises.size > 0 && !isFlushing) {
142
- isFlushing = true;
143
- Promise.allSettled(
144
- Array.from(pendingTracePromises).map(
145
- (p) => p.catch(() => {
146
- })
147
- )
148
- ).then(() => {
149
- isFlushing = false;
150
- }).catch(() => {
151
- isFlushing = false;
152
- });
153
- }
154
- });
155
- }
156
- HttpClient = class {
157
- constructor(config) {
158
- this.apiKey = config.apiKey;
159
- this.serviceUrl = config.serviceUrl;
160
- this.timeout = config.timeout ?? 12e4;
161
- }
162
- /**
163
- * Make an HTTP request to the Bitfab API. Defaults to POST; pass
164
- * `options.method` to use a different verb (e.g. "PATCH").
165
- *
166
- * @param endpoint - The API endpoint (without base URL)
167
- * @param payload - The request body
168
- * @param options - Optional request options
169
- * @returns The parsed JSON response
170
- * @throws {BitfabError} If the request fails
171
- */
172
- async request(endpoint, payload, options) {
173
- const url = `${this.serviceUrl}${endpoint}`;
174
- const timeout = options?.timeout ?? this.timeout;
175
- const method = options?.method ?? "POST";
176
- const controller = new AbortController();
177
- const timeoutId = setTimeout(() => controller.abort(), timeout);
178
- let body;
179
- let serializationError;
180
- try {
181
- body = JSON.stringify(payload);
182
- } catch (error) {
183
- serializationError = error instanceof Error ? error.message : String(error);
184
- body = JSON.stringify({
185
- ...Object.fromEntries(
186
- Object.entries(payload).filter(
187
- ([, v]) => typeof v === "string" || typeof v === "number"
188
- )
189
- ),
190
- rawSpan: {},
191
- errors: [
192
- { source: "sdk", step: "json_serialize", error: serializationError }
193
- ]
194
- });
195
- }
196
- try {
197
- const response = await fetch(url, {
198
- method,
199
- headers: {
200
- "Content-Type": "application/json",
201
- Authorization: `Bearer ${this.apiKey}`
202
- },
203
- body,
204
- signal: controller.signal
205
- });
206
- if (!response.ok) {
207
- const errorText = await response.text();
208
- throw new BitfabError(
209
- `HTTP ${response.status}: ${errorText.slice(0, 500)}`
210
- );
211
- }
212
- const result = await response.json();
213
- if (result.error) {
214
- if (result.url) {
215
- throw new BitfabError(
216
- `${result.error} Configure it at: ${this.serviceUrl}${result.url}`,
217
- result.url
218
- );
219
- }
220
- throw new BitfabError(result.error);
221
- }
222
- return result;
223
- } catch (error) {
224
- if (error instanceof BitfabError) {
225
- throw error;
226
- }
227
- if (error instanceof Error) {
228
- if (error.name === "AbortError") {
229
- throw new BitfabError(`Request timed out after ${timeout}ms`);
230
- }
231
- throw new BitfabError(error.message);
232
- }
233
- throw new BitfabError("Unknown error occurred");
234
- } finally {
235
- clearTimeout(timeoutId);
236
- }
237
- }
238
- /**
239
- * Look up a function by name.
240
- * Blocks until complete - needed for function execution.
241
- */
242
- async lookupFunction(name) {
243
- return this.request("/api/sdk/functions/lookup", { name });
244
- }
245
- /**
246
- * Send an internal trace (from BAML execution).
247
- * Fire-and-forget with awaitOnExit - doesn't block the caller.
248
- */
249
- sendInternalTrace(functionId, payload) {
250
- void awaitOnExit(
251
- this.request(`/api/sdk/functions/${functionId}/traces`, {
252
- ...payload,
253
- sdkVersion: __version__
254
- })
255
- ).catch((error) => {
256
- try {
257
- console.error("Bitfab: Failed to create trace:", error);
258
- } catch {
259
- }
260
- });
261
- }
262
- /**
263
- * Send an external span (from withSpan wrapper or OpenAI tracing).
264
- * Fire-and-forget with awaitOnExit - doesn't block the caller.
265
- * Returns the tracked promise so callers can optionally await it.
266
- */
267
- sendExternalSpan(payload) {
268
- return awaitOnExit(
269
- this.request("/api/sdk/externalSpans", {
270
- ...payload,
271
- sdkVersion: __version__
272
- })
273
- ).catch((error) => {
274
- try {
275
- console.error("Bitfab: Failed to create external span:", error);
276
- } catch {
277
- }
278
- });
279
- }
280
- /**
281
- * Send an external trace (from OpenAI tracing).
282
- * Fire-and-forget with awaitOnExit - doesn't block the caller.
283
- */
284
- sendExternalTrace(payload) {
285
- void awaitOnExit(
286
- this.request("/api/sdk/externalTraces", {
287
- ...payload,
288
- sdkVersion: __version__
289
- })
290
- ).catch((error) => {
291
- try {
292
- console.error("Bitfab: Failed to create external trace:", error);
293
- } catch {
294
- }
295
- });
296
- }
297
- /**
298
- * Partial update of an existing external trace identified by sourceTraceId.
299
- * Used by the detached `client.getTrace(id)` handle. Fire-and-forget;
300
- * returns a tracked promise that callers may optionally await.
301
- */
302
- patchTrace(sourceTraceId, payload) {
303
- const endpoint = `/api/sdk/externalTraces/${encodeURIComponent(sourceTraceId)}`;
304
- return awaitOnExit(
305
- this.request(endpoint, payload, { method: "PATCH" })
306
- ).catch((error) => {
307
- try {
308
- console.error("Bitfab: Failed to patch trace:", error);
309
- } catch {
310
- }
311
- });
312
- }
313
- /**
314
- * Start a replay session by fetching historical traces.
315
- * Blocking call — creates a test run and returns lightweight item references.
316
- */
317
- async startReplay(traceFunctionKey, limit, traceIds, codeChangeDescription, codeChangeFiles, includeDbBranchLease, experimentGroupId) {
318
- const payload = { traceFunctionKey };
319
- if (limit !== void 0) {
320
- payload.limit = limit;
321
- }
322
- if (traceIds) {
323
- payload.traceIds = traceIds;
324
- }
325
- if (codeChangeDescription !== void 0) {
326
- payload.codeChangeDescription = codeChangeDescription;
327
- }
328
- if (codeChangeFiles !== void 0) {
329
- payload.codeChangeFiles = codeChangeFiles;
330
- }
331
- if (includeDbBranchLease) {
332
- payload.includeDbBranchLease = true;
333
- }
334
- if (experimentGroupId !== void 0) {
335
- payload.experimentGroupId = experimentGroupId;
336
- }
337
- const timeout = includeDbBranchLease ? 18e4 : 3e4;
338
- return this.request("/api/sdk/replay/start", payload, {
339
- timeout
340
- });
341
- }
342
- /**
343
- * Fetch an external span by ID.
344
- * Blocking GET request.
345
- */
346
- async getExternalSpan(spanId) {
347
- const url = `${this.serviceUrl}/api/sdk/externalSpans/${spanId}`;
348
- const controller = new AbortController();
349
- const timeoutId = setTimeout(() => controller.abort(), 3e4);
350
- try {
351
- const response = await fetch(url, {
352
- method: "GET",
353
- headers: { Authorization: `Bearer ${this.apiKey}` },
354
- signal: controller.signal
355
- });
356
- if (!response.ok) {
357
- const errorText = await response.text();
358
- throw new BitfabError(
359
- `HTTP ${response.status}: ${errorText.slice(0, 500)}`
360
- );
361
- }
362
- return await response.json();
363
- } catch (error) {
364
- if (error instanceof BitfabError) {
365
- throw error;
366
- }
367
- if (error instanceof Error) {
368
- if (error.name === "AbortError") {
369
- throw new BitfabError("Request timed out after 30000ms");
370
- }
371
- throw new BitfabError(error.message);
372
- }
373
- throw new BitfabError("Unknown error occurred");
374
- } finally {
375
- clearTimeout(timeoutId);
376
- }
377
- }
378
- /**
379
- * Fetch the span tree for a root span.
380
- * Blocking GET request.
381
- */
382
- async getSpanTree(externalSpanId) {
383
- const url = `${this.serviceUrl}/api/sdk/replay/spanTree/${externalSpanId}`;
384
- const controller = new AbortController();
385
- const timeoutId = setTimeout(() => controller.abort(), 3e4);
386
- try {
387
- const response = await fetch(url, {
388
- method: "GET",
389
- headers: { Authorization: `Bearer ${this.apiKey}` },
390
- signal: controller.signal
391
- });
392
- if (!response.ok) {
393
- const errorText = await response.text();
394
- throw new BitfabError(
395
- `HTTP ${response.status}: ${errorText.slice(0, 500)}`
396
- );
397
- }
398
- return await response.json();
399
- } catch (error) {
400
- if (error instanceof BitfabError) {
401
- throw error;
402
- }
403
- if (error instanceof Error) {
404
- if (error.name === "AbortError") {
405
- throw new BitfabError("Request timed out after 30000ms");
406
- }
407
- throw new BitfabError(error.message);
408
- }
409
- throw new BitfabError("Unknown error occurred");
410
- } finally {
411
- clearTimeout(timeoutId);
412
- }
413
- }
414
- /**
415
- * Mark a replay test run as completed.
416
- * Blocking call.
417
- */
418
- async completeReplay(testRunId) {
419
- return this.request(
420
- "/api/sdk/replay/complete",
421
- { testRunId },
422
- { timeout: 3e4 }
423
- );
424
- }
425
- /**
426
- * Ask the server to materialize a per-trace DB branch lease from a
427
- * captured `dbSnapshotRef`. Blocking — the resolver creates a Neon
428
- * snapshot + preview branch and polls operations to readiness, which
429
- * can take seconds.
430
- */
431
- async resolveDbBranchLease(testRunId, traceId, dbSnapshotRef) {
432
- return this.request(
433
- "/api/sdk/replay/resolveDbBranchLease",
434
- { testRunId, traceId, dbSnapshotRef },
435
- { timeout: 9e4 }
436
- );
437
- }
438
- /** Release a previously-resolved DB branch by deleting its Neon branch. Idempotent server-side. */
439
- async releaseDbBranchLease(neonBranchId) {
440
- await this.request(
441
- "/api/sdk/replay/releaseDbBranchLease",
442
- { neonBranchId },
443
- { timeout: 3e4 }
444
- );
445
- }
446
- };
447
- }
448
- });
449
-
450
94
  // src/replayContext.ts
451
95
  function getReplayContext() {
452
96
  return replayContextStorage?.getStore() ?? null;
@@ -581,18 +225,25 @@ function buildMockTree(rootNode) {
581
225
  }
582
226
  return { spans };
583
227
  }
584
- async function processItem(httpClient, serverItem, fn, testRunId, mockStrategy, environment) {
228
+ async function processItem(httpClient, serverItem, fn, testRunId, mockStrategy, environment, adaptInputs) {
585
229
  const lease = environment ? serverItem.dbBranchLease : void 0;
586
230
  let inputs = [];
587
231
  let originalOutput;
588
232
  let result;
589
233
  let error = null;
590
234
  const replayedTraceId = crypto.randomUUID();
235
+ const pendingPersistence = [];
591
236
  try {
592
237
  const span = await httpClient.getExternalSpan(serverItem.externalSpanId);
593
238
  const spanData = span.rawData?.span_data ?? {};
594
239
  inputs = deserializeInputs(spanData);
595
240
  originalOutput = deserializeOutput(spanData);
241
+ if (adaptInputs) {
242
+ inputs = adaptInputs(inputs, {
243
+ traceId: serverItem.traceId,
244
+ sourceSpanId: serverItem.externalSpanId
245
+ });
246
+ }
596
247
  let mockTree;
597
248
  if (mockStrategy === "all" || mockStrategy === "marked") {
598
249
  const treeResponse = await httpClient.getSpanTree(
@@ -610,7 +261,8 @@ async function processItem(httpClient, serverItem, fn, testRunId, mockStrategy,
610
261
  mockTree,
611
262
  callCounters: mockTree ? /* @__PURE__ */ new Map() : void 0,
612
263
  mockStrategy,
613
- dbBranchLease: lease
264
+ dbBranchLease: lease,
265
+ pendingPersistence
614
266
  },
615
267
  () => fn(...inputs)
616
268
  );
@@ -618,6 +270,7 @@ async function processItem(httpClient, serverItem, fn, testRunId, mockStrategy,
618
270
  } catch (e) {
619
271
  error = e instanceof Error ? e.message : String(e);
620
272
  } finally {
273
+ await Promise.allSettled(pendingPersistence);
621
274
  if (lease) {
622
275
  try {
623
276
  await httpClient.releaseDbBranchLease(lease.neonBranchId);
@@ -701,24 +354,51 @@ async function replay(httpClient, serviceUrl, traceFunctionKey, fn, options) {
701
354
  fn,
702
355
  testRunId,
703
356
  mockStrategy,
704
- options?.environment
357
+ options?.environment,
358
+ options?.adaptInputs
705
359
  )
706
360
  );
707
361
  const resultItems = await mapWithConcurrency(tasks, maxConcurrency);
708
- await flushTraces();
709
- let serverTraceIds = {};
710
- try {
711
- const completeResult = await httpClient.completeReplay(testRunId);
712
- serverTraceIds = completeResult.traceIds ?? {};
713
- } catch (e) {
362
+ const completeResult = await httpClient.completeReplay(testRunId);
363
+ const serverTraceIds = completeResult.traceIds;
364
+ if (serverTraceIds === void 0) {
714
365
  try {
715
- console.error("Bitfab: Failed to complete replay:", e);
366
+ console.warn(
367
+ "Bitfab: server did not return replay trace IDs; item.traceId will be null (server upgrade required for verdict persistence)"
368
+ );
716
369
  } catch {
717
370
  }
718
- }
719
- for (const item of resultItems) {
720
- if (item.traceId) {
721
- item.traceId = serverTraceIds[item.traceId] ?? null;
371
+ for (const item of resultItems) {
372
+ item.traceId = null;
373
+ }
374
+ } else {
375
+ const missing = [];
376
+ let completedCount = 0;
377
+ for (const item of resultItems) {
378
+ if (item.traceId) {
379
+ const mapped = serverTraceIds[item.traceId];
380
+ if (item.error === null) {
381
+ completedCount += 1;
382
+ if (mapped === void 0) {
383
+ missing.push(item.traceId);
384
+ }
385
+ }
386
+ item.traceId = mapped ?? null;
387
+ }
388
+ }
389
+ if (missing.length > 0) {
390
+ const serverCount = completeResult.traceCount !== void 0 ? ` The server persisted ${completeResult.traceCount} trace(s) for this run.` : "";
391
+ if (missing.length === completedCount) {
392
+ throw new BitfabError(
393
+ `Replay completed but the server has no persisted trace for any of the ${completedCount} completed item(s) (testRunId ${testRunId}).${serverCount} Trace uploads were awaited, so either the uploads failed (check for "Bitfab: Failed to create" errors above) or the replayed function is not wrapped with withSpan.`
394
+ );
395
+ }
396
+ try {
397
+ console.error(
398
+ `Bitfab: server has no persisted trace for ${missing.length} of ${completedCount} completed replay item(s) (testRunId ${testRunId}).${serverCount} Their traceId is null and verdicts cannot be persisted for them. Missing: ${missing.join(", ")}`
399
+ );
400
+ } catch {
401
+ }
722
402
  }
723
403
  }
724
404
  return {
@@ -731,7 +411,6 @@ var init_replay = __esm({
731
411
  "src/replay.ts"() {
732
412
  "use strict";
733
413
  init_errors();
734
- init_http();
735
414
  init_replayContext();
736
415
  init_serialize();
737
416
  }
@@ -763,9 +442,346 @@ registerAsyncLocalStorageClass(
763
442
  import_node_async_hooks.AsyncLocalStorage
764
443
  );
765
444
 
445
+ // src/version.generated.ts
446
+ var __version__ = "0.16.0";
447
+
448
+ // src/constants.ts
449
+ var DEFAULT_SERVICE_URL = "https://bitfab.ai";
450
+
451
+ // src/http.ts
452
+ init_errors();
453
+ var pendingTracePromises = /* @__PURE__ */ new Set();
454
+ function awaitOnExit(promise) {
455
+ pendingTracePromises.add(promise);
456
+ void promise.finally(() => {
457
+ pendingTracePromises.delete(promise);
458
+ }).catch(() => {
459
+ });
460
+ return promise;
461
+ }
462
+ async function flushTraces(timeoutMs = 5e3) {
463
+ if (pendingTracePromises.size === 0) {
464
+ return;
465
+ }
466
+ await Promise.race([
467
+ Promise.allSettled(Array.from(pendingTracePromises)),
468
+ new Promise((resolve) => setTimeout(resolve, timeoutMs))
469
+ ]);
470
+ }
471
+ if (typeof process !== "undefined" && process.versions != null && process.versions.node != null) {
472
+ let isFlushing = false;
473
+ process.on("beforeExit", () => {
474
+ if (pendingTracePromises.size > 0 && !isFlushing) {
475
+ isFlushing = true;
476
+ Promise.allSettled(
477
+ Array.from(pendingTracePromises).map(
478
+ (p) => p.catch(() => {
479
+ })
480
+ )
481
+ ).then(() => {
482
+ isFlushing = false;
483
+ }).catch(() => {
484
+ isFlushing = false;
485
+ });
486
+ }
487
+ });
488
+ }
489
+ var HttpClient = class {
490
+ constructor(config) {
491
+ this.apiKey = config.apiKey;
492
+ this.serviceUrl = config.serviceUrl;
493
+ this.timeout = config.timeout ?? 12e4;
494
+ }
495
+ /**
496
+ * Make an HTTP request to the Bitfab API. Defaults to POST; pass
497
+ * `options.method` to use a different verb (e.g. "PATCH").
498
+ *
499
+ * @param endpoint - The API endpoint (without base URL)
500
+ * @param payload - The request body
501
+ * @param options - Optional request options
502
+ * @returns The parsed JSON response
503
+ * @throws {BitfabError} If the request fails
504
+ */
505
+ async request(endpoint, payload, options) {
506
+ const url = `${this.serviceUrl}${endpoint}`;
507
+ const timeout = options?.timeout ?? this.timeout;
508
+ const method = options?.method ?? "POST";
509
+ const controller = new AbortController();
510
+ const timeoutId = setTimeout(() => controller.abort(), timeout);
511
+ let body;
512
+ let serializationError;
513
+ try {
514
+ body = JSON.stringify(payload);
515
+ } catch (error) {
516
+ serializationError = error instanceof Error ? error.message : String(error);
517
+ body = JSON.stringify({
518
+ ...Object.fromEntries(
519
+ Object.entries(payload).filter(
520
+ ([, v]) => typeof v === "string" || typeof v === "number"
521
+ )
522
+ ),
523
+ rawSpan: {},
524
+ errors: [
525
+ { source: "sdk", step: "json_serialize", error: serializationError }
526
+ ]
527
+ });
528
+ }
529
+ try {
530
+ const response = await fetch(url, {
531
+ method,
532
+ headers: {
533
+ "Content-Type": "application/json",
534
+ Authorization: `Bearer ${this.apiKey}`
535
+ },
536
+ body,
537
+ signal: controller.signal
538
+ });
539
+ if (!response.ok) {
540
+ const errorText = await response.text();
541
+ throw new BitfabError(
542
+ `HTTP ${response.status}: ${errorText.slice(0, 500)}`
543
+ );
544
+ }
545
+ const result = await response.json();
546
+ if (result.error) {
547
+ if (result.url) {
548
+ throw new BitfabError(
549
+ `${result.error} Configure it at: ${this.serviceUrl}${result.url}`,
550
+ result.url
551
+ );
552
+ }
553
+ throw new BitfabError(result.error);
554
+ }
555
+ return result;
556
+ } catch (error) {
557
+ if (error instanceof BitfabError) {
558
+ throw error;
559
+ }
560
+ if (error instanceof Error) {
561
+ if (error.name === "AbortError") {
562
+ throw new BitfabError(`Request timed out after ${timeout}ms`);
563
+ }
564
+ throw new BitfabError(error.message);
565
+ }
566
+ throw new BitfabError("Unknown error occurred");
567
+ } finally {
568
+ clearTimeout(timeoutId);
569
+ }
570
+ }
571
+ /**
572
+ * Look up a function by name.
573
+ * Blocks until complete - needed for function execution.
574
+ */
575
+ async lookupFunction(name) {
576
+ return this.request("/api/sdk/functions/lookup", { name });
577
+ }
578
+ /**
579
+ * Send an internal trace (from BAML execution).
580
+ * Fire-and-forget with awaitOnExit - doesn't block the caller.
581
+ */
582
+ sendInternalTrace(functionId, payload) {
583
+ void awaitOnExit(
584
+ this.request(`/api/sdk/functions/${functionId}/traces`, {
585
+ ...payload,
586
+ sdkVersion: __version__
587
+ })
588
+ ).catch((error) => {
589
+ try {
590
+ console.error("Bitfab: Failed to create trace:", error);
591
+ } catch {
592
+ }
593
+ });
594
+ }
595
+ /**
596
+ * Send an external span (from withSpan wrapper or OpenAI tracing).
597
+ * Fire-and-forget with awaitOnExit - doesn't block the caller.
598
+ * Returns the tracked promise so callers can optionally await it.
599
+ */
600
+ sendExternalSpan(payload) {
601
+ return awaitOnExit(
602
+ this.request("/api/sdk/externalSpans", {
603
+ ...payload,
604
+ sdkVersion: __version__
605
+ })
606
+ ).catch((error) => {
607
+ try {
608
+ console.error("Bitfab: Failed to create external span:", error);
609
+ } catch {
610
+ }
611
+ });
612
+ }
613
+ /**
614
+ * Send an external trace (from OpenAI tracing).
615
+ * Fire-and-forget with awaitOnExit - doesn't block the caller.
616
+ * Returns the tracked promise so callers can optionally await it
617
+ * (the replay path does, so trace completions are persisted before
618
+ * `completeReplay` builds the trace-ID mapping).
619
+ */
620
+ sendExternalTrace(payload) {
621
+ return awaitOnExit(
622
+ this.request("/api/sdk/externalTraces", {
623
+ ...payload,
624
+ sdkVersion: __version__
625
+ })
626
+ ).catch((error) => {
627
+ try {
628
+ console.error("Bitfab: Failed to create external trace:", error);
629
+ } catch {
630
+ }
631
+ });
632
+ }
633
+ /**
634
+ * Partial update of an existing external trace identified by sourceTraceId.
635
+ * Used by the detached `client.getTrace(id)` handle. Fire-and-forget;
636
+ * returns a tracked promise that callers may optionally await.
637
+ */
638
+ patchTrace(sourceTraceId, payload) {
639
+ const endpoint = `/api/sdk/externalTraces/${encodeURIComponent(sourceTraceId)}`;
640
+ return awaitOnExit(
641
+ this.request(endpoint, payload, { method: "PATCH" })
642
+ ).catch((error) => {
643
+ try {
644
+ console.error("Bitfab: Failed to patch trace:", error);
645
+ } catch {
646
+ }
647
+ });
648
+ }
649
+ /**
650
+ * Start a replay session by fetching historical traces.
651
+ * Blocking call — creates a test run and returns lightweight item references.
652
+ */
653
+ async startReplay(traceFunctionKey, limit, traceIds, codeChangeDescription, codeChangeFiles, includeDbBranchLease, experimentGroupId) {
654
+ const payload = { traceFunctionKey };
655
+ if (limit !== void 0) {
656
+ payload.limit = limit;
657
+ }
658
+ if (traceIds) {
659
+ payload.traceIds = traceIds;
660
+ }
661
+ if (codeChangeDescription !== void 0) {
662
+ payload.codeChangeDescription = codeChangeDescription;
663
+ }
664
+ if (codeChangeFiles !== void 0) {
665
+ payload.codeChangeFiles = codeChangeFiles;
666
+ }
667
+ if (includeDbBranchLease) {
668
+ payload.includeDbBranchLease = true;
669
+ }
670
+ if (experimentGroupId !== void 0) {
671
+ payload.experimentGroupId = experimentGroupId;
672
+ }
673
+ const timeout = includeDbBranchLease ? 18e4 : 3e4;
674
+ return this.request("/api/sdk/replay/start", payload, {
675
+ timeout
676
+ });
677
+ }
678
+ /**
679
+ * Fetch an external span by ID.
680
+ * Blocking GET request.
681
+ */
682
+ async getExternalSpan(spanId) {
683
+ const url = `${this.serviceUrl}/api/sdk/externalSpans/${spanId}`;
684
+ const controller = new AbortController();
685
+ const timeoutId = setTimeout(() => controller.abort(), 3e4);
686
+ try {
687
+ const response = await fetch(url, {
688
+ method: "GET",
689
+ headers: { Authorization: `Bearer ${this.apiKey}` },
690
+ signal: controller.signal
691
+ });
692
+ if (!response.ok) {
693
+ const errorText = await response.text();
694
+ throw new BitfabError(
695
+ `HTTP ${response.status}: ${errorText.slice(0, 500)}`
696
+ );
697
+ }
698
+ return await response.json();
699
+ } catch (error) {
700
+ if (error instanceof BitfabError) {
701
+ throw error;
702
+ }
703
+ if (error instanceof Error) {
704
+ if (error.name === "AbortError") {
705
+ throw new BitfabError("Request timed out after 30000ms");
706
+ }
707
+ throw new BitfabError(error.message);
708
+ }
709
+ throw new BitfabError("Unknown error occurred");
710
+ } finally {
711
+ clearTimeout(timeoutId);
712
+ }
713
+ }
714
+ /**
715
+ * Fetch the span tree for a root span.
716
+ * Blocking GET request.
717
+ */
718
+ async getSpanTree(externalSpanId) {
719
+ const url = `${this.serviceUrl}/api/sdk/replay/spanTree/${externalSpanId}`;
720
+ const controller = new AbortController();
721
+ const timeoutId = setTimeout(() => controller.abort(), 3e4);
722
+ try {
723
+ const response = await fetch(url, {
724
+ method: "GET",
725
+ headers: { Authorization: `Bearer ${this.apiKey}` },
726
+ signal: controller.signal
727
+ });
728
+ if (!response.ok) {
729
+ const errorText = await response.text();
730
+ throw new BitfabError(
731
+ `HTTP ${response.status}: ${errorText.slice(0, 500)}`
732
+ );
733
+ }
734
+ return await response.json();
735
+ } catch (error) {
736
+ if (error instanceof BitfabError) {
737
+ throw error;
738
+ }
739
+ if (error instanceof Error) {
740
+ if (error.name === "AbortError") {
741
+ throw new BitfabError("Request timed out after 30000ms");
742
+ }
743
+ throw new BitfabError(error.message);
744
+ }
745
+ throw new BitfabError("Unknown error occurred");
746
+ } finally {
747
+ clearTimeout(timeoutId);
748
+ }
749
+ }
750
+ /**
751
+ * Mark a replay test run as completed.
752
+ * Blocking call.
753
+ */
754
+ async completeReplay(testRunId) {
755
+ return this.request(
756
+ "/api/sdk/replay/complete",
757
+ { testRunId },
758
+ { timeout: 3e4 }
759
+ );
760
+ }
761
+ /**
762
+ * Ask the server to materialize a per-trace DB branch lease from a
763
+ * captured `dbSnapshotRef`. Blocking — the resolver creates a Neon
764
+ * snapshot + preview branch and polls operations to readiness, which
765
+ * can take seconds.
766
+ */
767
+ async resolveDbBranchLease(testRunId, traceId, dbSnapshotRef) {
768
+ return this.request(
769
+ "/api/sdk/replay/resolveDbBranchLease",
770
+ { testRunId, traceId, dbSnapshotRef },
771
+ { timeout: 9e4 }
772
+ );
773
+ }
774
+ /** Release a previously-resolved DB branch by deleting its Neon branch. Idempotent server-side. */
775
+ async releaseDbBranchLease(neonBranchId) {
776
+ await this.request(
777
+ "/api/sdk/replay/releaseDbBranchLease",
778
+ { neonBranchId },
779
+ { timeout: 3e4 }
780
+ );
781
+ }
782
+ };
783
+
766
784
  // src/claudeAgentSdk.ts
767
- init_constants();
768
- init_http();
769
785
  function nowIso() {
770
786
  return (/* @__PURE__ */ new Date()).toISOString();
771
787
  }
@@ -1530,9 +1546,6 @@ async function runFunctionWithBaml(bamlSource, inputs, providers, envVars) {
1530
1546
  };
1531
1547
  }
1532
1548
 
1533
- // src/client.ts
1534
- init_constants();
1535
-
1536
1549
  // src/dbSnapshot.ts
1537
1550
  init_errors();
1538
1551
  var SUPPORTED_PROVIDERS = ["neon"];
@@ -1550,12 +1563,7 @@ function buildSnapshotRef(config, sdkWallClockBeforeFn) {
1550
1563
  };
1551
1564
  }
1552
1565
 
1553
- // src/client.ts
1554
- init_http();
1555
-
1556
1566
  // src/langgraph.ts
1557
- init_constants();
1558
- init_http();
1559
1567
  var LANGSMITH_HIDDEN_TAG = "langsmith:hidden";
1560
1568
  var LANGGRAPH_METADATA_KEYS = [
1561
1569
  "langgraph_step",
@@ -2103,8 +2111,6 @@ var ReplayEnvironment = class {
2103
2111
  init_serialize();
2104
2112
 
2105
2113
  // src/tracing.ts
2106
- init_constants();
2107
- init_http();
2108
2114
  var BitfabOpenAITracingProcessor = class {
2109
2115
  /**
2110
2116
  * Initialize the tracing processor.
@@ -2938,9 +2944,18 @@ var Bitfab = class {
2938
2944
  spanType: options.type ?? "custom"
2939
2945
  };
2940
2946
  const sendSpan = async (params) => {
2947
+ const replayCtx = getReplayContext();
2948
+ const persistenceCollector = isRootSpan ? replayCtx?.pendingPersistence : void 0;
2949
+ let resolvePersistence;
2950
+ if (persistenceCollector) {
2951
+ persistenceCollector.push(
2952
+ new Promise((resolve) => {
2953
+ resolvePersistence = resolve;
2954
+ })
2955
+ );
2956
+ }
2941
2957
  try {
2942
2958
  const endedAt = (/* @__PURE__ */ new Date()).toISOString();
2943
- const replayCtx = getReplayContext();
2944
2959
  const spanPromise = self.sendWrapperSpan({
2945
2960
  ...baseSpanParams,
2946
2961
  ...params,
@@ -2955,13 +2970,17 @@ var Bitfab = class {
2955
2970
  if (isRootSpan) {
2956
2971
  const pending = pendingSpanPromises.get(traceId) ?? [];
2957
2972
  pending.push(spanPromise);
2958
- await Promise.race([
2959
- Promise.allSettled(pending),
2960
- new Promise((resolve) => setTimeout(resolve, 5e3))
2961
- ]);
2973
+ if (persistenceCollector) {
2974
+ await Promise.allSettled(pending);
2975
+ } else {
2976
+ await Promise.race([
2977
+ Promise.allSettled(pending),
2978
+ new Promise((resolve) => setTimeout(resolve, 5e3))
2979
+ ]);
2980
+ }
2962
2981
  pendingSpanPromises.delete(traceId);
2963
2982
  const traceState = activeTraceStates.get(traceId);
2964
- self.sendTraceCompletion({
2983
+ const completionPromise = self.sendTraceCompletion({
2965
2984
  traceFunctionKey,
2966
2985
  traceId,
2967
2986
  startedAt: traceState?.startedAt ?? startedAt,
@@ -2974,6 +2993,9 @@ var Bitfab = class {
2974
2993
  dbSnapshotRef: traceState?.dbSnapshotRef
2975
2994
  });
2976
2995
  activeTraceStates.delete(traceId);
2996
+ if (persistenceCollector) {
2997
+ await completionPromise;
2998
+ }
2977
2999
  } else {
2978
3000
  const pending = pendingSpanPromises.get(traceId);
2979
3001
  if (pending) {
@@ -2983,6 +3005,8 @@ var Bitfab = class {
2983
3005
  }
2984
3006
  }
2985
3007
  } catch {
3008
+ } finally {
3009
+ resolvePersistence?.();
2986
3010
  }
2987
3011
  };
2988
3012
  const replayCtxForMock = getReplayContext();
@@ -3135,7 +3159,7 @@ var Bitfab = class {
3135
3159
  if (params.dbSnapshotRef) {
3136
3160
  rawTrace.db_snapshot_ref = params.dbSnapshotRef;
3137
3161
  }
3138
- this.httpClient.sendExternalTrace({
3162
+ return this.httpClient.sendExternalTrace({
3139
3163
  type: "sdk-function",
3140
3164
  source: "typescript-sdk-function",
3141
3165
  traceFunctionKey: params.traceFunctionKey,
@@ -3280,10 +3304,6 @@ var BitfabFunction = class {
3280
3304
  }
3281
3305
  };
3282
3306
 
3283
- // src/index.ts
3284
- init_constants();
3285
- init_http();
3286
-
3287
3307
  // src/node.ts
3288
3308
  init_asyncStorage();
3289
3309
  assertAsyncStorageRegistered();