deepspider 0.2.6 → 0.2.9

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.
@@ -0,0 +1,73 @@
1
+ # CI/CD Guidelines
2
+
3
+ > GitHub Actions 自动发布规范
4
+
5
+ ---
6
+
7
+ ## npm 自动发布
8
+
9
+ ### 触发条件
10
+
11
+ 推送 `v*` 标签时自动触发发布流程。
12
+
13
+ ```bash
14
+ npm version patch && git push && git push --tags
15
+ ```
16
+
17
+ ### Workflow 配置要点
18
+
19
+ #### 1. 原生模块处理
20
+
21
+ 项目包含 `isolated-vm` 等原生模块,在 CI 环境编译可能失败。
22
+
23
+ **解决方案**:使用 `--ignore-scripts` 跳过编译
24
+
25
+ ```yaml
26
+ - run: pnpm install --no-frozen-lockfile --ignore-scripts
27
+ ```
28
+
29
+ #### 2. NPM_TOKEN 认证
30
+
31
+ **正确配置**:
32
+
33
+ 1. 在 npmjs.com 生成 **Automation** 类型的 token
34
+ 2. 添加为 GitHub **Repository secret**(不是 Environment secret)
35
+ 3. 使用 `NODE_AUTH_TOKEN` 环境变量
36
+
37
+ ```yaml
38
+ - uses: actions/setup-node@v4
39
+ with:
40
+ node-version: '20'
41
+ registry-url: 'https://registry.npmjs.org'
42
+
43
+ - run: npm publish
44
+ env:
45
+ NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
46
+ ```
47
+
48
+ ---
49
+
50
+ ## 常见问题
51
+
52
+ ### pnpm lockfile 不匹配
53
+
54
+ **错误**:`ERR_PNPM_LOCKFILE_CONFIG_MISMATCH`
55
+
56
+ **解决**:添加 `--no-frozen-lockfile`
57
+
58
+ ### isolated-vm 编译失败
59
+
60
+ **错误**:`v8::SourceLocation does not name a type`
61
+
62
+ **原因**:isolated-vm 与新版 Node.js V8 API 不兼容
63
+
64
+ **解决**:添加 `--ignore-scripts` 跳过原生模块编译
65
+
66
+ ### NPM_TOKEN 认证失败
67
+
68
+ **错误**:`ENEEDAUTH` 或 `Access token expired`
69
+
70
+ **检查**:
71
+ 1. Token 类型是否为 Automation
72
+ 2. Secret 是否为 Repository secret
73
+ 3. 使用 `NODE_AUTH_TOKEN` 而非直接写入 `.npmrc`
@@ -242,6 +242,49 @@ tools: [...analyzerTools, ...deobfuscatorTools, ...traceTools]
242
242
  tools: [...analyzerTools, ...browserTools, ...sandboxTools]
243
243
  ```
244
244
 
245
+ ### systemPrompt 按任务类型动态组合
246
+
247
+ 当不同任务类型需要不同的约束时,应拆分提示词并动态组合:
248
+
249
+ ```javascript
250
+ // src/agent/prompts/system.js
251
+
252
+ // 基础提示 - 适用于所有对话
253
+ export const systemPrompt = `你是 DeepSpider,智能爬虫 Agent。
254
+
255
+ ## 浏览器面板
256
+ 当消息以"[浏览器已就绪]"开头时,浏览器已打开,不要再调用 launch_browser。
257
+
258
+ ## 委托子代理
259
+ 简单任务自己做,复杂任务委托子代理。`;
260
+
261
+ // 完整分析专用 - 仅在特定任务时添加
262
+ export const fullAnalysisPrompt = `
263
+ ## 完整分析任务要求
264
+ 必须完成端到端验证,验证成功后才能保存报告...`;
265
+ ```
266
+
267
+ 在消息处理时动态组合:
268
+
269
+ ```javascript
270
+ // src/agent/run.js
271
+ import { fullAnalysisPrompt } from './prompts/system.js';
272
+
273
+ if (data.type === 'analysis') {
274
+ // 完整分析:添加强制验证要求
275
+ userPrompt = `${browserReadyPrefix}用户选中数据要求分析...
276
+ ${fullAnalysisPrompt}`;
277
+ } else if (data.type === 'chat') {
278
+ // 普通聊天:不添加额外约束
279
+ userPrompt = `${browserReadyPrefix}${data.text}`;
280
+ }
281
+ ```
282
+
283
+ **好处**:
284
+ - 普通聊天不受端到端验证等强制要求约束
285
+ - 减少不必要的 token 消耗
286
+ - 任务类型明确,Agent 行为可预测
287
+
245
288
  ### Skills 只写经验
246
289
 
247
290
  ```markdown
@@ -104,6 +104,46 @@ for (const trap in handler) {
104
104
  }
