创建钩子

人工智能 社区

技能解读

当前原文的自动摘译,尚未人工审校。

1. 选择生命周期事件

在hooks.json中的以下键之一注册钩子: 事件 · 当它运行时 stop · 当代理停止时(回合结束) afterFileEdit · 在代理编辑文件后 afterAgentResponse · 在代理发送响应后

2.添加脚本

路径:.cursor/hooks/<name>.js(如果是 shell,则为.sh)。使用清晰的名称(例如memory-reminder.js、on-stop.js)。运行时:Node.js 是典型的; hooks.json中的命令在设置了光标的工作目录的情况下运行,以便node hooks/<name>.js解析(从.cursor运行)。输入:该钩子在标准输入上接收单个 JSON 对象。像这样阅读:有效负载:通常包括workspace_roots(回购根数组)。使用第一个根作为路径的项目根。特定于事件的有效负载可能包括file_path、prompt等。

3.在hooks.json中注册

路径:.cursor/hooks.json 格式:追加到所选事件的数组中;除非用户要求更换,否则不要删除现有的挂钩。

清单

[ ] 在 .cursor/hooks/<name>.js (或 .sh)下创建的脚本 [ ] 脚本从标准输入读取 JSON,并在需要时使用 workspace_roots 作为存储库根 [ ] 在正确的事件键下添加到 .cursor/hooks.json 的条目 [ ] 命令是 node hooks/<name>.js (或 ./hooks/<name>.sh);路径相对于.cursor

参考

此存储库中的现有挂钩:.cursor/hooks/on-stop.js、.cursor/hooks/after-file-edit.js、.cursor/hooks/memory-reminder.js — 将它们用作标准输入处理和path.join(root, ...)的模式。项目内存(.cursor/memory.md)指出,新的钩子应添加到.cursor/hooks/下并在.cursor/hooks.json中注册。

流狐档案 运行与权限由流狐静态检查推断,不代表作者声明或实际测试结果。
领域
人工智能
作者声明的智能体
未找到明确声明;不据此推断已兼容或已测试
静态检查
88 / 100 · 启发式扫描,不代表运行安全
运行环境证据
未执行;当前没有隔离运行测试证据
作者 / 版本 / 许可证
@diegosouzapw · 未声明许可证
上下文用量评级
低消耗
接入复杂程度
需简单配置
是否需要外部接口密钥
不需要
兼容的系统
macOS · Linux · Windows
运行环境要求
Node.js
检测到的文件与系统行为
  • 只读
  • 允许写入 / 修改
  • 命令执行
网络行为
仅限本地
安装命令数
无(仅作为资料)

需要注意: 未声明工具权限范围;实际权限由运行环境决定。

作者说明文件(原文) SKILL.md

# Create Hook

**Type:** Utility skill (procedural, no agentic lead or sub-agents).

Adds a Cursor lifecycle hook: a script that runs when a specific agent event fires. Hooks are project-level only (`.cursor/hooks/` and `.cursor/hooks.json`).

## 1. Choose the lifecycle event

Register the hook under one of these keys in `hooks.json`:

| Event                                           | When it runs                          |
| ----------------------------------------------- | ------------------------------------- |
| `stop`                                          | When the agent stops (end of turn)    |
| `afterFileEdit`                                 | After the agent edits a file          |
| `afterAgentResponse`                            | After the agent sends a response      |
| `beforeSubmitPrompt`                            | Before the user's prompt is submitted |
| `beforeReadFile` / `afterShellExecution` / etc. | See Cursor docs for full list         |

## 2. Add the script

- **Path**: `.cursor/hooks/<name>.js` (or `.sh` if shell). Use a clear name (e.g. `memory-reminder.js`, `on-stop.js`).
- **Runtime**: Node.js is typical; command in `hooks.json` is run with Cursor's working directory set so that `node hooks/<name>.js` resolves (run from `.cursor`).
- **Input**: The hook receives a single JSON object on **stdin**. Read it like this:

```javascript
let input = "";
process.stdin.setEncoding("utf8");
process.stdin.on("data", (chunk) => {
  input += chunk;
});
process.stdin.on("end", () => {
  const payload = JSON.parse(input);
  const roots = payload.workspace_roots || [process.cwd()];
  const root = path.resolve(roots[0]); // repo root
  // ... use root for paths like path.join(root, '.cursor', 'next-step.md')
});
```

- **Payload**: Often includes `workspace_roots` (array of repo roots). Use the first root as the project root for paths. Event-specific payloads may include `file_path`, `prompt`, etc.
- **Output**: Hook can write files (e.g. `.cursor/next-step.md`), log to stderr, or exit; avoid writing to stdout if the protocol expects JSON there.
- **Shebang**: Start Node scripts with `#!/usr/bin/env node`.

## 3. Register in hooks.json

- **Path**: `.cursor/hooks.json`
- **Format**:

```json
{
  "version": 1,
  "hooks": {
    "stop": [
      { "command": "node hooks/on-stop.js" },
      { "command": "node hooks/my-new-hook.js" }
    ]
  }
}
```

- Append to the array for the chosen event; do not remove existing hooks unless the user asks to replace them.

## Checklist

- [ ] Script created under `.cursor/hooks/<name>.js` (or `.sh`)
- [ ] Script reads JSON from stdin and uses `workspace_roots` for repo root when needed
- [ ] Entry added to `.cursor/hooks.json` under the correct event key
- [ ] Command is `node hooks/<name>.js` (or `./hooks/<name>.sh`); path is relative to `.cursor`

## Reference

- Existing hooks in this repo: `.cursor/hooks/on-stop.js`, `.cursor/hooks/after-file-edit.js`, `.cursor/hooks/memory-reminder.js` — use them as patterns for stdin handling and `path.join(root, ...)`.
- Project memory (`.cursor/memory.md`) notes that new hooks should be added under `.cursor/hooks/` and registered in `.cursor/hooks.json`.

讨论

基于 GitHub Discussions。登录 GitHub 即可参与讨论、点赞、订阅更新。