/**
 * suggest-compact.js — PostToolUse Hook
 * 工具呼叫達 50 次建議 /compact，之後每 25 次再提示。
 */
const fs = require('fs');
const os = require('os');
const path = require('path');
let d = '';
process.stdin.on('data', c => d += c);
process.stdin.on('end', () => {
  try {
    const sid = (process.env.CLAUDE_SESSION_ID || 'default').replace(/[^a-zA-Z0-9_-]/g, '_').substring(0, 64);
    const f = path.join(os.tmpdir(), `claude-tool-count-${sid}`);
    let count = 0;
    try { count = parseInt(fs.readFileSync(f, 'utf8')) || 0; } catch (e) {}
    count++;
    fs.writeFileSync(f, String(count));
    if (count === 50) process.stderr.write('[suggest-compact] context 偵測達 50 次工具呼叫，建議執行 /compact\n');
    else if (count > 50 && (count - 50) % 25 === 0) process.stderr.write('[suggest-compact] context 持續累積，建議 /compact\n');
  } catch (e) {}
  process.stdout.write(d);
});
