-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_fastapi.py
More file actions
507 lines (397 loc) · 16.1 KB
/
Copy pathmain_fastapi.py
File metadata and controls
507 lines (397 loc) · 16.1 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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
# main_api.py (FastAPI backend)
from fastapi import FastAPI, Request, HTTPException, Query
from fastapi.responses import RedirectResponse, JSONResponse
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel
from math_ai_agent_doc import process_input # import your function (now uses Claude CLI)
from claude_cli_client import call_llm
from fastapi import UploadFile, File
from rag_log_analyzer import build_vectorstore, get_qa_chain, build_vectorstore_from_all_logs
import os, shutil, json, pytz, requests, httpx
from PyPDF2 import PdfReader
#------------For Calendar --------------
from google_auth_oauthlib.flow import Flow
from google.oauth2.credentials import Credentials
from googleapiclient.discovery import build
from typing import Optional
from datetime import datetime, timedelta
#------------For Model Training--------------
from training_store import save_issue_resolution, find_similar_issues, load_training_data, TRAINING_FILE
#------------For PR Review--------------
from pr_review import handle_pull_request
app = FastAPI()
UPLOAD_DIR = "uploaded_docs"
os.makedirs(UPLOAD_DIR, exist_ok=True)
# -------------------------------
# Calendar Configuration
# -------------------------------
CLIENT_SECRETS_FILE = "credentials_calendar.json" # Download from Google Cloud
SCOPES = ["https://www.googleapis.com/auth/calendar"]
REDIRECT_URI = "http://localhost:8000/oauth2callback"
TOKEN_FILE = "token.json"
#os.environ["OAUTHLIB_INSECURE_TRANSPORT"] = "1" # Allow HTTP for local dev
def build_service():
creds = None
if os.path.exists(TOKEN_FILE):
creds = Credentials.from_authorized_user_file(TOKEN_FILE, [SCOPES])
if not creds or not creds.valid:
raise Exception("Invalid or missing credentials. Please authorize first.")
return build("calendar", "v3", credentials=creds)
# -------------------------------
# ✅ STEP 1: AUTHORIZATION URL
# -------------------------------
@app.get("/authorize-calendar")
def authorize_calendar():
flow = Flow.from_client_secrets_file(
CLIENT_SECRETS_FILE,
scopes=SCOPES,
redirect_uri=REDIRECT_URI
)
auth_url, state = flow.authorization_url(prompt="consent")
return RedirectResponse(auth_url)
# -------------------------------
# ✅ STEP 2: CALLBACK HANDLER
# -------------------------------
@app.get("/oauth2callback")
def oauth2callback(request: Request):
try:
# Extract query params
code = request.query_params.get("code")
if not code:
raise HTTPException(status_code=400, detail="Missing authorization code")
flow = Flow.from_client_secrets_file(
CLIENT_SECRETS_FILE,
scopes=SCOPES,
redirect_uri=REDIRECT_URI
)
# Fetch token using the authorization code
flow.fetch_token(code=code)
credentials = flow.credentials
# Save credentials to token.json for reuse
with open(TOKEN_FILE, "w") as token:
token.write(credentials.to_json())
return JSONResponse({"message": "Authorization successful. You can now use Calendar API."})
except Exception as e:
print(f"OAuth Error: {str(e)}")
raise HTTPException(status_code=500, detail="OAuth failed")
# -------------------------------
# ✅ STEP 3: LIST UPCOMING EVENTS
# -------------------------------
@app.get("/get-events")
def get_events():
if not os.path.exists(TOKEN_FILE):
raise HTTPException(status_code=400, detail="Authorize first at /authorize-calendar")
creds = Credentials.from_authorized_user_file(TOKEN_FILE, SCOPES)
try:
service = build("calendar", "v3", credentials=creds)
events_result = service.events().list(
calendarId="primary",
maxResults=10,
singleEvents=True,
orderBy="startTime"
).execute()
events = events_result.get("items", [])
if not events:
return {"message": "No upcoming events found."}
formatted_events = []
for event in events:
start = event["start"].get("dateTime", event["start"].get("date"))
formatted_events.append({"summary": event["summary"], "start": start})
return {"events": formatted_events}
except Exception as e:
print(f"Calendar API Error: {str(e)}")
raise HTTPException(status_code=500, detail="Failed to fetch events")
# -------------------------------
# ✅ STEP 4: CREATE EVENT
# -------------------------------
@app.post("/create-event")
async def create_event(request: Request):
if not os.path.exists(TOKEN_FILE):
raise HTTPException(status_code=400, detail="Authorize first at /authorize-calendar")
creds = Credentials.from_authorized_user_file(TOKEN_FILE, SCOPES)
try:
data = await request.json()
summary = data.get("summary")
start_time = data.get("start")
end_time = data.get("end")
if not (summary and start_time and end_time):
raise HTTPException(status_code=400, detail="Missing required fields")
service = build("calendar", "v3", credentials=creds)
event = {
"summary": summary,
"start": {"dateTime": start_time, "timeZone": "Asia/Kolkata"},
"end": {"dateTime": end_time, "timeZone": "Asia/Kolkata"},
}
created_event = service.events().insert(calendarId="primary", body=event).execute()
return {"message": "Event created", "eventLink": created_event.get("htmlLink")}
except Exception as e:
print(f"Create Event Error: {str(e)}")
raise HTTPException(status_code=500, detail="Failed to create event")
# -------------------------------
# ✅ STEP 5 : Retrieve events by date
# -------------------------------
@app.get("/get-events-by-date")
def get_events_by_date(date: str = Query(..., description="Date in YYYY-MM-DD format")):
try:
service = build_service()
# Parse input date
user_date = datetime.strptime(date, "%Y-%m-%d")
timezone = pytz.timezone("Asia/Kolkata") # Change as needed
start_of_day = timezone.localize(datetime(user_date.year, user_date.month, user_date.day, 0, 0, 0))
end_of_day = start_of_day + timedelta(days=1)
events_result = service.events().list(
calendarId="primary",
timeMin=start_of_day.isoformat(),
timeMax=end_of_day.isoformat(),
singleEvents=True,
orderBy="startTime"
).execute()
events = events_result.get("items", [])
if not events:
return {"message": f"No events found for {date}"}
print('events -> {}'.format(events[0]))
formatted_events = [
{
"summary": event.get("summary", "No Title"),
"start": event.get("start", {}).get("dateTime", event.get("start", {}).get("date")),
"end": event.get("end", {}).get("dateTime", event.get("end", {}).get("date")),
"link": event.get("hangoutLink", ""),
"attendees": [a.get("email") for a in event.get("attendees", [])] if event.get("attendees") else [],
"location": event.get("location", "N/A"),
"id": event.get("id")
}
for event in events
]
return {"date": date, "events": formatted_events}
except Exception as e:
print("Error:", str(e))
return {"error": str(e)}
# -------------------------------
# ✅ Request model for scheduling
# -------------------------------
class ScheduleRequest(BaseModel):
title: str
start_time: str
end_time: str
description: str = None
location: str = None
attendees: list[str] = []
create_meet_link: bool = False
@app.post("/schedule-meeting")
async def schedule_meeting(req: ScheduleRequest):
try:
service = build_service()
event_body = {
"summary": req.title,
"description": req.description,
"start": {"dateTime": req.start_time, "timeZone": "Asia/Kolkata"}, # Change TZ as needed
"end": {"dateTime": req.end_time, "timeZone": "Asia/Kolkata"},
"location": req.location,
"attendees": [{"email": email} for email in req.attendees],
}
if req.create_meet_link:
event_body["conferenceData"] = {
"createRequest": {"requestId": f"meet-{os.urandom(4).hex()}"}
}
print('event_body is {}'.format(event_body))
event = service.events().insert(
calendarId="primary",
body=event_body,
conferenceDataVersion=1 if req.create_meet_link else 0,
).execute()
return {
"message": "{} scheduled successfully".format(event.get("summary")),
"event_id": event["id"],
"html_link": event.get("htmlLink"),
"meet_link": event.get("hangoutLink")
}
except Exception as e:
raise HTTPException(status_code=500, detail=f"Error scheduling meeting: {str(e)}")
# -------------------------------
# ✅ Delete event by ID
# -------------------------------
@app.delete("/delete-event")
async def delete_event(event_id: str = Query(..., description="ID of the event to delete")):
try:
service = build_service()
service.events().delete(calendarId="primary", eventId=event_id).execute()
return {"message": f"Event {event_id} deleted successfully"}
except Exception as e:
raise HTTPException(status_code=500, detail=f"Error deleting event: {str(e)}")
#------------------end of Calendar support-----------------
# Allow local React frontend
app.add_middleware(
CORSMiddleware,
allow_origins=["http://localhost:3000"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
class QueryRequest(BaseModel):
query: str
'''
@app.post("/ask")
async def ask_agent(req: QueryRequest):
response = process_input(req.query)
return {"response": response}
'''
@app.post("/ask")
#async def ask(query: dict):
async def ask(req: QueryRequest):
#question = query.get("query", "").lower()
question = req.query.lower()
# Heuristics to detect if it's a log-related query
log_keywords = ["log", "error", "stacktrace", "traceback", "exception", "debug", "crash", "warning", "failure"]
if any(kw in question for kw in log_keywords):
# Use RAG for log-related query
qa = get_qa_chain()
result = qa.run(question)
else:
result = await process_input(req.query)
return {"response": result}
def extract_text(file_path: str) -> str:
if file_path.endswith(".pdf"):
reader = PdfReader(file_path)
return "\n".join(page.extract_text() or "" for page in reader.pages)
elif file_path.endswith(".txt"):
with open(file_path, "r", encoding="utf-8") as f:
return f.read()
else:
return "Unsupported file format"
@app.post("/upload")
async def upload_file(file: UploadFile = File(...)):
file_path = os.path.join(UPLOAD_DIR, file.filename)
with open(file_path, "wb") as f:
f.write(await file.read())
text = extract_text(file_path)
analysis_prompt = f"Please summarize the following document:\n\n{text[:4000]}" # limit size
# Uses Claude API via call_llm wrapper
summary = call_llm(analysis_prompt)
return {"summary": summary.strip()}
@app.post("/upload-log")
async def upload_log(file: UploadFile = File(...)):
os.makedirs("logs", exist_ok=True)
filepath = f"logs/{file.filename}"
with open(filepath, "wb") as buffer:
shutil.copyfileobj(file.file, buffer)
#build_vectorstore(filepath)
build_vectorstore_from_all_logs() # Consolidated across all logs
#return {"summary": f"{file.filename} uploaded and indexed successfully."}
return {"summary": f"{file.filename} uploaded and all logs re-indexed."}
@app.post("/analyze-log")
async def analyze_log(query: dict):
question = query.get("query", "")
qa = get_qa_chain()
result = qa.run(question)
return {"response": result}
def process_training_query(user_query):
matches = find_similar_issues(user_query)
if matches:
context = "\n".join([f"Issue: {m['issue']}\nResolution: {m['resolution']}" for m in matches])
prompt = f"""You are a helpful assistant. The user is troubleshooting an issue.
Here are similar past cases:
{context}
Now suggest the best resolution for the following new issue:
{user_query}
"""
else:
prompt = f"""You are a helpful assistant. Suggest a resolution for the following issue:
{user_query}"""
print ('Claude resp is {}'.format(prompt))
response = call_llm(prompt) # Uses Claude via wrapper
return response
@app.post("/train-model")
async def train_model(data: dict):
save_issue_resolution(data["issue"], data["resolution"])
return {"status": "Saved"}
@app.post("/suggest-resolution")
async def suggest_resolution(data: dict):
result = process_training_query(data["query"])
print ('backend process_training_query returns {}'.format(result))
return {"suggestion": result}
@app.get("/get-training-history")
async def get_training_history():
data = load_training_data()
return JSONResponse(content={"history": data})
@app.delete("/clear-training-history")
def clear_training_history():
with open(TRAINING_FILE, "w") as f:
f.write("[]")
return {"message": "Training history cleared."}
#-----------------handle PR review-------------------------
@app.post("/webhook")
async def github_webhook(request: Request):
payload = await request.json()
pr_url = payload["pull_request"]["html_url"]
branch = payload["pull_request"]["head"]["ref"]
# ✅ Use fork's repo clone URL if it's a fork
token = os.getenv("GITHUB_TOKEN")
repo_url = payload["repository"]["clone_url"]
await handle_pull_request(repo_url, branch, pr_url)
return {"status": "PR received and processed"}
class CommentRequest(BaseModel):
pr_url: str
comment: str
@app.post("/comment")
async def post_comment(req: CommentRequest):
token = os.getenv("GITHUB_TOKEN")
if not token:
return {"error": "Missing GitHub token"}
parts = req.pr_url.split("/")
owner, repo, pr_number = parts[3], parts[4], parts[6]
url = f"https://api.github.com/repos/{owner}/{repo}/issues/{pr_number}/comments"
headers = {
"Authorization": f"Bearer {token}",
"Accept": "application/vnd.github+json",
}
data = {"body": req.comment}
r = requests.post(url, headers=headers, json=data)
return r.json()
#-----------------generate PR Review Comment-------------------------
class PRUrlRequest(BaseModel):
pr_url: str
def extract_pr_info(pr_url: str):
# e.g., https://github.com/user/repo/pull/123
try:
parts = pr_url.strip().split("/")
owner = parts[3]
repo = parts[4]
pr_number = int(parts[6])
return owner, repo, pr_number
except Exception:
return None, None, None
def get_diff_from_github(owner, repo, pr_number, token):
headers = {
"Authorization": f"Bearer {token}",
"Accept": "application/vnd.github.v3.diff"
}
url = f"https://api.github.com/repos/{owner}/{repo}/pulls/{pr_number}"
response = httpx.get(url, headers=headers)
return response.text if response.status_code == 200 else ""
def generate_comment_with_claude(diff_text: str):
prompt = f"""
You are a helpful code reviewer.
Review the following pull request diff and generate a concise GitHub comment addressing:
- Code quality
- Error handling
- Testing coverage
- Best practices
Only include one paragraph with clear suggestions, not excessive praise.
PR Diff:
{diff_text}
"""
return call_llm(prompt) # Uses Claude via wrapper
@app.post("/generate-comment")
async def generate_comment(req: PRUrlRequest):
token = os.getenv("GITHUB_TOKEN")
if not token:
return {"error": "Missing GITHUB_TOKEN in environment"}
owner, repo, pr_number = extract_pr_info(req.pr_url)
if not all([owner, repo, pr_number]):
return {"error": "Invalid PR URL format"}
diff = get_diff_from_github(owner, repo, pr_number, token)
if not diff.strip():
return {"error": "Failed to retrieve PR diff"}
comment = generate_comment_with_claude(diff)
print (comment)
return {"comment": comment}