Se corrige bugs de archivos

This commit is contained in:
2025-11-28 09:59:26 -06:00
parent c44698d0c7
commit 31cd071175

View File

@@ -106,7 +106,9 @@ export const Route = createFileRoute("/_authenticated/archivos2")({
const stores = await callFilesAndVectorStoresApi<VectorStore[]>({ const stores = await callFilesAndVectorStoresApi<VectorStore[]>({
module: "vectorStores", module: "vectorStores",
action: "list", action: "list",
params: {}, params: {
limit: 10,
},
}) })
return stores ?? [] return stores ?? []
}, },
@@ -119,8 +121,8 @@ function StatusBadge({ status }: { status: string }) {
status === "completed" status === "completed"
? "Completado" ? "Completado"
: status === "in_progress" : status === "in_progress"
? "Procesando" ? "Procesando"
: status : status
const base = "text-[10px] px-2 py-0.5 rounded-full border" const base = "text-[10px] px-2 py-0.5 rounded-full border"
if (status === "completed") { if (status === "completed") {
return ( return (
@@ -184,7 +186,7 @@ function RouteComponent() {
await callFilesAndVectorStoresApi({ await callFilesAndVectorStoresApi({
module: "vectorStores", module: "vectorStores",
action: "delete", action: "delete",
params: { id }, params: { vector_store_id: id },
}) })
await supabase await supabase
@@ -472,50 +474,54 @@ function VectorStoreDialog({
} }
} }
async function toBase64(f: File): Promise<string> {
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() { async function handleUpload() {
if (!store || !file) { if (!store || !file) {
alert("Selecciona un archivo") alert("Selecciona un archivo")
return return
} }
setUploading(true) setUploading(true)
try { 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 { data, error } = await supabase.functions.invoke<any>(
const uploaded: any = await callFilesAndVectorStoresApi<any>({ "files-and-vector-stores-api",
module: "files", {
action: "upload", body: formData,
params: {
fileBase64,
filename: file.name,
mimeType: file.type,
}, },
}) )
const openaiFileId: string = if (error) {
uploaded?.id ?? uploaded?.file?.id ?? uploaded?.data?.id 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) { if (!openaiFileId) {
console.error("Respuesta Edge inesperada:", uploaded)
throw new Error("La Edge Function no devolvió el id del archivo") 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<any>({ await callFilesAndVectorStoresApi<any>({
module: "vectorStoreFiles", module: "vectorStoreFiles",
action: "create", action: "create",
params: { params: {
vector_store_id: store.id, vector_store_id: store.id,
file_id: openaiFileId, body: {
file_id: openaiFileId,
},
}, },
}) })
@@ -542,7 +548,6 @@ function VectorStoreDialog({
setUploading(false) setUploading(false)
} }
} }
async function handleDeleteFile(fileId: string) { async function handleDeleteFile(fileId: string) {
if (!store) return if (!store) return
if (!confirm("¿Eliminar este archivo del repositorio y de OpenAI?")) return if (!confirm("¿Eliminar este archivo del repositorio y de OpenAI?")) return