Spaces:
Sleeping
Sleeping
| import logging | |
| from app.services.enrichment.mindmap import generate_mindmap | |
| from app.services.enrichment.normalize import normalize_text | |
| from app.services.enrichment.summary import generate_summary | |
| from app.services.enrichment.title_keywords import extract_title_and_keywords | |
| from app.services.note_store import get_note, update_note | |
| from app.models.enums import NoteStatus | |
| from app.utils.time import now_ts | |
| logger = logging.getLogger(__name__) | |
| async def enrich_note(note_id: str, tasks: list[str]): | |
| note = get_note(note_id) | |
| if not note: | |
| return | |
| update_note(note_id, { | |
| "status": NoteStatus.processing, | |
| "updated_at": now_ts(), | |
| }) | |
| try: | |
| # ---------- TEXT SOURCE ---------- | |
| raw_text = note.get("raw_text", "").strip() | |
| if not raw_text: | |
| logger.info( | |
| "[enrich_note] skip enrich, raw_text empty note_id=%s", | |
| note_id, | |
| ) | |
| return | |
| text = raw_text | |
| # ---------- 1️⃣ Normalize ---------- | |
| if "normalize" in tasks: | |
| try: | |
| normalized = await normalize_text(raw_text) | |
| if normalized and len(normalized) > 10: | |
| text = normalized | |
| update_note(note_id, { | |
| "normalized_text": normalized, | |
| "status": NoteStatus.normalize_done, | |
| "updated_at": now_ts(), | |
| }) | |
| except Exception: | |
| pass | |
| # ---------- 2️⃣ Title + Keywords ---------- | |
| if "keywords" in tasks: | |
| try: | |
| title, keywords = await extract_title_and_keywords(text) | |
| updates = { | |
| "status": NoteStatus.keywords_done, | |
| "updated_at": now_ts(), | |
| } | |
| # 🔥 FAIL-SAFE | |
| if title and title.strip(): | |
| updates["title"] = title.strip() | |
| elif note.get("summary"): | |
| updates["title"] = note["summary"].split(".")[0][:120] | |
| updates["keywords"] = keywords or [] | |
| update_note(note_id, updates) | |
| except Exception: | |
| pass | |
| # ---------- 3️⃣ Summary ---------- | |
| if "summary" in tasks: | |
| try: | |
| summary = await generate_summary(text) | |
| if summary: | |
| update_note(note_id, { | |
| "summary": summary, | |
| "status": NoteStatus.summary_done, | |
| "updated_at": now_ts(), | |
| }) | |
| except Exception: | |
| pass | |
| # ---------- 4️⃣ Mindmap ---------- | |
| if "mindmap" in tasks: | |
| try: | |
| mindmap = await generate_mindmap(text) | |
| if mindmap: | |
| update_note(note_id, { | |
| "mindmap": mindmap, | |
| "status": NoteStatus.mindmap_done, | |
| "updated_at": now_ts(), | |
| }) | |
| except Exception: | |
| pass | |
| # ---------- DONE ---------- | |
| if raw_text: | |
| update_note(note_id, { | |
| "status": NoteStatus.ready, | |
| "updated_at": now_ts(), | |
| }) | |
| except Exception: | |
| logger.exception("Enrichment failed note_id=%s", note_id) | |
| update_note(note_id, { | |
| "status": NoteStatus.error, | |
| "updated_at": now_ts(), | |
| }) | |