Implement basic MTS logic for 10 flowcharts

This commit is contained in:
Dualmind-Assistant
2026-04-21 13:24:50 +00:00
parent 3531cd3299
commit 5cd2b99831
+80 -14
View File
@@ -68,7 +68,27 @@ def load_questions(language: str) -> QuestionsResponse:
return QuestionsResponse(**raw) 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( app.add_middleware(
CORSMiddleware, CORSMiddleware,
@@ -95,30 +115,76 @@ async def get_questions(language: str) -> QuestionsResponse:
async def create_session(payload: SessionCreate) -> MtsPreparation: async def create_session(payload: SessionCreate) -> MtsPreparation:
"""Create a triage preparation proposal from the given answers. """Create a triage preparation proposal from the given answers.
For now this implements a very small ruleset as a walking skeleton Aktuell wird bewusst nur ein kleiner, aber sinnvoller Ausschnitt
that we can refine step by step. 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" 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 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] = [] 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 priority: Optional[str] = None
for ans in payload.answers: high_risk_flowcharts = {
if ans.question_code == "chief_complaint" and ans.value == "chest_pain": "CHEST_PAIN",
flowchart = "CHEST_PAIN" "SHORTNESS_OF_BREATH",
if ans.question_code == "breathlessness" and ans.value is True: "COLLAPSED_ADULT",
red_flags.append("breathlessness") "OVERDOSE_POISONING",
if ( }
ans.question_code == "pain_intensity"
and isinstance(ans.value, int)
and ans.value >= 8
):
red_flags.append("severe_pain")
if "breathlessness" in red_flags: if "breathlessness" in red_flags:
# Starke Atemnot ist immer hoch dringlich
priority = "RED_OR_ORANGE" 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( return MtsPreparation(
session_id=session_id, session_id=session_id,