@browserbasehq/sdk 1.0.1 → 1.1.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/README.md +63 -20
- package/dist/index.d.ts +94 -8
- package/dist/index.js +115 -9
- package/dist/integrations/ai-sdk.d.ts +2 -2
- package/dist/integrations/ai-sdk.js +1 -1
- package/logo/dark.png +0 -0
- package/logo/light.png +0 -0
- package/package.json +1 -1
- package/src/index.ts +257 -18
- package/src/integrations/ai-sdk.ts +3 -3
- package/test/lib.test.ts +29 -1
package/README.md
CHANGED
|
@@ -1,35 +1,69 @@
|
|
|
1
|
-
|
|
1
|
+
<p align="center">
|
|
2
|
+
<picture>
|
|
3
|
+
<source media="(prefers-color-scheme: dark)" srcset="logo/dark.png"/>
|
|
4
|
+
<img alt="Browserbase logo" src="logo/light.png" style="max-width: 250px;"/>
|
|
5
|
+
</picture>
|
|
6
|
+
</p>
|
|
2
7
|
|
|
3
|
-
|
|
8
|
+
<p align="center">
|
|
9
|
+
<a href="https://docs.browserbase.com">Documentation</a>
|
|
10
|
+
<span> · </span>
|
|
11
|
+
<a href="https://www.browserbase.com/playground">Playground</a>
|
|
12
|
+
</p>
|
|
13
|
+
<br/>
|
|
14
|
+
|
|
15
|
+
## Browserbase JS SDK
|
|
16
|
+
|
|
17
|
+
Browserbase is the best developer platform to reliably run, manage, and monitor headless browsers.
|
|
18
|
+
|
|
19
|
+
Get browsers' full control and leverage Browserbase's
|
|
20
|
+
[Infrastructure](https://docs.browserbase.com/under-the-hood), [Stealth Mode](https://docs.browserbase.com/features/stealth-mode), and
|
|
21
|
+
[Session Debugger](https://docs.browserbase.com/features/sessions) to power your automation, test suites,
|
|
22
|
+
and LLM data retrievals.
|
|
4
23
|
|
|
5
24
|
## Installation and setup
|
|
6
25
|
|
|
7
|
-
|
|
8
|
-
- Install the required dependencies:
|
|
26
|
+
### 1. Install the SDK:
|
|
9
27
|
|
|
28
|
+
```bash
|
|
29
|
+
npm install -S @browserbasehq/sdk
|
|
10
30
|
```
|
|
11
|
-
|
|
12
|
-
|
|
31
|
+
|
|
32
|
+
### 2. Get your Browserbase API Key and Project ID:
|
|
33
|
+
|
|
34
|
+
- [Create an account](https://www.browserbase.com/sign-up) or [log in to Browserbase](https://www.browserbase.com/sign-in)
|
|
35
|
+
- Copy your API Key and Project ID [from your Settings page](https://www.browserbase.com/settings)
|
|
13
36
|
|
|
14
37
|
## Usage
|
|
15
38
|
|
|
16
39
|
```js
|
|
17
|
-
import { Browserbase
|
|
40
|
+
import { Browserbase } from '@browserbasehq/sdk'
|
|
18
41
|
|
|
19
42
|
// Init the SDK
|
|
20
43
|
const browserbase = new Browserbase()
|
|
21
44
|
|
|
22
45
|
// Load a webpage
|
|
23
|
-
const
|
|
46
|
+
const rawHtml = await browserbase.load('https://www.browserbase.com')
|
|
24
47
|
|
|
25
48
|
// Load multiple webpages (returns iterator)
|
|
26
|
-
const
|
|
49
|
+
const rawHtmls = browserbase.loadURLs([
|
|
50
|
+
'https://www.browserbase.com',
|
|
51
|
+
'https://docs.browserbase.com',
|
|
52
|
+
])
|
|
53
|
+
|
|
54
|
+
for await (let rawHtml of rawHtmls) {
|
|
55
|
+
// ...
|
|
56
|
+
}
|
|
27
57
|
|
|
28
58
|
// Text-only mode
|
|
29
|
-
const
|
|
59
|
+
const text = await browserbase.load('https://www.browserbase.com', {
|
|
60
|
+
textContent: true,
|
|
61
|
+
})
|
|
30
62
|
|
|
31
63
|
// Screenshot (returns bytes)
|
|
32
|
-
const result = await browserbase.screenshot(
|
|
64
|
+
const result = await browserbase.screenshot('https://www.browserbase.com', {
|
|
65
|
+
textContent: true,
|
|
66
|
+
})
|
|
33
67
|
```
|
|
34
68
|
|
|
35
69
|
### Vercel AI SDK Integration
|
|
@@ -41,29 +75,38 @@ npm i ai openai zod
|
|
|
41
75
|
```
|
|
42
76
|
|
|
43
77
|
```js
|
|
44
|
-
import OpenAI from 'openai'
|
|
78
|
+
import OpenAI from 'openai'
|
|
45
79
|
import { Browserbase } from '@browserbasehq/sdk'
|
|
46
80
|
import { BrowserbaseAISDK } from '@browserbasehq/sdk/dist/integrations/ai-sdk'
|
|
47
|
-
import {
|
|
81
|
+
import {
|
|
82
|
+
OpenAIStream,
|
|
83
|
+
StreamingTextResponse,
|
|
84
|
+
experimental_generateText,
|
|
85
|
+
} from 'ai'
|
|
48
86
|
|
|
49
87
|
// Create new OpenAI client
|
|
50
88
|
const openai = new OpenAI({
|
|
51
89
|
apiKey: process.env.OPENAI_API_KEY,
|
|
52
|
-
})
|
|
90
|
+
})
|
|
53
91
|
|
|
54
92
|
// Init the Browserbase SDK
|
|
55
|
-
const browserbase = new Browserbase()
|
|
93
|
+
const browserbase = new Browserbase()
|
|
56
94
|
|
|
57
95
|
// Init the tool
|
|
58
|
-
const browserTool = BrowserbaseAISDK(browserbase, { textContent: true })
|
|
96
|
+
const browserTool = BrowserbaseAISDK(browserbase, { textContent: true })
|
|
59
97
|
|
|
60
98
|
// Load completions
|
|
61
99
|
const result = await experimental_generateText({
|
|
62
100
|
model: openai.chat('gpt-3.5-turbo'),
|
|
63
101
|
tools: {
|
|
64
|
-
browserTool
|
|
102
|
+
browserTool,
|
|
65
103
|
},
|
|
66
|
-
prompt:
|
|
67
|
-
|
|
68
|
-
});
|
|
104
|
+
prompt: 'What is the weather in San Francisco?',
|
|
105
|
+
})
|
|
69
106
|
```
|
|
107
|
+
|
|
108
|
+
## Further reading
|
|
109
|
+
|
|
110
|
+
- [See how to leverage the Session Debugger for faster development](https://docs.browserbase.com/guides/browser-remote-control#accelerate-your-local-development-with-remote-debugging)
|
|
111
|
+
- [Learn more about Browserbase infrastructure](https://docs.browserbase.com/under-the-hood)
|
|
112
|
+
- [Sessions API Reference](https://docs.browserbase.com/api-reference/list-all-sessions)
|
package/dist/index.d.ts
CHANGED
|
@@ -1,16 +1,102 @@
|
|
|
1
1
|
/// <reference types="node" resolution-mode="require"/>
|
|
2
|
-
export type
|
|
3
|
-
|
|
2
|
+
export type ClientOptions = {
|
|
3
|
+
apiKey?: string;
|
|
4
|
+
projectId?: string;
|
|
5
|
+
baseURL?: string;
|
|
6
|
+
baseConnectURL?: string;
|
|
7
|
+
};
|
|
8
|
+
export type ConnectOptions = {
|
|
9
|
+
sessionId?: string;
|
|
10
|
+
proxy?: boolean;
|
|
4
11
|
};
|
|
5
|
-
export type
|
|
12
|
+
export type LoadOptions = {
|
|
13
|
+
textContent?: boolean;
|
|
14
|
+
} & ConnectOptions;
|
|
15
|
+
export type ScreenshotOptions = {
|
|
6
16
|
fullPage?: boolean;
|
|
17
|
+
} & ConnectOptions;
|
|
18
|
+
export type CreateSessionOptions = {
|
|
19
|
+
projectId?: string;
|
|
20
|
+
extensionId?: string;
|
|
21
|
+
fingerprint?: {
|
|
22
|
+
browserListQuery?: string;
|
|
23
|
+
httpVersion?: 1 | 2;
|
|
24
|
+
browsers?: Array<'chrome' | 'firefox' | 'edge' | 'safari'>;
|
|
25
|
+
devices?: Array<'desktop' | 'mobile'>;
|
|
26
|
+
locales?: string[];
|
|
27
|
+
operatingSystems?: Array<'windows' | 'macos' | 'linux' | 'ios' | 'android'>;
|
|
28
|
+
screen?: {
|
|
29
|
+
maxHeight?: number;
|
|
30
|
+
maxWidth?: number;
|
|
31
|
+
minHeight?: number;
|
|
32
|
+
minWidth?: number;
|
|
33
|
+
};
|
|
34
|
+
};
|
|
35
|
+
};
|
|
36
|
+
export type Session = {
|
|
37
|
+
id: string;
|
|
38
|
+
createdAt: string;
|
|
39
|
+
startedAt: string;
|
|
40
|
+
endedAt: string;
|
|
41
|
+
projectId: string;
|
|
42
|
+
status: 'NEW' | 'CREATED' | 'ERROR' | 'RUNNING' | 'REQUEST_RELEASE' | 'RELEASING';
|
|
43
|
+
taskId: string;
|
|
44
|
+
proxyBytes: number;
|
|
45
|
+
expiresAt: string;
|
|
46
|
+
avg_cpu_usage: number;
|
|
47
|
+
memory_usage: number;
|
|
48
|
+
details: string;
|
|
49
|
+
logs: string;
|
|
50
|
+
};
|
|
51
|
+
export type UpdateSessionOptions = {
|
|
52
|
+
projectId?: string;
|
|
53
|
+
status: Session['status'];
|
|
54
|
+
};
|
|
55
|
+
export type SessionRecording = {
|
|
56
|
+
type: number;
|
|
57
|
+
timestamp: number;
|
|
58
|
+
data: object;
|
|
59
|
+
};
|
|
60
|
+
export type SessionLog = {
|
|
61
|
+
sessionId: string;
|
|
62
|
+
id: string;
|
|
63
|
+
timestamp: string;
|
|
64
|
+
method: string;
|
|
65
|
+
request: {
|
|
66
|
+
timestamp: string;
|
|
67
|
+
params: object;
|
|
68
|
+
rawBody: string;
|
|
69
|
+
};
|
|
70
|
+
response: {
|
|
71
|
+
timestamp: string;
|
|
72
|
+
result: object;
|
|
73
|
+
rawBody: string;
|
|
74
|
+
};
|
|
75
|
+
pageId: string;
|
|
76
|
+
};
|
|
77
|
+
export type DebugConnectionURLs = {
|
|
78
|
+
debuggerFullscreenUrl: string;
|
|
79
|
+
debuggerUrl: string;
|
|
80
|
+
wsUrl: string;
|
|
7
81
|
};
|
|
8
82
|
export default class Browserbase {
|
|
9
83
|
private apiKey;
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
84
|
+
private projectId;
|
|
85
|
+
private baseApiURL;
|
|
86
|
+
private baseConnectURL;
|
|
87
|
+
constructor(options?: ClientOptions);
|
|
88
|
+
connectUrl({ sessionId, proxy }?: ConnectOptions): string;
|
|
89
|
+
listSessions(): Promise<Session[]>;
|
|
90
|
+
createSession(options?: CreateSessionOptions): Promise<Session>;
|
|
91
|
+
getSession(sessionId: string): Promise<Session>;
|
|
92
|
+
updateSession(sessionId: string, options: UpdateSessionOptions): Promise<Session>;
|
|
93
|
+
getSessionRecording(sessionId: string): Promise<SessionRecording[]>;
|
|
94
|
+
getSessionLogs(sessionId: string): Promise<SessionLog[]>;
|
|
95
|
+
getSessionDownloads(sessionId: string, filePath: string, retryInterval?: number, retryCount?: number): Promise<void>;
|
|
96
|
+
getDebugConnectionURLs(sessionId: string): Promise<DebugConnectionURLs>;
|
|
97
|
+
load(url: string | string[], options?: LoadOptions): Promise<string> | AsyncGenerator<string, any, unknown>;
|
|
98
|
+
loadUrl(url: string, options?: LoadOptions): Promise<string>;
|
|
99
|
+
loadUrls(urls: string[], options?: LoadOptions): AsyncGenerator<string>;
|
|
100
|
+
screenshot(url: string, options?: ScreenshotOptions): Promise<Buffer>;
|
|
15
101
|
}
|
|
16
102
|
export { Browserbase };
|
package/dist/index.js
CHANGED
|
@@ -1,25 +1,131 @@
|
|
|
1
1
|
import { chromium } from 'playwright';
|
|
2
|
+
import fs from 'fs/promises';
|
|
2
3
|
export default class Browserbase {
|
|
3
4
|
apiKey;
|
|
4
|
-
|
|
5
|
-
|
|
5
|
+
projectId;
|
|
6
|
+
baseApiURL;
|
|
7
|
+
baseConnectURL;
|
|
8
|
+
constructor(options = {}) {
|
|
9
|
+
this.apiKey = options.apiKey || process.env.BROWSERBASE_API_KEY;
|
|
10
|
+
this.projectId = options.projectId || process.env.BROWSERBASE_PROJECT_ID;
|
|
11
|
+
this.baseApiURL = options.baseURL || 'https://www.browserbase.com';
|
|
12
|
+
this.baseConnectURL =
|
|
13
|
+
options.baseConnectURL || 'wss://connect.browserbase.com';
|
|
14
|
+
}
|
|
15
|
+
connectUrl({ sessionId, proxy = false } = {}) {
|
|
16
|
+
return `${this.baseConnectURL}?apiKey=${this.apiKey}${sessionId ? `&sessionId=${sessionId}` : ''}${proxy ? `&enableProxy=true` : ''}`;
|
|
17
|
+
}
|
|
18
|
+
async listSessions() {
|
|
19
|
+
const response = await fetch(`${this.baseApiURL}/v1/sessions`, {
|
|
20
|
+
headers: {
|
|
21
|
+
'x-bb-api-key': this.apiKey,
|
|
22
|
+
'Content-Type': 'application/json',
|
|
23
|
+
},
|
|
24
|
+
});
|
|
25
|
+
return await response.json();
|
|
26
|
+
}
|
|
27
|
+
async createSession(options) {
|
|
28
|
+
const response = await fetch(`${this.baseApiURL}/v1/sessions`, {
|
|
29
|
+
method: 'POST',
|
|
30
|
+
headers: {
|
|
31
|
+
'x-bb-api-key': this.apiKey,
|
|
32
|
+
'Content-Type': 'application/json',
|
|
33
|
+
},
|
|
34
|
+
body: JSON.stringify({ projectId: this.projectId, ...options }),
|
|
35
|
+
});
|
|
36
|
+
return await response.json();
|
|
37
|
+
}
|
|
38
|
+
async getSession(sessionId) {
|
|
39
|
+
const response = await fetch(`${this.baseApiURL}/v1/sessions/${sessionId}`, {
|
|
40
|
+
headers: {
|
|
41
|
+
'x-bb-api-key': this.apiKey,
|
|
42
|
+
'Content-Type': 'application/json',
|
|
43
|
+
},
|
|
44
|
+
});
|
|
45
|
+
return await response.json();
|
|
46
|
+
}
|
|
47
|
+
async updateSession(sessionId, options) {
|
|
48
|
+
const response = await fetch(`${this.baseApiURL}/v1/sessions/${sessionId}`, {
|
|
49
|
+
method: 'POST',
|
|
50
|
+
headers: {
|
|
51
|
+
'x-bb-api-key': this.apiKey,
|
|
52
|
+
'Content-Type': 'application/json',
|
|
53
|
+
},
|
|
54
|
+
body: JSON.stringify({ projectId: this.projectId, ...options }),
|
|
55
|
+
});
|
|
56
|
+
return await response.json();
|
|
57
|
+
}
|
|
58
|
+
async getSessionRecording(sessionId) {
|
|
59
|
+
const response = await fetch(`${this.baseApiURL}/v1/sessions/${sessionId}/recording`, {
|
|
60
|
+
headers: {
|
|
61
|
+
'x-bb-api-key': this.apiKey,
|
|
62
|
+
'Content-Type': 'application/json',
|
|
63
|
+
},
|
|
64
|
+
});
|
|
65
|
+
return await response.json();
|
|
66
|
+
}
|
|
67
|
+
async getSessionLogs(sessionId) {
|
|
68
|
+
const response = await fetch(`${this.baseApiURL}/v1/sessions/${sessionId}/logs`, {
|
|
69
|
+
headers: {
|
|
70
|
+
'x-bb-api-key': this.apiKey,
|
|
71
|
+
'Content-Type': 'application/json',
|
|
72
|
+
},
|
|
73
|
+
});
|
|
74
|
+
return await response.json();
|
|
75
|
+
}
|
|
76
|
+
async getSessionDownloads(sessionId, filePath, retryInterval = 2000, retryCount = 2) {
|
|
77
|
+
return new Promise((resolve, reject) => {
|
|
78
|
+
const fetchDownload = async () => {
|
|
79
|
+
const response = await fetch(`${this.baseApiURL}/v1/sessions/${sessionId}/downloads`, {
|
|
80
|
+
method: 'GET',
|
|
81
|
+
headers: {
|
|
82
|
+
'x-bb-api-key': this.apiKey,
|
|
83
|
+
},
|
|
84
|
+
});
|
|
85
|
+
const arrayBuffer = await response.arrayBuffer();
|
|
86
|
+
if (arrayBuffer.byteLength > 0) {
|
|
87
|
+
const buffer = Buffer.from(arrayBuffer);
|
|
88
|
+
await fs.writeFile(filePath, buffer);
|
|
89
|
+
resolve();
|
|
90
|
+
}
|
|
91
|
+
else {
|
|
92
|
+
retryCount--;
|
|
93
|
+
if (retryCount <= 0) {
|
|
94
|
+
reject();
|
|
95
|
+
}
|
|
96
|
+
setTimeout(fetchDownload, retryInterval);
|
|
97
|
+
}
|
|
98
|
+
};
|
|
99
|
+
fetchDownload();
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
async getDebugConnectionURLs(sessionId) {
|
|
103
|
+
const response = await fetch(`${this.baseApiURL}/v1/sessions/${sessionId}/debug`, {
|
|
104
|
+
method: 'GET',
|
|
105
|
+
headers: {
|
|
106
|
+
'x-bb-api-key': this.apiKey,
|
|
107
|
+
'Content-Type': 'application/json',
|
|
108
|
+
},
|
|
109
|
+
});
|
|
110
|
+
const json = await response.json();
|
|
111
|
+
return json;
|
|
6
112
|
}
|
|
7
113
|
load(url, options = {}) {
|
|
8
114
|
if (typeof url === 'string') {
|
|
9
|
-
return this.
|
|
115
|
+
return this.loadUrl(url, options);
|
|
10
116
|
}
|
|
11
117
|
else if (Array.isArray(url)) {
|
|
12
|
-
return this.
|
|
118
|
+
return this.loadUrls(url, options);
|
|
13
119
|
}
|
|
14
120
|
else {
|
|
15
121
|
throw new TypeError('Input must be a URL string or array of URLs');
|
|
16
122
|
}
|
|
17
123
|
}
|
|
18
|
-
async
|
|
124
|
+
async loadUrl(url, options = {}) {
|
|
19
125
|
if (!url) {
|
|
20
126
|
throw new Error('Page URL was not provided');
|
|
21
127
|
}
|
|
22
|
-
const browser = await chromium.connectOverCDP(
|
|
128
|
+
const browser = await chromium.connectOverCDP(this.connectUrl({ sessionId: options.sessionId, proxy: options.proxy }));
|
|
23
129
|
const defaultContext = browser.contexts()[0];
|
|
24
130
|
const page = defaultContext.pages()[0];
|
|
25
131
|
await page.goto(url);
|
|
@@ -36,11 +142,11 @@ export default class Browserbase {
|
|
|
36
142
|
await browser.close();
|
|
37
143
|
return html;
|
|
38
144
|
}
|
|
39
|
-
async *
|
|
145
|
+
async *loadUrls(urls, options = {}) {
|
|
40
146
|
if (!urls.length) {
|
|
41
147
|
throw new Error('Page URLs were not provided');
|
|
42
148
|
}
|
|
43
|
-
const browser = await chromium.connectOverCDP(
|
|
149
|
+
const browser = await chromium.connectOverCDP(this.connectUrl({ sessionId: options.sessionId, proxy: options.proxy }));
|
|
44
150
|
const defaultContext = browser.contexts()[0];
|
|
45
151
|
const page = defaultContext.pages()[0];
|
|
46
152
|
for (const url of urls) {
|
|
@@ -63,7 +169,7 @@ export default class Browserbase {
|
|
|
63
169
|
if (!url) {
|
|
64
170
|
throw new Error('Page URL was not provided');
|
|
65
171
|
}
|
|
66
|
-
const browser = await chromium.connectOverCDP(
|
|
172
|
+
const browser = await chromium.connectOverCDP(this.connectUrl({ sessionId: options.sessionId, proxy: options.proxy }));
|
|
67
173
|
const page = await browser.newPage();
|
|
68
174
|
await page.goto(url);
|
|
69
175
|
const screenshot = await page.screenshot({ fullPage: options.fullPage });
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { z } from 'zod';
|
|
2
|
-
import Browserbase, {
|
|
3
|
-
export default function BrowserbaseAISDK(b: Browserbase, options?:
|
|
2
|
+
import Browserbase, { LoadOptions } from '../index.js';
|
|
3
|
+
export default function BrowserbaseAISDK(b: Browserbase, options?: LoadOptions): {
|
|
4
4
|
description: string;
|
|
5
5
|
parameters: z.ZodObject<{
|
|
6
6
|
url: z.ZodString;
|
|
@@ -6,7 +6,7 @@ export default function BrowserbaseAISDK(b, options = {}) {
|
|
|
6
6
|
url: z.string().describe('Page URL to be loaded'),
|
|
7
7
|
}),
|
|
8
8
|
execute: async ({ url }) => {
|
|
9
|
-
const page = await b.
|
|
9
|
+
const page = await b.loadUrl(url, options);
|
|
10
10
|
return { page };
|
|
11
11
|
},
|
|
12
12
|
};
|
package/logo/dark.png
ADDED
|
Binary file
|
package/logo/light.png
ADDED
|
Binary file
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -1,40 +1,279 @@
|
|
|
1
1
|
import { chromium } from 'playwright'
|
|
2
|
+
import fs from 'fs/promises'
|
|
2
3
|
|
|
3
|
-
export type
|
|
4
|
-
|
|
4
|
+
export type ClientOptions = {
|
|
5
|
+
apiKey?: string
|
|
6
|
+
projectId?: string
|
|
7
|
+
baseURL?: string
|
|
8
|
+
baseConnectURL?: string
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export type ConnectOptions = {
|
|
12
|
+
sessionId?: string
|
|
13
|
+
proxy?: boolean
|
|
5
14
|
}
|
|
6
15
|
|
|
7
|
-
export type
|
|
16
|
+
export type LoadOptions = {
|
|
17
|
+
textContent?: boolean
|
|
18
|
+
} & ConnectOptions
|
|
19
|
+
|
|
20
|
+
export type ScreenshotOptions = {
|
|
8
21
|
fullPage?: boolean
|
|
22
|
+
} & ConnectOptions
|
|
23
|
+
|
|
24
|
+
export type CreateSessionOptions = {
|
|
25
|
+
projectId?: string
|
|
26
|
+
extensionId?: string
|
|
27
|
+
fingerprint?: {
|
|
28
|
+
browserListQuery?: string
|
|
29
|
+
httpVersion?: 1 | 2
|
|
30
|
+
browsers?: Array<'chrome' | 'firefox' | 'edge' | 'safari'>
|
|
31
|
+
devices?: Array<'desktop' | 'mobile'>
|
|
32
|
+
locales?: string[]
|
|
33
|
+
operatingSystems?: Array<'windows' | 'macos' | 'linux' | 'ios' | 'android'>
|
|
34
|
+
screen?: {
|
|
35
|
+
maxHeight?: number
|
|
36
|
+
maxWidth?: number
|
|
37
|
+
minHeight?: number
|
|
38
|
+
minWidth?: number
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export type Session = {
|
|
44
|
+
id: string
|
|
45
|
+
createdAt: string
|
|
46
|
+
startedAt: string
|
|
47
|
+
endedAt: string
|
|
48
|
+
projectId: string
|
|
49
|
+
status:
|
|
50
|
+
| 'NEW'
|
|
51
|
+
| 'CREATED'
|
|
52
|
+
| 'ERROR'
|
|
53
|
+
| 'RUNNING'
|
|
54
|
+
| 'REQUEST_RELEASE'
|
|
55
|
+
| 'RELEASING'
|
|
56
|
+
taskId: string
|
|
57
|
+
proxyBytes: number
|
|
58
|
+
expiresAt: string
|
|
59
|
+
avg_cpu_usage: number
|
|
60
|
+
memory_usage: number
|
|
61
|
+
details: string
|
|
62
|
+
logs: string
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export type UpdateSessionOptions = {
|
|
66
|
+
projectId?: string
|
|
67
|
+
status: Session['status']
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export type SessionRecording = {
|
|
71
|
+
type: number
|
|
72
|
+
timestamp: number
|
|
73
|
+
data: object
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export type SessionLog = {
|
|
77
|
+
sessionId: string
|
|
78
|
+
id: string
|
|
79
|
+
timestamp: string
|
|
80
|
+
method: string
|
|
81
|
+
request: {
|
|
82
|
+
timestamp: string
|
|
83
|
+
params: object
|
|
84
|
+
rawBody: string
|
|
85
|
+
}
|
|
86
|
+
response: {
|
|
87
|
+
timestamp: string
|
|
88
|
+
result: object
|
|
89
|
+
rawBody: string
|
|
90
|
+
}
|
|
91
|
+
pageId: string
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
export type DebugConnectionURLs = {
|
|
95
|
+
debuggerFullscreenUrl: string
|
|
96
|
+
debuggerUrl: string
|
|
97
|
+
wsUrl: string
|
|
9
98
|
}
|
|
10
99
|
|
|
11
100
|
export default class Browserbase {
|
|
12
101
|
private apiKey: string
|
|
102
|
+
private projectId: string
|
|
103
|
+
private baseApiURL: string
|
|
104
|
+
private baseConnectURL: string
|
|
13
105
|
|
|
14
|
-
constructor(
|
|
15
|
-
this.apiKey = apiKey || process.env.BROWSERBASE_API_KEY!
|
|
106
|
+
constructor(options: ClientOptions = {}) {
|
|
107
|
+
this.apiKey = options.apiKey || process.env.BROWSERBASE_API_KEY!
|
|
108
|
+
this.projectId = options.projectId || process.env.BROWSERBASE_PROJECT_ID!
|
|
109
|
+
this.baseApiURL = options.baseURL || 'https://www.browserbase.com'
|
|
110
|
+
this.baseConnectURL =
|
|
111
|
+
options.baseConnectURL || 'wss://connect.browserbase.com'
|
|
16
112
|
}
|
|
17
113
|
|
|
18
|
-
|
|
114
|
+
connectUrl({ sessionId, proxy = false }: ConnectOptions = {}): string {
|
|
115
|
+
return `${this.baseConnectURL}?apiKey=${this.apiKey}${
|
|
116
|
+
sessionId ? `&sessionId=${sessionId}` : ''
|
|
117
|
+
}${proxy ? `&enableProxy=true` : ''}`
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
async listSessions(): Promise<Session[]> {
|
|
121
|
+
const response = await fetch(`${this.baseApiURL}/v1/sessions`, {
|
|
122
|
+
headers: {
|
|
123
|
+
'x-bb-api-key': this.apiKey,
|
|
124
|
+
'Content-Type': 'application/json',
|
|
125
|
+
},
|
|
126
|
+
})
|
|
127
|
+
|
|
128
|
+
return await response.json()
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
async createSession(options?: CreateSessionOptions): Promise<Session> {
|
|
132
|
+
const response = await fetch(`${this.baseApiURL}/v1/sessions`, {
|
|
133
|
+
method: 'POST',
|
|
134
|
+
headers: {
|
|
135
|
+
'x-bb-api-key': this.apiKey,
|
|
136
|
+
'Content-Type': 'application/json',
|
|
137
|
+
},
|
|
138
|
+
body: JSON.stringify({ projectId: this.projectId, ...options }),
|
|
139
|
+
})
|
|
140
|
+
|
|
141
|
+
return await response.json()
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
async getSession(sessionId: string): Promise<Session> {
|
|
145
|
+
const response = await fetch(
|
|
146
|
+
`${this.baseApiURL}/v1/sessions/${sessionId}`,
|
|
147
|
+
{
|
|
148
|
+
headers: {
|
|
149
|
+
'x-bb-api-key': this.apiKey,
|
|
150
|
+
'Content-Type': 'application/json',
|
|
151
|
+
},
|
|
152
|
+
}
|
|
153
|
+
)
|
|
154
|
+
|
|
155
|
+
return await response.json()
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
async updateSession(
|
|
159
|
+
sessionId: string,
|
|
160
|
+
options: UpdateSessionOptions
|
|
161
|
+
): Promise<Session> {
|
|
162
|
+
const response = await fetch(
|
|
163
|
+
`${this.baseApiURL}/v1/sessions/${sessionId}`,
|
|
164
|
+
{
|
|
165
|
+
method: 'POST',
|
|
166
|
+
headers: {
|
|
167
|
+
'x-bb-api-key': this.apiKey,
|
|
168
|
+
'Content-Type': 'application/json',
|
|
169
|
+
},
|
|
170
|
+
body: JSON.stringify({ projectId: this.projectId, ...options }),
|
|
171
|
+
}
|
|
172
|
+
)
|
|
173
|
+
|
|
174
|
+
return await response.json()
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
async getSessionRecording(sessionId: string): Promise<SessionRecording[]> {
|
|
178
|
+
const response = await fetch(
|
|
179
|
+
`${this.baseApiURL}/v1/sessions/${sessionId}/recording`,
|
|
180
|
+
{
|
|
181
|
+
headers: {
|
|
182
|
+
'x-bb-api-key': this.apiKey,
|
|
183
|
+
'Content-Type': 'application/json',
|
|
184
|
+
},
|
|
185
|
+
}
|
|
186
|
+
)
|
|
187
|
+
|
|
188
|
+
return await response.json()
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
async getSessionLogs(sessionId: string): Promise<SessionLog[]> {
|
|
192
|
+
const response = await fetch(
|
|
193
|
+
`${this.baseApiURL}/v1/sessions/${sessionId}/logs`,
|
|
194
|
+
{
|
|
195
|
+
headers: {
|
|
196
|
+
'x-bb-api-key': this.apiKey,
|
|
197
|
+
'Content-Type': 'application/json',
|
|
198
|
+
},
|
|
199
|
+
}
|
|
200
|
+
)
|
|
201
|
+
|
|
202
|
+
return await response.json()
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
async getSessionDownloads(
|
|
206
|
+
sessionId: string,
|
|
207
|
+
filePath: string,
|
|
208
|
+
retryInterval: number = 2000,
|
|
209
|
+
retryCount: number = 2
|
|
210
|
+
) {
|
|
211
|
+
return new Promise<void>((resolve, reject) => {
|
|
212
|
+
const fetchDownload = async () => {
|
|
213
|
+
const response = await fetch(
|
|
214
|
+
`${this.baseApiURL}/v1/sessions/${sessionId}/downloads`,
|
|
215
|
+
{
|
|
216
|
+
method: 'GET',
|
|
217
|
+
headers: {
|
|
218
|
+
'x-bb-api-key': this.apiKey,
|
|
219
|
+
},
|
|
220
|
+
}
|
|
221
|
+
)
|
|
222
|
+
|
|
223
|
+
const arrayBuffer = await response.arrayBuffer()
|
|
224
|
+
if (arrayBuffer.byteLength > 0) {
|
|
225
|
+
const buffer = Buffer.from(arrayBuffer)
|
|
226
|
+
await fs.writeFile(filePath, buffer)
|
|
227
|
+
resolve()
|
|
228
|
+
} else {
|
|
229
|
+
retryCount--
|
|
230
|
+
if (retryCount <= 0) {
|
|
231
|
+
reject()
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
setTimeout(fetchDownload, retryInterval)
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
fetchDownload()
|
|
239
|
+
})
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
async getDebugConnectionURLs(
|
|
243
|
+
sessionId: string
|
|
244
|
+
): Promise<DebugConnectionURLs> {
|
|
245
|
+
const response = await fetch(
|
|
246
|
+
`${this.baseApiURL}/v1/sessions/${sessionId}/debug`,
|
|
247
|
+
{
|
|
248
|
+
method: 'GET',
|
|
249
|
+
headers: {
|
|
250
|
+
'x-bb-api-key': this.apiKey,
|
|
251
|
+
'Content-Type': 'application/json',
|
|
252
|
+
},
|
|
253
|
+
}
|
|
254
|
+
)
|
|
255
|
+
|
|
256
|
+
const json = await response.json()
|
|
257
|
+
return json
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
load(url: string | string[], options: LoadOptions = {}) {
|
|
19
261
|
if (typeof url === 'string') {
|
|
20
|
-
return this.
|
|
262
|
+
return this.loadUrl(url, options)
|
|
21
263
|
} else if (Array.isArray(url)) {
|
|
22
|
-
return this.
|
|
264
|
+
return this.loadUrls(url, options)
|
|
23
265
|
} else {
|
|
24
266
|
throw new TypeError('Input must be a URL string or array of URLs')
|
|
25
267
|
}
|
|
26
268
|
}
|
|
27
269
|
|
|
28
|
-
async
|
|
29
|
-
url: string,
|
|
30
|
-
options: BrowserbaseLoadOptions = {}
|
|
31
|
-
): Promise<string> {
|
|
270
|
+
async loadUrl(url: string, options: LoadOptions = {}): Promise<string> {
|
|
32
271
|
if (!url) {
|
|
33
272
|
throw new Error('Page URL was not provided')
|
|
34
273
|
}
|
|
35
274
|
|
|
36
275
|
const browser = await chromium.connectOverCDP(
|
|
37
|
-
|
|
276
|
+
this.connectUrl({ sessionId: options.sessionId, proxy: options.proxy })
|
|
38
277
|
)
|
|
39
278
|
const defaultContext = browser.contexts()[0]
|
|
40
279
|
const page = defaultContext.pages()[0]
|
|
@@ -56,16 +295,16 @@ export default class Browserbase {
|
|
|
56
295
|
return html
|
|
57
296
|
}
|
|
58
297
|
|
|
59
|
-
async *
|
|
298
|
+
async *loadUrls(
|
|
60
299
|
urls: string[],
|
|
61
|
-
options:
|
|
300
|
+
options: LoadOptions = {}
|
|
62
301
|
): AsyncGenerator<string> {
|
|
63
302
|
if (!urls.length) {
|
|
64
303
|
throw new Error('Page URLs were not provided')
|
|
65
304
|
}
|
|
66
305
|
|
|
67
306
|
const browser = await chromium.connectOverCDP(
|
|
68
|
-
|
|
307
|
+
this.connectUrl({ sessionId: options.sessionId, proxy: options.proxy })
|
|
69
308
|
)
|
|
70
309
|
const defaultContext = browser.contexts()[0]
|
|
71
310
|
const page = defaultContext.pages()[0]
|
|
@@ -93,14 +332,14 @@ export default class Browserbase {
|
|
|
93
332
|
|
|
94
333
|
async screenshot(
|
|
95
334
|
url: string,
|
|
96
|
-
options:
|
|
335
|
+
options: ScreenshotOptions = { fullPage: false }
|
|
97
336
|
): Promise<Buffer> {
|
|
98
337
|
if (!url) {
|
|
99
338
|
throw new Error('Page URL was not provided')
|
|
100
339
|
}
|
|
101
340
|
|
|
102
341
|
const browser = await chromium.connectOverCDP(
|
|
103
|
-
|
|
342
|
+
this.connectUrl({ sessionId: options.sessionId, proxy: options.proxy })
|
|
104
343
|
)
|
|
105
344
|
const page = await browser.newPage()
|
|
106
345
|
await page.goto(url)
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { z } from 'zod'
|
|
2
|
-
import Browserbase, {
|
|
2
|
+
import Browserbase, { LoadOptions } from '../index.js'
|
|
3
3
|
|
|
4
4
|
export default function BrowserbaseAISDK(
|
|
5
5
|
b: Browserbase,
|
|
6
|
-
options:
|
|
6
|
+
options: LoadOptions = {}
|
|
7
7
|
) {
|
|
8
8
|
return {
|
|
9
9
|
description:
|
|
@@ -12,7 +12,7 @@ export default function BrowserbaseAISDK(
|
|
|
12
12
|
url: z.string().describe('Page URL to be loaded'),
|
|
13
13
|
}),
|
|
14
14
|
execute: async ({ url }: { url: string }) => {
|
|
15
|
-
const page = await b.
|
|
15
|
+
const page = await b.loadUrl(url, options)
|
|
16
16
|
return { page }
|
|
17
17
|
},
|
|
18
18
|
}
|
package/test/lib.test.ts
CHANGED
|
@@ -10,6 +10,33 @@ describe('Browserbase', () => {
|
|
|
10
10
|
browserbase = new Browserbase()
|
|
11
11
|
})
|
|
12
12
|
|
|
13
|
+
it('shoud create and retrieve session', async () => {
|
|
14
|
+
const { id } = await browserbase.createSession()
|
|
15
|
+
const session = await browserbase.getSession(id)
|
|
16
|
+
|
|
17
|
+
expect(session.status).to.equal('RUNNING')
|
|
18
|
+
expect(session.id).to.equal(id)
|
|
19
|
+
})
|
|
20
|
+
|
|
21
|
+
it('shoud create and update session', async () => {
|
|
22
|
+
const { id } = await browserbase.createSession()
|
|
23
|
+
const updated = await browserbase.updateSession(id, { status: 'RELEASING' })
|
|
24
|
+
|
|
25
|
+
expect(updated.status).to.equal('COMPLETED')
|
|
26
|
+
})
|
|
27
|
+
|
|
28
|
+
it('shoud create a session and retrieve a recording', async () => {
|
|
29
|
+
const { id } = await browserbase.createSession()
|
|
30
|
+
const recording = await browserbase.getSessionRecording(id)
|
|
31
|
+
|
|
32
|
+
expect(recording.length).to.equal(0)
|
|
33
|
+
})
|
|
34
|
+
|
|
35
|
+
it('shoud create a session and get debug url', async () => {
|
|
36
|
+
const { id } = await browserbase.createSession()
|
|
37
|
+
const debug = await browserbase.getDebugConnectionURLs(id)
|
|
38
|
+
})
|
|
39
|
+
|
|
13
40
|
it('should load a webpage', async () => {
|
|
14
41
|
const result = await browserbase.load('https://example.com')
|
|
15
42
|
expect(result).contain('Example Domain')
|
|
@@ -19,11 +46,12 @@ describe('Browserbase', () => {
|
|
|
19
46
|
const result = await browserbase.load('https://example.com/', {
|
|
20
47
|
textContent: true,
|
|
21
48
|
})
|
|
49
|
+
|
|
22
50
|
expect(result).contain('Example Domain')
|
|
23
51
|
})
|
|
24
52
|
|
|
25
53
|
it('should load URLs', async () => {
|
|
26
|
-
const result = await browserbase.
|
|
54
|
+
const result = await browserbase.loadUrls(['https://example.com'])
|
|
27
55
|
const page = await result.next()
|
|
28
56
|
|
|
29
57
|
// finish iterator
|