-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
feat: 为 @llm_tool 装饰器增加 permission_type 权限声明支持 #9069
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
Open
lingyun14beta
wants to merge
9
commits into
AstrBotDevs:master
Choose a base branch
from
lingyun14beta:dev-tool
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
31f07e2
feat: support permission_type declaration on @llm_tool decorator (#8947)
lingyun14beta 2a0082a
docs: document permission_type for @llm_tool (#8947)
lingyun14beta 06b296c
docs: fix incorrect claim about @dataclass tools not supporting permi…
lingyun14beta f7278e8
refactor: address code review feedback on permission fallback logic
lingyun14beta c2c2c67
fix: use getattr to avoid AttributeError on non-FunctionTool tool obj…
lingyun14beta de8d4b8
fix: raise ValueError instead of silently dropping permission_type on…
lingyun14beta 3efbea3
fix: make permission_type keyword-only, guard against positional Perm…
lingyun14beta d7dfbc4
test: add coverage for unexpected declared_permission_type values
lingyun14beta 3dae03e
refactor: extract permission validation helper, share tool lookup bet…
lingyun14beta File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
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
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
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -117,6 +117,51 @@ async def get_weather(self, event: AstrMessageEvent, location: str) -> MessageEv | |||||
| > | ||||||
| > 此外,装饰器**不支持**通过 `parameters=...` 显式传入参数 schema,该写法会被忽略。如需手动控制 schema,请使用上方的 `@dataclass` + `add_llm_tools()` 方式。 | ||||||
|
|
||||||
| ### 为 Tool 声明默认权限 | ||||||
|
|
||||||
| > [!TIP] | ||||||
| > 在 v4.X.X 时加入 | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion (typo): Consider a more idiomatic phrasing for the version note. The wording "在 v4.X.X 时加入" is slightly awkward for written technical docs. Prefer a more standard phrasing such as "在 v4.X.X 中加入" or "在 v4.X.X 中添加".
Suggested change
|
||||||
|
|
||||||
| `@filter.command()` 可以通过 `@filter.permission_type(filter.PermissionType.ADMIN)` 限制指令仅管理员可用,`@filter.llm_tool()` 也支持类似的写法,通过 `permission_type` 参数为工具声明一个默认权限: | ||||||
|
|
||||||
| ```py | ||||||
| from astrbot.api.event import filter, AstrMessageEvent | ||||||
|
|
||||||
| @filter.llm_tool(name="restart_server", permission_type=filter.PermissionType.ADMIN) | ||||||
| async def restart_server(self, event: AstrMessageEvent): | ||||||
| '''重启服务器。''' | ||||||
| # 处理逻辑 | ||||||
| ``` | ||||||
|
|
||||||
| `permission_type` 可选 `filter.PermissionType.ADMIN` 或 `filter.PermissionType.MEMBER`,不传则保持原有行为(所有人可用)。 | ||||||
|
|
||||||
| 如果你是通过 `@dataclass` + `FunctionTool` 的方式定义 Tool(见上方[定义 Tool](#定义-tool)一节),也可以用同样的方式声明默认权限,只需要在 dataclass 里加上 `declared_permission_type` 字段: | ||||||
|
|
||||||
| ```py | ||||||
| from pydantic import Field | ||||||
| from pydantic.dataclasses import dataclass | ||||||
|
|
||||||
| from astrbot.core.agent.run_context import ContextWrapper | ||||||
| from astrbot.core.agent.tool import FunctionTool, ToolExecResult | ||||||
| from astrbot.core.astr_agent_context import AstrAgentContext | ||||||
|
|
||||||
|
|
||||||
| @dataclass | ||||||
| class RestartServerTool(FunctionTool[AstrAgentContext]): | ||||||
| name: str = "restart_server" | ||||||
| description: str = "Restart the server." | ||||||
| parameters: dict = Field(default_factory=lambda: {"type": "object", "properties": {}}) | ||||||
| declared_permission_type: str | None = "admin" # 可选 "admin" / "member" / None | ||||||
|
|
||||||
| async def call(self, context: ContextWrapper[AstrAgentContext], **kwargs) -> ToolExecResult: | ||||||
| # 处理逻辑 | ||||||
| return "ok" | ||||||
| ``` | ||||||
|
|
||||||
| > [!WARNING] | ||||||
| > - `permission_type` / `declared_permission_type` 只是工具的**默认权限**。如果机器人主人在 WebUI 面板(扩展 -> 组件 -> 工具管理)里为该工具手动配置过权限,面板上的配置会**覆盖**插件代码里声明的默认值。 | ||||||
| > - 这个机制的意义在于:即便机器人主人从未打开过 WebUI 面板配置任何东西,插件作者依然可以为自己写的危险工具(例如重启服务、执行 shell 命令等)提供一层默认的安全防护,而不必依赖用户主动去配置。 | ||||||
|
|
||||||
| ## 调用 Agent | ||||||
|
|
||||||
| > [!TIP] | ||||||
|
|
||||||
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.