prthm11 commited on
Commit
e0500da
·
verified ·
1 Parent(s): 743d1c8

Update escalation_server.py

Browse files
Files changed (1) hide show
  1. escalation_server.py +110 -124
escalation_server.py CHANGED
@@ -187,89 +187,9 @@ async def get_teacher_info(subject: str, class_id: str) -> dict:
187
  }
188
 
189
  # --- RESEND EMAIL CONFIGURATION ---
190
- RESEND_API_KEY = os.getenv("RESEND_API_KEY")
191
-
192
-
193
- # @mcp.tool()
194
- # async def escalate_to_teacher(
195
- # teacher_name: str,
196
- # subject: str,
197
- # student_query: str,
198
- # ai_response: str,
199
- # ctx: Context = CurrentContext()
200
- # ) -> str:
201
- # """
202
- # Sends a real-time email via Resend API (HTTP-based).
203
- # Works on Hugging Face Spaces where SMTP is blocked.
204
- # """
205
-
206
- # # 1. Fetch recipient from your existing FACULTY_DATA
207
- # teacher_info = FACULTY_DATA.get(teacher_name)
208
- # if not teacher_info:
209
- # # Fallback to your own email for testing if teacher not found
210
- # recipient = "dummyuseai@gmail.com"
211
- # else:
212
- # recipient = teacher_info["email"]
213
-
214
- # # 2. Resend API setup
215
- # url = "https://api.resend.com/emails"
216
- # headers = {
217
- # "Authorization": f"Bearer {RESEND_API_KEY}",
218
- # "Content-Type": "application/json"
219
- # }
220
-
221
- # # NOTE: If you haven't verified a domain in Resend yet,
222
- # # you MUST use "onboarding@resend.dev" as the 'from' address.
223
- # payload = {
224
- # "from": "Tutor System <pratham.lakdawala@webashlar.com>",
225
- # "to": [recipient],
226
- # "subject": f"🚨 Escalation: {subject} Doubt for {teacher_name}",
227
- # "html": f"""
228
- # <html>
229
- # <body style="font-family: Arial, sans-serif; color: #333; line-height: 1.6;">
230
- # <div style="max-width: 600px; margin: auto; border: 1px solid #eee; padding: 20px;">
231
- # <h2 style="color: #d9534f; border-bottom: 2px solid #d9534f; padding-bottom: 10px;">
232
- # New Escalation: {subject}
233
- # </h2>
234
- # <p>Hello <strong>{teacher_name}</strong>,</p>
235
- # <p>A student has reached the maximum tutoring attempts. Here is the session context:</p>
236
- # <div style="background: #f9f9f9; padding: 15px; border-radius: 5px; margin: 20px 0;">
237
- # <p><strong>Student Doubt:</strong><br>{student_query}</p>
238
- # <hr style="border: 0; border-top: 1px solid #ccc;">
239
- # <p><strong>Last AI Response:</strong><br>{ai_response}</p>
240
- # </div>
241
- # <p style="font-size: 0.9em; color: #666;">
242
- # Please review this doubt and provide guidance on the student dashboard.
243
- # </p>
244
- # </div>
245
- # </body>
246
- # </html>
247
- # """
248
- # }
249
-
250
- # # 3. Send the request via HTTPS (Port 443 - never blocked)
251
- # async with httpx.AsyncClient() as client:
252
- # try:
253
- # response = await client.post(url, headers=headers, json=payload)
254
-
255
- # # Resend returns 200 or 201 on success
256
- # if response.status_code in [200, 201]:
257
- # # --- RESET ATTEMPTS ---
258
- # await ctx.set_state("math_attempts", 0)
259
-
260
- # # RE-ENABLE TOOLS: Using the correct 'names=' keyword
261
- # await ctx.enable_components(names=["explain_from_resource", "generate_quick_revision"])
262
-
263
- # return f"Success: {teacher_name} ({subject} Expert) has been notified via Email API."
264
- # else:
265
- # # Handle API-specific errors (e.g., unauthorized, over limit)
266
- # return f"Email API Error ({response.status_code}): {response.text}"
267
-
268
- # except Exception as e:
269
- # return f"Critical Network Error: Could not reach Email API. Details: {str(e)}"
270
-
271
  @mcp.tool()
