hyper-agent-browser 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +196 -0
- package/package.json +63 -0
- package/src/browser/context.ts +66 -0
- package/src/browser/manager.ts +414 -0
- package/src/browser/sync-chrome-data.ts +53 -0
- package/src/cli.ts +628 -0
- package/src/cli.ts.backup +529 -0
- package/src/commands/actions.ts +232 -0
- package/src/commands/advanced.ts +252 -0
- package/src/commands/config.ts +44 -0
- package/src/commands/getters.ts +110 -0
- package/src/commands/info.ts +195 -0
- package/src/commands/navigation.ts +50 -0
- package/src/commands/session.ts +83 -0
- package/src/daemon/browser-pool.ts +65 -0
- package/src/daemon/client.ts +128 -0
- package/src/daemon/main.ts +200 -0
- package/src/daemon/server.ts +562 -0
- package/src/session/manager.ts +110 -0
- package/src/session/store.ts +172 -0
- package/src/snapshot/accessibility.ts +182 -0
- package/src/snapshot/dom-extractor.ts +220 -0
- package/src/snapshot/formatter.ts +115 -0
- package/src/snapshot/reference-store.ts +97 -0
- package/src/utils/config.ts +183 -0
- package/src/utils/errors.ts +121 -0
- package/src/utils/logger.ts +12 -0
- package/src/utils/selector.ts +23 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Hubert
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
# hyper-agent-browser (hab)
|
|
2
|
+
|
|
3
|
+
**纯浏览器自动化 CLI,专为 AI Agent 设计**
|
|
4
|
+
|
|
5
|
+
[](https://www.typescriptlang.org/)
|
|
6
|
+
[](https://bun.sh)
|
|
7
|
+
[](./LICENSE)
|
|
8
|
+
|
|
9
|
+
## ✨ 特性
|
|
10
|
+
|
|
11
|
+
- 🎯 **@eN 元素引用** - 无需手写选择器,自动生成 `@e1`, `@e2` 等引用
|
|
12
|
+
- 🔐 **Session 持久化** - 保持登录状态,支持多账号隔离
|
|
13
|
+
- 🎭 **反检测** - 基于 Patchright,绕过自动化检测
|
|
14
|
+
- ⚡ **快速启动** - Bun 运行时,冷启动 ~25ms
|
|
15
|
+
- 🤖 **AI Agent 友好** - 设计用于 Claude Code 等 AI Agent 调用
|
|
16
|
+
|
|
17
|
+
## 🚀 快速开始
|
|
18
|
+
|
|
19
|
+
### 安装
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
# 克隆仓库
|
|
23
|
+
git clone https://github.com/yourusername/hyper-agent-browser.git
|
|
24
|
+
cd hyper-agent-browser
|
|
25
|
+
|
|
26
|
+
# 安装依赖
|
|
27
|
+
bun install
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
### 基础使用
|
|
31
|
+
|
|
32
|
+
```bash
|
|
33
|
+
# 1. 打开网页
|
|
34
|
+
bun dev -- --headed open https://google.com
|
|
35
|
+
|
|
36
|
+
# 2. 获取可交互元素快照
|
|
37
|
+
bun dev -- snapshot -i
|
|
38
|
+
|
|
39
|
+
# 输出:
|
|
40
|
+
# @e1 [textbox] "Search"
|
|
41
|
+
# @e2 [button] "Google Search"
|
|
42
|
+
# @e3 [link] "Gmail"
|
|
43
|
+
|
|
44
|
+
# 3. 使用 @eN 引用操作元素
|
|
45
|
+
bun dev -- fill @e1 "Bun JavaScript runtime"
|
|
46
|
+
bun dev -- press Enter
|
|
47
|
+
|
|
48
|
+
# 4. 截图
|
|
49
|
+
bun dev -- screenshot -o result.png
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
### 使用已登录的 Google Profile
|
|
53
|
+
|
|
54
|
+
```bash
|
|
55
|
+
# 导入 Chrome Profile(保留登录状态)
|
|
56
|
+
./scripts/import-chrome-profile.sh -s gmail
|
|
57
|
+
|
|
58
|
+
# 使用已登录状态访问 Gmail
|
|
59
|
+
bun dev -- -s gmail --headed open https://mail.google.com
|
|
60
|
+
bun dev -- -s gmail snapshot -i
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
## 📖 文档
|
|
64
|
+
|
|
65
|
+
- [GETTING_STARTED.md](./GETTING_STARTED.md) - 快速入门指南
|
|
66
|
+
- [ELEMENT_REFERENCE_GUIDE.md](./ELEMENT_REFERENCE_GUIDE.md) - @eN 引用完整文档
|
|
67
|
+
- [GOOGLE_PROFILE_GUIDE.md](./GOOGLE_PROFILE_GUIDE.md) - Google Profile 集成
|
|
68
|
+
- [CLAUDE.md](./CLAUDE.md) - 开发者文档
|
|
69
|
+
- [hyper-agent-browser-spec.md](./hyper-agent-browser-spec.md) - 技术规格
|
|
70
|
+
|
|
71
|
+
## 🎯 核心功能
|
|
72
|
+
|
|
73
|
+
### 元素引用系统
|
|
74
|
+
|
|
75
|
+
不需要手写复杂的选择器:
|
|
76
|
+
|
|
77
|
+
```bash
|
|
78
|
+
# 传统方式(繁琐)
|
|
79
|
+
hab click 'css=button.MuiButton-root.MuiButton-contained'
|
|
80
|
+
|
|
81
|
+
# hyper-agent-browser 方式(简单)
|
|
82
|
+
hab snapshot -i # 生成引用
|
|
83
|
+
hab click @e5 # 使用引用
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
### Session 管理
|
|
87
|
+
|
|
88
|
+
每个 session 独立的浏览器环境:
|
|
89
|
+
|
|
90
|
+
```bash
|
|
91
|
+
# 个人账号
|
|
92
|
+
bun dev -- -s personal open https://mail.google.com
|
|
93
|
+
|
|
94
|
+
# 工作账号
|
|
95
|
+
bun dev -- -s work open https://mail.google.com
|
|
96
|
+
|
|
97
|
+
# 列出所有 session
|
|
98
|
+
bun dev -- sessions
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
### 支持的命令
|
|
102
|
+
|
|
103
|
+
**导航**: `open`, `reload`, `back`, `forward`
|
|
104
|
+
**操作**: `click`, `fill`, `type`, `press`, `scroll`, `hover`, `select`, `wait`
|
|
105
|
+
**信息**: `snapshot`, `screenshot`, `evaluate`, `url`, `title`, `content`
|
|
106
|
+
**会话**: `sessions`, `close`
|
|
107
|
+
|
|
108
|
+
## 🛠️ 开发
|
|
109
|
+
|
|
110
|
+
```bash
|
|
111
|
+
# 运行测试
|
|
112
|
+
bun test
|
|
113
|
+
|
|
114
|
+
# 类型检查
|
|
115
|
+
bun run typecheck
|
|
116
|
+
|
|
117
|
+
# 代码规范检查
|
|
118
|
+
bun run lint
|
|
119
|
+
|
|
120
|
+
# 构建
|
|
121
|
+
bun run build # 当前平台
|
|
122
|
+
bun run build:all # 所有平台
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
## 🤖 AI Agent 集成
|
|
126
|
+
|
|
127
|
+
hyper-agent-browser 专为 AI Agent 设计。安装 Skill 文件:
|
|
128
|
+
|
|
129
|
+
```bash
|
|
130
|
+
mkdir -p ~/.claude/skills
|
|
131
|
+
cp skills/hyper-browser.md ~/.claude/skills/
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
**使用流程**:
|
|
135
|
+
1. Agent 打开网页:`hab open <url>`
|
|
136
|
+
2. Agent 获取快照:`hab snapshot -i`
|
|
137
|
+
3. Agent 分析快照,找到目标元素 `@eN`
|
|
138
|
+
4. Agent 执行操作:`hab click @eN`
|
|
139
|
+
5. 重复直到任务完成
|
|
140
|
+
|
|
141
|
+
## 📋 选择器格式
|
|
142
|
+
|
|
143
|
+
| 格式 | 示例 | 说明 |
|
|
144
|
+
|------|------|------|
|
|
145
|
+
| `@eN` | `@e1`, `@e5` | 元素引用(推荐) |
|
|
146
|
+
| `css=` | `css=#login` | CSS 选择器 |
|
|
147
|
+
| `text=` | `text=Sign in` | 文本匹配 |
|
|
148
|
+
| `xpath=` | `xpath=//button` | XPath |
|
|
149
|
+
|
|
150
|
+
## 🏗️ 架构
|
|
151
|
+
|
|
152
|
+
```
|
|
153
|
+
src/
|
|
154
|
+
├── cli.ts # CLI 入口
|
|
155
|
+
├── browser/ # 浏览器管理
|
|
156
|
+
├── session/ # Session 持久化
|
|
157
|
+
├── commands/ # 命令实现
|
|
158
|
+
├── snapshot/ # 元素引用系统
|
|
159
|
+
│ ├── accessibility.ts # Accessibility API
|
|
160
|
+
│ ├── dom-extractor.ts # DOM 提取器(fallback)
|
|
161
|
+
│ └── reference-store.ts # @eN 映射存储
|
|
162
|
+
└── utils/ # 工具函数
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
## 🔒 安全
|
|
166
|
+
|
|
167
|
+
- Session 目录权限 700(仅用户可访问)
|
|
168
|
+
- `evaluate` 命令禁止危险操作(require, process, fs 等)
|
|
169
|
+
- UserData 独立隔离,不影响系统 Chrome
|
|
170
|
+
|
|
171
|
+
## 📊 技术栈
|
|
172
|
+
|
|
173
|
+
- **Bun** 1.2.21 - JavaScript 运行时
|
|
174
|
+
- **Patchright** 1.57.0 - 反检测 Playwright fork
|
|
175
|
+
- **Commander.js** 12.1.0 - CLI 框架
|
|
176
|
+
- **Zod** 3.25.76 - 数据验证
|
|
177
|
+
- **Biome** 1.9.4 - 代码规范
|
|
178
|
+
|
|
179
|
+
## 🤝 贡献
|
|
180
|
+
|
|
181
|
+
欢迎 Pull Requests!请确保:
|
|
182
|
+
|
|
183
|
+
- TypeScript 类型检查通过:`bun run typecheck`
|
|
184
|
+
- 测试通过:`bun test`
|
|
185
|
+
- 代码规范检查通过:`bun run lint`
|
|
186
|
+
|
|
187
|
+
## 📄 许可证
|
|
188
|
+
|
|
189
|
+
MIT
|
|
190
|
+
|
|
191
|
+
## 🙏 致谢
|
|
192
|
+
|
|
193
|
+
- [Patchright](https://github.com/Patchright/patchright) - 反检测 Playwright fork
|
|
194
|
+
- [agent-browser](https://github.com/anthropics/agent-browser) - CLI 设计灵感
|
|
195
|
+
- [Bun](https://bun.sh) - 快速的 JavaScript 运行时
|
|
196
|
+
|
package/package.json
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "hyper-agent-browser",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Pure browser automation CLI for AI Agents - 纯浏览器自动化 CLI,专为 AI Agent 设计",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "src/cli.ts",
|
|
7
|
+
"bin": {
|
|
8
|
+
"hab": "src/cli.ts"
|
|
9
|
+
},
|
|
10
|
+
"files": ["src/**/*", "README.md", "LICENSE"],
|
|
11
|
+
"scripts": {
|
|
12
|
+
"dev": "bun run src/cli.ts",
|
|
13
|
+
"build": "bun build --compile --minify src/cli.ts --outfile dist/hab",
|
|
14
|
+
"build:all": "bun run scripts/build-all.ts",
|
|
15
|
+
"test": "bun test",
|
|
16
|
+
"test:unit": "bun test tests/unit",
|
|
17
|
+
"test:integration": "bun test tests/integration",
|
|
18
|
+
"test:watch": "bun test --watch",
|
|
19
|
+
"test:coverage": "bun test --coverage",
|
|
20
|
+
"lint": "bunx @biomejs/biome check .",
|
|
21
|
+
"lint:fix": "bunx @biomejs/biome check --write .",
|
|
22
|
+
"typecheck": "bunx tsc --noEmit",
|
|
23
|
+
"prepublishOnly": "bun run typecheck && bun run lint"
|
|
24
|
+
},
|
|
25
|
+
"keywords": [
|
|
26
|
+
"browser-automation",
|
|
27
|
+
"cli",
|
|
28
|
+
"ai-agent",
|
|
29
|
+
"playwright",
|
|
30
|
+
"patchright",
|
|
31
|
+
"bun",
|
|
32
|
+
"typescript",
|
|
33
|
+
"automation",
|
|
34
|
+
"testing",
|
|
35
|
+
"web-scraping"
|
|
36
|
+
],
|
|
37
|
+
"author": {
|
|
38
|
+
"name": "Hubert",
|
|
39
|
+
"url": "https://github.com/hubo1989"
|
|
40
|
+
},
|
|
41
|
+
"license": "MIT",
|
|
42
|
+
"repository": {
|
|
43
|
+
"type": "git",
|
|
44
|
+
"url": "https://github.com/hubo1989/hyper-agent-browser.git"
|
|
45
|
+
},
|
|
46
|
+
"bugs": {
|
|
47
|
+
"url": "https://github.com/hubo1989/hyper-agent-browser/issues"
|
|
48
|
+
},
|
|
49
|
+
"homepage": "https://github.com/hubo1989/hyper-agent-browser#readme",
|
|
50
|
+
"dependencies": {
|
|
51
|
+
"patchright": "^1.55.1",
|
|
52
|
+
"commander": "^12.0.0",
|
|
53
|
+
"zod": "^3.24.0"
|
|
54
|
+
},
|
|
55
|
+
"devDependencies": {
|
|
56
|
+
"@types/bun": "latest",
|
|
57
|
+
"@biomejs/biome": "^1.9.0",
|
|
58
|
+
"typescript": "^5.0.0"
|
|
59
|
+
},
|
|
60
|
+
"engines": {
|
|
61
|
+
"bun": ">=1.1.0"
|
|
62
|
+
}
|
|
63
|
+
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import type { BrowserContext, Page } from "patchright";
|
|
2
|
+
import type { Session } from "../session/store";
|
|
3
|
+
import { BrowserManager } from "./manager";
|
|
4
|
+
|
|
5
|
+
export interface BrowserContextOptions {
|
|
6
|
+
headed?: boolean;
|
|
7
|
+
timeout?: number;
|
|
8
|
+
channel?: "chrome" | "msedge" | "chromium";
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Manages browser context lifecycle for a session
|
|
13
|
+
*/
|
|
14
|
+
export class BrowserContextManager {
|
|
15
|
+
private browserManager: BrowserManager | null = null;
|
|
16
|
+
private session: Session;
|
|
17
|
+
|
|
18
|
+
constructor(session: Session) {
|
|
19
|
+
this.session = session;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
async getBrowserManager(options: BrowserContextOptions = {}): Promise<BrowserManager> {
|
|
23
|
+
if (!this.browserManager) {
|
|
24
|
+
this.browserManager = new BrowserManager(this.session, options);
|
|
25
|
+
await this.browserManager.connect();
|
|
26
|
+
}
|
|
27
|
+
return this.browserManager;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
async getPage(options: BrowserContextOptions = {}): Promise<Page> {
|
|
31
|
+
const manager = await this.getBrowserManager(options);
|
|
32
|
+
return manager.getPage();
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
async getContext(options: BrowserContextOptions = {}): Promise<BrowserContext | null> {
|
|
36
|
+
const manager = await this.getBrowserManager(options);
|
|
37
|
+
return manager.getContext();
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
getWsEndpoint(): string | undefined {
|
|
41
|
+
return this.browserManager?.getWsEndpoint();
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
getPid(): number | undefined {
|
|
45
|
+
return this.browserManager?.getPid();
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
async showOperationIndicator(operation: string): Promise<void> {
|
|
49
|
+
await this.browserManager?.showOperationIndicator(operation);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
async hideOperationIndicator(): Promise<void> {
|
|
53
|
+
await this.browserManager?.hideOperationIndicator();
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
async close(): Promise<void> {
|
|
57
|
+
if (this.browserManager) {
|
|
58
|
+
await this.browserManager.close();
|
|
59
|
+
this.browserManager = null;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
isConnected(): boolean {
|
|
64
|
+
return this.browserManager?.isConnected() ?? false;
|
|
65
|
+
}
|
|
66
|
+
}
|