@bitfab/sdk

中文文档
0.76.6发布于 2026/11/06MITTypeScriptESM + CJS近 30 天下载 1.5万

Bitfab client for provider-based API calls with local BAML execution

原始使用文档
文档目录21 节
  1. Monorepo Setup
  2. Installing Dependencies
  3. Updating pnpm
  4. Running Commands
  5. Available Development Tools
  6. Installation
  7. Basic Installation
  8. With OpenAI Agents SDK Tracing
  9. Local Development
  10. Usage
  11. Basic Usage (Local Execution)
  12. Server-Side Execution
  13. With TypeScript Generics
  14. Error Handling
  15. Configuration
  16. Execution Modes
  17. Local Execution (Default)
  18. Server-Side Execution
  19. Environment Variables
  20. Testing
  21. Publishing

Bitfab TypeScript SDK

Bitfab client for provider-based API calls.

Monorepo Setup

This package is part of a pnpm workspace monorepo. The workspace structure separates concerns:

  • Root package.json: Contains only shared dev dependencies (Biome, TypeScript, Vitest, Knip)
  • Package package.json: Contains runtime dependencies specific to this package
  • You can run tests and validation from the root directory: pnpm test or pnpm validate
  • No need to manually update shared dev tooling versions across packages

Installing Dependencies

Always install dependencies from the root directory:

# From the root directory
pnpm install

Updating pnpm

To update pnpm across the monorepo:

# From the root directory
pnpm pnpm:update

This automatically updates pnpm and syncs the version in package.json and CI/CD workflows.

Running Commands

You can run commands from this directory or from the root:

# From this directory
pnpm test
pnpm build
pnpm validate

# From the root directory (runs across all packages)
pnpm test          # Run all tests
pnpm validate      # Run lint + tsc + test

Available Development Tools

This package includes the following shared development tools:

ToolPurposeCommand
VitestUnit testing with coveragepnpm test
BiomeFast linting & formattingpnpm lint
TypeScriptType checkingpnpm tsc
KnipFind unused dependencies/exportspnpm knip

Run individual tools:

# Run tests with coverage
pnpm test

# Lint and format code
pnpm lint

# Type check without building
pnpm tsc

# Find unused dependencies
pnpm knip

# Run all validation checks
pnpm validate

Installation

Basic Installation

npm install @bitfab/sdk
# or
pnpm add @bitfab/sdk
# or
yarn add @bitfab/sdk

