@bitfab/sdk 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
@@ -11,10 +11,10 @@ import {
11
11
  flushTraces,
12
12
  getCurrentSpan,
13
13
  getCurrentTrace
14
- } from "./chunk-UTVFKV2Z.js";
14
+ } from "./chunk-WZ72P5SX.js";
15
15
  import {
16
16
  BitfabError
17
- } from "./chunk-PY6V4FE3.js";
17
+ } from "./chunk-EQI6ZJC3.js";
18
18
  export {
19
19
  Bitfab,
20
20
  BitfabClaudeAgentHandler,
package/dist/node.cjs CHANGED
@@ -91,28 +91,6 @@ var init_errors = __esm({
91
91
  }
92
92
  });
93
93
 
94
- // src/replayContext.ts
95
- function getReplayContext() {
96
- return replayContextStorage?.getStore() ?? null;
97
- }
98
- function runWithReplayContext(ctx, fn) {
99
- if (replayContextStorage) {
100
- return replayContextStorage.run(ctx, fn);
101
- }
102
- return fn();
103
- }
104
- var replayContextStorage, replayContextReady;
105
- var init_replayContext = __esm({
106
- "src/replayContext.ts"() {
107
- "use strict";
108
- init_asyncStorage();
109
- replayContextStorage = null;
110
- replayContextReady = asyncStorageReady.then(() => {
111
- replayContextStorage = createAsyncLocalStorage();
112
- });
113
- }
114
- });
115
-
116
94
  // src/serialize.ts
117
95
  function describeValue(value) {
118
96
  try {
@@ -163,12 +141,89 @@ function deserializeValue(serialized) {
163
141
  meta: serialized.meta
164
142
  });
165
143
  }
166
- var import_superjson, MAX_SERIALIZED_BYTES;
144
+ function toJsonSafe(value) {
145
+ return toJsonSafeInner(value, 0, /* @__PURE__ */ new WeakSet());
146
+ }
147
+ function toJsonSafeInner(value, depth, seen) {
148
+ if (value === null || value === void 0) {
149
+ return value;
150
+ }
151
+ if (typeof value === "string" || typeof value === "number" || typeof value === "boolean") {
152
+ return value;
153
+ }
154
+ const className = value?.constructor?.name ?? typeof value;
155
+ if (depth > MAX_SAFE_DEPTH) {
156
+ return `<${className}>`;
157
+ }
158
+ if (typeof value !== "object") {
159
+ try {
160
+ return String(value);
161
+ } catch {
162
+ return `<${className}>`;
163
+ }
164
+ }
165
+ if (seen.has(value)) {
166
+ return `<cycle ${className}>`;
167
+ }
168
+ seen.add(value);
169
+ let result;
170
+ if (Array.isArray(value)) {
171
+ result = value.map((item) => toJsonSafeInner(item, depth + 1, seen));
172
+ } else if (typeof value.toJSON === "function") {
173
+ try {
174
+ result = toJsonSafeInner(
175
+ value.toJSON(),
176
+ depth + 1,
177
+ seen
178
+ );
179
+ } catch {
180
+ result = `<${className}>`;
181
+ }
182
+ } else {
183
+ try {
184
+ const obj = {};
185
+ for (const [k, v] of Object.entries(value)) {
186
+ if (!k.startsWith("_")) {
187
+ obj[k] = toJsonSafeInner(v, depth + 1, seen);
188
+ }
189
+ }
190
+ result = obj;
191
+ } catch {
192
+ result = `<${className}>`;
193
+ }
194
+ }
195
+ seen.delete(value);
196
+ return result;
197
+ }
198
+ var import_superjson, MAX_SERIALIZED_BYTES, MAX_SAFE_DEPTH;
167
199
  var init_serialize = __esm({
168
200
  "src/serialize.ts"() {
169
201
  "use strict";
170
202
  import_superjson = __toESM(require("superjson"), 1);
171
203
  MAX_SERIALIZED_BYTES = 512e3;
204
+ MAX_SAFE_DEPTH = 6;
205
+ }
206
+ });
207
+
208
+ // src/replayContext.ts
209
+ function getReplayContext() {
210
+ return replayContextStorage?.getStore() ?? null;
211
+ }
212
+ function runWithReplayContext(ctx, fn) {
213
+ if (replayContextStorage) {
214
+ return replayContextStorage.run(ctx, fn);
215
+ }
216
+ return fn();
217
+ }
218
+ var replayContextStorage, replayContextReady;
219
+ var init_replayContext = __esm({
220
+ "src/replayContext.ts"() {
221
+ "use strict";
222
+ init_asyncStorage();
223
+ replayContextStorage = null;
224
+ replayContextReady = asyncStorageReady.then(() => {
225
+ replayContextStorage = createAsyncLocalStorage();
226
+ });
172
227
  }
173
228
  });
