All files / modules/book-generator/parsers outline.parser.ts

0% Statements 0/57
100% Branches 1/1
100% Functions 1/1
0% Lines 0/57

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87                                                                                                                                                                             
/**
 * 大纲解析器
 * 容错解析LLM返回的大纲JSON
 */
 
export interface OutlineData {
  mainTheme: string;
  structureLogic: string;
  recommendSubsections?: boolean;
  chapters: Array<{
    number: number;
    title: string;
    summary: string;
    keyPoints: string[];
    estimatedWords: number;
  }>;
}
 
export function parseOutline(jsonStr: string): OutlineData | null {
  if (!jsonStr || typeof jsonStr !== 'string') {
    console.error('[OutlineParser] 输入为空或非字符串');
    return null;
  }
 
  try {
    let data: any;
 
    // 策略1:直接解析(最理想情况,LLM 直接输出纯 JSON)
    let cleanedStr = jsonStr.trim();
    
    // 清理思考标签
    cleanedStr = cleanedStr.replace(/^[\s\S]*?<\/?blockquote>[\s\S]*/gi, '');
    const thinkEndIdx = cleanedStr.indexOf('</think>');
    if (thinkEndIdx !== -1) {
      cleanedStr = cleanedStr.substring(thinkEndIdx + 8).trim();
    }
    
    try {
      data = JSON.parse(cleanedStr);
    } catch {
      // 策略2:提取 JSON 对象(处理 LLM 加了 markdown 标记或多余文字)
      const match = cleanedStr.match(/\{[\s\S]*\}/);
      if (!match) {
        console.error('[OutlineParser] 未找到 JSON 对象');
        return null;
      }
      try {
        data = JSON.parse(match[0]);
      } catch (parseErr) {
        console.error('[OutlineParser] JSON 解析失败:', parseErr);
        return null;
      }
    }
 
    // 验证必要字段
    if (!data || typeof data !== 'object') {
      console.error('[OutlineParser] 解析结果非对象');
      return null;
    }
    if (!Array.isArray(data.chapters)) {
      console.error('[OutlineParser] chapters 字段缺失或非数组');
      // 尝试兼容:若顶层就是章节数组
      if (Array.isArray(data)) {
        data = { chapters: data };
      } else {
        return null;
      }
    }
 
    return {
      mainTheme: data.mainTheme || '主题待定',
      structureLogic: data.structureLogic || '由浅入深',
      recommendSubsections: data.recommendSubsections === true,
      chapters: data.chapters.map((c: any, i: number) => ({
        number: c.number || i + 1,
        title: c.title || `第${i + 1}章`,
        summary: typeof c.summary === 'string' ? c.summary : '',
        keyPoints: Array.isArray(c.keyPoints) ? c.keyPoints : [],
        estimatedWords: typeof c.estimatedWords === 'number' ? c.estimatedWords : 1000,
      })),
    };
  } catch (err) {
    console.error('[OutlineParser] 未知错误:', err);
    return null;
  }
}