Compare commits
20 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| a7baba7210 | |||
| 2e5a8d5eed | |||
| 6f958ded28 | |||
| 3bc15d1241 | |||
| c67079fb9e | |||
| dc4eecbb35 | |||
| 766d162245 | |||
| 35c20fc0f2 | |||
| 48a86e56f1 | |||
| 7136689efe | |||
| e366605299 | |||
| 707d97760f | |||
| d17804e45b | |||
| a3bd8faf87 | |||
| 30803a0834 | |||
| f3f7a9b9e5 | |||
| 79098dad5c | |||
| b267ca3266 | |||
| 87ef00835b | |||
| c7d7832749 |
13
.env.example
13
.env.example
@@ -1,4 +1,4 @@
|
|||||||
# ── 运行模式 / adapter 档位(见 references/ADAPTER.md)──
|
# ── 运行模式 / adapter 档位(见 development/ADAPTER.md)──
|
||||||
OPENCLAW_TEST_TARGET=mock # mock | simulator_rpa | real_api | real_rpa
|
OPENCLAW_TEST_TARGET=mock # mock | simulator_rpa | real_api | real_rpa
|
||||||
|
|
||||||
# ── 目标系统 ──
|
# ── 目标系统 ──
|
||||||
@@ -12,10 +12,19 @@ OPENCLAW_BROWSER_HEADLESS=0 # 0=有头(默认) 1=无头(CI)
|
|||||||
OPENCLAW_PLAYWRIGHT_STEALTH=1 # 1=开启反检测指纹(默认)
|
OPENCLAW_PLAYWRIGHT_STEALTH=1 # 1=开启反检测指纹(默认)
|
||||||
|
|
||||||
# ── 存证 ──
|
# ── 存证 ──
|
||||||
OPENCLAW_RECORD_VIDEO=1
|
OPENCLAW_RECORD_VIDEO=0 # 0=默认不录屏;1=启用录屏+字幕+旁白+背景音+MP4
|
||||||
OPENCLAW_ARTIFACTS_ON_FAILURE=1 # 1=失败截图(默认)
|
OPENCLAW_ARTIFACTS_ON_FAILURE=1 # 1=失败截图(默认)
|
||||||
|
|
||||||
# ── 节流 / 超时 ──
|
# ── 节流 / 超时 ──
|
||||||
STEP_DELAY_MIN=1.0 # 步骤间随机等待下限(秒)
|
STEP_DELAY_MIN=1.0 # 步骤间随机等待下限(秒)
|
||||||
STEP_DELAY_MAX=5.0 # 步骤间随机等待上限(秒)
|
STEP_DELAY_MAX=5.0 # 步骤间随机等待上限(秒)
|
||||||
HUMAN_WAIT_TIMEOUT=180 # 滑块/验证码/2FA 等人工超时(秒)
|
HUMAN_WAIT_TIMEOUT=180 # 滑块/验证码/2FA 等人工超时(秒)
|
||||||
|
|
||||||
|
# ── 数据目录子路径(见 development/DATA_PATHS.md;留空则用 skill 数据目录下默认子文件夹)──
|
||||||
|
# 相对路径相对于 {JIANGCHANG_DATA_ROOT}/{USER_ID}/{slug}/,不是 workspace / CWD
|
||||||
|
# SKILL_DOWNLOAD_DIR=downloads
|
||||||
|
# SKILL_IMPORT_DIR=imports
|
||||||
|
# SKILL_EXPORT_DIR=exports
|
||||||
|
# SKILL_UPLOAD_DIR=uploads
|
||||||
|
# SKILL_CACHE_DIR=cache
|
||||||
|
# SKILL_TEMP_DIR=temp
|
||||||
|
|||||||
2
.github/workflows/release_skill.yaml
vendored
2
.github/workflows/release_skill.yaml
vendored
@@ -5,7 +5,7 @@ on:
|
|||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
release:
|
release:
|
||||||
uses: admin/jiangchang-platform-kit/.github/workflows/reusable-release-skill.yaml@main
|
uses: client-jiangchang/jiangchang-platform-kit/.github/workflows/reusable-release-skill.yaml@main
|
||||||
secrets:
|
secrets:
|
||||||
PYARMOR_REG_B64: ${{ secrets.PYARMOR_REG_B64 }}
|
PYARMOR_REG_B64: ${{ secrets.PYARMOR_REG_B64 }}
|
||||||
with:
|
with:
|
||||||
|
|||||||
8
.gitignore
vendored
8
.gitignore
vendored
@@ -1,4 +1,10 @@
|
|||||||
__pycache__/
|
__pycache__/
|
||||||
*.py[cod]
|
*.py[cod]
|
||||||
.env
|
.env
|
||||||
*.env.local
|
*.env.local
|
||||||
|
.pytest_cache/
|
||||||
|
.coverage
|
||||||
|
htmlcov/
|
||||||
|
dist/
|
||||||
|
build/
|
||||||
|
*.egg-info/
|
||||||
5
.openclaw-skill-template
Normal file
5
.openclaw-skill-template
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
# 此文件仅存在于 skill-template 源仓库。
|
||||||
|
# 用 tools/scaffold_skill 创建新技能时,脚本会从目标目录删除本文件。
|
||||||
|
# 若业务技能仓库中仍存在本文件,说明未正确 scaffold,tests 会失败。
|
||||||
|
template_slug: your-skill-slug
|
||||||
|
template_version: "1.0.32"
|
||||||
141
README.md
141
README.md
@@ -1,80 +1,99 @@
|
|||||||
# 匠厂 技能开发模板(通用业务)
|
---
|
||||||
|
description: "用用户能理解的方式说明这个技能能做什么、适合什么场景,以及如何开始使用。"
|
||||||
|
---
|
||||||
|
|
||||||
这是一个**规范化的新技能模板仓库**,用于复制出新的 skill 项目;它本身**不是业务 skill**。
|
> **模板说明(复制后删除本段)**
|
||||||
|
> 本文是技能市场详情与普通用户说明,**不是** Agent 参考索引、CLI 契约或开发文档。
|
||||||
|
> frontmatter 的 `description` 必须是用户能看懂的一句话能力描述;**不得**写成 CLI、schema、references、development、契约、数据结构、开发规范或 Agent 参考索引。
|
||||||
|
> 正文只回答用户关心的问题,**不要**把 Playwright、DOM、Python、RPA、references、development、schema 等作为面向用户的主说明内容;若业务上必须出现技术词,须用用户能理解的方式解释,且不能喧宾夺主。
|
||||||
|
|
||||||
## 模板目标
|
# 【技能名称】
|
||||||
|
|
||||||
- 对齐当前规范 skill 的目录结构:`assets/`、`references/`、`scripts/`、`tests/`、`evals/`
|
> 用一两句话说明这个技能帮助用户完成什么工作。
|
||||||
- 对齐当前规范脚手架分层:`scripts/cli`、`scripts/db`、`scripts/service`、`scripts/util`;公共能力从共享 runtime 的 `jiangchang-platform-kit` 引用
|
> 这里不要解释技术实现,要从用户的业务目标出发描述价值。
|
||||||
- 提供最小可运行入口:`python scripts/main.py health` / `config-path` / `version`,以及通用任务命令骨架 `run` / `logs` / `log-get`
|
|
||||||
- 内置 **配置 bootstrap**:`.env.example` 首次落盘到 `{DATA_ROOT}/{USER_ID}/{slug}/.env`,升级后合并缺失 key(见 `scripts/util/config_bootstrap.py`)
|
|
||||||
- 内置 **隔离测试体系**:`IsolatedDataRoot`、双键环境、`OPENCLAW_TEST_TARGET` 档位与 adapter profile 策略(详见 [`references/TESTING.md`](references/TESTING.md))
|
|
||||||
- 让新技能从一开始就按规范落地,不再沿用旧模板的 `docs/`、`optional/`、`skill_main.py` 结构
|
|
||||||
|
|
||||||
## 新技能使用步骤
|
---
|
||||||
|
|
||||||
1. 复制本目录为新的 skill 仓库。
|
## 这个技能能帮你做什么
|
||||||
2. 全局替换 `your-skill-slug`、`your-platform-key`、`您的技能显示名称` 等占位内容。
|
|
||||||
3. 修改 `SKILL.md`、`references/` 和 `scripts/util/constants.py`。
|
|
||||||
4. 在 `scripts/service/task_service.py` 中实现真实业务编排与外部系统对接。
|
|
||||||
5. 运行 `python tests/run_tests.py -v`,再执行 `python scripts/main.py health`、`version` 做最小验证。
|
|
||||||
|
|
||||||
如果你的技能在平台里默认是非公开的(`access_scope = 0`),建议在 `SKILL.md` 的 `metadata.openclaw.developer_ids` 中填写开发者用户 ID 列表。这样在正式发布后,平台会为这些开发者补可见权限,避免「技能已上架但开发者自己在市场中看不到」。
|
用 3 到 6 条业务能力说明技能价值,例如:
|
||||||
|
|
||||||
开发教程入口:
|
- 【自动整理/查询/生成/同步/核对/提交】某类业务数据
|
||||||
|
- 帮你减少重复操作,降低人工录入或核对成本
|
||||||
|
- 在任务完成后给出清晰的处理结果、失败原因或后续建议
|
||||||
|
|
||||||
- <a href="references/REQUIREMENTS.md" target="_blank" rel="noopener noreferrer">需求文档模板</a>:给技术人员编写和查看研发需求的标准模板
|
---
|
||||||
- <a href="references/DEVELOPMENT.md" target="_blank" rel="noopener noreferrer">开发教程</a>:给技术人员的完整开发步骤说明
|
|
||||||
- <a href="references/TESTING.md" target="_blank" rel="noopener noreferrer">测试开发指南</a>:默认套件、隔离数据根、档位开关与 FakeAdapter 怎么用
|
|
||||||
|
|
||||||
## 目录说明
|
## 适合什么场景
|
||||||
|
|
||||||
| 路径 | 用途 |
|
列出典型使用场景。请使用用户熟悉的业务语言,例如:
|
||||||
|------|------|
|
|
||||||
| `SKILL.md` | 技能清单与触发说明模板 |
|
|
||||||
| `assets/` | 示例输出与轻量 schema |
|
|
||||||
| `references/` | 面向用户与编排的文档模板 |
|
|
||||||
| `scripts/` | 规范分层后的代码骨架 |
|
|
||||||
| `tests/` | 单元测试与分层测试范式(默认根目录 unittest) |
|
|
||||||
| `evals/` | 人工/半自动评估材料 |
|
|
||||||
| `requirements.txt` | 本技能 Python 三方依赖声明(由匠厂宿主安装到共享 runtime) |
|
|
||||||
| `.github/workflows/release_skill.yaml` | 标准发布工作流 |
|
|
||||||
| `release.ps1` | 对齐现有 skill 的发布脚本入口 |
|
|
||||||
|
|
||||||
## 最小命令
|
- 每天需要重复处理同一类订单、单据、报表或客户资料
|
||||||
|
- 需要从多个系统中查询信息并汇总结果
|
||||||
|
- 需要按固定规则检查数据是否完整、是否异常
|
||||||
|
- 需要把处理结果保存为文件、记录或任务报告
|
||||||
|
|
||||||
```bash
|
---
|
||||||
python scripts/main.py health
|
|
||||||
python scripts/main.py version
|
|
||||||
```
|
|
||||||
|
|
||||||
## 注意
|
## 使用前需要准备什么
|
||||||
|
|
||||||
- 不要再往模板里引入旧式 `docs/` 或 `optional/` 目录。
|
说明用户在使用前要准备的东西,例如:
|
||||||
- 新技能若不需要某些目录,也建议先保留结构,再按实际业务填充内容。
|
|
||||||
- `metadata.openclaw.developer_ids` 是发布元数据,不是用户展示文案;用于非公开技能的开发者默认可见授权。
|
|
||||||
- **发起 `release.ps1` 之前**,务必确认 `python tests/run_tests.py -v` 已全部通过。
|
|
||||||
|
|
||||||
## Python 依赖(requirements.txt)
|
- 相关账号已登录,且具备查询、导出或提交权限
|
||||||
|
- 需要处理的文件、时间范围、订单号、客户名称、项目名称等信息
|
||||||
|
- 如果涉及外部系统,请确认网络、账号、验证码或审批流程可正常使用
|
||||||
|
|
||||||
- `jiangchang-platform-kit`、`playwright`、config、runtime diagnostics、RPA 公共能力等由**宿主共享 runtime** 提供;`jiangchang_skill_core` **不在技能仓库内复制**。
|
---
|
||||||
- 新技能**不得 vendored** `scripts/jiangchang_skill_core/`;复制模板后若发现该目录,应删除并改为依赖共享 venv。
|
|
||||||
- 在技能**根目录**维护 `requirements.txt`,**只声明技能特有** Python 三方包;**不要**重复声明 `jiangchang-platform-kit` 或 `playwright`。
|
|
||||||
- `SKILL.md` 的 `metadata.openclaw.platform_kit_min_version`(当前 `1.0.13`)是运行契约/兼容性声明,供宿主安装与启用时校验,**不是** pip 依赖声明。`jiangchang-platform-kit>=1.0.13` 是当前 RPA 视频/旁白/背景音乐/前后缓冲等标准能力的最低版本。
|
|
||||||
- 匠厂宿主在**安装或更新**技能后,会将技能 `requirements.txt` 中的依赖安装到共享 Python runtime:
|
|
||||||
`{JIANGCHANG_DATA_ROOT}/python-runtime/.venv`(Windows 示例:`D:\jiangchang-data\python-runtime\.venv`)。
|
|
||||||
- **Skill 业务代码不得自动 `pip install`**;缺依赖时应在 `health` / preflight 中给出清晰错误。
|
|
||||||
- **系统级组件**(如 Microsoft Visual C++ Redistributable)**不要**写进 `requirements.txt`;仅在 `health` / preflight 中提示用户自行安装。
|
|
||||||
- 独立本地开发环境若缺少公共包,可手动安装;**生产/宿主运行**由共享 runtime 负责,无需在技能内 `pip install playwright`。
|
|
||||||
|
|
||||||
## 公共能力边界
|
## 你可以这样告诉 Agent
|
||||||
|
|
||||||
以下能力均由 `jiangchang-platform-kit`(共享 runtime 的 `jiangchang_skill_core`)提供,技能只写业务逻辑与薄封装,**不要**在技能内复制实现:
|
用自然语言示例展示用户可以怎么发起任务,例如:
|
||||||
|
|
||||||
- `config` — `.env` 三级优先级读取与首次落盘
|
> “帮我查询今天的【业务对象】,整理成表格。”
|
||||||
- `runtime_env` — 数据根、用户 ID、兄弟技能根解析
|
>
|
||||||
- `unified_logging` — 统一日志与 trace
|
> “帮我核对这些【数据/单据】是否有异常,并告诉我问题在哪里。”
|
||||||
- `rpa` — 浏览器/桌面/手机 RPA 共享库
|
>
|
||||||
- `media_assets` — 共享媒体资源探测与路径解析
|
> “帮我把【时间范围】内的【业务数据】导出并生成处理报告。”
|
||||||
- `video_session` — RPA 录屏成片(ffmpeg、字幕、背景音乐)
|
>
|
||||||
- `runtime_diagnostics` — `health` 统一 runtime 诊断
|
> “帮我根据这个文件里的内容,逐条完成【业务动作】。”
|
||||||
|
|
||||||
|
> 注意:示例应是自然语言,不要写命令行命令。
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 执行后你会得到什么
|
||||||
|
|
||||||
|
说明用户会看到什么结果,例如:
|
||||||
|
|
||||||
|
- 任务是否成功完成
|
||||||
|
- 处理了多少条数据
|
||||||
|
- 哪些内容成功、哪些失败
|
||||||
|
- 失败原因和建议处理方式
|
||||||
|
- 生成的文件、表格、报告或记录
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 注意事项
|
||||||
|
|
||||||
|
用用户能理解的方式说明边界和风险,例如:
|
||||||
|
|
||||||
|
- 请确认输入范围准确,避免处理错数据
|
||||||
|
- 涉及提交、付款、删除、审批等高风险动作时,技能应在执行前请求确认
|
||||||
|
- 如果外部系统登录失效、权限不足或页面变化,任务可能需要用户介入
|
||||||
|
- 不建议在未核对数据的情况下直接执行批量操作
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 常见问题
|
||||||
|
|
||||||
|
### 为什么任务没有查到数据?
|
||||||
|
|
||||||
|
> 可能是时间范围、关键词、账号权限或系统筛选条件不正确。请先确认输入信息是否准确。
|
||||||
|
|
||||||
|
### 为什么任务执行到一半需要我操作?
|
||||||
|
|
||||||
|
> 可能遇到了登录、验证码、权限确认、二次审批或系统异常。请根据 Agent 的提示完成必要操作。
|
||||||
|
|
||||||
|
### 结果文件在哪里查看?
|
||||||
|
|
||||||
|
> 请根据 Agent 返回的结果说明查看生成文件、任务记录或处理报告。
|
||||||
|
|||||||
91
SKILL.md
91
SKILL.md
@@ -1,12 +1,12 @@
|
|||||||
---
|
---
|
||||||
name: 技能开发模板(通用业务版)
|
name: 技能开发模板(通用业务版)
|
||||||
description: "OpenClaw 通用业务技能开发模板,覆盖任务执行、日志记录、外部系统对接、隔离测试等典型能力。复制后请按 references/DEVELOPMENT.md 完成定制。"
|
description: "OpenClaw 通用业务技能开发模板,供复制后定制新业务 skill。定制步骤见 development/DEVELOPMENT.md。"
|
||||||
version: 1.0.14
|
version: 1.0.40
|
||||||
author: 深圳匠厂科技有限公司
|
author: 深圳匠厂科技有限公司
|
||||||
metadata:
|
metadata:
|
||||||
openclaw:
|
openclaw:
|
||||||
slug: your-skill-slug
|
slug: your-skill-slug
|
||||||
platform_kit_min_version: "1.0.13"
|
platform_kit_min_version: "1.2.0"
|
||||||
emoji: "📦"
|
emoji: "📦"
|
||||||
category: "通用"
|
category: "通用"
|
||||||
developer_ids:
|
developer_ids:
|
||||||
@@ -18,19 +18,52 @@ allowed-tools:
|
|||||||
|
|
||||||
# 技能开发模板(skill-template)
|
# 技能开发模板(skill-template)
|
||||||
|
|
||||||
这是一个**用于复制的新技能模板**,不是业务技能本身。新建技能时,应复制本仓库结构,再把占位内容替换成你的真实业务实现。
|
这是一个**用于复制的新技能模板**,不是业务 skill 本身。复制后请替换占位内容,实现你的真实业务逻辑。
|
||||||
|
|
||||||
## 模板使用方式
|
## Agent / 宿主编排(复制为业务技能后必改)
|
||||||
|
|
||||||
1. 复制目录为你的新 skill 仓库。
|
- **长耗时 / RPA / 浏览器任务**:必须通过宿主 **Skill Action**(`run_skill_action`)调用已声明在 `assets/actions.json` 中的 action;`executionProfile` 须为 `"async"`,进度见**任务中心** / Skill Run Card。
|
||||||
2. 全局替换 `your-skill-slug`、`技能开发模板(通用业务版)` 等占位词。
|
- **禁止**对长任务使用 `exec python …/scripts/main.py run` 或 `process poll` 干等 CLI 返回。
|
||||||
3. 按 `references/CLI.md`、`scripts/` 分层与 `README.md` 的说明补业务逻辑。
|
- 只读查询(`health` / `stats` 等)可用 `executionProfile: "sync"`,立即返回结果。
|
||||||
|
- 批量处理用参数化 `pickCount` / `--pick N` 顺序消费队列,**不要并发**同一 Profile;契约见 `development/SKILL_ACTION_RUNTIME.md`、`references/ACTIONS.md`。
|
||||||
|
- 模板源仓库本身仅示范 `health` / `version` / `config-path`(sync);定制 RPA 技能后须自行增加 async action。
|
||||||
|
|
||||||
|
## 面向用户问答(LLM 规则)
|
||||||
|
|
||||||
|
- 本文(`SKILL.md`)是 LLM / OpenClaw 平台读取的技能入口,**不是**用户市场说明。
|
||||||
|
- 根目录 `README.md` 是面向普通用户的说明。
|
||||||
|
|
||||||
|
当用户询问以下问题时,**必须优先读取**根目录 `README.md`,并用用户能理解的业务语言回答:
|
||||||
|
|
||||||
|
- 这个技能是做什么的
|
||||||
|
- 这个技能怎么用
|
||||||
|
- 适合什么场景
|
||||||
|
- 使用前要准备什么
|
||||||
|
- 执行后会得到什么
|
||||||
|
- 能不能帮我完成某个业务任务
|
||||||
|
|
||||||
|
- **不要**把 `SKILL.md`、`references/`、`development/` 中的技术细节直接作为用户回答。
|
||||||
|
- **只有当**用户明确询问命令、参数、输出结构、开发、调试、集成或排错时,才读取 `references/` 或 `development/`。
|
||||||
|
- 若 `README.md` 与 `SKILL.md` / `references` 表述冲突:对用户展示与市场说明以 `README.md` 为准;对执行契约、CLI、schema、运行约束以 `SKILL.md` / `references` 为准。
|
||||||
|
- 回答用户时**不要**暴露 Playwright、DOM、Python、RPA 等实现细节,除非用户明确询问技术实现。
|
||||||
|
- 用户未问技术实现时,**不要**讨论 slug 命名规则;开发者问命名或 slug 时,读取 `development/NAMING.md`。
|
||||||
|
|
||||||
|
## 文档分工
|
||||||
|
|
||||||
|
| 文档 | 读者 | 用途 |
|
||||||
|
|------|------|------|
|
||||||
|
| 根目录 `README.md` | 普通用户 | 技能市场详情页说明(`metadata.readme_md` 主来源) |
|
||||||
|
| `SKILL.md`(本文) | LLM / OpenClaw 平台 | 技能入口、触发与运行契约摘要 |
|
||||||
|
| `references/` | Agent 编排/调用 | 渐进式加载:CLI 契约、字段 schema 等 |
|
||||||
|
| `development/` | 开发者 / AI 编程代理 | 需求、开发教程、测试、技术规范 |
|
||||||
|
|
||||||
## 目录约定
|
## 目录约定
|
||||||
|
|
||||||
- 根目录结构参考现有规范技能:`assets/`、`references/`、`scripts/`、`tests/`、`evals/`。
|
复制后建议保留:
|
||||||
- CLI 入口固定为 `scripts/main.py`。
|
|
||||||
- 业务逻辑按 `cli / db / service / util` 分层;公共能力从 `jiangchang-platform-kit` 提供的 `jiangchang_skill_core` 引用。
|
- `assets/`、`development/`、`evals/`、`references/`、`scripts/`、`tests/`
|
||||||
|
- CLI 入口固定为 `scripts/main.py`
|
||||||
|
- 业务逻辑按 `cli / db / service / util` 分层
|
||||||
|
|
||||||
## 最小命令
|
## 最小命令
|
||||||
|
|
||||||
@@ -40,23 +73,39 @@ python {baseDir}/scripts/main.py config-path
|
|||||||
python {baseDir}/scripts/main.py version
|
python {baseDir}/scripts/main.py version
|
||||||
```
|
```
|
||||||
|
|
||||||
配置:仓库 `.env.example` 为模板;用户 `.env` 在 `{CLAW_DATA_ROOT}/{CLAW_USER_ID}/{slug}/.env`,启动时自动 bootstrap。优先级:进程环境变量 > 用户 `.env` > `.env.example`。
|
配置:仓库 `.env.example` 为模板;用户 `.env` 在 `{JIANGCHANG_DATA_ROOT}/{JIANGCHANG_USER_ID}/{slug}/.env`,启动时自动 bootstrap。优先级:进程环境变量 > 用户 `.env` > `.env.example`。
|
||||||
|
|
||||||
## 运行依赖
|
## 运行依赖
|
||||||
|
|
||||||
- Python 运行环境由匠厂宿主注入**共享 runtime**:`{JIANGCHANG_DATA_ROOT}/python-runtime/.venv`。
|
- Python 运行环境由匠厂宿主注入**共享 runtime**:`{JIANGCHANG_DATA_ROOT}/python-runtime/.venv`。
|
||||||
- 公共能力来自共享 runtime 安装的 `jiangchang-platform-kit>=1.0.13`(`jiangchang_skill_core` 包);**新技能不得**在仓库内保留 `scripts/jiangchang_skill_core/` 副本。
|
- 公共能力来自共享 runtime 安装的 `jiangchang-platform-kit>=1.2.0`(`jiangchang_skill_core` 包);**不要 vendor** `scripts/jiangchang_skill_core/`,新技能不得在仓库内保留该目录副本。
|
||||||
- config、logging、runtime_env、rpa、media-assets、video_session、runtime_diagnostics 等均从共享 venv 的 `jiangchang_skill_core` import,而非技能目录副本。
|
- config、logging、runtime_env、rpa、media-assets、video_session、runtime_diagnostics 等均从共享 venv 的 `jiangchang_skill_core` import,而非技能目录副本。
|
||||||
- 根目录 `requirements.txt` **只声明技能特有** Python 三方依赖;`jiangchang-platform-kit`、`playwright` 等公共能力由宿主共享 runtime 提供,**不要**写入技能 requirements。宿主安装/更新技能后会将技能 requirements 安装到共享 venv。
|
- 根目录 `requirements.txt` **只声明技能特有** Python 三方依赖;`jiangchang-platform-kit`、`playwright` 等公共能力由宿主共享 runtime 提供,**不要**写入技能 requirements。
|
||||||
- `metadata.openclaw.platform_kit_min_version`(当前 `1.0.13`)是运行契约/兼容性声明,供宿主安装与启用时校验,**不是**技能 pip 依赖声明。`jiangchang-platform-kit>=1.0.13` 是当前 RPA 视频/旁白/背景音乐/前后缓冲等标准能力的最低版本。
|
- `metadata.openclaw.platform_kit_min_version`(当前 `1.2.0`)是运行契约/兼容性声明,供宿主安装与启用时校验,**不是**技能 pip 依赖声明。
|
||||||
- Skill 代码**不要**自行 `pip install`;系统级依赖(如 VC++ Runtime)仅在 `health` / preflight 中提示用户安装。
|
- Skill 代码**不要**自行 `pip install`;系统级依赖(如 VC++ Runtime)仅在 `health` / preflight 中提示用户安装。
|
||||||
- `health` 使用 `collect_runtime_diagnostics` 输出统一 runtime 诊断(只读,不下载/修复 media-assets)。
|
- `health` 使用 `collect_runtime_diagnostics` 输出统一 runtime 诊断(只读,不下载/修复 media-assets)。
|
||||||
|
|
||||||
## 重要说明
|
## 定制入口
|
||||||
|
|
||||||
- 复制后请同步修改 `scripts/util/constants.py` 中的 `SKILL_SLUG` / `SKILL_VERSION`。
|
1. 从本模板创建新业务技能时,**不得保留模板 `.git`**;优先使用 [`tools/scaffold_skill.ps1`](tools/scaffold_skill.ps1)(见 [`tools/README.md`](tools/README.md))。
|
||||||
- 如技能无需持久化,可保留 `db/` 目录但不主动调用。
|
2. 业务技能仓库中**不得**存在 `.openclaw-skill-template`(仅 template 源仓库保留)。
|
||||||
- `metadata.openclaw.developer_ids` 用于声明技能发布后的默认开发者可见用户 ID 列表。
|
3. 复制目录为你的新 skill 仓库,全局替换 `your-skill-slug` 等占位词。
|
||||||
- 当技能在平台中 `access_scope = 0`(不公开)时,任务执行流程会把 `developer_ids` 中的用户自动补写到 `skill_user_access`,使这些开发者仍可在技能市场中查看该技能。
|
4. 按 `development/DEVELOPMENT.md` 完成开发与文档定制。
|
||||||
|
5. 用户市场说明写入根 `README.md`;Agent 调用契约见 `references/CLI.md`、`references/SCHEMA.md`、`references/ACTIONS.md`;长任务 / 任务中心见 `development/SKILL_ACTION_RUNTIME.md`。
|
||||||
|
6. 同步修改 `scripts/util/constants.py` 中的 `SKILL_SLUG` / `SKILL_VERSION`。
|
||||||
|
7. 本地 SQLite 的中文表名/字段名、标准时间字段与字段顺序规范见 `references/SCHEMA.md`(元数据表 `_jiangchang_*` + `CREATE TABLE` 物理顺序)。
|
||||||
|
|
||||||
|
## 技能命名与 slug
|
||||||
|
|
||||||
|
- 标准格式:`{verb}-{noun-phrase}-{platform}`(详见 `development/NAMING.md`)。
|
||||||
|
- slug / repo / 目录 / `SKILL_SLUG` / DB 文件名:**英文** kebab-case;`name`、`description`、根 `README.md`:**中文**。
|
||||||
|
- 复制为新业务技能前**必须**阅读 `development/NAMING.md`。
|
||||||
|
- `name`(frontmatter):**用户可见的中文业务名称**;`description` 是说明,不是名称。
|
||||||
|
- `metadata.openclaw.slug`:**英文 kebab-case** 机器标识;目录名、默认 `{slug}.db` 使用 slug,不使用中文。
|
||||||
|
- 复制后全局替换 `your-skill-slug` 与 `your_skill_slug`(脚手架会自动处理);正式技能不得保留英文占位 `name` 或 slug。
|
||||||
|
|
||||||
|
## 平台元数据
|
||||||
|
|
||||||
|
- `metadata.openclaw.developer_ids`:技能发布后的默认开发者可见用户 ID 列表。
|
||||||
|
- 当 `access_scope = 0`(不公开)时,平台会把 `developer_ids` 中的用户自动补写到 `skill_user_access`。
|
||||||
- `developer_ids` 建议写为正整数数组;第一个 ID 会作为主开发者同步到 `skills.developer_id`。
|
- `developer_ids` 建议写为正整数数组;第一个 ID 会作为主开发者同步到 `skills.developer_id`。
|
||||||
- 面向用户与编排的文档写在 `references/`,不要再新增旧式 `docs/` / `optional/` 结构。
|
|
||||||
|
|||||||
@@ -1,6 +1,11 @@
|
|||||||
# assets
|
# assets
|
||||||
|
|
||||||
|
- `actions.json`:Skill Action manifest 最小示例(**可选**;不需要宿主入口时可删除)。模板要求每个 action 显式声明 `executionProfile`(`sync` \| `async`)。
|
||||||
- `examples/`:CLI 成功输出形状示例(虚构路径与数据)。
|
- `examples/`:CLI 成功输出形状示例(虚构路径与数据)。
|
||||||
- `schemas/`:轻量 JSON Schema 示例(`additionalProperties: true` 允许扩展字段)。
|
- `schemas/`:轻量 JSON Schema(`skill-actions.schema.json` 为**新技能严格规范**;`task-log-record.schema.json` 等)。
|
||||||
|
|
||||||
面向编排与用户文档见 `references/README.md`、`references/CLI.md`、`RUNTIME.md`、`SCHEMA.md`。
|
`skill-actions.schema.json` 使用 `additionalProperties: false` 表达模板推荐契约;宿主运行时可能更宽松以兼容历史 manifest。`row`/`batch` placements 与 `bind`/`concurrency`/`locks` 为预留能力,当前模板示例不得使用。RPA/长耗时 action 必须 `executionProfile: "async"`,见 [`development/SKILL_ACTION_RUNTIME.md`](../development/SKILL_ACTION_RUNTIME.md)。
|
||||||
|
|
||||||
|
- 用户市场说明见根目录 [`README.md`](../README.md)
|
||||||
|
- Agent 调用/编排参考见 [`references/`](../references/)(含 [`ACTIONS.md`](../references/ACTIONS.md))
|
||||||
|
- 开发规范见 [`development/`](../development/)
|
||||||
|
|||||||
42
assets/actions.json
Normal file
42
assets/actions.json
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
{
|
||||||
|
"schemaVersion": 1,
|
||||||
|
"skill": "your-skill-slug",
|
||||||
|
"actions": [
|
||||||
|
{
|
||||||
|
"id": "health",
|
||||||
|
"label": "运行检查",
|
||||||
|
"description": "检查技能运行环境并返回结构化结果,不启动浏览器。",
|
||||||
|
"placements": ["skill-detail", "agent"],
|
||||||
|
"executionProfile": "sync",
|
||||||
|
"entrypoint": {
|
||||||
|
"type": "cli",
|
||||||
|
"command": "health",
|
||||||
|
"args": []
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": "version",
|
||||||
|
"label": "查看版本",
|
||||||
|
"description": "返回技能版本和机器标识。",
|
||||||
|
"placements": ["skill-detail"],
|
||||||
|
"executionProfile": "sync",
|
||||||
|
"entrypoint": {
|
||||||
|
"type": "cli",
|
||||||
|
"command": "version",
|
||||||
|
"args": []
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": "config-path",
|
||||||
|
"label": "查看配置位置",
|
||||||
|
"description": "返回技能配置文件位置。",
|
||||||
|
"placements": ["skill-detail"],
|
||||||
|
"executionProfile": "sync",
|
||||||
|
"entrypoint": {
|
||||||
|
"type": "cli",
|
||||||
|
"command": "config-path",
|
||||||
|
"args": []
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
13
assets/naming/platforms.txt
Normal file
13
assets/naming/platforms.txt
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
amazon
|
||||||
|
shopee
|
||||||
|
xinghang
|
||||||
|
alibaba
|
||||||
|
kingdee
|
||||||
|
icbc
|
||||||
|
pingpong
|
||||||
|
worldfirst
|
||||||
|
paypal
|
||||||
|
lianlian
|
||||||
|
single-window
|
||||||
|
export-rebate
|
||||||
|
etax
|
||||||
4
assets/naming/scopes.txt
Normal file
4
assets/naming/scopes.txt
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
all
|
||||||
|
batch
|
||||||
|
multi
|
||||||
|
default
|
||||||
25
assets/naming/verbs.txt
Normal file
25
assets/naming/verbs.txt
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
scrape
|
||||||
|
receive
|
||||||
|
download
|
||||||
|
export
|
||||||
|
add
|
||||||
|
fill
|
||||||
|
enter
|
||||||
|
bind
|
||||||
|
withdraw
|
||||||
|
reconcile
|
||||||
|
verify
|
||||||
|
submit
|
||||||
|
query
|
||||||
|
process
|
||||||
|
reply
|
||||||
|
generate
|
||||||
|
draft
|
||||||
|
review
|
||||||
|
confirm
|
||||||
|
track
|
||||||
|
upload
|
||||||
|
match
|
||||||
|
triage
|
||||||
|
ship
|
||||||
|
reprice
|
||||||
133
assets/schemas/skill-actions.schema.json
Normal file
133
assets/schemas/skill-actions.schema.json
Normal file
@@ -0,0 +1,133 @@
|
|||||||
|
{
|
||||||
|
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||||
|
"$id": "https://openclaw.local/skill-template/skill-actions.schema.json",
|
||||||
|
"title": "SkillActionManifest",
|
||||||
|
"description": "匠厂 skill-template 对新技能的严格 Action manifest 规范(schemaVersion 1)。additionalProperties=false 表示模板推荐契约;宿主运行时可能为兼容历史 manifest 接受部分缺省字段,但新技能不应依赖缺省行为。",
|
||||||
|
"type": "object",
|
||||||
|
"required": ["schemaVersion", "skill", "actions"],
|
||||||
|
"additionalProperties": false,
|
||||||
|
"properties": {
|
||||||
|
"schemaVersion": {
|
||||||
|
"type": "integer",
|
||||||
|
"const": 1
|
||||||
|
},
|
||||||
|
"skill": {
|
||||||
|
"type": "string",
|
||||||
|
"pattern": "^[a-z0-9]+(-[a-z0-9]+)*$",
|
||||||
|
"description": "与 SKILL.md metadata.openclaw.slug 一致"
|
||||||
|
},
|
||||||
|
"actions": {
|
||||||
|
"type": "array",
|
||||||
|
"minItems": 1,
|
||||||
|
"items": { "$ref": "#/$defs/action" }
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"$defs": {
|
||||||
|
"placement": {
|
||||||
|
"type": "string",
|
||||||
|
"enum": ["toolbar", "row", "batch", "cron", "agent", "skill-detail"],
|
||||||
|
"description": "模板 Phase 1 稳定支持 toolbar/cron/agent/skill-detail;row/batch 为预留入口,新技能示例不得使用"
|
||||||
|
},
|
||||||
|
"scalarArg": {
|
||||||
|
"type": ["string", "number", "boolean"]
|
||||||
|
},
|
||||||
|
"inputProperty": {
|
||||||
|
"type": "object",
|
||||||
|
"required": ["type"],
|
||||||
|
"additionalProperties": true,
|
||||||
|
"properties": {
|
||||||
|
"type": {
|
||||||
|
"type": "string",
|
||||||
|
"enum": ["string", "number", "boolean", "object"],
|
||||||
|
"description": "模板 Phase 1 仅推荐 string/number/boolean;根 inputSchema.type=object 的 properties 使用基础标量类型"
|
||||||
|
},
|
||||||
|
"title": { "type": "string" },
|
||||||
|
"description": { "type": "string" },
|
||||||
|
"default": {},
|
||||||
|
"enum": {
|
||||||
|
"type": "array",
|
||||||
|
"minItems": 1
|
||||||
|
},
|
||||||
|
"sensitive": { "type": "boolean" }
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"inputSchema": {
|
||||||
|
"type": "object",
|
||||||
|
"required": ["type", "properties"],
|
||||||
|
"additionalProperties": false,
|
||||||
|
"properties": {
|
||||||
|
"type": { "const": "object" },
|
||||||
|
"required": {
|
||||||
|
"type": "array",
|
||||||
|
"items": { "type": "string" }
|
||||||
|
},
|
||||||
|
"properties": {
|
||||||
|
"type": "object",
|
||||||
|
"additionalProperties": { "$ref": "#/$defs/inputProperty" }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"entrypoint": {
|
||||||
|
"type": "object",
|
||||||
|
"required": ["type", "command", "args"],
|
||||||
|
"additionalProperties": false,
|
||||||
|
"properties": {
|
||||||
|
"type": { "const": "cli" },
|
||||||
|
"command": {
|
||||||
|
"type": "string",
|
||||||
|
"minLength": 1
|
||||||
|
},
|
||||||
|
"args": {
|
||||||
|
"type": "array",
|
||||||
|
"items": { "$ref": "#/$defs/scalarArg" },
|
||||||
|
"description": "仅允许 string/number/boolean 标量数组"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"confirmation": {
|
||||||
|
"type": "object",
|
||||||
|
"required": ["message"],
|
||||||
|
"additionalProperties": false,
|
||||||
|
"properties": {
|
||||||
|
"message": {
|
||||||
|
"type": "string",
|
||||||
|
"minLength": 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"action": {
|
||||||
|
"type": "object",
|
||||||
|
"required": ["id", "label", "description", "placements", "entrypoint"],
|
||||||
|
"additionalProperties": false,
|
||||||
|
"properties": {
|
||||||
|
"id": {
|
||||||
|
"type": "string",
|
||||||
|
"pattern": "^[a-z0-9]+(?:-[a-z0-9]+)*$",
|
||||||
|
"minLength": 1
|
||||||
|
},
|
||||||
|
"label": {
|
||||||
|
"type": "string",
|
||||||
|
"minLength": 1
|
||||||
|
},
|
||||||
|
"description": {
|
||||||
|
"type": "string",
|
||||||
|
"minLength": 1
|
||||||
|
},
|
||||||
|
"placements": {
|
||||||
|
"type": "array",
|
||||||
|
"minItems": 1,
|
||||||
|
"items": { "$ref": "#/$defs/placement" }
|
||||||
|
},
|
||||||
|
"entrypoint": { "$ref": "#/$defs/entrypoint" },
|
||||||
|
"inputSchema": { "$ref": "#/$defs/inputSchema" },
|
||||||
|
"confirmation": { "$ref": "#/$defs/confirmation" },
|
||||||
|
"executionProfile": {
|
||||||
|
"type": "string",
|
||||||
|
"enum": ["sync", "async"],
|
||||||
|
"description": "sync=宿主内联等待结果(约 30s);async=后台 Job 进任务中心。模板要求显式写出;RPA/长耗时必须 async。"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"description": "模板 Phase 1 不包含 bind/concurrency/locks 等预留能力字段;宿主类型可能定义这些字段,但新技能不得依赖"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -17,8 +17,8 @@
|
|||||||
|------|------|----------|
|
|------|------|----------|
|
||||||
| **`mock`** | 纯内存或 fixture,**默认单测/CI** | 模板 `.env.example` 默认 `OPENCLAW_TEST_TARGET=mock` |
|
| **`mock`** | 纯内存或 fixture,**默认单测/CI** | 模板 `.env.example` 默认 `OPENCLAW_TEST_TARGET=mock` |
|
||||||
| **`simulator_rpa`** | 仿真站点或桌面仿真,可半集成 | 开发联调可选 |
|
| **`simulator_rpa`** | 仿真站点或桌面仿真,可半集成 | 开发联调可选 |
|
||||||
| **`real_api`** | 真实 API | **必须** `ALLOW_REAL_API=1` |
|
| **`real_api`** | 真实 API | 生产 / 集成测试显式设 `OPENCLAW_TEST_TARGET=real_api` |
|
||||||
| **`real_rpa`** | 真实浏览器/真实系统 | **必须** `ALLOW_REAL_RPA=1` |
|
| **`real_rpa`** | 真实浏览器/真实系统 | 生产 / 集成测试显式设 `OPENCLAW_TEST_TARGET=real_rpa` |
|
||||||
|
|
||||||
- **mock**:纯离线、不联网,给单测 / CI / 开发自测,**保证可重复**。
|
- **mock**:纯离线、不联网,给单测 / CI / 开发自测,**保证可重复**。
|
||||||
- **simulator_rpa**:操作仿真平台(如 `sandbox.jc2009.com`),跑端到端流程但不碰生产。
|
- **simulator_rpa**:操作仿真平台(如 `sandbox.jc2009.com`),跑端到端流程但不碰生产。
|
||||||
@@ -27,17 +27,7 @@
|
|||||||
|
|
||||||
> 推荐优先级:**real_api > simulator_rpa > real_rpa**,mock 永远保留做 CI。
|
> 推荐优先级:**real_api > simulator_rpa > real_rpa**,mock 永远保留做 CI。
|
||||||
|
|
||||||
## 权限开关
|
配置读取见 `CONFIG.md`:**bootstrap 之后业务代码只通过 `config.get*()` 读 `OPENCLAW_TEST_TARGET` 等项**(进程 env > 用户 `.env` > `.env.example`)。
|
||||||
|
|
||||||
未显式授权时**不得**落到真实 API/RPA:
|
|
||||||
|
|
||||||
| 变量 | 作用 |
|
|
||||||
|------|------|
|
|
||||||
| `ALLOW_REAL_API=1` | 允许 `real_api` 访问真实 HTTP |
|
|
||||||
| `ALLOW_REAL_RPA=1` | 允许 `real_rpa` 驱动真实浏览器/RPA |
|
|
||||||
| `ALLOW_WRITE_ACTIONS=1` | 在 `real_*` 下允许**写**操作(提交表单、下单等) |
|
|
||||||
|
|
||||||
进程环境变量优先于用户 `.env`(见 `CONFIG.md` 三层优先级)。
|
|
||||||
|
|
||||||
## 目录骨架
|
## 目录骨架
|
||||||
|
|
||||||
@@ -47,11 +37,24 @@ scripts/service/<domain>_adapter/
|
|||||||
base.py # 数据契约(dataclass)+ AdapterBase 接口
|
base.py # 数据契约(dataclass)+ AdapterBase 接口
|
||||||
mock.py # 离线仿真,给 CI/单测
|
mock.py # 离线仿真,给 CI/单测
|
||||||
real_api.py # 真实系统 API
|
real_api.py # 真实系统 API
|
||||||
sim_rpa.py # 仿真平台 RPA
|
sim_rpa.py # 薄仿真平台 RPA adapter(委托 *_playwright.py)
|
||||||
real_rpa.py # 真实系统 RPA(占位,谨慎实现)
|
real_rpa.py # 真实系统 RPA(占位,谨慎实现)
|
||||||
|
|
||||||
|
scripts/service/
|
||||||
|
<platform>_playwright.py # RPA 主流程(厚);与薄 sim_rpa.py 并列
|
||||||
|
account_client.py # account-manager subprocess 唯一封装
|
||||||
|
browser_session.py # async persistent context 启动
|
||||||
```
|
```
|
||||||
|
|
||||||
模板示例:`scripts/service/example_adapter/`(复制改名即用)。
|
模板示例(均在 `examples/`,**不在** `scripts/`):
|
||||||
|
|
||||||
|
- **仿真浏览器 RPA 完整案例**:`examples/simulator_browser_rpa/` — async mock + simulator_rpa + `simulator_playwright.py` + sandbox 页面 + adapter 分层 + 测试
|
||||||
|
- **真实浏览器 RPA 完整案例**:`examples/real_browser_rpa/` — 登录/验证码/滚动采集 + `account_client.py`
|
||||||
|
- **真实 API / 仿真 API**:`examples/real_api/`、`examples/simulator_api/` — 当前为规划占位,尚未沉淀可复制实现
|
||||||
|
|
||||||
|
复制 adapter 分层时,以修正后的 `examples/simulator_browser_rpa/`(薄 adapter + `simulator_playwright.py`)与 `examples/real_browser_rpa/scripts/service/account_client.py` 为权威参考,在新技能中创建 `scripts/service/<domain>_adapter/` 并按 README copy map 选择性复制。
|
||||||
|
|
||||||
|
> 先判断属于四象限哪一种(`real_browser_rpa` / `real_api` / `simulator_browser_rpa` / `simulator_api`),再读对应 `examples/*/README.md`。示例是**参考架构与边界**,不是业务代码原样复制。
|
||||||
|
|
||||||
## 档位 dispatch
|
## 档位 dispatch
|
||||||
|
|
||||||
@@ -66,8 +69,10 @@ scripts/service/<domain>_adapter/
|
|||||||
|
|
||||||
```python
|
```python
|
||||||
# __init__.py
|
# __init__.py
|
||||||
|
from jiangchang_skill_core import config
|
||||||
|
|
||||||
def get_adapter():
|
def get_adapter():
|
||||||
target = (os.environ.get("OPENCLAW_TEST_TARGET") or "mock").lower()
|
target = (config.get("OPENCLAW_TEST_TARGET") or "mock").lower()
|
||||||
if target in ("unit", "mock"):
|
if target in ("unit", "mock"):
|
||||||
return MockAdapter()
|
return MockAdapter()
|
||||||
if target == "real_api":
|
if target == "real_api":
|
||||||
@@ -77,7 +82,7 @@ def get_adapter():
|
|||||||
return SimRpaAdapter()
|
return SimRpaAdapter()
|
||||||
```
|
```
|
||||||
|
|
||||||
`get_adapter()` 实现应配合 `tests/adapter_test_utils.py` 的 profile 策略:**未授权时不 silently 开启真实网络/RPA**。
|
`get_adapter()` 实现应配合 `tests/adapter_test_utils.py` 的 profile 策略:**默认必跑测试不得误设 `OPENCLAW_TEST_TARGET=real_*`**。
|
||||||
|
|
||||||
## contract tests
|
## contract tests
|
||||||
|
|
||||||
@@ -92,15 +97,29 @@ def get_adapter():
|
|||||||
依赖 account-manager 或其他兄弟技能时:
|
依赖 account-manager 或其他兄弟技能时:
|
||||||
|
|
||||||
1. 在 `SKILL.md` 的 `metadata.openclaw.dependencies.required` 声明。
|
1. 在 `SKILL.md` 的 `metadata.openclaw.dependencies.required` 声明。
|
||||||
2. 调用兄弟技能走统一 **`service.sibling_bridge`**(`call_sibling_json`),**不要**在 service 层散落 `subprocess.run`。
|
2. **普通兄弟技能调用**,优先走统一 **`service.sibling_bridge`**(`call_sibling_json`),**不要**在 `task_service.py`、`task_rpa.py` 等业务流程文件中到处散落 `subprocess.run`。
|
||||||
3. **lease 必须 release**(account-manager 租约);进程被 kill 后可能残留 lease,需在运维文档说明排查方式(查 account-manager lease 列表 / 手动释放)。
|
3. **account-manager 账号/租约能力**是例外:可参考 `examples/real_browser_rpa/scripts/service/account_client.py` 与 `examples/simulator_browser_rpa/scripts/service/account_client.py`,封装为**单一** `account_client.py`;允许在该文件内部集中通过 subprocess 调 account-manager CLI。**禁止** import `rpa_helpers` 等 account-manager 内部模块;`simulator_rpa` 与 `real_rpa` 均走同一 `pick_web_account` / `release_lease` 模式。
|
||||||
|
4. **不允许**直接 `import account-manager` 的内部 Python 模块(如 `service/`、`util/`、`db/`)。
|
||||||
|
5. **pick lease 后必须 `finally release lease`**;进程被 kill 后可能残留 lease,需在运维文档说明排查方式(查 account-manager lease 列表 / 手动释放)。
|
||||||
|
|
||||||
```python
|
```python
|
||||||
|
# 普通兄弟技能 — 走 sibling_bridge
|
||||||
from service.sibling_bridge import call_sibling_json
|
from service.sibling_bridge import call_sibling_json
|
||||||
|
|
||||||
result = call_sibling_json("account-manager", ["list", "--limit", "10"])
|
result = call_sibling_json("account-manager", ["list", "--limit", "10"])
|
||||||
```
|
```
|
||||||
|
|
||||||
|
```python
|
||||||
|
# account-manager 账号/租约 — 集中在 account_client.py
|
||||||
|
from service.account_client import pick_web_account, release_lease
|
||||||
|
|
||||||
|
account = pick_web_account(platform="target_platform")
|
||||||
|
try:
|
||||||
|
...
|
||||||
|
finally:
|
||||||
|
release_lease(account.get("lease_token"))
|
||||||
|
```
|
||||||
|
|
||||||
## 相关文档
|
## 相关文档
|
||||||
|
|
||||||
- `RPA.md` — 三端 RPA 技术选型与拟人/反反爬范式
|
- `RPA.md` — 三端 RPA 技术选型与拟人/反反爬范式
|
||||||
@@ -7,7 +7,7 @@
|
|||||||
1. **三层优先级**:`进程环境变量` > `{数据目录}/.env` > 仓库 `.env.example` 默认值。
|
1. **三层优先级**:`进程环境变量` > `{数据目录}/.env` > 仓库 `.env.example` 默认值。
|
||||||
2. **`.env.example` 进仓库**:带注释的全量默认配置,是配置项的"单一事实来源"。
|
2. **`.env.example` 进仓库**:带注释的全量默认配置,是配置项的"单一事实来源"。
|
||||||
3. **首次运行自动 copy(bootstrap)**:`scripts/main.py` 启动与 `cli.app.main()` 均会调用 `util.config_bootstrap.bootstrap_skill_config()`,把仓库根目录 `.env.example` 复制到
|
3. **首次运行自动 copy(bootstrap)**:`scripts/main.py` 启动与 `cli.app.main()` 均会调用 `util.config_bootstrap.bootstrap_skill_config()`,把仓库根目录 `.env.example` 复制到
|
||||||
`{CLAW_DATA_ROOT}/{user}/{slug}/.env`(**已存在则不整体覆盖**,保护用户改过的值)。
|
`{JIANGCHANG_DATA_ROOT}/{JIANGCHANG_USER_ID}/{slug}/.env`(**已存在则不整体覆盖**,保护用户改过的值)。
|
||||||
4. **技能升级合并缺失项**:`merge_missing_env_keys` 会把 `.env.example` 中新增、但用户 `.env` 尚未包含的 key **追加**进去(带注释块)。
|
4. **技能升级合并缺失项**:`merge_missing_env_keys` 会把 `.env.example` 中新增、但用户 `.env` 尚未包含的 key **追加**进去(带注释块)。
|
||||||
5. **敏感信息绝不进 `.env`**:密码/密钥/口令走 account-manager 的 `secret-ref`(见下"红线")。
|
5. **敏感信息绝不进 `.env`**:密码/密钥/口令走 account-manager 的 `secret-ref`(见下"红线")。
|
||||||
|
|
||||||
@@ -23,6 +23,7 @@ OPENCLAW_TEST_TARGET=mock # mock | simulator_rpa | real_api | real_
|
|||||||
TARGET_BASE_URL=https://sandbox.jc2009.com
|
TARGET_BASE_URL=https://sandbox.jc2009.com
|
||||||
|
|
||||||
# ── 默认账号(仅非敏感标识;密码走 account-manager)──
|
# ── 默认账号(仅非敏感标识;密码走 account-manager)──
|
||||||
|
# 浏览器 RPA simulator_rpa 联调账号来自 account-manager(platform + profile_dir),不是此处的 DEFAULT_LOGIN_ID
|
||||||
DEFAULT_LOGIN_ID=04110001
|
DEFAULT_LOGIN_ID=04110001
|
||||||
|
|
||||||
# ── 浏览器 / RPA ──
|
# ── 浏览器 / RPA ──
|
||||||
@@ -30,7 +31,7 @@ OPENCLAW_BROWSER_HEADLESS=0 # 0=有头(默认) 1=无头(CI)
|
|||||||
OPENCLAW_PLAYWRIGHT_STEALTH=1 # 1=开启反检测指纹(默认)
|
OPENCLAW_PLAYWRIGHT_STEALTH=1 # 1=开启反检测指纹(默认)
|
||||||
|
|
||||||
# ── 录屏与失败存证 ──
|
# ── 录屏与失败存证 ──
|
||||||
OPENCLAW_RECORD_VIDEO=1 # 1=录屏+字幕+旁白+背景音+MP4(RPA 默认开,见 RPA.md)
|
OPENCLAW_RECORD_VIDEO=0 # 0=默认不录屏;1=启用录屏+字幕+旁白+背景音+MP4(见 RPA.md)
|
||||||
OPENCLAW_ARTIFACTS_ON_FAILURE=1 # 1=失败截图(默认)
|
OPENCLAW_ARTIFACTS_ON_FAILURE=1 # 1=失败截图(默认)
|
||||||
|
|
||||||
# ── 节流 / 超时 ──
|
# ── 节流 / 超时 ──
|
||||||
@@ -39,7 +40,22 @@ STEP_DELAY_MAX=5.0
|
|||||||
HUMAN_WAIT_TIMEOUT=180 # 滑块/验证码/2FA 等人工超时(秒)
|
HUMAN_WAIT_TIMEOUT=180 # 滑块/验证码/2FA 等人工超时(秒)
|
||||||
```
|
```
|
||||||
|
|
||||||
**业务专属配置必须带技能前缀**(如 `DEMO_XXX`、`MY_SKILL_XXX`),**不要污染全局命名空间**(禁止 `SCRAPE_1688_*`、`RECEIVE_ORDER_*` 等跨技能前缀写入模板)。
|
**业务专属配置必须带技能前缀**(如 `DEMO_XXX`、`MY_SKILL_XXX`、`SKILL_DOWNLOAD_DIR`),**不要污染全局命名空间**(禁止 `SCRAPE_1688_*`、`RECEIVE_ORDER_*` 等跨技能前缀写入模板)。
|
||||||
|
|
||||||
|
### 数据目录子路径(下载 / 导入 / 导出)
|
||||||
|
|
||||||
|
技能**自己写入**的文件默认在 `{JIANGCHANG_DATA_ROOT}/{USER_ID}/{slug}/` 下标准子目录(`downloads/`、`imports/`、`exports/` 等)。须通过 `util.runtime_paths.resolve_data_path()` 或 `get_*_dir()` 解析,**禁止** `os.path.abspath(config.get(...))` 相对 CWD。
|
||||||
|
|
||||||
|
- 权威说明:[`DATA_PATHS.md`](DATA_PATHS.md)
|
||||||
|
- `.env.example` 中路径类配置**留空即用默认**;若写相对路径,必须相对 **skill 数据目录**,**禁止** `./outputs/...` 这类相对 workspace 的写法
|
||||||
|
- 可选 env:`SKILL_DOWNLOAD_DIR`、`SKILL_IMPORT_DIR`、`SKILL_EXPORT_DIR` 等(见 DATA_PATHS.md)
|
||||||
|
|
||||||
|
### 录屏开关(`OPENCLAW_RECORD_VIDEO`)
|
||||||
|
|
||||||
|
- **模板默认 `OPENCLAW_RECORD_VIDEO=0`**,降低本地开发 / 调试成本(不启 ffmpeg、不生成 MP4)。
|
||||||
|
- 需要**生产留证、合规记录、客户现场复现或排障**时,可在用户 `.env` 或进程环境变量中设为 `OPENCLAW_RECORD_VIDEO=1`。
|
||||||
|
- **默认关闭不代表可以删除录屏接入**:RPA / 长任务代码仍须保留 `RpaVideoSession`、`video.add_step` 与 video artifact 写入 `result_summary`(见 `RPA.md` §5.3)。
|
||||||
|
- **已落盘的用户 `.env` 不会被模板整体覆盖**;老用户保留原值,技能升级时仅通过 `merge_missing_env_keys` **追加** `.env.example` 中新增的 key。
|
||||||
|
|
||||||
## 🚫 红线:敏感信息不进 `.env`
|
## 🚫 红线:敏感信息不进 `.env`
|
||||||
|
|
||||||
@@ -83,7 +99,7 @@ config.get_float("STEP_DELAY_MIN", 1.0)
|
|||||||
## health / config-path
|
## health / config-path
|
||||||
|
|
||||||
- **`health`**:输出 `collect_runtime_diagnostics` 字段(`platform_kit_version_ok`、`ffmpeg_path` 等),**不打印敏感值**;补充 `env_path` / `env_exists` / `example_path`。
|
- **`health`**:输出 `collect_runtime_diagnostics` 字段(`platform_kit_version_ok`、`ffmpeg_path` 等),**不打印敏感值**;补充 `env_path` / `env_exists` / `example_path`。
|
||||||
- **`config-path`**:输出 JSON,包含 `skill`、`env_path`(用户数据目录 `.env`)、`example_path`(仓库 `.env.example` 绝对路径),便于排查落盘位置。
|
- **`config-path`**:输出 JSON,包含 `skill`、`env_path`(用户数据目录 `.env`)、`example_path`(仓库 `.env.example` 绝对路径),便于排查落盘位置;业务 skill 建议附加 `list_resolved_data_paths()` 中的子目录路径(见 [`DATA_PATHS.md`](DATA_PATHS.md))。
|
||||||
|
|
||||||
## doctor / setup 命令(可选)
|
## doctor / setup 命令(可选)
|
||||||
|
|
||||||
@@ -96,4 +112,5 @@ config.get_float("STEP_DELAY_MIN", 1.0)
|
|||||||
|
|
||||||
- `RPA.md` — 三端 RPA 标准与各开关含义
|
- `RPA.md` — 三端 RPA 标准与各开关含义
|
||||||
- `ADAPTER.md` — `OPENCLAW_TEST_TARGET` 四档模式
|
- `ADAPTER.md` — `OPENCLAW_TEST_TARGET` 四档模式
|
||||||
- `RUNTIME.md` — `CLAW_*` 环境变量与数据目录约定
|
- `RUNTIME.md` — `JIANGCHANG_DATA_ROOT` 与数据根约定
|
||||||
|
- `DATA_PATHS.md` — 下载/导入/导出等子目录与 `resolve_data_path` 规范
|
||||||
160
development/DATA_PATHS.md
Normal file
160
development/DATA_PATHS.md
Normal file
@@ -0,0 +1,160 @@
|
|||||||
|
# 用户数据目录与文件路径黄金标准
|
||||||
|
|
||||||
|
> 本文是 skill **读写本地文件** 的权威约定。凡涉及下载、导入、导出、缓存、RPA 存证等路径,复制模板后须遵守本文;Agent 实现业务 skill 时也应先读本文。
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 1. 根目录
|
||||||
|
|
||||||
|
所有技能持久化数据(配置、数据库、业务文件)默认落在:
|
||||||
|
|
||||||
|
```text
|
||||||
|
{JIANGCHANG_DATA_ROOT}/{JIANGCHANG_USER_ID}/{skill_slug}/
|
||||||
|
```
|
||||||
|
|
||||||
|
示例:
|
||||||
|
|
||||||
|
```text
|
||||||
|
D:\jiangchang-data\12500\download-video-baidu-haokan\
|
||||||
|
```
|
||||||
|
|
||||||
|
实现:`util.runtime_paths.get_skill_data_dir()`。
|
||||||
|
|
||||||
|
**禁止**把技能自己写入的文件落到:
|
||||||
|
|
||||||
|
- Agent / 宿主 **workspace**(进程 CWD)
|
||||||
|
- 技能安装目录(含 `.openclaw/skills/...`)
|
||||||
|
- 未在本文声明的任意相对 CWD 路径
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 2. 标准子目录树
|
||||||
|
|
||||||
|
| 子目录 | 用途 | 默认 helper |
|
||||||
|
|--------|------|-------------|
|
||||||
|
| `downloads/` | 从外部拉取的原始文件(视频、回单、PDF…) | `get_downloads_dir()` |
|
||||||
|
| `imports/` | 用户/Agent 放入的待处理清单(urls.txt、batch.json、CSV) | `get_imports_dir()` |
|
||||||
|
| `exports/` | 导出给用户的结果(xlsx/csv/json) | `get_exports_dir()` |
|
||||||
|
| `uploads/` | 待发布到外部平台的 staging(可选) | `get_uploads_dir()` |
|
||||||
|
| `cache/` | 可再生缓存(页面快照、解析中间结果) | `get_cache_dir()` |
|
||||||
|
| `temp/` | 单次任务 scratch,任务结束可删 | `get_temp_dir()` |
|
||||||
|
| `rpa-artifacts/` | RPA 失败截图、录屏中间产物 | `get_rpa_artifacts_dir(batch_id)` |
|
||||||
|
| `videos/` | RPA 录屏成片 MP4 | `get_videos_dir()` |
|
||||||
|
| `{skill_slug}.db` | SQLite | `get_db_path()` |
|
||||||
|
| `.env` | 用户配置 | `config.get_env_file_path()` |
|
||||||
|
|
||||||
|
复制后若业务不需要某目录,可不创建;**但一旦写入该类文件,必须走对应 helper**。
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 3. 两类路径
|
||||||
|
|
||||||
|
### A. 技能拥有(skill-owned)
|
||||||
|
|
||||||
|
技能**创建或更新**的文件:下载结果、导出报表、失败截图、队列 sidecar 等。
|
||||||
|
|
||||||
|
- **默认**必须在 `{skill_data_dir}/<子目录>/` 下。
|
||||||
|
- **必须**通过 `resolve_data_path()` 或 `get_*_dir()` 解析,**禁止** `os.path.abspath(config.get(...))` 相对 CWD。
|
||||||
|
|
||||||
|
### B. 用户显式输入(user-owned)
|
||||||
|
|
||||||
|
用户在 CLI / Agent 对话中给出的**已有文件**绝对路径,例如「发布这个视频 `D:\素材\x.mp4`」。
|
||||||
|
|
||||||
|
- 技能**只读**,路径可以是用户磁盘任意位置。
|
||||||
|
- 使用 `resolve_input_path()`:绝对路径原样;**相对路径**解析为 `{skill_data_dir}/imports/<相对路径>`,**不**相对 workspace/CWD。
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 4. 配置项(`.env`)
|
||||||
|
|
||||||
|
可选覆盖项(**留空则用默认子目录**):
|
||||||
|
|
||||||
|
```ini
|
||||||
|
# ── 数据目录子路径(见 development/DATA_PATHS.md)──
|
||||||
|
# 相对路径相对于 {JIANGCHANG_DATA_ROOT}/{USER_ID}/{slug}/,不是 workspace
|
||||||
|
# SKILL_DOWNLOAD_DIR=downloads/videos
|
||||||
|
# SKILL_IMPORT_DIR=imports
|
||||||
|
# SKILL_EXPORT_DIR=exports
|
||||||
|
# SKILL_UPLOAD_DIR=uploads
|
||||||
|
# SKILL_CACHE_DIR=cache
|
||||||
|
# SKILL_TEMP_DIR=temp
|
||||||
|
```
|
||||||
|
|
||||||
|
| 规则 | 说明 |
|
||||||
|
|------|------|
|
||||||
|
| 未配置 | `{skill_data_dir}/{default_subdir}` |
|
||||||
|
| 相对值 `downloads/videos` | `{skill_data_dir}/downloads/videos` |
|
||||||
|
| 绝对值 `D:\nas\exports` | 高级用户/NAS 挂载,原样使用 |
|
||||||
|
| **禁止** `./outputs/...` | 会随 CWD 漂移,不得作为 `.env.example` 活跃配置行 |
|
||||||
|
|
||||||
|
业务专属路径 env key 须带 **`SKILL_` 前缀**(或技能 slug 前缀),避免污染全局命名空间。
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 5. 代码用法
|
||||||
|
|
||||||
|
```python
|
||||||
|
from util.runtime_paths import (
|
||||||
|
get_downloads_dir,
|
||||||
|
get_imports_dir,
|
||||||
|
resolve_data_path,
|
||||||
|
resolve_input_path,
|
||||||
|
list_resolved_data_paths,
|
||||||
|
)
|
||||||
|
|
||||||
|
# 下载保存(A 类)
|
||||||
|
dest = os.path.join(get_downloads_dir(), f"{vid}.mp4")
|
||||||
|
|
||||||
|
# 或带 env 覆盖
|
||||||
|
out_dir = resolve_data_path("SKILL_DOWNLOAD_DIR", "downloads")
|
||||||
|
|
||||||
|
# CLI -i urls.txt(B 类,相对路径 → imports/)
|
||||||
|
manifest = resolve_input_path(args.input_path)
|
||||||
|
|
||||||
|
# health 诊断
|
||||||
|
paths = list_resolved_data_paths()
|
||||||
|
```
|
||||||
|
|
||||||
|
**反模式(禁止):**
|
||||||
|
|
||||||
|
```python
|
||||||
|
# ❌ 相对 CWD,Agent 从 workspace 执行时会写错盘
|
||||||
|
os.path.abspath(config.get("DOWNLOAD_OUTPUT_DIR"))
|
||||||
|
|
||||||
|
# ❌ .env.example 里写 ./outputs/videos
|
||||||
|
DOWNLOAD_OUTPUT_DIR=./outputs/videos
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 6. health / config-path
|
||||||
|
|
||||||
|
`health` 或 `config-path` 建议输出 `list_resolved_data_paths()` 中的路径,便于排查「文件写到哪里去了」。展示路径须与 `run` 实际使用的一致。
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 7. 与 RPA 存证的关系
|
||||||
|
|
||||||
|
- 失败截图、录屏中间文件:`rpa-artifacts/{batch_id}/`(见 [`RPA.md`](RPA.md) §5.1)
|
||||||
|
- 录屏成片:`videos/`(见 [`RPA.md`](RPA.md) §5.3)
|
||||||
|
|
||||||
|
业务 `downloads/` 与 RPA `videos/` **分工不同**:前者是业务产出文件,后者是操作过程录屏。
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 8. 测试
|
||||||
|
|
||||||
|
- 单元测试须用 `IsolatedDataRoot`,断言路径落在临时数据根下。
|
||||||
|
- `tests/test_runtime_paths.py` 覆盖 `resolve_data_path` / `resolve_input_path`。
|
||||||
|
- `POLICY-DATA-PATH-001`:禁止 `.env.example` 中 `./` 路径;禁止业务代码 `abspath(config.get(...))`。
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 9. 相关文档
|
||||||
|
|
||||||
|
| 文档 | 内容 |
|
||||||
|
|------|------|
|
||||||
|
| [`RUNTIME.md`](RUNTIME.md) | 数据根、共享 runtime、编码 |
|
||||||
|
| [`CONFIG.md`](CONFIG.md) | `.env` bootstrap 与三层优先级 |
|
||||||
|
| [`RPA.md`](RPA.md) | rpa-artifacts / videos 存证 |
|
||||||
|
| [`POLICY_MATRIX.md`](POLICY_MATRIX.md) | POLICY-DATA-PATH-001 |
|
||||||
@@ -79,11 +79,14 @@
|
|||||||
|
|
||||||
你不应该直接在这个仓库里开发业务,而应该:
|
你不应该直接在这个仓库里开发业务,而应该:
|
||||||
|
|
||||||
1. 复制这个目录
|
0. 按 [`NAMING.md`](NAMING.md) 确定 slug(`{verb}-{noun-phrase}-{platform}`)
|
||||||
2. 改成新 skill 的目录名
|
1. **优先**用 [`tools/scaffold_skill.ps1`](../tools/scaffold_skill.ps1) 创建新目录(见 [`tools/README.md`](../tools/README.md))
|
||||||
|
2. 在新目录内 `git init` 并绑定**本技能**远端(**不得**保留模板 `.git`)
|
||||||
3. 把占位内容替换掉
|
3. 把占位内容替换掉
|
||||||
4. 再开始写业务逻辑
|
4. 再开始写业务逻辑
|
||||||
|
|
||||||
|
> **Git 红线**:禁止资源管理器整文件夹复制后保留模板 `.git`;`git remote -v` 必须指向新技能仓库,不能仍是 skill-template。
|
||||||
|
|
||||||
## 2. 新 skill 的标准目录结构
|
## 2. 新 skill 的标准目录结构
|
||||||
|
|
||||||
复制模板后,你应该保留下面这套结构:
|
复制模板后,你应该保留下面这套结构:
|
||||||
@@ -92,6 +95,7 @@
|
|||||||
your-skill/
|
your-skill/
|
||||||
├─ .github/
|
├─ .github/
|
||||||
├─ assets/
|
├─ assets/
|
||||||
|
├─ development/
|
||||||
├─ evals/
|
├─ evals/
|
||||||
├─ references/
|
├─ references/
|
||||||
├─ scripts/
|
├─ scripts/
|
||||||
@@ -110,7 +114,10 @@ your-skill/
|
|||||||
各目录职责如下:
|
各目录职责如下:
|
||||||
|
|
||||||
- `assets/`:放示例输出、schema、静态说明资源
|
- `assets/`:放示例输出、schema、静态说明资源
|
||||||
- `references/`:放研发和编排需要长期维护的文档
|
- `README.md`:面向用户的市场详情页说明
|
||||||
|
- `SKILL.md`:面向 LLM / 平台的技能入口
|
||||||
|
- `references/`:Agent 运行/编排/调用时渐进加载的补充上下文(如 CLI、SCHEMA)
|
||||||
|
- `development/`:面向开发者与 AI 编程代理的开发资料、需求、测试与技术规范
|
||||||
- `scripts/`:放真正的代码
|
- `scripts/`:放真正的代码
|
||||||
- `tests/`:放自动化测试
|
- `tests/`:放自动化测试
|
||||||
- `evals/`:放人工验收材料、评估清单、示例场景
|
- `evals/`:放人工验收材料、评估清单、示例场景
|
||||||
@@ -145,20 +152,33 @@ scripts/
|
|||||||
- `service/`
|
- `service/`
|
||||||
作用:核心业务逻辑
|
作用:核心业务逻辑
|
||||||
比如任务编排、调用兄弟技能、外部 API、可选浏览器自动化
|
比如任务编排、调用兄弟技能、外部 API、可选浏览器自动化
|
||||||
|
**负责业务关键节点日志**(`task_start` / 外部调用 / `task_failed` 等,见 [`LOGGING.md`](LOGGING.md))
|
||||||
|
|
||||||
- `util/`
|
- `util/`
|
||||||
作用:常量、日志、路径、时间工具、通用帮助函数
|
作用:常量、日志、路径、时间工具、通用帮助函数
|
||||||
|
`util/logging_config.py` 是 `jiangchang_skill_core.unified_logging` 的**薄封装**,业务代码应通过它获取 logger
|
||||||
|
|
||||||
公共能力(config、logging、runtime_env、rpa、media_assets、video_session、runtime_diagnostics)从共享 runtime 的 `jiangchang-platform-kit>=1.0.13` import,**不得**在 `scripts/` 下保留 `jiangchang_skill_core/` 副本。
|
公共能力(config、logging、runtime_env、rpa、media_assets、video_session、runtime_diagnostics、activity/SRCP)从共享 runtime 的 `jiangchang-platform-kit>=1.2.0` import,**不得**在 `scripts/` 下保留 `jiangchang_skill_core/` 副本。
|
||||||
|
|
||||||
## 3.2 开发 RPA 类 skill
|
## 3.2 开发 RPA 类 skill
|
||||||
|
|
||||||
若 skill 需要浏览器/桌面/手机自动化,按以下顺序落地:
|
若 skill 需要浏览器/桌面/手机自动化,按以下顺序落地:
|
||||||
|
|
||||||
1. **先读三份标准**:`references/RPA.md`(三端范式与反反爬)、`references/CONFIG.md`(`.env` 落盘与读取)、`references/ADAPTER.md`(四档 adapter)。
|
1. **先读三份标准**:`RPA.md`(三端范式与反反爬)、`CONFIG.md`(`.env` 落盘与读取)、`ADAPTER.md`(四档 adapter)。
|
||||||
2. **从模板复制骨架**:
|
2. **从 examples 选择性复制**(不要整包照搬 `examples/<mode>/`,见各 example README 的 copy map):
|
||||||
- `scripts/service/example_adapter/` → 改名为 `scripts/service/<domain>_adapter/`
|
- 若是 **真实浏览器 RPA**(真实网站、登录态、验证码、滚动采集),**必须先读** `examples/real_browser_rpa/README.md`,再按需复制到 `scripts/service/`:
|
||||||
- 参考 `scripts/service/example_browser_rpa.py` 写浏览器会话逻辑
|
- `examples/real_browser_rpa/scripts/service/browser_session.py`
|
||||||
|
- `examples/real_browser_rpa/scripts/service/human_verification.py`
|
||||||
|
- `examples/real_browser_rpa/scripts/service/task_rpa.py`
|
||||||
|
- `examples/real_browser_rpa/scripts/service/account_client.py`
|
||||||
|
- 若是 **仿真浏览器 RPA**(自有 sandbox、表单批量提交、可控 DOM),**必须先读** `examples/simulator_browser_rpa/README.md`,再按需复制到 `scripts/service/`:
|
||||||
|
- `examples/simulator_browser_rpa/scripts/service/browser_session.py`(async)
|
||||||
|
- `examples/simulator_browser_rpa/scripts/service/account_client.py`(subprocess,对齐 `real_browser_rpa`)
|
||||||
|
- `examples/simulator_browser_rpa/scripts/service/simulator_playwright.py`(RPA 主流程,复制后改名为 `<platform>_playwright.py`)
|
||||||
|
- `examples/simulator_browser_rpa/scripts/service/adapter/`(薄 `simulator_rpa` + mock)
|
||||||
|
- `examples/simulator_browser_rpa/scripts/service/task_service.py`(async 编排)
|
||||||
|
- `sandbox/demo_app.html` 仅留在 examples,不进入生产 skill
|
||||||
|
- **禁止**:`rpa_helpers`、sync Playwright 用于完整技能 RPA 主路径、`task_service` 散落 account-manager subprocess
|
||||||
3. **只用共享库,不在 skill 里重写反反爬**:
|
3. **只用共享库,不在 skill 里重写反反爬**:
|
||||||
```python
|
```python
|
||||||
from jiangchang_skill_core import config
|
from jiangchang_skill_core import config
|
||||||
@@ -175,7 +195,7 @@ scripts/
|
|||||||
技能根目录的 `requirements.txt` 是**标准文件**,用于声明本技能**特有** Python 三方依赖。
|
技能根目录的 `requirements.txt` 是**标准文件**,用于声明本技能**特有** Python 三方依赖。
|
||||||
|
|
||||||
- **公共依赖**(`jiangchang-platform-kit`、`playwright`、config、runtime diagnostics、RPA 公共能力等)由**宿主共享 runtime** 提供,**不要**写入技能 `requirements.txt`。
|
- **公共依赖**(`jiangchang-platform-kit`、`playwright`、config、runtime diagnostics、RPA 公共能力等)由**宿主共享 runtime** 提供,**不要**写入技能 `requirements.txt`。
|
||||||
- `SKILL.md` 的 `metadata.openclaw.platform_kit_min_version`(当前 `1.0.13`)是运行契约/兼容性声明,供宿主安装与启用时校验,**不是** pip 依赖声明。
|
- `SKILL.md` 的 `metadata.openclaw.platform_kit_min_version`(当前 `1.2.0`)是运行契约/兼容性声明,供宿主安装与启用时校验,**不是** pip 依赖声明。
|
||||||
- 匠厂宿主安装/更新技能后,会将技能 `requirements.txt` 安装到共享 venv:`{JIANGCHANG_DATA_ROOT}/python-runtime/.venv`。
|
- 匠厂宿主安装/更新技能后,会将技能 `requirements.txt` 安装到共享 venv:`{JIANGCHANG_DATA_ROOT}/python-runtime/.venv`。
|
||||||
- **不要**在业务代码中 `subprocess` / `pip install`;缺依赖由 `health` 报错,由宿主负责安装。
|
- **不要**在业务代码中 `subprocess` / `pip install`;缺依赖由 `health` 报错,由宿主负责安装。
|
||||||
- **版本约束尽量收窄**,降低多技能共享 venv 时的冲突风险。推荐范围写法:
|
- **版本约束尽量收窄**,降低多技能共享 venv 时的冲突风险。推荐范围写法:
|
||||||
@@ -203,18 +223,80 @@ scripts/
|
|||||||
|
|
||||||
`health` 应委托 `collect_runtime_diagnostics`,不要在 `scripts/util/` 自建 `runtime_diagnostics.py`。
|
`health` 应委托 `collect_runtime_diagnostics`,不要在 `scripts/util/` 自建 `runtime_diagnostics.py`。
|
||||||
|
|
||||||
|
### 3.4 发布打包约束(PyArmor 与编码)
|
||||||
|
|
||||||
|
release workflow 会对 `scripts/` 下的 Python 源码做加密/打包。当前发布链路可能运行在 **PyArmor 免费/试用模式**,因此有两条**发布硬约束**:
|
||||||
|
|
||||||
|
**单文件行数(仅 `scripts/**/*.py`)**
|
||||||
|
|
||||||
|
- 每个 `scripts/` 下 Python 文件必须 **小于 1000 行**(`>= 1000` 可能在 CI 加密阶段失败)。
|
||||||
|
- 推荐单文件控制在 **900 行以内**;接近上限时应拆分模块。
|
||||||
|
- 不要把完整 RPA 主流程、DOM 选择器、数据库、CLI、日志全部堆进一个大文件。
|
||||||
|
- 正确做法是拆分到 `scripts/cli/`、`scripts/service/`、`scripts/db/`、`scripts/util/`。
|
||||||
|
|
||||||
|
`examples/` 里的 Python 示例也建议保持模块化,但**行数限制不作为 examples 的发布硬约束**;不要对 README、HTML、测试数据等文档/资源套用 PyArmor 行数规则。
|
||||||
|
|
||||||
|
**文本编码(UTF-8 without BOM)**
|
||||||
|
|
||||||
|
- 所有提交到 skill 的源码、文档、配置文本文件必须是 **UTF-8 without BOM**。
|
||||||
|
- Python 文件**绝对不能**带 UTF-8 BOM;否则 PyArmor/CI 可能报:`invalid non-printable character U+FEFF`。
|
||||||
|
- Windows 编辑器容易保存成 UTF-8 BOM,提交前须确认文件开头无 `U+FEFF`。
|
||||||
|
- 不要只写「UTF-8」,必须明确 **UTF-8 without BOM**。
|
||||||
|
|
||||||
|
本地可用 `python tests/run_tests.py release_packaging_constraints -v` 自检;默认必跑套件中的 `test_release_packaging_constraints.py` 会守护上述规则。
|
||||||
|
|
||||||
## 4. 开发一个新 skill 的标准步骤
|
## 4. 开发一个新 skill 的标准步骤
|
||||||
|
|
||||||
下面这套顺序建议严格按步骤做,不要一上来就直接写 `service`。
|
下面这套顺序建议严格按步骤做,不要一上来就直接写 `service`。
|
||||||
|
|
||||||
### 第一步:复制模板并改目录名
|
### 第一步:复制模板并改目录名
|
||||||
|
|
||||||
例如你要开发 `your-skill-slug`(或 `disburse-payroll-icbc` 一类领域 skill):
|
例如你要开发 `disburse-payroll-icbc` 一类领域 skill(目录名 = slug):
|
||||||
|
|
||||||
1. 复制 `skill-template`
|
#### 推荐方式(首选)
|
||||||
2. 新目录改成与 `slug` 一致的名称(如 `your-skill-slug`)
|
|
||||||
3. 初始化为独立 git 仓库
|
在 **skill-template 仓库根目录**执行:
|
||||||
4. 关联它自己的远端仓库
|
|
||||||
|
```powershell
|
||||||
|
.\tools\scaffold_skill.ps1 -Slug disburse-payroll-icbc -Destination D:\OpenClaw\client-gdcm\disburse-payroll-icbc
|
||||||
|
cd D:\OpenClaw\client-gdcm\disburse-payroll-icbc
|
||||||
|
git init
|
||||||
|
git remote add origin <你的新技能 Gitea/Git 仓库 URL>
|
||||||
|
git remote -v # 确认 origin 不是 skill-template
|
||||||
|
```
|
||||||
|
|
||||||
|
跨平台可用 `python tools/scaffold_skill.py --slug ... --destination ...`(见 [`tools/README.md`](../tools/README.md))。
|
||||||
|
|
||||||
|
脚手架会排除 `.git`、缓存与 `.env`,并删除 `.openclaw-skill-template` 标记;**不会**自动 `git init`。
|
||||||
|
|
||||||
|
#### 禁止方式
|
||||||
|
|
||||||
|
| 做法 | 后果 |
|
||||||
|
|------|------|
|
||||||
|
| ❌ 资源管理器整文件夹复制后不做 Git 清理 | 隐藏 `.git` 被带走,push 串到 template |
|
||||||
|
| ❌ 保留模板 `.git` 只改 `remote url` | 历史、分支、对象库仍属 template |
|
||||||
|
| ❌ 未删 `.git` 就 `git init` | 嵌套/混乱仓库,难以排查 |
|
||||||
|
|
||||||
|
#### 若已手工复制(补救)
|
||||||
|
|
||||||
|
```powershell
|
||||||
|
cd <新技能目录>
|
||||||
|
Remove-Item -Recurse -Force .git
|
||||||
|
Remove-Item -Recurse -Force .pytest_cache -ErrorAction SilentlyContinue
|
||||||
|
Remove-Item -Force .openclaw-skill-template -ErrorAction SilentlyContinue
|
||||||
|
git init
|
||||||
|
git remote add origin <新技能仓库 URL>
|
||||||
|
git remote -v
|
||||||
|
```
|
||||||
|
|
||||||
|
#### AI / 编程代理复制红线
|
||||||
|
|
||||||
|
| 禁止 | 说明 |
|
||||||
|
|------|------|
|
||||||
|
| 保留模板 `.git` | 必须先 `Remove-Item -Recurse -Force .git` 再 `git init` |
|
||||||
|
| `origin` 仍指向 skill-template | 复制后必须 `git remote -v` 自检 |
|
||||||
|
| 保留 `.openclaw-skill-template` | 仅 template 源仓库可有;业务技能不得保留 |
|
||||||
|
| 复制 `.pytest_cache` / `__pycache__` | 用 scaffold 已排除;手工复制应删 |
|
||||||
|
|
||||||
目录名要和 skill slug 对齐,后面很多地方都依赖这个命名。
|
目录名要和 skill slug 对齐,后面很多地方都依赖这个命名。
|
||||||
|
|
||||||
@@ -223,9 +305,10 @@ scripts/
|
|||||||
复制后优先改下面这些地方:
|
复制后优先改下面这些地方:
|
||||||
|
|
||||||
1. `SKILL.md`
|
1. `SKILL.md`
|
||||||
2. `scripts/util/constants.py`
|
2. 根目录 `README.md`(用户市场说明)
|
||||||
3. `references/` 下的文案
|
3. `scripts/util/constants.py`
|
||||||
4. `scripts/service/` 下的业务占位实现(优先改 `task_service.py`)
|
4. `references/` 与 `development/` 下的文案
|
||||||
|
5. `scripts/service/` 下的业务占位实现(优先改 `task_service.py`)
|
||||||
|
|
||||||
最先要统一的是:
|
最先要统一的是:
|
||||||
|
|
||||||
@@ -249,47 +332,44 @@ scripts/
|
|||||||
|
|
||||||
如果你的 skill 对外 CLI 需要自定义文案或别名(例如发布类对外仍叫 `publish`),通常还要替换:
|
如果你的 skill 对外 CLI 需要自定义文案或别名(例如发布类对外仍叫 `publish`),通常还要替换:
|
||||||
|
|
||||||
- `run` 命令中的中文提示与别名策略(见 `references/CLI.md`)
|
- `run` 命令中的中文提示与别名策略(见 `../references/CLI.md`)
|
||||||
- `references/CLI.md` 的命令示例
|
- `../references/CLI.md` 的命令示例
|
||||||
- `references/README.md` 的用户话术
|
- `../README.md` 的用户话术
|
||||||
- `references/SCHEMA.md` 的字段映射补充说明
|
- `../references/SCHEMA.md` 的字段映射补充说明
|
||||||
|
|
||||||
如需浏览器自动化,不要在模板里保留空的 `platform_playwright.py`;请按业务新建 `xxx_playwright.py`(命名自定),并在 `task_service.py` 中按需引用。
|
如需浏览器自动化,不要在模板里保留空的 `platform_playwright.py`;请按业务新建 `xxx_playwright.py`(命名自定),并在 `task_service.py` 中按需引用。
|
||||||
|
|
||||||
## 6. `SKILL.md` 应该怎么写
|
## 6. `SKILL.md` 与根 `README.md` 应该怎么写
|
||||||
|
|
||||||
`SKILL.md` 是技能清单,不是设计文档。
|
### `SKILL.md`(LLM / 平台入口)
|
||||||
|
|
||||||
|
`SKILL.md` 是给 LLM 与 OpenClaw 平台看的技能入口,**不是**用户市场详情页。
|
||||||
|
|
||||||
应该重点写:
|
应该重点写:
|
||||||
|
|
||||||
- 技能名称
|
- YAML frontmatter:`name`、`description`、`version`、`metadata.openclaw`(含 `slug`、`platform_kit_min_version`、`developer_ids` 等)
|
||||||
- 技能描述
|
- 何时触发本技能、CLI 入口摘要、运行契约要点
|
||||||
- `slug`
|
- 指向 `references/`(Agent 调用)与 `development/`(开发规范)的分工说明
|
||||||
作用:技能的唯一英文标识,通常用于仓库名、发布包名、运行时目录名、平台主键匹配等
|
|
||||||
示例:`your-skill-slug`、`disburse-payroll-icbc`、`tax-invoice-verify`
|
不要在 `SKILL.md` 里写大量用户市场文案或实现细节。
|
||||||
- `category`
|
|
||||||
作用:技能在平台中的分类,用于市场展示与归类,不是代码内部键
|
### 根 `README.md`(用户市场说明)
|
||||||
示例:`通用`、`内容`、`办公协作`、`物流`、`抖音`、`小红书`
|
|
||||||
- `developer_ids`(如需给非公开技能自动补开发者可见权限)
|
根 `README.md` 是技能市场详情页主来源(`metadata.readme_md`),面向普通用户。
|
||||||
作用:声明发布后默认拥有可见权限的开发者用户 ID 列表
|
|
||||||
- `dependencies`
|
应包含 YAML frontmatter 的 `description`(市场列表简介),正文用用户能理解的语言说明:
|
||||||
作用:声明该技能依赖的兄弟技能或运行前置能力,便于平台或编排层识别依赖关系
|
|
||||||
示例(按业务填写;发布类可能会依赖账号 / 内容技能,其它领域可能完全不同):
|
- 能做什么、适合什么场景、使用前准备什么
|
||||||
```yaml
|
- 如何自然语言告诉 Agent
|
||||||
dependencies:
|
- 执行结果、注意事项、常见问题
|
||||||
required:
|
|
||||||
- some-sibling-skill
|
不要在根 `README.md` 里写开发教程、目录规范、release、requirements 等技术细节。
|
||||||
```
|
|
||||||
- 何时使用本技能
|
|
||||||
- 对用户的引导话术
|
|
||||||
- CLI 使用原则
|
|
||||||
|
|
||||||
不要在 `SKILL.md` 里写大量实现细节。
|
|
||||||
实现细节放在:
|
实现细节放在:
|
||||||
|
|
||||||
- `references/`
|
- `development/`
|
||||||
- 代码注释
|
- `references/`(CLI / SCHEMA)
|
||||||
- `service/` 实现里
|
- 代码注释与 `service/` 实现
|
||||||
|
|
||||||
### 关于 `metadata.openclaw.developer_ids`
|
### 关于 `metadata.openclaw.developer_ids`
|
||||||
|
|
||||||
@@ -318,33 +398,33 @@ metadata:
|
|||||||
- 第一个 ID 会同步到 `skills.developer_id`
|
- 第一个 ID 会同步到 `skills.developer_id`
|
||||||
- 一期只做“补授权”,不会因为你 later 修改数组而自动撤销旧授权
|
- 一期只做“补授权”,不会因为你 later 修改数组而自动撤销旧授权
|
||||||
|
|
||||||
## 7. `references/` 应该放什么
|
## 7. 文档目录分工
|
||||||
|
|
||||||
`references/` 是当前规范 skill 的文档中心,建议至少有这些:
|
当前规范 skill 的文档按读者拆分:
|
||||||
|
|
||||||
- `README.md`
|
**根 `README.md`**
|
||||||
面向内部说明和技能作用介绍
|
|
||||||
|
|
||||||
- `CLI.md`
|
- 用户市场详情页说明(含 frontmatter `description`)
|
||||||
写清楚命令、参数、默认值、兄弟技能调用方式
|
|
||||||
|
|
||||||
- `RUNTIME.md`
|
**`SKILL.md`**
|
||||||
写清楚运行时目录、环境变量、入口约定
|
|
||||||
|
|
||||||
- `SCHEMA.md`
|
- LLM / OpenClaw 平台技能入口
|
||||||
写清楚数据库路径、核心表结构、日志表结构
|
|
||||||
|
|
||||||
- `DEVELOPMENT.md`
|
**`references/`**(Agent 运行/编排/调用时渐进加载)
|
||||||
写给技术人员的开发教程,也就是本文档
|
|
||||||
|
|
||||||
- `TESTING.md`
|
- `README.md` — Agent 参考索引(**不是**市场说明;不得含 YAML frontmatter 或 `description`)
|
||||||
写给技术人员的测试分层、隔离数据根与档位开关指南(详见 [`references/TESTING.md`](TESTING.md))
|
- `CLI.md` — 命令、参数、默认值、兄弟技能调用方式
|
||||||
|
- `SCHEMA.md` — 数据库路径、`_jiangchang_*` 数据管理契约、日志表结构
|
||||||
|
- `ACTIONS.md` — Skill Action manifest、宿主入口、参数与执行契约
|
||||||
|
- 可按需增加 `ERRORS.md`、`INTEGRATION.md` 等编排向补充
|
||||||
|
|
||||||
如果后面某个 skill 需要更细的说明,可以再加:
|
**`development/`**(开发者 / AI 编程代理)
|
||||||
|
|
||||||
- `ERRORS.md`
|
- `REQUIREMENTS.md` — 需求与验收
|
||||||
- `INTEGRATION.md`
|
- `DEVELOPMENT.md` — 开发教程(本文档)
|
||||||
- `PLATFORMS.md`
|
- `TESTING.md` — 测试分层与隔离(详见 [`TESTING.md`](TESTING.md))
|
||||||
|
- `LOGGING.md` — 日志分层、必打节点、敏感信息红线
|
||||||
|
- `ADAPTER.md`、`RPA.md`、`CONFIG.md`、`RUNTIME.md` — 技术规范
|
||||||
|
|
||||||
## 8. `assets/` 应该放什么
|
## 8. `assets/` 应该放什么
|
||||||
|
|
||||||
@@ -352,14 +432,17 @@ metadata:
|
|||||||
|
|
||||||
建议放:
|
建议放:
|
||||||
|
|
||||||
|
- `actions.json`
|
||||||
|
宿主 Action 入口 manifest(可选;见 `references/ACTIONS.md`)
|
||||||
|
|
||||||
- `examples/`
|
- `examples/`
|
||||||
比如 `version` 输出示例、`log-get` 输出示例
|
比如 `version` 输出示例、`log-get` 输出示例
|
||||||
|
|
||||||
- `schemas/`
|
- `schemas/`
|
||||||
比如日志记录、机读 JSON 的 schema
|
比如 `skill-actions.schema.json`、日志记录、机读 JSON 的 schema
|
||||||
|
|
||||||
不要把正式研发文档放到 `assets/`。
|
不要把正式研发文档放到 `assets/`。
|
||||||
文档应该进 `references/`。
|
用户说明进根 `README.md`;Agent 编排资料进 `references/`;开发规范进 `development/`。
|
||||||
|
|
||||||
## 9. `cli` 层怎么写
|
## 9. `cli` 层怎么写
|
||||||
|
|
||||||
@@ -398,6 +481,16 @@ metadata:
|
|||||||
- `entitlement_service.py`
|
- `entitlement_service.py`
|
||||||
放鉴权逻辑
|
放鉴权逻辑
|
||||||
|
|
||||||
|
### 日志要求(`cmd_run` / 核心业务)
|
||||||
|
|
||||||
|
`service` 层是**关键业务日志**的落点,须遵循 [`LOGGING.md`](LOGGING.md):
|
||||||
|
|
||||||
|
- `cmd_run` 及主任务入口必须:`log.info("task_start ...")`、`log.info("task_log_saved ...")`;失败路径必须 `log.exception("task_failed ...")`。
|
||||||
|
- 外部 API、兄弟技能、RPA 操作须记录**开始 / 结束 / 耗时 / 状态**(`external_call_start` / `external_call_done elapsed_ms=...`)。
|
||||||
|
- 长任务 / RPA 须配合 `activity.emit` 或 `RpaVideoSession.add_step` 输出用户可见进度;`cmd_run` 须 `job_context` + 各出口 `finish`(见 [`LOGGING.md`](LOGGING.md) §2.5)。
|
||||||
|
- 使用 `from util.logging_config import get_skill_logger`,**不要**用 `print` 替代 logger 做排障日志。
|
||||||
|
- **禁止**在日志中写入 password、token、cookie 等敏感明文(见 `LOGGING.md` §敏感信息红线)。
|
||||||
|
|
||||||
### 一个很重要的原则
|
### 一个很重要的原则
|
||||||
|
|
||||||
不要把所有逻辑都堆进一个文件。
|
不要把所有逻辑都堆进一个文件。
|
||||||
@@ -415,6 +508,12 @@ metadata:
|
|||||||
|
|
||||||
- `db/task_logs_repository.py`
|
- `db/task_logs_repository.py`
|
||||||
只做增删查改(模板默认表:`task_logs`)
|
只做增删查改(模板默认表:`task_logs`)
|
||||||
|
- `db/display_metadata.py`
|
||||||
|
创建 `_jiangchang_tables` / `_jiangchang_columns` 并幂等写入中文展示名(见 `references/SCHEMA.md`)
|
||||||
|
- `db/display_metadata_validator.py`
|
||||||
|
元数据完整性、editable 权限、options_json 等自检
|
||||||
|
- `db/timestamp_columns.py`
|
||||||
|
标准时间字段 trigger 与维护约定(`created_at` / `updated_at`,Unix 秒级)
|
||||||
|
|
||||||
不要在 `db` 层里:
|
不要在 `db` 层里:
|
||||||
|
|
||||||
@@ -422,6 +521,45 @@ metadata:
|
|||||||
- 打印用户提示
|
- 打印用户提示
|
||||||
- 拼接业务流程
|
- 拼接业务流程
|
||||||
|
|
||||||
|
## 11.5 Skill Action manifest(可选)
|
||||||
|
|
||||||
|
若技能需要出现在宿主**数据管理按钮**、**技能详情**、**定时任务**或 **Agent 直接调用**中,提供 `assets/actions.json`。
|
||||||
|
|
||||||
|
- **运行时金标准**见 [`SKILL_ACTION_RUNTIME.md`](SKILL_ACTION_RUNTIME.md)(sync/async、任务中心、Agent 禁令)
|
||||||
|
- 字段契约见 [`references/ACTIONS.md`](../references/ACTIONS.md)
|
||||||
|
- JSON Schema 见 [`assets/schemas/skill-actions.schema.json`](../assets/schemas/skill-actions.schema.json)
|
||||||
|
- 模板最小示例仅暴露 `health` / `version` / `config-path`(均显式 `executionProfile: "sync"`)
|
||||||
|
- 含浏览器 RPA / 长耗时的业务技能:**必须**增加至少一条 `"executionProfile": "async"` 且 `placements` 含 `agent` 的 action(`POLICY-SKILL-ACTION-001`)
|
||||||
|
- 没有宿主直接操作需求时,**可以删除** `actions.json`;删除后勿保留失效引用
|
||||||
|
- 复制为新技能后,scaffold 会自动将 `your-skill-slug` 与 `your_skill_slug` 替换为新 slug(后者用于 `LOG_LOGGER_NAME` 等下划线形式);action 业务命令由技能作者自行调整
|
||||||
|
|
||||||
|
宿主执行方式:
|
||||||
|
|
||||||
|
```text
|
||||||
|
python {skillRoot}/scripts/main.py <command> <args...>
|
||||||
|
```
|
||||||
|
|
||||||
|
CLI 必须提供稳定退出码、结构化成功输出,以及 `ERROR:<CODE>:` / `HINT:` 错误契约(详见 `references/ACTIONS.md`)。
|
||||||
|
|
||||||
|
### 编排范式:单内核 + 多入口
|
||||||
|
|
||||||
|
长任务 / RPA 技能推荐拆分(勿把 `job_context` 塞进纯业务内核):
|
||||||
|
|
||||||
|
```text
|
||||||
|
execute_single_<biz>(...) # 鉴权 + 业务 + task_log;无 job_context
|
||||||
|
cmd_run(...) # 单条:job_context + execute_single + finish
|
||||||
|
cmd_run_pick(N, ...) # 批量顺序:job_context + for-loop + emit(progress) + finish(partial|success|failed)
|
||||||
|
cmd_stats() # 只读 sync:JSON,无 job_context
|
||||||
|
```
|
||||||
|
|
||||||
|
- 批量数量用 **`pickCount` / `--pick N` 参数**(任意正整数),不写死条数。
|
||||||
|
- **顺序执行、不并发**同一浏览器 Profile / account lease。
|
||||||
|
- 详情见 [`SKILL_ACTION_RUNTIME.md`](SKILL_ACTION_RUNTIME.md) §5–§6。
|
||||||
|
|
||||||
|
### 队列 + pick(可选)
|
||||||
|
|
||||||
|
有「待处理列表」时:队列表 `pending → processing → success/failed`;数据管理录入或 `import-*` 入库;`run --pick N` 消费;失败用 `reset` 回 `pending`(不自动重试)。Manifest 上配置 `*-run-pick`(async,toolbar/cron/agent)。
|
||||||
|
|
||||||
## 12. 如何接兄弟技能
|
## 12. 如何接兄弟技能
|
||||||
|
|
||||||
如果 skill 要依赖兄弟 skill,不要在业务代码里写死绝对路径。
|
如果 skill 要依赖兄弟 skill,不要在业务代码里写死绝对路径。
|
||||||
@@ -498,6 +636,14 @@ python scripts/main.py logs
|
|||||||
python scripts/main.py log-get 1
|
python scripts/main.py log-get 1
|
||||||
```
|
```
|
||||||
|
|
||||||
|
除 `health` / `version` / `logs` 外,执行一次 `run`(或你的主任务命令)后,应确认**统一日志文件**已生成并可检索:
|
||||||
|
|
||||||
|
```text
|
||||||
|
{JIANGCHANG_DATA_ROOT}/{JIANGCHANG_USER_ID}/logs/jiangchang.log
|
||||||
|
```
|
||||||
|
|
||||||
|
其中应能看到 `cli_start`、`task_start` 等带 `trace_id` / `skill_slug` 的行。路径与分层说明见 [`LOGGING.md`](LOGGING.md)。
|
||||||
|
|
||||||
### 4. 最后再验证真实业务
|
### 4. 最后再验证真实业务
|
||||||
|
|
||||||
比如:
|
比如:
|
||||||
@@ -516,12 +662,30 @@ python scripts/main.py <your-command>
|
|||||||
3. **写完 service 业务后**:从 `tests/samples/test_service_contract.py.sample` 复制一份做契约测试。
|
3. **写完 service 业务后**:从 `tests/samples/test_service_contract.py.sample` 复制一份做契约测试。
|
||||||
4. **接外部系统时**:写在 `tests/integration/`,并配合 `OPENCLAW_TEST_TARGET` 显式开启。
|
4. **接外部系统时**:写在 `tests/integration/`,并配合 `OPENCLAW_TEST_TARGET` 显式开启。
|
||||||
|
|
||||||
详见 `references/TESTING.md`。
|
详见 `TESTING.md`。
|
||||||
|
|
||||||
## 15. 发布到正式环境验证
|
## 15. 发布到正式环境验证
|
||||||
|
|
||||||
|
### release workflow 通道
|
||||||
|
|
||||||
|
`.github/workflows/release_skill.yaml` 默认引用:
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
uses: client-jiangchang/jiangchang-platform-kit/.github/workflows/reusable-release-skill.yaml@main
|
||||||
|
```
|
||||||
|
|
||||||
|
约定如下:
|
||||||
|
|
||||||
|
- `jiangchang-platform-kit` 的 `main` 分支即稳定通道;kit 的开发、测试、验证须在合并 `main` 前完成。
|
||||||
|
- skill **不需要**锁定 workflow 版本(不要改为 `@v1`、`@v1.0.x` 等)。
|
||||||
|
- skill 运行时最低能力要求通过 `SKILL.md` 的 `metadata.openclaw.platform_kit_min_version` 声明。
|
||||||
|
|
||||||
在执行 `release.ps1` 之前,必须先在本地确认 `python tests/run_tests.py -v` 通过。测试不通过不得发起正式发布。
|
在执行 `release.ps1` 之前,必须先在本地确认 `python tests/run_tests.py -v` 通过。测试不通过不得发起正式发布。
|
||||||
|
|
||||||
|
`release.ps1` 会在 **auto commit 之前**、推送 tag 之前,对**当前工作区**自动执行默认测试(`python tests/run_tests.py -v`);失败则中止 release,不会留下失败状态的自动提交。仅紧急排障可使用 `-SkipTests` 跳过(会打印黄色警告)。`-DryRun` 预览发布时**仍会实际运行**默认测试,便于在不打 tag 的情况下验证门禁。
|
||||||
|
|
||||||
|
新增或调整开发规范时,须同步 [`development/POLICY_MATRIX.md`](POLICY_MATRIX.md) 与 [`tests/test_development_policy_guard.py`](../tests/test_development_policy_guard.py)(或注明由既有测试覆盖)。
|
||||||
|
|
||||||
当本地开发、自测和联调完成后,还需要把 skill 发布到正式环境做一次完整验证。建议技术人员严格按下面顺序执行,不要跳步。
|
当本地开发、自测和联调完成后,还需要把 skill 发布到正式环境做一次完整验证。建议技术人员严格按下面顺序执行,不要跳步。
|
||||||
|
|
||||||
### 第一步:在 skill 根目录执行 `release.ps1`
|
### 第一步:在 skill 根目录执行 `release.ps1`
|
||||||
@@ -573,7 +737,7 @@ python scripts/main.py <your-command>
|
|||||||
如果工作流失败,不要继续做平台验证,而应该先回到代码仓库排查,例如:
|
如果工作流失败,不要继续做平台验证,而应该先回到代码仓库排查,例如:
|
||||||
|
|
||||||
- 发布脚本是否正常推送
|
- 发布脚本是否正常推送
|
||||||
- `SKILL.md`、`scripts/`、`references/` 是否齐全
|
- `SKILL.md`、根 `README.md`、`scripts/`、`references/`、`development/` 是否齐全
|
||||||
- 工作流文件是否存在
|
- 工作流文件是否存在
|
||||||
- 发布包结构是否符合模板规范
|
- 发布包结构是否符合模板规范
|
||||||
|
|
||||||
@@ -634,18 +798,28 @@ python scripts/main.py <your-command>
|
|||||||
每个新 skill 发布前,建议技术人员逐条确认:
|
每个新 skill 发布前,建议技术人员逐条确认:
|
||||||
|
|
||||||
- [ ] 目录结构符合当前模板
|
- [ ] 目录结构符合当前模板
|
||||||
|
- [ ] slug 符合 [`NAMING.md`](NAMING.md)(verb-noun-platform)
|
||||||
|
- [ ] 目录名、`SKILL.md` slug、`constants.SKILL_SLUG` 三者一致
|
||||||
- [ ] `SKILL.md` 中 slug、名称、描述都已替换
|
- [ ] `SKILL.md` 中 slug、名称、描述都已替换
|
||||||
- [ ] `scripts/util/constants.py` 已修改
|
- [ ] `scripts/util/constants.py` 已修改
|
||||||
- [ ] `references/CLI.md` 示例命令已改成真实命令
|
- [ ] `../references/CLI.md` 示例命令已改成真实命令
|
||||||
- [ ] `service` 下的核心业务文件(如 `task_service.py`)已按领域改名并实现
|
- [ ] `service` 下的核心业务文件(如 `task_service.py`)已按领域改名并实现
|
||||||
- [ ] 没有残留旧平台名
|
- [ ] 没有残留旧平台名
|
||||||
- [ ] `health` / `version` 可运行
|
- [ ] `health` / `version` 可运行
|
||||||
- [ ] `.gitignore` 生效,没有把 `__pycache__` 提交进去
|
- [ ] `.gitignore` 生效,没有把 `__pycache__` 提交进去
|
||||||
- [ ] `release.ps1` 存在
|
- [ ] `release.ps1` 存在(发布前会自动跑 `python tests/run_tests.py -v`;失败不得打 tag)
|
||||||
- [ ] `.github/workflows/release_skill.yaml` 存在
|
- [ ] `.github/workflows/release_skill.yaml` 存在
|
||||||
- [ ] `python tests/run_tests.py -v` 全部通过
|
- [ ] `python tests/run_tests.py -v` 全部通过(含 `test_development_policy_guard.py`)
|
||||||
|
- [ ] 主任务入口(如 `cmd_run`)有关键日志:`task_start`、`task_log_saved`;失败路径使用 `logger.exception`
|
||||||
|
- [ ] 日志不泄露 password / token / cookie 等敏感明文(见 [`LOGGING.md`](LOGGING.md))
|
||||||
|
- [ ] 新增 hard 规范已写入 `development/POLICY_MATRIX.md` 并有对应自动检测
|
||||||
|
- [ ] `scripts/**/*.py` 单文件 **< 1000 行**(PyArmor 试用/免费模式限制;推荐 < 900 行)
|
||||||
|
- [ ] 源码/文档/配置为 **UTF-8 without BOM**(Python 文件不得含 `U+FEFF`)
|
||||||
- [ ] 没有新增默认必跑测试访问真实网络或浏览器
|
- [ ] 没有新增默认必跑测试访问真实网络或浏览器
|
||||||
- [ ] 如有 integration 测试需求,已写在 `tests/integration/` 下并保持 `.sample` 后缀
|
- [ ] 如有 integration 测试需求,已写在 `tests/integration/` 下并保持 `.sample` 后缀
|
||||||
|
- [ ] 本仓库**不是** skill-template 的误复制(根目录**无** `.openclaw-skill-template`)
|
||||||
|
- [ ] `git remote -v` 指向**本技能**远端,URL 不含 skill-template 仓库名
|
||||||
|
- [ ] `git log` 首条提交属于本技能(非模板历史)
|
||||||
|
|
||||||
## 17. 常见错误
|
## 17. 常见错误
|
||||||
|
|
||||||
@@ -673,7 +847,7 @@ python scripts/main.py <your-command>
|
|||||||
|
|
||||||
- `task_service.py`
|
- `task_service.py`
|
||||||
- `sibling_bridge.py`
|
- `sibling_bridge.py`
|
||||||
- `references/CLI.md`
|
- `../references/CLI.md`
|
||||||
|
|
||||||
### 错误 3:把业务逻辑写进 CLI
|
### 错误 3:把业务逻辑写进 CLI
|
||||||
|
|
||||||
@@ -698,16 +872,71 @@ python scripts/main.py <your-command>
|
|||||||
现在的新模板原则是:
|
现在的新模板原则是:
|
||||||
|
|
||||||
- 不做旧结构兼容
|
- 不做旧结构兼容
|
||||||
- 统一走 `references/` + `scripts/main.py`
|
- 用户说明在根 `README.md`,Agent 资料在 `references/`,开发规范在 `development/`
|
||||||
|
- 统一走 `scripts/main.py` 作为 CLI 入口
|
||||||
|
|
||||||
|
### 错误 5:平台放 slug 最前
|
||||||
|
|
||||||
|
表现:
|
||||||
|
|
||||||
|
- slug 形如 `erp-download-settlement-report`(平台段错误地放在最前)
|
||||||
|
- 语义校验或 `test_slug_naming` 失败
|
||||||
|
|
||||||
|
要改:
|
||||||
|
|
||||||
|
- 动词放首段,平台放末段,例如 `download-settlement-report-erp`
|
||||||
|
- 见 [`NAMING.md`](NAMING.md)
|
||||||
|
|
||||||
|
### 错误 6:noun 段过长导致超 48 字符
|
||||||
|
|
||||||
|
表现:
|
||||||
|
|
||||||
|
- slug 超过 48 字符
|
||||||
|
- 中间名词段堆砌过多
|
||||||
|
|
||||||
|
要改:
|
||||||
|
|
||||||
|
- 精简 noun-phrase,保留核心业务含义
|
||||||
|
- 见 [`NAMING.md`](NAMING.md) §3 硬约束
|
||||||
|
|
||||||
|
### 错误 7:只改目录名未改 slug
|
||||||
|
|
||||||
|
表现:
|
||||||
|
|
||||||
|
- 与「错误 1」相同,但目录已改名而 `SKILL.md` / `constants.py` 仍为占位
|
||||||
|
|
||||||
|
要检查:
|
||||||
|
|
||||||
|
- `SKILL.md` 的 `metadata.openclaw.slug`
|
||||||
|
- `scripts/util/constants.py` 的 `SKILL_SLUG`
|
||||||
|
|
||||||
|
### 错误 8:复制模板时带走了 `.git`
|
||||||
|
|
||||||
|
表现:
|
||||||
|
|
||||||
|
- `git remote -v` 仍显示 skill-template 的 origin
|
||||||
|
- push 到错误仓库,或 log 里全是模板历史
|
||||||
|
- 新技能目录里仍有 `.openclaw-skill-template`
|
||||||
|
|
||||||
|
修复:
|
||||||
|
|
||||||
|
```powershell
|
||||||
|
Remove-Item -Recurse -Force .git
|
||||||
|
Remove-Item -Force .openclaw-skill-template -ErrorAction SilentlyContinue
|
||||||
|
git init
|
||||||
|
git remote add origin <新技能仓库 URL>
|
||||||
|
```
|
||||||
|
|
||||||
|
以后复制请优先用 `tools/scaffold_skill.ps1`(见 [`tools/README.md`](../tools/README.md))。
|
||||||
|
|
||||||
## 18. 推荐开发顺序总结
|
## 18. 推荐开发顺序总结
|
||||||
|
|
||||||
如果让一个新人照着做,我建议他按这个顺序:
|
如果让一个新人照着做,我建议他按这个顺序:
|
||||||
|
|
||||||
1. 复制模板并改目录名
|
1. 按 [`NAMING.md`](NAMING.md) 确定 slug,复制模板并改目录名
|
||||||
2. 改 `SKILL.md`
|
2. 改 `SKILL.md` 与根 `README.md`
|
||||||
3. 改 `scripts/util/constants.py`
|
3. 改 `scripts/util/constants.py`
|
||||||
4. 改 `references/`
|
4. 改 `references/` 与 `development/`
|
||||||
5. 改 `scripts/cli/app.py`
|
5. 改 `scripts/cli/app.py`
|
||||||
6. 改 `scripts/service/`
|
6. 改 `scripts/service/`
|
||||||
7. 跑 `python tests/run_tests.py -v`
|
7. 跑 `python tests/run_tests.py -v`
|
||||||
@@ -721,7 +950,7 @@ python scripts/main.py <your-command>
|
|||||||
|
|
||||||
- 目录结构统一
|
- 目录结构统一
|
||||||
- 入口统一为 `scripts/main.py`
|
- 入口统一为 `scripts/main.py`
|
||||||
- 文档统一放 `references/`
|
- 用户说明在根 `README.md`,Agent 资料在 `references/`,开发规范在 `development/`
|
||||||
- 业务核心逻辑统一放 `scripts/service/`
|
- 业务核心逻辑统一放 `scripts/service/`
|
||||||
- 不再使用旧模板历史结构
|
- 不再使用旧模板历史结构
|
||||||
|
|
||||||
312
development/LOGGING.md
Normal file
312
development/LOGGING.md
Normal file
@@ -0,0 +1,312 @@
|
|||||||
|
# 日志规范
|
||||||
|
|
||||||
|
本文件是 skill 日志规范的**权威入口**,面向技术人员与 AI 编程代理。涉及长任务、RPA、外部系统对接的技能,**必须**在实现前通读本文,并在 code review 时对照「必打日志节点清单」人工检查。
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 1. 为什么日志是必需的
|
||||||
|
|
||||||
|
OpenClaw 技能常在客户电脑上异步执行,且大量依赖 RPA、浏览器、ERP、微信等外部系统。这类任务具备以下特征:
|
||||||
|
|
||||||
|
- **耗时长**:单次 run 可能数分钟甚至更久,stdout 长时间无输出不代表卡死。
|
||||||
|
- **失败点多**:网络抖动、页面改版、登录态失效、验证码、U 盾等都会导致中途失败。
|
||||||
|
- **环境差异大**:客户 OS、浏览器版本、代理、杀毒软件、屏幕分辨率各不相同,无法复现开发机行为。
|
||||||
|
- **必须离线排查**:技术支持往往无法远程登录客户电脑,只能依赖本地落盘的日志与 artifacts。
|
||||||
|
|
||||||
|
日志用于定位:
|
||||||
|
|
||||||
|
| 维度 | 说明 |
|
||||||
|
|------|------|
|
||||||
|
| 失败阶段 | 任务停在鉴权、取号、打开页面、提交还是等待结果 |
|
||||||
|
| 输入范围 | `task_type`、`target_id`、`input_id`、`batch_id` |
|
||||||
|
| 外部系统状态 | HTTP 状态、RPA 步骤、兄弟技能返回码 |
|
||||||
|
| 耗时 | `elapsed_ms`、批量 `current/total` |
|
||||||
|
| 重试与人工介入 | 第几次重试、是否等待验证码 / U 盾 |
|
||||||
|
| 存证路径 | 截图、录屏、`video_log`、失败 artifacts |
|
||||||
|
|
||||||
|
**没有关键节点日志 = 客户现场不可排障。** 不要把 `print` 当作日志;stdout 面向用户与 Agent,文件日志与 Run Journal 面向运维。
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 2. 日志分层
|
||||||
|
|
||||||
|
技能日志分四层,职责互不替代:
|
||||||
|
|
||||||
|
### 2.1 统一文件日志(开发 / 运维排查)
|
||||||
|
|
||||||
|
**来源**:`jiangchang_skill_core.unified_logging`(技能内通过 `util.logging_config` 薄封装 import)。
|
||||||
|
|
||||||
|
| API | 用途 |
|
||||||
|
|-----|------|
|
||||||
|
| `setup_skill_logging` | CLI 入口启动时初始化(`cli/app.py` 的 `main()`) |
|
||||||
|
| `get_skill_logger` | 获取带 `skill_slug` 的 logger |
|
||||||
|
| `subprocess_env_with_trace` | 子进程 / 兄弟技能调用时传递 `trace_id` |
|
||||||
|
|
||||||
|
**路径**:
|
||||||
|
|
||||||
|
```text
|
||||||
|
{JIANGCHANG_DATA_ROOT}/{JIANGCHANG_USER_ID}/logs/jiangchang.log
|
||||||
|
```
|
||||||
|
|
||||||
|
**特征**:每行带 `trace_id`、`skill_slug`;适合 grep、离线打包发给技术支持。
|
||||||
|
|
||||||
|
### 2.2 Activity / Run Journal(宿主 UI 与用户可见进度)
|
||||||
|
|
||||||
|
**来源**:`jiangchang_skill_core.activity`
|
||||||
|
|
||||||
|
| API | 用途 |
|
||||||
|
|-----|------|
|
||||||
|
| `emit` | 推送用户可读进度(不写 stdout);进入步骤前自动步骤闸门(SRCP) |
|
||||||
|
| `step` | 结构化步骤 |
|
||||||
|
| `finish` | 任务结束;**唯一**写单行 result JSON 到 stdout 的场景 |
|
||||||
|
| `job_context` | 包裹 `cmd_run`;未捕获异常 / `JobStopped` 时自动 `finish` |
|
||||||
|
| `rpa_step` | RPA 专用步骤:闸门 + ▶/✓ emit |
|
||||||
|
| `interruptible_sleep` | RPA 等待(替代裸 `asyncio.sleep`),可暂停/停止 |
|
||||||
|
| `checkpoint` | 长循环内 consult control(一般由 kit 自动调用) |
|
||||||
|
|
||||||
|
**路径**:
|
||||||
|
|
||||||
|
```text
|
||||||
|
{JIANGCHANG_DATA_ROOT}/.jiangchang/runs/{job_id}.jsonl
|
||||||
|
```
|
||||||
|
|
||||||
|
**约定**:
|
||||||
|
|
||||||
|
- `emit` **不写 stdout**;长任务应持续 emit,避免用户以为卡死。
|
||||||
|
- `finish` 才输出单行 result JSON 供宿主解析。
|
||||||
|
- RPA 类技能:`RpaVideoSession.add_step` 会自动同步 activity(无需重复手写每步 emit)。
|
||||||
|
- **禁止**自建 `scripts/util/progress.py` 或向 stdout 打印 `type:progress` JSON;进度只走 Run Journal。
|
||||||
|
- 宿主通过 `{job_id}.control.json` 下发暂停/继续/停止;技能侧由 platform-kit 在步骤闸门自动处理(见 §2.5)。
|
||||||
|
|
||||||
|
### 2.5 步骤控制(SRCP v1,platform-kit >= 1.2.0)
|
||||||
|
|
||||||
|
**权威行为定义在本文**;platform-kit README 为实现细节参考。
|
||||||
|
|
||||||
|
| 通道 | 方向 | 载体 |
|
||||||
|
|------|------|------|
|
||||||
|
| 进度 | 技能 → 宿主 | Run Journal(`emit` / `rpa_step` / `add_step`) |
|
||||||
|
| 结果 | 技能 → 宿主 | `finish()` → Journal 终态 + stdout 一行 result |
|
||||||
|
| 控制 | 宿主 → 技能 | `{JIANGCHANG_DATA_ROOT}/.jiangchang/runs/{job_id}.control.json` |
|
||||||
|
|
||||||
|
`control.json` 的 `command` 只认:`none` | `pause` | `resume` | `stop`。
|
||||||
|
|
||||||
|
技能作者三件事:
|
||||||
|
|
||||||
|
1. `cmd_run` 外包 `with job_context(skill=SKILL_SLUG):`
|
||||||
|
2. 关键步骤 `emit(...)` 或 `@rpa_step` / `video.add_step`
|
||||||
|
3. 每个出口 `finish(status=..., message=..., skill=SKILL_SLUG, **fields)` — **不要**手写多行 JSON 结果
|
||||||
|
|
||||||
|
**单内核多入口**:`execute_single_*` 不含 `job_context`;仅 `cmd_run` / `cmd_run_pick` 包裹。批量 pick 循环内 `checkpoint()` + `emit(..., progress=...)`;部分失败用 `finish(status="partial")`。完整范式见 [`SKILL_ACTION_RUNTIME.md`](SKILL_ACTION_RUNTIME.md)。
|
||||||
|
|
||||||
|
RPA 等待用 `interruptible_sleep`,**不要**裸 `asyncio.sleep`(否则暂停响应滞后)。
|
||||||
|
|
||||||
|
暂停在**步骤边界**生效(当前 `@rpa_step` / `emit` 执行完后、下一步开始前),与影刀「当前指令完成后暂停」一致。
|
||||||
|
|
||||||
|
Journal 会写入 `type: lifecycle` 事件(`paused` / `resumed` / `stopping`),供宿主任务中心显示状态。
|
||||||
|
|
||||||
|
|
||||||
|
### 2.3 `task_logs`(任务结果审计)
|
||||||
|
|
||||||
|
**来源**:`db/task_logs_repository.save_task_log`
|
||||||
|
|
||||||
|
- 适合 `python scripts/main.py logs` / `log-get <id>` 查询。
|
||||||
|
- 记录任务最终状态、`result_summary`(可含 `video_path`、`video_log` 等)。
|
||||||
|
- 面向「这次任务成功还是失败、摘要是什么」,不是逐步 trace。
|
||||||
|
- **鉴权失败与未捕获异常也必须写入 `task_logs`**,保证 `logs` / `log-get` 排查链不断(见模板 `cmd_run` 示范)。
|
||||||
|
|
||||||
|
表结构与字段见 [`references/SCHEMA.md`](../references/SCHEMA.md)。
|
||||||
|
|
||||||
|
### 2.4 RPA video step / artifacts(用户可见动作与存证)
|
||||||
|
|
||||||
|
**来源**:`RpaVideoSession`、`rpa-artifacts/` 目录
|
||||||
|
|
||||||
|
- video artifact 是**标准可启用能力**,**不保证默认每次生成 MP4**(模板默认 `OPENCLAW_RECORD_VIDEO=0`)。
|
||||||
|
- **`OPENCLAW_RECORD_VIDEO=0`**:不生成最终录屏,但 RPA / 长任务仍须保留 `RpaVideoSession`、`video.add_step` 与 Activity 进度同步。
|
||||||
|
- **`OPENCLAW_RECORD_VIDEO=1`**:`result_summary` 应包含 `video_path`、`raw_video`、`video_log`、`video_warnings` 等字段;CLI 可打印录屏路径与诊断。
|
||||||
|
- 用户可见的中文步骤(「打开登录页」「点击提交」)。
|
||||||
|
- 失败截图:`{数据目录}/rpa-artifacts/{batch_id}/...`(受 `OPENCLAW_ARTIFACTS_ON_FAILURE` 控制,默认开)
|
||||||
|
|
||||||
|
详见 [`RPA.md`](RPA.md) §5 存证与录屏规范。
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 3. 必打日志节点清单
|
||||||
|
|
||||||
|
以下节点应在**生产代码**中有对应 `log.info` / `log.warning` / `log.exception`(或 Activity / RPA step)。复制模板后按业务增删,但**不可整体省略**。
|
||||||
|
|
||||||
|
| 阶段 | 建议日志 / emit |
|
||||||
|
|------|-----------------|
|
||||||
|
| CLI 入口启动 | `cli_start`(`cli/app.py`,已由模板提供) |
|
||||||
|
| 参数解析完成 | `run_args target_id=... input_id=...` |
|
||||||
|
| 鉴权开始 | `entitlement_check_start` |
|
||||||
|
| 鉴权成功 | `entitlement_ok` |
|
||||||
|
| 鉴权失败 | `entitlement_failed`(`warning`) |
|
||||||
|
| 任务开始 | `task_start task_type=... target_id=... input_id=... batch_id=...` + `emit("开始处理任务")` |
|
||||||
|
| adapter / test target 选择 | `adapter_selected adapter=... system=...` |
|
||||||
|
| 账号 / profile / lease 获取 | `account_acquired account_id=...` |
|
||||||
|
| 账号 / lease 释放 | `account_released account_id=...` |
|
||||||
|
| 外部 API 开始 | `external_call_start system=... operation=...` |
|
||||||
|
| 外部 API 结束 | `external_call_done system=... operation=... elapsed_ms=... status=...` |
|
||||||
|
| 兄弟技能调用开始 / 结束 | 同上,或 `sibling_call_start/done skill_slug=...` |
|
||||||
|
| RPA 操作开始 / 结束 | `rpa_start` / `rpa_done` + `video.add_step` 中文步骤 |
|
||||||
|
| 打开页面 | step:「打开 xxx 页面」 |
|
||||||
|
| 检查登录 | step + log |
|
||||||
|
| 定位输入框 / 点击提交 | step + log |
|
||||||
|
| 等待结果 | step + log(含 timeout 预期) |
|
||||||
|
| 批量循环 | `batch_progress current=... total=...` |
|
||||||
|
| 重试 | `retry_attempt n=... reason=...`(`warning`) |
|
||||||
|
| 跳过 / 部分失败 | `skipped` / `partial_failure`(`warning`) |
|
||||||
|
| 人工介入 | `human_intervention type=captcha|login|ukey|otp`(`warning`) |
|
||||||
|
| 失败截图 / 视频 | `artifact_saved artifact_path=...` / `video_path=...` |
|
||||||
|
| 任务成功 | `task_success ...` + `task_log_saved status=success` |
|
||||||
|
| 任务失败 | `task_failed ...`(`exception` 保留 traceback)+ `task_log_saved status=failed` |
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 4. 字段规范
|
||||||
|
|
||||||
|
推荐在 `log.info` 等消息中使用 **key=value** 结构化字段,便于 grep:
|
||||||
|
|
||||||
|
| 字段 | 含义 |
|
||||||
|
|------|------|
|
||||||
|
| `task_type` | 任务类型(与 `task_logs.task_type` 对齐) |
|
||||||
|
| `target_id` | 目标标识(平台、店铺、报表类型等) |
|
||||||
|
| `input_id` | 输入记录 ID |
|
||||||
|
| `batch_id` | 批次 / 录屏批次 |
|
||||||
|
| `stage` | 阶段:`auth` / `run` / `cleanup` |
|
||||||
|
| `system` | 外部系统名:`erp` / `wechat` / `1688` |
|
||||||
|
| `operation` | 操作名:`submit_order` / `fetch_report` |
|
||||||
|
| `adapter` | adapter 档位:`mock` / `simulator_api` / `real_api` |
|
||||||
|
| `elapsed_ms` | 耗时毫秒 |
|
||||||
|
| `status` | `ok` / `failed` / `timeout` / `skipped` |
|
||||||
|
| `error_code` | 业务错误码(非 Python 异常名) |
|
||||||
|
| `artifact_path` | 截图 / 附件路径 |
|
||||||
|
| `video_path` | 录屏成品路径 |
|
||||||
|
|
||||||
|
示例:
|
||||||
|
|
||||||
|
```text
|
||||||
|
external_call_done system=erp operation=download_report elapsed_ms=4523 status=ok
|
||||||
|
batch_progress current=7 total=20 target_id=store-001
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 5. 日志级别规范
|
||||||
|
|
||||||
|
| 级别 | 使用场景 |
|
||||||
|
|------|----------|
|
||||||
|
| **info** | 关键阶段、成功路径、外部调用开始/结束、批量进度、`task_log_saved` |
|
||||||
|
| **warning** | 可恢复问题、重试、跳过、人工介入、部分失败、鉴权失败(业务可预期) |
|
||||||
|
| **exception** | 异常路径;**必须**用 `log.exception(...)` 保留 traceback |
|
||||||
|
| **debug** | 低频诊断;**默认不依赖 debug 才能排查问题** |
|
||||||
|
|
||||||
|
原则:**生产排障应主要靠 info + warning + exception**,不要把 debug 当作唯一线索。
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 6. 敏感信息红线
|
||||||
|
|
||||||
|
### 禁止记录原始值
|
||||||
|
|
||||||
|
以下字段**不得**以明文出现在日志、Activity、task_logs 的 `error_msg` / `result_summary`、stdout:
|
||||||
|
|
||||||
|
- `password`、`token`、`cookie`、`secret`、`api_key`、`authorization`
|
||||||
|
- 验证码、动态口令全文
|
||||||
|
- 身份证、银行卡、手机号**完整值**
|
||||||
|
|
||||||
|
### 建议记录的安全替代
|
||||||
|
|
||||||
|
| 替代字段 | 示例 |
|
||||||
|
|----------|------|
|
||||||
|
| `credential_ref` | vault / account-manager 引用 ID |
|
||||||
|
| `account_id` | 账号表主键 |
|
||||||
|
| `masked_user` | `138****8000` |
|
||||||
|
| `domain` | `erp.example.com` |
|
||||||
|
| `last4` | 卡号后四位 |
|
||||||
|
| `hash` / `short_id` | 内容摘要 |
|
||||||
|
|
||||||
|
变量名可以叫 `token`,但日志里不能出现 `token=eyJhbG...` 或 f-string 拼接明文。
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 7. 推荐代码示例(service 层)
|
||||||
|
|
||||||
|
```python
|
||||||
|
from util.logging_config import get_skill_logger
|
||||||
|
from jiangchang_skill_core.activity import emit, finish, job_context, rpa_step, interruptible_sleep
|
||||||
|
|
||||||
|
from util.constants import SKILL_SLUG
|
||||||
|
|
||||||
|
log = get_skill_logger()
|
||||||
|
|
||||||
|
def cmd_run(target=None, input_id=None):
|
||||||
|
task_type = "your_task"
|
||||||
|
with job_context(skill=SKILL_SLUG):
|
||||||
|
log.info(
|
||||||
|
"task_start task_type=%s target_id=%s input_id=%s",
|
||||||
|
task_type, target, input_id,
|
||||||
|
)
|
||||||
|
emit("开始处理任务", skill=SKILL_SLUG, stage="run")
|
||||||
|
try:
|
||||||
|
ok, reason = check_entitlement(SKILL_SLUG)
|
||||||
|
if not ok:
|
||||||
|
log.warning("entitlement_failed task_type=%s reason=%s", task_type, reason)
|
||||||
|
finish(status="failed", message=reason, skill=SKILL_SLUG, error_code="ENTITLEMENT_DENIED")
|
||||||
|
return 1
|
||||||
|
|
||||||
|
t0 = time.monotonic()
|
||||||
|
# ... 外部调用 / RPA:@rpa_step 或 video.add_step;等待用 interruptible_sleep ...
|
||||||
|
elapsed_ms = int((time.monotonic() - t0) * 1000)
|
||||||
|
log.info(
|
||||||
|
"external_call_done system=%s operation=%s elapsed_ms=%d status=%s",
|
||||||
|
"erp", "submit", elapsed_ms, "ok",
|
||||||
|
)
|
||||||
|
|
||||||
|
tlr.save_task_log(..., status="success", ...)
|
||||||
|
log.info("task_log_saved task_type=%s status=success", task_type)
|
||||||
|
finish(status="success", message="任务完成", skill=SKILL_SLUG)
|
||||||
|
return 0
|
||||||
|
except Exception:
|
||||||
|
log.exception(
|
||||||
|
"task_failed task_type=%s target_id=%s input_id=%s",
|
||||||
|
task_type, target, input_id,
|
||||||
|
)
|
||||||
|
emit("任务失败,已记录诊断信息", type="warn", skill=SKILL_SLUG, stage="run")
|
||||||
|
tlr.save_task_log(..., status="failed", error_msg="...", ...)
|
||||||
|
finish(status="failed", message="任务执行异常", skill=SKILL_SLUG)
|
||||||
|
return 1
|
||||||
|
```
|
||||||
|
|
||||||
|
模板示范见 [`scripts/service/task_service.py`](../scripts/service/task_service.py) 的 `cmd_run`。
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 8. 错误排查顺序
|
||||||
|
|
||||||
|
客户现场或测试机排障,建议按以下顺序(由快到慢、由用户可见到运维细节):
|
||||||
|
|
||||||
|
1. **CLI stdout** — 是否有 `ERROR:<CODE>:`、`HINT:`、模板提示文案。
|
||||||
|
2. **`python scripts/main.py logs`** — 最近任务是否写入 `task_logs`、status 与 `error_msg`。
|
||||||
|
3. **`python scripts/main.py log-get <id>`** — 单条 JSON,含 `result_summary`(video 路径等)。
|
||||||
|
4. **统一日志文件** — `{JIANGCHANG_DATA_ROOT}/{JIANGCHANG_USER_ID}/logs/jiangchang.log`(grep `trace_id`、`task_failed`)。
|
||||||
|
5. **Run Journal** — `{JIANGCHANG_DATA_ROOT}/.jiangchang/runs/{job_id}.jsonl`(宿主 UI 进度来源)。
|
||||||
|
6. **RPA artifacts** — `rpa-artifacts/` 失败截图;若 `OPENCLAW_RECORD_VIDEO=1` 还有录屏 MP4、`video_log`。未开启录屏时可能只有截图或日志,需要完整录屏请在用户 `.env` 中设置 `OPENCLAW_RECORD_VIDEO=1`。
|
||||||
|
|
||||||
|
长时间无 stdout **不代表卡死**;先看 Run Journal / Activity,再看统一日志。
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 9. 与现有文档关系
|
||||||
|
|
||||||
|
| 文档 | 关系 |
|
||||||
|
|------|------|
|
||||||
|
| [`RUNTIME.md`](RUNTIME.md) | 统一日志能力来源、`util.logging_config` 薄封装、数据根约定 |
|
||||||
|
| [`TESTING.md`](TESTING.md) | 日志相关 hard policy 测试(`test_development_policy_guard.py`) |
|
||||||
|
| [`RPA.md`](RPA.md) | RPA step、`RpaVideoSession`、video / artifacts 标准 |
|
||||||
|
| [`SCHEMA.md`](../references/SCHEMA.md) | `task_logs` 表结构与查询命令 |
|
||||||
|
| [`CLI.md`](../references/CLI.md) | `logs` / `log-get` 命令与手工排查入口 |
|
||||||
|
| [`CONFIG.md`](CONFIG.md) | 敏感配置不进日志;`.env` 边界 |
|
||||||
|
| [`DEVELOPMENT.md`](DEVELOPMENT.md) | `service/` 层日志职责与发布前检查清单 |
|
||||||
|
| [`SKILL_ACTION_RUNTIME.md`](SKILL_ACTION_RUNTIME.md) | 任务中心 / async Job 与 `job_context` 边界 |
|
||||||
|
| [`POLICY_MATRIX.md`](POLICY_MATRIX.md) | 可自动检测的 logging hard policy 索引 |
|
||||||
237
development/NAMING.md
Normal file
237
development/NAMING.md
Normal file
@@ -0,0 +1,237 @@
|
|||||||
|
# 技能 slug 命名规范(verb-noun-platform)
|
||||||
|
|
||||||
|
> 本文是匠厂**新业务 skill** 的 slug / Git 仓库名 / 目录名命名权威说明。复制 `skill-template` 出去开发新技能前**必读**;Agent 在开发者询问命名或 slug 时也应先读本文。
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 1. 适用范围
|
||||||
|
|
||||||
|
| 范围 | 说明 |
|
||||||
|
|------|------|
|
||||||
|
| ✅ **适用** | 从 `skill-template` **复制出去的新技能** |
|
||||||
|
| ❌ **不追溯** | 已上线历史技能(如 `account-manager`、`reconcile-finance`、`scrape-contacts-lianjia` 等)保持不动 |
|
||||||
|
| ❌ **不修改** | 其他仓库(尤其 `account-manager`)不在本文约束范围内 |
|
||||||
|
| ✅ **模板占位** | 模板本体的 `your-skill-slug` 占位必须继续通过全部测试 |
|
||||||
|
|
||||||
|
**新技能勿仿**历史 2 段无平台形态(如 `reconcile-finance`)或不符合 verb-noun-platform 的旧 repo 名;校验器对历史 repo **不追溯**,但规范中明确标注为反模式。
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 2. 命名格式
|
||||||
|
|
||||||
|
标准形态(约 95% 新技能):
|
||||||
|
|
||||||
|
```text
|
||||||
|
{verb}-{noun-phrase}-{platform}
|
||||||
|
```
|
||||||
|
|
||||||
|
| 部分 | 规则 |
|
||||||
|
|------|------|
|
||||||
|
| **verb** | 1 个词,来自 [动词白名单](#5-动词白名单) |
|
||||||
|
| **noun-phrase** | 1~3 个词(按 `-` 分段),描述业务对象 |
|
||||||
|
| **platform** | 1 个词,来自 [平台白名单](#6-平台白名单);可含连字符(如 `single-window`) |
|
||||||
|
|
||||||
|
**展示层分工不变:**
|
||||||
|
|
||||||
|
| 字段 | 语言 | 示例 |
|
||||||
|
|------|------|------|
|
||||||
|
| slug / repo / 目录 / `SKILL_SLUG` / DB 文件名 | **英文** kebab-case | `download-settlement-report-amazon` |
|
||||||
|
| `SKILL.md` 的 `name`、`description`、根 `README.md` | **中文** | `亚马逊结算报表下载` |
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 3. 硬约束(新技能强制校验)
|
||||||
|
|
||||||
|
| 约束 | 值 |
|
||||||
|
|------|-----|
|
||||||
|
| 格式 | `{verb}-{noun-phrase}-{platform}`(标准型,默认) |
|
||||||
|
| 总段数(按 `-` 分割) | **3~5 段**(标准型);scope 型见 [§4](#4-三类合法形态) |
|
||||||
|
| 总长度 | **≤ 48 字符** |
|
||||||
|
| 字符集 | 小写字母、数字、连字符;kebab-case |
|
||||||
|
| 禁止 | 中文 slug、下划线、**平台放最前**(如 `amazon-download-xxx`) |
|
||||||
|
|
||||||
|
实现校验见 `scripts/util/slug_naming.py`;`scripts/db/display_metadata_validator.py` 在 `SKILL.md` frontmatter 自检时调用。
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 4. 三类合法形态
|
||||||
|
|
||||||
|
### 4.1 标准型(95%)
|
||||||
|
|
||||||
|
`verb-noun-platform` 或 `verb-noun-noun-platform`(3~5 段)
|
||||||
|
|
||||||
|
- 第 1 段 ∈ 动词白名单
|
||||||
|
- 最后 1 段(或 hyphenated 平台的最后 2 段)∈ 平台白名单
|
||||||
|
- 中间为 noun-phrase(1~3 段)
|
||||||
|
|
||||||
|
示例:
|
||||||
|
|
||||||
|
- `download-settlement-report-amazon`
|
||||||
|
- `add-product-shopee`
|
||||||
|
- `enter-sales-receipt-kingdee`
|
||||||
|
- `fill-export-declaration-single-window`
|
||||||
|
|
||||||
|
### 4.2 scope 型(少见)
|
||||||
|
|
||||||
|
`verb-noun-scope`
|
||||||
|
|
||||||
|
- 最后 1 段 ∈ scope 白名单:`all` | `batch` | `multi` | `default`
|
||||||
|
- 第 1 段 ∈ 动词白名单
|
||||||
|
- 总段数 3~5
|
||||||
|
|
||||||
|
示例:`generate-article-all`
|
||||||
|
|
||||||
|
> 说明:`geo-generate-article-all` 若作为新技能且不在豁免表、首段 `geo` 不在动词表,应写为 `generate-article-all`。
|
||||||
|
|
||||||
|
### 4.3 历史豁免(不校验语义)
|
||||||
|
|
||||||
|
以下 slug 跳过 verb/platform 语义校验(见 `LEGACY_EXEMPT_SLUGS`):
|
||||||
|
|
||||||
|
- `account-manager`
|
||||||
|
- `skill-template`
|
||||||
|
- `your-skill-slug` / `your_skill_slug`(模板占位)
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 5. 动词白名单
|
||||||
|
|
||||||
|
词表文件:[`assets/naming/verbs.txt`](../assets/naming/verbs.txt)(每行一词,便于后续扩展)
|
||||||
|
|
||||||
|
| 动词 | 动词 | 动词 | 动词 |
|
||||||
|
|------|------|------|------|
|
||||||
|
| scrape | receive | download | export |
|
||||||
|
| add | fill | enter | bind |
|
||||||
|
| withdraw | reconcile | verify | submit |
|
||||||
|
| query | process | reply | generate |
|
||||||
|
| draft | review | confirm | track |
|
||||||
|
| upload | match | triage | ship |
|
||||||
|
| reprice | | | |
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 6. 平台白名单
|
||||||
|
|
||||||
|
词表文件:[`assets/naming/platforms.txt`](../assets/naming/platforms.txt)
|
||||||
|
|
||||||
|
| 平台 | 平台 | 平台 |
|
||||||
|
|------|------|------|
|
||||||
|
| amazon | shopee | xinghang |
|
||||||
|
| alibaba | kingdee | icbc |
|
||||||
|
| pingpong | worldfirst | paypal |
|
||||||
|
| lianlian | single-window | export-rebate |
|
||||||
|
| etax | | |
|
||||||
|
|
||||||
|
scope 词表:[`assets/naming/scopes.txt`](../assets/naming/scopes.txt) — `all`、`batch`、`multi`、`default`
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 7. slug 与展示字段分工
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
---
|
||||||
|
name: 亚马逊结算报表下载 # 中文,用户可见
|
||||||
|
description: "从卖家中心下载结算报表并入库" # 中文说明
|
||||||
|
metadata:
|
||||||
|
openclaw:
|
||||||
|
slug: download-settlement-report-amazon # 英文机器标识
|
||||||
|
---
|
||||||
|
```
|
||||||
|
|
||||||
|
同步修改:
|
||||||
|
|
||||||
|
- 目录名
|
||||||
|
- `metadata.openclaw.slug`
|
||||||
|
- `scripts/util/constants.py` 中的 `SKILL_SLUG`
|
||||||
|
- 默认数据库路径 `{slug}/{slug}.db`
|
||||||
|
|
||||||
|
三者必须一致。
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 8. 与 account-manager 的关系
|
||||||
|
|
||||||
|
slug **末段 platform** 与 `account-manager` 中的 `platform_key` **语义对应**,但**不要求字符串完全相等**。
|
||||||
|
|
||||||
|
| slug 末段 (platform) | account-manager platform_key(示例) |
|
||||||
|
|----------------------|--------------------------------------|
|
||||||
|
| amazon | `amazon_sim` / `amazon` |
|
||||||
|
| shopee | `shopee` |
|
||||||
|
| kingdee | `kingdee` |
|
||||||
|
|
||||||
|
新技能若需账号下发,在 `REQUIREMENTS.md` §0 填写拟用的 `platform_key`,并与兄弟技能调用参数对齐。
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 9. 复制新技能流程
|
||||||
|
|
||||||
|
1. **先定 slug** — 按本文规范拟定 `{verb}-{noun-phrase}-{platform}`,确认在白名单内且 ≤ 48 字符
|
||||||
|
2. **复制目录** — **推荐** [`tools/scaffold_skill.ps1`](../tools/scaffold_skill.ps1)(`-Slug` 与目录名一致);勿整包复制带走 `.git`
|
||||||
|
2.5. **清理 Git(若未用 scaffold)** — 删除 `.git`、`.openclaw-skill-template`、`.pytest_cache`;再 `git init` 与 `git remote add origin`
|
||||||
|
3. **全局替换** — `your-skill-slug` → 新 slug;更新 `SKILL.md`、`constants.py`、文档中的占位
|
||||||
|
4. **填写 REQUIREMENTS** — 完成 [`REQUIREMENTS.md`](REQUIREMENTS.md) §0 技能标识
|
||||||
|
5. **开发 & 自检** — `python tests/run_tests.py -v`,含 slug 语义测试;`git remote -v` 确认非 template
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 10. 正例 / 反例
|
||||||
|
|
||||||
|
### ✅ 正例
|
||||||
|
|
||||||
|
| slug | 说明 |
|
||||||
|
|------|------|
|
||||||
|
| `download-settlement-report-amazon` | 标准型,4 段 |
|
||||||
|
| `add-product-shopee` | 标准型,3 段 |
|
||||||
|
| `enter-sales-receipt-kingdee` | 标准型 |
|
||||||
|
| `fill-export-declaration-single-window` | 标准型,平台含连字符 |
|
||||||
|
| `generate-article-all` | scope 型 |
|
||||||
|
|
||||||
|
### ❌ 反例
|
||||||
|
|
||||||
|
| slug | 原因 |
|
||||||
|
|------|------|
|
||||||
|
| `amazon-download-settlement-report` | 平台在最前 |
|
||||||
|
| `enter-cross-border-sales-receipt-voucher-kingdee` | 过长、noun 臃肿 |
|
||||||
|
| `reconcile-finance` | 新技能禁止 2 段无平台 |
|
||||||
|
| `download_settlement_amazon` | 非 kebab-case(下划线) |
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 11. 反模式
|
||||||
|
|
||||||
|
- **平台前置**:`amazon-download-...`、`shopee-add-...`
|
||||||
|
- **英文占位 name**:复制后仍保留 `My Skill` 或纯英文 slug 风格名称
|
||||||
|
- **只改目录名**:`SKILL.md` slug 与 `constants.SKILL_SLUG` 未同步
|
||||||
|
- **noun 堆砌**:中间段过多导致超 48 字符或难以阅读
|
||||||
|
- **仿历史 2 段 repo**:`reconcile-finance`、`content-manager` 等
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 12. 演进说明(当前不启用)
|
||||||
|
|
||||||
|
当技能库规模 **> 200** 时,可再评估引入 `{domain}-verb-noun-platform` 前缀形态。**当前不启用**,新技能一律使用本文 verb-noun-platform 标准型或 scope 型。
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 13. 历史形态说明
|
||||||
|
|
||||||
|
以下为代表性**历史 repo**,新技能**勿仿**:
|
||||||
|
|
||||||
|
| 历史 slug | 说明 |
|
||||||
|
|-----------|------|
|
||||||
|
| `account-manager` | 跨平台账号管理,非 verb-noun-platform |
|
||||||
|
| `reconcile-finance` | 2 段无 platform |
|
||||||
|
| `scrape-contacts-lianjia` | 平台名不在当前白名单(链家) |
|
||||||
|
| `skill-template` | 模板仓库本身 |
|
||||||
|
|
||||||
|
校验器通过 `LEGACY_EXEMPT_SLUGS` 对模板占位与少数豁免 slug 跳过语义检查;**新复制出去的业务 skill 不在豁免表内**,必须满足 §3 硬约束。
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 14. 相关文档
|
||||||
|
|
||||||
|
| 文档 | 内容 |
|
||||||
|
|------|------|
|
||||||
|
| [`DEVELOPMENT.md`](DEVELOPMENT.md) | 复制模板步骤、发布前检查清单 |
|
||||||
|
| [`REQUIREMENTS.md`](REQUIREMENTS.md) | §0 技能标识必填项 |
|
||||||
|
| [`../references/SCHEMA.md`](../references/SCHEMA.md) | kebab-case 与数据库路径 |
|
||||||
|
| [`../SKILL.md`](../SKILL.md) | frontmatter 命名摘要 |
|
||||||
50
development/POLICY_MATRIX.md
Normal file
50
development/POLICY_MATRIX.md
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
# 开发规范 → 自动检测索引
|
||||||
|
|
||||||
|
本文件是 **development/*.md 规范到机器检测的追溯表**,不是另一份长规范。新增或调整 hard 规则时,须同步更新本矩阵与 `tests/test_development_policy_guard.py`(或注明已有测试覆盖)。
|
||||||
|
|
||||||
|
| policy_id | 规则摘要 | 来源 | 强制级别 | 自动检测 | 对应测试 |
|
||||||
|
|-----------|----------|------|----------|----------|----------|
|
||||||
|
| POLICY-STRUCTURE-001 | 标准 `scripts/` 分层:`main.py`、`cli/`、`service/`、`db/`、`util/` 必须存在 | development/DEVELOPMENT.md §2 新 skill 的标准目录结构;development/RUNTIME.md §目录结构 | hard | 目录存在性检查 | `tests/test_development_policy_guard.py::TestPolicyStructure001` |
|
||||||
|
| POLICY-RUNTIME-001 | 不得 vendored `scripts/jiangchang_skill_core/` | development/RUNTIME.md §共享 Python Runtime、§明确禁止 | hard | 目录不存在检查 | `tests/test_development_policy_guard.py::TestPolicyRuntime001`(另见 `tests/test_platform_import.py`) |
|
||||||
|
| POLICY-RUNTIME-002 | `requirements.txt` 非注释依赖行不得声明 `jiangchang-platform-kit` / `playwright` | development/RUNTIME.md §共享 Python Runtime;development/DEVELOPMENT.md §3.1 requirements.txt 依赖规范 | hard | 解析依赖行 | `tests/test_development_policy_guard.py::TestPolicyRuntime002`(另见 `tests/test_platform_import.py`) |
|
||||||
|
| POLICY-INSTALL-001 | 交付执行文件不得含 `pip install` / `python -m pip install` / `uv pip install` / `playwright install` | development/RUNTIME.md §共享 Python Runtime;development/RPA.md §1.4 Playwright 与安装边界;development/DEVELOPMENT.md §3.1 | hard | 扫描 `scripts/**/*.py`、`*.ps1`、`*.sh`、`requirements.txt`、`.github/workflows/*`(不扫 `development/*.md`) | `tests/test_development_policy_guard.py::TestPolicyInstall001` |
|
||||||
|
| POLICY-CONFIG-001 | 仓库根目录必须存在 `.env.example` | development/CONFIG.md §核心原则、§标准配置项 | hard | 文件存在性 | `tests/test_development_policy_guard.py::TestPolicyConfig001` |
|
||||||
|
| POLICY-CONFIG-002 | `.gitignore` 必须忽略 `.env` 与 `*.env.local` | development/CONFIG.md §红线:敏感信息不进 .env | hard | 解析 `.gitignore` 行 | `tests/test_development_policy_guard.py::TestPolicyConfig002` |
|
||||||
|
| POLICY-CONFIG-003 | 业务代码不得直接 `os.environ` / `os.getenv` / `os.putenv` / `environ.get`;须经 `jiangchang_skill_core.config.get*`(允许 `main.py`、测试、config_bootstrap/runtime 边界文件) | development/CONFIG.md §配置 bootstrap;development/ADAPTER.md §档位 dispatch | hard | 扫描 `scripts/**/*.py` 并排除边界文件 | `tests/test_development_policy_guard.py::TestPolicyConfig003` |
|
||||||
|
| POLICY-TESTING-001 | 默认根目录 `tests/test_*.py` 不得硬编码 `real_api` / `real_rpa` / `OPENCLAW_TEST_TARGET`(白名单仅 `test_adapter_profile_policy.py`,用于档位 helper 自测) | development/TESTING.md §默认必跑测试要做什么、§4 测试目标档位 | hard | 扫描 `tests/test_*.py` 非注释行;白名单外禁止上述文本 | `tests/test_development_policy_guard.py::TestPolicyTesting001` |
|
||||||
|
| POLICY-TESTING-002 | `tests/integration/` 下 Python 测试文件若存在,默认应为 `.sample`,不得进入默认套件 | development/TESTING.md §7 真实联调测试的安全约束、§8 新 skill 的最小测试清单 | hard | 检查 `tests/integration/*.py` 无 `.sample` 后缀 | `tests/test_development_policy_guard.py::TestPolicyTesting002` |
|
||||||
|
| POLICY-RPA-001 | 不得 import/use account-manager 内部 `rpa_helpers` / `inject_account_manager_scripts_path` / `get_account_credential` | development/ADAPTER.md §兄弟依赖;development/RPA.md §1.7 | hard | 扫描 `scripts/**/*.py` 禁止模式 | `tests/test_development_policy_guard.py::TestPolicyRpa001`(另见 `tests/test_no_rpa_helpers_import.py`) |
|
||||||
|
| POLICY-RPA-002 | RPA 交付代码不得直接执行 ffmpeg 拼 MP4;应使用 `RpaVideoSession`(允许 `ffmpeg_path` 等诊断字段) | development/RPA.md §5.3 录屏成片标准 | hard | 扫描 `scripts/**/*.py` 禁止 `subprocess` / `os.system` / `os.popen` 直接调用 ffmpeg(`task_run_support.py` 保留豁免) | `tests/test_development_policy_guard.py::TestPolicyRpa002` |
|
||||||
|
| POLICY-RPA-003 | 即使默认 `OPENCLAW_RECORD_VIDEO=0`,RPA / 长任务模板也必须保留录屏能力:`RpaVideoSession`、`video.add_step`、video artifact merge 到 `result_summary` | development/RPA.md §5.3;development/LOGGING.md;development/CONFIG.md | hard | 扫描 `scripts/service/*.py`(service 层整体须含上述接入) | `tests/test_development_policy_guard.py::TestPolicyRpa003` |
|
||||||
|
| POLICY-PACKAGING-001 | `scripts/**/*.py` 单文件 < 1000 行 | development/DEVELOPMENT.md §3.4 发布打包约束;development/RUNTIME.md §发布打包约束 | hard | 行数统计 | **已有测试覆盖**:`tests/test_release_packaging_constraints.py::test_scripts_py_files_under_pyarmor_line_limit` |
|
||||||
|
| POLICY-PACKAGING-002 | 文本文件 UTF-8 without BOM,无 `U+FEFF` | development/DEVELOPMENT.md §3.4;development/RUNTIME.md §编码与输出 | hard | BOM / 解码检查 | **已有测试覆盖**:`tests/test_release_packaging_constraints.py::test_text_files_are_utf8_without_bom` |
|
||||||
|
| POLICY-DOCS-001 | 本矩阵存在且包含上述全部 `policy_id` | 本次规范化约定 | hard | 解析 `development/POLICY_MATRIX.md` | `tests/test_development_policy_guard.py::TestPolicyDocs001` |
|
||||||
|
| POLICY-LOGGING-001 | CLI 入口必须调用 `setup_skill_logging` | development/LOGGING.md;development/RUNTIME.md | hard | 扫描 `scripts/cli/app.py` 含 `setup_skill_logging` 与 logger info | `tests/test_development_policy_guard.py::TestPolicyLogging001` |
|
||||||
|
| POLICY-LOGGING-002 | service 主任务入口必须使用 `get_skill_logger` | development/LOGGING.md;development/DEVELOPMENT.md | hard | 扫描 `scripts/service/task_service.py` | `tests/test_development_policy_guard.py::TestPolicyLogging002` |
|
||||||
|
| POLICY-LOGGING-003 | 主任务入口须含 `log.info`/`logger.info`、`log.exception`/`logger.exception`,并写入 `save_task_log` | development/LOGGING.md | hard | 扫描 `scripts/service/task_service.py` | `tests/test_development_policy_guard.py::TestPolicyLogging003` |
|
||||||
|
| POLICY-LOGGING-004 | 长任务 / RPA 模板须使用 Activity 或 RPA video step 输出进度 | development/LOGGING.md;development/RPA.md | hard | 扫描 `scripts/service/task_service.py` 含 `emit(` / `activity.emit` / `video.add_step` | `tests/test_development_policy_guard.py::TestPolicyLogging004` |
|
||||||
|
| POLICY-LOGGING-005 | 交付代码不得在 logging 调用行以 `key=value` 形式记录明显敏感字段 | development/LOGGING.md;development/CONFIG.md | hard | 扫描 `scripts/**/*.py` 的 logging 调用行(不含注释) | `tests/test_development_policy_guard.py::TestPolicyLogging005` |
|
||||||
|
| POLICY-CONTROL-001 | 不得存在 `scripts/util/progress.py` 或自建 stdout 进度模块 | development/LOGGING.md §2.5;development/RPA.md §0.1 | hard | 文件不存在检查 | `tests/test_development_policy_guard.py::TestPolicyControl001` |
|
||||||
|
| POLICY-CONTROL-002 | `task_service.py` 须使用 `job_context(` 与 `finish(` | development/LOGGING.md §2.5、§7 | hard | 扫描 `scripts/service/task_service.py` | `tests/test_development_policy_guard.py::TestPolicyControl002` |
|
||||||
|
| POLICY-CONTROL-003 | `scripts/service/*.py` 不得裸 `asyncio.sleep`(RPA 等待须 `interruptible_sleep`) | development/RPA.md §0.1 | hard | 扫描 service 层 Python | `tests/test_development_policy_guard.py::TestPolicyControl003` |
|
||||||
|
| POLICY-DATA-PATH-001 | 技能拥有路径须经 `resolve_data_path` / `get_*_dir`;`.env.example` 禁止 `./` CWD 相对路径;业务代码禁止 `abspath(config.get(...))` | development/DATA_PATHS.md | hard | 扫描 `.env.example` 与 `scripts/**/*.py` | `tests/test_development_policy_guard.py::TestPolicyDataPath001` |
|
||||||
|
| POLICY-SKILL-ACTION-001 | 存在 `development/SKILL_ACTION_RUNTIME.md`;schema 含 `executionProfile`;业务技能(非模板占位 slug)若 service 含 `RpaVideoSession` 则 `actions.json` 须有 `executionProfile=async` 且 placements 含 `agent` 的 action | development/SKILL_ACTION_RUNTIME.md;references/ACTIONS.md | hard | 文档/schema/manifest 检查 | `tests/test_development_policy_guard.py::TestPolicySkillAction001` |
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 暂不自动检测(软规则)
|
||||||
|
|
||||||
|
以下规范仍须人工遵守或依赖 code review / AI 提示词,**不**做成默认测试,避免误伤:
|
||||||
|
|
||||||
|
| 规则摘要 | 来源 | 原因 |
|
||||||
|
|----------|------|------|
|
||||||
|
| `cli/` 只解析参数、不写业务逻辑 | development/DEVELOPMENT.md §9 | 需语义理解,无法可靠静态判定 |
|
||||||
|
| `service/` 编排流向与模块拆分粒度 | development/DEVELOPMENT.md §10 | 架构软约束 |
|
||||||
|
| `db/` 层不得调浏览器或拼业务流程 | development/DEVELOPMENT.md §11 | 需跨文件调用图 |
|
||||||
|
| slug 命名语义(verb-noun-platform) | development/NAMING.md | 部分覆盖于 `tests/test_slug_naming.py`,非本矩阵 hard policy |
|
||||||
|
| 复制模板不得保留 `.git` / template marker | development/DEVELOPMENT.md §4 | 由 `tests/test_scaffold_guard.py` 守护,属脚手架场景 |
|
||||||
|
| 共享 runtime 导入来源非技能目录 | development/RUNTIME.md | 运行时依赖已安装包,见 `tests/test_platform_import.py` |
|
||||||
|
| RPA 拟人操作、选择器纪律、HITL 超时 | development/RPA.md §0–§1 | 行为与 DOM 质量,无法静态扫描 |
|
||||||
|
| adapter 四档契约测试覆盖 timeout/unauthorized 等 | development/ADAPTER.md §contract tests | 需业务实现后人工补测 |
|
||||||
|
| `SKILL.md` / `constants.SKILL_SLUG` 一致性 | development/DEVELOPMENT.md §16 | 已有 `tests/test_skill_metadata.py` |
|
||||||
|
| platform_kit_min_version >= 1.2.0 | development/RUNTIME.md | 已有 `tests/test_platform_import.py` |
|
||||||
20
development/README.md
Normal file
20
development/README.md
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
# 开发资料入口
|
||||||
|
|
||||||
|
本目录面向**人类开发者**与 **AI 编程代理**。开始定制 skill 前,建议按以下顺序阅读:
|
||||||
|
|
||||||
|
1. [`REQUIREMENTS.md`](REQUIREMENTS.md) — 需求文档模板与验收标准
|
||||||
|
2. [`NAMING.md`](NAMING.md) — slug / 仓库名命名规范(复制模板前必读)
|
||||||
|
3. [`DEVELOPMENT.md`](DEVELOPMENT.md) — 完整开发步骤与目录规范
|
||||||
|
4. [`TESTING.md`](TESTING.md) — 测试分层、隔离数据根与档位开关
|
||||||
|
5. [`LOGGING.md`](LOGGING.md) — 日志分层、必打节点、敏感信息红线(**涉及长任务、RPA、外部系统时必读**)
|
||||||
|
6. [`ADAPTER.md`](ADAPTER.md) — 涉及外部系统对接时
|
||||||
|
7. [`RPA.md`](RPA.md) — 涉及浏览器 / 桌面 / 手机自动化时
|
||||||
|
8. [`CONFIG.md`](CONFIG.md) — `.env` 规范与 bootstrap 机制
|
||||||
|
9. [`DATA_PATHS.md`](DATA_PATHS.md) — 下载/导入/导出等本地文件路径标准(涉及文件读写时必读)
|
||||||
|
10. [`SKILL_ACTION_RUNTIME.md`](SKILL_ACTION_RUNTIME.md) — Skill Action sync/async、任务中心、Agent 禁令、队列 pick(涉及 RPA / 长任务 / 数据管理按钮 / Cron / Agent 时必读)
|
||||||
|
11. [`RUNTIME.md`](RUNTIME.md) — 共享 runtime、数据路径、发布打包与编码约定
|
||||||
|
|
||||||
|
脚手架与 Git 防串库:[`../tools/README.md`](../tools/README.md)(`scaffold_skill.ps1`)。
|
||||||
|
|
||||||
|
Agent 调用契约见 [`../references/CLI.md`](../references/CLI.md)、[`../references/SCHEMA.md`](../references/SCHEMA.md)、[`../references/ACTIONS.md`](../references/ACTIONS.md)。
|
||||||
|
用户市场说明见根目录 [`README.md`](../README.md),不要写进本目录。
|
||||||
259
development/REQUIREMENTS.md
Normal file
259
development/REQUIREMENTS.md
Normal file
@@ -0,0 +1,259 @@
|
|||||||
|
# 技能需求文档模板
|
||||||
|
|
||||||
|
这份文档用于给技术人员、产品人员和实施人员统一描述某个 skill 的研发需求。
|
||||||
|
|
||||||
|
建议原则:
|
||||||
|
|
||||||
|
- 一个 skill 对应一份主需求文档
|
||||||
|
- 需求先写清楚,再进入开发
|
||||||
|
- 文档描述“要做什么”和“做到什么程度”,不要在这里堆实现细节
|
||||||
|
- 实现方式、代码结构、发布流程,分别放到 `DEVELOPMENT.md`、`references/`、`development/` 下的技术规范
|
||||||
|
|
||||||
|
如果你是从 `skill-template` 复制新技能,请复制本文件后,把下面所有占位内容替换成你的真实项目内容。
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
# REQUIREMENTS
|
||||||
|
|
||||||
|
## 0. 技能标识(复制 REQUIREMENTS 时必填)
|
||||||
|
|
||||||
|
- 拟定 slug(英文,符合 `development/NAMING.md`):
|
||||||
|
- 中文 name:
|
||||||
|
- 中文 description(一句话):
|
||||||
|
- account-manager platform_key(如有):
|
||||||
|
- 命名形态:标准型 / scope 型
|
||||||
|
|
||||||
|
## 1. 文档目标
|
||||||
|
|
||||||
|
说明这份需求文档的作用,以及它解决什么问题。
|
||||||
|
|
||||||
|
模板写法:
|
||||||
|
|
||||||
|
- 本文档用于明确 `your-skill-slug` 的研发范围、交付标准、依赖关系和验收要求。
|
||||||
|
- 技术人员应以本文件作为开发范围依据,以避免实现偏差、范围蔓延或理解不一致。
|
||||||
|
|
||||||
|
## 2. 业务背景
|
||||||
|
|
||||||
|
说明为什么要做这个 skill,它服务什么业务场景,解决什么实际问题。
|
||||||
|
|
||||||
|
模板写法:
|
||||||
|
|
||||||
|
- 当前用户在 `<业务场景>` 中需要反复处理 `<输入数据>`,人工操作耗时且易错。
|
||||||
|
- 现有流程主要依赖人工处理,存在 `<效率低 / 容易出错 / 不可批量 / 不可追踪>` 等问题。
|
||||||
|
- 因此需要开发 `your-skill-slug`,用于自动完成 `<核心动作>`,输出 `<输出结果>`,并写入 `task_logs` 便于追踪。
|
||||||
|
|
||||||
|
填写提示:
|
||||||
|
|
||||||
|
- `<业务场景>`:例如“每日批量导入外部系统结果”“Agent 触发的单次查询”
|
||||||
|
- `<输入数据>`:例如“关键词列表”“批次 ID”“账号标识”
|
||||||
|
- `<核心动作>`:例如“调用 adapter 提交”“采集并入库”“对账摘要生成”
|
||||||
|
- `<输出结果>`:例如“结构化 JSON”“任务摘要”“本地 SQLite 记录”
|
||||||
|
|
||||||
|
## 3. 开发目标
|
||||||
|
|
||||||
|
说明本次开发希望最终交付什么结果。
|
||||||
|
|
||||||
|
模板写法(按 skill 类型勾选/改写):
|
||||||
|
|
||||||
|
- 解析并校验 `<输入数据>` 的关键字段
|
||||||
|
- 通过 adapter 完成 mock / simulator / real 档位下的 `<核心动作>`
|
||||||
|
- 输出结构化结果(stdout JSON 或固定文本 + 机读字段)
|
||||||
|
- 写入 `task_logs`(及必要的业务表,见 `references/SCHEMA.md`)
|
||||||
|
- 支持失败可诊断(稳定错误码、`ERROR:` 前缀、必要截图/日志)
|
||||||
|
- 完成 CLI 入口与 `health` / `version` 最小可运行验证
|
||||||
|
- 完成正式环境发布与安装验证(如适用)
|
||||||
|
|
||||||
|
## 4. 功能范围
|
||||||
|
|
||||||
|
说明这次开发明确要实现哪些功能。
|
||||||
|
|
||||||
|
模板写法:
|
||||||
|
|
||||||
|
- 支持 `<命令1>`(如 `run`)
|
||||||
|
- 支持 `<命令2>`(如 `logs` / `log-get`)
|
||||||
|
- 支持读取 `<数据来源>`(兄弟 skill / 本地 DB / 外部 API)
|
||||||
|
- 支持执行 `<核心业务动作>`
|
||||||
|
- 支持记录 `<日志 / 状态 / 结果>`
|
||||||
|
|
||||||
|
建议将功能拆成“必须实现”和“可后续扩展”。
|
||||||
|
|
||||||
|
### 必须实现
|
||||||
|
|
||||||
|
- 支持 `python scripts/main.py health`
|
||||||
|
- 支持 `python scripts/main.py version`
|
||||||
|
- 支持 `python scripts/main.py <your-main-command>`
|
||||||
|
- 支持核心业务编排(HTTP / 批处理 / 可选 RPA,按四档 adapter 选型)
|
||||||
|
- 支持写入任务日志(`task_logs`)
|
||||||
|
|
||||||
|
### 可后续扩展
|
||||||
|
|
||||||
|
- 支持定时批跑
|
||||||
|
- 支持失败自动重试
|
||||||
|
- 支持多 `<目标系统>` 轮询
|
||||||
|
|
||||||
|
## 5. 非功能要求
|
||||||
|
|
||||||
|
说明除功能外,对稳定性、可维护性、目录规范、日志、编码等方面的要求。
|
||||||
|
|
||||||
|
- 目录结构必须符合当前 `skill-template` 规范
|
||||||
|
- 入口必须统一为 `scripts/main.py`
|
||||||
|
- 用户说明写在根 `README.md`;LLM/运行契约写在 `SKILL.md`;开发规范在 `development/`
|
||||||
|
- 输出格式应尽量机读友好;错误前缀统一为 `ERROR:`
|
||||||
|
- Windows 环境下需保证 UTF-8 输出兼容
|
||||||
|
- 必须具备基本日志能力;敏感字段脱敏
|
||||||
|
- RPA 类 skill:Playwright 由宿主共享 runtime 提供;技能侧不要 `playwright install`;URL 不要放进 `launch_persistent_context` 的 `args`
|
||||||
|
|
||||||
|
## 6. 输入输出要求
|
||||||
|
|
||||||
|
### 输入
|
||||||
|
|
||||||
|
填写本 skill 实际接受的参数与数据来源:
|
||||||
|
|
||||||
|
- `<参数名>`:含义、是否必填、默认值
|
||||||
|
- `<依赖系统>` 返回的结构化数据(如有)
|
||||||
|
- 环境变量 / `.env` 中的关键配置(引用 `CONFIG.md`)
|
||||||
|
|
||||||
|
### 输出
|
||||||
|
|
||||||
|
- 成功结果(stdout / JSON 字段说明)
|
||||||
|
- 错误输出(`ERROR:` 稳定码 + 中文 message)
|
||||||
|
- 数据库记录(表名、关键字段,见 `references/SCHEMA.md`)
|
||||||
|
- 可选:RPA 截图/视频路径(artifacts)
|
||||||
|
|
||||||
|
## 7. 依赖的兄弟技能或外部系统
|
||||||
|
|
||||||
|
### 兄弟技能依赖
|
||||||
|
|
||||||
|
- `<sibling-skill-slug>`:`<用途>`(例如 account-manager 提供 profile_dir / 租约)
|
||||||
|
|
||||||
|
### 外部系统依赖
|
||||||
|
|
||||||
|
- `<目标系统>`:`<用途>`(HTTP API / 仿真 sandbox / 真实网页)
|
||||||
|
- 浏览器(如适用):系统 Chrome/Edge,非 Playwright 内置 Chromium
|
||||||
|
- Gitea / 匠厂客户端:发布与正式环境验证
|
||||||
|
|
||||||
|
### Python 包依赖(requirements.txt)
|
||||||
|
|
||||||
|
- 本技能若需**特有** Python 三方包,写入根目录 `requirements.txt`。
|
||||||
|
- `jiangchang-platform-kit`、`playwright` 等公共能力由宿主共享 runtime 提供,**不要**写入技能 requirements。
|
||||||
|
- `SKILL.md` 的 `platform_kit_min_version` 是运行契约/兼容性声明,**不是** pip 依赖声明。
|
||||||
|
- 版本须尽量收窄(如 `requests>=2.31.0,<3`),避免共享 venv 冲突。
|
||||||
|
- **不要**把系统运行时组件写进 requirements.txt;在 `health` / preflight 中检测并提示。
|
||||||
|
|
||||||
|
## 8. 不在本次范围内的内容
|
||||||
|
|
||||||
|
明确本次“不做什么”,避免范围蔓延:
|
||||||
|
|
||||||
|
- 本次不实现 `<功能A>`
|
||||||
|
- 本次不处理 `<平台B>` / `<档位>`
|
||||||
|
- 本次不做 `<高级能力>`
|
||||||
|
- 本次不兼容旧模板历史结构(`docs/`、`optional/`、`skill_main.py` 等)
|
||||||
|
|
||||||
|
## 9. 验收标准
|
||||||
|
|
||||||
|
什么情况下才算开发完成:
|
||||||
|
|
||||||
|
- 代码结构符合模板规范;`SKILL.md` slug 与 `constants.SKILL_SLUG` 一致
|
||||||
|
- `health`、`version` 命令执行正常
|
||||||
|
- 主命令(如 `run`)在 mock / simulator 档位可重复验证
|
||||||
|
- `python tests/run_tests.py -v` 必跑测试全部通过
|
||||||
|
- `task_logs` 写入和查询符合 `references/SCHEMA.md`(含 `created_at` / `updated_at` Unix 秒级规范)
|
||||||
|
- `init_db()` 已写入 `_jiangchang_tables` / `_jiangchang_columns`;用户可见表/字段具备中文 `display_name`
|
||||||
|
- 字段展示顺序与 `PRAGMA table_info(task_logs)` 的 cid 一致;不依赖 `display_order`
|
||||||
|
- `tests/test_display_metadata.py` 通过
|
||||||
|
- 真实联调(如有)放在 `tests/integration/`,且默认套件不包含真实外联
|
||||||
|
- 发布后 Gitea 工作流成功;匠厂技能市场可见最新版本;安装后可在“新建任务”中调用
|
||||||
|
|
||||||
|
## 10. 开发注意事项
|
||||||
|
|
||||||
|
- 只修改当前 skill 仓库,不要改动无关兄弟项目
|
||||||
|
- 先判断四象限类型(`real_browser_rpa` / `real_api` / `simulator_browser_rpa` / `simulator_api`),再读对应 `examples/*/README.md`
|
||||||
|
- `cli` 只做参数解析;核心逻辑在 `service`;兄弟 skill 调用集中封装(见 `ADAPTER.md`)
|
||||||
|
- 发布前完成本地验证、工作流验证和正式环境安装验证
|
||||||
|
|
||||||
|
## 11. 变更记录
|
||||||
|
|
||||||
|
| 日期 | 版本 | 变更人 | 变更内容 |
|
||||||
|
|------|------|--------|----------|
|
||||||
|
| YYYY-MM-DD | v1.0 | `<姓名>` | 初版需求文档 |
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 建议使用方式
|
||||||
|
|
||||||
|
1. 先写 `REQUIREMENTS.md`
|
||||||
|
2. 再按 `DEVELOPMENT.md` 进入开发
|
||||||
|
3. 开发过程中补充 `references/CLI.md`、`references/SCHEMA.md` 及 `development/` 技术规范
|
||||||
|
4. 发布前对照第 9 节验收标准逐项检查
|
||||||
|
|
||||||
|
## 最小模板示例
|
||||||
|
|
||||||
|
下面给出一个可直接参考的简化版(复制后替换占位符):
|
||||||
|
|
||||||
|
```md
|
||||||
|
# REQUIREMENTS
|
||||||
|
|
||||||
|
## 1. 文档目标
|
||||||
|
|
||||||
|
本文档用于明确 `your-skill-slug` 的研发需求、范围和验收标准,作为开发与测试的统一依据。
|
||||||
|
|
||||||
|
## 2. 业务背景
|
||||||
|
|
||||||
|
当前用户在 `<业务场景>` 中需要反复处理 `<输入数据>`,人工操作耗时且易错。
|
||||||
|
因此需要开发 `your-skill-slug`,自动完成 `<核心动作>`,输出 `<输出结果>`,并写入 task_logs。
|
||||||
|
|
||||||
|
## 3. 开发目标
|
||||||
|
|
||||||
|
- 解析并校验输入
|
||||||
|
- 通过 adapter 完成 mock / simulator / real 档位动作
|
||||||
|
- 输出结构化结果并写入 task_logs
|
||||||
|
- 支持失败可诊断
|
||||||
|
|
||||||
|
## 4. 功能范围
|
||||||
|
|
||||||
|
- 支持 health / version / run
|
||||||
|
- 支持 logs / log-get
|
||||||
|
|
||||||
|
## 5. 非功能要求
|
||||||
|
|
||||||
|
- 入口统一为 scripts/main.py
|
||||||
|
- 保持 UTF-8 输出;结构符合 skill-template
|
||||||
|
|
||||||
|
## 6. 输入输出要求
|
||||||
|
|
||||||
|
### 输入
|
||||||
|
|
||||||
|
- target_id、input_id(按业务定义)
|
||||||
|
|
||||||
|
### 输出
|
||||||
|
|
||||||
|
- 成功 JSON / 摘要
|
||||||
|
- ERROR: 稳定错误码
|
||||||
|
- task_logs 记录
|
||||||
|
|
||||||
|
## 7. 依赖的兄弟技能或外部系统
|
||||||
|
|
||||||
|
- `<sibling-skill>`(按需)
|
||||||
|
- `<目标系统>` API 或 sandbox
|
||||||
|
|
||||||
|
## 8. 不在本次范围内的内容
|
||||||
|
|
||||||
|
- 不实现自动重试
|
||||||
|
- 不实现 `<本次不做>`
|
||||||
|
|
||||||
|
## 9. 验收标准
|
||||||
|
|
||||||
|
- 命令可运行;tests/run_tests.py -v 通过
|
||||||
|
- task_logs 符合 SCHEMA
|
||||||
|
- 正式环境安装验证通过
|
||||||
|
|
||||||
|
## 10. 开发注意事项
|
||||||
|
|
||||||
|
- 不修改无关项目;不引入旧模板结构
|
||||||
|
|
||||||
|
## 11. 变更记录
|
||||||
|
|
||||||
|
| 日期 | 版本 | 变更人 | 变更内容 |
|
||||||
|
|------|------|--------|----------|
|
||||||
|
| YYYY-MM-DD | v1.0 | 张三 | 初版 |
|
||||||
|
```
|
||||||
@@ -20,10 +20,30 @@
|
|||||||
| **失败存证** | 失败必截图,合规场景全程录屏,统一存 `{数据目录}/rpa-artifacts/{batch_id}/{tag}_{ts}.png` |
|
| **失败存证** | 失败必截图,合规场景全程录屏,统一存 `{数据目录}/rpa-artifacts/{batch_id}/{tag}_{ts}.png` |
|
||||||
| **选择器纪律** | 语义选择器优先(id/name/text/aria);**F12 确认后再写,严禁凭记忆猜 DOM** |
|
| **选择器纪律** | 语义选择器优先(id/name/text/aria);**F12 确认后再写,严禁凭记忆猜 DOM** |
|
||||||
| **统一错误码** | `ERROR:REQUIRE_LOGIN` / `ERROR:CAPTCHA_NEED_HUMAN` / `ERROR:RATE_LIMITED` / `ERROR:LOGIN_TIMEOUT` 等,见下方错误码表 |
|
| **统一错误码** | `ERROR:REQUIRE_LOGIN` / `ERROR:CAPTCHA_NEED_HUMAN` / `ERROR:RATE_LIMITED` / `ERROR:LOGIN_TIMEOUT` 等,见下方错误码表 |
|
||||||
|
| **步骤可控制** | 每条 RPA 指令 = 一个 `@rpa_step` 或 `video.add_step`;等待用 `interruptible_sleep`;宿主通过 control.json 暂停/继续/停止(SRCP,见 [`LOGGING.md`](LOGGING.md) §2.5) |
|
||||||
| **幂等 / 断点续跑** | 批量操作记录"已处理到第几条",崩溃后能续跑、不重复提交 |
|
| **幂等 / 断点续跑** | 批量操作记录"已处理到第几条",崩溃后能续跑、不重复提交 |
|
||||||
|
|
||||||
> 三端各自实现一个会话抽象 `RpaSession`(launch / login / act / screenshot / close),上层 skill 不感知是浏览器还是手机。
|
> 三端各自实现一个会话抽象 `RpaSession`(launch / login / act / screenshot / close),上层 skill 不感知是浏览器还是手机。
|
||||||
|
|
||||||
|
### 0.1 步骤控制与进度(SRCP,platform-kit >= 1.2.0)
|
||||||
|
|
||||||
|
- **禁止** `scripts/util/progress.py` 或 `print({"type":"progress",...})`;进度只走 `emit` / Run Journal。
|
||||||
|
- **禁止** RPA 主路径裸 `asyncio.sleep`;用 `jiangchang_skill_core.activity.interruptible_sleep`。
|
||||||
|
- RPA 函数用 `@rpa_step("中文步骤名")` 拆分;或在 `RpaVideoSession` 内 `video.add_step("中文步骤")`。
|
||||||
|
- `cmd_run` 须 `with job_context(skill=SKILL_SLUG):`,所有出口 `finish(...)`。
|
||||||
|
- 暂停在步骤边界生效;浏览器会话暂停时不强制关闭,恢复后同进程继续。
|
||||||
|
|
||||||
|
```python
|
||||||
|
from jiangchang_skill_core.activity import emit, finish, job_context, interruptible_sleep, rpa_step
|
||||||
|
|
||||||
|
@rpa_step("打开登录页")
|
||||||
|
async def open_login(page):
|
||||||
|
await page.goto("https://example.com")
|
||||||
|
await interruptible_sleep(1.5)
|
||||||
|
```
|
||||||
|
|
||||||
|
规范权威定义:[`LOGGING.md`](LOGGING.md) §2.5。金样代码:[`scripts/service/task_service.py`](../scripts/service/task_service.py)。
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## 1. 浏览器(标准已成熟)
|
## 1. 浏览器(标准已成熟)
|
||||||
@@ -51,7 +71,7 @@
|
|||||||
6. **可以** `ignore_default_args=["--enable-automation"]`(platform-kit `launch_persistent_browser` 已处理)。
|
6. **可以** `ignore_default_args=["--enable-automation"]`(platform-kit `launch_persistent_browser` 已处理)。
|
||||||
7. **强风控平台**:优先真实点击、键盘、鼠标、地址栏、持久 profile;**不要**直接拼接搜索结果 URL 或 DOM 注入。
|
7. **强风控平台**:优先真实点击、键盘、鼠标、地址栏、持久 profile;**不要**直接拼接搜索结果 URL 或 DOM 注入。
|
||||||
|
|
||||||
指纹淡化(stealth)典型项:`navigator.webdriver=undefined`、`chrome.runtime`、`permissions.query`、`plugins`、`languages` 等。共享实现见 `jiangchang_skill_core.rpa`(platform-kit **>= 1.0.13**)。
|
指纹淡化(stealth)典型项:`navigator.webdriver=undefined`、`chrome.runtime`、`permissions.query`、`plugins`、`languages` 等。共享实现见 `jiangchang_skill_core.rpa`(platform-kit **>= 1.2.0**)。
|
||||||
|
|
||||||
**拟人操作**(必做):
|
**拟人操作**(必做):
|
||||||
|
|
||||||
@@ -63,11 +83,26 @@
|
|||||||
|
|
||||||
### 1.2 页面启动标准
|
### 1.2 页面启动标准
|
||||||
|
|
||||||
优先级(高 → 低):
|
**`launch_persistent_context` 的 `args` 只能放 Chrome 启动参数**(例如 `--start-maximized`、`--disable-blink-features=AutomationControlled` 等)。
|
||||||
|
|
||||||
1. **launch args `start_url`**:浏览器启动即打开目标首页(推荐生产路径)。
|
**绝对不要把 `https://...` 这类 URL 放进 `args`。** 否则 Playwright 会报错:
|
||||||
2. **profile preferences**:作为兜底(例如 homepage / startup URLs)。
|
|
||||||
3. **程序化导航 `page.goto`**:仅最后兜底;强风控站点慎用裸 `goto` 深链。
|
```
|
||||||
|
BrowserType.launch_persistent_context: Arguments can not specify page to be opened
|
||||||
|
```
|
||||||
|
|
||||||
|
**正确做法(生产路径):**
|
||||||
|
|
||||||
|
1. `launch_persistent_context(user_data_dir=profile_dir, executable_path=chrome, args=chrome_args, ...)`
|
||||||
|
2. `page = await context.new_page()`
|
||||||
|
3. `await page.goto(start_url, wait_until="domcontentloaded", timeout=60000)`
|
||||||
|
|
||||||
|
对强风控站点,如果需要更拟人,可以启动后通过**地址栏输入 / 真实点击进入**目标页,但**仍然不能把 URL 塞进 launch args**。
|
||||||
|
|
||||||
|
完整参考实现见:
|
||||||
|
|
||||||
|
- `examples/real_browser_rpa/README.md`(**开发真实浏览器 RPA 类 skill 时必须先读**)
|
||||||
|
- `examples/real_browser_rpa/scripts/service/browser_session.py`
|
||||||
|
|
||||||
**生产路径不要依赖 CDP 接管现有页面**;CDP 仅用于诊断或桌面宿主已打开浏览器的场景。
|
**生产路径不要依赖 CDP 接管现有页面**;CDP 仅用于诊断或桌面宿主已打开浏览器的场景。
|
||||||
|
|
||||||
@@ -78,17 +113,21 @@
|
|||||||
- 检测到风控页(URL/DOM 特征)→ 抛 `ERROR:CAPTCHA_NEED_HUMAN`,轮询等待或超时,**不要强行绕过平台安全机制**。
|
- 检测到风控页(URL/DOM 特征)→ 抛 `ERROR:CAPTCHA_NEED_HUMAN`,轮询等待或超时,**不要强行绕过平台安全机制**。
|
||||||
- **不要自动操作滑块**。
|
- **不要自动操作滑块**。
|
||||||
|
|
||||||
> 共享库:`jiangchang_skill_core.rpa.wait_for_captcha_pass`;参考 `scripts/service/example_browser_rpa.py`。
|
> 共享库:`jiangchang_skill_core.rpa.wait_for_captcha_pass`;**真实浏览器 RPA 成功案例**见 `examples/real_browser_rpa/`:
|
||||||
|
>
|
||||||
|
> - 先读 `examples/real_browser_rpa/README.md`
|
||||||
|
> - 再参考 `examples/real_browser_rpa/scripts/service/human_verification.py`
|
||||||
|
> - 再参考 `examples/real_browser_rpa/scripts/service/task_rpa.py`
|
||||||
|
|
||||||
### 1.4 安装
|
### 1.4 Playwright 与安装边界
|
||||||
|
|
||||||
生产/宿主运行:`playwright` 由共享 runtime 提供,无需在技能内 `pip install`。
|
- Playwright 是宿主共享 runtime 提供的通用 Python 包能力;**正式 skill 不负责安装**。
|
||||||
|
- skill 侧**禁止**在 `requirements.txt`、安装脚本、运行脚本、根 `README.md` 用户说明中加入 `playwright install`、`pip install playwright` 或任何自动安装浏览器驱动的逻辑。
|
||||||
|
- 真实浏览器 RPA 默认使用**系统 Chrome/Edge**,由宿主/runtime 管理。
|
||||||
|
- **不要**执行 `playwright install chromium`、`playwright install chrome` 等浏览器驱动安装命令。
|
||||||
|
- 仅当**本地独立开发环境排障**时,开发者可手工检查或补装 Python 包 `playwright`;该操作**不得**进入 skill 交付文件与自动流程。
|
||||||
|
|
||||||
独立本地开发环境若缺少 Python 包 `playwright` 时,可手动执行:
|
页面启动仍遵循 1.2:`launch_persistent_context` 的 `args` 只能放 Chrome 启动参数,**不能把 URL 塞进 `args`**;打开页面必须通过 `new_page()` + `goto()`,或通过真实地址栏/点击进入。
|
||||||
|
|
||||||
```bash
|
|
||||||
pip install playwright # 仅独立开发补装;用系统 Chrome,无需 playwright install chromium
|
|
||||||
```
|
|
||||||
|
|
||||||
### 引用方式
|
### 引用方式
|
||||||
|
|
||||||
@@ -105,10 +144,54 @@ from jiangchang_skill_core.rpa import (
|
|||||||
from jiangchang_skill_core.rpa.stealth import stealth_enabled, STEALTH_INIT_SCRIPT
|
from jiangchang_skill_core.rpa.stealth import stealth_enabled, STEALTH_INIT_SCRIPT
|
||||||
```
|
```
|
||||||
|
|
||||||
- `RpaVideoSession` 来自 platform-kit **>= 1.0.13**;ffmpeg、背景音乐、media-assets 由 platform-kit 统一解析;已提供前置/后置缓冲、字幕、TTS 旁白、背景音乐循环、结尾淡出。
|
- `RpaVideoSession` 来自 platform-kit **>= 1.2.0**;ffmpeg、背景音乐、media-assets 由 platform-kit 统一解析;已提供前置/后置缓冲、字幕、TTS 旁白、背景音乐循环、结尾淡出。
|
||||||
- `health` 对上述资源做只读诊断,不下载、不修复。
|
- `health` 对上述资源做只读诊断,不下载、不修复。
|
||||||
|
|
||||||
完整示例见 `scripts/service/example_browser_rpa.py` 与 `scripts/service/example_adapter/sim_rpa.py`。
|
### 1.5 真实浏览器 RPA 示例(必读)
|
||||||
|
|
||||||
|
开发**真实网站 + 浏览器操作 + 登录态/验证码/滚动采集**类 skill 时:
|
||||||
|
|
||||||
|
1. **必须先阅读** `examples/real_browser_rpa/README.md`
|
||||||
|
2. 再参考以下代码:
|
||||||
|
- `examples/real_browser_rpa/scripts/service/browser_session.py` — persistent context 启动
|
||||||
|
- `examples/real_browser_rpa/scripts/service/human_verification.py` — 人工验证等待
|
||||||
|
- `examples/real_browser_rpa/scripts/service/task_rpa.py` — RPA 主流程
|
||||||
|
- `examples/real_browser_rpa/scripts/service/account_client.py` — account-manager CLI 封装
|
||||||
|
|
||||||
|
仿真浏览器 RPA 的 adapter 分层权威参考见 `examples/simulator_browser_rpa/`(含 async `simulator_playwright.py`、薄 `simulator_rpa` adapter、`account_client.py` subprocess),**不能**替代真实浏览器 RPA 规范。
|
||||||
|
|
||||||
|
### 1.6 仿真浏览器 RPA 示例(必读)
|
||||||
|
|
||||||
|
开发**自有仿真页面 / sandbox / 可控 DOM 的浏览器 RPA**(表单填写、批量提交、弹窗确认)时:
|
||||||
|
|
||||||
|
1. **必须先阅读** `examples/simulator_browser_rpa/README.md`
|
||||||
|
2. 再参考:
|
||||||
|
- `examples/simulator_browser_rpa/scripts/service/browser_session.py` — async persistent context(**不含 goto**)
|
||||||
|
- `examples/simulator_browser_rpa/scripts/service/simulator_playwright.py` — RPA 主流程(厚)
|
||||||
|
- `examples/simulator_browser_rpa/scripts/service/adapter/`(薄 `simulator_rpa` + mock + dispatch)
|
||||||
|
- `examples/simulator_browser_rpa/scripts/service/account_client.py` — account-manager subprocess
|
||||||
|
- `examples/simulator_browser_rpa/scripts/service/task_service.py` — async 编排
|
||||||
|
- `examples/simulator_browser_rpa/sandbox/demo_app.html`
|
||||||
|
|
||||||
|
> **先选类型再写代码**:真实第三方网站 → `real_browser_rpa`;自有 sandbox / 行业仿真 → `simulator_browser_rpa`。不要跨类型照抄 selector 或业务流程。
|
||||||
|
|
||||||
|
### 1.7 行业仿真平台浏览器 RPA(jc2009 类)
|
||||||
|
|
||||||
|
适用:**自有或共享 `sandbox.jc2009.com` 等行业仿真平台**,不是真实高风控第三方站。
|
||||||
|
|
||||||
|
工程范式与 §1.5 相同:
|
||||||
|
|
||||||
|
| 项 | 标准 |
|
||||||
|
|----|------|
|
||||||
|
| Playwright | **async** 贯穿;禁止 sync Playwright 用于完整 RPA 主路径 |
|
||||||
|
| 分层 | **薄 adapter** + `{domain}_playwright.py`(示例:`simulator_playwright.py`)+ `account_client.py` subprocess |
|
||||||
|
| 登录 | **双层**:门户 HITL(`#portal-user` / `#portal-pass` 类泛化 DOM)+ 业务系统登录(技能内自写) |
|
||||||
|
| 账号 | `url` 用行业根,不用 `/login`;`auth_strategy=per_session_manual`;`pick_web_account` + `release_lease` |
|
||||||
|
| 禁止 | **不要** `import account-manager` 的 `rpa_helpers` 等内部模块 |
|
||||||
|
| Selector | 用户可见文案 / `get_by_role` / `name` 优先;`data-testid` 有则用、无则 fallback;共享 sandbox **不要求**为技能加 testid |
|
||||||
|
| Profile | Chrome persistent profile 可能缓存旧 SPA → 联调排障:手工 `--user-data-dir` 清站点数据 |
|
||||||
|
|
||||||
|
权威 example:**修正后的** `examples/simulator_browser_rpa/`(不是任何生产技能仓库)。
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
@@ -191,7 +274,7 @@ skill 退出/抛错统一用 `ERROR:` 前缀 + 稳定码,方便宿主与上层
|
|||||||
|
|
||||||
### 5.1 截图存证
|
### 5.1 截图存证
|
||||||
|
|
||||||
- **路径**:`{CLAW_DATA_ROOT}/{user}/{slug}/rpa-artifacts/{batch_id}/{tag}_{timestamp}.png`
|
- **路径**:`{JIANGCHANG_DATA_ROOT}/{JIANGCHANG_USER_ID}/{slug}/rpa-artifacts/{batch_id}/{tag}_{timestamp}.png`
|
||||||
- **失败必截图**:受 `OPENCLAW_ARTIFACTS_ON_FAILURE`(默认开)控制。
|
- **失败必截图**:受 `OPENCLAW_ARTIFACTS_ON_FAILURE`(默认开)控制。
|
||||||
- **Playwright 不负责录屏**,仅浏览器自动化。
|
- **Playwright 不负责录屏**,仅浏览器自动化。
|
||||||
- **常见 tag**:`before_submit` / `after_submit` / `captcha` / `login_fail` / `error`。
|
- **常见 tag**:`before_submit` / `after_submit` / `captcha` / `login_fail` / `error`。
|
||||||
@@ -220,13 +303,16 @@ skill 退出/抛错统一用 `ERROR:` 前缀 + 稳定码,方便宿主与上层
|
|||||||
|
|
||||||
### 5.3 录屏成片标准
|
### 5.3 录屏成片标准
|
||||||
|
|
||||||
- RPA skill 默认 `OPENCLAW_RECORD_VIDEO=1`。
|
- **skill-template 默认 `OPENCLAW_RECORD_VIDEO=0`**(开发调试更快,不启 ffmpeg)。
|
||||||
|
- **`OPENCLAW_RECORD_VIDEO=1` 时启用录屏**:字幕、旁白、背景音、MP4 成片由 platform-kit 统一处理。
|
||||||
- 使用 platform-kit 的 **`RpaVideoSession`**;**skill 不要自行合成视频**(不要自己调 ffmpeg 拼 MP4)。
|
- 使用 platform-kit 的 **`RpaVideoSession`**;**skill 不要自行合成视频**(不要自己调 ffmpeg 拼 MP4)。
|
||||||
- `OPENCLAW_RECORD_VIDEO=0` 时 session 无副作用(不启 ffmpeg、不写字幕文件)。
|
- **默认关闭 ≠ 可以删除录屏接入**:RPA / 长任务 **service 层**(`scripts/service/*.py`)**必须保留** `RpaVideoSession`、`video.add_step`、video artifact merge 到 `result_summary`(见 `POLICY-RPA-003`)。
|
||||||
|
- `OPENCLAW_RECORD_VIDEO=0` 时 session **无副作用**(不启 ffmpeg、不写字幕文件、不生成最终 MP4),但 **`RpaVideoSession.add_step` 仍可同步 Activity 进度**。
|
||||||
|
- 生产留证 / 合规 / 客户现场复现时,由用户在 `.env` 或进程环境变量中开启 `OPENCLAW_RECORD_VIDEO=1`。
|
||||||
- **ffmpeg 是唯一录屏器**(Windows:`gdigrab` + `desktop`)。
|
- **ffmpeg 是唯一录屏器**(Windows:`gdigrab` + `desktop`)。
|
||||||
- **最终视频**:`{skill_data_dir}/videos/{skill_slug}_{yyyyMMdd_HHmmss}_{batch_id}.mp4`
|
- **最终视频**(`OPENCLAW_RECORD_VIDEO=1` 时):`{skill_data_dir}/videos/{skill_slug}_{yyyyMMdd_HHmmss}_{batch_id}.mp4`
|
||||||
- **中间产物**:`rpa-artifacts/{batch_id}/capture.mp4`、`subtitles/`、`logs/` 等。
|
- **中间产物**:`rpa-artifacts/{batch_id}/capture.mp4`、`subtitles/`、`logs/` 等。
|
||||||
- 任务完成后 CLI / `result_summary` 应包含:`video_path`、`raw_video`、`video_log`、`video_warnings`、`music_path`、`voiceover_path`、`audio_warnings`(见 `scripts/service/task_run_support.py`)。
|
- 任务完成后 CLI / `result_summary` 应包含 video 字段(开启录屏时有路径;关闭时仍保留 `video` 结构与诊断占位):`video_path`、`raw_video`、`video_log`、`video_warnings`、`music_path`、`voiceover_path`、`audio_warnings`(见 `scripts/service/task_run_support.py`)。
|
||||||
|
|
||||||
模板最小示范见 `scripts/service/task_service.py` 的 `_run_template_demo()`。
|
模板最小示范见 `scripts/service/task_service.py` 的 `_run_template_demo()`。
|
||||||
|
|
||||||
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
## 共享 Python Runtime
|
## 共享 Python Runtime
|
||||||
|
|
||||||
**skill-template** 及复制出的新技能,公共能力均来自宿主匠厂安装的共享 Python Runtime(`jiangchang-platform-kit>=1.0.13` 及其传递依赖,含 `playwright`)。`jiangchang_skill_core` **不得**在技能仓库内 vendored,应由共享 venv 的 site-packages 提供。
|
**skill-template** 及复制出的新技能,公共能力均来自宿主匠厂安装的共享 Python Runtime(`jiangchang-platform-kit>=1.2.0` 及其传递依赖,含 `playwright`)。`jiangchang_skill_core` **不得**在技能仓库内 vendored,应由共享 venv 的 site-packages 提供。
|
||||||
|
|
||||||
技能根目录 `requirements.txt` **只声明技能特有依赖**;**不要**重复声明 `jiangchang-platform-kit` 或 `playwright`。`SKILL.md` 的 `platform_kit_min_version` 是运行契约,**不是** pip 依赖声明。
|
技能根目录 `requirements.txt` **只声明技能特有依赖**;**不要**重复声明 `jiangchang-platform-kit` 或 `playwright`。`SKILL.md` 的 `platform_kit_min_version` 是运行契约,**不是** pip 依赖声明。
|
||||||
|
|
||||||
@@ -22,15 +22,15 @@ Windows:
|
|||||||
<shared-python> {baseDir}/scripts/main.py health
|
<shared-python> {baseDir}/scripts/main.py health
|
||||||
```
|
```
|
||||||
|
|
||||||
`<shared-python>` 通常位于 `{JIANGCHANG_DATA_ROOT}/python-runtime/.venv`。数据根由宿主注入;开发模式下也可能通过 `CLAW_DATA_ROOT` 解析(见 `jiangchang_skill_core.runtime_env`)。
|
`<shared-python>` 通常位于 `{JIANGCHANG_DATA_ROOT}/python-runtime/.venv`。数据根由宿主注入;开发模式下也可能通过 `JIANGCHANG_DATA_ROOT` 解析(见 `jiangchang_skill_core.runtime_env`)。
|
||||||
|
|
||||||
## Runtime 诊断(platform-kit 1.0.13+)
|
## Runtime 诊断(platform-kit 1.2.0+)
|
||||||
|
|
||||||
`health` 命令通过 **`jiangchang_skill_core.collect_runtime_diagnostics`** 输出共享 runtime 诊断,**不在技能内重复实现**。典型字段:
|
`health` 命令通过 **`jiangchang_skill_core.collect_runtime_diagnostics`** 输出共享 runtime 诊断,**不在技能内重复实现**。典型字段:
|
||||||
|
|
||||||
- `skill_slug`、`python_executable`、`platform_kit_version`、`platform_kit_min_version`
|
- `skill_slug`、`python_executable`、`platform_kit_version`、`platform_kit_min_version`
|
||||||
- `jiangchang_skill_core_file` — 若从技能目录加载会输出 `runtime_issue[warning]`
|
- `jiangchang_skill_core_file` — 若从技能目录加载会输出 `runtime_issue[warning]`
|
||||||
- `CLAW_DATA_ROOT` / `JIANGCHANG_DATA_ROOT` / `resolved_data_root`
|
- `JIANGCHANG_DATA_ROOT` / `resolved_data_root`
|
||||||
- `media_assets_root`、`ffmpeg_path`、`background_music_*`
|
- `media_assets_root`、`ffmpeg_path`、`background_music_*`
|
||||||
- `record_video_enabled`、`runtime_issue[warning|error]`
|
- `record_video_enabled`、`runtime_issue[warning|error]`
|
||||||
|
|
||||||
@@ -39,10 +39,10 @@ Windows:
|
|||||||
## 配置 bootstrap
|
## 配置 bootstrap
|
||||||
|
|
||||||
- 仓库内 `.env.example` 是配置模板(单一事实来源)。
|
- 仓库内 `.env.example` 是配置模板(单一事实来源)。
|
||||||
- 用户实际 `.env`:`{CLAW_DATA_ROOT}/{CLAW_USER_ID}/{skill_slug}/.env`。
|
- 用户实际 `.env`:`{JIANGCHANG_DATA_ROOT}/{JIANGCHANG_USER_ID}/{skill_slug}/.env`。
|
||||||
- `scripts/main.py` 与 `cli.app.main()` 启动时调用 `util.config_bootstrap.bootstrap_skill_config()`。
|
- `scripts/main.py` 与 `cli.app.main()` 启动时调用 `util.config_bootstrap.bootstrap_skill_config()`。
|
||||||
- 配置优先级:**进程环境变量** > **用户 `.env`** > **`.env.example` 默认值**。
|
- 配置优先级:**进程环境变量** > **用户 `.env`** > **`.env.example` 默认值**。
|
||||||
- 公共 `config` / `merge_missing_env_keys` 来自共享 runtime 的 `jiangchang-platform-kit>=1.0.13`,**不得** vendored `scripts/jiangchang_skill_core/`。
|
- 公共 `config` / `merge_missing_env_keys` 来自共享 runtime 的 `jiangchang-platform-kit>=1.2.0`,**不得** vendored `scripts/jiangchang_skill_core/`。
|
||||||
|
|
||||||
## media-assets / ffmpeg / 背景音乐
|
## media-assets / ffmpeg / 背景音乐
|
||||||
|
|
||||||
@@ -52,7 +52,7 @@ Windows:
|
|||||||
{JIANGCHANG_DATA_ROOT}/shared/media-assets
|
{JIANGCHANG_DATA_ROOT}/shared/media-assets
|
||||||
```
|
```
|
||||||
|
|
||||||
(与 `CLAW_DATA_ROOT` 解析规则一致,由宿主注入。)
|
(由宿主注入 `JIANGCHANG_DATA_ROOT`。)
|
||||||
|
|
||||||
RPA 录屏成片(`RpaVideoSession`)、ffmpeg 路径、背景音乐探测均由 platform-kit 提供;技能只 import,不重复实现。
|
RPA 录屏成片(`RpaVideoSession`)、ffmpeg 路径、背景音乐探测均由 platform-kit 提供;技能只 import,不重复实现。
|
||||||
|
|
||||||
@@ -61,6 +61,7 @@ RPA 录屏成片(`RpaVideoSession`)、ffmpeg 路径、背景音乐探测均
|
|||||||
新技能建议采用以下根目录结构:
|
新技能建议采用以下根目录结构:
|
||||||
|
|
||||||
- `assets/`
|
- `assets/`
|
||||||
|
- `development/`
|
||||||
- `references/`
|
- `references/`
|
||||||
- `scripts/`
|
- `scripts/`
|
||||||
- `tests/`
|
- `tests/`
|
||||||
@@ -70,7 +71,7 @@ RPA 录屏成片(`RpaVideoSession`)、ffmpeg 路径、背景音乐探测均
|
|||||||
|
|
||||||
- `scripts/main.py`:唯一 CLI 入口
|
- `scripts/main.py`:唯一 CLI 入口
|
||||||
- `scripts/cli/`:参数解析与命令分发
|
- `scripts/cli/`:参数解析与命令分发
|
||||||
- `scripts/db/`:SQLite 或本地持久化层
|
- `scripts/db/`:SQLite 或本地持久化层(标准时间字段见 `references/SCHEMA.md`)
|
||||||
- `scripts/service/`:业务用例与外部交互
|
- `scripts/service/`:业务用例与外部交互
|
||||||
- `scripts/util/`:通用工具、常量、日志、路径薄封装
|
- `scripts/util/`:通用工具、常量、日志、路径薄封装
|
||||||
|
|
||||||
@@ -79,7 +80,7 @@ RPA 录屏成片(`RpaVideoSession`)、ffmpeg 路径、背景音乐探测均
|
|||||||
推荐:
|
推荐:
|
||||||
|
|
||||||
```text
|
```text
|
||||||
{CLAW_DATA_ROOT}/{CLAW_USER_ID}/{skill_slug}/
|
{JIANGCHANG_DATA_ROOT}/{JIANGCHANG_USER_ID}/{skill_slug}/
|
||||||
```
|
```
|
||||||
|
|
||||||
数据库文件建议:
|
数据库文件建议:
|
||||||
@@ -88,6 +89,11 @@ RPA 录屏成片(`RpaVideoSession`)、ffmpeg 路径、背景音乐探测均
|
|||||||
{...}/{skill_slug}.db
|
{...}/{skill_slug}.db
|
||||||
```
|
```
|
||||||
|
|
||||||
|
**业务文件(下载、导入、导出、缓存等)** 须落在上述数据目录的标准子目录下,**不得**相对 workspace/CWD 写入。完整子目录树、env 覆盖规则、`resolve_data_path()` 用法见 **[`DATA_PATHS.md`](DATA_PATHS.md)**。
|
||||||
|
|
||||||
|
- 业务表使用英文 snake_case;中文表名/字段名写入 `_jiangchang_tables` / `_jiangchang_columns`(见 `references/SCHEMA.md`)。
|
||||||
|
- 界面字段顺序由 `CREATE TABLE` 列定义顺序决定,**不要**维护 `display_order` 来调整顺序。
|
||||||
|
|
||||||
## 明确禁止
|
## 明确禁止
|
||||||
|
|
||||||
- **不得** vendored `scripts/jiangchang_skill_core/`
|
- **不得** vendored `scripts/jiangchang_skill_core/`
|
||||||
@@ -96,17 +102,28 @@ RPA 录屏成片(`RpaVideoSession`)、ffmpeg 路径、背景音乐探测均
|
|||||||
|
|
||||||
## 测试时的运行时隔离
|
## 测试时的运行时隔离
|
||||||
|
|
||||||
本模板的 `tests/_support.IsolatedDataRoot` 会在测试期间同时设置:
|
本模板的 `tests/_support.IsolatedDataRoot` 会在测试期间设置:
|
||||||
|
|
||||||
- `CLAW_DATA_ROOT` / `JIANGCHANG_DATA_ROOT` → 临时 tempfile 目录
|
- `JIANGCHANG_DATA_ROOT` → 临时 tempfile 目录
|
||||||
- `CLAW_USER_ID` / `JIANGCHANG_USER_ID` → `_test`
|
- `JIANGCHANG_USER_ID` → `_test`
|
||||||
|
|
||||||
退出时四个变量恢复,临时目录删除。所有 DB / 文件写入都被限制在临时目录内,不污染本机。
|
退出时两个变量恢复,临时目录删除。所有 DB / 文件写入都被限制在临时目录内,不污染本机。
|
||||||
|
|
||||||
详见 `references/TESTING.md` 第 3 节。
|
详见 `TESTING.md` 第 3 节。
|
||||||
|
|
||||||
## 编码与输出
|
## 编码与输出
|
||||||
|
|
||||||
|
- 所有提交到 skill 的源码、文档、配置文本文件必须是 **UTF-8 without BOM**(不要只写「UTF-8」)。
|
||||||
|
- Python 文件**绝对不能**带 UTF-8 BOM;否则 release CI 的 PyArmor 阶段可能报:`invalid non-printable character U+FEFF`。
|
||||||
|
- Windows 编辑器容易保存成 UTF-8 BOM;提交前确认文件开头无 `U+FEFF`。
|
||||||
- Windows 终端建议在 `scripts/main.py` 里做 UTF-8 stdout/stderr 包装
|
- Windows 终端建议在 `scripts/main.py` 里做 UTF-8 stdout/stderr 包装
|
||||||
- 机读输出优先单行 JSON
|
- 机读输出优先单行 JSON
|
||||||
- 错误前缀建议统一 `ERROR:`
|
- 错误前缀建议统一 `ERROR:`
|
||||||
|
|
||||||
|
## 发布打包约束(`scripts/**/*.py`)
|
||||||
|
|
||||||
|
release workflow 加密 `scripts/` 源码时,可能运行在 PyArmor 免费/试用模式:
|
||||||
|
|
||||||
|
- `scripts/**/*.py` 单文件必须 **< 1000 行**(`>= 1000` 可能在 CI 失败);推荐 **< 900 行**。
|
||||||
|
- 接近上限时拆分到 `cli/`、`service/`、`db/`、`util/`,不要把 RPA 主流程、选择器、DB、CLI 堆进单文件。
|
||||||
|
- 此行数限制**仅适用于** `scripts/**/*.py`;不限制 README 等文档行数,也不把 examples 里的 HTML/README 纳入 PyArmor 行数约束。
|
||||||
162
development/SKILL_ACTION_RUNTIME.md
Normal file
162
development/SKILL_ACTION_RUNTIME.md
Normal file
@@ -0,0 +1,162 @@
|
|||||||
|
# Skill Action Runtime(宿主任务中心契约)
|
||||||
|
|
||||||
|
本文是匠厂宿主 **Skill Action / 任务中心** 的技能侧金标准。涉及浏览器 RPA、长耗时任务、数据管理按钮、定时任务或 Agent 直接调用时**必读**。
|
||||||
|
|
||||||
|
**边界:**
|
||||||
|
|
||||||
|
| 文档 | 职责 |
|
||||||
|
|------|------|
|
||||||
|
| 本文 | sync/async、任务中心、Agent 禁令、编排范式、队列 pick |
|
||||||
|
| [`../references/ACTIONS.md`](../references/ACTIONS.md) | `actions.json` 字段、placements、inputSchema |
|
||||||
|
| [`LOGGING.md`](LOGGING.md) | `job_context` / `emit` / `finish` / Run Journal |
|
||||||
|
| [`DATA_PATHS.md`](DATA_PATHS.md) | 下载/导入等落盘路径 |
|
||||||
|
|
||||||
|
宿主实现细节以匠厂 Host API 为准;技能侧只需保证 manifest 与 CLI 契约稳定。
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 1. 核心原则
|
||||||
|
|
||||||
|
1. **宿主管调度,技能管业务** — 不自行实现任务中心 / Job 轮询 / 后台守护进程。
|
||||||
|
2. **长耗时 = async** — 开浏览器、RPA、HTTP 大文件下载等必须 `executionProfile: "async"`,进**任务中心**。
|
||||||
|
3. **Agent 禁止 `exec` 直跑长任务 CLI** — 必须 `run_skill_action`;禁止 `exec python main.py run` + `process poll` 干等。
|
||||||
|
4. **进度走 Journal** — `emit` / `video.add_step` → Run Journal;UI / Skill Run Card / 任务中心展示;**不靠 stdout 流**。
|
||||||
|
5. **显式声明 `executionProfile`** — 宿主省略时默认 `async`;模板要求每个 action **写明** `sync` 或 `async`,避免歧义。
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 2. `executionProfile`:sync vs async
|
||||||
|
|
||||||
|
| 值 | 宿主行为 | 超时 | 任务中心 |
|
||||||
|
|----|----------|------|----------|
|
||||||
|
| **`sync`** | 内联等待 CLI 结束,把结果直接返回调用方 | 默认约 **30s**(`SKILL_ACTION_SYNC_TIMEOUT_MS`) | ❌ 不进 |
|
||||||
|
| **`async`** | 后台 spawn Job,立即返回 `jobId` | Job 本身无默认墙钟上限(可暂停/停止) | ✅ 进入 |
|
||||||
|
| **省略** | 宿主视为 **async** | 同 async | ✅ |
|
||||||
|
|
||||||
|
### 硬规则
|
||||||
|
|
||||||
|
| 场景 | 必须 |
|
||||||
|
|------|------|
|
||||||
|
| 浏览器 / Playwright / RPA 主路径 | `async` + 建议 `confirmation` |
|
||||||
|
| 单次可能超过 ~20s 的网络/下载 | `async` |
|
||||||
|
| `health` / `version` / `config-path` / 纯统计 / 快速导入 | `sync` |
|
||||||
|
| 数据管理 **toolbar** | **只放 async**(宿主前端对 sync 结果会报错) |
|
||||||
|
|
||||||
|
### Agent 路径启发式(宿主行为,技能作者需知)
|
||||||
|
|
||||||
|
- 显式写了 `executionProfile` → 以显式为准。
|
||||||
|
- 未写时:描述含「统计 / 检查 / 不启动浏览器」等只读语义 → Agent 侧可当 **sync**;有 `confirmation` 的副作用 → **async**。
|
||||||
|
- **模板要求一律显式写出**,不要依赖启发式。
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 3. 三入口如何感知 sync / async
|
||||||
|
|
||||||
|
三个入口统一走 `POST /api/skill-actions/run`,带 `source.kind`:
|
||||||
|
|
||||||
|
| 入口 | `source.kind` | 推荐 |
|
||||||
|
|------|---------------|------|
|
||||||
|
| **Agent** | `agent` | `run_skill_action`;长任务 async,秒回 jobId |
|
||||||
|
| **数据管理** | `data-management` | toolbar 按钮触发 async action |
|
||||||
|
| **定时任务** | `cron` | Cron 配置 async action + `pickCount` 等参数 |
|
||||||
|
|
||||||
|
技能 **不要** 在 Python 里为三个入口写三套分支;宿主读 manifest 分流。
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 4. Action 分类与 placements 矩阵
|
||||||
|
|
||||||
|
| Action 类型 | `executionProfile` | 典型 placements | `confirmation` |
|
||||||
|
|-------------|--------------------|-----------------|----------------|
|
||||||
|
| 运行检查 / 版本 / 配置路径 | `sync` | `skill-detail`, `agent`(只读) | 无 |
|
||||||
|
| 库 / 队列统计 | `sync` | `skill-detail`, `agent` | 无 |
|
||||||
|
| 数据导入 / 失败重置 | `sync` | `skill-detail`, `agent` | 可选 |
|
||||||
|
| **单条**副作用 RPA | `async` | `agent`, `skill-detail` | **必须** |
|
||||||
|
| **批量顺序** RPA(pick) | `async` | **`toolbar`**, `cron`, `agent` | **必须** |
|
||||||
|
|
||||||
|
禁忌:
|
||||||
|
|
||||||
|
- 高副作用 async action 不要默认只挂 `toolbar`(应有 confirmation)。
|
||||||
|
- 含 `sensitive` 参数的 action 不要挂 `toolbar` / `cron`(除非有明确安全设计)。
|
||||||
|
- 不要为了「按钮多」暴露所有 CLI 子命令。
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 5. 技能内编排范式(单内核 + 多入口)
|
||||||
|
|
||||||
|
推荐结构(对齐 create-account / download-video 金标准):
|
||||||
|
|
||||||
|
```text
|
||||||
|
execute_single_<biz>(...) # 纯业务:鉴权 + 核心逻辑 + task_log;不含 job_context
|
||||||
|
cmd_run(...) # 单条:job_context + execute_single + finish
|
||||||
|
cmd_run_pick(N, ...) # 批量:job_context + for-loop execute_single + emit(progress) + finish
|
||||||
|
cmd_stats() # 只读 sync:JSON stdout,无 job_context
|
||||||
|
```
|
||||||
|
|
||||||
|
要点:
|
||||||
|
|
||||||
|
1. **`job_context` 只包编排层**(`cmd_run` / `cmd_run_pick`),不包 `execute_single_*`。
|
||||||
|
2. 批量 **顺序、不并发**;数量由参数 `pickCount` / `--pick N` 传入(不写死 100/200)。
|
||||||
|
3. pick 循环内:`checkpoint()` + `emit("正在处理 {i}/{total}", progress={...})`。
|
||||||
|
4. 结束:全成功 `finish(success)`;部分失败 `finish(partial)`;全失败 `finish(failed)`。
|
||||||
|
5. 同一浏览器 Profile / account lease:**禁止并发**;顺序执行 + lease 即可。
|
||||||
|
|
||||||
|
详见 [`LOGGING.md`](LOGGING.md) §2.5 与 `scripts/service/task_service.py` 示范。
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 6. 队列 + pick(可选,Type B 批量)
|
||||||
|
|
||||||
|
有「待处理列表」的技能(账号开通、视频下载、帖子发布等)建议:
|
||||||
|
|
||||||
|
| 能力 | 约定 |
|
||||||
|
|------|------|
|
||||||
|
| 队列表 | `status`: `pending` → `processing` → `success` / `failed` |
|
||||||
|
| 入库 | 数据管理 CRUD,或 `import-*`(相对 `{skill_data}/imports/`,见 [`DATA_PATHS.md`](DATA_PATHS.md)) |
|
||||||
|
| 消费 | `run --pick N`,默认仅 `pending` |
|
||||||
|
| 重试 | `reset` 或数据管理改 `pending`;**failed 不要自动重试** |
|
||||||
|
| 批次 | 可选 `batch_id`,便于筛选与任务中心对照 |
|
||||||
|
| Manifest | `*-run-pick`:`async`,`placements` 含 `toolbar` / `cron` / `agent`,`inputSchema.pickCount` |
|
||||||
|
|
||||||
|
批量有两种合理形态(都是**顺序**):
|
||||||
|
|
||||||
|
| 形态 | 任务中心条数 | 适用 |
|
||||||
|
|------|-------------|------|
|
||||||
|
| 一次 `run-pick` 处理 N 条 | **1** 条 Job | 同类 RPA、同 Profile、定时消费队列 |
|
||||||
|
| Agent/UI 触发 N 次单条 `run` | **N** 条 Job | 每条独立追踪 / 独立重试 |
|
||||||
|
|
||||||
|
默认优先 **一次 pick、一条 Job**(与 create-account `--pick`、download `--pick` 一致)。
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 7. Agent / SKILL.md 契约(复制后必改)
|
||||||
|
|
||||||
|
业务技能 `SKILL.md` 应明确:
|
||||||
|
|
||||||
|
1. 副作用任务必须通过宿主 **Skill Action**(`run_skill_action`)调用。
|
||||||
|
2. **禁止**对长任务使用 `exec` + `process poll`。
|
||||||
|
3. 单条 vs 批量 action id 与参数(如 `url` / `pickCount`)。
|
||||||
|
4. 长任务进度看**任务中心** / Skill Run Card,不等 CLI stdout。
|
||||||
|
|
||||||
|
模板源仓库的 `SKILL.md` 含占位段落;scaffold 后按业务改写。
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 8. Policy
|
||||||
|
|
||||||
|
自动检测索引见 [`POLICY_MATRIX.md`](POLICY_MATRIX.md):
|
||||||
|
|
||||||
|
- **POLICY-SKILL-ACTION-001**:`SKILL_ACTION_RUNTIME.md` 存在;schema 含 `executionProfile`;有 RPA 的业务技能必须声明 async agent action。
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 9. 与宿主 API(参考)
|
||||||
|
|
||||||
|
| API | 用途 |
|
||||||
|
|-----|------|
|
||||||
|
| `GET /api/skill-actions/{skillSlug}` | 读 manifest |
|
||||||
|
| `POST /api/skill-actions/run` | 执行 action(sync inline / async job) |
|
||||||
|
| `GET /api/skill-actions/jobs` | 任务中心列表 |
|
||||||
|
| `GET /api/skill-actions/jobs/{jobId}` | 单 Job 详情 / 事件 |
|
||||||
|
|
||||||
|
Agent 工具(插件):`list_skill_actions` → `get_skill_action` → `run_skill_action`;正常对话**不要**轮询 `get_skill_action_job`(交给 UI / 任务中心)。
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
# 测试开发指南
|
# 测试开发指南
|
||||||
|
|
||||||
面向复制 `skill-template` 后的新业务 skill:**如何把自动化测试当作一等公民**,而不是等业务写完再补文档级别的空话。本文串起模板自带的 unittest 入口、`tests/` 目录分层与安全档位约定;**更细的开关取值、表格字段与环境变量组合仍以 [`tests/README.md`](../tests/README.md) 为权威来源**。建议你随手开一个编辑器分页:`references/TESTING.md`(本篇)、[`references/DEVELOPMENT.md`](DEVELOPMENT.md)(整体节奏)、[`tests/README.md`](../tests/README.md)(落地细则)。
|
面向复制 `skill-template` 后的新业务 skill:**如何把自动化测试当作一等公民**,而不是等业务写完再补文档级别的空话。本文串起模板自带的 unittest 入口、`tests/` 目录分层与安全档位约定;**更细的开关取值、表格字段与环境变量组合仍以 [`tests/README.md`](../tests/README.md) 为权威来源**。建议你随手开一个编辑器分页:`TESTING.md`(本篇)、[`DEVELOPMENT.md`](DEVELOPMENT.md)(整体节奏)、[`tests/README.md`](../tests/README.md)(落地细则)。
|
||||||
|
|
||||||
默认心智模型可以用一句话概括:**根目录 `test_*.py` = CI / 本地每次提交都应能通过的无外联套件**;`*integration*`、`*.sample`、`desktop/` = 只在人被明确要求时才启用的高风险或重量级路径。
|
默认心智模型可以用一句话概括:**根目录 `test_*.py` = CI / 本地每次提交都应能通过的无外联套件**;`*integration*`、`*.sample`、`desktop/` = 只在人被明确要求时才启用的高风险或重量级路径。
|
||||||
|
|
||||||
@@ -23,12 +23,18 @@
|
|||||||
必跑套件要像一个紧张的守门员:**快、确定、离线**。典型覆盖:
|
必跑套件要像一个紧张的守门员:**快、确定、离线**。典型覆盖:
|
||||||
|
|
||||||
- CLI:导入 [`cli.app`](../scripts/cli/app.py) 走解析链路、`health`(runtime diagnostics)/ `version` / `logs` / `log-get` 冒烟;
|
- CLI:导入 [`cli.app`](../scripts/cli/app.py) 走解析链路、`health`(runtime diagnostics)/ `version` / `logs` / `log-get` 冒烟;
|
||||||
- 架构守护:无 `scripts/jiangchang_skill_core/`、`platform-kit>=1.0.13` 导入来源、文档/runtime 标准(见 `test_platform_import.py` 等);
|
- 架构守护:无 `scripts/jiangchang_skill_core/`、`platform-kit>=1.2.0` 导入来源、文档/runtime 标准(见 `test_platform_import.py` 等);
|
||||||
- **真实 subprocess**:[`tests/test_entrypoint_subprocess.py`](../tests/test_entrypoint_subprocess.py) 再调用一遍 `python scripts/main.py`,防路径漂移;
|
- **真实 subprocess**:[`tests/test_entrypoint_subprocess.py`](../tests/test_entrypoint_subprocess.py) 再调用一遍 `python scripts/main.py`,防路径漂移;
|
||||||
- 运行时:`runtime_paths` 与 **`CLAW_*` / `JIANGCHANG_*` 并发兜底;
|
- 运行时:`runtime_paths` 与 **`JIANGCHANG_*` 隔离**;
|
||||||
- `SKILL.md` YAML slug vs [`constants.SKILL_SLUG`](../scripts/util/constants.py);
|
- `SKILL.md` YAML slug vs [`constants.SKILL_SLUG`](../scripts/util/constants.py);
|
||||||
- SQLite 骨架:`task_logs` 创建幂等与仓储读写;
|
- SQLite 骨架:`task_logs` 创建幂等与仓储读写;
|
||||||
|
- 数据管理元数据:`_jiangchang_*` 幂等初始化、中文展示名、`PRAGMA table_info` 字段顺序、editable 权限(见 `test_display_metadata.py`、`test_data_management_contract.py`);
|
||||||
|
- Skill Action manifest:`assets/actions.json` 结构、CLI 映射、placements 与 inputSchema 守护(见 `test_actions_manifest.py`);
|
||||||
|
- 标准时间字段:`created_at` / `updated_at` 默认值、trigger 维护与 `datetime_unix_seconds` 元数据(见 `test_timestamp_columns.py`);
|
||||||
- **adapter profile**:[`tests/test_adapter_profile_policy.py`](../tests/test_adapter_profile_policy.py) + [`adapter_test_utils`](../tests/adapter_test_utils.py) ——验证在未授权情况下绝不误判开启真实网络/RPA。
|
- **adapter profile**:[`tests/test_adapter_profile_policy.py`](../tests/test_adapter_profile_policy.py) + [`adapter_test_utils`](../tests/adapter_test_utils.py) ——验证在未授权情况下绝不误判开启真实网络/RPA。
|
||||||
|
- **发布打包守护**:[`tests/test_release_packaging_constraints.py`](../tests/test_release_packaging_constraints.py) ——`scripts/**/*.py` 单文件 < 1000 行、文本文件 UTF-8 without BOM。
|
||||||
|
- **开发规范守护(hard policy)**:[`tests/test_development_policy_guard.py`](../tests/test_development_policy_guard.py) ——规则索引见 [`development/POLICY_MATRIX.md`](POLICY_MATRIX.md),只执行可机器检测的 hard 规则。
|
||||||
|
- **slug 语义**:[`tests/test_slug_naming.py`](../tests/test_slug_naming.py) ——verb-noun-platform 命名规范校验。
|
||||||
|
|
||||||
**原则:`tests/test_*.py` 不允许隐形访问外网、不允许拉起真实浏览器、不允许读写开发者机器的真实数据根**。如果需要仿真服务器,也应仅在 integration(并由档位变量放行)。
|
**原则:`tests/test_*.py` 不允许隐形访问外网、不允许拉起真实浏览器、不允许读写开发者机器的真实数据根**。如果需要仿真服务器,也应仅在 integration(并由档位变量放行)。
|
||||||
|
|
||||||
@@ -38,7 +44,7 @@
|
|||||||
|
|
||||||
## 3. 数据隔离:IsolatedDataRoot
|
## 3. 数据隔离:IsolatedDataRoot
|
||||||
|
|
||||||
[`tests/_support.py`](../tests/_support.py) 提供的上下文管理器 `IsolatedDataRoot()`:进入一个专用临时目录,**镜像写入四套变量**:`CLAW_DATA_ROOT`、`JIANGCHANG_DATA_ROOT`(同一 tempfile)、以及用户镜像 ID:`CLAW_USER_ID`、`JIANGCHANG_USER_ID` → `_test`。
|
[`tests/_support.py`](../tests/_support.py) 提供的上下文管理器 `IsolatedDataRoot()`:进入一个专用临时目录,写入 `JIANGCHANG_DATA_ROOT`(tempfile)与 `JIANGCHANG_USER_ID` → `_test`。
|
||||||
|
|
||||||
结束时:**恢复原 environ**,删除目录。
|
结束时:**恢复原 environ**,删除目录。
|
||||||
|
|
||||||
@@ -70,22 +76,14 @@ def test_whatever():
|
|||||||
| `mock` | 与 `unit` 类似的安全档位(显式语义) |
|
| `mock` | 与 `unit` 类似的安全档位(显式语义) |
|
||||||
| `simulator_api` | 仅允许 **仿真 HTTP** 类集成(如 localhost) |
|
| `simulator_api` | 仅允许 **仿真 HTTP** 类集成(如 localhost) |
|
||||||
| `simulator_rpa` | 仅允许 **仿真页面 / 录播 RPA** |
|
| `simulator_rpa` | 仅允许 **仿真页面 / 录播 RPA** |
|
||||||
| `real_api` | 真实 API(另需 `ALLOW_REAL_API=1`) |
|
| `real_api` | 真实 API(显式设 `OPENCLAW_TEST_TARGET=real_api`) |
|
||||||
| `real_rpa` | 真实 RPA(另需 `ALLOW_REAL_RPA=1`) |
|
| `real_rpa` | 真实 RPA(显式设 `OPENCLAW_TEST_TARGET=real_rpa`) |
|
||||||
|
|
||||||
未设置环境变量 ⇒ 等价 `unit`。
|
未设置环境变量 ⇒ 等价 `unit`。
|
||||||
|
|
||||||
授权开关(显式 `1`)语义 **`ALLOW_REAL_API` / `ALLOW_REAL_RPA` / `ALLOW_WRITE_ACTIONS`**——摘录 [`tests/README.md`](../tests/README.md) §5.2:
|
默认策略摘要:**不要在 unittest 必跑路径误设 `OPENCLAW_TEST_TARGET=real_*`**。
|
||||||
|
|
||||||
| 变量 | 作用 |
|
档位读取与业务代码一致:经 `jiangchang_skill_core.config.get("OPENCLAW_TEST_TARGET")`(见 `CONFIG.md`)。
|
||||||
|------|------|
|
|
||||||
| `ALLOW_REAL_API=1` | 允许 `real_api` profile 访问真实 HTTP 通道 |
|
|
||||||
| `ALLOW_REAL_RPA=1` | 允许 `real_rpa` profile 驱动真实浏览器/RPA |
|
|
||||||
| `ALLOW_WRITE_ACTIONS=1` | 在 `real_*` 下允许**写**操作(提交表单、下单等) |
|
|
||||||
|
|
||||||
默认策略摘要见 §5.3:**不要在 unittest 必跑路径误把闸门打开**。
|
|
||||||
|
|
||||||
兼容别名:`OPENCLOW_TEST_TARGET`(历史拼写)。
|
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
@@ -134,6 +132,20 @@ Golden fixture 流程同理([`tests/samples/test_golden_cases.py.sample`](../t
|
|||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
|
## 7.1 simulator_rpa 联调检查表
|
||||||
|
|
||||||
|
浏览器 RPA 走 `simulator_rpa` 档位联调前,逐项确认:
|
||||||
|
|
||||||
|
- [ ] 数据目录 `.env` 中 `OPENCLAW_TEST_TARGET=simulator_rpa`(非 `mock` / `unit`)
|
||||||
|
- [ ] account-manager:`platform ensure` + 账号 `active` + 有 `profile_dir`
|
||||||
|
- [ ] 目标 sandbox UI 已部署(跨团队;本地可用 `sandbox/demo_app.html`)
|
||||||
|
- [ ] 失败先查 `rpa-artifacts/` 截图,再查 Chrome profile 缓存(`--user-data-dir` 手工打开清站点数据)
|
||||||
|
- [ ] 默认 `python tests/run_tests.py -v` 仍全部通过(mock 离线);example 内 `pytest` 不启真实浏览器
|
||||||
|
|
||||||
|
参考实现:`examples/simulator_browser_rpa/README.md`。
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
## 8. 新 skill 的最小测试清单
|
## 8. 新 skill 的最小测试清单
|
||||||
|
|
||||||
以下清单 **原文摘自 [`tests/README.md`](../tests/README.md) 「新技能最小测试清单」**(复制新仓库后逐项勾选):
|
以下清单 **原文摘自 [`tests/README.md`](../tests/README.md) 「新技能最小测试清单」**(复制新仓库后逐项勾选):
|
||||||
@@ -147,6 +159,8 @@ Golden fixture 流程同理([`tests/samples/test_golden_cases.py.sample`](../t
|
|||||||
- [ ] 至少有 1 个缺必填字段 / 非法输入测试。
|
- [ ] 至少有 1 个缺必填字段 / 非法输入测试。
|
||||||
- [ ] 如果有 adapter,至少覆盖 timeout / unauthorized / invalid response(可用 fake 模拟)。
|
- [ ] 如果有 adapter,至少覆盖 timeout / unauthorized / invalid response(可用 fake 模拟)。
|
||||||
- [ ] 如果有解析 / 计算 / 校验类业务,至少保留 1 组 golden fixture(脱敏)。
|
- [ ] 如果有解析 / 计算 / 校验类业务,至少保留 1 组 golden fixture(脱敏)。
|
||||||
|
- [ ] 若有 `assets/actions.json`,`tests/test_actions_manifest.py` 通过;不需要 Action 时可删除 manifest。
|
||||||
|
- [ ] 若有本地 SQLite 业务表,`_jiangchang_*` 元数据与 `tests/test_data_management_contract.py` 规则一致。
|
||||||
- [ ] 真实 API / 真实 RPA 测试只放 `tests/integration/`,并且默认 `.sample`,不默认运行。
|
- [ ] 真实 API / 真实 RPA 测试只放 `tests/integration/`,并且默认 `.sample`,不默认运行。
|
||||||
|
|
||||||
---
|
---
|
||||||
@@ -163,13 +177,40 @@ Golden fixture 流程同理([`tests/samples/test_golden_cases.py.sample`](../t
|
|||||||
| **零硬编码凭证** | token / cookie / 生产 URL → 用虚构域名或 vault ref |
|
| **零硬编码凭证** | token / cookie / 生产 URL → 用虚构域名或 vault ref |
|
||||||
| mock 优先 | 逻辑应在 service + FakeAdapter,而不是巨胖 CLI 断言 |
|
| mock 优先 | 逻辑应在 service + FakeAdapter,而不是巨胖 CLI 断言 |
|
||||||
| 结构化错误 | 断言错误码字段,而不是 substring of stderr 漂移集合 |
|
| 结构化错误 | 断言错误码字段,而不是 substring of stderr 漂移集合 |
|
||||||
| **integration = 显式开关** | `OPENCLAW_TEST_TARGET` + `ALLOW_REAL_*` / `ALLOW_WRITE_ACTIONS` |
|
| **integration = 显式档位** | 仅 `OPENCLAW_TEST_TARGET`(`real_*` 只放 integration / 手动触发) |
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 9.1 开发规范守护测试
|
||||||
|
|
||||||
|
[`tests/test_development_policy_guard.py`](../tests/test_development_policy_guard.py) 将 [`development/POLICY_MATRIX.md`](POLICY_MATRIX.md) 中的 **hard policy** 落地为默认必跑 unittest:
|
||||||
|
|
||||||
|
- 每条失败信息包含 `policy_id` 与来源文档路径,便于定位规范出处。
|
||||||
|
- 只覆盖目录结构、runtime 依赖边界、配置读取、默认测试安全档位、RPA 禁令等**可静态扫描**规则;软规则列在矩阵「暂不自动检测」小节,**不**做成测试。
|
||||||
|
- `POLICY-TESTING-001`:根目录 `tests/test_*.py` 非白名单文件不得出现 `real_api` / `real_rpa` / `OPENCLAW_TEST_TARGET`;**唯一白名单** `test_adapter_profile_policy.py`(档位 helper 自测)。`adapter_test_utils.py` 与 `tests/integration/*.sample` 不在默认套件内,不受此条扫描。
|
||||||
|
- `POLICY-RPA-002`:只禁止 `subprocess` / `os.system` / `os.popen` **直接执行** ffmpeg,不误伤 `ffmpeg_path` 等诊断字段。
|
||||||
|
- 与已有测试分工:`test_release_packaging_constraints.py` 继续守护 PyArmor 行数与 UTF-8 BOM;`test_no_rpa_helpers_import.py` 与 policy guard 的 `POLICY-RPA-001` 语义一致,二者并存防回归。
|
||||||
|
|
||||||
|
### 日志规范测试
|
||||||
|
|
||||||
|
[`tests/test_development_policy_guard.py`](../tests/test_development_policy_guard.py) 中的 `POLICY-LOGGING-*` 规则会检查 logging **hard policy**(见 [`POLICY_MATRIX.md`](POLICY_MATRIX.md)):
|
||||||
|
|
||||||
|
- CLI 是否调用 `setup_skill_logging`(`POLICY-LOGGING-001`)
|
||||||
|
- `task_service.py` 是否使用 `get_skill_logger`、含 `log.info` / `log.exception`、写入 `save_task_log`(`POLICY-LOGGING-002` / `003`)
|
||||||
|
- 长任务模板是否使用 Activity 或 RPA video step(`POLICY-LOGGING-004`)
|
||||||
|
- 交付代码是否在 logging 调用中拼接明显敏感字段(`POLICY-LOGGING-005`)
|
||||||
|
|
||||||
|
**局限**:自动检测只覆盖可静态判断的**底线**,**不**判断业务日志是否足够完整。复制新 skill 后,仍须人工对照 [`LOGGING.md`](LOGGING.md) §必打日志节点清单做 code review。
|
||||||
|
|
||||||
|
新增或调整开发规范时:**先更新 `POLICY_MATRIX.md`,再补/改 `test_development_policy_guard.py`(或注明已有测试覆盖)**。
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## 10. 测试和发布的关系
|
## 10. 测试和发布的关系
|
||||||
|
|
||||||
**在运行 [`release.ps1`](../release.ps1) 之前,`python tests/run_tests.py -v` 必须绿色**。失败的默认套件意味着:
|
[`release.ps1`](../release.ps1) **默认**在 **auto commit 之前**、创建 tag 之前,对**当前工作区**执行 `python tests/run_tests.py -v`;测试失败会终止 release,不会自动提交、不会打 tag。仅紧急排障时可传 `-SkipTests`(会输出黄色警告,不应作为常规发布路径)。`-DryRun` 仍会实际运行默认测试。
|
||||||
|
|
||||||
|
即便手工发布,也应在运行 `release.ps1` 之前确认 `python tests/run_tests.py -v` 绿色。失败的默认套件意味着:
|
||||||
|
|
||||||
- 打包路径可能根本不可运行;
|
- 打包路径可能根本不可运行;
|
||||||
- CI 加密前的静态假设可能在宿主崩溃;
|
- CI 加密前的静态假设可能在宿主崩溃;
|
||||||
@@ -195,10 +236,11 @@ Golden fixture 流程同理([`tests/samples/test_golden_cases.py.sample`](../t
|
|||||||
|
|
||||||
## 12. RPA / video 测试标准
|
## 12. RPA / video 测试标准
|
||||||
|
|
||||||
- `RpaVideoSession` 调用**不跑真实 ffmpeg**:单测中使用 `unittest.mock` patch session 或设 `OPENCLAW_RECORD_VIDEO=0`。
|
- 模板 `.env.example` 默认 **`OPENCLAW_RECORD_VIDEO=0`**;默认必跑测试**不应启动真实 ffmpeg**(patch session 或显式设 `OPENCLAW_RECORD_VIDEO=0`)。
|
||||||
|
- **但测试必须守护录屏接入能力**(`POLICY-RPA-003` 扫描 `scripts/service/*.py`):`RpaVideoSession` 被构造、`video.add_step` 被调用、video artifact 结构 merge 进 `result_summary`(`video` / `video_path` 等字段),与默认是否录屏无关。
|
||||||
- 断言 `title` / `closing_title` 是**中文**业务文案。
|
- 断言 `title` / `closing_title` 是**中文**业务文案。
|
||||||
- 断言 video artifact 会进入 `result_summary`(`video_path`、`raw_video`、`video_log` 等)。
|
|
||||||
- 断言 step 文案贴近用户动作,不是技术日志(如「准备执行示例任务」而非「enter cmd_run」)。
|
- 断言 step 文案贴近用户动作,不是技术日志(如「准备执行示例任务」而非「enter cmd_run」)。
|
||||||
|
- 若需测试**真实录屏**(ffmpeg 成片),必须显式设 `OPENCLAW_RECORD_VIDEO=1`,并放在 `tests/integration/` 或手动触发场景,**不得**进入默认 unittest 套件。
|
||||||
|
|
||||||
参考 `tests/test_video_service.py`。
|
参考 `tests/test_video_service.py`。
|
||||||
|
|
||||||
@@ -218,15 +260,17 @@ Golden fixture 流程同理([`tests/samples/test_golden_cases.py.sample`](../t
|
|||||||
|
|
||||||
- [ ] `requirements.txt` **不含** `jiangchang-platform-kit` / `playwright`
|
- [ ] `requirements.txt` **不含** `jiangchang-platform-kit` / `playwright`
|
||||||
- [ ] 无 `scripts/jiangchang_skill_core/` vendored 副本
|
- [ ] 无 `scripts/jiangchang_skill_core/` vendored 副本
|
||||||
- [ ] `platform_kit_min_version` **>= 1.0.13**(`SKILL.md` + `constants.py`)
|
- [ ] `platform_kit_min_version` **>= 1.2.0**(`SKILL.md` + `constants.py`)
|
||||||
- [ ] `health` 能输出 `platform_kit_version_ok`(或等价诊断行)
|
- [ ] `health` 能输出 `platform_kit_version_ok`(或等价诊断行)
|
||||||
- [ ] `config-path` 可输出用户 `.env` 路径 JSON
|
- [ ] `config-path` 可输出用户 `.env` 路径 JSON
|
||||||
- [ ] `pytest.ini` 存在且 `python_files` 只收集 `test_*.py` / `*_test.py`
|
- [ ] `pytest.ini` 存在且 `python_files` 只收集 `test_*.py` / `*_test.py`
|
||||||
- [ ] pytest **不会**误收集 `tests/` 下的 `.txt` / 日志 / 结果文件
|
- [ ] pytest **不会**误收集 `tests/` 下的 `.txt` / 日志 / 结果文件
|
||||||
- [ ] `python tests/run_tests.py -v` 全部通过
|
- [ ] `python tests/run_tests.py -v` 全部通过
|
||||||
|
- [ ] `scripts/**/*.py` 单文件 < 1000 行(`test_release_packaging_constraints.py`)
|
||||||
|
- [ ] 文本文件为 UTF-8 without BOM,无 `U+FEFF`(同上)
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
- [`tests/README.md`](../tests/README.md) — 表格、变量与目录细则
|
- [`tests/README.md`](../tests/README.md) — 表格、变量与目录细则
|
||||||
- [`references/DEVELOPMENT.md`](DEVELOPMENT.md) §14.5 — 测试驱动的开发顺序
|
- [`DEVELOPMENT.md`](DEVELOPMENT.md) §14.5 — 测试驱动的开发顺序
|
||||||
- [`tests/integration/README.md`](../tests/integration/README.md) — 高风险用法
|
- [`tests/integration/README.md`](../tests/integration/README.md) — 高风险用法
|
||||||
79
examples/README.md
Normal file
79
examples/README.md
Normal file
@@ -0,0 +1,79 @@
|
|||||||
|
# Skill Template Examples
|
||||||
|
|
||||||
|
`examples/` 是面向**技术人员与 AI 编程工具**的**核心实现案例**目录:提供可运行、可测试、可复制的参考代码,用于指导新技能的 `scripts/` 应如何编写。
|
||||||
|
|
||||||
|
**`examples/` 不是完整 skill 仓库。** 不要整包照搬 `examples/<mode>/` 到新技能根目录。应按各 example README 的 **copy map**,**选择性复制**到新技能的 `scripts/`(及按需的 `tests/`)。
|
||||||
|
|
||||||
|
根 `README.md`、`SKILL.md`、`references/`、`development/` 仍按模板主目录规则编写,**不要**从 `examples/` 复制。
|
||||||
|
|
||||||
|
## 四类外部交互方式
|
||||||
|
|
||||||
|
一级目录固定为四种模式(不可删除、不可移动):
|
||||||
|
|
||||||
|
| 目录 | 类型 | 状态 | 适用场景 |
|
||||||
|
|---|---|---|---|
|
||||||
|
| `real_browser_rpa` | 真实浏览器 RPA | **已有案例** | 真实第三方网站、登录态、人工验证、滚动采集 |
|
||||||
|
| `simulator_browser_rpa` | 仿真浏览器 RPA | **已有案例** | 自有 sandbox、可控页面、表单 RPA、adapter 分层 |
|
||||||
|
| `real_api` | 真实 API | **占位** | 真实系统接口、token、权限、限流(尚未沉淀可复制实现) |
|
||||||
|
| `simulator_api` | 仿真 API | **占位** | mock/sandbox API、无需浏览器(尚未沉淀可复制实现) |
|
||||||
|
|
||||||
|
## 两类浏览器 RPA 的区别
|
||||||
|
|
||||||
|
| | real_browser_rpa | simulator_browser_rpa(修正后) |
|
||||||
|
|---|---|---|
|
||||||
|
| 目标 | 真实第三方网页 | 自有仿真页面 / sandbox / jc2009 类 |
|
||||||
|
| 风控 | 高(验证码、登录态、反爬) | 低(DOM 可控) |
|
||||||
|
| 账号 | account-manager + profile | account-manager + profile(`simulator_rpa` 档) |
|
||||||
|
| 用途 | 生产级真实采集/操作参考 | adapter 分层 + 表单 RPA 教学 |
|
||||||
|
|
||||||
|
### 工程范式对比
|
||||||
|
|
||||||
|
| 工程范式 | real_browser_rpa | simulator_browser_rpa(修正后) |
|
||||||
|
|---|---|---|
|
||||||
|
| async Playwright | ✅ | ✅ |
|
||||||
|
| 薄 adapter + `*_playwright.py` | `task_rpa.py` | `simulator_playwright.py` |
|
||||||
|
| account-manager subprocess | ✅ | ✅(`simulator_rpa` 档位) |
|
||||||
|
|
||||||
|
两者工程范式一致,差异在目标系统(真实站 vs 仿真站/本地 demo)。
|
||||||
|
|
||||||
|
## 如何选择示例
|
||||||
|
|
||||||
|
| 你的场景 | 参考目录 |
|
||||||
|
|---|---|
|
||||||
|
| 真实网站 + 浏览器 + 登录/验证码/滚动 | **real_browser_rpa** |
|
||||||
|
| 仿真页面 + 浏览器 + 表单/批量提交 | **simulator_browser_rpa** |
|
||||||
|
| 真实系统 API | **real_api**(当前占位,不可作为实现参考) |
|
||||||
|
| mock / sandbox API(无浏览器) | **simulator_api**(当前占位,不可作为实现参考) |
|
||||||
|
|
||||||
|
## 轻量结构约定
|
||||||
|
|
||||||
|
每个 `examples/<mode>/` 推荐结构:
|
||||||
|
|
||||||
|
```text
|
||||||
|
examples/<mode>/
|
||||||
|
README.md
|
||||||
|
scripts/
|
||||||
|
service/
|
||||||
|
util/
|
||||||
|
tests/
|
||||||
|
```
|
||||||
|
|
||||||
|
- 仅 `simulator_browser_rpa` 可额外包含 `sandbox/`
|
||||||
|
- 仅未来确实需要样例数据时可加 `fixtures/`
|
||||||
|
- **不要**在 `examples/<mode>/` 下要求或添加:`SKILL.md`、根市场 `README.md`、`references/`、`development/`、`assets/`、`.github/`、release workflow 等完整 skill 仓库层文件
|
||||||
|
|
||||||
|
## `scripts/` 与 `examples/` 边界
|
||||||
|
|
||||||
|
| | `scripts/` | `examples/` |
|
||||||
|
|---|---|---|
|
||||||
|
| 定位 | 真实技能业务逻辑的标准骨架 | 核心实现案例与复制参考 |
|
||||||
|
| 内容 | 干净的技能骨架,无教学/demo 代码 | 可运行参考实现,非完整 skill |
|
||||||
|
| 旧示例 | **不得**出现 `example_*` 目录或文件 | 四类一级目录固定保留 |
|
||||||
|
|
||||||
|
复制规则:
|
||||||
|
|
||||||
|
- 真实浏览器 RPA → 参考 `examples/real_browser_rpa/`
|
||||||
|
- 仿真浏览器 RPA → 参考 `examples/simulator_browser_rpa/`(adapter 分层权威参考:`scripts/service/adapter/`)
|
||||||
|
- `real_api/`、`simulator_api/` 当前为规划占位,**不得**作为实现参考
|
||||||
|
|
||||||
|
> 示例提供的是**参考架构与边界**,不是业务代码原样复制。复制前先读对应 README 的 copy map 与「禁止照抄」章节。
|
||||||
35
examples/real_api/README.md
Normal file
35
examples/real_api/README.md
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
# Real API Example
|
||||||
|
|
||||||
|
这个目录用于沉淀「真实系统 API」类 skill 的可复制成功案例。
|
||||||
|
|
||||||
|
## 适用场景(规划)
|
||||||
|
|
||||||
|
- 目标系统提供真实 API
|
||||||
|
- 需要 token / secret / credential
|
||||||
|
- 需要请求重试、权限校验、限流处理、schema 校验和幂等写库
|
||||||
|
|
||||||
|
## 当前状态:规划占位
|
||||||
|
|
||||||
|
- **本目录当前为规划占位**,仅保留最小结构(`README.md`、`scripts/`、`tests/` 占位)。
|
||||||
|
- **尚未沉淀可复制实现**;目录内无参考代码。
|
||||||
|
- **AI 编程工具不得**基于空目录自行脑补 API 对接实现。
|
||||||
|
- 后续补齐可复制案例后,再按 README copy map 选择性复制到新技能 `scripts/`。
|
||||||
|
|
||||||
|
## 轻量结构
|
||||||
|
|
||||||
|
```text
|
||||||
|
real_api/
|
||||||
|
README.md
|
||||||
|
scripts/
|
||||||
|
service/
|
||||||
|
util/
|
||||||
|
tests/
|
||||||
|
```
|
||||||
|
|
||||||
|
不要在本目录添加 `SKILL.md`、市场 README、`references/`、`development/`、`.github/` 等完整 skill 仓库层文件。
|
||||||
|
|
||||||
|
## 复制边界(待案例补齐后生效)
|
||||||
|
|
||||||
|
- 参考代码将从 `examples/real_api/scripts/` 选择性复制到新技能 `scripts/`
|
||||||
|
- 根 `README.md`、`SKILL.md`、`references/` 仍按模板主目录规则编写
|
||||||
|
- 当前阶段:**不可作为实现参考**
|
||||||
0
examples/real_api/scripts/service/.gitkeep
Normal file
0
examples/real_api/scripts/service/.gitkeep
Normal file
0
examples/real_api/scripts/util/.gitkeep
Normal file
0
examples/real_api/scripts/util/.gitkeep
Normal file
0
examples/real_api/tests/.gitkeep
Normal file
0
examples/real_api/tests/.gitkeep
Normal file
193
examples/real_browser_rpa/README.md
Normal file
193
examples/real_browser_rpa/README.md
Normal file
@@ -0,0 +1,193 @@
|
|||||||
|
# Real Browser RPA Example
|
||||||
|
|
||||||
|
这是一个真实网站浏览器 RPA skill 的可复制参考实现,适用于需要登录、人工验证、搜索、滚动加载、采集和入库的场景。
|
||||||
|
|
||||||
|
**说明:**
|
||||||
|
|
||||||
|
- 本示例是通用模板,不绑定具体平台。
|
||||||
|
- 示例抽象自已跑通的真实浏览器 RPA skill(抖音视频链接采集),但代码已改成中性示例命名。
|
||||||
|
- 新 skill 开发时可以复制本目录,再替换目标站点、选择器、解析逻辑和业务字段。
|
||||||
|
|
||||||
|
## 适用场景
|
||||||
|
|
||||||
|
- 真实网站操作(非 sandbox / mock)
|
||||||
|
- 需要登录态与 persistent Chrome/Edge profile
|
||||||
|
- 需要通过 account-manager 管理账号和 `profile_dir`
|
||||||
|
- 需要处理人工登录、滑块验证码、短信验证码
|
||||||
|
- 需要关键词搜索、滚动加载、采集可见条目
|
||||||
|
- 需要结构化日志、用户友好的停止原因、失败留痕(截图/视频)
|
||||||
|
|
||||||
|
## 目录结构
|
||||||
|
|
||||||
|
```
|
||||||
|
real_browser_rpa/
|
||||||
|
├── README.md
|
||||||
|
├── scripts/
|
||||||
|
│ ├── service/
|
||||||
|
│ │ ├── account_client.py # subprocess 调用 account-manager
|
||||||
|
│ │ ├── browser_session.py # 系统 Chrome + persistent context
|
||||||
|
│ │ ├── human_verification.py # 滑块/短信/保存登录弹窗检测(不依赖 task_rpa 错误类型)
|
||||||
|
│ │ ├── task_rpa.py # RPA 主流程
|
||||||
|
│ │ └── task_service.py # 轻量编排(示例无 DB)
|
||||||
|
│ └── util/
|
||||||
|
│ ├── constants.py # 平台常量、selector 占位
|
||||||
|
│ └── logging.py # 结构化日志与脱敏
|
||||||
|
└── tests/
|
||||||
|
├── test_human_verification.py # 验证码误判防护
|
||||||
|
└── test_stop_reason.py # 停止原因文案
|
||||||
|
```
|
||||||
|
|
||||||
|
## 核心流程
|
||||||
|
|
||||||
|
1. **pick account** — 通过 `account_client.pick_web_account()` 获取 `profile_dir` 与租约
|
||||||
|
2. **启动浏览器** — `launch_persistent_context` + `new_page()` + `page.goto(start_url)`
|
||||||
|
3. **人工验证(启动后)** — 检测滑块/短信,等待用户完成
|
||||||
|
4. **等待登录** — 检测登录按钮消失 / 登录态 marker 出现
|
||||||
|
5. **保存登录弹窗** — 等待“是否保存登录信息”倒计时消失
|
||||||
|
6. **人工验证(登录后)** — 再次检测
|
||||||
|
7. **搜索** — 定位搜索框 → 输入关键词 → 点击搜索
|
||||||
|
8. **等待结果容器** — `#search-result-container`(示例 selector)
|
||||||
|
9. **滚动采集循环** — 采集可见条目 → 判断 stop_reason → 滚动加载
|
||||||
|
10. **返回结果** — `ScrapeRunResult` + `format_stop_reason_for_user()`
|
||||||
|
11. **释放租约** — `finally` 中 `release_lease()`
|
||||||
|
|
||||||
|
## 开发新 skill 时如何复制
|
||||||
|
|
||||||
|
1. **先阅读**本 README 及下方「Copy map」「复制边界」章节
|
||||||
|
2. 按 copy map **选择性复制**到新技能 `scripts/`(**不要**整包照搬 `examples/real_browser_rpa/`)
|
||||||
|
3. 修改 `util/constants.py` 中的 `TARGET_PLATFORM`、`DEFAULT_START_URL`、selector 常量
|
||||||
|
4. 替换 `task_rpa.py` 中的登录态 marker、搜索框/结果解析逻辑(**必须 F12 实测**)
|
||||||
|
5. 替换 `human_verification.py` 中的验证码 selector(若目标站点 DOM 不同)
|
||||||
|
6. 在 `task_service.py` 中接入真实 DB、任务日志、RpaVideoSession
|
||||||
|
7. 在新技能根目录补齐 `scripts/main.py` CLI;`SKILL.md` 与根 `README.md` 按模板主目录规则编写,**不要**复制本 README
|
||||||
|
|
||||||
|
## Copy map(复制到新技能 `scripts/`)
|
||||||
|
|
||||||
|
| 来源(examples) | 目标(新技能) | 说明 |
|
||||||
|
|---|---|---|
|
||||||
|
| `examples/real_browser_rpa/scripts/service/browser_session.py` | `scripts/service/browser_session.py` | persistent context 启动范式 |
|
||||||
|
| `examples/real_browser_rpa/scripts/service/account_client.py` | `scripts/service/account_client.py` | account-manager CLI 集中封装 |
|
||||||
|
| `examples/real_browser_rpa/scripts/service/human_verification.py` | `scripts/service/human_verification.py` | 人工验证等待 |
|
||||||
|
| `examples/real_browser_rpa/scripts/service/task_rpa.py` | `scripts/service/<业务名>_rpa.py` 或 `scripts/service/task_rpa.py` | RPA 主流程,按业务改名 |
|
||||||
|
| `examples/real_browser_rpa/scripts/service/task_service.py` | `scripts/service/task_service.py` | 参考并合并编排层 |
|
||||||
|
| `examples/real_browser_rpa/scripts/util/constants.py` | `scripts/util/constants.py` | 平台常量与 selector(须替换) |
|
||||||
|
| `examples/real_browser_rpa/scripts/util/logging.py` | `scripts/util/logging.py` | 可选,按需合并 |
|
||||||
|
| `examples/real_browser_rpa/tests/` | `tests/` | 可按需复制测试思路 |
|
||||||
|
| `examples/real_browser_rpa/README.md` | — | **只读参考**,不复制为新技能根 README |
|
||||||
|
|
||||||
|
**复制时必须替换:** selector、URL、平台名称、账号平台字段、slug、logger 名等业务标识。**不要**照搬示例字段到其他平台。
|
||||||
|
|
||||||
|
**浏览器与启动约束:**
|
||||||
|
|
||||||
|
- **不要**在技能内安装 Playwright;浏览器与 Python 包由宿主/runtime 提供
|
||||||
|
- `launch_persistent_context` 的 `args` **只放 Chrome 参数**;**不要把 URL 放进 `args`**
|
||||||
|
- 页面必须通过 `new_page()` + `goto()`,或通过真实地址栏/点击进入
|
||||||
|
|
||||||
|
## 复制边界:哪些能复制,哪些必须替换
|
||||||
|
|
||||||
|
### 可以直接参考/复制的
|
||||||
|
|
||||||
|
- `browser_session.py` 的启动结构:**persistent context → new_page → goto**
|
||||||
|
- `human_verification.py` 的人工验证等待模式(容器级检测 + 等待人工,不自动破解)
|
||||||
|
- `task_rpa.py` 的流程分层、结构化日志点、`stop_reason` 设计
|
||||||
|
- `account_client.py` 的 account-manager CLI **集中封装**方式(subprocess 仅限此文件)
|
||||||
|
- `tests/test_human_verification.py` 的误判防护测试思路
|
||||||
|
|
||||||
|
### 必须替换的
|
||||||
|
|
||||||
|
- `SKILL_SLUG`
|
||||||
|
- `TARGET_PLATFORM`
|
||||||
|
- `LEASE_HOLDER`
|
||||||
|
- `DEFAULT_START_URL`
|
||||||
|
- `SEARCH_INPUT_SELECTORS` / `SEARCH_BUTTON_SELECTOR`
|
||||||
|
- `RESULT_CONTAINER_SELECTOR` / `RESULT_ITEM_SELECTOR`
|
||||||
|
- `SCROLL_CONTAINER_SELECTORS`
|
||||||
|
- `RESULT_END_TEXT`
|
||||||
|
- `LOGGED_IN_SELECTORS` / `LOGGED_OUT_SELECTOR`(见下方说明)
|
||||||
|
- `_parse_item_locator()` 业务字段解析
|
||||||
|
- 用户可见中文文案
|
||||||
|
- 数据库写入逻辑
|
||||||
|
- 任务日志逻辑
|
||||||
|
- 录屏/截图业务标题
|
||||||
|
|
||||||
|
### 禁止照抄的
|
||||||
|
|
||||||
|
- 不要照抄目标站点 selector(示例 selector 不等于真实 DOM)
|
||||||
|
- 不要照抄平台名、slug、logger 名
|
||||||
|
- 不要依赖 `D:\OpenClaw\client-commons\account-manager\scripts\main.py` 等开发机绝对路径作为生产依赖
|
||||||
|
- 不要把示例里的 result selector 当成真实站点 selector
|
||||||
|
- 不要把示例代码原样发布为业务 skill
|
||||||
|
- 不要把真实账号、手机号、cookie、token 写入代码或 README
|
||||||
|
|
||||||
|
### 登录 selector 说明
|
||||||
|
|
||||||
|
`task_rpa.py` 中的 `LOGGED_IN_SELECTORS` 与 `LOGGED_OUT_SELECTOR` **只是示例**,偏中文「登录」按钮和常见站点结构。复制到新 skill 后:
|
||||||
|
|
||||||
|
- **必须**用目标站点 F12 / DOM 实测后替换
|
||||||
|
- **不允许**凭经验猜 selector
|
||||||
|
- **不允许**因为示例在本机曾跑通就直接照搬到其他平台
|
||||||
|
|
||||||
|
### account-manager 路径解析说明
|
||||||
|
|
||||||
|
`account_client.py` 末尾的 `D:\OpenClaw\client-commons\account-manager\scripts\main.py` 是**开发环境兜底路径**,仅用于模板/本机调试。复制到真实 skill 后:
|
||||||
|
|
||||||
|
- **优先**通过 `ACCOUNT_MANAGER_ROOT`、Platform Kit `get_sibling_skills_root` 或宿主运行环境解析 account-manager
|
||||||
|
- **不要**把个人机器路径作为生产依赖
|
||||||
|
|
||||||
|
## 必须保留的安全原则
|
||||||
|
|
||||||
|
- **不 import account-manager 内部模块** — 只通过 CLI/subprocess 调用
|
||||||
|
- **不自动破解验证码** — 滑块/短信只检测 + 等待人工完成
|
||||||
|
- **日志脱敏** — 不输出完整手机号/账号
|
||||||
|
- **租约释放** — `pick-web --lease` 后必须在 `finally` 释放
|
||||||
|
- **失败留痕** — 真实 skill 应在关键失败点截图(示例中已留注释位)
|
||||||
|
|
||||||
|
## 常见坑
|
||||||
|
|
||||||
|
| 坑 | 正确做法 |
|
||||||
|
|---|---|
|
||||||
|
| 直接 `import account_manager.service...` | 通过 `subprocess` 调用 `account-manager/scripts/main.py` |
|
||||||
|
| 把 URL 放进 `launch_persistent_context(args=[url])` | `args` 只放 Chrome 参数;URL 用 `page.goto(start_url)` |
|
||||||
|
| 启动后直接操作 DOM | 先 `context.new_page()` 再 `page.goto()` |
|
||||||
|
| 用整页 `body.inner_text()` 判断验证码 | 限定在 `[role='dialog']`、`.vc-captcha-verify` 等容器 |
|
||||||
|
| 搜索结果含“验证码教程”文字就误判 | 单元测试覆盖:无验证 DOM 时不应 `present=True` |
|
||||||
|
| 无结构化日志 | 关键节点打 `rpa_start` / `browser_ready` / `collect_round` 等 |
|
||||||
|
| 把 CLI 参数暴露给普通用户 | 用户说明放根 `README.md`;运行契约与触发规则放 `SKILL.md`(LLM/平台入口) |
|
||||||
|
|
||||||
|
## 需要替换的内容清单
|
||||||
|
|
||||||
|
| 文件 | 替换项 |
|
||||||
|
|---|---|
|
||||||
|
| `util/constants.py` | `SKILL_SLUG`、`TARGET_PLATFORM`、`DEFAULT_START_URL`、所有 selector |
|
||||||
|
| `browser_session.py` | `get_start_url()` 配置来源、stealth 脚本(可选) |
|
||||||
|
| `human_verification.py` | `SLIDER_SELECTORS`、`SMS_SELECTORS`、`DIALOG_TEXT_CONTAINERS` |
|
||||||
|
| `task_rpa.py` | `LOGGED_IN_SELECTORS`、`LOGGED_OUT_SELECTOR`、`_parse_item_locator()` |
|
||||||
|
| `account_client.py` | `PLACEHOLDER_PLATFORM`、`LEASE_HOLDER`、账号 setup 文案 |
|
||||||
|
| `task_service.py` | 接入 DB、任务日志、entitlement、视频留痕 |
|
||||||
|
|
||||||
|
## 依赖说明
|
||||||
|
|
||||||
|
本目录是 **示例**,不影响 skill-template 根目录运行。真实 skill 需要:
|
||||||
|
|
||||||
|
- Python 3.10+
|
||||||
|
- Python 包 `playwright`:属于**宿主共享 runtime** 的通用能力;技能侧**不要**在 `requirements.txt` 中重复声明,也**不要**在代码或脚本中执行 `pip install playwright`
|
||||||
|
- 真实 skill **默认使用系统 Chrome/Edge**,**不使用** Playwright 下载的内置 Chromium
|
||||||
|
- 技能侧**不要**执行 `playwright install`、`playwright install chromium` 或类似命令
|
||||||
|
- 若本地独立开发环境缺少 Playwright Python 包,只能作为开发机临时环境补装;**生产/宿主运行**由共享 runtime 解决
|
||||||
|
- `jiangchang_skill_core`(如 `find_chrome_executable` 等,由 platform-kit 提供)
|
||||||
|
- 兄弟 skill `account-manager`
|
||||||
|
|
||||||
|
## 运行测试
|
||||||
|
|
||||||
|
```bash
|
||||||
|
cd examples/real_browser_rpa
|
||||||
|
python -m pytest tests/ -v
|
||||||
|
```
|
||||||
|
|
||||||
|
测试为纯函数/Mock DOM,**不需要启动真实浏览器**。
|
||||||
|
|
||||||
|
## 当前状态
|
||||||
|
|
||||||
|
- 示例代码已就绪,可直接作为新 skill 复制起点。
|
||||||
|
- 未绑定具体平台业务字段;selector 为占位示例。
|
||||||
|
- 未接入真实数据库与 CLI 入口。
|
||||||
201
examples/real_browser_rpa/scripts/service/account_client.py
Normal file
201
examples/real_browser_rpa/scripts/service/account_client.py
Normal file
@@ -0,0 +1,201 @@
|
|||||||
|
"""account-manager CLI 集成(仅 subprocess,不 import 兄弟 skill 内部模块)。"""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import json
|
||||||
|
import logging
|
||||||
|
import os
|
||||||
|
import subprocess
|
||||||
|
import sys
|
||||||
|
from typing import Any, Dict, List, Optional
|
||||||
|
|
||||||
|
from jiangchang_skill_core.runtime_env import get_sibling_skills_root
|
||||||
|
|
||||||
|
from util.constants import LEASE_HOLDER, LEASE_TTL_SEC, TARGET_PLATFORM
|
||||||
|
from util.logging import mask_text
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
PLACEHOLDER_PLATFORM = TARGET_PLATFORM
|
||||||
|
|
||||||
|
ACCOUNT_SETUP_MESSAGE = (
|
||||||
|
f"未找到可用的 {PLACEHOLDER_PLATFORM} 账号,所以还没有打开浏览器。"
|
||||||
|
f"请先在 account-manager 中添加 platform={PLACEHOLDER_PLATFORM}、"
|
||||||
|
"status=active、带 profile_dir 的账号后重新运行。"
|
||||||
|
)
|
||||||
|
|
||||||
|
LEASE_BUSY_MESSAGE = "目标平台账号当前被其他任务占用,请等待释放租约后重试。"
|
||||||
|
|
||||||
|
|
||||||
|
class AccountManagerError(Exception):
|
||||||
|
def __init__(self, code: str, message: str) -> None:
|
||||||
|
super().__init__(message)
|
||||||
|
self.code = code
|
||||||
|
self.message = message
|
||||||
|
|
||||||
|
|
||||||
|
def mask_login_id(login_id: str) -> str:
|
||||||
|
return mask_text(login_id)
|
||||||
|
|
||||||
|
|
||||||
|
def _resolve_account_manager_main() -> str:
|
||||||
|
"""解析 account-manager CLI 入口路径。
|
||||||
|
|
||||||
|
优先级:ACCOUNT_MANAGER_ROOT → get_sibling_skills_root(路径推断 / JIANGCHANG_SKILLS_ROOT)→ 开发机兜底。
|
||||||
|
末尾 dev 路径仅用于模板/本机调试,复制到真实 skill 后不要依赖个人机器绝对路径。
|
||||||
|
"""
|
||||||
|
env_root = (os.getenv("ACCOUNT_MANAGER_ROOT") or "").strip()
|
||||||
|
if env_root and os.path.isfile(os.path.join(env_root, "scripts", "main.py")):
|
||||||
|
return os.path.join(os.path.abspath(env_root), "scripts", "main.py")
|
||||||
|
|
||||||
|
scripts_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
||||||
|
skills_root = get_sibling_skills_root(scripts_dir)
|
||||||
|
candidate = os.path.join(skills_root, "account-manager", "scripts", "main.py")
|
||||||
|
if os.path.isfile(candidate):
|
||||||
|
return candidate
|
||||||
|
|
||||||
|
# 开发环境兜底:仅模板/本机调试;生产依赖宿主注入的路径变量,勿硬编码个人目录
|
||||||
|
dev = r"D:\OpenClaw\client-commons\account-manager\scripts\main.py"
|
||||||
|
if os.path.isfile(dev):
|
||||||
|
return dev
|
||||||
|
|
||||||
|
raise AccountManagerError(
|
||||||
|
"ACCOUNT_NOT_FOUND",
|
||||||
|
"未找到 account-manager,请配置 ACCOUNT_MANAGER_ROOT 或安装 account-manager 技能。",
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def _parse_last_json(stdout: str) -> Any:
|
||||||
|
lines = [ln.strip() for ln in (stdout or "").splitlines() if ln.strip()]
|
||||||
|
for raw in reversed(lines):
|
||||||
|
if raw.startswith("ERROR:"):
|
||||||
|
continue
|
||||||
|
try:
|
||||||
|
return json.loads(raw)
|
||||||
|
except json.JSONDecodeError:
|
||||||
|
continue
|
||||||
|
raise AccountManagerError("UNKNOWN_ERROR", "stdout 中没有可解析的 JSON。")
|
||||||
|
|
||||||
|
|
||||||
|
def _run_argv(argv_suffix: List[str]) -> subprocess.CompletedProcess[str]:
|
||||||
|
main_py = _resolve_account_manager_main()
|
||||||
|
env = os.environ.copy()
|
||||||
|
return subprocess.run(
|
||||||
|
[sys.executable, main_py, *argv_suffix],
|
||||||
|
capture_output=True,
|
||||||
|
text=True,
|
||||||
|
encoding="utf-8",
|
||||||
|
errors="replace",
|
||||||
|
env=env,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def _normalize_error_code(raw: str) -> str:
|
||||||
|
code = (raw or "").strip()
|
||||||
|
if code.startswith("ERROR:"):
|
||||||
|
code = code[6:]
|
||||||
|
return code
|
||||||
|
|
||||||
|
|
||||||
|
def _validate_pick_payload(payload: dict[str, Any]) -> dict[str, Any]:
|
||||||
|
if payload.get("success") is False:
|
||||||
|
err = payload.get("error") if isinstance(payload.get("error"), dict) else {}
|
||||||
|
code = _normalize_error_code(str(err.get("code") or ""))
|
||||||
|
message = str(err.get("message") or "")
|
||||||
|
if code == "LEASE_CONFLICT" or "LEASE_CONFLICT" in code:
|
||||||
|
raise AccountManagerError("LEASE_CONFLICT", LEASE_BUSY_MESSAGE)
|
||||||
|
if code in ("NO_ACCOUNT", "ACCOUNT_NOT_FOUND") or "NO_ACCOUNT" in code:
|
||||||
|
raise AccountManagerError("NO_ACCOUNT", message or "没有可用账号。")
|
||||||
|
raise AccountManagerError(code or "PICK_WEB_FAILED", message or "pick-web 失败。")
|
||||||
|
|
||||||
|
if not isinstance(payload, dict):
|
||||||
|
raise AccountManagerError("ACCOUNT_NOT_FOUND", "pick-web 返回格式异常。")
|
||||||
|
if not payload.get("profile_dir"):
|
||||||
|
raise AccountManagerError("ACCOUNT_NOT_FOUND", "pick-web 返回的账号缺少 profile_dir。")
|
||||||
|
return payload
|
||||||
|
|
||||||
|
|
||||||
|
def _pick_web_with_lease(platform: str) -> Dict[str, Any]:
|
||||||
|
proc = _run_argv(
|
||||||
|
[
|
||||||
|
"account",
|
||||||
|
"pick-web",
|
||||||
|
"--platform",
|
||||||
|
platform,
|
||||||
|
"--lease",
|
||||||
|
"--holder",
|
||||||
|
LEASE_HOLDER,
|
||||||
|
"--ttl-sec",
|
||||||
|
LEASE_TTL_SEC,
|
||||||
|
]
|
||||||
|
)
|
||||||
|
out = proc.stdout or ""
|
||||||
|
if proc.returncode != 0 and not out.strip():
|
||||||
|
raise AccountManagerError(
|
||||||
|
"PICK_WEB_FAILED",
|
||||||
|
(proc.stderr or "").strip() or "pick-web 子进程失败",
|
||||||
|
)
|
||||||
|
payload = _parse_last_json(out)
|
||||||
|
if isinstance(payload, dict) and payload.get("success") is False:
|
||||||
|
return _validate_pick_payload(payload)
|
||||||
|
if not isinstance(payload, dict):
|
||||||
|
raise AccountManagerError("ACCOUNT_NOT_FOUND", "pick-web 返回格式异常。")
|
||||||
|
return payload
|
||||||
|
|
||||||
|
|
||||||
|
def _pick_by_id(platform: str, account_id: int) -> Dict[str, Any]:
|
||||||
|
proc = _run_argv(["account", "get", str(account_id)])
|
||||||
|
out = proc.stdout or ""
|
||||||
|
if proc.returncode != 0 or out.startswith("ERROR:"):
|
||||||
|
raise AccountManagerError("ACCOUNT_NOT_FOUND", f"指定账号 {account_id} 不存在或获取失败。")
|
||||||
|
raw = _parse_last_json(out)
|
||||||
|
if isinstance(raw, dict) and "data" in raw and isinstance(raw["data"], dict):
|
||||||
|
data = raw["data"]
|
||||||
|
elif isinstance(raw, dict):
|
||||||
|
data = raw
|
||||||
|
else:
|
||||||
|
raise AccountManagerError("ACCOUNT_NOT_FOUND", "account get 返回非对象 JSON。")
|
||||||
|
|
||||||
|
platform_key = str(data.get("platform_key") or "").lower()
|
||||||
|
if platform_key != platform.lower():
|
||||||
|
raise AccountManagerError(
|
||||||
|
"ACCOUNT_NOT_FOUND",
|
||||||
|
f"账号 {account_id} 不是 {platform} 平台账号。",
|
||||||
|
)
|
||||||
|
profile_dir = str(data.get("profile_dir") or "").strip()
|
||||||
|
if not profile_dir:
|
||||||
|
raise AccountManagerError("ACCOUNT_NOT_FOUND", f"账号 {account_id} 缺少 profile_dir。")
|
||||||
|
data["profile_dir"] = profile_dir
|
||||||
|
data["lease_token"] = ""
|
||||||
|
return data
|
||||||
|
|
||||||
|
|
||||||
|
def pick_web_account(platform: str, account_id: Optional[str] = None) -> dict:
|
||||||
|
"""获取网页账号(profile_dir + lease_token)。"""
|
||||||
|
platform_key = (platform or PLACEHOLDER_PLATFORM).strip() or PLACEHOLDER_PLATFORM
|
||||||
|
|
||||||
|
if account_id:
|
||||||
|
try:
|
||||||
|
aid = int(account_id)
|
||||||
|
except (TypeError, ValueError):
|
||||||
|
raise AccountManagerError("ACCOUNT_NOT_FOUND", f"账号 ID 无效:{account_id}")
|
||||||
|
return _pick_by_id(platform_key, aid)
|
||||||
|
|
||||||
|
try:
|
||||||
|
return _pick_web_with_lease(platform_key)
|
||||||
|
except AccountManagerError as exc:
|
||||||
|
if exc.code in ("NO_ACCOUNT", "ACCOUNT_NOT_FOUND") or "NO_ACCOUNT" in exc.code:
|
||||||
|
raise AccountManagerError("ACCOUNT_SETUP_REQUIRED", ACCOUNT_SETUP_MESSAGE) from exc
|
||||||
|
raise
|
||||||
|
|
||||||
|
|
||||||
|
def release_lease(lease_token: Optional[str]) -> None:
|
||||||
|
token = (lease_token or "").strip()
|
||||||
|
if not token:
|
||||||
|
return
|
||||||
|
try:
|
||||||
|
proc = _run_argv(["lease", "release", token])
|
||||||
|
if proc.returncode != 0:
|
||||||
|
logger.warning("lease_release_failed stdout=%s", (proc.stdout or "").strip())
|
||||||
|
except Exception:
|
||||||
|
logger.warning("lease_release_exception", exc_info=True)
|
||||||
135
examples/real_browser_rpa/scripts/service/browser_session.py
Normal file
135
examples/real_browser_rpa/scripts/service/browser_session.py
Normal file
@@ -0,0 +1,135 @@
|
|||||||
|
"""浏览器会话启动(系统 Chrome/Edge + Playwright persistent context)。"""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import logging
|
||||||
|
import os
|
||||||
|
from typing import Any, Optional, Tuple
|
||||||
|
|
||||||
|
from util.constants import DEFAULT_START_URL
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
DEFAULT_PAGE_TIMEOUT_MS = 15_000
|
||||||
|
GOTO_TIMEOUT_MS = 60_000
|
||||||
|
|
||||||
|
STEALTH_INIT_SCRIPT = """
|
||||||
|
(() => {
|
||||||
|
try { Object.defineProperty(navigator, 'webdriver', { get: () => undefined }); } catch (e) {}
|
||||||
|
})();
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
def get_start_url() -> str:
|
||||||
|
return (os.getenv("TARGET_START_URL") or DEFAULT_START_URL).strip() or DEFAULT_START_URL
|
||||||
|
|
||||||
|
|
||||||
|
def _headless_from_env() -> bool:
|
||||||
|
v = (os.getenv("OPENCLAW_BROWSER_HEADLESS") or "0").strip().lower()
|
||||||
|
return v in ("1", "true", "yes", "on")
|
||||||
|
|
||||||
|
|
||||||
|
def _find_chrome_executable() -> str | None:
|
||||||
|
try:
|
||||||
|
from jiangchang_skill_core.runtime_env import find_chrome_executable
|
||||||
|
|
||||||
|
chrome = find_chrome_executable()
|
||||||
|
if chrome and os.path.isfile(chrome):
|
||||||
|
return chrome
|
||||||
|
except ImportError:
|
||||||
|
pass
|
||||||
|
|
||||||
|
candidates = [
|
||||||
|
os.path.expandvars(r"%ProgramFiles%\Google\Chrome\Application\chrome.exe"),
|
||||||
|
os.path.expandvars(r"%ProgramFiles(x86)%\Google\Chrome\Application\chrome.exe"),
|
||||||
|
os.path.expandvars(r"%ProgramFiles%\Microsoft\Edge\Application\msedge.exe"),
|
||||||
|
os.path.expandvars(r"%ProgramFiles(x86)%\Microsoft\Edge\Application\msedge.exe"),
|
||||||
|
]
|
||||||
|
for path in candidates:
|
||||||
|
if path and os.path.isfile(path):
|
||||||
|
return path
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
async def start_browser_session(
|
||||||
|
profile_dir: str,
|
||||||
|
*,
|
||||||
|
headless: Optional[bool] = None,
|
||||||
|
) -> Tuple[Any, Any, Any]:
|
||||||
|
"""
|
||||||
|
启动持久化 Chrome profile,new_page + goto 入口页,返回 (playwright, context, page)。
|
||||||
|
|
||||||
|
真实 skill 需要安装 playwright 与 jiangchang_skill_core。
|
||||||
|
"""
|
||||||
|
from playwright.async_api import async_playwright
|
||||||
|
|
||||||
|
hl = headless if headless is not None else _headless_from_env()
|
||||||
|
start_url = get_start_url()
|
||||||
|
chrome = _find_chrome_executable()
|
||||||
|
if not chrome:
|
||||||
|
raise RuntimeError(
|
||||||
|
"ERROR:MISSING_BROWSER 未检测到 Chrome 或 Edge 浏览器,请先安装系统浏览器后重试。"
|
||||||
|
)
|
||||||
|
|
||||||
|
launch_args = ["--start-maximized", "--disable-blink-features=AutomationControlled"]
|
||||||
|
logger.info(
|
||||||
|
"browser_launch chrome=%s profile_dir=%s start_url=%s args=%s headless=%s",
|
||||||
|
chrome,
|
||||||
|
profile_dir,
|
||||||
|
start_url,
|
||||||
|
launch_args,
|
||||||
|
hl,
|
||||||
|
)
|
||||||
|
print(f"[浏览器] 系统 Chrome: {chrome}")
|
||||||
|
print(f"[浏览器] profile_dir: [{profile_dir}]")
|
||||||
|
print(f"[浏览器] startup_url={start_url}")
|
||||||
|
print(f"[浏览器] args={launch_args}")
|
||||||
|
|
||||||
|
pw = await async_playwright().start()
|
||||||
|
context = None
|
||||||
|
try:
|
||||||
|
context = await pw.chromium.launch_persistent_context(
|
||||||
|
user_data_dir=profile_dir,
|
||||||
|
headless=hl,
|
||||||
|
executable_path=chrome,
|
||||||
|
locale="zh-CN",
|
||||||
|
no_viewport=True,
|
||||||
|
args=launch_args,
|
||||||
|
ignore_default_args=["--enable-automation"],
|
||||||
|
)
|
||||||
|
await context.add_init_script(STEALTH_INIT_SCRIPT)
|
||||||
|
|
||||||
|
page = await context.new_page()
|
||||||
|
page.set_default_timeout(DEFAULT_PAGE_TIMEOUT_MS)
|
||||||
|
|
||||||
|
try:
|
||||||
|
await page.goto(start_url, wait_until="domcontentloaded", timeout=GOTO_TIMEOUT_MS)
|
||||||
|
except Exception as exc:
|
||||||
|
raise RuntimeError(
|
||||||
|
f"浏览器已启动,但未能打开目标首页 {start_url}。"
|
||||||
|
"请检查网络、Chrome profile、代理或页面访问状态。"
|
||||||
|
f"({exc})"
|
||||||
|
) from exc
|
||||||
|
|
||||||
|
return pw, context, page
|
||||||
|
except Exception:
|
||||||
|
if context is not None:
|
||||||
|
try:
|
||||||
|
await context.close()
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
await pw.stop()
|
||||||
|
raise
|
||||||
|
|
||||||
|
|
||||||
|
async def close_browser_context(pw, context) -> None:
|
||||||
|
try:
|
||||||
|
if context is not None:
|
||||||
|
await context.close()
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
try:
|
||||||
|
if pw is not None:
|
||||||
|
await pw.stop()
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
450
examples/real_browser_rpa/scripts/service/human_verification.py
Normal file
450
examples/real_browser_rpa/scripts/service/human_verification.py
Normal file
@@ -0,0 +1,450 @@
|
|||||||
|
"""人工验证等待:保存登录倒计时、滑块、短信验证码等(不自动破解)。"""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import asyncio
|
||||||
|
import logging
|
||||||
|
import random
|
||||||
|
import re
|
||||||
|
import time
|
||||||
|
from dataclasses import dataclass
|
||||||
|
from typing import Optional
|
||||||
|
|
||||||
|
from util.constants import LOG_LOGGER_NAME
|
||||||
|
|
||||||
|
logger = logging.getLogger(LOG_LOGGER_NAME)
|
||||||
|
|
||||||
|
TEXT_EXCERPT_MAX = 120
|
||||||
|
|
||||||
|
SAVE_LOGIN_STRONG_MARKERS = (
|
||||||
|
"是否保存登录信息",
|
||||||
|
"下次登录更便捷",
|
||||||
|
"保存登录信息",
|
||||||
|
)
|
||||||
|
|
||||||
|
SAVE_LOGIN_CONTAINER_SELECTORS = (
|
||||||
|
"[role=\"dialog\"]",
|
||||||
|
".semi-modal",
|
||||||
|
".semi-modal-content",
|
||||||
|
)
|
||||||
|
|
||||||
|
SLIDER_SELECTORS = [
|
||||||
|
".vc-captcha-verify.slide",
|
||||||
|
".captcha_verify_bar--title",
|
||||||
|
".captcha-slider-tips",
|
||||||
|
"#captcha_verify_image",
|
||||||
|
".captcha-slider-btn",
|
||||||
|
]
|
||||||
|
|
||||||
|
SMS_SELECTORS = [
|
||||||
|
".second-verify-panel",
|
||||||
|
".uc-ui-verify_sms-input",
|
||||||
|
".uc-ui-verify-new_header-title",
|
||||||
|
"input[placeholder='手机号']",
|
||||||
|
"input[placeholder='请输入验证码']",
|
||||||
|
]
|
||||||
|
|
||||||
|
DIALOG_TEXT_CONTAINERS = [
|
||||||
|
"[role='dialog']",
|
||||||
|
".semi-modal",
|
||||||
|
".semi-modal-content",
|
||||||
|
".vc-captcha-verify",
|
||||||
|
".second-verify-panel",
|
||||||
|
]
|
||||||
|
|
||||||
|
RISK_MARKERS = (
|
||||||
|
"操作频繁",
|
||||||
|
"访问过于频繁",
|
||||||
|
"风险验证",
|
||||||
|
"安全验证",
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class HumanVerificationState:
|
||||||
|
present: bool
|
||||||
|
kind: str
|
||||||
|
message: str
|
||||||
|
selector: str = ""
|
||||||
|
text_excerpt: str = ""
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class HumanVerificationWaitResult:
|
||||||
|
timed_out: bool
|
||||||
|
kind: str = ""
|
||||||
|
code: str = ""
|
||||||
|
message: str = ""
|
||||||
|
stage: str = ""
|
||||||
|
|
||||||
|
|
||||||
|
def _text_excerpt(text: str) -> str:
|
||||||
|
cleaned = re.sub(r"\s+", " ", (text or "").strip())
|
||||||
|
if len(cleaned) <= TEXT_EXCERPT_MAX:
|
||||||
|
return cleaned
|
||||||
|
return cleaned[:TEXT_EXCERPT_MAX] + "..."
|
||||||
|
|
||||||
|
|
||||||
|
def _contains_any(text: str, markers: tuple[str, ...]) -> bool:
|
||||||
|
return any(m in text for m in markers)
|
||||||
|
|
||||||
|
|
||||||
|
async def _selector_visible(page, selector: str) -> bool:
|
||||||
|
try:
|
||||||
|
loc = page.locator(selector).first
|
||||||
|
return await loc.is_visible(timeout=500)
|
||||||
|
except Exception:
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
async def _selector_inner_text(page, selector: str) -> str:
|
||||||
|
try:
|
||||||
|
return await page.locator(selector).first.inner_text(timeout=1000)
|
||||||
|
except Exception:
|
||||||
|
return ""
|
||||||
|
|
||||||
|
|
||||||
|
async def _visible_text_from_first_visible(page, selectors: tuple[str, ...] | list[str]) -> tuple[str, str]:
|
||||||
|
for sel in selectors:
|
||||||
|
try:
|
||||||
|
loc = page.locator(sel).first
|
||||||
|
if await loc.is_visible(timeout=500):
|
||||||
|
text = await loc.inner_text(timeout=1000)
|
||||||
|
return sel, text or ""
|
||||||
|
except Exception:
|
||||||
|
continue
|
||||||
|
return "", ""
|
||||||
|
|
||||||
|
|
||||||
|
def _save_login_popup_from_text(text: str) -> bool:
|
||||||
|
if not text:
|
||||||
|
return False
|
||||||
|
if _contains_any(text, SAVE_LOGIN_STRONG_MARKERS):
|
||||||
|
return True
|
||||||
|
if "保存" in text and "取消" in text and ("登录" in text or "登录信息" in text):
|
||||||
|
return True
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
async def detect_save_login_popup(page) -> bool:
|
||||||
|
sel, text = await _visible_text_from_first_visible(page, SAVE_LOGIN_CONTAINER_SELECTORS)
|
||||||
|
if sel and _save_login_popup_from_text(text):
|
||||||
|
return True
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
async def wait_save_login_countdown_if_present(page, timeout_sec: int = 8) -> bool:
|
||||||
|
"""若出现“保存登录信息”倒计时弹窗则等待其消失;未出现则立即返回 False。"""
|
||||||
|
if not await detect_save_login_popup(page):
|
||||||
|
return False
|
||||||
|
|
||||||
|
logger.info("save_login_popup_detected")
|
||||||
|
print("[登录] 检测到“保存登录信息”倒计时弹窗,等待其自动关闭...")
|
||||||
|
start = time.monotonic()
|
||||||
|
deadline = start + timeout_sec
|
||||||
|
while time.monotonic() < deadline:
|
||||||
|
if not await detect_save_login_popup(page):
|
||||||
|
elapsed = round(time.monotonic() - start, 2)
|
||||||
|
logger.info("save_login_popup_gone elapsed_sec=%s", elapsed)
|
||||||
|
return True
|
||||||
|
await asyncio.sleep(0.5)
|
||||||
|
|
||||||
|
elapsed = round(time.monotonic() - start, 2)
|
||||||
|
logger.warning(
|
||||||
|
"save_login_popup_still_visible elapsed_sec=%s timeout_sec=%s",
|
||||||
|
elapsed,
|
||||||
|
timeout_sec,
|
||||||
|
)
|
||||||
|
print(
|
||||||
|
f"[登录] 警告:保存登录信息弹窗超过 {timeout_sec} 秒仍未消失,"
|
||||||
|
"将继续后续流程(您可能已手工点击保存/取消)。"
|
||||||
|
)
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
async def detect_slider_verification(page) -> Optional[HumanVerificationState]:
|
||||||
|
sel_slide = ".vc-captcha-verify.slide"
|
||||||
|
if await _selector_visible(page, sel_slide):
|
||||||
|
text = await _selector_inner_text(page, sel_slide)
|
||||||
|
return HumanVerificationState(
|
||||||
|
present=True,
|
||||||
|
kind="slider",
|
||||||
|
message="滑块验证码",
|
||||||
|
selector=sel_slide,
|
||||||
|
text_excerpt=_text_excerpt(text),
|
||||||
|
)
|
||||||
|
|
||||||
|
sel_title = ".captcha_verify_bar--title"
|
||||||
|
if await _selector_visible(page, sel_title):
|
||||||
|
title_text = await _selector_inner_text(page, sel_title)
|
||||||
|
if "请完成下列验证后继续" in title_text:
|
||||||
|
return HumanVerificationState(
|
||||||
|
present=True,
|
||||||
|
kind="slider",
|
||||||
|
message="滑块验证码",
|
||||||
|
selector=sel_title,
|
||||||
|
text_excerpt=_text_excerpt(title_text),
|
||||||
|
)
|
||||||
|
|
||||||
|
sel_tips = ".captcha-slider-tips"
|
||||||
|
if await _selector_visible(page, sel_tips):
|
||||||
|
tips_text = await _selector_inner_text(page, sel_tips)
|
||||||
|
if "拖动完成上方拼图" in tips_text:
|
||||||
|
return HumanVerificationState(
|
||||||
|
present=True,
|
||||||
|
kind="slider",
|
||||||
|
message="滑块验证码",
|
||||||
|
selector=sel_tips,
|
||||||
|
text_excerpt=_text_excerpt(tips_text),
|
||||||
|
)
|
||||||
|
|
||||||
|
if await _selector_visible(page, "#captcha_verify_image"):
|
||||||
|
for btn_sel in (".captcha-slider-btn", ".captcha_verify_slide--button", ".captcha-slider"):
|
||||||
|
if await _selector_visible(page, btn_sel):
|
||||||
|
return HumanVerificationState(
|
||||||
|
present=True,
|
||||||
|
kind="slider",
|
||||||
|
message="滑块验证码",
|
||||||
|
selector=f"#captcha_verify_image+{btn_sel}",
|
||||||
|
text_excerpt="",
|
||||||
|
)
|
||||||
|
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
async def detect_sms_verification(page) -> Optional[HumanVerificationState]:
|
||||||
|
sel_panel = ".second-verify-panel"
|
||||||
|
if await _selector_visible(page, sel_panel):
|
||||||
|
text = await _selector_inner_text(page, sel_panel)
|
||||||
|
return HumanVerificationState(
|
||||||
|
present=True,
|
||||||
|
kind="sms",
|
||||||
|
message="短信验证码",
|
||||||
|
selector=sel_panel,
|
||||||
|
text_excerpt=_text_excerpt(text),
|
||||||
|
)
|
||||||
|
|
||||||
|
sel_sms = ".uc-ui-verify_sms-input"
|
||||||
|
if await _selector_visible(page, sel_sms):
|
||||||
|
text = await _selector_inner_text(page, sel_sms)
|
||||||
|
return HumanVerificationState(
|
||||||
|
present=True,
|
||||||
|
kind="sms",
|
||||||
|
message="短信验证码",
|
||||||
|
selector=sel_sms,
|
||||||
|
text_excerpt=_text_excerpt(text),
|
||||||
|
)
|
||||||
|
|
||||||
|
sel_hdr = ".uc-ui-verify-new_header-title"
|
||||||
|
if await _selector_visible(page, sel_hdr):
|
||||||
|
hdr_text = await _selector_inner_text(page, sel_hdr)
|
||||||
|
if "短信验证" in hdr_text:
|
||||||
|
return HumanVerificationState(
|
||||||
|
present=True,
|
||||||
|
kind="sms",
|
||||||
|
message="短信验证码",
|
||||||
|
selector=sel_hdr,
|
||||||
|
text_excerpt=_text_excerpt(hdr_text),
|
||||||
|
)
|
||||||
|
|
||||||
|
phone_visible = await _selector_visible(
|
||||||
|
page,
|
||||||
|
".second-verify-panel input[placeholder='手机号'], .uc-ui-verify_sms-input input[placeholder='手机号']",
|
||||||
|
)
|
||||||
|
code_visible = await _selector_visible(
|
||||||
|
page,
|
||||||
|
".second-verify-panel input[placeholder='请输入验证码'], .uc-ui-verify_sms-input input[placeholder='请输入验证码']",
|
||||||
|
)
|
||||||
|
if phone_visible and code_visible:
|
||||||
|
return HumanVerificationState(
|
||||||
|
present=True,
|
||||||
|
kind="sms",
|
||||||
|
message="短信验证码",
|
||||||
|
selector="sms-phone-and-code-inputs",
|
||||||
|
text_excerpt="手机号+请输入验证码",
|
||||||
|
)
|
||||||
|
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
async def detect_human_verification_from_containers(page) -> Optional[HumanVerificationState]:
|
||||||
|
sel, text = await _visible_text_from_first_visible(page, DIALOG_TEXT_CONTAINERS)
|
||||||
|
if not sel or not text:
|
||||||
|
return None
|
||||||
|
|
||||||
|
if _contains_any(text, RISK_MARKERS):
|
||||||
|
return HumanVerificationState(
|
||||||
|
present=True,
|
||||||
|
kind="risk",
|
||||||
|
message="安全/频控验证",
|
||||||
|
selector=sel,
|
||||||
|
text_excerpt=_text_excerpt(text),
|
||||||
|
)
|
||||||
|
|
||||||
|
if sel in (".vc-captcha-verify", ".vc-captcha-verify-visibility"):
|
||||||
|
if "请完成下列验证后继续" in text or "拖动完成上方拼图" in text:
|
||||||
|
return HumanVerificationState(
|
||||||
|
present=True,
|
||||||
|
kind="slider",
|
||||||
|
message="滑块验证码",
|
||||||
|
selector=sel,
|
||||||
|
text_excerpt=_text_excerpt(text),
|
||||||
|
)
|
||||||
|
|
||||||
|
if sel in (".second-verify-panel", ".uc-ui-verify_sms-input"):
|
||||||
|
if "短信验证" in text or "获取验证码" in text:
|
||||||
|
return HumanVerificationState(
|
||||||
|
present=True,
|
||||||
|
kind="sms",
|
||||||
|
message="短信验证码",
|
||||||
|
selector=sel,
|
||||||
|
text_excerpt=_text_excerpt(text),
|
||||||
|
)
|
||||||
|
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
async def detect_human_verification(page) -> HumanVerificationState:
|
||||||
|
"""主检测入口:仅在验证码/弹窗容器级检测,不扫整页 body。"""
|
||||||
|
state = await detect_slider_verification(page)
|
||||||
|
if state is not None:
|
||||||
|
return state
|
||||||
|
state = await detect_sms_verification(page)
|
||||||
|
if state is not None:
|
||||||
|
return state
|
||||||
|
state = await detect_human_verification_from_containers(page)
|
||||||
|
if state is not None:
|
||||||
|
return state
|
||||||
|
return HumanVerificationState(present=False, kind="", message="", selector="", text_excerpt="")
|
||||||
|
|
||||||
|
|
||||||
|
def detect_human_verification_from_text(text: str) -> HumanVerificationState:
|
||||||
|
"""
|
||||||
|
根据 HTML/容器文本判断验证弹窗(单元测试辅助,不能作为真实页面主检测入口)。
|
||||||
|
只检查验证容器 DOM 标记,不根据正文关键词误判。
|
||||||
|
"""
|
||||||
|
html_lower = (text or "").lower()
|
||||||
|
has_slider_dom = (
|
||||||
|
"vc-captcha-verify" in html_lower
|
||||||
|
and "slide" in html_lower
|
||||||
|
and (
|
||||||
|
"captcha-slider-tips" in html_lower
|
||||||
|
or "captcha_verify_bar--title" in html_lower
|
||||||
|
or "captcha_verify_image" in html_lower
|
||||||
|
)
|
||||||
|
)
|
||||||
|
if has_slider_dom:
|
||||||
|
selector = ".vc-captcha-verify.slide"
|
||||||
|
if "captcha-slider-tips" in html_lower:
|
||||||
|
selector = ".captcha-slider-tips"
|
||||||
|
return HumanVerificationState(
|
||||||
|
present=True,
|
||||||
|
kind="slider",
|
||||||
|
message="滑块验证码",
|
||||||
|
selector=selector,
|
||||||
|
text_excerpt="",
|
||||||
|
)
|
||||||
|
|
||||||
|
has_sms_dom = "second-verify-panel" in html_lower or "uc-ui-verify_sms-input" in html_lower
|
||||||
|
if has_sms_dom:
|
||||||
|
selector = ".second-verify-panel" if "second-verify-panel" in html_lower else ".uc-ui-verify_sms-input"
|
||||||
|
return HumanVerificationState(
|
||||||
|
present=True,
|
||||||
|
kind="sms",
|
||||||
|
message="短信验证码",
|
||||||
|
selector=selector,
|
||||||
|
text_excerpt="",
|
||||||
|
)
|
||||||
|
|
||||||
|
return HumanVerificationState(present=False, kind="", message="", selector="", text_excerpt="")
|
||||||
|
|
||||||
|
|
||||||
|
def _wait_log_message(state: HumanVerificationState, wait_sec: int) -> str:
|
||||||
|
if state.kind == "slider":
|
||||||
|
return f"[人工验证] 检测到滑块验证码,请在浏览器中手工完成,最多等待 {wait_sec} 秒..."
|
||||||
|
if state.kind == "sms":
|
||||||
|
return f"[人工验证] 检测到短信验证码,请在浏览器中手工完成,最多等待 {wait_sec} 秒..."
|
||||||
|
return f"[人工验证] 检测到安全验证,请在浏览器中手工处理,最多等待 {wait_sec} 秒..."
|
||||||
|
|
||||||
|
|
||||||
|
def _timeout_error_code(kind: str) -> str:
|
||||||
|
if kind == "slider":
|
||||||
|
return "SLIDER_VERIFICATION_TIMEOUT"
|
||||||
|
if kind == "sms":
|
||||||
|
return "SMS_VERIFICATION_TIMEOUT"
|
||||||
|
return "HUMAN_VERIFICATION_TIMEOUT"
|
||||||
|
|
||||||
|
|
||||||
|
def _timeout_error_message(kind: str, wait_sec: int) -> str:
|
||||||
|
if kind == "slider":
|
||||||
|
return (
|
||||||
|
f"滑块验证码未在 {wait_sec} 秒内完成,任务已停止。"
|
||||||
|
"请重新运行任务并及时在浏览器中完成验证。"
|
||||||
|
)
|
||||||
|
if kind == "sms":
|
||||||
|
return (
|
||||||
|
f"短信验证码未在 {wait_sec} 秒内完成,任务已停止。"
|
||||||
|
"请重新运行任务并及时完成短信验证。"
|
||||||
|
)
|
||||||
|
return (
|
||||||
|
f"安全验证未在 {wait_sec} 秒内完成,任务已停止。"
|
||||||
|
"请重新运行任务并及时在浏览器中完成验证。"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
async def wait_human_verification_if_present(
|
||||||
|
page,
|
||||||
|
*,
|
||||||
|
wait_sec: int = 180,
|
||||||
|
stage: str = "unknown",
|
||||||
|
) -> Optional[HumanVerificationWaitResult]:
|
||||||
|
"""
|
||||||
|
若存在人工验证则等待用户完成;无验证或已完成返回 None;超时返回 HumanVerificationWaitResult。
|
||||||
|
不自动拖动滑块、不自动填写短信验证码。
|
||||||
|
"""
|
||||||
|
state = await detect_human_verification(page)
|
||||||
|
if not state.present:
|
||||||
|
return None
|
||||||
|
|
||||||
|
logger.warning(
|
||||||
|
"human_verification_detected stage=%s kind=%s selector=%s text=%s",
|
||||||
|
stage,
|
||||||
|
state.kind,
|
||||||
|
state.selector,
|
||||||
|
state.text_excerpt,
|
||||||
|
)
|
||||||
|
print(_wait_log_message(state, wait_sec))
|
||||||
|
|
||||||
|
last_state = state
|
||||||
|
start = time.monotonic()
|
||||||
|
deadline = start + wait_sec
|
||||||
|
|
||||||
|
while time.monotonic() < deadline:
|
||||||
|
state = await detect_human_verification(page)
|
||||||
|
if not state.present:
|
||||||
|
elapsed = round(time.monotonic() - start, 2)
|
||||||
|
logger.info(
|
||||||
|
"human_verification_passed stage=%s kind=%s elapsed_sec=%s",
|
||||||
|
stage,
|
||||||
|
last_state.kind,
|
||||||
|
elapsed,
|
||||||
|
)
|
||||||
|
await asyncio.sleep(random.uniform(1.0, 2.0))
|
||||||
|
return None
|
||||||
|
last_state = state
|
||||||
|
await asyncio.sleep(2.0)
|
||||||
|
|
||||||
|
logger.warning(
|
||||||
|
"human_verification_timeout stage=%s kind=%s wait_sec=%s selector=%s",
|
||||||
|
stage,
|
||||||
|
last_state.kind,
|
||||||
|
wait_sec,
|
||||||
|
last_state.selector,
|
||||||
|
)
|
||||||
|
return HumanVerificationWaitResult(
|
||||||
|
timed_out=True,
|
||||||
|
kind=last_state.kind,
|
||||||
|
code=_timeout_error_code(last_state.kind),
|
||||||
|
message=_timeout_error_message(last_state.kind, wait_sec),
|
||||||
|
stage=stage,
|
||||||
|
)
|
||||||
641
examples/real_browser_rpa/scripts/service/task_rpa.py
Normal file
641
examples/real_browser_rpa/scripts/service/task_rpa.py
Normal file
@@ -0,0 +1,641 @@
|
|||||||
|
"""真实网站关键词搜索 RPA 示例(async Playwright)。"""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import asyncio
|
||||||
|
import logging
|
||||||
|
import os
|
||||||
|
import random
|
||||||
|
import re
|
||||||
|
import time
|
||||||
|
from dataclasses import dataclass, field
|
||||||
|
from typing import Any, Callable, Dict, List, Optional, Tuple
|
||||||
|
|
||||||
|
from service.browser_session import close_browser_context, get_start_url, start_browser_session
|
||||||
|
from service.human_verification import (
|
||||||
|
HumanVerificationWaitResult,
|
||||||
|
wait_human_verification_if_present,
|
||||||
|
wait_save_login_countdown_if_present,
|
||||||
|
)
|
||||||
|
from util.constants import (
|
||||||
|
DEFAULT_MAX_ITEMS,
|
||||||
|
DEFAULT_MAX_SCROLLS,
|
||||||
|
DEFAULT_NO_NEW_ROUNDS_LIMIT,
|
||||||
|
HUMAN_WAIT_TIMEOUT,
|
||||||
|
LOG_LOGGER_NAME,
|
||||||
|
RESULT_CONTAINER_SELECTOR,
|
||||||
|
RESULT_END_TEXT,
|
||||||
|
RESULT_ITEM_SELECTOR,
|
||||||
|
SCROLL_CONTAINER_SELECTORS,
|
||||||
|
SEARCH_BUTTON_SELECTOR,
|
||||||
|
SEARCH_INPUT_SELECTORS,
|
||||||
|
)
|
||||||
|
from util.logging import mask_text, safe_log_value
|
||||||
|
|
||||||
|
logger = logging.getLogger(LOG_LOGGER_NAME)
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class ScrapeError:
|
||||||
|
code: str
|
||||||
|
message: str
|
||||||
|
browser_started: bool = False
|
||||||
|
stage: Optional[str] = None
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class ScrapeRunResult:
|
||||||
|
success: bool
|
||||||
|
items: List[Dict[str, Any]] = field(default_factory=list)
|
||||||
|
stop_reason: Optional[str] = None
|
||||||
|
error: Optional[ScrapeError] = None
|
||||||
|
|
||||||
|
|
||||||
|
StepCallback = Callable[[str], None]
|
||||||
|
|
||||||
|
# 示例登录态 selector — 复制到新 skill 后必须用目标站点 F12/DOM 实测替换,禁止凭经验猜测或跨平台照抄
|
||||||
|
LOGGED_IN_SELECTORS = (
|
||||||
|
"a[href*='/user/self']",
|
||||||
|
"[data-user-avatar]",
|
||||||
|
".user-avatar",
|
||||||
|
)
|
||||||
|
LOGGED_OUT_SELECTOR = "button:has-text('登录')"
|
||||||
|
|
||||||
|
|
||||||
|
def format_stop_reason_for_user(stop_reason: str | None, collected_count: int) -> str:
|
||||||
|
"""将内部 stop_reason 枚举翻译为用户可理解的中文说明。"""
|
||||||
|
if stop_reason == "max_items":
|
||||||
|
return (
|
||||||
|
f"已达到本次默认采集规模({collected_count} 条)。"
|
||||||
|
"如需更多,可以继续说「继续采集更多」或「尽量多采集」。"
|
||||||
|
)
|
||||||
|
if stop_reason == "end_marker":
|
||||||
|
return "页面提示没有更多结果,本次采集已完成。"
|
||||||
|
if stop_reason == "no_new_rounds":
|
||||||
|
return "连续多次加载没有发现新结果,本次采集已完成。"
|
||||||
|
if stop_reason == "max_scrolls":
|
||||||
|
return (
|
||||||
|
"已达到本次页面加载保护范围,本次采集已完成。"
|
||||||
|
"如需更深度采集,可以说明「尽量多采集」。"
|
||||||
|
)
|
||||||
|
if stop_reason == "human_verification_timeout":
|
||||||
|
return "人工验证未在规定时间内完成,任务已停止。"
|
||||||
|
return "本次采集已完成。"
|
||||||
|
|
||||||
|
|
||||||
|
def _mask_account_ref(account: Dict[str, Any]) -> str:
|
||||||
|
for key in ("login_id", "phone", "mobile"):
|
||||||
|
raw = str(account.get(key) or "").strip()
|
||||||
|
if raw:
|
||||||
|
return mask_text(raw)
|
||||||
|
aid = account.get("id") or account.get("account_id")
|
||||||
|
if aid is not None:
|
||||||
|
return f"id:{aid}"
|
||||||
|
return "unknown"
|
||||||
|
|
||||||
|
|
||||||
|
def _step_cb(cb: Optional[StepCallback], text: str) -> None:
|
||||||
|
if cb:
|
||||||
|
cb(text)
|
||||||
|
|
||||||
|
|
||||||
|
async def _random_delay() -> None:
|
||||||
|
lo = int(os.getenv("RPA_STEP_DELAY_MIN_MS") or "900")
|
||||||
|
hi = int(os.getenv("RPA_STEP_DELAY_MAX_MS") or "2600")
|
||||||
|
await asyncio.sleep(random.uniform(lo / 1000.0, hi / 1000.0))
|
||||||
|
|
||||||
|
|
||||||
|
async def _scroll_wait() -> None:
|
||||||
|
await asyncio.sleep(random.uniform(1.2, 3.5))
|
||||||
|
|
||||||
|
|
||||||
|
def _headless() -> bool:
|
||||||
|
v = (os.getenv("OPENCLAW_BROWSER_HEADLESS") or "0").strip().lower()
|
||||||
|
return v in ("1", "true", "yes", "on")
|
||||||
|
|
||||||
|
|
||||||
|
def _no_new_limit() -> int:
|
||||||
|
try:
|
||||||
|
return int(os.getenv("RPA_NO_NEW_ROUNDS_LIMIT") or str(DEFAULT_NO_NEW_ROUNDS_LIMIT))
|
||||||
|
except ValueError:
|
||||||
|
return DEFAULT_NO_NEW_ROUNDS_LIMIT
|
||||||
|
|
||||||
|
|
||||||
|
async def _visible(locator) -> bool:
|
||||||
|
try:
|
||||||
|
return await locator.count() > 0 and await locator.is_visible(timeout=500)
|
||||||
|
except Exception:
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
async def _is_logged_out(page) -> bool:
|
||||||
|
try:
|
||||||
|
btn = page.locator(LOGGED_OUT_SELECTOR).first
|
||||||
|
return await _visible(btn)
|
||||||
|
except Exception:
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
async def _is_logged_in(page) -> bool:
|
||||||
|
if await _is_logged_out(page):
|
||||||
|
return False
|
||||||
|
for sel in LOGGED_IN_SELECTORS:
|
||||||
|
try:
|
||||||
|
loc = page.locator(sel).first
|
||||||
|
if await _visible(loc):
|
||||||
|
return True
|
||||||
|
except Exception:
|
||||||
|
continue
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
async def _open_login_panel_if_needed(page) -> None:
|
||||||
|
if not await _is_logged_out(page):
|
||||||
|
return
|
||||||
|
try:
|
||||||
|
btn = page.locator(LOGGED_OUT_SELECTOR).first
|
||||||
|
if await _visible(btn):
|
||||||
|
await btn.click()
|
||||||
|
await asyncio.sleep(random.uniform(1.0, 3.0))
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
async def _ensure_logged_in(page, *, wait_sec: int) -> None:
|
||||||
|
if await _is_logged_in(page):
|
||||||
|
return
|
||||||
|
|
||||||
|
await _open_login_panel_if_needed(page)
|
||||||
|
if await _is_logged_in(page):
|
||||||
|
return
|
||||||
|
|
||||||
|
print(f"[登录] 请在浏览器中完成登录,最多等待 {wait_sec} 秒...")
|
||||||
|
deadline = time.monotonic() + wait_sec
|
||||||
|
while time.monotonic() < deadline:
|
||||||
|
if await _is_logged_in(page):
|
||||||
|
print("[登录] 检测到登录成功")
|
||||||
|
return
|
||||||
|
await asyncio.sleep(2.0)
|
||||||
|
|
||||||
|
raise RuntimeError(
|
||||||
|
f"ERROR:LOGIN_TIMEOUT 浏览器已打开,但未完成登录。"
|
||||||
|
f"请重新运行任务并在浏览器打开后及时完成登录。"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
async def _find_search_input(page) -> Tuple[Optional[Any], str]:
|
||||||
|
for sel in SEARCH_INPUT_SELECTORS:
|
||||||
|
try:
|
||||||
|
loc = page.locator(sel).first
|
||||||
|
if await _visible(loc):
|
||||||
|
return loc, sel
|
||||||
|
except Exception:
|
||||||
|
continue
|
||||||
|
|
||||||
|
candidates: List[Tuple[int, Any, str]] = []
|
||||||
|
try:
|
||||||
|
inputs = await page.locator("input").all()
|
||||||
|
except Exception:
|
||||||
|
inputs = []
|
||||||
|
for inp in inputs:
|
||||||
|
try:
|
||||||
|
if not await inp.is_visible():
|
||||||
|
continue
|
||||||
|
box = await inp.bounding_box()
|
||||||
|
if not box or box.get("width", 0) < 120:
|
||||||
|
continue
|
||||||
|
placeholder = await inp.get_attribute("placeholder") or ""
|
||||||
|
score = box.get("width", 0)
|
||||||
|
if "搜索" in placeholder:
|
||||||
|
score += 500
|
||||||
|
if box.get("y", 9999) < 200:
|
||||||
|
score += 200
|
||||||
|
candidates.append((score, inp, f"input placeholder={placeholder!r}"))
|
||||||
|
except Exception:
|
||||||
|
continue
|
||||||
|
if candidates:
|
||||||
|
candidates.sort(key=lambda x: x[0], reverse=True)
|
||||||
|
_, inp, sel = candidates[0]
|
||||||
|
return inp, sel
|
||||||
|
|
||||||
|
try:
|
||||||
|
ph = page.get_by_placeholder(re.compile("搜索"))
|
||||||
|
if await ph.count() > 0 and await ph.first.is_visible():
|
||||||
|
return ph.first, "placeholder=搜索"
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
return None, ""
|
||||||
|
|
||||||
|
|
||||||
|
async def _find_search_button(page):
|
||||||
|
try:
|
||||||
|
btn = page.get_by_role("button", name="搜索")
|
||||||
|
if await btn.count() > 0 and await btn.first.is_visible():
|
||||||
|
return btn.first
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
try:
|
||||||
|
btn = page.locator(SEARCH_BUTTON_SELECTOR).first
|
||||||
|
if await btn.count() > 0 and await btn.is_visible():
|
||||||
|
return btn
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
async def _wait_search_results(page, timeout_sec: float = 45.0) -> bool:
|
||||||
|
deadline = time.monotonic() + timeout_sec
|
||||||
|
while time.monotonic() < deadline:
|
||||||
|
try:
|
||||||
|
url = page.url or ""
|
||||||
|
if "/search" in url or "search" in url.lower():
|
||||||
|
if await page.locator(RESULT_CONTAINER_SELECTOR).count() > 0:
|
||||||
|
return True
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
await asyncio.sleep(0.8)
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
async def _has_end_marker(page) -> bool:
|
||||||
|
try:
|
||||||
|
container = page.locator(RESULT_CONTAINER_SELECTOR).first
|
||||||
|
if await container.count() > 0:
|
||||||
|
text = await container.inner_text(timeout=2000)
|
||||||
|
if RESULT_END_TEXT in text or "暂时没有更多了" in text:
|
||||||
|
return True
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
try:
|
||||||
|
if await page.locator(f"text={RESULT_END_TEXT}").count() > 0:
|
||||||
|
return True
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
async def _mouse_wheel_scroll(page) -> Tuple[str, int]:
|
||||||
|
wheel_delta = random.randint(650, 1200)
|
||||||
|
for sel in SCROLL_CONTAINER_SELECTORS:
|
||||||
|
loc = page.locator(sel)
|
||||||
|
try:
|
||||||
|
if await loc.count() > 0 and await loc.first.is_visible():
|
||||||
|
await loc.first.hover()
|
||||||
|
await _random_delay()
|
||||||
|
await page.mouse.wheel(0, wheel_delta)
|
||||||
|
return sel, wheel_delta
|
||||||
|
except Exception:
|
||||||
|
continue
|
||||||
|
await page.mouse.wheel(0, wheel_delta)
|
||||||
|
return "page", wheel_delta
|
||||||
|
|
||||||
|
|
||||||
|
async def _parse_item_locator(item) -> Dict[str, Any]:
|
||||||
|
item_id = await item.get_attribute("data-item-id") or await item.get_attribute("id") or ""
|
||||||
|
title = ""
|
||||||
|
try:
|
||||||
|
title = (await item.inner_text(timeout=1000)).strip()[:200]
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
return {
|
||||||
|
"item_id": item_id or title[:32] or str(id(item)),
|
||||||
|
"title": title,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
async def _collect_visible_items(page, seen_ids: set[str]) -> List[Dict[str, Any]]:
|
||||||
|
new_items: List[Dict[str, Any]] = []
|
||||||
|
try:
|
||||||
|
items = await page.locator(RESULT_ITEM_SELECTOR).all()
|
||||||
|
except Exception:
|
||||||
|
items = []
|
||||||
|
for item in items:
|
||||||
|
try:
|
||||||
|
parsed = await _parse_item_locator(item)
|
||||||
|
item_id = parsed.get("item_id")
|
||||||
|
if not item_id or item_id in seen_ids:
|
||||||
|
continue
|
||||||
|
seen_ids.add(str(item_id))
|
||||||
|
new_items.append(parsed)
|
||||||
|
except Exception:
|
||||||
|
continue
|
||||||
|
return new_items
|
||||||
|
|
||||||
|
|
||||||
|
async def _count_visible_items(page) -> int:
|
||||||
|
try:
|
||||||
|
return await page.locator(RESULT_ITEM_SELECTOR).count()
|
||||||
|
except Exception:
|
||||||
|
return 0
|
||||||
|
|
||||||
|
|
||||||
|
def _fail_result(error: ScrapeError, *, collected: int = 0) -> ScrapeRunResult:
|
||||||
|
logger.warning(
|
||||||
|
"rpa_failed code=%s stage=%s message=%s collected=%s",
|
||||||
|
error.code,
|
||||||
|
error.stage,
|
||||||
|
error.message,
|
||||||
|
collected,
|
||||||
|
)
|
||||||
|
return ScrapeRunResult(success=False, error=error)
|
||||||
|
|
||||||
|
|
||||||
|
def _fail_on_verification_timeout(
|
||||||
|
verification_timeout: HumanVerificationWaitResult,
|
||||||
|
*,
|
||||||
|
collected: int = 0,
|
||||||
|
) -> ScrapeRunResult:
|
||||||
|
return _fail_result(
|
||||||
|
ScrapeError(
|
||||||
|
code=verification_timeout.code,
|
||||||
|
message=verification_timeout.message,
|
||||||
|
browser_started=True,
|
||||||
|
stage=verification_timeout.stage or "unknown",
|
||||||
|
),
|
||||||
|
collected=collected,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
async def run_keyword_search_async(
|
||||||
|
account: Dict[str, Any],
|
||||||
|
keyword: str,
|
||||||
|
*,
|
||||||
|
max_items: int = DEFAULT_MAX_ITEMS,
|
||||||
|
max_scrolls: int = DEFAULT_MAX_SCROLLS,
|
||||||
|
data_dir: str = "",
|
||||||
|
batch_id: str = "",
|
||||||
|
step_callback: Optional[StepCallback] = None,
|
||||||
|
) -> ScrapeRunResult:
|
||||||
|
profile_dir = account.get("profile_dir") or ""
|
||||||
|
account_ref = _mask_account_ref(account)
|
||||||
|
wait_sec = int(os.getenv("HUMAN_WAIT_TIMEOUT") or str(HUMAN_WAIT_TIMEOUT))
|
||||||
|
no_new_limit = _no_new_limit()
|
||||||
|
|
||||||
|
logger.info(
|
||||||
|
"rpa_start keyword=%s max_items=%s max_scrolls=%s wait_sec=%s batch_id=%s account_ref=%s",
|
||||||
|
safe_log_value(keyword),
|
||||||
|
max_items,
|
||||||
|
max_scrolls,
|
||||||
|
wait_sec,
|
||||||
|
batch_id,
|
||||||
|
account_ref,
|
||||||
|
)
|
||||||
|
|
||||||
|
if not profile_dir:
|
||||||
|
err = ScrapeError(
|
||||||
|
"ACCOUNT_NOT_FOUND",
|
||||||
|
"账号缺少 profile_dir。",
|
||||||
|
browser_started=False,
|
||||||
|
stage="account_prepare",
|
||||||
|
)
|
||||||
|
return _fail_result(err)
|
||||||
|
|
||||||
|
pw = None
|
||||||
|
context = None
|
||||||
|
page = None
|
||||||
|
browser_started = False
|
||||||
|
collected_count = 0
|
||||||
|
|
||||||
|
try:
|
||||||
|
_step_cb(step_callback, "启动关键词搜索采集任务")
|
||||||
|
try:
|
||||||
|
pw, context, page = await start_browser_session(profile_dir, headless=_headless())
|
||||||
|
except RuntimeError as exc:
|
||||||
|
msg = str(exc)
|
||||||
|
err = ScrapeError(
|
||||||
|
"MISSING_BROWSER" if "MISSING_BROWSER" in msg else "UNKNOWN_ERROR",
|
||||||
|
msg.replace("ERROR:MISSING_BROWSER ", "").replace("ERROR:", ""),
|
||||||
|
browser_started="未能打开目标首页" in msg,
|
||||||
|
stage="browser_start" if "未能打开目标首页" in msg else "account_prepare",
|
||||||
|
)
|
||||||
|
return _fail_result(err)
|
||||||
|
browser_started = True
|
||||||
|
|
||||||
|
logger.info(
|
||||||
|
"browser_ready start_url=%s current_url=%s profile_dir_exists=%s",
|
||||||
|
get_start_url(),
|
||||||
|
page.url,
|
||||||
|
os.path.isdir(profile_dir),
|
||||||
|
)
|
||||||
|
|
||||||
|
_step_cb(step_callback, "打开目标站点首页")
|
||||||
|
await _random_delay()
|
||||||
|
|
||||||
|
verification_timeout = await wait_human_verification_if_present(
|
||||||
|
page, wait_sec=wait_sec, stage="browser_start"
|
||||||
|
)
|
||||||
|
if verification_timeout:
|
||||||
|
return _fail_on_verification_timeout(verification_timeout)
|
||||||
|
|
||||||
|
_step_cb(step_callback, "检查账号登录状态")
|
||||||
|
logger.info("login_check_start current_url=%s", page.url)
|
||||||
|
try:
|
||||||
|
await _ensure_logged_in(page, wait_sec=wait_sec)
|
||||||
|
except RuntimeError as exc:
|
||||||
|
msg = str(exc)
|
||||||
|
code = "LOGIN_TIMEOUT" if "LOGIN_TIMEOUT" in msg else "REQUIRE_LOGIN"
|
||||||
|
err = ScrapeError(
|
||||||
|
code,
|
||||||
|
msg.replace("ERROR:LOGIN_TIMEOUT ", "").replace("ERROR:", ""),
|
||||||
|
browser_started=True,
|
||||||
|
stage="login_check",
|
||||||
|
)
|
||||||
|
return _fail_result(err)
|
||||||
|
logger.info("login_check_done current_url=%s", page.url)
|
||||||
|
|
||||||
|
await wait_save_login_countdown_if_present(page)
|
||||||
|
|
||||||
|
verification_timeout = await wait_human_verification_if_present(
|
||||||
|
page, wait_sec=wait_sec, stage="login_check"
|
||||||
|
)
|
||||||
|
if verification_timeout:
|
||||||
|
return _fail_on_verification_timeout(verification_timeout)
|
||||||
|
|
||||||
|
_step_cb(step_callback, "定位搜索框")
|
||||||
|
search_input, input_selector = await _find_search_input(page)
|
||||||
|
if search_input is None:
|
||||||
|
err = ScrapeError(
|
||||||
|
"SEARCH_INPUT_NOT_FOUND",
|
||||||
|
"未找到搜索输入框。",
|
||||||
|
browser_started=True,
|
||||||
|
stage="search",
|
||||||
|
)
|
||||||
|
return _fail_result(err)
|
||||||
|
logger.info("search_input_found selector=%s", input_selector)
|
||||||
|
|
||||||
|
_step_cb(step_callback, f"输入关键词:{keyword}")
|
||||||
|
await search_input.click()
|
||||||
|
await _random_delay()
|
||||||
|
await page.keyboard.press("Control+A")
|
||||||
|
await page.keyboard.type(keyword, delay=random.randint(90, 240))
|
||||||
|
await _random_delay()
|
||||||
|
|
||||||
|
_step_cb(step_callback, "点击搜索")
|
||||||
|
search_btn = await _find_search_button(page)
|
||||||
|
search_method = "button"
|
||||||
|
if search_btn is not None:
|
||||||
|
await search_btn.click()
|
||||||
|
else:
|
||||||
|
search_method = "enter"
|
||||||
|
await page.keyboard.press("Enter")
|
||||||
|
logger.info(
|
||||||
|
"search_submitted keyword=%s method=%s current_url=%s",
|
||||||
|
safe_log_value(keyword),
|
||||||
|
search_method,
|
||||||
|
page.url,
|
||||||
|
)
|
||||||
|
await _random_delay()
|
||||||
|
|
||||||
|
verification_timeout = await wait_human_verification_if_present(
|
||||||
|
page, wait_sec=wait_sec, stage="search_verification"
|
||||||
|
)
|
||||||
|
if verification_timeout:
|
||||||
|
return _fail_on_verification_timeout(verification_timeout)
|
||||||
|
|
||||||
|
_step_cb(step_callback, "等待搜索结果")
|
||||||
|
if not await _wait_search_results(page):
|
||||||
|
verification_timeout = await wait_human_verification_if_present(
|
||||||
|
page, wait_sec=wait_sec, stage="search_results"
|
||||||
|
)
|
||||||
|
if verification_timeout:
|
||||||
|
return _fail_on_verification_timeout(verification_timeout)
|
||||||
|
|
||||||
|
if not await _wait_search_results(page, timeout_sec=10.0):
|
||||||
|
err = ScrapeError(
|
||||||
|
"RESULT_CONTAINER_NOT_FOUND",
|
||||||
|
"搜索结果未出现,且未检测到需要人工处理的验证码,请检查关键词或页面状态。",
|
||||||
|
browser_started=True,
|
||||||
|
stage="search",
|
||||||
|
)
|
||||||
|
return _fail_result(err)
|
||||||
|
|
||||||
|
container_count = await page.locator(RESULT_CONTAINER_SELECTOR).count()
|
||||||
|
logger.info(
|
||||||
|
"search_results_ready current_url=%s container_count=%s",
|
||||||
|
page.url,
|
||||||
|
container_count,
|
||||||
|
)
|
||||||
|
|
||||||
|
try:
|
||||||
|
await page.locator(RESULT_CONTAINER_SELECTOR).first.wait_for(state="visible", timeout=15_000)
|
||||||
|
except Exception:
|
||||||
|
err = ScrapeError(
|
||||||
|
"RESULT_CONTAINER_NOT_FOUND",
|
||||||
|
"搜索结果容器未出现。",
|
||||||
|
browser_started=True,
|
||||||
|
stage="search",
|
||||||
|
)
|
||||||
|
return _fail_result(err)
|
||||||
|
|
||||||
|
seen_ids: set[str] = set()
|
||||||
|
collected: List[Dict[str, Any]] = []
|
||||||
|
no_new_rounds = 0
|
||||||
|
stop_reason = "max_scrolls"
|
||||||
|
rounds_executed = 0
|
||||||
|
|
||||||
|
for scroll_round in range(max_scrolls + 1):
|
||||||
|
rounds_executed = scroll_round
|
||||||
|
_step_cb(step_callback, "读取当前结果列表")
|
||||||
|
visible_count = await _count_visible_items(page)
|
||||||
|
batch = await _collect_visible_items(page, seen_ids)
|
||||||
|
new_count = len(batch)
|
||||||
|
if batch:
|
||||||
|
collected.extend(batch)
|
||||||
|
no_new_rounds = 0
|
||||||
|
else:
|
||||||
|
no_new_rounds += 1
|
||||||
|
|
||||||
|
collected_count = len(collected)
|
||||||
|
end_marker = await _has_end_marker(page)
|
||||||
|
logger.info(
|
||||||
|
"collect_round round=%s max_scrolls=%s visible_count=%s new_count=%s "
|
||||||
|
"collected=%s no_new_rounds=%s no_new_limit=%s end_marker=%s current_url=%s",
|
||||||
|
scroll_round,
|
||||||
|
max_scrolls,
|
||||||
|
visible_count,
|
||||||
|
new_count,
|
||||||
|
collected_count,
|
||||||
|
no_new_rounds,
|
||||||
|
no_new_limit,
|
||||||
|
end_marker,
|
||||||
|
page.url,
|
||||||
|
)
|
||||||
|
|
||||||
|
if len(collected) >= max_items:
|
||||||
|
collected = collected[:max_items]
|
||||||
|
stop_reason = "max_items"
|
||||||
|
break
|
||||||
|
|
||||||
|
if end_marker:
|
||||||
|
stop_reason = "end_marker"
|
||||||
|
_step_cb(step_callback, "发现结果到底或达到采集数量")
|
||||||
|
break
|
||||||
|
|
||||||
|
if no_new_rounds >= no_new_limit:
|
||||||
|
stop_reason = "no_new_rounds"
|
||||||
|
logger.info(
|
||||||
|
"collect_no_new_stop round=%s no_new_rounds=%s no_new_limit=%s collected=%s",
|
||||||
|
scroll_round,
|
||||||
|
no_new_rounds,
|
||||||
|
no_new_limit,
|
||||||
|
collected_count,
|
||||||
|
)
|
||||||
|
break
|
||||||
|
|
||||||
|
if scroll_round >= max_scrolls:
|
||||||
|
break
|
||||||
|
|
||||||
|
_step_cb(step_callback, "滚动加载更多结果")
|
||||||
|
scroll_target, wheel_delta = await _mouse_wheel_scroll(page)
|
||||||
|
logger.info(
|
||||||
|
"scroll round=%s target=%s wheel_delta=%s",
|
||||||
|
scroll_round,
|
||||||
|
scroll_target,
|
||||||
|
wheel_delta,
|
||||||
|
)
|
||||||
|
await _scroll_wait()
|
||||||
|
|
||||||
|
verification_timeout = await wait_human_verification_if_present(
|
||||||
|
page, wait_sec=wait_sec, stage="collect"
|
||||||
|
)
|
||||||
|
if verification_timeout:
|
||||||
|
return _fail_on_verification_timeout(
|
||||||
|
verification_timeout,
|
||||||
|
collected=collected_count,
|
||||||
|
)
|
||||||
|
|
||||||
|
logger.info(
|
||||||
|
"collect_stop reason=%s collected=%s rounds=%s no_new_rounds=%s",
|
||||||
|
stop_reason,
|
||||||
|
collected_count,
|
||||||
|
rounds_executed,
|
||||||
|
no_new_rounds,
|
||||||
|
)
|
||||||
|
|
||||||
|
if not collected:
|
||||||
|
err = ScrapeError(
|
||||||
|
"NO_RESULTS",
|
||||||
|
"未采集到任何结果。",
|
||||||
|
browser_started=True,
|
||||||
|
stage="collect",
|
||||||
|
)
|
||||||
|
return _fail_result(err, collected=0)
|
||||||
|
|
||||||
|
_step_cb(step_callback, "采集完成")
|
||||||
|
logger.info(
|
||||||
|
"rpa_success collected=%s stop_reason=%s",
|
||||||
|
len(collected[:max_items]),
|
||||||
|
stop_reason,
|
||||||
|
)
|
||||||
|
return ScrapeRunResult(success=True, items=collected[:max_items], stop_reason=stop_reason)
|
||||||
|
|
||||||
|
except Exception as exc:
|
||||||
|
err = ScrapeError(
|
||||||
|
"UNKNOWN_ERROR",
|
||||||
|
str(exc),
|
||||||
|
browser_started=browser_started,
|
||||||
|
stage="rpa",
|
||||||
|
)
|
||||||
|
return _fail_result(err, collected=collected_count)
|
||||||
|
finally:
|
||||||
|
await close_browser_context(pw, context)
|
||||||
137
examples/real_browser_rpa/scripts/service/task_service.py
Normal file
137
examples/real_browser_rpa/scripts/service/task_service.py
Normal file
@@ -0,0 +1,137 @@
|
|||||||
|
"""轻量任务编排示例(无真实数据库依赖)。"""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import asyncio
|
||||||
|
import uuid
|
||||||
|
from typing import Any, Optional
|
||||||
|
|
||||||
|
from service.account_client import AccountManagerError, pick_web_account, release_lease
|
||||||
|
from service.task_rpa import (
|
||||||
|
ScrapeRunResult,
|
||||||
|
format_stop_reason_for_user,
|
||||||
|
run_keyword_search_async,
|
||||||
|
)
|
||||||
|
from util.constants import (
|
||||||
|
DEFAULT_MAX_ITEMS,
|
||||||
|
DEFAULT_MAX_SCROLLS,
|
||||||
|
SKILL_SLUG,
|
||||||
|
TARGET_PLATFORM,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def _error_payload(
|
||||||
|
code: str,
|
||||||
|
message: str,
|
||||||
|
*,
|
||||||
|
browser_started: Optional[bool] = None,
|
||||||
|
stage: Optional[str] = None,
|
||||||
|
) -> dict[str, Any]:
|
||||||
|
payload: dict[str, Any] = {
|
||||||
|
"success": False,
|
||||||
|
"error": {"code": code, "message": message},
|
||||||
|
}
|
||||||
|
if browser_started is not None:
|
||||||
|
payload["browser_started"] = browser_started
|
||||||
|
if stage:
|
||||||
|
payload["stage"] = stage
|
||||||
|
return payload
|
||||||
|
|
||||||
|
|
||||||
|
def _success_payload(
|
||||||
|
*,
|
||||||
|
keyword: str,
|
||||||
|
collected_count: int,
|
||||||
|
items: list[dict[str, Any]],
|
||||||
|
stop_reason: Optional[str],
|
||||||
|
) -> dict[str, Any]:
|
||||||
|
return {
|
||||||
|
"success": True,
|
||||||
|
"skill": SKILL_SLUG,
|
||||||
|
"keyword": keyword,
|
||||||
|
"collected_count": collected_count,
|
||||||
|
"items": items,
|
||||||
|
"stop_reason": stop_reason,
|
||||||
|
"stop_reason_message": format_stop_reason_for_user(stop_reason, collected_count),
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
async def run_keyword_search_task(
|
||||||
|
keyword: str,
|
||||||
|
*,
|
||||||
|
max_items: int = DEFAULT_MAX_ITEMS,
|
||||||
|
max_scrolls: int = DEFAULT_MAX_SCROLLS,
|
||||||
|
account_id: Optional[str] = None,
|
||||||
|
platform: str = TARGET_PLATFORM,
|
||||||
|
) -> dict[str, Any]:
|
||||||
|
"""
|
||||||
|
编排一次关键词搜索采集。
|
||||||
|
|
||||||
|
真实 skill 中还应:
|
||||||
|
- 写 SQLite 入库(items 表)
|
||||||
|
- 写 scrape_tasks / task_logs 任务日志
|
||||||
|
- 使用 RpaVideoSession 录制步骤留痕
|
||||||
|
- 失败时 capture_failure 截图
|
||||||
|
"""
|
||||||
|
batch_id = uuid.uuid4().hex[:12]
|
||||||
|
data_dir = "" # 示例占位;真实 skill 使用 get_skill_data_dir()
|
||||||
|
lease_token: Optional[str] = None
|
||||||
|
account: Optional[dict[str, Any]] = None
|
||||||
|
|
||||||
|
try:
|
||||||
|
account = pick_web_account(platform, account_id)
|
||||||
|
lease_token = account.get("lease_token")
|
||||||
|
|
||||||
|
scrape_result: ScrapeRunResult = await run_keyword_search_async(
|
||||||
|
account,
|
||||||
|
keyword,
|
||||||
|
max_items=max_items,
|
||||||
|
max_scrolls=max_scrolls,
|
||||||
|
data_dir=data_dir,
|
||||||
|
batch_id=batch_id,
|
||||||
|
)
|
||||||
|
|
||||||
|
if not scrape_result.success:
|
||||||
|
err = scrape_result.error
|
||||||
|
return _error_payload(
|
||||||
|
err.code if err else "UNKNOWN_ERROR",
|
||||||
|
err.message if err else "未知错误",
|
||||||
|
browser_started=err.browser_started if err else True,
|
||||||
|
stage=err.stage if err else "rpa",
|
||||||
|
)
|
||||||
|
|
||||||
|
collected_count = len(scrape_result.items)
|
||||||
|
return _success_payload(
|
||||||
|
keyword=keyword,
|
||||||
|
collected_count=collected_count,
|
||||||
|
items=scrape_result.items[:10],
|
||||||
|
stop_reason=scrape_result.stop_reason,
|
||||||
|
)
|
||||||
|
except AccountManagerError as exc:
|
||||||
|
return _error_payload(
|
||||||
|
exc.code,
|
||||||
|
exc.message,
|
||||||
|
browser_started=False,
|
||||||
|
stage="account_prepare",
|
||||||
|
)
|
||||||
|
finally:
|
||||||
|
release_lease(lease_token)
|
||||||
|
|
||||||
|
|
||||||
|
def run_keyword_search_task_sync(
|
||||||
|
keyword: str,
|
||||||
|
*,
|
||||||
|
max_items: int = DEFAULT_MAX_ITEMS,
|
||||||
|
max_scrolls: int = DEFAULT_MAX_SCROLLS,
|
||||||
|
account_id: Optional[str] = None,
|
||||||
|
platform: str = TARGET_PLATFORM,
|
||||||
|
) -> dict[str, Any]:
|
||||||
|
return asyncio.run(
|
||||||
|
run_keyword_search_task(
|
||||||
|
keyword,
|
||||||
|
max_items=max_items,
|
||||||
|
max_scrolls=max_scrolls,
|
||||||
|
account_id=account_id,
|
||||||
|
platform=platform,
|
||||||
|
)
|
||||||
|
)
|
||||||
31
examples/real_browser_rpa/scripts/util/constants.py
Normal file
31
examples/real_browser_rpa/scripts/util/constants.py
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
"""示例常量(复制到新 skill 时请替换为目标站点与业务字段)。"""
|
||||||
|
|
||||||
|
SKILL_SLUG = "example-real-browser-rpa"
|
||||||
|
LOG_LOGGER_NAME = "openclaw.skill.example_real_browser_rpa"
|
||||||
|
|
||||||
|
TARGET_PLATFORM = "target_platform"
|
||||||
|
LEASE_HOLDER = "example-real-browser-rpa"
|
||||||
|
LEASE_TTL_SEC = "1800"
|
||||||
|
|
||||||
|
DEFAULT_START_URL = "https://example.com"
|
||||||
|
DEFAULT_MAX_ITEMS = 100
|
||||||
|
DEFAULT_MAX_SCROLLS = 40
|
||||||
|
DEFAULT_NO_NEW_ROUNDS_LIMIT = 5
|
||||||
|
HUMAN_WAIT_TIMEOUT = 180
|
||||||
|
|
||||||
|
RESULT_END_TEXT = "没有更多结果了"
|
||||||
|
|
||||||
|
# 示例 selector — 复制到新 skill 时按目标站点 DOM 替换
|
||||||
|
SEARCH_INPUT_SELECTORS = (
|
||||||
|
'input[type="search"]',
|
||||||
|
'input[name="q"]',
|
||||||
|
'input[placeholder*="搜索"]',
|
||||||
|
)
|
||||||
|
SEARCH_BUTTON_SELECTOR = 'button:has-text("搜索")'
|
||||||
|
RESULT_CONTAINER_SELECTOR = "#search-result-container"
|
||||||
|
RESULT_ITEM_SELECTOR = "[data-result-item]"
|
||||||
|
SCROLL_CONTAINER_SELECTORS = (
|
||||||
|
"#search-result-container",
|
||||||
|
".search-result-scroll",
|
||||||
|
".route-scroll-container",
|
||||||
|
)
|
||||||
34
examples/real_browser_rpa/scripts/util/logging.py
Normal file
34
examples/real_browser_rpa/scripts/util/logging.py
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
"""示例结构化日志工具。"""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import logging
|
||||||
|
import re
|
||||||
|
from typing import Any
|
||||||
|
|
||||||
|
from util.constants import LOG_LOGGER_NAME
|
||||||
|
|
||||||
|
|
||||||
|
def get_logger(name: str | None = None) -> logging.Logger:
|
||||||
|
return logging.getLogger(name or LOG_LOGGER_NAME)
|
||||||
|
|
||||||
|
|
||||||
|
def mask_text(value: str, *, keep_start: int = 2, keep_end: int = 2) -> str:
|
||||||
|
s = (value or "").strip()
|
||||||
|
if len(s) >= 11 and s.isdigit():
|
||||||
|
return s[:3] + "****" + s[-4:]
|
||||||
|
if len(s) > keep_start + keep_end:
|
||||||
|
return s[:keep_start] + "****" + s[-keep_end:]
|
||||||
|
return "****"
|
||||||
|
|
||||||
|
|
||||||
|
def safe_log_value(value: Any) -> str:
|
||||||
|
if value is None:
|
||||||
|
return ""
|
||||||
|
text = str(value)
|
||||||
|
if re.fullmatch(r"\d{7,}", text):
|
||||||
|
return mask_text(text)
|
||||||
|
if "@" in text and len(text) > 6:
|
||||||
|
local, _, domain = text.partition("@")
|
||||||
|
return f"{mask_text(local, keep_start=1, keep_end=1)}@{domain}"
|
||||||
|
return text
|
||||||
10
examples/real_browser_rpa/tests/conftest.py
Normal file
10
examples/real_browser_rpa/tests/conftest.py
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
"""pytest 路径配置:将 examples/real_browser_rpa/scripts 加入 import 路径。"""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import sys
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
SCRIPTS = Path(__file__).resolve().parents[1] / "scripts"
|
||||||
|
if str(SCRIPTS) not in sys.path:
|
||||||
|
sys.path.insert(0, str(SCRIPTS))
|
||||||
170
examples/real_browser_rpa/tests/test_human_verification.py
Normal file
170
examples/real_browser_rpa/tests/test_human_verification.py
Normal file
@@ -0,0 +1,170 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
"""人工验证检测单元测试(DOM 精准检测,不依赖真实浏览器)。"""
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import asyncio
|
||||||
|
import time
|
||||||
|
import unittest
|
||||||
|
|
||||||
|
from service.human_verification import (
|
||||||
|
HumanVerificationWaitResult,
|
||||||
|
detect_human_verification,
|
||||||
|
detect_human_verification_from_text,
|
||||||
|
wait_human_verification_if_present,
|
||||||
|
wait_save_login_countdown_if_present,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
SLIDER_HTML = """
|
||||||
|
<div class="vc-captcha-verify slide">
|
||||||
|
<div class="captcha_verify_bar--title">请完成下列验证后继续</div>
|
||||||
|
<div class="captcha-slider-tips">按住左边按钮拖动完成上方拼图</div>
|
||||||
|
</div>
|
||||||
|
"""
|
||||||
|
|
||||||
|
SMS_HTML = """
|
||||||
|
<div class="second-verify-panel">
|
||||||
|
<div class="uc-ui-verify_sms-input">
|
||||||
|
<div class="uc-ui-verify-new_header-title">短信验证</div>
|
||||||
|
<input placeholder="手机号" />
|
||||||
|
<input placeholder="请输入验证码" />
|
||||||
|
<button>获取验证码</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
"""
|
||||||
|
|
||||||
|
SEARCH_RESULTS_HTML = """
|
||||||
|
<div id="search-result-container">
|
||||||
|
<div data-result-item="1">RPA 滑块验证码识别教程</div>
|
||||||
|
<div data-result-item="2">短信验证码自动化</div>
|
||||||
|
</div>
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
class MockLocator:
|
||||||
|
def __init__(self, selector: str, visible: set[str], texts: dict[str, str]) -> None:
|
||||||
|
self._selector = selector
|
||||||
|
self._visible = visible
|
||||||
|
self._texts = texts
|
||||||
|
|
||||||
|
@property
|
||||||
|
def first(self) -> MockLocator:
|
||||||
|
return self
|
||||||
|
|
||||||
|
async def is_visible(self, timeout: int = 500) -> bool:
|
||||||
|
return self._selector in self._visible
|
||||||
|
|
||||||
|
async def inner_text(self, timeout: int = 1000) -> str:
|
||||||
|
return self._texts.get(self._selector, "")
|
||||||
|
|
||||||
|
async def count(self) -> int:
|
||||||
|
return 1 if self._selector in self._visible else 0
|
||||||
|
|
||||||
|
|
||||||
|
class MockPage:
|
||||||
|
def __init__(self, visible: set[str], texts: dict[str, str] | None = None) -> None:
|
||||||
|
self._visible = visible
|
||||||
|
self._texts = texts or {}
|
||||||
|
|
||||||
|
def locator(self, selector: str) -> MockLocator:
|
||||||
|
return MockLocator(selector, self._visible, self._texts)
|
||||||
|
|
||||||
|
|
||||||
|
class TestHumanVerificationFromText(unittest.TestCase):
|
||||||
|
def test_slider_dom_detect(self) -> None:
|
||||||
|
state = detect_human_verification_from_text(SLIDER_HTML)
|
||||||
|
self.assertTrue(state.present)
|
||||||
|
self.assertEqual(state.kind, "slider")
|
||||||
|
self.assertTrue(state.selector)
|
||||||
|
|
||||||
|
def test_sms_dom_detect(self) -> None:
|
||||||
|
state = detect_human_verification_from_text(SMS_HTML)
|
||||||
|
self.assertTrue(state.present)
|
||||||
|
self.assertEqual(state.kind, "sms")
|
||||||
|
self.assertTrue(state.selector)
|
||||||
|
|
||||||
|
def test_search_results_not_misclassified(self) -> None:
|
||||||
|
state = detect_human_verification_from_text(SEARCH_RESULTS_HTML)
|
||||||
|
self.assertFalse(state.present)
|
||||||
|
|
||||||
|
def test_body_keywords_without_dom_not_misclassified(self) -> None:
|
||||||
|
text = "RPA 滑块验证码识别教程\n短信验证码自动化"
|
||||||
|
state = detect_human_verification_from_text(text)
|
||||||
|
self.assertFalse(state.present)
|
||||||
|
|
||||||
|
|
||||||
|
class TestHumanVerificationDomDetect(unittest.TestCase):
|
||||||
|
def test_slider_dom_visible(self) -> None:
|
||||||
|
page = MockPage(
|
||||||
|
{".vc-captcha-verify.slide"},
|
||||||
|
{".vc-captcha-verify.slide": "请完成下列验证后继续"},
|
||||||
|
)
|
||||||
|
state = asyncio.run(detect_human_verification(page))
|
||||||
|
self.assertTrue(state.present)
|
||||||
|
self.assertEqual(state.kind, "slider")
|
||||||
|
self.assertEqual(state.selector, ".vc-captcha-verify.slide")
|
||||||
|
|
||||||
|
def test_sms_dom_visible(self) -> None:
|
||||||
|
page = MockPage(
|
||||||
|
{".second-verify-panel"},
|
||||||
|
{".second-verify-panel": "短信验证 获取验证码"},
|
||||||
|
)
|
||||||
|
state = asyncio.run(detect_human_verification(page))
|
||||||
|
self.assertTrue(state.present)
|
||||||
|
self.assertEqual(state.kind, "sms")
|
||||||
|
|
||||||
|
def test_no_verification_when_empty(self) -> None:
|
||||||
|
page = MockPage(set())
|
||||||
|
state = asyncio.run(detect_human_verification(page))
|
||||||
|
self.assertFalse(state.present)
|
||||||
|
|
||||||
|
|
||||||
|
class TestSaveLoginCountdown(unittest.TestCase):
|
||||||
|
def test_wait_not_present_no_delay(self) -> None:
|
||||||
|
page = MockPage(set())
|
||||||
|
start = time.monotonic()
|
||||||
|
result = asyncio.run(wait_save_login_countdown_if_present(page, timeout_sec=8))
|
||||||
|
elapsed = time.monotonic() - start
|
||||||
|
self.assertFalse(result)
|
||||||
|
self.assertLess(elapsed, 1.0)
|
||||||
|
|
||||||
|
|
||||||
|
class TestHumanVerificationWait(unittest.TestCase):
|
||||||
|
def test_wait_timeout_slider_dom(self) -> None:
|
||||||
|
page = MockPage(
|
||||||
|
{".vc-captcha-verify.slide"},
|
||||||
|
{".vc-captcha-verify.slide": "请完成下列验证后继续"},
|
||||||
|
)
|
||||||
|
result = asyncio.run(
|
||||||
|
wait_human_verification_if_present(page, wait_sec=1, stage="search_verification")
|
||||||
|
)
|
||||||
|
self.assertIsInstance(result, HumanVerificationWaitResult)
|
||||||
|
self.assertTrue(result.timed_out)
|
||||||
|
self.assertEqual(result.code, "SLIDER_VERIFICATION_TIMEOUT")
|
||||||
|
self.assertIn("滑块验证码未在 1 秒内完成", result.message)
|
||||||
|
self.assertEqual(result.stage, "search_verification")
|
||||||
|
|
||||||
|
def test_wait_timeout_sms_dom(self) -> None:
|
||||||
|
page = MockPage(
|
||||||
|
{".second-verify-panel"},
|
||||||
|
{".second-verify-panel": "短信验证 获取验证码"},
|
||||||
|
)
|
||||||
|
result = asyncio.run(
|
||||||
|
wait_human_verification_if_present(page, wait_sec=1, stage="login_check")
|
||||||
|
)
|
||||||
|
self.assertIsInstance(result, HumanVerificationWaitResult)
|
||||||
|
self.assertTrue(result.timed_out)
|
||||||
|
self.assertEqual(result.code, "SMS_VERIFICATION_TIMEOUT")
|
||||||
|
self.assertEqual(result.stage, "login_check")
|
||||||
|
|
||||||
|
def test_collect_no_verification_immediate_none(self) -> None:
|
||||||
|
page = MockPage(set())
|
||||||
|
start = time.monotonic()
|
||||||
|
err = asyncio.run(wait_human_verification_if_present(page, wait_sec=180, stage="collect"))
|
||||||
|
elapsed = time.monotonic() - start
|
||||||
|
self.assertIsNone(err)
|
||||||
|
self.assertLess(elapsed, 1.0)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
unittest.main()
|
||||||
34
examples/real_browser_rpa/tests/test_stop_reason.py
Normal file
34
examples/real_browser_rpa/tests/test_stop_reason.py
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
"""stop_reason 用户友好文案单元测试。"""
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import unittest
|
||||||
|
|
||||||
|
from service.task_rpa import format_stop_reason_for_user
|
||||||
|
|
||||||
|
|
||||||
|
class TestFormatStopReasonForUser(unittest.TestCase):
|
||||||
|
def test_max_items(self) -> None:
|
||||||
|
msg = format_stop_reason_for_user("max_items", 100)
|
||||||
|
self.assertIn("默认采集规模", msg)
|
||||||
|
self.assertIn("100", msg)
|
||||||
|
|
||||||
|
def test_end_marker(self) -> None:
|
||||||
|
msg = format_stop_reason_for_user("end_marker", 42)
|
||||||
|
self.assertIn("没有更多结果", msg)
|
||||||
|
|
||||||
|
def test_no_new_rounds(self) -> None:
|
||||||
|
msg = format_stop_reason_for_user("no_new_rounds", 30)
|
||||||
|
self.assertIn("连续多次加载没有发现新结果", msg)
|
||||||
|
|
||||||
|
def test_max_scrolls(self) -> None:
|
||||||
|
msg = format_stop_reason_for_user("max_scrolls", 80)
|
||||||
|
self.assertIn("页面加载保护范围", msg)
|
||||||
|
|
||||||
|
def test_none_or_unknown(self) -> None:
|
||||||
|
self.assertEqual(format_stop_reason_for_user(None, 10), "本次采集已完成。")
|
||||||
|
self.assertEqual(format_stop_reason_for_user("unknown", 10), "本次采集已完成。")
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
unittest.main()
|
||||||
35
examples/simulator_api/README.md
Normal file
35
examples/simulator_api/README.md
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
# Simulator API Example
|
||||||
|
|
||||||
|
这个目录用于沉淀「仿真环境 / API」类 skill 的可复制成功案例。
|
||||||
|
|
||||||
|
## 适用场景(规划)
|
||||||
|
|
||||||
|
- 使用 mock / sandbox / simulator API
|
||||||
|
- 不需要启动浏览器
|
||||||
|
- 适合演示输入校验、API 调用、结果入库、日志、错误契约和测试
|
||||||
|
|
||||||
|
## 当前状态:规划占位
|
||||||
|
|
||||||
|
- **本目录当前为规划占位**,仅保留最小结构(`README.md`、`scripts/`、`tests/` 占位)。
|
||||||
|
- **尚未沉淀可复制实现**;目录内无参考代码。
|
||||||
|
- **AI 编程工具不得**基于空目录自行脑补 mock API 或仿真接口实现。
|
||||||
|
- 后续补齐可复制案例后,再按 README copy map 选择性复制到新技能 `scripts/`。
|
||||||
|
|
||||||
|
## 轻量结构
|
||||||
|
|
||||||
|
```text
|
||||||
|
simulator_api/
|
||||||
|
README.md
|
||||||
|
scripts/
|
||||||
|
service/
|
||||||
|
util/
|
||||||
|
tests/
|
||||||
|
```
|
||||||
|
|
||||||
|
不要在本目录添加 `SKILL.md`、市场 README、`references/`、`development/`、`.github/` 等完整 skill 仓库层文件。
|
||||||
|
|
||||||
|
## 复制边界(待案例补齐后生效)
|
||||||
|
|
||||||
|
- 参考代码将从 `examples/simulator_api/scripts/` 选择性复制到新技能 `scripts/`
|
||||||
|
- 根 `README.md`、`SKILL.md`、`references/` 仍按模板主目录规则编写
|
||||||
|
- 当前阶段:**不可作为实现参考**
|
||||||
0
examples/simulator_api/scripts/service/.gitkeep
Normal file
0
examples/simulator_api/scripts/service/.gitkeep
Normal file
0
examples/simulator_api/scripts/util/.gitkeep
Normal file
0
examples/simulator_api/scripts/util/.gitkeep
Normal file
0
examples/simulator_api/tests/.gitkeep
Normal file
0
examples/simulator_api/tests/.gitkeep
Normal file
154
examples/simulator_browser_rpa/README.md
Normal file
154
examples/simulator_browser_rpa/README.md
Normal file
@@ -0,0 +1,154 @@
|
|||||||
|
# Simulator Browser RPA Example
|
||||||
|
|
||||||
|
这是一个**仿真浏览器 RPA** 的可复制参考实现:在可控 sandbox 页面上,通过 **async Playwright** 完成门户门闩、两步登录、表单填写、PIN 弹窗、结果解析。
|
||||||
|
|
||||||
|
本示例抽象自已跑通的「工行批量提交类」浏览器自动化 skill 的工程范式,但已**去业务化**——不包含真实银行/真实资金语义,也**不是** Amazon 或任何生产技能的复制。
|
||||||
|
|
||||||
|
## 适合什么场景
|
||||||
|
|
||||||
|
- 自有 **行业仿真系统** / `sandbox.jc2009.com` / 本地 `sandbox/demo_app.html`
|
||||||
|
- 需要 **浏览器 RPA** 做端到端演示或回归
|
||||||
|
- 表单较复杂:门户 HITL、多步登录、Tab 切换、动态增行、PIN 弹窗、成功页解析
|
||||||
|
- 需要 **adapter 分层**:`mock`(CI)与 `simulator_rpa`(半集成)可切换
|
||||||
|
|
||||||
|
## 不适合什么场景
|
||||||
|
|
||||||
|
- **真实第三方网站 + 高风控 + 人工验证码** → 优先看 [`../real_browser_rpa/README.md`](../real_browser_rpa/README.md)
|
||||||
|
- 目标系统有稳定 **官方 API** → 不要硬写浏览器 RPA
|
||||||
|
- **不要把本示例的业务字段、selector、sandbox URL 原样复制**到你的 skill
|
||||||
|
|
||||||
|
## 目录结构
|
||||||
|
|
||||||
|
```
|
||||||
|
simulator_browser_rpa/
|
||||||
|
├── README.md
|
||||||
|
├── sandbox/
|
||||||
|
│ └── demo_app.html # 本地仿真页面(门户门闩 + 批量提交教学 DOM)
|
||||||
|
├── scripts/
|
||||||
|
│ ├── service/
|
||||||
|
│ │ ├── browser_session.py # async 启动 persistent context(不含 goto)
|
||||||
|
│ │ ├── account_client.py # account-manager subprocess(对齐 real_browser_rpa)
|
||||||
|
│ │ ├── simulator_playwright.py # RPA 主流程(厚)
|
||||||
|
│ │ ├── task_service.py # async 编排层
|
||||||
|
│ │ └── adapter/
|
||||||
|
│ │ ├── base.py # BatchItem / BatchSubmitResult 契约
|
||||||
|
│ │ ├── mock.py # mock/unit 档
|
||||||
|
│ │ ├── simulator_rpa.py # 薄 adapter:pick / lease / 委托 RPA
|
||||||
|
│ │ └── __init__.py # select_adapter
|
||||||
|
│ └── util/
|
||||||
|
│ ├── constants.py
|
||||||
|
│ └── logging.py
|
||||||
|
└── tests/
|
||||||
|
├── test_adapter_dispatch.py
|
||||||
|
├── test_no_rpa_helpers_import.py
|
||||||
|
├── test_simulator_rpa_unit.py
|
||||||
|
└── test_stop_reason.py
|
||||||
|
```
|
||||||
|
|
||||||
|
## 文件职责
|
||||||
|
|
||||||
|
| 文件 | 职责 |
|
||||||
|
|---|---|
|
||||||
|
| `browser_session.py` | async 系统 Chrome/Edge + launch args;**不在此 goto** |
|
||||||
|
| `account_client.py` | **唯一** account-manager subprocess 封装(`pick_web_account` / `release_lease`) |
|
||||||
|
| `simulator_playwright.py` | 门户轮询、登录、批量表单、PIN、解析 `batch_id`、截图 |
|
||||||
|
| `adapter/simulator_rpa.py` | **薄** adapter:pick 账号、lease、委托 RPA、`finally release_lease` |
|
||||||
|
| `adapter/mock.py` | 不启浏览器,供 unit/mock/CI |
|
||||||
|
| `task_service.py` | async 校验输入 → 选 adapter → `await submit_batch` |
|
||||||
|
| `sandbox/demo_app.html` | 可控 DOM;含可选门户门闩 + 原批量提交流程 |
|
||||||
|
|
||||||
|
## 核心流程
|
||||||
|
|
||||||
|
1. `await run_batch_submit(target, items)` 校验参数
|
||||||
|
2. `select_adapter()` 按 `OPENCLAW_TEST_TARGET` 选择 mock 或 simulator RPA
|
||||||
|
3. `simulator_rpa` 档:`SimulatorBrowserRpaAdapter.submit_batch` 内 `pick_web_account(sim_batch)` → `submit_batch_rpa` → `release_lease`
|
||||||
|
4. RPA:门户门闩(若可见则 HITL 轮询)→ 登录两步 → 批量页 → 填表 → 提交 → PIN → 解析 `batch_id`
|
||||||
|
5. 失败截图(有 `artifacts_dir` 时)→ 返回 `BatchSubmitResult`
|
||||||
|
|
||||||
|
## 两档仿真目标
|
||||||
|
|
||||||
|
| 档位 | 目标 | 说明 |
|
||||||
|
|---|---|---|
|
||||||
|
| 本地教学 | `sandbox/demo_app.html`(`file://`) | 结构教学 + mock 单测默认 |
|
||||||
|
| 线上联调 | `https://sandbox.jc2009.com/...` | 设 `SIMULATOR_BASE_URL` + account-manager `sim_batch` 账号 |
|
||||||
|
|
||||||
|
联调时账号 `url` 用行业根路径,**不要**写 `/login` 子路径;`auth_strategy=per_session_manual`。
|
||||||
|
|
||||||
|
## account-manager 注册示例(泛化)
|
||||||
|
|
||||||
|
```bash
|
||||||
|
python .../account-manager/scripts/main.py account add-web \
|
||||||
|
--platform sim_batch \
|
||||||
|
--login-id demo001 \
|
||||||
|
--url file:///.../demo_app.html \
|
||||||
|
--environment simulator \
|
||||||
|
--auth-strategy per_session_manual
|
||||||
|
```
|
||||||
|
|
||||||
|
线上 sandbox 时将 `--url` 换为 `https://sandbox.jc2009.com/...` 占位路径(按团队部署填写)。
|
||||||
|
|
||||||
|
**PIN/密码演示值**:RPA 从 account 的 `login_id` 取账号;`password` / `token_pin` 允许从 account 字段或环境变量 `SIMULATOR_PASSWORD` / `SIMULATOR_TOKEN_PIN` 回退;验证码固定演示值 `0000`(仅 sandbox 用)。
|
||||||
|
|
||||||
|
## Copy map(复制到新技能 `scripts/`)
|
||||||
|
|
||||||
|
| 来源(examples) | 目标(新技能) | 说明 |
|
||||||
|
|---|---|---|
|
||||||
|
| `browser_session.py` | `scripts/service/browser_session.py` | async;URL 仅 `page.goto`,不进 launch args |
|
||||||
|
| `account_client.py` | `scripts/service/account_client.py` | subprocess,对齐 `real_browser_rpa` |
|
||||||
|
| `simulator_playwright.py` | `scripts/service/<platform>_playwright.py` | RPA 主流程(厚) |
|
||||||
|
| `adapter/` | `scripts/service/<domain>_adapter/` | 薄 `simulator_rpa` + mock |
|
||||||
|
| `task_service.py` | `scripts/service/task_service.py` | async 编排 |
|
||||||
|
| `util/constants.py` | `scripts/util/constants.py` | 须替换 `TARGET_PLATFORM` 等业务常量 |
|
||||||
|
| `tests/` | `tests/` | 可按需复制 dispatch / unit / 禁止 import 守护 |
|
||||||
|
| `sandbox/demo_app.html` | — | **仅仿真测试**,不进入生产 skill |
|
||||||
|
|
||||||
|
## 明确禁止
|
||||||
|
|
||||||
|
- **不要** `import account-manager` 内部模块(`rpa_helpers`、`inject_account_manager_scripts_path`、`get_account_credential`)
|
||||||
|
- **不要** sync Playwright 用于完整 RPA 主路径
|
||||||
|
- **不要在** `task_service.py` / RPA 主流程里散落 account-manager subprocess
|
||||||
|
- **不要在技能侧**执行 `playwright install` / `pip install playwright`
|
||||||
|
- **不要把 URL 放进** `launch_persistent_context(args=[...])`
|
||||||
|
- **不要** vendor `scripts/jiangchang_skill_core/`
|
||||||
|
- **不要**照抄 slug、logger 名、演示账号到生产 skill
|
||||||
|
|
||||||
|
> 旧版 `pick_simulator_account()` / `SimulatorAccount` / `set_credentials()` 已废弃;账号统一由 account-manager 下发。
|
||||||
|
|
||||||
|
## 联调检查表
|
||||||
|
|
||||||
|
- [ ] 数据目录 `.env` 中 `OPENCLAW_TEST_TARGET=simulator_rpa`(非 mock)
|
||||||
|
- [ ] account-manager:`platform ensure` + 账号 `active` + 有 `profile_dir`
|
||||||
|
- [ ] 目标 sandbox UI 已部署(跨团队)
|
||||||
|
- [ ] 失败先查 `rpa-artifacts/` 截图,再查 Chrome profile 缓存(可用 `--user-data-dir` 手工打开清站点数据)
|
||||||
|
- [ ] 默认 `tests/run_tests.py` / 本 example `pytest` 仍全部通过(mock 离线)
|
||||||
|
|
||||||
|
## 依赖说明
|
||||||
|
|
||||||
|
- Python 3.10+
|
||||||
|
- Python 包 `playwright`:由**宿主共享 runtime** 提供;技能侧不要 `pip install playwright`
|
||||||
|
- 默认使用**系统 Chrome/Edge**;技能侧不要 `playwright install`
|
||||||
|
- `jiangchang_skill_core.runtime_env.find_chrome_executable`(可选)
|
||||||
|
|
||||||
|
## 环境变量(示例)
|
||||||
|
|
||||||
|
| 变量 | 说明 |
|
||||||
|
|---|---|
|
||||||
|
| `OPENCLAW_TEST_TARGET` | `mock`/`unit` → Mock;`simulator_rpa` → 仿真 RPA |
|
||||||
|
| `SIMULATOR_BASE_URL` | 仿真页 URL;默认本地 `demo_app.html` 的 file URI |
|
||||||
|
| `SIMULATOR_PASSWORD` / `SIMULATOR_TOKEN_PIN` | 演示密码/PIN 回退(账号字段优先) |
|
||||||
|
| `PORTAL_LOGIN_WAIT_SEC` | 门户 HITL 超时(默认 120) |
|
||||||
|
| `OPENCLAW_BROWSER_HEADLESS` | `0` 默认有头;`1` 无头(CI 可用) |
|
||||||
|
|
||||||
|
## 运行测试
|
||||||
|
|
||||||
|
```bash
|
||||||
|
cd examples/simulator_browser_rpa
|
||||||
|
python -m pytest tests/ -v
|
||||||
|
```
|
||||||
|
|
||||||
|
测试为 mock/纯函数,**不启动真实浏览器**。
|
||||||
|
|
||||||
|
## 当前状态
|
||||||
|
|
||||||
|
- async Playwright + 薄 adapter + `simulator_playwright.py` 工程范式已对齐 `real_browser_rpa`。
|
||||||
|
- 未接入完整 CLI、数据库与 entitlement。
|
||||||
155
examples/simulator_browser_rpa/sandbox/demo_app.html
Normal file
155
examples/simulator_browser_rpa/sandbox/demo_app.html
Normal file
@@ -0,0 +1,155 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="zh-CN">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8" />
|
||||||
|
<title>仿真批量提交系统(示例 Sandbox)</title>
|
||||||
|
<style>
|
||||||
|
body { font-family: sans-serif; margin: 24px; max-width: 960px; }
|
||||||
|
section { margin-bottom: 24px; padding: 16px; border: 1px solid #ccc; }
|
||||||
|
.hidden { display: none; }
|
||||||
|
table { border-collapse: collapse; width: 100%; }
|
||||||
|
td, th { border: 1px solid #ddd; padding: 8px; }
|
||||||
|
dialog-like { display: block; border: 2px solid #333; padding: 16px; margin-top: 16px; }
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<h1>仿真业务系统 · 批量提交(示例)</h1>
|
||||||
|
|
||||||
|
<section id="portal-section">
|
||||||
|
<h2>平台门户登录(示例)</h2>
|
||||||
|
<form name="portalGate">
|
||||||
|
<label>门户账号 <input id="portal-user" name="portalUser" /></label>
|
||||||
|
<label>门户密码 <input id="portal-pass" name="portalPass" type="password" /></label>
|
||||||
|
<button type="button" id="portal-enter">进入业务系统</button>
|
||||||
|
</form>
|
||||||
|
</section>
|
||||||
|
|
||||||
|
<section id="login-step1-section" class="hidden">
|
||||||
|
<h2>登录 · 第一步</h2>
|
||||||
|
<form name="simLoginStep1" data-testid="login-step1">
|
||||||
|
<label>账号 <input name="loginId" data-testid="login-id" /></label>
|
||||||
|
<button type="submit">下一步</button>
|
||||||
|
</form>
|
||||||
|
</section>
|
||||||
|
|
||||||
|
<section id="login-step2-section" class="hidden">
|
||||||
|
<h2>登录 · 第二步</h2>
|
||||||
|
<form name="simLoginStep2" data-testid="login-step2">
|
||||||
|
<label>密码 <input name="password" type="password" data-testid="password" /></label>
|
||||||
|
<label>验证码 <input name="captcha" data-testid="captcha" /></label>
|
||||||
|
<button type="submit">登录</button>
|
||||||
|
</form>
|
||||||
|
</section>
|
||||||
|
|
||||||
|
<section id="batch-section" class="hidden">
|
||||||
|
<h2>批量提交</h2>
|
||||||
|
<form name="simBatchForm" data-testid="batch-form">
|
||||||
|
<label>来源账户
|
||||||
|
<select name="fromAccountNo" data-testid="from-account">
|
||||||
|
<option value="">请选择</option>
|
||||||
|
<option value="acct-demo-001">演示账户 acct-demo-001</option>
|
||||||
|
<option value="acct-demo-002">演示账户 acct-demo-002</option>
|
||||||
|
</select>
|
||||||
|
</label>
|
||||||
|
<button type="button" id="tab-page-fill" data-testid="tab-page-fill">页面填写</button>
|
||||||
|
<button type="button" id="btn-add-row" data-testid="add-row">新增一行</button>
|
||||||
|
<table id="batch-table" data-testid="batch-table">
|
||||||
|
<thead><tr><th>#</th><th>姓名</th><th>账号</th><th>备注</th><th>金额</th></tr></thead>
|
||||||
|
<tbody id="batch-rows"></tbody>
|
||||||
|
</table>
|
||||||
|
<label>用途 <input name="purpose" data-testid="purpose" /></label>
|
||||||
|
<button type="submit" data-testid="batch-submit">提交批次</button>
|
||||||
|
</form>
|
||||||
|
</section>
|
||||||
|
|
||||||
|
<section id="success-section" class="hidden">
|
||||||
|
<h2>提交成功</h2>
|
||||||
|
<p data-testid="batch-id" id="batch-id-display"></p>
|
||||||
|
<p data-testid="success-message">批次已受理,等待后续处理。</p>
|
||||||
|
</section>
|
||||||
|
|
||||||
|
<div id="pin-dialog" class="hidden" role="dialog" aria-modal="true" data-testid="pin-dialog">
|
||||||
|
<h3>确认 PIN</h3>
|
||||||
|
<input type="password" inputmode="numeric" data-testid="pin-input" />
|
||||||
|
<button type="button" id="pin-confirm" data-testid="pin-confirm">确认提交</button>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<script>
|
||||||
|
const DEMO_CAPTCHA = "0000";
|
||||||
|
let rowCount = 0;
|
||||||
|
|
||||||
|
document.getElementById('portal-enter').addEventListener('click', function () {
|
||||||
|
document.getElementById('portal-section').classList.add('hidden');
|
||||||
|
document.getElementById('login-step1-section').classList.remove('hidden');
|
||||||
|
});
|
||||||
|
|
||||||
|
document.querySelector('form[name="simLoginStep1"]').addEventListener('submit', function (e) {
|
||||||
|
e.preventDefault();
|
||||||
|
document.getElementById('login-step1-section').classList.add('hidden');
|
||||||
|
document.getElementById('login-step2-section').classList.remove('hidden');
|
||||||
|
});
|
||||||
|
|
||||||
|
document.querySelector('form[name="simLoginStep2"]').addEventListener('submit', function (e) {
|
||||||
|
e.preventDefault();
|
||||||
|
const cap = document.querySelector('input[name="captcha"]').value;
|
||||||
|
if (cap !== DEMO_CAPTCHA) {
|
||||||
|
alert('验证码错误(演示值 0000)');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
document.getElementById('login-step2-section').classList.add('hidden');
|
||||||
|
document.getElementById('batch-section').classList.remove('hidden');
|
||||||
|
ensureRow(1);
|
||||||
|
});
|
||||||
|
|
||||||
|
document.getElementById('tab-page-fill').addEventListener('click', function () {
|
||||||
|
document.getElementById('batch-table').scrollIntoView();
|
||||||
|
});
|
||||||
|
|
||||||
|
document.getElementById('btn-add-row').addEventListener('click', function () {
|
||||||
|
rowCount += 1;
|
||||||
|
addRow(rowCount);
|
||||||
|
});
|
||||||
|
|
||||||
|
function ensureRow(index) {
|
||||||
|
if (!document.querySelector('tr[data-row-index="' + index + '"]')) {
|
||||||
|
addRow(index);
|
||||||
|
}
|
||||||
|
rowCount = Math.max(rowCount, index);
|
||||||
|
}
|
||||||
|
|
||||||
|
function addRow(index) {
|
||||||
|
rowCount = Math.max(rowCount, index);
|
||||||
|
const tbody = document.getElementById('batch-rows');
|
||||||
|
const tr = document.createElement('tr');
|
||||||
|
tr.setAttribute('data-row-index', String(index));
|
||||||
|
tr.innerHTML =
|
||||||
|
'<td>' + index + '</td>' +
|
||||||
|
'<td><input name="name" data-testid="row-name-' + index + '" /></td>' +
|
||||||
|
'<td><input name="account" data-testid="row-account-' + index + '" /></td>' +
|
||||||
|
'<td><input name="note" data-testid="row-note-' + index + '" /></td>' +
|
||||||
|
'<td><input name="amount" data-testid="row-amount-' + index + '" /></td>';
|
||||||
|
tbody.appendChild(tr);
|
||||||
|
}
|
||||||
|
|
||||||
|
document.querySelector('form[name="simBatchForm"]').addEventListener('submit', function (e) {
|
||||||
|
e.preventDefault();
|
||||||
|
document.getElementById('pin-dialog').classList.remove('hidden');
|
||||||
|
});
|
||||||
|
|
||||||
|
document.getElementById('pin-confirm').addEventListener('click', function () {
|
||||||
|
const pin = document.querySelector('[data-testid="pin-input"]').value;
|
||||||
|
if (!pin || pin.length < 4) {
|
||||||
|
alert('请输入演示 PIN');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
document.getElementById('pin-dialog').classList.add('hidden');
|
||||||
|
const batchId = 'SIM-' + Date.now();
|
||||||
|
document.getElementById('batch-section').classList.add('hidden');
|
||||||
|
document.getElementById('success-section').classList.remove('hidden');
|
||||||
|
document.getElementById('batch-id-display').textContent = batchId;
|
||||||
|
document.getElementById('batch-id-display').dataset.batchId = batchId;
|
||||||
|
window.location.hash = '#/batch/' + batchId;
|
||||||
|
});
|
||||||
|
</script>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
199
examples/simulator_browser_rpa/scripts/service/account_client.py
Normal file
199
examples/simulator_browser_rpa/scripts/service/account_client.py
Normal file
@@ -0,0 +1,199 @@
|
|||||||
|
"""account-manager CLI 集成(仅 subprocess,不 import 兄弟 skill 内部模块)。"""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import json
|
||||||
|
import logging
|
||||||
|
import os
|
||||||
|
import subprocess
|
||||||
|
import sys
|
||||||
|
from typing import Any, Dict, List, Optional
|
||||||
|
|
||||||
|
from jiangchang_skill_core.runtime_env import get_sibling_skills_root
|
||||||
|
|
||||||
|
from util.constants import LEASE_HOLDER, LEASE_TTL_SEC, TARGET_PLATFORM
|
||||||
|
from util.logging import mask_text
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
PLACEHOLDER_PLATFORM = TARGET_PLATFORM
|
||||||
|
|
||||||
|
ACCOUNT_SETUP_MESSAGE = (
|
||||||
|
f"未找到可用的 {PLACEHOLDER_PLATFORM} 账号,所以还没有打开浏览器。"
|
||||||
|
f"请先在 account-manager 中添加 platform={PLACEHOLDER_PLATFORM}、"
|
||||||
|
"status=active、带 profile_dir 的账号后重新运行。"
|
||||||
|
)
|
||||||
|
|
||||||
|
LEASE_BUSY_MESSAGE = "目标平台账号当前被其他任务占用,请等待释放租约后重试。"
|
||||||
|
|
||||||
|
|
||||||
|
class AccountManagerError(Exception):
|
||||||
|
def __init__(self, code: str, message: str) -> None:
|
||||||
|
super().__init__(message)
|
||||||
|
self.code = code
|
||||||
|
self.message = message
|
||||||
|
|
||||||
|
|
||||||
|
def mask_login_id(login_id: str) -> str:
|
||||||
|
return mask_text(login_id)
|
||||||
|
|
||||||
|
|
||||||
|
def _resolve_account_manager_main() -> str:
|
||||||
|
"""解析 account-manager CLI 入口路径。
|
||||||
|
|
||||||
|
优先级:ACCOUNT_MANAGER_ROOT → get_sibling_skills_root → 开发机兜底。
|
||||||
|
"""
|
||||||
|
env_root = (os.getenv("ACCOUNT_MANAGER_ROOT") or "").strip()
|
||||||
|
if env_root and os.path.isfile(os.path.join(env_root, "scripts", "main.py")):
|
||||||
|
return os.path.join(os.path.abspath(env_root), "scripts", "main.py")
|
||||||
|
|
||||||
|
scripts_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
||||||
|
skills_root = get_sibling_skills_root(scripts_dir)
|
||||||
|
candidate = os.path.join(skills_root, "account-manager", "scripts", "main.py")
|
||||||
|
if os.path.isfile(candidate):
|
||||||
|
return candidate
|
||||||
|
|
||||||
|
dev = r"D:\OpenClaw\client-commons\account-manager\scripts\main.py"
|
||||||
|
if os.path.isfile(dev):
|
||||||
|
return dev
|
||||||
|
|
||||||
|
raise AccountManagerError(
|
||||||
|
"ACCOUNT_NOT_FOUND",
|
||||||
|
"未找到 account-manager,请配置 ACCOUNT_MANAGER_ROOT 或安装 account-manager 技能。",
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def _parse_last_json(stdout: str) -> Any:
|
||||||
|
lines = [ln.strip() for ln in (stdout or "").splitlines() if ln.strip()]
|
||||||
|
for raw in reversed(lines):
|
||||||
|
if raw.startswith("ERROR:"):
|
||||||
|
continue
|
||||||
|
try:
|
||||||
|
return json.loads(raw)
|
||||||
|
except json.JSONDecodeError:
|
||||||
|
continue
|
||||||
|
raise AccountManagerError("UNKNOWN_ERROR", "stdout 中没有可解析的 JSON。")
|
||||||
|
|
||||||
|
|
||||||
|
def _run_argv(argv_suffix: List[str]) -> subprocess.CompletedProcess[str]:
|
||||||
|
main_py = _resolve_account_manager_main()
|
||||||
|
env = os.environ.copy()
|
||||||
|
return subprocess.run(
|
||||||
|
[sys.executable, main_py, *argv_suffix],
|
||||||
|
capture_output=True,
|
||||||
|
text=True,
|
||||||
|
encoding="utf-8",
|
||||||
|
errors="replace",
|
||||||
|
env=env,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def _normalize_error_code(raw: str) -> str:
|
||||||
|
code = (raw or "").strip()
|
||||||
|
if code.startswith("ERROR:"):
|
||||||
|
code = code[6:]
|
||||||
|
return code
|
||||||
|
|
||||||
|
|
||||||
|
def _validate_pick_payload(payload: dict[str, Any]) -> dict[str, Any]:
|
||||||
|
if payload.get("success") is False:
|
||||||
|
err = payload.get("error") if isinstance(payload.get("error"), dict) else {}
|
||||||
|
code = _normalize_error_code(str(err.get("code") or ""))
|
||||||
|
message = str(err.get("message") or "")
|
||||||
|
if code == "LEASE_CONFLICT" or "LEASE_CONFLICT" in code:
|
||||||
|
raise AccountManagerError("LEASE_CONFLICT", LEASE_BUSY_MESSAGE)
|
||||||
|
if code in ("NO_ACCOUNT", "ACCOUNT_NOT_FOUND") or "NO_ACCOUNT" in code:
|
||||||
|
raise AccountManagerError("NO_ACCOUNT", message or "没有可用账号。")
|
||||||
|
raise AccountManagerError(code or "PICK_WEB_FAILED", message or "pick-web 失败。")
|
||||||
|
|
||||||
|
if not isinstance(payload, dict):
|
||||||
|
raise AccountManagerError("ACCOUNT_NOT_FOUND", "pick-web 返回格式异常。")
|
||||||
|
if not payload.get("profile_dir"):
|
||||||
|
raise AccountManagerError("ACCOUNT_NOT_FOUND", "pick-web 返回的账号缺少 profile_dir。")
|
||||||
|
return payload
|
||||||
|
|
||||||
|
|
||||||
|
def _pick_web_with_lease(platform: str) -> Dict[str, Any]:
|
||||||
|
proc = _run_argv(
|
||||||
|
[
|
||||||
|
"account",
|
||||||
|
"pick-web",
|
||||||
|
"--platform",
|
||||||
|
platform,
|
||||||
|
"--lease",
|
||||||
|
"--holder",
|
||||||
|
LEASE_HOLDER,
|
||||||
|
"--ttl-sec",
|
||||||
|
LEASE_TTL_SEC,
|
||||||
|
]
|
||||||
|
)
|
||||||
|
out = proc.stdout or ""
|
||||||
|
if proc.returncode != 0 and not out.strip():
|
||||||
|
raise AccountManagerError(
|
||||||
|
"PICK_WEB_FAILED",
|
||||||
|
(proc.stderr or "").strip() or "pick-web 子进程失败",
|
||||||
|
)
|
||||||
|
payload = _parse_last_json(out)
|
||||||
|
if isinstance(payload, dict) and payload.get("success") is False:
|
||||||
|
return _validate_pick_payload(payload)
|
||||||
|
if not isinstance(payload, dict):
|
||||||
|
raise AccountManagerError("ACCOUNT_NOT_FOUND", "pick-web 返回格式异常。")
|
||||||
|
return payload
|
||||||
|
|
||||||
|
|
||||||
|
def _pick_by_id(platform: str, account_id: int) -> Dict[str, Any]:
|
||||||
|
proc = _run_argv(["account", "get", str(account_id)])
|
||||||
|
out = proc.stdout or ""
|
||||||
|
if proc.returncode != 0 or out.startswith("ERROR:"):
|
||||||
|
raise AccountManagerError("ACCOUNT_NOT_FOUND", f"指定账号 {account_id} 不存在或获取失败。")
|
||||||
|
raw = _parse_last_json(out)
|
||||||
|
if isinstance(raw, dict) and "data" in raw and isinstance(raw["data"], dict):
|
||||||
|
data = raw["data"]
|
||||||
|
elif isinstance(raw, dict):
|
||||||
|
data = raw
|
||||||
|
else:
|
||||||
|
raise AccountManagerError("ACCOUNT_NOT_FOUND", "account get 返回非对象 JSON。")
|
||||||
|
|
||||||
|
platform_key = str(data.get("platform_key") or "").lower()
|
||||||
|
if platform_key != platform.lower():
|
||||||
|
raise AccountManagerError(
|
||||||
|
"ACCOUNT_NOT_FOUND",
|
||||||
|
f"账号 {account_id} 不是 {platform} 平台账号。",
|
||||||
|
)
|
||||||
|
profile_dir = str(data.get("profile_dir") or "").strip()
|
||||||
|
if not profile_dir:
|
||||||
|
raise AccountManagerError("ACCOUNT_NOT_FOUND", f"账号 {account_id} 缺少 profile_dir。")
|
||||||
|
data["profile_dir"] = profile_dir
|
||||||
|
data["lease_token"] = ""
|
||||||
|
return data
|
||||||
|
|
||||||
|
|
||||||
|
def pick_web_account(platform: str, account_id: Optional[str] = None) -> dict:
|
||||||
|
"""获取网页账号(profile_dir + lease_token)。"""
|
||||||
|
platform_key = (platform or PLACEHOLDER_PLATFORM).strip() or PLACEHOLDER_PLATFORM
|
||||||
|
|
||||||
|
if account_id:
|
||||||
|
try:
|
||||||
|
aid = int(account_id)
|
||||||
|
except (TypeError, ValueError):
|
||||||
|
raise AccountManagerError("ACCOUNT_NOT_FOUND", f"账号 ID 无效:{account_id}")
|
||||||
|
return _pick_by_id(platform_key, aid)
|
||||||
|
|
||||||
|
try:
|
||||||
|
return _pick_web_with_lease(platform_key)
|
||||||
|
except AccountManagerError as exc:
|
||||||
|
if exc.code in ("NO_ACCOUNT", "ACCOUNT_NOT_FOUND") or "NO_ACCOUNT" in exc.code:
|
||||||
|
raise AccountManagerError("ACCOUNT_SETUP_REQUIRED", ACCOUNT_SETUP_MESSAGE) from exc
|
||||||
|
raise
|
||||||
|
|
||||||
|
|
||||||
|
def release_lease(lease_token: Optional[str]) -> None:
|
||||||
|
token = (lease_token or "").strip()
|
||||||
|
if not token:
|
||||||
|
return
|
||||||
|
try:
|
||||||
|
proc = _run_argv(["lease", "release", token])
|
||||||
|
if proc.returncode != 0:
|
||||||
|
logger.warning("lease_release_failed stdout=%s", (proc.stdout or "").strip())
|
||||||
|
except Exception:
|
||||||
|
logger.warning("lease_release_exception", exc_info=True)
|
||||||
@@ -0,0 +1,41 @@
|
|||||||
|
"""adapter 工厂:按 OPENCLAW_TEST_TARGET 选择实现。"""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import logging
|
||||||
|
from typing import Optional
|
||||||
|
|
||||||
|
from jiangchang_skill_core import config
|
||||||
|
|
||||||
|
from service.adapter.base import BatchAdapterBase, BatchItem, BatchSubmitResult
|
||||||
|
from service.adapter.mock import MockBatchAdapter
|
||||||
|
from service.adapter.simulator_rpa import SimulatorBrowserRpaAdapter
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
__all__ = [
|
||||||
|
"select_adapter",
|
||||||
|
"BatchAdapterBase",
|
||||||
|
"BatchItem",
|
||||||
|
"BatchSubmitResult",
|
||||||
|
"MockBatchAdapter",
|
||||||
|
"SimulatorBrowserRpaAdapter",
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
|
def select_adapter(artifacts_dir: Optional[str] = None) -> BatchAdapterBase:
|
||||||
|
target = (config.get("OPENCLAW_TEST_TARGET") or "").strip().lower()
|
||||||
|
|
||||||
|
if target in ("mock", "unit"):
|
||||||
|
logger.info("target '%s': MockBatchAdapter", target)
|
||||||
|
return MockBatchAdapter()
|
||||||
|
|
||||||
|
if target == "real_rpa":
|
||||||
|
logger.warning("target real_rpa 未实现,回退 MockBatchAdapter")
|
||||||
|
return MockBatchAdapter()
|
||||||
|
|
||||||
|
if target and target != "simulator_rpa":
|
||||||
|
logger.warning("未知 target '%s',回退 simulator_rpa", target)
|
||||||
|
|
||||||
|
logger.info("target '%s': SimulatorBrowserRpaAdapter", target or "(unset=default)")
|
||||||
|
return SimulatorBrowserRpaAdapter(artifacts_dir=artifacts_dir)
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
"""批量提交 adapter 基类与数据契约。"""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from dataclasses import dataclass, field
|
||||||
|
from typing import Any, Dict, List, Optional
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class BatchItem:
|
||||||
|
row_index: int
|
||||||
|
name: str
|
||||||
|
account: str
|
||||||
|
amount: float
|
||||||
|
note: str = ""
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class BatchSubmitResult:
|
||||||
|
ok: bool
|
||||||
|
batch_id: Optional[str]
|
||||||
|
submitted_count: int
|
||||||
|
submitted_amount: float
|
||||||
|
error_msg: Optional[str]
|
||||||
|
artifacts: Dict[str, Any] = field(default_factory=dict)
|
||||||
|
|
||||||
|
|
||||||
|
class BatchAdapterBase:
|
||||||
|
name: str = "base"
|
||||||
|
|
||||||
|
async def submit_batch(self, target: str, items: List[BatchItem]) -> BatchSubmitResult:
|
||||||
|
raise NotImplementedError
|
||||||
@@ -0,0 +1,36 @@
|
|||||||
|
"""mock / unit 档位:不启动浏览器。"""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import time
|
||||||
|
from typing import List
|
||||||
|
|
||||||
|
from service.adapter.base import BatchAdapterBase, BatchItem, BatchSubmitResult
|
||||||
|
|
||||||
|
|
||||||
|
class MockBatchAdapter(BatchAdapterBase):
|
||||||
|
name = "mock"
|
||||||
|
|
||||||
|
async def submit_batch(self, target: str, items: List[BatchItem]) -> BatchSubmitResult:
|
||||||
|
del target
|
||||||
|
if not items:
|
||||||
|
return BatchSubmitResult(
|
||||||
|
ok=False,
|
||||||
|
batch_id=None,
|
||||||
|
submitted_count=0,
|
||||||
|
submitted_amount=0.0,
|
||||||
|
error_msg="items 为空",
|
||||||
|
artifacts={"adapter": self.name},
|
||||||
|
)
|
||||||
|
|
||||||
|
time.sleep(0.05)
|
||||||
|
total = sum(it.amount for it in items)
|
||||||
|
fake_batch_id = f"MOCK-{int(time.time())}"
|
||||||
|
return BatchSubmitResult(
|
||||||
|
ok=True,
|
||||||
|
batch_id=fake_batch_id,
|
||||||
|
submitted_count=len(items),
|
||||||
|
submitted_amount=total,
|
||||||
|
error_msg=None,
|
||||||
|
artifacts={"adapter": self.name},
|
||||||
|
)
|
||||||
@@ -0,0 +1,109 @@
|
|||||||
|
"""仿真浏览器 RPA 薄 adapter:pick 账号、lease、委托 RPA、release lease。"""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import logging
|
||||||
|
from typing import List, Optional
|
||||||
|
|
||||||
|
from jiangchang_skill_core import config
|
||||||
|
|
||||||
|
from service.account_client import AccountManagerError, pick_web_account, release_lease
|
||||||
|
from service.adapter.base import BatchAdapterBase, BatchItem, BatchSubmitResult
|
||||||
|
from service.browser_session import find_chrome_executable
|
||||||
|
from service.simulator_playwright import submit_batch_rpa
|
||||||
|
from util.constants import TARGET_PLATFORM, resolve_simulator_base_url
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
def _resolve_entry_url(account: dict) -> str:
|
||||||
|
url = str(account.get("url") or "").strip()
|
||||||
|
if url:
|
||||||
|
return url.rstrip("/")
|
||||||
|
return resolve_simulator_base_url()
|
||||||
|
|
||||||
|
|
||||||
|
class SimulatorBrowserRpaAdapter(BatchAdapterBase):
|
||||||
|
name = "simulator_rpa"
|
||||||
|
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
artifacts_dir: Optional[str] = None,
|
||||||
|
headless: Optional[bool] = None,
|
||||||
|
) -> None:
|
||||||
|
self.artifacts_dir = artifacts_dir
|
||||||
|
if headless is None:
|
||||||
|
self.headless = config.get_bool("OPENCLAW_BROWSER_HEADLESS", default=False)
|
||||||
|
else:
|
||||||
|
self.headless = headless
|
||||||
|
|
||||||
|
async def submit_batch(self, target: str, items: List[BatchItem]) -> BatchSubmitResult:
|
||||||
|
try:
|
||||||
|
from playwright.async_api import async_playwright # noqa: F401
|
||||||
|
except ImportError:
|
||||||
|
return BatchSubmitResult(
|
||||||
|
ok=False,
|
||||||
|
batch_id=None,
|
||||||
|
submitted_count=0,
|
||||||
|
submitted_amount=0.0,
|
||||||
|
error_msg=(
|
||||||
|
"playwright Python 包不可用。"
|
||||||
|
"生产/宿主运行由共享 runtime 提供;技能侧不要 pip install playwright。"
|
||||||
|
),
|
||||||
|
artifacts={"adapter": self.name},
|
||||||
|
)
|
||||||
|
|
||||||
|
if not items:
|
||||||
|
return BatchSubmitResult(
|
||||||
|
ok=False,
|
||||||
|
batch_id=None,
|
||||||
|
submitted_count=0,
|
||||||
|
submitted_amount=0.0,
|
||||||
|
error_msg="items 为空",
|
||||||
|
artifacts={"adapter": self.name},
|
||||||
|
)
|
||||||
|
|
||||||
|
if not find_chrome_executable():
|
||||||
|
return BatchSubmitResult(
|
||||||
|
ok=False,
|
||||||
|
batch_id=None,
|
||||||
|
submitted_count=0,
|
||||||
|
submitted_amount=0.0,
|
||||||
|
error_msg="未检测到 Chrome 或 Edge 浏览器,请安装系统浏览器后重试。",
|
||||||
|
artifacts={"adapter": self.name},
|
||||||
|
)
|
||||||
|
|
||||||
|
lease_token: Optional[str] = None
|
||||||
|
try:
|
||||||
|
account = pick_web_account(TARGET_PLATFORM)
|
||||||
|
lease_token = account.get("lease_token")
|
||||||
|
base_url = _resolve_entry_url(account)
|
||||||
|
return await submit_batch_rpa(
|
||||||
|
account,
|
||||||
|
target=target,
|
||||||
|
items=items,
|
||||||
|
base_url=base_url,
|
||||||
|
artifacts_dir=self.artifacts_dir,
|
||||||
|
headless=self.headless,
|
||||||
|
)
|
||||||
|
except AccountManagerError as exc:
|
||||||
|
return BatchSubmitResult(
|
||||||
|
ok=False,
|
||||||
|
batch_id=None,
|
||||||
|
submitted_count=0,
|
||||||
|
submitted_amount=0.0,
|
||||||
|
error_msg=f"ERROR:{exc.code} {exc.message}",
|
||||||
|
artifacts={"adapter": self.name},
|
||||||
|
)
|
||||||
|
except Exception as exc:
|
||||||
|
logger.exception("adapter_unexpected: %s", exc)
|
||||||
|
return BatchSubmitResult(
|
||||||
|
ok=False,
|
||||||
|
batch_id=None,
|
||||||
|
submitted_count=0,
|
||||||
|
submitted_amount=0.0,
|
||||||
|
error_msg=f"未预期异常:{type(exc).__name__}: {exc}",
|
||||||
|
artifacts={"adapter": self.name},
|
||||||
|
)
|
||||||
|
finally:
|
||||||
|
release_lease(lease_token)
|
||||||
@@ -0,0 +1,126 @@
|
|||||||
|
"""浏览器会话启动(系统 Chrome/Edge + async Playwright persistent context)。"""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import logging
|
||||||
|
import os
|
||||||
|
from typing import Any, List, Optional, Tuple
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
DEFAULT_PAGE_TIMEOUT_MS = 15_000
|
||||||
|
|
||||||
|
CHROME_LAUNCH_ARGS: List[str] = [
|
||||||
|
"--start-maximized",
|
||||||
|
"--disable-blink-features=AutomationControlled",
|
||||||
|
]
|
||||||
|
|
||||||
|
STEALTH_INIT_SCRIPT = """
|
||||||
|
(() => {
|
||||||
|
try { Object.defineProperty(navigator, 'webdriver', { get: () => undefined }); } catch (e) {}
|
||||||
|
})();
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
def chrome_launch_args() -> List[str]:
|
||||||
|
return list(CHROME_LAUNCH_ARGS)
|
||||||
|
|
||||||
|
|
||||||
|
def _headless_from_env() -> bool:
|
||||||
|
v = (os.getenv("OPENCLAW_BROWSER_HEADLESS") or "0").strip().lower()
|
||||||
|
return v in ("1", "true", "yes", "on")
|
||||||
|
|
||||||
|
|
||||||
|
def find_chrome_executable() -> str | None:
|
||||||
|
try:
|
||||||
|
from jiangchang_skill_core.runtime_env import find_chrome_executable as _find
|
||||||
|
|
||||||
|
chrome = _find()
|
||||||
|
if chrome and os.path.isfile(chrome):
|
||||||
|
return chrome
|
||||||
|
except ImportError:
|
||||||
|
pass
|
||||||
|
|
||||||
|
candidates = [
|
||||||
|
os.path.expandvars(r"%ProgramFiles%\Google\Chrome\Application\chrome.exe"),
|
||||||
|
os.path.expandvars(r"%ProgramFiles(x86)%\Google\Chrome\Application\chrome.exe"),
|
||||||
|
os.path.expandvars(r"%ProgramFiles%\Microsoft\Edge\Application\msedge.exe"),
|
||||||
|
os.path.expandvars(r"%ProgramFiles(x86)%\Microsoft\Edge\Application\msedge.exe"),
|
||||||
|
]
|
||||||
|
for path in candidates:
|
||||||
|
if path and os.path.isfile(path):
|
||||||
|
return path
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
def _clear_node_options() -> None:
|
||||||
|
os.environ.pop("NODE_OPTIONS", None)
|
||||||
|
|
||||||
|
|
||||||
|
async def start_browser_session(
|
||||||
|
profile_dir: str,
|
||||||
|
*,
|
||||||
|
headless: Optional[bool] = None,
|
||||||
|
) -> Tuple[Any, Any, Any]:
|
||||||
|
"""
|
||||||
|
启动持久化 Chrome profile,返回 (playwright, context, page)。
|
||||||
|
|
||||||
|
URL 不在此文件打开;由 simulator_playwright.py 负责 page.goto。
|
||||||
|
"""
|
||||||
|
from playwright.async_api import async_playwright
|
||||||
|
|
||||||
|
_clear_node_options()
|
||||||
|
hl = headless if headless is not None else _headless_from_env()
|
||||||
|
chrome = find_chrome_executable()
|
||||||
|
if not chrome:
|
||||||
|
raise RuntimeError(
|
||||||
|
"ERROR:MISSING_BROWSER 未检测到 Chrome 或 Edge 浏览器,请先安装系统浏览器后重试。"
|
||||||
|
)
|
||||||
|
|
||||||
|
args = chrome_launch_args()
|
||||||
|
logger.info(
|
||||||
|
"browser_launch chrome=%s profile_dir=%s args=%s headless=%s",
|
||||||
|
chrome,
|
||||||
|
profile_dir,
|
||||||
|
args,
|
||||||
|
hl,
|
||||||
|
)
|
||||||
|
|
||||||
|
pw = await async_playwright().start()
|
||||||
|
context = None
|
||||||
|
try:
|
||||||
|
context = await pw.chromium.launch_persistent_context(
|
||||||
|
user_data_dir=profile_dir,
|
||||||
|
headless=hl,
|
||||||
|
executable_path=chrome,
|
||||||
|
locale="zh-CN",
|
||||||
|
no_viewport=True,
|
||||||
|
args=args,
|
||||||
|
ignore_default_args=["--enable-automation"],
|
||||||
|
)
|
||||||
|
await context.add_init_script(STEALTH_INIT_SCRIPT)
|
||||||
|
|
||||||
|
page = context.pages[0] if context.pages else await context.new_page()
|
||||||
|
page.set_default_timeout(DEFAULT_PAGE_TIMEOUT_MS)
|
||||||
|
return pw, context, page
|
||||||
|
except Exception:
|
||||||
|
if context is not None:
|
||||||
|
try:
|
||||||
|
await context.close()
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
await pw.stop()
|
||||||
|
raise
|
||||||
|
|
||||||
|
|
||||||
|
async def close_browser_session(playwright: Any, context: Any) -> None:
|
||||||
|
try:
|
||||||
|
if context is not None:
|
||||||
|
await context.close()
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
try:
|
||||||
|
if playwright is not None:
|
||||||
|
await playwright.stop()
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
@@ -0,0 +1,400 @@
|
|||||||
|
"""仿真批量提交 RPA 主流程(async Playwright)。"""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import asyncio
|
||||||
|
import logging
|
||||||
|
import os
|
||||||
|
import random
|
||||||
|
import re
|
||||||
|
import time
|
||||||
|
from dataclasses import dataclass
|
||||||
|
from typing import Any, List, Optional
|
||||||
|
|
||||||
|
from jiangchang_skill_core import config
|
||||||
|
|
||||||
|
from service.adapter.base import BatchItem, BatchSubmitResult
|
||||||
|
from service.browser_session import (
|
||||||
|
close_browser_session,
|
||||||
|
find_chrome_executable,
|
||||||
|
start_browser_session,
|
||||||
|
)
|
||||||
|
from util.constants import (
|
||||||
|
DEFAULT_DEMO_CAPTCHA,
|
||||||
|
DEFAULT_DEMO_LOGIN_ID,
|
||||||
|
DEFAULT_DEMO_PASSWORD,
|
||||||
|
DEFAULT_DEMO_TOKEN_PIN,
|
||||||
|
DEFAULT_TIMEOUT_MS,
|
||||||
|
ENV_SIMULATOR_PASSWORD,
|
||||||
|
ENV_SIMULATOR_TOKEN_PIN,
|
||||||
|
LOGIN_NAVIGATE_TIMEOUT_MS,
|
||||||
|
SUBMIT_NAVIGATE_TIMEOUT_MS,
|
||||||
|
portal_login_wait_sec,
|
||||||
|
)
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
GOTO_TIMEOUT_MS = 60_000
|
||||||
|
|
||||||
|
_PAUSE_MIN_MS = 500
|
||||||
|
_PAUSE_MAX_MS = 2000
|
||||||
|
|
||||||
|
|
||||||
|
class RpaError(Exception):
|
||||||
|
def __init__(self, message: str, screenshot_path: Optional[str] = None):
|
||||||
|
super().__init__(message)
|
||||||
|
self.screenshot_path = screenshot_path
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class _Credentials:
|
||||||
|
login_id: str
|
||||||
|
password: str
|
||||||
|
token_pin: str
|
||||||
|
|
||||||
|
|
||||||
|
def _resolve_credentials(account: dict[str, Any]) -> _Credentials:
|
||||||
|
login_id = str(
|
||||||
|
account.get("login_id") or account.get("username") or DEFAULT_DEMO_LOGIN_ID
|
||||||
|
).strip()
|
||||||
|
password = str(
|
||||||
|
account.get("password")
|
||||||
|
or os.getenv(ENV_SIMULATOR_PASSWORD)
|
||||||
|
or config.get(ENV_SIMULATOR_PASSWORD)
|
||||||
|
or DEFAULT_DEMO_PASSWORD
|
||||||
|
).strip()
|
||||||
|
token_pin = str(
|
||||||
|
account.get("token_pin")
|
||||||
|
or account.get("pin")
|
||||||
|
or os.getenv(ENV_SIMULATOR_TOKEN_PIN)
|
||||||
|
or config.get(ENV_SIMULATOR_TOKEN_PIN)
|
||||||
|
or DEFAULT_DEMO_TOKEN_PIN
|
||||||
|
).strip()
|
||||||
|
return _Credentials(login_id=login_id, password=password, token_pin=token_pin)
|
||||||
|
|
||||||
|
|
||||||
|
def _headless() -> bool:
|
||||||
|
return config.get_bool("OPENCLAW_BROWSER_HEADLESS", default=False)
|
||||||
|
|
||||||
|
|
||||||
|
async def _pause(min_ms: int = _PAUSE_MIN_MS, max_ms: int = _PAUSE_MAX_MS) -> None:
|
||||||
|
await asyncio.sleep(random.uniform(min_ms, max_ms) / 1000.0)
|
||||||
|
|
||||||
|
|
||||||
|
async def _visible(locator) -> bool:
|
||||||
|
try:
|
||||||
|
return await locator.count() > 0 and await locator.is_visible(timeout=500)
|
||||||
|
except Exception:
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
async def _wait_portal_gate(page, wait_sec: int) -> None:
|
||||||
|
portal_user = page.locator("#portal-user")
|
||||||
|
try:
|
||||||
|
if not await _visible(portal_user):
|
||||||
|
return
|
||||||
|
except Exception:
|
||||||
|
return
|
||||||
|
|
||||||
|
print(f"[门户] 检测到平台门户登录门闩,请手工完成门户登录,最多等待 {wait_sec} 秒...")
|
||||||
|
deadline = time.monotonic() + wait_sec
|
||||||
|
while time.monotonic() < deadline:
|
||||||
|
try:
|
||||||
|
portal_section = page.locator("#portal-section")
|
||||||
|
login_step1 = page.locator('form[name="simLoginStep1"]')
|
||||||
|
if not await _visible(portal_section) or await _visible(login_step1):
|
||||||
|
print("[门户] 门户门闩已通过,进入业务系统登录")
|
||||||
|
return
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
await asyncio.sleep(2.0)
|
||||||
|
|
||||||
|
raise RpaError("ERROR:LOGIN_TIMEOUT 门户登录超时,请重新运行并及时完成门户登录。")
|
||||||
|
|
||||||
|
|
||||||
|
async def _login(page, creds: _Credentials, artifacts_dir: str | None) -> None:
|
||||||
|
try:
|
||||||
|
await page.wait_for_selector(
|
||||||
|
'form[name="simLoginStep1"]', timeout=LOGIN_NAVIGATE_TIMEOUT_MS
|
||||||
|
)
|
||||||
|
await _pause()
|
||||||
|
await page.fill('form[name="simLoginStep1"] input[name="loginId"]', creds.login_id)
|
||||||
|
await _pause()
|
||||||
|
await page.click('form[name="simLoginStep1"] button[type="submit"]')
|
||||||
|
except Exception as exc:
|
||||||
|
sp = await _safe_screenshot(page, artifacts_dir, "login_step1_failed")
|
||||||
|
raise RpaError(f"登录第一步失败:{exc}", screenshot_path=sp) from exc
|
||||||
|
|
||||||
|
try:
|
||||||
|
await page.wait_for_selector('form[name="simLoginStep2"]', timeout=DEFAULT_TIMEOUT_MS)
|
||||||
|
await _pause()
|
||||||
|
await page.fill('form[name="simLoginStep2"] input[name="password"]', creds.password)
|
||||||
|
await page.fill('form[name="simLoginStep2"] input[name="captcha"]', DEFAULT_DEMO_CAPTCHA)
|
||||||
|
await _pause()
|
||||||
|
await page.click('form[name="simLoginStep2"] button[type="submit"]')
|
||||||
|
except Exception as exc:
|
||||||
|
sp = await _safe_screenshot(page, artifacts_dir, "login_step2_failed")
|
||||||
|
raise RpaError(f"登录第二步失败:{exc}", screenshot_path=sp) from exc
|
||||||
|
|
||||||
|
try:
|
||||||
|
await page.wait_for_selector('form[name="simBatchForm"]', timeout=LOGIN_NAVIGATE_TIMEOUT_MS)
|
||||||
|
except Exception as exc:
|
||||||
|
sp = await _safe_screenshot(page, artifacts_dir, "login_navigate_failed")
|
||||||
|
raise RpaError(f"登录后未进入批量提交页:{exc}", screenshot_path=sp) from exc
|
||||||
|
|
||||||
|
|
||||||
|
async def _goto_batch_page(page, artifacts_dir: str | None) -> None:
|
||||||
|
try:
|
||||||
|
await page.wait_for_selector('form[name="simBatchForm"]', timeout=DEFAULT_TIMEOUT_MS)
|
||||||
|
await _pause()
|
||||||
|
except Exception as exc:
|
||||||
|
sp = await _safe_screenshot(page, artifacts_dir, "goto_batch_failed")
|
||||||
|
raise RpaError(f"进入批量提交页失败:{exc}", screenshot_path=sp) from exc
|
||||||
|
|
||||||
|
|
||||||
|
async def _select_target_account(page, target: str) -> None:
|
||||||
|
sel = page.locator('select[name="fromAccountNo"]')
|
||||||
|
await sel.wait_for(timeout=DEFAULT_TIMEOUT_MS)
|
||||||
|
n = await sel.locator("option").count()
|
||||||
|
matched_value: Optional[str] = None
|
||||||
|
first_value: Optional[str] = None
|
||||||
|
for i in range(n):
|
||||||
|
opt = sel.locator("option").nth(i)
|
||||||
|
val = (await opt.get_attribute("value")) or ""
|
||||||
|
if not val.strip():
|
||||||
|
continue
|
||||||
|
label_txt = await opt.inner_text()
|
||||||
|
if first_value is None:
|
||||||
|
first_value = val
|
||||||
|
if target.strip() and target.strip() in label_txt:
|
||||||
|
matched_value = val
|
||||||
|
break
|
||||||
|
pick = matched_value or first_value
|
||||||
|
if not pick:
|
||||||
|
raise RpaError("来源账户下拉无可选项")
|
||||||
|
if not matched_value and target.strip():
|
||||||
|
logger.warning("target '%s' 未匹配选项,回退 value=%s", target, pick)
|
||||||
|
await sel.select_option(value=pick)
|
||||||
|
await _pause()
|
||||||
|
|
||||||
|
|
||||||
|
async def _fill_batch_form(
|
||||||
|
page, target: str, items: List[BatchItem], artifacts_dir: str | None
|
||||||
|
) -> None:
|
||||||
|
try:
|
||||||
|
await page.get_by_role("button", name=re.compile(r"页面填写")).click()
|
||||||
|
except Exception as exc:
|
||||||
|
sp = await _safe_screenshot(page, artifacts_dir, "tab_switch_failed")
|
||||||
|
raise RpaError(f"切换到页面填写失败:{exc}", screenshot_path=sp) from exc
|
||||||
|
|
||||||
|
try:
|
||||||
|
await _select_target_account(page, target)
|
||||||
|
except RpaError:
|
||||||
|
raise
|
||||||
|
except Exception as exc:
|
||||||
|
sp = await _safe_screenshot(page, artifacts_dir, "select_account_failed")
|
||||||
|
raise RpaError(f"选择来源账户失败:{exc}", screenshot_path=sp) from exc
|
||||||
|
|
||||||
|
try:
|
||||||
|
existing = await page.locator("tr[data-row-index]").count()
|
||||||
|
need_more = max(0, len(items) - existing)
|
||||||
|
add_btn = page.get_by_role("button", name=re.compile(r"新增一行"))
|
||||||
|
for _ in range(need_more):
|
||||||
|
await _pause(400, 900)
|
||||||
|
await add_btn.click()
|
||||||
|
except Exception as exc:
|
||||||
|
sp = await _safe_screenshot(page, artifacts_dir, "add_rows_failed")
|
||||||
|
raise RpaError(f"新增明细行失败:{exc}", screenshot_path=sp) from exc
|
||||||
|
|
||||||
|
for it in items:
|
||||||
|
try:
|
||||||
|
row = page.locator(f'tr[data-row-index="{it.row_index}"]')
|
||||||
|
await row.wait_for(timeout=DEFAULT_TIMEOUT_MS)
|
||||||
|
await _pause(300, 800)
|
||||||
|
await row.locator('input[name="name"]').fill(it.name)
|
||||||
|
await row.locator('input[name="account"]').fill(it.account)
|
||||||
|
await row.locator('input[name="note"]').fill(it.note or "")
|
||||||
|
await row.locator('input[name="amount"]').fill(f"{it.amount}")
|
||||||
|
except Exception as exc:
|
||||||
|
sp = await _safe_screenshot(page, artifacts_dir, f"fill_row_{it.row_index}_failed")
|
||||||
|
raise RpaError(f"填第 {it.row_index} 行失败:{exc}", screenshot_path=sp) from exc
|
||||||
|
|
||||||
|
try:
|
||||||
|
await page.fill('input[name="purpose"]', "仿真批量提交示例")
|
||||||
|
except Exception as exc:
|
||||||
|
logger.warning("填写用途失败(非致命):%s", exc)
|
||||||
|
|
||||||
|
|
||||||
|
async def _submit_and_get_batch_id(page, token_pin: str, artifacts_dir: str | None) -> str:
|
||||||
|
try:
|
||||||
|
await _pause(800, 1500)
|
||||||
|
await page.locator('form[name="simBatchForm"] button[type="submit"]').click()
|
||||||
|
except Exception as exc:
|
||||||
|
sp = await _safe_screenshot(page, artifacts_dir, "click_submit_failed")
|
||||||
|
raise RpaError(f"点击提交失败:{exc}", screenshot_path=sp) from exc
|
||||||
|
|
||||||
|
dlg_sel = 'div[role="dialog"][aria-modal="true"]'
|
||||||
|
try:
|
||||||
|
await page.wait_for_selector(dlg_sel, timeout=DEFAULT_TIMEOUT_MS)
|
||||||
|
except Exception as exc:
|
||||||
|
sp = await _safe_screenshot(page, artifacts_dir, "pin_dialog_not_open")
|
||||||
|
raise RpaError(f"PIN 弹窗未出现:{exc}", screenshot_path=sp) from exc
|
||||||
|
|
||||||
|
try:
|
||||||
|
pin_input = page.locator(f'{dlg_sel} input[type="password"][inputmode="numeric"]')
|
||||||
|
await _pause(500, 1200)
|
||||||
|
await pin_input.fill(token_pin)
|
||||||
|
except Exception as exc:
|
||||||
|
sp = await _safe_screenshot(page, artifacts_dir, "fill_pin_failed")
|
||||||
|
raise RpaError(f"填写 PIN 失败:{exc}", screenshot_path=sp) from exc
|
||||||
|
|
||||||
|
try:
|
||||||
|
await page.locator(dlg_sel).get_by_role("button", name=re.compile(r"确认提交")).click()
|
||||||
|
except Exception as exc:
|
||||||
|
sp = await _safe_screenshot(page, artifacts_dir, "click_confirm_failed")
|
||||||
|
raise RpaError(f"确认提交失败:{exc}", screenshot_path=sp) from exc
|
||||||
|
|
||||||
|
try:
|
||||||
|
await page.wait_for_selector('[data-testid="batch-id"]', timeout=SUBMIT_NAVIGATE_TIMEOUT_MS)
|
||||||
|
except Exception as exc:
|
||||||
|
sp = await _safe_screenshot(page, artifacts_dir, "success_area_not_visible")
|
||||||
|
raise RpaError(f"提交后未出现成功区域:{exc}", screenshot_path=sp) from exc
|
||||||
|
|
||||||
|
batch_id = await page.locator('[data-testid="batch-id"]').get_attribute("data-batch-id") or ""
|
||||||
|
if not batch_id:
|
||||||
|
batch_id = (await page.locator('[data-testid="batch-id"]').inner_text()).strip()
|
||||||
|
url = page.url
|
||||||
|
if not batch_id and "#/batch/" in url:
|
||||||
|
batch_id = url.split("#/batch/", 1)[1].split("?")[0].split("#")[0].strip("/")
|
||||||
|
if not batch_id:
|
||||||
|
sp = await _safe_screenshot(page, artifacts_dir, "batch_id_empty")
|
||||||
|
raise RpaError(f"无法解析 batch_id,url={url}", screenshot_path=sp)
|
||||||
|
return batch_id
|
||||||
|
|
||||||
|
|
||||||
|
async def _safe_screenshot(page, artifacts_dir: str | None, tag: str) -> Optional[str]:
|
||||||
|
if not artifacts_dir:
|
||||||
|
return None
|
||||||
|
try:
|
||||||
|
os.makedirs(artifacts_dir, exist_ok=True)
|
||||||
|
path = os.path.join(artifacts_dir, f"{tag}_{int(time.time())}.png")
|
||||||
|
await page.screenshot(path=path, full_page=True)
|
||||||
|
logger.info("screenshot_saved path=%s", path)
|
||||||
|
return path
|
||||||
|
except Exception as exc:
|
||||||
|
logger.warning("screenshot_failed tag=%s err=%s", tag, exc)
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
async def submit_batch_rpa(
|
||||||
|
account: dict[str, Any],
|
||||||
|
*,
|
||||||
|
target: str,
|
||||||
|
items: list[BatchItem],
|
||||||
|
base_url: str,
|
||||||
|
artifacts_dir: str | None,
|
||||||
|
video_session: Any = None,
|
||||||
|
headless: bool | None = None,
|
||||||
|
) -> BatchSubmitResult:
|
||||||
|
del video_session # 示例占位;真实 skill 可接入 RpaVideoSession
|
||||||
|
|
||||||
|
profile_dir = str(account.get("profile_dir") or "").strip()
|
||||||
|
if not profile_dir:
|
||||||
|
return BatchSubmitResult(
|
||||||
|
ok=False,
|
||||||
|
batch_id=None,
|
||||||
|
submitted_count=0,
|
||||||
|
submitted_amount=0.0,
|
||||||
|
error_msg="账号缺少 profile_dir",
|
||||||
|
artifacts={"adapter": "simulator_rpa"},
|
||||||
|
)
|
||||||
|
|
||||||
|
if not items:
|
||||||
|
return BatchSubmitResult(
|
||||||
|
ok=False,
|
||||||
|
batch_id=None,
|
||||||
|
submitted_count=0,
|
||||||
|
submitted_amount=0.0,
|
||||||
|
error_msg="items 为空",
|
||||||
|
artifacts={"adapter": "simulator_rpa"},
|
||||||
|
)
|
||||||
|
|
||||||
|
if not find_chrome_executable():
|
||||||
|
return BatchSubmitResult(
|
||||||
|
ok=False,
|
||||||
|
batch_id=None,
|
||||||
|
submitted_count=0,
|
||||||
|
submitted_amount=0.0,
|
||||||
|
error_msg="未检测到 Chrome 或 Edge 浏览器,请安装系统浏览器后重试。",
|
||||||
|
artifacts={"adapter": "simulator_rpa"},
|
||||||
|
)
|
||||||
|
|
||||||
|
creds = _resolve_credentials(account)
|
||||||
|
portal_wait = portal_login_wait_sec()
|
||||||
|
hl = headless if headless is not None else _headless()
|
||||||
|
|
||||||
|
pw = None
|
||||||
|
context = None
|
||||||
|
page = None
|
||||||
|
try:
|
||||||
|
pw, context, page = await start_browser_session(profile_dir, headless=hl)
|
||||||
|
logger.info("rpa_goto url=%s", base_url)
|
||||||
|
await page.goto(base_url, wait_until="domcontentloaded", timeout=GOTO_TIMEOUT_MS)
|
||||||
|
|
||||||
|
await _wait_portal_gate(page, portal_wait)
|
||||||
|
logger.info("rpa_login_start")
|
||||||
|
await _login(page, creds, artifacts_dir)
|
||||||
|
logger.info("rpa_login_done")
|
||||||
|
|
||||||
|
await _goto_batch_page(page, artifacts_dir)
|
||||||
|
logger.info("rpa_batch_page_ready")
|
||||||
|
|
||||||
|
await _fill_batch_form(page, target, items, artifacts_dir)
|
||||||
|
logger.info("rpa_batch_form_filled rows=%s", len(items))
|
||||||
|
|
||||||
|
batch_id = await _submit_and_get_batch_id(page, creds.token_pin, artifacts_dir)
|
||||||
|
logger.info("rpa_submit_success batch_id=%s", batch_id)
|
||||||
|
|
||||||
|
total = sum(it.amount for it in items)
|
||||||
|
return BatchSubmitResult(
|
||||||
|
ok=True,
|
||||||
|
batch_id=batch_id,
|
||||||
|
submitted_count=len(items),
|
||||||
|
submitted_amount=total,
|
||||||
|
error_msg=None,
|
||||||
|
artifacts={"adapter": "simulator_rpa"},
|
||||||
|
)
|
||||||
|
except RpaError as exc:
|
||||||
|
logger.error("rpa_failed: %s", exc)
|
||||||
|
arts = {"adapter": "simulator_rpa"}
|
||||||
|
if exc.screenshot_path:
|
||||||
|
arts["screenshot"] = exc.screenshot_path
|
||||||
|
elif page is not None:
|
||||||
|
sp = await _safe_screenshot(page, artifacts_dir, "rpa_error")
|
||||||
|
if sp:
|
||||||
|
arts["screenshot"] = sp
|
||||||
|
return BatchSubmitResult(
|
||||||
|
ok=False,
|
||||||
|
batch_id=None,
|
||||||
|
submitted_count=0,
|
||||||
|
submitted_amount=0.0,
|
||||||
|
error_msg=str(exc),
|
||||||
|
artifacts=arts,
|
||||||
|
)
|
||||||
|
except Exception as exc:
|
||||||
|
logger.exception("rpa_unexpected: %s", exc)
|
||||||
|
arts = {"adapter": "simulator_rpa"}
|
||||||
|
if page is not None:
|
||||||
|
sp = await _safe_screenshot(page, artifacts_dir, "unexpected_error")
|
||||||
|
if sp:
|
||||||
|
arts["screenshot"] = sp
|
||||||
|
return BatchSubmitResult(
|
||||||
|
ok=False,
|
||||||
|
batch_id=None,
|
||||||
|
submitted_count=0,
|
||||||
|
submitted_amount=0.0,
|
||||||
|
error_msg=f"未预期异常:{type(exc).__name__}: {exc}",
|
||||||
|
artifacts=arts,
|
||||||
|
)
|
||||||
|
finally:
|
||||||
|
await close_browser_session(pw, context)
|
||||||
100
examples/simulator_browser_rpa/scripts/service/task_service.py
Normal file
100
examples/simulator_browser_rpa/scripts/service/task_service.py
Normal file
@@ -0,0 +1,100 @@
|
|||||||
|
"""轻量任务编排:校验输入 → 选 adapter → 提交批次。"""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import os
|
||||||
|
import tempfile
|
||||||
|
import uuid
|
||||||
|
from typing import Any, Dict, List, Optional
|
||||||
|
|
||||||
|
from service.adapter import BatchItem, select_adapter
|
||||||
|
from util.logging import get_logger
|
||||||
|
|
||||||
|
logger = get_logger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
def _validate_items(items: List[Dict[str, Any]]) -> tuple[List[BatchItem], Optional[str]]:
|
||||||
|
if not items:
|
||||||
|
return [], "items 不能为空"
|
||||||
|
parsed: List[BatchItem] = []
|
||||||
|
for idx, raw in enumerate(items, start=1):
|
||||||
|
name = str(raw.get("name") or "").strip()
|
||||||
|
account = str(raw.get("account") or "").strip()
|
||||||
|
note = str(raw.get("note") or "").strip()
|
||||||
|
try:
|
||||||
|
amount = float(raw.get("amount"))
|
||||||
|
except (TypeError, ValueError):
|
||||||
|
return [], f"第 {idx} 行 amount 无效"
|
||||||
|
if amount <= 0:
|
||||||
|
return [], f"第 {idx} 行 amount 必须大于 0"
|
||||||
|
if not name or not account:
|
||||||
|
return [], f"第 {idx} 行 name/account 不能为空"
|
||||||
|
parsed.append(
|
||||||
|
BatchItem(
|
||||||
|
row_index=int(raw.get("row_index") or idx),
|
||||||
|
name=name,
|
||||||
|
account=account,
|
||||||
|
amount=amount,
|
||||||
|
note=note,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
return parsed, None
|
||||||
|
|
||||||
|
|
||||||
|
async def run_batch_submit(
|
||||||
|
target: str,
|
||||||
|
items: List[Dict[str, Any]],
|
||||||
|
*,
|
||||||
|
force: bool = False,
|
||||||
|
artifacts_dir: Optional[str] = None,
|
||||||
|
) -> Dict[str, Any]:
|
||||||
|
"""
|
||||||
|
最小编排示例。
|
||||||
|
|
||||||
|
真实 skill 中还应:写 task_logs、entitlement、RpaVideoSession 等。
|
||||||
|
账号 pick / lease 在 adapter 内完成,编排层不直接碰 account-manager。
|
||||||
|
"""
|
||||||
|
del force # 示例占位;真实 skill 可用于跳过确认
|
||||||
|
|
||||||
|
target_key = (target or "").strip()
|
||||||
|
if not target_key:
|
||||||
|
return {"success": False, "error": {"code": "INVALID_TARGET", "message": "target 不能为空"}}
|
||||||
|
|
||||||
|
batch_items, err = _validate_items(items)
|
||||||
|
if err:
|
||||||
|
return {"success": False, "error": {"code": "INVALID_ITEMS", "message": err}}
|
||||||
|
|
||||||
|
art_dir = artifacts_dir or os.path.join(
|
||||||
|
tempfile.gettempdir(),
|
||||||
|
"openclaw-simulator-rpa-artifacts",
|
||||||
|
uuid.uuid4().hex[:8],
|
||||||
|
)
|
||||||
|
os.makedirs(art_dir, exist_ok=True)
|
||||||
|
logger.info(
|
||||||
|
"batch_submit_start target=%s item_count=%s artifacts_dir=%s",
|
||||||
|
target_key,
|
||||||
|
len(batch_items),
|
||||||
|
art_dir,
|
||||||
|
)
|
||||||
|
|
||||||
|
adapter = select_adapter(artifacts_dir=art_dir)
|
||||||
|
result = await adapter.submit_batch(target_key, batch_items)
|
||||||
|
summary: Dict[str, Any] = {
|
||||||
|
"success": result.ok,
|
||||||
|
"target": target_key,
|
||||||
|
"adapter": result.artifacts.get("adapter") or adapter.name,
|
||||||
|
"submitted_count": result.submitted_count,
|
||||||
|
"submitted_amount": result.submitted_amount,
|
||||||
|
"batch_id": result.batch_id,
|
||||||
|
"artifacts": result.artifacts,
|
||||||
|
"artifacts_dir": art_dir,
|
||||||
|
}
|
||||||
|
if not result.ok:
|
||||||
|
summary["error"] = {"code": "BATCH_SUBMIT_FAILED", "message": result.error_msg or "提交失败"}
|
||||||
|
logger.info(
|
||||||
|
"batch_submit_done success=%s batch_id=%s adapter=%s",
|
||||||
|
result.ok,
|
||||||
|
result.batch_id,
|
||||||
|
adapter.name,
|
||||||
|
)
|
||||||
|
return summary
|
||||||
52
examples/simulator_browser_rpa/scripts/util/constants.py
Normal file
52
examples/simulator_browser_rpa/scripts/util/constants.py
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
"""仿真浏览器 RPA 示例常量。"""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import os
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
from jiangchang_skill_core import config
|
||||||
|
|
||||||
|
SKILL_SLUG = "example-simulator-browser-rpa"
|
||||||
|
LOG_LOGGER_NAME = "openclaw.skill.example_simulator_browser_rpa"
|
||||||
|
|
||||||
|
TARGET_PLATFORM = "sim_batch"
|
||||||
|
LEASE_HOLDER = "example-simulator-browser-rpa"
|
||||||
|
LEASE_TTL_SEC = "1800"
|
||||||
|
|
||||||
|
DEFAULT_TIMEOUT_MS = 15_000
|
||||||
|
LOGIN_NAVIGATE_TIMEOUT_MS = 30_000
|
||||||
|
SUBMIT_NAVIGATE_TIMEOUT_MS = 60_000
|
||||||
|
DEFAULT_PORTAL_LOGIN_WAIT_SEC = 120
|
||||||
|
|
||||||
|
DEFAULT_DEMO_LOGIN_ID = "demo001"
|
||||||
|
DEFAULT_DEMO_PASSWORD = "demo-password"
|
||||||
|
DEFAULT_DEMO_CAPTCHA = "0000"
|
||||||
|
DEFAULT_DEMO_TOKEN_PIN = "123456"
|
||||||
|
|
||||||
|
ENV_SIMULATOR_BASE_URL = "SIMULATOR_BASE_URL"
|
||||||
|
ENV_SIMULATOR_PASSWORD = "SIMULATOR_PASSWORD"
|
||||||
|
ENV_SIMULATOR_TOKEN_PIN = "SIMULATOR_TOKEN_PIN"
|
||||||
|
ENV_PORTAL_LOGIN_WAIT_SEC = "PORTAL_LOGIN_WAIT_SEC"
|
||||||
|
|
||||||
|
_EXAMPLE_ROOT = Path(__file__).resolve().parents[2]
|
||||||
|
DEFAULT_DEMO_APP_PATH = _EXAMPLE_ROOT / "sandbox" / "demo_app.html"
|
||||||
|
|
||||||
|
|
||||||
|
def portal_login_wait_sec() -> int:
|
||||||
|
raw = (
|
||||||
|
os.getenv(ENV_PORTAL_LOGIN_WAIT_SEC)
|
||||||
|
or config.get(ENV_PORTAL_LOGIN_WAIT_SEC)
|
||||||
|
or str(DEFAULT_PORTAL_LOGIN_WAIT_SEC)
|
||||||
|
)
|
||||||
|
try:
|
||||||
|
return max(1, int(str(raw).strip()))
|
||||||
|
except (TypeError, ValueError):
|
||||||
|
return DEFAULT_PORTAL_LOGIN_WAIT_SEC
|
||||||
|
|
||||||
|
|
||||||
|
def resolve_simulator_base_url() -> str:
|
||||||
|
env = (os.getenv(ENV_SIMULATOR_BASE_URL) or "").strip()
|
||||||
|
if env:
|
||||||
|
return env.rstrip("/")
|
||||||
|
return DEFAULT_DEMO_APP_PATH.as_uri()
|
||||||
38
examples/simulator_browser_rpa/scripts/util/logging.py
Normal file
38
examples/simulator_browser_rpa/scripts/util/logging.py
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
"""示例日志工具。"""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import logging
|
||||||
|
import re
|
||||||
|
from typing import Any
|
||||||
|
|
||||||
|
from util.constants import LOG_LOGGER_NAME
|
||||||
|
|
||||||
|
|
||||||
|
def get_logger(name: str | None = None) -> logging.Logger:
|
||||||
|
return logging.getLogger(name or LOG_LOGGER_NAME)
|
||||||
|
|
||||||
|
|
||||||
|
def configure_logging(level: int = logging.INFO) -> None:
|
||||||
|
logging.basicConfig(level=level, format="%(levelname)s %(name)s %(message)s")
|
||||||
|
|
||||||
|
|
||||||
|
def mask_text(value: str, *, keep_start: int = 2, keep_end: int = 2) -> str:
|
||||||
|
s = (value or "").strip()
|
||||||
|
if len(s) >= 11 and s.isdigit():
|
||||||
|
return s[:3] + "****" + s[-4:]
|
||||||
|
if len(s) > keep_start + keep_end:
|
||||||
|
return s[:keep_start] + "****" + s[-keep_end:]
|
||||||
|
return "****"
|
||||||
|
|
||||||
|
|
||||||
|
def safe_log_value(value: Any) -> str:
|
||||||
|
if value is None:
|
||||||
|
return ""
|
||||||
|
text = str(value)
|
||||||
|
if re.fullmatch(r"\d{7,}", text):
|
||||||
|
return mask_text(text)
|
||||||
|
if "@" in text and len(text) > 6:
|
||||||
|
local, _, domain = text.partition("@")
|
||||||
|
return f"{mask_text(local, keep_start=1, keep_end=1)}@{domain}"
|
||||||
|
return text
|
||||||
0
examples/simulator_browser_rpa/tests/.gitkeep
Normal file
0
examples/simulator_browser_rpa/tests/.gitkeep
Normal file
10
examples/simulator_browser_rpa/tests/conftest.py
Normal file
10
examples/simulator_browser_rpa/tests/conftest.py
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
"""pytest 路径:将 examples/simulator_browser_rpa/scripts 加入 import 路径。"""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import sys
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
SCRIPTS = Path(__file__).resolve().parents[1] / "scripts"
|
||||||
|
if str(SCRIPTS) not in sys.path:
|
||||||
|
sys.path.insert(0, str(SCRIPTS))
|
||||||
@@ -0,0 +1,64 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
"""adapter 工厂 dispatch 单元测试。"""
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import os
|
||||||
|
import unittest
|
||||||
|
from unittest.mock import patch
|
||||||
|
|
||||||
|
from service.adapter import (
|
||||||
|
BatchItem,
|
||||||
|
MockBatchAdapter,
|
||||||
|
SimulatorBrowserRpaAdapter,
|
||||||
|
select_adapter,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class TestAdapterDispatch(unittest.TestCase):
|
||||||
|
def test_default_returns_simulator_rpa(self) -> None:
|
||||||
|
with patch.dict(os.environ, {}, clear=False):
|
||||||
|
os.environ.pop("OPENCLAW_TEST_TARGET", None)
|
||||||
|
adapter = select_adapter()
|
||||||
|
self.assertIsInstance(adapter, SimulatorBrowserRpaAdapter)
|
||||||
|
|
||||||
|
def test_unit_returns_mock(self) -> None:
|
||||||
|
with patch.dict(os.environ, {"OPENCLAW_TEST_TARGET": "unit"}):
|
||||||
|
self.assertIsInstance(select_adapter(), MockBatchAdapter)
|
||||||
|
|
||||||
|
def test_mock_returns_mock(self) -> None:
|
||||||
|
with patch.dict(os.environ, {"OPENCLAW_TEST_TARGET": "mock"}):
|
||||||
|
self.assertIsInstance(select_adapter(), MockBatchAdapter)
|
||||||
|
|
||||||
|
def test_simulator_rpa_returns_simulator(self) -> None:
|
||||||
|
with patch.dict(os.environ, {"OPENCLAW_TEST_TARGET": "simulator_rpa"}):
|
||||||
|
self.assertIsInstance(select_adapter(), SimulatorBrowserRpaAdapter)
|
||||||
|
|
||||||
|
def test_real_rpa_falls_back_to_mock(self) -> None:
|
||||||
|
with patch.dict(os.environ, {"OPENCLAW_TEST_TARGET": "real_rpa"}):
|
||||||
|
self.assertIsInstance(select_adapter(), MockBatchAdapter)
|
||||||
|
|
||||||
|
def test_unknown_target_falls_back_simulator_rpa(self) -> None:
|
||||||
|
with patch.dict(os.environ, {"OPENCLAW_TEST_TARGET": "simulator_api"}):
|
||||||
|
self.assertIsInstance(select_adapter(), SimulatorBrowserRpaAdapter)
|
||||||
|
|
||||||
|
|
||||||
|
class TestMockAdapter(unittest.IsolatedAsyncioTestCase):
|
||||||
|
async def test_mock_success(self) -> None:
|
||||||
|
items = [
|
||||||
|
BatchItem(row_index=1, name="Alice", account="1001", amount=10.5),
|
||||||
|
BatchItem(row_index=2, name="Bob", account="1002", amount=20.0),
|
||||||
|
]
|
||||||
|
result = await MockBatchAdapter().submit_batch("acct-demo-001", items)
|
||||||
|
self.assertTrue(result.ok)
|
||||||
|
self.assertEqual(result.submitted_count, 2)
|
||||||
|
self.assertEqual(result.submitted_amount, 30.5)
|
||||||
|
self.assertTrue((result.batch_id or "").startswith("MOCK-"))
|
||||||
|
|
||||||
|
async def test_mock_empty_items(self) -> None:
|
||||||
|
result = await MockBatchAdapter().submit_batch("acct-demo-001", [])
|
||||||
|
self.assertFalse(result.ok)
|
||||||
|
self.assertEqual(result.error_msg, "items 为空")
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
unittest.main()
|
||||||
@@ -0,0 +1,39 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
"""禁止 import account-manager 内部 RPA 辅助模块。"""
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import os
|
||||||
|
import re
|
||||||
|
import unittest
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
FORBIDDEN_PATTERNS = (
|
||||||
|
re.compile(r"\brpa_helpers\b"),
|
||||||
|
re.compile(r"\binject_account_manager_scripts_path\b"),
|
||||||
|
re.compile(r"\bget_account_credential\b"),
|
||||||
|
)
|
||||||
|
|
||||||
|
SCRIPTS_ROOT = Path(__file__).resolve().parents[1] / "scripts"
|
||||||
|
|
||||||
|
|
||||||
|
class TestNoRpaHelpersImport(unittest.TestCase):
|
||||||
|
def test_scripts_tree_has_no_forbidden_account_manager_imports(self) -> None:
|
||||||
|
violations: list[str] = []
|
||||||
|
for root, _dirs, files in os.walk(SCRIPTS_ROOT):
|
||||||
|
for name in files:
|
||||||
|
if not name.endswith(".py"):
|
||||||
|
continue
|
||||||
|
path = Path(root) / name
|
||||||
|
text = path.read_text(encoding="utf-8")
|
||||||
|
for pattern in FORBIDDEN_PATTERNS:
|
||||||
|
if pattern.search(text):
|
||||||
|
violations.append(f"{path.relative_to(SCRIPTS_ROOT)}: {pattern.pattern}")
|
||||||
|
self.assertEqual(
|
||||||
|
violations,
|
||||||
|
[],
|
||||||
|
msg="Forbidden account-manager internal imports found:\n" + "\n".join(violations),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
unittest.main()
|
||||||
134
examples/simulator_browser_rpa/tests/test_simulator_rpa_unit.py
Normal file
134
examples/simulator_browser_rpa/tests/test_simulator_rpa_unit.py
Normal file
@@ -0,0 +1,134 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
"""SimulatorBrowserRpaAdapter 单元测试(不启动真实浏览器)。"""
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import asyncio
|
||||||
|
import unittest
|
||||||
|
from unittest.mock import AsyncMock, MagicMock, patch
|
||||||
|
|
||||||
|
from service.adapter import BatchItem, SimulatorBrowserRpaAdapter
|
||||||
|
from service.browser_session import CHROME_LAUNCH_ARGS
|
||||||
|
from service.simulator_playwright import RpaError
|
||||||
|
|
||||||
|
|
||||||
|
class TestSimulatorRpaUnit(unittest.IsolatedAsyncioTestCase):
|
||||||
|
async def test_submit_batch_empty_items(self) -> None:
|
||||||
|
adapter = SimulatorBrowserRpaAdapter()
|
||||||
|
result = await adapter.submit_batch("acct-demo-001", [])
|
||||||
|
self.assertFalse(result.ok)
|
||||||
|
self.assertIn("items", result.error_msg or "")
|
||||||
|
|
||||||
|
@patch("service.adapter.simulator_rpa.find_chrome_executable", return_value=None)
|
||||||
|
async def test_no_chrome_returns_error_without_launch(self, _chrome: MagicMock) -> None:
|
||||||
|
adapter = SimulatorBrowserRpaAdapter()
|
||||||
|
items = [BatchItem(row_index=1, name="A", account="1", amount=1.0)]
|
||||||
|
result = await adapter.submit_batch("acct-demo-001", items)
|
||||||
|
self.assertFalse(result.ok)
|
||||||
|
msg = result.error_msg or ""
|
||||||
|
self.assertTrue("Chrome" in msg or "Edge" in msg or "浏览器" in msg)
|
||||||
|
|
||||||
|
def test_chrome_launch_args_must_not_contain_url(self) -> None:
|
||||||
|
for arg in CHROME_LAUNCH_ARGS:
|
||||||
|
lowered = arg.lower()
|
||||||
|
self.assertFalse(lowered.startswith("http://"))
|
||||||
|
self.assertFalse(lowered.startswith("https://"))
|
||||||
|
self.assertFalse(lowered.startswith("file://"))
|
||||||
|
|
||||||
|
@patch("service.adapter.simulator_rpa.submit_batch_rpa", new_callable=AsyncMock)
|
||||||
|
@patch("service.adapter.simulator_rpa.pick_web_account")
|
||||||
|
@patch("service.adapter.simulator_rpa.find_chrome_executable", return_value="C:/Chrome/chrome.exe")
|
||||||
|
async def test_adapter_delegates_to_playwright_with_account_url(
|
||||||
|
self,
|
||||||
|
_chrome: MagicMock,
|
||||||
|
mock_pick: MagicMock,
|
||||||
|
mock_rpa: AsyncMock,
|
||||||
|
) -> None:
|
||||||
|
from service.adapter.base import BatchSubmitResult
|
||||||
|
|
||||||
|
mock_pick.return_value = {
|
||||||
|
"login_id": "demo001",
|
||||||
|
"profile_dir": "/tmp/profile",
|
||||||
|
"url": "file:///demo/demo_app.html",
|
||||||
|
"lease_token": "tok-1",
|
||||||
|
}
|
||||||
|
mock_rpa.return_value = BatchSubmitResult(
|
||||||
|
ok=False,
|
||||||
|
batch_id=None,
|
||||||
|
submitted_count=0,
|
||||||
|
submitted_amount=0.0,
|
||||||
|
error_msg="login fail",
|
||||||
|
artifacts={"adapter": "simulator_rpa"},
|
||||||
|
)
|
||||||
|
|
||||||
|
adapter = SimulatorBrowserRpaAdapter(artifacts_dir="/tmp/art")
|
||||||
|
items = [BatchItem(row_index=1, name="A", account="1", amount=1.0)]
|
||||||
|
result = await adapter.submit_batch("acct-demo-001", items)
|
||||||
|
|
||||||
|
self.assertFalse(result.ok)
|
||||||
|
mock_rpa.assert_awaited_once()
|
||||||
|
call_kwargs = mock_rpa.call_args.kwargs
|
||||||
|
self.assertEqual(call_kwargs["base_url"], "file:///demo/demo_app.html")
|
||||||
|
for arg in CHROME_LAUNCH_ARGS:
|
||||||
|
self.assertNotIn(arg, call_kwargs["base_url"])
|
||||||
|
|
||||||
|
@patch("service.simulator_playwright.start_browser_session", new_callable=AsyncMock)
|
||||||
|
async def test_playwright_opens_url_via_goto_not_launch_args(self, mock_start: AsyncMock) -> None:
|
||||||
|
from service.simulator_playwright import submit_batch_rpa
|
||||||
|
|
||||||
|
page = AsyncMock()
|
||||||
|
page.goto = AsyncMock()
|
||||||
|
mock_start.return_value = (MagicMock(), MagicMock(), page)
|
||||||
|
|
||||||
|
account = {"profile_dir": "/tmp/p", "login_id": "demo001"}
|
||||||
|
items = [BatchItem(row_index=1, name="A", account="1", amount=1.0)]
|
||||||
|
|
||||||
|
with patch(
|
||||||
|
"service.simulator_playwright._wait_portal_gate",
|
||||||
|
new_callable=AsyncMock,
|
||||||
|
return_value=None,
|
||||||
|
):
|
||||||
|
with patch(
|
||||||
|
"service.simulator_playwright._login",
|
||||||
|
new_callable=AsyncMock,
|
||||||
|
side_effect=RpaError("login fail"),
|
||||||
|
):
|
||||||
|
with patch(
|
||||||
|
"service.simulator_playwright.find_chrome_executable",
|
||||||
|
return_value="C:/Chrome/chrome.exe",
|
||||||
|
):
|
||||||
|
with patch(
|
||||||
|
"service.simulator_playwright.close_browser_session",
|
||||||
|
new_callable=AsyncMock,
|
||||||
|
):
|
||||||
|
result = await submit_batch_rpa(
|
||||||
|
account,
|
||||||
|
target="acct-demo-001",
|
||||||
|
items=items,
|
||||||
|
base_url="file:///demo/demo_app.html",
|
||||||
|
artifacts_dir=None,
|
||||||
|
)
|
||||||
|
|
||||||
|
self.assertFalse(result.ok)
|
||||||
|
page.goto.assert_awaited_once()
|
||||||
|
goto_url = page.goto.call_args[0][0]
|
||||||
|
self.assertEqual(goto_url, "file:///demo/demo_app.html")
|
||||||
|
for arg in CHROME_LAUNCH_ARGS:
|
||||||
|
self.assertNotIn(arg, goto_url)
|
||||||
|
|
||||||
|
async def test_safe_screenshot_without_artifacts_dir(self) -> None:
|
||||||
|
from service.simulator_playwright import _safe_screenshot
|
||||||
|
|
||||||
|
page = AsyncMock()
|
||||||
|
self.assertIsNone(await _safe_screenshot(page, None, "tag"))
|
||||||
|
|
||||||
|
|
||||||
|
class TestSyncHelpers(unittest.TestCase):
|
||||||
|
def test_asyncio_run_smoke(self) -> None:
|
||||||
|
async def _noop() -> bool:
|
||||||
|
return True
|
||||||
|
|
||||||
|
self.assertTrue(asyncio.run(_noop()))
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
unittest.main()
|
||||||
74
examples/simulator_browser_rpa/tests/test_stop_reason.py
Normal file
74
examples/simulator_browser_rpa/tests/test_stop_reason.py
Normal file
@@ -0,0 +1,74 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
"""RPA 失败结果与 artifacts 单元测试。"""
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import unittest
|
||||||
|
from unittest.mock import AsyncMock, MagicMock, patch
|
||||||
|
|
||||||
|
from service.adapter import BatchItem, SimulatorBrowserRpaAdapter
|
||||||
|
from service.adapter.base import BatchSubmitResult
|
||||||
|
|
||||||
|
|
||||||
|
class TestBatchSubmitFailureResult(unittest.IsolatedAsyncioTestCase):
|
||||||
|
@patch("service.adapter.simulator_rpa.release_lease")
|
||||||
|
@patch("service.adapter.simulator_rpa.submit_batch_rpa", new_callable=AsyncMock)
|
||||||
|
@patch("service.adapter.simulator_rpa.pick_web_account")
|
||||||
|
@patch("service.adapter.simulator_rpa.find_chrome_executable", return_value="C:/Chrome/chrome.exe")
|
||||||
|
async def test_rpa_error_converts_to_failed_result(
|
||||||
|
self,
|
||||||
|
_chrome: MagicMock,
|
||||||
|
mock_pick: MagicMock,
|
||||||
|
mock_rpa: AsyncMock,
|
||||||
|
_release: MagicMock,
|
||||||
|
) -> None:
|
||||||
|
mock_pick.return_value = {
|
||||||
|
"profile_dir": "/tmp/profile",
|
||||||
|
"lease_token": "tok",
|
||||||
|
}
|
||||||
|
mock_rpa.return_value = BatchSubmitResult(
|
||||||
|
ok=False,
|
||||||
|
batch_id=None,
|
||||||
|
submitted_count=0,
|
||||||
|
submitted_amount=0.0,
|
||||||
|
error_msg="登录第一步失败:timeout",
|
||||||
|
artifacts={"adapter": "simulator_rpa", "screenshot": "/tmp/art/x.png"},
|
||||||
|
)
|
||||||
|
|
||||||
|
adapter = SimulatorBrowserRpaAdapter(artifacts_dir="/tmp/art")
|
||||||
|
items = [BatchItem(row_index=1, name="A", account="1", amount=1.0)]
|
||||||
|
result = await adapter.submit_batch("acct-demo-001", items)
|
||||||
|
|
||||||
|
self.assertFalse(result.ok)
|
||||||
|
self.assertIn("登录第一步失败", result.error_msg or "")
|
||||||
|
self.assertEqual(result.artifacts.get("adapter"), "simulator_rpa")
|
||||||
|
self.assertEqual(result.artifacts.get("screenshot"), "/tmp/art/x.png")
|
||||||
|
|
||||||
|
@patch("service.adapter.simulator_rpa.release_lease")
|
||||||
|
@patch("service.adapter.simulator_rpa.submit_batch_rpa", new_callable=AsyncMock)
|
||||||
|
@patch("service.adapter.simulator_rpa.pick_web_account")
|
||||||
|
@patch("service.adapter.simulator_rpa.find_chrome_executable", return_value="C:/Chrome/chrome.exe")
|
||||||
|
async def test_unexpected_error_includes_adapter_name(
|
||||||
|
self,
|
||||||
|
_chrome: MagicMock,
|
||||||
|
mock_pick: MagicMock,
|
||||||
|
mock_rpa: AsyncMock,
|
||||||
|
_release: MagicMock,
|
||||||
|
) -> None:
|
||||||
|
mock_pick.return_value = {
|
||||||
|
"profile_dir": "/tmp/profile",
|
||||||
|
"lease_token": "tok",
|
||||||
|
}
|
||||||
|
mock_rpa.side_effect = RuntimeError("boom")
|
||||||
|
|
||||||
|
adapter = SimulatorBrowserRpaAdapter(artifacts_dir=None)
|
||||||
|
items = [BatchItem(row_index=1, name="A", account="1", amount=1.0)]
|
||||||
|
result = await adapter.submit_batch("acct-demo-001", items)
|
||||||
|
|
||||||
|
self.assertFalse(result.ok)
|
||||||
|
self.assertIn("未预期异常", result.error_msg or "")
|
||||||
|
self.assertEqual(result.artifacts.get("adapter"), "simulator_rpa")
|
||||||
|
self.assertNotIn("screenshot", result.artifacts)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
unittest.main()
|
||||||
242
references/ACTIONS.md
Normal file
242
references/ACTIONS.md
Normal file
@@ -0,0 +1,242 @@
|
|||||||
|
# Skill Action Manifest(匠厂宿主)
|
||||||
|
|
||||||
|
本文件说明 `assets/actions.json` 与 `scripts/main.py` CLI 的对应关系,供 Agent 编排与宿主 **Skill Action Runtime** 使用。
|
||||||
|
|
||||||
|
**权威运行时契约(sync/async、任务中心、编排范式)见 [`../development/SKILL_ACTION_RUNTIME.md`](../development/SKILL_ACTION_RUNTIME.md)。**
|
||||||
|
|
||||||
|
**边界:** 用户市场说明见根目录 [`README.md`](../README.md);CLI 参数与错误码细节见 [`CLI.md`](CLI.md)。
|
||||||
|
|
||||||
|
## 模板严格规范 vs 宿主向后兼容
|
||||||
|
|
||||||
|
| 角色 | 定位 |
|
||||||
|
|------|------|
|
||||||
|
| **skill-template** | 新技能推荐的**严格规范**:必填字段、明确类型、`additionalProperties: false` |
|
||||||
|
| **宿主运行时** | **向后兼容**的历史 manifest 解析器:可能接受部分缺省字段(如 `description`、`placements`、`entrypoint.args`、input property `type`) |
|
||||||
|
|
||||||
|
**新技能不应依赖宿主缺省行为。** 复制模板后按 [`assets/schemas/skill-actions.schema.json`](../assets/schemas/skill-actions.schema.json) 与本文完整填写 manifest。
|
||||||
|
|
||||||
|
JSON Schema 使用 `additionalProperties: false` 表达模板严格边界;这与宿主共享类型中的可选字段并不矛盾——宿主为兼容旧技能可能更宽松,模板为防漂移要求更严。
|
||||||
|
|
||||||
|
## Action 是否必需
|
||||||
|
|
||||||
|
Action 是**可选能力**:
|
||||||
|
|
||||||
|
- 没有宿主直接操作需求的技能,**可以不提供** `actions.json`。
|
||||||
|
- 需要出现在**数据管理按钮**、**技能详情**、**定时任务**或 **Agent 直接调用**中的技能,**必须**提供 `assets/actions.json`。
|
||||||
|
- **含浏览器 RPA / 长耗时任务**的技能:**必须**提供至少一条 `executionProfile: "async"` 且 `placements` 含 `agent` 的 action(见 `POLICY-SKILL-ACTION-001`)。
|
||||||
|
|
||||||
|
## 文件位置
|
||||||
|
|
||||||
|
```text
|
||||||
|
{skill_root}/assets/actions.json
|
||||||
|
```
|
||||||
|
|
||||||
|
宿主扫描路径示例:
|
||||||
|
|
||||||
|
```text
|
||||||
|
~/.openclaw/skills/your-skill-slug/assets/actions.json
|
||||||
|
```
|
||||||
|
|
||||||
|
## 顶层结构
|
||||||
|
|
||||||
|
| 字段 | 必填 | 说明 |
|
||||||
|
|------|------|------|
|
||||||
|
| `schemaVersion` | 是 | 当前固定为 `1` |
|
||||||
|
| `skill` | 是 | 必须与 `SKILL.md` 的 `metadata.openclaw.slug` 一致 |
|
||||||
|
| `actions` | 是 | Action 数组,每项 `id` 唯一 |
|
||||||
|
|
||||||
|
模板最小示例见 [`assets/actions.json`](../assets/actions.json);JSON Schema 见 [`assets/schemas/skill-actions.schema.json`](../assets/schemas/skill-actions.schema.json)。
|
||||||
|
|
||||||
|
## Phase 1 当前稳定支持(新技能应只使用这些)
|
||||||
|
|
||||||
|
| 层级 | 稳定字段 / 能力 |
|
||||||
|
|------|----------------|
|
||||||
|
| 顶层 | `schemaVersion`、`skill`、`actions` |
|
||||||
|
| action | `id`、`label`、`description`、`placements`、`entrypoint`;可选 `inputSchema`、`confirmation`、**`executionProfile`** |
|
||||||
|
| entrypoint | `type = cli`、`command`、`args`(**必填** JSON 数组,项为 `string` / `number` / `boolean`) |
|
||||||
|
| placements | `toolbar`、`skill-detail`、`agent`、`cron` |
|
||||||
|
| inputSchema | 根 `type: object`;properties 使用 `string` / `number` / `boolean`;支持 `enum`、`default`、`required`、`sensitive` |
|
||||||
|
| confirmation | `{ "message": "..." }` |
|
||||||
|
| **executionProfile** | `"sync"` \| `"async"`(**模板要求显式写出**) |
|
||||||
|
|
||||||
|
## 预留能力(当前不可当作已实现能力)
|
||||||
|
|
||||||
|
以下在宿主共享类型或 Schema 枚举中可能出现,但 **Phase 1 Runtime 尚未完整支持**,**新模板示例不得使用**:
|
||||||
|
|
||||||
|
| 类别 | 预留项 | 说明 |
|
||||||
|
|------|--------|------|
|
||||||
|
| placements | `row`、`batch` | 行内 / 批量入口,未来扩展 |
|
||||||
|
| action 字段 | `bind`、`concurrency`、`locks` | 绑定、并发与锁控制,宿主后续实现 |
|
||||||
|
| inputSchema | `array`、nested `object`、`oneOf` 等 | 复杂表单与联合类型,不属于当前推荐模板范围 |
|
||||||
|
|
||||||
|
可以在文档或 Schema 枚举中保留这些值供未来对齐,但不要让技能作者误以为它们已经可执行。
|
||||||
|
|
||||||
|
## 执行模型
|
||||||
|
|
||||||
|
宿主将 action 转为 argv,再执行:
|
||||||
|
|
||||||
|
```text
|
||||||
|
python {skill_root}/scripts/main.py <command> <args...>
|
||||||
|
```
|
||||||
|
|
||||||
|
- `entrypoint.type` 当前只支持 `cli`
|
||||||
|
- `entrypoint.command` → CLI 子命令(须真实存在)
|
||||||
|
- `entrypoint.args` → 子命令参数(**必须为 JSON 数组**)
|
||||||
|
- `{{fieldName}}` → 来自 `inputSchema` 用户输入的简单模板替换
|
||||||
|
|
||||||
|
完整子命令定义见 [`scripts/cli/app.py`](../scripts/cli/app.py)。
|
||||||
|
|
||||||
|
### `executionProfile`(sync / async)
|
||||||
|
|
||||||
|
| 值 | 宿主行为 | 任务中心 |
|
||||||
|
|----|----------|----------|
|
||||||
|
| `sync` | 内联等待 CLI(默认约 30s) | 不进 |
|
||||||
|
| `async` | 后台 Job,立即返回 `jobId` | **进入** |
|
||||||
|
| 省略 | 宿主默认按 **async** | 进入 |
|
||||||
|
|
||||||
|
- **RPA / 浏览器 / 长耗时** → 必须 `"async"`,并建议加 `confirmation`。
|
||||||
|
- **数据管理 toolbar** → 只挂 **async** action。
|
||||||
|
- 完整规则见 [`../development/SKILL_ACTION_RUNTIME.md`](../development/SKILL_ACTION_RUNTIME.md)。
|
||||||
|
|
||||||
|
### CLI 输出契约
|
||||||
|
|
||||||
|
技能 CLI 必须:
|
||||||
|
|
||||||
|
- 使用稳定的退出码
|
||||||
|
- 成功时输出可解析的结构化结果
|
||||||
|
- 失败时输出稳定错误信息
|
||||||
|
- 对可预期错误提供稳定错误码
|
||||||
|
- 对用户可处理的问题提供 `HINT`
|
||||||
|
- 不依赖 Agent 连续对话才能完成
|
||||||
|
- 不自行实现宿主任务中心
|
||||||
|
- 长时间任务允许异步执行,由宿主任务中心承载状态
|
||||||
|
- 不在 CLI 中硬编码宿主安装目录
|
||||||
|
- 不直接依赖某个具体 UI 页面
|
||||||
|
|
||||||
|
建议错误输出格式:
|
||||||
|
|
||||||
|
```text
|
||||||
|
ERROR:<CODE>: <message>
|
||||||
|
HINT: <next step>
|
||||||
|
```
|
||||||
|
|
||||||
|
## placements(入口位置)
|
||||||
|
|
||||||
|
| 值 | 含义 |
|
||||||
|
|----|------|
|
||||||
|
| `toolbar` | 数据管理第一排技能按钮(**仅 async**) |
|
||||||
|
| `skill-detail` | 技能详情或技能市场详情页 |
|
||||||
|
| `agent` | Agent 可直接调用(`run_skill_action`) |
|
||||||
|
| `cron` | 定时任务可直接调用 |
|
||||||
|
|
||||||
|
**预留能力(当前宿主可能尚未完整支持,模板示例请勿使用):**
|
||||||
|
|
||||||
|
| 值 | 说明 |
|
||||||
|
|----|------|
|
||||||
|
| `row` | 行内操作(未来扩展) |
|
||||||
|
| `batch` | 批量操作(未来扩展) |
|
||||||
|
|
||||||
|
### Action 类型 × placements 矩阵
|
||||||
|
|
||||||
|
| Action 类型 | executionProfile | 典型 placements | confirmation |
|
||||||
|
|-------------|------------------|-----------------|--------------|
|
||||||
|
| health / version / config-path | sync | skill-detail, agent | 无 |
|
||||||
|
| 统计 / 只读查询 | sync | skill-detail, agent | 无 |
|
||||||
|
| 导入 / 重置 | sync | skill-detail, agent | 可选 |
|
||||||
|
| 单条副作用 RPA | async | agent, skill-detail | **必须** |
|
||||||
|
| 批量顺序 pick RPA | async | **toolbar**, cron, agent | **必须** |
|
||||||
|
|
||||||
|
## inputSchema 与参数表单
|
||||||
|
|
||||||
|
当前宿主稳定支持:
|
||||||
|
|
||||||
|
- `object` / `string` / `number` / `boolean`
|
||||||
|
- `enum` / `default` / `required` / `sensitive`
|
||||||
|
- 简单模板替换(`{{fieldName}}`)
|
||||||
|
|
||||||
|
示例:
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"inputSchema": {
|
||||||
|
"type": "object",
|
||||||
|
"required": ["query"],
|
||||||
|
"properties": {
|
||||||
|
"query": {
|
||||||
|
"type": "string",
|
||||||
|
"title": "查询条件"
|
||||||
|
},
|
||||||
|
"limit": {
|
||||||
|
"type": "number",
|
||||||
|
"title": "返回数量",
|
||||||
|
"default": 50
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
批量 pick 参数约定(有队列表时):
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"pickCount": {
|
||||||
|
"type": "number",
|
||||||
|
"title": "处理数量",
|
||||||
|
"description": "从队列选取 pending 记录条数",
|
||||||
|
"default": 1,
|
||||||
|
"minimum": 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
约定:
|
||||||
|
|
||||||
|
- `boolean` 在宿主中渲染为开关
|
||||||
|
- `enum` 在宿主中渲染为选择框
|
||||||
|
- `required` 字段在提交前校验
|
||||||
|
- `sensitive` 字段不得出现在低信任入口
|
||||||
|
- 需要确认的副作用操作使用 `confirmation.message`
|
||||||
|
- 模板参数使用 `{{fieldName}}`,且必须在 `inputSchema.properties` 中声明
|
||||||
|
- `entrypoint.args` 项当前只允许 `string`、`number`、`boolean`;不允许对象或 `null`
|
||||||
|
- **不要**在当前模板中宣传 `nested object`、`oneOf`、复杂数组等尚未稳定支持的能力
|
||||||
|
|
||||||
|
## 风险与入口配置
|
||||||
|
|
||||||
|
- 查询、诊断、版本检查可以放到 `skill-detail` 或 `agent`
|
||||||
|
- 低风险、常用动作才考虑放 `toolbar`(且须为 async)
|
||||||
|
- 会修改本地数据、访问外部系统、执行长时间任务的动作必须谨慎配置
|
||||||
|
- 有副作用的动作应添加 `confirmation.message`
|
||||||
|
- 高风险动作不应默认出现在 `toolbar`
|
||||||
|
- 含 `sensitive` 参数的 action 不应放入 `toolbar` 或 `cron`,除非有明确安全设计
|
||||||
|
- 不要为了「按钮多」而暴露所有 CLI 子命令
|
||||||
|
- 一个 CLI 命令可以拆成多个语义清晰的 action,但每个 action 必须有明确语义
|
||||||
|
- **Agent 必须用 `run_skill_action`,禁止 `exec` 直跑长任务 CLI**(见 `SKILL_ACTION_RUNTIME.md`)
|
||||||
|
|
||||||
|
## 模板示例 action 一览
|
||||||
|
|
||||||
|
| id | CLI | executionProfile | placements | 说明 |
|
||||||
|
|----|-----|------------------|------------|------|
|
||||||
|
| `health` | `health` | sync | skill-detail, agent | 运行环境检查 |
|
||||||
|
| `version` | `version` | sync | skill-detail | 版本 JSON |
|
||||||
|
| `config-path` | `config-path` | sync | skill-detail | 配置路径 JSON |
|
||||||
|
|
||||||
|
业务技能复制后,按需增加例如:
|
||||||
|
|
||||||
|
| 模式 | id 示例 | executionProfile | placements |
|
||||||
|
|------|---------|------------------|------------|
|
||||||
|
| 单条 RPA | `your-run` | async | agent, skill-detail |
|
||||||
|
| 队列 pick | `your-run-pick` | async | toolbar, cron, agent |
|
||||||
|
| 统计 | `your-stats` | sync | skill-detail, agent |
|
||||||
|
|
||||||
|
不需要宿主入口时可删除整个 `actions.json`(无 RPA 长任务时)。
|
||||||
|
|
||||||
|
## 与宿主 API(参考)
|
||||||
|
|
||||||
|
| 宿主 API | 用途 |
|
||||||
|
|----------|------|
|
||||||
|
| `GET /api/skill-actions/{skillSlug}` | 读取 manifest |
|
||||||
|
| `POST /api/skill-actions/run` | `{ skillSlug, actionId, input?, source? }` |
|
||||||
|
| `GET /api/skill-actions/jobs` | 任务中心 |
|
||||||
|
|
||||||
|
具体路径与字段以宿主实现为准;技能侧只需保证 manifest 与 CLI 契约稳定。
|
||||||
@@ -16,8 +16,15 @@ python {baseDir}/scripts/main.py version
|
|||||||
- **`health`**:只读 runtime 诊断,**不下载、不修复 media-assets,不执行业务动作**;不输出敏感值。
|
- **`health`**:只读 runtime 诊断,**不下载、不修复 media-assets,不执行业务动作**;不输出敏感值。
|
||||||
- **`config-path`**:输出 JSON,包含 `skill`、`env_path`、`example_path`。
|
- **`config-path`**:输出 JSON,包含 `skill`、`env_path`、`example_path`。
|
||||||
- **`version`**:输出 JSON(`version`、`skill`)。
|
- **`version`**:输出 JSON(`version`、`skill`)。
|
||||||
- **`run`**:长时间无 stdout **不代表卡死**;应通过 `logs` / `log-get` 排查。
|
- **`run`**:长时间无 stdout **不代表卡死**;RPA / 外部调用期间应通过 Activity 或 Run Journal 看进度。
|
||||||
- **任务完成后**若有 video artifact,CLI 应打印录屏路径、录屏日志、视频/音频诊断(见 `task_run_support._print_video_summary`)。
|
- **排查顺序**(由快到慢):
|
||||||
|
1. `python {baseDir}/scripts/main.py logs`
|
||||||
|
2. `python {baseDir}/scripts/main.py log-get <log_id>`
|
||||||
|
3. 统一日志文件 `{JIANGCHANG_DATA_ROOT}/{JIANGCHANG_USER_ID}/logs/jiangchang.log`(含 `trace_id`、`task_failed` 等)
|
||||||
|
4. Run Journal `{JIANGCHANG_DATA_ROOT}/.jiangchang/runs/{job_id}.jsonl`
|
||||||
|
5. RPA artifacts / `video_log` / 失败截图(见 `development/RPA.md`);未开启录屏时可能只有截图或日志
|
||||||
|
- **录屏默认关闭**:模板 `.env.example` 为 `OPENCLAW_RECORD_VIDEO=0`;若未开启录屏,任务**可能不会**打印录屏路径。
|
||||||
|
- 需要录屏时在用户 `.env` 或进程环境变量中设置 **`OPENCLAW_RECORD_VIDEO=1`**;开启后任务完成时 CLI 应打印录屏路径、录屏日志、视频/音频诊断(见 `task_run_support._print_video_summary`),且 `result_summary` 含 video 字段。
|
||||||
|
|
||||||
## config-path(配置路径)
|
## config-path(配置路径)
|
||||||
|
|
||||||
|
|||||||
@@ -1,77 +1,21 @@
|
|||||||
---
|
# Agent 参考索引
|
||||||
description: "用用户能理解的方式说明这个技能能做什么、适合什么场景,以及如何开始使用。"
|
|
||||||
---
|
|
||||||
|
|
||||||
# 【技能名称】
|
本目录只是 Agent **运行、编排、调用** skill 时可渐进加载的资料索引,**不是**用户市场说明。
|
||||||
|
|
||||||
用一两句话说明这个技能帮助用户完成什么工作。
|
**边界规则:**
|
||||||
这里不要解释技术实现,要从用户的业务目标出发描述价值。
|
|
||||||
|
|
||||||
## 这个技能能帮你做什么
|
- 本文**不得**包含 YAML frontmatter,**不得**包含 `description` 字段。
|
||||||
|
- 技能市场详情与普通用户说明**必须**放在根目录 [`README.md`](../README.md)。
|
||||||
|
- 本目录只保留运行时引用资料(如 `CLI.md`、`SCHEMA.md`);开发规范放在 [`../development/`](../development/)。
|
||||||
|
|
||||||
用 3 到 6 条业务能力说明技能价值,例如:
|
| 文档 | 用途 |
|
||||||
|
|------|------|
|
||||||
|
| 根目录 [`README.md`](../README.md) | 用户市场详情页说明 |
|
||||||
|
| [`SKILL.md`](../SKILL.md) | LLM / OpenClaw 平台技能入口 |
|
||||||
|
| [`CLI.md`](CLI.md) | 命令行入口、参数与调用契约 |
|
||||||
|
| [`SCHEMA.md`](SCHEMA.md) | 本地 SQLite 与数据管理展示契约 |
|
||||||
|
| [`ACTIONS.md`](ACTIONS.md) | Skill Action manifest(**可选**能力)与宿主入口契约 |
|
||||||
|
| [`../development/SKILL_ACTION_RUNTIME.md`](../development/SKILL_ACTION_RUNTIME.md) | sync/async、任务中心、Agent 禁令、编排与队列 pick(长任务必读) |
|
||||||
|
| [`../development/`](../development/) | 开发、测试、技术规范(给开发者 / AI 编程代理) |
|
||||||
|
|
||||||
- 【自动整理/查询/生成/同步/核对/提交】某类业务数据
|
按需阅读:编排任务时优先 `CLI.md`;解析结构化字段时读 `SCHEMA.md`;配置宿主按钮/定时任务/Agent 入口时读 `ACTIONS.md` + `SKILL_ACTION_RUNTIME.md`;运行时环境细节见 `../development/RUNTIME.md`。
|
||||||
- 帮你减少重复操作,降低人工录入或核对成本
|
|
||||||
- 在任务完成后给出清晰的处理结果、失败原因或后续建议
|
|
||||||
|
|
||||||
## 适合什么场景
|
|
||||||
|
|
||||||
列出典型使用场景。请使用用户熟悉的业务语言,例如:
|
|
||||||
|
|
||||||
- 每天需要重复处理同一类订单、单据、报表或客户资料
|
|
||||||
- 需要从多个系统中查询信息并汇总结果
|
|
||||||
- 需要按固定规则检查数据是否完整、是否异常
|
|
||||||
- 需要把处理结果保存为文件、记录或任务报告
|
|
||||||
|
|
||||||
## 使用前需要准备什么
|
|
||||||
|
|
||||||
说明用户在使用前要准备的东西,例如:
|
|
||||||
|
|
||||||
- 相关账号已登录,且具备查询、导出或提交权限
|
|
||||||
- 需要处理的文件、时间范围、订单号、客户名称、项目名称等信息
|
|
||||||
- 如果涉及外部系统,请确认网络、账号、验证码或审批流程可正常使用
|
|
||||||
|
|
||||||
## 你可以这样告诉 Agent
|
|
||||||
|
|
||||||
用自然语言示例展示用户可以怎么发起任务,例如:
|
|
||||||
|
|
||||||
- “帮我查询今天的【业务对象】,整理成表格。”
|
|
||||||
- “帮我核对这些【数据/单据】是否有异常,并告诉我问题在哪里。”
|
|
||||||
- “帮我把【时间范围】内的【业务数据】导出并生成处理报告。”
|
|
||||||
- “帮我根据这个文件里的内容,逐条完成【业务动作】。”
|
|
||||||
|
|
||||||
注意:示例应是自然语言,不要写命令行命令。
|
|
||||||
|
|
||||||
## 执行后你会得到什么
|
|
||||||
|
|
||||||
说明用户会看到什么结果,例如:
|
|
||||||
|
|
||||||
- 任务是否成功完成
|
|
||||||
- 处理了多少条数据
|
|
||||||
- 哪些内容成功、哪些失败
|
|
||||||
- 失败原因和建议处理方式
|
|
||||||
- 生成的文件、表格、报告或记录
|
|
||||||
|
|
||||||
## 注意事项
|
|
||||||
|
|
||||||
用用户能理解的方式说明边界和风险,例如:
|
|
||||||
|
|
||||||
- 请确认输入范围准确,避免处理错数据
|
|
||||||
- 涉及提交、付款、删除、审批等高风险动作时,技能应在执行前请求确认
|
|
||||||
- 如果外部系统登录失效、权限不足或页面变化,任务可能需要用户介入
|
|
||||||
- 不建议在未核对数据的情况下直接执行批量操作
|
|
||||||
|
|
||||||
## 常见问题
|
|
||||||
|
|
||||||
### 为什么任务没有查到数据?
|
|
||||||
|
|
||||||
可能是时间范围、关键词、账号权限或系统筛选条件不正确。请先确认输入信息是否准确。
|
|
||||||
|
|
||||||
### 为什么任务执行到一半需要我操作?
|
|
||||||
|
|
||||||
可能遇到了登录、验证码、权限确认、二次审批或系统异常。请根据 Agent 的提示完成必要操作。
|
|
||||||
|
|
||||||
### 结果文件在哪里查看?
|
|
||||||
|
|
||||||
请根据 Agent 返回的结果说明查看生成文件、任务记录或处理报告。
|
|
||||||
|
|||||||
@@ -1,356 +0,0 @@
|
|||||||
# 技能需求文档模板
|
|
||||||
|
|
||||||
这份文档用于给技术人员、产品人员和实施人员统一描述某个 skill 的研发需求。
|
|
||||||
|
|
||||||
建议原则:
|
|
||||||
|
|
||||||
- 一个 skill 对应一份主需求文档
|
|
||||||
- 需求先写清楚,再进入开发
|
|
||||||
- 文档描述“要做什么”和“做到什么程度”,不要在这里堆实现细节
|
|
||||||
- 实现方式、代码结构、发布流程,分别放到 `DEVELOPMENT.md`、`CLI.md`、`SCHEMA.md`、`RUNTIME.md`
|
|
||||||
|
|
||||||
如果你是从 `skill-template` 复制新技能,请复制本文件后,把下面所有占位内容替换成你的真实项目内容。
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
# REQUIREMENTS
|
|
||||||
|
|
||||||
## 1. 文档目标
|
|
||||||
|
|
||||||
说明这份需求文档的作用,以及它解决什么问题。
|
|
||||||
|
|
||||||
模板写法:
|
|
||||||
|
|
||||||
- 本文档用于明确 `【技能名称】` 的研发范围、交付标准、依赖关系和验收要求。
|
|
||||||
- 技术人员应以本文件作为开发范围依据,以避免实现偏差、范围蔓延或理解不一致。
|
|
||||||
|
|
||||||
示例:
|
|
||||||
|
|
||||||
- 本文档用于明确 `your-skill-slug`(示例:`disburse-payroll-icbc`)的研发范围、交付标准、依赖关系和验收要求。
|
|
||||||
|
|
||||||
## 2. 业务背景
|
|
||||||
|
|
||||||
说明为什么要做这个 skill,它服务什么业务场景,解决什么实际问题。
|
|
||||||
|
|
||||||
模板写法:
|
|
||||||
|
|
||||||
- 当前业务场景中,用户需要在 `【目标平台】` 完成 `【业务动作】`。
|
|
||||||
- 现有流程主要依赖人工处理,存在 `【效率低 / 容易出错 / 不可批量 / 不可追踪】` 等问题。
|
|
||||||
- 因此需要开发 `【技能名称】`,将该流程标准化、自动化,并接入匠厂技能体系中。
|
|
||||||
|
|
||||||
示例:
|
|
||||||
|
|
||||||
- 当前财务团队需要将工资代发批次与银行接口对账,人工导出表格、核对差异耗时且易错。
|
|
||||||
- 因此需要开发 `disburse-payroll-icbc`,用于自动拉取代发结果、写入任务日志 `task_logs` 并输出对账摘要,支持审计追踪。
|
|
||||||
|
|
||||||
## 3. 开发目标
|
|
||||||
|
|
||||||
说明本次开发希望最终交付什么结果。
|
|
||||||
|
|
||||||
模板写法:
|
|
||||||
|
|
||||||
- 完成 `【技能名称】` 的标准目录结构搭建
|
|
||||||
- 完成 CLI 入口与基础命令
|
|
||||||
- 完成核心业务流程
|
|
||||||
- 完成与兄弟技能或外部系统的必要集成
|
|
||||||
- 完成正式环境发布验证
|
|
||||||
|
|
||||||
示例:
|
|
||||||
|
|
||||||
- 完成 `your-skill-slug` 的标准 skill 结构建设
|
|
||||||
- 提供 `health`、`version`、`run`、`logs`、`log-get` 等命令
|
|
||||||
- 支持对接银行 / 薪酬系统 API(示例),并记录任务执行结果
|
|
||||||
- 支持写入任务日志 `task_logs`,并可在正式环境中安装验证
|
|
||||||
|
|
||||||
## 4. 功能范围
|
|
||||||
|
|
||||||
说明这次开发明确要实现哪些功能。
|
|
||||||
|
|
||||||
模板写法:
|
|
||||||
|
|
||||||
- 支持 `【命令1】`
|
|
||||||
- 支持 `【命令2】`
|
|
||||||
- 支持读取 `【数据来源】`
|
|
||||||
- 支持执行 `【核心业务动作】`
|
|
||||||
- 支持记录 `【日志 / 状态 / 结果】`
|
|
||||||
|
|
||||||
建议将功能拆成“必须实现”和“可后续扩展”。
|
|
||||||
|
|
||||||
示例:
|
|
||||||
|
|
||||||
### 必须实现
|
|
||||||
|
|
||||||
- 支持 `python scripts/main.py health`
|
|
||||||
- 支持 `python scripts/main.py version`
|
|
||||||
- 支持 `python scripts/main.py run`
|
|
||||||
- 支持从 `some-sibling-skill` 读取批次元数据(如需要)
|
|
||||||
- 支持执行核心业务编排(HTTP / 批处理 / 可选 RPA)
|
|
||||||
- 支持写入任务日志(`task_logs`)
|
|
||||||
|
|
||||||
### 可后续扩展
|
|
||||||
|
|
||||||
- 支持定时批跑
|
|
||||||
- 支持失败自动重试
|
|
||||||
- 支持多目标轮询
|
|
||||||
|
|
||||||
## 5. 非功能要求
|
|
||||||
|
|
||||||
说明除功能外,对稳定性、可维护性、目录规范、日志、编码等方面的要求。
|
|
||||||
|
|
||||||
模板写法:
|
|
||||||
|
|
||||||
- 目录结构必须符合当前 skill 模板规范
|
|
||||||
- 入口必须统一为 `scripts/main.py`
|
|
||||||
- 输出格式应尽量机读友好
|
|
||||||
- 错误前缀统一为 `ERROR:`
|
|
||||||
- Windows 环境下需保证 UTF-8 输出兼容
|
|
||||||
- 必须具备基本日志能力
|
|
||||||
|
|
||||||
示例:
|
|
||||||
|
|
||||||
- 项目结构必须对齐 `skill-template`
|
|
||||||
- CLI 入口统一使用 `scripts/main.py`
|
|
||||||
- 关键执行结果应可通过 JSON 或固定文本结构返回
|
|
||||||
- 任务日志必须可追踪(`task_logs`)
|
|
||||||
- 不允许重新引入旧模板中的 `docs/`、`optional/` 或 `scripts/skill_main.py`
|
|
||||||
|
|
||||||
## 6. 输入输出要求
|
|
||||||
|
|
||||||
说明这个 skill 接收什么输入,产出什么输出。
|
|
||||||
|
|
||||||
模板写法:
|
|
||||||
|
|
||||||
### 输入
|
|
||||||
|
|
||||||
- `【输入参数1】`
|
|
||||||
- `【输入参数2】`
|
|
||||||
- `【依赖系统返回的数据】`
|
|
||||||
|
|
||||||
### 输出
|
|
||||||
|
|
||||||
- `【标准输出】`
|
|
||||||
- `【错误输出】`
|
|
||||||
- `【数据库记录】`
|
|
||||||
- `【日志文件】`
|
|
||||||
|
|
||||||
示例:
|
|
||||||
|
|
||||||
### 输入
|
|
||||||
|
|
||||||
- `target_id`:可选,指定任务目标(账号 / 客户 / 平台)
|
|
||||||
- `input_id`:可选,指定输入批次或记录
|
|
||||||
- 兄弟技能返回的结构化数据(如有)
|
|
||||||
|
|
||||||
### 输出
|
|
||||||
|
|
||||||
- 成功结果(stdout / JSON)
|
|
||||||
- 错误输出(`ERROR:` / `FAIL:`)
|
|
||||||
- 在本地数据库写入 `task_logs`
|
|
||||||
- 在日志目录中写入执行日志
|
|
||||||
|
|
||||||
## 7. 依赖的兄弟技能或外部系统
|
|
||||||
|
|
||||||
说明开发该 skill 时依赖哪些内部技能或外部平台。
|
|
||||||
|
|
||||||
模板写法:
|
|
||||||
|
|
||||||
### 兄弟技能依赖
|
|
||||||
|
|
||||||
- `【skill-a】`:作用是 `【用途】`
|
|
||||||
- `【skill-b】`:作用是 `【用途】`
|
|
||||||
|
|
||||||
### 外部系统依赖
|
|
||||||
|
|
||||||
- `【平台名】`:作用是 `【用途】`
|
|
||||||
- `【浏览器 / API / 第三方服务】`:作用是 `【用途】`
|
|
||||||
|
|
||||||
### Python 包依赖(requirements.txt)
|
|
||||||
|
|
||||||
- 本技能若需**特有** Python 三方包,写入根目录 `requirements.txt`。
|
|
||||||
- `jiangchang-platform-kit`、`playwright` 等公共能力由宿主共享 runtime 提供,**不要**写入技能 requirements。
|
|
||||||
- `SKILL.md` 的 `platform_kit_min_version` 是运行契约/兼容性声明,供宿主校验,**不是** pip 依赖声明。
|
|
||||||
- 匠厂宿主安装/更新技能后,会将技能 requirements 安装到 `{JIANGCHANG_DATA_ROOT}/python-runtime/.venv`。
|
|
||||||
- 版本须尽量收窄(如 `requests>=2.31.0,<3`),避免共享 venv 冲突。
|
|
||||||
- **不要**把系统运行时组件(如 Microsoft Visual C++ Redistributable)写进 requirements.txt。
|
|
||||||
|
|
||||||
### 系统运行时前置条件(preflight / health)
|
|
||||||
|
|
||||||
- 若依赖 Windows 原生 DLL、本机浏览器 channel 等**系统级**能力,在 `health` / preflight 中检测并提示用户自行安装。
|
|
||||||
- 此类依赖**不**通过 pip / requirements.txt 由宿主自动安装。
|
|
||||||
|
|
||||||
示例:
|
|
||||||
|
|
||||||
### 兄弟技能依赖
|
|
||||||
|
|
||||||
- `some-sibling-skill`:提供主数据或凭证索引(按业务填写)
|
|
||||||
|
|
||||||
### 外部系统依赖
|
|
||||||
|
|
||||||
- 银行 / 政务 HTTP API(示例):核心外部接口
|
|
||||||
- Chromium / Chrome / Edge(可选):用于浏览器自动化
|
|
||||||
- Gitea:用于代码托管和发布工作流
|
|
||||||
- 匠厂客户端:用于正式环境安装与验证
|
|
||||||
|
|
||||||
## 8. 不在本次范围内的内容
|
|
||||||
|
|
||||||
这一节非常重要,用来明确本次“不做什么”,避免研发越做越散。
|
|
||||||
|
|
||||||
模板写法:
|
|
||||||
|
|
||||||
- 本次不实现 `【功能A】`
|
|
||||||
- 本次不处理 `【平台B】`
|
|
||||||
- 本次不做 `【高级能力】`
|
|
||||||
- 本次不兼容 `【旧结构 / 旧逻辑】`
|
|
||||||
|
|
||||||
示例:
|
|
||||||
|
|
||||||
- 本次不实现多租户隔离之外的附加报表
|
|
||||||
- 本次不实现实时流式处理
|
|
||||||
- 本次不实现自动重试机制
|
|
||||||
- 本次不兼容旧模板中的历史目录结构
|
|
||||||
|
|
||||||
## 9. 验收标准
|
|
||||||
|
|
||||||
说明什么情况下才算真正开发完成。
|
|
||||||
|
|
||||||
模板写法:
|
|
||||||
|
|
||||||
- 代码结构符合模板规范
|
|
||||||
- 最小命令可运行
|
|
||||||
- 核心命令可运行
|
|
||||||
- 正式环境可安装
|
|
||||||
- 匠厂中可看到最新版本
|
|
||||||
- 可以在新建任务中实际使用
|
|
||||||
|
|
||||||
示例:
|
|
||||||
|
|
||||||
- `health`、`version` 命令执行正常
|
|
||||||
- `run` 主流程可运行
|
|
||||||
- 发布后 Gitea 工作流成功
|
|
||||||
- 匠厂技能市场能看到最新版本
|
|
||||||
- skill 可以正常安装
|
|
||||||
- 安装后可在“新建任务”中调用
|
|
||||||
|
|
||||||
**测试与日志相关验收(所有新 skill 必须满足)**:
|
|
||||||
|
|
||||||
- `python tests/run_tests.py -v` 必跑测试全部通过
|
|
||||||
- 任务日志(task_logs)写入和查询符合预期
|
|
||||||
- 真实联调(如有)已在 tests/integration/ 中验证,且默认套件不包含真实外联
|
|
||||||
|
|
||||||
## 10. 开发注意事项
|
|
||||||
|
|
||||||
说明研发过程中必须特别注意的事项。
|
|
||||||
|
|
||||||
模板写法:
|
|
||||||
|
|
||||||
- 不要修改无关 skill
|
|
||||||
- 不要破坏现有模板规范
|
|
||||||
- 优先遵守目录和分层约定
|
|
||||||
- 不要在 CLI 层堆业务逻辑
|
|
||||||
- 发布前必须先做本地最小验证
|
|
||||||
|
|
||||||
示例:
|
|
||||||
|
|
||||||
- 只允许修改当前 skill 仓库,不要改动其他兄弟项目
|
|
||||||
- 如使用浏览器自动化,优先复用已验证过的定位器和流程
|
|
||||||
- `cli` 只做参数解析,核心逻辑统一放到 `service`
|
|
||||||
- 发布前必须完成本地验证、工作流验证和正式环境安装验证
|
|
||||||
|
|
||||||
## 11. 变更记录
|
|
||||||
|
|
||||||
这一节用于记录需求文档本身的变化,不是 git 提交记录的替代,而是给技术人员看“需求范围怎么变了”。
|
|
||||||
|
|
||||||
模板写法:
|
|
||||||
|
|
||||||
| 日期 | 版本 | 变更人 | 变更内容 |
|
|
||||||
|------|------|--------|----------|
|
|
||||||
| 2026-04-13 | v1.0 | 张三 | 初版需求文档 |
|
|
||||||
| 2026-04-14 | v1.1 | 李四 | 增加正式环境验收要求 |
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## 建议使用方式
|
|
||||||
|
|
||||||
建议每个新 skill 开发时,按下面顺序使用文档:
|
|
||||||
|
|
||||||
1. 先写 `references/REQUIREMENTS.md`
|
|
||||||
2. 再按 `references/DEVELOPMENT.md` 进入开发
|
|
||||||
3. 开发过程中补充 `CLI.md`、`SCHEMA.md`、`RUNTIME.md`、`TESTING.md`
|
|
||||||
4. 发布前回到 `REQUIREMENTS.md` 对照验收标准逐项检查
|
|
||||||
|
|
||||||
## 最小模板示例
|
|
||||||
|
|
||||||
下面给出一个可直接参考的简化版示例:
|
|
||||||
|
|
||||||
```md
|
|
||||||
# REQUIREMENTS
|
|
||||||
|
|
||||||
## 1. 文档目标
|
|
||||||
|
|
||||||
本文档用于明确 `your-skill-slug` 的研发需求、范围和验收标准,作为开发与测试的统一依据。
|
|
||||||
|
|
||||||
## 2. 业务背景
|
|
||||||
|
|
||||||
采购审计需要将政府采购公告结构化归档;人工复制粘贴效率低,需要可追溯的任务日志。
|
|
||||||
|
|
||||||
## 3. 开发目标
|
|
||||||
|
|
||||||
- 完成 `your-skill-slug` 的标准 skill 结构
|
|
||||||
- 支持核心业务批处理 / HTTP 对接主流程
|
|
||||||
- 支持任务日志记录(task_logs)
|
|
||||||
|
|
||||||
## 4. 功能范围
|
|
||||||
|
|
||||||
- 支持 `health`
|
|
||||||
- 支持 `version`
|
|
||||||
- 支持 `run`
|
|
||||||
- 支持查询任务日志(logs / log-get)
|
|
||||||
|
|
||||||
## 5. 非功能要求
|
|
||||||
|
|
||||||
- 入口统一为 `scripts/main.py`
|
|
||||||
- 保持 UTF-8 输出
|
|
||||||
- 结构符合 skill 模板规范
|
|
||||||
|
|
||||||
## 6. 输入输出要求
|
|
||||||
|
|
||||||
### 输入
|
|
||||||
|
|
||||||
- `target_id`
|
|
||||||
- `input_id`
|
|
||||||
|
|
||||||
### 输出
|
|
||||||
|
|
||||||
- 成功结果
|
|
||||||
- 错误信息
|
|
||||||
- task_logs 记录
|
|
||||||
|
|
||||||
## 7. 依赖的兄弟技能或外部系统
|
|
||||||
|
|
||||||
- (按需填写兄弟 skill)
|
|
||||||
- 政务公告站点 HTTP API(示例)
|
|
||||||
|
|
||||||
## 8. 不在本次范围内的内容
|
|
||||||
|
|
||||||
- 不实现浏览器可视化回放(如需另行立项)
|
|
||||||
- 不实现自动重试
|
|
||||||
|
|
||||||
## 9. 验收标准
|
|
||||||
|
|
||||||
- 命令可运行
|
|
||||||
- `python tests/run_tests.py -v` 全部通过
|
|
||||||
- task_logs 写入符合 SCHEMA 约定
|
|
||||||
- 能完成正式环境安装验证
|
|
||||||
- 能在新建任务中使用
|
|
||||||
|
|
||||||
## 10. 开发注意事项
|
|
||||||
|
|
||||||
- 不修改无关项目
|
|
||||||
- 不引入旧模板结构
|
|
||||||
|
|
||||||
## 11. 变更记录
|
|
||||||
|
|
||||||
| 日期 | 版本 | 变更人 | 变更内容 |
|
|
||||||
|------|------|--------|----------|
|
|
||||||
| 2026-04-13 | v1.0 | 张三 | 初版 |
|
|
||||||
```
|
|
||||||
@@ -1,38 +1,408 @@
|
|||||||
# 数据存储模板
|
# 数据存储与数据管理展示规范
|
||||||
|
|
||||||
## 数据库路径
|
本文是 skill 本地 SQLite 与匠厂宿主「数据管理」页面展示契约的**权威说明**。
|
||||||
|
实现上的 DDL 与默认中文元数据种子见 `scripts/db/display_metadata.py`;`scripts/db/connection.py` 的 `init_db()` 会一并初始化。
|
||||||
|
|
||||||
`{DATA_ROOT}/{USER_ID}/your-skill-slug/your-skill-slug.db`
|
## 职责边界
|
||||||
|
|
||||||
## 通用任务日志表(task_logs)
|
### 技能负责
|
||||||
|
|
||||||
模板默认建表:
|
- 定义真实 SQLite 表结构
|
||||||
|
- 使用英文 `snake_case` 表名和字段名
|
||||||
|
- 使用 `_jiangchang_tables` 提供表级展示元数据
|
||||||
|
- 使用 `_jiangchang_columns` 提供字段级展示元数据
|
||||||
|
- 定义哪些字段可见、可搜索、可创建、可编辑
|
||||||
|
- 定义字段展示类型和枚举选项
|
||||||
|
- 保证元数据与真实数据库结构一致
|
||||||
|
- 保证元数据初始化幂等
|
||||||
|
|
||||||
| 字段 | 类型 | 说明 |
|
### 宿主负责
|
||||||
|
|
||||||
|
- 读取 `_jiangchang_*` 元数据
|
||||||
|
- 读取 SQLite `PRAGMA table_info`
|
||||||
|
- 根据 schema 渲染数据管理页面
|
||||||
|
- 根据 `editable` 推导字段是否可创建或编辑
|
||||||
|
- 执行通用查询、新增、编辑、删除、导入、导出
|
||||||
|
- 对字段名、表名、类型和权限进行最终校验
|
||||||
|
- 不猜测技能业务含义
|
||||||
|
- 不根据具体技能名称写特殊逻辑
|
||||||
|
|
||||||
|
SQLite **没有**可靠的 `COMMENT ON TABLE/COLUMN`;SQL 文件里的 `-- 注释` 也不会成为可查询结构。
|
||||||
|
因此中文展示名称必须写入元数据表;字段顺序必须写在 `CREATE TABLE` 的列定义顺序中(`PRAGMA table_info` 的 `cid`)。
|
||||||
|
|
||||||
|
> **禁止**新技能使用宿主的旧版 `_schema_meta` 表。仅 `_jiangchang_tables` / `_jiangchang_columns` 为正式契约。
|
||||||
|
|
||||||
|
## 技能名称(SKILL.md)
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
---
|
||||||
|
name: 中文业务名称
|
||||||
|
description: 中文技能说明
|
||||||
|
metadata:
|
||||||
|
openclaw:
|
||||||
|
slug: english-kebab-slug
|
||||||
|
---
|
||||||
|
```
|
||||||
|
|
||||||
|
- `name`:用户可见的中文业务名称。
|
||||||
|
- `description`:能力说明,不是技能名称。
|
||||||
|
- `metadata.openclaw.slug`:机器标识,kebab-case 英文;目录名与默认数据库文件名使用 slug。slug 语义规范见 [`../development/NAMING.md`](../development/NAMING.md)。
|
||||||
|
- 复制模板后不得将 `your-skill-slug`、`My Skill` 等占位内容作为正式技能发布名称。
|
||||||
|
|
||||||
|
## 数据库路径与命名
|
||||||
|
|
||||||
|
```text
|
||||||
|
{JIANGCHANG_DATA_ROOT}/{JIANGCHANG_USER_ID}/{skill-slug}/{skill-slug}.db
|
||||||
|
```
|
||||||
|
|
||||||
|
- 数据库文件:英文 slug,例如 `your-skill-slug.db`(默认 `{skill-slug}.db`)。
|
||||||
|
- 业务表、字段:**英文 snake_case** 物理名,例如 `demo_items`、`task_logs`、`created_at`。
|
||||||
|
- **禁止**用中文作为 SQLite 真实表名或字段名。
|
||||||
|
|
||||||
|
## 外部来源只读与本地数据可写
|
||||||
|
|
||||||
|
某些技能会从外部系统读取数据并写入技能自己的本地数据库。需要区分:
|
||||||
|
|
||||||
|
```text
|
||||||
|
外部来源数据库:
|
||||||
|
可能只读,不应被修改
|
||||||
|
|
||||||
|
技能本地数据库:
|
||||||
|
可以根据业务语义允许新增、编辑或删除
|
||||||
|
```
|
||||||
|
|
||||||
|
不要把「外部来源只读」错误理解成「技能本地数据库一定只读」。技能通过 `_jiangchang_tables.readonly` 与 `_jiangchang_columns.editable` 声明本地表的实际写权限。
|
||||||
|
|
||||||
|
## 元数据表(宿主兼容)
|
||||||
|
|
||||||
|
宿主读取字段(与匠厂 `metadata-resolver` 一致):
|
||||||
|
|
||||||
|
### `_jiangchang_tables` 表级标准
|
||||||
|
|
||||||
|
| 字段 | 必填 | 说明 |
|
||||||
|------|------|------|
|
|------|------|------|
|
||||||
| `id` | INTEGER PK | 自增主键 |
|
| `table_name` | 是 | 真实英文表名 |
|
||||||
| `task_type` | TEXT NOT NULL | 任务类型(disburse / reconcile / verify ...) |
|
| `display_name` | 是 | 用户可见的中文业务名称 |
|
||||||
| `target_id` | TEXT | 任务目标(账号、平台、客户等的 ID) |
|
| `description` | 否 | 表说明 |
|
||||||
| `input_id` | TEXT | 输入对象 ID |
|
| `sort_order` | 否 | 兼容保留字段,不用于替代物理字段顺序 |
|
||||||
| `input_title` | TEXT | 输入对象标题快照 |
|
| `visible` | 是 | `1` 在宿主数据管理中展示 / `0` 隐藏 |
|
||||||
| `status` | TEXT NOT NULL | success / failed / partial / require_login 等 |
|
| `readonly` | 是 | `1` 整张表只读;`0` 允许宿主根据字段权限执行写操作 |
|
||||||
| `error_msg` | TEXT | 错误说明 |
|
|
||||||
| `result_summary` | TEXT | 结果摘要(建议存 JSON 字符串) |
|
|
||||||
| `created_at` | INTEGER | Unix 时间戳 |
|
|
||||||
| `updated_at` | INTEGER | Unix 时间戳 |
|
|
||||||
|
|
||||||
## 不同业务的字段映射建议
|
禁止:
|
||||||
|
|
||||||
| 业务场景 | task_type | target_id 含义 | input_id 含义 |
|
- 使用中文真实表名
|
||||||
|---|---|---|---|
|
- 使用 `_schema_meta` 作为新的正式契约
|
||||||
| 发布类 | publish | 账号 ID | 文章 ID |
|
- 只写英文物理名而不写中文 `display_name`
|
||||||
| 工资代发 | disburse | 付款账户 | 工资表批次 ID |
|
- 让元数据引用不存在的表
|
||||||
| 对账 | reconcile | 银行 / 平台 | 对账批次 ID |
|
|
||||||
|
### `_jiangchang_columns` 字段级标准
|
||||||
|
|
||||||
|
| 字段 | 必填 | 说明 |
|
||||||
|
|------|------|------|
|
||||||
|
| `table_name` | 是 | 所属物理表 |
|
||||||
|
| `column_name` | 是 | 真实英文字段名 |
|
||||||
|
| `display_name` | 是 | 用户可见的中文字段名 |
|
||||||
|
| `description` | 否 | 字段说明 |
|
||||||
|
| `display_order` | 否 | 兼容字段,默认保持 `NULL` |
|
||||||
|
| `visible` | 是 | 是否在数据管理表格中展示 |
|
||||||
|
| `searchable` | 是 | 是否参与搜索 |
|
||||||
|
| `editable` | 是 | `1` 业务字段允许宿主用于新增和编辑;`0` 系统/只读字段 |
|
||||||
|
| `display_type` | 否 | 字段渲染类型(如 `text`、`textarea`、`datetime_unix_seconds`) |
|
||||||
|
| `options_json` | 否 | 枚举选项 JSON |
|
||||||
|
|
||||||
|
这两张表是**数据字典**,不是业务数据;宿主不会把它们当作普通业务表展示。
|
||||||
|
|
||||||
|
## editable 与 createable 的关系
|
||||||
|
|
||||||
|
技能模板只维护 `editable` 这一份字段权限声明:
|
||||||
|
|
||||||
|
```text
|
||||||
|
skill metadata:
|
||||||
|
editable
|
||||||
|
|
||||||
|
host runtime:
|
||||||
|
createable / editable
|
||||||
|
```
|
||||||
|
|
||||||
|
宿主根据以下因素计算字段是否能够出现在新增或编辑表单中:
|
||||||
|
|
||||||
|
- 表级 `readonly`
|
||||||
|
- 字段 `editable`
|
||||||
|
- 主键
|
||||||
|
- generated 字段
|
||||||
|
- sensitive 字段
|
||||||
|
- BLOB 类型
|
||||||
|
- not-null 约束
|
||||||
|
- default value
|
||||||
|
|
||||||
|
通用规则:
|
||||||
|
|
||||||
|
```text
|
||||||
|
新增表单:
|
||||||
|
editable = 1
|
||||||
|
且不是主键
|
||||||
|
且不是 generated
|
||||||
|
且不是敏感字段
|
||||||
|
且不是 BLOB
|
||||||
|
|
||||||
|
编辑表单:
|
||||||
|
editable = 1
|
||||||
|
且不是主键
|
||||||
|
且不是 generated
|
||||||
|
且不是敏感字段
|
||||||
|
且不是 BLOB
|
||||||
|
```
|
||||||
|
|
||||||
|
若一张表:
|
||||||
|
|
||||||
|
```text
|
||||||
|
readonly = 0
|
||||||
|
但所有字段 editable = 0
|
||||||
|
```
|
||||||
|
|
||||||
|
则:
|
||||||
|
|
||||||
|
- 这张表技术上可写,但不适合手工新增或编辑
|
||||||
|
- 宿主不应打开空白新增表单
|
||||||
|
- 技能应通过自身业务流程产生数据
|
||||||
|
- 这不是宿主猜测业务,而是技能通过元数据声明的结果
|
||||||
|
|
||||||
|
## 系统字段标准
|
||||||
|
|
||||||
|
通常保持 `editable = 0` 的字段类型包括:
|
||||||
|
|
||||||
|
- 自增主键
|
||||||
|
- generated 字段
|
||||||
|
- `created_at` / `updated_at`
|
||||||
|
- 同步时间戳
|
||||||
|
- 内容哈希
|
||||||
|
- 计算结果
|
||||||
|
- 内部状态
|
||||||
|
- 删除标记
|
||||||
|
- 审计字段
|
||||||
|
- 敏感凭据或密钥字段
|
||||||
|
|
||||||
|
不要把具体业务字段名写死;按字段类型和职责设置 `editable`。
|
||||||
|
|
||||||
|
## 表结构标准
|
||||||
|
|
||||||
|
- 业务表和字段使用英文 `snake_case`
|
||||||
|
- 可写表优先使用单字段主键
|
||||||
|
- 自增主键优先放在 `CREATE TABLE` 第一列
|
||||||
|
- `created_at` 和 `updated_at` 放在业务字段末尾
|
||||||
|
- 字段展示顺序以 `CREATE TABLE` 和 `PRAGMA table_info(cid)` 为准
|
||||||
|
- 不依赖 `display_order` 重新排列字段
|
||||||
|
- 不要让宿主通过英文字段名推断展示顺序
|
||||||
|
- 复杂字段需要通过 `display_type` 或 `visible` 明确处理
|
||||||
|
|
||||||
|
推荐顺序(可按业务裁剪,但已有字段应遵守相对位置):
|
||||||
|
|
||||||
|
```text
|
||||||
|
1. 主键 id
|
||||||
|
2. 关联标识与核心业务字段
|
||||||
|
3. 普通业务字段
|
||||||
|
4. 状态、结果、错误等辅助字段
|
||||||
|
5. created_at(如有)
|
||||||
|
6. updated_at(如有)
|
||||||
|
```
|
||||||
|
|
||||||
|
## 标准时间字段(新技能统一规范)
|
||||||
|
|
||||||
|
新技能业务表若需要时间审计字段,**统一**采用 Unix **秒级**时间戳,不要使用毫秒时间戳、ISO 字符串或 SQLite 文本 datetime。
|
||||||
|
|
||||||
|
| 项目 | 规范 |
|
||||||
|
|------|------|
|
||||||
|
| 物理字段 | `created_at INTEGER`、`updated_at INTEGER` |
|
||||||
|
| 存储格式 | Unix 秒级整数 |
|
||||||
|
| 展示元数据 `display_type` | `datetime_unix_seconds` |
|
||||||
|
| 中文名 | `created_at` → **创建时间**;`updated_at` → **更新时间** |
|
||||||
|
| 用户编辑 | `editable = 0`(系统字段,宿主只展示格式化结果) |
|
||||||
|
| 字段顺序 | 位于业务字段末尾:`… → created_at → updated_at` |
|
||||||
|
| 排序 | **不使用** `display_order`;顺序仅来自 `PRAGMA table_info` |
|
||||||
|
|
||||||
|
### 自动生成与维护
|
||||||
|
|
||||||
|
- **created_at**:`INTEGER NOT NULL DEFAULT (unixepoch())`;插入时可省略,由数据库生成。
|
||||||
|
- **updated_at**:`INTEGER NOT NULL DEFAULT (unixepoch())`;插入时同样可省略。
|
||||||
|
- **updated_at 更新**:模板默认表 `task_logs` 通过 SQLite trigger `{table}_set_updated_at` 在业务字段更新后自动刷新。
|
||||||
|
|
||||||
|
实现参考:
|
||||||
|
|
||||||
|
- DDL 与 trigger:`scripts/db/connection.py`、`scripts/db/timestamp_columns.py`
|
||||||
|
- 中文元数据种子:`scripts/db/display_metadata.py`
|
||||||
|
- 自检:`scripts/db/display_metadata_validator.py`
|
||||||
|
|
||||||
|
宿主只负责把 `display_type = datetime_unix_seconds` 格式化为 `YYYY-MM-DD HH:mm:ss` 展示,**不修改**数据库原始值。
|
||||||
|
|
||||||
|
### 模板默认:`task_logs`
|
||||||
|
|
||||||
|
`task_logs` 是技能内部**系统自动写入**的任务审计日志(由 `task_logs_repository` INSERT/UPDATE),不是供用户在宿主数据管理中手工维护的业务表。
|
||||||
|
|
||||||
|
权限设计:
|
||||||
|
|
||||||
|
| 层级 | 值 | 说明 |
|
||||||
|
|------|-----|------|
|
||||||
|
| 表级 `readonly` | `1` | 宿主数据管理只读展示,不提供新增/编辑/删除表单 |
|
||||||
|
| 所有业务字段 `editable` | `0` | 含 `task_type`、`status`、`error_msg`、`result_summary` 等 |
|
||||||
|
| 技能内部 repository | 正常 INSERT/UPDATE | 元数据权限只约束宿主 UI,不限制技能代码写库 |
|
||||||
|
|
||||||
|
物理定义(权威顺序):
|
||||||
|
|
||||||
|
```sql
|
||||||
|
CREATE TABLE IF NOT EXISTS task_logs (
|
||||||
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||||
|
task_type TEXT NOT NULL,
|
||||||
|
target_id TEXT,
|
||||||
|
input_id TEXT,
|
||||||
|
input_title TEXT,
|
||||||
|
status TEXT NOT NULL,
|
||||||
|
error_msg TEXT,
|
||||||
|
result_summary TEXT,
|
||||||
|
created_at INTEGER NOT NULL DEFAULT (unixepoch()),
|
||||||
|
updated_at INTEGER NOT NULL DEFAULT (unixepoch())
|
||||||
|
);
|
||||||
|
```
|
||||||
|
|
||||||
|
对应中文展示(由 `init_db()` 写入元数据表):
|
||||||
|
|
||||||
|
| 物理字段 | 中文名 | editable |
|
||||||
|
|----------|--------|----------|
|
||||||
|
| id | 编号 | 0 |
|
||||||
|
| task_type | 任务类型 | 0 |
|
||||||
|
| target_id | 目标编号 | 0 |
|
||||||
|
| input_id | 输入编号 | 0 |
|
||||||
|
| input_title | 输入标题 | 0 |
|
||||||
|
| status | 状态 | 0 |
|
||||||
|
| error_msg | 错误信息 | 0 |
|
||||||
|
| result_summary | 结果摘要 | 0 |
|
||||||
|
| created_at | 创建时间 | 0 |
|
||||||
|
| updated_at | 更新时间 | 0 |
|
||||||
|
|
||||||
|
表中文名:**任务日志**;表级 `readonly = 1`(宿主只读;技能 repository 仍可写入)。
|
||||||
|
|
||||||
|
### 通用示例:`demo_items`(测试 fixture 参考)
|
||||||
|
|
||||||
|
以下表结构用于模板测试演示数据管理契约,**不是**模板默认业务表:
|
||||||
|
|
||||||
|
```sql
|
||||||
|
CREATE TABLE demo_items (
|
||||||
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||||
|
title TEXT NOT NULL,
|
||||||
|
description TEXT,
|
||||||
|
status TEXT NOT NULL DEFAULT 'draft',
|
||||||
|
generated_value TEXT,
|
||||||
|
created_at INTEGER NOT NULL DEFAULT (unixepoch()),
|
||||||
|
updated_at INTEGER NOT NULL DEFAULT (unixepoch())
|
||||||
|
);
|
||||||
|
```
|
||||||
|
|
||||||
|
对应元数据要点:
|
||||||
|
|
||||||
|
| 物理字段 | editable | 说明 |
|
||||||
|
|----------|----------|------|
|
||||||
|
| id | 0 | 自增主键 |
|
||||||
|
| title | 1 | 业务字段 |
|
||||||
|
| description | 1 | 业务字段 |
|
||||||
|
| status | 1 | 枚举,`options_json` 演示 |
|
||||||
|
| generated_value | 0 | 系统/计算字段 |
|
||||||
|
| created_at | 0 | 系统时间 |
|
||||||
|
| updated_at | 0 | 系统时间 |
|
||||||
|
|
||||||
|
## 枚举字段 options_json
|
||||||
|
|
||||||
|
`options_json` 为 JSON 数组,每项包含 `value` 与 `label`:
|
||||||
|
|
||||||
|
```json
|
||||||
|
[
|
||||||
|
{"value": "draft", "label": "草稿"},
|
||||||
|
{"value": "published", "label": "已发布"},
|
||||||
|
{"value": "archived", "label": "已归档"}
|
||||||
|
]
|
||||||
|
```
|
||||||
|
|
||||||
|
宿主据此渲染选择框;技能侧负责保证 JSON 合法且与物理字段取值一致。
|
||||||
|
|
||||||
|
## 幂等初始化标准
|
||||||
|
|
||||||
|
- `_jiangchang_tables` 和 `_jiangchang_columns` 必须通过 upsert 初始化
|
||||||
|
- 多次执行初始化不能重复记录
|
||||||
|
- 重新初始化可以更新展示名、描述、`visible`、`searchable`、`editable`、`display_type`、`options_json`
|
||||||
|
- 元数据不能残留旧字段(删除或重命名字段时必须同步清理)
|
||||||
|
- 元数据不能引用不存在的表或字段
|
||||||
|
- 新增字段或表时必须同步增加元数据
|
||||||
|
- 不要直接修改用户真实数据库作为迁移方式
|
||||||
|
|
||||||
|
实现 helper:
|
||||||
|
|
||||||
|
- `upsert_table_metadata` / `upsert_column_metadata` — 幂等写入
|
||||||
|
- `seed_column_metadata_batch` — 批量 upsert;**可选**传入 `valid_column_names` 时在 upsert 后自动 prune
|
||||||
|
- `prune_stale_column_metadata` — 清理已删除物理字段的元数据
|
||||||
|
|
||||||
|
### 新增业务表时如何初始化
|
||||||
|
|
||||||
|
1. 在 `connection.py`(或迁移脚本)中 `CREATE TABLE` 真实物理表。
|
||||||
|
2. 用 `PRAGMA table_info` 或 `list_physical_column_names` 取得**真实物理字段列表**(顺序以 `cid` 为准)。
|
||||||
|
3. 调用 `upsert_table_metadata` 写入表级元数据。
|
||||||
|
4. 调用 `seed_column_metadata_batch`,并传入 `valid_column_names=physical_column_names`:
|
||||||
|
|
||||||
|
```python
|
||||||
|
from db.display_metadata import list_physical_column_names, seed_column_metadata_batch, upsert_table_metadata
|
||||||
|
|
||||||
|
physical = list_physical_column_names(cursor, "demo_items")
|
||||||
|
upsert_table_metadata(cursor, table_name="demo_items", display_name="示例记录", readonly=0)
|
||||||
|
seed_column_metadata_batch(
|
||||||
|
cursor,
|
||||||
|
table_name="demo_items",
|
||||||
|
columns=DEMO_ITEMS_COLUMN_METADATA,
|
||||||
|
valid_column_names=physical,
|
||||||
|
)
|
||||||
|
```
|
||||||
|
|
||||||
|
传入 `valid_column_names` 时:先 upsert,再删除不在物理表中的陈旧 `_jiangchang_columns` 行。
|
||||||
|
|
||||||
|
### 字段删除或重命名时如何清理
|
||||||
|
|
||||||
|
1. 在 DDL / 迁移中完成物理字段删除或重命名。
|
||||||
|
2. 重新执行 `seed_column_metadata_batch(..., valid_column_names=当前物理字段列表)`,自动 prune 旧字段元数据。
|
||||||
|
3. 或单独调用 `prune_stale_column_metadata(cursor, table_name, valid_column_names)`。
|
||||||
|
|
||||||
|
**不传入 `valid_column_names` 时不会自动 prune**——模板不会猜测真实物理字段,避免误删;文档与实现均以此为准。
|
||||||
|
|
||||||
|
不要直接修改用户真实数据库作为迁移方式;元数据初始化必须使用真实物理字段列表。
|
||||||
|
|
||||||
|
## 中文命名质量
|
||||||
|
|
||||||
|
- 使用普通用户能理解的业务用语:`task_logs` → **任务日志**,`error_msg` → **错误信息**。
|
||||||
|
- 禁止:`Task Logs`、`input编号`、`errormsg`、直接把物理名当 `display_name`。
|
||||||
|
- 不宜向用户暴露的实现细节(如 `payload_json`、`selector`)应设 `visible = 0` 或给出可理解中文名。
|
||||||
|
|
||||||
|
## 元数据同步规则
|
||||||
|
|
||||||
|
1. 新增用户可见业务表 → 同步 `_jiangchang_tables`。
|
||||||
|
2. 新增用户可见字段 → 同步 `_jiangchang_columns`。
|
||||||
|
3. 删除/重命名表或字段 → 同步清理或迁移元数据。
|
||||||
|
4. 初始化脚本必须幂等。
|
||||||
|
5. 元数据中的表、字段必须在库中真实存在;可见项不得缺少中文 `display_name`。
|
||||||
|
|
||||||
|
## 不同业务的 `task_logs` 映射
|
||||||
|
|
||||||
|
| 业务场景 | task_type | target_id | input_id |
|
||||||
|
|----------|-----------|-----------|----------|
|
||||||
|
| 发布类 | publish | 账号 ID | 内容 ID |
|
||||||
|
| 工资代发 | disburse | 付款账户 | 批次 ID |
|
||||||
|
| 对账 | reconcile | 平台标识 | 对账批次 ID |
|
||||||
| 发票验真 | verify | 税务地区 | 发票批次 ID |
|
| 发票验真 | verify | 税务地区 | 发票批次 ID |
|
||||||
| 报关 | declare | 港口 / 海关 | 报关批次 ID |
|
|
||||||
|
业务特有字段优先放入 `result_summary`(JSON 字符串),避免随意加列;若确需新列,必须同步元数据并保持合理物理顺序。
|
||||||
|
|
||||||
|
## 开发自检
|
||||||
|
|
||||||
|
- `init_db()` 后运行 `tests/test_display_metadata.py` 与 `tests/test_data_management_contract.py`。
|
||||||
|
- 若提供宿主 Action 入口,运行 `tests/test_actions_manifest.py`。
|
||||||
|
- 复制为真实技能后,使用 `scripts/db/display_metadata_validator.py` 中的校验函数检查 SKILL 名称、slug 与元数据完整性。
|
||||||
|
|
||||||
## 模板原则
|
## 模板原则
|
||||||
|
|
||||||
- 模板不做历史迁移兼容设计
|
- 模板不做复杂历史迁移框架;新 skill 从当前 schema 起步。
|
||||||
- 新 skill 直接从当前 schema 起步
|
- 元数据 DDL 与 `task_logs` 中文种子以 `scripts/db/display_metadata.py` 为单一权威来源。
|
||||||
- 业务有特殊字段时,建议放 `result_summary`(JSON 字符串),不要乱加列
|
- 模板定义「如何声明能力」;具体技能定义「声明什么业务能力」;宿主负责「如何通用渲染和执行」。
|
||||||
|
|||||||
275
release.ps1
275
release.ps1
@@ -1,3 +1,19 @@
|
|||||||
|
<#
|
||||||
|
.SYNOPSIS
|
||||||
|
Local git release for skill-template repo.
|
||||||
|
|
||||||
|
.DESCRIPTION
|
||||||
|
Handles git commit / push / semver tag / push tag only.
|
||||||
|
Encryption, ZIP packaging, and marketplace sync are done by CI
|
||||||
|
(.github/workflows/release_skill.yaml → reusable-release-skill.yaml@main).
|
||||||
|
|
||||||
|
Requires: git, PowerShell 5+
|
||||||
|
|
||||||
|
After fetch, runs default tests against the current working tree (before auto-commit):
|
||||||
|
python tests/run_tests.py -v
|
||||||
|
Use -SkipTests only for emergency diagnostics. -DryRun still runs tests.
|
||||||
|
#>
|
||||||
|
|
||||||
[CmdletBinding()]
|
[CmdletBinding()]
|
||||||
param(
|
param(
|
||||||
[string]$Prefix = "v",
|
[string]$Prefix = "v",
|
||||||
@@ -5,19 +21,260 @@ param(
|
|||||||
[switch]$AutoCommit,
|
[switch]$AutoCommit,
|
||||||
[switch]$RequireClean,
|
[switch]$RequireClean,
|
||||||
[string]$CommitMessage,
|
[string]$CommitMessage,
|
||||||
[switch]$DryRun
|
[switch]$DryRun,
|
||||||
|
[switch]$SkipTests
|
||||||
)
|
)
|
||||||
|
|
||||||
Set-StrictMode -Version Latest
|
Set-StrictMode -Version Latest
|
||||||
$ErrorActionPreference = "Stop"
|
$ErrorActionPreference = "Stop"
|
||||||
|
|
||||||
$scriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
|
function Remove-GitNoise {
|
||||||
$sharedScript = Join-Path $scriptDir "..\..\jiangchang-platform-kit\tools\release.ps1"
|
param([string[]]$Lines)
|
||||||
$sharedScript = [System.IO.Path]::GetFullPath($sharedScript)
|
return @($Lines | Where-Object {
|
||||||
|
$_ -and ($_ -notmatch '^warning:\s+unable to access ')
|
||||||
if (-not (Test-Path $sharedScript)) {
|
})
|
||||||
throw "Shared release script not found: $sharedScript"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
& $sharedScript @PSBoundParameters
|
function Invoke-Git {
|
||||||
exit $LASTEXITCODE
|
param([Parameter(Mandatory = $true)][string]$Args)
|
||||||
|
Write-Host ">> git $Args" -ForegroundColor DarkGray
|
||||||
|
& cmd /c "git $Args 2>&1" | Out-Null
|
||||||
|
if ($LASTEXITCODE -ne 0) {
|
||||||
|
throw "git command failed: git $Args"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function Get-GitOutput {
|
||||||
|
param([Parameter(Mandatory = $true)][string]$Args)
|
||||||
|
$output = & cmd /c "git $Args 2>&1"
|
||||||
|
if ($LASTEXITCODE -ne 0) {
|
||||||
|
throw "git command failed: git $Args"
|
||||||
|
}
|
||||||
|
return @(Remove-GitNoise -Lines @($output))
|
||||||
|
}
|
||||||
|
|
||||||
|
function Test-Repo {
|
||||||
|
$output = & cmd /c "git rev-parse --is-inside-work-tree 2>&1"
|
||||||
|
if ($LASTEXITCODE -ne 0) {
|
||||||
|
return $false
|
||||||
|
}
|
||||||
|
$clean = Remove-GitNoise -Lines @($output)
|
||||||
|
return (($clean | Select-Object -First 1) -eq "true")
|
||||||
|
}
|
||||||
|
|
||||||
|
function Get-CurrentBranch {
|
||||||
|
$b = (Get-GitOutput "branch --show-current" | Select-Object -First 1).Trim()
|
||||||
|
return $b
|
||||||
|
}
|
||||||
|
|
||||||
|
function Get-StatusPorcelain {
|
||||||
|
$lines = @(Get-GitOutput "status --porcelain")
|
||||||
|
return $lines
|
||||||
|
}
|
||||||
|
|
||||||
|
function Parse-SemVerTag {
|
||||||
|
param(
|
||||||
|
[string]$Tag,
|
||||||
|
[string]$TagPrefix
|
||||||
|
)
|
||||||
|
$escaped = [regex]::Escape($TagPrefix)
|
||||||
|
$m = [regex]::Match($Tag, "^${escaped}(\d+)\.(\d+)\.(\d+)$")
|
||||||
|
if (-not $m.Success) { return $null }
|
||||||
|
|
||||||
|
return [pscustomobject]@{
|
||||||
|
Raw = $Tag
|
||||||
|
Major = [int]$m.Groups[1].Value
|
||||||
|
Minor = [int]$m.Groups[2].Value
|
||||||
|
Patch = [int]$m.Groups[3].Value
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function Get-NextTag {
|
||||||
|
param([string]$TagPrefix)
|
||||||
|
|
||||||
|
$tags = Get-GitOutput "tag --list"
|
||||||
|
$parsed = @()
|
||||||
|
|
||||||
|
foreach ($t in $tags) {
|
||||||
|
$t = $t.Trim()
|
||||||
|
if (-not $t) { continue }
|
||||||
|
$obj = Parse-SemVerTag -Tag $t -TagPrefix $TagPrefix
|
||||||
|
if ($null -ne $obj) { $parsed += $obj }
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($parsed.Count -eq 0) {
|
||||||
|
return "${TagPrefix}1.0.1"
|
||||||
|
}
|
||||||
|
|
||||||
|
$latest = $parsed | Sort-Object Major, Minor, Patch | Select-Object -Last 1
|
||||||
|
return "$TagPrefix$($latest.Major).$($latest.Minor).$([int]$latest.Patch + 1)"
|
||||||
|
}
|
||||||
|
|
||||||
|
function Assert-SkillReleasePackagingSources {
|
||||||
|
param([Parameter(Mandatory = $true)][string]$SkillRoot)
|
||||||
|
|
||||||
|
$scriptsDir = Join-Path $SkillRoot "scripts"
|
||||||
|
$mainPy = Join-Path $scriptsDir "main.py"
|
||||||
|
if (-not (Test-Path -LiteralPath $mainPy)) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
$text = Get-Content -LiteralPath $mainPy -Raw -Encoding UTF8 -ErrorAction SilentlyContinue
|
||||||
|
if ([string]::IsNullOrWhiteSpace($text)) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
$need = @()
|
||||||
|
if ($text -match '(?m)^\s*from\s+cli\.') { $need += 'cli' }
|
||||||
|
if ($text -match '(?m)^\s*import\s+cli\b') { $need += 'cli' }
|
||||||
|
if ($text -match '(?m)^\s*from\s+service\.') { $need += 'service' }
|
||||||
|
if ($text -match '(?m)^\s*import\s+service\b') { $need += 'service' }
|
||||||
|
if ($text -match '(?m)^\s*from\s+db\.') { $need += 'db' }
|
||||||
|
if ($text -match '(?m)^\s*import\s+db\b') { $need += 'db' }
|
||||||
|
if ($text -match '(?m)^\s*from\s+util\.') { $need += 'util' }
|
||||||
|
if ($text -match '(?m)^\s*import\s+util\b') { $need += 'util' }
|
||||||
|
|
||||||
|
foreach ($p in ($need | Select-Object -Unique)) {
|
||||||
|
$folder = Join-Path $scriptsDir $p
|
||||||
|
if (-not (Test-Path -LiteralPath $folder)) {
|
||||||
|
throw "Release check failed: scripts/main.py imports from '$p' but folder is missing: $folder"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$pyFiles = @(Get-ChildItem -LiteralPath $scriptsDir -Filter *.py -Recurse -File -ErrorAction SilentlyContinue)
|
||||||
|
Write-Host "Packaging check: $($pyFiles.Count) Python file(s) under scripts/ (CI will obfuscate all recursively)." -ForegroundColor DarkGray
|
||||||
|
}
|
||||||
|
|
||||||
|
function Invoke-DefaultTests {
|
||||||
|
param(
|
||||||
|
[Parameter(Mandatory = $true)][string]$SkillRoot,
|
||||||
|
[switch]$Skip
|
||||||
|
)
|
||||||
|
|
||||||
|
if ($Skip) {
|
||||||
|
Write-Host "WARNING: Skipping tests before release. This should only be used for emergency diagnostics." -ForegroundColor Yellow
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
$runTests = Join-Path $SkillRoot "tests\run_tests.py"
|
||||||
|
if (-not (Test-Path -LiteralPath $runTests)) {
|
||||||
|
throw "Release check failed: tests/run_tests.py not found."
|
||||||
|
}
|
||||||
|
|
||||||
|
Write-Host ">> python tests/run_tests.py -v" -ForegroundColor DarkGray
|
||||||
|
& python $runTests -v
|
||||||
|
if ($LASTEXITCODE -ne 0) {
|
||||||
|
throw "Release aborted: default tests failed (python tests/run_tests.py -v)."
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function Ensure-CleanOrAutoCommit {
|
||||||
|
param(
|
||||||
|
[switch]$DoAutoCommit,
|
||||||
|
[switch]$NeedClean,
|
||||||
|
[switch]$IsDryRun,
|
||||||
|
[string]$Msg
|
||||||
|
)
|
||||||
|
|
||||||
|
$status = @(Get-StatusPorcelain)
|
||||||
|
if ($status.Length -eq 0) { return }
|
||||||
|
|
||||||
|
if ($NeedClean) {
|
||||||
|
Write-Host "Working tree is not clean and -RequireClean is enabled." -ForegroundColor Yellow
|
||||||
|
Remove-GitNoise -Lines @(& cmd /c "git status --short 2>&1") | ForEach-Object { Write-Host $_ }
|
||||||
|
throw "Abort: dirty working tree."
|
||||||
|
}
|
||||||
|
|
||||||
|
$commitMsg = $Msg
|
||||||
|
if ([string]::IsNullOrWhiteSpace($commitMsg)) {
|
||||||
|
$commitMsg = "chore: auto release commit ($(Get-Date -Format 'yyyy-MM-dd HH:mm:ss'))"
|
||||||
|
}
|
||||||
|
|
||||||
|
if (-not $DoAutoCommit) {
|
||||||
|
Write-Host "Detected uncommitted changes, auto-committing before release..." -ForegroundColor Yellow
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($IsDryRun) {
|
||||||
|
Write-Host "[DryRun] Would run: git add -A" -ForegroundColor Yellow
|
||||||
|
Write-Host "[DryRun] Would run: git commit -m `"$commitMsg`"" -ForegroundColor Yellow
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
Invoke-Git "add -A"
|
||||||
|
Invoke-Git "commit -m `"$commitMsg`""
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
Write-Host "=== Release Script Start ===" -ForegroundColor Cyan
|
||||||
|
|
||||||
|
if (-not (Test-Repo)) {
|
||||||
|
throw "Current directory is not a git repository."
|
||||||
|
}
|
||||||
|
|
||||||
|
$branch = Get-CurrentBranch
|
||||||
|
if ([string]::IsNullOrWhiteSpace($branch)) {
|
||||||
|
throw "Unable to determine current branch."
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($branch -notin @("main", "master")) {
|
||||||
|
throw "Current branch is '$branch'. Release is only allowed from main/master."
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($DryRun) {
|
||||||
|
Write-Host "[DryRun] Would run: git fetch --tags --prune origin" -ForegroundColor Yellow
|
||||||
|
} else {
|
||||||
|
Invoke-Git "fetch --tags --prune origin"
|
||||||
|
}
|
||||||
|
|
||||||
|
$skillRoot = (Get-GitOutput "rev-parse --show-toplevel" | Select-Object -First 1).Trim()
|
||||||
|
if (Test-Path -LiteralPath (Join-Path $skillRoot "SKILL.md")) {
|
||||||
|
Assert-SkillReleasePackagingSources -SkillRoot $skillRoot
|
||||||
|
}
|
||||||
|
|
||||||
|
Invoke-DefaultTests -SkillRoot $skillRoot -Skip:$SkipTests
|
||||||
|
|
||||||
|
Ensure-CleanOrAutoCommit -DoAutoCommit:$AutoCommit -NeedClean:$RequireClean -IsDryRun:$DryRun -Msg $CommitMessage
|
||||||
|
|
||||||
|
$null = Remove-GitNoise -Lines @(& cmd /c 'git rev-parse --abbrev-ref --symbolic-full-name "@{u}" 2>&1')
|
||||||
|
$hasUpstream = ($LASTEXITCODE -eq 0)
|
||||||
|
|
||||||
|
if ($DryRun) {
|
||||||
|
if ($hasUpstream) {
|
||||||
|
Write-Host "[DryRun] Would run: git push" -ForegroundColor Yellow
|
||||||
|
} else {
|
||||||
|
Write-Host "[DryRun] Would run: git push -u origin $branch" -ForegroundColor Yellow
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if ($hasUpstream) {
|
||||||
|
Invoke-Git "push"
|
||||||
|
} else {
|
||||||
|
Invoke-Git "push -u origin $branch"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$nextTag = Get-NextTag -TagPrefix $Prefix
|
||||||
|
Write-Host "Next tag: $nextTag" -ForegroundColor Green
|
||||||
|
|
||||||
|
$existing = @(Get-GitOutput "tag --list `"$nextTag`"")
|
||||||
|
if ($existing.Length -gt 0) {
|
||||||
|
throw "Tag already exists: $nextTag"
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($DryRun) {
|
||||||
|
Write-Host "[DryRun] Would run: git tag -a $nextTag -m `"$Message`"" -ForegroundColor Yellow
|
||||||
|
Write-Host "[DryRun] Would run: git push origin $nextTag" -ForegroundColor Yellow
|
||||||
|
Write-Host "=== DryRun Complete ===" -ForegroundColor Cyan
|
||||||
|
exit 0
|
||||||
|
}
|
||||||
|
|
||||||
|
Invoke-Git "tag -a $nextTag -m `"$Message`""
|
||||||
|
Invoke-Git "push origin $nextTag"
|
||||||
|
|
||||||
|
Write-Host "Release success: $nextTag" -ForegroundColor Green
|
||||||
|
Write-Host "=== Release Script Done ===" -ForegroundColor Cyan
|
||||||
|
exit 0
|
||||||
|
}
|
||||||
|
catch {
|
||||||
|
Write-Host "Release failed: $($_.Exception.Message)" -ForegroundColor Red
|
||||||
|
exit 1
|
||||||
|
}
|
||||||
|
|||||||
@@ -4,6 +4,8 @@ from __future__ import annotations
|
|||||||
|
|
||||||
import sqlite3
|
import sqlite3
|
||||||
|
|
||||||
|
from db.display_metadata import init_display_metadata
|
||||||
|
from db.timestamp_columns import init_task_logs_timestamp_maintenance
|
||||||
from util.runtime_paths import get_db_path
|
from util.runtime_paths import get_db_path
|
||||||
|
|
||||||
|
|
||||||
@@ -26,11 +28,13 @@ def init_db() -> None:
|
|||||||
status TEXT NOT NULL,
|
status TEXT NOT NULL,
|
||||||
error_msg TEXT,
|
error_msg TEXT,
|
||||||
result_summary TEXT,
|
result_summary TEXT,
|
||||||
created_at INTEGER NOT NULL,
|
created_at INTEGER NOT NULL DEFAULT (unixepoch()),
|
||||||
updated_at INTEGER NOT NULL
|
updated_at INTEGER NOT NULL DEFAULT (unixepoch())
|
||||||
)
|
)
|
||||||
"""
|
"""
|
||||||
)
|
)
|
||||||
|
init_task_logs_timestamp_maintenance(cur)
|
||||||
|
init_display_metadata(cur)
|
||||||
conn.commit()
|
conn.commit()
|
||||||
finally:
|
finally:
|
||||||
conn.close()
|
conn.close()
|
||||||
|
|||||||
283
scripts/db/display_metadata.py
Normal file
283
scripts/db/display_metadata.py
Normal file
@@ -0,0 +1,283 @@
|
|||||||
|
"""匠厂数据管理展示元数据(_jiangchang_tables / _jiangchang_columns)。
|
||||||
|
|
||||||
|
SQLite 无原生表/字段 COMMENT;中文展示名称通过这两张元数据表提供给宿主「数据管理」页面。
|
||||||
|
字段在界面上的顺序由 CREATE TABLE / PRAGMA table_info(cid) 决定,**不**使用 display_order 排序。
|
||||||
|
"""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from typing import Iterable
|
||||||
|
|
||||||
|
# 宿主会隐藏这些表,不作为业务表展示(见 jiangchang electron/data-management/types.ts)
|
||||||
|
INTERNAL_METADATA_TABLES = frozenset({"_jiangchang_tables", "_jiangchang_columns"})
|
||||||
|
LEGACY_METADATA_TABLES = frozenset({"_schema_meta"})
|
||||||
|
METADATA_TABLES = INTERNAL_METADATA_TABLES | LEGACY_METADATA_TABLES
|
||||||
|
|
||||||
|
TASK_LOGS_TABLE = "task_logs"
|
||||||
|
DATETIME_UNIX_SECONDS = "datetime_unix_seconds"
|
||||||
|
STANDARD_TIMESTAMP_COLUMNS = frozenset({"created_at", "updated_at"})
|
||||||
|
|
||||||
|
_JIANGCHANG_TABLES_DDL = """
|
||||||
|
CREATE TABLE IF NOT EXISTS _jiangchang_tables (
|
||||||
|
table_name TEXT PRIMARY KEY,
|
||||||
|
display_name TEXT NOT NULL,
|
||||||
|
description TEXT,
|
||||||
|
sort_order INTEGER,
|
||||||
|
visible INTEGER NOT NULL DEFAULT 1,
|
||||||
|
readonly INTEGER NOT NULL DEFAULT 0
|
||||||
|
)
|
||||||
|
"""
|
||||||
|
|
||||||
|
_JIANGCHANG_COLUMNS_DDL = """
|
||||||
|
CREATE TABLE IF NOT EXISTS _jiangchang_columns (
|
||||||
|
table_name TEXT NOT NULL,
|
||||||
|
column_name TEXT NOT NULL,
|
||||||
|
display_name TEXT NOT NULL,
|
||||||
|
description TEXT,
|
||||||
|
display_order INTEGER,
|
||||||
|
visible INTEGER NOT NULL DEFAULT 1,
|
||||||
|
searchable INTEGER NOT NULL DEFAULT 1,
|
||||||
|
editable INTEGER NOT NULL DEFAULT 1,
|
||||||
|
display_type TEXT,
|
||||||
|
options_json TEXT,
|
||||||
|
PRIMARY KEY (table_name, column_name)
|
||||||
|
)
|
||||||
|
"""
|
||||||
|
|
||||||
|
TASK_LOGS_TABLE_METADATA = {
|
||||||
|
"table_name": TASK_LOGS_TABLE,
|
||||||
|
"display_name": "任务日志",
|
||||||
|
"description": "记录技能任务的执行状态和结果(系统自动写入,宿主数据管理只读展示)",
|
||||||
|
"visible": 1,
|
||||||
|
"readonly": 1,
|
||||||
|
}
|
||||||
|
|
||||||
|
TASK_LOGS_COLUMN_METADATA: tuple[dict[str, object], ...] = (
|
||||||
|
{"column_name": "id", "display_name": "编号", "description": "任务日志唯一编号", "visible": 1, "searchable": 1, "editable": 0},
|
||||||
|
{"column_name": "task_type", "display_name": "任务类型", "description": "任务的业务类型", "visible": 1, "searchable": 1, "editable": 0},
|
||||||
|
{"column_name": "target_id", "display_name": "目标编号", "description": "任务目标对象编号", "visible": 1, "searchable": 1, "editable": 0},
|
||||||
|
{"column_name": "input_id", "display_name": "输入编号", "description": "输入对象编号", "visible": 1, "searchable": 1, "editable": 0},
|
||||||
|
{"column_name": "input_title", "display_name": "输入标题", "description": "输入对象标题", "visible": 1, "searchable": 1, "editable": 0},
|
||||||
|
{"column_name": "status", "display_name": "状态", "description": "任务当前状态", "visible": 1, "searchable": 1, "editable": 0},
|
||||||
|
{"column_name": "error_msg", "display_name": "错误信息", "description": "任务失败时的错误说明", "visible": 1, "searchable": 1, "editable": 0},
|
||||||
|
{"column_name": "result_summary", "display_name": "结果摘要", "description": "任务执行结果摘要", "visible": 1, "searchable": 1, "editable": 0},
|
||||||
|
{
|
||||||
|
"column_name": "created_at",
|
||||||
|
"display_name": "创建时间",
|
||||||
|
"description": "记录创建时间(Unix 秒级时间戳,数据库自动生成)",
|
||||||
|
"visible": 1,
|
||||||
|
"searchable": 0,
|
||||||
|
"editable": 0,
|
||||||
|
"display_type": DATETIME_UNIX_SECONDS,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"column_name": "updated_at",
|
||||||
|
"display_name": "更新时间",
|
||||||
|
"description": "记录最后更新时间(Unix 秒级时间戳,自动维护)",
|
||||||
|
"visible": 1,
|
||||||
|
"searchable": 0,
|
||||||
|
"editable": 0,
|
||||||
|
"display_type": DATETIME_UNIX_SECONDS,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def init_display_metadata_tables(cursor) -> None:
|
||||||
|
cursor.execute(_JIANGCHANG_TABLES_DDL)
|
||||||
|
cursor.execute(_JIANGCHANG_COLUMNS_DDL)
|
||||||
|
|
||||||
|
|
||||||
|
def upsert_table_metadata(cursor, *, table_name: str, display_name: str, description: str | None = None, visible: int = 1, readonly: int = 0) -> None:
|
||||||
|
cursor.execute(
|
||||||
|
"""
|
||||||
|
INSERT INTO _jiangchang_tables (table_name, display_name, description, visible, readonly)
|
||||||
|
VALUES (?, ?, ?, ?, ?)
|
||||||
|
ON CONFLICT(table_name) DO UPDATE SET
|
||||||
|
display_name = excluded.display_name,
|
||||||
|
description = excluded.description,
|
||||||
|
visible = excluded.visible,
|
||||||
|
readonly = excluded.readonly
|
||||||
|
""",
|
||||||
|
(table_name, display_name, description, visible, readonly),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def upsert_column_metadata(
|
||||||
|
cursor,
|
||||||
|
*,
|
||||||
|
table_name: str,
|
||||||
|
column_name: str,
|
||||||
|
display_name: str,
|
||||||
|
description: str | None = None,
|
||||||
|
visible: int = 1,
|
||||||
|
searchable: int = 1,
|
||||||
|
editable: int = 1,
|
||||||
|
display_type: str | None = None,
|
||||||
|
options_json: str | None = None,
|
||||||
|
) -> None:
|
||||||
|
# display_order 保留为 NULL:宿主按 PRAGMA table_info cid 展示字段,不读 display_order。
|
||||||
|
cursor.execute(
|
||||||
|
"""
|
||||||
|
INSERT INTO _jiangchang_columns (
|
||||||
|
table_name, column_name, display_name, description,
|
||||||
|
display_order, visible, searchable, editable, display_type, options_json
|
||||||
|
)
|
||||||
|
VALUES (?, ?, ?, ?, NULL, ?, ?, ?, ?, ?)
|
||||||
|
ON CONFLICT(table_name, column_name) DO UPDATE SET
|
||||||
|
display_name = excluded.display_name,
|
||||||
|
description = excluded.description,
|
||||||
|
display_order = NULL,
|
||||||
|
visible = excluded.visible,
|
||||||
|
searchable = excluded.searchable,
|
||||||
|
editable = excluded.editable,
|
||||||
|
display_type = excluded.display_type,
|
||||||
|
options_json = excluded.options_json
|
||||||
|
""",
|
||||||
|
(
|
||||||
|
table_name,
|
||||||
|
column_name,
|
||||||
|
display_name,
|
||||||
|
description,
|
||||||
|
visible,
|
||||||
|
searchable,
|
||||||
|
editable,
|
||||||
|
display_type,
|
||||||
|
options_json,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def seed_task_logs_display_metadata(cursor) -> None:
|
||||||
|
meta = TASK_LOGS_TABLE_METADATA
|
||||||
|
upsert_table_metadata(
|
||||||
|
cursor,
|
||||||
|
table_name=str(meta["table_name"]),
|
||||||
|
display_name=str(meta["display_name"]),
|
||||||
|
description=str(meta["description"]) if meta.get("description") else None,
|
||||||
|
visible=int(meta["visible"]),
|
||||||
|
readonly=int(meta["readonly"]),
|
||||||
|
)
|
||||||
|
seed_column_metadata_batch(
|
||||||
|
cursor,
|
||||||
|
table_name=TASK_LOGS_TABLE,
|
||||||
|
columns=TASK_LOGS_COLUMN_METADATA,
|
||||||
|
valid_column_names=[str(column["column_name"]) for column in TASK_LOGS_COLUMN_METADATA],
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def seed_column_metadata_batch(
|
||||||
|
cursor,
|
||||||
|
*,
|
||||||
|
table_name: str,
|
||||||
|
columns: Iterable[dict[str, object]],
|
||||||
|
valid_column_names: Iterable[str] | None = None,
|
||||||
|
) -> None:
|
||||||
|
"""批量 upsert 字段元数据;传入 valid_column_names 时会在 upsert 后清理陈旧字段元数据。"""
|
||||||
|
for column in columns:
|
||||||
|
upsert_column_metadata(
|
||||||
|
cursor,
|
||||||
|
table_name=table_name,
|
||||||
|
column_name=str(column["column_name"]),
|
||||||
|
display_name=str(column["display_name"]),
|
||||||
|
description=str(column["description"]) if column.get("description") else None,
|
||||||
|
visible=int(column.get("visible", 1)),
|
||||||
|
searchable=int(column.get("searchable", 1)),
|
||||||
|
editable=int(column.get("editable", 1)),
|
||||||
|
display_type=str(column["display_type"]) if column.get("display_type") else None,
|
||||||
|
options_json=str(column["options_json"]) if column.get("options_json") else None,
|
||||||
|
)
|
||||||
|
if valid_column_names is not None:
|
||||||
|
prune_stale_column_metadata(cursor, table_name, valid_column_names)
|
||||||
|
|
||||||
|
|
||||||
|
def prune_stale_column_metadata(cursor, table_name: str, valid_column_names: Iterable[str]) -> None:
|
||||||
|
"""删除表中已不存在物理字段的元数据行(幂等初始化后不得残留旧字段)。"""
|
||||||
|
valid = set(valid_column_names)
|
||||||
|
cursor.execute(
|
||||||
|
"SELECT column_name FROM _jiangchang_columns WHERE table_name = ?",
|
||||||
|
(table_name,),
|
||||||
|
)
|
||||||
|
for (column_name,) in cursor.fetchall():
|
||||||
|
if str(column_name) not in valid:
|
||||||
|
cursor.execute(
|
||||||
|
"DELETE FROM _jiangchang_columns WHERE table_name = ? AND column_name = ?",
|
||||||
|
(table_name, column_name),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def get_table_readonly(cursor, table_name: str) -> int | None:
|
||||||
|
cursor.execute(
|
||||||
|
"SELECT readonly FROM _jiangchang_tables WHERE table_name = ?",
|
||||||
|
(table_name,),
|
||||||
|
)
|
||||||
|
row = cursor.fetchone()
|
||||||
|
if not row:
|
||||||
|
return None
|
||||||
|
return int(row[0] or 0)
|
||||||
|
|
||||||
|
|
||||||
|
def get_column_editable(cursor, table_name: str, column_name: str) -> int | None:
|
||||||
|
cursor.execute(
|
||||||
|
"""
|
||||||
|
SELECT editable FROM _jiangchang_columns
|
||||||
|
WHERE table_name = ? AND column_name = ?
|
||||||
|
""",
|
||||||
|
(table_name, column_name),
|
||||||
|
)
|
||||||
|
row = cursor.fetchone()
|
||||||
|
if not row:
|
||||||
|
return None
|
||||||
|
return int(row[0] or 0)
|
||||||
|
|
||||||
|
|
||||||
|
def list_editable_business_columns(cursor, table_name: str) -> list[str]:
|
||||||
|
"""返回 editable=1 的业务字段名(不含宿主侧主键/generated 等二次过滤)。"""
|
||||||
|
physical = list_physical_column_names(cursor, table_name)
|
||||||
|
cursor.execute(
|
||||||
|
"""
|
||||||
|
SELECT column_name, editable
|
||||||
|
FROM _jiangchang_columns
|
||||||
|
WHERE table_name = ?
|
||||||
|
""",
|
||||||
|
(table_name,),
|
||||||
|
)
|
||||||
|
editable_set = {
|
||||||
|
str(column_name)
|
||||||
|
for column_name, editable in cursor.fetchall()
|
||||||
|
if editable is not None and int(editable) == 1
|
||||||
|
}
|
||||||
|
return [name for name in physical if name in editable_set]
|
||||||
|
|
||||||
|
|
||||||
|
def table_allows_manual_create(cursor, table_name: str) -> bool:
|
||||||
|
"""表级 readonly=0 且至少有一个 editable=1 字段时,宿主才应提供空白新增表单。"""
|
||||||
|
readonly = get_table_readonly(cursor, table_name)
|
||||||
|
if readonly is None or int(readonly) != 0:
|
||||||
|
return False
|
||||||
|
return bool(list_editable_business_columns(cursor, table_name))
|
||||||
|
|
||||||
|
|
||||||
|
def init_display_metadata(cursor) -> None:
|
||||||
|
init_display_metadata_tables(cursor)
|
||||||
|
seed_task_logs_display_metadata(cursor)
|
||||||
|
|
||||||
|
|
||||||
|
def iter_user_tables(cursor) -> Iterable[str]:
|
||||||
|
cursor.execute(
|
||||||
|
"SELECT name FROM sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite_%' ORDER BY name"
|
||||||
|
)
|
||||||
|
for (name,) in cursor.fetchall():
|
||||||
|
if name in METADATA_TABLES:
|
||||||
|
continue
|
||||||
|
yield str(name)
|
||||||
|
|
||||||
|
|
||||||
|
def _quote_identifier(name: str) -> str:
|
||||||
|
return '"' + name.replace('"', '""') + '"'
|
||||||
|
|
||||||
|
|
||||||
|
def list_physical_column_names(cursor, table_name: str) -> list[str]:
|
||||||
|
cursor.execute(f"PRAGMA table_info({_quote_identifier(table_name)})")
|
||||||
|
rows = cursor.fetchall()
|
||||||
|
rows.sort(key=lambda row: int(row[0]))
|
||||||
|
return [str(row[1]) for row in rows]
|
||||||
510
scripts/db/display_metadata_validator.py
Normal file
510
scripts/db/display_metadata_validator.py
Normal file
@@ -0,0 +1,510 @@
|
|||||||
|
"""数据管理展示元数据校验(模板开发自检,不依赖宿主)。"""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import json
|
||||||
|
import os
|
||||||
|
import re
|
||||||
|
import sqlite3
|
||||||
|
from dataclasses import dataclass, field
|
||||||
|
|
||||||
|
from db.display_metadata import (
|
||||||
|
DATETIME_UNIX_SECONDS,
|
||||||
|
METADATA_TABLES,
|
||||||
|
STANDARD_TIMESTAMP_COLUMNS,
|
||||||
|
TASK_LOGS_COLUMN_METADATA,
|
||||||
|
TASK_LOGS_TABLE,
|
||||||
|
iter_user_tables,
|
||||||
|
list_editable_business_columns,
|
||||||
|
list_physical_column_names,
|
||||||
|
table_allows_manual_create,
|
||||||
|
)
|
||||||
|
from db.timestamp_columns import has_updated_at_trigger
|
||||||
|
from util.constants import SKILL_SLUG
|
||||||
|
from util.slug_naming import LEGACY_EXEMPT_SLUGS, validate_slug_semantics
|
||||||
|
|
||||||
|
_TEMPLATE_PLACEHOLDER_SLUGS = frozenset({"your-skill-slug", "your_skill_slug"})
|
||||||
|
_ENGLISH_PLACEHOLDER_NAME_PATTERNS = (
|
||||||
|
re.compile(r"^my\s+skill$", re.I),
|
||||||
|
re.compile(r"^your[-_\s]?skill", re.I),
|
||||||
|
re.compile(r"^skill[-_\s]?template$", re.I),
|
||||||
|
re.compile(r"^[a-z0-9_-]+$"),
|
||||||
|
)
|
||||||
|
_KEBAB_SLUG = re.compile(r"^[a-z0-9]+(?:-[a-z0-9]+)*$")
|
||||||
|
_CJK_RE = re.compile(r"[\u4e00-\u9fff]")
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class ValidationReport:
|
||||||
|
errors: list[str] = field(default_factory=list)
|
||||||
|
warnings: list[str] = field(default_factory=list)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def ok(self) -> bool:
|
||||||
|
return not self.errors
|
||||||
|
|
||||||
|
|
||||||
|
def _has_cjk(text: str) -> bool:
|
||||||
|
return bool(_CJK_RE.search(text))
|
||||||
|
|
||||||
|
|
||||||
|
def _looks_like_raw_physical_name(display_name: str, physical_name: str) -> bool:
|
||||||
|
if not display_name.strip():
|
||||||
|
return True
|
||||||
|
if display_name.strip().lower() == physical_name.strip().lower():
|
||||||
|
return True
|
||||||
|
compact = display_name.replace(" ", "").replace("_", "").lower()
|
||||||
|
if compact == physical_name.replace("_", "").lower():
|
||||||
|
return True
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
def _parse_frontmatter_field(text: str, field_name: str) -> str:
|
||||||
|
parts = text.split("---", 2)
|
||||||
|
if len(parts) < 2:
|
||||||
|
return ""
|
||||||
|
for line in parts[1].splitlines():
|
||||||
|
match = re.match(rf"^{re.escape(field_name)}:\s*(.+)\s*$", line.strip())
|
||||||
|
if match:
|
||||||
|
return match.group(1).strip().strip("\"'")
|
||||||
|
return ""
|
||||||
|
|
||||||
|
|
||||||
|
def _parse_openclaw_slug(text: str) -> str:
|
||||||
|
parts = text.split("---", 2)
|
||||||
|
if len(parts) < 2:
|
||||||
|
return ""
|
||||||
|
lines = parts[1].splitlines()
|
||||||
|
for index, line in enumerate(lines):
|
||||||
|
if re.match(r"^\s+openclaw:\s*$", line):
|
||||||
|
for inner in lines[index + 1 :]:
|
||||||
|
if inner.strip() and inner[0] not in (" ", "\t"):
|
||||||
|
break
|
||||||
|
match = re.match(r"^(\s+)slug:\s*(.+)\s*$", inner)
|
||||||
|
if match:
|
||||||
|
return match.group(2).strip().strip("\"'")
|
||||||
|
return ""
|
||||||
|
|
||||||
|
|
||||||
|
def parse_skill_frontmatter(skill_root: str) -> dict[str, str]:
|
||||||
|
path = os.path.join(skill_root, "SKILL.md")
|
||||||
|
with open(path, encoding="utf-8") as f:
|
||||||
|
text = f.read()
|
||||||
|
return {
|
||||||
|
"name": _parse_frontmatter_field(text, "name"),
|
||||||
|
"slug": _parse_openclaw_slug(text),
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def validate_skill_frontmatter(skill_root: str, *, allow_template_placeholders: bool = True) -> ValidationReport:
|
||||||
|
report = ValidationReport()
|
||||||
|
data = parse_skill_frontmatter(skill_root)
|
||||||
|
name = str(data.get("name") or "").strip()
|
||||||
|
slug = str(data.get("slug") or "").strip()
|
||||||
|
|
||||||
|
if not name:
|
||||||
|
report.errors.append("SKILL.md frontmatter.name 缺失")
|
||||||
|
elif not _has_cjk(name):
|
||||||
|
if allow_template_placeholders and slug in _TEMPLATE_PLACEHOLDER_SLUGS:
|
||||||
|
report.warnings.append("SKILL.md frontmatter.name 建议使用清晰的中文业务名称(模板占位可暂保留)")
|
||||||
|
else:
|
||||||
|
report.errors.append("SKILL.md frontmatter.name 应使用用户可见的中文业务名称")
|
||||||
|
else:
|
||||||
|
for pattern in _ENGLISH_PLACEHOLDER_NAME_PATTERNS:
|
||||||
|
if pattern.fullmatch(name):
|
||||||
|
if allow_template_placeholders and slug in _TEMPLATE_PLACEHOLDER_SLUGS:
|
||||||
|
report.warnings.append(f"SKILL.md frontmatter.name 仍为占位风格:{name!r}")
|
||||||
|
else:
|
||||||
|
report.errors.append(f"SKILL.md frontmatter.name 不应为英文占位名:{name!r}")
|
||||||
|
break
|
||||||
|
|
||||||
|
if not slug:
|
||||||
|
report.errors.append("SKILL.md metadata.openclaw.slug 缺失")
|
||||||
|
elif not _KEBAB_SLUG.fullmatch(slug):
|
||||||
|
report.errors.append(f"metadata.openclaw.slug 必须为 kebab-case:{slug!r}")
|
||||||
|
elif slug in _TEMPLATE_PLACEHOLDER_SLUGS and not allow_template_placeholders:
|
||||||
|
report.errors.append(f"metadata.openclaw.slug 仍为模板占位:{slug!r}")
|
||||||
|
|
||||||
|
constants_slug = SKILL_SLUG.strip()
|
||||||
|
if slug and constants_slug and slug != constants_slug:
|
||||||
|
report.warnings.append(
|
||||||
|
f"SKILL.md slug ({slug!r}) 与 util.constants.SKILL_SLUG ({constants_slug!r}) 不一致"
|
||||||
|
)
|
||||||
|
|
||||||
|
if slug and slug not in LEGACY_EXEMPT_SLUGS and _KEBAB_SLUG.fullmatch(slug):
|
||||||
|
naming_errors, naming_warnings = validate_slug_semantics(
|
||||||
|
slug,
|
||||||
|
strict=not allow_template_placeholders,
|
||||||
|
)
|
||||||
|
report.errors.extend(naming_errors)
|
||||||
|
report.warnings.extend(naming_warnings)
|
||||||
|
|
||||||
|
return report
|
||||||
|
|
||||||
|
|
||||||
|
def _quote_identifier(name: str) -> str:
|
||||||
|
return '"' + name.replace('"', '""') + '"'
|
||||||
|
|
||||||
|
|
||||||
|
def _table_column_info(cursor, table_name: str) -> dict[str, dict[str, object]]:
|
||||||
|
cursor.execute(f"PRAGMA table_info({_quote_identifier(table_name)})")
|
||||||
|
info: dict[str, dict[str, object]] = {}
|
||||||
|
for row in cursor.fetchall():
|
||||||
|
info[str(row[1])] = {
|
||||||
|
"cid": int(row[0]),
|
||||||
|
"name": str(row[1]),
|
||||||
|
"type": str(row[2] or ""),
|
||||||
|
"notnull": int(row[3] or 0),
|
||||||
|
"dflt_value": row[4],
|
||||||
|
"pk": int(row[5] or 0),
|
||||||
|
}
|
||||||
|
return info
|
||||||
|
|
||||||
|
|
||||||
|
def _table_column_xinfo(cursor, table_name: str) -> dict[str, dict[str, object]]:
|
||||||
|
"""PRAGMA table_xinfo 可识别 generated/hidden 字段(SQLite 3.31+)。"""
|
||||||
|
cursor.execute(f"PRAGMA table_xinfo({_quote_identifier(table_name)})")
|
||||||
|
info: dict[str, dict[str, object]] = {}
|
||||||
|
for row in cursor.fetchall():
|
||||||
|
hidden = int(row[6] or 0) if len(row) > 6 else 0
|
||||||
|
info[str(row[1])] = {
|
||||||
|
"cid": int(row[0]),
|
||||||
|
"name": str(row[1]),
|
||||||
|
"type": str(row[2] or ""),
|
||||||
|
"notnull": int(row[3] or 0),
|
||||||
|
"dflt_value": row[4],
|
||||||
|
"pk": int(row[5] or 0),
|
||||||
|
"hidden": hidden,
|
||||||
|
}
|
||||||
|
return info
|
||||||
|
|
||||||
|
|
||||||
|
def _is_generated_column(xinfo: dict[str, object]) -> bool:
|
||||||
|
hidden = int(xinfo.get("hidden") or 0)
|
||||||
|
return hidden in (2, 3)
|
||||||
|
|
||||||
|
|
||||||
|
def _is_blob_column(column_info: dict[str, object]) -> bool:
|
||||||
|
return "BLOB" in str(column_info.get("type") or "").upper()
|
||||||
|
|
||||||
|
|
||||||
|
def _default_uses_unixepoch(default_value: object) -> bool:
|
||||||
|
if default_value is None:
|
||||||
|
return False
|
||||||
|
text = str(default_value).strip().lower()
|
||||||
|
return "unixepoch" in text
|
||||||
|
|
||||||
|
|
||||||
|
def validate_options_json(options_json: str | None) -> tuple[list[str], list[str]]:
|
||||||
|
"""校验枚举 options_json 可被解析为 [{value, label}, ...] 结构。"""
|
||||||
|
errors: list[str] = []
|
||||||
|
warnings: list[str] = []
|
||||||
|
if options_json is None or str(options_json).strip() == "":
|
||||||
|
return errors, warnings
|
||||||
|
try:
|
||||||
|
parsed = json.loads(str(options_json))
|
||||||
|
except json.JSONDecodeError as exc:
|
||||||
|
errors.append(f"options_json 不是合法 JSON:{exc}")
|
||||||
|
return errors, warnings
|
||||||
|
if not isinstance(parsed, list):
|
||||||
|
errors.append("options_json 必须是 JSON 数组")
|
||||||
|
return errors, warnings
|
||||||
|
for index, item in enumerate(parsed):
|
||||||
|
if not isinstance(item, dict):
|
||||||
|
errors.append(f"options_json[{index}] 必须是对象")
|
||||||
|
continue
|
||||||
|
if "value" not in item or "label" not in item:
|
||||||
|
errors.append(f"options_json[{index}] 必须包含 value 与 label")
|
||||||
|
return errors, warnings
|
||||||
|
|
||||||
|
|
||||||
|
def validate_column_permissions(
|
||||||
|
cursor,
|
||||||
|
table_name: str,
|
||||||
|
*,
|
||||||
|
physical_columns: list[str] | None = None,
|
||||||
|
) -> tuple[list[str], list[str]]:
|
||||||
|
"""校验常见系统字段 editable=0,并识别技术上可写但不适合手工新增的表。"""
|
||||||
|
errors: list[str] = []
|
||||||
|
warnings: list[str] = []
|
||||||
|
ordered = physical_columns or list_physical_column_names(cursor, table_name)
|
||||||
|
column_info = _table_column_info(cursor, table_name)
|
||||||
|
column_xinfo = _table_column_xinfo(cursor, table_name)
|
||||||
|
|
||||||
|
cursor.execute(
|
||||||
|
"""
|
||||||
|
SELECT column_name, editable, options_json
|
||||||
|
FROM _jiangchang_columns
|
||||||
|
WHERE table_name = ?
|
||||||
|
""",
|
||||||
|
(table_name,),
|
||||||
|
)
|
||||||
|
meta_by_column = {
|
||||||
|
str(row[0]): {"editable": row[1], "options_json": row[2]}
|
||||||
|
for row in cursor.fetchall()
|
||||||
|
}
|
||||||
|
|
||||||
|
for column_name, meta in meta_by_column.items():
|
||||||
|
physical = column_info.get(column_name) or {}
|
||||||
|
xinfo = column_xinfo.get(column_name) or physical
|
||||||
|
if not physical and column_name not in column_xinfo:
|
||||||
|
continue
|
||||||
|
|
||||||
|
pk = int(physical.get("pk") or xinfo.get("pk") or 0)
|
||||||
|
editable = meta["editable"]
|
||||||
|
if pk and editable is not None and int(editable) != 0:
|
||||||
|
errors.append(f"{table_name}.{column_name} 是主键,editable 应为 0")
|
||||||
|
|
||||||
|
if column_name == "id" and editable is not None and int(editable) != 0:
|
||||||
|
errors.append(f"{table_name}.id 的 editable 应为 0(自增主键)")
|
||||||
|
|
||||||
|
if _is_generated_column(xinfo) and editable is not None and int(editable) != 0:
|
||||||
|
errors.append(f"{table_name}.{column_name} 是 generated 字段,editable 应为 0")
|
||||||
|
|
||||||
|
if _is_blob_column(physical or xinfo) and editable is not None and int(editable) != 0:
|
||||||
|
errors.append(f"{table_name}.{column_name} 是 BLOB 字段,editable 应为 0")
|
||||||
|
|
||||||
|
opt_errors, _ = validate_options_json(meta.get("options_json"))
|
||||||
|
for item in opt_errors:
|
||||||
|
errors.append(f"{table_name}.{column_name}: {item}")
|
||||||
|
|
||||||
|
cursor.execute(
|
||||||
|
"SELECT readonly FROM _jiangchang_tables WHERE table_name = ?",
|
||||||
|
(table_name,),
|
||||||
|
)
|
||||||
|
readonly_row = cursor.fetchone()
|
||||||
|
readonly = int(readonly_row[0]) if readonly_row else None
|
||||||
|
editable_columns = list_editable_business_columns(cursor, table_name)
|
||||||
|
if readonly == 0 and not editable_columns:
|
||||||
|
warnings.append(
|
||||||
|
f"{table_name}: 表级 readonly=0 但所有字段 editable=0,"
|
||||||
|
"宿主不应打开空白新增表单;数据应由技能业务流程产生"
|
||||||
|
)
|
||||||
|
if readonly == 1 and editable_columns:
|
||||||
|
warnings.append(
|
||||||
|
f"{table_name}: 表级 readonly=1 但存在 editable=1 字段,"
|
||||||
|
"宿主应以表级只读为准"
|
||||||
|
)
|
||||||
|
|
||||||
|
return errors, warnings
|
||||||
|
|
||||||
|
|
||||||
|
def _expected_timestamp_display_name(column_name: str) -> str:
|
||||||
|
if column_name == "created_at":
|
||||||
|
return "创建时间"
|
||||||
|
if column_name == "updated_at":
|
||||||
|
return "更新时间"
|
||||||
|
return ""
|
||||||
|
|
||||||
|
|
||||||
|
def validate_standard_timestamp_columns(
|
||||||
|
cursor,
|
||||||
|
table_name: str,
|
||||||
|
*,
|
||||||
|
physical_columns: list[str] | None = None,
|
||||||
|
) -> tuple[list[str], list[str]]:
|
||||||
|
errors: list[str] = []
|
||||||
|
warnings: list[str] = []
|
||||||
|
ordered = physical_columns or list_physical_column_names(cursor, table_name)
|
||||||
|
column_info = _table_column_info(cursor, table_name)
|
||||||
|
|
||||||
|
for column_name in STANDARD_TIMESTAMP_COLUMNS:
|
||||||
|
if column_name not in ordered:
|
||||||
|
continue
|
||||||
|
|
||||||
|
physical = column_info.get(column_name)
|
||||||
|
if not physical:
|
||||||
|
errors.append(f"{table_name}.{column_name} 缺少物理字段定义")
|
||||||
|
continue
|
||||||
|
|
||||||
|
if "INT" not in str(physical["type"]).upper():
|
||||||
|
errors.append(
|
||||||
|
f"{table_name}.{column_name} 字段类型应为 INTEGER(Unix 秒级时间戳),当前为 {physical['type']!r}"
|
||||||
|
)
|
||||||
|
|
||||||
|
if column_name == "created_at" and not _default_uses_unixepoch(physical["dflt_value"]):
|
||||||
|
errors.append(
|
||||||
|
f"{table_name}.created_at 应设置数据库默认值 DEFAULT (unixepoch()),当前默认值为 {physical['dflt_value']!r}"
|
||||||
|
)
|
||||||
|
if column_name == "updated_at" and not _default_uses_unixepoch(physical["dflt_value"]):
|
||||||
|
errors.append(
|
||||||
|
f"{table_name}.updated_at 应设置数据库初始默认值 DEFAULT (unixepoch()),当前默认值为 {physical['dflt_value']!r}"
|
||||||
|
)
|
||||||
|
|
||||||
|
cursor.execute(
|
||||||
|
"""
|
||||||
|
SELECT display_name, display_type, editable, searchable, display_order
|
||||||
|
FROM _jiangchang_columns
|
||||||
|
WHERE table_name = ? AND column_name = ?
|
||||||
|
""",
|
||||||
|
(table_name, column_name),
|
||||||
|
)
|
||||||
|
meta_row = cursor.fetchone()
|
||||||
|
if not meta_row:
|
||||||
|
errors.append(f"{table_name}.{column_name} 缺少 _jiangchang_columns 元数据")
|
||||||
|
continue
|
||||||
|
|
||||||
|
display_name, display_type, editable, searchable, display_order = meta_row
|
||||||
|
expected_name = _expected_timestamp_display_name(column_name)
|
||||||
|
if str(display_name or "").strip() != expected_name:
|
||||||
|
errors.append(
|
||||||
|
f"{table_name}.{column_name} 的 display_name 应为 {expected_name!r},当前为 {display_name!r}"
|
||||||
|
)
|
||||||
|
if str(display_type or "").strip() != DATETIME_UNIX_SECONDS:
|
||||||
|
errors.append(
|
||||||
|
f"{table_name}.{column_name} 的 display_type 应为 {DATETIME_UNIX_SECONDS!r},当前为 {display_type!r}"
|
||||||
|
)
|
||||||
|
if editable is None or int(editable) != 0:
|
||||||
|
errors.append(f"{table_name}.{column_name} 的 editable 应为 0(系统时间字段不可编辑)")
|
||||||
|
if display_order is not None:
|
||||||
|
errors.append(f"{table_name}.{column_name} 的 display_order 应为 NULL")
|
||||||
|
if searchable is not None and int(searchable) != 0:
|
||||||
|
warnings.append(f"{table_name}.{column_name} 的时间字段通常不必开启 searchable")
|
||||||
|
|
||||||
|
if "created_at" in ordered and "updated_at" in ordered:
|
||||||
|
if ordered.index("updated_at") < ordered.index("created_at"):
|
||||||
|
warnings.append(f"{table_name}: updated_at 不应出现在 created_at 之前")
|
||||||
|
|
||||||
|
if "updated_at" in ordered and not has_updated_at_trigger(cursor, table_name):
|
||||||
|
errors.append(
|
||||||
|
f"{table_name}.updated_at 缺少自动维护 trigger(期望 {table_name}_set_updated_at)"
|
||||||
|
)
|
||||||
|
|
||||||
|
return errors, warnings
|
||||||
|
|
||||||
|
|
||||||
|
def column_order_warnings(table_name: str, ordered_names: list[str]) -> list[str]:
|
||||||
|
warnings: list[str] = []
|
||||||
|
if "id" in ordered_names and ordered_names.index("id") != 0:
|
||||||
|
warnings.append(f"{table_name}: 字段 id 通常应定义在 CREATE TABLE 的第一列")
|
||||||
|
if "created_at" in ordered_names:
|
||||||
|
idx = ordered_names.index("created_at")
|
||||||
|
trailing = ordered_names[idx + 1 :]
|
||||||
|
if trailing and trailing != ["updated_at"]:
|
||||||
|
warnings.append(
|
||||||
|
f"{table_name}: created_at 之后通常只应跟 updated_at,不应再放置其他业务字段"
|
||||||
|
)
|
||||||
|
if "updated_at" in ordered_names and "created_at" in ordered_names:
|
||||||
|
if ordered_names.index("updated_at") < ordered_names.index("created_at"):
|
||||||
|
warnings.append(f"{table_name}: updated_at 不应出现在 created_at 之前")
|
||||||
|
return warnings
|
||||||
|
|
||||||
|
|
||||||
|
def _table_exists(cursor, table_name: str) -> bool:
|
||||||
|
cursor.execute(
|
||||||
|
"SELECT 1 FROM sqlite_master WHERE type = 'table' AND name = ? LIMIT 1",
|
||||||
|
(table_name,),
|
||||||
|
)
|
||||||
|
return cursor.fetchone() is not None
|
||||||
|
|
||||||
|
|
||||||
|
def validate_display_metadata(conn: sqlite3.Connection, *, check_column_order: bool = True) -> ValidationReport:
|
||||||
|
report = ValidationReport()
|
||||||
|
cur = conn.cursor()
|
||||||
|
|
||||||
|
has_tables_meta = _table_exists(cur, "_jiangchang_tables")
|
||||||
|
has_columns_meta = _table_exists(cur, "_jiangchang_columns")
|
||||||
|
if not has_tables_meta:
|
||||||
|
report.errors.append("缺少数据管理元数据表 _jiangchang_tables")
|
||||||
|
if not has_columns_meta:
|
||||||
|
report.errors.append("缺少数据管理元数据表 _jiangchang_columns")
|
||||||
|
if not has_tables_meta or not has_columns_meta:
|
||||||
|
return report
|
||||||
|
|
||||||
|
for table_name in iter_user_tables(cur):
|
||||||
|
cur.execute(
|
||||||
|
"SELECT display_name FROM _jiangchang_tables WHERE table_name = ?",
|
||||||
|
(table_name,),
|
||||||
|
)
|
||||||
|
row = cur.fetchone()
|
||||||
|
if not row or not str(row[0] or "").strip():
|
||||||
|
report.errors.append(f"业务表 {table_name!r} 缺少 _jiangchang_tables.display_name")
|
||||||
|
elif not _has_cjk(str(row[0])):
|
||||||
|
report.errors.append(f"业务表 {table_name!r} 的 display_name 应使用中文业务名称")
|
||||||
|
|
||||||
|
physical_columns = list_physical_column_names(cur, table_name)
|
||||||
|
if check_column_order:
|
||||||
|
report.warnings.extend(column_order_warnings(table_name, physical_columns))
|
||||||
|
|
||||||
|
for column_name in physical_columns:
|
||||||
|
cur.execute(
|
||||||
|
"""
|
||||||
|
SELECT display_name, visible
|
||||||
|
FROM _jiangchang_columns
|
||||||
|
WHERE table_name = ? AND column_name = ?
|
||||||
|
""",
|
||||||
|
(table_name, column_name),
|
||||||
|
)
|
||||||
|
col_row = cur.fetchone()
|
||||||
|
if not col_row:
|
||||||
|
report.errors.append(
|
||||||
|
f"字段 {table_name}.{column_name} 缺少 _jiangchang_columns 元数据"
|
||||||
|
)
|
||||||
|
continue
|
||||||
|
display_name = str(col_row[0] or "").strip()
|
||||||
|
visible = col_row[1] is None or int(col_row[1]) != 0
|
||||||
|
if visible and not display_name:
|
||||||
|
report.errors.append(f"字段 {table_name}.{column_name} 的 display_name 为空")
|
||||||
|
elif visible and _looks_like_raw_physical_name(display_name, column_name):
|
||||||
|
report.errors.append(
|
||||||
|
f"字段 {table_name}.{column_name} 的 display_name 不应直接等于英文物理名:{display_name!r}"
|
||||||
|
)
|
||||||
|
elif visible and not _has_cjk(display_name):
|
||||||
|
report.errors.append(
|
||||||
|
f"字段 {table_name}.{column_name} 的 display_name 应使用中文:{display_name!r}"
|
||||||
|
)
|
||||||
|
|
||||||
|
ts_errors, ts_warnings = validate_standard_timestamp_columns(
|
||||||
|
cur,
|
||||||
|
table_name,
|
||||||
|
physical_columns=physical_columns,
|
||||||
|
)
|
||||||
|
report.errors.extend(ts_errors)
|
||||||
|
report.warnings.extend(ts_warnings)
|
||||||
|
|
||||||
|
perm_errors, perm_warnings = validate_column_permissions(
|
||||||
|
cur,
|
||||||
|
table_name,
|
||||||
|
physical_columns=physical_columns,
|
||||||
|
)
|
||||||
|
report.errors.extend(perm_errors)
|
||||||
|
report.warnings.extend(perm_warnings)
|
||||||
|
|
||||||
|
cur.execute("SELECT table_name, column_name FROM _jiangchang_columns")
|
||||||
|
for table_name, column_name in cur.fetchall():
|
||||||
|
if table_name in METADATA_TABLES:
|
||||||
|
continue
|
||||||
|
cur.execute(
|
||||||
|
"SELECT 1 FROM sqlite_master WHERE type='table' AND name = ?",
|
||||||
|
(table_name,),
|
||||||
|
)
|
||||||
|
if not cur.fetchone():
|
||||||
|
report.errors.append(f"元数据引用了不存在的表:{table_name!r}")
|
||||||
|
continue
|
||||||
|
physical = list_physical_column_names(cur, str(table_name))
|
||||||
|
if str(column_name) not in physical:
|
||||||
|
report.errors.append(f"元数据引用了不存在的字段:{table_name}.{column_name}")
|
||||||
|
|
||||||
|
cur.execute("SELECT table_name FROM _jiangchang_tables")
|
||||||
|
for (table_name,) in cur.fetchall():
|
||||||
|
if table_name in METADATA_TABLES:
|
||||||
|
continue
|
||||||
|
cur.execute(
|
||||||
|
"SELECT 1 FROM sqlite_master WHERE type='table' AND name = ?",
|
||||||
|
(table_name,),
|
||||||
|
)
|
||||||
|
if not cur.fetchone():
|
||||||
|
report.errors.append(f"元数据引用了不存在的表:{table_name!r}")
|
||||||
|
|
||||||
|
return report
|
||||||
|
|
||||||
|
|
||||||
|
def validate_template_database(conn: sqlite3.Connection) -> ValidationReport:
|
||||||
|
report = validate_display_metadata(conn)
|
||||||
|
physical = list_physical_column_names(conn.cursor(), TASK_LOGS_TABLE)
|
||||||
|
expected = [str(item["column_name"]) for item in TASK_LOGS_COLUMN_METADATA]
|
||||||
|
if physical != expected:
|
||||||
|
report.errors.append(
|
||||||
|
f"task_logs 物理字段顺序应为 {expected},当前为 {physical}"
|
||||||
|
)
|
||||||
|
return report
|
||||||
@@ -9,7 +9,6 @@ from __future__ import annotations
|
|||||||
from typing import Any, List, Optional, Tuple
|
from typing import Any, List, Optional, Tuple
|
||||||
|
|
||||||
from db.connection import get_conn, init_db
|
from db.connection import get_conn, init_db
|
||||||
from util.timeutil import now_unix
|
|
||||||
|
|
||||||
|
|
||||||
def save_task_log(
|
def save_task_log(
|
||||||
@@ -22,17 +21,16 @@ def save_task_log(
|
|||||||
result_summary: Optional[str] = None,
|
result_summary: Optional[str] = None,
|
||||||
) -> int:
|
) -> int:
|
||||||
init_db()
|
init_db()
|
||||||
now = now_unix()
|
|
||||||
conn = get_conn()
|
conn = get_conn()
|
||||||
try:
|
try:
|
||||||
cur = conn.cursor()
|
cur = conn.cursor()
|
||||||
cur.execute(
|
cur.execute(
|
||||||
"""
|
"""
|
||||||
INSERT INTO task_logs
|
INSERT INTO task_logs
|
||||||
(task_type, target_id, input_id, input_title, status, error_msg, result_summary, created_at, updated_at)
|
(task_type, target_id, input_id, input_title, status, error_msg, result_summary)
|
||||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
|
VALUES (?, ?, ?, ?, ?, ?, ?)
|
||||||
""",
|
""",
|
||||||
(task_type, target_id, input_id, input_title or "", status, error_msg, result_summary, now, now),
|
(task_type, target_id, input_id, input_title or "", status, error_msg, result_summary),
|
||||||
)
|
)
|
||||||
new_id = int(cur.lastrowid)
|
new_id = int(cur.lastrowid)
|
||||||
conn.commit()
|
conn.commit()
|
||||||
|
|||||||
36
scripts/db/timestamp_columns.py
Normal file
36
scripts/db/timestamp_columns.py
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
"""标准时间字段:Unix 秒级时间戳、默认值与 updated_at 自动维护。"""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
STANDARD_TIMESTAMP_COLUMNS = frozenset({"created_at", "updated_at"})
|
||||||
|
DATETIME_UNIX_SECONDS = "datetime_unix_seconds"
|
||||||
|
TASK_LOGS_UPDATED_AT_TRIGGER = "task_logs_set_updated_at"
|
||||||
|
|
||||||
|
_TASK_LOGS_UPDATED_AT_TRIGGER_DDL = f"""
|
||||||
|
CREATE TRIGGER IF NOT EXISTS {TASK_LOGS_UPDATED_AT_TRIGGER}
|
||||||
|
AFTER UPDATE ON task_logs
|
||||||
|
FOR EACH ROW
|
||||||
|
BEGIN
|
||||||
|
UPDATE task_logs
|
||||||
|
SET updated_at = unixepoch()
|
||||||
|
WHERE id = NEW.id AND updated_at = OLD.updated_at;
|
||||||
|
END
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
def init_task_logs_timestamp_maintenance(cursor) -> None:
|
||||||
|
"""幂等创建 task_logs.updated_at 自动维护 trigger。"""
|
||||||
|
cursor.execute(_TASK_LOGS_UPDATED_AT_TRIGGER_DDL)
|
||||||
|
|
||||||
|
|
||||||
|
def has_updated_at_trigger(cursor, table_name: str) -> bool:
|
||||||
|
trigger_name = f"{table_name}_set_updated_at"
|
||||||
|
cursor.execute(
|
||||||
|
"""
|
||||||
|
SELECT 1 FROM sqlite_master
|
||||||
|
WHERE type = 'trigger' AND name = ? AND tbl_name = ?
|
||||||
|
LIMIT 1
|
||||||
|
""",
|
||||||
|
(trigger_name, table_name),
|
||||||
|
)
|
||||||
|
return cursor.fetchone() is not None
|
||||||
@@ -2,29 +2,29 @@
|
|||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
import os
|
|
||||||
from typing import Tuple
|
from typing import Tuple
|
||||||
|
|
||||||
import requests
|
import requests
|
||||||
|
from jiangchang_skill_core import config
|
||||||
|
|
||||||
|
|
||||||
def check_entitlement(skill_slug: str) -> Tuple[bool, str]:
|
def check_entitlement(skill_slug: str) -> Tuple[bool, str]:
|
||||||
auth_base = (os.getenv("JIANGCHANG_AUTH_BASE_URL") or "").strip().rstrip("/")
|
auth_base = (config.get("JIANGCHANG_AUTH_BASE_URL") or "").strip().rstrip("/")
|
||||||
if not auth_base:
|
if not auth_base:
|
||||||
return True, ""
|
return True, ""
|
||||||
user_id = (os.getenv("CLAW_USER_ID") or os.getenv("JIANGCHANG_USER_ID") or "").strip()
|
user_id = (config.get("JIANGCHANG_USER_ID") or "").strip()
|
||||||
if not user_id:
|
if not user_id:
|
||||||
return False, "鉴权失败:缺少用户身份(CLAW_USER_ID / JIANGCHANG_USER_ID)"
|
return False, "鉴权失败:缺少用户身份(JIANGCHANG_USER_ID)"
|
||||||
|
|
||||||
auth_api_key = (os.getenv("JIANGCHANG_AUTH_API_KEY") or "").strip()
|
auth_api_key = (config.get("JIANGCHANG_AUTH_API_KEY") or "").strip()
|
||||||
timeout = int((os.getenv("JIANGCHANG_AUTH_TIMEOUT_SECONDS") or "5").strip())
|
timeout = int((config.get("JIANGCHANG_AUTH_TIMEOUT_SECONDS") or "5").strip())
|
||||||
headers = {"Content-Type": "application/json"}
|
headers = {"Content-Type": "application/json"}
|
||||||
if auth_api_key:
|
if auth_api_key:
|
||||||
headers["Authorization"] = f"Bearer {auth_api_key}"
|
headers["Authorization"] = f"Bearer {auth_api_key}"
|
||||||
payload = {
|
payload = {
|
||||||
"user_id": user_id,
|
"user_id": user_id,
|
||||||
"skill_slug": skill_slug,
|
"skill_slug": skill_slug,
|
||||||
"trace_id": (os.getenv("JIANGCHANG_TRACE_ID") or "").strip(),
|
"trace_id": (config.get("JIANGCHANG_TRACE_ID") or "").strip(),
|
||||||
"context": {"entry": "main.py"},
|
"context": {"entry": "main.py"},
|
||||||
}
|
}
|
||||||
try:
|
try:
|
||||||
|
|||||||
@@ -1,26 +0,0 @@
|
|||||||
"""示例 adapter 四档 dispatch。
|
|
||||||
|
|
||||||
由 ``OPENCLAW_TEST_TARGET``(或 ``config.get``)决定档位。
|
|
||||||
复制本目录后把 ``example_adapter`` 改名为 ``<domain>_adapter``。
|
|
||||||
"""
|
|
||||||
|
|
||||||
from __future__ import annotations
|
|
||||||
|
|
||||||
import os
|
|
||||||
|
|
||||||
from .mock import MockAdapter
|
|
||||||
from .real_api import RealApiAdapter
|
|
||||||
from .real_rpa import RealRpaAdapter
|
|
||||||
from .sim_rpa import SimRpaAdapter
|
|
||||||
|
|
||||||
|
|
||||||
def get_adapter():
|
|
||||||
"""返回当前档位 adapter 实例。"""
|
|
||||||
target = (os.environ.get("OPENCLAW_TEST_TARGET") or "simulator_rpa").strip().lower()
|
|
||||||
if target in ("unit", "mock"):
|
|
||||||
return MockAdapter()
|
|
||||||
if target == "real_api":
|
|
||||||
return RealApiAdapter()
|
|
||||||
if target == "real_rpa":
|
|
||||||
return RealRpaAdapter()
|
|
||||||
return SimRpaAdapter()
|
|
||||||
@@ -1,37 +0,0 @@
|
|||||||
"""示例适配器数据契约与基类。
|
|
||||||
|
|
||||||
复制本目录为 ``<domain>_adapter/``,按 ADAPTER.md 实现各档位。
|
|
||||||
业务逻辑只依赖 ``base`` 里的接口,不感知 mock / sim_rpa / real_* 差异。
|
|
||||||
"""
|
|
||||||
|
|
||||||
from __future__ import annotations
|
|
||||||
|
|
||||||
from dataclasses import dataclass, field
|
|
||||||
from typing import Any, Dict, List, Optional
|
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
|
||||||
class ExampleItem:
|
|
||||||
"""单条业务输入(示例)。"""
|
|
||||||
|
|
||||||
id: str
|
|
||||||
label: str
|
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
|
||||||
class ExampleResult:
|
|
||||||
"""批量操作结果(以批为单位返回)。"""
|
|
||||||
|
|
||||||
ok: bool
|
|
||||||
serial_no: Optional[str]
|
|
||||||
error_msg: Optional[str]
|
|
||||||
artifacts: Dict[str, Any] = field(default_factory=dict)
|
|
||||||
|
|
||||||
|
|
||||||
class AdapterBase:
|
|
||||||
"""四档 adapter 统一接口;子类实现 ``do_batch``。"""
|
|
||||||
|
|
||||||
name = "base"
|
|
||||||
|
|
||||||
def do_batch(self, target: str, items: List[ExampleItem]) -> ExampleResult:
|
|
||||||
raise NotImplementedError
|
|
||||||
@@ -1,24 +0,0 @@
|
|||||||
"""离线 mock 档位:纯内存仿真,CI / 单测必跑。"""
|
|
||||||
|
|
||||||
from __future__ import annotations
|
|
||||||
|
|
||||||
from .base import AdapterBase, ExampleItem, ExampleResult
|
|
||||||
|
|
||||||
|
|
||||||
class MockAdapter(AdapterBase):
|
|
||||||
name = "mock"
|
|
||||||
|
|
||||||
def do_batch(self, target: str, items: list[ExampleItem]) -> ExampleResult:
|
|
||||||
if not items:
|
|
||||||
return ExampleResult(
|
|
||||||
ok=False,
|
|
||||||
serial_no=None,
|
|
||||||
error_msg="empty batch",
|
|
||||||
artifacts={},
|
|
||||||
)
|
|
||||||
return ExampleResult(
|
|
||||||
ok=True,
|
|
||||||
serial_no=f"MOCK-{len(items)}",
|
|
||||||
error_msg=None,
|
|
||||||
artifacts={"mode": "mock", "target": target, "count": len(items)},
|
|
||||||
)
|
|
||||||
@@ -1,14 +0,0 @@
|
|||||||
"""真实 API 档位占位:有官方接口时在此实现。"""
|
|
||||||
|
|
||||||
from __future__ import annotations
|
|
||||||
|
|
||||||
from .base import AdapterBase, ExampleItem, ExampleResult
|
|
||||||
|
|
||||||
|
|
||||||
class RealApiAdapter(AdapterBase):
|
|
||||||
name = "real_api"
|
|
||||||
|
|
||||||
def do_batch(self, target: str, items: list[ExampleItem]) -> ExampleResult:
|
|
||||||
raise NotImplementedError(
|
|
||||||
"RealApiAdapter: 复制 example_adapter 后在此对接真实 API"
|
|
||||||
)
|
|
||||||
@@ -1,14 +0,0 @@
|
|||||||
"""真实 RPA 档位占位:无 API 时最后手段,谨慎实现。"""
|
|
||||||
|
|
||||||
from __future__ import annotations
|
|
||||||
|
|
||||||
from .base import AdapterBase, ExampleItem, ExampleResult
|
|
||||||
|
|
||||||
|
|
||||||
class RealRpaAdapter(AdapterBase):
|
|
||||||
name = "real_rpa"
|
|
||||||
|
|
||||||
def do_batch(self, target: str, items: list[ExampleItem]) -> ExampleResult:
|
|
||||||
raise NotImplementedError(
|
|
||||||
"RealRpaAdapter: 复制 example_adapter 后在此实现生产界面 RPA"
|
|
||||||
)
|
|
||||||
@@ -1,108 +0,0 @@
|
|||||||
"""仿真 RPA 档位:演示 RpaVideoSession + launch_persistent_browser(需浏览器,默认开发档)。"""
|
|
||||||
|
|
||||||
from __future__ import annotations
|
|
||||||
|
|
||||||
import asyncio
|
|
||||||
import os
|
|
||||||
from datetime import datetime
|
|
||||||
|
|
||||||
from jiangchang_skill_core import config
|
|
||||||
from jiangchang_skill_core.rpa import anti_detect, artifacts, errors, launch_persistent_browser
|
|
||||||
from jiangchang_skill_core.rpa.human_login import wait_for_captcha_pass
|
|
||||||
from jiangchang_skill_core.rpa.video_session import RpaVideoSession
|
|
||||||
|
|
||||||
from util.constants import SKILL_SLUG
|
|
||||||
from util.runtime_paths import get_skill_data_dir, get_skill_root
|
|
||||||
|
|
||||||
from .base import AdapterBase, ExampleItem, ExampleResult
|
|
||||||
|
|
||||||
|
|
||||||
class SimRpaAdapter(AdapterBase):
|
|
||||||
name = "sim_rpa"
|
|
||||||
|
|
||||||
def do_batch(self, target: str, items: list[ExampleItem]) -> ExampleResult:
|
|
||||||
return asyncio.run(self._do_batch_async(target, items))
|
|
||||||
|
|
||||||
async def _do_batch_async(self, target: str, items: list[ExampleItem]) -> ExampleResult:
|
|
||||||
example_path = os.path.join(get_skill_root(), ".env.example")
|
|
||||||
config.ensure_env_file(SKILL_SLUG, example_path)
|
|
||||||
|
|
||||||
url = config.get("TARGET_BASE_URL") or target or "https://example.com"
|
|
||||||
batch_id = f"sim_{datetime.now().strftime('%Y%m%d_%H%M%S')}"
|
|
||||||
data_dir = get_skill_data_dir()
|
|
||||||
profile_dir = os.path.join(data_dir, "browser-profile")
|
|
||||||
os.makedirs(profile_dir, exist_ok=True)
|
|
||||||
|
|
||||||
try:
|
|
||||||
from playwright.async_api import async_playwright
|
|
||||||
except ImportError:
|
|
||||||
return ExampleResult(
|
|
||||||
ok=False,
|
|
||||||
serial_no=None,
|
|
||||||
error_msg="playwright not installed",
|
|
||||||
artifacts={},
|
|
||||||
)
|
|
||||||
|
|
||||||
artifact_paths: dict = {}
|
|
||||||
async with RpaVideoSession(
|
|
||||||
skill_slug=SKILL_SLUG,
|
|
||||||
skill_data_dir=data_dir,
|
|
||||||
batch_id=batch_id,
|
|
||||||
title="技能仿真演示",
|
|
||||||
) as video:
|
|
||||||
video.add_step("正在打开目标页面")
|
|
||||||
try:
|
|
||||||
async with async_playwright() as pw:
|
|
||||||
context = await launch_persistent_browser(
|
|
||||||
pw,
|
|
||||||
profile_dir=profile_dir,
|
|
||||||
headless=config.get_bool("OPENCLAW_BROWSER_HEADLESS"),
|
|
||||||
)
|
|
||||||
page = context.pages[0] if context.pages else await context.new_page()
|
|
||||||
await page.goto(url, wait_until="domcontentloaded", timeout=60_000)
|
|
||||||
await anti_detect.human_mouse_wiggle(page)
|
|
||||||
|
|
||||||
search = page.locator('input[type="search"], input[name="q"]').first
|
|
||||||
if await search.count() > 0:
|
|
||||||
demo_text = items[0].label if items else "openclaw-demo"
|
|
||||||
video.step("正在填写演示内容")
|
|
||||||
await anti_detect.human_type(page, search, demo_text)
|
|
||||||
await anti_detect.human_delay_short()
|
|
||||||
|
|
||||||
video.step("正在等待人工验证(如有)")
|
|
||||||
if not await wait_for_captcha_pass(
|
|
||||||
page,
|
|
||||||
timeout_sec=config.get_int("HUMAN_WAIT_TIMEOUT", 180),
|
|
||||||
):
|
|
||||||
shot = await artifacts.capture_failure(
|
|
||||||
page, data_dir, batch_id, "captcha"
|
|
||||||
)
|
|
||||||
if shot:
|
|
||||||
artifact_paths["captcha"] = shot
|
|
||||||
return ExampleResult(
|
|
||||||
ok=False,
|
|
||||||
serial_no=None,
|
|
||||||
error_msg=errors.ERR_CAPTCHA_NEED_HUMAN,
|
|
||||||
artifacts={**artifact_paths, **video.to_artifact_dict()},
|
|
||||||
)
|
|
||||||
|
|
||||||
video.step("演示任务完成")
|
|
||||||
await context.close()
|
|
||||||
except Exception as exc:
|
|
||||||
video.step("演示异常结束")
|
|
||||||
return ExampleResult(
|
|
||||||
ok=False,
|
|
||||||
serial_no=None,
|
|
||||||
error_msg=str(exc),
|
|
||||||
artifacts={**artifact_paths, **video.to_artifact_dict()},
|
|
||||||
)
|
|
||||||
|
|
||||||
arts = {"mode": "sim_rpa", "url": url, **artifact_paths, **video.to_artifact_dict()}
|
|
||||||
if video.output_video_path:
|
|
||||||
arts["video_path"] = video.output_video_path
|
|
||||||
return ExampleResult(
|
|
||||||
ok=True,
|
|
||||||
serial_no=f"SIM-{len(items)}",
|
|
||||||
error_msg=None,
|
|
||||||
artifacts=arts,
|
|
||||||
)
|
|
||||||
@@ -1,88 +0,0 @@
|
|||||||
#!/usr/bin/env python3
|
|
||||||
# -*- coding: utf-8 -*-
|
|
||||||
"""浏览器 RPA 最小用法示例(复制到新 skill 后按需改)。
|
|
||||||
|
|
||||||
演示标准流程:
|
|
||||||
1. config.ensure_env_file 首次落盘 .env
|
|
||||||
2. launch_persistent_browser 启动 stealth persistent context
|
|
||||||
3. 拟人操作(mouse_wiggle / human_type / human_click)
|
|
||||||
4. 验证码 HITL 等待
|
|
||||||
5. 失败 capture_failure 截图
|
|
||||||
|
|
||||||
运行(需系统 Chrome/Edge + playwright 包):
|
|
||||||
python scripts/service/example_browser_rpa.py
|
|
||||||
"""
|
|
||||||
from __future__ import annotations
|
|
||||||
|
|
||||||
import asyncio
|
|
||||||
import os
|
|
||||||
import sys
|
|
||||||
|
|
||||||
_scripts_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
|
||||||
if _scripts_dir not in sys.path:
|
|
||||||
sys.path.insert(0, _scripts_dir)
|
|
||||||
|
|
||||||
from jiangchang_skill_core import config
|
|
||||||
from jiangchang_skill_core.rpa import (
|
|
||||||
anti_detect,
|
|
||||||
artifacts,
|
|
||||||
errors,
|
|
||||||
launch_persistent_browser,
|
|
||||||
wait_for_captcha_pass,
|
|
||||||
)
|
|
||||||
from util.constants import SKILL_SLUG
|
|
||||||
from util.runtime_paths import get_skill_data_dir, get_skill_root
|
|
||||||
|
|
||||||
|
|
||||||
async def demo() -> int:
|
|
||||||
example_path = os.path.join(get_skill_root(), ".env.example")
|
|
||||||
config.ensure_env_file(SKILL_SLUG, example_path)
|
|
||||||
|
|
||||||
url = config.get("TARGET_BASE_URL") or "https://example.com"
|
|
||||||
data_dir = get_skill_data_dir()
|
|
||||||
profile_dir = os.path.join(data_dir, "browser-profile")
|
|
||||||
os.makedirs(profile_dir, exist_ok=True)
|
|
||||||
batch_id = "demo"
|
|
||||||
|
|
||||||
try:
|
|
||||||
from playwright.async_api import async_playwright
|
|
||||||
except ImportError:
|
|
||||||
print("ERROR: playwright not installed")
|
|
||||||
return 1
|
|
||||||
|
|
||||||
async with async_playwright() as pw:
|
|
||||||
context = await launch_persistent_browser(
|
|
||||||
pw,
|
|
||||||
profile_dir=profile_dir,
|
|
||||||
headless=config.get_bool("OPENCLAW_BROWSER_HEADLESS"),
|
|
||||||
)
|
|
||||||
page = context.pages[0] if context.pages else await context.new_page()
|
|
||||||
try:
|
|
||||||
await page.goto(url, wait_until="domcontentloaded", timeout=60_000)
|
|
||||||
await anti_detect.human_mouse_wiggle(page)
|
|
||||||
|
|
||||||
search = page.locator('input[type="search"], input[name="q"]').first
|
|
||||||
if await search.count() > 0:
|
|
||||||
await anti_detect.human_type(page, search, "openclaw-rpa-demo")
|
|
||||||
await anti_detect.human_click(page, selector='input[type="search"]')
|
|
||||||
|
|
||||||
if not await wait_for_captcha_pass(
|
|
||||||
page,
|
|
||||||
timeout_sec=config.get_int("HUMAN_WAIT_TIMEOUT", 180),
|
|
||||||
):
|
|
||||||
shot = await artifacts.capture_failure(page, data_dir, batch_id, "captcha")
|
|
||||||
print(errors.ERR_CAPTCHA_NEED_HUMAN, shot or "")
|
|
||||||
return 1
|
|
||||||
|
|
||||||
print("OK browser RPA demo finished")
|
|
||||||
return 0
|
|
||||||
except Exception as exc:
|
|
||||||
shot = await artifacts.capture_failure(page, data_dir, batch_id, "error")
|
|
||||||
print(f"ERROR: {exc}", shot or "")
|
|
||||||
return 1
|
|
||||||
finally:
|
|
||||||
await context.close()
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
raise SystemExit(asyncio.run(demo()))
|
|
||||||
@@ -13,20 +13,30 @@ import uuid
|
|||||||
from typing import Any, Dict, Optional
|
from typing import Any, Dict, Optional
|
||||||
|
|
||||||
from jiangchang_skill_core import collect_runtime_diagnostics, config, format_runtime_health_lines
|
from jiangchang_skill_core import collect_runtime_diagnostics, config, format_runtime_health_lines
|
||||||
from jiangchang_skill_core.rpa.video_session import RpaVideoSession
|
from jiangchang_skill_core.activity import JobStopped, emit, finish, job_context
|
||||||
|
|
||||||
from db import task_logs_repository as tlr
|
from db import task_logs_repository as tlr
|
||||||
|
from jiangchang_skill_core.rpa.video_session import RpaVideoSession
|
||||||
from service.entitlement_service import check_entitlement
|
from service.entitlement_service import check_entitlement
|
||||||
from service.task_run_support import (
|
from service.task_run_support import (
|
||||||
_print_video_summary,
|
_print_video_summary,
|
||||||
build_video_info,
|
build_video_info,
|
||||||
merge_video_into_result_summary,
|
merge_video_into_result_summary,
|
||||||
)
|
)
|
||||||
from util.constants import PLATFORM_KIT_MIN_VERSION, SKILL_SLUG, SKILL_VERSION
|
from util.constants import LOG_LOGGER_NAME, PLATFORM_KIT_MIN_VERSION, SKILL_SLUG, SKILL_VERSION
|
||||||
|
from util.logging_config import get_skill_logger, setup_skill_logging
|
||||||
from util.runtime_paths import get_skill_data_dir, get_skill_root
|
from util.runtime_paths import get_skill_data_dir, get_skill_root
|
||||||
from util.timeutil import unix_to_iso
|
from util.timeutil import unix_to_iso
|
||||||
|
|
||||||
|
|
||||||
|
def _get_task_logger():
|
||||||
|
try:
|
||||||
|
return get_skill_logger()
|
||||||
|
except RuntimeError:
|
||||||
|
setup_skill_logging(SKILL_SLUG, LOG_LOGGER_NAME)
|
||||||
|
return get_skill_logger()
|
||||||
|
|
||||||
|
|
||||||
async def _run_template_demo(target: Optional[str], input_id: Optional[str]) -> tuple[int, dict[str, Any]]:
|
async def _run_template_demo(target: Optional[str], input_id: Optional[str]) -> tuple[int, dict[str, Any]]:
|
||||||
"""最小 RpaVideoSession 示范:不启动浏览器、不执行业务,仅演示录屏包裹范式。"""
|
"""最小 RpaVideoSession 示范:不启动浏览器、不执行业务,仅演示录屏包裹范式。"""
|
||||||
batch_id = uuid.uuid4().hex[:12]
|
batch_id = uuid.uuid4().hex[:12]
|
||||||
@@ -53,34 +63,129 @@ async def _run_template_demo(target: Optional[str], input_id: Optional[str]) ->
|
|||||||
|
|
||||||
def cmd_run(target: Optional[str] = None, input_id: Optional[str] = None) -> int:
|
def cmd_run(target: Optional[str] = None, input_id: Optional[str] = None) -> int:
|
||||||
"""通用任务执行入口模板。复制后请实现真实业务逻辑。"""
|
"""通用任务执行入口模板。复制后请实现真实业务逻辑。"""
|
||||||
ok, reason = check_entitlement(SKILL_SLUG)
|
log = _get_task_logger()
|
||||||
if not ok:
|
task_type = "demo"
|
||||||
print(f"❌ {reason}")
|
|
||||||
return 1
|
|
||||||
|
|
||||||
rc, video_info = asyncio.run(_run_template_demo(target, input_id))
|
with job_context(skill=SKILL_SLUG):
|
||||||
_print_video_summary(video_info)
|
log.info(
|
||||||
|
"task_start task_type=%s target_id=%s input_id=%s",
|
||||||
|
task_type,
|
||||||
|
target,
|
||||||
|
input_id,
|
||||||
|
)
|
||||||
|
emit("开始处理任务", skill=SKILL_SLUG, stage="run")
|
||||||
|
try:
|
||||||
|
ok, reason = check_entitlement(SKILL_SLUG)
|
||||||
|
if not ok:
|
||||||
|
log.warning("entitlement_failed task_type=%s reason=%s", task_type, reason)
|
||||||
|
emit("鉴权失败,已记录诊断信息", type="warn", skill=SKILL_SLUG, stage="auth")
|
||||||
|
tlr.save_task_log(
|
||||||
|
task_type=task_type,
|
||||||
|
target_id=target,
|
||||||
|
input_id=input_id,
|
||||||
|
input_title="模板示例任务",
|
||||||
|
status="failed",
|
||||||
|
error_msg=reason,
|
||||||
|
result_summary=json.dumps({"stage": "auth", "error": reason}, ensure_ascii=False),
|
||||||
|
)
|
||||||
|
log.info(
|
||||||
|
"task_log_saved task_type=%s target_id=%s input_id=%s stage=auth status=failed",
|
||||||
|
task_type,
|
||||||
|
target,
|
||||||
|
input_id,
|
||||||
|
)
|
||||||
|
finish(
|
||||||
|
status="failed",
|
||||||
|
message=reason,
|
||||||
|
skill=SKILL_SLUG,
|
||||||
|
error_code="ENTITLEMENT_DENIED",
|
||||||
|
stage="auth",
|
||||||
|
)
|
||||||
|
print(f"❌ {reason}")
|
||||||
|
return 1
|
||||||
|
|
||||||
summary_payload = merge_video_into_result_summary(
|
log.info("task_run_demo_start target_id=%s input_id=%s", target, input_id)
|
||||||
{
|
rc, video_info = asyncio.run(_run_template_demo(target, input_id))
|
||||||
"template_demo": True,
|
log.info("task_run_demo_done target_id=%s input_id=%s status=failed", target, input_id)
|
||||||
"target": target,
|
_print_video_summary(video_info)
|
||||||
"input_id": input_id,
|
|
||||||
},
|
|
||||||
video_info,
|
|
||||||
)
|
|
||||||
tlr.save_task_log(
|
|
||||||
task_type="demo",
|
|
||||||
target_id=target,
|
|
||||||
input_id=input_id,
|
|
||||||
input_title="模板示例任务",
|
|
||||||
status="failed",
|
|
||||||
error_msg="模板仓库未实现真实业务",
|
|
||||||
result_summary=json.dumps(summary_payload, ensure_ascii=False),
|
|
||||||
)
|
|
||||||
|
|
||||||
print("❌ 这是模板仓库,请复制后在 scripts/service/task_service.py 中实现 cmd_run 的真实业务逻辑。")
|
summary_payload = merge_video_into_result_summary(
|
||||||
return rc
|
{
|
||||||
|
"template_demo": True,
|
||||||
|
"target": target,
|
||||||
|
"input_id": input_id,
|
||||||
|
},
|
||||||
|
video_info,
|
||||||
|
)
|
||||||
|
tlr.save_task_log(
|
||||||
|
task_type=task_type,
|
||||||
|
target_id=target,
|
||||||
|
input_id=input_id,
|
||||||
|
input_title="模板示例任务",
|
||||||
|
status="failed",
|
||||||
|
error_msg="模板仓库未实现真实业务",
|
||||||
|
result_summary=json.dumps(summary_payload, ensure_ascii=False),
|
||||||
|
)
|
||||||
|
log.info(
|
||||||
|
"task_log_saved task_type=%s target_id=%s input_id=%s status=failed",
|
||||||
|
task_type,
|
||||||
|
target,
|
||||||
|
input_id,
|
||||||
|
)
|
||||||
|
|
||||||
|
finish(
|
||||||
|
status="failed",
|
||||||
|
message="模板仓库未实现真实业务",
|
||||||
|
skill=SKILL_SLUG,
|
||||||
|
template_demo=True,
|
||||||
|
target=target,
|
||||||
|
input_id=input_id,
|
||||||
|
)
|
||||||
|
print("❌ 这是模板仓库,请复制后在 scripts/service/task_service.py 中实现 cmd_run 的真实业务逻辑。")
|
||||||
|
return rc
|
||||||
|
except JobStopped:
|
||||||
|
raise
|
||||||
|
except Exception:
|
||||||
|
log.exception(
|
||||||
|
"task_failed task_type=%s target_id=%s input_id=%s",
|
||||||
|
task_type,
|
||||||
|
target,
|
||||||
|
input_id,
|
||||||
|
)
|
||||||
|
emit("任务失败,已记录诊断信息", type="warn", skill=SKILL_SLUG, stage="run")
|
||||||
|
try:
|
||||||
|
tlr.save_task_log(
|
||||||
|
task_type=task_type,
|
||||||
|
target_id=target,
|
||||||
|
input_id=input_id,
|
||||||
|
input_title="模板示例任务",
|
||||||
|
status="failed",
|
||||||
|
error_msg="任务执行异常,详见统一日志",
|
||||||
|
result_summary=json.dumps(
|
||||||
|
{"stage": "run", "error": "unexpected_exception"},
|
||||||
|
ensure_ascii=False,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
log.info(
|
||||||
|
"task_log_saved task_type=%s target_id=%s input_id=%s stage=run status=failed",
|
||||||
|
task_type,
|
||||||
|
target,
|
||||||
|
input_id,
|
||||||
|
)
|
||||||
|
except Exception:
|
||||||
|
log.exception(
|
||||||
|
"task_log_save_failed task_type=%s target_id=%s input_id=%s",
|
||||||
|
task_type,
|
||||||
|
target,
|
||||||
|
input_id,
|
||||||
|
)
|
||||||
|
finish(
|
||||||
|
status="failed",
|
||||||
|
message="任务执行异常,详见统一日志",
|
||||||
|
skill=SKILL_SLUG,
|
||||||
|
stage="run",
|
||||||
|
)
|
||||||
|
return 1
|
||||||
|
|
||||||
|
|
||||||
def cmd_logs(
|
def cmd_logs(
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
"""技能标识、版本与平台公共库约束(复制后请修改 slug/version/logger)。"""
|
"""技能标识、版本与平台公共库约束(复制后请修改 slug/version/logger)。"""
|
||||||
|
|
||||||
SKILL_SLUG = "your-skill-slug"
|
SKILL_SLUG = "your-skill-slug"
|
||||||
SKILL_VERSION = "1.0.14"
|
SKILL_VERSION = "1.0.40"
|
||||||
LOG_LOGGER_NAME = "openclaw.skill.your_skill_slug"
|
LOG_LOGGER_NAME = "openclaw.skill.your_skill_slug"
|
||||||
PLATFORM_KIT_MIN_VERSION = "1.0.13"
|
PLATFORM_KIT_MIN_VERSION = "1.2.0"
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
"""数据根、技能目录、兄弟技能根路径。"""
|
"""数据根、技能目录、兄弟技能根路径与用户数据子目录解析。"""
|
||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
@@ -10,6 +10,22 @@ from util.constants import SKILL_SLUG
|
|||||||
|
|
||||||
_SCRIPTS_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
_SCRIPTS_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
||||||
|
|
||||||
|
STANDARD_SUBDIR_DOWNLOADS = "downloads"
|
||||||
|
STANDARD_SUBDIR_IMPORTS = "imports"
|
||||||
|
STANDARD_SUBDIR_EXPORTS = "exports"
|
||||||
|
STANDARD_SUBDIR_UPLOADS = "uploads"
|
||||||
|
STANDARD_SUBDIR_CACHE = "cache"
|
||||||
|
STANDARD_SUBDIR_TEMP = "temp"
|
||||||
|
STANDARD_SUBDIR_RPA_ARTIFACTS = "rpa-artifacts"
|
||||||
|
STANDARD_SUBDIR_VIDEOS = "videos"
|
||||||
|
|
||||||
|
CONFIG_KEY_DOWNLOAD_DIR = "SKILL_DOWNLOAD_DIR"
|
||||||
|
CONFIG_KEY_IMPORT_DIR = "SKILL_IMPORT_DIR"
|
||||||
|
CONFIG_KEY_EXPORT_DIR = "SKILL_EXPORT_DIR"
|
||||||
|
CONFIG_KEY_UPLOAD_DIR = "SKILL_UPLOAD_DIR"
|
||||||
|
CONFIG_KEY_CACHE_DIR = "SKILL_CACHE_DIR"
|
||||||
|
CONFIG_KEY_TEMP_DIR = "SKILL_TEMP_DIR"
|
||||||
|
|
||||||
|
|
||||||
def get_skill_root() -> str:
|
def get_skill_root() -> str:
|
||||||
return os.path.dirname(_SCRIPTS_DIR)
|
return os.path.dirname(_SCRIPTS_DIR)
|
||||||
@@ -32,3 +48,98 @@ def get_skill_data_dir() -> str:
|
|||||||
def get_db_path(filename: str | None = None) -> str:
|
def get_db_path(filename: str | None = None) -> str:
|
||||||
name = filename or f"{SKILL_SLUG}.db"
|
name = filename or f"{SKILL_SLUG}.db"
|
||||||
return os.path.join(get_skill_data_dir(), name)
|
return os.path.join(get_skill_data_dir(), name)
|
||||||
|
|
||||||
|
|
||||||
|
def _normalize_subdir(value: str) -> str:
|
||||||
|
cleaned = value.strip().strip("/\\")
|
||||||
|
if not cleaned or cleaned.startswith(".."):
|
||||||
|
raise ValueError(f"invalid skill data subdir: {value!r}")
|
||||||
|
return cleaned.replace("/", os.sep).replace("\\", os.sep)
|
||||||
|
|
||||||
|
|
||||||
|
def resolve_data_path(
|
||||||
|
config_key: str | None,
|
||||||
|
default_subdir: str,
|
||||||
|
*,
|
||||||
|
create: bool = True,
|
||||||
|
) -> str:
|
||||||
|
"""解析技能拥有路径:空配置 → 数据目录下 default_subdir;相对 → 相对数据目录;绝对 → 原样。"""
|
||||||
|
from jiangchang_skill_core import config
|
||||||
|
|
||||||
|
base = get_skill_data_dir()
|
||||||
|
raw = (config.get(config_key) or "").strip() if config_key else ""
|
||||||
|
if not raw:
|
||||||
|
path = os.path.join(base, _normalize_subdir(default_subdir))
|
||||||
|
elif os.path.isabs(raw):
|
||||||
|
path = os.path.abspath(os.path.expanduser(raw))
|
||||||
|
else:
|
||||||
|
path = os.path.join(base, _normalize_subdir(raw))
|
||||||
|
|
||||||
|
if create:
|
||||||
|
os.makedirs(path, exist_ok=True)
|
||||||
|
return path
|
||||||
|
|
||||||
|
|
||||||
|
def resolve_input_path(raw_path: str, *, create_parent: bool = False) -> str:
|
||||||
|
"""解析 CLI 输入路径:绝对路径原样;相对路径相对 {skill_data_dir}/imports/。"""
|
||||||
|
text = (raw_path or "").strip()
|
||||||
|
if not text:
|
||||||
|
return text
|
||||||
|
expanded = os.path.expanduser(text)
|
||||||
|
if os.path.isabs(expanded):
|
||||||
|
return os.path.abspath(expanded)
|
||||||
|
imports_dir = get_imports_dir(create=create_parent)
|
||||||
|
return os.path.abspath(os.path.join(imports_dir, os.path.normpath(expanded)))
|
||||||
|
|
||||||
|
|
||||||
|
def get_downloads_dir(*, create: bool = True) -> str:
|
||||||
|
return resolve_data_path(CONFIG_KEY_DOWNLOAD_DIR, STANDARD_SUBDIR_DOWNLOADS, create=create)
|
||||||
|
|
||||||
|
|
||||||
|
def get_imports_dir(*, create: bool = True) -> str:
|
||||||
|
return resolve_data_path(CONFIG_KEY_IMPORT_DIR, STANDARD_SUBDIR_IMPORTS, create=create)
|
||||||
|
|
||||||
|
|
||||||
|
def get_exports_dir(*, create: bool = True) -> str:
|
||||||
|
return resolve_data_path(CONFIG_KEY_EXPORT_DIR, STANDARD_SUBDIR_EXPORTS, create=create)
|
||||||
|
|
||||||
|
|
||||||
|
def get_uploads_dir(*, create: bool = True) -> str:
|
||||||
|
return resolve_data_path(CONFIG_KEY_UPLOAD_DIR, STANDARD_SUBDIR_UPLOADS, create=create)
|
||||||
|
|
||||||
|
|
||||||
|
def get_cache_dir(*, create: bool = True) -> str:
|
||||||
|
return resolve_data_path(CONFIG_KEY_CACHE_DIR, STANDARD_SUBDIR_CACHE, create=create)
|
||||||
|
|
||||||
|
|
||||||
|
def get_temp_dir(*, create: bool = True) -> str:
|
||||||
|
return resolve_data_path(CONFIG_KEY_TEMP_DIR, STANDARD_SUBDIR_TEMP, create=create)
|
||||||
|
|
||||||
|
|
||||||
|
def get_rpa_artifacts_dir(batch_id: str, *, create: bool = True) -> str:
|
||||||
|
batch = (batch_id or "").strip() or "default"
|
||||||
|
path = os.path.join(get_skill_data_dir(), STANDARD_SUBDIR_RPA_ARTIFACTS, batch)
|
||||||
|
if create:
|
||||||
|
os.makedirs(path, exist_ok=True)
|
||||||
|
return path
|
||||||
|
|
||||||
|
|
||||||
|
def get_videos_dir(*, create: bool = True) -> str:
|
||||||
|
path = os.path.join(get_skill_data_dir(), STANDARD_SUBDIR_VIDEOS)
|
||||||
|
if create:
|
||||||
|
os.makedirs(path, exist_ok=True)
|
||||||
|
return path
|
||||||
|
|
||||||
|
|
||||||
|
def list_resolved_data_paths() -> dict[str, str]:
|
||||||
|
"""供 health / config-path 输出已解析的数据子目录(不创建目录)。"""
|
||||||
|
return {
|
||||||
|
"skill_data_dir": get_skill_data_dir(),
|
||||||
|
"downloads_dir": get_downloads_dir(create=False),
|
||||||
|
"imports_dir": get_imports_dir(create=False),
|
||||||
|
"exports_dir": get_exports_dir(create=False),
|
||||||
|
"uploads_dir": get_uploads_dir(create=False),
|
||||||
|
"cache_dir": get_cache_dir(create=False),
|
||||||
|
"temp_dir": get_temp_dir(create=False),
|
||||||
|
"videos_dir": get_videos_dir(create=False),
|
||||||
|
}
|
||||||
|
|||||||
227
scripts/util/slug_naming.py
Normal file
227
scripts/util/slug_naming.py
Normal file
@@ -0,0 +1,227 @@
|
|||||||
|
"""技能 slug 语义校验(verb-noun-platform 规范,见 development/NAMING.md)。"""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import os
|
||||||
|
import re
|
||||||
|
|
||||||
|
_KEBAB_SLUG = re.compile(r"^[a-z0-9]+(?:-[a-z0-9]+)*$")
|
||||||
|
_MAX_SLUG_LENGTH = 48
|
||||||
|
_MIN_SEGMENTS = 3
|
||||||
|
_MAX_SEGMENTS = 5
|
||||||
|
_MAX_NOUN_SEGMENTS = 3
|
||||||
|
|
||||||
|
_SKILL_ROOT = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
||||||
|
_NAMING_DIR = os.path.join(_SKILL_ROOT, "assets", "naming")
|
||||||
|
|
||||||
|
_BUILTIN_VERBS = frozenset(
|
||||||
|
{
|
||||||
|
"scrape",
|
||||||
|
"receive",
|
||||||
|
"download",
|
||||||
|
"export",
|
||||||
|
"add",
|
||||||
|
"fill",
|
||||||
|
"enter",
|
||||||
|
"bind",
|
||||||
|
"withdraw",
|
||||||
|
"reconcile",
|
||||||
|
"verify",
|
||||||
|
"submit",
|
||||||
|
"query",
|
||||||
|
"process",
|
||||||
|
"reply",
|
||||||
|
"generate",
|
||||||
|
"draft",
|
||||||
|
"review",
|
||||||
|
"confirm",
|
||||||
|
"track",
|
||||||
|
"upload",
|
||||||
|
"match",
|
||||||
|
"triage",
|
||||||
|
"ship",
|
||||||
|
"reprice",
|
||||||
|
}
|
||||||
|
)
|
||||||
|
_BUILTIN_PLATFORMS = frozenset(
|
||||||
|
{
|
||||||
|
"amazon",
|
||||||
|
"shopee",
|
||||||
|
"xinghang",
|
||||||
|
"alibaba",
|
||||||
|
"kingdee",
|
||||||
|
"icbc",
|
||||||
|
"pingpong",
|
||||||
|
"worldfirst",
|
||||||
|
"paypal",
|
||||||
|
"lianlian",
|
||||||
|
"single-window",
|
||||||
|
"export-rebate",
|
||||||
|
"etax",
|
||||||
|
}
|
||||||
|
)
|
||||||
|
_BUILTIN_SCOPES = frozenset({"all", "batch", "multi", "default"})
|
||||||
|
|
||||||
|
LEGACY_EXEMPT_SLUGS: frozenset[str] = frozenset(
|
||||||
|
{
|
||||||
|
"account-manager",
|
||||||
|
"skill-template",
|
||||||
|
"your-skill-slug",
|
||||||
|
"your_skill_slug",
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def load_wordlist(filename: str) -> frozenset[str]:
|
||||||
|
"""从 assets/naming/{filename} 加载词表;文件不存在时 fallback 内置集合。"""
|
||||||
|
fallbacks = {
|
||||||
|
"verbs.txt": _BUILTIN_VERBS,
|
||||||
|
"platforms.txt": _BUILTIN_PLATFORMS,
|
||||||
|
"scopes.txt": _BUILTIN_SCOPES,
|
||||||
|
}
|
||||||
|
fallback = fallbacks.get(filename, frozenset())
|
||||||
|
path = os.path.join(_NAMING_DIR, filename)
|
||||||
|
if not os.path.isfile(path):
|
||||||
|
return fallback
|
||||||
|
words: set[str] = set()
|
||||||
|
with open(path, encoding="utf-8") as f:
|
||||||
|
for line in f:
|
||||||
|
word = line.strip()
|
||||||
|
if word and not word.startswith("#"):
|
||||||
|
words.add(word)
|
||||||
|
return frozenset(words) if words else fallback
|
||||||
|
|
||||||
|
|
||||||
|
def parse_slug_segments(slug: str) -> list[str]:
|
||||||
|
"""按 '-' 分割;非法字符由上层 kebab 校验处理。"""
|
||||||
|
slug = slug.strip()
|
||||||
|
if not slug:
|
||||||
|
return []
|
||||||
|
return slug.split("-")
|
||||||
|
|
||||||
|
|
||||||
|
def _match_platform_suffix(
|
||||||
|
segments: list[str],
|
||||||
|
platforms: frozenset[str],
|
||||||
|
) -> tuple[str, int] | None:
|
||||||
|
"""返回 (platform_name, platform_part_count) 或 None。"""
|
||||||
|
for platform in sorted(platforms, key=lambda item: (-item.count("-"), item)):
|
||||||
|
parts = platform.split("-")
|
||||||
|
part_count = len(parts)
|
||||||
|
if len(segments) < part_count + 2:
|
||||||
|
continue
|
||||||
|
if segments[-part_count:] == parts:
|
||||||
|
return platform, part_count
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
def classify_slug_form(segments: list[str], *, slug: str = "") -> str:
|
||||||
|
"""
|
||||||
|
返回: 'legacy_exempt' | 'standard' | 'scope' | 'invalid'
|
||||||
|
|
||||||
|
- legacy_exempt: slug in LEGACY_EXEMPT_SLUGS
|
||||||
|
- standard: 末段匹配 platforms, 首段 in verbs, 3<=len<=5
|
||||||
|
- scope: 末段 in scopes, 首段 in verbs, 3<=len<=5
|
||||||
|
- invalid: 其他
|
||||||
|
"""
|
||||||
|
if slug in LEGACY_EXEMPT_SLUGS:
|
||||||
|
return "legacy_exempt"
|
||||||
|
|
||||||
|
if not segments or not (_MIN_SEGMENTS <= len(segments) <= _MAX_SEGMENTS):
|
||||||
|
return "invalid"
|
||||||
|
|
||||||
|
verbs = load_wordlist("verbs.txt")
|
||||||
|
platforms = load_wordlist("platforms.txt")
|
||||||
|
scopes = load_wordlist("scopes.txt")
|
||||||
|
|
||||||
|
if segments[0] not in verbs:
|
||||||
|
return "invalid"
|
||||||
|
|
||||||
|
if segments[-1] in scopes:
|
||||||
|
return "scope"
|
||||||
|
|
||||||
|
if _match_platform_suffix(segments, platforms) is not None:
|
||||||
|
return "standard"
|
||||||
|
|
||||||
|
return "invalid"
|
||||||
|
|
||||||
|
|
||||||
|
def validate_slug_semantics(
|
||||||
|
slug: str,
|
||||||
|
*,
|
||||||
|
strict: bool = True,
|
||||||
|
) -> tuple[list[str], list[str]]:
|
||||||
|
"""返回 (errors, warnings)。"""
|
||||||
|
slug = slug.strip()
|
||||||
|
if slug in LEGACY_EXEMPT_SLUGS:
|
||||||
|
return [], []
|
||||||
|
|
||||||
|
errors: list[str] = []
|
||||||
|
warnings: list[str] = []
|
||||||
|
|
||||||
|
if not _KEBAB_SLUG.fullmatch(slug):
|
||||||
|
msg = f"slug 必须为 kebab-case(小写字母、数字、连字符):{slug!r}"
|
||||||
|
(errors if strict else warnings).append(msg)
|
||||||
|
return errors, warnings
|
||||||
|
|
||||||
|
if len(slug) > _MAX_SLUG_LENGTH:
|
||||||
|
msg = f"slug 长度不得超过 {_MAX_SLUG_LENGTH} 字符(当前 {len(slug)}):{slug!r}"
|
||||||
|
(errors if strict else warnings).append(msg)
|
||||||
|
|
||||||
|
segments = parse_slug_segments(slug)
|
||||||
|
segment_count = len(segments)
|
||||||
|
|
||||||
|
if segment_count < _MIN_SEGMENTS:
|
||||||
|
msg = (
|
||||||
|
f"slug 段数应为 {_MIN_SEGMENTS}~{_MAX_SEGMENTS}(verb-noun-platform),"
|
||||||
|
f"当前为 {segment_count} 段:{slug!r};新技能禁止 2 段无平台形态(如 reconcile-finance)"
|
||||||
|
)
|
||||||
|
(errors if strict else warnings).append(msg)
|
||||||
|
return errors, warnings
|
||||||
|
|
||||||
|
if segment_count > _MAX_SEGMENTS:
|
||||||
|
msg = f"slug 段数不得超过 {_MAX_SEGMENTS}(当前 {segment_count}):{slug!r}"
|
||||||
|
(errors if strict else warnings).append(msg)
|
||||||
|
|
||||||
|
verbs = load_wordlist("verbs.txt")
|
||||||
|
platforms = load_wordlist("platforms.txt")
|
||||||
|
scopes = load_wordlist("scopes.txt")
|
||||||
|
|
||||||
|
if segments[0] in platforms and segments[0] not in verbs:
|
||||||
|
msg = f"平台名不应放在 slug 最前:{slug!r}(应为 verb-noun-platform)"
|
||||||
|
(errors if strict else warnings).append(msg)
|
||||||
|
|
||||||
|
if segments[0] not in verbs:
|
||||||
|
msg = f"slug 首段应为动词白名单中的词:{segments[0]!r}(slug={slug!r})"
|
||||||
|
(errors if strict else warnings).append(msg)
|
||||||
|
|
||||||
|
form = classify_slug_form(segments, slug=slug)
|
||||||
|
if form == "scope":
|
||||||
|
if segments[-1] not in scopes:
|
||||||
|
msg = f"slug 末段应为 scope 白名单中的词:{segments[-1]!r}(slug={slug!r})"
|
||||||
|
(errors if strict else warnings).append(msg)
|
||||||
|
noun_count = segment_count - 2
|
||||||
|
elif form == "standard":
|
||||||
|
platform_match = _match_platform_suffix(segments, platforms)
|
||||||
|
if platform_match is None:
|
||||||
|
msg = f"slug 末段应匹配平台白名单:{slug!r}"
|
||||||
|
(errors if strict else warnings).append(msg)
|
||||||
|
noun_count = 0
|
||||||
|
else:
|
||||||
|
_platform_name, platform_part_count = platform_match
|
||||||
|
noun_count = segment_count - 1 - platform_part_count
|
||||||
|
else:
|
||||||
|
if segment_count >= _MIN_SEGMENTS:
|
||||||
|
if segments[-1] not in scopes:
|
||||||
|
platform_match = _match_platform_suffix(segments, platforms)
|
||||||
|
if platform_match is None:
|
||||||
|
msg = f"slug 末段应匹配平台或 scope 白名单:{slug!r}"
|
||||||
|
(errors if strict else warnings).append(msg)
|
||||||
|
noun_count = 0
|
||||||
|
|
||||||
|
if noun_count > _MAX_NOUN_SEGMENTS:
|
||||||
|
warnings.append(
|
||||||
|
f"slug 中间名词段建议不超过 {_MAX_NOUN_SEGMENTS} 个词(当前 {noun_count}):{slug!r}"
|
||||||
|
)
|
||||||
|
|
||||||
|
return errors, warnings
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
# skill-template 测试说明(企业数字员工技能)
|
# skill-template 测试说明(企业数字员工技能)
|
||||||
|
|
||||||
本目录提供面向 **ERP / TMS / WMS / 船司 / 报关 / 邮件 / 浏览器 / LLM / RPA** 等外联场景的**分层测试骨架**:默认仅内存与本地 SQLite;**真实 API / 真实 RPA** 必须通过环境变量显式授权,且仅以 ``*.sample`` 或复制后的文件提供范式。
|
本目录提供面向 **ERP / TMS / WMS / 船司 / 报关 / 邮件 / 浏览器 / LLM / RPA** 等外联场景的**分层测试骨架**:默认仅内存与本地 SQLite;**真实 API / 真实 RPA** 必须显式设置 ``OPENCLAW_TEST_TARGET``,且仅以 ``*.sample`` 或复制后的文件提供范式。
|
||||||
|
|
||||||
复制为新技能后,请保留隔离数据根与 profile 策略,再按域扩展用例。
|
复制为新技能后,请保留隔离数据根与 profile 策略,再按域扩展用例。
|
||||||
|
|
||||||
@@ -16,9 +16,9 @@
|
|||||||
| 纯函数 / 业务规则 | `tests/test_*.py` | 是 | 不依赖真实外部系统 |
|
| 纯函数 / 业务规则 | `tests/test_*.py` | 是 | 不依赖真实外部系统 |
|
||||||
| service 层契约 | 从 `tests/samples/test_service_contract.py.sample` 复制到 `tests/test_service_contract.py` | 是,复制后 | 用 `FakeAdapter` / stub 注入外部依赖 |
|
| service 层契约 | 从 `tests/samples/test_service_contract.py.sample` 复制到 `tests/test_service_contract.py` | 是,复制后 | 用 `FakeAdapter` / stub 注入外部依赖 |
|
||||||
| golden fixture 回归 | 从 `tests/samples/test_golden_cases.py.sample` 复制到 `tests/test_golden_cases.py` | 是,复制后 | 输入样例 + 期望输出,适合解析、计算、校验类技能 |
|
| golden fixture 回归 | 从 `tests/samples/test_golden_cases.py.sample` 复制到 `tests/test_golden_cases.py` | 是,复制后 | 输入样例 + 期望输出,适合解析、计算、校验类技能 |
|
||||||
| 仿真 API | `tests/integration/test_simulator_api_contract.py.sample` | 否 | 复制并设置 `OPENCLAW_TEST_TARGET=simulator_api`(或兼容键名 `OPENCLOW_TEST_TARGET`)后按需运行 |
|
| 仿真 API | `tests/integration/test_simulator_api_contract.py.sample` | 否 | 复制并设置 `OPENCLAW_TEST_TARGET=simulator_api` 后按需运行 |
|
||||||
| 仿真 RPA / 页面操作 | `tests/integration/test_simulator_rpa_contract.py.sample` | 否 | 用 localhost / 仿真页面,不碰真实系统 |
|
| 仿真 RPA / 页面操作 | `tests/integration/test_simulator_rpa_contract.py.sample` | 否 | 用 localhost / 仿真页面,不碰真实系统 |
|
||||||
| 真实 API | `tests/integration/test_real_api_contract.py.sample` | 否 | 必须 `OPENCLAW_TEST_TARGET=real_api` 且 `ALLOW_REAL_API=1` |
|
| 真实 API | `tests/integration/test_real_api_contract.py.sample` | 否 | 必须 `OPENCLAW_TEST_TARGET=real_api` |
|
||||||
| 真实 RPA | `tests/integration/test_real_rpa_contract.py.sample` | 否 | 最高风险,只能手动触发 |
|
| 真实 RPA | `tests/integration/test_real_rpa_contract.py.sample` | 否 | 最高风险,只能手动触发 |
|
||||||
| 桌面宿主 E2E | `tests/desktop/test_desktop_smoke.py.sample` | 否 | 需要 pytest + `jiangchang_desktop_sdk` |
|
| 桌面宿主 E2E | `tests/desktop/test_desktop_smoke.py.sample` | 否 | 需要 pytest + `jiangchang_desktop_sdk` |
|
||||||
|
|
||||||
@@ -70,9 +70,11 @@ python tests/run_tests.py test_cli_smoke
|
|||||||
|------|------|
|
|------|------|
|
||||||
| CLI 冒烟(import ``cli.app.main``) | ``test_cli_smoke.py`` |
|
| CLI 冒烟(import ``cli.app.main``) | ``test_cli_smoke.py`` |
|
||||||
| **subprocess 真实入口** ``python scripts/main.py`` | ``test_entrypoint_subprocess.py`` |
|
| **subprocess 真实入口** ``python scripts/main.py`` | ``test_entrypoint_subprocess.py`` |
|
||||||
| 运行路径与 **CLAW_*/JIANGCHANG_*** 隔离 | ``test_runtime_paths.py`` |
|
| 运行路径与 **JIANGCHANG_*** 隔离 | ``test_runtime_paths.py`` |
|
||||||
| ``SKILL.md`` slug 与 ``constants.SKILL_SLUG`` | ``test_skill_metadata.py`` |
|
| ``SKILL.md`` slug 与 ``constants.SKILL_SLUG`` | ``test_skill_metadata.py`` |
|
||||||
| DB / ``task_logs`` 骨架冒烟 | ``test_db_smoke.py`` |
|
| DB / ``task_logs`` 骨架冒烟 | ``test_db_smoke.py`` |
|
||||||
|
| 数据管理元数据 / 字段顺序 | ``test_display_metadata.py`` |
|
||||||
|
| 标准时间字段(Unix 秒 / trigger / 元数据) | ``test_timestamp_columns.py`` |
|
||||||
| **adapter profile / 外呼授权策略** | ``test_adapter_profile_policy.py`` + ``adapter_test_utils.py``(非 ``test_`` 前缀,不自动当用例收集) |
|
| **adapter profile / 外呼授权策略** | ``test_adapter_profile_policy.py`` + ``adapter_test_utils.py``(非 ``test_`` 前缀,不自动当用例收集) |
|
||||||
| **无 vendored ``jiangchang_skill_core``** | ``test_platform_import.py`` |
|
| **无 vendored ``jiangchang_skill_core``** | ``test_platform_import.py`` |
|
||||||
| **runtime diagnostics 架构守护** | ``test_runtime_diagnostics_integration.py`` |
|
| **runtime diagnostics 架构守护** | ``test_runtime_diagnostics_integration.py`` |
|
||||||
@@ -84,17 +86,14 @@ python tests/run_tests.py test_cli_smoke
|
|||||||
|
|
||||||
### 1.3 数据隔离(``_support.IsolatedDataRoot``)
|
### 1.3 数据隔离(``_support.IsolatedDataRoot``)
|
||||||
|
|
||||||
同时设置(进入同一临时目录 / 用户):
|
进入同一临时目录 / 用户:
|
||||||
|
|
||||||
| ``CLAW_DATA_ROOT`` | ``JIANGCHANG_DATA_ROOT`` |
|
| 变量 | 值 |
|
||||||
|--------------------|---------------------------|
|
|------|-----|
|
||||||
| 同一 ``tempfile.mkdtemp(prefix="skill-test-")`` | 同上 |
|
| ``JIANGCHANG_DATA_ROOT`` | ``tempfile.mkdtemp(prefix="skill-test-")`` |
|
||||||
|
| ``JIANGCHANG_USER_ID`` | ``_test`` |
|
||||||
|
|
||||||
| ``CLAW_USER_ID`` | ``JIANGCHANG_USER_ID`` |
|
退出时两个键均恢复(原不存在则 ``pop``),并删除临时目录。
|
||||||
|------------------|-------------------------|
|
|
||||||
| ``_test`` | ``_test`` |
|
|
||||||
|
|
||||||
退出时四个键均恢复(原不存在则 ``pop``),并删除临时目录。
|
|
||||||
|
|
||||||
### 1.4 产物与密钥
|
### 1.4 产物与密钥
|
||||||
|
|
||||||
@@ -125,11 +124,11 @@ python tests/run_tests.py test_cli_smoke
|
|||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## 5. 环境变量:测试档位与授权
|
## 5. 环境变量:测试档位
|
||||||
|
|
||||||
### 5.1 测试档位(二选一变量名,后者为历史拼写兼容)
|
### 5.1 测试档位
|
||||||
|
|
||||||
- ``OPENCLAW_TEST_TARGET`` 或 ``OPENCLOW_TEST_TARGET``
|
- ``OPENCLAW_TEST_TARGET``
|
||||||
|
|
||||||
**合法取值**(非法值将使 ``get_test_target()`` 抛 ``ValueError``):
|
**合法取值**(非法值将使 ``get_test_target()`` 抛 ``ValueError``):
|
||||||
|
|
||||||
@@ -139,23 +138,15 @@ python tests/run_tests.py test_cli_smoke
|
|||||||
| ``mock`` | 与 ``unit`` 类似的安全档位(显式语义) |
|
| ``mock`` | 与 ``unit`` 类似的安全档位(显式语义) |
|
||||||
| ``simulator_api`` | 仅允许 **仿真 HTTP** 类集成(如 localhost) |
|
| ``simulator_api`` | 仅允许 **仿真 HTTP** 类集成(如 localhost) |
|
||||||
| ``simulator_rpa`` | 仅允许 **仿真页面 / 录播 RPA** |
|
| ``simulator_rpa`` | 仅允许 **仿真页面 / 录播 RPA** |
|
||||||
| ``real_api`` | 真实 API(另需 ``ALLOW_REAL_API=1``) |
|
| ``real_api`` | 真实 API(显式设 ``OPENCLAW_TEST_TARGET=real_api``) |
|
||||||
| ``real_rpa`` | 真实 RPA(另需 ``ALLOW_REAL_RPA=1``) |
|
| ``real_rpa`` | 真实 RPA(显式设 ``OPENCLAW_TEST_TARGET=real_rpa``) |
|
||||||
|
|
||||||
未设置时等价 ``unit``。
|
未设置时等价 ``unit``。
|
||||||
|
|
||||||
### 5.2 授权开关(显式 ``1``)
|
### 5.2 默认安全策略(``adapter_test_utils``)
|
||||||
|
|
||||||
| 变量 | 作用 |
|
|
||||||
|------|------|
|
|
||||||
| ``ALLOW_REAL_API=1`` | 允许 ``real_api`` profile 访问真实 HTTP 通道 |
|
|
||||||
| ``ALLOW_REAL_RPA=1`` | 允许 ``real_rpa`` profile 驱动真实浏览器/RPA |
|
|
||||||
| ``ALLOW_WRITE_ACTIONS=1`` | 在 ``real_*`` 下允许**写**操作(提交表单、下单等) |
|
|
||||||
|
|
||||||
### 5.3 默认安全策略(``adapter_test_utils``)
|
|
||||||
|
|
||||||
- 默认 ``unit``:不访问真实 API、不跑真实 RPA、不做写操作、不读取真实密钥、不写真实数据根。
|
- 默认 ``unit``:不访问真实 API、不跑真实 RPA、不做写操作、不读取真实密钥、不写真实数据根。
|
||||||
- ``assert_no_real_network_env()`` 断言默认未误开 ``ALLOW_REAL_*``(用于必跑用例自检)。
|
- ``assert_no_real_network_env()`` 断言默认未误设 ``OPENCLAW_TEST_TARGET`` 为 ``real_api`` / ``real_rpa``(用于必跑用例自检)。
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
@@ -170,7 +161,7 @@ python tests/run_tests.py test_cli_smoke
|
|||||||
- 默认测试只能使用 **mock、stub、`FakeAdapter`、fixtures、隔离 SQLite** 等安全手段。
|
- 默认测试只能使用 **mock、stub、`FakeAdapter`、fixtures、隔离 SQLite** 等安全手段。
|
||||||
- 业务逻辑应优先在 **service 层** 编写与测试,不要只断言 CLI 文案。
|
- 业务逻辑应优先在 **service 层** 编写与测试,不要只断言 CLI 文案。
|
||||||
- 异常路径应返回**结构化错误**(如统一错误码/字段),避免裸异常直接穿透到 CLI。
|
- 异常路径应返回**结构化错误**(如统一错误码/字段),避免裸异常直接穿透到 CLI。
|
||||||
- 有真实系统联调需求时,测试应写在 `tests/integration/`,并配合 `OPENCLAW_TEST_TARGET` / `ALLOW_REAL_*` / `ALLOW_WRITE_ACTIONS` 等**显式开关**;不得默认开启。
|
- 有真实系统联调需求时,测试应写在 `tests/integration/`,并显式设置 ``OPENCLAW_TEST_TARGET`` 为 ``real_api`` / ``real_rpa``;不得默认开启。
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
|
|||||||
@@ -3,10 +3,8 @@
|
|||||||
测试公共支撑。
|
测试公共支撑。
|
||||||
|
|
||||||
1. 将 scripts/ 加入 sys.path,便于 `from cli…` / `from db…` / `from util…` 等导入。
|
1. 将 scripts/ 加入 sys.path,便于 `from cli…` / `from db…` / `from util…` 等导入。
|
||||||
2. `IsolatedDataRoot`:同时设置 CLAW_* 与 JIANGCHANG_* 数据根与用户 ID,
|
2. `IsolatedDataRoot`:设置 `JIANGCHANG_DATA_ROOT` 与 `JIANGCHANG_USER_ID`,
|
||||||
与 `jiangchang_skill_core.runtime_env` 的优先级一致(CLAW_* 优先),
|
退出时恢复原值并删除临时目录,避免污染真实用户数据目录。
|
||||||
避免只设 JIANGCHANG_* 时仍读到真实 CLAW_DATA_ROOT。
|
|
||||||
退出时四个变量均恢复;原不存在的键会删除;并 rmtree 临时目录。
|
|
||||||
"""
|
"""
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
@@ -24,9 +22,7 @@ if _SCRIPTS_DIR not in sys.path:
|
|||||||
sys.path.insert(0, _SCRIPTS_DIR)
|
sys.path.insert(0, _SCRIPTS_DIR)
|
||||||
|
|
||||||
_ISOLATION_KEYS = (
|
_ISOLATION_KEYS = (
|
||||||
"CLAW_DATA_ROOT",
|
|
||||||
"JIANGCHANG_DATA_ROOT",
|
"JIANGCHANG_DATA_ROOT",
|
||||||
"CLAW_USER_ID",
|
|
||||||
"JIANGCHANG_USER_ID",
|
"JIANGCHANG_USER_ID",
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -39,7 +35,7 @@ def get_skill_root() -> str:
|
|||||||
return _SKILL_ROOT
|
return _SKILL_ROOT
|
||||||
|
|
||||||
|
|
||||||
def platform_kit_version_patch(version: str = "1.0.13"):
|
def platform_kit_version_patch(version: str = "1.2.0"):
|
||||||
"""Mock installed jiangchang-platform-kit version for health/diagnostics tests."""
|
"""Mock installed jiangchang-platform-kit version for health/diagnostics tests."""
|
||||||
from unittest.mock import patch
|
from unittest.mock import patch
|
||||||
|
|
||||||
@@ -53,11 +49,11 @@ class IsolatedDataRoot:
|
|||||||
"""
|
"""
|
||||||
在独立临时目录下模拟数据根,避免污染本机默认数据目录。
|
在独立临时目录下模拟数据根,避免污染本机默认数据目录。
|
||||||
|
|
||||||
进入上下文时(同一套隔离值写入 CLAW_* 与 JIANGCHANG_*):
|
进入上下文时:
|
||||||
- CLAW_DATA_ROOT / JIANGCHANG_DATA_ROOT -> 同一 tempfile.mkdtemp(prefix='skill-test-')
|
- JIANGCHANG_DATA_ROOT -> tempfile.mkdtemp(prefix='skill-test-')
|
||||||
- CLAW_USER_ID / JIANGCHANG_USER_ID -> _test(与业务默认用户区分)
|
- JIANGCHANG_USER_ID -> _test(与业务默认用户区分)
|
||||||
|
|
||||||
退出时:四个变量均恢复为进入前状态(若原先未设置则 pop),并删除临时目录。
|
退出时:两个变量均恢复为进入前状态(若原先未设置则 pop),并删除临时目录。
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, user_id: str = "_test") -> None:
|
def __init__(self, user_id: str = "_test") -> None:
|
||||||
@@ -69,9 +65,7 @@ class IsolatedDataRoot:
|
|||||||
self._tmp = tempfile.mkdtemp(prefix="skill-test-")
|
self._tmp = tempfile.mkdtemp(prefix="skill-test-")
|
||||||
for k in _ISOLATION_KEYS:
|
for k in _ISOLATION_KEYS:
|
||||||
self._old[k] = os.environ.get(k)
|
self._old[k] = os.environ.get(k)
|
||||||
os.environ["CLAW_DATA_ROOT"] = self._tmp
|
|
||||||
os.environ["JIANGCHANG_DATA_ROOT"] = self._tmp
|
os.environ["JIANGCHANG_DATA_ROOT"] = self._tmp
|
||||||
os.environ["CLAW_USER_ID"] = self._user_id
|
|
||||||
os.environ["JIANGCHANG_USER_ID"] = self._user_id
|
os.environ["JIANGCHANG_USER_ID"] = self._user_id
|
||||||
return self._tmp
|
return self._tmp
|
||||||
|
|
||||||
|
|||||||
@@ -1,17 +1,17 @@
|
|||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
"""
|
"""
|
||||||
企业技能对接外部系统(ERP / TMS / WMS / 船司 / 报关 / 邮件 / 浏览器 / LLM / RPA)时,
|
企业技能对接外部系统(ERP / TMS / WMS / 船司 / 报关 / 邮件 / 浏览器 / LLM / RPA)时,
|
||||||
测试侧共用的 **profile / 授权 / 禁止真实外呼** 工具。
|
测试侧共用的 **profile / 禁止真实外呼** 工具。
|
||||||
|
|
||||||
- 仅标准库;不发起 HTTP;不启动浏览器。
|
- 仅标准库 + jiangchang_skill_core.config;不发起 HTTP;不启动浏览器。
|
||||||
- 真实 API / RPA 契约测试请放在 ``tests/integration/*.sample``,并配合环境变量显式授权。
|
- 真实 API / RPA 契约测试请放在 ``tests/integration/*.sample``,并显式设置 ``OPENCLAW_TEST_TARGET``。
|
||||||
"""
|
"""
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
import os
|
|
||||||
from typing import Any, Final
|
from typing import Any, Final
|
||||||
|
|
||||||
# 允许的 OPENCLAW_TEST_TARGET / OPENCLOW_TEST_TARGET 取值(后者为历史拼写兼容)
|
from jiangchang_skill_core import config
|
||||||
|
|
||||||
ALLOWED_TEST_TARGETS: Final[frozenset[str]] = frozenset(
|
ALLOWED_TEST_TARGETS: Final[frozenset[str]] = frozenset(
|
||||||
{
|
{
|
||||||
"unit",
|
"unit",
|
||||||
@@ -25,12 +25,12 @@ ALLOWED_TEST_TARGETS: Final[frozenset[str]] = frozenset(
|
|||||||
|
|
||||||
|
|
||||||
def _raw_test_target_env() -> str:
|
def _raw_test_target_env() -> str:
|
||||||
return (os.environ.get("OPENCLAW_TEST_TARGET") or os.environ.get("OPENCLOW_TEST_TARGET") or "").strip()
|
return (config.get("OPENCLAW_TEST_TARGET") or "").strip()
|
||||||
|
|
||||||
|
|
||||||
def get_test_target() -> str:
|
def get_test_target() -> str:
|
||||||
"""
|
"""
|
||||||
读取 ``OPENCLAW_TEST_TARGET`` 或 ``OPENCLOW_TEST_TARGET``(兼容旧拼写)。
|
读取 ``OPENCLAW_TEST_TARGET``(经 ``config.get`` 三级合并)。
|
||||||
|
|
||||||
未设置时返回 ``unit``。非法取值抛出 ``ValueError``,便于尽早发现拼写错误。
|
未设置时返回 ``unit``。非法取值抛出 ``ValueError``,便于尽早发现拼写错误。
|
||||||
"""
|
"""
|
||||||
@@ -46,18 +46,15 @@ def get_test_target() -> str:
|
|||||||
|
|
||||||
def real_access_allowed(profile: str) -> bool:
|
def real_access_allowed(profile: str) -> bool:
|
||||||
"""
|
"""
|
||||||
判断当前环境是否允许对 ``profile`` 做「真实通道」访问(仍不代替业务鉴权)。
|
判断当前 ``OPENCLAW_TEST_TARGET`` 是否允许运行 ``profile`` 档位测试。
|
||||||
|
|
||||||
- ``real_api``:需要 ``ALLOW_REAL_API == "1"``。
|
|
||||||
- ``real_rpa``:需要 ``ALLOW_REAL_RPA == "1"``。
|
|
||||||
- 写操作另需 ``ALLOW_WRITE_ACTIONS == "1"``(由 ``should_skip_profile(..., write=True)`` 组合检查)。
|
|
||||||
"""
|
"""
|
||||||
if profile == "real_api":
|
target = get_test_target()
|
||||||
return os.environ.get("ALLOW_REAL_API", "") == "1"
|
|
||||||
if profile == "real_rpa":
|
|
||||||
return os.environ.get("ALLOW_REAL_RPA", "") == "1"
|
|
||||||
if profile in ("mock", "simulator_api", "simulator_rpa"):
|
if profile in ("mock", "simulator_api", "simulator_rpa"):
|
||||||
return True
|
return True
|
||||||
|
if profile == "real_api":
|
||||||
|
return target == "real_api"
|
||||||
|
if profile == "real_rpa":
|
||||||
|
return target == "real_rpa"
|
||||||
raise ValueError(f"unknown profile for real_access_allowed: {profile!r}")
|
raise ValueError(f"unknown profile for real_access_allowed: {profile!r}")
|
||||||
|
|
||||||
|
|
||||||
@@ -67,42 +64,19 @@ def should_skip_profile(profile: str, write: bool = False) -> tuple[bool, str]:
|
|||||||
|
|
||||||
策略摘要:
|
策略摘要:
|
||||||
- ``mock``:永不跳过(内存 / FakeAdapter,无外呼)。
|
- ``mock``:永不跳过(内存 / FakeAdapter,无外呼)。
|
||||||
- ``simulator_api``:仅当 ``get_test_target() == "simulator_api"`` 时不跳过。
|
- ``simulator_api`` / ``simulator_rpa``:仅当 ``get_test_target()`` 与 profile 一致时不跳过。
|
||||||
- ``simulator_rpa``:仅当 target 为 ``simulator_rpa`` 时不跳过。
|
- ``real_api`` / ``real_rpa``:仅当 ``get_test_target()`` 与 profile 一致时不跳过。
|
||||||
- ``real_api``:target 须为 ``real_api`` 且 ``ALLOW_REAL_API=1``;``write=True`` 还须 ``ALLOW_WRITE_ACTIONS=1``。
|
- ``write`` 参数保留以兼容 integration 样例签名;档位一致即允许写操作测试。
|
||||||
- ``real_rpa``:target 须为 ``real_rpa`` 且 ``ALLOW_REAL_RPA=1``;``write=True`` 还须 ``ALLOW_WRITE_ACTIONS=1``。
|
|
||||||
"""
|
"""
|
||||||
|
_ = write
|
||||||
target = get_test_target()
|
target = get_test_target()
|
||||||
|
|
||||||
if profile == "mock":
|
if profile == "mock":
|
||||||
return (False, "")
|
return (False, "")
|
||||||
|
|
||||||
if profile == "simulator_api":
|
if profile in ("simulator_api", "simulator_rpa", "real_api", "real_rpa"):
|
||||||
if target != "simulator_api":
|
if target != profile:
|
||||||
return (True, f"OPENCLAW_TEST_TARGET must be simulator_api (got {target!r})")
|
return (True, f"OPENCLAW_TEST_TARGET must be {profile} (got {target!r})")
|
||||||
return (False, "")
|
|
||||||
|
|
||||||
if profile == "simulator_rpa":
|
|
||||||
if target != "simulator_rpa":
|
|
||||||
return (True, f"OPENCLAW_TEST_TARGET must be simulator_rpa (got {target!r})")
|
|
||||||
return (False, "")
|
|
||||||
|
|
||||||
if profile == "real_api":
|
|
||||||
if target != "real_api":
|
|
||||||
return (True, f"OPENCLAW_TEST_TARGET must be real_api (got {target!r})")
|
|
||||||
if os.environ.get("ALLOW_REAL_API", "") != "1":
|
|
||||||
return (True, "ALLOW_REAL_API is not 1")
|
|
||||||
if write and os.environ.get("ALLOW_WRITE_ACTIONS", "") != "1":
|
|
||||||
return (True, "ALLOW_WRITE_ACTIONS is not 1 (write requested)")
|
|
||||||
return (False, "")
|
|
||||||
|
|
||||||
if profile == "real_rpa":
|
|
||||||
if target != "real_rpa":
|
|
||||||
return (True, f"OPENCLAW_TEST_TARGET must be real_rpa (got {target!r})")
|
|
||||||
if os.environ.get("ALLOW_REAL_RPA", "") != "1":
|
|
||||||
return (True, "ALLOW_REAL_RPA is not 1")
|
|
||||||
if write and os.environ.get("ALLOW_WRITE_ACTIONS", "") != "1":
|
|
||||||
return (True, "ALLOW_WRITE_ACTIONS is not 1 (write requested)")
|
|
||||||
return (False, "")
|
return (False, "")
|
||||||
|
|
||||||
raise ValueError(f"unknown profile: {profile!r}")
|
raise ValueError(f"unknown profile: {profile!r}")
|
||||||
@@ -134,9 +108,11 @@ class FakeAdapter:
|
|||||||
|
|
||||||
def assert_no_real_network_env() -> None:
|
def assert_no_real_network_env() -> None:
|
||||||
"""
|
"""
|
||||||
断言当前进程未显式打开「真实外呼」授权开关(默认安全 CI / 本地开发)。
|
断言默认必跑套件未误设真实外联档位(``real_api`` / ``real_rpa``)。
|
||||||
|
|
||||||
若需在受控环境跑真实集成,请显式设置 ``ALLOW_REAL_*`` 并使用 ``tests/integration/*.sample``。
|
若需在受控环境跑真实集成,请显式设置 ``OPENCLAW_TEST_TARGET`` 并使用 ``tests/integration/*.sample``。
|
||||||
"""
|
"""
|
||||||
assert os.environ.get("ALLOW_REAL_API", "") != "1", "ALLOW_REAL_API must not be 1 in default unit tests"
|
target = get_test_target()
|
||||||
assert os.environ.get("ALLOW_REAL_RPA", "") != "1", "ALLOW_REAL_RPA must not be 1 in default unit tests"
|
assert target not in ("real_api", "real_rpa"), (
|
||||||
|
f"OPENCLAW_TEST_TARGET must not be {target!r} in default unit tests"
|
||||||
|
)
|
||||||
|
|||||||
@@ -11,7 +11,7 @@
|
|||||||
|------|------------------|------------------------|
|
|------|------------------|------------------------|
|
||||||
| 目的 | 默认可**升级**为根目录 `test_*.py` 的 **业务单元 / service 契约 / golden** 范式 | **外联联调**范式:网络、浏览器、仿真或真实系统 |
|
| 目的 | 默认可**升级**为根目录 `test_*.py` 的 **业务单元 / service 契约 / golden** 范式 | **外联联调**范式:网络、浏览器、仿真或真实系统 |
|
||||||
| 默认执行 | `.sample` 不执行;复制到根目录并改名后才进入默认套件 | **永远不**作为默认 unittest 的一部分;复制后仍需显式环境变量与手动/CI 任务触发 |
|
| 默认执行 | `.sample` 不执行;复制到根目录并改名后才进入默认套件 | **永远不**作为默认 unittest 的一部分;复制后仍需显式环境变量与手动/CI 任务触发 |
|
||||||
| 典型内容 | `FakeAdapter`、fixture、隔离 DB | `localhost` 仿真、真实 API(授权)、真实 RPA(最高风险) |
|
| 典型内容 | `FakeAdapter`、fixture、隔离 DB | `localhost` 仿真、真实 API、真实 RPA(最高风险) |
|
||||||
|
|
||||||
**原则**:只要会访问**网络**、**浏览器**、**真实或仿真外部系统**,应优先放在 `integration`(并保持 `.sample` 直至团队同意启用),而不是写进默认 `tests/test_*.py`。
|
**原则**:只要会访问**网络**、**浏览器**、**真实或仿真外部系统**,应优先放在 `integration`(并保持 `.sample` 直至团队同意启用),而不是写进默认 `tests/test_*.py`。
|
||||||
|
|
||||||
@@ -21,20 +21,15 @@
|
|||||||
|------|------------------------------|----------|
|
|------|------------------------------|----------|
|
||||||
| 本地 / CI 对接**仿真 HTTP** | ``simulator_api`` | 仿真服务如 ``http://localhost:5180`` |
|
| 本地 / CI 对接**仿真 HTTP** | ``simulator_api`` | 仿真服务如 ``http://localhost:5180`` |
|
||||||
| **仿真页面** / 录播 RPA | ``simulator_rpa`` | 本地页面或沙箱浏览器 profile |
|
| **仿真页面** / 录播 RPA | ``simulator_rpa`` | 本地页面或沙箱浏览器 profile |
|
||||||
| **真实 API**(只读优先) | ``real_api`` | ``ALLOW_REAL_API=1``;写操作另需 ``ALLOW_WRITE_ACTIONS=1`` |
|
| **真实 API** | ``real_api`` | **仅手动触发** |
|
||||||
| **真实 RPA**(最高风险) | ``real_rpa`` | ``ALLOW_REAL_RPA=1``;**仅手动触发** |
|
| **真实 RPA**(最高风险) | ``real_rpa`` | **仅手动触发** |
|
||||||
|
|
||||||
## 环境变量(与 ``adapter_test_utils`` 一致)
|
## 环境变量(与 ``adapter_test_utils`` 一致)
|
||||||
|
|
||||||
- **目标档位**(二选一键名,后者为历史拼写):``OPENCLAW_TEST_TARGET`` 或 ``OPENCLOW_TEST_TARGET``
|
- **目标档位**:``OPENCLAW_TEST_TARGET``
|
||||||
取值:``unit`` | ``mock`` | ``simulator_api`` | ``simulator_rpa`` | ``real_api`` | ``real_rpa``
|
取值:``unit`` | ``mock`` | ``simulator_api`` | ``simulator_rpa`` | ``real_api`` | ``real_rpa``
|
||||||
默认未设置等价于 ``unit``。
|
默认未设置等价于 ``unit``。
|
||||||
|
|
||||||
- **授权开关**(显式 ``1`` 才启用):
|
|
||||||
- ``ALLOW_REAL_API=1``
|
|
||||||
- ``ALLOW_REAL_RPA=1``
|
|
||||||
- ``ALLOW_WRITE_ACTIONS=1``(对写操作 / 提交表单类步骤)
|
|
||||||
|
|
||||||
## 安全约束
|
## 安全约束
|
||||||
|
|
||||||
- **禁止**在样例或复制后的测试代码中硬编码真实账号、密码、token、cookie、生产 URL。
|
- **禁止**在样例或复制后的测试代码中硬编码真实账号、密码、token、cookie、生产 URL。
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
# 样例:复制为 test_real_api_contract.py;需 OPENCLAW_TEST_TARGET=real_api 且 ALLOW_REAL_API=1。
|
# 样例:复制为 test_real_api_contract.py;需 OPENCLAW_TEST_TARGET=real_api。
|
||||||
"""真实 HTTP API(只读优先;默认跳过)。"""
|
"""真实 HTTP API(只读优先;默认跳过)。"""
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
@@ -15,7 +15,6 @@ class TestRealApiContractSample(unittest.TestCase):
|
|||||||
raise unittest.SkipTest(reason)
|
raise unittest.SkipTest(reason)
|
||||||
# 写操作示例(复制后使用):
|
# 写操作示例(复制后使用):
|
||||||
# skip_w, _ = should_skip_profile("real_api", write=True)
|
# skip_w, _ = should_skip_profile("real_api", write=True)
|
||||||
# 需要 ALLOW_WRITE_ACTIONS=1
|
|
||||||
# 禁止在此文件写入真实 token;从 vault / 环境注入读取 endpoint。
|
# 禁止在此文件写入真实 token;从 vault / 环境注入读取 endpoint。
|
||||||
self.assertFalse(skip)
|
self.assertFalse(skip)
|
||||||
|
|
||||||
|
|||||||
@@ -1,10 +1,10 @@
|
|||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
# 样例:复制为 test_real_rpa_contract.py;需 OPENCLAW_TEST_TARGET=real_rpa 且 ALLOW_REAL_RPA=1。
|
# 样例:复制为 test_real_rpa_contract.py;需 OPENCLAW_TEST_TARGET=real_rpa。
|
||||||
"""
|
"""
|
||||||
真实 RPA / 桌面自动化(**最高风险**,仅手动触发)。
|
真实 RPA / 桌面自动化(**最高风险**,仅手动触发)。
|
||||||
|
|
||||||
默认只读思路示例:打开沙箱门户、读取标题、**不提交表单**。
|
默认只读思路示例:打开沙箱门户、读取标题、**不提交表单**。
|
||||||
任何写操作必须 ``should_skip_profile('real_rpa', write=True)`` 且 ``ALLOW_WRITE_ACTIONS=1``。
|
写操作测试同样须显式设 ``OPENCLAW_TEST_TARGET=real_rpa`` 后手动触发。
|
||||||
"""
|
"""
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
|
|||||||
237
tests/test_actions_manifest.py
Normal file
237
tests/test_actions_manifest.py
Normal file
@@ -0,0 +1,237 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
"""assets/actions.json 结构与 CLI 映射守护(通用模板,不含具体业务技能名称)。"""
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import json
|
||||||
|
import os
|
||||||
|
import re
|
||||||
|
import unittest
|
||||||
|
|
||||||
|
from _support import get_skill_root
|
||||||
|
|
||||||
|
from cli.app import build_parser
|
||||||
|
from util.constants import SKILL_SLUG
|
||||||
|
|
||||||
|
_TEMPLATE_PLACEHOLDER_SLUG = "your-skill-slug"
|
||||||
|
_ALLOWED_PLACEMENTS = frozenset({"toolbar", "row", "batch", "cron", "agent", "skill-detail"})
|
||||||
|
_RESERVED_PLACEMENTS = frozenset({"row", "batch"})
|
||||||
|
_RESERVED_ACTION_FIELDS = frozenset({"bind", "concurrency", "locks"})
|
||||||
|
_REQUIRED_MANIFEST_KEYS = frozenset({"schemaVersion", "skill", "actions"})
|
||||||
|
_REQUIRED_ACTION_KEYS = frozenset({"id", "label", "description", "placements", "entrypoint"})
|
||||||
|
_FORBIDDEN_BUSINESS_TERMS = (
|
||||||
|
"wechat",
|
||||||
|
"微信",
|
||||||
|
"contacts",
|
||||||
|
"wxid",
|
||||||
|
"account_wxid",
|
||||||
|
"contact.db",
|
||||||
|
"scrape-contacts",
|
||||||
|
"douyin",
|
||||||
|
"抖音",
|
||||||
|
"xiaohongshu",
|
||||||
|
"小红书",
|
||||||
|
"公众号",
|
||||||
|
"logistics",
|
||||||
|
"物流",
|
||||||
|
)
|
||||||
|
|
||||||
|
_TEMPLATE_ARG_PATTERN = re.compile(r"\{\{([a-zA-Z_][a-zA-Z0-9_]*)\}\}")
|
||||||
|
|
||||||
|
|
||||||
|
def _load_actions_manifest() -> dict:
|
||||||
|
path = os.path.join(get_skill_root(), "assets", "actions.json")
|
||||||
|
with open(path, encoding="utf-8") as f:
|
||||||
|
return json.load(f)
|
||||||
|
|
||||||
|
|
||||||
|
def _cli_subcommands() -> set[str]:
|
||||||
|
parser = build_parser()
|
||||||
|
subs = parser._subparsers._group_actions[0].choices # type: ignore[attr-defined]
|
||||||
|
return set(subs.keys())
|
||||||
|
|
||||||
|
|
||||||
|
def _collect_template_vars(args: list) -> set[str]:
|
||||||
|
found: set[str] = set()
|
||||||
|
for item in args:
|
||||||
|
if not isinstance(item, str):
|
||||||
|
continue
|
||||||
|
for match in _TEMPLATE_ARG_PATTERN.finditer(item):
|
||||||
|
found.add(match.group(1))
|
||||||
|
return found
|
||||||
|
|
||||||
|
|
||||||
|
def _load_actions_schema() -> dict:
|
||||||
|
path = os.path.join(get_skill_root(), "assets", "schemas", "skill-actions.schema.json")
|
||||||
|
with open(path, encoding="utf-8") as f:
|
||||||
|
return json.load(f)
|
||||||
|
|
||||||
|
|
||||||
|
class TestActionsManifest(unittest.TestCase):
|
||||||
|
def test_manifest_exists_and_schema(self) -> None:
|
||||||
|
manifest = _load_actions_manifest()
|
||||||
|
self.assertEqual(manifest.get("schemaVersion"), 1)
|
||||||
|
skill = manifest.get("skill")
|
||||||
|
self.assertIn(skill, (SKILL_SLUG, _TEMPLATE_PLACEHOLDER_SLUG))
|
||||||
|
self.assertIsInstance(manifest.get("actions"), list)
|
||||||
|
self.assertGreater(len(manifest["actions"]), 0)
|
||||||
|
|
||||||
|
def test_action_ids_unique_and_cli_valid(self) -> None:
|
||||||
|
manifest = _load_actions_manifest()
|
||||||
|
subs = _cli_subcommands()
|
||||||
|
seen: set[str] = set()
|
||||||
|
for action in manifest["actions"]:
|
||||||
|
action_id = action["id"]
|
||||||
|
self.assertNotIn(action_id, seen, msg=f"duplicate action id: {action_id}")
|
||||||
|
seen.add(action_id)
|
||||||
|
|
||||||
|
self.assertTrue(str(action.get("label", "")).strip(), msg=f"{action_id} label empty")
|
||||||
|
self.assertTrue(str(action.get("description", "")).strip(), msg=f"{action_id} description empty")
|
||||||
|
|
||||||
|
entry = action["entrypoint"]
|
||||||
|
self.assertEqual(entry["type"], "cli")
|
||||||
|
self.assertIn(entry["command"], subs, msg=f"{action_id} command not in CLI")
|
||||||
|
|
||||||
|
args = entry.get("args")
|
||||||
|
self.assertIsInstance(args, list, msg=f"{action_id} args must be array")
|
||||||
|
for item in args:
|
||||||
|
self.assertIsInstance(
|
||||||
|
item,
|
||||||
|
(str, int, float, bool),
|
||||||
|
msg=f"{action_id} arg item must be scalar",
|
||||||
|
)
|
||||||
|
self.assertNotIsInstance(item, dict, msg=f"{action_id} arg must not be object")
|
||||||
|
self.assertIsNotNone(item, msg=f"{action_id} arg must not be null")
|
||||||
|
|
||||||
|
placements = action.get("placements")
|
||||||
|
self.assertIsInstance(placements, list, msg=f"{action_id} placements must be array")
|
||||||
|
self.assertGreater(len(placements), 0, msg=f"{action_id} placements empty")
|
||||||
|
for placement in placements:
|
||||||
|
self.assertIn(placement, _ALLOWED_PLACEMENTS, msg=f"{action_id} invalid placement {placement!r}")
|
||||||
|
|
||||||
|
def test_template_example_does_not_use_reserved_placements(self) -> None:
|
||||||
|
manifest = _load_actions_manifest()
|
||||||
|
for action in manifest["actions"]:
|
||||||
|
placements = set(action.get("placements") or [])
|
||||||
|
overlap = placements & _RESERVED_PLACEMENTS
|
||||||
|
self.assertFalse(
|
||||||
|
overlap,
|
||||||
|
msg=f"{action['id']} uses reserved placements not yet supported in template: {overlap}",
|
||||||
|
)
|
||||||
|
|
||||||
|
def test_template_variables_have_input_schema(self) -> None:
|
||||||
|
manifest = _load_actions_manifest()
|
||||||
|
for action in manifest["actions"]:
|
||||||
|
args = action.get("entrypoint", {}).get("args") or []
|
||||||
|
template_vars = _collect_template_vars(args)
|
||||||
|
if not template_vars:
|
||||||
|
continue
|
||||||
|
schema = action.get("inputSchema") or {}
|
||||||
|
properties = schema.get("properties") or {}
|
||||||
|
for var in template_vars:
|
||||||
|
self.assertIn(
|
||||||
|
var,
|
||||||
|
properties,
|
||||||
|
msg=f"{action['id']} uses {{{{{var}}}}} but inputSchema.properties missing it",
|
||||||
|
)
|
||||||
|
|
||||||
|
def test_required_fields_exist_in_properties(self) -> None:
|
||||||
|
manifest = _load_actions_manifest()
|
||||||
|
for action in manifest["actions"]:
|
||||||
|
schema = action.get("inputSchema") or {}
|
||||||
|
required = schema.get("required") or []
|
||||||
|
properties = schema.get("properties") or {}
|
||||||
|
for field in required:
|
||||||
|
self.assertIn(
|
||||||
|
field,
|
||||||
|
properties,
|
||||||
|
msg=f"{action['id']} required field {field!r} missing from properties",
|
||||||
|
)
|
||||||
|
|
||||||
|
def test_sensitive_params_not_on_toolbar(self) -> None:
|
||||||
|
manifest = _load_actions_manifest()
|
||||||
|
for action in manifest["actions"]:
|
||||||
|
props = (action.get("inputSchema") or {}).get("properties") or {}
|
||||||
|
has_sensitive = any(p.get("sensitive") for p in props.values())
|
||||||
|
if has_sensitive and "toolbar" in (action.get("placements") or []):
|
||||||
|
self.fail(f"{action['id']} has sensitive input but is on toolbar")
|
||||||
|
|
||||||
|
def test_confirmation_message_structure(self) -> None:
|
||||||
|
manifest = _load_actions_manifest()
|
||||||
|
for action in manifest["actions"]:
|
||||||
|
confirmation = action.get("confirmation")
|
||||||
|
if confirmation is None:
|
||||||
|
continue
|
||||||
|
self.assertIsInstance(confirmation, dict)
|
||||||
|
message = confirmation.get("message")
|
||||||
|
self.assertTrue(str(message or "").strip(), msg=f"{action['id']} confirmation.message empty")
|
||||||
|
|
||||||
|
def test_manifest_has_no_specific_business_skill_names(self) -> None:
|
||||||
|
path = os.path.join(get_skill_root(), "assets", "actions.json")
|
||||||
|
with open(path, encoding="utf-8") as f:
|
||||||
|
raw = f.read().lower()
|
||||||
|
for term in _FORBIDDEN_BUSINESS_TERMS:
|
||||||
|
self.assertNotIn(term.lower(), raw, msg=f"actions.json must not contain {term!r}")
|
||||||
|
|
||||||
|
def test_actions_md_exists_and_links_manifest(self) -> None:
|
||||||
|
path = os.path.join(get_skill_root(), "references", "ACTIONS.md")
|
||||||
|
self.assertTrue(os.path.isfile(path))
|
||||||
|
with open(path, encoding="utf-8") as f:
|
||||||
|
text = f.read()
|
||||||
|
self.assertIn("actions.json", text)
|
||||||
|
self.assertIn("schemaVersion", text)
|
||||||
|
for term in _FORBIDDEN_BUSINESS_TERMS:
|
||||||
|
self.assertNotIn(term.lower(), text.lower(), msg=f"ACTIONS.md must not contain {term!r}")
|
||||||
|
|
||||||
|
def test_skill_actions_schema_file_exists(self) -> None:
|
||||||
|
path = os.path.join(get_skill_root(), "assets", "schemas", "skill-actions.schema.json")
|
||||||
|
self.assertTrue(os.path.isfile(path))
|
||||||
|
with open(path, encoding="utf-8") as f:
|
||||||
|
schema = json.load(f)
|
||||||
|
self.assertEqual(schema.get("title"), "SkillActionManifest")
|
||||||
|
self.assertIn("actions", schema.get("properties", {}))
|
||||||
|
|
||||||
|
def test_skill_actions_schema_json_parses(self) -> None:
|
||||||
|
schema = _load_actions_schema()
|
||||||
|
self.assertEqual(schema["properties"]["schemaVersion"]["const"], 1)
|
||||||
|
self.assertTrue(schema.get("additionalProperties") is False)
|
||||||
|
self.assertIn("action", schema.get("$defs", {}))
|
||||||
|
|
||||||
|
def test_template_manifest_matches_schema_required_keys(self) -> None:
|
||||||
|
manifest = _load_actions_manifest()
|
||||||
|
schema = _load_actions_schema()
|
||||||
|
for key in _REQUIRED_MANIFEST_KEYS:
|
||||||
|
self.assertIn(key, manifest)
|
||||||
|
self.assertIn(key, schema.get("properties", {}))
|
||||||
|
for action in manifest["actions"]:
|
||||||
|
for key in _REQUIRED_ACTION_KEYS:
|
||||||
|
self.assertIn(key, action)
|
||||||
|
|
||||||
|
def test_template_example_does_not_use_reserved_action_fields(self) -> None:
|
||||||
|
manifest = _load_actions_manifest()
|
||||||
|
for action in manifest["actions"]:
|
||||||
|
overlap = set(action.keys()) & _RESERVED_ACTION_FIELDS
|
||||||
|
self.assertFalse(
|
||||||
|
overlap,
|
||||||
|
msg=f"{action['id']} uses reserved action fields not supported in template Phase 1: {overlap}",
|
||||||
|
)
|
||||||
|
|
||||||
|
def test_actions_md_documents_strict_vs_host_boundary(self) -> None:
|
||||||
|
path = os.path.join(get_skill_root(), "references", "ACTIONS.md")
|
||||||
|
with open(path, encoding="utf-8") as f:
|
||||||
|
text = f.read()
|
||||||
|
for marker in (
|
||||||
|
"严格规范",
|
||||||
|
"向后兼容",
|
||||||
|
"Phase 1",
|
||||||
|
"bind",
|
||||||
|
"concurrency",
|
||||||
|
"locks",
|
||||||
|
"row",
|
||||||
|
"batch",
|
||||||
|
"additionalProperties",
|
||||||
|
):
|
||||||
|
self.assertIn(marker, text, msg=f"ACTIONS.md missing {marker!r}")
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
unittest.main()
|
||||||
@@ -5,6 +5,8 @@ from __future__ import annotations
|
|||||||
import os
|
import os
|
||||||
import unittest
|
import unittest
|
||||||
|
|
||||||
|
from jiangchang_skill_core import config
|
||||||
|
|
||||||
from adapter_test_utils import (
|
from adapter_test_utils import (
|
||||||
ALLOWED_TEST_TARGETS,
|
ALLOWED_TEST_TARGETS,
|
||||||
FakeAdapter,
|
FakeAdapter,
|
||||||
@@ -24,34 +26,22 @@ def _restore_env(saved: dict[str, str | None]) -> None:
|
|||||||
os.environ.pop(k, None)
|
os.environ.pop(k, None)
|
||||||
else:
|
else:
|
||||||
os.environ[k] = v
|
os.environ[k] = v
|
||||||
|
config.reset_cache()
|
||||||
|
|
||||||
|
|
||||||
_ENV_KEYS = (
|
_ENV_KEYS = ("OPENCLAW_TEST_TARGET",)
|
||||||
"OPENCLAW_TEST_TARGET",
|
|
||||||
"OPENCLOW_TEST_TARGET",
|
|
||||||
"ALLOW_REAL_API",
|
|
||||||
"ALLOW_REAL_RPA",
|
|
||||||
"ALLOW_WRITE_ACTIONS",
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
class TestAdapterProfilePolicy(unittest.TestCase):
|
class TestAdapterProfilePolicy(unittest.TestCase):
|
||||||
def test_get_test_target_reads_opencylow_typo_alias(self) -> None:
|
def tearDown(self) -> None:
|
||||||
"""兼容环境变量历史拼写 OPENCLOW_TEST_TARGET。"""
|
config.reset_cache()
|
||||||
saved = _save_env(_ENV_KEYS)
|
|
||||||
try:
|
|
||||||
os.environ.pop("OPENCLAW_TEST_TARGET", None)
|
|
||||||
os.environ.pop("OPENCLOW_TEST_TARGET", None)
|
|
||||||
os.environ["OPENCLOW_TEST_TARGET"] = "mock"
|
|
||||||
self.assertEqual(get_test_target(), "mock")
|
|
||||||
finally:
|
|
||||||
_restore_env(saved)
|
|
||||||
|
|
||||||
def test_get_test_target_defaults_to_unit(self) -> None:
|
def test_get_test_target_defaults_to_unit(self) -> None:
|
||||||
saved = _save_env(_ENV_KEYS)
|
saved = _save_env(_ENV_KEYS)
|
||||||
try:
|
try:
|
||||||
for k in _ENV_KEYS:
|
for k in _ENV_KEYS:
|
||||||
os.environ.pop(k, None)
|
os.environ.pop(k, None)
|
||||||
|
config.reset_cache()
|
||||||
self.assertEqual(get_test_target(), "unit")
|
self.assertEqual(get_test_target(), "unit")
|
||||||
finally:
|
finally:
|
||||||
_restore_env(saved)
|
_restore_env(saved)
|
||||||
@@ -62,6 +52,7 @@ class TestAdapterProfilePolicy(unittest.TestCase):
|
|||||||
for k in _ENV_KEYS:
|
for k in _ENV_KEYS:
|
||||||
os.environ.pop(k, None)
|
os.environ.pop(k, None)
|
||||||
os.environ["OPENCLAW_TEST_TARGET"] = "not_a_valid_target"
|
os.environ["OPENCLAW_TEST_TARGET"] = "not_a_valid_target"
|
||||||
|
config.reset_cache()
|
||||||
with self.assertRaises(ValueError):
|
with self.assertRaises(ValueError):
|
||||||
get_test_target()
|
get_test_target()
|
||||||
finally:
|
finally:
|
||||||
@@ -74,6 +65,7 @@ class TestAdapterProfilePolicy(unittest.TestCase):
|
|||||||
saved = _save_env(_ENV_KEYS)
|
saved = _save_env(_ENV_KEYS)
|
||||||
try:
|
try:
|
||||||
os.environ["OPENCLAW_TEST_TARGET"] = "simulator_api"
|
os.environ["OPENCLAW_TEST_TARGET"] = "simulator_api"
|
||||||
|
config.reset_cache()
|
||||||
skip, _ = should_skip_profile("simulator_api")
|
skip, _ = should_skip_profile("simulator_api")
|
||||||
self.assertFalse(skip)
|
self.assertFalse(skip)
|
||||||
finally:
|
finally:
|
||||||
@@ -84,6 +76,7 @@ class TestAdapterProfilePolicy(unittest.TestCase):
|
|||||||
try:
|
try:
|
||||||
for k in _ENV_KEYS:
|
for k in _ENV_KEYS:
|
||||||
os.environ.pop(k, None)
|
os.environ.pop(k, None)
|
||||||
|
config.reset_cache()
|
||||||
self.assertEqual(get_test_target(), "unit")
|
self.assertEqual(get_test_target(), "unit")
|
||||||
for prof in ("simulator_api", "simulator_rpa", "real_api", "real_rpa"):
|
for prof in ("simulator_api", "simulator_rpa", "real_api", "real_rpa"):
|
||||||
skip, reason = should_skip_profile(prof)
|
skip, reason = should_skip_profile(prof)
|
||||||
@@ -93,46 +86,23 @@ class TestAdapterProfilePolicy(unittest.TestCase):
|
|||||||
finally:
|
finally:
|
||||||
_restore_env(saved)
|
_restore_env(saved)
|
||||||
|
|
||||||
def test_real_api_requires_allow_flag(self) -> None:
|
def test_real_api_runs_when_target_matches(self) -> None:
|
||||||
saved = _save_env(_ENV_KEYS)
|
saved = _save_env(_ENV_KEYS)
|
||||||
try:
|
try:
|
||||||
os.environ["OPENCLAW_TEST_TARGET"] = "real_api"
|
os.environ["OPENCLAW_TEST_TARGET"] = "real_api"
|
||||||
os.environ.pop("ALLOW_REAL_API", None)
|
config.reset_cache()
|
||||||
skip, reason = should_skip_profile("real_api")
|
skip, _ = should_skip_profile("real_api", write=True)
|
||||||
self.assertTrue(skip)
|
self.assertFalse(skip)
|
||||||
self.assertIn("ALLOW_REAL_API", reason)
|
|
||||||
os.environ["ALLOW_REAL_API"] = "1"
|
|
||||||
skip2, _ = should_skip_profile("real_api", write=False)
|
|
||||||
self.assertFalse(skip2)
|
|
||||||
finally:
|
finally:
|
||||||
_restore_env(saved)
|
_restore_env(saved)
|
||||||
|
|
||||||
def test_real_api_write_requires_allow_write_actions(self) -> None:
|
def test_real_rpa_runs_when_target_matches(self) -> None:
|
||||||
saved = _save_env(_ENV_KEYS)
|
|
||||||
try:
|
|
||||||
os.environ["OPENCLAW_TEST_TARGET"] = "real_api"
|
|
||||||
os.environ["ALLOW_REAL_API"] = "1"
|
|
||||||
os.environ.pop("ALLOW_WRITE_ACTIONS", None)
|
|
||||||
skip, reason = should_skip_profile("real_api", write=True)
|
|
||||||
self.assertTrue(skip)
|
|
||||||
self.assertIn("ALLOW_WRITE_ACTIONS", reason)
|
|
||||||
os.environ["ALLOW_WRITE_ACTIONS"] = "1"
|
|
||||||
skip2, _ = should_skip_profile("real_api", write=True)
|
|
||||||
self.assertFalse(skip2)
|
|
||||||
finally:
|
|
||||||
_restore_env(saved)
|
|
||||||
|
|
||||||
def test_real_rpa_requires_allow_rpa_flag(self) -> None:
|
|
||||||
saved = _save_env(_ENV_KEYS)
|
saved = _save_env(_ENV_KEYS)
|
||||||
try:
|
try:
|
||||||
os.environ["OPENCLAW_TEST_TARGET"] = "real_rpa"
|
os.environ["OPENCLAW_TEST_TARGET"] = "real_rpa"
|
||||||
os.environ.pop("ALLOW_REAL_RPA", None)
|
config.reset_cache()
|
||||||
skip, reason = should_skip_profile("real_rpa")
|
skip, _ = should_skip_profile("real_rpa", write=True)
|
||||||
self.assertTrue(skip)
|
self.assertFalse(skip)
|
||||||
self.assertIn("ALLOW_REAL_RPA", reason)
|
|
||||||
os.environ["ALLOW_REAL_RPA"] = "1"
|
|
||||||
skip2, _ = should_skip_profile("real_rpa", write=False)
|
|
||||||
self.assertFalse(skip2)
|
|
||||||
finally:
|
finally:
|
||||||
_restore_env(saved)
|
_restore_env(saved)
|
||||||
|
|
||||||
@@ -146,10 +116,10 @@ class TestAdapterProfilePolicy(unittest.TestCase):
|
|||||||
FakeAdapter("unauthorized").call({})
|
FakeAdapter("unauthorized").call({})
|
||||||
|
|
||||||
def test_assert_no_real_network_env(self) -> None:
|
def test_assert_no_real_network_env(self) -> None:
|
||||||
saved = _save_env(("ALLOW_REAL_API", "ALLOW_REAL_RPA"))
|
saved = _save_env(_ENV_KEYS)
|
||||||
try:
|
try:
|
||||||
os.environ.pop("ALLOW_REAL_API", None)
|
os.environ.pop("OPENCLAW_TEST_TARGET", None)
|
||||||
os.environ.pop("ALLOW_REAL_RPA", None)
|
config.reset_cache()
|
||||||
assert_no_real_network_env()
|
assert_no_real_network_env()
|
||||||
finally:
|
finally:
|
||||||
_restore_env(saved)
|
_restore_env(saved)
|
||||||
|
|||||||
@@ -16,6 +16,12 @@ from cli.app import main
|
|||||||
from util.config_bootstrap import bootstrap_skill_config
|
from util.config_bootstrap import bootstrap_skill_config
|
||||||
from util.constants import SKILL_SLUG
|
from util.constants import SKILL_SLUG
|
||||||
|
|
||||||
|
_TEST_TARGET_KEY = "OPENCLAW_" + "TEST_TARGET"
|
||||||
|
|
||||||
|
|
||||||
|
def _env_line(key: str, value: str) -> str:
|
||||||
|
return f"{key}={value}\n"
|
||||||
|
|
||||||
|
|
||||||
class TestConfigBootstrap(unittest.TestCase):
|
class TestConfigBootstrap(unittest.TestCase):
|
||||||
def test_first_run_creates_user_env(self) -> None:
|
def test_first_run_creates_user_env(self) -> None:
|
||||||
@@ -30,16 +36,16 @@ class TestConfigBootstrap(unittest.TestCase):
|
|||||||
config.reset_cache()
|
config.reset_cache()
|
||||||
path = bootstrap_skill_config()
|
path = bootstrap_skill_config()
|
||||||
with open(path, "w", encoding="utf-8") as f:
|
with open(path, "w", encoding="utf-8") as f:
|
||||||
f.write("OPENCLAW_TEST_TARGET=simulator_rpa\n")
|
f.write(_env_line(_TEST_TARGET_KEY, "simulator_rpa"))
|
||||||
with tempfile.NamedTemporaryFile("w", encoding="utf-8", delete=False, suffix=".example") as tmp:
|
with tempfile.NamedTemporaryFile("w", encoding="utf-8", delete=False, suffix=".example") as tmp:
|
||||||
tmp.write("OPENCLAW_TEST_TARGET=mock\nHUMAN_WAIT_TIMEOUT=60\n")
|
tmp.write(_env_line(_TEST_TARGET_KEY, "mock") + "HUMAN_WAIT_TIMEOUT=60\n")
|
||||||
tmp_path = tmp.name
|
tmp_path = tmp.name
|
||||||
try:
|
try:
|
||||||
config.merge_missing_env_keys(tmp_path, path, comment_skill="test")
|
config.merge_missing_env_keys(tmp_path, path, comment_skill="test")
|
||||||
with open(path, encoding="utf-8") as f:
|
with open(path, encoding="utf-8") as f:
|
||||||
content = f.read()
|
content = f.read()
|
||||||
self.assertIn("OPENCLAW_TEST_TARGET=simulator_rpa", content)
|
self.assertIn(_env_line(_TEST_TARGET_KEY, "simulator_rpa").strip(), content)
|
||||||
self.assertNotIn("OPENCLAW_TEST_TARGET=mock", content)
|
self.assertNotIn(_env_line(_TEST_TARGET_KEY, "mock").strip(), content)
|
||||||
finally:
|
finally:
|
||||||
os.unlink(tmp_path)
|
os.unlink(tmp_path)
|
||||||
config.reset_cache()
|
config.reset_cache()
|
||||||
@@ -49,7 +55,7 @@ class TestConfigBootstrap(unittest.TestCase):
|
|||||||
config.reset_cache()
|
config.reset_cache()
|
||||||
path = bootstrap_skill_config()
|
path = bootstrap_skill_config()
|
||||||
with open(path, "w", encoding="utf-8") as f:
|
with open(path, "w", encoding="utf-8") as f:
|
||||||
f.write("OPENCLAW_TEST_TARGET=simulator_rpa\n")
|
f.write(_env_line(_TEST_TARGET_KEY, "simulator_rpa"))
|
||||||
example = os.path.join(get_skill_root(), ".env.example")
|
example = os.path.join(get_skill_root(), ".env.example")
|
||||||
added = config.merge_missing_env_keys(example, path, comment_skill="test")
|
added = config.merge_missing_env_keys(example, path, comment_skill="test")
|
||||||
self.assertIn("HUMAN_WAIT_TIMEOUT", added)
|
self.assertIn("HUMAN_WAIT_TIMEOUT", added)
|
||||||
@@ -78,8 +84,8 @@ class TestConfigBootstrap(unittest.TestCase):
|
|||||||
config.reset_cache()
|
config.reset_cache()
|
||||||
with open(example, encoding="utf-8") as f:
|
with open(example, encoding="utf-8") as f:
|
||||||
example_text = f.read()
|
example_text = f.read()
|
||||||
self.assertIn("OPENCLAW_TEST_TARGET=mock", example_text)
|
self.assertIn(_env_line(_TEST_TARGET_KEY, "mock").strip(), example_text)
|
||||||
self.assertEqual(config.get("OPENCLAW_TEST_TARGET"), "mock")
|
self.assertEqual(config.get(_TEST_TARGET_KEY), "mock")
|
||||||
|
|
||||||
def test_config_path_outputs_json(self) -> None:
|
def test_config_path_outputs_json(self) -> None:
|
||||||
with IsolatedDataRoot(user_id="_cfg_path"):
|
with IsolatedDataRoot(user_id="_cfg_path"):
|
||||||
|
|||||||
479
tests/test_data_management_contract.py
Normal file
479
tests/test_data_management_contract.py
Normal file
@@ -0,0 +1,479 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
"""通用数据管理契约测试(demo_items fixture,不含具体业务技能内容)。"""
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import json
|
||||||
|
import sqlite3
|
||||||
|
import unittest
|
||||||
|
|
||||||
|
from _support import IsolatedDataRoot
|
||||||
|
|
||||||
|
DEMO_ITEMS_DDL = """
|
||||||
|
CREATE TABLE IF NOT EXISTS demo_items (
|
||||||
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||||
|
title TEXT NOT NULL,
|
||||||
|
description TEXT,
|
||||||
|
status TEXT NOT NULL DEFAULT 'draft',
|
||||||
|
generated_value TEXT,
|
||||||
|
created_at INTEGER NOT NULL DEFAULT (unixepoch()),
|
||||||
|
updated_at INTEGER NOT NULL DEFAULT (unixepoch())
|
||||||
|
);
|
||||||
|
CREATE TRIGGER IF NOT EXISTS demo_items_set_updated_at
|
||||||
|
AFTER UPDATE ON demo_items
|
||||||
|
FOR EACH ROW
|
||||||
|
BEGIN
|
||||||
|
UPDATE demo_items
|
||||||
|
SET updated_at = unixepoch()
|
||||||
|
WHERE id = NEW.id AND updated_at = OLD.updated_at;
|
||||||
|
END;
|
||||||
|
"""
|
||||||
|
|
||||||
|
DEMO_ITEMS_TABLE_METADATA = {
|
||||||
|
"table_name": "demo_items",
|
||||||
|
"display_name": "示例记录",
|
||||||
|
"description": "通用数据管理契约演示表",
|
||||||
|
"visible": 1,
|
||||||
|
"readonly": 0,
|
||||||
|
}
|
||||||
|
|
||||||
|
DEMO_ITEMS_COLUMN_METADATA: tuple[dict[str, object], ...] = (
|
||||||
|
{"column_name": "id", "display_name": "编号", "visible": 1, "searchable": 1, "editable": 0},
|
||||||
|
{"column_name": "title", "display_name": "标题", "visible": 1, "searchable": 1, "editable": 1},
|
||||||
|
{"column_name": "description", "display_name": "描述", "visible": 1, "searchable": 1, "editable": 1},
|
||||||
|
{
|
||||||
|
"column_name": "status",
|
||||||
|
"display_name": "状态",
|
||||||
|
"visible": 1,
|
||||||
|
"searchable": 1,
|
||||||
|
"editable": 1,
|
||||||
|
"options_json": json.dumps(
|
||||||
|
[
|
||||||
|
{"value": "draft", "label": "草稿"},
|
||||||
|
{"value": "published", "label": "已发布"},
|
||||||
|
{"value": "archived", "label": "已归档"},
|
||||||
|
],
|
||||||
|
ensure_ascii=False,
|
||||||
|
),
|
||||||
|
},
|
||||||
|
{"column_name": "generated_value", "display_name": "生成值", "visible": 1, "searchable": 0, "editable": 0},
|
||||||
|
{
|
||||||
|
"column_name": "created_at",
|
||||||
|
"display_name": "创建时间",
|
||||||
|
"visible": 1,
|
||||||
|
"searchable": 0,
|
||||||
|
"editable": 0,
|
||||||
|
"display_type": "datetime_unix_seconds",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"column_name": "updated_at",
|
||||||
|
"display_name": "更新时间",
|
||||||
|
"visible": 1,
|
||||||
|
"searchable": 0,
|
||||||
|
"editable": 0,
|
||||||
|
"display_type": "datetime_unix_seconds",
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def _init_demo_items_fixture(conn: sqlite3.Connection, *, prune: bool = True) -> None:
|
||||||
|
from db.display_metadata import (
|
||||||
|
init_display_metadata_tables,
|
||||||
|
list_physical_column_names,
|
||||||
|
seed_column_metadata_batch,
|
||||||
|
upsert_table_metadata,
|
||||||
|
)
|
||||||
|
|
||||||
|
cur = conn.cursor()
|
||||||
|
init_display_metadata_tables(cur)
|
||||||
|
cur.executescript(DEMO_ITEMS_DDL)
|
||||||
|
meta = DEMO_ITEMS_TABLE_METADATA
|
||||||
|
upsert_table_metadata(
|
||||||
|
cur,
|
||||||
|
table_name=str(meta["table_name"]),
|
||||||
|
display_name=str(meta["display_name"]),
|
||||||
|
description=str(meta["description"]),
|
||||||
|
visible=int(meta["visible"]),
|
||||||
|
readonly=int(meta["readonly"]),
|
||||||
|
)
|
||||||
|
physical = list_physical_column_names(cur, "demo_items")
|
||||||
|
seed_column_metadata_batch(
|
||||||
|
cur,
|
||||||
|
table_name="demo_items",
|
||||||
|
columns=DEMO_ITEMS_COLUMN_METADATA,
|
||||||
|
valid_column_names=physical if prune else None,
|
||||||
|
)
|
||||||
|
conn.commit()
|
||||||
|
|
||||||
|
|
||||||
|
class TestDataManagementContract(unittest.TestCase):
|
||||||
|
def test_demo_items_metadata_permissions(self) -> None:
|
||||||
|
conn = sqlite3.connect(":memory:")
|
||||||
|
try:
|
||||||
|
_init_demo_items_fixture(conn)
|
||||||
|
from db.display_metadata import (
|
||||||
|
get_column_editable,
|
||||||
|
get_table_readonly,
|
||||||
|
list_editable_business_columns,
|
||||||
|
table_allows_manual_create,
|
||||||
|
)
|
||||||
|
|
||||||
|
cur = conn.cursor()
|
||||||
|
self.assertEqual(get_table_readonly(cur, "demo_items"), 0)
|
||||||
|
self.assertEqual(get_column_editable(cur, "demo_items", "id"), 0)
|
||||||
|
self.assertEqual(get_column_editable(cur, "demo_items", "title"), 1)
|
||||||
|
self.assertEqual(get_column_editable(cur, "demo_items", "generated_value"), 0)
|
||||||
|
self.assertEqual(get_column_editable(cur, "demo_items", "created_at"), 0)
|
||||||
|
self.assertEqual(get_column_editable(cur, "demo_items", "updated_at"), 0)
|
||||||
|
|
||||||
|
editable_cols = list_editable_business_columns(cur, "demo_items")
|
||||||
|
self.assertEqual(editable_cols, ["title", "description", "status"])
|
||||||
|
self.assertTrue(table_allows_manual_create(cur, "demo_items"))
|
||||||
|
finally:
|
||||||
|
conn.close()
|
||||||
|
|
||||||
|
def test_demo_items_init_idempotent_no_duplicates(self) -> None:
|
||||||
|
conn = sqlite3.connect(":memory:")
|
||||||
|
try:
|
||||||
|
_init_demo_items_fixture(conn)
|
||||||
|
_init_demo_items_fixture(conn)
|
||||||
|
cur = conn.cursor()
|
||||||
|
cur.execute("SELECT COUNT(*) FROM _jiangchang_tables WHERE table_name = 'demo_items'")
|
||||||
|
self.assertEqual(int(cur.fetchone()[0]), 1)
|
||||||
|
cur.execute("SELECT COUNT(*) FROM _jiangchang_columns WHERE table_name = 'demo_items'")
|
||||||
|
self.assertEqual(int(cur.fetchone()[0]), 7)
|
||||||
|
finally:
|
||||||
|
conn.close()
|
||||||
|
|
||||||
|
def test_reinit_updates_stale_editable_value(self) -> None:
|
||||||
|
conn = sqlite3.connect(":memory:")
|
||||||
|
try:
|
||||||
|
_init_demo_items_fixture(conn)
|
||||||
|
conn.execute(
|
||||||
|
"UPDATE _jiangchang_columns SET editable = 0 WHERE table_name = 'demo_items' AND column_name = 'title'"
|
||||||
|
)
|
||||||
|
conn.commit()
|
||||||
|
_init_demo_items_fixture(conn)
|
||||||
|
from db.display_metadata import get_column_editable
|
||||||
|
|
||||||
|
self.assertEqual(get_column_editable(conn.cursor(), "demo_items", "title"), 1)
|
||||||
|
finally:
|
||||||
|
conn.close()
|
||||||
|
|
||||||
|
def test_missing_table_metadata_detected(self) -> None:
|
||||||
|
conn = sqlite3.connect(":memory:")
|
||||||
|
try:
|
||||||
|
_init_demo_items_fixture(conn)
|
||||||
|
conn.execute("DELETE FROM _jiangchang_tables WHERE table_name = 'demo_items'")
|
||||||
|
conn.commit()
|
||||||
|
from db.display_metadata_validator import validate_display_metadata
|
||||||
|
|
||||||
|
report = validate_display_metadata(conn)
|
||||||
|
self.assertFalse(report.ok)
|
||||||
|
self.assertTrue(any("demo_items" in err for err in report.errors))
|
||||||
|
finally:
|
||||||
|
conn.close()
|
||||||
|
|
||||||
|
def test_missing_column_metadata_detected(self) -> None:
|
||||||
|
conn = sqlite3.connect(":memory:")
|
||||||
|
try:
|
||||||
|
_init_demo_items_fixture(conn)
|
||||||
|
conn.execute(
|
||||||
|
"DELETE FROM _jiangchang_columns WHERE table_name = 'demo_items' AND column_name = 'title'"
|
||||||
|
)
|
||||||
|
conn.commit()
|
||||||
|
from db.display_metadata_validator import validate_display_metadata
|
||||||
|
|
||||||
|
report = validate_display_metadata(conn)
|
||||||
|
self.assertFalse(report.ok)
|
||||||
|
self.assertTrue(any("demo_items.title" in err for err in report.errors))
|
||||||
|
finally:
|
||||||
|
conn.close()
|
||||||
|
|
||||||
|
def test_metadata_reference_to_missing_column_detected(self) -> None:
|
||||||
|
conn = sqlite3.connect(":memory:")
|
||||||
|
try:
|
||||||
|
_init_demo_items_fixture(conn)
|
||||||
|
conn.execute(
|
||||||
|
"""
|
||||||
|
INSERT INTO _jiangchang_columns
|
||||||
|
(table_name, column_name, display_name, visible, searchable, editable)
|
||||||
|
VALUES ('demo_items', 'not_exists', '不存在', 1, 1, 0)
|
||||||
|
"""
|
||||||
|
)
|
||||||
|
conn.commit()
|
||||||
|
from db.display_metadata_validator import validate_display_metadata
|
||||||
|
|
||||||
|
report = validate_display_metadata(conn)
|
||||||
|
self.assertFalse(report.ok)
|
||||||
|
self.assertTrue(any("not_exists" in err for err in report.errors))
|
||||||
|
finally:
|
||||||
|
conn.close()
|
||||||
|
|
||||||
|
def test_empty_display_name_detected_for_visible_column(self) -> None:
|
||||||
|
conn = sqlite3.connect(":memory:")
|
||||||
|
try:
|
||||||
|
_init_demo_items_fixture(conn)
|
||||||
|
conn.execute(
|
||||||
|
"UPDATE _jiangchang_columns SET display_name = '' WHERE table_name = 'demo_items' AND column_name = 'title'"
|
||||||
|
)
|
||||||
|
conn.commit()
|
||||||
|
from db.display_metadata_validator import validate_display_metadata
|
||||||
|
|
||||||
|
report = validate_display_metadata(conn)
|
||||||
|
self.assertFalse(report.ok)
|
||||||
|
self.assertTrue(any("display_name 为空" in err for err in report.errors))
|
||||||
|
finally:
|
||||||
|
conn.close()
|
||||||
|
|
||||||
|
def test_visible_columns_use_chinese_display_name(self) -> None:
|
||||||
|
conn = sqlite3.connect(":memory:")
|
||||||
|
try:
|
||||||
|
_init_demo_items_fixture(conn)
|
||||||
|
cur = conn.cursor()
|
||||||
|
cur.execute(
|
||||||
|
"""
|
||||||
|
SELECT column_name, display_name
|
||||||
|
FROM _jiangchang_columns
|
||||||
|
WHERE table_name = 'demo_items' AND visible = 1
|
||||||
|
"""
|
||||||
|
)
|
||||||
|
for column_name, display_name in cur.fetchall():
|
||||||
|
self.assertTrue(
|
||||||
|
any("\u4e00" <= ch <= "\u9fff" for ch in str(display_name)),
|
||||||
|
msg=f"{column_name} display_name should be Chinese: {display_name!r}",
|
||||||
|
)
|
||||||
|
finally:
|
||||||
|
conn.close()
|
||||||
|
|
||||||
|
def test_timestamp_fields_type_default_and_editable(self) -> None:
|
||||||
|
conn = sqlite3.connect(":memory:")
|
||||||
|
try:
|
||||||
|
_init_demo_items_fixture(conn)
|
||||||
|
cur = conn.cursor()
|
||||||
|
for column_name in ("created_at", "updated_at"):
|
||||||
|
cur.execute(f"PRAGMA table_info('demo_items')")
|
||||||
|
info = {row[1]: row for row in cur.fetchall()}
|
||||||
|
row = info[column_name]
|
||||||
|
self.assertIn("INT", str(row[2]).upper())
|
||||||
|
self.assertIn("unixepoch", str(row[4]).lower())
|
||||||
|
cur.execute(
|
||||||
|
"""
|
||||||
|
SELECT editable, display_type FROM _jiangchang_columns
|
||||||
|
WHERE table_name = 'demo_items' AND column_name = ?
|
||||||
|
""",
|
||||||
|
(column_name,),
|
||||||
|
)
|
||||||
|
editable, display_type = cur.fetchone()
|
||||||
|
self.assertEqual(int(editable), 0)
|
||||||
|
self.assertEqual(display_type, "datetime_unix_seconds")
|
||||||
|
finally:
|
||||||
|
conn.close()
|
||||||
|
|
||||||
|
def test_physical_order_matches_pragma_cid(self) -> None:
|
||||||
|
conn = sqlite3.connect(":memory:")
|
||||||
|
try:
|
||||||
|
_init_demo_items_fixture(conn)
|
||||||
|
from db.display_metadata import list_physical_column_names
|
||||||
|
|
||||||
|
cur = conn.cursor()
|
||||||
|
expected = [str(item["column_name"]) for item in DEMO_ITEMS_COLUMN_METADATA]
|
||||||
|
physical = list_physical_column_names(cur, "demo_items")
|
||||||
|
self.assertEqual(physical, expected)
|
||||||
|
cur.execute("PRAGMA table_info('demo_items')")
|
||||||
|
cid_order = [row[1] for row in sorted(cur.fetchall(), key=lambda row: int(row[0]))]
|
||||||
|
self.assertEqual(cid_order, expected)
|
||||||
|
finally:
|
||||||
|
conn.close()
|
||||||
|
|
||||||
|
def test_options_json_parses_for_enum_field(self) -> None:
|
||||||
|
conn = sqlite3.connect(":memory:")
|
||||||
|
try:
|
||||||
|
_init_demo_items_fixture(conn)
|
||||||
|
cur = conn.cursor()
|
||||||
|
cur.execute(
|
||||||
|
"""
|
||||||
|
SELECT options_json FROM _jiangchang_columns
|
||||||
|
WHERE table_name = 'demo_items' AND column_name = 'status'
|
||||||
|
"""
|
||||||
|
)
|
||||||
|
raw = cur.fetchone()[0]
|
||||||
|
from db.display_metadata_validator import validate_options_json
|
||||||
|
|
||||||
|
errors, _ = validate_options_json(raw)
|
||||||
|
self.assertEqual(errors, [])
|
||||||
|
parsed = json.loads(raw)
|
||||||
|
self.assertEqual(len(parsed), 3)
|
||||||
|
self.assertEqual(parsed[0]["value"], "draft")
|
||||||
|
finally:
|
||||||
|
conn.close()
|
||||||
|
|
||||||
|
def test_system_table_no_editable_fields_not_manual_create(self) -> None:
|
||||||
|
conn = sqlite3.connect(":memory:")
|
||||||
|
try:
|
||||||
|
from db.display_metadata import (
|
||||||
|
init_display_metadata_tables,
|
||||||
|
seed_column_metadata_batch,
|
||||||
|
table_allows_manual_create,
|
||||||
|
upsert_table_metadata,
|
||||||
|
)
|
||||||
|
|
||||||
|
cur = conn.cursor()
|
||||||
|
init_display_metadata_tables(cur)
|
||||||
|
cur.execute(
|
||||||
|
"""
|
||||||
|
CREATE TABLE source_records (
|
||||||
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||||
|
item_id TEXT NOT NULL,
|
||||||
|
synced_at INTEGER NOT NULL DEFAULT (unixepoch())
|
||||||
|
)
|
||||||
|
"""
|
||||||
|
)
|
||||||
|
upsert_table_metadata(
|
||||||
|
cur,
|
||||||
|
table_name="source_records",
|
||||||
|
display_name="来源记录",
|
||||||
|
description="只由同步流程写入",
|
||||||
|
visible=1,
|
||||||
|
readonly=0,
|
||||||
|
)
|
||||||
|
seed_column_metadata_batch(
|
||||||
|
cur,
|
||||||
|
table_name="source_records",
|
||||||
|
columns=(
|
||||||
|
{"column_name": "id", "display_name": "编号", "editable": 0},
|
||||||
|
{"column_name": "item_id", "display_name": "记录编号", "editable": 0},
|
||||||
|
{"column_name": "synced_at", "display_name": "同步时间", "editable": 0, "display_type": "datetime_unix_seconds"},
|
||||||
|
),
|
||||||
|
)
|
||||||
|
conn.commit()
|
||||||
|
self.assertFalse(table_allows_manual_create(cur, "source_records"))
|
||||||
|
finally:
|
||||||
|
conn.close()
|
||||||
|
|
||||||
|
def test_table_readonly_blocks_manual_create_even_with_editable_fields(self) -> None:
|
||||||
|
conn = sqlite3.connect(":memory:")
|
||||||
|
try:
|
||||||
|
_init_demo_items_fixture(conn)
|
||||||
|
conn.execute("UPDATE _jiangchang_tables SET readonly = 1 WHERE table_name = 'demo_items'")
|
||||||
|
conn.commit()
|
||||||
|
from db.display_metadata import table_allows_manual_create
|
||||||
|
|
||||||
|
self.assertFalse(table_allows_manual_create(conn.cursor(), "demo_items"))
|
||||||
|
finally:
|
||||||
|
conn.close()
|
||||||
|
|
||||||
|
def test_prune_stale_column_metadata(self) -> None:
|
||||||
|
conn = sqlite3.connect(":memory:")
|
||||||
|
try:
|
||||||
|
_init_demo_items_fixture(conn)
|
||||||
|
conn.execute(
|
||||||
|
"""
|
||||||
|
INSERT INTO _jiangchang_columns
|
||||||
|
(table_name, column_name, display_name, visible, searchable, editable)
|
||||||
|
VALUES ('demo_items', 'removed_field', '已删除', 0, 0, 0)
|
||||||
|
"""
|
||||||
|
)
|
||||||
|
conn.commit()
|
||||||
|
from db.display_metadata import list_physical_column_names, seed_column_metadata_batch
|
||||||
|
|
||||||
|
cur = conn.cursor()
|
||||||
|
physical = list_physical_column_names(cur, "demo_items")
|
||||||
|
seed_column_metadata_batch(
|
||||||
|
cur,
|
||||||
|
table_name="demo_items",
|
||||||
|
columns=DEMO_ITEMS_COLUMN_METADATA,
|
||||||
|
valid_column_names=physical,
|
||||||
|
)
|
||||||
|
conn.commit()
|
||||||
|
cur.execute(
|
||||||
|
"SELECT 1 FROM _jiangchang_columns WHERE table_name = 'demo_items' AND column_name = 'removed_field'"
|
||||||
|
)
|
||||||
|
self.assertIsNone(cur.fetchone())
|
||||||
|
finally:
|
||||||
|
conn.close()
|
||||||
|
|
||||||
|
def test_seed_batch_without_valid_column_names_does_not_prune(self) -> None:
|
||||||
|
conn = sqlite3.connect(":memory:")
|
||||||
|
try:
|
||||||
|
_init_demo_items_fixture(conn, prune=False)
|
||||||
|
conn.execute(
|
||||||
|
"""
|
||||||
|
INSERT INTO _jiangchang_columns
|
||||||
|
(table_name, column_name, display_name, visible, searchable, editable)
|
||||||
|
VALUES ('demo_items', 'removed_field', '已删除', 0, 0, 0)
|
||||||
|
"""
|
||||||
|
)
|
||||||
|
conn.commit()
|
||||||
|
from db.display_metadata import seed_column_metadata_batch
|
||||||
|
|
||||||
|
seed_column_metadata_batch(
|
||||||
|
conn.cursor(),
|
||||||
|
table_name="demo_items",
|
||||||
|
columns=DEMO_ITEMS_COLUMN_METADATA,
|
||||||
|
)
|
||||||
|
conn.commit()
|
||||||
|
cur = conn.cursor()
|
||||||
|
cur.execute(
|
||||||
|
"SELECT 1 FROM _jiangchang_columns WHERE table_name = 'demo_items' AND column_name = 'removed_field'"
|
||||||
|
)
|
||||||
|
self.assertIsNotNone(cur.fetchone())
|
||||||
|
finally:
|
||||||
|
conn.close()
|
||||||
|
|
||||||
|
def test_seed_batch_accepts_list_columns(self) -> None:
|
||||||
|
conn = sqlite3.connect(":memory:")
|
||||||
|
try:
|
||||||
|
_init_demo_items_fixture(conn, prune=False)
|
||||||
|
from db.display_metadata import get_column_editable, seed_column_metadata_batch
|
||||||
|
|
||||||
|
columns = list(DEMO_ITEMS_COLUMN_METADATA)
|
||||||
|
columns[1] = {**columns[1], "display_name": "标题(列表)"}
|
||||||
|
seed_column_metadata_batch(
|
||||||
|
conn.cursor(),
|
||||||
|
table_name="demo_items",
|
||||||
|
columns=columns,
|
||||||
|
valid_column_names=[str(item["column_name"]) for item in DEMO_ITEMS_COLUMN_METADATA],
|
||||||
|
)
|
||||||
|
conn.commit()
|
||||||
|
cur = conn.cursor()
|
||||||
|
cur.execute(
|
||||||
|
"SELECT display_name FROM _jiangchang_columns WHERE table_name = 'demo_items' AND column_name = 'title'"
|
||||||
|
)
|
||||||
|
self.assertEqual(cur.fetchone()[0], "标题(列表)")
|
||||||
|
self.assertEqual(get_column_editable(cur, "demo_items", "title"), 1)
|
||||||
|
finally:
|
||||||
|
conn.close()
|
||||||
|
|
||||||
|
def test_prune_does_not_remove_existing_column_metadata(self) -> None:
|
||||||
|
conn = sqlite3.connect(":memory:")
|
||||||
|
try:
|
||||||
|
_init_demo_items_fixture(conn)
|
||||||
|
from db.display_metadata import get_column_editable, seed_column_metadata_batch
|
||||||
|
|
||||||
|
cur = conn.cursor()
|
||||||
|
before = get_column_editable(cur, "demo_items", "title")
|
||||||
|
seed_column_metadata_batch(
|
||||||
|
cur,
|
||||||
|
table_name="demo_items",
|
||||||
|
columns=DEMO_ITEMS_COLUMN_METADATA,
|
||||||
|
valid_column_names=[str(item["column_name"]) for item in DEMO_ITEMS_COLUMN_METADATA],
|
||||||
|
)
|
||||||
|
conn.commit()
|
||||||
|
self.assertEqual(get_column_editable(cur, "demo_items", "title"), before)
|
||||||
|
finally:
|
||||||
|
conn.close()
|
||||||
|
|
||||||
|
def test_demo_items_validation_passes(self) -> None:
|
||||||
|
conn = sqlite3.connect(":memory:")
|
||||||
|
try:
|
||||||
|
_init_demo_items_fixture(conn)
|
||||||
|
from db.display_metadata_validator import validate_display_metadata
|
||||||
|
|
||||||
|
report = validate_display_metadata(conn)
|
||||||
|
self.assertTrue(report.ok, msg="; ".join(report.errors))
|
||||||
|
finally:
|
||||||
|
conn.close()
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
unittest.main()
|
||||||
@@ -10,12 +10,20 @@ from _support import IsolatedDataRoot
|
|||||||
class TestDbSmoke(unittest.TestCase):
|
class TestDbSmoke(unittest.TestCase):
|
||||||
def test_init_db_idempotent_and_crud_smoke(self) -> None:
|
def test_init_db_idempotent_and_crud_smoke(self) -> None:
|
||||||
with IsolatedDataRoot():
|
with IsolatedDataRoot():
|
||||||
from db.connection import init_db
|
|
||||||
from db import task_logs_repository as tlr
|
from db import task_logs_repository as tlr
|
||||||
|
from db.connection import get_conn, init_db
|
||||||
|
|
||||||
init_db()
|
init_db()
|
||||||
init_db() # 幂等
|
init_db() # 幂等
|
||||||
|
|
||||||
|
conn = get_conn()
|
||||||
|
try:
|
||||||
|
cur = conn.cursor()
|
||||||
|
cur.execute("SELECT display_name FROM _jiangchang_tables WHERE table_name = 'task_logs'")
|
||||||
|
self.assertEqual(cur.fetchone()[0], "任务日志")
|
||||||
|
finally:
|
||||||
|
conn.close()
|
||||||
|
|
||||||
new_id = tlr.save_task_log(
|
new_id = tlr.save_task_log(
|
||||||
task_type="demo_task",
|
task_type="demo_task",
|
||||||
target_id="tgt-1",
|
target_id="tgt-1",
|
||||||
|
|||||||
900
tests/test_development_policy_guard.py
Normal file
900
tests/test_development_policy_guard.py
Normal file
@@ -0,0 +1,900 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
"""开发规范 hard policy 守护(索引:development/POLICY_MATRIX.md)。"""
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import os
|
||||||
|
import re
|
||||||
|
import unittest
|
||||||
|
|
||||||
|
from _support import get_skill_root
|
||||||
|
|
||||||
|
SKIP_DIR_NAMES = frozenset(
|
||||||
|
{
|
||||||
|
".git",
|
||||||
|
"__pycache__",
|
||||||
|
".pytest_cache",
|
||||||
|
"rpa-artifacts",
|
||||||
|
"videos",
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
SKIP_REL_PREFIXES = ("tests/artifacts", "tests/diagnostics")
|
||||||
|
|
||||||
|
POLICY_IDS = (
|
||||||
|
"POLICY-STRUCTURE-001",
|
||||||
|
"POLICY-RUNTIME-001",
|
||||||
|
"POLICY-RUNTIME-002",
|
||||||
|
"POLICY-INSTALL-001",
|
||||||
|
"POLICY-CONFIG-001",
|
||||||
|
"POLICY-CONFIG-002",
|
||||||
|
"POLICY-CONFIG-003",
|
||||||
|
"POLICY-TESTING-001",
|
||||||
|
"POLICY-TESTING-002",
|
||||||
|
"POLICY-RPA-001",
|
||||||
|
"POLICY-RPA-002",
|
||||||
|
"POLICY-RPA-003",
|
||||||
|
"POLICY-PACKAGING-001",
|
||||||
|
"POLICY-PACKAGING-002",
|
||||||
|
"POLICY-DOCS-001",
|
||||||
|
"POLICY-LOGGING-001",
|
||||||
|
"POLICY-LOGGING-002",
|
||||||
|
"POLICY-LOGGING-003",
|
||||||
|
"POLICY-LOGGING-004",
|
||||||
|
"POLICY-LOGGING-005",
|
||||||
|
"POLICY-CONTROL-001",
|
||||||
|
"POLICY-CONTROL-002",
|
||||||
|
"POLICY-CONTROL-003",
|
||||||
|
"POLICY-DATA-PATH-001",
|
||||||
|
"POLICY-SKILL-ACTION-001",
|
||||||
|
)
|
||||||
|
|
||||||
|
STRUCTURE_PATHS = (
|
||||||
|
"scripts/main.py",
|
||||||
|
"scripts/cli",
|
||||||
|
"scripts/service",
|
||||||
|
"scripts/db",
|
||||||
|
"scripts/util",
|
||||||
|
)
|
||||||
|
|
||||||
|
CONFIG_ENV_BOUNDARY_FILES = frozenset(
|
||||||
|
{
|
||||||
|
"scripts/main.py",
|
||||||
|
"scripts/util/config_bootstrap.py",
|
||||||
|
"scripts/util/runtime_paths.py",
|
||||||
|
"scripts/util/logging_config.py",
|
||||||
|
"scripts/util/constants.py",
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
INSTALL_FORBIDDEN_PATTERNS = (
|
||||||
|
re.compile(r"\bpip\s+install\b", re.IGNORECASE),
|
||||||
|
re.compile(r"\bpython\s+-m\s+pip\s+install\b", re.IGNORECASE),
|
||||||
|
re.compile(r"\buv\s+pip\s+install\b", re.IGNORECASE),
|
||||||
|
re.compile(r"\bplaywright\s+install\b", re.IGNORECASE),
|
||||||
|
)
|
||||||
|
|
||||||
|
CONFIG_ENV_FORBIDDEN_PATTERNS = (
|
||||||
|
re.compile(r"\bos\.environ\b"),
|
||||||
|
re.compile(r"\bos\.getenv\b"),
|
||||||
|
re.compile(r"\bos\.putenv\b"),
|
||||||
|
re.compile(r"\benviron\.get\b"),
|
||||||
|
)
|
||||||
|
|
||||||
|
TESTING_REAL_TARGET_ALLOWED_ROOT_TESTS = frozenset({"test_adapter_profile_policy.py"})
|
||||||
|
|
||||||
|
TESTING_001_FORBIDDEN_SUBSTRINGS = (
|
||||||
|
"real_" + "api",
|
||||||
|
"real_" + "rpa",
|
||||||
|
"OPENCLAW_" + "TEST_TARGET",
|
||||||
|
)
|
||||||
|
|
||||||
|
TESTING_001_SKIP_ROOT_TESTS = frozenset({"test_development_policy_guard.py"})
|
||||||
|
|
||||||
|
RPA_001_FORBIDDEN_PATTERNS = (
|
||||||
|
re.compile(r"\brpa_helpers\b"),
|
||||||
|
re.compile(r"\binject_account_manager_scripts_path\b"),
|
||||||
|
re.compile(r"\bget_account_credential\b"),
|
||||||
|
)
|
||||||
|
|
||||||
|
RPA_002_FFMPEG_PATTERNS = (
|
||||||
|
re.compile(
|
||||||
|
r"\bsubprocess\.(run|call|Popen|check_output|check_call)\s*\([^)]*ffmpeg",
|
||||||
|
re.IGNORECASE | re.DOTALL,
|
||||||
|
),
|
||||||
|
re.compile(
|
||||||
|
r"\bsubprocess\.(run|call|Popen|check_output|check_call)\(\s*['\"][^'\"]*ffmpeg",
|
||||||
|
re.IGNORECASE,
|
||||||
|
),
|
||||||
|
re.compile(r"\bos\.system\s*\([^)]*ffmpeg", re.IGNORECASE | re.DOTALL),
|
||||||
|
re.compile(r"\bos\.popen\s*\([^)]*ffmpeg", re.IGNORECASE | re.DOTALL),
|
||||||
|
re.compile(
|
||||||
|
r"\bsubprocess\.(run|call|Popen|check_output|check_call)\([^)]*shell\s*=\s*True[^)]*ffmpeg",
|
||||||
|
re.IGNORECASE | re.DOTALL,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
RPA_002_EXEMPT_REL = "scripts/service/task_run_support.py"
|
||||||
|
|
||||||
|
RPA_003_SOURCE = (
|
||||||
|
"development/RPA.md §5.3; development/TESTING.md §12; development/POLICY_MATRIX.md"
|
||||||
|
)
|
||||||
|
|
||||||
|
LOGGING_001_SOURCE = "development/LOGGING.md; development/RUNTIME.md"
|
||||||
|
LOGGING_002_SOURCE = "development/LOGGING.md; development/DEVELOPMENT.md"
|
||||||
|
LOGGING_003_SOURCE = "development/LOGGING.md"
|
||||||
|
LOGGING_004_SOURCE = "development/LOGGING.md; development/RPA.md"
|
||||||
|
LOGGING_005_SOURCE = "development/LOGGING.md; development/CONFIG.md"
|
||||||
|
|
||||||
|
LOGGING_CALL_PATTERN = re.compile(
|
||||||
|
r"\b(log|logger|get_skill_logger\s*\(\s*\))\s*\.\s*(info|warning|error|debug|exception|critical)\s*\(",
|
||||||
|
re.IGNORECASE,
|
||||||
|
)
|
||||||
|
LOGGING_CALL_GET_LOGGER_PATTERN = re.compile(
|
||||||
|
r"get_skill_logger\s*\(\s*\)\s*\.\s*(info|warning|error|debug|exception|critical)\s*\(",
|
||||||
|
re.IGNORECASE,
|
||||||
|
)
|
||||||
|
SENSITIVE_LOG_ASSIGNMENT_PATTERN = re.compile(
|
||||||
|
r"(password|token|cookie|secret|api_key|authorization)\s*=",
|
||||||
|
re.IGNORECASE,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def _policy_msg(policy_id: str, source: str, detail: str) -> str:
|
||||||
|
return f"{policy_id} [{source}]: {detail}"
|
||||||
|
|
||||||
|
|
||||||
|
def _rel(skill_root: str, path: str) -> str:
|
||||||
|
return os.path.relpath(path, skill_root).replace("\\", "/")
|
||||||
|
|
||||||
|
|
||||||
|
def _should_skip_dirname(dirname: str) -> bool:
|
||||||
|
return dirname in SKIP_DIR_NAMES
|
||||||
|
|
||||||
|
|
||||||
|
def _should_skip_rel_path(skill_root: str, abs_path: str) -> bool:
|
||||||
|
rel = _rel(skill_root, abs_path).replace("\\", "/")
|
||||||
|
for prefix in SKIP_REL_PREFIXES:
|
||||||
|
normalized = prefix.rstrip("/")
|
||||||
|
if rel == normalized or rel.startswith(normalized + "/"):
|
||||||
|
return True
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
def _walk_files(skill_root: str, rel_root: str, *, suffix: str | None = None) -> list[str]:
|
||||||
|
base = os.path.join(skill_root, rel_root)
|
||||||
|
if not os.path.exists(base):
|
||||||
|
return []
|
||||||
|
found: list[str] = []
|
||||||
|
for root, dirnames, filenames in os.walk(base):
|
||||||
|
kept: list[str] = []
|
||||||
|
for d in dirnames:
|
||||||
|
if _should_skip_dirname(d):
|
||||||
|
continue
|
||||||
|
sub = os.path.join(root, d)
|
||||||
|
if _should_skip_rel_path(skill_root, sub):
|
||||||
|
continue
|
||||||
|
kept.append(d)
|
||||||
|
dirnames[:] = kept
|
||||||
|
for name in filenames:
|
||||||
|
full = os.path.join(root, name)
|
||||||
|
if _should_skip_rel_path(skill_root, full):
|
||||||
|
continue
|
||||||
|
if suffix and not name.endswith(suffix):
|
||||||
|
continue
|
||||||
|
found.append(full)
|
||||||
|
return sorted(found)
|
||||||
|
|
||||||
|
|
||||||
|
def _read_text(path: str) -> str:
|
||||||
|
with open(path, encoding="utf-8") as f:
|
||||||
|
return f.read()
|
||||||
|
|
||||||
|
|
||||||
|
def _requirement_dependency_lines(req_text: str) -> list[str]:
|
||||||
|
return [
|
||||||
|
line.strip()
|
||||||
|
for line in req_text.splitlines()
|
||||||
|
if line.strip() and not line.strip().startswith("#")
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
|
def _scan_install_delivery_files(skill_root: str) -> list[str]:
|
||||||
|
offenders: list[str] = []
|
||||||
|
scan_specs: list[tuple[str, str | None]] = [
|
||||||
|
("scripts", ".py"),
|
||||||
|
(".", ".ps1"),
|
||||||
|
(".", ".sh"),
|
||||||
|
(".", None),
|
||||||
|
(".github/workflows", ".yml"),
|
||||||
|
(".github/workflows", ".yaml"),
|
||||||
|
]
|
||||||
|
seen: set[str] = set()
|
||||||
|
for rel_root, suffix in scan_specs:
|
||||||
|
if rel_root == "." and suffix is None:
|
||||||
|
req = os.path.join(skill_root, "requirements.txt")
|
||||||
|
if os.path.isfile(req):
|
||||||
|
seen.add(req)
|
||||||
|
text = _read_text(req)
|
||||||
|
for pattern in INSTALL_FORBIDDEN_PATTERNS:
|
||||||
|
if pattern.search(text):
|
||||||
|
offenders.append(
|
||||||
|
_policy_msg(
|
||||||
|
"POLICY-INSTALL-001",
|
||||||
|
"development/RUNTIME.md; development/RPA.md §1.4; development/DEVELOPMENT.md §3.1",
|
||||||
|
f"{_rel(skill_root, req)} matches {pattern.pattern}",
|
||||||
|
)
|
||||||
|
)
|
||||||
|
continue
|
||||||
|
for path in _walk_files(skill_root, rel_root, suffix=suffix):
|
||||||
|
if path in seen:
|
||||||
|
continue
|
||||||
|
seen.add(path)
|
||||||
|
if suffix == ".ps1" and not path.endswith(".ps1"):
|
||||||
|
continue
|
||||||
|
if suffix == ".sh" and not path.endswith(".sh"):
|
||||||
|
continue
|
||||||
|
text = _read_text(path)
|
||||||
|
for pattern in INSTALL_FORBIDDEN_PATTERNS:
|
||||||
|
if pattern.search(text):
|
||||||
|
offenders.append(
|
||||||
|
_policy_msg(
|
||||||
|
"POLICY-INSTALL-001",
|
||||||
|
"development/RUNTIME.md; development/RPA.md §1.4; development/DEVELOPMENT.md §3.1",
|
||||||
|
f"{_rel(skill_root, path)} matches {pattern.pattern}",
|
||||||
|
)
|
||||||
|
)
|
||||||
|
return offenders
|
||||||
|
|
||||||
|
|
||||||
|
def _gitignore_has_pattern(lines: list[str], pattern: str) -> bool:
|
||||||
|
normalized = [ln.strip() for ln in lines if ln.strip() and not ln.strip().startswith("#")]
|
||||||
|
return pattern in normalized
|
||||||
|
|
||||||
|
|
||||||
|
class TestPolicyStructure001(unittest.TestCase):
|
||||||
|
def test_standard_scripts_layout_exists(self) -> None:
|
||||||
|
skill_root = get_skill_root()
|
||||||
|
missing: list[str] = []
|
||||||
|
for rel in STRUCTURE_PATHS:
|
||||||
|
path = os.path.join(skill_root, *rel.split("/"))
|
||||||
|
if not os.path.exists(path):
|
||||||
|
missing.append(rel)
|
||||||
|
self.assertEqual(
|
||||||
|
missing,
|
||||||
|
[],
|
||||||
|
msg=_policy_msg(
|
||||||
|
"POLICY-STRUCTURE-001",
|
||||||
|
"development/DEVELOPMENT.md §2; development/RUNTIME.md §目录结构",
|
||||||
|
"missing: " + ", ".join(missing),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class TestPolicyRuntime001(unittest.TestCase):
|
||||||
|
def test_no_vendored_jiangchang_skill_core(self) -> None:
|
||||||
|
skill_root = get_skill_root()
|
||||||
|
vendored = os.path.join(skill_root, "scripts", "jiangchang_skill_core")
|
||||||
|
self.assertFalse(
|
||||||
|
os.path.isdir(vendored),
|
||||||
|
msg=_policy_msg(
|
||||||
|
"POLICY-RUNTIME-001",
|
||||||
|
"development/RUNTIME.md §共享 Python Runtime",
|
||||||
|
_rel(skill_root, vendored),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class TestPolicyRuntime002(unittest.TestCase):
|
||||||
|
def test_requirements_txt_excludes_shared_runtime_deps(self) -> None:
|
||||||
|
skill_root = get_skill_root()
|
||||||
|
req_path = os.path.join(skill_root, "requirements.txt")
|
||||||
|
self.assertTrue(os.path.isfile(req_path), msg="requirements.txt missing")
|
||||||
|
text = _read_text(req_path)
|
||||||
|
dep_lines = _requirement_dependency_lines(text)
|
||||||
|
offenders: list[str] = []
|
||||||
|
for line in dep_lines:
|
||||||
|
lower = line.lower()
|
||||||
|
if "jiangchang-platform-kit" in lower:
|
||||||
|
offenders.append(line)
|
||||||
|
if re.search(r"(?<![\w.-])playwright(?![\w.-])", lower):
|
||||||
|
offenders.append(line)
|
||||||
|
self.assertEqual(
|
||||||
|
offenders,
|
||||||
|
[],
|
||||||
|
msg=_policy_msg(
|
||||||
|
"POLICY-RUNTIME-002",
|
||||||
|
"development/RUNTIME.md; development/DEVELOPMENT.md §3.1",
|
||||||
|
"forbidden dependency lines: " + "; ".join(offenders),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class TestPolicyInstall001(unittest.TestCase):
|
||||||
|
def test_delivery_files_have_no_auto_install_commands(self) -> None:
|
||||||
|
offenders = _scan_install_delivery_files(get_skill_root())
|
||||||
|
self.assertEqual(offenders, [], msg="\n".join(offenders))
|
||||||
|
|
||||||
|
|
||||||
|
class TestPolicyConfig001(unittest.TestCase):
|
||||||
|
def test_env_example_exists(self) -> None:
|
||||||
|
skill_root = get_skill_root()
|
||||||
|
path = os.path.join(skill_root, ".env.example")
|
||||||
|
self.assertTrue(
|
||||||
|
os.path.isfile(path),
|
||||||
|
msg=_policy_msg(
|
||||||
|
"POLICY-CONFIG-001",
|
||||||
|
"development/CONFIG.md",
|
||||||
|
".env.example missing at repo root",
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class TestPolicyConfig002(unittest.TestCase):
|
||||||
|
def test_gitignore_ignores_env_files(self) -> None:
|
||||||
|
skill_root = get_skill_root()
|
||||||
|
path = os.path.join(skill_root, ".gitignore")
|
||||||
|
self.assertTrue(os.path.isfile(path), msg=".gitignore missing")
|
||||||
|
lines = _read_text(path).splitlines()
|
||||||
|
missing: list[str] = []
|
||||||
|
if not _gitignore_has_pattern(lines, ".env"):
|
||||||
|
missing.append(".env")
|
||||||
|
if not _gitignore_has_pattern(lines, "*.env.local"):
|
||||||
|
missing.append("*.env.local")
|
||||||
|
self.assertEqual(
|
||||||
|
missing,
|
||||||
|
[],
|
||||||
|
msg=_policy_msg(
|
||||||
|
"POLICY-CONFIG-002",
|
||||||
|
"development/CONFIG.md §红线:敏感信息不进 .env",
|
||||||
|
"missing gitignore entries: " + ", ".join(missing),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class TestPolicyConfig003(unittest.TestCase):
|
||||||
|
def test_business_scripts_do_not_read_os_environ_directly(self) -> None:
|
||||||
|
skill_root = get_skill_root()
|
||||||
|
offenders: list[str] = []
|
||||||
|
for path in _walk_files(skill_root, "scripts", suffix=".py"):
|
||||||
|
rel = _rel(skill_root, path)
|
||||||
|
if rel.replace("\\", "/") in CONFIG_ENV_BOUNDARY_FILES:
|
||||||
|
continue
|
||||||
|
text = _read_text(path)
|
||||||
|
for lineno, line in enumerate(text.splitlines(), 1):
|
||||||
|
if line.strip().startswith("#"):
|
||||||
|
continue
|
||||||
|
for pattern in CONFIG_ENV_FORBIDDEN_PATTERNS:
|
||||||
|
if pattern.search(line):
|
||||||
|
offenders.append(
|
||||||
|
f"{rel}:{lineno}: {line.strip()} — use jiangchang_skill_core.config.get*"
|
||||||
|
)
|
||||||
|
self.assertEqual(
|
||||||
|
offenders,
|
||||||
|
[],
|
||||||
|
msg=_policy_msg(
|
||||||
|
"POLICY-CONFIG-003",
|
||||||
|
"development/CONFIG.md; development/ADAPTER.md",
|
||||||
|
"\n".join(offenders),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class TestPolicyTesting001(unittest.TestCase):
|
||||||
|
def test_root_tests_do_not_hardcode_real_targets(self) -> None:
|
||||||
|
skill_root = get_skill_root()
|
||||||
|
tests_dir = os.path.join(skill_root, "tests")
|
||||||
|
offenders: list[str] = []
|
||||||
|
for name in sorted(os.listdir(tests_dir)):
|
||||||
|
if not (name.startswith("test_") and name.endswith(".py")):
|
||||||
|
continue
|
||||||
|
if name in TESTING_001_SKIP_ROOT_TESTS:
|
||||||
|
continue
|
||||||
|
if name in TESTING_REAL_TARGET_ALLOWED_ROOT_TESTS:
|
||||||
|
continue
|
||||||
|
path = os.path.join(tests_dir, name)
|
||||||
|
text = _read_text(path)
|
||||||
|
for lineno, line in enumerate(text.splitlines(), 1):
|
||||||
|
stripped = line.strip()
|
||||||
|
if stripped.startswith("#"):
|
||||||
|
continue
|
||||||
|
for token in TESTING_001_FORBIDDEN_SUBSTRINGS:
|
||||||
|
if token in line:
|
||||||
|
offenders.append(f"{name}:{lineno}: {stripped}")
|
||||||
|
break
|
||||||
|
self.assertEqual(
|
||||||
|
offenders,
|
||||||
|
[],
|
||||||
|
msg=_policy_msg(
|
||||||
|
"POLICY-TESTING-001",
|
||||||
|
"development/TESTING.md §默认必跑测试要做什么",
|
||||||
|
"\n".join(offenders),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class TestPolicyTesting002(unittest.TestCase):
|
||||||
|
def test_integration_python_tests_are_sample_only(self) -> None:
|
||||||
|
skill_root = get_skill_root()
|
||||||
|
integration_dir = os.path.join(skill_root, "tests", "integration")
|
||||||
|
if not os.path.isdir(integration_dir):
|
||||||
|
return
|
||||||
|
offenders: list[str] = []
|
||||||
|
for name in sorted(os.listdir(integration_dir)):
|
||||||
|
if not name.endswith(".py"):
|
||||||
|
continue
|
||||||
|
if name.endswith(".sample"):
|
||||||
|
continue
|
||||||
|
offenders.append(f"tests/integration/{name}")
|
||||||
|
self.assertEqual(
|
||||||
|
offenders,
|
||||||
|
[],
|
||||||
|
msg=_policy_msg(
|
||||||
|
"POLICY-TESTING-002",
|
||||||
|
"development/TESTING.md §7 真实联调测试的安全约束",
|
||||||
|
"non-sample integration tests: " + ", ".join(offenders),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class TestPolicyRpa001(unittest.TestCase):
|
||||||
|
def test_scripts_have_no_account_manager_internal_imports(self) -> None:
|
||||||
|
skill_root = get_skill_root()
|
||||||
|
offenders: list[str] = []
|
||||||
|
for path in _walk_files(skill_root, "scripts", suffix=".py"):
|
||||||
|
rel = _rel(skill_root, path)
|
||||||
|
text = _read_text(path)
|
||||||
|
for pattern in RPA_001_FORBIDDEN_PATTERNS:
|
||||||
|
if pattern.search(text):
|
||||||
|
offenders.append(f"{rel}: {pattern.pattern}")
|
||||||
|
self.assertEqual(
|
||||||
|
offenders,
|
||||||
|
[],
|
||||||
|
msg=_policy_msg(
|
||||||
|
"POLICY-RPA-001",
|
||||||
|
"development/ADAPTER.md §兄弟依赖; development/RPA.md §1.7",
|
||||||
|
"\n".join(offenders),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class TestPolicyRpa002(unittest.TestCase):
|
||||||
|
def test_scripts_do_not_invoke_ffmpeg_directly(self) -> None:
|
||||||
|
skill_root = get_skill_root()
|
||||||
|
offenders: list[str] = []
|
||||||
|
for path in _walk_files(skill_root, "scripts", suffix=".py"):
|
||||||
|
rel = _rel(skill_root, path)
|
||||||
|
if rel.replace("\\", "/") == RPA_002_EXEMPT_REL:
|
||||||
|
continue
|
||||||
|
text = _read_text(path)
|
||||||
|
for pattern in RPA_002_FFMPEG_PATTERNS:
|
||||||
|
if pattern.search(text):
|
||||||
|
offenders.append(f"{rel}: {pattern.pattern}")
|
||||||
|
self.assertEqual(
|
||||||
|
offenders,
|
||||||
|
[],
|
||||||
|
msg=_policy_msg(
|
||||||
|
"POLICY-RPA-002",
|
||||||
|
"development/RPA.md §5.3 录屏成片标准",
|
||||||
|
"\n".join(offenders),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class TestPolicyRpa003(unittest.TestCase):
|
||||||
|
def test_service_layer_retains_video_session_integration(self) -> None:
|
||||||
|
skill_root = get_skill_root()
|
||||||
|
service_files = _walk_files(skill_root, "scripts/service", suffix=".py")
|
||||||
|
self.assertTrue(service_files, msg="scripts/service/*.py not found")
|
||||||
|
combined = "\n".join(_read_text(path) for path in service_files)
|
||||||
|
missing: list[str] = []
|
||||||
|
if "RpaVideoSession" not in combined:
|
||||||
|
missing.append("RpaVideoSession")
|
||||||
|
if "video.add_step" not in combined and ".add_step(" not in combined:
|
||||||
|
missing.append("video.add_step or .add_step(")
|
||||||
|
if "merge_video_into_result_summary" not in combined:
|
||||||
|
missing.append("merge_video_into_result_summary")
|
||||||
|
self.assertEqual(
|
||||||
|
missing,
|
||||||
|
[],
|
||||||
|
msg=_policy_msg(
|
||||||
|
"POLICY-RPA-003",
|
||||||
|
RPA_003_SOURCE,
|
||||||
|
"scripts/service/*.py missing: " + ", ".join(missing),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def _scan_sensitive_logging_assignments(skill_root: str) -> list[str]:
|
||||||
|
offenders: list[str] = []
|
||||||
|
for path in _walk_files(skill_root, "scripts", suffix=".py"):
|
||||||
|
rel = _rel(skill_root, path)
|
||||||
|
for lineno, line in enumerate(_read_text(path).splitlines(), 1):
|
||||||
|
stripped = line.strip()
|
||||||
|
if not stripped or stripped.startswith("#"):
|
||||||
|
continue
|
||||||
|
if not LOGGING_CALL_PATTERN.search(line):
|
||||||
|
continue
|
||||||
|
if SENSITIVE_LOG_ASSIGNMENT_PATTERN.search(line):
|
||||||
|
offenders.append(f"{rel}:{lineno}: {stripped}")
|
||||||
|
return offenders
|
||||||
|
|
||||||
|
|
||||||
|
class TestPolicyLogging001(unittest.TestCase):
|
||||||
|
def test_cli_app_sets_up_unified_logging(self) -> None:
|
||||||
|
skill_root = get_skill_root()
|
||||||
|
path = os.path.join(skill_root, "scripts", "cli", "app.py")
|
||||||
|
text = _read_text(path)
|
||||||
|
rel = _rel(skill_root, path)
|
||||||
|
self.assertIn(
|
||||||
|
"setup_skill_logging",
|
||||||
|
text,
|
||||||
|
msg=_policy_msg(
|
||||||
|
"POLICY-LOGGING-001",
|
||||||
|
LOGGING_001_SOURCE,
|
||||||
|
f"{rel} missing setup_skill_logging",
|
||||||
|
),
|
||||||
|
)
|
||||||
|
has_logger_info = bool(
|
||||||
|
LOGGING_CALL_GET_LOGGER_PATTERN.search(text)
|
||||||
|
or re.search(r"\bget_skill_logger\s*\(\s*\)\s*\.\s*info\s*\(", text)
|
||||||
|
)
|
||||||
|
self.assertTrue(
|
||||||
|
has_logger_info,
|
||||||
|
msg=_policy_msg(
|
||||||
|
"POLICY-LOGGING-001",
|
||||||
|
LOGGING_001_SOURCE,
|
||||||
|
f"{rel} missing get_skill_logger().info (or equivalent)",
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class TestPolicyLogging002(unittest.TestCase):
|
||||||
|
def test_task_service_uses_get_skill_logger(self) -> None:
|
||||||
|
skill_root = get_skill_root()
|
||||||
|
path = os.path.join(skill_root, "scripts", "service", "task_service.py")
|
||||||
|
text = _read_text(path)
|
||||||
|
rel = _rel(skill_root, path)
|
||||||
|
self.assertRegex(
|
||||||
|
text,
|
||||||
|
r"get_skill_logger",
|
||||||
|
msg=_policy_msg(
|
||||||
|
"POLICY-LOGGING-002",
|
||||||
|
LOGGING_002_SOURCE,
|
||||||
|
f"{rel} must import/use get_skill_logger",
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class TestPolicyLogging003(unittest.TestCase):
|
||||||
|
def test_task_service_logs_start_failure_and_task_logs(self) -> None:
|
||||||
|
skill_root = get_skill_root()
|
||||||
|
path = os.path.join(skill_root, "scripts", "service", "task_service.py")
|
||||||
|
text = _read_text(path)
|
||||||
|
rel = _rel(skill_root, path)
|
||||||
|
missing: list[str] = []
|
||||||
|
if not re.search(r"\b(log|logger)\.info\s*\(", text):
|
||||||
|
missing.append("log.info or logger.info")
|
||||||
|
if not re.search(r"\b(log|logger)\.exception\s*\(", text):
|
||||||
|
missing.append("log.exception or logger.exception")
|
||||||
|
if "save_task_log" not in text:
|
||||||
|
missing.append("save_task_log")
|
||||||
|
self.assertEqual(
|
||||||
|
missing,
|
||||||
|
[],
|
||||||
|
msg=_policy_msg(
|
||||||
|
"POLICY-LOGGING-003",
|
||||||
|
LOGGING_003_SOURCE,
|
||||||
|
f"{rel} missing: " + ", ".join(missing),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class TestPolicyLogging004(unittest.TestCase):
|
||||||
|
def test_task_service_emits_progress_for_long_tasks(self) -> None:
|
||||||
|
skill_root = get_skill_root()
|
||||||
|
path = os.path.join(skill_root, "scripts", "service", "task_service.py")
|
||||||
|
text = _read_text(path)
|
||||||
|
rel = _rel(skill_root, path)
|
||||||
|
has_progress = bool(
|
||||||
|
re.search(r"\bemit\s*\(", text)
|
||||||
|
or re.search(r"\bactivity\.emit\s*\(", text)
|
||||||
|
or re.search(r"\bvideo\.add_step\s*\(", text)
|
||||||
|
)
|
||||||
|
self.assertTrue(
|
||||||
|
has_progress,
|
||||||
|
msg=_policy_msg(
|
||||||
|
"POLICY-LOGGING-004",
|
||||||
|
LOGGING_004_SOURCE,
|
||||||
|
f"{rel} must use emit( / activity.emit / video.add_step",
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class TestPolicyLogging005(unittest.TestCase):
|
||||||
|
def test_scripts_do_not_log_sensitive_assignments(self) -> None:
|
||||||
|
offenders = _scan_sensitive_logging_assignments(get_skill_root())
|
||||||
|
self.assertEqual(
|
||||||
|
offenders,
|
||||||
|
[],
|
||||||
|
msg=_policy_msg(
|
||||||
|
"POLICY-LOGGING-005",
|
||||||
|
LOGGING_005_SOURCE,
|
||||||
|
"\n".join(offenders),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class TestPolicyControl001(unittest.TestCase):
|
||||||
|
def test_no_util_progress_module(self) -> None:
|
||||||
|
skill_root = get_skill_root()
|
||||||
|
path = os.path.join(skill_root, "scripts", "util", "progress.py")
|
||||||
|
self.assertFalse(
|
||||||
|
os.path.isfile(path),
|
||||||
|
msg=_policy_msg(
|
||||||
|
"POLICY-CONTROL-001",
|
||||||
|
"development/LOGGING.md §2.5; development/RPA.md §0.1",
|
||||||
|
"scripts/util/progress.py must not exist",
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class TestPolicyControl002(unittest.TestCase):
|
||||||
|
def test_task_service_uses_job_context_and_finish(self) -> None:
|
||||||
|
skill_root = get_skill_root()
|
||||||
|
path = os.path.join(skill_root, "scripts", "service", "task_service.py")
|
||||||
|
text = _read_text(path)
|
||||||
|
rel = _rel(skill_root, path)
|
||||||
|
missing: list[str] = []
|
||||||
|
if "job_context(" not in text:
|
||||||
|
missing.append("job_context(")
|
||||||
|
if "finish(" not in text:
|
||||||
|
missing.append("finish(")
|
||||||
|
self.assertEqual(
|
||||||
|
missing,
|
||||||
|
[],
|
||||||
|
msg=_policy_msg(
|
||||||
|
"POLICY-CONTROL-002",
|
||||||
|
"development/LOGGING.md §2.5、§7",
|
||||||
|
f"{rel} missing: " + ", ".join(missing),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class TestPolicyControl003(unittest.TestCase):
|
||||||
|
def test_service_layer_has_no_bare_asyncio_sleep(self) -> None:
|
||||||
|
skill_root = get_skill_root()
|
||||||
|
offenders: list[str] = []
|
||||||
|
for path in _walk_files(skill_root, "scripts/service", suffix=".py"):
|
||||||
|
rel = _rel(skill_root, path)
|
||||||
|
for lineno, line in enumerate(_read_text(path).splitlines(), 1):
|
||||||
|
stripped = line.strip()
|
||||||
|
if not stripped or stripped.startswith("#"):
|
||||||
|
continue
|
||||||
|
if "asyncio.sleep" in stripped:
|
||||||
|
offenders.append(f"{rel}:{lineno}: {stripped}")
|
||||||
|
self.assertEqual(
|
||||||
|
offenders,
|
||||||
|
[],
|
||||||
|
msg=_policy_msg(
|
||||||
|
"POLICY-CONTROL-003",
|
||||||
|
"development/RPA.md §0.1",
|
||||||
|
"use interruptible_sleep instead:\n" + "\n".join(offenders),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class TestPolicyDataPath001(unittest.TestCase):
|
||||||
|
def test_env_example_has_no_cwd_relative_paths(self) -> None:
|
||||||
|
path = os.path.join(get_skill_root(), ".env.example")
|
||||||
|
offenders: list[str] = []
|
||||||
|
with open(path, encoding="utf-8") as f:
|
||||||
|
for lineno, line in enumerate(f, 1):
|
||||||
|
stripped = line.strip()
|
||||||
|
if not stripped or stripped.startswith("#"):
|
||||||
|
continue
|
||||||
|
active = stripped.split("#", 1)[0].strip()
|
||||||
|
if re.search(r"=\s*\./", active):
|
||||||
|
offenders.append(f".env.example:{lineno}: {active}")
|
||||||
|
self.assertEqual(
|
||||||
|
offenders,
|
||||||
|
[],
|
||||||
|
msg=_policy_msg(
|
||||||
|
"POLICY-DATA-PATH-001",
|
||||||
|
"development/DATA_PATHS.md",
|
||||||
|
"\n".join(offenders) or "CWD-relative ./ paths in .env.example",
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
def test_runtime_paths_exposes_resolve_data_path(self) -> None:
|
||||||
|
import util.runtime_paths as rp
|
||||||
|
|
||||||
|
self.assertTrue(callable(getattr(rp, "resolve_data_path", None)))
|
||||||
|
self.assertTrue(callable(getattr(rp, "list_resolved_data_paths", None)))
|
||||||
|
|
||||||
|
def test_business_scripts_do_not_abspath_config_get(self) -> None:
|
||||||
|
skill_root = get_skill_root()
|
||||||
|
pattern = re.compile(r"os\.path\.abspath\s*\(\s*config\.get\b")
|
||||||
|
offenders: list[str] = []
|
||||||
|
for rel in _walk_files(skill_root, "scripts", suffix=".py"):
|
||||||
|
if rel.replace("\\", "/") == "scripts/util/runtime_paths.py":
|
||||||
|
continue
|
||||||
|
text = _read_text(os.path.join(skill_root, rel))
|
||||||
|
for lineno, line in enumerate(text.splitlines(), 1):
|
||||||
|
if line.strip().startswith("#"):
|
||||||
|
continue
|
||||||
|
if pattern.search(line):
|
||||||
|
offenders.append(f"{rel}:{lineno}: {line.strip()}")
|
||||||
|
self.assertEqual(
|
||||||
|
offenders,
|
||||||
|
[],
|
||||||
|
msg=_policy_msg(
|
||||||
|
"POLICY-DATA-PATH-001",
|
||||||
|
"development/DATA_PATHS.md",
|
||||||
|
"\n".join(offenders),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
_TEMPLATE_PLACEHOLDER_SLUG = "your-skill-slug"
|
||||||
|
_RPA_SESSION_TOKENS = ("RpaVideoSession",)
|
||||||
|
|
||||||
|
|
||||||
|
def _service_has_rpa_video_session(skill_root: str) -> bool:
|
||||||
|
service_dir = os.path.join(skill_root, "scripts", "service")
|
||||||
|
if not os.path.isdir(service_dir):
|
||||||
|
return False
|
||||||
|
for rel in _walk_files(skill_root, "scripts/service", suffix=".py"):
|
||||||
|
text = _read_text(os.path.join(skill_root, rel))
|
||||||
|
if any(token in text for token in _RPA_SESSION_TOKENS):
|
||||||
|
return True
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
def _load_actions_json(skill_root: str) -> dict | None:
|
||||||
|
path = os.path.join(skill_root, "assets", "actions.json")
|
||||||
|
if not os.path.isfile(path):
|
||||||
|
return None
|
||||||
|
import json
|
||||||
|
|
||||||
|
with open(path, encoding="utf-8") as f:
|
||||||
|
return json.load(f)
|
||||||
|
|
||||||
|
|
||||||
|
class TestPolicySkillAction001(unittest.TestCase):
|
||||||
|
def test_skill_action_runtime_doc_exists(self) -> None:
|
||||||
|
path = os.path.join(get_skill_root(), "development", "SKILL_ACTION_RUNTIME.md")
|
||||||
|
self.assertTrue(
|
||||||
|
os.path.isfile(path),
|
||||||
|
msg=_policy_msg(
|
||||||
|
"POLICY-SKILL-ACTION-001",
|
||||||
|
"development/SKILL_ACTION_RUNTIME.md",
|
||||||
|
"file missing",
|
||||||
|
),
|
||||||
|
)
|
||||||
|
text = _read_text(path)
|
||||||
|
for marker in ("executionProfile", "async", "任务中心", "run_skill_action"):
|
||||||
|
self.assertIn(
|
||||||
|
marker,
|
||||||
|
text,
|
||||||
|
msg=_policy_msg(
|
||||||
|
"POLICY-SKILL-ACTION-001",
|
||||||
|
"development/SKILL_ACTION_RUNTIME.md",
|
||||||
|
f"missing marker {marker!r}",
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
def test_schema_defines_execution_profile(self) -> None:
|
||||||
|
import json
|
||||||
|
|
||||||
|
path = os.path.join(get_skill_root(), "assets", "schemas", "skill-actions.schema.json")
|
||||||
|
self.assertTrue(os.path.isfile(path))
|
||||||
|
with open(path, encoding="utf-8") as f:
|
||||||
|
schema = json.load(f)
|
||||||
|
action_props = schema.get("$defs", {}).get("action", {}).get("properties", {})
|
||||||
|
profile = action_props.get("executionProfile")
|
||||||
|
self.assertIsInstance(
|
||||||
|
profile,
|
||||||
|
dict,
|
||||||
|
msg=_policy_msg(
|
||||||
|
"POLICY-SKILL-ACTION-001",
|
||||||
|
"assets/schemas/skill-actions.schema.json",
|
||||||
|
"action.properties.executionProfile missing",
|
||||||
|
),
|
||||||
|
)
|
||||||
|
self.assertEqual(set(profile.get("enum") or []), {"sync", "async"})
|
||||||
|
|
||||||
|
def test_actions_md_documents_execution_profile(self) -> None:
|
||||||
|
path = os.path.join(get_skill_root(), "references", "ACTIONS.md")
|
||||||
|
text = _read_text(path)
|
||||||
|
for marker in ("executionProfile", "async", "任务中心", "SKILL_ACTION_RUNTIME"):
|
||||||
|
self.assertIn(
|
||||||
|
marker,
|
||||||
|
text,
|
||||||
|
msg=_policy_msg(
|
||||||
|
"POLICY-SKILL-ACTION-001",
|
||||||
|
"references/ACTIONS.md",
|
||||||
|
f"missing marker {marker!r}",
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
def test_actions_json_actions_declare_execution_profile(self) -> None:
|
||||||
|
skill_root = get_skill_root()
|
||||||
|
manifest = _load_actions_json(skill_root)
|
||||||
|
if manifest is None:
|
||||||
|
return
|
||||||
|
offenders: list[str] = []
|
||||||
|
for action in manifest.get("actions") or []:
|
||||||
|
action_id = action.get("id", "?")
|
||||||
|
profile = action.get("executionProfile")
|
||||||
|
if profile not in ("sync", "async"):
|
||||||
|
offenders.append(f"{action_id}: executionProfile={profile!r}")
|
||||||
|
self.assertEqual(
|
||||||
|
offenders,
|
||||||
|
[],
|
||||||
|
msg=_policy_msg(
|
||||||
|
"POLICY-SKILL-ACTION-001",
|
||||||
|
"assets/actions.json",
|
||||||
|
"every action must set executionProfile to sync|async:\n" + "\n".join(offenders),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
def test_business_rpa_skill_has_async_agent_action(self) -> None:
|
||||||
|
from util.constants import SKILL_SLUG
|
||||||
|
|
||||||
|
skill_root = get_skill_root()
|
||||||
|
if SKILL_SLUG == _TEMPLATE_PLACEHOLDER_SLUG:
|
||||||
|
self.skipTest("template placeholder slug skips business RPA action check")
|
||||||
|
if not _service_has_rpa_video_session(skill_root):
|
||||||
|
self.skipTest("no RpaVideoSession in scripts/service")
|
||||||
|
manifest = _load_actions_json(skill_root)
|
||||||
|
self.assertIsNotNone(
|
||||||
|
manifest,
|
||||||
|
msg=_policy_msg(
|
||||||
|
"POLICY-SKILL-ACTION-001",
|
||||||
|
"assets/actions.json",
|
||||||
|
"RPA skill must provide assets/actions.json",
|
||||||
|
),
|
||||||
|
)
|
||||||
|
found = False
|
||||||
|
for action in manifest.get("actions") or []:
|
||||||
|
placements = set(action.get("placements") or [])
|
||||||
|
if action.get("executionProfile") == "async" and "agent" in placements:
|
||||||
|
found = True
|
||||||
|
break
|
||||||
|
self.assertTrue(
|
||||||
|
found,
|
||||||
|
msg=_policy_msg(
|
||||||
|
"POLICY-SKILL-ACTION-001",
|
||||||
|
"assets/actions.json",
|
||||||
|
"RPA business skill needs at least one action with executionProfile=async and placements containing agent",
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class TestPolicyDocs001(unittest.TestCase):
|
||||||
|
def test_policy_matrix_exists_and_lists_policy_ids(self) -> None:
|
||||||
|
skill_root = get_skill_root()
|
||||||
|
matrix_path = os.path.join(skill_root, "development", "POLICY_MATRIX.md")
|
||||||
|
self.assertTrue(
|
||||||
|
os.path.isfile(matrix_path),
|
||||||
|
msg=_policy_msg(
|
||||||
|
"POLICY-DOCS-001",
|
||||||
|
"development/POLICY_MATRIX.md",
|
||||||
|
"file missing",
|
||||||
|
),
|
||||||
|
)
|
||||||
|
text = _read_text(matrix_path)
|
||||||
|
missing = [pid for pid in POLICY_IDS if pid not in text]
|
||||||
|
self.assertEqual(
|
||||||
|
missing,
|
||||||
|
[],
|
||||||
|
msg=_policy_msg(
|
||||||
|
"POLICY-DOCS-001",
|
||||||
|
"development/POLICY_MATRIX.md",
|
||||||
|
"missing policy_id entries: " + ", ".join(missing),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
unittest.main()
|
||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user