feat: IMAP mail sync with thread-to-contact matching (PRD §6.1)

Real IMAP connection via imapflow library:
- Connects to IMAP server (IMAPFlow, TLS on port 993)
- Fetches recent messages (configurable limit, default 20)
- Extracts: from, fromEmail, subject, date, snippet (200 chars)
- Thread-to-contact matching (PRD §6.1):
  1. Match sender email against persons table
  2. If no person match, extract domain and match against company.website
  3. Returns matchedCompany + matchedPerson for each email
- Audit-logged (action: crm.mailsync:imap)
- Zod validated (host, port, user, password, folder, limit)
- 502 on IMAP connection failure with detail
- 400 on validation failure

Tested: invalid creds → 502 'IMAP connection failed', missing host → 400
This commit is contained in:
Z User committed 2026-10-27 22:50:50 +00:00
1 parent 7acc0c5451
commit e45b68bb8b
1089 files changed
+260614

No files matched your search

+47
View File
@@ -0,0 +1,47 @@
#!/usr/bin/env python3
"""从学习资料生成知识点提取 prompt。
用法:python3 generate_from_material.py <file_path> <document_id>
输出:JSON 格式的 prompt(system_prompt + user_prompt),由 agent 发给 LLM 执行。
LLM 返回知识点 JSON 后,agent 应调用 service.save_knowledge_points() 保存。
"""
from pathlib import Path
import json
import sys
sys.path.append(str(Path(__file__).resolve().parents[1] / "src"))
from quiz_mastery.file_parser import parse_file, build_extraction_prompt
def main() -> None:
if len(sys.argv) < 3:
print("Usage: generate_from_material.py <file_path> <document_id>")
print(" file_path: Path to study material (.md, .txt, .text)")
print(" document_id: Identifier for this document")
sys.exit(1)
file_path = sys.argv[1]
document_id = sys.argv[2]
content = parse_file(file_path)
prompts = build_extraction_prompt(content)
output = {
"action": "extract_knowledge_points",
"document_id": document_id,
"file_path": file_path,
"prompts": prompts,
"instructions": (
"Send the system_prompt and user_prompt to an LLM. "
"The LLM should return a JSON array of knowledge points. "
"Then call save_knowledge_points(document_id, knowledge_points) to save."
),
}
print(json.dumps(output, ensure_ascii=False, indent=2))
if __name__ == "__main__":
main()
+51
View File
@@ -0,0 +1,51 @@
#!/usr/bin/env python3
"""从题目文件导入题目。
用法:python3 import_quiz.py <file_path> <document_id> <user_id>
输出:JSON 格式的 prompt(system_prompt + user_prompt),由 agent 发给 LLM 解析题目。
LLM 返回题目 JSON 后,agent 应调用 service.import_questions() 导入。
"""
from pathlib import Path
import json
import sys
sys.path.append(str(Path(__file__).resolve().parents[1] / "src"))
from quiz_mastery.file_parser import parse_file
from quiz_mastery.quiz_extractor import build_extraction_prompt
def main() -> None:
if len(sys.argv) < 4:
print("Usage: import_quiz.py <file_path> <document_id> <user_id>")
print(" file_path: Path to question file (.md, .txt, .text)")
print(" document_id: Identifier for this document")
print(" user_id: User identifier")
sys.exit(1)
file_path = sys.argv[1]
document_id = sys.argv[2]
user_id = sys.argv[3]
content = parse_file(file_path)
prompts = build_extraction_prompt(content)
output = {
"action": "import_questions",
"document_id": document_id,
"user_id": user_id,
"file_path": file_path,
"prompts": prompts,
"instructions": (
"Send the system_prompt and user_prompt to an LLM. "
"The LLM should return a JSON array of questions. "
"Then call service.import_questions(document_id, user_id, questions) to import."
),
}
print(json.dumps(output, ensure_ascii=False, indent=2))
if __name__ == "__main__":
main()
+63
View File
@@ -0,0 +1,63 @@
#!/usr/bin/env python3
"""生成出题 prompt。
用法:python3 run_quiz.py <user_id> <document_id>
根据已保存的知识点和用户掌握度记录,自动决定出题难度。
- 从 mastery_records 读取每个知识点的 current_level
- 首次出题的知识点强制 level=1
- 输出 JSON 格式的 prompt(system_prompt + user_prompt),由 agent 发给 LLM 生成题目
也支持指定知识点和难度:
python3 run_quiz.py <user_id> <document_id> [level] [kp_id1,kp_id2,...]
"""
from pathlib import Path
import json
import sys
sys.path.append(str(Path(__file__).resolve().parents[1] / "src"))
from quiz_mastery import QuizMasteryService
def main() -> None:
if len(sys.argv) < 3:
print("Usage: run_quiz.py <user_id> <document_id> [level] [kp_id1,kp_id2,...]")
print(" user_id: User identifier")
print(" document_id: Document identifier")
print(" level: Optional difficulty level (1/2/3)")
print(" kp_ids: Optional comma-separated knowledge point IDs")
sys.exit(1)
user_id = sys.argv[1]
document_id = sys.argv[2]
level = None
kp_ids = None
if len(sys.argv) >= 4:
try:
level = int(sys.argv[3])
except ValueError:
# Maybe it's kp_ids instead
kp_ids = sys.argv[3].split(",")
if len(sys.argv) >= 5:
kp_ids = sys.argv[4].split(",")
service = QuizMasteryService(
base_dir=Path(__file__).resolve().parents[1] / "data"
)
result = service.generate_quiz_for_user(
user_id=user_id,
document_id=document_id,
knowledge_point_ids=kp_ids,
level=level,
)
print(json.dumps(result, ensure_ascii=False, indent=2))
if __name__ == "__main__":
main()
+48
View File
@@ -0,0 +1,48 @@
#!/usr/bin/env python3
"""提交测验答案并评分。
用法:python3 submit_answers.py <user_id> <document_id> <session_id> <answers_json>
参数:
user_id: 用户标识
document_id: 文档标识
session_id: 测验会话 ID
answers_json: JSON 格式的答案字典,如 '{"q_001":"A","q_002":"True"}'
输出:评分结果 JSON(score, total, accuracy, results)。
"""
from pathlib import Path
import json
import sys
sys.path.append(str(Path(__file__).resolve().parents[1] / "src"))
from quiz_mastery import QuizMasteryService
def main() -> None:
if len(sys.argv) < 5:
print("Usage: submit_answers.py <user_id> <document_id> <session_id> <answers_json>")
sys.exit(1)
user_id = sys.argv[1]
document_id = sys.argv[2]
session_id = sys.argv[3]
answers = json.loads(sys.argv[4])
service = QuizMasteryService(
base_dir=Path(__file__).resolve().parents[1] / "data"
)
result = service.submit_quiz_answers(
user_id=user_id,
document_id=document_id,
session_id=session_id,
answers=answers,
)
print(json.dumps(result, ensure_ascii=False, indent=2))
if __name__ == "__main__":
main()