mindnet/app/core/ingestion.py

344 lines
15 KiB
Python
Raw Normal View History

2025-12-10 21:41:43 +01:00
"""
2025-12-15 15:40:39 +01:00
FILE: app/core/ingestion.py
2025-12-23 21:57:50 +01:00
DESCRIPTION: Haupt-Ingestion-Logik. Transformiert Markdown in den Graphen.
2025-12-25 17:17:55 +01:00
WP-20: Optimiert für OpenRouter (mistralai/mistral-7b-instruct:free).
WP-22: Content Lifecycle, Edge Registry Validation & Multi-Hash.
2025-12-26 21:52:08 +01:00
WP-15b: Two-Pass Ingestion mit LocalBatchCache & Candidate-Validation.
FIX: Beibehaltung der Deep Fallback Logic (v2.11.14) zur JSON-Recovery.
VERSION: 2.12.0
2025-12-15 15:40:39 +01:00
STATUS: Active
2025-12-26 21:52:08 +01:00
DEPENDENCIES: app.core.parser, app.core.note_payload, app.core.chunker,
app.services.llm_service, app.services.edge_registry
2025-12-10 21:41:43 +01:00
"""
import os
2025-12-23 14:33:51 +01:00
import json
2025-12-23 21:57:50 +01:00
import re
2025-12-11 10:23:23 +01:00
import logging
2025-12-12 17:53:31 +01:00
import asyncio
import time
2025-12-11 10:23:23 +01:00
from typing import Dict, List, Optional, Tuple, Any
2025-12-10 21:41:43 +01:00
2025-12-11 10:23:23 +01:00
# Core Module Imports
2025-12-10 21:41:43 +01:00
from app.core.parser import (
read_markdown,
2025-12-26 21:52:08 +01:00
pre_scan_markdown,
2025-12-10 21:41:43 +01:00
normalize_frontmatter,
validate_required_frontmatter,
2025-12-23 18:51:12 +01:00
extract_edges_with_context,
2025-12-26 21:52:08 +01:00
NoteContext
2025-12-10 21:41:43 +01:00
)
from app.core.note_payload import make_note_payload
2025-12-12 17:53:31 +01:00
from app.core.chunker import assemble_chunks, get_chunk_config
2025-12-10 21:41:43 +01:00
from app.core.chunk_payload import make_chunk_payloads
2025-12-11 10:23:23 +01:00
2025-12-12 18:10:26 +01:00
# Fallback für Edges
2025-12-10 21:41:43 +01:00
try:
from app.core.derive_edges import build_edges_for_note
except ImportError:
2025-12-12 17:53:31 +01:00
def build_edges_for_note(*args, **kwargs): return []
2025-12-10 21:41:43 +01:00
from app.core.qdrant import QdrantConfig, get_client, ensure_collections, ensure_payload_indexes
from app.core.qdrant_points import (
points_for_chunks,
points_for_note,
points_for_edges,
upsert_batch,
)
2025-12-11 10:23:23 +01:00
from app.services.embeddings_client import EmbeddingsClient
2025-12-18 11:50:52 +01:00
from app.services.edge_registry import registry as edge_registry
2025-12-23 18:51:12 +01:00
from app.services.llm_service import LLMService
2025-12-10 21:41:43 +01:00
2025-12-11 10:23:23 +01:00
logger = logging.getLogger(__name__)
2025-12-26 21:52:08 +01:00
# --- Global Helpers (Full Compatibility v2.11.14) ---
2025-12-23 21:57:50 +01:00
def extract_json_from_response(text: str) -> Any:
2025-12-25 17:17:55 +01:00
"""
Extrahiert JSON-Daten und bereinigt LLM-Steuerzeichen (Mistral/Llama).
Entfernt <s>, [OUT], [/OUT] und Markdown-Blöcke für maximale Robustheit.
"""
2025-12-25 21:28:54 +01:00
if not text or not isinstance(text, str):
return []
2025-12-25 17:17:55 +01:00
2025-12-25 21:46:40 +01:00
# 1. Entferne Mistral/Llama Steuerzeichen und Tags
2025-12-25 17:17:55 +01:00
clean = text.replace("<s>", "").replace("</s>", "")
clean = clean.replace("[OUT]", "").replace("[/OUT]", "")
clean = clean.strip()
# 2. Suche nach Markdown JSON-Blöcken (```json ... ```)
match = re.search(r"```(?:json)?\s*(.*?)\s*```", clean, re.DOTALL)
payload = match.group(1) if match else clean
2025-12-23 21:57:50 +01:00
try:
2025-12-25 17:17:55 +01:00
return json.loads(payload.strip())
2025-12-23 21:57:50 +01:00
except json.JSONDecodeError:
2025-12-25 17:17:55 +01:00
# 3. Recovery: Suche nach der ersten [ und letzten ] (Liste)
start = payload.find('[')
end = payload.rfind(']') + 1
if start != -1 and end > start:
try:
return json.loads(payload[start:end])
except: pass
# 4. Zweite Recovery: Suche nach der ersten { und letzten } (Objekt)
start_obj = payload.find('{')
end_obj = payload.rfind('}') + 1
if start_obj != -1 and end_obj > start_obj:
try:
return json.loads(payload[start_obj:end_obj])
2025-12-23 21:57:50 +01:00
except: pass
2025-12-25 17:17:55 +01:00
return []
2025-12-23 21:57:50 +01:00
2025-12-10 21:41:43 +01:00
def load_type_registry(custom_path: Optional[str] = None) -> dict:
"""Lädt die types.yaml zur Steuerung der typ-spezifischen Ingestion."""
2025-12-10 21:41:43 +01:00
import yaml
2025-12-23 18:51:12 +01:00
from app.config import get_settings
settings = get_settings()
path = custom_path or settings.MINDNET_TYPES_FILE
2025-12-12 17:53:31 +01:00
if not os.path.exists(path): return {}
2025-12-10 21:41:43 +01:00
try:
2025-12-12 17:53:31 +01:00
with open(path, "r", encoding="utf-8") as f: return yaml.safe_load(f) or {}
except Exception: return {}
2025-12-10 21:41:43 +01:00
# --- Service Class ---
2025-12-10 21:41:43 +01:00
class IngestionService:
2025-12-11 12:23:06 +01:00
def __init__(self, collection_prefix: str = None):
2025-12-23 14:33:51 +01:00
from app.config import get_settings
2025-12-23 15:20:14 +01:00
self.settings = get_settings()
2025-12-11 12:23:06 +01:00
2025-12-23 14:33:51 +01:00
self.prefix = collection_prefix or self.settings.COLLECTION_PREFIX
2025-12-10 21:41:43 +01:00
self.cfg = QdrantConfig.from_env()
2025-12-11 12:23:06 +01:00
self.cfg.prefix = self.prefix
2025-12-10 21:41:43 +01:00
self.client = get_client(self.cfg)
2025-12-23 21:44:49 +01:00
self.dim = self.settings.VECTOR_SIZE
2025-12-23 15:20:14 +01:00
self.registry = load_type_registry()
2025-12-11 10:23:23 +01:00
self.embedder = EmbeddingsClient()
2025-12-23 18:51:12 +01:00
self.llm = LLMService()
2025-12-11 10:23:23 +01:00
2025-12-23 18:51:12 +01:00
self.active_hash_mode = self.settings.CHANGE_DETECTION_MODE
2025-12-26 21:52:08 +01:00
self.batch_cache: Dict[str, NoteContext] = {} # WP-15b LocalBatchCache
2025-12-11 12:23:06 +01:00
try:
ensure_collections(self.client, self.prefix, self.dim)
ensure_payload_indexes(self.client, self.prefix)
except Exception as e:
2025-12-12 17:53:31 +01:00
logger.warning(f"DB init warning: {e}")
2025-12-10 21:41:43 +01:00
2025-12-26 21:52:08 +01:00
async def run_batch(self, file_paths: List[str], vault_root: str) -> List[Dict[str, Any]]:
"""
WP-15b: Implementiert den Two-Pass Ingestion Workflow.
Pass 1: Pre-Scan baut Kontext-Cache auf.
Pass 2: Processing führt semantische Validierung durch.
"""
logger.info(f"🔍 [Pass 1] Pre-Scanning {len(file_paths)} files for Batch Cache...")
for path in file_paths:
ctx = pre_scan_markdown(path)
if ctx:
self.batch_cache[ctx.note_id] = ctx
logger.info(f"🚀 [Pass 2] Processing {len(file_paths)} files...")
results = []
for path in file_paths:
res = await self.process_file(path, vault_root, apply=True)
results.append(res)
return results
async def _validate_candidate(self, chunk_text: str, edge: Dict) -> bool:
"""
WP-15b: Validiert einen Kanten-Kandidaten semantisch gegen das Ziel.
Nutzt den Cache aus Pass 1, um dem LLM Kontext der Ziel-Note zu geben.
"""
target_id = edge.get("to")
target_ctx = self.batch_cache.get(target_id)
# Falls Zielnotiz nicht im aktuellen Batch ist: 'explicit' durchlassen (Hard-Link Integrity)
if not target_ctx:
return True
provider = self.settings.MINDNET_LLM_PROVIDER
template = self.llm.get_prompt("edge_validation", provider)
try:
prompt = template.format(
chunk_text=chunk_text[:1500],
target_title=target_ctx.title,
target_summary=target_ctx.summary,
edge_kind=edge.get("kind", "related_to")
)
response = await self.llm.generate_raw_response(prompt, priority="background")
return "YES" in response.upper()
except Exception as e:
logger.warning(f"⚠️ Semantic validation error for {target_id}: {e}")
return True # Fallback: Im Zweifel Link behalten
def _resolve_note_type(self, requested: Optional[str]) -> str:
"""Bestimmt den finalen Notiz-Typ (Fallback auf 'concept')."""
types = self.registry.get("types", {})
if requested and requested in types: return requested
return "concept"
2025-12-23 22:02:32 +01:00
def _get_chunk_config_by_profile(self, profile_name: str, note_type: str) -> Dict[str, Any]:
"""Holt die Chunker-Parameter für ein spezifisches Profil aus der Registry."""
2025-12-23 22:02:32 +01:00
profiles = self.registry.get("chunking_profiles", {})
if profile_name in profiles:
cfg = profiles[profile_name].copy()
if "overlap" in cfg and isinstance(cfg["overlap"], list):
cfg["overlap"] = tuple(cfg["overlap"])
return cfg
return get_chunk_config(note_type)
2025-12-11 10:23:23 +01:00
async def process_file(
2025-12-23 21:57:50 +01:00
self, file_path: str, vault_root: str,
force_replace: bool = False, apply: bool = False, purge_before: bool = False,
note_scope_refs: bool = False, hash_source: str = "parsed", hash_normalize: str = "canonical"
2025-12-10 21:41:43 +01:00
) -> Dict[str, Any]:
2025-12-26 21:52:08 +01:00
"""Transformiert eine Markdown-Datei in den Graphen."""
result = {"path": file_path, "status": "skipped", "changed": False, "error": None}
2025-12-10 21:41:43 +01:00
# 1. Parse & Lifecycle Gate
2025-12-10 21:41:43 +01:00
try:
parsed = read_markdown(file_path)
2025-12-23 21:57:50 +01:00
if not parsed: return {**result, "error": "Empty file"}
2025-12-10 21:41:43 +01:00
fm = normalize_frontmatter(parsed.frontmatter)
validate_required_frontmatter(fm)
except Exception as e:
return {**result, "error": f"Validation failed: {str(e)}"}
2025-12-26 21:52:08 +01:00
# Lifecycle Filter (WP-22)
2025-12-18 11:50:52 +01:00
status = fm.get("status", "draft").lower().strip()
if status in ["system", "template", "archive", "hidden"]:
2025-12-23 21:57:50 +01:00
return {**result, "status": "skipped", "reason": f"lifecycle_{status}"}
2025-12-18 11:50:52 +01:00
2025-12-26 21:52:08 +01:00
# 2. Config Resolution & Payload
note_type = self._resolve_note_type(fm.get("type"))
2025-12-10 21:41:43 +01:00
fm["type"] = note_type
2025-12-10 21:41:43 +01:00
try:
2025-12-23 14:33:51 +01:00
note_pl = make_note_payload(parsed, vault_root=vault_root, hash_normalize=hash_normalize, hash_source=hash_source, file_path=file_path)
2025-12-10 21:41:43 +01:00
note_id = note_pl["note_id"]
except Exception as e:
2025-12-23 21:57:50 +01:00
return {**result, "error": f"Payload failed: {str(e)}"}
2025-12-10 21:41:43 +01:00
2025-12-26 21:52:08 +01:00
# 3. Change Detection (v2.11.14 Logic)
2025-12-23 21:57:50 +01:00
old_payload = None if force_replace else self._fetch_note_payload(note_id)
check_key = f"{self.active_hash_mode}:{hash_source}:{hash_normalize}"
2025-12-23 21:57:50 +01:00
old_hash = (old_payload or {}).get("hashes", {}).get(check_key)
new_hash = note_pl.get("hashes", {}).get(check_key)
2025-12-23 14:33:51 +01:00
chunks_missing, edges_missing = self._artifacts_missing(note_id)
should_write = force_replace or (not old_payload) or (old_hash != new_hash) or chunks_missing or edges_missing
if not should_write:
return {**result, "status": "unchanged", "note_id": note_id}
2025-12-10 21:41:43 +01:00
if not apply:
return {**result, "status": "dry-run", "changed": True, "note_id": note_id}
2025-12-10 21:41:43 +01:00
2025-12-26 21:52:08 +01:00
# 4. Processing (Chunking, Embedding, Validated Edges)
2025-12-10 21:41:43 +01:00
try:
body_text = getattr(parsed, "body", "") or ""
edge_registry.ensure_latest()
2025-12-23 14:33:51 +01:00
2025-12-26 21:52:08 +01:00
# Chunker Resolution
profile = fm.get("chunk_profile") or fm.get("chunking_profile") or "sliding_standard"
chunk_cfg = self._get_chunk_config_by_profile(profile, note_type)
chunks = await assemble_chunks(fm["id"], body_text, fm["type"], config=chunk_cfg)
2025-12-10 21:41:43 +01:00
chunk_pls = make_chunk_payloads(fm, note_pl["path"], chunks, note_text=body_text)
2025-12-26 21:52:08 +01:00
# Embeddings
vecs = []
if chunk_pls:
texts = [c.get("window") or c.get("text") or "" for c in chunk_pls]
vecs = await self.embedder.embed_documents(texts)
2025-12-18 11:50:52 +01:00
2025-12-26 21:52:08 +01:00
# Kanten-Extraktion & WP-15b Validierung
2025-12-18 11:50:52 +01:00
edges = []
2025-12-23 14:33:51 +01:00
context = {"file": file_path, "note_id": note_id}
2025-12-26 21:52:08 +01:00
# A. Explizite Kandidaten (Wikilinks)
raw_candidates = extract_edges_with_context(parsed)
for cand in raw_candidates:
# Semantische Prüfung gegen Pass 1 Cache
if await self._validate_candidate(body_text, cand):
cand["kind"] = edge_registry.resolve(
edge_type=cand["kind"],
provenance="explicit",
context={**context, "line": cand.get("line")}
)
edges.append(cand)
else:
logger.info(f"🚫 WP-15b: Candidate rejected: {cand['kind']} -> {cand['to']}")
2025-12-23 14:33:51 +01:00
2025-12-26 21:52:08 +01:00
# B. System Kanten (Struktur)
2025-12-23 15:20:14 +01:00
try:
2025-12-23 21:57:50 +01:00
sys_edges = build_edges_for_note(note_id, chunk_pls, note_level_references=note_pl.get("references", []), include_note_scope_refs=note_scope_refs)
except:
sys_edges = build_edges_for_note(note_id, chunk_pls)
2025-12-23 15:20:14 +01:00
2025-12-23 21:57:50 +01:00
for e in sys_edges:
2025-12-23 15:20:14 +01:00
valid_kind = edge_registry.resolve(edge_type=e.get("kind", "belongs_to"), provenance="structure", context={**context, "line": "system"})
2025-12-23 17:56:44 +01:00
if valid_kind:
e["kind"] = valid_kind
edges.append(e)
2025-12-11 12:23:06 +01:00
2025-12-10 21:41:43 +01:00
except Exception as e:
2025-12-23 18:51:12 +01:00
logger.error(f"Processing failed for {file_path}: {e}", exc_info=True)
2025-12-10 21:41:43 +01:00
return {**result, "error": f"Processing failed: {str(e)}"}
# 5. DB Upsert
2025-12-11 12:23:06 +01:00
try:
2025-12-23 21:57:50 +01:00
if purge_before and old_payload: self._purge_artifacts(note_id)
2025-12-11 12:23:06 +01:00
n_name, n_pts = points_for_note(self.prefix, note_pl, None, self.dim)
upsert_batch(self.client, n_name, n_pts)
2025-12-10 21:41:43 +01:00
2025-12-11 12:23:06 +01:00
if chunk_pls and vecs:
c_name, c_pts = points_for_chunks(self.prefix, chunk_pls, vecs)
upsert_batch(self.client, c_name, c_pts)
2025-12-12 17:53:31 +01:00
2025-12-11 12:23:06 +01:00
if edges:
e_name, e_pts = points_for_edges(self.prefix, edges)
upsert_batch(self.client, e_name, e_pts)
2025-12-10 21:41:43 +01:00
2025-12-23 15:20:14 +01:00
return {"path": file_path, "status": "success", "changed": True, "note_id": note_id, "chunks_count": len(chunk_pls), "edges_count": len(edges)}
2025-12-11 12:23:06 +01:00
except Exception as e:
return {**result, "error": f"DB Upsert failed: {e}"}
2025-12-10 21:41:43 +01:00
def _fetch_note_payload(self, note_id: str) -> Optional[dict]:
2025-12-25 21:46:40 +01:00
"""Holt die Metadaten einer Note aus Qdrant."""
2025-12-10 21:41:43 +01:00
from qdrant_client.http import models as rest
2025-12-11 12:23:06 +01:00
try:
f = rest.Filter(must=[rest.FieldCondition(key="note_id", match=rest.MatchValue(value=note_id))])
2025-12-23 21:57:50 +01:00
pts, _ = self.client.scroll(collection_name=f"{self.prefix}_notes", scroll_filter=f, limit=1, with_payload=True)
2025-12-11 12:23:06 +01:00
return pts[0].payload if pts else None
except: return None
2025-12-10 21:41:43 +01:00
def _artifacts_missing(self, note_id: str) -> Tuple[bool, bool]:
2025-12-25 21:28:54 +01:00
"""Prüft Qdrant aktiv auf vorhandene Chunks und Edges."""
2025-12-10 21:41:43 +01:00
from qdrant_client.http import models as rest
2025-12-11 12:23:06 +01:00
try:
f = rest.Filter(must=[rest.FieldCondition(key="note_id", match=rest.MatchValue(value=note_id))])
2025-12-23 14:33:51 +01:00
c_pts, _ = self.client.scroll(collection_name=f"{self.prefix}_chunks", scroll_filter=f, limit=1)
e_pts, _ = self.client.scroll(collection_name=f"{self.prefix}_edges", scroll_filter=f, limit=1)
2025-12-11 12:23:06 +01:00
return (not bool(c_pts)), (not bool(e_pts))
except: return True, True
2025-12-10 21:41:43 +01:00
def _purge_artifacts(self, note_id: str):
2025-12-25 21:46:40 +01:00
"""Löscht verwaiste Chunks/Edges vor einem Re-Import."""
2025-12-10 21:41:43 +01:00
from qdrant_client.http import models as rest
f = rest.Filter(must=[rest.FieldCondition(key="note_id", match=rest.MatchValue(value=note_id))])
for suffix in ["chunks", "edges"]:
2025-12-23 21:57:50 +01:00
try: self.client.delete(collection_name=f"{self.prefix}_{suffix}", points_selector=rest.FilterSelector(filter=f))
except: pass
2025-12-23 18:51:12 +01:00
async def create_from_text(self, markdown_content: str, filename: str, vault_root: str, folder: str = "00_Inbox") -> Dict[str, Any]:
"""Hilfsmethode zur Erstellung einer Note aus einem Textstream."""
2025-12-23 18:51:12 +01:00
target_dir = os.path.join(vault_root, folder)
os.makedirs(target_dir, exist_ok=True)
file_path = os.path.join(target_dir, filename)
2025-12-23 21:57:50 +01:00
with open(file_path, "w", encoding="utf-8") as f:
f.write(markdown_content)
await asyncio.sleep(0.1)
2025-12-23 18:51:12 +01:00
return await self.process_file(file_path=file_path, vault_root=vault_root, apply=True, force_replace=True, purge_before=True)