@browserbasehq/sdk 1.0.0 → 1.1.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 +69 -0
- package/dist/index.d.ts +88 -7
- package/dist/index.js +103 -2
- package/dist/integrations/ai-sdk.d.ts +3 -2
- package/dist/integrations/ai-sdk.js +1 -0
- package/package.json +1 -1
- package/src/index.ts +232 -11
- package/src/integrations/ai-sdk.ts +4 -2
- package/test/lib.test.ts +30 -2
package/README.md
CHANGED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
# Browserbase JS SDK
|
|
2
|
+
|
|
3
|
+
[Browserbase](https://browserbase.com) is a serverless platform for running headless browsers, it offers advanced debugging, session recordings, stealth mode, integrated proxies and captcha solving.
|
|
4
|
+
|
|
5
|
+
## Installation and setup
|
|
6
|
+
|
|
7
|
+
- Get an API key from [browserbase.com](https://browserbase.com) and set it in environment variables (`BROWSERBASE_API_KEY`).
|
|
8
|
+
- Install the required dependencies:
|
|
9
|
+
|
|
10
|
+
```
|
|
11
|
+
npm i @browserbasehq/sdk
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
## Usage
|
|
15
|
+
|
|
16
|
+
```js
|
|
17
|
+
import { Browserbase, BrowserbaseAISDK } from '@browserbasehq/sdk'
|
|
18
|
+
|
|
19
|
+
// Init the SDK
|
|
20
|
+
const browserbase = new Browserbase()
|
|
21
|
+
|
|
22
|
+
// Load a webpage
|
|
23
|
+
const result = await browserbase.load("https://example.com")
|
|
24
|
+
|
|
25
|
+
// Load multiple webpages (returns iterator)
|
|
26
|
+
const result = await browserbase.load(["https://example.com"])
|
|
27
|
+
|
|
28
|
+
// Text-only mode
|
|
29
|
+
const result = await browserbase.load("https://example.com", { textContent: true })
|
|
30
|
+
|
|
31
|
+
// Screenshot (returns bytes)
|
|
32
|
+
const result = await browserbase.screenshot("https://example.com", { textContent: true })
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
### Vercel AI SDK Integration
|
|
36
|
+
|
|
37
|
+
Install the additional dependencies:
|
|
38
|
+
|
|
39
|
+
```
|
|
40
|
+
npm i ai openai zod
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
```js
|
|
44
|
+
import OpenAI from 'openai';
|
|
45
|
+
import { Browserbase } from '@browserbasehq/sdk'
|
|
46
|
+
import { BrowserbaseAISDK } from '@browserbasehq/sdk/dist/integrations/ai-sdk'
|
|
47
|
+
import { OpenAIStream, StreamingTextResponse, experimental_generateText } from 'ai';
|
|
48
|
+
|
|
49
|
+
// Create new OpenAI client
|
|
50
|
+
const openai = new OpenAI({
|
|
51
|
+
apiKey: process.env.OPENAI_API_KEY,
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
// Init the Browserbase SDK
|
|
55
|
+
const browserbase = new Browserbase();
|
|
56
|
+
|
|
57
|
+
// Init the tool
|
|
58
|
+
const browserTool = BrowserbaseAISDK(browserbase, { textContent: true });
|
|
59
|
+
|
|
60
|
+
// Load completions
|
|
61
|
+
const result = await experimental_generateText({
|
|
62
|
+
model: openai.chat('gpt-3.5-turbo'),
|
|
63
|
+
tools: {
|
|
64
|
+
browserTool
|
|
65
|
+
},
|
|
66
|
+
prompt:
|
|
67
|
+
'What is the weather in San Francisco?',
|
|
68
|
+
});
|
|
69
|
+
```
|
package/dist/index.d.ts
CHANGED
|
@@ -1,15 +1,96 @@
|
|
|
1
1
|
/// <reference types="node" resolution-mode="require"/>
|
|
2
|
-
export type
|
|
2
|
+
export type ClientOptions = {
|
|
3
|
+
apiKey?: string;
|
|
4
|
+
projectId?: string;
|
|
5
|
+
baseURL?: string;
|
|
6
|
+
};
|
|
7
|
+
export type LoadOptions = {
|
|
8
|
+
sessionId?: string;
|
|
3
9
|
textContent?: boolean;
|
|
4
10
|
};
|
|
5
|
-
export type
|
|
11
|
+
export type ScreenshotOptions = {
|
|
6
12
|
fullPage?: boolean;
|
|
7
13
|
};
|
|
14
|
+
export type CreateSessionOptions = {
|
|
15
|
+
projectId?: string;
|
|
16
|
+
extensionId?: string;
|
|
17
|
+
fingerprint?: {
|
|
18
|
+
browserListQuery?: string;
|
|
19
|
+
httpVersion?: 1 | 2;
|
|
20
|
+
browsers?: Array<'chrome' | 'firefox' | 'edge' | 'safari'>;
|
|
21
|
+
devices?: Array<'desktop' | 'mobile'>;
|
|
22
|
+
locales?: string[];
|
|
23
|
+
operatingSystems?: Array<'windows' | 'macos' | 'linux' | 'ios' | 'android'>;
|
|
24
|
+
screen?: {
|
|
25
|
+
maxHeight?: number;
|
|
26
|
+
maxWidth?: number;
|
|
27
|
+
minHeight?: number;
|
|
28
|
+
minWidth?: number;
|
|
29
|
+
};
|
|
30
|
+
};
|
|
31
|
+
};
|
|
32
|
+
export type Session = {
|
|
33
|
+
id: string;
|
|
34
|
+
createdAt: string;
|
|
35
|
+
startedAt: string;
|
|
36
|
+
endedAt: string;
|
|
37
|
+
projectId: string;
|
|
38
|
+
status: 'NEW' | 'CREATED' | 'ERROR' | 'RUNNING' | 'REQUEST_RELEASE' | 'RELEASING';
|
|
39
|
+
taskId: string;
|
|
40
|
+
proxyBytes: number;
|
|
41
|
+
expiresAt: string;
|
|
42
|
+
avg_cpu_usage: number;
|
|
43
|
+
memory_usage: number;
|
|
44
|
+
details: string;
|
|
45
|
+
logs: string;
|
|
46
|
+
};
|
|
47
|
+
export type UpdateSessionOptions = {
|
|
48
|
+
projectId?: string;
|
|
49
|
+
status: Session['status'];
|
|
50
|
+
};
|
|
51
|
+
export type SessionRecording = {
|
|
52
|
+
type: number;
|
|
53
|
+
timestamp: number;
|
|
54
|
+
data: object;
|
|
55
|
+
};
|
|
56
|
+
export type SessionLog = {
|
|
57
|
+
sessionId: string;
|
|
58
|
+
id: string;
|
|
59
|
+
timestamp: string;
|
|
60
|
+
method: string;
|
|
61
|
+
request: {
|
|
62
|
+
timestamp: string;
|
|
63
|
+
params: object;
|
|
64
|
+
rawBody: string;
|
|
65
|
+
};
|
|
66
|
+
response: {
|
|
67
|
+
timestamp: string;
|
|
68
|
+
result: object;
|
|
69
|
+
rawBody: string;
|
|
70
|
+
};
|
|
71
|
+
pageId: string;
|
|
72
|
+
};
|
|
73
|
+
export type DebugConnectionURLs = {
|
|
74
|
+
debuggerFullscreenUrl: string;
|
|
75
|
+
debuggerUrl: string;
|
|
76
|
+
wsUrl: string;
|
|
77
|
+
};
|
|
8
78
|
export default class Browserbase {
|
|
9
79
|
private apiKey;
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
80
|
+
private projectId;
|
|
81
|
+
private baseURL;
|
|
82
|
+
constructor(options?: ClientOptions);
|
|
83
|
+
listSessions(): Promise<Session[]>;
|
|
84
|
+
createSession(options?: CreateSessionOptions): Promise<Session>;
|
|
85
|
+
getSession(sessionId: string): Promise<Session>;
|
|
86
|
+
updateSession(sessionId: string, options: UpdateSessionOptions): Promise<Session>;
|
|
87
|
+
getSessionRecording(sessionId: string): Promise<SessionRecording[]>;
|
|
88
|
+
getSessionLogs(sessionId: string): Promise<SessionLog[]>;
|
|
89
|
+
getSessionDownloads(sessionId: string, filePath: string, retryInterval?: number, retryCount?: number): Promise<void>;
|
|
90
|
+
getDebugConnectionURLs(sessionId: string): Promise<DebugConnectionURLs>;
|
|
91
|
+
load(url: string | string[], options?: LoadOptions): Promise<string> | AsyncGenerator<string, any, unknown>;
|
|
92
|
+
loadURL(url: string, options?: LoadOptions): Promise<string>;
|
|
93
|
+
loadURLs(urls: string[], options?: LoadOptions): AsyncGenerator<string>;
|
|
94
|
+
screenshot(url: string, options?: ScreenshotOptions): Promise<Buffer>;
|
|
15
95
|
}
|
|
96
|
+
export { Browserbase };
|
package/dist/index.js
CHANGED
|
@@ -1,8 +1,108 @@
|
|
|
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
|
+
baseURL;
|
|
7
|
+
constructor(options = {}) {
|
|
8
|
+
this.apiKey = options.apiKey || process.env.BROWSERBASE_API_KEY;
|
|
9
|
+
this.projectId = options.projectId || process.env.BROWSERBASE_PROJECT_ID;
|
|
10
|
+
this.baseURL = options.baseURL || 'https://www.browserbase.com';
|
|
11
|
+
}
|
|
12
|
+
async listSessions() {
|
|
13
|
+
const response = await fetch(`${this.baseURL}/v1/sessions`, {
|
|
14
|
+
headers: {
|
|
15
|
+
'x-bb-api-key': this.apiKey,
|
|
16
|
+
'Content-Type': 'application/json',
|
|
17
|
+
},
|
|
18
|
+
});
|
|
19
|
+
return await response.json();
|
|
20
|
+
}
|
|
21
|
+
async createSession(options) {
|
|
22
|
+
const response = await fetch(`${this.baseURL}/v1/sessions`, {
|
|
23
|
+
method: 'POST',
|
|
24
|
+
headers: {
|
|
25
|
+
'x-bb-api-key': this.apiKey,
|
|
26
|
+
'Content-Type': 'application/json',
|
|
27
|
+
},
|
|
28
|
+
body: JSON.stringify({ projectId: this.projectId, ...options }),
|
|
29
|
+
});
|
|
30
|
+
return await response.json();
|
|
31
|
+
}
|
|
32
|
+
async getSession(sessionId) {
|
|
33
|
+
const response = await fetch(`${this.baseURL}/v1/sessions/${sessionId}`, {
|
|
34
|
+
headers: {
|
|
35
|
+
'x-bb-api-key': this.apiKey,
|
|
36
|
+
'Content-Type': 'application/json',
|
|
37
|
+
},
|
|
38
|
+
});
|
|
39
|
+
return await response.json();
|
|
40
|
+
}
|
|
41
|
+
async updateSession(sessionId, options) {
|
|
42
|
+
const response = await fetch(`${this.baseURL}/v1/sessions/${sessionId}`, {
|
|
43
|
+
method: 'POST',
|
|
44
|
+
headers: {
|
|
45
|
+
'x-bb-api-key': this.apiKey,
|
|
46
|
+
'Content-Type': 'application/json',
|
|
47
|
+
},
|
|
48
|
+
body: JSON.stringify({ projectId: this.projectId, ...options }),
|
|
49
|
+
});
|
|
50
|
+
return await response.json();
|
|
51
|
+
}
|
|
52
|
+
async getSessionRecording(sessionId) {
|
|
53
|
+
const response = await fetch(`${this.baseURL}/v1/sessions/${sessionId}/recording`, {
|
|
54
|
+
headers: {
|
|
55
|
+
'x-bb-api-key': this.apiKey,
|
|
56
|
+
'Content-Type': 'application/json',
|
|
57
|
+
},
|
|
58
|
+
});
|
|
59
|
+
return await response.json();
|
|
60
|
+
}
|
|
61
|
+
async getSessionLogs(sessionId) {
|
|
62
|
+
const response = await fetch(`${this.baseURL}/v1/sessions/${sessionId}/logs`, {
|
|
63
|
+
headers: {
|
|
64
|
+
'x-bb-api-key': this.apiKey,
|
|
65
|
+
'Content-Type': 'application/json',
|
|
66
|
+
},
|
|
67
|
+
});
|
|
68
|
+
return await response.json();
|
|
69
|
+
}
|
|
70
|
+
async getSessionDownloads(sessionId, filePath, retryInterval = 2000, retryCount = 2) {
|
|
71
|
+
return new Promise((resolve, reject) => {
|
|
72
|
+
const fetchDownload = async () => {
|
|
73
|
+
const response = await fetch(`${this.baseURL}/v1/sessions/${sessionId}/downloads`, {
|
|
74
|
+
method: 'GET',
|
|
75
|
+
headers: {
|
|
76
|
+
'x-bb-api-key': this.apiKey,
|
|
77
|
+
},
|
|
78
|
+
});
|
|
79
|
+
const arrayBuffer = await response.arrayBuffer();
|
|
80
|
+
if (arrayBuffer.byteLength > 0) {
|
|
81
|
+
const buffer = Buffer.from(arrayBuffer);
|
|
82
|
+
await fs.writeFile(filePath, buffer);
|
|
83
|
+
resolve();
|
|
84
|
+
}
|
|
85
|
+
else {
|
|
86
|
+
retryCount--;
|
|
87
|
+
if (retryCount <= 0) {
|
|
88
|
+
reject();
|
|
89
|
+
}
|
|
90
|
+
setTimeout(fetchDownload, retryInterval);
|
|
91
|
+
}
|
|
92
|
+
};
|
|
93
|
+
fetchDownload();
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
async getDebugConnectionURLs(sessionId) {
|
|
97
|
+
const response = await fetch(`${this.baseURL}/v1/sessions/${sessionId}/debug`, {
|
|
98
|
+
method: 'GET',
|
|
99
|
+
headers: {
|
|
100
|
+
'x-bb-api-key': this.apiKey,
|
|
101
|
+
'Content-Type': 'application/json',
|
|
102
|
+
},
|
|
103
|
+
});
|
|
104
|
+
const json = await response.json();
|
|
105
|
+
return json;
|
|
6
106
|
}
|
|
7
107
|
load(url, options = {}) {
|
|
8
108
|
if (typeof url === 'string') {
|
|
@@ -71,3 +171,4 @@ export default class Browserbase {
|
|
|
71
171
|
return screenshot;
|
|
72
172
|
}
|
|
73
173
|
}
|
|
174
|
+
export { Browserbase };
|
|
@@ -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;
|
|
@@ -15,3 +15,4 @@ export default function BrowserbaseAISDK(b: Browserbase, options?: BrowserbaseLo
|
|
|
15
15
|
page: string;
|
|
16
16
|
}>;
|
|
17
17
|
};
|
|
18
|
+
export { BrowserbaseAISDK };
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -1,21 +1,243 @@
|
|
|
1
1
|
import { chromium } from 'playwright'
|
|
2
|
+
import fs from 'fs/promises'
|
|
2
3
|
|
|
3
|
-
export type
|
|
4
|
+
export type ClientOptions = {
|
|
5
|
+
apiKey?: string
|
|
6
|
+
projectId?: string
|
|
7
|
+
baseURL?: string
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export type LoadOptions = {
|
|
11
|
+
sessionId?: string
|
|
4
12
|
textContent?: boolean
|
|
5
13
|
}
|
|
6
14
|
|
|
7
|
-
export type
|
|
15
|
+
export type ScreenshotOptions = {
|
|
8
16
|
fullPage?: boolean
|
|
9
17
|
}
|
|
10
18
|
|
|
19
|
+
export type CreateSessionOptions = {
|
|
20
|
+
projectId?: string
|
|
21
|
+
extensionId?: string
|
|
22
|
+
fingerprint?: {
|
|
23
|
+
browserListQuery?: string
|
|
24
|
+
httpVersion?: 1 | 2
|
|
25
|
+
browsers?: Array<'chrome' | 'firefox' | 'edge' | 'safari'>
|
|
26
|
+
devices?: Array<'desktop' | 'mobile'>
|
|
27
|
+
locales?: string[]
|
|
28
|
+
operatingSystems?: Array<'windows' | 'macos' | 'linux' | 'ios' | 'android'>
|
|
29
|
+
screen?: {
|
|
30
|
+
maxHeight?: number
|
|
31
|
+
maxWidth?: number
|
|
32
|
+
minHeight?: number
|
|
33
|
+
minWidth?: number
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export type Session = {
|
|
39
|
+
id: string
|
|
40
|
+
createdAt: string
|
|
41
|
+
startedAt: string
|
|
42
|
+
endedAt: string
|
|
43
|
+
projectId: string
|
|
44
|
+
status:
|
|
45
|
+
| 'NEW'
|
|
46
|
+
| 'CREATED'
|
|
47
|
+
| 'ERROR'
|
|
48
|
+
| 'RUNNING'
|
|
49
|
+
| 'REQUEST_RELEASE'
|
|
50
|
+
| 'RELEASING'
|
|
51
|
+
taskId: string
|
|
52
|
+
proxyBytes: number
|
|
53
|
+
expiresAt: string
|
|
54
|
+
avg_cpu_usage: number
|
|
55
|
+
memory_usage: number
|
|
56
|
+
details: string
|
|
57
|
+
logs: string
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export type UpdateSessionOptions = {
|
|
61
|
+
projectId?: string
|
|
62
|
+
status: Session['status']
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export type SessionRecording = {
|
|
66
|
+
type: number
|
|
67
|
+
timestamp: number
|
|
68
|
+
data: object
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export type SessionLog = {
|
|
72
|
+
sessionId: string
|
|
73
|
+
id: string
|
|
74
|
+
timestamp: string
|
|
75
|
+
method: string
|
|
76
|
+
request: {
|
|
77
|
+
timestamp: string
|
|
78
|
+
params: object
|
|
79
|
+
rawBody: string
|
|
80
|
+
}
|
|
81
|
+
response: {
|
|
82
|
+
timestamp: string
|
|
83
|
+
result: object
|
|
84
|
+
rawBody: string
|
|
85
|
+
}
|
|
86
|
+
pageId: string
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export type DebugConnectionURLs = {
|
|
90
|
+
debuggerFullscreenUrl: string
|
|
91
|
+
debuggerUrl: string
|
|
92
|
+
wsUrl: string
|
|
93
|
+
}
|
|
94
|
+
|
|
11
95
|
export default class Browserbase {
|
|
12
96
|
private apiKey: string
|
|
97
|
+
private projectId: string
|
|
98
|
+
private baseURL: string
|
|
99
|
+
|
|
100
|
+
constructor(options: ClientOptions = {}) {
|
|
101
|
+
this.apiKey = options.apiKey || process.env.BROWSERBASE_API_KEY!
|
|
102
|
+
this.projectId = options.projectId || process.env.BROWSERBASE_PROJECT_ID!
|
|
103
|
+
this.baseURL = options.baseURL || 'https://www.browserbase.com'
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
async listSessions(): Promise<Session[]> {
|
|
107
|
+
const response = await fetch(`${this.baseURL}/v1/sessions`, {
|
|
108
|
+
headers: {
|
|
109
|
+
'x-bb-api-key': this.apiKey,
|
|
110
|
+
'Content-Type': 'application/json',
|
|
111
|
+
},
|
|
112
|
+
})
|
|
113
|
+
|
|
114
|
+
return await response.json()
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
async createSession(options?: CreateSessionOptions): Promise<Session> {
|
|
118
|
+
const response = await fetch(`${this.baseURL}/v1/sessions`, {
|
|
119
|
+
method: 'POST',
|
|
120
|
+
headers: {
|
|
121
|
+
'x-bb-api-key': this.apiKey,
|
|
122
|
+
'Content-Type': 'application/json',
|
|
123
|
+
},
|
|
124
|
+
body: JSON.stringify({ projectId: this.projectId, ...options }),
|
|
125
|
+
})
|
|
126
|
+
|
|
127
|
+
return await response.json()
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
async getSession(sessionId: string): Promise<Session> {
|
|
131
|
+
const response = await fetch(`${this.baseURL}/v1/sessions/${sessionId}`, {
|
|
132
|
+
headers: {
|
|
133
|
+
'x-bb-api-key': this.apiKey,
|
|
134
|
+
'Content-Type': 'application/json',
|
|
135
|
+
},
|
|
136
|
+
})
|
|
13
137
|
|
|
14
|
-
|
|
15
|
-
this.apiKey = apiKey || process.env.BROWSERBASE_API_KEY!
|
|
138
|
+
return await response.json()
|
|
16
139
|
}
|
|
17
140
|
|
|
18
|
-
|
|
141
|
+
async updateSession(
|
|
142
|
+
sessionId: string,
|
|
143
|
+
options: UpdateSessionOptions
|
|
144
|
+
): Promise<Session> {
|
|
145
|
+
const response = await fetch(`${this.baseURL}/v1/sessions/${sessionId}`, {
|
|
146
|
+
method: 'POST',
|
|
147
|
+
headers: {
|
|
148
|
+
'x-bb-api-key': this.apiKey,
|
|
149
|
+
'Content-Type': 'application/json',
|
|
150
|
+
},
|
|
151
|
+
body: JSON.stringify({ projectId: this.projectId, ...options }),
|
|
152
|
+
})
|
|
153
|
+
|
|
154
|
+
return await response.json()
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
async getSessionRecording(sessionId: string): Promise<SessionRecording[]> {
|
|
158
|
+
const response = await fetch(
|
|
159
|
+
`${this.baseURL}/v1/sessions/${sessionId}/recording`,
|
|
160
|
+
{
|
|
161
|
+
headers: {
|
|
162
|
+
'x-bb-api-key': this.apiKey,
|
|
163
|
+
'Content-Type': 'application/json',
|
|
164
|
+
},
|
|
165
|
+
}
|
|
166
|
+
)
|
|
167
|
+
|
|
168
|
+
return await response.json()
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
async getSessionLogs(sessionId: string): Promise<SessionLog[]> {
|
|
172
|
+
const response = await fetch(
|
|
173
|
+
`${this.baseURL}/v1/sessions/${sessionId}/logs`,
|
|
174
|
+
{
|
|
175
|
+
headers: {
|
|
176
|
+
'x-bb-api-key': this.apiKey,
|
|
177
|
+
'Content-Type': 'application/json',
|
|
178
|
+
},
|
|
179
|
+
}
|
|
180
|
+
)
|
|
181
|
+
|
|
182
|
+
return await response.json()
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
async getSessionDownloads(
|
|
186
|
+
sessionId: string,
|
|
187
|
+
filePath: string,
|
|
188
|
+
retryInterval: number = 2000,
|
|
189
|
+
retryCount: number = 2
|
|
190
|
+
) {
|
|
191
|
+
return new Promise<void>((resolve, reject) => {
|
|
192
|
+
const fetchDownload = async () => {
|
|
193
|
+
const response = await fetch(
|
|
194
|
+
`${this.baseURL}/v1/sessions/${sessionId}/downloads`,
|
|
195
|
+
{
|
|
196
|
+
method: 'GET',
|
|
197
|
+
headers: {
|
|
198
|
+
'x-bb-api-key': this.apiKey,
|
|
199
|
+
},
|
|
200
|
+
}
|
|
201
|
+
)
|
|
202
|
+
|
|
203
|
+
const arrayBuffer = await response.arrayBuffer()
|
|
204
|
+
if (arrayBuffer.byteLength > 0) {
|
|
205
|
+
const buffer = Buffer.from(arrayBuffer)
|
|
206
|
+
await fs.writeFile(filePath, buffer)
|
|
207
|
+
resolve()
|
|
208
|
+
} else {
|
|
209
|
+
retryCount--
|
|
210
|
+
if (retryCount <= 0) {
|
|
211
|
+
reject()
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
setTimeout(fetchDownload, retryInterval)
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
fetchDownload()
|
|
219
|
+
})
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
async getDebugConnectionURLs(
|
|
223
|
+
sessionId: string
|
|
224
|
+
): Promise<DebugConnectionURLs> {
|
|
225
|
+
const response = await fetch(
|
|
226
|
+
`${this.baseURL}/v1/sessions/${sessionId}/debug`,
|
|
227
|
+
{
|
|
228
|
+
method: 'GET',
|
|
229
|
+
headers: {
|
|
230
|
+
'x-bb-api-key': this.apiKey,
|
|
231
|
+
'Content-Type': 'application/json',
|
|
232
|
+
},
|
|
233
|
+
}
|
|
234
|
+
)
|
|
235
|
+
|
|
236
|
+
const json = await response.json()
|
|
237
|
+
return json
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
load(url: string | string[], options: LoadOptions = {}) {
|
|
19
241
|
if (typeof url === 'string') {
|
|
20
242
|
return this.loadURL(url, options)
|
|
21
243
|
} else if (Array.isArray(url)) {
|
|
@@ -25,10 +247,7 @@ export default class Browserbase {
|
|
|
25
247
|
}
|
|
26
248
|
}
|
|
27
249
|
|
|
28
|
-
async loadURL(
|
|
29
|
-
url: string,
|
|
30
|
-
options: BrowserbaseLoadOptions = {}
|
|
31
|
-
): Promise<string> {
|
|
250
|
+
async loadURL(url: string, options: LoadOptions = {}): Promise<string> {
|
|
32
251
|
if (!url) {
|
|
33
252
|
throw new Error('Page URL was not provided')
|
|
34
253
|
}
|
|
@@ -58,7 +277,7 @@ export default class Browserbase {
|
|
|
58
277
|
|
|
59
278
|
async *loadURLs(
|
|
60
279
|
urls: string[],
|
|
61
|
-
options:
|
|
280
|
+
options: LoadOptions = {}
|
|
62
281
|
): AsyncGenerator<string> {
|
|
63
282
|
if (!urls.length) {
|
|
64
283
|
throw new Error('Page URLs were not provided')
|
|
@@ -93,7 +312,7 @@ export default class Browserbase {
|
|
|
93
312
|
|
|
94
313
|
async screenshot(
|
|
95
314
|
url: string,
|
|
96
|
-
options:
|
|
315
|
+
options: ScreenshotOptions = { fullPage: false }
|
|
97
316
|
): Promise<Buffer> {
|
|
98
317
|
if (!url) {
|
|
99
318
|
throw new Error('Page URL was not provided')
|
|
@@ -109,3 +328,5 @@ export default class Browserbase {
|
|
|
109
328
|
return screenshot
|
|
110
329
|
}
|
|
111
330
|
}
|
|
331
|
+
|
|
332
|
+
export { Browserbase }
|
|
@@ -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:
|
|
@@ -17,3 +17,5 @@ export default function BrowserbaseAISDK(
|
|
|
17
17
|
},
|
|
18
18
|
}
|
|
19
19
|
}
|
|
20
|
+
|
|
21
|
+
export { BrowserbaseAISDK }
|
package/test/lib.test.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { describe, it, beforeEach } from 'node:test'
|
|
2
2
|
import { expect } from 'chai'
|
|
3
|
-
import Browserbase from '../src'
|
|
4
|
-
import BrowserbaseAISDK from '../src/integrations/ai-sdk'
|
|
3
|
+
import { Browserbase } from '../src'
|
|
4
|
+
import { BrowserbaseAISDK } from '../src/integrations/ai-sdk'
|
|
5
5
|
|
|
6
6
|
describe('Browserbase', () => {
|
|
7
7
|
let browserbase: Browserbase
|
|
@@ -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,6 +46,7 @@ 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
|
|