Skip to content

Commit 126a56c

Browse files
committed
feat: 支持 maafw 4.0 Agent 功能
1 parent f26f036 commit 126a56c

7 files changed

Lines changed: 103 additions & 2 deletions

File tree

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -446,3 +446,6 @@ install
446446

447447
# Tools
448448
tools/ImageCropper/**/*.png
449+
450+
config
451+
debug

agent/main.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import sys
2+
3+
from maa.agent.agent_server import AgentServer
4+
from maa.toolkit import Toolkit
5+
6+
import my_action
7+
import my_reco
8+
9+
10+
def main():
11+
Toolkit.init_option("./")
12+
13+
socket_id = sys.argv[-1]
14+
15+
AgentServer.start_up(socket_id)
16+
AgentServer.join()
17+
AgentServer.shut_down()
18+
19+
20+
if __name__ == "__main__":
21+
main()

agent/my_action.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from maa.agent.agent_server import AgentServer
2+
from maa.custom_action import CustomAction
3+
from maa.context import Context
4+
5+
6+
@AgentServer.custom_action("my_action_111")
7+
class MyCustomAction(CustomAction):
8+
9+
def run(
10+
self,
11+
context: Context,
12+
argv: CustomAction.RunArg,
13+
) -> bool:
14+
15+
print("my_action_111 is running!")
16+
17+
return True

agent/my_reco.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
from maa.agent.agent_server import AgentServer
2+
from maa.custom_recognition import CustomRecognition
3+
from maa.context import Context
4+
5+
6+
@AgentServer.custom_recognition("my_reco_222")
7+
class MyRecongition(CustomRecognition):
8+
9+
def analyze(
10+
self,
11+
context: Context,
12+
argv: CustomRecognition.AnalyzeArg,
13+
) -> CustomRecognition.AnalyzeResult:
14+
15+
reco_detail = context.run_recognition(
16+
"MyCustomOCR",
17+
argv.image,
18+
pipeline_override={"MyCustomOCR": {"roi": [100, 100, 200, 300]}},
19+
)
20+
21+
# context is a reference, will override the pipeline for whole task
22+
context.override_pipeline({"MyCustomOCR": {"roi": [1, 1, 114, 514]}})
23+
# context.run_recognition ...
24+
25+
# make a new context to override the pipeline, only for itself
26+
new_context = context.clone()
27+
new_context.override_pipeline({"MyCustomOCR": {"roi": [100, 200, 300, 400]}})
28+
reco_detail = new_context.run_recognition("MyCustomOCR", argv.image)
29+
30+
click_job = context.tasker.controller.post_click(10, 20)
31+
click_job.wait()
32+
33+
context.override_next(argv.node_name, ["TaskA", "TaskB"])
34+
35+
return CustomRecognition.AnalyzeResult(
36+
box=(0, 0, 100, 100), detail="Hello World!"
37+
)

assets/interface.json

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@
2828
]
2929
}
3030
],
31+
"agent": {
32+
"child_exec": "python",
33+
"child_args": [
34+
"{PROJECT_DIR}/agent/main.py"
35+
]
36+
},
3137
"task": [
3238
{
3339
"name": "普通任务",
@@ -47,6 +53,10 @@
4753
"任务选项1"
4854
],
4955
"pipeline_override": {}
56+
},
57+
{
58+
"name": "带Custom的任务",
59+
"entry": "MyTask4"
5060
}
5161
],
5262
"option": {
@@ -71,4 +81,4 @@
7181
]
7282
}
7383
}
74-
}
84+
}
Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
{
22
"MyTask1": {},
33
"MyTask2": {},
4-
"MyTask3": {}
4+
"MyTask3": {},
5+
"MyTask4": {
6+
"recognition": "Custom",
7+
"custom_recognition": "my_reco_222",
8+
"action": "Custom",
9+
"custom_action": "my_action_111"
10+
}
511
}

install.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,17 @@ def install_chores():
6969
install_path,
7070
)
7171

72+
def install_agent():
73+
shutil.copytree(
74+
working_dir / "agent",
75+
install_path / "agent",
76+
dirs_exist_ok=True,
77+
)
7278

7379
if __name__ == "__main__":
7480
install_deps()
7581
install_resource()
7682
install_chores()
83+
install_agent()
7784

7885
print(f"Install to {install_path} successfully.")

0 commit comments

Comments
 (0)