import { readFileSync, existsSync } from 'fs'; import { logger } from '../utils/logger.js'; /** * Extract last message of specified role from transcript JSONL file * @param transcriptPath Path to transcript file * @param role 'user' or 'assistant' * @param stripSystemReminders Whether to remove tags (for assistant) */ export function extractLastMessage( transcriptPath: string, role: 'user' | 'assistant', stripSystemReminders: boolean = false ): string { if (!transcriptPath || !existsSync(transcriptPath)) { return ''; } try { const content = readFileSync(transcriptPath, 'utf-8').trim(); if (!content) return ''; const lines = content.split('\n'); for (let i = lines.length - 1; i >= 0; i--) { try { const line = JSON.parse(lines[i]); if (line.type === role && line.message?.content) { let text = ''; const msgContent = line.message.content; if (typeof msgContent === 'string') { text = msgContent; } else if (Array.isArray(msgContent)) { text = msgContent .filter((c: any) => c.type === 'text') .map((c: any) => c.text) .join('\n'); } if (stripSystemReminders) { text = text.replace(/[\s\S]*?<\/system-reminder>/g, ''); text = text.replace(/\n{3,}/g, '\n\n').trim(); } return text; } } catch { continue; } } } catch (error) { logger.error('HOOK', 'Failed to read transcript', { transcriptPath }, error as Error); } return ''; }