@quietrobot/sandboxes 0.0.0-stage → 0.2.1
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/README.md +139 -2
- package/dist/e2b.d.ts +238 -0
- package/dist/e2b.js +353 -0
- package/dist/index.d.ts +692 -0
- package/dist/index.js +639 -0
- package/dist/kernel.d.ts +1 -0
- package/dist/kernel.js +2 -0
- package/package.json +48 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/README.md
CHANGED
|
@@ -1,3 +1,140 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @quietrobot/sandboxes
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
TypeScript client for Quiet Robot Sandboxes: governed sandboxes for AI agents.
|
|
4
|
+
It needs Node.js 20 or newer (or any runtime with `fetch`) and has no
|
|
5
|
+
dependencies.
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
npm install @quietrobot/sandboxes
|
|
9
|
+
export QR_API_KEY=qr_live_... # create one at quietrobot.ai/app → API keys
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
```ts
|
|
13
|
+
import { Sandboxes, CommandDeniedError } from "@quietrobot/sandboxes";
|
|
14
|
+
|
|
15
|
+
const qr = new Sandboxes(); // reads QR_API_KEY, and QR_BASE_URL if set
|
|
16
|
+
|
|
17
|
+
const sbx = await qr.create({ egress: { mode: "allowlist", allow: ["pypi.org"] }, timeoutSec: 600 });
|
|
18
|
+
try {
|
|
19
|
+
await sbx.files.write("hello.py", "print('hello')"); // paths are relative to /home/sandbox
|
|
20
|
+
const run = await sbx.exec("python3 hello.py");
|
|
21
|
+
console.log(run.exitCode, run.stdout);
|
|
22
|
+
|
|
23
|
+
try {
|
|
24
|
+
await sbx.exec("curl https://example.com"); // waits if a policy rule holds it
|
|
25
|
+
} catch (e) {
|
|
26
|
+
if (e instanceof CommandDeniedError) console.log("denied:", e.message);
|
|
27
|
+
else throw e;
|
|
28
|
+
}
|
|
29
|
+
} finally {
|
|
30
|
+
await sbx.destroy();
|
|
31
|
+
}
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
## Commands
|
|
35
|
+
|
|
36
|
+
```ts
|
|
37
|
+
// Wait for it
|
|
38
|
+
const r = await sbx.exec("make test", { cwd: "work", env: { CI: "1" }, timeoutMs: 120_000 });
|
|
39
|
+
|
|
40
|
+
// Stream output while it runs (up to an hour)
|
|
41
|
+
await sbx.exec("npm install", { onStdout: (d) => process.stdout.write(d), onStderr: (d) => process.stderr.write(d), timeoutMs: 600_000 });
|
|
42
|
+
|
|
43
|
+
// Run in the background, read output later, write to stdin, stop it
|
|
44
|
+
const p = await sbx.spawn("python3 -i", { interactive: true });
|
|
45
|
+
await p.sendStdin("print(1 + 1)\n");
|
|
46
|
+
for await (const { stream, data } of p) { console.log(stream, data); break; }
|
|
47
|
+
await p.kill();
|
|
48
|
+
const done = await p.wait();
|
|
49
|
+
|
|
50
|
+
// An interactive shell on a pseudo-terminal
|
|
51
|
+
const t = await sbx.terminal({ cols: 120, rows: 40 });
|
|
52
|
+
await t.sendStdin("ls\n");
|
|
53
|
+
await t.resize(100, 30);
|
|
54
|
+
|
|
55
|
+
// Pick up a background command again later, by its id
|
|
56
|
+
const again = await sbx.process(p.execution.id);
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
When a policy rule holds a command for approval, the SDK waits for the
|
|
60
|
+
decision. Pass `waitForApproval: false` to return at once (the execution's
|
|
61
|
+
status is `pending_approval`), `approvalTimeoutMs` to cap the wait, or
|
|
62
|
+
`onPending` to be told when a command starts waiting.
|
|
63
|
+
|
|
64
|
+
## Code
|
|
65
|
+
|
|
66
|
+
A notebook-style Python kernel. Variables persist between calls, the value of
|
|
67
|
+
the last line comes back, and matplotlib figures come back as PNG. The code is
|
|
68
|
+
part of the command, so policy rules and the audit log see every cell.
|
|
69
|
+
|
|
70
|
+
```ts
|
|
71
|
+
await sbx.runCode("import pandas as pd\ndf = pd.DataFrame({'a': [1, 2, 3]})");
|
|
72
|
+
const cell = await sbx.runCode("df.a.sum()");
|
|
73
|
+
console.log(cell.results[0].text); // "6"
|
|
74
|
+
if (cell.error) console.log(cell.error.traceback);
|
|
75
|
+
await sbx.restartCode(); // clears variables
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
## Files, ports and state
|
|
79
|
+
|
|
80
|
+
```ts
|
|
81
|
+
await sbx.files.write("data/in.csv", csv);
|
|
82
|
+
const text = await sbx.files.readText("data/out.txt");
|
|
83
|
+
const { entries } = await sbx.files.list("data");
|
|
84
|
+
|
|
85
|
+
await sbx.spawn("python3 -m http.server 8000");
|
|
86
|
+
const port = await sbx.ports.expose(8000); // private: port.shareUrl signs a browser in
|
|
87
|
+
await sbx.ports.expose(3000, "public"); // anyone with the URL
|
|
88
|
+
console.log(sbx.host(8000)); // 8000-sbx-....quietrobot.dev
|
|
89
|
+
|
|
90
|
+
await sbx.pause(); // memory and processes kept, not billed
|
|
91
|
+
await sbx.resume();
|
|
92
|
+
const snap = await sbx.snapshot("with-deps");
|
|
93
|
+
const copies = await sbx.fork({ count: 3 }); // three copies of this machine, as it is now
|
|
94
|
+
const big = await sbx.resize({ cpus: 4, memoryMb: 8192 }); // running or paused: a copy at the new size, original destroyed
|
|
95
|
+
await sbx.rewind({ snapshotId: snap.id }); // files back as they were in the snapshot
|
|
96
|
+
const fresh = await qr.create({ snapshotId: snap.id });
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
## Everything else
|
|
100
|
+
|
|
101
|
+
- Sandboxes: `qr.get(id)` / `qr.connect(id)`, `qr.list(status)`, `sbx.refresh()`,
|
|
102
|
+
`sbx.setTimeout(sec)`, `sbx.setEgress({...})`, `sbx.metrics(hours)`,
|
|
103
|
+
`sbx.executions()`, `sbx.audit()`, `sbx.destroy()`.
|
|
104
|
+
- Create options: `template`, `region`, `cpus`, `memoryMb`, `timeoutSec`,
|
|
105
|
+
`cpuClass`, `autoPauseSec`, `egress`, `secrets`, `env`, `snapshotId`, `name`, `durable`,
|
|
106
|
+
`ifExists`, `volumes`.
|
|
107
|
+
- Account: `qr.snapshots()`, `qr.deleteSnapshot(id)`, `qr.reservations()`,
|
|
108
|
+
`qr.usage(days)`, `qr.audit()`, `qr.secrets`, `qr.volumes`, `qr.webhooks`.
|
|
109
|
+
- Templates: start from one of ours with `template: "code-interpreter"` (or
|
|
110
|
+
`browser`, `desktop`, `coding-agent`, `fullstack-js`, `go`, `rust`,
|
|
111
|
+
`postgres`), or build your own from a Dockerfile:
|
|
112
|
+
`await qr.createTemplate({ name: "my-agent", dockerfile })` waits for the
|
|
113
|
+
build; also `qr.templates()`, `qr.template(name)`, `qr.rebuildTemplate()`,
|
|
114
|
+
`qr.updateTemplate()`, `qr.deleteTemplate()`, `qr.templateBuild(id)`.
|
|
115
|
+
- Outbound learn mode: `sbx.learnEgress()`, run your workload,
|
|
116
|
+
`sbx.learnedEgress()`, then `sbx.applyLearnedEgress()` (allow only what it
|
|
117
|
+
reached) or `sbx.stopLearningEgress()`.
|
|
118
|
+
- After the fact: `sbx.report()` (or `sbx.reportPdf()`) says what the agent
|
|
119
|
+
did; `qr.undo(executionId)` puts the files back to just before a command;
|
|
120
|
+
`sbx.image()` lists the exact package versions; `sbx.setSpendCap(dollars)`.
|
|
121
|
+
- Webhooks: `await verifyWebhook(secret, headers, rawBody)` checks the
|
|
122
|
+
signature and returns the event, or throws.
|
|
123
|
+
|
|
124
|
+
Pass `{ apiKey, baseUrl, fetch }` to the constructor to set the key, the
|
|
125
|
+
service address or the `fetch` implementation in code instead.
|
|
126
|
+
|
|
127
|
+
Errors throw `SandboxesError` with `.status`, `.code` and `.message`. The full
|
|
128
|
+
API is described at `https://api.quietrobot.ai/api/v1/openapi.json`.
|
|
129
|
+
|
|
130
|
+
## E2B-compatible mode
|
|
131
|
+
|
|
132
|
+
`import { Sandbox } from "@quietrobot/sandboxes/e2b"` gives the E2B and E2B
|
|
133
|
+
Code Interpreter API on top of this client. See the
|
|
134
|
+
[repository README](https://github.com/Quiet-Robot/sandboxes-sdk#coming-from-e2b).
|
|
135
|
+
|
|
136
|
+
For a command-line tool, install the Python package (`pipx install
|
|
137
|
+
quietrobot-sandboxes`) or download a standalone `qr` binary from this
|
|
138
|
+
repository's releases.
|
|
139
|
+
|
|
140
|
+
Licensed under Apache 2.0.
|
package/dist/e2b.d.ts
ADDED
|
@@ -0,0 +1,238 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* E2B-compatible mode. Switch with one line:
|
|
3
|
+
*
|
|
4
|
+
* - import { Sandbox } from "e2b"; // or "@e2b/code-interpreter"
|
|
5
|
+
* + import { Sandbox } from "@quietrobot/sandboxes/e2b";
|
|
6
|
+
*
|
|
7
|
+
* The common calls keep their names and shapes: Sandbox.create, connect, kill,
|
|
8
|
+
* commands.run (foreground and background, onStdout/onStderr), files.read /
|
|
9
|
+
* write / list / remove / exists / makeDir, getHost, setTimeout, runCode,
|
|
10
|
+
* pause. Reads QR_API_KEY. Every command still goes through your
|
|
11
|
+
* organization's policy, so one a rule holds waits for a person's approval.
|
|
12
|
+
*
|
|
13
|
+
* Differences worth knowing:
|
|
14
|
+
* - Templates are ours (base, minimal) or a snapshot id; E2B template names
|
|
15
|
+
* fall back to base.
|
|
16
|
+
* - getHost(port) exposes the port as a public preview URL the first time
|
|
17
|
+
* you ask for it. Use exposePort(port, "private") for a private one.
|
|
18
|
+
* - allowInternetAccess maps to egress; left out, your policy's default applies.
|
|
19
|
+
*/
|
|
20
|
+
import { CommandDeniedError, type CodeResult, type CreateOptions, Process, SandboxesError, Sandbox as QrSandbox } from "./index.js";
|
|
21
|
+
export { CommandDeniedError, SandboxesError };
|
|
22
|
+
export interface ConnectionOpts {
|
|
23
|
+
apiKey?: string;
|
|
24
|
+
/** Our API address; defaults to $QR_BASE_URL or the hosted service. */
|
|
25
|
+
domain?: string;
|
|
26
|
+
requestTimeoutMs?: number;
|
|
27
|
+
}
|
|
28
|
+
export interface SandboxOpts extends ConnectionOpts {
|
|
29
|
+
template?: string;
|
|
30
|
+
metadata?: Record<string, string>;
|
|
31
|
+
envs?: Record<string, string>;
|
|
32
|
+
/** Sandbox lifetime. Default 5 minutes, like E2B. */
|
|
33
|
+
timeoutMs?: number;
|
|
34
|
+
cpuCount?: 1 | 2 | 4 | 8 | 16;
|
|
35
|
+
memoryMB?: number;
|
|
36
|
+
/** true: egress open. false: no network. Left out: your policy's default. */
|
|
37
|
+
allowInternetAccess?: boolean;
|
|
38
|
+
/** Anything else our create() takes: region, egress, secrets, durable, volumes... */
|
|
39
|
+
quietrobot?: CreateOptions;
|
|
40
|
+
}
|
|
41
|
+
export interface CommandResult {
|
|
42
|
+
exitCode: number;
|
|
43
|
+
error?: string;
|
|
44
|
+
stdout: string;
|
|
45
|
+
stderr: string;
|
|
46
|
+
}
|
|
47
|
+
export declare class CommandExitError extends SandboxesError implements CommandResult {
|
|
48
|
+
private result;
|
|
49
|
+
constructor(result: CommandResult);
|
|
50
|
+
get exitCode(): number;
|
|
51
|
+
get error(): string | undefined;
|
|
52
|
+
get stdout(): string;
|
|
53
|
+
get stderr(): string;
|
|
54
|
+
}
|
|
55
|
+
export declare class TimeoutError extends SandboxesError {
|
|
56
|
+
constructor(message: string);
|
|
57
|
+
}
|
|
58
|
+
export interface CommandStartOpts {
|
|
59
|
+
background?: boolean;
|
|
60
|
+
cwd?: string;
|
|
61
|
+
/** Ignored: commands run as the sandbox user. */
|
|
62
|
+
user?: string;
|
|
63
|
+
envs?: Record<string, string>;
|
|
64
|
+
onStdout?: (data: string) => void | Promise<void>;
|
|
65
|
+
onStderr?: (data: string) => void | Promise<void>;
|
|
66
|
+
/** Keep stdin open for sendStdin(). */
|
|
67
|
+
stdin?: boolean;
|
|
68
|
+
/** Default 60 s. 0 means no limit (background only). */
|
|
69
|
+
timeoutMs?: number;
|
|
70
|
+
}
|
|
71
|
+
/** A background command: what commands.run({ background: true }) returns. */
|
|
72
|
+
export declare class CommandHandle {
|
|
73
|
+
private proc;
|
|
74
|
+
private opts;
|
|
75
|
+
stdout: string;
|
|
76
|
+
stderr: string;
|
|
77
|
+
private result?;
|
|
78
|
+
constructor(proc: Process, opts: CommandStartOpts);
|
|
79
|
+
/** Our execution id. E2B calls it pid; pass it to commands.connect / kill. */
|
|
80
|
+
get pid(): string;
|
|
81
|
+
get exitCode(): number | undefined;
|
|
82
|
+
/** Waits for the end. Throws CommandExitError on a non-zero exit, like E2B. */
|
|
83
|
+
wait(): Promise<CommandResult>;
|
|
84
|
+
sendStdin(data: string): Promise<void>;
|
|
85
|
+
kill(): Promise<boolean>;
|
|
86
|
+
/** Stop reading output; the command keeps running. */
|
|
87
|
+
disconnect(): Promise<void>;
|
|
88
|
+
}
|
|
89
|
+
declare class Commands {
|
|
90
|
+
private sbx;
|
|
91
|
+
constructor(sbx: QrSandbox);
|
|
92
|
+
run(cmd: string, opts: CommandStartOpts & {
|
|
93
|
+
background: true;
|
|
94
|
+
}): Promise<CommandHandle>;
|
|
95
|
+
run(cmd: string, opts?: CommandStartOpts & {
|
|
96
|
+
background?: false;
|
|
97
|
+
}): Promise<CommandResult>;
|
|
98
|
+
run(cmd: string, opts?: CommandStartOpts): Promise<CommandHandle | CommandResult>;
|
|
99
|
+
/** Reattach to a background command by its pid (our execution id). */
|
|
100
|
+
connect(pid: string, opts?: Pick<CommandStartOpts, "onStdout" | "onStderr">): Promise<CommandHandle>;
|
|
101
|
+
kill(pid: string): Promise<boolean>;
|
|
102
|
+
sendStdin(pid: string, data: string): Promise<void>;
|
|
103
|
+
/** Commands this sandbox has run, newest first. */
|
|
104
|
+
list(): Promise<{
|
|
105
|
+
pid: string;
|
|
106
|
+
cmd: string;
|
|
107
|
+
status: string;
|
|
108
|
+
}[]>;
|
|
109
|
+
}
|
|
110
|
+
export interface EntryInfo {
|
|
111
|
+
name: string;
|
|
112
|
+
type: "file" | "dir";
|
|
113
|
+
path: string;
|
|
114
|
+
size: number;
|
|
115
|
+
modifiedTime?: Date;
|
|
116
|
+
}
|
|
117
|
+
declare class Filesystem {
|
|
118
|
+
private sbx;
|
|
119
|
+
constructor(sbx: QrSandbox);
|
|
120
|
+
read(path: string, opts?: {
|
|
121
|
+
format?: "text";
|
|
122
|
+
}): Promise<string>;
|
|
123
|
+
read(path: string, opts: {
|
|
124
|
+
format: "bytes";
|
|
125
|
+
}): Promise<Uint8Array>;
|
|
126
|
+
read(path: string, opts: {
|
|
127
|
+
format: "blob";
|
|
128
|
+
}): Promise<Blob>;
|
|
129
|
+
write(path: string, data: string | ArrayBuffer | Uint8Array | Blob): Promise<EntryInfo>;
|
|
130
|
+
write(files: {
|
|
131
|
+
path: string;
|
|
132
|
+
data: string | ArrayBuffer | Uint8Array | Blob;
|
|
133
|
+
}[]): Promise<EntryInfo[]>;
|
|
134
|
+
list(path: string): Promise<EntryInfo[]>;
|
|
135
|
+
exists(path: string): Promise<boolean>;
|
|
136
|
+
makeDir(path: string): Promise<boolean>;
|
|
137
|
+
remove(path: string): Promise<void>;
|
|
138
|
+
rename(oldPath: string, newPath: string): Promise<EntryInfo>;
|
|
139
|
+
}
|
|
140
|
+
/** runCode()'s answer, shaped like @e2b/code-interpreter's Execution. */
|
|
141
|
+
export declare class CodeExecution {
|
|
142
|
+
results: (CodeResult & {
|
|
143
|
+
isMainResult: boolean;
|
|
144
|
+
})[];
|
|
145
|
+
logs: {
|
|
146
|
+
stdout: string[];
|
|
147
|
+
stderr: string[];
|
|
148
|
+
};
|
|
149
|
+
error?: {
|
|
150
|
+
name: string;
|
|
151
|
+
value: string;
|
|
152
|
+
traceback: string;
|
|
153
|
+
} | undefined;
|
|
154
|
+
constructor(results: (CodeResult & {
|
|
155
|
+
isMainResult: boolean;
|
|
156
|
+
})[], logs: {
|
|
157
|
+
stdout: string[];
|
|
158
|
+
stderr: string[];
|
|
159
|
+
}, error?: {
|
|
160
|
+
name: string;
|
|
161
|
+
value: string;
|
|
162
|
+
traceback: string;
|
|
163
|
+
} | undefined);
|
|
164
|
+
/** The main result's text, if any. */
|
|
165
|
+
get text(): string | undefined;
|
|
166
|
+
}
|
|
167
|
+
export declare class Sandbox {
|
|
168
|
+
readonly commands: Commands;
|
|
169
|
+
readonly files: Filesystem;
|
|
170
|
+
/** For E2B code that reads sandbox.pty: our terminals are on sandbox.raw.terminal(). */
|
|
171
|
+
readonly raw: QrSandbox;
|
|
172
|
+
private exposed;
|
|
173
|
+
/** Use Sandbox.create() or Sandbox.connect(). */
|
|
174
|
+
constructor(sbx: QrSandbox);
|
|
175
|
+
get sandboxId(): string;
|
|
176
|
+
/** Our domain for preview URLs. */
|
|
177
|
+
get sandboxDomain(): string;
|
|
178
|
+
static create<S extends typeof Sandbox>(this: S, opts?: SandboxOpts): Promise<InstanceType<S>>;
|
|
179
|
+
static create<S extends typeof Sandbox>(this: S, template: string, opts?: SandboxOpts): Promise<InstanceType<S>>;
|
|
180
|
+
static connect<S extends typeof Sandbox>(this: S, sandboxId: string, opts?: ConnectionOpts): Promise<InstanceType<S>>;
|
|
181
|
+
static kill(sandboxId: string, opts?: ConnectionOpts): Promise<boolean>;
|
|
182
|
+
static setTimeout(sandboxId: string, timeoutMs: number, opts?: ConnectionOpts): Promise<void>;
|
|
183
|
+
static list(opts?: ConnectionOpts): Promise<{
|
|
184
|
+
sandboxId: string;
|
|
185
|
+
templateId: string;
|
|
186
|
+
name?: string;
|
|
187
|
+
metadata: Record<string, string>;
|
|
188
|
+
startedAt: Date;
|
|
189
|
+
endAt: Date;
|
|
190
|
+
state: string;
|
|
191
|
+
}[]>;
|
|
192
|
+
/**
|
|
193
|
+
* The preview host for a port, like E2B's: `${port}-${id}.quietrobot.dev`.
|
|
194
|
+
* The first call exposes the port publicly (it is live within a moment);
|
|
195
|
+
* await exposePort() to be sure, or to make it private.
|
|
196
|
+
*/
|
|
197
|
+
getHost(port: number): string;
|
|
198
|
+
/** Exposes a port and returns its full URL. Private URLs need the token or the share link. */
|
|
199
|
+
exposePort(port: number, access?: "public" | "private"): Promise<import("./index.js").PortInfo>;
|
|
200
|
+
setTimeout(timeoutMs: number): Promise<void>;
|
|
201
|
+
isRunning(): Promise<boolean>;
|
|
202
|
+
getInfo(): Promise<{
|
|
203
|
+
sandboxId: string;
|
|
204
|
+
templateId: string;
|
|
205
|
+
name: string | undefined;
|
|
206
|
+
metadata: Record<string, string>;
|
|
207
|
+
startedAt: Date;
|
|
208
|
+
endAt: Date;
|
|
209
|
+
state: "starting" | "running" | "pausing" | "paused" | "archived" | "stopped" | "error";
|
|
210
|
+
}>;
|
|
211
|
+
/** Pauses (memory, files and processes kept). Resume with Sandbox.connect(id). */
|
|
212
|
+
pause(): Promise<boolean>;
|
|
213
|
+
/** Same as pause(), under E2B's older name. */
|
|
214
|
+
betaPause(): Promise<boolean>;
|
|
215
|
+
kill(): Promise<boolean>;
|
|
216
|
+
/**
|
|
217
|
+
* Runs Python in a kernel that keeps variables between calls, like
|
|
218
|
+
* @e2b/code-interpreter. Only Python is supported.
|
|
219
|
+
*/
|
|
220
|
+
runCode(code: string, opts?: {
|
|
221
|
+
language?: string;
|
|
222
|
+
onStdout?: (o: {
|
|
223
|
+
line: string;
|
|
224
|
+
}) => void;
|
|
225
|
+
onStderr?: (o: {
|
|
226
|
+
line: string;
|
|
227
|
+
}) => void;
|
|
228
|
+
onResult?: (r: CodeResult) => void;
|
|
229
|
+
onError?: (e: {
|
|
230
|
+
name: string;
|
|
231
|
+
value: string;
|
|
232
|
+
traceback: string;
|
|
233
|
+
}) => void;
|
|
234
|
+
envs?: Record<string, string>;
|
|
235
|
+
timeoutMs?: number;
|
|
236
|
+
}): Promise<CodeExecution>;
|
|
237
|
+
}
|
|
238
|
+
export default Sandbox;
|