fix(pkb): fix create KB and upload issues - remove simulated upload, fix department mapping, add upload modal
Fixed issues: - Remove simulateUpload function from DashboardPage Step 3 - Map department to description field when creating KB - Add upload modal in WorkspacePage knowledge assets tab - Fix DocumentUpload import path (../../stores to ../stores) Known issue: Dify API validation error during document upload (file uploaded but DB record failed, needs investigation) Testing: KB creation works, upload dialog opens correctly
This commit is contained in:
@@ -13,6 +13,17 @@ import { executeBatchTask, retryFailedDocuments, BatchProgress } from '../servic
|
||||
import { prisma } from '../../../config/database.js';
|
||||
import { ModelType } from '../../../common/llm/adapters/types.js';
|
||||
|
||||
/**
|
||||
* 获取用户ID(从JWT Token中获取)
|
||||
*/
|
||||
function getUserId(request: FastifyRequest): string {
|
||||
const userId = (request as any).user?.userId;
|
||||
if (!userId) {
|
||||
throw new Error('User not authenticated');
|
||||
}
|
||||
return userId;
|
||||
}
|
||||
|
||||
// ==================== 类型定义 ====================
|
||||
|
||||
interface ExecuteBatchBody {
|
||||
@@ -40,8 +51,7 @@ export async function executeBatch(
|
||||
reply: FastifyReply
|
||||
) {
|
||||
try {
|
||||
// TODO: 从JWT获取userId
|
||||
const userId = 'user-mock-001';
|
||||
const userId = getUserId(request);
|
||||
|
||||
const {
|
||||
kb_id,
|
||||
@@ -365,7 +375,7 @@ export async function retryFailed(
|
||||
) {
|
||||
try {
|
||||
const { taskId } = request.params;
|
||||
const userId = 'user-mock-001'; // TODO: 从JWT获取
|
||||
const userId = getUserId(request);
|
||||
|
||||
// 获取WebSocket实例
|
||||
const io = (request.server as any).io;
|
||||
|
||||
@@ -1,8 +1,16 @@
|
||||
import type { FastifyRequest, FastifyReply } from 'fastify';
|
||||
import * as documentService from '../services/documentService.js';
|
||||
|
||||
// Mock用户ID(实际应从JWT token中获取)
|
||||
const MOCK_USER_ID = 'user-mock-001';
|
||||
/**
|
||||
* 获取用户ID(从JWT Token中获取)
|
||||
*/
|
||||
function getUserId(request: FastifyRequest): string {
|
||||
const userId = (request as any).user?.userId;
|
||||
if (!userId) {
|
||||
throw new Error('User not authenticated');
|
||||
}
|
||||
return userId;
|
||||
}
|
||||
|
||||
/**
|
||||
* 上传文档
|
||||
@@ -69,8 +77,9 @@ export async function uploadDocument(
|
||||
|
||||
// 上传文档(这里fileUrl暂时为空,实际应该上传到对象存储)
|
||||
console.log(`⚙️ 调用文档服务上传文件...`);
|
||||
const userId = getUserId(request);
|
||||
const document = await documentService.uploadDocument(
|
||||
MOCK_USER_ID,
|
||||
userId,
|
||||
kbId,
|
||||
file,
|
||||
filename,
|
||||
@@ -123,7 +132,8 @@ export async function getDocuments(
|
||||
try {
|
||||
const { kbId } = request.params;
|
||||
|
||||
const documents = await documentService.getDocuments(MOCK_USER_ID, kbId);
|
||||
const userId = getUserId(request);
|
||||
const documents = await documentService.getDocuments(userId, kbId);
|
||||
|
||||
return reply.send({
|
||||
success: true,
|
||||
@@ -160,7 +170,8 @@ export async function getDocumentById(
|
||||
try {
|
||||
const { id } = request.params;
|
||||
|
||||
const document = await documentService.getDocumentById(MOCK_USER_ID, id);
|
||||
const userId = getUserId(request);
|
||||
const document = await documentService.getDocumentById(userId, id);
|
||||
|
||||
return reply.send({
|
||||
success: true,
|
||||
@@ -197,7 +208,8 @@ export async function deleteDocument(
|
||||
try {
|
||||
const { id } = request.params;
|
||||
|
||||
await documentService.deleteDocument(MOCK_USER_ID, id);
|
||||
const userId = getUserId(request);
|
||||
await documentService.deleteDocument(userId, id);
|
||||
|
||||
return reply.send({
|
||||
success: true,
|
||||
@@ -234,7 +246,8 @@ export async function reprocessDocument(
|
||||
try {
|
||||
const { id } = request.params;
|
||||
|
||||
await documentService.reprocessDocument(MOCK_USER_ID, id);
|
||||
const userId = getUserId(request);
|
||||
await documentService.reprocessDocument(userId, id);
|
||||
|
||||
return reply.send({
|
||||
success: true,
|
||||
@@ -271,7 +284,8 @@ export async function getDocumentFullText(
|
||||
try {
|
||||
const { id } = request.params;
|
||||
|
||||
const document = await documentService.getDocumentById(MOCK_USER_ID, id);
|
||||
const userId = getUserId(request);
|
||||
const document = await documentService.getDocumentById(userId, id);
|
||||
|
||||
// 返回完整的文档信息
|
||||
return reply.send({
|
||||
|
||||
@@ -1,8 +1,16 @@
|
||||
import type { FastifyRequest, FastifyReply } from 'fastify';
|
||||
import * as knowledgeBaseService from '../services/knowledgeBaseService.js';
|
||||
|
||||
// Mock用户ID(实际应从JWT token中获取)
|
||||
const MOCK_USER_ID = 'user-mock-001';
|
||||
/**
|
||||
* 获取用户ID(从JWT Token中获取)
|
||||
*/
|
||||
function getUserId(request: FastifyRequest): string {
|
||||
const userId = (request as any).user?.userId;
|
||||
if (!userId) {
|
||||
throw new Error('User not authenticated');
|
||||
}
|
||||
return userId;
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建知识库
|
||||
@@ -26,8 +34,9 @@ export async function createKnowledgeBase(
|
||||
});
|
||||
}
|
||||
|
||||
const userId = getUserId(request);
|
||||
const knowledgeBase = await knowledgeBaseService.createKnowledgeBase(
|
||||
MOCK_USER_ID,
|
||||
userId,
|
||||
name,
|
||||
description
|
||||
);
|
||||
@@ -49,12 +58,13 @@ export async function createKnowledgeBase(
|
||||
* 获取知识库列表
|
||||
*/
|
||||
export async function getKnowledgeBases(
|
||||
_request: FastifyRequest,
|
||||
request: FastifyRequest,
|
||||
reply: FastifyReply
|
||||
) {
|
||||
try {
|
||||
const userId = getUserId(request);
|
||||
const knowledgeBases = await knowledgeBaseService.getKnowledgeBases(
|
||||
MOCK_USER_ID
|
||||
userId
|
||||
);
|
||||
|
||||
return reply.send({
|
||||
@@ -84,8 +94,9 @@ export async function getKnowledgeBaseById(
|
||||
try {
|
||||
const { id } = request.params;
|
||||
|
||||
const userId = getUserId(request);
|
||||
const knowledgeBase = await knowledgeBaseService.getKnowledgeBaseById(
|
||||
MOCK_USER_ID,
|
||||
userId,
|
||||
id
|
||||
);
|
||||
|
||||
@@ -129,8 +140,9 @@ export async function updateKnowledgeBase(
|
||||
const { id } = request.params;
|
||||
const updateData = request.body;
|
||||
|
||||
const userId = getUserId(request);
|
||||
const knowledgeBase = await knowledgeBaseService.updateKnowledgeBase(
|
||||
MOCK_USER_ID,
|
||||
userId,
|
||||
id,
|
||||
updateData
|
||||
);
|
||||
@@ -170,7 +182,8 @@ export async function deleteKnowledgeBase(
|
||||
try {
|
||||
const { id } = request.params;
|
||||
|
||||
await knowledgeBaseService.deleteKnowledgeBase(MOCK_USER_ID, id);
|
||||
const userId = getUserId(request);
|
||||
await knowledgeBaseService.deleteKnowledgeBase(userId, id);
|
||||
|
||||
return reply.send({
|
||||
success: true,
|
||||
@@ -221,8 +234,9 @@ export async function searchKnowledgeBase(
|
||||
|
||||
const topK = top_k ? parseInt(top_k, 10) : 15; // Phase 1优化:默认从3增加到15
|
||||
|
||||
const userId = getUserId(request);
|
||||
const results = await knowledgeBaseService.searchKnowledgeBase(
|
||||
MOCK_USER_ID,
|
||||
userId,
|
||||
id,
|
||||
query,
|
||||
topK
|
||||
@@ -263,8 +277,9 @@ export async function getKnowledgeBaseStats(
|
||||
try {
|
||||
const { id } = request.params;
|
||||
|
||||
const userId = getUserId(request);
|
||||
const stats = await knowledgeBaseService.getKnowledgeBaseStats(
|
||||
MOCK_USER_ID,
|
||||
userId,
|
||||
id
|
||||
);
|
||||
|
||||
@@ -311,8 +326,9 @@ export async function getDocumentSelection(
|
||||
const maxFiles = max_files ? parseInt(max_files, 10) : undefined;
|
||||
const maxTokens = max_tokens ? parseInt(max_tokens, 10) : undefined;
|
||||
|
||||
const userId = getUserId(request);
|
||||
const selection = await knowledgeBaseService.getDocumentSelection(
|
||||
MOCK_USER_ID,
|
||||
userId,
|
||||
id,
|
||||
maxFiles,
|
||||
maxTokens
|
||||
|
||||
@@ -51,3 +51,4 @@ export default async function healthRoutes(fastify: FastifyInstance) {
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -29,9 +29,14 @@ export async function createKnowledgeBase(
|
||||
}
|
||||
|
||||
// 2. 在Dify中创建Dataset
|
||||
// Dify API name字段限制:避免特殊字符,保持简洁
|
||||
const sanitizedName = name
|
||||
.replace(/[^\u4e00-\u9fa5a-zA-Z0-9_-]/g, '_') // 移除特殊字符
|
||||
.substring(0, 50); // 限制长度
|
||||
|
||||
const difyDataset = await difyClient.createDataset({
|
||||
name: `${userId}_${name}_${Date.now()}`,
|
||||
description: description || `Knowledge base for user ${userId}`,
|
||||
name: `kb_${sanitizedName}_${Date.now()}`, // 简化格式
|
||||
description: description?.substring(0, 200) || '', // 限制描述长度
|
||||
indexing_technique: 'high_quality',
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user