272
-
273
  async def escalate_to_teacher(
274
  teacher_name: str,
275
  subject: str,
@@ -277,54 +197,120 @@ async def escalate_to_teacher(
277
  ai_response: str,
278
  ctx: Context = CurrentContext()
279
  ) -> str:
280
-
281
- """Sends a real-time email to the correct teacher by name."""
282
 
283
- # Safely get email from our central registry
284
  teacher_info = FACULTY_DATA.get(teacher_name)
 
285
 
286
- if not teacher_info:
287
- recipient = "dummyuseai@gmail.com" # Fallback to admin
288
- else:
289
- recipient = teacher_info["email"]
290
-
291
- msg = EmailMessage()
292
- msg["Subject"] = f"🚨 Escalation: {subject} Doubt for {teacher_name}"
293
- msg["From"] = SENDER_EMAIL
294
- msg["To"] = recipient
295
-
296
- html_content = f"""
297
  <html>
298
- <body style="font-family: Arial, sans-serif; color: #333;">
299
- <div style="background: #f9f9f9; padding: 20px; border: 1px solid #ddd;">
300
- <h2 style="color: #d9534f;">Hello {teacher_name},</h2>
301
- <p>A student has a <strong>{subject}</strong> doubt that requires your attention.</p>
302
- <hr>
303
- <p><strong>Doubt:</strong> {student_query}</p>
304
- <p><strong>AI Response:</strong> {ai_response}</p>
305
- </div>
306
- </body>
307
  </html>
308
  """
309
- msg.add_alternative(html_content, subtype="html")
310
-
311
- try:
312
- await aiosmtplib.send(msg,
313
- hostname=SMTP_SERVER,
314
- port=SMTP_PORT,
315
- username=SENDER_EMAIL,
316
- password=SENDER_PASSWORD,
317
- start_tls=True,
318
- use_tls=False,
319
- timeout=30
320
- )
321
- # --- THE FIX: RESET LOGIC ---
322
- # ctx.set_state("math_attempts", 0) # Reset counter to 0
323
- await ctx.set_state("math_attempts", 0)
324
- # await ctx.enable_components(tools=["explain_from_resource", "generate_quick_revision"]) # Re-enable tools
325
- return f"Success: {teacher_name} ({subject} Expert) has been notified."
326
- except Exception as e:
327
- return f"Error sending email: {str(e)}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
328
 
329
  # --- 4. MCP PROMPTS ---
330
  @mcp.prompt()
 
187
  }
188
 
189
  # --- RESEND EMAIL CONFIGURATION ---
190
+ # RESEND_API_KEY = os.getenv("RESEND_API_KEY")
191
+ VERCEL_EMAIL_API_URL = os.getenv("VERCEL_EMAIL_API_URL", "https://smtp-email-service-a61d.vercel.app/api/send_email")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
192
  @mcp.tool()
 
193
  async def escalate_to_teacher(
194
  teacher_name: str,
195
  subject: str,
 
197
  ai_response: str,
198
  ctx: Context = CurrentContext()
199
  ) -> str:
200
+ """Sends a real-time email via the Vercel SMTP Bridge API."""
 
201
 
202
+ # 1. Get recipient from registry
203
  teacher_info = FACULTY_DATA.get(teacher_name)
204
+ recipient = teacher_info["email"] if teacher_info else "dummyuseai@gmail.com"
205
 
206
+ # 2. Prepare the email content
207
+ email_subject = f"🚨 Escalation: {subject} Doubt for {teacher_name}"
208
+ html_body = f"""
 
 
 
 
 
 
 
 
209
  <html>
210
+ <body style="font-family: Arial, sans-serif; color: #333;">
211
+ <div style="background: #f9f9f9; padding: 20px; border: 1px solid #ddd;">
212
+ <h2 style="color: #d9534f;">Hello {teacher_name},</h2>
213
+ <p>A student has a <strong>{subject}</strong> doubt that requires your attention.</p>
214
+ <hr>
215
+ <p><strong>Doubt:</strong> {student_query}</p>
216
+ <p><strong>AI Response:</strong> {ai_response}</p>
217
+ </div>
218
+ </body>
219
  </html>
220
  """