105
105
  ```
106
106
 
107
+ ### 4. 内部操作触发 Hook
108
+
109
+ **问题**: 系统内部的消息发送、状态存储等操作也会触发 Hook,产生噪音日志。
110
+
111
+ ```javascript
112
+ // ❌ 错误:内部操作被记录
113
+ sessionStorage.setItem('deepspider_messages', JSON.stringify(messages));
114
+ // 触发 Storage Hook 和 JSON Hook,污染日志
115
+ ```
116
+
117
+ **解决方案**: 使用统一标记过滤内部数据。
118
+
119
+ 1. **Storage Hook**: 使用 `deepspider_` 前缀过滤 key
120
+ ```javascript
121
+ const INTERNAL_PREFIX = 'deepspider_';
122
+ storage.setItem = function(key, value) {
123
+ if (!key.startsWith(INTERNAL_PREFIX)) {
124
+ deepspider.log('storage', { ... });
125
+ }
126
+ return origSet(key, value);
127
+ };
128
+ ```
129
+
130
+ 2. **JSON Hook**: 使用 `__ds__` 标记过滤内部数据
131
+ ```javascript
132
+ // 内部消息添加标记
133
+ const msg = { __ds__: true, type: 'chat', text: '...' };
134
+
135
+ // Hook 中检查标记
136
+ const INTERNAL_MARKER = '"__ds__":true';
137
+ if (!result.includes(INTERNAL_MARKER)) {
138
+ deepspider.log('json', { ... });
139
+ }
140
+ ```
141
+
142
+ **规范**:
143
+ - sessionStorage/localStorage key 必须以 `deepspider_` 开头
144
+ - 发送到后端的 JSON 消息必须包含 `__ds__: true`
145
+ - 面板消息对象必须包含 `__ds__: true`
146
+
107
147
  ---
108
148
 
109
149
  ## Anti-Detection Patterns
@@ -22,6 +22,7 @@ DeepSpider 是基于 DeepAgents + Patchright 的智能爬虫 Agent。
22
22
  | [State Management](./state-management.md) | Agent 状态与数据存储 | Done |
23
23
  | [Quality Guidelines](./quality-guidelines.md) | 代码质量规范 | Done |
24
24
  | [Type Safety](./type-safety.md) | Zod 类型验证规范 | Done |
25
+ | [CI/CD Guidelines](./ci-cd-guidelines.md) | GitHub Actions 自动发布规范 | Done |
25
26
 
26
27
  ---
27
28
 
@@ -113,6 +113,42 @@ proc.on('close', () => {
113
113
 
114
114
  **原因**: `spawn` 的 options 不包含 `timeout`,这是 `execSync` 的选项。使用 spawn 时必须手动实现超时逻辑。
115
115
 
116
+ ### 6. 用正则替换 HTML 字符串
117
+
118
+ ```javascript
119
+ // ❌ 禁止:正则替换 HTML 字符串会破坏结构
120
+ function linkifyPaths(html) {
121
+ return html.replace(/(\/[\w.\-\/]+)/g, '<a href="$1">$1</a>');
122
+ }
123
+ // 会把 </strong> 中的 /strong 也匹配成路径!
124
+
125
+ // ✅ 使用 DOM TreeWalker 遍历文本节点
126
+ function linkifyPaths(container) {
127
+ const walker = document.createTreeWalker(container, NodeFilter.SHOW_TEXT);
128
+ const textNodes = [];
129
+ while (walker.nextNode()) textNodes.push(walker.currentNode);
130
+
131
+ textNodes.forEach(node => {
132
+ // 只处理纯文本,不会影响 HTML 标签
133
+ });
134
+ }
135
+ ```
136
+
137
+ **原因**: 正则无法区分 HTML 标签和文本内容,容易误匹配导致结构破坏。
138
+
139
+ ### 7. LLM 工具参数传递大段代码
140
+
141
+ ```javascript
142
+ // ❌ 禁止:直接传递大段代码内容,可能被 LLM 截断
143
+ await saveReport({ pythonCode: longCodeString });
144
+
145
+ // ✅ 先保存到文件,再传递文件路径
146
+ await artifactSave({ path: 'domain/decrypt.py', content: code });
147
+ await saveReport({ pythonCodeFile: 'domain/decrypt.py' });
148
+ ```
149
+
150
+ **原因**: LLM 输出有长度限制,大段代码作为参数传递时可能被截断。分步保存确保代码完整性。
151
+
116
152
  ---
117
153
 
118
154
  ## Required Patterns
@@ -135,6 +171,47 @@ traverse.default(ast, {
135
171
  const cdp = await browser.getCDPSession();
136
172
  ```
137
173
 
