@bitfab/sdk 0.18.1 → 0.19.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
@@ -45,6 +45,120 @@ var init_errors = __esm({
45
45
  }
46
46
  });
47
47
 
48
+ // src/serialize.ts
49
+ function describeValue(value) {
50
+ try {
51
+ const ctorName = value?.constructor?.name;
52
+ if (ctorName && ctorName !== "Object") {
53
+ return ctorName;
54
+ }
55
+ } catch {
56
+ }
57
+ return typeof value;
58
+ }
59
+ function unserializableStub(value, reason) {
60
+ let summary;
61
+ try {
62
+ summary = `<unserializable: ${describeValue(value)} (${reason})>`;
63
+ } catch {
64
+ summary = `<unserializable (${reason})>`;
65
+ }
66
+ return { json: summary };
67
+ }
68
+ function serializeValue(value) {
69
+ try {
70
+ const { json, meta } = import_superjson.default.serialize(value);
71
+ let size;
72
+ try {
73
+ size = JSON.stringify(json).length;
74
+ } catch {
75
+ return unserializableStub(value, "stringify_failed_after_superjson");
76
+ }
77
+ if (size > MAX_SERIALIZED_BYTES) {
78
+ return unserializableStub(value, `too_large_${size}_bytes`);
79
+ }
80
+ return meta ? { json, meta } : { json };
81
+ } catch {
82
+ try {
83
+ return { json: JSON.parse(JSON.stringify(value)) };
84
+ } catch {
85
+ return unserializableStub(value, "json_stringify_failed");
86
+ }
87
+ }
88
+ }
89
+ function deserializeValue(serialized) {
90
+ if (serialized.meta === void 0) {
91
+ return serialized.json;
92
+ }
93
+ return import_superjson.default.deserialize({
94
+ json: serialized.json,
95
+ meta: serialized.meta
96
+ });
97
+ }
98
+ function toJsonSafe(value) {
99
+ return toJsonSafeInner(value, 0, /* @__PURE__ */ new WeakSet());
100
+ }
101
+ function toJsonSafeInner(value, depth, seen) {
102
+ if (value === null || value === void 0) {
103
+ return value;
104
+ }
105
+ if (typeof value === "string" || typeof value === "number" || typeof value === "boolean") {
106
+ return value;
107
+ }
108
+ const className = value?.constructor?.name ?? typeof value;
109
+ if (depth > MAX_SAFE_DEPTH) {
110
+ return `<${className}>`;
111
+ }
112
+ if (typeof value !== "object") {
113
+ try {
114
+ return String(value);
115
+ } catch {
116
+ return `<${className}>`;
117
+ }
118
+ }
119
+ if (seen.has(value)) {
120
+ return `<cycle ${className}>`;
121
+ }
122
+ seen.add(value);
123
+ let result;
124
+ if (Array.isArray(value)) {
125
+ result = value.map((item) => toJsonSafeInner(item, depth + 1, seen));
126
+ } else if (typeof value.toJSON === "function") {
127
+ try {
128
+ result = toJsonSafeInner(
129
+ value.toJSON(),
130
+ depth + 1,
131
+ seen
132
+ );
133
+ } catch {
134
+ result = `<${className}>`;
135
+ }
136
+ } else {
137
+ try {
138
+ const obj = {};
139
+ for (const [k, v] of Object.entries(value)) {
140
+ if (!k.startsWith("_")) {
141
+ obj[k] = toJsonSafeInner(v, depth + 1, seen);
142
+ }
143
+ }
144
+ result = obj;
145
+ } catch {
146
+ result = `<${className}>`;
147
+ }
148
+ }
149
+ seen.delete(value);
150
+ return result;
151
+ }
152
+ var import_superjson, MAX_SERIALIZED_BYTES, MAX_SAFE_DEPTH;
153
+ var init_serialize = __esm({
154
+ "src/serialize.ts"() {
155
+ "use strict";
156
+ import_superjson = __toESM(require("superjson"), 1);
157
+ MAX_SERIALIZED_BYTES = 512e3;
158
+ MAX_SAFE_DEPTH = 6;
159
+ }
160
+ });
161
+
48
162
  // src/asyncStorage.ts
