beswarm 0.1.84__py3-none-any.whl → 0.1.86__py3-none-any.whl
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.
- beswarm/tools/worker.py +36 -9
- beswarm/utils.py +22 -0
- {beswarm-0.1.84.dist-info → beswarm-0.1.86.dist-info}/METADATA +1 -1
- {beswarm-0.1.84.dist-info → beswarm-0.1.86.dist-info}/RECORD +6 -6
- {beswarm-0.1.84.dist-info → beswarm-0.1.86.dist-info}/WHEEL +0 -0
- {beswarm-0.1.84.dist-info → beswarm-0.1.86.dist-info}/top_level.txt +0 -0
beswarm/tools/worker.py
CHANGED
@@ -3,12 +3,13 @@ import re
|
|
3
3
|
import copy
|
4
4
|
import json
|
5
5
|
import platform
|
6
|
+
from pathlib import Path
|
6
7
|
from datetime import datetime
|
7
8
|
|
8
9
|
from ..aient.src.aient.models import chatgpt
|
9
10
|
from ..aient.src.aient.plugins import register_tool, get_function_call_list
|
10
11
|
from ..prompt import worker_system_prompt, instruction_system_prompt
|
11
|
-
from ..utils import extract_xml_content, get_current_screen_image_message
|
12
|
+
from ..utils import extract_xml_content, get_current_screen_image_message, replace_xml_content
|
12
13
|
|
13
14
|
@register_tool()
|
14
15
|
async def worker(goal, tools, work_dir, cache_messages=None):
|
@@ -28,6 +29,7 @@ async def worker(goal, tools, work_dir, cache_messages=None):
|
|
28
29
|
str: 当任务成功完成时,返回字符串 "任务已完成"。
|
29
30
|
"""
|
30
31
|
start_time = datetime.now()
|
32
|
+
finish_flag = 0
|
31
33
|
|
32
34
|
tools_json = [value for _, value in get_function_call_list(tools).items()]
|
33
35
|
work_agent_system_prompt = worker_system_prompt.format(
|
@@ -48,7 +50,13 @@ async def worker(goal, tools, work_dir, cache_messages=None):
|
|
48
50
|
"function_call_max_loop": 100,
|
49
51
|
}
|
50
52
|
if cache_messages:
|
51
|
-
|
53
|
+
if isinstance(cache_messages, bool) and cache_messages == True:
|
54
|
+
cache_file_path = Path(work_dir) / ".beswarm" / "work_agent_conversation_history.json"
|
55
|
+
if cache_file_path.exists():
|
56
|
+
with cache_file_path.open("r", encoding="utf-8") as f:
|
57
|
+
cache_messages = json.load(f)
|
58
|
+
first_user_message = replace_xml_content(cache_messages[1]["content"], "goal", goal)
|
59
|
+
work_agent_config["cache_messages"] = cache_messages[0:1] + [{"role": "user", "content": first_user_message}] + cache_messages[2:]
|
52
60
|
|
53
61
|
instruction_agent_config = {
|
54
62
|
"api_key": os.getenv("API_KEY"),
|
@@ -131,7 +139,7 @@ async def worker(goal, tools, work_dir, cache_messages=None):
|
|
131
139
|
if conversation_history == []:
|
132
140
|
next_instruction = (
|
133
141
|
"任务描述:\n"
|
134
|
-
f"{goal}
|
142
|
+
f"<goal>{goal}</goal>\n\n"
|
135
143
|
"现在开始执行第一步:\n"
|
136
144
|
f"{next_instruction}"
|
137
145
|
)
|
@@ -146,8 +154,14 @@ async def worker(goal, tools, work_dir, cache_messages=None):
|
|
146
154
|
|
147
155
|
# 检查任务是否完成
|
148
156
|
if "任务已完成" in next_instruction and len(next_instruction) < 10:
|
149
|
-
|
150
|
-
|
157
|
+
if finish_flag == 0:
|
158
|
+
finish_flag = 1
|
159
|
+
continue
|
160
|
+
elif finish_flag == 1:
|
161
|
+
print("\n✅ 任务已完成!")
|
162
|
+
break
|
163
|
+
else:
|
164
|
+
finish_flag = 0
|
151
165
|
if "find_and_click_element" in str(tools_json):
|
152
166
|
next_instruction = await get_current_screen_image_message(next_instruction)
|
153
167
|
result = await work_agent.ask_async(next_instruction)
|
@@ -167,6 +181,7 @@ async def worker(goal, tools, work_dir, cache_messages=None):
|
|
167
181
|
|
168
182
|
async def worker_gen(goal, tools, work_dir, cache_messages=None):
|
169
183
|
start_time = datetime.now()
|
184
|
+
finish_flag = 0
|
170
185
|
tools_json = [value for _, value in get_function_call_list(tools).items()]
|
171
186
|
work_agent_system_prompt = worker_system_prompt.format(
|
172
187
|
os_version=platform.platform(),
|
@@ -186,7 +201,13 @@ async def worker_gen(goal, tools, work_dir, cache_messages=None):
|
|
186
201
|
"function_call_max_loop": 100,
|
187
202
|
}
|
188
203
|
if cache_messages:
|
189
|
-
|
204
|
+
if isinstance(cache_messages, bool) and cache_messages == True:
|
205
|
+
cache_file_path = Path(work_dir) / ".beswarm" / "work_agent_conversation_history.json"
|
206
|
+
if cache_file_path.exists():
|
207
|
+
with cache_file_path.open("r", encoding="utf-8") as f:
|
208
|
+
cache_messages = json.load(f)
|
209
|
+
first_user_message = replace_xml_content(cache_messages[1]["content"], "goal", goal)
|
210
|
+
work_agent_config["cache_messages"] = cache_messages[0:1] + [{"role": "user", "content": first_user_message}] + cache_messages[2:]
|
190
211
|
|
191
212
|
instruction_agent_config = {
|
192
213
|
"api_key": os.getenv("API_KEY"),
|
@@ -270,7 +291,7 @@ async def worker_gen(goal, tools, work_dir, cache_messages=None):
|
|
270
291
|
if conversation_history == []:
|
271
292
|
next_instruction = (
|
272
293
|
"任务描述:\n"
|
273
|
-
f"{goal}
|
294
|
+
f"<goal>{goal}</goal>\n\n"
|
274
295
|
"现在开始执行第一步:\n"
|
275
296
|
f"{next_instruction}"
|
276
297
|
)
|
@@ -287,8 +308,14 @@ async def worker_gen(goal, tools, work_dir, cache_messages=None):
|
|
287
308
|
|
288
309
|
# 检查任务是否完成
|
289
310
|
if "任务已完成" in next_instruction and len(next_instruction) < 10:
|
290
|
-
|
291
|
-
|
311
|
+
if finish_flag == 0:
|
312
|
+
finish_flag = 1
|
313
|
+
continue
|
314
|
+
elif finish_flag == 1:
|
315
|
+
print("\n✅ 任务已完成!")
|
316
|
+
break
|
317
|
+
else:
|
318
|
+
finish_flag = 0
|
292
319
|
if "find_and_click_element" in str(tools_json):
|
293
320
|
next_instruction = await get_current_screen_image_message(next_instruction)
|
294
321
|
result = await work_agent.ask_async(next_instruction)
|
beswarm/utils.py
CHANGED
@@ -10,6 +10,28 @@ def extract_xml_content(text, xml_tag):
|
|
10
10
|
return ''
|
11
11
|
return result
|
12
12
|
|
13
|
+
def replace_xml_content(original_string: str, tag_name: str, replacement_content: str) -> str:
|
14
|
+
"""
|
15
|
+
将指定XML标签内的内容替换为新内容。
|
16
|
+
|
17
|
+
此函数使用正则表达式查找所有匹配的XML标签对(例如 `<tag>...</tag>`),
|
18
|
+
并将其中的内容替换为 `replacement_content`。
|
19
|
+
|
20
|
+
Args:
|
21
|
+
original_string (str): 包含XML标记的原始字符串。
|
22
|
+
tag_name (str): 要定位的XML标签的名称(不带尖括号)。
|
23
|
+
replacement_content (str): 用于替换标签内部内容的新字符串。
|
24
|
+
|
25
|
+
Returns:
|
26
|
+
str: 返回内容已被替换的新字符串。如果未找到匹配的标签,则返回原始字符串。
|
27
|
+
"""
|
28
|
+
pattern = f"<{tag_name}>.*?<\\/{tag_name}>"
|
29
|
+
replacement = f"<{tag_name}>{replacement_content}</{tag_name}>"
|
30
|
+
|
31
|
+
new_string = re.sub(pattern, replacement, original_string, flags=re.DOTALL)
|
32
|
+
|
33
|
+
return new_string
|
34
|
+
|
13
35
|
import io
|
14
36
|
import base64
|
15
37
|
from .aient.src.aient.core.utils import get_image_message, get_text_message
|
@@ -1,6 +1,6 @@
|
|
1
1
|
beswarm/__init__.py,sha256=HZjUOJtZR5QhMuDbq-wukQQn1VrBusNWai_ysGo-VVI,20
|
2
2
|
beswarm/prompt.py,sha256=1jNxVXjfhb-A8CVHoudRxytV4qDT6FZIIk1NRCCE1Ns,31365
|
3
|
-
beswarm/utils.py,sha256=
|
3
|
+
beswarm/utils.py,sha256=cOYwuONpNG_dkSYIvdEqQOxRUdIy0Bh9CTYkvKKskdw,2816
|
4
4
|
beswarm/aient/main.py,sha256=SiYAIgQlLJqYusnTVEJOx1WNkSJKMImhgn5aWjfroxg,3814
|
5
5
|
beswarm/aient/setup.py,sha256=ub3Tx7R0rcvHG9bJy7qp-mDWUjcxJJ3yQm8jpOtx8AY,487
|
6
6
|
beswarm/aient/src/aient/__init__.py,sha256=SRfF7oDVlOOAi6nGKiJIUK6B_arqYLO9iSMp-2IZZps,21
|
@@ -128,8 +128,8 @@ beswarm/tools/screenshot.py,sha256=u6t8FCgW5YHJ_Oc4coo8e0F3wTusWE_-H8dFh1rBq9Q,1
|
|
128
128
|
beswarm/tools/search_arxiv.py,sha256=GpuIOYX8T0iRC-X-hmuR9AUJVn15WWZq864DaoC7BUc,8004
|
129
129
|
beswarm/tools/search_web.py,sha256=B24amOnGHnmdV_6S8bw8O2PdhZRRIDtJjg-wXcfP7dQ,11859
|
130
130
|
beswarm/tools/think.py,sha256=WLw-7jNIsnS6n8MMSYUin_f-BGLENFmnKM2LISEp0co,1760
|
131
|
-
beswarm/tools/worker.py,sha256=
|
132
|
-
beswarm-0.1.
|
133
|
-
beswarm-0.1.
|
134
|
-
beswarm-0.1.
|
135
|
-
beswarm-0.1.
|
131
|
+
beswarm/tools/worker.py,sha256=VOulEZBEc5nF2RBR9aLYs0vT7WBD-r2mlWH5ueEo4hM,16007
|
132
|
+
beswarm-0.1.86.dist-info/METADATA,sha256=CEzs_BeynBW6FQDq9GBOZmkYU0eQ4EkYxzSgwBnGfz4,3553
|
133
|
+
beswarm-0.1.86.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
|
134
|
+
beswarm-0.1.86.dist-info/top_level.txt,sha256=pJw4O87wvt5882smuSO6DfByJz7FJ8SxxT8h9fHCmpo,8
|
135
|
+
beswarm-0.1.86.dist-info/RECORD,,
|
File without changes
|
File without changes
|