Improve KB upload
This commit is contained in:
@@ -611,12 +611,31 @@ export const deleteKnowledgeBase = async (kbId: string): Promise<void> => {
|
||||
};
|
||||
|
||||
export const uploadKnowledgeDocument = async (kbId: string, file: File): Promise<void> => {
|
||||
const payload = {
|
||||
name: file.name,
|
||||
size: `${(file.size / 1024).toFixed(1)} KB`,
|
||||
fileType: file.type || 'txt',
|
||||
};
|
||||
await apiRequest(`/knowledge/bases/${kbId}/documents`, { method: 'POST', body: payload });
|
||||
const formData = new FormData();
|
||||
formData.append('file', file);
|
||||
formData.append('name', file.name);
|
||||
formData.append('size', `${file.size} bytes`);
|
||||
formData.append('file_type', file.type || 'application/octet-stream');
|
||||
|
||||
const base = (import.meta.env.VITE_API_BASE_URL || 'http://127.0.0.1:8100/api').replace(/\/+$/, '');
|
||||
const url = `${base}/knowledge/bases/${kbId}/documents`;
|
||||
const response = await fetch(url, {
|
||||
method: 'POST',
|
||||
body: formData,
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
let message = `Upload failed: ${response.status}`;
|
||||
try {
|
||||
const data = await response.json();
|
||||
if (data?.detail) {
|
||||
message = typeof data.detail === 'string' ? data.detail : message;
|
||||
}
|
||||
} catch {
|
||||
// ignore parse error
|
||||
}
|
||||
throw new Error(message);
|
||||
}
|
||||
};
|
||||
|
||||
export const deleteKnowledgeDocument = async (kbId: string, docId: string): Promise<void> => {
|
||||
|
||||
Reference in New Issue
Block a user