-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmain.py
297 lines (240 loc) · 9.69 KB
/
main.py
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
import os
import time
import base64
import requests
import json
from dotenv import load_dotenv
from playwright.sync_api import sync_playwright, Page
from steel import Steel
from const import MODIFIERS, PLAYWRIGHT_KEYS
load_dotenv()
def create_response(**kwargs):
"""Send a request to OpenAI API to get a response."""
url = "https://api.openai.com/v1/responses"
headers = {
"Authorization": f"Bearer {os.getenv('OPENAI_API_KEY')}",
"Content-Type": "application/json",
"Openai-beta": "responses=v1",
}
openai_org = os.getenv("OPENAI_ORG")
if openai_org:
headers["Openai-Organization"] = openai_org
response = requests.post(url, headers=headers, json=kwargs)
if response.status_code != 200:
print(f"Error: {response.status_code} {response.text}")
return response.json()
def pp(obj):
"""Pretty print a JSON object."""
print(json.dumps(obj, indent=4))
def sanitize_message(msg: dict) -> dict:
"""Return a copy of the message with image_url omitted for computer_call_output messages."""
if msg.get("type") == "computer_call_output":
output = msg.get("output", {})
if isinstance(output, dict):
sanitized = msg.copy()
sanitized["output"] = {**output, "image_url": "[omitted]"}
return sanitized
return msg
class SteelBrowser:
environment = "browser"
dimensions = (1024, 768)
def __init__(self):
self.client = Steel(
steel_api_key=os.getenv("STEEL_API_KEY"),
base_url=os.getenv("STEEL_API_URL"),
)
self.session = None
self._playwright = None
self._browser = None
self._page = None
def __enter__(self):
self.session = self.client.sessions.create(
use_proxy=False,
solve_captcha=False,
block_ads=True,
dimensions={"width": self.dimensions[0], "height": self.dimensions[1]},
)
print(f"Session created: {self.session.session_viewer_url}")
# Connect to the session
self._playwright = sync_playwright().start()
connect_url = os.getenv("STEEL_CONNECT_URL", "wss://connect.steel.dev")
cdp_url = f"{connect_url}?apiKey={os.getenv('STEEL_API_KEY')}&sessionId={self.session.id}"
self._browser = self._playwright.chromium.connect_over_cdp(cdp_url)
self._page = self._browser.contexts[0].pages[0]
self._page.goto("https://google.com")
self.apply_same_tab_script()
return self
def apply_same_tab_script(self):
"""Apply script to make links open in the same tab."""
self._page.add_init_script(
"""
window.addEventListener('load', () => {
// Initial cleanup
document.querySelectorAll('a[target="_blank"]').forEach(a => a.target = '_self');
// Watch for dynamic changes
const observer = new MutationObserver((mutations) => {
mutations.forEach((mutation) => {
if (mutation.addedNodes) {
mutation.addedNodes.forEach((node) => {
if (node.nodeType === 1) { // ELEMENT_NODE
// Check the added element itself
if (node.tagName === 'A' && node.target === '_blank') {
node.target = '_self';
}
// Check any anchor children
node.querySelectorAll('a[target="_blank"]').forEach(a => a.target = '_self');
}
});
}
});
});
observer.observe(document.body, {
childList: true,
subtree: true
});
});
"""
)
def __exit__(self, exc_type, exc_val, exc_tb):
if self._page:
self._page.close()
if self._browser:
self._browser.close()
if self._playwright:
self._playwright.stop()
if self.session:
self.client.sessions.release(self.session.id)
print(f"Session ended: {self.session.session_viewer_url}")
def screenshot(self) -> str:
try:
cdp_session = self._page.context.new_cdp_session(self._page)
result = cdp_session.send(
"Page.captureScreenshot", {"format": "png", "fromSurface": True}
)
return result["data"]
except:
png_bytes = self._page.screenshot()
return base64.b64encode(png_bytes).decode("utf-8")
def click(self, x: int, y: int, button: str = "left") -> None:
self._page.mouse.click(x, y, button=button)
def double_click(self, x: int, y: int) -> None:
self._page.mouse.dblclick(x, y)
def scroll(self, x: int, y: int, scroll_x: int, scroll_y: int) -> None:
self._page.mouse.move(x, y)
self._page.evaluate(f"window.scrollBy({scroll_x}, {scroll_y})")
def type(self, text: str) -> None:
self._page.keyboard.type(text)
def wait(self, ms: int = 1000) -> None:
time.sleep(ms / 1000)
def move(self, x: int, y: int) -> None:
self._page.mouse.move(x, y)
def keypress(self, keys: list[str]) -> None:
if not keys:
return
# Handle modifier keys
if keys[0].upper() in MODIFIERS:
# Press and hold the modifier key
self._page.keyboard.down(MODIFIERS[keys[0].upper()])
# Press the remaining keys
for k in keys[1:]:
key = PLAYWRIGHT_KEYS.get(k.upper(), k)
self._page.keyboard.press(key)
# Release the modifier key
self._page.keyboard.up(MODIFIERS[keys[0].upper()])
return
# Handle regular key presses
for k in keys:
key = PLAYWRIGHT_KEYS.get(k.upper(), k)
self._page.keyboard.press(key)
def drag(self, path: list[dict[str, int]]) -> None:
if not path:
return
self._page.mouse.move(path[0]["x"], path[0]["y"])
self._page.mouse.down()
for point in path[1:]:
self._page.mouse.move(point["x"], point["y"])
self._page.mouse.up()
def get_current_url(self) -> str:
return self._page.url
def goto(self, url: str) -> None:
"""Navigate to a specific URL."""
self._page.goto(url)
def back(self) -> None:
"""Navigate back in the browser history."""
self._page.go_back()
def refresh(self) -> None:
"""Refresh the current page."""
self._page.reload()
def acknowledge_safety_check_callback(message: str) -> bool:
response = input(
f"Safety Check Warning: {message}\nDo you want to acknowledge and proceed? (y/n): "
).lower()
return response.strip() == "y"
def handle_item(item, computer):
"""Handle each item; may cause a computer action + screenshot."""
if item["type"] == "message": # print messages
print(item["content"][0]["text"])
if item["type"] == "computer_call": # perform computer actions
action = item["action"]
action_type = action["type"]
action_args = {k: v for k, v in action.items() if k != "type"}
print(f"{action_type}({action_args})")
# give our computer environment action to perform
getattr(computer, action_type)(**action_args)
screenshot_base64 = computer.screenshot()
pending_checks = item.get("pending_safety_checks", [])
for check in pending_checks:
if not acknowledge_safety_check_callback(check["message"]):
raise ValueError(f"Safety check failed: {check['message']}")
# return value informs model of the latest screenshot
call_output = {
"type": "computer_call_output",
"call_id": item["call_id"],
"acknowledged_safety_checks": pending_checks,
"output": {
"type": "input_image",
"image_url": f"data:image/png;base64,{screenshot_base64}",
},
}
if computer.environment == "browser":
current_url = computer.get_current_url()
call_output["output"]["current_url"] = current_url
return [call_output]
return []
def main():
"""Run the CUA (Computer Use Assistant) loop with Steel browser."""
with SteelBrowser() as computer:
tools = [
{
"type": "computer-preview",
"display_width": computer.dimensions[0],
"display_height": computer.dimensions[1],
"environment": computer.environment,
}
]
items = []
while True:
user_input = input("> ")
items.append({"role": "user", "content": user_input})
while True:
response = create_response(
model="computer-use-preview",
input=items,
tools=tools,
truncation="auto",
)
if "output" not in response:
print(response)
raise ValueError("No output from model")
items += response["output"]
for item in response["output"]:
items += handle_item(item, computer)
if items[-1].get("role") == "assistant":
break
if __name__ == "__main__":
try:
main()
except KeyboardInterrupt:
print("\nInterrupted by user.")
except Exception as e:
print(f"Error: {e}")