From ebd232ec8e1c045ae22499d42b4b68498aedff9a Mon Sep 17 00:00:00 2001 From: whatevertogo <149563971+whatevertogo@users.noreply.github.com> Date: Tue, 17 Mar 2026 16:07:30 +0800 Subject: [PATCH] fix: register_agent decorator NameError (#5765) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix: 修改 register_agent 以避免运行时导入 AstrAgentContext * test: improve register_agent test robustness - Add fixture for llm_tools cleanup to avoid test interference - Use multiple import patterns to make guard more robust to refactors - Add assertion to verify decorated coroutine is wired as handoff handler Co-Authored-By: Claude Opus 4.6 * 删除测试文件: 移除 register_agent 装饰器的运行时行为测试 --------- Co-authored-by: whatevertogo Co-authored-by: Claude Opus 4.6 Co-authored-by: Soulter <905617992@qq.com> --- astrbot/core/star/register/star_handler.py | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/astrbot/core/star/register/star_handler.py b/astrbot/core/star/register/star_handler.py index 735bd3852..1385b5056 100644 --- a/astrbot/core/star/register/star_handler.py +++ b/astrbot/core/star/register/star_handler.py @@ -2,7 +2,7 @@ from __future__ import annotations import re from collections.abc import AsyncGenerator, Awaitable, Callable -from typing import TYPE_CHECKING, Any +from typing import Any import docstring_parser @@ -15,9 +15,6 @@ from astrbot.core.message.message_event_result import MessageEventResult from astrbot.core.provider.func_tool_manager import PY_TO_JSON_TYPE, SUPPORTED_TYPES from astrbot.core.provider.register import llm_tools -if TYPE_CHECKING: - from astrbot.core.astr_agent_context import AstrAgentContext - from ..filter.command import CommandFilter from ..filter.command_group import CommandGroupFilter from ..filter.custom_filter import CustomFilterAnd, CustomFilterOr @@ -619,7 +616,7 @@ class RegisteringAgent: kwargs["registering_agent"] = self return register_llm_tool(*args, **kwargs) - def __init__(self, agent: Agent[AstrAgentContext]) -> None: + def __init__(self, agent: Agent[Any]) -> None: self._agent = agent @@ -627,7 +624,7 @@ def register_agent( name: str, instruction: str, tools: list[str | FunctionTool] | None = None, - run_hooks: BaseAgentRunHooks[AstrAgentContext] | None = None, + run_hooks: BaseAgentRunHooks[Any] | None = None, ): """注册一个 Agent @@ -641,12 +638,12 @@ def register_agent( tools_ = tools or [] def decorator(awaitable: Callable[..., Awaitable[Any]]): - AstrAgent = Agent[AstrAgentContext] + AstrAgent = Agent[Any] agent = AstrAgent( name=name, instructions=instruction, tools=tools_, - run_hooks=run_hooks or BaseAgentRunHooks[AstrAgentContext](), + run_hooks=run_hooks or BaseAgentRunHooks[Any](), ) handoff_tool = HandoffTool(agent=agent) handoff_tool.handler = awaitable