mirror of
https://github.com/AstrBotDevs/AstrBot
synced 2026-07-16 01:40:15 +08:00
78 lines
2.3 KiB
Python
78 lines
2.3 KiB
Python
from typing import Any
|
|
|
|
from mcp.types import CallToolResult
|
|
|
|
from astrbot.core.agent.hooks import BaseAgentRunHooks
|
|
from astrbot.core.agent.run_context import ContextWrapper
|
|
from astrbot.core.agent.tool import FunctionTool
|
|
from astrbot.core.astr_agent_context import AstrAgentContext
|
|
from astrbot.core.pipeline.context_utils import call_event_hook
|
|
from astrbot.core.star.star_handler import EventType
|
|
|
|
|
|
class MainAgentHooks(BaseAgentRunHooks[AstrAgentContext]):
|
|
async def on_agent_begin(
|
|
self, run_context: ContextWrapper[AstrAgentContext]
|
|
) -> None:
|
|
await call_event_hook(
|
|
run_context.context.event,
|
|
EventType.OnAgentBeginEvent,
|
|
run_context,
|
|
)
|
|
|
|
async def on_agent_done(self, run_context, llm_response) -> None:
|
|
# 执行事件钩子
|
|
if llm_response and llm_response.reasoning_content:
|
|
# we will use this in result_decorate stage to inject reasoning content to chain
|
|
run_context.context.event.set_extra(
|
|
"_llm_reasoning_content", llm_response.reasoning_content
|
|
)
|
|
|
|
await call_event_hook(
|
|
run_context.context.event,
|
|
EventType.OnLLMResponseEvent,
|
|
llm_response,
|
|
)
|
|
await call_event_hook(
|
|
run_context.context.event,
|
|
EventType.OnAgentDoneEvent,
|
|
run_context,
|
|
llm_response,
|
|
)
|
|
|
|
async def on_tool_start(
|
|
self,
|
|
run_context: ContextWrapper[AstrAgentContext],
|
|
tool: FunctionTool[Any],
|
|
tool_args: dict | None,
|
|
) -> None:
|
|
await call_event_hook(
|
|
run_context.context.event,
|
|
EventType.OnUsingLLMToolEvent,
|
|
tool,
|
|
tool_args,
|
|
)
|
|
|
|
async def on_tool_end(
|
|
self,
|
|
run_context: ContextWrapper[AstrAgentContext],
|
|
tool: FunctionTool[Any],
|
|
tool_args: dict | None,
|
|
tool_result: CallToolResult | None,
|
|
) -> None:
|
|
run_context.context.event.clear_result()
|
|
await call_event_hook(
|
|
run_context.context.event,
|
|
EventType.OnLLMToolRespondEvent,
|
|
tool,
|
|
tool_args,
|
|
tool_result,
|
|
)
|
|
|
|
|
|
class EmptyAgentHooks(BaseAgentRunHooks[AstrAgentContext]):
|
|
pass
|
|
|
|
|
|
MAIN_AGENT_HOOKS = MainAgentHooks()
|