supertag-ai 0.1.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 +21 -0
- package/README.md +51 -0
- package/dist/chunk-B3EVEGNZ.js +13 -0
- package/dist/chunk-B3EVEGNZ.js.map +1 -0
- package/dist/chunk-ES42FTPC.js +1068 -0
- package/dist/chunk-ES42FTPC.js.map +1 -0
- package/dist/chunk-L7S2TKUG.js +656 -0
- package/dist/chunk-L7S2TKUG.js.map +1 -0
- package/dist/connectors.d.ts +45 -0
- package/dist/connectors.js +39 -0
- package/dist/connectors.js.map +1 -0
- package/dist/core.d.ts +721 -0
- package/dist/core.js +3 -0
- package/dist/core.js.map +1 -0
- package/dist/environments/e2b.d.ts +69 -0
- package/dist/environments/e2b.js +268 -0
- package/dist/environments/e2b.js.map +1 -0
- package/dist/gateway.d.ts +59 -0
- package/dist/gateway.js +1215 -0
- package/dist/gateway.js.map +1 -0
- package/dist/harnesses/claude-code.d.ts +67 -0
- package/dist/harnesses/claude-code.js +565 -0
- package/dist/harnesses/claude-code.js.map +1 -0
- package/dist/harnesses/codex.d.ts +68 -0
- package/dist/harnesses/codex.js +690 -0
- package/dist/harnesses/codex.js.map +1 -0
- package/dist/index.d.ts +100 -0
- package/dist/index.js +4 -0
- package/dist/index.js.map +1 -0
- package/dist/resources/github.d.ts +80 -0
- package/dist/resources/github.js +374 -0
- package/dist/resources/github.js.map +1 -0
- package/dist/resources/linear.d.ts +25 -0
- package/dist/resources/linear.js +92 -0
- package/dist/resources/linear.js.map +1 -0
- package/dist/resources/slack.d.ts +25 -0
- package/dist/resources/slack.js +98 -0
- package/dist/resources/slack.js.map +1 -0
- package/dist/resources/teams.d.ts +27 -0
- package/dist/resources/teams.js +101 -0
- package/dist/resources/teams.js.map +1 -0
- package/dist/transport.d.ts +5 -0
- package/dist/transport.js +3 -0
- package/dist/transport.js.map +1 -0
- package/dist/worker.d.ts +634 -0
- package/dist/worker.js +3 -0
- package/dist/worker.js.map +1 -0
- package/package.json +127 -0
package/dist/core.js
ADDED
package/dist/core.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"names":[],"mappings":"","file":"core.js"}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { ExecutionEnvironmentProvider, CapabilityIdentifier, ExecutionEnvironmentSpec, ExecutionEnvironment } from '../worker.js';
|
|
2
|
+
import { SandboxOpts } from 'e2b';
|
|
3
|
+
import '../core.js';
|
|
4
|
+
import 'zod';
|
|
5
|
+
|
|
6
|
+
declare const E2B_REMOTE_SANDBOX_CAPABILITY: CapabilityIdentifier;
|
|
7
|
+
interface E2BCommandResultLike {
|
|
8
|
+
readonly exitCode: number;
|
|
9
|
+
readonly stdout: string;
|
|
10
|
+
readonly stderr: string;
|
|
11
|
+
}
|
|
12
|
+
interface E2BCommandHandleLike {
|
|
13
|
+
readonly pid: number;
|
|
14
|
+
wait(): Promise<E2BCommandResultLike>;
|
|
15
|
+
kill(): Promise<boolean>;
|
|
16
|
+
sendStdin(data: string | Uint8Array): Promise<void>;
|
|
17
|
+
closeStdin(): Promise<void>;
|
|
18
|
+
}
|
|
19
|
+
interface E2BCommandOptionsLike {
|
|
20
|
+
readonly background?: boolean;
|
|
21
|
+
readonly cwd?: string;
|
|
22
|
+
readonly envs?: Record<string, string>;
|
|
23
|
+
readonly onStdout?: (data: string) => void | Promise<void>;
|
|
24
|
+
readonly onStderr?: (data: string) => void | Promise<void>;
|
|
25
|
+
readonly stdin?: boolean;
|
|
26
|
+
readonly timeoutMs?: number;
|
|
27
|
+
readonly signal?: AbortSignal;
|
|
28
|
+
}
|
|
29
|
+
/** Minimal E2B surface used by the provider and replaceable by tests. */
|
|
30
|
+
interface E2BSandboxClient {
|
|
31
|
+
readonly sandboxId: string;
|
|
32
|
+
readonly files: {
|
|
33
|
+
makeDir(path: string): Promise<boolean>;
|
|
34
|
+
write(path: string, data: string | ArrayBuffer): Promise<unknown>;
|
|
35
|
+
read(path: string): Promise<string>;
|
|
36
|
+
};
|
|
37
|
+
readonly commands: {
|
|
38
|
+
run(command: string, options: E2BCommandOptionsLike & {
|
|
39
|
+
readonly background: true;
|
|
40
|
+
}): Promise<E2BCommandHandleLike>;
|
|
41
|
+
run(command: string, options?: E2BCommandOptionsLike): Promise<E2BCommandResultLike>;
|
|
42
|
+
};
|
|
43
|
+
kill(): Promise<boolean>;
|
|
44
|
+
}
|
|
45
|
+
interface E2BSandboxFactory {
|
|
46
|
+
create(options: SandboxOpts): Promise<E2BSandboxClient>;
|
|
47
|
+
}
|
|
48
|
+
interface E2BEnvironmentProviderOptions {
|
|
49
|
+
/** E2B template or snapshot name. Defaults to E2B's `base` template. */
|
|
50
|
+
readonly template?: string;
|
|
51
|
+
/** Base directory created inside each sandbox. */
|
|
52
|
+
readonly rootDirectory?: string;
|
|
53
|
+
/** Sandbox lifetime enforced by E2B. */
|
|
54
|
+
readonly sandboxTimeoutMs?: number;
|
|
55
|
+
readonly requestTimeoutMs?: number;
|
|
56
|
+
readonly maxOutputCharacters?: number;
|
|
57
|
+
/** Test/custom-client seam. Authentication must still remain outside Worker Profiles. */
|
|
58
|
+
readonly sandboxFactory?: E2BSandboxFactory;
|
|
59
|
+
}
|
|
60
|
+
/** Provisions one isolated, attempt-scoped E2B sandbox per SuperTag Task Attempt. */
|
|
61
|
+
declare class E2BEnvironmentProvider implements ExecutionEnvironmentProvider {
|
|
62
|
+
#private;
|
|
63
|
+
readonly id = "e2b";
|
|
64
|
+
readonly capabilities: CapabilityIdentifier[];
|
|
65
|
+
constructor(options?: E2BEnvironmentProviderOptions);
|
|
66
|
+
provision(spec: ExecutionEnvironmentSpec, signal?: AbortSignal): Promise<ExecutionEnvironment>;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export { E2BEnvironmentProvider, type E2BEnvironmentProviderOptions, type E2BSandboxClient, type E2BSandboxFactory, E2B_REMOTE_SANDBOX_CAPABILITY };
|
|
@@ -0,0 +1,268 @@
|
|
|
1
|
+
import { lstat, readdir, readFile } from 'fs/promises';
|
|
2
|
+
import { posix, isAbsolute, join } from 'path';
|
|
3
|
+
import { fileURLToPath } from 'url';
|
|
4
|
+
import { Sandbox, CommandExitError } from 'e2b';
|
|
5
|
+
|
|
6
|
+
// ../runtime/environments/e2b/src/index.ts
|
|
7
|
+
var E2B_REMOTE_SANDBOX_CAPABILITY = {
|
|
8
|
+
id: "supertag.execution.remote-sandbox.e2b",
|
|
9
|
+
version: 1
|
|
10
|
+
};
|
|
11
|
+
var defaultSandboxFactory = {
|
|
12
|
+
async create(options) {
|
|
13
|
+
return await Sandbox.create(options);
|
|
14
|
+
}
|
|
15
|
+
};
|
|
16
|
+
var E2BEnvironmentProvider = class {
|
|
17
|
+
id = "e2b";
|
|
18
|
+
capabilities = [E2B_REMOTE_SANDBOX_CAPABILITY];
|
|
19
|
+
#options;
|
|
20
|
+
constructor(options = {}) {
|
|
21
|
+
this.#options = options;
|
|
22
|
+
}
|
|
23
|
+
async provision(spec, signal) {
|
|
24
|
+
if (spec.profile.execution.lifetime.scope !== "attempt") {
|
|
25
|
+
throw new Error("The E2B provider currently supports only attempt-scoped environments");
|
|
26
|
+
}
|
|
27
|
+
throwIfAborted(signal);
|
|
28
|
+
const createOptions = {
|
|
29
|
+
metadata: {
|
|
30
|
+
"supertag.taskId": spec.task.taskId,
|
|
31
|
+
"supertag.attemptId": spec.task.attemptId,
|
|
32
|
+
"supertag.tagId": spec.task.tag.id
|
|
33
|
+
},
|
|
34
|
+
...networkOptions(spec.plan.network),
|
|
35
|
+
...this.#options.template === void 0 ? {} : { template: this.#options.template },
|
|
36
|
+
...this.#options.sandboxTimeoutMs === void 0 ? {} : { timeoutMs: this.#options.sandboxTimeoutMs },
|
|
37
|
+
...this.#options.requestTimeoutMs === void 0 ? {} : { requestTimeoutMs: this.#options.requestTimeoutMs },
|
|
38
|
+
...signal === void 0 ? {} : { signal }
|
|
39
|
+
};
|
|
40
|
+
const sandbox = await (this.#options.sandboxFactory ?? defaultSandboxFactory).create(
|
|
41
|
+
createOptions
|
|
42
|
+
);
|
|
43
|
+
if (signal?.aborted === true) {
|
|
44
|
+
await sandbox.kill().catch(() => void 0);
|
|
45
|
+
throw abortError(signal);
|
|
46
|
+
}
|
|
47
|
+
const base = normalizeRoot(this.#options.rootDirectory ?? "/home/user/supertag-tasks");
|
|
48
|
+
const root = posix.join(
|
|
49
|
+
base,
|
|
50
|
+
safePathSegment(spec.task.taskId),
|
|
51
|
+
"attempts",
|
|
52
|
+
safePathSegment(spec.task.attemptId)
|
|
53
|
+
);
|
|
54
|
+
const environment = new E2BExecutionEnvironment(sandbox, root, this.#options);
|
|
55
|
+
try {
|
|
56
|
+
await sandbox.files.makeDir(root);
|
|
57
|
+
} catch (error) {
|
|
58
|
+
await sandbox.kill().catch(() => void 0);
|
|
59
|
+
throw error;
|
|
60
|
+
}
|
|
61
|
+
return environment;
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
var E2BExecutionEnvironment = class {
|
|
65
|
+
id;
|
|
66
|
+
placement = "remote";
|
|
67
|
+
rootDirectory;
|
|
68
|
+
#sandbox;
|
|
69
|
+
#options;
|
|
70
|
+
#disposed = false;
|
|
71
|
+
constructor(sandbox, rootDirectory, options) {
|
|
72
|
+
this.id = `e2b:${sandbox.sandboxId}`;
|
|
73
|
+
this.rootDirectory = rootDirectory;
|
|
74
|
+
this.#sandbox = sandbox;
|
|
75
|
+
this.#options = options;
|
|
76
|
+
}
|
|
77
|
+
async prepare(plan) {
|
|
78
|
+
this.#assertOpen();
|
|
79
|
+
if (plan.secretBindings.length > 0) {
|
|
80
|
+
throw new Error(
|
|
81
|
+
"The E2B provider requires a Worker-local Secret Resolver before secret bindings can be used"
|
|
82
|
+
);
|
|
83
|
+
}
|
|
84
|
+
for (const file of plan.files) {
|
|
85
|
+
const destination = this.resolvePath(file.path);
|
|
86
|
+
const content = typeof file.content === "string" ? file.content : new Uint8Array(file.content).buffer;
|
|
87
|
+
await this.#sandbox.files.write(destination, content);
|
|
88
|
+
if (file.mode !== void 0) {
|
|
89
|
+
await this.#sandbox.commands.run(
|
|
90
|
+
`chmod ${shellQuote(file.mode.toString(8))} ${shellQuote(destination)}`
|
|
91
|
+
);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
const readOnlyMounts = [];
|
|
95
|
+
for (const mount of plan.mounts) {
|
|
96
|
+
const destination = this.resolvePath(mount.destination);
|
|
97
|
+
await stageLocalResource(this.#sandbox, localFileResourcePath(mount.source.uri), destination);
|
|
98
|
+
if (mount.readOnly) readOnlyMounts.push(destination);
|
|
99
|
+
}
|
|
100
|
+
for (const destination of readOnlyMounts.toReversed()) {
|
|
101
|
+
const result = await this.#sandbox.commands.run(`chmod -R a-w ${shellQuote(destination)}`);
|
|
102
|
+
if (result.exitCode !== 0) {
|
|
103
|
+
throw new Error(`Failed to make E2B mount read-only: ${result.stderr}`);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
resolvePath(path) {
|
|
108
|
+
this.#assertOpen();
|
|
109
|
+
const destination = isAbsolute(path) ? posix.normalize(path) : posix.resolve(this.rootDirectory, path);
|
|
110
|
+
const traversal = posix.relative(this.rootDirectory, destination);
|
|
111
|
+
if (traversal.startsWith("..") || isAbsolute(traversal)) {
|
|
112
|
+
throw new Error(`Environment path escapes its root: ${path}`);
|
|
113
|
+
}
|
|
114
|
+
return destination;
|
|
115
|
+
}
|
|
116
|
+
async read(path) {
|
|
117
|
+
this.#assertOpen();
|
|
118
|
+
return await this.#sandbox.files.read(this.resolvePath(path));
|
|
119
|
+
}
|
|
120
|
+
async spawn(process, signal) {
|
|
121
|
+
this.#assertOpen();
|
|
122
|
+
throwIfAborted(signal);
|
|
123
|
+
const maximum = this.#options.maxOutputCharacters ?? 32e3;
|
|
124
|
+
let stdout = "";
|
|
125
|
+
let stderr = "";
|
|
126
|
+
const handle = await this.#sandbox.commands.run(renderCommand(process), {
|
|
127
|
+
background: true,
|
|
128
|
+
cwd: this.resolvePath(process.cwd ?? "."),
|
|
129
|
+
...process.environment === void 0 ? {} : { envs: { ...process.environment } },
|
|
130
|
+
...process.stdin === void 0 ? {} : { stdin: true },
|
|
131
|
+
...process.timeoutMs === void 0 ? {} : { timeoutMs: process.timeoutMs },
|
|
132
|
+
...signal === void 0 ? {} : { signal },
|
|
133
|
+
onStdout(data) {
|
|
134
|
+
stdout = appendBounded(stdout, data, maximum);
|
|
135
|
+
},
|
|
136
|
+
onStderr(data) {
|
|
137
|
+
stderr = appendBounded(stderr, data, maximum);
|
|
138
|
+
}
|
|
139
|
+
});
|
|
140
|
+
if (process.stdin !== void 0) {
|
|
141
|
+
await handle.sendStdin(process.stdin);
|
|
142
|
+
await handle.closeStdin();
|
|
143
|
+
}
|
|
144
|
+
const abort = () => {
|
|
145
|
+
void handle.kill().catch(() => void 0);
|
|
146
|
+
};
|
|
147
|
+
signal?.addEventListener("abort", abort, { once: true });
|
|
148
|
+
const wait = async () => {
|
|
149
|
+
try {
|
|
150
|
+
const result = await handle.wait();
|
|
151
|
+
throwIfAborted(signal);
|
|
152
|
+
return commandResult(result, stdout, stderr, maximum);
|
|
153
|
+
} catch (error) {
|
|
154
|
+
if (signal?.aborted === true) throw abortError(signal);
|
|
155
|
+
if (error instanceof CommandExitError) {
|
|
156
|
+
return commandResult(error, stdout, stderr, maximum);
|
|
157
|
+
}
|
|
158
|
+
throw error;
|
|
159
|
+
} finally {
|
|
160
|
+
signal?.removeEventListener("abort", abort);
|
|
161
|
+
}
|
|
162
|
+
};
|
|
163
|
+
return {
|
|
164
|
+
pid: handle.pid,
|
|
165
|
+
wait,
|
|
166
|
+
kill: () => {
|
|
167
|
+
void handle.kill().catch(() => void 0);
|
|
168
|
+
}
|
|
169
|
+
};
|
|
170
|
+
}
|
|
171
|
+
async dispose() {
|
|
172
|
+
if (this.#disposed) return;
|
|
173
|
+
await this.#sandbox.kill();
|
|
174
|
+
this.#disposed = true;
|
|
175
|
+
}
|
|
176
|
+
#assertOpen() {
|
|
177
|
+
if (this.#disposed) {
|
|
178
|
+
throw new Error(`Execution Environment ${this.id} has been disposed`);
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
};
|
|
182
|
+
function networkOptions(network) {
|
|
183
|
+
switch (network.mode) {
|
|
184
|
+
case "none":
|
|
185
|
+
return { allowInternetAccess: false };
|
|
186
|
+
case "unrestricted":
|
|
187
|
+
return { allowInternetAccess: true };
|
|
188
|
+
case "allowlist": {
|
|
189
|
+
const hosts = network.allowedHosts ?? [];
|
|
190
|
+
if (hosts.length === 0) {
|
|
191
|
+
throw new Error("An E2B network allowlist must contain at least one host");
|
|
192
|
+
}
|
|
193
|
+
return { network: { allowOut: [...hosts] } };
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
function localFileResourcePath(uri) {
|
|
198
|
+
let url;
|
|
199
|
+
try {
|
|
200
|
+
url = new URL(uri);
|
|
201
|
+
} catch {
|
|
202
|
+
throw new Error(`Resource ${uri} is not a valid URI`);
|
|
203
|
+
}
|
|
204
|
+
if (url.protocol !== "file:") {
|
|
205
|
+
throw new Error(
|
|
206
|
+
`The E2B provider can currently stage only file: resources, received ${url.protocol}`
|
|
207
|
+
);
|
|
208
|
+
}
|
|
209
|
+
return fileURLToPath(url);
|
|
210
|
+
}
|
|
211
|
+
async function stageLocalResource(sandbox, source, destination) {
|
|
212
|
+
const status = await lstat(source);
|
|
213
|
+
if (status.isSymbolicLink()) {
|
|
214
|
+
throw new Error(`Workspace resource contains unsupported symbolic link ${source}`);
|
|
215
|
+
}
|
|
216
|
+
if (status.isDirectory()) {
|
|
217
|
+
await sandbox.files.makeDir(destination);
|
|
218
|
+
for (const entry of await readdir(source)) {
|
|
219
|
+
await stageLocalResource(sandbox, join(source, entry), posix.join(destination, entry));
|
|
220
|
+
}
|
|
221
|
+
return;
|
|
222
|
+
}
|
|
223
|
+
if (!status.isFile()) throw new Error(`Workspace resource contains unsupported entry ${source}`);
|
|
224
|
+
const content = await readFile(source);
|
|
225
|
+
await sandbox.files.write(
|
|
226
|
+
destination,
|
|
227
|
+
content.buffer.slice(content.byteOffset, content.byteOffset + content.byteLength)
|
|
228
|
+
);
|
|
229
|
+
}
|
|
230
|
+
function renderCommand(process) {
|
|
231
|
+
return [process.command, ...process.args ?? []].map(shellQuote).join(" ");
|
|
232
|
+
}
|
|
233
|
+
function shellQuote(value) {
|
|
234
|
+
return `'${value.replaceAll("'", `'"'"'`)}'`;
|
|
235
|
+
}
|
|
236
|
+
function normalizeRoot(root) {
|
|
237
|
+
const normalized = posix.normalize(root);
|
|
238
|
+
if (!posix.isAbsolute(normalized) || normalized === "/") {
|
|
239
|
+
throw new Error("The E2B root directory must be an absolute non-root path");
|
|
240
|
+
}
|
|
241
|
+
return normalized;
|
|
242
|
+
}
|
|
243
|
+
function safePathSegment(value) {
|
|
244
|
+
const safe = value.replaceAll(/[^a-zA-Z0-9_.-]/g, "-").slice(0, 80);
|
|
245
|
+
return safe.length === 0 ? "task" : safe;
|
|
246
|
+
}
|
|
247
|
+
function commandResult(result, streamedStdout, streamedStderr, maximum) {
|
|
248
|
+
return {
|
|
249
|
+
exitCode: result.exitCode,
|
|
250
|
+
stdout: streamedStdout || appendBounded("", result.stdout, maximum),
|
|
251
|
+
stderr: streamedStderr || appendBounded("", result.stderr, maximum)
|
|
252
|
+
};
|
|
253
|
+
}
|
|
254
|
+
function appendBounded(current, chunk, limit) {
|
|
255
|
+
const combined = current + chunk;
|
|
256
|
+
return combined.length <= limit ? combined : combined.slice(combined.length - limit);
|
|
257
|
+
}
|
|
258
|
+
function throwIfAborted(signal) {
|
|
259
|
+
if (signal?.aborted === true) throw abortError(signal);
|
|
260
|
+
}
|
|
261
|
+
function abortError(signal) {
|
|
262
|
+
const reason = signal?.reason;
|
|
263
|
+
return reason instanceof Error ? reason : new Error(reason === void 0 ? "Task Attempt cancelled" : String(reason));
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
export { E2BEnvironmentProvider, E2B_REMOTE_SANDBOX_CAPABILITY };
|
|
267
|
+
//# sourceMappingURL=e2b.js.map
|
|
268
|
+
//# sourceMappingURL=e2b.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../runtime/environments/e2b/src/index.ts"],"names":[],"mappings":";;;;;;AAeO,IAAM,6BAAA,GAAsD;AAAA,EACjE,EAAA,EAAI,uCAAA;AAAA,EACJ,OAAA,EAAS;AACX;AA8DA,IAAM,qBAAA,GAA2C;AAAA,EAC/C,MAAM,OAAO,OAAA,EAAS;AAIpB,IAAA,OAAO,MAAM,OAAA,CAAQ,MAAA,CAAO,OAAO,CAAA;AAAA,EACrC;AACF,CAAA;AAGO,IAAM,yBAAN,MAAqE;AAAA,EACjE,EAAA,GAAK,KAAA;AAAA,EACL,YAAA,GAAe,CAAC,6BAA6B,CAAA;AAAA,EAC7C,QAAA;AAAA,EAET,WAAA,CAAY,OAAA,GAAyC,EAAC,EAAG;AACvD,IAAA,IAAA,CAAK,QAAA,GAAW,OAAA;AAAA,EAClB;AAAA,EAEA,MAAM,SAAA,CACJ,IAAA,EACA,MAAA,EAC+B;AAC/B,IAAA,IAAI,IAAA,CAAK,OAAA,CAAQ,SAAA,CAAU,QAAA,CAAS,UAAU,SAAA,EAAW;AACvD,MAAA,MAAM,IAAI,MAAM,sEAAsE,CAAA;AAAA,IACxF;AACA,IAAA,cAAA,CAAe,MAAM,CAAA;AACrB,IAAA,MAAM,aAAA,GAA6B;AAAA,MACjC,QAAA,EAAU;AAAA,QACR,iBAAA,EAAmB,KAAK,IAAA,CAAK,MAAA;AAAA,QAC7B,oBAAA,EAAsB,KAAK,IAAA,CAAK,SAAA;AAAA,QAChC,gBAAA,EAAkB,IAAA,CAAK,IAAA,CAAK,GAAA,CAAI;AAAA,OAClC;AAAA,MACA,GAAG,cAAA,CAAe,IAAA,CAAK,IAAA,CAAK,OAAO,CAAA;AAAA,MACnC,GAAI,IAAA,CAAK,QAAA,CAAS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AAEA,SAAS,aAAA,CAAc,OAAA,EAAiB,KAAA,EAAe,KAAA,EAAuB;AAC5E,EAAA,MAAM,WAAW,OAAA,GAAU,KAAA;AAC3B,EAAA,OAAO,QAAA,CAAS,UAAU,KAAA,GAAQ,QAAA,GAAW,SAAS,KAAA,CAAM,QAAA,CAAS,SAAS,KAAK,CAAA;AACrF;AAEA,SAAS,eAAe,MAAA,EAA4B;AAClD,EAAA,IAAI,MAAA,EAAQ,OAAA,KAAY,IAAA,EAAM,MAAM,WAAW,MAAM,CAAA;AACvD;AAEA,SAAS,WAAW,MAAA,EAA6B;AAC/C,EAAA,MAAM,SAAS,MAAA,EAAQ,MAAA;AACvB,EAAA,OAAO,MAAA,YAAkB,KAAA,GACrB,MAAA,GACA,IAAI,KAAA,CAAM,WAAW,MAAA,GAAY,wBAAA,GAA2B,MAAA,CAAO,MAAM,CAAC,CAAA;AAChF","file":"e2b.js","sourcesContent":["import { lstat, readdir, readFile } from \"node:fs/promises\";\nimport { isAbsolute, join, posix } from \"node:path\";\nimport { fileURLToPath } from \"node:url\";\nimport type {\n CapabilityIdentifier,\n ExecutionEnvironment,\n ExecutionEnvironmentProvider,\n ExecutionEnvironmentSpec,\n HarnessPlan,\n ProcessHandle,\n ProcessResult,\n ProcessSpec,\n} from \"@supertag/worker-sdk\";\nimport { CommandExitError, Sandbox, type SandboxOpts } from \"e2b\";\n\nexport const E2B_REMOTE_SANDBOX_CAPABILITY: CapabilityIdentifier = {\n id: \"supertag.execution.remote-sandbox.e2b\",\n version: 1,\n};\n\ninterface E2BCommandResultLike {\n readonly exitCode: number;\n readonly stdout: string;\n readonly stderr: string;\n}\n\ninterface E2BCommandHandleLike {\n readonly pid: number;\n wait(): Promise<E2BCommandResultLike>;\n kill(): Promise<boolean>;\n sendStdin(data: string | Uint8Array): Promise<void>;\n closeStdin(): Promise<void>;\n}\n\ninterface E2BCommandOptionsLike {\n readonly background?: boolean;\n readonly cwd?: string;\n readonly envs?: Record<string, string>;\n readonly onStdout?: (data: string) => void | Promise<void>;\n readonly onStderr?: (data: string) => void | Promise<void>;\n readonly stdin?: boolean;\n readonly timeoutMs?: number;\n readonly signal?: AbortSignal;\n}\n\n/** Minimal E2B surface used by the provider and replaceable by tests. */\nexport interface E2BSandboxClient {\n readonly sandboxId: string;\n readonly files: {\n makeDir(path: string): Promise<boolean>;\n write(path: string, data: string | ArrayBuffer): Promise<unknown>;\n read(path: string): Promise<string>;\n };\n readonly commands: {\n run(\n command: string,\n options: E2BCommandOptionsLike & { readonly background: true },\n ): Promise<E2BCommandHandleLike>;\n run(command: string, options?: E2BCommandOptionsLike): Promise<E2BCommandResultLike>;\n };\n kill(): Promise<boolean>;\n}\n\nexport interface E2BSandboxFactory {\n create(options: SandboxOpts): Promise<E2BSandboxClient>;\n}\n\nexport interface E2BEnvironmentProviderOptions {\n /** E2B template or snapshot name. Defaults to E2B's `base` template. */\n readonly template?: string;\n /** Base directory created inside each sandbox. */\n readonly rootDirectory?: string;\n /** Sandbox lifetime enforced by E2B. */\n readonly sandboxTimeoutMs?: number;\n readonly requestTimeoutMs?: number;\n readonly maxOutputCharacters?: number;\n /** Test/custom-client seam. Authentication must still remain outside Worker Profiles. */\n readonly sandboxFactory?: E2BSandboxFactory;\n}\n\nconst defaultSandboxFactory: E2BSandboxFactory = {\n async create(options) {\n // E2B resolves authentication exclusively from E2B_API_KEY (or its other\n // documented environment variables). Provider configuration never accepts\n // or transports an API key.\n return await Sandbox.create(options);\n },\n};\n\n/** Provisions one isolated, attempt-scoped E2B sandbox per SuperTag Task Attempt. */\nexport class E2BEnvironmentProvider implements ExecutionEnvironmentProvider {\n readonly id = \"e2b\";\n readonly capabilities = [E2B_REMOTE_SANDBOX_CAPABILITY];\n readonly #options: E2BEnvironmentProviderOptions;\n\n constructor(options: E2BEnvironmentProviderOptions = {}) {\n this.#options = options;\n }\n\n async provision(\n spec: ExecutionEnvironmentSpec,\n signal?: AbortSignal,\n ): Promise<ExecutionEnvironment> {\n if (spec.profile.execution.lifetime.scope !== \"attempt\") {\n throw new Error(\"The E2B provider currently supports only attempt-scoped environments\");\n }\n throwIfAborted(signal);\n const createOptions: SandboxOpts = {\n metadata: {\n \"supertag.taskId\": spec.task.taskId,\n \"supertag.attemptId\": spec.task.attemptId,\n \"supertag.tagId\": spec.task.tag.id,\n },\n ...networkOptions(spec.plan.network),\n ...(this.#options.template === undefined ? {} : { template: this.#options.template }),\n ...(this.#options.sandboxTimeoutMs === undefined\n ? {}\n : { timeoutMs: this.#options.sandboxTimeoutMs }),\n ...(this.#options.requestTimeoutMs === undefined\n ? {}\n : { requestTimeoutMs: this.#options.requestTimeoutMs }),\n ...(signal === undefined ? {} : { signal }),\n };\n const sandbox = await (this.#options.sandboxFactory ?? defaultSandboxFactory).create(\n createOptions,\n );\n if (signal?.aborted === true) {\n await sandbox.kill().catch(() => undefined);\n throw abortError(signal);\n }\n const base = normalizeRoot(this.#options.rootDirectory ?? \"/home/user/supertag-tasks\");\n const root = posix.join(\n base,\n safePathSegment(spec.task.taskId),\n \"attempts\",\n safePathSegment(spec.task.attemptId),\n );\n const environment = new E2BExecutionEnvironment(sandbox, root, this.#options);\n try {\n await sandbox.files.makeDir(root);\n } catch (error) {\n await sandbox.kill().catch(() => undefined);\n throw error;\n }\n return environment;\n }\n}\n\nclass E2BExecutionEnvironment implements ExecutionEnvironment {\n readonly id: string;\n readonly placement = \"remote\" as const;\n readonly rootDirectory: string;\n readonly #sandbox: E2BSandboxClient;\n readonly #options: E2BEnvironmentProviderOptions;\n #disposed = false;\n\n constructor(\n sandbox: E2BSandboxClient,\n rootDirectory: string,\n options: E2BEnvironmentProviderOptions,\n ) {\n this.id = `e2b:${sandbox.sandboxId}`;\n this.rootDirectory = rootDirectory;\n this.#sandbox = sandbox;\n this.#options = options;\n }\n\n async prepare(plan: HarnessPlan): Promise<void> {\n this.#assertOpen();\n if (plan.secretBindings.length > 0) {\n throw new Error(\n \"The E2B provider requires a Worker-local Secret Resolver before secret bindings can be used\",\n );\n }\n for (const file of plan.files) {\n const destination = this.resolvePath(file.path);\n const content =\n typeof file.content === \"string\" ? file.content : new Uint8Array(file.content).buffer;\n await this.#sandbox.files.write(destination, content);\n if (file.mode !== undefined) {\n await this.#sandbox.commands.run(\n `chmod ${shellQuote(file.mode.toString(8))} ${shellQuote(destination)}`,\n );\n }\n }\n const readOnlyMounts: string[] = [];\n for (const mount of plan.mounts) {\n const destination = this.resolvePath(mount.destination);\n await stageLocalResource(this.#sandbox, localFileResourcePath(mount.source.uri), destination);\n if (mount.readOnly) readOnlyMounts.push(destination);\n }\n for (const destination of readOnlyMounts.toReversed()) {\n const result = await this.#sandbox.commands.run(`chmod -R a-w ${shellQuote(destination)}`);\n if (result.exitCode !== 0) {\n throw new Error(`Failed to make E2B mount read-only: ${result.stderr}`);\n }\n }\n }\n\n resolvePath(path: string): string {\n this.#assertOpen();\n const destination = isAbsolute(path)\n ? posix.normalize(path)\n : posix.resolve(this.rootDirectory, path);\n const traversal = posix.relative(this.rootDirectory, destination);\n if (traversal.startsWith(\"..\") || isAbsolute(traversal)) {\n throw new Error(`Environment path escapes its root: ${path}`);\n }\n return destination;\n }\n\n async read(path: string): Promise<string> {\n this.#assertOpen();\n return await this.#sandbox.files.read(this.resolvePath(path));\n }\n\n async spawn(process: ProcessSpec, signal?: AbortSignal): Promise<ProcessHandle> {\n this.#assertOpen();\n throwIfAborted(signal);\n const maximum = this.#options.maxOutputCharacters ?? 32_000;\n let stdout = \"\";\n let stderr = \"\";\n const handle = await this.#sandbox.commands.run(renderCommand(process), {\n background: true,\n cwd: this.resolvePath(process.cwd ?? \".\"),\n ...(process.environment === undefined ? {} : { envs: { ...process.environment } }),\n ...(process.stdin === undefined ? {} : { stdin: true }),\n ...(process.timeoutMs === undefined ? {} : { timeoutMs: process.timeoutMs }),\n ...(signal === undefined ? {} : { signal }),\n onStdout(data) {\n stdout = appendBounded(stdout, data, maximum);\n },\n onStderr(data) {\n stderr = appendBounded(stderr, data, maximum);\n },\n });\n if (process.stdin !== undefined) {\n await handle.sendStdin(process.stdin);\n await handle.closeStdin();\n }\n const abort = () => {\n void handle.kill().catch(() => undefined);\n };\n signal?.addEventListener(\"abort\", abort, { once: true });\n const wait = async (): Promise<ProcessResult> => {\n try {\n const result = await handle.wait();\n throwIfAborted(signal);\n return commandResult(result, stdout, stderr, maximum);\n } catch (error) {\n if (signal?.aborted === true) throw abortError(signal);\n if (error instanceof CommandExitError) {\n return commandResult(error, stdout, stderr, maximum);\n }\n throw error;\n } finally {\n signal?.removeEventListener(\"abort\", abort);\n }\n };\n return {\n pid: handle.pid,\n wait,\n kill: () => {\n void handle.kill().catch(() => undefined);\n },\n };\n }\n\n async dispose(): Promise<void> {\n if (this.#disposed) return;\n await this.#sandbox.kill();\n this.#disposed = true;\n }\n\n #assertOpen(): void {\n if (this.#disposed) {\n throw new Error(`Execution Environment ${this.id} has been disposed`);\n }\n }\n}\n\nfunction networkOptions(\n network: HarnessPlan[\"network\"],\n): Pick<SandboxOpts, \"allowInternetAccess\" | \"network\"> {\n switch (network.mode) {\n case \"none\":\n return { allowInternetAccess: false };\n case \"unrestricted\":\n return { allowInternetAccess: true };\n case \"allowlist\": {\n const hosts = network.allowedHosts ?? [];\n if (hosts.length === 0) {\n throw new Error(\"An E2B network allowlist must contain at least one host\");\n }\n return { network: { allowOut: [...hosts] } };\n }\n }\n}\n\nfunction localFileResourcePath(uri: string): string {\n let url: URL;\n try {\n url = new URL(uri);\n } catch {\n throw new Error(`Resource ${uri} is not a valid URI`);\n }\n if (url.protocol !== \"file:\") {\n throw new Error(\n `The E2B provider can currently stage only file: resources, received ${url.protocol}`,\n );\n }\n return fileURLToPath(url);\n}\n\nasync function stageLocalResource(\n sandbox: E2BSandboxClient,\n source: string,\n destination: string,\n): Promise<void> {\n const status = await lstat(source);\n if (status.isSymbolicLink()) {\n throw new Error(`Workspace resource contains unsupported symbolic link ${source}`);\n }\n if (status.isDirectory()) {\n await sandbox.files.makeDir(destination);\n for (const entry of await readdir(source)) {\n await stageLocalResource(sandbox, join(source, entry), posix.join(destination, entry));\n }\n return;\n }\n if (!status.isFile()) throw new Error(`Workspace resource contains unsupported entry ${source}`);\n const content = await readFile(source);\n await sandbox.files.write(\n destination,\n content.buffer.slice(content.byteOffset, content.byteOffset + content.byteLength),\n );\n}\n\nfunction renderCommand(process: ProcessSpec): string {\n return [process.command, ...(process.args ?? [])].map(shellQuote).join(\" \");\n}\n\nfunction shellQuote(value: string): string {\n return `'${value.replaceAll(\"'\", `'\"'\"'`)}'`;\n}\n\nfunction normalizeRoot(root: string): string {\n const normalized = posix.normalize(root);\n if (!posix.isAbsolute(normalized) || normalized === \"/\") {\n throw new Error(\"The E2B root directory must be an absolute non-root path\");\n }\n return normalized;\n}\n\nfunction safePathSegment(value: string): string {\n const safe = value.replaceAll(/[^a-zA-Z0-9_.-]/g, \"-\").slice(0, 80);\n return safe.length === 0 ? \"task\" : safe;\n}\n\nfunction commandResult(\n result: E2BCommandResultLike,\n streamedStdout: string,\n streamedStderr: string,\n maximum: number,\n): ProcessResult {\n return {\n exitCode: result.exitCode,\n stdout: streamedStdout || appendBounded(\"\", result.stdout, maximum),\n stderr: streamedStderr || appendBounded(\"\", result.stderr, maximum),\n };\n}\n\nfunction appendBounded(current: string, chunk: string, limit: number): string {\n const combined = current + chunk;\n return combined.length <= limit ? combined : combined.slice(combined.length - limit);\n}\n\nfunction throwIfAborted(signal?: AbortSignal): void {\n if (signal?.aborted === true) throw abortError(signal);\n}\n\nfunction abortError(signal?: AbortSignal): Error {\n const reason = signal?.reason;\n return reason instanceof Error\n ? reason\n : new Error(reason === undefined ? \"Task Attempt cancelled\" : String(reason));\n}\n"]}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { ConnectorPresentation, VerifiedConnectorAccount } from './connectors.js';
|
|
2
|
+
import { Store, GatewayCredentialVault, InboundConnectorEvent, ConnectorAccount } from './core.js';
|
|
3
|
+
import { WorkerHost } from './worker.js';
|
|
4
|
+
import 'mdast';
|
|
5
|
+
import 'zod';
|
|
6
|
+
|
|
7
|
+
type InboundEventHandler = (event: InboundConnectorEvent, account: ConnectorAccount, route: InboundRoute) => Promise<void>;
|
|
8
|
+
interface InboundRoute {
|
|
9
|
+
readonly tagPresenceId: string;
|
|
10
|
+
readonly tagId: string;
|
|
11
|
+
}
|
|
12
|
+
type OutboundReplyHandler = (event: InboundConnectorEvent, presentation: ConnectorPresentation, account: ConnectorAccount) => Promise<void>;
|
|
13
|
+
interface InboundIngestResult {
|
|
14
|
+
readonly status: "accepted" | "duplicate" | "ignored" | "unconfigured";
|
|
15
|
+
readonly connectorAccountId: string;
|
|
16
|
+
readonly eventId: string;
|
|
17
|
+
readonly inboundEventId?: string;
|
|
18
|
+
readonly taskId?: string;
|
|
19
|
+
readonly tagPresenceId?: string;
|
|
20
|
+
readonly tagId?: string;
|
|
21
|
+
}
|
|
22
|
+
interface GatewayDispatchOptions {
|
|
23
|
+
readonly instanceId?: string;
|
|
24
|
+
readonly pollIntervalMs?: number;
|
|
25
|
+
readonly leaseDurationMs?: number;
|
|
26
|
+
readonly retryBaseMs?: number;
|
|
27
|
+
readonly maxAttempts?: number;
|
|
28
|
+
readonly onDispatchError?: (error: unknown) => void;
|
|
29
|
+
}
|
|
30
|
+
interface GatewayDependencies {
|
|
31
|
+
readonly store: Store;
|
|
32
|
+
readonly credentialVault: GatewayCredentialVault;
|
|
33
|
+
readonly onInboundEvent?: InboundEventHandler;
|
|
34
|
+
readonly worker?: WorkerHost;
|
|
35
|
+
readonly reply?: OutboundReplyHandler;
|
|
36
|
+
readonly dispatch?: GatewayDispatchOptions;
|
|
37
|
+
}
|
|
38
|
+
interface GatewayControllerStatus {
|
|
39
|
+
readonly name: "task-dispatch" | "reply-delivery";
|
|
40
|
+
readonly phase: "disabled" | "running" | "draining" | "stopped";
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* The Gateway is the long-running service boundary. Provider callbacks only return
|
|
44
|
+
* after admission is durable; Worker execution and reply delivery are recovered by
|
|
45
|
+
* the background dispatcher if the process is interrupted.
|
|
46
|
+
*/
|
|
47
|
+
declare class Gateway {
|
|
48
|
+
#private;
|
|
49
|
+
constructor(dependencies: GatewayDependencies);
|
|
50
|
+
get isAccepting(): boolean;
|
|
51
|
+
get isRunning(): boolean;
|
|
52
|
+
get controllerStatuses(): readonly GatewayControllerStatus[];
|
|
53
|
+
start(): Promise<void>;
|
|
54
|
+
stop(): Promise<void>;
|
|
55
|
+
setupConnectorAccount(verified: VerifiedConnectorAccount): Promise<ConnectorAccount>;
|
|
56
|
+
ingestConnectorEvent(event: InboundConnectorEvent): Promise<InboundIngestResult>;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export { Gateway, type GatewayControllerStatus, type GatewayDependencies, type GatewayDispatchOptions, type InboundEventHandler, type InboundIngestResult, type InboundRoute, type OutboundReplyHandler };
|