feat: enhance logging detail across all modules
- email_client: log IMAP connection, login, UNSEEN count, each email details (subject/sender/body size), mark-as-seen progress - ai_client: log AI request params, timing, token usage, response size - smtp_client: log SMTP connect, login, send details - tg_bot: log all callback actions with subject context, message states - main: periodic queue depth report (email_queue/tg_queue/pending_uids)
This commit is contained in:
@@ -1,9 +1,13 @@
|
||||
import json
|
||||
import logging
|
||||
import time
|
||||
from typing import Any
|
||||
import requests
|
||||
from src.config import AIConfig
|
||||
from src.retry import retry
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
SYSTEM_PROMPT = """你是一个邮件摘要助手。请分析邮件内容并以 JSON 格式返回结构化摘要。
|
||||
|
||||
返回格式必须严格遵循以下 JSON schema:
|
||||
@@ -35,6 +39,8 @@ SYSTEM_PROMPT = """你是一个邮件摘要助手。请分析邮件内容并以
|
||||
|
||||
@retry()
|
||||
def summarize_email(ai_cfg: AIConfig, recipient: str, subject: str, sender: str, body: str) -> dict[str, Any]:
|
||||
body_preview = body[:80].replace("\n", " ")
|
||||
logger.info("AI 摘要请求: sender=%s subject=%s body_preview=%s", sender, subject, body_preview)
|
||||
content = f"收件人: {recipient}\n发件人: {sender}\n主题: {subject}\n正文:\n{body[:4000]}"
|
||||
|
||||
payload = {
|
||||
@@ -68,6 +74,8 @@ MORE_REPLIES_PROMPT = """你是一个邮件回复助手。根据以下邮件内
|
||||
@retry()
|
||||
def generate_more_replies(ai_cfg: AIConfig, sender: str, subject: str, body: str,
|
||||
previous_suggestions: list[dict], user_hint: str = "") -> list[dict]:
|
||||
logger.info("AI 换批请求: sender=%s subject=%s user_hint=%s prev=%d 条",
|
||||
sender, subject, user_hint or "(无)", len(previous_suggestions))
|
||||
content = f"发件人: {sender}\n主题: {subject}\n正文:\n{body[:4000]}\n\n已生成过的回复:\n"
|
||||
for i, s in enumerate(previous_suggestions, 1):
|
||||
content += f"{i}. {s['text']}\n"
|
||||
@@ -87,6 +95,9 @@ def generate_more_replies(ai_cfg: AIConfig, sender: str, subject: str, body: str
|
||||
|
||||
|
||||
def _call_deepseek(ai_cfg: AIConfig, payload: dict) -> dict:
|
||||
model = payload.get("model", ai_cfg.model)
|
||||
logger.info("DeepSeek API 请求: model=%s", model)
|
||||
t0 = time.time()
|
||||
headers = {
|
||||
"Authorization": f"Bearer {ai_cfg.api_key}",
|
||||
"Content-Type": "application/json",
|
||||
@@ -97,7 +108,13 @@ def _call_deepseek(ai_cfg: AIConfig, payload: dict) -> dict:
|
||||
json=payload,
|
||||
timeout=60,
|
||||
)
|
||||
elapsed = time.time() - t0
|
||||
resp.raise_for_status()
|
||||
result = resp.json()
|
||||
usage = result.get("usage", {})
|
||||
prompt_tokens = usage.get("prompt_tokens", "?")
|
||||
completion_tokens = usage.get("completion_tokens", "?")
|
||||
raw = result["choices"][0]["message"]["content"]
|
||||
logger.info("DeepSeek API 响应: %.2fs, token=%d+%d, 输出长度=%d",
|
||||
elapsed, prompt_tokens, completion_tokens, len(raw))
|
||||
return json.loads(raw)
|
||||
|
||||
Reference in New Issue
Block a user