@magpiecloud/mags 1.8.16 → 1.9.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/API.md +388 -0
- package/Mags-API.postman_collection.json +374 -0
- package/QUICKSTART.md +295 -0
- package/README.md +378 -95
- package/bin/mags.js +79 -44
- package/deploy-page.sh +171 -0
- package/index.js +0 -2
- package/mags +0 -0
- package/mags.sh +270 -0
- package/nodejs/README.md +197 -0
- package/nodejs/bin/mags.js +1882 -0
- package/nodejs/index.js +602 -0
- package/nodejs/package.json +45 -0
- package/package.json +3 -18
- package/python/INTEGRATION.md +800 -0
- package/python/README.md +161 -0
- package/python/dist/magpie_mags-1.3.8-py3-none-any.whl +0 -0
- package/python/dist/magpie_mags-1.3.8.tar.gz +0 -0
- package/python/examples/demo.py +181 -0
- package/python/pyproject.toml +39 -0
- package/python/src/magpie_mags.egg-info/PKG-INFO +186 -0
- package/python/src/magpie_mags.egg-info/SOURCES.txt +9 -0
- package/python/src/magpie_mags.egg-info/dependency_links.txt +1 -0
- package/python/src/magpie_mags.egg-info/requires.txt +1 -0
- package/python/src/magpie_mags.egg-info/top_level.txt +1 -0
- package/python/src/mags/__init__.py +6 -0
- package/python/src/mags/client.py +527 -0
- package/python/test_sdk.py +78 -0
- package/skill.md +153 -0
- package/test-sdk.js +68 -0
- package/website/api.html +1095 -0
- package/website/claude-skill.html +481 -0
- package/website/cookbook/hn-marketing.html +410 -0
- package/website/cookbook/hn-marketing.sh +42 -0
- package/website/cookbook.html +282 -0
- package/website/docs.html +677 -0
- package/website/env.js +4 -0
- package/website/index.html +801 -0
- package/website/llms.txt +334 -0
- package/website/login.html +108 -0
- package/website/mags.md +210 -0
- package/website/script.js +453 -0
- package/website/styles.css +1075 -0
- package/website/tokens.html +169 -0
- package/website/usage.html +185 -0
package/nodejs/index.js
ADDED
|
@@ -0,0 +1,602 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Mags SDK - Execute scripts on Magpie's instant VM infrastructure
|
|
3
|
+
* @module @magpiecloud/mags
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
const https = require('https');
|
|
7
|
+
const http = require('http');
|
|
8
|
+
const { URL } = require('url');
|
|
9
|
+
|
|
10
|
+
class MagsError extends Error {
|
|
11
|
+
constructor(message, statusCode) {
|
|
12
|
+
super(message);
|
|
13
|
+
this.name = 'MagsError';
|
|
14
|
+
this.statusCode = statusCode || null;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
class Mags {
|
|
19
|
+
/**
|
|
20
|
+
* Create a Mags client
|
|
21
|
+
* @param {object} options - Configuration options
|
|
22
|
+
* @param {string} options.apiUrl - API endpoint (default: https://api.magpiecloud.com)
|
|
23
|
+
* @param {string} options.apiToken - API token (required, or set MAGS_API_TOKEN env var)
|
|
24
|
+
* @param {number} options.timeout - Default request timeout in ms (default: 30000)
|
|
25
|
+
*/
|
|
26
|
+
constructor(options = {}) {
|
|
27
|
+
this.apiUrl = (options.apiUrl || process.env.MAGS_API_URL || 'https://api.magpiecloud.com').replace(/\/+$/, '');
|
|
28
|
+
this.apiToken = options.apiToken || process.env.MAGS_API_TOKEN || process.env.MAGS_TOKEN;
|
|
29
|
+
this.timeout = options.timeout || 30000;
|
|
30
|
+
|
|
31
|
+
if (!this.apiToken) {
|
|
32
|
+
throw new MagsError('API token required. Set MAGS_API_TOKEN or pass apiToken option.');
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
_request(method, path, body = null, params = null) {
|
|
37
|
+
return new Promise((resolve, reject) => {
|
|
38
|
+
const url = new URL(path, this.apiUrl);
|
|
39
|
+
if (params) {
|
|
40
|
+
for (const [k, v] of Object.entries(params)) {
|
|
41
|
+
url.searchParams.set(k, v);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
const isHttps = url.protocol === 'https:';
|
|
45
|
+
const lib = isHttps ? https : http;
|
|
46
|
+
|
|
47
|
+
const options = {
|
|
48
|
+
hostname: url.hostname,
|
|
49
|
+
port: url.port || (isHttps ? 443 : 80),
|
|
50
|
+
path: url.pathname + url.search,
|
|
51
|
+
method,
|
|
52
|
+
headers: {
|
|
53
|
+
'Authorization': `Bearer ${this.apiToken}`,
|
|
54
|
+
'Content-Type': 'application/json'
|
|
55
|
+
},
|
|
56
|
+
timeout: this.timeout
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
const req = lib.request(options, (res) => {
|
|
60
|
+
let data = '';
|
|
61
|
+
res.on('data', chunk => data += chunk);
|
|
62
|
+
res.on('end', () => {
|
|
63
|
+
try {
|
|
64
|
+
const parsed = JSON.parse(data);
|
|
65
|
+
if (res.statusCode >= 400) {
|
|
66
|
+
reject(new MagsError(parsed.error || parsed.message || `HTTP ${res.statusCode}`, res.statusCode));
|
|
67
|
+
} else {
|
|
68
|
+
resolve(parsed);
|
|
69
|
+
}
|
|
70
|
+
} catch {
|
|
71
|
+
if (res.statusCode >= 400) {
|
|
72
|
+
reject(new MagsError(data || `HTTP ${res.statusCode}`, res.statusCode));
|
|
73
|
+
} else {
|
|
74
|
+
resolve(data);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
});
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
req.on('error', reject);
|
|
81
|
+
req.on('timeout', () => {
|
|
82
|
+
req.destroy();
|
|
83
|
+
reject(new MagsError('Request timed out'));
|
|
84
|
+
});
|
|
85
|
+
if (body) req.write(JSON.stringify(body));
|
|
86
|
+
req.end();
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// ── Jobs ──────────────────────────────────────────────────────────
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Submit a job for execution
|
|
94
|
+
* @param {string} script - Script to execute
|
|
95
|
+
* @param {object} options - Job options
|
|
96
|
+
* @param {string} options.name - Job name
|
|
97
|
+
* @param {string} options.workspaceId - Persistent workspace ID
|
|
98
|
+
* @param {string} options.baseWorkspaceId - Read-only base workspace to mount
|
|
99
|
+
* @param {boolean} options.persistent - Keep VM alive after script
|
|
100
|
+
* @param {boolean} options.noSleep - Never auto-sleep (requires persistent)
|
|
101
|
+
* @param {boolean} options.ephemeral - No workspace/S3 sync (fastest)
|
|
102
|
+
* @param {string} options.startupCommand - Command to run when waking from sleep
|
|
103
|
+
* @param {object} options.environment - Environment variables
|
|
104
|
+
* @param {string[]} options.fileIds - File IDs from uploadFiles()
|
|
105
|
+
* @param {number} options.diskGb - Custom disk size in GB (default 2)
|
|
106
|
+
* @param {string} options.rootfsType - VM rootfs type: standard, claude, pi
|
|
107
|
+
* @returns {Promise<{request_id: string, status: string}>}
|
|
108
|
+
*/
|
|
109
|
+
async run(script, options = {}) {
|
|
110
|
+
if (options.ephemeral && options.workspaceId) {
|
|
111
|
+
throw new MagsError('Cannot use ephemeral with workspaceId');
|
|
112
|
+
}
|
|
113
|
+
if (options.ephemeral && options.persistent) {
|
|
114
|
+
throw new MagsError('Cannot use ephemeral with persistent');
|
|
115
|
+
}
|
|
116
|
+
if (options.noSleep && !options.persistent) {
|
|
117
|
+
throw new MagsError('noSleep requires persistent=true');
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
const payload = {
|
|
121
|
+
script,
|
|
122
|
+
type: 'inline',
|
|
123
|
+
persistent: options.persistent || false,
|
|
124
|
+
};
|
|
125
|
+
|
|
126
|
+
if (options.noSleep) payload.no_sleep = true;
|
|
127
|
+
if (options.name) payload.name = options.name;
|
|
128
|
+
if (!options.ephemeral && options.workspaceId) payload.workspace_id = options.workspaceId;
|
|
129
|
+
if (options.baseWorkspaceId) payload.base_workspace_id = options.baseWorkspaceId;
|
|
130
|
+
if (options.startupCommand) payload.startup_command = options.startupCommand;
|
|
131
|
+
if (options.environment) payload.environment = options.environment;
|
|
132
|
+
if (options.fileIds && options.fileIds.length > 0) payload.file_ids = options.fileIds;
|
|
133
|
+
if (options.diskGb) payload.disk_gb = options.diskGb;
|
|
134
|
+
if (options.rootfsType) payload.rootfs_type = options.rootfsType;
|
|
135
|
+
|
|
136
|
+
return this._request('POST', '/api/v1/mags-jobs', payload);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Run a job and wait for completion
|
|
141
|
+
* @param {string} script - Script to execute
|
|
142
|
+
* @param {object} options - Job options (same as run() plus timeout/pollInterval)
|
|
143
|
+
* @param {number} options.timeout - Timeout in ms (default: 60000)
|
|
144
|
+
* @param {number} options.pollInterval - Poll interval in ms (default: 1000)
|
|
145
|
+
* @returns {Promise<{requestId: string, status: string, exitCode: number, durationMs: number, logs: Array}>}
|
|
146
|
+
*/
|
|
147
|
+
async runAndWait(script, options = {}) {
|
|
148
|
+
const timeout = options.timeout || 60000;
|
|
149
|
+
const pollInterval = options.pollInterval || 1000;
|
|
150
|
+
const result = await this.run(script, options);
|
|
151
|
+
const requestId = result.request_id;
|
|
152
|
+
|
|
153
|
+
const startTime = Date.now();
|
|
154
|
+
while (Date.now() - startTime < timeout) {
|
|
155
|
+
const status = await this.status(requestId);
|
|
156
|
+
|
|
157
|
+
if (status.status === 'completed' || status.status === 'error') {
|
|
158
|
+
const logsResp = await this.logs(requestId);
|
|
159
|
+
return {
|
|
160
|
+
requestId,
|
|
161
|
+
status: status.status,
|
|
162
|
+
exitCode: status.exit_code,
|
|
163
|
+
durationMs: status.script_duration_ms,
|
|
164
|
+
logs: logsResp.logs || []
|
|
165
|
+
};
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
await new Promise(resolve => setTimeout(resolve, pollInterval));
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
throw new MagsError(`Job ${requestId} timed out after ${timeout}ms`);
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* Get job status
|
|
176
|
+
* @param {string} requestId - Job request ID
|
|
177
|
+
* @returns {Promise<object>}
|
|
178
|
+
*/
|
|
179
|
+
async status(requestId) {
|
|
180
|
+
return this._request('GET', `/api/v1/mags-jobs/${requestId}/status`);
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* Get job logs
|
|
185
|
+
* @param {string} requestId - Job request ID
|
|
186
|
+
* @returns {Promise<{logs: Array}>}
|
|
187
|
+
*/
|
|
188
|
+
async logs(requestId) {
|
|
189
|
+
return this._request('GET', `/api/v1/mags-jobs/${requestId}/logs`);
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* List recent jobs
|
|
194
|
+
* @param {object} options - Pagination options
|
|
195
|
+
* @param {number} options.page - Page number (default: 1)
|
|
196
|
+
* @param {number} options.pageSize - Page size (default: 20)
|
|
197
|
+
* @returns {Promise<{jobs: Array, total: number}>}
|
|
198
|
+
*/
|
|
199
|
+
async list(options = {}) {
|
|
200
|
+
const page = options.page || 1;
|
|
201
|
+
const pageSize = options.pageSize || 20;
|
|
202
|
+
return this._request('GET', `/api/v1/mags-jobs`, null, { page, page_size: pageSize });
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* Update a job's settings
|
|
207
|
+
* @param {string} requestId - Job request ID
|
|
208
|
+
* @param {object} options - Settings to update
|
|
209
|
+
* @param {string} options.startupCommand - Command to run when VM wakes from sleep
|
|
210
|
+
* @param {boolean} options.noSleep - If true, VM never auto-sleeps. If false, re-enables auto-sleep.
|
|
211
|
+
* @returns {Promise<object>}
|
|
212
|
+
*/
|
|
213
|
+
async updateJob(requestId, options = {}) {
|
|
214
|
+
const payload = {};
|
|
215
|
+
if (options.startupCommand !== undefined) payload.startup_command = options.startupCommand;
|
|
216
|
+
if (options.noSleep !== undefined) payload.no_sleep = options.noSleep;
|
|
217
|
+
return this._request('PATCH', `/api/v1/mags-jobs/${requestId}`, payload);
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/**
|
|
221
|
+
* Enable URL or SSH access for a job
|
|
222
|
+
* @param {string} requestId - Job request ID
|
|
223
|
+
* @param {number} port - Port to expose (default: 8080, use 22 for SSH)
|
|
224
|
+
* @returns {Promise<object>}
|
|
225
|
+
*/
|
|
226
|
+
async enableAccess(requestId, port = 8080) {
|
|
227
|
+
return this._request('POST', `/api/v1/mags-jobs/${requestId}/access`, { port });
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
/**
|
|
231
|
+
* Enable public URL access for a job's VM
|
|
232
|
+
* @param {string} nameOrId - Job name, workspace ID, or request ID
|
|
233
|
+
* @param {number} port - Port to expose (default: 8080)
|
|
234
|
+
* @returns {Promise<object>} Object with url and access details
|
|
235
|
+
*/
|
|
236
|
+
async url(nameOrId, port = 8080) {
|
|
237
|
+
const requestId = await this._resolveJobId(nameOrId);
|
|
238
|
+
const st = await this.status(requestId);
|
|
239
|
+
const resp = await this.enableAccess(requestId, port);
|
|
240
|
+
const subdomain = st.subdomain || resp.subdomain;
|
|
241
|
+
if (subdomain) {
|
|
242
|
+
resp.url = `https://${subdomain}.apps.magpiecloud.com`;
|
|
243
|
+
}
|
|
244
|
+
return resp;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
/**
|
|
248
|
+
* Stop a running job. Accepts a job ID, job name, or workspace ID.
|
|
249
|
+
* @param {string} nameOrId - Job name, workspace ID, or request ID
|
|
250
|
+
* @returns {Promise<object>}
|
|
251
|
+
*/
|
|
252
|
+
async stop(nameOrId) {
|
|
253
|
+
const requestId = await this._resolveJobId(nameOrId);
|
|
254
|
+
return this._request('POST', `/api/v1/mags-jobs/${requestId}/stop`);
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
/**
|
|
258
|
+
* Sync a running job's workspace to S3 without stopping the VM
|
|
259
|
+
* @param {string} requestId - Job request ID
|
|
260
|
+
* @returns {Promise<object>}
|
|
261
|
+
*/
|
|
262
|
+
async sync(requestId) {
|
|
263
|
+
return this._request('POST', `/api/v1/mags-jobs/${requestId}/sync`);
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
/**
|
|
267
|
+
* Create a new VM sandbox and wait until it's running.
|
|
268
|
+
* By default, data lives on local disk only (no S3 sync).
|
|
269
|
+
* Pass persistent: true to enable S3 data persistence.
|
|
270
|
+
* @param {string} name - Workspace name
|
|
271
|
+
* @param {object} options - Options
|
|
272
|
+
* @param {boolean} options.persistent - Enable S3 data persistence (default: false)
|
|
273
|
+
* @param {string} options.baseWorkspaceId - Read-only base workspace to mount
|
|
274
|
+
* @param {number} options.diskGb - Custom disk size in GB
|
|
275
|
+
* @param {number} options.timeout - Timeout in ms (default: 30000)
|
|
276
|
+
* @param {number} options.pollInterval - Poll interval in ms (default: 1000)
|
|
277
|
+
* @returns {Promise<{request_id: string, status: string}>}
|
|
278
|
+
*/
|
|
279
|
+
async new(name, options = {}) {
|
|
280
|
+
const timeout = options.timeout || 30000;
|
|
281
|
+
const pollInterval = options.pollInterval || 1000;
|
|
282
|
+
|
|
283
|
+
const result = await this.run('sleep infinity', {
|
|
284
|
+
workspaceId: name,
|
|
285
|
+
persistent: true,
|
|
286
|
+
baseWorkspaceId: options.baseWorkspaceId,
|
|
287
|
+
diskGb: options.diskGb,
|
|
288
|
+
rootfsType: options.rootfsType,
|
|
289
|
+
});
|
|
290
|
+
const requestId = result.request_id;
|
|
291
|
+
|
|
292
|
+
const startTime = Date.now();
|
|
293
|
+
while (Date.now() - startTime < timeout) {
|
|
294
|
+
const st = await this.status(requestId);
|
|
295
|
+
if (st.status === 'running' && st.vm_id) {
|
|
296
|
+
return { request_id: requestId, status: 'running' };
|
|
297
|
+
}
|
|
298
|
+
if (st.status === 'completed' || st.status === 'error') {
|
|
299
|
+
throw new MagsError(`Job ${requestId} ended unexpectedly: ${st.status}`);
|
|
300
|
+
}
|
|
301
|
+
await new Promise(resolve => setTimeout(resolve, pollInterval));
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
throw new MagsError(`Job ${requestId} did not start within ${timeout}ms`);
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
/**
|
|
308
|
+
* Find a running or sleeping job by name, workspace ID, or job ID
|
|
309
|
+
* @param {string} nameOrId - Job name, workspace ID, or request ID
|
|
310
|
+
* @returns {Promise<object|null>} The job object, or null if not found
|
|
311
|
+
*/
|
|
312
|
+
async findJob(nameOrId) {
|
|
313
|
+
const resp = await this.list({ pageSize: 50 });
|
|
314
|
+
const jobs = resp.jobs || [];
|
|
315
|
+
|
|
316
|
+
// Priority 1: exact name match, running/sleeping
|
|
317
|
+
for (const j of jobs) {
|
|
318
|
+
if (j.name === nameOrId && (j.status === 'running' || j.status === 'sleeping')) return j;
|
|
319
|
+
}
|
|
320
|
+
// Priority 2: workspace_id match, running/sleeping
|
|
321
|
+
for (const j of jobs) {
|
|
322
|
+
if (j.workspace_id === nameOrId && (j.status === 'running' || j.status === 'sleeping')) return j;
|
|
323
|
+
}
|
|
324
|
+
// Priority 3: exact name match, any status
|
|
325
|
+
for (const j of jobs) {
|
|
326
|
+
if (j.name === nameOrId) return j;
|
|
327
|
+
}
|
|
328
|
+
// Priority 4: workspace_id match, any status
|
|
329
|
+
for (const j of jobs) {
|
|
330
|
+
if (j.workspace_id === nameOrId) return j;
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
return null;
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
/**
|
|
337
|
+
* Execute a command on an existing running/sleeping VM via HTTP exec endpoint
|
|
338
|
+
* @param {string} nameOrId - Job name, workspace ID, or request ID
|
|
339
|
+
* @param {string} command - Command to execute
|
|
340
|
+
* @param {object} options - Options
|
|
341
|
+
* @param {number} options.timeout - Timeout in ms (default: 30000)
|
|
342
|
+
* @returns {Promise<{exitCode: number, output: string, stderr: string}>}
|
|
343
|
+
*/
|
|
344
|
+
async exec(nameOrId, command, options = {}) {
|
|
345
|
+
const timeout = options.timeout || 30000;
|
|
346
|
+
|
|
347
|
+
const job = await this.findJob(nameOrId);
|
|
348
|
+
if (!job) throw new MagsError(`No running or sleeping VM found for '${nameOrId}'`);
|
|
349
|
+
if (job.status !== 'running' && job.status !== 'sleeping') {
|
|
350
|
+
throw new MagsError(`VM for '${nameOrId}' is ${job.status}, needs to be running or sleeping`);
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
const requestId = job.request_id || job.id;
|
|
354
|
+
const resp = await this._request('POST', `/api/v1/mags-jobs/${requestId}/exec`, {
|
|
355
|
+
command,
|
|
356
|
+
timeout: Math.ceil(timeout / 1000),
|
|
357
|
+
});
|
|
358
|
+
|
|
359
|
+
return { exitCode: resp.exit_code, output: resp.stdout, stderr: resp.stderr };
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
/**
|
|
363
|
+
* Resize a workspace's disk. Stops the existing VM, then creates a new one.
|
|
364
|
+
* @param {string} workspace - Workspace name
|
|
365
|
+
* @param {number} diskGb - New disk size in GB
|
|
366
|
+
* @param {object} options - Options
|
|
367
|
+
* @param {number} options.timeout - Timeout in ms (default: 30000)
|
|
368
|
+
* @param {number} options.pollInterval - Poll interval in ms (default: 1000)
|
|
369
|
+
* @returns {Promise<{request_id: string, status: string}>}
|
|
370
|
+
*/
|
|
371
|
+
async resize(workspace, diskGb, options = {}) {
|
|
372
|
+
const existing = await this.findJob(workspace);
|
|
373
|
+
if (existing && existing.status === 'running') {
|
|
374
|
+
await this._request('POST', `/api/v1/mags-jobs/${existing.request_id}/sync`);
|
|
375
|
+
await this._request('POST', `/api/v1/mags-jobs/${existing.request_id}/stop`);
|
|
376
|
+
await new Promise(resolve => setTimeout(resolve, 1000));
|
|
377
|
+
} else if (existing && existing.status === 'sleeping') {
|
|
378
|
+
await this._request('POST', `/api/v1/mags-jobs/${existing.request_id}/stop`);
|
|
379
|
+
await new Promise(resolve => setTimeout(resolve, 1000));
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
return this.new(workspace, { persistent: true, diskGb, timeout: options.timeout, pollInterval: options.pollInterval });
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
/**
|
|
386
|
+
* Get aggregated usage summary
|
|
387
|
+
* @param {object} options - Options
|
|
388
|
+
* @param {number} options.windowDays - Time window in days (default: 30)
|
|
389
|
+
* @returns {Promise<object>}
|
|
390
|
+
*/
|
|
391
|
+
async usage(options = {}) {
|
|
392
|
+
return this._request('GET', '/api/v1/mags-jobs/usage', null, { window_days: options.windowDays || 30 });
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
// ── Workspaces ────────────────────────────────────────────────────
|
|
396
|
+
|
|
397
|
+
/**
|
|
398
|
+
* List all workspaces
|
|
399
|
+
* @returns {Promise<{workspaces: Array, total: number}>}
|
|
400
|
+
*/
|
|
401
|
+
async listWorkspaces() {
|
|
402
|
+
return this._request('GET', '/api/v1/mags-workspaces');
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
/**
|
|
406
|
+
* Delete a workspace and all its stored data
|
|
407
|
+
* @param {string} workspaceId - Workspace ID to delete
|
|
408
|
+
* @returns {Promise<object>}
|
|
409
|
+
*/
|
|
410
|
+
async deleteWorkspace(workspaceId) {
|
|
411
|
+
return this._request('DELETE', `/api/v1/mags-workspaces/${workspaceId}`);
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
// ── File uploads ──────────────────────────────────────────────────
|
|
415
|
+
|
|
416
|
+
/**
|
|
417
|
+
* Upload files for use in a job
|
|
418
|
+
* @param {string[]} filePaths - Array of local file paths
|
|
419
|
+
* @returns {Promise<string[]>} Array of file IDs
|
|
420
|
+
*/
|
|
421
|
+
async uploadFiles(filePaths) {
|
|
422
|
+
const fs = require('fs');
|
|
423
|
+
const path = require('path');
|
|
424
|
+
const fileIds = [];
|
|
425
|
+
|
|
426
|
+
for (const filePath of filePaths) {
|
|
427
|
+
const fileName = path.basename(filePath);
|
|
428
|
+
const fileData = fs.readFileSync(filePath);
|
|
429
|
+
const boundary = '----MagsBoundary' + Date.now().toString(16);
|
|
430
|
+
|
|
431
|
+
const parts = [];
|
|
432
|
+
parts.push(`--${boundary}\r\n`);
|
|
433
|
+
parts.push(`Content-Disposition: form-data; name="file"; filename="${fileName}"\r\n`);
|
|
434
|
+
parts.push(`Content-Type: application/octet-stream\r\n\r\n`);
|
|
435
|
+
const header = Buffer.from(parts.join(''));
|
|
436
|
+
const footer = Buffer.from(`\r\n--${boundary}--\r\n`);
|
|
437
|
+
const body = Buffer.concat([header, fileData, footer]);
|
|
438
|
+
|
|
439
|
+
const response = await this._multipartRequest('/api/v1/mags-files', body, boundary);
|
|
440
|
+
if (response.file_id) {
|
|
441
|
+
fileIds.push(response.file_id);
|
|
442
|
+
} else {
|
|
443
|
+
throw new MagsError(`Failed to upload file: ${fileName}`);
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
return fileIds;
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
_multipartRequest(apiPath, body, boundary) {
|
|
451
|
+
return new Promise((resolve, reject) => {
|
|
452
|
+
const url = new URL(apiPath, this.apiUrl);
|
|
453
|
+
const isHttps = url.protocol === 'https:';
|
|
454
|
+
const lib = isHttps ? https : http;
|
|
455
|
+
|
|
456
|
+
const options = {
|
|
457
|
+
hostname: url.hostname,
|
|
458
|
+
port: url.port || (isHttps ? 443 : 80),
|
|
459
|
+
path: url.pathname,
|
|
460
|
+
method: 'POST',
|
|
461
|
+
headers: {
|
|
462
|
+
'Authorization': `Bearer ${this.apiToken}`,
|
|
463
|
+
'Content-Type': `multipart/form-data; boundary=${boundary}`,
|
|
464
|
+
'Content-Length': body.length
|
|
465
|
+
}
|
|
466
|
+
};
|
|
467
|
+
|
|
468
|
+
const req = lib.request(options, (res) => {
|
|
469
|
+
let data = '';
|
|
470
|
+
res.on('data', chunk => data += chunk);
|
|
471
|
+
res.on('end', () => {
|
|
472
|
+
try {
|
|
473
|
+
resolve(JSON.parse(data));
|
|
474
|
+
} catch {
|
|
475
|
+
resolve(data);
|
|
476
|
+
}
|
|
477
|
+
});
|
|
478
|
+
});
|
|
479
|
+
|
|
480
|
+
req.on('error', reject);
|
|
481
|
+
req.write(body);
|
|
482
|
+
req.end();
|
|
483
|
+
});
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
// ── Cron jobs ─────────────────────────────────────────────────────
|
|
487
|
+
|
|
488
|
+
/**
|
|
489
|
+
* Create a cron job
|
|
490
|
+
* @param {object} options - Cron job options
|
|
491
|
+
* @param {string} options.name - Cron job name
|
|
492
|
+
* @param {string} options.cronExpression - Cron expression (e.g., "0 * * * *")
|
|
493
|
+
* @param {string} options.script - Script to execute
|
|
494
|
+
* @param {string} options.workspaceId - Workspace ID
|
|
495
|
+
* @param {object} options.environment - Environment variables
|
|
496
|
+
* @param {boolean} options.persistent - Keep VM alive
|
|
497
|
+
* @returns {Promise<object>}
|
|
498
|
+
*/
|
|
499
|
+
async cronCreate(options) {
|
|
500
|
+
const payload = {
|
|
501
|
+
name: options.name,
|
|
502
|
+
cron_expression: options.cronExpression,
|
|
503
|
+
script: options.script,
|
|
504
|
+
persistent: options.persistent || false
|
|
505
|
+
};
|
|
506
|
+
if (options.workspaceId) payload.workspace_id = options.workspaceId;
|
|
507
|
+
if (options.environment) payload.environment = options.environment;
|
|
508
|
+
return this._request('POST', '/api/v1/mags-cron', payload);
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
/**
|
|
512
|
+
* List cron jobs
|
|
513
|
+
* @returns {Promise<{cron_jobs: Array}>}
|
|
514
|
+
*/
|
|
515
|
+
async cronList() {
|
|
516
|
+
return this._request('GET', '/api/v1/mags-cron');
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
/**
|
|
520
|
+
* Get a cron job
|
|
521
|
+
* @param {string} id - Cron job ID
|
|
522
|
+
* @returns {Promise<object>}
|
|
523
|
+
*/
|
|
524
|
+
async cronGet(id) {
|
|
525
|
+
return this._request('GET', `/api/v1/mags-cron/${id}`);
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
/**
|
|
529
|
+
* Update a cron job
|
|
530
|
+
* @param {string} id - Cron job ID
|
|
531
|
+
* @param {object} updates - Fields to update
|
|
532
|
+
* @returns {Promise<object>}
|
|
533
|
+
*/
|
|
534
|
+
async cronUpdate(id, updates) {
|
|
535
|
+
return this._request('PATCH', `/api/v1/mags-cron/${id}`, updates);
|
|
536
|
+
}
|
|
537
|
+
|
|
538
|
+
/**
|
|
539
|
+
* Delete a cron job
|
|
540
|
+
* @param {string} id - Cron job ID
|
|
541
|
+
* @returns {Promise<object>}
|
|
542
|
+
*/
|
|
543
|
+
async cronDelete(id) {
|
|
544
|
+
return this._request('DELETE', `/api/v1/mags-cron/${id}`);
|
|
545
|
+
}
|
|
546
|
+
|
|
547
|
+
// ── URL aliases ───────────────────────────────────────────────────
|
|
548
|
+
|
|
549
|
+
/**
|
|
550
|
+
* Create a stable URL alias for a workspace
|
|
551
|
+
* @param {string} subdomain - Subdomain for the alias
|
|
552
|
+
* @param {string} workspaceId - Workspace to point to
|
|
553
|
+
* @param {string} domain - Domain (default: apps.magpiecloud.com)
|
|
554
|
+
* @returns {Promise<{id: string, subdomain: string, url: string}>}
|
|
555
|
+
*/
|
|
556
|
+
async urlAliasCreate(subdomain, workspaceId, domain = 'apps.magpiecloud.com') {
|
|
557
|
+
return this._request('POST', '/api/v1/mags-url-aliases', {
|
|
558
|
+
subdomain,
|
|
559
|
+
workspace_id: workspaceId,
|
|
560
|
+
domain,
|
|
561
|
+
});
|
|
562
|
+
}
|
|
563
|
+
|
|
564
|
+
/**
|
|
565
|
+
* List all URL aliases
|
|
566
|
+
* @returns {Promise<{aliases: Array, total: number}>}
|
|
567
|
+
*/
|
|
568
|
+
async urlAliasList() {
|
|
569
|
+
return this._request('GET', '/api/v1/mags-url-aliases');
|
|
570
|
+
}
|
|
571
|
+
|
|
572
|
+
/**
|
|
573
|
+
* Delete a URL alias by subdomain
|
|
574
|
+
* @param {string} subdomain - Subdomain to delete
|
|
575
|
+
* @returns {Promise<object>}
|
|
576
|
+
*/
|
|
577
|
+
async urlAliasDelete(subdomain) {
|
|
578
|
+
return this._request('DELETE', `/api/v1/mags-url-aliases/${subdomain}`);
|
|
579
|
+
}
|
|
580
|
+
|
|
581
|
+
// ── Internal helpers ──────────────────────────────────────────────
|
|
582
|
+
|
|
583
|
+
/**
|
|
584
|
+
* Resolve a job name, workspace ID, or UUID to a request_id
|
|
585
|
+
* @param {string} nameOrId - Name, workspace ID, or request ID
|
|
586
|
+
* @returns {Promise<string>} The resolved request_id
|
|
587
|
+
*/
|
|
588
|
+
async _resolveJobId(nameOrId) {
|
|
589
|
+
// If it looks like a UUID, use directly
|
|
590
|
+
if (nameOrId.length >= 32 && nameOrId.includes('-')) {
|
|
591
|
+
return nameOrId;
|
|
592
|
+
}
|
|
593
|
+
const job = await this.findJob(nameOrId);
|
|
594
|
+
if (!job) throw new MagsError(`No job found for '${nameOrId}'`);
|
|
595
|
+
return job.request_id || job.id;
|
|
596
|
+
}
|
|
597
|
+
}
|
|
598
|
+
|
|
599
|
+
module.exports = Mags;
|
|
600
|
+
module.exports.Mags = Mags;
|
|
601
|
+
module.exports.MagsError = MagsError;
|
|
602
|
+
module.exports.default = Mags;
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@magpiecloud/mags",
|
|
3
|
+
"version": "1.8.16",
|
|
4
|
+
"description": "Mags CLI & SDK - Execute commands and scripts on Mags Sandboxes",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"bin": {
|
|
7
|
+
"mags": "./bin/mags.js"
|
|
8
|
+
},
|
|
9
|
+
"scripts": {
|
|
10
|
+
"test": "node bin/mags.js --help"
|
|
11
|
+
},
|
|
12
|
+
"keywords": [
|
|
13
|
+
"magpie",
|
|
14
|
+
"mags",
|
|
15
|
+
"vm",
|
|
16
|
+
"microvm",
|
|
17
|
+
"cloud",
|
|
18
|
+
"serverless",
|
|
19
|
+
"execution",
|
|
20
|
+
"cli",
|
|
21
|
+
"claude",
|
|
22
|
+
"claude-code"
|
|
23
|
+
],
|
|
24
|
+
"author": "Magpie Cloud",
|
|
25
|
+
"license": "MIT",
|
|
26
|
+
"repository": {
|
|
27
|
+
"type": "git",
|
|
28
|
+
"url": "https://github.com/magpiecloud/mags"
|
|
29
|
+
},
|
|
30
|
+
"homepage": "https://mags.run",
|
|
31
|
+
"bugs": {
|
|
32
|
+
"url": "https://github.com/magpiecloud/mags/issues"
|
|
33
|
+
},
|
|
34
|
+
"engines": {
|
|
35
|
+
"node": ">=14.0.0"
|
|
36
|
+
},
|
|
37
|
+
"dependencies": {
|
|
38
|
+
"ws": "^8.18.0"
|
|
39
|
+
},
|
|
40
|
+
"files": [
|
|
41
|
+
"index.js",
|
|
42
|
+
"bin/mags.js",
|
|
43
|
+
"README.md"
|
|
44
|
+
]
|
|
45
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@magpiecloud/mags",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.9.0",
|
|
4
4
|
"description": "Mags CLI & SDK - Execute commands and scripts on Mags Sandboxes",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"bin": {
|
|
@@ -16,30 +16,15 @@
|
|
|
16
16
|
"microvm",
|
|
17
17
|
"cloud",
|
|
18
18
|
"serverless",
|
|
19
|
-
"execution"
|
|
20
|
-
"cli",
|
|
21
|
-
"claude",
|
|
22
|
-
"claude-code"
|
|
19
|
+
"execution"
|
|
23
20
|
],
|
|
24
21
|
"author": "Magpie Cloud",
|
|
25
22
|
"license": "MIT",
|
|
26
|
-
"repository": {
|
|
27
|
-
"type": "git",
|
|
28
|
-
"url": "https://github.com/magpiecloud/mags"
|
|
29
|
-
},
|
|
30
23
|
"homepage": "https://mags.run",
|
|
31
|
-
"bugs": {
|
|
32
|
-
"url": "https://github.com/magpiecloud/mags/issues"
|
|
33
|
-
},
|
|
34
24
|
"engines": {
|
|
35
25
|
"node": ">=14.0.0"
|
|
36
26
|
},
|
|
37
27
|
"dependencies": {
|
|
38
28
|
"ws": "^8.18.0"
|
|
39
|
-
}
|
|
40
|
-
"files": [
|
|
41
|
-
"index.js",
|
|
42
|
-
"bin/mags.js",
|
|
43
|
-
"README.md"
|
|
44
|
-
]
|
|
29
|
+
}
|
|
45
30
|
}
|