refactor(backend): incremental architecture evolution (Task 19)

- Add common/ layer for shared capabilities (LLM, RAG, document, middleware)
- Add legacy/ layer for existing business code
- Move files to new structure (controllers, routes, services)
- Update index.ts for new route registration
- System remains fully functional
This commit is contained in:
2025-11-16 15:42:44 +08:00
parent 8a17dc80ae
commit 0c5310fb77
39 changed files with 3904 additions and 353 deletions

View File

@@ -0,0 +1,218 @@
import { FastifyRequest, FastifyReply } from 'fastify';
import { agentService } from '../services/agentService.js';
interface AgentParams {
id: string;
}
interface RenderPromptBody {
projectBackground?: string;
userInput: string;
knowledgeBaseContext?: string;
}
class AgentController {
// 获取所有智能体列表
async getAllAgents(request: FastifyRequest, reply: FastifyReply) {
try {
const agents = agentService.getAllAgents();
return reply.code(200).send({
success: true,
data: agents,
});
} catch (error) {
request.log.error(error);
return reply.code(500).send({
success: false,
message: '获取智能体列表失败',
error: error instanceof Error ? error.message : 'Unknown error',
});
}
}
// 获取启用的智能体列表
async getEnabledAgents(request: FastifyRequest, reply: FastifyReply) {
try {
const agents = agentService.getEnabledAgents();
return reply.code(200).send({
success: true,
data: agents,
});
} catch (error) {
request.log.error(error);
return reply.code(500).send({
success: false,
message: '获取智能体列表失败',
error: error instanceof Error ? error.message : 'Unknown error',
});
}
}
// 获取单个智能体详情
async getAgentById(
request: FastifyRequest<{ Params: AgentParams }>,
reply: FastifyReply
) {
try {
const { id } = request.params;
const agent = agentService.getAgentById(id);
if (!agent) {
return reply.code(404).send({
success: false,
message: '智能体不存在',
});
}
return reply.code(200).send({
success: true,
data: agent,
});
} catch (error) {
request.log.error(error);
return reply.code(500).send({
success: false,
message: '获取智能体详情失败',
error: error instanceof Error ? error.message : 'Unknown error',
});
}
}
// 获取智能体的系统Prompt
async getSystemPrompt(
request: FastifyRequest<{ Params: AgentParams }>,
reply: FastifyReply
) {
try {
const { id } = request.params;
if (!agentService.agentExists(id)) {
return reply.code(404).send({
success: false,
message: '智能体不存在',
});
}
const systemPrompt = agentService.getSystemPrompt(id);
return reply.code(200).send({
success: true,
data: {
agentId: id,
systemPrompt,
},
});
} catch (error) {
request.log.error(error);
return reply.code(500).send({
success: false,
message: '获取系统Prompt失败',
error: error instanceof Error ? error.message : 'Unknown error',
});
}
}
// 渲染用户Prompt用于预览或调试
async renderPrompt(
request: FastifyRequest<{ Params: AgentParams }>,
reply: FastifyReply
) {
try {
const { id } = request.params;
const body = request.body as RenderPromptBody;
if (!agentService.agentExists(id)) {
return reply.code(404).send({
success: false,
message: '智能体不存在',
});
}
if (!body.userInput) {
return reply.code(400).send({
success: false,
message: 'userInput为必填项',
});
}
const renderedPrompt = agentService.renderUserPrompt(id, {
projectBackground: body.projectBackground,
userInput: body.userInput,
knowledgeBaseContext: body.knowledgeBaseContext,
});
return reply.code(200).send({
success: true,
data: {
agentId: id,
renderedPrompt,
},
});
} catch (error) {
request.log.error(error);
return reply.code(500).send({
success: false,
message: '渲染Prompt失败',
error: error instanceof Error ? error.message : 'Unknown error',
});
}
}
// 根据分类获取智能体
async getAgentsByCategory(
request: FastifyRequest<{ Querystring: { category: string } }>,
reply: FastifyReply
) {
try {
const { category } = request.query;
if (!category) {
return reply.code(400).send({
success: false,
message: 'category参数为必填项',
});
}
const agents = agentService.getAgentsByCategory(category);
return reply.code(200).send({
success: true,
data: agents,
});
} catch (error) {
request.log.error(error);
return reply.code(500).send({
success: false,
message: '获取智能体列表失败',
error: error instanceof Error ? error.message : 'Unknown error',
});
}
}
// 重新加载配置(管理员功能)
async reloadConfig(request: FastifyRequest, reply: FastifyReply) {
try {
agentService.reloadConfig();
return reply.code(200).send({
success: true,
message: '智能体配置已重新加载',
});
} catch (error) {
request.log.error(error);
return reply.code(500).send({
success: false,
message: '重新加载配置失败',
error: error instanceof Error ? error.message : 'Unknown error',
});
}
}
}
export const agentController = new AgentController();

View File

@@ -0,0 +1,428 @@
/**
* Phase 3: 批处理模式 - 批处理控制器
*
* API路由
* - POST /api/v1/batch/execute - 执行批处理任务
* - GET /api/v1/batch/tasks/:taskId - 获取任务状态
* - GET /api/v1/batch/tasks/:taskId/results - 获取任务结果
* - POST /api/v1/batch/tasks/:taskId/retry-failed - 重试失败项
*/
import { FastifyRequest, FastifyReply } from 'fastify';
import { executeBatchTask, retryFailedDocuments, BatchProgress } from '../services/batchService.js';
import { prisma } from '../../config/database.js';
import { ModelType } from '../../common/llm/adapters/types.js';
// ==================== 类型定义 ====================
interface ExecuteBatchBody {
kb_id: string;
document_ids: string[];
template_type: 'preset' | 'custom';
template_id?: string;
custom_prompt?: string;
model_type: ModelType;
task_name?: string;
}
interface TaskIdParams {
taskId: string;
}
// ==================== API处理器 ====================
/**
* POST /api/v1/batch/execute
* 执行批处理任务
*/
export async function executeBatch(
request: FastifyRequest<{ Body: ExecuteBatchBody }>,
reply: FastifyReply
) {
try {
// TODO: 从JWT获取userId
const userId = 'user-mock-001';
const {
kb_id,
document_ids,
template_type,
template_id,
custom_prompt,
model_type,
task_name,
} = request.body;
console.log('📦 [BatchController] 收到批处理请求', {
userId,
kbId: kb_id,
documentCount: document_ids.length,
templateType: template_type,
modelType: model_type,
});
// 验证参数
if (!kb_id || !document_ids || document_ids.length === 0) {
return reply.code(400).send({
success: false,
message: '缺少必要参数kb_id 或 document_ids',
});
}
if (document_ids.length < 3) {
return reply.code(400).send({
success: false,
message: '文献数量不能少于3篇',
});
}
if (document_ids.length > 50) {
return reply.code(400).send({
success: false,
message: '文献数量不能超过50篇',
});
}
if (template_type === 'preset' && !template_id) {
return reply.code(400).send({
success: false,
message: '预设模板类型需要提供 template_id',
});
}
if (template_type === 'custom' && !custom_prompt) {
return reply.code(400).send({
success: false,
message: '自定义模板需要提供 custom_prompt',
});
}
// 验证模型类型
const validModels: ModelType[] = ['deepseek-v3', 'qwen3-72b', 'qwen-long'];
if (!validModels.includes(model_type)) {
return reply.code(400).send({
success: false,
message: `不支持的模型类型: ${model_type}`,
});
}
// 验证知识库是否存在
const kb = await prisma.knowledgeBase.findUnique({
where: { id: kb_id },
});
if (!kb) {
return reply.code(404).send({
success: false,
message: `知识库不存在: ${kb_id}`,
});
}
// 验证文档是否都存在
const documents = await prisma.document.findMany({
where: {
id: { in: document_ids },
kbId: kb_id,
},
});
if (documents.length !== document_ids.length) {
return reply.code(400).send({
success: false,
message: `部分文档不存在或不属于该知识库`,
});
}
// 获取WebSocket实例用于进度推送
const io = (request.server as any).io;
// 先创建任务记录获取taskId
const taskPreview = await prisma.batchTask.create({
data: {
userId,
kbId: kb_id,
name: task_name || `批处理任务_${new Date().toLocaleString('zh-CN')}`,
templateType: template_type,
templateId: template_id || null,
prompt: custom_prompt || template_id || '',
status: 'processing',
totalDocuments: document_ids.length,
modelType: model_type,
concurrency: 3,
startedAt: new Date(),
},
});
const taskId = taskPreview.id;
console.log(`✅ [BatchController] 创建任务: ${taskId}`);
// 执行批处理任务(异步)
executeBatchTask({
userId,
kbId: kb_id,
documentIds: document_ids,
templateType: template_type,
templateId: template_id,
customPrompt: custom_prompt,
modelType: model_type,
taskName: task_name,
existingTaskId: taskId, // 使用已创建的任务ID
onProgress: (progress: BatchProgress) => {
// WebSocket推送进度
if (io) {
io.to(userId).emit('batch-progress', progress);
}
},
})
.then((result) => {
console.log(`🎉 [BatchController] 批处理任务完成: ${result.taskId}`);
// 推送完成事件
if (io) {
io.to(userId).emit('batch-completed', {
task_id: result.taskId,
status: result.status,
});
}
})
.catch((error) => {
console.error(`❌ [BatchController] 批处理任务失败:`, error);
// 推送失败事件
if (io) {
io.to(userId).emit('batch-failed', {
task_id: 'unknown',
error: error.message,
});
}
});
// 立即返回任务ID任务在后台执行
reply.send({
success: true,
message: '批处理任务已开始',
data: {
task_id: taskId,
status: 'processing',
websocket_event: 'batch-progress',
},
});
} catch (error: any) {
console.error('❌ [BatchController] 执行批处理失败:', error);
reply.code(500).send({
success: false,
message: error.message || '执行批处理任务失败',
});
}
}
/**
* GET /api/v1/batch/tasks/:taskId
* 获取任务状态
*/
export async function getTask(
request: FastifyRequest<{ Params: TaskIdParams }>,
reply: FastifyReply
) {
try {
const { taskId } = request.params;
const task = await prisma.batchTask.findUnique({
where: { id: taskId },
select: {
id: true,
name: true,
status: true,
totalDocuments: true,
completedCount: true,
failedCount: true,
modelType: true,
startedAt: true,
completedAt: true,
durationSeconds: true,
createdAt: true,
},
});
if (!task) {
return reply.code(404).send({
success: false,
message: `任务不存在: ${taskId}`,
});
}
reply.send({
success: true,
data: {
id: task.id,
name: task.name,
status: task.status,
total_documents: task.totalDocuments,
completed_count: task.completedCount,
failed_count: task.failedCount,
model_type: task.modelType,
started_at: task.startedAt,
completed_at: task.completedAt,
duration_seconds: task.durationSeconds,
created_at: task.createdAt,
},
});
} catch (error: any) {
console.error('❌ [BatchController] 获取任务失败:', error);
reply.code(500).send({
success: false,
message: error.message || '获取任务失败',
});
}
}
/**
* GET /api/v1/batch/tasks/:taskId/results
* 获取任务结果
*/
export async function getTaskResults(
request: FastifyRequest<{ Params: TaskIdParams }>,
reply: FastifyReply
) {
try {
const { taskId } = request.params;
// 获取任务信息
const task = await prisma.batchTask.findUnique({
where: { id: taskId },
include: {
results: {
include: {
document: {
select: {
filename: true,
tokensCount: true,
},
},
},
orderBy: {
createdAt: 'asc',
},
},
},
});
if (!task) {
return reply.code(404).send({
success: false,
message: `任务不存在: ${taskId}`,
});
}
// 格式化结果
const results = task.results.map((r, index) => ({
id: r.id,
index: index + 1,
document_id: r.documentId,
document_name: r.document.filename,
status: r.status,
data: r.data,
raw_output: r.rawOutput,
error_message: r.errorMessage,
processing_time_ms: r.processingTimeMs,
tokens_used: r.tokensUsed,
created_at: r.createdAt,
}));
reply.send({
success: true,
data: {
task: {
id: task.id,
name: task.name,
status: task.status,
template_type: task.templateType,
template_id: task.templateId,
total_documents: task.totalDocuments,
completed_count: task.completedCount,
failed_count: task.failedCount,
duration_seconds: task.durationSeconds,
created_at: task.createdAt,
completed_at: task.completedAt,
},
results,
},
});
} catch (error: any) {
console.error('❌ [BatchController] 获取任务结果失败:', error);
reply.code(500).send({
success: false,
message: error.message || '获取任务结果失败',
});
}
}
/**
* POST /api/v1/batch/tasks/:taskId/retry-failed
* 重试失败的文档
*/
export async function retryFailed(
request: FastifyRequest<{ Params: TaskIdParams }>,
reply: FastifyReply
) {
try {
const { taskId } = request.params;
const userId = 'user-mock-001'; // TODO: 从JWT获取
// 获取WebSocket实例
const io = (request.server as any).io;
// 执行重试(异步)
retryFailedDocuments(taskId, (progress: BatchProgress) => {
if (io) {
io.to(userId).emit('batch-progress', progress);
}
})
.then((result) => {
console.log(`✅ [BatchController] 重试完成: ${result.retriedCount}`);
})
.catch((error) => {
console.error(`❌ [BatchController] 重试失败:`, error);
});
reply.send({
success: true,
message: '已开始重试失败的文档',
});
} catch (error: any) {
console.error('❌ [BatchController] 重试失败:', error);
reply.code(500).send({
success: false,
message: error.message || '重试失败',
});
}
}
/**
* GET /api/v1/batch/templates
* 获取所有预设模板
*/
export async function getTemplates(
request: FastifyRequest,
reply: FastifyReply
) {
try {
const { getAllTemplates } = await import('../templates/clinicalResearch.js');
const templates = getAllTemplates();
reply.send({
success: true,
data: templates.map(t => ({
id: t.id,
name: t.name,
description: t.description,
output_fields: t.outputFields,
})),
});
} catch (error: any) {
console.error('❌ [BatchController] 获取模板失败:', error);
reply.code(500).send({
success: false,
message: error.message || '获取模板失败',
});
}
}

View File

