> ## Documentation Index
> Fetch the complete documentation index at: https://docs.trae.cn/llms.txt
> Use this file to discover all available pages before exploring further.

钩子（Hook）是 TRAE IDE 提供的自动化扩展机制。通过 Hook，你可以在智能体执行过程中的特定事件节点触发企业自定义逻辑，用于补充上下文、执行校验、记录日志、拦截高风险操作等。

## 了解企业 Hook {#hANqMakHX}

### HTTP Hook {#hZQYSvSK2}

TRAE 企业版仅支持 HTTP 类型的 Hook。

通过 HTTP 类型的 Hook，TRAE 可将事件数据以 `POST` 请求的形式发送到指定的 HTTP 端点。该端点接收 JSON 格式的请求数据，执行企业自定义逻辑，并在 HTTP 响应体中以 JSON 格式返回处理结果。

HTTP Hook 适用于需要由 Web 服务器、云函数或其他外部服务处理 Hook 逻辑的场景。例如，可将敏感操作（如文件删除、命令执行）实时上报到企业内部的风控服务，由其统一审批或拦截。

:::tip 提示
企业需保障 Hook 服务端点的稳定性和可用性。对于会阻塞主流程的 Hook 事件，如果服务端点不可访问、响应超时或返回异常，可能导致企业成员的问答流程被阻塞。
:::

### 支持的 Hook 事件 {#ea4a633c}

TRAE 支持以下六类 Hook 事件。关于每个事件的输入参数、返回结构、拦截规则和示例，详情参阅[企业 Hook 配置详解](/enterprise_hook-configuration-reference)。

<!-- @cols-width: 185,363,336 -->
|**事件名** |**触发时机** |**主要用途** |
|---|---|---|
|`SessionStart` |创建 Session 后、发起第一个对话前。 |初始化环境、注入环境变量或补充上下文。 |
|`UserPromptSubmit` |用户发送 Query 后、智能体开始处理前。 |拦截不允许的请求，或向模型附加上下文。 |
|`PreToolUse` |智能体发起工具调用后、实际执行前。 |校验、拦截、修改工具参数，或要求用户确认。 |
|`PostToolUse` |工具调用实际执行完成后。 |检查工具执行结果，并向模型附加上下文。 |
|`Stop` |智能体完成输出、准备结束当前 Query 时。 |检查产出是否达标。必要时阻断停止并让智能体继续执行。 |
|`Notification` |工具调用执行等待用户确认时，或智能体完成任务时。该事件异步触发，不阻塞主流程。 |向用户发送通知。 |

### Hook 的生命周期 {#ee0d2af5}

Hook 在会话的特定节点触发。事件触发并命中所配置的条件后，TRAE 会将该事件的 JSON 上下文传递给你的 Hook 处理程序，由该程序根据返回结果决定是否附加上下文、拦截请求或继续执行。Hook 的生命周期示意图如下：

