@browserbasehq/sdk 1.3.0 → 1.3.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.
@@ -0,0 +1,107 @@
1
+ /// <reference types="node" />
2
+ import BrowserbaseAISDK from './integrations/ai-sdk.js';
3
+ export type ClientOptions = {
4
+ apiKey?: string;
5
+ projectId?: string;
6
+ baseURL?: string;
7
+ baseConnectURL?: string;
8
+ };
9
+ export type ConnectOptions = {
10
+ sessionId?: string;
11
+ proxy?: boolean;
12
+ };
13
+ export type LoadOptions = {
14
+ textContent?: boolean;
15
+ } & ConnectOptions;
16
+ export type ScreenshotOptions = {
17
+ fullPage?: boolean;
18
+ } & ConnectOptions;
19
+ export type CreateSessionOptions = {
20
+ projectId?: string;
21
+ extensionId?: string;
22
+ browserSettings?: {
23
+ fingerprint?: {
24
+ browserListQuery?: string;
25
+ httpVersion?: 1 | 2;
26
+ browsers?: Array<'chrome' | 'firefox' | 'edge' | 'safari'>;
27
+ devices?: Array<'desktop' | 'mobile'>;
28
+ locales?: string[];
29
+ operatingSystems?: Array<'windows' | 'macos' | 'linux' | 'ios' | 'android'>;
30
+ screen?: {
31
+ maxHeight?: number;
32
+ maxWidth?: number;
33
+ minHeight?: number;
34
+ minWidth?: number;
35
+ };
36
+ };
37
+ viewport?: {
38
+ width?: number;
39
+ height?: number;
40
+ };
41
+ };
42
+ keepAlive?: boolean;
43
+ timeout?: number;
44
+ };
45
+ export type Session = {
46
+ id: string;
47
+ createdAt: string;
48
+ startedAt: string;
49
+ endedAt?: string;
50
+ expiresAt: string;
51
+ projectId: string;
52
+ status: 'RUNNING' | 'COMPLETED' | 'ERROR' | 'TIMED_OUT';
53
+ taskId?: string;
54
+ proxyBytes?: number;
55
+ avg_cpu_usage?: number;
56
+ memory_usage?: number;
57
+ details?: string;
58
+ logs?: string;
59
+ };
60
+ export type SessionRecording = {
61
+ type?: number;
62
+ time?: string;
63
+ data?: any;
64
+ };
65
+ export type DebugConnectionURLs = {
66
+ debuggerFullscreenUrl?: string;
67
+ debuggerUrl?: string;
68
+ wsUrl?: string;
69
+ };
70
+ export type SessionLog = {
71
+ sessionId?: string;
72
+ id: string;
73
+ timestamp?: string;
74
+ method?: string;
75
+ request?: {
76
+ timestamp?: string;
77
+ params?: any;
78
+ rawBody?: string;
79
+ };
80
+ response?: {
81
+ timestamp?: string;
82
+ result?: any;
83
+ rawBody?: string;
84
+ };
85
+ pageId?: string;
86
+ };
87
+ export default class Browserbase {
88
+ private apiKey;
89
+ private projectId;
90
+ private baseAPIURL;
91
+ private baseConnectURL;
92
+ constructor(options?: ClientOptions);
93
+ getConnectURL({ sessionId, proxy }?: ConnectOptions): string;
94
+ listSessions(): Promise<Session[]>;
95
+ createSession(options?: CreateSessionOptions): Promise<Session>;
96
+ completeSession(sessionId: string): Promise<Session>;
97
+ getSession(sessionId: string): Promise<Session>;
98
+ getSessionRecording(sessionId: string): Promise<SessionRecording[]>;
99
+ getSessionDownloads(sessionId: string, retryInterval?: number, retryCount?: number): Promise<Buffer>;
100
+ getDebugConnectionURLs(sessionId: string): Promise<DebugConnectionURLs>;
101
+ getSessionLogs(sessionId: string): Promise<SessionLog[]>;
102
+ load(url: string | string[], options?: LoadOptions): Promise<string> | AsyncGenerator<string, any, unknown>;
103
+ loadURL(url: string, options?: LoadOptions): Promise<string>;
104
+ loadURLs(urls: string[], options?: LoadOptions): AsyncGenerator<string>;
105
+ screenshot(url: string, options?: ScreenshotOptions): Promise<Buffer>;
106
+ }
107
+ export { Browserbase, BrowserbaseAISDK };
package/dist/index.js ADDED
@@ -0,0 +1,211 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.BrowserbaseAISDK = exports.Browserbase = void 0;
7
+ const puppeteer_core_1 = __importDefault(require("puppeteer-core"));
8
+ const ai_sdk_js_1 = __importDefault(require("./integrations/ai-sdk.js"));
9
+ exports.BrowserbaseAISDK = ai_sdk_js_1.default;
10
+ class Browserbase {
11
+ apiKey;
12
+ projectId;
13
+ baseAPIURL;
14
+ baseConnectURL;
15
+ constructor(options = {}) {
16
+ this.apiKey = options.apiKey || process.env.BROWSERBASE_API_KEY;
17
+ this.projectId = options.projectId || process.env.BROWSERBASE_PROJECT_ID;
18
+ this.baseAPIURL = options.baseURL || 'https://www.browserbase.com';
19
+ this.baseConnectURL =
20
+ options.baseConnectURL || 'wss://connect.browserbase.com';
21
+ }
22
+ getConnectURL({ sessionId, proxy = false } = {}) {
23
+ return `${this.baseConnectURL}?apiKey=${this.apiKey}${sessionId ? `&sessionId=${sessionId}` : ''}${proxy ? `&enableProxy=true` : ''}`;
24
+ }
25
+ async listSessions() {
26
+ const response = await fetch(`${this.baseAPIURL}/v1/sessions`, {
27
+ headers: {
28
+ 'x-bb-api-key': this.apiKey,
29
+ 'Content-Type': 'application/json',
30
+ },
31
+ });
32
+ return await response.json();
33
+ }
34
+ async createSession(options) {
35
+ const mergedOptions = { projectId: this.projectId, ...options };
36
+ if (!mergedOptions.projectId) {
37
+ throw new Error('a projectId is missing: use the options.projectId or BROWSERBASE_PROJECT_ID environment variable to set one.');
38
+ }
39
+ const response = await fetch(`${this.baseAPIURL}/v1/sessions`, {
40
+ method: 'POST',
41
+ headers: {
42
+ 'x-bb-api-key': this.apiKey,
43
+ 'Content-Type': 'application/json',
44
+ },
45
+ body: JSON.stringify(mergedOptions),
46
+ });
47
+ return await response.json();
48
+ }
49
+ async completeSession(sessionId) {
50
+ if (!sessionId || sessionId === '') {
51
+ throw new Error('sessionId is required');
52
+ }
53
+ if (!this.projectId) {
54
+ throw new Error('a projectId is missing: use the options.projectId or BROWSERBASE_PROJECT_ID environment variable to set one.');
55
+ }
56
+ const response = await fetch(`${this.baseAPIURL}/v1/sessions/${sessionId}`, {
57
+ method: 'POST',
58
+ headers: {
59
+ 'x-bb-api-key': this.apiKey,
60
+ 'Content-Type': 'application/json',
61
+ },
62
+ body: JSON.stringify({
63
+ projectId: this.projectId,
64
+ status: 'REQUEST_RELEASE',
65
+ }),
66
+ });
67
+ return await response.json();
68
+ }
69
+ async getSession(sessionId) {
70
+ const response = await fetch(`${this.baseAPIURL}/v1/sessions/${sessionId}`, {
71
+ headers: {
72
+ 'x-bb-api-key': this.apiKey,
73
+ 'Content-Type': 'application/json',
74
+ },
75
+ });
76
+ return await response.json();
77
+ }
78
+ async getSessionRecording(sessionId) {
79
+ const response = await fetch(`${this.baseAPIURL}/v1/sessions/${sessionId}/recording`, {
80
+ headers: {
81
+ 'x-bb-api-key': this.apiKey,
82
+ 'Content-Type': 'application/json',
83
+ },
84
+ });
85
+ return await response.json();
86
+ }
87
+ async getSessionDownloads(sessionId, retryInterval = 2000, retryCount = 2) {
88
+ return new Promise((resolve, reject) => {
89
+ const fetchDownload = async () => {
90
+ const response = await fetch(`${this.baseAPIURL}/v1/sessions/${sessionId}/downloads`, {
91
+ method: 'GET',
92
+ headers: {
93
+ 'x-bb-api-key': this.apiKey,
94
+ },
95
+ });
96
+ const arrayBuffer = await response.arrayBuffer();
97
+ if (arrayBuffer.byteLength > 0) {
98
+ resolve(Buffer.from(arrayBuffer));
99
+ }
100
+ else {
101
+ retryCount--;
102
+ if (retryCount <= 0) {
103
+ reject();
104
+ }
105
+ setTimeout(fetchDownload, retryInterval);
106
+ }
107
+ };
108
+ fetchDownload();
109
+ });
110
+ }
111
+ async getDebugConnectionURLs(sessionId) {
112
+ const response = await fetch(`${this.baseAPIURL}/v1/sessions/${sessionId}/debug`, {
113
+ method: 'GET',
114
+ headers: {
115
+ 'x-bb-api-key': this.apiKey,
116
+ 'Content-Type': 'application/json',
117
+ },
118
+ });
119
+ const json = await response.json();
120
+ return json;
121
+ }
122
+ async getSessionLogs(sessionId) {
123
+ const response = await fetch(`${this.baseAPIURL}/v1/sessions/${sessionId}/logs`, {
124
+ headers: {
125
+ 'x-bb-api-key': this.apiKey,
126
+ 'Content-Type': 'application/json',
127
+ },
128
+ });
129
+ return await response.json();
130
+ }
131
+ load(url, options = {}) {
132
+ if (typeof url === 'string') {
133
+ return this.loadURL(url, options);
134
+ }
135
+ else if (Array.isArray(url)) {
136
+ return this.loadURLs(url, options);
137
+ }
138
+ else {
139
+ throw new TypeError('Input must be a URL string or array of URLs');
140
+ }
141
+ }
142
+ async loadURL(url, options = {}) {
143
+ if (!url) {
144
+ throw new Error('Page URL was not provided');
145
+ }
146
+ const browser = await puppeteer_core_1.default.connect({
147
+ browserWSEndpoint: this.getConnectURL({
148
+ sessionId: options.sessionId,
149
+ proxy: options.proxy,
150
+ }),
151
+ });
152
+ const pages = await browser.pages();
153
+ const page = pages[0];
154
+ await page.goto(url);
155
+ let html = await page.content();
156
+ if (options.textContent) {
157
+ const readable = await page.evaluate(`
158
+ import('https://cdn.skypack.dev/@mozilla/readability').then(readability => {
159
+ return new readability.Readability(document).parse()
160
+ })`);
161
+ html = `${readable.title}\n${readable.textContent}`;
162
+ }
163
+ await browser.close();
164
+ return html;
165
+ }
166
+ async *loadURLs(urls, options = {}) {
167
+ if (!urls.length) {
168
+ throw new Error('Page URLs were not provided');
169
+ }
170
+ const browser = await puppeteer_core_1.default.connect({
171
+ browserWSEndpoint: this.getConnectURL({
172
+ sessionId: options.sessionId,
173
+ proxy: options.proxy,
174
+ }),
175
+ });
176
+ const pages = await browser.pages();
177
+ const page = pages[0];
178
+ for (const url of urls) {
179
+ await page.goto(url);
180
+ let html = await page.content();
181
+ if (options.textContent) {
182
+ const readable = await page.evaluate(`
183
+ import('https://cdn.skypack.dev/@mozilla/readability').then(readability => {
184
+ return new readability.Readability(document).parse()
185
+ })`);
186
+ html = `${readable.title}\n${readable.textContent}`;
187
+ }
188
+ yield html;
189
+ }
190
+ await browser.close();
191
+ }
192
+ async screenshot(url, options = { fullPage: false }) {
193
+ if (!url) {
194
+ throw new Error('Page URL was not provided');
195
+ }
196
+ const browser = await puppeteer_core_1.default.connect({
197
+ browserWSEndpoint: this.getConnectURL({
198
+ sessionId: options.sessionId,
199
+ proxy: options.proxy,
200
+ }),
201
+ });
202
+ const pages = await browser.pages();
203
+ const page = pages[0];
204
+ await page.goto(url);
205
+ const screenshot = await page.screenshot({ fullPage: options.fullPage });
206
+ await browser.close();
207
+ return screenshot;
208
+ }
209
+ }
210
+ exports.default = Browserbase;
211
+ exports.Browserbase = Browserbase;
@@ -0,0 +1,18 @@
1
+ import { z } from 'zod';
2
+ import Browserbase, { LoadOptions } from '../index.js';
3
+ export default function BrowserbaseAISDK(b: Browserbase, options?: LoadOptions): {
4
+ description: string;
5
+ parameters: z.ZodObject<{
6
+ url: z.ZodString;
7
+ }, "strip", z.ZodTypeAny, {
8
+ url?: string;
9
+ }, {
10
+ url?: string;
11
+ }>;
12
+ execute: ({ url }: {
13
+ url: string;
14
+ }) => Promise<{
15
+ page: string;
16
+ }>;
17
+ };
18
+ export { BrowserbaseAISDK };
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.BrowserbaseAISDK = void 0;
4
+ const zod_1 = require("zod");
5
+ function BrowserbaseAISDK(b, options = {}) {
6
+ return {
7
+ description: 'Load a webpage in a browser hosted by Browserbase and return its contents',
8
+ parameters: zod_1.z.object({
9
+ url: zod_1.z.string().describe('Page URL to be loaded'),
10
+ }),
11
+ execute: async ({ url }) => {
12
+ const page = await b.loadURL(url, options);
13
+ return { page };
14
+ },
15
+ };
16
+ }
17
+ exports.default = BrowserbaseAISDK;
18
+ exports.BrowserbaseAISDK = BrowserbaseAISDK;
package/package.json CHANGED
@@ -1,8 +1,9 @@
1
1
  {
2
2
  "name": "@browserbasehq/sdk",
3
- "version": "1.3.0",
3
+ "version": "1.3.1",
4
4
  "description": "Browserbase JS SDK",
5
5
  "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
6
7
  "scripts": {
7
8
  "build": "tsc -p tsconfig.json",
8
9
  "test": "tsx --test test/*"