-
Notifications
You must be signed in to change notification settings - Fork 31
/
commands.py
143 lines (101 loc) · 3.66 KB
/
commands.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
import subprocess
import re
from langchain.agents import tool
@tool
def computer_applescript_action(apple_script):
"""
Use this when you want to execute a command on the computer. The command should be in AppleScript.
Always start with starting the app and activating it.
If it's a calculation, use the calculator app.
Use delay 0.5 between keystrokes.
When possible click buttons instead of typing.
Here are some examples of good AppleScript commands:
Command: Create a new page in Notion
AppleScript: tell application "Notion"
activate
delay 0.5
tell application "System Events" to keystroke "n" using {{command down}}
end tell
Command: Search for a table nearby
AppleScript: tell application "Google Chrome"
activate
delay 0.5
open location "https://www.google.com/search?q=Table+nearby"
end tell
The AppleScript should be valid including quotations.
Write the AppleScript for the Command:
Command:
"""
print("Running\n", apple_script)
return run_applescript(apple_script)
@tool
def chrome_get_the_links_on_the_page(input):
"""
Use this when you want to get the links on the current page.
You should use this before clicking on anything
"""
return run_javascript('Array.from(document.querySelectorAll("a")).map(x => x.innerText + ": " + x.href).join(" - ")')[:4000]
@tool
def chrome_click_on_link(link):
"""
Use this when you want to go to a link.
The link should be a url from a previous observation
"""
return run_javascript(f'window.location.href = "{link}"')[:4000]
@tool
def chrome_read_the_page(input):
"""
Use this when you want to read the page.
"""
return run_javascript('document.body.innerText')[:4000]
# @tool
# def chrome_javascript_action(javascript):
# """
# Use this when you want to execute a javascript command on Chrome either to get data or trigger an action. The command should be in Javascript.
# Here are some examples of good Javascript commands:
# Command: Get the links on the page
# document.querySelectorAll('a')
# Command: Get the buttons on the page
# document.querySelectorAll('button')
# Command: Click the first button on the page
# document.querySelectorAll('button')[0].click()
# Write the Javascript for the command:
# """
# stdout = run_javascript(javascript)
# return f"""
# Current URL: {run_javascript('window.location.href')}
# Result: {stdout}
# """
@tool
def chrome_open_url(url):
"""
Use this tool to open a URL in Chrome. It is recommended to use this tool before doing any other actions on Chrome.
The URL should be a string. For example: https://gmail.com
"""
script = f'''
tell application "Google Chrome"
open location "{url}"
end tell
'''
return run_applescript(script)
def run_javascript(javascript):
javascript = javascript.replace('"', '\\"')
if javascript.startswith('open '):
return "Invalid command, not javascript"
script = f'''
tell application "Google Chrome"
tell active tab of front window
execute javascript "{javascript}"
end tell
end tell
'''
return run_applescript(script)
def run_applescript(applescript):
p = subprocess.Popen(['osascript', '-'], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = p.communicate(applescript.encode('utf-8'))
if p.returncode != 0:
raise Exception(stderr)
decoded_text = stdout.decode("utf-8")
return decoded_text
def say_text(text):
run_applescript(f'say "{text}"')