feat: 实现 Hook 系统

参考 open-code 的实现,添加工具执行前后的 hook 功能:

- 添加 Hook 类型定义 (tool.execute.before/after, file.edited/created/deleted 等)
- 实现 HookManager 管理器,支持插件注册和事件触发
- 实现配置文件加载器,支持 .ai-assistant.json/jsonc 格式
- 支持 glob 模式匹配文件触发 shell 命令
- 集成到 Agent 工具执行流程
- 添加 minimatch 依赖用于 glob 匹配
- 编写完整测试用例 (27 个测试)

配置示例:
```json
{
  "hooks": {
    "file_edited": {
      "*.ts": [{ "command": ["npx", "tsc", "--noEmit"] }]
    }
  }
}
```
This commit is contained in:
2025-12-11 23:12:04 +08:00
parent 89673e28cb
commit 630ce9fd4b
8 changed files with 1667 additions and 1 deletions
+37
View File
@@ -18,6 +18,7 @@
"commander": "^12.1.0",
"inquirer": "^12.0.0",
"js-yaml": "^4.1.1",
"minimatch": "^10.1.1",
"ora": "^8.1.0",
"qwen-ai-provider-v5": "^1.0.2",
"tree-sitter-bash": "^0.25.1",
@@ -972,6 +973,27 @@
}
}
},
"node_modules/@isaacs/balanced-match": {
"version": "4.0.1",
"resolved": "https://registry.npmjs.org/@isaacs/balanced-match/-/balanced-match-4.0.1.tgz",
"integrity": "sha512-yzMTt9lEb8Gv7zRioUilSglI0c0smZ9k5D65677DLWLtWJaXIS3CqcGyUFByYKlnUj6TkjLVs54fBl6+TiGQDQ==",
"license": "MIT",
"engines": {
"node": "20 || >=22"
}
},
"node_modules/@isaacs/brace-expansion": {
"version": "5.0.0",
"resolved": "https://registry.npmjs.org/@isaacs/brace-expansion/-/brace-expansion-5.0.0.tgz",
"integrity": "sha512-ZT55BDLV0yv0RBm2czMiZ+SqCGO7AvmOM3G/w2xhVPH+te0aKgFjmBvGlL1dH+ql2tgGO3MVrbb3jCKyvpgnxA==",
"license": "MIT",
"dependencies": {
"@isaacs/balanced-match": "^4.0.1"
},
"engines": {
"node": "20 || >=22"
}
},
"node_modules/@jridgewell/resolve-uri": {
"version": "3.1.2",
"resolved": "https://registry.npmjs.org/@jridgewell/resolve-uri/-/resolve-uri-3.1.2.tgz",
@@ -2438,6 +2460,21 @@
"url": "https://github.com/sponsors/sindresorhus"
}
},
"node_modules/minimatch": {
"version": "10.1.1",
"resolved": "https://registry.npmjs.org/minimatch/-/minimatch-10.1.1.tgz",
"integrity": "sha512-enIvLvRAFZYXJzkCYG5RKmPfrFArdLv+R+lbQ53BmIMLIry74bjKzX6iHAm8WYamJkhSSEabrWN5D97XnKObjQ==",
"license": "BlueOak-1.0.0",
"dependencies": {
"@isaacs/brace-expansion": "^5.0.0"
},
"engines": {
"node": "20 || >=22"
},
"funding": {
"url": "https://github.com/sponsors/isaacs"
}
},
"node_modules/ms": {
"version": "2.1.3",
"resolved": "https://registry.npmjs.org/ms/-/ms-2.1.3.tgz",