72 lines
2.0 KiB
TypeScript
72 lines
2.0 KiB
TypeScript
import { apiFetch, apiFetchForm } from "@/lib/api";
|
|
|
|
/**
|
|
* 知识库(/api/knowledge/*)适配层。
|
|
* 权限:后端 require_knowledge_operator —— 仅管理员或运营角色,前端不做二次判断。
|
|
*/
|
|
|
|
export interface KnowledgeDocument {
|
|
doc_id?: string;
|
|
id?: string;
|
|
title?: string | null;
|
|
filename?: string | null;
|
|
collection_name?: string | null;
|
|
chunk_count?: number | null;
|
|
strategy?: string | null;
|
|
create_time?: string | null;
|
|
[key: string]: unknown;
|
|
}
|
|
|
|
export interface UploadPreview {
|
|
filename?: string;
|
|
strategy?: string;
|
|
chunk_size?: number;
|
|
chunk_overlap?: number;
|
|
chunks?: unknown[];
|
|
preview?: unknown;
|
|
[key: string]: unknown;
|
|
}
|
|
|
|
export const knowledgeApi = {
|
|
list: () => apiFetch<KnowledgeDocument[]>("/knowledge/documents"),
|
|
|
|
get: (docId: string) =>
|
|
apiFetch<KnowledgeDocument>(`/knowledge/documents/${encodeURIComponent(docId)}`),
|
|
|
|
/** 第一步:上传文件拿切分预览,不落库。 */
|
|
preview: (form: FormData) =>
|
|
apiFetchForm<UploadPreview>("/knowledge/documents/preview", form),
|
|
|
|
/** 第二步:确认入库(可复用 preview 返回的 doc_id)。 */
|
|
confirm: (form: FormData) =>
|
|
apiFetchForm<KnowledgeDocument>("/knowledge/documents/confirm", form),
|
|
|
|
remove: (docId: string) =>
|
|
apiFetch<{ doc_id?: string; deleted?: boolean; [key: string]: unknown }>(
|
|
`/knowledge/documents/${encodeURIComponent(docId)}`,
|
|
{ method: "DELETE" }
|
|
),
|
|
};
|
|
|
|
/** 组装知识库上传表单,空值不提交,避免后端把空串当成有效参数。 */
|
|
export function buildKnowledgeForm(
|
|
file: File,
|
|
extra: {
|
|
title?: string;
|
|
doc_id?: string;
|
|
collection_name?: string;
|
|
strategy?: string;
|
|
chunk_size?: number | null;
|
|
chunk_overlap?: number | null;
|
|
} = {}
|
|
): FormData {
|
|
const form = new FormData();
|
|
form.append("file", file);
|
|
for (const [key, value] of Object.entries(extra)) {
|
|
if (value !== undefined && value !== null && value !== "") {
|
|
form.append(key, String(value));
|
|
}
|
|
}
|
|
return form;
|
|
}
|