import { Injectable, NotFoundException, BadRequestException } from '@nestjs/common'; import { KnowledgeSourceRepository } from './knowledge-source.repository'; import { DocumentImportRepository } from '../document-import/document-import.repository'; @Injectable() export class KnowledgeSourceService { constructor( private readonly repository: KnowledgeSourceRepository, private readonly importRepo: DocumentImportRepository, ) {} async addSource(userId: string, knowledgeBaseId: string, dto: { fileId?: string; type?: string; title?: string; originalFilename?: string; mimeType?: string; sizeBytes?: number; originalObjectKey?: string; }) { const source = await this.repository.create(userId, knowledgeBaseId, dto); // 自动创建 import 任务 await this.importRepo.create({ userId, knowledgeBaseId, sourceId: source.id, fileId: dto.fileId, sourceType: dto.type ?? 'file', sourceName: dto.originalFilename ?? dto.title ?? '', status: 'QUEUED', }); return source; } async findByKnowledgeBase(knowledgeBaseId: string, pagination: { page?: number; limit?: number }) { return this.repository.findByKnowledgeBase(knowledgeBaseId, pagination); } async findOne(id: string) { const source = await this.repository.findById(id); if (!source) throw new NotFoundException('资料来源不存在'); return source; } async remove(id: string) { const source = await this.repository.findById(id); if (!source) throw new NotFoundException('资料来源不存在'); return this.repository.softDelete(id); } async updateParseStatus(id: string, parseStatus: string, data?: any) { return this.repository.updateParseStatus(id, parseStatus, data); } async updateIndexStatus(id: string, indexStatus: string, errorCode?: string, errorMessage?: string) { return this.repository.updateIndexStatus(id, indexStatus, errorCode, errorMessage); } }