create-deepagent-server 0.1.0 → 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/README.md +82 -8
- package/package.json +1 -1
- package/src/args.js +6 -4
- package/src/cli.js +1 -1
- package/src/dependencies.js +1 -1
- package/src/prompts.js +1 -0
- package/src/registry.js +8 -8
- package/src/scaffold.js +6 -7
- package/src/setup.js +2 -0
- package/src/version.js +1 -1
- package/src/versions.js +1 -1
- package/templates/deepagent/js/scripts/check.js +34 -11
- package/templates/deepagent/js/scripts/dev.js +9 -0
- package/templates/deepagent/js/src/agents/factory.js +2 -1
- package/templates/deepagent/js/src/agents/prompts/loader.js +9 -0
- package/templates/deepagent/js/src/agents/prompts/supervisor.js +4 -0
- package/templates/deepagent/js/src/agents/prompts/supervisor.md +1 -2
- package/templates/deepagent/js/src/agents/state/types.js +1 -0
- package/templates/deepagent/js/src/agents/subagents/analyst.js +4 -3
- package/templates/deepagent/js/src/agents/subagents/coder.js +1 -0
- package/templates/deepagent/js/src/agents/subagents/index.js +5 -4
- package/templates/deepagent/js/src/agents/supervisor.js +12 -4
- package/templates/deepagent/js/src/api/app.js +7 -0
- package/templates/deepagent/js/src/api/middleware/auth.js +9 -0
- package/templates/deepagent/js/src/api/middleware/error.js +4 -0
- package/templates/deepagent/js/src/api/routes/agent.js +19 -49
- package/templates/deepagent/js/src/api/routes/health.js +12 -3
- package/templates/deepagent/js/src/api/routes/session.js +28 -48
- package/templates/deepagent/js/src/app.js +31 -12
- package/templates/deepagent/js/src/backends/composite.js +2 -0
- package/templates/deepagent/js/src/backends/filesystem.js +1 -0
- package/templates/deepagent/js/src/backends/index.js +35 -3
- package/templates/deepagent/js/src/backends/sandbox.js +3 -0
- package/templates/deepagent/js/src/config/agent.js +1 -0
- package/templates/deepagent/js/src/config/database.js +19 -0
- package/templates/deepagent/js/src/config/env.js +46 -23
- package/templates/deepagent/js/src/config/index.js +13 -3
- package/templates/deepagent/js/src/graph/checkpointer.js +10 -2
- package/templates/deepagent/js/src/graph/compile.js +1 -0
- package/templates/deepagent/js/src/graph/store.js +10 -2
- package/templates/deepagent/js/src/index.js +9 -2
- package/templates/deepagent/js/src/memory/index.js +2 -0
- package/templates/deepagent/js/src/memory/manager.js +21 -10
- package/templates/deepagent/js/src/memory/store.js +7 -0
- package/templates/deepagent/js/src/middleware/approval.js +6 -1
- package/templates/deepagent/js/src/middleware/context.js +5 -0
- package/templates/deepagent/js/src/middleware/logging.js +47 -43
- package/templates/deepagent/js/src/middleware/security.js +7 -0
- package/templates/deepagent/js/src/model/config.js +38 -0
- package/templates/deepagent/js/src/model/index.js +9 -0
- package/templates/deepagent/js/src/model/openai-compatible.js +18 -0
- package/templates/deepagent/js/src/model/presets.js +71 -0
- package/templates/deepagent/js/src/sandbox/config.js +12 -0
- package/templates/deepagent/js/src/sandbox/index.js +3 -0
- package/templates/deepagent/js/src/sandbox/manager.js +161 -0
- package/templates/deepagent/js/src/sandbox/opensandbox.js +195 -0
- package/templates/deepagent/js/src/schemas/agent.js +7 -0
- package/templates/deepagent/js/src/schemas/common.js +5 -0
- package/templates/deepagent/js/src/schemas/locale.js +1 -0
- package/templates/deepagent/js/src/schemas/session.js +4 -0
- package/templates/deepagent/js/src/services/{agent.service.js → agent.js} +39 -20
- package/templates/deepagent/js/src/services/{memory.service.js → memory.js} +13 -7
- package/templates/deepagent/js/src/services/{session.service.js → session.js} +43 -17
- package/templates/deepagent/js/src/skills/index.js +4 -0
- package/templates/deepagent/js/src/skills/loader.js +20 -10
- package/templates/deepagent/js/src/storage/database/client.js +57 -0
- package/templates/deepagent/{ts/src/infrastructure → js/src/storage}/database/migrations/001_init.sql +1 -1
- package/templates/deepagent/js/src/{infrastructure → storage}/logger/index.js +2 -0
- package/templates/deepagent/js/src/{infrastructure → storage}/redis/client.js +25 -19
- package/templates/deepagent/js/src/{infrastructure → storage}/shutdown/index.js +20 -13
- package/templates/deepagent/js/src/tools/database/index.js +1 -1
- package/templates/deepagent/js/src/tools/database/query.js +44 -32
- package/templates/deepagent/js/src/tools/filesystem/index.js +16 -0
- package/templates/deepagent/js/src/tools/index.js +6 -11
- package/templates/deepagent/js/src/tools/mcp/client.js +7 -0
- package/templates/deepagent/js/src/tools/mcp/connections.js +13 -3
- package/templates/deepagent/js/src/tools/mcp/index.js +2 -0
- package/templates/deepagent/js/src/tools/mcp/registry.js +15 -11
- package/templates/deepagent/js/src/utils/errors.js +9 -0
- package/templates/deepagent/js/src/utils/helpers.js +15 -0
- package/templates/deepagent/js/src/utils/validation.js +15 -0
- package/templates/deepagent/js/tests/agents/prompts.test.js +9 -3
- package/templates/deepagent/js/tests/agents/supervisor.test.js +13 -5
- package/templates/deepagent/js/tests/config/database.test.js +40 -0
- package/templates/deepagent/js/tests/integration/api.test.js +18 -3
- package/templates/deepagent/js/tests/middleware/security.test.js +4 -1
- package/templates/deepagent/js/tests/model/config.test.js +61 -0
- package/templates/deepagent/js/tests/sandbox/config.test.js +46 -0
- package/templates/deepagent/js/tests/sandbox/manager.test.js +236 -0
- package/templates/deepagent/js/tests/sandbox/opensandbox.test.js +173 -0
- package/templates/deepagent/js/tests/services/session.test.js +41 -0
- package/templates/deepagent/js/tests/tools/query.test.js +5 -1
- package/templates/deepagent/shared/README.md +101 -14
- package/templates/deepagent/shared/_env.example +40 -10
- package/templates/deepagent/shared/deployment/docker-compose.yml +8 -4
- package/templates/deepagent/shared/resources/AGENTS.md +1 -1
- package/templates/deepagent/ts/scripts/check.ts +19 -11
- package/templates/deepagent/ts/scripts/copy-assets.ts +2 -0
- package/templates/deepagent/ts/src/agents/factory.ts +1 -1
- package/templates/deepagent/ts/src/agents/prompts/loader.ts +1 -0
- package/templates/deepagent/ts/src/agents/prompts/supervisor.md +1 -2
- package/templates/deepagent/ts/src/agents/prompts/supervisor.ts +1 -1
- package/templates/deepagent/ts/src/agents/subagents/analyst.ts +7 -3
- package/templates/deepagent/ts/src/agents/subagents/index.ts +4 -5
- package/templates/deepagent/ts/src/agents/supervisor.ts +11 -9
- package/templates/deepagent/ts/src/api/app.ts +4 -4
- package/templates/deepagent/ts/src/api/middleware/auth.ts +1 -0
- package/templates/deepagent/ts/src/api/middleware/error.ts +1 -1
- package/templates/deepagent/ts/src/api/routes/agent.ts +13 -58
- package/templates/deepagent/ts/src/api/routes/health.ts +5 -3
- package/templates/deepagent/ts/src/api/routes/session.ts +21 -51
- package/templates/deepagent/ts/src/app.ts +19 -12
- package/templates/deepagent/ts/src/backends/index.ts +41 -7
- package/templates/deepagent/ts/src/backends/sandbox.ts +1 -0
- package/templates/deepagent/ts/src/config/database.ts +21 -0
- package/templates/deepagent/ts/src/config/env.ts +32 -28
- package/templates/deepagent/ts/src/config/index.ts +12 -4
- package/templates/deepagent/ts/src/graph/checkpointer.ts +5 -2
- package/templates/deepagent/ts/src/graph/store.ts +5 -2
- package/templates/deepagent/ts/src/index.ts +2 -2
- package/templates/deepagent/ts/src/memory/manager.ts +12 -10
- package/templates/deepagent/ts/src/memory/store.ts +2 -0
- package/templates/deepagent/ts/src/middleware/approval.ts +2 -2
- package/templates/deepagent/ts/src/middleware/logging.ts +43 -44
- package/templates/deepagent/ts/src/middleware/security.ts +1 -0
- package/templates/deepagent/ts/src/model/config.ts +56 -0
- package/templates/deepagent/ts/src/model/index.ts +14 -0
- package/templates/deepagent/ts/src/model/openai-compatible.ts +21 -0
- package/templates/deepagent/ts/src/model/presets.ts +82 -0
- package/templates/deepagent/ts/src/sandbox/config.ts +30 -0
- package/templates/deepagent/ts/src/sandbox/index.ts +7 -0
- package/templates/deepagent/ts/src/sandbox/manager.ts +176 -0
- package/templates/deepagent/ts/src/sandbox/opensandbox.ts +216 -0
- package/templates/deepagent/ts/src/services/{agent.service.ts → agent.ts} +22 -22
- package/templates/deepagent/ts/src/services/{memory.service.ts → memory.ts} +7 -7
- package/templates/deepagent/ts/src/services/{session.service.ts → session.ts} +29 -19
- package/templates/deepagent/ts/src/skills/loader.ts +6 -10
- package/templates/deepagent/ts/src/storage/database/client.ts +65 -0
- package/templates/deepagent/{js/src/infrastructure → ts/src/storage}/database/migrations/001_init.sql +1 -1
- package/templates/deepagent/ts/src/{infrastructure → storage}/redis/client.ts +14 -21
- package/templates/deepagent/ts/src/{infrastructure → storage}/shutdown/index.ts +10 -13
- package/templates/deepagent/ts/src/tools/database/index.ts +5 -1
- package/templates/deepagent/ts/src/tools/database/query.ts +40 -32
- package/templates/deepagent/ts/src/tools/filesystem/index.ts +2 -0
- package/templates/deepagent/ts/src/tools/index.ts +10 -8
- package/templates/deepagent/ts/src/tools/mcp/client.ts +1 -0
- package/templates/deepagent/ts/src/tools/mcp/connections.ts +3 -3
- package/templates/deepagent/ts/src/tools/mcp/registry.ts +9 -12
- package/templates/deepagent/ts/src/utils/helpers.ts +5 -0
- package/templates/deepagent/ts/src/utils/validation.ts +17 -0
- package/templates/deepagent/ts/tests/agents/prompts.test.ts +3 -3
- package/templates/deepagent/ts/tests/agents/supervisor.test.ts +5 -5
- package/templates/deepagent/ts/tests/config/database.test.ts +40 -0
- package/templates/deepagent/ts/tests/integration/api.test.ts +5 -6
- package/templates/deepagent/ts/tests/middleware/security.test.ts +1 -1
- package/templates/deepagent/ts/tests/model/config.test.ts +61 -0
- package/templates/deepagent/ts/tests/sandbox/config.test.ts +46 -0
- package/templates/deepagent/ts/tests/sandbox/manager.test.ts +241 -0
- package/templates/deepagent/ts/tests/sandbox/opensandbox.test.ts +188 -0
- package/templates/deepagent/ts/tests/services/session.test.ts +45 -0
- package/templates/deepagent/ts/tests/tools/query.test.ts +1 -1
- package/templates/deepagent/js/src/agents/prompts/subagents/researcher.md +0 -16
- package/templates/deepagent/js/src/agents/subagents/researcher.js +0 -12
- package/templates/deepagent/js/src/config/model.js +0 -34
- package/templates/deepagent/js/src/infrastructure/database/client.js +0 -43
- package/templates/deepagent/js/src/tools/search/index.js +0 -1
- package/templates/deepagent/js/src/tools/search/web-search.js +0 -54
- package/templates/deepagent/shared/skills/research/SKILL.md +0 -31
- package/templates/deepagent/ts/src/agents/prompts/subagents/researcher.md +0 -16
- package/templates/deepagent/ts/src/agents/subagents/researcher.ts +0 -15
- package/templates/deepagent/ts/src/config/model.ts +0 -49
- package/templates/deepagent/ts/src/infrastructure/database/client.ts +0 -61
- package/templates/deepagent/ts/src/tools/search/index.ts +0 -1
- package/templates/deepagent/ts/src/tools/search/web-search.ts +0 -82
- /package/templates/deepagent/ts/src/{infrastructure → storage}/logger/index.ts +0 -0
|
@@ -0,0 +1,236 @@
|
|
|
1
|
+
import assert from "node:assert/strict";
|
|
2
|
+
import test from "node:test";
|
|
3
|
+
|
|
4
|
+
import { SandboxManager, threadIdFromRuntime } from "../../src/sandbox/index.js";
|
|
5
|
+
|
|
6
|
+
const sandboxConfig = (overrides = {}) => ({
|
|
7
|
+
enabled: true,
|
|
8
|
+
domain: "localhost:8080",
|
|
9
|
+
protocol: "http",
|
|
10
|
+
image: "python:3.12",
|
|
11
|
+
workspaceRoot: "/workspace",
|
|
12
|
+
...overrides,
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
const createFactory = () => {
|
|
16
|
+
const created = [];
|
|
17
|
+
const destroyed = [];
|
|
18
|
+
const renewed = [];
|
|
19
|
+
let count = 0;
|
|
20
|
+
|
|
21
|
+
const create = async () => {
|
|
22
|
+
count += 1;
|
|
23
|
+
const id = `sbx-${count}`;
|
|
24
|
+
created.push(id);
|
|
25
|
+
|
|
26
|
+
return {
|
|
27
|
+
id,
|
|
28
|
+
renew: async (timeoutSeconds) => {
|
|
29
|
+
renewed.push(timeoutSeconds);
|
|
30
|
+
},
|
|
31
|
+
destroy: async () => {
|
|
32
|
+
destroyed.push(id);
|
|
33
|
+
},
|
|
34
|
+
};
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
return { create, created, destroyed, renewed };
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
test("同一会话复用同一个沙箱", async () => {
|
|
41
|
+
const factory = createFactory();
|
|
42
|
+
const manager = new SandboxManager(sandboxConfig(), { create: factory.create });
|
|
43
|
+
|
|
44
|
+
const first = await manager.get("thread-a");
|
|
45
|
+
const second = await manager.get("thread-a");
|
|
46
|
+
|
|
47
|
+
assert.equal(first, second);
|
|
48
|
+
assert.deepEqual(factory.created, ["sbx-1"]);
|
|
49
|
+
assert.equal(manager.size, 1);
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
test("不同会话使用不同的沙箱", async () => {
|
|
53
|
+
const factory = createFactory();
|
|
54
|
+
const manager = new SandboxManager(sandboxConfig(), { create: factory.create });
|
|
55
|
+
|
|
56
|
+
const first = await manager.get("thread-a");
|
|
57
|
+
const second = await manager.get("thread-b");
|
|
58
|
+
|
|
59
|
+
assert.notEqual(first, second);
|
|
60
|
+
assert.deepEqual(factory.created, ["sbx-1", "sbx-2"]);
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
test("释放会话后再次使用时创建新沙箱", async () => {
|
|
64
|
+
const factory = createFactory();
|
|
65
|
+
const manager = new SandboxManager(sandboxConfig(), { create: factory.create });
|
|
66
|
+
|
|
67
|
+
await manager.get("thread-a");
|
|
68
|
+
await manager.release("thread-a");
|
|
69
|
+
|
|
70
|
+
assert.deepEqual(factory.destroyed, ["sbx-1"]);
|
|
71
|
+
assert.equal(manager.size, 0);
|
|
72
|
+
|
|
73
|
+
const next = await manager.get("thread-a");
|
|
74
|
+
assert.equal(next.id, "sbx-2");
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
test("closeAll 销毁所有会话的沙箱", async () => {
|
|
78
|
+
const factory = createFactory();
|
|
79
|
+
const manager = new SandboxManager(sandboxConfig(), { create: factory.create });
|
|
80
|
+
|
|
81
|
+
await manager.get("thread-a");
|
|
82
|
+
await manager.get("thread-b");
|
|
83
|
+
await manager.closeAll();
|
|
84
|
+
|
|
85
|
+
assert.deepEqual(factory.destroyed.sort(), ["sbx-1", "sbx-2"]);
|
|
86
|
+
assert.equal(manager.size, 0);
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
test("创建失败后清理缓存,下一次可重试", async () => {
|
|
90
|
+
const factory = createFactory();
|
|
91
|
+
let attempts = 0;
|
|
92
|
+
|
|
93
|
+
const create = async () => {
|
|
94
|
+
attempts += 1;
|
|
95
|
+
if (attempts === 1) throw new Error("sandbox server unavailable");
|
|
96
|
+
|
|
97
|
+
return factory.create();
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
const manager = new SandboxManager(sandboxConfig(), { create });
|
|
101
|
+
|
|
102
|
+
await assert.rejects(() => manager.get("thread-a"), /unavailable/);
|
|
103
|
+
assert.equal(manager.size, 0);
|
|
104
|
+
|
|
105
|
+
const backend = await manager.get("thread-a");
|
|
106
|
+
assert.equal(backend.id, "sbx-1");
|
|
107
|
+
assert.equal(attempts, 2);
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
test("未配置 TTL 时不续期", async () => {
|
|
111
|
+
let now = 0;
|
|
112
|
+
const factory = createFactory();
|
|
113
|
+
const manager = new SandboxManager(sandboxConfig(), { create: factory.create, now: () => now });
|
|
114
|
+
|
|
115
|
+
await manager.get("thread-a");
|
|
116
|
+
now += 24 * 60 * 60 * 1_000;
|
|
117
|
+
await manager.get("thread-a");
|
|
118
|
+
|
|
119
|
+
assert.deepEqual(factory.renewed, []);
|
|
120
|
+
assert.equal(factory.created.length, 1);
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
test("使用中按 TTL 的三分之一续期", async () => {
|
|
124
|
+
let now = 0;
|
|
125
|
+
const factory = createFactory();
|
|
126
|
+
const manager = new SandboxManager(sandboxConfig({ timeoutSeconds: 1800 }), {
|
|
127
|
+
create: factory.create,
|
|
128
|
+
now: () => now,
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
const first = await manager.get("thread-a");
|
|
132
|
+
|
|
133
|
+
assert.equal(await manager.get("thread-a"), first);
|
|
134
|
+
assert.deepEqual(factory.renewed, []);
|
|
135
|
+
|
|
136
|
+
now += 600_000;
|
|
137
|
+
assert.equal(await manager.get("thread-a"), first);
|
|
138
|
+
assert.deepEqual(factory.renewed, [1800]);
|
|
139
|
+
assert.equal(factory.created.length, 1);
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
test("续期发现沙箱已被回收时重建", async () => {
|
|
143
|
+
let now = 0;
|
|
144
|
+
let count = 0;
|
|
145
|
+
const destroyed = [];
|
|
146
|
+
|
|
147
|
+
const create = async () => {
|
|
148
|
+
count += 1;
|
|
149
|
+
const id = `sbx-${count}`;
|
|
150
|
+
|
|
151
|
+
return {
|
|
152
|
+
id,
|
|
153
|
+
renew: async () => {
|
|
154
|
+
if (id === "sbx-1") throw new Error("sandbox not found");
|
|
155
|
+
},
|
|
156
|
+
destroy: async () => {
|
|
157
|
+
destroyed.push(id);
|
|
158
|
+
},
|
|
159
|
+
};
|
|
160
|
+
};
|
|
161
|
+
|
|
162
|
+
const manager = new SandboxManager(sandboxConfig({ timeoutSeconds: 1800 }), {
|
|
163
|
+
create,
|
|
164
|
+
now: () => now,
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
const first = await manager.get("thread-a");
|
|
168
|
+
|
|
169
|
+
now += 600_000;
|
|
170
|
+
const second = await manager.get("thread-a");
|
|
171
|
+
|
|
172
|
+
assert.notEqual(first, second);
|
|
173
|
+
assert.equal(second.id, "sbx-2");
|
|
174
|
+
assert.deepEqual(destroyed, ["sbx-1"]);
|
|
175
|
+
assert.equal(manager.size, 1);
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
test("续期失败但沙箱仍在时继续复用", async () => {
|
|
179
|
+
let now = 0;
|
|
180
|
+
let renewals = 0;
|
|
181
|
+
let count = 0;
|
|
182
|
+
|
|
183
|
+
const create = async () => {
|
|
184
|
+
count += 1;
|
|
185
|
+
|
|
186
|
+
return {
|
|
187
|
+
id: `sbx-${count}`,
|
|
188
|
+
renew: async () => {
|
|
189
|
+
renewals += 1;
|
|
190
|
+
throw new Error("upstream timeout");
|
|
191
|
+
},
|
|
192
|
+
destroy: async () => {},
|
|
193
|
+
};
|
|
194
|
+
};
|
|
195
|
+
|
|
196
|
+
const manager = new SandboxManager(sandboxConfig({ timeoutSeconds: 1800 }), {
|
|
197
|
+
create,
|
|
198
|
+
now: () => now,
|
|
199
|
+
});
|
|
200
|
+
|
|
201
|
+
const first = await manager.get("thread-a");
|
|
202
|
+
|
|
203
|
+
now += 600_000;
|
|
204
|
+
assert.equal(await manager.get("thread-a"), first);
|
|
205
|
+
assert.equal(renewals, 1);
|
|
206
|
+
assert.equal(count, 1);
|
|
207
|
+
});
|
|
208
|
+
|
|
209
|
+
test("设置 OPEN_SANDBOX_ID 时所有会话共享同一个沙箱", async () => {
|
|
210
|
+
const factory = createFactory();
|
|
211
|
+
const manager = new SandboxManager(sandboxConfig({ sandboxId: "sbx-existing" }), {
|
|
212
|
+
create: factory.create,
|
|
213
|
+
});
|
|
214
|
+
|
|
215
|
+
const first = await manager.get("thread-a");
|
|
216
|
+
const second = await manager.get("thread-b");
|
|
217
|
+
|
|
218
|
+
assert.equal(first, second);
|
|
219
|
+
assert.deepEqual(factory.created, ["sbx-1"]);
|
|
220
|
+
|
|
221
|
+
await manager.release("thread-a");
|
|
222
|
+
assert.deepEqual(factory.destroyed, []);
|
|
223
|
+
|
|
224
|
+
await manager.closeAll();
|
|
225
|
+
assert.deepEqual(factory.destroyed, ["sbx-1"]);
|
|
226
|
+
});
|
|
227
|
+
|
|
228
|
+
test("threadIdFromRuntime 支持 context 与 configurable", () => {
|
|
229
|
+
assert.equal(threadIdFromRuntime({ context: { threadId: "from-context" } }), "from-context");
|
|
230
|
+
assert.equal(
|
|
231
|
+
threadIdFromRuntime({ config: { configurable: { thread_id: "from-config" } } }),
|
|
232
|
+
"from-config",
|
|
233
|
+
);
|
|
234
|
+
assert.equal(threadIdFromRuntime({ state: {} }), undefined);
|
|
235
|
+
assert.equal(threadIdFromRuntime(undefined), undefined);
|
|
236
|
+
});
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
import assert from "node:assert/strict";
|
|
2
|
+
import test from "node:test";
|
|
3
|
+
|
|
4
|
+
import { SandboxApiException } from "@alibaba-group/opensandbox";
|
|
5
|
+
|
|
6
|
+
import { OpenSandboxBackend } from "../../src/sandbox/index.js";
|
|
7
|
+
|
|
8
|
+
const createFakeSandbox = ({ failFirstRun = false } = {}) => {
|
|
9
|
+
const calls = {
|
|
10
|
+
sessions: [],
|
|
11
|
+
run: [],
|
|
12
|
+
deleted: [],
|
|
13
|
+
directories: [],
|
|
14
|
+
writes: [],
|
|
15
|
+
reads: [],
|
|
16
|
+
renewals: [],
|
|
17
|
+
killed: false,
|
|
18
|
+
closed: false,
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
let created = 0;
|
|
22
|
+
let remainingFailures = failFirstRun ? 1 : 0;
|
|
23
|
+
|
|
24
|
+
const sandbox = {
|
|
25
|
+
id: "sbx-test",
|
|
26
|
+
commands: {
|
|
27
|
+
createSession: async (options) => {
|
|
28
|
+
calls.sessions.push({ workingDirectory: options?.workingDirectory });
|
|
29
|
+
created += 1;
|
|
30
|
+
return `session-${created}`;
|
|
31
|
+
},
|
|
32
|
+
runInSession: async (sessionId, command) => {
|
|
33
|
+
if (remainingFailures > 0) {
|
|
34
|
+
remainingFailures -= 1;
|
|
35
|
+
throw new SandboxApiException({ statusCode: 404, message: "session not found" });
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
calls.run.push({ sessionId, command });
|
|
39
|
+
return {
|
|
40
|
+
logs: {
|
|
41
|
+
stdout: [{ text: "hello", timestamp: 0 }],
|
|
42
|
+
stderr: [{ text: "boom", timestamp: 0 }],
|
|
43
|
+
},
|
|
44
|
+
exitCode: 0,
|
|
45
|
+
};
|
|
46
|
+
},
|
|
47
|
+
deleteSession: async (sessionId) => {
|
|
48
|
+
calls.deleted.push(sessionId);
|
|
49
|
+
},
|
|
50
|
+
},
|
|
51
|
+
files: {
|
|
52
|
+
createDirectories: async (entries) => {
|
|
53
|
+
calls.directories.push(...entries.map((entry) => entry.path));
|
|
54
|
+
},
|
|
55
|
+
writeFiles: async (entries) => {
|
|
56
|
+
calls.writes.push(...entries.map((entry) => entry.path));
|
|
57
|
+
},
|
|
58
|
+
readBytes: async (path) => {
|
|
59
|
+
calls.reads.push(path);
|
|
60
|
+
return new Uint8Array([104, 105]);
|
|
61
|
+
},
|
|
62
|
+
},
|
|
63
|
+
renew: async (timeoutSeconds) => {
|
|
64
|
+
calls.renewals.push(timeoutSeconds);
|
|
65
|
+
},
|
|
66
|
+
kill: async () => {
|
|
67
|
+
calls.killed = true;
|
|
68
|
+
},
|
|
69
|
+
close: async () => {
|
|
70
|
+
calls.closed = true;
|
|
71
|
+
},
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
return { sandbox, calls };
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
test("execute 在持久 session 中组合 stdout/stderr", async () => {
|
|
78
|
+
const { sandbox, calls } = createFakeSandbox();
|
|
79
|
+
const backend = new OpenSandboxBackend({ sandbox, workspaceRoot: "/workspace" });
|
|
80
|
+
|
|
81
|
+
const result = await backend.execute("echo hello");
|
|
82
|
+
|
|
83
|
+
assert.equal(result.output, "hello\n[stderr] boom");
|
|
84
|
+
assert.equal(result.exitCode, 0);
|
|
85
|
+
assert.equal(result.truncated, false);
|
|
86
|
+
assert.deepEqual(calls.sessions, [{ workingDirectory: "/workspace" }]);
|
|
87
|
+
assert.deepEqual(calls.run, [{ sessionId: "session-1", command: "echo hello" }]);
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
test("连续命令复用同一个 session,并按顺序执行", async () => {
|
|
91
|
+
const { sandbox, calls } = createFakeSandbox();
|
|
92
|
+
const backend = new OpenSandboxBackend({ sandbox });
|
|
93
|
+
|
|
94
|
+
await backend.execute("cd /tmp");
|
|
95
|
+
await backend.execute("pwd");
|
|
96
|
+
await Promise.all([backend.execute("echo a"), backend.execute("echo b")]);
|
|
97
|
+
|
|
98
|
+
assert.equal(calls.sessions.length, 1);
|
|
99
|
+
assert.deepEqual(
|
|
100
|
+
calls.run.map(({ sessionId }) => sessionId),
|
|
101
|
+
["session-1", "session-1", "session-1", "session-1"],
|
|
102
|
+
);
|
|
103
|
+
assert.deepEqual(
|
|
104
|
+
calls.run.map(({ command }) => command),
|
|
105
|
+
["cd /tmp", "pwd", "echo a", "echo b"],
|
|
106
|
+
);
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
test("session 失效时重建并重试一次", async () => {
|
|
110
|
+
const { sandbox, calls } = createFakeSandbox({ failFirstRun: true });
|
|
111
|
+
const backend = new OpenSandboxBackend({ sandbox });
|
|
112
|
+
|
|
113
|
+
const result = await backend.execute("echo hello");
|
|
114
|
+
|
|
115
|
+
assert.equal(result.exitCode, 0);
|
|
116
|
+
assert.equal(calls.sessions.length, 2);
|
|
117
|
+
assert.deepEqual(calls.run, [{ sessionId: "session-2", command: "echo hello" }]);
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
test("renew 向服务端续期 TTL", async () => {
|
|
121
|
+
const { sandbox, calls } = createFakeSandbox();
|
|
122
|
+
const backend = new OpenSandboxBackend({ sandbox });
|
|
123
|
+
|
|
124
|
+
await backend.renew(1800);
|
|
125
|
+
|
|
126
|
+
assert.deepEqual(calls.renewals, [1800]);
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
test("uploadFiles 先创建父目录再写入文件", async () => {
|
|
130
|
+
const { sandbox, calls } = createFakeSandbox();
|
|
131
|
+
const backend = new OpenSandboxBackend({ sandbox });
|
|
132
|
+
|
|
133
|
+
const result = await backend.uploadFiles([
|
|
134
|
+
["/workspace/notes/today.md", new Uint8Array([35, 32])],
|
|
135
|
+
]);
|
|
136
|
+
|
|
137
|
+
assert.deepEqual(result, [{ path: "/workspace/notes/today.md", error: null }]);
|
|
138
|
+
assert.deepEqual(calls.directories, ["/workspace/notes"]);
|
|
139
|
+
assert.deepEqual(calls.writes, ["/workspace/notes/today.md"]);
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
test("downloadFiles 返回文件字节", async () => {
|
|
143
|
+
const { sandbox, calls } = createFakeSandbox();
|
|
144
|
+
const backend = new OpenSandboxBackend({ sandbox });
|
|
145
|
+
|
|
146
|
+
const result = await backend.downloadFiles(["/workspace/notes/today.md"]);
|
|
147
|
+
|
|
148
|
+
assert.deepEqual(calls.reads, ["/workspace/notes/today.md"]);
|
|
149
|
+
assert.equal(result[0]?.error, null);
|
|
150
|
+
assert.deepEqual(result[0]?.content, new Uint8Array([104, 105]));
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
test("destroy 删除 session、终止自建沙箱并关闭连接", async () => {
|
|
154
|
+
const { sandbox, calls } = createFakeSandbox();
|
|
155
|
+
const backend = new OpenSandboxBackend({ sandbox });
|
|
156
|
+
|
|
157
|
+
await backend.execute("echo hello");
|
|
158
|
+
await backend.destroy();
|
|
159
|
+
|
|
160
|
+
assert.deepEqual(calls.deleted, ["session-1"]);
|
|
161
|
+
assert.equal(calls.killed, true);
|
|
162
|
+
assert.equal(calls.closed, true);
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
test("复用已有沙箱时只关闭连接,不 kill", async () => {
|
|
166
|
+
const { sandbox, calls } = createFakeSandbox();
|
|
167
|
+
const backend = new OpenSandboxBackend({ sandbox, owned: false });
|
|
168
|
+
|
|
169
|
+
await backend.destroy();
|
|
170
|
+
|
|
171
|
+
assert.equal(calls.killed, false);
|
|
172
|
+
assert.equal(calls.closed, true);
|
|
173
|
+
});
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import assert from "node:assert/strict";
|
|
2
|
+
import test from "node:test";
|
|
3
|
+
|
|
4
|
+
import { SessionService } from "../../src/services/session.js";
|
|
5
|
+
|
|
6
|
+
test("删除会话时释放对应沙箱", async () => {
|
|
7
|
+
const deletedThreads = [];
|
|
8
|
+
const deletedKeys = [];
|
|
9
|
+
const released = [];
|
|
10
|
+
|
|
11
|
+
const checkpointer = {
|
|
12
|
+
deleteThread: async (threadId) => {
|
|
13
|
+
deletedThreads.push(threadId);
|
|
14
|
+
},
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
const store = {
|
|
18
|
+
delete: async (key, value) => {
|
|
19
|
+
deletedKeys.push([key, value]);
|
|
20
|
+
},
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
const sandboxes = {
|
|
24
|
+
release: async (threadId) => {
|
|
25
|
+
released.push(threadId);
|
|
26
|
+
},
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
const service = new SessionService({
|
|
30
|
+
graph: {},
|
|
31
|
+
checkpointer,
|
|
32
|
+
store,
|
|
33
|
+
sandboxes,
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
await service.remove("thread-1", "tenant-1");
|
|
37
|
+
|
|
38
|
+
assert.deepEqual(deletedThreads, ["thread-1"]);
|
|
39
|
+
assert.deepEqual(deletedKeys, [[["sessions", "tenant-1"], "thread-1"]]);
|
|
40
|
+
assert.deepEqual(released, ["thread-1"]);
|
|
41
|
+
});
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import assert from "node:assert/strict";
|
|
2
2
|
import test from "node:test";
|
|
3
|
-
|
|
3
|
+
|
|
4
|
+
import { assertReadOnlyQuery } from "../../src/tools/database/index.js";
|
|
5
|
+
|
|
4
6
|
test("接受 SELECT 与 WITH 语句", () => {
|
|
5
7
|
assert.doesNotThrow(() => assertReadOnlyQuery("select id, name from users"));
|
|
6
8
|
assert.doesNotThrow(() =>
|
|
@@ -8,6 +10,7 @@ test("接受 SELECT 与 WITH 语句", () => {
|
|
|
8
10
|
);
|
|
9
11
|
assert.doesNotThrow(() => assertReadOnlyQuery("select 1;"));
|
|
10
12
|
});
|
|
13
|
+
|
|
11
14
|
test("拒绝写入与 DDL", () => {
|
|
12
15
|
for (const sql of [
|
|
13
16
|
"delete from users",
|
|
@@ -18,6 +21,7 @@ test("拒绝写入与 DDL", () => {
|
|
|
18
21
|
assert.throws(() => assertReadOnlyQuery(sql));
|
|
19
22
|
}
|
|
20
23
|
});
|
|
24
|
+
|
|
21
25
|
test("拒绝多条语句拼接", () => {
|
|
22
26
|
assert.throws(() => assertReadOnlyQuery("select 1; delete from users"), /只允许单条 SQL 语句/);
|
|
23
27
|
});
|
|
@@ -4,13 +4,15 @@
|
|
|
4
4
|
|
|
5
5
|
- 仅使用 ESM、现代 ES6+ 语法,Node.js 22.6+
|
|
6
6
|
- 默认 JavaScript,可选 TypeScript
|
|
7
|
+
- 模型统一走 OpenAI 兼容协议,默认 MiniMax M3;改环境变量即可切换 OpenAI / DeepSeek / Kimi / MiniMax / 通义千问 / 智谱 / OpenRouter / vLLM / Ollama
|
|
8
|
+
- 可选 OpenSandbox 远程沙箱:`SANDBOX_ENABLED=true` 时 `execute` 与 `/workspace/` 都落在隔离环境中
|
|
7
9
|
- LangGraph checkpointer + store(默认内存,可切换 PostgreSQL)
|
|
8
10
|
- Express 5 API:Bearer 鉴权、SSE 流式输出、结构化日志
|
|
9
11
|
|
|
10
12
|
## 快速开始
|
|
11
13
|
|
|
12
14
|
```bash
|
|
13
|
-
cp .env.example .env # 填入
|
|
15
|
+
cp .env.example .env # 填入 MODEL_API_KEY(示例默认 MiniMax M3)
|
|
14
16
|
npm run check # 可选:启动前环境检查
|
|
15
17
|
npm run dev # http://localhost:3000/health
|
|
16
18
|
```
|
|
@@ -47,18 +49,20 @@ curl -X POST http://localhost:3000/api/agents/invoke \
|
|
|
47
49
|
src/
|
|
48
50
|
├── index.ts|js 入口:配置 → 应用 → HTTP 服务
|
|
49
51
|
├── app.ts 应用装配(测试和脚本复用同一套装配)
|
|
50
|
-
├── config/
|
|
52
|
+
├── config/ 环境变量与 Agent 配置
|
|
53
|
+
├── model/ 模型配置:OpenAI 兼容接入、服务商预设
|
|
51
54
|
├── agents/ 主 Agent、子 Agent、提示词、自定义状态
|
|
52
|
-
├── tools/
|
|
55
|
+
├── tools/ 只读 SQL、工作区文件、MCP
|
|
53
56
|
├── middleware/ 日志、安全、审批、运行时上下文
|
|
54
57
|
├── backends/ 文件系统 / 沙箱 / 组合后端
|
|
58
|
+
├── sandbox/ OpenSandbox 远程沙箱(可选执行后端)
|
|
55
59
|
├── memory/ 长期记忆(BaseStore)封装
|
|
56
60
|
├── skills/ SKILL.md 发现与校验
|
|
57
61
|
├── graph/ createDeepAgent 编译、checkpointer、store
|
|
58
62
|
├── services/ Agent、会话、记忆服务
|
|
59
63
|
├── api/ Express 应用、路由与中间件
|
|
60
64
|
├── schemas/ Zod 请求/响应结构
|
|
61
|
-
├──
|
|
65
|
+
├── storage/ PostgreSQL、Redis、pino 日志、优雅关闭
|
|
62
66
|
└── utils/
|
|
63
67
|
resources/ AGENTS.md + 知识库,挂载到 /resources/
|
|
64
68
|
skills/ SKILL.md,挂载到 /skills/
|
|
@@ -72,9 +76,9 @@ deployment/ Dockerfile + docker-compose
|
|
|
72
76
|
|
|
73
77
|
`src/app.ts` 通过 `createSupervisorAgent` 构建一个主 Agent:
|
|
74
78
|
|
|
75
|
-
- **模型** —— `src/
|
|
76
|
-
- **后端** —— `src/backends/composite.ts` 把 `/workspace/`、`/resources/`、`/skills/` 挂载到磁盘,把 `/memories/` 挂载到 LangGraph store。`ENABLE_SHELL=true` 时默认后端切换为 `LocalShellBackend` 并暴露 `execute
|
|
77
|
-
- **子 Agent** —— `
|
|
79
|
+
- **模型** —— `src/model/`:`resolveModelConfig()` 解析环境变量,`createChatModel()` 用 `ChatOpenAI` 指向任意 OpenAI 兼容地址,整个进程共用一个实例。服务商预设见 `src/model/presets`。
|
|
80
|
+
- **后端** —— `src/backends/composite.ts` 把 `/workspace/`、`/resources/`、`/skills/` 挂载到磁盘,把 `/memories/` 挂载到 LangGraph store。`ENABLE_SHELL=true` 时默认后端切换为 `LocalShellBackend` 并暴露 `execute`;`SANDBOX_ENABLED=true` 时后端按 `threadId` 切换为对应会话的 OpenSandbox,`/workspace/` 与 `execute` 都落在该会话的远程沙箱内。
|
|
81
|
+
- **子 Agent** —— `coder`(工作区改代码)、`analyst`(只读 SQL)。它们都是声明式 `SubAgent`,各自拥有独立上下文窗口。
|
|
78
82
|
- **提示词** —— `src/agents/prompts/` 下的纯 Markdown。`loadPrompt()` 读取文件,`renderPrompt()` 填充 `{{占位符}}`;TypeScript 项目构建时会把 `.md` 复制到 `dist/`。提示词在 Agent 构建时读取,修改后需重启 dev server。
|
|
79
83
|
- **中间件** —— 调用日志、危险工具拦截、按请求注入租户/用户上下文。
|
|
80
84
|
- **审批** —— `APPROVAL_MODE=sensitive`(默认)会通过 `interruptOn` 暂停有副作用的工具。中断响应包含 `interrupts`,用同一个 `threadId` 调用 `POST /api/agents/resume` 批准后继续。
|
|
@@ -119,24 +123,75 @@ curl -N -X POST http://localhost:3000/api/agents/stream \
|
|
|
119
123
|
|
|
120
124
|
| 变量 | 默认值 | 说明 |
|
|
121
125
|
| --- | --- | --- |
|
|
122
|
-
| `MODEL_PROVIDER` | `
|
|
123
|
-
| `MODEL_NAME` |
|
|
126
|
+
| `MODEL_PROVIDER` | `minimax` | 预设地址:`openai`、`deepseek`、`moonshot`、`minimax`、`qwen`、`zhipu`、`openrouter`、`ollama`、`vllm`、`custom` |
|
|
127
|
+
| `MODEL_NAME` | 按预设选择 | 模型名,默认 `MiniMax-M3`;切换服务商时同步修改 |
|
|
128
|
+
| `MODEL_MAX_COMPLETION_TOKENS` | `32768` | 生成上限(含思考 token);M3 最高 128K |
|
|
129
|
+
| `MODEL_BASE_URL` | 预设地址 | 覆盖预设,用于自建网关、代理或私有化部署 |
|
|
130
|
+
| `MODEL_API_KEY` | — | 所接入服务的 Key;本地服务可填任意非空值 |
|
|
124
131
|
| `APPROVAL_MODE` | `sensitive` | `none`、`sensitive`、`all` |
|
|
125
132
|
| `ENABLE_SHELL` | `false` | 是否在宿主机暴露 `execute` |
|
|
126
|
-
| `
|
|
127
|
-
| `
|
|
133
|
+
| `SANDBOX_ENABLED` | `false` | 开启 OpenSandbox 远程沙箱,作为执行后端(按会话惰性创建) |
|
|
134
|
+
| `OPEN_SANDBOX_DOMAIN` / `OPEN_SANDBOX_API_KEY` | `localhost:8080` / — | OpenSandbox 服务地址与 API Key |
|
|
135
|
+
| `OPEN_SANDBOX_PROTOCOL` | `http` | `http` 或 `https` |
|
|
136
|
+
| `OPEN_SANDBOX_ID` | — | 共享模式:所有会话连接同一个已有沙箱,退出时只关闭连接 |
|
|
137
|
+
| `SANDBOX_IMAGE` / `SANDBOX_TIMEOUT_SECONDS` | `python:3.12` / `1800` | 沙箱镜像与 TTL(秒);使用中按 TTL/3 续期,`0` 表示不过期 |
|
|
138
|
+
| `SANDBOX_WORKSPACE_ROOT` | `/workspace` | 沙箱工作目录,也是 `execute` 的默认 cwd |
|
|
139
|
+
| `CHECKPOINTER` / `STORE` | `memory` | 设为 `postgres` 时启用 PostgreSQL 持久化 |
|
|
140
|
+
| `POSTGRES_HOST` / `POSTGRES_PORT` | `localhost` / `5432` | PostgreSQL 地址 |
|
|
141
|
+
| `POSTGRES_USERNAME` / `POSTGRES_PASSWORD` | `postgres` / `postgres` | PostgreSQL 凭证 |
|
|
142
|
+
| `POSTGRES_DATABASE` | `deepagent` | PostgreSQL 数据库名 |
|
|
143
|
+
| `DATABASE_URL` | — | 可选完整连接串,优先于上面的 `POSTGRES_*` |
|
|
128
144
|
| `MCP_SERVERS` | — | MCP 服务器的 JSON 映射 |
|
|
129
145
|
| `LANGSMITH_TRACING` | `false` | 配合 `LANGSMITH_API_KEY` 开启链路追踪 |
|
|
130
146
|
|
|
147
|
+
### 接入模型
|
|
148
|
+
|
|
149
|
+
模型层只依赖 OpenAI 兼容协议(`POST {baseUrl}/chat/completions`),更换服务商不需要改代码:
|
|
150
|
+
|
|
151
|
+
```bash
|
|
152
|
+
# DeepSeek
|
|
153
|
+
MODEL_PROVIDER=deepseek
|
|
154
|
+
MODEL_NAME=deepseek-chat
|
|
155
|
+
MODEL_API_KEY=sk-...
|
|
156
|
+
|
|
157
|
+
# MiniMax(国内站;国际站用 MODEL_BASE_URL=https://api.minimax.io/v1)
|
|
158
|
+
MODEL_PROVIDER=minimax
|
|
159
|
+
MODEL_NAME=MiniMax-M3
|
|
160
|
+
MODEL_API_KEY=sk-...
|
|
161
|
+
|
|
162
|
+
# 通义千问(阿里云百炼)
|
|
163
|
+
MODEL_PROVIDER=qwen
|
|
164
|
+
MODEL_NAME=qwen-plus
|
|
165
|
+
MODEL_API_KEY=sk-...
|
|
166
|
+
|
|
167
|
+
# 本地 Ollama:Key 可填任意非空值
|
|
168
|
+
MODEL_PROVIDER=ollama
|
|
169
|
+
MODEL_NAME=qwen3:8b
|
|
170
|
+
MODEL_API_KEY=ollama
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
自建网关或私有化部署用 `MODEL_PROVIDER=custom` 并同时指定 `MODEL_BASE_URL` 与 `MODEL_NAME`。
|
|
174
|
+
预设列表和默认模型在 `src/model/presets`;需要额外参数(超时、重试、自定义请求头等)时,调整 `src/model/openai-compatible` 里 `ChatOpenAI` 的构造参数即可。
|
|
175
|
+
|
|
131
176
|
### PostgreSQL
|
|
132
177
|
|
|
133
|
-
设置 `CHECKPOINTER=postgres
|
|
178
|
+
设置 `CHECKPOINTER=postgres` 或 `STORE=postgres` 后,服务会用 `POSTGRES_*` 分项拼装连接信息;同时设置 `DATABASE_URL` 时优先使用完整连接串。
|
|
179
|
+
|
|
180
|
+
| 变量 | 默认值 |
|
|
181
|
+
| --- | --- |
|
|
182
|
+
| `POSTGRES_HOST` | `localhost` |
|
|
183
|
+
| `POSTGRES_PORT` | `5432` |
|
|
184
|
+
| `POSTGRES_USERNAME` | `postgres` |
|
|
185
|
+
| `POSTGRES_PASSWORD` | `postgres` |
|
|
186
|
+
| `POSTGRES_DATABASE` | `deepagent` |
|
|
187
|
+
|
|
188
|
+
本地启动数据库:
|
|
134
189
|
|
|
135
190
|
```bash
|
|
136
191
|
docker compose -f deployment/docker-compose.yml up -d postgres
|
|
137
192
|
```
|
|
138
193
|
|
|
139
|
-
`PostgresSaver.setup()` / `PostgresStore.setup()` 会在启动时自动建表。`src/
|
|
194
|
+
`PostgresSaver.setup()` / `PostgresStore.setup()` 会在启动时自动建表。`src/storage/database/migrations/001_init.sql` 提供了一个可选的审计日志表。
|
|
140
195
|
|
|
141
196
|
### MCP
|
|
142
197
|
|
|
@@ -146,6 +201,38 @@ MCP_SERVERS='{"filesystem":{"transport":"stdio","command":"npx","args":["-y","@m
|
|
|
146
201
|
|
|
147
202
|
工具会在启动时加载,并合并进主 Agent 的工具列表。
|
|
148
203
|
|
|
204
|
+
### 沙箱(OpenSandbox)
|
|
205
|
+
|
|
206
|
+
[OpenSandbox](https://open-sandbox.ai/) 为 Agent 提供隔离的代码执行环境。先启动服务(默认 `http://127.0.0.1:8080`):
|
|
207
|
+
|
|
208
|
+
```bash
|
|
209
|
+
uvx opensandbox-server init-config ~/.sandbox.toml --example docker
|
|
210
|
+
uvx opensandbox-server
|
|
211
|
+
```
|
|
212
|
+
|
|
213
|
+
然后在 `.env` 中开启:
|
|
214
|
+
|
|
215
|
+
```bash
|
|
216
|
+
SANDBOX_ENABLED=true
|
|
217
|
+
OPEN_SANDBOX_DOMAIN=localhost:8080
|
|
218
|
+
SANDBOX_IMAGE=python:3.12
|
|
219
|
+
SANDBOX_TIMEOUT_SECONDS=1800
|
|
220
|
+
```
|
|
221
|
+
|
|
222
|
+
开启后:
|
|
223
|
+
|
|
224
|
+
- **按会话隔离**:每个 `threadId` 第一次运行 Agent 时惰性创建一个沙箱(不是在服务启动时),该会话后续的消息、工具调用和子 Agent 都复用同一个后端;不同会话互不影响;
|
|
225
|
+
- `/workspace/` 与 `execute` 在各自会话的沙箱内执行;`AGENTS.md`、`SKILL.md` 仍在启动时从本地读取并注入提示词,`/memories/` 继续使用 LangGraph store;
|
|
226
|
+
- 开启沙箱后 `ENABLE_SHELL` 不再生效,宿主机的 `execute` 不会被暴露;
|
|
227
|
+
- `execute` 在该会话的沙箱内维护一个持久 bash session,`cd`、`export`、函数定义等状态跨命令保留;同一会话的命令串行排队,避免互相覆盖 shell 状态;
|
|
228
|
+
- **过期与续期**:沙箱默认 TTL 1800 秒,每次使用间隔超过 TTL 的三分之一会调用 `renew` 续期,所以活跃会话不会被回收;空闲超过 TTL 由服务端回收,下次访问自动重建(沙箱内的 `/workspace` 内容随之丢失);
|
|
229
|
+
- `DELETE /api/sessions/:threadId` 会释放对应沙箱;进程退出时销毁所有沙箱;
|
|
230
|
+
- 设置 `OPEN_SANDBOX_ID` 时切换为共享模式:所有会话连接同一个已有沙箱,退出时只关闭连接、不会 `kill` 它。
|
|
231
|
+
|
|
232
|
+
> `SANDBOX_TIMEOUT_SECONDS=0` 表示不设置服务端 TTL,完全由应用在删除会话 / 退出时清理;进程崩溃时会留下无人回收的沙箱,生产环境建议保留默认 TTL。
|
|
233
|
+
|
|
234
|
+
实现位于 `src/sandbox/`:`OpenSandboxBackend` 继承 deepagents 的 `BaseSandbox`,只实现 `execute` 与文件上传/下载,`ls`、`read`、`grep` 等由 `BaseSandbox` 复用同一 session 完成。
|
|
235
|
+
|
|
149
236
|
## Docker
|
|
150
237
|
|
|
151
238
|
```bash
|
|
@@ -157,6 +244,6 @@ docker compose -f deployment/docker-compose.yml up --build
|
|
|
157
244
|
Deep Agents 遵循“信任模型”的策略:工具允许什么,Agent 就能做什么。模板提供了一些纵深防御(危险命令拦截、只读 SQL 事务、审批、工作区隔离),但真正的边界必须在部署层保证:
|
|
158
245
|
|
|
159
246
|
- 除非运行在一次性容器中,否则保持 `ENABLE_SHELL=false`。
|
|
160
|
-
-
|
|
247
|
+
- PostgreSQL 连接使用只读数据库角色。
|
|
161
248
|
- 对公网部署时,在 API 前增加鉴权与限流。
|
|
162
249
|
- 仔细审查新增的 `SKILL.md` 和 `AGENTS.md`,它们会成为模型指令。
|