from __future__ import annotations from typing import Any, Dict, Optional def _coerce_float(val: Any) -> Optional[float]: if val is None: return None try: if isinstance(val, (int, float)): return float(val) if isinstance(val, str): v = val.strip() if not v: return None return float(v.replace(",", ".")) except Exception: 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") def make_note_payload( frontmatter: Dict[str, Any], *args, note_id: Optional[str] = None, path: str = "", text: str = "", retriever_weight: Optional[float] = None, **kwargs, ) -> Dict[str, Any]: """ 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. """ 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, } weight = _extract_weight(frontmatter, retriever_weight) if weight is not None: payload["retriever_weight"] = weight return payload