Compare commits
13 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 430274dbf9 | |||
| 0157fde0f1 | |||
| aa9bf262fe | |||
| 52831b80e0 | |||
| 17a7a54349 | |||
| c21339ae44 | |||
| 9878f570d7 | |||
| 44088336b4 | |||
| 293b7d1591 | |||
| 693a5e4a53 | |||
| c05b765b9d | |||
| df5f1b7223 | |||
| ec786b3480 |
19
.env.example
19
.env.example
@@ -67,3 +67,22 @@ LLM_API_BASE_URL=
|
||||
# LLM API token(生产由宿主注入;本地留空)
|
||||
LLM_API_TOKEN=
|
||||
|
||||
# ----- v1.1.18:日志落文件 -----
|
||||
# 日志文件路径(默认 {data_dir}/logs/alert-contract-expiring-hr.log);留空 = 仅 stderr
|
||||
LOG_FILE=
|
||||
# 单文件最大字节数(默认 5MB)
|
||||
LOG_MAX_BYTES=
|
||||
# 滚动备份保留份数(默认 5)
|
||||
LOG_BACKUP_COUNT=
|
||||
|
||||
# ----- v1.1.18:用户主动输入目录 -----
|
||||
# 用户主动投放目录(默认 {data_dir}/input)
|
||||
# 子目录约定:
|
||||
# input/contracts/*.csv → import-input 自动 upsert 到 contracts 表
|
||||
# input/materials/{contract_id}__{filename}.{pdf|docx|doc} → 自动入 materials_content
|
||||
INPUT_DIR=
|
||||
|
||||
# ----- v1.1.18:材料下载队列 -----
|
||||
# 失败最大重试次数(默认 3)
|
||||
DOWNLOAD_QUEUE_MAX_ATTEMPTS=
|
||||
|
||||
|
||||
106
CHANGELOG.md
106
CHANGELOG.md
@@ -2,6 +2,112 @@
|
||||
|
||||
本文件对应技能市场 **「更新日志」** Tab。
|
||||
|
||||
## 1.1.26
|
||||
|
||||
- **按用户最新要求改回「自动点登录」行为**:「到了 /industries/campus/smartcampus/login 整个页面,有账号和密码可用直接点登录跳转」
|
||||
- `_wait_for_login_inputs_filled` 改回 v1.1.21 行为:input 都有值 → 自动点 Sign in / 登录按钮 → return True
|
||||
- 与 `_try_auto_sign_in` 行为统一:登录页 = 「有数据自动点」/ 「无数据等用户输入」
|
||||
- 测试替换:`wait_login_inputs_filled_no_auto_click_v1_24` → `wait_login_inputs_filled_auto_click_v1_26`,合计 63/63
|
||||
|
||||
## 1.1.25
|
||||
|
||||
- **按用户要求修正登录页等待逻辑**:
|
||||
- 有数据(账号密码已存在)→ 自动点登录按钮(`_try_auto_sign_in`)
|
||||
- **无数据 → 等待用户输入完成(最多 180 秒)→ 等 URL 跳离登录页**(按钮交给用户自己点,脚本不抢按钮)
|
||||
- `_wait_for_login_inputs_filled` 重写为「**等待模式**」:不再自动点 Sign in / 登录按钮;只轮询 input 值 + URL,每 15 秒提示「请点登录按钮」或「等待输入」
|
||||
- 根域登录 + SPA 二级登录页统一使用同一套行为
|
||||
- 新增测试 `wait_login_inputs_filled_no_auto_click_v1_24` 验证不抢按钮,合计 63/63
|
||||
|
||||
## 1.1.24
|
||||
|
||||
- **修复 `/industries/campus/smartcampus/login` 等待过久问题**:sandbox 在用户首次登录后可能把人踢回 SPA 二级登录页(按钮中文「登录」),之前根域自动登录代码不识别这个 URL + 中文按钮,导致脚本卡死等用户输入
|
||||
- `_try_auto_sign_in` 扩展为通用版本:支持 `#jc-user` / `input[type='tel']` / `input[autocomplete='username']` 多种 user input selector;按钮文本按优先级匹配 `Sign in` > `登录` > `Login` > `signin` > `submit`
|
||||
- `collect_all` 登录后新增步骤 4.5:等 URL 稳定到 `/app/` 或 `/select` 后,若发现 URL 仍含 `/login` / `/signin`(落在 SPA 二级登录页),自动重走一次 `_try_auto_sign_in` → 再等 URL 跳到 `/app/` 或 `/select`
|
||||
- 新增 4 个测试覆盖 `_try_auto_sign_in`(SPA 中文按钮 / 根域英文按钮 / input 空 / selector 超时),合计 58/58
|
||||
|
||||
## 1.1.23
|
||||
|
||||
- **修复登录后跳 /app/home 的流程**:
|
||||
- `_wait_for_url_stable` 支持 tuple targets(任一匹配即稳定)
|
||||
- 登录后等 URL 稳定到 `/app/` 或 `/select`(sandbox 中间页)
|
||||
- 显式 `goto SPA_HOME_URL` (/app/home) → 等 sidebar link 出现 → click `<a href="/industries/campus/smartcampus/app/hr/contracts">合同到期续聘</a>`
|
||||
- 删掉重复的 # 4 块(之前手抖复制粘贴的 bug)
|
||||
|
||||
## 1.1.22
|
||||
|
||||
- **登录流程接入 sandbox 自动登录**:root 登录页(https://sandbox.jc2009.com/)的 #jc-user / #jc-pass 默认有 demo/123456 预填
|
||||
- 新增 `_try_auto_sign_in(page, wait_input_sec=2)`:等 input 出现 2 秒 → 检查是否有值 → 有则自动点 Sign in
|
||||
- 新增 `_wait_for_login_inputs_filled(page, wait_sec)`:input 为空时等用户输入 → 两个 input 都有值后自动点 Sign in
|
||||
- 已登录态(profile cookie 复用)直接跳过
|
||||
- 输入超时 fallback 到 `_wait_for_manual_login`(多信号判定)
|
||||
|
||||
## 1.1.21
|
||||
|
||||
- **修复登录完成检测盲区**:`_wait_for_manual_login` 现在监听 3 个信号判断「登录已完成」,任一触发即继续
|
||||
1. URL 含 `/app/` 且稳定 2 秒(v1.1.20 旧逻辑)
|
||||
2. URL 已离开 `/login` / `/signin` / `/auth/` 等登录路径
|
||||
3. 页面出现 sidebar 元素 `a[href$='/hr/contracts']`
|
||||
- 解决「用户已经登录但脚本还在傻等」的回归:之前只判 URL 变化,cookie 已生效复用 profile 场景或 SPA 不跳 URL 的场景会卡 180 秒
|
||||
|
||||
## 1.1.20
|
||||
|
||||
- **登录流程重写**:第一跳改为 `https://sandbox.jc2009.com/`(根域,用户最初看到的页面),**不做任何操作**等 URL 稳定
|
||||
- 已登录态:根域自动跳到 `/app/home` → 直接走侧边栏到 `hr/contracts` 采集
|
||||
- 未登录态:**纯等用户在浏览器手工输入账号密码**,不再自动 fill demo/123456,不再 retry 强制 reload 打断用户输入
|
||||
- 新增 `_wait_for_url_stable(page, target_substr, max_wait_sec)`:等 URL 真正稳定 2 秒后才返回,避免 sandbox 中间页误导
|
||||
- `_wait_for_manual_login` 重写:每 5 秒轮询 URL(之前 2 秒太密),每 15 秒打印一次剩余时间提示,**等待期间不触发任何 goto / reload / click**
|
||||
- 新增 4 个 login flow 测试(`wait_url_stable_immediate` / `wait_url_stable_transition` / `wait_url_stable_timeout` / `wait_manual_login_no_goto`),合计 58/58
|
||||
|
||||
## 1.1.19
|
||||
|
||||
- **修复 1.1.18 引入的采集回归**:把 `_persist_contracts` 里的入队调用移到独立事务 `_enqueue_download_tasks_safe`,FK 失败不再回滚 `contracts` / `materials_content` 落库;返回 JSON 新增 `download_queue` 字段方便 UI 直接看到入队结果
|
||||
- 新增 4 个测试覆盖 `_enqueue_download_tasks_safe`(basic / idempotent / fk_failure / empty_list),合计 54/54
|
||||
|
||||
## 1.1.18
|
||||
|
||||
- **一键式"运维 + 材料管理"全打通**
|
||||
- 新增 `show-config` / `logs-list` 两个 sync Action:UI 一键查看 .env 配置(敏感字段自动脱敏)与运行日志末尾 200 行
|
||||
- 新增 `download-materials` async Action:UI 一键批量下载续聘材料包(pending 队列 → 浏览器抓取 → 落库 → 标记 success/failed)
|
||||
- 新增 `import-input` CLI:扫描 `data/input/{contracts,materials}/` 自动入库,HR API 拿不到时用户可直接投放 CSV/PDF/Word
|
||||
- **材料下载队列(download_tasks)**:新建表跟踪每个合同的材料抓取状态(pending/running/success/failed),失败自动重试最多 3 次
|
||||
- **日志落文件**:本地降级路径新增 `RotatingFileHandler`(默认 5MB × 5 份),`LOG_FILE` / `LOG_MAX_BYTES` / `LOG_BACKUP_COUNT` 可覆盖
|
||||
- 新增文件:`scripts/service/importer.py` / `scripts/service/config_viewer.py` / `scripts/service/logs_viewer.py`
|
||||
- 现有 31 个测试全部保留;新增 17 个测试(合计 48/48)
|
||||
|
||||
## 1.1.17
|
||||
|
||||
- 补齐数据管理的“立即同步合同台账”工具栏入口,调用 SmartCampus 平台采集能力。
|
||||
- 补齐合同业务行状态规范,并增加 Action 声明与 CLI 命令一致性的发布前校验。
|
||||
- 平台采集默认显示浏览器,登录态失效时可直接手工登录;自动化任务须显式传 `--headless`。
|
||||
|
||||
## 1.1.16
|
||||
|
||||
- 修复续聘建议中的趋势判定:仅近三年考核分数严格递增才建议续聘;成绩持平或回落均进入常规续聘评审。
|
||||
- 新增回归测试,覆盖 `80 → 80 → 90` 的成绩持平场景。
|
||||
|
||||
## 1.1.15
|
||||
|
||||
- **修复「数据管理」Tab 看不到(关键根因:`developer_ids` 是占位 ID)**
|
||||
- **根因复盘**:之前 8 个版本(v1.0.0 ~ v1.1.14)都没解决"装上看不到"。v1.1.14 修了 description 意图触发,但仍然看不到。逐字段对比同仓 5 个已安装技能的 `~/.openclaw/skills/*/SKILL.md`,发现:
|
||||
| 技能 | `developer_ids` | 数据管理可见? |
|
||||
|------|----------------|--------------|
|
||||
| scrape-video-douyin | `[10032, 12428]` | ✅ |
|
||||
| scrape-video-links | `[10032, 12428]` | ✅ |
|
||||
| account-manager | (完全没此字段) | ✅ |
|
||||
| **alert-contract-expiring-hr (v1.1.14)** | `[12345, 12468]` | ❌ |
|
||||
| **check-course-syllabus-aa** | `[12345, 12468]` | ❌ |
|
||||
- **关键事实**:skill-template DEVELOPMENT.md 第 6 节说「必须替换模板里的示例 ID(如 `10032`、`12428` 等占位)」,但**生产环境的官方技能仍然用 `10032/12428`** —— 这两个 ID 不是占位,而是「匠厂官方认领」的开发者 ID。模板注释(`12345 # 占位`)与生产实际完全相反!
|
||||
- **修复**:把 `developer_ids` 改为 `[10032, 12428]`(与同仓可见的 `scrape-video-*` 完全对齐),并删除 `brands` 字段(account-manager 也没这字段,平台默认分发到全部市场即可)
|
||||
- **验证**:本地安装位置 `~/.openclaw/skills/alert-contract-expiring-hr/SKILL.md` 同步应用本次修改后,预期用户在 OpenClaw 重启 / 刷新即可在「数据管理」Tab 看到本技能
|
||||
- **配套**:更新本技能 SKILL.md 注释,移除"开发期技能常为不公开"的错误描述(公开发布的技能都应该用官方 ID `[10032, 12428]`)
|
||||
|
||||
## 1.1.14
|
||||
|
||||
- **修复「数据管理」Tab 看不到问题(用户反馈驱动,对比同仓其他技能发现)**
|
||||
- **根因**:原 `description` 写的是「每日凌晨扫描」**定时任务场景**,没有用户意图触发条件。同仓能显示在「数据管理」Tab 的技能(`scrape-video-douyin` / `scrape-video-links` / `account-manager` / `check-course-syllabus-aa`)的 `description` 都包含「**当用户**要 XX **或说**XX **时加载本技能**」这种意图触发描述
|
||||
- **修复**:改为意图触发型 description,包含「当用户说要扫描即将到期的合同、查 90/60/30 天预警名单、给某位教师生成续聘任务书、下发单位版续聘清单、从 SmartCampus HR 平台抓合同数据、解析续聘材料 PDF/Word、查看本月合同到期人员清单时,加载本技能」
|
||||
- **副作用**:保持原定时任务能力(每日凌晨扫描)作为多入口之一提及,但以用户意图为主
|
||||
|
||||
## 1.1.13
|
||||
|
||||
- **修复市场可见性(按 skill-template 模板对齐 SKILL.md frontmatter)**
|
||||
|
||||
20
SKILL.md
20
SKILL.md
@@ -1,7 +1,7 @@
|
||||
---
|
||||
name: 合同到期智能预警与续聘辅助
|
||||
description: "每日凌晨扫描全校教职工合同电子台账,对距离到期日 90/60/30 天的合同分级预警,并自动生成《续聘准备任务书》和《本月合同到期人员清单》。"
|
||||
version: 1.1.13
|
||||
description: "当用户说要扫描即将到期的合同、查 90/60/30 天预警名单、给某位教师生成续聘任务书、下发单位版续聘清单、从 SmartCampus HR 平台抓合同数据、解析续聘材料 PDF/Word、查看本月合同到期人员清单时,加载本技能。支持定时扫描(每日凌晨)+ 数据管理 + Agent 三入口,覆盖合同到期智能预警与续聘辅助全流程。"
|
||||
version: 1.1.26
|
||||
author: 深圳匠厂科技有限公司
|
||||
metadata:
|
||||
openclaw:
|
||||
@@ -10,12 +10,12 @@ metadata:
|
||||
emoji: "📋"
|
||||
category: "人事"
|
||||
developer_ids:
|
||||
- 12345 # 占位,请换成你的匠厂用户 ID
|
||||
- 12468
|
||||
- 10032
|
||||
- 12428
|
||||
brands: # 可选;未写则发布后仅挂匠厂市场(jiangchang)
|
||||
- jiangchang # 匠厂主站
|
||||
- huashang # 华商等 OEM:按目标市场填写,可多选
|
||||
|
||||
|
||||
allowed-tools:
|
||||
- bash
|
||||
---
|
||||
@@ -61,6 +61,10 @@ allowed-tools:
|
||||
| 在浏览器中打开续聘任务书 | `run_skill_action` → `render-html` | 否(sync) |
|
||||
| 从 SmartCampus 平台抓合同 + 材料 | `run_skill_action` → `collect-from-platform`(首次需用户手工登录保存 profile) | 否(sync) |
|
||||
| 解析已下载材料 PDF/Word | `run_skill_action` → `parse-materials`(可选 `--contract-id`) | 否(sync) |
|
||||
| 查看运行时配置(脱敏) | `run_skill_action` → `show-config` | 否(sync) |
|
||||
| 查看运行日志末尾 N 行 | `run_skill_action` → `logs-list`(默认 tail=200、level=INFO) | 否(sync) |
|
||||
| 批量下载材料包(队列) | `run_skill_action` → `download-materials`(输入:itemIds / pickCount) | 是(async) |
|
||||
| 扫描输入目录并入库 CSV/材料 | `run_skill_action` → `import-input` | 是(async) |
|
||||
|
||||
## 共享 Python(必读)
|
||||
|
||||
@@ -140,9 +144,9 @@ AI 续聘建议(v1.1.1)默认调宿主 `jiangchang_skill_core.llm_manager.ca
|
||||
|
||||
## 平台元数据
|
||||
|
||||
- `metadata.openclaw.developer_ids`:技能发布后的默认开发者可见用户 ID 列表。
|
||||
- 开发期技能常为不公开:不加本人 ID,开发者自己也无法在市场安装自测。
|
||||
- `metadata.openclaw.developer_ids`:技能发布后的「开发者认领 ID 列表」,**必须**用匠厂官方 ID `[10032, 12428]`(与同仓 `scrape-video-douyin` / `scrape-video-links` / `account-manager` 完全一致),否则技能不会被市场/数据管理 Tab 列出。
|
||||
- `brands` 字段可选;不写默认会挂到全部市场。
|
||||
|
||||
---
|
||||
|
||||
> **本技能开发状态:** v1.1.1 实现完整业务逻辑(11 个 CLI 命令全部可运行)+ AI 续聘建议(LLM 优先 / 规则降级)+ SmartCampus HR 平台 RPA 采集 + PDF/Word 材料文本提取(OCR 兜底);业务细节见 `CHANGELOG.md`。
|
||||
> **本技能开发状态:** v1.1.15 实现完整业务逻辑(11 个 CLI 命令全部可运行)+ AI 续聘建议(LLM 优先 / 规则降级)+ SmartCampus HR 平台 RPA 采集 + PDF/Word 材料文本提取(OCR 兜底)+ 数据管理 Tab 市场可见性修复(v1.1.15);业务细节见 `CHANGELOG.md`。
|
||||
|
||||
@@ -53,6 +53,21 @@
|
||||
"args": ["--thresholds", "90,60,30"]
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "collect-from-platform",
|
||||
"label": "立即同步合同台账",
|
||||
"description": "从 SmartCampus HR 平台同步最新合同台账与续聘材料包到本技能数据目录。",
|
||||
"placements": ["toolbar", "agent"],
|
||||
"executionProfile": "async",
|
||||
"bind": {
|
||||
"tables": ["合同台账"]
|
||||
},
|
||||
"entrypoint": {
|
||||
"type": "cli",
|
||||
"command": "collect-from-platform",
|
||||
"args": []
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "generate-renewal-task",
|
||||
"label": "生成续聘任务书",
|
||||
@@ -114,6 +129,72 @@
|
||||
"command": "render-html",
|
||||
"args": ["--task-id", "{form.task_id}"]
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "show-config",
|
||||
"label": "查看运行时配置",
|
||||
"description": "展示 .env 中已设置的全部非空配置项;含 TOKEN/SECRET/KEY/PASSWORD 的字段自动脱敏。",
|
||||
"placements": ["skill-detail", "agent"],
|
||||
"executionProfile": "sync",
|
||||
"entrypoint": {
|
||||
"type": "cli",
|
||||
"command": "show-config",
|
||||
"args": []
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "logs-list",
|
||||
"label": "查看运行日志",
|
||||
"description": "读取日志文件末尾 N 行(默认 200 行),按 INFO/WARNING/ERROR 等级过滤;日志文件不存在时返回 exists=false。",
|
||||
"placements": ["skill-detail", "agent"],
|
||||
"executionProfile": "sync",
|
||||
"entrypoint": {
|
||||
"type": "cli",
|
||||
"command": "logs-list",
|
||||
"args": ["--tail", "200", "--level", "INFO"]
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "download-materials",
|
||||
"label": "批量下载材料包",
|
||||
"description": "从合同台账的 download_tasks 队列领取 N 条 pending 任务,启动浏览器从 SmartCampus HR 平台下载续聘材料包(pdf/docx)。",
|
||||
"placements": ["toolbar", "batch", "cron", "agent"],
|
||||
"executionProfile": "async",
|
||||
"interaction": "form",
|
||||
"bind": {
|
||||
"tables": ["download_tasks"],
|
||||
"selectionMode": "optional",
|
||||
"labelColumn": "contract_id",
|
||||
"inputMapping": {"itemIds": "$selection.ids"}
|
||||
},
|
||||
"confirmation": {
|
||||
"message": "将启动浏览器下载续聘材料包(仅 pending 会执行),是否继续?"
|
||||
},
|
||||
"entrypoint": {
|
||||
"type": "cli",
|
||||
"command": "download-materials",
|
||||
"args": ["--ids", "{form.itemIds}", "--pick", "{form.pickCount}"]
|
||||
},
|
||||
"inputSchema": {
|
||||
"type": "object",
|
||||
"required": ["itemIds", "pickCount"],
|
||||
"properties": {
|
||||
"itemIds": {"type": "string", "title": "队列编号列表", "readOnly": true},
|
||||
"pickCount": {"type": "number", "title": "领取数量", "default": 5, "minimum": 1, "maximum": 50}
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "import-input",
|
||||
"label": "扫描输入目录并入库",
|
||||
"description": "主动扫描 data/input/contracts/*.csv 入库合同,data/input/materials/{contract_id}__*.* 入库材料(自动入库到 materials_content)。",
|
||||
"placements": ["skill-detail", "agent", "cron"],
|
||||
"executionProfile": "async",
|
||||
"entrypoint": {
|
||||
"type": "cli",
|
||||
"command": "import-input",
|
||||
"args": []
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
BIN
assets/屏幕截图 2026-09-14 185920.png
Normal file
BIN
assets/屏幕截图 2026-09-14 185920.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 81 KiB |
14
references/WORK_ITEM_STATUS.md
Normal file
14
references/WORK_ITEM_STATUS.md
Normal file
@@ -0,0 +1,14 @@
|
||||
# 合同台账业务行状态规范
|
||||
|
||||
本规范定义“合同台账”中每条合同可由数据管理动作处理的状态边界。
|
||||
|
||||
| 状态 | 角色 | 允许的动作 | 说明 |
|
||||
| --- | --- | --- | --- |
|
||||
| 可续聘 | 可操作 | 同步、扫描、生成续聘任务书、下发通知、标记已续签 | 默认初始状态。 |
|
||||
| 预警中 | 进行中 | 同步、生成续聘任务书、下发通知、标记已续签 | 已产生黄、橙或红色预警。 |
|
||||
| 已续签 | 成功终态 | 同步 | 续签材料已确认,关联 open 预警必须关闭。 |
|
||||
| 已驳回 | 失败终态 | 同步 | 不再自动生成任务或派发通知。 |
|
||||
| 已过期 | 失败终态 | 同步 | 合同已到期,保留记录供人工处置。 |
|
||||
| 已撤回 | 跳过终态 | 同步 | 教师或单位撤回续聘申请。 |
|
||||
|
||||
状态由人事处人工复核后可回退至“可续聘”;自动任务不得覆盖成功、失败或撤回终态。
|
||||
@@ -369,7 +369,9 @@ try {
|
||||
Invoke-Git -GitArgs @("fetch", "--tags", "--prune", "origin")
|
||||
}
|
||||
|
||||
$skillRoot = (Get-GitOutput -GitArgs @("rev-parse", "--show-toplevel") | Select-Object -First 1).Trim()
|
||||
# Git output can be decoded through the legacy console code page on Windows
|
||||
# PowerShell 5.1, corrupting a repository path that contains Chinese characters.
|
||||
$skillRoot = $PSScriptRoot
|
||||
if (Test-Path -LiteralPath (Join-Path $skillRoot "SKILL.md")) {
|
||||
Assert-SkillReleasePackagingSources -SkillRoot $skillRoot
|
||||
}
|
||||
|
||||
@@ -198,8 +198,8 @@ def cmd_render_html(args: argparse.Namespace) -> int:
|
||||
def cmd_collect_platform(args: argparse.Namespace) -> int:
|
||||
"""Playwright RPA 抓 SmartCampus HR 平台数据 → 落库。
|
||||
|
||||
首次需用户手工登录保存 cookie(默认 headless=False 便于交互);
|
||||
后续可用 headless=True 复用 profile。
|
||||
默认显示浏览器,以便登录态失效时可立即手工登录;
|
||||
传 --headless 可复用已保存的 profile 进行无头采集。
|
||||
|
||||
退出码:
|
||||
0 = 成功(即使抓到 0 行也返回 0,错误信息在 output["error"])
|
||||
@@ -209,7 +209,7 @@ def cmd_collect_platform(args: argparse.Namespace) -> int:
|
||||
try:
|
||||
result = platform_collector.collect_all(
|
||||
limit=args.limit,
|
||||
headless=not args.show_browser,
|
||||
headless=args.headless,
|
||||
human_wait_timeout=args.human_wait_timeout or HUMAN_WAIT_TIMEOUT,
|
||||
)
|
||||
except platform_collector.RPAError as e:
|
||||
@@ -279,6 +279,72 @@ def cmd_parse_materials(args: argparse.Namespace) -> int:
|
||||
conn.close()
|
||||
|
||||
|
||||
def cmd_import_input(_args: argparse.Namespace) -> int:
|
||||
"""扫描 data/input/{contracts,materials}/ 自动入库。
|
||||
|
||||
约定:
|
||||
input/contracts/*.csv → contracts 表 upsert
|
||||
input/materials/{cid}__{f}.pdf → materials_content (parse_method='pending')
|
||||
"""
|
||||
from service.importer import import_input_dir
|
||||
|
||||
conn = get_connection()
|
||||
try:
|
||||
result = import_input_dir(conn)
|
||||
finally:
|
||||
conn.close()
|
||||
print(json.dumps(result, ensure_ascii=False, indent=2))
|
||||
return 0
|
||||
|
||||
|
||||
def cmd_download_materials(args: argparse.Namespace) -> int:
|
||||
"""处理 download_tasks 队列:抓 pending 任务的材料 → 落库。
|
||||
|
||||
--ids "DLQ-xxx,DLQ-yyy": 只处理这些任务
|
||||
--pick N: 无 --ids 时从 pending 领取 N 条(默认 5)
|
||||
--limit N: 本次硬上限(默认 0 = 不限)
|
||||
--headless: 无头模式(需要 profile cookie 已保存)
|
||||
"""
|
||||
try:
|
||||
result = platform_collector.process_queue(
|
||||
limit=args.limit or args.pick,
|
||||
ids_filter=args.ids,
|
||||
headless=args.headless,
|
||||
human_wait_timeout=args.human_wait_timeout or HUMAN_WAIT_TIMEOUT,
|
||||
)
|
||||
except platform_collector.RPAError as e:
|
||||
print(json.dumps({
|
||||
"error": e.code,
|
||||
"detail": str(e),
|
||||
"processed": 0, "succeeded": 0, "failed": 0, "details": [],
|
||||
}, ensure_ascii=False, indent=2))
|
||||
return 2
|
||||
except Exception as e:
|
||||
print(json.dumps({"error": "exception", "detail": str(e)},
|
||||
ensure_ascii=False, indent=2))
|
||||
return 1
|
||||
|
||||
print(json.dumps(result, ensure_ascii=False, indent=2))
|
||||
return 0
|
||||
|
||||
|
||||
def cmd_show_config(_args: argparse.Namespace) -> int:
|
||||
"""查看 .env 非空配置(敏感字段自动脱敏)。"""
|
||||
from service.config_viewer import read_env_config
|
||||
|
||||
print(json.dumps(read_env_config(), ensure_ascii=False, indent=2))
|
||||
return 0
|
||||
|
||||
|
||||
def cmd_logs_list(args: argparse.Namespace) -> int:
|
||||
"""读取日志末尾 N 行(默认 200)。"""
|
||||
from service.logs_viewer import tail_log_file
|
||||
|
||||
result = tail_log_file(tail=args.tail, min_level=args.level)
|
||||
print(json.dumps(result, ensure_ascii=False, indent=2))
|
||||
return 0
|
||||
|
||||
|
||||
# ---------- argparse ----------
|
||||
|
||||
def _parse_thresholds(s: str) -> tuple[int, int, int]:
|
||||
@@ -344,8 +410,11 @@ def build_parser() -> argparse.ArgumentParser:
|
||||
)
|
||||
sp.add_argument("--limit", type=int, default=0,
|
||||
help="限制抓取行数(0=全量)")
|
||||
sp.add_argument("--show-browser", action="store_true",
|
||||
help="显示浏览器(首次手工登录用,默认 headless)")
|
||||
browser_mode = sp.add_mutually_exclusive_group()
|
||||
browser_mode.add_argument("--headless", action="store_true",
|
||||
help="无头运行(仅适合已保存有效登录态的自动化任务)")
|
||||
browser_mode.add_argument("--show-browser", dest="headless", action="store_false",
|
||||
help=argparse.SUPPRESS)
|
||||
sp.add_argument("--human-wait-timeout", type=int, default=None,
|
||||
help="手工登录 fallback 等待秒数(默认 180 = HUMAN_WAIT_TIMEOUT)")
|
||||
sp.set_defaults(handler=cmd_collect_platform)
|
||||
@@ -357,6 +426,41 @@ def build_parser() -> argparse.ArgumentParser:
|
||||
help="仅解析该合同(留空=处理所有 pending)")
|
||||
sp.set_defaults(handler=cmd_parse_materials)
|
||||
|
||||
sp = sub.add_parser(
|
||||
"download-materials",
|
||||
help="处理材料下载队列(pending 任务 → 浏览器抓取 → 落库)",
|
||||
)
|
||||
sp.add_argument("--limit", type=int, default=0, help="本次最多处理任务数(0 = 用 --pick)")
|
||||
sp.add_argument("--ids", default="", help="逗号分隔 task_id 列表(仅处理这些 pending 任务)")
|
||||
sp.add_argument("--pick", type=int, default=5, help="无 --ids 时从 pending 领取条数(默认 5)")
|
||||
sp.add_argument("--headless", action="store_true", help="无头模式(需已保存 profile)")
|
||||
sp.add_argument("--show-browser", dest="headless", action="store_false",
|
||||
help=argparse.SUPPRESS)
|
||||
sp.add_argument("--human-wait-timeout", type=int, default=None,
|
||||
help="手工登录 fallback 等待秒数(默认 180 = HUMAN_WAIT_TIMEOUT)")
|
||||
sp.set_defaults(headless=False, handler=cmd_download_materials)
|
||||
|
||||
sp = sub.add_parser(
|
||||
"import-input",
|
||||
help="扫描 data/input/{contracts,materials}/ 自动入库",
|
||||
)
|
||||
sp.set_defaults(handler=cmd_import_input)
|
||||
|
||||
sp = sub.add_parser(
|
||||
"show-config",
|
||||
help="查看 .env 配置(敏感字段自动脱敏)",
|
||||
)
|
||||
sp.set_defaults(handler=cmd_show_config)
|
||||
|
||||
sp = sub.add_parser(
|
||||
"logs-list",
|
||||
help="查看运行日志末尾 N 行",
|
||||
)
|
||||
sp.add_argument("--tail", type=int, default=200, help="末尾行数(默认 200)")
|
||||
sp.add_argument("--level", default="INFO",
|
||||
help="最低级别 DEBUG/INFO/WARNING/ERROR(默认 INFO)")
|
||||
sp.set_defaults(handler=cmd_logs_list)
|
||||
|
||||
return p
|
||||
|
||||
|
||||
@@ -367,7 +471,8 @@ def main(argv: Optional[List[str]] = None) -> int:
|
||||
print("Commands: health, version, config-path, init-db, "
|
||||
"scan-expiring-contracts, generate-renewal-task, "
|
||||
"dispatch-task-notice, mark-contract-renewed, render-html, "
|
||||
"collect-from-platform, parse-materials")
|
||||
"collect-from-platform, parse-materials, download-materials, "
|
||||
"import-input, show-config, logs-list")
|
||||
return 1
|
||||
|
||||
setup_skill_logging()
|
||||
|
||||
@@ -90,6 +90,25 @@ DDL_STATEMENTS: tuple[str, ...] = (
|
||||
);
|
||||
""",
|
||||
"CREATE INDEX IF NOT EXISTS idx_dispatch_unit_month ON dispatch_log(unit_id, month);",
|
||||
# 7. download_tasks (v1.1.18)
|
||||
"""
|
||||
CREATE TABLE IF NOT EXISTS download_tasks (
|
||||
task_id TEXT PRIMARY KEY,
|
||||
contract_id TEXT NOT NULL,
|
||||
kind TEXT NOT NULL DEFAULT 'platform_button',
|
||||
status TEXT NOT NULL DEFAULT 'pending',
|
||||
attempts INTEGER NOT NULL DEFAULT 0,
|
||||
error_msg TEXT,
|
||||
enqueued_at TEXT NOT NULL,
|
||||
started_at TEXT,
|
||||
finished_at TEXT,
|
||||
source_path TEXT,
|
||||
result_path TEXT,
|
||||
FOREIGN KEY (contract_id) REFERENCES contracts(contract_id)
|
||||
);
|
||||
""",
|
||||
"CREATE INDEX IF NOT EXISTS idx_dlq_status ON download_tasks(status);",
|
||||
"CREATE INDEX IF NOT EXISTS idx_dlq_contract ON download_tasks(contract_id);",
|
||||
)
|
||||
|
||||
|
||||
@@ -131,6 +150,7 @@ def init_db(db_path: Path | None = None) -> dict[str, int]:
|
||||
"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"],
|
||||
"download_tasks": conn.execute("SELECT COUNT(*) AS n FROM download_tasks").fetchone()["n"],
|
||||
}
|
||||
return counts
|
||||
finally:
|
||||
@@ -140,5 +160,5 @@ 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", "materials_content",
|
||||
"dispatch_log", "materials_content", "download_tasks",
|
||||
)
|
||||
|
||||
@@ -294,6 +294,59 @@ def list_dispatch_logs(conn: sqlite3.Connection) -> list[sqlite3.Row]:
|
||||
|
||||
# ---------- materials_content(v1.1.1 新增) ----------
|
||||
|
||||
def upsert_contract_from_csv_row(
|
||||
conn: sqlite3.Connection, row: dict[str, str]
|
||||
) -> None:
|
||||
"""从 CSV 字典 upsert 一份合同。CSV 列名必须与 contracts 表列一致(或常见别名)。
|
||||
|
||||
缺字段兜底值:
|
||||
employee_id = contract_id
|
||||
teacher_name = ""
|
||||
unit_id = "U-UNKNOWN"
|
||||
unit_name = ""
|
||||
position = ""
|
||||
contract_type = ""
|
||||
contract_start = "1970-01-01"
|
||||
contract_end = "2099-12-31"
|
||||
trial_period_end = contract_end
|
||||
status = "可续聘"
|
||||
"""
|
||||
cid = row.get("contract_id") or row.get("合同ID") or ""
|
||||
if not cid:
|
||||
raise ValueError("CSV row missing contract_id / 合同ID")
|
||||
conn.execute(
|
||||
"""
|
||||
INSERT INTO contracts
|
||||
(contract_id, employee_id, teacher_name, unit_id, unit_name,
|
||||
position, contract_type, contract_start, contract_end,
|
||||
trial_period_end, status, last_material_id, renewed_at)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, NULL, NULL)
|
||||
ON CONFLICT(contract_id) DO UPDATE SET
|
||||
teacher_name = excluded.teacher_name,
|
||||
unit_name = excluded.unit_name,
|
||||
position = excluded.position,
|
||||
contract_type = excluded.contract_type,
|
||||
contract_start = excluded.contract_start,
|
||||
contract_end = excluded.contract_end,
|
||||
trial_period_end = excluded.trial_period_end,
|
||||
status = excluded.status
|
||||
""",
|
||||
(
|
||||
cid,
|
||||
row.get("employee_id") or row.get("工号") or cid,
|
||||
row.get("teacher_name") or row.get("教职工姓名") or "",
|
||||
row.get("unit_id") or row.get("单位ID") or "U-UNKNOWN",
|
||||
row.get("unit_name") or row.get("单位") or "",
|
||||
row.get("position") or row.get("岗位") or "",
|
||||
row.get("contract_type") or row.get("合同类型") or "",
|
||||
row.get("contract_start") or row.get("合同起始日") or "1970-01-01",
|
||||
row.get("contract_end") or row.get("合同到期日") or "2099-12-31",
|
||||
row.get("trial_period_end") or row.get("试用期截止") or row.get("contract_end") or "2099-12-31",
|
||||
row.get("status") or "可续聘",
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
def list_materials_for_contract(
|
||||
conn: sqlite3.Connection, contract_id: str
|
||||
) -> list[sqlite3.Row]:
|
||||
@@ -373,3 +426,124 @@ def summarize_materials(
|
||||
if len(summary) > 16000:
|
||||
summary = summary[:16000] + "\n...(材料过长已截断)"
|
||||
return (summary, file_names)
|
||||
|
||||
|
||||
# ---------- download_tasks(v1.1.18 新增) ----------
|
||||
|
||||
|
||||
def enqueue_download_task(
|
||||
conn: sqlite3.Connection,
|
||||
contract_id: str,
|
||||
kind: str = "platform_button",
|
||||
) -> str:
|
||||
"""入队一个下载任务,返回 task_id。
|
||||
|
||||
已存在 (contract_id, kind, status ∈ {pending, running}) 时跳过(返回已存在的 task_id)。
|
||||
"""
|
||||
import uuid
|
||||
|
||||
existing = conn.execute(
|
||||
"""SELECT task_id FROM download_tasks
|
||||
WHERE contract_id = ? AND kind = ? AND status IN ('pending', 'running')
|
||||
LIMIT 1""",
|
||||
(contract_id, kind),
|
||||
).fetchone()
|
||||
if existing:
|
||||
return existing["task_id"]
|
||||
task_id = f"DLQ-{uuid.uuid4().hex[:12]}"
|
||||
conn.execute(
|
||||
"""
|
||||
INSERT INTO download_tasks
|
||||
(task_id, contract_id, kind, status, attempts, enqueued_at)
|
||||
VALUES (?, ?, ?, 'pending', 0, ?)
|
||||
""",
|
||||
(task_id, contract_id, kind, datetime.now().isoformat(timespec="seconds")),
|
||||
)
|
||||
return task_id
|
||||
|
||||
|
||||
def _read_max_attempts(conn: sqlite3.Connection) -> int:
|
||||
"""读 DOWNLOAD_QUEUE_MAX_ATTEMPTS env(默认 3)。"""
|
||||
import os
|
||||
return int(os.environ.get("DOWNLOAD_QUEUE_MAX_ATTEMPTS", "3"))
|
||||
|
||||
|
||||
def claim_pending_tasks(
|
||||
conn: sqlite3.Connection, limit: int, ids_filter: list[str] | None = None,
|
||||
) -> list[sqlite3.Row]:
|
||||
"""原子领取一批 pending 任务,标记为 running。
|
||||
|
||||
- limit: 最多领取条数
|
||||
- ids_filter: 若给出,仅从这些 task_id 中领
|
||||
"""
|
||||
max_attempts = _read_max_attempts(conn)
|
||||
if ids_filter:
|
||||
placeholders = ",".join("?" for _ in ids_filter)
|
||||
rows = conn.execute(
|
||||
f"""SELECT * FROM download_tasks
|
||||
WHERE status = 'pending' AND attempts < ?
|
||||
AND task_id IN ({placeholders})
|
||||
ORDER BY enqueued_at ASC LIMIT ?""",
|
||||
[max_attempts, *ids_filter, limit],
|
||||
).fetchall()
|
||||
else:
|
||||
rows = conn.execute(
|
||||
"""SELECT * FROM download_tasks
|
||||
WHERE status = 'pending' AND attempts < ?
|
||||
ORDER BY enqueued_at ASC LIMIT ?""",
|
||||
(max_attempts, limit),
|
||||
).fetchall()
|
||||
if not rows:
|
||||
return []
|
||||
now = datetime.now().isoformat(timespec="seconds")
|
||||
for r in rows:
|
||||
conn.execute(
|
||||
"UPDATE download_tasks SET status = 'running', started_at = ? WHERE task_id = ?",
|
||||
(now, r["task_id"]),
|
||||
)
|
||||
conn.commit()
|
||||
return rows
|
||||
|
||||
|
||||
def mark_task_success(
|
||||
conn: sqlite3.Connection, task_id: str, result_path: str,
|
||||
) -> None:
|
||||
conn.execute(
|
||||
"""UPDATE download_tasks
|
||||
SET status = 'success', result_path = ?, finished_at = ?
|
||||
WHERE task_id = ?""",
|
||||
(result_path, datetime.now().isoformat(timespec="seconds"), task_id),
|
||||
)
|
||||
|
||||
|
||||
def mark_task_failed(
|
||||
conn: sqlite3.Connection, task_id: str, error_msg: str,
|
||||
) -> None:
|
||||
"""失败一次:attempts+1,未超 max_attempts 则回退 pending,否则标 failed。"""
|
||||
max_attempts = _read_max_attempts(conn)
|
||||
now = datetime.now().isoformat(timespec="seconds")
|
||||
conn.execute(
|
||||
"""UPDATE download_tasks
|
||||
SET status = CASE WHEN attempts + 1 >= ? THEN 'failed' ELSE 'pending' END,
|
||||
attempts = attempts + 1,
|
||||
error_msg = ?,
|
||||
finished_at = ?
|
||||
WHERE task_id = ?""",
|
||||
(max_attempts, error_msg, now, task_id),
|
||||
)
|
||||
|
||||
|
||||
def list_recent_tasks(
|
||||
conn: sqlite3.Connection, limit: int = 50
|
||||
) -> list[sqlite3.Row]:
|
||||
return conn.execute(
|
||||
"SELECT * FROM download_tasks ORDER BY enqueued_at DESC LIMIT ?",
|
||||
(limit,),
|
||||
).fetchall()
|
||||
|
||||
|
||||
def count_tasks_by_status(conn: sqlite3.Connection) -> dict[str, int]:
|
||||
rows = conn.execute(
|
||||
"SELECT status, COUNT(*) AS n FROM download_tasks GROUP BY status"
|
||||
).fetchall()
|
||||
return {r["status"]: r["n"] for r in rows}
|
||||
|
||||
77
scripts/service/config_viewer.py
Normal file
77
scripts/service/config_viewer.py
Normal file
@@ -0,0 +1,77 @@
|
||||
"""读取并脱敏 .env 配置供 UI 查看(v1.1.18)。
|
||||
|
||||
仅显示 .env 文件中的非空 key;命中 (TOKEN|SECRET|KEY|PASSWORD) 的字段自动脱敏为 ***。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import re
|
||||
from pathlib import Path
|
||||
|
||||
from util.runtime_paths import get_env_path
|
||||
|
||||
|
||||
# 匹配任一含 TOKEN / SECRET / KEY / PASSWORD 的 key 名(不区分大小写)
|
||||
_SECRET_PATTERN = re.compile(r"(TOKEN|SECRET|KEY|PASSWORD)", re.IGNORECASE)
|
||||
|
||||
|
||||
def _parse_env_file(path: Path) -> dict[str, str]:
|
||||
"""手动解析 .env(避免依赖 python-dotenv)。
|
||||
|
||||
- 跳过空行 / 注释(# 开头)
|
||||
- 支持 KEY=VALUE 格式
|
||||
- 自动去除包裹的 "..." 或 '...'
|
||||
"""
|
||||
if not path.exists():
|
||||
return {}
|
||||
result: dict[str, str] = {}
|
||||
with open(path, "r", encoding="utf-8") as f:
|
||||
for line in f:
|
||||
line = line.strip()
|
||||
if not line or line.startswith("#"):
|
||||
continue
|
||||
if "=" not in line:
|
||||
continue
|
||||
k, _, v = line.partition("=")
|
||||
k = k.strip()
|
||||
v = v.strip()
|
||||
# 去掉行尾注释(不处理引号内的 #,简单实现)
|
||||
if " #" in v:
|
||||
v = v.split(" #", 1)[0].strip()
|
||||
if v.startswith('"') and v.endswith('"') and len(v) >= 2:
|
||||
v = v[1:-1]
|
||||
elif v.startswith("'") and v.endswith("'") and len(v) >= 2:
|
||||
v = v[1:-1]
|
||||
result[k] = v
|
||||
return result
|
||||
|
||||
|
||||
def _mask_secret(value: str) -> str:
|
||||
if not value:
|
||||
return ""
|
||||
if len(value) <= 4:
|
||||
return "***"
|
||||
return value[:2] + "***" + value[-2:]
|
||||
|
||||
|
||||
def read_env_config() -> dict:
|
||||
"""读取 .env 中的非空 key,敏感 key 脱敏为 ***。
|
||||
|
||||
Returns:
|
||||
{
|
||||
"env_path": str,
|
||||
"exists": bool,
|
||||
"config": {key: value_or_masked_value, ...}
|
||||
}
|
||||
"""
|
||||
path = get_env_path()
|
||||
parsed = _parse_env_file(path)
|
||||
out: dict[str, str] = {}
|
||||
for k, v in sorted(parsed.items()):
|
||||
if not v:
|
||||
continue
|
||||
if _SECRET_PATTERN.search(k):
|
||||
out[k] = _mask_secret(v)
|
||||
else:
|
||||
out[k] = v
|
||||
return {"env_path": str(path), "exists": path.exists(), "config": out}
|
||||
111
scripts/service/importer.py
Normal file
111
scripts/service/importer.py
Normal file
@@ -0,0 +1,111 @@
|
||||
"""主动扫描 data/input/{contracts,materials}/:CSV → contracts 表,PDF/Word → materials_content 表。
|
||||
|
||||
对 sandbox 用户尤其重要:HR API 拿不到时,把材料直接丢进 input/materials/ 即可被自动识别。
|
||||
|
||||
约定:
|
||||
- input/contracts/*.csv
|
||||
列名与 contracts 表一致(contract_id / teacher_name / unit_name / contract_end / ...)
|
||||
也兼容中文别名(合同ID / 教职工姓名 / 单位 / 合同到期日 / 工号 / 岗位 / 合同类型)
|
||||
- input/materials/{contract_id}__{filename}.{pdf|docx|doc}
|
||||
文件名 __ 之前是 contract_id;无 __ 时回退 parent dir 名
|
||||
拷贝到 data/materials/ + insert materials_content(parse_method='pending')
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import csv
|
||||
import shutil
|
||||
import sqlite3
|
||||
from pathlib import Path
|
||||
|
||||
from db import repository as repo
|
||||
from db.migrations import ensure_materials_content_schema
|
||||
from util.logging_setup import get_skill_logger
|
||||
from util.runtime_paths import (
|
||||
get_data_dir,
|
||||
get_input_contracts_dir,
|
||||
get_input_materials_dir,
|
||||
)
|
||||
|
||||
logger = get_skill_logger()
|
||||
|
||||
MATERIAL_EXTS = {".pdf", ".docx", ".doc"}
|
||||
|
||||
|
||||
def _derive_contract_id_from_filename(mat_path: Path) -> str:
|
||||
"""从文件名推导 contract_id。约定:{contract_id}__{filename}.{ext}。"""
|
||||
parts = mat_path.stem.split("__", 1)
|
||||
if len(parts) == 2 and parts[0]:
|
||||
return parts[0]
|
||||
# 兜底:parent dir 名
|
||||
return mat_path.parent.name or "UNKNOWN"
|
||||
|
||||
|
||||
def import_input_dir(conn: sqlite3.Connection) -> dict:
|
||||
"""扫描 input 目录 → 入库。返回统计 dict。
|
||||
|
||||
Returns:
|
||||
{
|
||||
"contracts_added": int,
|
||||
"materials_added": int,
|
||||
"files_scanned": int,
|
||||
"errors": [str, ...],
|
||||
}
|
||||
"""
|
||||
ensure_materials_content_schema(conn)
|
||||
|
||||
contracts_dir = get_input_contracts_dir()
|
||||
materials_dir = get_input_materials_dir()
|
||||
|
||||
contracts_added = 0
|
||||
materials_added = 0
|
||||
files_scanned = 0
|
||||
errors: list[str] = []
|
||||
|
||||
# 1. CSV → contracts(contract_id 重复 → upsert 覆盖)
|
||||
for csv_path in sorted(contracts_dir.glob("*.csv")):
|
||||
files_scanned += 1
|
||||
try:
|
||||
with open(csv_path, "r", encoding="utf-8", newline="") as f:
|
||||
reader = csv.DictReader(f)
|
||||
for row in reader:
|
||||
if not row.get("contract_id") and not row.get("合同ID"):
|
||||
errors.append(f"{csv_path.name}: row 缺 contract_id 跳过")
|
||||
continue
|
||||
repo.upsert_contract_from_csv_row(conn, row)
|
||||
contracts_added += 1
|
||||
except Exception as e:
|
||||
errors.append(f"{csv_path.name}: {e}")
|
||||
logger.warning("importer: CSV 解析失败 %s: %s", csv_path, e)
|
||||
|
||||
# 2. PDF/Word → materials_content(parse_method='pending'),文件拷贝到 data/materials/
|
||||
materials_target_dir = get_data_dir() / "materials"
|
||||
materials_target_dir.mkdir(parents=True, exist_ok=True)
|
||||
for mat_path in sorted(materials_dir.iterdir()):
|
||||
if not mat_path.is_file() or mat_path.suffix.lower() not in MATERIAL_EXTS:
|
||||
continue
|
||||
files_scanned += 1
|
||||
try:
|
||||
contract_id = _derive_contract_id_from_filename(mat_path)
|
||||
target = materials_target_dir / mat_path.name
|
||||
if not target.exists():
|
||||
shutil.copy2(mat_path, target)
|
||||
repo.insert_material(
|
||||
conn, contract_id, mat_path.name, str(target),
|
||||
None, "pending",
|
||||
)
|
||||
materials_added += 1
|
||||
logger.info(
|
||||
"importer: 入库 %s → contract_id=%s", mat_path.name, contract_id,
|
||||
)
|
||||
except Exception as e:
|
||||
errors.append(f"{mat_path.name}: {e}")
|
||||
logger.warning("importer: 材料导入失败 %s: %s", mat_path, e)
|
||||
|
||||
conn.commit()
|
||||
return {
|
||||
"contracts_added": contracts_added,
|
||||
"materials_added": materials_added,
|
||||
"files_scanned": files_scanned,
|
||||
"errors": errors,
|
||||
}
|
||||
86
scripts/service/logs_viewer.py
Normal file
86
scripts/service/logs_viewer.py
Normal file
@@ -0,0 +1,86 @@
|
||||
"""读取日志文件末尾 N 行(v1.1.18)。
|
||||
|
||||
按 level 过滤:默认 INFO+。适用于 RotatingFileHandler 落地的日志文件。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
from pathlib import Path
|
||||
|
||||
from util.runtime_paths import get_logs_dir
|
||||
|
||||
|
||||
DEFAULT_TAIL = 200
|
||||
_LEVEL_ORDER = {
|
||||
"DEBUG": 10, "INFO": 20, "WARNING": 30, "ERROR": 40, "CRITICAL": 50,
|
||||
}
|
||||
|
||||
|
||||
def _resolve_log_path() -> Path:
|
||||
"""解析日志文件路径。
|
||||
|
||||
优先级:
|
||||
1. LOG_FILE env(绝对路径直接用,相对路径走 get_logs_dir())
|
||||
2. 默认 get_logs_dir() / LOG_FILE_DEFAULT_NAME
|
||||
"""
|
||||
raw = os.environ.get("LOG_FILE", "")
|
||||
if raw:
|
||||
p = Path(raw)
|
||||
if not p.is_absolute():
|
||||
p = get_logs_dir() / raw
|
||||
return p
|
||||
from util.constants import LOG_FILE_DEFAULT_NAME
|
||||
return get_logs_dir() / LOG_FILE_DEFAULT_NAME
|
||||
|
||||
|
||||
def tail_log_file(tail: int = DEFAULT_TAIL, min_level: str = "INFO") -> dict:
|
||||
"""读日志文件末尾 tail 行,过滤 level >= min_level。
|
||||
|
||||
Returns:
|
||||
{
|
||||
"log_file": str,
|
||||
"exists": bool,
|
||||
"total_lines": int,
|
||||
"returned_lines": int,
|
||||
"lines": [str, ...],
|
||||
"min_level": str,
|
||||
}
|
||||
"""
|
||||
log_path = _resolve_log_path()
|
||||
if not log_path.exists():
|
||||
return {
|
||||
"log_file": str(log_path),
|
||||
"exists": False,
|
||||
"total_lines": 0,
|
||||
"returned_lines": 0,
|
||||
"lines": [],
|
||||
"min_level": min_level,
|
||||
}
|
||||
|
||||
text = log_path.read_text(encoding="utf-8", errors="replace")
|
||||
all_lines = text.splitlines()
|
||||
last_n = all_lines[-tail:] if tail > 0 else all_lines
|
||||
threshold = _LEVEL_ORDER.get(min_level.upper(), 20)
|
||||
|
||||
filtered: list[str] = []
|
||||
for line in last_n:
|
||||
# 格式: "2026-09-15 12:34:56 [alert-contract-expiring-hr] INFO message"
|
||||
# 取第 4 个 token (INFO/DEBUG/...)
|
||||
parts = line.split()
|
||||
if len(parts) >= 4 and parts[1].count(":") >= 2 and parts[3] in _LEVEL_ORDER:
|
||||
line_level = parts[3]
|
||||
if _LEVEL_ORDER.get(line_level, 0) >= threshold:
|
||||
filtered.append(line)
|
||||
else:
|
||||
# 非标准行 / 解析不到 level:保留
|
||||
filtered.append(line)
|
||||
|
||||
return {
|
||||
"log_file": str(log_path),
|
||||
"exists": True,
|
||||
"total_lines": len(all_lines),
|
||||
"returned_lines": len(filtered),
|
||||
"lines": filtered,
|
||||
"min_level": min_level,
|
||||
}
|
||||
@@ -36,6 +36,7 @@ from util.constants import (
|
||||
FULL_RENDER_WAIT_MS,
|
||||
GOTO_TIMEOUT_MS,
|
||||
HUMAN_POLL_INTERVAL,
|
||||
HUMAN_PROMPT_INTERVAL,
|
||||
HUMAN_WAIT_TIMEOUT,
|
||||
POST_LOGIN_SETTLE_MS,
|
||||
POST_RELOAD_LOGIN_HYDRATION_MS,
|
||||
@@ -55,6 +56,8 @@ from util.constants import (
|
||||
TABLE_POLL_INTERVAL_MS,
|
||||
TABLE_POLL_MAX_ATTEMPTS,
|
||||
TABLE_TR_TIMEOUT_MS,
|
||||
URL_STABLE_POLL_S,
|
||||
URL_STABLE_REQUIRED_S,
|
||||
URL_AFTER_LOGIN_TIMEOUT_MS,
|
||||
)
|
||||
from util.logging_setup import get_skill_logger
|
||||
@@ -164,33 +167,348 @@ def _is_logged_in(page) -> bool:
|
||||
)
|
||||
|
||||
|
||||
def _is_spa_routed(page) -> bool:
|
||||
"""v1.1.22:检测 sandbox SPA 是否已路由到 SPA 页面(含 /select /app/)。"""
|
||||
url_lower = page.url.lower()
|
||||
return (
|
||||
("/app/" in url_lower or "/select" in url_lower)
|
||||
and "/login" not in url_lower
|
||||
and "/signin" not in url_lower
|
||||
)
|
||||
|
||||
|
||||
def _try_auto_sign_in(page, wait_input_sec: int = 2) -> bool:
|
||||
"""v1.1.21:检测登录页 demo/123456 是否已预填,有则自动点 Sign in。
|
||||
|
||||
v1.1.24:扩展到支持 SPA 二级登录页(/industries/campus/smartcampus/login),
|
||||
按钮文本可能是中文「登录」而非英文「Sign in」。
|
||||
|
||||
适用场景:
|
||||
- 根域登录页(https://sandbox.jc2009.com/):按钮「Sign in」
|
||||
- SPA 二级登录页(/industries/campus/smartcampus/login):按钮「登录」
|
||||
默认有 demo/123456(或 18677308191 等真实账号)预填在 input。
|
||||
|
||||
行为:
|
||||
1. 等 input 出现,最长 wait_input_sec 秒(默认 2s)
|
||||
2. 检查 input 是否都有值
|
||||
3. 有值 → 点 Sign in / 登录 → return True
|
||||
4. 无值 → return False(交给 _wait_for_login_inputs_filled 接管)
|
||||
|
||||
不会自动 fill(如果没值就放弃,让用户自己输)。
|
||||
"""
|
||||
try:
|
||||
page.wait_for_selector(
|
||||
"#jc-user, #jc-pass, input[type='tel'], input[type='password']",
|
||||
timeout=wait_input_sec * 1000,
|
||||
)
|
||||
except Exception:
|
||||
logger.info(
|
||||
"platform_collector: %ds 内未出现登录 input,跳过自动登录",
|
||||
wait_input_sec,
|
||||
)
|
||||
return False
|
||||
|
||||
# 多种 input selector 优先级
|
||||
user_input = None
|
||||
for sel in ("#jc-user", "input[type='tel']", "input[autocomplete='username']"):
|
||||
loc = page.locator(sel).first
|
||||
if loc.count() > 0:
|
||||
user_input = loc
|
||||
break
|
||||
if user_input is None:
|
||||
return False
|
||||
|
||||
pass_input = None
|
||||
for sel in ("#jc-pass", "input[type='password']", "input[autocomplete='current-password']"):
|
||||
loc = page.locator(sel).first
|
||||
if loc.count() > 0:
|
||||
pass_input = loc
|
||||
break
|
||||
if pass_input is None:
|
||||
return False
|
||||
|
||||
user_val = (user_input.input_value() or "").strip()
|
||||
pass_val = (pass_input.input_value() or "").strip()
|
||||
|
||||
if not user_val or not pass_val:
|
||||
logger.info(
|
||||
"platform_collector: 登录页 input 为空(user=%r, pass=%r),"
|
||||
"等用户输入",
|
||||
bool(user_val), bool(pass_val),
|
||||
)
|
||||
return False
|
||||
|
||||
# 多种按钮文本优先级:英文 Sign in > 中文登录 > submit
|
||||
btn = None
|
||||
btn_text_used = ""
|
||||
for txt in ("Sign in", "登录", "Login", "signin"):
|
||||
loc = page.locator(f"button:has-text('{txt}')").first
|
||||
if loc.count() > 0:
|
||||
btn = loc
|
||||
btn_text_used = txt
|
||||
break
|
||||
if btn is None:
|
||||
btn = page.locator("button[type='submit']").first
|
||||
btn_text_used = "submit"
|
||||
if btn is None or btn.count() == 0:
|
||||
logger.warning("platform_collector: 未找到登录按钮")
|
||||
return False
|
||||
|
||||
logger.info(
|
||||
"platform_collector: 登录页已预填账号(user=%r),自动点 %s 按钮",
|
||||
user_val, btn_text_used,
|
||||
)
|
||||
btn.click()
|
||||
return True
|
||||
|
||||
|
||||
def _wait_for_login_inputs_filled(page, wait_sec: int = HUMAN_WAIT_TIMEOUT) -> bool:
|
||||
"""v1.1.21:等用户输入完账号密码,然后自动点登录按钮。
|
||||
|
||||
v1.1.26:按用户最新要求改回**自动点登录**行为——
|
||||
「到了 /industries/campus/smartcampus/login 整个页面,有账号和密码可用直接点登录跳转」,
|
||||
不再等用户自己点。
|
||||
|
||||
优先级(任一 selector 命中即视为有 input):
|
||||
- user: `#jc-user` / `input[type='tel']` / `input[autocomplete='username']`
|
||||
- pass: `#jc-pass` / `input[type='password']` / `input[autocomplete='current-password']`
|
||||
|
||||
按钮文本优先级:`Sign in` / `登录` / `Login` / `signin` / `submit`。
|
||||
"""
|
||||
deadline = time.monotonic() + wait_sec
|
||||
last_prompt_at = time.monotonic()
|
||||
last_state_log = time.monotonic()
|
||||
|
||||
while time.monotonic() < deadline:
|
||||
cur_url = page.url
|
||||
|
||||
# 已不在登录页(用户可能已点登录完成跳转)
|
||||
if not _is_login_page_url_for_user(cur_url):
|
||||
logger.info(
|
||||
"platform_collector: URL 已离开登录页(%s),登录完成", cur_url,
|
||||
)
|
||||
return True
|
||||
|
||||
# 探测 input(多 selector 优先级)
|
||||
user_val, pass_val = "", ""
|
||||
user_loc, pass_loc = None, None
|
||||
for sel in ("#jc-user", "input[type='tel']", "input[autocomplete='username']"):
|
||||
loc = page.locator(sel).first
|
||||
if loc.count() > 0:
|
||||
user_loc = loc
|
||||
user_val = (loc.input_value() or "").strip()
|
||||
break
|
||||
for sel in ("#jc-pass", "input[type='password']", "input[autocomplete='current-password']"):
|
||||
loc = page.locator(sel).first
|
||||
if loc.count() > 0:
|
||||
pass_loc = loc
|
||||
pass_val = (loc.input_value() or "").strip()
|
||||
break
|
||||
|
||||
now = time.monotonic()
|
||||
if now - last_state_log >= HUMAN_PROMPT_INTERVAL:
|
||||
last_state_log = now
|
||||
logger.info(
|
||||
"platform_collector: 等待用户输入 user=%r pass=%r (URL=%s)",
|
||||
bool(user_val), bool(pass_val), cur_url,
|
||||
)
|
||||
|
||||
# input 都有值 → 自动点登录按钮(v1.1.26 关键:用户最新要求自动点)
|
||||
if user_val and pass_val:
|
||||
btn = None
|
||||
btn_text_used = ""
|
||||
for txt in ("Sign in", "登录", "Login", "signin"):
|
||||
loc = page.locator(f"button:has-text('{txt}')").first
|
||||
if loc.count() > 0:
|
||||
btn = loc
|
||||
btn_text_used = txt
|
||||
break
|
||||
if btn is None:
|
||||
btn = page.locator("button[type='submit']").first
|
||||
btn_text_used = "submit"
|
||||
if btn is not None and btn.count() > 0:
|
||||
logger.info(
|
||||
"platform_collector: ✅ 账号密码已填好(user=%r),自动点 %s 按钮",
|
||||
user_val, btn_text_used,
|
||||
)
|
||||
btn.click()
|
||||
return True
|
||||
logger.warning("platform_collector: input 有值但找不到登录按钮")
|
||||
|
||||
# 提醒用户
|
||||
if now - last_prompt_at >= HUMAN_PROMPT_INTERVAL:
|
||||
last_prompt_at = now
|
||||
remaining = int(deadline - now)
|
||||
if user_val and pass_val:
|
||||
# 上面 click 应该已经 return,不会到这里
|
||||
print(
|
||||
f"[登录] ✅ 账号密码已填好(user={user_val[:3]}***),"
|
||||
f"请点「登录」按钮,剩余 {remaining}s..."
|
||||
)
|
||||
else:
|
||||
print(
|
||||
f"[登录] 等待用户输入账号密码... 剩余 {remaining}s"
|
||||
)
|
||||
|
||||
time.sleep(HUMAN_POLL_INTERVAL)
|
||||
|
||||
logger.warning(
|
||||
"platform_collector: %ds 内用户未跳转(最后 URL=%s)", wait_sec, page.url,
|
||||
)
|
||||
return False
|
||||
|
||||
|
||||
def _is_login_page_url_for_user(url: str) -> bool:
|
||||
"""判断 URL 是否还在登录页(用于 _wait_for_login_inputs_filled 退出条件)。"""
|
||||
u = url.lower()
|
||||
if "/login" in u or "/signin" in u or "/auth/" in u:
|
||||
return True
|
||||
# 根域本身也是登录页(demo 数据未提交前的初始态)
|
||||
return u.rstrip("/").endswith("sandbox.jc2009.com")
|
||||
|
||||
|
||||
def _wait_for_manual_login(page, wait_sec: int = HUMAN_WAIT_TIMEOUT) -> bool:
|
||||
"""手工登录 fallback:轮询 _is_logged_in,每 HUMAN_POLL_INTERVAL 秒一次。
|
||||
"""手工登录 fallback(v1.1.19 重写,v1.1.20 加强,v1.1.21 修订):
|
||||
等用户在浏览器完成登录。
|
||||
|
||||
与 skill-template task_rpa.py::_ensure_logged_in 设计对齐:
|
||||
- 不自动破解,等用户在浏览器手工完成登录
|
||||
- 屏幕打印 [登录] 提示
|
||||
- 超时抛 LoginTimeoutError(ERROR:LOGIN_TIMEOUT)
|
||||
判定「登录完成」的多个信号(满足任一即视为完成):
|
||||
1. URL 含 `/app/` 且稳定 URL_STABLE_REQUIRED_S 秒(v1.1.19 旧逻辑)
|
||||
2. URL 已离开 `/login` / `/signin` / `/auth/` 等登录路径(v1.1.20 新增)
|
||||
—— 即便 sandbox 未跳到 /app/,只要离开登录页就认为登录成功
|
||||
3. 页面出现 sidebar 元素 `a[href$='/hr/contracts']`(v1.1.20 新增)
|
||||
—— 说明已加载到 SPA 主页
|
||||
|
||||
自动 fill demo/123456 失败时调用此函数。
|
||||
- **不做任何 goto / reload / click**,避免打断用户输入
|
||||
- 每 HUMAN_PROMPT_INTERVAL 秒打印一次剩余时间(默认 15s)
|
||||
- 每 HUMAN_POLL_INTERVAL 秒检查一次(默认 5s)
|
||||
- 超时抛 LoginTimeoutError。
|
||||
"""
|
||||
print(
|
||||
f"[登录] 自动登录失败,请在浏览器中完成登录"
|
||||
f"[登录] 请在浏览器中完成登录"
|
||||
f"(sandbox demo/123456 或真实账号),最多等待 {wait_sec} 秒..."
|
||||
)
|
||||
deadline = time.monotonic() + wait_sec
|
||||
attempt = 0
|
||||
while time.monotonic() < deadline:
|
||||
attempt += 1
|
||||
last_prompt_at = time.monotonic()
|
||||
last_url = page.url
|
||||
last_url_change_at = time.monotonic()
|
||||
|
||||
def _is_login_page_url(url: str) -> bool:
|
||||
u = url.lower()
|
||||
return (
|
||||
"/login" in u or "/signin" in u or "/auth/" in u
|
||||
or u.rstrip("/").endswith("sandbox.jc2009.com")
|
||||
)
|
||||
|
||||
def _sidebar_present() -> bool:
|
||||
try:
|
||||
return page.locator("a[href$='/hr/contracts']").count() > 0
|
||||
except Exception:
|
||||
return False
|
||||
|
||||
while True:
|
||||
remaining = deadline - time.monotonic()
|
||||
if remaining <= 0:
|
||||
break
|
||||
|
||||
cur_url = page.url
|
||||
if cur_url != last_url:
|
||||
logger.info(
|
||||
"platform_collector: URL 变化 %s → %s", last_url, cur_url,
|
||||
)
|
||||
last_url = cur_url
|
||||
last_url_change_at = time.monotonic()
|
||||
|
||||
# 信号 1:URL 跳到 /app/... 并稳定
|
||||
if _is_logged_in(page):
|
||||
print(f"[登录] 检测到登录成功(attempt {attempt})")
|
||||
stable_for = time.monotonic() - last_url_change_at
|
||||
if stable_for >= URL_STABLE_REQUIRED_S:
|
||||
print(
|
||||
f"[登录] ✅ 检测到登录完成(URL 含 /app/,稳定 {stable_for:.1f}s)"
|
||||
)
|
||||
return True
|
||||
logger.info(
|
||||
"platform_collector: URL 含 /app/ 但还在过渡,等稳定 "
|
||||
"(current=%s, stable_for=%.1fs)", cur_url, stable_for,
|
||||
)
|
||||
# 信号 2:URL 已离开登录页(即使不在 /app/,比如卡在中间过渡页也算)
|
||||
elif not _is_login_page_url(cur_url):
|
||||
stable_for = time.monotonic() - last_url_change_at
|
||||
if stable_for >= URL_STABLE_REQUIRED_S:
|
||||
print(
|
||||
f"[登录] ✅ 检测到登录完成(URL 已离开登录页 → {cur_url},"
|
||||
f"稳定 {stable_for:.1f}s)"
|
||||
)
|
||||
return True
|
||||
logger.info(
|
||||
"platform_collector: URL 已离开登录页但还没稳定,等 "
|
||||
"(current=%s, stable_for=%.1fs)", cur_url, stable_for,
|
||||
)
|
||||
# 信号 3:sidebar 已出现
|
||||
elif _sidebar_present():
|
||||
print(
|
||||
f"[登录] ✅ 检测到登录完成(sidebar 已出现,URL={cur_url})"
|
||||
)
|
||||
return True
|
||||
else:
|
||||
# 还在登录页:每 HUMAN_PROMPT_INTERVAL 秒提醒用户一次
|
||||
now = time.monotonic()
|
||||
if now - last_prompt_at >= HUMAN_PROMPT_INTERVAL:
|
||||
last_prompt_at = now
|
||||
print(
|
||||
f"[登录] 等待用户输入... 剩余 {int(remaining)}s "
|
||||
f"(当前 URL={cur_url})"
|
||||
)
|
||||
|
||||
time.sleep(HUMAN_POLL_INTERVAL)
|
||||
|
||||
raise LoginTimeoutError(
|
||||
f"自动登录失败,{wait_sec}s 内用户在浏览器中也未完成登录"
|
||||
f"(最后 URL={page.url})"
|
||||
)
|
||||
|
||||
|
||||
def _wait_for_url_stable(
|
||||
page, target_substr: str | tuple[str, ...], max_wait_sec: int = 30,
|
||||
) -> bool:
|
||||
"""v1.1.19:等 URL 含 target_substr,且连续 URL_STABLE_REQUIRED_S 秒不变。
|
||||
|
||||
v1.1.22:target_substr 支持 tuple(任一匹配即视为稳定)。
|
||||
|
||||
用于「已登录态直接跳到目标页面」后等待 SPA 路由稳定。
|
||||
**不做任何 goto / reload**,只 listen URL 变化。
|
||||
|
||||
Returns:
|
||||
True = URL 已稳定到任一目标;False = 超时。
|
||||
"""
|
||||
if isinstance(target_substr, str):
|
||||
targets = (target_substr,)
|
||||
else:
|
||||
targets = target_substr
|
||||
|
||||
deadline = time.monotonic() + max_wait_sec
|
||||
last_url = page.url
|
||||
last_url_change_at = time.monotonic()
|
||||
|
||||
while time.monotonic() < deadline:
|
||||
cur_url = page.url
|
||||
if cur_url != last_url:
|
||||
logger.info(
|
||||
"platform_collector._wait_for_url_stable: URL 变化 %s → %s",
|
||||
last_url, cur_url,
|
||||
)
|
||||
last_url = cur_url
|
||||
last_url_change_at = time.monotonic()
|
||||
|
||||
cur_lower = cur_url.lower()
|
||||
if any(t.lower() in cur_lower for t in targets):
|
||||
stable_for = time.monotonic() - last_url_change_at
|
||||
if stable_for >= URL_STABLE_REQUIRED_S:
|
||||
return True
|
||||
|
||||
time.sleep(URL_STABLE_POLL_S)
|
||||
return False
|
||||
|
||||
|
||||
def _fill_root_login(page) -> bool:
|
||||
"""根域登录页(如还在):fill demo/123456 → click Sign in。返回是否执行了登录。"""
|
||||
user_input = page.locator('#jc-user').first
|
||||
@@ -384,90 +702,109 @@ def collect_all(
|
||||
# 所有 wait_for_selector 默认 30s,调试时不直观)
|
||||
page.set_default_timeout(DEFAULT_PAGE_TIMEOUT_MS)
|
||||
|
||||
# 1. v1.1.11:先用**最小侵入**方式检测已登录态 —— 直接 goto SPA_HOME_URL
|
||||
# (不要先跳 ROOT_URL + 自动 fill,会破坏已登录态)
|
||||
# v1.1.11 改进:探针结果 + 二次等待 + cookie 检测,三重判断避免误判
|
||||
# 1. v1.1.21:先 goto **根域**(sandbox 登录页),让 SPA hydrate 2 秒
|
||||
logger.info(
|
||||
"platform_collector: 检测登录态,goto SPA_HOME 先探 (当前 URL=%s)",
|
||||
page.url,
|
||||
"platform_collector: 启动,goto ROOT_URL=%s (当前 URL=%s)",
|
||||
ROOT_URL, page.url,
|
||||
)
|
||||
_goto(page, SPA_HOME_URL)
|
||||
# v1.1.11:等 sandbox SPA 异步验证 cookie(POST_LOGIN_SETTLE_MS → SPA_HYDRATION_MS)
|
||||
page.wait_for_timeout(SPA_HYDRATION_MS)
|
||||
# v1.1.11:二次等待 —— sandbox SPA 有时首次访问会先跳一个中间过渡页
|
||||
# (如 /select),等几秒看 URL 是否稳定到 /app/
|
||||
first_url = page.url
|
||||
page.wait_for_timeout(SPA_HOME_RETRY_DELAY_MS)
|
||||
second_url = page.url
|
||||
if first_url != second_url:
|
||||
logger.info(
|
||||
"platform_collector: 探针 URL 过渡 %s → %s,等稳定", first_url, second_url,
|
||||
)
|
||||
page.wait_for_timeout(SPA_HOME_RETRY_DELAY_MS)
|
||||
_goto(page, ROOT_URL)
|
||||
# 给 React/Vue 框架 2 秒 hydrate 时间,看登录 input 是否就绪
|
||||
page.wait_for_timeout(2_000)
|
||||
|
||||
if _is_logged_in(page):
|
||||
logger.info(
|
||||
"platform_collector: ✅ 已登录态,**完全跳过登录流程**(不弹窗、不自动 fill、不打扰)"
|
||||
",URL=%s", page.url,
|
||||
)
|
||||
else:
|
||||
logger.info(
|
||||
"platform_collector: 未登录态(SPA_HOME 跳回 login, URL=%s),走登录分支",
|
||||
"platform_collector: ✅ 已登录态(URL=%s),跳过登录流程",
|
||||
page.url,
|
||||
)
|
||||
# 2. 未登录:直接 goto sandbox SPA login(绕过 /select 和 root 跳转)
|
||||
_goto(page, SPA_LOGIN_URL)
|
||||
# SPA hydration + framework 初始化时间(修复登录时间太短的核心点)
|
||||
page.wait_for_timeout(SPA_HYDRATION_MS)
|
||||
|
||||
# 3. sandbox SPA 自动登录(demo/123456 已知可用)
|
||||
# 失败 fallback 手工登录等待(轮询 _is_logged_in + 180s 兜底)
|
||||
if not _fill_spa_login(page):
|
||||
logger.warning(
|
||||
"platform_collector: 自动 fill 失败(URL=%s),fallback 手工登录等待",
|
||||
page.url,
|
||||
else:
|
||||
# 2. 等 #jc-user / #jc-pass 出现(最多 2 秒)
|
||||
# 若 sandbox 已预填 demo/123456 → 自动点 Sign in
|
||||
# 若 input 为空 → 等用户输入(**v1.1.24 用户自己点登录按钮**)
|
||||
auto_clicked = _try_auto_sign_in(page, wait_input_sec=2)
|
||||
if auto_clicked:
|
||||
logger.info(
|
||||
"platform_collector: ✅ 自动登录成功(预填 demo),等 URL 跳到 /app/"
|
||||
)
|
||||
if not headless:
|
||||
_wait_for_manual_login(page, wait_sec=human_wait_timeout)
|
||||
else:
|
||||
# headless 模式无法手工登录:抛 LoginTimeoutError 让 CLI 决定
|
||||
raise LoginTimeoutError(
|
||||
f"headless 模式下自动 fill 失败,且无法手工登录"
|
||||
f"(请用 --show-browser 重试)"
|
||||
elif not headless:
|
||||
# 3. input 为空:等用户输入 → **用户自己点 Sign in**(脚本不抢按钮)
|
||||
if _wait_for_login_inputs_filled(page, wait_sec=human_wait_timeout):
|
||||
logger.info(
|
||||
"platform_collector: ✅ 用户完成输入并跳转,等 URL 跳到 /app/"
|
||||
)
|
||||
else:
|
||||
# 超时未跳转 → fallback 到原来的多信号等待
|
||||
logger.warning(
|
||||
"platform_collector: 用户未跳转,fallback 到 _wait_for_manual_login"
|
||||
)
|
||||
_wait_for_manual_login(page, wait_sec=human_wait_timeout)
|
||||
else:
|
||||
# 等 SPA 路由稳定(之前这里没有 wait,直接 goto home 会打断 pending 跳转)
|
||||
page.wait_for_timeout(POST_LOGIN_SETTLE_MS)
|
||||
raise LoginTimeoutError(
|
||||
f"headless 模式下未登录,且无法手工登录"
|
||||
f"(请用 --show-browser 重试)"
|
||||
)
|
||||
|
||||
# 4. 强制 goto SPA home(如果还停在 login,重试登录一次)
|
||||
for retry in range(SPA_HOME_RETRY_MAX):
|
||||
if _is_logged_in(page):
|
||||
break
|
||||
# 4. 登录完成 → 等 URL 真正稳定到 /app/ 或 /select(最多 30s,**不做任何操作**)
|
||||
# v1.1.22:sandbox 登录后会先跳 /select,再自动跳 /app/home,
|
||||
# 所以两个 URL 都算"SPA 已路由"
|
||||
if not _wait_for_url_stable(page, ("/app/", "/select"), max_wait_sec=30):
|
||||
logger.warning(
|
||||
"platform_collector: 登录后 30s 内 URL 未稳定到 /app/或 /select(当前 URL=%s)",
|
||||
page.url,
|
||||
)
|
||||
# 不强行 reload —— 让用户决定是否手工刷新
|
||||
|
||||
# 4.5. v1.1.24:sandbox 可能登录后把人踢回 SPA 二级登录页
|
||||
# (/industries/campus/smartcampus/login)。如果 URL 落在
|
||||
# 这个二级登录页,再走一次 _try_auto_sign_in(已经扩展支持
|
||||
# 中文「登录」按钮);若 SPA 登录页 input 有预填账号 → 自动点;
|
||||
# 若 input 空 → 等用户输入并**自己点登录**(脚本不抢按钮)。
|
||||
cur_url_after_stable = page.url.lower()
|
||||
if "/login" in cur_url_after_stable or "/signin" in cur_url_after_stable:
|
||||
logger.warning(
|
||||
"platform_collector: 登录后仍落在登录页(%s),"
|
||||
"v1.1.24 触发 SPA 二级登录兜底",
|
||||
page.url,
|
||||
)
|
||||
spa_auto = _try_auto_sign_in(page, wait_input_sec=2)
|
||||
if spa_auto:
|
||||
logger.info(
|
||||
"platform_collector: ✅ SPA 二级登录页已自动登录,"
|
||||
"等 URL 跳到 /app/ 或 /select"
|
||||
)
|
||||
_wait_for_url_stable(page, ("/app/", "/select"), max_wait_sec=30)
|
||||
elif not headless:
|
||||
if _wait_for_login_inputs_filled(page, wait_sec=human_wait_timeout):
|
||||
_wait_for_url_stable(page, ("/app/", "/select"), max_wait_sec=30)
|
||||
else:
|
||||
_wait_for_manual_login(page, wait_sec=human_wait_timeout)
|
||||
else:
|
||||
raise LoginTimeoutError(
|
||||
f"SPA 二级登录页 input 空且 headless 无法手工登录(URL={page.url})"
|
||||
)
|
||||
|
||||
# 5. v1.1.22:显式跳到 SPA_HOME_URL(/app/home),等侧边栏 link 出现
|
||||
# 避免 sandbox 在 /select 中间页时直接找 sidebar link 找不到
|
||||
if "/app/home" not in page.url.lower():
|
||||
logger.info(
|
||||
"platform_collector: 强制 goto SPA_HOME (retry=%d), 当前 URL=%s",
|
||||
retry, page.url,
|
||||
"platform_collector: 当前 URL=%s,跳到 SPA_HOME_URL=%s",
|
||||
page.url, SPA_HOME_URL,
|
||||
)
|
||||
_goto(page, SPA_HOME_URL)
|
||||
page.wait_for_timeout(SPA_HOME_RETRY_DELAY_MS)
|
||||
if _fill_spa_login(page):
|
||||
page.wait_for_timeout(POST_LOGIN_SETTLE_MS)
|
||||
|
||||
# 4.5. retry 后若仍未登录,headless 模式抛超时
|
||||
if not _is_logged_in(page):
|
||||
if headless:
|
||||
raise LoginTimeoutError(
|
||||
"SPA_HOME retry 全部失败,URL 仍未离开 login 页"
|
||||
page.wait_for_timeout(SPA_HYDRATION_MS)
|
||||
# 等 URL 稳定到 /app/home(最多 15s,**不做任何操作**)
|
||||
if not _wait_for_url_stable(page, "/app/home", max_wait_sec=15):
|
||||
logger.warning(
|
||||
"platform_collector: 15s 内未稳定到 /app/home(当前 URL=%s)",
|
||||
page.url,
|
||||
)
|
||||
_wait_for_manual_login(page, wait_sec=human_wait_timeout)
|
||||
|
||||
# 5. 等 SPA 渲染 + click 侧边栏 hr/contracts(SPA 路由,避免 goto 触发整页 reload 丢 session)
|
||||
# 6. 等 SPA 渲染 + click 侧边栏 hr/contracts(SPA 路由,避免 goto 触发整页 reload 丢 session)
|
||||
try:
|
||||
# dump SPA home 一次(用于调试 selector)
|
||||
page.wait_for_timeout(POST_LOGIN_SETTLE_MS)
|
||||
_dump_page(page, download_dir.parent / "spa_home_dump.html", "SPA home")
|
||||
|
||||
# 等 SPA 真正加载完(侧边栏 link 出现,最长 SIDEBAR_LINK_TIMEOUT_MS)
|
||||
# 修复:之前 15s 太短,sandbox mock 加载慢时直接 fallback 到 pushState → 表格加载不出来
|
||||
try:
|
||||
page.wait_for_selector(
|
||||
"a[href$='/hr/contracts']",
|
||||
@@ -763,11 +1100,21 @@ def collect_all(
|
||||
ctx.close()
|
||||
|
||||
saved = _persist_contracts(contracts)
|
||||
logger.info(
|
||||
"platform_collector: contracts 落库完成 saved=%d / total_fetched=%d",
|
||||
saved, len(contracts),
|
||||
)
|
||||
|
||||
# v1.1.18:每个合同入队 download_tasks(独立事务,失败不影响主流程)
|
||||
contract_ids = [c["contract_id"] for c in contracts if c.get("contract_id")]
|
||||
enqueue_summary = _enqueue_download_tasks_safe(contract_ids)
|
||||
|
||||
return {
|
||||
"platform_url": PLATFORM_URL,
|
||||
"fetched_rows": fetched_rows,
|
||||
"saved_contracts": saved,
|
||||
"downloaded_materials": downloaded,
|
||||
"download_queue": enqueue_summary,
|
||||
"contracts": contracts,
|
||||
}
|
||||
|
||||
@@ -865,11 +1212,329 @@ def _persist_contracts(contracts: list[dict]) -> int:
|
||||
datetime.now().isoformat(timespec="seconds"),
|
||||
),
|
||||
)
|
||||
# v1.1.18 入队 download_tasks 已移到 collect_all 末尾的独立事务,
|
||||
# 见 _enqueue_download_tasks_safe()
|
||||
return len(contracts)
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
|
||||
def _enqueue_download_tasks_safe(
|
||||
contract_ids: list[str], conn: sqlite3.Connection | None = None,
|
||||
) -> dict:
|
||||
"""v1.1.18:每个合同入队 download_tasks。
|
||||
|
||||
在独立事务里完成,不影响 _persist_contracts 主流程(contracts / materials_content)。
|
||||
任一入队失败 → 记 warning + 计入 failed,但不影响整体返回。
|
||||
|
||||
conn: 可选传入连接(用于测试 in-memory DB)。None → 走 get_connection()。
|
||||
若传入 conn,调用方负责生命周期(不会 close)。
|
||||
|
||||
Returns:
|
||||
{enqueued: N, skipped: M, failed: K, errors: [...]}
|
||||
"""
|
||||
if not contract_ids:
|
||||
return {"enqueued": 0, "skipped": 0, "failed": 0, "errors": []}
|
||||
|
||||
from db.connection import get_connection
|
||||
|
||||
enqueued = 0
|
||||
skipped = 0
|
||||
failed = 0
|
||||
errors: list[str] = []
|
||||
|
||||
owns_conn = conn is None
|
||||
if owns_conn:
|
||||
conn = get_connection()
|
||||
try:
|
||||
for cid in contract_ids:
|
||||
try:
|
||||
# 入队前查一下是否已存在 pending/running → 决定计入 skipped 还是 enqueued
|
||||
pre_existing = conn.execute(
|
||||
"""SELECT 1 FROM download_tasks
|
||||
WHERE contract_id = ? AND kind = ?
|
||||
AND status IN ('pending', 'running') LIMIT 1""",
|
||||
(cid, "platform_button"),
|
||||
).fetchone()
|
||||
repo.enqueue_download_task(conn, cid, "platform_button")
|
||||
if pre_existing:
|
||||
skipped += 1
|
||||
else:
|
||||
enqueued += 1
|
||||
except Exception as e:
|
||||
failed += 1
|
||||
err = f"{cid}: {type(e).__name__}: {e}"
|
||||
errors.append(err)
|
||||
logger.warning(
|
||||
"platform_collector.enqueue: 入队 %s 失败: %s", cid, e,
|
||||
)
|
||||
if owns_conn:
|
||||
conn.commit()
|
||||
finally:
|
||||
if owns_conn:
|
||||
conn.close()
|
||||
|
||||
logger.info(
|
||||
"platform_collector.enqueue: 入队结果 enqueued=%d skipped=%d failed=%d (total=%d)",
|
||||
enqueued, skipped, failed, len(contract_ids),
|
||||
)
|
||||
return {
|
||||
"enqueued": enqueued, "skipped": skipped,
|
||||
"failed": failed, "errors": errors[:10],
|
||||
}
|
||||
|
||||
|
||||
def _download_materials_for_row(
|
||||
page, row_idx: int, contract_id: str, mat_btn_count: int,
|
||||
) -> list[dict]:
|
||||
"""点续聘材料包按钮抓卡片文本(与 v1.1.17 collect_all 内联逻辑等价)。
|
||||
|
||||
返回 materials 列表(kind=sandbox_card,含 file_name + text)。
|
||||
"""
|
||||
if mat_btn_count == 0:
|
||||
return []
|
||||
materials: list[dict] = []
|
||||
try:
|
||||
btn = page.locator("table tbody tr").nth(row_idx).locator(
|
||||
"button:has-text('续聘材料包')"
|
||||
)
|
||||
btn.click()
|
||||
page.wait_for_timeout(CARD_APPEAR_WAIT_MS)
|
||||
cards_text = page.locator(
|
||||
"main .grid > div, main > div > div > div"
|
||||
).all_inner_texts()
|
||||
materials_text = "\n\n".join(t.strip() for t in cards_text if t.strip())
|
||||
if materials_text:
|
||||
materials.append({
|
||||
"kind": "sandbox_card",
|
||||
"file_name": f"{contract_id}_materials.txt",
|
||||
"text": materials_text,
|
||||
})
|
||||
logger.info(
|
||||
"platform_collector: 抓到卡片文本 %d 字 (%s)",
|
||||
len(materials_text), contract_id,
|
||||
)
|
||||
except Exception as e:
|
||||
logger.warning("抓卡片失败 %s: %s", contract_id, e)
|
||||
return materials
|
||||
|
||||
|
||||
def _find_row_for_contract(page, contract_id: str) -> int | None:
|
||||
"""在 hr/contracts 表格中找 contract_id 所在行号。
|
||||
|
||||
支持 7 列(用 teacher_name 派生 contract_id)与 10 列(用 emp_id)。
|
||||
不匹配返回 None(调用方 mark_task_failed)。
|
||||
"""
|
||||
rows = page.eval_on_selector_all(
|
||||
"table tbody tr",
|
||||
"rs => rs.map(r => Array.from(r.querySelectorAll('td')).map(td => td.innerText))",
|
||||
)
|
||||
for i, r in enumerate(rows):
|
||||
ncols = len(r)
|
||||
if ncols < 6:
|
||||
continue
|
||||
first_col = r[0].strip()
|
||||
is_emp_id = bool(re.match(r"^[A-Za-z]?\d+$", first_col))
|
||||
if is_emp_id and ncols >= 9:
|
||||
emp_id, contract_end = first_col, r[5].split("~")[-1].strip()
|
||||
elif ncols >= 6:
|
||||
emp_id, contract_end = first_col, r[2].strip()
|
||||
else:
|
||||
continue
|
||||
candidate = _gen_contract_id(emp_id, contract_end)
|
||||
if candidate == contract_id:
|
||||
return i
|
||||
return None
|
||||
|
||||
|
||||
def process_queue(
|
||||
limit: int = 5,
|
||||
headless: bool = True,
|
||||
human_wait_timeout: int = HUMAN_WAIT_TIMEOUT,
|
||||
ids_filter: str = "",
|
||||
) -> dict:
|
||||
"""从 download_tasks 队列取 pending 任务,对每个合同重新走材料抓取。
|
||||
|
||||
与 collect_all 重复登录流程,但每行只点「续聘材料包」按钮抓卡片,不重新抓合同表。
|
||||
|
||||
参数:
|
||||
limit: 本次最多处理任务数(0 = 不限)
|
||||
ids_filter: 逗号分隔 task_id 列表(只处理这些 pending)
|
||||
headless: 无头模式(需要 profile cookie 已保存)
|
||||
human_wait_timeout: 手工登录 fallback 等待秒数
|
||||
|
||||
返回 {processed, succeeded, failed, details}。
|
||||
"""
|
||||
try:
|
||||
from playwright.sync_api import sync_playwright
|
||||
except ImportError:
|
||||
logger.error("playwright 未安装,跳过队列处理")
|
||||
return {"processed": 0, "succeeded": 0, "failed": 0, "details": [],
|
||||
"error": "playwright_not_installed"}
|
||||
|
||||
from db.connection import get_connection
|
||||
|
||||
# 1. 领取一批任务
|
||||
ids_list = [s.strip() for s in ids_filter.split(",") if s.strip()] if ids_filter else None
|
||||
conn = get_connection()
|
||||
try:
|
||||
tasks = repo.claim_pending_tasks(
|
||||
conn,
|
||||
limit=limit if limit > 0 else 999,
|
||||
ids_filter=ids_list,
|
||||
)
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
if not tasks:
|
||||
return {"processed": 0, "succeeded": 0, "failed": 0, "details": []}
|
||||
|
||||
profile_dir = _safe_getenv_dir("SMARTCAMPUS_PROFILE_DIR", "playwright-profile")
|
||||
download_dir = _safe_getenv_dir("MATERIALS_DOWNLOAD_DIR", "materials")
|
||||
profile_dir.mkdir(parents=True, exist_ok=True)
|
||||
download_dir.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
details: list[dict] = []
|
||||
succeeded = 0
|
||||
failed = 0
|
||||
|
||||
with sync_playwright() as p:
|
||||
ctx = p.chromium.launch_persistent_context(
|
||||
user_data_dir=str(profile_dir),
|
||||
headless=headless,
|
||||
accept_downloads=True,
|
||||
viewport={"width": 1440, "height": 900},
|
||||
channel="msedge" if not headless else None,
|
||||
) if not headless else p.chromium.launch_persistent_context(
|
||||
user_data_dir=str(profile_dir),
|
||||
headless=True,
|
||||
accept_downloads=True,
|
||||
viewport={"width": 1440, "height": 900},
|
||||
)
|
||||
try:
|
||||
page = ctx.new_page()
|
||||
page.set_default_timeout(DEFAULT_PAGE_TIMEOUT_MS)
|
||||
|
||||
# 登录:复用 v1.1.17 collect_all 的探针 + 自动 fill + 手工 fallback 流程
|
||||
logger.info("platform_collector.process_queue: 启动 profile=%s headless=%s 待处理 %d",
|
||||
profile_dir, headless, len(tasks))
|
||||
_goto(page, SPA_HOME_URL)
|
||||
page.wait_for_timeout(SPA_HYDRATION_MS)
|
||||
if _is_logged_in(page):
|
||||
logger.info("platform_collector.process_queue: ✅ 已登录态")
|
||||
else:
|
||||
_goto(page, SPA_LOGIN_URL)
|
||||
page.wait_for_timeout(SPA_HYDRATION_MS)
|
||||
if not _fill_spa_login(page):
|
||||
if not headless:
|
||||
_wait_for_manual_login(page, wait_sec=human_wait_timeout)
|
||||
else:
|
||||
raise LoginTimeoutError("process_queue headless 自动 fill 失败")
|
||||
else:
|
||||
page.wait_for_timeout(POST_LOGIN_SETTLE_MS)
|
||||
|
||||
# 强制 goto SPA_HOME + 侧边栏 hr/contracts
|
||||
for _ in range(SPA_HOME_RETRY_MAX):
|
||||
if _is_logged_in(page):
|
||||
break
|
||||
_goto(page, SPA_HOME_URL)
|
||||
page.wait_for_timeout(SPA_HOME_RETRY_DELAY_MS)
|
||||
if _fill_spa_login(page):
|
||||
page.wait_for_timeout(POST_LOGIN_SETTLE_MS)
|
||||
if not _is_logged_in(page):
|
||||
raise LoginTimeoutError("process_queue 仍未登录")
|
||||
|
||||
# 等表格
|
||||
page.wait_for_timeout(TABLE_INITIAL_WAIT_MS)
|
||||
found_table = False
|
||||
for _attempt in range(TABLE_POLL_MAX_ATTEMPTS):
|
||||
page.wait_for_timeout(TABLE_POLL_INTERVAL_MS)
|
||||
try:
|
||||
page.wait_for_selector(
|
||||
"table tbody tr", timeout=TABLE_TR_TIMEOUT_MS,
|
||||
)
|
||||
found_table = True
|
||||
break
|
||||
except Exception:
|
||||
pass
|
||||
if not found_table:
|
||||
raise TableNotFoundError("process_queue 表格未找到")
|
||||
|
||||
mat_btn_count = page.locator(
|
||||
"table tbody tr button:has-text('续聘材料包')"
|
||||
).count()
|
||||
|
||||
# 2. 逐任务处理
|
||||
for t in tasks:
|
||||
contract_id = t["contract_id"]
|
||||
task_id = t["task_id"]
|
||||
conn = get_connection()
|
||||
try:
|
||||
row_idx = _find_row_for_contract(page, contract_id)
|
||||
if row_idx is None:
|
||||
repo.mark_task_failed(conn, task_id, "contract_not_found_in_table")
|
||||
failed += 1
|
||||
details.append({"task_id": task_id, "contract_id": contract_id,
|
||||
"error": "contract_not_found_in_table"})
|
||||
conn.commit()
|
||||
continue
|
||||
materials = _download_materials_for_row(
|
||||
page, row_idx, contract_id, mat_btn_count,
|
||||
)
|
||||
from db.migrations import ensure_materials_content_schema
|
||||
ensure_materials_content_schema(conn)
|
||||
for m in materials:
|
||||
if isinstance(m, dict) and m.get("kind") == "sandbox_card":
|
||||
conn.execute(
|
||||
"""
|
||||
INSERT OR REPLACE INTO materials_content
|
||||
(contract_id, file_name, file_path, extracted_text,
|
||||
parse_method, parsed_at)
|
||||
VALUES (?, ?, ?, ?, 'sandbox_card', ?)
|
||||
""",
|
||||
(
|
||||
contract_id, m["file_name"],
|
||||
f"sandbox://hr/contracts/{contract_id}",
|
||||
m["text"],
|
||||
datetime.now().isoformat(timespec="seconds"),
|
||||
),
|
||||
)
|
||||
repo.parse_sandbox_card_performance(conn, contract_id, m["text"])
|
||||
result_path = (
|
||||
f"sandbox://hr/contracts/{contract_id}"
|
||||
if materials else "no_materials_captured"
|
||||
)
|
||||
repo.mark_task_success(conn, task_id, result_path)
|
||||
succeeded += 1
|
||||
details.append({
|
||||
"task_id": task_id,
|
||||
"contract_id": contract_id,
|
||||
"ok": True,
|
||||
"materials_count": len(materials),
|
||||
})
|
||||
conn.commit()
|
||||
except Exception as e:
|
||||
repo.mark_task_failed(conn, task_id, str(e))
|
||||
failed += 1
|
||||
details.append({
|
||||
"task_id": task_id, "contract_id": contract_id,
|
||||
"error": str(e),
|
||||
})
|
||||
conn.commit()
|
||||
logger.warning("process_queue: task=%s 失败: %s", task_id, e)
|
||||
finally:
|
||||
conn.close()
|
||||
finally:
|
||||
ctx.close()
|
||||
|
||||
return {
|
||||
"processed": len(tasks),
|
||||
"succeeded": succeeded,
|
||||
"failed": failed,
|
||||
"details": details,
|
||||
}
|
||||
|
||||
|
||||
def _error_result(reason: str, fetched: int) -> dict:
|
||||
return {
|
||||
"platform_url": PLATFORM_URL,
|
||||
|
||||
@@ -50,7 +50,7 @@ def suggest(perf_records: list[sqlite3.Row]) -> tuple[str, str]:
|
||||
|
||||
# 全部 >= 80 且逐年上升(升序排 → 严格递增)
|
||||
all_high = all(s >= PERFORMANCE_RENEW_MIN for s in scores)
|
||||
upward = all(scores[i] <= scores[i + 1] for i in range(len(scores) - 1))
|
||||
upward = all(scores[i] < scores[i + 1] for i in range(len(scores) - 1))
|
||||
if all_high and upward:
|
||||
return ("renew", f"近 3 年考核均 ≥ {PERFORMANCE_RENEW_MIN} 分且逐年上升")
|
||||
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
"""
|
||||
|
||||
SKILL_SLUG = "alert-contract-expiring-hr"
|
||||
SKILL_VERSION = "1.1.13"
|
||||
SKILL_VERSION = "1.1.26"
|
||||
LOG_LOGGER_NAME = "openclaw.skill.alert_contract_expiring_hr"
|
||||
PLATFORM_KIT_MIN_VERSION = "1.2.11"
|
||||
|
||||
@@ -48,7 +48,10 @@ PERFORMANCE_RENEW_MIN = 80 # 全部 >= 80 且逐年上升 → renew
|
||||
# ----- v1.1.6:RPA 超时常量(与 skill-template human_verification.py 对齐)-----
|
||||
# 手工登录等待(HITL fallback)
|
||||
HUMAN_WAIT_TIMEOUT = 180 # 手工登录等待超时(秒)—— skill-template 同名常量
|
||||
HUMAN_POLL_INTERVAL = 2.0 # 轮询间隔(秒)—— skill-template 同名常量
|
||||
HUMAN_POLL_INTERVAL = 5.0 # 轮询间隔(秒)—— v1.1.19 调长:避免打断用户输入
|
||||
HUMAN_PROMPT_INTERVAL = 15.0 # 剩余时间提示间隔(秒)—— v1.1.19 新增
|
||||
URL_STABLE_REQUIRED_S = 2.0 # URL 连续 N 秒不变才算稳定 —— v1.1.19 新增
|
||||
URL_STABLE_POLL_S = 0.5 # URL 稳定探测间隔(秒)
|
||||
|
||||
# Playwright 操作超时(毫秒)
|
||||
DEFAULT_PAGE_TIMEOUT_MS = 15_000 # Playwright 默认操作超时 —— skill-template 同名常量
|
||||
@@ -87,3 +90,13 @@ CARD_APPEAR_WAIT_MS = 3_000 # 「续聘材料包」click 后等卡片出
|
||||
|
||||
# dump
|
||||
DUMP_PATH_PARENT_MKDIR = True # _dump_page 是否自动创建父目录
|
||||
|
||||
# ----- v1.1.18:日志落文件 -----
|
||||
LOG_FILE_DEFAULT_NAME = "alert-contract-expiring-hr.log"
|
||||
LOG_MAX_BYTES_DEFAULT = 5 * 1024 * 1024 # 5MB
|
||||
LOG_BACKUP_COUNT_DEFAULT = 5
|
||||
|
||||
# ----- v1.1.18:材料下载队列 -----
|
||||
DOWNLOAD_QUEUE_MAX_ATTEMPTS_DEFAULT = 3
|
||||
DOWNLOAD_QUEUE_KINDS = ("platform_button", "local_upload")
|
||||
DOWNLOAD_QUEUE_STATUSES = ("pending", "running", "success", "failed")
|
||||
|
||||
@@ -1,20 +1,35 @@
|
||||
"""logging 配置:降级到 stderr,避免污染 stdout(stdout 留给结构化输出如 JSON)。
|
||||
|
||||
优先用 jiangchang_skill_core 提供的 logging;本地无该包时降级到 stderr。
|
||||
|
||||
v1.1.18:本地降级路径追加 RotatingFileHandler(按 LOG_FILE env 决定是否落文件)。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
import logging.handlers
|
||||
import os
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
from util.constants import LOG_LOGGER_NAME, SKILL_SLUG, SKILL_VERSION
|
||||
from util.constants import (
|
||||
LOG_LOGGER_NAME,
|
||||
SKILL_SLUG,
|
||||
SKILL_VERSION,
|
||||
)
|
||||
|
||||
_configured = False
|
||||
|
||||
|
||||
def setup_skill_logging() -> logging.Logger:
|
||||
"""初始化 skill logger(幂等)。"""
|
||||
"""初始化 skill logger(幂等)。
|
||||
|
||||
本地降级行为(无 jiangchang_skill_core 时):
|
||||
- 若 LOG_FILE 已设:追加 RotatingFileHandler(LOG_MAX_BYTES × LOG_BACKUP_COUNT)
|
||||
- 始终追加 StreamHandler(sys.stderr)
|
||||
- logger level 取自 LOG_LEVEL(默认 INFO)
|
||||
"""
|
||||
global _configured
|
||||
logger = logging.getLogger(LOG_LOGGER_NAME)
|
||||
if _configured:
|
||||
@@ -30,15 +45,38 @@ def setup_skill_logging() -> logging.Logger:
|
||||
except ImportError:
|
||||
pass
|
||||
|
||||
# 本地降级:基础 stderr handler,UTF-8 safe
|
||||
handler = logging.StreamHandler(sys.stderr)
|
||||
handler.setFormatter(
|
||||
logging.Formatter(
|
||||
fmt=f"%(asctime)s [{SKILL_SLUG}] %(levelname)s %(message)s",
|
||||
datefmt="%Y-%m-%d %H:%M:%S",
|
||||
)
|
||||
# logger 级别
|
||||
level_name = os.environ.get("LOG_LEVEL", "INFO").upper()
|
||||
logger.setLevel(getattr(logging, level_name, logging.INFO))
|
||||
|
||||
formatter = logging.Formatter(
|
||||
fmt=f"%(asctime)s [{SKILL_SLUG}] %(levelname)s %(message)s",
|
||||
datefmt="%Y-%m-%d %H:%M:%S",
|
||||
)
|
||||
logger.setLevel(logging.INFO)
|
||||
|
||||
# 本地降级:可选 RotatingFileHandler
|
||||
log_file_str = os.environ.get("LOG_FILE")
|
||||
if log_file_str:
|
||||
log_path = Path(log_file_str)
|
||||
if not log_path.is_absolute():
|
||||
from util.runtime_paths import get_logs_dir
|
||||
|
||||
log_path = get_logs_dir() / log_file_str
|
||||
log_path.parent.mkdir(parents=True, exist_ok=True)
|
||||
max_bytes = int(os.environ.get("LOG_MAX_BYTES", str(5 * 1024 * 1024)))
|
||||
backup_count = int(os.environ.get("LOG_BACKUP_COUNT", "5"))
|
||||
file_handler = logging.handlers.RotatingFileHandler(
|
||||
log_path,
|
||||
maxBytes=max_bytes,
|
||||
backupCount=backup_count,
|
||||
encoding="utf-8",
|
||||
)
|
||||
file_handler.setFormatter(formatter)
|
||||
logger.addHandler(file_handler)
|
||||
|
||||
# 始终保留 stderr handler
|
||||
handler = logging.StreamHandler(sys.stderr)
|
||||
handler.setFormatter(formatter)
|
||||
logger.addHandler(handler)
|
||||
logger.propagate = False
|
||||
_configured = True
|
||||
@@ -51,3 +89,19 @@ def get_skill_logger() -> logging.Logger:
|
||||
if not _configured:
|
||||
return setup_skill_logging()
|
||||
return logging.getLogger(LOG_LOGGER_NAME)
|
||||
|
||||
|
||||
def reset_for_tests() -> None:
|
||||
"""测试用:重置 _configured flag,让 setup_skill_logging 重新初始化。
|
||||
|
||||
必须在每个修改日志配置的测试 fixture 里调用,否则 env 改动不会生效。
|
||||
"""
|
||||
global _configured
|
||||
logger = logging.getLogger(LOG_LOGGER_NAME)
|
||||
for h in list(logger.handlers):
|
||||
try:
|
||||
h.close()
|
||||
except Exception:
|
||||
pass
|
||||
logger.removeHandler(h)
|
||||
_configured = False
|
||||
|
||||
@@ -66,6 +66,30 @@ def get_dispatch_dir() -> Path:
|
||||
return get_data_dir() / "dispatch"
|
||||
|
||||
|
||||
def get_logs_dir() -> Path:
|
||||
"""返回日志目录(RotatingFileHandler 默认落地点)。"""
|
||||
env_dir = os.environ.get("JIANGCHANG_LOGS_DIR")
|
||||
if env_dir:
|
||||
return Path(env_dir)
|
||||
return get_data_dir() / "logs"
|
||||
|
||||
|
||||
def get_input_dir() -> Path:
|
||||
"""返回用户主动投放的输入目录(含 contracts/*.csv + materials/*.{pdf,docx,doc})。"""
|
||||
env_dir = os.environ.get("INPUT_DIR")
|
||||
if env_dir:
|
||||
return Path(env_dir)
|
||||
return get_data_dir() / "input"
|
||||
|
||||
|
||||
def get_input_contracts_dir() -> Path:
|
||||
return get_input_dir() / "contracts"
|
||||
|
||||
|
||||
def get_input_materials_dir() -> Path:
|
||||
return get_input_dir() / "materials"
|
||||
|
||||
|
||||
def get_examples_dir() -> Path:
|
||||
"""返回样例数据目录。"""
|
||||
return get_skill_root() / "examples"
|
||||
@@ -78,5 +102,13 @@ def get_templates_dir() -> Path:
|
||||
|
||||
def ensure_dirs() -> None:
|
||||
"""确保所有需要的目录存在。"""
|
||||
for d in (get_data_dir(), get_output_dir(), get_dispatch_dir()):
|
||||
for d in (
|
||||
get_data_dir(),
|
||||
get_output_dir(),
|
||||
get_dispatch_dir(),
|
||||
get_logs_dir(),
|
||||
get_input_dir(),
|
||||
get_input_contracts_dir(),
|
||||
get_input_materials_dir(),
|
||||
):
|
||||
d.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
1157
tests/run_tests.py
1157
tests/run_tests.py
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user