Fix AI upload: process inline instead of background
Background AI processing returned before results were ready, leaving the UI showing "AI processing..." forever. Now AI metadata generation runs inline (awaited) so results are returned immediately in the upload response. Background SSE-based processing can be re-enabled later for large batch uploads (10+ files). Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
parent
b3bb2831a0
commit
3fe4ed0b4a
1 changed files with 12 additions and 40 deletions
|
|
@ -87,45 +87,23 @@ async def upload_files(
|
|||
)
|
||||
|
||||
results = []
|
||||
ai_pending = [] # Files needing background AI processing
|
||||
|
||||
for upload_file in files:
|
||||
try:
|
||||
filepath = await file_svc.save_upload(upload_file, user_id)
|
||||
filename = Path(filepath).name
|
||||
|
||||
if metadata_source == "ai":
|
||||
# For AI source: save files first, process AI in background
|
||||
file_type = metadata_service.detect_file(filepath)
|
||||
old_metadata = metadata_service.extract_metadata(filepath, file_type)
|
||||
file_result = {
|
||||
"success": True,
|
||||
"filename": filename,
|
||||
"file_type": file_type.value,
|
||||
"current_metadata": old_metadata,
|
||||
"suggested_metadata": {"title": "", "subject": "AI processing...", "keywords": ""},
|
||||
"filepath": filepath,
|
||||
"metadata_source": "ai",
|
||||
"ai_status": "pending",
|
||||
}
|
||||
store.add_file_to_session(session_id, file_result)
|
||||
ai_pending.append({
|
||||
"file_index": len(results),
|
||||
"filepath": filepath,
|
||||
"filename": filename,
|
||||
"file_type": file_type,
|
||||
})
|
||||
results.append(file_result)
|
||||
else:
|
||||
file_result = await metadata_service.process_uploaded_file(
|
||||
filepath=filepath,
|
||||
filename=filename,
|
||||
metadata_source=metadata_source,
|
||||
lookup=lookup,
|
||||
import_map=import_map,
|
||||
)
|
||||
store.add_file_to_session(session_id, file_result)
|
||||
results.append(file_result)
|
||||
# Process file through the standard pipeline (including AI inline)
|
||||
file_result = await metadata_service.process_uploaded_file(
|
||||
filepath=filepath,
|
||||
filename=filename,
|
||||
metadata_source=metadata_source,
|
||||
lookup=lookup,
|
||||
import_map=import_map,
|
||||
)
|
||||
|
||||
store.add_file_to_session(session_id, file_result)
|
||||
results.append(file_result)
|
||||
|
||||
except ValueError as e:
|
||||
results.append({"filename": upload_file.filename, "error": str(e)})
|
||||
|
|
@ -133,16 +111,10 @@ async def upload_files(
|
|||
logger.error(f"Upload error for {upload_file.filename}: {e}")
|
||||
results.append({"filename": upload_file.filename, "error": str(e)})
|
||||
|
||||
# Start background AI processing if needed
|
||||
if ai_pending:
|
||||
import asyncio
|
||||
from ..services.ai_service import process_bulk_ai
|
||||
asyncio.create_task(process_bulk_ai(session_id, ai_pending, store, user_id))
|
||||
|
||||
# Strip server paths from client response
|
||||
safe_results = [{k: v for k, v in r.items() if k != "filepath"} for r in results]
|
||||
|
||||
return {"success": True, "session_id": session_id, "files": safe_results, "ai_processing": bool(ai_pending)}
|
||||
return {"success": True, "session_id": session_id, "files": safe_results}
|
||||
|
||||
|
||||
@router.post("/upload-excel")
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue