Spaces:
Sleeping
Sleeping
Update agent.py
Browse files
agent.py
CHANGED
|
@@ -388,9 +388,32 @@ class MagAgent:
|
|
| 388 |
)
|
| 389 |
|
| 390 |
def _validate_and_format(self, result: str, context: dict) -> str:
|
| 391 |
-
validation
|
| 392 |
-
|
| 393 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 394 |
"""Central error handling with context-aware formatting"""
|
| 395 |
error_type = error.__class__.__name__
|
| 396 |
error_msg = str(error)
|
|
|
|
| 388 |
)
|
| 389 |
|
| 390 |
def _validate_and_format(self, result: str, context: dict) -> str:
|
| 391 |
+
"""Final validation and formatting pipeline"""
|
| 392 |
+
try:
|
| 393 |
+
# Basic result validation
|
| 394 |
+
if not result:
|
| 395 |
+
raise ValueError("Empty agent response")
|
| 396 |
+
|
| 397 |
+
# Type checking
|
| 398 |
+
if not isinstance(result, str):
|
| 399 |
+
raise TypeError(f"Expected string response, got {type(result)}")
|
| 400 |
+
|
| 401 |
+
# Length validation
|
| 402 |
+
if len(result) > 4096:
|
| 403 |
+
result = result[:4090] + "..."
|
| 404 |
+
|
| 405 |
+
# Record successful validation
|
| 406 |
+
context["validation_checks"].append({
|
| 407 |
+
"type": "success",
|
| 408 |
+
"timestamp": datetime.now().isoformat()
|
| 409 |
+
})
|
| 410 |
+
|
| 411 |
+
return result
|
| 412 |
+
|
| 413 |
+
except Exception as e:
|
| 414 |
+
return self._handle_error(e, context)
|
| 415 |
+
|
| 416 |
+
def _handle_error(self, error: Exception, context: dict) -> str:
|
| 417 |
"""Central error handling with context-aware formatting"""
|
| 418 |
error_type = error.__class__.__name__
|
| 419 |
error_msg = str(error)
|