pranavkv commited on
Commit
a7b642e
Β·
verified Β·
1 Parent(s): b6e2226

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +68 -113
app.py CHANGED
@@ -338,133 +338,88 @@ class UltimateTopcoderMCPEngine:
338
  sort_by: str = None,
339
  sort_order: str = None,
340
  ) -> List[Challenge]:
341
- """FIXED: Debug version - shows exactly what MCP is doing"""
342
 
343
- # Always try to connect
344
- print(f"πŸ”„ Attempting to fetch REAL challenges (limit: {limit})")
345
  print(f"🎯 Query: '{query}' | Skills: {user_profile.skills}")
346
- connection_success = await self.initialize_connection()
347
 
 
 
348
  if not connection_success:
349
- print("❌ Could not establish MCP connection, using fallback")
350
  return []
351
 
352
- # Try multiple query strategies to see what works
353
- query_strategies = []
354
 
355
- # Strategy 1: Simple query with minimal parameters
356
- simple_query = {"perPage": min(limit, 20)}
357
  if query.strip():
358
- simple_query["search"] = query.strip()
359
- query_strategies.append(("Simple Search", simple_query))
360
 
361
- # Strategy 2: Skills-based query
362
- skill_keywords = self.extract_technologies_from_query(
363
- query + " " + " ".join(user_profile.skills + user_profile.interests)
364
- )
365
- if skill_keywords:
366
- skills_query = {"perPage": min(limit, 20)}
367
- skills_query["tags"] = skill_keywords[:3] # Limit to 3 skills
368
- query_strategies.append(("Skills Query", skills_query))
369
-
370
- # Strategy 3: Your comprehensive query
371
- comprehensive_query = {"perPage": min(limit, 20)}
372
- if status:
373
- comprehensive_query["status"] = status
374
- if query.strip():
375
- comprehensive_query["search"] = query.strip()
376
- if sort_by:
377
- comprehensive_query["sortBy"] = sort_by
378
- if sort_order:
379
- comprehensive_query["sortOrder"] = sort_order
380
- query_strategies.append(("Comprehensive", comprehensive_query))
381
-
382
- # Try each strategy until one works
383
- for strategy_name, mcp_query in query_strategies:
384
- print(f"\nπŸ§ͺ Trying {strategy_name} strategy:")
385
- print(f"πŸ”§ Query parameters: {mcp_query}")
386
-
387
- # Call the MCP tool
388
- result = await self.call_tool("query-tc-challenges", mcp_query)
389
 
390
- if not result:
391
- print(f"❌ {strategy_name} failed - no result")
392
- continue
393
-
394
- print(f"πŸ“Š {strategy_name} result type: {type(result)}")
395
- if isinstance(result, dict):
396
- print(f"πŸ“Š Result keys: {list(result.keys())}")
397
-
398
- # Show first few characters of each field for debugging
399
- for key, value in result.items():
400
- if isinstance(value, str):
401
- print(f" {key}: {str(value)[:100]}...")
402
- elif isinstance(value, dict):
403
- print(f" {key}: dict with keys {list(value.keys())}")
404
- elif isinstance(value, list):
405
- print(f" {key}: list with {len(value)} items")
406
- else:
407
- print(f" {key}: {type(value)}")
408
 
409
- # Try to parse the response
410
- challenge_data_list = []
411
-
412
- # Check all possible data locations
413
- data_sources = [
414
- ("structuredContent.data", result.get("structuredContent", {}).get("data", [])),
415
- ("data", result.get("data", [])),
416
- ("content[0].text", None), # Will handle separately
417
- ("result", result if isinstance(result, list) else [])
418
- ]
419
-
420
- for source_name, data in data_sources:
421
- if data and isinstance(data, list) and len(data) > 0:
422
- challenge_data_list = data
423
- print(f"βœ… Found {len(challenge_data_list)} challenges in {source_name}")
424
- break
 
 
 
425
 
426
- # Special handling for content field
427
- if not challenge_data_list and "content" in result:
428
- content = result["content"]
429
- if isinstance(content, list) and len(content) > 0:
430
- content_item = content[0]
431
- if isinstance(content_item, dict) and content_item.get("type") == "text":
432
- try:
433
- text_content = content_item.get("text", "")
434
- print(f"πŸ“ Content text preview: {text_content[:200]}...")
435
- parsed_data = json.loads(text_content)
436
- if "data" in parsed_data:
437
- challenge_data_list = parsed_data["data"]
438
- print(f"βœ… Found {len(challenge_data_list)} challenges in parsed content")
439
- except json.JSONDecodeError as e:
440
- print(f"❌ Failed to parse content JSON: {e}")
441
-
442
- if challenge_data_list:
443
- print(f"🎯 Sample challenge data keys: {list(challenge_data_list[0].keys()) if challenge_data_list else 'None'}")
444
-
445
- # Convert challenges
446
- challenges = []
447
- for i, item in enumerate(challenge_data_list):
448
- if isinstance(item, dict):
449
- try:
450
- challenge = self.convert_topcoder_challenge(item)
451
- challenges.append(challenge)
452
- if i == 0: # Show first challenge details
453
- print(f"πŸ“‹ First challenge: {challenge.title} | {challenge.prize} | {challenge.technologies}")
454
- except Exception as e:
455
- print(f"❌ Error converting challenge {i}: {e}")
456
- continue
457
-
458
- if challenges:
459
- print(f"πŸŽ‰ SUCCESS with {strategy_name}! Converted {len(challenges)} REAL challenges")
460
- return challenges
461
- else:
462
- print(f"❌ {strategy_name} - no challenges converted")
463
  else:
