Skip to content

⛰️ LifeOS — The AI-powered Life Operating System that helps you hill climb to wherever you're going.

fix: Handle string/array content in stop-hook transcript parsing

smol compute7b3031e533af
diff --git a/.claude/hooks/stop-hook.ts b/.claude/hooks/stop-hook.ts
index 8182077e..bc71dfb7 100755
--- a/.claude/hooks/stop-hook.ts
+++ b/.claude/hooks/stop-hook.ts
@@ -144,6 +144,23 @@ interface VoicesConfig {
voices: Record<string, VoiceConfig>;
}
+// Helper to safely turn Claude content (string or array of blocks) into plain text
+function contentToText(content: any): string {
+ if (typeof content === 'string') return content;
+ if (Array.isArray(content)) {
+ return content
+ .map((c) => {
+ if (typeof c === 'string') return c;
+ if (c?.text) return c.text;
+ if (c?.content) return String(c.content);
+ return '';
+ })
+ .join(' ')
+ .trim();
+ }
+ return '';
+}
+
// Load voices configuration
let VOICE_CONFIG: VoicesConfig;
try {
@@ -325,8 +342,9 @@ async function main() {
if (entry.type === 'assistant' && entry.message?.content) {
// Check if this assistant message contains a Task tool_use
let foundTask = false;
- for (const content of entry.message.content) {
- if (content.type === 'tool_use' && content.name === 'Task') {
+ const contentArray = Array.isArray(entry.message.content) ? entry.message.content : [entry.message.content];
+ for (const content of contentArray) {
+ if (content?.type === 'tool_use' && content.name === 'Task') {
// This is an agent task - find its result
foundTask = true;
agentType = content.input?.subagent_type || '';
@@ -335,9 +353,12 @@ async function main() {
for (let j = i + 1; j < lines.length; j++) {
const resultEntry = JSON.parse(lines[j]);
if (resultEntry.type === 'user' && resultEntry.message?.content) {
- for (const resultContent of resultEntry.message.content) {
- if (resultContent.type === 'tool_result' && resultContent.tool_use_id === content.id) {
- taskResult = resultContent.content;
+ const resultContentArray = Array.isArray(resultEntry.message.content)
+ ? resultEntry.message.content
+ : [resultEntry.message.content];
+ for (const resultContent of resultContentArray) {
+ if (resultContent?.type === 'tool_result' && resultContent.tool_use_id === content.id) {
+ taskResult = contentToText(resultContent.content);
isAgentTask = true;
break;
}
@@ -367,7 +388,7 @@ async function main() {
try {
const entry = JSON.parse(lastResponse);
if (entry.type === 'assistant' && entry.message?.content) {
- const content = entry.message.content.map(c => c.text || '').join(' ');
+ const content = contentToText(entry.message.content);
// First, look for CUSTOM COMPLETED line (voice-optimized)
const customCompletedMatch = content.match(/🗣️\s*CUSTOM\s+COMPLETED:\s*(.+?)(?:\n|$)/im);
@@ -473,12 +494,20 @@ async function main() {
// FIRST: Send voice notification if we have a message
if (message) {
- // Send to voice server with both voice name and speech rate
+ // Align voice payload with initialize-pai-session.ts (prefer voice_id)
+ const voiceId = process.env.DA_VOICE_ID || 'default-voice-id';
+ const priority = 'low';
+ // Send to voice server
await fetch('http://localhost:8888/notify', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
- message: message,
+ title: 'Completion',
+ message,
+ voice_enabled: true,
+ priority,
+ voice_id: voiceId,
+ // keep legacy fields for compatibility with voice server configs that use names/rates
voice_name: voiceConfig.voice_name,
rate: voiceConfig.rate_wpm
})
@@ -497,7 +526,7 @@ async function main() {
const lastResponse = lines[lines.length - 1];
const entry = JSON.parse(lastResponse);
if (entry.type === 'assistant' && entry.message?.content) {
- const content = entry.message.content.map(c => c.text || '').join(' ');
+ const content = contentToText(entry.message.content);
const completedMatch = content.match(/🎯\s*COMPLETED:\s*(.+?)(?:\n|$)/im);
if (completedMatch) {
tabTitle = completedMatch[1].trim()
@@ -545,4 +574,4 @@ async function main() {
console.error(`🎬 STOP-HOOK COMPLETED SUCCESSFULLY at ${new Date().toISOString()}\n`);
}
-main().catch(() => {});
\ No newline at end of file
+main().catch(() => {});