"""Gap analyzer node with context-bus aware clarifying questions.""" from __future__ import annotations from typing import Any, Dict, List from langchain_core.messages import AIMessage from agents.document_gap_analyzer import _collect_structured_gaps from core.context.context_bus import filter_clarifying_questions from schemas import AgentState def _gap_to_question(gap: str) -> str | None: g = (gap or "").lower() if "pkd" in g: return "Podaj główny kod PKD działalności objętej projektem." if "nip" in g or "gus" in g or "profilu firmy" in g: return "Podaj NIP firmy, aby pobrać dane z rejestru GUS." if "opis" in g or "cel" in g: return "Opisz cel projektu i planowane wydatki w 2–3 zdaniach." if "finans" in g or "przychod" in g: return "Podaj przychody firmy z ostatniego roku obrotowego." if "wojew" in g or "region" in g: return "Uzupełnij województwo realizacji projektu." return f"Uzupełnij brakującą informację: {gap}" def build_clarifying_questions(gaps: List[str], profile: dict | None) -> List[str]: profile = profile or {} company = dict(profile) if hasattr(profile, "model_dump"): company = profile.model_dump() elif hasattr(profile, "dict"): company = profile.dict() questions: List[str] = [] for gap in gaps or []: q = _gap_to_question(gap) if q and q not in questions: questions.append(q) return filter_clarifying_questions(questions, company, gaps) def gap_analyzer_node(state: AgentState) -> Dict[str, Any]: gaps = _collect_structured_gaps(state) profile_dict: dict = {} if state.profile: if hasattr(state.profile, "model_dump"): profile_dict = state.profile.model_dump() elif hasattr(state.profile, "dict"): profile_dict = state.profile.dict() else: profile_dict = dict(state.profile) clarifying = build_clarifying_questions(gaps, profile_dict) bb = dict(state.blackboard or {}) bb["data_gaps"] = gaps bb["gap_analysis_done"] = True bb["clarifying_questions"] = clarifying summary = "\n".join(f"- {g}" for g in gaps) if gaps else "Brak krytycznych braków danych." return { "messages": [ AIMessage( content=f"[GAP ANALYZER] Analiza braków zakończona.\n{summary}" ) ], "blackboard": bb, "current_agent": "supervisor", }