File size: 15,904 Bytes
28a08e7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
"""backend/api/files.py β€” Virtual File System CRUD (S354).

GAP-VFS-FIX (P37):
- _lint_and_update_manifest() ora riceve content_updated_at (updated_at del record
  appena salvato). Passato a lint_and_store() per confronto ottimistico.
- PUT /api/files/{id}: accetta optional "expected_updated_at" nel body.
  Se presente e il valore non corrisponde all'updated_at corrente β†’ HTTP 409 Conflict.
  Backward compatible: assente = comportamento legacy (last-write-wins).
- Tutti i writer (POST, PUT, _write_file_internal) propagano updated_at al task lint.
"""
import uuid, time, asyncio
from typing import Optional
from fastapi import APIRouter, Body, HTTPException, Depends
from .state import sb

from .auth_guard import require_role, AuthRole
from tools.registry import _scaffold_project

# P19-SEC2: era fail-open. Il fix concorrente (backend/auth/auth_managed.get_user_session_token)
# usava un token placeholder hardcoded e non importava Depends (NameError a runtime).
# Allineato al pattern require_role(AuthRole.MACHINE) usato su tutti gli altri endpoint interni.
router = APIRouter(dependencies=[Depends(require_role(AuthRole.MACHINE))])


# ─── S364: fire-and-forget lint + manifest helper ────────────────────────────

async def _lint_and_update_manifest(
    file_id: str,
    content: str,
    language: str,
    path: str,
    conversation_id: str,
    content_updated_at: int = 0,
) -> None:
    """S364: background lint + manifest update. Never raises, never blocks VFS write.

    GAP-VFS-FIX: content_updated_at Γ¨ l'updated_at del record al momento della
    scrittura. Passato a lint_and_store() per evitare scrittura di risultati stale
    se il file Γ¨ stato aggiornato di nuovo nel frattempo.
    """
    try:
        from .linter import lint_and_store
        await lint_and_store(
            file_id, content, language, path, conversation_id,
            content_updated_at=content_updated_at,
        )
    except Exception:
        pass  # S364: silent failure always
    try:
        from .project_manifest import update_manifest
        await update_manifest(conversation_id, path, content, language)
    except Exception:
        pass  # S364: silent failure always



# ─── P20-MCP: _write_file_internal β€” usata da mcp.py file_write tool ─────────

_LANG_BY_EXT: dict[str, str] = {
    ".py": "python", ".ts": "typescript", ".tsx": "typescript",
    ".js": "javascript", ".jsx": "javascript", ".html": "html",
    ".css": "css", ".json": "json", ".md": "markdown",
    ".yaml": "yaml", ".yml": "yaml", ".sh": "bash",
    ".txt": "text", ".env": "env",
}

def _detect_lang(path: str) -> str:
    """Infer language from file extension for VFS metadata."""
    import os as _os
    _, ext = _os.path.splitext(path or "")
    return _LANG_BY_EXT.get(ext.lower(), "text")


async def _write_file_internal(path: str, content: str,
                                conversation_id: str = "") -> dict:
    """P20-MCP: Crea/aggiorna un file VFS per path.

    Usata dal MCP file_write tool e altri consumer interni.
    Se Supabase non disponibile β†’ ritorna payload senza persist (mai lancia eccezioni).
    Avvia lint + manifest update in background (S364 pattern).

    GAP-VFS-FIX: propaga l'updated_at effettivo del record salvato al task lint,
    così lint_and_store() può scartare il risultato se il file è cambiato di nuovo.
    """
    body: dict = {
        "id":              str(uuid.uuid4()),
        "path":            path,
        "content":         content,
        "language":        _detect_lang(path),
        "conversation_id": conversation_id or "",
        "updated_at":      int(time.time() * 1000),
        "created_at":      int(time.time() * 1000),
    }
    try:
        data  = sb().table("vfs_files").upsert(body).execute()
        saved = data.data[0] if data.data else body
        _fid  = saved.get("id", body["id"])
        # GAP-VFS-FIX: usa l'updated_at del record effettivamente salvato
        _saved_at = saved.get("updated_at", body["updated_at"])
        if content and _fid:
            asyncio.create_task(_lint_and_update_manifest(
                _fid, content, body["language"], path, body["conversation_id"],
                content_updated_at=_saved_at,
            )).add_done_callback(_log_files_exc)  # BUGFIX
        return saved
    except Exception as exc:
        import logging as _lg
        _lg.getLogger("files").warning("_write_file_internal %s: %s", path, exc)
        return body  # in-memory fallback
def _log_files_exc(t):  # BUGFIX: log eccezioni background da create_task
    if not t.cancelled() and t.exception():
        import logging
        logging.getLogger("files").warning("[files] bg task raised: %s", t.exception())


