File size: 12,594 Bytes
2e818da
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
"""Project registry API -> the persistent, growable unit (1B foundation).

Replaces the throwaway session-UUID model for new work. Old /library/history
stays for legacy on-disk data (no migration).
"""
import asyncio
import os
import time
from typing import List

from fastapi import APIRouter, BackgroundTasks, File, HTTPException, UploadFile
from pydantic import BaseModel

from app.rag.ingestion import LIBRARY_COLLECTION, ingest_file
from app.routers.session import set_ingest_status
from app.schemas.project import Project, ProjectDetail, ProjectFile, ProjectUpdateRequest
from app.services.project_files import delete_project_uploads, project_upload_dir, remove_file_from_project
from app.services.project_service import ProjectService
from app.services.project_activity import ProjectActivityService
from app.websockets.handlers import get_db, get_graph_manager

router = APIRouter(prefix="/projects", tags=["projects"])
_service = ProjectService()
_activity = ProjectActivityService()

_PDF_CACHE_DIR = os.path.expanduser("~/.studybuddy/pdfs")


def _is_demo_mode() -> bool:
    return os.getenv("DEPLOYMENT_ENV", "desktop") == "demo"


class CreateProjectRequest(BaseModel):
    name: str
    intention: str = "learn"


def _valid_project_id(project_id: str) -> bool:
    """Reject path-traversal payloads before they ever reach ProjectService._path.

    project_id is interpolated directly into a filesystem path
    (ProjectService._path); a value containing "/", "\\", or ".." could
    otherwise escape the registry root.
    """
    return bool(project_id) and not any(c in project_id for c in ("/", "\\", ".."))


def _project_detail(project: Project) -> ProjectDetail:
    return ProjectDetail(**project.model_dump(), document_id=_service.derive_document_id(project))


def _sync_project_files(project: Project, *, lock_held: bool = False) -> Project:
    """Drop registry entries whose project-upload file is no longer on disk."""
    if not lock_held:
        with _service.mutation_sync(project.project_id):
            current = _service.load(project.project_id)
            return _sync_project_files(current or project, lock_held=True)
    upload_dir = project_upload_dir(project.project_id)
    existing = [f for f in project.files if os.path.isfile(os.path.join(upload_dir, f.filename))]
    if len(existing) != len(project.files):
        project.files = existing
        project.updated_at = time.time()
        _service.save(project)
    return project


def _unique_project_filename(upload_dir: str, filename: str, file_id: str) -> str:
    stem, ext = os.path.splitext(os.path.basename(filename or "upload"))
    candidate = f"{stem}{ext}"
    path = os.path.join(upload_dir, candidate)
    if not os.path.exists(path):
        return candidate
    try:
        with open(path, "rb") as f:
            if ProjectService.file_id_for(f.read()) == file_id:
                return candidate
    except OSError:
        pass
    return f"{stem}-{file_id[:8]}{ext}"


@router.post("", response_model=ProjectDetail)
async def create_project(req: CreateProjectRequest):
    project = _service.create(req.name, req.intention)
    # Project memory is created lazily in Chroma on the first grounded
    # observation. Cognee is reserved for the cross-project student profile.
    _activity.record(project.project_id, "project_created", "Project created", metadata={"name": project.name})
    return _project_detail(project)


@router.get("")
def list_projects():
    return {"items": [_project_detail(_sync_project_files(p)).model_dump() for p in _service.list()]}


@router.post("/{project_id}/rebuild-evidence")
async def rebuild_project_evidence(project_id: str):
    """Recreate derived evidence from preserved PDFs, regions, and annotations."""

    from app.services.evidence_rebuild import EvidenceRebuildService

    get_graph_manager().clear_session(project_id)
    return await asyncio.get_running_loop().run_in_executor(
        None,
        EvidenceRebuildService().rebuild_project,
        project_id,
    )


@router.get("/{project_id}/rebuild-evidence/status")
def rebuild_project_evidence_status(project_id: str):
    from app.services.evidence_rebuild import EvidenceRebuildService

    return EvidenceRebuildService().status(project_id)


@router.get("/{project_id}", response_model=ProjectDetail)
def get_project(project_id: str):
    if not _valid_project_id(project_id):
        raise HTTPException(404, "Project not found")
    p = _service.load(project_id)
    if p is None:
        raise HTTPException(404, "Project not found")
    p = _sync_project_files(p)
    return _project_detail(p)


@router.patch("/{project_id}", response_model=ProjectDetail)
def update_project(project_id: str, req: ProjectUpdateRequest):
    if not _valid_project_id(project_id):
        raise HTTPException(404, "Project not found")
    project = _service.update(
        project_id,
        name=req.name,
        intention=req.intention.value if req.intention is not None else None,
        title=req.title,
    )
    if project is None:
        raise HTTPException(404, "Project not found")
    _activity.record(project_id, "project_updated", "Project details updated", metadata={"name": project.name})
    return _project_detail(_sync_project_files(project))


@router.delete("/{project_id}")
def delete_project(project_id: str):
    if not _valid_project_id(project_id):
        raise HTTPException(404, "Project not found")
    with _service.mutation_sync(project_id):
        if _service.load(project_id) is None:
            raise HTTPException(404, "Project not found")
        try:
            from app.rag.evidence_ingestion import EvidenceIngestionService

            EvidenceIngestionService().reset_project(project_id)
        except Exception as exc:
            raise HTTPException(500, "Project evidence cleanup failed; project was preserved") from exc
        _service.delete(project_id)
        delete_project_uploads(project_id)
    try:
        from app.services.visual_lesson_store import VisualLessonStore

        VisualLessonStore().delete_project(project_id)
    except Exception:
        import logging

        logging.getLogger(__name__).exception("Visual lesson cleanup failed for %s", project_id)
    return {"status": "deleted"}


