完善模板,加入RPA标准
This commit is contained in:
21
.env.example
Normal file
21
.env.example
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
# ── 运行模式(对应 adapter 四档,见 references/ADAPTER.md)──
|
||||||
|
OPENCLAW_TEST_TARGET=simulator_rpa # 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=0 # 1=全程录屏(合规场景建议开)
|
||||||
|
OPENCLAW_ARTIFACTS_ON_FAILURE=1 # 1=失败截图(默认)
|
||||||
|
|
||||||
|
# ── 节流 / 超时 ──
|
||||||
|
STEP_DELAY_MIN=1.0 # 步骤间随机等待下限(秒)
|
||||||
|
STEP_DELAY_MAX=5.0 # 步骤间随机等待上限(秒)
|
||||||
|
HUMAN_WAIT_TIMEOUT=180 # 滑块/验证码/2FA 等人工超时(秒)
|
||||||
3
.gitignore
vendored
3
.gitignore
vendored
@@ -1,3 +1,4 @@
|
|||||||
__pycache__/
|
__pycache__/
|
||||||
*.py[cod]
|
*.py[cod]
|
||||||
.env
|
.env
|
||||||
|
*.env.local
|
||||||
115
references/ADAPTER.md
Normal file
115
references/ADAPTER.md
Normal file
@@ -0,0 +1,115 @@
|
|||||||
|
# 适配器标准:真实/仿真 × API/RPA 四档模式
|
||||||
|
|
||||||
|
> 凡是"连接三方系统"(ERP、CRM、SaaS、银行等)的 skill,都应采用本文的 **adapter 四档模式**。这样同一套业务逻辑可以在不同档位间切换:开发用仿真、CI 用 mock、上线用真实,互不影响。
|
||||||
|
|
||||||
|
## 为什么要分档
|
||||||
|
|
||||||
|
对接一个外部系统,工程上无非四种方式的组合:
|
||||||
|
|
||||||
|
| | API(有接口) | RPA(操作界面) |
|
||||||
|
|---|---|---|
|
||||||
|
| **真实系统** | `real_api` 真实 API | `real_rpa` 真实界面 |
|
||||||
|
| **仿真系统** | `mock` 离线仿真 | `sim_rpa` 仿真平台界面 |
|
||||||
|
|
||||||
|
- **mock**:纯离线、不联网,给单测 / CI / 开发自测,**保证可重复**。
|
||||||
|
- **sim_rpa**:操作仿真平台(如 `sandbox.jc2009.com`),跑真实端到端流程但不碰生产,**默认开发档位**。
|
||||||
|
- **real_api**:有官方接口时**首选**(最稳、最快、最易维护)。
|
||||||
|
- **real_rpa**:没有 API 只能操作生产界面,**风险最高、放最后**。
|
||||||
|
|
||||||
|
> 推荐优先级:**real_api > sim_rpa > real_rpa**,mock 永远保留做 CI。
|
||||||
|
|
||||||
|
## 目录骨架
|
||||||
|
|
||||||
|
业务逻辑只依赖 `base` 里定义的接口与数据契约,具体档位实现各自一个文件:
|
||||||
|
|
||||||
|
```
|
||||||
|
scripts/service/<domain>_adapter/
|
||||||
|
__init__.py # dispatch:按档位返回对应 adapter 实例
|
||||||
|
base.py # 数据契约(dataclass)+ AdapterBase 接口(NotImplementedError)
|
||||||
|
mock.py # 离线仿真,给 CI/单测
|
||||||
|
real_api.py # 真实系统 API
|
||||||
|
sim_rpa.py # 仿真平台 RPA(Playwright/pywinauto/uiautomator2)
|
||||||
|
real_rpa.py # 真实系统 RPA(占位,谨慎实现)
|
||||||
|
```
|
||||||
|
|
||||||
|
> 参考实现:`disburse-payroll-icbc/scripts/service/disburse_adapter/`(已有 `base.py` / `mock.py` / `icbc_simulator_rpa.py`)。
|
||||||
|
> 模板示例:`scripts/service/example_adapter/`(复制改名即用)。
|
||||||
|
|
||||||
|
### 引用方式
|
||||||
|
|
||||||
|
```python
|
||||||
|
from service.example_adapter import get_adapter
|
||||||
|
from service.example_adapter.base import ExampleItem
|
||||||
|
|
||||||
|
adapter = get_adapter() # 由 OPENCLAW_TEST_TARGET 决定档位
|
||||||
|
result = adapter.do_batch(
|
||||||
|
target="https://sandbox.jc2009.com",
|
||||||
|
items=[ExampleItem(id="1", label="demo")],
|
||||||
|
)
|
||||||
|
```
|
||||||
|
|
||||||
|
mock 档位纯离线验证:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
set OPENCLAW_TEST_TARGET=mock
|
||||||
|
python -c "import sys; sys.path.insert(0,'scripts'); from service.example_adapter import get_adapter; from service.example_adapter.base import ExampleItem; r=get_adapter().do_batch('x',[ExampleItem('1','a')]); print(r.ok, r.serial_no)"
|
||||||
|
```
|
||||||
|
|
||||||
|
## base.py 写法(要点)
|
||||||
|
|
||||||
|
1. **数据契约用 `@dataclass`**:输入项(如 `PayrollItem`)、批量结果(如 `BatchResult{ok, serial_no, error_msg, artifacts}`)。
|
||||||
|
2. **接口方法抛 `NotImplementedError`**:所有档位实现同一签名。
|
||||||
|
3. **以"批"为单位返回**:真实 RPA 往往是整批提交,结果按批次返回,不要假设逐条。
|
||||||
|
4. **结果里带 `artifacts`**:截图/录屏/日志路径,便于排查。
|
||||||
|
|
||||||
|
```python
|
||||||
|
@dataclass
|
||||||
|
class XxxResult:
|
||||||
|
ok: bool
|
||||||
|
serial_no: Optional[str]
|
||||||
|
error_msg: Optional[str]
|
||||||
|
artifacts: Dict[str, Any] = field(default_factory=dict)
|
||||||
|
|
||||||
|
class AdapterBase:
|
||||||
|
name = "base"
|
||||||
|
def do_batch(self, target: str, items: list) -> XxxResult:
|
||||||
|
raise NotImplementedError
|
||||||
|
```
|
||||||
|
|
||||||
|
## 档位 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 "simulator_rpa").lower()
|
||||||
|
if target in ("unit", "mock"):
|
||||||
|
return MockAdapter()
|
||||||
|
if target == "real_api":
|
||||||
|
return RealApiAdapter()
|
||||||
|
if target == "real_rpa":
|
||||||
|
return RealRpaAdapter()
|
||||||
|
return SimRpaAdapter() # 默认
|
||||||
|
```
|
||||||
|
|
||||||
|
> 实际可改为读 `.env` 里的运行模式(见 `CONFIG.md`),`OPENCLAW_TEST_TARGET` 作为进程级覆盖优先。
|
||||||
|
|
||||||
|
## 测试要求
|
||||||
|
|
||||||
|
- **mock 档必须能纯离线跑通**,进 CI。
|
||||||
|
- **每个 adapter 单测同一组用例**(契约测试),保证档位间行为一致。
|
||||||
|
- RPA 档的真实联网测试单独标记,不进默认 CI(参考 `tests/desktop/`)。
|
||||||
|
|
||||||
|
## 相关文档
|
||||||
|
|
||||||
|
- `RPA.md` — 三端 RPA 技术选型与拟人/反反爬范式
|
||||||
|
- `CONFIG.md` — `.env` 里如何配置运行模式与目标地址
|
||||||
|
- `TESTING.md` — 测试 target 与隔离体系
|
||||||
99
references/CONFIG.md
Normal file
99
references/CONFIG.md
Normal file
@@ -0,0 +1,99 @@
|
|||||||
|
# 配置标准:`.env` 规范与首次落盘机制
|
||||||
|
|
||||||
|
> 解决"每个 skill 每次都要手动搞一堆环境变量、账号、地址"的痛点。统一约定:**模板内置 `.env.example`,skill 首次运行自动落盘到用户数据目录,之后从那里读。**
|
||||||
|
|
||||||
|
## 核心原则
|
||||||
|
|
||||||
|
1. **`.env.example` 进仓库**:带注释的全量默认配置,是配置项的"单一事实来源"。
|
||||||
|
2. **首次运行自动 copy**:`setup` / `doctor` / 首次 `run` 时,把 `.env.example` 复制到
|
||||||
|
`{CLAW_DATA_ROOT}/{user}/{slug}/.env`(**已存在则不覆盖**,保护用户改过的值)。
|
||||||
|
3. **三级优先级读取**:`进程环境变量` > `数据目录/.env` > `.env.example 默认值`。
|
||||||
|
4. **敏感信息绝不进 `.env`**:密码/密钥/口令走 account-manager 的 `secret-ref`(见下"红线")。
|
||||||
|
|
||||||
|
## 标准配置项
|
||||||
|
|
||||||
|
每个 skill 的 `.env.example` 至少包含这些(按需增减):
|
||||||
|
|
||||||
|
```ini
|
||||||
|
# ── 运行模式(对应 adapter 四档,见 ADAPTER.md)──
|
||||||
|
OPENCLAW_TEST_TARGET=simulator_rpa # 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=0 # 1=全程录屏(合规场景建议开)
|
||||||
|
OPENCLAW_ARTIFACTS_ON_FAILURE=1 # 1=失败截图(默认)
|
||||||
|
|
||||||
|
# ── 节流 / 超时 ──
|
||||||
|
STEP_DELAY_MIN=1.0 # 步骤间随机等待下限(秒)
|
||||||
|
STEP_DELAY_MAX=5.0 # 步骤间随机等待上限(秒)
|
||||||
|
HUMAN_WAIT_TIMEOUT=180 # 滑块/验证码/2FA 等人工超时(秒)
|
||||||
|
```
|
||||||
|
|
||||||
|
## 🚫 红线:敏感信息不进 `.env`
|
||||||
|
|
||||||
|
`.env` 只放**非敏感运行参数**(模式、地址、开关、超时)。密码、密钥、动态口令、token——
|
||||||
|
|
||||||
|
- 走 **account-manager** 注册凭据,用 `--secret-storage env --secret-ref XXX` 引用;
|
||||||
|
- 真实值由宿主/用户通过环境变量注入,**不落任何文件、不进 git**。
|
||||||
|
- 参考 `disburse-payroll-icbc/scripts/cli/cmd_setup.py`:账号写入 account-manager,密码只登记 `secret-ref`(如 `ICBC_SIM_PASSWORD`)。
|
||||||
|
|
||||||
|
`.gitignore` 必须包含:
|
||||||
|
|
||||||
|
```gitignore
|
||||||
|
.env
|
||||||
|
*.env.local
|
||||||
|
```
|
||||||
|
|
||||||
|
## 配置读取层(模板提供)
|
||||||
|
|
||||||
|
模板会提供一个统一 `config.py`,约定行为:
|
||||||
|
|
||||||
|
```python
|
||||||
|
config.get("TARGET_BASE_URL") # 三级优先级解析
|
||||||
|
config.get_bool("OPENCLAW_BROWSER_HEADLESS")
|
||||||
|
config.get_float("STEP_DELAY_MIN", 1.0)
|
||||||
|
config.ensure_env_file() # 首次把 .env.example copy 到数据目录
|
||||||
|
```
|
||||||
|
|
||||||
|
- 启动时调一次 `ensure_env_file()` 完成落盘。
|
||||||
|
- 所有业务代码**只通过 `config.get*` 读配置**,不直接 `os.environ`,便于统一管理与测试覆盖。
|
||||||
|
|
||||||
|
### 引用方式
|
||||||
|
|
||||||
|
实现位于 `jiangchang-platform-kit` 的 `jiangchang_skill_core.config`;模板 vendor 拷贝在 `scripts/jiangchang_skill_core/config.py`:
|
||||||
|
|
||||||
|
```python
|
||||||
|
from jiangchang_skill_core import config
|
||||||
|
from util.constants import SKILL_SLUG
|
||||||
|
from util.runtime_paths import get_skill_root
|
||||||
|
import os
|
||||||
|
|
||||||
|
example = os.path.join(get_skill_root(), ".env.example")
|
||||||
|
config.ensure_env_file(SKILL_SLUG, example)
|
||||||
|
|
||||||
|
headless = config.get_bool("OPENCLAW_BROWSER_HEADLESS")
|
||||||
|
target = config.get("TARGET_BASE_URL")
|
||||||
|
timeout = config.get_int("HUMAN_WAIT_TIMEOUT", 180)
|
||||||
|
```
|
||||||
|
|
||||||
|
## doctor / setup 命令
|
||||||
|
|
||||||
|
每个 skill 应提供自检命令(参考 `disburse` 的 `test_doctor`):
|
||||||
|
|
||||||
|
- `python scripts/main.py doctor`:检查浏览器是否安装、Profile/账号是否就绪、`.env` 是否已落盘、设备(手机)是否连接。
|
||||||
|
- `python scripts/main.py setup`:初始化演示账号、首次落盘 `.env`。
|
||||||
|
|
||||||
|
## 相关文档
|
||||||
|
|
||||||
|
- `RPA.md` — 三端 RPA 标准与各开关含义
|
||||||
|
- `ADAPTER.md` — `OPENCLAW_TEST_TARGET` 四档模式
|
||||||
|
- `RUNTIME.md` — `CLAW_*` 环境变量与数据目录约定
|
||||||
@@ -152,9 +152,28 @@ scripts/
|
|||||||
作用:常量、日志、路径、时间工具、通用帮助函数
|
作用:常量、日志、路径、时间工具、通用帮助函数
|
||||||
|
|
||||||
- `jiangchang_skill_core/`
|
- `jiangchang_skill_core/`
|
||||||
作用:运行时环境与统一日志副本
|
作用:运行时环境、统一日志、配置读取、RPA 共享库(vendor 自 platform-kit)
|
||||||
一般按现有规范技能复制,不要自己乱改结构
|
一般按现有规范技能复制,不要自己乱改结构
|
||||||
|
|
||||||
|
## 3.2 开发 RPA 类 skill
|
||||||
|
|
||||||
|
若 skill 需要浏览器/桌面/手机自动化,按以下顺序落地:
|
||||||
|
|
||||||
|
1. **先读三份标准**:`references/RPA.md`(三端范式与反反爬)、`references/CONFIG.md`(`.env` 落盘与读取)、`references/ADAPTER.md`(四档 adapter)。
|
||||||
|
2. **从模板复制骨架**:
|
||||||
|
- `scripts/service/example_adapter/` → 改名为 `scripts/service/<domain>_adapter/`
|
||||||
|
- 参考 `scripts/service/example_browser_rpa.py` 写浏览器会话逻辑
|
||||||
|
3. **只用共享库,不在 skill 里重写反反爬**:
|
||||||
|
```python
|
||||||
|
from jiangchang_skill_core import config
|
||||||
|
from jiangchang_skill_core.rpa import launch_persistent_browser, anti_detect, wait_for_captcha_pass
|
||||||
|
```
|
||||||
|
vendor 源码在 `scripts/jiangchang_skill_core/rpa/`(与 `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` 依赖规范
|
### 3.1 `requirements.txt` 依赖规范
|
||||||
|
|
||||||
技能根目录的 `requirements.txt` 是**标准文件**,用于声明本技能特有的 Python 三方依赖。
|
技能根目录的 `requirements.txt` 是**标准文件**,用于声明本技能特有的 Python 三方依赖。
|
||||||
|
|||||||
@@ -46,3 +46,19 @@ description: "规范化的新技能模板说明;复制后替换名称、CLI
|
|||||||
- 旧入口 `scripts/skill_main.py`
|
- 旧入口 `scripts/skill_main.py`
|
||||||
|
|
||||||
新模板统一使用 `scripts/main.py` 作为入口。
|
新模板统一使用 `scripts/main.py` 作为入口。
|
||||||
|
|
||||||
|
## 参考文档索引
|
||||||
|
|
||||||
|
| 文档 | 内容 |
|
||||||
|
|------|------|
|
||||||
|
| `DEVELOPMENT.md` | 从复制模板到开发出新 skill 的完整教程 |
|
||||||
|
| `RUNTIME.md` | 运行时目录结构与 `CLAW_*` 环境变量约定 |
|
||||||
|
| `CLI.md` | CLI 命令与参数示例 |
|
||||||
|
| `SCHEMA.md` | task_logs 与字段映射说明 |
|
||||||
|
| `TESTING.md` | 测试 target 与隔离体系 |
|
||||||
|
| **`RPA.md`** | **三端 RPA 标准(浏览器/桌面/手机):选型、拟人操作、反反爬、人工兜底、存证** |
|
||||||
|
| **`ADAPTER.md`** | **真实/仿真 × API/RPA 四档适配器模式** |
|
||||||
|
| **`CONFIG.md`** | **`.env` 配置规范与首次自动落盘机制** |
|
||||||
|
| `REQUIREMENTS.md` | 依赖与运行要求 |
|
||||||
|
|
||||||
|
> 开发"操作浏览器/桌面/手机"或"对接 ERP/CRM/SaaS/银行"类 skill 前,**务必先读 `RPA.md` + `ADAPTER.md` + `CONFIG.md`** 这三份标准,避免重复踩坑。
|
||||||
|
|||||||
171
references/RPA.md
Normal file
171
references/RPA.md
Normal file
@@ -0,0 +1,171 @@
|
|||||||
|
# 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`、`disburse-payroll-icbc`)。
|
||||||
|
|
||||||
|
| 项 | 标准 |
|
||||||
|
|----|------|
|
||||||
|
| 驱动 | Playwright(Python) |
|
||||||
|
| 浏览器 | 系统已装 Chrome/Edge,`channel="chrome"`;**不用内置 Chromium,无需 `playwright install`** |
|
||||||
|
| 登录态 | `launch_persistent_context` + 用户 Profile 目录(由 account-manager 下发) |
|
||||||
|
| 模式 | 默认有头;`OPENCLAW_BROWSER_HEADLESS=1` 转无头 |
|
||||||
|
| 反反爬 | stealth 指纹注入 + 拟人输入/鼠标/延迟 + 滑块停下等人工(见下) |
|
||||||
|
|
||||||
|
### 1.1 防反爬三件套(必做)
|
||||||
|
|
||||||
|
1. **指纹淡化(stealth)**:启动时 `add_init_script` 注入,抹平自动化特征——
|
||||||
|
`navigator.webdriver=undefined`、`chrome.runtime`、`permissions.query`、`plugins`、`languages`、`hardwareConcurrency`、`deviceMemory`、WebGL vendor。
|
||||||
|
启动参数 `--disable-blink-features=AutomationControlled` + `ignore_default_args=["--enable-automation"]`。
|
||||||
|
开关:`OPENCLAW_PLAYWRIGHT_STEALTH`(默认开)。
|
||||||
|
2. **拟人操作**:
|
||||||
|
- 输入:逐字符 `keyboard.type(delay=90~240ms)`,**先真实点击聚焦再输入,绝不 `el.value=`**。
|
||||||
|
- 鼠标:贝塞尔曲线轨迹移动 + 微抖动;进场随机晃动 2~4 次。
|
||||||
|
- 导航:用真实点击触发,**不要 `window.location.href=` / `el.click()` 这类 JS 跳转/点击**。
|
||||||
|
- 翻页:真实点击翻页箭头,注意排除禁用态选择器。
|
||||||
|
- 延迟:每步之间 `random_delay`。
|
||||||
|
3. **风控页 = 停下等人工**:检测到滑块/拦截(URL 含 `punish/x5sec/baxia/tmd`,或 DOM 命中 `#nocaptcha/.nc-container/punish-component` 等)→ 抛 `ERROR:CAPTCHA_NEED_HUMAN`,轮询等人工通过或超时,**不自动操作滑块**。
|
||||||
|
|
||||||
|
> 参考实现:`1688-scrape-contacts/scripts/service/anti_detect.py`、`util/playwright_stealth.py`、`service/login_service.py`(验证码等待)。已提升为共享库 `jiangchang_skill_core.rpa`(见 platform-kit)。
|
||||||
|
|
||||||
|
### 1.2 安装
|
||||||
|
|
||||||
|
```bash
|
||||||
|
pip install playwright # 仅装包;用系统 Chrome,无需 playwright install chromium
|
||||||
|
```
|
||||||
|
|
||||||
|
### 引用方式
|
||||||
|
|
||||||
|
共享实现位于 `jiangchang-platform-kit` 的 `jiangchang_skill_core.rpa`;模板通过 vendor 拷贝引用:
|
||||||
|
|
||||||
|
```python
|
||||||
|
# scripts/jiangchang_skill_core/rpa/ — vendored from platform-kit
|
||||||
|
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
|
||||||
|
```
|
||||||
|
|
||||||
|
完整示例见 `scripts/service/example_browser_rpa.py` 与 `scripts/service/example_adapter/sim_rpa.py`。
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 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. 存证规范
|
||||||
|
|
||||||
|
- **路径**:`{CLAW_DATA_ROOT}/{user}/{slug}/rpa-artifacts/{batch_id}/{tag}_{timestamp}.png`
|
||||||
|
- **失败必截图**:受 `OPENCLAW_ARTIFACTS_ON_FAILURE`(默认开)控制。
|
||||||
|
- **全程录屏**:受 `OPENCLAW_RECORD_VIDEO`(默认关)控制;合规场景(如代发工资)建议开。浏览器用 Playwright `record_video_dir`。
|
||||||
|
- **常见 tag**:`before_submit` / `after_submit` / `captcha` / `login_fail` / `error`。
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 相关文档
|
||||||
|
|
||||||
|
- `ADAPTER.md` — 真实/仿真 × API/RPA 的四档适配器模式
|
||||||
|
- `CONFIG.md` — `.env` 配置规范与首次落盘机制
|
||||||
|
- `RUNTIME.md` — 运行时目录与环境变量约定
|
||||||
@@ -3,6 +3,5 @@
|
|||||||
# Platform-provided shared dependencies are installed by Jiangchang runtime.
|
# Platform-provided shared dependencies are installed by Jiangchang runtime.
|
||||||
# Add only dependencies required by this skill.
|
# Add only dependencies required by this skill.
|
||||||
#
|
#
|
||||||
# Examples:
|
# Browser RPA: uses system Chrome/Edge; no `playwright install chromium` needed.
|
||||||
# requests>=2.31.0,<3
|
playwright>=1.42.0,<2
|
||||||
# python-dotenv>=1.0.0,<2
|
|
||||||
|
|||||||
@@ -1 +1,16 @@
|
|||||||
# Vendored from jiangchang-platform-kit/sdk/jiangchang_skill_core/ — keep runtime_env + unified_logging in sync.
|
# Vendored from jiangchang-platform-kit/sdk/jiangchang_skill_core/ — keep runtime_env + unified_logging + config + rpa in sync.
|
||||||
|
from .config import ensure_env_file, get, get_bool, get_float, get_int
|
||||||
|
|
||||||
|
try:
|
||||||
|
from . import rpa
|
||||||
|
except ImportError:
|
||||||
|
rpa = None # type: ignore[assignment,misc]
|
||||||
|
|
||||||
|
__all__ = [
|
||||||
|
"ensure_env_file",
|
||||||
|
"get",
|
||||||
|
"get_bool",
|
||||||
|
"get_float",
|
||||||
|
"get_int",
|
||||||
|
"rpa",
|
||||||
|
]
|
||||||
|
|||||||
126
scripts/jiangchang_skill_core/config.py
Normal file
126
scripts/jiangchang_skill_core/config.py
Normal file
@@ -0,0 +1,126 @@
|
|||||||
|
# vendored from jiangchang-platform-kit; do not edit here, edit the kit.
|
||||||
|
"""三级优先级配置读取 + 首次 .env 落盘(进程 env > 数据目录 .env > .env.example)。"""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import os
|
||||||
|
import shutil
|
||||||
|
from typing import Any
|
||||||
|
|
||||||
|
from .runtime_env import get_data_root, get_user_id
|
||||||
|
|
||||||
|
_skill_slug: str | None = None
|
||||||
|
_example_path: str | None = None
|
||||||
|
_env_file_path: str | None = None
|
||||||
|
_user_env_cache: dict[str, str] | None = None
|
||||||
|
_example_defaults_cache: dict[str, str] | None = None
|
||||||
|
|
||||||
|
|
||||||
|
def _parse_env_file(path: str) -> dict[str, str]:
|
||||||
|
"""标准库手写 .env 解析(不引 python-dotenv)。"""
|
||||||
|
result: dict[str, str] = {}
|
||||||
|
if not path or not os.path.isfile(path):
|
||||||
|
return result
|
||||||
|
with open(path, encoding="utf-8") as f:
|
||||||
|
for raw_line in f:
|
||||||
|
line = raw_line.strip()
|
||||||
|
if not line or line.startswith("#"):
|
||||||
|
continue
|
||||||
|
if "=" not in line:
|
||||||
|
continue
|
||||||
|
key, _, value = line.partition("=")
|
||||||
|
key = key.strip()
|
||||||
|
value = value.strip()
|
||||||
|
if not key:
|
||||||
|
continue
|
||||||
|
if len(value) >= 2 and value[0] == value[-1] and value[0] in ('"', "'"):
|
||||||
|
value = value[1:-1]
|
||||||
|
elif " #" in value:
|
||||||
|
value = value.split(" #", 1)[0].strip()
|
||||||
|
result[key] = value
|
||||||
|
return result
|
||||||
|
|
||||||
|
|
||||||
|
def _get_user_env() -> dict[str, str]:
|
||||||
|
global _user_env_cache
|
||||||
|
if _user_env_cache is not None:
|
||||||
|
return _user_env_cache
|
||||||
|
if _env_file_path and os.path.isfile(_env_file_path):
|
||||||
|
_user_env_cache = _parse_env_file(_env_file_path)
|
||||||
|
else:
|
||||||
|
_user_env_cache = {}
|
||||||
|
return _user_env_cache
|
||||||
|
|
||||||
|
|
||||||
|
def _get_example_defaults() -> dict[str, str]:
|
||||||
|
global _example_defaults_cache
|
||||||
|
if _example_defaults_cache is not None:
|
||||||
|
return _example_defaults_cache
|
||||||
|
if _example_path and os.path.isfile(_example_path):
|
||||||
|
_example_defaults_cache = _parse_env_file(_example_path)
|
||||||
|
else:
|
||||||
|
_example_defaults_cache = {}
|
||||||
|
return _example_defaults_cache
|
||||||
|
|
||||||
|
|
||||||
|
def reset_cache() -> None:
|
||||||
|
"""测试用:清空解析缓存。"""
|
||||||
|
global _user_env_cache, _example_defaults_cache
|
||||||
|
_user_env_cache = None
|
||||||
|
_example_defaults_cache = None
|
||||||
|
|
||||||
|
|
||||||
|
def ensure_env_file(skill_slug: str, example_path: str) -> str:
|
||||||
|
"""首次把 .env.example copy 到 {data_root}/{user}/{slug}/.env(已存在不覆盖),返回路径。"""
|
||||||
|
global _skill_slug, _example_path, _env_file_path
|
||||||
|
_skill_slug = skill_slug
|
||||||
|
_example_path = os.path.abspath(example_path)
|
||||||
|
dest_dir = os.path.join(get_data_root(), get_user_id(), skill_slug)
|
||||||
|
os.makedirs(dest_dir, exist_ok=True)
|
||||||
|
dest = os.path.join(dest_dir, ".env")
|
||||||
|
_env_file_path = dest
|
||||||
|
if not os.path.isfile(dest) and os.path.isfile(_example_path):
|
||||||
|
shutil.copy2(_example_path, dest)
|
||||||
|
reset_cache()
|
||||||
|
return dest
|
||||||
|
|
||||||
|
|
||||||
|
def get(key: str, default: Any = None) -> str | None:
|
||||||
|
"""进程环境变量 > 数据目录 .env > .env.example 默认值。"""
|
||||||
|
env_val = os.environ.get(key)
|
||||||
|
if env_val is not None and env_val != "":
|
||||||
|
return env_val
|
||||||
|
user_val = _get_user_env().get(key)
|
||||||
|
if user_val is not None and user_val != "":
|
||||||
|
return user_val
|
||||||
|
example_val = _get_example_defaults().get(key)
|
||||||
|
if example_val is not None and example_val != "":
|
||||||
|
return example_val
|
||||||
|
return default
|
||||||
|
|
||||||
|
|
||||||
|
def get_bool(key: str, default: bool = False) -> bool:
|
||||||
|
val = get(key)
|
||||||
|
if val is None:
|
||||||
|
return default
|
||||||
|
return str(val).strip().lower() in ("1", "true", "yes", "on")
|
||||||
|
|
||||||
|
|
||||||
|
def get_float(key: str, default: float) -> float:
|
||||||
|
val = get(key)
|
||||||
|
if val is None:
|
||||||
|
return default
|
||||||
|
try:
|
||||||
|
return float(val)
|
||||||
|
except (TypeError, ValueError):
|
||||||
|
return default
|
||||||
|
|
||||||
|
|
||||||
|
def get_int(key: str, default: int) -> int:
|
||||||
|
val = get(key)
|
||||||
|
if val is None:
|
||||||
|
return default
|
||||||
|
try:
|
||||||
|
return int(val)
|
||||||
|
except (TypeError, ValueError):
|
||||||
|
return default
|
||||||
60
scripts/jiangchang_skill_core/rpa/__init__.py
Normal file
60
scripts/jiangchang_skill_core/rpa/__init__.py
Normal file
@@ -0,0 +1,60 @@
|
|||||||
|
# vendored from jiangchang-platform-kit; do not edit here, edit the kit.
|
||||||
|
"""浏览器 RPA 共享库:stealth、拟人操作、启动封装、HITL 等待、失败存证。"""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from . import anti_detect, artifacts, errors, human_login, stealth
|
||||||
|
|
||||||
|
try:
|
||||||
|
from .browser import launch_persistent_browser
|
||||||
|
except ImportError:
|
||||||
|
launch_persistent_browser = None # type: ignore[misc, assignment]
|
||||||
|
|
||||||
|
__all__ = [
|
||||||
|
"anti_detect",
|
||||||
|
"artifacts",
|
||||||
|
"errors",
|
||||||
|
"human_login",
|
||||||
|
"stealth",
|
||||||
|
"launch_persistent_browser",
|
||||||
|
# re-export common anti_detect helpers
|
||||||
|
"random_delay",
|
||||||
|
"human_delay_short",
|
||||||
|
"human_delay_page",
|
||||||
|
"human_delay_batch",
|
||||||
|
"human_mouse_move",
|
||||||
|
"human_mouse_wiggle",
|
||||||
|
"human_click",
|
||||||
|
"human_type",
|
||||||
|
"human_scroll",
|
||||||
|
# human_login
|
||||||
|
"wait_for_captcha_pass",
|
||||||
|
"wait_for_login",
|
||||||
|
"DEFAULT_CAPTCHA_MARKERS",
|
||||||
|
# artifacts
|
||||||
|
"artifact_path",
|
||||||
|
"capture_failure",
|
||||||
|
# stealth
|
||||||
|
"STEALTH_INIT_SCRIPT",
|
||||||
|
"stealth_enabled",
|
||||||
|
"persistent_context_launch_parts",
|
||||||
|
]
|
||||||
|
|
||||||
|
from .anti_detect import (
|
||||||
|
human_click,
|
||||||
|
human_delay_batch,
|
||||||
|
human_delay_page,
|
||||||
|
human_delay_short,
|
||||||
|
human_mouse_move,
|
||||||
|
human_mouse_wiggle,
|
||||||
|
human_scroll,
|
||||||
|
human_type,
|
||||||
|
random_delay,
|
||||||
|
)
|
||||||
|
from .artifacts import artifact_path, capture_failure
|
||||||
|
from .human_login import DEFAULT_CAPTCHA_MARKERS, wait_for_captcha_pass, wait_for_login
|
||||||
|
from .stealth import (
|
||||||
|
STEALTH_INIT_SCRIPT,
|
||||||
|
persistent_context_launch_parts,
|
||||||
|
stealth_enabled,
|
||||||
|
)
|
||||||
216
scripts/jiangchang_skill_core/rpa/anti_detect.py
Normal file
216
scripts/jiangchang_skill_core/rpa/anti_detect.py
Normal file
@@ -0,0 +1,216 @@
|
|||||||
|
# vendored from jiangchang-platform-kit; do not edit here, edit the kit.
|
||||||
|
"""防反爬:随机延迟、人工鼠标轨迹模拟、拟人滚动。
|
||||||
|
|
||||||
|
所有延迟使用 random.uniform() + asyncio.sleep()。
|
||||||
|
鼠标移动使用三次贝塞尔曲线 + 随机抖动,避免机械直线轨迹。
|
||||||
|
"""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import asyncio
|
||||||
|
import math
|
||||||
|
import random
|
||||||
|
from typing import Optional
|
||||||
|
|
||||||
|
|
||||||
|
# ── 随机延迟 ────────────────────────────────────────────────
|
||||||
|
|
||||||
|
|
||||||
|
async def random_delay(min_s: float, max_s: float) -> float:
|
||||||
|
"""随机延迟 min_s ~ max_s 秒,返回实际延迟秒数。"""
|
||||||
|
delay = random.uniform(min_s, max_s)
|
||||||
|
await asyncio.sleep(delay)
|
||||||
|
return delay
|
||||||
|
|
||||||
|
|
||||||
|
async def human_delay_short() -> float:
|
||||||
|
"""页面内操作间短延迟 0.5~2 秒(点击/滚动)。"""
|
||||||
|
return await random_delay(0.5, 2.0)
|
||||||
|
|
||||||
|
|
||||||
|
async def human_delay_page(min_s: float = 2, max_s: float = 8) -> float:
|
||||||
|
"""页面间/店铺间切换延迟(可配置范围)。"""
|
||||||
|
return await random_delay(min_s, max_s)
|
||||||
|
|
||||||
|
|
||||||
|
async def human_delay_batch(min_s: float = 5, max_s: float = 15) -> float:
|
||||||
|
"""批次间/翻页间长延迟。"""
|
||||||
|
return await random_delay(min_s, max_s)
|
||||||
|
|
||||||
|
|
||||||
|
# ── 鼠标轨迹 ────────────────────────────────────────────────
|
||||||
|
|
||||||
|
|
||||||
|
async def human_mouse_move(
|
||||||
|
page,
|
||||||
|
target_x: float,
|
||||||
|
target_y: float,
|
||||||
|
steps: Optional[int] = None,
|
||||||
|
) -> None:
|
||||||
|
"""模拟人工鼠标移动到目标坐标。
|
||||||
|
|
||||||
|
使用三次贝塞尔曲线生成 S/C 形路径,中间控制点随机偏移,
|
||||||
|
每步添加微小随机抖动,避免完美直线移动。
|
||||||
|
|
||||||
|
Args:
|
||||||
|
page: Playwright Page 对象。
|
||||||
|
target_x: 目标 X 坐标。
|
||||||
|
target_y: 目标 Y 坐标。
|
||||||
|
steps: 移动步数(None 时按距离自动计算)。
|
||||||
|
"""
|
||||||
|
if steps is None:
|
||||||
|
dist = math.hypot(target_x, target_y)
|
||||||
|
steps = max(8, int(dist / 15))
|
||||||
|
|
||||||
|
# 随机起始点(模拟鼠标从页面其他位置移动过来)
|
||||||
|
start_x = target_x + random.uniform(-300, 300)
|
||||||
|
start_y = target_y + random.uniform(-200, 200)
|
||||||
|
|
||||||
|
# 两个贝塞尔控制点(制造弯曲路径)
|
||||||
|
dx = target_x - start_x
|
||||||
|
dy = target_y - start_y
|
||||||
|
|
||||||
|
cp1_x = start_x + dx * random.uniform(0.2, 0.5) + random.uniform(-120, 120)
|
||||||
|
cp1_y = start_y + dy * random.uniform(0.1, 0.4) + random.uniform(-100, 100)
|
||||||
|
cp2_x = start_x + dx * random.uniform(0.5, 0.8) + random.uniform(-100, 100)
|
||||||
|
cp2_y = start_y + dy * random.uniform(0.4, 0.7) + random.uniform(-80, 80)
|
||||||
|
|
||||||
|
for i in range(steps + 1):
|
||||||
|
t = i / steps
|
||||||
|
# 三次贝塞尔: B(t) = (1-t)³·P0 + 3(1-t)²·t·P1 + 3(1-t)·t²·P2 + t³·P3
|
||||||
|
u = 1 - t
|
||||||
|
x = (
|
||||||
|
(u ** 3) * start_x
|
||||||
|
+ 3 * (u ** 2) * t * cp1_x
|
||||||
|
+ 3 * u * (t ** 2) * cp2_x
|
||||||
|
+ (t ** 3) * target_x
|
||||||
|
)
|
||||||
|
y = (
|
||||||
|
(u ** 3) * start_y
|
||||||
|
+ 3 * (u ** 2) * t * cp1_y
|
||||||
|
+ 3 * u * (t ** 2) * cp2_y
|
||||||
|
+ (t ** 3) * target_y
|
||||||
|
)
|
||||||
|
|
||||||
|
# 微小随机抖动
|
||||||
|
x += random.uniform(-1.5, 1.5)
|
||||||
|
y += random.uniform(-1.5, 1.5)
|
||||||
|
|
||||||
|
await page.mouse.move(x, y)
|
||||||
|
await asyncio.sleep(random.uniform(0.003, 0.015))
|
||||||
|
|
||||||
|
# 最终精确到位
|
||||||
|
await page.mouse.move(target_x, target_y)
|
||||||
|
|
||||||
|
|
||||||
|
async def human_click(
|
||||||
|
page,
|
||||||
|
selector: Optional[str] = None,
|
||||||
|
*,
|
||||||
|
x: Optional[float] = None,
|
||||||
|
y: Optional[float] = None,
|
||||||
|
) -> None:
|
||||||
|
"""拟人点击:贝塞尔轨迹移动 → 微延迟 → 点击。
|
||||||
|
|
||||||
|
支持两种定位方式:
|
||||||
|
- selector: CSS 选择器,自动取元素中心附近随机偏移
|
||||||
|
- x, y: 直接指定坐标(自动加微小随机偏移)
|
||||||
|
|
||||||
|
Args:
|
||||||
|
page: Playwright Page 对象。
|
||||||
|
selector: CSS 选择器(与 x/y 二选一)。
|
||||||
|
x: 目标 X 坐标。
|
||||||
|
y: 目标 Y 坐标。
|
||||||
|
"""
|
||||||
|
if selector:
|
||||||
|
locator = page.locator(selector).first
|
||||||
|
box = await locator.bounding_box()
|
||||||
|
if box is None:
|
||||||
|
raise ValueError(f"无法获取元素边界: {selector}")
|
||||||
|
target_x = box["x"] + box["width"] * random.uniform(0.3, 0.7)
|
||||||
|
target_y = box["y"] + box["height"] * random.uniform(0.3, 0.7)
|
||||||
|
elif x is not None and y is not None:
|
||||||
|
target_x = x + random.uniform(-3, 3)
|
||||||
|
target_y = y + random.uniform(-3, 3)
|
||||||
|
else:
|
||||||
|
raise ValueError("必须提供 selector 或 (x, y) 坐标")
|
||||||
|
|
||||||
|
await human_mouse_move(page, target_x, target_y)
|
||||||
|
await human_delay_short()
|
||||||
|
await page.mouse.click(target_x, target_y)
|
||||||
|
|
||||||
|
|
||||||
|
# ── 滚动模拟 ────────────────────────────────────────────────
|
||||||
|
|
||||||
|
|
||||||
|
async def human_scroll(
|
||||||
|
page, direction: str = "down", amount: Optional[int] = None
|
||||||
|
) -> None:
|
||||||
|
"""模拟人工滚动(分段 + 变速 + 微延迟)。
|
||||||
|
|
||||||
|
Args:
|
||||||
|
page: Playwright Page 对象。
|
||||||
|
direction: "up" 或 "down"。
|
||||||
|
amount: 滚动像素总量,默认随机 300~800。
|
||||||
|
"""
|
||||||
|
if amount is None:
|
||||||
|
amount = random.randint(300, 800)
|
||||||
|
|
||||||
|
sign = -1 if direction == "up" else 1
|
||||||
|
remaining = amount
|
||||||
|
step_count = random.randint(3, 7)
|
||||||
|
|
||||||
|
for i in range(step_count):
|
||||||
|
step = remaining // (step_count - i) + random.randint(-30, 30)
|
||||||
|
step = max(10, min(step, remaining))
|
||||||
|
remaining -= step
|
||||||
|
|
||||||
|
await page.mouse.wheel(0, sign * step)
|
||||||
|
await asyncio.sleep(random.uniform(0.05, 0.2))
|
||||||
|
|
||||||
|
if remaining > 0:
|
||||||
|
await page.mouse.wheel(0, sign * remaining)
|
||||||
|
|
||||||
|
await human_delay_short()
|
||||||
|
|
||||||
|
|
||||||
|
# ── 进场鼠标随机晃动 ────────────────────────────────────────
|
||||||
|
|
||||||
|
|
||||||
|
async def human_mouse_wiggle(page) -> None:
|
||||||
|
"""页面进入后随机移动鼠标 2~4 次(贝塞尔轨迹),模拟真人到场环顾。"""
|
||||||
|
try:
|
||||||
|
vp = page.viewport_size or {"width": 1280, "height": 800}
|
||||||
|
except Exception:
|
||||||
|
vp = {"width": 1280, "height": 800}
|
||||||
|
w, h = vp.get("width", 1280), vp.get("height", 800)
|
||||||
|
for _ in range(random.randint(2, 4)):
|
||||||
|
tx = random.uniform(w * 0.2, w * 0.8)
|
||||||
|
ty = random.uniform(h * 0.2, h * 0.7)
|
||||||
|
await human_mouse_move(page, tx, ty)
|
||||||
|
await asyncio.sleep(random.uniform(0.2, 0.8))
|
||||||
|
|
||||||
|
|
||||||
|
# ── 拟人逐字输入 ────────────────────────────────────────────
|
||||||
|
|
||||||
|
|
||||||
|
async def human_type(page, locator, text: str) -> None:
|
||||||
|
"""真人式输入:贝塞尔移动到输入框 → 真实点击聚焦 → 逐字符 keyboard.type。
|
||||||
|
|
||||||
|
全程使用 isTrusted=true 的可信事件,绝不用 JS 设置 value。
|
||||||
|
"""
|
||||||
|
box = await locator.bounding_box()
|
||||||
|
if box is None:
|
||||||
|
raise ValueError("human_type: 无法获取输入框边界")
|
||||||
|
tx = box["x"] + box["width"] * random.uniform(0.3, 0.7)
|
||||||
|
ty = box["y"] + box["height"] * random.uniform(0.3, 0.7)
|
||||||
|
await human_mouse_move(page, tx, ty)
|
||||||
|
await asyncio.sleep(random.uniform(0.15, 0.5))
|
||||||
|
await page.mouse.click(tx, ty)
|
||||||
|
await asyncio.sleep(random.uniform(0.2, 0.6))
|
||||||
|
# 清空已有内容(用键盘,不用 JS)
|
||||||
|
await page.keyboard.press("Control+A")
|
||||||
|
await page.keyboard.press("Delete")
|
||||||
|
await asyncio.sleep(random.uniform(0.2, 0.5))
|
||||||
|
for ch in text:
|
||||||
|
await page.keyboard.type(ch, delay=random.uniform(90, 240))
|
||||||
29
scripts/jiangchang_skill_core/rpa/artifacts.py
Normal file
29
scripts/jiangchang_skill_core/rpa/artifacts.py
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
# vendored from jiangchang-platform-kit; do not edit here, edit the kit.
|
||||||
|
"""RPA 失败存证路径与截图。"""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import os
|
||||||
|
from datetime import datetime
|
||||||
|
|
||||||
|
|
||||||
|
def _artifacts_enabled() -> bool:
|
||||||
|
v = (os.environ.get("OPENCLAW_ARTIFACTS_ON_FAILURE") or "1").strip().lower()
|
||||||
|
return v not in ("0", "false", "no", "off")
|
||||||
|
|
||||||
|
|
||||||
|
def artifact_path(data_dir: str, batch_id: str, tag: str, ext: str = "png") -> str:
|
||||||
|
"""返回 {data_dir}/rpa-artifacts/{batch_id}/{tag}_{timestamp}.{ext}"""
|
||||||
|
ts = datetime.now().strftime("%Y%m%d_%H%M%S")
|
||||||
|
directory = os.path.join(data_dir, "rpa-artifacts", batch_id)
|
||||||
|
os.makedirs(directory, exist_ok=True)
|
||||||
|
return os.path.join(directory, f"{tag}_{ts}.{ext}")
|
||||||
|
|
||||||
|
|
||||||
|
async def capture_failure(page, data_dir: str, batch_id: str, tag: str) -> str:
|
||||||
|
"""受 OPENCLAW_ARTIFACTS_ON_FAILURE 控制,截图并返回路径;禁用时返回空字符串。"""
|
||||||
|
if not _artifacts_enabled():
|
||||||
|
return ""
|
||||||
|
path = artifact_path(data_dir, batch_id, tag, ext="png")
|
||||||
|
await page.screenshot(path=path, full_page=True)
|
||||||
|
return path
|
||||||
62
scripts/jiangchang_skill_core/rpa/browser.py
Normal file
62
scripts/jiangchang_skill_core/rpa/browser.py
Normal file
@@ -0,0 +1,62 @@
|
|||||||
|
# vendored from jiangchang-platform-kit; do not edit here, edit the kit.
|
||||||
|
"""统一 persistent context 启动封装。"""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import os
|
||||||
|
from typing import TYPE_CHECKING
|
||||||
|
|
||||||
|
from ..runtime_env import find_chrome_executable
|
||||||
|
from .stealth import (
|
||||||
|
STEALTH_INIT_SCRIPT,
|
||||||
|
persistent_context_launch_parts,
|
||||||
|
stealth_enabled,
|
||||||
|
)
|
||||||
|
|
||||||
|
if TYPE_CHECKING:
|
||||||
|
from playwright.async_api import BrowserContext
|
||||||
|
|
||||||
|
|
||||||
|
async def launch_persistent_browser(
|
||||||
|
playwright,
|
||||||
|
*,
|
||||||
|
profile_dir: str,
|
||||||
|
channel: str = "chrome",
|
||||||
|
executable_path: str | None = None,
|
||||||
|
headless: bool | None = None,
|
||||||
|
extra_args: list[str] | None = None,
|
||||||
|
record_video_dir: str | None = None,
|
||||||
|
) -> "BrowserContext":
|
||||||
|
"""用 stealth 参数启动 persistent context + 注入 STEALTH_INIT_SCRIPT。
|
||||||
|
|
||||||
|
headless=None 时读 OPENCLAW_BROWSER_HEADLESS。
|
||||||
|
record_video_dir 非空则开录屏。
|
||||||
|
"""
|
||||||
|
if headless is None:
|
||||||
|
v = (os.environ.get("OPENCLAW_BROWSER_HEADLESS") or "0").strip().lower()
|
||||||
|
headless = v in ("1", "true", "yes", "on")
|
||||||
|
|
||||||
|
chrome = executable_path or find_chrome_executable()
|
||||||
|
if not chrome:
|
||||||
|
raise RuntimeError(
|
||||||
|
"ERROR:MISSING_BROWSER 未检测到 Chrome 或 Edge 浏览器,请安装后重试。"
|
||||||
|
)
|
||||||
|
|
||||||
|
args, ignore = persistent_context_launch_parts(extra_args=extra_args)
|
||||||
|
launch_kwargs: dict = dict(
|
||||||
|
user_data_dir=profile_dir,
|
||||||
|
headless=headless,
|
||||||
|
executable_path=chrome,
|
||||||
|
locale="zh-CN",
|
||||||
|
no_viewport=True,
|
||||||
|
args=args,
|
||||||
|
)
|
||||||
|
if ignore is not None:
|
||||||
|
launch_kwargs["ignore_default_args"] = ignore
|
||||||
|
if record_video_dir:
|
||||||
|
launch_kwargs["record_video_dir"] = record_video_dir
|
||||||
|
|
||||||
|
context = await playwright.chromium.launch_persistent_context(**launch_kwargs)
|
||||||
|
if stealth_enabled():
|
||||||
|
await context.add_init_script(STEALTH_INIT_SCRIPT)
|
||||||
|
return context
|
||||||
20
scripts/jiangchang_skill_core/rpa/errors.py
Normal file
20
scripts/jiangchang_skill_core/rpa/errors.py
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
# vendored from jiangchang-platform-kit; do not edit here, edit the kit.
|
||||||
|
"""RPA 场景统一错误码(stdout / 异常消息前缀)。"""
|
||||||
|
|
||||||
|
ERR_REQUIRE_LOGIN = "ERROR:REQUIRE_LOGIN"
|
||||||
|
ERR_LOGIN_TIMEOUT = "ERROR:LOGIN_TIMEOUT"
|
||||||
|
ERR_CAPTCHA_NEED_HUMAN = "ERROR:CAPTCHA_NEED_HUMAN"
|
||||||
|
ERR_RATE_LIMITED = "ERROR:RATE_LIMITED"
|
||||||
|
ERR_MISSING_BROWSER = "ERROR:MISSING_BROWSER"
|
||||||
|
ERR_DEVICE_NOT_READY = "ERROR:DEVICE_NOT_READY"
|
||||||
|
ERR_WINDOW_NOT_FOUND = "ERROR:WINDOW_NOT_FOUND"
|
||||||
|
|
||||||
|
__all__ = [
|
||||||
|
"ERR_REQUIRE_LOGIN",
|
||||||
|
"ERR_LOGIN_TIMEOUT",
|
||||||
|
"ERR_CAPTCHA_NEED_HUMAN",
|
||||||
|
"ERR_RATE_LIMITED",
|
||||||
|
"ERR_MISSING_BROWSER",
|
||||||
|
"ERR_DEVICE_NOT_READY",
|
||||||
|
"ERR_WINDOW_NOT_FOUND",
|
||||||
|
]
|
||||||
97
scripts/jiangchang_skill_core/rpa/human_login.py
Normal file
97
scripts/jiangchang_skill_core/rpa/human_login.py
Normal file
@@ -0,0 +1,97 @@
|
|||||||
|
# vendored from jiangchang-platform-kit; do not edit here, edit the kit.
|
||||||
|
"""验证码/登录 HITL 等待(站点无关通用部分)。"""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import asyncio
|
||||||
|
|
||||||
|
DEFAULT_CAPTCHA_MARKERS = (
|
||||||
|
"punish",
|
||||||
|
"baxia",
|
||||||
|
"nc_login",
|
||||||
|
"tmd",
|
||||||
|
"x5sec",
|
||||||
|
"安全验证",
|
||||||
|
"请按住滑块",
|
||||||
|
"拖动到最右边",
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
async def wait_for_captcha_pass(
|
||||||
|
page,
|
||||||
|
captcha_markers: tuple[str, ...] = DEFAULT_CAPTCHA_MARKERS,
|
||||||
|
timeout_sec: int = 120,
|
||||||
|
) -> bool:
|
||||||
|
"""
|
||||||
|
检测到验证码/拦截页时,暂停等待用户手工完成验证。
|
||||||
|
每 3 秒轮询一次页面状态,直到页面不再是拦截状态或超时。
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
True — 验证码已通过,可以继续
|
||||||
|
False — 超时,用户未在规定时间内完成验证
|
||||||
|
"""
|
||||||
|
print(
|
||||||
|
f"[验证码] 检测到安全拦截,请在浏览器中手工完成验证。"
|
||||||
|
f"等待最多 {timeout_sec} 秒..."
|
||||||
|
)
|
||||||
|
|
||||||
|
elapsed = 0
|
||||||
|
while elapsed < timeout_sec:
|
||||||
|
await asyncio.sleep(3)
|
||||||
|
elapsed += 3
|
||||||
|
try:
|
||||||
|
url = page.url or ""
|
||||||
|
text = ""
|
||||||
|
try:
|
||||||
|
text = await page.locator("body").inner_text(timeout=2_000)
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
haystack = (url + " " + text[:1000]).lower()
|
||||||
|
still_blocked = any(m.lower() in haystack for m in captcha_markers)
|
||||||
|
if not still_blocked:
|
||||||
|
print(f"[验证码] 已通过!({elapsed}s 后检测到正常页面)")
|
||||||
|
return True
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
|
||||||
|
print(f"[验证码] 超时 {timeout_sec}s,用户未完成验证")
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
async def wait_for_login(
|
||||||
|
page,
|
||||||
|
success_selectors: list[str],
|
||||||
|
timeout_sec: int = 180,
|
||||||
|
) -> bool:
|
||||||
|
"""
|
||||||
|
轮询等待登录成功标志出现(站点专属选择器由调用方传入)。
|
||||||
|
|
||||||
|
Args:
|
||||||
|
page: Playwright Page 对象。
|
||||||
|
success_selectors: 登录成功后应出现的 CSS 选择器列表。
|
||||||
|
timeout_sec: 最长等待秒数。
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
True — 检测到登录成功标志
|
||||||
|
False — 超时
|
||||||
|
"""
|
||||||
|
print(
|
||||||
|
f"[登录] 请在浏览器中完成登录。等待最多 {timeout_sec} 秒,"
|
||||||
|
"期间程序不会操作页面..."
|
||||||
|
)
|
||||||
|
|
||||||
|
elapsed = 0
|
||||||
|
while elapsed < timeout_sec:
|
||||||
|
await asyncio.sleep(2)
|
||||||
|
elapsed += 2
|
||||||
|
for selector in success_selectors:
|
||||||
|
try:
|
||||||
|
el = await page.query_selector(selector)
|
||||||
|
if el is not None:
|
||||||
|
print("[登录] 检测到登录成功!")
|
||||||
|
return True
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
|
||||||
|
print(f"[登录] 超时 {timeout_sec}s,未完成登录")
|
||||||
|
return False
|
||||||
67
scripts/jiangchang_skill_core/rpa/stealth.py
Normal file
67
scripts/jiangchang_skill_core/rpa/stealth.py
Normal file
@@ -0,0 +1,67 @@
|
|||||||
|
# vendored from jiangchang-platform-kit; do not edit here, edit the kit.
|
||||||
|
"""淡化 Playwright 自动化指纹,降低 baxia/x5sec 风控命中率。
|
||||||
|
|
||||||
|
关闭:环境变量 OPENCLAW_PLAYWRIGHT_STEALTH=0(或 false/off/no)。
|
||||||
|
"""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import os
|
||||||
|
|
||||||
|
STEALTH_INIT_SCRIPT = """
|
||||||
|
(() => {
|
||||||
|
try { Object.defineProperty(navigator, 'webdriver', { get: () => undefined }); } catch (e) {}
|
||||||
|
try {
|
||||||
|
window.chrome = window.chrome || {};
|
||||||
|
window.chrome.runtime = window.chrome.runtime || {};
|
||||||
|
} catch (e) {}
|
||||||
|
try {
|
||||||
|
const orig = navigator.permissions && navigator.permissions.query;
|
||||||
|
if (orig) {
|
||||||
|
navigator.permissions.query = (p) =>
|
||||||
|
p && p.name === 'notifications'
|
||||||
|
? Promise.resolve({ state: Notification.permission })
|
||||||
|
: orig(p);
|
||||||
|
}
|
||||||
|
} catch (e) {}
|
||||||
|
try {
|
||||||
|
Object.defineProperty(navigator, 'plugins', { get: () => [1, 2, 3, 4, 5] });
|
||||||
|
} catch (e) {}
|
||||||
|
try {
|
||||||
|
Object.defineProperty(navigator, 'languages', { get: () => ['zh-CN', 'zh'] });
|
||||||
|
} catch (e) {}
|
||||||
|
try {
|
||||||
|
Object.defineProperty(navigator, 'hardwareConcurrency', { get: () => 8 });
|
||||||
|
} catch (e) {}
|
||||||
|
try {
|
||||||
|
Object.defineProperty(navigator, 'deviceMemory', { get: () => 8 });
|
||||||
|
} catch (e) {}
|
||||||
|
try {
|
||||||
|
const getParameter = WebGLRenderingContext.prototype.getParameter;
|
||||||
|
WebGLRenderingContext.prototype.getParameter = function (p) {
|
||||||
|
if (p === 37445) return 'Intel Inc.';
|
||||||
|
if (p === 37446) return 'Intel Iris OpenGL Engine';
|
||||||
|
return getParameter.call(this, p);
|
||||||
|
};
|
||||||
|
} catch (e) {}
|
||||||
|
})();
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
def stealth_enabled() -> bool:
|
||||||
|
v = (os.environ.get("OPENCLAW_PLAYWRIGHT_STEALTH") or "1").strip().lower()
|
||||||
|
return v not in ("0", "false", "no", "off")
|
||||||
|
|
||||||
|
|
||||||
|
def persistent_context_launch_parts(
|
||||||
|
*, extra_args: list[str] | None = None
|
||||||
|
) -> tuple[list[str], list[str] | None]:
|
||||||
|
"""返回 (args, ignore_default_args)。ignore_default_args 为 None 时不要传给 launch。"""
|
||||||
|
args = ["--start-maximized"]
|
||||||
|
if extra_args:
|
||||||
|
args = args + list(extra_args)
|
||||||
|
if not stealth_enabled():
|
||||||
|
return args, None
|
||||||
|
if "--disable-blink-features=AutomationControlled" not in args:
|
||||||
|
args.append("--disable-blink-features=AutomationControlled")
|
||||||
|
return args, ["--enable-automation"]
|
||||||
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"
|
||||||
|
)
|
||||||
97
scripts/service/example_adapter/sim_rpa.py
Normal file
97
scripts/service/example_adapter/sim_rpa.py
Normal file
@@ -0,0 +1,97 @@
|
|||||||
|
"""仿真 RPA 档位:演示共享库 browser / anti_detect 用法(需浏览器,默认开发档)。"""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import asyncio
|
||||||
|
import os
|
||||||
|
|
||||||
|
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 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 = "sim-demo"
|
||||||
|
data_dir = get_skill_data_dir()
|
||||||
|
profile_dir = os.path.join(data_dir, "browser-profile")
|
||||||
|
os.makedirs(profile_dir, exist_ok=True)
|
||||||
|
|
||||||
|
record_video = config.get_bool("OPENCLAW_RECORD_VIDEO")
|
||||||
|
video_dir = os.path.join(data_dir, "rpa-artifacts", batch_id, "video") if record_video else None
|
||||||
|
if video_dir:
|
||||||
|
os.makedirs(video_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 = {}
|
||||||
|
try:
|
||||||
|
async with async_playwright() as pw:
|
||||||
|
context = await launch_persistent_browser(
|
||||||
|
pw,
|
||||||
|
profile_dir=profile_dir,
|
||||||
|
headless=config.get_bool("OPENCLAW_BROWSER_HEADLESS"),
|
||||||
|
record_video_dir=video_dir,
|
||||||
|
)
|
||||||
|
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 输入框则尝试输入
|
||||||
|
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"
|
||||||
|
await anti_detect.human_type(page, search, demo_text)
|
||||||
|
await anti_detect.human_delay_short()
|
||||||
|
|
||||||
|
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,
|
||||||
|
)
|
||||||
|
|
||||||
|
await context.close()
|
||||||
|
except Exception as exc:
|
||||||
|
return ExampleResult(
|
||||||
|
ok=False,
|
||||||
|
serial_no=None,
|
||||||
|
error_msg=str(exc),
|
||||||
|
artifacts=artifact_paths,
|
||||||
|
)
|
||||||
|
|
||||||
|
return ExampleResult(
|
||||||
|
ok=True,
|
||||||
|
serial_no=f"SIM-{len(items)}",
|
||||||
|
error_msg=None,
|
||||||
|
artifacts={"mode": "sim_rpa", "url": url, **artifact_paths},
|
||||||
|
)
|
||||||
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()))
|
||||||
Reference in New Issue
Block a user