Commit 8c7893f0 by sunqi02

feat(agent_hub): 添加IM推理结果和工具调用信息的钩子支持

1 parent e0e7a3e8
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
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!