File size: 1,971 Bytes
9b006e9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from typing import Dict, Any, Optional
import json
import re

class ResponseFormatter:
    """Formats AI agent responses for optimal user experience"""
    
    @staticmethod  
    def format_research_response(response: str, data: Optional[Dict[str, Any]] = None) -> str:
        """Format research response with structured data presentation"""
        if not response:
            return "No information available."
        
        formatted = response.strip()
        
        if data:
            if "prices" in data:
                formatted = ResponseFormatter._add_price_formatting(formatted, data["prices"])
            if "metrics" in data:
                formatted = ResponseFormatter._add_metrics_formatting(formatted, data["metrics"])
        
        formatted = ResponseFormatter._enhance_markdown(formatted)
        return formatted
    
    @staticmethod
    def _add_price_formatting(text: str, prices: Dict[str, float]) -> str:
        """Add price data with formatting"""
        price_section = "\n\nπŸ“ˆ **Current Prices:**\n"
        for symbol, price in prices.items():
            price_section += f"β€’ **{symbol.upper()}**: ${price:,.2f}\n"
        return text + price_section
    
    @staticmethod  
    def _add_metrics_formatting(text: str, metrics: Dict[str, Any]) -> str:
        """Add metrics with formatting"""
        metrics_section = "\n\nπŸ“Š **Key Metrics:**\n"
        for key, value in metrics.items():
            if isinstance(value, (int, float)):
                metrics_section += f"β€’ **{key.title()}**: {value:,.2f}\n"
            else:
                metrics_section += f"β€’ **{key.title()}**: {value}\n"
        return text + metrics_section
    
    @staticmethod
    def _enhance_markdown(text: str) -> str:
        """Enhance markdown formatting for better readability"""
        text = re.sub(r'\*\*([^*]+)\*\*', r'**\1**', text)
        text = re.sub(r'\n\s*\n\s*\n', '\n\n', text)
        return text.strip()