v1.1.1: 升级 AI 续聘建议为 LLM + Playwright RPA 采集 SmartCampus 平台材料
新增:
- 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>
This commit is contained in:
@@ -104,7 +104,7 @@ def get_connection(db_path: Path | None = None) -> sqlite3.Connection:
|
||||
|
||||
|
||||
def init_db(db_path: Path | None = None) -> dict[str, int]:
|
||||
"""建表 + 加载种子数据。返回表行数 dict。"""
|
||||
"""建表 + 加载种子数据 + 应用 v1.1.1 迁移。返回表行数 dict。"""
|
||||
ensure_dirs()
|
||||
conn = get_connection(db_path)
|
||||
try:
|
||||
@@ -112,6 +112,12 @@ def init_db(db_path: Path | None = None) -> dict[str, int]:
|
||||
conn.execute(ddl)
|
||||
conn.commit()
|
||||
|
||||
# v1.1.1 增量迁移(幂等)
|
||||
from db.migrations import apply_all
|
||||
|
||||
apply_all(conn)
|
||||
conn.commit()
|
||||
|
||||
# 加载种子(如果 contracts 表为空)
|
||||
from db.seed_data import load_seed_if_empty
|
||||
|
||||
@@ -124,6 +130,7 @@ def init_db(db_path: Path | None = None) -> dict[str, int]:
|
||||
"alerts": conn.execute("SELECT COUNT(*) AS n FROM alerts").fetchone()["n"],
|
||||
"renewal_tasks": conn.execute("SELECT COUNT(*) AS n FROM renewal_tasks").fetchone()["n"],
|
||||
"dispatch_log": conn.execute("SELECT COUNT(*) AS n FROM dispatch_log").fetchone()["n"],
|
||||
"materials_content": conn.execute("SELECT COUNT(*) AS n FROM materials_content").fetchone()["n"],
|
||||
}
|
||||
return counts
|
||||
finally:
|
||||
@@ -131,4 +138,7 @@ def init_db(db_path: Path | None = None) -> dict[str, int]:
|
||||
|
||||
|
||||
def table_names() -> Iterable[str]:
|
||||
return ("contracts", "performance_records", "alerts", "renewal_tasks", "dispatch_log")
|
||||
return (
|
||||
"contracts", "performance_records", "alerts", "renewal_tasks",
|
||||
"dispatch_log", "materials_content",
|
||||
)
|
||||
|
||||
56
scripts/db/migrations.py
Normal file
56
scripts/db/migrations.py
Normal file
@@ -0,0 +1,56 @@
|
||||
"""增量迁移:在 init_db 之外支持 idempotent ALTER/CREATE。
|
||||
|
||||
v1.1.1 新增:
|
||||
- materials_content 表(材料包 PDF/Word 解析缓存)
|
||||
- renewal_tasks.decision_source 列(AI 建议来源透明化)
|
||||
- renewal_tasks.materials_summary 列(材料摘要缓存,便于离线渲染)
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import sqlite3
|
||||
|
||||
|
||||
def _column_exists(conn: sqlite3.Connection, table: str, column: str) -> bool:
|
||||
rows = conn.execute(f"PRAGMA table_info({table})").fetchall()
|
||||
return any(r["name"] == column for r in rows)
|
||||
|
||||
|
||||
def ensure_materials_content_schema(conn: sqlite3.Connection) -> None:
|
||||
"""创建 materials_content 表(如不存在)。幂等。"""
|
||||
conn.execute(
|
||||
"""
|
||||
CREATE TABLE IF NOT EXISTS materials_content (
|
||||
contract_id TEXT NOT NULL,
|
||||
file_name TEXT NOT NULL,
|
||||
file_path TEXT NOT NULL,
|
||||
extracted_text TEXT,
|
||||
parse_method TEXT,
|
||||
parsed_at TEXT NOT NULL,
|
||||
PRIMARY KEY (contract_id, file_name),
|
||||
FOREIGN KEY (contract_id) REFERENCES contracts(contract_id)
|
||||
);
|
||||
"""
|
||||
)
|
||||
conn.execute(
|
||||
"CREATE INDEX IF NOT EXISTS idx_materials_contract "
|
||||
"ON materials_content(contract_id);"
|
||||
)
|
||||
|
||||
|
||||
def ensure_renewal_tasks_columns(conn: sqlite3.Connection) -> None:
|
||||
"""为 renewal_tasks 加 decision_source / materials_summary 列(如不存在)。"""
|
||||
if not _column_exists(conn, "renewal_tasks", "decision_source"):
|
||||
conn.execute(
|
||||
"ALTER TABLE renewal_tasks ADD COLUMN decision_source TEXT DEFAULT 'rules';"
|
||||
)
|
||||
if not _column_exists(conn, "renewal_tasks", "materials_summary"):
|
||||
conn.execute(
|
||||
"ALTER TABLE renewal_tasks ADD COLUMN materials_summary TEXT DEFAULT '';"
|
||||
)
|
||||
|
||||
|
||||
def apply_all(conn: sqlite3.Connection) -> None:
|
||||
"""应用全部 v1.1.1 迁移。"""
|
||||
ensure_materials_content_schema(conn)
|
||||
ensure_renewal_tasks_columns(conn)
|
||||
@@ -126,13 +126,16 @@ def insert_task(
|
||||
reason: str,
|
||||
summary: str,
|
||||
checklist: list[str],
|
||||
decision_source: str = "rules",
|
||||
materials_summary: str = "",
|
||||
) -> None:
|
||||
conn.execute(
|
||||
"""
|
||||
INSERT OR REPLACE INTO renewal_tasks
|
||||
(task_id, contract_id, alert_id, ai_suggestion, suggestion_reason,
|
||||
performance_summary, materials_checklist, generated_at, status)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, 'pending')
|
||||
performance_summary, materials_checklist, generated_at, status,
|
||||
decision_source, materials_summary)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, 'pending', ?, ?)
|
||||
""",
|
||||
(
|
||||
task_id,
|
||||
@@ -143,6 +146,8 @@ def insert_task(
|
||||
summary,
|
||||
json.dumps(checklist, ensure_ascii=False),
|
||||
datetime.now().isoformat(timespec="seconds"),
|
||||
decision_source,
|
||||
materials_summary,
|
||||
),
|
||||
)
|
||||
|
||||
@@ -160,6 +165,22 @@ def get_task(conn: sqlite3.Connection, task_id: str) -> sqlite3.Row | None:
|
||||
).fetchone()
|
||||
|
||||
|
||||
def get_task_full(conn: sqlite3.Connection, task_id: str) -> sqlite3.Row | None:
|
||||
"""v1.1.1:拉取 task + 关联合同 + alert(含 days_to_expire/level)。"""
|
||||
return conn.execute(
|
||||
"""
|
||||
SELECT t.*, c.teacher_name, c.unit_name, c.position, c.contract_type,
|
||||
c.contract_start, c.contract_end, c.employee_id, c.unit_id,
|
||||
a.days_to_expire AS alert_days, a.alert_level AS alert_level
|
||||
FROM renewal_tasks t
|
||||
JOIN contracts c ON c.contract_id = t.contract_id
|
||||
LEFT JOIN alerts a ON a.alert_id = t.alert_id
|
||||
WHERE t.task_id = ?
|
||||
""",
|
||||
(task_id,),
|
||||
).fetchone()
|
||||
|
||||
|
||||
# ---------- dispatch_log ----------
|
||||
|
||||
def insert_dispatch_log(
|
||||
@@ -193,3 +214,86 @@ def list_dispatch_logs(conn: sqlite3.Connection) -> list[sqlite3.Row]:
|
||||
return conn.execute(
|
||||
"SELECT * FROM dispatch_log ORDER BY dispatched_at DESC"
|
||||
).fetchall()
|
||||
|
||||
|
||||
# ---------- materials_content(v1.1.1 新增) ----------
|
||||
|
||||
def list_materials_for_contract(
|
||||
conn: sqlite3.Connection, contract_id: str
|
||||
) -> list[sqlite3.Row]:
|
||||
"""列出某合同的全部材料文件。"""
|
||||
return conn.execute(
|
||||
"SELECT * FROM materials_content WHERE contract_id = ? ORDER BY parsed_at ASC",
|
||||
(contract_id,),
|
||||
).fetchall()
|
||||
|
||||
|
||||
def insert_material(
|
||||
conn: sqlite3.Connection,
|
||||
contract_id: str,
|
||||
file_name: str,
|
||||
file_path: str,
|
||||
extracted_text: str,
|
||||
parse_method: str,
|
||||
) -> None:
|
||||
conn.execute(
|
||||
"""
|
||||
INSERT OR REPLACE INTO materials_content
|
||||
(contract_id, file_name, file_path, extracted_text,
|
||||
parse_method, parsed_at)
|
||||
VALUES (?, ?, ?, ?, ?, ?)
|
||||
""",
|
||||
(
|
||||
contract_id,
|
||||
file_name,
|
||||
file_path,
|
||||
extracted_text,
|
||||
parse_method,
|
||||
datetime.now().isoformat(timespec="seconds"),
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
def get_material(
|
||||
conn: sqlite3.Connection, contract_id: str, file_name: str
|
||||
) -> sqlite3.Row | None:
|
||||
return conn.execute(
|
||||
"SELECT * FROM materials_content WHERE contract_id = ? AND file_name = ?",
|
||||
(contract_id, file_name),
|
||||
).fetchone()
|
||||
|
||||
|
||||
def list_pending_materials(conn: sqlite3.Connection) -> list[sqlite3.Row]:
|
||||
"""列出待解析的材料(extracted_text IS NULL)。"""
|
||||
return conn.execute(
|
||||
"SELECT * FROM materials_content "
|
||||
"WHERE extracted_text IS NULL OR parse_method = 'pending' "
|
||||
"ORDER BY parsed_at ASC"
|
||||
).fetchall()
|
||||
|
||||
|
||||
def summarize_materials(
|
||||
conn: sqlite3.Connection, contract_id: str
|
||||
) -> tuple[str, list[str]]:
|
||||
"""汇总某合同所有已解析材料的文本 + 文件名列表。
|
||||
|
||||
返回 (summary_text, file_names)。
|
||||
"""
|
||||
rows = list_materials_for_contract(conn, contract_id)
|
||||
if not rows:
|
||||
return ("", [])
|
||||
|
||||
file_names: list[str] = []
|
||||
parts: list[str] = []
|
||||
for r in rows:
|
||||
text = r["extracted_text"] or ""
|
||||
if not text:
|
||||
continue
|
||||
file_names.append(r["file_name"])
|
||||
parts.append(f"### {r['file_name']}\n{text}")
|
||||
|
||||
summary = "\n\n".join(parts)
|
||||
# 截断 16k 字避免 LLM context 爆栈
|
||||
if len(summary) > 16000:
|
||||
summary = summary[:16000] + "\n...(材料过长已截断)"
|
||||
return (summary, file_names)
|
||||
|
||||
Reference in New Issue
Block a user