mindnet/app/routers/ingest.py
2025-12-10 22:05:35 +01:00

89 lines
2.6 KiB
Python

"""
app/routers/ingest.py
API-Endpunkte für WP-11 (Discovery & Persistence).
"""
import os
import time
from fastapi import APIRouter, HTTPException
from pydantic import BaseModel
from typing import Optional, List, Dict, Any
from app.core.ingestion import IngestionService
from app.services.discovery import DiscoveryService
router = APIRouter()
# --- DTOs ---
class AnalyzeRequest(BaseModel):
text: str
type: str = "concept"
class SaveRequest(BaseModel):
markdown_content: str
filename: Optional[str] = None # Optional, fallback auf Timestamp/Titel
folder: str = "00_Inbox" # Zielordner im Vault
class SaveResponse(BaseModel):
status: str
file_path: str
note_id: str
stats: Dict[str, Any]
# --- Services ---
# Instanzierung hier oder via Dependency Injection
discovery_service = DiscoveryService()
@router.post("/analyze")
async def analyze_draft(req: AnalyzeRequest):
"""
WP-11 Intelligence: Analysiert einen Entwurf und liefert Link-Vorschläge.
"""
try:
result = await discovery_service.analyze_draft(req.text, req.type)
return result
except Exception as e:
raise HTTPException(status_code=500, detail=f"Analysis failed: {str(e)}")
@router.post("/save", response_model=SaveResponse)
async def save_note(req: SaveRequest):
"""
WP-11 Persistence: Speichert Markdown physisch und indiziert es in Qdrant.
"""
# 1. Vault Root ermitteln
vault_root = os.getenv("MINDNET_VAULT_ROOT", "./vault")
if not os.path.exists(vault_root):
# Fallback für Dev-Umgebungen
if os.path.exists("../vault"):
vault_root = "../vault"
else:
raise HTTPException(status_code=500, detail="Vault root not configured or missing")
# 2. Filename generieren falls fehlend
final_filename = req.filename
if not final_filename:
# Einfacher Fallback: Timestamp
final_filename = f"draft_{int(time.time())}.md"
# 3. Ingestion Service nutzen
ingest_service = IngestionService() # nutzt Default-Prefix oder aus Env
result = ingest_service.create_from_text(
markdown_content=req.markdown_content,
filename=final_filename,
vault_root=os.path.abspath(vault_root),
folder=req.folder
)
if result.get("status") == "error":
raise HTTPException(status_code=500, detail=result.get("error"))
return SaveResponse(
status="success",
file_path=result["path"],
note_id=result.get("note_id", "unknown"),
stats={
"chunks": result.get("chunks_count", 0),
"edges": result.get("edges_count", 0)
}
)