diff --git a/backend/app/main.py b/backend/app/main.py index a3f111a..f5ed355 100644 --- a/backend/app/main.py +++ b/backend/app/main.py @@ -68,7 +68,27 @@ def load_questions(language: str) -> QuestionsResponse: return QuestionsResponse(**raw) -app = FastAPI(title="Triage-Fragen API", version="0.2.0") +def load_flowcharts_config() -> List[Dict[str, Any]]: + """Load flowchart mapping configuration. + + This maps chief-complaint codes (e.g. "chest_pain") to MTS + presenting flowchart codes (e.g. "CHEST_PAIN"). + """ + + file_path = MTS_CONFIG_DIR / "flowcharts.json" + if not file_path.exists(): + return [] + + with file_path.open("r", encoding="utf-8") as f: + raw: Dict[str, Any] = json.load(f) + + flowcharts = raw.get("flowcharts", []) + if not isinstance(flowcharts, list): + return [] + return flowcharts + + +app = FastAPI(title="Triage-Fragen API", version="0.3.0") app.add_middleware( CORSMiddleware, @@ -95,30 +115,76 @@ async def get_questions(language: str) -> QuestionsResponse: async def create_session(payload: SessionCreate) -> MtsPreparation: """Create a triage preparation proposal from the given answers. - For now this implements a very small ruleset as a walking skeleton - that we can refine step by step. + Aktuell wird bewusst nur ein kleiner, aber sinnvoller Ausschnitt + der Manchester-Triage-Logik abgebildet: + + - Zuordnung von "chief_complaint" zu einem der konfigurierten + MTS-Präsentations-Flowcharts (10 Pfade in mts-config/flowcharts.json). + - Ableitung einfacher Red-Flag-Indikatoren wie "severe_pain" aus + der Schmerzskala. + - Grobe Prioritätsstufe, insbesondere für Hochrisiko-Flowcharts + wie Brustschmerz, Atemnot, Kollaps und Intoxikation. """ session_id = "dummy-session-id" + # Antworten bequem zugreifbar machen + answers_by_code: Dict[str, Any] = {a.question_code: a.value for a in payload.answers} + + # 1) Flowchart aus chief_complaint bestimmen (Daten-getrieben aus mts-config) flowchart: Optional[str] = None + flowcharts = load_flowcharts_config() + + complaint_to_flowchart: Dict[str, str] = {} + for fc in flowcharts: + linked = fc.get("linked_chief_complaints") or [] + if not isinstance(linked, list): + continue + for cc in linked: + # Erstes Mapping gewinnt; spätere Einträge überschreiben nicht + complaint_to_flowchart.setdefault(cc, fc.get("code")) + + chief = answers_by_code.get("chief_complaint") + if isinstance(chief, str): + mapped = complaint_to_flowchart.get(chief) + if isinstance(mapped, str): + flowchart = mapped + + # 2) Red-Flag-Indikatoren aus vorhandenen Antworten ableiten red_flags: List[str] = [] + severe_pain = False + + # Atemnot: vorgesehen für spätere UI-Erweiterung + if answers_by_code.get("breathlessness") is True: + red_flags.append("breathlessness") + + pain_value = answers_by_code.get("pain_intensity") + if isinstance(pain_value, int): + if pain_value >= 8: + red_flags.append("severe_pain") + severe_pain = True + elif pain_value >= 5: + red_flags.append("moderate_pain") + + # 3) Grobe Priorität abhängig vom Flowchart und den Red Flags priority: Optional[str] = None - for ans in payload.answers: - if ans.question_code == "chief_complaint" and ans.value == "chest_pain": - flowchart = "CHEST_PAIN" - if ans.question_code == "breathlessness" and ans.value is True: - red_flags.append("breathlessness") - if ( - ans.question_code == "pain_intensity" - and isinstance(ans.value, int) - and ans.value >= 8 - ): - red_flags.append("severe_pain") + high_risk_flowcharts = { + "CHEST_PAIN", + "SHORTNESS_OF_BREATH", + "COLLAPSED_ADULT", + "OVERDOSE_POISONING", + } if "breathlessness" in red_flags: + # Starke Atemnot ist immer hoch dringlich priority = "RED_OR_ORANGE" + elif severe_pain and flowchart in high_risk_flowcharts: + # Starke Schmerzen bei Hochrisiko-Präsentation → hohe Dringlichkeit + priority = "RED_OR_ORANGE" + elif severe_pain: + # Starke Schmerzen bei anderen Flowcharts → mindestens erhöhte Dringlichkeit + priority = "YELLOW_OR_ORANGE" return MtsPreparation( session_id=session_id,