mirror of
https://github.com/yuruotong1/autoMate.git
synced 2026-03-22 13:07:17 +08:00
更新 edit 页面的加载逻辑
This commit is contained in:
@@ -18,21 +18,14 @@ class ActionBase(BaseModel):
|
||||
def __init__(self, **data: Any):
|
||||
super().__init__(**data)
|
||||
self.__ui_name_and_line_edit = {}
|
||||
self.__config_ui = QtUtil.load_ui("config_page.ui")
|
||||
self.__config_ui = QtUtil.load_ui("action_config_page.ui")
|
||||
|
||||
|
||||
def run(self, *args, **kwargs):
|
||||
raise TypeError("Not realize run function")
|
||||
|
||||
def run_with_out_arg(self):
|
||||
return self.run(**self.action_arg)
|
||||
|
||||
def convert_langchain_tool(self):
|
||||
return StructuredTool.from_function(
|
||||
func=self.run,
|
||||
name=self.name,
|
||||
description=self.description,
|
||||
args_schema=self.args
|
||||
)
|
||||
return self.run(**self.args.model_dump())
|
||||
|
||||
def _run(self, *args, **kwargs):
|
||||
self.run(*args, **kwargs)
|
||||
@@ -63,10 +56,11 @@ class ActionBase(BaseModel):
|
||||
self.__config_ui.hide()
|
||||
|
||||
def __save_button_clicked(self, the_insert_row):
|
||||
args = {}
|
||||
from pages.edit_page import GlobalUtil
|
||||
arg = {}
|
||||
for arg_name in self.__ui_name_and_line_edit:
|
||||
args[arg_name] = self.__ui_name_and_line_edit[arg_name].text()
|
||||
arg[arg_name] = self.__ui_name_and_line_edit[arg_name].text()
|
||||
self.args= self.args.model_validate(arg)
|
||||
self.action_pos = the_insert_row
|
||||
self.action_level = 0
|
||||
# 向新位置增加元素
|
||||
|
||||
@@ -15,4 +15,5 @@ class OpenApplicationAction(ActionBase):
|
||||
args: OpenApplicationInput
|
||||
|
||||
def run(self, path):
|
||||
print("运行了这里", path)
|
||||
subprocess.Popen(path)
|
||||
|
||||
@@ -5,7 +5,7 @@ from utils.selenium_util import SeleniumUtil
|
||||
|
||||
|
||||
class SearchInput(BaseModel):
|
||||
key: str = Field(description="要查询的关键词", default="")
|
||||
key: str = Field(description="要查询的内容", title="要查询的内容", default="")
|
||||
|
||||
|
||||
# 利用搜索引擎搜索关键词
|
||||
|
||||
@@ -10,16 +10,6 @@ class WorkerAgent:
|
||||
def get_executor(self):
|
||||
llm = LLM_Util().llm()
|
||||
tools = ToolsUtil.get_tools()
|
||||
# prompt = PromptTemplate(
|
||||
# template="{instructions}\n\nTOOLS:\n------\n\n你可以使用以下工具:\n\n{tools}\n\n使用工具时,请使用以下格式:\n\n"
|
||||
# "```\nThought: 我需要使用工具吗? 需要\nAction: {tool_names}\n"
|
||||
# "Action Input: Action的输入'\n'Observation: 运行Action的结果\n```"
|
||||
# "\n\n当输出内容时,或者不需要使用工具,必须使用以下格式: "
|
||||
# "\n\n```\nThought: 我需要使用工具吗? 不需要\nFinal Answer: [你的回复]\n```\n\n开始!\n\n"
|
||||
# "Previous conversation history:\n{chat_history}\n\nNew input: {input}\n{agent_scratchpad}",
|
||||
# input_variables=['agent_scratchpad', 'input', 'tool_names', 'tools'],
|
||||
# partial_variables={'chat_history': '', 'instructions': ""}
|
||||
# )
|
||||
prompt = hub.pull("langchain-ai/react-agent-template")
|
||||
prompt.partial(instructions="")
|
||||
agent = create_react_agent(llm=llm, tools=tools, prompt=prompt)
|
||||
|
||||
@@ -21,4 +21,4 @@ components:
|
||||
browser:
|
||||
description: 配置本电脑的浏览器信息,用于浏览器自动化
|
||||
config:
|
||||
brow: edge
|
||||
浏览器类型: edge
|
||||
4
main.py
4
main.py
@@ -5,6 +5,7 @@ import traceback
|
||||
|
||||
import leancloud
|
||||
from PyQt6.QtWidgets import QApplication
|
||||
from actions import open_application_action
|
||||
|
||||
from pages.chat_page import ChatPage
|
||||
from pages.edit_page import GlobalUtil, EditPage
|
||||
@@ -48,6 +49,7 @@ def excepthook(exc_type, exc_value, exc_tb):
|
||||
def load():
|
||||
# 生成 config.yaml 文件
|
||||
Config()
|
||||
EditPage.global_load()
|
||||
|
||||
|
||||
|
||||
@@ -56,7 +58,7 @@ if __name__ == "__main__":
|
||||
sys.excepthook = excepthook
|
||||
app = QApplication(sys.argv)
|
||||
load()
|
||||
# page = FuncListPage()
|
||||
page = ChatPage()
|
||||
page.show()
|
||||
sys.exit(app.exec())
|
||||
|
||||
|
||||
121
pages/action_config_page.ui
Normal file
121
pages/action_config_page.ui
Normal file
@@ -0,0 +1,121 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<ui version="4.0">
|
||||
<class>MainWindow</class>
|
||||
<widget class="QMainWindow" name="MainWindow">
|
||||
<property name="geometry">
|
||||
<rect>
|
||||
<x>0</x>
|
||||
<y>0</y>
|
||||
<width>586</width>
|
||||
<height>447</height>
|
||||
</rect>
|
||||
</property>
|
||||
<property name="windowTitle">
|
||||
<string>MainWindow</string>
|
||||
</property>
|
||||
<widget class="QWidget" name="centralwidget">
|
||||
<widget class="QWidget" name="verticalLayoutWidget">
|
||||
<property name="geometry">
|
||||
<rect>
|
||||
<x>20</x>
|
||||
<y>60</y>
|
||||
<width>531</width>
|
||||
<height>261</height>
|
||||
</rect>
|
||||
</property>
|
||||
<layout class="QVBoxLayout" name="config_list"/>
|
||||
</widget>
|
||||
<widget class="QScrollBar" name="verticalScrollBar">
|
||||
<property name="geometry">
|
||||
<rect>
|
||||
<x>560</x>
|
||||
<y>40</y>
|
||||
<width>20</width>
|
||||
<height>321</height>
|
||||
</rect>
|
||||
</property>
|
||||
<property name="orientation">
|
||||
<enum>Qt::Vertical</enum>
|
||||
</property>
|
||||
</widget>
|
||||
<widget class="QPushButton" name="saveButton">
|
||||
<property name="geometry">
|
||||
<rect>
|
||||
<x>460</x>
|
||||
<y>380</y>
|
||||
<width>51</width>
|
||||
<height>21</height>
|
||||
</rect>
|
||||
</property>
|
||||
<property name="text">
|
||||
<string>保存</string>
|
||||
</property>
|
||||
</widget>
|
||||
<widget class="QPushButton" name="cancelButton">
|
||||
<property name="geometry">
|
||||
<rect>
|
||||
<x>520</x>
|
||||
<y>380</y>
|
||||
<width>51</width>
|
||||
<height>21</height>
|
||||
</rect>
|
||||
</property>
|
||||
<property name="text">
|
||||
<string>取消</string>
|
||||
</property>
|
||||
</widget>
|
||||
<widget class="QLabel" name="label">
|
||||
<property name="geometry">
|
||||
<rect>
|
||||
<x>10</x>
|
||||
<y>30</y>
|
||||
<width>54</width>
|
||||
<height>12</height>
|
||||
</rect>
|
||||
</property>
|
||||
<property name="text">
|
||||
<string>配置</string>
|
||||
</property>
|
||||
</widget>
|
||||
<widget class="QLabel" name="label_2">
|
||||
<property name="geometry">
|
||||
<rect>
|
||||
<x>0</x>
|
||||
<y>0</y>
|
||||
<width>54</width>
|
||||
<height>12</height>
|
||||
</rect>
|
||||
</property>
|
||||
<property name="text">
|
||||
<string>description</string>
|
||||
</property>
|
||||
</widget>
|
||||
<widget class="Line" name="line">
|
||||
<property name="geometry">
|
||||
<rect>
|
||||
<x>0</x>
|
||||
<y>10</y>
|
||||
<width>581</width>
|
||||
<height>16</height>
|
||||
</rect>
|
||||
</property>
|
||||
<property name="orientation">
|
||||
<enum>Qt::Horizontal</enum>
|
||||
</property>
|
||||
</widget>
|
||||
</widget>
|
||||
<widget class="QMenuBar" name="menubar">
|
||||
<property name="geometry">
|
||||
<rect>
|
||||
<x>0</x>
|
||||
<y>0</y>
|
||||
<width>586</width>
|
||||
<height>22</height>
|
||||
</rect>
|
||||
</property>
|
||||
</widget>
|
||||
<widget class="QStatusBar" name="statusbar"/>
|
||||
</widget>
|
||||
<resources/>
|
||||
<connections/>
|
||||
</ui>
|
||||
@@ -6,7 +6,7 @@ from PyQt6.QtWidgets import QAbstractItemView, QListWidget, QListWidgetItem
|
||||
|
||||
from actions.action_util import ActionUtil
|
||||
from pages.edit_action_list_view import ActionListItem
|
||||
from pages.global_util import GlobalUtil
|
||||
from utils.global_util import GlobalUtil
|
||||
|
||||
|
||||
class FunctionListView(QListWidget):
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
from pages.edit_action_list_view import ActionList
|
||||
from pages.edit_function_page import FunctionListView
|
||||
from pages.global_util import GlobalUtil
|
||||
from utils.global_util import GlobalUtil
|
||||
from utils.qt_util import QtUtil
|
||||
from PyQt6.QtWidgets import QMainWindow
|
||||
from PyQt6.QtCore import pyqtSignal
|
||||
|
||||
|
||||
|
||||
@@ -10,24 +11,26 @@ interface_ui = QtUtil.load_ui_type("edit_page.ui")
|
||||
|
||||
|
||||
class EditPage(QMainWindow, interface_ui):
|
||||
def __init__(self, parent_page, func_status, func_list_pos_row, func_list_pos_column, action_list: ActionList = None):
|
||||
# 页面关闭的信号
|
||||
page_closed = pyqtSignal(str)
|
||||
|
||||
def __init__(self, func_status, func_list_pos_row, func_list_pos_column, action_list: ActionList = None, func_name="默认名称", func_description="无"):
|
||||
self.func_list_pos_column = func_list_pos_column
|
||||
self.func_list_pos_row = func_list_pos_row
|
||||
# 属于通用还是专属
|
||||
self.func_status = func_status
|
||||
self.func_description = ""
|
||||
self.func_description = func_description
|
||||
# 在func上的名称
|
||||
self.func_name = "默认名称"
|
||||
self.func_name = func_name
|
||||
if not action_list:
|
||||
action_list = ActionList()
|
||||
self.action_list = action_list
|
||||
self.parent_page = parent_page
|
||||
super().__init__()
|
||||
self.setupUi(self)
|
||||
self.setup_up()
|
||||
|
||||
def closeEvent(self, event):
|
||||
self.parent_page.show()
|
||||
self.page_closed.emit(self.func_name)
|
||||
|
||||
def dump(self):
|
||||
return {"func_list_pos_column": self.func_list_pos_column,
|
||||
@@ -44,7 +47,7 @@ class EditPage(QMainWindow, interface_ui):
|
||||
function_list_view = FunctionListView()
|
||||
self.function_list_layout.addWidget(function_list_view)
|
||||
self.action_list_view_layout.addWidget(self.action_list)
|
||||
self.run_button.clicked.connect(self.__run_button_click)
|
||||
self.run_button.clicked.connect(self.run_action)
|
||||
self.save_button.clicked.connect(self.__save_button_click)
|
||||
self.cancel_button.clicked.connect(self.__cancel_button_click)
|
||||
# 设置间距
|
||||
@@ -65,10 +68,11 @@ class EditPage(QMainWindow, interface_ui):
|
||||
self.close()
|
||||
|
||||
|
||||
def __run_button_click(self):
|
||||
def run_action(self, *args, **kwargs):
|
||||
print("run_action", args, kwargs)
|
||||
for index in range(self.action_list.count()):
|
||||
func = self.action_list.item(index)
|
||||
res = func.__getattribute__("get_action")().run_with_out_arg()
|
||||
res = func.action.run_with_out_arg()
|
||||
print("执行结果:", res)
|
||||
|
||||
|
||||
@@ -81,7 +85,7 @@ class EditPage(QMainWindow, interface_ui):
|
||||
|
||||
|
||||
@staticmethod
|
||||
def global_load( parent):
|
||||
def global_load():
|
||||
# 根据配置文件的配置,从本地文件中或者网上读取
|
||||
edit_pages_json = GlobalUtil.read_from_local()
|
||||
for edit_page_json in edit_pages_json:
|
||||
@@ -91,9 +95,9 @@ class EditPage(QMainWindow, interface_ui):
|
||||
func_status=edit_page_json["func_status"],
|
||||
func_list_pos_row=edit_page_json["func_list_pos_row"],
|
||||
func_list_pos_column=edit_page_json["func_list_pos_column"],
|
||||
# TODO待优化加载问题
|
||||
action_list=ActionList.load(edit_page_json["action_list"]),
|
||||
parent_page=parent
|
||||
func_name = edit_page_json["func_name"],
|
||||
func_description = edit_page_json["func_description"],
|
||||
action_list=ActionList.load(edit_page_json["action_list"])
|
||||
)
|
||||
edit_page.func_name = edit_page_json["func_name"]
|
||||
edit_page.func_description = edit_page_json["func_description"]
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import typing
|
||||
from PyQt6 import QtCore
|
||||
from PyQt6 import QtCore, QtGui
|
||||
from PyQt6.QtCore import QSize, Qt
|
||||
from PyQt6.QtGui import QIcon
|
||||
from PyQt6.QtWidgets import QGraphicsOpacityEffect, QToolButton, QMainWindow, QWidget
|
||||
@@ -37,10 +37,12 @@ class AddFuncButton(QToolButton):
|
||||
def click(self):
|
||||
self.func_list_page.hide()
|
||||
if not self.edit_page:
|
||||
edit_page = EditPage(self.func_list_page, self.func_status, self.func_list_pos_row, self.func_list_pos_column)
|
||||
edit_page = EditPage(self.func_status, self.func_list_pos_row, self.func_list_pos_column)
|
||||
GlobalUtil.current_page = edit_page
|
||||
else:
|
||||
GlobalUtil.current_page = self.edit_page
|
||||
# 接收信号
|
||||
GlobalUtil.current_page.page_closed.connect(lambda: self.func_list_page.show())
|
||||
GlobalUtil.current_page.show()
|
||||
|
||||
def enterEvent(self, event):
|
||||
@@ -51,25 +53,24 @@ class AddFuncButton(QToolButton):
|
||||
if not self.edit_page:
|
||||
self.opacity_effect.setOpacity(0)
|
||||
|
||||
|
||||
|
||||
|
||||
interface_ui = QtUtil.load_ui_type("func_list_page.ui")
|
||||
|
||||
|
||||
class FuncListPage(QMainWindow, interface_ui):
|
||||
def __init__(self, parent):
|
||||
self.parent_ui = parent
|
||||
super().__init__()
|
||||
self.setupUi(self)
|
||||
self.setup_up()
|
||||
# self.setup_up()
|
||||
|
||||
# 关闭事件
|
||||
def closeEvent(self, event):
|
||||
self.parent_ui.show()
|
||||
|
||||
def showEvent(self, a0) -> None:
|
||||
self.setup_up()
|
||||
return super().showEvent(a0)
|
||||
|
||||
|
||||
def setup_up(self):
|
||||
EditPage.global_load(self)
|
||||
# self.ui = QtUtil.load_ui("func_list_page.ui")
|
||||
# 四行三列,辅满通用应用列表布局
|
||||
for i in range(3): # 3行
|
||||
|
||||
@@ -2,9 +2,23 @@ from tools.FindDesktopPath import FindDesktopPath
|
||||
from tools.ListAllFile import ListAllFile
|
||||
from tools.ListDesktopFiles import ListDesktopFiles
|
||||
from tools.OpenApplication import OpenApplicationAction
|
||||
from langchain.tools import StructuredTool
|
||||
from utils.global_util import GlobalUtil
|
||||
|
||||
|
||||
class ToolsUtil:
|
||||
@staticmethod
|
||||
def get_tools():
|
||||
return [OpenApplicationAction(), FindDesktopPath(), ListAllFile(), ListDesktopFiles()]
|
||||
print("get_tools")
|
||||
action_tools = []
|
||||
# 从 function_list 中生成工具
|
||||
for edit_page in GlobalUtil.edit_page_global:
|
||||
# 动态生成 langchain 工具
|
||||
langchain_tools = StructuredTool.from_function(
|
||||
func=edit_page.run_action,
|
||||
name=edit_page.func_name,
|
||||
description=edit_page.func_description,
|
||||
return_direct=True)
|
||||
action_tools.append(langchain_tools)
|
||||
print(action_tools)
|
||||
return [OpenApplicationAction(), FindDesktopPath(), ListAllFile(), ListDesktopFiles()] + action_tools
|
||||
|
||||
Reference in New Issue
Block a user