Core Components: - PDFStorageService with Dify/OSS adapters - LLM12FieldsService with Nougat-first + dual-model + 3-layer JSON parsing - PromptBuilder for dynamic prompt assembly - MedicalLogicValidator with 5 rules + fault tolerance - EvidenceChainValidator for citation integrity - ConflictDetectionService for dual-model comparison Prompt Engineering: - System Prompt (6601 chars, Section-Aware strategy) - User Prompt template (PICOS context injection) - JSON Schema (12 fields constraints) - Cochrane standards (not loaded in MVP) Key Innovations: - 3-layer JSON parsing (JSON.parse + json-repair + code block extraction) - Promise.allSettled for dual-model fault tolerance - safeGetFieldValue for robust field extraction - Mixed CN/EN token calculation Integration Tests: - integration-test.ts (full test) - quick-test.ts (quick test) - cached-result-test.ts (fault tolerance test) Documentation Updates: - Development record (Day 2-3 summary) - Quality assurance strategy (full-text screening) - Development plan (progress update) - Module status (v1.1 update) - Technical debt (10 new items) Test Results: - JSON parsing success rate: 100% - Medical logic validation: 5/5 passed - Dual-model parallel processing: OK - Cost per PDF: CNY 0.10 Files: 238 changed, 14383 insertions(+), 32 deletions(-) Docs: docs/03-涓氬姟妯″潡/ASL-AI鏅鸿兘鏂囩尞/05-寮€鍙戣褰?2025-11-22_Day2-Day3_LLM鏈嶅姟涓庨獙璇佺郴缁熷紑鍙?md
92 lines
2.0 KiB
TypeScript
92 lines
2.0 KiB
TypeScript
import { JobQueue } from './types.js'
|
||
import { MemoryQueue } from './MemoryQueue.js'
|
||
|
||
/**
|
||
* 任务队列工厂类
|
||
*
|
||
* 根据环境变量自动选择队列实现:
|
||
* - QUEUE_TYPE=memory: 使用MemoryQueue(内存队列)
|
||
* - QUEUE_TYPE=database: 使用DatabaseQueue(数据库队列,待实现)
|
||
*
|
||
* 零代码切换:
|
||
* - 本地开发:不配置QUEUE_TYPE,默认使用memory
|
||
* - 云端部署:配置QUEUE_TYPE=database(多实例共享)
|
||
*
|
||
* @example
|
||
* ```typescript
|
||
* import { jobQueue } from '@/common/jobs'
|
||
*
|
||
* // 业务代码不关心是memory还是database
|
||
* const job = await jobQueue.push('asl:screening', { projectId: 123 })
|
||
* ```
|
||
*/
|
||
export class JobFactory {
|
||
private static instance: JobQueue | null = null
|
||
|
||
/**
|
||
* 获取任务队列实例(单例模式)
|
||
*/
|
||
static getInstance(): JobQueue {
|
||
if (!this.instance) {
|
||
this.instance = this.createQueue()
|
||
}
|
||
return this.instance
|
||
}
|
||
|
||
/**
|
||
* 创建任务队列
|
||
*/
|
||
private static createQueue(): JobQueue {
|
||
const queueType = process.env.QUEUE_TYPE || 'memory'
|
||
|
||
switch (queueType) {
|
||
case 'memory':
|
||
return this.createMemoryQueue()
|
||
|
||
case 'database':
|
||
// TODO: 实现DatabaseQueue
|
||
console.warn('[JobFactory] DatabaseQueue not implemented yet, fallback to MemoryQueue')
|
||
return this.createMemoryQueue()
|
||
|
||
default:
|
||
console.warn(`[JobFactory] Unknown QUEUE_TYPE: ${queueType}, fallback to memory`)
|
||
return this.createMemoryQueue()
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 创建内存队列
|
||
*/
|
||
private static createMemoryQueue(): MemoryQueue {
|
||
console.log('[JobFactory] Using MemoryQueue')
|
||
|
||
const queue = new MemoryQueue()
|
||
|
||
// 定期清理已完成的任务(避免内存泄漏)
|
||
if (process.env.NODE_ENV !== 'test') {
|
||
setInterval(() => {
|
||
queue.cleanup()
|
||
}, 60 * 60 * 1000) // 每小时清理一次
|
||
}
|
||
|
||
return queue
|
||
}
|
||
|
||
/**
|
||
* 重置实例(用于测试)
|
||
*/
|
||
static reset(): void {
|
||
this.instance = null
|
||
}
|
||
}
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|