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 8c7893f0
authored
May 25, 2026
by
sunqi02
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
feat(agent_hub): 添加IM推理结果和工具调用信息的钩子支持
1 parent
e0e7a3e8
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
54 additions
and
0 deletions
workspace/agent-admin/backend/app/service/agent_hub.py
workspace/agent-admin/backend/app/service/agent_hub.py
View file @
8c7893f
import
logging
import
logging
import
time
import
time
from
functools
import
partial
from
pathlib
import
Path
from
pathlib
import
Path
from
typing
import
Dict
,
List
,
Optional
,
Any
from
typing
import
Dict
,
List
,
Optional
,
Any
from
app.config.loader
import
ConfigLoader
from
app.config.loader
import
ConfigLoader
...
@@ -20,6 +21,41 @@ from agentscope.memory import InMemoryMemory
...
@@ -20,6 +21,41 @@ from agentscope.memory import InMemoryMemory
logger
=
logging
.
getLogger
(
__name__
)
logger
=
logging
.
getLogger
(
__name__
)
async
def
_im_post_reasoning_hook
(
self
,
kwargs
,
output
,
*
,
im_service
,
account_mapping
,
room_id
,
user_id
,
bot_id_mapping
,
):
"""AgentScope native hook: 推理完成后通过 IM 发送推理结果。"""
bot_id
=
(
bot_id_mapping
.
get
(
self
.
name
)
or
account_mapping
.
get
(
self
.
name
))
if
not
bot_id
:
return
None
content
=
str
(
output
)
if
output
else
None
if
not
content
:
return
None
await
im_service
.
send_message
(
bot_id
=
bot_id
,
content
=
content
,
contact_id
=
room_id
,
at_list
=
user_id
,
)
async
def
_im_pre_acting_hook
(
self
,
kwargs
,
*
,
im_service
,
account_mapping
,
room_id
,
user_id
,
bot_id_mapping
,
):
"""AgentScope native hook: 工具调用前通过 IM 发送即将执行的工具信息。"""
bot_id
=
(
bot_id_mapping
.
get
(
self
.
name
)
or
account_mapping
.
get
(
self
.
name
))
if
not
bot_id
:
return
None
tool_call
=
kwargs
.
get
(
"tool_call"
)
if
not
tool_call
:
return
None
content
=
str
(
tool_call
)
await
im_service
.
send_message
(
bot_id
=
bot_id
,
content
=
content
,
contact_id
=
room_id
,
at_list
=
user_id
,
)
class
AgentHubError
(
Exception
):
class
AgentHubError
(
Exception
):
"""Raised when AgentHub processing fails."""
"""Raised when AgentHub processing fails."""
pass
pass
...
@@ -44,6 +80,7 @@ class AgentHub:
...
@@ -44,6 +80,7 @@ class AgentHub:
):
):
self
.
config_loader
=
config_loader
self
.
config_loader
=
config_loader
self
.
session_service
=
session_service
self
.
session_service
=
session_service
self
.
im_service
=
im_service
self
.
agents
=
config_loader
.
load_agents
()
self
.
agents
=
config_loader
.
load_agents
()
self
.
main_model_config
=
config_loader
.
load_models
()[
"main"
]
self
.
main_model_config
=
config_loader
.
load_models
()[
"main"
]
...
@@ -295,6 +332,23 @@ class AgentHub:
...
@@ -295,6 +332,23 @@ class AgentHub:
max_iters
=
10
,
max_iters
=
10
,
)
)
# Register AgentScope native hooks: send IM at post_reasoning & pre_acting
_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
),
)
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
effective_query
=
user_query
effective_query
=
user_query
...
...
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