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,73 @@
|
|
|
1
|
+
import { Router } from "express";
|
|
2
|
+
import { invokeAgentSchema, resumeAgentSchema } from "../../schemas/agent.js";
|
|
3
|
+
import { ValidationError } from "../../utils/errors.js";
|
|
4
|
+
export const createAgentRouter = ({ agent }) => {
|
|
5
|
+
const router = Router();
|
|
6
|
+
router.post("/invoke", async (req, res, next) => {
|
|
7
|
+
try {
|
|
8
|
+
const parsed = invokeAgentSchema.safeParse(req.body);
|
|
9
|
+
if (!parsed.success) {
|
|
10
|
+
throw new ValidationError("请求体格式错误。", {
|
|
11
|
+
issues: parsed.error.issues,
|
|
12
|
+
});
|
|
13
|
+
}
|
|
14
|
+
const result = await agent.invoke(parsed.data);
|
|
15
|
+
res.json(result);
|
|
16
|
+
} catch (error) {
|
|
17
|
+
next(error);
|
|
18
|
+
}
|
|
19
|
+
});
|
|
20
|
+
router.post("/resume", async (req, res, next) => {
|
|
21
|
+
try {
|
|
22
|
+
const parsed = resumeAgentSchema.safeParse(req.body);
|
|
23
|
+
if (!parsed.success) {
|
|
24
|
+
throw new ValidationError("请求体格式错误。", {
|
|
25
|
+
issues: parsed.error.issues,
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
const result = await agent.resume(parsed.data);
|
|
29
|
+
res.json(result);
|
|
30
|
+
} catch (error) {
|
|
31
|
+
next(error);
|
|
32
|
+
}
|
|
33
|
+
});
|
|
34
|
+
router.post("/stream", async (req, res, next) => {
|
|
35
|
+
const parsed = invokeAgentSchema.safeParse(req.body);
|
|
36
|
+
if (!parsed.success) {
|
|
37
|
+
next(
|
|
38
|
+
new ValidationError("请求体格式错误。", {
|
|
39
|
+
issues: parsed.error.issues,
|
|
40
|
+
}),
|
|
41
|
+
);
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
const controller = new AbortController();
|
|
45
|
+
req.on("close", () => controller.abort());
|
|
46
|
+
res.status(200);
|
|
47
|
+
res.setHeader("Content-Type", "text/event-stream");
|
|
48
|
+
res.setHeader("Cache-Control", "no-cache, no-transform");
|
|
49
|
+
res.setHeader("Connection", "keep-alive");
|
|
50
|
+
res.flushHeaders();
|
|
51
|
+
try {
|
|
52
|
+
for await (const event of agent.stream(parsed.data, {
|
|
53
|
+
signal: controller.signal,
|
|
54
|
+
})) {
|
|
55
|
+
res.write(`event: ${event.event}\ndata: ${JSON.stringify(event.data)}\n\n`);
|
|
56
|
+
}
|
|
57
|
+
res.write("event: done\ndata: {}\n\n");
|
|
58
|
+
} catch (error) {
|
|
59
|
+
if (!res.headersSent) {
|
|
60
|
+
next(error);
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
res.write(
|
|
64
|
+
`event: error\ndata: ${JSON.stringify({
|
|
65
|
+
message: error instanceof Error ? error.message : String(error),
|
|
66
|
+
})}\n\n`,
|
|
67
|
+
);
|
|
68
|
+
} finally {
|
|
69
|
+
res.end();
|
|
70
|
+
}
|
|
71
|
+
});
|
|
72
|
+
return router;
|
|
73
|
+
};
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { Router } from "express";
|
|
2
|
+
import { checkDatabase } from "../../infrastructure/database/client.js";
|
|
3
|
+
import { checkRedis } from "../../infrastructure/redis/client.js";
|
|
4
|
+
export const createHealthRouter = ({ config, mcp }) => {
|
|
5
|
+
const router = Router();
|
|
6
|
+
router.get("/", (_req, res) => {
|
|
7
|
+
res.json({
|
|
8
|
+
status: "ok",
|
|
9
|
+
service: "deepagent",
|
|
10
|
+
agent: config.agent.name,
|
|
11
|
+
model: `${config.model.provider}/${config.model.name}`,
|
|
12
|
+
uptimeSeconds: Math.round(process.uptime()),
|
|
13
|
+
});
|
|
14
|
+
});
|
|
15
|
+
router.get("/ready", async (_req, res) => {
|
|
16
|
+
const [database, redis] = await Promise.all([
|
|
17
|
+
config.database.url ? checkDatabase() : Promise.resolve({ ok: true, skipped: true }),
|
|
18
|
+
config.redis.url ? checkRedis() : Promise.resolve({ ok: true, skipped: true }),
|
|
19
|
+
]);
|
|
20
|
+
const ready = (!config.database.url || database.ok) && (!config.redis.url || redis.ok);
|
|
21
|
+
res.status(ready ? 200 : 503).json({
|
|
22
|
+
status: ready ? "ready" : "degraded",
|
|
23
|
+
checks: {
|
|
24
|
+
database,
|
|
25
|
+
redis,
|
|
26
|
+
mcp: { servers: mcp?.serverNames ?? [] },
|
|
27
|
+
},
|
|
28
|
+
});
|
|
29
|
+
});
|
|
30
|
+
return router;
|
|
31
|
+
};
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { Router } from "express";
|
|
2
|
+
import { createSessionSchema } from "../../schemas/session.js";
|
|
3
|
+
import { ValidationError } from "../../utils/errors.js";
|
|
4
|
+
const tenantFrom = (value) => {
|
|
5
|
+
return typeof value === "string" && value.length > 0 ? value : "default";
|
|
6
|
+
};
|
|
7
|
+
export const createSessionRouter = ({ sessions }) => {
|
|
8
|
+
const router = Router();
|
|
9
|
+
router.post("/", async (req, res, next) => {
|
|
10
|
+
try {
|
|
11
|
+
const parsed = createSessionSchema.safeParse(req.body ?? {});
|
|
12
|
+
if (!parsed.success) {
|
|
13
|
+
throw new ValidationError("请求体格式错误。", {
|
|
14
|
+
issues: parsed.error.issues,
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
const input = parsed.data;
|
|
18
|
+
const session = await sessions.create(input);
|
|
19
|
+
res.status(201).json(session);
|
|
20
|
+
} catch (error) {
|
|
21
|
+
next(error);
|
|
22
|
+
}
|
|
23
|
+
});
|
|
24
|
+
router.get("/", async (req, res, next) => {
|
|
25
|
+
try {
|
|
26
|
+
const tenantId = tenantFrom(req.query.tenantId);
|
|
27
|
+
const limit = Number(req.query.limit ?? 20);
|
|
28
|
+
const result = await sessions.list(
|
|
29
|
+
tenantId,
|
|
30
|
+
Number.isFinite(limit) ? Math.min(Math.max(limit, 1), 100) : 20,
|
|
31
|
+
);
|
|
32
|
+
res.json(result);
|
|
33
|
+
} catch (error) {
|
|
34
|
+
next(error);
|
|
35
|
+
}
|
|
36
|
+
});
|
|
37
|
+
router.get("/:threadId", async (req, res, next) => {
|
|
38
|
+
try {
|
|
39
|
+
const session = await sessions.get(req.params.threadId, tenantFrom(req.query.tenantId));
|
|
40
|
+
res.json(session);
|
|
41
|
+
} catch (error) {
|
|
42
|
+
next(error);
|
|
43
|
+
}
|
|
44
|
+
});
|
|
45
|
+
router.get("/:threadId/messages", async (req, res, next) => {
|
|
46
|
+
try {
|
|
47
|
+
const messages = await sessions.history(req.params.threadId);
|
|
48
|
+
res.json({ messages });
|
|
49
|
+
} catch (error) {
|
|
50
|
+
next(error);
|
|
51
|
+
}
|
|
52
|
+
});
|
|
53
|
+
router.delete("/:threadId", async (req, res, next) => {
|
|
54
|
+
try {
|
|
55
|
+
await sessions.remove(req.params.threadId, tenantFrom(req.query.tenantId));
|
|
56
|
+
res.status(204).end();
|
|
57
|
+
} catch (error) {
|
|
58
|
+
next(error);
|
|
59
|
+
}
|
|
60
|
+
});
|
|
61
|
+
return router;
|
|
62
|
+
};
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import fs from "node:fs/promises";
|
|
2
|
+
import { createSupervisorAgent } from "./agents/supervisor.js";
|
|
3
|
+
import { createApiApp } from "./api/app.js";
|
|
4
|
+
import { createBackend } from "./backends/index.js";
|
|
5
|
+
import { createChatModel, loadConfig } from "./config/index.js";
|
|
6
|
+
import { createCheckpointer } from "./graph/checkpointer.js";
|
|
7
|
+
import { createStore } from "./graph/store.js";
|
|
8
|
+
import { closeDatabase } from "./infrastructure/database/client.js";
|
|
9
|
+
import { createLogger } from "./infrastructure/logger/index.js";
|
|
10
|
+
import { closeRedis } from "./infrastructure/redis/client.js";
|
|
11
|
+
import { createMemoryManager } from "./memory/index.js";
|
|
12
|
+
import { AgentService } from "./services/agent.service.js";
|
|
13
|
+
import { MemoryService } from "./services/memory.service.js";
|
|
14
|
+
import { SessionService } from "./services/session.service.js";
|
|
15
|
+
import { loadSkills, validateSkills } from "./skills/index.js";
|
|
16
|
+
import { createDefaultTools } from "./tools/index.js";
|
|
17
|
+
import { createMcpToolRegistry } from "./tools/mcp/index.js";
|
|
18
|
+
/**
|
|
19
|
+
* 应用装配:配置 → 基础设施 → Agent 运行时 → 服务 → HTTP 应用。
|
|
20
|
+
* 这里不启动服务,因此测试、脚本和 HTTP 入口可以复用同一套装配。
|
|
21
|
+
*/
|
|
22
|
+
export const createApplication = async (config = loadConfig()) => {
|
|
23
|
+
const logger = createLogger(config);
|
|
24
|
+
await fs.mkdir(config.paths.workspaces, { recursive: true });
|
|
25
|
+
await fs.mkdir(config.paths.resources, { recursive: true });
|
|
26
|
+
const skills = await loadSkills({ rootDir: config.paths.skills });
|
|
27
|
+
for (const warning of validateSkills(skills)) {
|
|
28
|
+
logger.warn(warning);
|
|
29
|
+
}
|
|
30
|
+
logger.info({ skills: skills.map((skill) => skill.name) }, "技能加载完成");
|
|
31
|
+
const store = await createStore(config);
|
|
32
|
+
const checkpointer = await createCheckpointer(config);
|
|
33
|
+
const backend = await createBackend(config);
|
|
34
|
+
const mcp = await createMcpToolRegistry();
|
|
35
|
+
const tools = createDefaultTools({
|
|
36
|
+
workspaceRoot: config.paths.workspaces,
|
|
37
|
+
additionalTools: mcp.tools,
|
|
38
|
+
});
|
|
39
|
+
const agent = createSupervisorAgent({
|
|
40
|
+
config,
|
|
41
|
+
model: createChatModel(config.model),
|
|
42
|
+
backend: backend.backend,
|
|
43
|
+
checkpointer,
|
|
44
|
+
store,
|
|
45
|
+
logger,
|
|
46
|
+
tools,
|
|
47
|
+
});
|
|
48
|
+
const sessions = new SessionService({ graph: agent, checkpointer, store });
|
|
49
|
+
const agentService = new AgentService({ agent, config, sessions });
|
|
50
|
+
const memoryService = new MemoryService(createMemoryManager(store));
|
|
51
|
+
const app = createApiApp({
|
|
52
|
+
config,
|
|
53
|
+
logger,
|
|
54
|
+
mcp,
|
|
55
|
+
services: {
|
|
56
|
+
agent: agentService,
|
|
57
|
+
sessions,
|
|
58
|
+
memory: memoryService,
|
|
59
|
+
},
|
|
60
|
+
});
|
|
61
|
+
let closed = false;
|
|
62
|
+
return {
|
|
63
|
+
app,
|
|
64
|
+
logger,
|
|
65
|
+
services: {
|
|
66
|
+
agent: agentService,
|
|
67
|
+
sessions,
|
|
68
|
+
memory: memoryService,
|
|
69
|
+
},
|
|
70
|
+
close: async () => {
|
|
71
|
+
if (closed) return;
|
|
72
|
+
closed = true;
|
|
73
|
+
await mcp.close();
|
|
74
|
+
await backend.dispose();
|
|
75
|
+
await closeDatabase();
|
|
76
|
+
await closeRedis();
|
|
77
|
+
},
|
|
78
|
+
};
|
|
79
|
+
};
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { CompositeBackend, StateBackend, StoreBackend } from "deepagents";
|
|
2
|
+
import { createFilesystemBackend } from "./filesystem.js";
|
|
3
|
+
/**
|
|
4
|
+
* 把 Agent 需要的所有资源挂载到同一个虚拟文件系统:
|
|
5
|
+
*
|
|
6
|
+
* /workspace/ 草稿与产物(落磁盘)
|
|
7
|
+
* /resources/ AGENTS.md 与只读知识库(落磁盘)
|
|
8
|
+
* /skills/ SKILL.md 目录(落磁盘)
|
|
9
|
+
* /memories/ 长期记忆(LangGraph BaseStore)
|
|
10
|
+
*
|
|
11
|
+
* 其余路径落到默认后端(默认是临时状态)。
|
|
12
|
+
*/
|
|
13
|
+
export const createCompositeBackend = ({ defaultBackend = new StateBackend(), paths }) => {
|
|
14
|
+
return new CompositeBackend(defaultBackend, {
|
|
15
|
+
"/workspace/": createFilesystemBackend({ rootDir: paths.workspaces }),
|
|
16
|
+
"/resources/": createFilesystemBackend({ rootDir: paths.resources }),
|
|
17
|
+
"/skills/": createFilesystemBackend({ rootDir: paths.skills }),
|
|
18
|
+
"/memories/": new StoreBackend(),
|
|
19
|
+
});
|
|
20
|
+
};
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { FilesystemBackend } from "deepagents";
|
|
2
|
+
/**
|
|
3
|
+
* 虚拟路径被限制在 `rootDir` 内的文件系统后端。
|
|
4
|
+
* Agent 看到的是 `/notes.md`,实际文件位于 `<rootDir>/notes.md`。
|
|
5
|
+
*/
|
|
6
|
+
export const createFilesystemBackend = ({ rootDir, virtualMode = true, maxFileSizeMb = 10 }) => {
|
|
7
|
+
return new FilesystemBackend({ rootDir, virtualMode, maxFileSizeMb });
|
|
8
|
+
};
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { createCompositeBackend } from "./composite.js";
|
|
2
|
+
import { createSandboxBackend } from "./sandbox.js";
|
|
3
|
+
/**
|
|
4
|
+
* 构建主 Agent 和所有子 Agent 共用的组合后端。
|
|
5
|
+
* Shell 访问会执行宿主机命令,因此默认关闭。
|
|
6
|
+
*/
|
|
7
|
+
export const createBackend = async (config) => {
|
|
8
|
+
const rootDir = config.paths.workspaces;
|
|
9
|
+
const shell = config.agent.enableShell ? await createSandboxBackend({ rootDir }) : null;
|
|
10
|
+
const backend = createCompositeBackend({
|
|
11
|
+
...(shell ? { defaultBackend: shell } : {}),
|
|
12
|
+
paths: {
|
|
13
|
+
workspaces: config.paths.workspaces,
|
|
14
|
+
resources: config.paths.resources,
|
|
15
|
+
skills: config.paths.skills,
|
|
16
|
+
},
|
|
17
|
+
});
|
|
18
|
+
return {
|
|
19
|
+
backend,
|
|
20
|
+
shell,
|
|
21
|
+
dispose: async () => {
|
|
22
|
+
await shell?.close();
|
|
23
|
+
},
|
|
24
|
+
};
|
|
25
|
+
};
|
|
26
|
+
export { createCompositeBackend } from "./composite.js";
|
|
27
|
+
export { createFilesystemBackend } from "./filesystem.js";
|
|
28
|
+
export { createSandboxBackend } from "./sandbox.js";
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { LocalShellBackend } from "deepagents";
|
|
2
|
+
/**
|
|
3
|
+
* 本地 shell 后端。在文件系统工具之上增加 `execute` 工具,
|
|
4
|
+
* 命令会以当前进程的权限在宿主机执行。
|
|
5
|
+
*
|
|
6
|
+
* 生产环境请保持 ENABLE_SHELL=false,或替换为真正的沙箱
|
|
7
|
+
* (LangSmithSandbox、容器、远程执行器等)。
|
|
8
|
+
*/
|
|
9
|
+
export const createSandboxBackend = async ({
|
|
10
|
+
rootDir,
|
|
11
|
+
virtualMode = true,
|
|
12
|
+
timeoutSeconds = 60,
|
|
13
|
+
maxOutputBytes = 100_000,
|
|
14
|
+
}) => {
|
|
15
|
+
const backend = new LocalShellBackend({
|
|
16
|
+
rootDir,
|
|
17
|
+
virtualMode,
|
|
18
|
+
timeout: timeoutSeconds,
|
|
19
|
+
maxOutputBytes,
|
|
20
|
+
});
|
|
21
|
+
await backend.initialize();
|
|
22
|
+
return backend;
|
|
23
|
+
};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export const DEFAULT_AGENT_DESCRIPTION =
|
|
2
|
+
"负责规划任务、把工作分派给专职子 Agent,并维护工作区文件的主 Agent。";
|
|
3
|
+
export const resolveAgentConfig = (env) => {
|
|
4
|
+
return {
|
|
5
|
+
name: env.AGENT_NAME,
|
|
6
|
+
description: DEFAULT_AGENT_DESCRIPTION,
|
|
7
|
+
recursionLimit: env.AGENT_RECURSION_LIMIT,
|
|
8
|
+
approvals: env.APPROVAL_MODE,
|
|
9
|
+
enableShell: env.ENABLE_SHELL,
|
|
10
|
+
memoryFiles: ["/resources/AGENTS.md"],
|
|
11
|
+
skillDirectories: ["/skills/"],
|
|
12
|
+
};
|
|
13
|
+
};
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import "../schemas/locale.js";
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
import { ConfigurationError } from "../utils/errors.js";
|
|
4
|
+
const booleanFlag = z
|
|
5
|
+
.union([z.boolean(), z.string()])
|
|
6
|
+
.default("false")
|
|
7
|
+
.transform((value) =>
|
|
8
|
+
typeof value === "boolean" ? value : ["1", "true", "yes", "on"].includes(value.toLowerCase()),
|
|
9
|
+
);
|
|
10
|
+
const optionalString = z
|
|
11
|
+
.string()
|
|
12
|
+
.trim()
|
|
13
|
+
.optional()
|
|
14
|
+
.transform((value) => (value === "" ? undefined : value));
|
|
15
|
+
export const envSchema = z.object({
|
|
16
|
+
NODE_ENV: z.enum(["development", "test", "production"]).default("development"),
|
|
17
|
+
HOST: z.string().default("0.0.0.0"),
|
|
18
|
+
PORT: z.coerce.number().int().min(1).max(65535).default(3000),
|
|
19
|
+
LOG_LEVEL: z.enum(["fatal", "error", "warn", "info", "debug", "trace", "silent"]).default("info"),
|
|
20
|
+
MODEL_PROVIDER: z.enum(["openai", "anthropic"]).default("openai"),
|
|
21
|
+
MODEL_NAME: optionalString,
|
|
22
|
+
MODEL_TEMPERATURE: z.coerce.number().min(0).max(2).default(0.2),
|
|
23
|
+
MODEL_MAX_TOKENS: z.coerce.number().int().positive().default(4096),
|
|
24
|
+
OPENAI_API_KEY: optionalString,
|
|
25
|
+
OPENAI_BASE_URL: optionalString,
|
|
26
|
+
ANTHROPIC_API_KEY: optionalString,
|
|
27
|
+
TAVILY_API_KEY: optionalString,
|
|
28
|
+
AGENT_NAME: z.string().default("deepagent"),
|
|
29
|
+
AGENT_API_KEY: optionalString,
|
|
30
|
+
AGENT_RECURSION_LIMIT: z.coerce.number().int().min(1).max(1000).default(50),
|
|
31
|
+
APPROVAL_MODE: z.enum(["none", "sensitive", "all"]).default("sensitive"),
|
|
32
|
+
ENABLE_SHELL: booleanFlag,
|
|
33
|
+
WORKSPACE_ROOT: z.string().default("workspaces"),
|
|
34
|
+
RESOURCES_ROOT: z.string().default("resources"),
|
|
35
|
+
SKILLS_ROOT: z.string().default("skills"),
|
|
36
|
+
CHECKPOINTER: z.enum(["memory", "postgres"]).default("memory"),
|
|
37
|
+
STORE: z.enum(["memory", "postgres"]).default("memory"),
|
|
38
|
+
DATABASE_URL: optionalString,
|
|
39
|
+
REDIS_URL: optionalString,
|
|
40
|
+
LANGSMITH_TRACING: booleanFlag,
|
|
41
|
+
LANGSMITH_API_KEY: optionalString,
|
|
42
|
+
LANGSMITH_PROJECT: z.string().default("deepagent"),
|
|
43
|
+
MCP_SERVERS: optionalString,
|
|
44
|
+
});
|
|
45
|
+
export const loadEnv = (source = process.env, { requireModelCredentials = true } = {}) => {
|
|
46
|
+
const parsed = envSchema.safeParse(source);
|
|
47
|
+
if (!parsed.success) {
|
|
48
|
+
const issues = parsed.error.issues
|
|
49
|
+
.map((issue) => `${issue.path.join(".") || "env"}: ${issue.message}`)
|
|
50
|
+
.join("; ");
|
|
51
|
+
throw new ConfigurationError(`环境变量配置无效:${issues}`);
|
|
52
|
+
}
|
|
53
|
+
const env = parsed.data;
|
|
54
|
+
if (requireModelCredentials && env.NODE_ENV !== "test") {
|
|
55
|
+
if (env.MODEL_PROVIDER === "openai" && !env.OPENAI_API_KEY) {
|
|
56
|
+
throw new ConfigurationError(
|
|
57
|
+
"MODEL_PROVIDER=openai 时必须设置 OPENAI_API_KEY(可复制 .env.example 为 .env)。",
|
|
58
|
+
);
|
|
59
|
+
}
|
|
60
|
+
if (env.MODEL_PROVIDER === "anthropic" && !env.ANTHROPIC_API_KEY) {
|
|
61
|
+
throw new ConfigurationError(
|
|
62
|
+
"MODEL_PROVIDER=anthropic 时必须设置 ANTHROPIC_API_KEY(可复制 .env.example 为 .env)。",
|
|
63
|
+
);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
if (env.CHECKPOINTER === "postgres" && !env.DATABASE_URL) {
|
|
67
|
+
throw new ConfigurationError("CHECKPOINTER=postgres 时必须设置 DATABASE_URL。");
|
|
68
|
+
}
|
|
69
|
+
if (env.STORE === "postgres" && !env.DATABASE_URL) {
|
|
70
|
+
throw new ConfigurationError("STORE=postgres 时必须设置 DATABASE_URL。");
|
|
71
|
+
}
|
|
72
|
+
return env;
|
|
73
|
+
};
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import path from "node:path";
|
|
2
|
+
import { resolveAgentConfig } from "./agent.js";
|
|
3
|
+
import { loadEnv } from "./env.js";
|
|
4
|
+
import { resolveModelConfig } from "./model.js";
|
|
5
|
+
export { loadEnv } from "./env.js";
|
|
6
|
+
export { createChatModel, DEFAULT_MODELS } from "./model.js";
|
|
7
|
+
/**
|
|
8
|
+
* 把环境变量转换为带类型、使用绝对路径的应用配置,全局只在这里处理。
|
|
9
|
+
*/
|
|
10
|
+
export const loadConfig = (options = {}) => {
|
|
11
|
+
const root = path.resolve(options.cwd ?? process.cwd());
|
|
12
|
+
const env =
|
|
13
|
+
options.env ??
|
|
14
|
+
loadEnv(process.env, {
|
|
15
|
+
requireModelCredentials: options.requireModelCredentials ?? true,
|
|
16
|
+
});
|
|
17
|
+
return {
|
|
18
|
+
env,
|
|
19
|
+
server: { host: env.HOST, port: env.PORT },
|
|
20
|
+
paths: {
|
|
21
|
+
root,
|
|
22
|
+
workspaces: path.resolve(root, env.WORKSPACE_ROOT),
|
|
23
|
+
resources: path.resolve(root, env.RESOURCES_ROOT),
|
|
24
|
+
skills: path.resolve(root, env.SKILLS_ROOT),
|
|
25
|
+
},
|
|
26
|
+
model: resolveModelConfig(env),
|
|
27
|
+
agent: resolveAgentConfig(env),
|
|
28
|
+
database: { ...(env.DATABASE_URL ? { url: env.DATABASE_URL } : {}) },
|
|
29
|
+
redis: { ...(env.REDIS_URL ? { url: env.REDIS_URL } : {}) },
|
|
30
|
+
logging: {
|
|
31
|
+
level: env.LOG_LEVEL,
|
|
32
|
+
pretty: env.NODE_ENV === "development",
|
|
33
|
+
},
|
|
34
|
+
};
|
|
35
|
+
};
|
|
@@ -0,0 +1,34 @@
|
|
|
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
|
+
};
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { MemorySaver } from "@langchain/langgraph";
|
|
2
|
+
import { ConfigurationError } from "../utils/errors.js";
|
|
3
|
+
/**
|
|
4
|
+
* Checkpointer 以 `thread_id` 为单位持久化会话状态。
|
|
5
|
+
*
|
|
6
|
+
* - memory:进程内存,零配置(默认)
|
|
7
|
+
* - postgres:持久化,需要 DATABASE_URL 和 checkpoint 表
|
|
8
|
+
*/
|
|
9
|
+
export const createCheckpointer = async (config) => {
|
|
10
|
+
if (config.env.CHECKPOINTER !== "postgres") {
|
|
11
|
+
return new MemorySaver();
|
|
12
|
+
}
|
|
13
|
+
if (!config.database.url) {
|
|
14
|
+
throw new ConfigurationError("CHECKPOINTER=postgres 时必须设置 DATABASE_URL。");
|
|
15
|
+
}
|
|
16
|
+
const { PostgresSaver } = await import("@langchain/langgraph-checkpoint-postgres");
|
|
17
|
+
const saver = PostgresSaver.fromConnString(config.database.url);
|
|
18
|
+
await saver.setup();
|
|
19
|
+
return saver;
|
|
20
|
+
};
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { InMemoryStore } from "@langchain/langgraph";
|
|
2
|
+
import { ConfigurationError } from "../utils/errors.js";
|
|
3
|
+
/**
|
|
4
|
+
* Store 用于长期记忆和 `/memories/` 后端挂载。
|
|
5
|
+
*
|
|
6
|
+
* - memory:进程内存(默认)
|
|
7
|
+
* - postgres:持久化,需要 DATABASE_URL 和 store 表
|
|
8
|
+
*/
|
|
9
|
+
export const createStore = async (config) => {
|
|
10
|
+
if (config.env.STORE !== "postgres") {
|
|
11
|
+
return new InMemoryStore();
|
|
12
|
+
}
|
|
13
|
+
if (!config.database.url) {
|
|
14
|
+
throw new ConfigurationError("STORE=postgres 时必须设置 DATABASE_URL。");
|
|
15
|
+
}
|
|
16
|
+
const { PostgresStore } = await import("@langchain/langgraph-checkpoint-postgres/store");
|
|
17
|
+
const store = PostgresStore.fromConnString(config.database.url);
|
|
18
|
+
await store.setup();
|
|
19
|
+
return store;
|
|
20
|
+
};
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import "dotenv/config";
|
|
2
|
+
import { createApplication } from "./app.js";
|
|
3
|
+
import { loadConfig } from "./config/index.js";
|
|
4
|
+
import { createLogger } from "./infrastructure/logger/index.js";
|
|
5
|
+
import { createShutdownManager } from "./infrastructure/shutdown/index.js";
|
|
6
|
+
const config = loadConfig();
|
|
7
|
+
const logger = createLogger(config);
|
|
8
|
+
const shutdown = createShutdownManager({ logger });
|
|
9
|
+
const application = await createApplication(config);
|
|
10
|
+
const server = application.app.listen(config.server.port, config.server.host, () => {
|
|
11
|
+
logger.info(
|
|
12
|
+
{
|
|
13
|
+
host: config.server.host,
|
|
14
|
+
port: config.server.port,
|
|
15
|
+
model: `${config.model.provider}/${config.model.name}`,
|
|
16
|
+
},
|
|
17
|
+
"DeepAgent API 已启动",
|
|
18
|
+
);
|
|
19
|
+
});
|
|
20
|
+
server.on("error", (error) => {
|
|
21
|
+
logger.error({ error: error.message }, "HTTP 服务启动失败");
|
|
22
|
+
void shutdown.shutdown("server-error").finally(() => process.exit(1));
|
|
23
|
+
});
|
|
24
|
+
shutdown.register({
|
|
25
|
+
name: "http",
|
|
26
|
+
close: () =>
|
|
27
|
+
new Promise((resolve) => {
|
|
28
|
+
server.close(() => resolve());
|
|
29
|
+
}),
|
|
30
|
+
});
|
|
31
|
+
shutdown.register({ name: "application", close: application.close });
|
|
32
|
+
shutdown.install();
|
|
@@ -0,0 +1,43 @@
|
|
|
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
|
+
};
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
-- 可选的业务表。
|
|
2
|
+
--
|
|
3
|
+
-- LangGraph 的 PostgresSaver 和 PostgresStore 在设置
|
|
4
|
+
-- CHECKPOINTER=postgres / STORE=postgres 后,会通过 `.setup()` 自行创建并迁移
|
|
5
|
+
-- 所需的表;本迁移只用于应用自有的表结构。
|
|
6
|
+
--
|
|
7
|
+
-- 使用 psql 或你常用的迁移工具执行:
|
|
8
|
+
-- psql "$DATABASE_URL" -f src/infrastructure/database/migrations/001_init.sql
|
|
9
|
+
|
|
10
|
+
CREATE TABLE IF NOT EXISTS agent_audit_log (
|
|
11
|
+
id BIGSERIAL PRIMARY KEY,
|
|
12
|
+
tenant_id TEXT NOT NULL DEFAULT 'default',
|
|
13
|
+
thread_id TEXT NOT NULL,
|
|
14
|
+
event TEXT NOT NULL,
|
|
15
|
+
payload JSONB NOT NULL DEFAULT '{}'::jsonb,
|
|
16
|
+
created_at TIMESTAMPTZ NOT NULL DEFAULT now()
|
|
17
|
+
);
|
|
18
|
+
|
|
19
|
+
CREATE INDEX IF NOT EXISTS agent_audit_log_thread_idx
|
|
20
|
+
ON agent_audit_log (tenant_id, thread_id, created_at DESC);
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import pino from "pino";
|
|
2
|
+
/** `LOG_PRETTY=true` 时使用的 pino-pretty 传输配置。 */
|
|
3
|
+
const PRETTY_TRANSPORT = {
|
|
4
|
+
transport: {
|
|
5
|
+
target: "pino-pretty",
|
|
6
|
+
options: {
|
|
7
|
+
colorize: true,
|
|
8
|
+
translateTime: "SYS:standard",
|
|
9
|
+
ignore: "pid,hostname",
|
|
10
|
+
},
|
|
11
|
+
},
|
|
12
|
+
};
|
|
13
|
+
export const createLogger = (config) => {
|
|
14
|
+
return pino({
|
|
15
|
+
level: config.logging.level,
|
|
16
|
+
base: { service: "deepagent", env: config.env.NODE_ENV },
|
|
17
|
+
...(config.logging.pretty ? PRETTY_TRANSPORT : {}),
|
|
18
|
+
});
|
|
19
|
+
};
|