174
+ ### 3. Hook 日志记录调用位置
175
+
176
+ ```javascript
177
+ // ✅ 在日志中包含解析后的调用位置
178
+ const entry = {
179
+ ...data,
180
+ timestamp: Date.now(),
181
+ stack: stack,
182
+ caller: caller, // { func, file, line, col }
183
+ };
184
+
185
+ // 控制台输出显示文件名和行号
186
+ const loc = caller ? ' @ ' + caller.file.split('/').pop() + ':' + caller.line : '';
187
+ console.log('[DeepSpider:' + type + ']' + loc, data);
188
+ ```
189
+
190
+ **原因**: Hook 日志需要记录 JS 文件调用位置,便于快速定位加密代码来源。
191
+
192
+ ---
193
+
194
+ ## Release Process
195
+
196
+ ### 版本发布流程
197
+
198
+ 升级版本时必须同步创建 git tag:
199
+
200
+ ```bash
201
+ # 1. 升级 package.json 版本
202
+ npm version patch --no-git-tag-version
203
+
204
+ # 2. 提交版本变更
205
+ git add package.json
206
+ git commit -m "chore: bump version to x.x.x"
207
+
208
+ # 3. 创建并推送 git tag
209
+ git tag vx.x.x
210
+ git push && git push origin vx.x.x
211
+ ```
212
+
213
+ **原因**: npm 版本和 git tag 需要保持同步,便于版本追踪和回溯。
214
+
138
215
  ---
139
216
 
140
217
  ## Testing Requirements
@@ -8,7 +8,7 @@
8
8
 
9
9
  <!-- @@@auto:current-status -->
10
10
  - **Active File**: `journal-1.md`
11
- - **Total Sessions**: 1
11
+ - **Total Sessions**: 2
12
12
  - **Last Active**: 2026-02-03
13
13
  <!-- @@@/auto:current-status -->
14
14
 
@@ -19,7 +19,7 @@
19
19
  <!-- @@@auto:active-documents -->
20
20
  | File | Lines | Status |
21
21
  |------|-------|--------|
22
- | `journal-1.md` | ~61 | Active |
22
+ | `journal-1.md` | ~125 | Active |
23
23
  <!-- @@@/auto:active-documents -->
24
24
 
25
25
  ---
@@ -29,6 +29,7 @@
29
29
  <!-- @@@auto:session-history -->
30
30
  | # | Date | Title | Commits |
31
31
  |---|------|-------|---------|
32
+ | 2 | 2026-02-03 | GitHub Actions 自动发布 npm | `4ff9a25`, `debdc4e`, `ab56fe2`, `67f9c55`, `b13b03d`, `63a6304`, `327ca39`, `78de837`, `46ce73e` |
32
33
  | 1 | 2026-02-03 | 环境变量重命名与配置检测 | `4aa6cad` |
33
34
  <!-- @@@/auto:session-history -->
34
35
 
@@ -59,3 +59,67 @@
59
59
  ### Next Steps
60
60
 
61
61
  - None - task complete
62
+
63
+ ## Session 2: GitHub Actions 自动发布 npm
64
+
65
+ **Date**: 2026-02-03
66
+ **Task**: GitHub Actions 自动发布 npm
67
+
68
+ ### Summary
69
+
70
+ (Add summary)
71
+
72
+ ### Main Changes
73
+
74
+ ## 完成内容
75
+
76
+ 实现 GitHub Actions 自动发布到 npm。
77
+
78
+ | 变更 | 说明 |
79
+ |------|------|
80
+ | GitHub Actions | 添加 .github/workflows/publish.yml |
81
+ | 触发条件 | 推送 v* 标签时自动发布 |
82
+ | CI 流程 | lint → publish |
83
+ | Node.js | 使用 v20 + --ignore-scripts 跳过原生模块编译 |
84
+
85
+ ## 遇到的问题与解决
86
+
87
+ 1. **pnpm lockfile 不匹配** → 添加 --no-frozen-lockfile
88
+ 2. **isolated-vm 编译失败** → 添加 --ignore-scripts
89
+ 3. **NPM_TOKEN 认证失败** → 使用 NODE_AUTH_TOKEN 环境变量
90
+
91
+ ## 发布流程
92
+
93
+ ```bash
94
+ npm version patch && git push && git push --tags
95
+ ```
96
+
97
+ ## 变更文件
98
+
99
+ - `.github/workflows/publish.yml` - GitHub Actions 配置
100
+
101
+ ### Git Commits
102
+
103
+ | Hash | Message |
104
+ |------|---------|
105
+ | `4ff9a25` | (see git log) |
106
+ | `debdc4e` | (see git log) |
107
+ | `ab56fe2` | (see git log) |
108
+ | `67f9c55` | (see git log) |
109
+ | `b13b03d` | (see git log) |
110
+ | `63a6304` | (see git log) |
111
+ | `327ca39` | (see git log) |
112
+ | `78de837` | (see git log) |
113
+ | `46ce73e` | (see git log) |
114
+
115
+ ### Testing
116
+
117
+ - [OK] (Add test results)
118
+
119
+ ### Status
120
+
121
+ [OK] **Completed**
122
+
123
+ ### Next Steps
124
+
125
+ - None - task complete
package/CLAUDE.md CHANGED
@@ -2,6 +2,11 @@
2
2
 
