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,188 @@
|
|
|
1
|
+
import assert from "node:assert/strict";
|
|
2
|
+
import test from "node:test";
|
|
3
|
+
|
|
4
|
+
import { SandboxApiException, type Sandbox } from "@alibaba-group/opensandbox";
|
|
5
|
+
|
|
6
|
+
import { OpenSandboxBackend } from "../../src/sandbox/index.js";
|
|
7
|
+
|
|
8
|
+
interface FakeSandboxCalls {
|
|
9
|
+
sessions: Array<{ workingDirectory?: string }>;
|
|
10
|
+
run: Array<{ sessionId: string; command: string }>;
|
|
11
|
+
deleted: string[];
|
|
12
|
+
directories: string[];
|
|
13
|
+
writes: string[];
|
|
14
|
+
reads: string[];
|
|
15
|
+
renewals: number[];
|
|
16
|
+
killed: boolean;
|
|
17
|
+
closed: boolean;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const createFakeSandbox = ({ failFirstRun = false }: { failFirstRun?: boolean } = {}): {
|
|
21
|
+
sandbox: Sandbox;
|
|
22
|
+
calls: FakeSandboxCalls;
|
|
23
|
+
} => {
|
|
24
|
+
const calls: FakeSandboxCalls = {
|
|
25
|
+
sessions: [],
|
|
26
|
+
run: [],
|
|
27
|
+
deleted: [],
|
|
28
|
+
directories: [],
|
|
29
|
+
writes: [],
|
|
30
|
+
reads: [],
|
|
31
|
+
renewals: [],
|
|
32
|
+
killed: false,
|
|
33
|
+
closed: false,
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
let created = 0;
|
|
37
|
+
let remainingFailures = failFirstRun ? 1 : 0;
|
|
38
|
+
|
|
39
|
+
const sandbox = {
|
|
40
|
+
id: "sbx-test",
|
|
41
|
+
commands: {
|
|
42
|
+
createSession: async (options?: { workingDirectory?: string }) => {
|
|
43
|
+
calls.sessions.push({ workingDirectory: options?.workingDirectory });
|
|
44
|
+
created += 1;
|
|
45
|
+
return `session-${created}`;
|
|
46
|
+
},
|
|
47
|
+
runInSession: async (sessionId: string, command: string) => {
|
|
48
|
+
if (remainingFailures > 0) {
|
|
49
|
+
remainingFailures -= 1;
|
|
50
|
+
throw new SandboxApiException({ statusCode: 404, message: "session not found" });
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
calls.run.push({ sessionId, command });
|
|
54
|
+
return {
|
|
55
|
+
logs: {
|
|
56
|
+
stdout: [{ text: "hello", timestamp: 0 }],
|
|
57
|
+
stderr: [{ text: "boom", timestamp: 0 }],
|
|
58
|
+
},
|
|
59
|
+
exitCode: 0,
|
|
60
|
+
};
|
|
61
|
+
},
|
|
62
|
+
deleteSession: async (sessionId: string) => {
|
|
63
|
+
calls.deleted.push(sessionId);
|
|
64
|
+
},
|
|
65
|
+
},
|
|
66
|
+
files: {
|
|
67
|
+
createDirectories: async (entries: Array<{ path: string }>) => {
|
|
68
|
+
calls.directories.push(...entries.map((entry) => entry.path));
|
|
69
|
+
},
|
|
70
|
+
writeFiles: async (entries: Array<{ path: string }>) => {
|
|
71
|
+
calls.writes.push(...entries.map((entry) => entry.path));
|
|
72
|
+
},
|
|
73
|
+
readBytes: async (path: string) => {
|
|
74
|
+
calls.reads.push(path);
|
|
75
|
+
return new Uint8Array([104, 105]);
|
|
76
|
+
},
|
|
77
|
+
},
|
|
78
|
+
renew: async (timeoutSeconds: number) => {
|
|
79
|
+
calls.renewals.push(timeoutSeconds);
|
|
80
|
+
},
|
|
81
|
+
kill: async () => {
|
|
82
|
+
calls.killed = true;
|
|
83
|
+
},
|
|
84
|
+
close: async () => {
|
|
85
|
+
calls.closed = true;
|
|
86
|
+
},
|
|
87
|
+
} as unknown as Sandbox;
|
|
88
|
+
|
|
89
|
+
return { sandbox, calls };
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
test("execute 在持久 session 中组合 stdout/stderr", async () => {
|
|
93
|
+
const { sandbox, calls } = createFakeSandbox();
|
|
94
|
+
const backend = new OpenSandboxBackend({ sandbox, workspaceRoot: "/workspace" });
|
|
95
|
+
|
|
96
|
+
const result = await backend.execute("echo hello");
|
|
97
|
+
|
|
98
|
+
assert.equal(result.output, "hello\n[stderr] boom");
|
|
99
|
+
assert.equal(result.exitCode, 0);
|
|
100
|
+
assert.equal(result.truncated, false);
|
|
101
|
+
assert.deepEqual(calls.sessions, [{ workingDirectory: "/workspace" }]);
|
|
102
|
+
assert.deepEqual(calls.run, [{ sessionId: "session-1", command: "echo hello" }]);
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
test("连续命令复用同一个 session,并按顺序执行", async () => {
|
|
106
|
+
const { sandbox, calls } = createFakeSandbox();
|
|
107
|
+
const backend = new OpenSandboxBackend({ sandbox });
|
|
108
|
+
|
|
109
|
+
await backend.execute("cd /tmp");
|
|
110
|
+
await backend.execute("pwd");
|
|
111
|
+
await Promise.all([backend.execute("echo a"), backend.execute("echo b")]);
|
|
112
|
+
|
|
113
|
+
assert.equal(calls.sessions.length, 1);
|
|
114
|
+
assert.deepEqual(
|
|
115
|
+
calls.run.map(({ sessionId }) => sessionId),
|
|
116
|
+
["session-1", "session-1", "session-1", "session-1"],
|
|
117
|
+
);
|
|
118
|
+
assert.deepEqual(
|
|
119
|
+
calls.run.map(({ command }) => command),
|
|
120
|
+
["cd /tmp", "pwd", "echo a", "echo b"],
|
|
121
|
+
);
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
test("session 失效时重建并重试一次", async () => {
|
|
125
|
+
const { sandbox, calls } = createFakeSandbox({ failFirstRun: true });
|
|
126
|
+
const backend = new OpenSandboxBackend({ sandbox });
|
|
127
|
+
|
|
128
|
+
const result = await backend.execute("echo hello");
|
|
129
|
+
|
|
130
|
+
assert.equal(result.exitCode, 0);
|
|
131
|
+
assert.equal(calls.sessions.length, 2);
|
|
132
|
+
assert.deepEqual(calls.run, [{ sessionId: "session-2", command: "echo hello" }]);
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
test("renew 向服务端续期 TTL", async () => {
|
|
136
|
+
const { sandbox, calls } = createFakeSandbox();
|
|
137
|
+
const backend = new OpenSandboxBackend({ sandbox });
|
|
138
|
+
|
|
139
|
+
await backend.renew(1800);
|
|
140
|
+
|
|
141
|
+
assert.deepEqual(calls.renewals, [1800]);
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
test("uploadFiles 先创建父目录再写入文件", async () => {
|
|
145
|
+
const { sandbox, calls } = createFakeSandbox();
|
|
146
|
+
const backend = new OpenSandboxBackend({ sandbox });
|
|
147
|
+
|
|
148
|
+
const result = await backend.uploadFiles([
|
|
149
|
+
["/workspace/notes/today.md", new Uint8Array([35, 32])],
|
|
150
|
+
]);
|
|
151
|
+
|
|
152
|
+
assert.deepEqual(result, [{ path: "/workspace/notes/today.md", error: null }]);
|
|
153
|
+
assert.deepEqual(calls.directories, ["/workspace/notes"]);
|
|
154
|
+
assert.deepEqual(calls.writes, ["/workspace/notes/today.md"]);
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
test("downloadFiles 返回文件字节", async () => {
|
|
158
|
+
const { sandbox, calls } = createFakeSandbox();
|
|
159
|
+
const backend = new OpenSandboxBackend({ sandbox });
|
|
160
|
+
|
|
161
|
+
const result = await backend.downloadFiles(["/workspace/notes/today.md"]);
|
|
162
|
+
|
|
163
|
+
assert.deepEqual(calls.reads, ["/workspace/notes/today.md"]);
|
|
164
|
+
assert.equal(result[0]?.error, null);
|
|
165
|
+
assert.deepEqual(result[0]?.content, new Uint8Array([104, 105]));
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
test("destroy 删除 session、终止自建沙箱并关闭连接", async () => {
|
|
169
|
+
const { sandbox, calls } = createFakeSandbox();
|
|
170
|
+
const backend = new OpenSandboxBackend({ sandbox });
|
|
171
|
+
|
|
172
|
+
await backend.execute("echo hello");
|
|
173
|
+
await backend.destroy();
|
|
174
|
+
|
|
175
|
+
assert.deepEqual(calls.deleted, ["session-1"]);
|
|
176
|
+
assert.equal(calls.killed, true);
|
|
177
|
+
assert.equal(calls.closed, true);
|
|
178
|
+
});
|
|
179
|
+
|
|
180
|
+
test("复用已有沙箱时只关闭连接,不 kill", async () => {
|
|
181
|
+
const { sandbox, calls } = createFakeSandbox();
|
|
182
|
+
const backend = new OpenSandboxBackend({ sandbox, owned: false });
|
|
183
|
+
|
|
184
|
+
await backend.destroy();
|
|
185
|
+
|
|
186
|
+
assert.equal(calls.killed, false);
|
|
187
|
+
assert.equal(calls.closed, true);
|
|
188
|
+
});
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import assert from "node:assert/strict";
|
|
2
|
+
import test from "node:test";
|
|
3
|
+
|
|
4
|
+
import type { BaseCheckpointSaver, BaseStore } from "@langchain/langgraph";
|
|
5
|
+
|
|
6
|
+
import type { CompiledAgent } from "../../src/graph/index.js";
|
|
7
|
+
import type { SandboxManager } from "../../src/sandbox/index.js";
|
|
8
|
+
import { SessionService } from "../../src/services/session.js";
|
|
9
|
+
|
|
10
|
+
test("删除会话时释放对应沙箱", async () => {
|
|
11
|
+
const deletedThreads: string[] = [];
|
|
12
|
+
const deletedKeys: Array<[string[], string]> = [];
|
|
13
|
+
const released: string[] = [];
|
|
14
|
+
|
|
15
|
+
const checkpointer = {
|
|
16
|
+
deleteThread: async (threadId: string) => {
|
|
17
|
+
deletedThreads.push(threadId);
|
|
18
|
+
},
|
|
19
|
+
} as unknown as BaseCheckpointSaver;
|
|
20
|
+
|
|
21
|
+
const store = {
|
|
22
|
+
delete: async (key: string[], value: string) => {
|
|
23
|
+
deletedKeys.push([key, value]);
|
|
24
|
+
},
|
|
25
|
+
} as unknown as BaseStore;
|
|
26
|
+
|
|
27
|
+
const sandboxes = {
|
|
28
|
+
release: async (threadId: string) => {
|
|
29
|
+
released.push(threadId);
|
|
30
|
+
},
|
|
31
|
+
} as unknown as SandboxManager;
|
|
32
|
+
|
|
33
|
+
const service = new SessionService({
|
|
34
|
+
graph: {} as CompiledAgent,
|
|
35
|
+
checkpointer,
|
|
36
|
+
store,
|
|
37
|
+
sandboxes,
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
await service.remove("thread-1", "tenant-1");
|
|
41
|
+
|
|
42
|
+
assert.deepEqual(deletedThreads, ["thread-1"]);
|
|
43
|
+
assert.deepEqual(deletedKeys, [[["sessions", "tenant-1"], "thread-1"]]);
|
|
44
|
+
assert.deepEqual(released, ["thread-1"]);
|
|
45
|
+
});
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import assert from "node:assert/strict";
|
|
2
|
+
import test from "node:test";
|
|
3
|
+
|
|
4
|
+
import { assertReadOnlyQuery } from "../../src/tools/database/index.js";
|
|
5
|
+
|
|
6
|
+
test("接受 SELECT 与 WITH 语句", () => {
|
|
7
|
+
assert.doesNotThrow(() => assertReadOnlyQuery("select id, name from users"));
|
|
8
|
+
assert.doesNotThrow(() =>
|
|
9
|
+
assertReadOnlyQuery("with recent as (select * from events) select count(*) from recent"),
|
|
10
|
+
);
|
|
11
|
+
assert.doesNotThrow(() => assertReadOnlyQuery("select 1;"));
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
test("拒绝写入与 DDL", () => {
|
|
15
|
+
for (const sql of [
|
|
16
|
+
"delete from users",
|
|
17
|
+
"update users set admin = true",
|
|
18
|
+
"drop table users",
|
|
19
|
+
"insert into users values (1)",
|
|
20
|
+
]) {
|
|
21
|
+
assert.throws(() => assertReadOnlyQuery(sql));
|
|
22
|
+
}
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
test("拒绝多条语句拼接", () => {
|
|
26
|
+
assert.throws(() => assertReadOnlyQuery("select 1; delete from users"), /只允许单条 SQL 语句/);
|
|
27
|
+
});
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "ES2023",
|
|
4
|
+
"lib": ["ES2023"],
|
|
5
|
+
"module": "NodeNext",
|
|
6
|
+
"moduleResolution": "NodeNext",
|
|
7
|
+
"rootDir": "src",
|
|
8
|
+
"outDir": "dist",
|
|
9
|
+
"strict": true,
|
|
10
|
+
"esModuleInterop": true,
|
|
11
|
+
"skipLibCheck": true,
|
|
12
|
+
"forceConsistentCasingInFileNames": true,
|
|
13
|
+
"resolveJsonModule": true,
|
|
14
|
+
"sourceMap": true,
|
|
15
|
+
"types": ["node"]
|
|
16
|
+
},
|
|
17
|
+
"include": ["src/**/*.ts"],
|
|
18
|
+
"exclude": ["node_modules", "dist"]
|
|
19
|
+
}
|