feat(admin): Implement System Knowledge Base management module

Features:

- Backend: SystemKbService with full CRUD (knowledge bases + documents)

- Backend: 8 RESTful API endpoints (list/detail/create/update/delete/upload/download)

- Backend: OSS storage integration (system/knowledge-bases/{kbId}/{docId})

- Backend: RAG engine integration (document parsing, chunking, vectorization)

- Frontend: SystemKbListPage with card-based layout

- Frontend: SystemKbDetailPage with document management table

- Frontend: Master-Detail UX pattern for better user experience

- Document upload (single/batch), download (preserving original filename), delete

Technical:

- Database migration for system_knowledge_bases and system_kb_documents tables

- OSSAdapter.getSignedUrl with Content-Disposition for original filename

- Reuse RAG engine from common/rag for document processing

Tested: Local environment verified, all features working
This commit is contained in:
2026-01-28 21:57:44 +08:00
parent 3a4aa9123c
commit 0d9e6b9922
28 changed files with 2827 additions and 247 deletions

View File

@@ -0,0 +1,168 @@
# ===========================================
# System Knowledge Base - Document Upload Test
# ===========================================
# 测试文档上传功能,包含 OSS 存储和 RAG 向量化
$baseUrl = "http://localhost:3001"
$phone = "13800000001" # SUPER_ADMIN
$password = "123456"
$testFile = "D:\MyCursor\AIclinicalresearch\docs\06-测试文档\近红外光谱NIRS队列研究举例.pdf"
Write-Host "=============================================" -ForegroundColor Cyan
Write-Host "System KB - Document Upload Test" -ForegroundColor Cyan
Write-Host "=============================================" -ForegroundColor Cyan
# 检查测试文件是否存在
if (-not (Test-Path $testFile)) {
Write-Host "[ERROR] Test file not found: $testFile" -ForegroundColor Red
exit 1
}
$fileInfo = Get-Item $testFile
Write-Host "Test file: $($fileInfo.Name)" -ForegroundColor Gray
Write-Host "File size: $([math]::Round($fileInfo.Length / 1024, 2)) KB" -ForegroundColor Gray
# 1. Login
Write-Host "`n[1/6] Login..." -ForegroundColor Yellow
$loginBody = @{
phone = $phone
password = $password
} | ConvertTo-Json
try {
$loginResponse = Invoke-RestMethod -Uri "$baseUrl/api/v1/auth/login/password" `
-Method POST `
-Body $loginBody `
-ContentType "application/json"
$token = $loginResponse.data.tokens.accessToken
Write-Host " [OK] Login success!" -ForegroundColor Green
} catch {
Write-Host " [FAIL] Login failed: $($_.Exception.Message)" -ForegroundColor Red
exit 1
}
$headers = @{
"Authorization" = "Bearer $token"
}
# 2. Create Knowledge Base
Write-Host "`n[2/6] Create Knowledge Base..." -ForegroundColor Yellow
$testCode = "TEST_UPLOAD_$(Get-Date -Format 'yyyyMMddHHmmss')"
$createBody = @{
code = $testCode
name = "文档上传测试知识库"
description = "用于测试文档上传和 RAG 向量化功能"
category = "test"
} | ConvertTo-Json -Depth 10
try {
$createResponse = Invoke-RestMethod -Uri "$baseUrl/api/v1/admin/system-kb" `
-Method POST `
-Body $createBody `
-ContentType "application/json; charset=utf-8" `
-Headers $headers
$kbId = $createResponse.data.id
Write-Host " [OK] Created! ID: $kbId" -ForegroundColor Green
Write-Host " Code: $($createResponse.data.code)" -ForegroundColor Gray
} catch {
Write-Host " [FAIL]: $($_.Exception.Message)" -ForegroundColor Red
exit 1
}
# 3. Upload Document
Write-Host "`n[3/6] Upload Document (may take a while for RAG processing)..." -ForegroundColor Yellow
try {
# 使用 curl 上传文件PowerShell 的 Invoke-RestMethod 对 multipart 支持不好)
$uploadResult = curl.exe -s -X POST "$baseUrl/api/v1/admin/system-kb/$kbId/documents" `
-H "Authorization: Bearer $token" `
-F "file=@$testFile" | ConvertFrom-Json
if ($uploadResult.success) {
Write-Host " [OK] Upload success!" -ForegroundColor Green
Write-Host " ----------------------------" -ForegroundColor Gray
Write-Host " Doc ID: $($uploadResult.data.docId)" -ForegroundColor White
Write-Host " Filename: $($uploadResult.data.filename)" -ForegroundColor White
Write-Host " Chunks: $($uploadResult.data.chunkCount)" -ForegroundColor White
Write-Host " Tokens: $($uploadResult.data.tokenCount)" -ForegroundColor White
Write-Host " Duration: $($uploadResult.data.duration) ms" -ForegroundColor White
$docId = $uploadResult.data.docId
} else {
Write-Host " [FAIL]: $($uploadResult.error)" -ForegroundColor Red
$docId = $null
}
} catch {
Write-Host " [FAIL]: $($_.Exception.Message)" -ForegroundColor Red
$docId = $null
}
# 4. List Documents
Write-Host "`n[4/6] List Documents..." -ForegroundColor Yellow
try {
$docsResponse = Invoke-RestMethod -Uri "$baseUrl/api/v1/admin/system-kb/$kbId/documents" `
-Method GET `
-Headers $headers
$docCount = $docsResponse.data.Count
Write-Host " [OK] Total: $docCount documents" -ForegroundColor Green
if ($docCount -gt 0) {
Write-Host " ----------------------------" -ForegroundColor Gray
$docsResponse.data | ForEach-Object {
Write-Host " - $($_.filename) | Status: $($_.status) | Tokens: $($_.tokenCount)" -ForegroundColor Gray
Write-Host " Path: $($_.filePath)" -ForegroundColor DarkGray
}
}
} catch {
Write-Host " [FAIL]: $($_.Exception.Message)" -ForegroundColor Red
}
# 5. Get KB Detail (check updated stats)
Write-Host "`n[5/6] Get KB Detail (verify stats updated)..." -ForegroundColor Yellow
try {
$detailResponse = Invoke-RestMethod -Uri "$baseUrl/api/v1/admin/system-kb/$kbId" `
-Method GET `
-Headers $headers
Write-Host " [OK] Success!" -ForegroundColor Green
Write-Host " ----------------------------" -ForegroundColor Gray
Write-Host " Document Count: $($detailResponse.data.documentCount)" -ForegroundColor White
Write-Host " Total Tokens: $($detailResponse.data.totalTokens)" -ForegroundColor White
} catch {
Write-Host " [FAIL]: $($_.Exception.Message)" -ForegroundColor Red
}
# 6. Cleanup - Delete KB
Write-Host "`n[6/6] Cleanup - Delete Knowledge Base..." -ForegroundColor Yellow
try {
$deleteResponse = Invoke-RestMethod -Uri "$baseUrl/api/v1/admin/system-kb/$kbId" `
-Method DELETE `
-Headers $headers
Write-Host " [OK] Deleted!" -ForegroundColor Green
} catch {
Write-Host " [FAIL]: $($_.Exception.Message)" -ForegroundColor Red
}
Write-Host "`n=============================================" -ForegroundColor Cyan
Write-Host "Test Complete!" -ForegroundColor Cyan
Write-Host "=============================================" -ForegroundColor Cyan
if ($docId) {
Write-Host "`n[Summary]" -ForegroundColor Green
Write-Host " - Document uploaded successfully" -ForegroundColor White
Write-Host " - RAG vectorization completed" -ForegroundColor White
Write-Host " - OSS path: system/knowledge-bases/$kbId/$docId.pdf" -ForegroundColor White
} else {
Write-Host "`n[Summary]" -ForegroundColor Yellow
Write-Host " - Document upload may have failed" -ForegroundColor White
Write-Host " - Check backend logs for details" -ForegroundColor White
}