create-deepagent-server 0.1.0 → 1.0.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.
Files changed (175) hide show
  1. package/README.md +83 -8
  2. package/package.json +1 -1
  3. package/src/args.js +6 -4
  4. package/src/cli.js +1 -4
  5. package/src/dependencies.js +1 -1
  6. package/src/prompts.js +1 -0
  7. package/src/registry.js +8 -8
  8. package/src/scaffold.js +21 -7
  9. package/src/setup.js +2 -0
  10. package/src/version.js +1 -1
  11. package/src/versions.js +1 -1
  12. package/templates/deepagent/js/scripts/check.js +34 -11
  13. package/templates/deepagent/js/scripts/dev.js +9 -0
  14. package/templates/deepagent/js/src/agents/factory.js +2 -1
  15. package/templates/deepagent/js/src/agents/prompts/loader.js +9 -0
  16. package/templates/deepagent/js/src/agents/prompts/supervisor.js +4 -0
  17. package/templates/deepagent/js/src/agents/prompts/supervisor.md +1 -2
  18. package/templates/deepagent/js/src/agents/state/types.js +1 -0
  19. package/templates/deepagent/js/src/agents/subagents/analyst.js +4 -3
  20. package/templates/deepagent/js/src/agents/subagents/coder.js +1 -0
  21. package/templates/deepagent/js/src/agents/subagents/index.js +5 -4
  22. package/templates/deepagent/js/src/agents/supervisor.js +12 -4
  23. package/templates/deepagent/js/src/api/app.js +7 -0
  24. package/templates/deepagent/js/src/api/middleware/auth.js +9 -0
  25. package/templates/deepagent/js/src/api/middleware/error.js +4 -0
  26. package/templates/deepagent/js/src/api/routes/agent.js +19 -49
  27. package/templates/deepagent/js/src/api/routes/health.js +12 -3
  28. package/templates/deepagent/js/src/api/routes/session.js +28 -48
  29. package/templates/deepagent/js/src/app.js +31 -12
  30. package/templates/deepagent/js/src/backends/composite.js +2 -0
  31. package/templates/deepagent/js/src/backends/filesystem.js +1 -0
  32. package/templates/deepagent/js/src/backends/index.js +35 -3
  33. package/templates/deepagent/js/src/backends/sandbox.js +3 -0
  34. package/templates/deepagent/js/src/config/agent.js +1 -0
  35. package/templates/deepagent/js/src/config/database.js +19 -0
  36. package/templates/deepagent/js/src/config/env.js +46 -23
  37. package/templates/deepagent/js/src/config/index.js +13 -3
  38. package/templates/deepagent/js/src/graph/checkpointer.js +10 -2
  39. package/templates/deepagent/js/src/graph/compile.js +1 -0
  40. package/templates/deepagent/js/src/graph/store.js +10 -2
  41. package/templates/deepagent/js/src/index.js +9 -2
  42. package/templates/deepagent/js/src/memory/index.js +2 -0
  43. package/templates/deepagent/js/src/memory/manager.js +21 -10
  44. package/templates/deepagent/js/src/memory/store.js +7 -0
  45. package/templates/deepagent/js/src/middleware/approval.js +6 -1
  46. package/templates/deepagent/js/src/middleware/context.js +5 -0
  47. package/templates/deepagent/js/src/middleware/logging.js +47 -43
  48. package/templates/deepagent/js/src/middleware/security.js +7 -0
  49. package/templates/deepagent/js/src/model/config.js +38 -0
  50. package/templates/deepagent/js/src/model/index.js +9 -0
  51. package/templates/deepagent/js/src/model/openai-compatible.js +18 -0
  52. package/templates/deepagent/js/src/model/presets.js +71 -0
  53. package/templates/deepagent/js/src/sandbox/config.js +12 -0
  54. package/templates/deepagent/js/src/sandbox/index.js +3 -0
  55. package/templates/deepagent/js/src/sandbox/manager.js +161 -0
  56. package/templates/deepagent/js/src/sandbox/opensandbox.js +195 -0
  57. package/templates/deepagent/js/src/schemas/agent.js +7 -0
  58. package/templates/deepagent/js/src/schemas/common.js +5 -0
  59. package/templates/deepagent/js/src/schemas/locale.js +1 -0
  60. package/templates/deepagent/js/src/schemas/session.js +4 -0
  61. package/templates/deepagent/js/src/services/{agent.service.js → agent.js} +39 -20
  62. package/templates/deepagent/js/src/services/{memory.service.js → memory.js} +13 -7
  63. package/templates/deepagent/js/src/services/{session.service.js → session.js} +43 -17
  64. package/templates/deepagent/js/src/skills/index.js +4 -0
  65. package/templates/deepagent/js/src/skills/loader.js +20 -10
  66. package/templates/deepagent/js/src/storage/database/client.js +57 -0
  67. package/templates/deepagent/{ts/src/infrastructure → js/src/storage}/database/migrations/001_init.sql +1 -1
  68. package/templates/deepagent/js/src/{infrastructure → storage}/logger/index.js +2 -0
  69. package/templates/deepagent/js/src/{infrastructure → storage}/redis/client.js +25 -19
  70. package/templates/deepagent/js/src/{infrastructure → storage}/shutdown/index.js +20 -13
  71. package/templates/deepagent/js/src/tools/database/index.js +1 -1
  72. package/templates/deepagent/js/src/tools/database/query.js +44 -32
  73. package/templates/deepagent/js/src/tools/filesystem/index.js +16 -0
  74. package/templates/deepagent/js/src/tools/index.js +6 -11
  75. package/templates/deepagent/js/src/tools/mcp/client.js +7 -0
  76. package/templates/deepagent/js/src/tools/mcp/connections.js +13 -3
  77. package/templates/deepagent/js/src/tools/mcp/index.js +2 -0
  78. package/templates/deepagent/js/src/tools/mcp/registry.js +15 -11
  79. package/templates/deepagent/js/src/utils/errors.js +9 -0
  80. package/templates/deepagent/js/src/utils/helpers.js +15 -0
  81. package/templates/deepagent/js/src/utils/validation.js +15 -0
  82. package/templates/deepagent/js/tests/agents/prompts.test.js +9 -3
  83. package/templates/deepagent/js/tests/agents/supervisor.test.js +13 -5
  84. package/templates/deepagent/js/tests/config/database.test.js +40 -0
  85. package/templates/deepagent/js/tests/integration/api.test.js +18 -3
  86. package/templates/deepagent/js/tests/middleware/security.test.js +4 -1
  87. package/templates/deepagent/js/tests/model/config.test.js +61 -0
  88. package/templates/deepagent/js/tests/sandbox/config.test.js +46 -0
  89. package/templates/deepagent/js/tests/sandbox/manager.test.js +236 -0
  90. package/templates/deepagent/js/tests/sandbox/opensandbox.test.js +173 -0
  91. package/templates/deepagent/js/tests/services/session.test.js +41 -0
  92. package/templates/deepagent/js/tests/tools/query.test.js +5 -1
  93. package/templates/deepagent/shared/README.md +101 -14
  94. package/templates/deepagent/shared/_env.example +40 -10
  95. package/templates/deepagent/shared/deployment/docker-compose.yml +8 -4
  96. package/templates/deepagent/shared/resources/AGENTS.md +1 -1
  97. package/templates/deepagent/ts/scripts/check.ts +19 -11
  98. package/templates/deepagent/ts/scripts/copy-assets.ts +2 -0
  99. package/templates/deepagent/ts/src/agents/factory.ts +1 -1
  100. package/templates/deepagent/ts/src/agents/prompts/loader.ts +1 -0
  101. package/templates/deepagent/ts/src/agents/prompts/supervisor.md +1 -2
  102. package/templates/deepagent/ts/src/agents/prompts/supervisor.ts +1 -1
  103. package/templates/deepagent/ts/src/agents/subagents/analyst.ts +7 -3
  104. package/templates/deepagent/ts/src/agents/subagents/index.ts +4 -5
  105. package/templates/deepagent/ts/src/agents/supervisor.ts +11 -9
  106. package/templates/deepagent/ts/src/api/app.ts +4 -4
  107. package/templates/deepagent/ts/src/api/middleware/auth.ts +1 -0
  108. package/templates/deepagent/ts/src/api/middleware/error.ts +1 -1
  109. package/templates/deepagent/ts/src/api/routes/agent.ts +13 -58
  110. package/templates/deepagent/ts/src/api/routes/health.ts +5 -3
  111. package/templates/deepagent/ts/src/api/routes/session.ts +21 -51
  112. package/templates/deepagent/ts/src/app.ts +19 -12
  113. package/templates/deepagent/ts/src/backends/index.ts +41 -7
  114. package/templates/deepagent/ts/src/backends/sandbox.ts +1 -0
  115. package/templates/deepagent/ts/src/config/database.ts +21 -0
  116. package/templates/deepagent/ts/src/config/env.ts +32 -28
  117. package/templates/deepagent/ts/src/config/index.ts +12 -4
  118. package/templates/deepagent/ts/src/graph/checkpointer.ts +5 -2
  119. package/templates/deepagent/ts/src/graph/store.ts +5 -2
  120. package/templates/deepagent/ts/src/index.ts +2 -2
  121. package/templates/deepagent/ts/src/memory/manager.ts +12 -10
  122. package/templates/deepagent/ts/src/memory/store.ts +2 -0
  123. package/templates/deepagent/ts/src/middleware/approval.ts +2 -2
  124. package/templates/deepagent/ts/src/middleware/logging.ts +43 -44
  125. package/templates/deepagent/ts/src/middleware/security.ts +1 -0
  126. package/templates/deepagent/ts/src/model/config.ts +56 -0
  127. package/templates/deepagent/ts/src/model/index.ts +14 -0
  128. package/templates/deepagent/ts/src/model/openai-compatible.ts +21 -0
  129. package/templates/deepagent/ts/src/model/presets.ts +82 -0
  130. package/templates/deepagent/ts/src/sandbox/config.ts +30 -0
  131. package/templates/deepagent/ts/src/sandbox/index.ts +7 -0
  132. package/templates/deepagent/ts/src/sandbox/manager.ts +176 -0
  133. package/templates/deepagent/ts/src/sandbox/opensandbox.ts +216 -0
  134. package/templates/deepagent/ts/src/services/{agent.service.ts → agent.ts} +22 -22
  135. package/templates/deepagent/ts/src/services/{memory.service.ts → memory.ts} +7 -7
  136. package/templates/deepagent/ts/src/services/{session.service.ts → session.ts} +29 -19
  137. package/templates/deepagent/ts/src/skills/loader.ts +6 -10
  138. package/templates/deepagent/ts/src/storage/database/client.ts +65 -0
  139. package/templates/deepagent/{js/src/infrastructure → ts/src/storage}/database/migrations/001_init.sql +1 -1
  140. package/templates/deepagent/ts/src/{infrastructure → storage}/redis/client.ts +14 -21
  141. package/templates/deepagent/ts/src/{infrastructure → storage}/shutdown/index.ts +10 -13
  142. package/templates/deepagent/ts/src/tools/database/index.ts +5 -1
  143. package/templates/deepagent/ts/src/tools/database/query.ts +40 -32
  144. package/templates/deepagent/ts/src/tools/filesystem/index.ts +2 -0
  145. package/templates/deepagent/ts/src/tools/index.ts +10 -8
  146. package/templates/deepagent/ts/src/tools/mcp/client.ts +1 -0
  147. package/templates/deepagent/ts/src/tools/mcp/connections.ts +3 -3
  148. package/templates/deepagent/ts/src/tools/mcp/registry.ts +9 -12
  149. package/templates/deepagent/ts/src/utils/helpers.ts +5 -0
  150. package/templates/deepagent/ts/src/utils/validation.ts +17 -0
  151. package/templates/deepagent/ts/tests/agents/prompts.test.ts +3 -3
  152. package/templates/deepagent/ts/tests/agents/supervisor.test.ts +5 -5
  153. package/templates/deepagent/ts/tests/config/database.test.ts +40 -0
  154. package/templates/deepagent/ts/tests/integration/api.test.ts +5 -6
  155. package/templates/deepagent/ts/tests/middleware/security.test.ts +1 -1
  156. package/templates/deepagent/ts/tests/model/config.test.ts +61 -0
  157. package/templates/deepagent/ts/tests/sandbox/config.test.ts +46 -0
  158. package/templates/deepagent/ts/tests/sandbox/manager.test.ts +241 -0
  159. package/templates/deepagent/ts/tests/sandbox/opensandbox.test.ts +188 -0
  160. package/templates/deepagent/ts/tests/services/session.test.ts +45 -0
  161. package/templates/deepagent/ts/tests/tools/query.test.ts +1 -1
  162. package/templates/deepagent/js/src/agents/prompts/subagents/researcher.md +0 -16
  163. package/templates/deepagent/js/src/agents/subagents/researcher.js +0 -12
  164. package/templates/deepagent/js/src/config/model.js +0 -34
  165. package/templates/deepagent/js/src/infrastructure/database/client.js +0 -43
  166. package/templates/deepagent/js/src/tools/search/index.js +0 -1
  167. package/templates/deepagent/js/src/tools/search/web-search.js +0 -54
  168. package/templates/deepagent/shared/skills/research/SKILL.md +0 -31
  169. package/templates/deepagent/ts/src/agents/prompts/subagents/researcher.md +0 -16
  170. package/templates/deepagent/ts/src/agents/subagents/researcher.ts +0 -15
  171. package/templates/deepagent/ts/src/config/model.ts +0 -49
  172. package/templates/deepagent/ts/src/infrastructure/database/client.ts +0 -61
  173. package/templates/deepagent/ts/src/tools/search/index.ts +0 -1
  174. package/templates/deepagent/ts/src/tools/search/web-search.ts +0 -82
  175. /package/templates/deepagent/ts/src/{infrastructure → storage}/logger/index.ts +0 -0
