This commit is contained in:
Timothy Jaeryang Baek
2026-06-01 11:34:46 -07:00
parent 1010fd3142
commit ad9f2eeb15
4 changed files with 193 additions and 1 deletions
+44
View File
@@ -253,6 +253,50 @@ export const searchKnowledgeFilesById = async (
return res;
};
export const getPendingKnowledgeFiles = async (token: string, id: string) => {
let error = null;
const res = await fetch(`${WEBUI_API_BASE_URL}/knowledge/${id}/files/pending`, {
method: 'GET',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
authorization: `Bearer ${token}`
}
})
.then(async (res) => {
if (!res.ok) throw await res.json();
return res.json();
})
.catch((err) => {
error = err.detail;
console.error(err);
return [];
});
if (error) {
throw error;
}
return res;
};
export const streamPendingKnowledgeFiles = async (token: string, id: string) => {
const res = await fetch(`${WEBUI_API_BASE_URL}/knowledge/${id}/files/pending?stream=true`, {
method: 'GET',
headers: {
Accept: 'text/event-stream',
authorization: `Bearer ${token}`
}
});
if (!res.ok) {
throw new Error('Failed to stream pending files');
}
return res;
};
type KnowledgeUpdateForm = {
name?: string;
description?: string;
@@ -28,6 +28,7 @@
import {
addFileToKnowledgeById,
getKnowledgeById,
getPendingKnowledgeFiles,
removeFileFromKnowledgeById,
resetKnowledgeById,
updateFileFromKnowledgeById,
@@ -130,6 +131,8 @@
let pendingDeleteDirectoryId: string | null = null;
let deleteDirectoryContents = true;
let pendingPollTimer: ReturnType<typeof setInterval> | null = null;
const reset = () => {
currentPage = 1;
};
@@ -198,7 +201,42 @@
fileItemsTotal = res.total;
directoryItems = res.directories ?? [];
breadcrumbs = res.breadcrumbs ?? [];
// Merge in-flight files not yet linked to the knowledge base
try {
const pendingFiles = await getPendingKnowledgeFiles(localStorage.token, knowledgeId);
if (pendingFiles && pendingFiles.length > 0) {
const existingIds = new Set(fileItems.map((f) => f.id));
const newPending = pendingFiles
.filter((f) => !existingIds.has(f.id))
.map((f) => ({
...f,
name: f.meta?.name ?? f.filename,
status: 'uploading'
}));
if (newPending.length > 0) {
fileItems = [...newPending, ...fileItems];
// Start polling for completion (if not already polling)
if (!pendingPollTimer) {
pendingPollTimer = setInterval(async () => {
try {
const still = await getPendingKnowledgeFiles(localStorage.token, knowledgeId);
if (!still || still.length === 0) {
clearInterval(pendingPollTimer);
pendingPollTimer = null;
init();
}
} catch {}
}, 5000);
}
}
}
} catch (e) {
console.warn('Failed to fetch pending files:', e);
}
}
return res;
};
@@ -1079,6 +1117,7 @@
onDestroy(() => {
clearTimeout(searchDebounceTimer);
if (pendingPollTimer) { clearInterval(pendingPollTimer); pendingPollTimer = null; }
mediaQuery?.removeEventListener('change', handleMediaQuery);
const dropZone = document.querySelector('body');
dropZone?.removeEventListener('dragover', onDragOver);
@@ -1086,6 +1125,7 @@
dropZone?.removeEventListener('dragleave', onDragLeave);
});
const decodeString = (str: string) => {
try {
return decodeURIComponent(str);