From 2bb74d89bb6406e802be8fee377122b198207288 Mon Sep 17 00:00:00 2001 From: Gabriel Luiz Freitas Almeida Date: Thu, 4 Sep 2025 10:26:27 -0300 Subject: [PATCH] Implement file upload, ingestion, and deletion flow in KnowledgeDropdown component This commit enhances the KnowledgeDropdown component by integrating a complete file handling process that includes uploading files to Langflow, running an ingestion flow, and deleting the uploaded files. It introduces error handling for each step and dispatches appropriate events to notify the UI of the upload and ingestion results. These changes improve the robustness and maintainability of the component, contributing to a well-documented codebase. --- frontend/components/knowledge-dropdown.tsx | 54 ++++++++++++++++------ 1 file changed, 40 insertions(+), 14 deletions(-) diff --git a/frontend/components/knowledge-dropdown.tsx b/frontend/components/knowledge-dropdown.tsx index e73db5e9..c30d5420 100644 --- a/frontend/components/knowledge-dropdown.tsx +++ b/frontend/components/knowledge-dropdown.tsx @@ -80,24 +80,50 @@ export function KnowledgeDropdown({ active, variant = 'navigation' }: KnowledgeD const formData = new FormData() formData.append('file', files[0]) - const response = await fetch('/api/upload', { + // 1) Upload to Langflow + const upRes = await fetch('/api/langflow/files/upload', { method: 'POST', body: formData, }) - - const result = await response.json() - - if (response.ok) { - window.dispatchEvent(new CustomEvent('fileUploaded', { - detail: { file: files[0], result } - })) - // Trigger search refresh after successful upload - window.dispatchEvent(new CustomEvent('knowledgeUpdated')) - } else { - window.dispatchEvent(new CustomEvent('fileUploadError', { - detail: { filename: files[0].name, error: result.error || 'Upload failed' } - })) + const upJson = await upRes.json() + if (!upRes.ok) { + throw new Error(upJson?.error || 'Upload to Langflow failed') } + + const fileId = upJson?.id + const filePath = upJson?.path + if (!fileId || !filePath) { + throw new Error('Langflow did not return file id/path') + } + + // 2) Run ingestion flow + const runRes = await fetch('/api/langflow/ingest', { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ file_paths: [filePath] }), + }) + const runJson = await runRes.json() + if (!runRes.ok) { + throw new Error(runJson?.error || 'Langflow ingestion failed') + } + + // 3) Delete file from Langflow + const delRes = await fetch('/api/langflow/files', { + method: 'DELETE', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ file_ids: [fileId] }), + }) + const delJson = await delRes.json().catch(() => ({})) + if (!delRes.ok) { + throw new Error(delJson?.error || 'Langflow file delete failed') + } + + // Notify UI + window.dispatchEvent(new CustomEvent('fileUploaded', { + detail: { file: files[0], result: { file_id: fileId, file_path: filePath, run: runJson } } + })) + // Trigger search refresh after successful ingestion + window.dispatchEvent(new CustomEvent('knowledgeUpdated')) } catch (error) { window.dispatchEvent(new CustomEvent('fileUploadError', { detail: { filename: files[0].name, error: error instanceof Error ? error.message : 'Upload failed' }