@omnirun/sdk 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/CHANGELOG.md +10 -0
- package/LICENSE +21 -0
- package/README.md +108 -0
- package/dist/client.d.ts +26 -0
- package/dist/client.js +214 -0
- package/dist/command-handle.d.ts +19 -0
- package/dist/command-handle.js +79 -0
- package/dist/commands.d.ts +19 -0
- package/dist/commands.js +68 -0
- package/dist/config.d.ts +12 -0
- package/dist/config.js +16 -0
- package/dist/contexts.d.ts +27 -0
- package/dist/contexts.js +113 -0
- package/dist/e2ee.d.ts +42 -0
- package/dist/e2ee.js +107 -0
- package/dist/errors.d.ts +18 -0
- package/dist/errors.js +36 -0
- package/dist/filesystem.d.ts +35 -0
- package/dist/filesystem.js +124 -0
- package/dist/index.d.ts +19 -0
- package/dist/index.js +14 -0
- package/dist/models.d.ts +154 -0
- package/dist/models.js +1 -0
- package/dist/production.d.ts +23 -0
- package/dist/production.js +70 -0
- package/dist/pty.d.ts +26 -0
- package/dist/pty.js +49 -0
- package/dist/sandbox.d.ts +66 -0
- package/dist/sandbox.js +275 -0
- package/dist/utils.d.ts +8 -0
- package/dist/utils.js +11 -0
- package/dist/watch-handle.d.ts +16 -0
- package/dist/watch-handle.js +45 -0
- package/dist/webhooks.d.ts +15 -0
- package/dist/webhooks.js +25 -0
- package/package.json +63 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to this project will be documented in this file.
|
|
4
|
+
|
|
5
|
+
## 0.3.0 - 2026-03-04
|
|
6
|
+
|
|
7
|
+
- First standalone release of `@omnirun/sdk`.
|
|
8
|
+
- Full sandbox SDK surface extracted from OmniRun monorepo.
|
|
9
|
+
- API contract fixes for metadata filters and context stream execution.
|
|
10
|
+
- Added Changesets and CI/publish workflows.
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 OmniRun
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
# @omnirun/sdk
|
|
2
|
+
|
|
3
|
+
TypeScript SDK for OmniRun sandboxes.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @omnirun/sdk
|
|
9
|
+
pnpm add @omnirun/sdk
|
|
10
|
+
bun add @omnirun/sdk
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Configuration
|
|
14
|
+
|
|
15
|
+
The SDK resolves configuration in this order:
|
|
16
|
+
|
|
17
|
+
1. Explicit options passed to SDK methods.
|
|
18
|
+
2. Environment variables: `OMNIRUN_API_URL`, `OMNIRUN_API_KEY`.
|
|
19
|
+
3. Default API URL: `https://api.omnirun.io`.
|
|
20
|
+
|
|
21
|
+
## Quickstart
|
|
22
|
+
|
|
23
|
+
```ts
|
|
24
|
+
import { Sandbox } from "@omnirun/sdk";
|
|
25
|
+
|
|
26
|
+
const sbx = await Sandbox.create("python-3.11", {
|
|
27
|
+
apiKey: process.env.OMNIRUN_API_KEY,
|
|
28
|
+
apiUrl: process.env.OMNIRUN_API_URL,
|
|
29
|
+
timeout: 300,
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
const result = await sbx.commands.run("python3 -c \"print('hello')\"");
|
|
33
|
+
console.log(result.stdout);
|
|
34
|
+
|
|
35
|
+
await sbx.kill();
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
## Files
|
|
39
|
+
|
|
40
|
+
```ts
|
|
41
|
+
await sbx.files.write("/tmp/input.txt", "hello");
|
|
42
|
+
const content = await sbx.files.read("/tmp/input.txt");
|
|
43
|
+
const bytes = await sbx.files.read("/tmp/input.txt", "bytes");
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
## Streaming
|
|
47
|
+
|
|
48
|
+
```ts
|
|
49
|
+
const command = await sbx.commands.run("for i in 1 2 3; do echo $i; sleep 1; done", {
|
|
50
|
+
background: true,
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
for await (const event of command) {
|
|
54
|
+
if (event.type === "stdout") {
|
|
55
|
+
process.stdout.write(event.data ?? "");
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
## Integration tests
|
|
61
|
+
|
|
62
|
+
Integration tests are opt-in:
|
|
63
|
+
|
|
64
|
+
```bash
|
|
65
|
+
OMNIRUN_API_URL=https://api.omnirun.io \
|
|
66
|
+
OMNIRUN_API_KEY=<key> \
|
|
67
|
+
RUN_OMNIRUN_INTEGRATION=1 \
|
|
68
|
+
npm run test:integration
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
## Hosted API smoke examples
|
|
72
|
+
|
|
73
|
+
```bash
|
|
74
|
+
OMNIRUN_API_URL=https://<your-api-host> \
|
|
75
|
+
OMNIRUN_API_KEY=<key> \
|
|
76
|
+
npm run examples:hosted
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
Hosted example suites live in `examples/hosted/`.
|
|
80
|
+
|
|
81
|
+
## E2EE scaffold (client key bootstrap)
|
|
82
|
+
|
|
83
|
+
The SDK can generate a client keypair and send `clientPublicKey` during sandbox creation:
|
|
84
|
+
|
|
85
|
+
```ts
|
|
86
|
+
import { Sandbox } from "@omnirun/sdk";
|
|
87
|
+
|
|
88
|
+
const sbx = await Sandbox.create("python-3.11", {
|
|
89
|
+
apiUrl: process.env.OMNIRUN_API_URL,
|
|
90
|
+
apiKey: process.env.OMNIRUN_API_KEY,
|
|
91
|
+
e2ee: true,
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
console.log(sbx.e2ee?.clientPublicKey);
|
|
95
|
+
console.log(sbx.e2ee?.serverPublicKey); // available when backend returns it
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
Protocol scaffold details: `docs/E2EE-PROTOCOL-SCAFFOLD.md`.
|
|
99
|
+
|
|
100
|
+
## API Surface
|
|
101
|
+
|
|
102
|
+
- `Sandbox`
|
|
103
|
+
- `Commands` / `CommandHandle`
|
|
104
|
+
- `Filesystem`
|
|
105
|
+
- `Pty` / `PtySession`
|
|
106
|
+
- `Contexts`
|
|
107
|
+
- `Production`
|
|
108
|
+
- `Webhooks`
|
package/dist/client.d.ts
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { ConnectionConfig } from "./config.js";
|
|
2
|
+
import type { StreamEvent } from "./models.js";
|
|
3
|
+
/** Low-level HTTP transport for OmniRun API requests. */
|
|
4
|
+
export declare class HTTPClient {
|
|
5
|
+
private config;
|
|
6
|
+
constructor(config: ConnectionConfig);
|
|
7
|
+
get baseUrl(): string;
|
|
8
|
+
private get headers();
|
|
9
|
+
get<T = any>(path: string, params?: Record<string, string>): Promise<T>;
|
|
10
|
+
post<T = any>(path: string, body?: any): Promise<T>;
|
|
11
|
+
delete(path: string, params?: Record<string, string>): Promise<void>;
|
|
12
|
+
download(path: string, params?: Record<string, string>): Promise<Uint8Array>;
|
|
13
|
+
streamDownload(path: string, params?: Record<string, string>): Promise<ReadableStream<Uint8Array>>;
|
|
14
|
+
getWithHeaders<T = any>(path: string, params?: Record<string, string>): Promise<{
|
|
15
|
+
data: T;
|
|
16
|
+
headers: Headers;
|
|
17
|
+
}>;
|
|
18
|
+
upload(path: string, filePath: string, content: Uint8Array, filename: string): Promise<any>;
|
|
19
|
+
streamSSE(path: string, params?: Record<string, string>): AsyncGenerator<StreamEvent>;
|
|
20
|
+
streamSSEPost(path: string, body?: unknown, signal?: AbortSignal): AsyncGenerator<StreamEvent>;
|
|
21
|
+
streamSSEWithSignal(path: string, params?: Record<string, string>, signal?: AbortSignal): AsyncGenerator<StreamEvent>;
|
|
22
|
+
private buildUrl;
|
|
23
|
+
private handle;
|
|
24
|
+
private parseSSE;
|
|
25
|
+
private errorMessage;
|
|
26
|
+
}
|
package/dist/client.js
ADDED
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
import { SandboxError, StreamError } from "./errors.js";
|
|
2
|
+
/** Low-level HTTP transport for OmniRun API requests. */
|
|
3
|
+
export class HTTPClient {
|
|
4
|
+
config;
|
|
5
|
+
constructor(config) {
|
|
6
|
+
this.config = config;
|
|
7
|
+
}
|
|
8
|
+
get baseUrl() {
|
|
9
|
+
return this.config.apiUrl;
|
|
10
|
+
}
|
|
11
|
+
get headers() {
|
|
12
|
+
const h = { "Content-Type": "application/json" };
|
|
13
|
+
if (this.config.apiKey) {
|
|
14
|
+
h["X-API-Key"] = this.config.apiKey;
|
|
15
|
+
}
|
|
16
|
+
return h;
|
|
17
|
+
}
|
|
18
|
+
async get(path, params) {
|
|
19
|
+
const url = this.buildUrl(path, params);
|
|
20
|
+
const resp = await fetch(url, {
|
|
21
|
+
method: "GET",
|
|
22
|
+
headers: this.headers,
|
|
23
|
+
signal: AbortSignal.timeout(this.config.requestTimeout),
|
|
24
|
+
});
|
|
25
|
+
return this.handle(resp);
|
|
26
|
+
}
|
|
27
|
+
async post(path, body) {
|
|
28
|
+
const resp = await fetch(this.buildUrl(path), {
|
|
29
|
+
method: "POST",
|
|
30
|
+
headers: this.headers,
|
|
31
|
+
body: body != null ? JSON.stringify(body) : undefined,
|
|
32
|
+
signal: AbortSignal.timeout(this.config.requestTimeout),
|
|
33
|
+
});
|
|
34
|
+
return this.handle(resp);
|
|
35
|
+
}
|
|
36
|
+
async delete(path, params) {
|
|
37
|
+
const url = this.buildUrl(path, params);
|
|
38
|
+
const resp = await fetch(url, {
|
|
39
|
+
method: "DELETE",
|
|
40
|
+
headers: this.headers,
|
|
41
|
+
signal: AbortSignal.timeout(this.config.requestTimeout),
|
|
42
|
+
});
|
|
43
|
+
if (resp.status === 204)
|
|
44
|
+
return;
|
|
45
|
+
if (!resp.ok) {
|
|
46
|
+
const text = await resp.text();
|
|
47
|
+
throw new SandboxError(`API error ${resp.status}: ${text}`);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
async download(path, params) {
|
|
51
|
+
const url = this.buildUrl(path, params);
|
|
52
|
+
const resp = await fetch(url, {
|
|
53
|
+
method: "GET",
|
|
54
|
+
headers: { "X-API-Key": this.config.apiKey || "" },
|
|
55
|
+
signal: AbortSignal.timeout(this.config.requestTimeout),
|
|
56
|
+
});
|
|
57
|
+
if (!resp.ok) {
|
|
58
|
+
throw new SandboxError(`Download error ${resp.status}: ${await resp.text()}`);
|
|
59
|
+
}
|
|
60
|
+
return new Uint8Array(await resp.arrayBuffer());
|
|
61
|
+
}
|
|
62
|
+
async streamDownload(path, params) {
|
|
63
|
+
const url = this.buildUrl(path, params);
|
|
64
|
+
const resp = await fetch(url, {
|
|
65
|
+
method: "GET",
|
|
66
|
+
headers: { "X-API-Key": this.config.apiKey || "" },
|
|
67
|
+
});
|
|
68
|
+
if (!resp.ok) {
|
|
69
|
+
throw new SandboxError(`Download error ${resp.status}: ${await resp.text()}`);
|
|
70
|
+
}
|
|
71
|
+
if (!resp.body) {
|
|
72
|
+
throw new SandboxError("No response body for stream download");
|
|
73
|
+
}
|
|
74
|
+
return resp.body;
|
|
75
|
+
}
|
|
76
|
+
async getWithHeaders(path, params) {
|
|
77
|
+
const url = this.buildUrl(path, params);
|
|
78
|
+
const resp = await fetch(url, {
|
|
79
|
+
method: "GET",
|
|
80
|
+
headers: this.headers,
|
|
81
|
+
signal: AbortSignal.timeout(this.config.requestTimeout),
|
|
82
|
+
});
|
|
83
|
+
const data = await this.handle(resp);
|
|
84
|
+
return { data, headers: resp.headers };
|
|
85
|
+
}
|
|
86
|
+
async upload(path, filePath, content, filename) {
|
|
87
|
+
const form = new FormData();
|
|
88
|
+
form.append("path", filePath);
|
|
89
|
+
form.append("file", new Blob([content]), filename);
|
|
90
|
+
const headers = {};
|
|
91
|
+
if (this.config.apiKey) {
|
|
92
|
+
headers["X-API-Key"] = this.config.apiKey;
|
|
93
|
+
}
|
|
94
|
+
const resp = await fetch(this.buildUrl(path), {
|
|
95
|
+
method: "POST",
|
|
96
|
+
headers,
|
|
97
|
+
body: form,
|
|
98
|
+
signal: AbortSignal.timeout(this.config.requestTimeout),
|
|
99
|
+
});
|
|
100
|
+
return this.handle(resp);
|
|
101
|
+
}
|
|
102
|
+
async *streamSSE(path, params) {
|
|
103
|
+
const url = this.buildUrl(path, params);
|
|
104
|
+
const resp = await fetch(url, {
|
|
105
|
+
method: "GET",
|
|
106
|
+
headers: {
|
|
107
|
+
...this.headers,
|
|
108
|
+
Accept: "text/event-stream",
|
|
109
|
+
},
|
|
110
|
+
});
|
|
111
|
+
yield* this.parseSSE(resp);
|
|
112
|
+
}
|
|
113
|
+
async *streamSSEPost(path, body, signal) {
|
|
114
|
+
const resp = await fetch(this.buildUrl(path), {
|
|
115
|
+
method: "POST",
|
|
116
|
+
headers: {
|
|
117
|
+
...this.headers,
|
|
118
|
+
Accept: "text/event-stream",
|
|
119
|
+
},
|
|
120
|
+
body: body != null ? JSON.stringify(body) : undefined,
|
|
121
|
+
signal,
|
|
122
|
+
});
|
|
123
|
+
yield* this.parseSSE(resp);
|
|
124
|
+
}
|
|
125
|
+
async *streamSSEWithSignal(path, params, signal) {
|
|
126
|
+
const url = this.buildUrl(path, params);
|
|
127
|
+
const resp = await fetch(url, {
|
|
128
|
+
method: "GET",
|
|
129
|
+
headers: {
|
|
130
|
+
...this.headers,
|
|
131
|
+
Accept: "text/event-stream",
|
|
132
|
+
},
|
|
133
|
+
signal,
|
|
134
|
+
});
|
|
135
|
+
yield* this.parseSSE(resp);
|
|
136
|
+
}
|
|
137
|
+
buildUrl(path, params) {
|
|
138
|
+
const url = new URL(path, this.config.apiUrl);
|
|
139
|
+
if (params) {
|
|
140
|
+
for (const [key, val] of Object.entries(params)) {
|
|
141
|
+
url.searchParams.set(key, val);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
return url.toString();
|
|
145
|
+
}
|
|
146
|
+
async handle(resp) {
|
|
147
|
+
if (!resp.ok) {
|
|
148
|
+
const msg = await this.errorMessage(resp);
|
|
149
|
+
throw new SandboxError(`API error ${resp.status}: ${msg}`);
|
|
150
|
+
}
|
|
151
|
+
if (resp.status === 204) {
|
|
152
|
+
return undefined;
|
|
153
|
+
}
|
|
154
|
+
const contentType = resp.headers.get("content-type") || "";
|
|
155
|
+
if (contentType.includes("application/json")) {
|
|
156
|
+
return resp.json();
|
|
157
|
+
}
|
|
158
|
+
return (await resp.text());
|
|
159
|
+
}
|
|
160
|
+
async *parseSSE(resp) {
|
|
161
|
+
if (!resp.ok) {
|
|
162
|
+
throw new StreamError(`Stream error ${resp.status}: ${await this.errorMessage(resp)}`);
|
|
163
|
+
}
|
|
164
|
+
const reader = resp.body?.getReader();
|
|
165
|
+
if (!reader)
|
|
166
|
+
throw new StreamError("No response body");
|
|
167
|
+
const decoder = new TextDecoder();
|
|
168
|
+
let buffer = "";
|
|
169
|
+
try {
|
|
170
|
+
while (true) {
|
|
171
|
+
const { done, value } = await reader.read();
|
|
172
|
+
if (done)
|
|
173
|
+
break;
|
|
174
|
+
buffer += decoder.decode(value, { stream: true });
|
|
175
|
+
const lines = buffer.split("\n");
|
|
176
|
+
buffer = lines.pop() || "";
|
|
177
|
+
for (const line of lines) {
|
|
178
|
+
if (line.startsWith("data: ")) {
|
|
179
|
+
try {
|
|
180
|
+
yield JSON.parse(line.slice(6));
|
|
181
|
+
}
|
|
182
|
+
catch {
|
|
183
|
+
// Skip malformed events
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
finally {
|
|
190
|
+
reader.releaseLock();
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
async errorMessage(resp) {
|
|
194
|
+
const raw = await resp.text();
|
|
195
|
+
if (!raw)
|
|
196
|
+
return "request failed";
|
|
197
|
+
try {
|
|
198
|
+
const parsed = JSON.parse(raw);
|
|
199
|
+
if (typeof parsed === "string")
|
|
200
|
+
return parsed;
|
|
201
|
+
if (parsed && typeof parsed === "object") {
|
|
202
|
+
const obj = parsed;
|
|
203
|
+
if (typeof obj.message === "string")
|
|
204
|
+
return obj.message;
|
|
205
|
+
if (typeof obj.error === "string")
|
|
206
|
+
return obj.error;
|
|
207
|
+
}
|
|
208
|
+
return raw;
|
|
209
|
+
}
|
|
210
|
+
catch {
|
|
211
|
+
return raw;
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { HTTPClient } from "./client.js";
|
|
2
|
+
import type { CommandResult, ProcessInfo, StreamEvent } from "./models.js";
|
|
3
|
+
/** Handle for a background command process. */
|
|
4
|
+
export declare class CommandHandle {
|
|
5
|
+
readonly pid: number;
|
|
6
|
+
private sandboxId;
|
|
7
|
+
private client;
|
|
8
|
+
private _result;
|
|
9
|
+
constructor(pid: number, sandboxId: string, client: HTTPClient);
|
|
10
|
+
private get url();
|
|
11
|
+
wait(timeout?: number): Promise<CommandResult>;
|
|
12
|
+
getInfo(): Promise<ProcessInfo>;
|
|
13
|
+
kill(): Promise<void>;
|
|
14
|
+
stream(opts?: {
|
|
15
|
+
onStdout?: (data: string) => void;
|
|
16
|
+
onStderr?: (data: string) => void;
|
|
17
|
+
}): Promise<CommandResult>;
|
|
18
|
+
[Symbol.asyncIterator](): AsyncGenerator<StreamEvent>;
|
|
19
|
+
}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { TimeoutError } from "./errors.js";
|
|
2
|
+
import { makeCommandResult } from "./utils.js";
|
|
3
|
+
/** Handle for a background command process. */
|
|
4
|
+
export class CommandHandle {
|
|
5
|
+
pid;
|
|
6
|
+
sandboxId;
|
|
7
|
+
client;
|
|
8
|
+
_result = null;
|
|
9
|
+
constructor(pid, sandboxId, client) {
|
|
10
|
+
this.pid = pid;
|
|
11
|
+
this.sandboxId = sandboxId;
|
|
12
|
+
this.client = client;
|
|
13
|
+
}
|
|
14
|
+
get url() {
|
|
15
|
+
return `/sandboxes/${this.sandboxId}/commands/${this.pid}`;
|
|
16
|
+
}
|
|
17
|
+
async wait(timeout = 60_000) {
|
|
18
|
+
if (this._result)
|
|
19
|
+
return this._result;
|
|
20
|
+
const deadline = Date.now() + timeout;
|
|
21
|
+
while (Date.now() < deadline) {
|
|
22
|
+
const info = await this.getInfo();
|
|
23
|
+
if (!info.running) {
|
|
24
|
+
this._result = makeCommandResult({
|
|
25
|
+
stdout: info.stdout || "",
|
|
26
|
+
stderr: info.stderr || "",
|
|
27
|
+
exitCode: info.exitCode,
|
|
28
|
+
});
|
|
29
|
+
return this._result;
|
|
30
|
+
}
|
|
31
|
+
await new Promise((r) => setTimeout(r, 500));
|
|
32
|
+
}
|
|
33
|
+
throw new TimeoutError(`Process ${this.pid} did not finish within ${timeout}ms`);
|
|
34
|
+
}
|
|
35
|
+
async getInfo() {
|
|
36
|
+
const raw = await this.client.get(this.url);
|
|
37
|
+
return {
|
|
38
|
+
pid: raw.pid,
|
|
39
|
+
command: raw.command,
|
|
40
|
+
running: raw.running,
|
|
41
|
+
exitCode: raw.exit_code ?? raw.exitCode ?? 0,
|
|
42
|
+
stdout: raw.stdout,
|
|
43
|
+
stderr: raw.stderr,
|
|
44
|
+
startedAt: raw.started_at ?? raw.startedAt,
|
|
45
|
+
endedAt: raw.ended_at ?? raw.endedAt,
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
async kill() {
|
|
49
|
+
await this.client.delete(this.url);
|
|
50
|
+
}
|
|
51
|
+
async stream(opts) {
|
|
52
|
+
const stdoutParts = [];
|
|
53
|
+
const stderrParts = [];
|
|
54
|
+
let exitCode = 0;
|
|
55
|
+
for await (const event of this.client.streamSSE(`${this.url}/stream`)) {
|
|
56
|
+
if (event.type === "stdout" && event.data) {
|
|
57
|
+
stdoutParts.push(event.data);
|
|
58
|
+
opts?.onStdout?.(event.data);
|
|
59
|
+
}
|
|
60
|
+
else if (event.type === "stderr" && event.data) {
|
|
61
|
+
stderrParts.push(event.data);
|
|
62
|
+
opts?.onStderr?.(event.data);
|
|
63
|
+
}
|
|
64
|
+
else if (event.type === "exit") {
|
|
65
|
+
exitCode = event.exit_code ?? 0;
|
|
66
|
+
break;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
this._result = makeCommandResult({
|
|
70
|
+
stdout: stdoutParts.join(""),
|
|
71
|
+
stderr: stderrParts.join(""),
|
|
72
|
+
exitCode,
|
|
73
|
+
});
|
|
74
|
+
return this._result;
|
|
75
|
+
}
|
|
76
|
+
async *[Symbol.asyncIterator]() {
|
|
77
|
+
yield* this.client.streamSSE(`${this.url}/stream`);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { HTTPClient } from "./client.js";
|
|
2
|
+
import { CommandHandle } from "./command-handle.js";
|
|
3
|
+
import type { CommandResult, ProcessInfo, RunCommandOptions } from "./models.js";
|
|
4
|
+
/** Command execution namespace for a single sandbox. */
|
|
5
|
+
export declare class Commands {
|
|
6
|
+
private sandboxId;
|
|
7
|
+
private client;
|
|
8
|
+
constructor(sandboxId: string, client: HTTPClient);
|
|
9
|
+
private get baseUrl();
|
|
10
|
+
run(cmd: string, opts?: RunCommandOptions): Promise<CommandResult>;
|
|
11
|
+
run(cmd: string, opts: RunCommandOptions & {
|
|
12
|
+
background: true;
|
|
13
|
+
}): Promise<CommandHandle>;
|
|
14
|
+
list(): Promise<ProcessInfo[]>;
|
|
15
|
+
get(pid: number): Promise<ProcessInfo>;
|
|
16
|
+
kill(pid: number): Promise<void>;
|
|
17
|
+
sendStdin(pid: number, data: string): Promise<void>;
|
|
18
|
+
connect(pid: number): CommandHandle;
|
|
19
|
+
}
|
package/dist/commands.js
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { CommandHandle } from "./command-handle.js";
|
|
2
|
+
import { CommandExitException } from "./errors.js";
|
|
3
|
+
import { makeCommandResult } from "./utils.js";
|
|
4
|
+
/** Command execution namespace for a single sandbox. */
|
|
5
|
+
export class Commands {
|
|
6
|
+
sandboxId;
|
|
7
|
+
client;
|
|
8
|
+
constructor(sandboxId, client) {
|
|
9
|
+
this.sandboxId = sandboxId;
|
|
10
|
+
this.client = client;
|
|
11
|
+
}
|
|
12
|
+
get baseUrl() {
|
|
13
|
+
return `/sandboxes/${this.sandboxId}/commands`;
|
|
14
|
+
}
|
|
15
|
+
async run(cmd, opts) {
|
|
16
|
+
const body = {
|
|
17
|
+
command: cmd,
|
|
18
|
+
timeout: opts?.timeout ?? 60,
|
|
19
|
+
};
|
|
20
|
+
if (opts?.cwd)
|
|
21
|
+
body.cwd = opts.cwd;
|
|
22
|
+
if (opts?.envs)
|
|
23
|
+
body.envs = opts.envs;
|
|
24
|
+
if (opts?.user)
|
|
25
|
+
body.user = opts.user;
|
|
26
|
+
if (opts?.background) {
|
|
27
|
+
body.background = true;
|
|
28
|
+
const data = await this.client.post(this.baseUrl, body);
|
|
29
|
+
return new CommandHandle(data.pid, this.sandboxId, this.client);
|
|
30
|
+
}
|
|
31
|
+
// Streaming via callbacks
|
|
32
|
+
if (opts?.onStdout || opts?.onStderr) {
|
|
33
|
+
body.background = true;
|
|
34
|
+
const data = await this.client.post(this.baseUrl, body);
|
|
35
|
+
const handle = new CommandHandle(data.pid, this.sandboxId, this.client);
|
|
36
|
+
const result = await handle.stream({
|
|
37
|
+
onStdout: opts.onStdout,
|
|
38
|
+
onStderr: opts.onStderr,
|
|
39
|
+
});
|
|
40
|
+
if (result.exitCode !== 0) {
|
|
41
|
+
throw new CommandExitException(result.stdout, result.stderr, result.exitCode);
|
|
42
|
+
}
|
|
43
|
+
return result;
|
|
44
|
+
}
|
|
45
|
+
// Synchronous execution
|
|
46
|
+
const data = await this.client.post(this.baseUrl, body);
|
|
47
|
+
const result = makeCommandResult(data);
|
|
48
|
+
if (result.exitCode !== 0) {
|
|
49
|
+
throw new CommandExitException(result.stdout, result.stderr, result.exitCode);
|
|
50
|
+
}
|
|
51
|
+
return result;
|
|
52
|
+
}
|
|
53
|
+
async list() {
|
|
54
|
+
return this.client.get(this.baseUrl);
|
|
55
|
+
}
|
|
56
|
+
async get(pid) {
|
|
57
|
+
return this.client.get(`${this.baseUrl}/${pid}`);
|
|
58
|
+
}
|
|
59
|
+
async kill(pid) {
|
|
60
|
+
await this.client.delete(`${this.baseUrl}/${pid}`);
|
|
61
|
+
}
|
|
62
|
+
async sendStdin(pid, data) {
|
|
63
|
+
await this.client.post(`${this.baseUrl}/${pid}/stdin`, { data });
|
|
64
|
+
}
|
|
65
|
+
connect(pid) {
|
|
66
|
+
return new CommandHandle(pid, this.sandboxId, this.client);
|
|
67
|
+
}
|
|
68
|
+
}
|
package/dist/config.d.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/** Connection options for OmniRun API calls. */
|
|
2
|
+
export interface ConnectionConfig {
|
|
3
|
+
apiUrl: string;
|
|
4
|
+
apiKey: string;
|
|
5
|
+
requestTimeout: number;
|
|
6
|
+
}
|
|
7
|
+
/** Resolves runtime config from explicit options, env vars, and defaults. */
|
|
8
|
+
export declare function resolveConfig(opts?: {
|
|
9
|
+
apiUrl?: string;
|
|
10
|
+
apiKey?: string;
|
|
11
|
+
requestTimeout?: number;
|
|
12
|
+
}): ConnectionConfig;
|
package/dist/config.js
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/** Resolves runtime config from explicit options, env vars, and defaults. */
|
|
2
|
+
export function resolveConfig(opts) {
|
|
3
|
+
return {
|
|
4
|
+
apiUrl: opts?.apiUrl ||
|
|
5
|
+
(typeof process !== "undefined"
|
|
6
|
+
? process.env.OMNIRUN_API_URL
|
|
7
|
+
: undefined) ||
|
|
8
|
+
"https://api.omnirun.io",
|
|
9
|
+
apiKey: opts?.apiKey ||
|
|
10
|
+
(typeof process !== "undefined"
|
|
11
|
+
? process.env.OMNIRUN_API_KEY
|
|
12
|
+
: undefined) ||
|
|
13
|
+
"",
|
|
14
|
+
requestTimeout: opts?.requestTimeout ?? 60_000,
|
|
15
|
+
};
|
|
16
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { HTTPClient } from "./client.js";
|
|
2
|
+
import type { ContextInfo, ExecutionResult, ResultItem } from "./models.js";
|
|
3
|
+
export interface ExecuteOptions {
|
|
4
|
+
onStdout?: (data: string) => void;
|
|
5
|
+
onStderr?: (data: string) => void;
|
|
6
|
+
onResult?: (result: ResultItem) => void;
|
|
7
|
+
}
|
|
8
|
+
/** Persistent interpreter context namespace for a single sandbox. */
|
|
9
|
+
export declare class Contexts {
|
|
10
|
+
private sandboxId;
|
|
11
|
+
private client;
|
|
12
|
+
constructor(sandboxId: string, client: HTTPClient);
|
|
13
|
+
private get baseUrl();
|
|
14
|
+
/** Create a new interpreter context for the given language. */
|
|
15
|
+
create(language: string, opts?: {
|
|
16
|
+
cwd?: string;
|
|
17
|
+
}): Promise<ContextInfo>;
|
|
18
|
+
/** List all interpreter contexts. */
|
|
19
|
+
list(): Promise<ContextInfo[]>;
|
|
20
|
+
/** Execute code in an existing interpreter context. */
|
|
21
|
+
execute(contextId: string, code: string, opts?: ExecuteOptions): Promise<ExecutionResult>;
|
|
22
|
+
/** Delete an interpreter context. */
|
|
23
|
+
delete(contextId: string): Promise<void>;
|
|
24
|
+
/** Restart an interpreter context. */
|
|
25
|
+
restart(contextId: string): Promise<ContextInfo>;
|
|
26
|
+
private streamExecute;
|
|
27
|
+
}
|