const fs = require('fs');

const file = 'src/modules/book-generator/langgraph-generator.ts';
let content = fs.readFileSync(file, 'utf8');

// 修复错误的字符串
content = content.replace(
  "const thinkEndIdx = cleanedStr.indexOf('>>>>>>>');",
  "const thinkEndIdx = cleanedStr.indexOf('>>>>>>>>' + ' endthink'.substring(0,0) + ''); // 搜索 ffffff"
);

// 实际上应该直接搜索 
content = content.replace(
  "const thinkEndIdx = cleanedStr.indexOf('>>>>>>>>' + ' endthink'.substring(0,0) + ''); // 搜索 ffffff",
  "const thinkEndIdx = cleanedStr.indexOf(String.fromCharCode(0x1f4e8) + ' end'); // 占位"
);

// 最简单的方式：直接替换整个块
content = content.replace(
  /    \/\/ 移除思考标签.*?const thinkEndIdx = cleanedStr\.indexOf\(.*?\);[\s\S]*?cleanedStr = cleanedStr\.substring\(thinkEndIdx \+ 8\)\.trim\(\);[\s\S]*?\}/,
  `    // 移除思考标签
    const endTag = String.fromCodePoint(0x1F4E8);  // 使用emoji占位，实际搜索
    const searchStr = '<' + 'thi' + 'nk' + ' end>';
    const thinkEndIdx = cleanedStr.indexOf(searchStr);
    if (thinkEndIdx !== -1) {
      cleanedStr = cleanedStr.substring(thinkEndIdx + searchStr.length).trim();
    }`
);

fs.writeFileSync(file, content, 'utf8');
console.log('✅ 修复完成');
