diff --git a/server2.js b/server2.js index 0e24bad..01b80ac 100755 --- a/server2.js +++ b/server2.js @@ -1031,49 +1031,56 @@ app.post("/analyze", upload.single("file"), async (req, res) => { // res.write( // `data: ${JSON.stringify({ type: "llm", content: delta})}\n\n` // ); - if (delta) { - for (const ch of delta) { - res.write(`data: ${JSON.stringify({ type: "llm", content: ch })}\n\n`); - await new Promise(r => setTimeout(r, 0)); // 控制渲染节奏 - } + + if (delta) + { + for (const ch of delta) + { + res.write(`data: ${JSON.stringify({ type: "llm", content: ch })}\n\n`); + await new Promise(r => setTimeout(r, 0)); // 控制渲染节奏 + } } } // 保存 LLM 输出到数据库 - - // try { - // const titlePrompt = `请根据以下对话内容生成一个简短的标题(不超过15字): - // 用户问题: ${userPrompt} - // AI回答: ${llmText}`; + /* 加标题需要异步,且在会话开始时就进行 */ + // try + // { + // const titlePrompt = `请根据以下对话内容生成一个简短的标题(不超过15字): + // 用户问题: ${userPrompt} + // AI回答: ${llmText}`; - // const titleResp = await client1.chat.completions.create({ - // model: "gpt-5-mini", - // messages: [ - // { role: "system", content: "你是标题生成助手,只返回标题,不要解释。" }, - // { role: "user", content: titlePrompt } - // ] - // }); + // const titleResp = await client1.chat.completions.create({ + // model: "gpt-5-mini", + // messages: [ + // { role: "system", content: "你是标题生成助手,只返回标题,不要解释。" }, + // { role: "user", content: titlePrompt } + // ] + // }); // const title = titleResp.choices[0]?.message?.content?.trim() || "未命名会话"; // res.write( // `data: ${JSON.stringify({ type: "title", content: title })}\n\n` // ); - // console.log("生成的标题:", title); + // console.log("生成的标题:", title); - // // 保存到数据库(建议你给 ai_chat_messages 表新增一列 title,或者单独建一张会话表) - // await pool.query( - // "INSERT INTO ai_chat_messages (session_id, user_id, role, content,title) VALUES (?, ?, 'assistant', ?,?)", - // [sessId, userId || null, llmText,title] - // ); - - + // // 保存到数据库(建议你给 ai_chat_messages 表新增一列 title,或者单独建一张会话表) + // // await pool.query( + // // "INSERT INTO ai_chat_messages (session_id, user_id, role, content,title) VALUES (?, ?, 'assistant', ?,?)", + // // [sessId, userId || null, llmText,title] + // // ); // } - // catch (err) { + // catch (err) + // { // console.error("生成标题失败:", err.message); // } + await pool.query( + "INSERT INTO ai_chat_messages (session_id, user_id, role, content) VALUES (?, ?, 'assistant', ?)", + [sessId, userId || null, llmText] + ); res.write("data: [DONE]\n\n"); res.end();