Compare commits
29 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| b267ca3266 | |||
| 87ef00835b | |||
| c7d7832749 | |||
| 955816c8a7 | |||
| 7827cc75b2 | |||
| a24f6809da | |||
| 5b555e4313 | |||
| 4c5bc31b0e | |||
| 45cf1741e1 | |||
| 1bbcc65cf3 | |||
| ae5466854f | |||
| b7163f12a5 | |||
| f52536bdcc | |||
| 38343d22ca | |||
| 685ad20bd2 | |||
| e4418d68e4 | |||
| c20d6498da | |||
| a656534461 | |||
| 579db91ab2 | |||
| fd247f6ec3 | |||
| d347e36fea | |||
| 5c18d308bb | |||
| f0032b2201 | |||
| f3f59278b4 | |||
| 4730cd527d | |||
| dd6236866e | |||
| 07fa0b0038 | |||
| b1e93a323f | |||
| f214598470 |
21
.env.example
Normal file
21
.env.example
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
# ── 运行模式 / adapter 档位(见 references/ADAPTER.md)──
|
||||||
|
OPENCLAW_TEST_TARGET=mock # mock | simulator_rpa | real_api | real_rpa
|
||||||
|
|
||||||
|
# ── 目标系统 ──
|
||||||
|
TARGET_BASE_URL=https://sandbox.jc2009.com # 仿真/生产地址,可被进程环境变量覆盖
|
||||||
|
|
||||||
|
# ── 默认账号(仅非敏感标识;密码走 account-manager)──
|
||||||
|
DEFAULT_LOGIN_ID=04110001
|
||||||
|
|
||||||
|
# ── 浏览器 / RPA ──
|
||||||
|
OPENCLAW_BROWSER_HEADLESS=0 # 0=有头(默认) 1=无头(CI)
|
||||||
|
OPENCLAW_PLAYWRIGHT_STEALTH=1 # 1=开启反检测指纹(默认)
|
||||||
|
|
||||||
|
# ── 存证 ──
|
||||||
|
OPENCLAW_RECORD_VIDEO=1
|
||||||
|
OPENCLAW_ARTIFACTS_ON_FAILURE=1 # 1=失败截图(默认)
|
||||||
|
|
||||||
|
# ── 节流 / 超时 ──
|
||||||
|
STEP_DELAY_MIN=1.0 # 步骤间随机等待下限(秒)
|
||||||
|
STEP_DELAY_MAX=5.0 # 步骤间随机等待上限(秒)
|
||||||
|
HUMAN_WAIT_TIMEOUT=180 # 滑块/验证码/2FA 等人工超时(秒)
|
||||||
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: admin/jiangchang-platform-kit/.github/workflows/reusable-release-skill.yaml@v1.0.14
|
||||||
secrets:
|
secrets:
|
||||||
PYARMOR_REG_B64: ${{ secrets.PYARMOR_REG_B64 }}
|
PYARMOR_REG_B64: ${{ secrets.PYARMOR_REG_B64 }}
|
||||||
with:
|
with:
|
||||||
|
|||||||
7
.gitignore
vendored
7
.gitignore
vendored
@@ -1,3 +1,10 @@
|
|||||||
__pycache__/
|
__pycache__/
|
||||||
*.py[cod]
|
*.py[cod]
|
||||||
.env
|
.env
|
||||||
|
*.env.local
|
||||||
|
.pytest_cache/
|
||||||
|
.coverage
|
||||||
|
htmlcov/
|
||||||
|
dist/
|
||||||
|
build/
|
||||||
|
*.egg-info/
|
||||||
116
README.md
116
README.md
@@ -1,48 +1,94 @@
|
|||||||
# 匠厂 技能开发模板
|
---
|
||||||
|
description: "用用户能理解的方式说明这个技能能做什么、适合什么场景,以及如何开始使用。"
|
||||||
|
---
|
||||||
|
|
||||||
这是一个**规范化的新技能模板仓库**,用于复制出新的 skill 项目;它本身**不是业务 skill**。
|
# 【技能名称】
|
||||||
|
|
||||||
## 模板目标
|
> 用一两句话说明这个技能帮助用户完成什么工作。
|
||||||
|
> 这里不要解释技术实现,要从用户的业务目标出发描述价值。
|
||||||
|
|
||||||
- 对齐当前规范 skill 的目录结构:`assets/`、`references/`、`scripts/`、`tests/`、`evals/`
|
---
|
||||||
- 对齐当前规范脚手架分层:`scripts/cli`、`scripts/db`、`scripts/service`、`scripts/util`、`scripts/jiangchang_skill_core`
|
|
||||||
- 提供最小可运行入口:`python scripts/main.py health` / `version`
|
|
||||||
- 让新技能从一开始就按规范落地,不再沿用旧模板的 `docs/`、`optional/`、`skill_main.py` 结构
|
|
||||||
|
|
||||||
## 新技能使用步骤
|
## 这个技能能帮你做什么
|
||||||
|
|
||||||
1. 复制本目录为新的 skill 仓库。
|
用 3 到 6 条业务能力说明技能价值,例如:
|
||||||
2. 全局替换 `your-skill-slug`、`your-platform-key`、`您的技能显示名称`、`你的平台名` 等占位内容。
|
|
||||||
3. 修改 `SKILL.md`、`references/` 和 `scripts/util/constants.py`。
|
|
||||||
4. 在 `scripts/service/` 中补业务 service 与真正的发布/执行逻辑。
|
|
||||||
5. 用 `python scripts/main.py health` 和 `python scripts/main.py version` 做最小验证。
|
|
||||||
|
|
||||||
开发教程入口:
|
- 【自动整理/查询/生成/同步/核对/提交】某类业务数据
|
||||||
|
- 帮你减少重复操作,降低人工录入或核对成本
|
||||||
|
- 在任务完成后给出清晰的处理结果、失败原因或后续建议
|
||||||
|
|
||||||
- <a href="references/REQUIREMENTS.md" target="_blank" rel="noopener noreferrer">需求文档模板</a>:给技术人员编写和查看研发需求的标准模板
|
---
|
||||||
- <a href="references/DEVELOPMENT.md" target="_blank" rel="noopener noreferrer">开发教程</a>:给技术人员的完整开发步骤说明
|
|
||||||
|
|
||||||
## 目录说明
|
## 适合什么场景
|
||||||
|
|
||||||
| 路径 | 用途 |
|
列出典型使用场景。请使用用户熟悉的业务语言,例如:
|
||||||
|------|------|
|
|
||||||
| `SKILL.md` | 技能清单与触发说明模板 |
|
|
||||||
| `assets/` | 示例输出与轻量 schema |
|
|
||||||
| `references/` | 面向用户与编排的文档模板 |
|
|
||||||
| `scripts/` | 规范分层后的代码骨架 |
|
|
||||||
| `tests/` | 单元测试或最小回归测试 |
|
|
||||||
| `evals/` | 人工/半自动评估材料 |
|
|
||||||
| `.github/workflows/release_skill.yaml` | 标准发布工作流 |
|
|
||||||
| `release.ps1` | 对齐现有 skill 的发布脚本入口 |
|
|
||||||
|
|
||||||
## 最小命令
|
- 每天需要重复处理同一类订单、单据、报表或客户资料
|
||||||
|
- 需要从多个系统中查询信息并汇总结果
|
||||||
|
- 需要按固定规则检查数据是否完整、是否异常
|
||||||
|
- 需要把处理结果保存为文件、记录或任务报告
|
||||||
|
|
||||||
```bash
|
---
|
||||||
python scripts/main.py health
|
|
||||||
python scripts/main.py version
|
|
||||||
```
|
|
||||||
|
|
||||||
## 注意
|
## 使用前需要准备什么
|
||||||
|
|
||||||
- 不要再往模板里引入旧式 `docs/` 或 `optional/` 目录。
|
说明用户在使用前要准备的东西,例如:
|
||||||
- 新技能若不需要某些目录,也建议先保留结构,再按实际业务填充内容。
|
|
||||||
|
- 相关账号已登录,且具备查询、导出或提交权限
|
||||||
|
- 需要处理的文件、时间范围、订单号、客户名称、项目名称等信息
|
||||||
|
- 如果涉及外部系统,请确认网络、账号、验证码或审批流程可正常使用
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 你可以这样告诉 Agent
|
||||||
|
|
||||||
|
用自然语言示例展示用户可以怎么发起任务,例如:
|
||||||
|
|
||||||
|
> “帮我查询今天的【业务对象】,整理成表格。”
|
||||||
|
>
|
||||||
|
> “帮我核对这些【数据/单据】是否有异常,并告诉我问题在哪里。”
|
||||||
|
>
|
||||||
|
> “帮我把【时间范围】内的【业务数据】导出并生成处理报告。”
|
||||||
|
>
|
||||||
|
> “帮我根据这个文件里的内容,逐条完成【业务动作】。”
|
||||||
|
|
||||||
|
> 注意:示例应是自然语言,不要写命令行命令。
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 执行后你会得到什么
|
||||||
|
|
||||||
|
说明用户会看到什么结果,例如:
|
||||||
|
|
||||||
|
- 任务是否成功完成
|
||||||
|
- 处理了多少条数据
|
||||||
|
- 哪些内容成功、哪些失败
|
||||||
|
- 失败原因和建议处理方式
|
||||||
|
- 生成的文件、表格、报告或记录
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 注意事项
|
||||||
|
|
||||||
|
用用户能理解的方式说明边界和风险,例如:
|
||||||
|
|
||||||
|
- 请确认输入范围准确,避免处理错数据
|
||||||
|
- 涉及提交、付款、删除、审批等高风险动作时,技能应在执行前请求确认
|
||||||
|
- 如果外部系统登录失效、权限不足或页面变化,任务可能需要用户介入
|
||||||
|
- 不建议在未核对数据的情况下直接执行批量操作
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 常见问题
|
||||||
|
|
||||||
|
### 为什么任务没有查到数据?
|
||||||
|
|
||||||
|
> 可能是时间范围、关键词、账号权限或系统筛选条件不正确。请先确认输入信息是否准确。
|
||||||
|
|
||||||
|
### 为什么任务执行到一半需要我操作?
|
||||||
|
|
||||||
|
> 可能遇到了登录、验证码、权限确认、二次审批或系统异常。请根据 Agent 的提示完成必要操作。
|
||||||
|
|
||||||
|
### 结果文件在哪里查看?
|
||||||
|
|
||||||
|
> 请根据 Agent 返回的结果说明查看生成文件、任务记录或处理报告。
|
||||||
|
|||||||
58
SKILL.md
58
SKILL.md
@@ -1,15 +1,16 @@
|
|||||||
---
|
---
|
||||||
name: 技能开发模板(复制后请修改)
|
name: 技能开发模板(通用业务版)
|
||||||
description: "这是 OpenClaw 技能开发模板仓库,不直接作为业务技能发布。复制为新技能仓库后,按本模板替换 slug、名称、说明、CLI 子命令与 service 实现。"
|
description: "OpenClaw 通用业务技能开发模板,供复制后定制新业务 skill。定制步骤见 development/DEVELOPMENT.md。"
|
||||||
version: 1.0.11
|
version: 1.0.14
|
||||||
author: 深圳匠厂科技有限公司
|
author: 深圳匠厂科技有限公司
|
||||||
metadata:
|
metadata:
|
||||||
openclaw:
|
openclaw:
|
||||||
slug: your-skill-slug
|
slug: your-skill-slug
|
||||||
|
platform_kit_min_version: "1.0.14"
|
||||||
emoji: "📦"
|
emoji: "📦"
|
||||||
category: "通用"
|
category: "通用"
|
||||||
developer_ids:
|
developer_ids:
|
||||||
- 1032
|
- 10032
|
||||||
- 12428
|
- 12428
|
||||||
allowed-tools:
|
allowed-tools:
|
||||||
- bash
|
- bash
|
||||||
@@ -17,29 +18,54 @@ allowed-tools:
|
|||||||
|
|
||||||
# 技能开发模板(skill-template)
|
# 技能开发模板(skill-template)
|
||||||
|
|
||||||
这是一个**用于复制的新技能模板**,不是业务技能本身。新建技能时,应复制本仓库结构,再把占位内容替换成你的真实业务实现。
|
这是一个**用于复制的新技能模板**,不是业务 skill 本身。复制后请替换占位内容,实现你的真实业务逻辑。
|
||||||
|
|
||||||
## 模板使用方式
|
## 文档分工
|
||||||
|
|
||||||
1. 复制目录为你的新 skill 仓库。
|
| 文档 | 读者 | 用途 |
|
||||||
2. 全局替换 `your-skill-slug`、`技能开发模板(复制后请修改)` 等占位词。
|
|------|------|------|
|
||||||
3. 按 `references/CLI.md`、`scripts/` 分层与 `README.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_skill_core` 分层。
|
- `assets/`、`development/`、`evals/`、`references/`、`scripts/`、`tests/`
|
||||||
|
- CLI 入口固定为 `scripts/main.py`
|
||||||
|
- 业务逻辑按 `cli / db / service / util` 分层
|
||||||
|
|
||||||
## 最小命令
|
## 最小命令
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
python {baseDir}/scripts/main.py health
|
python {baseDir}/scripts/main.py health
|
||||||
|
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`。
|
||||||
|
|
||||||
- 复制后请同步修改 `scripts/util/constants.py` 中的 `SKILL_SLUG` / `SKILL_VERSION`。
|
## 运行依赖
|
||||||
- 如技能无需持久化,可保留 `db/` 目录但不主动调用。
|
|
||||||
- 面向用户与编排的文档写在 `references/`,不要再新增旧式 `docs/` / `optional/` 结构。
|
- Python 运行环境由匠厂宿主注入**共享 runtime**:`{JIANGCHANG_DATA_ROOT}/python-runtime/.venv`。
|
||||||
|
- 公共能力来自共享 runtime 安装的 `jiangchang-platform-kit>=1.0.14`(`jiangchang_skill_core` 包);**不要 vendor** `scripts/jiangchang_skill_core/`,新技能不得在仓库内保留该目录副本。
|
||||||
|
- 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。
|
||||||
|
- `metadata.openclaw.platform_kit_min_version`(当前 `1.0.14`)是运行契约/兼容性声明,供宿主安装与启用时校验,**不是**技能 pip 依赖声明。
|
||||||
|
- Skill 代码**不要**自行 `pip install`;系统级依赖(如 VC++ Runtime)仅在 `health` / preflight 中提示用户安装。
|
||||||
|
- `health` 使用 `collect_runtime_diagnostics` 输出统一 runtime 诊断(只读,不下载/修复 media-assets)。
|
||||||
|
|
||||||
|
## 定制入口
|
||||||
|
|
||||||
|
1. 复制目录为你的新 skill 仓库,全局替换 `your-skill-slug` 等占位词。
|
||||||
|
2. 按 `development/DEVELOPMENT.md` 完成开发与文档定制。
|
||||||
|
3. 用户市场说明写入根 `README.md`;Agent 调用契约见 `references/CLI.md`、`references/SCHEMA.md`。
|
||||||
|
4. 同步修改 `scripts/util/constants.py` 中的 `SKILL_SLUG` / `SKILL_VERSION`。
|
||||||
|
|
||||||
|
## 平台元数据
|
||||||
|
|
||||||
|
- `metadata.openclaw.developer_ids`:技能发布后的默认开发者可见用户 ID 列表。
|
||||||
|
- 当 `access_scope = 0`(不公开)时,平台会把 `developer_ids` 中的用户自动补写到 `skill_user_access`。
|
||||||
|
- `developer_ids` 建议写为正整数数组;第一个 ID 会作为主开发者同步到 `skills.developer_id`。
|
||||||
|
|||||||
@@ -3,5 +3,6 @@
|
|||||||
- `examples/`:CLI 成功输出形状示例(虚构路径与数据)。
|
- `examples/`:CLI 成功输出形状示例(虚构路径与数据)。
|
||||||
- `schemas/`:轻量 JSON Schema 示例(`additionalProperties: true` 允许扩展字段)。
|
- `schemas/`:轻量 JSON Schema 示例(`additionalProperties: true` 允许扩展字段)。
|
||||||
|
|
||||||
- 面向用户的介绍见 `references/README.md`。
|
- 用户市场说明见根目录 [`README.md`](../README.md)
|
||||||
- 面向编排/CLI 的细节见 `references/CLI.md`、`RUNTIME.md`、`SCHEMA.md`。
|
- Agent 调用/编排参考见 [`references/`](../references/)
|
||||||
|
- 开发规范见 [`development/`](../development/)
|
||||||
|
|||||||
@@ -1,10 +1,12 @@
|
|||||||
{
|
{
|
||||||
"id": 1,
|
"id": 1,
|
||||||
"account_id": "demo_account_1",
|
"task_type": "demo_task",
|
||||||
"article_id": 12,
|
"target_id": "demo_target_1",
|
||||||
"article_title": "示例标题",
|
"input_id": "demo_input_42",
|
||||||
"status": "published",
|
"input_title": "示例任务输入标题",
|
||||||
|
"status": "success",
|
||||||
"error_msg": null,
|
"error_msg": null,
|
||||||
|
"result_summary": "{\"processed\": 10, \"skipped\": 0}",
|
||||||
"created_at": "2026-04-01T10:00:00",
|
"created_at": "2026-04-01T10:00:00",
|
||||||
"updated_at": "2026-04-01T10:00:00"
|
"updated_at": "2026-04-01T10:00:00"
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,19 +0,0 @@
|
|||||||
{
|
|
||||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
|
||||||
"$id": "https://openclaw.local/skill-template/publish-log-record.schema.json",
|
|
||||||
"title": "PublishLogRecord",
|
|
||||||
"description": "发布型技能 log-get 返回的单条日志对象模板",
|
|
||||||
"type": "object",
|
|
||||||
"required": ["id", "account_id", "article_id", "article_title", "status", "created_at", "updated_at"],
|
|
||||||
"properties": {
|
|
||||||
"id": { "type": "integer" },
|
|
||||||
"account_id": { "type": ["string", "integer"] },
|
|
||||||
"article_id": { "type": "integer" },
|
|
||||||
"article_title": { "type": "string" },
|
|
||||||
"status": { "type": "string" },
|
|
||||||
"error_msg": { "type": ["string", "null"] },
|
|
||||||
"created_at": { "type": ["string", "null"] },
|
|
||||||
"updated_at": { "type": ["string", "null"] }
|
|
||||||
},
|
|
||||||
"additionalProperties": true
|
|
||||||
}
|
|
||||||
21
assets/schemas/task-log-record.schema.json
Normal file
21
assets/schemas/task-log-record.schema.json
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
{
|
||||||
|
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||||
|
"$id": "https://openclaw.local/skill-template/task-log-record.schema.json",
|
||||||
|
"title": "TaskLogRecord",
|
||||||
|
"description": "通用业务技能 log-get 返回的单条任务日志对象模板",
|
||||||
|
"type": "object",
|
||||||
|
"required": ["id", "task_type", "status", "created_at", "updated_at"],
|
||||||
|
"properties": {
|
||||||
|
"id": { "type": "integer" },
|
||||||
|
"task_type": { "type": "string" },
|
||||||
|
"target_id": { "type": ["string", "null"] },
|
||||||
|
"input_id": { "type": ["string", "null"] },
|
||||||
|
"input_title": { "type": ["string", "null"] },
|
||||||
|
"status": { "type": "string" },
|
||||||
|
"error_msg": { "type": ["string", "null"] },
|
||||||
|
"result_summary": { "type": ["string", "null"] },
|
||||||
|
"created_at": { "type": ["string", "null"] },
|
||||||
|
"updated_at": { "type": ["string", "null"] }
|
||||||
|
},
|
||||||
|
"additionalProperties": true
|
||||||
|
}
|
||||||
128
development/ADAPTER.md
Normal file
128
development/ADAPTER.md
Normal file
@@ -0,0 +1,128 @@
|
|||||||
|
# 适配器标准:真实/仿真 × API/RPA 四档模式
|
||||||
|
|
||||||
|
> 凡是"连接三方系统"(ERP、CRM、SaaS、银行等)的 skill,都应采用本文的 **adapter 四档模式**。这样同一套业务逻辑可以在不同档位间切换:开发用 mock、半集成用 simulator、上线用真实,互不影响。
|
||||||
|
|
||||||
|
## 为什么要分档
|
||||||
|
|
||||||
|
对接一个外部系统,工程上无非四种方式的组合:
|
||||||
|
|
||||||
|
| | API(有接口) | RPA(操作界面) |
|
||||||
|
|---|---|---|
|
||||||
|
| **真实系统** | `real_api` 真实 API | `real_rpa` 真实界面 |
|
||||||
|
| **仿真/离线** | `mock` 纯内存/fixture | `simulator_rpa` 仿真站点/桌面仿真 |
|
||||||
|
|
||||||
|
### 四档定义
|
||||||
|
|
||||||
|
| 档位 | 含义 | 默认策略 |
|
||||||
|
|------|------|----------|
|
||||||
|
| **`mock`** | 纯内存或 fixture,**默认单测/CI** | 模板 `.env.example` 默认 `OPENCLAW_TEST_TARGET=mock` |
|
||||||
|
| **`simulator_rpa`** | 仿真站点或桌面仿真,可半集成 | 开发联调可选 |
|
||||||
|
| **`real_api`** | 真实 API | **必须** `ALLOW_REAL_API=1` |
|
||||||
|
| **`real_rpa`** | 真实浏览器/真实系统 | **必须** `ALLOW_REAL_RPA=1` |
|
||||||
|
|
||||||
|
- **mock**:纯离线、不联网,给单测 / CI / 开发自测,**保证可重复**。
|
||||||
|
- **simulator_rpa**:操作仿真平台(如 `sandbox.jc2009.com`),跑端到端流程但不碰生产。
|
||||||
|
- **real_api**:有官方接口时**首选**(最稳、最快、最易维护)。
|
||||||
|
- **real_rpa**:没有 API 只能操作生产界面,**风险最高、放最后**。
|
||||||
|
|
||||||
|
> 推荐优先级:**real_api > simulator_rpa > real_rpa**,mock 永远保留做 CI。
|
||||||
|
|
||||||
|
## 权限开关
|
||||||
|
|
||||||
|
未显式授权时**不得**落到真实 API/RPA:
|
||||||
|
|
||||||
|
| 变量 | 作用 |
|
||||||
|
|------|------|
|
||||||
|
| `ALLOW_REAL_API=1` | 允许 `real_api` 访问真实 HTTP |
|
||||||
|
| `ALLOW_REAL_RPA=1` | 允许 `real_rpa` 驱动真实浏览器/RPA |
|
||||||
|
| `ALLOW_WRITE_ACTIONS=1` | 在 `real_*` 下允许**写**操作(提交表单、下单等) |
|
||||||
|
|
||||||
|
进程环境变量优先于用户 `.env`(见 `CONFIG.md` 三层优先级)。
|
||||||
|
|
||||||
|
## 目录骨架
|
||||||
|
|
||||||
|
```
|
||||||
|
scripts/service/<domain>_adapter/
|
||||||
|
__init__.py # dispatch:按档位返回对应 adapter 实例
|
||||||
|
base.py # 数据契约(dataclass)+ AdapterBase 接口
|
||||||
|
mock.py # 离线仿真,给 CI/单测
|
||||||
|
real_api.py # 真实系统 API
|
||||||
|
sim_rpa.py # 仿真平台 RPA
|
||||||
|
real_rpa.py # 真实系统 RPA(占位,谨慎实现)
|
||||||
|
```
|
||||||
|
|
||||||
|
模板示例:
|
||||||
|
|
||||||
|
- **四档骨架(占位)**:`scripts/service/example_adapter/` — 复制改名后补齐业务 adapter
|
||||||
|
- **仿真浏览器 RPA 完整案例**:`examples/simulator_browser_rpa/` — mock + simulator_rpa + sandbox 页面 + 测试
|
||||||
|
- **真实浏览器 RPA 完整案例**:`examples/real_browser_rpa/` — 登录/验证码/滚动采集
|
||||||
|
|
||||||
|
> 先判断属于四象限哪一种(`real_browser_rpa` / `real_api` / `simulator_browser_rpa` / `simulator_api`),再读对应 `examples/*/README.md`。示例是**参考架构与边界**,不是业务代码原样复制。
|
||||||
|
|
||||||
|
## 档位 dispatch
|
||||||
|
|
||||||
|
由 `OPENCLAW_TEST_TARGET` 统一决定用哪个 adapter:
|
||||||
|
|
||||||
|
| `OPENCLAW_TEST_TARGET` | adapter | 用途 |
|
||||||
|
|---|---|---|
|
||||||
|
| `unit` / `mock` | `MockAdapter` | 单测 / CI,离线 |
|
||||||
|
| `simulator_rpa` | `SimRpaAdapter` | 开发/演示,操作仿真平台 |
|
||||||
|
| `real_api` | `RealApiAdapter` | 生产,走官方接口 |
|
||||||
|
| `real_rpa` | `RealRpaAdapter` | 生产,操作真实界面 |
|
||||||
|
|
||||||
|
```python
|
||||||
|
# __init__.py
|
||||||
|
def get_adapter():
|
||||||
|
target = (os.environ.get("OPENCLAW_TEST_TARGET") or "mock").lower()
|
||||||
|
if target in ("unit", "mock"):
|
||||||
|
return MockAdapter()
|
||||||
|
if target == "real_api":
|
||||||
|
return RealApiAdapter()
|
||||||
|
if target == "real_rpa":
|
||||||
|
return RealRpaAdapter()
|
||||||
|
return SimRpaAdapter()
|
||||||
|
```
|
||||||
|
|
||||||
|
`get_adapter()` 实现应配合 `tests/adapter_test_utils.py` 的 profile 策略:**未授权时不 silently 开启真实网络/RPA**。
|
||||||
|
|
||||||
|
## contract tests
|
||||||
|
|
||||||
|
每个 adapter **必须覆盖同一套契约测试**(复制 `tests/samples/test_service_contract.py.sample`):
|
||||||
|
|
||||||
|
- 至少覆盖:**timeout**、**unauthorized**、**invalid response**、**empty result**
|
||||||
|
- **mock 不允许真实网络**
|
||||||
|
- 使用 `FakeAdapter` 或等价 stub 模拟异常路径
|
||||||
|
|
||||||
|
## 兄弟依赖
|
||||||
|
|
||||||
|
依赖 account-manager 或其他兄弟技能时:
|
||||||
|
|
||||||
|
1. 在 `SKILL.md` 的 `metadata.openclaw.dependencies.required` 声明。
|
||||||
|
2. **普通兄弟技能调用**,优先走统一 **`service.sibling_bridge`**(`call_sibling_json`),**不要**在 `task_service.py`、`task_rpa.py` 等业务流程文件中到处散落 `subprocess.run`。
|
||||||
|
3. **account-manager 账号/租约能力**是例外:可参考 `examples/real_browser_rpa/scripts/service/account_client.py`,封装为**单一** `account_client.py`;允许在该文件内部集中通过 subprocess 调 account-manager CLI。
|
||||||
|
4. **不允许**直接 `import account-manager` 的内部 Python 模块(如 `service/`、`util/`、`db/`)。
|
||||||
|
5. **pick lease 后必须 `finally release lease`**;进程被 kill 后可能残留 lease,需在运维文档说明排查方式(查 account-manager lease 列表 / 手动释放)。
|
||||||
|
|
||||||
|
```python
|
||||||
|
# 普通兄弟技能 — 走 sibling_bridge
|
||||||
|
from service.sibling_bridge import call_sibling_json
|
||||||
|
|
||||||
|
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 技术选型与拟人/反反爬范式
|
||||||
|
- `CONFIG.md` — `.env` 里如何配置运行模式与目标地址
|
||||||
|
- `TESTING.md` — 测试 target 与隔离体系
|
||||||
99
development/CONFIG.md
Normal file
99
development/CONFIG.md
Normal file
@@ -0,0 +1,99 @@
|
|||||||
|
# 配置标准:`.env` 规范与首次落盘机制
|
||||||
|
|
||||||
|
> 解决"每个 skill 每次都要手动搞一堆环境变量、账号、地址"的痛点。统一约定:**模板内置 `.env.example`,skill 首次运行自动落盘到用户数据目录,之后从那里读。**
|
||||||
|
|
||||||
|
## 核心原则
|
||||||
|
|
||||||
|
1. **三层优先级**:`进程环境变量` > `{数据目录}/.env` > 仓库 `.env.example` 默认值。
|
||||||
|
2. **`.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`(**已存在则不整体覆盖**,保护用户改过的值)。
|
||||||
|
4. **技能升级合并缺失项**:`merge_missing_env_keys` 会把 `.env.example` 中新增、但用户 `.env` 尚未包含的 key **追加**进去(带注释块)。
|
||||||
|
5. **敏感信息绝不进 `.env`**:密码/密钥/口令走 account-manager 的 `secret-ref`(见下"红线")。
|
||||||
|
|
||||||
|
## 标准配置项(`.env` 分组)
|
||||||
|
|
||||||
|
每个 skill 的 `.env.example` 至少包含这些通用项(按需增减):
|
||||||
|
|
||||||
|
```ini
|
||||||
|
# ── 运行模式 / adapter 档位(见 ADAPTER.md)──
|
||||||
|
OPENCLAW_TEST_TARGET=mock # mock | simulator_rpa | real_api | real_rpa
|
||||||
|
|
||||||
|
# ── 目标系统(示例占位,复制后改为业务地址)──
|
||||||
|
TARGET_BASE_URL=https://sandbox.jc2009.com
|
||||||
|
|
||||||
|
# ── 默认账号(仅非敏感标识;密码走 account-manager)──
|
||||||
|
DEFAULT_LOGIN_ID=04110001
|
||||||
|
|
||||||
|
# ── 浏览器 / RPA ──
|
||||||
|
OPENCLAW_BROWSER_HEADLESS=0 # 0=有头(默认) 1=无头(CI)
|
||||||
|
OPENCLAW_PLAYWRIGHT_STEALTH=1 # 1=开启反检测指纹(默认)
|
||||||
|
|
||||||
|
# ── 录屏与失败存证 ──
|
||||||
|
OPENCLAW_RECORD_VIDEO=1 # 1=录屏+字幕+旁白+背景音+MP4(RPA 默认开,见 RPA.md)
|
||||||
|
OPENCLAW_ARTIFACTS_ON_FAILURE=1 # 1=失败截图(默认)
|
||||||
|
|
||||||
|
# ── 节流 / 超时 ──
|
||||||
|
STEP_DELAY_MIN=1.0
|
||||||
|
STEP_DELAY_MAX=5.0
|
||||||
|
HUMAN_WAIT_TIMEOUT=180 # 滑块/验证码/2FA 等人工超时(秒)
|
||||||
|
```
|
||||||
|
|
||||||
|
**业务专属配置必须带技能前缀**(如 `DEMO_XXX`、`MY_SKILL_XXX`),**不要污染全局命名空间**(禁止 `SCRAPE_1688_*`、`RECEIVE_ORDER_*` 等跨技能前缀写入模板)。
|
||||||
|
|
||||||
|
## 🚫 红线:敏感信息不进 `.env`
|
||||||
|
|
||||||
|
`.env` 只放**非敏感运行参数**(模式、地址、开关、超时、账号标识)。密码、密钥、动态口令、token——
|
||||||
|
|
||||||
|
- 走 **account-manager** 注册凭据,用 `--secret-storage env --secret-ref XXX` 引用;
|
||||||
|
- 真实值由宿主/用户通过进程环境变量注入,**不落任何文件、不进 git**。
|
||||||
|
|
||||||
|
`.gitignore` 必须包含:
|
||||||
|
|
||||||
|
```gitignore
|
||||||
|
.env
|
||||||
|
*.env.local
|
||||||
|
```
|
||||||
|
|
||||||
|
## 配置 bootstrap(模板提供)
|
||||||
|
|
||||||
|
模板在 `scripts/util/config_bootstrap.py` 提供 `bootstrap_skill_config()`:
|
||||||
|
|
||||||
|
```python
|
||||||
|
from util.config_bootstrap import bootstrap_skill_config
|
||||||
|
|
||||||
|
bootstrap_skill_config() # main.py 与 cli.app.main() 启动时调用
|
||||||
|
```
|
||||||
|
|
||||||
|
内部委托共享 runtime 的 `jiangchang_skill_core.config`:
|
||||||
|
|
||||||
|
```python
|
||||||
|
config.ensure_env_file(SKILL_SLUG, example_path) # 首次落盘
|
||||||
|
config.merge_missing_env_keys(example_path, env_path) # 升级后追加缺失 key
|
||||||
|
```
|
||||||
|
|
||||||
|
业务代码**只通过 `config.get*` 读配置**,不直接 `os.environ`:
|
||||||
|
|
||||||
|
```python
|
||||||
|
config.get("TARGET_BASE_URL")
|
||||||
|
config.get_bool("OPENCLAW_BROWSER_HEADLESS")
|
||||||
|
config.get_float("STEP_DELAY_MIN", 1.0)
|
||||||
|
```
|
||||||
|
|
||||||
|
## health / config-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` 绝对路径),便于排查落盘位置。
|
||||||
|
|
||||||
|
## doctor / setup 命令(可选)
|
||||||
|
|
||||||
|
每个 skill 可提供自检命令:
|
||||||
|
|
||||||
|
- `python scripts/main.py doctor`:检查浏览器、Profile/账号、`.env` 落盘、设备等。
|
||||||
|
- `python scripts/main.py setup`:初始化演示账号、首次落盘 `.env`。
|
||||||
|
|
||||||
|
## 相关文档
|
||||||
|
|
||||||
|
- `RPA.md` — 三端 RPA 标准与各开关含义
|
||||||
|
- `ADAPTER.md` — `OPENCLAW_TEST_TARGET` 四档模式
|
||||||
|
- `RUNTIME.md` — `CLAW_*` 环境变量与数据目录约定
|
||||||
@@ -2,14 +2,14 @@
|
|||||||
|
|
||||||
这份文档是给**技术人员**看的,目标不是解释概念,而是让你拿到 `skill-template` 后,可以**一步一步开发出一个新的 skill**。
|
这份文档是给**技术人员**看的,目标不是解释概念,而是让你拿到 `skill-template` 后,可以**一步一步开发出一个新的 skill**。
|
||||||
|
|
||||||
本文默认你开发的是当前最常见的一类技能:
|
本文默认你开发的是当前最常见的一类业务 skill:
|
||||||
|
|
||||||
- 有明确的 `scripts/main.py` CLI 入口
|
- 有明确的 `scripts/main.py` CLI 入口
|
||||||
- 可能需要读写本地 SQLite
|
- 可能需要读写本地 SQLite(例如任务日志 `task_logs`)
|
||||||
- 可能需要调用兄弟技能
|
- 可能需要调用兄弟技能或外部 HTTP / RPA
|
||||||
- 业务逻辑主要放在 `scripts/service/`
|
- 业务逻辑主要放在 `scripts/service/`
|
||||||
|
|
||||||
如果你开发的是发布型 skill,这个模板就是直接可用的起点。
|
发布、对账、代发、报表分析等场景都只是业务特例;模板提供通用骨架,复制后按领域补齐实现即可。
|
||||||
|
|
||||||
## 推荐 AI 开发工具
|
## 推荐 AI 开发工具
|
||||||
|
|
||||||
@@ -92,17 +92,18 @@
|
|||||||
your-skill/
|
your-skill/
|
||||||
├─ .github/
|
├─ .github/
|
||||||
├─ assets/
|
├─ assets/
|
||||||
|
├─ development/
|
||||||
├─ evals/
|
├─ evals/
|
||||||
├─ references/
|
├─ references/
|
||||||
├─ scripts/
|
├─ scripts/
|
||||||
│ ├─ cli/
|
│ ├─ cli/
|
||||||
│ ├─ db/
|
│ ├─ db/
|
||||||
│ ├─ jiangchang_skill_core/
|
|
||||||
│ ├─ service/
|
│ ├─ service/
|
||||||
│ └─ util/
|
│ └─ util/
|
||||||
├─ tests/
|
├─ tests/
|
||||||
├─ .gitignore
|
├─ .gitignore
|
||||||
├─ README.md
|
├─ README.md
|
||||||
|
├─ requirements.txt
|
||||||
├─ release.ps1
|
├─ release.ps1
|
||||||
└─ SKILL.md
|
└─ SKILL.md
|
||||||
```
|
```
|
||||||
@@ -110,7 +111,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/`:放人工验收材料、评估清单、示例场景
|
||||||
@@ -124,7 +128,6 @@ scripts/
|
|||||||
├─ main.py
|
├─ main.py
|
||||||
├─ cli/
|
├─ cli/
|
||||||
├─ db/
|
├─ db/
|
||||||
├─ jiangchang_skill_core/
|
|
||||||
├─ service/
|
├─ service/
|
||||||
└─ util/
|
└─ util/
|
||||||
```
|
```
|
||||||
@@ -145,14 +148,73 @@ scripts/
|
|||||||
|
|
||||||
- `service/`
|
- `service/`
|
||||||
作用:核心业务逻辑
|
作用:核心业务逻辑
|
||||||
比如发布流程、调用兄弟技能、浏览器自动化
|
比如任务编排、调用兄弟技能、外部 API、可选浏览器自动化
|
||||||
|
|
||||||
- `util/`
|
- `util/`
|
||||||
作用:常量、日志、路径、时间工具、通用帮助函数
|
作用:常量、日志、路径、时间工具、通用帮助函数
|
||||||
|
|
||||||
- `jiangchang_skill_core/`
|
公共能力(config、logging、runtime_env、rpa、media_assets、video_session、runtime_diagnostics)从共享 runtime 的 `jiangchang-platform-kit>=1.0.14` import,**不得**在 `scripts/` 下保留 `jiangchang_skill_core/` 副本。
|
||||||
作用:运行时环境与统一日志副本
|
|
||||||
一般按现有规范技能复制,不要自己乱改结构
|
## 3.2 开发 RPA 类 skill
|
||||||
|
|
||||||
|
若 skill 需要浏览器/桌面/手机自动化,按以下顺序落地:
|
||||||
|
|
||||||
|
1. **先读三份标准**:`RPA.md`(三端范式与反反爬)、`CONFIG.md`(`.env` 落盘与读取)、`ADAPTER.md`(四档 adapter)。
|
||||||
|
2. **从模板复制骨架**:
|
||||||
|
- `scripts/service/example_adapter/` → 改名为 `scripts/service/<domain>_adapter/`(仅 adapter 四档骨架,**不等同于**真实浏览器 RPA 成功案例)
|
||||||
|
- 若是 **真实浏览器 RPA**(真实网站、登录态、验证码、滚动采集),**必须先读** `examples/real_browser_rpa/README.md`,再按需参考:
|
||||||
|
- `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`,再按需参考:
|
||||||
|
- `examples/simulator_browser_rpa/scripts/service/browser_session.py`
|
||||||
|
- `examples/simulator_browser_rpa/scripts/service/adapter/`
|
||||||
|
- `examples/simulator_browser_rpa/scripts/service/task_service.py`
|
||||||
|
- `examples/simulator_browser_rpa/sandbox/demo_app.html`
|
||||||
|
3. **只用共享库,不在 skill 里重写反反爬**:
|
||||||
|
```python
|
||||||
|
from jiangchang_skill_core import config
|
||||||
|
from jiangchang_skill_core.rpa import launch_persistent_browser, anti_detect, wait_for_captcha_pass
|
||||||
|
```
|
||||||
|
上述 import 来自宿主共享 runtime 安装的 `jiangchang-platform-kit`,不是技能目录副本。
|
||||||
|
4. **mock 档必须离线可跑**(`OPENCLAW_TEST_TARGET=mock`);sim_rpa / real_* 按需单独测。
|
||||||
|
5. **桌面/手机**:本期标准见 RPA.md 第 2/3 节,复用 `jiangchang_desktop_sdk` / `screencast`,**不要在新 skill 里重复造包**(尚待实战验证)。
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### 3.1 `requirements.txt` 依赖规范
|
||||||
|
|
||||||
|
技能根目录的 `requirements.txt` 是**标准文件**,用于声明本技能**特有** Python 三方依赖。
|
||||||
|
|
||||||
|
- **公共依赖**(`jiangchang-platform-kit`、`playwright`、config、runtime diagnostics、RPA 公共能力等)由**宿主共享 runtime** 提供,**不要**写入技能 `requirements.txt`。
|
||||||
|
- `SKILL.md` 的 `metadata.openclaw.platform_kit_min_version`(当前 `1.0.14`)是运行契约/兼容性声明,供宿主安装与启用时校验,**不是** pip 依赖声明。
|
||||||
|
- 匠厂宿主安装/更新技能后,会将技能 `requirements.txt` 安装到共享 venv:`{JIANGCHANG_DATA_ROOT}/python-runtime/.venv`。
|
||||||
|
- **不要**在业务代码中 `subprocess` / `pip install`;缺依赖由 `health` 报错,由宿主负责安装。
|
||||||
|
- **版本约束尽量收窄**,降低多技能共享 venv 时的冲突风险。推荐范围写法:
|
||||||
|
- `pkg>=1.2.0,<2`
|
||||||
|
- 对**原生扩展 / 高风险依赖**(如 `chromadb`、`onnxruntime`)建议 pin 或窄范围,避免无上限:
|
||||||
|
- 不推荐:`chromadb>=0.5.0`
|
||||||
|
- 推荐:`chromadb>=0.5.23,<0.6`
|
||||||
|
- **不要**把系统组件(VC++ Runtime、浏览器安装包等)写进 `requirements.txt`;这类前置条件写在 `health` / preflight 文档与错误提示中。
|
||||||
|
- 若技能无额外 Python 依赖,可保留空文件或仅含注释说明;**不要**为 platform-kit 或 playwright 保留占位行。
|
||||||
|
- 独立本地开发环境若缺少公共包,可手动安装;**生产/宿主运行**由共享 runtime 负责。
|
||||||
|
|
||||||
|
### 3.3 公共能力不要复制
|
||||||
|
|
||||||
|
以下模块由 platform-kit 提供,技能内**不要**重复实现或 vendored 拷贝:
|
||||||
|
|
||||||
|
| 能力 | 导入方式 |
|
||||||
|
|------|----------|
|
||||||
|
| config | `from jiangchang_skill_core import config` |
|
||||||
|
| logging | `from jiangchang_skill_core.unified_logging import ...` 或 `util.logging_config` 薄封装 |
|
||||||
|
| runtime_env | `from jiangchang_skill_core.runtime_env import ...` 或 `util.runtime_paths` 薄封装 |
|
||||||
|
| rpa | `from jiangchang_skill_core.rpa import ...` |
|
||||||
|
| media_assets | `from jiangchang_skill_core.media_assets import ...` |
|
||||||
|
| video_session | `from jiangchang_skill_core.rpa.video_session import RpaVideoSession` |
|
||||||
|
| runtime_diagnostics | `from jiangchang_skill_core import collect_runtime_diagnostics, format_runtime_health_lines` |
|
||||||
|
|
||||||
|
`health` 应委托 `collect_runtime_diagnostics`,不要在 `scripts/util/` 自建 `runtime_diagnostics.py`。
|
||||||
|
|
||||||
## 4. 开发一个新 skill 的标准步骤
|
## 4. 开发一个新 skill 的标准步骤
|
||||||
|
|
||||||
@@ -160,10 +222,10 @@ scripts/
|
|||||||
|
|
||||||
### 第一步:复制模板并改目录名
|
### 第一步:复制模板并改目录名
|
||||||
|
|
||||||
例如你要开发 `weibo-publisher`:
|
例如你要开发 `your-skill-slug`(或 `disburse-payroll-icbc` 一类领域 skill):
|
||||||
|
|
||||||
1. 复制 `skill-template`
|
1. 复制 `skill-template`
|
||||||
2. 新目录改成 `weibo-publisher`
|
2. 新目录改成与 `slug` 一致的名称(如 `your-skill-slug`)
|
||||||
3. 初始化为独立 git 仓库
|
3. 初始化为独立 git 仓库
|
||||||
4. 关联它自己的远端仓库
|
4. 关联它自己的远端仓库
|
||||||
|
|
||||||
@@ -174,9 +236,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/` 下的平台占位文件名
|
4. `references/` 与 `development/` 下的文案
|
||||||
|
5. `scripts/service/` 下的业务占位实现(优先改 `task_service.py`)
|
||||||
|
|
||||||
最先要统一的是:
|
最先要统一的是:
|
||||||
|
|
||||||
@@ -186,70 +249,111 @@ scripts/
|
|||||||
- 平台内部键
|
- 平台内部键
|
||||||
- 日志 logger 名
|
- 日志 logger 名
|
||||||
|
|
||||||
|
此外,如果该技能发布后默认不公开(`access_scope = 0`),建议一开始就把 `SKILL.md` 中的 `metadata.openclaw.developer_ids` 配好。这样后续发布到平台时,开发者本人仍能在技能市场中看到并验证该技能。
|
||||||
|
|
||||||
## 5. 哪些占位内容必须替换
|
## 5. 哪些占位内容必须替换
|
||||||
|
|
||||||
复制后,至少要全局检查并替换下面这些内容:
|
复制后,至少要全局检查并替换下面这些内容:
|
||||||
|
|
||||||
- `your-skill-slug`
|
- `your-skill-slug`
|
||||||
- `your-platform-key`
|
- `your-platform-key`(若 skill 涉及外部平台对接,可能会用到这类占位)
|
||||||
- `技能开发模板(复制后请修改)`
|
- `技能开发模板(通用业务版)`
|
||||||
- `你的平台名`
|
- `你的平台名`
|
||||||
- `platform_playwright.py`
|
|
||||||
- `openclaw.skill.your_skill_slug`
|
- `openclaw.skill.your_skill_slug`
|
||||||
|
|
||||||
如果你是做发布型 skill,通常还要替换:
|
如果你的 skill 对外 CLI 需要自定义文案或别名(例如发布类对外仍叫 `publish`),通常还要替换:
|
||||||
|
|
||||||
- `publish` 命令中的中文提示
|
- `run` 命令中的中文提示与别名策略(见 `../references/CLI.md`)
|
||||||
- `references/CLI.md` 的命令示例
|
- `../references/CLI.md` 的命令示例
|
||||||
- `references/README.md` 的用户话术
|
- `../README.md` 的用户话术
|
||||||
- `references/SCHEMA.md` 的数据库文件名
|
- `../references/SCHEMA.md` 的字段映射补充说明
|
||||||
|
|
||||||
## 6. `SKILL.md` 应该怎么写
|
如需浏览器自动化,不要在模板里保留空的 `platform_playwright.py`;请按业务新建 `xxx_playwright.py`(命名自定),并在 `task_service.py` 中按需引用。
|
||||||
|
|
||||||
`SKILL.md` 是技能清单,不是设计文档。
|
## 6. `SKILL.md` 与根 `README.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/`(开发规范)的分工说明
|
||||||
- category
|
|
||||||
- dependencies
|
不要在 `SKILL.md` 里写大量用户市场文案或实现细节。
|
||||||
- 何时使用本技能
|
|
||||||
- 对用户的引导话术
|
### 根 `README.md`(用户市场说明)
|
||||||
- CLI 使用原则
|
|
||||||
|
根 `README.md` 是技能市场详情页主来源(`metadata.readme_md`),面向普通用户。
|
||||||
|
|
||||||
|
应包含 YAML frontmatter 的 `description`(市场列表简介),正文用用户能理解的语言说明:
|
||||||
|
|
||||||
|
- 能做什么、适合什么场景、使用前准备什么
|
||||||
|
- 如何自然语言告诉 Agent
|
||||||
|
- 执行结果、注意事项、常见问题
|
||||||
|
|
||||||
|
不要在根 `README.md` 里写开发教程、目录规范、release、requirements 等技术细节。
|
||||||
|
|
||||||
不要在 `SKILL.md` 里写大量实现细节。
|
|
||||||
实现细节放在:
|
实现细节放在:
|
||||||
|
|
||||||
- `references/`
|
- `development/`
|
||||||
- 代码注释
|
- `references/`(CLI / SCHEMA)
|
||||||
- `service/` 实现里
|
- 代码注释与 `service/` 实现
|
||||||
|
|
||||||
## 7. `references/` 应该放什么
|
### 关于 `metadata.openclaw.developer_ids`
|
||||||
|
|
||||||
`references/` 是当前规范 skill 的文档中心,建议至少有这些:
|
这是一个平台发布元数据字段,用于解决下面这个问题:
|
||||||
|
|
||||||
- `README.md`
|
- 技能发布后若平台记录中的 `access_scope = 0`,技能默认不公开
|
||||||
面向内部说明和技能作用介绍
|
- 如果不额外授权,连开发者自己也可能在技能市场里看不到这个技能
|
||||||
|
|
||||||
- `CLI.md`
|
因此可以在 `SKILL.md` 中声明:
|
||||||
写清楚命令、参数、默认值、兄弟技能调用方式
|
|
||||||
|
|
||||||
- `RUNTIME.md`
|
```yaml
|
||||||
写清楚运行时目录、环境变量、入口约定
|
metadata:
|
||||||
|
openclaw:
|
||||||
|
slug: your-skill-slug
|
||||||
|
category: 通用
|
||||||
|
developer_ids:
|
||||||
|
- 1032
|
||||||
|
- 12428
|
||||||
|
```
|
||||||
|
|
||||||
- `SCHEMA.md`
|
约定如下:
|
||||||
写清楚数据库路径、核心表结构、日志表结构
|
|
||||||
|
|
||||||
- `DEVELOPMENT.md`
|
- 只允许填写正整数用户 ID
|
||||||
写给技术人员的开发教程,也就是本文档
|
- 推荐使用数组,即使当前只有 1 个开发者
|
||||||
|
- 发布时平台会把这些用户自动补写到 `skill_user_access`
|
||||||
|
- 第一个 ID 会同步到 `skills.developer_id`
|
||||||
|
- 一期只做“补授权”,不会因为你 later 修改数组而自动撤销旧授权
|
||||||
|
|
||||||
如果后面某个 skill 需要更细的说明,可以再加:
|
## 7. 文档目录分工
|
||||||
|
|
||||||
- `ERRORS.md`
|
当前规范 skill 的文档按读者拆分:
|
||||||
- `INTEGRATION.md`
|
|
||||||
- `PLATFORMS.md`
|
**根 `README.md`**
|
||||||
|
|
||||||
|
- 用户市场详情页说明(含 frontmatter `description`)
|
||||||
|
|
||||||
|
**`SKILL.md`**
|
||||||
|
|
||||||
|
- LLM / OpenClaw 平台技能入口
|
||||||
|
|
||||||
|
**`references/`**(Agent 运行/编排/调用时渐进加载)
|
||||||
|
|
||||||
|
- `README.md` — Agent 参考索引
|
||||||
|
- `CLI.md` — 命令、参数、默认值、兄弟技能调用方式
|
||||||
|
- `SCHEMA.md` — 数据库路径、核心表结构、日志表结构
|
||||||
|
- 可按需增加 `ERRORS.md`、`INTEGRATION.md` 等编排向补充
|
||||||
|
|
||||||
|
**`development/`**(开发者 / AI 编程代理)
|
||||||
|
|
||||||
|
- `REQUIREMENTS.md` — 需求与验收
|
||||||
|
- `DEVELOPMENT.md` — 开发教程(本文档)
|
||||||
|
- `TESTING.md` — 测试分层与隔离(详见 [`TESTING.md`](TESTING.md))
|
||||||
|
- `ADAPTER.md`、`RPA.md`、`CONFIG.md`、`RUNTIME.md` — 技术规范
|
||||||
|
|
||||||
## 8. `assets/` 应该放什么
|
## 8. `assets/` 应该放什么
|
||||||
|
|
||||||
@@ -264,7 +368,7 @@ scripts/
|
|||||||
比如日志记录、机读 JSON 的 schema
|
比如日志记录、机读 JSON 的 schema
|
||||||
|
|
||||||
不要把正式研发文档放到 `assets/`。
|
不要把正式研发文档放到 `assets/`。
|
||||||
文档应该进 `references/`。
|
用户说明进根 `README.md`;Agent 编排资料进 `references/`;开发规范进 `development/`。
|
||||||
|
|
||||||
## 9. `cli` 层怎么写
|
## 9. `cli` 层怎么写
|
||||||
|
|
||||||
@@ -276,8 +380,8 @@ scripts/
|
|||||||
也就是说,`cli/app.py` 的职责是:
|
也就是说,`cli/app.py` 的职责是:
|
||||||
|
|
||||||
1. 打印帮助
|
1. 打印帮助
|
||||||
2. 定义 `publish / logs / log-get / health / version`
|
2. 定义 `run / logs / log-get / health / version`
|
||||||
3. 把参数转交给 `service.publish_service`
|
3. 把参数转交给 `service.task_service`
|
||||||
|
|
||||||
不要在 `cli/app.py` 里直接写:
|
不要在 `cli/app.py` 里直接写:
|
||||||
|
|
||||||
@@ -291,14 +395,14 @@ scripts/
|
|||||||
|
|
||||||
通常可以这样拆:
|
通常可以这样拆:
|
||||||
|
|
||||||
- `publish_service.py`
|
- `task_service.py`
|
||||||
放命令编排、参数兜底、结果分流
|
放命令编排、参数兜底、结果分流(例如 `cmd_run`)
|
||||||
|
|
||||||
- `sibling_bridge.py`
|
- `sibling_bridge.py`
|
||||||
放兄弟技能调用,例如调 `account-manager`、`content-manager`
|
放通用兄弟技能子进程工具(`call_sibling_json`);**具体调用哪个 slug 由业务在 `task_service.py` 决定**,不要在本文件硬编码发布类专用 helper
|
||||||
|
|
||||||
- `*_playwright.py`
|
- `xxx_playwright.py`(按需新建)
|
||||||
放浏览器后台自动化
|
浏览器后台自动化 **不在模板默认结构中**;若确有需要,按命名约定自建模块并在 `task_service.py` 引用
|
||||||
|
|
||||||
- `entitlement_service.py`
|
- `entitlement_service.py`
|
||||||
放鉴权逻辑
|
放鉴权逻辑
|
||||||
@@ -309,7 +413,7 @@ scripts/
|
|||||||
|
|
||||||
推荐流向是:
|
推荐流向是:
|
||||||
|
|
||||||
`cli.app` -> `service.publish_service` -> `service.sibling_bridge` / `service.xxx_playwright` -> `db`
|
`cli.app` -> `service.task_service` -> `service.sibling_bridge` / (可选)`service.xxx_playwright` -> `db`
|
||||||
|
|
||||||
## 11. `db` 层怎么写
|
## 11. `db` 层怎么写
|
||||||
|
|
||||||
@@ -318,8 +422,8 @@ scripts/
|
|||||||
- `db/connection.py`
|
- `db/connection.py`
|
||||||
只做连接和建表
|
只做连接和建表
|
||||||
|
|
||||||
- `db/publish_logs_repository.py`
|
- `db/task_logs_repository.py`
|
||||||
只做增删查改
|
只做增删查改(模板默认表:`task_logs`)
|
||||||
|
|
||||||
不要在 `db` 层里:
|
不要在 `db` 层里:
|
||||||
|
|
||||||
@@ -338,10 +442,7 @@ scripts/
|
|||||||
|
|
||||||
来做调用。
|
来做调用。
|
||||||
|
|
||||||
常见调用对象是:
|
调用哪些兄弟技能由具体业务决定;请在 `task_service.py` 中使用 `service.sibling_bridge.call_sibling_json(skill_slug, args)` 或 `get_sibling_main_path(skill_slug)`,不要在本模板仓库的 `sibling_bridge.py` 中堆积特定技能函数。
|
||||||
|
|
||||||
- `account-manager`
|
|
||||||
- `content-manager`
|
|
||||||
|
|
||||||
调用原则:
|
调用原则:
|
||||||
|
|
||||||
@@ -350,27 +451,27 @@ scripts/
|
|||||||
3. 用子进程调用
|
3. 用子进程调用
|
||||||
4. 机器可读输出优先 JSON
|
4. 机器可读输出优先 JSON
|
||||||
|
|
||||||
## 13. 如何开发发布型 skill
|
## 13. 如何开发一个新 skill
|
||||||
|
|
||||||
如果你开发的是 publisher 类 skill,建议按这个顺序做:
|
不管你开发的是发布类、采集类、分析类还是知识库类 skill,建议都先按下面这个顺序推进:
|
||||||
|
|
||||||
1. 先把目录结构搭完整
|
1. 先把目录结构搭完整
|
||||||
2. 先让 `health` / `version` 跑通
|
2. 先让 `health` / `version` 跑通
|
||||||
3. 再让 `publish_service.py` 的骨架跑通
|
3. 再把核心 `service` 骨架跑通
|
||||||
4. 再接 `sibling_bridge.py`
|
4. 再接兄弟技能桥接、数据库或外部系统
|
||||||
5. 最后再写 `*_playwright.py`
|
5. 最后再补浏览器自动化、复杂流程编排或高风险集成
|
||||||
|
|
||||||
不要一开始就直接写页面选择器。
|
不要一开始就直接写页面选择器、复杂接口编排或深层业务逻辑。
|
||||||
|
|
||||||
推荐先确保这些基础能力正常:
|
推荐先确保这些基础能力正常:
|
||||||
|
|
||||||
- 能取到账号
|
- CLI 入口能跑通
|
||||||
- 能取到文章
|
- 基础命令输出稳定
|
||||||
- 能写日志
|
- 关键依赖能取到
|
||||||
- CLI 子命令通了
|
- 日志或本地状态能落下来
|
||||||
- 错误返回值格式定好了
|
- 错误返回值格式定好了
|
||||||
|
|
||||||
然后再进浏览器自动化。
|
如果你的 skill 属于外联型 / RPA 型,可以把上面的“核心 `service`”具体落成 `task_service.py`,再按需接 `sibling_bridge.py`、(可选)`*_playwright.py`。这只是示例路径组合,不代表模板绑定某一业务领域。
|
||||||
|
|
||||||
## 14. 本地开发的最小验证顺序
|
## 14. 本地开发的最小验证顺序
|
||||||
|
|
||||||
@@ -387,12 +488,19 @@ python scripts/main.py version
|
|||||||
|
|
||||||
```bash
|
```bash
|
||||||
python scripts/main.py -h
|
python scripts/main.py -h
|
||||||
python scripts/main.py publish -h
|
python scripts/main.py <your-command> -h
|
||||||
```
|
```
|
||||||
|
|
||||||
### 3. 验证本地日志与数据库
|
### 3. 验证本地日志与数据库
|
||||||
|
|
||||||
如果是发布型 skill,再继续:
|
如果你的 skill 需要本地日志或数据库,再继续:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
python scripts/main.py <your-log-command>
|
||||||
|
python scripts/main.py <your-detail-command> <id>
|
||||||
|
```
|
||||||
|
|
||||||
|
如果你沿用了模板中的通用任务日志骨架,那么这里可以具体对应成:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
python scripts/main.py logs
|
python scripts/main.py logs
|
||||||
@@ -404,11 +512,25 @@ python scripts/main.py log-get 1
|
|||||||
比如:
|
比如:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
python scripts/main.py publish
|
python scripts/main.py <your-command>
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## 14.5 测试驱动的开发顺序
|
||||||
|
|
||||||
|
新 skill 不应该等所有业务做完才补测试。建议在每个开发阶段都先把对应测试跑一遍:
|
||||||
|
|
||||||
|
1. **目录搭建后**:跑 `python tests/run_tests.py -v`,确认全部默认测试套件通过。
|
||||||
|
- 此时 `test_skill_metadata.py` 会校验 `SKILL.md` slug 与 `constants.SKILL_SLUG` 一致;如果你只改了一边,会立刻发现。
|
||||||
|
2. **改完 constants 后**:再跑一次必跑套件,确认未引入回归。
|
||||||
|
3. **写完 service 业务后**:从 `tests/samples/test_service_contract.py.sample` 复制一份做契约测试。
|
||||||
|
4. **接外部系统时**:写在 `tests/integration/`,并配合 `OPENCLAW_TEST_TARGET` 显式开启。
|
||||||
|
|
||||||
|
详见 `TESTING.md`。
|
||||||
|
|
||||||
## 15. 发布到正式环境验证
|
## 15. 发布到正式环境验证
|
||||||
|
|
||||||
|
在执行 `release.ps1` 之前,必须先在本地确认 `python tests/run_tests.py -v` 通过。测试不通过不得发起正式发布。
|
||||||
|
|
||||||
当本地开发、自测和联调完成后,还需要把 skill 发布到正式环境做一次完整验证。建议技术人员严格按下面顺序执行,不要跳步。
|
当本地开发、自测和联调完成后,还需要把 skill 发布到正式环境做一次完整验证。建议技术人员严格按下面顺序执行,不要跳步。
|
||||||
|
|
||||||
### 第一步:在 skill 根目录执行 `release.ps1`
|
### 第一步:在 skill 根目录执行 `release.ps1`
|
||||||
@@ -419,6 +541,8 @@ python scripts/main.py publish
|
|||||||
.\release.ps1
|
.\release.ps1
|
||||||
```
|
```
|
||||||
|
|
||||||
|
如果你的技能使用了 `metadata.openclaw.developer_ids`,那么这一步触发的发布工作流除了同步 `skills` / `skill_versions` 外,还会在平台侧自动补开发者可见权限。测试非公开技能时,建议重点验证这部分是否生效。
|
||||||
|
|
||||||
这一步会自动完成标准发布动作,包括:
|
这一步会自动完成标准发布动作,包括:
|
||||||
|
|
||||||
1. 检查当前仓库状态
|
1. 检查当前仓库状态
|
||||||
@@ -458,7 +582,7 @@ python scripts/main.py publish
|
|||||||
如果工作流失败,不要继续做平台验证,而应该先回到代码仓库排查,例如:
|
如果工作流失败,不要继续做平台验证,而应该先回到代码仓库排查,例如:
|
||||||
|
|
||||||
- 发布脚本是否正常推送
|
- 发布脚本是否正常推送
|
||||||
- `SKILL.md`、`scripts/`、`references/` 是否齐全
|
- `SKILL.md`、根 `README.md`、`scripts/`、`references/`、`development/` 是否齐全
|
||||||
- 工作流文件是否存在
|
- 工作流文件是否存在
|
||||||
- 发布包结构是否符合模板规范
|
- 发布包结构是否符合模板规范
|
||||||
|
|
||||||
@@ -521,13 +645,16 @@ python scripts/main.py publish
|
|||||||
- [ ] 目录结构符合当前模板
|
- [ ] 目录结构符合当前模板
|
||||||
- [ ] `SKILL.md` 中 slug、名称、描述都已替换
|
- [ ] `SKILL.md` 中 slug、名称、描述都已替换
|
||||||
- [ ] `scripts/util/constants.py` 已修改
|
- [ ] `scripts/util/constants.py` 已修改
|
||||||
- [ ] `references/CLI.md` 示例命令已改成真实命令
|
- [ ] `../references/CLI.md` 示例命令已改成真实命令
|
||||||
- [ ] `service` 下的平台文件名已改对
|
- [ ] `service` 下的核心业务文件(如 `task_service.py`)已按领域改名并实现
|
||||||
- [ ] 没有残留旧平台名
|
- [ ] 没有残留旧平台名
|
||||||
- [ ] `health` / `version` 可运行
|
- [ ] `health` / `version` 可运行
|
||||||
- [ ] `.gitignore` 生效,没有把 `__pycache__` 提交进去
|
- [ ] `.gitignore` 生效,没有把 `__pycache__` 提交进去
|
||||||
- [ ] `release.ps1` 存在
|
- [ ] `release.ps1` 存在
|
||||||
- [ ] `.github/workflows/release_skill.yaml` 存在
|
- [ ] `.github/workflows/release_skill.yaml` 存在
|
||||||
|
- [ ] `python tests/run_tests.py -v` 全部通过
|
||||||
|
- [ ] 没有新增默认必跑测试访问真实网络或浏览器
|
||||||
|
- [ ] 如有 integration 测试需求,已写在 `tests/integration/` 下并保持 `.sample` 后缀
|
||||||
|
|
||||||
## 17. 常见错误
|
## 17. 常见错误
|
||||||
|
|
||||||
@@ -549,13 +676,13 @@ python scripts/main.py publish
|
|||||||
表现:
|
表现:
|
||||||
|
|
||||||
- 兄弟技能筛选账号失败
|
- 兄弟技能筛选账号失败
|
||||||
- 发布命令走错平台
|
- `run` 命令走错平台或 task_type
|
||||||
|
|
||||||
要检查:
|
要检查:
|
||||||
|
|
||||||
- `publish_service.py`
|
- `task_service.py`
|
||||||
- `sibling_bridge.py`
|
- `sibling_bridge.py`
|
||||||
- `references/CLI.md`
|
- `../references/CLI.md`
|
||||||
|
|
||||||
### 错误 3:把业务逻辑写进 CLI
|
### 错误 3:把业务逻辑写进 CLI
|
||||||
|
|
||||||
@@ -580,21 +707,23 @@ python scripts/main.py publish
|
|||||||
现在的新模板原则是:
|
现在的新模板原则是:
|
||||||
|
|
||||||
- 不做旧结构兼容
|
- 不做旧结构兼容
|
||||||
- 统一走 `references/` + `scripts/main.py`
|
- 用户说明在根 `README.md`,Agent 资料在 `references/`,开发规范在 `development/`
|
||||||
|
- 统一走 `scripts/main.py` 作为 CLI 入口
|
||||||
|
|
||||||
## 18. 推荐开发顺序总结
|
## 18. 推荐开发顺序总结
|
||||||
|
|
||||||
如果让一个新人照着做,我建议他按这个顺序:
|
如果让一个新人照着做,我建议他按这个顺序:
|
||||||
|
|
||||||
1. 复制模板并改目录名
|
1. 复制模板并改目录名
|
||||||
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. 跑 `health` / `version`
|
7. 跑 `python tests/run_tests.py -v`
|
||||||
8. 再做业务联调
|
8. 跑 `health` / `version`
|
||||||
9. 最后 release
|
9. 再做业务联调
|
||||||
|
10. 最后 release
|
||||||
|
|
||||||
## 19. 这份模板的底线要求
|
## 19. 这份模板的底线要求
|
||||||
|
|
||||||
@@ -602,7 +731,7 @@ python scripts/main.py publish
|
|||||||
|
|
||||||
- 目录结构统一
|
- 目录结构统一
|
||||||
- 入口统一为 `scripts/main.py`
|
- 入口统一为 `scripts/main.py`
|
||||||
- 文档统一放 `references/`
|
- 用户说明在根 `README.md`,Agent 资料在 `references/`,开发规范在 `development/`
|
||||||
- 业务核心逻辑统一放 `scripts/service/`
|
- 业务核心逻辑统一放 `scripts/service/`
|
||||||
- 不再使用旧模板历史结构
|
- 不再使用旧模板历史结构
|
||||||
|
|
||||||
14
development/README.md
Normal file
14
development/README.md
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
# 开发资料入口
|
||||||
|
|
||||||
|
本目录面向**人类开发者**与 **AI 编程代理**。开始定制 skill 前,建议按以下顺序阅读:
|
||||||
|
|
||||||
|
1. [`REQUIREMENTS.md`](REQUIREMENTS.md) — 需求文档模板与验收标准
|
||||||
|
2. [`DEVELOPMENT.md`](DEVELOPMENT.md) — 完整开发步骤与目录规范
|
||||||
|
3. [`TESTING.md`](TESTING.md) — 测试分层、隔离数据根与档位开关
|
||||||
|
4. [`ADAPTER.md`](ADAPTER.md) — 涉及外部系统对接时
|
||||||
|
5. [`RPA.md`](RPA.md) — 涉及浏览器 / 桌面 / 手机自动化时
|
||||||
|
6. [`CONFIG.md`](CONFIG.md) — `.env` 规范与 bootstrap 机制
|
||||||
|
7. [`RUNTIME.md`](RUNTIME.md) — 共享 runtime、数据路径与诊断约定
|
||||||
|
|
||||||
|
Agent 调用契约见 [`../references/CLI.md`](../references/CLI.md)、[`../references/SCHEMA.md`](../references/SCHEMA.md)。
|
||||||
|
用户市场说明见根目录 [`README.md`](../README.md),不要写进本目录。
|
||||||
248
development/REQUIREMENTS.md
Normal file
248
development/REQUIREMENTS.md
Normal file
@@ -0,0 +1,248 @@
|
|||||||
|
# 技能需求文档模板
|
||||||
|
|
||||||
|
这份文档用于给技术人员、产品人员和实施人员统一描述某个 skill 的研发需求。
|
||||||
|
|
||||||
|
建议原则:
|
||||||
|
|
||||||
|
- 一个 skill 对应一份主需求文档
|
||||||
|
- 需求先写清楚,再进入开发
|
||||||
|
- 文档描述“要做什么”和“做到什么程度”,不要在这里堆实现细节
|
||||||
|
- 实现方式、代码结构、发布流程,分别放到 `DEVELOPMENT.md`、`references/`、`development/` 下的技术规范
|
||||||
|
|
||||||
|
如果你是从 `skill-template` 复制新技能,请复制本文件后,把下面所有占位内容替换成你的真实项目内容。
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
# REQUIREMENTS
|
||||||
|
|
||||||
|
## 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`
|
||||||
|
- 真实联调(如有)放在 `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 | 张三 | 初版 |
|
||||||
|
```
|
||||||
282
development/RPA.md
Normal file
282
development/RPA.md
Normal file
@@ -0,0 +1,282 @@
|
|||||||
|
# RPA 操作标准(浏览器 / 桌面 / 手机)
|
||||||
|
|
||||||
|
> 本文是团队 RPA 开发的**统一标准**。任何需要"自动操作软件界面"的 skill,都应先读这份文档,按这里的选型和范式落地,不要每个项目重新踩坑。
|
||||||
|
|
||||||
|
我们开发的各类 skill,本质上都是在替人操作三类界面:**浏览器、桌面软件、手机软件**。三类的底层技术不同,但**工程范式相同**:保持登录态、有头运行、拟人操作、失败存证、人工兜底。
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 0. 三端通用范式(先看这个)
|
||||||
|
|
||||||
|
无论操作哪类界面,都遵循同一套约定:
|
||||||
|
|
||||||
|
| 约定 | 说明 |
|
||||||
|
|------|------|
|
||||||
|
| **保持登录态** | 复用持久化 Profile / session,避免每次重新登录触发风控;账号由 account-manager 下发,不硬编码 |
|
||||||
|
| **有头运行** | 默认有头(headless 易被识别 / 难人工介入);`OPENCLAW_BROWSER_HEADLESS=1` 仅给 CI |
|
||||||
|
| **拟人操作** | 真实事件(isTrusted=true),逐字输入、随机延迟、贝塞尔鼠标轨迹;严禁 JS 直接设值/JS 点击/JS 跳转 |
|
||||||
|
| **步骤间随机等待** | 每两步操作之间 `random_delay(min,max)`,区间由 `.env` 配置(默认 1~5s) |
|
||||||
|
| **人工兜底(HITL)** | 滑块 / 短信验证码 / 人脸 / U盾 / 动态口令 → **停下来轮询等人工**,超时报 `ERROR:XXX_NEED_HUMAN`,绝不自动硬闯 |
|
||||||
|
| **失败存证** | 失败必截图,合规场景全程录屏,统一存 `{数据目录}/rpa-artifacts/{batch_id}/{tag}_{ts}.png` |
|
||||||
|
| **选择器纪律** | 语义选择器优先(id/name/text/aria);**F12 确认后再写,严禁凭记忆猜 DOM** |
|
||||||
|
| **统一错误码** | `ERROR:REQUIRE_LOGIN` / `ERROR:CAPTCHA_NEED_HUMAN` / `ERROR:RATE_LIMITED` / `ERROR:LOGIN_TIMEOUT` 等,见下方错误码表 |
|
||||||
|
| **幂等 / 断点续跑** | 批量操作记录"已处理到第几条",崩溃后能续跑、不重复提交 |
|
||||||
|
|
||||||
|
> 三端各自实现一个会话抽象 `RpaSession`(launch / login / act / screenshot / close),上层 skill 不感知是浏览器还是手机。
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 1. 浏览器(标准已成熟)
|
||||||
|
|
||||||
|
**选型:Playwright + 系统 Chrome/Edge。** 这是团队验证最充分的一条线(标杆技能 `1688-scrape-contacts`、`receive-order` 已落地)。
|
||||||
|
|
||||||
|
### 1.0 生产路径(优先)
|
||||||
|
|
||||||
|
| 项 | 标准 |
|
||||||
|
|----|------|
|
||||||
|
| 浏览器 | **优先系统 Chrome/Edge** + `launch_persistent_context`,`channel="chrome"` 或 Edge;不用内置 Chromium,**技能内不要 `playwright install`** |
|
||||||
|
| 登录态 | 持久 Profile 目录;账号、profile、lease **统一走 account-manager**(或对应兄弟技能),不硬编码密码 |
|
||||||
|
| CDP | **仅作诊断 / 桌面宿主类场景**;**不要**作为强风控站点的默认生产路径 |
|
||||||
|
| 行为 | 模拟真实用户:真实点击、键盘、鼠标、地址栏输入;**不要**拼接搜索结果 URL、DOM 注入、`el.value=`、JS 跳转 |
|
||||||
|
| 模式 | 默认有头 `OPENCLAW_BROWSER_HEADLESS=0`;无头仅 CI |
|
||||||
|
| 反检测 | stealth 默认开 `OPENCLAW_PLAYWRIGHT_STEALTH=1`(见 1.1) |
|
||||||
|
|
||||||
|
### 1.1 Playwright 启动标准
|
||||||
|
|
||||||
|
1. **默认有头**:`OPENCLAW_BROWSER_HEADLESS=0`(`.env.example` 默认值)。
|
||||||
|
2. **stealth 默认开**:`OPENCLAW_PLAYWRIGHT_STEALTH=1`;通过 `add_init_script` 注入指纹淡化脚本。
|
||||||
|
3. **不要在技能里自行安装 playwright**;由宿主共享 runtime 提供。
|
||||||
|
4. **不要默认传 `--no-sandbox`**(除非特定容器环境且已评估风险)。
|
||||||
|
5. **不要默认传 `--disable-blink-features=AutomationControlled`**;platform-kit stealth 已覆盖,额外 flag 可能适得其反。
|
||||||
|
6. **可以** `ignore_default_args=["--enable-automation"]`(platform-kit `launch_persistent_browser` 已处理)。
|
||||||
|
7. **强风控平台**:优先真实点击、键盘、鼠标、地址栏、持久 profile;**不要**直接拼接搜索结果 URL 或 DOM 注入。
|
||||||
|
|
||||||
|
指纹淡化(stealth)典型项:`navigator.webdriver=undefined`、`chrome.runtime`、`permissions.query`、`plugins`、`languages` 等。共享实现见 `jiangchang_skill_core.rpa`(platform-kit **>= 1.0.14**)。
|
||||||
|
|
||||||
|
**拟人操作**(必做):
|
||||||
|
|
||||||
|
- 输入:逐字符 `keyboard.type(delay=90~240ms)`,**先真实点击聚焦再输入,绝不 `el.value=`**。
|
||||||
|
- 鼠标:贝塞尔曲线轨迹 + 微抖动;进场随机晃动。
|
||||||
|
- 导航:用真实点击触发,**不要 `window.location.href=` / JS 点击跳转**。
|
||||||
|
- 翻页:真实点击翻页控件,注意排除禁用态。
|
||||||
|
- 延迟:每步之间 `random_delay`(`.env` 中 `STEP_DELAY_MIN/MAX`)。
|
||||||
|
|
||||||
|
### 1.2 页面启动标准
|
||||||
|
|
||||||
|
**`launch_persistent_context` 的 `args` 只能放 Chrome 启动参数**(例如 `--start-maximized`、`--disable-blink-features=AutomationControlled` 等)。
|
||||||
|
|
||||||
|
**绝对不要把 `https://...` 这类 URL 放进 `args`。** 否则 Playwright 会报错:
|
||||||
|
|
||||||
|
```
|
||||||
|
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 仅用于诊断或桌面宿主已打开浏览器的场景。
|
||||||
|
|
||||||
|
### 1.3 HITL / 验证码
|
||||||
|
|
||||||
|
- 自动处理失败时**允许等待人工**:滑块、短信、人脸、U盾、动态口令等。
|
||||||
|
- 等待人工验证时必须有:**字幕 step**(用户可见动作说明)、**结构化日志**、**超时**(`HUMAN_WAIT_TIMEOUT`)。
|
||||||
|
- 检测到风控页(URL/DOM 特征)→ 抛 `ERROR:CAPTCHA_NEED_HUMAN`,轮询等待或超时,**不要强行绕过平台安全机制**。
|
||||||
|
- **不要自动操作滑块**。
|
||||||
|
|
||||||
|
> 共享库:`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 安装
|
||||||
|
|
||||||
|
生产/宿主运行:`playwright` 由共享 runtime 提供,无需在技能内 `pip install`。
|
||||||
|
|
||||||
|
独立本地开发环境若缺少 Python 包 `playwright` 时,可手动执行:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
pip install playwright # 仅独立开发补装;用系统 Chrome,无需 playwright install chromium
|
||||||
|
```
|
||||||
|
|
||||||
|
### 引用方式
|
||||||
|
|
||||||
|
共享实现位于宿主共享 runtime 安装的 `jiangchang-platform-kit`(`jiangchang_skill_core.rpa`)。复制后的业务技能直接 import,**技能仓库不得保留 rpa 公共代码副本**:
|
||||||
|
|
||||||
|
```python
|
||||||
|
from jiangchang_skill_core.rpa import (
|
||||||
|
launch_persistent_browser,
|
||||||
|
anti_detect,
|
||||||
|
wait_for_captcha_pass,
|
||||||
|
capture_failure,
|
||||||
|
errors,
|
||||||
|
)
|
||||||
|
from jiangchang_skill_core.rpa.stealth import stealth_enabled, STEALTH_INIT_SCRIPT
|
||||||
|
```
|
||||||
|
|
||||||
|
- `RpaVideoSession` 来自 platform-kit **>= 1.0.14**;ffmpeg、背景音乐、media-assets 由 platform-kit 统一解析;已提供前置/后置缓冲、字幕、TTS 旁白、背景音乐循环、结尾淡出。
|
||||||
|
- `health` 对上述资源做只读诊断,不下载、不修复。
|
||||||
|
|
||||||
|
### 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 封装
|
||||||
|
|
||||||
|
模板内 adapter 仿真示例(`scripts/service/example_adapter/sim_rpa.py`)仅用于 **adapter 四档骨架**,不能替代真实浏览器 RPA 规范。
|
||||||
|
|
||||||
|
### 1.6 仿真浏览器 RPA 示例(必读)
|
||||||
|
|
||||||
|
开发**自有仿真页面 / sandbox / 可控 DOM 的浏览器 RPA**(表单填写、批量提交、弹窗确认)时:
|
||||||
|
|
||||||
|
1. **必须先阅读** `examples/simulator_browser_rpa/README.md`
|
||||||
|
2. 再参考:
|
||||||
|
- `examples/simulator_browser_rpa/scripts/service/browser_session.py`
|
||||||
|
- `examples/simulator_browser_rpa/scripts/service/adapter/`(base / mock / simulator_rpa / dispatch)
|
||||||
|
- `examples/simulator_browser_rpa/scripts/service/task_service.py`
|
||||||
|
- `examples/simulator_browser_rpa/sandbox/demo_app.html`
|
||||||
|
|
||||||
|
> **先选类型再写代码**:真实第三方网站 → `real_browser_rpa`;自有 sandbox → `simulator_browser_rpa`。不要跨类型照抄 selector 或业务流程。
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 2. 桌面软件(Windows 原生程序)
|
||||||
|
|
||||||
|
**选型:pywinauto(UIA backend)为主 + 图像识别兜底。**
|
||||||
|
|
||||||
|
桌面端常见于 ERP 客户端、网银控件、银企直连等本地程序。优先走可访问性树(控件 ID/名字),坐标点击只做最后兜底。
|
||||||
|
|
||||||
|
| 技术 | 优先级 | 适用 / 说明 |
|
||||||
|
|------|--------|------|
|
||||||
|
| **pywinauto(`backend="uia"`)** | ✅ 首选 | 基于微软 UI Automation 树,拿控件 AutomationId/Name/ControlType,**稳定、不依赖屏幕坐标**,纯 Python |
|
||||||
|
| **FlaUI**(经 pythonnet 调 .NET) | 备选 | UIA 拿不到的复杂/自绘控件时更完整,但需引入 .NET 运行时 |
|
||||||
|
| **Playwright** | 特例 | 目标是 **Electron 套壳应用**(很多新 SaaS 客户端)时,可当浏览器驱动 |
|
||||||
|
| **PyAutoGUI / SikuliX(图像识别)** | ⚠️ 兜底 | 控件树完全拿不到时(Flash/远程桌面/纯自绘 UI);**靠截图找图+坐标,分辨率/缩放一变就崩**,仅最后手段 |
|
||||||
|
|
||||||
|
### 桌面端注意事项
|
||||||
|
|
||||||
|
- **窗口聚焦/置顶**:操作前确保目标窗口前置,避免误操作其它窗口。
|
||||||
|
- **DPI/缩放**:图像识别方案必须固定显示缩放比例;UIA 方案不受影响(优先用 UIA 即是为此)。
|
||||||
|
- **存证**:同样要失败截图(截目标窗口/全屏),存到 `rpa-artifacts`。
|
||||||
|
- **人工兜底**:U盾插拔、动态口令、人脸 → 停下等人工,超时 `ERROR:XXX_NEED_HUMAN`。
|
||||||
|
|
||||||
|
```bash
|
||||||
|
pip install pywinauto # UIA 自动化
|
||||||
|
# 图像兜底:pip install pyautogui opencv-python
|
||||||
|
```
|
||||||
|
|
||||||
|
> 状态:桌面端选型为推荐标准,**尚待真实项目实战验证**,落地时回补踩坑记录到本文。
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 3. 手机软件(USB 连接电脑)
|
||||||
|
|
||||||
|
**选型:Android 用 uiautomator2(或 Appium);iOS 用 Appium + WebDriverAgent(需 Mac)。** 底层都是经 USB 的 ADB / WDA。
|
||||||
|
|
||||||
|
| 平台 | 技术 | 优先级 | 说明 |
|
||||||
|
|------|------|--------|------|
|
||||||
|
| **Android** | **uiautomator2**(python 原生) | ✅ 首选 | ADB over USB,直接拿控件树点击/输入,比 Appium 轻快,纯 Python |
|
||||||
|
| Android | **Appium**(uiautomator2 driver) | 备选 | 需要跨平台统一接口、或团队已有 Appium 资产时用 |
|
||||||
|
| Android | **Airtest + Poco**(网易开源) | 兜底 | 图像+控件混合,自带 IDE 可录制;控件树拿不到时用 |
|
||||||
|
| **iOS** | **Appium + WebDriverAgent(XCUITest)** | 唯一可行 | **必须有一台 Mac 做中转**,Windows host 无法直接驱动 iOS |
|
||||||
|
| 投屏/人工介入 | **scrcpy** | 辅助 | USB 投屏到电脑,配合人工过验证码/人脸 |
|
||||||
|
|
||||||
|
### 手机端注意事项
|
||||||
|
|
||||||
|
- **设备就绪检查**:`adb devices` 确认已授权连接;放进 `doctor` 自检。
|
||||||
|
- **登录态**:靠 App 自身保持登录,必要时引导人工首登一次。
|
||||||
|
- **人工兜底**:短信验证码、人脸、指纹 → scrcpy 投屏让人工完成,程序轮询等待。
|
||||||
|
- **存证**:失败时 `adb screencap` / Appium 截图存 `rpa-artifacts`。
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# Android(首选)
|
||||||
|
pip install uiautomator2
|
||||||
|
python -m uiautomator2 init # 初始化设备端 agent
|
||||||
|
# 或统一走 Appium:pip install Appium-Python-Client(另需 Appium Server)
|
||||||
|
```
|
||||||
|
|
||||||
|
> 状态:手机端选型为推荐标准,**尚待真实项目实战验证**,落地时回补踩坑记录到本文。
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 4. 统一错误码(RPA 场景)
|
||||||
|
|
||||||
|
skill 退出/抛错统一用 `ERROR:` 前缀 + 稳定码,方便宿主与上层判断与重试:
|
||||||
|
|
||||||
|
| 错误码 | 含义 | 上层处理建议 |
|
||||||
|
|--------|------|------|
|
||||||
|
| `ERROR:REQUIRE_LOGIN` | 未登录 / 登录态失效 | 触发登录流程 |
|
||||||
|
| `ERROR:LOGIN_TIMEOUT` | 等待人工登录超时 | 提示用户重跑并及时操作 |
|
||||||
|
| `ERROR:CAPTCHA_NEED_HUMAN` | 命中滑块/验证码拦截 | 暂停等人工,或转人工队列 |
|
||||||
|
| `ERROR:RATE_LIMITED` | 触发频控 | 退避后重试 |
|
||||||
|
| `ERROR:MISSING_BROWSER` | 未检测到 Chrome/Edge | 提示安装 |
|
||||||
|
| `ERROR:DEVICE_NOT_READY` | 手机未连接/未授权 | 检查 USB/ADB |
|
||||||
|
| `ERROR:WINDOW_NOT_FOUND` | 桌面目标窗口未找到 | 检查程序是否启动 |
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 5. 存证与录屏规范
|
||||||
|
|
||||||
|
### 5.1 截图存证
|
||||||
|
|
||||||
|
- **路径**:`{CLAW_DATA_ROOT}/{user}/{slug}/rpa-artifacts/{batch_id}/{tag}_{timestamp}.png`
|
||||||
|
- **失败必截图**:受 `OPENCLAW_ARTIFACTS_ON_FAILURE`(默认开)控制。
|
||||||
|
- **Playwright 不负责录屏**,仅浏览器自动化。
|
||||||
|
- **常见 tag**:`before_submit` / `after_submit` / `captcha` / `login_fail` / `error`。
|
||||||
|
|
||||||
|
### 5.2 RPA 视频 step 标准
|
||||||
|
|
||||||
|
字幕是**用户可见动作说明**,不是技术日志。step 要贴近真实动作,不要只在大流程入口打点。
|
||||||
|
|
||||||
|
推荐关键动作示例(按业务裁剪):
|
||||||
|
|
||||||
|
- 启动浏览器
|
||||||
|
- 打开首页
|
||||||
|
- 检查登录状态
|
||||||
|
- 定位输入框
|
||||||
|
- 输入关键词:xxx
|
||||||
|
- 点击搜索
|
||||||
|
- 等待结果
|
||||||
|
- 打开详情页
|
||||||
|
- 提取信息
|
||||||
|
- 写入结果
|
||||||
|
- 任务完成
|
||||||
|
|
||||||
|
技术诊断、重复跳过、DB 写入可以显示但**通常不需要旁白**。
|
||||||
|
|
||||||
|
`title` / `closing_title` 必须由 skill 传入**中文业务文案**(如「开始执行示例任务」「示例任务执行完成」)。
|
||||||
|
|
||||||
|
### 5.3 录屏成片标准
|
||||||
|
|
||||||
|
- RPA skill 默认 `OPENCLAW_RECORD_VIDEO=1`。
|
||||||
|
- 使用 platform-kit 的 **`RpaVideoSession`**;**skill 不要自行合成视频**(不要自己调 ffmpeg 拼 MP4)。
|
||||||
|
- `OPENCLAW_RECORD_VIDEO=0` 时 session 无副作用(不启 ffmpeg、不写字幕文件)。
|
||||||
|
- **ffmpeg 是唯一录屏器**(Windows:`gdigrab` + `desktop`)。
|
||||||
|
- **最终视频**:`{skill_data_dir}/videos/{skill_slug}_{yyyyMMdd_HHmmss}_{batch_id}.mp4`
|
||||||
|
- **中间产物**:`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`)。
|
||||||
|
|
||||||
|
模板最小示范见 `scripts/service/task_service.py` 的 `_run_template_demo()`。
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 相关文档
|
||||||
|
|
||||||
|
- `ADAPTER.md` — 真实/仿真 × API/RPA 的四档适配器模式
|
||||||
|
- `CONFIG.md` — `.env` 配置规范与首次落盘机制
|
||||||
|
- `RUNTIME.md` — 运行时目录与环境变量约定
|
||||||
113
development/RUNTIME.md
Normal file
113
development/RUNTIME.md
Normal file
@@ -0,0 +1,113 @@
|
|||||||
|
# 运行时约定
|
||||||
|
|
||||||
|
## 共享 Python Runtime
|
||||||
|
|
||||||
|
**skill-template** 及复制出的新技能,公共能力均来自宿主匠厂安装的共享 Python Runtime(`jiangchang-platform-kit>=1.0.14` 及其传递依赖,含 `playwright`)。`jiangchang_skill_core` **不得**在技能仓库内 vendored,应由共享 venv 的 site-packages 提供。
|
||||||
|
|
||||||
|
技能根目录 `requirements.txt` **只声明技能特有依赖**;**不要**重复声明 `jiangchang-platform-kit` 或 `playwright`。`SKILL.md` 的 `platform_kit_min_version` 是运行契约,**不是** pip 依赖声明。
|
||||||
|
|
||||||
|
| 场景 | 推荐做法 |
|
||||||
|
|------|----------|
|
||||||
|
| 日常运行 | 由宿主匠厂触发技能 |
|
||||||
|
| 手工排查 / 测试机 | 使用共享 runtime 的 `python.exe` 执行 `scripts/main.py` |
|
||||||
|
| **不推荐** | 在技能目录内 `uv run python …` — 可能创建临时 venv,加载不到宿主共享 runtime |
|
||||||
|
|
||||||
|
占位命令(路径因环境而异,勿写死本机目录):
|
||||||
|
|
||||||
|
```text
|
||||||
|
Windows:
|
||||||
|
{JIANGCHANG_DATA_ROOT}\python-runtime\.venv\Scripts\python.exe {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`)。
|
||||||
|
|
||||||
|
## Runtime 诊断(platform-kit 1.0.14+)
|
||||||
|
|
||||||
|
`health` 命令通过 **`jiangchang_skill_core.collect_runtime_diagnostics`** 输出共享 runtime 诊断,**不在技能内重复实现**。典型字段:
|
||||||
|
|
||||||
|
- `skill_slug`、`python_executable`、`platform_kit_version`、`platform_kit_min_version`
|
||||||
|
- `jiangchang_skill_core_file` — 若从技能目录加载会输出 `runtime_issue[warning]`
|
||||||
|
- `CLAW_DATA_ROOT` / `JIANGCHANG_DATA_ROOT` / `resolved_data_root`
|
||||||
|
- `media_assets_root`、`ffmpeg_path`、`background_music_*`
|
||||||
|
- `record_video_enabled`、`runtime_issue[warning|error]`
|
||||||
|
|
||||||
|
`health` 是**只读诊断**,**不会**触发 media-assets 下载或修复。并补充 `env_path` / `env_exists` / `example_path` 配置路径字段。
|
||||||
|
|
||||||
|
## 配置 bootstrap
|
||||||
|
|
||||||
|
- 仓库内 `.env.example` 是配置模板(单一事实来源)。
|
||||||
|
- 用户实际 `.env`:`{CLAW_DATA_ROOT}/{CLAW_USER_ID}/{skill_slug}/.env`。
|
||||||
|
- `scripts/main.py` 与 `cli.app.main()` 启动时调用 `util.config_bootstrap.bootstrap_skill_config()`。
|
||||||
|
- 配置优先级:**进程环境变量** > **用户 `.env`** > **`.env.example` 默认值**。
|
||||||
|
- 公共 `config` / `merge_missing_env_keys` 来自共享 runtime 的 `jiangchang-platform-kit>=1.0.14`,**不得** vendored `scripts/jiangchang_skill_core/`。
|
||||||
|
|
||||||
|
## media-assets / ffmpeg / 背景音乐
|
||||||
|
|
||||||
|
背景音乐、ffmpeg 等共享资源由 platform-kit 统一解析,默认路径:
|
||||||
|
|
||||||
|
```text
|
||||||
|
{JIANGCHANG_DATA_ROOT}/shared/media-assets
|
||||||
|
```
|
||||||
|
|
||||||
|
(与 `CLAW_DATA_ROOT` 解析规则一致,由宿主注入。)
|
||||||
|
|
||||||
|
RPA 录屏成片(`RpaVideoSession`)、ffmpeg 路径、背景音乐探测均由 platform-kit 提供;技能只 import,不重复实现。
|
||||||
|
|
||||||
|
## 目录结构
|
||||||
|
|
||||||
|
新技能建议采用以下根目录结构:
|
||||||
|
|
||||||
|
- `assets/`
|
||||||
|
- `development/`
|
||||||
|
- `references/`
|
||||||
|
- `scripts/`
|
||||||
|
- `tests/`
|
||||||
|
- `evals/`
|
||||||
|
|
||||||
|
**标准 `scripts/` 分层(不含 `jiangchang_skill_core/` 副本):**
|
||||||
|
|
||||||
|
- `scripts/main.py`:唯一 CLI 入口
|
||||||
|
- `scripts/cli/`:参数解析与命令分发
|
||||||
|
- `scripts/db/`:SQLite 或本地持久化层
|
||||||
|
- `scripts/service/`:业务用例与外部交互
|
||||||
|
- `scripts/util/`:通用工具、常量、日志、路径薄封装
|
||||||
|
|
||||||
|
## 数据路径
|
||||||
|
|
||||||
|
推荐:
|
||||||
|
|
||||||
|
```text
|
||||||
|
{CLAW_DATA_ROOT}/{CLAW_USER_ID}/{skill_slug}/
|
||||||
|
```
|
||||||
|
|
||||||
|
数据库文件建议:
|
||||||
|
|
||||||
|
```text
|
||||||
|
{...}/{skill_slug}.db
|
||||||
|
```
|
||||||
|
|
||||||
|
## 明确禁止
|
||||||
|
|
||||||
|
- **不得** vendored `scripts/jiangchang_skill_core/`
|
||||||
|
- **不得**在技能内本地重复实现 `RuntimeDiagnostics` / `collect_runtime_diagnostics`
|
||||||
|
- **不得**写死开发机绝对路径作为运行时约定
|
||||||
|
|
||||||
|
## 测试时的运行时隔离
|
||||||
|
|
||||||
|
本模板的 `tests/_support.IsolatedDataRoot` 会在测试期间同时设置:
|
||||||
|
|
||||||
|
- `CLAW_DATA_ROOT` / `JIANGCHANG_DATA_ROOT` → 临时 tempfile 目录
|
||||||
|
- `CLAW_USER_ID` / `JIANGCHANG_USER_ID` → `_test`
|
||||||
|
|
||||||
|
退出时四个变量恢复,临时目录删除。所有 DB / 文件写入都被限制在临时目录内,不污染本机。
|
||||||
|
|
||||||
|
详见 `TESTING.md` 第 3 节。
|
||||||
|
|
||||||
|
## 编码与输出
|
||||||
|
|
||||||
|
- Windows 终端建议在 `scripts/main.py` 里做 UTF-8 stdout/stderr 包装
|
||||||
|
- 机读输出优先单行 JSON
|
||||||
|
- 错误前缀建议统一 `ERROR:`
|
||||||
232
development/TESTING.md
Normal file
232
development/TESTING.md
Normal file
@@ -0,0 +1,232 @@
|
|||||||
|
# 测试开发指南
|
||||||
|
|
||||||
|
面向复制 `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/` = 只在人被明确要求时才启用的高风险或重量级路径。
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 1. 测试体系总览
|
||||||
|
|
||||||
|
本模板把测试分成四层漏斗:**默认必跑(unittest + `run_tests.py`)**、从 `tests/samples/` **按需复制的 service / golden**、放在 `tests/integration/` **默认不落盘的仿真或真实联调范式(多数仍是 `.sample`)**、以及 **desktop E2E(pytest + 宿主 SDK)**。
|
||||||
|
|
||||||
|
[`tests/run_tests.py`](../tests/run_tests.py) 只做三件事:把 `scripts/` 与 `tests/` 放进 `sys.path`、做 Windows UTF-8 包装、收集 **`tests/` 根目录**下的 `test_*.py`。它不递归子目录——这正是刻意的安全边界:**不想让 AI 或拷贝粘贴 accidentally 把 integration 拉进默认套件**。
|
||||||
|
|
||||||
|
当你在设计一个新 skill 的测试策略时,请先问自己:**这段代码在没有外部凭证与浏览器的前提下是否有意义?** 若有,留在默认套件;若无,放进 integration / `.sample`,并要求明确的 `OPENCLAW_TEST_TARGET` 组合开关。
|
||||||
|
|
||||||
|
更深表格化的目录映射、`FakeAdapter` 与 profile 的耦合细节见 [`tests/README.md`](../tests/README.md) 开头章节『我该把测试写在哪里』。
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 2. 默认必跑测试要做什么
|
||||||
|
|
||||||
|
必跑套件要像一个紧张的守门员:**快、确定、离线**。典型覆盖:
|
||||||
|
|
||||||
|
- CLI:导入 [`cli.app`](../scripts/cli/app.py) 走解析链路、`health`(runtime diagnostics)/ `version` / `logs` / `log-get` 冒烟;
|
||||||
|
- 架构守护:无 `scripts/jiangchang_skill_core/`、`platform-kit>=1.0.14` 导入来源、文档/runtime 标准(见 `test_platform_import.py` 等);
|
||||||
|
- **真实 subprocess**:[`tests/test_entrypoint_subprocess.py`](../tests/test_entrypoint_subprocess.py) 再调用一遍 `python scripts/main.py`,防路径漂移;
|
||||||
|
- 运行时:`runtime_paths` 与 **`CLAW_*` / `JIANGCHANG_*` 并发兜底;
|
||||||
|
- `SKILL.md` YAML slug vs [`constants.SKILL_SLUG`](../scripts/util/constants.py);
|
||||||
|
- SQLite 骨架:`task_logs` 创建幂等与仓储读写;
|
||||||
|
- **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_*.py` 不允许隐形访问外网、不允许拉起真实浏览器、不允许读写开发者机器的真实数据根**。如果需要仿真服务器,也应仅在 integration(并由档位变量放行)。
|
||||||
|
|
||||||
|
套件能力与表格参见 [`tests/README.md`](../tests/README.md) 「1.2 默认套件覆盖」。
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 3. 数据隔离:IsolatedDataRoot
|
||||||
|
|
||||||
|
[`tests/_support.py`](../tests/_support.py) 提供的上下文管理器 `IsolatedDataRoot()`:进入一个专用临时目录,**镜像写入四套变量**:`CLAW_DATA_ROOT`、`JIANGCHANG_DATA_ROOT`(同一 tempfile)、以及用户镜像 ID:`CLAW_USER_ID`、`JIANGCHANG_USER_ID` → `_test`。
|
||||||
|
|
||||||
|
结束时:**恢复原 environ**,删除目录。
|
||||||
|
|
||||||
|
这样可以断言:**SQLite DB / spill files / caches** 都在 sandbox;不会在开发者桌面遗留 `{REAL_ROOT}`。
|
||||||
|
|
||||||
|
示例:
|
||||||
|
|
||||||
|
```python
|
||||||
|
from _support import IsolatedDataRoot
|
||||||
|
|
||||||
|
def test_whatever():
|
||||||
|
with IsolatedDataRoot():
|
||||||
|
from db.connection import init_db
|
||||||
|
init_db()
|
||||||
|
# …断言读写均在隔离路径…
|
||||||
|
```
|
||||||
|
|
||||||
|
**不要把真实凭证路径硬编码进默认测试**:隔离不等于你有权触碰真实目录。
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 4. 测试目标档位(OPENCLAW_TEST_TARGET)
|
||||||
|
|
||||||
|
模板采用统一闸门:**你想跑到哪一层外部世界,就用变量明说**。合法档位(非法值会让 helper 抛错)如下——**直接摘录自 [`tests/README.md`](../tests/README.md) §5.1**:
|
||||||
|
|
||||||
|
| 取值 | 含义 |
|
||||||
|
|------|------|
|
||||||
|
| `unit` | **默认**:单元 / 内存 / mock,不跑仿真与真实外联 |
|
||||||
|
| `mock` | 与 `unit` 类似的安全档位(显式语义) |
|
||||||
|
| `simulator_api` | 仅允许 **仿真 HTTP** 类集成(如 localhost) |
|
||||||
|
| `simulator_rpa` | 仅允许 **仿真页面 / 录播 RPA** |
|
||||||
|
| `real_api` | 真实 API(另需 `ALLOW_REAL_API=1`) |
|
||||||
|
| `real_rpa` | 真实 RPA(另需 `ALLOW_REAL_RPA=1`) |
|
||||||
|
|
||||||
|
未设置环境变量 ⇒ 等价 `unit`。
|
||||||
|
|
||||||
|
授权开关(显式 `1`)语义 **`ALLOW_REAL_API` / `ALLOW_REAL_RPA` / `ALLOW_WRITE_ACTIONS`**——摘录 [`tests/README.md`](../tests/README.md) §5.2:
|
||||||
|
|
||||||
|
| 变量 | 作用 |
|
||||||
|
|------|------|
|
||||||
|
| `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`(历史拼写)。
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 5. FakeAdapter:怎么模拟外部系统
|
||||||
|
|
||||||
|
[`tests/adapter_test_utils.py`](../tests/adapter_test_utils.py) 暴露 `FakeAdapter`,典型四种 **mode**:
|
||||||
|
|
||||||
|
| mode | 用途 |
|
||||||
|
|------|------|
|
||||||
|
| `success` | 构造干净的成功响应路径 |
|
||||||
|
| `timeout` | 模拟悬挂 / 慢链路 |
|
||||||
|
| `invalid_response` | 畸形负载 / schema drift |
|
||||||
|
| `unauthorized` | token / license / cookie 失效语义 |
|
||||||
|
|
||||||
|
**何时用它**:service 层出现『调用第三方 HTTP / RPA stub』但又不能把真实系统纳入 CI。**契约测试**(复制 [`tests/samples/test_service_contract.py.sample`](../tests/samples/test_service_contract.py.sample))应优先组合 FakeAdapter,而不是直接把 CLI when-json 断言堆上天。
|
||||||
|
|
||||||
|
把它看成:**你把不确定性收敛到可控的测试矩阵里**,而不是在生产日志里才第一次看到错位字段。
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 6. 怎么从 .sample 启用一个测试
|
||||||
|
|
||||||
|
步骤模板:
|
||||||
|
|
||||||
|
1. 找到范式文件(例如 [`tests/samples/test_service_contract.py.sample`](../tests/samples/test_service_contract.py.sample))。
|
||||||
|
2. **复制**到 `tests/` 根:`tests/test_service_contract.py`(去掉 `.sample`)。
|
||||||
|
3. 打开副本:**替换占位函数名 /技能特有枚举 / adapter profile**,删掉与本技能无关的示例断言。
|
||||||
|
4. 本地执行:`python tests/run_tests.py -v [可选筛选关键词]`。
|
||||||
|
5. **不要把 integration `.sample` 批量改名混进根目录**——除非你已经读过 [`tests/integration/README.md`](../tests/integration/README.md) 的风险清单。
|
||||||
|
|
||||||
|
Golden fixture 流程同理([`tests/samples/test_golden_cases.py.sample`](../tests/samples/test_golden_cases.py.sample))。
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 7. 真实联调测试的安全约束
|
||||||
|
|
||||||
|
任何 touching **真实租户数据** 的路径:
|
||||||
|
|
||||||
|
1. **禁止**硬编码 token / cookie / 内部域名落入仓库;
|
||||||
|
2. **禁止**默认套件隐式导入 integration;
|
||||||
|
3. **真实 RPA** 只能标记为手动触发(双人复核 / 本地 `.env` 不入库)。
|
||||||
|
|
||||||
|
范式阅读 [`tests/integration/README.md`](../tests/integration/README.md):那里有针对凭证来源、目录 artifact 忽略策略的补充。
|
||||||
|
|
||||||
|
记住:**测试代码也是一种部署面**,别把 staging 凭证写死进仓库。
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 8. 新 skill 的最小测试清单
|
||||||
|
|
||||||
|
以下清单 **原文摘自 [`tests/README.md`](../tests/README.md) 「新技能最小测试清单」**(复制新仓库后逐项勾选):
|
||||||
|
|
||||||
|
- [ ] `python tests/run_tests.py -v` 能通过。
|
||||||
|
- [ ] `python scripts/main.py health` 能通过。
|
||||||
|
- [ ] `python scripts/main.py version` 输出 JSON,且 `skill` 与目录名 / `SKILL.md` / `constants.SKILL_SLUG` 一致。
|
||||||
|
- [ ] 所有 DB / 文件写入都在 `IsolatedDataRoot`(或等价隔离)下测试,不写真实数据目录。
|
||||||
|
- [ ] 外部系统默认使用 mock / `FakeAdapter`,不访问真实 API,不打开真实 RPA。
|
||||||
|
- [ ] 至少有 1 个成功路径测试。
|
||||||
|
- [ ] 至少有 1 个缺必填字段 / 非法输入测试。
|
||||||
|
- [ ] 如果有 adapter,至少覆盖 timeout / unauthorized / invalid response(可用 fake 模拟)。
|
||||||
|
- [ ] 如果有解析 / 计算 / 校验类业务,至少保留 1 组 golden fixture(脱敏)。
|
||||||
|
- [ ] 真实 API / 真实 RPA 测试只放 `tests/integration/`,并且默认 `.sample`,不默认运行。
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 9. AI 编程工具使用测试时的红线
|
||||||
|
|
||||||
|
改编自 [`tests/README.md`](../tests/README.md) 「AI 编程工具注意事项」,压缩成 skill 开发者视角:
|
||||||
|
|
||||||
|
| 红线 | 解释 |
|
||||||
|
|------|------|
|
||||||
|
| 不改业务凑测试 | 除非需求变更已确认,否则**不要为了让 CI 变绿而砍掉业务分支** |
|
||||||
|
| **默认套件零外联** | 不把真实 HTTP / 浏览器写进 `tests/test_*.py` |
|
||||||
|
| `.sample` 尊重 | 集成范式改名前先读完 README;别让 `.sample` 悄悄变成根测试 |
|
||||||
|
| **零硬编码凭证** | token / cookie / 生产 URL → 用虚构域名或 vault ref |
|
||||||
|
| mock 优先 | 逻辑应在 service + FakeAdapter,而不是巨胖 CLI 断言 |
|
||||||
|
| 结构化错误 | 断言错误码字段,而不是 substring of stderr 漂移集合 |
|
||||||
|
| **integration = 显式开关** | `OPENCLAW_TEST_TARGET` + `ALLOW_REAL_*` / `ALLOW_WRITE_ACTIONS` |
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 10. 测试和发布的关系
|
||||||
|
|
||||||
|
**在运行 [`release.ps1`](../release.ps1) 之前,`python tests/run_tests.py -v` 必须绿色**。失败的默认套件意味着:
|
||||||
|
|
||||||
|
- 打包路径可能根本不可运行;
|
||||||
|
- CI 加密前的静态假设可能在宿主崩溃;
|
||||||
|
- metadata slug 漂移将被市场拒绝。
|
||||||
|
|
||||||
|
把『本地 unittest 绿』视作 tag 的前置条件,而不是『有空再跑』。发布流水线成功后仍要做安装验证——那是另一个维度;**测试是第一个维度**。
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 11. pytest 收集卫生标准
|
||||||
|
|
||||||
|
模板根目录提供 `pytest.ini`(或等价配置),约束:
|
||||||
|
|
||||||
|
- **只收集** `test_*.py` / `*_test.py`
|
||||||
|
- **不让** `.txt`、结果文件、日志文件被 pytest 误收集
|
||||||
|
- `norecursedirs` 排除 `integration/`、`desktop/`、`samples/`、`fixtures/`、`artifacts/`、`diagnostics/`
|
||||||
|
|
||||||
|
测试结果文件**不要**放在 `tests/` 根目录;应放 `tests/artifacts/` 或 `tests/diagnostics/` 并加入 `.gitignore`。
|
||||||
|
|
||||||
|
默认 `python tests/run_tests.py` 仍只发现 `tests/` 根目录一层 `test_*.py`(不递归子目录),与 pytest 策略一致。
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 12. RPA / video 测试标准
|
||||||
|
|
||||||
|
- `RpaVideoSession` 调用**不跑真实 ffmpeg**:单测中使用 `unittest.mock` patch session 或设 `OPENCLAW_RECORD_VIDEO=0`。
|
||||||
|
- 断言 `title` / `closing_title` 是**中文**业务文案。
|
||||||
|
- 断言 video artifact 会进入 `result_summary`(`video_path`、`raw_video`、`video_log` 等)。
|
||||||
|
- 断言 step 文案贴近用户动作,不是技术日志(如「准备执行示例任务」而非「enter cmd_run」)。
|
||||||
|
|
||||||
|
参考 `tests/test_video_service.py`。
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 13. 宿主 E2E 标准
|
||||||
|
|
||||||
|
- 使用 `jiangchang_desktop_sdk.e2e_helpers`(见 `tests/desktop/` 下 `.sample`)。
|
||||||
|
- **不伪造**用户数据目录;通过宿主 IPC 获取真实 `skill_data_dir`。
|
||||||
|
- E2E **不自动生成**真实密钥或生产凭证。
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 14. 测试前检查清单(模板守护)
|
||||||
|
|
||||||
|
复制新 skill 或修改模板后,确认:
|
||||||
|
|
||||||
|
- [ ] `requirements.txt` **不含** `jiangchang-platform-kit` / `playwright`
|
||||||
|
- [ ] 无 `scripts/jiangchang_skill_core/` vendored 副本
|
||||||
|
- [ ] `platform_kit_min_version` **>= 1.0.14**(`SKILL.md` + `constants.py`)
|
||||||
|
- [ ] `health` 能输出 `platform_kit_version_ok`(或等价诊断行)
|
||||||
|
- [ ] `config-path` 可输出用户 `.env` 路径 JSON
|
||||||
|
- [ ] `pytest.ini` 存在且 `python_files` 只收集 `test_*.py` / `*_test.py`
|
||||||
|
- [ ] pytest **不会**误收集 `tests/` 下的 `.txt` / 日志 / 结果文件
|
||||||
|
- [ ] `python tests/run_tests.py -v` 全部通过
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
- [`tests/README.md`](../tests/README.md) — 表格、变量与目录细则
|
||||||
|
- [`DEVELOPMENT.md`](DEVELOPMENT.md) §14.5 — 测试驱动的开发顺序
|
||||||
|
- [`tests/integration/README.md`](../tests/integration/README.md) — 高风险用法
|
||||||
36
examples/README.md
Normal file
36
examples/README.md
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
# Skill Template Examples
|
||||||
|
|
||||||
|
四类 adapter 示例(先选类型,再写代码;**不要跨类型照抄**):
|
||||||
|
|
||||||
|
| 目录 | 类型 | 状态 | 适用场景 |
|
||||||
|
|---|---|---|---|
|
||||||
|
| real_browser_rpa | 真实浏览器 RPA | **已有案例** | 真实第三方网站、登录态、人工验证、滚动采集 |
|
||||||
|
| simulator_browser_rpa | 仿真浏览器 RPA | **已有案例** | 自有 sandbox、可控页面、表单 RPA、端到端教学/回归 |
|
||||||
|
| real_api | 真实 API | 占位,后续补案例 | 真实系统接口、token、权限、限流 |
|
||||||
|
| simulator_api | 仿真 API | 占位,后续补案例 | mock/sandbox API、无需浏览器 |
|
||||||
|
|
||||||
|
## 两类浏览器 RPA 的区别
|
||||||
|
|
||||||
|
| | real_browser_rpa | simulator_browser_rpa |
|
||||||
|
|---|---|---|
|
||||||
|
| 目标 | 真实第三方网页 | 自有仿真页面 / sandbox |
|
||||||
|
| 风控 | 高(验证码、登录态、反爬) | 低(DOM 可控) |
|
||||||
|
| 账号 | 通常需 account-manager + profile | 示例可用 env / demo 账号 |
|
||||||
|
| 用途 | 生产级真实采集/操作参考 | adapter 分层 + 表单 RPA 教学 |
|
||||||
|
|
||||||
|
## 当前状态
|
||||||
|
|
||||||
|
- **real_browser_rpa**:已有可参考代码 → 先读 `real_browser_rpa/README.md`
|
||||||
|
- **simulator_browser_rpa**:已有完整示例(含 `sandbox/demo_app.html`)→ 先读 `simulator_browser_rpa/README.md`
|
||||||
|
- **real_api** / **simulator_api**:目录占位,后续补案例
|
||||||
|
|
||||||
|
## 如何选择示例
|
||||||
|
|
||||||
|
| 你的场景 | 参考目录 |
|
||||||
|
|---|---|
|
||||||
|
| 真实网站 + 浏览器 + 登录/验证码/滚动 | **real_browser_rpa** |
|
||||||
|
| 仿真页面 + 浏览器 + 表单/批量提交 | **simulator_browser_rpa** |
|
||||||
|
| 真实系统 API | 后续 **real_api** |
|
||||||
|
| mock / sandbox API(无浏览器) | 后续 **simulator_api** |
|
||||||
|
|
||||||
|
> 示例提供的是**参考架构与边界**,不是业务代码原样复制。复制前先读对应 README 的「复制边界 / 禁止照抄」章节。
|
||||||
15
examples/real_api/README.md
Normal file
15
examples/real_api/README.md
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
# Real API Example
|
||||||
|
|
||||||
|
这个目录用于沉淀“真实系统 API”类 skill 的可复制成功案例。
|
||||||
|
|
||||||
|
## 适用场景
|
||||||
|
|
||||||
|
- 目标系统提供真实 API
|
||||||
|
- 需要 token / secret / credential
|
||||||
|
- 需要请求重试、权限校验、限流处理、schema 校验和幂等写库
|
||||||
|
|
||||||
|
## 当前状态
|
||||||
|
|
||||||
|
- 本目录暂时只保留结构占位。
|
||||||
|
- 示例业务尚未确定。
|
||||||
|
- 本轮不包含示例代码。
|
||||||
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
171
examples/real_browser_rpa/README.md
Normal file
171
examples/real_browser_rpa/README.md
Normal file
@@ -0,0 +1,171 @@
|
|||||||
|
# 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. **先阅读** `examples/real_browser_rpa/README.md`(本节及下方「复制边界」)
|
||||||
|
2. 复制整个 `examples/real_browser_rpa/` 到新 skill 的 `scripts/` 结构(或作为起点合并)
|
||||||
|
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. 添加 CLI 入口(`scripts/main.py`),并补齐 `SKILL.md` 的 LLM/运行契约说明与根 `README.md` 的用户市场说明
|
||||||
|
|
||||||
|
## 复制边界:哪些能复制,哪些必须替换
|
||||||
|
|
||||||
|
### 可以直接参考/复制的
|
||||||
|
|
||||||
|
- `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`、`CLAW_SKILLS_ROOT`、`JIANGCHANG_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 入口。
|
||||||
199
examples/real_browser_rpa/scripts/service/account_client.py
Normal file
199
examples/real_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 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 → CLAW_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")
|
||||||
|
|
||||||
|
skills_root = (os.getenv("CLAW_SKILLS_ROOT") or os.getenv("JIANGCHANG_SKILLS_ROOT") or "").strip()
|
||||||
|
if skills_root:
|
||||||
|
candidate = os.path.join(os.path.abspath(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()
|
||||||
15
examples/simulator_api/README.md
Normal file
15
examples/simulator_api/README.md
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
# Simulator API Example
|
||||||
|
|
||||||
|
这个目录用于沉淀“仿真环境 / API”类 skill 的可复制成功案例。
|
||||||
|
|
||||||
|
## 适用场景
|
||||||
|
|
||||||
|
- 使用 mock / sandbox / simulator API
|
||||||
|
- 不需要启动浏览器
|
||||||
|
- 适合演示输入校验、API 调用、结果入库、日志、错误契约和测试
|
||||||
|
|
||||||
|
## 当前状态
|
||||||
|
|
||||||
|
- 本目录暂时只保留结构占位。
|
||||||
|
- 示例业务尚未确定。
|
||||||
|
- 本轮不包含示例代码。
|
||||||
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
125
examples/simulator_browser_rpa/README.md
Normal file
125
examples/simulator_browser_rpa/README.md
Normal file
@@ -0,0 +1,125 @@
|
|||||||
|
# Simulator Browser RPA Example
|
||||||
|
|
||||||
|
这是一个**仿真浏览器 RPA** 的可复制参考实现:在可控 sandbox 页面上,通过 Playwright 完成登录、表单填写、弹窗确认、结果解析。
|
||||||
|
|
||||||
|
本示例抽象自已跑通的「批量提交类」浏览器自动化 skill,但已去业务化,不包含真实银行/真实资金语义。
|
||||||
|
|
||||||
|
## 适合什么场景
|
||||||
|
|
||||||
|
- 自有 **仿真系统 / sandbox / training 页面**
|
||||||
|
- 需要 **浏览器 RPA** 做端到端演示或回归
|
||||||
|
- 表单较复杂:多步登录、Tab 切换、动态增行、PIN 弹窗、成功页解析
|
||||||
|
- 需要 **adapter 分层**:mock(CI)与 simulator_rpa(半集成)可切换
|
||||||
|
|
||||||
|
## 不适合什么场景
|
||||||
|
|
||||||
|
- **真实第三方网站 + 高风控 + 人工验证码** → 优先看 [`../real_browser_rpa/README.md`](../real_browser_rpa/README.md)
|
||||||
|
- 目标系统有稳定 **官方 API** → 不要硬写浏览器 RPA,后续参考 `real_api`
|
||||||
|
- **不要把本示例的业务字段、selector、sandbox URL 原样复制**到你的 skill
|
||||||
|
|
||||||
|
## 目录结构
|
||||||
|
|
||||||
|
```
|
||||||
|
simulator_browser_rpa/
|
||||||
|
├── README.md
|
||||||
|
├── sandbox/
|
||||||
|
│ └── demo_app.html # 本地仿真页面(稳定 data-testid / name)
|
||||||
|
├── scripts/
|
||||||
|
│ ├── service/
|
||||||
|
│ │ ├── browser_session.py # 浏览器启动(URL 仅 page.goto)
|
||||||
|
│ │ ├── account_client.py # 账号来源集中封装
|
||||||
|
│ │ ├── task_service.py # 编排层
|
||||||
|
│ │ └── adapter/
|
||||||
|
│ │ ├── base.py # BatchItem / BatchSubmitResult 契约
|
||||||
|
│ │ ├── mock.py # mock/unit 档
|
||||||
|
│ │ ├── simulator_rpa.py
|
||||||
|
│ │ └── __init__.py # select_adapter
|
||||||
|
│ └── util/
|
||||||
|
│ ├── constants.py
|
||||||
|
│ └── logging.py
|
||||||
|
└── tests/
|
||||||
|
├── test_adapter_dispatch.py
|
||||||
|
├── test_simulator_rpa_unit.py
|
||||||
|
└── test_stop_reason.py
|
||||||
|
```
|
||||||
|
|
||||||
|
## 文件职责
|
||||||
|
|
||||||
|
| 文件 | 职责 |
|
||||||
|
|---|---|
|
||||||
|
| `browser_session.py` | 系统 Chrome/Edge + launch args 规范;支持 profile / 无 profile |
|
||||||
|
| `account_client.py` | 示例从环境变量读账号;真实 skill 可在此集中对接 account-manager |
|
||||||
|
| `adapter/base.py` | 数据契约与 `BatchAdapterBase` |
|
||||||
|
| `adapter/mock.py` | 不启浏览器,供 unit/mock/CI |
|
||||||
|
| `adapter/simulator_rpa.py` | 仿真 RPA 主流程 |
|
||||||
|
| `task_service.py` | 校验输入 → 选 adapter → 提交 → 返回 summary |
|
||||||
|
| `sandbox/demo_app.html` | 可控 DOM,配合 selector 稳定 |
|
||||||
|
| `tests/` | dispatch、URL 不进 launch args、失败转结果 |
|
||||||
|
|
||||||
|
## 核心流程
|
||||||
|
|
||||||
|
1. `task_service.run_batch_submit(target, items)` 校验参数
|
||||||
|
2. `select_adapter()` 按 `OPENCLAW_TEST_TARGET` 选择 mock 或 simulator RPA
|
||||||
|
3. simulator 档:`pick_simulator_account()` → `set_credentials()` → `submit_batch()`
|
||||||
|
4. RPA:登录两步 → 批量页 → 填表 → 提交 → PIN → 解析 `batch_id`
|
||||||
|
5. 失败截图(有 `artifacts_dir` 时)→ 返回 `BatchSubmitResult`
|
||||||
|
|
||||||
|
## 复制到新 skill 时怎么做
|
||||||
|
|
||||||
|
**保留:**
|
||||||
|
|
||||||
|
- adapter 分层(base / mock / simulator_rpa / `select_adapter`)
|
||||||
|
- `browser_session.py` 启动规范(**URL 不进 launch args**)
|
||||||
|
- `account_client.py` 集中封装模式
|
||||||
|
- 关键节点结构化日志 + 失败截图
|
||||||
|
- 测试:dispatch、launch args、错误转 `BatchSubmitResult`
|
||||||
|
|
||||||
|
**替换:**
|
||||||
|
|
||||||
|
- `BatchItem` 字段与校验规则
|
||||||
|
- `sandbox/demo_app.html` 或 `SIMULATOR_BASE_URL` 指向的页面
|
||||||
|
- 全部 DOM selector(必须 F12 实测)
|
||||||
|
- `task_service.py` 输入输出与 DB/任务日志
|
||||||
|
- mock 返回字段、成功页 `batch_id` 解析逻辑
|
||||||
|
|
||||||
|
## 明确禁止
|
||||||
|
|
||||||
|
- 不要复制 `__pycache__`、`.pytest_cache`、`artifacts/`、`recordings/` 等生成物
|
||||||
|
- **不要在技能侧**执行 `playwright install` / `pip install playwright`(Python 包由宿主共享 runtime 提供)
|
||||||
|
- **不要把 URL 放进** `launch_persistent_context(args=[...])`
|
||||||
|
- **不要在** `task_service.py` / RPA 主流程里散落 account-manager subprocess
|
||||||
|
- **不要把真实生产系统**配置到 `simulator_rpa` 档位
|
||||||
|
- 不要照抄 slug、logger 名、演示账号到生产 skill
|
||||||
|
|
||||||
|
## 依赖说明
|
||||||
|
|
||||||
|
- Python 3.10+
|
||||||
|
- Python 包 `playwright`:由**宿主共享 runtime** 提供;技能侧不要写入 `requirements.txt`,不要 `pip install playwright`
|
||||||
|
- 默认使用**系统 Chrome/Edge**,不使用 Playwright 内置 Chromium
|
||||||
|
- 技能侧不要执行 `playwright install` / `playwright install chromium`
|
||||||
|
- 本地独立开发若缺 Playwright Python 包,仅作开发机临时补装;生产由宿主解决
|
||||||
|
- `jiangchang_skill_core.runtime_env.find_chrome_executable`(可选,用于查找系统浏览器)
|
||||||
|
|
||||||
|
## 环境变量(示例)
|
||||||
|
|
||||||
|
| 变量 | 说明 |
|
||||||
|
|---|---|
|
||||||
|
| `OPENCLAW_TEST_TARGET` | `mock`/`unit` → Mock;默认 → Simulator RPA |
|
||||||
|
| `SIMULATOR_BASE_URL` | 仿真页 URL;默认本地 `sandbox/demo_app.html` 的 file URI |
|
||||||
|
| `SIMULATOR_LOGIN_ID` / `PASSWORD` / `TOKEN_PIN` | 演示账号 |
|
||||||
|
| `SIMULATOR_PROFILE_DIR` | 可选 persistent profile |
|
||||||
|
| `OPENCLAW_BROWSER_HEADLESS` | `0` 默认有头;`1` 无头模式(CI 可用) |
|
||||||
|
|
||||||
|
## 运行测试
|
||||||
|
|
||||||
|
```bash
|
||||||
|
cd examples/simulator_browser_rpa
|
||||||
|
python -m pytest tests/ -v
|
||||||
|
```
|
||||||
|
|
||||||
|
测试为 mock/纯函数,**不需要启动真实浏览器**。
|
||||||
|
|
||||||
|
## 当前状态
|
||||||
|
|
||||||
|
- 示例代码与 sandbox 页面已就绪,可作为仿真浏览器 RPA 复制起点。
|
||||||
|
- 未接入完整 CLI、数据库与 entitlement。
|
||||||
141
examples/simulator_browser_rpa/sandbox/demo_app.html
Normal file
141
examples/simulator_browser_rpa/sandbox/demo_app.html
Normal file
@@ -0,0 +1,141 @@
|
|||||||
|
<!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="login-step1-section">
|
||||||
|
<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.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>
|
||||||
@@ -0,0 +1,40 @@
|
|||||||
|
"""仿真账号来源封装(示例级;真实 skill 可在此集中对接 account-manager)。"""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import os
|
||||||
|
from dataclasses import dataclass
|
||||||
|
from typing import Optional
|
||||||
|
|
||||||
|
from util.constants import (
|
||||||
|
DEFAULT_DEMO_LOGIN_ID,
|
||||||
|
DEFAULT_DEMO_PASSWORD,
|
||||||
|
DEFAULT_DEMO_TOKEN_PIN,
|
||||||
|
ENV_SIMULATOR_LOGIN_ID,
|
||||||
|
ENV_SIMULATOR_PASSWORD,
|
||||||
|
ENV_SIMULATOR_PROFILE_DIR,
|
||||||
|
ENV_SIMULATOR_TOKEN_PIN,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class SimulatorAccount:
|
||||||
|
login_id: str
|
||||||
|
password: str
|
||||||
|
token_pin: str
|
||||||
|
profile_dir: str = ""
|
||||||
|
|
||||||
|
|
||||||
|
def pick_simulator_account() -> SimulatorAccount:
|
||||||
|
"""
|
||||||
|
示例账号来源:优先读环境变量,否则返回内置 demo 账号。
|
||||||
|
|
||||||
|
真实 skill 若依赖 account-manager,应把 subprocess / sibling_bridge 调用
|
||||||
|
集中在本模块,不要散落到 task_service 或 RPA 主流程中。
|
||||||
|
"""
|
||||||
|
return SimulatorAccount(
|
||||||
|
login_id=(os.getenv(ENV_SIMULATOR_LOGIN_ID) or DEFAULT_DEMO_LOGIN_ID).strip(),
|
||||||
|
password=(os.getenv(ENV_SIMULATOR_PASSWORD) or DEFAULT_DEMO_PASSWORD).strip(),
|
||||||
|
token_pin=(os.getenv(ENV_SIMULATOR_TOKEN_PIN) or DEFAULT_DEMO_TOKEN_PIN).strip(),
|
||||||
|
profile_dir=(os.getenv(ENV_SIMULATOR_PROFILE_DIR) or "").strip(),
|
||||||
|
)
|
||||||
@@ -0,0 +1,40 @@
|
|||||||
|
"""adapter 工厂:按 OPENCLAW_TEST_TARGET 选择实现。"""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import logging
|
||||||
|
import os
|
||||||
|
from typing import Optional
|
||||||
|
|
||||||
|
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 = os.environ.get("OPENCLAW_TEST_TARGET", "").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"
|
||||||
|
|
||||||
|
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"
|
||||||
|
|
||||||
|
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,340 @@
|
|||||||
|
"""仿真浏览器 RPA:操作 sandbox/demo_app.html 完成批量提交。"""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import logging
|
||||||
|
import os
|
||||||
|
import random
|
||||||
|
import re
|
||||||
|
import time
|
||||||
|
from typing import List, Optional
|
||||||
|
|
||||||
|
from service.adapter.base import BatchAdapterBase, BatchItem, BatchSubmitResult
|
||||||
|
from service.browser_session import browser_session, find_chrome_executable
|
||||||
|
from util.constants import (
|
||||||
|
DEFAULT_DEMO_CAPTCHA,
|
||||||
|
DEFAULT_DEMO_LOGIN_ID,
|
||||||
|
DEFAULT_DEMO_PASSWORD,
|
||||||
|
DEFAULT_DEMO_TOKEN_PIN,
|
||||||
|
DEFAULT_TIMEOUT_MS,
|
||||||
|
LOGIN_NAVIGATE_TIMEOUT_MS,
|
||||||
|
SUBMIT_NAVIGATE_TIMEOUT_MS,
|
||||||
|
resolve_simulator_base_url,
|
||||||
|
)
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
_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
|
||||||
|
|
||||||
|
|
||||||
|
class SimulatorBrowserRpaAdapter(BatchAdapterBase):
|
||||||
|
name = "simulator_browser_rpa"
|
||||||
|
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
base_url: Optional[str] = None,
|
||||||
|
headless: Optional[bool] = None,
|
||||||
|
artifacts_dir: Optional[str] = None,
|
||||||
|
) -> None:
|
||||||
|
self.base_url = (base_url or resolve_simulator_base_url()).rstrip("/")
|
||||||
|
if headless is None:
|
||||||
|
env = os.environ.get("OPENCLAW_BROWSER_HEADLESS", "").strip().lower()
|
||||||
|
self.headless = env in ("1", "true", "yes")
|
||||||
|
else:
|
||||||
|
self.headless = headless
|
||||||
|
self.artifacts_dir = artifacts_dir
|
||||||
|
|
||||||
|
self._login_id: str = DEFAULT_DEMO_LOGIN_ID
|
||||||
|
self._password: str = DEFAULT_DEMO_PASSWORD
|
||||||
|
self._token_pin: str = DEFAULT_DEMO_TOKEN_PIN
|
||||||
|
self._profile_dir: Optional[str] = None
|
||||||
|
|
||||||
|
def set_credentials(
|
||||||
|
self,
|
||||||
|
login_id: str,
|
||||||
|
password: str,
|
||||||
|
token_pin: str,
|
||||||
|
profile_dir: str,
|
||||||
|
) -> None:
|
||||||
|
self._login_id = login_id
|
||||||
|
self._password = password
|
||||||
|
self._token_pin = token_pin
|
||||||
|
stripped = (profile_dir or "").strip()
|
||||||
|
self._profile_dir = stripped or None
|
||||||
|
|
||||||
|
def submit_batch(self, target: str, items: List[BatchItem]) -> BatchSubmitResult:
|
||||||
|
try:
|
||||||
|
from playwright.sync_api import sync_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},
|
||||||
|
)
|
||||||
|
|
||||||
|
start_url = self.base_url
|
||||||
|
try:
|
||||||
|
with browser_session(
|
||||||
|
start_url,
|
||||||
|
profile_dir=self._profile_dir,
|
||||||
|
headless=self.headless,
|
||||||
|
) as page:
|
||||||
|
try:
|
||||||
|
logger.info("rpa_login_start url=%s", start_url)
|
||||||
|
self._login(page)
|
||||||
|
logger.info("rpa_login_done")
|
||||||
|
|
||||||
|
self._goto_batch_page(page)
|
||||||
|
logger.info("rpa_batch_page_ready")
|
||||||
|
|
||||||
|
self._fill_batch_form(page, target, items)
|
||||||
|
logger.info("rpa_batch_form_filled rows=%s", len(items))
|
||||||
|
|
||||||
|
batch_id = self._submit_and_get_batch_id(page)
|
||||||
|
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": self.name},
|
||||||
|
)
|
||||||
|
except RpaError as exc:
|
||||||
|
logger.error("rpa_failed: %s", exc)
|
||||||
|
arts = {"adapter": self.name}
|
||||||
|
if exc.screenshot_path:
|
||||||
|
arts["screenshot"] = exc.screenshot_path
|
||||||
|
return BatchSubmitResult(
|
||||||
|
ok=False,
|
||||||
|
batch_id=None,
|
||||||
|
submitted_count=0,
|
||||||
|
submitted_amount=0.0,
|
||||||
|
error_msg=str(exc),
|
||||||
|
artifacts=arts,
|
||||||
|
)
|
||||||
|
except Exception as exc:
|
||||||
|
sp = self._safe_screenshot(page, "unexpected_error")
|
||||||
|
logger.exception("rpa_unexpected: %s", exc)
|
||||||
|
arts = {"adapter": self.name}
|
||||||
|
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,
|
||||||
|
)
|
||||||
|
except Exception as outer:
|
||||||
|
return BatchSubmitResult(
|
||||||
|
ok=False,
|
||||||
|
batch_id=None,
|
||||||
|
submitted_count=0,
|
||||||
|
submitted_amount=0.0,
|
||||||
|
error_msg=f"启动浏览器失败:{outer}",
|
||||||
|
artifacts={"adapter": self.name},
|
||||||
|
)
|
||||||
|
|
||||||
|
def _pause(self, min_ms: int = _PAUSE_MIN_MS, max_ms: int = _PAUSE_MAX_MS) -> None:
|
||||||
|
time.sleep(random.uniform(min_ms, max_ms) / 1000.0)
|
||||||
|
|
||||||
|
def _login(self, page) -> None:
|
||||||
|
try:
|
||||||
|
page.wait_for_selector('form[name="simLoginStep1"]', timeout=LOGIN_NAVIGATE_TIMEOUT_MS)
|
||||||
|
self._pause()
|
||||||
|
page.fill('form[name="simLoginStep1"] input[name="loginId"]', self._login_id)
|
||||||
|
self._pause()
|
||||||
|
page.click('form[name="simLoginStep1"] button[type="submit"]')
|
||||||
|
except Exception as exc:
|
||||||
|
sp = self._safe_screenshot(page, "login_step1_failed")
|
||||||
|
raise RpaError(f"登录第一步失败:{exc}", screenshot_path=sp) from exc
|
||||||
|
|
||||||
|
try:
|
||||||
|
page.wait_for_selector('form[name="simLoginStep2"]', timeout=DEFAULT_TIMEOUT_MS)
|
||||||
|
self._pause()
|
||||||
|
page.fill('form[name="simLoginStep2"] input[name="password"]', self._password)
|
||||||
|
page.fill('form[name="simLoginStep2"] input[name="captcha"]', DEFAULT_DEMO_CAPTCHA)
|
||||||
|
self._pause()
|
||||||
|
page.click('form[name="simLoginStep2"] button[type="submit"]')
|
||||||
|
except Exception as exc:
|
||||||
|
sp = self._safe_screenshot(page, "login_step2_failed")
|
||||||
|
raise RpaError(f"登录第二步失败:{exc}", screenshot_path=sp) from exc
|
||||||
|
|
||||||
|
try:
|
||||||
|
page.wait_for_selector('form[name="simBatchForm"]', timeout=LOGIN_NAVIGATE_TIMEOUT_MS)
|
||||||
|
except Exception as exc:
|
||||||
|
sp = self._safe_screenshot(page, "login_navigate_failed")
|
||||||
|
raise RpaError(f"登录后未进入批量提交页:{exc}", screenshot_path=sp) from exc
|
||||||
|
|
||||||
|
def _goto_batch_page(self, page) -> None:
|
||||||
|
try:
|
||||||
|
page.wait_for_selector('form[name="simBatchForm"]', timeout=DEFAULT_TIMEOUT_MS)
|
||||||
|
self._pause()
|
||||||
|
except Exception as exc:
|
||||||
|
sp = self._safe_screenshot(page, "goto_batch_failed")
|
||||||
|
raise RpaError(f"进入批量提交页失败:{exc}", screenshot_path=sp) from exc
|
||||||
|
|
||||||
|
def _select_target_account(self, page, target: str) -> None:
|
||||||
|
sel = page.locator('select[name="fromAccountNo"]')
|
||||||
|
sel.wait_for(timeout=DEFAULT_TIMEOUT_MS)
|
||||||
|
n = 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 = opt.get_attribute("value") or ""
|
||||||
|
if not val.strip():
|
||||||
|
continue
|
||||||
|
label_txt = 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)
|
||||||
|
sel.select_option(value=pick)
|
||||||
|
self._pause()
|
||||||
|
|
||||||
|
def _fill_batch_form(self, page, target: str, items: List[BatchItem]) -> None:
|
||||||
|
try:
|
||||||
|
page.get_by_role("button", name=re.compile(r"页面填写")).click()
|
||||||
|
except Exception as exc:
|
||||||
|
sp = self._safe_screenshot(page, "tab_switch_failed")
|
||||||
|
raise RpaError(f"切换到页面填写失败:{exc}", screenshot_path=sp) from exc
|
||||||
|
|
||||||
|
try:
|
||||||
|
self._select_target_account(page, target)
|
||||||
|
except RpaError:
|
||||||
|
raise
|
||||||
|
except Exception as exc:
|
||||||
|
sp = self._safe_screenshot(page, "select_account_failed")
|
||||||
|
raise RpaError(f"选择来源账户失败:{exc}", screenshot_path=sp) from exc
|
||||||
|
|
||||||
|
try:
|
||||||
|
existing = 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):
|
||||||
|
self._pause(400, 900)
|
||||||
|
add_btn.click()
|
||||||
|
except Exception as exc:
|
||||||
|
sp = self._safe_screenshot(page, "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}"]')
|
||||||
|
row.wait_for(timeout=DEFAULT_TIMEOUT_MS)
|
||||||
|
self._pause(300, 800)
|
||||||
|
row.locator('input[name="name"]').fill(it.name)
|
||||||
|
row.locator('input[name="account"]').fill(it.account)
|
||||||
|
row.locator('input[name="note"]').fill(it.note or "")
|
||||||
|
row.locator('input[name="amount"]').fill(f"{it.amount}")
|
||||||
|
except Exception as exc:
|
||||||
|
sp = self._safe_screenshot(page, f"fill_row_{it.row_index}_failed")
|
||||||
|
raise RpaError(f"填第 {it.row_index} 行失败:{exc}", screenshot_path=sp) from exc
|
||||||
|
|
||||||
|
try:
|
||||||
|
page.fill('input[name="purpose"]', "仿真批量提交示例")
|
||||||
|
except Exception as exc:
|
||||||
|
logger.warning("填写用途失败(非致命):%s", exc)
|
||||||
|
|
||||||
|
def _submit_and_get_batch_id(self, page) -> str:
|
||||||
|
try:
|
||||||
|
self._pause(800, 1500)
|
||||||
|
page.locator('form[name="simBatchForm"] button[type="submit"]').click()
|
||||||
|
except Exception as exc:
|
||||||
|
sp = self._safe_screenshot(page, "click_submit_failed")
|
||||||
|
raise RpaError(f"点击提交失败:{exc}", screenshot_path=sp) from exc
|
||||||
|
|
||||||
|
dlg_sel = 'div[role="dialog"][aria-modal="true"]'
|
||||||
|
try:
|
||||||
|
page.wait_for_selector(dlg_sel, timeout=DEFAULT_TIMEOUT_MS)
|
||||||
|
except Exception as exc:
|
||||||
|
sp = self._safe_screenshot(page, "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"]')
|
||||||
|
self._pause(500, 1200)
|
||||||
|
pin_input.fill(self._token_pin)
|
||||||
|
except Exception as exc:
|
||||||
|
sp = self._safe_screenshot(page, "fill_pin_failed")
|
||||||
|
raise RpaError(f"填写 PIN 失败:{exc}", screenshot_path=sp) from exc
|
||||||
|
|
||||||
|
try:
|
||||||
|
page.locator(dlg_sel).get_by_role("button", name=re.compile(r"确认提交")).click()
|
||||||
|
except Exception as exc:
|
||||||
|
sp = self._safe_screenshot(page, "click_confirm_failed")
|
||||||
|
raise RpaError(f"确认提交失败:{exc}", screenshot_path=sp) from exc
|
||||||
|
|
||||||
|
try:
|
||||||
|
page.wait_for_selector('[data-testid="batch-id"]', timeout=SUBMIT_NAVIGATE_TIMEOUT_MS)
|
||||||
|
except Exception as exc:
|
||||||
|
sp = self._safe_screenshot(page, "success_area_not_visible")
|
||||||
|
raise RpaError(f"提交后未出现成功区域:{exc}", screenshot_path=sp) from exc
|
||||||
|
|
||||||
|
batch_id = page.locator('[data-testid="batch-id"]').get_attribute("data-batch-id") or ""
|
||||||
|
if not batch_id:
|
||||||
|
batch_id = 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 = self._safe_screenshot(page, "batch_id_empty")
|
||||||
|
raise RpaError(f"无法解析 batch_id,url={url}", screenshot_path=sp)
|
||||||
|
return batch_id
|
||||||
|
|
||||||
|
def _safe_screenshot(self, page, tag: str) -> Optional[str]:
|
||||||
|
if not self.artifacts_dir:
|
||||||
|
return None
|
||||||
|
try:
|
||||||
|
os.makedirs(self.artifacts_dir, exist_ok=True)
|
||||||
|
path = os.path.join(self.artifacts_dir, f"{tag}_{int(time.time())}.png")
|
||||||
|
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
|
||||||
@@ -0,0 +1,138 @@
|
|||||||
|
"""浏览器会话启动(系统 Chrome/Edge;URL 仅通过 page.goto 打开)。"""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import logging
|
||||||
|
import os
|
||||||
|
from contextlib import contextmanager
|
||||||
|
from typing import Any, Generator, Iterator, List, Optional, Tuple
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
DEFAULT_PAGE_TIMEOUT_MS = 15_000
|
||||||
|
GOTO_TIMEOUT_MS = 60_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
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
|
|
||||||
|
def _clear_node_options() -> None:
|
||||||
|
os.environ.pop("NODE_OPTIONS", None)
|
||||||
|
|
||||||
|
|
||||||
|
@contextmanager
|
||||||
|
def browser_session(
|
||||||
|
start_url: str,
|
||||||
|
*,
|
||||||
|
profile_dir: Optional[str] = None,
|
||||||
|
headless: Optional[bool] = None,
|
||||||
|
) -> Generator[Any, None, None]:
|
||||||
|
"""
|
||||||
|
同步 Playwright 浏览器会话。
|
||||||
|
|
||||||
|
- Playwright Python 包由宿主共享 runtime 提供;技能侧不要 playwright install。
|
||||||
|
- launch args 仅放 Chrome 参数,不含 URL。
|
||||||
|
- 流程:launch → new_page → goto(start_url)
|
||||||
|
"""
|
||||||
|
from playwright.sync_api import sync_playwright
|
||||||
|
|
||||||
|
_clear_node_options()
|
||||||
|
hl = headless if headless is not None else _headless_from_env()
|
||||||
|
chrome = _find_chrome_executable()
|
||||||
|
if not chrome:
|
||||||
|
raise RuntimeError(
|
||||||
|
"未检测到 Chrome 或 Edge 浏览器,请安装系统浏览器后重试。"
|
||||||
|
)
|
||||||
|
|
||||||
|
args = chrome_launch_args()
|
||||||
|
logger.info(
|
||||||
|
"browser_launch chrome=%s profile_dir=%s start_url=%s args=%s headless=%s",
|
||||||
|
chrome,
|
||||||
|
profile_dir or "",
|
||||||
|
start_url,
|
||||||
|
args,
|
||||||
|
hl,
|
||||||
|
)
|
||||||
|
|
||||||
|
browser_holder = None
|
||||||
|
context = None
|
||||||
|
with sync_playwright() as pw:
|
||||||
|
try:
|
||||||
|
if profile_dir:
|
||||||
|
context = 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"],
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
browser_holder = pw.chromium.launch(
|
||||||
|
headless=hl,
|
||||||
|
executable_path=chrome,
|
||||||
|
args=args,
|
||||||
|
ignore_default_args=["--enable-automation"],
|
||||||
|
)
|
||||||
|
context = browser_holder.new_context(no_viewport=True, locale="zh-CN")
|
||||||
|
|
||||||
|
context.add_init_script(STEALTH_INIT_SCRIPT)
|
||||||
|
page = context.new_page()
|
||||||
|
page.set_default_timeout(DEFAULT_PAGE_TIMEOUT_MS)
|
||||||
|
page.goto(start_url, wait_until="domcontentloaded", timeout=GOTO_TIMEOUT_MS)
|
||||||
|
yield page
|
||||||
|
finally:
|
||||||
|
try:
|
||||||
|
if context is not None:
|
||||||
|
context.close()
|
||||||
|
except Exception as exc:
|
||||||
|
logger.warning("browser_context_close_failed: %s", exc)
|
||||||
|
if browser_holder is not None:
|
||||||
|
try:
|
||||||
|
browser_holder.close()
|
||||||
|
except Exception as exc:
|
||||||
|
logger.warning("browser_close_failed: %s", exc)
|
||||||
|
|
||||||
|
|
||||||
|
def find_chrome_executable() -> str | None:
|
||||||
|
return _find_chrome_executable()
|
||||||
104
examples/simulator_browser_rpa/scripts/service/task_service.py
Normal file
104
examples/simulator_browser_rpa/scripts/service/task_service.py
Normal file
@@ -0,0 +1,104 @@
|
|||||||
|
"""轻量任务编排:校验输入 → 选 adapter → 提交批次。"""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import os
|
||||||
|
import tempfile
|
||||||
|
import uuid
|
||||||
|
from typing import Any, Dict, List, Optional
|
||||||
|
|
||||||
|
from service.account_client import pick_simulator_account
|
||||||
|
from service.adapter import BatchItem, SimulatorBrowserRpaAdapter, 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
|
||||||
|
|
||||||
|
|
||||||
|
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 等。
|
||||||
|
"""
|
||||||
|
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)
|
||||||
|
if isinstance(adapter, SimulatorBrowserRpaAdapter):
|
||||||
|
account = pick_simulator_account()
|
||||||
|
adapter.set_credentials(
|
||||||
|
account.login_id,
|
||||||
|
account.password,
|
||||||
|
account.token_pin,
|
||||||
|
account.profile_dir,
|
||||||
|
)
|
||||||
|
|
||||||
|
result = 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
|
||||||
34
examples/simulator_browser_rpa/scripts/util/constants.py
Normal file
34
examples/simulator_browser_rpa/scripts/util/constants.py
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
"""仿真浏览器 RPA 示例常量。"""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import os
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
SKILL_SLUG = "example-simulator-browser-rpa"
|
||||||
|
LOG_LOGGER_NAME = "openclaw.skill.example_simulator_browser_rpa"
|
||||||
|
|
||||||
|
DEFAULT_TIMEOUT_MS = 15_000
|
||||||
|
LOGIN_NAVIGATE_TIMEOUT_MS = 30_000
|
||||||
|
SUBMIT_NAVIGATE_TIMEOUT_MS = 60_000
|
||||||
|
|
||||||
|
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_LOGIN_ID = "SIMULATOR_LOGIN_ID"
|
||||||
|
ENV_SIMULATOR_PASSWORD = "SIMULATOR_PASSWORD"
|
||||||
|
ENV_SIMULATOR_TOKEN_PIN = "SIMULATOR_TOKEN_PIN"
|
||||||
|
ENV_SIMULATOR_PROFILE_DIR = "SIMULATOR_PROFILE_DIR"
|
||||||
|
|
||||||
|
_EXAMPLE_ROOT = Path(__file__).resolve().parents[2]
|
||||||
|
DEFAULT_DEMO_APP_PATH = _EXAMPLE_ROOT / "sandbox" / "demo_app.html"
|
||||||
|
|
||||||
|
|
||||||
|
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()
|
||||||
15
examples/simulator_browser_rpa/scripts/util/logging.py
Normal file
15
examples/simulator_browser_rpa/scripts/util/logging.py
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
"""示例日志工具。"""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import logging
|
||||||
|
|
||||||
|
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")
|
||||||
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.TestCase):
|
||||||
|
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 = 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-"))
|
||||||
|
|
||||||
|
def test_mock_empty_items(self) -> None:
|
||||||
|
result = MockBatchAdapter().submit_batch("acct-demo-001", [])
|
||||||
|
self.assertFalse(result.ok)
|
||||||
|
self.assertEqual(result.error_msg, "items 为空")
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
unittest.main()
|
||||||
@@ -0,0 +1,76 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
"""SimulatorBrowserRpaAdapter 单元测试(不启动真实浏览器)。"""
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import unittest
|
||||||
|
from unittest.mock import MagicMock, patch
|
||||||
|
|
||||||
|
from service.adapter import BatchItem, SimulatorBrowserRpaAdapter
|
||||||
|
from service.browser_session import CHROME_LAUNCH_ARGS
|
||||||
|
|
||||||
|
|
||||||
|
class TestSimulatorRpaUnit(unittest.TestCase):
|
||||||
|
def test_set_credentials_and_empty_profile_dir(self) -> None:
|
||||||
|
adapter = SimulatorBrowserRpaAdapter()
|
||||||
|
adapter.set_credentials("demo001", "pwd", "pin", " ")
|
||||||
|
self.assertEqual(adapter._login_id, "demo001")
|
||||||
|
self.assertIsNone(adapter._profile_dir)
|
||||||
|
|
||||||
|
def test_submit_batch_empty_items(self) -> None:
|
||||||
|
adapter = SimulatorBrowserRpaAdapter()
|
||||||
|
result = 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)
|
||||||
|
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 = 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.browser_session")
|
||||||
|
@patch("service.adapter.simulator_rpa.find_chrome_executable", return_value="C:/Chrome/chrome.exe")
|
||||||
|
def test_start_url_opened_via_goto_not_launch_args(
|
||||||
|
self,
|
||||||
|
_chrome: MagicMock,
|
||||||
|
mock_session: MagicMock,
|
||||||
|
) -> None:
|
||||||
|
from service.adapter.simulator_rpa import RpaError
|
||||||
|
|
||||||
|
adapter = SimulatorBrowserRpaAdapter(base_url="file:///demo/demo_app.html")
|
||||||
|
items = [BatchItem(row_index=1, name="A", account="1", amount=1.0)]
|
||||||
|
|
||||||
|
page = MagicMock()
|
||||||
|
mock_cm = MagicMock()
|
||||||
|
mock_cm.__enter__.return_value = page
|
||||||
|
mock_cm.__exit__.return_value = False
|
||||||
|
mock_session.return_value = mock_cm
|
||||||
|
|
||||||
|
with patch.object(adapter, "_login", side_effect=RpaError("login fail")):
|
||||||
|
result = adapter.submit_batch("acct-demo-001", items)
|
||||||
|
|
||||||
|
self.assertFalse(result.ok)
|
||||||
|
mock_session.assert_called_once()
|
||||||
|
called_start_url = mock_session.call_args[0][0]
|
||||||
|
self.assertEqual(called_start_url, "file:///demo/demo_app.html")
|
||||||
|
for arg in CHROME_LAUNCH_ARGS:
|
||||||
|
self.assertNotIn(arg, called_start_url)
|
||||||
|
|
||||||
|
def test_safe_screenshot_without_artifacts_dir(self) -> None:
|
||||||
|
adapter = SimulatorBrowserRpaAdapter(artifacts_dir=None)
|
||||||
|
page = MagicMock()
|
||||||
|
self.assertIsNone(adapter._safe_screenshot(page, "tag"))
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
unittest.main()
|
||||||
55
examples/simulator_browser_rpa/tests/test_stop_reason.py
Normal file
55
examples/simulator_browser_rpa/tests/test_stop_reason.py
Normal file
@@ -0,0 +1,55 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
"""RPA 失败结果与 artifacts 单元测试。"""
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import unittest
|
||||||
|
from unittest.mock import MagicMock, patch
|
||||||
|
|
||||||
|
from service.adapter import BatchItem, SimulatorBrowserRpaAdapter
|
||||||
|
from service.adapter.simulator_rpa import RpaError
|
||||||
|
|
||||||
|
|
||||||
|
class TestBatchSubmitFailureResult(unittest.TestCase):
|
||||||
|
@patch("service.adapter.simulator_rpa.find_chrome_executable", return_value="C:/Chrome/chrome.exe")
|
||||||
|
@patch("service.adapter.simulator_rpa.browser_session")
|
||||||
|
def test_rpa_error_converts_to_failed_result(self, mock_session: MagicMock, _chrome: MagicMock) -> None:
|
||||||
|
page = MagicMock()
|
||||||
|
mock_cm = MagicMock()
|
||||||
|
mock_cm.__enter__.return_value = page
|
||||||
|
mock_cm.__exit__.return_value = False
|
||||||
|
mock_session.return_value = mock_cm
|
||||||
|
|
||||||
|
adapter = SimulatorBrowserRpaAdapter(artifacts_dir="/tmp/art")
|
||||||
|
items = [BatchItem(row_index=1, name="A", account="1", amount=1.0)]
|
||||||
|
|
||||||
|
with patch.object(adapter, "_login", side_effect=RpaError("登录第一步失败:timeout", screenshot_path="/tmp/art/x.png")):
|
||||||
|
result = adapter.submit_batch("acct-demo-001", items)
|
||||||
|
|
||||||
|
self.assertFalse(result.ok)
|
||||||
|
self.assertIn("登录第一步失败", result.error_msg or "")
|
||||||
|
self.assertEqual(result.artifacts.get("adapter"), "simulator_browser_rpa")
|
||||||
|
self.assertEqual(result.artifacts.get("screenshot"), "/tmp/art/x.png")
|
||||||
|
|
||||||
|
@patch("service.adapter.simulator_rpa.find_chrome_executable", return_value="C:/Chrome/chrome.exe")
|
||||||
|
@patch("service.adapter.simulator_rpa.browser_session")
|
||||||
|
def test_unexpected_error_includes_adapter_name(self, mock_session: MagicMock, _chrome: MagicMock) -> None:
|
||||||
|
page = MagicMock()
|
||||||
|
mock_cm = MagicMock()
|
||||||
|
mock_cm.__enter__.return_value = page
|
||||||
|
mock_cm.__exit__.return_value = False
|
||||||
|
mock_session.return_value = mock_cm
|
||||||
|
|
||||||
|
adapter = SimulatorBrowserRpaAdapter(artifacts_dir=None)
|
||||||
|
items = [BatchItem(row_index=1, name="A", account="1", amount=1.0)]
|
||||||
|
|
||||||
|
with patch.object(adapter, "_login", side_effect=RuntimeError("boom")):
|
||||||
|
result = adapter.submit_batch("acct-demo-001", items)
|
||||||
|
|
||||||
|
self.assertFalse(result.ok)
|
||||||
|
self.assertIn("未预期异常", result.error_msg or "")
|
||||||
|
self.assertEqual(result.artifacts.get("adapter"), "simulator_browser_rpa")
|
||||||
|
self.assertNotIn("screenshot", result.artifacts)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
unittest.main()
|
||||||
8
pytest.ini
Normal file
8
pytest.ini
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
[pytest]
|
||||||
|
testpaths = tests
|
||||||
|
python_files = test_*.py *_test.py
|
||||||
|
python_classes = Test*
|
||||||
|
python_functions = test_*
|
||||||
|
norecursedirs = .git __pycache__ .pytest_cache integration desktop samples fixtures artifacts diagnostics
|
||||||
|
filterwarnings =
|
||||||
|
ignore::DeprecationWarning
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
# skill-template CLI 模板
|
# 通用业务技能模板 CLI 标准
|
||||||
|
|
||||||
将 `{baseDir}` 替换为技能根目录(含 `SKILL.md`、`scripts/` 的目录)。所有命令通过 `python {baseDir}/scripts/main.py` 调用。
|
将 `{baseDir}` 替换为技能根目录(含 `SKILL.md`、`scripts/` 的目录)。所有命令通过 `python {baseDir}/scripts/main.py` 调用。
|
||||||
|
|
||||||
@@ -6,31 +6,82 @@
|
|||||||
|
|
||||||
```bash
|
```bash
|
||||||
python {baseDir}/scripts/main.py health
|
python {baseDir}/scripts/main.py health
|
||||||
|
python {baseDir}/scripts/main.py config-path
|
||||||
python {baseDir}/scripts/main.py version
|
python {baseDir}/scripts/main.py version
|
||||||
```
|
```
|
||||||
|
|
||||||
## 若你的技能是发布型
|
## 标准行为
|
||||||
|
|
||||||
建议继续扩展这些子命令:
|
- **任意 CLI 启动前**执行 `bootstrap_skill_config()`(`main.py` 与 `cli.app.main()` 均触发)。
|
||||||
|
- **`health`**:只读 runtime 诊断,**不下载、不修复 media-assets,不执行业务动作**;不输出敏感值。
|
||||||
|
- **`config-path`**:输出 JSON,包含 `skill`、`env_path`、`example_path`。
|
||||||
|
- **`version`**:输出 JSON(`version`、`skill`)。
|
||||||
|
- **`run`**:长时间无 stdout **不代表卡死**;应通过 `logs` / `log-get` 排查。
|
||||||
|
- **任务完成后**若有 video artifact,CLI 应打印录屏路径、录屏日志、视频/音频诊断(见 `task_run_support._print_video_summary`)。
|
||||||
|
|
||||||
|
## config-path(配置路径)
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"skill": "your-skill-slug",
|
||||||
|
"env_path": "{DATA_ROOT}/{USER_ID}/your-skill-slug/.env",
|
||||||
|
"example_path": "{skill_root}/.env.example"
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## health 标准输出(runtime diagnostics)
|
||||||
|
|
||||||
|
`health` 委托 `jiangchang_skill_core.collect_runtime_diagnostics`,典型字段:
|
||||||
|
|
||||||
|
- `python_executable` — 当前 Python 解释器路径
|
||||||
|
- `platform_kit_version` / `platform_kit_min_version` / `platform_kit_version_ok`
|
||||||
|
- `jiangchang_skill_core_file` — 公共库加载来源(若来自技能目录副本会报 warning)
|
||||||
|
- `media_assets_root`、`ffmpeg_path`、`ffmpeg_available`、`background_music_mp3_count`
|
||||||
|
- `runtime_issue[warning|error]` — 非致命/致命问题列表
|
||||||
|
- `env_path` / `env_exists` / `example_path` — 用户 `.env` 与仓库模板路径
|
||||||
|
|
||||||
|
## 通用业务命令骨架
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
python {baseDir}/scripts/main.py publish
|
python {baseDir}/scripts/main.py run
|
||||||
python {baseDir}/scripts/main.py publish -a <账号id> -i <文章id>
|
python {baseDir}/scripts/main.py run -t <target> -i <input_id>
|
||||||
python {baseDir}/scripts/main.py logs
|
python {baseDir}/scripts/main.py logs
|
||||||
|
python {baseDir}/scripts/main.py logs --task-type demo --status failed
|
||||||
python {baseDir}/scripts/main.py log-get <log_id>
|
python {baseDir}/scripts/main.py log-get <log_id>
|
||||||
```
|
```
|
||||||
|
|
||||||
|
| 命令 | 用途 |
|
||||||
|
|------|------|
|
||||||
|
| `run` | 执行一次任务 |
|
||||||
|
| `logs` | 列出最近 N 条任务日志 |
|
||||||
|
| `log-get` | 按 log_id 查看单条任务日志(JSON) |
|
||||||
|
|
||||||
|
## 查询/导出类(可选)
|
||||||
|
|
||||||
|
复制后按需增加,例如:`contacts`、`contact-get`、`export` 或业务等价命令。
|
||||||
|
|
||||||
## 若你的技能依赖兄弟技能
|
## 若你的技能依赖兄弟技能
|
||||||
|
|
||||||
并列技能与 `{baseDir}` 同级时,兄弟技能路径写为:
|
使用 `service.sibling_bridge.call_sibling_json(skill_slug, args)`,不要硬编码兄弟技能名到 `sibling_bridge.py` 自身;并在 `SKILL.md` 声明 `dependencies.required`。
|
||||||
|
|
||||||
```bash
|
## 手工排查命令(推荐)
|
||||||
python {baseDir}/../content-manager/scripts/main.py ...
|
|
||||||
python {baseDir}/../account-manager/scripts/main.py ...
|
**建议使用宿主共享 python-runtime**,避免技能目录内临时 venv 加载不到公共库:
|
||||||
|
|
||||||
|
```text
|
||||||
|
Windows:
|
||||||
|
{JIANGCHANG_DATA_ROOT}\python-runtime\.venv\Scripts\python.exe {baseDir}\scripts\main.py health
|
||||||
|
|
||||||
|
通用:
|
||||||
|
<shared-python> {baseDir}/scripts/main.py health
|
||||||
```
|
```
|
||||||
|
|
||||||
|
`<shared-python>` 通常位于 `{JIANGCHANG_DATA_ROOT}/python-runtime/.venv`。
|
||||||
|
|
||||||
|
**不建议**在生产/测试机使用技能目录内 `uv run python`,以免加载不到共享 runtime 中的 `jiangchang-platform-kit` / `playwright`。
|
||||||
|
|
||||||
## 模板约定
|
## 模板约定
|
||||||
|
|
||||||
- 最小模板至少保留 `health` / `version`
|
- 最小模板至少保留 `health` / `config-path` / `version`
|
||||||
- 发布型技能建议使用 `publish` / `logs` / `log-get`
|
- 通用业务技能建议使用 `run` / `logs` / `log-get`
|
||||||
- 不要再用旧模板的 `scripts/skill_main.py`
|
- 不要再用旧模板的 `publish` / `--account-id` / `--article-id` 命名
|
||||||
|
|||||||
@@ -1,30 +1,13 @@
|
|||||||
---
|
# Agent 参考索引
|
||||||
description: "这是规范化的新技能模板说明,不直接作为业务技能使用。复制后请替换技能名、平台名、CLI 示例与 service 实现。"
|
|
||||||
---
|
|
||||||
|
|
||||||
# 技能模板说明
|
本目录供 Agent **运行、编排、调用** skill 时渐进式加载,不是用户市场说明。
|
||||||
|
|
||||||
这个仓库是**给开发者复制的新技能模板**,不是终端用户直接调用的业务 skill。
|
| 文档 | 用途 |
|
||||||
|
|------|------|
|
||||||
|
| 根目录 [`README.md`](../README.md) | 用户市场详情页说明 |
|
||||||
|
| [`SKILL.md`](../SKILL.md) | LLM / OpenClaw 平台技能入口 |
|
||||||
|
| [`CLI.md`](CLI.md) | 命令行入口、参数与调用契约 |
|
||||||
|
| [`SCHEMA.md`](SCHEMA.md) | 输入输出、数据库与字段说明 |
|
||||||
|
| [`../development/`](../development/) | 开发、测试、技术规范(给开发者 / AI 编程代理) |
|
||||||
|
|
||||||
## 它提供什么
|
按需阅读:编排任务时优先 `CLI.md`;解析结构化字段时读 `SCHEMA.md`;运行时环境细节见 `../development/RUNTIME.md`。
|
||||||
|
|
||||||
- 标准目录结构
|
|
||||||
- 最小 CLI 入口
|
|
||||||
- 发布型技能常见的日志表骨架
|
|
||||||
- `service` 层占位模块
|
|
||||||
- 与现有规范 skill 一致的发布脚本与 GitHub workflow
|
|
||||||
|
|
||||||
## 复制后你需要改什么
|
|
||||||
|
|
||||||
- `SKILL.md` 中的名称、描述、slug、触发说明
|
|
||||||
- `references/CLI.md` 里的命令示例
|
|
||||||
- `scripts/util/constants.py` 中的 slug / 版本 / logger 名
|
|
||||||
- `scripts/service/` 下的真实业务实现
|
|
||||||
|
|
||||||
## 不建议再保留的旧结构
|
|
||||||
|
|
||||||
- 旧模板里的 `docs/`
|
|
||||||
- 旧模板里的 `optional/`
|
|
||||||
- 旧入口 `scripts/skill_main.py`
|
|
||||||
|
|
||||||
新模板统一使用 `scripts/main.py` 作为入口。
|
|
||||||
|
|||||||
@@ -1,338 +0,0 @@
|
|||||||
# 技能需求文档模板
|
|
||||||
|
|
||||||
这份文档用于给技术人员、产品人员和实施人员统一描述某个 skill 的研发需求。
|
|
||||||
|
|
||||||
建议原则:
|
|
||||||
|
|
||||||
- 一个 skill 对应一份主需求文档
|
|
||||||
- 需求先写清楚,再进入开发
|
|
||||||
- 文档描述“要做什么”和“做到什么程度”,不要在这里堆实现细节
|
|
||||||
- 实现方式、代码结构、发布流程,分别放到 `DEVELOPMENT.md`、`CLI.md`、`SCHEMA.md`、`RUNTIME.md`
|
|
||||||
|
|
||||||
如果你是从 `skill-template` 复制新技能,请复制本文件后,把下面所有占位内容替换成你的真实项目内容。
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
# REQUIREMENTS
|
|
||||||
|
|
||||||
## 1. 文档目标
|
|
||||||
|
|
||||||
说明这份需求文档的作用,以及它解决什么问题。
|
|
||||||
|
|
||||||
模板写法:
|
|
||||||
|
|
||||||
- 本文档用于明确 `【技能名称】` 的研发范围、交付标准、依赖关系和验收要求。
|
|
||||||
- 技术人员应以本文件作为开发范围依据,以避免实现偏差、范围蔓延或理解不一致。
|
|
||||||
|
|
||||||
示例:
|
|
||||||
|
|
||||||
- 本文档用于明确 `weibo-publisher` 的研发需求,约束该 skill 的功能边界、输入输出和验收标准,作为开发、测试和上线验证的统一依据。
|
|
||||||
|
|
||||||
## 2. 业务背景
|
|
||||||
|
|
||||||
说明为什么要做这个 skill,它服务什么业务场景,解决什么实际问题。
|
|
||||||
|
|
||||||
模板写法:
|
|
||||||
|
|
||||||
- 当前业务场景中,用户需要在 `【目标平台】` 完成 `【业务动作】`。
|
|
||||||
- 现有流程主要依赖人工处理,存在 `【效率低 / 容易出错 / 不可批量 / 不可追踪】` 等问题。
|
|
||||||
- 因此需要开发 `【技能名称】`,将该流程标准化、自动化,并接入匠厂技能体系中。
|
|
||||||
|
|
||||||
示例:
|
|
||||||
|
|
||||||
- 当前内容运营团队需要将内容库中的文章批量发布到微博平台,但人工登录后台、复制文章、填写标题和确认发布的流程效率较低,且不便于统一记录发布历史。
|
|
||||||
- 因此需要开发 `weibo-publisher`,用于把内容库文章按规范发布到微博平台,并记录发布结果,支持后续排查与复用。
|
|
||||||
|
|
||||||
## 3. 开发目标
|
|
||||||
|
|
||||||
说明本次开发希望最终交付什么结果。
|
|
||||||
|
|
||||||
模板写法:
|
|
||||||
|
|
||||||
- 完成 `【技能名称】` 的标准目录结构搭建
|
|
||||||
- 完成 CLI 入口与基础命令
|
|
||||||
- 完成核心业务流程
|
|
||||||
- 完成与兄弟技能或外部系统的必要集成
|
|
||||||
- 完成正式环境发布验证
|
|
||||||
|
|
||||||
示例:
|
|
||||||
|
|
||||||
- 完成 `weibo-publisher` 的标准 skill 结构建设
|
|
||||||
- 提供 `health`、`version`、`publish`、`logs`、`log-get` 等命令
|
|
||||||
- 支持从兄弟技能读取账号与文章,并执行微博发布流程
|
|
||||||
- 支持记录发布日志,并可在正式环境中安装验证
|
|
||||||
|
|
||||||
## 4. 功能范围
|
|
||||||
|
|
||||||
说明这次开发明确要实现哪些功能。
|
|
||||||
|
|
||||||
模板写法:
|
|
||||||
|
|
||||||
- 支持 `【命令1】`
|
|
||||||
- 支持 `【命令2】`
|
|
||||||
- 支持读取 `【数据来源】`
|
|
||||||
- 支持执行 `【核心业务动作】`
|
|
||||||
- 支持记录 `【日志 / 状态 / 结果】`
|
|
||||||
|
|
||||||
建议将功能拆成“必须实现”和“可后续扩展”。
|
|
||||||
|
|
||||||
示例:
|
|
||||||
|
|
||||||
### 必须实现
|
|
||||||
|
|
||||||
- 支持 `python scripts/main.py health`
|
|
||||||
- 支持 `python scripts/main.py version`
|
|
||||||
- 支持 `python scripts/main.py publish`
|
|
||||||
- 支持从 `account-manager` 获取可用账号
|
|
||||||
- 支持从 `content-manager` 获取待发布文章
|
|
||||||
- 支持执行微博后台发布流程
|
|
||||||
- 支持写入发布日志
|
|
||||||
|
|
||||||
### 可后续扩展
|
|
||||||
|
|
||||||
- 支持定时发布
|
|
||||||
- 支持失败自动重试
|
|
||||||
- 支持多账号轮询发布
|
|
||||||
|
|
||||||
## 5. 非功能要求
|
|
||||||
|
|
||||||
说明除功能外,对稳定性、可维护性、目录规范、日志、编码等方面的要求。
|
|
||||||
|
|
||||||
模板写法:
|
|
||||||
|
|
||||||
- 目录结构必须符合当前 skill 模板规范
|
|
||||||
- 入口必须统一为 `scripts/main.py`
|
|
||||||
- 输出格式应尽量机读友好
|
|
||||||
- 错误前缀统一为 `ERROR:`
|
|
||||||
- Windows 环境下需保证 UTF-8 输出兼容
|
|
||||||
- 必须具备基本日志能力
|
|
||||||
|
|
||||||
示例:
|
|
||||||
|
|
||||||
- 项目结构必须对齐 `skill-template`
|
|
||||||
- CLI 入口统一使用 `scripts/main.py`
|
|
||||||
- 关键执行结果应可通过 JSON 或固定文本结构返回
|
|
||||||
- 发布日志必须可追踪
|
|
||||||
- 不允许重新引入旧模板中的 `docs/`、`optional/` 或 `scripts/skill_main.py`
|
|
||||||
|
|
||||||
## 6. 输入输出要求
|
|
||||||
|
|
||||||
说明这个 skill 接收什么输入,产出什么输出。
|
|
||||||
|
|
||||||
模板写法:
|
|
||||||
|
|
||||||
### 输入
|
|
||||||
|
|
||||||
- `【输入参数1】`
|
|
||||||
- `【输入参数2】`
|
|
||||||
- `【依赖系统返回的数据】`
|
|
||||||
|
|
||||||
### 输出
|
|
||||||
|
|
||||||
- `【标准输出】`
|
|
||||||
- `【错误输出】`
|
|
||||||
- `【数据库记录】`
|
|
||||||
- `【日志文件】`
|
|
||||||
|
|
||||||
示例:
|
|
||||||
|
|
||||||
### 输入
|
|
||||||
|
|
||||||
- `account_id`:可选,指定发布账号
|
|
||||||
- `article_id`:可选,指定发布文章
|
|
||||||
- `account-manager` 返回的账号信息
|
|
||||||
- `content-manager` 返回的文章信息
|
|
||||||
|
|
||||||
### 输出
|
|
||||||
|
|
||||||
- 发布成功时返回成功结果
|
|
||||||
- 发布失败时返回 `ERROR:` 或 `FAIL:` 前缀的错误信息
|
|
||||||
- 在本地数据库中写入 `publish_logs`
|
|
||||||
- 在日志目录中写入执行日志
|
|
||||||
|
|
||||||
## 7. 依赖的兄弟技能或外部系统
|
|
||||||
|
|
||||||
说明开发该 skill 时依赖哪些内部技能或外部平台。
|
|
||||||
|
|
||||||
模板写法:
|
|
||||||
|
|
||||||
### 兄弟技能依赖
|
|
||||||
|
|
||||||
- `【skill-a】`:作用是 `【用途】`
|
|
||||||
- `【skill-b】`:作用是 `【用途】`
|
|
||||||
|
|
||||||
### 外部系统依赖
|
|
||||||
|
|
||||||
- `【平台名】`:作用是 `【用途】`
|
|
||||||
- `【浏览器 / API / 第三方服务】`:作用是 `【用途】`
|
|
||||||
|
|
||||||
示例:
|
|
||||||
|
|
||||||
### 兄弟技能依赖
|
|
||||||
|
|
||||||
- `account-manager`:提供账号信息
|
|
||||||
- `content-manager`:提供文章数据
|
|
||||||
|
|
||||||
### 外部系统依赖
|
|
||||||
|
|
||||||
- 微博后台:目标发布平台
|
|
||||||
- Chromium / Chrome / Edge:用于浏览器自动化
|
|
||||||
- Gitea:用于代码托管和发布工作流
|
|
||||||
- 匠厂客户端:用于正式环境安装与验证
|
|
||||||
|
|
||||||
## 8. 不在本次范围内的内容
|
|
||||||
|
|
||||||
这一节非常重要,用来明确本次“不做什么”,避免研发越做越散。
|
|
||||||
|
|
||||||
模板写法:
|
|
||||||
|
|
||||||
- 本次不实现 `【功能A】`
|
|
||||||
- 本次不处理 `【平台B】`
|
|
||||||
- 本次不做 `【高级能力】`
|
|
||||||
- 本次不兼容 `【旧结构 / 旧逻辑】`
|
|
||||||
|
|
||||||
示例:
|
|
||||||
|
|
||||||
- 本次不实现多平台统一发布
|
|
||||||
- 本次不实现定时任务调度
|
|
||||||
- 本次不实现自动重试机制
|
|
||||||
- 本次不兼容旧模板中的历史目录结构
|
|
||||||
|
|
||||||
## 9. 验收标准
|
|
||||||
|
|
||||||
说明什么情况下才算真正开发完成。
|
|
||||||
|
|
||||||
模板写法:
|
|
||||||
|
|
||||||
- 代码结构符合模板规范
|
|
||||||
- 最小命令可运行
|
|
||||||
- 核心命令可运行
|
|
||||||
- 正式环境可安装
|
|
||||||
- 匠厂中可看到最新版本
|
|
||||||
- 可以在新建任务中实际使用
|
|
||||||
|
|
||||||
示例:
|
|
||||||
|
|
||||||
- `health`、`version` 命令执行正常
|
|
||||||
- `publish` 主流程可运行
|
|
||||||
- 发布后 Gitea 工作流成功
|
|
||||||
- 匠厂技能市场能看到最新版本
|
|
||||||
- skill 可以正常安装
|
|
||||||
- 安装后可在“新建任务”中调用
|
|
||||||
|
|
||||||
## 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`
|
|
||||||
4. 发布前回到 `REQUIREMENTS.md` 对照验收标准逐项检查
|
|
||||||
|
|
||||||
## 最小模板示例
|
|
||||||
|
|
||||||
下面给出一个可直接参考的简化版示例:
|
|
||||||
|
|
||||||
```md
|
|
||||||
# REQUIREMENTS
|
|
||||||
|
|
||||||
## 1. 文档目标
|
|
||||||
|
|
||||||
本文档用于明确 `weibo-publisher` 的研发需求、范围和验收标准,作为开发与测试的统一依据。
|
|
||||||
|
|
||||||
## 2. 业务背景
|
|
||||||
|
|
||||||
运营团队需要将内容库中的文章发布到微博平台,现有人工操作效率低,且缺少统一的发布记录。
|
|
||||||
|
|
||||||
## 3. 开发目标
|
|
||||||
|
|
||||||
- 完成 `weibo-publisher` 的标准 skill 结构
|
|
||||||
- 支持文章发布主流程
|
|
||||||
- 支持发布日志记录
|
|
||||||
|
|
||||||
## 4. 功能范围
|
|
||||||
|
|
||||||
- 支持 `health`
|
|
||||||
- 支持 `version`
|
|
||||||
- 支持 `publish`
|
|
||||||
- 支持查询发布日志
|
|
||||||
|
|
||||||
## 5. 非功能要求
|
|
||||||
|
|
||||||
- 入口统一为 `scripts/main.py`
|
|
||||||
- 保持 UTF-8 输出
|
|
||||||
- 结构符合 skill 模板规范
|
|
||||||
|
|
||||||
## 6. 输入输出要求
|
|
||||||
|
|
||||||
### 输入
|
|
||||||
|
|
||||||
- `account_id`
|
|
||||||
- `article_id`
|
|
||||||
|
|
||||||
### 输出
|
|
||||||
|
|
||||||
- 成功结果
|
|
||||||
- 错误信息
|
|
||||||
- 发布日志记录
|
|
||||||
|
|
||||||
## 7. 依赖的兄弟技能或外部系统
|
|
||||||
|
|
||||||
- `account-manager`
|
|
||||||
- `content-manager`
|
|
||||||
- 微博后台
|
|
||||||
|
|
||||||
## 8. 不在本次范围内的内容
|
|
||||||
|
|
||||||
- 不实现定时发布
|
|
||||||
- 不实现自动重试
|
|
||||||
|
|
||||||
## 9. 验收标准
|
|
||||||
|
|
||||||
- 命令可运行
|
|
||||||
- 能完成正式环境安装验证
|
|
||||||
- 能在新建任务中使用
|
|
||||||
|
|
||||||
## 10. 开发注意事项
|
|
||||||
|
|
||||||
- 不修改无关项目
|
|
||||||
- 不引入旧模板结构
|
|
||||||
|
|
||||||
## 11. 变更记录
|
|
||||||
|
|
||||||
| 日期 | 版本 | 变更人 | 变更内容 |
|
|
||||||
|------|------|--------|----------|
|
|
||||||
| 2026-04-13 | v1.0 | 张三 | 初版 |
|
|
||||||
```
|
|
||||||
@@ -1,40 +0,0 @@
|
|||||||
# 运行时约定
|
|
||||||
|
|
||||||
## 目录结构
|
|
||||||
|
|
||||||
新技能建议采用以下根目录结构:
|
|
||||||
|
|
||||||
- `assets/`
|
|
||||||
- `references/`
|
|
||||||
- `scripts/`
|
|
||||||
- `tests/`
|
|
||||||
- `evals/`
|
|
||||||
|
|
||||||
## `scripts/` 分层
|
|
||||||
|
|
||||||
- `scripts/main.py`:唯一 CLI 入口
|
|
||||||
- `scripts/cli/`:参数解析与命令分发
|
|
||||||
- `scripts/db/`:SQLite 或本地持久化层
|
|
||||||
- `scripts/service/`:业务用例与外部交互
|
|
||||||
- `scripts/util/`:通用工具、常量、日志、路径
|
|
||||||
- `scripts/jiangchang_skill_core/`:运行时与统一日志副本
|
|
||||||
|
|
||||||
## 数据路径
|
|
||||||
|
|
||||||
推荐:
|
|
||||||
|
|
||||||
```text
|
|
||||||
{CLAW_DATA_ROOT}/{CLAW_USER_ID}/{skill_slug}/
|
|
||||||
```
|
|
||||||
|
|
||||||
数据库文件建议:
|
|
||||||
|
|
||||||
```text
|
|
||||||
{...}/{skill_slug}.db
|
|
||||||
```
|
|
||||||
|
|
||||||
## 编码与输出
|
|
||||||
|
|
||||||
- Windows 终端建议在 `scripts/main.py` 里做 UTF-8 stdout/stderr 包装
|
|
||||||
- 机读输出优先单行 JSON
|
|
||||||
- 错误前缀建议统一 `ERROR:`
|
|
||||||
@@ -4,20 +4,35 @@
|
|||||||
|
|
||||||
`{DATA_ROOT}/{USER_ID}/your-skill-slug/your-skill-slug.db`
|
`{DATA_ROOT}/{USER_ID}/your-skill-slug/your-skill-slug.db`
|
||||||
|
|
||||||
## 发布型技能推荐日志表
|
## 通用任务日志表(task_logs)
|
||||||
|
|
||||||
| 字段 | 说明 |
|
模板默认建表:
|
||||||
|------|------|
|
|
||||||
| `id` | 自增主键 |
|
| 字段 | 类型 | 说明 |
|
||||||
| `account_id` | 账号 id |
|
|------|------|------|
|
||||||
| `article_id` | 文章 id |
|
| `id` | INTEGER PK | 自增主键 |
|
||||||
| `article_title` | 标题快照 |
|
| `task_type` | TEXT NOT NULL | 任务类型(disburse / reconcile / verify ...) |
|
||||||
| `status` | `published` / `failed` / `require_login` |
|
| `target_id` | TEXT | 任务目标(账号、平台、客户等的 ID) |
|
||||||
| `error_msg` | 错误说明 |
|
| `input_id` | TEXT | 输入对象 ID |
|
||||||
| `created_at` | Unix 时间戳 |
|
| `input_title` | TEXT | 输入对象标题快照 |
|
||||||
| `updated_at` | Unix 时间戳 |
|
| `status` | TEXT NOT NULL | success / failed / partial / require_login 等 |
|
||||||
|
| `error_msg` | TEXT | 错误说明 |
|
||||||
|
| `result_summary` | TEXT | 结果摘要(建议存 JSON 字符串) |
|
||||||
|
| `created_at` | INTEGER | Unix 时间戳 |
|
||||||
|
| `updated_at` | INTEGER | Unix 时间戳 |
|
||||||
|
|
||||||
|
## 不同业务的字段映射建议
|
||||||
|
|
||||||
|
| 业务场景 | task_type | target_id 含义 | input_id 含义 |
|
||||||
|
|---|---|---|---|
|
||||||
|
| 发布类 | publish | 账号 ID | 文章 ID |
|
||||||
|
| 工资代发 | disburse | 付款账户 | 工资表批次 ID |
|
||||||
|
| 对账 | reconcile | 银行 / 平台 | 对账批次 ID |
|
||||||
|
| 发票验真 | verify | 税务地区 | 发票批次 ID |
|
||||||
|
| 报关 | declare | 港口 / 海关 | 报关批次 ID |
|
||||||
|
|
||||||
## 模板原则
|
## 模板原则
|
||||||
|
|
||||||
- 模板不做历史迁移兼容设计
|
- 模板不做历史迁移兼容设计
|
||||||
- 新 skill 直接从当前 schema 起步
|
- 新 skill 直接从当前 schema 起步
|
||||||
|
- 业务有特殊字段时,建议放 `result_summary`(JSON 字符串),不要乱加列
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ Set-StrictMode -Version Latest
|
|||||||
$ErrorActionPreference = "Stop"
|
$ErrorActionPreference = "Stop"
|
||||||
|
|
||||||
$scriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
|
$scriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
|
||||||
$sharedScript = Join-Path $scriptDir "..\jiangchang-platform-kit\tools\release.ps1"
|
$sharedScript = Join-Path $scriptDir "..\..\jiangchang-platform-kit\tools\release.ps1"
|
||||||
$sharedScript = [System.IO.Path]::GetFullPath($sharedScript)
|
$sharedScript = [System.IO.Path]::GetFullPath($sharedScript)
|
||||||
|
|
||||||
if (-not (Test-Path $sharedScript)) {
|
if (-not (Test-Path $sharedScript)) {
|
||||||
|
|||||||
1
requirements.txt
Normal file
1
requirements.txt
Normal file
@@ -0,0 +1 @@
|
|||||||
|
# 公共依赖由宿主共享 runtime 提供;这里只声明技能特有 Python 依赖。
|
||||||
@@ -6,13 +6,15 @@ import argparse
|
|||||||
import sys
|
import sys
|
||||||
from typing import List, Optional
|
from typing import List, Optional
|
||||||
|
|
||||||
from service.publish_service import (
|
from service.task_service import (
|
||||||
|
cmd_config_path,
|
||||||
cmd_health,
|
cmd_health,
|
||||||
cmd_log_get,
|
cmd_log_get,
|
||||||
cmd_logs,
|
cmd_logs,
|
||||||
cmd_publish,
|
cmd_run,
|
||||||
cmd_version,
|
cmd_version,
|
||||||
)
|
)
|
||||||
|
from util.config_bootstrap import bootstrap_skill_config
|
||||||
from util.argparse_zh import ZhArgumentParser
|
from util.argparse_zh import ZhArgumentParser
|
||||||
from util.constants import LOG_LOGGER_NAME, SKILL_SLUG
|
from util.constants import LOG_LOGGER_NAME, SKILL_SLUG
|
||||||
from util.logging_config import get_skill_logger, setup_skill_logging
|
from util.logging_config import get_skill_logger, setup_skill_logging
|
||||||
@@ -25,66 +27,73 @@ def _cli_str_or_none(raw: Optional[str]) -> Optional[str]:
|
|||||||
return v or None
|
return v or None
|
||||||
|
|
||||||
|
|
||||||
def _handle_publish(args: argparse.Namespace) -> int:
|
def _handle_run(args: argparse.Namespace) -> int:
|
||||||
tail = [str(x).strip() for x in (args.publish_tail or []) if str(x).strip()]
|
tail = [str(x).strip() for x in (args.run_tail or []) if str(x).strip()]
|
||||||
if len(tail) > 2:
|
if len(tail) > 2:
|
||||||
print("❌ 参数过多。")
|
print("❌ 参数过多。")
|
||||||
print("用法:python main.py publish [账号id [文章id]] | publish [-a 账号id] [-i 文章id]")
|
print("用法:python main.py run [target [input_id]] | run [-t target] [-i input_id]")
|
||||||
return 1
|
return 1
|
||||||
|
|
||||||
t_acc: Optional[str] = None
|
t_target: Optional[str] = None
|
||||||
t_art: Optional[str] = None
|
t_input: Optional[str] = None
|
||||||
if len(tail) == 2:
|
if len(tail) == 2:
|
||||||
t_acc, t_art = tail[0], tail[1]
|
t_target, t_input = tail[0], tail[1]
|
||||||
elif len(tail) == 1:
|
elif len(tail) == 1:
|
||||||
if tail[0].isdigit():
|
if tail[0].isdigit():
|
||||||
t_art = tail[0]
|
t_input = tail[0]
|
||||||
else:
|
else:
|
||||||
t_acc = tail[0]
|
t_target = tail[0]
|
||||||
|
|
||||||
pick_a = _cli_str_or_none(getattr(args, "account_id", None))
|
pick_t = _cli_str_or_none(getattr(args, "target", None))
|
||||||
pick_i = _cli_str_or_none(getattr(args, "article_id", None))
|
pick_i = _cli_str_or_none(getattr(args, "input_id", None))
|
||||||
acc = pick_a or t_acc
|
target = pick_t or t_target
|
||||||
art = pick_i or t_art
|
input_id = pick_i or t_input
|
||||||
return cmd_publish(account_id=acc, article_id=art)
|
return cmd_run(target=target, input_id=input_id)
|
||||||
|
|
||||||
|
|
||||||
def _print_full_usage() -> None:
|
def _print_full_usage() -> None:
|
||||||
print("模板技能(main.py)可用命令:")
|
print("通用业务技能模板(main.py)可用命令:")
|
||||||
print(" python main.py publish [账号id [文章id]] [-a 账号] [-i 文章id]")
|
print(" python main.py run [target [input_id]] [-t target] [-i input_id]")
|
||||||
print(" python main.py logs [--limit N] [--status s] [--account-id a]")
|
print(" python main.py logs [--limit N] [--status s] [--task-type t] [--target-id tid]")
|
||||||
print(" python main.py log-get <log_id>")
|
print(" python main.py log-get <log_id>")
|
||||||
print(" python main.py health")
|
print(" python main.py health")
|
||||||
|
print(" python main.py config-path")
|
||||||
print(" python main.py version")
|
print(" python main.py version")
|
||||||
|
|
||||||
|
|
||||||
def build_parser() -> ZhArgumentParser:
|
def build_parser() -> ZhArgumentParser:
|
||||||
p = ZhArgumentParser(
|
p = ZhArgumentParser(
|
||||||
prog="main.py",
|
prog="main.py",
|
||||||
description="模板技能:发布命令骨架、日志查询、健康检查、版本输出。",
|
description="通用业务技能模板:任务执行命令骨架、日志查询、健康检查、版本输出。",
|
||||||
formatter_class=argparse.RawDescriptionHelpFormatter,
|
formatter_class=argparse.RawDescriptionHelpFormatter,
|
||||||
)
|
)
|
||||||
sub = p.add_subparsers(dest="cmd", required=True, parser_class=ZhArgumentParser)
|
sub = p.add_subparsers(dest="cmd", required=True, parser_class=ZhArgumentParser)
|
||||||
|
|
||||||
sp = sub.add_parser("publish", help="发布型技能命令骨架")
|
sp = sub.add_parser("run", help="任务执行命令骨架")
|
||||||
sp.add_argument("--account-id", "-a", default=None, metavar="账号id")
|
sp.add_argument("--target", "-t", default=None, metavar="目标")
|
||||||
sp.add_argument("--article-id", "-i", default=None, metavar="文章id")
|
sp.add_argument("--input-id", "-i", default=None, metavar="输入id", dest="input_id")
|
||||||
sp.add_argument("publish_tail", nargs="*", metavar="位置参数")
|
sp.add_argument("run_tail", nargs="*", metavar="位置参数")
|
||||||
sp.set_defaults(handler=_handle_publish)
|
sp.set_defaults(handler=_handle_run)
|
||||||
|
|
||||||
sp = sub.add_parser("logs", help="查看发布记录")
|
sp = sub.add_parser("logs", help="查看任务日志")
|
||||||
sp.add_argument("--limit", type=int, default=10)
|
sp.add_argument("--limit", type=int, default=10)
|
||||||
sp.add_argument("--status", default=None)
|
sp.add_argument("--status", default=None)
|
||||||
sp.add_argument("--account-id", default=None)
|
sp.add_argument("--task-type", default=None, dest="task_type")
|
||||||
sp.set_defaults(handler=lambda a: cmd_logs(limit=a.limit, status=a.status, account_id=a.account_id))
|
sp.add_argument("--target-id", default=None, dest="target_id")
|
||||||
|
sp.set_defaults(handler=lambda a: cmd_logs(
|
||||||
|
limit=a.limit, status=a.status, task_type=a.task_type, target_id=a.target_id
|
||||||
|
))
|
||||||
|
|
||||||
sp = sub.add_parser("log-get", help="按 log_id 查看单条发布记录(JSON)")
|
sp = sub.add_parser("log-get", help="按 log_id 查看单条任务日志(JSON)")
|
||||||
sp.add_argument("log_id")
|
sp.add_argument("log_id")
|
||||||
sp.set_defaults(handler=lambda a: cmd_log_get(a.log_id))
|
sp.set_defaults(handler=lambda a: cmd_log_get(a.log_id))
|
||||||
|
|
||||||
sp = sub.add_parser("health", help="健康检查")
|
sp = sub.add_parser("health", help="健康检查")
|
||||||
sp.set_defaults(handler=lambda _a: cmd_health())
|
sp.set_defaults(handler=lambda _a: cmd_health())
|
||||||
|
|
||||||
|
sp = sub.add_parser("config-path", help="输出用户 .env 与模板路径")
|
||||||
|
sp.set_defaults(handler=lambda _a: cmd_config_path())
|
||||||
|
|
||||||
sp = sub.add_parser("version", help="版本信息(JSON)")
|
sp = sub.add_parser("version", help="版本信息(JSON)")
|
||||||
sp.set_defaults(handler=lambda _a: cmd_version())
|
sp.set_defaults(handler=lambda _a: cmd_version())
|
||||||
return p
|
return p
|
||||||
@@ -92,13 +101,16 @@ def build_parser() -> ZhArgumentParser:
|
|||||||
|
|
||||||
def main(argv: Optional[List[str]] = None) -> int:
|
def main(argv: Optional[List[str]] = None) -> int:
|
||||||
argv = argv if argv is not None else sys.argv[1:]
|
argv = argv if argv is not None else sys.argv[1:]
|
||||||
|
bootstrap_skill_config()
|
||||||
setup_skill_logging(SKILL_SLUG, LOG_LOGGER_NAME)
|
setup_skill_logging(SKILL_SLUG, LOG_LOGGER_NAME)
|
||||||
get_skill_logger().info("cli_start argv=%s", sys.argv)
|
get_skill_logger().info("cli_start argv=%s", sys.argv)
|
||||||
if not argv:
|
if not argv:
|
||||||
_print_full_usage()
|
_print_full_usage()
|
||||||
return 1
|
return 1
|
||||||
if len(argv) == 2 and argv[0] not in {"publish", "logs", "log-get", "health", "version", "-h", "--help"}:
|
if len(argv) == 2 and argv[0] not in {
|
||||||
return cmd_publish(account_id=argv[0], article_id=argv[1])
|
"run", "logs", "log-get", "health", "config-path", "version", "-h", "--help"
|
||||||
|
}:
|
||||||
|
return cmd_run(target=argv[0], input_id=argv[1])
|
||||||
parser = build_parser()
|
parser = build_parser()
|
||||||
args = parser.parse_args(argv)
|
args = parser.parse_args(argv)
|
||||||
return int(args.handler(args))
|
return int(args.handler(args))
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
"""SQLite 连接与日志表迁移模板。"""
|
"""SQLite 连接与任务日志表迁移模板。"""
|
||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
@@ -17,13 +17,15 @@ def init_db() -> None:
|
|||||||
cur = conn.cursor()
|
cur = conn.cursor()
|
||||||
cur.execute(
|
cur.execute(
|
||||||
"""
|
"""
|
||||||
CREATE TABLE IF NOT EXISTS publish_logs (
|
CREATE TABLE IF NOT EXISTS task_logs (
|
||||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||||
account_id TEXT NOT NULL,
|
task_type TEXT NOT NULL,
|
||||||
article_id INTEGER NOT NULL,
|
target_id TEXT,
|
||||||
article_title TEXT,
|
input_id TEXT,
|
||||||
|
input_title TEXT,
|
||||||
status TEXT NOT NULL,
|
status TEXT NOT NULL,
|
||||||
error_msg TEXT,
|
error_msg TEXT,
|
||||||
|
result_summary TEXT,
|
||||||
created_at INTEGER NOT NULL,
|
created_at INTEGER NOT NULL,
|
||||||
updated_at INTEGER NOT NULL
|
updated_at INTEGER NOT NULL
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -1,76 +0,0 @@
|
|||||||
"""publish_logs 表读写模板。"""
|
|
||||||
|
|
||||||
from __future__ import annotations
|
|
||||||
|
|
||||||
from typing import Any, List, Optional, Tuple
|
|
||||||
|
|
||||||
from db.connection import get_conn, init_db
|
|
||||||
from util.timeutil import now_unix
|
|
||||||
|
|
||||||
|
|
||||||
def save_publish_log(
|
|
||||||
account_id: str,
|
|
||||||
article_id: int,
|
|
||||||
article_title: str,
|
|
||||||
status: str,
|
|
||||||
error_msg: Optional[str] = None,
|
|
||||||
) -> int:
|
|
||||||
init_db()
|
|
||||||
now = now_unix()
|
|
||||||
conn = get_conn()
|
|
||||||
try:
|
|
||||||
cur = conn.cursor()
|
|
||||||
cur.execute(
|
|
||||||
"""
|
|
||||||
INSERT INTO publish_logs (account_id, article_id, article_title, status, error_msg, created_at, updated_at)
|
|
||||||
VALUES (?, ?, ?, ?, ?, ?, ?)
|
|
||||||
""",
|
|
||||||
(account_id, int(article_id), article_title or "", status, error_msg, now, now),
|
|
||||||
)
|
|
||||||
new_id = int(cur.lastrowid)
|
|
||||||
conn.commit()
|
|
||||||
finally:
|
|
||||||
conn.close()
|
|
||||||
return new_id
|
|
||||||
|
|
||||||
|
|
||||||
def list_publish_logs(
|
|
||||||
limit: int,
|
|
||||||
status: Optional[str] = None,
|
|
||||||
account_id: Optional[str] = None,
|
|
||||||
) -> List[Tuple[Any, ...]]:
|
|
||||||
init_db()
|
|
||||||
conn = get_conn()
|
|
||||||
try:
|
|
||||||
cur = conn.cursor()
|
|
||||||
sql = (
|
|
||||||
"SELECT id, account_id, article_id, article_title, status, error_msg, created_at, updated_at "
|
|
||||||
"FROM publish_logs WHERE 1=1 "
|
|
||||||
)
|
|
||||||
params: List[Any] = []
|
|
||||||
if status:
|
|
||||||
sql += "AND status = ? "
|
|
||||||
params.append(status)
|
|
||||||
if account_id:
|
|
||||||
sql += "AND account_id = ? "
|
|
||||||
params.append(account_id)
|
|
||||||
sql += "ORDER BY created_at DESC, id DESC LIMIT ?"
|
|
||||||
params.append(int(limit))
|
|
||||||
cur.execute(sql, tuple(params))
|
|
||||||
return list(cur.fetchall())
|
|
||||||
finally:
|
|
||||||
conn.close()
|
|
||||||
|
|
||||||
|
|
||||||
def get_publish_log_by_id(log_id: int) -> Optional[Tuple[Any, ...]]:
|
|
||||||
init_db()
|
|
||||||
conn = get_conn()
|
|
||||||
try:
|
|
||||||
cur = conn.cursor()
|
|
||||||
cur.execute(
|
|
||||||
"SELECT id, account_id, article_id, article_title, status, error_msg, created_at, updated_at FROM publish_logs WHERE id = ?",
|
|
||||||
(int(log_id),),
|
|
||||||
)
|
|
||||||
return cur.fetchone()
|
|
||||||
finally:
|
|
||||||
conn.close()
|
|
||||||
88
scripts/db/task_logs_repository.py
Normal file
88
scripts/db/task_logs_repository.py
Normal file
@@ -0,0 +1,88 @@
|
|||||||
|
"""task_logs 表读写模板。
|
||||||
|
|
||||||
|
通用业务日志仓储:记录每次任务执行的 task_type / target / input / 状态 / 结果摘要。
|
||||||
|
不假设任何特定业务(发布、对账、审核等都可复用)。
|
||||||
|
"""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from typing import Any, List, Optional, Tuple
|
||||||
|
|
||||||
|
from db.connection import get_conn, init_db
|
||||||
|
from util.timeutil import now_unix
|
||||||
|
|
||||||
|
|
||||||
|
def save_task_log(
|
||||||
|
task_type: str,
|
||||||
|
target_id: Optional[str] = None,
|
||||||
|
input_id: Optional[str] = None,
|
||||||
|
input_title: Optional[str] = None,
|
||||||
|
status: str = "success",
|
||||||
|
error_msg: Optional[str] = None,
|
||||||
|
result_summary: Optional[str] = None,
|
||||||
|
) -> int:
|
||||||
|
init_db()
|
||||||
|
now = now_unix()
|
||||||
|
conn = get_conn()
|
||||||
|
try:
|
||||||
|
cur = conn.cursor()
|
||||||
|
cur.execute(
|
||||||
|
"""
|
||||||
|
INSERT INTO task_logs
|
||||||
|
(task_type, target_id, input_id, input_title, status, error_msg, result_summary, created_at, updated_at)
|
||||||
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||||
|
""",
|
||||||
|
(task_type, target_id, input_id, input_title or "", status, error_msg, result_summary, now, now),
|
||||||
|
)
|
||||||
|
new_id = int(cur.lastrowid)
|
||||||
|
conn.commit()
|
||||||
|
finally:
|
||||||
|
conn.close()
|
||||||
|
return new_id
|
||||||
|
|
||||||
|
|
||||||
|
def list_task_logs(
|
||||||
|
limit: int,
|
||||||
|
status: Optional[str] = None,
|
||||||
|
task_type: Optional[str] = None,
|
||||||
|
target_id: Optional[str] = None,
|
||||||
|
) -> List[Tuple[Any, ...]]:
|
||||||
|
init_db()
|
||||||
|
conn = get_conn()
|
||||||
|
try:
|
||||||
|
cur = conn.cursor()
|
||||||
|
sql = (
|
||||||
|
"SELECT id, task_type, target_id, input_id, input_title, status, error_msg, result_summary, "
|
||||||
|
"created_at, updated_at FROM task_logs WHERE 1=1 "
|
||||||
|
)
|
||||||
|
params: List[Any] = []
|
||||||
|
if status:
|
||||||
|
sql += "AND status = ? "
|
||||||
|
params.append(status)
|
||||||
|
if task_type:
|
||||||
|
sql += "AND task_type = ? "
|
||||||
|
params.append(task_type)
|
||||||
|
if target_id:
|
||||||
|
sql += "AND target_id = ? "
|
||||||
|
params.append(target_id)
|
||||||
|
sql += "ORDER BY created_at DESC, id DESC LIMIT ?"
|
||||||
|
params.append(int(limit))
|
||||||
|
cur.execute(sql, tuple(params))
|
||||||
|
return list(cur.fetchall())
|
||||||
|
finally:
|
||||||
|
conn.close()
|
||||||
|
|
||||||
|
|
||||||
|
def get_task_log_by_id(log_id: int) -> Optional[Tuple[Any, ...]]:
|
||||||
|
init_db()
|
||||||
|
conn = get_conn()
|
||||||
|
try:
|
||||||
|
cur = conn.cursor()
|
||||||
|
cur.execute(
|
||||||
|
"SELECT id, task_type, target_id, input_id, input_title, status, error_msg, result_summary, "
|
||||||
|
"created_at, updated_at FROM task_logs WHERE id = ?",
|
||||||
|
(int(log_id),),
|
||||||
|
)
|
||||||
|
return cur.fetchone()
|
||||||
|
finally:
|
||||||
|
conn.close()
|
||||||
@@ -1 +0,0 @@
|
|||||||
# Vendored from jiangchang-platform-kit/sdk/jiangchang_skill_core/ — keep runtime_env + unified_logging in sync.
|
|
||||||
@@ -1,113 +0,0 @@
|
|||||||
"""
|
|
||||||
JIANGCHANG_* 数据根与用户目录:解析规则 + 可选本地 CLI 默认值。
|
|
||||||
|
|
||||||
模板复制后通常无需大改;如组织环境不同,再按项目实际调整。
|
|
||||||
"""
|
|
||||||
|
|
||||||
from __future__ import annotations
|
|
||||||
|
|
||||||
import os
|
|
||||||
import sys
|
|
||||||
|
|
||||||
CLI_LOCAL_DEV_ENABLED = True
|
|
||||||
DEFAULT_LOCAL_USER_ID = "10032"
|
|
||||||
WIN_DEFAULT_DATA_ROOT = r"D:\jiangchang-data"
|
|
||||||
WIN_DEFAULT_JIANGCHANG_APP_ROOT = r"D:\AI\jiangchang"
|
|
||||||
|
|
||||||
|
|
||||||
def platform_default_data_root() -> str:
|
|
||||||
if sys.platform == "win32":
|
|
||||||
return WIN_DEFAULT_DATA_ROOT
|
|
||||||
return os.path.join(os.path.expanduser("~"), ".jiangchang-data")
|
|
||||||
|
|
||||||
|
|
||||||
def get_data_root() -> str:
|
|
||||||
env = (
|
|
||||||
os.getenv("CLAW_DATA_ROOT")
|
|
||||||
or os.getenv("JIANGCHANG_DATA_ROOT")
|
|
||||||
or ""
|
|
||||||
).strip()
|
|
||||||
if env:
|
|
||||||
return env
|
|
||||||
return platform_default_data_root()
|
|
||||||
|
|
||||||
|
|
||||||
def get_user_id() -> str:
|
|
||||||
return (
|
|
||||||
os.getenv("CLAW_USER_ID")
|
|
||||||
or os.getenv("JIANGCHANG_USER_ID")
|
|
||||||
or ""
|
|
||||||
).strip() or "_anon"
|
|
||||||
|
|
||||||
|
|
||||||
def _looks_like_skills_root(path: str) -> bool:
|
|
||||||
if not path or not os.path.isdir(path):
|
|
||||||
return False
|
|
||||||
for marker in (
|
|
||||||
"llm-manager",
|
|
||||||
"content-manager",
|
|
||||||
"account-manager",
|
|
||||||
"sohu-publisher",
|
|
||||||
"toutiao-publisher",
|
|
||||||
"gongzhonghao-publisher",
|
|
||||||
"weibo-publisher",
|
|
||||||
"skill-template",
|
|
||||||
):
|
|
||||||
if os.path.isdir(os.path.join(path, marker)):
|
|
||||||
return True
|
|
||||||
return False
|
|
||||||
|
|
||||||
|
|
||||||
def get_skills_root() -> str:
|
|
||||||
for key in ("JIANGCHANG_SKILLS_ROOT", "CLAW_SKILLS_ROOT"):
|
|
||||||
v = (os.getenv(key) or "").strip()
|
|
||||||
if v:
|
|
||||||
return os.path.normpath(v)
|
|
||||||
|
|
||||||
app = (os.getenv("JIANGCHANG_APP_ROOT") or "").strip()
|
|
||||||
if sys.platform == "win32" and not app:
|
|
||||||
app = WIN_DEFAULT_JIANGCHANG_APP_ROOT
|
|
||||||
if app:
|
|
||||||
nested = os.path.join(app, "skills")
|
|
||||||
if _looks_like_skills_root(nested):
|
|
||||||
return os.path.normpath(nested)
|
|
||||||
if _looks_like_skills_root(app):
|
|
||||||
return os.path.normpath(app)
|
|
||||||
|
|
||||||
if sys.platform == "win32":
|
|
||||||
nested = os.path.join(WIN_DEFAULT_JIANGCHANG_APP_ROOT, "skills")
|
|
||||||
if _looks_like_skills_root(nested):
|
|
||||||
return os.path.normpath(nested)
|
|
||||||
if _looks_like_skills_root(WIN_DEFAULT_JIANGCHANG_APP_ROOT):
|
|
||||||
return os.path.normpath(WIN_DEFAULT_JIANGCHANG_APP_ROOT)
|
|
||||||
|
|
||||||
return os.path.normpath(os.path.join(os.path.expanduser("~"), ".openclaw", "skills"))
|
|
||||||
|
|
||||||
|
|
||||||
def get_sibling_skills_root(skill_scripts_dir: str | None = None) -> str:
|
|
||||||
if skill_scripts_dir:
|
|
||||||
scripts = os.path.abspath(skill_scripts_dir)
|
|
||||||
skill_root = os.path.dirname(scripts)
|
|
||||||
inferred = os.path.dirname(skill_root)
|
|
||||||
if _looks_like_skills_root(inferred):
|
|
||||||
return os.path.normpath(inferred)
|
|
||||||
|
|
||||||
for key in ("JIANGCHANG_SKILLS_ROOT", "CLAW_SKILLS_ROOT"):
|
|
||||||
v = (os.getenv(key) or "").strip()
|
|
||||||
if v:
|
|
||||||
return os.path.normpath(v)
|
|
||||||
|
|
||||||
return get_skills_root()
|
|
||||||
|
|
||||||
|
|
||||||
def apply_cli_local_defaults() -> None:
|
|
||||||
enabled = CLI_LOCAL_DEV_ENABLED
|
|
||||||
if not enabled:
|
|
||||||
v = (os.getenv("JIANGCHANG_CLI_LOCAL_DEV") or "").strip().lower()
|
|
||||||
enabled = v in ("1", "true", "yes", "on")
|
|
||||||
if not enabled:
|
|
||||||
return
|
|
||||||
if not (os.getenv("CLAW_DATA_ROOT") or os.getenv("JIANGCHANG_DATA_ROOT") or "").strip():
|
|
||||||
os.environ["JIANGCHANG_DATA_ROOT"] = platform_default_data_root()
|
|
||||||
if not (os.getenv("CLAW_USER_ID") or os.getenv("JIANGCHANG_USER_ID") or "").strip():
|
|
||||||
os.environ["JIANGCHANG_USER_ID"] = DEFAULT_LOCAL_USER_ID.strip()
|
|
||||||
@@ -1,137 +0,0 @@
|
|||||||
"""
|
|
||||||
统一文件日志:{DATA_ROOT}/{USER_ID}/logs/jiangchang.log
|
|
||||||
按日轮转;行内带 trace_id 与 skill_slug,便于跨技能排查。
|
|
||||||
"""
|
|
||||||
|
|
||||||
from __future__ import annotations
|
|
||||||
|
|
||||||
import logging
|
|
||||||
import os
|
|
||||||
import sys
|
|
||||||
import uuid
|
|
||||||
from logging.handlers import TimedRotatingFileHandler
|
|
||||||
from typing import Optional
|
|
||||||
|
|
||||||
from .runtime_env import get_data_root, get_user_id
|
|
||||||
|
|
||||||
_skill_slug: str = ""
|
|
||||||
_logger_name: str = ""
|
|
||||||
|
|
||||||
|
|
||||||
def get_unified_logs_dir() -> str:
|
|
||||||
path = os.path.join(get_data_root(), get_user_id(), "logs")
|
|
||||||
os.makedirs(path, exist_ok=True)
|
|
||||||
return path
|
|
||||||
|
|
||||||
|
|
||||||
def get_skill_log_file_path() -> str:
|
|
||||||
override = (os.getenv("JIANGCHANG_LOG_FILE") or "").strip()
|
|
||||||
if override:
|
|
||||||
parent = os.path.dirname(os.path.abspath(override))
|
|
||||||
if parent:
|
|
||||||
os.makedirs(parent, exist_ok=True)
|
|
||||||
return os.path.abspath(override)
|
|
||||||
return os.path.join(get_unified_logs_dir(), "jiangchang.log")
|
|
||||||
|
|
||||||
|
|
||||||
def ensure_trace_for_process() -> str:
|
|
||||||
existing = (os.getenv("JIANGCHANG_TRACE_ID") or "").strip()
|
|
||||||
if existing:
|
|
||||||
return existing
|
|
||||||
tid = uuid.uuid4().hex[:12]
|
|
||||||
os.environ["JIANGCHANG_TRACE_ID"] = tid
|
|
||||||
return tid
|
|
||||||
|
|
||||||
|
|
||||||
def subprocess_env_with_trace(environ: Optional[dict] = None) -> dict:
|
|
||||||
ensure_trace_for_process()
|
|
||||||
tid = (os.getenv("JIANGCHANG_TRACE_ID") or "").strip() or ensure_trace_for_process()
|
|
||||||
base = os.environ if environ is None else environ
|
|
||||||
return {**base, "JIANGCHANG_TRACE_ID": tid}
|
|
||||||
|
|
||||||
|
|
||||||
class _SkillContextFilter(logging.Filter):
|
|
||||||
def filter(self, record: logging.LogRecord) -> bool:
|
|
||||||
record.trace_id = (os.getenv("JIANGCHANG_TRACE_ID") or "").strip() or "-"
|
|
||||||
record.skill_slug = _skill_slug or "-"
|
|
||||||
return True
|
|
||||||
|
|
||||||
|
|
||||||
_FORMAT = "%(asctime)s | %(levelname)-8s | %(trace_id)s | %(skill_slug)s | %(name)s | %(message)s"
|
|
||||||
_DATEFMT = "%Y-%m-%dT%H:%M:%S"
|
|
||||||
|
|
||||||
|
|
||||||
def _log_level_from_env() -> int:
|
|
||||||
v = (os.getenv("JIANGCHANG_LOG_LEVEL") or "INFO").strip().upper()
|
|
||||||
return getattr(logging, v, None) or logging.INFO
|
|
||||||
|
|
||||||
|
|
||||||
def _backup_count() -> int:
|
|
||||||
try:
|
|
||||||
n = int((os.getenv("JIANGCHANG_LOG_BACKUP_COUNT") or "30").strip())
|
|
||||||
return max(1, min(n, 365))
|
|
||||||
except ValueError:
|
|
||||||
return 30
|
|
||||||
|
|
||||||
|
|
||||||
def setup_skill_logging(skill_slug: str, logger_name: str) -> None:
|
|
||||||
global _skill_slug, _logger_name
|
|
||||||
ensure_trace_for_process()
|
|
||||||
_skill_slug = skill_slug
|
|
||||||
_logger_name = logger_name
|
|
||||||
|
|
||||||
log = logging.getLogger(logger_name)
|
|
||||||
if log.handlers:
|
|
||||||
return
|
|
||||||
log.setLevel(_log_level_from_env())
|
|
||||||
path = get_skill_log_file_path()
|
|
||||||
fh = TimedRotatingFileHandler(
|
|
||||||
path,
|
|
||||||
when="midnight",
|
|
||||||
interval=1,
|
|
||||||
backupCount=_backup_count(),
|
|
||||||
encoding="utf-8",
|
|
||||||
delay=True,
|
|
||||||
)
|
|
||||||
fmt = logging.Formatter(_FORMAT, datefmt=_DATEFMT)
|
|
||||||
fh.setFormatter(fmt)
|
|
||||||
fh.addFilter(_SkillContextFilter())
|
|
||||||
log.addHandler(fh)
|
|
||||||
if (os.getenv("JIANGCHANG_LOG_TO_STDERR") or "").strip().lower() in ("1", "true", "yes", "on"):
|
|
||||||
sh = logging.StreamHandler(sys.stderr)
|
|
||||||
sh.setLevel(logging.WARNING)
|
|
||||||
sh.setFormatter(fmt)
|
|
||||||
sh.addFilter(_SkillContextFilter())
|
|
||||||
log.addHandler(sh)
|
|
||||||
log.propagate = False
|
|
||||||
|
|
||||||
|
|
||||||
def get_skill_logger() -> logging.Logger:
|
|
||||||
if not _logger_name:
|
|
||||||
raise RuntimeError("get_skill_logger: call setup_skill_logging first")
|
|
||||||
return logging.getLogger(_logger_name)
|
|
||||||
|
|
||||||
|
|
||||||
def attach_unified_file_handler(
|
|
||||||
log_path: str,
|
|
||||||
*,
|
|
||||||
skill_slug: str,
|
|
||||||
logger_name: str,
|
|
||||||
level: int = logging.DEBUG,
|
|
||||||
) -> logging.Logger:
|
|
||||||
global _skill_slug
|
|
||||||
ensure_trace_for_process()
|
|
||||||
_skill_slug = skill_slug
|
|
||||||
lg = logging.getLogger(logger_name)
|
|
||||||
lg.handlers.clear()
|
|
||||||
lg.setLevel(level)
|
|
||||||
parent = os.path.dirname(os.path.abspath(log_path))
|
|
||||||
if parent:
|
|
||||||
os.makedirs(parent, exist_ok=True)
|
|
||||||
fh = logging.FileHandler(log_path, encoding="utf-8")
|
|
||||||
fmt = logging.Formatter(_FORMAT, datefmt=_DATEFMT)
|
|
||||||
fh.setFormatter(fmt)
|
|
||||||
fh.addFilter(_SkillContextFilter())
|
|
||||||
lg.addHandler(fh)
|
|
||||||
lg.propagate = False
|
|
||||||
return lg
|
|
||||||
@@ -5,6 +5,9 @@ skill-template CLI 入口。
|
|||||||
|
|
||||||
复制为新技能后,请修改注释与常量,但保留当前分层结构:
|
复制为新技能后,请修改注释与常量,但保留当前分层结构:
|
||||||
cli(argv)→ service(业务编排)→ db(持久化)→ util(通用工具)。
|
cli(argv)→ service(业务编排)→ db(持久化)→ util(通用工具)。
|
||||||
|
|
||||||
|
jiangchang_skill_core 来自宿主共享 Python runtime 安装的 jiangchang-platform-kit,
|
||||||
|
不在技能仓库内 vendored。
|
||||||
"""
|
"""
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
@@ -25,6 +28,10 @@ from jiangchang_skill_core.runtime_env import apply_cli_local_defaults
|
|||||||
|
|
||||||
apply_cli_local_defaults()
|
apply_cli_local_defaults()
|
||||||
|
|
||||||
|
from util.config_bootstrap import bootstrap_skill_config
|
||||||
|
|
||||||
|
bootstrap_skill_config()
|
||||||
|
|
||||||
from cli.app import main
|
from cli.app import main
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|||||||
26
scripts/service/example_adapter/__init__.py
Normal file
26
scripts/service/example_adapter/__init__.py
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
"""示例 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()
|
||||||
37
scripts/service/example_adapter/base.py
Normal file
37
scripts/service/example_adapter/base.py
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
"""示例适配器数据契约与基类。
|
||||||
|
|
||||||
|
复制本目录为 ``<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
|
||||||
24
scripts/service/example_adapter/mock.py
Normal file
24
scripts/service/example_adapter/mock.py
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
"""离线 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)},
|
||||||
|
)
|
||||||
14
scripts/service/example_adapter/real_api.py
Normal file
14
scripts/service/example_adapter/real_api.py
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
"""真实 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"
|
||||||
|
)
|
||||||
14
scripts/service/example_adapter/real_rpa.py
Normal file
14
scripts/service/example_adapter/real_rpa.py
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
"""真实 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"
|
||||||
|
)
|
||||||
108
scripts/service/example_adapter/sim_rpa.py
Normal file
108
scripts/service/example_adapter/sim_rpa.py
Normal file
@@ -0,0 +1,108 @@
|
|||||||
|
"""仿真 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,
|
||||||
|
)
|
||||||
88
scripts/service/example_browser_rpa.py
Normal file
88
scripts/service/example_browser_rpa.py
Normal file
@@ -0,0 +1,88 @@
|
|||||||
|
#!/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()))
|
||||||
@@ -1,10 +0,0 @@
|
|||||||
"""后台自动化占位模块模板。"""
|
|
||||||
|
|
||||||
from __future__ import annotations
|
|
||||||
|
|
||||||
from typing import Any, Dict
|
|
||||||
|
|
||||||
|
|
||||||
async def publish(account: Dict[str, Any], article: Dict[str, Any], account_id: str) -> str:
|
|
||||||
_ = (account, article, account_id)
|
|
||||||
return "ERROR:NOT_IMPLEMENTED 请复制模板后将本文件改名为具体平台模块并实现后台自动化逻辑"
|
|
||||||
@@ -1,83 +0,0 @@
|
|||||||
"""发布编排、日志查询模板。"""
|
|
||||||
|
|
||||||
from __future__ import annotations
|
|
||||||
|
|
||||||
import json
|
|
||||||
import sys
|
|
||||||
from typing import Optional
|
|
||||||
|
|
||||||
from db import publish_logs_repository as plr
|
|
||||||
from service.entitlement_service import check_entitlement
|
|
||||||
from util.constants import SKILL_SLUG, SKILL_VERSION
|
|
||||||
from util.timeutil import unix_to_iso
|
|
||||||
|
|
||||||
|
|
||||||
def cmd_publish(account_id: Optional[str] = None, article_id: Optional[str] = None) -> int:
|
|
||||||
_ = (account_id, article_id)
|
|
||||||
ok, reason = check_entitlement(SKILL_SLUG)
|
|
||||||
if not ok:
|
|
||||||
print(f"❌ {reason}")
|
|
||||||
return 1
|
|
||||||
print("❌ 这是模板仓库,请复制后在 scripts/service/ 中实现真正的发布逻辑。")
|
|
||||||
return 1
|
|
||||||
|
|
||||||
|
|
||||||
def cmd_logs(limit: int = 10, status: Optional[str] = None, account_id: Optional[str] = None) -> int:
|
|
||||||
if limit <= 0:
|
|
||||||
limit = 10
|
|
||||||
rows = plr.list_publish_logs(limit, status, account_id)
|
|
||||||
if not rows:
|
|
||||||
print("暂无发布记录")
|
|
||||||
return 0
|
|
||||||
|
|
||||||
sep_line = "_" * 39
|
|
||||||
for idx, r in enumerate(rows):
|
|
||||||
rid, aid, arid, title, st, err, cat, uat = r
|
|
||||||
print(f"id:{rid}")
|
|
||||||
print(f"account_id:{aid or ''}")
|
|
||||||
print(f"article_id:{arid}")
|
|
||||||
print(f"article_title:{title or ''}")
|
|
||||||
print(f"status:{st or ''}")
|
|
||||||
print(f"error_msg:{err or ''}")
|
|
||||||
print(f"created_at:{unix_to_iso(cat) or str(cat or '')}")
|
|
||||||
print(f"updated_at:{unix_to_iso(uat) or str(uat or '')}")
|
|
||||||
if idx != len(rows) - 1:
|
|
||||||
print(sep_line)
|
|
||||||
print()
|
|
||||||
return 0
|
|
||||||
|
|
||||||
|
|
||||||
def cmd_log_get(log_id: str) -> int:
|
|
||||||
if not str(log_id).isdigit():
|
|
||||||
print("❌ log_id 必须是数字")
|
|
||||||
return 1
|
|
||||||
row = plr.get_publish_log_by_id(int(log_id))
|
|
||||||
if not row:
|
|
||||||
print("❌ 没有这条发布记录")
|
|
||||||
return 1
|
|
||||||
rid, aid, arid, title, st, err, cat, uat = row
|
|
||||||
print(
|
|
||||||
json.dumps(
|
|
||||||
{
|
|
||||||
"id": int(rid),
|
|
||||||
"account_id": aid,
|
|
||||||
"article_id": int(arid),
|
|
||||||
"article_title": title,
|
|
||||||
"status": st,
|
|
||||||
"error_msg": err,
|
|
||||||
"created_at": unix_to_iso(cat),
|
|
||||||
"updated_at": unix_to_iso(uat),
|
|
||||||
},
|
|
||||||
ensure_ascii=False,
|
|
||||||
)
|
|
||||||
)
|
|
||||||
return 0
|
|
||||||
|
|
||||||
|
|
||||||
def cmd_health() -> int:
|
|
||||||
return 0 if sys.version_info >= (3, 10) else 1
|
|
||||||
|
|
||||||
|
|
||||||
def cmd_version() -> int:
|
|
||||||
print(json.dumps({"version": SKILL_VERSION, "skill": SKILL_SLUG}, ensure_ascii=False))
|
|
||||||
return 0
|
|
||||||
@@ -1,4 +1,8 @@
|
|||||||
"""兄弟技能 CLI 调用模板。"""
|
"""兄弟技能 CLI 调用工具。
|
||||||
|
|
||||||
|
通过子进程调用同级其他 skill 的 main.py,并解析 JSON 输出。
|
||||||
|
具体调用哪些兄弟技能,由复制后的业务 skill 在 service 层按需决定。
|
||||||
|
"""
|
||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
@@ -12,7 +16,19 @@ from util.logging_config import subprocess_env_with_trace
|
|||||||
from util.runtime_paths import get_skills_root
|
from util.runtime_paths import get_skills_root
|
||||||
|
|
||||||
|
|
||||||
def _call_json_script(script_path: str, args: List[str]) -> Optional[Dict[str, Any]]:
|
def call_sibling_json(skill_slug: str, args: List[str]) -> Optional[Dict[str, Any]]:
|
||||||
|
"""通用兄弟技能调用器。
|
||||||
|
|
||||||
|
Args:
|
||||||
|
skill_slug: 兄弟技能的 slug(即同级目录名)。
|
||||||
|
args: 传给 main.py 的命令行参数列表。
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
若兄弟技能输出可解析的 JSON 对象则返回 dict;否则返回 None。
|
||||||
|
"""
|
||||||
|
script_path = os.path.join(get_skills_root(), skill_slug, "scripts", "main.py")
|
||||||
|
if not os.path.isfile(script_path):
|
||||||
|
return None
|
||||||
proc = subprocess.run(
|
proc = subprocess.run(
|
||||||
[sys.executable, script_path, *args],
|
[sys.executable, script_path, *args],
|
||||||
capture_output=True,
|
capture_output=True,
|
||||||
@@ -31,9 +47,11 @@ def _call_json_script(script_path: str, args: List[str]) -> Optional[Dict[str, A
|
|||||||
return data if isinstance(data, dict) else None
|
return data if isinstance(data, dict) else None
|
||||||
|
|
||||||
|
|
||||||
def get_account_manager_main_path() -> str:
|
def get_sibling_main_path(skill_slug: str) -> str:
|
||||||
return os.path.join(get_skills_root(), "account-manager", "scripts", "main.py")
|
"""获取兄弟技能的 main.py 绝对路径。
|
||||||
|
|
||||||
|
使用示例(在 task_service.py 中):
|
||||||
def get_content_manager_main_path() -> str:
|
from service.sibling_bridge import get_sibling_main_path, call_sibling_json
|
||||||
return os.path.join(get_skills_root(), "content-manager", "scripts", "main.py")
|
result = call_sibling_json("account-manager", ["list", "--limit", "10"])
|
||||||
|
"""
|
||||||
|
return os.path.join(get_skills_root(), skill_slug, "scripts", "main.py")
|
||||||
|
|||||||
84
scripts/service/task_run_support.py
Normal file
84
scripts/service/task_run_support.py
Normal file
@@ -0,0 +1,84 @@
|
|||||||
|
"""任务运行辅助:录屏信息组装与 CLI 输出(通用模板,无业务逻辑)。"""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from typing import Any, Dict, Optional
|
||||||
|
|
||||||
|
|
||||||
|
def build_video_info(
|
||||||
|
video_summary: dict[str, Any],
|
||||||
|
compose_context_before: Optional[dict[str, Any]] = None,
|
||||||
|
compose_context_after: Optional[dict[str, Any]] = None,
|
||||||
|
) -> dict[str, Any]:
|
||||||
|
"""将 RpaVideoSession.summary() 转为 task log / CLI 可用的 video 块。"""
|
||||||
|
return {
|
||||||
|
"enabled": video_summary.get("enabled", False),
|
||||||
|
"path": video_summary.get("path"),
|
||||||
|
"capture_path": video_summary.get("capture_path"),
|
||||||
|
"record_log_path": video_summary.get("record_log_path"),
|
||||||
|
"warnings": list(video_summary.get("warnings") or []),
|
||||||
|
"voiceover_path": video_summary.get("voiceover_path"),
|
||||||
|
"music_path": video_summary.get("music_path"),
|
||||||
|
"audio_warnings": list(video_summary.get("audio_warnings") or []),
|
||||||
|
"compose_context": {
|
||||||
|
**(compose_context_before or {}),
|
||||||
|
"after_compose": compose_context_after or {},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def merge_video_into_result_summary(
|
||||||
|
payload: dict[str, Any],
|
||||||
|
video_info: Optional[dict[str, Any]],
|
||||||
|
) -> dict[str, Any]:
|
||||||
|
"""把 video artifact 字段写入 result_summary JSON 对象。"""
|
||||||
|
if not video_info:
|
||||||
|
return payload
|
||||||
|
payload = dict(payload)
|
||||||
|
payload["video"] = video_info
|
||||||
|
payload["video_path"] = video_info.get("path")
|
||||||
|
payload["raw_video"] = video_info.get("capture_path")
|
||||||
|
payload["video_log"] = video_info.get("record_log_path")
|
||||||
|
payload["video_warnings"] = list(video_info.get("warnings") or [])
|
||||||
|
payload["voiceover_path"] = video_info.get("voiceover_path")
|
||||||
|
payload["music_path"] = video_info.get("music_path")
|
||||||
|
payload["audio_warnings"] = list(video_info.get("audio_warnings") or [])
|
||||||
|
return payload
|
||||||
|
|
||||||
|
|
||||||
|
def _print_video_summary(video_block: Optional[dict[str, Any]]) -> None:
|
||||||
|
"""任务完成后在 CLI 打印录屏路径、日志与诊断信息。"""
|
||||||
|
if not isinstance(video_block, dict) or not video_block.get("enabled"):
|
||||||
|
return
|
||||||
|
record_log = video_block.get("record_log_path")
|
||||||
|
if record_log:
|
||||||
|
print(f"录屏日志:{record_log}")
|
||||||
|
path = video_block.get("path")
|
||||||
|
if path:
|
||||||
|
print(f"录屏路径:{path}")
|
||||||
|
else:
|
||||||
|
print("录屏路径:未生成")
|
||||||
|
warnings = video_block.get("warnings") or []
|
||||||
|
if warnings:
|
||||||
|
print(f"视频诊断:{'; '.join(str(w) for w in warnings)}")
|
||||||
|
capture = video_block.get("capture_path")
|
||||||
|
if capture:
|
||||||
|
print(f"原始录屏路径:{capture}")
|
||||||
|
voiceover = video_block.get("voiceover_path")
|
||||||
|
if voiceover:
|
||||||
|
print(f"旁白路径:{voiceover}")
|
||||||
|
music = video_block.get("music_path")
|
||||||
|
if music:
|
||||||
|
print(f"背景音乐:{music}")
|
||||||
|
audio_warnings = video_block.get("audio_warnings") or []
|
||||||
|
if audio_warnings:
|
||||||
|
print(f"音频诊断:{'; '.join(str(w) for w in audio_warnings)}")
|
||||||
|
compose_ctx = video_block.get("compose_context")
|
||||||
|
if isinstance(compose_ctx, dict):
|
||||||
|
print(
|
||||||
|
"视频合成上下文:"
|
||||||
|
f"media_assets_root={compose_ctx.get('media_assets_root', '')}; "
|
||||||
|
f"ffmpeg_path={compose_ctx.get('ffmpeg_path', '')}; "
|
||||||
|
f"background_music_issue={compose_ctx.get('background_music_issue') or ''}; "
|
||||||
|
f"background_music_sample_path={compose_ctx.get('background_music_sample_path', '')}"
|
||||||
|
)
|
||||||
188
scripts/service/task_service.py
Normal file
188
scripts/service/task_service.py
Normal file
@@ -0,0 +1,188 @@
|
|||||||
|
"""任务编排、日志查询模板。
|
||||||
|
|
||||||
|
通用业务编排层:负责参数校验、鉴权、调用具体业务实现、写入任务日志。
|
||||||
|
复制后在 cmd_run 中实现真正的业务逻辑。
|
||||||
|
"""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import asyncio
|
||||||
|
import json
|
||||||
|
import os
|
||||||
|
import uuid
|
||||||
|
from typing import Any, Dict, Optional
|
||||||
|
|
||||||
|
from jiangchang_skill_core import collect_runtime_diagnostics, config, format_runtime_health_lines
|
||||||
|
from jiangchang_skill_core.rpa.video_session import RpaVideoSession
|
||||||
|
|
||||||
|
from db import task_logs_repository as tlr
|
||||||
|
from service.entitlement_service import check_entitlement
|
||||||
|
from service.task_run_support import (
|
||||||
|
_print_video_summary,
|
||||||
|
build_video_info,
|
||||||
|
merge_video_into_result_summary,
|
||||||
|
)
|
||||||
|
from util.constants import PLATFORM_KIT_MIN_VERSION, SKILL_SLUG, SKILL_VERSION
|
||||||
|
from util.runtime_paths import get_skill_data_dir, get_skill_root
|
||||||
|
from util.timeutil import unix_to_iso
|
||||||
|
|
||||||
|
|
||||||
|
async def _run_template_demo(target: Optional[str], input_id: Optional[str]) -> tuple[int, dict[str, Any]]:
|
||||||
|
"""最小 RpaVideoSession 示范:不启动浏览器、不执行业务,仅演示录屏包裹范式。"""
|
||||||
|
batch_id = uuid.uuid4().hex[:12]
|
||||||
|
data_dir = get_skill_data_dir()
|
||||||
|
|
||||||
|
async with RpaVideoSession(
|
||||||
|
skill_slug=SKILL_SLUG,
|
||||||
|
skill_data_dir=data_dir,
|
||||||
|
batch_id=batch_id,
|
||||||
|
title="开始执行示例任务",
|
||||||
|
closing_title="示例任务执行完成",
|
||||||
|
) as video:
|
||||||
|
video.add_step("准备执行示例任务")
|
||||||
|
if target:
|
||||||
|
video.add_step(f"接收目标参数:{target}")
|
||||||
|
if input_id:
|
||||||
|
video.add_step(f"接收输入标识:{input_id}")
|
||||||
|
video.add_step("示例任务执行完成")
|
||||||
|
|
||||||
|
video_summary = video.summary()
|
||||||
|
video_info = build_video_info(video_summary, {}, {})
|
||||||
|
return 1, video_info
|
||||||
|
|
||||||
|
|
||||||
|
def cmd_run(target: Optional[str] = None, input_id: Optional[str] = None) -> int:
|
||||||
|
"""通用任务执行入口模板。复制后请实现真实业务逻辑。"""
|
||||||
|
ok, reason = check_entitlement(SKILL_SLUG)
|
||||||
|
if not ok:
|
||||||
|
print(f"❌ {reason}")
|
||||||
|
return 1
|
||||||
|
|
||||||
|
rc, video_info = asyncio.run(_run_template_demo(target, input_id))
|
||||||
|
_print_video_summary(video_info)
|
||||||
|
|
||||||
|
summary_payload = merge_video_into_result_summary(
|
||||||
|
{
|
||||||
|
"template_demo": True,
|
||||||
|
"target": target,
|
||||||
|
"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 的真实业务逻辑。")
|
||||||
|
return rc
|
||||||
|
|
||||||
|
|
||||||
|
def cmd_logs(
|
||||||
|
limit: int = 10,
|
||||||
|
status: Optional[str] = None,
|
||||||
|
task_type: Optional[str] = None,
|
||||||
|
target_id: Optional[str] = None,
|
||||||
|
) -> int:
|
||||||
|
if limit <= 0:
|
||||||
|
limit = 10
|
||||||
|
rows = tlr.list_task_logs(limit, status, task_type, target_id)
|
||||||
|
if not rows:
|
||||||
|
print("暂无任务日志")
|
||||||
|
return 0
|
||||||
|
|
||||||
|
sep_line = "_" * 39
|
||||||
|
for idx, r in enumerate(rows):
|
||||||
|
rid, ttype, tid, iid, ititle, st, err, rsum, cat, uat = r
|
||||||
|
print(f"id:{rid}")
|
||||||
|
print(f"task_type:{ttype or ''}")
|
||||||
|
print(f"target_id:{tid or ''}")
|
||||||
|
print(f"input_id:{iid or ''}")
|
||||||
|
print(f"input_title:{ititle or ''}")
|
||||||
|
print(f"status:{st or ''}")
|
||||||
|
print(f"error_msg:{err or ''}")
|
||||||
|
print(f"result_summary:{rsum or ''}")
|
||||||
|
print(f"created_at:{unix_to_iso(cat) or str(cat or '')}")
|
||||||
|
print(f"updated_at:{unix_to_iso(uat) or str(uat or '')}")
|
||||||
|
if idx != len(rows) - 1:
|
||||||
|
print(sep_line)
|
||||||
|
print()
|
||||||
|
return 0
|
||||||
|
|
||||||
|
|
||||||
|
def cmd_log_get(log_id: str) -> int:
|
||||||
|
if not str(log_id).isdigit():
|
||||||
|
print("❌ log_id 必须是数字")
|
||||||
|
return 1
|
||||||
|
row = tlr.get_task_log_by_id(int(log_id))
|
||||||
|
if not row:
|
||||||
|
print("❌ 没有这条任务日志")
|
||||||
|
return 1
|
||||||
|
rid, ttype, tid, iid, ititle, st, err, rsum, cat, uat = row
|
||||||
|
print(
|
||||||
|
json.dumps(
|
||||||
|
{
|
||||||
|
"id": int(rid),
|
||||||
|
"task_type": ttype,
|
||||||
|
"target_id": tid,
|
||||||
|
"input_id": iid,
|
||||||
|
"input_title": ititle,
|
||||||
|
"status": st,
|
||||||
|
"error_msg": err,
|
||||||
|
"result_summary": rsum,
|
||||||
|
"created_at": unix_to_iso(cat),
|
||||||
|
"updated_at": unix_to_iso(uat),
|
||||||
|
},
|
||||||
|
ensure_ascii=False,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
return 0
|
||||||
|
|
||||||
|
|
||||||
|
def cmd_config_path() -> int:
|
||||||
|
example_path = os.path.join(get_skill_root(), ".env.example")
|
||||||
|
env_path = config.get_env_file_path() or ""
|
||||||
|
print(
|
||||||
|
json.dumps(
|
||||||
|
{
|
||||||
|
"skill": SKILL_SLUG,
|
||||||
|
"env_path": env_path,
|
||||||
|
"example_path": os.path.abspath(example_path),
|
||||||
|
},
|
||||||
|
ensure_ascii=False,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
return 0
|
||||||
|
|
||||||
|
|
||||||
|
def cmd_health() -> int:
|
||||||
|
runtime = collect_runtime_diagnostics(
|
||||||
|
skill_slug=SKILL_SLUG,
|
||||||
|
platform_kit_min_version=PLATFORM_KIT_MIN_VERSION,
|
||||||
|
skill_root=get_skill_root(),
|
||||||
|
)
|
||||||
|
example_path = os.path.join(get_skill_root(), ".env.example")
|
||||||
|
env_path = config.get_env_file_path() or ""
|
||||||
|
env_exists = bool(env_path and os.path.isfile(env_path))
|
||||||
|
|
||||||
|
health_status = "failed" if runtime.has_fatal_issues else "ok"
|
||||||
|
lines = [
|
||||||
|
f"{SKILL_SLUG} health: {health_status}",
|
||||||
|
*format_runtime_health_lines(runtime),
|
||||||
|
f"env_path: {env_path}",
|
||||||
|
f"env_exists: {env_exists}",
|
||||||
|
f"example_path: {os.path.abspath(example_path)}",
|
||||||
|
]
|
||||||
|
for line in lines:
|
||||||
|
print(line)
|
||||||
|
return 1 if runtime.has_fatal_issues else 0
|
||||||
|
|
||||||
|
|
||||||
|
def cmd_version() -> int:
|
||||||
|
print(json.dumps({"version": SKILL_VERSION, "skill": SKILL_SLUG}, ensure_ascii=False))
|
||||||
|
return 0
|
||||||
19
scripts/util/config_bootstrap.py
Normal file
19
scripts/util/config_bootstrap.py
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
"""技能配置初始化:.env.example 落盘与用户 .env 缺失项合并。"""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import os
|
||||||
|
|
||||||
|
from jiangchang_skill_core import config
|
||||||
|
|
||||||
|
from util.constants import SKILL_SLUG, SKILL_VERSION
|
||||||
|
from util.runtime_paths import get_skill_root
|
||||||
|
|
||||||
|
|
||||||
|
def bootstrap_skill_config() -> str:
|
||||||
|
"""确保用户数据目录 .env 存在,并追加 .env.example 中的新配置项。"""
|
||||||
|
example_path = os.path.join(get_skill_root(), ".env.example")
|
||||||
|
env_path = config.ensure_env_file(SKILL_SLUG, example_path)
|
||||||
|
comment = f"{SKILL_SLUG} v{SKILL_VERSION}" if SKILL_VERSION else None
|
||||||
|
config.merge_missing_env_keys(example_path, env_path, comment_skill=comment)
|
||||||
|
return env_path
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
"""技能标识与版本(复制后请修改)。"""
|
"""技能标识、版本与平台公共库约束(复制后请修改 slug/version/logger)。"""
|
||||||
|
|
||||||
SKILL_SLUG = "your-skill-slug"
|
SKILL_SLUG = "your-skill-slug"
|
||||||
SKILL_VERSION = "1.0.11"
|
SKILL_VERSION = "1.0.14"
|
||||||
LOG_LOGGER_NAME = "openclaw.skill.your_skill_slug"
|
LOG_LOGGER_NAME = "openclaw.skill.your_skill_slug"
|
||||||
|
PLATFORM_KIT_MIN_VERSION = "1.0.14"
|
||||||
|
|||||||
10
tests/.gitignore
vendored
Normal file
10
tests/.gitignore
vendored
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
# 测试运行产物与本地密钥,勿提交
|
||||||
|
__pycache__/
|
||||||
|
*.py[cod]
|
||||||
|
.pytest_cache/
|
||||||
|
desktop/artifacts/
|
||||||
|
integration/artifacts/
|
||||||
|
*.log
|
||||||
|
.env
|
||||||
|
.env.*
|
||||||
|
!.env.sample
|
||||||
182
tests/README.md
182
tests/README.md
@@ -1,8 +1,180 @@
|
|||||||
# tests
|
# skill-template 测试说明(企业数字员工技能)
|
||||||
|
|
||||||
这里放模板复制后的单元测试或最小回归测试。
|
本目录提供面向 **ERP / TMS / WMS / 船司 / 报关 / 邮件 / 浏览器 / LLM / RPA** 等外联场景的**分层测试骨架**:默认仅内存与本地 SQLite;**真实 API / 真实 RPA** 必须通过环境变量显式授权,且仅以 ``*.sample`` 或复制后的文件提供范式。
|
||||||
|
|
||||||
模板仓库本身不强制附带业务测试,但建议新 skill 至少补:
|
复制为新技能后,请保留隔离数据根与 profile 策略,再按域扩展用例。
|
||||||
|
|
||||||
- `health` / `version` 最小冒烟测试
|
---
|
||||||
- 关键 `service` 层函数的单元测试
|
|
||||||
|
## 我该把测试写在哪里?
|
||||||
|
|
||||||
|
人类开发者与 Cursor 等 AI 工具在加用例前,先对照下表决定**文件落点**与**是否默认执行**:
|
||||||
|
|
||||||
|
| 场景 | 放在哪里 | 默认是否运行 | 说明 |
|
||||||
|
|------|----------|--------------|------|
|
||||||
|
| CLI / health / version / metadata / runtime 路径 | `tests/test_*.py` | 是 | 默认必跑,保持轻量、无外联 |
|
||||||
|
| 纯函数 / 业务规则 | `tests/test_*.py` | 是 | 不依赖真实外部系统 |
|
||||||
|
| 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` | 是,复制后 | 输入样例 + 期望输出,适合解析、计算、校验类技能 |
|
||||||
|
| 仿真 API | `tests/integration/test_simulator_api_contract.py.sample` | 否 | 复制并设置 `OPENCLAW_TEST_TARGET=simulator_api`(或兼容键名 `OPENCLOW_TEST_TARGET`)后按需运行 |
|
||||||
|
| 仿真 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` |
|
||||||
|
| 真实 RPA | `tests/integration/test_real_rpa_contract.py.sample` | 否 | 最高风险,只能手动触发 |
|
||||||
|
| 桌面宿主 E2E | `tests/desktop/test_desktop_smoke.py.sample` | 否 | 需要 pytest + `jiangchang_desktop_sdk` |
|
||||||
|
|
||||||
|
说明:`python tests/run_tests.py` **只**收集 `tests/` **根目录**下的 `test_*.py`;子目录里的 `*.sample` 与 desktop 用例**不会**被默认发现。
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 新技能最小测试清单
|
||||||
|
|
||||||
|
从本模板复制出新技能仓库后,建议至少逐项确认:
|
||||||
|
|
||||||
|
- [ ] `python tests/run_tests.py -v` 能通过(含 platform-kit 导入与无 vendored core 守护)。
|
||||||
|
- [ ] `python scripts/main.py health` 能通过(输出 runtime diagnostics;warning 不导致非零退出)。
|
||||||
|
- [ ] `python scripts/main.py version` 输出 JSON,且 `skill` 与目录名 / `SKILL.md` / `constants.SKILL_SLUG` 一致。
|
||||||
|
- [ ] 所有 DB / 文件写入都在 `IsolatedDataRoot`(或等价隔离)下测试,不写真实数据目录。
|
||||||
|
- [ ] 外部系统默认使用 mock / `FakeAdapter`,不访问真实 API,不打开真实 RPA。
|
||||||
|
- [ ] 至少有 1 个成功路径测试。
|
||||||
|
- [ ] 至少有 1 个缺必填字段 / 非法输入测试。
|
||||||
|
- [ ] 如果有 adapter,至少覆盖 timeout / unauthorized / invalid response(可用 fake 模拟)。
|
||||||
|
- [ ] 如果有解析 / 计算 / 校验类业务,至少保留 1 组 golden fixture(脱敏)。
|
||||||
|
- [ ] 如果技能有**特有** Python 三方依赖,`requirements.txt` 已声明且版本约束合理(尽量收窄范围);公共依赖(platform-kit、playwright)由宿主共享 runtime 提供,**不要**写入技能 requirements。
|
||||||
|
- [ ] `health` 能在依赖缺失或原生运行库未就绪时给出清晰、可操作的错误(非裸 traceback)。
|
||||||
|
- [ ] 真实 API / 真实 RPA 测试只放 `tests/integration/`,并且默认 `.sample`,不默认运行。
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 1. 默认必跑(unittest)
|
||||||
|
|
||||||
|
在**技能仓库根目录**执行:
|
||||||
|
|
||||||
|
```powershell
|
||||||
|
$env:PYTHONDONTWRITEBYTECODE = "1"
|
||||||
|
python tests/run_tests.py
|
||||||
|
python tests/run_tests.py -v
|
||||||
|
python tests/run_tests.py cli_smoke
|
||||||
|
python tests/run_tests.py test_cli_smoke
|
||||||
|
```
|
||||||
|
|
||||||
|
### 1.1 收集范围
|
||||||
|
|
||||||
|
- ``run_tests.py`` **只**发现 ``tests/`` **根目录**下 ``test_*.py``。
|
||||||
|
- **不递归**:``tests/samples/``、``tests/integration/``、``tests/desktop/``(其中文件为 ``*.sample`` 或需 pytest 单独跑)。
|
||||||
|
- 启动时把 ``scripts/`` 与 ``tests/`` 加入 ``sys.path``。
|
||||||
|
- Windows 下将 **stdout/stderr** 包装为 **UTF-8**。
|
||||||
|
|
||||||
|
### 1.2 默认套件覆盖
|
||||||
|
|
||||||
|
| 能力 | 文件 |
|
||||||
|
|------|------|
|
||||||
|
| CLI 冒烟(import ``cli.app.main``) | ``test_cli_smoke.py`` |
|
||||||
|
| **subprocess 真实入口** ``python scripts/main.py`` | ``test_entrypoint_subprocess.py`` |
|
||||||
|
| 运行路径与 **CLAW_*/JIANGCHANG_*** 隔离 | ``test_runtime_paths.py`` |
|
||||||
|
| ``SKILL.md`` slug 与 ``constants.SKILL_SLUG`` | ``test_skill_metadata.py`` |
|
||||||
|
| DB / ``task_logs`` 骨架冒烟 | ``test_db_smoke.py`` |
|
||||||
|
| **adapter profile / 外呼授权策略** | ``test_adapter_profile_policy.py`` + ``adapter_test_utils.py``(非 ``test_`` 前缀,不自动当用例收集) |
|
||||||
|
| **无 vendored ``jiangchang_skill_core``** | ``test_platform_import.py`` |
|
||||||
|
| **runtime diagnostics 架构守护** | ``test_runtime_diagnostics_integration.py`` |
|
||||||
|
| **文档/runtime 标准守护** | ``test_template_runtime_standard.py`` |
|
||||||
|
| **配置 bootstrap / config-path** | ``test_config_bootstrap.py`` |
|
||||||
|
| **health runtime diagnostics** | ``test_health_runtime.py`` |
|
||||||
|
|
||||||
|
复制为新技能后**不得删除**上述架构守护测试,除非同步更新模板标准并理解影响。
|
||||||
|
|
||||||
|
### 1.3 数据隔离(``_support.IsolatedDataRoot``)
|
||||||
|
|
||||||
|
同时设置(进入同一临时目录 / 用户):
|
||||||
|
|
||||||
|
| ``CLAW_DATA_ROOT`` | ``JIANGCHANG_DATA_ROOT`` |
|
||||||
|
|--------------------|---------------------------|
|
||||||
|
| 同一 ``tempfile.mkdtemp(prefix="skill-test-")`` | 同上 |
|
||||||
|
|
||||||
|
| ``CLAW_USER_ID`` | ``JIANGCHANG_USER_ID`` |
|
||||||
|
|------------------|-------------------------|
|
||||||
|
| ``_test`` | ``_test`` |
|
||||||
|
|
||||||
|
退出时四个键均恢复(原不存在则 ``pop``),并删除临时目录。
|
||||||
|
|
||||||
|
### 1.4 产物与密钥
|
||||||
|
|
||||||
|
- ``tests/.gitignore`` 忽略 ``__pycache__``、``.pytest_cache``、``*.pyc``、``desktop/artifacts/``、``integration/artifacts/``、``.env`` 等。
|
||||||
|
- 推荐运行前设置 ``PYTHONDONTWRITEBYTECODE=1`` 减少 ``.pyc``。
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 2. 可选:Service / Golden(``tests/samples/*.sample``)
|
||||||
|
|
||||||
|
- 展示 **service 契约** 与 **fixture 回归** 写法;**默认不执行**。
|
||||||
|
- 启用:复制 ``tests/samples/test_service_contract.py.sample`` 等为 ``tests/test_*.py``(去掉 ``.sample``),按业务修改后再 ``python tests/run_tests.py``。
|
||||||
|
- 脱敏样例数据见 ``tests/fixtures/README.md`` 与 ``sample_request.json`` / ``expected_response.json``;profile 结构见 ``adapter_profiles.sample.yaml``。
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 3. 可选:Integration(``tests/integration/*.sample``)
|
||||||
|
|
||||||
|
- **默认不跑**;文件均为 ``*.py.sample``。
|
||||||
|
- 说明与开关见 **`tests/integration/README.md`**。
|
||||||
|
- **禁止**在样例中硬编码真实 token、密码、生产 URL;凭证须来自密钥库或受控环境变量。
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 4. 可选:Desktop E2E(pytest)
|
||||||
|
|
||||||
|
见 **`tests/desktop/README.md`**。示例为 `test_desktop_smoke.py.sample`,需复制为 `test_desktop_smoke.py` 后用 pytest 运行。
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 5. 环境变量:测试档位与授权
|
||||||
|
|
||||||
|
### 5.1 测试档位(二选一变量名,后者为历史拼写兼容)
|
||||||
|
|
||||||
|
- ``OPENCLAW_TEST_TARGET`` 或 ``OPENCLOW_TEST_TARGET``
|
||||||
|
|
||||||
|
**合法取值**(非法值将使 ``get_test_target()`` 抛 ``ValueError``):
|
||||||
|
|
||||||
|
| 取值 | 含义 |
|
||||||
|
|------|------|
|
||||||
|
| ``unit`` | **默认**:单元 / 内存 / mock,不跑仿真与真实外联 |
|
||||||
|
| ``mock`` | 与 ``unit`` 类似的安全档位(显式语义) |
|
||||||
|
| ``simulator_api`` | 仅允许 **仿真 HTTP** 类集成(如 localhost) |
|
||||||
|
| ``simulator_rpa`` | 仅允许 **仿真页面 / 录播 RPA** |
|
||||||
|
| ``real_api`` | 真实 API(另需 ``ALLOW_REAL_API=1``) |
|
||||||
|
| ``real_rpa`` | 真实 RPA(另需 ``ALLOW_REAL_RPA=1``) |
|
||||||
|
|
||||||
|
未设置时等价 ``unit``。
|
||||||
|
|
||||||
|
### 5.2 授权开关(显式 ``1``)
|
||||||
|
|
||||||
|
| 变量 | 作用 |
|
||||||
|
|------|------|
|
||||||
|
| ``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、不做写操作、不读取真实密钥、不写真实数据根。
|
||||||
|
- ``assert_no_real_network_env()`` 断言默认未误开 ``ALLOW_REAL_*``(用于必跑用例自检)。
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## AI 编程工具注意事项(Cursor 等)
|
||||||
|
|
||||||
|
以下约束适用于为本仓库或复制出的技能编写/修改测试时,**避免误伤业务与生产**:
|
||||||
|
|
||||||
|
- **不要为了**让测试通过去改业务代码,除非用户明确要求修改实现。
|
||||||
|
- **不要**把真实外部系统调用写进默认的 `tests/test_*.py`(默认套件须无外联、无真实浏览器)。
|
||||||
|
- **不要**把 `*.sample` 改名为可执行测试或移入根目录并去掉 `.sample`,除非用户明确要求启用 integration / 真实联调。
|
||||||
|
- **不要**硬编码真实凭证、token、cookie、生产 URL;占位请用明显虚构域名(如 `example.invalid`)或文档约定的 `credential_ref`。
|
||||||
|
- 默认测试只能使用 **mock、stub、`FakeAdapter`、fixtures、隔离 SQLite** 等安全手段。
|
||||||
|
- 业务逻辑应优先在 **service 层** 编写与测试,不要只断言 CLI 文案。
|
||||||
|
- 异常路径应返回**结构化错误**(如统一错误码/字段),避免裸异常直接穿透到 CLI。
|
||||||
|
- 有真实系统联调需求时,测试应写在 `tests/integration/`,并配合 `OPENCLAW_TEST_TARGET` / `ALLOW_REAL_*` / `ALLOW_WRITE_ACTIONS` 等**显式开关**;不得默认开启。
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 6. 与 content-manager 的关系
|
||||||
|
|
||||||
|
- **未**复制 article / media / prompt / NotebookLM 等业务测试。
|
||||||
|
- 本骨架面向**通用外联技能**;业务契约请放在各自技能的 ``tests/samples`` / ``tests/integration`` 复制件中实现。
|
||||||
|
|||||||
87
tests/_support.py
Normal file
87
tests/_support.py
Normal file
@@ -0,0 +1,87 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
"""
|
||||||
|
测试公共支撑。
|
||||||
|
|
||||||
|
1. 将 scripts/ 加入 sys.path,便于 `from cli…` / `from db…` / `from util…` 等导入。
|
||||||
|
2. `IsolatedDataRoot`:同时设置 CLAW_* 与 JIANGCHANG_* 数据根与用户 ID,
|
||||||
|
与 `jiangchang_skill_core.runtime_env` 的优先级一致(CLAW_* 优先),
|
||||||
|
避免只设 JIANGCHANG_* 时仍读到真实 CLAW_DATA_ROOT。
|
||||||
|
退出时四个变量均恢复;原不存在的键会删除;并 rmtree 临时目录。
|
||||||
|
"""
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import os
|
||||||
|
import shutil
|
||||||
|
import sys
|
||||||
|
import tempfile
|
||||||
|
from typing import Optional
|
||||||
|
|
||||||
|
_TESTS_DIR = os.path.dirname(os.path.abspath(__file__))
|
||||||
|
_SKILL_ROOT = os.path.abspath(os.path.join(_TESTS_DIR, ".."))
|
||||||
|
_SCRIPTS_DIR = os.path.join(_SKILL_ROOT, "scripts")
|
||||||
|
|
||||||
|
if _SCRIPTS_DIR not in sys.path:
|
||||||
|
sys.path.insert(0, _SCRIPTS_DIR)
|
||||||
|
|
||||||
|
_ISOLATION_KEYS = (
|
||||||
|
"CLAW_DATA_ROOT",
|
||||||
|
"JIANGCHANG_DATA_ROOT",
|
||||||
|
"CLAW_USER_ID",
|
||||||
|
"JIANGCHANG_USER_ID",
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def get_scripts_dir() -> str:
|
||||||
|
return _SCRIPTS_DIR
|
||||||
|
|
||||||
|
|
||||||
|
def get_skill_root() -> str:
|
||||||
|
return _SKILL_ROOT
|
||||||
|
|
||||||
|
|
||||||
|
def platform_kit_version_patch(version: str = "1.0.14"):
|
||||||
|
"""Mock installed jiangchang-platform-kit version for health/diagnostics tests."""
|
||||||
|
from unittest.mock import patch
|
||||||
|
|
||||||
|
return patch(
|
||||||
|
"jiangchang_skill_core.runtime_diagnostics._platform_kit_version",
|
||||||
|
return_value=version,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class IsolatedDataRoot:
|
||||||
|
"""
|
||||||
|
在独立临时目录下模拟数据根,避免污染本机默认数据目录。
|
||||||
|
|
||||||
|
进入上下文时(同一套隔离值写入 CLAW_* 与 JIANGCHANG_*):
|
||||||
|
- CLAW_DATA_ROOT / JIANGCHANG_DATA_ROOT -> 同一 tempfile.mkdtemp(prefix='skill-test-')
|
||||||
|
- CLAW_USER_ID / JIANGCHANG_USER_ID -> _test(与业务默认用户区分)
|
||||||
|
|
||||||
|
退出时:四个变量均恢复为进入前状态(若原先未设置则 pop),并删除临时目录。
|
||||||
|
"""
|
||||||
|
|
||||||
|
def __init__(self, user_id: str = "_test") -> None:
|
||||||
|
self._tmp: Optional[str] = None
|
||||||
|
self._old: dict[str, Optional[str]] = {}
|
||||||
|
self._user_id = user_id
|
||||||
|
|
||||||
|
def __enter__(self) -> str:
|
||||||
|
self._tmp = tempfile.mkdtemp(prefix="skill-test-")
|
||||||
|
for k in _ISOLATION_KEYS:
|
||||||
|
self._old[k] = os.environ.get(k)
|
||||||
|
os.environ["CLAW_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
|
||||||
|
return self._tmp
|
||||||
|
|
||||||
|
def __exit__(self, *exc_info: object) -> None:
|
||||||
|
for k in _ISOLATION_KEYS:
|
||||||
|
v = self._old.get(k)
|
||||||
|
if v is None:
|
||||||
|
os.environ.pop(k, None)
|
||||||
|
else:
|
||||||
|
os.environ[k] = v
|
||||||
|
if self._tmp and os.path.isdir(self._tmp):
|
||||||
|
shutil.rmtree(self._tmp, ignore_errors=True)
|
||||||
|
self._tmp = None
|
||||||
142
tests/adapter_test_utils.py
Normal file
142
tests/adapter_test_utils.py
Normal file
@@ -0,0 +1,142 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
"""
|
||||||
|
企业技能对接外部系统(ERP / TMS / WMS / 船司 / 报关 / 邮件 / 浏览器 / LLM / RPA)时,
|
||||||
|
测试侧共用的 **profile / 授权 / 禁止真实外呼** 工具。
|
||||||
|
|
||||||
|
- 仅标准库;不发起 HTTP;不启动浏览器。
|
||||||
|
- 真实 API / RPA 契约测试请放在 ``tests/integration/*.sample``,并配合环境变量显式授权。
|
||||||
|
"""
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import os
|
||||||
|
from typing import Any, Final
|
||||||
|
|
||||||
|
# 允许的 OPENCLAW_TEST_TARGET / OPENCLOW_TEST_TARGET 取值(后者为历史拼写兼容)
|
||||||
|
ALLOWED_TEST_TARGETS: Final[frozenset[str]] = frozenset(
|
||||||
|
{
|
||||||
|
"unit",
|
||||||
|
"mock",
|
||||||
|
"simulator_api",
|
||||||
|
"simulator_rpa",
|
||||||
|
"real_api",
|
||||||
|
"real_rpa",
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def _raw_test_target_env() -> str:
|
||||||
|
return (os.environ.get("OPENCLAW_TEST_TARGET") or os.environ.get("OPENCLOW_TEST_TARGET") or "").strip()
|
||||||
|
|
||||||
|
|
||||||
|
def get_test_target() -> str:
|
||||||
|
"""
|
||||||
|
读取 ``OPENCLAW_TEST_TARGET`` 或 ``OPENCLOW_TEST_TARGET``(兼容旧拼写)。
|
||||||
|
|
||||||
|
未设置时返回 ``unit``。非法取值抛出 ``ValueError``,便于尽早发现拼写错误。
|
||||||
|
"""
|
||||||
|
raw = _raw_test_target_env()
|
||||||
|
if not raw:
|
||||||
|
return "unit"
|
||||||
|
if raw not in ALLOWED_TEST_TARGETS:
|
||||||
|
raise ValueError(
|
||||||
|
f"Invalid test target {raw!r}. Allowed: {sorted(ALLOWED_TEST_TARGETS)}"
|
||||||
|
)
|
||||||
|
return raw
|
||||||
|
|
||||||
|
|
||||||
|
def real_access_allowed(profile: str) -> bool:
|
||||||
|
"""
|
||||||
|
判断当前环境是否允许对 ``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":
|
||||||
|
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"):
|
||||||
|
return True
|
||||||
|
raise ValueError(f"unknown profile for real_access_allowed: {profile!r}")
|
||||||
|
|
||||||
|
|
||||||
|
def should_skip_profile(profile: str, write: bool = False) -> tuple[bool, str]:
|
||||||
|
"""
|
||||||
|
返回 ``(should_skip, reason)``。
|
||||||
|
|
||||||
|
策略摘要:
|
||||||
|
- ``mock``:永不跳过(内存 / FakeAdapter,无外呼)。
|
||||||
|
- ``simulator_api``:仅当 ``get_test_target() == "simulator_api"`` 时不跳过。
|
||||||
|
- ``simulator_rpa``:仅当 target 为 ``simulator_rpa`` 时不跳过。
|
||||||
|
- ``real_api``:target 须为 ``real_api`` 且 ``ALLOW_REAL_API=1``;``write=True`` 还须 ``ALLOW_WRITE_ACTIONS=1``。
|
||||||
|
- ``real_rpa``:target 须为 ``real_rpa`` 且 ``ALLOW_REAL_RPA=1``;``write=True`` 还须 ``ALLOW_WRITE_ACTIONS=1``。
|
||||||
|
"""
|
||||||
|
target = get_test_target()
|
||||||
|
|
||||||
|
if profile == "mock":
|
||||||
|
return (False, "")
|
||||||
|
|
||||||
|
if profile == "simulator_api":
|
||||||
|
if target != "simulator_api":
|
||||||
|
return (True, f"OPENCLAW_TEST_TARGET must be simulator_api (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, "")
|
||||||
|
|
||||||
|
raise ValueError(f"unknown profile: {profile!r}")
|
||||||
|
|
||||||
|
|
||||||
|
class FakeAdapter:
|
||||||
|
"""
|
||||||
|
模拟外部系统 adapter,用于单元测试中的超时 / 鉴权 / 畸形响应等分支。
|
||||||
|
|
||||||
|
``mode``:``success`` | ``timeout`` | ``invalid_response`` | ``unauthorized``
|
||||||
|
"""
|
||||||
|
|
||||||
|
def __init__(self, mode: str = "success") -> None:
|
||||||
|
if mode not in ("success", "timeout", "invalid_response", "unauthorized"):
|
||||||
|
raise ValueError(f"unsupported FakeAdapter mode: {mode!r}")
|
||||||
|
self._mode = mode
|
||||||
|
|
||||||
|
def call(self, payload: Any) -> Any:
|
||||||
|
if self._mode == "success":
|
||||||
|
return {"success": True, "data": {"echo": payload}}
|
||||||
|
if self._mode == "timeout":
|
||||||
|
raise TimeoutError("simulated upstream timeout")
|
||||||
|
if self._mode == "invalid_response":
|
||||||
|
return object() # 非 dict,模拟无法解析的响应体
|
||||||
|
if self._mode == "unauthorized":
|
||||||
|
raise PermissionError("simulated 401/403")
|
||||||
|
raise RuntimeError("unreachable")
|
||||||
|
|
||||||
|
|
||||||
|
def assert_no_real_network_env() -> None:
|
||||||
|
"""
|
||||||
|
断言当前进程未显式打开「真实外呼」授权开关(默认安全 CI / 本地开发)。
|
||||||
|
|
||||||
|
若需在受控环境跑真实集成,请显式设置 ``ALLOW_REAL_*`` 并使用 ``tests/integration/*.sample``。
|
||||||
|
"""
|
||||||
|
assert os.environ.get("ALLOW_REAL_API", "") != "1", "ALLOW_REAL_API must not be 1 in default unit tests"
|
||||||
|
assert os.environ.get("ALLOW_REAL_RPA", "") != "1", "ALLOW_REAL_RPA must not be 1 in default unit tests"
|
||||||
63
tests/desktop/README.md
Normal file
63
tests/desktop/README.md
Normal file
@@ -0,0 +1,63 @@
|
|||||||
|
# 可选桌面 E2E(pytest)
|
||||||
|
|
||||||
|
本目录用于**匠厂桌面 + jiangchang_desktop_sdk** 的端到端冒烟,**不是** `python tests/run_tests.py` 默认路径的一部分。
|
||||||
|
|
||||||
|
## 依赖
|
||||||
|
|
||||||
|
- `pytest`
|
||||||
|
- `jiangchang_desktop_sdk`(由 `conftest.py` 按多级路径尝试加入 `sys.path`,见该文件说明)
|
||||||
|
- 匠厂桌面已安装或开发态可启动,且 `jiangchang://` 协议可用
|
||||||
|
|
||||||
|
## 为什么不默认跑
|
||||||
|
|
||||||
|
- 需要真实宿主环境,无法在纯 CI/无头环境稳定复现。
|
||||||
|
- `unittest` 的 `tests/run_tests.py` **只发现** `tests/` 根目录下 `test_*.py`,**不递归**本目录。
|
||||||
|
- 示例文件为 **`test_desktop_smoke.py.sample`**,避免被误收集。
|
||||||
|
|
||||||
|
## 如何启用
|
||||||
|
|
||||||
|
1. 将 `test_desktop_smoke.py.sample` **复制或重命名**为 `test_desktop_smoke.py`。
|
||||||
|
2. 按你的技能名称、路由关键词、期望输出修改 `ask` 与断言。
|
||||||
|
3. 运行:
|
||||||
|
|
||||||
|
```powershell
|
||||||
|
pytest tests/desktop/test_desktop_smoke.py -v -s
|
||||||
|
```
|
||||||
|
|
||||||
|
失败时,`conftest.py` 中的 `failure_snapshot_dir` fixture 会将现场目录指向 `tests/desktop/artifacts/`(需自行避免将大文件提交进仓库)。
|
||||||
|
|
||||||
|
## 与后端测试的关系
|
||||||
|
|
||||||
|
- **后端 / CLI / DB**:请使用仓库根目录下 `python tests/run_tests.py`(见 `tests/README.md`)。
|
||||||
|
- **桌面 E2E**:仅在本目录用 **pytest** 维护,二者互补、互不替代。
|
||||||
|
|
||||||
|
若仅需验证 **service 业务逻辑**、契约与 golden 回归,**不要**写 desktop E2E;desktop 只用于验证**宿主 UI、路由、`jiangchang://` 协议、真实用户交互链路**等与桌面宿主强相关的行为。
|
||||||
|
|
||||||
|
## 模板选择指南
|
||||||
|
|
||||||
|
| 场景 | 用哪个模板 |
|
||||||
|
|---|---|
|
||||||
|
| 纯问答 skill(不上传附件、不跑长流程) | `test_desktop_smoke.py.sample` |
|
||||||
|
| 需要上传附件 / RPA 长流程 / Agent 多轮工具调用 | `test_desktop_smoke_with_attachment.py.sample` |
|
||||||
|
|
||||||
|
录屏入口都用 `record_screencast.py.sample`,里面 `TEST_FILE_NAME` 常量切换测试文件。
|
||||||
|
|
||||||
|
## 已知 SDK 坑(来自 disburse-payroll-icbc 实战)
|
||||||
|
|
||||||
|
1. **`client.send_file()` 在 v2.0.17+ 抛 NotImplementedError**——附件场景必须用
|
||||||
|
`jiangchang_desktop_sdk.e2e_helpers.drop_file_into_composer`(DataTransfer 拖拽)。
|
||||||
|
2. **`client.ask()` 内部 `user_msg_index<0` 死循环**——如果匠厂启动慢 / gateway 握手有
|
||||||
|
延迟,导致 15s 内没抓到 user 节点,ask() 会锁死后续 900s 等待。建议长流程 / 附件场景
|
||||||
|
绕开 ask(),用 `jiangchang_desktop_sdk.e2e_helpers` 里的 `send_prompt_via_composer`
|
||||||
|
+ `wait_for_user_message` + `wait_for_agent_complete` 自己控时。
|
||||||
|
3. **字幕关键词建议用 ASCII**——虽然 platform-kit 已修了 subprocess 编码乱码,
|
||||||
|
保险起见字幕脚本里关键词用 `step: 1/` 这种 ASCII 锚而非中文短语。
|
||||||
|
4. **`client.bring_to_front()` 已升级**——v?? 起会 ctypes ShowWindow(SW_MAXIMIZE) +
|
||||||
|
SetForegroundWindow,能真正铺满桌面。dev 模式下也能用(不依赖 jiangchang:// 协议)。
|
||||||
|
|
||||||
|
## 录屏新能力(platform-kit v??)
|
||||||
|
|
||||||
|
- `run_screencast(..., activate_window=True)` 自动激活+最大化匠厂窗口
|
||||||
|
- `run_screencast(..., monitor_index=1)` 多显示器场景显式录主屏
|
||||||
|
- 字幕脚本支持三元组 `(keyword, text, duration)` 给个别字幕指定独立时长,
|
||||||
|
覆盖长等待场景中间没日志输出的"字幕真空"
|
||||||
88
tests/desktop/conftest.py
Normal file
88
tests/desktop/conftest.py
Normal file
@@ -0,0 +1,88 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
"""
|
||||||
|
可选桌面 E2E:pytest 收集时自动加载,负责把 jiangchang_desktop_sdk 加入 sys.path。
|
||||||
|
|
||||||
|
查找顺序:
|
||||||
|
1. 已 pip install 则直接 import;
|
||||||
|
2. 环境变量 JIANGCHANG_KIT_ROOT → sdk/jiangchang-desktop-sdk/src(旧版布局);
|
||||||
|
3. 环境变量 JIANGCHANG_KIT_ROOT → src(新版 platform-kit 布局);
|
||||||
|
4. 自本文件上溯:skill 根 → workspace 根 → OpenClaw 根,拼路径;
|
||||||
|
5. 兜底 D:\\AI\\jiangchang-platform-kit\\src(若存在);
|
||||||
|
6. 都找不到则 pytest.exit 给出可操作的排障说明。
|
||||||
|
"""
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import importlib.util
|
||||||
|
import os
|
||||||
|
import sys
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
|
||||||
|
def _bootstrap_jiangchang_desktop_sdk() -> None:
|
||||||
|
if importlib.util.find_spec("jiangchang_desktop_sdk") is not None:
|
||||||
|
return
|
||||||
|
|
||||||
|
env_kit_root = (os.environ.get("JIANGCHANG_KIT_ROOT") or "").strip()
|
||||||
|
|
||||||
|
here = os.path.dirname(os.path.abspath(__file__))
|
||||||
|
skill_root = os.path.abspath(os.path.join(here, "..", ".."))
|
||||||
|
workspace_root = os.path.abspath(os.path.join(skill_root, ".."))
|
||||||
|
open_claw_root = os.path.abspath(os.path.join(workspace_root, ".."))
|
||||||
|
|
||||||
|
candidates = []
|
||||||
|
if env_kit_root:
|
||||||
|
candidates.append(os.path.join(env_kit_root, "sdk", "jiangchang-desktop-sdk", "src"))
|
||||||
|
candidates.append(os.path.join(env_kit_root, "src"))
|
||||||
|
candidates.append(
|
||||||
|
os.path.join(workspace_root, "jiangchang-platform-kit", "sdk", "jiangchang-desktop-sdk", "src")
|
||||||
|
)
|
||||||
|
candidates.append(
|
||||||
|
os.path.join(open_claw_root, "jiangchang-platform-kit", "sdk", "jiangchang-desktop-sdk", "src")
|
||||||
|
)
|
||||||
|
candidates.append(
|
||||||
|
os.path.join(workspace_root, "jiangchang-platform-kit", "src")
|
||||||
|
)
|
||||||
|
candidates.append(
|
||||||
|
os.path.join(open_claw_root, "jiangchang-platform-kit", "src")
|
||||||
|
)
|
||||||
|
default_kit_src = r"D:\AI\jiangchang-platform-kit\src"
|
||||||
|
if os.path.isdir(default_kit_src):
|
||||||
|
candidates.append(default_kit_src)
|
||||||
|
|
||||||
|
tried = []
|
||||||
|
for cand in candidates:
|
||||||
|
cand_abs = os.path.abspath(cand)
|
||||||
|
tried.append(cand_abs)
|
||||||
|
if os.path.isdir(cand_abs) and os.path.isdir(os.path.join(cand_abs, "jiangchang_desktop_sdk")):
|
||||||
|
if cand_abs not in sys.path:
|
||||||
|
sys.path.insert(0, cand_abs)
|
||||||
|
return
|
||||||
|
|
||||||
|
pytest.exit(
|
||||||
|
"无法定位 jiangchang_desktop_sdk。请任选一种方式解决:\n"
|
||||||
|
" 1) pip install -e <path-to>/jiangchang-platform-kit\n"
|
||||||
|
" 2) 设置环境变量 JIANGCHANG_KIT_ROOT=<path-to>/jiangchang-platform-kit\n"
|
||||||
|
" (SDK 新版位置:<root>\\src;旧版:<root>\\sdk\\jiangchang-desktop-sdk\\src)\n"
|
||||||
|
" 3) 将 jiangchang-platform-kit 与本仓库放在同一 workspace 父级可推断的位置\n"
|
||||||
|
"已尝试的候选路径:\n - " + "\n - ".join(tried),
|
||||||
|
returncode=4,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
_bootstrap_jiangchang_desktop_sdk()
|
||||||
|
|
||||||
|
_ARTIFACT_DIR = os.path.join(os.path.dirname(__file__), "artifacts")
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.hookimpl(tryfirst=True, hookwrapper=True)
|
||||||
|
def pytest_runtest_makereport(item, call):
|
||||||
|
outcome = yield
|
||||||
|
rep = outcome.get_result()
|
||||||
|
setattr(item, f"rep_{rep.when}", rep)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def failure_snapshot_dir() -> str:
|
||||||
|
os.makedirs(_ARTIFACT_DIR, exist_ok=True)
|
||||||
|
return _ARTIFACT_DIR
|
||||||
116
tests/desktop/record_screencast.py.sample
Normal file
116
tests/desktop/record_screencast.py.sample
Normal file
@@ -0,0 +1,116 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
"""
|
||||||
|
Screencast 录制入口(模板)。
|
||||||
|
|
||||||
|
启用方式:
|
||||||
|
1. 复制为 record_screencast.py
|
||||||
|
2. 修改 SKILL_SLUG 和 SUBTITLE_SCRIPT
|
||||||
|
3. 决定 test_file 指向哪个测试(默认 test_desktop_smoke.py;
|
||||||
|
有附件场景的用 test_desktop_smoke_with_attachment.py)
|
||||||
|
4. 运行:python tests/desktop/record_screencast.py
|
||||||
|
|
||||||
|
依赖:
|
||||||
|
1. pip install mss
|
||||||
|
2. 系统已安装 ffmpeg 并在 PATH 中
|
||||||
|
3. jiangchang-platform-kit 可推断路径(见 _bootstrap 逻辑)
|
||||||
|
4. MEDIA_ASSETS_ROOT 环境变量指向 media-assets 仓库(或使用默认路径)
|
||||||
|
|
||||||
|
平台 kit v?? 之后 run_screencast 自动激活并最大化匠厂窗口、支持指定显示器、
|
||||||
|
字幕支持每条独立时长。本模板已经利用这些能力,新 skill 复制后无需再内嵌
|
||||||
|
ctypes 激活代码。
|
||||||
|
"""
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import os
|
||||||
|
import sys
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
|
||||||
|
def _bootstrap_screencast() -> None:
|
||||||
|
"""将 jiangchang-platform-kit/tools 加入 sys.path,使 screencast 包可导入。"""
|
||||||
|
here = Path(__file__).resolve()
|
||||||
|
skill_root = here.parents[2]
|
||||||
|
workspace_root = skill_root.parent
|
||||||
|
open_claw_root = workspace_root.parent
|
||||||
|
|
||||||
|
candidates = [
|
||||||
|
workspace_root / "jiangchang-platform-kit" / "tools",
|
||||||
|
open_claw_root / "jiangchang-platform-kit" / "tools",
|
||||||
|
Path(r"D:\AI\jiangchang-platform-kit") / "tools",
|
||||||
|
]
|
||||||
|
env_kit = os.environ.get("JIANGCHANG_KIT_ROOT", "")
|
||||||
|
if env_kit:
|
||||||
|
candidates.insert(0, Path(env_kit) / "tools")
|
||||||
|
|
||||||
|
for cand in candidates:
|
||||||
|
if (cand / "screencast" / "__init__.py").exists():
|
||||||
|
if str(cand) not in sys.path:
|
||||||
|
sys.path.insert(0, str(cand))
|
||||||
|
return
|
||||||
|
|
||||||
|
raise RuntimeError(
|
||||||
|
"找不到 jiangchang-platform-kit/tools/screencast。\n"
|
||||||
|
"请设置 JIANGCHANG_KIT_ROOT 环境变量,或将 platform-kit 放在 workspace "
|
||||||
|
"同级目录。"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
_bootstrap_screencast()
|
||||||
|
|
||||||
|
from screencast import run_screencast # noqa: E402
|
||||||
|
|
||||||
|
# ============================================================
|
||||||
|
# ↓↓↓ 复制后只需修改下面这些 ↓↓↓
|
||||||
|
# ============================================================
|
||||||
|
|
||||||
|
SKILL_SLUG = "your-skill-slug" # 替换为本技能 slug
|
||||||
|
|
||||||
|
# 字幕脚本设计要点(来自 disburse-payroll-icbc 实战经验):
|
||||||
|
# 1. **关键词全用 ASCII**:保险起见绕开 Windows cp936/UTF-8 编码偶发问题。
|
||||||
|
# 虽然 platform-kit 已修了 subprocess 编码,ASCII 关键词仍是最稳的。
|
||||||
|
# 2. **用 "step: N/X" 模式作触发锚**:在测试代码里加 _logger.info("step: 1/5 ...")
|
||||||
|
# 日志作为字幕触发点,能精确控制字幕出现时机,比依赖 SDK 内部日志更可靠。
|
||||||
|
# 3. **关键词每条只触发一次**,所以"step: 1/" 这种唯一前缀比"准备数据"更稳。
|
||||||
|
# 4. **三元组支持每条独立时长**:(关键词, 文案, 显示秒数);
|
||||||
|
# long wait 场景可以让某条字幕停留更久(如 30s)覆盖中间无日志输出的等待期。
|
||||||
|
SUBTITLE_SCRIPT = [
|
||||||
|
# (keyword, text) 二元组用默认时长(5s)
|
||||||
|
("ensure_app_running", "启动匠厂智能体平台"),
|
||||||
|
("wait_gateway_ready", "网关就绪"),
|
||||||
|
("step: 1/", "准备测试数据"),
|
||||||
|
("step: 2/", "执行技能操作"),
|
||||||
|
# (keyword, text, duration) 三元组指定独立时长
|
||||||
|
("step: 3/", "技能在后端持续执行,请稍候...", 30.0),
|
||||||
|
("step: 4/", "结果汇总返回"),
|
||||||
|
("PASSED", "测试通过 ✓"),
|
||||||
|
]
|
||||||
|
|
||||||
|
# 测试文件选择(按需切换):
|
||||||
|
# - test_desktop_smoke.py: 纯问答 skill 用这个
|
||||||
|
# - test_desktop_smoke_with_attachment.py: 需要上传附件的 skill 用这个
|
||||||
|
TEST_FILE_NAME = "test_desktop_smoke.py"
|
||||||
|
|
||||||
|
# 音乐子目录(按 skill 风格选):
|
||||||
|
# - "music/corporate": 商务严肃(金融、办公场景)
|
||||||
|
# - "music/calm": 舒缓(流程演示、配置类)
|
||||||
|
# - "music/upbeat": 活力(成功汇报、营销视频)
|
||||||
|
# - "music"(默认): 三个子目录混选
|
||||||
|
MUSIC_SUBDIR = "music/corporate"
|
||||||
|
|
||||||
|
# ============================================================
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
_here = Path(__file__).resolve()
|
||||||
|
test_file = str(_here.parent / TEST_FILE_NAME)
|
||||||
|
output_dir = str(_here.parent / "artifacts" / "recordings")
|
||||||
|
|
||||||
|
run_screencast(
|
||||||
|
skill_slug=SKILL_SLUG,
|
||||||
|
subtitle_script=SUBTITLE_SCRIPT,
|
||||||
|
pytest_args=[test_file, "-v", "-s"],
|
||||||
|
output_dir=output_dir,
|
||||||
|
music_subdir=MUSIC_SUBDIR,
|
||||||
|
# platform-kit 自动激活+最大化窗口(Windows ctypes / 其他系统协议),
|
||||||
|
# 想关掉传 activate_window=False
|
||||||
|
# monitor_index=1 显式录主屏(多显示器场景推荐)
|
||||||
|
)
|
||||||
75
tests/desktop/test_desktop_smoke.py.sample
Normal file
75
tests/desktop/test_desktop_smoke.py.sample
Normal file
@@ -0,0 +1,75 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
"""
|
||||||
|
可选桌面 E2E 示例(默认不执行:文件名后缀为 .sample)—— **简单问答场景**。
|
||||||
|
|
||||||
|
启用方式:
|
||||||
|
复制本文件为 test_desktop_smoke.py,按目标技能修改提问文案与断言。
|
||||||
|
运行:pytest tests/desktop/test_desktop_smoke.py -v -s
|
||||||
|
|
||||||
|
依赖:pytest、jiangchang_desktop_sdk、匠厂桌面宿主已就绪。
|
||||||
|
|
||||||
|
【何时用此模板】
|
||||||
|
- 技能只需要发一句话给 Main Agent,等回复,做断言(纯问答)
|
||||||
|
- 不涉及上传附件 / 不涉及 RPA 长流程
|
||||||
|
|
||||||
|
【何时不要用此模板,改用 test_desktop_smoke_with_attachment.py.sample】
|
||||||
|
- 需要上传文件(Excel / PDF / 图片)作为附件
|
||||||
|
- 技能内部跑 RPA / 长流程(> 2 分钟),需要精细等待
|
||||||
|
|
||||||
|
【已知 SDK 限制(来自 disburse-payroll-icbc 实战)】
|
||||||
|
- client.ask() 内部对"找到 user 消息节点"只给 15 秒窗口,找不到就锁死
|
||||||
|
user_msg_index=-1,后续等待 assistant 永远 return None。如果你的场景里
|
||||||
|
匠厂启动较慢 / gateway 握手有延迟,client.ask() 可能假死到 timeout。
|
||||||
|
此模板仍用 ask(),因为它对**简单场景**够用。复杂场景请用 _with_attachment.py.sample
|
||||||
|
里的"绕开 ask()"模式。
|
||||||
|
- client.send_file() 在 v2.0.17+ 抛 NotImplementedError,**绝不要调用它**。
|
||||||
|
需要附件请用 _with_attachment.py.sample 里的拖拽方案。
|
||||||
|
"""
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import logging
|
||||||
|
import os
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
from jiangchang_desktop_sdk import AskOptions, JiangchangDesktopClient
|
||||||
|
|
||||||
|
_logger = logging.getLogger("skill-template-desktop-e2e-sample")
|
||||||
|
if not _logger.handlers:
|
||||||
|
_logger.setLevel(logging.INFO)
|
||||||
|
_h = logging.StreamHandler()
|
||||||
|
_h.setFormatter(logging.Formatter("[E2E-sample] %(message)s"))
|
||||||
|
_logger.addHandler(_h)
|
||||||
|
|
||||||
|
_ARTIFACT_DIR = os.path.join(os.path.dirname(__file__), "artifacts")
|
||||||
|
|
||||||
|
|
||||||
|
class TestDesktopSmokeSample:
|
||||||
|
"""最小 smoke:拉起桌面、等待网关、发一条与技能路由相关的问句并做宽松断言。"""
|
||||||
|
|
||||||
|
@pytest.fixture(autouse=True)
|
||||||
|
def connect_and_teardown(self, request):
|
||||||
|
client = JiangchangDesktopClient()
|
||||||
|
_logger.info("ensure_app_running")
|
||||||
|
client.ensure_app_running(wait_timeout_s=30)
|
||||||
|
client.bring_to_front()
|
||||||
|
client.wait_gateway_ready(timeout_ms=30000)
|
||||||
|
self.client = client
|
||||||
|
yield
|
||||||
|
try:
|
||||||
|
rep = getattr(request.node, "rep_call", None)
|
||||||
|
if rep is not None and rep.failed:
|
||||||
|
os.makedirs(_ARTIFACT_DIR, exist_ok=True)
|
||||||
|
paths = client.snapshot(_ARTIFACT_DIR, tag=request.node.name)
|
||||||
|
_logger.error("snapshot: %s", paths)
|
||||||
|
finally:
|
||||||
|
client.disconnect()
|
||||||
|
|
||||||
|
def test_gateway_and_simple_ask(self) -> None:
|
||||||
|
"""请将 SKILL 名与路由关键词改成你复制后的真实技能。"""
|
||||||
|
answer = self.client.ask(
|
||||||
|
"请使用当前已安装技能执行一次 health 检查并返回 OK 或等价结果。",
|
||||||
|
AskOptions(timeout=120000, new_task=True),
|
||||||
|
)
|
||||||
|
assert answer
|
||||||
|
assert "OK" in answer or "ok" in answer.lower() or "健康" in answer
|
||||||
137
tests/desktop/test_desktop_smoke_with_attachment.py.sample
Normal file
137
tests/desktop/test_desktop_smoke_with_attachment.py.sample
Normal file
@@ -0,0 +1,137 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
"""
|
||||||
|
可选桌面 E2E 示例(默认不执行:文件名后缀为 .sample)—— **附件上传 + 长流程场景**。
|
||||||
|
|
||||||
|
启用方式:
|
||||||
|
1. 复制为 test_desktop_smoke_with_attachment.py
|
||||||
|
2. 修改 SKILL_SLUG / PROMPT / 断言为你技能的真实业务
|
||||||
|
3. 准备样例附件,或动态生成(参考 _prepare_sample_attachment)
|
||||||
|
4. 运行:pytest tests/desktop/test_desktop_smoke_with_attachment.py -v -s
|
||||||
|
|
||||||
|
依赖:
|
||||||
|
- pytest / jiangchang_desktop_sdk / 匠厂桌面宿主已就绪
|
||||||
|
- 通过 conftest.py 自动 bootstrap,jiangchang_desktop_sdk.e2e_helpers 模块在 sys.path
|
||||||
|
|
||||||
|
【为什么用此模板而不是 test_desktop_smoke.py.sample】
|
||||||
|
- client.send_file() 在 v2.0.17+ 抛 NotImplementedError,必须用拖拽方式
|
||||||
|
- client.ask() 在 user_idx<0 时会锁死(已知 SDK 缺陷),本模板绕开 ask(),
|
||||||
|
在测试侧自己用 jiangchang_desktop_sdk.e2e_helpers 做等待
|
||||||
|
|
||||||
|
【字幕配合】
|
||||||
|
- 关键节点都打了 _logger.info("step: N/7 ..."),对应 record_screencast.py 的
|
||||||
|
SUBTITLE_SCRIPT 用 step: 关键词触发字幕
|
||||||
|
"""
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import logging
|
||||||
|
import os
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
from jiangchang_desktop_sdk import JiangchangDesktopClient
|
||||||
|
from jiangchang_desktop_sdk.e2e_helpers import (
|
||||||
|
drop_file_into_composer,
|
||||||
|
send_prompt_via_composer,
|
||||||
|
wait_for_agent_complete,
|
||||||
|
wait_for_attachment_ready,
|
||||||
|
wait_for_composer_enabled,
|
||||||
|
wait_for_user_message,
|
||||||
|
)
|
||||||
|
|
||||||
|
# ============================================================
|
||||||
|
# ↓↓↓ 复制后修改这些 ↓↓↓
|
||||||
|
# ============================================================
|
||||||
|
|
||||||
|
SKILL_SLUG = "your-skill-slug"
|
||||||
|
|
||||||
|
PROMPT = (
|
||||||
|
"请使用 /your-skill-slug 处理我刚才上传的文件。"
|
||||||
|
"完成后请用清单方式汇总:[改成你技能要求的字段]。"
|
||||||
|
)
|
||||||
|
|
||||||
|
# 业务断言:根据你的技能输出修改
|
||||||
|
EXPECTED_KEYWORDS_IN_REPLY = ["关键词1", "关键词2"]
|
||||||
|
|
||||||
|
# ============================================================
|
||||||
|
|
||||||
|
_logger = logging.getLogger("desktop-e2e-with-attachment-sample")
|
||||||
|
if not _logger.handlers:
|
||||||
|
_logger.setLevel(logging.INFO)
|
||||||
|
_h = logging.StreamHandler()
|
||||||
|
_h.setFormatter(logging.Formatter("[E2E] %(message)s"))
|
||||||
|
_logger.addHandler(_h)
|
||||||
|
|
||||||
|
_ARTIFACT_DIR = os.path.join(os.path.dirname(__file__), "artifacts")
|
||||||
|
|
||||||
|
|
||||||
|
def _prepare_sample_attachment(tmp_path: Path) -> Path:
|
||||||
|
"""在 tmp_path 下生成一个样例附件(按 skill 真实需要修改)。
|
||||||
|
|
||||||
|
示例:生成一个 5 行的 xlsx。你的 skill 可能需要 CSV / PDF / 图片等其他格式。
|
||||||
|
"""
|
||||||
|
from openpyxl import Workbook # noqa: WPS433
|
||||||
|
xlsx = tmp_path / "sample.xlsx"
|
||||||
|
wb = Workbook()
|
||||||
|
ws = wb.active
|
||||||
|
ws.append(["列1", "列2", "列3"])
|
||||||
|
for i in range(1, 6):
|
||||||
|
ws.append([f"值{i}A", f"值{i}B", i * 100])
|
||||||
|
wb.save(str(xlsx))
|
||||||
|
return xlsx
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def client(request):
|
||||||
|
c = JiangchangDesktopClient()
|
||||||
|
_logger.info("ensure_app_running")
|
||||||
|
c.ensure_app_running(wait_timeout_s=45)
|
||||||
|
c.bring_to_front()
|
||||||
|
c.wait_gateway_ready(timeout_ms=30000)
|
||||||
|
yield c
|
||||||
|
try:
|
||||||
|
rep = getattr(request.node, "rep_call", None)
|
||||||
|
if rep is not None and rep.failed:
|
||||||
|
os.makedirs(_ARTIFACT_DIR, exist_ok=True)
|
||||||
|
paths = c.snapshot(_ARTIFACT_DIR, tag=request.node.name)
|
||||||
|
_logger.error("failure snapshot: %s", paths)
|
||||||
|
finally:
|
||||||
|
c.disconnect()
|
||||||
|
|
||||||
|
|
||||||
|
def test_desktop_with_attachment(client: JiangchangDesktopClient, tmp_path: Path) -> None:
|
||||||
|
_logger.info("step: 1/7 准备样例附件")
|
||||||
|
attachment_path = _prepare_sample_attachment(tmp_path)
|
||||||
|
|
||||||
|
client.new_task()
|
||||||
|
page = client.get_page()
|
||||||
|
client.bring_to_front()
|
||||||
|
|
||||||
|
_logger.info("step: 2/7 拖拽附件到聊天框")
|
||||||
|
drop_file_into_composer(page, attachment_path)
|
||||||
|
|
||||||
|
_logger.info("step: 3/7 等附件 stage-buffer 完成")
|
||||||
|
wait_for_attachment_ready(page, attachment_path.name)
|
||||||
|
|
||||||
|
wait_for_composer_enabled(page)
|
||||||
|
client.bring_to_front()
|
||||||
|
_logger.info("step: 4/7 键入并发送指令")
|
||||||
|
send_prompt_via_composer(page, PROMPT)
|
||||||
|
|
||||||
|
_logger.info("step: 5/7 Main Agent 接收,等待路由到技能")
|
||||||
|
user_idx = wait_for_user_message(page, PROMPT, timeout_s=60.0)
|
||||||
|
|
||||||
|
_logger.info("step: 6/7 技能在后端执行(可能 2-3 分钟)")
|
||||||
|
client.bring_to_front()
|
||||||
|
reply = wait_for_agent_complete(
|
||||||
|
page, user_idx,
|
||||||
|
overall_timeout_s=1200.0,
|
||||||
|
stable_seconds=5.0,
|
||||||
|
)
|
||||||
|
|
||||||
|
_logger.info("step: 7/7 校验回复内容")
|
||||||
|
_logger.info("reply length=%d", len(reply))
|
||||||
|
|
||||||
|
assert reply, "assistant 回复为空"
|
||||||
|
for kw in EXPECTED_KEYWORDS_IN_REPLY:
|
||||||
|
assert kw in reply, f"期望关键词 {kw!r} 未出现在回复中;片段:{reply[:400]!r}"
|
||||||
27
tests/fixtures/README.md
vendored
Normal file
27
tests/fixtures/README.md
vendored
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
# fixtures(脱敏样例)
|
||||||
|
|
||||||
|
本目录存放 **golden case / contract test** 用的输入、期望输出与 **adapter profile 样例**。
|
||||||
|
|
||||||
|
## 规则
|
||||||
|
|
||||||
|
- 只能放**虚构、脱敏、可公开**的数据(演示租户、演示 request_id 等)。
|
||||||
|
- **禁止**:真实客户名、真实订单/提单号、真实报关单、真实 token/cookie、真实内网 URL、生产环境凭证。
|
||||||
|
- `adapter_profiles.sample.yaml` 中的 URL 须使用 `localhost`、`example.invalid` 等明显占位;`credential_ref` 仅表达「从密钥库引用」的约定,**不得**内嵌密钥。
|
||||||
|
|
||||||
|
## 与本仓库测试的对应关系
|
||||||
|
|
||||||
|
| 文件 | 用途 |
|
||||||
|
|------|------|
|
||||||
|
| `sample_request.json` | 通用请求体样例,供 ``tests/samples/test_golden_cases.py.sample`` 演示读取。 |
|
||||||
|
| `expected_response.json` | 与上配套的期望摘要字段(业务复制后按域替换)。 |
|
||||||
|
| `adapter_profiles.sample.yaml` | 五类运行方式(mock / simulator_api / simulator_rpa / real_api / real_rpa)的配置表达样例。 |
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 给 AI 工具的替换规则
|
||||||
|
|
||||||
|
- 可以按业务**重命名字段**、增减键名,但必须保持**虚构、脱敏**;不得把真实客户标识原样搬进仓库。
|
||||||
|
- 可以**新增**更多 fixture 文件,例如 `case_valid_001.json`、`case_missing_required_001.json`,并在测试中通过相对路径读取。
|
||||||
|
- **禁止**将真实企业数据、真实单证截图中的文字、真实报关要素未经处理直接复制进 `fixtures/`。
|
||||||
|
- 若用例来自真实客户样本,须先**脱敏、泛化、替换编号与地名**,再作为模板数据提交。
|
||||||
|
|
||||||
40
tests/fixtures/adapter_profiles.sample.yaml
vendored
Normal file
40
tests/fixtures/adapter_profiles.sample.yaml
vendored
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
# 样例:五类 adapter 运行方式(复制到业务仓后按环境改名;勿提交真实凭证)
|
||||||
|
runtime:
|
||||||
|
profile: mock
|
||||||
|
|
||||||
|
adapters:
|
||||||
|
demo_external_system:
|
||||||
|
interface: demo
|
||||||
|
profile: mock
|
||||||
|
|
||||||
|
mock:
|
||||||
|
target: mock
|
||||||
|
channel: none
|
||||||
|
|
||||||
|
simulator_api:
|
||||||
|
target: simulator
|
||||||
|
channel: api
|
||||||
|
base_url: http://localhost:5180/api/demo
|
||||||
|
timeout_seconds: 10
|
||||||
|
|
||||||
|
simulator_rpa:
|
||||||
|
target: simulator
|
||||||
|
channel: rpa
|
||||||
|
entry_url: http://localhost:5180/select
|
||||||
|
timeout_seconds: 30
|
||||||
|
|
||||||
|
real_api:
|
||||||
|
target: real
|
||||||
|
channel: api
|
||||||
|
base_url: https://example.invalid/api
|
||||||
|
credential_ref: vault://tenant/demo/external-system/api
|
||||||
|
write_enabled: false
|
||||||
|
timeout_seconds: 20
|
||||||
|
|
||||||
|
real_rpa:
|
||||||
|
target: real
|
||||||
|
channel: rpa
|
||||||
|
entry_url: https://example.invalid/portal
|
||||||
|
credential_ref: vault://tenant/demo/external-system/rpa
|
||||||
|
write_enabled: false
|
||||||
|
timeout_seconds: 60
|
||||||
6
tests/fixtures/expected_response.json
vendored
Normal file
6
tests/fixtures/expected_response.json
vendored
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
{
|
||||||
|
"success": true,
|
||||||
|
"data": {
|
||||||
|
"status": "ok"
|
||||||
|
}
|
||||||
|
}
|
||||||
11
tests/fixtures/sample_request.json
vendored
Normal file
11
tests/fixtures/sample_request.json
vendored
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
{
|
||||||
|
"request_id": "req_test_001",
|
||||||
|
"tenant_id": "tenant_demo",
|
||||||
|
"actor": "tester",
|
||||||
|
"parameters": {
|
||||||
|
"input_text": "demo input",
|
||||||
|
"options": {
|
||||||
|
"dry_run": true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
53
tests/integration/README.md
Normal file
53
tests/integration/README.md
Normal file
@@ -0,0 +1,53 @@
|
|||||||
|
# integration(可选集成 / 真实外联)
|
||||||
|
|
||||||
|
本目录**默认不执行**:
|
||||||
|
|
||||||
|
- ``python tests/run_tests.py`` 只收集 ``tests/`` 根目录下 ``test_*.py``,**不会**进入本目录。
|
||||||
|
- 本目录下文件一律为 ``*.py.sample``,pytest / unittest **默认也不会**按普通 ``test_*.py`` 收集。
|
||||||
|
|
||||||
|
## 与 samples 的区别
|
||||||
|
|
||||||
|
| 维度 | `tests/samples/` | `tests/integration/` |
|
||||||
|
|------|------------------|------------------------|
|
||||||
|
| 目的 | 默认可**升级**为根目录 `test_*.py` 的 **业务单元 / service 契约 / golden** 范式 | **外联联调**范式:网络、浏览器、仿真或真实系统 |
|
||||||
|
| 默认执行 | `.sample` 不执行;复制到根目录并改名后才进入默认套件 | **永远不**作为默认 unittest 的一部分;复制后仍需显式环境变量与手动/CI 任务触发 |
|
||||||
|
| 典型内容 | `FakeAdapter`、fixture、隔离 DB | `localhost` 仿真、真实 API(授权)、真实 RPA(最高风险) |
|
||||||
|
|
||||||
|
**原则**:只要会访问**网络**、**浏览器**、**真实或仿真外部系统**,应优先放在 `integration`(并保持 `.sample` 直至团队同意启用),而不是写进默认 `tests/test_*.py`。
|
||||||
|
|
||||||
|
## 何时使用
|
||||||
|
|
||||||
|
| 场景 | 建议 ``OPENCLAW_TEST_TARGET`` | 其它条件 |
|
||||||
|
|------|------------------------------|----------|
|
||||||
|
| 本地 / CI 对接**仿真 HTTP** | ``simulator_api`` | 仿真服务如 ``http://localhost:5180`` |
|
||||||
|
| **仿真页面** / 录播 RPA | ``simulator_rpa`` | 本地页面或沙箱浏览器 profile |
|
||||||
|
| **真实 API**(只读优先) | ``real_api`` | ``ALLOW_REAL_API=1``;写操作另需 ``ALLOW_WRITE_ACTIONS=1`` |
|
||||||
|
| **真实 RPA**(最高风险) | ``real_rpa`` | ``ALLOW_REAL_RPA=1``;**仅手动触发** |
|
||||||
|
|
||||||
|
## 环境变量(与 ``adapter_test_utils`` 一致)
|
||||||
|
|
||||||
|
- **目标档位**(二选一键名,后者为历史拼写):``OPENCLAW_TEST_TARGET`` 或 ``OPENCLOW_TEST_TARGET``
|
||||||
|
取值:``unit`` | ``mock`` | ``simulator_api`` | ``simulator_rpa`` | ``real_api`` | ``real_rpa``
|
||||||
|
默认未设置等价于 ``unit``。
|
||||||
|
|
||||||
|
- **授权开关**(显式 ``1`` 才启用):
|
||||||
|
- ``ALLOW_REAL_API=1``
|
||||||
|
- ``ALLOW_REAL_RPA=1``
|
||||||
|
- ``ALLOW_WRITE_ACTIONS=1``(对写操作 / 提交表单类步骤)
|
||||||
|
|
||||||
|
## 安全约束
|
||||||
|
|
||||||
|
- **禁止**在样例或复制后的测试代码中硬编码真实账号、密码、token、cookie、生产 URL。
|
||||||
|
- 凭证应来自**平台密钥库**、受控环境变量或本机安全配置(``.env`` 已列入 ``tests/.gitignore``,勿提交)。
|
||||||
|
- 真实 RPA 可能对 ERP/TMS/WMS 等产生真实操作;**务必**沙箱账号、只读权限,并由人工触发 CI job。
|
||||||
|
|
||||||
|
## 如何运行某个样例
|
||||||
|
|
||||||
|
1. 将目标 ``*.sample`` 复制为 ``test_*.py``(去掉 ``.sample``)。
|
||||||
|
2. 按技能修改其中的占位 URL / 路由 / 断言。
|
||||||
|
3. 使用 **pytest** 指向该文件(或整目录),并导出上表所需环境变量。
|
||||||
|
|
||||||
|
```powershell
|
||||||
|
$env:OPENCLAW_TEST_TARGET = "simulator_api"
|
||||||
|
pytest tests/integration/test_simulator_api_contract.py -v
|
||||||
|
```
|
||||||
24
tests/integration/test_real_api_contract.py.sample
Normal file
24
tests/integration/test_real_api_contract.py.sample
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
# 样例:复制为 test_real_api_contract.py;需 OPENCLAW_TEST_TARGET=real_api 且 ALLOW_REAL_API=1。
|
||||||
|
"""真实 HTTP API(只读优先;默认跳过)。"""
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import unittest
|
||||||
|
|
||||||
|
from adapter_test_utils import should_skip_profile
|
||||||
|
|
||||||
|
|
||||||
|
class TestRealApiContractSample(unittest.TestCase):
|
||||||
|
def test_skip_by_default(self) -> None:
|
||||||
|
skip, reason = should_skip_profile("real_api", write=False)
|
||||||
|
if skip:
|
||||||
|
raise unittest.SkipTest(reason)
|
||||||
|
# 写操作示例(复制后使用):
|
||||||
|
# skip_w, _ = should_skip_profile("real_api", write=True)
|
||||||
|
# 需要 ALLOW_WRITE_ACTIONS=1
|
||||||
|
# 禁止在此文件写入真实 token;从 vault / 环境注入读取 endpoint。
|
||||||
|
self.assertFalse(skip)
|
||||||
|
|
||||||
|
|
||||||
|
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