bitfab 0.18.2 → 0.19.1

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
@@ -1063,7 +1063,7 @@ declare class BitfabFunction {
1063
1063
  /**
1064
1064
  * SDK version from package.json (injected at build time)
1065
1065
  */
1066
- declare const __version__ = "0.18.2";
1066
+ declare const __version__ = "0.19.1";
1067
1067
 
1068
1068
  /**
1069
1069
  * Constants for the Bitfab SDK.
package/dist/index.d.ts CHANGED
@@ -1063,7 +1063,7 @@ declare class BitfabFunction {
1063
1063
  /**
1064
1064
  * SDK version from package.json (injected at build time)
1065
1065
  */
1066
- declare const __version__ = "0.18.2";
1066
+ declare const __version__ = "0.19.1";
1067
1067
 
1068
1068
  /**
1069
1069
  * Constants for the Bitfab SDK.
package/dist/index.js CHANGED
@@ -20,10 +20,10 @@ import {
20
20
  flushTraces,
21
21
  getCurrentSpan,
22
22
  getCurrentTrace
23
- } from "./chunk-UTVFKV2Z.js";
23
+ } from "./chunk-WZ72P5SX.js";
24
24
  import {
25
25
  BitfabError
26
- } from "./chunk-PY6V4FE3.js";
26
+ } from "./chunk-EQI6ZJC3.js";
27
27
  export {
28
28
  Bitfab,
29
29
  BitfabClaudeAgentHandler,
package/dist/node.cjs CHANGED
@@ -100,28 +100,6 @@ var init_errors = __esm({
100
100
  }
101
101
  });
102
102
 
103
- // src/replayContext.ts
104
- function getReplayContext() {
105
- return replayContextStorage?.getStore() ?? null;
106
- }
107
- function runWithReplayContext(ctx, fn) {
108
- if (replayContextStorage) {
109
- return replayContextStorage.run(ctx, fn);
110
- }
111
- return fn();
112
- }
113
- var replayContextStorage, replayContextReady;
114
- var init_replayContext = __esm({
115
- "src/replayContext.ts"() {
116
- "use strict";
117
- init_asyncStorage();
118
- replayContextStorage = null;
119
- replayContextReady = asyncStorageReady.then(() => {
120
- replayContextStorage = createAsyncLocalStorage();
121
- });
122
- }
123
- });
124
-
125
103
  // src/serialize.ts
126
104
  function describeValue(value) {
127
105
  try {
@@ -172,12 +150,89 @@ function deserializeValue(serialized) {
172
150
  meta: serialized.meta
173
151
  });
174
152
  }
175
- var import_superjson, MAX_SERIALIZED_BYTES;
153
+ function toJsonSafe(value) {
154
+ return toJsonSafeInner(value, 0, /* @__PURE__ */ new WeakSet());
155
+ }
156
+ function toJsonSafeInner(value, depth, seen) {
157
+ if (value === null || value === void 0) {
158
+ return value;
159
+ }
160
+ if (typeof value === "string" || typeof value === "number" || typeof value === "boolean") {
161
+ return value;
162
+ }
163
+ const className = value?.constructor?.name ?? typeof value;
164
+ if (depth > MAX_SAFE_DEPTH) {
165
+ return `<${className}>`;
166
+ }
167
+ if (typeof value !== "object") {
168
+ try {
169
+ return String(value);
170
+ } catch {
171
+ return `<${className}>`;
172
+ }
173
+ }
174
+ if (seen.has(value)) {
175
+ return `<cycle ${className}>`;
176
+ }
177
+ seen.add(value);
178
+ let result;
179
+ if (Array.isArray(value)) {
180
+ result = value.map((item) => toJsonSafeInner(item, depth + 1, seen));
181
+ } else if (typeof value.toJSON === "function") {
182
+ try {
183
+ result = toJsonSafeInner(
184
+ value.toJSON(),
185
+ depth + 1,
186
+ seen
187
+ );
188
+ } catch {
189
+ result = `<${className}>`;
190
+ }
191
+ } else {
192
+ try {
193
+ const obj = {};
194
+ for (const [k, v] of Object.entries(value)) {
195
+ if (!k.startsWith("_")) {
196
+ obj[k] = toJsonSafeInner(v, depth + 1, seen);
197
+ }
198
+ }
199
+ result = obj;
200
+ } catch {
201
+ result = `<${className}>`;
202
+ }
203
+ }
204
+ seen.delete(value);
205
+ return result;
206
+ }
207
+ var import_superjson, MAX_SERIALIZED_BYTES, MAX_SAFE_DEPTH;
176
208
  var init_serialize = __esm({
177
209
  "src/serialize.ts"() {
178
210
  "use strict";
179
211
  import_superjson = __toESM(require("superjson"), 1);
180
212
  MAX_SERIALIZED_BYTES = 512e3;
213
+ MAX_SAFE_DEPTH = 6;
214
+ }
215
+ });
216
+
217
+ // src/replayContext.ts
218
+ function getReplayContext() {
219
+ return replayContextStorage?.getStore() ?? null;
220
+ }
221
+ function runWithReplayContext(ctx, fn) {
222
+ if (replayContextStorage) {
223
+ return replayContextStorage.run(ctx, fn);
224
+ }
225
+ return fn();
226
+ }
227
+ var replayContextStorage, replayContextReady;
228
+ var init_replayContext = __esm({
229
+ "src/replayContext.ts"() {
230
+ "use strict";
231
+ init_asyncStorage();
232
+ replayContextStorage = null;
233
+ replayContextReady = asyncStorageReady.then(() => {
234
+ replayContextStorage = createAsyncLocalStorage();
235
+ });
181
236
  }
182
237
  });