221
+
222
+ # 3. Construct the payload for your Vercel API
223
+ # Based on your index.py: to, subject, body, is_html
224
+ api_payload = {
225
+ "to": recipient,
226
+ "subject": email_subject,
227
+ "body": html_body,
228
+ "is_html": True
229
+ }
230
+
231
+ # 4. Call the Vercel API via HTTPS
232
+ async with httpx.AsyncClient() as client:
233
+ try:
234
+ response = await client.post(
235
+ VERCEL_EMAIL_API_URL,
236
+ json=api_payload,
237
+ timeout=30.0
238
+ )
239
+
240
+ if response.status_code == 200:
241
+ # --- SUCCESS: RESET LOGIC ---
242
+ await ctx.set_state("math_attempts", 0)
243
+
244
+ # Re-enable tutoring tools if they were disabled
245
+ await ctx.enable_components(names=["explain_from_resource", "generate_quick_revision"])
246
+
247
+ return f"Success: {teacher_name} ({subject} Expert) has been notified via Vercel Bridge."
248
+ else:
249
+ # Handle API-level errors (400, 401, 502, etc.)
250
+ error_data = response.json()
251
+ return f"Vercel API Error ({response.status_code}): {error_data.get('error', 'Unknown error')}"
252
+
253
+ except httpx.ConnectError:
254
+ return "Critical Error: Could not connect to Vercel API. Check your VERCEL_EMAIL_API_URL."
255
+ except Exception as e:
256
+ return f"Unexpected Error: {str(e)}"
257
+
258
+ # @mcp.tool()
259
+ # async def escalate_to_teacher(
260
+ # teacher_name: str,
261
+ # subject: str,
262
+ # student_query: str,
263
+ # ai_response: str,
264
+ # ctx: Context = CurrentContext()
265
+ # ) -> str:
266
+
267
+ # """Sends a real-time email to the correct teacher by name."""
268
+
269
+ # # Safely get email from our central registry
270
+ # teacher_info = FACULTY_DATA.get(teacher_name)
271
+
272
+ # if not teacher_info:
273
+ # recipient = "dummyuseai@gmail.com" # Fallback to admin
274
+ # else:
275
+ # recipient = teacher_info["email"]
276
+
277
+ # msg = EmailMessage()
278
+ # msg["Subject"] = f"🚨 Escalation: {subject} Doubt for {teacher_name}"
279
+ # msg["From"] = SENDER_EMAIL
280
+ # msg["To"] = recipient
281
+
282
+ # html_content = f"""
283
+ # <html>
284
+ # <body style="font-family: Arial, sans-serif; color: #333;">
285
+ # <div style="background: #f9f9f9; padding: 20px; border: 1px solid #ddd;">
286
+ # <h2 style="color: #d9534f;">Hello {teacher_name},</h2>
287
+ # <p>A student has a <strong>{subject}</strong> doubt that requires your attention.</p>
288
+ # <hr>
289
+ # <p><strong>Doubt:</strong> {student_query}</p>
290
+ # <p><strong>AI Response:</strong> {ai_response}</p>
291
+ # </div>
292
+ # </body>
293
+ # </html>
294
+ # """
295
+ # msg.add_alternative(html_content, subtype="html")
296
+
297
+ # try:
298
+ # await aiosmtplib.send(msg,
299
+ # hostname=SMTP_SERVER,
300
+ # port=SMTP_PORT,
301
+ # username=SENDER_EMAIL,
302
+ # password=SENDER_PASSWORD,
303
+ # start_tls=True,
304
+ # use_tls=False,
305
+ # timeout=30
306
+ # )
307
+ # # --- THE FIX: RESET LOGIC ---
308
+ # # ctx.set_state("math_attempts", 0) # Reset counter to 0
309
+ # await ctx.set_state("math_attempts", 0)
310
+ # # await ctx.enable_components(tools=["explain_from_resource", "generate_quick_revision"]) # Re-enable tools
311
+ # return f"Success: {teacher_name} ({subject} Expert) has been notified."
312
+ # except Exception as e:
313
+ # return f"Error sending email: {str(e)}"
314
 
315
  # --- 4. MCP PROMPTS ---
316
  @mcp.prompt()