@bitfab/sdk 0.13.0

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/README.md ADDED
@@ -0,0 +1,292 @@
1
+ # Bitfab TypeScript SDK
2
+
3
+ Bitfab client for provider-based API calls.
4
+
5
+ ## Monorepo Setup
6
+
7
+ This package is part of a **pnpm workspace** monorepo. The workspace structure separates concerns:
8
+
9
+ - **Root `package.json`**: Contains only shared dev dependencies (Biome, TypeScript, Vitest, Knip, Madge)
10
+ - **Package `package.json`**: Contains runtime dependencies specific to this package
11
+ - You can run tests and validation from the root directory: `pnpm test` or `pnpm validate`
12
+ - No need to manually update shared dev tooling versions across packages
13
+
14
+ ### Installing Dependencies
15
+
16
+ Always install dependencies from the **root directory**:
17
+
18
+ ```bash
19
+ # From the root directory
20
+ pnpm install
21
+ ```
22
+
23
+ ### Updating pnpm
24
+
25
+ To update pnpm across the monorepo:
26
+
27
+ ```bash
28
+ # From the root directory
29
+ pnpm pnpm:update
30
+ ```
31
+
32
+ This automatically updates pnpm and syncs the version in `package.json` and CI/CD workflows.
33
+
34
+ ### Running Commands
35
+
36
+ You can run commands from this directory or from the root:
37
+
38
+ ```bash
39
+ # From this directory
40
+ pnpm test
41
+ pnpm build
42
+ pnpm validate
43
+
44
+ # From the root directory (runs across all packages)
45
+ pnpm test # Run all tests
46
+ pnpm validate # Run lint + tsc + test
47
+ ```
48
+
49
+ ### Available Development Tools
50
+
51
+ This package includes the following shared development tools:
52
+
53
+ | Tool | Purpose | Command |
54
+ |------|---------|---------|
55
+ | **Vitest** | Unit testing with coverage | `pnpm test` |
56
+ | **Biome** | Fast linting & formatting | `pnpm lint` |
57
+ | **TypeScript** | Type checking | `pnpm tsc` |
58
+ | **Knip** | Find unused dependencies/exports | `pnpm knip` |
59
+ | **Madge** | Detect circular dependencies | `pnpm madge` |
60
+
61
+ **Run individual tools:**
62
+
63
+ ```bash
64
+ # Run tests with coverage
65
+ pnpm test
66
+
67
+ # Lint and format code
68
+ pnpm lint
69
+
70
+ # Type check without building
71
+ pnpm tsc
72
+
73
+ # Find unused dependencies
74
+ pnpm knip
75
+
76
+ # Check for circular dependencies
77
+ pnpm madge
78
+
79
+ # Run all validation checks
80
+ pnpm validate
81
+ ```
82
+
83
+ ## Installation
84
+
85
+ ### Basic Installation
86
+
87
+ ```bash
88
+ npm install bitfab
89
+ # or
90
+ pnpm add bitfab
91
+ # or
92
+ yarn add bitfab
93
+ ```
94
+
95
+ ### With OpenAI Agents SDK Tracing
96
+
97
+ If you want to use the OpenAI Agents SDK tracing integration:
98
+
99
+ ```bash
100
+ npm install bitfab @openai/agents
101
+ # or
102
+ pnpm add bitfab @openai/agents
103
+ ```
104
+
105
+ The `@openai/agents` package is an optional peer dependency - the SDK works fine without it unless you need tracing functionality.
106
+
107
+ ## Local Development
108
+
109
+ ```bash
110
+ cd bitfab-typescript-sdk
111
+ pnpm build
112
+ ```
113
+
114
+ To link for local development:
115
+
116
+ ```bash
117
+ # In this directory
118
+ pnpm link --global
119
+
120
+ # In your app directory
121
+ pnpm link --global bitfab
122
+ ```
123
+
124
+ To switch back to npm version:
125
+
126
+ ```bash
127
+ pnpm unlink bitfab
128
+ pnpm add bitfab
129
+ ```
130
+
131
+ ## Usage
132
+
133
+ ### Basic Usage (Local Execution)
134
+
135
+ 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:
136
+
137
+ ```typescript
138
+ import { Bitfab } from "bitfab";
139
+
140
+ const client = new Bitfab({
141
+ apiKey: "bf_your_api_key_here", // Required: Your Bitfab API key
142
+ serviceUrl: "https://bitfab.ai", // Optional
143
+ envVars: {
144
+ OPENAI_API_KEY: process.env.OPENAI_API_KEY, // Your LLM provider API keys
145
+ },
146
+ executeLocally: true, // Default: true
147
+ });
148
+
149
+ const result = await client.call("method_name", {
150
+ arg1: "value1",
151
+ arg2: "value2",
152
+ });
153
+ ```
154
+
155
+ ### Server-Side Execution
156
+
157
+ If you prefer to execute BAML on the server (using the server's API keys), set `executeLocally: false`:
158
+
159
+ ```typescript
160
+ const client = new Bitfab({
161
+ apiKey: "bf_your_api_key_here",
162
+ executeLocally: false, // Execute on server instead of locally
163
+ });
164
+
165
+ const result = await client.call("method_name", {
166
+ arg1: "value1",
167
+ arg2: "value2",
168
+ });
169
+ ```
170
+
171
+ ### With TypeScript Generics
172
+
173
+ You can specify the return type for type safety:
174
+
175
+ ```typescript
176
+ interface MyResponse {
177
+ answer: string;
178
+ confidence: number;
179
+ }
180
+
181
+ const result = await client.call<MyResponse>("analyze", {
182
+ text: "Hello world",
183
+ });
184
+
185
+ console.log(result.answer); // TypeScript knows this is a string
186
+ ```
187
+
188
+ ### Error Handling
189
+
190
+ ```typescript
191
+ import { Bitfab, BitfabError } from "bitfab";
192
+
193
+ const client = new Bitfab({
194
+ apiKey: "bf_your_api_key_here",
195
+ serviceUrl: "https://your-bitfab-instance.com",
196
+ });
197
+
198
+ try {
199
+ const result = await client.call("method_name", { input: "value" });
200
+ } catch (error) {
201
+ if (error instanceof BitfabError) {
202
+ console.error("Bitfab error:", error.message);
203
+ if (error.url) {
204
+ console.error("Configure at:", error.url);
205
+ }
206
+ }
207
+ }
208
+ ```
209
+
210
+ ## Configuration
211
+
212
+ | Option | Required | Description |
213
+ | ---------------- | -------- | -------------------------------------------------------------------------------------------------------------------------------- |
214
+ | `apiKey` | Yes | Your Bitfab API key (generate from your Bitfab dashboard) |
215
+ | `serviceUrl` | No | The base URL for the Bitfab API (default: https://bitfab.ai) |
216
+ | `timeout` | No | Request timeout in milliseconds (default: 120000) |
217
+ | `envVars` | No | Environment variables for LLM provider API keys (e.g., `{ OPENAI_API_KEY: "..." }`). Required for local execution. |
218
+ | `executeLocally` | No | Whether to execute BAML locally on the client (default: true). Set to false to execute on the server with the server's API keys. |
219
+
220
+ ## Execution Modes
221
+
222
+ ### Local Execution (Default)
223
+
224
+ When `executeLocally: true` (default), the SDK:
225
+
226
+ 1. Fetches the BAML function definition from the Bitfab server
227
+ 2. Executes the BAML locally using the `@boundaryml/baml` runtime
228
+ 3. Uses your own LLM provider API keys (passed via `envVars`)
229
+ 4. Returns the result directly to you
230
+
231
+ **Benefits:**
232
+
233
+ - Full control over API keys and costs
234
+ - Lower latency (no server round-trip for execution)
235
+ - Privacy: Your data doesn't go through the Bitfab server
236
+
237
+ **Requirements:**
238
+
239
+ - You must provide LLM provider API keys via `envVars`
240
+ - The BAML runtime runs in your environment (Node.js, browser with polyfills, etc.)
241
+
242
+ ### Server-Side Execution
243
+
244
+ When `executeLocally: false`, the SDK:
245
+
246
+ 1. Sends the function name and inputs to the Bitfab server
247
+ 2. The server executes the BAML using its own API keys
248
+ 3. Returns the result to you
249
+
250
+ **Benefits:**
251
+
252
+ - No need to manage LLM provider API keys
253
+ - Simpler setup
254
+ - Works in any environment
255
+
256
+ **Requirements:**
257
+
258
+ - The Bitfab server must have the necessary API keys configured
259
+
260
+ ## Environment Variables
261
+
262
+ - `BITFAB_URL`: The base URL for the Bitfab API (used if `serviceUrl` is not provided)
263
+ - `OPENAI_API_KEY`, `ANTHROPIC_API_KEY`, etc.: LLM provider API keys (required for local execution)
264
+
265
+ ## Testing
266
+
267
+ A test script is provided to verify both local and server-side execution:
268
+
269
+ ```bash
270
+ # Set up environment variables
271
+ export BITFAB_API_KEY="bf_your_api_key"
272
+ export OPENAI_API_KEY="your_openai_key"
273
+
274
+ # Run the test script
275
+ pnpm tsx test-local-execution.ts [function-name] [query]
276
+
277
+ # Example
278
+ pnpm tsx test-local-execution.ts extract-entities "What is the capital of France?"
279
+ ```
280
+
281
+ The test script will:
282
+
283
+ 1. Test local execution with your API keys
284
+ 2. Test server-side execution
285
+ 3. Compare results and performance
286
+
287
+ ## Publishing
288
+
289
+ ```bash
290
+ pnpm build
291
+ npm publish --access public
292
+ ```