110 lines
3.9 KiB
Python
110 lines
3.9 KiB
Python
#!/usr/bin/env python3
|
|
from __future__ import annotations
|
|
import argparse, os, glob, json, sys
|
|
from dotenv import load_dotenv
|
|
from qdrant_client import QdrantClient
|
|
|
|
from app.core.parser import read_markdown, normalize_frontmatter, validate_required_frontmatter
|
|
from app.core.note_payload import make_note_payload
|
|
from app.core.validate_note import validate_note_payload
|
|
from app.core.chunker import assemble_chunks
|
|
from app.core.chunk_payload import make_chunk_payloads
|
|
from app.core.embed import embed_texts, embed_one
|
|
from app.core.qdrant import QdrantConfig, ensure_collections, get_client
|
|
from app.core.qdrant_points import points_for_chunks, points_for_note, points_for_edges, upsert_batch
|
|
from app.core.edges import deriv_edges_for_note
|
|
|
|
def iter_md(root: str, exclude=(" /.obsidian/ ", " /_backup_frontmatter/ ", " /_imported/ ")):
|
|
files = [p for p in glob.glob(os.path.join(root, "**", "*.md"), recursive=True)]
|
|
out = []
|
|
for p in files:
|
|
pn = p.replace("\\","/")
|
|
if any(ex.strip() in pn for ex in ["/.obsidian/", "/_backup_frontmatter/", "/_imported/"]):
|
|
continue
|
|
out.append(p)
|
|
return out
|
|
|
|
def main():
|
|
load_dotenv()
|
|
ap = argparse.ArgumentParser()
|
|
ap.add_argument("--vault", required=True, help="Obsidian Vault Pfad (z.B. mindnet/vault)")
|
|
ap.add_argument("--apply", action="store_true", help="Schreibt in Qdrant (sonst Dry-Run)")
|
|
ap.add_argument("--note-id", help="Nur eine Note-ID verarbeiten")
|
|
ap.add_argument("--embed-note", action="store_true", help="Auch Note-Volltext einbetten (optional)")
|
|
args = ap.parse_args()
|
|
|
|
# Qdrant
|
|
cfg = QdrantConfig(
|
|
url=os.getenv("QDRANT_URL", "http://127.0.0.1:6333"),
|
|
api_key=os.getenv("QDRANT_API_KEY") or None,
|
|
prefix=os.getenv("COLLECTION_PREFIX", "mindnet"),
|
|
dim=int(os.getenv("VECTOR_DIM","384")),
|
|
)
|
|
client = get_client(cfg)
|
|
ensure_collections(cfg)
|
|
|
|
root = os.path.abspath(args.vault)
|
|
files = iter_md(root)
|
|
if not files:
|
|
print("Keine Markdown-Dateien gefunden.", file=sys.stderr); sys.exit(2)
|
|
|
|
total_notes = 0
|
|
for path in files:
|
|
parsed = read_markdown(path)
|
|
fm = normalize_frontmatter(parsed.frontmatter)
|
|
try:
|
|
validate_required_frontmatter(fm)
|
|
except Exception:
|
|
continue
|
|
if args.note_id and fm.get("id") != args.note_id:
|
|
continue
|
|
|
|
total_notes += 1
|
|
# Note-Payload
|
|
note_pl = make_note_payload(parsed, vault_root=root)
|
|
validate_note_payload(note_pl)
|
|
|
|
# Chunks
|
|
chunks = assemble_chunks(fm["id"], parsed.body, fm.get("type", "concept"))
|
|
chunk_pls = make_chunk_payloads(fm, note_pl["path"], chunks)
|
|
|
|
# Embeddings (Chunks)
|
|
texts = [c for c in (ch.text for ch in chunks)]
|
|
vectors = embed_texts(texts)
|
|
|
|
# Optional: Note-Vektor
|
|
note_vec = None
|
|
if args.embed_note:
|
|
note_vec = embed_one(parsed.body)
|
|
|
|
# Kanten
|
|
edges = deriv_edges_for_note(fm, chunk_pls)
|
|
|
|
# Dry-Run-Ausgabe
|
|
summary = {
|
|
"note_id": fm["id"],
|
|
"title": fm["title"],
|
|
"chunks": len(chunk_pls),
|
|
"edges": len(edges),
|
|
"path": note_pl["path"]
|
|
}
|
|
print(json.dumps(summary, ensure_ascii=False))
|
|
|
|
if args.apply:
|
|
# Notes upsert
|
|
notes_col, note_pts = points_for_note(cfg.prefix, note_pl, note_vec, cfg.dim)
|
|
upsert_batch(client, notes_col, note_pts)
|
|
|
|
# Chunks upsert
|
|
chunks_col, chunk_pts = points_for_chunks(cfg.prefix, chunk_pls, vectors)
|
|
upsert_batch(client, chunks_col, chunk_pts)
|
|
|
|
# Edges upsert
|
|
edges_col, edge_pts = points_for_edges(cfg.prefix, edges)
|
|
upsert_batch(client, edges_col, edge_pts)
|
|
|
|
print(f"Done. Processed notes: {total_notes}")
|
|
|
|
if __name__ == "__main__":
|
|
main()
|