mindnet/app/core/note_payload.py

79 lines
2.5 KiB
Python
Raw Normal View History

2025-09-09 16:31:25 +02:00
from __future__ import annotations
2025-11-08 21:19:18 +01:00
from typing import Any, Dict, Optional
2025-09-09 16:31:25 +02:00
2025-11-08 21:19:18 +01:00
def _coerce_float(val: Any) -> Optional[float]:
if val is None:
return None
2025-09-09 16:31:25 +02:00
try:
2025-11-08 21:19:18 +01:00
if isinstance(val, (int, float)):
return float(val)
if isinstance(val, str):
v = val.strip()
if not v:
return None
return float(v.replace(",", "."))
2025-09-09 16:31:25 +02:00
except Exception:
2025-11-08 21:19:18 +01:00
return None
return None
def _extract_weight(frontmatter: Dict[str, Any], explicit: Optional[float]) -> Optional[float]:
if explicit is not None:
return _coerce_float(explicit)
if frontmatter is None:
return None
if "retriever_weight" in frontmatter:
return _coerce_float(frontmatter.get("retriever_weight"))
retriever = frontmatter.get("retriever")
if isinstance(retriever, dict) and "weight" in retriever:
return _coerce_float(retriever.get("weight"))
return None
def _resolve_note_id(frontmatter: Dict[str, Any], kw_note_id: Optional[str]) -> Optional[str]:
if kw_note_id:
return kw_note_id
if not isinstance(frontmatter, dict):
return None
return frontmatter.get("id") or frontmatter.get("note_id")
2025-09-09 16:31:25 +02:00
def make_note_payload(
2025-11-08 21:19:18 +01:00
frontmatter: Dict[str, Any],
*args,
note_id: Optional[str] = None,
path: str = "",
text: str = "",
retriever_weight: Optional[float] = None,
**kwargs,
2025-09-09 16:31:25 +02:00
) -> Dict[str, Any]:
"""
2025-11-08 21:19:18 +01:00
Build a note-level payload for Qdrant and inject `retriever_weight` if provided
in frontmatter or as explicit argument.
Extra *args/**kwargs are accepted for backward compatibility.
2025-09-09 16:31:25 +02:00
"""
2025-11-08 21:19:18 +01:00
nid = _resolve_note_id(frontmatter, note_id)
title = None
typ = None
tags = None
if isinstance(frontmatter, dict):
title = frontmatter.get("title")
typ = frontmatter.get("type") or frontmatter.get("note_type")
tags = frontmatter.get("tags")
if isinstance(tags, str):
tags = [t.strip() for t in tags.split(",") if t.strip()]
payload = {
"id": nid, # keep both 'id' and 'note_id' for downstream compatibility
"note_id": nid,
"title": title,
"type": typ,
"tags": tags,
"path": path or None,
# keep optional raw text for convenience (some tools scroll notes by text)
"text": text or None,
2025-09-09 16:55:36 +02:00
}
2025-11-08 21:19:18 +01:00
weight = _extract_weight(frontmatter, retriever_weight)
if weight is not None:
payload["retriever_weight"] = weight
2025-09-09 16:55:36 +02:00
return payload