{"slug":"windows-ai-agent-tts","title":"让 AI 干完活自己喊一声：Windows 下给 Codex、Claude Code、OpenCode 加 TTS 提示","description":"不用 Beep，也不额外维护 .ps1 文件，让三个终端 AI 工具在响应结束后用 PowerShell 内置 TTS 播报完成提示。","coverImage":"https://b.acofork.com/img/windows-ai-agent-tts.svg","coverWidth":1600,"coverHeight":860,"date":"2026-08-22","tags":["AI","Windows"],"pinned":false,"wordCount":2902,"html":"<aside class=\"markdown-alert markdown-alert--note\"><div class=\"markdown-alert-title\"><svg class=\"markdown-alert-icon\" aria-hidden=\"true\" width=\"18\" height=\"18\" viewbox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" xmlns=\"http://www.w3.org/2000/svg\"><circle cx=\"12\" cy=\"12\" r=\"10\"></circle><path d=\"M12 16v-4\"></path><path d=\"M12 8h.01\"></path></svg><span>备注</span></div><div class=\"markdown-alert-body\"><p>本文由 GPT-5（Codex）根据给定教程和官方文档生成，并经过人工整理。文中的配置按 2026 年 8 月的文档口径编写，工具更新后字段可能变化。</p>\n</div></aside><h2 id=\"先说结论\">先说结论</h2><p>我很讨厌把任务丢给 AI 以后去刷网页。回来以后，第一件事不是看它写得怎么样，而是先确认：<strong>它到底是在认真干活，还是早就停在那里等我了？</strong></p>\n<p>终端里盯着光标看当然也能知道，但那种感觉很像守着洗衣机等它脱水。Windows 自带语音合成，正好可以把这件事变成一句人话：</p>\n<div class=\"markdown-code\" data-language=\"text\"><div class=\"markdown-code-header\"><span class=\"markdown-code-language\"><svg class=\"code-language-icon\" aria-hidden=\"true\" width=\"16\" height=\"16\" viewbox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" xmlns=\"http://www.w3.org/2000/svg\"><path d=\"M6 22a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h8a2.4 2.4 0 0 1 1.704.706l3.588 3.588A2.4 2.4 0 0 1 20 8v12a2 2 0 0 1-2 2z\"></path><path d=\"M14 2v5a1 1 0 0 0 1 1h5\"></path><path d=\"M10 9H8\"></path><path d=\"M16 13H8\"></path><path d=\"M16 17H8\"></path></svg><span>text</span></span><button type=\"button\" class=\"code-block-copy\" aria-label=\"复制代码\" title=\"复制代码\"><svg class=\"code-block-copy-icon\" aria-hidden=\"true\" width=\"16\" height=\"16\" viewbox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" xmlns=\"http://www.w3.org/2000/svg\"><rect width=\"14\" height=\"14\" x=\"8\" y=\"8\" rx=\"2\" ry=\"2\"></rect><path d=\"M4 16c-1.1 0-2-.9-2-2V4c0-1.1.9-2 2-2h10c1.1 0 2 .9 2 2\"></path></svg></button></div><pre><code class=\"hljs language-text\">ChatGPT工作完成\nClaude Code工作完成\nOpen Code工作完成</code></pre></div><p>这篇记录一套比较克制的做法：不使用 <code>Beep</code>，不创建额外的 <code>.ps1</code> 文件，不启动一堆后台 PowerShell worker，只在原来的 Hook 或插件里替换完成命令。</p>\n<p><img src=\"https://b.acofork.com/img/windows-ai-agent-tts.svg\" alt=\"Windows 下给三个 AI 编程工具添加语音完成提示\" width=\"1600\" height=\"860\" loading=\"eager\" decoding=\"async\" /></p>\n<h2 id=\"三个工具的-完成-不是一回事\">三个工具的“完成”不是一回事</h2><p>先把事件选对，不然最后会得到一个非常勤快、但非常吵的电脑。</p>\n<table>\n<thead>\n<tr>\n<th>工具</th>\n<th>配置位置</th>\n<th>推荐事件</th>\n<th>它更接近什么</th>\n</tr>\n</thead>\n<tbody><tr>\n<td>Codex</td>\n<td><code>%USERPROFILE%\\.codex\\hooks.json</code></td>\n<td><code>Stop</code></td>\n<td>一次代理回合停止</td>\n</tr>\n<tr>\n<td>Claude Code</td>\n<td><code>%USERPROFILE%\\.claude\\settings.json</code></td>\n<td><code>Stop</code></td>\n<td>主代理完成一次响应</td>\n</tr>\n<tr>\n<td>OpenCode</td>\n<td><code>%USERPROFILE%\\.config\\opencode\\plugins\\*.ts</code></td>\n<td><code>session.idle</code></td>\n<td>会话进入空闲状态</td>\n</tr>\n</tbody></table>\n<p>Codex 的 <a href=\"https://learn.chatgpt.com/docs/hooks\" target=\"_blank\" rel=\"noopener noreferrer\">Hooks 官方文档</a> 将 <code>Stop</code> 列为回合生命周期事件；当前版本还支持 <code>commandWindows</code> 作为 Windows 专用命令覆盖。需要注意的是，Codex 会加载多个匹配的 Hook，同一个事件的多个命令也可能并行启动，所以旧配置里的重复提醒最好清理掉。</p>\n<p>Claude Code 的 <a href=\"https://code.claude.com/docs/en/hooks#stop\" target=\"_blank\" rel=\"noopener noreferrer\"><code>Stop</code></a> 是“主代理完成响应”，不是“整个复杂任务从此彻底结束”。多步骤任务中，模型可能先停一次，随后又被 Hook 或后台任务唤醒。因此它是最接近的事件，不是严格的一次性任务完成信号。</p>\n<p>OpenCode 的插件事件里有 <a href=\"https://dev.opencode.ai/docs/plugins/#send-notifications\" target=\"_blank\" rel=\"noopener noreferrer\"><code>session.idle</code></a>，官方示例也用它做会话完成通知。当前文档说明，放在 <code>.opencode/plugins/</code> 或用户目录 <code>~/.config/opencode/plugins/</code> 下的本地插件会被自动加载，不必为了一个本地文件再写一层命令模板。</p>\n<h2 id=\"powershell-内置-tts\">PowerShell 内置 TTS</h2><p>Windows 自带的语音组件是：</p>\n<div class=\"markdown-code\" data-language=\"powershell\"><div class=\"markdown-code-header\"><span class=\"markdown-code-language\"><svg class=\"code-language-icon\" aria-hidden=\"true\" width=\"16\" height=\"16\" viewbox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" xmlns=\"http://www.w3.org/2000/svg\"><path d=\"M12 19h8\"></path><path d=\"m4 17 6-6-6-6\"></path></svg><span>powershell</span></span><button type=\"button\" class=\"code-block-copy\" aria-label=\"复制代码\" title=\"复制代码\"><svg class=\"code-block-copy-icon\" aria-hidden=\"true\" width=\"16\" height=\"16\" viewbox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" xmlns=\"http://www.w3.org/2000/svg\"><rect width=\"14\" height=\"14\" x=\"8\" y=\"8\" rx=\"2\" ry=\"2\"></rect><path d=\"M4 16c-1.1 0-2-.9-2-2V4c0-1.1.9-2 2-2h10c1.1 0 2 .9 2 2\"></path></svg></button></div><pre><code class=\"hljs language-powershell\">System.Speech.Synthesis.SpeechSynthesizer</code></pre></div><p>先在 PowerShell 里手动测试一个可读版本：</p>\n<div class=\"markdown-code\" data-language=\"powershell\"><div class=\"markdown-code-header\"><span class=\"markdown-code-language\"><svg class=\"code-language-icon\" aria-hidden=\"true\" width=\"16\" height=\"16\" viewbox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" xmlns=\"http://www.w3.org/2000/svg\"><path d=\"M12 19h8\"></path><path d=\"m4 17 6-6-6-6\"></path></svg><span>powershell</span></span><button type=\"button\" class=\"code-block-copy\" aria-label=\"复制代码\" title=\"复制代码\"><svg class=\"code-block-copy-icon\" aria-hidden=\"true\" width=\"16\" height=\"16\" viewbox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" xmlns=\"http://www.w3.org/2000/svg\"><rect width=\"14\" height=\"14\" x=\"8\" y=\"8\" rx=\"2\" ry=\"2\"></rect><path d=\"M4 16c-1.1 0-2-.9-2-2V4c0-1.1.9-2 2-2h10c1.1 0 2 .9 2 2\"></path></svg></button></div><pre><code class=\"hljs language-powershell\">powershell.exe <span class=\"hljs-literal\">-NoProfile</span> <span class=\"hljs-literal\">-NonInteractive</span> <span class=\"hljs-literal\">-Command</span> <span class=\"hljs-string\">'Add-Type -AssemblyName System.Speech; $tts = New-Object System.Speech.Synthesis.SpeechSynthesizer; try { $tts.Speak((\"ChatGPT\" + [char]0x5DE5 + [char]0x4F5C + [char]0x5B8C + [char]0x6210)) } finally { $tts.Dispose() }'</span></code></pre></div><p>这里没有直接把中文写进命令，而是把“工作完成”拆成 Unicode 码点：</p>\n<div class=\"markdown-code\" data-language=\"text\"><div class=\"markdown-code-header\"><span class=\"markdown-code-language\"><svg class=\"code-language-icon\" aria-hidden=\"true\" width=\"16\" height=\"16\" viewbox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" xmlns=\"http://www.w3.org/2000/svg\"><path d=\"M6 22a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h8a2.4 2.4 0 0 1 1.704.706l3.588 3.588A2.4 2.4 0 0 1 20 8v12a2 2 0 0 1-2 2z\"></path><path d=\"M14 2v5a1 1 0 0 0 1 1h5\"></path><path d=\"M10 9H8\"></path><path d=\"M16 13H8\"></path><path d=\"M16 17H8\"></path></svg><span>text</span></span><button type=\"button\" class=\"code-block-copy\" aria-label=\"复制代码\" title=\"复制代码\"><svg class=\"code-block-copy-icon\" aria-hidden=\"true\" width=\"16\" height=\"16\" viewbox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" xmlns=\"http://www.w3.org/2000/svg\"><rect width=\"14\" height=\"14\" x=\"8\" y=\"8\" rx=\"2\" ry=\"2\"></rect><path d=\"M4 16c-1.1 0-2-.9-2-2V4c0-1.1.9-2 2-2h10c1.1 0 2 .9 2 2\"></path></svg></button></div><pre><code class=\"hljs language-text\">[char]0x5DE5 = 工\n[char]0x4F5C = 作\n[char]0x5B8C = 完\n[char]0x6210 = 成</code></pre></div><p>这样做有点丑，但可以绕开配置文件编码、终端代码页和多层引号带来的麻烦。<code>Speak()</code> 是同步调用，短句播完以后才退出；这比每次事件都 <code>Start-Process</code> 一个新的 PowerShell 稳定得多。</p>\n<p>只想换播报名称的话，把 <code>ChatGPT</code> 换成：</p>\n<div class=\"markdown-code\" data-language=\"powershell\"><div class=\"markdown-code-header\"><span class=\"markdown-code-language\"><svg class=\"code-language-icon\" aria-hidden=\"true\" width=\"16\" height=\"16\" viewbox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" xmlns=\"http://www.w3.org/2000/svg\"><path d=\"M12 19h8\"></path><path d=\"m4 17 6-6-6-6\"></path></svg><span>powershell</span></span><button type=\"button\" class=\"code-block-copy\" aria-label=\"复制代码\" title=\"复制代码\"><svg class=\"code-block-copy-icon\" aria-hidden=\"true\" width=\"16\" height=\"16\" viewbox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" xmlns=\"http://www.w3.org/2000/svg\"><rect width=\"14\" height=\"14\" x=\"8\" y=\"8\" rx=\"2\" ry=\"2\"></rect><path d=\"M4 16c-1.1 0-2-.9-2-2V4c0-1.1.9-2 2-2h10c1.1 0 2 .9 2 2\"></path></svg></button></div><pre><code class=\"hljs language-powershell\"><span class=\"hljs-string\">\"Claude Code\"</span></code></pre></div><p>或者：</p>\n<div class=\"markdown-code\" data-language=\"powershell\"><div class=\"markdown-code-header\"><span class=\"markdown-code-language\"><svg class=\"code-language-icon\" aria-hidden=\"true\" width=\"16\" height=\"16\" viewbox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" xmlns=\"http://www.w3.org/2000/svg\"><path d=\"M12 19h8\"></path><path d=\"m4 17 6-6-6-6\"></path></svg><span>powershell</span></span><button type=\"button\" class=\"code-block-copy\" aria-label=\"复制代码\" title=\"复制代码\"><svg class=\"code-block-copy-icon\" aria-hidden=\"true\" width=\"16\" height=\"16\" viewbox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" xmlns=\"http://www.w3.org/2000/svg\"><rect width=\"14\" height=\"14\" x=\"8\" y=\"8\" rx=\"2\" ry=\"2\"></rect><path d=\"M4 16c-1.1 0-2-.9-2-2V4c0-1.1.9-2 2-2h10c1.1 0 2 .9 2 2\"></path></svg></button></div><pre><code class=\"hljs language-powershell\"><span class=\"hljs-string\">\"Open Code\"</span></code></pre></div><h2 id=\"为什么要用-encodedcommand\">为什么要用 EncodedCommand</h2><p>把 PowerShell 直接塞进 JSON 或 TypeScript，容易撞上几层解析器：</p>\n<ul>\n<li>JSON 自己要处理双引号；</li>\n<li>PowerShell 里的 <code>$tts</code> 可能被外层提前展开；</li>\n<li>TypeScript 模板字符串还会再处理一遍反引号和插值；</li>\n<li>Bash、cmd、PowerShell 对引号和转义的规则不一样；</li>\n<li>Hook 如果误读 stdin，会一直等输入，最后看起来像卡死。</li>\n</ul>\n<p>所以真正放进配置文件的命令使用：</p>\n<div class=\"markdown-code\" data-language=\"powershell\"><div class=\"markdown-code-header\"><span class=\"markdown-code-language\"><svg class=\"code-language-icon\" aria-hidden=\"true\" width=\"16\" height=\"16\" viewbox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" xmlns=\"http://www.w3.org/2000/svg\"><path d=\"M12 19h8\"></path><path d=\"m4 17 6-6-6-6\"></path></svg><span>powershell</span></span><button type=\"button\" class=\"code-block-copy\" aria-label=\"复制代码\" title=\"复制代码\"><svg class=\"code-block-copy-icon\" aria-hidden=\"true\" width=\"16\" height=\"16\" viewbox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" xmlns=\"http://www.w3.org/2000/svg\"><rect width=\"14\" height=\"14\" x=\"8\" y=\"8\" rx=\"2\" ry=\"2\"></rect><path d=\"M4 16c-1.1 0-2-.9-2-2V4c0-1.1.9-2 2-2h10c1.1 0 2 .9 2 2\"></path></svg></button></div><pre><code class=\"hljs language-powershell\">powershell.exe <span class=\"hljs-literal\">-NoProfile</span> <span class=\"hljs-literal\">-NonInteractive</span> <span class=\"hljs-literal\">-EncodedCommand</span> &lt;BASE64字符串&gt;</code></pre></div><p><code>EncodedCommand</code> 不是普通 UTF-8 Base64，它要求先用 UTF-16LE（PowerShell 里的 <code>[Text.Encoding]::Unicode</code>）编码。</p>\n<p>用下面的生成器得到编码字符串：</p>\n<div class=\"markdown-code\" data-language=\"powershell\"><div class=\"markdown-code-header\"><span class=\"markdown-code-language\"><svg class=\"code-language-icon\" aria-hidden=\"true\" width=\"16\" height=\"16\" viewbox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" xmlns=\"http://www.w3.org/2000/svg\"><path d=\"M12 19h8\"></path><path d=\"m4 17 6-6-6-6\"></path></svg><span>powershell</span></span><button type=\"button\" class=\"code-block-copy\" aria-label=\"复制代码\" title=\"复制代码\"><svg class=\"code-block-copy-icon\" aria-hidden=\"true\" width=\"16\" height=\"16\" viewbox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" xmlns=\"http://www.w3.org/2000/svg\"><rect width=\"14\" height=\"14\" x=\"8\" y=\"8\" rx=\"2\" ry=\"2\"></rect><path d=\"M4 16c-1.1 0-2-.9-2-2V4c0-1.1.9-2 2-2h10c1.1 0 2 .9 2 2\"></path></svg></button></div><pre><code class=\"hljs language-powershell\"><span class=\"hljs-variable\">$label</span> = <span class=\"hljs-string\">'ChatGPT'</span>\n\n<span class=\"hljs-variable\">$ttsCommand</span> = <span class=\"hljs-string\">'Add-Type -AssemblyName System.Speech; $tts = New-Object System.Speech.Synthesis.SpeechSynthesizer; try { $tts.Speak((\"'</span> + <span class=\"hljs-variable\">$label</span> + <span class=\"hljs-string\">'\" + [char]0x5DE5 + [char]0x4F5C + [char]0x5B8C + [char]0x6210)) } finally { $tts.Dispose() }'</span>\n\n[<span class=\"hljs-type\">Convert</span>]::ToBase64String(\n    [<span class=\"hljs-type\">Text.Encoding</span>]::Unicode.GetBytes(<span class=\"hljs-variable\">$ttsCommand</span>)\n)</code></pre></div><p>分别把 <code>$label</code> 改成 <code>ChatGPT</code>、<code>Claude Code</code>、<code>Open Code</code>，保存三份输出。不要手动折行，也不要把尖括号一起填进配置。</p>\n<h2 id=\"给-codex-加语音提醒\">给 Codex 加语音提醒</h2><h3 id=\"配置文件\">配置文件</h3><p>用户级配置一般在：</p>\n<div class=\"markdown-code\" data-language=\"text\"><div class=\"markdown-code-header\"><span class=\"markdown-code-language\"><svg class=\"code-language-icon\" aria-hidden=\"true\" width=\"16\" height=\"16\" viewbox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" xmlns=\"http://www.w3.org/2000/svg\"><path d=\"M6 22a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h8a2.4 2.4 0 0 1 1.704.706l3.588 3.588A2.4 2.4 0 0 1 20 8v12a2 2 0 0 1-2 2z\"></path><path d=\"M14 2v5a1 1 0 0 0 1 1h5\"></path><path d=\"M10 9H8\"></path><path d=\"M16 13H8\"></path><path d=\"M16 17H8\"></path></svg><span>text</span></span><button type=\"button\" class=\"code-block-copy\" aria-label=\"复制代码\" title=\"复制代码\"><svg class=\"code-block-copy-icon\" aria-hidden=\"true\" width=\"16\" height=\"16\" viewbox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" xmlns=\"http://www.w3.org/2000/svg\"><rect width=\"14\" height=\"14\" x=\"8\" y=\"8\" rx=\"2\" ry=\"2\"></rect><path d=\"M4 16c-1.1 0-2-.9-2-2V4c0-1.1.9-2 2-2h10c1.1 0 2 .9 2 2\"></path></svg></button></div><pre><code class=\"hljs language-text\">%USERPROFILE%\\.codex\\hooks.json</code></pre></div><p>当前 Codex 也支持在 <code>config.toml</code> 里写内联 Hook；如果同一层同时存在两种写法，文档建议只保留一种。这里沿用更容易搬运的 <code>hooks.json</code>。</p>\n<h3 id=\"配置结构\">配置结构</h3><p>把 <code>&lt;CHATGPT_TTS_BASE64&gt;</code> 换成刚才生成的字符串：</p>\n<div class=\"markdown-code\" data-language=\"json\"><div class=\"markdown-code-header\"><span class=\"markdown-code-language\"><svg class=\"code-language-icon\" aria-hidden=\"true\" width=\"16\" height=\"16\" viewbox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" xmlns=\"http://www.w3.org/2000/svg\"><path d=\"M8 3H7a2 2 0 0 0-2 2v5a2 2 0 0 1-2 2 2 2 0 0 1 2 2v5c0 1.1.9 2 2 2h1\"></path><path d=\"M16 21h1a2 2 0 0 0 2-2v-5c0-1.1.9-2 2-2a2 2 0 0 0-2-2V5a2 2 0 0 0-2-2h-1\"></path></svg><span>json</span></span><button type=\"button\" class=\"code-block-copy\" aria-label=\"复制代码\" title=\"复制代码\"><svg class=\"code-block-copy-icon\" aria-hidden=\"true\" width=\"16\" height=\"16\" viewbox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" xmlns=\"http://www.w3.org/2000/svg\"><rect width=\"14\" height=\"14\" x=\"8\" y=\"8\" rx=\"2\" ry=\"2\"></rect><path d=\"M4 16c-1.1 0-2-.9-2-2V4c0-1.1.9-2 2-2h10c1.1 0 2 .9 2 2\"></path></svg></button></div><pre><code class=\"hljs language-json\"><span class=\"hljs-punctuation\">{</span>\n  <span class=\"hljs-attr\">\"description\"</span><span class=\"hljs-punctuation\">:</span> <span class=\"hljs-string\">\"Speak when a Codex agent turn stops.\"</span><span class=\"hljs-punctuation\">,</span>\n  <span class=\"hljs-attr\">\"hooks\"</span><span class=\"hljs-punctuation\">:</span> <span class=\"hljs-punctuation\">{</span>\n    <span class=\"hljs-attr\">\"Stop\"</span><span class=\"hljs-punctuation\">:</span> <span class=\"hljs-punctuation\">[</span>\n      <span class=\"hljs-punctuation\">{</span>\n        <span class=\"hljs-attr\">\"hooks\"</span><span class=\"hljs-punctuation\">:</span> <span class=\"hljs-punctuation\">[</span>\n          <span class=\"hljs-punctuation\">{</span>\n            <span class=\"hljs-attr\">\"type\"</span><span class=\"hljs-punctuation\">:</span> <span class=\"hljs-string\">\"command\"</span><span class=\"hljs-punctuation\">,</span>\n            <span class=\"hljs-attr\">\"command\"</span><span class=\"hljs-punctuation\">:</span> <span class=\"hljs-string\">\"powershell.exe -NoProfile -NonInteractive -EncodedCommand &lt;CHATGPT_TTS_BASE64&gt;\"</span><span class=\"hljs-punctuation\">,</span>\n            <span class=\"hljs-attr\">\"commandWindows\"</span><span class=\"hljs-punctuation\">:</span> <span class=\"hljs-string\">\"powershell.exe -NoProfile -NonInteractive -EncodedCommand &lt;CHATGPT_TTS_BASE64&gt;\"</span><span class=\"hljs-punctuation\">,</span>\n            <span class=\"hljs-attr\">\"timeout\"</span><span class=\"hljs-punctuation\">:</span> <span class=\"hljs-number\">10</span>\n          <span class=\"hljs-punctuation\">}</span>\n        <span class=\"hljs-punctuation\">]</span>\n      <span class=\"hljs-punctuation\">}</span>\n    <span class=\"hljs-punctuation\">]</span>\n  <span class=\"hljs-punctuation\">}</span>\n<span class=\"hljs-punctuation\">}</span></code></pre></div><p><code>commandWindows</code> 是 Windows 专用覆盖；在纯 Windows 配置里保留 <code>command</code> 作为回退也没关系。</p>\n<h3 id=\"这里最容易忘的两件事\">这里最容易忘的两件事</h3><p>第一，如果 <code>config.toml</code> 里还留着旧的 <code>notify = [...]</code>，或者你自己写过 <code>turn-ended</code>、后台 worker 一类的完成提醒，不要让它和 <code>Stop</code> 同时播报。同一件事只保留一个声音来源。</p>\n<p>第二，修改 Hook 后不是保存文件就结束。重启 Codex，在 <code>/hooks</code> 里重新检查并信任这个变更。当前 Codex 会按 Hook 定义的哈希记录信任状态，改过的命令可能会被标记为需要重新审核。</p>\n<p>这个 TTS 命令不会读取 stdin，也不会向 stdout 输出普通文本。这样既不会因为等待输入卡住，也不会把无效文本塞进 <code>Stop</code> 事件的输出通道。</p>\n<h2 id=\"给-claude-code-加语音提醒\">给 Claude Code 加语音提醒</h2><h3 id=\"配置文件-2\">配置文件</h3><div class=\"markdown-code\" data-language=\"text\"><div class=\"markdown-code-header\"><span class=\"markdown-code-language\"><svg class=\"code-language-icon\" aria-hidden=\"true\" width=\"16\" height=\"16\" viewbox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" xmlns=\"http://www.w3.org/2000/svg\"><path d=\"M6 22a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h8a2.4 2.4 0 0 1 1.704.706l3.588 3.588A2.4 2.4 0 0 1 20 8v12a2 2 0 0 1-2 2z\"></path><path d=\"M14 2v5a1 1 0 0 0 1 1h5\"></path><path d=\"M10 9H8\"></path><path d=\"M16 13H8\"></path><path d=\"M16 17H8\"></path></svg><span>text</span></span><button type=\"button\" class=\"code-block-copy\" aria-label=\"复制代码\" title=\"复制代码\"><svg class=\"code-block-copy-icon\" aria-hidden=\"true\" width=\"16\" height=\"16\" viewbox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" xmlns=\"http://www.w3.org/2000/svg\"><rect width=\"14\" height=\"14\" x=\"8\" y=\"8\" rx=\"2\" ry=\"2\"></rect><path d=\"M4 16c-1.1 0-2-.9-2-2V4c0-1.1.9-2 2-2h10c1.1 0 2 .9 2 2\"></path></svg></button></div><pre><code class=\"hljs language-text\">%USERPROFILE%\\.claude\\settings.json</code></pre></div><p>如果原来已经有 <code>Stop</code> Hook，不要把整个文件覆盖掉，只替换原来播放 Beep 的那一项 <code>command</code>。</p>\n<p>例如：</p>\n<div class=\"markdown-code\" data-language=\"json\"><div class=\"markdown-code-header\"><span class=\"markdown-code-language\"><svg class=\"code-language-icon\" aria-hidden=\"true\" width=\"16\" height=\"16\" viewbox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" xmlns=\"http://www.w3.org/2000/svg\"><path d=\"M8 3H7a2 2 0 0 0-2 2v5a2 2 0 0 1-2 2 2 2 0 0 1 2 2v5c0 1.1.9 2 2 2h1\"></path><path d=\"M16 21h1a2 2 0 0 0 2-2v-5c0-1.1.9-2 2-2a2 2 0 0 0-2-2V5a2 2 0 0 0-2-2h-1\"></path></svg><span>json</span></span><button type=\"button\" class=\"code-block-copy\" aria-label=\"复制代码\" title=\"复制代码\"><svg class=\"code-block-copy-icon\" aria-hidden=\"true\" width=\"16\" height=\"16\" viewbox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" xmlns=\"http://www.w3.org/2000/svg\"><rect width=\"14\" height=\"14\" x=\"8\" y=\"8\" rx=\"2\" ry=\"2\"></rect><path d=\"M4 16c-1.1 0-2-.9-2-2V4c0-1.1.9-2 2-2h10c1.1 0 2 .9 2 2\"></path></svg></button></div><pre><code class=\"hljs language-json\"><span class=\"hljs-punctuation\">{</span>\n  <span class=\"hljs-attr\">\"hooks\"</span><span class=\"hljs-punctuation\">:</span> <span class=\"hljs-punctuation\">{</span>\n    <span class=\"hljs-attr\">\"Stop\"</span><span class=\"hljs-punctuation\">:</span> <span class=\"hljs-punctuation\">[</span>\n      <span class=\"hljs-punctuation\">{</span>\n        <span class=\"hljs-attr\">\"hooks\"</span><span class=\"hljs-punctuation\">:</span> <span class=\"hljs-punctuation\">[</span>\n          <span class=\"hljs-punctuation\">{</span>\n            <span class=\"hljs-attr\">\"async\"</span><span class=\"hljs-punctuation\">:</span> <span class=\"hljs-literal\"><span class=\"hljs-keyword\">true</span></span><span class=\"hljs-punctuation\">,</span>\n            <span class=\"hljs-attr\">\"command\"</span><span class=\"hljs-punctuation\">:</span> <span class=\"hljs-string\">\"powershell.exe -NoProfile -NonInteractive -EncodedCommand &lt;CLAUDE_CODE_TTS_BASE64&gt;\"</span><span class=\"hljs-punctuation\">,</span>\n            <span class=\"hljs-attr\">\"timeout\"</span><span class=\"hljs-punctuation\">:</span> <span class=\"hljs-number\">5</span><span class=\"hljs-punctuation\">,</span>\n            <span class=\"hljs-attr\">\"type\"</span><span class=\"hljs-punctuation\">:</span> <span class=\"hljs-string\">\"command\"</span>\n          <span class=\"hljs-punctuation\">}</span>\n        <span class=\"hljs-punctuation\">]</span>\n      <span class=\"hljs-punctuation\">}</span>\n    <span class=\"hljs-punctuation\">]</span>\n  <span class=\"hljs-punctuation\">}</span>\n<span class=\"hljs-punctuation\">}</span></code></pre></div><p>这里保留了 <code>async: true</code>，因为播报一句短提示没有必要阻塞 Claude Code。代价是：如果连续触发多个 <code>Stop</code>，语音可能重叠。只配置一个短句通常够用；如果你需要绝对不重叠，就要额外做互斥或单实例队列。</p>\n<p>Claude Code 的 Stop Hook 不会在用户主动中断时触发，API 错误则会走 <code>StopFailure</code>，所以它表达的是“正常完成了一次响应”，不是“所有情况都收到了完成通知”。</p>\n<h2 id=\"给-opencode-加语音提醒\">给 OpenCode 加语音提醒</h2><p>OpenCode 的完成提示是插件逻辑，不是 <code>opencode.json</code> 里的普通命令模板。</p>\n<h3 id=\"插件文件\">插件文件</h3><p>推荐放在：</p>\n<div class=\"markdown-code\" data-language=\"text\"><div class=\"markdown-code-header\"><span class=\"markdown-code-language\"><svg class=\"code-language-icon\" aria-hidden=\"true\" width=\"16\" height=\"16\" viewbox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" xmlns=\"http://www.w3.org/2000/svg\"><path d=\"M6 22a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h8a2.4 2.4 0 0 1 1.704.706l3.588 3.588A2.4 2.4 0 0 1 20 8v12a2 2 0 0 1-2 2z\"></path><path d=\"M14 2v5a1 1 0 0 0 1 1h5\"></path><path d=\"M10 9H8\"></path><path d=\"M16 13H8\"></path><path d=\"M16 17H8\"></path></svg><span>text</span></span><button type=\"button\" class=\"code-block-copy\" aria-label=\"复制代码\" title=\"复制代码\"><svg class=\"code-block-copy-icon\" aria-hidden=\"true\" width=\"16\" height=\"16\" viewbox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" xmlns=\"http://www.w3.org/2000/svg\"><rect width=\"14\" height=\"14\" x=\"8\" y=\"8\" rx=\"2\" ry=\"2\"></rect><path d=\"M4 16c-1.1 0-2-.9-2-2V4c0-1.1.9-2 2-2h10c1.1 0 2 .9 2 2\"></path></svg></button></div><pre><code class=\"hljs language-text\">%USERPROFILE%\\.config\\opencode\\plugins\\done-sound.ts</code></pre></div><p>当前 OpenCode 会自动加载这个目录下的本地插件。项目级插件则放进项目的：</p>\n<div class=\"markdown-code\" data-language=\"text\"><div class=\"markdown-code-header\"><span class=\"markdown-code-language\"><svg class=\"code-language-icon\" aria-hidden=\"true\" width=\"16\" height=\"16\" viewbox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" xmlns=\"http://www.w3.org/2000/svg\"><path d=\"M6 22a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h8a2.4 2.4 0 0 1 1.704.706l3.588 3.588A2.4 2.4 0 0 1 20 8v12a2 2 0 0 1-2 2z\"></path><path d=\"M14 2v5a1 1 0 0 0 1 1h5\"></path><path d=\"M10 9H8\"></path><path d=\"M16 13H8\"></path><path d=\"M16 17H8\"></path></svg><span>text</span></span><button type=\"button\" class=\"code-block-copy\" aria-label=\"复制代码\" title=\"复制代码\"><svg class=\"code-block-copy-icon\" aria-hidden=\"true\" width=\"16\" height=\"16\" viewbox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" xmlns=\"http://www.w3.org/2000/svg\"><rect width=\"14\" height=\"14\" x=\"8\" y=\"8\" rx=\"2\" ry=\"2\"></rect><path d=\"M4 16c-1.1 0-2-.9-2-2V4c0-1.1.9-2 2-2h10c1.1 0 2 .9 2 2\"></path></svg></button></div><pre><code class=\"hljs language-text\">.opencode\\plugins\\done-sound.ts</code></pre></div><p>把生成好的编码字符串填到 <code>TTS_BASE64</code>：</p>\n<div class=\"markdown-code\" data-language=\"typescript\"><div class=\"markdown-code-header\"><span class=\"markdown-code-language\"><svg class=\"code-language-icon\" aria-hidden=\"true\" width=\"16\" height=\"16\" viewbox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" xmlns=\"http://www.w3.org/2000/svg\"><path d=\"M8 3H7a2 2 0 0 0-2 2v5a2 2 0 0 1-2 2 2 2 0 0 1 2 2v5c0 1.1.9 2 2 2h1\"></path><path d=\"M16 21h1a2 2 0 0 0 2-2v-5c0-1.1.9-2 2-2a2 2 0 0 0-2-2V5a2 2 0 0 0-2-2h-1\"></path></svg><span>typescript</span></span><button type=\"button\" class=\"code-block-copy\" aria-label=\"复制代码\" title=\"复制代码\"><svg class=\"code-block-copy-icon\" aria-hidden=\"true\" width=\"16\" height=\"16\" viewbox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" xmlns=\"http://www.w3.org/2000/svg\"><rect width=\"14\" height=\"14\" x=\"8\" y=\"8\" rx=\"2\" ry=\"2\"></rect><path d=\"M4 16c-1.1 0-2-.9-2-2V4c0-1.1.9-2 2-2h10c1.1 0 2 .9 2 2\"></path></svg></button></div><pre><code class=\"hljs language-typescript\"><span class=\"hljs-keyword\">const</span> <span class=\"hljs-variable constant_\">TTS_BASE64</span> = <span class=\"hljs-string\">\"&lt;OPEN_CODE_TTS_BASE64&gt;\"</span>\n\n<span class=\"hljs-keyword\">export</span> <span class=\"hljs-keyword\">const</span> <span class=\"hljs-title function_\">DoneSound</span> = <span class=\"hljs-keyword\">async</span> (<span class=\"hljs-params\">{ $ }</span>) =&gt; {\n  <span class=\"hljs-keyword\">return</span> {\n    <span class=\"hljs-attr\">event</span>: <span class=\"hljs-title function_\">async</span> ({ event }) =&gt; {\n      <span class=\"hljs-keyword\">if</span> (event.<span class=\"hljs-property\">type</span> !== <span class=\"hljs-string\">\"session.idle\"</span>) <span class=\"hljs-keyword\">return</span>\n\n      <span class=\"hljs-keyword\">await</span> $<span class=\"hljs-string\">`powershell.exe -NoProfile -NonInteractive -EncodedCommand <span class=\"hljs-subst\">${TTS_BASE64}</span>`</span>\n    },\n  }\n}</code></pre></div><p>这里通过 Bun 的 <code>$</code> Shell API 执行 PowerShell，并把 Base64 作为插值传入，避免在 TypeScript 模板字符串里再手动处理引号。</p>\n<p>如果你使用的是旧版本，已经在 <code>opencode.json</code> 里显式注册了本地插件路径，可以继续保留原来的注册方式；但不要把下面这种普通命令模板当成事件 Hook：</p>\n<div class=\"markdown-code\" data-language=\"json\"><div class=\"markdown-code-header\"><span class=\"markdown-code-language\"><svg class=\"code-language-icon\" aria-hidden=\"true\" width=\"16\" height=\"16\" viewbox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" xmlns=\"http://www.w3.org/2000/svg\"><path d=\"M8 3H7a2 2 0 0 0-2 2v5a2 2 0 0 1-2 2 2 2 0 0 1 2 2v5c0 1.1.9 2 2 2h1\"></path><path d=\"M16 21h1a2 2 0 0 0 2-2v-5c0-1.1.9-2 2-2a2 2 0 0 0-2-2V5a2 2 0 0 0-2-2h-1\"></path></svg><span>json</span></span><button type=\"button\" class=\"code-block-copy\" aria-label=\"复制代码\" title=\"复制代码\"><svg class=\"code-block-copy-icon\" aria-hidden=\"true\" width=\"16\" height=\"16\" viewbox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" xmlns=\"http://www.w3.org/2000/svg\"><rect width=\"14\" height=\"14\" x=\"8\" y=\"8\" rx=\"2\" ry=\"2\"></rect><path d=\"M4 16c-1.1 0-2-.9-2-2V4c0-1.1.9-2 2-2h10c1.1 0 2 .9 2 2\"></path></svg></button></div><pre><code class=\"hljs language-json\"><span class=\"hljs-punctuation\">{</span>\n  <span class=\"hljs-attr\">\"command\"</span><span class=\"hljs-punctuation\">:</span> <span class=\"hljs-punctuation\">{</span>\n    <span class=\"hljs-attr\">\"done\"</span><span class=\"hljs-punctuation\">:</span> <span class=\"hljs-punctuation\">{</span>\n      <span class=\"hljs-attr\">\"description\"</span><span class=\"hljs-punctuation\">:</span> <span class=\"hljs-string\">\"播放任务完成提示音\"</span><span class=\"hljs-punctuation\">,</span>\n      <span class=\"hljs-attr\">\"template\"</span><span class=\"hljs-punctuation\">:</span> <span class=\"hljs-string\">\"...\"</span>\n    <span class=\"hljs-punctuation\">}</span>\n  <span class=\"hljs-punctuation\">}</span>\n<span class=\"hljs-punctuation\">}</span></code></pre></div><p>这只是一个可调用的命令模板。真正监听 <code>session.idle</code> 并执行 TTS 的，是 <code>plugins/done-sound.ts</code>。</p>\n<p>另外，OpenCode 桌面版本身也可能在响应完成或会话出错时发送系统通知。如果你已经打开了桌面通知，又启用了 TTS，最好先关掉其中一套，免得一次任务收到两份提醒。</p>\n<h2 id=\"我之前踩过的坑\">我之前踩过的坑</h2><h3 id=\"同时使用多个完成事件\">同时使用多个完成事件</h3><p>错误结构通常长这样：</p>\n<div class=\"markdown-code\" data-language=\"text\"><div class=\"markdown-code-header\"><span class=\"markdown-code-language\"><svg class=\"code-language-icon\" aria-hidden=\"true\" width=\"16\" height=\"16\" viewbox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" xmlns=\"http://www.w3.org/2000/svg\"><path d=\"M6 22a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h8a2.4 2.4 0 0 1 1.704.706l3.588 3.588A2.4 2.4 0 0 1 20 8v12a2 2 0 0 1-2 2z\"></path><path d=\"M14 2v5a1 1 0 0 0 1 1h5\"></path><path d=\"M10 9H8\"></path><path d=\"M16 13H8\"></path><path d=\"M16 17H8\"></path></svg><span>text</span></span><button type=\"button\" class=\"code-block-copy\" aria-label=\"复制代码\" title=\"复制代码\"><svg class=\"code-block-copy-icon\" aria-hidden=\"true\" width=\"16\" height=\"16\" viewbox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" xmlns=\"http://www.w3.org/2000/svg\"><rect width=\"14\" height=\"14\" x=\"8\" y=\"8\" rx=\"2\" ry=\"2\"></rect><path d=\"M4 16c-1.1 0-2-.9-2-2V4c0-1.1.9-2 2-2h10c1.1 0 2 .9 2 2\"></path></svg></button></div><pre><code class=\"hljs language-text\">notify → turn-ended → PowerShell worker → TTS</code></pre></div><p>同时又加了：</p>\n<div class=\"markdown-code\" data-language=\"text\"><div class=\"markdown-code-header\"><span class=\"markdown-code-language\"><svg class=\"code-language-icon\" aria-hidden=\"true\" width=\"16\" height=\"16\" viewbox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" xmlns=\"http://www.w3.org/2000/svg\"><path d=\"M6 22a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h8a2.4 2.4 0 0 1 1.704.706l3.588 3.588A2.4 2.4 0 0 1 20 8v12a2 2 0 0 1-2 2z\"></path><path d=\"M14 2v5a1 1 0 0 0 1 1h5\"></path><path d=\"M10 9H8\"></path><path d=\"M16 13H8\"></path><path d=\"M16 17H8\"></path></svg><span>text</span></span><button type=\"button\" class=\"code-block-copy\" aria-label=\"复制代码\" title=\"复制代码\"><svg class=\"code-block-copy-icon\" aria-hidden=\"true\" width=\"16\" height=\"16\" viewbox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" xmlns=\"http://www.w3.org/2000/svg\"><rect width=\"14\" height=\"14\" x=\"8\" y=\"8\" rx=\"2\" ry=\"2\"></rect><path d=\"M4 16c-1.1 0-2-.9-2-2V4c0-1.1.9-2 2-2h10c1.1 0 2 .9 2 2\"></path></svg></button></div><pre><code class=\"hljs language-text\">Stop Hook → TTS</code></pre></div><p>一次任务可能触发两条链路，最后不是“提醒完成”，而是电脑开始自言自语。先决定你想监听的是“响应结束”还是“会话空闲”，然后只留一条链。</p>\n<h3 id=\"把每个小回合当成整个任务完成\">把每个小回合当成整个任务完成</h3><p>多步骤 Agent 的真实过程经常是：</p>\n<div class=\"markdown-code\" data-language=\"text\"><div class=\"markdown-code-header\"><span class=\"markdown-code-language\"><svg class=\"code-language-icon\" aria-hidden=\"true\" width=\"16\" height=\"16\" viewbox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" xmlns=\"http://www.w3.org/2000/svg\"><path d=\"M6 22a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h8a2.4 2.4 0 0 1 1.704.706l3.588 3.588A2.4 2.4 0 0 1 20 8v12a2 2 0 0 1-2 2z\"></path><path d=\"M14 2v5a1 1 0 0 0 1 1h5\"></path><path d=\"M10 9H8\"></path><path d=\"M16 13H8\"></path><path d=\"M16 17H8\"></path></svg><span>text</span></span><button type=\"button\" class=\"code-block-copy\" aria-label=\"复制代码\" title=\"复制代码\"><svg class=\"code-block-copy-icon\" aria-hidden=\"true\" width=\"16\" height=\"16\" viewbox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" xmlns=\"http://www.w3.org/2000/svg\"><rect width=\"14\" height=\"14\" x=\"8\" y=\"8\" rx=\"2\" ry=\"2\"></rect><path d=\"M4 16c-1.1 0-2-.9-2-2V4c0-1.1.9-2 2-2h10c1.1 0 2 .9 2 2\"></path></svg></button></div><pre><code class=\"hljs language-text\">模型响应\n→ 工具调用\n→ 中间回合\n→ 继续推理\n→ 最终响应</code></pre></div><p>Codex 和 Claude Code 的 <code>Stop</code> 都更接近“当前回合停了”。如果你的要求是“复杂任务全部结束后只响一次”，就需要额外的去重、冷却时间或状态管理。OpenCode 的 <code>session.idle</code> 在语义上更接近这个目标，但也不等于跨所有版本的绝对一次性保证。</p>\n<h3 id=\"用-start-process-启动一堆语音进程\">用 Start-Process 启动一堆语音进程</h3><p>容易出问题的写法：</p>\n<div class=\"markdown-code\" data-language=\"powershell\"><div class=\"markdown-code-header\"><span class=\"markdown-code-language\"><svg class=\"code-language-icon\" aria-hidden=\"true\" width=\"16\" height=\"16\" viewbox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" xmlns=\"http://www.w3.org/2000/svg\"><path d=\"M12 19h8\"></path><path d=\"m4 17 6-6-6-6\"></path></svg><span>powershell</span></span><button type=\"button\" class=\"code-block-copy\" aria-label=\"复制代码\" title=\"复制代码\"><svg class=\"code-block-copy-icon\" aria-hidden=\"true\" width=\"16\" height=\"16\" viewbox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" xmlns=\"http://www.w3.org/2000/svg\"><rect width=\"14\" height=\"14\" x=\"8\" y=\"8\" rx=\"2\" ry=\"2\"></rect><path d=\"M4 16c-1.1 0-2-.9-2-2V4c0-1.1.9-2 2-2h10c1.1 0 2 .9 2 2\"></path></svg></button></div><pre><code class=\"hljs language-powershell\"><span class=\"hljs-built_in\">Start-Process</span> powershell.exe ...</code></pre></div><p>每个事件都开一个新进程，多个短句会重叠，旧进程还可能在下一次任务开始时继续说话。短提示直接调用：</p>\n<div class=\"markdown-code\" data-language=\"powershell\"><div class=\"markdown-code-header\"><span class=\"markdown-code-language\"><svg class=\"code-language-icon\" aria-hidden=\"true\" width=\"16\" height=\"16\" viewbox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" xmlns=\"http://www.w3.org/2000/svg\"><path d=\"M12 19h8\"></path><path d=\"m4 17 6-6-6-6\"></path></svg><span>powershell</span></span><button type=\"button\" class=\"code-block-copy\" aria-label=\"复制代码\" title=\"复制代码\"><svg class=\"code-block-copy-icon\" aria-hidden=\"true\" width=\"16\" height=\"16\" viewbox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" xmlns=\"http://www.w3.org/2000/svg\"><rect width=\"14\" height=\"14\" x=\"8\" y=\"8\" rx=\"2\" ry=\"2\"></rect><path d=\"M4 16c-1.1 0-2-.9-2-2V4c0-1.1.9-2 2-2h10c1.1 0 2 .9 2 2\"></path></svg></button></div><pre><code class=\"hljs language-powershell\"><span class=\"hljs-variable\">$tts</span>.Speak(<span class=\"hljs-string\">\"工作完成\"</span>)\n<span class=\"hljs-variable\">$tts</span>.Dispose()</code></pre></div><p>播完退出，生命周期最简单。</p>\n<h3 id=\"让-hook-读取-stdin\">让 Hook 读取 stdin</h3><p>如果你的语音命令根本不需要事件 JSON，就不要写：</p>\n<div class=\"markdown-code\" data-language=\"powershell\"><div class=\"markdown-code-header\"><span class=\"markdown-code-language\"><svg class=\"code-language-icon\" aria-hidden=\"true\" width=\"16\" height=\"16\" viewbox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" xmlns=\"http://www.w3.org/2000/svg\"><path d=\"M12 19h8\"></path><path d=\"m4 17 6-6-6-6\"></path></svg><span>powershell</span></span><button type=\"button\" class=\"code-block-copy\" aria-label=\"复制代码\" title=\"复制代码\"><svg class=\"code-block-copy-icon\" aria-hidden=\"true\" width=\"16\" height=\"16\" viewbox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" xmlns=\"http://www.w3.org/2000/svg\"><rect width=\"14\" height=\"14\" x=\"8\" y=\"8\" rx=\"2\" ry=\"2\"></rect><path d=\"M4 16c-1.1 0-2-.9-2-2V4c0-1.1.9-2 2-2h10c1.1 0 2 .9 2 2\"></path></svg></button></div><pre><code class=\"hljs language-powershell\">[<span class=\"hljs-type\">Console</span>]::In.ReadToEnd()</code></pre></div><p>Hook 的输入流什么时候结束并不总是符合你的直觉。只执行固定 TTS 时，读 stdin 没有任何收益，只有卡死的可能。</p>\n<h3 id=\"三层引号互相打架\">三层引号互相打架</h3><p>这种命令本身就已经经过了配置文件、外层 Shell 和 PowerShell 三层解析：</p>\n<div class=\"markdown-code\" data-language=\"powershell\"><div class=\"markdown-code-header\"><span class=\"markdown-code-language\"><svg class=\"code-language-icon\" aria-hidden=\"true\" width=\"16\" height=\"16\" viewbox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" xmlns=\"http://www.w3.org/2000/svg\"><path d=\"M12 19h8\"></path><path d=\"m4 17 6-6-6-6\"></path></svg><span>powershell</span></span><button type=\"button\" class=\"code-block-copy\" aria-label=\"复制代码\" title=\"复制代码\"><svg class=\"code-block-copy-icon\" aria-hidden=\"true\" width=\"16\" height=\"16\" viewbox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" xmlns=\"http://www.w3.org/2000/svg\"><rect width=\"14\" height=\"14\" x=\"8\" y=\"8\" rx=\"2\" ry=\"2\"></rect><path d=\"M4 16c-1.1 0-2-.9-2-2V4c0-1.1.9-2 2-2h10c1.1 0 2 .9 2 2\"></path></svg></button></div><pre><code class=\"hljs language-powershell\">powershell.exe <span class=\"hljs-literal\">-Command</span> <span class=\"hljs-string\">\"<span class=\"hljs-variable\">$tts</span>.Speak(...)\"</span></code></pre></div><p>外层 PowerShell 可能先把 <code>$tts</code> 展开，最后真正启动的进程拿到的是一条残缺命令。手动测试时可以用单引号包住 <code>-Command</code>，真正写进 Hook 时则统一用 <code>-EncodedCommand</code>。</p>\n<h2 id=\"推荐的实施顺序\">推荐的实施顺序</h2><ol>\n<li>先手动执行可读版本，确认能听到语音、播报内容正确、PowerShell 能正常返回。</li>\n<li>用 <code>$label</code> 生成对应的 UTF-16LE Base64。</li>\n<li>只替换现有 Hook 的 <code>command</code>，不要一边加新 Hook，一边保留旧的 Beep 链路。</li>\n<li>按工具重新加载：Codex 重启并在 <code>/hooks</code> 里重新信任；Claude Code 开新会话；OpenCode 重启以加载插件。</li>\n<li>只执行一次真实任务验证，别同时手动播放、手动触发 Hook、再跑一遍真实任务，否则很容易把三次声音误判成重复事件。</li>\n</ol>\n<h2 id=\"最后\">最后</h2><p>如果你的目标是“每次 AI 给出一个可见响应后提醒”：</p>\n<ul>\n<li>Codex 用 <code>Stop</code>；</li>\n<li>Claude Code 用 <code>Stop</code>；</li>\n<li>OpenCode 用 <code>session.idle</code>。</li>\n</ul>\n<p>如果你的目标是“整个复杂任务彻底结束后只提醒一次”，那就不要把 <code>Stop</code> 误当成任务级事件。Codex 和 Claude Code 需要额外去重，OpenCode 的 <code>session.idle</code> 更合适，但仍然应该避免同时启用桌面通知、插件通知和旧的 worker 链路。</p>\n<p>核心 PowerShell 结构始终就这么几行：</p>\n<div class=\"markdown-code\" data-language=\"powershell\"><div class=\"markdown-code-header\"><span class=\"markdown-code-language\"><svg class=\"code-language-icon\" aria-hidden=\"true\" width=\"16\" height=\"16\" viewbox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" xmlns=\"http://www.w3.org/2000/svg\"><path d=\"M12 19h8\"></path><path d=\"m4 17 6-6-6-6\"></path></svg><span>powershell</span></span><button type=\"button\" class=\"code-block-copy\" aria-label=\"复制代码\" title=\"复制代码\"><svg class=\"code-block-copy-icon\" aria-hidden=\"true\" width=\"16\" height=\"16\" viewbox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" xmlns=\"http://www.w3.org/2000/svg\"><rect width=\"14\" height=\"14\" x=\"8\" y=\"8\" rx=\"2\" ry=\"2\"></rect><path d=\"M4 16c-1.1 0-2-.9-2-2V4c0-1.1.9-2 2-2h10c1.1 0 2 .9 2 2\"></path></svg></button></div><pre><code class=\"hljs language-powershell\"><span class=\"hljs-built_in\">Add-Type</span> <span class=\"hljs-literal\">-AssemblyName</span> System.Speech\n<span class=\"hljs-variable\">$tts</span> = <span class=\"hljs-built_in\">New-Object</span> System.Speech.Synthesis.SpeechSynthesizer\n\n<span class=\"hljs-keyword\">try</span> {\n    <span class=\"hljs-variable\">$tts</span>.Speak((<span class=\"hljs-string\">\"应用名称\"</span> + [<span class=\"hljs-built_in\">char</span>]<span class=\"hljs-number\">0</span>x5DE5 + [<span class=\"hljs-built_in\">char</span>]<span class=\"hljs-number\">0</span>x4F5C + [<span class=\"hljs-built_in\">char</span>]<span class=\"hljs-number\">0</span>x5B8C + [<span class=\"hljs-built_in\">char</span>]<span class=\"hljs-number\">0</span>x6210))\n}\n<span class=\"hljs-keyword\">finally</span> {\n    <span class=\"hljs-variable\">$tts</span>.Dispose()\n}</code></pre></div><p>剩下的只是决定：<strong>什么时候算完成，以及你想让谁来喊。</strong></p>\n","toc":[{"id":"先说结论","text":"先说结论","depth":2},{"id":"三个工具的-完成-不是一回事","text":"三个工具的“完成”不是一回事","depth":2},{"id":"powershell-内置-tts","text":"PowerShell 内置 TTS","depth":2},{"id":"为什么要用-encodedcommand","text":"为什么要用 EncodedCommand","depth":2},{"id":"给-codex-加语音提醒","text":"给 Codex 加语音提醒","depth":2},{"id":"配置文件","text":"配置文件","depth":3},{"id":"配置结构","text":"配置结构","depth":3},{"id":"这里最容易忘的两件事","text":"这里最容易忘的两件事","depth":3},{"id":"给-claude-code-加语音提醒","text":"给 Claude Code 加语音提醒","depth":2},{"id":"配置文件-2","text":"配置文件","depth":3},{"id":"给-opencode-加语音提醒","text":"给 OpenCode 加语音提醒","depth":2},{"id":"插件文件","text":"插件文件","depth":3},{"id":"我之前踩过的坑","text":"我之前踩过的坑","depth":2},{"id":"同时使用多个完成事件","text":"同时使用多个完成事件","depth":3},{"id":"把每个小回合当成整个任务完成","text":"把每个小回合当成整个任务完成","depth":3},{"id":"用-start-process-启动一堆语音进程","text":"用 Start-Process 启动一堆语音进程","depth":3},{"id":"让-hook-读取-stdin","text":"让 Hook 读取 stdin","depth":3},{"id":"三层引号互相打架","text":"三层引号互相打架","depth":3},{"id":"推荐的实施顺序","text":"推荐的实施顺序","depth":2},{"id":"最后","text":"最后","depth":2}]}