@braintrust/browser 0.0.2-rc.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,75 @@
1
+ # Braintrust Browser SDK
2
+
3
+ Official browser-only SDK for [Braintrust](https://braintrust.dev).
4
+
5
+ This is an integration package that provides browser-optimized builds of the Braintrust SDK with AsyncLocalStorage polyfill support for standard browsers.
6
+
7
+ ## Installation
8
+
9
+ ```bash
10
+ npm install @braintrust/browser braintrust
11
+ # or
12
+ pnpm add @braintrust/browser braintrust
13
+ # or
14
+ yarn add @braintrust/browser braintrust
15
+ ```
16
+
17
+ Note: `braintrust` is a peer dependency and must be installed alongside `@braintrust/browser`.
18
+
19
+ ## Usage
20
+
21
+ ```typescript
22
+ import * as braintrust from "@braintrust/browser";
23
+
24
+ const experiment = await braintrust.init("BrowserExperiment", {
25
+ apiKey: "YOUR_API_KEY",
26
+ });
27
+
28
+ // Use tracing in browser
29
+ const result = await braintrust.traced(
30
+ async () => {
31
+ // Your code here
32
+ return "result";
33
+ },
34
+ { name: "myOperation" },
35
+ );
36
+ ```
37
+
38
+ ## Differences from Main Package
39
+
40
+ This package:
41
+
42
+ - **Includes** `als-browser` polyfill for AsyncLocalStorage (bundled)
43
+ - **Requires** `braintrust` as a peer dependency
44
+
45
+ ## When to Use
46
+
47
+ Use `@braintrust/browser` when:
48
+
49
+ - Building browser-only applications
50
+ - Need AsyncLocalStorage support in standard browsers
51
+
52
+ Use `braintrust` directly when:
53
+
54
+ - Building Node.js applications
55
+ - Using in Next.js or other full-stack frameworks (with proper module resolution)
56
+ - Need CLI tools or filesystem access
57
+
58
+ ## Features
59
+
60
+ All browser-compatible features from the main SDK:
61
+
62
+ - Logging and tracing
63
+ - Experiments and datasets
64
+ - Prompt management
65
+ - AI provider wrappers (OpenAI, Anthropic, Google)
66
+ - Evaluation framework
67
+ - OpenTelemetry integration
68
+
69
+ ## Documentation
70
+
71
+ For full documentation, visit [https://www.braintrust.dev/docs](https://www.braintrust.dev/docs)
72
+
73
+ ## License
74
+
75
+ Apache-2.0
@@ -0,0 +1 @@
1
+ export * from 'braintrust';
package/dist/index.js ADDED
@@ -0,0 +1,41 @@
1
+ import { _internalIso, _internalSetInitialState } from 'braintrust';
2
+ export * from 'braintrust';
3
+ import { AsyncLocalStorage } from 'als-browser';
4
+
5
+ // src/browser-config.ts
6
+ var browserConfigured = false;
7
+ function configureBrowser() {
8
+ if (browserConfigured) {
9
+ return;
10
+ }
11
+ _internalIso.buildType = "browser";
12
+ _internalIso.newAsyncLocalStorage = () => new AsyncLocalStorage();
13
+ _internalIso.getEnv = (name) => {
14
+ if (typeof process === "undefined" || typeof process.env === "undefined") {
15
+ return void 0;
16
+ }
17
+ return process.env[name];
18
+ };
19
+ _internalIso.getRepoInfo = async () => ({
20
+ commit: null,
21
+ branch: null,
22
+ tag: null,
23
+ dirty: false
24
+ });
25
+ _internalIso.getCallerLocation = () => void 0;
26
+ _internalIso.hash = (data) => {
27
+ let hash = 0;
28
+ for (let i = 0; i < data.length; i++) {
29
+ const char = data.charCodeAt(i);
30
+ hash = (hash << 5) - hash + char;
31
+ hash = hash & hash;
32
+ }
33
+ const hashHex = (hash >>> 0).toString(16).padStart(8, "0");
34
+ return hashHex.repeat(8).substring(0, 64);
35
+ };
36
+ _internalSetInitialState();
37
+ browserConfigured = true;
38
+ }
39
+ configureBrowser();
40
+ //# sourceMappingURL=index.js.map
41
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/browser-config.ts","../src/index.ts"],"names":["iso","BrowserAsyncLocalStorage"],"mappings":";;;;;AAGA,IAAI,iBAAA,GAAoB,KAAA;AAEjB,SAAS,gBAAA,GAAmB;AACjC,EAAA,IAAI,iBAAA,EAAmB;AACrB,IAAA;AAAA,EACF;AAGA,EAAAA,YAAA,CAAI,SAAA,GAAY,SAAA;AAEhB,EAAAA,YAAA,CAAI,oBAAA,GAAuB,MAA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AsyncLocalStorage as BrowserAsyncLocalStorage } from \"als-browser\";\n\nlet browserConfigured = false;\n\nexport function configureBrowser() {\n if (browserConfigured) {\n return;\n }\n\n // Set build type indicator\n iso.buildType = \"browser\";\n\n iso.newAsyncLocalStorage = <T>() => new BrowserAsyncLocalStorage<T>();\n\n iso.getEnv = (name: string) => {\n if (typeof process === \"undefined\" || typeof process.env === \"undefined\") {\n return undefined;\n }\n return process.env[name];\n };\n\n // noop implementations for git config\n iso.getRepoInfo = async () => ({\n commit: null,\n branch: null,\n tag: null,\n dirty: false,\n });\n iso.getCallerLocation = () => undefined;\n\n // Implement browser-compatible hash function using a simple hash algorithm\n iso.hash = (data: string): string => {\n // Simple hash function for browser compatibility\n let hash = 0;\n for (let i = 0; i < data.length; i++) {\n const char = data.charCodeAt(i);\n hash = (hash << 5) - hash + char;\n hash = hash & hash; // Convert to 32bit integer\n }\n // Convert to hex string\n const hashHex = (hash >>> 0).toString(16).padStart(8, \"0\");\n return hashHex.repeat(8).substring(0, 64); // Make it look like a SHA-256 hash length\n };\n\n _internalSetInitialState();\n browserConfigured = true;\n}\n","import { configureBrowser } from \"./browser-config\";\n\nconfigureBrowser();\n\nexport * from \"braintrust\";\n"]}
package/package.json ADDED
@@ -0,0 +1,58 @@
1
+ {
2
+ "name": "@braintrust/browser",
3
+ "version": "0.0.2-rc.0",
4
+ "description": "Braintrust SDK for browser environments with AsyncLocalStorage polyfill",
5
+ "type": "module",
6
+ "module": "./dist/index.js",
7
+ "types": "./dist/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./dist/index.d.ts",
11
+ "import": "./dist/index.js",
12
+ "default": "./dist/index.js"
13
+ }
14
+ },
15
+ "files": [
16
+ "dist",
17
+ "README.md"
18
+ ],
19
+ "scripts": {
20
+ "build": "tsup",
21
+ "watch": "tsup --watch",
22
+ "clean": "rm -rf dist",
23
+ "test": "vitest run"
24
+ },
25
+ "dependencies": {
26
+ "als-browser": "^1.0.1"
27
+ },
28
+ "devDependencies": {
29
+ "@types/node": "^20.10.5",
30
+ "braintrust": "workspace:*",
31
+ "tsup": "^8.3.5",
32
+ "typescript": "^5.3.3",
33
+ "vitest": "^2.1.9"
34
+ },
35
+ "peerDependencies": {
36
+ "braintrust": ">=3.0.0",
37
+ "zod": "^3.25.34 || ^4.0"
38
+ },
39
+ "peerDependenciesMeta": {
40
+ "braintrust": {
41
+ "optional": false
42
+ },
43
+ "zod": {
44
+ "optional": false
45
+ }
46
+ },
47
+ "repository": {
48
+ "type": "git",
49
+ "url": "git+https://github.com/braintrustdata/braintrust-sdk.git",
50
+ "directory": "sdk/integrations/browser-js"
51
+ },
52
+ "homepage": "https://www.braintrust.dev/docs",
53
+ "license": "Apache-2.0",
54
+ "publishConfig": {
55
+ "access": "public",
56
+ "registry": "https://registry.npmjs.org/"
57
+ }
58
+ }