@tarkahq/sdk 0.0.0-stage → 0.3.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/LICENSE +201 -0
- package/NOTICE +69 -0
- package/README.md +700 -2
- package/api-source.json +38 -0
- package/dist/cjs/client.d.ts +114 -0
- package/dist/cjs/client.js +174 -0
- package/dist/cjs/errors.d.ts +27 -0
- package/dist/cjs/errors.js +78 -0
- package/dist/cjs/generated/google/api/annotations.d.ts +1 -0
- package/dist/cjs/generated/google/api/annotations.js +10 -0
- package/dist/cjs/generated/google/api/http.d.ts +395 -0
- package/dist/cjs/generated/google/api/http.js +421 -0
- package/dist/cjs/generated/google/protobuf/descriptor.d.ts +873 -0
- package/dist/cjs/generated/google/protobuf/descriptor.js +3992 -0
- package/dist/cjs/generated/google/protobuf/empty.d.ts +35 -0
- package/dist/cjs/generated/google/protobuf/empty.js +58 -0
- package/dist/cjs/generated/google/protobuf/struct.d.ts +127 -0
- package/dist/cjs/generated/google/protobuf/struct.js +529 -0
- package/dist/cjs/generated/google/protobuf/timestamp.d.ts +128 -0
- package/dist/cjs/generated/google/protobuf/timestamp.js +95 -0
- package/dist/cjs/generated/protoc-gen-openapiv2/options/annotations.d.ts +1 -0
- package/dist/cjs/generated/protoc-gen-openapiv2/options/annotations.js +10 -0
- package/dist/cjs/generated/protoc-gen-openapiv2/options/openapiv2.d.ts +983 -0
- package/dist/cjs/generated/protoc-gen-openapiv2/options/openapiv2.js +4874 -0
- package/dist/cjs/generated/tarka/access/v1/access.d.ts +322 -0
- package/dist/cjs/generated/tarka/access/v1/access.js +2979 -0
- package/dist/cjs/generated/tarka/channels/v1/channels.d.ts +370 -0
- package/dist/cjs/generated/tarka/channels/v1/channels.js +3603 -0
- package/dist/cjs/generated/tarka/gateway/v1/gateway.d.ts +804 -0
- package/dist/cjs/generated/tarka/gateway/v1/gateway.js +8081 -0
- package/dist/cjs/generated/tarka/iam/v1/iam.d.ts +1291 -0
- package/dist/cjs/generated/tarka/iam/v1/iam.js +13207 -0
- package/dist/cjs/generated/tarka/inference/v1/inference.d.ts +699 -0
- package/dist/cjs/generated/tarka/inference/v1/inference.js +7058 -0
- package/dist/cjs/generated/tarka/provisioning/v1/agent_secrets.d.ts +70 -0
- package/dist/cjs/generated/tarka/provisioning/v1/agent_secrets.js +603 -0
- package/dist/cjs/generated/tarka/provisioning/v1/billing_quote.d.ts +171 -0
- package/dist/cjs/generated/tarka/provisioning/v1/billing_quote.js +1588 -0
- package/dist/cjs/generated/tarka/provisioning/v1/job.d.ts +168 -0
- package/dist/cjs/generated/tarka/provisioning/v1/job.js +1505 -0
- package/dist/cjs/generated/tarka/provisioning/v1/provisioning.d.ts +2644 -0
- package/dist/cjs/generated/tarka/provisioning/v1/provisioning.js +19501 -0
- package/dist/cjs/generated/tarka/provisioning/v1/registry_credentials.d.ts +89 -0
- package/dist/cjs/generated/tarka/provisioning/v1/registry_credentials.js +856 -0
- package/dist/cjs/generated/tarka/provisioning/v1/sandbox.d.ts +431 -0
- package/dist/cjs/generated/tarka/provisioning/v1/sandbox.js +3203 -0
- package/dist/cjs/generated/tarka/provisioning/v1/source_builds.d.ts +291 -0
- package/dist/cjs/generated/tarka/provisioning/v1/source_builds.js +3920 -0
- package/dist/cjs/grpc.d.ts +12 -0
- package/dist/cjs/grpc.js +55 -0
- package/dist/cjs/index.d.ts +6 -0
- package/dist/cjs/index.js +21 -0
- package/dist/cjs/package.json +1 -0
- package/dist/cjs/resources/inference.d.ts +293 -0
- package/dist/cjs/resources/inference.js +451 -0
- package/dist/cjs/resources/provisioning.d.ts +389 -0
- package/dist/cjs/resources/provisioning.js +552 -0
- package/dist/cjs/resources/sandboxes.d.ts +360 -0
- package/dist/cjs/resources/sandboxes.js +732 -0
- package/dist/cjs/rpc-types.d.ts +36 -0
- package/dist/cjs/rpc-types.js +29 -0
- package/dist/cjs/runtime.d.ts +99 -0
- package/dist/cjs/runtime.js +473 -0
- package/dist/cjs/transport.d.ts +60 -0
- package/dist/cjs/transport.js +114 -0
- package/dist/cjs/types.d.ts +309 -0
- package/dist/cjs/types.js +2 -0
- package/dist/esm/client.d.ts +114 -0
- package/dist/esm/client.js +169 -0
- package/dist/esm/errors.d.ts +27 -0
- package/dist/esm/errors.js +71 -0
- package/dist/esm/generated/google/api/annotations.d.ts +1 -0
- package/dist/esm/generated/google/api/annotations.js +7 -0
- package/dist/esm/generated/google/api/http.d.ts +395 -0
- package/dist/esm/generated/google/api/http.js +418 -0
- package/dist/esm/generated/google/protobuf/descriptor.d.ts +873 -0
- package/dist/esm/generated/google/protobuf/descriptor.js +3977 -0
- package/dist/esm/generated/google/protobuf/empty.d.ts +35 -0
- package/dist/esm/generated/google/protobuf/empty.js +55 -0
- package/dist/esm/generated/google/protobuf/struct.d.ts +127 -0
- package/dist/esm/generated/google/protobuf/struct.js +524 -0
- package/dist/esm/generated/google/protobuf/timestamp.d.ts +128 -0
- package/dist/esm/generated/google/protobuf/timestamp.js +92 -0
- package/dist/esm/generated/protoc-gen-openapiv2/options/annotations.d.ts +1 -0
- package/dist/esm/generated/protoc-gen-openapiv2/options/annotations.js +7 -0
- package/dist/esm/generated/protoc-gen-openapiv2/options/openapiv2.d.ts +983 -0
- package/dist/esm/generated/protoc-gen-openapiv2/options/openapiv2.js +4859 -0
- package/dist/esm/generated/tarka/access/v1/access.d.ts +322 -0
- package/dist/esm/generated/tarka/access/v1/access.js +2976 -0
- package/dist/esm/generated/tarka/channels/v1/channels.d.ts +370 -0
- package/dist/esm/generated/tarka/channels/v1/channels.js +3600 -0
- package/dist/esm/generated/tarka/gateway/v1/gateway.d.ts +804 -0
- package/dist/esm/generated/tarka/gateway/v1/gateway.js +8077 -0
- package/dist/esm/generated/tarka/iam/v1/iam.d.ts +1291 -0
- package/dist/esm/generated/tarka/iam/v1/iam.js +13203 -0
- package/dist/esm/generated/tarka/inference/v1/inference.d.ts +699 -0
- package/dist/esm/generated/tarka/inference/v1/inference.js +7055 -0
- package/dist/esm/generated/tarka/provisioning/v1/agent_secrets.d.ts +70 -0
- package/dist/esm/generated/tarka/provisioning/v1/agent_secrets.js +600 -0
- package/dist/esm/generated/tarka/provisioning/v1/billing_quote.d.ts +171 -0
- package/dist/esm/generated/tarka/provisioning/v1/billing_quote.js +1585 -0
- package/dist/esm/generated/tarka/provisioning/v1/job.d.ts +168 -0
- package/dist/esm/generated/tarka/provisioning/v1/job.js +1500 -0
- package/dist/esm/generated/tarka/provisioning/v1/provisioning.d.ts +2644 -0
- package/dist/esm/generated/tarka/provisioning/v1/provisioning.js +19496 -0
- package/dist/esm/generated/tarka/provisioning/v1/registry_credentials.d.ts +89 -0
- package/dist/esm/generated/tarka/provisioning/v1/registry_credentials.js +853 -0
- package/dist/esm/generated/tarka/provisioning/v1/sandbox.d.ts +431 -0
- package/dist/esm/generated/tarka/provisioning/v1/sandbox.js +3192 -0
- package/dist/esm/generated/tarka/provisioning/v1/source_builds.d.ts +291 -0
- package/dist/esm/generated/tarka/provisioning/v1/source_builds.js +3917 -0
- package/dist/esm/grpc.d.ts +12 -0
- package/dist/esm/grpc.js +11 -0
- package/dist/esm/index.d.ts +6 -0
- package/dist/esm/index.js +4 -0
- package/dist/esm/resources/inference.d.ts +293 -0
- package/dist/esm/resources/inference.js +437 -0
- package/dist/esm/resources/provisioning.d.ts +389 -0
- package/dist/esm/resources/provisioning.js +531 -0
- package/dist/esm/resources/sandboxes.d.ts +360 -0
- package/dist/esm/resources/sandboxes.js +718 -0
- package/dist/esm/rpc-types.d.ts +36 -0
- package/dist/esm/rpc-types.js +26 -0
- package/dist/esm/runtime.d.ts +99 -0
- package/dist/esm/runtime.js +466 -0
- package/dist/esm/transport.d.ts +60 -0
- package/dist/esm/transport.js +110 -0
- package/dist/esm/types.d.ts +309 -0
- package/dist/esm/types.js +1 -0
- package/package.json +155 -4
|
@@ -0,0 +1,732 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.OrganizationSandboxes = exports.SandboxTemplates = exports.SandboxTemplate = exports.Sandbox = exports.Executions = exports.Execution = exports.ResourceStateError = exports.ExecutionOperationError = exports.TemplateOperationError = exports.SandboxOperationError = exports.OperationError = void 0;
|
|
4
|
+
const node_crypto_1 = require("node:crypto");
|
|
5
|
+
const promises_1 = require("node:timers/promises");
|
|
6
|
+
const errors_js_1 = require("../errors.js");
|
|
7
|
+
/**
|
|
8
|
+
* A transient failure with explicit recovery for the original operation.
|
|
9
|
+
*
|
|
10
|
+
* No mutation is automatically retried. `resume()` returns the same entity type as the interrupted operation.
|
|
11
|
+
* @typeParam T - The result type of the original operation.
|
|
12
|
+
*/
|
|
13
|
+
class OperationError extends errors_js_1.TarkaError {
|
|
14
|
+
/** Frozen recovery context containing available IDs and the original idempotency key, without the request payload. */
|
|
15
|
+
recovery;
|
|
16
|
+
#resume;
|
|
17
|
+
constructor(cause, recovery, resume) {
|
|
18
|
+
const error = (0, errors_js_1.normalizeError)(cause);
|
|
19
|
+
super(error.message, error.code, {
|
|
20
|
+
cause: error,
|
|
21
|
+
metadata: error.metadata,
|
|
22
|
+
});
|
|
23
|
+
this.name = "OperationError";
|
|
24
|
+
this.recovery = Object.freeze({ ...recovery });
|
|
25
|
+
this.#resume = resume;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Resume the original operation with a fresh timeout and cancellation scope.
|
|
29
|
+
*
|
|
30
|
+
* Known execution IDs are polled without resubmitting code. Unacknowledged mutations replay the preserved input and idempotency key; resume within the API's 24-hour deduplication window.
|
|
31
|
+
* Previous deadlines and aborted signals are discarded. Metadata and polling interval are retained unless overridden. A further transient failure carries another resumable error.
|
|
32
|
+
* @param options - New request/wait settings. Waits default to 60 seconds; submission-only operations use the client timeout unless overridden.
|
|
33
|
+
* @returns The original operation's entity type. Resuming `run()` waits for completion; resuming `execute()` only completes submission.
|
|
34
|
+
* @example
|
|
35
|
+
* if (error instanceof ExecutionOperationError) {
|
|
36
|
+
* const execution = await error.resume({ timeoutMs: 60_000 });
|
|
37
|
+
* }
|
|
38
|
+
*/
|
|
39
|
+
resume(options) {
|
|
40
|
+
return this.#resume(options);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
exports.OperationError = OperationError;
|
|
44
|
+
/** Interrupted sandbox creation or readiness wait. `resume()` returns a Sandbox handle. */
|
|
45
|
+
class SandboxOperationError extends OperationError {
|
|
46
|
+
name = "SandboxOperationError";
|
|
47
|
+
}
|
|
48
|
+
exports.SandboxOperationError = SandboxOperationError;
|
|
49
|
+
/** Interrupted template creation or readiness wait. `resume()` returns a SandboxTemplate handle. */
|
|
50
|
+
class TemplateOperationError extends OperationError {
|
|
51
|
+
name = "TemplateOperationError";
|
|
52
|
+
}
|
|
53
|
+
exports.TemplateOperationError = TemplateOperationError;
|
|
54
|
+
/** Interrupted code submission, run, or execution wait. `resume()` returns an Execution handle and preserves the original operation. */
|
|
55
|
+
class ExecutionOperationError extends OperationError {
|
|
56
|
+
name = "ExecutionOperationError";
|
|
57
|
+
}
|
|
58
|
+
exports.ExecutionOperationError = ExecutionOperationError;
|
|
59
|
+
function recoverable(error) {
|
|
60
|
+
if (error instanceof TypeError)
|
|
61
|
+
return false;
|
|
62
|
+
return [
|
|
63
|
+
"deadline_exceeded",
|
|
64
|
+
"cancelled",
|
|
65
|
+
"unavailable",
|
|
66
|
+
"aborted",
|
|
67
|
+
"unknown",
|
|
68
|
+
"internal",
|
|
69
|
+
].includes((0, errors_js_1.normalizeError)(error).code);
|
|
70
|
+
}
|
|
71
|
+
function idempotencyKey(value) {
|
|
72
|
+
if (value === undefined)
|
|
73
|
+
return (0, node_crypto_1.randomUUID)();
|
|
74
|
+
(0, errors_js_1.requireText)(value, "idempotencyKey");
|
|
75
|
+
return value;
|
|
76
|
+
}
|
|
77
|
+
// Keep request context, but never inherit an expired deadline or aborted signal.
|
|
78
|
+
function resumeOptions(previous, next) {
|
|
79
|
+
return {
|
|
80
|
+
metadata: previous?.metadata,
|
|
81
|
+
pollIntervalMs: previous?.pollIntervalMs,
|
|
82
|
+
...next,
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
/** A resource cannot reach the requested state. Includes its snapshot in `info`; unknown statuses use `data_loss`, other terminal states use `failed_precondition`. */
|
|
86
|
+
class ResourceStateError extends errors_js_1.TarkaError {
|
|
87
|
+
resource;
|
|
88
|
+
info;
|
|
89
|
+
constructor(
|
|
90
|
+
/** Kind of resource whose state prevents this operation. */
|
|
91
|
+
resource,
|
|
92
|
+
/** Latest snapshot, including the status and available failure details. */
|
|
93
|
+
info) {
|
|
94
|
+
super(`${resource} ${info.id} is ${info.status}`, info.status === "unknown" ? "data_loss" : "failed_precondition");
|
|
95
|
+
this.resource = resource;
|
|
96
|
+
this.info = info;
|
|
97
|
+
this.name = "ResourceStateError";
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
exports.ResourceStateError = ResourceStateError;
|
|
101
|
+
const sandboxStatuses = [
|
|
102
|
+
"unknown",
|
|
103
|
+
"starting",
|
|
104
|
+
"ready",
|
|
105
|
+
"busy",
|
|
106
|
+
"stopping",
|
|
107
|
+
"stopped",
|
|
108
|
+
"failed",
|
|
109
|
+
"expired",
|
|
110
|
+
"deleted",
|
|
111
|
+
];
|
|
112
|
+
const templateStatuses = [
|
|
113
|
+
"unknown",
|
|
114
|
+
"pending",
|
|
115
|
+
"building",
|
|
116
|
+
"ready",
|
|
117
|
+
"failed",
|
|
118
|
+
"deleting",
|
|
119
|
+
"deleted",
|
|
120
|
+
];
|
|
121
|
+
const executionStatuses = [
|
|
122
|
+
"unknown",
|
|
123
|
+
"running",
|
|
124
|
+
"succeeded",
|
|
125
|
+
"failed",
|
|
126
|
+
"timed_out",
|
|
127
|
+
"cancelled",
|
|
128
|
+
];
|
|
129
|
+
const runtime = (value) => value === 1
|
|
130
|
+
? "python"
|
|
131
|
+
: value === 2
|
|
132
|
+
? "node"
|
|
133
|
+
: "unknown";
|
|
134
|
+
const sandboxData = (value) => ({
|
|
135
|
+
...value,
|
|
136
|
+
status: sandboxStatuses[value.status] ?? "unknown",
|
|
137
|
+
runtime: runtime(value.runtime),
|
|
138
|
+
});
|
|
139
|
+
const templateData = (value) => ({
|
|
140
|
+
...value,
|
|
141
|
+
status: templateStatuses[value.status] ?? "unknown",
|
|
142
|
+
runtime: runtime(value.runtime),
|
|
143
|
+
});
|
|
144
|
+
const executionData = (value) => ({
|
|
145
|
+
...value,
|
|
146
|
+
status: executionStatuses[value.status] ?? "unknown",
|
|
147
|
+
});
|
|
148
|
+
function budget(options) {
|
|
149
|
+
const { timeoutMs = 60_000, pollIntervalMs = 1000, ...rest } = options;
|
|
150
|
+
if (!Number.isFinite(timeoutMs) || timeoutMs < 0)
|
|
151
|
+
throw new TypeError("timeoutMs must be a finite, non-negative number");
|
|
152
|
+
if (!Number.isFinite(pollIntervalMs) || pollIntervalMs <= 0)
|
|
153
|
+
throw new TypeError("pollIntervalMs must be a finite, positive number");
|
|
154
|
+
const deadline = Math.min(Date.now() + timeoutMs, options.deadline ?? Infinity);
|
|
155
|
+
return {
|
|
156
|
+
pollIntervalMs,
|
|
157
|
+
deadline,
|
|
158
|
+
options: { ...rest, timeoutMs, deadline, pollIntervalMs },
|
|
159
|
+
};
|
|
160
|
+
}
|
|
161
|
+
function check(wait) {
|
|
162
|
+
if (wait.options.signal?.aborted)
|
|
163
|
+
throw new errors_js_1.TarkaError("Operation cancelled", "cancelled");
|
|
164
|
+
if (Date.now() >= wait.deadline)
|
|
165
|
+
throw new errors_js_1.TarkaError("Timed out waiting for operation", "deadline_exceeded");
|
|
166
|
+
}
|
|
167
|
+
async function pause(wait) {
|
|
168
|
+
check(wait);
|
|
169
|
+
try {
|
|
170
|
+
await (0, promises_1.setTimeout)(Math.min(wait.pollIntervalMs, wait.deadline - Date.now(), 2 ** 31 - 1), undefined, { signal: wait.options.signal });
|
|
171
|
+
}
|
|
172
|
+
catch (error) {
|
|
173
|
+
if (wait.options.signal?.aborted)
|
|
174
|
+
throw new errors_js_1.TarkaError("Operation cancelled", "cancelled", {
|
|
175
|
+
cause: error,
|
|
176
|
+
});
|
|
177
|
+
throw error;
|
|
178
|
+
}
|
|
179
|
+
check(wait);
|
|
180
|
+
}
|
|
181
|
+
/** A code execution with retained output. Returned by submission, retrieval, and waiting; call `refresh()` or `wait()` to update its snapshot. */
|
|
182
|
+
class Execution {
|
|
183
|
+
orgId;
|
|
184
|
+
sandboxId;
|
|
185
|
+
idempotencyKey;
|
|
186
|
+
#rpc;
|
|
187
|
+
/** Stable execution identifier; use `sandbox.executions.get(id)` to reattach later. */
|
|
188
|
+
id;
|
|
189
|
+
#info;
|
|
190
|
+
constructor(rpc,
|
|
191
|
+
/** Organization identifier retained by this handle or collection. */
|
|
192
|
+
orgId,
|
|
193
|
+
/** Sandbox identifier retained by this execution or collection. */
|
|
194
|
+
sandboxId, info,
|
|
195
|
+
/** Original mutation key on created/submitted handles; absent on handles fetched by ID or listed. */
|
|
196
|
+
idempotencyKey) {
|
|
197
|
+
this.orgId = orgId;
|
|
198
|
+
this.sandboxId = sandboxId;
|
|
199
|
+
this.idempotencyKey = idempotencyKey;
|
|
200
|
+
this.#rpc = rpc;
|
|
201
|
+
this.id = info.id;
|
|
202
|
+
this.#info = executionData(info);
|
|
203
|
+
}
|
|
204
|
+
/** Latest fetched execution snapshot. Reading it makes no network request. */
|
|
205
|
+
get info() {
|
|
206
|
+
return this.#info;
|
|
207
|
+
}
|
|
208
|
+
/** Latest known execution status. A terminal `failed`, `timed_out`, or `cancelled` result is returned for inspection. */
|
|
209
|
+
get status() {
|
|
210
|
+
return this.#info.status;
|
|
211
|
+
}
|
|
212
|
+
/** Retained standard output from the latest snapshot; may be empty after server-side output retention expires. */
|
|
213
|
+
get stdout() {
|
|
214
|
+
return this.#info.stdout;
|
|
215
|
+
}
|
|
216
|
+
/** Retained standard error from the latest snapshot; inspect alongside status and exitCode. */
|
|
217
|
+
get stderr() {
|
|
218
|
+
return this.#info.stderr;
|
|
219
|
+
}
|
|
220
|
+
/** Process exit code from the latest snapshot. Inspect it after the execution has reached a terminal status. */
|
|
221
|
+
get exitCode() {
|
|
222
|
+
return this.#info.exitCode;
|
|
223
|
+
}
|
|
224
|
+
/** Whether the server truncated retained execution output. */
|
|
225
|
+
get outputTruncated() {
|
|
226
|
+
return this.#info.outputTruncated;
|
|
227
|
+
}
|
|
228
|
+
/** Return the latest snapshot for serialization, without making a network request or including client internals. */
|
|
229
|
+
toJSON() {
|
|
230
|
+
return this.#info;
|
|
231
|
+
}
|
|
232
|
+
/**
|
|
233
|
+
* Fetch the latest execution snapshot and update this handle.
|
|
234
|
+
*
|
|
235
|
+
* @returns This same handle.
|
|
236
|
+
* @param options - Per-call timeout in milliseconds, cancellation signal, and metadata.
|
|
237
|
+
*/
|
|
238
|
+
async refresh(options) {
|
|
239
|
+
this.#info = executionData(await this.#rpc.getExecution({ orgId: this.orgId, sandboxId: this.sandboxId, executionId: this.id }, options));
|
|
240
|
+
return this;
|
|
241
|
+
}
|
|
242
|
+
/**
|
|
243
|
+
* Poll this execution until it reaches a terminal status.
|
|
244
|
+
*
|
|
245
|
+
* Code failures and server execution timeouts return this handle for inspection; they are not transport errors. An already terminal handle is returned without another request.
|
|
246
|
+
* @param options - One total wait budget in `timeoutMs` (default 60,000), `pollIntervalMs` (default 1,000), cancellation, and metadata.
|
|
247
|
+
* @returns This same handle with updated output and status.
|
|
248
|
+
* @throws ExecutionOperationError on a transient failure; `resume()` polls the same execution.
|
|
249
|
+
* @throws ResourceStateError for an unknown execution status.
|
|
250
|
+
* @example
|
|
251
|
+
* const execution = await sandbox.executions.get(executionId);
|
|
252
|
+
* await execution.wait({ timeoutMs: 60_000 });
|
|
253
|
+
*/
|
|
254
|
+
async wait(options = {}) {
|
|
255
|
+
const wait = budget(options);
|
|
256
|
+
try {
|
|
257
|
+
while (this.status === "running") {
|
|
258
|
+
check(wait);
|
|
259
|
+
await this.refresh(wait.options);
|
|
260
|
+
if (this.status === "running")
|
|
261
|
+
await pause(wait);
|
|
262
|
+
}
|
|
263
|
+
if (this.status === "unknown")
|
|
264
|
+
throw new ResourceStateError("execution", this.#info);
|
|
265
|
+
return this;
|
|
266
|
+
}
|
|
267
|
+
catch (error) {
|
|
268
|
+
if (!recoverable(error))
|
|
269
|
+
throw error;
|
|
270
|
+
throw new ExecutionOperationError(error, {
|
|
271
|
+
operation: "execution.wait",
|
|
272
|
+
orgId: this.orgId,
|
|
273
|
+
sandboxId: this.sandboxId,
|
|
274
|
+
executionId: this.id,
|
|
275
|
+
idempotencyKey: this.idempotencyKey,
|
|
276
|
+
}, (next) => this.wait(resumeOptions(options, next)));
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
exports.Execution = Execution;
|
|
281
|
+
/** Execution submission and retrieval bound to one sandbox. Access through `sandbox.executions`. */
|
|
282
|
+
class Executions {
|
|
283
|
+
orgId;
|
|
284
|
+
sandboxId;
|
|
285
|
+
#rpc;
|
|
286
|
+
constructor(rpc,
|
|
287
|
+
/** Organization identifier retained by this handle or collection. */
|
|
288
|
+
orgId,
|
|
289
|
+
/** Sandbox identifier retained by this execution or collection. */
|
|
290
|
+
sandboxId) {
|
|
291
|
+
this.orgId = orgId;
|
|
292
|
+
this.sandboxId = sandboxId;
|
|
293
|
+
this.#rpc = rpc;
|
|
294
|
+
}
|
|
295
|
+
/**
|
|
296
|
+
* Retrieve an execution handle without waiting for completion.
|
|
297
|
+
*
|
|
298
|
+
* @param id - Execution identifier within this sandbox.
|
|
299
|
+
* @param options - Per-call timeout in milliseconds, cancellation signal, and metadata.
|
|
300
|
+
*/
|
|
301
|
+
async get(id, options) {
|
|
302
|
+
(0, errors_js_1.requireText)(id, "execution id");
|
|
303
|
+
return new Execution(this.#rpc, this.orgId, this.sandboxId, await this.#rpc.getExecution({ orgId: this.orgId, sandboxId: this.sandboxId, executionId: id }, options));
|
|
304
|
+
}
|
|
305
|
+
/**
|
|
306
|
+
* Submit code without readiness or completion polling.
|
|
307
|
+
*
|
|
308
|
+
* @param input - Source `code`, optional stdin, server `timeoutSeconds`, and optional idempotency key. A key is generated when omitted.
|
|
309
|
+
* @returns An Execution handle; call `wait()` to await completion.
|
|
310
|
+
* @throws ExecutionOperationError on a transient failure; recovery repeats the preserved request and key within the 24-hour deduplication window.
|
|
311
|
+
* @example
|
|
312
|
+
* const execution = await sandbox.executions.create({ code: "print(1)" });
|
|
313
|
+
* await execution.wait();
|
|
314
|
+
* @param options - Per-call timeout in milliseconds, cancellation signal, and metadata.
|
|
315
|
+
*/
|
|
316
|
+
async create(input, options) {
|
|
317
|
+
if (typeof input?.code !== "string")
|
|
318
|
+
throw new TypeError("code must be a string");
|
|
319
|
+
const request = {
|
|
320
|
+
...input,
|
|
321
|
+
orgId: this.orgId,
|
|
322
|
+
sandboxId: this.sandboxId,
|
|
323
|
+
idempotencyKey: idempotencyKey(input.idempotencyKey),
|
|
324
|
+
};
|
|
325
|
+
try {
|
|
326
|
+
return new Execution(this.#rpc, this.orgId, this.sandboxId, await this.#rpc.execute(request, options), request.idempotencyKey);
|
|
327
|
+
}
|
|
328
|
+
catch (error) {
|
|
329
|
+
if (!recoverable(error))
|
|
330
|
+
throw error;
|
|
331
|
+
throw new ExecutionOperationError(error, {
|
|
332
|
+
operation: "execution.submit",
|
|
333
|
+
orgId: this.orgId,
|
|
334
|
+
sandboxId: this.sandboxId,
|
|
335
|
+
idempotencyKey: request.idempotencyKey,
|
|
336
|
+
}, (next) => this.create(request, resumeOptions(options, next)));
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
exports.Executions = Executions;
|
|
341
|
+
/** An organization-bound sandbox handle. Lists, gets, and creates return this same type; local properties reflect the latest fetched snapshot. */
|
|
342
|
+
class Sandbox {
|
|
343
|
+
orgId;
|
|
344
|
+
idempotencyKey;
|
|
345
|
+
#rpc;
|
|
346
|
+
/** Stable sandbox identifier; save it to reattach with `org.sandboxes.get(id)`. */
|
|
347
|
+
id;
|
|
348
|
+
/** Submit code and retrieve executions within this sandbox. */
|
|
349
|
+
executions;
|
|
350
|
+
#info;
|
|
351
|
+
constructor(rpc,
|
|
352
|
+
/** Organization identifier retained by this handle or collection. */
|
|
353
|
+
orgId, info,
|
|
354
|
+
/** Original mutation key on created/submitted handles; absent on handles fetched by ID or listed. */
|
|
355
|
+
idempotencyKey) {
|
|
356
|
+
this.orgId = orgId;
|
|
357
|
+
this.idempotencyKey = idempotencyKey;
|
|
358
|
+
this.#rpc = rpc;
|
|
359
|
+
this.id = info.id;
|
|
360
|
+
this.#info = sandboxData(info);
|
|
361
|
+
this.executions = new Executions(rpc, orgId, this.id);
|
|
362
|
+
}
|
|
363
|
+
/** Latest fetched sandbox snapshot, including lifetime, runtime, and failure details. Reading it makes no request. */
|
|
364
|
+
get info() {
|
|
365
|
+
return this.#info;
|
|
366
|
+
}
|
|
367
|
+
/** Latest known lifecycle status, such as `starting`, `ready`, or `failed`. */
|
|
368
|
+
get status() {
|
|
369
|
+
return this.#info.status;
|
|
370
|
+
}
|
|
371
|
+
/** Return the latest snapshot for serialization, without client internals or a network request. */
|
|
372
|
+
toJSON() {
|
|
373
|
+
return this.#info;
|
|
374
|
+
}
|
|
375
|
+
/**
|
|
376
|
+
* Fetch the latest sandbox snapshot and update this handle.
|
|
377
|
+
*
|
|
378
|
+
* @returns This same handle.
|
|
379
|
+
* @param options - Per-call timeout in milliseconds, cancellation signal, and metadata.
|
|
380
|
+
*/
|
|
381
|
+
async refresh(options) {
|
|
382
|
+
this.#info = sandboxData(await this.#rpc.getSandbox({ orgId: this.orgId, sandboxId: this.id }, options));
|
|
383
|
+
return this;
|
|
384
|
+
}
|
|
385
|
+
/**
|
|
386
|
+
* Poll while the sandbox is starting or busy, until it becomes ready.
|
|
387
|
+
*
|
|
388
|
+
* @param options - One total wait budget in `timeoutMs` (default 60,000), `pollIntervalMs` (default 1,000), cancellation, and metadata.
|
|
389
|
+
* @returns This same handle. An already ready snapshot returns immediately.
|
|
390
|
+
* @throws SandboxOperationError on a transient failure; `resume()` continues waiting on this sandbox.
|
|
391
|
+
* @throws ResourceStateError when the sandbox cannot become ready.
|
|
392
|
+
* Cancellation stops waiting; it does not delete the sandbox.
|
|
393
|
+
*/
|
|
394
|
+
async waitUntilReady(options = {}) {
|
|
395
|
+
const wait = budget(options);
|
|
396
|
+
try {
|
|
397
|
+
while (this.status !== "ready") {
|
|
398
|
+
if (this.status !== "starting" && this.status !== "busy")
|
|
399
|
+
throw new ResourceStateError("sandbox", this.#info);
|
|
400
|
+
check(wait);
|
|
401
|
+
await this.refresh(wait.options);
|
|
402
|
+
if (this.status === "starting" || this.status === "busy")
|
|
403
|
+
await pause(wait);
|
|
404
|
+
}
|
|
405
|
+
return this;
|
|
406
|
+
}
|
|
407
|
+
catch (error) {
|
|
408
|
+
if (!recoverable(error))
|
|
409
|
+
throw error;
|
|
410
|
+
throw new SandboxOperationError(error, {
|
|
411
|
+
operation: "sandbox.wait",
|
|
412
|
+
orgId: this.orgId,
|
|
413
|
+
sandboxId: this.id,
|
|
414
|
+
idempotencyKey: this.idempotencyKey,
|
|
415
|
+
}, (next) => this.waitUntilReady(resumeOptions(options, next)));
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
/**
|
|
419
|
+
* Submit code and return an Execution handle without waiting for readiness or completion.
|
|
420
|
+
*
|
|
421
|
+
* @param code - Source interpreted by the sandbox template's runtime.
|
|
422
|
+
* @param input - Optional stdin, server execution limit in `timeoutSeconds`, and idempotency key; a key is generated when omitted.
|
|
423
|
+
* @throws ExecutionOperationError on a transient failure; `resume()` recovers submission with the original input and key.
|
|
424
|
+
* @param options - Per-call timeout in milliseconds, cancellation signal, and metadata.
|
|
425
|
+
*/
|
|
426
|
+
execute(code, input = {}, options) {
|
|
427
|
+
return this.executions.create({ ...input, code }, options);
|
|
428
|
+
}
|
|
429
|
+
/**
|
|
430
|
+
* Wait for readiness, submit code, then wait for completion within one total budget.
|
|
431
|
+
*
|
|
432
|
+
* @param code - Source interpreted by the sandbox template's runtime.
|
|
433
|
+
* @param input - Optional stdin, server execution limit in `timeoutSeconds`, and idempotency key. Pass `undefined` when supplying only wait settings.
|
|
434
|
+
* @param options - Total client wait in `timeoutMs` (default 60,000), polling interval (default 1,000), cancellation, and metadata. This overrides the client timeout.
|
|
435
|
+
* @returns The completed Execution handle, including code failures and server timeouts; inspect `status`, `exitCode`, and `stderr`.
|
|
436
|
+
* @throws ExecutionOperationError on a transient failure. `resume()` polls a known execution or replays an unacknowledged submission with the same key.
|
|
437
|
+
* @throws ResourceStateError if readiness is impossible or the execution status is unknown.
|
|
438
|
+
* Client timeout/cancellation does not necessarily stop remotely running code. Keep the sandbox available while recovering.
|
|
439
|
+
* @example
|
|
440
|
+
* const result = await sandbox.run("print(1)", { timeoutSeconds: 30 }, { timeoutMs: 60_000 });
|
|
441
|
+
* console.log(result.status, result.stdout);
|
|
442
|
+
*/
|
|
443
|
+
async run(code, input = {}, options = {}) {
|
|
444
|
+
if (typeof code !== "string")
|
|
445
|
+
throw new TypeError("code must be a string");
|
|
446
|
+
const request = {
|
|
447
|
+
...input,
|
|
448
|
+
idempotencyKey: idempotencyKey(input.idempotencyKey),
|
|
449
|
+
};
|
|
450
|
+
const wait = budget(options);
|
|
451
|
+
try {
|
|
452
|
+
check(wait);
|
|
453
|
+
await this.waitUntilReady(wait.options);
|
|
454
|
+
const execution = await this.execute(code, request, wait.options);
|
|
455
|
+
return await execution.wait(wait.options);
|
|
456
|
+
}
|
|
457
|
+
catch (error) {
|
|
458
|
+
// Once we know the execution ID, resume only reads its state.
|
|
459
|
+
if (error instanceof ExecutionOperationError &&
|
|
460
|
+
error.recovery.executionId)
|
|
461
|
+
throw error;
|
|
462
|
+
if (!recoverable(error))
|
|
463
|
+
throw error;
|
|
464
|
+
throw new ExecutionOperationError(error, {
|
|
465
|
+
operation: "execution.run",
|
|
466
|
+
orgId: this.orgId,
|
|
467
|
+
sandboxId: this.id,
|
|
468
|
+
idempotencyKey: request.idempotencyKey,
|
|
469
|
+
}, (next) => this.run(code, request, resumeOptions(options, next)));
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
/**
|
|
473
|
+
* Stop and delete this sandbox, updating the local snapshot from the server response.
|
|
474
|
+
*
|
|
475
|
+
* @returns Resolves without a value after acknowledgement. Finish recovery before deleting the sandbox.
|
|
476
|
+
* @param options - Per-call timeout in milliseconds, cancellation signal, and metadata.
|
|
477
|
+
*/
|
|
478
|
+
async delete(options) {
|
|
479
|
+
this.#info = sandboxData(await this.#rpc.deleteSandbox({ orgId: this.orgId, sandboxId: this.id }, options));
|
|
480
|
+
}
|
|
481
|
+
}
|
|
482
|
+
exports.Sandbox = Sandbox;
|
|
483
|
+
/** A reusable sandbox template handle with build-state polling. Obtain from `org.sandboxes.templates`. */
|
|
484
|
+
class SandboxTemplate {
|
|
485
|
+
orgId;
|
|
486
|
+
idempotencyKey;
|
|
487
|
+
#rpc;
|
|
488
|
+
/** Stable template identifier, accepted by sandbox creation. */
|
|
489
|
+
id;
|
|
490
|
+
#info;
|
|
491
|
+
constructor(rpc,
|
|
492
|
+
/** Organization identifier retained by this handle or collection. */
|
|
493
|
+
orgId, info,
|
|
494
|
+
/** Original mutation key on created/submitted handles; absent on handles fetched by ID or listed. */
|
|
495
|
+
idempotencyKey) {
|
|
496
|
+
this.orgId = orgId;
|
|
497
|
+
this.idempotencyKey = idempotencyKey;
|
|
498
|
+
this.#rpc = rpc;
|
|
499
|
+
this.id = info.id;
|
|
500
|
+
this.#info = templateData(info);
|
|
501
|
+
}
|
|
502
|
+
/** Latest fetched template snapshot, including runtime, manifest digest, and build error code. */
|
|
503
|
+
get info() {
|
|
504
|
+
return this.#info;
|
|
505
|
+
}
|
|
506
|
+
/** Latest known template lifecycle status, such as `building`, `ready`, or `failed`. */
|
|
507
|
+
get status() {
|
|
508
|
+
return this.#info.status;
|
|
509
|
+
}
|
|
510
|
+
/** Template alias from the latest snapshot; also accepted by sandbox creation. */
|
|
511
|
+
get alias() {
|
|
512
|
+
return this.#info.alias;
|
|
513
|
+
}
|
|
514
|
+
/** Return the latest snapshot for serialization without client internals or a network request. */
|
|
515
|
+
toJSON() {
|
|
516
|
+
return this.#info;
|
|
517
|
+
}
|
|
518
|
+
/**
|
|
519
|
+
* Fetch the latest template snapshot and update this handle.
|
|
520
|
+
*
|
|
521
|
+
* @returns This same handle.
|
|
522
|
+
* @param options - Per-call timeout in milliseconds, cancellation signal, and metadata.
|
|
523
|
+
*/
|
|
524
|
+
async refresh(options) {
|
|
525
|
+
this.#info = templateData(await this.#rpc.getSandboxTemplate({ orgId: this.orgId, templateId: this.id }, options));
|
|
526
|
+
return this;
|
|
527
|
+
}
|
|
528
|
+
/**
|
|
529
|
+
* Poll a pending or building template until it is ready for sandbox creation.
|
|
530
|
+
*
|
|
531
|
+
* @param options - One total wait budget in `timeoutMs` (default 60,000), `pollIntervalMs` (default 1,000), cancellation, and metadata. Builds may need a larger budget.
|
|
532
|
+
* @returns This same handle. An already ready snapshot returns immediately.
|
|
533
|
+
* @throws TemplateOperationError on a transient failure; `resume()` continues polling this template.
|
|
534
|
+
* @throws ResourceStateError when the template cannot become ready.
|
|
535
|
+
* Cancelling a wait stops polling; it does not delete the template or cancel its build.
|
|
536
|
+
*/
|
|
537
|
+
async waitUntilReady(options = {}) {
|
|
538
|
+
const wait = budget(options);
|
|
539
|
+
try {
|
|
540
|
+
while (this.status !== "ready") {
|
|
541
|
+
if (this.status !== "pending" && this.status !== "building")
|
|
542
|
+
throw new ResourceStateError("template", this.#info);
|
|
543
|
+
check(wait);
|
|
544
|
+
await this.refresh(wait.options);
|
|
545
|
+
if (this.status === "pending" || this.status === "building")
|
|
546
|
+
await pause(wait);
|
|
547
|
+
}
|
|
548
|
+
return this;
|
|
549
|
+
}
|
|
550
|
+
catch (error) {
|
|
551
|
+
if (!recoverable(error))
|
|
552
|
+
throw error;
|
|
553
|
+
throw new TemplateOperationError(error, {
|
|
554
|
+
operation: "template.wait",
|
|
555
|
+
orgId: this.orgId,
|
|
556
|
+
templateId: this.id,
|
|
557
|
+
idempotencyKey: this.idempotencyKey,
|
|
558
|
+
}, (next) => this.waitUntilReady(resumeOptions(options, next)));
|
|
559
|
+
}
|
|
560
|
+
}
|
|
561
|
+
/**
|
|
562
|
+
* Request deletion of this template and update its local snapshot.
|
|
563
|
+
*
|
|
564
|
+
* @returns Resolves without a value after acknowledgement; inspect the snapshot for lifecycle state.
|
|
565
|
+
* @param options - Per-call timeout in milliseconds, cancellation signal, and metadata.
|
|
566
|
+
*/
|
|
567
|
+
async delete(options) {
|
|
568
|
+
this.#info = templateData(await this.#rpc.deleteSandboxTemplate({ orgId: this.orgId, templateId: this.id }, options));
|
|
569
|
+
}
|
|
570
|
+
}
|
|
571
|
+
exports.SandboxTemplate = SandboxTemplate;
|
|
572
|
+
/** Organization-visible reusable templates, including platform built-ins. Access through `org.sandboxes.templates`. */
|
|
573
|
+
class SandboxTemplates {
|
|
574
|
+
orgId;
|
|
575
|
+
#rpc;
|
|
576
|
+
constructor(rpc,
|
|
577
|
+
/** Organization identifier retained by this handle or collection. */
|
|
578
|
+
orgId) {
|
|
579
|
+
this.orgId = orgId;
|
|
580
|
+
this.#rpc = rpc;
|
|
581
|
+
}
|
|
582
|
+
/**
|
|
583
|
+
* List visible templates as SandboxTemplate handles, including platform built-ins.
|
|
584
|
+
*
|
|
585
|
+
* @param options - Per-call timeout in milliseconds, cancellation signal, and metadata.
|
|
586
|
+
*/
|
|
587
|
+
async list(options) {
|
|
588
|
+
return (await this.#rpc.listSandboxTemplates({ orgId: this.orgId }, options)).templates.map((info) => new SandboxTemplate(this.#rpc, this.orgId, info));
|
|
589
|
+
}
|
|
590
|
+
/**
|
|
591
|
+
* Retrieve a template handle without waiting for build completion.
|
|
592
|
+
*
|
|
593
|
+
* @param id - Organization-visible template identifier.
|
|
594
|
+
* @param options - Per-call timeout in milliseconds, cancellation signal, and metadata.
|
|
595
|
+
*/
|
|
596
|
+
async get(id, options) {
|
|
597
|
+
(0, errors_js_1.requireText)(id, "template id");
|
|
598
|
+
return new SandboxTemplate(this.#rpc, this.orgId, await this.#rpc.getSandboxTemplate({ orgId: this.orgId, templateId: id }, options));
|
|
599
|
+
}
|
|
600
|
+
/**
|
|
601
|
+
* Create a dependency-pinned template and return its handle without build polling.
|
|
602
|
+
*
|
|
603
|
+
* @param input - Alias, built-in base template alias, and exactly one manifest string: `requirementsTxt`, `packageJson`, or `pyprojectToml`. An omitted idempotency key is generated.
|
|
604
|
+
* @returns A SandboxTemplate handle; call `waitUntilReady()` before using a building template.
|
|
605
|
+
* @throws TemplateOperationError on a transient failure; `resume()` preserves the manifest and idempotency key.
|
|
606
|
+
* @example
|
|
607
|
+
* const template = await org.sandboxes.templates.create({
|
|
608
|
+
* alias: "custom-python", baseTemplateAlias, requirementsTxt: "numpy==2.0.0",
|
|
609
|
+
* });
|
|
610
|
+
* await template.waitUntilReady({ timeoutMs: 300_000 });
|
|
611
|
+
* @param options - Per-call timeout in milliseconds, cancellation signal, and metadata.
|
|
612
|
+
*/
|
|
613
|
+
async create(input, options) {
|
|
614
|
+
(0, errors_js_1.requireText)(input?.alias, "alias");
|
|
615
|
+
(0, errors_js_1.requireText)(input?.baseTemplateAlias, "baseTemplateAlias");
|
|
616
|
+
const fields = ["requirementsTxt", "packageJson", "pyprojectToml"];
|
|
617
|
+
const selected = fields.filter((field) => input[field] !== undefined);
|
|
618
|
+
if (selected.length !== 1 || typeof input[selected[0]] !== "string")
|
|
619
|
+
throw new TypeError("Provide exactly one dependency manifest: requirementsTxt, packageJson, or pyprojectToml");
|
|
620
|
+
const field = selected[0];
|
|
621
|
+
const replay = {
|
|
622
|
+
...input,
|
|
623
|
+
idempotencyKey: idempotencyKey(input.idempotencyKey),
|
|
624
|
+
};
|
|
625
|
+
const request = {
|
|
626
|
+
orgId: this.orgId,
|
|
627
|
+
alias: input.alias,
|
|
628
|
+
baseTemplateAlias: input.baseTemplateAlias,
|
|
629
|
+
displayName: input.displayName,
|
|
630
|
+
idempotencyKey: replay.idempotencyKey,
|
|
631
|
+
dependencyFile: { $case: field, value: input[field] },
|
|
632
|
+
};
|
|
633
|
+
try {
|
|
634
|
+
return new SandboxTemplate(this.#rpc, this.orgId, await this.#rpc.createSandboxTemplate(request, options), replay.idempotencyKey);
|
|
635
|
+
}
|
|
636
|
+
catch (error) {
|
|
637
|
+
if (!recoverable(error))
|
|
638
|
+
throw error;
|
|
639
|
+
throw new TemplateOperationError(error, {
|
|
640
|
+
operation: "template.create",
|
|
641
|
+
orgId: this.orgId,
|
|
642
|
+
idempotencyKey: replay.idempotencyKey,
|
|
643
|
+
}, (next) => this.create(replay, resumeOptions(options, next)));
|
|
644
|
+
}
|
|
645
|
+
}
|
|
646
|
+
/**
|
|
647
|
+
* Request deletion of an organization-owned template.
|
|
648
|
+
*
|
|
649
|
+
* @param id - Template identifier.
|
|
650
|
+
* @returns Resolves without a value after acknowledgement.
|
|
651
|
+
* @param options - Per-call timeout in milliseconds, cancellation signal, and metadata.
|
|
652
|
+
*/
|
|
653
|
+
async delete(id, options) {
|
|
654
|
+
(0, errors_js_1.requireText)(id, "template id");
|
|
655
|
+
await this.#rpc.deleteSandboxTemplate({ orgId: this.orgId, templateId: id }, options);
|
|
656
|
+
}
|
|
657
|
+
}
|
|
658
|
+
exports.SandboxTemplates = SandboxTemplates;
|
|
659
|
+
/** Sandbox lifecycle and templates bound to one organization. Access through `client.org(orgId).sandboxes`. */
|
|
660
|
+
class OrganizationSandboxes {
|
|
661
|
+
orgId;
|
|
662
|
+
#rpc;
|
|
663
|
+
/** Visible reusable templates and custom template creation. */
|
|
664
|
+
templates;
|
|
665
|
+
constructor(rpc,
|
|
666
|
+
/** Organization identifier retained by this handle or collection. */
|
|
667
|
+
orgId) {
|
|
668
|
+
this.orgId = orgId;
|
|
669
|
+
this.#rpc = rpc;
|
|
670
|
+
(0, errors_js_1.requireText)(orgId, "organization id");
|
|
671
|
+
this.templates = new SandboxTemplates(rpc, orgId);
|
|
672
|
+
}
|
|
673
|
+
/**
|
|
674
|
+
* List non-deleted sandboxes, newest first, as Sandbox handles.
|
|
675
|
+
*
|
|
676
|
+
* @param options - Per-call timeout in milliseconds, cancellation signal, and metadata.
|
|
677
|
+
*/
|
|
678
|
+
async list(options) {
|
|
679
|
+
return (await this.#rpc.listSandboxes({ orgId: this.orgId }, options)).sandboxes.map((info) => new Sandbox(this.#rpc, this.orgId, info));
|
|
680
|
+
}
|
|
681
|
+
/**
|
|
682
|
+
* Retrieve a Sandbox handle without waiting for readiness.
|
|
683
|
+
*
|
|
684
|
+
* @param id - Sandbox identifier within this organization.
|
|
685
|
+
* @param options - Per-call timeout in milliseconds, cancellation signal, and metadata.
|
|
686
|
+
*/
|
|
687
|
+
async get(id, options) {
|
|
688
|
+
(0, errors_js_1.requireText)(id, "sandbox id");
|
|
689
|
+
return new Sandbox(this.#rpc, this.orgId, await this.#rpc.getSandbox({ orgId: this.orgId, sandboxId: id }, options));
|
|
690
|
+
}
|
|
691
|
+
/**
|
|
692
|
+
* Start a sandbox from a ready template and return its handle.
|
|
693
|
+
*
|
|
694
|
+
* @param input - Visible template ID or alias, optional server lifetime in `timeoutSeconds`, and optional idempotency key. A key is generated when omitted.
|
|
695
|
+
* @returns A Sandbox handle without readiness polling; `run()` waits for readiness automatically.
|
|
696
|
+
* @throws SandboxOperationError on a transient failure; `resume()` repeats the original request and key within the 24-hour deduplication window.
|
|
697
|
+
* @example
|
|
698
|
+
* const sandbox = await client.org(orgId).sandboxes.create({ template, timeoutSeconds: 600 });
|
|
699
|
+
* @param options - Per-call timeout in milliseconds, cancellation signal, and metadata.
|
|
700
|
+
*/
|
|
701
|
+
async create(input, options) {
|
|
702
|
+
(0, errors_js_1.requireText)(input?.template, "template");
|
|
703
|
+
const replay = {
|
|
704
|
+
...input,
|
|
705
|
+
idempotencyKey: idempotencyKey(input.idempotencyKey),
|
|
706
|
+
};
|
|
707
|
+
try {
|
|
708
|
+
return new Sandbox(this.#rpc, this.orgId, await this.#rpc.createSandbox({ ...replay, orgId: this.orgId }, options), replay.idempotencyKey);
|
|
709
|
+
}
|
|
710
|
+
catch (error) {
|
|
711
|
+
if (!recoverable(error))
|
|
712
|
+
throw error;
|
|
713
|
+
throw new SandboxOperationError(error, {
|
|
714
|
+
operation: "sandbox.create",
|
|
715
|
+
orgId: this.orgId,
|
|
716
|
+
idempotencyKey: replay.idempotencyKey,
|
|
717
|
+
}, (next) => this.create(replay, resumeOptions(options, next)));
|
|
718
|
+
}
|
|
719
|
+
}
|
|
720
|
+
/**
|
|
721
|
+
* Stop and delete an organization sandbox.
|
|
722
|
+
*
|
|
723
|
+
* @param id - Sandbox identifier.
|
|
724
|
+
* @returns Resolves without a value after acknowledgement.
|
|
725
|
+
* @param options - Per-call timeout in milliseconds, cancellation signal, and metadata.
|
|
726
|
+
*/
|
|
727
|
+
async delete(id, options) {
|
|
728
|
+
(0, errors_js_1.requireText)(id, "sandbox id");
|
|
729
|
+
await this.#rpc.deleteSandbox({ orgId: this.orgId, sandboxId: id }, options);
|
|
730
|
+
}
|
|
731
|
+
}
|
|
732
|
+
exports.OrganizationSandboxes = OrganizationSandboxes;
|