Upload app.py
Browse files1234567176 unmodified lines1841851861861871881891901911921931941951961971981992002011872032042052061881892082091901911921932121941952142151961971982171992002012022032042052062072082092102112122132142152162172186 unmodified lines2252262272362282292382302312322332342422352362372452382392402412422432442502452462472532482492502512572582592602612622632522532542552562572582592702602612622632642652762662672682693 unmodified lines27327427528628727627727828929029129229329429529629729829930030130230330427928028128228328428530628628728828929029129229329429529629729829930030130230330430530630730830931031131231331472 unmodified lines38738838938539039139239338939039439539639739839439940040139640240340440528 unmodified lines434435436431437438439440441"""
FINAL Topcoder Challenge Intelligence Assistant
With REAL MCP Integration - Ready for Production
FIXED: Now uses structuredContent for real challenge data
"""
import asyncio
import httpx176 unmodified lines
return None
def extract_real_challenge_data(self, raw_data: str) -> List[Dict]:
"""Extract challenge data from the raw JSON string response"""
try:
# The content comes as a JSON string, parse it
if isinstance(raw_data, str):
parsed_data = json.loads(raw_data)
if isinstance(parsed_data, list):
return parsed_data
elif isinstance(parsed_data, dict) and "challenges" in parsed_data:
return parsed_data["challenges"]
except:
pass
return []
def convert_topcoder_challenge(self, tc_data: Dict) -> Challenge:
"""Convert real Topcoder challenge data to Challenge object"""
# Debug print to see actual structure
# print(f"Converting challenge: {json.dumps(tc_data, indent=2)[:500]}...")
"""Convert real Topcoder challenge data to Challenge object - FIXED VERSION"""
# Extract basic info - handle the actual Topcoder field names
challenge_id = str(tc_data.get('id') or tc_data.get('legacyId') or 'unknown')
# Extract real fields from Topcoder data structure
challenge_id = str(tc_data.get('id', 'unknown'))
# Topcoder uses 'name' field for challenge title
title = tc_data.get('name') or tc_data.get('title') or 'Topcoder Challenge'
title = tc_data.get('name', 'Topcoder Challenge')
# Description field
description = tc_data.get('description') or tc_data.get('overview', {}).get('description') or 'Challenge description not available'
# Description
description = tc_data.get('description', 'Challenge description not available')
# Extract technologies from skills array (this is the correct field)
# Extract technologies from skills array
technologies = []
skills = tc_data.get('skills', [])
for skill in skills:
if isinstance(skill, dict) and 'name' in skill:
technologies.append(skill['name'])
# Also check for direct technologies field
if 'technologies' in tc_data:
tech_list = tc_data['technologies']
if isinstance(tech_list, list):
for tech in tech_list:
if isinstance(tech, dict) and 'name' in tech:
technologies.append(tech['name'])
elif isinstance(tech, str):
technologies.append(tech)
# Calculate total prize from prizeSets
total_prize = 0
prize_sets = tc_data.get('prizeSets', [])6 unmodified lines
prize = f"${total_prize:,}" if total_prize > 0 else "Merit-based"
# Map challenge type to difficulty
# Map challenge type to difficulty
challenge_type = tc_data.get('type', 'Unknown')
track = tc_data.get('track', 'Development')
type_id = tc_data.get('typeId', '')
# Topcoder difficulty mapping
difficulty_mapping = {
'First2Finish': 'Beginner',
'Code': 'Intermediate',
'Code': 'Intermediate',
'Assembly Competition': 'Advanced',
'UI Prototype Competition': 'Intermediate',
'Copilot Posting': 'Beginner'
'Copilot Posting': 'Beginner',
'Bug Hunt': 'Beginner',
'Test Suites': 'Intermediate'
}
difficulty = difficulty_mapping.get(challenge_type, 'Intermediate')
# Time estimate from duration or phases
# Time estimate
time_estimate = "Variable duration"
# Check if challenge is completed
# Check status and dates
status = tc_data.get('status', '')
if status == 'Completed':
time_estimate = "Recently completed"
elif 'endDate' in tc_data:
try:
end_date = tc_data['endDate']
# Could parse date and calculate if still active
time_estimate = "Check deadline"
except:
pass
elif status in ['Active', 'Draft']:
time_estimate = "Active challenge"
return Challenge(
id=challenge_id,
title=title,
description=description[:300] + "..." if len(description) > 300 else description,
technologies=technologies,
difficulty=difficulty,
difficulty=difficulty,
prize=prize,
time_estimate=time_estimate
)
async def fetch_real_challenges(self, limit: int = 20) -> List[Challenge]:
"""Fetch real challenges from Topcoder MCP"""
"""Fetch real challenges from Topcoder MCP - FIXED VERSION"""
if not await self.initialize_connection():
return []3 unmodified lines if not result:
return []
# Extract the content which contains the JSON string
content = result.get("content", "")
# π― THE FIX: Use structuredContent instead of content!
challenge_data_list = []
if isinstance(content, str) and content.strip():
challenge_data_list = self.extract_real_challenge_data(content)
challenges = []
for item in challenge_data_list:
if isinstance(item, dict):
try:
challenge = self.convert_topcoder_challenge(item)
challenges.append(challenge)
except Exception as e:
print(f"Error converting challenge: {e}")
continue
if challenges:
print(f"β
Successfully converted {len(challenges)} real Topcoder challenges")
return challenges
# Method 1: Use structuredContent (already parsed JSON)
if "structuredContent" in result:
structured = result["structuredContent"]
if isinstance(structured, dict) and "data" in structured:
challenge_data_list = structured["data"]
print(f"β
Found {len(challenge_data_list)} challenges in structuredContent")
return []
# Method 2: Fallback to parsing content[0]['text']
elif "content" in result and len(result["content"]) > 0:
content_item = result["content"][0]
if isinstance(content_item, dict) and content_item.get("type") == "text":
try:
text_content = content_item.get("text", "")
parsed_data = json.loads(text_content)
if "data" in parsed_data:
challenge_data_list = parsed_data["data"]
print(f"β
Found {len(challenge_data_list)} challenges in content text")
except json.JSONDecodeError:
pass
# Convert to Challenge objects
challenges = []
for item in challenge_data_list:
if isinstance(item, dict):
try:
challenge = self.convert_topcoder_challenge(item)
challenges.append(challenge)
except Exception as e:
print(f"Error converting challenge: {e}")
continue
print(f"π Successfully converted {len(challenges)} real Topcoder challenges!")
return challenges
def extract_technologies_from_query(self, query: str) -> List[str]:
"""Extract technology keywords from user query"""72 unmodified lines return min(score, 1.0), factors
async def get_personalized_recommendations(self, user_profile: UserProfile, query: str = "") -> Dict[str, Any]:
"""Get personalized recommendations - tries real MCP, falls back to enhanced mock"""
"""Get personalized recommendations using REAL Topcoder data - FIXED VERSION"""
start_time = datetime.now()
# Try to get real challenges first
real_challenges = await self.fetch_real_challenges(limit=30)
# Fetch REAL challenges
real_challenges = await self.fetch_real_challenges(limit=50)
if real_challenges:
challenges = real_challenges
data_source = "π₯ REAL Topcoder MCP Server"
data_source = "π₯ REAL Topcoder MCP Server (4,596+ challenges)"
print(f"π Using {len(challenges)} REAL Topcoder challenges!")
else:
# Fallback to enhanced mock data
# Fallback to mock data
challenges = self.mock_challenges
data_source = "Enhanced Mock Data (MCP unavailable)"
28 unmodified lines "top_match": f"{recommendations[0].compatibility_score:.0%}" if recommendations else "0%",
"technologies_detected": query_techs,
"session_active": bool(self.session_id),
"mcp_connected": self.is_connected
"mcp_connected": self.is_connected,
"topcoder_total": "4,596+ live challenges" if real_challenges else "Mock data"
}
}
app.py+73-66