|
12 | 12 | from common.config.app_config import config |
13 | 13 | from auth.auth_utils import get_authenticated_user_details |
14 | 14 | from azure.monitor.opentelemetry import configure_azure_monitor |
15 | | -from dateutil import parser |
16 | 15 | from common.utils.event_utils import track_event_if_configured |
17 | 16 |
|
18 | 17 | # FastAPI imports |
|
39 | 38 | from common.utils.utils_kernel import rai_success |
40 | 39 |
|
41 | 40 | from common.database.database_factory import DatabaseFactory |
| 41 | +from common.utils.utils_date import format_dates_in_messages |
42 | 42 | from v3.api.router import app_v3 |
43 | 43 |
|
44 | 44 | # Check if the Application Insights Instrumentation Key is set in the environment variables |
|
92 | 92 | logging.info("Added health check middleware") |
93 | 93 |
|
94 | 94 |
|
95 | | -def format_dates_in_messages(messages, target_locale="en-US"): |
96 | | - """ |
97 | | - Format dates in agent messages according to the specified locale. |
98 | | -
|
99 | | - Args: |
100 | | - messages: List of message objects or string content |
101 | | - target_locale: Target locale for date formatting (default: en-US) |
102 | | -
|
103 | | - Returns: |
104 | | - Formatted messages with dates converted to target locale format |
105 | | - """ |
106 | | - # Define target format patterns per locale |
107 | | - locale_date_formats = { |
108 | | - "en-IN": "%d %b %Y", # 30 Jul 2025 |
109 | | - "en-US": "%b %d, %Y", # Jul 30, 2025 |
110 | | - } |
111 | | - |
112 | | - output_format = locale_date_formats.get(target_locale, "%d %b %Y") |
113 | | - # Match both "Jul 30, 2025, 12:00:00 AM" and "30 Jul 2025" |
114 | | - date_pattern = r"(\d{1,2} [A-Za-z]{3,9} \d{4}|[A-Za-z]{3,9} \d{1,2}, \d{4}(, \d{1,2}:\d{2}:\d{2} ?[APap][Mm])?)" |
115 | | - |
116 | | - def convert_date(match): |
117 | | - date_str = match.group(0) |
118 | | - try: |
119 | | - dt = parser.parse(date_str) |
120 | | - return dt.strftime(output_format) |
121 | | - except Exception: |
122 | | - return date_str # Leave it unchanged if parsing fails |
123 | | - |
124 | | - # Process messages |
125 | | - if isinstance(messages, list): |
126 | | - formatted_messages = [] |
127 | | - for message in messages: |
128 | | - if hasattr(message, "content") and message.content: |
129 | | - # Create a copy of the message with formatted content |
130 | | - formatted_message = ( |
131 | | - message.model_copy() if hasattr(message, "model_copy") else message |
132 | | - ) |
133 | | - if hasattr(formatted_message, "content"): |
134 | | - formatted_message.content = re.sub( |
135 | | - date_pattern, convert_date, formatted_message.content |
136 | | - ) |
137 | | - formatted_messages.append(formatted_message) |
138 | | - else: |
139 | | - formatted_messages.append(message) |
140 | | - return formatted_messages |
141 | | - elif isinstance(messages, str): |
142 | | - return re.sub(date_pattern, convert_date, messages) |
143 | | - else: |
144 | | - return messages |
145 | | - |
146 | | - |
147 | 95 | @app.post("/api/user_browser_language") |
148 | 96 | async def user_browser_language_endpoint(user_language: UserLanguage, request: Request): |
149 | 97 | """ |
|
0 commit comments