mindnet/app/core/note_payload.py

105 lines
3.8 KiB
Python
Raw Normal View History

2025-11-09 10:11:19 +01:00
# note_payload.py
from __future__ import annotations
2025-11-09 10:17:24 +01:00
from typing import Any, Dict, Optional, Tuple
import os, json, pathlib, yaml
2025-11-08 22:06:21 +01:00
2025-11-09 09:51:05 +01:00
def _as_dict(note: Any) -> Dict[str, Any]:
if isinstance(note, dict):
2025-11-09 10:11:19 +01:00
return note
d: Dict[str, Any] = {}
2025-11-09 10:17:24 +01:00
for attr in ("id","note_id","title","path","frontmatter","meta","metadata","type","created","modified","date","tags"):
2025-11-09 09:51:05 +01:00
if hasattr(note, attr):
2025-11-09 10:11:19 +01:00
d[attr] = getattr(note, attr)
2025-11-09 10:17:24 +01:00
# Normalisiere Frontmatter
fm = d.get("frontmatter") or d.get("meta") or d.get("metadata") or {}
d["frontmatter"] = fm if isinstance(fm, dict) else {}
2025-11-09 10:11:19 +01:00
return d
2025-11-09 10:17:24 +01:00
def _pick_args(*args, **kwargs) -> Tuple[Optional[str], Optional[Dict[str,Any]]]:
path = kwargs.get("path")
types_cfg = kwargs.get("types_config")
# legacy positional: (path, types_config) oder (types_config, ...)
for a in args:
if path is None and isinstance(a, (str, pathlib.Path)):
path = str(a)
if types_cfg is None and isinstance(a, dict):
types_cfg = a
return path, types_cfg
2025-11-09 10:11:19 +01:00
def _load_types_config(explicit: Optional[Dict[str, Any]] = None) -> Dict[str, Any]:
if isinstance(explicit, dict):
return explicit
for rel in ("config/config.yaml", "config/types.yaml"):
p = pathlib.Path(rel)
if p.exists():
with p.open("r", encoding="utf-8") as f:
data = yaml.safe_load(f) or {}
if isinstance(data, dict) and "types" in data and isinstance(data["types"], dict):
return data["types"]
return data if isinstance(data, dict) else {}
return {}
def _coalesce(*vals):
for v in vals:
if v is not None:
return v
return None
def _env_float(name: str, default: float) -> float:
2025-11-09 09:15:48 +01:00
try:
2025-11-09 10:11:19 +01:00
return float(os.environ.get(name, default))
2025-11-09 09:15:48 +01:00
except Exception:
2025-11-09 10:11:19 +01:00
return default
2025-11-08 22:06:21 +01:00
2025-11-09 10:11:19 +01:00
def _ensure_list(x) -> list:
2025-11-09 10:17:24 +01:00
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-09 10:11:19 +01:00
return [str(x)]
2025-11-09 09:51:05 +01:00
def make_note_payload(note: Any, *args, **kwargs) -> Dict[str, Any]:
2025-11-09 10:11:19 +01:00
n = _as_dict(note)
2025-11-09 10:17:24 +01:00
path_arg, types_cfg_explicit = _pick_args(*args, **kwargs)
types_cfg = _load_types_config(types_cfg_explicit)
2025-11-09 09:15:48 +01:00
2025-11-09 10:17:24 +01:00
fm = n.get("frontmatter") or {}
2025-11-09 10:11:19 +01:00
note_type = str(fm.get("type") or n.get("type") or "note")
cfg_for_type = types_cfg.get(note_type, {}) if isinstance(types_cfg, dict) else {}
2025-11-09 09:15:48 +01:00
2025-11-09 10:11:19 +01:00
default_rw = _env_float("MINDNET_DEFAULT_RETRIEVER_WEIGHT", 1.0)
2025-11-09 10:17:24 +01:00
retriever_weight = _coalesce(fm.get("retriever_weight"), cfg_for_type.get("retriever_weight"), default_rw)
2025-11-09 10:11:19 +01:00
try:
retriever_weight = float(retriever_weight)
except Exception:
retriever_weight = default_rw
2025-11-09 10:17:24 +01:00
chunk_profile = _coalesce(fm.get("chunk_profile"), cfg_for_type.get("chunk_profile"), os.environ.get("MINDNET_DEFAULT_CHUNK_PROFILE","medium"))
chunk_profile = chunk_profile if isinstance(chunk_profile, str) else "medium"
2025-11-09 10:11:19 +01:00
2025-11-09 10:17:24 +01:00
edge_defaults = _ensure_list(_coalesce(fm.get("edge_defaults"), cfg_for_type.get("edge_defaults"), []))
2025-11-09 10:11:19 +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 ""
2025-11-09 10:17:24 +01:00
path = n.get("path") or path_arg
2025-11-09 10:11:19 +01:00
if isinstance(path, pathlib.Path):
path = str(path)
2025-11-09 09:15:48 +01:00
2025-11-09 10:17:24 +01:00
payload = {
2025-11-09 10:11:19 +01:00
"note_id": note_id,
2025-11-09 09:51:05 +01:00
"title": title,
2025-11-09 10:11:19 +01:00
"type": note_type,
2025-11-09 10:17:24 +01:00
"path": path or "", # immer vorhanden
2025-11-09 10:11:19 +01:00
"retriever_weight": retriever_weight,
"chunk_profile": chunk_profile,
2025-11-09 09:51:05 +01:00
"edge_defaults": edge_defaults,
2025-11-09 09:15:48 +01:00
}
2025-11-08 22:06:21 +01:00
2025-11-09 10:11:19 +01:00
tags = fm.get("tags") or fm.get("keywords") or n.get("tags")
2025-11-09 10:17:24 +01:00
if tags: payload["tags"] = _ensure_list(tags)
for k in ("created","modified","date"):
2025-11-09 10:11:19 +01:00
v = fm.get(k) or n.get(k)
2025-11-09 10:17:24 +01:00
if v: payload[k] = str(v)
2025-11-09 10:11:19 +01:00
json.loads(json.dumps(payload, ensure_ascii=False))
2025-11-08 22:06:21 +01:00
return payload