Commit ddb04b2a by sunqi02

subAgent Hook机制实现

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