feat: three-stage status: 获取邮件中 -> 等待处理 -> AI思考中

This commit is contained in:
2026-07-13 08:19:36 +08:00
parent 02b0941ed0
commit 01504e6295
2 changed files with 17 additions and 10 deletions

13
main.py
View File

@@ -8,7 +8,7 @@ import time
from concurrent.futures import ThreadPoolExecutor
from src.config import load_config
from src.summarizer import poll_accounts, ai_process, tg_send_and_mark
from src.tg_bot import poll_telegram
from src.tg_bot import poll_telegram, send_thinking
# Thread-safe logging: all log records go through a single QueueListener thread
_log_queue = queue_module.Queue(-1)
@@ -55,7 +55,12 @@ def _email_poller(cfg):
if mail.uid not in _processing_uids:
_processing_uids.add(mail.uid)
_pending_uids.add(mail.uid)
_email_queue.put((acct_idx, mail))
# 发送"获取邮件中"提示
try:
tg_msg_id = send_thinking(cfg.telegram, cfg.telegram.chat_id, "📥 获取邮件中...")
except Exception:
tg_msg_id = 0
_email_queue.put((acct_idx, mail, tg_msg_id))
except Exception as e:
logger.error(f"邮件轮询线程异常: {e}", exc_info=True)
time.sleep(5)
@@ -74,10 +79,10 @@ def _ai_processor(cfg):
while _running:
while len(pending_futures) < max_workers:
try:
acct_idx, mail = _email_queue.get_nowait()
acct_idx, mail, tg_msg_id = _email_queue.get_nowait()
except queue_module.Empty:
break
future = executor.submit(ai_process, cfg, acct_idx, mail)
future = executor.submit(ai_process, cfg, acct_idx, mail, tg_msg_id)
pending_futures[future] = (acct_idx, mail)
completed = [f for f in pending_futures if f.done()]

View File

@@ -21,14 +21,16 @@ def poll_accounts(cfg: Config, skip_uids: set[bytes] | None = None) -> Generator
logger.error(f"轮询 {acct.username} 失败: {e}", exc_info=True)
def ai_process(cfg: Config, acct_idx: int, mail: Email) -> Optional[dict]:
def ai_process(cfg: Config, acct_idx: int, mail: Email, tg_msg_id: int = 0) -> Optional[dict]:
logger.info(f" 正在摘要: {mail.subject}")
# 阶段1显示获取邮件中
msg_id = send_thinking(cfg.telegram, cfg.telegram.chat_id, "📥 获取邮件中...")
logger.info(f" 获取邮件中消息已发送: msg_id={msg_id}")
# 阶段1显示等待处理
if tg_msg_id:
edit_message(cfg.telegram, cfg.telegram.chat_id, tg_msg_id, "⏳ 等待处理...")
else:
tg_msg_id = send_thinking(cfg.telegram, cfg.telegram.chat_id, "⏳ 等待处理...")
# 阶段2AI 处理,更新为思考中
edit_message(cfg.telegram, cfg.telegram.chat_id, msg_id, "🤖 AI思考中...")
edit_message(cfg.telegram, cfg.telegram.chat_id, tg_msg_id, "🤖 AI思考中...")
summary = summarize_email(cfg.ai, mail.recipient, mail.subject, mail.sender, mail.body, mail.account_email)
summary["recipient"] = mail.recipient
if "@" not in summary.get("sender", ""):
@@ -46,7 +48,7 @@ def ai_process(cfg: Config, acct_idx: int, mail: Email) -> Optional[dict]:
"account_email": mail.account_email,
"uid": mail.uid,
"folder": mail.folder,
"thinking_msg_id": msg_id,
"thinking_msg_id": tg_msg_id if tg_msg_id else 0,
}