File size: 6,825 Bytes
2baba5d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
import abc
import pydantic
from typing import Literal


class EnvState(pydantic.BaseModel):
    # The screenshot in PNG format.
    screenshot: bytes
    url: str


class Computer(abc.ABC):
    """Defines an interface for environments."""

    @abc.abstractmethod
    def screen_size(self) -> tuple[int, int]:
        """Returns the screen size of the environment."""

    @abc.abstractmethod
    def open_web_browser(self) -> EnvState:
        """Opens the web browser."""

    @abc.abstractmethod
    def click_at(self, x: int, y: int) -> EnvState:
        """Clicks at a specific x, y coordinate on the screen.

        The 'x' and 'y' values are absolute values, scaled to the height and width of the screen.
        """

    @abc.abstractmethod
    def hover_at(self, x: int, y: int) -> EnvState:
        """Hovers at a specific x, y coordinate on the screen.

        May be used to explore UI elements such as tooltips or sub-menus that appear on hover.
        The 'x' and 'y' values are absolute values, scaled to the height and width of the screen.
        """

    @abc.abstractmethod
    def type_text_at(
        self,
        x: int,
        y: int,
        text: str,
        press_enter: bool,
        clear_before_typing: bool,
    ) -> EnvState:
        """Types text at a specific x, y coordinate on the screen.

        The system automatically presses ENTER after typing. To disable this, set `press_enter` to False.
        The system automatically clears any existing content before typing the specified `text`. To disable this, set `clear_before_typing` to False.
        The 'x' and 'y' values are absolute values, scaled to the height and width of the screen.
        """

    @abc.abstractmethod
    def scroll_document(
        self, direction: Literal["up", "down", "left", "right"]
    ) -> EnvState:
        """Scrolls the active window or document in the specified direction.

        Direction can be "up", "down", "left", or "right".
        """

    @abc.abstractmethod
    def scroll_at(
        self,
        x: int,
        y: int,
        direction: Literal["up", "down", "left", "right"],
        magnitude: int = 5,
    ) -> EnvState:
        """Scrolls at a specific x, y coordinate in the specified direction by magnitude.

        The 'x' and 'y' values are absolute values, scaled to the height and width of the screen.
        The magnitude parameter controls the scroll amount (default: 5).
        """


    @abc.abstractmethod
    def wait(self, seconds: int = 5) -> EnvState:
        """Waits for a specified number of seconds.

        Args:
            seconds: Number of seconds to wait (default: 5)
        """

    @abc.abstractmethod
    def go_back(self) -> EnvState:
        """Navigates back to the previous page in the browser history."""

    @abc.abstractmethod
    def go_forward(self) -> EnvState:
        """Navigates forward to the next page in the browser history."""

    @abc.abstractmethod
    def search(self) -> EnvState:
        """Directly jumps to a search engine home page.

        Used when you need to start with a search. For example, this is used when
        the current page doesn't have the information needed or because a new
        task is being started.
        """

    @abc.abstractmethod
    def navigate(self, url: str) -> EnvState:
        """Navigates directly to a specified URL."""

    @abc.abstractmethod
    def key_combination(self, keys: list[str]) -> EnvState:
        """Executes keyboard key combinations.

        Examples: ["control", "c"], ["alt", "tab"], ["enter"]
        """

    @abc.abstractmethod
    def drag_and_drop(
        self, x: int, y: int, destination_x: int, destination_y: int
    ) -> EnvState:
        """Drag and drop an element from a x, y coordinate to a destination destination_y, destination_x coordinate.
        The 'x', 'y', 'destination_y' and 'destination_x' values are absolute values, scaled to the height and width of the screen.
        """

    @abc.abstractmethod
    def current_state(self) -> EnvState:
        """Returns the current state of the environment including screenshot and URL."""

    # ========== Shell Operations ==========

    @abc.abstractmethod
    def exec_command(self, command: str, timeout: int = 30):
        """Execute a shell command in the environment.

        Args:
            command: Shell command to execute
            timeout: Command timeout in seconds (default: 30)

        Returns:
            Command result with stdout, stderr, and exit code
        """

    @abc.abstractmethod
    def exec_script(self, script: str, timeout: int = 60):
        """Execute a multi-line bash script.

        Args:
            script: Multi-line bash script to execute
            timeout: Script timeout in seconds (default: 60)

        Returns:
            Command result with stdout, stderr, and exit code
        """

    # ========== File Operations ==========

    @abc.abstractmethod
    def read_file(self, path: str) -> str:
        """Read file contents from the environment.

        Args:
            path: File path to read

        Returns:
            File contents as string
        """

    @abc.abstractmethod
    def write_file(self, path: str, content: str) -> None:
        """Write content to a file in the environment.

        Args:
            path: File path to write
            content: Content to write to the file
        """

    @abc.abstractmethod
    def list_directory(self, path: str = ".") -> list[str]:
        """List contents of a directory.

        Args:
            path: Directory path to list (default: current directory)

        Returns:
            List of file and directory names
        """

    # ========== Code Execution Operations ==========

    @abc.abstractmethod
    def run_python(self, code: str, timeout: int = 60):
        """Execute Python code in the environment.

        Args:
            code: Python code to execute
            timeout: Execution timeout in seconds (default: 60)

        Returns:
            Code execution result with stdout, stderr, and exit code
        """

    @abc.abstractmethod
    def run_javascript(self, code: str, timeout: int = 60):
        """Execute JavaScript code in the environment.

        Args:
            code: JavaScript code to execute
            timeout: Execution timeout in seconds (default: 60)

        Returns:
            Code execution result with stdout, stderr, and exit code
        """

    @abc.abstractmethod
    def run_bash(self, code: str, timeout: int = 60):
        """Execute Bash code in the environment.

        Args:
            code: Bash code to execute
            timeout: Execution timeout in seconds (default: 60)

        Returns:
            Code execution result with stdout, stderr, and exit code
        """