|
107 | 107 | DB_INITIALIZED_SUCCESSFULLY = False
|
108 | 108 | # --- End Import ---
|
109 | 109 |
|
| 110 | +# get today's usage regarding OpenAI API's responses (for auto-switching) |
| 111 | +def get_today_usage(): |
| 112 | + """ |
| 113 | + Return (premium_used, mini_used) for today if DB is ready, |
| 114 | + or None if DB not ready or usage could not be retrieved. |
| 115 | + """ |
| 116 | + if not DB_INITIALIZED_SUCCESSFULLY or not DB_PATH: |
| 117 | + return None # Not ready |
| 118 | + usage_date = datetime.datetime.utcnow().strftime('%Y-%m-%d') |
| 119 | + daily_usage = _get_daily_usage_sync(DB_PATH, usage_date) |
| 120 | + return daily_usage # daily_usage is (premium_tokens, mini_tokens) or None |
| 121 | + |
110 | 122 | # model picker auto-switch
|
111 | 123 | def pick_model_auto_switch(bot):
|
112 | 124 | if not config_auto.has_section('ModelAutoSwitch'):
|
@@ -1128,19 +1140,68 @@ async def handle_message(bot, update: Update, context: CallbackContext, logger)
|
1128 | 1140 | # escaped_reply = bot_reply
|
1129 | 1141 | logger.info(f"[Debug] Reply message after escaping: {escaped_reply}")
|
1130 | 1142 |
|
1131 |
| - # Log the bot's response |
| 1143 | + # new detailed logging in v0.76 |
| 1144 | + try: |
| 1145 | + # 1) Attempt to read daily usage from DB: |
| 1146 | + usage_tuple = get_today_usage() # returns (premium_used, mini_used) or None |
| 1147 | + if usage_tuple: |
| 1148 | + premium_used, mini_used = usage_tuple |
| 1149 | + else: |
| 1150 | + premium_used, mini_used = None, None |
| 1151 | + |
| 1152 | + # 2) read from config.ini for limits & model |
| 1153 | + premium_model = config_auto["ModelAutoSwitch"].get("PremiumModel", "") |
| 1154 | + fallback_model = config_auto["ModelAutoSwitch"].get("FallbackModel", "") |
| 1155 | + premium_limit = config_auto["ModelAutoSwitch"].getint("PremiumTokenLimit", 0) |
| 1156 | + fallback_limit = config_auto["ModelAutoSwitch"].getint("MiniTokenLimit", 0) |
| 1157 | + |
| 1158 | + # 3) figure out if current model is 'premium' or 'mini' |
| 1159 | + if bot.model == premium_model and premium_model: |
| 1160 | + tier_str = "premium" |
| 1161 | + used_so_far = premium_used if premium_used is not None else "N/A" |
| 1162 | + limit_str = premium_limit if premium_limit else "N/A" |
| 1163 | + elif bot.model == fallback_model and fallback_model: |
| 1164 | + tier_str = "mini" |
| 1165 | + used_so_far = mini_used if mini_used is not None else "N/A" |
| 1166 | + limit_str = fallback_limit if fallback_limit else "N/A" |
| 1167 | + else: |
| 1168 | + tier_str = "?" |
| 1169 | + used_so_far = "N/A" |
| 1170 | + limit_str = "N/A" |
| 1171 | + |
| 1172 | + if used_so_far == "N/A" or limit_str == "N/A": |
| 1173 | + usage_str = "N/A" |
| 1174 | + else: |
| 1175 | + usage_str = f"{used_so_far}/{limit_str}" |
| 1176 | + |
| 1177 | + model_info = f"model={bot.model}, tier={tier_str}, usage={usage_str}" |
| 1178 | + |
| 1179 | + except Exception as e: |
| 1180 | + bot.logger.warning(f"Could not build model_info: {e}") |
| 1181 | + # Fallback if something went wrong |
| 1182 | + model_info = "model=N/A, usage=N/A" |
| 1183 | + |
| 1184 | + # pass the bot log with more info |
1132 | 1185 | bot.log_message(
|
1133 | 1186 | message_type='Bot',
|
| 1187 | + user_id=update.message.from_user.id, |
1134 | 1188 | message=bot_reply,
|
| 1189 | + model_info=model_info |
1135 | 1190 | )
|
1136 | 1191 |
|
1137 |
| - # # send the response |
1138 |
| - # await context.bot.send_message( |
1139 |
| - # chat_id=chat_id, |
1140 |
| - # text=escaped_reply, |
1141 |
| - # parse_mode=ParseMode.HTML |
| 1192 | + # # Log the bot's response |
| 1193 | + # bot.log_message( |
| 1194 | + # message_type='Bot', |
| 1195 | + # message=bot_reply, |
1142 | 1196 | # )
|
1143 | 1197 |
|
| 1198 | + # # # send the response |
| 1199 | + # # await context.bot.send_message( |
| 1200 | + # # chat_id=chat_id, |
| 1201 | + # # text=escaped_reply, |
| 1202 | + # # parse_mode=ParseMode.HTML |
| 1203 | + # # ) |
| 1204 | + |
1144 | 1205 | escaped_reply = sanitize_html(escaped_reply)
|
1145 | 1206 |
|
1146 | 1207 | message_parts = split_message(escaped_reply)
|
|
0 commit comments