@router.get('/api/files')
async def list_files(conversation_id: Optional[str] = None):
    try:
        q = sb().table('vfs_files').select('id, path, language, conversation_id, updated_at')
        if conversation_id:
            q = q.eq('conversation_id', conversation_id)
        data = q.order('path').execute()
        return {'files': data.data}
    except HTTPException:
        raise
    except Exception as exc:
        # S750-GAP-J: Supabase irraggiungibile β†’ lista vuota invece di 500
        import logging; logging.getLogger("files").warning("list_files: %s", exc)
        return {'files': [], '_error': str(exc)[:120]}


@router.get('/api/files/{file_id}')
async def get_file(file_id: str):
    try:
        data = sb().table('vfs_files').select('*').eq('id', file_id).single().execute()
        if not data.data:
            raise HTTPException(status_code=404, detail='File not found')
        return {'file': data.data}
    except HTTPException:
        raise
    except Exception:
        raise HTTPException(status_code=404, detail='File not found')


@router.post('/api/files')
async def save_file(body: dict = Body(...)):
    if 'id' not in body:
        body['id'] = str(uuid.uuid4())
    if 'updated_at' not in body:
        body['updated_at'] = int(time.time() * 1000)
    if 'created_at' not in body:
        body['created_at'] = body['updated_at']
    try:
        data = sb().table('vfs_files').upsert(body).execute()
        saved = data.data[0] if data.data else body
    except HTTPException:
        raise
    except Exception as exc:
        # S750-GAP-J: Supabase irraggiungibile β†’ restituisce payload originale
        import logging; logging.getLogger("files").warning("save_file: %s", exc)
        saved = body
    # S364: fire-and-forget lint + manifest update (never blocks response)
    _content  = body.get('content', '') or ''
    _lang     = body.get('language', '') or ''
    _path     = body.get('path', '') or ''
    _file_id  = saved.get('id', body.get('id', ''))
    _conv_id  = body.get('conversation_id', '') or ''
    # GAP-VFS-FIX: usa updated_at effettivo del record salvato
    _saved_at = saved.get('updated_at', body.get('updated_at', 0))
    if _content and _file_id:
        asyncio.create_task(_lint_and_update_manifest(
            _file_id, _content, _lang, _path, _conv_id,
            content_updated_at=_saved_at,
        )).add_done_callback(_log_files_exc)  # BUGFIX
    return {'file': saved}


@router.put('/api/files/{file_id}')
async def update_file(file_id: str, body: dict = Body(...)):
    """[S195] Aggiorna il contenuto di un file VFS esistente.

    GAP-VFS-FIX: supporto optimistic locking via expected_updated_at.
    Se body contiene "expected_updated_at" (BIGINT ms), la UPDATE usa
    WHERE id=? AND updated_at=expected_updated_at.
    0 righe aggiornate β†’ 409 Conflict con {error, current_updated_at}.
    Assente/None β†’ comportamento legacy (last-write-wins).
    """
    # Estrai ed eventualmente rimuovi il campo di controllo prima dell'update
    expected_updated_at: int | None = body.pop('expected_updated_at', None)

    body.pop('id', None)
    body['updated_at'] = int(time.time() * 1000)
    try:
        q = sb().table('vfs_files').update(body).eq('id', file_id)

        if expected_updated_at is not None:
            # GAP-VFS-FIX: confronto ottimistico β€” filtra per updated_at atteso
            q = q.eq('updated_at', int(expected_updated_at))

        data = q.execute()

        if not data.data:
            if expected_updated_at is not None:
                # Nessuna riga aggiornata + expected_updated_at fornito β†’ conflitto
                # Recupera lo stato corrente per restituirlo al client
                try:
                    current = (
                        sb().table('vfs_files')
                        .select('id, updated_at, version')
                        .eq('id', file_id)
                        .maybe_single()
                        .execute()
                    )
                    current_data = current.data if current else None
                except Exception:
                    current_data = None

                raise HTTPException(
                    status_code=409,
                    detail={
                        'error':               'conflict',
                        'message':             (
                            'Il file Γ¨ stato modificato da un altro writer. '
                            'Ricarica e riapplica le modifiche.'
                        ),
                        'expected_updated_at': expected_updated_at,
                        'current_updated_at':  current_data.get('updated_at') if current_data else None,
                        'current_version':     current_data.get('version') if current_data else None,
                        'file_id':             file_id,
                    },
                )
            else:
                # Nessun expected_updated_at β†’ tratta come not found (comportamento originale)
                raise HTTPException(status_code=404, detail='File not found')

        saved = data.data[0]
        # S364: fire-and-forget lint + manifest update (never blocks response)
        _content = body.get('content', '') or ''
        _lang    = body.get('language', '') or ''
        _path    = body.get('path', '') or saved.get('path', '')
        _conv_id = body.get('conversation_id', '') or saved.get('conversation_id', '') or ''
        # GAP-VFS-FIX: updated_at dal record effettivamente salvato
        _saved_at = saved.get('updated_at', body['updated_at'])
        if _content and file_id:
            asyncio.create_task(_lint_and_update_manifest(
                file_id, _content, _lang, _path, _conv_id,
                content_updated_at=_saved_at,
            )).add_done_callback(_log_files_exc)  # BUGFIX
        return {'file': saved}
    except HTTPException:
        raise
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))


