From 939adcebd341058b7759d6db36592211ab319c3e Mon Sep 17 00:00:00 2001 From: wangdl Date: Thu, 18 Jun 2026 21:54:58 +0800 Subject: [PATCH] feat(API-AI-081): add KnowledgeItem.sourceId FK to KnowledgeSource - Prisma schema: add sourceId field + @relation to KnowledgeSource - KnowledgeSource: add items[] reverse relation + @index on sourceId - KnowledgeItemsRepository: accept sourceId in create() - ImportCandidateService.accept(): pass sourceId to create() - DocumentImport worker: pass sourceId alongside sourceRef Co-Authored-By: Claude Opus 4.7 --- prisma/schema.prisma | 4 ++++ src/modules/import-candidate/import-candidate.service.ts | 4 +++- src/modules/knowledge-items/knowledge-items.repository.ts | 2 ++ src/workers/document-import.worker.ts | 1 + 4 files changed, 10 insertions(+), 1 deletion(-) diff --git a/prisma/schema.prisma b/prisma/schema.prisma index 31cf239..164c372 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -247,6 +247,7 @@ model KnowledgeItem { content String? @db.LongText summary String? @db.Text learnable Boolean @default(true) + sourceId String? sourceType String? @db.VarChar(32) sourceRef String? @db.VarChar(500) sourceDeleted Boolean @default(false) @@ -262,6 +263,7 @@ model KnowledgeItem { user User @relation(fields: [userId], references: [id]) knowledgeBase KnowledgeBase @relation(fields: [knowledgeBaseId], references: [id]) + source KnowledgeSource? @relation(fields: [sourceId], references: [id]) parent KnowledgeItem? @relation("KnowledgeItemRelations", fields: [parentId], references: [id]) children KnowledgeItem[] @relation("KnowledgeItemRelations") tags KnowledgeItemTag[] @@ -270,6 +272,7 @@ model KnowledgeItem { @@index([knowledgeBaseId]) @@index([parentId]) @@index([itemType]) + @@index([sourceId]) } model KnowledgeItemRelation { @@ -881,6 +884,7 @@ model KnowledgeSource { knowledgeBase KnowledgeBase @relation(fields: [knowledgeBaseId], references: [id]) file UploadedFile? @relation(fields: [fileId], references: [id]) chunks KnowledgeChunk[] + items KnowledgeItem[] imports DocumentImport[] references SourceReference[] candidates ImportCandidate[] diff --git a/src/modules/import-candidate/import-candidate.service.ts b/src/modules/import-candidate/import-candidate.service.ts index 39920bc..4b97729 100644 --- a/src/modules/import-candidate/import-candidate.service.ts +++ b/src/modules/import-candidate/import-candidate.service.ts @@ -38,12 +38,14 @@ export class ImportCandidateService { await this.repository.updateStatus(id, 'ACCEPTED'); // 生成 KnowledgeItem,关联到来源 + const sourceId = (candidate as any).sourceId ?? null; await this.itemsRepo.create(candidate.userId, candidate.knowledgeBaseId, { title: candidate.title, content: (candidate.content as string) ?? '', itemType: 'ai_generated', orderIndex: candidate.orderIndex, - sourceRef: (candidate as any).sourceId ?? null, + sourceId, + sourceRef: sourceId, }); return { status: 'ACCEPTED' }; diff --git a/src/modules/knowledge-items/knowledge-items.repository.ts b/src/modules/knowledge-items/knowledge-items.repository.ts index 4e0eb6f..64bd2f3 100644 --- a/src/modules/knowledge-items/knowledge-items.repository.ts +++ b/src/modules/knowledge-items/knowledge-items.repository.ts @@ -49,6 +49,7 @@ export class KnowledgeItemsRepository { content?: string; parentId?: string; itemType?: string; + sourceId?: string; sourceType?: string; sourceRef?: string; durationSeconds?: number; @@ -65,6 +66,7 @@ export class KnowledgeItemsRepository { parentId: dto.parentId ?? null, itemType: dto.itemType ?? 'lesson', sourceType, + sourceId: dto.sourceId ?? null, sourceRef: dto.sourceRef ?? null, durationSeconds: dto.durationSeconds ?? 0, fileSize: dto.fileSize ?? null, diff --git a/src/workers/document-import.worker.ts b/src/workers/document-import.worker.ts index 941b077..fa0649f 100644 --- a/src/workers/document-import.worker.ts +++ b/src/workers/document-import.worker.ts @@ -69,6 +69,7 @@ export class DocumentImportWorker extends WorkerHost { content: kp.content, itemType: 'lesson', orderIndex: kp.suggestedOrder ?? i + 1, + sourceId: sourceId, sourceRef: sourceId, }); }