Neural Arun commited on
Commit
cde68a1
·
1 Parent(s): ecad036

added diagnostic route

Browse files
Files changed (2) hide show
  1. core/agent.py +8 -0
  2. core/api.py +26 -0
core/agent.py CHANGED
@@ -347,6 +347,10 @@ def queue_chat_history_to_telegram(
347
  user_input: str,
348
  assistant_response: str,
349
  ) -> str:
 
 
 
 
350
  if _submit_background_task("chat_history_log", send_chat_history_to_telegram, session_id, user_input, assistant_response):
351
  return "QUEUED: chat history scheduled."
352
  return "FAILED: could not queue chat history."
@@ -423,6 +427,10 @@ def _attempt_notify_arun_with_retry_queue(
423
  user_input: str,
424
  user_metadata_json: str = "",
425
  ) -> str:
 
 
 
 
426
  submitted = _submit_background_task(
427
  "notify_arun_bg",
428
  _deliver_notify_arun,
 
347
  user_input: str,
348
  assistant_response: str,
349
  ) -> str:
350
+ token, chat_id = _get_telegram_target(debug=False)
351
+ if not token or not chat_id:
352
+ return "FAILED: Telegram credentials are missing from the environment."
353
+
354
  if _submit_background_task("chat_history_log", send_chat_history_to_telegram, session_id, user_input, assistant_response):
355
  return "QUEUED: chat history scheduled."
356
  return "FAILED: could not queue chat history."
 
427
  user_input: str,
428
  user_metadata_json: str = "",
429
  ) -> str:
430
+ token, chat_id = _get_telegram_target(debug=False)
431
+ if not token or not chat_id:
432
+ return "FAILED: Telegram credentials are missing from the environment."
433
+
434
  submitted = _submit_background_task(
435
  "notify_arun_bg",
436
  _deliver_notify_arun,
core/api.py CHANGED
@@ -93,6 +93,8 @@ async def chat_endpoint(req: ChatRequest):
93
  pre_escalation_status = "Notification was not confirmed immediately. Retrying in background."
94
  elif "QUEUED" in pre_escalation_result:
95
  pre_escalation_status = "Sending notification to Arun in the background."
 
 
96
  else:
97
  pre_escalation_status = "Notification could not be confirmed."
98
 
@@ -209,11 +211,35 @@ async def chat_endpoint(req: ChatRequest):
209
  return StreamingResponse(event_generator(), media_type="application/x-ndjson")
210
 
211
 
 
 
 
 
212
  @app.get("/health")
213
  async def health_check():
214
  return {"status": "online", "active_sessions": len(active_sessions)}
215
 
216
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
217
  if __name__ == "__main__":
218
  import uvicorn
219
  uvicorn.run("core.api:app", host="0.0.0.0", port=8000, reload=True)
 
93
  pre_escalation_status = "Notification was not confirmed immediately. Retrying in background."
94
  elif "QUEUED" in pre_escalation_result:
95
  pre_escalation_status = "Sending notification to Arun in the background."
96
+ elif "credentials are missing" in pre_escalation_result:
97
+ pre_escalation_status = "Error: Please set TELEGRAM_BOT_TOKEN and TELEGRAM_CHAT_ID in HuggingFace Spaces Settings!"
98
  else:
99
  pre_escalation_status = "Notification could not be confirmed."
100
 
 
211
  return StreamingResponse(event_generator(), media_type="application/x-ndjson")
212
 
213
 
214
+
215
+ return StreamingResponse(event_generator(), media_type="application/x-ndjson")
216
+
217
+
218
  @app.get("/health")
219
  async def health_check():
220
  return {"status": "online", "active_sessions": len(active_sessions)}
221
 
222
 
223
+ @app.get("/test-telegram")
224
+ def test_telegram():
225
+ import os, requests, traceback
226
+ token = os.getenv("TELEGRAM_BOT_TOKEN")
227
+ chat_id = os.getenv("TELEGRAM_CHAT_ID")
228
+ if not token or not chat_id:
229
+ return {"status": "error", "message": "Missing credentials", "has_token": bool(token), "has_chat_id": bool(chat_id)}
230
+
231
+ token = token.strip(' "\'')
232
+ chat_id = chat_id.strip(' "\'')
233
+ url = f"https://api.telegram.org/bot{token}/sendMessage"
234
+ payload = {"chat_id": chat_id, "text": "Test message from HuggingFace backend!"}
235
+
236
+ try:
237
+ response = requests.post(url, json=payload, timeout=10)
238
+ return {"status": "finished", "status_code": response.status_code, "response": response.text}
239
+ except Exception as e:
240
+ return {"status": "exception", "error": str(e), "traceback": traceback.format_exc()}
241
+
242
+
243
  if __name__ == "__main__":
244
  import uvicorn
245
  uvicorn.run("core.api:app", host="0.0.0.0", port=8000, reload=True)