174
229
 
@@ -447,13 +502,93 @@ registerAsyncLocalStorageClass(
447
502
  );
448
503
 
449
504
  // src/version.generated.ts
450
- var __version__ = "0.18.2";
505
+ var __version__ = "0.19.1";
451
506
 
452
507
  // src/constants.ts
453
508
  var DEFAULT_SERVICE_URL = "https://bitfab.ai";
454
509
 
455
510
  // src/http.ts
456
511
  init_errors();
512
+ function serializePayloadBody(payload) {
513
+ try {
514
+ return { body: JSON.stringify(payload), dropped: [] };
515
+ } catch {
516
+ const dropped = [];
517
+ const sanitize = (value, seen) => {
518
+ const t = typeof value;
519
+ if (value === null || t === "string" || t === "number" || t === "boolean") {
520
+ return value;
521
+ }
522
+ if (t === "bigint") {
523
+ dropped.push("BigInt");
524
+ return "<unserializable: BigInt>";
525
+ }
526
+ if (t === "function") {
527
+ const name = value.name || "Function";
528
+ dropped.push(name);
529
+ return `<unserializable: ${name}>`;
530
+ }
531
+ if (t === "symbol") {
532
+ dropped.push("Symbol");
533
+ return "<unserializable: Symbol>";
534
+ }
535
+ if (t !== "object") {
536
+ return void 0;
537
+ }
538
+ const obj = value;
539
+ const className = obj.constructor?.name || "object";
540
+ if (seen.has(obj)) {
541
+ dropped.push(className);
542
+ return `<cycle: ${className}>`;
543
+ }
544
+ seen.add(obj);
545
+ let result;
546
+ if (Array.isArray(obj)) {
547
+ result = obj.map((item) => sanitize(item, seen));
548
+ } else if (typeof obj.toJSON === "function") {
549
+ try {
550
+ result = sanitize(obj.toJSON(), seen);
551
+ } catch {
552
+ dropped.push(className);
553
+ result = `<unserializable: ${className}>`;
554
+ }
555
+ } else {
556
+ const out = {};
557
+ for (const [k, v] of Object.entries(obj)) {
558
+ out[k] = sanitize(v, seen);
559
+ }
560
+ result = out;
561
+ }
562
+ seen.delete(obj);
563
+ return result;
564
+ };
565
+ let sanitized;
566
+ try {
567
+ sanitized = sanitize(payload, /* @__PURE__ */ new WeakSet());
568
+ } catch (error) {
569
+ const message = error instanceof Error ? error.message : String(error);
570
+ return {
571
+ body: JSON.stringify({ error: `payload_serialize_failed: ${message}` }),
572
+ dropped
573
+ };
574
+ }
575
+ if (dropped.length > 0 && typeof sanitized === "object" && sanitized !== null && !Array.isArray(sanitized)) {
576
+ const obj = sanitized;
577
+ const existing = Array.isArray(obj.errors) ? obj.errors : [];
578
+ obj.errors = [
579
+ ...existing,
580
+ {
581
+ source: "sdk",
582
+ step: "json_serialize",
583
+ error: `stubbed non-serializable value(s): ${[
584
+ ...new Set(dropped)
585
+ ].join(", ")}`
586
+ }
587
+ ];
588
+ }
589
+ return { body: JSON.stringify(sanitized), dropped };
590
+ }
591
+ }
457
592
  var pendingTracePromises = /* @__PURE__ */ new Set();
