@@ -293,15 +293,18 @@ async def enrich_course(
293293 result ["agent_results" ]["agent2" ] = course_data
294294
295295 # ====================================================================
296- # CONTACT FALLBACK CASCADE: Ensure we have 2+ contacts
296+ # CONTACT FALLBACK CASCADE: Accumulate contacts from multiple sources
297297 # ====================================================================
298298 contact_source = course_data ["data" ].get ("source" , "pga_directory" ) # Default source
299+ all_sources = ["pga_directory" ] # Track all sources used
299300
300- if staff and len (staff ) >= 2 :
301- print (f" ✅ Agent 2 met threshold: { len (staff )} contacts found" )
302- course_data ["data" ]["contact_source" ] = contact_source
301+ if len (staff ) >= 1 :
302+ print (f" ✅ Agent 2 found { len (staff )} contact(s) - checking for additional contacts..." )
303303 else :
304- print (f" ⚠️ Agent 2 below threshold: { len (staff )} contact(s), triggering fallbacks..." )
304+ print (f" ⚠️ Agent 2 found 0 contacts - triggering fallbacks..." )
305+
306+ # ALWAYS try fallbacks to maximize contact count (unless we already have 3+)
307+ if len (staff ) < 3 :
305308 fallback_attempts = []
306309
307310 # FALLBACK 1: LinkedIn Company Page
@@ -316,16 +319,20 @@ async def enrich_course(
316319 state_code
317320 )
318321
319- if linkedin_staff and len (linkedin_staff ) >= 2 :
320- print (f" ✅ Agent 2.1 SUCCESS: { len (linkedin_staff )} contacts from LinkedIn" )
321- staff = linkedin_staff
322+ if linkedin_staff and len (linkedin_staff ) > 0 :
323+ print (f" ✅ Agent 2.1 found { len (linkedin_staff )} contact(s) from LinkedIn" )
324+ # MERGE contacts (avoid duplicates by name)
325+ existing_names = {c .get ("name" , "" ).lower () for c in staff }
326+ new_contacts = [c for c in linkedin_staff if c .get ("name" , "" ).lower () not in existing_names ]
327+ staff .extend (new_contacts )
322328 course_data ["data" ]["staff" ] = staff
323- course_data [ "data" ][ "contact_source" ] = " linkedin_company"
324- course_data [ "data" ][ "fallback_sources_attempted" ] = fallback_attempts
329+ all_sources . append ( " linkedin_company")
330+ print ( f" 📊 Total contacts now: { len ( staff ) } " )
325331 else :
326- print (f" ⚠️ Agent 2.1 below threshold: { len ( linkedin_staff ) } contacts" )
332+ print (f" ⚠️ Agent 2.1 found 0 contacts" )
327333
328- # FALLBACK 2: Perplexity AI Research
334+ # FALLBACK 2: Perplexity AI Research (try even if LinkedIn succeeded)
335+ if len (staff ) < 3 :
329336 print (" 🤖 [Fallback 2/2] Agent 2.2: Perplexity AI Research..." )
330337 fallback_attempts .append ("perplexity_research" )
331338
@@ -340,21 +347,33 @@ async def enrich_course(
340347 state_code
341348 )
342349
343- if perplexity_staff and len (perplexity_staff ) >= 1 :
344- print (f" ✅ Agent 2.2 SUCCESS: { len (perplexity_staff )} contacts from Perplexity" )
345- staff = perplexity_staff
350+ if perplexity_staff and len (perplexity_staff ) > 0 :
351+ print (f" ✅ Agent 2.2 found { len (perplexity_staff )} contact(s) from Perplexity" )
352+ # MERGE contacts (avoid duplicates by name)
353+ existing_names = {c .get ("name" , "" ).lower () for c in staff }
354+ new_contacts = [c for c in perplexity_staff if c .get ("name" , "" ).lower () not in existing_names ]
355+ staff .extend (new_contacts )
346356 course_data ["data" ]["staff" ] = staff
347- course_data [ "data" ][ "contact_source" ] = " perplexity_research"
348- course_data [ "data" ][ "fallback_sources_attempted" ] = fallback_attempts
357+ all_sources . append ( " perplexity_research")
358+ print ( f" 📊 Total contacts now: { len ( staff ) } " )
349359 else :
350- # All sources exhausted
351- print (" ❌ All sources exhausted (Agent 2, 2.1, 2.2) - NO CONTACTS FOUND" )
352- course_data ["data" ]["fallback_sources_attempted" ] = fallback_attempts
353- raise Exception ("No contacts available from any source (PGA, LinkedIn, Perplexity)" )
360+ print (f" ❌ Perplexity found no staff for { course_data ['data' ].get ('course_name' )} " )
361+
362+ # Set primary contact source (prioritize based on most contacts)
363+ if len (staff ) > 0 :
364+ course_data ["data" ]["contact_source" ] = all_sources [- 1 ] if len (all_sources ) > 1 else "pga_directory"
365+ course_data ["data" ]["fallback_sources_attempted" ] = fallback_attempts
366+ course_data ["data" ]["all_sources" ] = all_sources
367+ else :
368+ # All sources exhausted with 0 contacts
369+ print (" ❌ All sources exhausted (Agent 2, 2.1, 2.2) - NO CONTACTS FOUND" )
370+ course_data ["data" ]["fallback_sources_attempted" ] = fallback_attempts
371+ raise Exception ("No contacts available from any source (PGA, LinkedIn, Perplexity)" )
354372
355373 except ImportError as e :
356374 print (f" ⚠️ Fallback agents not available: { e } " )
357- raise Exception ("Agent 2: No staff contacts found and fallbacks unavailable" )
375+ if len (staff ) == 0 :
376+ raise Exception ("Agent 2: No staff contacts found and fallbacks unavailable" )
358377
359378 # At this point, we have staff from SOME source (Agent 2, 2.1, or 2.2)
360379 print (f" ▶️ Proceeding with { len (staff )} contacts from source: { course_data ['data' ].get ('contact_source' )} \n " )
@@ -610,7 +629,7 @@ async def enrich_course(
610629 "total_duration_seconds" : round (total_duration , 1 ),
611630 "contacts_enriched" : len (enriched_contacts ),
612631 "agent_costs" : {
613- "agent1" : course_data .get ("cost" , 0 ),
632+ "agent1" : url_result .get ("cost" , 0 ), # Fixed: was reading from wrong variable
614633 "agent2" : course_data .get ("cost" , 0 ),
615634 "agent6" : round (course_intel .get ("cost" , 0 ), 4 ),
616635 "agent7" : water_data .get ("cost" , 0 ),
@@ -663,6 +682,7 @@ async def enrich_course(
663682 )
664683
665684 result ["error" ] = str (e )
685+ result ["course_id" ] = course_id # Include course_id so webhook can fire on failures
666686 result ["summary" ]["total_duration_seconds" ] = round (total_duration , 1 )
667687
668688 return result
0 commit comments