-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathRemoteTools.py
More file actions
499 lines (412 loc) · 16.2 KB
/
Copy pathRemoteTools.py
File metadata and controls
499 lines (412 loc) · 16.2 KB
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
from langchain.tools import BaseTool
from uuid import uuid4
from pydantic import BaseModel, Field, Extra
import requests
import json
import os
class BaseRemoteTool(BaseTool):
url: str = Field(default_factory=lambda: os.getenv('TERMUX_AGENT_URL', 'http://localhost:8080'))
def _send_cmd(self, cmd: str):
return requests.post(f'{self.url}/run', json={'cmd': cmd}).json()
class Config(BaseTool.Config):
arbitrary_types_allowed = True
extra = Extra.forbid
class BatteryStatusTool(BaseRemoteTool, BaseTool):
name = 'BatteryStatus'
description = (
'Get the current battery status of the device.'
'Useful for determining if the device is charging or not.'
'Useful for getting the battery level.'
'Does not accept any arguments.'
)
def _run(self, *args):
return self._send_cmd('termux-battery-status')
async def _arun(self, *args):
return self._run(*args)
class BrightnessTool(BaseRemoteTool, BaseTool):
name = 'Brightness'
description = (
'Set the screen brightness.'
'Useful for setting the screen brightness.'
'Accepts an integer between 0 and 255 OR the string "auto".'
)
def _run(self, level: int = 'auto'):
return self._send_cmd(f'termux-brightness {level}')
async def _arun(self, level: int = 0):
return self._run(level)
class PhotoTool(BaseRemoteTool, BaseTool):
name = 'TakePhoto'
description = (
'Take a photo with the device camera.'
'Useful for taking a photo with the device camera.'
'Accepts a single optional integer camera id. Default is 0.'
)
def _run(self, camera_id: int = 0):
fn = f'photo-{str(uuid4())}.png'
resp = self._send_cmd(f'termux-camera-photo -c {camera_id} {fn}')
return {'file': fn, 'response': resp}
async def _arun(self, camera_id: int = 0):
return self._run(camera_id)
class ClipboardGetTool(BaseRemoteTool, BaseTool):
name = 'GetClipboard'
description = (
'Get the current clipboard contents.'
'Useful for getting the current clipboard contents.'
'Does not accept any arguments.'
)
def _run(self, *args):
return self._send_cmd('termux-clipboard-get')
async def _arun(self, *args):
return self._run(*args)
class ClipboardSetTool(BaseRemoteTool, BaseTool):
name = 'SetClipboard'
description = (
'Set the current clipboard contents.'
'Useful for setting the current clipboard contents.'
'Accepts a single string.'
)
def _run(self, text: str):
return self._send_cmd(f'termux-clipboard-set {text}')
async def _arun(self, text: str):
return self._run(text)
class FingerprintTool(BaseRemoteTool, BaseTool):
name = 'Fingerprint'
description = (
'Asks the user to scan their fingerprint.'
'Useful for getting authentication from the device owner.'
'Does not accept any arguments.'
)
def _run(self, *args):
return self._send_cmd('termux-fingerprint')
async def _arun(self, *args):
return self._run(*args)
class LocationTool(BaseRemoteTool, BaseTool):
name = 'GetLocation'
description = (
'Get the current location of the device.'
'Useful for getting the current location of the device.'
'Does not accept any arguments.'
'Returns a dict containing latitude and longitude.'
)
def _run(self, *args):
return self._send_cmd('termux-location')
async def _arun(self, *args):
return self._run(*args)
class MediaPlayTool(BaseRemoteTool, BaseTool):
name = 'PlayMedia'
description = (
'Play a media file.'
'Useful for playing a media file.'
'Accepts a single string file path.'
)
def _run(self, path: str):
return self._send_cmd(f'termux-media-player play {path}')
async def _arun(self, path: str):
return self._run(path)
class MediaPauseTool(BaseRemoteTool, BaseTool):
name = 'PauseMedia'
description = (
'Pause the currently playing media.'
'Useful for pausing the currently playing media.'
)
def _run(self, *args):
return self._send_cmd('termux-media-player pause')
async def _arun(self, *args):
return self._run(*args)
class RecordMicTool(BaseRemoteTool, BaseTool):
name = 'RecordMicrophone'
description = (
'Record audio from the device microphone.'
'Useful for recording audio from the device microphone.'
'Accepts a single integer duration in seconds.'
'Returns filename.'
)
def _run(self, duration: int):
fn = f'record-{str(uuid4())}.mp3'
resp = self._send_cmd(f'termux-microphone-record -d {duration} {fn}')
return {'file': fn, 'response': resp}
async def _arun(self, duration: int = 3):
return self._run(duration)
class NotificationTool(BaseRemoteTool, BaseTool):
name = 'CreateNotification'
description = (
'Send a notification to the device.'
'Useful for sending a notification to the device.'
'Accepts one argument, a dictionary in JSON format containing the keys "title" and "message".'
'If message is not provided, it will be set to the same as title.'
)
def _run(self, arguments):
if type(arguments) == dict:
notif = arguments
else:
try:
notif = json.loads(arguments)
except json.JSONDecodeError:
return {'error': 'Invalid JSON'}
return self._send_cmd(f'termux-notification -t {notif["title"]} -c {notif.get("message", notif["title"])}')
async def _arun(self, arguments):
return self._run(arguments)
class ListNotificationsTool(BaseRemoteTool, BaseTool):
name = 'ListNotifications'
description = (
'List the current notifications on the device.'
'Useful for listing the current notifications on the device.'
'Does not accept any arguments.'
'Returns a list of dicts.'
)
def _run(self, *args):
return self._send_cmd('termux-notification-list')
async def _arun(self, *args):
return self._run(*args)
class RemoveNotificationTool(BaseRemoteTool, BaseTool):
name = 'RemoveNotification'
description = (
'Remove a notification from the device.'
'Useful for removing a notification from the device.'
'Accepts a single string id.'
)
def _run(self, id: str):
return self._send_cmd(f'termux-notification-remove {id}')
async def _arun(self, id: str):
return self._run(id)
class URLOpenerTool(BaseRemoteTool, BaseTool):
name = 'OpenURL'
description = (
'Open a URL in the default browser.'
'Useful for opening a URL in the default browser.'
'Accepts a single argument type string, "URL".'
)
def _run(self, url: str):
return self._send_cmd(f'termux-open-url {url}')
async def _arun(self, url: str):
return self._run(url)
class TorchTool(BaseRemoteTool, BaseTool):
name = 'Torch'
description = (
'Turn the device torch on or off.'
'Useful for turning the device torch on or off.'
'Accepts a single argument type string, "on" or "off".'
)
def _run(self, state: str):
if not state in ('on', 'off'):
return 'Error: argument must be "on" or "off"'
return self._send_cmd(f'termux-torch {state}')
async def _arun(self, state: str):
return self._run(state)
class SpeakTool(BaseRemoteTool, BaseTool):
name = 'Speak'
description = (
'Speak a string with TTS.'
'Useful for speaking a string.'
'Accepts a single argument type string.'
)
def _run(self, text: str):
return self._send_cmd(f'termux-tts-speak {text}')
async def _arun(self, text: str):
return self._run(text)
class GetVolumeTool(BaseRemoteTool, BaseTool):
name = 'GetVolume'
description = (
'Get the current volume.'
'Useful for getting the current volume.'
'Returns a dict.'
)
def _run(self, *args):
return self._send_cmd('termux-volume')
async def _arun(self, *args):
return self._run(*args)
class SetVolumeTool(BaseRemoteTool, BaseTool):
name = 'SetVolume'
description = (
'Set the current volume.'
'Useful for setting the current volume.'
'Accepts one argument, a dictionary in JSON format containing the keys "volume_type" (either: "music", "alarm", "notification", or "ring"), and key "value" containing an integer representing volume percentage (0-100).'
)
def _run(self, arguments):
if type(arguments) == dict:
args = arguments
else:
try:
args = json.loads(arguments)
except json.JSONDecodeError:
return {'error': 'Invalid JSON'}
if not args['volume_type'] in ('music', 'alarm', 'notification', 'ring'):
return 'Error: type must be "music", "alarm", "notification", or "ring"'
return self._send_cmd(f'termux-volume {args["volume_type"]} {args["value"]}')
async def _arun(self, arguments):
return self._run(arguments)
class WiFiInfoTool(BaseRemoteTool, BaseTool):
name = 'WiFiInfo'
description = (
'Get information about the WiFi connection.'
'Useful for getting information about the WiFi connection.'
'Does not accept any arguments.'
'Returns a dict.'
)
def _run(self, *args):
return self._send_cmd('termux-wifi-connectioninfo')
async def _arun(self, *args):
return self._run(*args)
class WiFiScanTool(BaseRemoteTool, BaseTool):
name = 'WiFiScan'
description = (
'Scan for WiFi networks.'
'Useful for scanning for WiFi networks.'
'Does not accept any arguments.'
'Returns a list of dicts.'
)
def _run(self, *args):
return self._send_cmd('termux-wifi-scaninfo')
async def _arun(self, *args):
return self._run(*args)
class VibratorTool(BaseRemoteTool, BaseTool):
name = 'Vibrator'
description = (
'Vibrate the device.'
'Useful for vibrating the device.'
'Accepts a single argument type integer, duration in milliseconds.'
)
def _run(self, duration: int):
return self._send_cmd(f'termux-vibrate -f -d {duration}')
async def _arun(self, duration: int):
return self._run(duration)
class SearchContactsTool(BaseRemoteTool, BaseTool):
name = "SearchContacts"
description = (
"Search the contacts on the device."
"Useful for finding phone numbers for people."
"Accepts a single string as an argument, any name containing it will have their info returned."
"Returns a list of dicts containing 'name' and 'number."
)
def _run(self, search: str):
res = self._send_cmd('termux-contact-list')
contacts = json.loads(res['output'])
matches = [c for c in contacts if search.lower() in c['name'].lower()]
return matches
async def _arun(self, *args):
return self._run(*args)
class ListSMSTool(BaseRemoteTool, BaseTool):
name = "ListSMS"
description = (
"Lists SMS messages on the device."
"Useful for reading SMS messages on the device."
"Accepts a single argument, a dictionary in JSON format containing the keys 'box' (either: 'inbox', 'sent', 'draft', 'outbox', 'failed', 'queued', or 'all'), and 'limit' (an integer representing the maximum number of messages to return)."
"Returns a list of dicts containing 'address', 'body', 'date', 'date_sent', 'read', 'status', 'type', 'thread_id', and 'person'."
)
def _run(self, arguments):
if type(arguments) == dict:
args = arguments
else:
try:
args = json.loads(arguments)
except json.JSONDecodeError:
return {'error': 'Invalid JSON'}
if not args['box'] in ('inbox', 'sent', 'draft', 'outbox', 'failed', 'queued', 'all'):
return 'Error: box must be "inbox", "sent", "draft", "outbox", "failed", "queued", or "all"'
return self._send_cmd(f'termux-sms-list -t {args["box"]} -l {args["limit"]}')
async def _arun(self, arguments):
return self._run(arguments)
class SendSMSTool(BaseRemoteTool, BaseTool):
name = "SendSMS"
description = (
"Sends an SMS message."
"Useful for sending an SMS message."
"Accepts a single argument, a dictionary in JSON format containing the keys 'number' (a string representing the phone number to send the message to without spaces or symbols barring country code - seperated by commas for multiple numbers - use the SearchContactsTool to find numbers by name), and 'message' (a string representing the message to send)."
"Does not return any response."
)
def _run(self, arguments):
if type(arguments) == dict:
args = arguments
else:
try:
args = json.loads(arguments)
except json.JSONDecodeError:
return {'error': 'Invalid JSON'}
numbers = args['number'].replace("-",'').replace(' ','').replace('(','').replace(")","")
return self._send_cmd(f'termux-sms-send -n "{numbers}" "{args["message"]}"')
async def _arun(self, arguments):
return self._run(arguments)
class GetCellInfoTool(BaseRemoteTool, BaseTool):
name = "GetCellInfo"
description = (
"Get information about the cellular connection."
"Useful for getting information about the cellular connection."
"Does not accept any arguments."
"Returns two dicts."
)
def _run(self, *args):
return self._send_cmd('termux-telephony-cellinfo && termux-telephony-deviceinfo')
async def _arun(self, *args):
return self._run(*args)
class StartCallTool(BaseRemoteTool, BaseTool):
name = 'StartCall'
description = (
'Start a phone call.'
'Useful for calling a phone number.'
'Accepts a single string argument, the phone number to call.'
'Does not return any response.'
)
def _run(self, number: str):
return self._send_cmd(f'termux-telephony-call {number}')
async def _arun(self, number: str):
return self._run(number)
class ListSensorsTool(BaseRemoteTool, BaseTool):
name = 'ListSensors'
description = (
'List the sensors on the device.'
'Useful for getting all sensors on the device.'
'Does not accept any arguments.'
'Returns a list of dicts containing "name" and "vendor".'
)
def _run(self, *args):
return self._send_cmd('termux-sensor -l')
async def _arun(self, *args):
return self._run(*args)
class ReadSensorTool(BaseRemoteTool, BaseTool):
name = 'ReadSensor'
description = (
"Useful for reading the value(s) from a device sensor."
"Accepts a single arumgnet, a dictionary in JSON format containing the key 'sensor' (a string representing the sensor to read from - seperated by commas for multiple sensors), the key 'limit' containing an integer representing the number of times to read from the sensor."
"Returns a dict containing the sensor name as the key, and the value(s) as a list."
)
def _run(self, arguments):
if type(arguments) == dict:
args = arguments
else:
try:
args = json.loads(arguments)
except json.JSONDecodeError:
return {'error': 'Invalid JSON'}
return self._send_cmd(f'termux-sensor -s {args["sensor"]} -n {args["limit"]}')
async def _arun(self, arguments):
return self._run(arguments)
REMOTE_TOOLS = [
BatteryStatusTool(),
BrightnessTool(),
PhotoTool(),
ClipboardGetTool(),
ClipboardSetTool(),
FingerprintTool(),
LocationTool(),
MediaPauseTool(),
RecordMicTool(),
NotificationTool(),
ListNotificationsTool(),
RemoveNotificationTool(),
URLOpenerTool(),
TorchTool(),
SpeakTool(),
GetVolumeTool(),
SetVolumeTool(),
WiFiInfoTool(),
WiFiScanTool(),
VibratorTool(),
MediaPlayTool(),
SearchContactsTool(),
ListSMSTool(),
SendSMSTool(),
GetCellInfoTool(),
StartCallTool(),
ListSensorsTool(),
ReadSensorTool(),
]