@@ -0,0 +1,566 @@
import { FastifyRequest, FastifyReply } from 'fastify';
import { ModelType } from '../../common/llm/adapters/types.js';
import { LLMFactory } from '../../common/llm/adapters/LLMFactory.js';
import * as knowledgeBaseService from '../services/knowledgeBaseService.js';
import { prisma } from '../../config/database.js';
/**
* 引用信息接口
*/
interface Citation {
id: number;
fileName: string;
position: number;
score: number;
content: string;
}
/**
* 提取文本片段(用于引用上下文)
*/
function extractContextPreview(text: string, maxLength: number = 100): string {
if (!text) return '';
const cleaned = text.replace(/\s+/g, ' ').trim();
if (cleaned.length <= maxLength) {
return cleaned;
}
const truncated = cleaned.substring(0, maxLength);
const lastPunctuation = Math.max(
truncated.lastIndexOf('。'),
truncated.lastIndexOf(''),
truncated.lastIndexOf(''),
truncated.lastIndexOf('.'),
truncated.lastIndexOf('!'),
truncated.lastIndexOf('?')
);
if (lastPunctuation > maxLength * 0.5) {
return truncated.substring(0, lastPunctuation + 1);
}
return truncated + '...';
}
/**
* 格式化引用清单
*/
function formatCitations(citations: Citation[]): string {
if (citations.length === 0) return '';
let result = '\n\n---\n\n📚 **参考文献**\n\n';
for (const cite of citations) {
const scorePercent = (cite.score * 100).toFixed(0);
const preview = extractContextPreview(cite.content, 100);
// 使用HTML span标签给引用编号添加id方便跳转
result += `<span id="citation-detail-${cite.id}">[${cite.id}]</span> 📄 **${cite.fileName}** - 第${cite.position}段 (相关度${scorePercent}%)\n`;
result += ` "${preview}"\n\n`;
}
return result;
}
interface SendChatMessageBody {
content: string;
modelType: ModelType;
knowledgeBaseIds?: string[];
documentIds?: string[]; // Phase 2: 逐篇精读模式 - 限定文档范围
fullTextDocumentIds?: string[]; // Phase 2: 全文阅读模式 - 传递全文
conversationId?: string; // 可选:续接已有对话
}
/**
* 通用聊天Controller
* 无需项目和智能体,纯大模型对话
*/
export class ChatController {
/**
* 发送消息(流式输出)
*/
async sendMessageStream(
request: FastifyRequest<{ Body: SendChatMessageBody }>,
reply: FastifyReply
) {
try {
// TODO: 从JWT token获取userId
const userId = 'user-mock-001';
const { content, modelType, knowledgeBaseIds, documentIds, fullTextDocumentIds, conversationId } = request.body;
console.log('💬 [ChatController] 收到通用对话请求', {
content,
modelType,
knowledgeBaseIds: knowledgeBaseIds || [],
documentIds: documentIds || [],
fullTextDocumentIds: fullTextDocumentIds || [],
conversationId,
});
// 验证modelType
if (modelType !== 'deepseek-v3' && modelType !== 'qwen3-72b' && modelType !== 'qwen-long' && modelType !== 'gemini-pro') {
reply.code(400).send({
success: false,
message: `不支持的模型类型: ${modelType}`,
});
return;
}
// 获取或创建对话记录
let conversation;
if (conversationId) {
// 验证对话是否存在且属于当前用户
conversation = await prisma.generalConversation.findFirst({
where: {
id: conversationId,
userId,
deletedAt: null,
},
});
if (!conversation) {
reply.code(404).send({
success: false,
message: '对话不存在',
});
return;
}
} else {
// 创建新对话
conversation = await prisma.generalConversation.create({
data: {
userId,
title: content.substring(0, 50), // 用第一条消息的前50字作为标题
modelName: modelType,
},
});
console.log('✅ [ChatController] 创建新对话', { conversationId: conversation.id });
}
// 检索知识库上下文
let knowledgeBaseContext = '';
const allCitations: Citation[] = []; // 存储所有引用信息
let citationCounter = 1; // 全局引用计数器
// Phase 2: 全文阅读模式 - 传递完整文献全文
if (fullTextDocumentIds && fullTextDocumentIds.length > 0) {
console.log('📚 [ChatController] 全文阅读模式 - 加载文献全文', {
documentCount: fullTextDocumentIds.length,
});
try {
// 获取所有选中文档的全文
const documents = await prisma.document.findMany({
where: {
id: { in: fullTextDocumentIds },
},
select: {
id: true,
filename: true,
extractedText: true,
tokensCount: true,
},
orderBy: {
filename: 'asc', // 按文件名排序
},
});
console.log(`📄 [ChatController] 加载了 ${documents.length} 篇文献全文`);
// 过滤掉没有extractedText的文档
const validDocuments = documents.filter(doc => doc.extractedText && doc.extractedText.trim().length > 0);
if (validDocuments.length === 0) {
console.warn('⚠️ [ChatController] 所有文档都没有提取文本,无法使用全文模式');
} else if (validDocuments.length < documents.length) {
console.warn(`⚠️ [ChatController] ${documents.length - validDocuments.length} 篇文档没有提取文本,已跳过`);
}
// 组装全文上下文,每篇文献用明确的标记分隔
const fullTextParts: string[] = [];
for (let i = 0; i < validDocuments.length; i++) {
const doc = validDocuments[i];
const docNumber = i + 1;
// 为每篇文献添加引用信息
allCitations.push({
id: docNumber,
fileName: doc.filename,
position: 0, // 全文没有position概念
score: 1.0, // 全文模式相关度100%
content: doc.extractedText?.substring(0, 200) || '(无内容)',
});
// 格式【文献N文件名】\n全文内容
fullTextParts.push(
`【文献${docNumber}${doc.filename}\n\n${doc.extractedText}`
);
}
knowledgeBaseContext = fullTextParts.join('\n\n---\n\n');
const totalTokens = validDocuments.reduce((sum, doc) => sum + (doc.tokensCount || 0), 0);
console.log(`📚 [ChatController] 全文上下文已组装`, {
totalDocuments: validDocuments.length,
totalCharacters: knowledgeBaseContext.length,
totalTokens: totalTokens,
estimatedTokens: Math.round(knowledgeBaseContext.length / 2.5), // 粗略估算
});
// ⚠️ 检查Token限制Qwen-Long输入限制1M tokens
const QWEN_LONG_INPUT_LIMIT = 1000000;
const SYSTEM_OVERHEAD = 10000; // 系统提示、格式等开销
const SAFE_INPUT_LIMIT = QWEN_LONG_INPUT_LIMIT - SYSTEM_OVERHEAD;
if (totalTokens > SAFE_INPUT_LIMIT) {
const errorMsg = `输入Token数量 (${totalTokens}) 超出Qwen-Long模型限制 (${SAFE_INPUT_LIMIT})。请减少文献数量后重试。`;
console.error(`❌ [ChatController] ${errorMsg}`);
// 返回错误信息给前端
reply.raw.write(`data: ${JSON.stringify({
content: `\n\n⚠ **Token数量超限**\n\n${errorMsg}\n\n**建议**\n- 当前选中 ${validDocuments.length} 篇文献,共 ${totalTokens.toLocaleString()} tokens\n- 请减少到 ${Math.floor(validDocuments.length * SAFE_INPUT_LIMIT / totalTokens)} 篇以内\n- 或使用"逐篇精读"模式深入分析单篇文献`,
role: 'assistant',
error: true,
})}\n\n`);
reply.raw.write('data: [DONE]\n\n');
return reply.raw.end();
}
// 警告:如果接近限制
if (totalTokens > SAFE_INPUT_LIMIT * 0.8) {
console.warn(`⚠️ [ChatController] Token数量接近限制 (${totalTokens}/${SAFE_INPUT_LIMIT}), 建议减少文献数量`);
}
} catch (error) {
console.error('❌ [ChatController] 加载文献全文失败:', error);
// 不throw错误继续执行可能没有全文也能正常对话
}
}
// RAG检索模式逐篇精读或通用对话
else if (knowledgeBaseIds && knowledgeBaseIds.length > 0) {
console.log('📚 [ChatController] 开始检索知识库');
const knowledgeResults: string[] = [];
// Phase 2: 如果指定了文档ID逐篇精读模式需要更多结果用于过滤
const topK = documentIds && documentIds.length > 0 ? 50 : 15;
for (const kbId of knowledgeBaseIds) {
try {
const searchResult = await knowledgeBaseService.searchKnowledgeBase(
userId,
kbId,
content,
topK
);
if (searchResult.records && searchResult.records.length > 0) {
let records = searchResult.records;
// Phase 2: 逐篇精读模式 - 过滤出指定文档的结果
if (documentIds && documentIds.length > 0) {
console.log(`🔍 [ChatController] 逐篇精读模式 - 过滤文档`, { documentIds });
// 获取文档的Dify ID映射
const documents = await prisma.document.findMany({
where: {
id: { in: documentIds },
knowledgeBase: {
id: kbId,
},
},
select: {
id: true,
filename: true,
difyDocumentId: true,
},
});
const difyDocIds = documents.map(d => d.difyDocumentId).filter(Boolean);
console.log(`📄 [ChatController] 目标Dify文档ID:`, difyDocIds);
// 过滤结果
const beforeCount = records.length;
records = records.filter((record: any) => {
const docId = record.segment?.document?.id || record.document_id;
return docId && difyDocIds.includes(docId);
});
console.log(`✂️ [ChatController] 过滤结果: ${beforeCount}${records.length}`);
// 如果过滤后结果太少,警告
if (records.length === 0) {
console.warn('⚠️ [ChatController] 过滤后没有结果可能是文档ID不匹配');
}
// 只取前15个
records = records.slice(0, 15);
}
if (records.length > 0) {
const kbInfo = await prisma.knowledgeBase.findUnique({
where: { id: kbId },
select: { name: true },
});
// 优化格式:使用[来源N]标记便于AI引用
const formattedResult = `【知识库:${kbInfo?.name || '未命名'}\n` +
records
.map((record: any) => {
const citationId = citationCounter++;
const score = (record.score * 100).toFixed(1);
// 保存引用信息
allCitations.push({
id: citationId,
fileName: record.segment?.document?.name || record.document_name || '未知文档',
position: record.segment?.position || record.segment_position || 0,
score: record.score,
content: record.segment?.content || record.content || '',
});
return `[来源${citationId}] [相关度${score}%]\n${record.segment?.content || record.content}`;
})
.join('\n\n');
knowledgeResults.push(formattedResult);
}
}
} catch (error) {
console.error(`❌ [ChatController] 检索知识库失败 ${kbId}:`, error);
}
}
if (knowledgeResults.length > 0) {
knowledgeBaseContext = knowledgeResults.join('\n\n---\n\n');
console.log(`💾 [ChatController] 知识库上下文: ${knowledgeBaseContext.length} 字符`);
console.log(`📚 [ChatController] 收集到 ${allCitations.length} 个引用`);
}
}
// 获取历史消息最近20条
const historyMessages = await prisma.generalMessage.findMany({
where: {
conversationId: conversation.id,
},
orderBy: {
createdAt: 'desc',
},
take: 20,
});
historyMessages.reverse();
console.log(`📜 [ChatController] 历史消息数: ${historyMessages.length}`);
// 组装消息上下文
let systemPrompt = '你是一个专业、友好的AI助手。当用户提供参考资料时请优先基于参考资料回答。';
// 全文阅读模式的系统提示
if (fullTextDocumentIds && fullTextDocumentIds.length > 0) {
systemPrompt = '你是一个专业的学术文献分析助手。用户会提供多篇文献的完整全文每篇文献用【文献N文件名】标记。请认真阅读所有文献进行深入的综合分析。在回答时请引用具体文献使用【文献N】格式。你的优势是能够看到所有文献的全貌进行跨文献的比较、归纳和总结。';
}
const messages: any[] = [
{
role: 'system',
content: systemPrompt,
},
];
// 添加历史消息
for (const msg of historyMessages) {
messages.push({
role: msg.role,
content: msg.content,
});
}
// 添加当前用户消息
let userContent = content;
if (knowledgeBaseContext) {
// 全文阅读模式的提示
if (fullTextDocumentIds && fullTextDocumentIds.length > 0) {
userContent = `${content}\n\n## 参考资料(文献全文)\n\n**重要提示**下面提供的是完整的文献全文。每篇文献用【文献N文件名】标记。请在回答时引用文献格式如"根据【文献1】..."或"研究表明【文献2】【文献3】..."。你可以综合分析所有文献,进行跨文献的比较和总结。\n\n${knowledgeBaseContext}`;
}
// RAG检索模式的提示
else {
userContent = `${content}\n\n## 参考资料(来自知识库)\n\n**重要提示**:下面提供的文献片段已经用[来源N]进行了标记。请在回答中引用具体来源时使用对应的编号,如"根据[来源1]..."或"研究表明[来源3][来源5]..."。系统会在你回答结束后自动显示完整的引用清单。\n\n${knowledgeBaseContext}`;
}
}
messages.push({
role: 'user',
content: userContent,
});
// 设置SSE响应头
reply.raw.writeHead(200, {
'Content-Type': 'text/event-stream',
'Cache-Control': 'no-cache',
Connection: 'keep-alive',
'Access-Control-Allow-Origin': '*',
});
// 保存用户消息
await prisma.generalMessage.create({
data: {
conversationId: conversation.id,
role: 'user',
content,
metadata: {
knowledgeBaseIds,
},
},
});
// 流式输出
const adapter = LLMFactory.getAdapter(modelType);
let fullContent = '';
let usage: any = null;
// Phase 2: 全文阅读模式需要更大的输出空间(用于综合分析、引用等)
const maxOutputTokens = fullTextDocumentIds && fullTextDocumentIds.length > 0
? 6000 // 全文模式:需要更长的回答空间
: 2000; // 其他模式:常规长度
console.log(`🤖 [ChatController] 开始调用LLM`, {
model: modelType,
maxOutputTokens,
mode: fullTextDocumentIds && fullTextDocumentIds.length > 0 ? '全文阅读' : '其他',
});
for await (const chunk of adapter.chatStream(messages, {
temperature: 0.7,
maxTokens: maxOutputTokens,
})) {
fullContent += chunk.content;
if (chunk.usage) {
usage = chunk.usage;
}
// 发送SSE数据
reply.raw.write(`data: ${JSON.stringify(chunk)}\n\n`);
}
// AI回答完毕后追加引用清单
if (allCitations.length > 0) {
console.log(`📚 [ChatController] 追加 ${allCitations.length} 个引用清单`);
const citationsText = formatCitations(allCitations);
fullContent += citationsText;
// 将引用清单也流式输出
const citationChunk = {
content: citationsText,
role: 'assistant' as const,
};
reply.raw.write(`data: ${JSON.stringify(citationChunk)}\n\n`);
}
// 保存助手消息
await prisma.generalMessage.create({
data: {
conversationId: conversation.id,
role: 'assistant',
content: fullContent,
model: modelType,
tokens: usage?.totalTokens,
metadata: {
usage,
},
},
});
// 更新对话
await prisma.generalConversation.update({
where: { id: conversation.id },
data: {
updatedAt: new Date(),
},
});
// 发送完成信号
reply.raw.write(`data: [DONE]\n\n`);
reply.raw.end();
console.log('✅ [ChatController] 对话完成');
} catch (error: any) {
console.error('❌ [ChatController] 错误:', error);
reply.code(500).send({
success: false,
message: error.message || '服务器错误',
});
}
}
/**
* 获取对话列表
*/
async getConversations(
_request: FastifyRequest,
reply: FastifyReply
) {
try {
const userId = 'user-mock-001';
const conversations = await prisma.generalConversation.findMany({
where: {
userId,
deletedAt: null,
},
orderBy: {
updatedAt: 'desc',
},
take: 50,
});
reply.send({
success: true,
data: conversations,
});
} catch (error: any) {
reply.code(500).send({
success: false,
message: error.message || '获取对话列表失败',
});
}
}
/**
* 删除对话
*/
async deleteConversation(
request: FastifyRequest<{ Params: { id: string } }>,
reply: FastifyReply
) {
try {
const userId = 'user-mock-001';
const { id } = request.params;
await prisma.generalConversation.update({
where: {
id,
userId,
},
data: {
deletedAt: new Date(),
},
});
reply.send({
success: true,
message: '删除成功',
});
} catch (error: any) {
reply.code(500).send({
success: false,
message: error.message || '删除失败',
});
}
}
}
export const chatController = new ChatController();

View File

@@ -0,0 +1,263 @@
import { FastifyRequest, FastifyReply } from 'fastify';
import { conversationService } from '../services/conversationService.js';
import { ModelType } from '../adapters/types.js';
export class ConversationController {
/**
* 创建新对话
*/
async createConversation(
request: FastifyRequest<{
Body: {
projectId: string;
agentId: string;
title?: string;
};
}>,
reply: FastifyReply
) {
try {
// TODO: 从JWT token获取userId
const userId = 'user-mock-001'; // 临时使用模拟用户
const { projectId, agentId, title } = request.body;
const conversation = await conversationService.createConversation({
userId,
projectId,
agentId,
title,
});
reply.code(201).send({
success: true,
data: conversation,
});
} catch (error: any) {
reply.code(400).send({
success: false,
message: error.message || '创建对话失败',
});
}
}
/**
* 获取对话列表
*/
async getConversations(
request: FastifyRequest<{
Querystring: {
projectId?: string;
};
}>,
reply: FastifyReply
) {
try {
// TODO: 从JWT token获取userId
const userId = 'user-mock-001';
const projectId = request.query.projectId;
const conversations = await conversationService.getConversations(
userId,
projectId
);
reply.send({
success: true,
data: conversations,
});
} catch (error: any) {
reply.code(500).send({
success: false,
message: error.message || '获取对话列表失败',
});
}
}
/**
* 获取对话详情
*/
async getConversationById(
request: FastifyRequest<{
Params: {
id: string;
};
}>,
reply: FastifyReply
) {
try {
// TODO: 从JWT token获取userId
const userId = 'user-mock-001';
const conversationId = request.params.id;
const conversation = await conversationService.getConversationById(
conversationId,
userId
);
reply.send({
success: true,
data: conversation,
});
} catch (error: any) {
reply.code(404).send({
success: false,
message: error.message || '对话不存在',
});
}
}
/**
* 发送消息(非流式)
*/
async sendMessage(
request: FastifyRequest<{
Body: {
conversationId: string;
content: string;
modelType: ModelType;
knowledgeBaseIds?: string[];
};
}>,
reply: FastifyReply
) {
try {
// TODO: 从JWT token获取userId
const userId = 'user-mock-001';
const { conversationId, content, modelType, knowledgeBaseIds } =
request.body;
// 验证modelType
if (modelType !== 'deepseek-v3' && modelType !== 'qwen3-72b' && modelType !== 'qwen-long' && modelType !== 'gemini-pro') {
reply.code(400).send({
success: false,
message: `不支持的模型类型: ${modelType}`,
});
return;
}
const result = await conversationService.sendMessage(
{
conversationId,
content,
modelType,
knowledgeBaseIds,
},
userId
);
reply.send({
success: true,
data: result,
});
} catch (error: any) {
reply.code(400).send({
success: false,
message: error.message || '发送消息失败',
});
}
}
/**
* 发送消息流式输出SSE
*/
async sendMessageStream(
request: FastifyRequest<{
Body: {
conversationId: string;
content: string;
modelType: ModelType;
knowledgeBaseIds?: string[];
};
}>,
reply: FastifyReply
) {
try {
// TODO: 从JWT token获取userId
const userId = 'user-mock-001';
const { conversationId, content, modelType, knowledgeBaseIds } =
request.body;
// 验证modelType
if (modelType !== 'deepseek-v3' && modelType !== 'qwen3-72b' && modelType !== 'qwen-long' && modelType !== 'gemini-pro') {
reply.code(400).send({
success: false,
message: `不支持的模型类型: ${modelType}`,
});
return;
}
// 设置SSE响应头
reply.raw.writeHead(200, {
'Content-Type': 'text/event-stream',
'Cache-Control': 'no-cache',
Connection: 'keep-alive',
'Access-Control-Allow-Origin': '*',
});
// 流式输出
for await (const chunk of conversationService.sendMessageStream(
{
conversationId,
content,
modelType,
knowledgeBaseIds,
},
userId
)) {
// 发送SSE数据
reply.raw.write(`data: ${JSON.stringify(chunk)}\n\n`);
}
// 发送结束标记
reply.raw.write('data: [DONE]\n\n');
reply.raw.end();
} catch (error: any) {
console.error('Stream error:', error);
reply.raw.write(
`data: ${JSON.stringify({
error: error.message || '发送消息失败',
})}\n\n`
);
reply.raw.end();
}
}
/**
* 删除对话
*/
async deleteConversation(
request: FastifyRequest<{
Params: {
id: string;
};
}>,
reply: FastifyReply
) {
try {
// TODO: 从JWT token获取userId
const userId = 'user-mock-001';
const conversationId = request.params.id;
await conversationService.deleteConversation(conversationId, userId);
reply.send({
success: true,
message: '对话已删除',
});
} catch (error: any) {
reply.code(400).send({
success: false,
message: error.message || '删除对话失败',
});
}
}
}
export const conversationController = new ConversationController();

