|
12 | 12 | from workers.planner.db import get_all_mcp_descriptions |
13 | 13 | from workers.tasks import calculate_next_run |
14 | 14 | from zoneinfo import ZoneInfo, ZoneInfoNotFoundError |
| 15 | +from json_extractor import JsonExtractor |
15 | 16 |
|
16 | 17 | router = APIRouter( |
17 | 18 | prefix="/agents", |
@@ -258,24 +259,19 @@ async def generate_plan( |
258 | 259 | user_prompt = f"Please create a plan for the following goal: {request.prompt}" |
259 | 260 | messages = [{'role': 'user', 'content': user_prompt}] |
260 | 261 |
|
261 | | - final_response_str = "" |
262 | 262 | for chunk in agent.run(messages=messages): |
263 | 263 | if isinstance(chunk, list) and chunk: |
264 | 264 | last_message = chunk[-1] |
265 | 265 | if last_message.get("role") == "assistant" and isinstance(last_message.get("content"), str): |
266 | 266 | content = last_message["content"] |
267 | 267 |
|
268 | | - |
269 | | - # Extract JSON from markdown code block if present |
270 | | - match = re.search(r'```json\n(.*?)\n```', content, re.DOTALL) |
271 | | - final_response_str = match.group(1) if match else content |
272 | | - |
| 268 | + plan_data = JsonExtractor.extract_valid_json(content) |
| 269 | + |
273 | 270 | print(f"[INFO] Received chunk from planner agent: {content}") |
274 | | - print(f"[INFO] Final response from planner agent: {final_response_str}") |
275 | | - if not final_response_str: |
| 271 | + print(f"[INFO] Final response from planner agent: {plan_data}") |
| 272 | + if not plan_data: |
276 | 273 | raise HTTPException(status_code=500, detail="Planner agent returned an empty response.") |
277 | 274 |
|
278 | | - plan_data = json.loads(final_response_str) |
279 | 275 | return {"description": plan_data.get("description"), "plan": plan_data.get("plan", [])} |
280 | 276 | except Exception as e: |
281 | 277 | import traceback |
|
0 commit comments