Spaces:
Sleeping
Sleeping
Upload folder using huggingface_hub
Browse files- core/a2a_helpers.py +43 -8
- core/dynamic_a2a_manager.py +7 -8
core/a2a_helpers.py
CHANGED
|
@@ -25,11 +25,36 @@ def parse_a2a_response(result: Any, output_class: type[BaseModel]) -> Optional[B
|
|
| 25 |
"""
|
| 26 |
# Handle string responses
|
| 27 |
if isinstance(result, str):
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
# Handle direct output class instance
|
| 35 |
if isinstance(result, output_class):
|
|
@@ -42,14 +67,13 @@ def parse_a2a_response(result: Any, output_class: type[BaseModel]) -> Optional[B
|
|
| 42 |
try:
|
| 43 |
return output_class(**result)
|
| 44 |
except Exception as e:
|
| 45 |
-
|
| 46 |
-
|
| 47 |
|
| 48 |
# Handle A2A wrapper format
|
| 49 |
if 'status' in result and 'message' in result.get('status', {}):
|
| 50 |
return _extract_from_wrapper(result, output_class)
|
| 51 |
|
| 52 |
-
print(f"Unable to parse result of type {type(result)}")
|
| 53 |
return None
|
| 54 |
|
| 55 |
|
|
@@ -68,8 +92,19 @@ def _extract_from_wrapper(wrapped_result: Dict, output_class: type[BaseModel]) -
|
|
| 68 |
|
| 69 |
def extract_task_id(result: Any) -> Optional[str]:
|
| 70 |
"""Extract task_id from A2A response if present."""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
if isinstance(result, dict) and 'task_id' in result:
|
| 72 |
return result['task_id']
|
|
|
|
| 73 |
return None
|
| 74 |
|
| 75 |
|
|
|
|
| 25 |
"""
|
| 26 |
# Handle string responses
|
| 27 |
if isinstance(result, str):
|
| 28 |
+
# First check if it's the Status/Message/TaskId format
|
| 29 |
+
if "Status:" in result and "Message:" in result:
|
| 30 |
+
try:
|
| 31 |
+
# Extract the JSON from the Message: line
|
| 32 |
+
lines = result.strip().split('\n')
|
| 33 |
+
for i, line in enumerate(lines):
|
| 34 |
+
if line.startswith("Message:"):
|
| 35 |
+
# Get the JSON part after "Message: "
|
| 36 |
+
json_str = line[8:].strip()
|
| 37 |
+
# If the JSON continues on multiple lines, collect them
|
| 38 |
+
if i + 1 < len(lines) and not lines[i + 1].startswith(("TaskId:", "Timestamp:")):
|
| 39 |
+
# Continue collecting lines until we hit TaskId or Timestamp
|
| 40 |
+
json_lines = [json_str]
|
| 41 |
+
for j in range(i + 1, len(lines)):
|
| 42 |
+
if lines[j].startswith(("TaskId:", "Timestamp:")):
|
| 43 |
+
break
|
| 44 |
+
json_lines.append(lines[j])
|
| 45 |
+
json_str = '\n'.join(json_lines)
|
| 46 |
+
result = json.loads(json_str)
|
| 47 |
+
break
|
| 48 |
+
except Exception as e:
|
| 49 |
+
# Silently fail - the debug output in get_pick will show the issue
|
| 50 |
+
return None
|
| 51 |
+
else:
|
| 52 |
+
# Try parsing as plain JSON
|
| 53 |
+
try:
|
| 54 |
+
result = json.loads(result)
|
| 55 |
+
except Exception as e:
|
| 56 |
+
# Not JSON format
|
| 57 |
+
return None
|
| 58 |
|
| 59 |
# Handle direct output class instance
|
| 60 |
if isinstance(result, output_class):
|
|
|
|
| 67 |
try:
|
| 68 |
return output_class(**result)
|
| 69 |
except Exception as e:
|
| 70 |
+
# Conversion failed
|
| 71 |
+
pass
|
| 72 |
|
| 73 |
# Handle A2A wrapper format
|
| 74 |
if 'status' in result and 'message' in result.get('status', {}):
|
| 75 |
return _extract_from_wrapper(result, output_class)
|
| 76 |
|
|
|
|
| 77 |
return None
|
| 78 |
|
| 79 |
|
|
|
|
| 92 |
|
| 93 |
def extract_task_id(result: Any) -> Optional[str]:
|
| 94 |
"""Extract task_id from A2A response if present."""
|
| 95 |
+
# Handle string format with TaskId: line
|
| 96 |
+
if isinstance(result, str) and "TaskId:" in result:
|
| 97 |
+
lines = result.strip().split('\n')
|
| 98 |
+
for line in lines:
|
| 99 |
+
if line.startswith("TaskId:"):
|
| 100 |
+
task_id = line[7:].strip()
|
| 101 |
+
if task_id and task_id != "None":
|
| 102 |
+
return task_id
|
| 103 |
+
|
| 104 |
+
# Handle dict format
|
| 105 |
if isinstance(result, dict) and 'task_id' in result:
|
| 106 |
return result['task_id']
|
| 107 |
+
|
| 108 |
return None
|
| 109 |
|
| 110 |
|
core/dynamic_a2a_manager.py
CHANGED
|
@@ -273,7 +273,6 @@ BE LOUD! BE PROUD! BE UNFORGETTABLE! π―""",
|
|
| 273 |
previous_picks: List[str], round_num: int = 1) -> Optional[A2AOutput]:
|
| 274 |
"""Get a pick from an A2A agent."""
|
| 275 |
if team_num not in self.agent_tools:
|
| 276 |
-
print(f"β Team {team_num} not found in agent_tools: {list(self.agent_tools.keys())}")
|
| 277 |
return None
|
| 278 |
|
| 279 |
# Build the prompt with essential info
|
|
@@ -292,22 +291,22 @@ Remember your ENEMIES and CRUSH their dreams! Use emojis to emphasize your DOMIN
|
|
| 292 |
try:
|
| 293 |
# Use task_id if we have one for this agent
|
| 294 |
task_id = self.task_ids.get(team_num)
|
| 295 |
-
print(f"π Calling A2A agent for Team {team_num} (task_id: {task_id})")
|
| 296 |
result = await self.agent_tools[team_num](prompt, task_id=task_id)
|
| 297 |
-
print(f"π₯ Raw result type: {type(result)}")
|
| 298 |
|
| 299 |
# Extract and store task_id
|
| 300 |
-
|
| 301 |
-
if
|
| 302 |
-
self.task_ids[team_num] =
|
| 303 |
|
| 304 |
# Parse the response
|
| 305 |
output = parse_a2a_response(result, A2AOutput)
|
| 306 |
if output:
|
| 307 |
-
print(f"β
|
| 308 |
else:
|
| 309 |
print(f"β Failed to parse response from Team {team_num}")
|
| 310 |
-
|
|
|
|
|
|
|
| 311 |
return output
|
| 312 |
|
| 313 |
except Exception as e:
|
|
|
|
| 273 |
previous_picks: List[str], round_num: int = 1) -> Optional[A2AOutput]:
|
| 274 |
"""Get a pick from an A2A agent."""
|
| 275 |
if team_num not in self.agent_tools:
|
|
|
|
| 276 |
return None
|
| 277 |
|
| 278 |
# Build the prompt with essential info
|
|
|
|
| 291 |
try:
|
| 292 |
# Use task_id if we have one for this agent
|
| 293 |
task_id = self.task_ids.get(team_num)
|
|
|
|
| 294 |
result = await self.agent_tools[team_num](prompt, task_id=task_id)
|
|
|
|
| 295 |
|
| 296 |
# Extract and store task_id
|
| 297 |
+
new_task_id = extract_task_id(result)
|
| 298 |
+
if new_task_id:
|
| 299 |
+
self.task_ids[team_num] = new_task_id
|
| 300 |
|
| 301 |
# Parse the response
|
| 302 |
output = parse_a2a_response(result, A2AOutput)
|
| 303 |
if output:
|
| 304 |
+
print(f"β
Team {team_num} pick: {output.player_name}")
|
| 305 |
else:
|
| 306 |
print(f"β Failed to parse response from Team {team_num}")
|
| 307 |
+
if isinstance(result, str) and len(result) > 100:
|
| 308 |
+
# Show compact preview for debugging
|
| 309 |
+
print(f" Response format issue - check a2a_helpers.py parsing")
|
| 310 |
return output
|
| 311 |
|
| 312 |
except Exception as e:
|