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

Update escalation_server.py

Browse files
Files changed (1) hide show
  1. escalation_server.py +123 -65
escalation_server.py CHANGED
@@ -190,83 +190,141 @@ async def get_teacher_info(subject: str, class_id: str) -> dict:
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
  # --- 4. MCP PROMPTS ---
272
  @mcp.prompt()
 
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,
276
+ student_query: str,
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()