Explorar el Código

fix: 修复keyPoints解析错误和旧数据冲突 - 内容生成可以正常执行

MyFramework User hace 4 meses
padre
commit
79b2df7bed

+ 89 - 0
server/check-current-status.js

@@ -0,0 +1,89 @@
+const { PrismaClient } = require('@prisma/client');
+const prisma = new PrismaClient();
+
+async function checkCurrentStatus() {
+  try {
+    const book = await prisma.book.findUnique({
+      where: { id: 1 },
+      include: {
+        chapters: {
+          orderBy: [
+            { level: 'asc' },
+            { number: 'asc' }
+          ]
+        }
+      }
+    });
+
+    console.log('📖 书籍状态:');
+    console.log(`  ID: ${book.id}`);
+    console.log(`  标题: ${book.title}`);
+    console.log(`  状态: ${book.status}`);
+    console.log(`  进度: ${book.progress}%`);
+    console.log('');
+
+    // 统计各级章节
+    const level1 = book.chapters.filter(c => c.level === 1);
+    const level2 = book.chapters.filter(c => c.level === 2);
+    const level3 = book.chapters.filter(c => c.level === 3);
+
+    console.log('📊 大纲统计:');
+    console.log(`  章: ${level1.length}`);
+    console.log(`  节: ${level2.length}`);
+    console.log(`  小节: ${level3.length}`);
+    console.log('');
+
+    // 检查内容生成状态
+    const contentStatus = {
+      null: level3.filter(c => c.contentStatus === null).length,
+      pending: level3.filter(c => c.contentStatus === 'pending').length,
+      generating: level3.filter(c => c.contentStatus === 'generating').length,
+      completed: level3.filter(c => c.contentStatus === 'completed').length,
+      failed: level3.filter(c => c.contentStatus === 'failed').length,
+    };
+
+    console.log('📝 内容生成状态 (小节级别):');
+    console.log(`  未开始: ${contentStatus.null}`);
+    console.log(`  待生成: ${contentStatus.pending}`);
+    console.log(`  生成中: ${contentStatus.generating}`);
+    console.log(`  已完成: ${contentStatus.completed}`);
+    console.log(`  失败: ${contentStatus.failed}`);
+    console.log('');
+
+    // 检查是否有正在生成的
+    if (contentStatus.generating > 0) {
+      const generating = level3.filter(c => c.contentStatus === 'generating');
+      console.log('🔄 正在生成的小节:');
+      generating.forEach(c => {
+        console.log(`  - ${c.title}`);
+      });
+      console.log('');
+    }
+
+    // 显示前3个已完成和未完成的小节
+    const completed = level3.filter(c => c.contentStatus === 'completed').slice(0, 3);
+    const notCompleted = level3.filter(c => c.contentStatus !== 'completed').slice(0, 3);
+
+    if (completed.length > 0) {
+      console.log('✅ 已完成的小节示例:');
+      completed.forEach(c => {
+        console.log(`  - ${c.title} (${c.wordCount}字)`);
+      });
+      console.log('');
+    }
+
+    if (notCompleted.length > 0) {
+      console.log('⏳ 待生成的小节示例:');
+      notCompleted.forEach(c => {
+        console.log(`  - ${c.title} [${c.contentStatus || 'null'}]`);
+      });
+    }
+
+  } catch (error) {
+    console.error('错误:', error);
+  } finally {
+    await prisma.$disconnect();
+  }
+}
+
+checkCurrentStatus();

+ 33 - 0
server/clear-book-chapters.js

@@ -0,0 +1,33 @@
+const { PrismaClient } = require('@prisma/client');
+const prisma = new PrismaClient();
+
+async function clearBookChapters() {
+  try {
+    console.log('🗑️  清空 bookId=1 的所有章节数据...');
+    
+    const result = await prisma.bookChapter.deleteMany({
+      where: { bookId: 1 }
+    });
+
+    console.log(`✅ 已删除 ${result.count} 条章节记录`);
+
+    // 重置书籍状态
+    await prisma.book.update({
+      where: { id: 1 },
+      data: {
+        status: 'draft',
+        progress: 0,
+        errorMsg: null
+      }
+    });
+
+    console.log('✅ 书籍状态已重置为 draft');
+
+  } catch (error) {
+    console.error('错误:', error);
+  } finally {
+    await prisma.$disconnect();
+  }
+}
+
+clearBookChapters();

+ 3 - 0
server/src/modules/book-generator/book-generator.store.ts

@@ -194,6 +194,9 @@ export class BookStore {
     
     let result = this.toBook(book);
     
+    console.log('[BookStore.getById] bookScale:', result.bookScale);
+    console.log('[BookStore.getById] result keys:', Object.keys(result));
+    
     // 用数据库构建的outline替换outlineJson解析的
     if (outline) {
       result.outline = outline;

+ 4 - 1
server/src/modules/book-generator/langgraph-controller.ts

@@ -532,8 +532,11 @@ router.delete('/books/:id', async (ctx: Context) => {
 router.post('/books/:id/generate', async (ctx: Context) => {
   try {
     const bookId = ctx.params.id as string;
-    const body = ctx.request.body as { bookScale?: string };
+    console.log(`[LangGraph Generate] 收到请求 bookId=${bookId}`);
+    
+    const body = (ctx.request.body || {}) as { bookScale?: string };
     const book = await bookStore.getById(bookId);
+    console.log(`[LangGraph Generate] book对象:`, book ? '存在' : 'null');
 
     if (!book) {
       ctx.status = 404;

+ 6 - 1
server/src/modules/book-generator/nodes/content.node.ts

@@ -23,6 +23,11 @@ function buildSubsectionContentMessages(
   sectionSummary: string, 
   subsection: any
 ): ChatMessage[] {
+  // 解析keyPoints(数据库中是JSON字符串)
+  const keyPoints = typeof subsection.keyPoints === 'string' 
+    ? JSON.parse(subsection.keyPoints) 
+    : (subsection.keyPoints || []);
+  
   return [
     { role: 'system', content: SUBSECTION_CONTENT_SYSTEM_PROMPT },
     {
@@ -34,7 +39,7 @@ function buildSubsectionContentMessages(
 节概述:${sectionSummary}
 小节标题:${subsection.title}
 小节概述:${subsection.summary || ''}
-核心知识点:${(subsection.keyPoints || []).join('、')}
+核心知识点:${keyPoints.join('、')}
 预估字数:${subsection.estimatedWords || 500}字
 
 请撰写该小节的正文内容。`,