<svg xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" width="100%" viewBox="-0.5 -0.5 840 765"><defs></defs><g><path d="M 149 42 L 149 75.63" fill="none" stroke="#000000" stroke-miterlimit="10" pointer-events="stroke"></path><path d="M 149 80.88 L 145.5 73.88 L 149 75.63 L 152.5 73.88 Z" fill="#000000" stroke="#000000" stroke-miterlimit="10" pointer-events="all"></path><rect x="89" y="2" width="120" height="40" rx="6" ry="6" fill="#e1d5e7" stroke="#000000" pointer-events="all"></rect><g transform="translate(-0.5 -0.5)"><foreignObject style="overflow: visible; text-align: left;" pointer-events="none" width="100%" height="100%"><div style="display: flex; align-items: unsafe center; justify-content: unsafe center; width: 118px; height: 1px; padding-top: 22px; margin-left: 90px;"><div style="box-sizing: border-box; font-size: 0; text-align: center; "><div style="display: inline-block; font-size: 12px; font-family: Helvetica; color: #000000; line-height: 1.2; pointer-events: all; white-space: normal; word-wrap: normal; ">Session 创建</div></div></div></foreignObject></g><path d="M 149 122 L 149 155.63" fill="none" stroke="#000000" stroke-miterlimit="10" pointer-events="stroke"></path><path d="M 149 160.88 L 145.5 153.88 L 149 155.63 L 152.5 153.88 Z" fill="#000000" stroke="#000000" stroke-miterlimit="10" pointer-events="all"></path><rect x="89" y="82" width="120" height="40" rx="6" ry="6" fill="#d5e8d4" stroke="#000000" pointer-events="all"></rect><g transform="translate(-0.5 -0.5)"><foreignObject style="overflow: visible; text-align: left;" pointer-events="none" width="100%" height="100%"><div style="display: flex; align-items: unsafe center; justify-content: unsafe center; width: 118px; height: 1px; padding-top: 102px; margin-left: 90px;"><div style="box-sizing: border-box; font-size: 0; text-align: center; "><div style="display: inline-block; font-size: 12px; font-family: Helvetica; color: #000000; line-height: 1.2; pointer-events: all; white-space: normal; word-wrap: normal; ">SessionStart</div></div></div></foreignObject></g><path d="M 149 202 L 149 235.63" fill="none" stroke="#000000" stroke-miterlimit="10" pointer-events="stroke"></path><path d="M 149 240.88 L 145.5 233.88 L 149 235.63 L 152.5 233.88 Z" fill="#000000" stroke="#000000" stroke-miterlimit="10" pointer-events="all"></path><rect x="69" y="162" width="160" height="40" rx="6" ry="6" fill="#e6e6e6" stroke="#000000" pointer-events="all"></rect><g transform="translate(-0.5 -0.5)"><foreignObject style="overflow: visible; text-align: left;" pointer-events="none" width="100%" height="100%"><div style="display: flex; align-items: unsafe center; justify-content: unsafe center; width: 158px; height: 1px; padding-top: 182px; margin-left: 70px;"><div style="box-sizing: border-box; font-size: 0; text-align: center; "><div style="display: inline-block; font-size: 12px; font-family: Helvetica; color: #000000; line-height: 1.2; pointer-events: all; white-space: normal; word-wrap: normal; ">UserPromptSubmit</div></div></div></foreignObject></g><path d="M 149 282 L 149 395.63" fill="none" stroke="#000000" stroke-miterlimit="10" pointer-events="stroke"></path><path d="M 149 400.88 L 145.5 393.88 L 149 395.63 L 152.5 393.88 Z" fill="#000000" stroke="#000000" stroke-miterlimit="10" pointer-events="all"></path><rect x="89" y="242" width="120" height="40" rx="6" ry="6" fill="#fff2cc" stroke="#000000" pointer-events="all"></rect><g transform="translate(-0.5 -0.5)"><foreignObject style="overflow: visible; text-align: left;" pointer-events="none" width="100%" height="100%"><div style="display: flex; align-items: unsafe center; justify-content: unsafe center; width: 118px; height: 1px; padding-top: 262px; margin-left: 90px;"><div style="box-sizing: border-box; font-size: 0; text-align: center; "><div style="display: inline-block; font-size: 12px; font-family: Helvetica; color: #000000; line-height: 1.2; pointer-events: all; white-space: normal; word-wrap: normal; ">PreToolUse</div></div></div></foreignObject></g><path d="M 149 442 L 149 475.63" fill="none" stroke="#000000" stroke-miterlimit="10" pointer-events="stroke"></path><path d="M 149 480.88 L 145.5 473.88 L 149 475.63 L 152.5 473.88 Z" fill="#000000" stroke="#000000" stroke-miterlimit="10" pointer-events="all"></path><rect x="89" y="402" width="120" height="40" rx="6" ry="6" fill="#d4e1f5" stroke="#000000" pointer-events="all"></rect><g transform="translate(-0.5 -0.5)"><foreignObject style="overflow: visible; text-align: left;" pointer-events="none" width="100%" height="100%"><div style="display: flex; align-items: unsafe center; justify-content: unsafe center; width: 118px; height: 1px; padding-top: 422px; margin-left: 90px;"><div style="box-sizing: border-box; font-size: 0; text-align: center; "><div style="display: inline-block; font-size: 12px; font-family: Helvetica; color: #000000; line-height: 1.2; pointer-events: all; white-space: normal; word-wrap: normal; ">[工具执行]</div></div></div></foreignObject></g><path d="M 149 522 L 149 555.63" fill="none" stroke="#000000" stroke-miterlimit="10" pointer-events="stroke"></path><path d="M 149 560.88 L 145.5 553.88 L 149 555.63 L 152.5 553.88 Z" fill="#000000" stroke="#000000" stroke-miterlimit="10" pointer-events="all"></path><rect x="89" y="482" width="120" height="40" rx="6" ry="6" fill="#fff2cc" stroke="#000000" pointer-events="all"></rect><g transform="translate(-0.5 -0.5)"><foreignObject style="overflow: visible; text-align: left;" pointer-events="none" width="100%" height="100%"><div style="display: flex; align-items: unsafe center; justify-content: unsafe center; width: 118px; height: 1px; padding-top: 502px; margin-left: 90px;"><div style="box-sizing: border-box; font-size: 0; text-align: center; "><div style="display: inline-block; font-size: 12px; font-family: Helvetica; color: #000000; line-height: 1.2; pointer-events: all; white-space: normal; word-wrap: normal; ">PostToolUse</div></div></div></foreignObject></g><path d="M 149 602 L 149 635.63" fill="none" stroke="#000000" stroke-miterlimit="10" pointer-events="stroke"></path><path d="M 149 640.88 L 145.5 633.88 L 149 635.63 L 152.5 633.88 Z" fill="#000000" stroke="#000000" stroke-miterlimit="10" pointer-events="all"></path><path d="M 29 582 L 9 582 L 9 262 L 82.63 262" fill="none" stroke="#000000" stroke-miterlimit="10" pointer-events="stroke"></path><path d="M 87.88 262 L 80.88 265.5 L 82.63 262 L 80.88 258.5 Z" fill="#000000" stroke="#000000" stroke-miterlimit="10" pointer-events="all"></path><rect x="29" y="562" width="240" height="40" fill="none" stroke="none" pointer-events="all"></rect><g transform="translate(-0.5 -0.5)"><foreignObject style="overflow: visible; text-align: left;" pointer-events="none" width="100%" height="100%"><div style="display: flex; align-items: unsafe center; justify-content: unsafe center; width: 238px; height: 1px; padding-top: 582px; margin-left: 30px;"><div style="box-sizing: border-box; font-size: 0; text-align: center; "><div style="display: inline-block; font-size: 12px; font-family: Helvetica; color: #000000; line-height: 1.2; pointer-events: all; white-space: normal; word-wrap: normal; ">（若有更多工具调用，循环回 PreToolUse）</div></div></div></foreignObject></g><rect x="89" y="642" width="120" height="40" rx="6" ry="6" fill="#ffcccc" stroke="#000000" pointer-events="all"></rect><g transform="translate(-0.5 -0.5)"><foreignObject style="overflow: visible; text-align: left;" pointer-events="none" width="100%" height="100%"><div style="display: flex; align-items: unsafe center; justify-content: unsafe center; width: 118px; height: 1px; padding-top: 662px; margin-left: 90px;"><div style="box-sizing: border-box; font-size: 0; text-align: center; "><div style="display: inline-block; font-size: 12px; font-family: Helvetica; color: #000000; line-height: 1.2; pointer-events: all; white-space: normal; word-wrap: normal; ">Stop</div></div></div></foreignObject></g><path d="M 209 101.5 L 329 102" fill="none" stroke="#000000" stroke-miterlimit="10" pointer-events="stroke"></path><rect x="459" y="92" width="40" height="20" fill="none" stroke="none" pointer-events="all"></rect><g transform="translate(-0.5 -0.5)"><foreignObject style="overflow: visible; text-align: left;" pointer-events="none" width="100%" height="100%"><div style="display: flex; align-items: unsafe center; justify-content: unsafe center; width: 1px; height: 1px; padding-top: 102px; margin-left: 479px;"><div style="box-sizing: border-box; font-size: 0; text-align: center; "><div style="display: inline-block; font-size: 12px; font-family: Helvetica; color: #000000; line-height: 1.2; pointer-events: all; white-space: nowrap; "><div><div>会话初始化，可通过 Hooks 注入环境变量和上下文。</div></div><span></span></div></div></div></foreignObject></g><path d="M 229 182 L 329 182" fill="none" stroke="#000000" stroke-miterlimit="10" pointer-events="stroke"></path><rect x="499" y="172" width="40" height="20" fill="none" stroke="none" pointer-events="all"></rect><g transform="translate(-0.5 -0.5)"><foreignObject style="overflow: visible; text-align: left;" pointer-events="none" width="100%" height="100%"><div style="display: flex; align-items: unsafe center; justify-content: unsafe center; width: 1px; height: 1px; padding-top: 182px; margin-left: 519px;"><div style="box-sizing: border-box; font-size: 0; text-align: center; "><div style="display: inline-block; font-size: 12px; font-family: Helvetica; color: #000000; line-height: 1.2; pointer-events: all; white-space: nowrap; "><div><div>用户提交 Prompt，可通过 Hooks 拦截不允许的请求或附加上下文。</div></div><span></span></div></div></div></foreignObject></g><rect x="549" y="252" width="40" height="20" fill="none" stroke="none" pointer-events="all"></rect><g transform="translate(-0.5 -0.5)"><foreignObject style="overflow: visible; text-align: left;" pointer-events="none" width="100%" height="100%"><div style="display: flex; align-items: unsafe center; justify-content: unsafe center; width: 1px; height: 1px; padding-top: 262px; margin-left: 569px;"><div style="box-sizing: border-box; font-size: 0; text-align: center; "><div style="display: inline-block; font-size: 12px; font-family: Helvetica; color: #000000; line-height: 1.2; pointer-events: all; white-space: nowrap; "><br>智能体执行工具调用前，可通过 Hooks <span style="background-color:initial">校验、拦截、修改工具参数，或要求用户确认。<br></span> </div></div></div></foreignObject></g><path d="M 209 261.5 L 329 262" fill="none" stroke="#000000" stroke-miterlimit="10" pointer-events="stroke"></path><path d="M 209 501.5 L 329 502" fill="none" stroke="#000000" stroke-miterlimit="10" pointer-events="stroke"></path><rect x="529" y="492" width="40" height="20" fill="none" stroke="none" pointer-events="all"></rect><g transform="translate(-0.5 -0.5)"><foreignObject style="overflow: visible; text-align: left;" pointer-events="none" width="100%" height="100%"><div style="display: flex; align-items: unsafe center; justify-content: unsafe center; width: 1px; height: 1px; padding-top: 502px; margin-left: 549px;"><div style="box-sizing: border-box; font-size: 0; text-align: center; "><div style="display: inline-block; font-size: 12px; font-family: Helvetica; color: #000000; line-height: 1.2; pointer-events: all; white-space: nowrap; ">工具调用执行完毕后，可通过 Hooks <span style="background-color:initial">检查工具执行结果，并向模型附加上下文。</span></div></div></div></foreignObject></g><rect x="569" y="652" width="40" height="20" fill="none" stroke="none" pointer-events="all"></rect><g transform="translate(-0.5 -0.5)"><foreignObject style="overflow: visible; text-align: left;" pointer-events="none" width="100%" height="100%"><div style="display: flex; align-items: unsafe center; justify-content: unsafe center; width: 1px; height: 1px; padding-top: 662px; margin-left: 589px;"><div style="box-sizing: border-box; font-size: 0; text-align: center; "><div style="display: inline-block; font-size: 12px; font-family: Helvetica; color: #000000; line-height: 1.2; pointer-events: all; white-space: nowrap; "><div>智能体完成输出后，可通过 Hooks <span style="background-color:initial">检查产出是否达标。必要时阻断停止并让智能体继续执行。</span></div></div></div></div></foreignObject></g><path d="M 209 661.5 L 329 662" fill="none" stroke="#000000" stroke-miterlimit="10" pointer-events="stroke"></path><rect x="269" y="322" width="120" height="40" rx="6" ry="6" fill="#bbebeb" stroke="#000000" pointer-events="all"></rect><g transform="translate(-0.5 -0.5)"><foreignObject style="overflow: visible; text-align: left;" pointer-events="none" width="100%" height="100%"><div style="display: flex; align-items: unsafe center; justify-content: unsafe center; width: 118px; height: 1px; padding-top: 342px; margin-left: 270px;"><div style="box-sizing: border-box; font-size: 0; text-align: center; "><div style="display: inline-block; font-size: 12px; font-family: Helvetica; color: #000000; line-height: 1.2; pointer-events: all; white-space: normal; word-wrap: normal; ">Notificaiton</div></div></div></foreignObject></g><path d="M 149 344 L 269 344" fill="none" stroke="#000000" stroke-miterlimit="10" stroke-dasharray="3 3" pointer-events="stroke"></path><path d="M 389 342 L 449 343" fill="none" stroke="#000000" stroke-miterlimit="10" pointer-events="stroke"></path><rect x="609" y="332" width="40" height="20" fill="none" stroke="none" pointer-events="all"></rect><g transform="translate(-0.5 -0.5)"><foreignObject style="overflow: visible; text-align: left;" pointer-events="none" width="100%" height="100%"><div style="display: flex; align-items: unsafe center; justify-content: unsafe center; width: 1px; height: 1px; padding-top: 342px; margin-left: 629px;"><div style="box-sizing: border-box; font-size: 0; text-align: center; "><div style="display: inline-block; font-size: 12px; font-family: Helvetica; color: #000000; line-height: 1.2; pointer-events: all; white-space: nowrap; ">工具调用执行等待用户确认时，可通过 Hooks 向用户发送通知。</div></div></div></foreignObject></g><path d="M 149 742 L 149 682" fill="none" stroke="#000000" stroke-miterlimit="10" stroke-dasharray="3 3" pointer-events="stroke"></path><path d="M 149 742 L 262.63 742" fill="none" stroke="#000000" stroke-miterlimit="10" stroke-dasharray="3 3" pointer-events="stroke"></path><path d="M 267.88 742 L 260.88 745.5 L 262.63 742 L 260.88 738.5 Z" fill="#000000" stroke="#000000" stroke-miterlimit="10" pointer-events="all"></path><rect x="269" y="722" width="120" height="40" rx="6" ry="6" fill="#bbebeb" stroke="#000000" pointer-events="all"></rect><g transform="translate(-0.5 -0.5)"><foreignObject style="overflow: visible; text-align: left;" pointer-events="none" width="100%" height="100%"><div style="display: flex; align-items: unsafe center; justify-content: unsafe center; width: 118px; height: 1px; padding-top: 742px; margin-left: 270px;"><div style="box-sizing: border-box; font-size: 0; text-align: center; "><div style="display: inline-block; font-size: 12px; font-family: Helvetica; color: #000000; line-height: 1.2; pointer-events: all; white-space: normal; word-wrap: normal; ">Notificaiton</div></div></div></foreignObject></g><path d="M 389 741.5 L 449 742.5" fill="none" stroke="#000000" stroke-miterlimit="10" pointer-events="stroke"></path><rect x="539" y="732" width="40" height="20" fill="none" stroke="none" pointer-events="all"></rect><g transform="translate(-0.5 -0.5)"><foreignObject style="overflow: visible; text-align: left;" pointer-events="none" width="100%" height="100%"><div style="display: flex; align-items: unsafe center; justify-content: unsafe center; width: 1px; height: 1px; padding-top: 742px; margin-left: 559px;"><div style="box-sizing: border-box; font-size: 0; text-align: center; "><div style="display: inline-block; font-size: 12px; font-family: Helvetica; color: #000000; line-height: 1.2; pointer-events: all; white-space: nowrap; ">智能体完成任务时，向用户发送通知。</div></div></div></foreignObject></g></g></svg>