3
3
  > 基于 DeepAgents + Patchright 的智能爬虫 Agent,覆盖爬虫全生命周期
4
4
 
5
+ ## 分析方法论
6
+
7
+ ** 每次都分别从资深爬虫工程师和资深技术架构师的两个角度进行理性的辩论性的分析。**
8
+ 从最佳实践出发,结合当前项目的实际架构。
9
+
5
10
  ## 功能
6
11
 
7
12
  ### 逆向分析
@@ -156,6 +161,24 @@ pnpm run agent https://example.com
156
161
 
157
162
  ## 代码规范
158
163
 
164
+ ### Hook 内部数据过滤
165
+
166
+ 系统内部操作(消息存储、前后端通信)不应触发 Hook 记录。使用统一标记过滤:
167
+
168
+ ```javascript
169
+ // Storage: 使用 deepspider_ 前缀
170
+ sessionStorage.setItem('deepspider_messages', data); // 不触发 Hook
171
+
172
+ // JSON: 使用 __ds__ 标记
173
+ const msg = { __ds__: true, type: 'chat', text: '...' }; // 不触发 Hook
174
+ ```
175
+
176
+ | 场景 | 过滤方式 | 示例 |
177
+ |------|----------|------|
178
+ | sessionStorage | `deepspider_` 前缀 | `deepspider_chat_messages` |
179
+ | 发送到后端的消息 | `__ds__: true` | `{ __ds__: true, type: 'chat' }` |
180
+ | 面板消息对象 | `__ds__: true` | `{ __ds__: true, role, content }` |
181
+
159
182
  ### 浏览器交互
160
183
 
161
184
  与浏览器的交互优先使用 CDP(Chrome DevTools Protocol)方式,而非 `page.evaluate()`。
package/README.md CHANGED
@@ -15,6 +15,7 @@
15
15
  - **验证码处理**: 滑块、点选、图片验证码
16
16
  - **反检测**: 指纹伪装、代理轮换、风控规避
17
17
  - **爬虫编排**: 智能调度,输出可运行的 Python 爬虫
18
+ - **交互面板**: 浏览器内置分析面板,支持元素选择、对话交互
18
19
 
19
20
  ## 快速开始
20
21
 
@@ -37,6 +38,11 @@ cp .env.example .env
37
38
  # 编辑 .env 填入配置(见下方环境变量说明)
38
39
  ```
39
40
 
41
+ > **注意**: 项目依赖 `isolated-vm` 原生模块,需要 C++ 编译环境:
42
+ > - macOS: `xcode-select --install`
43
+ > - Ubuntu: `sudo apt install build-essential`
44
+ > - Windows: 安装 [Visual Studio Build Tools](https://visualstudio.microsoft.com/visual-cpp-build-tools/)
45
+
40
46
  ### 环境变量配置
41
47
 
42
48
  DeepSpider 需要配置 LLM API 才能运行。支持任何兼容 OpenAI 格式的供应商。
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "deepspider",
3
- "version": "0.2.6",
3
+ "version": "0.2.9",
4
4
  "description": "智能爬虫工程平台 - 基于 DeepAgents + Patchright 的 AI 爬虫 Agent",
5
5
  "type": "module",
6
6
  "main": "src/index.js",
@@ -17,6 +17,7 @@
17
17
  "lint": "eslint src/",
18
18
  "lint:fix": "eslint src/ --fix",
19
19
  "setup:crypto": "uv venv .venv --python 3.11 2>/dev/null || true && uv pip install -r requirements-crypto.txt",
20
+ "postinstall": "patchright install chromium && npm rebuild isolated-vm 2>/dev/null || true",
20
21
  "prepare": "husky"
21
22
  },
22
23
  "keywords": [
@@ -51,7 +52,7 @@
51
52
  "@langchain/core": "^1.1.17",
52
53
  "@langchain/langgraph": "^1.1.2",
53
54
  "@langchain/openai": "^1.2.3",
54
- "@modelcontextprotocol/sdk": "^1.25.3",
55
+ "@modelcontextprotocol/sdk": "^1.26.0",
55
56
  "crypto-js": "^4.2.0",
56
57
  "deepagents": "^1.6.0",
57
58
  "dotenv": "^17.2.3",