新增:
- scripts/service/platform_collector.py:Playwright RPA(持久化 browser profile 复用登录态),抓合同表(姓名/单位/到期日/剩余天数/预警/建议),逐行点"续聘材料包"按钮下载 PDF/Word 到 data/materials/
- scripts/service/material_parser.py:PDF/Word 文本提取(pypdf → pdf2image + pytesseract OCR 扫描件兜底;python-docx 抽 docx;antiword 兜底 .doc)
- scripts/service/llm_decision.py:AI 决策统一入口(优先调宿主 jiangchang_skill_core.llm_manager.call_llm_json;本地降级到规则版;输出 decision_source ∈ {llm, rules, rules_no_materials})
- scripts/db/migrations.py:idempotent ALTER/CREATE,建 materials_content 表 + renewal_tasks 加 decision_source / materials_summary 列
修改:
- scripts/db/connection.py:init_db() 自动调 apply_all() 应用 v1.1.1 迁移
- scripts/db/repository.py:加 insert_material / list_materials_for_contract / list_pending_materials / summarize_materials / get_task_full
- scripts/service/task_generator.py:AI 决策统一改为调 llm_decision.decide_suggestion(materials_summary=...);reason 文案加「已结合材料包(X 份文件,Y 字 / 待解析)综合判断:...」
- scripts/service/html_renderer.py:ctx 加 DECISION_SOURCE(LLM 推理 / 本地规则 / 本地规则(无材料))+ MATERIALS_COUNT
- scripts/templates/task.html.j2:footer 加 "AI 建议来源:{{DECISION_SOURCE}} · 已审阅材料 {{MATERIALS_COUNT}} 份"
- scripts/cli/app.py:新增 2 个 CLI(collect-from-platform / parse-materials);现有 9 个不动
- requirements.txt:playwright / pypdf / python-docx / pdf2image / pytesseract / Pillow
- .env.example:SMARTCAMPUS_BASE_URL / SMARTCAMPUS_PROFILE_DIR / MATERIALS_DOWNLOAD_DIR / OCR_LANG / LLM_MODEL_NAME / LLM_API_BASE_URL / LLM_API_TOKEN
- SKILL.md:version 1.1.0 → 1.1.1;用户意图路由表加 2 行;新增「数据来源」段(三层来源 + AI 决策透明化)
- README.md:使用前要准备 → 加 playwright install chromium 步骤
- CHANGELOG.md:v1.1.1 条目
- scripts/util/constants.py:SKILL_VERSION 1.1.0 → 1.1.1
本地验证:11 个 CLI 全部 exit 0;decision_source=rules;reason 含「已结合材料包(6 份文件,待解析)」;HTML footer 含决策来源。
Co-Authored-By: Claude Code <noreply@anthropic.com>
133 lines
4.8 KiB
Python
133 lines
4.8 KiB
Python
"""HTML 报告渲染:Jinja2 模板 → 写 output/<task_id>.html。
|
||
|
||
模板:scripts/templates/task.html.j2
|
||
占位替换规则:
|
||
- {{TITLE}}, {{TASK_ID}}, {{GENERATED_AT}}
|
||
- {{TEACHER_NAME}}, {{EMPLOYEE_ID}}, {{UNIT_NAME}}, {{POSITION}}, {{CONTRACT_TYPE}}
|
||
- {{CONTRACT_START}}, {{CONTRACT_END}}, {{DAYS_TO_EXPIRE}}, {{ALERT_LEVEL}}
|
||
- {{PERFORMANCE_SUMMARY}}
|
||
- {{SUGGESTION_CLASS}}, {{SUGGESTION_LABEL}}, {{SUGGESTION_REASON}}
|
||
- {{DEADLINE}}, {{CHECKLIST_BODY}}
|
||
"""
|
||
|
||
from __future__ import annotations
|
||
|
||
import json
|
||
import sqlite3
|
||
from datetime import date, datetime, timedelta
|
||
from pathlib import Path
|
||
|
||
from db import repository as repo
|
||
from util.constants import ALERT_LEVEL_LABELS, SUGGESTION_LABELS
|
||
from util.date_util import parse_iso_date
|
||
from util.logging_setup import get_skill_logger
|
||
from util.runtime_paths import get_output_dir, get_templates_dir
|
||
|
||
logger = get_skill_logger()
|
||
|
||
SUGGESTION_CLASS_MAP = {
|
||
"renew": "renew",
|
||
"observe": "observe",
|
||
"non_renew": "non-renew",
|
||
}
|
||
|
||
|
||
def render_task_html(conn: sqlite3.Connection, task_id: str) -> Path:
|
||
"""渲染指定 task 为 HTML 文件,返回输出路径。
|
||
|
||
不存在 task → 抛 ValueError。
|
||
"""
|
||
task = repo.get_task(conn, task_id)
|
||
if task is None:
|
||
raise ValueError(f"task_id={task_id!r} 不存在")
|
||
|
||
# 关联 alert 的 days_to_expire / level(用于显示)
|
||
alert = conn.execute(
|
||
"SELECT * FROM alerts WHERE alert_id = ?", (task["alert_id"],)
|
||
).fetchone() if task["alert_id"] else None
|
||
|
||
days_to_expire = alert["days_to_expire"] if alert else None
|
||
alert_level = alert["alert_level"] if alert else "yellow"
|
||
alert_level_label = ALERT_LEVEL_LABELS.get(alert_level, alert_level)
|
||
|
||
# 截止日 = 合同到期日 - 30 天(教师备齐材料的最后期限)
|
||
contract_end = parse_iso_date(task["contract_end"])
|
||
deadline = (contract_end - timedelta(days=30)).isoformat()
|
||
|
||
# 模板上下文
|
||
checklist_items = json.loads(task["materials_checklist"]) if task["materials_checklist"] else []
|
||
checklist_html = "\n".join(
|
||
f' <li><span class="box"></span>{item}</li>' for item in checklist_items
|
||
)
|
||
|
||
suggestion_class = SUGGESTION_CLASS_MAP.get(task["ai_suggestion"], "observe")
|
||
suggestion_label = SUGGESTION_LABELS.get(task["ai_suggestion"], task["ai_suggestion"])
|
||
|
||
# v1.1.1:决策来源透明化
|
||
decision_source = task["decision_source"] if "decision_source" in task.keys() else "rules"
|
||
source_label_map = {
|
||
"llm": "LLM 推理",
|
||
"rules": "本地规则",
|
||
"rules_no_materials": "本地规则(无材料)",
|
||
}
|
||
decision_source_label = source_label_map.get(decision_source, decision_source)
|
||
|
||
# v1.1.1:材料计数(来自 materials_content)
|
||
materials_count = conn.execute(
|
||
"SELECT COUNT(*) AS n FROM materials_content "
|
||
"WHERE contract_id = ? AND extracted_text IS NOT NULL",
|
||
(task["contract_id"],),
|
||
).fetchone()["n"]
|
||
|
||
ctx = {
|
||
"TITLE": "续聘准备任务书",
|
||
"TASK_ID": task_id,
|
||
"GENERATED_AT": task["generated_at"],
|
||
"TEACHER_NAME": task["teacher_name"],
|
||
"EMPLOYEE_ID": task["employee_id"],
|
||
"UNIT_NAME": task["unit_name"],
|
||
"POSITION": task["position"],
|
||
"CONTRACT_TYPE": task["contract_type"],
|
||
"CONTRACT_START": task["contract_start"],
|
||
"CONTRACT_END": task["contract_end"],
|
||
"DAYS_TO_EXPIRE": days_to_expire if days_to_expire is not None else "—",
|
||
"ALERT_LEVEL": alert_level_label,
|
||
"PERFORMANCE_SUMMARY": task["performance_summary"],
|
||
"SUGGESTION_CLASS": suggestion_class,
|
||
"SUGGESTION_LABEL": suggestion_label,
|
||
"SUGGESTION_REASON": task["suggestion_reason"],
|
||
"DEADLINE": deadline,
|
||
"CHECKLIST_BODY": checklist_html,
|
||
"DECISION_SOURCE": decision_source_label,
|
||
"MATERIALS_COUNT": materials_count,
|
||
}
|
||
|
||
template_path = get_templates_dir() / "task.html.j2"
|
||
if not template_path.exists():
|
||
raise FileNotFoundError(f"模板不存在: {template_path}")
|
||
|
||
html = _render_template(template_path, ctx)
|
||
|
||
output_dir = get_output_dir()
|
||
output_dir.mkdir(parents=True, exist_ok=True)
|
||
output_path = output_dir / f"{task_id}.html"
|
||
output_path.write_text(html, encoding="utf-8")
|
||
|
||
logger.info("html_renderer: %s → %s", task_id, output_path)
|
||
return output_path
|
||
|
||
|
||
def _render_template(template_path: Path, ctx: dict) -> str:
|
||
"""用 Jinja2 渲染模板;本地无 jinja2 时降级到占位符替换。"""
|
||
template_text = template_path.read_text(encoding="utf-8")
|
||
|
||
try:
|
||
from jinja2 import Template
|
||
|
||
return Template(template_text).render(**ctx)
|
||
except ImportError:
|
||
# 降级:纯 str.replace(最简实现)
|
||
for k, v in ctx.items():
|
||
template_text = template_text.replace("{{" + k + "}}", str(v))
|
||
return template_text
|