@@ -0,0 +1,61 @@
1
+ import assert from "node:assert/strict";
2
+ import test from "node:test";
3
+
4
+ import { loadEnv } from "../../src/config/index.js";
5
+ import { resolveModelConfig } from "../../src/model/index.js";
6
+
7
+ const envFor = (source) => {
8
+ return loadEnv({ NODE_ENV: "test", ...source }, { requireModelCredentials: false });
9
+ };
10
+
11
+ test("默认使用 MiniMax M3(OpenAI 兼容协议)", () => {
12
+ const config = resolveModelConfig(envFor({}));
13
+
14
+ assert.equal(config.provider, "minimax");
15
+ assert.equal(config.label, "MiniMax");
16
+ assert.equal(config.name, "MiniMax-M3");
17
+ assert.equal(config.baseUrl, "https://api.minimaxi.com/v1");
18
+ assert.equal(config.maxCompletionTokens, 32768);
19
+ });
20
+
21
+ test("预设切换服务商,并允许用 MODEL_* 覆盖", () => {
22
+ const config = resolveModelConfig(
23
+ envFor({
24
+ MODEL_PROVIDER: "deepseek",
25
+ MODEL_NAME: "deepseek-reasoner",
26
+ MODEL_BASE_URL: "https://gateway.internal/v1",
27
+ MODEL_API_KEY: "sk-test",
28
+ MODEL_TEMPERATURE: "0.5",
29
+ MODEL_MAX_COMPLETION_TOKENS: "1024",
30
+ }),
31
+ );
32
+
33
+ assert.equal(config.provider, "deepseek");
34
+ assert.equal(config.name, "deepseek-reasoner");
35
+ assert.equal(config.baseUrl, "https://gateway.internal/v1");
36
+ assert.equal(config.apiKey, "sk-test");
37
+ assert.equal(config.temperature, 0.5);
38
+ assert.equal(config.maxCompletionTokens, 1024);
39
+ });
40
+
41
+ test("旧变量 MODEL_MAX_TOKENS 仍然兼容,新变量优先", () => {
42
+ assert.equal(resolveModelConfig(envFor({ MODEL_MAX_TOKENS: "2048" })).maxCompletionTokens, 2048);
43
+
44
+ const config = resolveModelConfig(
45
+ envFor({ MODEL_MAX_COMPLETION_TOKENS: "8192", MODEL_MAX_TOKENS: "2048" }),
46
+ );
47
+ assert.equal(config.maxCompletionTokens, 8192);
48
+ });
49
+
50
+ test("MiniMax 预设使用国内 OpenAI 兼容地址", () => {
51
+ const config = resolveModelConfig(envFor({ MODEL_PROVIDER: "minimax" }));
52
+
53
+ assert.equal(config.label, "MiniMax");
54
+ assert.equal(config.name, "MiniMax-M3");
55
+ assert.equal(config.baseUrl, "https://api.minimaxi.com/v1");
56
+ });
57
+
58
+ test("缺少地址或模型名时给出明确的配置错误", () => {
59
+ assert.throws(() => resolveModelConfig(envFor({ MODEL_PROVIDER: "custom" })), /MODEL_BASE_URL/);
60
+ assert.throws(() => resolveModelConfig(envFor({ MODEL_PROVIDER: "vllm" })), /MODEL_NAME/);
61
+ });
@@ -0,0 +1,46 @@
1
+ import assert from "node:assert/strict";
2
+ import test from "node:test";
3
+
4
+ import { loadEnv } from "../../src/config/index.js";
5
+ import { resolveSandboxConfig } from "../../src/sandbox/config.js";
6
+
7
+ const envFor = (source) => {
8
+ return loadEnv({ NODE_ENV: "test", ...source }, { requireModelCredentials: false });
9
+ };
10
+
11
+ test("默认不启用沙箱", () => {
12
+ const config = resolveSandboxConfig(envFor({}));
13
+
14
+ assert.equal(config.enabled, false);
15
+ assert.equal(config.domain, "localhost:8080");
16
+ assert.equal(config.protocol, "http");
17
+ assert.equal(config.image, "python:3.12");
18
+ assert.equal(config.timeoutSeconds, 1800);
19
+ assert.equal(config.workspaceRoot, "/workspace");
20
+ assert.equal(config.apiKey, undefined);
21
+ assert.equal(config.sandboxId, undefined);
22
+ });
23
+
24
+ test("启用沙箱时解析 OpenSandbox 配置", () => {
25
+ const config = resolveSandboxConfig(
26
+ envFor({
27
+ SANDBOX_ENABLED: "true",
28
+ OPEN_SANDBOX_DOMAIN: "sandbox.example.com",
29
+ OPEN_SANDBOX_PROTOCOL: "https",
30
+ OPEN_SANDBOX_API_KEY: "sk-test",
31
+ OPEN_SANDBOX_ID: "sbx-existing",
32
+ SANDBOX_IMAGE: "node:22",
33
+ SANDBOX_TIMEOUT_SECONDS: "600",
34
+ SANDBOX_WORKSPACE_ROOT: "/home/agent",
35
+ }),
36
+ );
37
+
38
+ assert.equal(config.enabled, true);
39
+ assert.equal(config.domain, "sandbox.example.com");
40
+ assert.equal(config.protocol, "https");
41
+ assert.equal(config.apiKey, "sk-test");
42
+ assert.equal(config.sandboxId, "sbx-existing");
43
+ assert.equal(config.image, "node:22");
44
+ assert.equal(config.timeoutSeconds, 600);
45
+ assert.equal(config.workspaceRoot, "/home/agent");
46
+ });
@@ -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
- import { assertReadOnlyQuery } from "../../src/tools/database/query.js";
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
  });