subsection.parser.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /**
  2. * 小节解析器
  3. * 容错解析LLM返回的小节大纲JSON
  4. */
  5. export function parseSubsections(jsonStr: string): { subsections: any[] } | null {
  6. if (!jsonStr || typeof jsonStr !== 'string') {
  7. console.error('[SubsectionParser] 输入为空或非字符串');
  8. return null;
  9. }
  10. try {
  11. let data: any;
  12. let cleanedStr = jsonStr.trim();
  13. // 调试:打印前80个字符
  14. console.log('[SubsectionParser] 原始前80字符:', JSON.stringify(cleanedStr.substring(0, 80)));
  15. // 清理 JSON 字符串中的非法控制字符
  16. cleanedStr = cleanedStr.replace(/[\x00-\x1F\x7F]/g, (char) => {
  17. // 将控制字符替换为空格(换行符会导致JSON解析失败)
  18. return ' ';
  19. });
  20. // 移除 markdown 代码块标记
  21. cleanedStr = cleanedStr.replace(/```json\s*/gi, '').replace(/```\s*/g, '').trim();
  22. // 移除思考标签
  23. cleanedStr = cleanedStr.replace(/^[\s\S]*?<blockquote>\s*[\s\S]*?<\/blockquote>\s*/, '');
  24. // 如果以 开头,说明思考标签在JSON之前,需要移除
  25. // 修复:</think>是结束标签,</think>是开始标签
  26. const thinkEndIdx = cleanedStr.indexOf('</think>');
  27. if (thinkEndIdx !== -1) {
  28. cleanedStr = cleanedStr.substring(thinkEndIdx + 8).trim();
  29. }
  30. console.log('[SubsectionParser] 处理后前100字符:', JSON.stringify(cleanedStr.substring(0, 100)));
  31. try {
  32. data = JSON.parse(cleanedStr);
  33. } catch {
  34. // 使用括号计数法提取JSON对象
  35. const firstBrace = cleanedStr.indexOf('{');
  36. if (firstBrace === -1) {
  37. console.error('[SubsectionParser] 未找到 JSON 对象');
  38. return null;
  39. }
  40. let jsonCandidate = '';
  41. let depth = 0;
  42. let started = false;
  43. for (let i = firstBrace; i < cleanedStr.length; i++) {
  44. const ch = cleanedStr[i];
  45. if (ch === '{') {
  46. depth++;
  47. started = true;
  48. } else if (ch === '}') {
  49. depth--;
  50. }
  51. if (started) jsonCandidate += ch;
  52. if (started && depth === 0) break;
  53. }
  54. if (!jsonCandidate) {
  55. console.error('[SubsectionParser] JSON 对象提取失败');
  56. return null;
  57. }
  58. try {
  59. data = JSON.parse(jsonCandidate);
  60. } catch (parseErr) {
  61. console.error('[SubsectionParser] JSON 解析失败:', parseErr);
  62. return null;
  63. }
  64. }
  65. if (!data || typeof data !== 'object') {
  66. console.error('[SubsectionParser] 解析结果非对象');
  67. return null;
  68. }
  69. // 兼容:如果顶层就是数组
  70. if (Array.isArray(data)) {
  71. data = { subsections: data };
  72. }
  73. // 兼容多种字段名:subsections, Subsections, subsection_list 等
  74. let subsectionsArray = data.subsections || data.Subsections || data.subsection_list || data.section_subsections;
  75. if (!Array.isArray(subsectionsArray)) {
  76. console.error('[SubsectionParser] subsections 字段缺失或非数组');
  77. return null;
  78. }
  79. return {
  80. subsections: subsectionsArray.map((s: any, i: number) => ({
  81. number: s.number || i + 1,
  82. title: s.title || `第${i + 1}小节`,
  83. summary: typeof s.summary === 'string' ? s.summary : '',
  84. keyPoints: Array.isArray(s.keyPoints) ? s.keyPoints : [],
  85. estimatedWords: typeof s.estimatedWords === 'number' ? s.estimatedWords : 500,
  86. })),
  87. };
  88. } catch (err) {
  89. console.error('[SubsectionParser] 未知错误:', err);
  90. return null;
  91. }
  92. }