Преглед изворни кода

fix: 修复parentId为null时的upsert问题 - 改用find+update/create模式

MyFramework User пре 4 месеци
родитељ
комит
9186846684
2 измењених фајлова са 53 додато и 43 уклоњено
  1. 33 23
      server/src/modules/book-generator/book-generator.store.ts
  2. 20 20
      server/test-ai.js

+ 33 - 23
server/src/modules/book-generator/book-generator.store.ts

@@ -354,36 +354,46 @@ export class BookStore {
     estimatedWords?: number;
     estimatedWords?: number;
   }>): Promise<void> {
   }>): Promise<void> {
     const bookIdNum = parseInt(bookId);
     const bookIdNum = parseInt(bookId);
-    // 使用 upsert 避免唯一约束冲突
+    // 由于parentId为null不能作为复合唯一约束,改用find+upsert方式
     for (const c of chapters) {
     for (const c of chapters) {
-      await prisma.bookChapter.upsert({
+      // 先查找是否存在
+      const existing = await prisma.bookChapter.findFirst({
         where: {
         where: {
-          bookId_parentId_level_number: {
-            bookId: bookIdNum,
-            parentId: null,
-            level: 1,
-            number: c.number,
-          }
-        },
-        update: {
-          title: c.title,
-          summary: c.summary,
-          keyPoints: c.keyPoints ? JSON.stringify(c.keyPoints) : null,
-          estimatedWords: c.estimatedWords || 5000,
-          status: 'completed',
-        } as any,
-        create: {
           bookId: bookIdNum,
           bookId: bookIdNum,
           parentId: null,
           parentId: null,
           level: 1,
           level: 1,
           number: c.number,
           number: c.number,
-          title: c.title,
-          summary: c.summary,
-          keyPoints: c.keyPoints ? JSON.stringify(c.keyPoints) : null,
-          estimatedWords: c.estimatedWords || 5000,
-          status: 'completed',
-        } as any,
+        }
       });
       });
+
+      if (existing) {
+        // 存在则更新
+        await prisma.bookChapter.update({
+          where: { id: existing.id },
+          data: {
+            title: c.title,
+            summary: c.summary,
+            keyPoints: c.keyPoints ? JSON.stringify(c.keyPoints) : null,
+            estimatedWords: c.estimatedWords || 5000,
+            status: 'completed',
+          } as any,
+        });
+      } else {
+        // 不存在则创建
+        await prisma.bookChapter.create({
+          data: {
+            bookId: bookIdNum,
+            parentId: null,
+            level: 1,
+            number: c.number,
+            title: c.title,
+            summary: c.summary,
+            keyPoints: c.keyPoints ? JSON.stringify(c.keyPoints) : null,
+            estimatedWords: c.estimatedWords || 5000,
+            status: 'completed',
+          } as any,
+        });
+      }
     }
     }
   }
   }
 
 

+ 20 - 20
server/test-ai.js

@@ -1,30 +1,30 @@
 const axios = require('axios');
 const axios = require('axios');
 
 
-const BASE_URL = 'http://localhost:3000';
-
-async function testAIModels() {
-  console.log('=== 测试 AI 模型接口 ===\n');
-
+async function testAI() {
   try {
   try {
-    console.log('1️⃣  测试获取 AI 模型列表...');
-    const result = await axios.get(`${BASE_URL}/api/ai/models`);
-    console.log('✅ AI 模型列表:', JSON.stringify(result.data, null, 2));
-
-    console.log('\n2️⃣  测试 AI 文本生成...');
-    const generate = await axios.post(`${BASE_URL}/api/ai/generate`, {
-      prompt: '写一首关于春天的短诗',
-      model: 'tongyi-xiaomi-analysis-pro'
+    console.log('🧪 测试 AI 接口...');
+    
+    const response = await axios.post('https://api.minimaxi.chat/v1/text/chatcompletion_v2', {
+      model: 'MiniMax-M2.7',
+      messages: [
+        { role: 'user', content: '你好,请回复OK' }
+      ],
+      max_tokens: 10
     }, {
     }, {
-      timeout: 60000
+      headers: {
+        'Authorization': `Bearer ${process.env.MINIMAX_API_KEY}`,
+        'Content-Type': 'application/json'
+      },
+      timeout: 30000
     });
     });
-    console.log('✅ AI 生成成功:', JSON.stringify(generate.data, null, 2));
 
 
+    console.log('✅ AI 接口正常');
+    console.log('响应:', response.data.choices?.[0]?.message?.content);
   } catch (error) {
   } catch (error) {
-    console.error('❌ 测试失败:', error.message);
-    if (error.response) {
-      console.error('📋 错误响应:', JSON.stringify(error.response.data, null, 2));
-    }
+    console.error('❌ AI 接口失败:');
+    console.error('状态码:', error.response?.status);
+    console.error('错误信息:', error.response?.data || error.message);
   }
   }
 }
 }
 
 
-testAIModels();
+testAI();