93 lines
2.8 KiB
Python
93 lines
2.8 KiB
Python
"""
|
|
FILE: app/routers/ingest.py
|
|
DESCRIPTION: Endpunkte für WP-11. Nimmt Markdown entgegen, steuert Ingestion und Discovery (Link-Vorschläge).
|
|
VERSION: 0.6.0
|
|
STATUS: Active
|
|
DEPENDENCIES: app.core.ingestion, app.services.discovery, fastapi, pydantic
|
|
LAST_ANALYSIS: 2025-12-15
|
|
"""
|
|
|
|
import os
|
|
import time
|
|
import logging
|
|
from fastapi import APIRouter, HTTPException
|
|
from pydantic import BaseModel
|
|
from typing import Optional, Dict, Any
|
|
|
|
from app.core.ingestion import IngestionService
|
|
from app.services.discovery import DiscoveryService
|
|
|
|
logger = logging.getLogger(__name__)
|
|
router = APIRouter()
|
|
|
|
# Services Init (Global oder via Dependency Injection)
|
|
discovery_service = DiscoveryService()
|
|
|
|
class AnalyzeRequest(BaseModel):
|
|
text: str
|
|
type: str = "concept"
|
|
|
|
class SaveRequest(BaseModel):
|
|
markdown_content: str
|
|
filename: Optional[str] = None
|
|
folder: str = "00_Inbox"
|
|
|
|
class SaveResponse(BaseModel):
|
|
status: str
|
|
file_path: str
|
|
note_id: str
|
|
stats: Dict[str, Any]
|
|
|
|
@router.post("/analyze")
|
|
async def analyze_draft(req: AnalyzeRequest):
|
|
"""
|
|
WP-11 Intelligence: Liefert Link-Vorschläge via DiscoveryService.
|
|
"""
|
|
try:
|
|
# Hier rufen wir jetzt den verbesserten Service auf
|
|
result = await discovery_service.analyze_draft(req.text, req.type)
|
|
return result
|
|
except Exception as e:
|
|
logger.error(f"Analyze failed: {e}", exc_info=True)
|
|
return {"suggestions": [], "error": str(e)}
|
|
|
|
@router.post("/save", response_model=SaveResponse)
|
|
async def save_note(req: SaveRequest):
|
|
"""
|
|
WP-11 Persistence: Speichert und indiziert.
|
|
"""
|
|
try:
|
|
vault_root = os.getenv("MINDNET_VAULT_ROOT", "./vault")
|
|
abs_vault_root = os.path.abspath(vault_root)
|
|
|
|
if not os.path.exists(abs_vault_root):
|
|
try: os.makedirs(abs_vault_root, exist_ok=True)
|
|
except: pass
|
|
|
|
final_filename = req.filename or f"draft_{int(time.time())}.md"
|
|
ingest_service = IngestionService()
|
|
|
|
# Async Call
|
|
result = await ingest_service.create_from_text(
|
|
markdown_content=req.markdown_content,
|
|
filename=final_filename,
|
|
vault_root=abs_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.get("path", "unknown"),
|
|
note_id=result.get("note_id", "unknown"),
|
|
stats={
|
|
"chunks": result.get("chunks_count", 0),
|
|
"edges": result.get("edges_count", 0)
|
|
}
|
|
)
|
|
except HTTPException as he: raise he
|
|
except Exception as e:
|
|
logger.error(f"Save failed: {e}", exc_info=True)
|
|
raise HTTPException(status_code=500, detail=f"Save failed: {str(e)}") |