fix: save_conversation in CALLBACK_HINT, move AI hint to thread pool
This commit is contained in:
@@ -268,6 +268,27 @@ def _do_regen(cfg: Config, chat_id: int, msg_id: int, ctx: dict, conv: dict):
|
|||||||
_show_ai_suggestions(cfg.telegram, chat_id, msg_id, new_suggestions)
|
_show_ai_suggestions(cfg.telegram, chat_id, msg_id, new_suggestions)
|
||||||
|
|
||||||
|
|
||||||
|
def _do_ai_hint(cfg: Config, chat_id: int, conv: dict, ctx: dict, hint: str):
|
||||||
|
"""在 AI 线程池中根据用户提示生成回复"""
|
||||||
|
try:
|
||||||
|
new_suggestions = generate_more_replies(
|
||||||
|
cfg.ai, ctx["sender"], ctx["subject"], ctx["original_body"],
|
||||||
|
conv.get("all_suggestions", []), user_hint=hint,
|
||||||
|
)
|
||||||
|
except Exception as e:
|
||||||
|
send_text(cfg.telegram, str(chat_id), f"生成失败: {e}")
|
||||||
|
delete_conversation(chat_id)
|
||||||
|
return
|
||||||
|
if new_suggestions:
|
||||||
|
conv["ai_suggestions"] = new_suggestions
|
||||||
|
conv["all_suggestions"].extend(new_suggestions)
|
||||||
|
conv["state"] = "ai_reply_selection"
|
||||||
|
conv.pop("prompt_msg_id", None)
|
||||||
|
save_conversation(chat_id, conv)
|
||||||
|
logger.info("[ai_hint] 根据提示生成 %d 条新回复", len(new_suggestions))
|
||||||
|
_show_ai_suggestions(cfg.telegram, chat_id, conv["summary_msg_id"], new_suggestions)
|
||||||
|
|
||||||
|
|
||||||
def _handle_update(tg_cfg: TelegramConfig, cfg: Config, update: dict):
|
def _handle_update(tg_cfg: TelegramConfig, cfg: Config, update: dict):
|
||||||
if "callback_query" in update:
|
if "callback_query" in update:
|
||||||
_handle_callback(tg_cfg, cfg, update["callback_query"])
|
_handle_callback(tg_cfg, cfg, update["callback_query"])
|
||||||
@@ -381,6 +402,7 @@ def _handle_callback(tg_cfg: TelegramConfig, cfg: Config, cb: dict):
|
|||||||
if conv and conv.get("summary_msg_id") == msg_id:
|
if conv and conv.get("summary_msg_id") == msg_id:
|
||||||
conv["state"] = "awaiting_ai_hint"
|
conv["state"] = "awaiting_ai_hint"
|
||||||
conv["prompt_msg_id"] = prompt_msg_id
|
conv["prompt_msg_id"] = prompt_msg_id
|
||||||
|
save_conversation(chat_id, conv)
|
||||||
logger.info(" 等待用户输入 AI 提示")
|
logger.info(" 等待用户输入 AI 提示")
|
||||||
|
|
||||||
elif action == CALLBACK_CANCEL:
|
elif action == CALLBACK_CANCEL:
|
||||||
@@ -474,23 +496,14 @@ def _handle_message(tg_cfg: TelegramConfig, cfg: Config, msg: dict):
|
|||||||
delete_message(tg_cfg, chat_id_str, user_msg_id)
|
delete_message(tg_cfg, chat_id_str, user_msg_id)
|
||||||
if prompt_msg_id:
|
if prompt_msg_id:
|
||||||
delete_message(tg_cfg, chat_id_str, prompt_msg_id)
|
delete_message(tg_cfg, chat_id_str, prompt_msg_id)
|
||||||
try:
|
|
||||||
new_suggestions = generate_more_replies(
|
|
||||||
cfg.ai, ctx["sender"], ctx["subject"], ctx["original_body"],
|
|
||||||
conv.get("all_suggestions", []), user_hint=text,
|
|
||||||
)
|
|
||||||
except Exception as e:
|
|
||||||
send_text(tg_cfg, chat_id_str, f"生成失败: {e}")
|
|
||||||
delete_conversation(chat_id)
|
|
||||||
return
|
|
||||||
|
|
||||||
if new_suggestions:
|
# 更新消息为 AI 思考中
|
||||||
conv["ai_suggestions"] = new_suggestions
|
_tg_req(tg_cfg, "editMessageText", {
|
||||||
conv["all_suggestions"].extend(new_suggestions)
|
"chat_id": chat_id, "message_id": conv["summary_msg_id"],
|
||||||
conv["state"] = "ai_reply_selection"
|
"text": "🤖 *AI思考中…*", "parse_mode": "MarkdownV2",
|
||||||
conv.pop("prompt_msg_id", None)
|
})
|
||||||
logger.info(" 根据提示生成 %d 条新回复", len(new_suggestions))
|
# 丢到 AI 线程池处理
|
||||||
_show_ai_suggestions(tg_cfg, chat_id, conv["summary_msg_id"], new_suggestions)
|
_ai_pool.submit(_do_ai_hint, cfg, chat_id, conv, ctx, text)
|
||||||
|
|
||||||
|
|
||||||
def _show_ai_suggestions(tg_cfg: TelegramConfig, chat_id: int, msg_id: int,
|
def _show_ai_suggestions(tg_cfg: TelegramConfig, chat_id: int, msg_id: int,
|
||||||
|
|||||||
Reference in New Issue
Block a user