# 许可权限

Canonical URL: https://developers.iagenify.com/zh/sdk/agent/events/permission/
Language: zh

`{ type: "permission", id, tool, category, args, mode }`

正在询问一些事情。 它会在等待回回拨之前被排放出来, 也就是让界面在等待点击时给对话框涂上颜色。

Status: before the callback · permission_result follows · the whole args

## 字段 {#fields}

| Name | Type | Requirement | Description |
| --- | --- | --- | --- |
| `id` | string |  | 工具电话识别特征——同一 `tool_start` 将携带。 |
| `tool` | string |  | 要求我们做什么。 |
| `category` | string |  | 其 其 其 其 [类别](/zh/sdk/agent/permissions/categories). |
| `args` | object |  | 实际的论据。 这是一个人应该展示的。 |
| `mode` | string |  | 此时此刻生效的模式 。 |

## 人与圈人模式 {#pattern}

```javascript
const pending = new Map();

const agent = api.agents.create({
  runtime: "local", machine: pc, model, mode: "ask",

  // The promise is what the loop waits on
  onPermission: ({ id }) => new Promise((resolve) => pending.set(id, resolve)),

  // The event is what paints the dialog
  onEvent: (e) => {
    if (e.type === "permission") showDialog(e.id, e.tool, e.category, e.args);
    if (e.type === "permission_result") closeDialog(e.id, e.decision);
  },
});

// When the person clicks
function answer(id, decision) { pending.get(id)?.(decision); pending.delete(id); }
```

## permission_result {#result}

```javascript
{ type: "permission_result", id, tool, decision }   // "once" | "always" | "deny"
```

是在你回击的决断后 随决定结果而释放出来的 `true` 成为 `once`; `false`, `null` 却无所成 `deny`.

> **以寄出、出版和金钱,“总是”报称“曾经”**
>
> 这三个人接受了答案,并将其作为一个单一的认可,结果事件只说一次。下一次的电话被再次询问,无论点击什么。

## 完全没有排放任何事件 {#not-emitted}

- A A A `read`, `memory` 或 `task` 总是允许打电话
- 拒绝修改的计划模式——彻底拒绝,仅 `tool_end` 被排放。
- 允许打开四个打开的 全部模式之一 。
- 电话里写着 “永远”已经记起

因此,缺乏及时性并不是没有行动: `tool_start` 告诉你什么 东西正在发生。

> **子智能体事件也会传递到这里**
>
> 它们随身携带 palclId —— 启动它们的任务电话的识别符号 —— 以及代理、 其名称。 他们自己的代号与任务名称相加, 所以两个运行时不会相撞 。 在将事件作为主运行程序处理之前先测试 palId 。

- [权限](/zh/sdk/agent/permissions): 重新回调和决定的顺序。
- [tool_start](/zh/sdk/agent/events/tool-start): 批准后应采取的行动。

```python
import threading

pending = {}

def on_permission(request):
    # The Python callback is synchronous: block this run's thread until the person answers
    answered = threading.Event()
    pending[request["id"]] = {"event": answered, "decision": "deny"}
    answered.wait()
    return pending.pop(request["id"])["decision"]

def on_event(event):
    if event["type"] == "permission":
        show_dialog(event["id"], event["tool"], event["category"], event["args"])
    if event["type"] == "permission_result":
        close_dialog(event["id"], event["decision"])

agent = api.agents.create(runtime="local", machine=pc, model=model, mode="ask", on_permission=on_permission)

# When the person clicks (from another thread)
def answer(call_id, decision):
    pending[call_id]["decision"] = decision
    pending[call_id]["event"].set()
```

```python
{"type": "permission_result", "id": ..., "tool": ..., "decision": ...}   # "once" | "always" | "deny"
```
