-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathmain.py
More file actions
435 lines (364 loc) · 17.6 KB
/
Copy pathmain.py
File metadata and controls
435 lines (364 loc) · 17.6 KB
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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
from fastapi import FastAPI, HTTPException, Depends
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import StreamingResponse
import json
import asyncio
import os
from .models.schemas import RepositoryAnalysisRequest, RepositoryAnalysisResponse, RepositoryInfoResponse, AnalysisProgressUpdate, TaskBreakdownRequest, TaskBreakdownResponse, Task, DevinSessionRequest, DevinSessionResponse, CodexPlaygroundRequest, CodexPlaygroundResponse, PlaygroundTaskStatus, RunnerTokenRequest
from .services.github import GitHubService
from .services.agent import AzureAgentService
from .config import CORS_ORIGINS
from .logging_config import setup_logging, get_api_logger
# Set up logging
log_level = os.getenv("LOG_LEVEL", "INFO")
setup_logging(level=log_level, format_style="detailed")
logger = get_api_logger()
app = FastAPI(title="gitagu Backend", description="Backend API for gitagu")
logger.info("Starting gitagu Backend API")
app.add_middleware(
CORSMiddleware,
allow_origins=CORS_ORIGINS,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
def get_github_service():
return GitHubService()
def get_agent_service():
return AzureAgentService()
@app.get("/")
async def root():
logger.info("Root endpoint accessed")
return {"message": "gitagu Backend API", "status": "healthy"}
@app.get("/health")
async def health_check():
"""Health check endpoint for monitoring."""
try:
# Basic health check - could be extended to check dependencies
return {
"status": "healthy",
"service": "gitagu Backend",
"timestamp": "2025-01-27T08:00:00Z"
}
except Exception as e:
logger.error(f"Health check failed: {str(e)}")
raise HTTPException(status_code=503, detail="Service unavailable")
@app.post("/api/analyze", response_model=RepositoryAnalysisResponse)
async def analyze_repository(
request: RepositoryAnalysisRequest,
github_service: GitHubService = Depends(get_github_service),
agent_service: AzureAgentService = Depends(get_agent_service)
):
try:
logger.info(f"Starting analysis for repository: {request.owner}/{request.repo} with agent: {request.agent_id}")
print(f"Analyzing repository: {request.owner}/{request.repo} with agent: {request.agent_id}")
repo_info = await github_service.get_repository_info(request.owner, request.repo)
if not repo_info:
print(f"Repository not found: {request.owner}/{request.repo}")
repo_info = {"name": request.repo, "full_name": f"{request.owner}/{request.repo}"}
readme_content = await github_service.get_readme_content(request.owner, request.repo)
print(f"README content found: {readme_content is not None}")
dependencies = await github_service.get_requirements(request.owner, request.repo)
print(f"Dependencies found: {len(dependencies)}")
files = await github_service.get_repository_files(request.owner, request.repo)
print(f"Repository files found: {len(files)}")
files_dict = [{"path": file.path, "type": file.type, "size": file.size} for file in files]
analysis_result = await agent_service.analyze_repository(
request.agent_id,
f"{request.owner}/{request.repo}",
readme_content or "No README found", # Provide default if None
dependencies or {}, # Provide empty dict if None
files_dict
)
analysis = analysis_result.get("analysis", "")
setup_commands = analysis_result.get("setup_commands", {})
print(f"Analysis result length: {len(analysis)}")
print(f"Setup commands found: {len(setup_commands)}")
logger.info(f"Analysis completed successfully for {request.owner}/{request.repo}")
return RepositoryAnalysisResponse(
agent_id=request.agent_id,
repo_name=f"{request.owner}/{request.repo}",
analysis=analysis,
setup_commands=setup_commands
)
except Exception as e:
logger.error(f"Error analyzing repository {request.owner}/{request.repo}: {str(e)}", exc_info=True)
print(f"Error analyzing repository: {str(e)}")
return RepositoryAnalysisResponse(
agent_id=request.agent_id,
repo_name=f"{request.owner}/{request.repo}",
analysis=f"Error analyzing repository: {str(e)}",
error=str(e)
)
@app.post("/api/analyze-stream")
async def analyze_repository_stream(
request: RepositoryAnalysisRequest,
github_service: GitHubService = Depends(get_github_service),
agent_service: AzureAgentService = Depends(get_agent_service)
):
"""Stream real-time progress updates during repository analysis."""
async def generate_progress_stream():
try:
print(f"Starting streaming analysis for repository: {request.owner}/{request.repo} with agent: {request.agent_id}")
# Create a queue to collect progress updates
import asyncio
progress_queue = asyncio.Queue()
analysis_complete = False
async def progress_callback(update: AnalysisProgressUpdate):
await progress_queue.put(update)
# Start the analysis in a background task
async def run_analysis():
nonlocal analysis_complete
try:
# Fetch repository data
repo_info = await github_service.get_repository_info(request.owner, request.repo)
if not repo_info:
print(f"Repository not found: {request.owner}/{request.repo}")
repo_info = {"name": request.repo, "full_name": f"{request.owner}/{request.repo}"}
readme_content = await github_service.get_readme_content(request.owner, request.repo)
dependencies = await github_service.get_requirements(request.owner, request.repo)
files = await github_service.get_repository_files(request.owner, request.repo)
files_dict = [{"path": file.path, "type": file.type, "size": file.size} for file in files]
# Perform analysis with progress callback
analysis_result = await agent_service.analyze_repository(
request.agent_id,
f"{request.owner}/{request.repo}",
readme_content or "No README found",
dependencies or {},
files_dict,
progress_callback=progress_callback
)
# Send final result to queue
final_response = RepositoryAnalysisResponse(
agent_id=request.agent_id,
repo_name=f"{request.owner}/{request.repo}",
analysis=analysis_result.get("analysis", ""),
setup_commands=analysis_result.get("setup_commands", {})
)
await progress_queue.put({"type": "final_result", "data": final_response.model_dump()})
await progress_queue.put({"type": "complete"})
except Exception as e:
print(f"Error in analysis task: {str(e)}")
await progress_queue.put({
"type": "error",
"error": str(e),
"agent_id": request.agent_id,
"repo_name": f"{request.owner}/{request.repo}"
})
finally:
analysis_complete = True
# Start the analysis task
analysis_task = asyncio.create_task(run_analysis())
# Stream progress updates as they come in
while not analysis_complete or not progress_queue.empty():
try:
# Wait for next update with timeout
update = await asyncio.wait_for(progress_queue.get(), timeout=1.0)
if hasattr(update, 'model_dump'): # It's an AnalysisProgressUpdate
yield f"data: {json.dumps(update.model_dump())}\n\n"
else: # It's a dict (final result, error, etc.)
yield f"data: {json.dumps(update)}\n\n"
if update.get("type") in ["complete", "error"]:
break
except asyncio.TimeoutError:
# Check if analysis is still running
if analysis_complete:
break
continue
# Ensure the analysis task completes
if not analysis_task.done():
await analysis_task
except Exception as e:
print(f"Error in streaming analysis: {str(e)}")
error_response = {
"type": "error",
"error": str(e),
"agent_id": request.agent_id,
"repo_name": f"{request.owner}/{request.repo}"
}
yield f"data: {json.dumps(error_response)}\n\n"
return StreamingResponse(
generate_progress_stream(),
media_type="text/plain",
headers={
"Cache-Control": "no-cache",
"Connection": "keep-alive",
"Content-Type": "text/event-stream",
}
)
@app.get("/api/repo-info/{owner}/{repo}", response_model=RepositoryInfoResponse)
async def get_repository_info(
owner: str,
repo: str,
github_service: GitHubService = Depends(get_github_service)
):
try:
print(f"Fetching repository data for {owner}/{repo}...")
repo_data = await github_service.get_repository_snapshot(owner, repo)
if not repo_data:
print(f"Repository not found: {owner}/{repo}")
raise HTTPException(status_code=404, detail="Repository not found")
return RepositoryInfoResponse(**repo_data)
except RuntimeError as e:
error_msg = f"Error fetching repository info: {str(e)}"
print(error_msg)
raise HTTPException(status_code=500, detail=error_msg)
except Exception as e:
error_msg = f"Unexpected error: {str(e)}"
print(error_msg)
raise HTTPException(status_code=500, detail=error_msg)
@app.post("/api/breakdown-tasks", response_model=TaskBreakdownResponse)
async def breakdown_tasks(
request: TaskBreakdownRequest,
agent_service: AzureAgentService = Depends(get_agent_service)
):
"""Break down a user request into multiple tasks for Devin sessions."""
try:
logger.info(f"Breaking down task: {request.request[:100]}...")
print(f"Breaking down task: {request.request[:100]}...")
# Use the existing agent service to break down the task
breakdown_result = await agent_service.breakdown_user_request(request.request)
# Convert the result to our Task models
tasks = [
Task(title=task["title"], description=task["description"])
for task in breakdown_result["tasks"]
]
logger.info(f"Successfully broke down request into {len(tasks)} tasks")
return TaskBreakdownResponse(tasks=tasks)
except Exception as e:
logger.error(f"Error breaking down tasks: {str(e)}", exc_info=True)
print(f"Error breaking down tasks: {str(e)}")
raise HTTPException(status_code=500, detail=f"Failed to break down tasks: {str(e)}")
@app.post("/api/create-devin-session", response_model=DevinSessionResponse)
async def create_devin_session(request: DevinSessionRequest):
"""Proxy endpoint to create a Devin session."""
try:
import httpx
logger.info(f"Creating Devin session with prompt: {request.prompt[:100]}...")
print(f"Creating Devin session with prompt: {request.prompt[:100]}...")
# Prepare the payload for Devin API
payload = {
"prompt": request.prompt
}
if request.snapshot_id:
payload["snapshot_id"] = request.snapshot_id
if request.playbook_id:
payload["playbook_id"] = request.playbook_id
# Make the request to Devin API
async with httpx.AsyncClient() as client:
response = await client.post(
"https://api.devin.ai/v1/sessions",
headers={
"Authorization": f"Bearer {request.api_key}",
"Content-Type": "application/json",
},
json=payload,
timeout=30.0
)
if response.status_code != 200:
error_detail = f"Devin API error: {response.status_code}"
try:
error_data = response.json()
error_detail += f" - {error_data}"
except:
error_detail += f" - {response.text}"
logger.error(f"Devin API error: {error_detail}")
raise HTTPException(status_code=response.status_code, detail=error_detail)
session_data = response.json()
# Extract session info
session_id = session_data.get("session_id") or session_data.get("id")
if not session_id:
raise HTTPException(status_code=500, detail="No session ID returned from Devin API")
# Remove "devin-" prefix if present for the URL
clean_session_id = session_id.replace("devin-", "") if session_id.startswith("devin-") else session_id
session_url = f"https://app.devin.ai/sessions/{clean_session_id}"
logger.info(f"Successfully created Devin session: {session_id}")
return DevinSessionResponse(
session_id=session_id,
session_url=session_url
)
except httpx.RequestError as e:
logger.error(f"Network error calling Devin API: {str(e)}")
raise HTTPException(status_code=503, detail=f"Failed to connect to Devin API: {str(e)}")
except Exception as e:
logger.error(f"Error creating Devin session: {str(e)}", exc_info=True)
raise HTTPException(status_code=500, detail=f"Failed to create Devin session: {str(e)}")
@app.post("/api/playground/start")
async def start_playground_task(
request: CodexPlaygroundRequest,
github_service: GitHubService = Depends(get_github_service)
):
"""Start a new Codex playground task in GitHub Actions."""
try:
import uuid
task_id = str(uuid.uuid4())
workflow_inputs = {
"prompt": request.prompt,
"task_id": task_id,
"azure_openai_endpoint": request.azure_openai_endpoint,
"azure_openai_key": request.azure_openai_key,
"azure_openai_deployment": request.azure_openai_deployment
}
success = await github_service.dispatch_workflow(
owner=request.owner,
repo=request.repo,
workflow_id="codex-playground.yml",
inputs=workflow_inputs
)
if not success:
raise HTTPException(status_code=500, detail="Failed to dispatch workflow")
return CodexPlaygroundResponse(
task_id=task_id,
status="queued"
)
except Exception as e:
logger.error(f"Error starting playground task: {str(e)}")
raise HTTPException(status_code=500, detail=str(e))
@app.get("/api/playground/status/{task_id}")
async def get_playground_status(
task_id: str,
owner: str,
repo: str,
github_service: GitHubService = Depends(get_github_service)
):
"""Get status of a playground task."""
try:
runs = await github_service.get_workflow_runs(owner, repo, "codex-playground.yml")
matching_run = None
for run in runs:
if task_id in str(run.get("html_url", "")):
matching_run = run
break
if not matching_run:
return PlaygroundTaskStatus(task_id=task_id, status="not_found")
return PlaygroundTaskStatus(
task_id=task_id,
status=matching_run["status"],
workflow_run_id=matching_run["id"],
logs_url=matching_run["html_url"]
)
except Exception as e:
logger.error(f"Error getting playground status: {str(e)}")
raise HTTPException(status_code=500, detail=str(e))
@app.get("/api/playground/logs/{task_id}")
async def get_playground_logs(
task_id: str,
owner: str,
repo: str,
github_service: GitHubService = Depends(get_github_service)
):
"""Get logs URL for a playground task."""
try:
runs = await github_service.get_workflow_runs(owner, repo, "codex-playground.yml")
matching_run = None
for run in runs:
if task_id in str(run.get("html_url", "")):
matching_run = run
break
if not matching_run:
raise HTTPException(status_code=404, detail="Task not found")
logs_url = await github_service.get_workflow_logs(owner, repo, matching_run["id"])
return {"logs_url": logs_url, "workflow_url": matching_run["html_url"]}
except Exception as e:
logger.error(f"Error getting playground logs: {str(e)}")
raise HTTPException(status_code=500, detail=str(e))