deepspider 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/.claude/agents/check.md +122 -0
- package/.claude/agents/debug.md +106 -0
- package/.claude/agents/dispatch.md +214 -0
- package/.claude/agents/implement.md +96 -0
- package/.claude/agents/plan.md +396 -0
- package/.claude/agents/research.md +120 -0
- package/.claude/commands/evolve/merge.md +80 -0
- package/.claude/commands/trellis/before-backend-dev.md +13 -0
- package/.claude/commands/trellis/before-frontend-dev.md +13 -0
- package/.claude/commands/trellis/break-loop.md +107 -0
- package/.claude/commands/trellis/check-backend.md +13 -0
- package/.claude/commands/trellis/check-cross-layer.md +153 -0
- package/.claude/commands/trellis/check-frontend.md +13 -0
- package/.claude/commands/trellis/create-command.md +154 -0
- package/.claude/commands/trellis/finish-work.md +129 -0
- package/.claude/commands/trellis/integrate-skill.md +219 -0
- package/.claude/commands/trellis/onboard.md +358 -0
- package/.claude/commands/trellis/parallel.md +193 -0
- package/.claude/commands/trellis/record-session.md +62 -0
- package/.claude/commands/trellis/start.md +280 -0
- package/.claude/commands/trellis/update-spec.md +213 -0
- package/.claude/hooks/inject-subagent-context.py +758 -0
- package/.claude/hooks/ralph-loop.py +374 -0
- package/.claude/hooks/session-start.py +126 -0
- package/.claude/settings.json +41 -0
- package/.claude/skills/deepagents-guide/SKILL.md +428 -0
- package/.cursor/commands/trellis-before-backend-dev.md +13 -0
- package/.cursor/commands/trellis-before-frontend-dev.md +13 -0
- package/.cursor/commands/trellis-break-loop.md +107 -0
- package/.cursor/commands/trellis-check-backend.md +13 -0
- package/.cursor/commands/trellis-check-cross-layer.md +153 -0
- package/.cursor/commands/trellis-check-frontend.md +13 -0
- package/.cursor/commands/trellis-create-command.md +154 -0
- package/.cursor/commands/trellis-finish-work.md +129 -0
- package/.cursor/commands/trellis-integrate-skill.md +219 -0
- package/.cursor/commands/trellis-onboard.md +358 -0
- package/.cursor/commands/trellis-record-session.md +62 -0
- package/.cursor/commands/trellis-start.md +156 -0
- package/.cursor/commands/trellis-update-spec.md +213 -0
- package/.env.example +11 -0
- package/.husky/pre-commit +1 -0
- package/.mcp.json +8 -0
- package/.trellis/.template-hashes.json +65 -0
- package/.trellis/.version +1 -0
- package/.trellis/scripts/add-session.sh +384 -0
- package/.trellis/scripts/common/developer.sh +129 -0
- package/.trellis/scripts/common/git-context.sh +263 -0
- package/.trellis/scripts/common/paths.sh +208 -0
- package/.trellis/scripts/common/phase.sh +150 -0
- package/.trellis/scripts/common/registry.sh +247 -0
- package/.trellis/scripts/common/task-queue.sh +142 -0
- package/.trellis/scripts/common/task-utils.sh +151 -0
- package/.trellis/scripts/common/worktree.sh +128 -0
- package/.trellis/scripts/create-bootstrap.sh +299 -0
- package/.trellis/scripts/get-context.sh +7 -0
- package/.trellis/scripts/get-developer.sh +15 -0
- package/.trellis/scripts/init-developer.sh +34 -0
- package/.trellis/scripts/multi-agent/cleanup.sh +396 -0
- package/.trellis/scripts/multi-agent/create-pr.sh +241 -0
- package/.trellis/scripts/multi-agent/plan.sh +207 -0
- package/.trellis/scripts/multi-agent/start.sh +310 -0
- package/.trellis/scripts/multi-agent/status.sh +828 -0
- package/.trellis/scripts/task.sh +1118 -0
- package/.trellis/spec/backend/deepagents-guide.md +337 -0
- package/.trellis/spec/backend/directory-structure.md +126 -0
- package/.trellis/spec/backend/examples/skills/deepagents-guide/README.md +11 -0
- package/.trellis/spec/backend/examples/skills/deepagents-guide/agent.js.template +20 -0
- package/.trellis/spec/backend/examples/skills/deepagents-guide/skills-config.js.template +13 -0
- package/.trellis/spec/backend/examples/skills/deepagents-guide/subagent.js.template +19 -0
- package/.trellis/spec/backend/hook-guidelines.md +178 -0
- package/.trellis/spec/backend/index.md +36 -0
- package/.trellis/spec/backend/quality-guidelines.md +201 -0
- package/.trellis/spec/backend/state-management.md +76 -0
- package/.trellis/spec/backend/tool-guidelines.md +144 -0
- package/.trellis/spec/backend/type-safety.md +71 -0
- package/.trellis/spec/guides/code-reuse-thinking-guide.md +92 -0
- package/.trellis/spec/guides/cross-layer-thinking-guide.md +94 -0
- package/.trellis/spec/guides/index.md +79 -0
- package/.trellis/tasks/archive/02-02-evolving-skills/prd.md +61 -0
- package/.trellis/tasks/archive/02-02-evolving-skills/task.json +29 -0
- package/.trellis/tasks/archive/2026-02/00-bootstrap-guidelines/prd.md +86 -0
- package/.trellis/tasks/archive/2026-02/00-bootstrap-guidelines/task.json +27 -0
- package/.trellis/tasks/archive/2026-02/02-02-skills-system/check.jsonl +3 -0
- package/.trellis/tasks/archive/2026-02/02-02-skills-system/debug.jsonl +2 -0
- package/.trellis/tasks/archive/2026-02/02-02-skills-system/implement.jsonl +5 -0
- package/.trellis/tasks/archive/2026-02/02-02-skills-system/prd.md +33 -0
- package/.trellis/tasks/archive/2026-02/02-02-skills-system/task.json +41 -0
- package/.trellis/workflow.md +407 -0
- package/.trellis/workspace/index.md +123 -0
- package/.trellis/workspace/pony/index.md +40 -0
- package/.trellis/workspace/pony/journal-1.md +7 -0
- package/.trellis/worktree.yaml +47 -0
- package/AGENTS.md +18 -0
- package/CLAUDE.md +292 -0
- package/README.md +134 -0
- package/agents/deepspider.md +142 -0
- package/docs/DEBUG.md +42 -0
- package/docs/GUIDE.md +334 -0
- package/docs/PROMPT.md +60 -0
- package/docs/USAGE.md +226 -0
- package/eslint.config.js +51 -0
- package/package.json +78 -0
- package/requirements-crypto.txt +14 -0
- package/src/agent/index.js +97 -0
- package/src/agent/logger.js +164 -0
- package/src/agent/middleware/filterTools.js +64 -0
- package/src/agent/middleware/report.js +79 -0
- package/src/agent/prompts/system.js +315 -0
- package/src/agent/run.js +575 -0
- package/src/agent/skills/anti-detect/SKILL.md +28 -0
- package/src/agent/skills/anti-detect/evolved.md +12 -0
- package/src/agent/skills/captcha/SKILL.md +37 -0
- package/src/agent/skills/captcha/evolved.md +12 -0
- package/src/agent/skills/config.js +30 -0
- package/src/agent/skills/crawler/SKILL.md +9 -0
- package/src/agent/skills/crawler/evolved.md +16 -0
- package/src/agent/skills/dynamic-analysis/SKILL.md +91 -0
- package/src/agent/skills/dynamic-analysis/evolved.md +12 -0
- package/src/agent/skills/env/SKILL.md +72 -0
- package/src/agent/skills/env/evolved.md +12 -0
- package/src/agent/skills/evolve.js +79 -0
- package/src/agent/skills/general/SKILL.md +12 -0
- package/src/agent/skills/general/evolved.md +12 -0
- package/src/agent/skills/js2python/SKILL.md +30 -0
- package/src/agent/skills/js2python/evolved.md +13 -0
- package/src/agent/skills/report/SKILL.md +21 -0
- package/src/agent/skills/report/evolved.md +12 -0
- package/src/agent/skills/sandbox/SKILL.md +22 -0
- package/src/agent/skills/sandbox/evolved.md +16 -0
- package/src/agent/skills/static-analysis/SKILL.md +93 -0
- package/src/agent/skills/static-analysis/evolved.md +12 -0
- package/src/agent/skills/xpath/SKILL.md +119 -0
- package/src/agent/subagents/anti-detect.js +45 -0
- package/src/agent/subagents/captcha.js +51 -0
- package/src/agent/subagents/crawler.js +138 -0
- package/src/agent/subagents/dynamic.js +64 -0
- package/src/agent/subagents/env-agent.js +82 -0
- package/src/agent/subagents/index.js +37 -0
- package/src/agent/subagents/js2python.js +72 -0
- package/src/agent/subagents/sandbox.js +55 -0
- package/src/agent/subagents/static.js +66 -0
- package/src/agent/tools/analysis.js +135 -0
- package/src/agent/tools/analyzer.js +85 -0
- package/src/agent/tools/anti-detect.js +89 -0
- package/src/agent/tools/antidebug.js +64 -0
- package/src/agent/tools/async.js +43 -0
- package/src/agent/tools/browser.js +324 -0
- package/src/agent/tools/captcha.js +223 -0
- package/src/agent/tools/capture.js +179 -0
- package/src/agent/tools/correlate.js +303 -0
- package/src/agent/tools/crawler.js +116 -0
- package/src/agent/tools/cryptohook.js +80 -0
- package/src/agent/tools/debug.js +246 -0
- package/src/agent/tools/deobfuscator.js +90 -0
- package/src/agent/tools/env.js +83 -0
- package/src/agent/tools/envdump.js +92 -0
- package/src/agent/tools/evolve.js +164 -0
- package/src/agent/tools/extract.js +114 -0
- package/src/agent/tools/extractor.js +54 -0
- package/src/agent/tools/file.js +224 -0
- package/src/agent/tools/hook.js +84 -0
- package/src/agent/tools/hookManager.js +178 -0
- package/src/agent/tools/index.js +137 -0
- package/src/agent/tools/nodejs.js +101 -0
- package/src/agent/tools/patch.js +46 -0
- package/src/agent/tools/preprocess.js +71 -0
- package/src/agent/tools/profile.js +122 -0
- package/src/agent/tools/python.js +627 -0
- package/src/agent/tools/report.js +124 -0
- package/src/agent/tools/runtime.js +132 -0
- package/src/agent/tools/sandbox.js +79 -0
- package/src/agent/tools/store.js +73 -0
- package/src/agent/tools/trace.js +74 -0
- package/src/agent/tools/tracing.js +201 -0
- package/src/agent/tools/utils.js +51 -0
- package/src/agent/tools/verify.js +184 -0
- package/src/agent/tools/webcrack.js +109 -0
- package/src/analyzer/ASTAnalyzer.js +387 -0
- package/src/analyzer/CallStackAnalyzer.js +379 -0
- package/src/analyzer/Deobfuscator.js +289 -0
- package/src/analyzer/EncryptionAnalyzer.js +99 -0
- package/src/analyzer/index.js +22 -0
- package/src/browser/EnvBridge.js +186 -0
- package/src/browser/cdp.js +168 -0
- package/src/browser/client.js +197 -0
- package/src/browser/collector.js +444 -0
- package/src/browser/collectors/RequestCryptoLinker.js +109 -0
- package/src/browser/collectors/ResponseSearcher.js +107 -0
- package/src/browser/collectors/ScriptCollector.js +158 -0
- package/src/browser/collectors/index.js +26 -0
- package/src/browser/defaultHooks.js +932 -0
- package/src/browser/hooks/crypto.js +55 -0
- package/src/browser/hooks/index.js +64 -0
- package/src/browser/hooks/native.js +9 -0
- package/src/browser/hooks/network.js +33 -0
- package/src/browser/index.js +42 -0
- package/src/browser/interceptors/NetworkInterceptor.js +116 -0
- package/src/browser/interceptors/ScriptInterceptor.js +76 -0
- package/src/browser/interceptors/index.js +6 -0
- package/src/browser/ui/analysisPanel.js +1782 -0
- package/src/browser/ui/confirmDialog.js +158 -0
- package/src/browser/ui/panel.html +152 -0
- package/src/browser/ui/selector.js +170 -0
- package/src/config/index.js +5 -0
- package/src/config/paths.js +71 -0
- package/src/config/patterns/crypto.js +36 -0
- package/src/config/profiles/chrome.json +71 -0
- package/src/config/profiles/firefox.json +44 -0
- package/src/config/profiles/safari.json +38 -0
- package/src/core/EnvMonitor.js +200 -0
- package/src/core/PatchGenerator.js +278 -0
- package/src/core/Sandbox.js +181 -0
- package/src/env/AntiAntiDebug.js +111 -0
- package/src/env/AsyncHook.js +68 -0
- package/src/env/BrowserAPIList.js +265 -0
- package/src/env/CookieHook.js +48 -0
- package/src/env/CryptoHook.js +205 -0
- package/src/env/EnvCodeGenerator.js +157 -0
- package/src/env/EnvDumper.js +356 -0
- package/src/env/EnvExtractor.js +220 -0
- package/src/env/HookBase.js +618 -0
- package/src/env/NetworkHook.js +159 -0
- package/src/env/modules/bom/history.js +29 -0
- package/src/env/modules/bom/location.js +26 -0
- package/src/env/modules/bom/navigator.js +70 -0
- package/src/env/modules/bom/screen.js +26 -0
- package/src/env/modules/bom/storage.js +23 -0
- package/src/env/modules/dom/document.js +110 -0
- package/src/env/modules/dom/event.js +51 -0
- package/src/env/modules/index.js +34 -0
- package/src/env/modules/webapi/fetch.js +46 -0
- package/src/env/modules/webapi/url.js +47 -0
- package/src/env/modules/webapi/xhr.js +48 -0
- package/src/index.js +27 -0
- package/src/mcp/server.js +89 -0
- package/src/store/DataStore.js +708 -0
- package/src/store/Store.js +158 -0
- package/src/store/Validator.js +24 -0
- package/test/analyze.test.js +90 -0
- package/test/envdump.test.js +74 -0
- package/test/flow.test.js +90 -0
- package/test/hooks.test.js +138 -0
- package/test/plugin.test.js +35 -0
- package/test/refactor-full.test.js +30 -0
- package/test/refactor.test.js +21 -0
- package/test/samples/obfuscated.js +61 -0
- package/test/samples/original.js +66 -0
- package/test/samples/v10_eval_chain.js +52 -0
- package/test/samples/v11_bytecode_vm.js +81 -0
- package/test/samples/v12_polymorphic.js +69 -0
- package/test/samples/v1_ob_basic.js +98 -0
- package/test/samples/v2_ob_advanced.js +99 -0
- package/test/samples/v3_jjencode.js +77 -0
- package/test/samples/v4_aaencode.js +73 -0
- package/test/samples/v5_control_flow.js +86 -0
- package/test/samples/v6_string_encryption.js +71 -0
- package/test/samples/v7_jsvmp.js +83 -0
- package/test/samples/v8_anti_debug.js +79 -0
- package/test/samples/v9_proxy_trap.js +49 -0
- package/test/samples.test.js +96 -0
- package/test/webcrack.test.js +55 -0
package/package.json
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "deepspider",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "智能爬虫工程平台 - 基于 DeepAgents + Patchright 的 AI 爬虫 Agent",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "src/index.js",
|
|
7
|
+
"bin": {
|
|
8
|
+
"deepspider": "./src/agent/run.js"
|
|
9
|
+
},
|
|
10
|
+
"scripts": {
|
|
11
|
+
"start": "node src/index.js",
|
|
12
|
+
"dev": "node --watch src/index.js",
|
|
13
|
+
"cli": "node bin/cli.js",
|
|
14
|
+
"mcp": "node src/mcp/server.js",
|
|
15
|
+
"agent": "node src/agent/run.js",
|
|
16
|
+
"test": "node --test test/",
|
|
17
|
+
"lint": "eslint src/",
|
|
18
|
+
"lint:fix": "eslint src/ --fix",
|
|
19
|
+
"setup:crypto": "uv venv .venv --python 3.11 2>/dev/null || true && uv pip install -r requirements-crypto.txt",
|
|
20
|
+
"prepare": "husky"
|
|
21
|
+
},
|
|
22
|
+
"keywords": [
|
|
23
|
+
"crawler",
|
|
24
|
+
"web-scraping",
|
|
25
|
+
"javascript",
|
|
26
|
+
"reverse-engineering",
|
|
27
|
+
"deobfuscation",
|
|
28
|
+
"captcha",
|
|
29
|
+
"anti-detect",
|
|
30
|
+
"automation"
|
|
31
|
+
],
|
|
32
|
+
"author": "pony-ma",
|
|
33
|
+
"license": "MIT",
|
|
34
|
+
"repository": {
|
|
35
|
+
"type": "git",
|
|
36
|
+
"url": "git+https://github.com/ma-pony/deepspider.git"
|
|
37
|
+
},
|
|
38
|
+
"homepage": "https://github.com/ma-pony/deepspider#readme",
|
|
39
|
+
"bugs": {
|
|
40
|
+
"url": "https://github.com/ma-pony/deepspider/issues"
|
|
41
|
+
},
|
|
42
|
+
"engines": {
|
|
43
|
+
"node": ">=18.0.0"
|
|
44
|
+
},
|
|
45
|
+
"dependencies": {
|
|
46
|
+
"@babel/generator": "^7.28.6",
|
|
47
|
+
"@babel/parser": "^7.28.6",
|
|
48
|
+
"@babel/traverse": "^7.28.6",
|
|
49
|
+
"@babel/types": "^7.28.6",
|
|
50
|
+
"@langchain/anthropic": "^1.3.12",
|
|
51
|
+
"@langchain/core": "^1.1.17",
|
|
52
|
+
"@langchain/langgraph": "^1.1.2",
|
|
53
|
+
"@langchain/openai": "^1.2.3",
|
|
54
|
+
"@modelcontextprotocol/sdk": "^1.25.3",
|
|
55
|
+
"crypto-js": "^4.2.0",
|
|
56
|
+
"deepagents": "^1.6.0",
|
|
57
|
+
"dotenv": "^17.2.3",
|
|
58
|
+
"hono": "4.11.7",
|
|
59
|
+
"isolated-vm": "^6.0.2",
|
|
60
|
+
"js-md5": "^0.8.3",
|
|
61
|
+
"js-sha256": "^0.11.1",
|
|
62
|
+
"jsencrypt": "^3.5.4",
|
|
63
|
+
"langchain": "^1.2.15",
|
|
64
|
+
"marked": "^17.0.1",
|
|
65
|
+
"patchright": "^1.57.0",
|
|
66
|
+
"sm-crypto": "^0.4.0",
|
|
67
|
+
"webcrack": "^2.15.1",
|
|
68
|
+
"zod": "^4.3.6"
|
|
69
|
+
},
|
|
70
|
+
"devDependencies": {
|
|
71
|
+
"eslint": "^9.39.2",
|
|
72
|
+
"husky": "^9.1.7",
|
|
73
|
+
"lint-staged": "^16.2.7"
|
|
74
|
+
},
|
|
75
|
+
"lint-staged": {
|
|
76
|
+
"*.js": "eslint --fix"
|
|
77
|
+
}
|
|
78
|
+
}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* DeepSpider - DeepAgent 主入口
|
|
3
|
+
* 基于 DeepAgents 最佳实践重构
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import 'dotenv/config';
|
|
7
|
+
import { createDeepAgent, StateBackend, FilesystemBackend } from 'deepagents';
|
|
8
|
+
import { ChatOpenAI } from '@langchain/openai';
|
|
9
|
+
import { MemorySaver } from '@langchain/langgraph';
|
|
10
|
+
|
|
11
|
+
import { coreTools } from './tools/index.js';
|
|
12
|
+
import { allSubagents } from './subagents/index.js';
|
|
13
|
+
import { systemPrompt } from './prompts/system.js';
|
|
14
|
+
import { createReportMiddleware } from './middleware/report.js';
|
|
15
|
+
import { createFilterToolsMiddleware } from './middleware/filterTools.js';
|
|
16
|
+
|
|
17
|
+
// 从环境变量读取配置
|
|
18
|
+
const config = {
|
|
19
|
+
apiKey: process.env.LLM_API_KEY,
|
|
20
|
+
baseUrl: process.env.LLM_BASE_URL,
|
|
21
|
+
model: process.env.LLM_MODEL || 'gpt-4o',
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* 创建 LLM 模型实例
|
|
26
|
+
* 使用 ChatOpenAI 兼容 OpenAI 格式的任意供应商
|
|
27
|
+
*/
|
|
28
|
+
function createModel(options = {}) {
|
|
29
|
+
const {
|
|
30
|
+
model = config.model,
|
|
31
|
+
apiKey = config.apiKey,
|
|
32
|
+
baseUrl = config.baseUrl,
|
|
33
|
+
} = options;
|
|
34
|
+
|
|
35
|
+
return new ChatOpenAI({
|
|
36
|
+
model,
|
|
37
|
+
apiKey,
|
|
38
|
+
configuration: baseUrl ? { baseURL: baseUrl } : undefined,
|
|
39
|
+
temperature: 0,
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* 创建 DeepSpider Agent
|
|
45
|
+
*/
|
|
46
|
+
export function createDeepSpiderAgent(options = {}) {
|
|
47
|
+
const {
|
|
48
|
+
model = config.model,
|
|
49
|
+
apiKey = config.apiKey,
|
|
50
|
+
baseUrl = config.baseUrl,
|
|
51
|
+
enableMemory = true,
|
|
52
|
+
enableInterrupt = false,
|
|
53
|
+
onReportReady = null, // 报告就绪回调
|
|
54
|
+
} = options;
|
|
55
|
+
|
|
56
|
+
// 创建 LLM 模型实例
|
|
57
|
+
const llm = createModel({ model, apiKey, baseUrl });
|
|
58
|
+
|
|
59
|
+
// 后端配置:使用文件系统持久化
|
|
60
|
+
const backend = enableMemory
|
|
61
|
+
? new FilesystemBackend({ rootDir: './.deepspider-agent' })
|
|
62
|
+
: new StateBackend();
|
|
63
|
+
|
|
64
|
+
// Checkpointer:保存对话状态,支持断点恢复
|
|
65
|
+
const checkpointer = new MemorySaver();
|
|
66
|
+
|
|
67
|
+
// 人机交互配置
|
|
68
|
+
const interruptOn = enableInterrupt
|
|
69
|
+
? {
|
|
70
|
+
sandbox_execute: { allowedDecisions: ['approve', 'reject', 'edit'] },
|
|
71
|
+
sandbox_inject: { allowedDecisions: ['approve', 'reject'] },
|
|
72
|
+
}
|
|
73
|
+
: undefined;
|
|
74
|
+
|
|
75
|
+
// 中间件配置
|
|
76
|
+
const middleware = [
|
|
77
|
+
createFilterToolsMiddleware(), // 过滤内置的 write_file/read_file
|
|
78
|
+
createReportMiddleware({ onReportReady }),
|
|
79
|
+
];
|
|
80
|
+
|
|
81
|
+
return createDeepAgent({
|
|
82
|
+
name: 'deepspider',
|
|
83
|
+
model: llm,
|
|
84
|
+
tools: coreTools,
|
|
85
|
+
subagents: allSubagents,
|
|
86
|
+
systemPrompt,
|
|
87
|
+
backend,
|
|
88
|
+
checkpointer,
|
|
89
|
+
interruptOn,
|
|
90
|
+
middleware,
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// 默认导出
|
|
95
|
+
export const agent = createDeepSpiderAgent();
|
|
96
|
+
|
|
97
|
+
export default agent;
|
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* DeepSpider - 日志回调处理器
|
|
3
|
+
* 记录 AI 交互、工具调用等详细日志
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { BaseCallbackHandler } from '@langchain/core/callbacks/base';
|
|
7
|
+
import { appendFileSync, mkdirSync, existsSync } from 'fs';
|
|
8
|
+
import { join, dirname } from 'path';
|
|
9
|
+
import { DEEPSPIDER_HOME } from '../config/paths.js';
|
|
10
|
+
|
|
11
|
+
const LOG_DIR = join(DEEPSPIDER_HOME, 'logs');
|
|
12
|
+
const LOG_FILE = join(LOG_DIR, 'agent.log');
|
|
13
|
+
|
|
14
|
+
function ensureLogDir() {
|
|
15
|
+
if (!existsSync(LOG_DIR)) {
|
|
16
|
+
mkdirSync(LOG_DIR, { recursive: true });
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function formatTime() {
|
|
21
|
+
return new Date().toISOString();
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function truncate(str, maxLen = 500) {
|
|
25
|
+
if (!str) return '';
|
|
26
|
+
const s = typeof str === 'string' ? str : JSON.stringify(str);
|
|
27
|
+
return s.length > maxLen ? s.slice(0, maxLen) + '...' : s;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* 文件日志回调处理器
|
|
32
|
+
*/
|
|
33
|
+
export class FileLoggerCallback extends BaseCallbackHandler {
|
|
34
|
+
name = 'FileLoggerCallback';
|
|
35
|
+
|
|
36
|
+
constructor(options = {}) {
|
|
37
|
+
super();
|
|
38
|
+
this.logFile = options.logFile || LOG_FILE;
|
|
39
|
+
this.verbose = options.verbose || false;
|
|
40
|
+
ensureLogDir();
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
log(level, category, message, data = null) {
|
|
44
|
+
const line = JSON.stringify({
|
|
45
|
+
time: formatTime(),
|
|
46
|
+
level,
|
|
47
|
+
category,
|
|
48
|
+
message,
|
|
49
|
+
data,
|
|
50
|
+
}) + '\n';
|
|
51
|
+
|
|
52
|
+
appendFileSync(this.logFile, line);
|
|
53
|
+
|
|
54
|
+
if (this.verbose) {
|
|
55
|
+
console.log(`[${level}] [${category}] ${message}`);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// ========== LLM 事件 ==========
|
|
60
|
+
handleLLMStart(llm, prompts, runId) {
|
|
61
|
+
this.log('INFO', 'LLM', 'LLM 调用开始', {
|
|
62
|
+
runId,
|
|
63
|
+
model: llm?.id?.[2] || llm?.name,
|
|
64
|
+
promptCount: prompts?.length,
|
|
65
|
+
promptPreview: truncate(prompts?.[0], 200),
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
handleLLMEnd(output, runId) {
|
|
70
|
+
const content = output?.generations?.[0]?.[0]?.text
|
|
71
|
+
|| output?.generations?.[0]?.[0]?.message?.content;
|
|
72
|
+
this.log('INFO', 'LLM', 'LLM 调用结束', {
|
|
73
|
+
runId,
|
|
74
|
+
outputPreview: truncate(content, 300),
|
|
75
|
+
tokenUsage: output?.llmOutput?.tokenUsage,
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
handleLLMError(error, runId) {
|
|
80
|
+
this.log('ERROR', 'LLM', 'LLM 调用错误', {
|
|
81
|
+
runId,
|
|
82
|
+
error: error?.message || String(error),
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
// ========== 工具事件 ==========
|
|
87
|
+
handleToolStart(tool, input, runId) {
|
|
88
|
+
this.log('INFO', 'TOOL', `工具调用: ${tool?.name || 'unknown'}`, {
|
|
89
|
+
runId,
|
|
90
|
+
toolName: tool?.name,
|
|
91
|
+
input: truncate(input, 500),
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
handleToolEnd(output, runId) {
|
|
96
|
+
this.log('INFO', 'TOOL', '工具返回', {
|
|
97
|
+
runId,
|
|
98
|
+
output: truncate(output, 500),
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
handleToolError(error, runId) {
|
|
103
|
+
this.log('ERROR', 'TOOL', '工具错误', {
|
|
104
|
+
runId,
|
|
105
|
+
error: error?.message || String(error),
|
|
106
|
+
stack: error?.stack?.split('\n').slice(0, 5),
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
// ========== Chain 事件 ==========
|
|
111
|
+
handleChainStart(chain, inputs, runId) {
|
|
112
|
+
this.log('DEBUG', 'CHAIN', `Chain 开始: ${chain?.name || 'unknown'}`, {
|
|
113
|
+
runId,
|
|
114
|
+
chainName: chain?.name,
|
|
115
|
+
inputKeys: Object.keys(inputs || {}),
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
handleChainEnd(outputs, runId) {
|
|
120
|
+
this.log('DEBUG', 'CHAIN', 'Chain 结束', {
|
|
121
|
+
runId,
|
|
122
|
+
outputKeys: Object.keys(outputs || {}),
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
handleChainError(error, runId) {
|
|
127
|
+
this.log('ERROR', 'CHAIN', 'Chain 错误', {
|
|
128
|
+
runId,
|
|
129
|
+
error: error?.message || String(error),
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
// ========== Agent 事件 ==========
|
|
134
|
+
handleAgentAction(action, runId) {
|
|
135
|
+
this.log('INFO', 'AGENT', `Agent 动作: ${action?.tool}`, {
|
|
136
|
+
runId,
|
|
137
|
+
tool: action?.tool,
|
|
138
|
+
toolInput: truncate(action?.toolInput, 300),
|
|
139
|
+
log: truncate(action?.log, 200),
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
handleAgentEnd(action, runId) {
|
|
144
|
+
this.log('INFO', 'AGENT', 'Agent 结束', {
|
|
145
|
+
runId,
|
|
146
|
+
returnValues: truncate(action?.returnValues, 300),
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* 创建日志回调实例
|
|
153
|
+
*/
|
|
154
|
+
export function createLogger(options = {}) {
|
|
155
|
+
const enabled = process.env.DEBUG === 'true' || options.enabled;
|
|
156
|
+
if (!enabled) return null;
|
|
157
|
+
|
|
158
|
+
return new FileLoggerCallback({
|
|
159
|
+
verbose: options.verbose || false,
|
|
160
|
+
logFile: options.logFile || LOG_FILE,
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
export default FileLoggerCallback;
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* DeepSpider - 工具过滤中间件
|
|
3
|
+
* 过滤掉 DeepAgents 内置的文件工具,避免与自定义工具冲突
|
|
4
|
+
* 同时替换框架提示词中的工具名称
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { createMiddleware } from 'langchain';
|
|
8
|
+
|
|
9
|
+
// 需要过滤的内置工具名称
|
|
10
|
+
const FILTERED_TOOLS = ['write_file', 'read_file', 'edit_file', 'glob', 'grep'];
|
|
11
|
+
|
|
12
|
+
// 提示词替换规则
|
|
13
|
+
const PROMPT_REPLACEMENTS = [
|
|
14
|
+
// 工具名称替换
|
|
15
|
+
{ from: /\bwrite_file\b/g, to: 'artifact_save' },
|
|
16
|
+
{ from: /\bread_file\b/g, to: 'artifact_load' },
|
|
17
|
+
{ from: /\bedit_file\b/g, to: 'artifact_edit' },
|
|
18
|
+
{ from: /\bglob\b/g, to: 'artifact_glob' },
|
|
19
|
+
{ from: /\bgrep\b/g, to: 'artifact_grep' },
|
|
20
|
+
];
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* 替换提示词中的工具名称
|
|
24
|
+
*/
|
|
25
|
+
function replacePromptContent(prompt) {
|
|
26
|
+
if (!prompt) return prompt;
|
|
27
|
+
|
|
28
|
+
let result = prompt;
|
|
29
|
+
for (const rule of PROMPT_REPLACEMENTS) {
|
|
30
|
+
result = result.replace(rule.from, rule.to);
|
|
31
|
+
}
|
|
32
|
+
return result;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* 创建工具过滤中间件
|
|
37
|
+
* 在模型调用前过滤掉指定的工具,并替换提示词
|
|
38
|
+
*/
|
|
39
|
+
export function createFilterToolsMiddleware(options = {}) {
|
|
40
|
+
const { filteredTools = FILTERED_TOOLS } = options;
|
|
41
|
+
|
|
42
|
+
return createMiddleware({
|
|
43
|
+
name: 'filterToolsMiddleware',
|
|
44
|
+
|
|
45
|
+
// 在模型调用前过滤工具并替换提示词
|
|
46
|
+
wrapModelCall: async (request, handler) => {
|
|
47
|
+
// 过滤工具
|
|
48
|
+
const tools = request.tools?.filter(
|
|
49
|
+
(t) => !filteredTools.includes(t.name)
|
|
50
|
+
) || [];
|
|
51
|
+
|
|
52
|
+
// 替换系统提示词
|
|
53
|
+
const systemPrompt = replacePromptContent(request.systemPrompt);
|
|
54
|
+
|
|
55
|
+
return handler({
|
|
56
|
+
...request,
|
|
57
|
+
tools,
|
|
58
|
+
systemPrompt,
|
|
59
|
+
});
|
|
60
|
+
},
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export default createFilterToolsMiddleware;
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* DeepSpider - 报告中间件
|
|
3
|
+
* 在 Agent 执行完成后自动检测并准备报告
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { createMiddleware } from 'langchain';
|
|
7
|
+
import { ToolMessage } from '@langchain/core/messages';
|
|
8
|
+
import { z } from 'zod';
|
|
9
|
+
|
|
10
|
+
// 报告状态 schema
|
|
11
|
+
const reportStateSchema = z.object({
|
|
12
|
+
lastWrittenMdFile: z.string().optional(),
|
|
13
|
+
reportReady: z.boolean().default(false),
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* 创建报告中间件
|
|
18
|
+
* 在 afterModel 中检测 artifact_save 工具调用结果
|
|
19
|
+
* 在 afterAgent 中触发报告显示回调
|
|
20
|
+
*/
|
|
21
|
+
export function createReportMiddleware(options = {}) {
|
|
22
|
+
const { onReportReady } = options;
|
|
23
|
+
|
|
24
|
+
return createMiddleware({
|
|
25
|
+
name: 'reportMiddleware',
|
|
26
|
+
stateSchema: reportStateSchema,
|
|
27
|
+
|
|
28
|
+
// 模型调用后,检测工具调用结果
|
|
29
|
+
afterModel: (state) => {
|
|
30
|
+
const messages = state.messages;
|
|
31
|
+
if (!messages || messages.length === 0) return undefined;
|
|
32
|
+
|
|
33
|
+
// 查找最近的 ToolMessage
|
|
34
|
+
for (let i = messages.length - 1; i >= 0; i--) {
|
|
35
|
+
const msg = messages[i];
|
|
36
|
+
if (ToolMessage.isInstance(msg)) {
|
|
37
|
+
try {
|
|
38
|
+
const content = typeof msg.content === 'string'
|
|
39
|
+
? JSON.parse(msg.content)
|
|
40
|
+
: msg.content;
|
|
41
|
+
|
|
42
|
+
// 检测是否是 artifact_save 写入的 .md 文件
|
|
43
|
+
if (content.success && content.path?.endsWith('.md')) {
|
|
44
|
+
console.log('[reportMiddleware] 检测到 .md 文件:', content.path);
|
|
45
|
+
return { lastWrittenMdFile: content.path };
|
|
46
|
+
}
|
|
47
|
+
} catch (e) {
|
|
48
|
+
// 解析失败,忽略
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
return undefined;
|
|
53
|
+
},
|
|
54
|
+
|
|
55
|
+
// Agent 执行完成后
|
|
56
|
+
afterAgent: async (state) => {
|
|
57
|
+
const mdFile = state.lastWrittenMdFile;
|
|
58
|
+
|
|
59
|
+
if (mdFile) {
|
|
60
|
+
console.log('[reportMiddleware] afterAgent: 准备显示报告:', mdFile);
|
|
61
|
+
|
|
62
|
+
// 调用回调通知外部
|
|
63
|
+
if (onReportReady) {
|
|
64
|
+
try {
|
|
65
|
+
await onReportReady(mdFile);
|
|
66
|
+
} catch (e) {
|
|
67
|
+
console.error('[reportMiddleware] onReportReady 失败:', e.message);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
return { reportReady: true };
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
return undefined;
|
|
75
|
+
},
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export default createReportMiddleware;
|