Hacker News 中文摘要

RSS订阅

Claude Code now supports hooks

文章摘要

Claude Code允许用户通过注册shell命令来定制和扩展其行为。这些用户定义的钩子命令在Claude Code生命周期的不同阶段执行,确保特定操作始终发生,而不是依赖LLM选择执行。常见用例包括通知、自动格式化、日志记录、反馈和自定义权限。通过将规则编码为钩子而非提示指令,这些建议会变成应用级代码,每次预期时都会执行。钩子以用户权限执行shell命令,用户需确保其安全性。

文章总结

文章主要内容是关于如何通过注册Shell命令来定制和扩展Claude Code的行为。以下是关键点和重要信息的总结:

1. Claude Code Hooks概述

  • Hooks是用户定义的Shell命令,在Claude Code生命周期的不同阶段执行。
  • 通过Hooks,用户可以确定性地控制Claude Code的行为,确保某些操作始终执行,而不是依赖LLM(大语言模型)选择是否运行它们。

2. Hooks的用例

  • 通知:自定义Claude Code等待用户输入或运行权限时的通知方式。
  • 自动格式化:在每次文件编辑后自动运行格式化工具(如prettiergofmt等)。
  • 日志记录:跟踪和记录所有执行的命令,用于合规性或调试。
  • 反馈:当Claude Code生成的代码不符合代码库规范时,提供自动反馈。
  • 自定义权限:阻止对生产文件或敏感目录的修改。

3. Hooks的执行

  • Hooks以用户的完整权限执行Shell命令,无需确认。
  • 用户需确保Hooks的安全性和安全性,Anthropic对因使用Hooks导致的数据丢失或系统损坏不承担责任。

4. 快速入门:添加日志记录Hook

  • 前提条件:安装jq用于命令行中的JSON处理。
  • 步骤
    1. 打开Hooks配置,选择PreToolUse事件。
    2. 添加匹配器,选择Bash工具调用。
    3. 添加Hook命令,将命令日志记录到文件中。
    4. 保存配置,选择用户设置。
    5. 验证Hook是否生效。

5. Hooks的配置

  • Hooks配置在设置文件中,包括用户设置、项目设置和本地项目设置。
  • Hooks按匹配器组织,每个匹配器可以有多个Hook。

6. Hook事件

  • PreToolUse:在工具调用前执行,可以阻止工具调用并提供反馈。
  • PostToolUse:在工具成功完成后执行。
  • Notification:在Claude Code发送通知时执行。
  • Stop:在Claude Code完成响应时执行。

7. Hook输入

  • Hooks通过stdin接收JSON数据,包含会话信息和事件特定数据。

8. Hook输出

  • 简单输出:通过退出码、stdoutstderr返回状态。
  • 高级输出:通过返回结构化JSON进行更复杂的控制。

9. 安全考虑

  • 风险:Hooks可以执行任意Shell命令,可能导致数据丢失或系统损坏。
  • 最佳实践:验证和清理输入、使用绝对路径、避免敏感文件等。

10. 配置安全性

  • 直接编辑设置文件中的Hooks不会立即生效,Claude Code会在启动时捕获Hooks的快照,并在整个会话中使用该快照。

11. Hook执行细节

  • 超时:60秒执行限制。
  • 并行化:所有匹配的Hooks并行运行。
  • 环境:在当前目录和Claude Code的环境中运行。

12. 故障排除

  • 检查Hooks配置、验证设置文件、手动测试命令、检查退出码、审查stdoutstderr格式等。

通过Hooks,用户可以更灵活地控制Claude Code的行为,但也需要承担相应的安全责任。

评论总结

  1. 对Claude Code新功能的期待与认可

    • 评论1(ramoz)认为Hooks功能对于“上下文工程”和代理行为的运行时验证非常重要,尤其是在企业合规性和监督方面。
      • 引用:“Hooks will be important for 'context engineering' and runtime verification of an agent's performance.”
    • 评论5(nojs)表示Hooks可以设置复杂的规则,限制Claude Code运行的命令,提升了确定性。
      • 引用:“This is great, it means you can set up complex concrete rules about commands CC is allowed to run.”
  2. 对功能扩展的期望

    • 评论2(wilde)希望允许用户直接fork代码。
      • 引用:“I really wish they’d just let me fork the damn thing already.”
    • 评论8(parhamn)建议增加对monorepo中目录变更的匹配功能。
      • 引用:“This needs a way to match directories for changes in monorepos.”
  3. 对Claude Code使用体验的反馈

    • 评论11(bearjaws)表示Claude Code已成为其编码工作流的核心工具,尽管需要频繁审核更改,但整体效果很好。
      • 引用:“Claude Code has basically grown to dominate my initial coding workflow.”
    • 评论13(jonstewart)指出Claude Code有时会偏离指令,希望Hooks能限制其执行特定命令。
      • 引用:“Despite explicit directions in CLAUDE.md, I see it sometimes running make without -j.”
  4. 对AI工具局限性的讨论

    • 评论10(AdieuToLogic)认为LLM更像“通用翻译器”,而非无条件提供正确答案的“星际迷航”计算机。
      • 引用:“A better analogy is LLM's are closer to the 'universal translator'.”
    • 评论19(ainiriand)对Claude Code的实际能力表示怀疑,认为其只能作为调试助手或增强版搜索引擎。
      • 引用:“I am tired of pretending that this can actually pull any meaningful work.”
  5. 对AI工具未来发展的思考

    • 评论15(mkagenius)认为AI不会完全取代编码工作,因为配置Hooks等工具仍需人类参与。
      • 引用:“These kinds of tooling and related work will still be there unless AI evolves to the point that it even thinks of this.”
    • 评论17(b0a04gl)强调Hooks的确定性,允许多个代理安全地交接工作。
      • 引用:“hooks let you build workflows where multiple agents can hand off work safely.”
  6. 对Anthropic法律条款的质疑

    • 评论7(bionhoward)质疑Anthropic的法律条款限制了用户的使用场景,认为几乎所有用例都可能违反条款。
      • 引用:“Feels like the dirty secret of AI services is, every possible use case violates the terms.”
  7. 对Claude Code实际应用的积极反馈

    • 评论12(rtp4me)表示Claude Code帮助其快速完成跨语言脚本编写,提升了效率。
      • 引用:“I simply ask Claude to make it happen. This helps me focus on content creation.”
    • 评论14(cheriot)认为Claude Code在工具领域处于领先地位。
      • 引用:“Amazing how there's whole companies dedicated to this and yet claude code keeps leading the way.”