@alibaba-group/opensandbox 0.1.0 → 0.1.1-dev0
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/adapters/commandsAdapter.js +1 -0
- package/dist/adapters/commandsAdapter.js.map +1 -0
- package/dist/adapters/filesystemAdapter.js +1 -0
- package/dist/adapters/filesystemAdapter.js.map +1 -0
- package/dist/adapters/healthAdapter.js +1 -0
- package/dist/adapters/healthAdapter.js.map +1 -0
- package/dist/adapters/metricsAdapter.js +1 -0
- package/dist/adapters/metricsAdapter.js.map +1 -0
- package/dist/adapters/openapiError.js +1 -0
- package/dist/adapters/openapiError.js.map +1 -0
- package/dist/adapters/sandboxesAdapter.js +1 -0
- package/dist/adapters/sandboxesAdapter.js.map +1 -0
- package/dist/adapters/sse.js +1 -0
- package/dist/adapters/sse.js.map +1 -0
- package/dist/api/execd.js +1 -0
- package/dist/api/execd.js.map +1 -0
- package/dist/api/lifecycle.js +1 -0
- package/dist/api/lifecycle.js.map +1 -0
- package/dist/config/connection.d.ts.map +1 -1
- package/dist/config/connection.js +5 -2
- package/dist/config/connection.js.map +1 -0
- package/dist/core/constants.js +1 -0
- package/dist/core/constants.js.map +1 -0
- package/dist/core/exceptions.js +1 -0
- package/dist/core/exceptions.js.map +1 -0
- package/dist/factory/adapterFactory.js +1 -0
- package/dist/factory/adapterFactory.js.map +1 -0
- package/dist/factory/defaultAdapterFactory.js +1 -0
- package/dist/factory/defaultAdapterFactory.js.map +1 -0
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -0
- package/dist/internal.js +1 -0
- package/dist/internal.js.map +1 -0
- package/dist/manager.js +1 -0
- package/dist/manager.js.map +1 -0
- package/dist/models/execd.js +1 -0
- package/dist/models/execd.js.map +1 -0
- package/dist/models/execution.js +1 -0
- package/dist/models/execution.js.map +1 -0
- package/dist/models/executionEventDispatcher.js +1 -0
- package/dist/models/executionEventDispatcher.js.map +1 -0
- package/dist/models/filesystem.js +1 -0
- package/dist/models/filesystem.js.map +1 -0
- package/dist/models/sandboxes.js +1 -0
- package/dist/models/sandboxes.js.map +1 -0
- package/dist/openapi/execdClient.js +1 -0
- package/dist/openapi/execdClient.js.map +1 -0
- package/dist/openapi/lifecycleClient.js +1 -0
- package/dist/openapi/lifecycleClient.js.map +1 -0
- package/dist/sandbox.js +1 -0
- package/dist/sandbox.js.map +1 -0
- package/dist/services/execdCommands.js +1 -0
- package/dist/services/execdCommands.js.map +1 -0
- package/dist/services/execdHealth.js +1 -0
- package/dist/services/execdHealth.js.map +1 -0
- package/dist/services/execdMetrics.js +1 -0
- package/dist/services/execdMetrics.js.map +1 -0
- package/dist/services/filesystem.js +1 -0
- package/dist/services/filesystem.js.map +1 -0
- package/dist/services/sandboxes.js +1 -0
- package/dist/services/sandboxes.js.map +1 -0
- package/package.json +3 -2
- package/src/adapters/commandsAdapter.ts +112 -0
- package/src/adapters/filesystemAdapter.ts +575 -0
- package/src/adapters/healthAdapter.ts +27 -0
- package/src/adapters/metricsAdapter.ts +51 -0
- package/src/adapters/openapiError.ts +42 -0
- package/src/adapters/sandboxesAdapter.ts +187 -0
- package/src/adapters/sse.ts +95 -0
- package/src/api/execd.ts +1569 -0
- package/src/api/lifecycle.ts +801 -0
- package/src/config/connection.ts +377 -0
- package/src/core/constants.ts +29 -0
- package/src/core/exceptions.ts +134 -0
- package/src/factory/adapterFactory.ts +51 -0
- package/src/factory/defaultAdapterFactory.ts +69 -0
- package/src/index.ts +108 -0
- package/src/internal.ts +39 -0
- package/src/manager.ts +111 -0
- package/src/models/execd.ts +90 -0
- package/src/models/execution.ts +71 -0
- package/src/models/executionEventDispatcher.ts +97 -0
- package/src/models/filesystem.ts +103 -0
- package/src/models/sandboxes.ts +142 -0
- package/src/openapi/execdClient.ts +49 -0
- package/src/openapi/lifecycleClient.ts +70 -0
- package/src/sandbox.ts +459 -0
- package/src/services/execdCommands.ts +35 -0
- package/src/services/execdHealth.ts +17 -0
- package/src/services/execdMetrics.ts +19 -0
- package/src/services/filesystem.ts +47 -0
- package/src/services/sandboxes.ts +42 -0
package/src/sandbox.ts
ADDED
|
@@ -0,0 +1,459 @@
|
|
|
1
|
+
// Copyright 2026 Alibaba Group Holding Ltd.
|
|
2
|
+
//
|
|
3
|
+
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
// you may not use this file except in compliance with the License.
|
|
5
|
+
// You may obtain a copy of the License at
|
|
6
|
+
//
|
|
7
|
+
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
//
|
|
9
|
+
// Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
// See the License for the specific language governing permissions and
|
|
13
|
+
// limitations under the License.
|
|
14
|
+
|
|
15
|
+
import {
|
|
16
|
+
DEFAULT_ENTRYPOINT,
|
|
17
|
+
DEFAULT_EXECD_PORT,
|
|
18
|
+
DEFAULT_HEALTH_CHECK_POLLING_INTERVAL_MILLIS,
|
|
19
|
+
DEFAULT_READY_TIMEOUT_SECONDS,
|
|
20
|
+
DEFAULT_RESOURCE_LIMITS,
|
|
21
|
+
DEFAULT_TIMEOUT_SECONDS,
|
|
22
|
+
} from "./core/constants.js";
|
|
23
|
+
import { ConnectionConfig, type ConnectionConfigOptions } from "./config/connection.js";
|
|
24
|
+
import type { SandboxFiles } from "./services/filesystem.js";
|
|
25
|
+
import { createDefaultAdapterFactory } from "./factory/defaultAdapterFactory.js";
|
|
26
|
+
import type { AdapterFactory } from "./factory/adapterFactory.js";
|
|
27
|
+
|
|
28
|
+
import type { Sandboxes } from "./services/sandboxes.js";
|
|
29
|
+
import type { ExecdCommands } from "./services/execdCommands.js";
|
|
30
|
+
import type { ExecdHealth } from "./services/execdHealth.js";
|
|
31
|
+
import type { ExecdMetrics } from "./services/execdMetrics.js";
|
|
32
|
+
import type {
|
|
33
|
+
CreateSandboxRequest,
|
|
34
|
+
Endpoint,
|
|
35
|
+
RenewSandboxExpirationResponse,
|
|
36
|
+
SandboxId,
|
|
37
|
+
SandboxInfo,
|
|
38
|
+
} from "./models/sandboxes.js";
|
|
39
|
+
import { SandboxReadyTimeoutException } from "./core/exceptions.js";
|
|
40
|
+
|
|
41
|
+
export interface SandboxCreateOptions {
|
|
42
|
+
/**
|
|
43
|
+
* Connection configuration for calling the OpenSandbox Lifecycle API and the sandbox's execd API.
|
|
44
|
+
*/
|
|
45
|
+
connectionConfig?: ConnectionConfig | ConnectionConfigOptions;
|
|
46
|
+
/**
|
|
47
|
+
* Advanced override: inject a custom adapter factory (custom transports, dependency injection).
|
|
48
|
+
*/
|
|
49
|
+
adapterFactory?: AdapterFactory;
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Container image uri, e.g. `python:3.11`
|
|
53
|
+
*/
|
|
54
|
+
image:
|
|
55
|
+
| string
|
|
56
|
+
| { uri: string; auth?: { username: string; password: string } };
|
|
57
|
+
|
|
58
|
+
entrypoint?: string[];
|
|
59
|
+
env?: Record<string, string>;
|
|
60
|
+
metadata?: Record<string, string>;
|
|
61
|
+
extensions?: Record<string, string>;
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Resource limits applied to the sandbox container.
|
|
65
|
+
*
|
|
66
|
+
* This is forwarded to the Lifecycle API as `resourceLimits`.
|
|
67
|
+
*/
|
|
68
|
+
resource?: Record<string, string>;
|
|
69
|
+
/**
|
|
70
|
+
* Sandbox timeout in seconds.
|
|
71
|
+
*/
|
|
72
|
+
timeoutSeconds?: number;
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Skip readiness checks during create/connect.
|
|
76
|
+
*
|
|
77
|
+
* When true, the SDK will not wait for lifecycle state `Running` or perform the health check.
|
|
78
|
+
* The returned sandbox instance may not be ready yet.
|
|
79
|
+
*/
|
|
80
|
+
skipHealthCheck?: boolean;
|
|
81
|
+
/**
|
|
82
|
+
* Optional custom readiness check used by {@link Sandbox.waitUntilReady}.
|
|
83
|
+
*
|
|
84
|
+
* If provided, the SDK will call this function during readiness checks instead of
|
|
85
|
+
* using the default `execd` ping check.
|
|
86
|
+
*/
|
|
87
|
+
healthCheck?: (sbx: Sandbox) => boolean | Promise<boolean>;
|
|
88
|
+
readyTimeoutSeconds?: number;
|
|
89
|
+
healthCheckPollingInterval?: number;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
export interface SandboxConnectOptions {
|
|
93
|
+
connectionConfig?: ConnectionConfig | ConnectionConfigOptions;
|
|
94
|
+
adapterFactory?: AdapterFactory;
|
|
95
|
+
sandboxId: SandboxId;
|
|
96
|
+
|
|
97
|
+
skipHealthCheck?: boolean;
|
|
98
|
+
healthCheck?: (sbx: Sandbox) => boolean | Promise<boolean>;
|
|
99
|
+
readyTimeoutSeconds?: number;
|
|
100
|
+
healthCheckPollingInterval?: number;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
function sleep(ms: number): Promise<void> {
|
|
104
|
+
return new Promise((r) => setTimeout(r, ms));
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
function toImageSpec(
|
|
108
|
+
image: SandboxCreateOptions["image"]
|
|
109
|
+
): CreateSandboxRequest["image"] {
|
|
110
|
+
if (typeof image === "string") return { uri: image };
|
|
111
|
+
return { uri: image.uri, auth: image.auth };
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
export class Sandbox {
|
|
115
|
+
readonly id: SandboxId;
|
|
116
|
+
readonly connectionConfig: ConnectionConfig;
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Lifecycle (sandbox management) service.
|
|
120
|
+
*/
|
|
121
|
+
readonly sandboxes: Sandboxes;
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Execd services.
|
|
125
|
+
*/
|
|
126
|
+
readonly commands: ExecdCommands;
|
|
127
|
+
/**
|
|
128
|
+
* High-level filesystem facade (JS-friendly).
|
|
129
|
+
*/
|
|
130
|
+
readonly files: SandboxFiles;
|
|
131
|
+
readonly health: ExecdHealth;
|
|
132
|
+
readonly metrics: ExecdMetrics;
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* Internal state kept out of the public instance shape.
|
|
136
|
+
*
|
|
137
|
+
* This avoids nominal typing issues when multiple copies of the SDK exist in a dependency graph.
|
|
138
|
+
*/
|
|
139
|
+
private static readonly _priv = new WeakMap<
|
|
140
|
+
Sandbox,
|
|
141
|
+
{
|
|
142
|
+
adapterFactory: AdapterFactory;
|
|
143
|
+
lifecycleBaseUrl: string;
|
|
144
|
+
execdBaseUrl: string;
|
|
145
|
+
}
|
|
146
|
+
>();
|
|
147
|
+
|
|
148
|
+
private constructor(opts: {
|
|
149
|
+
id: SandboxId;
|
|
150
|
+
connectionConfig: ConnectionConfig;
|
|
151
|
+
adapterFactory: AdapterFactory;
|
|
152
|
+
lifecycleBaseUrl: string;
|
|
153
|
+
execdBaseUrl: string;
|
|
154
|
+
sandboxes: Sandboxes;
|
|
155
|
+
commands: ExecdCommands;
|
|
156
|
+
files: SandboxFiles;
|
|
157
|
+
health: ExecdHealth;
|
|
158
|
+
metrics: ExecdMetrics;
|
|
159
|
+
}) {
|
|
160
|
+
this.id = opts.id;
|
|
161
|
+
this.connectionConfig = opts.connectionConfig;
|
|
162
|
+
Sandbox._priv.set(this, {
|
|
163
|
+
adapterFactory: opts.adapterFactory,
|
|
164
|
+
lifecycleBaseUrl: opts.lifecycleBaseUrl,
|
|
165
|
+
execdBaseUrl: opts.execdBaseUrl,
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
this.sandboxes = opts.sandboxes;
|
|
169
|
+
this.commands = opts.commands;
|
|
170
|
+
this.files = opts.files;
|
|
171
|
+
this.health = opts.health;
|
|
172
|
+
this.metrics = opts.metrics;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
static async create(opts: SandboxCreateOptions): Promise<Sandbox> {
|
|
176
|
+
const baseConnectionConfig =
|
|
177
|
+
opts.connectionConfig instanceof ConnectionConfig
|
|
178
|
+
? opts.connectionConfig
|
|
179
|
+
: new ConnectionConfig(opts.connectionConfig);
|
|
180
|
+
const connectionConfig = baseConnectionConfig.withTransportIfMissing();
|
|
181
|
+
const lifecycleBaseUrl = connectionConfig.getBaseUrl();
|
|
182
|
+
const adapterFactory = opts.adapterFactory ?? createDefaultAdapterFactory();
|
|
183
|
+
|
|
184
|
+
let sandboxes: Sandboxes;
|
|
185
|
+
try {
|
|
186
|
+
sandboxes = adapterFactory.createLifecycleStack({
|
|
187
|
+
connectionConfig,
|
|
188
|
+
lifecycleBaseUrl,
|
|
189
|
+
}).sandboxes;
|
|
190
|
+
} catch (err) {
|
|
191
|
+
await connectionConfig.closeTransport();
|
|
192
|
+
throw err;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
const req: CreateSandboxRequest = {
|
|
196
|
+
image: toImageSpec(opts.image),
|
|
197
|
+
entrypoint: opts.entrypoint ?? DEFAULT_ENTRYPOINT,
|
|
198
|
+
timeout: Math.floor(opts.timeoutSeconds ?? DEFAULT_TIMEOUT_SECONDS),
|
|
199
|
+
resourceLimits: opts.resource ?? DEFAULT_RESOURCE_LIMITS,
|
|
200
|
+
env: opts.env ?? {},
|
|
201
|
+
metadata: opts.metadata ?? {},
|
|
202
|
+
extensions: opts.extensions ?? {},
|
|
203
|
+
};
|
|
204
|
+
|
|
205
|
+
let sandboxId: SandboxId | undefined;
|
|
206
|
+
try {
|
|
207
|
+
const created = await sandboxes.createSandbox(req);
|
|
208
|
+
sandboxId = created.id as SandboxId;
|
|
209
|
+
|
|
210
|
+
const endpoint = await sandboxes.getSandboxEndpoint(
|
|
211
|
+
sandboxId,
|
|
212
|
+
DEFAULT_EXECD_PORT
|
|
213
|
+
);
|
|
214
|
+
const execdBaseUrl = `${connectionConfig.protocol}://${endpoint.endpoint}`;
|
|
215
|
+
|
|
216
|
+
const { commands, files, health, metrics } =
|
|
217
|
+
adapterFactory.createExecdStack({
|
|
218
|
+
connectionConfig,
|
|
219
|
+
execdBaseUrl,
|
|
220
|
+
});
|
|
221
|
+
|
|
222
|
+
const sbx = new Sandbox({
|
|
223
|
+
id: sandboxId,
|
|
224
|
+
connectionConfig,
|
|
225
|
+
adapterFactory,
|
|
226
|
+
lifecycleBaseUrl,
|
|
227
|
+
execdBaseUrl,
|
|
228
|
+
sandboxes,
|
|
229
|
+
commands,
|
|
230
|
+
files,
|
|
231
|
+
health,
|
|
232
|
+
metrics,
|
|
233
|
+
});
|
|
234
|
+
|
|
235
|
+
if (!(opts.skipHealthCheck ?? false)) {
|
|
236
|
+
await sbx.waitUntilReady({
|
|
237
|
+
readyTimeoutSeconds:
|
|
238
|
+
opts.readyTimeoutSeconds ?? DEFAULT_READY_TIMEOUT_SECONDS,
|
|
239
|
+
pollingIntervalMillis:
|
|
240
|
+
opts.healthCheckPollingInterval ??
|
|
241
|
+
DEFAULT_HEALTH_CHECK_POLLING_INTERVAL_MILLIS,
|
|
242
|
+
healthCheck: opts.healthCheck,
|
|
243
|
+
});
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
return sbx;
|
|
247
|
+
} catch (err) {
|
|
248
|
+
if (sandboxId) {
|
|
249
|
+
try {
|
|
250
|
+
await sandboxes.deleteSandbox(sandboxId);
|
|
251
|
+
} catch {
|
|
252
|
+
// Ignore cleanup failure; surface original error.
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
await connectionConfig.closeTransport();
|
|
256
|
+
throw err;
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
static async connect(opts: SandboxConnectOptions): Promise<Sandbox> {
|
|
261
|
+
const baseConnectionConfig =
|
|
262
|
+
opts.connectionConfig instanceof ConnectionConfig
|
|
263
|
+
? opts.connectionConfig
|
|
264
|
+
: new ConnectionConfig(opts.connectionConfig);
|
|
265
|
+
const connectionConfig = baseConnectionConfig.withTransportIfMissing();
|
|
266
|
+
const adapterFactory = opts.adapterFactory ?? createDefaultAdapterFactory();
|
|
267
|
+
const lifecycleBaseUrl = connectionConfig.getBaseUrl();
|
|
268
|
+
|
|
269
|
+
let sandboxes: Sandboxes;
|
|
270
|
+
try {
|
|
271
|
+
sandboxes = adapterFactory.createLifecycleStack({
|
|
272
|
+
connectionConfig,
|
|
273
|
+
lifecycleBaseUrl,
|
|
274
|
+
}).sandboxes;
|
|
275
|
+
} catch (err) {
|
|
276
|
+
await connectionConfig.closeTransport();
|
|
277
|
+
throw err;
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
try {
|
|
281
|
+
const endpoint = await sandboxes.getSandboxEndpoint(
|
|
282
|
+
opts.sandboxId,
|
|
283
|
+
DEFAULT_EXECD_PORT
|
|
284
|
+
);
|
|
285
|
+
const execdBaseUrl = `${connectionConfig.protocol}://${endpoint.endpoint}`;
|
|
286
|
+
const { commands, files, health, metrics } =
|
|
287
|
+
adapterFactory.createExecdStack({
|
|
288
|
+
connectionConfig,
|
|
289
|
+
execdBaseUrl,
|
|
290
|
+
});
|
|
291
|
+
|
|
292
|
+
const sbx = new Sandbox({
|
|
293
|
+
id: opts.sandboxId,
|
|
294
|
+
connectionConfig,
|
|
295
|
+
adapterFactory,
|
|
296
|
+
lifecycleBaseUrl,
|
|
297
|
+
execdBaseUrl,
|
|
298
|
+
sandboxes,
|
|
299
|
+
commands,
|
|
300
|
+
files,
|
|
301
|
+
health,
|
|
302
|
+
metrics,
|
|
303
|
+
});
|
|
304
|
+
|
|
305
|
+
if (!(opts.skipHealthCheck ?? false)) {
|
|
306
|
+
await sbx.waitUntilReady({
|
|
307
|
+
readyTimeoutSeconds:
|
|
308
|
+
opts.readyTimeoutSeconds ?? DEFAULT_READY_TIMEOUT_SECONDS,
|
|
309
|
+
pollingIntervalMillis:
|
|
310
|
+
opts.healthCheckPollingInterval ??
|
|
311
|
+
DEFAULT_HEALTH_CHECK_POLLING_INTERVAL_MILLIS,
|
|
312
|
+
healthCheck: opts.healthCheck,
|
|
313
|
+
});
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
return sbx;
|
|
317
|
+
} catch (err) {
|
|
318
|
+
await connectionConfig.closeTransport();
|
|
319
|
+
throw err;
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
async getInfo(): Promise<SandboxInfo> {
|
|
324
|
+
return await this.sandboxes.getSandbox(this.id);
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
async isHealthy(): Promise<boolean> {
|
|
328
|
+
try {
|
|
329
|
+
return await this.health.ping();
|
|
330
|
+
} catch {
|
|
331
|
+
return false;
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
async getMetrics() {
|
|
336
|
+
return await this.metrics.getMetrics();
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
async pause(): Promise<void> {
|
|
340
|
+
await this.sandboxes.pauseSandbox(this.id);
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
/**
|
|
344
|
+
* Resume a paused sandbox and return a fresh, connected Sandbox instance.
|
|
345
|
+
*
|
|
346
|
+
* After resume, the execd endpoint may change, so this method returns a new
|
|
347
|
+
* {@link Sandbox} instance with a refreshed execd base URL.
|
|
348
|
+
*/
|
|
349
|
+
async resume(
|
|
350
|
+
opts: {
|
|
351
|
+
skipHealthCheck?: boolean;
|
|
352
|
+
readyTimeoutSeconds?: number;
|
|
353
|
+
healthCheckPollingInterval?: number;
|
|
354
|
+
} = {}
|
|
355
|
+
): Promise<Sandbox> {
|
|
356
|
+
await this.sandboxes.resumeSandbox(this.id);
|
|
357
|
+
return await Sandbox.connect({
|
|
358
|
+
sandboxId: this.id,
|
|
359
|
+
connectionConfig: this.connectionConfig,
|
|
360
|
+
adapterFactory: Sandbox._priv.get(this)!.adapterFactory,
|
|
361
|
+
skipHealthCheck: opts.skipHealthCheck ?? false,
|
|
362
|
+
readyTimeoutSeconds: opts.readyTimeoutSeconds,
|
|
363
|
+
healthCheckPollingInterval: opts.healthCheckPollingInterval,
|
|
364
|
+
});
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
/**
|
|
368
|
+
* Resume a paused sandbox by id, then connect to its execd endpoint.
|
|
369
|
+
*/
|
|
370
|
+
static async resume(opts: SandboxConnectOptions): Promise<Sandbox> {
|
|
371
|
+
const baseConnectionConfig =
|
|
372
|
+
opts.connectionConfig instanceof ConnectionConfig
|
|
373
|
+
? opts.connectionConfig
|
|
374
|
+
: new ConnectionConfig(opts.connectionConfig);
|
|
375
|
+
const adapterFactory = opts.adapterFactory ?? createDefaultAdapterFactory();
|
|
376
|
+
const resumeConnectionConfig = baseConnectionConfig.withTransportIfMissing();
|
|
377
|
+
const lifecycleBaseUrl = resumeConnectionConfig.getBaseUrl();
|
|
378
|
+
|
|
379
|
+
let sandboxes: Sandboxes;
|
|
380
|
+
try {
|
|
381
|
+
sandboxes = adapterFactory.createLifecycleStack({
|
|
382
|
+
connectionConfig: resumeConnectionConfig,
|
|
383
|
+
lifecycleBaseUrl,
|
|
384
|
+
}).sandboxes;
|
|
385
|
+
await sandboxes.resumeSandbox(opts.sandboxId);
|
|
386
|
+
} catch (err) {
|
|
387
|
+
await resumeConnectionConfig.closeTransport();
|
|
388
|
+
throw err;
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
await resumeConnectionConfig.closeTransport();
|
|
392
|
+
return await Sandbox.connect({ ...opts, connectionConfig: baseConnectionConfig, adapterFactory });
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
async kill(): Promise<void> {
|
|
396
|
+
await this.sandboxes.deleteSandbox(this.id);
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
/**
|
|
400
|
+
* Release any client-side resources (e.g. Node.js HTTP agents) owned by this Sandbox instance.
|
|
401
|
+
*/
|
|
402
|
+
async close(): Promise<void> {
|
|
403
|
+
await this.connectionConfig.closeTransport();
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
/**
|
|
407
|
+
* Renew expiration by setting expiresAt to now + timeoutSeconds.
|
|
408
|
+
*/
|
|
409
|
+
async renew(timeoutSeconds: number): Promise<RenewSandboxExpirationResponse> {
|
|
410
|
+
const expiresAt = new Date(
|
|
411
|
+
Date.now() + timeoutSeconds * 1000
|
|
412
|
+
).toISOString();
|
|
413
|
+
return await this.sandboxes.renewSandboxExpiration(this.id, { expiresAt });
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
/**
|
|
417
|
+
* Get sandbox endpoint for a port (STRICT: no scheme), e.g. "localhost:44772" or "domain/route/.../44772".
|
|
418
|
+
*/
|
|
419
|
+
async getEndpoint(port: number): Promise<Endpoint> {
|
|
420
|
+
return await this.sandboxes.getSandboxEndpoint(this.id, port);
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
/**
|
|
424
|
+
* Get absolute endpoint URL with scheme (convenience for HTTP clients).
|
|
425
|
+
*/
|
|
426
|
+
async getEndpointUrl(port: number): Promise<string> {
|
|
427
|
+
const ep = await this.getEndpoint(port);
|
|
428
|
+
return `${this.connectionConfig.protocol}://${ep.endpoint}`;
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
async waitUntilReady(opts: {
|
|
432
|
+
readyTimeoutSeconds: number;
|
|
433
|
+
pollingIntervalMillis: number;
|
|
434
|
+
healthCheck?: (sbx: Sandbox) => boolean | Promise<boolean>;
|
|
435
|
+
}): Promise<void> {
|
|
436
|
+
const deadline = Date.now() + opts.readyTimeoutSeconds * 1000;
|
|
437
|
+
|
|
438
|
+
// Wait until execd becomes reachable and passes health check.
|
|
439
|
+
while (true) {
|
|
440
|
+
if (Date.now() > deadline) {
|
|
441
|
+
throw new SandboxReadyTimeoutException({
|
|
442
|
+
message: `Sandbox not ready: timed out waiting for health check (timeoutSeconds=${opts.readyTimeoutSeconds})`,
|
|
443
|
+
});
|
|
444
|
+
}
|
|
445
|
+
try {
|
|
446
|
+
if (opts.healthCheck) {
|
|
447
|
+
const ok = await opts.healthCheck(this);
|
|
448
|
+
if (ok) return;
|
|
449
|
+
} else {
|
|
450
|
+
const ok = await this.health.ping();
|
|
451
|
+
if (ok) return;
|
|
452
|
+
}
|
|
453
|
+
} catch {
|
|
454
|
+
// ignore and retry
|
|
455
|
+
}
|
|
456
|
+
await sleep(opts.pollingIntervalMillis);
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
// Copyright 2026 Alibaba Group Holding Ltd.
|
|
2
|
+
//
|
|
3
|
+
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
// you may not use this file except in compliance with the License.
|
|
5
|
+
// You may obtain a copy of the License at
|
|
6
|
+
//
|
|
7
|
+
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
//
|
|
9
|
+
// Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
// See the License for the specific language governing permissions and
|
|
13
|
+
// limitations under the License.
|
|
14
|
+
|
|
15
|
+
import type { ExecutionHandlers } from "../models/execution.js";
|
|
16
|
+
import type { CommandExecution, RunCommandOpts, ServerStreamEvent } from "../models/execd.js";
|
|
17
|
+
|
|
18
|
+
export interface ExecdCommands {
|
|
19
|
+
/**
|
|
20
|
+
* Run a command and stream server events (SSE). This is the lowest-level API.
|
|
21
|
+
*/
|
|
22
|
+
runStream(command: string, opts?: RunCommandOpts, signal?: AbortSignal): AsyncIterable<ServerStreamEvent>;
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Convenience: run a command, consume the stream, and build a structured execution result.
|
|
26
|
+
*/
|
|
27
|
+
run(command: string, opts?: RunCommandOpts, handlers?: ExecutionHandlers, signal?: AbortSignal): Promise<CommandExecution>;
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Interrupt the current execution in the given context/session.
|
|
31
|
+
*
|
|
32
|
+
* Note: Execd spec uses `DELETE /command?id=<sessionId>`.
|
|
33
|
+
*/
|
|
34
|
+
interrupt(sessionId: string): Promise<void>;
|
|
35
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
// Copyright 2026 Alibaba Group Holding Ltd.
|
|
2
|
+
//
|
|
3
|
+
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
// you may not use this file except in compliance with the License.
|
|
5
|
+
// You may obtain a copy of the License at
|
|
6
|
+
//
|
|
7
|
+
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
//
|
|
9
|
+
// Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
// See the License for the specific language governing permissions and
|
|
13
|
+
// limitations under the License.
|
|
14
|
+
|
|
15
|
+
export interface ExecdHealth {
|
|
16
|
+
ping(): Promise<boolean>;
|
|
17
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
// Copyright 2026 Alibaba Group Holding Ltd.
|
|
2
|
+
//
|
|
3
|
+
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
// you may not use this file except in compliance with the License.
|
|
5
|
+
// You may obtain a copy of the License at
|
|
6
|
+
//
|
|
7
|
+
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
//
|
|
9
|
+
// Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
// See the License for the specific language governing permissions and
|
|
13
|
+
// limitations under the License.
|
|
14
|
+
|
|
15
|
+
import type { SandboxMetrics } from "../models/execd.js";
|
|
16
|
+
|
|
17
|
+
export interface ExecdMetrics {
|
|
18
|
+
getMetrics(): Promise<SandboxMetrics>;
|
|
19
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
// Copyright 2026 Alibaba Group Holding Ltd.
|
|
2
|
+
//
|
|
3
|
+
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
// you may not use this file except in compliance with the License.
|
|
5
|
+
// You may obtain a copy of the License at
|
|
6
|
+
//
|
|
7
|
+
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
//
|
|
9
|
+
// Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
// See the License for the specific language governing permissions and
|
|
13
|
+
// limitations under the License.
|
|
14
|
+
|
|
15
|
+
import type { SearchFilesResponse } from "../models/filesystem.js";
|
|
16
|
+
import type {
|
|
17
|
+
ContentReplaceEntry,
|
|
18
|
+
FileInfo,
|
|
19
|
+
MoveEntry,
|
|
20
|
+
SearchEntry,
|
|
21
|
+
SetPermissionEntry,
|
|
22
|
+
WriteEntry,
|
|
23
|
+
} from "../models/filesystem.js";
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* High-level filesystem facade (JS-friendly).
|
|
27
|
+
*
|
|
28
|
+
* This interface provides a convenience layer over the underlying execd filesystem API:
|
|
29
|
+
* it offers common operations (read/write/search/move/delete) and supports streaming I/O for large files.
|
|
30
|
+
*/
|
|
31
|
+
export interface SandboxFiles {
|
|
32
|
+
getFileInfo(paths: string[]): Promise<Record<string, FileInfo>>;
|
|
33
|
+
search(entry: SearchEntry): Promise<SearchFilesResponse>;
|
|
34
|
+
|
|
35
|
+
createDirectories(entries: Pick<WriteEntry, "path" | "mode" | "owner" | "group">[]): Promise<void>;
|
|
36
|
+
deleteDirectories(paths: string[]): Promise<void>;
|
|
37
|
+
|
|
38
|
+
writeFiles(entries: WriteEntry[]): Promise<void>;
|
|
39
|
+
readFile(path: string, opts?: { encoding?: string; range?: string }): Promise<string>;
|
|
40
|
+
readBytes(path: string, opts?: { range?: string }): Promise<Uint8Array>;
|
|
41
|
+
readBytesStream(path: string, opts?: { range?: string }): AsyncIterable<Uint8Array>;
|
|
42
|
+
|
|
43
|
+
deleteFiles(paths: string[]): Promise<void>;
|
|
44
|
+
moveFiles(entries: MoveEntry[]): Promise<void>;
|
|
45
|
+
replaceContents(entries: ContentReplaceEntry[]): Promise<void>;
|
|
46
|
+
setPermissions(entries: SetPermissionEntry[]): Promise<void>;
|
|
47
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
// Copyright 2026 Alibaba Group Holding Ltd.
|
|
2
|
+
//
|
|
3
|
+
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
// you may not use this file except in compliance with the License.
|
|
5
|
+
// You may obtain a copy of the License at
|
|
6
|
+
//
|
|
7
|
+
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
//
|
|
9
|
+
// Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
// See the License for the specific language governing permissions and
|
|
13
|
+
// limitations under the License.
|
|
14
|
+
|
|
15
|
+
import type {
|
|
16
|
+
CreateSandboxRequest,
|
|
17
|
+
CreateSandboxResponse,
|
|
18
|
+
Endpoint,
|
|
19
|
+
ListSandboxesParams,
|
|
20
|
+
ListSandboxesResponse,
|
|
21
|
+
RenewSandboxExpirationRequest,
|
|
22
|
+
RenewSandboxExpirationResponse,
|
|
23
|
+
SandboxId,
|
|
24
|
+
SandboxInfo,
|
|
25
|
+
} from "../models/sandboxes.js";
|
|
26
|
+
|
|
27
|
+
export interface Sandboxes {
|
|
28
|
+
createSandbox(req: CreateSandboxRequest): Promise<CreateSandboxResponse>;
|
|
29
|
+
getSandbox(sandboxId: SandboxId): Promise<SandboxInfo>;
|
|
30
|
+
listSandboxes(params?: ListSandboxesParams): Promise<ListSandboxesResponse>;
|
|
31
|
+
deleteSandbox(sandboxId: SandboxId): Promise<void>;
|
|
32
|
+
|
|
33
|
+
pauseSandbox(sandboxId: SandboxId): Promise<void>;
|
|
34
|
+
resumeSandbox(sandboxId: SandboxId): Promise<void>;
|
|
35
|
+
|
|
36
|
+
renewSandboxExpiration(
|
|
37
|
+
sandboxId: SandboxId,
|
|
38
|
+
req: RenewSandboxExpirationRequest,
|
|
39
|
+
): Promise<RenewSandboxExpirationResponse>;
|
|
40
|
+
|
|
41
|
+
getSandboxEndpoint(sandboxId: SandboxId, port: number): Promise<Endpoint>;
|
|
42
|
+
}
|