Update API parameters and refactor file upload handling in VectorStoreDialog
This commit is contained in:
@@ -106,7 +106,9 @@ export const Route = createFileRoute("/_authenticated/archivos")({
|
||||
const stores = await callFilesAndVectorStoresApi<VectorStore[]>({
|
||||
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<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() {
|
||||
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<any>({
|
||||
module: "files",
|
||||
action: "upload",
|
||||
params: {
|
||||
fileBase64,
|
||||
filename: file.name,
|
||||
mimeType: file.type,
|
||||
const { data, error } = await supabase.functions.invoke<any>(
|
||||
"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<any>({
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user