from __future__ import annotations from .shared import * def _normalize_review_content(raw: dict[str, Any] | None) -> dict[str, Any]: payload = dict(raw or {}) summary = str(payload.get("summary") or "").strip() goals = payload.get("goals") if not isinstance(goals, list): goals = [] what_went_well = payload.get("what_went_well") if not isinstance(what_went_well, list): what_went_well = [] root_causes = payload.get("root_causes") if not isinstance(root_causes, list): root_causes = [] improvements = payload.get("improvements") if not isinstance(improvements, list): improvements = [] normalized_improvements: list[dict[str, Any]] = [] def _optional_int(value: Any) -> int | None: if value in (None, ""): return None try: return int(value) except (TypeError, ValueError): return None for idx, item in enumerate(improvements): if isinstance(item, str): title = item.strip() if not title: continue normalized_improvements.append( { "id": f"imp-{idx+1}", "title": title, "status": "pending", "owner_id": None, "due_date": None, "task_id": None, } ) continue if not isinstance(item, dict): continue title = str(item.get("title") or "").strip() if not title: continue normalized_improvements.append( { "id": str(item.get("id") or f"imp-{idx+1}"), "title": title, "status": str(item.get("status") or "pending"), "owner_id": _optional_int(item.get("owner_id")), "due_date": item.get("due_date"), "task_id": _optional_int(item.get("task_id")), "description": str(item.get("description") or "").strip() or None, } ) return { "summary": summary, "goals": [str(x).strip() for x in goals if str(x).strip()], "what_went_well": [str(x).strip() for x in what_went_well if str(x).strip()], "root_causes": [str(x).strip() for x in root_causes if str(x).strip()], "improvements": normalized_improvements, "last_updated_at": now_cn_aware().isoformat(), }