Spaces:
Sleeping
Sleeping
ishaan-007 commited on
Commit Β·
fef765c
1
Parent(s): db21b05
modify app to handle NoneType error
Browse files
app.py
CHANGED
|
@@ -499,7 +499,14 @@ def call_agent(message: str, session_id: str, max_retries: int = 3) -> tuple[str
|
|
| 499 |
{"messages": [{"role": "user", "content": message}]},
|
| 500 |
config=config,
|
| 501 |
)
|
| 502 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 503 |
if hasattr(msg, "type") and msg.type == "ai":
|
| 504 |
return safe_str(msg.content), current_sid
|
| 505 |
if isinstance(msg, dict) and msg.get("role") in ("assistant", "ai"):
|
|
@@ -508,6 +515,7 @@ def call_agent(message: str, session_id: str, max_retries: int = 3) -> tuple[str
|
|
| 508 |
|
| 509 |
except Exception as e:
|
| 510 |
err = str(e)
|
|
|
|
| 511 |
|
| 512 |
# ββ FIX-A: Corrupted history (dangling tool call in MemorySaver) ββ
|
| 513 |
# Rotate to a new thread so MemorySaver starts fresh.
|
|
@@ -530,15 +538,24 @@ def call_agent(message: str, session_id: str, max_retries: int = 3) -> tuple[str
|
|
| 530 |
{"messages": [{"role": "user", "content": recovery_msg}]},
|
| 531 |
config=config,
|
| 532 |
)
|
| 533 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 534 |
if hasattr(msg, "type") and msg.type == "ai":
|
| 535 |
return safe_str(msg.content), current_sid
|
| 536 |
if isinstance(msg, dict) and msg.get("role") in ("assistant", "ai"):
|
| 537 |
return safe_str(msg.get("content", "")), current_sid
|
| 538 |
return "Agent returned no response after history rotation. Please try again.", current_sid
|
| 539 |
except Exception as e2:
|
|
|
|
| 540 |
log_error(str(e2), ctx="call_agent [post-rotation]")
|
| 541 |
-
return f"β οΈ Agent Error after session rotation: {e2}\n\
|
| 542 |
|
| 543 |
# ββ FIX-B: Mistral rate-limit / server errors β extended back-off ββ
|
| 544 |
if any(c in err for c in ["429", "520", "502", "503", "529", "mistral.ai", "Rate limit"]):
|
|
@@ -549,7 +566,7 @@ def call_agent(message: str, session_id: str, max_retries: int = 3) -> tuple[str
|
|
| 549 |
continue
|
| 550 |
|
| 551 |
log_error(err, ctx="call_agent")
|
| 552 |
-
return f"β οΈ Agent Error: {err}\n\
|
| 553 |
|
| 554 |
return "β Mistral not responding after retries. Wait a few minutes and try again.", current_sid
|
| 555 |
|
|
|
|
| 499 |
{"messages": [{"role": "user", "content": message}]},
|
| 500 |
config=config,
|
| 501 |
)
|
| 502 |
+
if not result:
|
| 503 |
+
return "Agent returned empty result. Please try again.", current_sid
|
| 504 |
+
|
| 505 |
+
messages = result.get("messages", [])
|
| 506 |
+
if messages is None:
|
| 507 |
+
messages = []
|
| 508 |
+
|
| 509 |
+
for msg in reversed(messages):
|
| 510 |
if hasattr(msg, "type") and msg.type == "ai":
|
| 511 |
return safe_str(msg.content), current_sid
|
| 512 |
if isinstance(msg, dict) and msg.get("role") in ("assistant", "ai"):
|
|
|
|
| 515 |
|
| 516 |
except Exception as e:
|
| 517 |
err = str(e)
|
| 518 |
+
tb = traceback.format_exc()
|
| 519 |
|
| 520 |
# ββ FIX-A: Corrupted history (dangling tool call in MemorySaver) ββ
|
| 521 |
# Rotate to a new thread so MemorySaver starts fresh.
|
|
|
|
| 538 |
{"messages": [{"role": "user", "content": recovery_msg}]},
|
| 539 |
config=config,
|
| 540 |
)
|
| 541 |
+
|
| 542 |
+
if not result:
|
| 543 |
+
return "Agent returned empty result after rotation.", current_sid
|
| 544 |
+
|
| 545 |
+
messages = result.get("messages", [])
|
| 546 |
+
if messages is None:
|
| 547 |
+
messages = []
|
| 548 |
+
|
| 549 |
+
for msg in reversed(messages):
|
| 550 |
if hasattr(msg, "type") and msg.type == "ai":
|
| 551 |
return safe_str(msg.content), current_sid
|
| 552 |
if isinstance(msg, dict) and msg.get("role") in ("assistant", "ai"):
|
| 553 |
return safe_str(msg.get("content", "")), current_sid
|
| 554 |
return "Agent returned no response after history rotation. Please try again.", current_sid
|
| 555 |
except Exception as e2:
|
| 556 |
+
tb2 = traceback.format_exc()
|
| 557 |
log_error(str(e2), ctx="call_agent [post-rotation]")
|
| 558 |
+
return f"β οΈ Agent Error after session rotation: {e2}\n\nTraceback:\n{tb2}", current_sid
|
| 559 |
|
| 560 |
# ββ FIX-B: Mistral rate-limit / server errors β extended back-off ββ
|
| 561 |
if any(c in err for c in ["429", "520", "502", "503", "529", "mistral.ai", "Rate limit"]):
|
|
|
|
| 566 |
continue
|
| 567 |
|
| 568 |
log_error(err, ctx="call_agent")
|
| 569 |
+
return f"β οΈ Agent Error: {err}\n\nTraceback:\n{tb}", current_sid
|
| 570 |
|
| 571 |
return "β Mistral not responding after retries. Wait a few minutes and try again.", current_sid
|
| 572 |
|