Spaces:
Running
Running
| """ | |
| NeuraPrompt Agent — Utility Helpers (v7.7) | |
| Common functions used across the agent | |
| """ | |
| import logging | |
| import json | |
| from datetime import datetime | |
| from typing import Any, Dict | |
| log = logging.getLogger("agent.utils") | |
| def safe_json_parse(text: str) -> Dict[str, Any]: | |
| """Safely parse JSON with fallback""" | |
| text = text.strip() | |
| if text.startswith("```"): | |
| text = text.replace("```json", "").replace("```", "").strip() | |
| try: | |
| return json.loads(text) | |
| except json.JSONDecodeError: | |
| # Try to extract JSON from text | |
| import re | |
| match = re.search(r'\{.*\}', text, re.DOTALL) | |
| if match: | |
| try: | |
| return json.loads(match.group()) | |
| except: | |
| pass | |
| return {"error": "Failed to parse JSON", "raw": text[:200]} | |
| def format_duration(seconds: float) -> str: | |
| """Format seconds into human readable string""" | |
| if seconds < 1: | |
| return f"{int(seconds * 1000)}ms" | |
| return f"{seconds:.1f}s" | |
| def log_agent_step(step: int, action: str, thought: str = ""): | |
| """Log agent step for debugging""" | |
| log.info(f"[Step {step}] {action} | {thought}") | |
| def get_timestamp() -> str: | |
| """Get current timestamp in readable format""" | |
| return datetime.now().strftime("%Y-%m-%d %H:%M:%S") | |