Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
ai
/
ai-workflow
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit ddb04b2a
authored
May 25, 2026
by
sunqi02
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
subAgent Hook机制实现
1 parent
617ca96a
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
46 additions
and
18 deletions
workspace/agent-admin/backend/app/service/agent_hub.py
workspace/agent-admin/backend/app/service/agent_hub.py
View file @
ddb04b2
...
@@ -25,11 +25,18 @@ async def _im_post_reasoning_hook(
...
@@ -25,11 +25,18 @@ async def _im_post_reasoning_hook(
self
,
kwargs
,
output
,
self
,
kwargs
,
output
,
*
,
im_service
,
account_mapping
,
room_id
,
user_id
,
bot_id_mapping
,
*
,
im_service
,
account_mapping
,
room_id
,
user_id
,
bot_id_mapping
,
):
):
"""AgentScope native hook: 推理完成后通过 IM 发送
推理结果
。"""
"""AgentScope native hook: 推理完成后通过 IM 发送
thinking 文本
。"""
bot_id
=
(
bot_id_mapping
.
get
(
self
.
name
)
or
account_mapping
.
get
(
self
.
name
))
bot_id
=
(
bot_id_mapping
.
get
(
self
.
name
)
or
account_mapping
.
get
(
self
.
name
))
if
not
bot_id
:
if
not
bot_id
:
return
None
return
None
content
=
str
(
output
)
if
output
else
None
# 只提取 ThinkingBlock,跳过 tool_use 等其他 block
content_parts
=
[]
if
output
and
hasattr
(
output
,
"content"
):
blocks
=
output
.
content
if
isinstance
(
output
.
content
,
list
)
else
[]
for
block
in
blocks
:
if
isinstance
(
block
,
dict
)
and
block
.
get
(
"type"
)
==
"thinking"
:
content_parts
.
append
(
block
.
get
(
"thinking"
,
""
))
content
=
"
\n
"
.
join
(
content_parts
)
if
content_parts
else
None
if
not
content
:
if
not
content
:
return
None
return
None
await
im_service
.
send_message
(
await
im_service
.
send_message
(
...
@@ -166,6 +173,31 @@ class AgentHub:
...
@@ -166,6 +173,31 @@ class AgentHub:
agent_tk
.
register_agent_skill
(
skill_dir
)
agent_tk
.
register_agent_skill
(
skill_dir
)
self
.
agent_toolkits
[
agent_code
]
=
agent_tk
self
.
agent_toolkits
[
agent_code
]
=
agent_tk
def
_register_im_hooks
(
self
,
agent
,
room_id
:
str
,
user_id
:
str
,
bot_id_mapping
:
Optional
[
Dict
[
str
,
str
]],
):
"""为 Agent 实例注册 IM 发送 hook。"""
route_ctx
=
dict
(
im_service
=
self
.
im_service
,
account_mapping
=
self
.
agent_to_account
,
room_id
=
room_id
,
user_id
=
user_id
,
bot_id_mapping
=
bot_id_mapping
or
{},
)
agent
.
register_instance_hook
(
"post_reasoning"
,
"im_post_reasoning"
,
partial
(
_im_post_reasoning_hook
,
**
route_ctx
),
)
# pre_acting hook 保留未启用,需要时取消注释
# agent.register_instance_hook(
# "pre_acting", "im_pre_acting",
# partial(_im_pre_acting_hook, **route_ctx),
# )
def
_build_main_prompt
(
def
_build_main_prompt
(
self
,
self
,
context
:
Dict
[
str
,
Any
],
context
:
Dict
[
str
,
Any
],
...
@@ -259,6 +291,11 @@ class AgentHub:
...
@@ -259,6 +291,11 @@ class AgentHub:
"""
"""
start_time
=
time
.
time
()
start_time
=
time
.
time
()
# 暂存路由上下文,供 _call_sub_agent 创建子 Agent 时注册 hook
self
.
_current_route_ctx
=
dict
(
room_id
=
room_id
,
user_id
=
user_id
,
bot_id_mapping
=
bot_id_mapping
or
{},
)
session
=
self
.
session_service
.
get_or_create_session
(
session
=
self
.
session_service
.
get_or_create_session
(
session_id
=
session_id
,
session_id
=
session_id
,
room_id
=
room_id
,
room_id
=
room_id
,
...
@@ -332,22 +369,8 @@ class AgentHub:
...
@@ -332,22 +369,8 @@ class AgentHub:
max_iters
=
10
,
max_iters
=
10
,
)
)
# Register AgentScope native hooks: send IM at post_reasoning & pre_acting
# Register IM hooks for this agent instance
_route_ctx
=
dict
(
self
.
_register_im_hooks
(
agent
,
room_id
,
user_id
,
bot_id_mapping
)
im_service
=
self
.
im_service
,
account_mapping
=
self
.
agent_to_account
,
room_id
=
room_id
,
user_id
=
user_id
,
bot_id_mapping
=
bot_id_mapping
or
{},
)
agent
.
register_instance_hook
(
"post_reasoning"
,
"im_post_reasoning"
,
partial
(
_im_post_reasoning_hook
,
**
_route_ctx
),
)
agent
.
register_instance_hook
(
"pre_acting"
,
"im_pre_acting"
,
partial
(
_im_pre_acting_hook
,
**
_route_ctx
),
)
# Run ReAct loop via framework
# Run ReAct loop via framework
# If there's an image URL, append it to the query so the agent can see it
# If there's an image URL, append it to the query so the agent can see it
...
@@ -409,6 +432,11 @@ class AgentHub:
...
@@ -409,6 +432,11 @@ class AgentHub:
memory
=
sub_memory
,
memory
=
sub_memory
,
max_iters
=
10
,
max_iters
=
10
,
)
)
# 为子 Agent 注册 IM hook
ctx
=
self
.
_current_route_ctx
self
.
_register_im_hooks
(
sub_agent
,
ctx
[
"room_id"
],
ctx
[
"user_id"
],
ctx
[
"bot_id_mapping"
])
response
=
await
sub_agent
.
reply
(
Msg
(
"user"
,
instruction
,
"user"
))
response
=
await
sub_agent
.
reply
(
Msg
(
"user"
,
instruction
,
"user"
))
return
response
.
get_text_content
()
return
response
.
get_text_content
()
...
...
Write
Preview
Markdown
is supported
Attach a file
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to post a comment