464
- print(f"❌ {strategy_name} - no challenge data found")
465
 
466
- print("❌ All strategies failed - no real MCP data retrieved")
467
- return []
 
 
 
 
 
 
468
 
469
  def calculate_advanced_compatibility_score(self, challenge: Challenge, user_profile: UserProfile, query: str) -> tuple:
470
  score = 0.0
 
338
  sort_by: str = None,
339
  sort_order: str = None,
340
  ) -> List[Challenge]:
341
+ """FIXED: Guaranteed to return real challenges when MCP works"""
342
 
343
+ print(f"πŸ”„ Fetching REAL challenges (limit: {limit})")
 
344
  print(f"🎯 Query: '{query}' | Skills: {user_profile.skills}")
 
345
 
346
+ # Always try to connect
347
+ connection_success = await self.initialize_connection()
348
  if not connection_success:
349
+ print("❌ Could not establish MCP connection")
350
  return []
351
 
352
+ # Build simple query that we KNOW works from debug
353
+ mcp_query = {"perPage": min(limit, 20)}
354
 
355
+ # Add search term if provided
 
356
  if query.strip():
357
+ mcp_query["search"] = query.strip()
 
358
 
359
+ # Add status filter
360
+ if status and status != "":
361
+ mcp_query["status"] = status
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
362
 
363
+ print(f"πŸ”§ MCP Query: {mcp_query}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
364
 
365
+ # Call the MCP tool (we know this works from debug)
366
+ result = await self.call_tool("query-tc-challenges", mcp_query)
367
+
368
+ if not result:
369
+ print("❌ No result from MCP tool call")
370
+ return []
371
+
372
+ print(f"πŸ“Š Got MCP result with keys: {list(result.keys()) if isinstance(result, dict) else 'Not dict'}")
373
+
374
+ # Extract challenge data (we know it's in structuredContent.data from debug)
375
+ challenge_data_list = []
376
+
377
+ if isinstance(result, dict):
378
+ # Primary location: structuredContent.data
379
+ if "structuredContent" in result:
380
+ structured = result["structuredContent"]
381
+ if isinstance(structured, dict) and "data" in structured:
382
+ challenge_data_list = structured["data"]
383
+ print(f"βœ… Found {len(challenge_data_list)} challenges in structuredContent.data")
384
 
385
+ # Fallback: direct data field
386
+ elif "data" in result:
387
+ challenge_data_list = result["data"]
388
+ print(f"βœ… Found {len(challenge_data_list)} challenges in data field")
389
+
390
+ if not challenge_data_list:
391
+ print("❌ No challenge data found in response")
392
+ return []
393
+
394
+ print(f"🎯 Processing {len(challenge_data_list)} raw challenges...")
395
+
396
+ # Convert to Challenge objects
397
+ challenges = []
398
+ for i, item in enumerate(challenge_data_list):
399
+ if isinstance(item, dict):
400
+ try:
401
+ challenge = self.convert_topcoder_challenge(item)
402
+ challenges.append(challenge)
403
+
404
+ # Show details for first challenge
405
+ if i == 0:
406
+ print(f"πŸ“‹ Sample: {challenge.title} | {challenge.prize} | {challenge.technologies}")
407
+
408
+ except Exception as e:
409
+ print(f"⚠️ Error converting challenge {i}: {e}")
410
+ # Don't let one bad challenge break everything
411
+ continue
 
 
 
 
 
 
 
 
 
 
412
  else:
413
+ print(f"⚠️ Unexpected challenge format at index {i}: {type(item)}")
414
 
415
+ print(f"πŸŽ‰ SUCCESSFULLY converted {len(challenges)} REAL challenges!")
416
+
417
+ # IMPORTANT: Always return challenges if we got any data at all
418
+ if len(challenges) > 0:
419
+ return challenges
420
+ else:
421
+ print("❌ No challenges could be converted from raw data")
422
+ return []
423
 
424
  def calculate_advanced_compatibility_score(self, challenge: Challenge, user_profile: UserProfile, query: str) -> tuple:
425
  score = 0.0