create-deepagent-server 0.1.0 → 0.2.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/README.md +82 -8
- package/package.json +1 -1
- package/src/args.js +6 -4
- package/src/cli.js +1 -1
- package/src/dependencies.js +1 -1
- package/src/prompts.js +1 -0
- package/src/registry.js +8 -8
- package/src/scaffold.js +6 -7
- package/src/setup.js +2 -0
- package/src/version.js +1 -1
- package/src/versions.js +1 -1
- package/templates/deepagent/js/scripts/check.js +34 -11
- package/templates/deepagent/js/scripts/dev.js +9 -0
- package/templates/deepagent/js/src/agents/factory.js +2 -1
- package/templates/deepagent/js/src/agents/prompts/loader.js +9 -0
- package/templates/deepagent/js/src/agents/prompts/supervisor.js +4 -0
- package/templates/deepagent/js/src/agents/prompts/supervisor.md +1 -2
- package/templates/deepagent/js/src/agents/state/types.js +1 -0
- package/templates/deepagent/js/src/agents/subagents/analyst.js +4 -3
- package/templates/deepagent/js/src/agents/subagents/coder.js +1 -0
- package/templates/deepagent/js/src/agents/subagents/index.js +5 -4
- package/templates/deepagent/js/src/agents/supervisor.js +12 -4
- package/templates/deepagent/js/src/api/app.js +7 -0
- package/templates/deepagent/js/src/api/middleware/auth.js +9 -0
- package/templates/deepagent/js/src/api/middleware/error.js +4 -0
- package/templates/deepagent/js/src/api/routes/agent.js +19 -49
- package/templates/deepagent/js/src/api/routes/health.js +12 -3
- package/templates/deepagent/js/src/api/routes/session.js +28 -48
- package/templates/deepagent/js/src/app.js +31 -12
- package/templates/deepagent/js/src/backends/composite.js +2 -0
- package/templates/deepagent/js/src/backends/filesystem.js +1 -0
- package/templates/deepagent/js/src/backends/index.js +35 -3
- package/templates/deepagent/js/src/backends/sandbox.js +3 -0
- package/templates/deepagent/js/src/config/agent.js +1 -0
- package/templates/deepagent/js/src/config/database.js +19 -0
- package/templates/deepagent/js/src/config/env.js +46 -23
- package/templates/deepagent/js/src/config/index.js +13 -3
- package/templates/deepagent/js/src/graph/checkpointer.js +10 -2
- package/templates/deepagent/js/src/graph/compile.js +1 -0
- package/templates/deepagent/js/src/graph/store.js +10 -2
- package/templates/deepagent/js/src/index.js +9 -2
- package/templates/deepagent/js/src/memory/index.js +2 -0
- package/templates/deepagent/js/src/memory/manager.js +21 -10
- package/templates/deepagent/js/src/memory/store.js +7 -0
- package/templates/deepagent/js/src/middleware/approval.js +6 -1
- package/templates/deepagent/js/src/middleware/context.js +5 -0
- package/templates/deepagent/js/src/middleware/logging.js +47 -43
- package/templates/deepagent/js/src/middleware/security.js +7 -0
- package/templates/deepagent/js/src/model/config.js +38 -0
- package/templates/deepagent/js/src/model/index.js +9 -0
- package/templates/deepagent/js/src/model/openai-compatible.js +18 -0
- package/templates/deepagent/js/src/model/presets.js +71 -0
- package/templates/deepagent/js/src/sandbox/config.js +12 -0
- package/templates/deepagent/js/src/sandbox/index.js +3 -0
- package/templates/deepagent/js/src/sandbox/manager.js +161 -0
- package/templates/deepagent/js/src/sandbox/opensandbox.js +195 -0
- package/templates/deepagent/js/src/schemas/agent.js +7 -0
- package/templates/deepagent/js/src/schemas/common.js +5 -0
- package/templates/deepagent/js/src/schemas/locale.js +1 -0
- package/templates/deepagent/js/src/schemas/session.js +4 -0
- package/templates/deepagent/js/src/services/{agent.service.js → agent.js} +39 -20
- package/templates/deepagent/js/src/services/{memory.service.js → memory.js} +13 -7
- package/templates/deepagent/js/src/services/{session.service.js → session.js} +43 -17
- package/templates/deepagent/js/src/skills/index.js +4 -0
- package/templates/deepagent/js/src/skills/loader.js +20 -10
- package/templates/deepagent/js/src/storage/database/client.js +57 -0
- package/templates/deepagent/{ts/src/infrastructure → js/src/storage}/database/migrations/001_init.sql +1 -1
- package/templates/deepagent/js/src/{infrastructure → storage}/logger/index.js +2 -0
- package/templates/deepagent/js/src/{infrastructure → storage}/redis/client.js +25 -19
- package/templates/deepagent/js/src/{infrastructure → storage}/shutdown/index.js +20 -13
- package/templates/deepagent/js/src/tools/database/index.js +1 -1
- package/templates/deepagent/js/src/tools/database/query.js +44 -32
- package/templates/deepagent/js/src/tools/filesystem/index.js +16 -0
- package/templates/deepagent/js/src/tools/index.js +6 -11
- package/templates/deepagent/js/src/tools/mcp/client.js +7 -0
- package/templates/deepagent/js/src/tools/mcp/connections.js +13 -3
- package/templates/deepagent/js/src/tools/mcp/index.js +2 -0
- package/templates/deepagent/js/src/tools/mcp/registry.js +15 -11
- package/templates/deepagent/js/src/utils/errors.js +9 -0
- package/templates/deepagent/js/src/utils/helpers.js +15 -0
- package/templates/deepagent/js/src/utils/validation.js +15 -0
- package/templates/deepagent/js/tests/agents/prompts.test.js +9 -3
- package/templates/deepagent/js/tests/agents/supervisor.test.js +13 -5
- package/templates/deepagent/js/tests/config/database.test.js +40 -0
- package/templates/deepagent/js/tests/integration/api.test.js +18 -3
- package/templates/deepagent/js/tests/middleware/security.test.js +4 -1
- package/templates/deepagent/js/tests/model/config.test.js +61 -0
- package/templates/deepagent/js/tests/sandbox/config.test.js +46 -0
- package/templates/deepagent/js/tests/sandbox/manager.test.js +236 -0
- package/templates/deepagent/js/tests/sandbox/opensandbox.test.js +173 -0
- package/templates/deepagent/js/tests/services/session.test.js +41 -0
- package/templates/deepagent/js/tests/tools/query.test.js +5 -1
- package/templates/deepagent/shared/README.md +101 -14
- package/templates/deepagent/shared/_env.example +40 -10
- package/templates/deepagent/shared/deployment/docker-compose.yml +8 -4
- package/templates/deepagent/shared/resources/AGENTS.md +1 -1
- package/templates/deepagent/ts/scripts/check.ts +19 -11
- package/templates/deepagent/ts/scripts/copy-assets.ts +2 -0
- package/templates/deepagent/ts/src/agents/factory.ts +1 -1
- package/templates/deepagent/ts/src/agents/prompts/loader.ts +1 -0
- package/templates/deepagent/ts/src/agents/prompts/supervisor.md +1 -2
- package/templates/deepagent/ts/src/agents/prompts/supervisor.ts +1 -1
- package/templates/deepagent/ts/src/agents/subagents/analyst.ts +7 -3
- package/templates/deepagent/ts/src/agents/subagents/index.ts +4 -5
- package/templates/deepagent/ts/src/agents/supervisor.ts +11 -9
- package/templates/deepagent/ts/src/api/app.ts +4 -4
- package/templates/deepagent/ts/src/api/middleware/auth.ts +1 -0
- package/templates/deepagent/ts/src/api/middleware/error.ts +1 -1
- package/templates/deepagent/ts/src/api/routes/agent.ts +13 -58
- package/templates/deepagent/ts/src/api/routes/health.ts +5 -3
- package/templates/deepagent/ts/src/api/routes/session.ts +21 -51
- package/templates/deepagent/ts/src/app.ts +19 -12
- package/templates/deepagent/ts/src/backends/index.ts +41 -7
- package/templates/deepagent/ts/src/backends/sandbox.ts +1 -0
- package/templates/deepagent/ts/src/config/database.ts +21 -0
- package/templates/deepagent/ts/src/config/env.ts +32 -28
- package/templates/deepagent/ts/src/config/index.ts +12 -4
- package/templates/deepagent/ts/src/graph/checkpointer.ts +5 -2
- package/templates/deepagent/ts/src/graph/store.ts +5 -2
- package/templates/deepagent/ts/src/index.ts +2 -2
- package/templates/deepagent/ts/src/memory/manager.ts +12 -10
- package/templates/deepagent/ts/src/memory/store.ts +2 -0
- package/templates/deepagent/ts/src/middleware/approval.ts +2 -2
- package/templates/deepagent/ts/src/middleware/logging.ts +43 -44
- package/templates/deepagent/ts/src/middleware/security.ts +1 -0
- package/templates/deepagent/ts/src/model/config.ts +56 -0
- package/templates/deepagent/ts/src/model/index.ts +14 -0
- package/templates/deepagent/ts/src/model/openai-compatible.ts +21 -0
- package/templates/deepagent/ts/src/model/presets.ts +82 -0
- package/templates/deepagent/ts/src/sandbox/config.ts +30 -0
- package/templates/deepagent/ts/src/sandbox/index.ts +7 -0
- package/templates/deepagent/ts/src/sandbox/manager.ts +176 -0
- package/templates/deepagent/ts/src/sandbox/opensandbox.ts +216 -0
- package/templates/deepagent/ts/src/services/{agent.service.ts → agent.ts} +22 -22
- package/templates/deepagent/ts/src/services/{memory.service.ts → memory.ts} +7 -7
- package/templates/deepagent/ts/src/services/{session.service.ts → session.ts} +29 -19
- package/templates/deepagent/ts/src/skills/loader.ts +6 -10
- package/templates/deepagent/ts/src/storage/database/client.ts +65 -0
- package/templates/deepagent/{js/src/infrastructure → ts/src/storage}/database/migrations/001_init.sql +1 -1
- package/templates/deepagent/ts/src/{infrastructure → storage}/redis/client.ts +14 -21
- package/templates/deepagent/ts/src/{infrastructure → storage}/shutdown/index.ts +10 -13
- package/templates/deepagent/ts/src/tools/database/index.ts +5 -1
- package/templates/deepagent/ts/src/tools/database/query.ts +40 -32
- package/templates/deepagent/ts/src/tools/filesystem/index.ts +2 -0
- package/templates/deepagent/ts/src/tools/index.ts +10 -8
- package/templates/deepagent/ts/src/tools/mcp/client.ts +1 -0
- package/templates/deepagent/ts/src/tools/mcp/connections.ts +3 -3
- package/templates/deepagent/ts/src/tools/mcp/registry.ts +9 -12
- package/templates/deepagent/ts/src/utils/helpers.ts +5 -0
- package/templates/deepagent/ts/src/utils/validation.ts +17 -0
- package/templates/deepagent/ts/tests/agents/prompts.test.ts +3 -3
- package/templates/deepagent/ts/tests/agents/supervisor.test.ts +5 -5
- package/templates/deepagent/ts/tests/config/database.test.ts +40 -0
- package/templates/deepagent/ts/tests/integration/api.test.ts +5 -6
- package/templates/deepagent/ts/tests/middleware/security.test.ts +1 -1
- package/templates/deepagent/ts/tests/model/config.test.ts +61 -0
- package/templates/deepagent/ts/tests/sandbox/config.test.ts +46 -0
- package/templates/deepagent/ts/tests/sandbox/manager.test.ts +241 -0
- package/templates/deepagent/ts/tests/sandbox/opensandbox.test.ts +188 -0
- package/templates/deepagent/ts/tests/services/session.test.ts +45 -0
- package/templates/deepagent/ts/tests/tools/query.test.ts +1 -1
- package/templates/deepagent/js/src/agents/prompts/subagents/researcher.md +0 -16
- package/templates/deepagent/js/src/agents/subagents/researcher.js +0 -12
- package/templates/deepagent/js/src/config/model.js +0 -34
- package/templates/deepagent/js/src/infrastructure/database/client.js +0 -43
- package/templates/deepagent/js/src/tools/search/index.js +0 -1
- package/templates/deepagent/js/src/tools/search/web-search.js +0 -54
- package/templates/deepagent/shared/skills/research/SKILL.md +0 -31
- package/templates/deepagent/ts/src/agents/prompts/subagents/researcher.md +0 -16
- package/templates/deepagent/ts/src/agents/subagents/researcher.ts +0 -15
- package/templates/deepagent/ts/src/config/model.ts +0 -49
- package/templates/deepagent/ts/src/infrastructure/database/client.ts +0 -61
- package/templates/deepagent/ts/src/tools/search/index.ts +0 -1
- package/templates/deepagent/ts/src/tools/search/web-search.ts +0 -82
- /package/templates/deepagent/ts/src/{infrastructure → storage}/logger/index.ts +0 -0
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import { webSearchTool } from "../../tools/search/index.js";
|
|
2
|
-
import { loadPrompt } from "../prompts/index.js";
|
|
3
|
-
export const createResearcherSubagent = () => {
|
|
4
|
-
return {
|
|
5
|
-
name: "researcher",
|
|
6
|
-
description:
|
|
7
|
-
"从网络收集可验证的信息,阅读来源,并把带引用的研究笔记写入 /workspace/research/。",
|
|
8
|
-
systemPrompt: loadPrompt("subagents/researcher"),
|
|
9
|
-
tools: [webSearchTool],
|
|
10
|
-
skills: ["/skills/research/"],
|
|
11
|
-
};
|
|
12
|
-
};
|
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
import { ChatAnthropic } from "@langchain/anthropic";
|
|
2
|
-
import { ChatOpenAI } from "@langchain/openai";
|
|
3
|
-
export const DEFAULT_MODELS = {
|
|
4
|
-
openai: "gpt-4.1-mini",
|
|
5
|
-
anthropic: "claude-sonnet-4-5",
|
|
6
|
-
};
|
|
7
|
-
export const resolveModelConfig = (env) => {
|
|
8
|
-
return {
|
|
9
|
-
provider: env.MODEL_PROVIDER,
|
|
10
|
-
name: env.MODEL_NAME ?? DEFAULT_MODELS[env.MODEL_PROVIDER],
|
|
11
|
-
temperature: env.MODEL_TEMPERATURE,
|
|
12
|
-
maxTokens: env.MODEL_MAX_TOKENS,
|
|
13
|
-
...(env.OPENAI_BASE_URL ? { baseUrl: env.OPENAI_BASE_URL } : {}),
|
|
14
|
-
};
|
|
15
|
-
};
|
|
16
|
-
/**
|
|
17
|
-
* 构建主 Agent 和所有子 Agent 共用的聊天模型。
|
|
18
|
-
* 要接入其他提供商(Bedrock、Vertex、Ollama 等),替换此函数即可。
|
|
19
|
-
*/
|
|
20
|
-
export const createChatModel = (config) => {
|
|
21
|
-
if (config.provider === "anthropic") {
|
|
22
|
-
return new ChatAnthropic({
|
|
23
|
-
model: config.name,
|
|
24
|
-
temperature: config.temperature,
|
|
25
|
-
maxTokens: config.maxTokens,
|
|
26
|
-
});
|
|
27
|
-
}
|
|
28
|
-
return new ChatOpenAI({
|
|
29
|
-
model: config.name,
|
|
30
|
-
temperature: config.temperature,
|
|
31
|
-
maxTokens: config.maxTokens,
|
|
32
|
-
...(config.baseUrl ? { configuration: { baseURL: config.baseUrl } } : {}),
|
|
33
|
-
});
|
|
34
|
-
};
|
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
import { performance } from "node:perf_hooks";
|
|
2
|
-
import { Pool } from "pg";
|
|
3
|
-
import { ConfigurationError } from "../../utils/errors.js";
|
|
4
|
-
let pool;
|
|
5
|
-
/** 懒加载共享的 PostgreSQL 连接池。 */
|
|
6
|
-
export const getPool = (connectionString = process.env.DATABASE_URL) => {
|
|
7
|
-
if (!connectionString) {
|
|
8
|
-
throw new ConfigurationError("未配置 DATABASE_URL。请先在 .env 中设置后再使用数据库功能。");
|
|
9
|
-
}
|
|
10
|
-
pool ??= new Pool({
|
|
11
|
-
connectionString,
|
|
12
|
-
max: 10,
|
|
13
|
-
idleTimeoutMillis: 30_000,
|
|
14
|
-
connectionTimeoutMillis: 5_000,
|
|
15
|
-
});
|
|
16
|
-
return pool;
|
|
17
|
-
};
|
|
18
|
-
export const checkDatabase = async () => {
|
|
19
|
-
if (!process.env.DATABASE_URL) {
|
|
20
|
-
return { ok: false, error: "未配置 DATABASE_URL" };
|
|
21
|
-
}
|
|
22
|
-
const startedAt = performance.now();
|
|
23
|
-
try {
|
|
24
|
-
const client = await getPool().connect();
|
|
25
|
-
try {
|
|
26
|
-
await client.query("select 1");
|
|
27
|
-
} finally {
|
|
28
|
-
client.release();
|
|
29
|
-
}
|
|
30
|
-
return { ok: true, latencyMs: Math.round(performance.now() - startedAt) };
|
|
31
|
-
} catch (error) {
|
|
32
|
-
return {
|
|
33
|
-
ok: false,
|
|
34
|
-
error: error instanceof Error ? error.message : String(error),
|
|
35
|
-
};
|
|
36
|
-
}
|
|
37
|
-
};
|
|
38
|
-
export const closeDatabase = async () => {
|
|
39
|
-
if (!pool) return;
|
|
40
|
-
const current = pool;
|
|
41
|
-
pool = undefined;
|
|
42
|
-
await current.end();
|
|
43
|
-
};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export { createWebSearchTool, webSearchTool } from "./web-search.js";
|
|
@@ -1,54 +0,0 @@
|
|
|
1
|
-
import { tool } from "@langchain/core/tools";
|
|
2
|
-
import { z } from "zod";
|
|
3
|
-
const TAVILY_ENDPOINT = "https://api.tavily.com/search";
|
|
4
|
-
const webSearchSchema = z.object({
|
|
5
|
-
query: z.string().min(2).describe("搜索关键词。"),
|
|
6
|
-
maxResults: z.number().int().min(1).max(10).default(5).describe("返回结果数量(1-10)。"),
|
|
7
|
-
});
|
|
8
|
-
/**
|
|
9
|
-
* 基于 Tavily 的 Web 搜索。API Key 延迟读取,因此即使未配置搜索也能注册
|
|
10
|
-
* 该工具;真正调用时会返回清晰错误,供模型报告。
|
|
11
|
-
*/
|
|
12
|
-
export const createWebSearchTool = ({ apiKey, maxResults: defaultMaxResults = 5 } = {}) => {
|
|
13
|
-
return tool(
|
|
14
|
-
async ({ query, maxResults }) => {
|
|
15
|
-
const key = apiKey ?? process.env.TAVILY_API_KEY;
|
|
16
|
-
if (!key) {
|
|
17
|
-
throw new Error("未设置 TAVILY_API_KEY。请在 .env 中添加后启用 Web 搜索。");
|
|
18
|
-
}
|
|
19
|
-
const response = await fetch(TAVILY_ENDPOINT, {
|
|
20
|
-
method: "POST",
|
|
21
|
-
headers: { "content-type": "application/json" },
|
|
22
|
-
body: JSON.stringify({
|
|
23
|
-
api_key: key,
|
|
24
|
-
query,
|
|
25
|
-
max_results: Math.min(maxResults ?? defaultMaxResults, 10),
|
|
26
|
-
search_depth: "basic",
|
|
27
|
-
include_answer: false,
|
|
28
|
-
include_raw_content: false,
|
|
29
|
-
}),
|
|
30
|
-
signal: AbortSignal.timeout(20_000),
|
|
31
|
-
});
|
|
32
|
-
if (!response.ok) {
|
|
33
|
-
const detail = await response.text().catch(() => "");
|
|
34
|
-
throw new Error(
|
|
35
|
-
`Web 搜索失败,HTTP ${response.status}${detail ? `:${detail.slice(0, 300)}` : ""}`,
|
|
36
|
-
);
|
|
37
|
-
}
|
|
38
|
-
const data = await response.json();
|
|
39
|
-
const results = (data.results ?? []).map((result) => ({
|
|
40
|
-
title: result.title ?? "",
|
|
41
|
-
url: result.url ?? "",
|
|
42
|
-
snippet: result.content ?? "",
|
|
43
|
-
score: result.score ?? 0,
|
|
44
|
-
}));
|
|
45
|
-
return JSON.stringify({ query, results }, null, 2);
|
|
46
|
-
},
|
|
47
|
-
{
|
|
48
|
-
name: "web_search",
|
|
49
|
-
description: "搜索公开网络,返回标题、URL 和摘要。用于获取最新信息,并引用实际使用的来源。",
|
|
50
|
-
schema: webSearchSchema,
|
|
51
|
-
},
|
|
52
|
-
);
|
|
53
|
-
};
|
|
54
|
-
export const webSearchTool = createWebSearchTool();
|
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
---
|
|
2
|
-
name: research
|
|
3
|
-
description: 当问题需要最新的外部事实、多来源对比,或需要把带引用的研究笔记写入工作区时使用。
|
|
4
|
-
---
|
|
5
|
-
|
|
6
|
-
# 研究(Research)
|
|
7
|
-
|
|
8
|
-
## 适用场景
|
|
9
|
-
|
|
10
|
-
- 答案依赖训练截止日期之后的信息。
|
|
11
|
-
- 用户要求来源、对比、市场/竞品数据或官方文档。
|
|
12
|
-
- 某个结论在复述前需要先核实。
|
|
13
|
-
|
|
14
|
-
## 工作流程
|
|
15
|
-
|
|
16
|
-
1. 复述子问题,以及这些信息要支撑的决策。
|
|
17
|
-
2. 使用 `web_search` 做 2-4 次聚焦搜索,而不是一次宽泛搜索。
|
|
18
|
-
3. 优先选择一手来源:官方文档、变更日志、论文、源码、财报。
|
|
19
|
-
4. 打开最有价值的链接,只摘取与子问题相关的事实。
|
|
20
|
-
5. 写入 `/workspace/research/<主题>.md`,包含:
|
|
21
|
-
- 5 条以内的摘要;
|
|
22
|
-
- 按子问题分组的发现;
|
|
23
|
-
- 带 URL 和日期的来源列表。
|
|
24
|
-
6. 把摘要和文件路径返回给主 Agent。
|
|
25
|
-
|
|
26
|
-
## 质量标准
|
|
27
|
-
|
|
28
|
-
- 每个事实旁边都要有 URL。
|
|
29
|
-
- 来源冲突时如实报告,不要取平均值掩盖矛盾。
|
|
30
|
-
- 无日期或无法验证的结论标注为 `unverified`。
|
|
31
|
-
- 没有来源就不下结论:说明哪些内容无法核实。
|
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
你是 researcher(研究)子 Agent。你负责收集可验证的信息,并整理成主 Agent 可信赖的笔记。
|
|
2
|
-
|
|
3
|
-
## 工作流程
|
|
4
|
-
|
|
5
|
-
1. 把问题拆成 2-5 个具体的子问题。
|
|
6
|
-
2. 使用 `web_search` 搜索。优先选择一手来源(官方文档、论文、源码),而不是聚合站点。
|
|
7
|
-
3. 阅读最有价值的来源,只提取能回答子问题的事实。
|
|
8
|
-
4. 把笔记写入 `/workspace/research/<主题>.md`,包含简短摘要和来源列表。
|
|
9
|
-
5. 返回摘要和文件路径。
|
|
10
|
-
|
|
11
|
-
## 规则
|
|
12
|
-
|
|
13
|
-
- 每个事实性结论都必须附 URL。无法验证的来源要明确说明。
|
|
14
|
-
- 时间敏感的信息要记录发布日期,并标注可能过时的内容。
|
|
15
|
-
- 控制引用篇幅,不要大段复制原文。
|
|
16
|
-
- 搜索工具不可用时明确说明,并标注所给信息为“未经验证”。
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import type { SubAgent } from "deepagents";
|
|
2
|
-
|
|
3
|
-
import { webSearchTool } from "../../tools/search/index.js";
|
|
4
|
-
import { loadPrompt } from "../prompts/index.js";
|
|
5
|
-
|
|
6
|
-
export const createResearcherSubagent = (): SubAgent => {
|
|
7
|
-
return {
|
|
8
|
-
name: "researcher",
|
|
9
|
-
description:
|
|
10
|
-
"从网络收集可验证的信息,阅读来源,并把带引用的研究笔记写入 /workspace/research/。",
|
|
11
|
-
systemPrompt: loadPrompt("subagents/researcher"),
|
|
12
|
-
tools: [webSearchTool],
|
|
13
|
-
skills: ["/skills/research/"],
|
|
14
|
-
};
|
|
15
|
-
};
|
|
@@ -1,49 +0,0 @@
|
|
|
1
|
-
import { ChatAnthropic } from "@langchain/anthropic";
|
|
2
|
-
import type { BaseChatModel } from "@langchain/core/language_models/chat_models";
|
|
3
|
-
import { ChatOpenAI } from "@langchain/openai";
|
|
4
|
-
|
|
5
|
-
import type { Env } from "./env.js";
|
|
6
|
-
|
|
7
|
-
export interface ModelConfig {
|
|
8
|
-
provider: "openai" | "anthropic";
|
|
9
|
-
name: string;
|
|
10
|
-
temperature: number;
|
|
11
|
-
maxTokens: number;
|
|
12
|
-
baseUrl?: string;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
export const DEFAULT_MODELS = {
|
|
16
|
-
openai: "gpt-4.1-mini",
|
|
17
|
-
anthropic: "claude-sonnet-4-5",
|
|
18
|
-
} as const;
|
|
19
|
-
|
|
20
|
-
export const resolveModelConfig = (env: Env): ModelConfig => {
|
|
21
|
-
return {
|
|
22
|
-
provider: env.MODEL_PROVIDER,
|
|
23
|
-
name: env.MODEL_NAME ?? DEFAULT_MODELS[env.MODEL_PROVIDER],
|
|
24
|
-
temperature: env.MODEL_TEMPERATURE,
|
|
25
|
-
maxTokens: env.MODEL_MAX_TOKENS,
|
|
26
|
-
...(env.OPENAI_BASE_URL ? { baseUrl: env.OPENAI_BASE_URL } : {}),
|
|
27
|
-
};
|
|
28
|
-
};
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* 构建主 Agent 和所有子 Agent 共用的聊天模型。
|
|
32
|
-
* 要接入其他提供商(Bedrock、Vertex、Ollama 等),替换此函数即可。
|
|
33
|
-
*/
|
|
34
|
-
export const createChatModel = (config: ModelConfig): BaseChatModel => {
|
|
35
|
-
if (config.provider === "anthropic") {
|
|
36
|
-
return new ChatAnthropic({
|
|
37
|
-
model: config.name,
|
|
38
|
-
temperature: config.temperature,
|
|
39
|
-
maxTokens: config.maxTokens,
|
|
40
|
-
});
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
return new ChatOpenAI({
|
|
44
|
-
model: config.name,
|
|
45
|
-
temperature: config.temperature,
|
|
46
|
-
maxTokens: config.maxTokens,
|
|
47
|
-
...(config.baseUrl ? { configuration: { baseURL: config.baseUrl } } : {}),
|
|
48
|
-
});
|
|
49
|
-
};
|
|
@@ -1,61 +0,0 @@
|
|
|
1
|
-
import { performance } from "node:perf_hooks";
|
|
2
|
-
|
|
3
|
-
import { Pool, type PoolClient } from "pg";
|
|
4
|
-
|
|
5
|
-
import { ConfigurationError } from "../../utils/errors.js";
|
|
6
|
-
|
|
7
|
-
let pool: Pool | undefined;
|
|
8
|
-
|
|
9
|
-
/** 懒加载共享的 PostgreSQL 连接池。 */
|
|
10
|
-
export const getPool = (connectionString = process.env.DATABASE_URL): Pool => {
|
|
11
|
-
if (!connectionString) {
|
|
12
|
-
throw new ConfigurationError("未配置 DATABASE_URL。请先在 .env 中设置后再使用数据库功能。");
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
pool ??= new Pool({
|
|
16
|
-
connectionString,
|
|
17
|
-
max: 10,
|
|
18
|
-
idleTimeoutMillis: 30_000,
|
|
19
|
-
connectionTimeoutMillis: 5_000,
|
|
20
|
-
});
|
|
21
|
-
|
|
22
|
-
return pool;
|
|
23
|
-
};
|
|
24
|
-
|
|
25
|
-
export interface DatabaseHealth {
|
|
26
|
-
ok: boolean;
|
|
27
|
-
latencyMs?: number;
|
|
28
|
-
error?: string;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
export const checkDatabase = async (): Promise<DatabaseHealth> => {
|
|
32
|
-
if (!process.env.DATABASE_URL) {
|
|
33
|
-
return { ok: false, error: "未配置 DATABASE_URL" };
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
const startedAt = performance.now();
|
|
37
|
-
|
|
38
|
-
try {
|
|
39
|
-
const client: PoolClient = await getPool().connect();
|
|
40
|
-
try {
|
|
41
|
-
await client.query("select 1");
|
|
42
|
-
} finally {
|
|
43
|
-
client.release();
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
return { ok: true, latencyMs: Math.round(performance.now() - startedAt) };
|
|
47
|
-
} catch (error) {
|
|
48
|
-
return {
|
|
49
|
-
ok: false,
|
|
50
|
-
error: error instanceof Error ? error.message : String(error),
|
|
51
|
-
};
|
|
52
|
-
}
|
|
53
|
-
};
|
|
54
|
-
|
|
55
|
-
export const closeDatabase = async (): Promise<void> => {
|
|
56
|
-
if (!pool) return;
|
|
57
|
-
|
|
58
|
-
const current = pool;
|
|
59
|
-
pool = undefined;
|
|
60
|
-
await current.end();
|
|
61
|
-
};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export { createWebSearchTool, webSearchTool, type WebSearchToolOptions } from "./web-search.js";
|
|
@@ -1,82 +0,0 @@
|
|
|
1
|
-
import { tool } from "@langchain/core/tools";
|
|
2
|
-
import { z } from "zod";
|
|
3
|
-
|
|
4
|
-
const TAVILY_ENDPOINT = "https://api.tavily.com/search";
|
|
5
|
-
|
|
6
|
-
interface TavilyResult {
|
|
7
|
-
title?: string;
|
|
8
|
-
url?: string;
|
|
9
|
-
content?: string;
|
|
10
|
-
score?: number;
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
interface TavilyResponse {
|
|
14
|
-
answer?: string;
|
|
15
|
-
results?: TavilyResult[];
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
export interface WebSearchToolOptions {
|
|
19
|
-
apiKey?: string;
|
|
20
|
-
maxResults?: number;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
const webSearchSchema = z.object({
|
|
24
|
-
query: z.string().min(2).describe("搜索关键词。"),
|
|
25
|
-
maxResults: z.number().int().min(1).max(10).default(5).describe("返回结果数量(1-10)。"),
|
|
26
|
-
});
|
|
27
|
-
|
|
28
|
-
/**
|
|
29
|
-
* 基于 Tavily 的 Web 搜索。API Key 延迟读取,因此即使未配置搜索也能注册
|
|
30
|
-
* 该工具;真正调用时会返回清晰错误,供模型报告。
|
|
31
|
-
*/
|
|
32
|
-
export const createWebSearchTool = ({
|
|
33
|
-
apiKey,
|
|
34
|
-
maxResults: defaultMaxResults = 5,
|
|
35
|
-
}: WebSearchToolOptions = {}) => {
|
|
36
|
-
return tool(
|
|
37
|
-
async ({ query, maxResults }: z.infer<typeof webSearchSchema>) => {
|
|
38
|
-
const key = apiKey ?? process.env.TAVILY_API_KEY;
|
|
39
|
-
if (!key) {
|
|
40
|
-
throw new Error("未设置 TAVILY_API_KEY。请在 .env 中添加后启用 Web 搜索。");
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
const response = await fetch(TAVILY_ENDPOINT, {
|
|
44
|
-
method: "POST",
|
|
45
|
-
headers: { "content-type": "application/json" },
|
|
46
|
-
body: JSON.stringify({
|
|
47
|
-
api_key: key,
|
|
48
|
-
query,
|
|
49
|
-
max_results: Math.min(maxResults ?? defaultMaxResults, 10),
|
|
50
|
-
search_depth: "basic",
|
|
51
|
-
include_answer: false,
|
|
52
|
-
include_raw_content: false,
|
|
53
|
-
}),
|
|
54
|
-
signal: AbortSignal.timeout(20_000),
|
|
55
|
-
});
|
|
56
|
-
|
|
57
|
-
if (!response.ok) {
|
|
58
|
-
const detail = await response.text().catch(() => "");
|
|
59
|
-
throw new Error(
|
|
60
|
-
`Web 搜索失败,HTTP ${response.status}${detail ? `:${detail.slice(0, 300)}` : ""}`,
|
|
61
|
-
);
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
const data = (await response.json()) as TavilyResponse;
|
|
65
|
-
const results = (data.results ?? []).map((result) => ({
|
|
66
|
-
title: result.title ?? "",
|
|
67
|
-
url: result.url ?? "",
|
|
68
|
-
snippet: result.content ?? "",
|
|
69
|
-
score: result.score ?? 0,
|
|
70
|
-
}));
|
|
71
|
-
|
|
72
|
-
return JSON.stringify({ query, results }, null, 2);
|
|
73
|
-
},
|
|
74
|
-
{
|
|
75
|
-
name: "web_search",
|
|
76
|
-
description: "搜索公开网络,返回标题、URL 和摘要。用于获取最新信息,并引用实际使用的来源。",
|
|
77
|
-
schema: webSearchSchema,
|
|
78
|
-
},
|
|
79
|
-
);
|
|
80
|
-
};
|
|
81
|
-
|
|
82
|
-
export const webSearchTool = createWebSearchTool();
|
|
File without changes
|