feat: 完善知识库批量导入与公共 API
- 新增批量异步导入、状态查询、失败重试与中断恢复链路 - 拆分知识库读取、导入、维护权限并完善 Public API 契约 - 补充数据库迁移、管理端交互、接口说明与相关测试
This commit is contained in:
@@ -0,0 +1,75 @@
|
||||
interface UploadResponsePayload {
|
||||
code?: unknown;
|
||||
error?: unknown;
|
||||
errorCode?: unknown;
|
||||
message?: unknown;
|
||||
msg?: unknown;
|
||||
}
|
||||
|
||||
interface ResolveUploadResponseOptions {
|
||||
fallbackMessage: string;
|
||||
responseText: string;
|
||||
status: number;
|
||||
statusText?: string;
|
||||
}
|
||||
|
||||
interface UploadResponseResult {
|
||||
message?: string;
|
||||
success: boolean;
|
||||
}
|
||||
|
||||
function resolveMessage(
|
||||
payload: undefined | UploadResponsePayload,
|
||||
statusText: string | undefined,
|
||||
fallbackMessage: string,
|
||||
) {
|
||||
const message =
|
||||
payload?.message ?? payload?.error ?? payload?.msg ?? statusText;
|
||||
return typeof message === 'string' && message.trim()
|
||||
? message.trim()
|
||||
: fallbackMessage;
|
||||
}
|
||||
|
||||
export function resolveDocumentUploadResponse({
|
||||
fallbackMessage,
|
||||
responseText,
|
||||
status,
|
||||
statusText,
|
||||
}: ResolveUploadResponseOptions): UploadResponseResult {
|
||||
const isHttpSuccess = status >= 200 && status < 300;
|
||||
const normalizedBody = responseText.replace(/^\uFEFF/, '').trim();
|
||||
let payload: undefined | UploadResponsePayload;
|
||||
|
||||
if (normalizedBody) {
|
||||
try {
|
||||
payload = JSON.parse(normalizedBody) as UploadResponsePayload;
|
||||
} catch {
|
||||
return {
|
||||
message: fallbackMessage,
|
||||
success: false,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
if (!isHttpSuccess) {
|
||||
return {
|
||||
message: resolveMessage(payload, statusText, fallbackMessage),
|
||||
success: false,
|
||||
};
|
||||
}
|
||||
|
||||
if (status === 204) {
|
||||
return { success: true };
|
||||
}
|
||||
|
||||
const rawErrorCode = payload?.errorCode ?? payload?.code;
|
||||
const errorCode = Number(rawErrorCode);
|
||||
if (!Number.isFinite(errorCode) || errorCode !== 0) {
|
||||
return {
|
||||
message: resolveMessage(payload, statusText, fallbackMessage),
|
||||
success: false,
|
||||
};
|
||||
}
|
||||
|
||||
return { success: true };
|
||||
}
|
||||
Reference in New Issue
Block a user