Skip to content

Commit 1249929

Browse files
committed
feat: 新增Agent浏览器操作工具(browse_webpage),支持通过Playwright控制浏览器进行网页交互
1 parent 864af45 commit 1249929

2 files changed

Lines changed: 568 additions & 18 deletions

File tree

app/agent/tools/factory.py

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
from app.agent.tools.impl.edit_file import EditFileTool
4646
from app.agent.tools.impl.write_file import WriteFileTool
4747
from app.agent.tools.impl.read_file import ReadFileTool
48+
from app.agent.tools.impl.browse_webpage import BrowseWebpageTool
4849
from app.core.plugin import PluginManager
4950
from app.log import logger
5051
from .base import MoviePilotTool
@@ -56,9 +57,14 @@ class MoviePilotToolFactory:
5657
"""
5758

5859
@staticmethod
59-
def create_tools(session_id: str, user_id: str,
60-
channel: str = None, source: str = None, username: str = None,
61-
stream_handler: Callable = None) -> List[MoviePilotTool]:
60+
def create_tools(
61+
session_id: str,
62+
user_id: str,
63+
channel: str = None,
64+
source: str = None,
65+
username: str = None,
66+
stream_handler: Callable = None,
67+
) -> List[MoviePilotTool]:
6268
"""
6369
创建MoviePilot工具列表
6470
"""
@@ -108,18 +114,16 @@ def create_tools(session_id: str, user_id: str,
108114
ExecuteCommandTool,
109115
EditFileTool,
110116
WriteFileTool,
111-
ReadFileTool
117+
ReadFileTool,
118+
BrowseWebpageTool,
112119
]
113120
# 创建内置工具
114121
for ToolClass in tool_definitions:
115-
tool = ToolClass(
116-
session_id=session_id,
117-
user_id=user_id
118-
)
122+
tool = ToolClass(session_id=session_id, user_id=user_id)
119123
tool.set_message_attr(channel=channel, source=source, username=username)
120124
tool.set_stream_handler(stream_handler=stream_handler)
121125
tools.append(tool)
122-
126+
123127
# 加载插件提供的工具
124128
plugin_tools_count = 0
125129
plugin_tools_info = PluginManager().get_plugin_agent_tools()
@@ -131,24 +135,31 @@ def create_tools(session_id: str, user_id: str,
131135
try:
132136
# 验证工具类是否继承自 MoviePilotTool
133137
if not issubclass(ToolClass, MoviePilotTool):
134-
logger.warning(f"插件 {plugin_name}({plugin_id}) 提供的工具类 {ToolClass.__name__} 未继承自 MoviePilotTool,已跳过")
138+
logger.warning(
139+
f"插件 {plugin_name}({plugin_id}) 提供的工具类 {ToolClass.__name__} 未继承自 MoviePilotTool,已跳过"
140+
)
135141
continue
136142
# 创建工具实例
137-
tool = ToolClass(
138-
session_id=session_id,
139-
user_id=user_id
143+
tool = ToolClass(session_id=session_id, user_id=user_id)
144+
tool.set_message_attr(
145+
channel=channel, source=source, username=username
140146
)
141-
tool.set_message_attr(channel=channel, source=source, username=username)
142147
tool.set_stream_handler(stream_handler=stream_handler)
143148
tools.append(tool)
144149
plugin_tools_count += 1
145-
logger.debug(f"成功加载插件 {plugin_name}({plugin_id}) 的工具: {ToolClass.__name__}")
150+
logger.debug(
151+
f"成功加载插件 {plugin_name}({plugin_id}) 的工具: {ToolClass.__name__}"
152+
)
146153
except Exception as e:
147-
logger.error(f"加载插件 {plugin_name}({plugin_id}) 的工具 {ToolClass.__name__} 失败: {str(e)}")
148-
154+
logger.error(
155+
f"加载插件 {plugin_name}({plugin_id}) 的工具 {ToolClass.__name__} 失败: {str(e)}"
156+
)
157+
149158
builtin_tools_count = len(tool_definitions)
150159
if plugin_tools_count > 0:
151-
logger.info(f"成功创建 {len(tools)} 个MoviePilot工具(内置工具: {builtin_tools_count} 个,插件工具: {plugin_tools_count} 个)")
160+
logger.info(
161+
f"成功创建 {len(tools)} 个MoviePilot工具(内置工具: {builtin_tools_count} 个,插件工具: {plugin_tools_count} 个)"
162+
)
152163
else:
153164
logger.info(f"成功创建 {len(tools)} 个MoviePilot工具")
154165
return tools

0 commit comments

Comments
 (0)