Automatic subtree transform adapters are included under @bitfab/sdk/transform/*; no second package is required.

With OpenAI Agents SDK Tracing

If you want to use the OpenAI Agents SDK tracing integration:

npm install @bitfab/sdk @openai/agents
# or
pnpm add @bitfab/sdk @openai/agents

The @openai/agents package is an optional peer dependency - the SDK works fine without it unless you need tracing functionality.

Local Development

cd bitfab-typescript-sdk
pnpm build

To link for local development:

# In this directory
pnpm link --global

# In your app directory
pnpm link --global bitfab

To switch back to npm version:

pnpm unlink bitfab
pnpm add bitfab

Usage

Basic Usage (Local Execution)

By default, the SDK executes BAML functions locally on the client by fetching the BAML prompt from the server and running it with your own API keys:

import { Bitfab } from "bitfab";

const client = new Bitfab({
  apiKey: "bf_your_api_key_here", // Required: Your Bitfab API key
  serviceUrl: "https://bitfab.ai", // Optional
  envVars: {
    OPENAI_API_KEY: process.env.OPENAI_API_KEY, // Your LLM provider API keys
  },
  executeLocally: true, // Default: true
});

const result = await client.call("method_name", {
  arg1: "value1",
  arg2: "value2",
});

Server-Side Execution

If you prefer to execute BAML on the server (using the server's API keys), set executeLocally: false:

const client = new Bitfab({
  apiKey: "bf_your_api_key_here",
  executeLocally: false, // Execute on server instead of locally
});

const result = await client.call("method_name", {
  arg1: "value1",
  arg2: "value2",
});

With TypeScript Generics

You can specify the return type for type safety:

interface MyResponse {
  answer: string;
  confidence: number;
}

const result = await client.call<MyResponse>("analyze", {
  text: "Hello world",
});

console.log(result.answer); // TypeScript knows this is a string

Error Handling

import { Bitfab, BitfabError } from "bitfab";

const client = new Bitfab({
  apiKey: "bf_your_api_key_here",
  serviceUrl: "https://your-bitfab-instance.com",
});

try {
  const result = await client.call("method_name", { input: "value" });
} catch (error) {
  if (error instanceof BitfabError) {
    console.error("Bitfab error:", error.message);
    if (error.url) {
      console.error("Configure at:", error.url);
    }
  }
}

Configuration

OptionRequiredDescription
apiKeyYesYour Bitfab API key (generate from your Bitfab dashboard)
serviceUrlNoThe base URL for the Bitfab API (default: https://bitfab.ai)
timeoutNoRequest timeout in milliseconds (default: 120000)
envVarsNoEnvironment variables for LLM provider API keys (e.g., { OPENAI_API_KEY: "..." }). Required for local execution.
executeLocallyNoWhether to execute BAML locally on the client (default: true). Set to false to execute on the server with the server's API keys.

Execution Modes

Local Execution (Default)

When executeLocally: true (default), the SDK:

  1. Fetches the BAML function definition from the Bitfab server
  2. Executes the BAML locally using the @boundaryml/baml runtime
  3. Uses your own LLM provider API keys (passed via envVars)
  4. Returns the result directly to you

Benefits:

  • Full control over API keys and costs
  • Lower latency (no server round-trip for execution)
  • Privacy: Your data doesn't go through the Bitfab server

Requirements:

  • You must provide LLM provider API keys via envVars
  • The BAML runtime runs in your environment (Node.js, browser with polyfills, etc.)

Server-Side Execution

When executeLocally: false, the SDK:

  1. Sends the function name and inputs to the Bitfab server
  2. The server executes the BAML using its own API keys
  3. Returns the result to you

Benefits:

  • No need to manage LLM provider API keys
  • Simpler setup
  • Works in any environment

Requirements:

  • The Bitfab server must have the necessary API keys configured

Environment Variables

  • BITFAB_API_KEY: the API key used when apiKey is omitted or resolves empty, read at first use
  • BITFAB_CAPTURE_ENABLED: the capture switch for a client that does not pass captureEnabled. 1, true, or yes captures; 0, false, or no does not. Unset captures, and captureEnabled in code always wins
  • OPENAI_API_KEY, ANTHROPIC_API_KEY, etc.: LLM provider API keys (required for local execution)
  • BITFAB_OTEL_MAX_REQUEST_BYTES: request-size target for exports (positive integer up to 3000000)
  • BITFAB_OTEL_EXPORT_CONCURRENCY: concurrent direct requests per export window (1-64, default 32)
  • BITFAB_COMMIT_SHA: the commit recorded as commit_ref.sha on every root trace, for a container built without a deploy platform's build variables or a git checkout
  • BITFAB_DISABLE_COMMIT_REF: set to send no commit_ref at all

Testing

A test script is provided to verify both local and server-side execution:

# Set up environment variables
export BITFAB_API_KEY="bf_your_api_key"
export OPENAI_API_KEY="your_openai_key"

# Run the test script
pnpm tsx test-local-execution.ts [function-name] [query]

# Example
pnpm tsx test-local-execution.ts extract-entities "What is the capital of France?"

The test script will:

  1. Test local execution with your API keys
  2. Test server-side execution
  3. Compare results and performance

Publishing

pnpm build
npm publish --access public

版本与发布记录

@bitfab/sdk 共发布 252 个版本,首次发布于 2026/05/21;过去 12 个月发布了 252 个版本。

版本发布日期距今
0.76.6latest2026/11/062 天前
0.76.52026/11/062 天前
0.76.42026/11/053 天前
0.76.32026/11/053 天前
0.76.22026/11/053 天前
0.76.12026/11/044 天前

关于 @bitfab/sdk 的常见问题

@bitfab/sdk 是什么?

Bitfab client for provider-based API calls with local BAML execution

如何安装 @bitfab/sdk?

在项目目录运行 npm install @bitfab/sdk,或使用 pnpm add / yarn add / bun add。它还提供命令行工具 bitfab-seed、bitfab-replay,可以用 npx bitfab-seed 直接运行。

@bitfab/sdk 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@bitfab/sdk 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@bitfab/sdk 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=22.15.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@bitfab/sdk 可以商用吗?

@bitfab/sdk 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@bitfab/sdk 的下载量有多少?

根据 npm 官方下载统计,@bitfab/sdk 在 2026/10/08 – 2026/11/06 这 30 天内被下载 15,117 次,日均约 504 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@bitfab/sdk 还在维护吗?

最新版本 0.76.6 发布于 2026/11/06(2 天前);至今共发布 252 个版本,其中过去 12 个月发布了 252 个;首次发布于 2026/05/21。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。