app/core/note_payload.py aktualisiert
All checks were successful
Deploy mindnet to llm-node / deploy (push) Successful in 3s
All checks were successful
Deploy mindnet to llm-node / deploy (push) Successful in 3s
This commit is contained in:
parent
bdbc4a1bf7
commit
8800160564
|
|
@ -1,52 +1,30 @@
|
||||||
# note_payload.py
|
# note_payload.py
|
||||||
"""
|
|
||||||
Mindnet - Note Payload Builder
|
|
||||||
Version: 1.4.3
|
|
||||||
Beschreibung:
|
|
||||||
- Robust gegenüber alten/neuen Aufrufsignaturen (toleriert *args, **kwargs).
|
|
||||||
- Liest Typ-Defaults aus ./config/config.yaml oder ./config/types.yaml.
|
|
||||||
- Setzt in mindnet_notes u.a.:
|
|
||||||
- retriever_weight (Frontmatter > Typ-Defaults > ENV > 1.0)
|
|
||||||
- chunk_profile (Frontmatter > Typ-Defaults > ENV > "medium")
|
|
||||||
- edge_defaults (Frontmatter > Typ-Defaults > [])
|
|
||||||
- path, type, title, note_id, tags, created/modified/date (falls vorhanden)
|
|
||||||
- Garantiert JSON-serialisierbare Payloads.
|
|
||||||
"""
|
|
||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
from typing import Any, Dict, Optional
|
from typing import Any, Dict, Optional, Tuple
|
||||||
import os
|
import os, json, pathlib, yaml
|
||||||
import json
|
|
||||||
import pathlib
|
|
||||||
import yaml
|
|
||||||
|
|
||||||
|
|
||||||
def _as_dict(note: Any) -> Dict[str, Any]:
|
def _as_dict(note: Any) -> Dict[str, Any]:
|
||||||
if isinstance(note, dict):
|
if isinstance(note, dict):
|
||||||
return note
|
return note
|
||||||
d: Dict[str, Any] = {}
|
d: Dict[str, Any] = {}
|
||||||
for attr in (
|
for attr in ("id","note_id","title","path","frontmatter","meta","metadata","type","created","modified","date","tags"):
|
||||||
"id",
|
|
||||||
"note_id",
|
|
||||||
"title",
|
|
||||||
"path",
|
|
||||||
"frontmatter",
|
|
||||||
"meta",
|
|
||||||
"body",
|
|
||||||
"text",
|
|
||||||
"type",
|
|
||||||
"created",
|
|
||||||
"modified",
|
|
||||||
"chunks",
|
|
||||||
"tags",
|
|
||||||
):
|
|
||||||
if hasattr(note, attr):
|
if hasattr(note, attr):
|
||||||
d[attr] = getattr(note, attr)
|
d[attr] = getattr(note, attr)
|
||||||
# manche Parser nutzen "metadata" statt "frontmatter"
|
# Normalisiere Frontmatter
|
||||||
if "frontmatter" not in d and hasattr(note, "metadata"):
|
fm = d.get("frontmatter") or d.get("meta") or d.get("metadata") or {}
|
||||||
d["frontmatter"] = getattr(note, "metadata")
|
d["frontmatter"] = fm if isinstance(fm, dict) else {}
|
||||||
return d
|
return d
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
def _load_types_config(explicit: Optional[Dict[str, Any]] = None) -> Dict[str, Any]:
|
def _load_types_config(explicit: Optional[Dict[str, Any]] = None) -> Dict[str, Any]:
|
||||||
if isinstance(explicit, dict):
|
if isinstance(explicit, dict):
|
||||||
|
|
@ -56,104 +34,71 @@ def _load_types_config(explicit: Optional[Dict[str, Any]] = None) -> Dict[str, A
|
||||||
if p.exists():
|
if p.exists():
|
||||||
with p.open("r", encoding="utf-8") as f:
|
with p.open("r", encoding="utf-8") as f:
|
||||||
data = yaml.safe_load(f) or {}
|
data = yaml.safe_load(f) or {}
|
||||||
# zulässig: {"types": {...}} oder direkt {...}
|
|
||||||
if isinstance(data, dict) and "types" in data and isinstance(data["types"], dict):
|
if isinstance(data, dict) and "types" in data and isinstance(data["types"], dict):
|
||||||
return data["types"]
|
return data["types"]
|
||||||
return data if isinstance(data, dict) else {}
|
return data if isinstance(data, dict) else {}
|
||||||
return {}
|
return {}
|
||||||
|
|
||||||
|
|
||||||
def _get_front(n: Dict[str, Any]) -> Dict[str, Any]:
|
|
||||||
fm = n.get("frontmatter") or n.get("meta") or {}
|
|
||||||
return fm if isinstance(fm, dict) else {}
|
|
||||||
|
|
||||||
|
|
||||||
def _coalesce(*vals):
|
def _coalesce(*vals):
|
||||||
for v in vals:
|
for v in vals:
|
||||||
if v is not None:
|
if v is not None:
|
||||||
return v
|
return v
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
def _env_float(name: str, default: float) -> float:
|
def _env_float(name: str, default: float) -> float:
|
||||||
try:
|
try:
|
||||||
return float(os.environ.get(name, default))
|
return float(os.environ.get(name, default))
|
||||||
except Exception:
|
except Exception:
|
||||||
return default
|
return default
|
||||||
|
|
||||||
|
|
||||||
def _ensure_list(x) -> list:
|
def _ensure_list(x) -> list:
|
||||||
if x is None:
|
if x is None: return []
|
||||||
return []
|
if isinstance(x, list): return [str(i) for i in x]
|
||||||
if isinstance(x, list):
|
if isinstance(x, (set, tuple)): return [str(i) for i in x]
|
||||||
return [str(i) for i in x]
|
|
||||||
if isinstance(x, (set, tuple)):
|
|
||||||
return [str(i) for i in list(x)]
|
|
||||||
return [str(x)]
|
return [str(x)]
|
||||||
|
|
||||||
|
|
||||||
def make_note_payload(note: Any, *args, **kwargs) -> Dict[str, Any]:
|
def make_note_payload(note: Any, *args, **kwargs) -> Dict[str, Any]:
|
||||||
"""
|
|
||||||
Build JSON-serialisable payload for a Note.
|
|
||||||
Accepts legacy extra args/kwargs (e.g. types_config, vault_root) without error.
|
|
||||||
"""
|
|
||||||
n = _as_dict(note)
|
n = _as_dict(note)
|
||||||
types_cfg = kwargs.get("types_config") or (args[0] if args else None)
|
path_arg, types_cfg_explicit = _pick_args(*args, **kwargs)
|
||||||
types_cfg = _load_types_config(types_cfg)
|
types_cfg = _load_types_config(types_cfg_explicit)
|
||||||
|
|
||||||
fm = _get_front(n)
|
fm = n.get("frontmatter") or {}
|
||||||
note_type = str(fm.get("type") or n.get("type") or "note")
|
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 {}
|
cfg_for_type = types_cfg.get(note_type, {}) if isinstance(types_cfg, dict) else {}
|
||||||
|
|
||||||
default_rw = _env_float("MINDNET_DEFAULT_RETRIEVER_WEIGHT", 1.0)
|
default_rw = _env_float("MINDNET_DEFAULT_RETRIEVER_WEIGHT", 1.0)
|
||||||
|
retriever_weight = _coalesce(fm.get("retriever_weight"), cfg_for_type.get("retriever_weight"), default_rw)
|
||||||
retriever_weight = _coalesce(
|
|
||||||
fm.get("retriever_weight"),
|
|
||||||
cfg_for_type.get("retriever_weight"),
|
|
||||||
default_rw,
|
|
||||||
)
|
|
||||||
try:
|
try:
|
||||||
retriever_weight = float(retriever_weight)
|
retriever_weight = float(retriever_weight)
|
||||||
except Exception:
|
except Exception:
|
||||||
retriever_weight = default_rw
|
retriever_weight = default_rw
|
||||||
|
|
||||||
chunk_profile = _coalesce(
|
chunk_profile = _coalesce(fm.get("chunk_profile"), cfg_for_type.get("chunk_profile"), os.environ.get("MINDNET_DEFAULT_CHUNK_PROFILE","medium"))
|
||||||
fm.get("chunk_profile"),
|
chunk_profile = chunk_profile if isinstance(chunk_profile, str) else "medium"
|
||||||
cfg_for_type.get("chunk_profile"),
|
|
||||||
os.environ.get("MINDNET_DEFAULT_CHUNK_PROFILE", "medium"),
|
|
||||||
)
|
|
||||||
if not isinstance(chunk_profile, str):
|
|
||||||
chunk_profile = "medium"
|
|
||||||
|
|
||||||
edge_defaults = _ensure_list(
|
edge_defaults = _ensure_list(_coalesce(fm.get("edge_defaults"), cfg_for_type.get("edge_defaults"), []))
|
||||||
_coalesce(fm.get("edge_defaults"), cfg_for_type.get("edge_defaults"), [])
|
|
||||||
)
|
|
||||||
|
|
||||||
note_id = n.get("note_id") or n.get("id") or fm.get("id")
|
note_id = n.get("note_id") or n.get("id") or fm.get("id")
|
||||||
title = n.get("title") or fm.get("title") or ""
|
title = n.get("title") or fm.get("title") or ""
|
||||||
path = n.get("path")
|
path = n.get("path") or path_arg
|
||||||
if isinstance(path, pathlib.Path):
|
if isinstance(path, pathlib.Path):
|
||||||
path = str(path)
|
path = str(path)
|
||||||
|
|
||||||
payload: Dict[str, Any] = {
|
payload = {
|
||||||
"note_id": note_id,
|
"note_id": note_id,
|
||||||
"title": title,
|
"title": title,
|
||||||
"type": note_type,
|
"type": note_type,
|
||||||
"path": path,
|
"path": path or "", # immer vorhanden
|
||||||
"retriever_weight": retriever_weight,
|
"retriever_weight": retriever_weight,
|
||||||
"chunk_profile": chunk_profile,
|
"chunk_profile": chunk_profile,
|
||||||
"edge_defaults": edge_defaults,
|
"edge_defaults": edge_defaults,
|
||||||
}
|
}
|
||||||
|
|
||||||
tags = fm.get("tags") or fm.get("keywords") or n.get("tags")
|
tags = fm.get("tags") or fm.get("keywords") or n.get("tags")
|
||||||
if tags:
|
if tags: payload["tags"] = _ensure_list(tags)
|
||||||
payload["tags"] = _ensure_list(tags)
|
for k in ("created","modified","date"):
|
||||||
|
|
||||||
for k in ("created", "modified", "date"):
|
|
||||||
v = fm.get(k) or n.get(k)
|
v = fm.get(k) or n.get(k)
|
||||||
if v:
|
if v: payload[k] = str(v)
|
||||||
payload[k] = str(v)
|
|
||||||
|
|
||||||
# Validierungs-RTT (stellt JSON-Serialisierbarkeit sicher)
|
|
||||||
json.loads(json.dumps(payload, ensure_ascii=False))
|
json.loads(json.dumps(payload, ensure_ascii=False))
|
||||||
return payload
|
return payload
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user