|
15 | 15 | import json |
16 | 16 | import asyncio |
17 | 17 | import openai |
| 18 | +from datetime import datetime, timezone |
18 | 19 |
|
19 | 20 | import utils |
20 | 21 | from utils import holiday_replacements |
|
29 | 30 | from telegram.constants import ChatAction |
30 | 31 | from telegram.error import TimedOut |
31 | 32 |
|
| 33 | +# time & date handling |
| 34 | +from timedate_handler import ( |
| 35 | + get_ordinal_suffix, |
| 36 | + get_english_timestamp_str, |
| 37 | + get_finnish_timestamp_str |
| 38 | +) |
| 39 | + |
32 | 40 | # reminder handling |
33 | 41 | from reminder_handler import handle_add_reminder, handle_delete_reminder, handle_edit_reminder, handle_view_reminders |
34 | 42 |
|
@@ -142,8 +150,9 @@ def pick_model_auto_switch(bot): |
142 | 150 | if not DB_INITIALIZED_SUCCESSFULLY or not DB_PATH: |
143 | 151 | logging.warning("DB not initialized or path missing — can't auto-switch, fallback to default model.") |
144 | 152 | return True |
145 | | - |
146 | | - usage_date = datetime.datetime.utcnow().strftime('%Y-%m-%d') |
| 153 | + # // old method |
| 154 | + # usage_date = datetime.datetime.utcnow().strftime('%Y-%m-%d') |
| 155 | + usage_date = datetime.now(timezone.utc).strftime('%Y-%m-%d') |
147 | 156 | daily_usage = _get_daily_usage_sync(DB_PATH, usage_date) |
148 | 157 | if not daily_usage: |
149 | 158 | daily_premium_tokens, daily_fallback_tokens = (0, 0) |
@@ -259,16 +268,31 @@ async def handle_message(bot, update: Update, context: CallbackContext, logger) |
259 | 268 | # Debug: Print before token limit checks |
260 | 269 | bot.logger.info(f"[Debug] is_no_limit: {is_no_limit}, user_token_count: {user_token_count}, max_tokens_config: {max_tokens_config}") |
261 | 270 |
|
262 | | - # get date & time for timestamps |
| 271 | + # ~~~~~~~~~~~~~~~ |
| 272 | + # Make timestamp |
| 273 | + # ~~~~~~~~~~~~~~~ |
263 | 274 | now_utc = datetime.datetime.utcnow() |
264 | | - current_time = now_utc |
265 | | - # utc_timestamp = now_utc.strftime("%Y-%m-%d %H:%M:%S UTC") |
266 | | - |
267 | | - # display abbreviated |
268 | | - utc_timestamp = now_utc.strftime("%Y-%m-%d %H:%M:%S %a UTC") |
269 | 275 |
|
| 276 | + # We'll keep this for session timeout comparisons |
| 277 | + current_time = now_utc |
270 | 278 | day_of_week = now_utc.strftime("%A") |
271 | | - user_message_with_timestamp = f"[{utc_timestamp}] {user_message}" |
| 279 | + system_timestamp = now_utc.strftime("%Y-%m-%d %H:%M:%S UTC") |
| 280 | + |
| 281 | + english_line = get_english_timestamp_str(now_utc) |
| 282 | + finnish_line = get_finnish_timestamp_str(now_utc) |
| 283 | + |
| 284 | + # Combine them however you like, e.g.: |
| 285 | + # |
| 286 | + # Monday, April 9th, 2025 | Time (UTC): 12:34:56 |
| 287 | + # maanantai, 9. huhtikuuta 2025, klo 15:34:56 Suomen aikaa |
| 288 | + # |
| 289 | + current_timestamp_str = f"{english_line}\n{finnish_line}" |
| 290 | + |
| 291 | + # We'll put that into a system message |
| 292 | + timestamp_system_msg = { |
| 293 | + "role": "system", |
| 294 | + "content": current_timestamp_str |
| 295 | + } |
272 | 296 |
|
273 | 297 | # Add the user's tokens to the total usage (JSON style) |
274 | 298 | bot.total_token_usage += user_token_count |
@@ -316,12 +340,24 @@ async def handle_message(bot, update: Update, context: CallbackContext, logger) |
316 | 340 | # Initialize chat_history as an empty list if it doesn't exist |
317 | 341 | chat_history = context.chat_data.get('chat_history', []) |
318 | 342 |
|
| 343 | + # ~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 344 | + # Insert the new system msg |
| 345 | + # ~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 346 | + # logger info on the appended system message |
| 347 | + logger.info(f"Inserting timestamp system message: {current_timestamp_str}") |
| 348 | + |
| 349 | + chat_history.append(timestamp_system_msg) |
| 350 | + |
319 | 351 | # Append the new user message to the chat history |
320 | | - chat_history.append({"role": "user", "content": user_message_with_timestamp}) |
| 352 | + chat_history.append({"role": "user", "content": user_message}) |
321 | 353 |
|
322 | 354 | # Prepare the conversation history to send to the OpenAI API |
323 | | - system_timestamp = datetime.datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S UTC") |
324 | | - system_message = {"role": "system", "content": f"System time+date: {system_timestamp}, {day_of_week}): {bot.system_instructions}"} |
| 355 | + |
| 356 | + # # // old method that included the timestamp in the original system message |
| 357 | + # system_timestamp = datetime.datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S UTC") |
| 358 | + # system_message = {"role": "system", "content": f"System time+date: {system_timestamp}, {day_of_week}): {bot.system_instructions}"} |
| 359 | + |
| 360 | + system_message = {"role": "system", "content": f"Instructions: {bot.system_instructions}"} |
325 | 361 |
|
326 | 362 | chat_history_with_system_message = [system_message] + chat_history |
327 | 363 |
|
|
0 commit comments