458
593
  function awaitOnExit(promise) {
459
594
  pendingTracePromises.add(promise);
@@ -512,23 +647,14 @@ var HttpClient = class {
512
647
  const method = options?.method ?? "POST";
513
648
  const controller = new AbortController();
514
649
  const timeoutId = setTimeout(() => controller.abort(), timeout);
515
- let body;
516
- let serializationError;
517
- try {
518
- body = JSON.stringify(payload);
519
- } catch (error) {
520
- serializationError = error instanceof Error ? error.message : String(error);
521
- body = JSON.stringify({
522
- ...Object.fromEntries(
523
- Object.entries(payload).filter(
524
- ([, v]) => typeof v === "string" || typeof v === "number"
525
- )
526
- ),
527
- rawSpan: {},
528
- errors: [
529
- { source: "sdk", step: "json_serialize", error: serializationError }
530
- ]
531
- });
650
+ const { body, dropped } = serializePayloadBody(payload);
651
+ if (dropped.length > 0) {
652
+ try {
653
+ console.warn(
654
+ `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.`
655
+ );
656
+ } catch {
657
+ }
532
658
  }
533
659
  try {
534
660
  const response = await fetch(url, {
@@ -786,56 +912,41 @@ var HttpClient = class {
786
912
  };
787
913
 
788
914
  // src/claudeAgentSdk.ts
915
+ init_serialize();
789
916
  function nowIso() {
790
917
  return (/* @__PURE__ */ new Date()).toISOString();
791
918
  }
792
- function safeSerialize(value) {
793
- if (value === null || value === void 0) {
794
- return value;
795
- }
796
- if (typeof value === "string" || typeof value === "number" || typeof value === "boolean") {
797
- return value;
798
- }
799
- if (Array.isArray(value)) {
800
- return value.map(safeSerialize);
801
- }
802
- if (typeof value === "object") {
803
- if (typeof value.toJSON === "function") {
804
- return value.toJSON();
805
- }
806
- const result = {};
807
- for (const [k, v] of Object.entries(value)) {
808
- if (!k.startsWith("_")) {
809
- result[k] = safeSerialize(v);
810
- }
811
- }
812
- return result;
813
- }
814
- return String(value);
815
- }
919
+ var safeSerialize = toJsonSafe;
816
920
  function extractContentBlocks(content) {
817
921
  if (!Array.isArray(content)) {
818
922
  return [];
819
923
  }
820
924
  return content.map((block) => safeSerialize(block));
821
925
  }
926
+ function asTokenCount(val) {
927
+ return typeof val === "number" && Number.isFinite(val) ? val : null;
928
+ }
822
929
  function extractUsage(message) {
823
930
  const usageInfo = {};
824
931
  const usage = message.usage;
825
932
  if (!usage) {
826
933
  return usageInfo;
827
934
  }
828
- const mapping = {
829
- input_tokens: "inputTokens",
830
- output_tokens: "outputTokens",
831
- cache_read_input_tokens: "cacheReadTokens",
832
- cache_creation_input_tokens: "cacheCreationTokens"
833
- };
834
- for (const [srcKey, dstKey] of Object.entries(mapping)) {
835
- const val = usage[srcKey];
836
- if (val !== void 0 && val !== null) {
837
- usageInfo[dstKey] = val;
838
- }
935
+ const baseInput = asTokenCount(usage.input_tokens);
936
+ const cacheRead = asTokenCount(usage.cache_read_input_tokens);
937
+ const cacheCreation = asTokenCount(usage.cache_creation_input_tokens);
938
+ if (baseInput !== null || cacheRead !== null || cacheCreation !== null) {
939
+ usageInfo.inputTokens = (baseInput ?? 0) + (cacheRead ?? 0) + (cacheCreation ?? 0);
940
+ }
941
+ const output = asTokenCount(usage.output_tokens);
942
+ if (output !== null) {
943
+ usageInfo.outputTokens = output;
944
+ }
945
+ if (cacheRead !== null) {
946
+ usageInfo.cacheReadTokens = cacheRead;
947
+ }
948
+ if (cacheCreation !== null) {
949
+ usageInfo.cacheCreationTokens = cacheCreation;
839
950
  }
840
951
  return usageInfo;
841
952
  }
@@ -1568,6 +1679,7 @@ function buildSnapshotRef(config, sdkWallClockBeforeFn) {
1568
1679
  }
1569
1680
 
1570
1681
  // src/langgraph.ts
1682
+ init_serialize();
1571
1683
  var LANGSMITH_HIDDEN_TAG = "langsmith:hidden";
1572
1684
  var LANGGRAPH_METADATA_KEYS = [
1573
1685
  "langgraph_step",
@@ -1579,56 +1691,7 @@ var LANGGRAPH_METADATA_KEYS = [
1579
1691
  function nowIso2() {
1580
1692
  return (/* @__PURE__ */ new Date()).toISOString();
1581
1693
  }
1582
- var MAX_SERIALIZE_DEPTH = 6;
1583
- function safeSerialize2(value) {
1584
- return safeSerializeInner(value, 0, /* @__PURE__ */ new WeakSet());
1585
- }
1586
- function safeSerializeInner(value, depth, seen) {
1587
- if (value === null || value === void 0) {
1588
- return value;
1589
- }
1590
- if (typeof value === "string" || typeof value === "number" || typeof value === "boolean") {
1591
- return value;
1592
- }
1593
- const className = value?.constructor?.name ?? typeof value;
1594
- if (depth > MAX_SERIALIZE_DEPTH) {
1595
- return `<${className}>`;
1596
- }
1597
- if (typeof value === "object") {
1598
- if (seen.has(value)) {
1599
- return `<cycle ${className}>`;
1600
- }
1601
- seen.add(value);
1602
- }
1603
- if (Array.isArray(value)) {
1604
- return value.map((item) => safeSerializeInner(item, depth + 1, seen));
1605
- }
1606
- if (typeof value === "object") {
1607
- if (typeof value.toJSON === "function") {
1608
- try {
1609
- return value.toJSON();
1610
- } catch {
1611
- return `<${className}>`;
1612
- }
1613
- }
1614
- try {
1615
- const result = {};
1616
- for (const [k, v] of Object.entries(value)) {
1617
- if (!k.startsWith("_")) {
1618
- result[k] = safeSerializeInner(v, depth + 1, seen);
1619
- }
1620
- }
1621
- return result;
1622
- } catch {
1623
- return `<${className}>`;
1624
- }
1625
- }
1626
- try {
1627
- return String(value);
1628
- } catch {
1629
- return `<${className}>`;
1630
- }
1631
- }
1694
+ var safeSerialize2 = toJsonSafe;
1632
1695
  function convertMessage(message) {
1633
1696
  if (typeof message !== "object" || message === null) {
1634
1697
  return { role: "unknown", content: String(message) };
@@ -1677,7 +1740,7 @@ function extractModelName(serialized, metadata) {
1677
1740
  }
1678
1741
  return void 0;
1679
1742
  }
1680
- function asTokenCount(value) {
1743
+ function asTokenCount2(value) {
1681
1744
  return typeof value === "number" && Number.isFinite(value) ? value : null;
1682
1745
  }
1683
1746
  function normalizeTokenUsage(raw) {
@@ -1686,10 +1749,10 @@ function normalizeTokenUsage(raw) {
1686
1749
  }
1687
1750
  const u = raw;
1688
1751
  if ("cache_read_input_tokens" in u || "cache_creation_input_tokens" in u) {
1689
- const cacheRead = asTokenCount(u.cache_read_input_tokens);
1690
- const cacheCreation = asTokenCount(u.cache_creation_input_tokens);
1691
- const baseInput = asTokenCount(u.input_tokens);
1692
- const outputTokens = asTokenCount(u.output_tokens);
1752
+ const cacheRead = asTokenCount2(u.cache_read_input_tokens);
1753
+ const cacheCreation = asTokenCount2(u.cache_creation_input_tokens);
1754
+ const baseInput = asTokenCount2(u.input_tokens);
1755
+ const outputTokens = asTokenCount2(u.output_tokens);
1693
1756
  if (cacheRead === null && cacheCreation === null && baseInput === null && outputTokens === null) {
1694
1757
  return null;
1695
1758
  }
@@ -1704,25 +1767,25 @@ function normalizeTokenUsage(raw) {
1704
1767
  if ("prompt_tokens" in u || "completion_tokens" in u || "promptTokens" in u || "completionTokens" in u) {
1705
1768
  const promptDetails = u.prompt_tokens_details ?? {};
1706
1769
  return withAnyTokenCount({
1707
- inputTokens: asTokenCount(u.prompt_tokens) ?? asTokenCount(u.promptTokens),
1708
- outputTokens: asTokenCount(u.completion_tokens) ?? asTokenCount(u.completionTokens),
1709
- totalTokens: asTokenCount(u.total_tokens) ?? asTokenCount(u.totalTokens),
1710
- cachedInputTokens: asTokenCount(promptDetails.cached_tokens)
1770
+ inputTokens: asTokenCount2(u.prompt_tokens) ?? asTokenCount2(u.promptTokens),
1771
+ outputTokens: asTokenCount2(u.completion_tokens) ?? asTokenCount2(u.completionTokens),
1772
+ totalTokens: asTokenCount2(u.total_tokens) ?? asTokenCount2(u.totalTokens),
1773
+ cachedInputTokens: asTokenCount2(promptDetails.cached_tokens)
1711
1774
  });
1712
1775
  }
1713
1776
  if ("prompt_token_count" in u || "candidates_token_count" in u) {
1714
1777
  return withAnyTokenCount({
1715
- inputTokens: asTokenCount(u.prompt_token_count),
1716
- outputTokens: asTokenCount(u.candidates_token_count),
1717
- totalTokens: asTokenCount(u.total_token_count),
1718
- cachedInputTokens: asTokenCount(u.cached_content_token_count)
1778
+ inputTokens: asTokenCount2(u.prompt_token_count),
1779
+ outputTokens: asTokenCount2(u.candidates_token_count),
1780
+ totalTokens: asTokenCount2(u.total_token_count),
1781
+ cachedInputTokens: asTokenCount2(u.cached_content_token_count)
1719
1782
  });
1720
1783
  }
1721
1784
  if ("input_tokens" in u || "output_tokens" in u) {
1722
1785
  const inputDetails = u.input_token_details ?? {};
1723
- const inputTokens = asTokenCount(u.input_tokens);
1724
- const outputTokens = asTokenCount(u.output_tokens);
1725
- let totalTokens = asTokenCount(u.total_tokens);
1786
+ const inputTokens = asTokenCount2(u.input_tokens);
1787
+ const outputTokens = asTokenCount2(u.output_tokens);
1788
+ let totalTokens = asTokenCount2(u.total_tokens);
1726
1789
  if (totalTokens === null && inputTokens !== null && outputTokens !== null) {
1727
1790
  totalTokens = inputTokens + outputTokens;
1728
1791
  }
@@ -1730,7 +1793,7 @@ function normalizeTokenUsage(raw) {
1730
1793
  inputTokens,
1731
1794
  outputTokens,
1732
1795
  totalTokens,
1733
- cachedInputTokens: asTokenCount(inputDetails.cache_read)
1796
+ cachedInputTokens: asTokenCount2(inputDetails.cache_read)
1734
1797
  });
1735
1798
  }
1736
1799
  return null;