create-deepagent-server 0.1.0 → 1.0.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 +83 -8
- package/package.json +1 -1
- package/src/args.js +6 -4
- package/src/cli.js +1 -4
- package/src/dependencies.js +1 -1
- package/src/prompts.js +1 -0
- package/src/registry.js +8 -8
- package/src/scaffold.js +21 -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
|
@@ -3,25 +3,31 @@
|
|
|
3
3
|
* 让路由层不需要了解命名空间和 store 细节。
|
|
4
4
|
*/
|
|
5
5
|
export class MemoryService {
|
|
6
|
-
|
|
6
|
+
memory;
|
|
7
|
+
|
|
7
8
|
constructor(memory) {
|
|
8
|
-
this
|
|
9
|
+
this.memory = memory;
|
|
9
10
|
}
|
|
11
|
+
|
|
10
12
|
async remember({ tenantId, userId, key, value, kind = "fact" }) {
|
|
11
13
|
const target = { tenantId, ...(userId ? { userId } : {}) };
|
|
14
|
+
|
|
12
15
|
if (kind === "preference") {
|
|
13
|
-
await this
|
|
16
|
+
await this.memory.rememberPreference(target, key, value);
|
|
14
17
|
} else {
|
|
15
|
-
await this
|
|
18
|
+
await this.memory.rememberFact(target, key, value);
|
|
16
19
|
}
|
|
17
20
|
}
|
|
21
|
+
|
|
18
22
|
async recall(tenantId, key, userId) {
|
|
19
|
-
return this
|
|
23
|
+
return this.memory.recall({ tenantId, ...(userId ? { userId } : {}) }, key);
|
|
20
24
|
}
|
|
25
|
+
|
|
21
26
|
async list(tenantId, userId, limit = 50) {
|
|
22
|
-
return this
|
|
27
|
+
return this.memory.list({ tenantId, ...(userId ? { userId } : {}) }, limit);
|
|
23
28
|
}
|
|
29
|
+
|
|
24
30
|
async forget(tenantId, key, userId) {
|
|
25
|
-
await this
|
|
31
|
+
await this.memory.forget({ tenantId, ...(userId ? { userId } : {}) }, key);
|
|
26
32
|
}
|
|
27
33
|
}
|
|
@@ -1,22 +1,29 @@
|
|
|
1
1
|
import { randomUUID } from "node:crypto";
|
|
2
|
+
|
|
2
3
|
import { NotFoundError } from "../utils/errors.js";
|
|
4
|
+
|
|
3
5
|
/**
|
|
4
6
|
* 会话元数据存放在 LangGraph store 的
|
|
5
7
|
* `["sessions", tenantId, threadId]` 下;对话本身存放在 checkpointer 中。
|
|
6
8
|
* 该服务在 HTTP API 层把两者组合起来。
|
|
7
9
|
*/
|
|
8
10
|
export class SessionService {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
this
|
|
11
|
+
graph;
|
|
12
|
+
checkpointer;
|
|
13
|
+
store;
|
|
14
|
+
sandboxes;
|
|
15
|
+
|
|
16
|
+
constructor({ graph, checkpointer, store, sandboxes }) {
|
|
17
|
+
this.graph = graph;
|
|
18
|
+
this.checkpointer = checkpointer;
|
|
19
|
+
this.store = store;
|
|
20
|
+
this.sandboxes = sandboxes;
|
|
16
21
|
}
|
|
22
|
+
|
|
17
23
|
async create(input) {
|
|
18
24
|
const threadId = input.threadId ?? randomUUID();
|
|
19
25
|
const now = new Date().toISOString();
|
|
26
|
+
|
|
20
27
|
const record = {
|
|
21
28
|
threadId,
|
|
22
29
|
title: input.title,
|
|
@@ -27,13 +34,17 @@ export class SessionService {
|
|
|
27
34
|
messageCount: 0,
|
|
28
35
|
metadata: input.metadata,
|
|
29
36
|
};
|
|
30
|
-
|
|
37
|
+
|
|
38
|
+
await this.store.put(namespace(input.tenantId), threadId, record);
|
|
39
|
+
|
|
31
40
|
return { ...record };
|
|
32
41
|
}
|
|
42
|
+
|
|
33
43
|
/** 会话记录不存在时创建(幂等)。 */
|
|
34
44
|
async ensure({ threadId, tenantId, userId }) {
|
|
35
|
-
const existing = await this
|
|
45
|
+
const existing = await this.store.get(namespace(tenantId), threadId);
|
|
36
46
|
if (existing) return;
|
|
47
|
+
|
|
37
48
|
await this.create({
|
|
38
49
|
threadId,
|
|
39
50
|
tenantId,
|
|
@@ -42,51 +53,66 @@ export class SessionService {
|
|
|
42
53
|
...(userId ? { userId } : {}),
|
|
43
54
|
});
|
|
44
55
|
}
|
|
56
|
+
|
|
45
57
|
async touch({ threadId, tenantId, lastMessage }) {
|
|
46
|
-
const item = await this
|
|
58
|
+
const item = await this.store.get(namespace(tenantId), threadId);
|
|
47
59
|
if (!item) return;
|
|
60
|
+
|
|
48
61
|
const record = item.value;
|
|
49
|
-
|
|
62
|
+
|
|
63
|
+
await this.store.put(namespace(tenantId), threadId, {
|
|
50
64
|
...record,
|
|
51
65
|
updatedAt: new Date().toISOString(),
|
|
52
66
|
messageCount: record.messageCount + 2,
|
|
53
67
|
...(lastMessage ? { lastMessage: lastMessage.slice(0, 1_000) } : {}),
|
|
54
68
|
});
|
|
55
69
|
}
|
|
70
|
+
|
|
56
71
|
async get(threadId, tenantId = "default") {
|
|
57
|
-
const item = await this
|
|
72
|
+
const item = await this.store.get(namespace(tenantId), threadId);
|
|
58
73
|
if (!item) {
|
|
59
74
|
throw new NotFoundError(`未找到会话 "${threadId}"。`);
|
|
60
75
|
}
|
|
76
|
+
|
|
61
77
|
const record = item.value;
|
|
62
|
-
|
|
78
|
+
|
|
79
|
+
const state = await this.graph.getState({
|
|
63
80
|
configurable: { thread_id: threadId },
|
|
64
81
|
});
|
|
65
82
|
const values = state.values;
|
|
83
|
+
|
|
66
84
|
return {
|
|
67
85
|
...record,
|
|
68
86
|
messageCount: values?.messages?.length ?? record.messageCount,
|
|
69
87
|
};
|
|
70
88
|
}
|
|
89
|
+
|
|
71
90
|
async list(tenantId = "default", limit = 20) {
|
|
72
|
-
const items = await this
|
|
91
|
+
const items = await this.store.search(namespace(tenantId), { limit });
|
|
92
|
+
|
|
73
93
|
const sessions = items.map(({ value }) => ({
|
|
74
94
|
...value,
|
|
75
95
|
}));
|
|
96
|
+
|
|
76
97
|
return { sessions, total: sessions.length };
|
|
77
98
|
}
|
|
99
|
+
|
|
78
100
|
async remove(threadId, tenantId = "default") {
|
|
79
|
-
await this
|
|
80
|
-
await this
|
|
101
|
+
await this.checkpointer.deleteThread(threadId);
|
|
102
|
+
await this.store.delete(namespace(tenantId), threadId);
|
|
103
|
+
await this.sandboxes?.release(threadId);
|
|
81
104
|
}
|
|
105
|
+
|
|
82
106
|
async history(threadId) {
|
|
83
|
-
const state = await this
|
|
107
|
+
const state = await this.graph.getState({
|
|
84
108
|
configurable: { thread_id: threadId },
|
|
85
109
|
});
|
|
86
110
|
const values = state.values;
|
|
111
|
+
|
|
87
112
|
return values?.messages ?? [];
|
|
88
113
|
}
|
|
89
114
|
}
|
|
115
|
+
|
|
90
116
|
const namespace = (tenantId) => {
|
|
91
117
|
return ["sessions", tenantId];
|
|
92
118
|
};
|
|
@@ -1,11 +1,15 @@
|
|
|
1
1
|
import { discoverSkills } from "./loader.js";
|
|
2
|
+
|
|
2
3
|
export { discoverSkills, parseSkillFrontmatter } from "./loader.js";
|
|
4
|
+
|
|
3
5
|
/** Deep Agents skills 中间件扫描的虚拟后端路径。 */
|
|
4
6
|
export const DEFAULT_SKILL_SOURCES = ["/skills/"];
|
|
7
|
+
|
|
5
8
|
/** 从磁盘发现技能(用于启动校验和 `scripts/check`)。 */
|
|
6
9
|
export const loadSkills = async ({ rootDir }) => {
|
|
7
10
|
return discoverSkills(rootDir);
|
|
8
11
|
};
|
|
12
|
+
|
|
9
13
|
/**
|
|
10
14
|
* 为缺少 description 的技能返回可读警告。
|
|
11
15
|
* 模型正是依赖 description 来选择技能。
|
|
@@ -1,45 +1,51 @@
|
|
|
1
1
|
import fs from "node:fs/promises";
|
|
2
2
|
import path from "node:path";
|
|
3
|
+
|
|
3
4
|
/** 不依赖 YAML 库,解析 SKILL.md 的 frontmatter。 */
|
|
4
5
|
export const parseSkillFrontmatter = (content) => {
|
|
5
6
|
if (!content.startsWith("---")) return {};
|
|
7
|
+
|
|
6
8
|
const end = content.indexOf("\n---", 3);
|
|
7
9
|
if (end === -1) return {};
|
|
10
|
+
|
|
8
11
|
const frontmatter = content.slice(3, end);
|
|
9
12
|
const fields = {};
|
|
13
|
+
|
|
10
14
|
for (const line of frontmatter.split("\n")) {
|
|
11
15
|
const separator = line.indexOf(":");
|
|
12
16
|
if (separator === -1) continue;
|
|
17
|
+
|
|
13
18
|
const key = line.slice(0, separator).trim();
|
|
14
19
|
const value = line
|
|
15
20
|
.slice(separator + 1)
|
|
16
21
|
.trim()
|
|
17
22
|
.replace(/^["']|["']$/g, "");
|
|
23
|
+
|
|
18
24
|
if (key) fields[key] = value;
|
|
19
25
|
}
|
|
26
|
+
|
|
20
27
|
return {
|
|
21
28
|
...(fields.name ? { name: fields.name } : {}),
|
|
22
29
|
...(fields.description ? { description: fields.description } : {}),
|
|
23
30
|
};
|
|
24
31
|
};
|
|
32
|
+
|
|
25
33
|
/** 读取目录;目录不存在时返回空数组。 */
|
|
26
|
-
const readDirectory =
|
|
27
|
-
|
|
28
|
-
return await fs.readdir(directory, { withFileTypes: true });
|
|
29
|
-
} catch (error) {
|
|
34
|
+
const readDirectory = (directory) => {
|
|
35
|
+
return fs.readdir(directory, { withFileTypes: true }).catch((error) => {
|
|
30
36
|
if (error.code === "ENOENT") return [];
|
|
31
37
|
throw error;
|
|
32
|
-
}
|
|
38
|
+
});
|
|
33
39
|
};
|
|
40
|
+
|
|
34
41
|
/** 读取文本文件;文件不存在时返回 null。 */
|
|
35
|
-
const readTextFile =
|
|
36
|
-
|
|
37
|
-
return await fs.readFile(file, "utf8");
|
|
38
|
-
} catch (error) {
|
|
42
|
+
const readTextFile = (file) => {
|
|
43
|
+
return fs.readFile(file, "utf8").catch((error) => {
|
|
39
44
|
if (error.code === "ENOENT") return null;
|
|
40
45
|
throw error;
|
|
41
|
-
}
|
|
46
|
+
});
|
|
42
47
|
};
|
|
48
|
+
|
|
43
49
|
/**
|
|
44
50
|
* 扫描 `rootDir` 下包含 SKILL.md 的子目录。
|
|
45
51
|
* 目录不存在时返回空数组,保证没有技能时也能启动。
|
|
@@ -47,12 +53,15 @@ const readTextFile = async (file) => {
|
|
|
47
53
|
export const discoverSkills = async (rootDir) => {
|
|
48
54
|
const entries = await readDirectory(rootDir);
|
|
49
55
|
const skills = [];
|
|
56
|
+
|
|
50
57
|
for (const entry of entries) {
|
|
51
58
|
if (!entry.isDirectory()) continue;
|
|
59
|
+
|
|
52
60
|
const directory = path.join(rootDir, entry.name);
|
|
53
61
|
const file = path.join(directory, "SKILL.md");
|
|
54
62
|
const content = await readTextFile(file);
|
|
55
63
|
if (content === null) continue;
|
|
64
|
+
|
|
56
65
|
const metadata = parseSkillFrontmatter(content);
|
|
57
66
|
skills.push({
|
|
58
67
|
name: metadata.name ?? entry.name,
|
|
@@ -61,5 +70,6 @@ export const discoverSkills = async (rootDir) => {
|
|
|
61
70
|
file,
|
|
62
71
|
});
|
|
63
72
|
}
|
|
73
|
+
|
|
64
74
|
return skills.sort((a, b) => a.name.localeCompare(b.name));
|
|
65
75
|
};
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { performance } from "node:perf_hooks";
|
|
2
|
+
|
|
3
|
+
import { Pool } from "pg";
|
|
4
|
+
|
|
5
|
+
import { ConfigurationError } from "../../utils/errors.js";
|
|
6
|
+
import { errorMessage } from "../../utils/helpers.js";
|
|
7
|
+
|
|
8
|
+
let pool;
|
|
9
|
+
|
|
10
|
+
/** 懒加载共享的 PostgreSQL 连接池。 */
|
|
11
|
+
export const getPool = (connectionString = process.env.DATABASE_URL) => {
|
|
12
|
+
if (!connectionString) {
|
|
13
|
+
throw new ConfigurationError(
|
|
14
|
+
"未配置 PostgreSQL 连接信息。请设置 DATABASE_URL 或 POSTGRES_* 后使用数据库功能。",
|
|
15
|
+
);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
pool ??= new Pool({
|
|
19
|
+
connectionString,
|
|
20
|
+
max: 10,
|
|
21
|
+
idleTimeoutMillis: 30_000,
|
|
22
|
+
connectionTimeoutMillis: 5_000,
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
return pool;
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
const probe = async (connectionString) => {
|
|
29
|
+
const client = await getPool(connectionString).connect();
|
|
30
|
+
const startedAt = performance.now();
|
|
31
|
+
|
|
32
|
+
return client
|
|
33
|
+
.query("select 1")
|
|
34
|
+
.then(() => ({ ok: true, latencyMs: Math.round(performance.now() - startedAt) }))
|
|
35
|
+
.finally(() => client.release());
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
/** 健康检查只返回状态,不向上抛错:连接或查询失败都折算成 `ok: false`。 */
|
|
39
|
+
export const checkDatabase = async (connectionString = process.env.DATABASE_URL) => {
|
|
40
|
+
if (!connectionString) {
|
|
41
|
+
return { ok: false, error: "未配置 PostgreSQL 连接信息" };
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
return probe(connectionString).catch((error) => ({
|
|
45
|
+
ok: false,
|
|
46
|
+
error: errorMessage(error),
|
|
47
|
+
}));
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
export const closeDatabase = async () => {
|
|
51
|
+
if (!pool) return;
|
|
52
|
+
|
|
53
|
+
const current = pool;
|
|
54
|
+
pool = undefined;
|
|
55
|
+
|
|
56
|
+
await current.end();
|
|
57
|
+
};
|
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
-- 所需的表;本迁移只用于应用自有的表结构。
|
|
6
6
|
--
|
|
7
7
|
-- 使用 psql 或你常用的迁移工具执行:
|
|
8
|
-
-- psql "$DATABASE_URL" -f src/
|
|
8
|
+
-- psql "$DATABASE_URL" -f src/storage/database/migrations/001_init.sql
|
|
9
9
|
|
|
10
10
|
CREATE TABLE IF NOT EXISTS agent_audit_log (
|
|
11
11
|
id BIGSERIAL PRIMARY KEY,
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import pino from "pino";
|
|
2
|
+
|
|
2
3
|
/** `LOG_PRETTY=true` 时使用的 pino-pretty 传输配置。 */
|
|
3
4
|
const PRETTY_TRANSPORT = {
|
|
4
5
|
transport: {
|
|
@@ -10,6 +11,7 @@ const PRETTY_TRANSPORT = {
|
|
|
10
11
|
},
|
|
11
12
|
},
|
|
12
13
|
};
|
|
14
|
+
|
|
13
15
|
export const createLogger = (config) => {
|
|
14
16
|
return pino({
|
|
15
17
|
level: config.logging.level,
|
|
@@ -1,45 +1,51 @@
|
|
|
1
1
|
import { performance } from "node:perf_hooks";
|
|
2
|
+
|
|
2
3
|
import { Redis } from "ioredis";
|
|
4
|
+
|
|
3
5
|
import { ConfigurationError } from "../../utils/errors.js";
|
|
6
|
+
import { errorMessage } from "../../utils/helpers.js";
|
|
7
|
+
|
|
4
8
|
let client;
|
|
9
|
+
|
|
5
10
|
/** 懒加载共享的 Redis 客户端(首次执行命令时才建立连接)。 */
|
|
6
11
|
export const getRedis = (url = process.env.REDIS_URL) => {
|
|
7
12
|
if (!url) {
|
|
8
13
|
throw new ConfigurationError("未配置 REDIS_URL。请先在 .env 中设置后再使用 Redis 功能。");
|
|
9
14
|
}
|
|
15
|
+
|
|
10
16
|
client ??= new Redis(url, {
|
|
11
17
|
lazyConnect: true,
|
|
12
18
|
maxRetriesPerRequest: 1,
|
|
13
19
|
enableOfflineQueue: false,
|
|
14
20
|
});
|
|
21
|
+
|
|
15
22
|
return client;
|
|
16
23
|
};
|
|
24
|
+
|
|
25
|
+
const probe = async () => {
|
|
26
|
+
const redis = getRedis();
|
|
27
|
+
if (redis.status === "wait") await redis.connect();
|
|
28
|
+
|
|
29
|
+
const startedAt = performance.now();
|
|
30
|
+
await redis.ping();
|
|
31
|
+
|
|
32
|
+
return { ok: true, latencyMs: Math.round(performance.now() - startedAt) };
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
/** 健康检查只返回状态,不向上抛错:连接或 ping 失败都折算成 `ok: false`。 */
|
|
17
36
|
export const checkRedis = async () => {
|
|
18
37
|
if (!process.env.REDIS_URL) {
|
|
19
38
|
return { ok: false, error: "未配置 REDIS_URL" };
|
|
20
39
|
}
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
if (redis.status === "wait") {
|
|
24
|
-
await redis.connect();
|
|
25
|
-
}
|
|
26
|
-
const startedAt = performance.now();
|
|
27
|
-
await redis.ping();
|
|
28
|
-
return { ok: true, latencyMs: Math.round(performance.now() - startedAt) };
|
|
29
|
-
} catch (error) {
|
|
30
|
-
return {
|
|
31
|
-
ok: false,
|
|
32
|
-
error: error instanceof Error ? error.message : String(error),
|
|
33
|
-
};
|
|
34
|
-
}
|
|
40
|
+
|
|
41
|
+
return probe().catch((error) => ({ ok: false, error: errorMessage(error) }));
|
|
35
42
|
};
|
|
43
|
+
|
|
36
44
|
export const closeRedis = async () => {
|
|
37
45
|
if (!client) return;
|
|
46
|
+
|
|
38
47
|
const current = client;
|
|
39
48
|
client = undefined;
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
} catch {
|
|
43
|
-
current.disconnect();
|
|
44
|
-
}
|
|
49
|
+
|
|
50
|
+
await current.quit().catch(() => current.disconnect());
|
|
45
51
|
};
|
|
@@ -1,32 +1,36 @@
|
|
|
1
|
+
import { errorMessage } from "../../utils/helpers.js";
|
|
2
|
+
|
|
1
3
|
const withTimeout = (promise, timeoutMs) => {
|
|
2
4
|
let timer;
|
|
5
|
+
|
|
3
6
|
const timeout = new Promise((_, reject) => {
|
|
4
7
|
timer = setTimeout(() => reject(new Error(`hook 执行超时(${timeoutMs}ms)`)), timeoutMs);
|
|
5
8
|
});
|
|
9
|
+
|
|
6
10
|
return Promise.race([promise, timeout]).finally(() => {
|
|
7
11
|
if (timer) clearTimeout(timer);
|
|
8
12
|
});
|
|
9
13
|
};
|
|
14
|
+
|
|
10
15
|
export const createShutdownManager = ({ logger, timeoutMs = 10_000 }) => {
|
|
11
16
|
const hooks = [];
|
|
12
17
|
let pending;
|
|
18
|
+
|
|
19
|
+
const closeHook = (hook) => {
|
|
20
|
+
return withTimeout(Promise.resolve(hook.close()), timeoutMs).then(
|
|
21
|
+
() => logger.info({ hook: hook.name }, "已关闭"),
|
|
22
|
+
(error) => logger.warn({ hook: hook.name, error: errorMessage(error) }, "关闭资源失败"),
|
|
23
|
+
);
|
|
24
|
+
};
|
|
25
|
+
|
|
13
26
|
const run = async (reason) => {
|
|
14
27
|
logger.info({ reason, hooks: hooks.map((hook) => hook.name) }, "开始关闭");
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
await withTimeout(Promise.resolve(hook.close()), timeoutMs);
|
|
19
|
-
logger.info({ hook: hook.name }, "已关闭");
|
|
20
|
-
} catch (error) {
|
|
21
|
-
logger.warn(
|
|
22
|
-
{ hook: hook.name, error: error instanceof Error ? error.message : error },
|
|
23
|
-
"关闭资源失败",
|
|
24
|
-
);
|
|
25
|
-
}
|
|
26
|
-
}),
|
|
27
|
-
);
|
|
28
|
+
|
|
29
|
+
await Promise.all(hooks.map(closeHook));
|
|
30
|
+
|
|
28
31
|
logger.info("关闭完成");
|
|
29
32
|
};
|
|
33
|
+
|
|
30
34
|
const manager = {
|
|
31
35
|
register: (hook) => {
|
|
32
36
|
hooks.push(hook);
|
|
@@ -35,10 +39,12 @@ export const createShutdownManager = ({ logger, timeoutMs = 10_000 }) => {
|
|
|
35
39
|
if (index >= 0) hooks.splice(index, 1);
|
|
36
40
|
};
|
|
37
41
|
},
|
|
42
|
+
|
|
38
43
|
shutdown: (reason = "shutdown") => {
|
|
39
44
|
pending ??= run(reason);
|
|
40
45
|
return pending;
|
|
41
46
|
},
|
|
47
|
+
|
|
42
48
|
install: () => {
|
|
43
49
|
for (const signal of ["SIGINT", "SIGTERM"]) {
|
|
44
50
|
process.once(signal, () => {
|
|
@@ -47,5 +53,6 @@ export const createShutdownManager = ({ logger, timeoutMs = 10_000 }) => {
|
|
|
47
53
|
}
|
|
48
54
|
},
|
|
49
55
|
};
|
|
56
|
+
|
|
50
57
|
return manager;
|
|
51
58
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export { assertReadOnlyQuery,
|
|
1
|
+
export { assertReadOnlyQuery, createQueryDatabaseTool } from "./query.js";
|
|
@@ -1,15 +1,19 @@
|
|
|
1
1
|
import { tool } from "@langchain/core/tools";
|
|
2
2
|
import { z } from "zod";
|
|
3
|
-
|
|
3
|
+
|
|
4
|
+
import { getPool } from "../../storage/database/client.js";
|
|
5
|
+
|
|
4
6
|
const READ_ONLY_START = /^\s*(?:select|with)\b/i;
|
|
5
7
|
const FORBIDDEN_KEYWORDS =
|
|
6
8
|
/\b(insert|update|delete|drop|alter|truncate|create|grant|revoke|copy|vacuum|reindex|call|do)\b/i;
|
|
9
|
+
|
|
7
10
|
/**
|
|
8
11
|
* 对模型生成的 SQL 做前置校验。查询同时运行在 READ ONLY 事务中,
|
|
9
12
|
* 即使绕过校验也无法修改数据。
|
|
10
13
|
*/
|
|
11
14
|
export const assertReadOnlyQuery = (sql) => {
|
|
12
15
|
const statement = sql.trim().replace(/;\s*$/, "");
|
|
16
|
+
|
|
13
17
|
if (!READ_ONLY_START.test(statement)) {
|
|
14
18
|
throw new Error("只允许 SELECT 和 WITH 查询。");
|
|
15
19
|
}
|
|
@@ -20,6 +24,7 @@ export const assertReadOnlyQuery = (sql) => {
|
|
|
20
24
|
throw new Error("查询包含写入或 DDL 关键字,已拒绝执行。");
|
|
21
25
|
}
|
|
22
26
|
};
|
|
27
|
+
|
|
23
28
|
const querySchema = z.object({
|
|
24
29
|
sql: z.string().min(1).describe("单条只读 SQL 语句(SELECT 或 WITH)。"),
|
|
25
30
|
params: z
|
|
@@ -28,35 +33,42 @@ const querySchema = z.object({
|
|
|
28
33
|
.describe("与 $1、$2…… 占位符对应的位置参数。"),
|
|
29
34
|
limit: z.number().int().min(1).max(500).default(100),
|
|
30
35
|
});
|
|
36
|
+
|
|
31
37
|
/** 只读、参数化的 PostgreSQL 查询工具。 */
|
|
32
|
-
export const
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
38
|
+
export const createQueryDatabaseTool = ({ connectionString } = {}) => {
|
|
39
|
+
return tool(
|
|
40
|
+
async ({ sql, params, limit }) => {
|
|
41
|
+
assertReadOnlyQuery(sql);
|
|
42
|
+
|
|
43
|
+
const client = await getPool(connectionString).connect();
|
|
44
|
+
|
|
45
|
+
try {
|
|
46
|
+
await client.query("BEGIN TRANSACTION READ ONLY");
|
|
47
|
+
await client.query("SET LOCAL statement_timeout = '10s'");
|
|
48
|
+
|
|
49
|
+
const result = await client.query(sql, params);
|
|
50
|
+
await client.query("COMMIT");
|
|
51
|
+
|
|
52
|
+
return JSON.stringify(
|
|
53
|
+
{
|
|
54
|
+
rowCount: result.rowCount,
|
|
55
|
+
truncated: result.rows.length > limit,
|
|
56
|
+
rows: result.rows.slice(0, limit),
|
|
57
|
+
},
|
|
58
|
+
null,
|
|
59
|
+
2,
|
|
60
|
+
);
|
|
61
|
+
} catch (error) {
|
|
62
|
+
await client.query("ROLLBACK").catch(() => undefined);
|
|
63
|
+
throw error;
|
|
64
|
+
} finally {
|
|
65
|
+
client.release();
|
|
66
|
+
}
|
|
67
|
+
},
|
|
68
|
+
{
|
|
69
|
+
name: "query_database",
|
|
70
|
+
description: "对应用 PostgreSQL 执行单条只读 SQL,并以 JSON 返回结果行。",
|
|
71
|
+
schema: querySchema,
|
|
72
|
+
},
|
|
73
|
+
);
|
|
74
|
+
};
|