183
238
 
@@ -456,13 +511,93 @@ registerAsyncLocalStorageClass(
456
511
  );
457
512
 
458
513
  // src/version.generated.ts
459
- var __version__ = "0.18.2";
514
+ var __version__ = "0.19.1";
460
515
 
461
516
  // src/constants.ts
462
517
  var DEFAULT_SERVICE_URL = "https://bitfab.ai";
463
518
 
464
519
  // src/http.ts
465
520
  init_errors();
521
+ function serializePayloadBody(payload) {
522
+ try {
523
+ return { body: JSON.stringify(payload), dropped: [] };
524
+ } catch {
525
+ const dropped = [];
526
+ const sanitize = (value, seen) => {
527
+ const t = typeof value;
528
+ if (value === null || t === "string" || t === "number" || t === "boolean") {
529
+ return value;
530
+ }
531
+ if (t === "bigint") {
532
+ dropped.push("BigInt");
533
+ return "<unserializable: BigInt>";
534
+ }
535
+ if (t === "function") {
536
+ const name = value.name || "Function";
537
+ dropped.push(name);
538
+ return `<unserializable: ${name}>`;
539
+ }
540
+ if (t === "symbol") {
541
+ dropped.push("Symbol");
542
+ return "<unserializable: Symbol>";
543
+ }
544
+ if (t !== "object") {
545
+ return void 0;
546
+ }
547
+ const obj = value;
548
+ const className = obj.constructor?.name || "object";
549
+ if (seen.has(obj)) {
550
+ dropped.push(className);
551
+ return `<cycle: ${className}>`;
552
+ }
553
+ seen.add(obj);
554
+ let result;
555
+ if (Array.isArray(obj)) {
556
+ result = obj.map((item) => sanitize(item, seen));
557
+ } else if (typeof obj.toJSON === "function") {
558
+ try {
559
+ result = sanitize(obj.toJSON(), seen);
560
+ } catch {
561
+ dropped.push(className);
562
+ result = `<unserializable: ${className}>`;
563
+ }
564
+ } else {
565
+ const out = {};
566
+ for (const [k, v] of Object.entries(obj)) {
567
+ out[k] = sanitize(v, seen);
568
+ }
569
+ result = out;
570
+ }
571
+ seen.delete(obj);
572
+ return result;
573
+ };
574
+ let sanitized;
575
+ try {
576
+ sanitized = sanitize(payload, /* @__PURE__ */ new WeakSet());
577
+ } catch (error) {
578
+ const message = error instanceof Error ? error.message : String(error);
579
+ return {
580
+ body: JSON.stringify({ error: `payload_serialize_failed: ${message}` }),
581
+ dropped
582
+ };
583
+ }
584
+ if (dropped.length > 0 && typeof sanitized === "object" && sanitized !== null && !Array.isArray(sanitized)) {
585
+ const obj = sanitized;
586
+ const existing = Array.isArray(obj.errors) ? obj.errors : [];
587
+ obj.errors = [
588
+ ...existing,
589
+ {
590
+ source: "sdk",
591
+ step: "json_serialize",
592
+ error: `stubbed non-serializable value(s): ${[
593
+ ...new Set(dropped)
594
+ ].join(", ")}`
595
+ }
596
+ ];
597
+ }
598
+ return { body: JSON.stringify(sanitized), dropped };
599
+ }
600
+ }
466
601
  var pendingTracePromises = /* @__PURE__ */ new Set();
