bitfab 0.18.0 → 0.18.2

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
@@ -225,7 +225,7 @@ interface CodeChangeFile {
225
225
  * LangGraph/LangChain callback handler for Bitfab tracing.
226
226
  *
227
227
  * Hooks into LangGraph's callback system to capture graph node execution,
228
- * LLM calls, and tool invocations as Bitfab spans without requiring users
228
+ * LLM calls, and tool invocations as Bitfab spans, without requiring users
229
229
  * to wrap their functions with withSpan (which fails on non-serializable args).
230
230
  *
231
231
  * Duck-typed to match LangChain.js's BaseCallbackHandler interface.
@@ -238,8 +238,8 @@ interface ActiveSpanContext$1 {
238
238
  /**
239
239
  * LangChain/LangGraph callback handler that sends traces to Bitfab.
240
240
  *
241
- * Duck-typed to match LangChain.js's BaseCallbackHandler no
242
- * `@langchain/core` dependency required. Pass as a callback:
241
+ * Duck-typed to match LangChain.js's BaseCallbackHandler, so no
242
+ * `@langchain/core` dependency is required. Pass as a callback:
243
243
  *
244
244
  * ```typescript
245
245
  * const handler = bitfab.getLangGraphCallbackHandler("my-agent");
@@ -335,6 +335,14 @@ declare class ReplayEnvironment {
335
335
  get active(): boolean;
336
336
  /** Non-throwing variant for callers that handle the inactive case. */
337
337
  snapshot(): ReplayEnvironmentSnapshot | null;
338
+ /**
339
+ * Record on the replay context that customer code obtained the branch
340
+ * URL. Only `databaseUrl` and `snapshot()` count — `active`, `readOnly`
341
+ * and friends inspect the lease without exposing the connection string,
342
+ * so they don't prove the replayed code could have connected to the
343
+ * branch.
344
+ */
345
+ private markAccessed;
338
346
  private read;
339
347
  private require;
340
348
  }
@@ -1055,7 +1063,7 @@ declare class BitfabFunction {
1055
1063
  /**
1056
1064
  * SDK version from package.json (injected at build time)
1057
1065
  */
1058
- declare const __version__ = "0.18.0";
1066
+ declare const __version__ = "0.18.2";
1059
1067
 
1060
1068
  /**
1061
1069
  * Constants for the Bitfab SDK.
package/dist/index.d.ts CHANGED
@@ -225,7 +225,7 @@ interface CodeChangeFile {
225
225
  * LangGraph/LangChain callback handler for Bitfab tracing.
226
226
  *
227
227
  * Hooks into LangGraph's callback system to capture graph node execution,
228
- * LLM calls, and tool invocations as Bitfab spans without requiring users
228
+ * LLM calls, and tool invocations as Bitfab spans, without requiring users
229
229
  * to wrap their functions with withSpan (which fails on non-serializable args).
230
230
  *
231
231
  * Duck-typed to match LangChain.js's BaseCallbackHandler interface.
@@ -238,8 +238,8 @@ interface ActiveSpanContext$1 {
238
238
  /**
239
239
  * LangChain/LangGraph callback handler that sends traces to Bitfab.
240
240
  *
241
- * Duck-typed to match LangChain.js's BaseCallbackHandler no
242
- * `@langchain/core` dependency required. Pass as a callback:
241
+ * Duck-typed to match LangChain.js's BaseCallbackHandler, so no
242
+ * `@langchain/core` dependency is required. Pass as a callback:
243
243
  *
244
244
  * ```typescript
245
245
  * const handler = bitfab.getLangGraphCallbackHandler("my-agent");
@@ -335,6 +335,14 @@ declare class ReplayEnvironment {
335
335
  get active(): boolean;
336
336
  /** Non-throwing variant for callers that handle the inactive case. */
337
337
  snapshot(): ReplayEnvironmentSnapshot | null;
338
+ /**
339
+ * Record on the replay context that customer code obtained the branch
340
+ * URL. Only `databaseUrl` and `snapshot()` count — `active`, `readOnly`
341
+ * and friends inspect the lease without exposing the connection string,
342
+ * so they don't prove the replayed code could have connected to the
343
+ * branch.
344
+ */
345
+ private markAccessed;
338
346
  private read;
339
347
  private require;
340
348
  }
@@ -1055,7 +1063,7 @@ declare class BitfabFunction {
1055
1063
  /**
1056
1064
  * SDK version from package.json (injected at build time)
1057
1065
  */
1058
- declare const __version__ = "0.18.0";
1066
+ declare const __version__ = "0.18.2";
1059
1067
 
1060
1068
  /**
1061
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-S3YLZ47O.js";
23
+ } from "./chunk-UTVFKV2Z.js";
24
24
  import {
25
25
  BitfabError
26
- } from "./chunk-QT7HWOKU.js";
26
+ } from "./chunk-PY6V4FE3.js";
27
27
  export {
28
28
  Bitfab,
29
29
  BitfabClaudeAgentHandler,
package/dist/node.cjs CHANGED
@@ -456,7 +456,7 @@ registerAsyncLocalStorageClass(
456
456
  );
457
457
 
458
458
  // src/version.generated.ts
459
- var __version__ = "0.18.0";
459
+ var __version__ = "0.18.2";
460
460
 
461
461
  // src/constants.ts
462
462
  var DEFAULT_SERVICE_URL = "https://bitfab.ai";
@@ -1686,21 +1686,131 @@ function extractModelName(serialized, metadata) {
1686
1686
  }
1687
1687
  return void 0;
1688
1688
  }
1689
+ function asTokenCount(value) {
1690
+ return typeof value === "number" && Number.isFinite(value) ? value : null;
1691
+ }
1692
+ function normalizeTokenUsage(raw) {
1693
+ if (typeof raw !== "object" || raw === null || Array.isArray(raw)) {
1694
+ return null;
1695
+ }
1696
+ const u = raw;
1697
+ 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);
1702
+ if (cacheRead === null && cacheCreation === null && baseInput === null && outputTokens === null) {
1703
+ return null;
1704
+ }
1705
+ const inputTokens = (baseInput ?? 0) + (cacheRead ?? 0) + (cacheCreation ?? 0);
1706
+ return {
1707
+ inputTokens,
1708
+ outputTokens,
1709
+ totalTokens: inputTokens + (outputTokens ?? 0),
1710
+ cachedInputTokens: cacheRead
1711
+ };
1712
+ }
1713
+ if ("prompt_tokens" in u || "completion_tokens" in u || "promptTokens" in u || "completionTokens" in u) {
1714
+ const promptDetails = u.prompt_tokens_details ?? {};
1715
+ 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)
1720
+ });
1721
+ }
1722
+ if ("prompt_token_count" in u || "candidates_token_count" in u) {
1723
+ 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)
1728
+ });
1729
+ }
1730
+ if ("input_tokens" in u || "output_tokens" in u) {
1731
+ 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);
1735
+ if (totalTokens === null && inputTokens !== null && outputTokens !== null) {
1736
+ totalTokens = inputTokens + outputTokens;
1737
+ }
1738
+ return withAnyTokenCount({
1739
+ inputTokens,
1740
+ outputTokens,
1741
+ totalTokens,
1742
+ cachedInputTokens: asTokenCount(inputDetails.cache_read)
1743
+ });
1744
+ }
1745
+ return null;
1746
+ }
1747
+ function withAnyTokenCount(usage) {
1748
+ const hasCount = usage.inputTokens !== null || usage.outputTokens !== null || usage.totalTokens !== null || usage.cachedInputTokens !== null;
1749
+ return hasCount ? usage : null;
1750
+ }
1751
+ function addUsage(totals, usage) {
1752
+ for (const key of [
1753
+ "inputTokens",
1754
+ "outputTokens",
1755
+ "totalTokens",
1756
+ "cachedInputTokens"
1757
+ ]) {
1758
+ const value = usage[key];
1759
+ if (value !== null) {
1760
+ totals[key] = (totals[key] ?? 0) + value;
1761
+ }
1762
+ }
1763
+ }
1764
+ function usageFromGenerations(generations) {
1765
+ if (!generations?.length) {
1766
+ return null;
1767
+ }
1768
+ const totals = {
1769
+ inputTokens: null,
1770
+ outputTokens: null,
1771
+ totalTokens: null,
1772
+ cachedInputTokens: null
1773
+ };
1774
+ let found = false;
1775
+ for (const batch of generations) {
1776
+ if (!Array.isArray(batch)) {
1777
+ continue;
1778
+ }
1779
+ for (const gen of batch) {
1780
+ const msg = gen?.message;
1781
+ if (!msg || typeof msg !== "object") {
1782
+ continue;
1783
+ }
1784
+ const responseMetadata = msg.response_metadata;
1785
+ const usage = normalizeTokenUsage(msg.usage_metadata) ?? normalizeTokenUsage(responseMetadata?.token_usage) ?? normalizeTokenUsage(responseMetadata?.usage) ?? normalizeTokenUsage(responseMetadata?.tokenUsage);
1786
+ if (!usage) {
1787
+ continue;
1788
+ }
1789
+ found = true;
1790
+ addUsage(totals, usage);
1791
+ }
1792
+ }
1793
+ return found ? totals : null;
1794
+ }
1689
1795
  function extractUsage2(output) {
1796
+ const generations = output.generations;
1797
+ const llmOutput = output.llmOutput ?? output.llm_output;
1798
+ const normalized = usageFromGenerations(generations) ?? normalizeTokenUsage(llmOutput?.tokenUsage) ?? normalizeTokenUsage(llmOutput?.token_usage) ?? normalizeTokenUsage(llmOutput?.usage);
1690
1799
  const usage = {};
1691
- const llmOutput = output.llmOutput;
1692
- const tokenUsage = llmOutput?.tokenUsage ?? llmOutput?.token_usage ?? llmOutput?.usage ?? {};
1693
- const inputTokens = tokenUsage.promptTokens ?? tokenUsage.prompt_tokens ?? tokenUsage.input_tokens;
1694
- const outputTokens = tokenUsage.completionTokens ?? tokenUsage.completion_tokens ?? tokenUsage.output_tokens;
1695
- const totalTokens = tokenUsage.totalTokens ?? tokenUsage.total_tokens;
1696
- if (inputTokens !== void 0 && inputTokens !== null) {
1697
- usage.inputTokens = inputTokens;
1800
+ if (!normalized) {
1801
+ return usage;
1802
+ }
1803
+ if (normalized.inputTokens !== null) {
1804
+ usage.inputTokens = normalized.inputTokens;
1698
1805
  }
1699
- if (outputTokens !== void 0 && outputTokens !== null) {
1700
- usage.outputTokens = outputTokens;
1806
+ if (normalized.outputTokens !== null) {
1807
+ usage.outputTokens = normalized.outputTokens;
1701
1808
  }
1702
- if (totalTokens !== void 0 && totalTokens !== null) {
1703
- usage.totalTokens = totalTokens;
1809
+ if (normalized.totalTokens !== null) {
1810
+ usage.totalTokens = normalized.totalTokens;
1811
+ }
1812
+ if (normalized.cachedInputTokens !== null) {
1813
+ usage.cachedInputTokens = normalized.cachedInputTokens;
1704
1814
  }
1705
1815
  return usage;
1706
1816
  }
@@ -2061,7 +2171,9 @@ var ReplayEnvironment = class {
2061
2171
  * Throws if read outside a replay item.
2062
2172
  */
2063
2173
  get databaseUrl() {
2064
- return this.require().databaseUrl;
2174
+ const snapshot = this.require();
2175
+ this.markAccessed();
2176
+ return snapshot.databaseUrl;
2065
2177
  }
2066
2178
  /** When the per-trace branch URL stops being valid. ISO-8601. */
2067
2179
  get expiresAt() {
@@ -2089,7 +2201,24 @@ var ReplayEnvironment = class {
2089
2201
  }
2090
2202
  /** Non-throwing variant for callers that handle the inactive case. */
2091
2203
  snapshot() {
2092
- return this.read();
2204
+ const snapshot = this.read();
2205
+ if (snapshot) {
2206
+ this.markAccessed();
2207
+ }
2208
+ return snapshot;
2209
+ }
2210
+ /**
2211
+ * Record on the replay context that customer code obtained the branch
2212
+ * URL. Only `databaseUrl` and `snapshot()` count — `active`, `readOnly`
2213
+ * and friends inspect the lease without exposing the connection string,
2214
+ * so they don't prove the replayed code could have connected to the
2215
+ * branch.
2216
+ */
2217
+ markAccessed() {
2218
+ const ctx = getReplayContext();
2219
+ if (ctx?.dbBranchLease) {
2220
+ ctx.dbSnapshotAccessed = true;
2221
+ }
2093
2222
  }
2094
2223
  read() {
2095
2224
  const ctx = getReplayContext();
@@ -3021,7 +3150,19 @@ var Bitfab = class {
3021
3150
  contexts: traceState?.contexts ?? [],
3022
3151
  testRunId: traceState?.testRunId,
3023
3152
  inputSourceTraceId: traceState?.inputSourceTraceId,
3024
- dbSnapshotRef: traceState?.dbSnapshotRef
3153
+ dbSnapshotRef: traceState?.dbSnapshotRef,
3154
+ // Built AFTER the wrapped fn finished, so `accessed` reflects
3155
+ // whether customer code obtained the branch URL during this
3156
+ // item. Omitted entirely when no lease was attached, so the
3157
+ // server can distinguish "no branch" from "branch ignored".
3158
+ ...replayCtx?.dbBranchLease && {
3159
+ dbSnapshotUsage: {
3160
+ neonBranchId: replayCtx.dbBranchLease.neonBranchId,
3161
+ snapshotTimestamp: replayCtx.dbBranchLease.snapshotTimestamp,
3162
+ sourceTraceId: replayCtx.sourceBitfabTraceId,
3163
+ accessed: replayCtx.dbSnapshotAccessed === true
3164
+ }
3165
+ }
3025
3166
  });
3026
3167
  activeTraceStates.delete(traceId);
3027
3168
  if (persistenceCollector) {
@@ -3193,6 +3334,18 @@ var Bitfab = class {
3193
3334
  if (params.dbSnapshotRef) {
3194
3335
  rawTrace.db_snapshot_ref = params.dbSnapshotRef;
3195
3336
  }
3337
+ if (params.dbSnapshotUsage) {
3338
+ rawTrace.db_snapshot_usage = {
3339
+ neon_branch_id: params.dbSnapshotUsage.neonBranchId,
3340
+ ...params.dbSnapshotUsage.snapshotTimestamp && {
3341
+ snapshot_timestamp: params.dbSnapshotUsage.snapshotTimestamp
3342
+ },
3343
+ ...params.dbSnapshotUsage.sourceTraceId && {
3344
+ source_trace_id: params.dbSnapshotUsage.sourceTraceId
3345
+ },
3346
+ accessed: params.dbSnapshotUsage.accessed
3347
+ };
3348
+ }
3196
3349
  return this.httpClient.sendExternalTrace({
3197
3350
  type: "sdk-function",
3198
3351
  source: "typescript-sdk-function",