@router.delete('/api/files/{file_id}')
async def delete_file(file_id: str):
    try:
        sb().table('vfs_files').delete().eq('id', file_id).execute()
    except HTTPException:
        raise
    except Exception as exc:
        import logging; logging.getLogger("files").warning("delete_file %s: %s", file_id, exc)
    return {'deleted': file_id}


# ─── S363-Blueprint: Instant Project Export ──────────────────────────────────

@router.get('/api/project/export')
async def export_project(conversation_id: str):
    """
    Package all VFS files for a conversation into a ready-to-use ZIP.
    S363-Blueprint: Final Artifact β€” sempre consegna un link funzionante.
    
    Usage: GET /api/project/export?conversation_id=<uuid>
    Returns: application/zip download
    """
    import io
    import zipfile
    from fastapi.responses import StreamingResponse

    try:
        data = (
            sb()
            .table('vfs_files')
            .select('path, content, language')
            .eq('conversation_id', conversation_id)
            .order('path')
            .execute()
        )
    except Exception as e:
        raise HTTPException(status_code=500, detail=f'VFS query failed: {e}')

    files = data.data or []
    if not files:
        raise HTTPException(status_code=404, detail='No files found for this conversation')

    buf = io.BytesIO()
    with zipfile.ZipFile(buf, 'w', zipfile.ZIP_DEFLATED) as zf:
        for f in files:
            # Normalize path β€” strip leading slash, default to 'file.txt'
            rel_path = (f.get('path') or 'unnamed_file').lstrip('/')
            content  = f.get('content') or ''
            zf.writestr(rel_path, content)

        # Add a minimal README
        readme = (
            f"# Project Export\n\n"
            f"Conversation: {conversation_id}\n"
            f"Files: {len(files)}\n\n"
            "Generated by Agente AI Pro.\n"
        )
        zf.writestr('README.md', readme)

    buf.seek(0)
    short_id = conversation_id[:8] if len(conversation_id) >= 8 else conversation_id
    return StreamingResponse(
        buf,
        media_type='application/zip',
        headers={
            'Content-Disposition': f'attachment; filename="project_{short_id}.zip"',
            'Cache-Control': 'no-cache',
        },
    )


@router.get('/api/project/export/manifest')
async def export_manifest(conversation_id: str):
    """
    Return list of exportable files and sizes for a conversation.
    Used by frontend to show 'Download ZIP (N files)' button.
    """
    try:
        data = (
            sb()
            .table('vfs_files')
            .select('path, language, updated_at')
            .eq('conversation_id', conversation_id)
            .order('path')
            .execute()
        )
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))

    files = data.data or []
    return {
        'conversation_id': conversation_id,
        'file_count': len(files),
        'files': [{'path': f.get('path', ''), 'language': f.get('language', '')} for f in files],
        'download_url': f'/api/project/export?conversation_id={conversation_id}',
    }


# ── GAP-X6: scaffold_project REST endpoint ────────────────────────────────────
# Espone _scaffold_project per chiamate dirette dal frontend (NewProjectPanel.tsx).
# Ritorna {success, files, framework, project_name, project_dir, output, created}.
# Il frontend (vfsAsync.write) scrive ogni file nel VFS locale β€” nessun round-trip agent.
@router.post('/api/scaffold_project')
async def scaffold_project_endpoint(body: dict = Body(...)):
    """
    GAP-X6: genera boilerplate da template e restituisce i file al frontend.
    framework: react|nextjs|fastapi|flask|django|express
    project_name: slug del progetto (verrΓ  sanitizzato)
    """
    import re as _re_ep
    _do_scaffold = _scaffold_project

    framework    = str(body.get('framework', 'react')).strip().lower()
    project_name = str(body.get('project_name', 'my-project')).strip()

    # Sanity check: solo framework supportati
    _SUPPORTED = {'react', 'nextjs', 'fastapi', 'flask', 'django', 'express'}
    if framework not in _SUPPORTED:
        from fastapi import HTTPException
        raise HTTPException(status_code=400, detail=f"framework '{framework}' non supportato. Usa: {', '.join(sorted(_SUPPORTED))}")

    # Sanitizza project_name (identico a _scaffold_project internamente)
    _safe = _re_ep.sub(r'[^a-z0-9\-]', '-', project_name.lower())[:30] or 'my-project'
    if not _safe:
        from fastapi import HTTPException
        raise HTTPException(status_code=400, detail="project_name non valido")

    try:
        result = await _do_scaffold(
            framework=framework,
            project_name=_safe,
            target_dir='/tmp',
        )
    except Exception as e:
        return {'success': False, 'error': str(e)[:300], 'files': {}}

    return result