-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
feat: add event hooks for llm tool usage and response handling #4516
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+82
−0
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Contributor
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
你好,我发现了 1 个问题,并给出了一些整体层面的反馈:
- 新增 docstring 中的装饰器示例使用的是
@on_using_llm_tool()/@on_llm_tool_respond(),但实际导出的装饰器名称是register_on_using_llm_tool/register_on_llm_tool_respond;建议统一命名,或者添加别名,这样示例代码就能与用户实际需要导入和调用的内容保持一致。 - 你新增了
EventType.OnCallingFuncToolEvent和EventType.OnAfterCallingFuncToolEvent两个事件;建议在枚举的注释(或命名)中进一步明确哪个是“前(before)”、哪个是“后(after)”,以减少未来 Hook 实现者的歧义。
给 AI 代理的提示词
Please address the comments from this code review:
## Overall Comments
- The decorator examples in the new docstrings use `@on_using_llm_tool()` / `@on_llm_tool_respond()`, but the actual exported decorator names are `register_on_using_llm_tool` / `register_on_llm_tool_respond`; consider aligning the naming or adding aliases so the example code matches what users should actually import and call.
- You’ve added both `EventType.OnCallingFuncToolEvent` and `EventType.OnAfterCallingFuncToolEvent`; it may be worth clarifying in the enum comments (or names) which is explicitly "before" vs "after" to reduce ambiguity for future hook implementers.
## Individual Comments
### Comment 1
<location> `astrbot/core/star/register/star_handler.py:420` </location>
<code_context>
+ ```py
+ from astrbot.core.agent.tool import FunctionTool
+
+ @on_using_llm_tool()
+ async def test(self, event: AstrMessageEvent, tool: FunctionTool, tool_args: dict | None) -> None:
+ ...
</code_context>
<issue_to_address>
**issue (typo):** Example decorator name in the docstring likely should be `@on_llm_tool_respond()` instead of `@on_using_llm_tool()`.
In the `register_on_llm_tool_respond` docstring, this example still uses `@on_using_llm_tool()`, likely copied from the earlier function. Updating it to `@on_llm_tool_respond()` would better match this function’s name and behavior and avoid confusing users.
Suggested implementation:
```python
def register_on_llm_tool_respond(**kwargs):
"""当函数工具调用结束,准备将函数工具的结果加入消息流中时的事件。
Examples:
```py
from astrbot.core.agent.tool import FunctionTool
@on_llm_tool_respond()
async def test(self, event: AstrMessageEvent, tool: FunctionTool, tool_args: dict | None) -> None:
...
```
请务必接收三个参数:event, tool, tool_args
"""
def decorator(awaitable):
_ = get_handler_or_create(awaitable, EventType.OnFuncToolRespondEvent, **kwargs)
return awaitable
```
If the existing `register_on_llm_tool_respond` docstring text differs (e.g., different Chinese description or parameter list), adjust the `SEARCH` block to match the current docstring exactly and only replace the decorator line in the example from `@on_using_llm_tool()` to `@on_llm_tool_respond()`. The `register_on_using_llm_tool` example in the snippet you shared is already correct and does not need to be changed.
</issue_to_address>帮我变得更有用!请在每条评论上点 👍 或 👎,我会根据你的反馈改进后续的 Review。
Original comment in English
Hey - I've found 1 issue, and left some high level feedback:
- The decorator examples in the new docstrings use
@on_using_llm_tool()/@on_llm_tool_respond(), but the actual exported decorator names areregister_on_using_llm_tool/register_on_llm_tool_respond; consider aligning the naming or adding aliases so the example code matches what users should actually import and call. - You’ve added both
EventType.OnCallingFuncToolEventandEventType.OnAfterCallingFuncToolEvent; it may be worth clarifying in the enum comments (or names) which is explicitly "before" vs "after" to reduce ambiguity for future hook implementers.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- The decorator examples in the new docstrings use `@on_using_llm_tool()` / `@on_llm_tool_respond()`, but the actual exported decorator names are `register_on_using_llm_tool` / `register_on_llm_tool_respond`; consider aligning the naming or adding aliases so the example code matches what users should actually import and call.
- You’ve added both `EventType.OnCallingFuncToolEvent` and `EventType.OnAfterCallingFuncToolEvent`; it may be worth clarifying in the enum comments (or names) which is explicitly "before" vs "after" to reduce ambiguity for future hook implementers.
## Individual Comments
### Comment 1
<location> `astrbot/core/star/register/star_handler.py:420` </location>
<code_context>
+ ```py
+ from astrbot.core.agent.tool import FunctionTool
+
+ @on_using_llm_tool()
+ async def test(self, event: AstrMessageEvent, tool: FunctionTool, tool_args: dict | None) -> None:
+ ...
</code_context>
<issue_to_address>
**issue (typo):** Example decorator name in the docstring likely should be `@on_llm_tool_respond()` instead of `@on_using_llm_tool()`.
In the `register_on_llm_tool_respond` docstring, this example still uses `@on_using_llm_tool()`, likely copied from the earlier function. Updating it to `@on_llm_tool_respond()` would better match this function’s name and behavior and avoid confusing users.
Suggested implementation:
```python
def register_on_llm_tool_respond(**kwargs):
"""当函数工具调用结束,准备将函数工具的结果加入消息流中时的事件。
Examples:
```py
from astrbot.core.agent.tool import FunctionTool
@on_llm_tool_respond()
async def test(self, event: AstrMessageEvent, tool: FunctionTool, tool_args: dict | None) -> None:
...
```
请务必接收三个参数:event, tool, tool_args
"""
def decorator(awaitable):
_ = get_handler_or_create(awaitable, EventType.OnFuncToolRespondEvent, **kwargs)
return awaitable
```
If the existing `register_on_llm_tool_respond` docstring text differs (e.g., different Chinese description or parameter list), adjust the `SEARCH` block to match the current docstring exactly and only replace the decorator line in the example from `@on_using_llm_tool()` to `@on_llm_tool_respond()`. The `register_on_using_llm_tool` example in the snippet you shared is already correct and does not need to be changed.
</issue_to_address>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Modifications / 改动点
Screenshots or Test Results / 运行截图或测试结果
Checklist / 检查清单
requirements.txt和pyproject.toml文件相应位置。/ I have ensured that no new dependencies are introduced, OR if new dependencies are introduced, they have been added to the appropriate locations inrequirements.txtandpyproject.toml.由 Sourcery 提供的摘要
在 LLM 工具调用前后新增事件钩子,并将其接入到代理的钩子管线中。
新功能:
增强内容:
Original summary in English
Summary by Sourcery
Add new event hooks around LLM tool invocation and wire them into the agent hook pipeline.
New Features:
Enhancements: