File size: 2,537 Bytes
4d2fcd2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# Copyright 2025 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import os
import termcolor
from ..playwright.playwright import PlaywrightComputer
import browserbase
from playwright.sync_api import sync_playwright


class BrowserbaseComputer(PlaywrightComputer):
    def __init__(
        self,
        screen_size: tuple[int, int],
        initial_url: str = "https://www.google.com",
    ):
        super().__init__(screen_size, initial_url)

    def __enter__(self):
        print("Creating session...")

        self._playwright = sync_playwright().start()
        self._browserbase = browserbase.Browserbase(
            api_key=os.environ["BROWSERBASE_API_KEY"]
        )

        self._session = self._browserbase.sessions.create(
            project_id=os.environ["BROWSERBASE_PROJECT_ID"],
            browser_settings={
                "fingerprint": {
                    "screen": {
                        "maxWidth": 1920,
                        "maxHeight": 1080,
                        "minWidth": 1024,
                        "minHeight": 768,
                    },
                },
                "viewport": {
                    "width": self._screen_size[0],
                    "height": self._screen_size[1],
                },
            },
        )

        self._browser = self._playwright.chromium.connect_over_cdp(
            self._session.connect_url
        )
        self._context = self._browser.contexts[0]
        self._page = self._context.pages[0]
        self._page.goto(self._initial_url)

        self._context.on("page", self._handle_new_page)

        termcolor.cprint(
            f"Session started at https://browserbase.com/sessions/{self._session.id}",
            color="green",
            attrs=["bold"],
        )
        return self

    def __exit__(self, exc_type, exc_val, exc_tb):
        self._page.close()

        if self._context:
            self._context.close()

        if self._browser:
            self._browser.close()

        self._playwright.stop()