-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
refactor: revise LLM message schema and fix the reload logic when using dataclass-based LLM Tool registration #3234
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
Conversation
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.
Pull Request Overview
This PR refactors the agent tool system to improve type safety and modularity by introducing a new message model, restructuring tool abstractions, and consolidating context handling. The changes modernize type annotations using union syntax (| instead of Optional) and relocate message-related classes to a dedicated module.
Key changes include:
- Introduction of
astrbot/core/agent/message.pywith Pydantic-based message models (Message,AssistantMessageSegment,ToolCallMessageSegment, etc.) - Refactored
FunctionToolto useToolSchemabase class with proper JSON schema validation - Created
MCPToolas a specialized subclass ofFunctionToolfor MCP service calls - Moved
tool_call_timeoutfromAstrAgentContexttoContextWrapperfor better encapsulation - Updated all provider sources to handle optional prompts and convert Message objects to dicts
Reviewed Changes
Copilot reviewed 19 out of 19 changed files in this pull request and generated 9 comments.
Show a summary per file
| File | Description |
|---|---|
| astrbot/core/utils/pip_installer.py | Updated type hints to use modern union syntax (str | None) |
| astrbot/core/provider/sources/openai_source.py | Added Message import, made prompt optional, added type guards for tool calls |
| astrbot/core/provider/sources/gemini_source.py | Made prompt optional, added Message conversion logic, removed unreachable code |
| astrbot/core/provider/sources/coze_source.py | Added Message-to-dict conversion before processing contexts |
| astrbot/core/provider/sources/anthropic_source.py | Made prompt optional, added Message conversion logic |
| astrbot/core/provider/provider.py | Updated abstract method signatures to support list[Message] contexts, added conversion helper |
| astrbot/core/provider/func_tool_manager.py | Refactored MCP tool filtering to use isinstance(f, MCPTool) instead of origin field |
| astrbot/core/provider/entities.py | Moved message segment classes to agent.message module, renamed Anthropic Message import to avoid collision |
| astrbot/core/pipeline/process_stage/method/llm_request.py | Refactored tool execution logic to detect overridden call method, moved event to context |
| astrbot/core/pipeline/context_utils.py | Added call_local_llm_tool helper function for executing local tools |
| astrbot/core/pipeline/context.py | Exported call_local_llm_tool from context_utils |
| astrbot/core/conversation_mgr.py | Added add_message_pair method for recording user-assistant conversations |
| astrbot/core/astr_agent_context.py | Moved tool_call_timeout to ContextWrapper, added event field |
| astrbot/core/agent/tool.py | Refactored to use ToolSchema base class, added JSON schema validation, introduced abstract call method |
| astrbot/core/agent/runners/tool_loop_agent_runner.py | Updated to use new message classes, removed redundant event clearing |
| astrbot/core/agent/run_context.py | Moved tool_call_timeout from AstrAgentContext, removed unused AstrMessageEvent import |
| astrbot/core/agent/message.py | New file containing Pydantic message models with ContentPart registry system |
| astrbot/core/agent/mcp_client.py | Added MCPTool class implementing the call method for MCP services |
| astrbot/core/agent/hooks.py | Removed unused @dataclass decorator from BaseAgentRunHooks |
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
LIghtJUNction
left a comment
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.
一些细节修正
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.
Pull Request Overview
Copilot reviewed 19 out of 19 changed files in this pull request and generated 11 comments.
Comments suppressed due to low confidence (2)
astrbot/core/pipeline/process_stage/method/llm_request.py:148
- Corrected spelling of 'deligate' to 'delegate'.
text=f"error when deligate task to {tool.agent.name}",
astrbot/core/pipeline/process_stage/method/llm_request.py:170
- Corrected spelling of 'deligate' to 'delegate'.
text=f"error when deligate task to {tool.agent.name}",
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
…l method return type
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.
Pull Request Overview
Copilot reviewed 20 out of 20 changed files in this pull request and generated 7 comments.
更好地描述 LLM Message 模型。
Inspired by elegant https://github.com/MoonshotAI/kosong
在这个 PR 中,
我们基于 Pydantic 并且参考 MoonshotAI/kosong 引入了新的表示 LLM Message 的数据模型。由于 kosong 需要 Python 版本 >= 3.13,因此我们无法直接以库的形式引入 kosong。
重构了以类的方式注册的 Tool 的 Tool 运行方法。现在我们将重载
call来添加 Tool 的运行方法。相比原来的 run 方法,call 方法将 event 实参改为了
ContextWrapper[AstrAgentContext]类型的 context 方法。优点是更加标准化、独立。并且,我们引入了 Pydantic 进行数据校验,现在将会自动对 parameters 进行数据校验以符合 https://json-schema.org/draft/2020-12 。同时,run 方法我们仍然做了兼容,不过从此开始我们推荐使用 call 方法。修复了以类注册 Tool 后,重载插件的 Bug。
[Bug]插件主动发送的AI消息无法进入对话历史,导致上下文断裂 #3216 为插件开发引入了更好用的添加 LLM 消息上下文到对话历史记录的方法
add_message_pair。可以参考第一个代码片段。为 Astr Agent SDK 做准备。
Compatibility & Breaking Changes / 兼容性与破坏性变更
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.