View File

@@ -0,0 +1,314 @@
import type { FastifyRequest, FastifyReply } from 'fastify';
import * as documentService from '../services/documentService.js';
// Mock用户ID实际应从JWT token中获取
const MOCK_USER_ID = 'user-mock-001';
/**
* 上传文档
*/
export async function uploadDocument(
request: FastifyRequest<{
Params: {
kbId: string;
};
}>,
reply: FastifyReply
) {
try {
const { kbId } = request.params;
console.log(`📤 开始上传文档到知识库: ${kbId}`);
// 获取上传的文件
const data = await request.file();
if (!data) {
console.error('❌ 没有接收到文件');
return reply.status(400).send({
success: false,
message: 'No file uploaded',
});
}
console.log(`📄 接收到文件: ${data.filename}, 类型: ${data.mimetype}`);
const file = await data.toBuffer();
const filename = data.filename;
const fileType = data.mimetype;
const fileSizeBytes = file.length;
// 文件大小限制10MB
const maxSize = 10 * 1024 * 1024;
console.log(`📊 文件大小: ${(fileSizeBytes / 1024 / 1024).toFixed(2)}MB (限制: 10MB)`);
if (fileSizeBytes > maxSize) {
console.error(`❌ 文件太大: ${(fileSizeBytes / 1024 / 1024).toFixed(2)}MB`);
return reply.status(400).send({
success: false,
message: 'File size exceeds 10MB limit',
});
}
// 文件类型限制
const allowedTypes = [
'application/pdf',
'application/msword',
'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
'text/plain',
'text/markdown',
];
console.log(`🔍 检查文件类型: ${fileType}`);
if (!allowedTypes.includes(fileType)) {
console.error(`❌ 不支持的文件类型: ${fileType}`);
return reply.status(400).send({
success: false,
message: 'File type not supported. Allowed: PDF, DOC, DOCX, TXT, MD',
});
}
// 上传文档这里fileUrl暂时为空实际应该上传到对象存储
console.log(`⚙️ 调用文档服务上传文件...`);
const document = await documentService.uploadDocument(
MOCK_USER_ID,
kbId,
file,
filename,
fileType,
fileSizeBytes,
'' // fileUrl - 可以上传到OSS后填入
);
console.log(`✅ 文档上传成功: ${document.id}`);
return reply.status(201).send({
success: true,
data: document,
});
} catch (error: any) {
console.error('❌ 文档上传失败:', error.message);
console.error('错误详情:', error);
if (error.message.includes('not found') || error.message.includes('access denied')) {
return reply.status(404).send({
success: false,
message: error.message,
});
}
if (error.message.includes('limit exceeded')) {
return reply.status(400).send({
success: false,
message: error.message,
});
}
return reply.status(500).send({
success: false,
message: error.message || 'Failed to upload document',
});
}
}
/**
* 获取文档列表
*/
export async function getDocuments(
request: FastifyRequest<{
Params: {
kbId: string;
};
}>,
reply: FastifyReply
) {
try {
const { kbId } = request.params;
const documents = await documentService.getDocuments(MOCK_USER_ID, kbId);
return reply.send({
success: true,
data: documents,
});
} catch (error: any) {
console.error('Failed to get documents:', error);
if (error.message.includes('not found')) {
return reply.status(404).send({
success: false,
message: error.message,
});
}
return reply.status(500).send({
success: false,
message: error.message || 'Failed to get documents',
});
}
}
/**
* 获取文档详情
*/
export async function getDocumentById(
request: FastifyRequest<{
Params: {
id: string;
};
}>,
reply: FastifyReply
) {
try {
const { id } = request.params;
const document = await documentService.getDocumentById(MOCK_USER_ID, id);
return reply.send({
success: true,
data: document,
});
} catch (error: any) {
console.error('Failed to get document:', error);
if (error.message.includes('not found')) {
return reply.status(404).send({
success: false,
message: error.message,
});
}
return reply.status(500).send({
success: false,
message: error.message || 'Failed to get document',
});
}
}
/**
* 删除文档
*/
export async function deleteDocument(
request: FastifyRequest<{
Params: {
id: string;
};
}>,
reply: FastifyReply
) {
try {
const { id } = request.params;
await documentService.deleteDocument(MOCK_USER_ID, id);
return reply.send({
success: true,
message: 'Document deleted successfully',
});
} catch (error: any) {
console.error('Failed to delete document:', error);
if (error.message.includes('not found')) {
return reply.status(404).send({
success: false,
message: error.message,
});
}
return reply.status(500).send({
success: false,
message: error.message || 'Failed to delete document',
});
}
}
/**
* 重新处理文档
*/
export async function reprocessDocument(
request: FastifyRequest<{
Params: {
id: string;
};
}>,
reply: FastifyReply
) {
try {
const { id } = request.params;
await documentService.reprocessDocument(MOCK_USER_ID, id);
return reply.send({
success: true,
message: 'Document reprocessing started',
});
} catch (error: any) {
console.error('Failed to reprocess document:', error);
if (error.message.includes('not found')) {
return reply.status(404).send({
success: false,
message: error.message,
});
}
return reply.status(500).send({
success: false,
message: error.message || 'Failed to reprocess document',
});
}
}
/**
* Phase 2: 获取文档全文(用于逐篇精读模式)
*/
export async function getDocumentFullText(
request: FastifyRequest<{
Params: {
id: string;
};
}>,
reply: FastifyReply
) {
try {
const { id } = request.params;
const document = await documentService.getDocumentById(MOCK_USER_ID, id);
// 返回完整的文档信息
return reply.send({
success: true,
data: {
documentId: document.id,
filename: document.filename,
fileType: document.fileType,
fileSizeBytes: document.fileSizeBytes,
extractedText: (document as any).extractedText || null,
charCount: (document as any).charCount || null,
tokensCount: document.tokensCount || null,
extractionMethod: (document as any).extractionMethod || null,
extractionQuality: (document as any).extractionQuality || null,
language: (document as any).language || null,
metadata: {
uploadedAt: document.uploadedAt,
processedAt: document.processedAt,
status: document.status,
},
},
});
} catch (error: any) {
console.error('Failed to get document full text:', error);
if (error.message.includes('not found')) {
return reply.status(404).send({
success: false,
message: error.message,
});
}
return reply.status(500).send({
success: false,
message: error.message || 'Failed to get document full text',
});
}
}

View File

@@ -0,0 +1,341 @@
import type { FastifyRequest, FastifyReply } from 'fastify';
import * as knowledgeBaseService from '../services/knowledgeBaseService.js';
// Mock用户ID实际应从JWT token中获取
const MOCK_USER_ID = 'user-mock-001';
/**
* 创建知识库
*/
export async function createKnowledgeBase(
request: FastifyRequest<{
Body: {
name: string;
description?: string;
};
}>,
reply: FastifyReply
) {
try {
const { name, description } = request.body;
if (!name || name.trim().length === 0) {
return reply.status(400).send({
success: false,
message: 'Knowledge base name is required',
});
}
const knowledgeBase = await knowledgeBaseService.createKnowledgeBase(
MOCK_USER_ID,
name,
description
);
return reply.status(201).send({
success: true,
data: knowledgeBase,
});
} catch (error: any) {
console.error('Failed to create knowledge base:', error);
return reply.status(500).send({
success: false,
message: error.message || 'Failed to create knowledge base',
});
}
}
/**
* 获取知识库列表
*/
export async function getKnowledgeBases(
_request: FastifyRequest,
reply: FastifyReply
) {
try {
const knowledgeBases = await knowledgeBaseService.getKnowledgeBases(
MOCK_USER_ID
);
return reply.send({
success: true,
data: knowledgeBases,
});
} catch (error: any) {
console.error('Failed to get knowledge bases:', error);
return reply.status(500).send({
success: false,
message: error.message || 'Failed to get knowledge bases',
});
}
}
/**
* 获取知识库详情
*/
export async function getKnowledgeBaseById(
request: FastifyRequest<{
Params: {
id: string;
};
}>,
reply: FastifyReply
) {
try {
const { id } = request.params;
const knowledgeBase = await knowledgeBaseService.getKnowledgeBaseById(
MOCK_USER_ID,
id
);
return reply.send({
success: true,
data: knowledgeBase,
});
} catch (error: any) {
console.error('Failed to get knowledge base:', error);
if (error.message.includes('not found')) {
return reply.status(404).send({
success: false,
message: error.message,
});
}
return reply.status(500).send({
success: false,
message: error.message || 'Failed to get knowledge base',
});
}
}
/**
* 更新知识库
*/
export async function updateKnowledgeBase(
request: FastifyRequest<{
Params: {
id: string;
};
Body: {
name?: string;
description?: string;
};
}>,
reply: FastifyReply
) {
try {
const { id } = request.params;
const updateData = request.body;
const knowledgeBase = await knowledgeBaseService.updateKnowledgeBase(
MOCK_USER_ID,
id,
updateData
);
return reply.send({
success: true,
data: knowledgeBase,
});
} catch (error: any) {
console.error('Failed to update knowledge base:', error);
if (error.message.includes('not found')) {
return reply.status(404).send({
success: false,
message: error.message,
});
}
return reply.status(500).send({
success: false,
message: error.message || 'Failed to update knowledge base',
});
}
}
/**
* 删除知识库
*/
export async function deleteKnowledgeBase(
request: FastifyRequest<{
Params: {
id: string;
};
}>,
reply: FastifyReply
) {
try {
const { id } = request.params;
await knowledgeBaseService.deleteKnowledgeBase(MOCK_USER_ID, id);
return reply.send({
success: true,
message: 'Knowledge base deleted successfully',
});
} catch (error: any) {
console.error('Failed to delete knowledge base:', error);
if (error.message.includes('not found')) {
return reply.status(404).send({
success: false,
message: error.message,
});
}
return reply.status(500).send({
success: false,
message: error.message || 'Failed to delete knowledge base',
});
}
}
/**
* 检索知识库
*/
export async function searchKnowledgeBase(
request: FastifyRequest<{
Params: {
id: string;
};
Querystring: {
query: string;
top_k?: string;
};
}>,
reply: FastifyReply
) {
try {
const { id } = request.params;
const { query, top_k } = request.query;
if (!query || query.trim().length === 0) {
return reply.status(400).send({
success: false,
message: 'Query parameter is required',
});
}
const topK = top_k ? parseInt(top_k, 10) : 15; // Phase 1优化默认从3增加到15
const results = await knowledgeBaseService.searchKnowledgeBase(
MOCK_USER_ID,
id,
query,
topK
);
return reply.send({
success: true,
data: results,
});
} catch (error: any) {
console.error('Failed to search knowledge base:', error);
if (error.message.includes('not found')) {
return reply.status(404).send({
success: false,
message: error.message,
});
}
return reply.status(500).send({
success: false,
message: error.message || 'Failed to search knowledge base',
});
}
}
/**
* 获取知识库统计信息
*/
export async function getKnowledgeBaseStats(
request: FastifyRequest<{
Params: {
id: string;
};
}>,
reply: FastifyReply
) {
try {
const { id } = request.params;
const stats = await knowledgeBaseService.getKnowledgeBaseStats(
MOCK_USER_ID,
id
);
return reply.send({
success: true,
data: stats,
});
} catch (error: any) {
console.error('Failed to get knowledge base stats:', error);
if (error.message.includes('not found')) {
return reply.status(404).send({
success: false,
message: error.message,
});
}
return reply.status(500).send({
success: false,
message: error.message || 'Failed to get knowledge base stats',
});
}
}
/**
* 获取知识库文档选择Phase 2: 全文阅读模式)
*/
export async function getDocumentSelection(
request: FastifyRequest<{
Params: {
id: string;
};
Querystring: {
max_files?: string;
max_tokens?: string;
};
}>,
reply: FastifyReply
) {
try {
const { id } = request.params;
const { max_files, max_tokens } = request.query;
const maxFiles = max_files ? parseInt(max_files, 10) : undefined;
const maxTokens = max_tokens ? parseInt(max_tokens, 10) : undefined;
const selection = await knowledgeBaseService.getDocumentSelection(
MOCK_USER_ID,
id,
maxFiles,
maxTokens
);
return reply.send({
success: true,
data: selection,
});
} catch (error: any) {
console.error('Failed to get document selection:', error);
if (error.message.includes('not found')) {
return reply.status(404).send({
success: false,
message: error.message,
});
}
return reply.status(500).send({
success: false,
message: error.message || 'Failed to get document selection',
});
}
}

View File

@@ -0,0 +1,183 @@
import { FastifyRequest, FastifyReply } from 'fastify';
import { projectService } from '../services/projectService.js';
interface ProjectParams {
id: string;
}
interface CreateProjectBody {
name: string;
background: string;
researchType: 'observational' | 'interventional';
}
interface UpdateProjectBody {
name?: string;
background?: string;
researchType?: 'observational' | 'interventional';
}
class ProjectController {
// 获取项目列表
async getProjects(request: FastifyRequest, reply: FastifyReply) {
try {
// TODO: 从JWT token中获取真实的userId
// 目前使用模拟用户ID
const userId = 'user-mock-001';
const projects = await projectService.getProjectsByUserId(userId);
return reply.code(200).send({
success: true,
data: projects,
});
} catch (error) {
request.log.error(error);
return reply.code(500).send({
success: false,
message: '获取项目列表失败',
error: error instanceof Error ? error.message : 'Unknown error',
});
}
}
// 获取单个项目详情
async getProjectById(
request: FastifyRequest<{ Params: ProjectParams }>,
reply: FastifyReply
) {
try {
const { id } = request.params;
const userId = 'user-mock-001'; // TODO: 从JWT获取
const project = await projectService.getProjectById(id, userId);
if (!project) {
return reply.code(404).send({
success: false,
message: '项目不存在或无权访问',
});
}
return reply.code(200).send({
success: true,
data: project,
});
} catch (error) {
request.log.error(error);
return reply.code(500).send({
success: false,
message: '获取项目详情失败',
error: error instanceof Error ? error.message : 'Unknown error',
});
}
}
// 创建项目
async createProject(request: FastifyRequest, reply: FastifyReply) {
try {
const body = request.body as CreateProjectBody;
const userId = 'user-mock-001'; // TODO: 从JWT获取
// 检查用户项目数量限制(可选)
const projectCount = await projectService.countUserProjects(userId);
const MAX_PROJECTS = 50; // 可以配置到环境变量
if (projectCount >= MAX_PROJECTS) {
return reply.code(400).send({
success: false,
message: `最多只能创建${MAX_PROJECTS}个项目`,
});
}
const project = await projectService.createProject({
name: body.name,
background: body.background,
researchType: body.researchType,
userId,
});
return reply.code(201).send({
success: true,
message: '项目创建成功',
data: project,
});
} catch (error) {
request.log.error(error);
return reply.code(500).send({
success: false,
message: '创建项目失败',
error: error instanceof Error ? error.message : 'Unknown error',
});
}
}
// 更新项目
async updateProject(
request: FastifyRequest<{ Params: ProjectParams }>,
reply: FastifyReply
) {
try {
const { id } = request.params;
const body = request.body as UpdateProjectBody;
const userId = 'user-mock-001'; // TODO: 从JWT获取
const project = await projectService.updateProject(id, userId, body);
if (!project) {
return reply.code(404).send({
success: false,
message: '项目不存在或无权访问',
});
}
return reply.code(200).send({
success: true,
message: '项目更新成功',
data: project,
});
} catch (error) {
request.log.error(error);
return reply.code(500).send({
success: false,
message: '更新项目失败',
error: error instanceof Error ? error.message : 'Unknown error',
});
}
}
// 删除项目
async deleteProject(
request: FastifyRequest<{ Params: ProjectParams }>,
reply: FastifyReply
) {
try {
const { id } = request.params;
const userId = 'user-mock-001'; // TODO: 从JWT获取
const project = await projectService.deleteProject(id, userId);
if (!project) {
return reply.code(404).send({
success: false,
message: '项目不存在或无权访问',
});
}
return reply.code(200).send({
success: true,
message: '项目删除成功',
});
} catch (error) {
request.log.error(error);
return reply.code(500).send({
success: false,
message: '删除项目失败',
error: error instanceof Error ? error.message : 'Unknown error',
});
}
}
}
export const projectController = new ProjectController();

