Render structured task JSON into readable final reports
Browse files
backend/services/orchestrator_service.py
CHANGED
|
@@ -2,12 +2,27 @@ from services.supabase_service import supabase
|
|
| 2 |
from agents.agent_factory import AgentFactory
|
| 3 |
import json
|
| 4 |
import logging
|
|
|
|
| 5 |
from services.config import settings
|
| 6 |
from services.agent_runner_service import AgentRunnerService
|
| 7 |
from services.output_quality import clean_report_text, dedupe_lines, filter_report_sections, report_text_from_output
|
| 8 |
|
| 9 |
logger = logging.getLogger("uvicorn")
|
| 10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
def _humanize_key(key: str) -> str:
|
| 12 |
return key.replace("_", " ").replace("-", " ").strip().title()
|
| 13 |
|
|
@@ -36,6 +51,8 @@ def _format_value_for_report(value, level: int = 0) -> list[str]:
|
|
| 36 |
if isinstance(value, dict):
|
| 37 |
lines: list[str] = []
|
| 38 |
for key, item in value.items():
|
|
|
|
|
|
|
| 39 |
title = _humanize_key(str(key))
|
| 40 |
if isinstance(item, dict):
|
| 41 |
lines.append(f"{title}:")
|
|
@@ -49,6 +66,24 @@ def _format_value_for_report(value, level: int = 0) -> list[str]:
|
|
| 49 |
|
| 50 |
return [str(value)]
|
| 51 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
def _format_output_for_report(output_data) -> str:
|
| 53 |
if not output_data:
|
| 54 |
return "No approved output was saved for this task."
|
|
@@ -64,6 +99,9 @@ def _format_output_for_report(output_data) -> str:
|
|
| 64 |
primary = output_data
|
| 65 |
|
| 66 |
if isinstance(primary, str):
|
|
|
|
|
|
|
|
|
|
| 67 |
return clean_report_text(dedupe_lines(primary))
|
| 68 |
|
| 69 |
return clean_report_text(dedupe_lines("\n".join(_format_value_for_report(primary))))
|
|
|
|
| 2 |
from agents.agent_factory import AgentFactory
|
| 3 |
import json
|
| 4 |
import logging
|
| 5 |
+
import re
|
| 6 |
from services.config import settings
|
| 7 |
from services.agent_runner_service import AgentRunnerService
|
| 8 |
from services.output_quality import clean_report_text, dedupe_lines, filter_report_sections, report_text_from_output
|
| 9 |
|
| 10 |
logger = logging.getLogger("uvicorn")
|
| 11 |
|
| 12 |
+
NOISY_REPORT_KEYS = {
|
| 13 |
+
"raw_text",
|
| 14 |
+
"sampleBackendCode",
|
| 15 |
+
"sampleUploadSnippet",
|
| 16 |
+
"sampleSearchEndpoint",
|
| 17 |
+
"sampleRedisCartHelper",
|
| 18 |
+
"sampleWebhookHandler",
|
| 19 |
+
"sampleStateMachine",
|
| 20 |
+
"repositoryStructure",
|
| 21 |
+
"wireframes",
|
| 22 |
+
"dataModel",
|
| 23 |
+
"userStories",
|
| 24 |
+
}
|
| 25 |
+
|
| 26 |
def _humanize_key(key: str) -> str:
|
| 27 |
return key.replace("_", " ").replace("-", " ").strip().title()
|
| 28 |
|
|
|
|
| 51 |
if isinstance(value, dict):
|
| 52 |
lines: list[str] = []
|
| 53 |
for key, item in value.items():
|
| 54 |
+
if str(key) in NOISY_REPORT_KEYS:
|
| 55 |
+
continue
|
| 56 |
title = _humanize_key(str(key))
|
| 57 |
if isinstance(item, dict):
|
| 58 |
lines.append(f"{title}:")
|
|
|
|
| 66 |
|
| 67 |
return [str(value)]
|
| 68 |
|
| 69 |
+
|
| 70 |
+
def _extract_json_payload(text: str):
|
| 71 |
+
stripped = text.strip()
|
| 72 |
+
if stripped.startswith("```"):
|
| 73 |
+
stripped = stripped.strip("`")
|
| 74 |
+
if stripped.lower().startswith("json"):
|
| 75 |
+
stripped = stripped[4:].strip()
|
| 76 |
+
try:
|
| 77 |
+
return json.loads(stripped)
|
| 78 |
+
except Exception:
|
| 79 |
+
match = re.search(r"```json\s*(.*?)\s*```", text, re.IGNORECASE | re.DOTALL)
|
| 80 |
+
if match:
|
| 81 |
+
try:
|
| 82 |
+
return json.loads(match.group(1))
|
| 83 |
+
except Exception:
|
| 84 |
+
return None
|
| 85 |
+
return None
|
| 86 |
+
|
| 87 |
def _format_output_for_report(output_data) -> str:
|
| 88 |
if not output_data:
|
| 89 |
return "No approved output was saved for this task."
|
|
|
|
| 99 |
primary = output_data
|
| 100 |
|
| 101 |
if isinstance(primary, str):
|
| 102 |
+
parsed = _extract_json_payload(primary)
|
| 103 |
+
if parsed is not None:
|
| 104 |
+
return clean_report_text(dedupe_lines("\n".join(_format_value_for_report(parsed))))
|
| 105 |
return clean_report_text(dedupe_lines(primary))
|
| 106 |
|
| 107 |
return clean_report_text(dedupe_lines("\n".join(_format_value_for_report(primary))))
|