@agentmeshhq/agent 0.1.11 → 0.1.13
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/dist/__tests__/injector.test.d.ts +1 -0
- package/dist/__tests__/injector.test.js +26 -0
- package/dist/__tests__/injector.test.js.map +1 -0
- package/dist/__tests__/sandbox.test.d.ts +1 -0
- package/dist/__tests__/sandbox.test.js +362 -0
- package/dist/__tests__/sandbox.test.js.map +1 -0
- package/dist/cli/build.d.ts +6 -0
- package/dist/cli/build.js +111 -0
- package/dist/cli/build.js.map +1 -0
- package/dist/cli/deploy.d.ts +9 -0
- package/dist/cli/deploy.js +130 -0
- package/dist/cli/deploy.js.map +1 -0
- package/dist/cli/inbox.d.ts +5 -0
- package/dist/cli/inbox.js +123 -0
- package/dist/cli/inbox.js.map +1 -0
- package/dist/cli/index.js +159 -0
- package/dist/cli/index.js.map +1 -1
- package/dist/cli/issue.d.ts +42 -0
- package/dist/cli/issue.js +297 -0
- package/dist/cli/issue.js.map +1 -0
- package/dist/cli/local.d.ts +9 -0
- package/dist/cli/local.js +139 -0
- package/dist/cli/local.js.map +1 -0
- package/dist/cli/migrate.d.ts +8 -0
- package/dist/cli/migrate.js +167 -0
- package/dist/cli/migrate.js.map +1 -0
- package/dist/cli/ready.d.ts +5 -0
- package/dist/cli/ready.js +131 -0
- package/dist/cli/ready.js.map +1 -0
- package/dist/cli/slack.d.ts +3 -0
- package/dist/cli/slack.js +57 -0
- package/dist/cli/slack.js.map +1 -0
- package/dist/cli/start.d.ts +12 -0
- package/dist/cli/start.js +14 -0
- package/dist/cli/start.js.map +1 -1
- package/dist/cli/sync.d.ts +8 -0
- package/dist/cli/sync.js +154 -0
- package/dist/cli/sync.js.map +1 -0
- package/dist/cli/test.d.ts +8 -0
- package/dist/cli/test.js +110 -0
- package/dist/cli/test.js.map +1 -0
- package/dist/core/daemon.d.ts +31 -0
- package/dist/core/daemon.js +188 -28
- package/dist/core/daemon.js.map +1 -1
- package/dist/core/injector.d.ts +6 -1
- package/dist/core/injector.js +64 -1
- package/dist/core/injector.js.map +1 -1
- package/dist/core/issue-cache.d.ts +44 -0
- package/dist/core/issue-cache.js +75 -0
- package/dist/core/issue-cache.js.map +1 -0
- package/dist/core/registry.d.ts +5 -0
- package/dist/core/registry.js +8 -1
- package/dist/core/registry.js.map +1 -1
- package/dist/core/sandbox.d.ts +127 -0
- package/dist/core/sandbox.js +377 -0
- package/dist/core/sandbox.js.map +1 -0
- package/dist/core/tmux.js +8 -10
- package/dist/core/tmux.js.map +1 -1
- package/package.json +1 -1
- package/src/__tests__/injector.test.ts +29 -0
- package/src/__tests__/sandbox.test.ts +435 -0
- package/src/cli/build.ts +137 -0
- package/src/cli/deploy.ts +153 -0
- package/src/cli/index.ts +163 -0
- package/src/cli/local.ts +174 -0
- package/src/cli/migrate.ts +210 -0
- package/src/cli/slack.ts +69 -0
- package/src/cli/start.ts +22 -0
- package/src/cli/test.ts +141 -0
- package/src/core/daemon.ts +228 -37
- package/src/core/injector.ts +98 -1
- package/src/core/registry.ts +14 -1
- package/src/core/sandbox.ts +505 -0
- package/src/core/tmux.ts +9 -11
|
@@ -0,0 +1,435 @@
|
|
|
1
|
+
import os from "node:os";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
4
|
+
import { DockerSandbox, type SandboxConfig, type SandboxMountPolicy } from "../core/sandbox.js";
|
|
5
|
+
|
|
6
|
+
// Mock child_process
|
|
7
|
+
vi.mock("node:child_process", () => ({
|
|
8
|
+
execSync: vi.fn(),
|
|
9
|
+
spawn: vi.fn(() => ({
|
|
10
|
+
on: vi.fn(),
|
|
11
|
+
unref: vi.fn(),
|
|
12
|
+
pid: 12345,
|
|
13
|
+
})),
|
|
14
|
+
spawnSync: vi.fn(() => ({
|
|
15
|
+
status: 0,
|
|
16
|
+
stdout: "mock-container-id\n",
|
|
17
|
+
stderr: "",
|
|
18
|
+
})),
|
|
19
|
+
}));
|
|
20
|
+
|
|
21
|
+
// Mock fs
|
|
22
|
+
vi.mock("node:fs", () => ({
|
|
23
|
+
default: {
|
|
24
|
+
existsSync: vi.fn(() => true),
|
|
25
|
+
},
|
|
26
|
+
}));
|
|
27
|
+
|
|
28
|
+
describe("DockerSandbox", () => {
|
|
29
|
+
const defaultConfig: SandboxConfig = {
|
|
30
|
+
agentName: "test-agent",
|
|
31
|
+
image: "agentmesh/agent-sandbox:latest",
|
|
32
|
+
workspacePath: "/tmp/workspace",
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
beforeEach(() => {
|
|
36
|
+
vi.clearAllMocks();
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
describe("constructor", () => {
|
|
40
|
+
it("should create sandbox with default values", () => {
|
|
41
|
+
const sandbox = new DockerSandbox(defaultConfig);
|
|
42
|
+
|
|
43
|
+
expect(sandbox.getContainerName()).toMatch(/^agentmesh-sandbox-test-agent-[a-f0-9]{8}$/);
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
it("should merge config with defaults", () => {
|
|
47
|
+
const config: SandboxConfig = {
|
|
48
|
+
...defaultConfig,
|
|
49
|
+
cpuLimit: "0.5",
|
|
50
|
+
memoryLimit: "1g",
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
const sandbox = new DockerSandbox(config);
|
|
54
|
+
expect(sandbox).toBeDefined();
|
|
55
|
+
});
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
describe("validateMountPolicy", () => {
|
|
59
|
+
it("should deny sensitive paths by default", () => {
|
|
60
|
+
const sandbox = new DockerSandbox({
|
|
61
|
+
...defaultConfig,
|
|
62
|
+
workspacePath: path.join(os.homedir(), ".ssh"),
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
expect(() => sandbox.validateMountPolicy()).toThrow(/Mount denied.*\.ssh/);
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
it("should deny ~/.gnupg", () => {
|
|
69
|
+
const sandbox = new DockerSandbox({
|
|
70
|
+
...defaultConfig,
|
|
71
|
+
workspacePath: path.join(os.homedir(), ".gnupg"),
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
expect(() => sandbox.validateMountPolicy()).toThrow(/Mount denied.*\.gnupg/);
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
it("should deny ~/.aws", () => {
|
|
78
|
+
const sandbox = new DockerSandbox({
|
|
79
|
+
...defaultConfig,
|
|
80
|
+
workspacePath: path.join(os.homedir(), ".aws"),
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
expect(() => sandbox.validateMountPolicy()).toThrow(/Mount denied.*\.aws/);
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
it("should deny ~/.config/gcloud", () => {
|
|
87
|
+
const sandbox = new DockerSandbox({
|
|
88
|
+
...defaultConfig,
|
|
89
|
+
workspacePath: path.join(os.homedir(), ".config/gcloud"),
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
expect(() => sandbox.validateMountPolicy()).toThrow(/Mount denied.*gcloud/);
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
it("should deny ~/.kube", () => {
|
|
96
|
+
const sandbox = new DockerSandbox({
|
|
97
|
+
...defaultConfig,
|
|
98
|
+
workspacePath: path.join(os.homedir(), ".kube"),
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
expect(() => sandbox.validateMountPolicy()).toThrow(/Mount denied.*\.kube/);
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
it("should deny ~/.docker", () => {
|
|
105
|
+
const sandbox = new DockerSandbox({
|
|
106
|
+
...defaultConfig,
|
|
107
|
+
workspacePath: path.join(os.homedir(), ".docker"),
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
expect(() => sandbox.validateMountPolicy()).toThrow(/Mount denied.*\.docker/);
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
it("should deny /var/run/docker.sock", () => {
|
|
114
|
+
const sandbox = new DockerSandbox({
|
|
115
|
+
...defaultConfig,
|
|
116
|
+
workspacePath: "/var/run/docker.sock",
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
expect(() => sandbox.validateMountPolicy()).toThrow(/Mount denied.*docker\.sock/);
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
it("should allow regular workspace paths", () => {
|
|
123
|
+
const sandbox = new DockerSandbox({
|
|
124
|
+
...defaultConfig,
|
|
125
|
+
workspacePath: "/tmp/workspace",
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
expect(() => sandbox.validateMountPolicy()).not.toThrow();
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
it("should enforce custom allowed paths when specified", () => {
|
|
132
|
+
const sandbox = new DockerSandbox({
|
|
133
|
+
...defaultConfig,
|
|
134
|
+
workspacePath: "/home/user/random",
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
const policy: SandboxMountPolicy = {
|
|
138
|
+
allowedPaths: ["/home/user/allowed-only"],
|
|
139
|
+
deniedPaths: [],
|
|
140
|
+
};
|
|
141
|
+
|
|
142
|
+
expect(() => sandbox.validateMountPolicy(policy)).toThrow(/not in allowed paths/);
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
it("should allow path within custom allowed paths", () => {
|
|
146
|
+
const sandbox = new DockerSandbox({
|
|
147
|
+
...defaultConfig,
|
|
148
|
+
workspacePath: "/home/user/projects/myrepo",
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
const policy: SandboxMountPolicy = {
|
|
152
|
+
allowedPaths: ["/home/user/projects"],
|
|
153
|
+
deniedPaths: [],
|
|
154
|
+
};
|
|
155
|
+
|
|
156
|
+
expect(() => sandbox.validateMountPolicy(policy)).not.toThrow();
|
|
157
|
+
});
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
describe("checkDockerAvailable", () => {
|
|
161
|
+
it("should return true when docker is available", async () => {
|
|
162
|
+
const { spawnSync } = await import("node:child_process");
|
|
163
|
+
vi.mocked(spawnSync).mockReturnValueOnce({
|
|
164
|
+
status: 0,
|
|
165
|
+
stdout: "Docker version 24.0.0",
|
|
166
|
+
stderr: "",
|
|
167
|
+
pid: 1,
|
|
168
|
+
output: [],
|
|
169
|
+
signal: null,
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
expect(DockerSandbox.checkDockerAvailable()).toBe(true);
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
it("should return false when docker is not available", async () => {
|
|
176
|
+
const { spawnSync } = await import("node:child_process");
|
|
177
|
+
vi.mocked(spawnSync).mockReturnValueOnce({
|
|
178
|
+
status: 1,
|
|
179
|
+
stdout: "",
|
|
180
|
+
stderr: "docker: command not found",
|
|
181
|
+
pid: 1,
|
|
182
|
+
output: [],
|
|
183
|
+
signal: null,
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
expect(DockerSandbox.checkDockerAvailable()).toBe(false);
|
|
187
|
+
});
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
describe("listAll", () => {
|
|
191
|
+
it("should parse docker ps output correctly", async () => {
|
|
192
|
+
const { spawnSync } = await import("node:child_process");
|
|
193
|
+
vi.mocked(spawnSync).mockReturnValueOnce({
|
|
194
|
+
status: 0,
|
|
195
|
+
stdout:
|
|
196
|
+
"abc123|agentmesh-sandbox-agent1-12345678|Up 5 minutes|agentmesh/agent-sandbox:latest\ndef456|agentmesh-sandbox-agent2-87654321|Exited (0)|agentmesh/agent-sandbox:latest",
|
|
197
|
+
stderr: "",
|
|
198
|
+
pid: 1,
|
|
199
|
+
output: [],
|
|
200
|
+
signal: null,
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
const containers = DockerSandbox.listAll();
|
|
204
|
+
|
|
205
|
+
expect(containers).toHaveLength(2);
|
|
206
|
+
expect(containers[0]).toEqual({
|
|
207
|
+
id: "abc123",
|
|
208
|
+
name: "agentmesh-sandbox-agent1-12345678",
|
|
209
|
+
status: "Up 5 minutes",
|
|
210
|
+
image: "agentmesh/agent-sandbox:latest",
|
|
211
|
+
});
|
|
212
|
+
});
|
|
213
|
+
|
|
214
|
+
it("should return empty array when no containers found", async () => {
|
|
215
|
+
const { spawnSync } = await import("node:child_process");
|
|
216
|
+
vi.mocked(spawnSync).mockReturnValueOnce({
|
|
217
|
+
status: 0,
|
|
218
|
+
stdout: "",
|
|
219
|
+
stderr: "",
|
|
220
|
+
pid: 1,
|
|
221
|
+
output: [],
|
|
222
|
+
signal: null,
|
|
223
|
+
});
|
|
224
|
+
|
|
225
|
+
const containers = DockerSandbox.listAll();
|
|
226
|
+
expect(containers).toEqual([]);
|
|
227
|
+
});
|
|
228
|
+
});
|
|
229
|
+
|
|
230
|
+
describe("findExisting", () => {
|
|
231
|
+
it("should find existing container for agent", async () => {
|
|
232
|
+
const { spawnSync } = await import("node:child_process");
|
|
233
|
+
vi.mocked(spawnSync).mockReturnValueOnce({
|
|
234
|
+
status: 0,
|
|
235
|
+
stdout: "abc123def456\n",
|
|
236
|
+
stderr: "",
|
|
237
|
+
pid: 1,
|
|
238
|
+
output: [],
|
|
239
|
+
signal: null,
|
|
240
|
+
});
|
|
241
|
+
|
|
242
|
+
const containerId = DockerSandbox.findExisting("test-agent");
|
|
243
|
+
expect(containerId).toBe("abc123def456");
|
|
244
|
+
});
|
|
245
|
+
|
|
246
|
+
it("should return null when no container exists", async () => {
|
|
247
|
+
const { spawnSync } = await import("node:child_process");
|
|
248
|
+
vi.mocked(spawnSync).mockReturnValueOnce({
|
|
249
|
+
status: 0,
|
|
250
|
+
stdout: "\n",
|
|
251
|
+
stderr: "",
|
|
252
|
+
pid: 1,
|
|
253
|
+
output: [],
|
|
254
|
+
signal: null,
|
|
255
|
+
});
|
|
256
|
+
|
|
257
|
+
const containerId = DockerSandbox.findExisting("nonexistent");
|
|
258
|
+
expect(containerId).toBeNull();
|
|
259
|
+
});
|
|
260
|
+
});
|
|
261
|
+
|
|
262
|
+
describe("container lifecycle", () => {
|
|
263
|
+
it("should build correct docker run arguments", async () => {
|
|
264
|
+
const { spawnSync } = await import("node:child_process");
|
|
265
|
+
const mockSpawnSync = vi.mocked(spawnSync);
|
|
266
|
+
|
|
267
|
+
// Mock image inspect (image exists)
|
|
268
|
+
mockSpawnSync.mockReturnValueOnce({
|
|
269
|
+
status: 0,
|
|
270
|
+
stdout: "{}",
|
|
271
|
+
stderr: "",
|
|
272
|
+
pid: 1,
|
|
273
|
+
output: [],
|
|
274
|
+
signal: null,
|
|
275
|
+
});
|
|
276
|
+
|
|
277
|
+
// Mock docker run
|
|
278
|
+
mockSpawnSync.mockReturnValueOnce({
|
|
279
|
+
status: 0,
|
|
280
|
+
stdout: "container-id-12345\n",
|
|
281
|
+
stderr: "",
|
|
282
|
+
pid: 1,
|
|
283
|
+
output: [],
|
|
284
|
+
signal: null,
|
|
285
|
+
});
|
|
286
|
+
|
|
287
|
+
const sandbox = new DockerSandbox({
|
|
288
|
+
agentName: "test",
|
|
289
|
+
image: "agentmesh/agent-sandbox:latest",
|
|
290
|
+
workspacePath: "/tmp/workspace",
|
|
291
|
+
cpuLimit: "2",
|
|
292
|
+
memoryLimit: "4g",
|
|
293
|
+
env: {
|
|
294
|
+
AGENT_TOKEN: "test-token",
|
|
295
|
+
},
|
|
296
|
+
});
|
|
297
|
+
|
|
298
|
+
await sandbox.pullImage();
|
|
299
|
+
await sandbox.start();
|
|
300
|
+
|
|
301
|
+
// Find the docker run call
|
|
302
|
+
const runCall = mockSpawnSync.mock.calls.find(
|
|
303
|
+
(call) => call[0] === "docker" && call[1]?.[0] === "run",
|
|
304
|
+
);
|
|
305
|
+
|
|
306
|
+
expect(runCall).toBeDefined();
|
|
307
|
+
const args = runCall![1] as string[];
|
|
308
|
+
|
|
309
|
+
// Check key arguments
|
|
310
|
+
expect(args).toContain("-d");
|
|
311
|
+
expect(args).toContain("--cpus");
|
|
312
|
+
expect(args).toContain("2");
|
|
313
|
+
expect(args).toContain("--memory");
|
|
314
|
+
expect(args).toContain("4g");
|
|
315
|
+
expect(args).toContain("--user");
|
|
316
|
+
expect(args).toContain("1000:1000");
|
|
317
|
+
expect(args).toContain("-e");
|
|
318
|
+
expect(args).toContain("AGENT_TOKEN=test-token");
|
|
319
|
+
});
|
|
320
|
+
|
|
321
|
+
it("should get container status", async () => {
|
|
322
|
+
const { spawnSync } = await import("node:child_process");
|
|
323
|
+
vi.mocked(spawnSync).mockReturnValueOnce({
|
|
324
|
+
status: 0,
|
|
325
|
+
stdout: "true|running|healthy",
|
|
326
|
+
stderr: "",
|
|
327
|
+
pid: 1,
|
|
328
|
+
output: [],
|
|
329
|
+
signal: null,
|
|
330
|
+
});
|
|
331
|
+
|
|
332
|
+
const sandbox = new DockerSandbox(defaultConfig);
|
|
333
|
+
// @ts-expect-error - accessing private property for testing
|
|
334
|
+
sandbox.containerId = "test-container";
|
|
335
|
+
|
|
336
|
+
const status = sandbox.getStatus();
|
|
337
|
+
|
|
338
|
+
expect(status).toEqual({
|
|
339
|
+
running: true,
|
|
340
|
+
status: "running",
|
|
341
|
+
health: "healthy",
|
|
342
|
+
});
|
|
343
|
+
});
|
|
344
|
+
|
|
345
|
+
it("should return not found status when container not started", async () => {
|
|
346
|
+
const { spawnSync } = await import("node:child_process");
|
|
347
|
+
vi.mocked(spawnSync).mockReturnValueOnce({
|
|
348
|
+
status: 1, // docker inspect fails for non-existent container
|
|
349
|
+
stdout: "",
|
|
350
|
+
stderr: "Error: No such container",
|
|
351
|
+
pid: 1,
|
|
352
|
+
output: [],
|
|
353
|
+
signal: null,
|
|
354
|
+
});
|
|
355
|
+
|
|
356
|
+
const sandbox = new DockerSandbox(defaultConfig);
|
|
357
|
+
const status = sandbox.getStatus();
|
|
358
|
+
|
|
359
|
+
expect(status).toEqual({
|
|
360
|
+
running: false,
|
|
361
|
+
status: "not found",
|
|
362
|
+
});
|
|
363
|
+
});
|
|
364
|
+
});
|
|
365
|
+
|
|
366
|
+
describe("resource limits", () => {
|
|
367
|
+
it("should apply default CPU limit of 1", () => {
|
|
368
|
+
const sandbox = new DockerSandbox(defaultConfig);
|
|
369
|
+
// The default is applied in constructor
|
|
370
|
+
expect(sandbox).toBeDefined();
|
|
371
|
+
});
|
|
372
|
+
|
|
373
|
+
it("should apply default memory limit of 2g", () => {
|
|
374
|
+
const sandbox = new DockerSandbox(defaultConfig);
|
|
375
|
+
expect(sandbox).toBeDefined();
|
|
376
|
+
});
|
|
377
|
+
|
|
378
|
+
it("should allow custom resource limits", () => {
|
|
379
|
+
const sandbox = new DockerSandbox({
|
|
380
|
+
...defaultConfig,
|
|
381
|
+
cpuLimit: "0.5",
|
|
382
|
+
memoryLimit: "512m",
|
|
383
|
+
});
|
|
384
|
+
expect(sandbox).toBeDefined();
|
|
385
|
+
});
|
|
386
|
+
});
|
|
387
|
+
|
|
388
|
+
describe("serve mode", () => {
|
|
389
|
+
it("should configure serve mode with port", async () => {
|
|
390
|
+
const { spawnSync } = await import("node:child_process");
|
|
391
|
+
const mockSpawnSync = vi.mocked(spawnSync);
|
|
392
|
+
|
|
393
|
+
// Mock image inspect
|
|
394
|
+
mockSpawnSync.mockReturnValueOnce({
|
|
395
|
+
status: 0,
|
|
396
|
+
stdout: "{}",
|
|
397
|
+
stderr: "",
|
|
398
|
+
pid: 1,
|
|
399
|
+
output: [],
|
|
400
|
+
signal: null,
|
|
401
|
+
});
|
|
402
|
+
|
|
403
|
+
// Mock docker run
|
|
404
|
+
mockSpawnSync.mockReturnValueOnce({
|
|
405
|
+
status: 0,
|
|
406
|
+
stdout: "container-id\n",
|
|
407
|
+
stderr: "",
|
|
408
|
+
pid: 1,
|
|
409
|
+
output: [],
|
|
410
|
+
signal: null,
|
|
411
|
+
});
|
|
412
|
+
|
|
413
|
+
const sandbox = new DockerSandbox({
|
|
414
|
+
...defaultConfig,
|
|
415
|
+
serveMode: true,
|
|
416
|
+
servePort: 3001,
|
|
417
|
+
});
|
|
418
|
+
|
|
419
|
+
await sandbox.pullImage();
|
|
420
|
+
await sandbox.start();
|
|
421
|
+
|
|
422
|
+
const runCall = mockSpawnSync.mock.calls.find(
|
|
423
|
+
(call) => call[0] === "docker" && call[1]?.[0] === "run",
|
|
424
|
+
);
|
|
425
|
+
|
|
426
|
+
expect(runCall).toBeDefined();
|
|
427
|
+
const args = runCall![1] as string[];
|
|
428
|
+
|
|
429
|
+
expect(args).toContain("-p");
|
|
430
|
+
expect(args).toContain("3001:3001");
|
|
431
|
+
expect(args).toContain("opencode");
|
|
432
|
+
expect(args).toContain("serve");
|
|
433
|
+
});
|
|
434
|
+
});
|
|
435
|
+
});
|
package/src/cli/build.ts
ADDED
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
import { spawnSync, execSync } from "node:child_process";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import pc from "picocolors";
|
|
4
|
+
|
|
5
|
+
function findProjectRoot(): string {
|
|
6
|
+
let dir = process.cwd();
|
|
7
|
+
for (let i = 0; i < 10; i++) {
|
|
8
|
+
const packageJson = path.join(dir, "package.json");
|
|
9
|
+
const pnpmWorkspace = path.join(dir, "pnpm-workspace.yaml");
|
|
10
|
+
try {
|
|
11
|
+
execSync(`test -f "${packageJson}" && test -f "${pnpmWorkspace}"`, { stdio: "ignore" });
|
|
12
|
+
return dir;
|
|
13
|
+
} catch {
|
|
14
|
+
dir = path.dirname(dir);
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
throw new Error("Could not find AgentMesh project root. Make sure you're in the agentmesh repository.");
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export interface BuildOptions {
|
|
21
|
+
docker?: boolean;
|
|
22
|
+
package?: string;
|
|
23
|
+
clean?: boolean;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export async function build(options: BuildOptions = {}): Promise<void> {
|
|
27
|
+
const projectRoot = findProjectRoot();
|
|
28
|
+
|
|
29
|
+
if (options.docker) {
|
|
30
|
+
await buildDocker(projectRoot, options);
|
|
31
|
+
} else {
|
|
32
|
+
await buildPackages(projectRoot, options);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
async function buildPackages(projectRoot: string, options: BuildOptions): Promise<void> {
|
|
37
|
+
console.log(pc.cyan("Building AgentMesh packages..."));
|
|
38
|
+
console.log();
|
|
39
|
+
|
|
40
|
+
const args = ["pnpm"];
|
|
41
|
+
|
|
42
|
+
if (options.package) {
|
|
43
|
+
args.push("--filter", options.package);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
if (options.clean) {
|
|
47
|
+
console.log(pc.dim("Cleaning build artifacts..."));
|
|
48
|
+
const cleanResult = spawnSync("pnpm", ["-r", "exec", "rm", "-rf", "dist"], {
|
|
49
|
+
cwd: projectRoot,
|
|
50
|
+
stdio: "inherit",
|
|
51
|
+
});
|
|
52
|
+
if (cleanResult.status !== 0) {
|
|
53
|
+
console.warn(pc.yellow("Warning: Clean may have partially failed"));
|
|
54
|
+
}
|
|
55
|
+
console.log();
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
args.push("build");
|
|
59
|
+
|
|
60
|
+
const result = spawnSync(args[0], args.slice(1), {
|
|
61
|
+
cwd: projectRoot,
|
|
62
|
+
stdio: "inherit",
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
if (result.status !== 0) {
|
|
66
|
+
console.error(pc.red("Build failed"));
|
|
67
|
+
process.exit(1);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
console.log();
|
|
71
|
+
console.log(pc.green("Build completed successfully!"));
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
async function buildDocker(projectRoot: string, options: BuildOptions): Promise<void> {
|
|
75
|
+
console.log(pc.cyan("Building Docker images..."));
|
|
76
|
+
console.log();
|
|
77
|
+
|
|
78
|
+
// Check if docker is available
|
|
79
|
+
try {
|
|
80
|
+
execSync("docker --version", { stdio: "ignore" });
|
|
81
|
+
} catch {
|
|
82
|
+
console.error(pc.red("Docker is not installed or not running"));
|
|
83
|
+
process.exit(1);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
// Use docker buildx bake for multi-image builds
|
|
87
|
+
const dockerDir = path.join(projectRoot, "docker");
|
|
88
|
+
const dockerfilePath = path.join(dockerDir, "Dockerfile");
|
|
89
|
+
|
|
90
|
+
try {
|
|
91
|
+
execSync(`test -f "${dockerfilePath}"`, { stdio: "ignore" });
|
|
92
|
+
} catch {
|
|
93
|
+
console.error(pc.red("Dockerfile not found at docker/Dockerfile"));
|
|
94
|
+
process.exit(1);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
// Build using docker compose to leverage the existing setup
|
|
98
|
+
const composePath = path.join(dockerDir, "docker-compose.local.yml");
|
|
99
|
+
|
|
100
|
+
const args = ["compose", "-f", composePath, "build"];
|
|
101
|
+
if (options.package) {
|
|
102
|
+
// Map package names to service names
|
|
103
|
+
const serviceMap: Record<string, string> = {
|
|
104
|
+
"@agentmesh/hub": "hub-api",
|
|
105
|
+
"@agentmesh/admin": "admin",
|
|
106
|
+
hub: "hub-api",
|
|
107
|
+
admin: "admin",
|
|
108
|
+
};
|
|
109
|
+
const service = serviceMap[options.package] || options.package;
|
|
110
|
+
args.push(service);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
const result = spawnSync("docker", args, {
|
|
114
|
+
cwd: projectRoot,
|
|
115
|
+
stdio: "inherit",
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
if (result.status !== 0) {
|
|
119
|
+
console.error(pc.red("Docker build failed"));
|
|
120
|
+
process.exit(1);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
console.log();
|
|
124
|
+
console.log(pc.green("Docker images built successfully!"));
|
|
125
|
+
|
|
126
|
+
// List built images
|
|
127
|
+
console.log();
|
|
128
|
+
console.log(pc.bold("Built images:"));
|
|
129
|
+
const listResult = spawnSync(
|
|
130
|
+
"docker",
|
|
131
|
+
["images", "--filter", "reference=*agentmesh*", "--format", "{{.Repository}}:{{.Tag}}\t{{.Size}}"],
|
|
132
|
+
{ encoding: "utf-8" }
|
|
133
|
+
);
|
|
134
|
+
if (listResult.stdout) {
|
|
135
|
+
console.log(pc.dim(listResult.stdout));
|
|
136
|
+
}
|
|
137
|
+
}
|