create-deepagent-server 0.0.0-stage → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +292 -2
- package/bin/create-deepagent-server.js +27 -0
- package/package.json +50 -4
- package/src/args.js +195 -0
- package/src/cli.js +269 -0
- package/src/dependencies.js +34 -0
- package/src/prompts.js +86 -0
- package/src/registry.js +68 -0
- package/src/scaffold.js +181 -0
- package/src/setup.js +79 -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 +97 -0
- package/templates/deepagent/js/scripts/dev.js +40 -0
- package/templates/deepagent/js/src/agents/factory.js +11 -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 +53 -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/supervisor.js +21 -0
- package/templates/deepagent/js/src/agents/prompts/supervisor.md +41 -0
- package/templates/deepagent/js/src/agents/state/index.js +1 -0
- package/templates/deepagent/js/src/agents/state/types.js +11 -0
- package/templates/deepagent/js/src/agents/subagents/analyst.js +12 -0
- package/templates/deepagent/js/src/agents/subagents/coder.js +12 -0
- package/templates/deepagent/js/src/agents/subagents/index.js +9 -0
- package/templates/deepagent/js/src/agents/supervisor.js +64 -0
- package/templates/deepagent/js/src/api/app.js +29 -0
- package/templates/deepagent/js/src/api/middleware/auth.js +36 -0
- package/templates/deepagent/js/src/api/middleware/error.js +30 -0
- package/templates/deepagent/js/src/api/routes/agent.js +43 -0
- package/templates/deepagent/js/src/api/routes/health.js +40 -0
- package/templates/deepagent/js/src/api/routes/session.js +42 -0
- package/templates/deepagent/js/src/app.js +98 -0
- package/templates/deepagent/js/src/backends/composite.js +22 -0
- package/templates/deepagent/js/src/backends/filesystem.js +9 -0
- package/templates/deepagent/js/src/backends/index.js +60 -0
- package/templates/deepagent/js/src/backends/sandbox.js +26 -0
- package/templates/deepagent/js/src/config/agent.js +14 -0
- package/templates/deepagent/js/src/config/database.js +19 -0
- package/templates/deepagent/js/src/config/env.js +96 -0
- package/templates/deepagent/js/src/config/index.js +45 -0
- package/templates/deepagent/js/src/graph/checkpointer.js +28 -0
- package/templates/deepagent/js/src/graph/compile.js +9 -0
- package/templates/deepagent/js/src/graph/index.js +3 -0
- package/templates/deepagent/js/src/graph/store.js +28 -0
- package/templates/deepagent/js/src/index.js +39 -0
- package/templates/deepagent/js/src/memory/index.js +8 -0
- package/templates/deepagent/js/src/memory/manager.js +50 -0
- package/templates/deepagent/js/src/memory/store.js +43 -0
- package/templates/deepagent/js/src/middleware/approval.js +47 -0
- package/templates/deepagent/js/src/middleware/context.js +35 -0
- package/templates/deepagent/js/src/middleware/index.js +9 -0
- package/templates/deepagent/js/src/middleware/logging.js +70 -0
- package/templates/deepagent/js/src/middleware/security.js +58 -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 +40 -0
- package/templates/deepagent/js/src/schemas/common.js +24 -0
- package/templates/deepagent/js/src/schemas/locale.js +4 -0
- package/templates/deepagent/js/src/schemas/session.js +27 -0
- package/templates/deepagent/js/src/services/agent.js +136 -0
- package/templates/deepagent/js/src/services/memory.js +33 -0
- package/templates/deepagent/js/src/services/session.js +118 -0
- package/templates/deepagent/js/src/skills/index.js +21 -0
- package/templates/deepagent/js/src/skills/loader.js +75 -0
- package/templates/deepagent/js/src/storage/database/client.js +57 -0
- package/templates/deepagent/js/src/storage/database/migrations/001_init.sql +20 -0
- package/templates/deepagent/js/src/storage/logger/index.js +21 -0
- package/templates/deepagent/js/src/storage/redis/client.js +51 -0
- package/templates/deepagent/js/src/storage/shutdown/index.js +58 -0
- package/templates/deepagent/js/src/tools/database/index.js +1 -0
- package/templates/deepagent/js/src/tools/database/query.js +74 -0
- package/templates/deepagent/js/src/tools/filesystem/index.js +102 -0
- package/templates/deepagent/js/src/tools/index.js +25 -0
- package/templates/deepagent/js/src/tools/mcp/client.js +42 -0
- package/templates/deepagent/js/src/tools/mcp/connections.js +64 -0
- package/templates/deepagent/js/src/tools/mcp/index.js +10 -0
- package/templates/deepagent/js/src/tools/mcp/registry.js +36 -0
- package/templates/deepagent/js/src/utils/errors.js +73 -0
- package/templates/deepagent/js/src/utils/helpers.js +57 -0
- package/templates/deepagent/js/src/utils/validation.js +15 -0
- package/templates/deepagent/js/tests/agents/prompts.test.js +30 -0
- package/templates/deepagent/js/tests/agents/supervisor.test.js +42 -0
- package/templates/deepagent/js/tests/config/database.test.js +40 -0
- package/templates/deepagent/js/tests/integration/api.test.js +114 -0
- package/templates/deepagent/js/tests/middleware/security.test.js +27 -0
- 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 +27 -0
- package/templates/deepagent/shared/README.md +249 -0
- package/templates/deepagent/shared/_editorconfig +12 -0
- package/templates/deepagent/shared/_env.example +86 -0
- package/templates/deepagent/shared/_gitignore +22 -0
- package/templates/deepagent/shared/deployment/docker-compose.yml +60 -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/workspaces/README.md +5 -0
- package/templates/deepagent/ts/deployment/Dockerfile +25 -0
- package/templates/deepagent/ts/scripts/check.ts +99 -0
- package/templates/deepagent/ts/scripts/copy-assets.ts +37 -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 +53 -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/supervisor.md +41 -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 +18 -0
- package/templates/deepagent/ts/src/agents/subagents/coder.ts +14 -0
- package/templates/deepagent/ts/src/agents/subagents/index.ts +9 -0
- package/templates/deepagent/ts/src/agents/supervisor.ts +82 -0
- package/templates/deepagent/ts/src/api/app.ts +48 -0
- package/templates/deepagent/ts/src/api/middleware/auth.ts +38 -0
- package/templates/deepagent/ts/src/api/middleware/error.ts +33 -0
- package/templates/deepagent/ts/src/api/routes/agent.ts +48 -0
- package/templates/deepagent/ts/src/api/routes/health.ts +47 -0
- package/templates/deepagent/ts/src/api/routes/session.ts +47 -0
- package/templates/deepagent/ts/src/app.ts +111 -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 +76 -0
- package/templates/deepagent/ts/src/backends/sandbox.ts +33 -0
- package/templates/deepagent/ts/src/config/agent.ts +37 -0
- package/templates/deepagent/ts/src/config/database.ts +21 -0
- package/templates/deepagent/ts/src/config/env.ts +106 -0
- package/templates/deepagent/ts/src/config/index.ts +84 -0
- package/templates/deepagent/ts/src/graph/checkpointer.ts +29 -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 +29 -0
- package/templates/deepagent/ts/src/index.ts +39 -0
- package/templates/deepagent/ts/src/memory/index.ts +11 -0
- package/templates/deepagent/ts/src/memory/manager.ts +76 -0
- package/templates/deepagent/ts/src/memory/store.ts +59 -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 +71 -0
- package/templates/deepagent/ts/src/middleware/security.ts +61 -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/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.ts +170 -0
- package/templates/deepagent/ts/src/services/memory.ts +43 -0
- package/templates/deepagent/ts/src/services/session.ts +158 -0
- package/templates/deepagent/ts/src/skills/index.ts +26 -0
- package/templates/deepagent/ts/src/skills/loader.ts +90 -0
- package/templates/deepagent/ts/src/storage/database/client.ts +65 -0
- package/templates/deepagent/ts/src/storage/database/migrations/001_init.sql +20 -0
- package/templates/deepagent/ts/src/storage/logger/index.ts +25 -0
- package/templates/deepagent/ts/src/storage/redis/client.ts +57 -0
- package/templates/deepagent/ts/src/storage/shutdown/index.ts +82 -0
- package/templates/deepagent/ts/src/tools/database/index.ts +5 -0
- package/templates/deepagent/ts/src/tools/database/query.ts +79 -0
- package/templates/deepagent/ts/src/tools/filesystem/index.ts +110 -0
- package/templates/deepagent/ts/src/tools/index.ts +43 -0
- package/templates/deepagent/ts/src/tools/mcp/client.ts +54 -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 +45 -0
- package/templates/deepagent/ts/src/utils/errors.ts +95 -0
- package/templates/deepagent/ts/src/utils/helpers.ts +57 -0
- package/templates/deepagent/ts/src/utils/validation.ts +17 -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/config/database.test.ts +40 -0
- package/templates/deepagent/ts/tests/integration/api.test.ts +122 -0
- package/templates/deepagent/ts/tests/middleware/security.test.ts +27 -0
- 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 +27 -0
- package/templates/deepagent/ts/tsconfig.json +19 -0
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
|
|
3
|
+
import { threadIdSchema } from "./common.js";
|
|
4
|
+
|
|
5
|
+
export const invokeAgentSchema = z.object({
|
|
6
|
+
message: z.string().min(1).max(32_000),
|
|
7
|
+
threadId: threadIdSchema.optional(),
|
|
8
|
+
tenantId: z.string().min(1).max(128).default("default"),
|
|
9
|
+
userId: z.string().min(1).max(128).optional(),
|
|
10
|
+
objective: z.string().max(2_000).optional(),
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
export type InvokeAgentInput = z.infer<typeof invokeAgentSchema>;
|
|
14
|
+
|
|
15
|
+
export const messageSchema = z.object({
|
|
16
|
+
role: z.string(),
|
|
17
|
+
content: z.unknown(),
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
export const invokeAgentResponseSchema = z.object({
|
|
21
|
+
threadId: z.string(),
|
|
22
|
+
text: z.string(),
|
|
23
|
+
messages: z.array(messageSchema),
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
export type InvokeAgentResponse = z.infer<typeof invokeAgentResponseSchema>;
|
|
27
|
+
|
|
28
|
+
export const approvalDecisionSchema = z.discriminatedUnion("type", [
|
|
29
|
+
z.object({ type: z.literal("approve") }),
|
|
30
|
+
z.object({ type: z.literal("reject"), message: z.string().max(2_000).optional() }),
|
|
31
|
+
z.object({ type: z.literal("respond"), message: z.string().max(8_000) }),
|
|
32
|
+
]);
|
|
33
|
+
|
|
34
|
+
export type ApprovalDecision = z.infer<typeof approvalDecisionSchema>;
|
|
35
|
+
|
|
36
|
+
export const resumeAgentSchema = z.object({
|
|
37
|
+
threadId: threadIdSchema,
|
|
38
|
+
tenantId: z.string().min(1).max(128).default("default"),
|
|
39
|
+
userId: z.string().min(1).max(128).optional(),
|
|
40
|
+
decisions: z.array(approvalDecisionSchema).min(1),
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
export type ResumeAgentInput = z.infer<typeof resumeAgentSchema>;
|
|
44
|
+
|
|
45
|
+
export const streamEventSchema = z.object({
|
|
46
|
+
event: z.enum(["token", "values", "error", "done"]),
|
|
47
|
+
data: z.unknown(),
|
|
48
|
+
});
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import "./locale.js";
|
|
2
|
+
|
|
3
|
+
import { z } from "zod";
|
|
4
|
+
|
|
5
|
+
export const idSchema = z.string().uuid();
|
|
6
|
+
|
|
7
|
+
export const threadIdSchema = z
|
|
8
|
+
.string()
|
|
9
|
+
.min(1)
|
|
10
|
+
.max(128)
|
|
11
|
+
.regex(/^[a-zA-Z0-9._:-]+$/, "会话 ID 只能包含字母、数字、点、下划线、冒号和短横线。");
|
|
12
|
+
|
|
13
|
+
export const paginationSchema = z.object({
|
|
14
|
+
limit: z.coerce.number().int().min(1).max(100).default(20),
|
|
15
|
+
offset: z.coerce.number().int().min(0).default(0),
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
export const apiErrorSchema = z.object({
|
|
19
|
+
error: z.object({
|
|
20
|
+
code: z.string(),
|
|
21
|
+
message: z.string(),
|
|
22
|
+
details: z.record(z.string(), z.unknown()).optional(),
|
|
23
|
+
}),
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
export type Pagination = z.infer<typeof paginationSchema>;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
|
|
3
|
+
import { threadIdSchema } from "./common.js";
|
|
4
|
+
|
|
5
|
+
export const createSessionSchema = z.object({
|
|
6
|
+
threadId: threadIdSchema.optional(),
|
|
7
|
+
title: z.string().min(1).max(200).default("未命名会话"),
|
|
8
|
+
tenantId: z.string().min(1).max(128).default("default"),
|
|
9
|
+
userId: z.string().min(1).max(128).optional(),
|
|
10
|
+
metadata: z.record(z.string(), z.unknown()).default({}),
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
export type CreateSessionInput = z.infer<typeof createSessionSchema>;
|
|
14
|
+
|
|
15
|
+
export const sessionSummarySchema = z.object({
|
|
16
|
+
threadId: z.string(),
|
|
17
|
+
title: z.string(),
|
|
18
|
+
tenantId: z.string(),
|
|
19
|
+
userId: z.string().optional(),
|
|
20
|
+
createdAt: z.string(),
|
|
21
|
+
updatedAt: z.string(),
|
|
22
|
+
messageCount: z.number().int().nonnegative(),
|
|
23
|
+
metadata: z.record(z.string(), z.unknown()).default({}),
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
export type SessionSummary = z.infer<typeof sessionSummarySchema>;
|
|
27
|
+
|
|
28
|
+
export const sessionListSchema = z.object({
|
|
29
|
+
sessions: z.array(sessionSummarySchema),
|
|
30
|
+
total: z.number().int().nonnegative(),
|
|
31
|
+
});
|
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
|
+
|
|
3
|
+
import { Command } from "@langchain/langgraph";
|
|
4
|
+
|
|
5
|
+
import type { CompiledAgent } from "../graph/index.js";
|
|
6
|
+
import type { AppConfig } from "../config/index.js";
|
|
7
|
+
import type { InvokeAgentInput, ResumeAgentInput } from "../schemas/agent.js";
|
|
8
|
+
import { lastMessageText } from "../utils/helpers.js";
|
|
9
|
+
import type { SessionService } from "./session.js";
|
|
10
|
+
|
|
11
|
+
export interface AgentServiceDependencies {
|
|
12
|
+
agent: CompiledAgent;
|
|
13
|
+
config: AppConfig;
|
|
14
|
+
sessions?: SessionService;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export interface InvokeAgentResult {
|
|
18
|
+
threadId: string;
|
|
19
|
+
text: string;
|
|
20
|
+
messages: unknown[];
|
|
21
|
+
/** 运行因审批门暂停时存在。 */
|
|
22
|
+
interrupts?: unknown[];
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export interface AgentStreamEvent {
|
|
26
|
+
event: "values";
|
|
27
|
+
data: unknown;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export interface RunOptions {
|
|
31
|
+
signal?: AbortSignal;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* 应用层 Agent API:构建 invoke 输入,附加会话、租户和运行时上下文,
|
|
36
|
+
* 并把结果整理为适合 HTTP 客户端的形式。
|
|
37
|
+
*/
|
|
38
|
+
export class AgentService {
|
|
39
|
+
private readonly agent: CompiledAgent;
|
|
40
|
+
private readonly config: AppConfig;
|
|
41
|
+
private readonly sessions?: SessionService;
|
|
42
|
+
|
|
43
|
+
constructor({ agent, config, sessions }: AgentServiceDependencies) {
|
|
44
|
+
this.agent = agent;
|
|
45
|
+
this.config = config;
|
|
46
|
+
this.sessions = sessions;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
get graph(): CompiledAgent {
|
|
50
|
+
return this.agent;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
async invoke(input: InvokeAgentInput, { signal }: RunOptions = {}): Promise<InvokeAgentResult> {
|
|
54
|
+
const threadId = input.threadId ?? randomUUID();
|
|
55
|
+
|
|
56
|
+
await this.sessions?.ensure({
|
|
57
|
+
threadId,
|
|
58
|
+
tenantId: input.tenantId,
|
|
59
|
+
...(input.userId ? { userId: input.userId } : {}),
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
const result = await this.agent.invoke(
|
|
63
|
+
{
|
|
64
|
+
messages: [{ role: "user", content: input.message }],
|
|
65
|
+
tenantId: input.tenantId,
|
|
66
|
+
...(input.userId ? { userId: input.userId } : {}),
|
|
67
|
+
objective: input.objective ?? input.message,
|
|
68
|
+
},
|
|
69
|
+
{
|
|
70
|
+
configurable: { thread_id: threadId },
|
|
71
|
+
recursionLimit: this.config.agent.recursionLimit,
|
|
72
|
+
context: {
|
|
73
|
+
tenantId: input.tenantId,
|
|
74
|
+
...(input.userId ? { userId: input.userId } : {}),
|
|
75
|
+
threadId,
|
|
76
|
+
},
|
|
77
|
+
...(signal ? { signal } : {}),
|
|
78
|
+
},
|
|
79
|
+
);
|
|
80
|
+
|
|
81
|
+
return this.finish(threadId, input.tenantId, result);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* 恢复因审批而暂停的运行。decisions 会按顺序匹配待处理的工具调用
|
|
86
|
+
* (approve、reject 或 respond)。
|
|
87
|
+
*/
|
|
88
|
+
async resume(input: ResumeAgentInput, { signal }: RunOptions = {}): Promise<InvokeAgentResult> {
|
|
89
|
+
const result = await this.agent.invoke(
|
|
90
|
+
new Command({ resume: { decisions: input.decisions } }),
|
|
91
|
+
{
|
|
92
|
+
configurable: { thread_id: input.threadId },
|
|
93
|
+
recursionLimit: this.config.agent.recursionLimit,
|
|
94
|
+
context: {
|
|
95
|
+
tenantId: input.tenantId,
|
|
96
|
+
...(input.userId ? { userId: input.userId } : {}),
|
|
97
|
+
threadId: input.threadId,
|
|
98
|
+
},
|
|
99
|
+
...(signal ? { signal } : {}),
|
|
100
|
+
},
|
|
101
|
+
);
|
|
102
|
+
|
|
103
|
+
return this.finish(input.threadId, input.tenantId, result);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
private async finish(
|
|
107
|
+
threadId: string,
|
|
108
|
+
tenantId: string,
|
|
109
|
+
result: { messages?: unknown[]; __interrupt__?: unknown },
|
|
110
|
+
): Promise<InvokeAgentResult> {
|
|
111
|
+
const messages = Array.isArray(result.messages) ? result.messages : [];
|
|
112
|
+
const text = lastMessageText(messages);
|
|
113
|
+
const interrupts = Array.isArray(result.__interrupt__) ? result.__interrupt__ : [];
|
|
114
|
+
|
|
115
|
+
await this.sessions?.touch({
|
|
116
|
+
threadId,
|
|
117
|
+
tenantId,
|
|
118
|
+
lastMessage: text,
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
return {
|
|
122
|
+
threadId,
|
|
123
|
+
text,
|
|
124
|
+
messages,
|
|
125
|
+
...(interrupts.length > 0 ? { interrupts } : {}),
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/** 以状态快照流式输出 Agent 的工作过程(适配 SSE)。 */
|
|
130
|
+
async *stream(
|
|
131
|
+
input: InvokeAgentInput,
|
|
132
|
+
{ signal }: RunOptions = {},
|
|
133
|
+
): AsyncGenerator<AgentStreamEvent> {
|
|
134
|
+
const threadId = input.threadId ?? randomUUID();
|
|
135
|
+
|
|
136
|
+
await this.sessions?.ensure({
|
|
137
|
+
threadId,
|
|
138
|
+
tenantId: input.tenantId,
|
|
139
|
+
...(input.userId ? { userId: input.userId } : {}),
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
const stream = await this.agent.stream(
|
|
143
|
+
{
|
|
144
|
+
messages: [{ role: "user", content: input.message }],
|
|
145
|
+
tenantId: input.tenantId,
|
|
146
|
+
...(input.userId ? { userId: input.userId } : {}),
|
|
147
|
+
objective: input.objective ?? input.message,
|
|
148
|
+
},
|
|
149
|
+
{
|
|
150
|
+
configurable: { thread_id: threadId },
|
|
151
|
+
recursionLimit: this.config.agent.recursionLimit,
|
|
152
|
+
streamMode: "values",
|
|
153
|
+
context: {
|
|
154
|
+
tenantId: input.tenantId,
|
|
155
|
+
...(input.userId ? { userId: input.userId } : {}),
|
|
156
|
+
threadId,
|
|
157
|
+
},
|
|
158
|
+
...(signal ? { signal } : {}),
|
|
159
|
+
},
|
|
160
|
+
);
|
|
161
|
+
|
|
162
|
+
for await (const chunk of stream as AsyncIterable<unknown>) {
|
|
163
|
+
yield { event: "values", data: chunk };
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
async state(threadId: string): Promise<unknown> {
|
|
168
|
+
return this.agent.getState({ configurable: { thread_id: threadId } });
|
|
169
|
+
}
|
|
170
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import type { MemoryManager, MemoryRecord } from "../memory/index.js";
|
|
2
|
+
|
|
3
|
+
export interface RememberInput {
|
|
4
|
+
tenantId: string;
|
|
5
|
+
userId?: string;
|
|
6
|
+
key: string;
|
|
7
|
+
value: Record<string, unknown>;
|
|
8
|
+
kind?: "preference" | "fact";
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* 面向 HTTP 的记忆服务。封装 `MemoryManager`,
|
|
13
|
+
* 让路由层不需要了解命名空间和 store 细节。
|
|
14
|
+
*/
|
|
15
|
+
export class MemoryService {
|
|
16
|
+
private readonly memory: MemoryManager;
|
|
17
|
+
|
|
18
|
+
constructor(memory: MemoryManager) {
|
|
19
|
+
this.memory = memory;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
async remember({ tenantId, userId, key, value, kind = "fact" }: RememberInput): Promise<void> {
|
|
23
|
+
const target = { tenantId, ...(userId ? { userId } : {}) };
|
|
24
|
+
|
|
25
|
+
if (kind === "preference") {
|
|
26
|
+
await this.memory.rememberPreference(target, key, value);
|
|
27
|
+
} else {
|
|
28
|
+
await this.memory.rememberFact(target, key, value);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
async recall(tenantId: string, key: string, userId?: string): Promise<MemoryRecord | null> {
|
|
33
|
+
return this.memory.recall({ tenantId, ...(userId ? { userId } : {}) }, key);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
async list(tenantId: string, userId?: string, limit = 50): Promise<MemoryRecord[]> {
|
|
37
|
+
return this.memory.list({ tenantId, ...(userId ? { userId } : {}) }, limit);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
async forget(tenantId: string, key: string, userId?: string): Promise<void> {
|
|
41
|
+
await this.memory.forget({ tenantId, ...(userId ? { userId } : {}) }, key);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
|
+
|
|
3
|
+
import type { BaseCheckpointSaver, BaseStore } from "@langchain/langgraph";
|
|
4
|
+
|
|
5
|
+
import type { CompiledAgent } from "../graph/index.js";
|
|
6
|
+
import type { SandboxManager } from "../sandbox/index.js";
|
|
7
|
+
import type { CreateSessionInput, SessionSummary } from "../schemas/session.js";
|
|
8
|
+
import { NotFoundError } from "../utils/errors.js";
|
|
9
|
+
|
|
10
|
+
interface SessionRecord {
|
|
11
|
+
threadId: string;
|
|
12
|
+
title: string;
|
|
13
|
+
tenantId: string;
|
|
14
|
+
userId?: string;
|
|
15
|
+
createdAt: string;
|
|
16
|
+
updatedAt: string;
|
|
17
|
+
lastMessage?: string;
|
|
18
|
+
messageCount: number;
|
|
19
|
+
metadata: Record<string, unknown>;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export interface SessionServiceDependencies {
|
|
23
|
+
graph: CompiledAgent;
|
|
24
|
+
checkpointer: BaseCheckpointSaver;
|
|
25
|
+
store: BaseStore;
|
|
26
|
+
/** 提供后,删除会话会同时释放该会话的沙箱。 */
|
|
27
|
+
sandboxes?: SandboxManager;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export interface EnsureSessionInput {
|
|
31
|
+
threadId: string;
|
|
32
|
+
tenantId: string;
|
|
33
|
+
userId?: string;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export interface TouchSessionInput {
|
|
37
|
+
threadId: string;
|
|
38
|
+
tenantId: string;
|
|
39
|
+
lastMessage?: string;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* 会话元数据存放在 LangGraph store 的
|
|
44
|
+
* `["sessions", tenantId, threadId]` 下;对话本身存放在 checkpointer 中。
|
|
45
|
+
* 该服务在 HTTP API 层把两者组合起来。
|
|
46
|
+
*/
|
|
47
|
+
export class SessionService {
|
|
48
|
+
private readonly graph: CompiledAgent;
|
|
49
|
+
private readonly checkpointer: BaseCheckpointSaver;
|
|
50
|
+
private readonly store: BaseStore;
|
|
51
|
+
private readonly sandboxes?: SandboxManager;
|
|
52
|
+
|
|
53
|
+
constructor({ graph, checkpointer, store, sandboxes }: SessionServiceDependencies) {
|
|
54
|
+
this.graph = graph;
|
|
55
|
+
this.checkpointer = checkpointer;
|
|
56
|
+
this.store = store;
|
|
57
|
+
this.sandboxes = sandboxes;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
async create(input: CreateSessionInput): Promise<SessionSummary> {
|
|
61
|
+
const threadId = input.threadId ?? randomUUID();
|
|
62
|
+
const now = new Date().toISOString();
|
|
63
|
+
|
|
64
|
+
const record: SessionRecord = {
|
|
65
|
+
threadId,
|
|
66
|
+
title: input.title,
|
|
67
|
+
tenantId: input.tenantId,
|
|
68
|
+
...(input.userId ? { userId: input.userId } : {}),
|
|
69
|
+
createdAt: now,
|
|
70
|
+
updatedAt: now,
|
|
71
|
+
messageCount: 0,
|
|
72
|
+
metadata: input.metadata,
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
await this.store.put(namespace(input.tenantId), threadId, record);
|
|
76
|
+
|
|
77
|
+
return { ...record };
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/** 会话记录不存在时创建(幂等)。 */
|
|
81
|
+
async ensure({ threadId, tenantId, userId }: EnsureSessionInput): Promise<void> {
|
|
82
|
+
const existing = await this.store.get(namespace(tenantId), threadId);
|
|
83
|
+
if (existing) return;
|
|
84
|
+
|
|
85
|
+
await this.create({
|
|
86
|
+
threadId,
|
|
87
|
+
tenantId,
|
|
88
|
+
title: `Session ${threadId.slice(0, 8)}`,
|
|
89
|
+
metadata: {},
|
|
90
|
+
...(userId ? { userId } : {}),
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
async touch({ threadId, tenantId, lastMessage }: TouchSessionInput): Promise<void> {
|
|
95
|
+
const item = await this.store.get(namespace(tenantId), threadId);
|
|
96
|
+
if (!item) return;
|
|
97
|
+
|
|
98
|
+
const record = item.value as unknown as SessionRecord;
|
|
99
|
+
|
|
100
|
+
await this.store.put(namespace(tenantId), threadId, {
|
|
101
|
+
...record,
|
|
102
|
+
updatedAt: new Date().toISOString(),
|
|
103
|
+
messageCount: record.messageCount + 2,
|
|
104
|
+
...(lastMessage ? { lastMessage: lastMessage.slice(0, 1_000) } : {}),
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
async get(threadId: string, tenantId = "default"): Promise<SessionSummary> {
|
|
109
|
+
const item = await this.store.get(namespace(tenantId), threadId);
|
|
110
|
+
if (!item) {
|
|
111
|
+
throw new NotFoundError(`未找到会话 "${threadId}"。`);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
const record = item.value as unknown as SessionRecord;
|
|
115
|
+
|
|
116
|
+
const state = await this.graph.getState({
|
|
117
|
+
configurable: { thread_id: threadId },
|
|
118
|
+
});
|
|
119
|
+
const values = state.values as { messages?: unknown[] } | undefined;
|
|
120
|
+
|
|
121
|
+
return {
|
|
122
|
+
...record,
|
|
123
|
+
messageCount: values?.messages?.length ?? record.messageCount,
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
async list(
|
|
128
|
+
tenantId = "default",
|
|
129
|
+
limit = 20,
|
|
130
|
+
): Promise<{ sessions: SessionSummary[]; total: number }> {
|
|
131
|
+
const items = await this.store.search(namespace(tenantId), { limit });
|
|
132
|
+
|
|
133
|
+
const sessions = items.map(({ value }) => ({
|
|
134
|
+
...(value as unknown as SessionRecord),
|
|
135
|
+
}));
|
|
136
|
+
|
|
137
|
+
return { sessions, total: sessions.length };
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
async remove(threadId: string, tenantId = "default"): Promise<void> {
|
|
141
|
+
await this.checkpointer.deleteThread(threadId);
|
|
142
|
+
await this.store.delete(namespace(tenantId), threadId);
|
|
143
|
+
await this.sandboxes?.release(threadId);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
async history(threadId: string): Promise<unknown[]> {
|
|
147
|
+
const state = await this.graph.getState({
|
|
148
|
+
configurable: { thread_id: threadId },
|
|
149
|
+
});
|
|
150
|
+
const values = state.values as { messages?: unknown[] } | undefined;
|
|
151
|
+
|
|
152
|
+
return values?.messages ?? [];
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
const namespace = (tenantId: string): string[] => {
|
|
157
|
+
return ["sessions", tenantId];
|
|
158
|
+
};
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { discoverSkills, type SkillMetadata } from "./loader.js";
|
|
2
|
+
|
|
3
|
+
export type { SkillMetadata } from "./loader.js";
|
|
4
|
+
export { discoverSkills, parseSkillFrontmatter } from "./loader.js";
|
|
5
|
+
|
|
6
|
+
/** Deep Agents skills 中间件扫描的虚拟后端路径。 */
|
|
7
|
+
export const DEFAULT_SKILL_SOURCES = ["/skills/"] as const;
|
|
8
|
+
|
|
9
|
+
export interface LoadSkillsOptions {
|
|
10
|
+
rootDir: string;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/** 从磁盘发现技能(用于启动校验和 `scripts/check`)。 */
|
|
14
|
+
export const loadSkills = async ({ rootDir }: LoadSkillsOptions): Promise<SkillMetadata[]> => {
|
|
15
|
+
return discoverSkills(rootDir);
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* 为缺少 description 的技能返回可读警告。
|
|
20
|
+
* 模型正是依赖 description 来选择技能。
|
|
21
|
+
*/
|
|
22
|
+
export const validateSkills = (skills: readonly SkillMetadata[]): string[] => {
|
|
23
|
+
return skills
|
|
24
|
+
.filter((skill) => skill.description.trim().length === 0)
|
|
25
|
+
.map((skill) => `技能 "${skill.name}" 缺少 description(${skill.file})`);
|
|
26
|
+
};
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import type { Dirent } from "node:fs";
|
|
2
|
+
import fs from "node:fs/promises";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
|
|
5
|
+
export interface SkillMetadata {
|
|
6
|
+
name: string;
|
|
7
|
+
description: string;
|
|
8
|
+
/** 包含 SKILL.md 的目录。 */
|
|
9
|
+
directory: string;
|
|
10
|
+
/** SKILL.md 的绝对路径。 */
|
|
11
|
+
file: string;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export interface SkillFrontmatter {
|
|
15
|
+
name?: string;
|
|
16
|
+
description?: string;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** 不依赖 YAML 库,解析 SKILL.md 的 frontmatter。 */
|
|
20
|
+
export const parseSkillFrontmatter = (content: string): SkillFrontmatter => {
|
|
21
|
+
if (!content.startsWith("---")) return {};
|
|
22
|
+
|
|
23
|
+
const end = content.indexOf("\n---", 3);
|
|
24
|
+
if (end === -1) return {};
|
|
25
|
+
|
|
26
|
+
const frontmatter = content.slice(3, end);
|
|
27
|
+
const fields: Record<string, string> = {};
|
|
28
|
+
|
|
29
|
+
for (const line of frontmatter.split("\n")) {
|
|
30
|
+
const separator = line.indexOf(":");
|
|
31
|
+
if (separator === -1) continue;
|
|
32
|
+
|
|
33
|
+
const key = line.slice(0, separator).trim();
|
|
34
|
+
const value = line
|
|
35
|
+
.slice(separator + 1)
|
|
36
|
+
.trim()
|
|
37
|
+
.replace(/^["']|["']$/g, "");
|
|
38
|
+
|
|
39
|
+
if (key) fields[key] = value;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
return {
|
|
43
|
+
...(fields.name ? { name: fields.name } : {}),
|
|
44
|
+
...(fields.description ? { description: fields.description } : {}),
|
|
45
|
+
};
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
/** 读取目录;目录不存在时返回空数组。 */
|
|
49
|
+
const readDirectory = (directory: string): Promise<Dirent[]> => {
|
|
50
|
+
return fs.readdir(directory, { withFileTypes: true }).catch((error: unknown) => {
|
|
51
|
+
if ((error as NodeJS.ErrnoException).code === "ENOENT") return [];
|
|
52
|
+
throw error;
|
|
53
|
+
});
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
/** 读取文本文件;文件不存在时返回 null。 */
|
|
57
|
+
const readTextFile = (file: string): Promise<string | null> => {
|
|
58
|
+
return fs.readFile(file, "utf8").catch((error: unknown) => {
|
|
59
|
+
if ((error as NodeJS.ErrnoException).code === "ENOENT") return null;
|
|
60
|
+
throw error;
|
|
61
|
+
});
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* 扫描 `rootDir` 下包含 SKILL.md 的子目录。
|
|
66
|
+
* 目录不存在时返回空数组,保证没有技能时也能启动。
|
|
67
|
+
*/
|
|
68
|
+
export const discoverSkills = async (rootDir: string): Promise<SkillMetadata[]> => {
|
|
69
|
+
const entries = await readDirectory(rootDir);
|
|
70
|
+
const skills: SkillMetadata[] = [];
|
|
71
|
+
|
|
72
|
+
for (const entry of entries) {
|
|
73
|
+
if (!entry.isDirectory()) continue;
|
|
74
|
+
|
|
75
|
+
const directory = path.join(rootDir, entry.name);
|
|
76
|
+
const file = path.join(directory, "SKILL.md");
|
|
77
|
+
const content = await readTextFile(file);
|
|
78
|
+
if (content === null) continue;
|
|
79
|
+
|
|
80
|
+
const metadata = parseSkillFrontmatter(content);
|
|
81
|
+
skills.push({
|
|
82
|
+
name: metadata.name ?? entry.name,
|
|
83
|
+
description: metadata.description ?? "",
|
|
84
|
+
directory,
|
|
85
|
+
file,
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
return skills.sort((a, b) => a.name.localeCompare(b.name));
|
|
90
|
+
};
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { performance } from "node:perf_hooks";
|
|
2
|
+
|
|
3
|
+
import { Pool, type PoolClient } from "pg";
|
|
4
|
+
|
|
5
|
+
import { ConfigurationError } from "../../utils/errors.js";
|
|
6
|
+
import { errorMessage } from "../../utils/helpers.js";
|
|
7
|
+
|
|
8
|
+
let pool: Pool | undefined;
|
|
9
|
+
|
|
10
|
+
/** 懒加载共享的 PostgreSQL 连接池。 */
|
|
11
|
+
export const getPool = (connectionString = process.env.DATABASE_URL): Pool => {
|
|
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
|
+
export interface DatabaseHealth {
|
|
29
|
+
ok: boolean;
|
|
30
|
+
latencyMs?: number;
|
|
31
|
+
error?: string;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const probe = async (connectionString: string): Promise<DatabaseHealth> => {
|
|
35
|
+
const client: PoolClient = await getPool(connectionString).connect();
|
|
36
|
+
const startedAt = performance.now();
|
|
37
|
+
|
|
38
|
+
return client
|
|
39
|
+
.query("select 1")
|
|
40
|
+
.then(() => ({ ok: true, latencyMs: Math.round(performance.now() - startedAt) }))
|
|
41
|
+
.finally(() => client.release());
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
/** 健康检查只返回状态,不向上抛错:连接或查询失败都折算成 `ok: false`。 */
|
|
45
|
+
export const checkDatabase = async (
|
|
46
|
+
connectionString = process.env.DATABASE_URL,
|
|
47
|
+
): Promise<DatabaseHealth> => {
|
|
48
|
+
if (!connectionString) {
|
|
49
|
+
return { ok: false, error: "未配置 PostgreSQL 连接信息" };
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
return probe(connectionString).catch((error: unknown) => ({
|
|
53
|
+
ok: false,
|
|
54
|
+
error: errorMessage(error),
|
|
55
|
+
}));
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
export const closeDatabase = async (): Promise<void> => {
|
|
59
|
+
if (!pool) return;
|
|
60
|
+
|
|
61
|
+
const current = pool;
|
|
62
|
+
pool = undefined;
|
|
63
|
+
|
|
64
|
+
await current.end();
|
|
65
|
+
};
|
|
@@ -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/storage/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);
|