mindnet/app/core/note_payload.py

236 lines
7.3 KiB
Python
Raw Normal View History

2025-11-16 21:20:10 +01:00
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
2025-11-17 12:21:59 +01:00
Modul: app/core/note_payload.py
2025-12-10 22:17:40 +01:00
Version: 2.1.0 (WP-11 Update: Aliases support)
2025-11-17 12:21:59 +01:00
Zweck
-----
Erzeugt ein robustes Note-Payload. Werte wie `retriever_weight`, `chunk_profile`
und `edge_defaults` werden in folgender Priorität bestimmt:
1) Frontmatter (Note)
2) Typ-Registry (config/types.yaml: types.<type>.*)
3) Registry-Defaults (config/types.yaml: defaults.*)
4) ENV-Defaults (MINDNET_DEFAULT_RETRIEVER_WEIGHT / MINDNET_DEFAULT_CHUNK_PROFILE)
2025-11-16 21:20:10 +01:00
"""
2025-12-04 12:37:31 +01:00
2025-11-09 10:11:19 +01:00
from __future__ import annotations
2025-11-17 12:21:59 +01:00
from typing import Any, Dict, Tuple, Optional
2025-12-04 12:37:31 +01:00
import os
import json
import pathlib
2025-11-17 12:21:59 +01:00
try:
2025-12-04 12:37:31 +01:00
import yaml # type: ignore
2025-11-17 12:21:59 +01:00
except Exception:
yaml = None
2025-12-04 12:37:31 +01:00
# ---------------------------------------------------------------------------
# Helper
# ---------------------------------------------------------------------------
2025-11-17 12:21:59 +01:00
def _as_dict(x) -> Dict[str, Any]:
2025-12-04 12:37:31 +01:00
"""Versucht, ein ParsedMarkdown-ähnliches Objekt in ein Dict zu überführen."""
2025-11-17 12:21:59 +01:00
if isinstance(x, dict):
return dict(x)
2025-12-04 12:37:31 +01:00
2025-12-03 14:27:09 +01:00
out: Dict[str, Any] = {}
2025-12-04 12:37:31 +01:00
# bekannte Attribute übernehmen, sofern vorhanden
for attr in (
"frontmatter",
"body",
"id",
"note_id",
"title",
"path",
"tags",
"type",
"created",
"modified",
"date",
):
2025-12-03 14:27:09 +01:00
if hasattr(x, attr):
val = getattr(x, attr)
if val is not None:
out[attr] = val
2025-12-04 12:37:31 +01:00
2025-12-03 14:27:09 +01:00
# Fallback: wenn immer noch leer, raw speichern
if not out:
out["raw"] = str(x)
2025-12-04 12:37:31 +01:00
2025-12-03 14:27:09 +01:00
return out
2025-11-09 10:11:19 +01:00
2025-12-04 12:37:31 +01:00
2025-11-17 12:21:59 +01:00
def _pick_args(*args, **kwargs) -> Tuple[Optional[str], Optional[dict]]:
2025-12-04 12:37:31 +01:00
"""Extrahiert optionale Zusatzargumente wie path und types_cfg."""
2025-11-17 12:21:59 +01:00
path = kwargs.get("path") or (args[0] if args else None)
types_cfg = kwargs.get("types_cfg") or kwargs.get("types") or None
return path, types_cfg
2025-11-16 21:20:10 +01:00
2025-12-04 12:37:31 +01:00
2025-11-17 12:21:59 +01:00
def _env_float(name: str, default: float) -> float:
2025-12-04 12:37:31 +01:00
"""Liest einen Float-Wert aus der Umgebung, mit robustem Fallback."""
2025-11-17 12:21:59 +01:00
try:
return float(os.environ.get(name, default))
except Exception:
return default
2025-12-04 12:37:31 +01:00
2025-11-17 12:21:59 +01:00
def _ensure_list(x) -> list:
2025-12-04 12:37:31 +01:00
"""Garantiert eine String-Liste."""
if x is None:
return []
if isinstance(x, list):
return [str(i) for i in x]
if isinstance(x, (set, tuple)):
return [str(i) for i in x]
2025-11-17 12:21:59 +01:00
return [str(x)]
2025-12-04 12:37:31 +01:00
# ---------------------------------------------------------------------------
# Type-Registry laden
# ---------------------------------------------------------------------------
2025-11-17 12:21:59 +01:00
def _load_types_config(explicit_cfg: Optional[dict] = None) -> dict:
2025-12-04 12:37:31 +01:00
"""Lädt die Type-Registry aus YAML/JSON oder nutzt ein explizit übergebenes Dict."""
2025-11-17 12:21:59 +01:00
if explicit_cfg and isinstance(explicit_cfg, dict):
return explicit_cfg
2025-12-04 12:37:31 +01:00
2025-11-17 12:21:59 +01:00
path = os.getenv("MINDNET_TYPES_FILE") or "./config/types.yaml"
if not os.path.isfile(path) or yaml is None:
return {}
2025-12-04 12:37:31 +01:00
2025-11-09 09:15:48 +01:00
try:
2025-11-17 12:21:59 +01:00
with open(path, "r", encoding="utf-8") as f:
data = yaml.safe_load(f) or {}
return data if isinstance(data, dict) else {}
2025-11-09 09:15:48 +01:00
except Exception:
2025-11-16 21:20:10 +01:00
return {}
2025-11-08 22:06:21 +01:00
2025-12-04 12:37:31 +01:00
2025-11-17 12:21:59 +01:00
def _cfg_for_type(note_type: str, reg: dict) -> dict:
2025-12-04 12:37:31 +01:00
"""Liefert die Konfiguration für einen konkreten Notiztyp aus der Registry."""
2025-11-17 12:21:59 +01:00
if not isinstance(reg, dict):
return {}
types = reg.get("types") if isinstance(reg.get("types"), dict) else reg
return types.get(note_type, {}) if isinstance(types, dict) else {}
2025-12-04 12:37:31 +01:00
2025-11-17 12:21:59 +01:00
def _cfg_defaults(reg: dict) -> dict:
2025-12-04 12:37:31 +01:00
"""Liefert den Default-Block aus der Registry (defaults/global)."""
2025-11-17 12:21:59 +01:00
if not isinstance(reg, dict):
return {}
for key in ("defaults", "default", "global"):
v = reg.get(key)
if isinstance(v, dict):
return v
2025-11-16 21:20:10 +01:00
return {}
2025-11-09 09:15:48 +01:00
2025-12-04 12:37:31 +01:00
# ---------------------------------------------------------------------------
# Haupt-API
# ---------------------------------------------------------------------------
2025-11-17 12:21:59 +01:00
def make_note_payload(note: Any, *args, **kwargs) -> Dict[str, Any]:
2025-12-04 12:37:31 +01:00
"""
Baut das Note-Payload für mindnet_notes auf.
Erwartete Felder im Payload:
- note_id: stabile ID aus Frontmatter (id) oder Note-Objekt
- title: Titel der Notiz
- type: Notiztyp (z. B. concept, project, journal, ...)
- path: Pfad im Vault
- retriever_weight: effektives Gewicht für den Retriever
- chunk_profile: Profil für Chunking (short|medium|long|default|...)
- edge_defaults: Liste von Kanten-Typen, die als Defaults gelten
2025-12-10 22:17:40 +01:00
- aliases: Liste von Synonymen (WP-11)
2025-12-04 12:37:31 +01:00
"""
2025-11-17 12:21:59 +01:00
n = _as_dict(note)
path_arg, types_cfg_explicit = _pick_args(*args, **kwargs)
reg = _load_types_config(types_cfg_explicit)
fm = n.get("frontmatter") or {}
fm_type = fm.get("type") or n.get("type") or "concept"
note_type = str(fm_type)
cfg_type = _cfg_for_type(note_type, reg)
2025-12-04 12:37:31 +01:00
cfg_def = _cfg_defaults(reg)
2025-11-17 12:21:59 +01:00
2025-12-04 12:37:31 +01:00
# --- retriever_weight: Frontmatter > Typ-Config > Registry-Defaults > ENV ---
2025-11-17 12:21:59 +01:00
default_rw = _env_float("MINDNET_DEFAULT_RETRIEVER_WEIGHT", 1.0)
retriever_weight = fm.get("retriever_weight")
if retriever_weight is None:
2025-12-04 12:37:31 +01:00
retriever_weight = cfg_type.get(
"retriever_weight",
cfg_def.get("retriever_weight", default_rw),
)
2025-11-09 10:11:19 +01:00
try:
2025-11-17 12:21:59 +01:00
retriever_weight = float(retriever_weight)
2025-11-09 10:11:19 +01:00
except Exception:
2025-11-17 12:21:59 +01:00
retriever_weight = default_rw
2025-12-04 12:37:31 +01:00
# --- chunk_profile: Frontmatter > Typ-Config > Registry-Defaults > ENV ---
2025-11-17 12:21:59 +01:00
chunk_profile = fm.get("chunk_profile")
if chunk_profile is None:
2025-12-04 12:37:31 +01:00
chunk_profile = cfg_type.get(
"chunk_profile",
cfg_def.get(
"chunk_profile",
os.environ.get("MINDNET_DEFAULT_CHUNK_PROFILE", "medium"),
),
)
2025-11-17 12:21:59 +01:00
if not isinstance(chunk_profile, str):
chunk_profile = "medium"
2025-12-04 12:37:31 +01:00
# --- edge_defaults: Frontmatter > Typ-Config > Registry-Defaults ---
2025-11-17 12:21:59 +01:00
edge_defaults = fm.get("edge_defaults")
if edge_defaults is None:
2025-12-04 12:37:31 +01:00
edge_defaults = cfg_type.get(
"edge_defaults",
cfg_def.get("edge_defaults", []),
)
2025-11-17 12:21:59 +01:00
edge_defaults = _ensure_list(edge_defaults)
2025-12-04 12:37:31 +01:00
# --- Basis-Metadaten (IDs, Titel, Pfad) ---
2025-11-17 12:21:59 +01:00
note_id = n.get("note_id") or n.get("id") or fm.get("id")
title = n.get("title") or fm.get("title") or ""
path = n.get("path") or path_arg
if isinstance(path, pathlib.Path):
path = str(path)
2025-12-04 12:37:31 +01:00
payload: Dict[str, Any] = {
2025-11-09 10:11:19 +01:00
"note_id": note_id,
2025-11-16 21:20:10 +01:00
"title": title,
2025-11-17 12:21:59 +01:00
"type": note_type,
"path": path or "",
"retriever_weight": retriever_weight,
2025-11-09 10:11:19 +01:00
"chunk_profile": chunk_profile,
2025-11-17 12:21:59 +01:00
"edge_defaults": edge_defaults,
2025-11-09 09:15:48 +01:00
}
2025-11-08 22:06:21 +01:00
2025-12-04 12:37:31 +01:00
# Tags / Keywords übernehmen
2025-11-17 12:21:59 +01:00
tags = fm.get("tags") or fm.get("keywords") or n.get("tags")
2025-12-04 12:37:31 +01:00
if tags:
payload["tags"] = _ensure_list(tags)
2025-12-10 22:17:40 +01:00
# WP-11: Aliases übernehmen (für Discovery Service)
aliases = fm.get("aliases")
if aliases:
payload["aliases"] = _ensure_list(aliases)
2025-12-04 12:37:31 +01:00
# Zeitliche Metadaten (sofern vorhanden)
for k in ("created", "modified", "date"):
2025-11-17 12:21:59 +01:00
v = fm.get(k) or n.get(k)
2025-12-04 12:37:31 +01:00
if v:
payload[k] = str(v)
2025-12-10 22:17:40 +01:00
# Fulltext (Fallback, falls body im Input)
if "body" in n and n["body"]:
payload["fulltext"] = str(n["body"])
2025-11-16 21:20:10 +01:00
2025-11-17 12:21:59 +01:00
# JSON-Roundtrip zur harten Validierung (ASCII beibehalten)
json.loads(json.dumps(payload, ensure_ascii=False))
2025-12-04 12:11:51 +01:00
2025-12-10 22:17:40 +01:00
return payload