挂钩允许你在代理执行过程中通过在关键点执行自定义 shell 命令来扩展和自定义代理的行为 GitHub Copilot 。 有关挂钩的概念性概述(包括可用挂钩触发器的详细信息),请参阅 GitHub Copilot 的 Hook。
先决条件
仅Windows用户: 本文中的示例挂钩设计为在 Windows、Linux 和 macOS 上运行。 对于Windows,它们使用 PowerShell,并要求安装 PowerShell 7.0 或更高版本并在 PATH 中安装。 可以通过在终端中运行 pwsh --version 来检查 PowerShell 版本。 若要安装 PowerShell,请运行 winget install Microsoft.PowerShell,然后重启终端。
创建仓库级钩子
如果使用以下示例 sessionStart ,请在存储库的根目录中创建目录 logs 。
-
在存储库的文件夹中创建一个新
NAME.json文件(其中NAME描述了文件.github/hooks/的目的)。 -
在文本编辑器中,复制并粘贴以下挂钩模板。 从
hooks数组中删除您不打算使用的任何挂钩。JSON { "version": 1, "hooks": { "sessionStart": [...], "sessionEnd": [...], "userPromptSubmitted": [...], "preToolUse": [...], "postToolUse": [...], "errorOccurred": [...] } }{ "version": 1, "hooks": { "sessionStart": [...], "sessionEnd": [...], "userPromptSubmitted": [...], "preToolUse": [...], "postToolUse": [...], "errorOccurred": [...] } } -
在
bash和powershell键下配置挂钩语法,或直接引用已创建的脚本文件。注意
包括
bash键(包含适用于 Linux 和 macOS 的脚本)和powershell键(适用于 Windows 脚本),以允许挂钩在所有三个操作系统上运行。 Copilot 根据用户的操作系统使用相应的密钥。-
此示例运行一个脚本,该脚本使用
sessionStart挂钩将会话的开始日期输出到日志文件:JSON "sessionStart": [ { "type": "command", "bash": "echo \"Session started: $(date)\" >> logs/session.log", "powershell": "Add-Content -Path logs/session.log -Value \"Session started: $(Get-Date)\"", "cwd": ".", "timeoutSec": 10 } ],"sessionStart": [ { "type": "command", "bash": "echo \"Session started: $(date)\" >> logs/session.log", "powershell": "Add-Content -Path logs/session.log -Value \"Session started: $(Get-Date)\"", "cwd": ".", "timeoutSec": 10 } ], -
此示例调用外部
log-prompt脚本:JSON "userPromptSubmitted": [ { "type": "command", "bash": "./scripts/log-prompt.sh", "powershell": "./scripts/log-prompt.ps1", "cwd": ".", "env": { "LOG_LEVEL": "INFO" } } ],"userPromptSubmitted": [ { "type": "command", "bash": "./scripts/log-prompt.sh", "powershell": "./scripts/log-prompt.ps1", "cwd": ".", "env": { "LOG_LEVEL": "INFO" } } ],有关代理会话中的输入 JSON 以及示例脚本的完整参考,请参阅 GitHub Copilot 挂钩参考。
-
-
在受信任的存储库中启动或重启 Copilot CLI 以加载挂钩。 无需提交或合并文件,以便钩子在本地运行。
创建用户级钩子
用户级挂钩的配置就像存储库级挂钩一样,但挂钩文件存储在本地,位于主目录下方。
macOS 和 Windows 以下示例演示如何配置挂钩,这些挂钩将在 CLI 完成响应提示时以及退出 Copilot CLI时播放声音并显示消息框。 适用于 Linux 的挂钩类似于 macOS 示例,但使用 Linux 工具播放声音和显示消息。
退出 CLI 时,一个会话的 sessionEnd 挂钩共享 1.5 秒限制,即使 timeoutSec 更长。 删除用户级挂钩文件后,重启 CLI。 挂钩可以继续在加载了该文件的会话中运行,甚至在会话结束时也会继续运行。
macOS 的用户级示例
-
在
notification-hooks.json中创建一个名为~/.copilot/hooks/的文件。注意
如果设置了
COPILOT_HOME,请在$COPILOT_HOME/hooks/中创建该文件。 -
将以下 JSON 复制并粘贴到文件中:
JSON { "version": 1, "hooks": { "agentStop": [ { "type": "command", "bash": "osascript -e 'do shell script \"afplay /System/Library/Sounds/Funk.aiff &> /dev/null &\"' -e 'display dialog \"Agent stopped.\" with title \"Hook-generated message\" buttons {\"OK\"} default button \"OK\"'", "timeoutSec": 5 } ], "sessionEnd": [ { "type": "command", "bash": "osascript -e 'do shell script \"afplay /System/Library/Sounds/Funk.aiff &> /dev/null &\"' -e 'display dialog \"Session ended.\" with title \"Hook-generated message\" buttons {\"OK\"} default button \"OK\"'", "timeoutSec": 5 } ] } }{ "version": 1, "hooks": { "agentStop": [ { "type": "command", "bash": "osascript -e 'do shell script \"afplay /System/Library/Sounds/Funk.aiff &> /dev/null &\"' -e 'display dialog \"Agent stopped.\" with title \"Hook-generated message\" buttons {\"OK\"} default button \"OK\"'", "timeoutSec": 5 } ], "sessionEnd": [ { "type": "command", "bash": "osascript -e 'do shell script \"afplay /System/Library/Sounds/Funk.aiff &> /dev/null &\"' -e 'display dialog \"Session ended.\" with title \"Hook-generated message\" buttons {\"OK\"} default button \"OK\"'", "timeoutSec": 5 } ] } } -
启动或重启 Copilot CLI。
注意
CLI 启动时会加载对挂钩配置的更改。
-
输入提示并检查是否听到声音,并在代理完成响应时以及退出 CLI 时看到消息框。
-
删除
notification-hooks.json文件以移除这些挂钩。
Windows的用户级示例
-
在
notification-hooks.json中创建一个名为%USERPROFILE%\.copilot\hooks\的文件。注意
如果设置了
COPILOT_HOME,请在%COPILOT_HOME%\hooks\中创建该文件。 -
将以下 JSON 复制并粘贴到文件中:
JSON { "version": 1, "hooks": { "agentStop": [ { "type": "command", "powershell": "Add-Type -AssemblyName System.Windows.Forms; [System.Media.SystemSounds]::Asterisk.Play(); [System.Windows.Forms.MessageBox]::Show('Agent stopped.', 'Hook-generated message') | Out-Null", "timeoutSec": 5 } ], "sessionEnd": [ { "type": "command", "powershell": "Add-Type -AssemblyName System.Windows.Forms; [System.Media.SystemSounds]::Asterisk.Play(); [System.Windows.Forms.MessageBox]::Show('Session ended.', 'Hook-generated message') | Out-Null", "timeoutSec": 5 } ] } }{ "version": 1, "hooks": { "agentStop": [ { "type": "command", "powershell": "Add-Type -AssemblyName System.Windows.Forms; [System.Media.SystemSounds]::Asterisk.Play(); [System.Windows.Forms.MessageBox]::Show('Agent stopped.', 'Hook-generated message') | Out-Null", "timeoutSec": 5 } ], "sessionEnd": [ { "type": "command", "powershell": "Add-Type -AssemblyName System.Windows.Forms; [System.Media.SystemSounds]::Asterisk.Play(); [System.Windows.Forms.MessageBox]::Show('Session ended.', 'Hook-generated message') | Out-Null", "timeoutSec": 5 } ] } } -
启动或重启 Copilot CLI。
注意
CLI 启动时会加载对挂钩配置的更改。
-
输入提示并检查是否听到声音,并在代理完成响应时以及退出 CLI 时看到消息框。
-
删除
notification-hooks.json文件以移除这些挂钩。
故障排除
警告
下面的调试示例记录完整的钩子输入,其中可能包括敏感数据。
在将其与敏感输入一起使用之前,请移除 echo "$INPUT" >&2 和 set -x 之间的内容。
如果使用挂钩遇到问题,请使用下表进行故障排除。
| 問题 | Action |
|---|---|
| 钩子没有运行 |
|
| 挂钩超时 |
|
| JSON 输出无效 |
|
调试
可以使用以下方法调试挂钩:
-
在脚本中启用详细日志记录以检查输入数据和跟踪脚本执行。
Shell #!/bin/bash set -x # Enable bash debug mode INPUT=$(cat) echo "DEBUG: Received input" >&2 echo "$INPUT" >&2 # ... rest of script
#!/bin/bash set -x # Enable bash debug mode INPUT=$(cat) echo "DEBUG: Received input" >&2 echo "$INPUT" >&2 # ... rest of script -
在本地测试挂钩的方法是,将测试输入通过管道传递到挂钩,以验证其行为****。
Shell # Create test input echo '{"timestamp":1704614400000,"cwd":"/tmp","toolName":"bash","toolArgs":{"command":"ls"}}' | ./my-hook.sh # Check exit code echo $? # Validate output is valid JSON ./my-hook.sh | jq .# Create test input echo '{"timestamp":1704614400000,"cwd":"/tmp","toolName":"bash","toolArgs":{"command":"ls"}}' | ./my-hook.sh # Check exit code echo $? # Validate output is valid JSON ./my-hook.sh | jq .
沙盒钩子疑难解答
如果挂钩仅在启用沙盒时失败或超时,则它调用的工具可能需要访问配置文件或缓存。 请参阅“GitHub Copilot 挂钩参考”。
在授予访问权限之前,请记住,用户设置适用于所有 CLI 会话,并且不限于此钩子。 配置文件(例如 .npmrc 可以包含凭据)。 仅授予你愿意让其他沙盒命令也能获得的访问权限。 组织管理的限制仍然适用。
-
检查挂钩或工具的错误输出和日志,查找有关文件或目录被拒绝访问的信息。 如果脚本抑制错误,请启用诊断输出而不记录凭据。
-
运行
/sandbox policy后跟挂钩命令,以预览当前目录中特定于命令的开发人员工具访问权限。注意
如果挂钩设置了
cwd,则必须从配置的/sandbox policy目录运行cwd。这不会检查脚本文件或包括挂钩的其他源和数据目录权限。 请参阅“了解 GitHub Copilot CLI 中用于本地沙盒的文件系统策略”。
-
在用户设置中,仅将所需的路径添加到
sandbox.userPolicy.filesystem.readwritePaths或sandbox.userPolicy.filesystem.readonlyPaths。 对配置文件使用只读访问权限,仅对工具需要更新的缓存使用读/写访问权限。 请参阅“GitHub Copilot CLI 配置目录”。 -
启动新的 CLI 会话,并检查当该事件下一次发生时,钩子是否成功运行。