Spaces:
Sleeping
Sleeping
axeronis commited on
Commit Β·
95641e0
1
Parent(s): dd5dc05
fix: resolve SyntaxError in webhook.py by properly nesting try-except blocks
Browse files- backend/app/api/webhook.py +121 -121
backend/app/api/webhook.py
CHANGED
|
@@ -2339,129 +2339,129 @@ async def process_background_ai_tasks(
|
|
| 2339 |
conversation_history = result.scalars().all()
|
| 2340 |
|
| 2341 |
# 1. & 2. PARALLEL PROCESSING: ESCALATION + DEEP ANALYSIS
|
| 2342 |
-
|
| 2343 |
-
|
| 2344 |
-
|
| 2345 |
-
|
| 2346 |
-
|
| 2347 |
-
|
| 2348 |
-
|
| 2349 |
-
|
| 2350 |
-
|
| 2351 |
-
|
| 2352 |
-
|
| 2353 |
-
|
| 2354 |
-
|
| 2355 |
-
|
| 2356 |
-
|
| 2357 |
-
|
| 2358 |
-
|
| 2359 |
-
|
| 2360 |
-
|
| 2361 |
-
|
| 2362 |
-
|
| 2363 |
-
|
| 2364 |
-
|
| 2365 |
-
|
| 2366 |
-
|
| 2367 |
-
|
| 2368 |
-
|
| 2369 |
-
|
| 2370 |
-
|
| 2371 |
-
|
| 2372 |
-
|
| 2373 |
-
|
| 2374 |
-
|
| 2375 |
-
|
| 2376 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2377 |
|
| 2378 |
-
|
| 2379 |
-
|
| 2380 |
-
esc_lead.
|
| 2381 |
-
|
| 2382 |
-
|
| 2383 |
-
|
| 2384 |
-
|
| 2385 |
-
|
| 2386 |
-
|
| 2387 |
-
lead_name=esc_lead.name or "Unknown",
|
| 2388 |
-
reason=escalation_result.reason,
|
| 2389 |
-
urgency=escalation_result.urgency,
|
| 2390 |
-
details=escalation_result.details,
|
| 2391 |
-
last_message=user_message_text,
|
| 2392 |
-
tenant_id=str(esc_tenant.id),
|
| 2393 |
-
channel=channel
|
| 2394 |
-
)
|
| 2395 |
-
except Exception as e:
|
| 2396 |
-
logger.error(f"Background escalation check failed: {e}")
|
| 2397 |
-
|
| 2398 |
-
async def run_analysis():
|
| 2399 |
-
try:
|
| 2400 |
-
async with async_session_maker() as an_session:
|
| 2401 |
-
# Re-fetch everything in fresh session
|
| 2402 |
-
an_lead = await an_session.get(Lead, lead_id)
|
| 2403 |
-
an_tenant = await an_session.get(Tenant, tenant_id)
|
| 2404 |
-
stmt = select(Message).where(Message.conversation_id == conversation_id).order_by(Message.created_at)
|
| 2405 |
-
res = await an_session.execute(stmt)
|
| 2406 |
-
an_history = res.scalars().all()
|
| 2407 |
-
|
| 2408 |
-
ai_core = get_ai_core()
|
| 2409 |
-
analysis = await ai_core.analyze_lead(
|
| 2410 |
-
session=an_session,
|
| 2411 |
-
lead=an_lead,
|
| 2412 |
-
conversation_history=an_history,
|
| 2413 |
-
tenant=an_tenant
|
| 2414 |
)
|
| 2415 |
-
|
| 2416 |
-
|
| 2417 |
-
logger.info(f"β
Background analysis complete: {analysis.get('heat_score')}%")
|
| 2418 |
-
return analysis
|
| 2419 |
-
except Exception as e:
|
| 2420 |
-
logger.error(f"Background lead analysis failed: {e}")
|
| 2421 |
-
return None
|
| 2422 |
|
| 2423 |
-
|
| 2424 |
-
|
| 2425 |
-
|
| 2426 |
-
|
| 2427 |
-
|
| 2428 |
-
|
| 2429 |
-
|
| 2430 |
-
|
| 2431 |
-
|
| 2432 |
-
|
| 2433 |
-
|
| 2434 |
-
|
| 2435 |
-
|
| 2436 |
-
|
| 2437 |
-
|
| 2438 |
-
|
| 2439 |
-
|
| 2440 |
|
| 2441 |
-
|
| 2442 |
-
"
|
| 2443 |
-
|
| 2444 |
-
|
| 2445 |
-
|
| 2446 |
-
|
| 2447 |
-
|
| 2448 |
-
|
| 2449 |
-
|
| 2450 |
-
|
| 2451 |
-
|
| 2452 |
-
|
| 2453 |
-
|
| 2454 |
-
|
| 2455 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2456 |
|
| 2457 |
-
except Exception as e:
|
| 2458 |
-
logger.error(f"CRITICAL: Background tasks failed: {e}", exc_info=True)
|
| 2459 |
-
# Try to reset status as last resort
|
| 2460 |
-
async with async_session_maker() as reset_session:
|
| 2461 |
-
try:
|
| 2462 |
-
l = await reset_session.get(Lead, lead_id)
|
| 2463 |
-
if l:
|
| 2464 |
-
l.processing_status = "idle"
|
| 2465 |
-
reset_session.add(l)
|
| 2466 |
-
await reset_session.commit()
|
| 2467 |
-
except: pass
|
|
|
|
| 2339 |
conversation_history = result.scalars().all()
|
| 2340 |
|
| 2341 |
# 1. & 2. PARALLEL PROCESSING: ESCALATION + DEEP ANALYSIS
|
| 2342 |
+
# Set status to analyzing
|
| 2343 |
+
lead.processing_status = "analyzing"
|
| 2344 |
+
session.add(lead)
|
| 2345 |
+
await session.commit()
|
| 2346 |
+
|
| 2347 |
+
async def run_escalation():
|
| 2348 |
+
try:
|
| 2349 |
+
async with async_session_maker() as esc_session:
|
| 2350 |
+
# Re-fetch everything in fresh session
|
| 2351 |
+
esc_lead = await esc_session.get(Lead, lead_id)
|
| 2352 |
+
esc_tenant = await esc_session.get(Tenant, tenant_id)
|
| 2353 |
+
stmt = select(Message).where(Message.conversation_id == conversation_id).order_by(Message.created_at)
|
| 2354 |
+
res = await esc_session.execute(stmt)
|
| 2355 |
+
esc_history = res.scalars().all()
|
| 2356 |
+
|
| 2357 |
+
# Build context for escalation
|
| 2358 |
+
simplified_history = []
|
| 2359 |
+
for msg in esc_history[-10:]:
|
| 2360 |
+
simplified_history.append({"sender_type": msg.sender_type, "content": msg.content})
|
| 2361 |
+
|
| 2362 |
+
last_ai_response = ""
|
| 2363 |
+
if esc_history:
|
| 2364 |
+
last_msg = esc_history[-1]
|
| 2365 |
+
if last_msg.sender_type == 'ai':
|
| 2366 |
+
last_ai_response = last_msg.content
|
| 2367 |
+
|
| 2368 |
+
escalation_result = await escalation_service.analyze_message_smart(
|
| 2369 |
+
user_message=user_message_text,
|
| 2370 |
+
ai_response=last_ai_response,
|
| 2371 |
+
conversation_history=simplified_history,
|
| 2372 |
+
heat_score=esc_lead.heat_score or 0,
|
| 2373 |
+
business_type=getattr(esc_tenant, 'business_type', ''),
|
| 2374 |
+
current_stage=None
|
| 2375 |
+
)
|
| 2376 |
+
|
| 2377 |
+
if escalation_result.should_escalate:
|
| 2378 |
+
logger.info(f"π¨ Escalation triggered: {escalation_result.reason}")
|
| 2379 |
+
esc_lead.needs_attention = True
|
| 2380 |
+
esc_lead.escalation_reason = escalation_result.reason
|
| 2381 |
+
esc_session.add(esc_lead)
|
| 2382 |
+
await esc_session.commit()
|
| 2383 |
|
| 2384 |
+
await escalation_service.send_escalation_notification(
|
| 2385 |
+
lead_id=str(esc_lead.id),
|
| 2386 |
+
lead_name=esc_lead.name or "Unknown",
|
| 2387 |
+
reason=escalation_result.reason,
|
| 2388 |
+
urgency=escalation_result.urgency,
|
| 2389 |
+
details=escalation_result.details,
|
| 2390 |
+
last_message=user_message_text,
|
| 2391 |
+
tenant_id=str(esc_tenant.id),
|
| 2392 |
+
channel=channel
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2393 |
)
|
| 2394 |
+
except Exception as e:
|
| 2395 |
+
logger.error(f"Background escalation check failed: {e}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2396 |
|
| 2397 |
+
async def run_analysis():
|
| 2398 |
+
try:
|
| 2399 |
+
async with async_session_maker() as an_session:
|
| 2400 |
+
# Re-fetch everything in fresh session
|
| 2401 |
+
an_lead = await an_session.get(Lead, lead_id)
|
| 2402 |
+
an_tenant = await an_session.get(Tenant, tenant_id)
|
| 2403 |
+
stmt = select(Message).where(Message.conversation_id == conversation_id).order_by(Message.created_at)
|
| 2404 |
+
res = await an_session.execute(stmt)
|
| 2405 |
+
an_history = res.scalars().all()
|
| 2406 |
+
|
| 2407 |
+
ai_core = get_ai_core()
|
| 2408 |
+
analysis = await ai_core.analyze_lead(
|
| 2409 |
+
session=an_session,
|
| 2410 |
+
lead=an_lead,
|
| 2411 |
+
conversation_history=an_history,
|
| 2412 |
+
tenant=an_tenant
|
| 2413 |
+
)
|
| 2414 |
|
| 2415 |
+
if analysis:
|
| 2416 |
+
logger.info(f"β
Background analysis complete: {analysis.get('heat_score')}%")
|
| 2417 |
+
return analysis
|
| 2418 |
+
except Exception as e:
|
| 2419 |
+
logger.error(f"Background lead analysis failed: {e}")
|
| 2420 |
+
return None
|
| 2421 |
+
|
| 2422 |
+
logger.info(
|
| 2423 |
+
"π Starting background AI tasks (Isolated Sessions)",
|
| 2424 |
+
lead_id=str(lead_id),
|
| 2425 |
+
conversation_id=str(conversation_id)
|
| 2426 |
+
)
|
| 2427 |
+
|
| 2428 |
+
# Execute both in parallel
|
| 2429 |
+
results = await asyncio.gather(run_escalation(), run_analysis())
|
| 2430 |
+
analysis_result = results[1]
|
| 2431 |
+
|
| 2432 |
+
# 3. FINAL UI UPDATE
|
| 2433 |
+
async with async_session_maker() as final_session:
|
| 2434 |
+
updated_lead = await final_session.get(Lead, lead_id)
|
| 2435 |
+
if updated_lead:
|
| 2436 |
+
updated_lead.processing_status = "idle"
|
| 2437 |
+
final_session.add(updated_lead)
|
| 2438 |
+
await final_session.commit()
|
| 2439 |
+
|
| 2440 |
+
await notify_lead_updated(str(updated_lead.id), {
|
| 2441 |
+
"id": str(updated_lead.id),
|
| 2442 |
+
"ai_summary_json": updated_lead.ai_summary_json,
|
| 2443 |
+
"qualification_data": updated_lead.qualification_data,
|
| 2444 |
+
"qualification_complete": updated_lead.qualification_complete,
|
| 2445 |
+
"heat_score": updated_lead.heat_score,
|
| 2446 |
+
"heat_level": updated_lead.heat_level.value if hasattr(updated_lead.heat_level, 'value') else str(updated_lead.heat_level),
|
| 2447 |
+
"psychotype": updated_lead.psychotype.value if hasattr(updated_lead.psychotype, 'value') else str(updated_lead.psychotype),
|
| 2448 |
+
"name": updated_lead.name,
|
| 2449 |
+
"processing_status": "idle",
|
| 2450 |
+
"needs_attention": updated_lead.needs_attention,
|
| 2451 |
+
"current_stage_id": str(updated_lead.current_stage_id) if updated_lead.current_stage_id else None
|
| 2452 |
+
})
|
| 2453 |
+
|
| 2454 |
+
logger.info(f"β
All background tasks completed for lead {lead_id}")
|
| 2455 |
+
|
| 2456 |
+
except Exception as e:
|
| 2457 |
+
logger.error(f"CRITICAL: Background tasks failed: {e}", exc_info=True)
|
| 2458 |
+
# Try to reset status as last resort
|
| 2459 |
+
async with async_session_maker() as reset_session:
|
| 2460 |
+
try:
|
| 2461 |
+
l = await reset_session.get(Lead, lead_id)
|
| 2462 |
+
if l:
|
| 2463 |
+
l.processing_status = "idle"
|
| 2464 |
+
reset_session.add(l)
|
| 2465 |
+
await reset_session.commit()
|
| 2466 |
+
except: pass
|
| 2467 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|