From 31cd071175eb00aaaf979f60bb5ef93d0ca42803 Mon Sep 17 00:00:00 2001 From: "Roberto.silva" Date: Fri, 28 Nov 2025 09:59:26 -0600 Subject: [PATCH] Se corrige bugs de archivos --- src/routes/_authenticated/archivos2.tsx | 61 +++++++++++++------------ 1 file changed, 33 insertions(+), 28 deletions(-) diff --git a/src/routes/_authenticated/archivos2.tsx b/src/routes/_authenticated/archivos2.tsx index a429717..a949496 100644 --- a/src/routes/_authenticated/archivos2.tsx +++ b/src/routes/_authenticated/archivos2.tsx @@ -106,7 +106,9 @@ export const Route = createFileRoute("/_authenticated/archivos2")({ const stores = await callFilesAndVectorStoresApi({ module: "vectorStores", action: "list", - params: {}, + params: { + limit: 10, + }, }) return stores ?? [] }, @@ -119,8 +121,8 @@ function StatusBadge({ status }: { status: string }) { status === "completed" ? "Completado" : status === "in_progress" - ? "Procesando" - : status + ? "Procesando" + : status const base = "text-[10px] px-2 py-0.5 rounded-full border" if (status === "completed") { return ( @@ -184,7 +186,7 @@ function RouteComponent() { await callFilesAndVectorStoresApi({ module: "vectorStores", action: "delete", - params: { id }, + params: { vector_store_id: id }, }) await supabase @@ -472,50 +474,54 @@ function VectorStoreDialog({ } } - async function toBase64(f: File): Promise { - const buf = await f.arrayBuffer() - const bytes = new Uint8Array(buf) - let binary = "" - for (let i = 0; i < bytes.byteLength; i++) { - binary += String.fromCharCode(bytes[i]) - } - return btoa(binary) - } async function handleUpload() { if (!store || !file) { alert("Selecciona un archivo") return } + setUploading(true) try { - const fileBase64 = await toBase64(file) + // 1) Subir archivo a OpenAI vía Edge con FormData (igual que en tu script) + const formData = new FormData() + formData.append("module", "files") + formData.append("action", "upload") + formData.append("file", file) + formData.append("purpose", "assistants") // o lo que uses en tu flujo - // 1) Subir archivo a OpenAI vía Edge (módulo files) - const uploaded: any = await callFilesAndVectorStoresApi({ - module: "files", - action: "upload", - params: { - fileBase64, - filename: file.name, - mimeType: file.type, + const { data, error } = await supabase.functions.invoke( + "files-and-vector-stores-api", + { + body: formData, }, - }) + ) - const openaiFileId: string = - uploaded?.id ?? uploaded?.file?.id ?? uploaded?.data?.id + if (error) { + console.error(error) + throw error + } + + const uploaded = data + // La respuesta es el objeto "file" de OpenAI: + // { object: "file", id: "file-xxx", ... } + const openaiFileId: string | undefined = uploaded?.id if (!openaiFileId) { + console.error("Respuesta Edge inesperada:", uploaded) throw new Error("La Edge Function no devolvió el id del archivo") } - // 2) Mapear archivo al Vector Store + // 2) Mapear archivo al Vector Store (JSON normal) await callFilesAndVectorStoresApi({ module: "vectorStoreFiles", action: "create", params: { vector_store_id: store.id, - file_id: openaiFileId, + body: { + file_id: openaiFileId, + + }, }, }) @@ -542,7 +548,6 @@ function VectorStoreDialog({ setUploading(false) } } - async function handleDeleteFile(fileId: string) { if (!store) return if (!confirm("¿Eliminar este archivo del repositorio y de OpenAI?")) return