fix: move regenerate to AI thread pool, fix parse_mode None error
This commit is contained in:
@@ -1,5 +1,7 @@
|
|||||||
import json
|
import json
|
||||||
import logging
|
import logging
|
||||||
|
import threading
|
||||||
|
from concurrent.futures import ThreadPoolExecutor
|
||||||
from email.utils import parseaddr
|
from email.utils import parseaddr
|
||||||
import requests
|
import requests
|
||||||
from src.config import TelegramConfig, Config
|
from src.config import TelegramConfig, Config
|
||||||
@@ -10,6 +12,8 @@ from src.database import save_email_context, load_email_context, save_conversati
|
|||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
_ai_pool = ThreadPoolExecutor(max_workers=3, thread_name_prefix="ai_regen")
|
||||||
|
|
||||||
|
|
||||||
# ── Public API ──────────────────────────────────────────
|
# ── Public API ──────────────────────────────────────────
|
||||||
|
|
||||||
@@ -246,6 +250,24 @@ def _tg_req(tg_cfg: TelegramConfig, method: str, payload: dict) -> dict:
|
|||||||
|
|
||||||
# ── Update handling ─────────────────────────────────────
|
# ── Update handling ─────────────────────────────────────
|
||||||
|
|
||||||
|
def _do_regen(cfg: Config, chat_id: int, msg_id: int, ctx: dict, conv: dict):
|
||||||
|
"""在 AI 线程池中执行换一批,不阻塞按钮轮询"""
|
||||||
|
try:
|
||||||
|
new_suggestions = generate_more_replies(
|
||||||
|
cfg.ai, ctx["sender"], ctx["subject"], ctx["original_body"],
|
||||||
|
conv.get("all_suggestions", []),
|
||||||
|
)
|
||||||
|
except Exception as e:
|
||||||
|
send_text(cfg.telegram, str(chat_id), f"生成失败: {e}")
|
||||||
|
return
|
||||||
|
if new_suggestions:
|
||||||
|
conv["ai_suggestions"] = new_suggestions
|
||||||
|
conv["all_suggestions"].extend(new_suggestions)
|
||||||
|
save_conversation(chat_id, conv)
|
||||||
|
logger.info("[ai_regen] 新生成 %d 条回复", len(new_suggestions))
|
||||||
|
_show_ai_suggestions(cfg.telegram, chat_id, 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"])
|
||||||
@@ -345,21 +367,10 @@ def _handle_callback(tg_cfg: TelegramConfig, cfg: Config, cb: dict):
|
|||||||
# AI 处理中
|
# AI 处理中
|
||||||
_tg_req(tg_cfg, "editMessageText", {
|
_tg_req(tg_cfg, "editMessageText", {
|
||||||
"chat_id": chat_id, "message_id": msg_id,
|
"chat_id": chat_id, "message_id": msg_id,
|
||||||
"text": "🤖 AI思考中...", "parse_mode": None,
|
"text": "🤖 AI思考中...",
|
||||||
})
|
})
|
||||||
try:
|
# 丢到 AI 线程池处理,不阻塞按钮轮询
|
||||||
new_suggestions = generate_more_replies(
|
_ai_pool.submit(_do_regen, cfg, chat_id, msg_id, ctx, conv)
|
||||||
cfg.ai, ctx["sender"], ctx["subject"], ctx["original_body"],
|
|
||||||
conv.get("all_suggestions", []),
|
|
||||||
)
|
|
||||||
except Exception as e:
|
|
||||||
send_text(tg_cfg, str(chat_id), f"生成失败: {e}")
|
|
||||||
return
|
|
||||||
if new_suggestions:
|
|
||||||
conv["ai_suggestions"] = new_suggestions
|
|
||||||
conv["all_suggestions"].extend(new_suggestions)
|
|
||||||
logger.info(" 新生成 %d 条回复", len(new_suggestions))
|
|
||||||
_show_ai_suggestions(tg_cfg, chat_id, msg_id, new_suggestions)
|
|
||||||
|
|
||||||
elif action == CALLBACK_HINT and ctx:
|
elif action == CALLBACK_HINT and ctx:
|
||||||
prompt_msg_id = send_text(
|
prompt_msg_id = send_text(
|
||||||
|
|||||||
Reference in New Issue
Block a user