From 5f358d329b7ef51f3d649832e1eceec6ab8dab6e Mon Sep 17 00:00:00 2001 From: Lars Date: Fri, 7 Nov 2025 13:28:41 +0100 Subject: [PATCH] =?UTF-8?q?app/core/type=5Fregistry.py=20hinzugef=C3=BCgt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/core/type_registry.py | 79 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 app/core/type_registry.py diff --git a/app/core/type_registry.py b/app/core/type_registry.py new file mode 100644 index 0000000..f959e7e --- /dev/null +++ b/app/core/type_registry.py @@ -0,0 +1,79 @@ +# -*- coding: utf-8 -*- +""" +app/core/type_registry.py — mindnet · WP-03 (Version 1.0.0) + +Zweck: + - Lädt eine optionale Typen-Registry aus config/types.yaml|json. + - Liefert pro `type` eine Konfiguration (z. B. Chunk-Profile, Standard-Edges, + Retriever-Gewichtungen). Wird in import/chunk/edges integriert. + +Verhalten: + - Fehlt die Datei oder der Typ → es werden Defaults genutzt. + - Registry wird gecacht (lazy-load). +""" + +__version__ = "1.0.0" + +import os +import json +from typing import Any, Dict +try: + import yaml # type: ignore +except Exception: + yaml = None # optional + +_CACHE: Dict[str, Any] = {} + +DEFAULT_REG = { + "version": "1.0", + "types": { + "concept": { + "chunk_profile": "medium", + "edge_defaults": ["references", "related_to"], + "retriever_weight": 1.0 + }, + "task": { + "chunk_profile": "short", + "edge_defaults": ["depends_on", "belongs_to"], + "retriever_weight": 0.8 + }, + "experience": { + "chunk_profile": "medium", + "edge_defaults": ["derived_from"], + "retriever_weight": 0.9 + } + } +} + +def load_type_registry(path: str = "config/types.yaml", silent: bool = False) -> Dict[str, Any]: + global _CACHE + if _CACHE: + return _CACHE + + def load_yaml(p: str) -> Dict[str, Any]: + if yaml is None: + return {} + with open(p, "r", encoding="utf-8") as f: + return yaml.safe_load(f) or {} + + def load_json(p: str) -> Dict[str, Any]: + with open(p, "r", encoding="utf-8") as f: + return json.load(f) or {} + + reg: Dict[str, Any] = {} + if os.path.exists(path): + try: + if path.endswith(".yaml") or path.endswith(".yml"): + reg = load_yaml(path) + elif path.endswith(".json"): + reg = load_json(path) + except Exception as e: + if not silent: + print(f"[type_registry] WARN: failed to load {path}: {e}") + + if not reg: + # Fallback + reg = DEFAULT_REG + + _CACHE = reg + return _CACHE