diff --git a/web/app.py b/web/app.py index 4bdc270..bb7eb33 100644 --- a/web/app.py +++ b/web/app.py @@ -4389,18 +4389,41 @@ async def precedent_library_delete(case_law_id: str): # drain the queue. +async def _wake_ceo_for_precedent(case_law_id: UUID, kind: str) -> dict: + """Trigger Paperclip CEO to drain the precedent extraction queue, mirroring + the upload flow (see ``precedent_library_upload`` → ``pc_wake_for_precedent_extraction``). + + Best-effort — any failure is logged but doesn't surface to the user, who + can still invoke ``mcp__legal-ai__precedent_process_pending`` manually. + Returns a dict with the wakeup outcome for inclusion in the API response. + """ + record = await db.get_case_law(case_law_id) + if not record: + return {"ok": False, "skipped": "record_missing"} + try: + return await pc_wake_for_precedent_extraction( + case_law_id=str(case_law_id), + citation=str(record.get("case_number") or ""), + practice_area=str(record.get("practice_area") or ""), + ) + except Exception: + logger.exception("precedent-extraction wakeup failed (non-fatal, kind=%s)", kind) + return {"ok": False, "error": "wakeup_failed"} + + @app.post("/api/precedent-library/{case_law_id}/request-metadata") async def precedent_request_metadata(case_law_id: str): - """Stamp the case_law row as needing metadata extraction. The local - MCP worker (`precedent_process_pending_metadata`) will pick it up.""" + """Stamp the case_law row as needing metadata extraction AND wake the + Paperclip CEO so extraction runs automatically — same flow as upload.""" try: cid = UUID(case_law_id) except ValueError: raise HTTPException(400, "case_law_id לא תקין") ok = await db.request_metadata_extraction(cid) if not ok: - raise HTTPException(404, "פסיקה לא נמצאה (או לא מסוג external_upload)") - return {"queued": True, "case_law_id": case_law_id, "kind": "metadata"} + raise HTTPException(404, "פסיקה לא נמצאה") + wakeup = await _wake_ceo_for_precedent(cid, kind="metadata") + return {"queued": True, "case_law_id": case_law_id, "kind": "metadata", "wakeup": wakeup} @app.post("/api/precedent-library/{case_law_id}/request-halachot") @@ -4412,8 +4435,9 @@ async def precedent_request_halachot(case_law_id: str): raise HTTPException(400, "case_law_id לא תקין") ok = await db.request_halacha_extraction(cid) if not ok: - raise HTTPException(404, "פסיקה לא נמצאה (או לא מסוג external_upload)") - return {"queued": True, "case_law_id": case_law_id, "kind": "halacha"} + raise HTTPException(404, "פסיקה לא נמצאה") + wakeup = await _wake_ceo_for_precedent(cid, kind="halacha") + return {"queued": True, "case_law_id": case_law_id, "kind": "halacha", "wakeup": wakeup} @app.get("/api/precedent-library/queue/pending")