Skip to content

Commit 119777b

Browse files
committed
browserbase init
1 parent cb64910 commit 119777b

File tree

7 files changed

+521
-4
lines changed

7 files changed

+521
-4
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import browserbase from "../../browserbase.app.mjs";
2+
import { axios } from "@pipedream/platform";
3+
4+
export default {
5+
key: "browserbase-create-context",
6+
name: "Create Context",
7+
description: "Creates a new context in Browserbase. [See the documentation](https://docs.browserbase.com/reference/api/create-a-context)",
8+
version: "0.0.{{ts}}",
9+
type: "action",
10+
props: {
11+
browserbase,
12+
projectId: {
13+
propDefinition: [
14+
browserbase,
15+
"projectId",
16+
],
17+
},
18+
},
19+
async run({ $ }) {
20+
const response = await this.browserbase.createContext({
21+
projectId: this.projectId,
22+
});
23+
24+
$.export("$summary", `Successfully created context with ID: ${response.id}`);
25+
26+
return response;
27+
},
28+
};
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import browserbase from "../../browserbase.app.mjs";
2+
import { axios } from "@pipedream/platform";
3+
4+
export default {
5+
key: "browserbase-create-session",
6+
name: "Create Session",
7+
description: "Creates a new browser session with specified settings. [See the documentation](https://docs.browserbase.com/reference/api/create-a-session)",
8+
version: "0.0.{{ts}}",
9+
type: "action",
10+
props: {
11+
browserbase,
12+
projectId: {
13+
propDefinition: [
14+
browserbase,
15+
"projectId",
16+
],
17+
},
18+
extensionId: {
19+
propDefinition: [
20+
browserbase,
21+
"extensionId",
22+
],
23+
optional: true,
24+
},
25+
browserSettings: {
26+
propDefinition: [
27+
browserbase,
28+
"browserSettings",
29+
],
30+
optional: true,
31+
},
32+
timeout: {
33+
propDefinition: [
34+
browserbase,
35+
"timeout",
36+
],
37+
optional: true,
38+
},
39+
keepAlive: {
40+
propDefinition: [
41+
browserbase,
42+
"keepAlive",
43+
],
44+
optional: true,
45+
},
46+
proxies: {
47+
propDefinition: [
48+
browserbase,
49+
"proxies",
50+
],
51+
optional: true,
52+
},
53+
region: {
54+
propDefinition: [
55+
browserbase,
56+
"region",
57+
],
58+
optional: true,
59+
},
60+
userMetadata: {
61+
propDefinition: [
62+
browserbase,
63+
"userMetadata",
64+
],
65+
optional: true,
66+
},
67+
},
68+
async run({ $ }) {
69+
const response = await this.browserbase.createSession({
70+
projectId: this.projectId,
71+
extensionId: this.extensionId,
72+
browserSettings: this.browserSettings,
73+
timeout: this.timeout,
74+
keepAlive: this.keepAlive,
75+
proxies: this.proxies && this.proxies.map(JSON.parse),
76+
region: this.region,
77+
userMetadata: this.userMetadata,
78+
});
79+
80+
$.export("$summary", `Session created successfully with ID: ${response.id}`);
81+
return response;
82+
},
83+
};
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import browserbase from "../../browserbase.app.mjs";
2+
import { axios } from "@pipedream/platform";
3+
4+
export default {
5+
key: "browserbase-list-projects",
6+
name: "List Projects",
7+
description: "Lists all projects. [See the documentation](https://docs.browserbase.com/reference/api/list-projects)",
8+
version: "0.0.{{ts}}",
9+
type: "action",
10+
props: {
11+
browserbase,
12+
},
13+
async run({ $ }) {
14+
const projects = await this.browserbase.listProjects();
15+
$.export("$summary", `Successfully listed ${projects.length} projects.`);
16+
return projects;
17+
},
18+
};
Lines changed: 199 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,206 @@
1+
import { axios } from "@pipedream/platform";
2+
13
export default {
24
type: "app",
35
app: "browserbase",
4-
propDefinitions: {},
6+
propDefinitions: {
7+
sessionId: {
8+
type: "string",
9+
label: "Session ID",
10+
description: "The ID of the session",
11+
async options() {
12+
const sessions = await this.listSessions();
13+
return sessions.map((session) => ({
14+
value: session.id,
15+
label: session.id,
16+
}));
17+
},
18+
},
19+
projectId: {
20+
type: "string",
21+
label: "Project ID",
22+
description: "The ID of the project",
23+
async options() {
24+
const projects = await this.listProjects();
25+
return projects.map((project) => ({
26+
value: project.id,
27+
label: project.name,
28+
}));
29+
},
30+
},
31+
extensionId: {
32+
type: "string",
33+
label: "Extension ID",
34+
description: "The uploaded Extension ID",
35+
optional: true,
36+
},
37+
browserSettings: {
38+
type: "object",
39+
label: "Browser Settings",
40+
description: "The settings for the browser",
41+
optional: true,
42+
},
43+
timeout: {
44+
type: "integer",
45+
label: "Timeout",
46+
description: "Duration in seconds after which the session will automatically end.",
47+
optional: true,
48+
min: 60,
49+
max: 21600,
50+
},
51+
keepAlive: {
52+
type: "boolean",
53+
label: "Keep Alive",
54+
description: "Set to true to keep the session alive even after disconnections.",
55+
optional: true,
56+
},
57+
proxies: {
58+
type: "string[]",
59+
label: "Proxies",
60+
description: "Proxy configuration. Can be true for default proxy, or an array of proxy configurations.",
61+
optional: true,
62+
},
63+
region: {
64+
type: "string",
65+
label: "Region",
66+
description: "The region where the session should run.",
67+
options: [
68+
{
69+
label: "US West 2",
70+
value: "us-west-2",
71+
},
72+
{
73+
label: "US East 1",
74+
value: "us-east-1",
75+
},
76+
{
77+
label: "EU Central 1",
78+
value: "eu-central-1",
79+
},
80+
{
81+
label: "AP Southeast 1",
82+
value: "ap-southeast-1",
83+
},
84+
],
85+
optional: true,
86+
},
87+
userMetadata: {
88+
type: "object",
89+
label: "User Metadata",
90+
description: "Arbitrary user metadata to attach to the session.",
91+
optional: true,
92+
},
93+
},
594
methods: {
6-
// this.$auth contains connected account data
7-
authKeys() {
8-
console.log(Object.keys(this.$auth));
95+
_baseUrl() {
96+
return "https://api.browserbase.com/v1";
97+
},
98+
async _makeRequest(opts = {}) {
99+
const {
100+
$ = this, method = "GET", path = "/", headers, ...otherOpts
101+
} = opts;
102+
return axios($, {
103+
...otherOpts,
104+
method,
105+
url: this._baseUrl() + path,
106+
headers: {
107+
...headers,
108+
"X-BB-API-Key": this.$auth.api_key,
109+
},
110+
});
9111
},
112+
async listProjects(opts = {}) {
113+
return this._makeRequest({
114+
path: "/projects",
115+
...opts,
116+
});
117+
},
118+
async listSessions(opts = {}) {
119+
return this._makeRequest({
120+
path: "/sessions",
121+
...opts,
122+
});
123+
},
124+
async createSession(opts = {}) {
125+
const {
126+
projectId, extensionId, browserSettings, timeout, keepAlive, proxies, region, userMetadata, ...otherOpts
127+
} = opts;
128+
const data = {
129+
projectId,
130+
extensionId,
131+
browserSettings,
132+
timeout,
133+
keepAlive,
134+
proxies,
135+
region,
136+
userMetadata,
137+
};
138+
return this._makeRequest({
139+
method: "POST",
140+
path: "/sessions",
141+
data,
142+
...otherOpts,
143+
});
144+
},
145+
async createContext(opts = {}) {
146+
const {
147+
projectId, ...otherOpts
148+
} = opts;
149+
return this._makeRequest({
150+
method: "POST",
151+
path: "/contexts",
152+
data: {
153+
projectId,
154+
},
155+
...otherOpts,
156+
});
157+
},
158+
async getSessionLogs(opts = {}) {
159+
const {
160+
sessionId, ...otherOpts
161+
} = opts;
162+
return this._makeRequest({
163+
path: `/sessions/${sessionId}/logs`,
164+
...otherOpts,
165+
});
166+
},
167+
},
168+
hooks: {
169+
async activate() {
170+
this._checkForNewProjects();
171+
this._checkForNewSessions();
172+
},
173+
},
174+
async _checkForNewProjects() {
175+
const projects = await this.listProjects();
176+
for (const project of projects) {
177+
this.$emit(project, {
178+
id: project.id,
179+
summary: project.name,
180+
ts: new Date().getTime(),
181+
});
182+
}
183+
},
184+
async _checkForNewSessions() {
185+
const sessions = await this.listSessions();
186+
for (const session of sessions) {
187+
this.$emit(session, {
188+
id: session.id,
189+
summary: `New session: ${session.id}`,
190+
ts: new Date().getTime(),
191+
});
192+
}
193+
},
194+
async _checkForNewLogs(sessionId) {
195+
const logs = await this.getSessionLogs({
196+
sessionId,
197+
});
198+
for (const log of logs) {
199+
this.$emit(log, {
200+
id: log.id,
201+
summary: `Log for session: ${sessionId}`,
202+
ts: new Date().getTime(),
203+
});
204+
}
10205
},
11206
};

0 commit comments

Comments
 (0)