feat(rag): 添加文件相对路径支持功能

- 在FileInfo DTO中新增relativePath字段
- 实现文件相对路径的规范化处理逻辑
- 将文件相对路径存储到元数据中
- 前端添加文件路径解析和显示功能
- 优化路径分隔符统一处理机制
- 更新文件列表展示逻辑以支持路径层级结构
This commit is contained in:
2026-01-30 21:46:03 +08:00
parent a00a6ed3c3
commit ca7ff56610
4 changed files with 58 additions and 17 deletions

View File

@@ -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,
});
});