File size: 3,672 Bytes
b0b150b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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

from typing import List
from fastapi import APIRouter, Depends, HTTPException, UploadFile, File, Form
from sqlalchemy.orm import Session
import tempfile
from pathlib import Path

from core.database import get_db
from services.agent_service import agent_service
from services.storage_service import storage_service
from workers.compilation_worker import compilation_worker
from api.deps import get_current_user
from models.user import User

router = APIRouter(prefix="/api/compile", tags=["compile"])

@router.post("/")
async def compile_agent_v2(
    files: List[UploadFile] = File(...),
    agent_name: str = Form(...),
    system_prompt: str = Form(...),
    db: Session = Depends(get_db),
    current_user: User = Depends(get_current_user)
):
    """
    Compile an agent from uploaded files (Phase 2 - Database integrated).
    
    Creates agent record in database and starts background compilation.
    """
    if not files:
        raise HTTPException(status_code=400, detail="No files uploaded")
    if not agent_name or not agent_name.strip():
        raise HTTPException(status_code=400, detail="Agent name is required")
    if not system_prompt or not system_prompt.strip():
        raise HTTPException(status_code=400, detail="System prompt is required")
    
    try:
        # Create agent record
        agent = agent_service.create_agent(db, current_user, agent_name, system_prompt)
        
        # Read file contents and upload to Supabase
        files_data = []
        for file in files:
            content = await file.read()
            
            # Upload to Supabase Storage (agent-uploads bucket)
            try:
                upload_result = await storage_service.upload_file(
                    file=file,
                    bucket="agent-uploads",
                    folder=f"raw/{agent.id}"
                )
                storage_path = upload_result["path"]
                storage_url = upload_result["url"]
            except Exception as e:
                logger.error(f"Failed to upload raw file to Supabase: {e}")
                storage_path = None
                storage_url = None

            files_data.append({
                "filename": file.filename,
                "content": content.decode("utf-8", errors="ignore"),
                "storage_path": storage_path,
                "storage_url": storage_url
            })
        
        # Start background compilation
        job = compilation_worker.start_compilation(
            db=db,
            agent=agent,
            files_data=files_data
        )
        
        return {
            "success": True,
            "message": f"Compilation started for agent '{agent.name}'",
            "agent_id": agent.id,
            "agent_name": agent.name,
            "job_id": job.id
        }
        
    except ValueError as e:
        raise HTTPException(status_code=400, detail=str(e))
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))

@router.get("/{agent_name}/status")
def get_compilation_status(
    agent_name: str,
    db: Session = Depends(get_db),
    current_user: User = Depends(get_current_user)
):
    """Get compilation status for an agent."""
    agent = agent_service.get_agent(db, current_user, agent_name)
    if not agent:
        raise HTTPException(status_code=404, detail="Agent not found")
    
    job_status = compilation_worker.get_job_status(db, agent.id)
    
    if not job_status:
        return {
            "status": agent.status,
            "message": "No compilation job found"
        }
    
    return {
        "agent_status": agent.status,
        "job": job_status
    }