@omnirun/sdk 0.5.0 → 0.7.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/CHANGELOG.md +24 -0
- package/README.md +266 -61
- package/dist/client.d.ts +44 -2
- package/dist/client.js +174 -81
- package/dist/commands.d.ts +10 -0
- package/dist/commands.js +39 -7
- package/dist/config.d.ts +12 -0
- package/dist/config.js +3 -0
- package/dist/contexts.js +1 -1
- package/dist/desktop.d.ts +72 -0
- package/dist/desktop.js +86 -0
- package/dist/errors.d.ts +32 -2
- package/dist/errors.js +28 -4
- package/dist/exposures.d.ts +52 -1
- package/dist/exposures.js +52 -1
- package/dist/filesystem.js +5 -3
- package/dist/index.d.ts +6 -2
- package/dist/index.js +1 -0
- package/dist/models.d.ts +96 -1
- package/dist/production.d.ts +2 -0
- package/dist/production.js +13 -9
- package/dist/pty.js +1 -1
- package/dist/sandbox.d.ts +123 -8
- package/dist/sandbox.js +156 -27
- package/dist/webhooks.d.ts +9 -1
- package/dist/webhooks.js +10 -2
- package/package.json +6 -6
package/dist/client.js
CHANGED
|
@@ -1,15 +1,53 @@
|
|
|
1
|
+
import { DEFAULT_RETRY_MAX_ATTEMPTS, DEFAULT_RETRY_MAX_TOTAL_WAIT_MS } from "./config.js";
|
|
2
|
+
import { encryptE2EEJSON, decryptE2EEJSON } from "./e2ee.js";
|
|
1
3
|
import { SandboxError, StreamError } from "./errors.js";
|
|
2
|
-
/**
|
|
3
|
-
const
|
|
4
|
-
/**
|
|
5
|
-
|
|
6
|
-
|
|
4
|
+
/** 429 reasons that waiting does not resolve: the account's own sandboxes use its limit. */
|
|
5
|
+
const ACCOUNT_LIMIT_REASONS = new Set(["per_user_sandbox_limit", "per_user_ram_limit"]);
|
|
6
|
+
/** 503 reasons on which the service guarantees the request was not carried out. */
|
|
7
|
+
const REFUSED_503_REASONS = new Set(["node_unavailable", "no_healthy_node"]);
|
|
8
|
+
/** Backoff before the first retry when the service sent no `Retry-After`; doubles per attempt. */
|
|
9
|
+
const BASE_BACKOFF_MS = 250;
|
|
10
|
+
/** Whether sending the same request again is safe under `policy`. */
|
|
11
|
+
function isRetryable(policy, status, reason) {
|
|
12
|
+
if (policy === "never")
|
|
13
|
+
return false;
|
|
14
|
+
if (status === 429)
|
|
15
|
+
return true;
|
|
16
|
+
if (policy === "idempotent")
|
|
17
|
+
return status === 502 || status === 503 || status === 504;
|
|
18
|
+
return status === 503 && reason !== undefined && REFUSED_503_REASONS.has(reason);
|
|
19
|
+
}
|
|
20
|
+
/** Parse a `Retry-After` header (delta seconds or HTTP date) into seconds. */
|
|
21
|
+
export function parseRetryAfter(value) {
|
|
22
|
+
if (value == null)
|
|
23
|
+
return undefined;
|
|
24
|
+
const trimmed = value.trim();
|
|
25
|
+
if (trimmed === "")
|
|
26
|
+
return undefined;
|
|
27
|
+
if (/^\d+(\.\d+)?$/.test(trimmed))
|
|
28
|
+
return Number(trimmed);
|
|
29
|
+
const date = Date.parse(trimmed);
|
|
30
|
+
if (Number.isNaN(date))
|
|
31
|
+
return undefined;
|
|
32
|
+
return Math.max(0, Math.ceil((date - Date.now()) / 1000));
|
|
33
|
+
}
|
|
34
|
+
function sleep(ms) {
|
|
35
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
7
36
|
}
|
|
8
37
|
/** Low-level HTTP transport for OmniRun API requests. */
|
|
9
38
|
export class HTTPClient {
|
|
10
39
|
config;
|
|
40
|
+
e2eeKey = null;
|
|
11
41
|
constructor(config) {
|
|
12
42
|
this.config = config;
|
|
43
|
+
if (config.apiUrl.startsWith("http://") &&
|
|
44
|
+
!config.apiUrl.includes("localhost") &&
|
|
45
|
+
!config.apiUrl.includes("127.0.0.1")) {
|
|
46
|
+
console.warn("[omnirun] WARNING: API URL uses unencrypted HTTP. API keys will be sent in plaintext. Use HTTPS for production.");
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
setE2EEKey(key) {
|
|
50
|
+
this.e2eeKey = key;
|
|
13
51
|
}
|
|
14
52
|
get baseUrl() {
|
|
15
53
|
return this.config.apiUrl;
|
|
@@ -24,58 +62,101 @@ export class HTTPClient {
|
|
|
24
62
|
get jsonHeaders() {
|
|
25
63
|
return { ...this.authHeaders, "Content-Type": "application/json" };
|
|
26
64
|
}
|
|
65
|
+
get maxAttempts() {
|
|
66
|
+
const retry = this.config.retry;
|
|
67
|
+
if (retry === false)
|
|
68
|
+
return 1;
|
|
69
|
+
return Math.max(1, Math.floor(retry?.maxAttempts ?? DEFAULT_RETRY_MAX_ATTEMPTS));
|
|
70
|
+
}
|
|
71
|
+
get maxTotalWaitMs() {
|
|
72
|
+
const retry = this.config.retry;
|
|
73
|
+
if (retry === false)
|
|
74
|
+
return 0;
|
|
75
|
+
return Math.max(0, retry?.maxTotalWaitMs ?? DEFAULT_RETRY_MAX_TOTAL_WAIT_MS);
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Send a request, retrying it as far as `policy` and the configured bounds
|
|
79
|
+
* allow. Returns the first successful response; throws a {@link SandboxError}
|
|
80
|
+
* for the last error response, or the network error itself.
|
|
81
|
+
*/
|
|
82
|
+
async request(url, policy, errorPrefix, makeInit, timeoutMs) {
|
|
83
|
+
const maxAttempts = this.maxAttempts;
|
|
84
|
+
const maxTotalWaitMs = this.maxTotalWaitMs;
|
|
85
|
+
let waitedMs = 0;
|
|
86
|
+
for (let attempt = 1;; attempt++) {
|
|
87
|
+
// The wait before the next attempt, or undefined when this failure is final.
|
|
88
|
+
const nextWait = (retryAfterSeconds) => {
|
|
89
|
+
if (attempt >= maxAttempts)
|
|
90
|
+
return undefined;
|
|
91
|
+
const waitMs = retryAfterSeconds != null
|
|
92
|
+
? retryAfterSeconds * 1000
|
|
93
|
+
: BASE_BACKOFF_MS * 2 ** (attempt - 1);
|
|
94
|
+
return waitedMs + waitMs <= maxTotalWaitMs ? waitMs : undefined;
|
|
95
|
+
};
|
|
96
|
+
let resp;
|
|
97
|
+
try {
|
|
98
|
+
resp = await fetch(url, {
|
|
99
|
+
...makeInit(),
|
|
100
|
+
signal: AbortSignal.timeout(timeoutMs ?? this.config.requestTimeout),
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
catch (err) {
|
|
104
|
+
// Only a failed connection is retried, and only for idempotent calls:
|
|
105
|
+
// the request may have reached the service. A timeout is never retried.
|
|
106
|
+
const waitMs = policy === "idempotent" && err instanceof TypeError ? nextWait() : undefined;
|
|
107
|
+
if (waitMs === undefined)
|
|
108
|
+
throw err;
|
|
109
|
+
await sleep(waitMs);
|
|
110
|
+
waitedMs += waitMs;
|
|
111
|
+
continue;
|
|
112
|
+
}
|
|
113
|
+
if (resp.ok)
|
|
114
|
+
return resp;
|
|
115
|
+
const error = await this.toError(resp, policy, errorPrefix);
|
|
116
|
+
const autoRetry = error.retryable && !(error.reason && ACCOUNT_LIMIT_REASONS.has(error.reason));
|
|
117
|
+
const waitMs = autoRetry ? nextWait(error.retryAfterSeconds) : undefined;
|
|
118
|
+
if (waitMs === undefined)
|
|
119
|
+
throw error;
|
|
120
|
+
await sleep(waitMs);
|
|
121
|
+
waitedMs += waitMs;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
27
124
|
async get(path, params) {
|
|
28
|
-
const
|
|
29
|
-
const resp = await fetch(url, {
|
|
125
|
+
const resp = await this.request(this.buildUrl(path, params), "idempotent", "API error", () => ({
|
|
30
126
|
method: "GET",
|
|
31
127
|
headers: this.authHeaders,
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
return this.handle(resp);
|
|
128
|
+
}));
|
|
129
|
+
return this.handleWithDecrypt(resp);
|
|
35
130
|
}
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
await resp.text();
|
|
48
|
-
continue;
|
|
49
|
-
}
|
|
50
|
-
return this.handle(resp);
|
|
131
|
+
/**
|
|
132
|
+
* POST a JSON body. By default the request is retried only when the service
|
|
133
|
+
* refused it without acting on it (`"refusal"`); pass `retry: "idempotent"`
|
|
134
|
+
* for calls that are safe to repeat and `retry: "never"` for calls that must
|
|
135
|
+
* not be sent twice.
|
|
136
|
+
*/
|
|
137
|
+
async post(path, body, opts) {
|
|
138
|
+
let actualBody = body;
|
|
139
|
+
if (this.e2eeKey && body != null) {
|
|
140
|
+
const envelope = await encryptE2EEJSON(this.e2eeKey, body);
|
|
141
|
+
actualBody = { _encrypted: envelope };
|
|
51
142
|
}
|
|
52
|
-
|
|
53
|
-
|
|
143
|
+
const jsonBody = actualBody != null ? JSON.stringify(actualBody) : undefined;
|
|
144
|
+
const resp = await this.request(this.buildUrl(path), opts?.retry ?? "refusal", "API error", () => ({ method: "POST", headers: this.jsonHeaders, body: jsonBody }), opts?.timeoutMs);
|
|
145
|
+
return this.handleWithDecrypt(resp);
|
|
54
146
|
}
|
|
55
147
|
async delete(path, params) {
|
|
56
|
-
const
|
|
57
|
-
const resp = await fetch(url, {
|
|
148
|
+
const resp = await this.request(this.buildUrl(path, params), "idempotent", "API error", () => ({
|
|
58
149
|
method: "DELETE",
|
|
59
150
|
headers: this.authHeaders,
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
return;
|
|
64
|
-
if (!resp.ok) {
|
|
65
|
-
const text = await resp.text();
|
|
66
|
-
throw new SandboxError(`API error ${resp.status}: ${text}`);
|
|
67
|
-
}
|
|
151
|
+
}));
|
|
152
|
+
// Drain the body so the connection can be reused.
|
|
153
|
+
await resp.text();
|
|
68
154
|
}
|
|
69
155
|
async download(path, params) {
|
|
70
|
-
const
|
|
71
|
-
const resp = await fetch(url, {
|
|
156
|
+
const resp = await this.request(this.buildUrl(path, params), "idempotent", "Download error", () => ({
|
|
72
157
|
method: "GET",
|
|
73
158
|
headers: { "X-API-Key": this.config.apiKey || "" },
|
|
74
|
-
|
|
75
|
-
});
|
|
76
|
-
if (!resp.ok) {
|
|
77
|
-
throw new SandboxError(`Download error ${resp.status}: ${await resp.text()}`);
|
|
78
|
-
}
|
|
159
|
+
}));
|
|
79
160
|
return new Uint8Array(await resp.arrayBuffer());
|
|
80
161
|
}
|
|
81
162
|
async streamDownload(path, params) {
|
|
@@ -85,7 +166,7 @@ export class HTTPClient {
|
|
|
85
166
|
headers: { "X-API-Key": this.config.apiKey || "" },
|
|
86
167
|
});
|
|
87
168
|
if (!resp.ok) {
|
|
88
|
-
throw
|
|
169
|
+
throw await this.toError(resp, "idempotent", "Download error");
|
|
89
170
|
}
|
|
90
171
|
if (!resp.body) {
|
|
91
172
|
throw new SandboxError("No response body for stream download");
|
|
@@ -93,38 +174,23 @@ export class HTTPClient {
|
|
|
93
174
|
return resp.body;
|
|
94
175
|
}
|
|
95
176
|
async getWithHeaders(path, params) {
|
|
96
|
-
const
|
|
97
|
-
const resp = await fetch(url, {
|
|
177
|
+
const resp = await this.request(this.buildUrl(path, params), "idempotent", "API error", () => ({
|
|
98
178
|
method: "GET",
|
|
99
179
|
headers: this.authHeaders,
|
|
100
|
-
|
|
101
|
-
});
|
|
180
|
+
}));
|
|
102
181
|
const data = await this.handle(resp);
|
|
103
182
|
return { data, headers: resp.headers };
|
|
104
183
|
}
|
|
184
|
+
/** Upload a file (multipart). Writing the same file twice is harmless, so this is retried as idempotent. */
|
|
105
185
|
async upload(path, filePath, content, filename) {
|
|
106
|
-
const
|
|
107
|
-
if (this.config.apiKey) {
|
|
108
|
-
headers["X-API-Key"] = this.config.apiKey;
|
|
109
|
-
}
|
|
110
|
-
for (let attempt = 0; attempt < MAX_RETRIES; attempt++) {
|
|
186
|
+
const resp = await this.request(this.buildUrl(path), "idempotent", "API error", () => {
|
|
111
187
|
// Rebuild FormData for each attempt since the body stream is consumed
|
|
112
188
|
const form = new FormData();
|
|
113
189
|
form.append("path", filePath);
|
|
114
190
|
form.append("file", new Blob([content]), filename);
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
body: form,
|
|
119
|
-
signal: AbortSignal.timeout(this.config.requestTimeout),
|
|
120
|
-
});
|
|
121
|
-
if (isRetryable(resp.status) && attempt < MAX_RETRIES - 1) {
|
|
122
|
-
await resp.text();
|
|
123
|
-
continue;
|
|
124
|
-
}
|
|
125
|
-
return this.handle(resp);
|
|
126
|
-
}
|
|
127
|
-
throw new SandboxError("max retries exceeded");
|
|
191
|
+
return { method: "POST", headers: this.authHeaders, body: form };
|
|
192
|
+
});
|
|
193
|
+
return this.handle(resp);
|
|
128
194
|
}
|
|
129
195
|
async *streamSSE(path, params) {
|
|
130
196
|
const url = this.buildUrl(path, params);
|
|
@@ -172,8 +238,7 @@ export class HTTPClient {
|
|
|
172
238
|
}
|
|
173
239
|
async handle(resp) {
|
|
174
240
|
if (!resp.ok) {
|
|
175
|
-
|
|
176
|
-
throw new SandboxError(`API error ${resp.status}: ${msg}`);
|
|
241
|
+
throw await this.toError(resp, "never", "API error");
|
|
177
242
|
}
|
|
178
243
|
if (resp.status === 204) {
|
|
179
244
|
return undefined;
|
|
@@ -184,9 +249,17 @@ export class HTTPClient {
|
|
|
184
249
|
}
|
|
185
250
|
return (await resp.text());
|
|
186
251
|
}
|
|
252
|
+
async handleWithDecrypt(resp) {
|
|
253
|
+
const data = await this.handle(resp);
|
|
254
|
+
if (this.e2eeKey && data && typeof data === "object" && "_encrypted" in data) {
|
|
255
|
+
return decryptE2EEJSON(this.e2eeKey, data._encrypted);
|
|
256
|
+
}
|
|
257
|
+
return data;
|
|
258
|
+
}
|
|
187
259
|
async *parseSSE(resp) {
|
|
188
260
|
if (!resp.ok) {
|
|
189
|
-
|
|
261
|
+
const { message, info } = await this.errorInfo(resp, "never");
|
|
262
|
+
throw new StreamError(`Stream error ${resp.status}: ${message}`, resp.status, info);
|
|
190
263
|
}
|
|
191
264
|
const reader = resp.body?.getReader();
|
|
192
265
|
if (!reader)
|
|
@@ -217,25 +290,45 @@ export class HTTPClient {
|
|
|
217
290
|
reader.releaseLock();
|
|
218
291
|
}
|
|
219
292
|
}
|
|
220
|
-
|
|
293
|
+
/** Build the {@link SandboxError} for an error response. */
|
|
294
|
+
async toError(resp, policy, prefix) {
|
|
295
|
+
const { message, info } = await this.errorInfo(resp, policy);
|
|
296
|
+
return new SandboxError(`${prefix} ${resp.status}: ${message}`, resp.status, info);
|
|
297
|
+
}
|
|
298
|
+
/** Read the message, `reason`, `detail` and `Retry-After` of an error response. */
|
|
299
|
+
async errorInfo(resp, policy) {
|
|
221
300
|
const raw = await resp.text();
|
|
222
|
-
|
|
223
|
-
|
|
301
|
+
let message = raw || "request failed";
|
|
302
|
+
let reason;
|
|
303
|
+
let detail;
|
|
224
304
|
try {
|
|
225
305
|
const parsed = JSON.parse(raw);
|
|
226
|
-
if (typeof parsed === "string")
|
|
227
|
-
|
|
228
|
-
|
|
306
|
+
if (typeof parsed === "string") {
|
|
307
|
+
message = parsed;
|
|
308
|
+
}
|
|
309
|
+
else if (parsed && typeof parsed === "object") {
|
|
229
310
|
const obj = parsed;
|
|
230
311
|
if (typeof obj.message === "string")
|
|
231
|
-
|
|
232
|
-
if (typeof obj.error === "string")
|
|
233
|
-
|
|
312
|
+
message = obj.message;
|
|
313
|
+
else if (typeof obj.error === "string")
|
|
314
|
+
message = obj.error;
|
|
315
|
+
if (typeof obj.reason === "string")
|
|
316
|
+
reason = obj.reason;
|
|
317
|
+
if (typeof obj.detail === "string")
|
|
318
|
+
detail = obj.detail;
|
|
234
319
|
}
|
|
235
|
-
return raw;
|
|
236
320
|
}
|
|
237
321
|
catch {
|
|
238
|
-
|
|
322
|
+
// Not JSON: keep the raw text.
|
|
239
323
|
}
|
|
324
|
+
return {
|
|
325
|
+
message,
|
|
326
|
+
info: {
|
|
327
|
+
reason,
|
|
328
|
+
detail,
|
|
329
|
+
retryAfterSeconds: parseRetryAfter(resp.headers.get("retry-after")),
|
|
330
|
+
retryable: isRetryable(policy, resp.status, reason),
|
|
331
|
+
},
|
|
332
|
+
};
|
|
240
333
|
}
|
|
241
334
|
}
|
package/dist/commands.d.ts
CHANGED
|
@@ -1,12 +1,22 @@
|
|
|
1
1
|
import type { HTTPClient } from "./client.js";
|
|
2
2
|
import { CommandHandle } from "./command-handle.js";
|
|
3
3
|
import type { CommandResult, ProcessInfo, RunCommandOptions } from "./models.js";
|
|
4
|
+
/** HTTP request timeout (ms) for a blocking command with the given timeout (s). */
|
|
5
|
+
export declare function commandRequestTimeoutMs(timeoutSeconds?: number): number;
|
|
4
6
|
/** Command execution namespace for a single sandbox. */
|
|
5
7
|
export declare class Commands {
|
|
6
8
|
private sandboxId;
|
|
7
9
|
private client;
|
|
8
10
|
constructor(sandboxId: string, client: HTTPClient);
|
|
9
11
|
private get baseUrl();
|
|
12
|
+
/**
|
|
13
|
+
* Run a shell command. A blocking call waits for the command for as long as
|
|
14
|
+
* its `timeout` allows (default 60 s, at most 3600 s) plus a margin.
|
|
15
|
+
*
|
|
16
|
+
* The request is never retried automatically: after a `502
|
|
17
|
+
* node_connection_lost` the command may have run. Check the sandbox before
|
|
18
|
+
* running it again.
|
|
19
|
+
*/
|
|
10
20
|
run(cmd: string, opts?: RunCommandOptions): Promise<CommandResult>;
|
|
11
21
|
run(cmd: string, opts: RunCommandOptions & {
|
|
12
22
|
background: true;
|
package/dist/commands.js
CHANGED
|
@@ -1,6 +1,30 @@
|
|
|
1
1
|
import { CommandHandle } from "./command-handle.js";
|
|
2
2
|
import { CommandExitException } from "./errors.js";
|
|
3
3
|
import { makeCommandResult } from "./utils.js";
|
|
4
|
+
function parseProcessInfo(p) {
|
|
5
|
+
return {
|
|
6
|
+
pid: p.pid ?? 0,
|
|
7
|
+
command: p.command ?? "",
|
|
8
|
+
running: p.running ?? false,
|
|
9
|
+
exitCode: p.exit_code ?? p.exitCode ?? 0,
|
|
10
|
+
stdout: p.stdout,
|
|
11
|
+
stderr: p.stderr,
|
|
12
|
+
startedAt: p.started_at ?? p.startedAt,
|
|
13
|
+
endedAt: p.ended_at ?? p.endedAt,
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
/** Default command timeout in seconds, and the service's maximum. */
|
|
17
|
+
const DEFAULT_COMMAND_TIMEOUT_S = 60;
|
|
18
|
+
const MAX_COMMAND_TIMEOUT_S = 3600;
|
|
19
|
+
/** Margin on top of the command timeout before the HTTP request gives up. */
|
|
20
|
+
const COMMAND_REQUEST_MARGIN_MS = 15_000;
|
|
21
|
+
/** HTTP request timeout (ms) for a blocking command with the given timeout (s). */
|
|
22
|
+
export function commandRequestTimeoutMs(timeoutSeconds) {
|
|
23
|
+
const seconds = timeoutSeconds != null && timeoutSeconds > 0
|
|
24
|
+
? Math.min(timeoutSeconds, MAX_COMMAND_TIMEOUT_S)
|
|
25
|
+
: DEFAULT_COMMAND_TIMEOUT_S;
|
|
26
|
+
return seconds * 1000 + COMMAND_REQUEST_MARGIN_MS;
|
|
27
|
+
}
|
|
4
28
|
/** Command execution namespace for a single sandbox. */
|
|
5
29
|
export class Commands {
|
|
6
30
|
sandboxId;
|
|
@@ -15,8 +39,10 @@ export class Commands {
|
|
|
15
39
|
async run(cmd, opts) {
|
|
16
40
|
const body = {
|
|
17
41
|
command: cmd,
|
|
18
|
-
timeout: opts?.timeout ??
|
|
42
|
+
timeout: opts?.timeout ?? DEFAULT_COMMAND_TIMEOUT_S,
|
|
19
43
|
};
|
|
44
|
+
// Starting a process is not idempotent: never re-send it.
|
|
45
|
+
const startOpts = { retry: "never", timeoutMs: opts?.requestTimeout };
|
|
20
46
|
if (opts?.cwd)
|
|
21
47
|
body.cwd = opts.cwd;
|
|
22
48
|
if (opts?.envs)
|
|
@@ -25,13 +51,13 @@ export class Commands {
|
|
|
25
51
|
body.user = opts.user;
|
|
26
52
|
if (opts?.background) {
|
|
27
53
|
body.background = true;
|
|
28
|
-
const data = await this.client.post(this.baseUrl, body);
|
|
54
|
+
const data = await this.client.post(this.baseUrl, body, startOpts);
|
|
29
55
|
return new CommandHandle(data.pid, this.sandboxId, this.client);
|
|
30
56
|
}
|
|
31
57
|
// Streaming via callbacks
|
|
32
58
|
if (opts?.onStdout || opts?.onStderr) {
|
|
33
59
|
body.background = true;
|
|
34
|
-
const data = await this.client.post(this.baseUrl, body);
|
|
60
|
+
const data = await this.client.post(this.baseUrl, body, startOpts);
|
|
35
61
|
const handle = new CommandHandle(data.pid, this.sandboxId, this.client);
|
|
36
62
|
const result = await handle.stream({
|
|
37
63
|
onStdout: opts.onStdout,
|
|
@@ -43,7 +69,10 @@ export class Commands {
|
|
|
43
69
|
return result;
|
|
44
70
|
}
|
|
45
71
|
// Synchronous execution
|
|
46
|
-
const data = await this.client.post(this.baseUrl, body
|
|
72
|
+
const data = await this.client.post(this.baseUrl, body, {
|
|
73
|
+
retry: "never",
|
|
74
|
+
timeoutMs: opts?.requestTimeout ?? commandRequestTimeoutMs(opts?.timeout),
|
|
75
|
+
});
|
|
47
76
|
const result = makeCommandResult(data);
|
|
48
77
|
if (result.exitCode !== 0) {
|
|
49
78
|
throw new CommandExitException(result.stdout, result.stderr, result.exitCode);
|
|
@@ -51,16 +80,19 @@ export class Commands {
|
|
|
51
80
|
return result;
|
|
52
81
|
}
|
|
53
82
|
async list() {
|
|
54
|
-
|
|
83
|
+
const data = await this.client.get(this.baseUrl);
|
|
84
|
+
const items = Array.isArray(data) ? data : [];
|
|
85
|
+
return items.map(parseProcessInfo);
|
|
55
86
|
}
|
|
56
87
|
async get(pid) {
|
|
57
|
-
|
|
88
|
+
const data = await this.client.get(`${this.baseUrl}/${pid}`);
|
|
89
|
+
return parseProcessInfo(data);
|
|
58
90
|
}
|
|
59
91
|
async kill(pid) {
|
|
60
92
|
await this.client.delete(`${this.baseUrl}/${pid}`);
|
|
61
93
|
}
|
|
62
94
|
async sendStdin(pid, data) {
|
|
63
|
-
await this.client.post(`${this.baseUrl}/${pid}/stdin`, { data });
|
|
95
|
+
await this.client.post(`${this.baseUrl}/${pid}/stdin`, { data }, { retry: "never" });
|
|
64
96
|
}
|
|
65
97
|
connect(pid) {
|
|
66
98
|
return new CommandHandle(pid, this.sandboxId, this.client);
|
package/dist/config.d.ts
CHANGED
|
@@ -1,12 +1,24 @@
|
|
|
1
|
+
/** Bounds for automatic retries. See the README for which requests are retried. */
|
|
2
|
+
export interface RetryOptions {
|
|
3
|
+
/** Maximum number of attempts per request, including the first. Default `3`. `1` disables retries. */
|
|
4
|
+
maxAttempts?: number;
|
|
5
|
+
/** Maximum total time in milliseconds spent waiting between attempts of one request. Default `30000`. */
|
|
6
|
+
maxTotalWaitMs?: number;
|
|
7
|
+
}
|
|
8
|
+
export declare const DEFAULT_RETRY_MAX_ATTEMPTS = 3;
|
|
9
|
+
export declare const DEFAULT_RETRY_MAX_TOTAL_WAIT_MS = 30000;
|
|
1
10
|
/** Connection options for OmniRun API calls. */
|
|
2
11
|
export interface ConnectionConfig {
|
|
3
12
|
apiUrl: string;
|
|
4
13
|
apiKey: string;
|
|
5
14
|
requestTimeout: number;
|
|
15
|
+
/** Automatic retry bounds, or `false` to switch automatic retries off. */
|
|
16
|
+
retry?: RetryOptions | false;
|
|
6
17
|
}
|
|
7
18
|
/** Resolves runtime config from explicit options, env vars, and defaults. */
|
|
8
19
|
export declare function resolveConfig(opts?: {
|
|
9
20
|
apiUrl?: string;
|
|
10
21
|
apiKey?: string;
|
|
11
22
|
requestTimeout?: number;
|
|
23
|
+
retry?: RetryOptions | false;
|
|
12
24
|
}): ConnectionConfig;
|
package/dist/config.js
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
export const DEFAULT_RETRY_MAX_ATTEMPTS = 3;
|
|
2
|
+
export const DEFAULT_RETRY_MAX_TOTAL_WAIT_MS = 30_000;
|
|
1
3
|
/** Resolves runtime config from explicit options, env vars, and defaults. */
|
|
2
4
|
export function resolveConfig(opts) {
|
|
3
5
|
return {
|
|
@@ -12,5 +14,6 @@ export function resolveConfig(opts) {
|
|
|
12
14
|
: undefined) ||
|
|
13
15
|
"",
|
|
14
16
|
requestTimeout: opts?.requestTimeout ?? 60_000,
|
|
17
|
+
retry: opts?.retry,
|
|
15
18
|
};
|
|
16
19
|
}
|
package/dist/contexts.js
CHANGED
|
@@ -38,7 +38,7 @@ export class Contexts {
|
|
|
38
38
|
if (opts?.onStdout || opts?.onStderr || opts?.onResult) {
|
|
39
39
|
return this.streamExecute(contextId, code, opts);
|
|
40
40
|
}
|
|
41
|
-
const data = await this.client.post(`${this.baseUrl}/${contextId}/execute`, { code });
|
|
41
|
+
const data = await this.client.post(`${this.baseUrl}/${contextId}/execute`, { code }, { retry: "never" });
|
|
42
42
|
return parseExecutionResult(data);
|
|
43
43
|
}
|
|
44
44
|
/** Delete an interpreter context. */
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import type { HTTPClient } from "./client.js";
|
|
2
|
+
export interface ScreenInfo {
|
|
3
|
+
width: number;
|
|
4
|
+
height: number;
|
|
5
|
+
cursorX: number;
|
|
6
|
+
cursorY: number;
|
|
7
|
+
}
|
|
8
|
+
export interface DesktopStreamInfo {
|
|
9
|
+
novncPort: number;
|
|
10
|
+
vncPort: number;
|
|
11
|
+
wsPath: string;
|
|
12
|
+
}
|
|
13
|
+
export type MouseAction = "click" | "doubleClick" | "rightClick" | "move" | "drag" | "scroll";
|
|
14
|
+
export type ScrollDirection = "up" | "down" | "left" | "right";
|
|
15
|
+
export type KeyboardAction = "type" | "press";
|
|
16
|
+
export interface MouseOptions {
|
|
17
|
+
action: MouseAction;
|
|
18
|
+
x?: number;
|
|
19
|
+
y?: number;
|
|
20
|
+
endX?: number;
|
|
21
|
+
endY?: number;
|
|
22
|
+
direction?: ScrollDirection;
|
|
23
|
+
amount?: number;
|
|
24
|
+
}
|
|
25
|
+
export interface KeyboardOptions {
|
|
26
|
+
action: KeyboardAction;
|
|
27
|
+
text?: string;
|
|
28
|
+
key?: string;
|
|
29
|
+
}
|
|
30
|
+
/** Desktop control namespace for GUI sandboxes. */
|
|
31
|
+
export declare class Desktop {
|
|
32
|
+
private sandboxId;
|
|
33
|
+
private client;
|
|
34
|
+
constructor(sandboxId: string, client: HTTPClient);
|
|
35
|
+
private get baseUrl();
|
|
36
|
+
/** Capture a screenshot of the desktop as a PNG image. */
|
|
37
|
+
screenshot(): Promise<Uint8Array>;
|
|
38
|
+
/** Send a mouse action to the desktop. */
|
|
39
|
+
mouse(opts: MouseOptions): Promise<void>;
|
|
40
|
+
/** Send a keyboard action to the desktop. */
|
|
41
|
+
keyboard(opts: KeyboardOptions): Promise<void>;
|
|
42
|
+
/** Get current screen dimensions and cursor position. */
|
|
43
|
+
getScreen(): Promise<ScreenInfo>;
|
|
44
|
+
/** Get streaming connection info (noVNC / VNC ports and WebSocket path). */
|
|
45
|
+
getStreamInfo(): Promise<DesktopStreamInfo>;
|
|
46
|
+
/** Left-click at the given coordinates. */
|
|
47
|
+
leftClick(x: number, y: number): Promise<void>;
|
|
48
|
+
/** Right-click at the given coordinates. */
|
|
49
|
+
rightClick(x: number, y: number): Promise<void>;
|
|
50
|
+
/** Double-click at the given coordinates. */
|
|
51
|
+
doubleClick(x: number, y: number): Promise<void>;
|
|
52
|
+
/** Move the mouse cursor to the given coordinates. */
|
|
53
|
+
moveMouse(x: number, y: number): Promise<void>;
|
|
54
|
+
/** Drag from (startX, startY) to (endX, endY). */
|
|
55
|
+
drag(startX: number, startY: number, endX: number, endY: number): Promise<void>;
|
|
56
|
+
/** Scroll in a direction. */
|
|
57
|
+
scroll(direction: ScrollDirection, amount?: number): Promise<void>;
|
|
58
|
+
/** Type text character by character. */
|
|
59
|
+
type(text: string): Promise<void>;
|
|
60
|
+
/** Press a key or key combination (e.g. "Enter", "ctrl+c"). */
|
|
61
|
+
press(key: string): Promise<void>;
|
|
62
|
+
/** Get the current screen dimensions. */
|
|
63
|
+
getScreenSize(): Promise<{
|
|
64
|
+
width: number;
|
|
65
|
+
height: number;
|
|
66
|
+
}>;
|
|
67
|
+
/** Get the current cursor position. */
|
|
68
|
+
getCursorPosition(): Promise<{
|
|
69
|
+
x: number;
|
|
70
|
+
y: number;
|
|
71
|
+
}>;
|
|
72
|
+
}
|
package/dist/desktop.js
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
/** Desktop control namespace for GUI sandboxes. */
|
|
2
|
+
export class Desktop {
|
|
3
|
+
sandboxId;
|
|
4
|
+
client;
|
|
5
|
+
constructor(sandboxId, client) {
|
|
6
|
+
this.sandboxId = sandboxId;
|
|
7
|
+
this.client = client;
|
|
8
|
+
}
|
|
9
|
+
get baseUrl() {
|
|
10
|
+
return `/sandboxes/${this.sandboxId}/desktop`;
|
|
11
|
+
}
|
|
12
|
+
/** Capture a screenshot of the desktop as a PNG image. */
|
|
13
|
+
async screenshot() {
|
|
14
|
+
return this.client.download(`${this.baseUrl}/screenshot`);
|
|
15
|
+
}
|
|
16
|
+
/** Send a mouse action to the desktop. */
|
|
17
|
+
async mouse(opts) {
|
|
18
|
+
await this.client.post(`${this.baseUrl}/mouse`, opts);
|
|
19
|
+
}
|
|
20
|
+
/** Send a keyboard action to the desktop. */
|
|
21
|
+
async keyboard(opts) {
|
|
22
|
+
await this.client.post(`${this.baseUrl}/keyboard`, opts);
|
|
23
|
+
}
|
|
24
|
+
/** Get current screen dimensions and cursor position. */
|
|
25
|
+
async getScreen() {
|
|
26
|
+
const data = await this.client.get(`${this.baseUrl}/screen`);
|
|
27
|
+
return {
|
|
28
|
+
width: data.width ?? 0,
|
|
29
|
+
height: data.height ?? 0,
|
|
30
|
+
cursorX: data.cursorX ?? 0,
|
|
31
|
+
cursorY: data.cursorY ?? 0,
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
/** Get streaming connection info (noVNC / VNC ports and WebSocket path). */
|
|
35
|
+
async getStreamInfo() {
|
|
36
|
+
const data = await this.client.get(`${this.baseUrl}/stream`);
|
|
37
|
+
return {
|
|
38
|
+
novncPort: data.novncPort ?? 0,
|
|
39
|
+
vncPort: data.vncPort ?? 0,
|
|
40
|
+
wsPath: data.wsPath ?? "",
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
// ── Convenience methods (E2B parity) ──────────────────────────────────
|
|
44
|
+
/** Left-click at the given coordinates. */
|
|
45
|
+
async leftClick(x, y) {
|
|
46
|
+
await this.mouse({ action: "click", x, y });
|
|
47
|
+
}
|
|
48
|
+
/** Right-click at the given coordinates. */
|
|
49
|
+
async rightClick(x, y) {
|
|
50
|
+
await this.mouse({ action: "rightClick", x, y });
|
|
51
|
+
}
|
|
52
|
+
/** Double-click at the given coordinates. */
|
|
53
|
+
async doubleClick(x, y) {
|
|
54
|
+
await this.mouse({ action: "doubleClick", x, y });
|
|
55
|
+
}
|
|
56
|
+
/** Move the mouse cursor to the given coordinates. */
|
|
57
|
+
async moveMouse(x, y) {
|
|
58
|
+
await this.mouse({ action: "move", x, y });
|
|
59
|
+
}
|
|
60
|
+
/** Drag from (startX, startY) to (endX, endY). */
|
|
61
|
+
async drag(startX, startY, endX, endY) {
|
|
62
|
+
await this.mouse({ action: "drag", x: startX, y: startY, endX, endY });
|
|
63
|
+
}
|
|
64
|
+
/** Scroll in a direction. */
|
|
65
|
+
async scroll(direction, amount) {
|
|
66
|
+
await this.mouse({ action: "scroll", direction, amount });
|
|
67
|
+
}
|
|
68
|
+
/** Type text character by character. */
|
|
69
|
+
async type(text) {
|
|
70
|
+
await this.keyboard({ action: "type", text });
|
|
71
|
+
}
|
|
72
|
+
/** Press a key or key combination (e.g. "Enter", "ctrl+c"). */
|
|
73
|
+
async press(key) {
|
|
74
|
+
await this.keyboard({ action: "press", key });
|
|
75
|
+
}
|
|
76
|
+
/** Get the current screen dimensions. */
|
|
77
|
+
async getScreenSize() {
|
|
78
|
+
const screen = await this.getScreen();
|
|
79
|
+
return { width: screen.width, height: screen.height };
|
|
80
|
+
}
|
|
81
|
+
/** Get the current cursor position. */
|
|
82
|
+
async getCursorPosition() {
|
|
83
|
+
const screen = await this.getScreen();
|
|
84
|
+
return { x: screen.cursorX, y: screen.cursorY };
|
|
85
|
+
}
|
|
86
|
+
}
|