feat: three-stage status: 获取邮件中 -> 等待处理 -> AI思考中
This commit is contained in:
13
main.py
13
main.py
@@ -8,7 +8,7 @@ import time
|
|||||||
from concurrent.futures import ThreadPoolExecutor
|
from concurrent.futures import ThreadPoolExecutor
|
||||||
from src.config import load_config
|
from src.config import load_config
|
||||||
from src.summarizer import poll_accounts, ai_process, tg_send_and_mark
|
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
|
# Thread-safe logging: all log records go through a single QueueListener thread
|
||||||
_log_queue = queue_module.Queue(-1)
|
_log_queue = queue_module.Queue(-1)
|
||||||
@@ -55,7 +55,12 @@ def _email_poller(cfg):
|
|||||||
if mail.uid not in _processing_uids:
|
if mail.uid not in _processing_uids:
|
||||||
_processing_uids.add(mail.uid)
|
_processing_uids.add(mail.uid)
|
||||||
_pending_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:
|
except Exception as e:
|
||||||
logger.error(f"邮件轮询线程异常: {e}", exc_info=True)
|
logger.error(f"邮件轮询线程异常: {e}", exc_info=True)
|
||||||
time.sleep(5)
|
time.sleep(5)
|
||||||
@@ -74,10 +79,10 @@ def _ai_processor(cfg):
|
|||||||
while _running:
|
while _running:
|
||||||
while len(pending_futures) < max_workers:
|
while len(pending_futures) < max_workers:
|
||||||
try:
|
try:
|
||||||
acct_idx, mail = _email_queue.get_nowait()
|
acct_idx, mail, tg_msg_id = _email_queue.get_nowait()
|
||||||
except queue_module.Empty:
|
except queue_module.Empty:
|
||||||
break
|
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)
|
pending_futures[future] = (acct_idx, mail)
|
||||||
|
|
||||||
completed = [f for f in pending_futures if f.done()]
|
completed = [f for f in pending_futures if f.done()]
|
||||||
|
|||||||
@@ -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)
|
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}")
|
logger.info(f" 正在摘要: {mail.subject}")
|
||||||
# 阶段1:显示获取邮件中
|
# 阶段1:显示等待处理
|
||||||
msg_id = send_thinking(cfg.telegram, cfg.telegram.chat_id, "📥 获取邮件中...")
|
if tg_msg_id:
|
||||||
logger.info(f" 获取邮件中消息已发送: msg_id={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, "⏳ 等待处理...")
|
||||||
|
|
||||||
# 阶段2:AI 处理,更新为思考中
|
# 阶段2:AI 处理,更新为思考中
|
||||||
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 = summarize_email(cfg.ai, mail.recipient, mail.subject, mail.sender, mail.body, mail.account_email)
|
||||||
summary["recipient"] = mail.recipient
|
summary["recipient"] = mail.recipient
|
||||||
if "@" not in summary.get("sender", ""):
|
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,
|
"account_email": mail.account_email,
|
||||||
"uid": mail.uid,
|
"uid": mail.uid,
|
||||||
"folder": mail.folder,
|
"folder": mail.folder,
|
||||||
"thinking_msg_id": msg_id,
|
"thinking_msg_id": tg_msg_id if tg_msg_id else 0,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user