mindnet/app/core/derive_edges.py

125 lines
4.0 KiB
Python
Raw Normal View History

2025-09-05 09:47:09 +02:00
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
2025-09-30 12:36:19 +02:00
Modul: app/core/derive_edges.py
2025-11-08 07:59:24 +01:00
Version: 1.9.0
Datum: 2025-11-07
2025-09-05 09:47:09 +02:00
2025-09-30 12:36:19 +02:00
Zweck
-----
Robuste Kantenbildung für mindnet (Notes/Chunks):
2025-11-08 07:59:24 +01:00
• belongs_to (chunk → note)
• next / prev (Chunk-Sequenz)
• references (chunk-scope) aus Chunk.window/text
• optional note-scope references/backlink (Flag)
• optional Default-Kanten pro Note-Type aus Type-Registry (falls vorhanden)
Abwärtskompatibel zu v1.4.0 (keine Pflicht auf Registry).
2025-09-05 09:47:09 +02:00
"""
2025-09-04 09:26:59 +02:00
from __future__ import annotations
2025-09-30 12:36:19 +02:00
2025-10-01 10:54:46 +02:00
from typing import Dict, List, Optional, Iterable
2025-09-30 12:36:19 +02:00
2025-10-01 10:54:46 +02:00
from app.core.parser import extract_wikilinks
2025-09-30 12:36:19 +02:00
2025-11-08 07:59:24 +01:00
# Type-Registry (optional)
try:
from app.core.type_registry import get_edge_defaults # type: ignore
except Exception:
def get_edge_defaults(_note_type: str) -> List[str]:
return []
2025-09-30 12:36:19 +02:00
def _get(d: dict, *keys, default=None):
for k in keys:
if k in d and d[k] is not None:
return d[k]
return default
def _chunk_text_for_refs(chunk: dict) -> str:
return (
_get(chunk, "window")
or _get(chunk, "text")
or _get(chunk, "content")
or _get(chunk, "raw")
or ""
)
def _dedupe(seq: Iterable[str]) -> List[str]:
seen = set()
out: List[str] = []
for s in seq:
if s not in seen:
seen.add(s)
out.append(s)
return out
def _edge(kind: str, scope: str, source_id: str, target_id: str, note_id: str, extra: Optional[dict] = None) -> dict:
2025-11-08 07:59:24 +01:00
pl = {"kind": kind, "scope": scope, "source_id": source_id, "target_id": target_id, "note_id": note_id}
2025-09-30 12:36:19 +02:00
if extra:
pl.update(extra)
return pl
def build_edges_for_note(
note_id: str,
chunks: List[dict],
note_level_references: Optional[List[str]] = None,
include_note_scope_refs: bool = False,
) -> List[dict]:
2025-09-04 09:26:59 +02:00
edges: List[dict] = []
2025-09-30 12:36:19 +02:00
# belongs_to
for ch in chunks:
cid = _get(ch, "chunk_id", "id")
if not cid:
continue
edges.append(_edge("belongs_to", "chunk", cid, note_id, note_id, {"chunk_id": cid}))
# next/prev
for i in range(len(chunks) - 1):
2025-10-01 10:54:46 +02:00
a, b = chunks[i], chunks[i + 1]
2025-09-30 12:36:19 +02:00
a_id = _get(a, "chunk_id", "id")
b_id = _get(b, "chunk_id", "id")
if not a_id or not b_id:
continue
edges.append(_edge("next", "chunk", a_id, b_id, note_id, {"chunk_id": a_id}))
edges.append(_edge("prev", "chunk", b_id, a_id, note_id, {"chunk_id": b_id}))
2025-11-08 07:59:24 +01:00
# references (chunk-scope)
2025-09-30 12:36:19 +02:00
refs_all: List[str] = []
for ch in chunks:
cid = _get(ch, "chunk_id", "id")
if not cid:
continue
txt = _chunk_text_for_refs(ch)
2025-11-08 07:59:24 +01:00
refs = extract_wikilinks(txt)
2025-09-30 12:36:19 +02:00
for r in refs:
edges.append(_edge("references", "chunk", cid, r, note_id, {"chunk_id": cid, "ref_text": r}))
refs_all.extend(refs)
2025-10-01 10:54:46 +02:00
# optional: note-scope references/backlinks
2025-09-30 12:36:19 +02:00
if include_note_scope_refs:
refs_note = refs_all[:]
if note_level_references:
refs_note.extend([r for r in note_level_references if isinstance(r, str) and r])
refs_note = _dedupe(refs_note)
for r in refs_note:
edges.append(_edge("references", "note", note_id, r, note_id))
edges.append(_edge("backlink", "note", r, note_id, note_id))
2025-11-08 07:59:24 +01:00
# optional: Default-Kanten aus Registry (no-op, wenn leer)
# Beispiel: task → depends_on, concept → related_to etc.
# Wir erzeugen nur "formale" Kanten note→note_id selbst nicht; diese dienen Retri­ever-Gewichtung später.
# (Hier keine Targets – das sind Typ-Hinweise und werden als self-hints abgelegt.)
try:
note_type = ""
if chunks:
note_type = str(_get(chunks[0], "type", default="") or "")
defaults = get_edge_defaults(note_type)
for k in defaults:
# Self-hint-Kante (kann vom Retriever gewichtet werden). Scope 'note'
edges.append(_edge(k, "note", note_id, note_id, note_id))
except Exception:
pass
2025-09-04 09:26:59 +02:00
return edges