View File

@@ -0,0 +1,292 @@
import type { FastifyRequest, FastifyReply } from 'fastify';
import * as reviewService from '../services/reviewService.js';
import { ModelType } from '../../common/llm/adapters/types.js';
// Mock用户ID实际应从JWT token中获取
const MOCK_USER_ID = 'user-mock-001';
/**
* 上传稿件并开始审查
* POST /api/review/upload
*/
export async function uploadManuscript(
request: FastifyRequest<{
Body: {
modelType?: string;
};
}>,
reply: FastifyReply
) {
try {
console.log('📤 开始上传稿件进行审查...');
// 获取上传的文件
const data = await request.file();
if (!data) {
console.error('❌ 没有接收到文件');
return reply.status(400).send({
success: false,
message: 'No file uploaded',
});
}
console.log(`📄 接收到文件: ${data.filename}, 类型: ${data.mimetype}`);
const file = await data.toBuffer();
const filename = data.filename;
const fileType = data.mimetype;
const fileSizeBytes = file.length;
// 文件大小限制5MB稿件通常不会太大
const maxSize = 5 * 1024 * 1024;
console.log(`📊 文件大小: ${(fileSizeBytes / 1024 / 1024).toFixed(2)}MB (限制: 5MB)`);
if (fileSizeBytes > maxSize) {
console.error(`❌ 文件太大: ${(fileSizeBytes / 1024 / 1024).toFixed(2)}MB`);
return reply.status(400).send({
success: false,
message: 'File size exceeds 5MB limit',
});
}
// 文件类型限制仅支持Word文档
const allowedTypes = [
'application/msword', // .doc
'application/vnd.openxmlformats-officedocument.wordprocessingml.document', // .docx
];
console.log(`🔍 检查文件类型: ${fileType}`);
if (!allowedTypes.includes(fileType)) {
console.error(`❌ 不支持的文件类型: ${fileType}`);
return reply.status(400).send({
success: false,
message: 'File type not supported. Only Word documents (.doc, .docx) are allowed',
});
}
// 获取模型类型默认deepseek-v3
const modelType = (data.fields.modelType?.value || 'deepseek-v3') as ModelType;
// 验证模型类型
const validModels: ModelType[] = ['deepseek-v3', 'qwen3-72b', 'qwen-long'];
if (!validModels.includes(modelType)) {
return reply.status(400).send({
success: false,
message: `Invalid model type. Allowed: ${validModels.join(', ')}`,
});
}
console.log(`🤖 使用模型: ${modelType}`);
// 调用服务层进行审查
console.log('⚙️ 调用审查服务...');
const task = await reviewService.reviewManuscript(file, filename, MOCK_USER_ID, modelType);
console.log(`✅ 审查任务已创建: ${task.id}`);
return reply.send({
success: true,
message: 'Manuscript uploaded successfully. Review task created.',
data: {
taskId: task.id,
fileName: task.fileName,
status: task.status,
createdAt: task.createdAt,
},
});
} catch (error) {
console.error('❌ 上传稿件失败:', error);
return reply.status(500).send({
success: false,
message: error instanceof Error ? error.message : 'Upload failed',
});
}
}
/**
* 获取任务状态
* GET /api/review/tasks/:taskId
*/
export async function getTaskStatus(
request: FastifyRequest<{
Params: {
taskId: string;
};
}>,
reply: FastifyReply
) {
try {
const { taskId } = request.params;
console.log(`🔍 查询任务状态: ${taskId}`);
const task = await reviewService.getReviewTask(MOCK_USER_ID, taskId);
console.log(`✅ 任务状态: ${task.status}`);
return reply.send({
success: true,
data: {
id: task.id,
fileName: task.fileName,
fileSize: task.fileSize,
status: task.status,
wordCount: task.wordCount,
overallScore: task.overallScore,
modelUsed: task.modelUsed,
createdAt: task.createdAt,
startedAt: task.startedAt,
completedAt: task.completedAt,
durationSeconds: task.durationSeconds,
errorMessage: task.errorMessage,
},
});
} catch (error) {
console.error('❌ 查询任务状态失败:', error);
return reply.status(404).send({
success: false,
message: error instanceof Error ? error.message : 'Task not found',
});
}
}
/**
* 获取审查报告
* GET /api/review/tasks/:taskId/report
*/
export async function getTaskReport(
request: FastifyRequest<{
Params: {
taskId: string;
};
}>,
reply: FastifyReply
) {
try {
const { taskId } = request.params;
console.log(`📊 获取审查报告: ${taskId}`);
const report = await reviewService.getReviewReport(MOCK_USER_ID, taskId);
console.log(`✅ 报告已生成`);
return reply.send({
success: true,
data: report,
});
} catch (error) {
console.error('❌ 获取报告失败:', error);
// 如果报告尚未完成返回202状态
if (error instanceof Error && error.message.includes('not ready yet')) {
return reply.status(202).send({
success: false,
message: error.message,
});
}
return reply.status(404).send({
success: false,
message: error instanceof Error ? error.message : 'Report not found',
});
}
}
/**
* 获取任务列表
* GET /api/review/tasks
*/
export async function getTaskList(
request: FastifyRequest<{
Querystring: {
page?: string;
limit?: string;
};
}>,
reply: FastifyReply
) {
try {
const page = parseInt(request.query.page || '1', 10);
const limit = parseInt(request.query.limit || '20', 10);
console.log(`📋 获取任务列表: page=${page}, limit=${limit}`);
const result = await reviewService.getReviewTasks(MOCK_USER_ID, page, limit);
console.log(`✅ 找到 ${result.tasks.length} 个任务`);
return reply.send({
success: true,
data: result.tasks,
pagination: result.pagination,
});
} catch (error) {
console.error('❌ 获取任务列表失败:', error);
return reply.status(500).send({
success: false,
message: error instanceof Error ? error.message : 'Failed to get task list',
});
}
}
/**
* 删除任务
* DELETE /api/review/tasks/:taskId
*/
export async function deleteTask(
request: FastifyRequest<{
Params: {
taskId: string;
};
}>,
reply: FastifyReply
) {
try {
const { taskId } = request.params;
console.log(`🗑️ 删除任务: ${taskId}`);
await reviewService.deleteReviewTask(MOCK_USER_ID, taskId);
console.log(`✅ 任务已删除`);
return reply.send({
success: true,
message: 'Task deleted successfully',
});
} catch (error) {
console.error('❌ 删除任务失败:', error);
return reply.status(404).send({
success: false,
message: error instanceof Error ? error.message : 'Failed to delete task',
});
}
}

View File

@@ -0,0 +1,56 @@
import { FastifyInstance, FastifyRequest, FastifyReply } from 'fastify';
import { agentController } from '../controllers/agentController.js';
interface AgentParams {
id: string;
}
export async function agentRoutes(fastify: FastifyInstance) {
// 获取所有智能体列表
fastify.get('/agents', async (request: FastifyRequest, reply: FastifyReply) => {
return agentController.getAllAgents(request, reply);
});
// 获取启用的智能体列表
fastify.get('/agents/enabled', async (request: FastifyRequest, reply: FastifyReply) => {
return agentController.getEnabledAgents(request, reply);
});
// 根据分类获取智能体
fastify.get<{ Querystring: { category: string } }>(
'/agents/by-category',
async (request: FastifyRequest<{ Querystring: { category: string } }>, reply: FastifyReply) => {
return agentController.getAgentsByCategory(request, reply);
}
);
// 获取单个智能体详情
fastify.get<{ Params: AgentParams }>(
'/agents/:id',
async (request: FastifyRequest<{ Params: AgentParams }>, reply: FastifyReply) => {
return agentController.getAgentById(request, reply);
}
);
// 获取智能体的系统Prompt
fastify.get<{ Params: AgentParams }>(
'/agents/:id/system-prompt',
async (request: FastifyRequest<{ Params: AgentParams }>, reply: FastifyReply) => {
return agentController.getSystemPrompt(request, reply);
}
);
// 渲染用户Prompt预览
fastify.post<{ Params: AgentParams }>(
'/agents/:id/render-prompt',
async (request: FastifyRequest<{ Params: AgentParams }>, reply: FastifyReply) => {
return agentController.renderPrompt(request, reply);
}
);
// 重新加载配置(管理员功能)
fastify.post('/agents/reload-config', async (request: FastifyRequest, reply: FastifyReply) => {
return agentController.reloadConfig(request, reply);
});
}

View File

@@ -0,0 +1,38 @@
/**
* Phase 3: 批处理模式 - 路由配置
*/
import { FastifyInstance } from 'fastify';
import {
executeBatch,
getTask,
getTaskResults,
retryFailed,
getTemplates,
} from '../controllers/batchController.js';
export async function batchRoutes(fastify: FastifyInstance) {
// 执行批处理任务
fastify.post('/batch/execute', executeBatch);
// 获取任务状态
fastify.get('/batch/tasks/:taskId', getTask);
// 获取任务结果
fastify.get('/batch/tasks/:taskId/results', getTaskResults);
// 重试失败的文档
fastify.post('/batch/tasks/:taskId/retry-failed', retryFailed);
// 获取所有预设模板
fastify.get('/batch/templates', getTemplates);
}

View File

@@ -0,0 +1,15 @@
import { FastifyInstance } from 'fastify';
import { chatController } from '../controllers/chatController.js';
export async function chatRoutes(fastify: FastifyInstance) {
// 发送消息(流式输出)
fastify.post('/chat/stream', chatController.sendMessageStream.bind(chatController));
// 获取对话列表
fastify.get('/chat/conversations', chatController.getConversations.bind(chatController));
// 删除对话
fastify.delete('/chat/conversations/:id', chatController.deleteConversation.bind(chatController));
}

View File

@@ -0,0 +1,35 @@
import { FastifyInstance, FastifyRequest, FastifyReply } from 'fastify';
import { conversationController } from '../controllers/conversationController.js';
export async function conversationRoutes(fastify: FastifyInstance) {
// 创建对话
fastify.post('/conversations', async (request: FastifyRequest, reply: FastifyReply) => {
return conversationController.createConversation(request as any, reply);
});
// 获取对话列表
fastify.get('/conversations', async (request: FastifyRequest, reply: FastifyReply) => {
return conversationController.getConversations(request as any, reply);
});
// 获取对话详情
fastify.get('/conversations/:id', async (request: FastifyRequest, reply: FastifyReply) => {
return conversationController.getConversationById(request as any, reply);
});
// 发送消息(非流式)
fastify.post('/conversations/message', async (request: FastifyRequest, reply: FastifyReply) => {
return conversationController.sendMessage(request as any, reply);
});
// 发送消息(流式输出)
fastify.post('/conversations/message/stream', async (request: FastifyRequest, reply: FastifyReply) => {
return conversationController.sendMessageStream(request as any, reply);
});
// 删除对话
fastify.delete('/conversations/:id', async (request: FastifyRequest, reply: FastifyReply) => {
return conversationController.deleteConversation(request as any, reply);
});
}

View File

@@ -0,0 +1,53 @@
import type { FastifyInstance } from 'fastify';
import * as knowledgeBaseController from '../controllers/knowledgeBaseController.js';
import * as documentController from '../controllers/documentController.js';
export default async function knowledgeBaseRoutes(fastify: FastifyInstance) {
// ==================== 知识库管理 API ====================
// 创建知识库
fastify.post('/knowledge-bases', knowledgeBaseController.createKnowledgeBase);
// 获取知识库列表
fastify.get('/knowledge-bases', knowledgeBaseController.getKnowledgeBases);
// 获取知识库详情
fastify.get('/knowledge-bases/:id', knowledgeBaseController.getKnowledgeBaseById);
// 更新知识库
fastify.put('/knowledge-bases/:id', knowledgeBaseController.updateKnowledgeBase);
// 删除知识库
fastify.delete('/knowledge-bases/:id', knowledgeBaseController.deleteKnowledgeBase);
// 检索知识库
fastify.get('/knowledge-bases/:id/search', knowledgeBaseController.searchKnowledgeBase);
// 获取知识库统计信息
fastify.get('/knowledge-bases/:id/stats', knowledgeBaseController.getKnowledgeBaseStats);
// Phase 2: 获取文档选择(全文阅读模式)
fastify.get('/knowledge-bases/:id/document-selection', knowledgeBaseController.getDocumentSelection);
// ==================== 文档管理 API ====================
// 上传文档
fastify.post('/knowledge-bases/:kbId/documents', documentController.uploadDocument);
// 获取文档列表
fastify.get('/knowledge-bases/:kbId/documents', documentController.getDocuments);
// 获取文档详情
fastify.get('/documents/:id', documentController.getDocumentById);
// Phase 2: 获取文档全文
fastify.get('/documents/:id/full-text', documentController.getDocumentFullText);
// 删除文档
fastify.delete('/documents/:id', documentController.deleteDocument);
// 重新处理文档
fastify.post('/documents/:id/reprocess', documentController.reprocessDocument);
}

View File

@@ -0,0 +1,53 @@
import { FastifyInstance, FastifyRequest, FastifyReply } from 'fastify';
import { projectController } from '../controllers/projectController.js';
import { validateProjectCreate, validateProjectUpdate } from '../../common/middleware/validateProject.js';
interface ProjectParams {
id: string;
}
export async function projectRoutes(fastify: FastifyInstance) {
// 获取项目列表
fastify.get('/projects', async (request: FastifyRequest, reply: FastifyReply) => {
return projectController.getProjects(request, reply);
});
// 获取单个项目详情
fastify.get<{ Params: ProjectParams }>(
'/projects/:id',
async (request: FastifyRequest<{ Params: ProjectParams }>, reply: FastifyReply) => {
return projectController.getProjectById(request, reply);
}
);
// 创建项目
fastify.post(
'/projects',
{
preHandler: validateProjectCreate,
},
async (request: FastifyRequest, reply: FastifyReply) => {
return projectController.createProject(request, reply);
}
);
// 更新项目
fastify.put<{ Params: ProjectParams }>(
'/projects/:id',
{
preHandler: validateProjectUpdate,
},
async (request: FastifyRequest<{ Params: ProjectParams }>, reply: FastifyReply) => {
return projectController.updateProject(request, reply);
}
);
// 删除项目
fastify.delete<{ Params: ProjectParams }>(
'/projects/:id',
async (request: FastifyRequest<{ Params: ProjectParams }>, reply: FastifyReply) => {
return projectController.deleteProject(request, reply);
}
);
}

View File

@@ -0,0 +1,50 @@
import type { FastifyInstance } from 'fastify';
import * as reviewController from '../controllers/reviewController.js';
export default async function reviewRoutes(fastify: FastifyInstance) {
// ==================== 稿件审查 API ====================
// 上传稿件并开始审查
fastify.post('/review/upload', reviewController.uploadManuscript);
// 获取任务状态
fastify.get('/review/tasks/:taskId', reviewController.getTaskStatus);
// 获取审查报告
fastify.get('/review/tasks/:taskId/report', reviewController.getTaskReport);
// 获取任务列表
fastify.get('/review/tasks', reviewController.getTaskList);
// 删除任务
fastify.delete('/review/tasks/:taskId', reviewController.deleteTask);
}

View File

