-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
409 lines (361 loc) · 13.9 KB
/
main.py
File metadata and controls
409 lines (361 loc) · 13.9 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
from fastapi import FastAPI, HTTPException, UploadFile, File, Form, Request, Depends
from fastapi.exception_handlers import RequestValidationError
from fastapi.responses import JSONResponse
from fastapi.exceptions import RequestValidationError
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel
from typing import List, Optional
from db_utils import insert_record, update_conversation_title, upload_file, update_draft_reply
from crew import manager_orchestrator
import base64
import json
from jwt_auth import verify_jwt_token
from dotenv import load_dotenv
from logging import basicConfig, getLogger
import logging
from fastapi import Body
from fastapi.responses import JSONResponse
import datetime
from tools.reply_email_tool import reply_to_latest_email
from tzlocal import get_localzone_name
local_tz = get_localzone_name()
current_utc_date = datetime.datetime.utcnow().strftime("%Y-%m-%d %H:%M UTC")
load_dotenv()
basicConfig(level=logging.INFO)
logger = getLogger(__name__)
# Initialize FastAPI without global auth dependency
app = FastAPI()
# Log validation errors for easier debugging
@app.exception_handler(RequestValidationError)
async def validation_exception_handler(request: Request, exc: RequestValidationError):
logger.error(f"422 Validation error for {request.url}: {exc.errors()}")
return JSONResponse(
status_code=422,
content={"detail": exc.errors(), "body": exc.body},
)
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# Request/response models
class QuestionRequest(BaseModel):
type: str
question: str
sender: str
conversation_id: int
class AnswerResponse(BaseModel):
type: str
question: str
answer: str
class IncomingEmailEvent(BaseModel):
"""
Model for an incoming‐email webhook. The JSON sent here must include:
• id (the Graph mail ID)
• subject
• bodyPreview
• sender (sender’s email address)
• receiver (recipient’s email address = our user’s mailbox)
• (optional) userId – internal user ID, if we know it
"""
type: str
id: str
subject: str
bodyPreview: str
body: str
sender: str
receiver: str
userId: Optional[int] = None
receivedDateTime: Optional[str] = None
from fastapi import Form, File, UploadFile # <— make sure these are imported
import base64
# …
@app.post("/ask", dependencies=[Depends(verify_jwt_token)], response_model=AnswerResponse)
async def ask_question(
type: str = Form(...),
conversation_id: str = Form(...),
sender: str = Form(...),
question: str = Form(...),
files: List[UploadFile] = File(default=None),
):
"""
Accepts:
• type – what kind of request this is (e.g. "send_email", "ask_question")
• conversation_id – your internal thread ID
• sender – the user’s email or identifier
• question – the free-text prompt
• files – zero or more file attachments
"""
try:
# 1) Read & encode attachments, if any
attachments = []
if files:
for f in files:
data = await f.read()
attachments.append({
"filename": f.filename,
"size": len(data),
"content": base64.b64encode(data).decode("utf-8"),
"content_type": f.content_type or "application/octet-stream",
})
# 2) Build the payload for your orchestrator
orchestrator_input = {
"type": type,
"conversation_id": conversation_id,
"sender": sender,
"question": question,
"attachments": attachments or None,
}
# 3) Dispatch to crew.py
result = manager_orchestrator(orchestrator_input)
return result
except Exception as e:
logger.exception("Error in /ask endpoint with form data:")
raise HTTPException(status_code=500, detail=str(e))
# Incoming email webhook: no auth required
@app.post("/incoming_email")
def incoming_email(event: IncomingEmailEvent):
"""
Receives a webhook POST whenever a new email arrives. The body must match IncomingEmailEvent.
We forward the payload into manager_orchestrator, which will run email_task_pipeline and return
whatever the pipeline returned (either “dropped” or a list of tasks).
"""
try:
orchestrator_input = event.dict()
result = manager_orchestrator(orchestrator_input)
return {"status": "ok", "detail": result}
except Exception as e:
logging.exception("Error in /incoming_email endpoint:")
raise HTTPException(status_code=500, detail=str(e))
# File upload endpoint: protected
@app.post("/upload", dependencies=[Depends(verify_jwt_token)])
def upload_file_endpoint(file: UploadFile = File(...)):
try:
contents = file.file.read()
b64data = base64.b64encode(contents).decode("utf-8")
upload_file(file.filename, b64data) # reuse upload logic
return {"filename": file.filename}
except Exception as e:
logging.exception("Error uploading file:")
raise HTTPException(status_code=500, detail=str(e))
# Draft reply endpoint: protected
from crew import email_draft_reply_crew
from pydantic import BaseModel
class DraftReplyRequest(BaseModel):
sender: str
mail_id: str
question: str
attachments: List[str] = []
@app.post("/replyMail", dependencies=[Depends(verify_jwt_token)])
async def draft_reply_endpoint(
question: str = Form(...),
sender: str = Form(...),
files: List[UploadFile] = File(default=None),
mail_id: str = Form(...)):
"""
Endpoint to send an email using the orchestrated pipeline.
Accepts: {question, sender, attachments}
"""
try:
# 1) Read & encode attachments, if any
attachments = []
if files:
for f in files:
data = await f.read()
attachments.append({
"filename": f.filename,
"size": len(data),
"content": base64.b64encode(data).decode("utf-8"),
"content_type": f.content_type or "application/octet-stream",
})
# 2) Build the payload for your orchestrator
send_payload = {
"question": question,
"sender_email": sender,
"attachments": attachments or None,
"mail_id": mail_id
}
result = reply_to_latest_email.run(
sender_email=sender,
comment=question,
attachment=attachments if attachments else None,
mail_id=mail_id
)
if result:
answer = f"Email was sent to {sender} successfully"
else:
answer = "Sorry, there is problem occured. Failed to Sent Email"
return JSONResponse(content={"type": "email_sent", "question": question, "answer": answer})
except Exception as e:
logger.exception("Error in /draftReply endpoint:")
raise HTTPException(status_code=500, detail=str(e))
# Preview draft reply endpoint: protected
class PreviewDraftReplyRequest(BaseModel):
sender: str
mail_id: str
@app.post("/draftReplyPreview", dependencies=[Depends(verify_jwt_token)])
def preview_draft_reply_endpoint(
mail_id: str = Form(...),
sender: str = Form(...)):
"""
Endpoint to preview a draft reply using the email_draft_reply_crew.
Accepts: {task_id, sender}
"""
try:
payload = {
"mail_id": mail_id,
"sender_email": sender
}
result = email_draft_reply_crew.kickoff(inputs=payload)
answer = getattr(result, "output", str(result))
return JSONResponse(content={
"type": "draft_preview",
"answer": answer
})
except Exception as e:
logger.exception("Error in /draftReplyPreview endpoint:")
raise HTTPException(status_code=500, detail=str(e))
class SendEmailRequest(BaseModel):
question: str
sender: str
attachments: Optional[list] = None
@app.post("/sendEmail", dependencies=[Depends(verify_jwt_token)])
async def send_email_endpoint(
question: str = Form(...),
sender: str = Form(...),
files: List[UploadFile] = File(default=None),
):
"""
Endpoint to send an email using the orchestrated pipeline.
Accepts: {question, sender, attachments}
"""
try:
# 1) Read & encode attachments, if any
attachments = []
if files:
for f in files:
data = await f.read()
attachments.append({
"filename": f.filename,
"size": len(data),
"content": base64.b64encode(data).decode("utf-8"),
"content_type": f.content_type or "application/octet-stream",
})
# 2) Build the payload for your orchestrator
send_payload = {
"question": question,
"sender_email": sender,
"attachments": attachments or None,
}
logger.info(f"[Orchestrator] Sending email: {send_payload}")
send_result = email_onboard_crew.kickoff(inputs=send_payload)
raw_output = str(send_result)
# Strip triple backticks and markdown labeling if present
clean_output = raw_output.strip().removeprefix('"json').removesuffix('"""').strip()
# Now parse clean JSON
final_json = json.loads(clean_output)
sender = sender
receiver = final_json.get("receiver")
subject = final_json.get("subject")
content = final_json.get("content")
attachments = attachments
result = send_email.run(
sender=sender,
receiver=receiver,
subject=subject,
content=question,
attachments=attachments if attachments else None
)
if result:
answer = f"Email was sent to {receiver} successfully"
else:
answer = "Sorry, there is problem occured. Failed to Sent Email"
return JSONResponse(content={"type": "email_sent", "question": question, "answer": answer})
except Exception as e:
logger.exception("Error in /sendEmail endpoint:")
raise HTTPException(status_code=500, detail=str(e))
# ──────────────────────────────────────────────────────────────
# Reminder Endpoint (from crew.py logic)
# ──────────────────────────────────────────────────────────────
from crew import email_onboard_crew, reminder_crew, reminder_todo_crew
from crew import send_email
class ReminderRequest(BaseModel):
sender: str
question: str
@app.post("/todoTask", dependencies=[Depends(verify_jwt_token)])
def todo_task_endpoint(request: ReminderRequest = Body(...)):
"""
Endpoint to create a Microsoft To Do reminder task for a user.
Accepts: {email, task_title, task_body (optional), due_date_time (optional)}
"""
try:
input_payload = request.dict()
# Prepare the payload for the crew
reminder_payload = {
"sender": input_payload.get("sender"),
"question": input_payload.get("question"),
"current_date": current_utc_date,
}
result = reminder_todo_crew.kickoff(inputs=reminder_payload)
answer = getattr(result, "output", str(result))
return JSONResponse(content={
"type": "reminder_created",
"question": input_payload.get("question"),
"answer": answer
})
except Exception as e:
logger.exception("Error in /todoTask endpoint:")
raise HTTPException(status_code=500, detail=str(e))
@app.post("/reminder", dependencies=[Depends(verify_jwt_token)])
def reminder_endpoint(request: ReminderRequest = Body(...)):
"""
Endpoint to create a Microsoft To Do reminder task for a user.
Accepts: {email, task_title, task_body (optional), due_date_time (optional)}
"""
try:
input_payload = request.dict()
# Prepare the payload for the crew
reminder_payload = {
"sender": input_payload.get("sender"),
"question": input_payload.get("question"),
}
result = reminder_crew.kickoff(inputs=reminder_payload)
answer = getattr(result, "output", str(result))
return JSONResponse(content={
"type": "reminder_created",
"question": input_payload.get("question"),
"answer": answer
})
except Exception as e:
logger.exception("Error in /reminder endpoint:")
raise HTTPException(status_code=500, detail=str(e))
class EventRequest(BaseModel):
sender: str
question: str
@app.post("/event", dependencies=[Depends(verify_jwt_token)])
def event_endpoint(request: EventRequest = Body(...)):
"""
Endpoint to create a calendar event or event reminder for a user.
Accepts: {sender, question}
"""
try:
input_payload = request.dict()
event_payload = {
"sender": input_payload.get("sender"),
"question": input_payload.get("question"),
"current_date": current_utc_date,
"local_tz": local_tz
}
from crew import reminder_event_crew
result = reminder_event_crew.kickoff(inputs=event_payload)
answer = getattr(result, "output", str(result))
return JSONResponse(content={
"type": "event_created",
"question": input_payload.get("question"),
"answer": answer
})
except Exception as e:
logger.exception("Error in /event endpoint:")
raise HTTPException(status_code=500, detail=str(e))