create-deepagent-server 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +218 -2
- package/bin/create-deepagent-server.js +27 -0
- package/package.json +50 -4
- package/src/args.js +193 -0
- package/src/cli.js +269 -0
- package/src/dependencies.js +34 -0
- package/src/prompts.js +85 -0
- package/src/registry.js +68 -0
- package/src/scaffold.js +182 -0
- package/src/setup.js +77 -0
- package/src/ui.js +43 -0
- package/src/version.js +1 -0
- package/src/versions.js +29 -0
- package/templates/deepagent/js/deployment/Dockerfile +19 -0
- package/templates/deepagent/js/scripts/check.js +74 -0
- package/templates/deepagent/js/scripts/dev.js +31 -0
- package/templates/deepagent/js/src/agents/factory.js +10 -0
- package/templates/deepagent/js/src/agents/index.js +4 -0
- package/templates/deepagent/js/src/agents/prompts/index.js +2 -0
- package/templates/deepagent/js/src/agents/prompts/loader.js +44 -0
- package/templates/deepagent/js/src/agents/prompts/subagents/analyst.md +17 -0
- package/templates/deepagent/js/src/agents/prompts/subagents/coder.md +17 -0
- package/templates/deepagent/js/src/agents/prompts/subagents/researcher.md +16 -0
- package/templates/deepagent/js/src/agents/prompts/supervisor.js +17 -0
- package/templates/deepagent/js/src/agents/prompts/supervisor.md +42 -0
- package/templates/deepagent/js/src/agents/state/index.js +1 -0
- package/templates/deepagent/js/src/agents/state/types.js +10 -0
- package/templates/deepagent/js/src/agents/subagents/analyst.js +11 -0
- package/templates/deepagent/js/src/agents/subagents/coder.js +11 -0
- package/templates/deepagent/js/src/agents/subagents/index.js +8 -0
- package/templates/deepagent/js/src/agents/subagents/researcher.js +12 -0
- package/templates/deepagent/js/src/agents/supervisor.js +56 -0
- package/templates/deepagent/js/src/api/app.js +22 -0
- package/templates/deepagent/js/src/api/middleware/auth.js +27 -0
- package/templates/deepagent/js/src/api/middleware/error.js +26 -0
- package/templates/deepagent/js/src/api/routes/agent.js +73 -0
- package/templates/deepagent/js/src/api/routes/health.js +31 -0
- package/templates/deepagent/js/src/api/routes/session.js +62 -0
- package/templates/deepagent/js/src/app.js +79 -0
- package/templates/deepagent/js/src/backends/composite.js +20 -0
- package/templates/deepagent/js/src/backends/filesystem.js +8 -0
- package/templates/deepagent/js/src/backends/index.js +28 -0
- package/templates/deepagent/js/src/backends/sandbox.js +23 -0
- package/templates/deepagent/js/src/config/agent.js +13 -0
- package/templates/deepagent/js/src/config/env.js +73 -0
- package/templates/deepagent/js/src/config/index.js +35 -0
- package/templates/deepagent/js/src/config/model.js +34 -0
- package/templates/deepagent/js/src/graph/checkpointer.js +20 -0
- package/templates/deepagent/js/src/graph/compile.js +8 -0
- package/templates/deepagent/js/src/graph/index.js +3 -0
- package/templates/deepagent/js/src/graph/store.js +20 -0
- package/templates/deepagent/js/src/index.js +32 -0
- package/templates/deepagent/js/src/infrastructure/database/client.js +43 -0
- package/templates/deepagent/js/src/infrastructure/database/migrations/001_init.sql +20 -0
- package/templates/deepagent/js/src/infrastructure/logger/index.js +19 -0
- package/templates/deepagent/js/src/infrastructure/redis/client.js +45 -0
- package/templates/deepagent/js/src/infrastructure/shutdown/index.js +51 -0
- package/templates/deepagent/js/src/memory/index.js +6 -0
- package/templates/deepagent/js/src/memory/manager.js +39 -0
- package/templates/deepagent/js/src/memory/store.js +36 -0
- package/templates/deepagent/js/src/middleware/approval.js +42 -0
- package/templates/deepagent/js/src/middleware/context.js +30 -0
- package/templates/deepagent/js/src/middleware/index.js +9 -0
- package/templates/deepagent/js/src/middleware/logging.js +66 -0
- package/templates/deepagent/js/src/middleware/security.js +51 -0
- package/templates/deepagent/js/src/schemas/agent.js +33 -0
- package/templates/deepagent/js/src/schemas/common.js +19 -0
- package/templates/deepagent/js/src/schemas/locale.js +3 -0
- package/templates/deepagent/js/src/schemas/session.js +23 -0
- package/templates/deepagent/js/src/services/agent.service.js +117 -0
- package/templates/deepagent/js/src/services/memory.service.js +27 -0
- package/templates/deepagent/js/src/services/session.service.js +92 -0
- package/templates/deepagent/js/src/skills/index.js +17 -0
- package/templates/deepagent/js/src/skills/loader.js +65 -0
- package/templates/deepagent/js/src/tools/database/index.js +1 -0
- package/templates/deepagent/js/src/tools/database/query.js +62 -0
- package/templates/deepagent/js/src/tools/filesystem/index.js +86 -0
- package/templates/deepagent/js/src/tools/index.js +30 -0
- package/templates/deepagent/js/src/tools/mcp/client.js +35 -0
- package/templates/deepagent/js/src/tools/mcp/connections.js +54 -0
- package/templates/deepagent/js/src/tools/mcp/index.js +8 -0
- package/templates/deepagent/js/src/tools/mcp/registry.js +32 -0
- package/templates/deepagent/js/src/tools/search/index.js +1 -0
- package/templates/deepagent/js/src/tools/search/web-search.js +54 -0
- package/templates/deepagent/js/src/utils/errors.js +64 -0
- package/templates/deepagent/js/src/utils/helpers.js +42 -0
- package/templates/deepagent/js/tests/agents/prompts.test.js +24 -0
- package/templates/deepagent/js/tests/agents/supervisor.test.js +34 -0
- package/templates/deepagent/js/tests/integration/api.test.js +99 -0
- package/templates/deepagent/js/tests/middleware/security.test.js +24 -0
- package/templates/deepagent/js/tests/tools/query.test.js +23 -0
- package/templates/deepagent/shared/README.md +162 -0
- package/templates/deepagent/shared/_editorconfig +12 -0
- package/templates/deepagent/shared/_env.example +56 -0
- package/templates/deepagent/shared/_gitignore +22 -0
- package/templates/deepagent/shared/deployment/docker-compose.yml +56 -0
- package/templates/deepagent/shared/resources/AGENTS.md +32 -0
- package/templates/deepagent/shared/resources/knowledge/README.md +5 -0
- package/templates/deepagent/shared/skills/analysis/SKILL.md +28 -0
- package/templates/deepagent/shared/skills/coding/SKILL.md +28 -0
- package/templates/deepagent/shared/skills/research/SKILL.md +31 -0
- package/templates/deepagent/shared/workspaces/README.md +5 -0
- package/templates/deepagent/ts/deployment/Dockerfile +25 -0
- package/templates/deepagent/ts/scripts/check.ts +91 -0
- package/templates/deepagent/ts/scripts/copy-assets.ts +35 -0
- package/templates/deepagent/ts/scripts/dev.ts +40 -0
- package/templates/deepagent/ts/src/agents/factory.ts +11 -0
- package/templates/deepagent/ts/src/agents/index.ts +4 -0
- package/templates/deepagent/ts/src/agents/prompts/index.ts +2 -0
- package/templates/deepagent/ts/src/agents/prompts/loader.ts +52 -0
- package/templates/deepagent/ts/src/agents/prompts/subagents/analyst.md +17 -0
- package/templates/deepagent/ts/src/agents/prompts/subagents/coder.md +17 -0
- package/templates/deepagent/ts/src/agents/prompts/subagents/researcher.md +16 -0
- package/templates/deepagent/ts/src/agents/prompts/supervisor.md +42 -0
- package/templates/deepagent/ts/src/agents/prompts/supervisor.ts +33 -0
- package/templates/deepagent/ts/src/agents/state/index.ts +1 -0
- package/templates/deepagent/ts/src/agents/state/types.ts +13 -0
- package/templates/deepagent/ts/src/agents/subagents/analyst.ts +14 -0
- package/templates/deepagent/ts/src/agents/subagents/coder.ts +14 -0
- package/templates/deepagent/ts/src/agents/subagents/index.ts +10 -0
- package/templates/deepagent/ts/src/agents/subagents/researcher.ts +15 -0
- package/templates/deepagent/ts/src/agents/supervisor.ts +80 -0
- package/templates/deepagent/ts/src/api/app.ts +48 -0
- package/templates/deepagent/ts/src/api/middleware/auth.ts +37 -0
- package/templates/deepagent/ts/src/api/middleware/error.ts +33 -0
- package/templates/deepagent/ts/src/api/routes/agent.ts +93 -0
- package/templates/deepagent/ts/src/api/routes/health.ts +45 -0
- package/templates/deepagent/ts/src/api/routes/session.ts +77 -0
- package/templates/deepagent/ts/src/app.ts +104 -0
- package/templates/deepagent/ts/src/backends/composite.ts +34 -0
- package/templates/deepagent/ts/src/backends/filesystem.ts +19 -0
- package/templates/deepagent/ts/src/backends/index.ts +42 -0
- package/templates/deepagent/ts/src/backends/sandbox.ts +32 -0
- package/templates/deepagent/ts/src/config/agent.ts +37 -0
- package/templates/deepagent/ts/src/config/env.ts +102 -0
- package/templates/deepagent/ts/src/config/index.ts +76 -0
- package/templates/deepagent/ts/src/config/model.ts +49 -0
- package/templates/deepagent/ts/src/graph/checkpointer.ts +26 -0
- package/templates/deepagent/ts/src/graph/compile.ts +15 -0
- package/templates/deepagent/ts/src/graph/index.ts +3 -0
- package/templates/deepagent/ts/src/graph/store.ts +26 -0
- package/templates/deepagent/ts/src/index.ts +39 -0
- package/templates/deepagent/ts/src/infrastructure/database/client.ts +61 -0
- package/templates/deepagent/ts/src/infrastructure/database/migrations/001_init.sql +20 -0
- package/templates/deepagent/ts/src/infrastructure/logger/index.ts +25 -0
- package/templates/deepagent/ts/src/infrastructure/redis/client.ts +64 -0
- package/templates/deepagent/ts/src/infrastructure/shutdown/index.ts +85 -0
- package/templates/deepagent/ts/src/memory/index.ts +11 -0
- package/templates/deepagent/ts/src/memory/manager.ts +74 -0
- package/templates/deepagent/ts/src/memory/store.ts +57 -0
- package/templates/deepagent/ts/src/middleware/approval.ts +49 -0
- package/templates/deepagent/ts/src/middleware/context.ts +37 -0
- package/templates/deepagent/ts/src/middleware/index.ts +9 -0
- package/templates/deepagent/ts/src/middleware/logging.ts +72 -0
- package/templates/deepagent/ts/src/middleware/security.ts +60 -0
- package/templates/deepagent/ts/src/schemas/agent.ts +48 -0
- package/templates/deepagent/ts/src/schemas/common.ts +26 -0
- package/templates/deepagent/ts/src/schemas/locale.ts +4 -0
- package/templates/deepagent/ts/src/schemas/session.ts +31 -0
- package/templates/deepagent/ts/src/services/agent.service.ts +170 -0
- package/templates/deepagent/ts/src/services/memory.service.ts +43 -0
- package/templates/deepagent/ts/src/services/session.service.ts +148 -0
- package/templates/deepagent/ts/src/skills/index.ts +26 -0
- package/templates/deepagent/ts/src/skills/loader.ts +94 -0
- package/templates/deepagent/ts/src/tools/database/index.ts +1 -0
- package/templates/deepagent/ts/src/tools/database/query.ts +71 -0
- package/templates/deepagent/ts/src/tools/filesystem/index.ts +108 -0
- package/templates/deepagent/ts/src/tools/index.ts +41 -0
- package/templates/deepagent/ts/src/tools/mcp/client.ts +53 -0
- package/templates/deepagent/ts/src/tools/mcp/connections.ts +68 -0
- package/templates/deepagent/ts/src/tools/mcp/index.ts +10 -0
- package/templates/deepagent/ts/src/tools/mcp/registry.ts +48 -0
- package/templates/deepagent/ts/src/tools/search/index.ts +1 -0
- package/templates/deepagent/ts/src/tools/search/web-search.ts +82 -0
- package/templates/deepagent/ts/src/utils/errors.ts +95 -0
- package/templates/deepagent/ts/src/utils/helpers.ts +52 -0
- package/templates/deepagent/ts/tests/agents/prompts.test.ts +30 -0
- package/templates/deepagent/ts/tests/agents/supervisor.test.ts +42 -0
- package/templates/deepagent/ts/tests/integration/api.test.ts +123 -0
- package/templates/deepagent/ts/tests/middleware/security.test.ts +27 -0
- package/templates/deepagent/ts/tests/tools/query.test.ts +27 -0
- package/templates/deepagent/ts/tsconfig.json +19 -0
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { Router } from "express";
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
invokeAgentSchema,
|
|
5
|
+
resumeAgentSchema,
|
|
6
|
+
type InvokeAgentInput,
|
|
7
|
+
} from "../../schemas/agent.js";
|
|
8
|
+
import type { AgentService } from "../../services/agent.service.js";
|
|
9
|
+
import { ValidationError } from "../../utils/errors.js";
|
|
10
|
+
|
|
11
|
+
export interface AgentRouterDependencies {
|
|
12
|
+
agent: AgentService;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export const createAgentRouter = ({ agent }: AgentRouterDependencies): Router => {
|
|
16
|
+
const router = Router();
|
|
17
|
+
|
|
18
|
+
router.post("/invoke", async (req, res, next) => {
|
|
19
|
+
try {
|
|
20
|
+
const parsed = invokeAgentSchema.safeParse(req.body);
|
|
21
|
+
if (!parsed.success) {
|
|
22
|
+
throw new ValidationError("请求体格式错误。", {
|
|
23
|
+
issues: parsed.error.issues,
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const result = await agent.invoke(parsed.data);
|
|
28
|
+
res.json(result);
|
|
29
|
+
} catch (error) {
|
|
30
|
+
next(error);
|
|
31
|
+
}
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
router.post("/resume", async (req, res, next) => {
|
|
35
|
+
try {
|
|
36
|
+
const parsed = resumeAgentSchema.safeParse(req.body);
|
|
37
|
+
if (!parsed.success) {
|
|
38
|
+
throw new ValidationError("请求体格式错误。", {
|
|
39
|
+
issues: parsed.error.issues,
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const result = await agent.resume(parsed.data);
|
|
44
|
+
res.json(result);
|
|
45
|
+
} catch (error) {
|
|
46
|
+
next(error);
|
|
47
|
+
}
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
router.post("/stream", async (req, res, next) => {
|
|
51
|
+
const parsed = invokeAgentSchema.safeParse(req.body);
|
|
52
|
+
if (!parsed.success) {
|
|
53
|
+
next(
|
|
54
|
+
new ValidationError("请求体格式错误。", {
|
|
55
|
+
issues: parsed.error.issues,
|
|
56
|
+
}),
|
|
57
|
+
);
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const controller = new AbortController();
|
|
62
|
+
req.on("close", () => controller.abort());
|
|
63
|
+
|
|
64
|
+
res.status(200);
|
|
65
|
+
res.setHeader("Content-Type", "text/event-stream");
|
|
66
|
+
res.setHeader("Cache-Control", "no-cache, no-transform");
|
|
67
|
+
res.setHeader("Connection", "keep-alive");
|
|
68
|
+
res.flushHeaders();
|
|
69
|
+
|
|
70
|
+
try {
|
|
71
|
+
for await (const event of agent.stream(parsed.data as InvokeAgentInput, {
|
|
72
|
+
signal: controller.signal,
|
|
73
|
+
})) {
|
|
74
|
+
res.write(`event: ${event.event}\ndata: ${JSON.stringify(event.data)}\n\n`);
|
|
75
|
+
}
|
|
76
|
+
res.write("event: done\ndata: {}\n\n");
|
|
77
|
+
} catch (error) {
|
|
78
|
+
if (!res.headersSent) {
|
|
79
|
+
next(error);
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
res.write(
|
|
83
|
+
`event: error\ndata: ${JSON.stringify({
|
|
84
|
+
message: error instanceof Error ? error.message : String(error),
|
|
85
|
+
})}\n\n`,
|
|
86
|
+
);
|
|
87
|
+
} finally {
|
|
88
|
+
res.end();
|
|
89
|
+
}
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
return router;
|
|
93
|
+
};
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { Router } from "express";
|
|
2
|
+
|
|
3
|
+
import type { AppConfig } from "../../config/index.js";
|
|
4
|
+
import type { McpToolRegistry } from "../../tools/mcp/index.js";
|
|
5
|
+
import { checkDatabase } from "../../infrastructure/database/client.js";
|
|
6
|
+
import { checkRedis } from "../../infrastructure/redis/client.js";
|
|
7
|
+
|
|
8
|
+
export interface HealthRouterDependencies {
|
|
9
|
+
config: AppConfig;
|
|
10
|
+
mcp?: McpToolRegistry;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export const createHealthRouter = ({ config, mcp }: HealthRouterDependencies): Router => {
|
|
14
|
+
const router = Router();
|
|
15
|
+
|
|
16
|
+
router.get("/", (_req, res) => {
|
|
17
|
+
res.json({
|
|
18
|
+
status: "ok",
|
|
19
|
+
service: "deepagent",
|
|
20
|
+
agent: config.agent.name,
|
|
21
|
+
model: `${config.model.provider}/${config.model.name}`,
|
|
22
|
+
uptimeSeconds: Math.round(process.uptime()),
|
|
23
|
+
});
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
router.get("/ready", async (_req, res) => {
|
|
27
|
+
const [database, redis] = await Promise.all([
|
|
28
|
+
config.database.url ? checkDatabase() : Promise.resolve({ ok: true, skipped: true }),
|
|
29
|
+
config.redis.url ? checkRedis() : Promise.resolve({ ok: true, skipped: true }),
|
|
30
|
+
]);
|
|
31
|
+
|
|
32
|
+
const ready = (!config.database.url || database.ok) && (!config.redis.url || redis.ok);
|
|
33
|
+
|
|
34
|
+
res.status(ready ? 200 : 503).json({
|
|
35
|
+
status: ready ? "ready" : "degraded",
|
|
36
|
+
checks: {
|
|
37
|
+
database,
|
|
38
|
+
redis,
|
|
39
|
+
mcp: { servers: mcp?.serverNames ?? [] },
|
|
40
|
+
},
|
|
41
|
+
});
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
return router;
|
|
45
|
+
};
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { Router } from "express";
|
|
2
|
+
|
|
3
|
+
import { createSessionSchema, type CreateSessionInput } from "../../schemas/session.js";
|
|
4
|
+
import type { SessionService } from "../../services/session.service.js";
|
|
5
|
+
import { ValidationError } from "../../utils/errors.js";
|
|
6
|
+
|
|
7
|
+
export interface SessionRouterDependencies {
|
|
8
|
+
sessions: SessionService;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
const tenantFrom = (value: unknown): string => {
|
|
12
|
+
return typeof value === "string" && value.length > 0 ? value : "default";
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
export const createSessionRouter = ({ sessions }: SessionRouterDependencies): Router => {
|
|
16
|
+
const router = Router();
|
|
17
|
+
|
|
18
|
+
router.post("/", async (req, res, next) => {
|
|
19
|
+
try {
|
|
20
|
+
const parsed = createSessionSchema.safeParse(req.body ?? {});
|
|
21
|
+
if (!parsed.success) {
|
|
22
|
+
throw new ValidationError("请求体格式错误。", {
|
|
23
|
+
issues: parsed.error.issues,
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const input: CreateSessionInput = parsed.data;
|
|
28
|
+
const session = await sessions.create(input);
|
|
29
|
+
res.status(201).json(session);
|
|
30
|
+
} catch (error) {
|
|
31
|
+
next(error);
|
|
32
|
+
}
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
router.get("/", async (req, res, next) => {
|
|
36
|
+
try {
|
|
37
|
+
const tenantId = tenantFrom(req.query.tenantId);
|
|
38
|
+
const limit = Number(req.query.limit ?? 20);
|
|
39
|
+
const result = await sessions.list(
|
|
40
|
+
tenantId,
|
|
41
|
+
Number.isFinite(limit) ? Math.min(Math.max(limit, 1), 100) : 20,
|
|
42
|
+
);
|
|
43
|
+
res.json(result);
|
|
44
|
+
} catch (error) {
|
|
45
|
+
next(error);
|
|
46
|
+
}
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
router.get("/:threadId", async (req, res, next) => {
|
|
50
|
+
try {
|
|
51
|
+
const session = await sessions.get(req.params.threadId, tenantFrom(req.query.tenantId));
|
|
52
|
+
res.json(session);
|
|
53
|
+
} catch (error) {
|
|
54
|
+
next(error);
|
|
55
|
+
}
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
router.get("/:threadId/messages", async (req, res, next) => {
|
|
59
|
+
try {
|
|
60
|
+
const messages = await sessions.history(req.params.threadId);
|
|
61
|
+
res.json({ messages });
|
|
62
|
+
} catch (error) {
|
|
63
|
+
next(error);
|
|
64
|
+
}
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
router.delete("/:threadId", async (req, res, next) => {
|
|
68
|
+
try {
|
|
69
|
+
await sessions.remove(req.params.threadId, tenantFrom(req.query.tenantId));
|
|
70
|
+
res.status(204).end();
|
|
71
|
+
} catch (error) {
|
|
72
|
+
next(error);
|
|
73
|
+
}
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
return router;
|
|
77
|
+
};
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import fs from "node:fs/promises";
|
|
2
|
+
|
|
3
|
+
import type { Express } from "express";
|
|
4
|
+
|
|
5
|
+
import { createSupervisorAgent } from "./agents/supervisor.js";
|
|
6
|
+
import { createApiApp } from "./api/app.js";
|
|
7
|
+
import { createBackend } from "./backends/index.js";
|
|
8
|
+
import { createChatModel, loadConfig, type AppConfig } from "./config/index.js";
|
|
9
|
+
import { createCheckpointer } from "./graph/checkpointer.js";
|
|
10
|
+
import { createStore } from "./graph/store.js";
|
|
11
|
+
import { closeDatabase } from "./infrastructure/database/client.js";
|
|
12
|
+
import { createLogger, type Logger } from "./infrastructure/logger/index.js";
|
|
13
|
+
import { closeRedis } from "./infrastructure/redis/client.js";
|
|
14
|
+
import { createMemoryManager } from "./memory/index.js";
|
|
15
|
+
import { AgentService } from "./services/agent.service.js";
|
|
16
|
+
import { MemoryService } from "./services/memory.service.js";
|
|
17
|
+
import { SessionService } from "./services/session.service.js";
|
|
18
|
+
import { loadSkills, validateSkills } from "./skills/index.js";
|
|
19
|
+
import { createDefaultTools } from "./tools/index.js";
|
|
20
|
+
import { createMcpToolRegistry } from "./tools/mcp/index.js";
|
|
21
|
+
|
|
22
|
+
export interface Application {
|
|
23
|
+
app: Express;
|
|
24
|
+
logger: Logger;
|
|
25
|
+
services: {
|
|
26
|
+
agent: AgentService;
|
|
27
|
+
sessions: SessionService;
|
|
28
|
+
memory: MemoryService;
|
|
29
|
+
};
|
|
30
|
+
close: () => Promise<void>;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* 应用装配:配置 → 基础设施 → Agent 运行时 → 服务 → HTTP 应用。
|
|
35
|
+
* 这里不启动服务,因此测试、脚本和 HTTP 入口可以复用同一套装配。
|
|
36
|
+
*/
|
|
37
|
+
export const createApplication = async (config: AppConfig = loadConfig()): Promise<Application> => {
|
|
38
|
+
const logger = createLogger(config);
|
|
39
|
+
|
|
40
|
+
await fs.mkdir(config.paths.workspaces, { recursive: true });
|
|
41
|
+
await fs.mkdir(config.paths.resources, { recursive: true });
|
|
42
|
+
|
|
43
|
+
const skills = await loadSkills({ rootDir: config.paths.skills });
|
|
44
|
+
for (const warning of validateSkills(skills)) {
|
|
45
|
+
logger.warn(warning);
|
|
46
|
+
}
|
|
47
|
+
logger.info({ skills: skills.map((skill) => skill.name) }, "技能加载完成");
|
|
48
|
+
|
|
49
|
+
const store = await createStore(config);
|
|
50
|
+
const checkpointer = await createCheckpointer(config);
|
|
51
|
+
const backend = await createBackend(config);
|
|
52
|
+
const mcp = await createMcpToolRegistry();
|
|
53
|
+
|
|
54
|
+
const tools = createDefaultTools({
|
|
55
|
+
workspaceRoot: config.paths.workspaces,
|
|
56
|
+
additionalTools: mcp.tools,
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
const agent = createSupervisorAgent({
|
|
60
|
+
config,
|
|
61
|
+
model: createChatModel(config.model),
|
|
62
|
+
backend: backend.backend,
|
|
63
|
+
checkpointer,
|
|
64
|
+
store,
|
|
65
|
+
logger,
|
|
66
|
+
tools,
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
const sessions = new SessionService({ graph: agent, checkpointer, store });
|
|
70
|
+
const agentService = new AgentService({ agent, config, sessions });
|
|
71
|
+
const memoryService = new MemoryService(createMemoryManager(store));
|
|
72
|
+
|
|
73
|
+
const app = createApiApp({
|
|
74
|
+
config,
|
|
75
|
+
logger,
|
|
76
|
+
mcp,
|
|
77
|
+
services: {
|
|
78
|
+
agent: agentService,
|
|
79
|
+
sessions,
|
|
80
|
+
memory: memoryService,
|
|
81
|
+
},
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
let closed = false;
|
|
85
|
+
|
|
86
|
+
return {
|
|
87
|
+
app,
|
|
88
|
+
logger,
|
|
89
|
+
services: {
|
|
90
|
+
agent: agentService,
|
|
91
|
+
sessions,
|
|
92
|
+
memory: memoryService,
|
|
93
|
+
},
|
|
94
|
+
close: async () => {
|
|
95
|
+
if (closed) return;
|
|
96
|
+
closed = true;
|
|
97
|
+
|
|
98
|
+
await mcp.close();
|
|
99
|
+
await backend.dispose();
|
|
100
|
+
await closeDatabase();
|
|
101
|
+
await closeRedis();
|
|
102
|
+
},
|
|
103
|
+
};
|
|
104
|
+
};
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { CompositeBackend, StateBackend, StoreBackend, type BackendProtocolV2 } from "deepagents";
|
|
2
|
+
|
|
3
|
+
import { createFilesystemBackend } from "./filesystem.js";
|
|
4
|
+
|
|
5
|
+
export interface CompositeBackendOptions {
|
|
6
|
+
defaultBackend?: BackendProtocolV2;
|
|
7
|
+
paths: {
|
|
8
|
+
workspaces: string;
|
|
9
|
+
resources: string;
|
|
10
|
+
skills: string;
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* 把 Agent 需要的所有资源挂载到同一个虚拟文件系统:
|
|
16
|
+
*
|
|
17
|
+
* /workspace/ 草稿与产物(落磁盘)
|
|
18
|
+
* /resources/ AGENTS.md 与只读知识库(落磁盘)
|
|
19
|
+
* /skills/ SKILL.md 目录(落磁盘)
|
|
20
|
+
* /memories/ 长期记忆(LangGraph BaseStore)
|
|
21
|
+
*
|
|
22
|
+
* 其余路径落到默认后端(默认是临时状态)。
|
|
23
|
+
*/
|
|
24
|
+
export const createCompositeBackend = ({
|
|
25
|
+
defaultBackend = new StateBackend(),
|
|
26
|
+
paths,
|
|
27
|
+
}: CompositeBackendOptions): CompositeBackend => {
|
|
28
|
+
return new CompositeBackend(defaultBackend, {
|
|
29
|
+
"/workspace/": createFilesystemBackend({ rootDir: paths.workspaces }),
|
|
30
|
+
"/resources/": createFilesystemBackend({ rootDir: paths.resources }),
|
|
31
|
+
"/skills/": createFilesystemBackend({ rootDir: paths.skills }),
|
|
32
|
+
"/memories/": new StoreBackend(),
|
|
33
|
+
});
|
|
34
|
+
};
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { FilesystemBackend } from "deepagents";
|
|
2
|
+
|
|
3
|
+
export interface WorkspaceBackendOptions {
|
|
4
|
+
rootDir: string;
|
|
5
|
+
virtualMode?: boolean;
|
|
6
|
+
maxFileSizeMb?: number;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* 虚拟路径被限制在 `rootDir` 内的文件系统后端。
|
|
11
|
+
* Agent 看到的是 `/notes.md`,实际文件位于 `<rootDir>/notes.md`。
|
|
12
|
+
*/
|
|
13
|
+
export const createFilesystemBackend = ({
|
|
14
|
+
rootDir,
|
|
15
|
+
virtualMode = true,
|
|
16
|
+
maxFileSizeMb = 10,
|
|
17
|
+
}: WorkspaceBackendOptions): FilesystemBackend => {
|
|
18
|
+
return new FilesystemBackend({ rootDir, virtualMode, maxFileSizeMb });
|
|
19
|
+
};
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import type { CompositeBackend } from "deepagents";
|
|
2
|
+
|
|
3
|
+
import type { AppConfig } from "../config/index.js";
|
|
4
|
+
import { createCompositeBackend } from "./composite.js";
|
|
5
|
+
import { createSandboxBackend } from "./sandbox.js";
|
|
6
|
+
|
|
7
|
+
export interface BackendBundle {
|
|
8
|
+
backend: CompositeBackend;
|
|
9
|
+
/** 仅在 ENABLE_SHELL=true 时存在。 */
|
|
10
|
+
shell: Awaited<ReturnType<typeof createSandboxBackend>> | null;
|
|
11
|
+
dispose: () => Promise<void>;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* 构建主 Agent 和所有子 Agent 共用的组合后端。
|
|
16
|
+
* Shell 访问会执行宿主机命令,因此默认关闭。
|
|
17
|
+
*/
|
|
18
|
+
export const createBackend = async (config: AppConfig): Promise<BackendBundle> => {
|
|
19
|
+
const rootDir = config.paths.workspaces;
|
|
20
|
+
const shell = config.agent.enableShell ? await createSandboxBackend({ rootDir }) : null;
|
|
21
|
+
|
|
22
|
+
const backend = createCompositeBackend({
|
|
23
|
+
...(shell ? { defaultBackend: shell } : {}),
|
|
24
|
+
paths: {
|
|
25
|
+
workspaces: config.paths.workspaces,
|
|
26
|
+
resources: config.paths.resources,
|
|
27
|
+
skills: config.paths.skills,
|
|
28
|
+
},
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
return {
|
|
32
|
+
backend,
|
|
33
|
+
shell,
|
|
34
|
+
dispose: async () => {
|
|
35
|
+
await shell?.close();
|
|
36
|
+
},
|
|
37
|
+
};
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
export { createCompositeBackend } from "./composite.js";
|
|
41
|
+
export { createFilesystemBackend } from "./filesystem.js";
|
|
42
|
+
export { createSandboxBackend } from "./sandbox.js";
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { LocalShellBackend } from "deepagents";
|
|
2
|
+
|
|
3
|
+
export interface SandboxBackendOptions {
|
|
4
|
+
rootDir: string;
|
|
5
|
+
virtualMode?: boolean;
|
|
6
|
+
timeoutSeconds?: number;
|
|
7
|
+
maxOutputBytes?: number;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* 本地 shell 后端。在文件系统工具之上增加 `execute` 工具,
|
|
12
|
+
* 命令会以当前进程的权限在宿主机执行。
|
|
13
|
+
*
|
|
14
|
+
* 生产环境请保持 ENABLE_SHELL=false,或替换为真正的沙箱
|
|
15
|
+
* (LangSmithSandbox、容器、远程执行器等)。
|
|
16
|
+
*/
|
|
17
|
+
export const createSandboxBackend = async ({
|
|
18
|
+
rootDir,
|
|
19
|
+
virtualMode = true,
|
|
20
|
+
timeoutSeconds = 60,
|
|
21
|
+
maxOutputBytes = 100_000,
|
|
22
|
+
}: SandboxBackendOptions): Promise<LocalShellBackend> => {
|
|
23
|
+
const backend = new LocalShellBackend({
|
|
24
|
+
rootDir,
|
|
25
|
+
virtualMode,
|
|
26
|
+
timeout: timeoutSeconds,
|
|
27
|
+
maxOutputBytes,
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
await backend.initialize();
|
|
31
|
+
return backend;
|
|
32
|
+
};
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import type { Env } from "./env.js";
|
|
2
|
+
|
|
3
|
+
export type ApprovalMode = Env["APPROVAL_MODE"];
|
|
4
|
+
|
|
5
|
+
export interface AgentConfig {
|
|
6
|
+
/** Agent 名称,出现在提示词和链路追踪中。 */
|
|
7
|
+
name: string;
|
|
8
|
+
/** Agent 职责的可读描述。 */
|
|
9
|
+
description: string;
|
|
10
|
+
/** 每次运行允许的最大 LangGraph 超级步数。 */
|
|
11
|
+
recursionLimit: number;
|
|
12
|
+
/** 哪些工具调用需要通过 `interruptOn` 等待人工审批。 */
|
|
13
|
+
approvals: ApprovalMode;
|
|
14
|
+
/** 挂载可执行 shell 的后端并暴露 `execute` 工具。 */
|
|
15
|
+
enableShell: boolean;
|
|
16
|
+
/** 加载进系统提示词的 AGENTS.md 文件。 */
|
|
17
|
+
memoryFiles: string[];
|
|
18
|
+
/** 扫描 `SKILL.md` 的后端目录。 */
|
|
19
|
+
skillDirectories: string[];
|
|
20
|
+
/** 追加到主 Agent 提示词末尾的额外说明。 */
|
|
21
|
+
systemPromptAppendix?: string;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export const DEFAULT_AGENT_DESCRIPTION =
|
|
25
|
+
"负责规划任务、把工作分派给专职子 Agent,并维护工作区文件的主 Agent。";
|
|
26
|
+
|
|
27
|
+
export const resolveAgentConfig = (env: Env): AgentConfig => {
|
|
28
|
+
return {
|
|
29
|
+
name: env.AGENT_NAME,
|
|
30
|
+
description: DEFAULT_AGENT_DESCRIPTION,
|
|
31
|
+
recursionLimit: env.AGENT_RECURSION_LIMIT,
|
|
32
|
+
approvals: env.APPROVAL_MODE,
|
|
33
|
+
enableShell: env.ENABLE_SHELL,
|
|
34
|
+
memoryFiles: ["/resources/AGENTS.md"],
|
|
35
|
+
skillDirectories: ["/skills/"],
|
|
36
|
+
};
|
|
37
|
+
};
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import "../schemas/locale.js";
|
|
2
|
+
|
|
3
|
+
import { z } from "zod";
|
|
4
|
+
|
|
5
|
+
import { ConfigurationError } from "../utils/errors.js";
|
|
6
|
+
|
|
7
|
+
const booleanFlag = z
|
|
8
|
+
.union([z.boolean(), z.string()])
|
|
9
|
+
.default("false")
|
|
10
|
+
.transform((value) =>
|
|
11
|
+
typeof value === "boolean" ? value : ["1", "true", "yes", "on"].includes(value.toLowerCase()),
|
|
12
|
+
);
|
|
13
|
+
|
|
14
|
+
const optionalString = z
|
|
15
|
+
.string()
|
|
16
|
+
.trim()
|
|
17
|
+
.optional()
|
|
18
|
+
.transform((value) => (value === "" ? undefined : value));
|
|
19
|
+
|
|
20
|
+
export const envSchema = z.object({
|
|
21
|
+
NODE_ENV: z.enum(["development", "test", "production"]).default("development"),
|
|
22
|
+
HOST: z.string().default("0.0.0.0"),
|
|
23
|
+
PORT: z.coerce.number().int().min(1).max(65535).default(3000),
|
|
24
|
+
LOG_LEVEL: z.enum(["fatal", "error", "warn", "info", "debug", "trace", "silent"]).default("info"),
|
|
25
|
+
|
|
26
|
+
MODEL_PROVIDER: z.enum(["openai", "anthropic"]).default("openai"),
|
|
27
|
+
MODEL_NAME: optionalString,
|
|
28
|
+
MODEL_TEMPERATURE: z.coerce.number().min(0).max(2).default(0.2),
|
|
29
|
+
MODEL_MAX_TOKENS: z.coerce.number().int().positive().default(4096),
|
|
30
|
+
|
|
31
|
+
OPENAI_API_KEY: optionalString,
|
|
32
|
+
OPENAI_BASE_URL: optionalString,
|
|
33
|
+
ANTHROPIC_API_KEY: optionalString,
|
|
34
|
+
TAVILY_API_KEY: optionalString,
|
|
35
|
+
|
|
36
|
+
AGENT_NAME: z.string().default("deepagent"),
|
|
37
|
+
AGENT_API_KEY: optionalString,
|
|
38
|
+
AGENT_RECURSION_LIMIT: z.coerce.number().int().min(1).max(1000).default(50),
|
|
39
|
+
APPROVAL_MODE: z.enum(["none", "sensitive", "all"]).default("sensitive"),
|
|
40
|
+
ENABLE_SHELL: booleanFlag,
|
|
41
|
+
WORKSPACE_ROOT: z.string().default("workspaces"),
|
|
42
|
+
RESOURCES_ROOT: z.string().default("resources"),
|
|
43
|
+
SKILLS_ROOT: z.string().default("skills"),
|
|
44
|
+
|
|
45
|
+
CHECKPOINTER: z.enum(["memory", "postgres"]).default("memory"),
|
|
46
|
+
STORE: z.enum(["memory", "postgres"]).default("memory"),
|
|
47
|
+
DATABASE_URL: optionalString,
|
|
48
|
+
REDIS_URL: optionalString,
|
|
49
|
+
|
|
50
|
+
LANGSMITH_TRACING: booleanFlag,
|
|
51
|
+
LANGSMITH_API_KEY: optionalString,
|
|
52
|
+
LANGSMITH_PROJECT: z.string().default("deepagent"),
|
|
53
|
+
|
|
54
|
+
MCP_SERVERS: optionalString,
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
export type Env = z.infer<typeof envSchema>;
|
|
58
|
+
|
|
59
|
+
export interface LoadEnvOptions {
|
|
60
|
+
/** 缺少所选模型提供商的 API Key 时直接报错。 */
|
|
61
|
+
requireModelCredentials?: boolean;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export const loadEnv = (
|
|
65
|
+
source: NodeJS.ProcessEnv = process.env,
|
|
66
|
+
{ requireModelCredentials = true }: LoadEnvOptions = {},
|
|
67
|
+
): Env => {
|
|
68
|
+
const parsed = envSchema.safeParse(source);
|
|
69
|
+
|
|
70
|
+
if (!parsed.success) {
|
|
71
|
+
const issues = parsed.error.issues
|
|
72
|
+
.map((issue) => `${issue.path.join(".") || "env"}: ${issue.message}`)
|
|
73
|
+
.join("; ");
|
|
74
|
+
throw new ConfigurationError(`环境变量配置无效:${issues}`);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
const env = parsed.data;
|
|
78
|
+
|
|
79
|
+
if (requireModelCredentials && env.NODE_ENV !== "test") {
|
|
80
|
+
if (env.MODEL_PROVIDER === "openai" && !env.OPENAI_API_KEY) {
|
|
81
|
+
throw new ConfigurationError(
|
|
82
|
+
"MODEL_PROVIDER=openai 时必须设置 OPENAI_API_KEY(可复制 .env.example 为 .env)。",
|
|
83
|
+
);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
if (env.MODEL_PROVIDER === "anthropic" && !env.ANTHROPIC_API_KEY) {
|
|
87
|
+
throw new ConfigurationError(
|
|
88
|
+
"MODEL_PROVIDER=anthropic 时必须设置 ANTHROPIC_API_KEY(可复制 .env.example 为 .env)。",
|
|
89
|
+
);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
if (env.CHECKPOINTER === "postgres" && !env.DATABASE_URL) {
|
|
94
|
+
throw new ConfigurationError("CHECKPOINTER=postgres 时必须设置 DATABASE_URL。");
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
if (env.STORE === "postgres" && !env.DATABASE_URL) {
|
|
98
|
+
throw new ConfigurationError("STORE=postgres 时必须设置 DATABASE_URL。");
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
return env;
|
|
102
|
+
};
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import path from "node:path";
|
|
2
|
+
|
|
3
|
+
import { resolveAgentConfig, type AgentConfig } from "./agent.js";
|
|
4
|
+
import { loadEnv, type Env } from "./env.js";
|
|
5
|
+
import { resolveModelConfig, type ModelConfig } from "./model.js";
|
|
6
|
+
|
|
7
|
+
export type { AgentConfig, ApprovalMode } from "./agent.js";
|
|
8
|
+
export type { Env } from "./env.js";
|
|
9
|
+
export type { ModelConfig } from "./model.js";
|
|
10
|
+
export { loadEnv } from "./env.js";
|
|
11
|
+
export { createChatModel, DEFAULT_MODELS } from "./model.js";
|
|
12
|
+
|
|
13
|
+
export interface AppPaths {
|
|
14
|
+
root: string;
|
|
15
|
+
workspaces: string;
|
|
16
|
+
resources: string;
|
|
17
|
+
skills: string;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export interface AppConfig {
|
|
21
|
+
env: Env;
|
|
22
|
+
server: {
|
|
23
|
+
host: string;
|
|
24
|
+
port: number;
|
|
25
|
+
};
|
|
26
|
+
paths: AppPaths;
|
|
27
|
+
model: ModelConfig;
|
|
28
|
+
agent: AgentConfig;
|
|
29
|
+
database: {
|
|
30
|
+
url?: string;
|
|
31
|
+
};
|
|
32
|
+
redis: {
|
|
33
|
+
url?: string;
|
|
34
|
+
};
|
|
35
|
+
logging: {
|
|
36
|
+
level: Env["LOG_LEVEL"];
|
|
37
|
+
pretty: boolean;
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export interface LoadConfigOptions {
|
|
42
|
+
cwd?: string;
|
|
43
|
+
env?: Env;
|
|
44
|
+
requireModelCredentials?: boolean;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* 把环境变量转换为带类型、使用绝对路径的应用配置,全局只在这里处理。
|
|
49
|
+
*/
|
|
50
|
+
export const loadConfig = (options: LoadConfigOptions = {}): AppConfig => {
|
|
51
|
+
const root = path.resolve(options.cwd ?? process.cwd());
|
|
52
|
+
const env =
|
|
53
|
+
options.env ??
|
|
54
|
+
loadEnv(process.env, {
|
|
55
|
+
requireModelCredentials: options.requireModelCredentials ?? true,
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
return {
|
|
59
|
+
env,
|
|
60
|
+
server: { host: env.HOST, port: env.PORT },
|
|
61
|
+
paths: {
|
|
62
|
+
root,
|
|
63
|
+
workspaces: path.resolve(root, env.WORKSPACE_ROOT),
|
|
64
|
+
resources: path.resolve(root, env.RESOURCES_ROOT),
|
|
65
|
+
skills: path.resolve(root, env.SKILLS_ROOT),
|
|
66
|
+
},
|
|
67
|
+
model: resolveModelConfig(env),
|
|
68
|
+
agent: resolveAgentConfig(env),
|
|
69
|
+
database: { ...(env.DATABASE_URL ? { url: env.DATABASE_URL } : {}) },
|
|
70
|
+
redis: { ...(env.REDIS_URL ? { url: env.REDIS_URL } : {}) },
|
|
71
|
+
logging: {
|
|
72
|
+
level: env.LOG_LEVEL,
|
|
73
|
+
pretty: env.NODE_ENV === "development",
|
|
74
|
+
},
|
|
75
|
+
};
|
|
76
|
+
};
|