@@ -0,0 +1,215 @@
import fs from 'fs';
import path from 'path';
import yaml from 'js-yaml';
import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
// 智能体配置接口
export interface AgentConfig {
id: string;
name: string;
nameEn: string;
description: string;
category: string;
icon: string;
enabled: boolean;
systemPromptFile: string;
userPromptTemplateFile: string;
models: {
[modelName: string]: {
temperature: number;
maxTokens: number;
topP?: number;
};
};
ragEnabled: boolean;
requiresProject: boolean;
outputFormat: 'text' | 'structured' | 'document';
tags: string[];
}
// 配置文件根结构
interface AgentsConfigFile {
agents: AgentConfig[];
}
class AgentService {
private agents: Map<string, AgentConfig> = new Map();
private prompts: Map<string, string> = new Map();
private configPath: string;
private promptsPath: string;
constructor() {
// 配置文件路径
this.configPath = path.resolve(__dirname, '../../../config/agents.yaml');
this.promptsPath = path.resolve(__dirname, '../../../prompts');
// 初始化加载配置
this.loadAgents();
}
// 加载智能体配置
private loadAgents() {
try {
const fileContents = fs.readFileSync(this.configPath, 'utf8');
const config = yaml.load(fileContents) as AgentsConfigFile;
if (!config || !config.agents) {
throw new Error('Invalid agents configuration file');
}
// 存储到Map中
config.agents.forEach((agent) => {
this.agents.set(agent.id, agent);
});
console.log(`✅ Loaded ${this.agents.size} agent configurations`);
} catch (error) {
console.error('❌ Failed to load agent configurations:', error);
throw error;
}
}
// 加载Prompt模板
private loadPrompt(filename: string): string {
const cacheKey = filename;
// 检查缓存
if (this.prompts.has(cacheKey)) {
return this.prompts.get(cacheKey)!;
}
try {
const promptPath = path.join(this.promptsPath, filename);
const content = fs.readFileSync(promptPath, 'utf8');
// 缓存到内存
this.prompts.set(cacheKey, content);
return content;
} catch (error) {
console.error(`❌ Failed to load prompt file: ${filename}`, error);
throw new Error(`Prompt file not found: ${filename}`);
}
}
// 获取所有智能体列表
getAllAgents(): AgentConfig[] {
return Array.from(this.agents.values());
}
// 获取启用的智能体列表
getEnabledAgents(): AgentConfig[] {
return Array.from(this.agents.values()).filter((agent) => agent.enabled);
}
// 根据ID获取智能体配置
getAgentById(agentId: string): AgentConfig | null {
return this.agents.get(agentId) || null;
}
// 根据分类获取智能体列表
getAgentsByCategory(category: string): AgentConfig[] {
return Array.from(this.agents.values()).filter(
(agent) => agent.category === category
);
}
// 获取智能体的系统Prompt
getSystemPrompt(agentId: string): string {
const agent = this.getAgentById(agentId);
if (!agent) {
throw new Error(`Agent not found: ${agentId}`);
}
return this.loadPrompt(agent.systemPromptFile);
}
// 获取智能体的用户Prompt模板
getUserPromptTemplate(agentId: string): string {
const agent = this.getAgentById(agentId);
if (!agent) {
throw new Error(`Agent not found: ${agentId}`);
}
return this.loadPrompt(agent.userPromptTemplateFile);
}
// 渲染用户Prompt替换模板变量
renderUserPrompt(
agentId: string,
variables: {
projectBackground?: string;
userInput: string;
knowledgeBaseContext?: string;
}
): string {
const template = this.getUserPromptTemplate(agentId);
let rendered = template;
// 替换变量
rendered = rendered.replace(/\{\{projectBackground\}\}/g, variables.projectBackground || '未提供项目背景');
rendered = rendered.replace(/\{\{userInput\}\}/g, variables.userInput);
// 处理条件块(知识库上下文)
if (variables.knowledgeBaseContext) {
rendered = rendered.replace(
/\{\{#if knowledgeBaseContext\}\}([\s\S]*?)\{\{\/if\}\}/g,
'$1'
);
rendered = rendered.replace(/\{\{knowledgeBaseContext\}\}/g, variables.knowledgeBaseContext);
} else {
// 移除条件块
rendered = rendered.replace(
/\{\{#if knowledgeBaseContext\}\}[\s\S]*?\{\{\/if\}\}/g,
''
);
}
return rendered.trim();
}
// 检查智能体是否存在
agentExists(agentId: string): boolean {
return this.agents.has(agentId);
}
// 检查智能体是否启用
isAgentEnabled(agentId: string): boolean {
const agent = this.getAgentById(agentId);
return agent ? agent.enabled : false;
}
// 获取智能体的模型配置
getModelConfig(agentId: string, modelName: string) {
const agent = this.getAgentById(agentId);
if (!agent) {
throw new Error(`Agent not found: ${agentId}`);
}
const modelConfig = agent.models[modelName];
if (!modelConfig) {
throw new Error(`Model ${modelName} not configured for agent ${agentId}`);
}
return modelConfig;
}
// 重新加载配置(热更新)
reloadConfig() {
this.agents.clear();
this.prompts.clear();
this.loadAgents();
console.log('✅ Agent configurations reloaded');
}
}
// 导出单例
export const agentService = new AgentService();

View File

@@ -0,0 +1,420 @@
/**
* Phase 3: 批处理模式 - 批处理服务
*
* 核心功能:
* 1. 执行批处理任务3并发
* 2. 处理单个文档
* 3. 进度推送WebSocket
* 4. 错误处理和重试
*/
import PQueue from 'p-queue';
import { prisma } from '../../config/database.js';
import { LLMFactory } from '../../common/llm/adapters/LLMFactory.js';
import { ModelType } from '../../common/llm/adapters/types.js';
import { getTemplate } from '../templates/clinicalResearch.js';
import { parseJSON } from '../../common/utils/jsonParser.js';
export interface ExecuteBatchTaskParams {
userId: string;
kbId: string;
documentIds: string[];
templateType: 'preset' | 'custom';
templateId?: string; // 预设模板ID
customPrompt?: string; // 自定义提示词
modelType: ModelType;
taskName?: string;
existingTaskId?: string; // 已存在的任务ID可选
onProgress?: (progress: BatchProgress) => void;
}
export interface BatchProgress {
taskId: string;
completed: number;
total: number;
failed: number;
currentDocument?: string;
estimatedSeconds?: number;
}
export interface BatchTaskResult {
taskId: string;
status: 'processing' | 'completed' | 'failed';
totalDocuments: number;
completedCount: number;
failedCount: number;
durationSeconds?: number;
}
/**
* 执行批处理任务
*/
export async function executeBatchTask(
params: ExecuteBatchTaskParams
): Promise<BatchTaskResult> {
const {
userId,
kbId,
documentIds,
templateType,
templateId,
customPrompt,
modelType,
taskName,
existingTaskId,
onProgress,
} = params;
console.log('📦 [BatchService] 开始执行批处理任务', {
userId,
kbId,
documentCount: documentIds.length,
templateType,
modelType,
existingTaskId: existingTaskId || '新建',
});
// 验证文献数量 (3-50篇)
if (documentIds.length < 3 || documentIds.length > 50) {
throw new Error(`文献数量必须在3-50篇之间当前${documentIds.length}`);
}
// 获取模板或使用自定义提示词
let systemPrompt: string;
let userPromptTemplate: string;
let expectedFields: string[] = [];
if (templateType === 'preset') {
if (!templateId) {
throw new Error('预设模板类型需要提供templateId');
}
const template = getTemplate(templateId);
if (!template) {
throw new Error(`未找到模板: ${templateId}`);
}
systemPrompt = template.systemPrompt;
userPromptTemplate = template.userPrompt;
expectedFields = template.outputFields.map(f => f.key);
} else {
// 自定义模板
if (!customPrompt) {
throw new Error('自定义模板需要提供customPrompt');
}
systemPrompt = '你是一个专业的文献分析助手。请根据用户的要求分析文献内容。';
userPromptTemplate = customPrompt;
}
// 使用已存在的任务或创建新任务
let task;
if (existingTaskId) {
task = await prisma.batchTask.findUnique({
where: { id: existingTaskId },
});
if (!task) {
throw new Error(`任务不存在: ${existingTaskId}`);
}
console.log(`✅ [BatchService] 使用已存在的任务: ${task.id}`);
} else {
task = await prisma.batchTask.create({
data: {
userId,
kbId,
name: taskName || `批处理任务_${new Date().toLocaleString('zh-CN')}`,
templateType,
templateId: templateId || null,
prompt: userPromptTemplate,
status: 'processing',
totalDocuments: documentIds.length,
completedCount: 0,
failedCount: 0,
modelType,
concurrency: 3, // 固定3并发
startedAt: new Date(),
},
});
console.log(`✅ [BatchService] 创建任务记录: ${task.id}`);
}
const startTime = Date.now();
let completedCount = 0;
let failedCount = 0;
// 创建并发队列固定3并发
const queue = new PQueue({ concurrency: 3 });
// 处理所有文档
const promises = documentIds.map((docId, index) =>
queue.add(async () => {
try {
console.log(`🔄 [BatchService] 处理文档 ${index + 1}/${documentIds.length}: ${docId}`);
// 获取文档
const document = await prisma.document.findUnique({
where: { id: docId },
select: {
id: true,
filename: true,
extractedText: true,
tokensCount: true,
},
});
if (!document) {
throw new Error(`文档不存在: ${docId}`);
}
if (!document.extractedText) {
throw new Error(`文档未提取文本: ${document.filename}`);
}
// 调用LLM处理
const result = await processDocument({
document,
systemPrompt,
userPromptTemplate,
modelType,
templateType,
expectedFields,
});
// 保存结果
await prisma.batchResult.create({
data: {
taskId: task.id,
documentId: docId,
status: 'success',
data: result.data,
rawOutput: result.rawOutput,
processingTimeMs: result.processingTimeMs,
tokensUsed: result.tokensUsed,
},
});
completedCount++;
console.log(`✅ [BatchService] 文档处理成功: ${document.filename} (${result.processingTimeMs}ms)`);
} catch (error: any) {
// 处理失败
console.error(`❌ [BatchService] 文档处理失败: ${docId}`, error);
await prisma.batchResult.create({
data: {
taskId: task.id,
documentId: docId,
status: 'failed',
errorMessage: error.message,
},
});
failedCount++;
}
// 推送进度
if (onProgress) {
const progress: BatchProgress = {
taskId: task.id,
completed: completedCount + failedCount,
total: documentIds.length,
failed: failedCount,
estimatedSeconds: calculateEstimatedTime(
completedCount + failedCount,
documentIds.length,
Date.now() - startTime
),
};
onProgress(progress);
}
// 更新任务进度
await prisma.batchTask.update({
where: { id: task.id },
data: {
completedCount,
failedCount,
},
});
})
);
// 等待所有任务完成
await Promise.allSettled(promises);
// 计算总时长
const durationSeconds = Math.round((Date.now() - startTime) / 1000);
// 更新任务状态
await prisma.batchTask.update({
where: { id: task.id },
data: {
status: 'completed',
completedAt: new Date(),
durationSeconds,
},
});
console.log(`🎉 [BatchService] 批处理任务完成: ${task.id}`, {
total: documentIds.length,
success: completedCount,
failed: failedCount,
durationSeconds,
});
return {
taskId: task.id,
status: 'completed',
totalDocuments: documentIds.length,
completedCount,
failedCount,
durationSeconds,
};
}
/**
* 处理单个文档
*/
async function processDocument(params: {
document: {
id: string;
filename: string;
extractedText: string;
tokensCount: number | null;
};
systemPrompt: string;
userPromptTemplate: string;
modelType: ModelType;
templateType: 'preset' | 'custom';
expectedFields: string[];
}): Promise<{
data: any;
rawOutput: string;
processingTimeMs: number;
tokensUsed?: number;
}> {
const {
document,
systemPrompt,
userPromptTemplate,
modelType,
templateType,
expectedFields,
} = params;
const startTime = Date.now();
// 构造完整的用户消息
const userMessage = `${userPromptTemplate}\n\n【文献${document.filename}\n\n${document.extractedText}`;
// 调用LLM
const adapter = LLMFactory.getAdapter(modelType);
const response = await adapter.chat(
[
{ role: 'system', content: systemPrompt },
{ role: 'user', content: userMessage },
],
{
temperature: 0.3, // 降低温度提高稳定性
maxTokens: 2000,
}
);
const processingTimeMs = Date.now() - startTime;
const rawOutput = response.content;
// 解析结果
let data: any;
if (templateType === 'preset') {
// 预设模板解析JSON
const parseResult = parseJSON(rawOutput, expectedFields);
if (!parseResult.success) {
throw new Error(`JSON解析失败: ${parseResult.error}`);
}
data = parseResult.data;
} else {
// 自定义模板:直接使用文本
data = {
extracted_text: rawOutput,
};
}
return {
data,
rawOutput,
processingTimeMs,
tokensUsed: response.usage?.totalTokens,
};
}
/**
* 计算预估剩余时间
*/
function calculateEstimatedTime(
completed: number,
total: number,
elapsedMs: number
): number {
if (completed === 0) return 0;
const avgTimePerDoc = elapsedMs / completed;
const remaining = total - completed;
return Math.round((avgTimePerDoc * remaining) / 1000);
}
/**
* 重试失败的文档
*/
export async function retryFailedDocuments(
taskId: string,
onProgress?: (progress: BatchProgress) => void
): Promise<{ retriedCount: number }> {
console.log(`🔄 [BatchService] 重试失败文档: ${taskId}`);
// 获取任务信息
const task = await prisma.batchTask.findUnique({
where: { id: taskId },
include: {
results: {
where: { status: 'failed' },
},
},
});
if (!task) {
throw new Error(`任务不存在: ${taskId}`);
}
const failedDocIds = task.results.map(r => r.documentId);
if (failedDocIds.length === 0) {
return { retriedCount: 0 };
}
// 删除旧的失败记录
await prisma.batchResult.deleteMany({
where: {
taskId,
status: 'failed',
},
});
// 重新执行
await executeBatchTask({
userId: task.userId,
kbId: task.kbId,
documentIds: failedDocIds,
templateType: task.templateType as 'preset' | 'custom',
templateId: task.templateId || undefined,
customPrompt: task.templateType === 'custom' ? task.prompt : undefined,
modelType: task.modelType as ModelType,
taskName: `${task.name} (重试)`,
onProgress,
});
return { retriedCount: failedDocIds.length };
}

View File

@@ -0,0 +1,624 @@
import { prisma } from '../../config/database.js';
import { LLMFactory } from '../../common/llm/adapters/LLMFactory.js';
import { Message, ModelType, StreamChunk } from '../../common/llm/adapters/types.js';
import { agentService } from './agentService.js';
import * as knowledgeBaseService from './knowledgeBaseService.js';
/**
* 引用信息接口
*/
interface Citation {
id: number;
fileName: string;
position: number;
score: number;
content: string;
}
/**
* 提取文本片段(用于引用上下文)
* @param text 完整文本
* @param maxLength 最大长度默认100字
* @returns 提取的片段
*/
function extractContextPreview(text: string, maxLength: number = 100): string {
if (!text) return '';
// 移除多余的空白字符
const cleaned = text.replace(/\s+/g, ' ').trim();
// 如果文本短于限制,直接返回
if (cleaned.length <= maxLength) {
return cleaned;
}
// 截取前maxLength个字符并尝试在句号、问号、感叹号处截断
const truncated = cleaned.substring(0, maxLength);
const lastPunctuation = Math.max(
truncated.lastIndexOf('。'),
truncated.lastIndexOf(''),
truncated.lastIndexOf(''),
truncated.lastIndexOf('.'),
truncated.lastIndexOf('!'),
truncated.lastIndexOf('?')
);
// 如果找到了标点符号,在标点后截断;否则直接截断并加省略号
if (lastPunctuation > maxLength * 0.5) {
return truncated.substring(0, lastPunctuation + 1);
}
return truncated + '...';
}
/**
* 格式化引用清单
* @param citations 引用列表
* @returns 格式化的引用清单字符串
*/
function formatCitations(citations: Citation[]): string {
if (citations.length === 0) return '';
let result = '\n\n---\n\n📚 **参考文献**\n\n';
for (const cite of citations) {
const scorePercent = (cite.score * 100).toFixed(0);
const preview = extractContextPreview(cite.content, 100);
// 使用HTML span标签给引用编号添加id方便跳转
result += `<span id="citation-detail-${cite.id}">[${cite.id}]</span> 📄 **${cite.fileName}** - 第${cite.position}段 (相关度${scorePercent}%)\n`;
result += ` "${preview}"\n\n`;
}
return result;
}
interface CreateConversationData {
userId: string;
projectId: string;
agentId: string;
title?: string;
}
interface SendMessageData {
conversationId: string;
content: string;
modelType: ModelType;
knowledgeBaseIds?: string[];
}
export class ConversationService {
/**
* 创建新对话
*/
async createConversation(data: CreateConversationData) {
const { userId, projectId, agentId, title } = data;
// 验证智能体是否存在
const agent = agentService.getAgentById(agentId);
if (!agent) {
throw new Error('智能体不存在');
}
// 验证项目是否存在
const project = await prisma.project.findFirst({
where: {
id: projectId,
userId: userId,
deletedAt: null,
},
});
if (!project) {
throw new Error('项目不存在或无权访问');
}
// 创建对话
const conversation = await prisma.conversation.create({
data: {
userId,
projectId,
agentId,
title: title || `${agent.name}的对话`,
metadata: {
agentName: agent.name,
agentCategory: agent.category,
},
},
});
return conversation;
}
/**
* 获取对话列表
*/
async getConversations(userId: string, projectId?: string) {
const where: any = {
userId,
deletedAt: null,
};
if (projectId) {
where.projectId = projectId;
}
const conversations = await prisma.conversation.findMany({
where,
include: {
project: {
select: {
id: true,
name: true,
},
},
_count: {
select: {
messages: true,
},
},
},
orderBy: {
updatedAt: 'desc',
},
});
return conversations;
}
/**
* 获取对话详情(包含消息)
*/
async getConversationById(conversationId: string, userId: string) {
const conversation = await prisma.conversation.findFirst({
where: {
id: conversationId,
userId,
deletedAt: null,
},
include: {
project: {
select: {
id: true,
name: true,
background: true,
researchType: true,
},
},
messages: {
orderBy: {
createdAt: 'asc',
},
},
},
});
if (!conversation) {
throw new Error('对话不存在或无权访问');
}
return conversation;
}
/**
* 组装上下文消息
*/
private async assembleContext(
conversationId: string,
agentId: string,
projectBackground: string,
userInput: string,
knowledgeBaseContext?: string
): Promise<Message[]> {
console.log('🔧 [assembleContext] 开始组装上下文', {
conversationId,
agentId,
hasKnowledgeBaseContext: !!knowledgeBaseContext,
knowledgeBaseContextLength: knowledgeBaseContext?.length || 0
});
// 获取系统Prompt
const systemPrompt = agentService.getSystemPrompt(agentId);
// 获取历史消息最近100条约50轮对话
// DeepSeek-V3支持64K tokens实际可容纳100-200轮对话
const historyMessages = await prisma.message.findMany({
where: {
conversationId,
},
orderBy: {
createdAt: 'desc',
},
take: 100,
});
// 反转顺序(最早的在前)
historyMessages.reverse();
// 判断是否是第一条消息
const isFirstMessage = historyMessages.length === 0;
console.log(`📜 [assembleContext] 历史消息数: ${historyMessages.length}, 是否首次: ${isFirstMessage}`);
// 渲染用户Prompt
let userPromptContent: string;
if (isFirstMessage) {
// 第一条消息:使用完整模板(包含项目背景)
userPromptContent = agentService.renderUserPrompt(agentId, {
projectBackground,
userInput,
knowledgeBaseContext,
});
console.log(`📝 [assembleContext] 首次消息,使用完整模板,长度: ${userPromptContent.length}`);
console.log(`📋 [assembleContext] userPromptContent完整内容:\n${userPromptContent}`);
console.log(`🔍 [assembleContext] 是否包含"参考文献": ${userPromptContent.includes('参考文献')}`);
console.log(`🔍 [assembleContext] 是否包含知识库内容: ${userPromptContent.includes('阿尔兹海默症')}`);
} else {
// 后续消息:只发送用户输入和知识库上下文(如果有)
if (knowledgeBaseContext) {
userPromptContent = `${userInput}\n\n## 参考文献(来自知识库)\n\n**重要提示**:下面提供的文献片段已经用[来源N]进行了标记。请在回答中引用具体来源时使用对应的编号,如"根据[来源1]..."或"研究表明[来源3][来源5]..."。系统会在你回答结束后自动显示完整的引用清单。\n\n${knowledgeBaseContext}`;
console.log(`📝 [assembleContext] 后续消息+知识库,总长度: ${userPromptContent.length}`);
console.log(`📋 [assembleContext] userPromptContent预览:\n${userPromptContent.substring(0, 300)}...`);
} else {
userPromptContent = userInput;
console.log(`📝 [assembleContext] 后续消息,仅用户输入: ${userPromptContent}`);
}
}
// 组装消息数组
const messages: Message[] = [
{
role: 'system',
content: systemPrompt,
},
];
// 添加历史消息
for (const msg of historyMessages) {
messages.push({
role: msg.role as 'user' | 'assistant',
content: msg.content,
});
}
// 添加当前用户输入
messages.push({
role: 'user',
content: userPromptContent,
});
console.log(`✅ [assembleContext] 组装完成,消息总数: ${messages.length}`);
return messages;
}
/**
* 发送消息(非流式)
*/
async sendMessage(data: SendMessageData, userId: string) {
const { conversationId, content, modelType, knowledgeBaseIds } = data;
// 获取对话信息
const conversation = await this.getConversationById(conversationId, userId);
// 获取知识库上下文(如果有@知识库)
let knowledgeBaseContext = '';
const allCitations: Citation[] = []; // 存储所有引用信息
let citationCounter = 1; // 全局引用计数器
if (knowledgeBaseIds && knowledgeBaseIds.length > 0) {
const knowledgeResults: string[] = [];
// 对每个知识库进行检索
for (const kbId of knowledgeBaseIds) {
try {
const searchResult = await knowledgeBaseService.searchKnowledgeBase(
userId,
kbId,
content,
15 // Phase 1优化从3增加到15个最相关的段落
);
// 格式化检索结果
if (searchResult.records && searchResult.records.length > 0) {
const kbInfo = await prisma.knowledgeBase.findUnique({
where: { id: kbId },
select: { name: true },
});
// 优化格式:使用[来源N]标记便于AI引用
const formattedResult = `【知识库:${kbInfo?.name || '未命名'}\n` +
searchResult.records
.map((record: any) => {
const citationId = citationCounter++;
const score = (record.score * 100).toFixed(1);
// 保存引用信息
allCitations.push({
id: citationId,
fileName: record.segment?.document?.name || record.document_name || '未知文档',
position: record.segment?.position || record.segment_position || 0,
score: record.score,
content: record.segment?.content || record.content || '',
});
return `[来源${citationId}] [相关度${score}%]\n${record.segment?.content || record.content}`;
})
.join('\n\n');
knowledgeResults.push(formattedResult);
}
} catch (error) {
console.error(`Failed to search knowledge base ${kbId}:`, error);
// 检索失败不阻止对话,继续处理
}
}
if (knowledgeResults.length > 0) {
knowledgeBaseContext = knowledgeResults.join('\n\n---\n\n');
}
}
// 组装上下文
const messages = await this.assembleContext(
conversationId,
conversation.agentId,
conversation.project?.background || '',
content,
knowledgeBaseContext
);
// 获取LLM适配器
const adapter = LLMFactory.getAdapter(modelType);
// 获取智能体配置的模型参数
const agent = agentService.getAgentById(conversation.agentId);
const modelConfig = agent?.models?.[modelType];
// 调用LLM
const response = await adapter.chat(messages, {
temperature: modelConfig?.temperature,
maxTokens: modelConfig?.maxTokens,
topP: modelConfig?.topP,
});
// AI回答完毕后追加引用清单
let finalContent = response.content;
if (allCitations.length > 0) {
const citationsText = formatCitations(allCitations);
finalContent += citationsText;
}
// 保存用户消息
const userMessage = await prisma.message.create({
data: {
conversationId,
role: 'user',
content,
metadata: {
knowledgeBaseIds,
},
},
});
// 保存助手回复
const assistantMessage = await prisma.message.create({
data: {
conversationId,
role: 'assistant',
content: finalContent,
model: response.model,
tokens: response.usage?.totalTokens,
metadata: {
usage: response.usage,
finishReason: response.finishReason,
},
},
});
// 更新对话的最后更新时间
await prisma.conversation.update({
where: { id: conversationId },
data: { updatedAt: new Date() },
});
return {
userMessage,
assistantMessage,
usage: response.usage,
};
}
/**
* 发送消息(流式)
*/
async *sendMessageStream(
data: SendMessageData,
userId: string
): AsyncGenerator<StreamChunk, void, unknown> {
const { conversationId, content, modelType, knowledgeBaseIds } = data;
// 获取对话信息
const conversation = await this.getConversationById(conversationId, userId);
// 获取知识库上下文(如果有@知识库)
console.log('📚 [sendMessageStream] 开始处理知识库', { knowledgeBaseIds });
let knowledgeBaseContext = '';
const allCitations: Citation[] = []; // 存储所有引用信息
let citationCounter = 1; // 全局引用计数器
if (knowledgeBaseIds && knowledgeBaseIds.length > 0) {
const knowledgeResults: string[] = [];
// 对每个知识库进行检索
for (const kbId of knowledgeBaseIds) {
try {
console.log(`🔎 [sendMessageStream] 检索知识库 ${kbId}`);
const searchResult = await knowledgeBaseService.searchKnowledgeBase(
userId,
kbId,
content,
15 // Phase 1优化从3增加到15个最相关的段落
);
console.log(`✅ [sendMessageStream] 检索结果`, {
kbId,
recordCount: searchResult.records?.length || 0
});
// 格式化检索结果
if (searchResult.records && searchResult.records.length > 0) {
const kbInfo = await prisma.knowledgeBase.findUnique({
where: { id: kbId },
select: { name: true },
});
// 优化格式:使用[来源N]标记便于AI引用
const formattedResult = `【知识库:${kbInfo?.name || '未命名'}\n` +
searchResult.records
.map((record: any) => {
const citationId = citationCounter++;
const score = (record.score * 100).toFixed(1);
// 保存引用信息
allCitations.push({
id: citationId,
fileName: record.segment?.document?.name || record.document_name || '未知文档',
position: record.segment?.position || record.segment_position || 0,
score: record.score,
content: record.segment?.content || record.content || '',
});
return `[来源${citationId}] [相关度${score}%]\n${record.segment?.content || record.content}`;
})
.join('\n\n');
console.log(`📄 [sendMessageStream] 格式化结果长度: ${formattedResult.length} 字符`);
knowledgeResults.push(formattedResult);
} else {
console.warn(`⚠️ [sendMessageStream] 知识库 ${kbId} 没有检索到记录`);
}
} catch (error) {
console.error(`❌ [sendMessageStream] 检索知识库失败 ${kbId}:`, error);
// 检索失败不阻止对话,继续处理
}
}
if (knowledgeResults.length > 0) {
knowledgeBaseContext = knowledgeResults.join('\n\n---\n\n');
console.log(`💾 [sendMessageStream] 知识库上下文总长度: ${knowledgeBaseContext.length} 字符`);
console.log(`📚 [sendMessageStream] 收集到 ${allCitations.length} 个引用`);
} else {
console.warn('⚠️ [sendMessageStream] 没有构建任何知识库上下文');
}
} else {
console.log(' [sendMessageStream] 未选择知识库');
}
// 组装上下文
const messages = await this.assembleContext(
conversationId,
conversation.agentId,
conversation.project?.background || '',
content,
knowledgeBaseContext
);
// 获取LLM适配器
const adapter = LLMFactory.getAdapter(modelType);
// 获取智能体配置的模型参数
const agent = agentService.getAgentById(conversation.agentId);
const modelConfig = agent?.models?.[modelType];
// 保存用户消息
await prisma.message.create({
data: {
conversationId,
role: 'user',
content,
metadata: {
knowledgeBaseIds,
},
},
});
// 用于累积完整的回复内容
let fullContent = '';
let usage: any = null;
// 流式调用LLM
for await (const chunk of adapter.chatStream(messages, {
temperature: modelConfig?.temperature,
maxTokens: modelConfig?.maxTokens,
topP: modelConfig?.topP,
})) {
fullContent += chunk.content;
if (chunk.usage) {
usage = chunk.usage;
}
yield chunk;
}
// AI回答完毕后追加引用清单
if (allCitations.length > 0) {
console.log(`📚 [sendMessageStream] 追加 ${allCitations.length} 个引用清单`);
const citationsText = formatCitations(allCitations);
fullContent += citationsText;
// 将引用清单也流式输出
yield {
content: citationsText,
done: false,
};
}
// 流式输出完成后,保存助手回复
await prisma.message.create({
data: {
conversationId,
role: 'assistant',
content: fullContent,
model: modelType,
tokens: usage?.totalTokens,
metadata: {
usage,
},
},
});
// 更新对话的最后更新时间
await prisma.conversation.update({
where: { id: conversationId },
data: { updatedAt: new Date() },
});
}
/**
* 删除对话(软删除)
*/
async deleteConversation(conversationId: string, userId: string) {
const conversation = await prisma.conversation.findFirst({
where: {
id: conversationId,
userId,
deletedAt: null,
},
});
if (!conversation) {
throw new Error('对话不存在或无权访问');
}
await prisma.conversation.update({
where: { id: conversationId },
data: { deletedAt: new Date() },
});
return { success: true };
}
}
export const conversationService = new ConversationService();

View File

@@ -0,0 +1,360 @@
import { prisma } from '../../config/database.js';
import { difyClient } from '../../common/rag/DifyClient.js';
import { extractionClient } from '../../common/document/ExtractionClient.js';
/**
* 文档服务
*/
/**
* 上传文档到知识库
*/
export async function uploadDocument(
userId: string,
kbId: string,
file: Buffer,
filename: string,
fileType: string,
fileSizeBytes: number,
fileUrl: string
) {
// 1. 验证知识库权限
const knowledgeBase = await prisma.knowledgeBase.findFirst({
where: {
id: kbId,
userId,
},
});
if (!knowledgeBase) {
throw new Error('Knowledge base not found or access denied');
}
// 2. 检查文档数量限制每个知识库最多50个文档
const documentCount = await prisma.document.count({
where: { kbId },
});
if (documentCount >= 50) {
throw new Error('Document limit exceeded. Maximum 50 documents per knowledge base');
}
// 3. 在数据库中创建文档记录状态uploading
const document = await prisma.document.create({
data: {
kbId,
userId,
filename,
fileType,
fileSizeBytes,
fileUrl,
difyDocumentId: '', // 暂时为空,稍后更新
status: 'uploading',
progress: 0,
},
});
try {
// 4. Phase 2: 调用提取服务提取文本内容
let extractionResult;
let extractedText = '';
let extractionMethod = '';
let extractionQuality: number | null = null;
let charCount: number | null = null;
let detectedLanguage: string | null = null;
try {
console.log(`[Phase2] 开始提取文档: ${filename}`);
extractionResult = await extractionClient.extractDocument(file, filename);
if (extractionResult.success) {
extractedText = extractionResult.text;
extractionMethod = extractionResult.method;
extractionQuality = extractionResult.quality || null;
charCount = extractionResult.metadata?.char_count || null;
detectedLanguage = extractionResult.language || null;
console.log(`[Phase2] 提取成功: method=${extractionMethod}, chars=${charCount}, language=${detectedLanguage}`);
}
} catch (extractionError) {
console.error('[Phase2] 文档提取失败但继续上传到Dify:', extractionError);
// 提取失败不影响Dify上传但记录错误
}
// 5. 上传到Dify
const difyResult = await difyClient.uploadDocumentDirectly(
knowledgeBase.difyDatasetId,
file,
filename
);
// 6. 更新文档记录更新difyDocumentId、状态和Phase 2字段
const updatedDocument = await prisma.document.update({
where: { id: document.id },
data: {
difyDocumentId: difyResult.document.id,
status: difyResult.document.indexing_status,
progress: 50,
// Phase 2新增字段
extractedText: extractedText || null,
extractionMethod: extractionMethod || null,
extractionQuality: extractionQuality,
charCount: charCount,
language: detectedLanguage,
},
});
// 7. 启动后台轮询任务,等待处理完成
pollDocumentStatus(userId, kbId, document.id, difyResult.document.id).catch(error => {
console.error('Failed to poll document status:', error);
});
// 8. 更新知识库统计
await updateKnowledgeBaseStats(kbId);
// 9. 转换BigInt为Number
return {
...updatedDocument,
fileSizeBytes: Number(updatedDocument.fileSizeBytes),
};
} catch (error) {
// 上传失败更新状态为error
await prisma.document.update({
where: { id: document.id },
data: {
status: 'error',
errorMessage: error instanceof Error ? error.message : 'Upload failed',
},
});
throw error;
}
}
/**
* 轮询文档处理状态
*/
async function pollDocumentStatus(
userId: string,
kbId: string,
documentId: string,
difyDocumentId: string,
maxAttempts: number = 30
) {
const knowledgeBase = await prisma.knowledgeBase.findFirst({
where: { id: kbId, userId },
});
if (!knowledgeBase) {
return;
}
for (let i = 0; i < maxAttempts; i++) {
await new Promise(resolve => setTimeout(resolve, 2000)); // 等待2秒
try {
// 查询Dify中的文档状态
const difyDocument = await difyClient.getDocument(
knowledgeBase.difyDatasetId,
difyDocumentId
);
// 更新数据库中的状态
await prisma.document.update({
where: { id: documentId },
data: {
status: difyDocument.indexing_status,
progress: difyDocument.indexing_status === 'completed' ? 100 : 50 + (i * 2),
segmentsCount: difyDocument.indexing_status === 'completed' ? difyDocument.word_count : null,
tokensCount: difyDocument.indexing_status === 'completed' ? difyDocument.tokens : null,
processedAt: difyDocument.indexing_status === 'completed' ? new Date() : null,
errorMessage: difyDocument.error || null,
},
});
// 如果完成或失败,退出轮询
if (difyDocument.indexing_status === 'completed') {
await updateKnowledgeBaseStats(kbId);
break;
}
if (difyDocument.indexing_status === 'error') {
break;
}
} catch (error) {
console.error(`Polling attempt ${i + 1} failed:`, error);
}
}
}
/**
* 获取文档列表
*/
export async function getDocuments(userId: string, kbId: string) {
// 1. 验证权限
const knowledgeBase = await prisma.knowledgeBase.findFirst({
where: {
id: kbId,
userId,
},
});
if (!knowledgeBase) {
throw new Error('Knowledge base not found or access denied');
}
// 2. 查询文档列表
const documents = await prisma.document.findMany({
where: { kbId },
orderBy: { uploadedAt: 'desc' },
});
// 3. 转换BigInt为Number
return documents.map(doc => ({
...doc,
fileSizeBytes: Number(doc.fileSizeBytes),
}));
}
/**
* 获取文档详情
*/
export async function getDocumentById(userId: string, documentId: string) {
const document = await prisma.document.findFirst({
where: {
id: documentId,
userId, // 确保只能访问自己的文档
},
include: {
knowledgeBase: true,
},
});
if (!document) {
throw new Error('Document not found or access denied');
}
// 转换BigInt为Number
return {
...document,
fileSizeBytes: Number(document.fileSizeBytes),
knowledgeBase: {
...document.knowledgeBase,
totalSizeBytes: Number(document.knowledgeBase.totalSizeBytes),
},
};
}
/**
* 删除文档
*/
export async function deleteDocument(userId: string, documentId: string) {
// 1. 查询文档信息
const document = await prisma.document.findFirst({
where: {
id: documentId,
userId,
},
include: {
knowledgeBase: true,
},
});
if (!document) {
throw new Error('Document not found or access denied');
}
// 2. 删除Dify中的文档
if (document.difyDocumentId) {
try {
await difyClient.deleteDocument(
document.knowledgeBase.difyDatasetId,
document.difyDocumentId
);
} catch (error) {
console.error('Failed to delete Dify document:', error);
// 继续删除本地记录
}
}
// 3. 删除数据库记录
await prisma.document.delete({
where: { id: documentId },
});
// 4. 更新知识库统计
await updateKnowledgeBaseStats(document.kbId);
}
/**
* 重新处理文档
*/
export async function reprocessDocument(userId: string, documentId: string) {
// 1. 查询文档信息
const document = await prisma.document.findFirst({
where: {
id: documentId,
userId,
},
include: {
knowledgeBase: true,
},
});
if (!document) {
throw new Error('Document not found or access denied');
}
// 2. 触发Dify重新索引
if (document.difyDocumentId) {
try {
await difyClient.updateDocument(
document.knowledgeBase.difyDatasetId,
document.difyDocumentId
);
// 3. 更新状态为processing
await prisma.document.update({
where: { id: documentId },
data: {
status: 'parsing',
progress: 0,
errorMessage: null,
},
});
// 4. 启动轮询
pollDocumentStatus(
userId,
document.kbId,
documentId,
document.difyDocumentId
).catch(error => {
console.error('Failed to poll document status:', error);
});
} catch (error) {
throw new Error('Failed to reprocess document');
}
}
}
/**
* 更新知识库统计信息
*/
async function updateKnowledgeBaseStats(kbId: string) {
const documents = await prisma.document.findMany({
where: { kbId },
});
const totalSizeBytes = documents.reduce((sum, d) => sum + Number(d.fileSizeBytes), 0);
const fileCount = documents.length;
await prisma.knowledgeBase.update({
where: { id: kbId },
data: {
fileCount,
totalSizeBytes: BigInt(totalSizeBytes),
},
});
}

View File

@@ -0,0 +1,364 @@
import { prisma } from '../../config/database.js';
import { difyClient } from '../../common/rag/DifyClient.js';
import { calculateDocumentTokens, selectDocumentsForFullText, TOKEN_LIMITS } from './tokenService.js';
/**
* 知识库服务
*/
/**
* 创建知识库
*/
export async function createKnowledgeBase(
userId: string,
name: string,
description?: string
) {
// 1. 检查用户知识库配额
const user = await prisma.user.findUnique({
where: { id: userId },
select: { kbQuota: true, kbUsed: true }
});
if (!user) {
throw new Error('User not found');
}
if (user.kbUsed >= user.kbQuota) {
throw new Error(`Knowledge base quota exceeded. Maximum: ${user.kbQuota}`);
}
// 2. 在Dify中创建Dataset
const difyDataset = await difyClient.createDataset({
name: `${userId}_${name}_${Date.now()}`,
description: description || `Knowledge base for user ${userId}`,
indexing_technique: 'high_quality',
});
// 3. 在数据库中创建记录
const knowledgeBase = await prisma.knowledgeBase.create({
data: {
userId,
name,
description,
difyDatasetId: difyDataset.id,
},
});
// 4. 更新用户的知识库使用计数
await prisma.user.update({
where: { id: userId },
data: {
kbUsed: { increment: 1 },
},
});
// 5. 转换BigInt为Number
return {
...knowledgeBase,
totalSizeBytes: Number(knowledgeBase.totalSizeBytes),
};
}
/**
* 获取用户的知识库列表
*/
export async function getKnowledgeBases(userId: string) {
const knowledgeBases = await prisma.knowledgeBase.findMany({
where: { userId },
orderBy: { createdAt: 'desc' },
include: {
_count: {
select: { documents: true },
},
},
});
// 转换BigInt为Number
return knowledgeBases.map(kb => ({
...kb,
totalSizeBytes: Number(kb.totalSizeBytes),
}));
}
/**
* 获取知识库详情
*/
export async function getKnowledgeBaseById(userId: string, kbId: string) {
const knowledgeBase = await prisma.knowledgeBase.findFirst({
where: {
id: kbId,
userId, // 确保只能访问自己的知识库
},
include: {
documents: {
orderBy: { uploadedAt: 'desc' },
},
_count: {
select: { documents: true },
},
},
});
if (!knowledgeBase) {
throw new Error('Knowledge base not found or access denied');
}
// 转换BigInt为Number
const result = {
...knowledgeBase,
totalSizeBytes: Number(knowledgeBase.totalSizeBytes),
documents: knowledgeBase.documents.map(doc => ({
...doc,
fileSizeBytes: Number(doc.fileSizeBytes),
})),
};
return result;
}
/**
* 更新知识库
*/
export async function updateKnowledgeBase(
userId: string,
kbId: string,
data: { name?: string; description?: string }
) {
// 1. 验证权限
const existingKb = await prisma.knowledgeBase.findFirst({
where: {
id: kbId,
userId,
},
});
if (!existingKb) {
throw new Error('Knowledge base not found or access denied');
}
// 2. 更新数据库
const knowledgeBase = await prisma.knowledgeBase.update({
where: { id: kbId },
data,
});
// 3. 转换BigInt为Number
return {
...knowledgeBase,
totalSizeBytes: Number(knowledgeBase.totalSizeBytes),
};
}
/**
* 删除知识库
*/
export async function deleteKnowledgeBase(userId: string, kbId: string) {
// 1. 验证权限
const knowledgeBase = await prisma.knowledgeBase.findFirst({
where: {
id: kbId,
userId,
},
});
if (!knowledgeBase) {
throw new Error('Knowledge base not found or access denied');
}
// 2. 删除Dify中的Dataset
try {
await difyClient.deleteDataset(knowledgeBase.difyDatasetId);
} catch (error) {
console.error('Failed to delete Dify dataset:', error);
// 继续删除本地记录即使Dify删除失败
}
// 3. 删除数据库记录会级联删除documents
await prisma.knowledgeBase.delete({
where: { id: kbId },
});
// 4. 更新用户的知识库使用计数
await prisma.user.update({
where: { id: userId },
data: {
kbUsed: { decrement: 1 },
},
});
}
/**
* 检索知识库
*/
export async function searchKnowledgeBase(
userId: string,
kbId: string,
query: string,
topK: number = 15 // Phase 1优化默认从3增加到15
) {
console.log('🔍 [searchKnowledgeBase] 开始检索', { kbId, query, topK });
// 1. 验证权限
const knowledgeBase = await prisma.knowledgeBase.findFirst({
where: {
id: kbId,
userId,
},
});
if (!knowledgeBase) {
console.error('❌ [searchKnowledgeBase] 知识库不存在', { kbId, userId });
throw new Error('Knowledge base not found or access denied');
}
console.log('📚 [searchKnowledgeBase] 找到知识库', {
id: knowledgeBase.id,
name: knowledgeBase.name,
difyDatasetId: knowledgeBase.difyDatasetId
});
// 2. 调用Dify检索API
console.log('🌐 [searchKnowledgeBase] 调用Dify检索API', {
difyDatasetId: knowledgeBase.difyDatasetId,
query,
topK
});
const results = await difyClient.retrieveKnowledge(
knowledgeBase.difyDatasetId,
query,
{
retrieval_model: {
search_method: 'semantic_search',
top_k: topK,
},
}
);
console.log('✅ [searchKnowledgeBase] Dify返回结果', {
recordCount: results.records?.length || 0,
hasRecords: results.records && results.records.length > 0
});
if (results.records && results.records.length > 0) {
console.log('📄 [searchKnowledgeBase] 检索到的记录:',
results.records.map((r: any) => ({
score: r.score,
contentPreview: r.segment?.content?.substring(0, 100)
}))
);
} else {
console.warn('⚠️ [searchKnowledgeBase] 没有检索到任何记录');
}
return results;
}
/**
* 获取知识库统计信息
*/
export async function getKnowledgeBaseStats(userId: string, kbId: string) {
// 1. 验证权限
const knowledgeBase = await prisma.knowledgeBase.findFirst({
where: {
id: kbId,
userId,
},
include: {
documents: true,
},
});
if (!knowledgeBase) {
throw new Error('Knowledge base not found or access denied');
}
// 2. 统计信息
const stats = {
totalDocuments: knowledgeBase.documents.length,
completedDocuments: knowledgeBase.documents.filter(d => d.status === 'completed').length,
processingDocuments: knowledgeBase.documents.filter(d =>
['uploading', 'parsing', 'indexing'].includes(d.status)
).length,
errorDocuments: knowledgeBase.documents.filter(d => d.status === 'error').length,
totalSizeBytes: knowledgeBase.totalSizeBytes,
totalTokens: knowledgeBase.documents.reduce((sum, d) => sum + (d.tokensCount || 0), 0),
};
return stats;
}
/**
* 获取知识库文档选择(用于全文阅读模式)
* Phase 2新增根据Token限制选择文档
*/
export async function getDocumentSelection(
userId: string,
kbId: string,
maxFiles?: number,
maxTokens?: number
) {
// 1. 验证权限
const knowledgeBase = await prisma.knowledgeBase.findFirst({
where: { id: kbId, userId },
include: {
documents: {
where: {
status: 'completed', // 只选择已完成的文档
},
select: {
id: true,
filename: true,
extractedText: true,
charCount: true,
extractionMethod: true,
tokensCount: true,
fileSizeBytes: true,
},
orderBy: { uploadedAt: 'desc' },
},
},
});
if (!knowledgeBase) {
throw new Error('Knowledge base not found or access denied');
}
// 2. 计算每个文档的Token数
const documentTokens = calculateDocumentTokens(knowledgeBase.documents);
// 3. 选择文档根据Token限制
const selection = selectDocumentsForFullText(
documentTokens,
maxFiles || TOKEN_LIMITS.MAX_FILES,
maxTokens || TOKEN_LIMITS.MAX_TOTAL_TOKENS
);
// 4. 返回结果
return {
knowledgeBaseId: kbId,
knowledgeBaseName: knowledgeBase.name,
limits: {
maxFiles: maxFiles || TOKEN_LIMITS.MAX_FILES,
maxTokens: maxTokens || TOKEN_LIMITS.MAX_TOTAL_TOKENS,
},
selection: {
selectedCount: selection.totalFiles,
selectedTokens: selection.totalTokens,
excludedCount: selection.excludedDocuments.length,
availableTokens: selection.availableTokens,
reason: selection.reason,
},
selectedDocuments: selection.selectedDocuments.map(doc => ({
...doc,
// 查找原始文档信息
...knowledgeBase.documents.find(d => d.id === doc.documentId),
})),
excludedDocuments: selection.excludedDocuments.map(doc => ({
...doc,
// 查找原始文档信息
...knowledgeBase.documents.find(d => d.id === doc.documentId),
})),
};
}

View File

@@ -0,0 +1,102 @@
import { prisma } from '../../config/database.js';
export interface CreateProjectDTO {
name: string;
background: string;
researchType: 'observational' | 'interventional';
userId: string;
}
export interface UpdateProjectDTO {
name?: string;
background?: string;
researchType?: 'observational' | 'interventional';
}
class ProjectService {
// 获取用户的所有项目
async getProjectsByUserId(userId: string) {
return prisma.project.findMany({
where: {
userId,
deletedAt: null,
},
orderBy: {
createdAt: 'desc',
},
});
}
// 根据ID获取项目
async getProjectById(projectId: string, userId: string) {
return prisma.project.findFirst({
where: {
id: projectId,
userId,
deletedAt: null,
},
});
}
// 创建项目
async createProject(data: CreateProjectDTO) {
return prisma.project.create({
data: {
name: data.name,
background: data.background,
researchType: data.researchType,
userId: data.userId,
},
});
}
// 更新项目
async updateProject(projectId: string, userId: string, data: UpdateProjectDTO) {
// 先检查项目是否存在且属于该用户
const project = await this.getProjectById(projectId, userId);
if (!project) {
return null;
}
return prisma.project.update({
where: {
id: projectId,
},
data: {
...data,
updatedAt: new Date(),
},
});
}
// 软删除项目
async deleteProject(projectId: string, userId: string) {
// 先检查项目是否存在且属于该用户
const project = await this.getProjectById(projectId, userId);
if (!project) {
return null;
}
return prisma.project.update({
where: {
id: projectId,
},
data: {
deletedAt: new Date(),
},
});
}
// 统计用户的项目数量
async countUserProjects(userId: string) {
return prisma.project.count({
where: {
userId,
deletedAt: null,
},
});
}
}
export const projectService = new ProjectService();

View File

@@ -0,0 +1,452 @@
import { prisma } from '../../config/database.js';
import { extractionClient } from '../../common/document/ExtractionClient.js';
import { LLMFactory } from '../../common/llm/adapters/LLMFactory.js';
import { ModelType } from '../../common/llm/adapters/types.js';
import fs from 'fs/promises';
import path from 'path';
import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
/**
* 稿件审查服务
*/
// ==================== 类型定义 ====================
export interface EditorialItem {
criterion: string;
status: 'pass' | 'warning' | 'fail';
score: number;
issues: string[];
suggestions: string[];
}
export interface EditorialReview {
overall_score: number;
summary: string;
items: EditorialItem[];
}
export interface MethodologyIssue {
type: string;
severity: 'major' | 'minor';
description: string;
location: string;
suggestion: string;
}
export interface MethodologyPart {
part: string;
score: number;
issues: MethodologyIssue[];
}
export interface MethodologyReview {
overall_score: number;
summary: string;
parts: MethodologyPart[];
}
// ==================== 主要功能函数 ====================
/**
* 审查稿件(主入口函数)
* @param file 文件Buffer
* @param filename 文件名
* @param userId 用户ID
* @param modelType 模型类型
* @returns 审查任务
*/
export async function reviewManuscript(
file: Buffer,
filename: string,
userId: string,
modelType: ModelType = 'deepseek-v3'
) {
const startTime = Date.now();
// 1. 创建任务记录
const task = await prisma.reviewTask.create({
data: {
userId,
fileName: filename,
fileSize: file.length,
extractedText: '', // 初始为空
status: 'pending',
modelUsed: modelType,
startedAt: new Date(),
},
});
try {
// 2. 提取文档文本(异步执行,不阻塞响应)
processReviewTask(task.id, file, filename, userId, modelType, startTime).catch(error => {
console.error(`[ReviewService] Task ${task.id} failed:`, error);
});
return task;
} catch (error) {
// 如果任务创建失败,更新状态
await prisma.reviewTask.update({
where: { id: task.id },
data: {
status: 'failed',
errorMessage: error instanceof Error ? error.message : 'Unknown error',
},
});
throw error;
}
}
/**
* 处理审查任务(后台异步执行)
*/
async function processReviewTask(
taskId: string,
file: Buffer,
filename: string,
userId: string,
modelType: ModelType,
startTime: number
) {
try {
// 1. 更新状态为extracting
await prisma.reviewTask.update({
where: { id: taskId },
data: { status: 'extracting' },
});
// 2. 提取文档文本
console.log(`[ReviewService] 开始提取文档: ${filename}`);
const extractionResult = await extractionClient.extractDocument(file, filename);
if (!extractionResult.success || !extractionResult.text) {
throw new Error('文档提取失败或内容为空');
}
const extractedText = extractionResult.text;
const wordCount = extractionResult.metadata?.char_count || extractedText.length;
console.log(`[ReviewService] 提取成功: ${wordCount} 字符`);
// 更新提取的文本
await prisma.reviewTask.update({
where: { id: taskId },
data: {
extractedText,
wordCount,
status: 'reviewing_editorial',
},
});
// 3. 执行稿约规范性评估
console.log(`[ReviewService] 开始稿约规范性评估...`);
const editorialReview = await reviewEditorialStandards(extractedText, modelType);
await prisma.reviewTask.update({
where: { id: taskId },
data: {
editorialReview: editorialReview as any,
status: 'reviewing_methodology',
},
});
// 4. 执行方法学评估
console.log(`[ReviewService] 开始方法学评估...`);
const methodologyReview = await reviewMethodology(extractedText, modelType);
// 5. 计算总体评分加权平均稿约40% + 方法学60%
const overallScore = editorialReview.overall_score * 0.4 + methodologyReview.overall_score * 0.6;
// 6. 完成任务
const endTime = Date.now();
const durationSeconds = Math.floor((endTime - startTime) / 1000);
await prisma.reviewTask.update({
where: { id: taskId },
data: {
methodologyReview: methodologyReview as any,
overallScore,
status: 'completed',
completedAt: new Date(),
durationSeconds,
},
});
console.log(`[ReviewService] 任务完成: ${taskId}, 总分: ${overallScore.toFixed(1)}, 耗时: ${durationSeconds}s`);
} catch (error) {
console.error(`[ReviewService] 任务处理失败:`, error);
// 更新任务状态为failed
await prisma.reviewTask.update({
where: { id: taskId },
data: {
status: 'failed',
errorMessage: error instanceof Error ? error.message : 'Unknown error',
},
});
}
}
/**
* 稿约规范性评估
* @param text 稿件文本
* @param modelType 模型类型
* @returns 评估结果
*/
export async function reviewEditorialStandards(
text: string,
modelType: ModelType = 'deepseek-v3'
): Promise<EditorialReview> {
try {
// 1. 读取系统Prompt
const promptPath = path.join(__dirname, '../../../prompts/review_editorial_system.txt');
const systemPrompt = await fs.readFile(promptPath, 'utf-8');
// 2. 构建消息
const messages = [
{ role: 'system' as const, content: systemPrompt },
{ role: 'user' as const, content: `请对以下稿件进行稿约规范性评估:\n\n${text}` },
];
// 3. 调用LLM
console.log(`[ReviewService] 开始调用 ${modelType} 进行稿约规范性评估...`);
const llmAdapter = LLMFactory.getAdapter(modelType);
const response = await llmAdapter.chat(messages, {
temperature: 0.3, // 较低温度以获得更稳定的评估
maxTokens: 8000, // 增加token限制确保完整输出
});
console.log(`[ReviewService] ${modelType} 稿约规范性评估完成,响应长度: ${response.content.length}`);
// 4. 解析JSON响应
const result = parseJSONFromLLMResponse<EditorialReview>(response.content);
// 5. 验证响应格式
if (!result || typeof result.overall_score !== 'number' || !Array.isArray(result.items)) {
throw new Error('LLM返回的数据格式不正确');
}
return result;
} catch (error) {
console.error('[ReviewService] 稿约规范性评估失败:', error);
if (error instanceof Error) {
console.error('[ReviewService] 错误详情:', {
message: error.message,
stack: error.stack,
});
}
throw new Error(`稿约规范性评估失败: ${error instanceof Error ? error.message : 'Unknown error'}`);
}
}
/**
* 方法学评估
* @param text 稿件文本
* @param modelType 模型类型
* @returns 评估结果
*/
export async function reviewMethodology(
text: string,
modelType: ModelType = 'deepseek-v3'
): Promise<MethodologyReview> {
try {
// 1. 读取系统Prompt
const promptPath = path.join(__dirname, '../../../prompts/review_methodology_system.txt');
const systemPrompt = await fs.readFile(promptPath, 'utf-8');
// 2. 构建消息
const messages = [
{ role: 'system' as const, content: systemPrompt },
{ role: 'user' as const, content: `请对以下稿件进行方法学评估:\n\n${text}` },
];
// 3. 调用LLM
console.log(`[ReviewService] 开始调用 ${modelType} 进行方法学评估...`);
const llmAdapter = LLMFactory.getAdapter(modelType);
const response = await llmAdapter.chat(messages, {
temperature: 0.3,
maxTokens: 8000, // 增加token限制确保完整输出
});
console.log(`[ReviewService] ${modelType} 方法学评估完成,响应长度: ${response.content.length}`);
// 4. 解析JSON响应
const result = parseJSONFromLLMResponse<MethodologyReview>(response.content);
// 5. 验证响应格式
if (!result || typeof result.overall_score !== 'number' || !Array.isArray(result.parts)) {
throw new Error('LLM返回的数据格式不正确');
}
return result;
} catch (error) {
console.error('[ReviewService] 方法学评估失败:', error);
if (error instanceof Error) {
console.error('[ReviewService] 错误详情:', {
message: error.message,
stack: error.stack,
});
}
throw new Error(`方法学评估失败: ${error instanceof Error ? error.message : 'Unknown error'}`);
}
}
/**
* 从LLM响应中解析JSON
* 支持多种格式纯JSON、```json代码块、混合文本
*/
function parseJSONFromLLMResponse<T>(content: string): T {
try {
// 1. 尝试直接解析
return JSON.parse(content) as T;
} catch {
// 2. 尝试提取```json代码块
const jsonMatch = content.match(/```json\s*\n?([\s\S]*?)\n?```/);
if (jsonMatch) {
try {
return JSON.parse(jsonMatch[1].trim()) as T;
} catch {
// 继续尝试其他方法
}
}
// 3. 尝试提取{}或[]包裹的内容
const objectMatch = content.match(/(\{[\s\S]*\})/);
if (objectMatch) {
try {
return JSON.parse(objectMatch[1]) as T;
} catch {
// 继续尝试其他方法
}
}
const arrayMatch = content.match(/(\[[\s\S]*\])/);
if (arrayMatch) {
try {
return JSON.parse(arrayMatch[1]) as T;
} catch {
// 失败
}
}
// 4. 所有尝试都失败
throw new Error('无法从LLM响应中解析JSON');
}
}
// ==================== 任务查询与管理 ====================
/**
* 获取任务详情
*/
export async function getReviewTask(userId: string, taskId: string) {
const task = await prisma.reviewTask.findFirst({
where: {
id: taskId,
userId,
},
});
if (!task) {
throw new Error('Task not found or access denied');
}
return task;
}
/**
* 获取用户的所有审查任务
*/
export async function getReviewTasks(userId: string, page: number = 1, limit: number = 20) {
const skip = (page - 1) * limit;
const [tasks, total] = await Promise.all([
prisma.reviewTask.findMany({
where: { userId },
orderBy: { createdAt: 'desc' },
skip,
take: limit,
select: {
id: true,
fileName: true,
fileSize: true,
status: true,
overallScore: true,
modelUsed: true,
createdAt: true,
completedAt: true,
durationSeconds: true,
wordCount: true,
},
}),
prisma.reviewTask.count({
where: { userId },
}),
]);
return {
tasks,
pagination: {
page,
limit,
total,
totalPages: Math.ceil(total / limit),
},
};
}
/**
* 删除审查任务
*/
export async function deleteReviewTask(userId: string, taskId: string) {
const task = await prisma.reviewTask.findFirst({
where: {
id: taskId,
userId,
},
});
if (!task) {
throw new Error('Task not found or access denied');
}
await prisma.reviewTask.delete({
where: { id: taskId },
});
return { success: true };
}
/**
* 获取任务报告(完整的评估结果)
*/
export async function getReviewReport(userId: string, taskId: string) {
const task = await prisma.reviewTask.findFirst({
where: {
id: taskId,
userId,
},
});
if (!task) {
throw new Error('Task not found or access denied');
}
if (task.status !== 'completed') {
throw new Error('Report is not ready yet. Task status: ' + task.status);
}
return {
taskId: task.id,
fileName: task.fileName,
wordCount: task.wordCount,
modelUsed: task.modelUsed,
overallScore: task.overallScore,
editorialReview: task.editorialReview,
methodologyReview: task.methodologyReview,
completedAt: task.completedAt,
durationSeconds: task.durationSeconds,
};
}

View File

@@ -0,0 +1,232 @@
import { encoding_for_model, Tiktoken } from 'tiktoken';
/**
* Token计数服务
* 用于全文阅读模式的Token管理
*/
// Token限制配置
export const TOKEN_LIMITS = {
MAX_FILES: 50, // 最多50个文件
MAX_TOTAL_TOKENS: 980000, // 最多980K tokens为Qwen-Long 1M上下文留20K余量
CONTEXT_RESERVE: 20000, // 预留给系统提示词和用户查询的token
};
// 缓存编码器
let encoderCache: Tiktoken | null = null;
/**
* 获取编码器使用gpt-4作为Qwen的替代
*/
function getEncoder(): Tiktoken {
if (!encoderCache) {
// Qwen使用类似GPT-4的tokenizer
encoderCache = encoding_for_model('gpt-4');
}
return encoderCache;
}
/**
* 计算文本的Token数
*/
export function countTokens(text: string): number {
if (!text || text.trim().length === 0) {
return 0;
}
try {
const encoder = getEncoder();
const tokens = encoder.encode(text);
return tokens.length;
} catch (error) {
console.error('[TokenService] Failed to count tokens:', error);
// 降级粗略估算中文约1.5字符/token英文约4字符/token
const chineseChars = (text.match(/[\u4e00-\u9fff]/g) || []).length;
const totalChars = text.length;
const englishChars = totalChars - chineseChars;
return Math.ceil(chineseChars / 1.5 + englishChars / 4);
}
}
/**
* 批量计算多个文本的Token数
*/
export function countTokensBatch(texts: string[]): number[] {
return texts.map(text => countTokens(text));
}
/**
* 计算文档Token数基于提取的文本
*/
export interface DocumentTokenInfo {
documentId: string;
filename: string;
charCount: number;
estimatedTokens: number;
extractionMethod?: string;
}
/**
* 为文档列表计算Token数
*/
export function calculateDocumentTokens(
documents: Array<{
id: string;
filename: string;
extractedText?: string | null;
charCount?: number | null;
extractionMethod?: string | null;
}>
): DocumentTokenInfo[] {
return documents.map(doc => {
let estimatedTokens = 0;
if (doc.extractedText) {
// 使用提取的文本计算精确token数
estimatedTokens = countTokens(doc.extractedText);
} else if (doc.charCount) {
// 如果没有提取文本,使用字符数估算
// 假设中英文混合平均2.5字符/token
estimatedTokens = Math.ceil(doc.charCount / 2.5);
}
return {
documentId: doc.id,
filename: doc.filename,
charCount: doc.charCount || 0,
estimatedTokens,
extractionMethod: doc.extractionMethod || undefined,
};
});
}
/**
* 选择文档以满足Token限制
* 策略优先选择Token数少的文档直到达到限制
*/
export interface DocumentSelectionResult {
selectedDocuments: DocumentTokenInfo[];
totalTokens: number;
totalFiles: number;
excludedDocuments: DocumentTokenInfo[];
reason: 'all_included' | 'file_limit' | 'token_limit';
availableTokens: number;
}
export function selectDocumentsForFullText(
documents: DocumentTokenInfo[],
maxFiles: number = TOKEN_LIMITS.MAX_FILES,
maxTokens: number = TOKEN_LIMITS.MAX_TOTAL_TOKENS
): DocumentSelectionResult {
// 按Token数升序排序优先选择小文件
const sortedDocs = [...documents].sort(
(a, b) => a.estimatedTokens - b.estimatedTokens
);
const selected: DocumentTokenInfo[] = [];
const excluded: DocumentTokenInfo[] = [];
let totalTokens = 0;
for (const doc of sortedDocs) {
// 检查文件数限制
if (selected.length >= maxFiles) {
excluded.push(doc);
continue;
}
// 检查Token限制
if (totalTokens + doc.estimatedTokens > maxTokens) {
excluded.push(doc);
continue;
}
// 添加到选中列表
selected.push(doc);
totalTokens += doc.estimatedTokens;
}
// 判断限制原因
let reason: 'all_included' | 'file_limit' | 'token_limit' = 'all_included';
if (excluded.length > 0) {
if (selected.length >= maxFiles) {
reason = 'file_limit';
} else {
reason = 'token_limit';
}
}
return {
selectedDocuments: selected,
totalTokens,
totalFiles: selected.length,
excludedDocuments: excluded,
reason,
availableTokens: maxTokens - totalTokens,
};
}
/**
* 估算查询需要的Token数
*/
export function estimateQueryTokens(query: string, systemPrompt?: string): number {
let total = countTokens(query);
if (systemPrompt) {
total += countTokens(systemPrompt);
}
// 为响应预留空间
total += 2000; // 假设响应最多2000 tokens
return total;
}
/**
* 检查是否超过Token限制
*/
export function checkTokenLimit(
documentsTokens: number,
queryTokens: number,
maxTokens: number = TOKEN_LIMITS.MAX_TOTAL_TOKENS
): {
withinLimit: boolean;
totalTokens: number;
maxTokens: number;
remaining: number;
} {
const totalTokens = documentsTokens + queryTokens;
const remaining = maxTokens - totalTokens;
return {
withinLimit: remaining >= 0,
totalTokens,
maxTokens,
remaining,
};
}
/**
* 释放编码器(清理资源)
*/
export function cleanup() {
if (encoderCache) {
encoderCache.free();
encoderCache = null;
}
}
// 进程退出时清理
if (typeof process !== 'undefined') {
process.on('exit', cleanup);
process.on('SIGINT', () => {
cleanup();
process.exit();
});
}

View File

@@ -0,0 +1,152 @@
/**
* Phase 3: 批处理模式 - 临床研究信息提取模板
*
* 提取临床研究的8个核心字段
* 1. 研究目的
* 2. 研究设计
* 3. 研究对象
* 4. 样本量text类型保留原文描述
* 5. 干预组
* 6. 对照组
* 7. 结果及数据
* 8. 牛津评级(提供详细标准)
*/
export interface TemplateField {
key: string;
label: string;
type: 'text' | 'longtext' | 'number';
description?: string;
}
export interface BatchTemplate {
id: string;
name: string;
description: string;
outputFields: TemplateField[];
systemPrompt: string;
userPrompt: string;
}
export const CLINICAL_RESEARCH_TEMPLATE: BatchTemplate = {
id: 'clinical_research',
name: '临床研究信息提取',
description: '提取研究目的、设计、对象、样本量、干预、对照、结果、证据等级',
outputFields: [
{
key: 'research_purpose',
label: '研究目的',
type: 'text',
description: '研究想要解决的问题或验证的假设'
},
{
key: 'research_design',
label: '研究设计',
type: 'text',
description: '研究类型RCT、队列研究等'
},
{
key: 'research_subjects',
label: '研究对象',
type: 'text',
description: '纳入/排除标准、人群特征'
},
{
key: 'sample_size',
label: '样本量',
type: 'text', // ✅ text类型保留原文描述
description: '实际纳入的受试者人数'
},
{
key: 'intervention_group',
label: '干预组',
type: 'text',
description: '实验组的干预措施'
},
{
key: 'control_group',
label: '对照组',
type: 'text',
description: '对照组的情况'
},
{
key: 'results_data',
label: '结果及数据',
type: 'longtext',
description: '主要结局指标的具体数据'
},
{
key: 'oxford_level',
label: '牛津评级',
type: 'text',
description: '证据等级(1a-5)'
},
],
systemPrompt: `你是一个专业的临床研究数据提取助手。
你的任务是从临床研究文献中提取结构化信息。
你的回答必须严格遵循JSON格式不要有任何额外的文字说明。`,
userPrompt: `请仔细阅读这篇临床研究文献,提取以下信息:
1. **研究目的**本研究想要解决什么问题或验证什么假设用1-2句话概括。
2. **研究设计**:研究类型,如随机对照试验(RCT)、队列研究、病例对照研究、横断面研究、系统评价/Meta分析等。
3. **研究对象**:描述纳入标准、排除标准、人群特征(年龄、性别、疾病状态等)。
4. **样本量**:实际纳入的受试者人数,保留原文描述(如"干预组156人对照组152人共308人")。
5. **干预组**:实验组接受的治疗或干预措施,包括药物名称、剂量、给药方式、疗程等。
6. **对照组**:对照组的情况,如安慰剂、标准治疗、空白对照等。
7. **结果及数据**主要结局指标的具体数据、统计结果、P值、置信区间等。包括基线数据对比和终点数据对比。
8. **牛津评级**:根据研究设计判断证据等级,参考以下标准:
- **1a**:系统评价/Meta分析多个RCT的汇总分析
- **1b**:单个随机对照试验(RCT)
- **2a**:设计良好的对照研究(无随机化)
- **2b**:设计良好的准实验研究(队列研究、病例对照研究)
- **3a**:描述性研究(横断面研究、病例系列)
- **3b**:个案报告(单一病例)
- **4**:专家意见、共识声明
- **5**:基础研究(动物实验、体外研究)
请严格按照以下JSON格式输出不要有任何额外说明或前言
{
"research_purpose": "...",
"research_design": "...",
"research_subjects": "...",
"sample_size": "...",
"intervention_group": "...",
"control_group": "...",
"results_data": "...",
"oxford_level": "..."
}`,
};
// 导出所有预设模板
export const PRESET_TEMPLATES: Record<string, BatchTemplate> = {
[CLINICAL_RESEARCH_TEMPLATE.id]: CLINICAL_RESEARCH_TEMPLATE,
};
// 获取模板
export function getTemplate(templateId: string): BatchTemplate | null {
return PRESET_TEMPLATES[templateId] || null;
}
// 获取所有模板列表
export function getAllTemplates(): BatchTemplate[] {
return Object.values(PRESET_TEMPLATES);
}