iTranslated by AI
Voice Explanation of Claude Code Task Completion
TL;DR
Add the following to ~/.claude/settings.json.
{
"hooks": {
"Stop": [
{
"matcher": "",
"hooks": [
{
"type": "command",
"command": "jq -r '.transcript_path' | xargs cat | jq -r 'select(.type == \"assistant\")' | jq -sr '.[-1].message.content[-1].text' | say"
}
]
}
]
}
}
Introduction
I think many people set up Claude Code to send notifications when a task is completed.
There are methods like configuring terminal-notifier or connecting VOICEVOX MCP to make Claude Code speak, but with the introduction of the Hooks feature, it has become easier to reliably receive notifications when a task is finished.
While it's fine to simply play a sound or a fixed message, this time I will set it up to read out the content of the work that was performed.
Hooks
Since Claude Code v1.0.38, the Hooks feature has become available.
This feature allows custom commands to be automatically executed at several points in the Claude Code lifecycle.
To run a command when a Claude Code task is completed, it's best to use the Stop event.
When the Stop hook fires, the command receives the following JSON via standard input:
{
"session_id": string; // Session ID
"transcript_path": string; // File path of the conversation transcript
"stop_hook_active": boolean; // Flag indicating whether the stop hook is already running
}
The key point is that the command can receive transcript_path.
By using this, you can obtain Claude Code's actual response content and pass it to text-to-speech.
You can configure it interactively by entering dialogue mode and running the /hooks command.
You can also add the configuration directly to the ~/.claude/settings.json file.
{
"hooks": {
"Stop": [
{
"matcher": "",
"hooks": [
{
"type": "command",
"command": "say 'タスクが完了しました'"
}
]
}
]
}
}
Retrieving Task Content
To read out the actual task content via audio notification, you need to obtain Claude Code's output.
Claude Code seems to save conversation history in JSON format in a log file, but since there's no detailed specification, I'll interpret it roughly.
jq -r '.transcript_path' | xargs cat | jq -r 'select(.type == "assistant")' | jq -sr '.[-1].message.content[-1].text'
A brief explanation:
- Get the log file path from standard input using
.transcript_path - Get the file content using
xargs cat - Extract only Claude's messages using
select(.type == "assistant") - Get the last response text using
.[-1].message.content[-1].text
I'm also using jq roughly, so there might be a better way. If you know, please let me know.
Text-to-Speech
On macOS, you can use the say command to read text aloud.
It's the same voice used by VoiceOver and similar features.
say "テキスト内容"
If you have Claude Code output in Japanese, it's a good idea to choose a voice that can read Japanese.
You can specify it with the -v option, or set the default voice via System Settings > Accessibility > Spoken Content > System Voice.
Bonus
The say command is good enough, but you can also combine it with voice synthesis engines like VOICEVOX or AivisSpeech to read out the content in any voice.
I use Style-Bert-VITS2 to have it speak in my preferred voice.
I've also made various other adjustments, resulting in something like the following:
{
"hooks": {
"Stop": [
{
"matcher": "",
"hooks": [
{
"type": "command",
"command": "afplay ~/.claude/assets/sound.mp3 & say \"Claude Code has notified that the task has been completed.\" && jq -r '.transcript_path' | xargs cat | jq -r 'select(.type == \"assistant\")' | jq -sr '.[-1].message.content[-1].text' | uvx --from sbv2-server tts"
}
]
}
]
}
}
This plays a sound, mechanically notifies that the task is complete, and then reads out the work content.
I place my preferred sound file at ~/.claude/assets/sound.mp3.
sbv2-server is a Python project I set up locally. This might be the essence of it, but I'll write about it later if I feel like it.
Summary
Claude Code now tells me the work content by voice when it finishes a task. This is absolutely amazing.
I also wrote this article with Claude Code, and being able to interact through voice really takes the enjoyment up a notch.
Actually, since it's so fun just talking normally, my productivity has dropped.
That's all from me.
Discussion