agentscope.permissionPermissionEngine
class PermissionEngine
agentscope/permission/_engine.py:17–848权限引擎,驱动智能体的推理、工具调用与会话执行。位于 agentscope.permission 包。核心成员包括 add_rule、check_permission。
Evaluates tool execution requests against configured permission rules.
Matching strategy is delegated to each tool's :meth:ToolBase.match_rule:
- Bash tools: substring / prefix wildcard matching against the command
- Write/Read/Edit tools: glob matching against file paths
- Other tools: generic pattern matching (or tool-name-level only)
Each :class:PermissionMode has its own _check_<mode> method so
that mode policies are self-contained and readable in isolation. See
:meth:check_permission for the dispatcher and the individual methods
for per-mode evaluation order.
构造器摘要Constructor Summary
| 构造器Constructor | 说明Description |
|---|---|
def __init__(context: PermissionContext) -> None | 初始化 `PermissionEngine` 实例。 Initialize the permission engine. |
方法摘要Method Summary
| 方法Method | 说明Description |
|---|---|
def add_rule(rule: PermissionRule) -> None | 添加规则。 Add a permission rule to the context. |
async def check_permission(tool: ToolBase, tool_input: dict[str, Any]) -> PermissionDecisionasync | 检查权限用于工具执行请求。 Check permission for a tool execution request. |
构造器详情Constructor Detail
PermissionEngine
def __init__(context: PermissionContext) -> None
初始化 PermissionEngine 实例。
Initialize the permission engine.
示例Example
>>> context = PermissionContext(mode=PermissionMode.ACCEPT_EDITS)
>>> engine = PermissionEngine(context)
| 名称Name | 类型Type | 说明Description |
|---|---|---|
context | PermissionContext | 上下文(类型 `PermissionContext`)。The permission context containing rules and mode |
agentscope/permission/_engine.py:33–47方法详情Method Detail
add_rule
def add_rule(rule: PermissionRule) -> None
添加规则。
Add a permission rule to the context.
示例Example
>>> engine.add_rule(PermissionRule(
... tool_name="Bash",
... rule_content="git:*",
... behavior=PermissionBehavior.ALLOW,
... ))
| 名称Name | 类型Type | 说明Description |
|---|---|---|
rule | PermissionRule | 权限规则到添加。The permission rule to add |
agentscope/permission/_engine.py:49–75check_permission
async def check_permission(tool: ToolBase, tool_input: dict[str, Any]) -> PermissionDecision
检查权限用于工具执行请求。
Dispatches to a per-mode private method so each mode's policy is self-contained and readable in isolation: - DEFAULT → :meth:_check_default - EXPLORE → :meth:_check_explore - ACCEPT_EDITS → :meth:_check_accept_edits - BYPASS → :meth:_check_bypass - DONT_ASK → :meth:`_che…
| 名称Name | 类型Type | 说明Description |
|---|---|---|
tool | ToolBase | 工具(类型 `ToolBase`)。The tool instance being called. |
tool_input | dict[str, Any] | 工具输入(类型 `dict[str, Any]`)。The tool input data, used for rule matching and tool-specific checks. |
agentscope/permission/_engine.py:77–115