You've already forked DataMate
feat(rag): 添加文件相对路径支持功能
- 在FileInfo DTO中新增relativePath字段 - 实现文件相对路径的规范化处理逻辑 - 将文件相对路径存储到元数据中 - 前端添加文件路径解析和显示功能 - 优化路径分隔符统一处理机制 - 更新文件列表展示逻辑以支持路径层级结构
This commit is contained in:
@@ -57,14 +57,19 @@ type KBFileRow = KBFile & {
|
||||
fileCount?: number;
|
||||
};
|
||||
|
||||
const normalizePath = (value?: string) => (value ?? "").replace(/\\/g, "/");
|
||||
const PATH_SEPARATOR = "/";
|
||||
const RELATIVE_PATH_KEY = "relativePath";
|
||||
const normalizePath = (value?: string) =>
|
||||
(value ?? "").replace(/\\/g, PATH_SEPARATOR);
|
||||
|
||||
const normalizePrefix = (value?: string) => {
|
||||
const trimmed = normalizePath(value).replace(/^\/+/, "").trim();
|
||||
if (!trimmed) {
|
||||
return "";
|
||||
}
|
||||
return trimmed.endsWith("/") ? trimmed : `${trimmed}/`;
|
||||
return trimmed.endsWith(PATH_SEPARATOR)
|
||||
? trimmed
|
||||
: `${trimmed}${PATH_SEPARATOR}`;
|
||||
};
|
||||
|
||||
const splitRelativePath = (fullPath: string, prefix: string) => {
|
||||
@@ -72,7 +77,17 @@ const splitRelativePath = (fullPath: string, prefix: string) => {
|
||||
return [];
|
||||
}
|
||||
const remainder = fullPath.slice(prefix.length);
|
||||
return remainder.split("/").filter(Boolean);
|
||||
return remainder.split(PATH_SEPARATOR).filter(Boolean);
|
||||
};
|
||||
|
||||
const resolveFileRelativePath = (file: KBFile) => {
|
||||
const metadata = file?.metadata as Record<string, unknown> | undefined;
|
||||
const metadataPath =
|
||||
metadata && typeof metadata[RELATIVE_PATH_KEY] === "string"
|
||||
? String(metadata[RELATIVE_PATH_KEY])
|
||||
: "";
|
||||
const rawPath = metadataPath || file.fileName || file.name || "";
|
||||
return normalizePath(rawPath).replace(/^\/+/, "");
|
||||
};
|
||||
|
||||
const KnowledgeBaseDetailPage: React.FC = () => {
|
||||
@@ -196,9 +211,11 @@ const KnowledgeBaseDetailPage: React.FC = () => {
|
||||
return;
|
||||
}
|
||||
const trimmed = currentPrefix.replace(/\/$/, "");
|
||||
const parts = trimmed.split("/").filter(Boolean);
|
||||
const parts = trimmed.split(PATH_SEPARATOR).filter(Boolean);
|
||||
parts.pop();
|
||||
const parentPrefix = parts.length ? `${parts.join("/")}/` : "";
|
||||
const parentPrefix = parts.length
|
||||
? `${parts.join(PATH_SEPARATOR)}${PATH_SEPARATOR}`
|
||||
: "";
|
||||
setFilePrefix(parentPrefix);
|
||||
};
|
||||
|
||||
@@ -210,7 +227,7 @@ const KnowledgeBaseDetailPage: React.FC = () => {
|
||||
const directoryPrefix = normalizePrefix(`${currentPrefix}${directoryName}`);
|
||||
const targetIds = allFiles
|
||||
.filter((file) => {
|
||||
const fullPath = normalizePath(file.fileName || file.name);
|
||||
const fullPath = resolveFileRelativePath(file);
|
||||
return fullPath.startsWith(directoryPrefix);
|
||||
})
|
||||
.map((file) => file.id);
|
||||
@@ -245,7 +262,7 @@ const KnowledgeBaseDetailPage: React.FC = () => {
|
||||
const fileItems: KBFileRow[] = [];
|
||||
|
||||
allFiles.forEach((file) => {
|
||||
const fullPath = normalizePath(file.fileName || file.name);
|
||||
const fullPath = resolveFileRelativePath(file);
|
||||
if (!fullPath) {
|
||||
return;
|
||||
}
|
||||
@@ -278,10 +295,11 @@ const KnowledgeBaseDetailPage: React.FC = () => {
|
||||
return;
|
||||
}
|
||||
|
||||
const displayName = file.fileName || leafName;
|
||||
fileItems.push({
|
||||
...file,
|
||||
name: leafName,
|
||||
displayName: leafName,
|
||||
name: displayName,
|
||||
displayName,
|
||||
fullPath,
|
||||
});
|
||||
});
|
||||
|
||||
@@ -26,19 +26,20 @@ export default function AddDataDialog({ knowledgeBase, onDataAdded }) {
|
||||
|
||||
const [selectedFilesMap, setSelectedFilesMap] = useState({});
|
||||
|
||||
const PATH_SEPARATOR = "/";
|
||||
const normalizePath = (value?: string) =>
|
||||
(value ?? "").replace(/\\/g, "/");
|
||||
(value ?? "").replace(/\\/g, PATH_SEPARATOR);
|
||||
|
||||
const resolveRelativeFileName = (file: DatasetFile) => {
|
||||
const resolveRelativePath = (file: DatasetFile) => {
|
||||
const normalizedName = normalizePath(file.fileName);
|
||||
if (normalizedName.includes("/")) {
|
||||
if (normalizedName.includes(PATH_SEPARATOR)) {
|
||||
return normalizedName.replace(/^\/+/, "");
|
||||
}
|
||||
|
||||
const rawPath = normalizePath(file.path || file.filePath);
|
||||
const datasetId = String(file.datasetId || "");
|
||||
if (rawPath && datasetId) {
|
||||
const marker = `/${datasetId}/`;
|
||||
const marker = `${PATH_SEPARATOR}${datasetId}${PATH_SEPARATOR}`;
|
||||
const index = rawPath.lastIndexOf(marker);
|
||||
if (index >= 0) {
|
||||
const relative = rawPath
|
||||
@@ -50,7 +51,7 @@ export default function AddDataDialog({ knowledgeBase, onDataAdded }) {
|
||||
}
|
||||
}
|
||||
|
||||
const fallbackName = rawPath.split("/").pop();
|
||||
const fallbackName = rawPath.split(PATH_SEPARATOR).pop();
|
||||
return fallbackName || file.fileName;
|
||||
};
|
||||
|
||||
@@ -158,7 +159,8 @@ export default function AddDataDialog({ knowledgeBase, onDataAdded }) {
|
||||
const requestData = {
|
||||
files: Object.values(selectedFilesMap).map((file) => ({
|
||||
id: String(file.id),
|
||||
fileName: resolveRelativeFileName(file as DatasetFile),
|
||||
fileName: (file as DatasetFile).fileName,
|
||||
relativePath: resolveRelativePath(file as DatasetFile),
|
||||
})),
|
||||
processType: newKB.processType,
|
||||
chunkSize: Number(newKB.chunkSize), // 确保是数字类型
|
||||
|
||||
Reference in New Issue
Block a user