File size: 5,122 Bytes
7ce9fc3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# API Handler for processing deployment requests

import logging
import json
import base64
import os
import tempfile
from datetime import datetime
from typing import Dict, List, Any

from .llm_generator import LLMCodeGenerator
from .github_deployer import GitHubDeployer
from .evaluation_notifier import EvaluationNotifier
from .utils import save_request_log, decode_attachments
from .config import Config

logger = logging.getLogger(__name__)

def process_deployment_request(request_data: Dict[str, Any]):
    """
    Process a deployment request (build or revise round)
    Runs in background thread to avoid API timeout
    """
    try:
        # Extract request data
        email = request_data['email']
        task_id = request_data['task']
        round_num = request_data['round']
        nonce = request_data['nonce']
        brief = request_data['brief']
        evaluation_url = request_data['evaluation_url']
        attachments = request_data.get('attachments', [])
        checks = request_data.get('checks', [])
        
        logger.info(f"Processing round {round_num} for {email}, task {task_id}")
        
        # Save request log
        log_file = save_request_log(request_data)
        logger.info(f"Request logged to {log_file}")
        
        # Generate repository name
        repo_name = Config.get_repo_name(task_id, email)
        
        # Initialize components
        llm_generator = LLMCodeGenerator()
        github_deployer = GitHubDeployer()
        evaluator = EvaluationNotifier()
        
        # Decode and save attachments
        attachment_files = []
        if attachments:
            attachment_files = decode_attachments(attachments, task_id)
            logger.info(f"Decoded {len(attachment_files)} attachments")
        
        if round_num == 1:
            # Build Phase: Generate new project
            logger.info(f"Generating new project for task {task_id}")
            
            project_files = llm_generator.generate_project_from_brief(
                brief=brief,
                attachments=attachment_files,
                task_id=task_id,
                checks=checks
            )
            
            # Create new repository
            repo_url, commit_sha, pages_url = github_deployer.create_and_deploy_repo(
                repo_name=repo_name,
                project_files=project_files,
                attachment_files=attachment_files,
                brief=brief
            )
            
        else:
            # Revise Phase: Update existing project
            logger.info(f"Revising existing project for task {task_id}")
            
            # Generate revised code
            revised_files = llm_generator.revise_project_from_brief(
                brief=brief,
                repo_name=repo_name,
                attachments=attachment_files,
                checks=checks
            )
            
            # Update existing repository
            repo_url, commit_sha, pages_url = github_deployer.update_existing_repo(
                repo_name=repo_name,
                revised_files=revised_files,
                attachment_files=attachment_files,
                brief=brief
            )
        
        logger.info(f"Deployment successful - Repo: {repo_url}, Pages: {pages_url}")
        
        # Notify evaluation endpoint
        evaluation_data = {
            'email': email,
            'task': task_id,
            'round': round_num,
            'nonce': nonce,
            'repo_url': repo_url,
            'commit_sha': commit_sha,
            'pages_url': pages_url
        }
        
        success = evaluator.notify_evaluation_endpoint(evaluation_url, evaluation_data)
        
        if success:
            logger.info(f"Successfully notified evaluation endpoint for {email}")
        else:
            logger.error(f"Failed to notify evaluation endpoint for {email}")
        
        # Clean up temporary files
        for attachment_file in attachment_files:
            try:
                if os.path.exists(attachment_file):
                    os.remove(attachment_file)
            except Exception as e:
                logger.warning(f"Failed to clean up {attachment_file}: {e}")
        
    except Exception as e:
        logger.error(f"Deployment failed for {request_data.get('email', 'unknown')}: {str(e)}")
        
        # Still try to notify evaluation endpoint about the failure
        try:
            evaluator = EvaluationNotifier()
            evaluation_data = {
                'email': request_data.get('email', ''),
                'task': request_data.get('task', ''),
                'round': request_data.get('round', 0),
                'nonce': request_data.get('nonce', ''),
                'error': str(e),
                'status': 'failed'
            }
            evaluator.notify_evaluation_endpoint(
                request_data.get('evaluation_url', ''),
                evaluation_data
            )
        except Exception as notify_error:
            logger.error(f"Failed to notify evaluation endpoint about error: {notify_error}")