File size: 13,800 Bytes
92c4ae6 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 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 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 |
from datetime import datetime
import json
import logging
import re
from typing import Any, Dict, Optional, Tuple
logger = logging.getLogger(__name__)
def parse_react_response(llm_output: str) -> Tuple[Optional[str], Optional[Dict[str, Any]], Optional[str]]:
"""
Parse the LLM output to extract Thought, Action, and Final Answer.
Expected Formats:
Format 1 (JSON Action):
Thought: I need to search for X.
Action: {
"tool": "tool_name",
"params": { ... }
}
Format 2 (Final Answer):
Thought: I have found the info.
Final Answer: The answer is X.
Returns:
(thought_text, action_dict, final_answer_text)
"""
# Normalize
text = llm_output.strip()
thought = None
action = None
final_answer = None
# Extract Thought
# Extract Thought
# Lookahead for Action or Final Answer, allowing for whitespace/newlines
thought_match = re.search(r"Thought:\s*(.*?)(?=\n\s*Action:|\n\s*Final Answer:|$)", text, re.DOTALL | re.IGNORECASE)
if thought_match:
thought = thought_match.group(1).strip()
else:
# If no explicit "Thought:", treat beginning as thought
# But be careful if it starts with Action
if not text.lower().strip().startswith("action:") and not text.lower().strip().startswith("final answer:"):
# Try to grab everything until Action or Final Answer
split_match = re.split(r"\n\s*Action:|\n\s*Final Answer:", text, flags=re.IGNORECASE)
if split_match:
thought = split_match[0].strip()
# Extract Final Answer
final_answer_match = re.search(r"Final Answer:\s*(.*)", text, re.DOTALL | re.IGNORECASE)
if final_answer_match:
final_answer = final_answer_match.group(1).strip()
return thought, None, final_answer
# Extract Action
# Look for Action: ... json ...
action_match = re.search(r"Action:\s*(.*)", text, re.DOTALL | re.IGNORECASE)
if action_match:
action_text = action_match.group(1).strip()
# Try to find JSON blob in action text
# Simple heuristic: find first { and last }
try:
json_start = action_text.find("{")
json_end = action_text.rfind("}")
if json_start != -1 and json_end != -1:
json_str = action_text[json_start:json_end+1]
action = json.loads(json_str)
else:
# No valid JSON structure found
logger.warning(f"Action found but no valid JSON structure: {action_text[:200]}")
raise ValueError(f"Invalid action format: no JSON structure found in '{action_text[:100]}...'")
except json.JSONDecodeError as e:
# JSON parsing failed - log error and raise
logger.error(f"Failed to parse agent action JSON: {e}\nAction text: {action_text[:500]}")
raise ValueError(f"Invalid JSON in agent action: {e}") from e
return thought, action, final_answer
# ==============================================================================
# Agent Utility Formatters
# ==============================================================================
def format_agent_id(agent_id: str) -> str:
"""
Format agent ID for display.
Args:
agent_id: Raw agent ID (e.g., "agent-abc123")
Returns:
Formatted agent ID with consistent capitalization and spacing
Examples:
>>> format_agent_id("agent-abc123")
'Agent ABC123'
>>> format_agent_id("workflow-xyz")
'Workflow XYZ'
"""
if not agent_id:
return "Unknown Agent"
# Split by common delimiters
parts = re.split(r'[-_]', agent_id)
# Capitalize each part
formatted_parts = [part.upper() if part.lower() in ['id', 'uuid'] else part.capitalize() for part in parts if part]
return ' '.join(formatted_parts)
def parse_agent_id(agent_id: str) -> Dict[str, Optional[str]]:
"""
Parse agent ID into components.
Args:
agent_id: Agent ID string (e.g., "agent-abc123" or "workflow-v2-test")
Returns:
Dict with 'type', 'id', 'version' keys
Examples:
>>> parse_agent_id("agent-abc123")
{'type': 'agent', 'id': 'abc123', 'version': None}
>>> parse_agent_id("workflow-v2-test")
{'type': 'workflow', 'id': 'test', 'version': 'v2'}
"""
if not agent_id:
return {'type': None, 'id': None, 'version': None}
parts = agent_id.split('-')
result = {
'type': parts[0] if len(parts) > 0 else None,
'id': parts[-1] if len(parts) > 1 else None,
'version': None
}
# Check for version pattern (v1, v2, etc.)
for part in parts:
if re.match(r'^v\d+$', part.lower()):
result['version'] = part.lower()
# Remove version from id if it's there
if result['id'] == part:
result['id'] = parts[-2] if len(parts) > 2 else None
return result
def format_maturity_level(maturity: str) -> str:
"""
Format maturity level for display.
Args:
maturity: Maturity level (STUDENT, INTERN, SUPERVISED, AUTONOMOUS)
Returns:
Formatted display name
Examples:
>>> format_maturity_level("STUDENT")
'Student Agent'
>>> format_maturity_level("AUTONOMOUS")
'Autonomous Agent'
"""
maturity_map = {
'STUDENT': 'Student Agent',
'INTERN': 'Intern Agent',
'SUPERVISED': 'Supervised Agent',
'AUTONOMOUS': 'Autonomous Agent'
}
return maturity_map.get(maturity.upper(), maturity.capitalize() + ' Agent')
# ==============================================================================
# Date/Time Formatters
# ==============================================================================
def format_date_iso(date_string: str) -> str:
"""
Format ISO date string to readable format.
Args:
date_string: ISO date string (YYYY-MM-DD)
Returns:
Formatted date (e.g., "March 8, 2026")
Examples:
>>> format_date_iso("2026-03-08")
'March 8, 2026'
"""
try:
date_obj = datetime.fromisoformat(date_string)
return date_obj.strftime("%B %d, %Y").replace(" 0", " ")
except (ValueError, AttributeError, TypeError):
return date_string
def format_datetime(date_string: str) -> str:
"""
Format ISO datetime string to readable format with time.
Args:
date_string: ISO datetime string (YYYY-MM-DDTHH:MM:SS)
Returns:
Formatted datetime (e.g., "March 8, 2026 at 2:30 PM")
Examples:
>>> format_datetime("2026-03-08T14:30:00")
'March 8, 2026 at 2:30 PM'
"""
try:
date_obj = datetime.fromisoformat(date_string.replace('Z', '+00:00'))
return date_obj.strftime("%B %d, %Y at %I:%M %p").replace(" 0", " ").lstrip('0')
except (ValueError, AttributeError):
return date_string
def format_timestamp(timestamp: float) -> str:
"""
Format Unix timestamp to readable date.
Args:
timestamp: Unix timestamp (seconds since epoch)
Returns:
Formatted date (e.g., "March 8, 2026")
Examples:
>>> format_timestamp(1772974705)
'March 8, 2026'
"""
try:
date_obj = datetime.utcfromtimestamp(timestamp)
return date_obj.strftime("%B %d, %Y").replace(" 0", " ")
except (ValueError, TypeError, OSError):
return str(timestamp)
def format_relative_time(timestamp: float) -> str:
"""
Format timestamp as relative time (e.g., "2 hours ago").
Args:
timestamp: Unix timestamp
Returns:
Relative time string
Examples:
>>> format_relative_time(time.time() - 3600)
'1 hour ago'
"""
try:
from datetime import timezone
now = datetime.now(timezone.utc)
past = datetime.fromtimestamp(timestamp, tz=timezone.utc)
diff = now - past
seconds = diff.total_seconds()
if seconds < 60:
return "just now"
elif seconds < 3600:
minutes = int(seconds / 60)
return f"{minutes} minute{'s' if minutes != 1 else ''} ago"
elif seconds < 86400:
hours = int(seconds / 3600)
return f"{hours} hour{'s' if hours != 1 else ''} ago"
elif seconds < 604800:
days = int(seconds / 86400)
return f"{days} day{'s' if days != 1 else ''} ago"
else:
weeks = int(seconds / 604800)
return f"{weeks} week{'s' if weeks != 1 else ''} ago"
except (ValueError, TypeError, OSError):
return "unknown time"
# ==============================================================================
# Number/Currency Formatters
# ==============================================================================
def format_currency_usd(amount: float) -> str:
"""
Format amount as USD currency.
Args:
amount: Amount in USD
Returns:
Formatted currency string (e.g., "$1,000.00")
Examples:
>>> format_currency_usd(1000)
'$1,000.00'
"""
try:
if amount < 0:
return f"-${abs(amount):,.2f}"
return f"${amount:,.2f}"
except (ValueError, TypeError):
return "$0.00"
def format_currency_eur(amount: float) -> str:
"""
Format amount as EUR currency.
Args:
amount: Amount in EUR
Returns:
Formatted currency string (e.g., "€1,000.00")
Examples:
>>> format_currency_eur(1000)
'€1,000.00'
"""
try:
return f"€{amount:,.2f}"
except (ValueError, TypeError):
return "€0.00"
def format_number(number: int) -> str:
"""
Format number with thousands separators.
Args:
number: Number to format
Returns:
Formatted number string (e.g., "1,000,000")
Examples:
>>> format_number(1000000)
'1,000,000'
"""
try:
return f"{number:,}"
except (ValueError, TypeError):
return "0"
def format_percentage(value: float, decimals: int = 1) -> str:
"""
Format value as percentage.
Args:
value: Value as decimal (0.5 = 50%)
decimals: Number of decimal places
Returns:
Formatted percentage string (e.g., "50.5%")
Examples:
>>> format_percentage(0.505)
'50.5%'
"""
try:
return f"{value * 100:.{decimals}f}%"
except (ValueError, TypeError):
return "0.0%"
def format_decimal(value: float, precision: int = 2) -> str:
"""
Format value with specified decimal precision.
Args:
value: Value to format
precision: Number of decimal places
Returns:
Formatted decimal string
Examples:
>>> format_decimal(3.14159, 2)
'3.14'
"""
try:
return f"{value:.{precision}f}"
except (ValueError, TypeError):
return "0.00"
# ==============================================================================
# String Formatters
# ==============================================================================
def format_phone(phone: str) -> str:
"""
Format phone number to US format.
Args:
phone: Phone number string (digits only or with formatting)
Returns:
Formatted phone number (e.g., "(123) 456-7890")
Examples:
>>> format_phone("1234567890")
'(123) 456-7890'
"""
if not phone:
return ""
# Remove all non-digit characters
digits = re.sub(r'\D', '', phone)
# Format based on length
if len(digits) == 10:
return f"({digits[:3]}) {digits[3:6]}-{digits[6:]}"
elif len(digits) == 11 and digits[0] == '1':
return f"+1 ({digits[1:4]}) {digits[4:7]}-{digits[7:]}"
else:
# Return original if can't format
return phone
def format_name(name: str) -> str:
"""
Format name with proper capitalization.
Args:
name: Name string
Returns:
Formatted name with each word capitalized
Examples:
>>> format_name("john doe")
'John Doe'
"""
if not name:
return ""
# Capitalize each word
return ' '.join(word.capitalize() for word in name.split())
def truncate_text(text: str, max_length: int = 100, suffix: str = "...") -> str:
"""
Truncate text to maximum length with suffix.
Args:
text: Text to truncate
max_length: Maximum length (including suffix)
suffix: Suffix to add when truncated
Returns:
Truncated text
Examples:
>>> truncate_text("This is a very long text", 10)
'This is...'
"""
if not text:
return ""
if len(text) <= max_length:
return text
# Handle edge case where max_length is less than suffix length
if max_length <= len(suffix):
return suffix[:max_length]
# Truncate and add suffix
return text[:max_length - len(suffix)] + suffix
def sanitize_string(text: str, remove_html: bool = True, remove_special: bool = False) -> str:
"""
Sanitize string by removing HTML and/or special characters.
Args:
text: Text to sanitize
remove_html: Remove HTML tags
remove_special: Remove special characters (keep alphanumeric and spaces)
Returns:
Sanitized string
Examples:
>>> sanitize_string("<p>Hello</p>")
'Hello'
"""
if not text:
return ""
result = text
# Remove HTML tags
if remove_html:
result = re.sub(r'<[^>]+>', '', result)
# Remove special characters
if remove_special:
result = re.sub(r'[^a-zA-Z0-9\s]', '', result)
return result.strip()
|