### 适用场景 {#691ec078}

<!-- @cols-width: 150,448,266 -->
|**场景** |**描述** |**建议事件** |
|---|---|---|
|安全合规 |拦截敏感请求、保护关键文件、阻止高风险命令。 |`UserPromptSubmit`、`PreToolUse` |
|研发流程自动化 |在代码变更后，调用脚本进行代码格式化或校验；在工具调用前后，调用脚本来记录日志或审计。 |`PreToolUse`、`PostToolUse` |
|上下文增强 |会话开始时，注入环境变量、工程约束或背景信息。 |`SessionStart` |
|任务验收 |任务结束前，检查产出质量。若质量不达标，则阻断停止，让智能体继续优化其输出。 |`Stop` |
|工具链联动 |将智能体的行为接入现有脚本、审查机制或规范检查流程。 |根据实际需求选择。 |

### 使用限制 {#hEJTCnDJs}

目前，仅支持在 TRAE IDE 和 TRAE CLI 中使用企业 Hook。

## 创建企业 Hook {#hl9Y6Dt1S}

### 第一步：创建 HTTP 服务端点 {#heiJDylng}

在控制台中配置 HTTP Hook 前，你需要先准备一个可被 TRAE 访问的 HTTP 服务端点。TRAE 在命中 Hook 事件后，会向该端点发送 `POST` 请求。服务端点需要解析请求体中的 JSON 数据，执行企业自定义逻辑，并以 JSON 格式返回处理结果。

