@finalai/sandbox-router-final 0.0.0-stage → 0.2.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 +202 -0
- package/NOTICE +4 -0
- package/README.md +38 -2
- package/dist/modules/frames.d.mts +18 -0
- package/dist/modules/frames.mjs +98 -0
- package/dist/modules/frames.mjs.map +7 -0
- package/dist/modules/images.d.mts +15 -0
- package/dist/modules/images.mjs +116 -0
- package/dist/modules/images.mjs.map +7 -0
- package/dist/modules/index.d.mts +4 -0
- package/dist/modules/index.mjs +16 -0
- package/dist/modules/index.mjs.map +7 -0
- package/dist/modules/limits.d.mts +6 -0
- package/dist/modules/limits.mjs +37 -0
- package/dist/modules/limits.mjs.map +7 -0
- package/dist/modules/options.d.mts +24 -0
- package/dist/modules/options.mjs +19 -0
- package/dist/modules/options.mjs.map +7 -0
- package/dist/modules/provider.d.mts +5 -0
- package/dist/modules/provider.mjs +647 -0
- package/dist/modules/provider.mjs.map +7 -0
- package/dist/modules/vm/api.d.mts +28 -0
- package/dist/modules/vm/api.mjs +94 -0
- package/dist/modules/vm/api.mjs.map +7 -0
- package/dist/modules/vm/client.d.mts +104 -0
- package/dist/modules/vm/client.mjs +186 -0
- package/dist/modules/vm/client.mjs.map +7 -0
- package/dist/modules/vm/clock.d.mts +5 -0
- package/dist/modules/vm/clock.mjs +11 -0
- package/dist/modules/vm/clock.mjs.map +7 -0
- package/dist/modules/vm/errors.d.mts +23 -0
- package/dist/modules/vm/errors.mjs +30 -0
- package/dist/modules/vm/errors.mjs.map +7 -0
- package/dist/modules/vm/exec.d.mts +47 -0
- package/dist/modules/vm/exec.mjs +292 -0
- package/dist/modules/vm/exec.mjs.map +7 -0
- package/dist/modules/vm/files.d.mts +18 -0
- package/dist/modules/vm/files.mjs +160 -0
- package/dist/modules/vm/files.mjs.map +7 -0
- package/dist/modules/vm/grants.d.mts +35 -0
- package/dist/modules/vm/grants.mjs +106 -0
- package/dist/modules/vm/grants.mjs.map +7 -0
- package/dist/modules/vm/ids.d.mts +3 -0
- package/dist/modules/vm/ids.mjs +35 -0
- package/dist/modules/vm/ids.mjs.map +7 -0
- package/dist/modules/vm/ingress.d.mts +99 -0
- package/dist/modules/vm/ingress.mjs +263 -0
- package/dist/modules/vm/ingress.mjs.map +7 -0
- package/dist/modules/vm/machines.d.mts +79 -0
- package/dist/modules/vm/machines.mjs +60 -0
- package/dist/modules/vm/machines.mjs.map +7 -0
- package/dist/modules/vm/network.d.mts +17 -0
- package/dist/modules/vm/network.mjs +44 -0
- package/dist/modules/vm/network.mjs.map +7 -0
- package/dist/modules/vm/sandbox.d.mts +75 -0
- package/dist/modules/vm/sandbox.mjs +175 -0
- package/dist/modules/vm/sandbox.mjs.map +7 -0
- package/dist/modules/vm/schemas.d.mts +1209 -0
- package/dist/modules/vm/schemas.mjs +289 -0
- package/dist/modules/vm/schemas.mjs.map +7 -0
- package/dist/modules/vm/wire.d.mts +3 -0
- package/dist/modules/vm/wire.mjs +9 -0
- package/dist/modules/vm/wire.mjs.map +7 -0
- package/dist/modules/vm/workload.d.mts +14 -0
- package/dist/modules/vm/workload.mjs +11 -0
- package/dist/modules/vm/workload.mjs.map +7 -0
- package/package.json +38 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
package/NOTICE
ADDED
package/README.md
CHANGED
|
@@ -1,3 +1,39 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @finalai/sandbox-router-final
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
[Final](https://final.ai) VMs as a provider for `@finalai/sandbox-router`.
|
|
4
|
+
|
|
5
|
+
```sh
|
|
6
|
+
npm install @finalai/sandbox-router @finalai/sandbox-router-final
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
```ts
|
|
10
|
+
import { createSandboxRouter } from '@finalai/sandbox-router';
|
|
11
|
+
import { final } from '@finalai/sandbox-router-final';
|
|
12
|
+
|
|
13
|
+
const router = createSandboxRouter({ providers: [final({ apiKey: process.env.FINAL_API_KEY! })] });
|
|
14
|
+
const sandbox = await router.sandboxes.create({ cpu: 2, memoryMib: 4096 });
|
|
15
|
+
console.log((await sandbox.run('uname -sm')).stdout);
|
|
16
|
+
await sandbox.delete();
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## `final(credentials, options?)`
|
|
20
|
+
|
|
21
|
+
- `credentials.apiKey`: a Final API key with edit access to the project.
|
|
22
|
+
- `credentials.apiUrl`: the API's base URL (default: the public API).
|
|
23
|
+
- `options.fetch`: a `fetch` implementation.
|
|
24
|
+
- `options.previewDomain`: the domain port URLs are served under, for an API other than the
|
|
25
|
+
public one.
|
|
26
|
+
|
|
27
|
+
`providerOptions.final` (`finalOptionsSchema`): `projectId`; `cpuLimit` and `memoryLimitMib`
|
|
28
|
+
(burst above the spec's size; without them the size is fixed); `machine` (`types`,
|
|
29
|
+
`cpuFamilies`, `sort`, `maxPricePerHour`); `publicPorts` (published at create); `tools`
|
|
30
|
+
(`docker`).
|
|
31
|
+
|
|
32
|
+
The provider's `prices` reads the API's machine types for the spec's size.
|
|
33
|
+
|
|
34
|
+
The package also exports `capabilities`, `regions`, `limits` and `MachineSort`.
|
|
35
|
+
|
|
36
|
+
## Capabilities
|
|
37
|
+
|
|
38
|
+
`labels`, `pause`, `resume`, `stop`, `snapshot`, `fork`, `pty`, `image`, `networkPolicy`,
|
|
39
|
+
`publicPorts`, `directConnect`, `idleTimeout` and `maxLifetime`.
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { ExitReason, ProcessFrameKind, type ProcessFrameIface, type SandboxRouterErrorCode } from '@finalai/sandbox-router/provider';
|
|
2
|
+
export type OutputKind = ProcessFrameKind.Stdout | ProcessFrameKind.Stderr | ProcessFrameKind.Data;
|
|
3
|
+
export interface SeqFramesIface {
|
|
4
|
+
started: (processId: string, pid: number | null) => void;
|
|
5
|
+
output: (kind: OutputKind, seq: number, bytes: Uint8Array) => void;
|
|
6
|
+
exit: (code: number | null, reason: ExitReason, signal: string | null) => void;
|
|
7
|
+
error: (code: SandboxRouterErrorCode, message: string) => void;
|
|
8
|
+
stop: () => void;
|
|
9
|
+
ended: () => boolean;
|
|
10
|
+
frames: AsyncIterable<ProcessFrameIface>;
|
|
11
|
+
}
|
|
12
|
+
export interface SeqFramesOptionsIface {
|
|
13
|
+
afterSeq: number;
|
|
14
|
+
awaitStarted: boolean;
|
|
15
|
+
maxOutputBytes?: number;
|
|
16
|
+
onOutputCap?: () => void;
|
|
17
|
+
}
|
|
18
|
+
export declare const createSeqFrames: (options: SeqFramesOptionsIface) => SeqFramesIface;
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import {
|
|
2
|
+
ExitReason,
|
|
3
|
+
ProcessFrameKind
|
|
4
|
+
} from "@finalai/sandbox-router/provider";
|
|
5
|
+
const createSeqFrames = (options) => {
|
|
6
|
+
let last = options.afterSeq;
|
|
7
|
+
let done = false;
|
|
8
|
+
let stopped = false;
|
|
9
|
+
let started = !options.awaitStarted;
|
|
10
|
+
let output = 0;
|
|
11
|
+
const held = [];
|
|
12
|
+
const buffered = [];
|
|
13
|
+
let wake = null;
|
|
14
|
+
const push = (frame) => {
|
|
15
|
+
if (done) return;
|
|
16
|
+
buffered.push(frame);
|
|
17
|
+
last = frame.seq;
|
|
18
|
+
if (frame.kind === ProcessFrameKind.Exit || frame.kind === ProcessFrameKind.Error) done = true;
|
|
19
|
+
wake?.();
|
|
20
|
+
wake = null;
|
|
21
|
+
};
|
|
22
|
+
const terminal = (frame) => {
|
|
23
|
+
if (!started) {
|
|
24
|
+
held.push(() => push(frame(last + 1)));
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
push(frame(last + 1));
|
|
28
|
+
};
|
|
29
|
+
const emit = (kind, seq, bytes) => {
|
|
30
|
+
if (done || seq <= last || bytes.byteLength === 0) return;
|
|
31
|
+
if (seq > last + 1)
|
|
32
|
+
push({ kind: ProcessFrameKind.Gap, seq: seq - 1, from: last + 1, to: seq - 1 });
|
|
33
|
+
const cap = options.maxOutputBytes;
|
|
34
|
+
if (cap != null && output + bytes.byteLength > cap) {
|
|
35
|
+
const room = cap - output;
|
|
36
|
+
output = cap;
|
|
37
|
+
if (room > 0) push({ kind, seq, bytes: bytes.subarray(0, room) });
|
|
38
|
+
push({
|
|
39
|
+
kind: ProcessFrameKind.Exit,
|
|
40
|
+
seq: last + 1,
|
|
41
|
+
code: null,
|
|
42
|
+
signal: null,
|
|
43
|
+
reason: ExitReason.OutputCap
|
|
44
|
+
});
|
|
45
|
+
options.onOutputCap?.();
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
output += bytes.byteLength;
|
|
49
|
+
push({ kind, seq, bytes });
|
|
50
|
+
};
|
|
51
|
+
return {
|
|
52
|
+
started: (processId, pid) => {
|
|
53
|
+
if (started) return;
|
|
54
|
+
started = true;
|
|
55
|
+
push({
|
|
56
|
+
kind: ProcessFrameKind.Started,
|
|
57
|
+
seq: 0,
|
|
58
|
+
processId,
|
|
59
|
+
pid,
|
|
60
|
+
startedAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
61
|
+
});
|
|
62
|
+
for (const release of held.splice(0)) release();
|
|
63
|
+
},
|
|
64
|
+
output: (kind, seq, bytes) => {
|
|
65
|
+
if (!started) held.push(() => emit(kind, seq, bytes));
|
|
66
|
+
else emit(kind, seq, bytes);
|
|
67
|
+
},
|
|
68
|
+
exit: (code, reason, signal) => terminal((seq) => ({ kind: ProcessFrameKind.Exit, seq, code, signal, reason })),
|
|
69
|
+
error: (code, message) => terminal((seq) => ({ kind: ProcessFrameKind.Error, seq, code, message })),
|
|
70
|
+
stop: () => {
|
|
71
|
+
stopped = true;
|
|
72
|
+
wake?.();
|
|
73
|
+
wake = null;
|
|
74
|
+
},
|
|
75
|
+
ended: () => done,
|
|
76
|
+
frames: {
|
|
77
|
+
[Symbol.asyncIterator]: async function* () {
|
|
78
|
+
for (; ; ) {
|
|
79
|
+
const frame = buffered.shift();
|
|
80
|
+
if (frame != null) {
|
|
81
|
+
yield frame;
|
|
82
|
+
if (frame.kind === ProcessFrameKind.Exit || frame.kind === ProcessFrameKind.Error)
|
|
83
|
+
return;
|
|
84
|
+
continue;
|
|
85
|
+
}
|
|
86
|
+
if (stopped) return;
|
|
87
|
+
await new Promise((resolve) => {
|
|
88
|
+
wake = resolve;
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
};
|
|
94
|
+
};
|
|
95
|
+
export {
|
|
96
|
+
createSeqFrames
|
|
97
|
+
};
|
|
98
|
+
//# sourceMappingURL=frames.mjs.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../../../src/frames.ts"],
|
|
4
|
+
"sourcesContent": ["import {\n ExitReason,\n ProcessFrameKind,\n type ProcessFrameIface,\n type SandboxRouterErrorCode,\n} from '@finalai/sandbox-router/provider';\n\n// Process frames numbered by the VM's own output sequence. Final VMs number a process's output\n// chunks from 1 across stdout and stderr and keep them, so a frame's `seq` here is that number:\n// `started` is 0, each output chunk carries its own seq, and the terminal frame follows the last\n// one. Reattaching after `seq` n asks the VM for what followed n, and the numbers line up across\n// clients without any state kept here. A jump in the VM's numbering (output it no longer keeps)\n// becomes one `gap` frame; repeats are dropped.\n\nexport type OutputKind = ProcessFrameKind.Stdout | ProcessFrameKind.Stderr | ProcessFrameKind.Data;\n\nexport interface SeqFramesIface {\n started: (processId: string, pid: number | null) => void;\n output: (kind: OutputKind, seq: number, bytes: Uint8Array) => void;\n exit: (code: number | null, reason: ExitReason, signal: string | null) => void;\n error: (code: SandboxRouterErrorCode, message: string) => void;\n // Ends the stream without a terminal frame (the caller detached).\n stop: () => void;\n ended: () => boolean;\n frames: AsyncIterable<ProcessFrameIface>;\n}\n\nexport interface SeqFramesOptionsIface {\n // The last seq the consumer has: -1 for a new process, `afterSeq` on reattach.\n afterSeq: number;\n // A new process: output is held until `started`, which must come first.\n awaitStarted: boolean;\n maxOutputBytes?: number;\n onOutputCap?: () => void;\n}\n\nexport const createSeqFrames = (options: SeqFramesOptionsIface): SeqFramesIface => {\n let last = options.afterSeq;\n let done = false;\n let stopped = false;\n let started = !options.awaitStarted;\n let output = 0;\n const held: (() => void)[] = [];\n const buffered: ProcessFrameIface[] = [];\n let wake: (() => void) | null = null;\n const push = (frame: ProcessFrameIface) => {\n if (done) return;\n buffered.push(frame);\n last = frame.seq;\n if (frame.kind === ProcessFrameKind.Exit || frame.kind === ProcessFrameKind.Error) done = true;\n wake?.();\n wake = null;\n };\n const terminal = (frame: (seq: number) => ProcessFrameIface) => {\n if (!started) {\n held.push(() => push(frame(last + 1)));\n return;\n }\n push(frame(last + 1));\n };\n const emit = (kind: OutputKind, seq: number, bytes: Uint8Array) => {\n if (done || seq <= last || bytes.byteLength === 0) return;\n if (seq > last + 1)\n push({ kind: ProcessFrameKind.Gap, seq: seq - 1, from: last + 1, to: seq - 1 });\n const cap = options.maxOutputBytes;\n if (cap != null && output + bytes.byteLength > cap) {\n const room = cap - output;\n output = cap;\n if (room > 0) push({ kind, seq, bytes: bytes.subarray(0, room) });\n push({\n kind: ProcessFrameKind.Exit,\n seq: last + 1,\n code: null,\n signal: null,\n reason: ExitReason.OutputCap,\n });\n options.onOutputCap?.();\n return;\n }\n output += bytes.byteLength;\n push({ kind, seq, bytes });\n };\n return {\n started: (processId, pid) => {\n if (started) return;\n started = true;\n push({\n kind: ProcessFrameKind.Started,\n seq: 0,\n processId,\n pid,\n startedAt: new Date().toISOString(),\n });\n for (const release of held.splice(0)) release();\n },\n output: (kind, seq, bytes) => {\n if (!started) held.push(() => emit(kind, seq, bytes));\n else emit(kind, seq, bytes);\n },\n exit: (code, reason, signal) =>\n terminal((seq) => ({ kind: ProcessFrameKind.Exit, seq, code, signal, reason })),\n error: (code, message) =>\n terminal((seq) => ({ kind: ProcessFrameKind.Error, seq, code, message })),\n stop: () => {\n stopped = true;\n wake?.();\n wake = null;\n },\n ended: () => done,\n frames: {\n [Symbol.asyncIterator]: async function* () {\n for (;;) {\n const frame = buffered.shift();\n if (frame != null) {\n yield frame;\n if (frame.kind === ProcessFrameKind.Exit || frame.kind === ProcessFrameKind.Error)\n return;\n continue;\n }\n if (stopped) return;\n await new Promise<void>((resolve) => {\n wake = resolve;\n });\n }\n },\n },\n };\n};\n"],
|
|
5
|
+
"mappings": "AAAA;AAAA,EACE;AAAA,EACA;AAAA,OAGK;AA+BA,MAAM,kBAAkB,CAAC,YAAmD;AACjF,MAAI,OAAO,QAAQ;AACnB,MAAI,OAAO;AACX,MAAI,UAAU;AACd,MAAI,UAAU,CAAC,QAAQ;AACvB,MAAI,SAAS;AACb,QAAM,OAAuB,CAAC;AAC9B,QAAM,WAAgC,CAAC;AACvC,MAAI,OAA4B;AAChC,QAAM,OAAO,CAAC,UAA6B;AACzC,QAAI,KAAM;AACV,aAAS,KAAK,KAAK;AACnB,WAAO,MAAM;AACb,QAAI,MAAM,SAAS,iBAAiB,QAAQ,MAAM,SAAS,iBAAiB,MAAO,QAAO;AAC1F,WAAO;AACP,WAAO;AAAA,EACT;AACA,QAAM,WAAW,CAAC,UAA8C;AAC9D,QAAI,CAAC,SAAS;AACZ,WAAK,KAAK,MAAM,KAAK,MAAM,OAAO,CAAC,CAAC,CAAC;AACrC;AAAA,IACF;AACA,SAAK,MAAM,OAAO,CAAC,CAAC;AAAA,EACtB;AACA,QAAM,OAAO,CAAC,MAAkB,KAAa,UAAsB;AACjE,QAAI,QAAQ,OAAO,QAAQ,MAAM,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QAAQ,EAAE;AAAA,IAC1E,MAAM,MAAM;AACV,gBAAU;AACV,aAAO;AACP,aAAO;AAAA,IACT;AAAA,IACA,OAAO,MAAM;AAAA,IACb,QAAQ;AAAA,MACN,CAAC,OAAO,aAAa,GAAG,mBAAmB;AACzC,mBAAS;AACP,gBAAM,QAAQ,SAAS,MAAM;AAC7B,cAAI,SAAS,MAAM;AACjB,kBAAM;AACN,gBAAI,MAAM,SAAS,iBAAiB,QAAQ,MAAM,SAAS,iBAAiB;AAC1E;AACF;AAAA,UACF;AACA,cAAI,QAAS;AACb,gBAAM,IAAI,QAAc,CAAC,YAAY;AACnC,mBAAO;AAAA,UACT,CAAC;AAAA,QACH;AAAA,MACF;AAAA,IACF;AAAA,EACF;AACF;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { ImageStrategy, type ImageBaseIface, type ImageSpecIface, type ResultIface, type VendorArtifactIface } from '@finalai/sandbox-router/provider';
|
|
2
|
+
import type { VmClientIface } from "./vm/client.mjs";
|
|
3
|
+
import type { ClockIface } from "./vm/clock.mjs";
|
|
4
|
+
export declare const baseReference: (base: ImageBaseIface) => string | null;
|
|
5
|
+
export declare const imageStrategy: (spec: ImageSpecIface) => ImageStrategy | null;
|
|
6
|
+
interface ImageContextIface {
|
|
7
|
+
client: VmClientIface;
|
|
8
|
+
clock: ClockIface;
|
|
9
|
+
signal?: AbortSignal;
|
|
10
|
+
}
|
|
11
|
+
export declare const ensureImage: (context: ImageContextIface, reference: string | null) => Promise<ResultIface<string>>;
|
|
12
|
+
export declare const buildImage: (context: ImageContextIface & {
|
|
13
|
+
onLog?: (line: string) => void;
|
|
14
|
+
}, spec: ImageSpecIface) => Promise<ResultIface<VendorArtifactIface>>;
|
|
15
|
+
export {};
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import {
|
|
2
|
+
DEFAULT_CPU,
|
|
3
|
+
DEFAULT_MEMORY_MIB,
|
|
4
|
+
fail,
|
|
5
|
+
ImagePreset,
|
|
6
|
+
ImageStrategy,
|
|
7
|
+
imageKey,
|
|
8
|
+
ok,
|
|
9
|
+
readyCommand,
|
|
10
|
+
SandboxRouterErrorCode,
|
|
11
|
+
shellQuote
|
|
12
|
+
} from "@finalai/sandbox-router/provider";
|
|
13
|
+
import { ImageState, TemplateState } from "./vm/schemas.mjs";
|
|
14
|
+
const BUILD_TIMEOUT_MS = 20 * 6e4;
|
|
15
|
+
const POLL_MS = 2e3;
|
|
16
|
+
const DEFAULT_UBUNTU = "24.04";
|
|
17
|
+
const baseReference = (base) => {
|
|
18
|
+
if (base.kind === "default") return null;
|
|
19
|
+
if (base.kind === "oci") return base.reference;
|
|
20
|
+
switch (base.name) {
|
|
21
|
+
case ImagePreset.Ubuntu:
|
|
22
|
+
return base.version === DEFAULT_UBUNTU ? null : `docker.io/library/ubuntu:${base.version}`;
|
|
23
|
+
case ImagePreset.Node:
|
|
24
|
+
return `docker.io/library/node:${base.version}`;
|
|
25
|
+
case ImagePreset.Python:
|
|
26
|
+
return `docker.io/library/python:${base.version}`;
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
const imageStrategy = (spec) => {
|
|
30
|
+
if (spec.base.kind === "oci" && spec.base.registryCredential != null) return null;
|
|
31
|
+
if (spec.steps.length > 0) return ImageStrategy.Bake;
|
|
32
|
+
if (baseReference(spec.base) == null && spec.start == null) return ImageStrategy.Runtime;
|
|
33
|
+
return ImageStrategy.Build;
|
|
34
|
+
};
|
|
35
|
+
const deadlineError = (what) => fail(
|
|
36
|
+
SandboxRouterErrorCode.Timeout,
|
|
37
|
+
`${what} took longer than ${BUILD_TIMEOUT_MS / 6e4} min`,
|
|
38
|
+
{
|
|
39
|
+
retryable: true
|
|
40
|
+
}
|
|
41
|
+
);
|
|
42
|
+
const waitForImage = async (context, image) => {
|
|
43
|
+
const deadline = context.clock.now() + BUILD_TIMEOUT_MS;
|
|
44
|
+
let current = image;
|
|
45
|
+
for (; ; ) {
|
|
46
|
+
if (current.state === ImageState.Ready) return ok(current.id);
|
|
47
|
+
if (current.state === ImageState.Failed)
|
|
48
|
+
return fail(
|
|
49
|
+
SandboxRouterErrorCode.InvalidRequest,
|
|
50
|
+
`importing ${current.reference} failed: ${current.errorCode ?? "no code"}`,
|
|
51
|
+
{ nativeError: current.errorClass ?? current.errorCode }
|
|
52
|
+
);
|
|
53
|
+
if (context.clock.now() >= deadline) return deadlineError(`importing ${current.reference}`);
|
|
54
|
+
await context.clock.sleep(POLL_MS, context.signal);
|
|
55
|
+
current = await context.client.images.get(current.id);
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
const ensureImage = async (context, reference) => {
|
|
59
|
+
const images = await context.client.images.list();
|
|
60
|
+
if (reference == null) {
|
|
61
|
+
const platform = images.find((image) => image.platform && image.lineage === "base");
|
|
62
|
+
return platform == null ? fail(SandboxRouterErrorCode.ProviderError, "the platform has no default image") : ok(platform.id);
|
|
63
|
+
}
|
|
64
|
+
const existing = images.filter(
|
|
65
|
+
(image) => !image.platform && image.reference === reference && image.state !== ImageState.Failed
|
|
66
|
+
).sort((a, b) => b.createdAt.getTime() - a.createdAt.getTime())[0];
|
|
67
|
+
return waitForImage(context, existing ?? await context.client.images.create({ reference }));
|
|
68
|
+
};
|
|
69
|
+
const templateStart = (spec, command) => {
|
|
70
|
+
const exports = Object.entries(spec.env).map(
|
|
71
|
+
([name, value]) => `[ -n "\${${name}+x}" ] || ${name}=${shellQuote(value)}; export ${name}; `
|
|
72
|
+
);
|
|
73
|
+
const cd = spec.workdir == null ? "" : `mkdir -p ${shellQuote(spec.workdir)} && cd ${shellQuote(spec.workdir)} && `;
|
|
74
|
+
return `${exports.join("")}${cd}exec sh -c ${shellQuote(command)}`;
|
|
75
|
+
};
|
|
76
|
+
const templateName = async (spec) => `router-${(await imageKey(spec)).slice(4)}`;
|
|
77
|
+
const buildImage = async (context, spec) => {
|
|
78
|
+
const image = await ensureImage(context, baseReference(spec.base));
|
|
79
|
+
if (!image.ok) return image;
|
|
80
|
+
const exportable = Object.keys(spec.env).every((name2) => /^[A-Za-z_][A-Za-z0-9_]*$/.test(name2));
|
|
81
|
+
if (spec.start == null || spec.user != null || !exportable)
|
|
82
|
+
return ok({ kind: "image", id: image.value, startsOnBoot: false });
|
|
83
|
+
const name = await templateName(spec);
|
|
84
|
+
const existing = (await context.client.templates.list()).find(
|
|
85
|
+
(template2) => template2.name === name && template2.state !== TemplateState.Failed
|
|
86
|
+
);
|
|
87
|
+
let template = existing ?? await context.client.templates.create({
|
|
88
|
+
name,
|
|
89
|
+
imageId: image.value,
|
|
90
|
+
startCmd: templateStart(spec, spec.start.command),
|
|
91
|
+
readyCmd: spec.start.ready == null ? void 0 : readyCommand(spec.start.ready, 0.5),
|
|
92
|
+
cpu: [DEFAULT_CPU, DEFAULT_CPU],
|
|
93
|
+
memory: [DEFAULT_MEMORY_MIB, DEFAULT_MEMORY_MIB]
|
|
94
|
+
});
|
|
95
|
+
if (existing == null) {
|
|
96
|
+
context.onLog?.(`building template ${template.id}`);
|
|
97
|
+
await context.client.templates.build(template.id);
|
|
98
|
+
}
|
|
99
|
+
const deadline = context.clock.now() + BUILD_TIMEOUT_MS;
|
|
100
|
+
for (; ; ) {
|
|
101
|
+
if (template.state === TemplateState.Ready)
|
|
102
|
+
return ok({ kind: "template", id: template.id, startsOnBoot: true });
|
|
103
|
+
if (template.state === TemplateState.Failed)
|
|
104
|
+
return fail(SandboxRouterErrorCode.InvalidRequest, `template ${template.id} failed to build`);
|
|
105
|
+
if (context.clock.now() >= deadline) return deadlineError(`building template ${template.id}`);
|
|
106
|
+
await context.clock.sleep(POLL_MS, context.signal);
|
|
107
|
+
template = await context.client.templates.get(template.id);
|
|
108
|
+
}
|
|
109
|
+
};
|
|
110
|
+
export {
|
|
111
|
+
baseReference,
|
|
112
|
+
buildImage,
|
|
113
|
+
ensureImage,
|
|
114
|
+
imageStrategy
|
|
115
|
+
};
|
|
116
|
+
//# sourceMappingURL=images.mjs.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../../../src/images.ts"],
|
|
4
|
+
"sourcesContent": ["import {\n DEFAULT_CPU,\n DEFAULT_MEMORY_MIB,\n fail,\n ImagePreset,\n ImageStrategy,\n imageKey,\n ok,\n readyCommand,\n SandboxRouterErrorCode,\n shellQuote,\n type ImageBaseIface,\n type ImageSpecIface,\n type ResultIface,\n type VendorArtifactIface,\n} from '@finalai/sandbox-router/provider';\nimport type { VmClientIface } from \"./vm/client.mjs\";\nimport type { ClockIface } from \"./vm/clock.mjs\";\nimport { ImageState, TemplateState, type ImageIface } from \"./vm/schemas.mjs\";\n\n// How specs become Final VMs images: a base is an imported container image (or the platform's\n// default image); a spec with a start command and no steps becomes a template, which boots the\n// image once, runs the start command until the ready check passes and saves the running VM.\n// Specs with steps are baked by the router (create, run the steps, snapshot).\n\nconst BUILD_TIMEOUT_MS = 20 * 60_000;\nconst POLL_MS = 2_000;\n\n// The platform's default image is Ubuntu 24.04 with the router's baseline tools (and more).\nconst DEFAULT_UBUNTU = '24.04';\n\n// The container image a base names; null for the platform's default image.\nexport const baseReference = (base: ImageBaseIface): string | null => {\n if (base.kind === 'default') return null;\n if (base.kind === 'oci') return base.reference;\n switch (base.name) {\n case ImagePreset.Ubuntu:\n return base.version === DEFAULT_UBUNTU ? null : `docker.io/library/ubuntu:${base.version}`;\n case ImagePreset.Node:\n return `docker.io/library/node:${base.version}`;\n case ImagePreset.Python:\n return `docker.io/library/python:${base.version}`;\n }\n};\n\nexport const imageStrategy = (spec: ImageSpecIface): ImageStrategy | null => {\n // A registry credential is the name of a secret this adapter cannot read.\n if (spec.base.kind === 'oci' && spec.base.registryCredential != null) return null;\n if (spec.steps.length > 0) return ImageStrategy.Bake;\n if (baseReference(spec.base) == null && spec.start == null) return ImageStrategy.Runtime;\n return ImageStrategy.Build;\n};\n\ninterface ImageContextIface {\n client: VmClientIface;\n clock: ClockIface;\n signal?: AbortSignal;\n}\n\nconst deadlineError = (what: string) =>\n fail(\n SandboxRouterErrorCode.Timeout,\n `${what} took longer than ${BUILD_TIMEOUT_MS / 60_000} min`,\n {\n retryable: true,\n },\n );\n\nconst waitForImage = async (\n context: ImageContextIface,\n image: ImageIface,\n): Promise<ResultIface<string>> => {\n const deadline = context.clock.now() + BUILD_TIMEOUT_MS;\n let current = image;\n for (;;) {\n if (current.state === ImageState.Ready) return ok(current.id);\n if (current.state === ImageState.Failed)\n return fail(\n SandboxRouterErrorCode.InvalidRequest,\n `importing ${current.reference} failed: ${current.errorCode ?? 'no code'}`,\n { nativeError: current.errorClass ?? current.errorCode },\n );\n if (context.clock.now() >= deadline) return deadlineError(`importing ${current.reference}`);\n await context.clock.sleep(POLL_MS, context.signal);\n current = await context.client.images.get(current.id);\n }\n};\n\n// An imported image of `reference`, reusing one the project already has.\nexport const ensureImage = async (\n context: ImageContextIface,\n reference: string | null,\n): Promise<ResultIface<string>> => {\n const images = await context.client.images.list();\n if (reference == null) {\n const platform = images.find((image) => image.platform && image.lineage === 'base');\n return platform == null\n ? fail(SandboxRouterErrorCode.ProviderError, 'the platform has no default image')\n : ok(platform.id);\n }\n const existing = images\n .filter(\n (image) =>\n !image.platform && image.reference === reference && image.state !== ImageState.Failed,\n )\n .sort((a, b) => b.createdAt.getTime() - a.createdAt.getTime())[0];\n return waitForImage(context, existing ?? (await context.client.images.create({ reference })));\n};\n\n// The template's start command carries the spec's environment and working directory: a\n// template starts its service once, before any sandbox's own environment exists. Variables\n// already set (a create's own environment) keep their values.\nconst templateStart = (spec: ImageSpecIface, command: string): string => {\n const exports = Object.entries(spec.env).map(\n ([name, value]) => `[ -n \"\\${${name}+x}\" ] || ${name}=${shellQuote(value)}; export ${name}; `,\n );\n const cd =\n spec.workdir == null\n ? ''\n : `mkdir -p ${shellQuote(spec.workdir)} && cd ${shellQuote(spec.workdir)} && `;\n return `${exports.join('')}${cd}exec sh -c ${shellQuote(command)}`;\n};\n\nconst templateName = async (spec: ImageSpecIface) => `router-${(await imageKey(spec)).slice(4)}`;\n\nexport const buildImage = async (\n context: ImageContextIface & { onLog?: (line: string) => void },\n spec: ImageSpecIface,\n): Promise<ResultIface<VendorArtifactIface>> => {\n const image = await ensureImage(context, baseReference(spec.base));\n if (!image.ok) return image;\n // A start command run as another user, or with variables a shell cannot export, is left to\n // the router at create.\n const exportable = Object.keys(spec.env).every((name) => /^[A-Za-z_][A-Za-z0-9_]*$/.test(name));\n if (spec.start == null || spec.user != null || !exportable)\n return ok({ kind: 'image', id: image.value, startsOnBoot: false });\n const name = await templateName(spec);\n const existing = (await context.client.templates.list()).find(\n (template) => template.name === name && template.state !== TemplateState.Failed,\n );\n let template =\n existing ??\n (await context.client.templates.create({\n name,\n imageId: image.value,\n startCmd: templateStart(spec, spec.start.command),\n readyCmd: spec.start.ready == null ? undefined : readyCommand(spec.start.ready, 0.5),\n cpu: [DEFAULT_CPU, DEFAULT_CPU],\n memory: [DEFAULT_MEMORY_MIB, DEFAULT_MEMORY_MIB],\n }));\n if (existing == null) {\n context.onLog?.(`building template ${template.id}`);\n await context.client.templates.build(template.id);\n }\n const deadline = context.clock.now() + BUILD_TIMEOUT_MS;\n for (;;) {\n if (template.state === TemplateState.Ready)\n return ok({ kind: 'template', id: template.id, startsOnBoot: true });\n if (template.state === TemplateState.Failed)\n return fail(SandboxRouterErrorCode.InvalidRequest, `template ${template.id} failed to build`);\n if (context.clock.now() >= deadline) return deadlineError(`building template ${template.id}`);\n await context.clock.sleep(POLL_MS, context.signal);\n template = await context.client.templates.get(template.id);\n }\n};\n"],
|
|
5
|
+
"mappings": "AAAA;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OAKK;AAGP,SAAS,YAAY,qBAAsC;AAO3D,MAAM,mBAAmB,KAAK;AAC9B,MAAM,UAAU;AAGhB,MAAM,iBAAiB;AAGh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AAM,QAAQ,OAAO,UAAU,IAAI,SAAS,EAAE;AAAA,EAC3D;AACF;",
|
|
6
|
+
"names": ["name", "template"]
|
|
7
|
+
}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export { final, sandboxStateOf, type FinalProviderIface } from "./provider.mjs";
|
|
2
|
+
export { capabilities, limits, regions } from "./limits.mjs";
|
|
3
|
+
export { finalOptionsSchema, type FinalCredentialsIface, type FinalOptionsIface, type FinalProviderOptionsIface, } from "./options.mjs";
|
|
4
|
+
export { MachineSort } from "./vm/machines.mjs";
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { final, sandboxStateOf } from "./provider.mjs";
|
|
2
|
+
import { capabilities, limits, regions } from "./limits.mjs";
|
|
3
|
+
import {
|
|
4
|
+
finalOptionsSchema
|
|
5
|
+
} from "./options.mjs";
|
|
6
|
+
import { MachineSort } from "./vm/machines.mjs";
|
|
7
|
+
export {
|
|
8
|
+
MachineSort,
|
|
9
|
+
capabilities,
|
|
10
|
+
final,
|
|
11
|
+
finalOptionsSchema,
|
|
12
|
+
limits,
|
|
13
|
+
regions,
|
|
14
|
+
sandboxStateOf
|
|
15
|
+
};
|
|
16
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../../../src/index.ts"],
|
|
4
|
+
"sourcesContent": ["export { final, sandboxStateOf, type FinalProviderIface } from \"./provider.mjs\";\nexport { capabilities, limits, regions } from \"./limits.mjs\";\nexport {\n finalOptionsSchema,\n type FinalCredentialsIface,\n type FinalOptionsIface,\n type FinalProviderOptionsIface,\n} from \"./options.mjs\";\nexport { MachineSort } from \"./vm/machines.mjs\";\n"],
|
|
5
|
+
"mappings": "AAAA,SAAS,OAAO,sBAA+C;AAC/D,SAAS,cAAc,QAAQ,eAAe;AAC9C;AAAA,EACE;AAAA,OAIK;AACP,SAAS,mBAAmB;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { Capability, type ProviderLimitsIface } from '@finalai/sandbox-router/provider';
|
|
2
|
+
export declare const PROVIDER = "final";
|
|
3
|
+
export declare const regions: string[];
|
|
4
|
+
export declare const capabilities: Capability[];
|
|
5
|
+
export declare const DEFAULT_DISK_GIB = 10;
|
|
6
|
+
export declare const limits: ProviderLimitsIface;
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { Capability } from "@finalai/sandbox-router/provider";
|
|
2
|
+
const PROVIDER = "final";
|
|
3
|
+
const regions = ["us-west"];
|
|
4
|
+
const capabilities = [
|
|
5
|
+
Capability.Labels,
|
|
6
|
+
Capability.Pause,
|
|
7
|
+
Capability.Resume,
|
|
8
|
+
Capability.Stop,
|
|
9
|
+
Capability.Snapshot,
|
|
10
|
+
Capability.Fork,
|
|
11
|
+
Capability.Pty,
|
|
12
|
+
Capability.Image,
|
|
13
|
+
Capability.NetworkPolicy,
|
|
14
|
+
Capability.PublicPorts,
|
|
15
|
+
Capability.DirectConnect,
|
|
16
|
+
Capability.IdleTimeout,
|
|
17
|
+
Capability.MaxLifetime
|
|
18
|
+
];
|
|
19
|
+
const DEFAULT_DISK_GIB = 10;
|
|
20
|
+
const limits = {
|
|
21
|
+
minCpu: 0.25,
|
|
22
|
+
maxCpu: 32,
|
|
23
|
+
minMemoryMib: 256,
|
|
24
|
+
maxMemoryMib: 65536,
|
|
25
|
+
memoryStepMib: 1,
|
|
26
|
+
maxDiskGib: 2048,
|
|
27
|
+
// Enforced by this adapter: a process that writes more ends with `output_cap`.
|
|
28
|
+
maxOutputBytes: 16 * 1024 * 1024
|
|
29
|
+
};
|
|
30
|
+
export {
|
|
31
|
+
DEFAULT_DISK_GIB,
|
|
32
|
+
PROVIDER,
|
|
33
|
+
capabilities,
|
|
34
|
+
limits,
|
|
35
|
+
regions
|
|
36
|
+
};
|
|
37
|
+
//# sourceMappingURL=limits.mjs.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../../../src/limits.ts"],
|
|
4
|
+
"sourcesContent": ["import { Capability, type ProviderLimitsIface } from '@finalai/sandbox-router/provider';\n\nexport const PROVIDER = 'final';\n\nexport const regions = ['us-west'];\n\nexport const capabilities: Capability[] = [\n Capability.Labels,\n Capability.Pause,\n Capability.Resume,\n Capability.Stop,\n Capability.Snapshot,\n Capability.Fork,\n Capability.Pty,\n Capability.Image,\n Capability.NetworkPolicy,\n Capability.PublicPorts,\n Capability.DirectConnect,\n Capability.IdleTimeout,\n Capability.MaxLifetime,\n];\n\n// The disk a sandbox gets when the spec names none.\nexport const DEFAULT_DISK_GIB = 10;\n\nexport const limits: ProviderLimitsIface = {\n minCpu: 0.25,\n maxCpu: 32,\n minMemoryMib: 256,\n maxMemoryMib: 65_536,\n memoryStepMib: 1,\n maxDiskGib: 2_048,\n // Enforced by this adapter: a process that writes more ends with `output_cap`.\n maxOutputBytes: 16 * 1024 * 1024,\n};\n"],
|
|
5
|
+
"mappings": "AAAA,SAAS,kBAA4C;AAE9C,MAAM,WAAW;AAEjB,MAAM,UAAU,CAAC,SAAS;AAE1B,MAAM,eAA6B;AAAA,EACxC,WAAW;AAAA,EACX,WAAW;AAAA,EACX,WAAW;AAAA,EACX,WAAW;AAAA,EACX,WAAW;AAAA,EACX,WAAW;AAAA,EACX,WAAW;AAAA,EACX,WAAW;AAAA,EACX,WAAW;AAAA,EACX,WAAW;AAAA,EACX,WAAW;AAAA,EACX,WAAW;AAAA,EACX,WAAW;AACb;AAGO,MAAM,mBAAmB;AAEzB,MAAM,SAA8B;AAAA,EACzC,QAAQ;AAAA,EACR,QAAQ;AAAA,EACR,cAAc;AAAA,EACd,cAAc;AAAA,EACd,eAAe;AAAA,EACf,YAAY;AAAA;AAAA,EAEZ,gBAAgB,KAAK,OAAO;AAC9B;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|