49
163
  function registerAsyncLocalStorageClass(cls) {
50
164
  if (!AsyncLocalStorageClass) {
@@ -106,65 +220,6 @@ var init_replayContext = __esm({
106
220
  }
107
221
  });
108
222
 
109
- // src/serialize.ts
110
- function describeValue(value) {
111
- try {
112
- const ctorName = value?.constructor?.name;
113
- if (ctorName && ctorName !== "Object") {
114
- return ctorName;
115
- }
116
- } catch {
117
- }
118
- return typeof value;
119
- }
120
- function unserializableStub(value, reason) {
121
- let summary;
122
- try {
123
- summary = `<unserializable: ${describeValue(value)} (${reason})>`;
124
- } catch {
125
- summary = `<unserializable (${reason})>`;
126
- }
127
- return { json: summary };
128
- }
129
- function serializeValue(value) {
130
- try {
131
- const { json, meta } = import_superjson.default.serialize(value);
132
- let size;
133
- try {
134
- size = JSON.stringify(json).length;
135
- } catch {
136
- return unserializableStub(value, "stringify_failed_after_superjson");
137
- }
138
- if (size > MAX_SERIALIZED_BYTES) {
139
- return unserializableStub(value, `too_large_${size}_bytes`);
140
- }
141
- return meta ? { json, meta } : { json };
142
- } catch {
143
- try {
144
- return { json: JSON.parse(JSON.stringify(value)) };
145
- } catch {
146
- return unserializableStub(value, "json_stringify_failed");
147
- }
148
- }
149
- }
150
- function deserializeValue(serialized) {
151
- if (serialized.meta === void 0) {
152
- return serialized.json;
153
- }
154
- return import_superjson.default.deserialize({
155
- json: serialized.json,
156
- meta: serialized.meta
157
- });
158
- }
159
- var import_superjson, MAX_SERIALIZED_BYTES;
160
- var init_serialize = __esm({
161
- "src/serialize.ts"() {
162
- "use strict";
163
- import_superjson = __toESM(require("superjson"), 1);
164
- MAX_SERIALIZED_BYTES = 512e3;
165
- }
166
- });
167
-
168
223
  // src/replay.ts
169
224
  var replay_exports = {};
170
225
  __export(replay_exports, {
@@ -433,13 +488,93 @@ __export(index_exports, {
433
488
  module.exports = __toCommonJS(index_exports);
434
489
 
435
490
  // src/version.generated.ts
436
- var __version__ = "0.18.1";
491
+ var __version__ = "0.19.0";
437
492
 
438
493
  // src/constants.ts
439
494
  var DEFAULT_SERVICE_URL = "https://bitfab.ai";
440
495
 
441
496
  // src/http.ts
442
497
  init_errors();
498
+ function serializePayloadBody(payload) {
499
+ try {
500
+ return { body: JSON.stringify(payload), dropped: [] };
501
+ } catch {
502
+ const dropped = [];
503
+ const sanitize = (value, seen) => {
504
+ const t = typeof value;
505
+ if (value === null || t === "string" || t === "number" || t === "boolean") {
506
+ return value;
507
+ }
508
+ if (t === "bigint") {
509
+ dropped.push("BigInt");
510
+ return "<unserializable: BigInt>";
511
+ }
512
+ if (t === "function") {
513
+ const name = value.name || "Function";
514
+ dropped.push(name);
515
+ return `<unserializable: ${name}>`;
516
+ }
517
+ if (t === "symbol") {
518
+ dropped.push("Symbol");
519
+ return "<unserializable: Symbol>";
520
+ }
521
+ if (t !== "object") {
522
+ return void 0;
523
+ }
524
+ const obj = value;
525
+ const className = obj.constructor?.name || "object";
526
+ if (seen.has(obj)) {
527
+ dropped.push(className);
528
+ return `<cycle: ${className}>`;
529
+ }
530
+ seen.add(obj);
531
+ let result;
532
+ if (Array.isArray(obj)) {
533
+ result = obj.map((item) => sanitize(item, seen));
534
+ } else if (typeof obj.toJSON === "function") {
535
+ try {
536
+ result = sanitize(obj.toJSON(), seen);
537
+ } catch {
538
+ dropped.push(className);
539
+ result = `<unserializable: ${className}>`;
540
+ }
541
+ } else {
542
+ const out = {};
543
+ for (const [k, v] of Object.entries(obj)) {
544
+ out[k] = sanitize(v, seen);
545
+ }
546
+ result = out;
547
+ }
548
+ seen.delete(obj);
549
+ return result;
550
+ };
551
+ let sanitized;
552
+ try {
553
+ sanitized = sanitize(payload, /* @__PURE__ */ new WeakSet());
554
+ } catch (error) {
555
+ const message = error instanceof Error ? error.message : String(error);
556
+ return {
557
+ body: JSON.stringify({ error: `payload_serialize_failed: ${message}` }),
558
+ dropped
559
+ };
560
+ }
561
+ if (dropped.length > 0 && typeof sanitized === "object" && sanitized !== null && !Array.isArray(sanitized)) {
562
+ const obj = sanitized;
563
+ const existing = Array.isArray(obj.errors) ? obj.errors : [];
564
+ obj.errors = [
565
+ ...existing,
566
+ {
567
+ source: "sdk",
568
+ step: "json_serialize",
569
+ error: `stubbed non-serializable value(s): ${[
570
+ ...new Set(dropped)
571
+ ].join(", ")}`
572
+ }
573
+ ];
574
+ }
575
+ return { body: JSON.stringify(sanitized), dropped };
576
+ }
577
+ }
443
578
  var pendingTracePromises = /* @__PURE__ */ new Set();
444
579
  function awaitOnExit(promise) {
445
580
  pendingTracePromises.add(promise);
@@ -498,23 +633,14 @@ var HttpClient = class {
498
633
  const method = options?.method ?? "POST";
499
634
  const controller = new AbortController();
500
635
  const timeoutId = setTimeout(() => controller.abort(), timeout);
501
- let body;
502
- let serializationError;
503
- try {
504
- body = JSON.stringify(payload);
505
- } catch (error) {
506
- serializationError = error instanceof Error ? error.message : String(error);
507
- body = JSON.stringify({
508
- ...Object.fromEntries(
509
- Object.entries(payload).filter(
510
- ([, v]) => typeof v === "string" || typeof v === "number"
511
- )
512
- ),
513
- rawSpan: {},
514
- errors: [
515
- { source: "sdk", step: "json_serialize", error: serializationError }
516
- ]
517
- });
636
+ const { body, dropped } = serializePayloadBody(payload);
637
+ if (dropped.length > 0) {
638
+ try {
639
+ console.warn(
640
+ `Bitfab: request body to ${endpoint} held ${dropped.length} non-serializable value(s) (${[...new Set(dropped)].join(", ")}); they were stubbed so the span still sends, but the trace may be incomplete or not replayable. Capture a JSON-safe projection of this input to make it replayable.`
641
+ );
642
+ } catch {
643
+ }
518
644
  }
519
645
  try {
520
646
  const response = await fetch(url, {
@@ -772,33 +898,11 @@ var HttpClient = class {
772
898
  };
773
899
 
774
900
  // src/claudeAgentSdk.ts
901
+ init_serialize();
775
902
  function nowIso() {
776
903
  return (/* @__PURE__ */ new Date()).toISOString();
777
904
  }
778
- function safeSerialize(value) {
779
- if (value === null || value === void 0) {
780
- return value;
781
- }
782
- if (typeof value === "string" || typeof value === "number" || typeof value === "boolean") {
783
- return value;
784
- }
785
- if (Array.isArray(value)) {
786
- return value.map(safeSerialize);
787
- }
788
- if (typeof value === "object") {
789
- if (typeof value.toJSON === "function") {
790
- return value.toJSON();
791
- }
792
- const result = {};
793
- for (const [k, v] of Object.entries(value)) {
794
- if (!k.startsWith("_")) {
795
- result[k] = safeSerialize(v);
796
- }
797
- }
798
- return result;
799
- }
800
- return String(value);
801
- }
905
+ var safeSerialize = toJsonSafe;
802
906
  function extractContentBlocks(content) {
803
907
  if (!Array.isArray(content)) {
804
908
  return [];
@@ -1554,6 +1658,7 @@ function buildSnapshotRef(config, sdkWallClockBeforeFn) {
1554
1658
  }
1555
1659
 
1556
1660
  // src/langgraph.ts
1661
+ init_serialize();
1557
1662
  var LANGSMITH_HIDDEN_TAG = "langsmith:hidden";
1558
1663
  var LANGGRAPH_METADATA_KEYS = [
1559
1664
  "langgraph_step",
@@ -1565,56 +1670,7 @@ var LANGGRAPH_METADATA_KEYS = [
1565
1670
  function nowIso2() {
1566
1671
  return (/* @__PURE__ */ new Date()).toISOString();
1567
1672
  }
1568
- var MAX_SERIALIZE_DEPTH = 6;
1569
- function safeSerialize2(value) {
1570
- return safeSerializeInner(value, 0, /* @__PURE__ */ new WeakSet());
1571
- }
1572
- function safeSerializeInner(value, depth, seen) {
1573
- if (value === null || value === void 0) {
1574
- return value;
1575
- }
1576
- if (typeof value === "string" || typeof value === "number" || typeof value === "boolean") {
1577
- return value;
1578
- }
1579
- const className = value?.constructor?.name ?? typeof value;
1580
- if (depth > MAX_SERIALIZE_DEPTH) {
1581
- return `<${className}>`;
1582
- }
1583
- if (typeof value === "object") {
1584
- if (seen.has(value)) {
1585
- return `<cycle ${className}>`;
1586
- }
1587
- seen.add(value);
1588
- }
1589
- if (Array.isArray(value)) {
1590
- return value.map((item) => safeSerializeInner(item, depth + 1, seen));
1591
- }
1592
- if (typeof value === "object") {
1593
- if (typeof value.toJSON === "function") {
1594
- try {
1595
- return value.toJSON();
1596
- } catch {
1597
- return `<${className}>`;
1598
- }
1599
- }
1600
- try {
1601
- const result = {};
1602
- for (const [k, v] of Object.entries(value)) {
1603
- if (!k.startsWith("_")) {
1604
- result[k] = safeSerializeInner(v, depth + 1, seen);
1605
- }
1606
- }
1607
- return result;
1608
- } catch {
1609
- return `<${className}>`;
1610
- }
1611
- }
1612
- try {
1613
- return String(value);
1614
- } catch {
1615
- return `<${className}>`;
1616
- }
1617
- }
1673
+ var safeSerialize2 = toJsonSafe;
1618
1674
  function convertMessage(message) {
1619
1675
  if (typeof message !== "object" || message === null) {
1620
1676
  return { role: "unknown", content: String(message) };
@@ -2148,7 +2204,9 @@ var ReplayEnvironment = class {
2148
2204
  * Throws if read outside a replay item.
2149
2205
  */
2150
2206
  get databaseUrl() {
2151
- return this.require().databaseUrl;
2207
+ const snapshot = this.require();
2208
+ this.markAccessed();
2209
+ return snapshot.databaseUrl;
2152
2210
  }
2153
2211
  /** When the per-trace branch URL stops being valid. ISO-8601. */
2154
2212
  get expiresAt() {
@@ -2176,7 +2234,24 @@ var ReplayEnvironment = class {
2176
2234
  }
2177
2235
  /** Non-throwing variant for callers that handle the inactive case. */
2178
2236
  snapshot() {
2179
- return this.read();
2237
+ const snapshot = this.read();
2238
+ if (snapshot) {
2239
+ this.markAccessed();
2240
+ }
2241
+ return snapshot;
2242
+ }
2243
+ /**
2244
+ * Record on the replay context that customer code obtained the branch
2245
+ * URL. Only `databaseUrl` and `snapshot()` count — `active`, `readOnly`
2246
+ * and friends inspect the lease without exposing the connection string,
2247
+ * so they don't prove the replayed code could have connected to the
2248
+ * branch.
2249
+ */
2250
+ markAccessed() {
2251
+ const ctx = getReplayContext();
2252
+ if (ctx?.dbBranchLease) {
2253
+ ctx.dbSnapshotAccessed = true;
2254
+ }
2180
2255
  }
2181
2256
  read() {
2182
2257
  const ctx = getReplayContext();
@@ -3108,7 +3183,19 @@ var Bitfab = class {
3108
3183
  contexts: traceState?.contexts ?? [],
3109
3184
  testRunId: traceState?.testRunId,
3110
3185
  inputSourceTraceId: traceState?.inputSourceTraceId,
3111
- dbSnapshotRef: traceState?.dbSnapshotRef
3186
+ dbSnapshotRef: traceState?.dbSnapshotRef,
3187
+ // Built AFTER the wrapped fn finished, so `accessed` reflects
3188
+ // whether customer code obtained the branch URL during this
3189
+ // item. Omitted entirely when no lease was attached, so the
3190
+ // server can distinguish "no branch" from "branch ignored".
3191
+ ...replayCtx?.dbBranchLease && {
3192
+ dbSnapshotUsage: {
3193
+ neonBranchId: replayCtx.dbBranchLease.neonBranchId,
3194
+ snapshotTimestamp: replayCtx.dbBranchLease.snapshotTimestamp,
3195
+ sourceTraceId: replayCtx.sourceBitfabTraceId,
3196
+ accessed: replayCtx.dbSnapshotAccessed === true
3197
+ }
3198
+ }
3112
3199
  });
3113
3200
  activeTraceStates.delete(traceId);
3114
3201
  if (persistenceCollector) {
@@ -3280,6 +3367,18 @@ var Bitfab = class {
3280
3367
  if (params.dbSnapshotRef) {
3281
3368
  rawTrace.db_snapshot_ref = params.dbSnapshotRef;
3282
3369
  }
3370
+ if (params.dbSnapshotUsage) {
3371
+ rawTrace.db_snapshot_usage = {
3372
+ neon_branch_id: params.dbSnapshotUsage.neonBranchId,
3373
+ ...params.dbSnapshotUsage.snapshotTimestamp && {
3374
+ snapshot_timestamp: params.dbSnapshotUsage.snapshotTimestamp
3375
+ },
3376
+ ...params.dbSnapshotUsage.sourceTraceId && {
3377
+ source_trace_id: params.dbSnapshotUsage.sourceTraceId
3378
+ },
3379
+ accessed: params.dbSnapshotUsage.accessed
3380
+ };
3381
+ }
3283
3382
  return this.httpClient.sendExternalTrace({
3284
3383
  type: "sdk-function",
3285
3384
  source: "typescript-sdk-function",