Compare commits
12 Commits
8c088ddff5
...
5113d78662
| Author | SHA1 | Date | |
|---|---|---|---|
| 5113d78662 | |||
| 8b885f1c4c | |||
| 8b577b4c12 | |||
| 26468aa43d | |||
| 98147e6a6c | |||
| bd5b824a6a | |||
| 01504e6295 | |||
| 02b0941ed0 | |||
| 85b02651b9 | |||
| e74ae8be88 | |||
| f38259e4bb | |||
| 5368812e45 |
25
main.py
25
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)
|
||||||
@@ -28,6 +28,7 @@ _log_listener.start()
|
|||||||
logger = logging.getLogger("main")
|
logger = logging.getLogger("main")
|
||||||
|
|
||||||
_running = True
|
_running = True
|
||||||
|
_shutdown_event = threading.Event()
|
||||||
_pending_lock = threading.Lock()
|
_pending_lock = threading.Lock()
|
||||||
_pending_uids: set[bytes] = set()
|
_pending_uids: set[bytes] = set()
|
||||||
_processing_uids: set[bytes] = set() # 正在处理(含等待TG发送)的邮件UID
|
_processing_uids: set[bytes] = set() # 正在处理(含等待TG发送)的邮件UID
|
||||||
@@ -42,6 +43,7 @@ def _signal_handler(signum, frame):
|
|||||||
global _running
|
global _running
|
||||||
logger.info("收到退出信号,正在停止...")
|
logger.info("收到退出信号,正在停止...")
|
||||||
_running = False
|
_running = False
|
||||||
|
_shutdown_event.set()
|
||||||
|
|
||||||
|
|
||||||
def _email_poller(cfg):
|
def _email_poller(cfg):
|
||||||
@@ -55,15 +57,17 @@ 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)
|
||||||
if _running:
|
if _running:
|
||||||
for _ in range(cfg.polling.interval_seconds):
|
_shutdown_event.wait(cfg.polling.interval_seconds)
|
||||||
if not _running:
|
|
||||||
return
|
|
||||||
time.sleep(1)
|
|
||||||
|
|
||||||
|
|
||||||
def _ai_processor(cfg):
|
def _ai_processor(cfg):
|
||||||
@@ -74,10 +78,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()]
|
||||||
@@ -124,7 +128,7 @@ def _tg_worker(cfg):
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
if _running:
|
if _running:
|
||||||
time.sleep(1)
|
_shutdown_event.wait(1)
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
@@ -153,9 +157,10 @@ def main():
|
|||||||
if tick % 30 == 0:
|
if tick % 30 == 0:
|
||||||
logger.info("队列状态: email_queue=%d tg_queue=%d pending_uids=%d",
|
logger.info("队列状态: email_queue=%d tg_queue=%d pending_uids=%d",
|
||||||
_email_queue.qsize(), _tg_queue.qsize(), len(_pending_uids))
|
_email_queue.qsize(), _tg_queue.qsize(), len(_pending_uids))
|
||||||
time.sleep(1)
|
_shutdown_event.wait(1)
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
_running = False
|
_running = False
|
||||||
|
_shutdown_event.set()
|
||||||
|
|
||||||
for t in threads:
|
for t in threads:
|
||||||
t.join(timeout=5)
|
t.join(timeout=5)
|
||||||
|
|||||||
@@ -34,6 +34,7 @@ SYSTEM_PROMPT = """你是一个邮件摘要助手。请分析邮件内容并以
|
|||||||
- verification_code: 仅当 category 为 "notification" 且邮件包含验证码时填写,提取完整的验证码字符串(纯数字、字母数字混合等均可,如「123456」「abc-789」「701383」)。非验证码邮件留空字符串
|
- verification_code: 仅当 category 为 "notification" 且邮件包含验证码时填写,提取完整的验证码字符串(纯数字、字母数字混合等均可,如「123456」「abc-789」「701383」)。非验证码邮件留空字符串
|
||||||
- app_name: 仅当 category 为 "notification" 且邮件包含验证码时填写,填写发送验证码的应用/平台名称(如「GitHub」「微信」「Kraken」)。非验证码邮件留空字符串
|
- app_name: 仅当 category 为 "notification" 且邮件包含验证码时填写,填写发送验证码的应用/平台名称(如「GitHub」「微信」「Kraken」)。非验证码邮件留空字符串
|
||||||
- links: 仅提取以下两类链接:1)邮件明确要求用户点击才能完成某操作的链接(如确认邮箱、重置密码、查看账单、审批请求、查看通知详情等);2)退订/取消订阅链接。不要提取推广性质的链接(如下载App、注册领优惠、查看更多商品等营销链接)、追踪像素、邮件签名中的社交链接。每条包含简短描述文字和完整 URL。最多 3 条。没有符合条件的链接时为空数组
|
- links: 仅提取以下两类链接:1)邮件明确要求用户点击才能完成某操作的链接(如确认邮箱、重置密码、查看账单、审批请求、查看通知详情等);2)退订/取消订阅链接。不要提取推广性质的链接(如下载App、注册领优惠、查看更多商品等营销链接)、追踪像素、邮件签名中的社交链接。每条包含简短描述文字和完整 URL。最多 3 条。没有符合条件的链接时为空数组
|
||||||
|
- 退订链接提取规则:推广邮件必须提取退订链接。退订链接通常在邮件最底部,包含以下关键词之一:unsubscribe、退订、取消订阅、opt out、manage preferences、email preferences。即使链接被包裹在 HTML 标签中,也要提取 href 属性里的完整 URL。如果邮件底部同时有多个退订相关链接,只提取最主要的那个
|
||||||
- summary 要简洁,把需要知道的信息浓缩成一句话,不需要分条列出
|
- summary 要简洁,把需要知道的信息浓缩成一句话,不需要分条列出
|
||||||
- 当 category 为 "promotion" 时,summary 直接写成一句话:「[发送方名称] 在推广 [推广的产品/服务/活动]」,例如「腾讯云在推广双十一云服务器折扣」。此时 can_reply 为 false,reply_suggestions 为空数组
|
- 当 category 为 "promotion" 时,summary 直接写成一句话:「[发送方名称] 在推广 [推广的产品/服务/活动]」,例如「腾讯云在推广双十一云服务器折扣」。此时 can_reply 为 false,reply_suggestions 为空数组
|
||||||
- can_reply 为 false 表示无需回复或不适合建议回复(此时忽略 reply_suggestions)
|
- can_reply 为 false 表示无需回复或不适合建议回复(此时忽略 reply_suggestions)
|
||||||
|
|||||||
@@ -113,9 +113,8 @@ def fetch_unseen_emails(account: EmailAccount, skip_uids: set[bytes] | None = No
|
|||||||
conn = _login_and_prepare(account, select_inbox=False)
|
conn = _login_and_prepare(account, select_inbox=False)
|
||||||
if skip_uids is None:
|
if skip_uids is None:
|
||||||
skip_uids = set()
|
skip_uids = set()
|
||||||
logger.info("fetch_unseen_emails: %s, skip_uids大小=%d", account.username, len(skip_uids))
|
|
||||||
|
|
||||||
# 列出所有文件夹
|
# 列出所有文件夹,跳过 NoSelect 文件夹
|
||||||
_, folder_data = conn.list()
|
_, folder_data = conn.list()
|
||||||
folders = []
|
folders = []
|
||||||
if folder_data:
|
if folder_data:
|
||||||
@@ -123,6 +122,9 @@ def fetch_unseen_emails(account: EmailAccount, skip_uids: set[bytes] | None = No
|
|||||||
if item is None:
|
if item is None:
|
||||||
continue
|
continue
|
||||||
line = item.decode("utf-8", errors="replace")
|
line = item.decode("utf-8", errors="replace")
|
||||||
|
# 跳过 \NoSelect 文件夹(父文件夹,不能直接 SELECT)
|
||||||
|
if "\\NoSelect" in line:
|
||||||
|
continue
|
||||||
m = re.search(r'"([^"]*)"\s*$', line)
|
m = re.search(r'"([^"]*)"\s*$', line)
|
||||||
if m:
|
if m:
|
||||||
folders.append(m.group(1))
|
folders.append(m.group(1))
|
||||||
@@ -136,7 +138,7 @@ def fetch_unseen_emails(account: EmailAccount, skip_uids: set[bytes] | None = No
|
|||||||
try:
|
try:
|
||||||
status, _ = conn.select(folder, readonly=True)
|
status, _ = conn.select(folder, readonly=True)
|
||||||
if status != "OK":
|
if status != "OK":
|
||||||
logger.warning("无法选择文件夹: %s", folder)
|
logger.debug("无法选择文件夹: %s", folder)
|
||||||
continue
|
continue
|
||||||
_, data = conn.uid("SEARCH", None, "UNSEEN")
|
_, data = conn.uid("SEARCH", None, "UNSEEN")
|
||||||
uids = data[0].split() if data[0] else []
|
uids = data[0].split() if data[0] else []
|
||||||
@@ -148,11 +150,7 @@ def fetch_unseen_emails(account: EmailAccount, skip_uids: set[bytes] | None = No
|
|||||||
skipped += len(uids) - len(new_uids)
|
skipped += len(uids) - len(new_uids)
|
||||||
if not new_uids:
|
if not new_uids:
|
||||||
continue
|
continue
|
||||||
if skipped > 0 or len(uids) > 0:
|
logger.info(" %s: %d 封未读 (%d 封跳过)", folder, len(new_uids), len(uids) - len(new_uids))
|
||||||
logger.info(" %s: %d 封未读 (%d 封跳过) skip_uids=%d",
|
|
||||||
folder, len(new_uids), len(uids) - len(new_uids), len(skip_uids))
|
|
||||||
if skip_uids and len(skip_uids) <= 5:
|
|
||||||
logger.info(" skip_uids 内容: %s", [u for u in skip_uids])
|
|
||||||
|
|
||||||
for uid in new_uids:
|
for uid in new_uids:
|
||||||
try:
|
try:
|
||||||
@@ -206,7 +204,7 @@ def mark_as_seen(account: EmailAccount, uids_with_folder: list[tuple[bytes, str]
|
|||||||
by_folder[folder].append(uid)
|
by_folder[folder].append(uid)
|
||||||
for folder, uids in by_folder.items():
|
for folder, uids in by_folder.items():
|
||||||
try:
|
try:
|
||||||
conn.select(folder, readonly=True)
|
conn.select(folder, readonly=False)
|
||||||
for uid in uids:
|
for uid in uids:
|
||||||
conn.uid("STORE", uid, "+FLAGS", "\\Seen")
|
conn.uid("STORE", uid, "+FLAGS", "\\Seen")
|
||||||
logger.info(" %s: 标记 %d 封", folder, len(uids))
|
logger.info(" %s: 标记 %d 封", folder, len(uids))
|
||||||
|
|||||||
@@ -21,17 +21,22 @@ 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:显示等待处理
|
||||||
thinking_msg_id = send_thinking(cfg.telegram, cfg.telegram.chat_id)
|
if tg_msg_id:
|
||||||
logger.info(f" 思考中消息已发送: msg_id={thinking_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 处理,更新为思考中
|
||||||
|
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", ""):
|
||||||
summary["sender"] = mail.sender
|
summary["sender"] = mail.sender
|
||||||
text = format_summary(summary, mail.account_email)
|
text = format_summary(summary, mail.account_email)
|
||||||
|
# 阶段3:编辑为最终结果
|
||||||
return {
|
return {
|
||||||
"text": text,
|
"text": text,
|
||||||
"data": summary,
|
"data": summary,
|
||||||
@@ -43,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": thinking_msg_id,
|
"thinking_msg_id": tg_msg_id if tg_msg_id else 0,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -57,10 +57,10 @@ def send_text(tg_cfg: TelegramConfig, chat_id: str, text: str,
|
|||||||
return result["result"]["message_id"]
|
return result["result"]["message_id"]
|
||||||
|
|
||||||
|
|
||||||
def send_thinking(tg_cfg: TelegramConfig, chat_id: str) -> int:
|
def send_thinking(tg_cfg: TelegramConfig, chat_id: str, text: str = "💭 思考中…") -> int:
|
||||||
result = _tg_req(tg_cfg, "sendMessage", {
|
result = _tg_req(tg_cfg, "sendMessage", {
|
||||||
"chat_id": chat_id,
|
"chat_id": chat_id,
|
||||||
"text": "💭 思考中...",
|
"text": text,
|
||||||
})
|
})
|
||||||
return result["result"]["message_id"]
|
return result["result"]["message_id"]
|
||||||
|
|
||||||
@@ -345,10 +345,10 @@ def _handle_callback(tg_cfg: TelegramConfig, cfg: Config, cb: dict):
|
|||||||
if not conv or conv.get("summary_msg_id") != msg_id:
|
if not conv or conv.get("summary_msg_id") != msg_id:
|
||||||
return
|
return
|
||||||
logger.info(" 换一批 (已有 %d 条历史)", len(conv.get("all_suggestions", [])))
|
logger.info(" 换一批 (已有 %d 条历史)", len(conv.get("all_suggestions", [])))
|
||||||
# 先显示思考中
|
# 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": "💭 思考中...", "parse_mode": None,
|
"text": "🤖 AI思考中...", "parse_mode": None,
|
||||||
})
|
})
|
||||||
try:
|
try:
|
||||||
new_suggestions = generate_more_replies(
|
new_suggestions = generate_more_replies(
|
||||||
@@ -390,10 +390,9 @@ def _handle_callback(tg_cfg: TelegramConfig, cfg: Config, cb: dict):
|
|||||||
logger.info(" 取消, 删除提示消息")
|
logger.info(" 取消, 删除提示消息")
|
||||||
|
|
||||||
elif action == CALLBACK_PROMO_DETAIL and ctx:
|
elif action == CALLBACK_PROMO_DETAIL and ctx:
|
||||||
# 先显示思考中
|
|
||||||
_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": "💭 思考中...", "parse_mode": None,
|
"text": "🤖 AI思考中...", "parse_mode": None,
|
||||||
})
|
})
|
||||||
try:
|
try:
|
||||||
details = expand_promo_detail(
|
details = expand_promo_detail(
|
||||||
|
|||||||
Reference in New Issue
Block a user