467
602
  function awaitOnExit(promise) {
468
603
  pendingTracePromises.add(promise);
@@ -521,23 +656,14 @@ var HttpClient = class {
521
656
  const method = options?.method ?? "POST";
522
657
  const controller = new AbortController();
523
658
  const timeoutId = setTimeout(() => controller.abort(), timeout);
524
- let body;
525
- let serializationError;
526
- try {
527
- body = JSON.stringify(payload);
528
- } catch (error) {
529
- serializationError = error instanceof Error ? error.message : String(error);
530
- body = JSON.stringify({
531
- ...Object.fromEntries(
532
- Object.entries(payload).filter(
533
- ([, v]) => typeof v === "string" || typeof v === "number"
534
- )
535
- ),
536
- rawSpan: {},
537
- errors: [
538
- { source: "sdk", step: "json_serialize", error: serializationError }
539
- ]
540
- });
659
+ const { body, dropped } = serializePayloadBody(payload);
660
+ if (dropped.length > 0) {
661
+ try {
662
+ console.warn(
663
+ `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.`
664
+ );
665
+ } catch {
666
+ }
541
667
  }
542
668
  try {
543
669
  const response = await fetch(url, {
@@ -795,56 +921,41 @@ var HttpClient = class {
795
921
  };
796
922
 
797
923
  // src/claudeAgentSdk.ts
924
+ init_serialize();
798
925
  function nowIso() {
799
926
  return (/* @__PURE__ */ new Date()).toISOString();
800
927
  }
801
- function safeSerialize(value) {
802
- if (value === null || value === void 0) {
803
- return value;
804
- }
805
- if (typeof value === "string" || typeof value === "number" || typeof value === "boolean") {
806
- return value;
807
- }
808
- if (Array.isArray(value)) {
809
- return value.map(safeSerialize);
810
- }
811
- if (typeof value === "object") {
812
- if (typeof value.toJSON === "function") {
813
- return value.toJSON();
814
- }
815
- const result = {};
816
- for (const [k, v] of Object.entries(value)) {
817
- if (!k.startsWith("_")) {
818
- result[k] = safeSerialize(v);
819
- }
820
- }
821
- return result;
822
- }
823
- return String(value);
824
- }
928
+ var safeSerialize = toJsonSafe;
825
929
  function extractContentBlocks(content) {
826
930
  if (!Array.isArray(content)) {
827
931
  return [];
828
932
  }
829
933
  return content.map((block) => safeSerialize(block));
830
934
  }
935
+ function asTokenCount(val) {
936
+ return typeof val === "number" && Number.isFinite(val) ? val : null;
937
+ }
831
938
  function extractUsage(message) {
832
939
  const usageInfo = {};
833
940
  const usage = message.usage;
834
941
  if (!usage) {
835
942
  return usageInfo;
836
943
  }
837
- const mapping = {
838
- input_tokens: "inputTokens",
839
- output_tokens: "outputTokens",
840
- cache_read_input_tokens: "cacheReadTokens",
841
- cache_creation_input_tokens: "cacheCreationTokens"
842
- };
843
- for (const [srcKey, dstKey] of Object.entries(mapping)) {
844
- const val = usage[srcKey];
845
- if (val !== void 0 && val !== null) {
846
- usageInfo[dstKey] = val;
847
- }
944
+ const baseInput = asTokenCount(usage.input_tokens);
945
+ const cacheRead = asTokenCount(usage.cache_read_input_tokens);
946
+ const cacheCreation = asTokenCount(usage.cache_creation_input_tokens);
947
+ if (baseInput !== null || cacheRead !== null || cacheCreation !== null) {
948
+ usageInfo.inputTokens = (baseInput ?? 0) + (cacheRead ?? 0) + (cacheCreation ?? 0);
949
+ }
950
+ const output = asTokenCount(usage.output_tokens);
951
+ if (output !== null) {
952
+ usageInfo.outputTokens = output;
953
+ }
954
+ if (cacheRead !== null) {
955
+ usageInfo.cacheReadTokens = cacheRead;
956
+ }
957
+ if (cacheCreation !== null) {
958
+ usageInfo.cacheCreationTokens = cacheCreation;
848
959
  }
849
960
  return usageInfo;
850
961
  }
@@ -1577,6 +1688,7 @@ function buildSnapshotRef(config, sdkWallClockBeforeFn) {
1577
1688
  }
1578
1689
 
1579
1690
  // src/langgraph.ts
1691
+ init_serialize();
1580
1692
  var LANGSMITH_HIDDEN_TAG = "langsmith:hidden";
1581
1693
  var LANGGRAPH_METADATA_KEYS = [
1582
1694
  "langgraph_step",
@@ -1588,56 +1700,7 @@ var LANGGRAPH_METADATA_KEYS = [
1588
1700
  function nowIso2() {
1589
1701
  return (/* @__PURE__ */ new Date()).toISOString();
1590
1702
  }
1591
- var MAX_SERIALIZE_DEPTH = 6;
1592
- function safeSerialize2(value) {
1593
- return safeSerializeInner(value, 0, /* @__PURE__ */ new WeakSet());
1594
- }
1595
- function safeSerializeInner(value, depth, seen) {
1596
- if (value === null || value === void 0) {
1597
- return value;
1598
- }
1599
- if (typeof value === "string" || typeof value === "number" || typeof value === "boolean") {
1600
- return value;
1601
- }
1602
- const className = value?.constructor?.name ?? typeof value;
1603
- if (depth > MAX_SERIALIZE_DEPTH) {
1604
- return `<${className}>`;
1605
- }
1606
- if (typeof value === "object") {
1607
- if (seen.has(value)) {
1608
- return `<cycle ${className}>`;
1609
- }
1610
- seen.add(value);
1611
- }
1612
- if (Array.isArray(value)) {
1613
- return value.map((item) => safeSerializeInner(item, depth + 1, seen));
1614
- }
1615
- if (typeof value === "object") {
1616
- if (typeof value.toJSON === "function") {
1617
- try {
1618
- return value.toJSON();
1619
- } catch {
1620
- return `<${className}>`;
1621
- }
1622
- }
1623
- try {
1624
- const result = {};
1625
- for (const [k, v] of Object.entries(value)) {
1626
- if (!k.startsWith("_")) {
1627
- result[k] = safeSerializeInner(v, depth + 1, seen);
1628
- }
1629
- }
1630
- return result;
1631
- } catch {
1632
- return `<${className}>`;
1633
- }
1634
- }
1635
- try {
1636
- return String(value);
1637
- } catch {
1638
- return `<${className}>`;
1639
- }
1640
- }
1703
+ var safeSerialize2 = toJsonSafe;
1641
1704
  function convertMessage(message) {
1642
1705
  if (typeof message !== "object" || message === null) {
1643
1706
  return { role: "unknown", content: String(message) };
@@ -1686,7 +1749,7 @@ function extractModelName(serialized, metadata) {
1686
1749
  }
1687
1750
  return void 0;
1688
1751
  }
1689
- function asTokenCount(value) {
1752
+ function asTokenCount2(value) {
1690
1753
  return typeof value === "number" && Number.isFinite(value) ? value : null;
1691
1754
  }
1692
1755
  function normalizeTokenUsage(raw) {
@@ -1695,10 +1758,10 @@ function normalizeTokenUsage(raw) {
1695
1758
  }
1696
1759
  const u = raw;
1697
1760
  if ("cache_read_input_tokens" in u || "cache_creation_input_tokens" in u) {
1698
- const cacheRead = asTokenCount(u.cache_read_input_tokens);
1699
- const cacheCreation = asTokenCount(u.cache_creation_input_tokens);
1700
- const baseInput = asTokenCount(u.input_tokens);
1701
- const outputTokens = asTokenCount(u.output_tokens);
1761
+ const cacheRead = asTokenCount2(u.cache_read_input_tokens);
1762
+ const cacheCreation = asTokenCount2(u.cache_creation_input_tokens);
1763
+ const baseInput = asTokenCount2(u.input_tokens);
1764
+ const outputTokens = asTokenCount2(u.output_tokens);
1702
1765
  if (cacheRead === null && cacheCreation === null && baseInput === null && outputTokens === null) {
1703
1766
  return null;
1704
1767
  }
@@ -1713,25 +1776,25 @@ function normalizeTokenUsage(raw) {
1713
1776
  if ("prompt_tokens" in u || "completion_tokens" in u || "promptTokens" in u || "completionTokens" in u) {
1714
1777
  const promptDetails = u.prompt_tokens_details ?? {};
1715
1778
  return withAnyTokenCount({
1716
- inputTokens: asTokenCount(u.prompt_tokens) ?? asTokenCount(u.promptTokens),
1717
- outputTokens: asTokenCount(u.completion_tokens) ?? asTokenCount(u.completionTokens),
1718
- totalTokens: asTokenCount(u.total_tokens) ?? asTokenCount(u.totalTokens),
1719
- cachedInputTokens: asTokenCount(promptDetails.cached_tokens)
1779
+ inputTokens: asTokenCount2(u.prompt_tokens) ?? asTokenCount2(u.promptTokens),
1780
+ outputTokens: asTokenCount2(u.completion_tokens) ?? asTokenCount2(u.completionTokens),
1781
+ totalTokens: asTokenCount2(u.total_tokens) ?? asTokenCount2(u.totalTokens),
1782
+ cachedInputTokens: asTokenCount2(promptDetails.cached_tokens)
1720
1783
  });
1721
1784
  }
1722
1785
  if ("prompt_token_count" in u || "candidates_token_count" in u) {
1723
1786
  return withAnyTokenCount({
1724
- inputTokens: asTokenCount(u.prompt_token_count),
1725
- outputTokens: asTokenCount(u.candidates_token_count),
1726
- totalTokens: asTokenCount(u.total_token_count),
1727
- cachedInputTokens: asTokenCount(u.cached_content_token_count)
1787
+ inputTokens: asTokenCount2(u.prompt_token_count),
1788
+ outputTokens: asTokenCount2(u.candidates_token_count),
1789
+ totalTokens: asTokenCount2(u.total_token_count),
1790
+ cachedInputTokens: asTokenCount2(u.cached_content_token_count)
1728
1791
  });
1729
1792
  }
1730
1793
  if ("input_tokens" in u || "output_tokens" in u) {
1731
1794
  const inputDetails = u.input_token_details ?? {};
1732
- const inputTokens = asTokenCount(u.input_tokens);
1733
- const outputTokens = asTokenCount(u.output_tokens);
1734
- let totalTokens = asTokenCount(u.total_tokens);
1795
+ const inputTokens = asTokenCount2(u.input_tokens);
1796
+ const outputTokens = asTokenCount2(u.output_tokens);
1797
+ let totalTokens = asTokenCount2(u.total_tokens);
1735
1798
  if (totalTokens === null && inputTokens !== null && outputTokens !== null) {
1736
1799
  totalTokens = inputTokens + outputTokens;
1737
1800
  }
@@ -1739,7 +1802,7 @@ function normalizeTokenUsage(raw) {
1739
1802
  inputTokens,
1740
1803
  outputTokens,
1741
1804
  totalTokens,
1742
- cachedInputTokens: asTokenCount(inputDetails.cache_read)
1805
+ cachedInputTokens: asTokenCount2(inputDetails.cache_read)
1743
1806
  });
1744
1807
  }
1745
1808
  return null;