@router.post("/{project_id}/files", response_model=ProjectDetail)
async def add_files(project_id: str, background_tasks: BackgroundTasks, files: List[UploadFile] = File(...)):
    if not _valid_project_id(project_id):
        raise HTTPException(404, "Project not found")
    if len(files) > 5:
        raise HTTPException(400, "Add at most 5 files at a time")
    incoming = [(await file.read(), file.filename or "upload") for file in files]
    to_chunk: list[tuple[bytes, str]] = []
    async with _service.mutation(project_id):
        project = _service.load(project_id)
        if project is None:
            raise HTTPException(404, "Project not found")
        project = _sync_project_files(project, lock_held=True)
        upload_dir = project_upload_dir(project_id)
        os.makedirs(upload_dir, exist_ok=True)
        os.makedirs(_PDF_CACHE_DIR, exist_ok=True)
        existing_ids = {file.file_id for file in project.files}
        for content, supplied_filename in incoming:
            file_id = ProjectService.file_id_for(content)
            cache_path = os.path.join(_PDF_CACHE_DIR, f"{file_id}.pdf")
            if not os.path.exists(cache_path):
                with open(cache_path, "wb") as handle:
                    handle.write(content)
            if file_id not in existing_ids:
                filename = _unique_project_filename(upload_dir, supplied_filename, file_id)
                with open(os.path.join(upload_dir, filename), "wb") as handle:
                    handle.write(content)
                project.files.append(ProjectFile(filename=filename, file_id=file_id))
                existing_ids.add(file_id)
                to_chunk.append((content, filename))
        project.updated_at = time.time()
        _service.save(project)
    if to_chunk:
        _activity.record(
            project_id,
            "paper_added",
            f"Added {len(to_chunk)} paper{'s' if len(to_chunk) != 1 else ''}",
            metadata={"filenames": [filename for _, filename in to_chunk]},
        )

    db = get_db()
    set_ingest_status(project_id, "indexing")
    if to_chunk:
        _activity.record(project_id, "ingest", "Parsing and indexing uploaded material", status="running")

    def _chunk_all():
        try:
            total = 0
            for content, filename in to_chunk:
                total += ingest_file(
                    content, filename, LIBRARY_COLLECTION, db=db,
                    skip_if_indexed=False, project_id=project_id,
                )
            set_ingest_status(project_id, "ready")
            if to_chunk:
                _activity.record(project_id, "ingest", "Structured evidence indexing complete", metadata={"evidence_units": total})
            if _is_demo_mode():
                _activity.record(project_id, "citation_graph_refresh", "Citation map skipped in demo mode", status="warning")
                return
            try:
                import asyncio
                from app.services.citation_graph import CitationGraphService

                _activity.record(project_id, "citation_graph_refresh", "Citation map refreshing", status="running")
                asyncio.run(CitationGraphService().refresh(project_id))
                _activity.record(project_id, "citation_graph_refresh", "Citation map refreshed")
            except Exception:
                import logging
                logging.getLogger(__name__).exception("citation graph refresh failed for %s", project_id)
                _activity.record(project_id, "citation_graph_refresh", "Citation map refresh failed", status="warning")
        except Exception:
            import logging
            logging.getLogger(__name__).exception("structured evidence indexing failed for %s", project_id)
            _activity.record(project_id, "ingest", "Structured evidence indexing failed", status="error")
            set_ingest_status(project_id, "error")

    background_tasks.add_task(_chunk_all)
    return _project_detail(project)


@router.delete("/{project_id}/files/{file_id}", response_model=ProjectDetail)
async def remove_file(project_id: str, file_id: str):
    if not _valid_project_id(project_id):
        raise HTTPException(404, "Project not found")
    async with _service.mutation(project_id):
        project = _service.load(project_id)
        if project is None:
            raise HTTPException(404, "Project not found")
        project = _sync_project_files(project, lock_held=True)
        entry = next((file for file in project.files if file.file_id == file_id), None)
        if entry is None:
            raise HTTPException(404, "File not in project")
        previous_document_set_id = _service.derive_document_id(project)
        # Derived cleanup and source removal must finish before the registry
        # claims the paper is gone. Cleanup failures therefore leave the
        # project/file visible and retryable.
        await remove_file_from_project(project_id, entry.filename)
        project.files = [file for file in project.files if file.file_id != file_id]
        project.updated_at = time.time()
        _service.save(project)
        graph_manager = get_graph_manager()
        graph_manager.clear_session(project_id)
        graph_manager.clear_document_graph(previous_document_set_id)
        graph_manager.clear_document_graph(_service.derive_document_id(project))
    _activity.record(project_id, "paper_removed", "Removed paper", metadata={"filename": entry.filename, "file_id": file_id})
    if _is_demo_mode():
        _activity.record(project_id, "citation_graph_refresh", "Citation map skipped in demo mode", status="warning")
        return _project_detail(project)
    try:
        from app.services.citation_graph import CitationGraphService
        _activity.record(project_id, "citation_graph_refresh", "Citation map refreshing", status="running")
        await CitationGraphService().refresh(project_id)
        _activity.record(project_id, "citation_graph_refresh", "Citation map refreshed")
    except Exception:
        import logging
        logging.getLogger(__name__).exception("citation graph refresh failed for %s", project_id)
        _activity.record(project_id, "citation_graph_refresh", "Citation map refresh failed", status="warning")
    return _project_detail(project)