Spaces:
Sleeping
Sleeping
Neural Arun commited on
Commit ·
bbf7f4c
1
Parent(s): d51f529
fixed telegram notification
Browse files- core/agent.py +56 -46
- core/api.py +1 -1
- core/bot.py +1 -1
core/agent.py
CHANGED
|
@@ -91,10 +91,13 @@ def _should_send_alert(category: str, user_input: str) -> bool:
|
|
| 91 |
last_seen = _RECENT_ALERTS.get(key)
|
| 92 |
if last_seen and (now - last_seen) < NOTIFICATION_COOLDOWN_SECONDS:
|
| 93 |
return False
|
| 94 |
-
_RECENT_ALERTS[key] = now
|
| 95 |
return True
|
| 96 |
|
| 97 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
def _escape_html(text: str) -> str:
|
| 99 |
return html.escape(text or "")
|
| 100 |
|
|
@@ -152,35 +155,40 @@ def _send_telegram_message(
|
|
| 152 |
"parse_mode": parse_mode,
|
| 153 |
"disable_web_page_preview": True,
|
| 154 |
}
|
|
|
|
|
|
|
| 155 |
|
| 156 |
last_error = "Unknown Telegram error."
|
| 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 |
print(f"[TELEGRAM ERROR] {last_error}")
|
| 186 |
return f"FAILED: {last_error}"
|
|
@@ -318,6 +326,7 @@ def _deliver_notify_arun(category: str, user_input: str, user_metadata_json: str
|
|
| 318 |
text=text,
|
| 319 |
)
|
| 320 |
if result.startswith("SUCCESS"):
|
|
|
|
| 321 |
return "SUCCESS: Arun has been notified."
|
| 322 |
return result
|
| 323 |
|
|
@@ -802,25 +811,26 @@ def _run_pre_escalation(
|
|
| 802 |
reason=reason,
|
| 803 |
user_metadata=user_metadata,
|
| 804 |
)
|
| 805 |
-
tool_func = tool_map.get("notify_arun")
|
| 806 |
-
if not tool_func:
|
| 807 |
-
return {
|
| 808 |
-
"handled": False,
|
| 809 |
-
"category": category,
|
| 810 |
-
"reason": reason,
|
| 811 |
-
"result": "SKIPPED: notify_arun tool unavailable.",
|
| 812 |
-
}
|
| 813 |
-
|
| 814 |
if background:
|
| 815 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 816 |
return {
|
| 817 |
-
"handled":
|
| 818 |
"category": category,
|
| 819 |
"reason": reason,
|
| 820 |
-
"result":
|
| 821 |
}
|
| 822 |
|
| 823 |
-
result =
|
|
|
|
|
|
|
|
|
|
|
|
|
| 824 |
return {
|
| 825 |
"handled": result.startswith("SUCCESS") or result.startswith("SKIPPED"),
|
| 826 |
"category": category,
|
|
@@ -873,10 +883,6 @@ def maybe_notify_arun(
|
|
| 873 |
if not should_notify or used_notify_tool:
|
| 874 |
return None
|
| 875 |
|
| 876 |
-
tool_func = tool_map.get("notify_arun")
|
| 877 |
-
if not tool_func:
|
| 878 |
-
return "SKIPPED: notify_arun tool unavailable."
|
| 879 |
-
|
| 880 |
payload = _build_notify_payload(
|
| 881 |
category=category,
|
| 882 |
user_input=user_input,
|
|
@@ -885,7 +891,11 @@ def maybe_notify_arun(
|
|
| 885 |
assistant_output=final_response,
|
| 886 |
)
|
| 887 |
|
| 888 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
| 889 |
|
| 890 |
|
| 891 |
def queue_maybe_notify_arun(
|
|
|
|
| 91 |
last_seen = _RECENT_ALERTS.get(key)
|
| 92 |
if last_seen and (now - last_seen) < NOTIFICATION_COOLDOWN_SECONDS:
|
| 93 |
return False
|
|
|
|
| 94 |
return True
|
| 95 |
|
| 96 |
|
| 97 |
+
def _mark_alert_sent(category: str, user_input: str) -> None:
|
| 98 |
+
_RECENT_ALERTS[_alert_key(category, user_input)] = time.time()
|
| 99 |
+
|
| 100 |
+
|
| 101 |
def _escape_html(text: str) -> str:
|
| 102 |
return html.escape(text or "")
|
| 103 |
|
|
|
|
| 155 |
"parse_mode": parse_mode,
|
| 156 |
"disable_web_page_preview": True,
|
| 157 |
}
|
| 158 |
+
session = requests.Session()
|
| 159 |
+
session.trust_env = False
|
| 160 |
|
| 161 |
last_error = "Unknown Telegram error."
|
| 162 |
+
try:
|
| 163 |
+
for attempt in range(3):
|
| 164 |
+
try:
|
| 165 |
+
response = session.post(url, data=payload, timeout=(3.05, 10))
|
| 166 |
+
if response.status_code == 200:
|
| 167 |
+
print(f"[TELEGRAM] sendMessage success on attempt {attempt + 1}")
|
| 168 |
+
return "SUCCESS"
|
| 169 |
+
|
| 170 |
+
last_error = f"Telegram API returned {response.status_code} - {response.text[:300]}"
|
| 171 |
+
except requests.exceptions.Timeout:
|
| 172 |
+
last_error = "Telegram request timed out."
|
| 173 |
+
except requests.exceptions.RequestException as e:
|
| 174 |
+
last_error = f"Could not send notification. {str(e)}"
|
| 175 |
+
|
| 176 |
+
try:
|
| 177 |
+
response = session.get(url, params=payload, timeout=(3.05, 10))
|
| 178 |
+
if response.status_code == 200:
|
| 179 |
+
print(f"[TELEGRAM] sendMessage success via GET fallback on attempt {attempt + 1}")
|
| 180 |
+
return "SUCCESS"
|
| 181 |
+
|
| 182 |
+
last_error = f"Telegram GET fallback returned {response.status_code} - {response.text[:300]}"
|
| 183 |
+
except requests.exceptions.Timeout:
|
| 184 |
+
last_error = "Telegram GET fallback timed out."
|
| 185 |
+
except requests.exceptions.RequestException as e:
|
| 186 |
+
last_error = f"Telegram GET fallback failed. {str(e)}"
|
| 187 |
+
|
| 188 |
+
if attempt < 2:
|
| 189 |
+
time.sleep(0.8 * (attempt + 1))
|
| 190 |
+
finally:
|
| 191 |
+
session.close()
|
| 192 |
|
| 193 |
print(f"[TELEGRAM ERROR] {last_error}")
|
| 194 |
return f"FAILED: {last_error}"
|
|
|
|
| 326 |
text=text,
|
| 327 |
)
|
| 328 |
if result.startswith("SUCCESS"):
|
| 329 |
+
_mark_alert_sent(category, cleaned_input)
|
| 330 |
return "SUCCESS: Arun has been notified."
|
| 331 |
return result
|
| 332 |
|
|
|
|
| 811 |
reason=reason,
|
| 812 |
user_metadata=user_metadata,
|
| 813 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 814 |
if background:
|
| 815 |
+
submitted = _submit_background_task(
|
| 816 |
+
"pre_escalation_notify",
|
| 817 |
+
_deliver_notify_arun,
|
| 818 |
+
category,
|
| 819 |
+
user_input,
|
| 820 |
+
payload["user_metadata_json"],
|
| 821 |
+
)
|
| 822 |
return {
|
| 823 |
+
"handled": False,
|
| 824 |
"category": category,
|
| 825 |
"reason": reason,
|
| 826 |
+
"result": "QUEUED: pre-escalation notification scheduled." if submitted else "FAILED: could not queue pre-escalation notification.",
|
| 827 |
}
|
| 828 |
|
| 829 |
+
result = _deliver_notify_arun(
|
| 830 |
+
category=category,
|
| 831 |
+
user_input=user_input,
|
| 832 |
+
user_metadata_json=payload["user_metadata_json"],
|
| 833 |
+
)
|
| 834 |
return {
|
| 835 |
"handled": result.startswith("SUCCESS") or result.startswith("SKIPPED"),
|
| 836 |
"category": category,
|
|
|
|
| 883 |
if not should_notify or used_notify_tool:
|
| 884 |
return None
|
| 885 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 886 |
payload = _build_notify_payload(
|
| 887 |
category=category,
|
| 888 |
user_input=user_input,
|
|
|
|
| 891 |
assistant_output=final_response,
|
| 892 |
)
|
| 893 |
|
| 894 |
+
return _deliver_notify_arun(
|
| 895 |
+
category=payload["category"],
|
| 896 |
+
user_input=payload["user_input"],
|
| 897 |
+
user_metadata_json=payload["user_metadata_json"],
|
| 898 |
+
)
|
| 899 |
|
| 900 |
|
| 901 |
def queue_maybe_notify_arun(
|
core/api.py
CHANGED
|
@@ -79,7 +79,7 @@ async def chat_endpoint(req: ChatRequest):
|
|
| 79 |
req.message,
|
| 80 |
global_tool_map,
|
| 81 |
{"channel": "api", "session_id": req.session_id},
|
| 82 |
-
|
| 83 |
)
|
| 84 |
if pre_escalation:
|
| 85 |
yield json.dumps({"type": "status", "content": "Sending notification to Arun..."}) + "\n"
|
|
|
|
| 79 |
req.message,
|
| 80 |
global_tool_map,
|
| 81 |
{"channel": "api", "session_id": req.session_id},
|
| 82 |
+
False,
|
| 83 |
)
|
| 84 |
if pre_escalation:
|
| 85 |
yield json.dumps({"type": "status", "content": "Sending notification to Arun..."}) + "\n"
|
core/bot.py
CHANGED
|
@@ -50,7 +50,7 @@ def run_agent(chat_id: int, user_message: str) -> str:
|
|
| 50 |
user_message,
|
| 51 |
tool_map,
|
| 52 |
{"channel": "telegram", "chat_id": chat_id},
|
| 53 |
-
|
| 54 |
)
|
| 55 |
if pre_escalation:
|
| 56 |
queue_debug_event(
|
|
|
|
| 50 |
user_message,
|
| 51 |
tool_map,
|
| 52 |
{"channel": "telegram", "chat_id": chat_id},
|
| 53 |
+
False,
|
| 54 |
)
|
| 55 |
if pre_escalation:
|
| 56 |
queue_debug_event(
|