meshagent-computers 0.0.7__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of meshagent-computers might be problematic. Click here for more details.
- meshagent_computers-0.0.7/CHANGELOG.md +12 -0
- meshagent_computers-0.0.7/LICENSE +201 -0
- meshagent_computers-0.0.7/MANIFEST.in +2 -0
- meshagent_computers-0.0.7/PKG-INFO +28 -0
- meshagent_computers-0.0.7/README.md +0 -0
- meshagent_computers-0.0.7/meshagent/computers/__init__.py +7 -0
- meshagent_computers-0.0.7/meshagent/computers/agent.py +225 -0
- meshagent_computers-0.0.7/meshagent/computers/base_playwright.py +179 -0
- meshagent_computers-0.0.7/meshagent/computers/browserbase.py +197 -0
- meshagent_computers-0.0.7/meshagent/computers/computer.py +36 -0
- meshagent_computers-0.0.7/meshagent/computers/docker.py +179 -0
- meshagent_computers-0.0.7/meshagent/computers/local_playwright.py +24 -0
- meshagent_computers-0.0.7/meshagent/computers/operator.py +78 -0
- meshagent_computers-0.0.7/meshagent/computers/scrapybara.py +212 -0
- meshagent_computers-0.0.7/meshagent/computers/utils.py +78 -0
- meshagent_computers-0.0.7/meshagent_computers.egg-info/PKG-INFO +28 -0
- meshagent_computers-0.0.7/meshagent_computers.egg-info/SOURCES.txt +22 -0
- meshagent_computers-0.0.7/meshagent_computers.egg-info/dependency_links.txt +1 -0
- meshagent_computers-0.0.7/meshagent_computers.egg-info/requires.txt +9 -0
- meshagent_computers-0.0.7/meshagent_computers.egg-info/top_level.txt +1 -0
- meshagent_computers-0.0.7/pyproject.toml +5 -0
- meshagent_computers-0.0.7/setup.cfg +4 -0
- meshagent_computers-0.0.7/setup.py +46 -0
- meshagent_computers-0.0.7/version.py +1 -0
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: meshagent-computers
|
|
3
|
+
Version: 0.0.7
|
|
4
|
+
Summary: Computer Building Blocks for Meshagent
|
|
5
|
+
Home-page:
|
|
6
|
+
License: Apache License 2.0
|
|
7
|
+
Project-URL: Documentation, https://meshagent.com
|
|
8
|
+
Project-URL: Website, https://meshagent.com
|
|
9
|
+
Project-URL: Source, https://github.com/meshagent
|
|
10
|
+
Requires-Python: >=3.9.0
|
|
11
|
+
Description-Content-Type: text/markdown
|
|
12
|
+
License-File: LICENSE
|
|
13
|
+
Requires-Dist: pytest>=8.3.4
|
|
14
|
+
Requires-Dist: pytest-asyncio>=0.24.0
|
|
15
|
+
Requires-Dist: openai>=1.66.2
|
|
16
|
+
Requires-Dist: meshagent-api>=0.0.7
|
|
17
|
+
Requires-Dist: meshagent-agents>=0.0.7
|
|
18
|
+
Requires-Dist: meshagent-tools>=0.0.7
|
|
19
|
+
Requires-Dist: playwright~=1.50.0
|
|
20
|
+
Requires-Dist: browserbase~=1.2.0
|
|
21
|
+
Requires-Dist: scrapybara~=2.4.1
|
|
22
|
+
Dynamic: description-content-type
|
|
23
|
+
Dynamic: license
|
|
24
|
+
Dynamic: license-file
|
|
25
|
+
Dynamic: project-url
|
|
26
|
+
Dynamic: requires-dist
|
|
27
|
+
Dynamic: requires-python
|
|
28
|
+
Dynamic: summary
|
|
File without changes
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
from .computer import Computer
|
|
2
|
+
from .browserbase import BrowserbaseBrowser
|
|
3
|
+
from .local_playwright import LocalPlaywrightComputer
|
|
4
|
+
from .docker import DockerComputer
|
|
5
|
+
from .scrapybara import ScrapybaraBrowser, ScrapybaraUbuntu
|
|
6
|
+
from .operator import Operator
|
|
7
|
+
from .agent import ComputerAgent
|
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
from meshagent.openai import OpenAIResponsesAdapter
|
|
2
|
+
from meshagent.agents import LLMAdapter, AgentChatContext
|
|
3
|
+
from meshagent.tools import Tool, Toolkit, ToolContext
|
|
4
|
+
from meshagent.agents.prompt import PromptAgent
|
|
5
|
+
from meshagent.computers import Computer, Operator
|
|
6
|
+
from meshagent.agents.chat import ChatBot, ChatThreadContext
|
|
7
|
+
from meshagent.api import RemoteParticipant, FileResponse
|
|
8
|
+
from meshagent.api.messaging import RawOutputs
|
|
9
|
+
|
|
10
|
+
from typing import Optional
|
|
11
|
+
import base64
|
|
12
|
+
import json
|
|
13
|
+
import logging
|
|
14
|
+
|
|
15
|
+
logging.basicConfig()
|
|
16
|
+
logger = logging.getLogger("computer")
|
|
17
|
+
logger.setLevel(logging.INFO)
|
|
18
|
+
|
|
19
|
+
class ComputerAgent[ComputerType:Computer, OperatorType:Operator](ChatBot):
|
|
20
|
+
def __init__(self, *, name,
|
|
21
|
+
title=None,
|
|
22
|
+
description=None,
|
|
23
|
+
requires=None,
|
|
24
|
+
labels = None,
|
|
25
|
+
computer_cls: ComputerType,
|
|
26
|
+
operator_cls: OperatorType,
|
|
27
|
+
rules: Optional[list[str]] = None,
|
|
28
|
+
llm_adapter: Optional[LLMAdapter] = None,
|
|
29
|
+
toolkits: list[Toolkit] = None
|
|
30
|
+
):
|
|
31
|
+
|
|
32
|
+
if rules == None:
|
|
33
|
+
rules=[
|
|
34
|
+
"if asked to go to a URL, you MUST use the goto function to go to the url if it is available",
|
|
35
|
+
"after going directly to a URL, the screen will change so you should take a look at it to know what to do next"
|
|
36
|
+
]
|
|
37
|
+
super().__init__(
|
|
38
|
+
name=name,
|
|
39
|
+
title=title,
|
|
40
|
+
description=description,
|
|
41
|
+
requires=requires,
|
|
42
|
+
labels=labels,
|
|
43
|
+
llm_adapter=llm_adapter,
|
|
44
|
+
toolkits=toolkits,
|
|
45
|
+
rules=rules
|
|
46
|
+
)
|
|
47
|
+
self.computer_cls = computer_cls
|
|
48
|
+
self.operator_cls = operator_cls
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
async def init_thread_context(self, *, thread_context: ChatThreadContext):
|
|
52
|
+
|
|
53
|
+
operator : Operator = self.operator_cls()
|
|
54
|
+
computer : Computer = self.computer_cls()
|
|
55
|
+
started = False
|
|
56
|
+
|
|
57
|
+
class ComputerTool(Tool):
|
|
58
|
+
def __init__(self, *, operator: Operator, computer: Computer, title = "computer_call", description = "handle computer calls from computer use preview", rules = [], thumbnail_url = None, defs = None):
|
|
59
|
+
super().__init__(
|
|
60
|
+
name="computer_call",
|
|
61
|
+
# TODO: give a correct schema
|
|
62
|
+
input_schema={
|
|
63
|
+
"additionalProperties" : False,
|
|
64
|
+
"type" : "object",
|
|
65
|
+
"required" : [],
|
|
66
|
+
"properties" : {}
|
|
67
|
+
},
|
|
68
|
+
title=title,
|
|
69
|
+
description=description,
|
|
70
|
+
rules=rules,
|
|
71
|
+
thumbnail_url=thumbnail_url,
|
|
72
|
+
defs=defs,
|
|
73
|
+
|
|
74
|
+
)
|
|
75
|
+
self.computer = computer
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
@property
|
|
79
|
+
def options(self):
|
|
80
|
+
return {
|
|
81
|
+
"type": "computer-preview",
|
|
82
|
+
"display_width": self.computer.dimensions[0],
|
|
83
|
+
"display_height": self.computer.dimensions[1],
|
|
84
|
+
"environment": self.computer.environment,
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
async def execute(self, context: ToolContext, *, arguments):
|
|
88
|
+
|
|
89
|
+
nonlocal started
|
|
90
|
+
if started == False:
|
|
91
|
+
await self.computer.__aenter__()
|
|
92
|
+
started = True
|
|
93
|
+
|
|
94
|
+
for participant in thread_context.participants:
|
|
95
|
+
await context.room.messaging.send_message(
|
|
96
|
+
to=participant,
|
|
97
|
+
type="computer_use",
|
|
98
|
+
message={
|
|
99
|
+
"arguments" : arguments
|
|
100
|
+
}
|
|
101
|
+
)
|
|
102
|
+
|
|
103
|
+
outputs = await operator.play(computer=self.computer, item=arguments)
|
|
104
|
+
for output in outputs:
|
|
105
|
+
if output["type"] == "computer_call_output":
|
|
106
|
+
if output["output"] != None:
|
|
107
|
+
if output["output"]["type"] == "input_image":
|
|
108
|
+
|
|
109
|
+
b64 : str = output["output"]["image_url"]
|
|
110
|
+
image_data_b64 = b64.split(",", 1)
|
|
111
|
+
|
|
112
|
+
image_bytes = base64.b64decode(image_data_b64[1])
|
|
113
|
+
|
|
114
|
+
for participant in thread_context.participants:
|
|
115
|
+
context.room.messaging.send_message_nowait(
|
|
116
|
+
to=participant,
|
|
117
|
+
type="computer_screen",
|
|
118
|
+
message={
|
|
119
|
+
},
|
|
120
|
+
attachment=image_bytes
|
|
121
|
+
)
|
|
122
|
+
|
|
123
|
+
nonlocal computer_toolkit
|
|
124
|
+
if len(computer_toolkit.tools) == 1:
|
|
125
|
+
# HACK: after looking at the page, add the other tools,
|
|
126
|
+
# if we add these first then the computer-use-preview mode fails if it calls them before using the computer
|
|
127
|
+
computer_toolkit.tools.extend([
|
|
128
|
+
ScreenshotTool(computer=computer),
|
|
129
|
+
GotoURL(computer=computer),
|
|
130
|
+
])
|
|
131
|
+
return RawOutputs(outputs=outputs)
|
|
132
|
+
|
|
133
|
+
class ScreenshotTool(Tool):
|
|
134
|
+
def __init__(self, computer: Computer):
|
|
135
|
+
self.computer = computer
|
|
136
|
+
|
|
137
|
+
super().__init__(
|
|
138
|
+
name="screenshot",
|
|
139
|
+
# TODO: give a correct schema
|
|
140
|
+
input_schema={
|
|
141
|
+
"additionalProperties" : False,
|
|
142
|
+
"type" : "object",
|
|
143
|
+
"required" : ["full_page","save_path"],
|
|
144
|
+
"properties" : {
|
|
145
|
+
"full_page" : {
|
|
146
|
+
"type" : "boolean"
|
|
147
|
+
},
|
|
148
|
+
"save_path" : {
|
|
149
|
+
"type" : "string",
|
|
150
|
+
"description" : "a file path to save the screenshot to (should end with .png)"
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
},
|
|
154
|
+
description="take a screenshot of the current page",
|
|
155
|
+
)
|
|
156
|
+
|
|
157
|
+
|
|
158
|
+
async def execute(self, context: ToolContext, save_path: str, full_page: bool):
|
|
159
|
+
nonlocal started
|
|
160
|
+
if started == False:
|
|
161
|
+
await self.computer.__aenter__()
|
|
162
|
+
started = True
|
|
163
|
+
|
|
164
|
+
screenshot_bytes = await self.computer.screenshot_bytes(full_page=full_page)
|
|
165
|
+
handle = await context.room.storage.open(path=save_path, overwrite=True)
|
|
166
|
+
await context.room.storage.write(handle=handle, data=screenshot_bytes)
|
|
167
|
+
await context.room.storage.close(handle=handle)
|
|
168
|
+
|
|
169
|
+
return f"saved screenshot to {save_path}"
|
|
170
|
+
|
|
171
|
+
class GotoURL(Tool):
|
|
172
|
+
def __init__(self, computer: Computer):
|
|
173
|
+
self.computer = computer
|
|
174
|
+
|
|
175
|
+
super().__init__(
|
|
176
|
+
name="goto",
|
|
177
|
+
description="goes to a specific URL. Make sure it starts with http:// or https://",
|
|
178
|
+
# TODO: give a correct schema
|
|
179
|
+
input_schema={
|
|
180
|
+
"additionalProperties" : False,
|
|
181
|
+
"type" : "object",
|
|
182
|
+
"required" : ["url"],
|
|
183
|
+
"properties" : {
|
|
184
|
+
"url" : {
|
|
185
|
+
"type" : "string",
|
|
186
|
+
"description": "Fully qualified URL to navigate to.",
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
},
|
|
190
|
+
)
|
|
191
|
+
|
|
192
|
+
|
|
193
|
+
async def execute(self, context: ToolContext, url: str):
|
|
194
|
+
nonlocal started
|
|
195
|
+
if started == False:
|
|
196
|
+
await self.computer.__aenter__()
|
|
197
|
+
started = True
|
|
198
|
+
|
|
199
|
+
if url.startswith("https://") == False and url.startswith("http://") == False:
|
|
200
|
+
url = "https://"+url
|
|
201
|
+
|
|
202
|
+
await self.computer.goto(url)
|
|
203
|
+
|
|
204
|
+
# send an updated screen out
|
|
205
|
+
for participant in thread_context.participants:
|
|
206
|
+
context.room.messaging.send_message_nowait(
|
|
207
|
+
to=participant,
|
|
208
|
+
type="computer_screen",
|
|
209
|
+
message={
|
|
210
|
+
},
|
|
211
|
+
attachment = await self.computer.screenshot_bytes(full_page=False)
|
|
212
|
+
)
|
|
213
|
+
|
|
214
|
+
computer_tool = ComputerTool(computer=computer, operator=operator)
|
|
215
|
+
|
|
216
|
+
computer_toolkit = Toolkit(name="meshagent.openai.computer", tools=[
|
|
217
|
+
computer_tool
|
|
218
|
+
])
|
|
219
|
+
|
|
220
|
+
thread_context.toolkits = [
|
|
221
|
+
computer_toolkit,
|
|
222
|
+
*thread_context.toolkits
|
|
223
|
+
]
|
|
224
|
+
|
|
225
|
+
|
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
import time
|
|
2
|
+
import base64
|
|
3
|
+
from typing import List, Dict, Literal
|
|
4
|
+
from playwright.async_api import async_playwright, Browser, Page, Route, Request
|
|
5
|
+
from meshagent.computers.utils import check_blocklisted_url
|
|
6
|
+
|
|
7
|
+
# Optional: key mapping if your model uses "CUA" style keys
|
|
8
|
+
CUA_KEY_TO_PLAYWRIGHT_KEY = {
|
|
9
|
+
"/": "Divide",
|
|
10
|
+
"\\": "Backslash",
|
|
11
|
+
"alt": "Alt",
|
|
12
|
+
"arrowdown": "ArrowDown",
|
|
13
|
+
"arrowleft": "ArrowLeft",
|
|
14
|
+
"arrowright": "ArrowRight",
|
|
15
|
+
"arrowup": "ArrowUp",
|
|
16
|
+
"backspace": "Backspace",
|
|
17
|
+
"capslock": "CapsLock",
|
|
18
|
+
"cmd": "Meta",
|
|
19
|
+
"ctrl": "Control",
|
|
20
|
+
"delete": "Delete",
|
|
21
|
+
"end": "End",
|
|
22
|
+
"enter": "Enter",
|
|
23
|
+
"esc": "Escape",
|
|
24
|
+
"home": "Home",
|
|
25
|
+
"insert": "Insert",
|
|
26
|
+
"option": "Alt",
|
|
27
|
+
"pagedown": "PageDown",
|
|
28
|
+
"pageup": "PageUp",
|
|
29
|
+
"shift": "Shift",
|
|
30
|
+
"space": " ",
|
|
31
|
+
"super": "Meta",
|
|
32
|
+
"tab": "Tab",
|
|
33
|
+
"win": "Meta",
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
class BasePlaywrightComputer:
|
|
38
|
+
"""
|
|
39
|
+
Abstract base for Playwright-based computers:
|
|
40
|
+
|
|
41
|
+
- Subclasses override `_get_browser_and_page()` to do local or remote connection,
|
|
42
|
+
returning (Browser, Page).
|
|
43
|
+
- This base class handles context creation (`__enter__`/`__exit__`),
|
|
44
|
+
plus standard "Computer" actions like click, scroll, etc.
|
|
45
|
+
- We also have extra browser actions: `goto(url)` and `back()`.
|
|
46
|
+
"""
|
|
47
|
+
|
|
48
|
+
environment: Literal["browser"] = "browser"
|
|
49
|
+
dimensions = (1024, 768)
|
|
50
|
+
|
|
51
|
+
def __init__(self):
|
|
52
|
+
self._playwright = None
|
|
53
|
+
self._browser: Browser | None = None
|
|
54
|
+
self._page: Page | None = None
|
|
55
|
+
|
|
56
|
+
async def __aenter__(self):
|
|
57
|
+
# Start Playwright and call the subclass hook for getting browser/page
|
|
58
|
+
self._context = async_playwright()
|
|
59
|
+
self._playwright = await self._context.__aenter__()
|
|
60
|
+
self._browser, self._page = await self._get_browser_and_page()
|
|
61
|
+
|
|
62
|
+
# Set up network interception to flag URLs matching domains in BLOCKED_DOMAINS
|
|
63
|
+
async def handle_route(route: Route, request: Request):
|
|
64
|
+
|
|
65
|
+
url = request.url
|
|
66
|
+
if check_blocklisted_url(url):
|
|
67
|
+
print(f"Flagging blocked domain: {url}")
|
|
68
|
+
await route.abort()
|
|
69
|
+
else:
|
|
70
|
+
await route.continue_()
|
|
71
|
+
|
|
72
|
+
await self._page.route("**/*", handle_route)
|
|
73
|
+
|
|
74
|
+
return self
|
|
75
|
+
|
|
76
|
+
async def __aexit__(self, exc_type, exc_val, exc_tb):
|
|
77
|
+
if self._browser:
|
|
78
|
+
await self._browser.close()
|
|
79
|
+
if self._playwright:
|
|
80
|
+
await self._context.__aexit__(exc_type, exc_val, exc_tb)
|
|
81
|
+
|
|
82
|
+
def get_current_url(self) -> str:
|
|
83
|
+
if self._page == None:
|
|
84
|
+
return "about:blank"
|
|
85
|
+
|
|
86
|
+
async def ensure_page(self):
|
|
87
|
+
# After a timeout, we might loose our browser
|
|
88
|
+
if self._page == None or self._browser.is_connected == False:
|
|
89
|
+
self._browser, self._page = await self._get_browser_and_page()
|
|
90
|
+
|
|
91
|
+
# --- Common "Computer" actions ---
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
async def screenshot_bytes(self, full_page: bool = False) -> bytes:
|
|
95
|
+
await self.ensure_page()
|
|
96
|
+
png_bytes = await self._page.screenshot(full_page=full_page)
|
|
97
|
+
return png_bytes
|
|
98
|
+
|
|
99
|
+
async def screenshot(self, full_page: bool = False) -> str:
|
|
100
|
+
await self.ensure_page()
|
|
101
|
+
png_bytes = await self.screenshot_bytes(full_page=full_page)
|
|
102
|
+
return base64.b64encode(png_bytes).decode("utf-8")
|
|
103
|
+
|
|
104
|
+
async def click(self, x: int, y: int, button: str = "left") -> None:
|
|
105
|
+
await self.ensure_page()
|
|
106
|
+
match button:
|
|
107
|
+
case "back":
|
|
108
|
+
await self.back()
|
|
109
|
+
case "forward":
|
|
110
|
+
await self.forward()
|
|
111
|
+
case "wheel":
|
|
112
|
+
await self._page.mouse.wheel(x, y)
|
|
113
|
+
case _:
|
|
114
|
+
button_mapping = {"left": "left", "right": "right"}
|
|
115
|
+
button_type = button_mapping.get(button, "left")
|
|
116
|
+
await self._page.mouse.click(x, y, button=button_type)
|
|
117
|
+
|
|
118
|
+
async def double_click(self, x: int, y: int) -> None:
|
|
119
|
+
await self.ensure_page()
|
|
120
|
+
await self._page.mouse.dblclick(x, y)
|
|
121
|
+
|
|
122
|
+
async def scroll(self, x: int, y: int, scroll_x: int, scroll_y: int) -> None:
|
|
123
|
+
await self.ensure_page()
|
|
124
|
+
await self._page.mouse.move(x, y)
|
|
125
|
+
await self._page.evaluate(f"window.scrollBy({scroll_x}, {scroll_y})")
|
|
126
|
+
|
|
127
|
+
async def type(self, text: str) -> None:
|
|
128
|
+
await self.ensure_page()
|
|
129
|
+
await self._page.keyboard.type(text)
|
|
130
|
+
|
|
131
|
+
async def wait(self, ms: int = 1000) -> None:
|
|
132
|
+
await self.ensure_page()
|
|
133
|
+
time.sleep(ms / 1000)
|
|
134
|
+
|
|
135
|
+
async def move(self, x: int, y: int) -> None:
|
|
136
|
+
await self.ensure_page()
|
|
137
|
+
await self._page.mouse.move(x, y)
|
|
138
|
+
|
|
139
|
+
async def keypress(self, keys: List[str]) -> None:
|
|
140
|
+
await self.ensure_page()
|
|
141
|
+
for key in keys:
|
|
142
|
+
mapped_key = CUA_KEY_TO_PLAYWRIGHT_KEY.get(key.lower(), key)
|
|
143
|
+
await self._page.keyboard.press(mapped_key)
|
|
144
|
+
|
|
145
|
+
async def drag(self, path: List[Dict[str, int]]) -> None:
|
|
146
|
+
await self.ensure_page()
|
|
147
|
+
if not path:
|
|
148
|
+
return
|
|
149
|
+
|
|
150
|
+
await self._page.mouse.move(path[0]["x"], path[0]["y"])
|
|
151
|
+
await self._page.mouse.down()
|
|
152
|
+
for point in path[1:]:
|
|
153
|
+
await self._page.mouse.move(point["x"], point["y"])
|
|
154
|
+
await self._page.mouse.up()
|
|
155
|
+
|
|
156
|
+
async def get_current_url(self) -> str:
|
|
157
|
+
await self.ensure_page()
|
|
158
|
+
return self._page.url
|
|
159
|
+
|
|
160
|
+
# --- Extra browser-oriented actions ---
|
|
161
|
+
async def goto(self, url: str) -> None:
|
|
162
|
+
await self.ensure_page()
|
|
163
|
+
try:
|
|
164
|
+
return await self._page.goto(url)
|
|
165
|
+
except Exception as e:
|
|
166
|
+
print(f"Error navigating to {url}: {e}")
|
|
167
|
+
|
|
168
|
+
async def back(self) -> None:
|
|
169
|
+
await self.ensure_page()
|
|
170
|
+
return await self._page.go_back()
|
|
171
|
+
|
|
172
|
+
async def forward(self) -> None:
|
|
173
|
+
await self.ensure_page()
|
|
174
|
+
return await self._page.go_forward()
|
|
175
|
+
|
|
176
|
+
# --- Subclass hook ---
|
|
177
|
+
async def _get_browser_and_page(self) -> tuple[Browser, Page]:
|
|
178
|
+
"""Subclasses must implement, returning (Browser, Page)."""
|
|
179
|
+
raise NotImplementedError
|