### 第二步：创建并启用企业 Hook {#hVmdYXZzp}

在 TRAE 企业版控制台中，企业管理员可以为企业成员统一创建 Hook。

1. 登录 [TRAE 企业版控制台](https://console.enterprise.trae.cn/)。
2. 前往 **企业配置** > **企业 Hooks**。
   你将进入企业 Hook 管理面板。
3. 找到需配置 Hook 的事件，然后点击 **操作** 栏中的 **编辑** 按钮。
   你将进入该事件的 Hook 配置面板。
   ![Image=2544x1627](https://p9-arcosite.byteimg.com/tos-cn-i-goo7wpa0wc/6e0e7e67e143493882ff1d413bd3b1ab~tplv-goo7wpa0wc-topic.png)
4. 填入 Hook 配置。不同事件支持的配置项不同，详情参阅[企业 Hook 配置详解](/enterprise_hook-configuration-reference)。
5. 点击 **保存** 按钮。
   页面自动跳转至企业 Hook 管理面板。客户端将定期拉取最新 Hook 配置；命中事件后，执行对应 Hook。
6. 启用该 Hook 事件。

## 创建个人 Hook {#6d07ca9d}

在 TRAE IDE 中，企业成员可以创建并管理仅供个人使用的 Hook，详情参考[此文档](/ide/automate-actions-with-hooks)。
