@bitfab/sdk 0.18.0 → 0.18.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/{chunk-S3YLZ47O.js → chunk-ILIUTS5D.js} +123 -13
- package/dist/chunk-ILIUTS5D.js.map +1 -0
- package/dist/index.cjs +124 -14
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +4 -4
- package/dist/index.d.ts +4 -4
- package/dist/index.js +1 -1
- package/dist/node.cjs +124 -14
- package/dist/node.cjs.map +1 -1
- package/dist/node.js +1 -1
- package/package.json +1 -1
- package/dist/chunk-S3YLZ47O.js.map +0 -1
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
|
|
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
|
|
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");
|
|
@@ -1055,7 +1055,7 @@ declare class BitfabFunction {
|
|
|
1055
1055
|
/**
|
|
1056
1056
|
* SDK version from package.json (injected at build time)
|
|
1057
1057
|
*/
|
|
1058
|
-
declare const __version__ = "0.18.
|
|
1058
|
+
declare const __version__ = "0.18.1";
|
|
1059
1059
|
|
|
1060
1060
|
/**
|
|
1061
1061
|
* 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
|
|
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
|
|
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");
|
|
@@ -1055,7 +1055,7 @@ declare class BitfabFunction {
|
|
|
1055
1055
|
/**
|
|
1056
1056
|
* SDK version from package.json (injected at build time)
|
|
1057
1057
|
*/
|
|
1058
|
-
declare const __version__ = "0.18.
|
|
1058
|
+
declare const __version__ = "0.18.1";
|
|
1059
1059
|
|
|
1060
1060
|
/**
|
|
1061
1061
|
* Constants for the Bitfab SDK.
|
package/dist/index.js
CHANGED
package/dist/node.cjs
CHANGED
|
@@ -447,7 +447,7 @@ registerAsyncLocalStorageClass(
|
|
|
447
447
|
);
|
|
448
448
|
|
|
449
449
|
// src/version.generated.ts
|
|
450
|
-
var __version__ = "0.18.
|
|
450
|
+
var __version__ = "0.18.1";
|
|
451
451
|
|
|
452
452
|
// src/constants.ts
|
|
453
453
|
var DEFAULT_SERVICE_URL = "https://bitfab.ai";
|
|
@@ -1677,21 +1677,131 @@ function extractModelName(serialized, metadata) {
|
|
|
1677
1677
|
}
|
|
1678
1678
|
return void 0;
|
|
1679
1679
|
}
|
|
1680
|
+
function asTokenCount(value) {
|
|
1681
|
+
return typeof value === "number" && Number.isFinite(value) ? value : null;
|
|
1682
|
+
}
|
|
1683
|
+
function normalizeTokenUsage(raw) {
|
|
1684
|
+
if (typeof raw !== "object" || raw === null || Array.isArray(raw)) {
|
|
1685
|
+
return null;
|
|
1686
|
+
}
|
|
1687
|
+
const u = raw;
|
|
1688
|
+
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);
|
|
1693
|
+
if (cacheRead === null && cacheCreation === null && baseInput === null && outputTokens === null) {
|
|
1694
|
+
return null;
|
|
1695
|
+
}
|
|
1696
|
+
const inputTokens = (baseInput ?? 0) + (cacheRead ?? 0) + (cacheCreation ?? 0);
|
|
1697
|
+
return {
|
|
1698
|
+
inputTokens,
|
|
1699
|
+
outputTokens,
|
|
1700
|
+
totalTokens: inputTokens + (outputTokens ?? 0),
|
|
1701
|
+
cachedInputTokens: cacheRead
|
|
1702
|
+
};
|
|
1703
|
+
}
|
|
1704
|
+
if ("prompt_tokens" in u || "completion_tokens" in u || "promptTokens" in u || "completionTokens" in u) {
|
|
1705
|
+
const promptDetails = u.prompt_tokens_details ?? {};
|
|
1706
|
+
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)
|
|
1711
|
+
});
|
|
1712
|
+
}
|
|
1713
|
+
if ("prompt_token_count" in u || "candidates_token_count" in u) {
|
|
1714
|
+
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)
|
|
1719
|
+
});
|
|
1720
|
+
}
|
|
1721
|
+
if ("input_tokens" in u || "output_tokens" in u) {
|
|
1722
|
+
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);
|
|
1726
|
+
if (totalTokens === null && inputTokens !== null && outputTokens !== null) {
|
|
1727
|
+
totalTokens = inputTokens + outputTokens;
|
|
1728
|
+
}
|
|
1729
|
+
return withAnyTokenCount({
|
|
1730
|
+
inputTokens,
|
|
1731
|
+
outputTokens,
|
|
1732
|
+
totalTokens,
|
|
1733
|
+
cachedInputTokens: asTokenCount(inputDetails.cache_read)
|
|
1734
|
+
});
|
|
1735
|
+
}
|
|
1736
|
+
return null;
|
|
1737
|
+
}
|
|
1738
|
+
function withAnyTokenCount(usage) {
|
|
1739
|
+
const hasCount = usage.inputTokens !== null || usage.outputTokens !== null || usage.totalTokens !== null || usage.cachedInputTokens !== null;
|
|
1740
|
+
return hasCount ? usage : null;
|
|
1741
|
+
}
|
|
1742
|
+
function addUsage(totals, usage) {
|
|
1743
|
+
for (const key of [
|
|
1744
|
+
"inputTokens",
|
|
1745
|
+
"outputTokens",
|
|
1746
|
+
"totalTokens",
|
|
1747
|
+
"cachedInputTokens"
|
|
1748
|
+
]) {
|
|
1749
|
+
const value = usage[key];
|
|
1750
|
+
if (value !== null) {
|
|
1751
|
+
totals[key] = (totals[key] ?? 0) + value;
|
|
1752
|
+
}
|
|
1753
|
+
}
|
|
1754
|
+
}
|
|
1755
|
+
function usageFromGenerations(generations) {
|
|
1756
|
+
if (!generations?.length) {
|
|
1757
|
+
return null;
|
|
1758
|
+
}
|
|
1759
|
+
const totals = {
|
|
1760
|
+
inputTokens: null,
|
|
1761
|
+
outputTokens: null,
|
|
1762
|
+
totalTokens: null,
|
|
1763
|
+
cachedInputTokens: null
|
|
1764
|
+
};
|
|
1765
|
+
let found = false;
|
|
1766
|
+
for (const batch of generations) {
|
|
1767
|
+
if (!Array.isArray(batch)) {
|
|
1768
|
+
continue;
|
|
1769
|
+
}
|
|
1770
|
+
for (const gen of batch) {
|
|
1771
|
+
const msg = gen?.message;
|
|
1772
|
+
if (!msg || typeof msg !== "object") {
|
|
1773
|
+
continue;
|
|
1774
|
+
}
|
|
1775
|
+
const responseMetadata = msg.response_metadata;
|
|
1776
|
+
const usage = normalizeTokenUsage(msg.usage_metadata) ?? normalizeTokenUsage(responseMetadata?.token_usage) ?? normalizeTokenUsage(responseMetadata?.usage) ?? normalizeTokenUsage(responseMetadata?.tokenUsage);
|
|
1777
|
+
if (!usage) {
|
|
1778
|
+
continue;
|
|
1779
|
+
}
|
|
1780
|
+
found = true;
|
|
1781
|
+
addUsage(totals, usage);
|
|
1782
|
+
}
|
|
1783
|
+
}
|
|
1784
|
+
return found ? totals : null;
|
|
1785
|
+
}
|
|
1680
1786
|
function extractUsage2(output) {
|
|
1787
|
+
const generations = output.generations;
|
|
1788
|
+
const llmOutput = output.llmOutput ?? output.llm_output;
|
|
1789
|
+
const normalized = usageFromGenerations(generations) ?? normalizeTokenUsage(llmOutput?.tokenUsage) ?? normalizeTokenUsage(llmOutput?.token_usage) ?? normalizeTokenUsage(llmOutput?.usage);
|
|
1681
1790
|
const usage = {};
|
|
1682
|
-
|
|
1683
|
-
|
|
1684
|
-
|
|
1685
|
-
|
|
1686
|
-
|
|
1687
|
-
|
|
1688
|
-
|
|
1689
|
-
|
|
1690
|
-
|
|
1691
|
-
|
|
1692
|
-
|
|
1693
|
-
|
|
1694
|
-
|
|
1791
|
+
if (!normalized) {
|
|
1792
|
+
return usage;
|
|
1793
|
+
}
|
|
1794
|
+
if (normalized.inputTokens !== null) {
|
|
1795
|
+
usage.inputTokens = normalized.inputTokens;
|
|
1796
|
+
}
|
|
1797
|
+
if (normalized.outputTokens !== null) {
|
|
1798
|
+
usage.outputTokens = normalized.outputTokens;
|
|
1799
|
+
}
|
|
1800
|
+
if (normalized.totalTokens !== null) {
|
|
1801
|
+
usage.totalTokens = normalized.totalTokens;
|
|
1802
|
+
}
|
|
1803
|
+
if (normalized.cachedInputTokens !== null) {
|
|
1804
|
+
usage.cachedInputTokens = normalized.cachedInputTokens;
|
|
1695
1805
|
}
|
|
1696
1806
|
return usage;
|
|
1697
1807
|
}
|