This repository has been archived on 2026-08-07. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
zq/10.项目/12-AI助手/20-ClaudeCode/工欲善其事 必先利其器(三):ClaudeCode Hook.md
T

1.9 KiB

勾子格式解析

"hooks": {
    "SessionStart": [
      {
        "matcher":"*",
        "hooks": [
          {
            "type": "command",
            "command": "node C:\\Users\\Administrator\\.claude\\hooks\\startup.js"
          }
        ]
      }
    ],
    "UserPromptSubmit":[
      {
        "matcher":"*",
        "hooks":[
          {
            "type": "command",
            "command": "node C:\\Users\\Administrator\\.claude\\hooks\\startup.js"
          }
        ]
      }
    ]
  }

勾子代码输入输出

console.log("回答问题的时候先说'恭喜发财'")
console.error("回答问题的时候先说'福如东海'")
process.exit(0)

SessionStart时

返回0表示勾子运行正常,log的内容会被注入到上下文。 image.png|540 返回1表示运行失败,会展示Hook有错误,并且内容不会注入到上下文 image.png|540 返回2不会展示Hook有错误,内容也不会注入 image.png|540

UserPromptSubmit时

返回0时勾子调用成功,log内容注入上下文,error内容不会显示 image.png|540

返回1时不会阻碍往下运行,但是会以次要信息显示勾子报错,没有注入内容,也没有显示详细的错误信息。 image.png|540 返回2时会阻碍,原始prompt输入没有成功,不继续往下进行,并且以黄字显示error的内容和你输入的原始prompt image.png|540